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,
75 static guint cell_renderer_signals[LAST_SIGNAL] = { 0 };
77 G_DEFINE_ABSTRACT_TYPE (GtkCellRenderer, gtk_cell_renderer, GTK_TYPE_OBJECT)
80 gtk_cell_renderer_init (GtkCellRenderer *cell)
82 cell->mode = GTK_CELL_RENDERER_MODE_INERT;
90 cell->sensitive = TRUE;
91 cell->is_expander = FALSE;
92 cell->is_expanded = FALSE;
93 cell->editing = FALSE;
97 gtk_cell_renderer_class_init (GtkCellRendererClass *class)
99 GObjectClass *object_class = G_OBJECT_CLASS (class);
101 object_class->get_property = gtk_cell_renderer_get_property;
102 object_class->set_property = gtk_cell_renderer_set_property;
104 class->render = NULL;
105 class->get_size = NULL;
108 * GtkCellRenderer::editing-canceled:
109 * @renderer: the object which received the signal
111 * This signal gets emitted when the user cancels the process of editing a
112 * cell. For example, an editable cell renderer could be written to cancel
113 * editing when the user presses Escape.
115 * See also: gtk_cell_renderer_stop_editing().
119 cell_renderer_signals[EDITING_CANCELED] =
120 g_signal_new (I_("editing-canceled"),
121 G_OBJECT_CLASS_TYPE (object_class),
123 G_STRUCT_OFFSET (GtkCellRendererClass, editing_canceled),
125 _gtk_marshal_VOID__VOID,
129 * GtkCellRenderer::editing-started:
130 * @renderer: the object which received the signal
131 * @editable: the #GtkCellEditable
132 * @path: the path identifying the edited cell
134 * This signal gets emitted when a cell starts to be edited.
135 * The indended use of this signal is to do special setup
136 * on @editable, e.g. adding a #GtkEntryCompletion or setting
137 * up additional columns in a #GtkComboBox.
139 * Note that GTK+ doesn't guarantee that cell renderers will
140 * continue to use the same kind of widget for editing in future
141 * releases, therefore you should check the type of @editable
142 * before doing any specific setup, as in the following example:
145 * text_editing_started (GtkCellRenderer *cell,
146 * GtkCellEditable *editable,
150 * if (GTK_IS_ENTRY (editable))
152 * GtkEntry *entry = GTK_ENTRY (editable);
154 * /* ... create a GtkEntryCompletion */
156 * gtk_entry_set_completion (entry, completion);
163 cell_renderer_signals[EDITING_STARTED] =
164 g_signal_new (I_("editing-started"),
165 G_OBJECT_CLASS_TYPE (object_class),
167 G_STRUCT_OFFSET (GtkCellRendererClass, editing_started),
169 _gtk_marshal_VOID__OBJECT_STRING,
171 GTK_TYPE_CELL_EDITABLE,
174 g_object_class_install_property (object_class,
176 g_param_spec_enum ("mode",
178 P_("Editable mode of the CellRenderer"),
179 GTK_TYPE_CELL_RENDERER_MODE,
180 GTK_CELL_RENDERER_MODE_INERT,
181 GTK_PARAM_READWRITE));
183 g_object_class_install_property (object_class,
185 g_param_spec_boolean ("visible",
187 P_("Display the cell"),
189 GTK_PARAM_READWRITE));
190 g_object_class_install_property (object_class,
192 g_param_spec_boolean ("sensitive",
194 P_("Display the cell sensitive"),
196 GTK_PARAM_READWRITE));
198 g_object_class_install_property (object_class,
200 g_param_spec_float ("xalign",
206 GTK_PARAM_READWRITE));
208 g_object_class_install_property (object_class,
210 g_param_spec_float ("yalign",
216 GTK_PARAM_READWRITE));
218 g_object_class_install_property (object_class,
220 g_param_spec_uint ("xpad",
226 GTK_PARAM_READWRITE));
228 g_object_class_install_property (object_class,
230 g_param_spec_uint ("ypad",
236 GTK_PARAM_READWRITE));
238 g_object_class_install_property (object_class,
240 g_param_spec_int ("width",
242 P_("The fixed width"),
246 GTK_PARAM_READWRITE));
248 g_object_class_install_property (object_class,
250 g_param_spec_int ("height",
252 P_("The fixed height"),
256 GTK_PARAM_READWRITE));
258 g_object_class_install_property (object_class,
260 g_param_spec_boolean ("is-expander",
262 P_("Row has children"),
264 GTK_PARAM_READWRITE));
267 g_object_class_install_property (object_class,
269 g_param_spec_boolean ("is-expanded",
271 P_("Row is an expander row, and is expanded"),
273 GTK_PARAM_READWRITE));
275 g_object_class_install_property (object_class,
276 PROP_CELL_BACKGROUND,
277 g_param_spec_string ("cell-background",
278 P_("Cell background color name"),
279 P_("Cell background color as a string"),
281 GTK_PARAM_WRITABLE));
283 g_object_class_install_property (object_class,
284 PROP_CELL_BACKGROUND_GDK,
285 g_param_spec_boxed ("cell-background-gdk",
286 P_("Cell background color"),
287 P_("Cell background color as a GdkColor"),
289 GTK_PARAM_READWRITE));
291 g_object_class_install_property (object_class,
293 g_param_spec_boolean ("editing",
295 P_("Whether the cell renderer is currently in editing mode"),
297 GTK_PARAM_READABLE));
300 #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))
302 ADD_SET_PROP ("cell-background-set", PROP_CELL_BACKGROUND_SET,
303 P_("Cell background set"),
304 P_("Whether this tag affects the cell background color"));
306 g_type_class_add_private (object_class, sizeof (GtkCellRendererPrivate));
310 gtk_cell_renderer_get_property (GObject *object,
315 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
316 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (object);
321 g_value_set_enum (value, cell->mode);
324 g_value_set_boolean (value, cell->visible);
327 g_value_set_boolean (value, cell->sensitive);
330 g_value_set_boolean (value, cell->editing);
333 g_value_set_float (value, cell->xalign);
336 g_value_set_float (value, cell->yalign);
339 g_value_set_uint (value, cell->xpad);
342 g_value_set_uint (value, cell->ypad);
345 g_value_set_int (value, cell->width);
348 g_value_set_int (value, cell->height);
350 case PROP_IS_EXPANDER:
351 g_value_set_boolean (value, cell->is_expander);
353 case PROP_IS_EXPANDED:
354 g_value_set_boolean (value, cell->is_expanded);
356 case PROP_CELL_BACKGROUND_GDK:
360 color.red = priv->cell_background.red;
361 color.green = priv->cell_background.green;
362 color.blue = priv->cell_background.blue;
364 g_value_set_boxed (value, &color);
367 case PROP_CELL_BACKGROUND_SET:
368 g_value_set_boolean (value, cell->cell_background_set);
370 case PROP_CELL_BACKGROUND:
372 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
379 gtk_cell_renderer_set_property (GObject *object,
384 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
389 cell->mode = g_value_get_enum (value);
392 cell->visible = g_value_get_boolean (value);
395 cell->sensitive = g_value_get_boolean (value);
398 cell->editing = g_value_get_boolean (value);
401 cell->xalign = g_value_get_float (value);
404 cell->yalign = g_value_get_float (value);
407 cell->xpad = g_value_get_uint (value);
410 cell->ypad = g_value_get_uint (value);
413 cell->width = g_value_get_int (value);
416 cell->height = g_value_get_int (value);
418 case PROP_IS_EXPANDER:
419 cell->is_expander = g_value_get_boolean (value);
421 case PROP_IS_EXPANDED:
422 cell->is_expanded = g_value_get_boolean (value);
424 case PROP_CELL_BACKGROUND:
428 if (!g_value_get_string (value))
429 set_cell_bg_color (cell, NULL);
430 else if (gdk_color_parse (g_value_get_string (value), &color))
431 set_cell_bg_color (cell, &color);
433 g_warning ("Don't know color `%s'", g_value_get_string (value));
435 g_object_notify (object, "cell-background-gdk");
438 case PROP_CELL_BACKGROUND_GDK:
439 set_cell_bg_color (cell, g_value_get_boxed (value));
441 case PROP_CELL_BACKGROUND_SET:
442 cell->cell_background_set = g_value_get_boolean (value);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
451 set_cell_bg_color (GtkCellRenderer *cell,
454 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
458 if (!cell->cell_background_set)
460 cell->cell_background_set = TRUE;
461 g_object_notify (G_OBJECT (cell), "cell-background-set");
464 priv->cell_background.red = color->red;
465 priv->cell_background.green = color->green;
466 priv->cell_background.blue = color->blue;
470 if (cell->cell_background_set)
472 cell->cell_background_set = FALSE;
473 g_object_notify (G_OBJECT (cell), "cell-background-set");
479 * gtk_cell_renderer_get_size:
480 * @cell: a #GtkCellRenderer
481 * @widget: the widget the renderer is rendering to
482 * @cell_area: The area a cell will be allocated, or %NULL
483 * @x_offset: location to return x offset of cell relative to @cell_area, or %NULL
484 * @y_offset: location to return y offset of cell relative to @cell_area, or %NULL
485 * @width: location to return width needed to render a cell, or %NULL
486 * @height: location to return height needed to render a cell, or %NULL
488 * Obtains the width and height needed to render the cell. Used by view
489 * widgets to determine the appropriate size for the cell_area passed to
490 * gtk_cell_renderer_render(). If @cell_area is not %NULL, fills in the
491 * x and y offsets (if set) of the cell relative to this location.
493 * Please note that the values set in @width and @height, as well as those
494 * in @x_offset and @y_offset are inclusive of the xpad and ypad properties.
497 gtk_cell_renderer_get_size (GtkCellRenderer *cell,
499 const GdkRectangle *cell_area,
505 gint *real_width = width;
506 gint *real_height = height;
508 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
509 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size != NULL);
511 if (width && cell->width != -1)
514 *width = cell->width;
516 if (height && cell->height != -1)
519 *height = cell->height;
522 GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (cell,
524 (GdkRectangle *) cell_area,
532 * gtk_cell_renderer_render:
533 * @cell: a #GtkCellRenderer
534 * @window: a #GdkDrawable to draw to
535 * @widget: the widget owning @window
536 * @background_area: entire cell area (including tree expanders and maybe
537 * padding on the sides)
538 * @cell_area: area normally rendered by a cell renderer
539 * @expose_area: area that actually needs updating
540 * @flags: flags that affect rendering
542 * Invokes the virtual render function of the #GtkCellRenderer. The three
543 * passed-in rectangles are areas of @window. Most renderers will draw within
544 * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer
545 * should be honored with respect to @cell_area. @background_area includes the
546 * blank space around the cell, and also the area containing the tree expander;
547 * so the @background_area rectangles for all cells tile to cover the entire
548 * @window. @expose_area is a clip rectangle.
551 gtk_cell_renderer_render (GtkCellRenderer *cell,
554 const GdkRectangle *background_area,
555 const GdkRectangle *cell_area,
556 const GdkRectangle *expose_area,
557 GtkCellRendererState flags)
559 gboolean selected = FALSE;
560 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
562 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
563 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
565 selected = (flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED;
567 if (cell->cell_background_set && !selected)
569 cairo_t *cr = gdk_cairo_create (window);
571 gdk_cairo_rectangle (cr, background_area);
572 gdk_cairo_set_source_color (cr, &priv->cell_background);
578 GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
581 (GdkRectangle *) background_area,
582 (GdkRectangle *) cell_area,
583 (GdkRectangle *) expose_area,
588 * gtk_cell_renderer_activate:
589 * @cell: a #GtkCellRenderer
590 * @event: a #GdkEvent
591 * @widget: widget that received the event
592 * @path: widget-dependent string representation of the event location;
593 * e.g. for #GtkTreeView, a string representation of #GtkTreePath
594 * @background_area: background area as passed to gtk_cell_renderer_render()
595 * @cell_area: cell area as passed to gtk_cell_renderer_render()
596 * @flags: render flags
598 * Passes an activate event to the cell renderer for possible processing.
599 * Some cell renderers may use events; for example, #GtkCellRendererToggle
600 * toggles when it gets a mouse click.
602 * Return value: %TRUE if the event was consumed/handled
605 gtk_cell_renderer_activate (GtkCellRenderer *cell,
609 const GdkRectangle *background_area,
610 const GdkRectangle *cell_area,
611 GtkCellRendererState flags)
613 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
615 if (cell->mode != GTK_CELL_RENDERER_MODE_ACTIVATABLE)
618 if (GTK_CELL_RENDERER_GET_CLASS (cell)->activate == NULL)
621 return GTK_CELL_RENDERER_GET_CLASS (cell)->activate (cell,
625 (GdkRectangle *) background_area,
626 (GdkRectangle *) cell_area,
631 * gtk_cell_renderer_start_editing:
632 * @cell: a #GtkCellRenderer
633 * @event: a #GdkEvent
634 * @widget: widget that received the event
635 * @path: widget-dependent string representation of the event location;
636 * e.g. for #GtkTreeView, a string representation of #GtkTreePath
637 * @background_area: background area as passed to gtk_cell_renderer_render()
638 * @cell_area: cell area as passed to gtk_cell_renderer_render()
639 * @flags: render flags
641 * Passes an activate event to the cell renderer for possible processing.
643 * Return value: A new #GtkCellEditable, or %NULL
646 gtk_cell_renderer_start_editing (GtkCellRenderer *cell,
650 const GdkRectangle *background_area,
651 const GdkRectangle *cell_area,
652 GtkCellRendererState flags)
655 GtkCellEditable *editable;
657 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), NULL);
659 if (cell->mode != GTK_CELL_RENDERER_MODE_EDITABLE)
662 if (GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing == NULL)
665 editable = GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing (cell,
669 (GdkRectangle *) background_area,
670 (GdkRectangle *) cell_area,
674 cell_renderer_signals[EDITING_STARTED], 0,
677 cell->editing = TRUE;
683 * gtk_cell_renderer_set_fixed_size:
684 * @cell: A #GtkCellRenderer
685 * @width: the width of the cell renderer, or -1
686 * @height: the height of the cell renderer, or -1
688 * Sets the renderer size to be explicit, independent of the properties set.
691 gtk_cell_renderer_set_fixed_size (GtkCellRenderer *cell,
695 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
696 g_return_if_fail (width >= -1 && height >= -1);
698 if ((width != cell->width) || (height != cell->height))
700 g_object_freeze_notify (G_OBJECT (cell));
702 if (width != cell->width)
705 g_object_notify (G_OBJECT (cell), "width");
708 if (height != cell->height)
710 cell->height = height;
711 g_object_notify (G_OBJECT (cell), "height");
714 g_object_thaw_notify (G_OBJECT (cell));
719 * gtk_cell_renderer_get_fixed_size:
720 * @cell: A #GtkCellRenderer
721 * @width: location to fill in with the fixed width of the widget, or %NULL
722 * @height: location to fill in with the fixed height of the widget, or %NULL
724 * Fills in @width and @height with the appropriate size of @cell.
727 gtk_cell_renderer_get_fixed_size (GtkCellRenderer *cell,
731 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
734 (* width) = cell->width;
736 (* height) = cell->height;
740 * gtk_cell_renderer_editing_canceled:
741 * @cell: A #GtkCellRenderer
743 * Causes the cell renderer to emit the #GtkCellRenderer::editing-canceled
746 * This function is for use only by implementations of cell renderers that
747 * need to notify the client program that an editing process was canceled
748 * and the changes were not committed.
751 * Deprecated: 2.6: Use gtk_cell_renderer_stop_editing() instead
754 gtk_cell_renderer_editing_canceled (GtkCellRenderer *cell)
756 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
758 gtk_cell_renderer_stop_editing (cell, TRUE);
762 * gtk_cell_renderer_stop_editing:
763 * @cell: A #GtkCellRenderer
764 * @canceled: %TRUE if the editing has been canceled
766 * Informs the cell renderer that the editing is stopped.
767 * If @canceled is %TRUE, the cell renderer will emit the
768 * #GtkCellRenderer::editing-canceled signal.
770 * This function should be called by cell renderer implementations
771 * in response to the #GtkCellEditable::editing-done signal of
777 gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
780 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
784 cell->editing = FALSE;
786 g_signal_emit (cell, cell_renderer_signals[EDITING_CANCELED], 0);
790 #define __GTK_CELL_RENDERER_C__
791 #include "gtkaliasdef.c"