]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderer.c
865dd96274bf2720e392b9f476dd3985c2ec6340
[~andy/gtk] / gtk / gtkcellrenderer.c
1 /* gtkcellrenderer.c
2  * Copyright (C) 2000  Red Hat, Inc. Jonathan Blandford
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include "gtkcellrenderer.h"
22 #include "gtkcellsizerequest.h"
23 #include "gtkintl.h"
24 #include "gtkmarshalers.h"
25 #include "gtkprivate.h"
26 #include "gtktreeprivate.h"
27
28
29 static void gtk_cell_renderer_init          (GtkCellRenderer      *cell);
30 static void gtk_cell_renderer_class_init    (GtkCellRendererClass *class);
31 static void gtk_cell_renderer_get_property  (GObject              *object,
32                                              guint                 param_id,
33                                              GValue               *value,
34                                              GParamSpec           *pspec);
35 static void gtk_cell_renderer_set_property  (GObject              *object,
36                                              guint                 param_id,
37                                              const GValue         *value,
38                                              GParamSpec           *pspec);
39 static void set_cell_bg_color               (GtkCellRenderer      *cell,
40                                              GdkColor             *color);
41
42 /* Fallback GtkCellSizeRequest implementation to use remaining ->get_size() implementations */
43 static void gtk_cell_renderer_cell_size_request_init   (GtkCellSizeRequestIface *iface);
44 static void gtk_cell_renderer_get_width                (GtkCellSizeRequest      *cell,
45                                                         GtkWidget               *widget,
46                                                         gint                    *minimum_size,
47                                                         gint                    *natural_size);
48 static void gtk_cell_renderer_get_height               (GtkCellSizeRequest      *cell,
49                                                         GtkWidget               *widget,
50                                                         gint                    *minimum_size,
51                                                         gint                    *natural_size);
52 static void gtk_cell_renderer_get_height_for_width     (GtkCellSizeRequest      *cell,
53                                                         GtkWidget               *widget,
54                                                         gint                     width,
55                                                         gint                    *minimum_height,
56                                                         gint                    *natural_height);
57 static void gtk_cell_renderer_get_width_for_height     (GtkCellSizeRequest      *cell,
58                                                         GtkWidget               *widget,
59                                                         gint                     height,
60                                                         gint                    *minimum_width,
61                                                         gint                    *natural_width);
62
63
64
65 struct _GtkCellRendererPrivate
66 {
67   gfloat xalign;
68   gfloat yalign;
69
70   gint width;
71   gint height;
72
73   guint16 xpad;
74   guint16 ypad;
75
76   guint mode                : 2;
77   guint visible             : 1;
78   guint is_expander         : 1;
79   guint is_expanded         : 1;
80   guint cell_background_set : 1;
81   guint sensitive           : 1;
82   guint editing             : 1;
83
84   GdkColor cell_background;
85 };
86
87
88 enum {
89   PROP_0,
90   PROP_MODE,
91   PROP_VISIBLE,
92   PROP_SENSITIVE,
93   PROP_XALIGN,
94   PROP_YALIGN,
95   PROP_XPAD,
96   PROP_YPAD,
97   PROP_WIDTH,
98   PROP_HEIGHT,
99   PROP_IS_EXPANDER,
100   PROP_IS_EXPANDED,
101   PROP_CELL_BACKGROUND,
102   PROP_CELL_BACKGROUND_GDK,
103   PROP_CELL_BACKGROUND_SET,
104   PROP_EDITING
105 };
106
107 /* Signal IDs */
108 enum {
109   EDITING_CANCELED,
110   EDITING_STARTED,
111   LAST_SIGNAL
112 };
113
114 static guint  cell_renderer_signals[LAST_SIGNAL] = { 0 };
115
116
117 /* Do a manual _get_type() here to avoid a deadlock implementing
118  * the interface which we are a prerequisite of.
119  */
120 GType
121 gtk_cell_renderer_get_type (void)
122 {
123   static GType cell_renderer_type = 0;
124
125   if (G_UNLIKELY (cell_renderer_type == 0))
126     {
127       const GTypeInfo cell_renderer_info =
128       {
129         sizeof (GtkCellRendererClass),
130         NULL,           /* base_init */
131         NULL,           /* base_finalize */
132         (GClassInitFunc) gtk_cell_renderer_class_init,
133         NULL,           /* class_finalize */
134         NULL,           /* class_init */
135         sizeof (GtkCellRenderer),
136         0,              /* n_preallocs */
137         (GInstanceInitFunc) gtk_cell_renderer_init,
138         NULL,           /* value_table */
139       };
140
141       const GInterfaceInfo cell_size_request_info =
142       {
143         (GInterfaceInitFunc) gtk_cell_renderer_cell_size_request_init,
144         (GInterfaceFinalizeFunc) NULL,
145         NULL /* interface data */
146       };
147
148       cell_renderer_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkCellRenderer",
149                                                    &cell_renderer_info, G_TYPE_FLAG_ABSTRACT);
150
151       g_type_add_interface_static (cell_renderer_type, GTK_TYPE_CELL_SIZE_REQUEST,
152                                    &cell_size_request_info) ;
153     }
154
155   return cell_renderer_type;
156 }
157
158
159 static void
160 gtk_cell_renderer_init (GtkCellRenderer *cell)
161 {
162   GtkCellRendererPrivate *priv;
163
164   cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
165                                             GTK_TYPE_CELL_RENDERER,
166                                             GtkCellRendererPrivate);
167   priv = cell->priv;
168
169   priv->mode = GTK_CELL_RENDERER_MODE_INERT;
170   priv->visible = TRUE;
171   priv->width = -1;
172   priv->height = -1;
173   priv->xalign = 0.5;
174   priv->yalign = 0.5;
175   priv->xpad = 0;
176   priv->ypad = 0;
177   priv->sensitive = TRUE;
178   priv->is_expander = FALSE;
179   priv->is_expanded = FALSE;
180   priv->editing = FALSE;
181 }
182
183 static void
184 gtk_cell_renderer_class_init (GtkCellRendererClass *class)
185 {
186   GObjectClass *object_class = G_OBJECT_CLASS (class);
187
188   object_class->get_property = gtk_cell_renderer_get_property;
189   object_class->set_property = gtk_cell_renderer_set_property;
190
191   class->render = NULL;
192   class->get_size = NULL;
193
194   /**
195    * GtkCellRenderer::editing-canceled:
196    * @renderer: the object which received the signal
197    *
198    * This signal gets emitted when the user cancels the process of editing a
199    * cell.  For example, an editable cell renderer could be written to cancel
200    * editing when the user presses Escape. 
201    *
202    * See also: gtk_cell_renderer_stop_editing().
203    *
204    * Since: 2.4
205    */
206   cell_renderer_signals[EDITING_CANCELED] =
207     g_signal_new (I_("editing-canceled"),
208                   G_OBJECT_CLASS_TYPE (object_class),
209                   G_SIGNAL_RUN_FIRST,
210                   G_STRUCT_OFFSET (GtkCellRendererClass, editing_canceled),
211                   NULL, NULL,
212                   _gtk_marshal_VOID__VOID,
213                   G_TYPE_NONE, 0);
214
215   /**
216    * GtkCellRenderer::editing-started:
217    * @renderer: the object which received the signal
218    * @editable: the #GtkCellEditable
219    * @path: the path identifying the edited cell
220    *
221    * This signal gets emitted when a cell starts to be edited.
222    * The intended use of this signal is to do special setup
223    * on @editable, e.g. adding a #GtkEntryCompletion or setting
224    * up additional columns in a #GtkComboBox.
225    *
226    * Note that GTK+ doesn't guarantee that cell renderers will
227    * continue to use the same kind of widget for editing in future
228    * releases, therefore you should check the type of @editable
229    * before doing any specific setup, as in the following example:
230    * |[
231    * static void
232    * text_editing_started (GtkCellRenderer *cell,
233    *                       GtkCellEditable *editable,
234    *                       const gchar     *path,
235    *                       gpointer         data)
236    * {
237    *   if (GTK_IS_ENTRY (editable)) 
238    *     {
239    *       GtkEntry *entry = GTK_ENTRY (editable);
240    *       
241    *       /* ... create a GtkEntryCompletion */
242    *       
243    *       gtk_entry_set_completion (entry, completion);
244    *     }
245    * }
246    * ]|
247    *
248    * Since: 2.6
249    */
250   cell_renderer_signals[EDITING_STARTED] =
251     g_signal_new (I_("editing-started"),
252                   G_OBJECT_CLASS_TYPE (object_class),
253                   G_SIGNAL_RUN_FIRST,
254                   G_STRUCT_OFFSET (GtkCellRendererClass, editing_started),
255                   NULL, NULL,
256                   _gtk_marshal_VOID__OBJECT_STRING,
257                   G_TYPE_NONE, 2,
258                   GTK_TYPE_CELL_EDITABLE,
259                   G_TYPE_STRING);
260
261   g_object_class_install_property (object_class,
262                                    PROP_MODE,
263                                    g_param_spec_enum ("mode",
264                                                       P_("mode"),
265                                                       P_("Editable mode of the CellRenderer"),
266                                                       GTK_TYPE_CELL_RENDERER_MODE,
267                                                       GTK_CELL_RENDERER_MODE_INERT,
268                                                       GTK_PARAM_READWRITE));
269
270   g_object_class_install_property (object_class,
271                                    PROP_VISIBLE,
272                                    g_param_spec_boolean ("visible",
273                                                          P_("visible"),
274                                                          P_("Display the cell"),
275                                                          TRUE,
276                                                          GTK_PARAM_READWRITE));
277   g_object_class_install_property (object_class,
278                                    PROP_SENSITIVE,
279                                    g_param_spec_boolean ("sensitive",
280                                                          P_("Sensitive"),
281                                                          P_("Display the cell sensitive"),
282                                                          TRUE,
283                                                          GTK_PARAM_READWRITE));
284
285   g_object_class_install_property (object_class,
286                                    PROP_XALIGN,
287                                    g_param_spec_float ("xalign",
288                                                        P_("xalign"),
289                                                        P_("The x-align"),
290                                                        0.0,
291                                                        1.0,
292                                                        0.5,
293                                                        GTK_PARAM_READWRITE));
294
295   g_object_class_install_property (object_class,
296                                    PROP_YALIGN,
297                                    g_param_spec_float ("yalign",
298                                                        P_("yalign"),
299                                                        P_("The y-align"),
300                                                        0.0,
301                                                        1.0,
302                                                        0.5,
303                                                        GTK_PARAM_READWRITE));
304
305   g_object_class_install_property (object_class,
306                                    PROP_XPAD,
307                                    g_param_spec_uint ("xpad",
308                                                       P_("xpad"),
309                                                       P_("The xpad"),
310                                                       0,
311                                                       G_MAXUINT,
312                                                       0,
313                                                       GTK_PARAM_READWRITE));
314
315   g_object_class_install_property (object_class,
316                                    PROP_YPAD,
317                                    g_param_spec_uint ("ypad",
318                                                       P_("ypad"),
319                                                       P_("The ypad"),
320                                                       0,
321                                                       G_MAXUINT,
322                                                       0,
323                                                       GTK_PARAM_READWRITE));
324
325   g_object_class_install_property (object_class,
326                                    PROP_WIDTH,
327                                    g_param_spec_int ("width",
328                                                      P_("width"),
329                                                      P_("The fixed width"),
330                                                      -1,
331                                                      G_MAXINT,
332                                                      -1,
333                                                      GTK_PARAM_READWRITE));
334
335   g_object_class_install_property (object_class,
336                                    PROP_HEIGHT,
337                                    g_param_spec_int ("height",
338                                                      P_("height"),
339                                                      P_("The fixed height"),
340                                                      -1,
341                                                      G_MAXINT,
342                                                      -1,
343                                                      GTK_PARAM_READWRITE));
344
345   g_object_class_install_property (object_class,
346                                    PROP_IS_EXPANDER,
347                                    g_param_spec_boolean ("is-expander",
348                                                          P_("Is Expander"),
349                                                          P_("Row has children"),
350                                                          FALSE,
351                                                          GTK_PARAM_READWRITE));
352
353
354   g_object_class_install_property (object_class,
355                                    PROP_IS_EXPANDED,
356                                    g_param_spec_boolean ("is-expanded",
357                                                          P_("Is Expanded"),
358                                                          P_("Row is an expander row, and is expanded"),
359                                                          FALSE,
360                                                          GTK_PARAM_READWRITE));
361
362   g_object_class_install_property (object_class,
363                                    PROP_CELL_BACKGROUND,
364                                    g_param_spec_string ("cell-background",
365                                                         P_("Cell background color name"),
366                                                         P_("Cell background color as a string"),
367                                                         NULL,
368                                                         GTK_PARAM_WRITABLE));
369
370   g_object_class_install_property (object_class,
371                                    PROP_CELL_BACKGROUND_GDK,
372                                    g_param_spec_boxed ("cell-background-gdk",
373                                                        P_("Cell background color"),
374                                                        P_("Cell background color as a GdkColor"),
375                                                        GDK_TYPE_COLOR,
376                                                        GTK_PARAM_READWRITE));
377
378   g_object_class_install_property (object_class,
379                                    PROP_EDITING,
380                                    g_param_spec_boolean ("editing",
381                                                          P_("Editing"),
382                                                          P_("Whether the cell renderer is currently in editing mode"),
383                                                          FALSE,
384                                                          GTK_PARAM_READABLE));
385
386
387 #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))
388
389   ADD_SET_PROP ("cell-background-set", PROP_CELL_BACKGROUND_SET,
390                 P_("Cell background set"),
391                 P_("Whether this tag affects the cell background color"));
392
393   g_type_class_add_private (class, sizeof (GtkCellRendererPrivate));
394 }
395
396 static void
397 gtk_cell_renderer_get_property (GObject     *object,
398                                 guint        param_id,
399                                 GValue      *value,
400                                 GParamSpec  *pspec)
401 {
402   GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
403   GtkCellRendererPrivate *priv = cell->priv;
404
405   switch (param_id)
406     {
407     case PROP_MODE:
408       g_value_set_enum (value, priv->mode);
409       break;
410     case PROP_VISIBLE:
411       g_value_set_boolean (value, priv->visible);
412       break;
413     case PROP_SENSITIVE:
414       g_value_set_boolean (value, priv->sensitive);
415       break;
416     case PROP_EDITING:
417       g_value_set_boolean (value, priv->editing);
418       break;
419     case PROP_XALIGN:
420       g_value_set_float (value, priv->xalign);
421       break;
422     case PROP_YALIGN:
423       g_value_set_float (value, priv->yalign);
424       break;
425     case PROP_XPAD:
426       g_value_set_uint (value, priv->xpad);
427       break;
428     case PROP_YPAD:
429       g_value_set_uint (value, priv->ypad);
430       break;
431     case PROP_WIDTH:
432       g_value_set_int (value, priv->width);
433       break;
434     case PROP_HEIGHT:
435       g_value_set_int (value, priv->height);
436       break;
437     case PROP_IS_EXPANDER:
438       g_value_set_boolean (value, priv->is_expander);
439       break;
440     case PROP_IS_EXPANDED:
441       g_value_set_boolean (value, priv->is_expanded);
442       break;
443     case PROP_CELL_BACKGROUND_GDK:
444       {
445         GdkColor color;
446
447         color.red = priv->cell_background.red;
448         color.green = priv->cell_background.green;
449         color.blue = priv->cell_background.blue;
450
451         g_value_set_boxed (value, &color);
452       }
453       break;
454     case PROP_CELL_BACKGROUND_SET:
455       g_value_set_boolean (value, priv->cell_background_set);
456       break;
457     case PROP_CELL_BACKGROUND:
458     default:
459       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
460       break;
461     }
462
463 }
464
465 static void
466 gtk_cell_renderer_set_property (GObject      *object,
467                                 guint         param_id,
468                                 const GValue *value,
469                                 GParamSpec   *pspec)
470 {
471   GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
472   GtkCellRendererPrivate *priv = cell->priv;
473
474   switch (param_id)
475     {
476     case PROP_MODE:
477       priv->mode = g_value_get_enum (value);
478       break;
479     case PROP_VISIBLE:
480       priv->visible = g_value_get_boolean (value);
481       break;
482     case PROP_SENSITIVE:
483       priv->sensitive = g_value_get_boolean (value);
484       break;
485     case PROP_EDITING:
486       priv->editing = g_value_get_boolean (value);
487       break;
488     case PROP_XALIGN:
489       priv->xalign = g_value_get_float (value);
490       break;
491     case PROP_YALIGN:
492       priv->yalign = g_value_get_float (value);
493       break;
494     case PROP_XPAD:
495       priv->xpad = g_value_get_uint (value);
496       break;
497     case PROP_YPAD:
498       priv->ypad = g_value_get_uint (value);
499       break;
500     case PROP_WIDTH:
501       priv->width = g_value_get_int (value);
502       break;
503     case PROP_HEIGHT:
504       priv->height = g_value_get_int (value);
505       break;
506     case PROP_IS_EXPANDER:
507       priv->is_expander = g_value_get_boolean (value);
508       break;
509     case PROP_IS_EXPANDED:
510       priv->is_expanded = g_value_get_boolean (value);
511       break;
512     case PROP_CELL_BACKGROUND:
513       {
514         GdkColor color;
515
516         if (!g_value_get_string (value))
517           set_cell_bg_color (cell, NULL);
518         else if (gdk_color_parse (g_value_get_string (value), &color))
519           set_cell_bg_color (cell, &color);
520         else
521           g_warning ("Don't know color `%s'", g_value_get_string (value));
522
523         g_object_notify (object, "cell-background-gdk");
524       }
525       break;
526     case PROP_CELL_BACKGROUND_GDK:
527       set_cell_bg_color (cell, g_value_get_boxed (value));
528       break;
529     case PROP_CELL_BACKGROUND_SET:
530       priv->cell_background_set = g_value_get_boolean (value);
531       break;
532     default:
533       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
534       break;
535     }
536 }
537
538 static void
539 set_cell_bg_color (GtkCellRenderer *cell,
540                    GdkColor        *color)
541 {
542   GtkCellRendererPrivate *priv = cell->priv;
543
544   if (color)
545     {
546       if (!priv->cell_background_set)
547         {
548           priv->cell_background_set = TRUE;
549           g_object_notify (G_OBJECT (cell), "cell-background-set");
550         }
551
552       priv->cell_background.red = color->red;
553       priv->cell_background.green = color->green;
554       priv->cell_background.blue = color->blue;
555     }
556   else
557     {
558       if (priv->cell_background_set)
559         {
560           priv->cell_background_set = FALSE;
561           g_object_notify (G_OBJECT (cell), "cell-background-set");
562         }
563     }
564 }
565
566 /**
567  * gtk_cell_renderer_get_size:
568  * @cell: a #GtkCellRenderer
569  * @widget: the widget the renderer is rendering to
570  * @cell_area: (allow-none): The area a cell will be allocated, or %NULL
571  * @x_offset: (allow-none): location to return x offset of cell relative to @cell_area, or %NULL
572  * @y_offset: (allow-none): location to return y offset of cell relative to @cell_area, or %NULL
573  * @width: (allow-none): location to return width needed to render a cell, or %NULL
574  * @height: (allow-none): location to return height needed to render a cell, or %NULL
575  *
576  * Obtains the width and height needed to render the cell. Used by view 
577  * widgets to determine the appropriate size for the cell_area passed to
578  * gtk_cell_renderer_render().  If @cell_area is not %NULL, fills in the
579  * x and y offsets (if set) of the cell relative to this location. 
580  *
581  * Please note that the values set in @width and @height, as well as those 
582  * in @x_offset and @y_offset are inclusive of the xpad and ypad properties.
583  *
584  *
585  * Deprecated: 3.0: Use gtk_cell_size_request_get_size() instead.
586  **/
587 void
588 gtk_cell_renderer_get_size (GtkCellRenderer    *cell,
589                             GtkWidget          *widget,
590                             const GdkRectangle *cell_area,
591                             gint               *x_offset,
592                             gint               *y_offset,
593                             gint               *width,
594                             gint               *height)
595 {
596   GtkRequisition request;
597
598   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
599
600   gtk_cell_size_request_get_size (GTK_CELL_SIZE_REQUEST (cell),
601                                   widget, &request, NULL);
602
603   if (width)
604     *width = request.width;
605   
606   if (height)
607     *height = request.height;
608
609   if (cell_area)
610     _gtk_cell_renderer_calc_offset (cell, cell_area, gtk_widget_get_direction (widget),
611                                     request.width, request.height, x_offset, y_offset);
612 }
613
614 /**
615  * gtk_cell_renderer_render:
616  * @cell: a #GtkCellRenderer
617  * @window: a #GdkDrawable to draw to
618  * @widget: the widget owning @window
619  * @background_area: entire cell area (including tree expanders and maybe 
620  *    padding on the sides)
621  * @cell_area: area normally rendered by a cell renderer
622  * @expose_area: area that actually needs updating
623  * @flags: flags that affect rendering
624  *
625  * Invokes the virtual render function of the #GtkCellRenderer. The three
626  * passed-in rectangles are areas of @window. Most renderers will draw within
627  * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer
628  * should be honored with respect to @cell_area. @background_area includes the
629  * blank space around the cell, and also the area containing the tree expander;
630  * so the @background_area rectangles for all cells tile to cover the entire
631  * @window.  @expose_area is a clip rectangle.
632  **/
633 void
634 gtk_cell_renderer_render (GtkCellRenderer      *cell,
635                           GdkWindow            *window,
636                           GtkWidget            *widget,
637                           const GdkRectangle   *background_area,
638                           const GdkRectangle   *cell_area,
639                           const GdkRectangle   *expose_area,
640                           GtkCellRendererState  flags)
641 {
642   gboolean selected = FALSE;
643   GtkCellRendererPrivate *priv = cell->priv;
644
645   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
646   g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
647
648   selected = (flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED;
649
650   if (priv->cell_background_set && !selected)
651     {
652       cairo_t *cr = gdk_cairo_create (window);
653
654       gdk_cairo_rectangle (cr, background_area);
655       gdk_cairo_set_source_color (cr, &priv->cell_background);
656       cairo_fill (cr);
657       
658       cairo_destroy (cr);
659     }
660
661   GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
662                                               window,
663                                               widget,
664                                               (GdkRectangle *) background_area,
665                                               (GdkRectangle *) cell_area,
666                                               (GdkRectangle *) expose_area,
667                                               flags);
668 }
669
670 /**
671  * gtk_cell_renderer_activate:
672  * @cell: a #GtkCellRenderer
673  * @event: a #GdkEvent
674  * @widget: widget that received the event
675  * @path: widget-dependent string representation of the event location; 
676  *    e.g. for #GtkTreeView, a string representation of #GtkTreePath
677  * @background_area: background area as passed to gtk_cell_renderer_render()
678  * @cell_area: cell area as passed to gtk_cell_renderer_render()
679  * @flags: render flags
680  *
681  * Passes an activate event to the cell renderer for possible processing.  
682  * Some cell renderers may use events; for example, #GtkCellRendererToggle 
683  * toggles when it gets a mouse click.
684  *
685  * Return value: %TRUE if the event was consumed/handled
686  **/
687 gboolean
688 gtk_cell_renderer_activate (GtkCellRenderer      *cell,
689                             GdkEvent             *event,
690                             GtkWidget            *widget,
691                             const gchar          *path,
692                             const GdkRectangle   *background_area,
693                             const GdkRectangle   *cell_area,
694                             GtkCellRendererState  flags)
695 {
696   GtkCellRendererPrivate *priv;
697
698   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
699
700   priv = cell->priv;
701
702   if (priv->mode != GTK_CELL_RENDERER_MODE_ACTIVATABLE)
703     return FALSE;
704
705   if (GTK_CELL_RENDERER_GET_CLASS (cell)->activate == NULL)
706     return FALSE;
707
708   return GTK_CELL_RENDERER_GET_CLASS (cell)->activate (cell,
709                                                        event,
710                                                        widget,
711                                                        path,
712                                                        (GdkRectangle *) background_area,
713                                                        (GdkRectangle *) cell_area,
714                                                        flags);
715 }
716
717 /**
718  * gtk_cell_renderer_start_editing:
719  * @cell: a #GtkCellRenderer
720  * @event: a #GdkEvent
721  * @widget: widget that received the event
722  * @path: widget-dependent string representation of the event location; 
723  *    e.g. for #GtkTreeView, a string representation of #GtkTreePath
724  * @background_area: background area as passed to gtk_cell_renderer_render()
725  * @cell_area: cell area as passed to gtk_cell_renderer_render()
726  * @flags: render flags
727  * 
728  * Passes an activate event to the cell renderer for possible processing.
729  * 
730  * Return value: A new #GtkCellEditable, or %NULL
731  **/
732 GtkCellEditable *
733 gtk_cell_renderer_start_editing (GtkCellRenderer      *cell,
734                                  GdkEvent             *event,
735                                  GtkWidget            *widget,
736                                  const gchar          *path,
737                                  const GdkRectangle   *background_area,
738                                  const GdkRectangle   *cell_area,
739                                  GtkCellRendererState  flags)
740
741 {
742   GtkCellRendererPrivate *priv;
743   GtkCellEditable *editable;
744
745   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), NULL);
746
747   priv = cell->priv;
748
749   if (priv->mode != GTK_CELL_RENDERER_MODE_EDITABLE)
750     return NULL;
751
752   if (GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing == NULL)
753     return NULL;
754
755   editable = GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing (cell,
756                                                                 event,
757                                                                 widget,
758                                                                 path,
759                                                                 (GdkRectangle *) background_area,
760                                                                 (GdkRectangle *) cell_area,
761                                                                 flags);
762
763   g_signal_emit (cell, 
764                  cell_renderer_signals[EDITING_STARTED], 0,
765                  editable, path);
766
767   priv->editing = TRUE;
768
769   return editable;
770 }
771
772 /**
773  * gtk_cell_renderer_set_fixed_size:
774  * @cell: A #GtkCellRenderer
775  * @width: the width of the cell renderer, or -1
776  * @height: the height of the cell renderer, or -1
777  *
778  * Sets the renderer size to be explicit, independent of the properties set.
779  **/
780 void
781 gtk_cell_renderer_set_fixed_size (GtkCellRenderer *cell,
782                                   gint             width,
783                                   gint             height)
784 {
785   GtkCellRendererPrivate *priv;
786
787   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
788   g_return_if_fail (width >= -1 && height >= -1);
789
790   priv = cell->priv;
791
792   if ((width != priv->width) || (height != priv->height))
793     {
794       g_object_freeze_notify (G_OBJECT (cell));
795
796       if (width != priv->width)
797         {
798           priv->width = width;
799           g_object_notify (G_OBJECT (cell), "width");
800         }
801
802       if (height != priv->height)
803         {
804           priv->height = height;
805           g_object_notify (G_OBJECT (cell), "height");
806         }
807
808       g_object_thaw_notify (G_OBJECT (cell));
809     }
810 }
811
812 /**
813  * gtk_cell_renderer_get_fixed_size:
814  * @cell: A #GtkCellRenderer
815  * @width: (allow-none): location to fill in with the fixed width of the cell, or %NULL
816  * @height: (allow-none): location to fill in with the fixed height of the cell, or %NULL
817  *
818  * Fills in @width and @height with the appropriate size of @cell.
819  **/
820 void
821 gtk_cell_renderer_get_fixed_size (GtkCellRenderer *cell,
822                                   gint            *width,
823                                   gint            *height)
824 {
825   GtkCellRendererPrivate *priv;
826
827   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
828
829   priv = cell->priv;
830
831   if (width)
832     *width = priv->width;
833   if (height)
834     *height = priv->height;
835 }
836
837 /**
838  * gtk_cell_renderer_set_alignment:
839  * @cell: A #GtkCellRenderer
840  * @xalign: the x alignment of the cell renderer
841  * @yalign: the y alignment of the cell renderer
842  *
843  * Sets the renderer's alignment within its available space.
844  *
845  * Since: 2.18
846  **/
847 void
848 gtk_cell_renderer_set_alignment (GtkCellRenderer *cell,
849                                  gfloat           xalign,
850                                  gfloat           yalign)
851 {
852   GtkCellRendererPrivate *priv;
853
854   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
855   g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
856   g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
857
858   priv = cell->priv;
859
860   if ((xalign != priv->xalign) || (yalign != priv->yalign))
861     {
862       g_object_freeze_notify (G_OBJECT (cell));
863
864       if (xalign != priv->xalign)
865         {
866           priv->xalign = xalign;
867           g_object_notify (G_OBJECT (cell), "xalign");
868         }
869
870       if (yalign != priv->yalign)
871         {
872           priv->yalign = yalign;
873           g_object_notify (G_OBJECT (cell), "yalign");
874         }
875
876       g_object_thaw_notify (G_OBJECT (cell));
877     }
878 }
879
880 /**
881  * gtk_cell_renderer_get_alignment:
882  * @cell: A #GtkCellRenderer
883  * @xalign: (allow-none): location to fill in with the x alignment of the cell, or %NULL
884  * @yalign: (allow-none): location to fill in with the y alignment of the cell, or %NULL
885  *
886  * Fills in @xalign and @yalign with the appropriate values of @cell.
887  *
888  * Since: 2.18
889  **/
890 void
891 gtk_cell_renderer_get_alignment (GtkCellRenderer *cell,
892                                  gfloat          *xalign,
893                                  gfloat          *yalign)
894 {
895   GtkCellRendererPrivate *priv;
896
897   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
898
899   priv = cell->priv;
900
901   if (xalign)
902     *xalign = priv->xalign;
903   if (yalign)
904     *yalign = priv->yalign;
905 }
906
907 /**
908  * gtk_cell_renderer_set_padding:
909  * @cell: A #GtkCellRenderer
910  * @xpad: the x padding of the cell renderer
911  * @ypad: the y padding of the cell renderer
912  *
913  * Sets the renderer's padding.
914  *
915  * Since: 2.18
916  **/
917 void
918 gtk_cell_renderer_set_padding (GtkCellRenderer *cell,
919                                gint             xpad,
920                                gint             ypad)
921 {
922   GtkCellRendererPrivate *priv;
923
924   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
925   g_return_if_fail (xpad >= 0 && xpad >= 0);
926
927   priv = cell->priv;
928
929   if ((xpad != priv->xpad) || (ypad != priv->ypad))
930     {
931       g_object_freeze_notify (G_OBJECT (cell));
932
933       if (xpad != priv->xpad)
934         {
935           priv->xpad = xpad;
936           g_object_notify (G_OBJECT (cell), "xpad");
937         }
938
939       if (ypad != priv->ypad)
940         {
941           priv->ypad = ypad;
942           g_object_notify (G_OBJECT (cell), "ypad");
943         }
944
945       g_object_thaw_notify (G_OBJECT (cell));
946     }
947 }
948
949 /**
950  * gtk_cell_renderer_get_padding:
951  * @cell: A #GtkCellRenderer
952  * @xpad: (allow-none): location to fill in with the x padding of the cell, or %NULL
953  * @ypad: (allow-none): location to fill in with the y padding of the cell, or %NULL
954  *
955  * Fills in @xpad and @ypad with the appropriate values of @cell.
956  *
957  * Since: 2.18
958  **/
959 void
960 gtk_cell_renderer_get_padding (GtkCellRenderer *cell,
961                                gint            *xpad,
962                                gint            *ypad)
963 {
964   GtkCellRendererPrivate *priv;
965
966   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
967
968   priv = cell->priv;
969
970   if (xpad)
971     *xpad = priv->xpad;
972   if (ypad)
973     *ypad = priv->ypad;
974 }
975
976 /**
977  * gtk_cell_renderer_set_visible:
978  * @cell: A #GtkCellRenderer
979  * @visible: the visibility of the cell
980  *
981  * Sets the cell renderer's visibility.
982  *
983  * Since: 2.18
984  **/
985 void
986 gtk_cell_renderer_set_visible (GtkCellRenderer *cell,
987                                gboolean         visible)
988 {
989   GtkCellRendererPrivate *priv;
990
991   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
992
993   priv = cell->priv;
994
995   if (priv->visible != visible)
996     {
997       priv->visible = visible ? TRUE : FALSE;
998       g_object_notify (G_OBJECT (cell), "visible");
999     }
1000 }
1001
1002 /**
1003  * gtk_cell_renderer_get_visible:
1004  * @cell: A #GtkCellRenderer
1005  *
1006  * Returns the cell renderer's visibility.
1007  *
1008  * Returns: %TRUE if the cell renderer is visible
1009  *
1010  * Since: 2.18
1011  */
1012 gboolean
1013 gtk_cell_renderer_get_visible (GtkCellRenderer *cell)
1014 {
1015   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1016
1017   return cell->priv->visible;
1018 }
1019
1020 /**
1021  * gtk_cell_renderer_set_sensitive:
1022  * @cell: A #GtkCellRenderer
1023  * @sensitive: the sensitivity of the cell
1024  *
1025  * Sets the cell renderer's sensitivity.
1026  *
1027  * Since: 2.18
1028  **/
1029 void
1030 gtk_cell_renderer_set_sensitive (GtkCellRenderer *cell,
1031                                  gboolean         sensitive)
1032 {
1033   GtkCellRendererPrivate *priv;
1034
1035   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1036
1037   priv = cell->priv;
1038
1039   if (priv->sensitive != sensitive)
1040     {
1041       priv->sensitive = sensitive ? TRUE : FALSE;
1042       g_object_notify (G_OBJECT (cell), "sensitive");
1043     }
1044 }
1045
1046 /**
1047  * gtk_cell_renderer_get_sensitive:
1048  * @cell: A #GtkCellRenderer
1049  *
1050  * Returns the cell renderer's sensitivity.
1051  *
1052  * Returns: %TRUE if the cell renderer is sensitive
1053  *
1054  * Since: 2.18
1055  */
1056 gboolean
1057 gtk_cell_renderer_get_sensitive (GtkCellRenderer *cell)
1058 {
1059   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1060
1061   return cell->priv->sensitive;
1062 }
1063
1064 /**
1065  * gtk_cell_renderer_stop_editing:
1066  * @cell: A #GtkCellRenderer
1067  * @canceled: %TRUE if the editing has been canceled
1068  * 
1069  * Informs the cell renderer that the editing is stopped.
1070  * If @canceled is %TRUE, the cell renderer will emit the 
1071  * #GtkCellRenderer::editing-canceled signal. 
1072  *
1073  * This function should be called by cell renderer implementations 
1074  * in response to the #GtkCellEditable::editing-done signal of 
1075  * #GtkCellEditable.
1076  *
1077  * Since: 2.6
1078  **/
1079 void
1080 gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
1081                                 gboolean         canceled)
1082 {
1083   GtkCellRendererPrivate *priv;
1084
1085   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1086
1087   priv = cell->priv;
1088
1089   if (priv->editing)
1090     {
1091       priv->editing = FALSE;
1092       if (canceled)
1093         g_signal_emit (cell, cell_renderer_signals[EDITING_CANCELED], 0);
1094     }
1095 }
1096
1097 static void
1098 gtk_cell_renderer_cell_size_request_init (GtkCellSizeRequestIface *iface)
1099 {
1100   iface->get_width     = gtk_cell_renderer_get_width;
1101   iface->get_height    = gtk_cell_renderer_get_height;
1102
1103   iface->get_width_for_height  = gtk_cell_renderer_get_width_for_height;
1104   iface->get_height_for_width  = gtk_cell_renderer_get_height_for_width;
1105 }
1106
1107 static void
1108 gtk_cell_renderer_get_desired_size (GtkCellSizeRequest   *cell,
1109                                     GtkWidget         *widget,
1110                                     GtkOrientation     orientation,
1111                                     gint              *minimum_size,
1112                                     gint              *natural_size)
1113 {
1114   GtkRequisition min_req;
1115
1116   /* Fallback on the old API to get the size. */
1117   if (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size)
1118     GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (GTK_CELL_RENDERER (cell), widget, NULL, NULL, NULL,
1119                                                   &min_req.width, &min_req.height);
1120   else
1121     {
1122       min_req.width = 0;
1123       min_req.height = 0;
1124     }
1125
1126   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1127     {
1128       if (minimum_size)
1129         *minimum_size = min_req.width;
1130
1131       if (natural_size)
1132         *natural_size = min_req.width;
1133     }
1134   else
1135     {
1136       if (minimum_size)
1137         *minimum_size = min_req.height;
1138
1139       if (natural_size)
1140         *natural_size = min_req.height;
1141     }
1142 }
1143
1144 static void
1145 gtk_cell_renderer_get_width (GtkCellSizeRequest   *cell,
1146                              GtkWidget         *widget,
1147                              gint              *minimum_size,
1148                              gint              *natural_size)
1149 {
1150   gtk_cell_renderer_get_desired_size (cell, widget, GTK_ORIENTATION_HORIZONTAL, 
1151                                       minimum_size, natural_size);
1152 }
1153
1154 static void
1155 gtk_cell_renderer_get_height (GtkCellSizeRequest   *cell,
1156                               GtkWidget         *widget,
1157                               gint              *minimum_size,
1158                               gint              *natural_size)
1159 {
1160   gtk_cell_renderer_get_desired_size (cell, widget, GTK_ORIENTATION_VERTICAL, 
1161                                       minimum_size, natural_size);
1162 }
1163
1164
1165 static void
1166 gtk_cell_renderer_get_height_for_width (GtkCellSizeRequest *cell,
1167                                         GtkWidget       *widget,
1168                                         gint             width,
1169                                         gint            *minimum_height,
1170                                         gint            *natural_height)
1171 {
1172   /* Fall back on the height reported from ->get_size() */
1173   gtk_cell_size_request_get_height (cell, widget, minimum_height, natural_height);
1174 }
1175
1176 static void
1177 gtk_cell_renderer_get_width_for_height (GtkCellSizeRequest *cell,
1178                                         GtkWidget       *widget,
1179                                         gint             height,
1180                                         gint            *minimum_width,
1181                                         gint            *natural_width)
1182 {
1183   /* Fall back on the width reported from ->get_size() */
1184   gtk_cell_size_request_get_width (cell, widget, minimum_width, natural_width);
1185 }
1186
1187 /* An internal convenience function for some containers to peek at the
1188  * cell alignment in a target allocation (used to draw focus and align
1189  * cells in the icon view).
1190  *
1191  * Note this is only a trivial 'align * (allocation - request)' operation.
1192  */
1193 void
1194 _gtk_cell_renderer_calc_offset    (GtkCellRenderer      *cell,
1195                                    const GdkRectangle   *cell_area,
1196                                    GtkTextDirection      direction,
1197                                    gint                  width,
1198                                    gint                  height,
1199                                    gint                 *x_offset,
1200                                    gint                 *y_offset)
1201
1202   GtkCellRendererPrivate *priv;
1203
1204   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1205   g_return_if_fail (cell_area != NULL);
1206   g_return_if_fail (x_offset || y_offset);
1207
1208   priv = cell->priv;
1209
1210   if (x_offset)
1211     {
1212       *x_offset = (((direction == GTK_TEXT_DIR_RTL) ?
1213                     (1.0 - priv->xalign) : priv->xalign) * 
1214                    (cell_area->width - width));
1215       *x_offset = MAX (*x_offset, 0);
1216     }
1217   if (y_offset)
1218     {
1219       *y_offset = (priv->yalign *
1220                    (cell_area->height - height));
1221       *y_offset = MAX (*y_offset, 0);
1222     }
1223 }