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"
29 * SECTION:gtkcellrenderer
30 * @Short_description: An object for rendering a single cell
31 * @Title: GtkCellRenderer
32 * @See_also: #GtkCellRendererText, #GtkCellRendererPixbuf, #GtkCellRendererToggle
34 * The #GtkCellRenderer is a base class of a set of objects used for
35 * rendering a cell to a #cairo_t. These objects are used primarily by
36 * the #GtkTreeView widget, though they aren't tied to them in any
37 * specific way. It is worth noting that #GtkCellRenderer is not a
38 * #GtkWidget and cannot be treated as such.
40 * The primary use of a #GtkCellRenderer is for drawing a certain graphical
41 * elements on a #cairo_t. Typically, one cell renderer is used to
42 * draw many cells on the screen. To this extent, it isn't expected that a
43 * CellRenderer keep any permanent state around. Instead, any state is set
44 * just prior to use using #GObject<!-- -->s property system. Then, the
45 * cell is measured using gtk_cell_renderer_get_size(). Finally, the cell
46 * is rendered in the correct location using gtk_cell_renderer_render().
48 * There are a number of rules that must be followed when writing a new
49 * #GtkCellRenderer. First and formost, it's important that a certain set
50 * of properties will always yield a cell renderer of the same size,
51 * barring a #GtkStyle change. The #GtkCellRenderer also has a number of
52 * generic properties that are expected to be honored by all children.
54 * Beyond merely rendering a cell, cell renderers can optionally
55 * provide active user interface elements. A cell renderer can be
56 * <firstterm>activatable</firstterm> like #GtkCellRendererToggle,
57 * which toggles when it gets activated by a mouse click, or it can be
58 * <firstterm>editable</firstterm> like #GtkCellRendererText, which
59 * allows the user to edit the text using a #GtkEntry.
60 * To make a cell renderer activatable or editable, you have to
61 * implement the #GtkCellRendererClass.activate or
62 * #GtkCellRendererClass.start_editing virtual functions, respectively.
66 #define DEBUG_CELL_SIZE_REQUEST 0
68 static void gtk_cell_renderer_init (GtkCellRenderer *cell);
69 static void gtk_cell_renderer_class_init (GtkCellRendererClass *class);
70 static void gtk_cell_renderer_get_property (GObject *object,
74 static void gtk_cell_renderer_set_property (GObject *object,
78 static void set_cell_bg_color (GtkCellRenderer *cell,
81 /* Fallback GtkCellRenderer implementation to use remaining ->get_size() implementations */
82 static void gtk_cell_renderer_real_get_preferred_width (GtkCellRenderer *cell,
86 static void gtk_cell_renderer_real_get_preferred_height (GtkCellRenderer *cell,
90 static void gtk_cell_renderer_real_get_preferred_height_for_width(GtkCellRenderer *cell,
94 gint *natural_height);
95 static void gtk_cell_renderer_real_get_preferred_width_for_height(GtkCellRenderer *cell,
100 static void gtk_cell_renderer_real_get_aligned_area (GtkCellRenderer *cell,
102 GtkCellRendererState flags,
103 const GdkRectangle *cell_area,
104 GdkRectangle *aligned_area);
107 struct _GtkCellRendererPrivate
120 guint is_expander : 1;
121 guint is_expanded : 1;
122 guint cell_background_set : 1;
126 GdkRGBA cell_background;
143 PROP_CELL_BACKGROUND,
144 PROP_CELL_BACKGROUND_GDK,
145 PROP_CELL_BACKGROUND_RGBA,
146 PROP_CELL_BACKGROUND_SET,
157 static guint cell_renderer_signals[LAST_SIGNAL] = { 0 };
159 G_DEFINE_ABSTRACT_TYPE(GtkCellRenderer, gtk_cell_renderer, G_TYPE_INITIALLY_UNOWNED)
162 gtk_cell_renderer_init (GtkCellRenderer *cell)
164 GtkCellRendererPrivate *priv;
166 cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
167 GTK_TYPE_CELL_RENDERER,
168 GtkCellRendererPrivate);
171 priv->mode = GTK_CELL_RENDERER_MODE_INERT;
172 priv->visible = TRUE;
179 priv->sensitive = TRUE;
180 priv->is_expander = FALSE;
181 priv->is_expanded = FALSE;
182 priv->editing = FALSE;
186 gtk_cell_renderer_class_init (GtkCellRendererClass *class)
188 GObjectClass *object_class = G_OBJECT_CLASS (class);
190 object_class->get_property = gtk_cell_renderer_get_property;
191 object_class->set_property = gtk_cell_renderer_set_property;
193 class->render = NULL;
194 class->get_size = NULL;
195 class->get_preferred_width = gtk_cell_renderer_real_get_preferred_width;
196 class->get_preferred_height = gtk_cell_renderer_real_get_preferred_height;
197 class->get_preferred_width_for_height = gtk_cell_renderer_real_get_preferred_width_for_height;
198 class->get_preferred_height_for_width = gtk_cell_renderer_real_get_preferred_height_for_width;
199 class->get_aligned_area = gtk_cell_renderer_real_get_aligned_area;
202 * GtkCellRenderer::editing-canceled:
203 * @renderer: the object which received the signal
205 * This signal gets emitted when the user cancels the process of editing a
206 * cell. For example, an editable cell renderer could be written to cancel
207 * editing when the user presses Escape.
209 * See also: gtk_cell_renderer_stop_editing().
213 cell_renderer_signals[EDITING_CANCELED] =
214 g_signal_new (I_("editing-canceled"),
215 G_OBJECT_CLASS_TYPE (object_class),
217 G_STRUCT_OFFSET (GtkCellRendererClass, editing_canceled),
219 _gtk_marshal_VOID__VOID,
223 * GtkCellRenderer::editing-started:
224 * @renderer: the object which received the signal
225 * @editable: the #GtkCellEditable
226 * @path: the path identifying the edited cell
228 * This signal gets emitted when a cell starts to be edited.
229 * The intended use of this signal is to do special setup
230 * on @editable, e.g. adding a #GtkEntryCompletion or setting
231 * up additional columns in a #GtkComboBox.
233 * Note that GTK+ doesn't guarantee that cell renderers will
234 * continue to use the same kind of widget for editing in future
235 * releases, therefore you should check the type of @editable
236 * before doing any specific setup, as in the following example:
239 * text_editing_started (GtkCellRenderer *cell,
240 * GtkCellEditable *editable,
244 * if (GTK_IS_ENTRY (editable))
246 * GtkEntry *entry = GTK_ENTRY (editable);
248 * /* ... create a GtkEntryCompletion */
250 * gtk_entry_set_completion (entry, completion);
257 cell_renderer_signals[EDITING_STARTED] =
258 g_signal_new (I_("editing-started"),
259 G_OBJECT_CLASS_TYPE (object_class),
261 G_STRUCT_OFFSET (GtkCellRendererClass, editing_started),
263 _gtk_marshal_VOID__OBJECT_STRING,
265 GTK_TYPE_CELL_EDITABLE,
268 g_object_class_install_property (object_class,
270 g_param_spec_enum ("mode",
272 P_("Editable mode of the CellRenderer"),
273 GTK_TYPE_CELL_RENDERER_MODE,
274 GTK_CELL_RENDERER_MODE_INERT,
275 GTK_PARAM_READWRITE));
277 g_object_class_install_property (object_class,
279 g_param_spec_boolean ("visible",
281 P_("Display the cell"),
283 GTK_PARAM_READWRITE));
284 g_object_class_install_property (object_class,
286 g_param_spec_boolean ("sensitive",
288 P_("Display the cell sensitive"),
290 GTK_PARAM_READWRITE));
292 g_object_class_install_property (object_class,
294 g_param_spec_float ("xalign",
300 GTK_PARAM_READWRITE));
302 g_object_class_install_property (object_class,
304 g_param_spec_float ("yalign",
310 GTK_PARAM_READWRITE));
312 g_object_class_install_property (object_class,
314 g_param_spec_uint ("xpad",
320 GTK_PARAM_READWRITE));
322 g_object_class_install_property (object_class,
324 g_param_spec_uint ("ypad",
330 GTK_PARAM_READWRITE));
332 g_object_class_install_property (object_class,
334 g_param_spec_int ("width",
336 P_("The fixed width"),
340 GTK_PARAM_READWRITE));
342 g_object_class_install_property (object_class,
344 g_param_spec_int ("height",
346 P_("The fixed height"),
350 GTK_PARAM_READWRITE));
352 g_object_class_install_property (object_class,
354 g_param_spec_boolean ("is-expander",
356 P_("Row has children"),
358 GTK_PARAM_READWRITE));
361 g_object_class_install_property (object_class,
363 g_param_spec_boolean ("is-expanded",
365 P_("Row is an expander row, and is expanded"),
367 GTK_PARAM_READWRITE));
369 g_object_class_install_property (object_class,
370 PROP_CELL_BACKGROUND,
371 g_param_spec_string ("cell-background",
372 P_("Cell background color name"),
373 P_("Cell background color as a string"),
375 GTK_PARAM_WRITABLE));
377 g_object_class_install_property (object_class,
378 PROP_CELL_BACKGROUND_GDK,
379 g_param_spec_boxed ("cell-background-gdk",
380 P_("Cell background color"),
381 P_("Cell background color as a GdkColor"),
383 GTK_PARAM_READWRITE));
385 * GtkCellRenderer:cell-background-rgba:
387 * Cell background as a #GdkRGBA
391 g_object_class_install_property (object_class,
392 PROP_CELL_BACKGROUND_RGBA,
393 g_param_spec_boxed ("cell-background-rgba",
394 P_("Cell background RGBA color"),
395 P_("Cell background color as a GdkRGBA"),
397 GTK_PARAM_READWRITE));
399 g_object_class_install_property (object_class,
401 g_param_spec_boolean ("editing",
403 P_("Whether the cell renderer is currently in editing mode"),
405 GTK_PARAM_READABLE));
408 #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))
410 ADD_SET_PROP ("cell-background-set", PROP_CELL_BACKGROUND_SET,
411 P_("Cell background set"),
412 P_("Whether this tag affects the cell background color"));
414 g_type_class_add_private (class, sizeof (GtkCellRendererPrivate));
418 gtk_cell_renderer_get_property (GObject *object,
423 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
424 GtkCellRendererPrivate *priv = cell->priv;
429 g_value_set_enum (value, priv->mode);
432 g_value_set_boolean (value, priv->visible);
435 g_value_set_boolean (value, priv->sensitive);
438 g_value_set_boolean (value, priv->editing);
441 g_value_set_float (value, priv->xalign);
444 g_value_set_float (value, priv->yalign);
447 g_value_set_uint (value, priv->xpad);
450 g_value_set_uint (value, priv->ypad);
453 g_value_set_int (value, priv->width);
456 g_value_set_int (value, priv->height);
458 case PROP_IS_EXPANDER:
459 g_value_set_boolean (value, priv->is_expander);
461 case PROP_IS_EXPANDED:
462 g_value_set_boolean (value, priv->is_expanded);
464 case PROP_CELL_BACKGROUND_GDK:
468 color.red = (guint16) (priv->cell_background.red * 65535);
469 color.green = (guint16) (priv->cell_background.green * 65535);
470 color.blue = (guint16) (priv->cell_background.blue * 65535);
472 g_value_set_boxed (value, &color);
475 case PROP_CELL_BACKGROUND_RGBA:
476 g_value_set_boxed (value, &priv->cell_background);
478 case PROP_CELL_BACKGROUND_SET:
479 g_value_set_boolean (value, priv->cell_background_set);
481 case PROP_CELL_BACKGROUND:
483 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
490 gtk_cell_renderer_set_property (GObject *object,
495 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
496 GtkCellRendererPrivate *priv = cell->priv;
501 priv->mode = g_value_get_enum (value);
504 priv->visible = g_value_get_boolean (value);
507 priv->sensitive = g_value_get_boolean (value);
510 priv->editing = g_value_get_boolean (value);
513 priv->xalign = g_value_get_float (value);
516 priv->yalign = g_value_get_float (value);
519 priv->xpad = g_value_get_uint (value);
522 priv->ypad = g_value_get_uint (value);
525 priv->width = g_value_get_int (value);
528 priv->height = g_value_get_int (value);
530 case PROP_IS_EXPANDER:
531 priv->is_expander = g_value_get_boolean (value);
533 case PROP_IS_EXPANDED:
534 priv->is_expanded = g_value_get_boolean (value);
536 case PROP_CELL_BACKGROUND:
540 if (!g_value_get_string (value))
541 set_cell_bg_color (cell, NULL);
542 else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
543 set_cell_bg_color (cell, &rgba);
545 g_warning ("Don't know color `%s'", g_value_get_string (value));
547 g_object_notify (object, "cell-background-gdk");
550 case PROP_CELL_BACKGROUND_GDK:
554 color = g_value_get_boxed (value);
559 rgba.red = color->red / 65535.;
560 rgba.green = color->green / 65535.;
561 rgba.blue = color->blue / 65535.;
564 set_cell_bg_color (cell, &rgba);
568 set_cell_bg_color (cell, NULL);
572 case PROP_CELL_BACKGROUND_RGBA:
573 set_cell_bg_color (cell, g_value_get_boxed (value));
575 case PROP_CELL_BACKGROUND_SET:
576 priv->cell_background_set = g_value_get_boolean (value);
579 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
585 set_cell_bg_color (GtkCellRenderer *cell,
588 GtkCellRendererPrivate *priv = cell->priv;
592 if (!priv->cell_background_set)
594 priv->cell_background_set = TRUE;
595 g_object_notify (G_OBJECT (cell), "cell-background-set");
598 priv->cell_background = *rgba;
602 if (priv->cell_background_set)
604 priv->cell_background_set = FALSE;
605 g_object_notify (G_OBJECT (cell), "cell-background-set");
611 * gtk_cell_renderer_get_size:
612 * @cell: a #GtkCellRenderer
613 * @widget: the widget the renderer is rendering to
614 * @cell_area: (allow-none): The area a cell will be allocated, or %NULL
615 * @x_offset: (allow-none): location to return x offset of cell relative to @cell_area, or %NULL
616 * @y_offset: (allow-none): location to return y offset of cell relative to @cell_area, or %NULL
617 * @width: (allow-none): location to return width needed to render a cell, or %NULL
618 * @height: (allow-none): location to return height needed to render a cell, or %NULL
620 * Obtains the width and height needed to render the cell. Used by view
621 * widgets to determine the appropriate size for the cell_area passed to
622 * gtk_cell_renderer_render(). If @cell_area is not %NULL, fills in the
623 * x and y offsets (if set) of the cell relative to this location.
625 * Please note that the values set in @width and @height, as well as those
626 * in @x_offset and @y_offset are inclusive of the xpad and ypad properties.
629 * Deprecated: 3.0: Use gtk_cell_renderer_get_preferred_size() instead.
632 gtk_cell_renderer_get_size (GtkCellRenderer *cell,
634 const GdkRectangle *cell_area,
640 GtkRequisition request;
642 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
644 gtk_cell_renderer_get_preferred_size (cell, widget, &request, NULL);
647 *width = request.width;
650 *height = request.height;
653 _gtk_cell_renderer_calc_offset (cell, cell_area, gtk_widget_get_direction (widget),
654 request.width, request.height, x_offset, y_offset);
658 * gtk_cell_renderer_render:
659 * @cell: a #GtkCellRenderer
660 * @cr: a cairo context to draw to
661 * @widget: the widget owning @window
662 * @background_area: entire cell area (including tree expanders and maybe
663 * padding on the sides)
664 * @cell_area: area normally rendered by a cell renderer
665 * @flags: flags that affect rendering
667 * Invokes the virtual render function of the #GtkCellRenderer. The three
668 * passed-in rectangles are areas in @cr. Most renderers will draw within
669 * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer
670 * should be honored with respect to @cell_area. @background_area includes the
671 * blank space around the cell, and also the area containing the tree expander;
672 * so the @background_area rectangles for all cells tile to cover the entire
676 gtk_cell_renderer_render (GtkCellRenderer *cell,
679 const GdkRectangle *background_area,
680 const GdkRectangle *cell_area,
681 GtkCellRendererState flags)
683 gboolean selected = FALSE;
684 GtkCellRendererPrivate *priv = cell->priv;
686 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
687 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
688 g_return_if_fail (cr != NULL);
690 selected = (flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED;
694 if (priv->cell_background_set && !selected)
696 gdk_cairo_rectangle (cr, background_area);
697 gdk_cairo_set_source_rgba (cr, &priv->cell_background);
701 gdk_cairo_rectangle (cr, background_area);
704 GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
715 * gtk_cell_renderer_activate:
716 * @cell: a #GtkCellRenderer
717 * @event: a #GdkEvent
718 * @widget: widget that received the event
719 * @path: widget-dependent string representation of the event location;
720 * e.g. for #GtkTreeView, a string representation of #GtkTreePath
721 * @background_area: background area as passed to gtk_cell_renderer_render()
722 * @cell_area: cell area as passed to gtk_cell_renderer_render()
723 * @flags: render flags
725 * Passes an activate event to the cell renderer for possible processing.
726 * Some cell renderers may use events; for example, #GtkCellRendererToggle
727 * toggles when it gets a mouse click.
729 * Return value: %TRUE if the event was consumed/handled
732 gtk_cell_renderer_activate (GtkCellRenderer *cell,
736 const GdkRectangle *background_area,
737 const GdkRectangle *cell_area,
738 GtkCellRendererState flags)
740 GtkCellRendererPrivate *priv;
742 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
746 if (priv->mode != GTK_CELL_RENDERER_MODE_ACTIVATABLE)
749 if (GTK_CELL_RENDERER_GET_CLASS (cell)->activate == NULL)
752 return GTK_CELL_RENDERER_GET_CLASS (cell)->activate (cell,
756 (GdkRectangle *) background_area,
757 (GdkRectangle *) cell_area,
762 * gtk_cell_renderer_start_editing:
763 * @cell: a #GtkCellRenderer
764 * @event: a #GdkEvent
765 * @widget: widget that received the event
766 * @path: widget-dependent string representation of the event location;
767 * e.g. for #GtkTreeView, a string representation of #GtkTreePath
768 * @background_area: background area as passed to gtk_cell_renderer_render()
769 * @cell_area: cell area as passed to gtk_cell_renderer_render()
770 * @flags: render flags
772 * Passes an activate event to the cell renderer for possible processing.
774 * Return value: (transfer full): A new #GtkCellEditable, or %NULL
777 gtk_cell_renderer_start_editing (GtkCellRenderer *cell,
781 const GdkRectangle *background_area,
782 const GdkRectangle *cell_area,
783 GtkCellRendererState flags)
786 GtkCellRendererPrivate *priv;
787 GtkCellEditable *editable;
789 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), NULL);
793 if (priv->mode != GTK_CELL_RENDERER_MODE_EDITABLE)
796 if (GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing == NULL)
799 editable = GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing (cell,
803 (GdkRectangle *) background_area,
804 (GdkRectangle *) cell_area,
808 cell_renderer_signals[EDITING_STARTED], 0,
811 priv->editing = TRUE;
817 * gtk_cell_renderer_set_fixed_size:
818 * @cell: A #GtkCellRenderer
819 * @width: the width of the cell renderer, or -1
820 * @height: the height of the cell renderer, or -1
822 * Sets the renderer size to be explicit, independent of the properties set.
825 gtk_cell_renderer_set_fixed_size (GtkCellRenderer *cell,
829 GtkCellRendererPrivate *priv;
831 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
832 g_return_if_fail (width >= -1 && height >= -1);
836 if ((width != priv->width) || (height != priv->height))
838 g_object_freeze_notify (G_OBJECT (cell));
840 if (width != priv->width)
843 g_object_notify (G_OBJECT (cell), "width");
846 if (height != priv->height)
848 priv->height = height;
849 g_object_notify (G_OBJECT (cell), "height");
852 g_object_thaw_notify (G_OBJECT (cell));
857 * gtk_cell_renderer_get_fixed_size:
858 * @cell: A #GtkCellRenderer
859 * @width: (allow-none): location to fill in with the fixed width of the cell, or %NULL
860 * @height: (allow-none): location to fill in with the fixed height of the cell, or %NULL
862 * Fills in @width and @height with the appropriate size of @cell.
865 gtk_cell_renderer_get_fixed_size (GtkCellRenderer *cell,
869 GtkCellRendererPrivate *priv;
871 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
876 *width = priv->width;
878 *height = priv->height;
882 * gtk_cell_renderer_set_alignment:
883 * @cell: A #GtkCellRenderer
884 * @xalign: the x alignment of the cell renderer
885 * @yalign: the y alignment of the cell renderer
887 * Sets the renderer's alignment within its available space.
892 gtk_cell_renderer_set_alignment (GtkCellRenderer *cell,
896 GtkCellRendererPrivate *priv;
898 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
899 g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
900 g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
904 if ((xalign != priv->xalign) || (yalign != priv->yalign))
906 g_object_freeze_notify (G_OBJECT (cell));
908 if (xalign != priv->xalign)
910 priv->xalign = xalign;
911 g_object_notify (G_OBJECT (cell), "xalign");
914 if (yalign != priv->yalign)
916 priv->yalign = yalign;
917 g_object_notify (G_OBJECT (cell), "yalign");
920 g_object_thaw_notify (G_OBJECT (cell));
925 * gtk_cell_renderer_get_alignment:
926 * @cell: A #GtkCellRenderer
927 * @xalign: (allow-none): location to fill in with the x alignment of the cell, or %NULL
928 * @yalign: (allow-none): location to fill in with the y alignment of the cell, or %NULL
930 * Fills in @xalign and @yalign with the appropriate values of @cell.
935 gtk_cell_renderer_get_alignment (GtkCellRenderer *cell,
939 GtkCellRendererPrivate *priv;
941 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
946 *xalign = priv->xalign;
948 *yalign = priv->yalign;
952 * gtk_cell_renderer_set_padding:
953 * @cell: A #GtkCellRenderer
954 * @xpad: the x padding of the cell renderer
955 * @ypad: the y padding of the cell renderer
957 * Sets the renderer's padding.
962 gtk_cell_renderer_set_padding (GtkCellRenderer *cell,
966 GtkCellRendererPrivate *priv;
968 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
969 g_return_if_fail (xpad >= 0 && xpad >= 0);
973 if ((xpad != priv->xpad) || (ypad != priv->ypad))
975 g_object_freeze_notify (G_OBJECT (cell));
977 if (xpad != priv->xpad)
980 g_object_notify (G_OBJECT (cell), "xpad");
983 if (ypad != priv->ypad)
986 g_object_notify (G_OBJECT (cell), "ypad");
989 g_object_thaw_notify (G_OBJECT (cell));
994 * gtk_cell_renderer_get_padding:
995 * @cell: A #GtkCellRenderer
996 * @xpad: (allow-none): location to fill in with the x padding of the cell, or %NULL
997 * @ypad: (allow-none): location to fill in with the y padding of the cell, or %NULL
999 * Fills in @xpad and @ypad with the appropriate values of @cell.
1004 gtk_cell_renderer_get_padding (GtkCellRenderer *cell,
1008 GtkCellRendererPrivate *priv;
1010 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1021 * gtk_cell_renderer_set_visible:
1022 * @cell: A #GtkCellRenderer
1023 * @visible: the visibility of the cell
1025 * Sets the cell renderer's visibility.
1030 gtk_cell_renderer_set_visible (GtkCellRenderer *cell,
1033 GtkCellRendererPrivate *priv;
1035 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1039 if (priv->visible != visible)
1041 priv->visible = visible ? TRUE : FALSE;
1042 g_object_notify (G_OBJECT (cell), "visible");
1047 * gtk_cell_renderer_get_visible:
1048 * @cell: A #GtkCellRenderer
1050 * Returns the cell renderer's visibility.
1052 * Returns: %TRUE if the cell renderer is visible
1057 gtk_cell_renderer_get_visible (GtkCellRenderer *cell)
1059 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1061 return cell->priv->visible;
1065 * gtk_cell_renderer_set_sensitive:
1066 * @cell: A #GtkCellRenderer
1067 * @sensitive: the sensitivity of the cell
1069 * Sets the cell renderer's sensitivity.
1074 gtk_cell_renderer_set_sensitive (GtkCellRenderer *cell,
1077 GtkCellRendererPrivate *priv;
1079 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1083 if (priv->sensitive != sensitive)
1085 priv->sensitive = sensitive ? TRUE : FALSE;
1086 g_object_notify (G_OBJECT (cell), "sensitive");
1091 * gtk_cell_renderer_get_sensitive:
1092 * @cell: A #GtkCellRenderer
1094 * Returns the cell renderer's sensitivity.
1096 * Returns: %TRUE if the cell renderer is sensitive
1101 gtk_cell_renderer_get_sensitive (GtkCellRenderer *cell)
1103 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1105 return cell->priv->sensitive;
1110 * gtk_cell_renderer_is_activatable:
1111 * @cell: A #GtkCellRenderer
1113 * Checks whether the cell renderer can do something when activated.
1115 * Returns: %TRUE if the cell renderer can do anything when activated.
1120 gtk_cell_renderer_is_activatable (GtkCellRenderer *cell)
1122 GtkCellRendererPrivate *priv;
1124 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1128 return (cell->priv->visible &&
1129 (cell->priv->mode == GTK_CELL_RENDERER_MODE_EDITABLE ||
1130 cell->priv->mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE));
1135 * gtk_cell_renderer_stop_editing:
1136 * @cell: A #GtkCellRenderer
1137 * @canceled: %TRUE if the editing has been canceled
1139 * Informs the cell renderer that the editing is stopped.
1140 * If @canceled is %TRUE, the cell renderer will emit the
1141 * #GtkCellRenderer::editing-canceled signal.
1143 * This function should be called by cell renderer implementations
1144 * in response to the #GtkCellEditable::editing-done signal of
1150 gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
1153 GtkCellRendererPrivate *priv;
1155 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1161 priv->editing = FALSE;
1163 g_signal_emit (cell, cell_renderer_signals[EDITING_CANCELED], 0);
1168 gtk_cell_renderer_real_get_preferred_size (GtkCellRenderer *cell,
1170 GtkOrientation orientation,
1174 GtkRequisition min_req;
1176 /* Fallback on the old API to get the size. */
1177 if (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size)
1178 GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (GTK_CELL_RENDERER (cell), widget, NULL, NULL, NULL,
1179 &min_req.width, &min_req.height);
1186 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1189 *minimum_size = min_req.width;
1192 *natural_size = min_req.width;
1197 *minimum_size = min_req.height;
1200 *natural_size = min_req.height;
1205 gtk_cell_renderer_real_get_preferred_width (GtkCellRenderer *cell,
1210 gtk_cell_renderer_real_get_preferred_size (cell, widget, GTK_ORIENTATION_HORIZONTAL,
1211 minimum_size, natural_size);
1215 gtk_cell_renderer_real_get_preferred_height (GtkCellRenderer *cell,
1220 gtk_cell_renderer_real_get_preferred_size (cell, widget, GTK_ORIENTATION_VERTICAL,
1221 minimum_size, natural_size);
1226 gtk_cell_renderer_real_get_preferred_height_for_width (GtkCellRenderer *cell,
1229 gint *minimum_height,
1230 gint *natural_height)
1232 /* Fall back on the height reported from ->get_size() */
1233 gtk_cell_renderer_get_preferred_height (cell, widget, minimum_height, natural_height);
1237 gtk_cell_renderer_real_get_preferred_width_for_height (GtkCellRenderer *cell,
1240 gint *minimum_width,
1241 gint *natural_width)
1243 /* Fall back on the width reported from ->get_size() */
1244 gtk_cell_renderer_get_preferred_width (cell, widget, minimum_width, natural_width);
1248 /* Default implementation assumes that a cell renderer will never use more
1249 * space than it's natural size (this is fine for toggles and pixbufs etc
1250 * but needs to be overridden from wrapping/ellipsizing text renderers) */
1252 gtk_cell_renderer_real_get_aligned_area (GtkCellRenderer *cell,
1254 GtkCellRendererState flags,
1255 const GdkRectangle *cell_area,
1256 GdkRectangle *aligned_area)
1258 gint opposite_size, x_offset, y_offset;
1261 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1262 g_return_if_fail (GTK_IS_WIDGET (widget));
1263 g_return_if_fail (cell_area != NULL);
1264 g_return_if_fail (aligned_area != NULL);
1266 *aligned_area = *cell_area;
1268 /* Trim up the aligned size */
1269 if (gtk_cell_renderer_get_request_mode (cell) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1271 gtk_cell_renderer_get_preferred_width (cell, widget,
1272 NULL, &natural_size);
1274 aligned_area->width = MIN (aligned_area->width, natural_size);
1276 gtk_cell_renderer_get_preferred_height_for_width (cell, widget,
1277 aligned_area->width,
1278 NULL, &opposite_size);
1280 aligned_area->height = MIN (opposite_size, aligned_area->height);
1284 gtk_cell_renderer_get_preferred_height (cell, widget,
1285 NULL, &natural_size);
1287 aligned_area->height = MIN (aligned_area->width, natural_size);
1289 gtk_cell_renderer_get_preferred_width_for_height (cell, widget,
1290 aligned_area->height,
1291 NULL, &opposite_size);
1293 aligned_area->width = MIN (opposite_size, aligned_area->width);
1296 /* offset the cell position */
1297 _gtk_cell_renderer_calc_offset (cell, cell_area,
1298 gtk_widget_get_direction (widget),
1299 aligned_area->width,
1300 aligned_area->height,
1301 &x_offset, &y_offset);
1303 aligned_area->x += x_offset;
1304 aligned_area->y += y_offset;
1308 /* An internal convenience function for some containers to peek at the
1309 * cell alignment in a target allocation (used to draw focus and align
1310 * cells in the icon view).
1312 * Note this is only a trivial 'align * (allocation - request)' operation.
1315 _gtk_cell_renderer_calc_offset (GtkCellRenderer *cell,
1316 const GdkRectangle *cell_area,
1317 GtkTextDirection direction,
1323 GtkCellRendererPrivate *priv;
1325 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1326 g_return_if_fail (cell_area != NULL);
1327 g_return_if_fail (x_offset || y_offset);
1333 *x_offset = (((direction == GTK_TEXT_DIR_RTL) ?
1334 (1.0 - priv->xalign) : priv->xalign) *
1335 (cell_area->width - width));
1336 *x_offset = MAX (*x_offset, 0);
1340 *y_offset = (priv->yalign *
1341 (cell_area->height - height));
1342 *y_offset = MAX (*y_offset, 0);
1347 * gtk_cell_renderer_get_request_mode:
1348 * @cell: a #GtkCellRenderer instance
1350 * Gets whether the cell renderer prefers a height-for-width layout
1351 * or a width-for-height layout.
1353 * Returns: The #GtkSizeRequestMode preferred by this renderer.
1358 gtk_cell_renderer_get_request_mode (GtkCellRenderer *cell)
1360 GtkCellRendererClass *klass;
1362 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1364 klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1365 if (klass->get_request_mode)
1366 return klass->get_request_mode (cell);
1368 /* By default cell renderers are height-for-width. */
1369 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1373 * gtk_cell_renderer_get_preferred_width:
1374 * @cell: a #GtkCellRenderer instance
1375 * @widget: the #GtkWidget this cell will be rendering to
1376 * @minimum_size: location to store the minimum size, or %NULL
1377 * @natural_size: location to store the natural size, or %NULL
1379 * Retreives a renderer's natural size when rendered to @widget.
1384 gtk_cell_renderer_get_preferred_width (GtkCellRenderer *cell,
1389 GtkCellRendererClass *klass;
1392 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1393 g_return_if_fail (GTK_IS_WIDGET (widget));
1394 g_return_if_fail (NULL != minimum_size || NULL != natural_size);
1396 gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), &width, NULL);
1400 klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1401 klass->get_preferred_width (cell, widget, minimum_size, natural_size);
1406 *minimum_size = width;
1408 *natural_size = width;
1411 #if DEBUG_CELL_SIZE_REQUEST
1412 g_message ("%s returning minimum width: %d and natural width: %d",
1413 G_OBJECT_TYPE_NAME (cell),
1414 minimum_size ? *minimum_size : 20000,
1415 natural_size ? *natural_size : 20000);
1421 * gtk_cell_renderer_get_preferred_height:
1422 * @cell: a #GtkCellRenderer instance
1423 * @widget: the #GtkWidget this cell will be rendering to
1424 * @minimum_size: location to store the minimum size, or %NULL
1425 * @natural_size: location to store the natural size, or %NULL
1427 * Retreives a renderer's natural size when rendered to @widget.
1432 gtk_cell_renderer_get_preferred_height (GtkCellRenderer *cell,
1437 GtkCellRendererClass *klass;
1440 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1441 g_return_if_fail (GTK_IS_WIDGET (widget));
1442 g_return_if_fail (NULL != minimum_size || NULL != natural_size);
1444 gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), NULL, &height);
1448 klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1449 klass->get_preferred_height (cell, widget, minimum_size, natural_size);
1454 *minimum_size = height;
1456 *natural_size = height;
1459 #if DEBUG_CELL_SIZE_REQUEST
1460 g_message ("%s returning minimum height: %d and natural height: %d",
1461 G_OBJECT_TYPE_NAME (cell),
1462 minimum_size ? *minimum_size : 20000,
1463 natural_size ? *natural_size : 20000);
1469 * gtk_cell_renderer_get_preferred_width_for_height:
1470 * @cell: a #GtkCellRenderer instance
1471 * @widget: the #GtkWidget this cell will be rendering to
1472 * @height: the size which is available for allocation
1473 * @minimum_width: location for storing the minimum size, or %NULL
1474 * @natural_width: location for storing the preferred size, or %NULL
1476 * Retreives a cell renderers's minimum and natural width if it were rendered to
1477 * @widget with the specified @height.
1482 gtk_cell_renderer_get_preferred_width_for_height (GtkCellRenderer *cell,
1485 gint *minimum_width,
1486 gint *natural_width)
1488 GtkCellRendererClass *klass;
1491 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1492 g_return_if_fail (GTK_IS_WIDGET (widget));
1493 g_return_if_fail (NULL != minimum_width || NULL != natural_width);
1495 gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), &width, NULL);
1499 klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1500 klass->get_preferred_width_for_height (cell, widget, height, minimum_width, natural_width);
1505 *minimum_width = width;
1507 *natural_width = width;
1510 #if DEBUG_CELL_SIZE_REQUEST
1511 g_message ("%s width for height: %d is minimum %d and natural: %d",
1512 G_OBJECT_TYPE_NAME (cell), height,
1513 minimum_width ? *minimum_width : 20000,
1514 natural_width ? *natural_width : 20000);
1519 * gtk_cell_renderer_get_preferred_height_for_width:
1520 * @cell: a #GtkCellRenderer instance
1521 * @widget: the #GtkWidget this cell will be rendering to
1522 * @width: the size which is available for allocation
1523 * @minimum_height: location for storing the minimum size, or %NULL
1524 * @natural_height: location for storing the preferred size, or %NULL
1526 * Retreives a cell renderers's minimum and natural height if it were rendered to
1527 * @widget with the specified @width.
1532 gtk_cell_renderer_get_preferred_height_for_width (GtkCellRenderer *cell,
1535 gint *minimum_height,
1536 gint *natural_height)
1538 GtkCellRendererClass *klass;
1541 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1542 g_return_if_fail (GTK_IS_WIDGET (widget));
1543 g_return_if_fail (NULL != minimum_height || NULL != natural_height);
1545 gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), NULL, &height);
1549 klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1550 klass->get_preferred_height_for_width (cell, widget, width, minimum_height, natural_height);
1555 *minimum_height = height;
1557 *natural_height = height;
1560 #if DEBUG_CELL_SIZE_REQUEST
1561 g_message ("%s height for width: %d is minimum %d and natural: %d",
1562 G_OBJECT_TYPE_NAME (cell), width,
1563 minimum_height ? *minimum_height : 20000,
1564 natural_height ? *natural_height : 20000);
1569 * gtk_cell_renderer_get_preferred_size:
1570 * @cell: a #GtkCellRenderer instance
1571 * @widget: the #GtkWidget this cell will be rendering to
1572 * @request_natural: Whether to base the contextual request off of the
1573 * base natural or the base minimum
1574 * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
1575 * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
1577 * Retrieves the minimum and natural size of a cell taking
1578 * into account the widget's preference for height-for-width management.
1580 * If request_natural is specified, the non-contextual natural value will
1581 * be used to make the contextual request; otherwise the minimum will be used.
1586 gtk_cell_renderer_get_preferred_size (GtkCellRenderer *cell,
1588 GtkRequisition *minimum_size,
1589 GtkRequisition *natural_size)
1591 gint min_width, nat_width;
1592 gint min_height, nat_height;
1594 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1596 if (gtk_cell_renderer_get_request_mode (cell) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1598 gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, &nat_width);
1602 minimum_size->width = min_width;
1603 gtk_cell_renderer_get_preferred_height_for_width (cell, widget, min_width,
1604 &minimum_size->height, NULL);
1609 natural_size->width = nat_width;
1610 gtk_cell_renderer_get_preferred_height_for_width (cell, widget, nat_width,
1611 NULL, &natural_size->height);
1614 else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1616 gtk_cell_renderer_get_preferred_height (cell, widget, &min_height, &nat_height);
1620 minimum_size->height = min_height;
1621 gtk_cell_renderer_get_preferred_width_for_height (cell, widget, min_height,
1622 &minimum_size->width, NULL);
1627 natural_size->height = nat_height;
1628 gtk_cell_renderer_get_preferred_width_for_height (cell, widget, nat_height,
1629 NULL, &natural_size->width);
1635 * gtk_cell_renderer_get_aligned_area:
1636 * @cell: a #GtkCellRenderer instance
1637 * @widget: the #GtkWidget this cell will be rendering to
1638 * @flags: render flags
1639 * @cell_area: cell area which would be passed to gtk_cell_renderer_render()
1640 * @aligned_area: the return location for the space inside @cell_area that
1641 * would acually be used to render.
1643 * Gets the aligned area used by @cell inside @cell_area. Used for finding
1644 * the appropriate edit and focus rectangle.
1649 gtk_cell_renderer_get_aligned_area (GtkCellRenderer *cell,
1651 GtkCellRendererState flags,
1652 const GdkRectangle *cell_area,
1653 GdkRectangle *aligned_area)
1655 GtkCellRendererClass *klass;
1657 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1658 g_return_if_fail (GTK_IS_WIDGET (widget));
1659 g_return_if_fail (cell_area != NULL);
1660 g_return_if_fail (aligned_area != NULL);
1662 klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1663 klass->get_aligned_area (cell, widget, flags, cell_area, aligned_area);