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.
22 #include "gtkcellrenderer.h"
24 #include "gtkmarshalers.h"
25 #include "gtktreeprivate.h"
27 static void gtk_cell_renderer_init (GtkCellRenderer *cell);
28 static void gtk_cell_renderer_class_init (GtkCellRendererClass *class);
29 static void gtk_cell_renderer_get_property (GObject *object,
33 static void gtk_cell_renderer_set_property (GObject *object,
37 static void set_cell_bg_color (GtkCellRenderer *cell,
41 #define GTK_CELL_RENDERER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_RENDERER, GtkCellRendererPrivate))
43 typedef struct _GtkCellRendererPrivate GtkCellRendererPrivate;
44 struct _GtkCellRendererPrivate
46 GdkColor cell_background;
64 PROP_CELL_BACKGROUND_GDK,
65 PROP_CELL_BACKGROUND_SET
75 static guint cell_renderer_signals[LAST_SIGNAL] = { 0 };
79 gtk_cell_renderer_get_type (void)
81 static GType cell_type = 0;
85 static const GTypeInfo cell_info =
87 sizeof (GtkCellRendererClass),
89 NULL, /* base_finalize */
90 (GClassInitFunc) gtk_cell_renderer_class_init,
91 NULL, /* class_finalize */
92 NULL, /* class_data */
93 sizeof (GtkCellRenderer),
95 (GInstanceInitFunc) gtk_cell_renderer_init,
96 NULL, /* value_table */
99 cell_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkCellRenderer",
100 &cell_info, G_TYPE_FLAG_ABSTRACT);
107 gtk_cell_renderer_init (GtkCellRenderer *cell)
109 cell->mode = GTK_CELL_RENDERER_MODE_INERT;
110 cell->visible = TRUE;
117 cell->sensitive = TRUE;
118 cell->is_expander = FALSE;
119 cell->is_expanded = FALSE;
120 cell->editing = FALSE;
124 gtk_cell_renderer_class_init (GtkCellRendererClass *class)
126 GObjectClass *object_class = G_OBJECT_CLASS (class);
128 object_class->get_property = gtk_cell_renderer_get_property;
129 object_class->set_property = gtk_cell_renderer_set_property;
131 class->render = NULL;
132 class->get_size = NULL;
135 * GtkCellRenderer::editing-canceled:
136 * @renderer: the object which received the signal
138 * This signal gets emitted when the user cancels the process of editing a
139 * cell. For example, an editable cell renderer could be written to cancel
140 * editing when the user presses Escape.
142 * See also: gtk_cell_renderer_editing_canceled()
146 cell_renderer_signals[EDITING_CANCELED] =
147 g_signal_new ("editing-canceled",
148 G_OBJECT_CLASS_TYPE (object_class),
150 G_STRUCT_OFFSET (GtkCellRendererClass, editing_canceled),
152 _gtk_marshal_VOID__VOID,
156 * GtkCellRenderer::editing-started:
157 * @renderer: the object which received the signal
158 * @editable: the #GtkCellEditable
159 * @path: the path identifying the edited cell
161 * This signal gets emitted when a cell starts to be edited.
162 * The indended use of this signal is to do special setup
163 * on @editable, e.g. adding a #GtkEntryCompletion or setting
164 * up additional columns in a #GtkComboBox.
166 * Note that GTK+ doesn't guarantee that cell renderers will
167 * continue to use the same kind of widget for editing in future
168 * releases, therefore you should check the type of @editable
169 * before doing any specific setup, as in the following example:
171 * <informalexample><programlisting>
173 * text_editing_started (GtkCellRenderer *cell,
174 * GtkCellEditable *editable,
178 * if (GTK_IS_ENTRY (editable))
180 * GtkEntry *entry = GTK_ENTRY (editable);
182 * /<!-- -->* ... create a GtkEntryCompletion *<!-- -->/
184 * gtk_entry_set_completion (entry, completion);
187 * </programlisting></informalexample>
191 cell_renderer_signals[EDITING_STARTED] =
192 g_signal_new ("editing-started",
193 G_OBJECT_CLASS_TYPE (object_class),
195 G_STRUCT_OFFSET (GtkCellRendererClass, editing_started),
197 _gtk_marshal_VOID__OBJECT_STRING,
199 GTK_TYPE_CELL_EDITABLE,
202 g_object_class_install_property (object_class,
204 g_param_spec_enum ("mode",
206 P_("Editable mode of the CellRenderer"),
207 GTK_TYPE_CELL_RENDERER_MODE,
208 GTK_CELL_RENDERER_MODE_INERT,
212 g_object_class_install_property (object_class,
214 g_param_spec_boolean ("visible",
216 P_("Display the cell"),
219 g_object_class_install_property (object_class,
221 g_param_spec_boolean ("sensitive",
223 P_("Display the cell sensitive"),
227 g_object_class_install_property (object_class,
229 g_param_spec_float ("xalign",
238 g_object_class_install_property (object_class,
240 g_param_spec_float ("yalign",
249 g_object_class_install_property (object_class,
251 g_param_spec_uint ("xpad",
260 g_object_class_install_property (object_class,
262 g_param_spec_uint ("ypad",
271 g_object_class_install_property (object_class,
273 g_param_spec_int ("width",
275 P_("The fixed width"),
282 g_object_class_install_property (object_class,
284 g_param_spec_int ("height",
286 P_("The fixed height"),
293 g_object_class_install_property (object_class,
295 g_param_spec_boolean ("is-expander",
297 P_("Row has children"),
303 g_object_class_install_property (object_class,
305 g_param_spec_boolean ("is-expanded",
307 P_("Row is an expander row, and is expanded"),
312 g_object_class_install_property (object_class,
313 PROP_CELL_BACKGROUND,
314 g_param_spec_string ("cell-background",
315 P_("Cell background color name"),
316 P_("Cell background color as a string"),
320 g_object_class_install_property (object_class,
321 PROP_CELL_BACKGROUND_GDK,
322 g_param_spec_boxed ("cell-background-gdk",
323 P_("Cell background color"),
324 P_("Cell background color as a GdkColor"),
326 G_PARAM_READABLE | G_PARAM_WRITABLE));
329 #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, G_PARAM_READABLE | G_PARAM_WRITABLE))
331 ADD_SET_PROP ("cell-background-set", PROP_CELL_BACKGROUND_SET,
332 P_("Cell background set"),
333 P_("Whether this tag affects the cell background color"));
335 g_type_class_add_private (object_class, sizeof (GtkCellRendererPrivate));
339 gtk_cell_renderer_get_property (GObject *object,
344 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
345 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (object);
350 g_value_set_enum (value, cell->mode);
353 g_value_set_boolean (value, cell->visible);
356 g_value_set_boolean (value, cell->sensitive);
359 g_value_set_float (value, cell->xalign);
362 g_value_set_float (value, cell->yalign);
365 g_value_set_uint (value, cell->xpad);
368 g_value_set_uint (value, cell->ypad);
371 g_value_set_int (value, cell->width);
374 g_value_set_int (value, cell->height);
376 case PROP_IS_EXPANDER:
377 g_value_set_boolean (value, cell->is_expander);
379 case PROP_IS_EXPANDED:
380 g_value_set_boolean (value, cell->is_expanded);
382 case PROP_CELL_BACKGROUND_GDK:
386 color.red = priv->cell_background.red;
387 color.green = priv->cell_background.green;
388 color.blue = priv->cell_background.blue;
390 g_value_set_boxed (value, &color);
393 case PROP_CELL_BACKGROUND_SET:
394 g_value_set_boolean (value, cell->cell_background_set);
396 case PROP_CELL_BACKGROUND:
398 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
405 gtk_cell_renderer_set_property (GObject *object,
410 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
415 cell->mode = g_value_get_enum (value);
418 cell->visible = g_value_get_boolean (value);
421 cell->sensitive = g_value_get_boolean (value);
424 cell->xalign = g_value_get_float (value);
427 cell->yalign = g_value_get_float (value);
430 cell->xpad = g_value_get_uint (value);
433 cell->ypad = g_value_get_uint (value);
436 cell->width = g_value_get_int (value);
439 cell->height = g_value_get_int (value);
441 case PROP_IS_EXPANDER:
442 cell->is_expander = g_value_get_boolean (value);
444 case PROP_IS_EXPANDED:
445 cell->is_expanded = g_value_get_boolean (value);
447 case PROP_CELL_BACKGROUND:
451 if (!g_value_get_string (value))
452 set_cell_bg_color (cell, NULL);
453 else if (gdk_color_parse (g_value_get_string (value), &color))
454 set_cell_bg_color (cell, &color);
456 g_warning ("Don't know color `%s'", g_value_get_string (value));
458 g_object_notify (object, "cell_background_gdk");
461 case PROP_CELL_BACKGROUND_GDK:
462 set_cell_bg_color (cell, g_value_get_boxed (value));
464 case PROP_CELL_BACKGROUND_SET:
465 cell->cell_background_set = g_value_get_boolean (value);
468 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
474 set_cell_bg_color (GtkCellRenderer *cell,
477 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
481 if (!cell->cell_background_set)
483 cell->cell_background_set = TRUE;
484 g_object_notify (G_OBJECT (cell), "cell_background_set");
487 priv->cell_background.red = color->red;
488 priv->cell_background.green = color->green;
489 priv->cell_background.blue = color->blue;
493 if (cell->cell_background_set)
495 cell->cell_background_set = FALSE;
496 g_object_notify (G_OBJECT (cell), "cell_background_set");
502 * gtk_cell_renderer_get_size:
503 * @cell: a #GtkCellRenderer
504 * @widget: the widget the renderer is rendering to
505 * @cell_area: The area a cell will be allocated, or %NULL
506 * @x_offset: location to return x offset of cell relative to @cell_area, or %NULL
507 * @y_offset: location to return y offset of cell relative to @cell_area, or %NULL
508 * @width: location to return width needed to render a cell, or %NULL
509 * @height: location to return height needed to render a cell, or %NULL
511 * Obtains the width and height needed to render the cell. Used by view widgets
512 * to determine the appropriate size for the cell_area passed to
513 * gtk_cell_renderer_render(). If @cell_area is not %NULL, fills in the x and y
514 * offsets (if set) of the cell relative to this location. Please note that the
515 * values set in @width and @height, as well as those in @x_offset and @y_offset
516 * are inclusive of the xpad and ypad properties.
519 gtk_cell_renderer_get_size (GtkCellRenderer *cell,
521 GdkRectangle *cell_area,
527 gint *real_width = width;
528 gint *real_height = height;
530 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
531 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size != NULL);
533 if (width && cell->width != -1)
536 *width = cell->width;
538 if (height && cell->height != -1)
541 *height = cell->height;
544 GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (cell,
554 * gtk_cell_renderer_render:
555 * @cell: a #GtkCellRenderer
556 * @window: a #GdkDrawable to draw to
557 * @widget: the widget owning @window
558 * @background_area: entire cell area (including tree expanders and maybe padding on the sides)
559 * @cell_area: area normally rendered by a cell renderer
560 * @expose_area: area that actually needs updating
561 * @flags: flags that affect rendering
563 * Invokes the virtual render function of the #GtkCellRenderer. The three
564 * passed-in rectangles are areas of @window. Most renderers will draw within
565 * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer
566 * should be honored with respect to @cell_area. @background_area includes the
567 * blank space around the cell, and also the area containing the tree expander;
568 * so the @background_area rectangles for all cells tile to cover the entire
569 * @window. @expose_area is a clip rectangle.
573 gtk_cell_renderer_render (GtkCellRenderer *cell,
576 GdkRectangle *background_area,
577 GdkRectangle *cell_area,
578 GdkRectangle *expose_area,
579 GtkCellRendererState flags)
581 gboolean selected = FALSE;
582 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
584 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
585 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
587 selected = (flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED;
589 if (cell->cell_background_set && !selected)
594 color.red = priv->cell_background.red;
595 color.green = priv->cell_background.green;
596 color.blue = priv->cell_background.blue;
598 gc = gdk_gc_new (window);
599 gdk_gc_set_rgb_fg_color (gc, &color);
600 gdk_draw_rectangle (window, gc, TRUE,
601 background_area->x, background_area->y,
602 background_area->width, background_area->height);
606 GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
616 * gtk_cell_renderer_activate:
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; e.g. for #GtkTreeView, a string representation of #GtkTreePath
621 * @background_area: background area as passed to @gtk_cell_renderer_render
622 * @cell_area: cell area as passed to @gtk_cell_renderer_render
623 * @flags: render flags
625 * Passes an activate event to the cell renderer for possible processing. Some
626 * cell renderers may use events; for example, #GtkCellRendererToggle toggles
627 * when it gets a mouse click.
629 * Return value: %TRUE if the event was consumed/handled
632 gtk_cell_renderer_activate (GtkCellRenderer *cell,
636 GdkRectangle *background_area,
637 GdkRectangle *cell_area,
638 GtkCellRendererState flags)
640 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
642 if (cell->mode != GTK_CELL_RENDERER_MODE_ACTIVATABLE)
645 if (GTK_CELL_RENDERER_GET_CLASS (cell)->activate == NULL)
648 return GTK_CELL_RENDERER_GET_CLASS (cell)->activate (cell,
658 * gtk_cell_renderer_start_editing:
659 * @cell: a #GtkCellRenderer
660 * @event: a #GdkEvent
661 * @widget: widget that received the event
662 * @path: widget-dependent string representation of the event location; e.g. for #GtkTreeView, a string representation of #GtkTreePath
663 * @background_area: background area as passed to @gtk_cell_renderer_render
664 * @cell_area: cell area as passed to @gtk_cell_renderer_render
665 * @flags: render flags
667 * Passes an activate event to the cell renderer for possible processing.
669 * Return value: A new #GtkCellEditable, or %NULL
672 gtk_cell_renderer_start_editing (GtkCellRenderer *cell,
676 GdkRectangle *background_area,
677 GdkRectangle *cell_area,
678 GtkCellRendererState flags)
681 GtkCellEditable *editable;
683 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), NULL);
685 if (cell->mode != GTK_CELL_RENDERER_MODE_EDITABLE)
688 if (GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing == NULL)
691 editable = GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing (cell,
700 cell_renderer_signals[EDITING_STARTED], 0,
703 cell->editing = TRUE;
709 * gtk_cell_renderer_set_fixed_size:
710 * @cell: A #GtkCellRenderer
711 * @width: the width of the cell renderer, or -1
712 * @height: the height of the cell renderer, or -1
714 * Sets the renderer size to be explicit, independent of the properties set.
717 gtk_cell_renderer_set_fixed_size (GtkCellRenderer *cell,
721 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
722 g_return_if_fail (width >= -1 && height >= -1);
724 if ((width != cell->width) || (height != cell->height))
726 g_object_freeze_notify (G_OBJECT (cell));
728 if (width != cell->width)
731 g_object_notify (G_OBJECT (cell), "width");
734 if (height != cell->height)
736 cell->height = height;
737 g_object_notify (G_OBJECT (cell), "height");
740 g_object_thaw_notify (G_OBJECT (cell));
745 * gtk_cell_renderer_get_fixed_size:
746 * @cell: A #GtkCellRenderer
747 * @width: location to fill in with the fixed width of the widget, or %NULL
748 * @height: location to fill in with the fixed height of the widget, or %NULL
750 * Fills in @width and @height with the appropriate size of @cell.
753 gtk_cell_renderer_get_fixed_size (GtkCellRenderer *cell,
757 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
760 (* width) = cell->width;
762 (* height) = cell->height;
766 * gtk_cell_renderer_editing_canceled:
767 * @cell: A #GtkCellRenderer
769 * Causes the cell renderer to emit the "editing-canceled" signal. This
770 * function is for use only by implementations of cell renderers that need to
771 * notify the client program that an editing process was canceled and the
772 * changes were not committed.
775 * Deprecated: Use gtk_cell_renderer_stop_editing() instead
778 gtk_cell_renderer_editing_canceled (GtkCellRenderer *cell)
780 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
782 gtk_cell_renderer_stop_editing (cell, TRUE);
786 * gtk_cell_renderer_stop_editing:
787 * @cell: A #GtkCellRenderer
788 * @canceled: %TRUE if the editing has been canceled
790 * Informs the cell renderer that the editing is stopped.
791 * If @canceled is %TRUE, the cell renderer will emit the "editing-canceled"
792 * signal. This function should be called by cell renderer implementations
793 * in response to the "editing-done" signal of #GtkCellEditable.
798 gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
801 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
805 cell->editing = FALSE;
807 g_signal_emit (cell, cell_renderer_signals[EDITING_CANCELED], 0);