2 * Copyright (C) 2000 Red Hat, Inc. Jonathan Blandford
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #include "gtkcellrenderer.h"
23 #include "gtkmarshalers.h"
24 #include "gtkprivate.h"
25 #include "gtktreeprivate.h"
28 static void gtk_cell_renderer_get_property (GObject *object,
32 static void gtk_cell_renderer_set_property (GObject *object,
36 static void set_cell_bg_color (GtkCellRenderer *cell,
40 #define GTK_CELL_RENDERER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER, GtkCellRendererPrivate))
42 typedef struct _GtkCellRendererPrivate GtkCellRendererPrivate;
43 struct _GtkCellRendererPrivate
45 GdkColor cell_background;
63 PROP_CELL_BACKGROUND_GDK,
64 PROP_CELL_BACKGROUND_SET
74 static guint cell_renderer_signals[LAST_SIGNAL] = { 0 };
76 G_DEFINE_ABSTRACT_TYPE (GtkCellRenderer, gtk_cell_renderer, GTK_TYPE_OBJECT)
79 gtk_cell_renderer_init (GtkCellRenderer *cell)
81 cell->mode = GTK_CELL_RENDERER_MODE_INERT;
89 cell->sensitive = TRUE;
90 cell->is_expander = FALSE;
91 cell->is_expanded = FALSE;
92 cell->editing = FALSE;
96 gtk_cell_renderer_class_init (GtkCellRendererClass *class)
98 GObjectClass *object_class = G_OBJECT_CLASS (class);
100 object_class->get_property = gtk_cell_renderer_get_property;
101 object_class->set_property = gtk_cell_renderer_set_property;
103 class->render = NULL;
104 class->get_size = NULL;
107 * GtkCellRenderer::editing-canceled:
108 * @renderer: the object which received the signal
110 * This signal gets emitted when the user cancels the process of editing a
111 * cell. For example, an editable cell renderer could be written to cancel
112 * editing when the user presses Escape.
114 * See also: gtk_cell_renderer_stop_editing().
118 cell_renderer_signals[EDITING_CANCELED] =
119 g_signal_new (I_("editing-canceled"),
120 G_OBJECT_CLASS_TYPE (object_class),
122 G_STRUCT_OFFSET (GtkCellRendererClass, editing_canceled),
124 _gtk_marshal_VOID__VOID,
128 * GtkCellRenderer::editing-started:
129 * @renderer: the object which received the signal
130 * @editable: the #GtkCellEditable
131 * @path: the path identifying the edited cell
133 * This signal gets emitted when a cell starts to be edited.
134 * The indended use of this signal is to do special setup
135 * on @editable, e.g. adding a #GtkEntryCompletion or setting
136 * up additional columns in a #GtkComboBox.
138 * Note that GTK+ doesn't guarantee that cell renderers will
139 * continue to use the same kind of widget for editing in future
140 * releases, therefore you should check the type of @editable
141 * before doing any specific setup, as in the following example:
144 * text_editing_started (GtkCellRenderer *cell,
145 * GtkCellEditable *editable,
149 * if (GTK_IS_ENTRY (editable))
151 * GtkEntry *entry = GTK_ENTRY (editable);
153 * /* ... create a GtkEntryCompletion */
155 * gtk_entry_set_completion (entry, completion);
162 cell_renderer_signals[EDITING_STARTED] =
163 g_signal_new (I_("editing-started"),
164 G_OBJECT_CLASS_TYPE (object_class),
166 G_STRUCT_OFFSET (GtkCellRendererClass, editing_started),
168 _gtk_marshal_VOID__OBJECT_STRING,
170 GTK_TYPE_CELL_EDITABLE,
173 g_object_class_install_property (object_class,
175 g_param_spec_enum ("mode",
177 P_("Editable mode of the CellRenderer"),
178 GTK_TYPE_CELL_RENDERER_MODE,
179 GTK_CELL_RENDERER_MODE_INERT,
180 GTK_PARAM_READWRITE));
182 g_object_class_install_property (object_class,
184 g_param_spec_boolean ("visible",
186 P_("Display the cell"),
188 GTK_PARAM_READWRITE));
189 g_object_class_install_property (object_class,
191 g_param_spec_boolean ("sensitive",
193 P_("Display the cell sensitive"),
195 GTK_PARAM_READWRITE));
197 g_object_class_install_property (object_class,
199 g_param_spec_float ("xalign",
205 GTK_PARAM_READWRITE));
207 g_object_class_install_property (object_class,
209 g_param_spec_float ("yalign",
215 GTK_PARAM_READWRITE));
217 g_object_class_install_property (object_class,
219 g_param_spec_uint ("xpad",
225 GTK_PARAM_READWRITE));
227 g_object_class_install_property (object_class,
229 g_param_spec_uint ("ypad",
235 GTK_PARAM_READWRITE));
237 g_object_class_install_property (object_class,
239 g_param_spec_int ("width",
241 P_("The fixed width"),
245 GTK_PARAM_READWRITE));
247 g_object_class_install_property (object_class,
249 g_param_spec_int ("height",
251 P_("The fixed height"),
255 GTK_PARAM_READWRITE));
257 g_object_class_install_property (object_class,
259 g_param_spec_boolean ("is-expander",
261 P_("Row has children"),
263 GTK_PARAM_READWRITE));
266 g_object_class_install_property (object_class,
268 g_param_spec_boolean ("is-expanded",
270 P_("Row is an expander row, and is expanded"),
272 GTK_PARAM_READWRITE));
274 g_object_class_install_property (object_class,
275 PROP_CELL_BACKGROUND,
276 g_param_spec_string ("cell-background",
277 P_("Cell background color name"),
278 P_("Cell background color as a string"),
280 GTK_PARAM_WRITABLE));
282 g_object_class_install_property (object_class,
283 PROP_CELL_BACKGROUND_GDK,
284 g_param_spec_boxed ("cell-background-gdk",
285 P_("Cell background color"),
286 P_("Cell background color as a GdkColor"),
288 GTK_PARAM_READWRITE));
291 #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, GTK_PARAM_READWRITE))
293 ADD_SET_PROP ("cell-background-set", PROP_CELL_BACKGROUND_SET,
294 P_("Cell background set"),
295 P_("Whether this tag affects the cell background color"));
297 g_type_class_add_private (object_class, sizeof (GtkCellRendererPrivate));
301 gtk_cell_renderer_get_property (GObject *object,
306 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
307 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (object);
312 g_value_set_enum (value, cell->mode);
315 g_value_set_boolean (value, cell->visible);
318 g_value_set_boolean (value, cell->sensitive);
321 g_value_set_float (value, cell->xalign);
324 g_value_set_float (value, cell->yalign);
327 g_value_set_uint (value, cell->xpad);
330 g_value_set_uint (value, cell->ypad);
333 g_value_set_int (value, cell->width);
336 g_value_set_int (value, cell->height);
338 case PROP_IS_EXPANDER:
339 g_value_set_boolean (value, cell->is_expander);
341 case PROP_IS_EXPANDED:
342 g_value_set_boolean (value, cell->is_expanded);
344 case PROP_CELL_BACKGROUND_GDK:
348 color.red = priv->cell_background.red;
349 color.green = priv->cell_background.green;
350 color.blue = priv->cell_background.blue;
352 g_value_set_boxed (value, &color);
355 case PROP_CELL_BACKGROUND_SET:
356 g_value_set_boolean (value, cell->cell_background_set);
358 case PROP_CELL_BACKGROUND:
360 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
367 gtk_cell_renderer_set_property (GObject *object,
372 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
377 cell->mode = g_value_get_enum (value);
380 cell->visible = g_value_get_boolean (value);
383 cell->sensitive = g_value_get_boolean (value);
386 cell->xalign = g_value_get_float (value);
389 cell->yalign = g_value_get_float (value);
392 cell->xpad = g_value_get_uint (value);
395 cell->ypad = g_value_get_uint (value);
398 cell->width = g_value_get_int (value);
401 cell->height = g_value_get_int (value);
403 case PROP_IS_EXPANDER:
404 cell->is_expander = g_value_get_boolean (value);
406 case PROP_IS_EXPANDED:
407 cell->is_expanded = g_value_get_boolean (value);
409 case PROP_CELL_BACKGROUND:
413 if (!g_value_get_string (value))
414 set_cell_bg_color (cell, NULL);
415 else if (gdk_color_parse (g_value_get_string (value), &color))
416 set_cell_bg_color (cell, &color);
418 g_warning ("Don't know color `%s'", g_value_get_string (value));
420 g_object_notify (object, "cell-background-gdk");
423 case PROP_CELL_BACKGROUND_GDK:
424 set_cell_bg_color (cell, g_value_get_boxed (value));
426 case PROP_CELL_BACKGROUND_SET:
427 cell->cell_background_set = g_value_get_boolean (value);
430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
436 set_cell_bg_color (GtkCellRenderer *cell,
439 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
443 if (!cell->cell_background_set)
445 cell->cell_background_set = TRUE;
446 g_object_notify (G_OBJECT (cell), "cell-background-set");
449 priv->cell_background.red = color->red;
450 priv->cell_background.green = color->green;
451 priv->cell_background.blue = color->blue;
455 if (cell->cell_background_set)
457 cell->cell_background_set = FALSE;
458 g_object_notify (G_OBJECT (cell), "cell-background-set");
464 * gtk_cell_renderer_get_size:
465 * @cell: a #GtkCellRenderer
466 * @widget: the widget the renderer is rendering to
467 * @cell_area: The area a cell will be allocated, or %NULL
468 * @x_offset: location to return x offset of cell relative to @cell_area, or %NULL
469 * @y_offset: location to return y offset of cell relative to @cell_area, or %NULL
470 * @width: location to return width needed to render a cell, or %NULL
471 * @height: location to return height needed to render a cell, or %NULL
473 * Obtains the width and height needed to render the cell. Used by view
474 * widgets to determine the appropriate size for the cell_area passed to
475 * gtk_cell_renderer_render(). If @cell_area is not %NULL, fills in the
476 * x and y offsets (if set) of the cell relative to this location.
478 * Please note that the values set in @width and @height, as well as those
479 * in @x_offset and @y_offset are inclusive of the xpad and ypad properties.
482 gtk_cell_renderer_get_size (GtkCellRenderer *cell,
484 const GdkRectangle *cell_area,
490 gint *real_width = width;
491 gint *real_height = height;
493 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
494 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size != NULL);
496 if (width && cell->width != -1)
499 *width = cell->width;
501 if (height && cell->height != -1)
504 *height = cell->height;
507 GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (cell,
509 (GdkRectangle *) cell_area,
517 * gtk_cell_renderer_render:
518 * @cell: a #GtkCellRenderer
519 * @window: a #GdkDrawable to draw to
520 * @widget: the widget owning @window
521 * @background_area: entire cell area (including tree expanders and maybe
522 * padding on the sides)
523 * @cell_area: area normally rendered by a cell renderer
524 * @expose_area: area that actually needs updating
525 * @flags: flags that affect rendering
527 * Invokes the virtual render function of the #GtkCellRenderer. The three
528 * passed-in rectangles are areas of @window. Most renderers will draw within
529 * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer
530 * should be honored with respect to @cell_area. @background_area includes the
531 * blank space around the cell, and also the area containing the tree expander;
532 * so the @background_area rectangles for all cells tile to cover the entire
533 * @window. @expose_area is a clip rectangle.
536 gtk_cell_renderer_render (GtkCellRenderer *cell,
539 const GdkRectangle *background_area,
540 const GdkRectangle *cell_area,
541 const GdkRectangle *expose_area,
542 GtkCellRendererState flags)
544 gboolean selected = FALSE;
545 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
547 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
548 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
550 selected = (flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED;
552 if (cell->cell_background_set && !selected)
554 cairo_t *cr = gdk_cairo_create (window);
556 gdk_cairo_rectangle (cr, background_area);
557 gdk_cairo_set_source_color (cr, &priv->cell_background);
563 GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
566 (GdkRectangle *) background_area,
567 (GdkRectangle *) cell_area,
568 (GdkRectangle *) expose_area,
573 * gtk_cell_renderer_activate:
574 * @cell: a #GtkCellRenderer
575 * @event: a #GdkEvent
576 * @widget: widget that received the event
577 * @path: widget-dependent string representation of the event location;
578 * e.g. for #GtkTreeView, a string representation of #GtkTreePath
579 * @background_area: background area as passed to gtk_cell_renderer_render()
580 * @cell_area: cell area as passed to gtk_cell_renderer_render()
581 * @flags: render flags
583 * Passes an activate event to the cell renderer for possible processing.
584 * Some cell renderers may use events; for example, #GtkCellRendererToggle
585 * toggles when it gets a mouse click.
587 * Return value: %TRUE if the event was consumed/handled
590 gtk_cell_renderer_activate (GtkCellRenderer *cell,
594 const GdkRectangle *background_area,
595 const GdkRectangle *cell_area,
596 GtkCellRendererState flags)
598 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
600 if (cell->mode != GTK_CELL_RENDERER_MODE_ACTIVATABLE)
603 if (GTK_CELL_RENDERER_GET_CLASS (cell)->activate == NULL)
606 return GTK_CELL_RENDERER_GET_CLASS (cell)->activate (cell,
610 (GdkRectangle *) background_area,
611 (GdkRectangle *) cell_area,
616 * gtk_cell_renderer_start_editing:
617 * @cell: a #GtkCellRenderer
618 * @event: a #GdkEvent
619 * @widget: widget that received the event
620 * @path: widget-dependent string representation of the event location;
621 * e.g. for #GtkTreeView, a string representation of #GtkTreePath
622 * @background_area: background area as passed to gtk_cell_renderer_render()
623 * @cell_area: cell area as passed to gtk_cell_renderer_render()
624 * @flags: render flags
626 * Passes an activate event to the cell renderer for possible processing.
628 * Return value: A new #GtkCellEditable, or %NULL
631 gtk_cell_renderer_start_editing (GtkCellRenderer *cell,
635 const GdkRectangle *background_area,
636 const GdkRectangle *cell_area,
637 GtkCellRendererState flags)
640 GtkCellEditable *editable;
642 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), NULL);
644 if (cell->mode != GTK_CELL_RENDERER_MODE_EDITABLE)
647 if (GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing == NULL)
650 editable = GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing (cell,
654 (GdkRectangle *) background_area,
655 (GdkRectangle *) cell_area,
659 cell_renderer_signals[EDITING_STARTED], 0,
662 cell->editing = TRUE;
668 * gtk_cell_renderer_set_fixed_size:
669 * @cell: A #GtkCellRenderer
670 * @width: the width of the cell renderer, or -1
671 * @height: the height of the cell renderer, or -1
673 * Sets the renderer size to be explicit, independent of the properties set.
676 gtk_cell_renderer_set_fixed_size (GtkCellRenderer *cell,
680 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
681 g_return_if_fail (width >= -1 && height >= -1);
683 if ((width != cell->width) || (height != cell->height))
685 g_object_freeze_notify (G_OBJECT (cell));
687 if (width != cell->width)
690 g_object_notify (G_OBJECT (cell), "width");
693 if (height != cell->height)
695 cell->height = height;
696 g_object_notify (G_OBJECT (cell), "height");
699 g_object_thaw_notify (G_OBJECT (cell));
704 * gtk_cell_renderer_get_fixed_size:
705 * @cell: A #GtkCellRenderer
706 * @width: location to fill in with the fixed width of the widget, or %NULL
707 * @height: location to fill in with the fixed height of the widget, or %NULL
709 * Fills in @width and @height with the appropriate size of @cell.
712 gtk_cell_renderer_get_fixed_size (GtkCellRenderer *cell,
716 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
719 (* width) = cell->width;
721 (* height) = cell->height;
725 * gtk_cell_renderer_editing_canceled:
726 * @cell: A #GtkCellRenderer
728 * Causes the cell renderer to emit the #GtkCellRenderer::editing-canceled
731 * This function is for use only by implementations of cell renderers that
732 * need to notify the client program that an editing process was canceled
733 * and the changes were not committed.
736 * Deprecated: 2.6: Use gtk_cell_renderer_stop_editing() instead
739 gtk_cell_renderer_editing_canceled (GtkCellRenderer *cell)
741 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
743 gtk_cell_renderer_stop_editing (cell, TRUE);
747 * gtk_cell_renderer_stop_editing:
748 * @cell: A #GtkCellRenderer
749 * @canceled: %TRUE if the editing has been canceled
751 * Informs the cell renderer that the editing is stopped.
752 * If @canceled is %TRUE, the cell renderer will emit the
753 * #GtkCellRenderer::editing-canceled signal.
755 * This function should be called by cell renderer implementations
756 * in response to the #GtkCellEditable::editing-done signal of
762 gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
765 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
769 cell->editing = FALSE;
771 g_signal_emit (cell, cell_renderer_signals[EDITING_CANCELED], 0);
775 #define __GTK_CELL_RENDERER_C__
776 #include "gtkaliasdef.c"