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