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 "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 #define STATIC_STRINGS G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB
204 g_object_class_install_property (object_class,
206 g_param_spec_enum ("mode",
208 P_("Editable mode of the CellRenderer"),
209 GTK_TYPE_CELL_RENDERER_MODE,
210 GTK_CELL_RENDERER_MODE_INERT,
211 G_PARAM_READWRITE | STATIC_STRINGS));
213 g_object_class_install_property (object_class,
215 g_param_spec_boolean ("visible",
217 P_("Display the cell"),
219 G_PARAM_READWRITE | STATIC_STRINGS));
220 g_object_class_install_property (object_class,
222 g_param_spec_boolean ("sensitive",
224 P_("Display the cell sensitive"),
226 G_PARAM_READWRITE | STATIC_STRINGS));
228 g_object_class_install_property (object_class,
230 g_param_spec_float ("xalign",
236 G_PARAM_READWRITE | STATIC_STRINGS));
238 g_object_class_install_property (object_class,
240 g_param_spec_float ("yalign",
246 G_PARAM_READWRITE | STATIC_STRINGS));
248 g_object_class_install_property (object_class,
250 g_param_spec_uint ("xpad",
256 G_PARAM_READWRITE | STATIC_STRINGS));
258 g_object_class_install_property (object_class,
260 g_param_spec_uint ("ypad",
266 G_PARAM_READWRITE | STATIC_STRINGS));
268 g_object_class_install_property (object_class,
270 g_param_spec_int ("width",
272 P_("The fixed width"),
276 G_PARAM_READWRITE | STATIC_STRINGS));
278 g_object_class_install_property (object_class,
280 g_param_spec_int ("height",
282 P_("The fixed height"),
286 G_PARAM_READWRITE | STATIC_STRING));
288 g_object_class_install_property (object_class,
290 g_param_spec_boolean ("is-expander",
292 P_("Row has children"),
294 G_PARAM_READWRITE | STATIC_STRINGS));
297 g_object_class_install_property (object_class,
299 g_param_spec_boolean ("is-expanded",
301 P_("Row is an expander row, and is expanded"),
303 G_PARAM_READWRITE | STATIC_STRINGS));
305 g_object_class_install_property (object_class,
306 PROP_CELL_BACKGROUND,
307 g_param_spec_string ("cell-background",
308 P_("Cell background color name"),
309 P_("Cell background color as a string"),
311 G_PARAM_WRITABLE | STATIC_STRINGS));
313 g_object_class_install_property (object_class,
314 PROP_CELL_BACKGROUND_GDK,
315 g_param_spec_boxed ("cell-background-gdk",
316 P_("Cell background color"),
317 P_("Cell background color as a GdkColor"),
319 G_PARAM_READWRITE | STATIC_STRINGS));
322 #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_READWRITE | STATIC_STRINGS))
324 ADD_SET_PROP ("cell-background-set", PROP_CELL_BACKGROUND_SET,
325 P_("Cell background set"),
326 P_("Whether this tag affects the cell background color"));
328 g_type_class_add_private (object_class, sizeof (GtkCellRendererPrivate));
332 gtk_cell_renderer_get_property (GObject *object,
337 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
338 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (object);
343 g_value_set_enum (value, cell->mode);
346 g_value_set_boolean (value, cell->visible);
349 g_value_set_boolean (value, cell->sensitive);
352 g_value_set_float (value, cell->xalign);
355 g_value_set_float (value, cell->yalign);
358 g_value_set_uint (value, cell->xpad);
361 g_value_set_uint (value, cell->ypad);
364 g_value_set_int (value, cell->width);
367 g_value_set_int (value, cell->height);
369 case PROP_IS_EXPANDER:
370 g_value_set_boolean (value, cell->is_expander);
372 case PROP_IS_EXPANDED:
373 g_value_set_boolean (value, cell->is_expanded);
375 case PROP_CELL_BACKGROUND_GDK:
379 color.red = priv->cell_background.red;
380 color.green = priv->cell_background.green;
381 color.blue = priv->cell_background.blue;
383 g_value_set_boxed (value, &color);
386 case PROP_CELL_BACKGROUND_SET:
387 g_value_set_boolean (value, cell->cell_background_set);
389 case PROP_CELL_BACKGROUND:
391 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
398 gtk_cell_renderer_set_property (GObject *object,
403 GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
408 cell->mode = g_value_get_enum (value);
411 cell->visible = g_value_get_boolean (value);
414 cell->sensitive = g_value_get_boolean (value);
417 cell->xalign = g_value_get_float (value);
420 cell->yalign = g_value_get_float (value);
423 cell->xpad = g_value_get_uint (value);
426 cell->ypad = g_value_get_uint (value);
429 cell->width = g_value_get_int (value);
432 cell->height = g_value_get_int (value);
434 case PROP_IS_EXPANDER:
435 cell->is_expander = g_value_get_boolean (value);
437 case PROP_IS_EXPANDED:
438 cell->is_expanded = g_value_get_boolean (value);
440 case PROP_CELL_BACKGROUND:
444 if (!g_value_get_string (value))
445 set_cell_bg_color (cell, NULL);
446 else if (gdk_color_parse (g_value_get_string (value), &color))
447 set_cell_bg_color (cell, &color);
449 g_warning ("Don't know color `%s'", g_value_get_string (value));
451 g_object_notify (object, "cell_background_gdk");
454 case PROP_CELL_BACKGROUND_GDK:
455 set_cell_bg_color (cell, g_value_get_boxed (value));
457 case PROP_CELL_BACKGROUND_SET:
458 cell->cell_background_set = g_value_get_boolean (value);
461 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
467 set_cell_bg_color (GtkCellRenderer *cell,
470 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
474 if (!cell->cell_background_set)
476 cell->cell_background_set = TRUE;
477 g_object_notify (G_OBJECT (cell), "cell_background_set");
480 priv->cell_background.red = color->red;
481 priv->cell_background.green = color->green;
482 priv->cell_background.blue = color->blue;
486 if (cell->cell_background_set)
488 cell->cell_background_set = FALSE;
489 g_object_notify (G_OBJECT (cell), "cell_background_set");
495 * gtk_cell_renderer_get_size:
496 * @cell: a #GtkCellRenderer
497 * @widget: the widget the renderer is rendering to
498 * @cell_area: The area a cell will be allocated, or %NULL
499 * @x_offset: location to return x offset of cell relative to @cell_area, or %NULL
500 * @y_offset: location to return y offset of cell relative to @cell_area, or %NULL
501 * @width: location to return width needed to render a cell, or %NULL
502 * @height: location to return height needed to render a cell, or %NULL
504 * Obtains the width and height needed to render the cell. Used by view widgets
505 * to determine the appropriate size for the cell_area passed to
506 * gtk_cell_renderer_render(). If @cell_area is not %NULL, fills in the x and y
507 * offsets (if set) of the cell relative to this location. Please note that the
508 * values set in @width and @height, as well as those in @x_offset and @y_offset
509 * are inclusive of the xpad and ypad properties.
512 gtk_cell_renderer_get_size (GtkCellRenderer *cell,
514 GdkRectangle *cell_area,
520 gint *real_width = width;
521 gint *real_height = height;
523 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
524 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size != NULL);
526 if (width && cell->width != -1)
529 *width = cell->width;
531 if (height && cell->height != -1)
534 *height = cell->height;
537 GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (cell,
547 * gtk_cell_renderer_render:
548 * @cell: a #GtkCellRenderer
549 * @window: a #GdkDrawable to draw to
550 * @widget: the widget owning @window
551 * @background_area: entire cell area (including tree expanders and maybe padding on the sides)
552 * @cell_area: area normally rendered by a cell renderer
553 * @expose_area: area that actually needs updating
554 * @flags: flags that affect rendering
556 * Invokes the virtual render function of the #GtkCellRenderer. The three
557 * passed-in rectangles are areas of @window. Most renderers will draw within
558 * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer
559 * should be honored with respect to @cell_area. @background_area includes the
560 * blank space around the cell, and also the area containing the tree expander;
561 * so the @background_area rectangles for all cells tile to cover the entire
562 * @window. @expose_area is a clip rectangle.
566 gtk_cell_renderer_render (GtkCellRenderer *cell,
569 GdkRectangle *background_area,
570 GdkRectangle *cell_area,
571 GdkRectangle *expose_area,
572 GtkCellRendererState flags)
574 gboolean selected = FALSE;
575 GtkCellRendererPrivate *priv = GTK_CELL_RENDERER_GET_PRIVATE (cell);
577 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
578 g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
580 selected = (flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED;
582 if (cell->cell_background_set && !selected)
587 color.red = priv->cell_background.red;
588 color.green = priv->cell_background.green;
589 color.blue = priv->cell_background.blue;
591 gc = gdk_gc_new (window);
592 gdk_gc_set_rgb_fg_color (gc, &color);
593 gdk_draw_rectangle (window, gc, TRUE,
594 background_area->x, background_area->y,
595 background_area->width, background_area->height);
599 GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
609 * gtk_cell_renderer_activate:
610 * @cell: a #GtkCellRenderer
611 * @event: a #GdkEvent
612 * @widget: widget that received the event
613 * @path: widget-dependent string representation of the event location; e.g. for #GtkTreeView, a string representation of #GtkTreePath
614 * @background_area: background area as passed to @gtk_cell_renderer_render
615 * @cell_area: cell area as passed to @gtk_cell_renderer_render
616 * @flags: render flags
618 * Passes an activate event to the cell renderer for possible processing. Some
619 * cell renderers may use events; for example, #GtkCellRendererToggle toggles
620 * when it gets a mouse click.
622 * Return value: %TRUE if the event was consumed/handled
625 gtk_cell_renderer_activate (GtkCellRenderer *cell,
629 GdkRectangle *background_area,
630 GdkRectangle *cell_area,
631 GtkCellRendererState flags)
633 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
635 if (cell->mode != GTK_CELL_RENDERER_MODE_ACTIVATABLE)
638 if (GTK_CELL_RENDERER_GET_CLASS (cell)->activate == NULL)
641 return GTK_CELL_RENDERER_GET_CLASS (cell)->activate (cell,
651 * gtk_cell_renderer_start_editing:
652 * @cell: a #GtkCellRenderer
653 * @event: a #GdkEvent
654 * @widget: widget that received the event
655 * @path: widget-dependent string representation of the event location; e.g. for #GtkTreeView, a string representation of #GtkTreePath
656 * @background_area: background area as passed to @gtk_cell_renderer_render
657 * @cell_area: cell area as passed to @gtk_cell_renderer_render
658 * @flags: render flags
660 * Passes an activate event to the cell renderer for possible processing.
662 * Return value: A new #GtkCellEditable, or %NULL
665 gtk_cell_renderer_start_editing (GtkCellRenderer *cell,
669 GdkRectangle *background_area,
670 GdkRectangle *cell_area,
671 GtkCellRendererState flags)
674 GtkCellEditable *editable;
676 g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), NULL);
678 if (cell->mode != GTK_CELL_RENDERER_MODE_EDITABLE)
681 if (GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing == NULL)
684 editable = GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing (cell,
693 cell_renderer_signals[EDITING_STARTED], 0,
696 cell->editing = TRUE;
702 * gtk_cell_renderer_set_fixed_size:
703 * @cell: A #GtkCellRenderer
704 * @width: the width of the cell renderer, or -1
705 * @height: the height of the cell renderer, or -1
707 * Sets the renderer size to be explicit, independent of the properties set.
710 gtk_cell_renderer_set_fixed_size (GtkCellRenderer *cell,
714 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
715 g_return_if_fail (width >= -1 && height >= -1);
717 if ((width != cell->width) || (height != cell->height))
719 g_object_freeze_notify (G_OBJECT (cell));
721 if (width != cell->width)
724 g_object_notify (G_OBJECT (cell), "width");
727 if (height != cell->height)
729 cell->height = height;
730 g_object_notify (G_OBJECT (cell), "height");
733 g_object_thaw_notify (G_OBJECT (cell));
738 * gtk_cell_renderer_get_fixed_size:
739 * @cell: A #GtkCellRenderer
740 * @width: location to fill in with the fixed width of the widget, or %NULL
741 * @height: location to fill in with the fixed height of the widget, or %NULL
743 * Fills in @width and @height with the appropriate size of @cell.
746 gtk_cell_renderer_get_fixed_size (GtkCellRenderer *cell,
750 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
753 (* width) = cell->width;
755 (* height) = cell->height;
759 * gtk_cell_renderer_editing_canceled:
760 * @cell: A #GtkCellRenderer
762 * Causes the cell renderer to emit the "editing-canceled" signal. This
763 * function is for use only by implementations of cell renderers that need to
764 * notify the client program that an editing process was canceled and the
765 * changes were not committed.
768 * Deprecated: Use gtk_cell_renderer_stop_editing() instead
771 gtk_cell_renderer_editing_canceled (GtkCellRenderer *cell)
773 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
775 gtk_cell_renderer_stop_editing (cell, TRUE);
779 * gtk_cell_renderer_stop_editing:
780 * @cell: A #GtkCellRenderer
781 * @canceled: %TRUE if the editing has been canceled
783 * Informs the cell renderer that the editing is stopped.
784 * If @canceled is %TRUE, the cell renderer will emit the "editing-canceled"
785 * signal. This function should be called by cell renderer implementations
786 * in response to the "editing-done" signal of #GtkCellEditable.
791 gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
794 g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
798 cell->editing = FALSE;
800 g_signal_emit (cell, cell_renderer_signals[EDITING_CANCELED], 0);
804 #define __GTK_CELL_RENDERER_C__
805 #include "gtkaliasdef.c"