]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellrenderer.c
cellrenderer: Use the widget state flags as a base for cell renderers' state
[~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 "gtkintl.h"
23 #include "gtkmarshalers.h"
24 #include "gtktypebuiltins.h"
25 #include "gtkprivate.h"
26 #include "gtktreeprivate.h"
27 #include "a11y/gtkrenderercellaccessible.h"
28
29
30 /**
31  * SECTION:gtkcellrenderer
32  * @Short_description: An object for rendering a single cell
33  * @Title: GtkCellRenderer
34  * @See_also: #GtkCellRendererText, #GtkCellRendererPixbuf, #GtkCellRendererToggle
35  *
36  * The #GtkCellRenderer is a base class of a set of objects used for
37  * rendering a cell to a #cairo_t.  These objects are used primarily by
38  * the #GtkTreeView widget, though they aren't tied to them in any
39  * specific way.  It is worth noting that #GtkCellRenderer is not a
40  * #GtkWidget and cannot be treated as such.
41  *
42  * The primary use of a #GtkCellRenderer is for drawing a certain graphical
43  * elements on a #cairo_t. Typically, one cell renderer is used to
44  * draw many cells on the screen.  To this extent, it isn't expected that a
45  * CellRenderer keep any permanent state around.  Instead, any state is set
46  * just prior to use using #GObject<!-- -->s property system.  Then, the
47  * cell is measured using gtk_cell_renderer_get_size(). Finally, the cell
48  * is rendered in the correct location using gtk_cell_renderer_render().
49  *
50  * There are a number of rules that must be followed when writing a new
51  * #GtkCellRenderer.  First and formost, its important that a certain set
52  * of properties will always yield a cell renderer of the same size,
53  * barring a #GtkStyle change.  The #GtkCellRenderer also has a number of
54  * generic properties that are expected to be honored by all children.
55  *
56  * Beyond merely rendering a cell, cell renderers can optionally
57  * provide active user interface elements. A cell renderer can be
58  * <firstterm>activatable</firstterm> like #GtkCellRendererToggle,
59  * which toggles when it gets activated by a mouse click, or it can be
60  * <firstterm>editable</firstterm> like #GtkCellRendererText, which
61  * allows the user to edit the text using a #GtkEntry.
62  * To make a cell renderer activatable or editable, you have to
63  * implement the #GtkCellRendererClass.activate or
64  * #GtkCellRendererClass.start_editing virtual functions, respectively.
65  */
66
67
68 #define DEBUG_CELL_SIZE_REQUEST 0
69
70 static void gtk_cell_renderer_init          (GtkCellRenderer      *cell);
71 static void gtk_cell_renderer_class_init    (GtkCellRendererClass *class);
72 static void gtk_cell_renderer_get_property  (GObject              *object,
73                                              guint                 param_id,
74                                              GValue               *value,
75                                              GParamSpec           *pspec);
76 static void gtk_cell_renderer_set_property  (GObject              *object,
77                                              guint                 param_id,
78                                              const GValue         *value,
79                                              GParamSpec           *pspec);
80 static void set_cell_bg_color               (GtkCellRenderer      *cell,
81                                              GdkRGBA              *rgba);
82
83 /* Fallback GtkCellRenderer    implementation to use remaining ->get_size() implementations */
84 static GtkSizeRequestMode gtk_cell_renderer_real_get_request_mode(GtkCellRenderer         *cell);
85 static void gtk_cell_renderer_real_get_preferred_width           (GtkCellRenderer         *cell,
86                                                                   GtkWidget               *widget,
87                                                                   gint                    *minimum_size,
88                                                                   gint                    *natural_size);
89 static void gtk_cell_renderer_real_get_preferred_height          (GtkCellRenderer         *cell,
90                                                                   GtkWidget               *widget,
91                                                                   gint                    *minimum_size,
92                                                                   gint                    *natural_size);
93 static void gtk_cell_renderer_real_get_preferred_height_for_width(GtkCellRenderer         *cell,
94                                                                   GtkWidget               *widget,
95                                                                   gint                     width,
96                                                                   gint                    *minimum_height,
97                                                                   gint                    *natural_height);
98 static void gtk_cell_renderer_real_get_preferred_width_for_height(GtkCellRenderer         *cell,
99                                                                   GtkWidget               *widget,
100                                                                   gint                     height,
101                                                                   gint                    *minimum_width,
102                                                                   gint                    *natural_width);
103 static void gtk_cell_renderer_real_get_aligned_area              (GtkCellRenderer         *cell,
104                                                                   GtkWidget               *widget,
105                                                                   GtkCellRendererState     flags,
106                                                                   const GdkRectangle      *cell_area,
107                                                                   GdkRectangle            *aligned_area);
108
109
110 struct _GtkCellRendererPrivate
111 {
112   gfloat xalign;
113   gfloat yalign;
114
115   gint width;
116   gint height;
117
118   guint16 xpad;
119   guint16 ypad;
120
121   guint mode                : 2;
122   guint visible             : 1;
123   guint is_expander         : 1;
124   guint is_expanded         : 1;
125   guint cell_background_set : 1;
126   guint sensitive           : 1;
127   guint editing             : 1;
128
129   GdkRGBA cell_background;
130 };
131
132 struct _GtkCellRendererClassPrivate
133 {
134   GType accessible_type;
135 };
136
137 enum {
138   PROP_0,
139   PROP_MODE,
140   PROP_VISIBLE,
141   PROP_SENSITIVE,
142   PROP_XALIGN,
143   PROP_YALIGN,
144   PROP_XPAD,
145   PROP_YPAD,
146   PROP_WIDTH,
147   PROP_HEIGHT,
148   PROP_IS_EXPANDER,
149   PROP_IS_EXPANDED,
150   PROP_CELL_BACKGROUND,
151   PROP_CELL_BACKGROUND_GDK,
152   PROP_CELL_BACKGROUND_RGBA,
153   PROP_CELL_BACKGROUND_SET,
154   PROP_EDITING
155 };
156
157 /* Signal IDs */
158 enum {
159   EDITING_CANCELED,
160   EDITING_STARTED,
161   LAST_SIGNAL
162 };
163
164 static guint  cell_renderer_signals[LAST_SIGNAL] = { 0 };
165
166 static void
167 gtk_cell_renderer_init (GtkCellRenderer *cell)
168 {
169   GtkCellRendererPrivate *priv;
170
171   cell->priv = G_TYPE_INSTANCE_GET_PRIVATE (cell,
172                                             GTK_TYPE_CELL_RENDERER,
173                                             GtkCellRendererPrivate);
174   priv = cell->priv;
175
176   priv->mode = GTK_CELL_RENDERER_MODE_INERT;
177   priv->visible = TRUE;
178   priv->width = -1;
179   priv->height = -1;
180   priv->xalign = 0.5;
181   priv->yalign = 0.5;
182   priv->xpad = 0;
183   priv->ypad = 0;
184   priv->sensitive = TRUE;
185   priv->is_expander = FALSE;
186   priv->is_expanded = FALSE;
187   priv->editing = FALSE;
188 }
189
190 static void
191 gtk_cell_renderer_class_init (GtkCellRendererClass *class)
192 {
193   GObjectClass *object_class = G_OBJECT_CLASS (class);
194
195   object_class->get_property = gtk_cell_renderer_get_property;
196   object_class->set_property = gtk_cell_renderer_set_property;
197
198   class->render = NULL;
199   class->get_size = NULL;
200   class->get_request_mode               = gtk_cell_renderer_real_get_request_mode;
201   class->get_preferred_width            = gtk_cell_renderer_real_get_preferred_width;
202   class->get_preferred_height           = gtk_cell_renderer_real_get_preferred_height;
203   class->get_preferred_width_for_height = gtk_cell_renderer_real_get_preferred_width_for_height;
204   class->get_preferred_height_for_width = gtk_cell_renderer_real_get_preferred_height_for_width;
205   class->get_aligned_area               = gtk_cell_renderer_real_get_aligned_area;
206
207   /**
208    * GtkCellRenderer::editing-canceled:
209    * @renderer: the object which received the signal
210    *
211    * This signal gets emitted when the user cancels the process of editing a
212    * cell.  For example, an editable cell renderer could be written to cancel
213    * editing when the user presses Escape. 
214    *
215    * See also: gtk_cell_renderer_stop_editing().
216    *
217    * Since: 2.4
218    */
219   cell_renderer_signals[EDITING_CANCELED] =
220     g_signal_new (I_("editing-canceled"),
221                   G_OBJECT_CLASS_TYPE (object_class),
222                   G_SIGNAL_RUN_FIRST,
223                   G_STRUCT_OFFSET (GtkCellRendererClass, editing_canceled),
224                   NULL, NULL,
225                   _gtk_marshal_VOID__VOID,
226                   G_TYPE_NONE, 0);
227
228   /**
229    * GtkCellRenderer::editing-started:
230    * @renderer: the object which received the signal
231    * @editable: the #GtkCellEditable
232    * @path: the path identifying the edited cell
233    *
234    * This signal gets emitted when a cell starts to be edited.
235    * The intended use of this signal is to do special setup
236    * on @editable, e.g. adding a #GtkEntryCompletion or setting
237    * up additional columns in a #GtkComboBox.
238    *
239    * Note that GTK+ doesn't guarantee that cell renderers will
240    * continue to use the same kind of widget for editing in future
241    * releases, therefore you should check the type of @editable
242    * before doing any specific setup, as in the following example:
243    * |[
244    * static void
245    * text_editing_started (GtkCellRenderer *cell,
246    *                       GtkCellEditable *editable,
247    *                       const gchar     *path,
248    *                       gpointer         data)
249    * {
250    *   if (GTK_IS_ENTRY (editable)) 
251    *     {
252    *       GtkEntry *entry = GTK_ENTRY (editable);
253    *       
254    *       /&ast; ... create a GtkEntryCompletion &ast;/
255    *       
256    *       gtk_entry_set_completion (entry, completion);
257    *     }
258    * }
259    * ]|
260    *
261    * Since: 2.6
262    */
263   cell_renderer_signals[EDITING_STARTED] =
264     g_signal_new (I_("editing-started"),
265                   G_OBJECT_CLASS_TYPE (object_class),
266                   G_SIGNAL_RUN_FIRST,
267                   G_STRUCT_OFFSET (GtkCellRendererClass, editing_started),
268                   NULL, NULL,
269                   _gtk_marshal_VOID__OBJECT_STRING,
270                   G_TYPE_NONE, 2,
271                   GTK_TYPE_CELL_EDITABLE,
272                   G_TYPE_STRING);
273
274   g_object_class_install_property (object_class,
275                                    PROP_MODE,
276                                    g_param_spec_enum ("mode",
277                                                       P_("mode"),
278                                                       P_("Editable mode of the CellRenderer"),
279                                                       GTK_TYPE_CELL_RENDERER_MODE,
280                                                       GTK_CELL_RENDERER_MODE_INERT,
281                                                       GTK_PARAM_READWRITE));
282
283   g_object_class_install_property (object_class,
284                                    PROP_VISIBLE,
285                                    g_param_spec_boolean ("visible",
286                                                          P_("visible"),
287                                                          P_("Display the cell"),
288                                                          TRUE,
289                                                          GTK_PARAM_READWRITE));
290   g_object_class_install_property (object_class,
291                                    PROP_SENSITIVE,
292                                    g_param_spec_boolean ("sensitive",
293                                                          P_("Sensitive"),
294                                                          P_("Display the cell sensitive"),
295                                                          TRUE,
296                                                          GTK_PARAM_READWRITE));
297
298   g_object_class_install_property (object_class,
299                                    PROP_XALIGN,
300                                    g_param_spec_float ("xalign",
301                                                        P_("xalign"),
302                                                        P_("The x-align"),
303                                                        0.0,
304                                                        1.0,
305                                                        0.5,
306                                                        GTK_PARAM_READWRITE));
307
308   g_object_class_install_property (object_class,
309                                    PROP_YALIGN,
310                                    g_param_spec_float ("yalign",
311                                                        P_("yalign"),
312                                                        P_("The y-align"),
313                                                        0.0,
314                                                        1.0,
315                                                        0.5,
316                                                        GTK_PARAM_READWRITE));
317
318   g_object_class_install_property (object_class,
319                                    PROP_XPAD,
320                                    g_param_spec_uint ("xpad",
321                                                       P_("xpad"),
322                                                       P_("The xpad"),
323                                                       0,
324                                                       G_MAXUINT,
325                                                       0,
326                                                       GTK_PARAM_READWRITE));
327
328   g_object_class_install_property (object_class,
329                                    PROP_YPAD,
330                                    g_param_spec_uint ("ypad",
331                                                       P_("ypad"),
332                                                       P_("The ypad"),
333                                                       0,
334                                                       G_MAXUINT,
335                                                       0,
336                                                       GTK_PARAM_READWRITE));
337
338   g_object_class_install_property (object_class,
339                                    PROP_WIDTH,
340                                    g_param_spec_int ("width",
341                                                      P_("width"),
342                                                      P_("The fixed width"),
343                                                      -1,
344                                                      G_MAXINT,
345                                                      -1,
346                                                      GTK_PARAM_READWRITE));
347
348   g_object_class_install_property (object_class,
349                                    PROP_HEIGHT,
350                                    g_param_spec_int ("height",
351                                                      P_("height"),
352                                                      P_("The fixed height"),
353                                                      -1,
354                                                      G_MAXINT,
355                                                      -1,
356                                                      GTK_PARAM_READWRITE));
357
358   g_object_class_install_property (object_class,
359                                    PROP_IS_EXPANDER,
360                                    g_param_spec_boolean ("is-expander",
361                                                          P_("Is Expander"),
362                                                          P_("Row has children"),
363                                                          FALSE,
364                                                          GTK_PARAM_READWRITE));
365
366
367   g_object_class_install_property (object_class,
368                                    PROP_IS_EXPANDED,
369                                    g_param_spec_boolean ("is-expanded",
370                                                          P_("Is Expanded"),
371                                                          P_("Row is an expander row, and is expanded"),
372                                                          FALSE,
373                                                          GTK_PARAM_READWRITE));
374
375   g_object_class_install_property (object_class,
376                                    PROP_CELL_BACKGROUND,
377                                    g_param_spec_string ("cell-background",
378                                                         P_("Cell background color name"),
379                                                         P_("Cell background color as a string"),
380                                                         NULL,
381                                                         GTK_PARAM_WRITABLE));
382
383   g_object_class_install_property (object_class,
384                                    PROP_CELL_BACKGROUND_GDK,
385                                    g_param_spec_boxed ("cell-background-gdk",
386                                                        P_("Cell background color"),
387                                                        P_("Cell background color as a GdkColor"),
388                                                        GDK_TYPE_COLOR,
389                                                        GTK_PARAM_READWRITE));
390   /**
391    * GtkCellRenderer:cell-background-rgba:
392    *
393    * Cell background as a #GdkRGBA
394    *
395    * Since: 3.0
396    */
397   g_object_class_install_property (object_class,
398                                    PROP_CELL_BACKGROUND_RGBA,
399                                    g_param_spec_boxed ("cell-background-rgba",
400                                                        P_("Cell background RGBA color"),
401                                                        P_("Cell background color as a GdkRGBA"),
402                                                        GDK_TYPE_RGBA,
403                                                        GTK_PARAM_READWRITE));
404
405   g_object_class_install_property (object_class,
406                                    PROP_EDITING,
407                                    g_param_spec_boolean ("editing",
408                                                          P_("Editing"),
409                                                          P_("Whether the cell renderer is currently in editing mode"),
410                                                          FALSE,
411                                                          GTK_PARAM_READABLE));
412
413
414 #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))
415
416   ADD_SET_PROP ("cell-background-set", PROP_CELL_BACKGROUND_SET,
417                 P_("Cell background set"),
418                 P_("Whether this tag affects the cell background color"));
419
420   g_type_class_add_private (class, sizeof (GtkCellRendererPrivate));
421
422   _gtk_cell_renderer_class_set_accessible_type (class, GTK_TYPE_RENDERER_CELL_ACCESSIBLE);
423 }
424
425 static void
426 gtk_cell_renderer_base_class_init (gpointer g_class)
427 {
428   GtkCellRendererClass *klass = g_class;
429
430   klass->priv = G_TYPE_CLASS_GET_PRIVATE (g_class, GTK_TYPE_CELL_RENDERER, GtkCellRendererClassPrivate);
431 }
432
433 GType
434 gtk_cell_renderer_get_type (void)
435 {
436   static GType cell_renderer_type = 0;
437
438   if (G_UNLIKELY (cell_renderer_type == 0))
439     {
440       const GTypeInfo cell_renderer_info =
441       {
442         sizeof (GtkCellRendererClass),
443         gtk_cell_renderer_base_class_init,
444         NULL,
445         (GClassInitFunc) gtk_cell_renderer_class_init,
446         NULL,           /* class_finalize */
447         NULL,           /* class_init */
448         sizeof (GtkWidget),
449         0,              /* n_preallocs */
450         (GInstanceInitFunc) gtk_cell_renderer_init,
451         NULL,           /* value_table */
452       };
453       cell_renderer_type = g_type_register_static (G_TYPE_INITIALLY_UNOWNED, "GtkCellRenderer",
454                                                    &cell_renderer_info, G_TYPE_FLAG_ABSTRACT);
455
456       g_type_add_class_private (cell_renderer_type, sizeof (GtkCellRendererClassPrivate));
457     }
458
459   return cell_renderer_type;
460 }
461
462 static void
463 gtk_cell_renderer_get_property (GObject     *object,
464                                 guint        param_id,
465                                 GValue      *value,
466                                 GParamSpec  *pspec)
467 {
468   GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
469   GtkCellRendererPrivate *priv = cell->priv;
470
471   switch (param_id)
472     {
473     case PROP_MODE:
474       g_value_set_enum (value, priv->mode);
475       break;
476     case PROP_VISIBLE:
477       g_value_set_boolean (value, priv->visible);
478       break;
479     case PROP_SENSITIVE:
480       g_value_set_boolean (value, priv->sensitive);
481       break;
482     case PROP_EDITING:
483       g_value_set_boolean (value, priv->editing);
484       break;
485     case PROP_XALIGN:
486       g_value_set_float (value, priv->xalign);
487       break;
488     case PROP_YALIGN:
489       g_value_set_float (value, priv->yalign);
490       break;
491     case PROP_XPAD:
492       g_value_set_uint (value, priv->xpad);
493       break;
494     case PROP_YPAD:
495       g_value_set_uint (value, priv->ypad);
496       break;
497     case PROP_WIDTH:
498       g_value_set_int (value, priv->width);
499       break;
500     case PROP_HEIGHT:
501       g_value_set_int (value, priv->height);
502       break;
503     case PROP_IS_EXPANDER:
504       g_value_set_boolean (value, priv->is_expander);
505       break;
506     case PROP_IS_EXPANDED:
507       g_value_set_boolean (value, priv->is_expanded);
508       break;
509     case PROP_CELL_BACKGROUND_GDK:
510       {
511         GdkColor color;
512
513         color.red = (guint16) (priv->cell_background.red * 65535);
514         color.green = (guint16) (priv->cell_background.green * 65535);
515         color.blue = (guint16) (priv->cell_background.blue * 65535);
516
517         g_value_set_boxed (value, &color);
518       }
519       break;
520     case PROP_CELL_BACKGROUND_RGBA:
521       g_value_set_boxed (value, &priv->cell_background);
522       break;
523     case PROP_CELL_BACKGROUND_SET:
524       g_value_set_boolean (value, priv->cell_background_set);
525       break;
526     case PROP_CELL_BACKGROUND:
527     default:
528       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
529       break;
530     }
531
532 }
533
534 static void
535 gtk_cell_renderer_set_property (GObject      *object,
536                                 guint         param_id,
537                                 const GValue *value,
538                                 GParamSpec   *pspec)
539 {
540   GtkCellRenderer *cell = GTK_CELL_RENDERER (object);
541   GtkCellRendererPrivate *priv = cell->priv;
542
543   switch (param_id)
544     {
545     case PROP_MODE:
546       priv->mode = g_value_get_enum (value);
547       break;
548     case PROP_VISIBLE:
549       priv->visible = g_value_get_boolean (value);
550       break;
551     case PROP_SENSITIVE:
552       priv->sensitive = g_value_get_boolean (value);
553       break;
554     case PROP_EDITING:
555       priv->editing = g_value_get_boolean (value);
556       break;
557     case PROP_XALIGN:
558       priv->xalign = g_value_get_float (value);
559       break;
560     case PROP_YALIGN:
561       priv->yalign = g_value_get_float (value);
562       break;
563     case PROP_XPAD:
564       priv->xpad = g_value_get_uint (value);
565       break;
566     case PROP_YPAD:
567       priv->ypad = g_value_get_uint (value);
568       break;
569     case PROP_WIDTH:
570       priv->width = g_value_get_int (value);
571       break;
572     case PROP_HEIGHT:
573       priv->height = g_value_get_int (value);
574       break;
575     case PROP_IS_EXPANDER:
576       priv->is_expander = g_value_get_boolean (value);
577       break;
578     case PROP_IS_EXPANDED:
579       priv->is_expanded = g_value_get_boolean (value);
580       break;
581     case PROP_CELL_BACKGROUND:
582       {
583         GdkRGBA rgba;
584
585         if (!g_value_get_string (value))
586           set_cell_bg_color (cell, NULL);
587         else if (gdk_rgba_parse (&rgba, g_value_get_string (value)))
588           set_cell_bg_color (cell, &rgba);
589         else
590           g_warning ("Don't know color `%s'", g_value_get_string (value));
591
592         g_object_notify (object, "cell-background-gdk");
593       }
594       break;
595     case PROP_CELL_BACKGROUND_GDK:
596       {
597         GdkColor *color;
598
599         color = g_value_get_boxed (value);
600         if (color)
601           {
602             GdkRGBA rgba;
603
604             rgba.red = color->red / 65535.;
605             rgba.green = color->green / 65535.;
606             rgba.blue = color->blue / 65535.;
607             rgba.alpha = 1;
608
609             set_cell_bg_color (cell, &rgba);
610           }
611         else
612           {
613             set_cell_bg_color (cell, NULL);
614           }
615       }
616       break;
617     case PROP_CELL_BACKGROUND_RGBA:
618       set_cell_bg_color (cell, g_value_get_boxed (value));
619       break;
620     case PROP_CELL_BACKGROUND_SET:
621       priv->cell_background_set = g_value_get_boolean (value);
622       break;
623     default:
624       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
625       break;
626     }
627 }
628
629 static void
630 set_cell_bg_color (GtkCellRenderer *cell,
631                    GdkRGBA         *rgba)
632 {
633   GtkCellRendererPrivate *priv = cell->priv;
634
635   if (rgba)
636     {
637       if (!priv->cell_background_set)
638         {
639           priv->cell_background_set = TRUE;
640           g_object_notify (G_OBJECT (cell), "cell-background-set");
641         }
642
643       priv->cell_background = *rgba;
644     }
645   else
646     {
647       if (priv->cell_background_set)
648         {
649           priv->cell_background_set = FALSE;
650           g_object_notify (G_OBJECT (cell), "cell-background-set");
651         }
652     }
653 }
654
655 /**
656  * gtk_cell_renderer_get_size:
657  * @cell: a #GtkCellRenderer
658  * @widget: the widget the renderer is rendering to
659  * @cell_area: (allow-none): The area a cell will be allocated, or %NULL
660  * @x_offset: (out) (allow-none): location to return x offset of cell relative to @cell_area, or %NULL
661  * @y_offset: (out) (allow-none): location to return y offset of cell relative to @cell_area, or %NULL
662  * @width: (out) (allow-none): location to return width needed to render a cell, or %NULL
663  * @height: (out) (allow-none): location to return height needed to render a cell, or %NULL
664  *
665  * Obtains the width and height needed to render the cell. Used by view 
666  * widgets to determine the appropriate size for the cell_area passed to
667  * gtk_cell_renderer_render().  If @cell_area is not %NULL, fills in the
668  * x and y offsets (if set) of the cell relative to this location. 
669  *
670  * Please note that the values set in @width and @height, as well as those 
671  * in @x_offset and @y_offset are inclusive of the xpad and ypad properties.
672  *
673  *
674  * Deprecated: 3.0: Use gtk_cell_renderer_get_preferred_size() instead.
675  **/
676 void
677 gtk_cell_renderer_get_size (GtkCellRenderer    *cell,
678                             GtkWidget          *widget,
679                             const GdkRectangle *cell_area,
680                             gint               *x_offset,
681                             gint               *y_offset,
682                             gint               *width,
683                             gint               *height)
684 {
685   GtkRequisition request;
686
687   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
688
689   gtk_cell_renderer_get_preferred_size (cell, widget, &request, NULL);
690
691   if (width)
692     *width = request.width;
693   
694   if (height)
695     *height = request.height;
696
697   if (cell_area)
698     _gtk_cell_renderer_calc_offset (cell, cell_area, gtk_widget_get_direction (widget),
699                                     request.width, request.height, x_offset, y_offset);
700 }
701
702 /**
703  * gtk_cell_renderer_render:
704  * @cell: a #GtkCellRenderer
705  * @cr: a cairo context to draw to
706  * @widget: the widget owning @window
707  * @background_area: entire cell area (including tree expanders and maybe 
708  *    padding on the sides)
709  * @cell_area: area normally rendered by a cell renderer
710  * @flags: flags that affect rendering
711  *
712  * Invokes the virtual render function of the #GtkCellRenderer. The three
713  * passed-in rectangles are areas in @cr. Most renderers will draw within
714  * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer
715  * should be honored with respect to @cell_area. @background_area includes the
716  * blank space around the cell, and also the area containing the tree expander;
717  * so the @background_area rectangles for all cells tile to cover the entire
718  * @window.
719  **/
720 void
721 gtk_cell_renderer_render (GtkCellRenderer      *cell,
722                           cairo_t              *cr,
723                           GtkWidget            *widget,
724                           const GdkRectangle   *background_area,
725                           const GdkRectangle   *cell_area,
726                           GtkCellRendererState  flags)
727 {
728   gboolean selected = FALSE;
729   GtkCellRendererPrivate *priv = cell->priv;
730   GtkStyleContext *context;
731   GtkStateFlags state;
732
733   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
734   g_return_if_fail (GTK_CELL_RENDERER_GET_CLASS (cell)->render != NULL);
735   g_return_if_fail (cr != NULL);
736
737   selected = (flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED;
738
739   cairo_save (cr);
740
741   if (priv->cell_background_set && !selected)
742     {
743       gdk_cairo_rectangle (cr, background_area);
744       gdk_cairo_set_source_rgba (cr, &priv->cell_background);
745       cairo_fill (cr);
746     }
747
748   gdk_cairo_rectangle (cr, background_area);
749   cairo_clip (cr);
750
751   context = gtk_widget_get_style_context (widget);
752
753   gtk_style_context_save (context);
754   gtk_style_context_add_class (context, GTK_STYLE_CLASS_CELL);
755
756   state = gtk_cell_renderer_get_state (cell, widget, flags);
757   gtk_style_context_set_state (context, state);
758
759   GTK_CELL_RENDERER_GET_CLASS (cell)->render (cell,
760                                               cr,
761                                               widget,
762                                               background_area,
763                                               cell_area,
764                                               flags);
765   gtk_style_context_restore (context);
766   cairo_restore (cr);
767 }
768
769 /**
770  * gtk_cell_renderer_activate:
771  * @cell: a #GtkCellRenderer
772  * @event: a #GdkEvent
773  * @widget: widget that received the event
774  * @path: widget-dependent string representation of the event location; 
775  *    e.g. for #GtkTreeView, a string representation of #GtkTreePath
776  * @background_area: background area as passed to gtk_cell_renderer_render()
777  * @cell_area: cell area as passed to gtk_cell_renderer_render()
778  * @flags: render flags
779  *
780  * Passes an activate event to the cell renderer for possible processing.  
781  * Some cell renderers may use events; for example, #GtkCellRendererToggle 
782  * toggles when it gets a mouse click.
783  *
784  * Return value: %TRUE if the event was consumed/handled
785  **/
786 gboolean
787 gtk_cell_renderer_activate (GtkCellRenderer      *cell,
788                             GdkEvent             *event,
789                             GtkWidget            *widget,
790                             const gchar          *path,
791                             const GdkRectangle   *background_area,
792                             const GdkRectangle   *cell_area,
793                             GtkCellRendererState  flags)
794 {
795   GtkCellRendererPrivate *priv;
796
797   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
798
799   priv = cell->priv;
800
801   if (priv->mode != GTK_CELL_RENDERER_MODE_ACTIVATABLE)
802     return FALSE;
803
804   if (GTK_CELL_RENDERER_GET_CLASS (cell)->activate == NULL)
805     return FALSE;
806
807   return GTK_CELL_RENDERER_GET_CLASS (cell)->activate (cell,
808                                                        event,
809                                                        widget,
810                                                        path,
811                                                        (GdkRectangle *) background_area,
812                                                        (GdkRectangle *) cell_area,
813                                                        flags);
814 }
815
816 /**
817  * gtk_cell_renderer_start_editing:
818  * @cell: a #GtkCellRenderer
819  * @event: a #GdkEvent
820  * @widget: widget that received the event
821  * @path: widget-dependent string representation of the event location;
822  *    e.g. for #GtkTreeView, a string representation of #GtkTreePath
823  * @background_area: background area as passed to gtk_cell_renderer_render()
824  * @cell_area: cell area as passed to gtk_cell_renderer_render()
825  * @flags: render flags
826  *
827  * Passes an activate event to the cell renderer for possible processing.
828  *
829  * Return value: (transfer none): A new #GtkCellEditable, or %NULL
830  **/
831 GtkCellEditable *
832 gtk_cell_renderer_start_editing (GtkCellRenderer      *cell,
833                                  GdkEvent             *event,
834                                  GtkWidget            *widget,
835                                  const gchar          *path,
836                                  const GdkRectangle   *background_area,
837                                  const GdkRectangle   *cell_area,
838                                  GtkCellRendererState  flags)
839
840 {
841   GtkCellRendererPrivate *priv;
842   GtkCellEditable *editable;
843
844   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), NULL);
845
846   priv = cell->priv;
847
848   if (priv->mode != GTK_CELL_RENDERER_MODE_EDITABLE)
849     return NULL;
850
851   if (GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing == NULL)
852     return NULL;
853
854   editable = GTK_CELL_RENDERER_GET_CLASS (cell)->start_editing (cell,
855                                                                 event,
856                                                                 widget,
857                                                                 path,
858                                                                 (GdkRectangle *) background_area,
859                                                                 (GdkRectangle *) cell_area,
860                                                                 flags);
861
862   g_signal_emit (cell, 
863                  cell_renderer_signals[EDITING_STARTED], 0,
864                  editable, path);
865
866   priv->editing = TRUE;
867
868   return editable;
869 }
870
871 /**
872  * gtk_cell_renderer_set_fixed_size:
873  * @cell: A #GtkCellRenderer
874  * @width: the width of the cell renderer, or -1
875  * @height: the height of the cell renderer, or -1
876  *
877  * Sets the renderer size to be explicit, independent of the properties set.
878  **/
879 void
880 gtk_cell_renderer_set_fixed_size (GtkCellRenderer *cell,
881                                   gint             width,
882                                   gint             height)
883 {
884   GtkCellRendererPrivate *priv;
885
886   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
887   g_return_if_fail (width >= -1 && height >= -1);
888
889   priv = cell->priv;
890
891   if ((width != priv->width) || (height != priv->height))
892     {
893       g_object_freeze_notify (G_OBJECT (cell));
894
895       if (width != priv->width)
896         {
897           priv->width = width;
898           g_object_notify (G_OBJECT (cell), "width");
899         }
900
901       if (height != priv->height)
902         {
903           priv->height = height;
904           g_object_notify (G_OBJECT (cell), "height");
905         }
906
907       g_object_thaw_notify (G_OBJECT (cell));
908     }
909 }
910
911 /**
912  * gtk_cell_renderer_get_fixed_size:
913  * @cell: A #GtkCellRenderer
914  * @width: (out) (allow-none): location to fill in with the fixed width of the cell, or %NULL
915  * @height: (out) (allow-none): location to fill in with the fixed height of the cell, or %NULL
916  *
917  * Fills in @width and @height with the appropriate size of @cell.
918  **/
919 void
920 gtk_cell_renderer_get_fixed_size (GtkCellRenderer *cell,
921                                   gint            *width,
922                                   gint            *height)
923 {
924   GtkCellRendererPrivate *priv;
925
926   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
927
928   priv = cell->priv;
929
930   if (width)
931     *width = priv->width;
932   if (height)
933     *height = priv->height;
934 }
935
936 /**
937  * gtk_cell_renderer_set_alignment:
938  * @cell: A #GtkCellRenderer
939  * @xalign: the x alignment of the cell renderer
940  * @yalign: the y alignment of the cell renderer
941  *
942  * Sets the renderer's alignment within its available space.
943  *
944  * Since: 2.18
945  **/
946 void
947 gtk_cell_renderer_set_alignment (GtkCellRenderer *cell,
948                                  gfloat           xalign,
949                                  gfloat           yalign)
950 {
951   GtkCellRendererPrivate *priv;
952
953   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
954   g_return_if_fail (xalign >= 0.0 && xalign <= 1.0);
955   g_return_if_fail (yalign >= 0.0 && yalign <= 1.0);
956
957   priv = cell->priv;
958
959   if ((xalign != priv->xalign) || (yalign != priv->yalign))
960     {
961       g_object_freeze_notify (G_OBJECT (cell));
962
963       if (xalign != priv->xalign)
964         {
965           priv->xalign = xalign;
966           g_object_notify (G_OBJECT (cell), "xalign");
967         }
968
969       if (yalign != priv->yalign)
970         {
971           priv->yalign = yalign;
972           g_object_notify (G_OBJECT (cell), "yalign");
973         }
974
975       g_object_thaw_notify (G_OBJECT (cell));
976     }
977 }
978
979 /**
980  * gtk_cell_renderer_get_alignment:
981  * @cell: A #GtkCellRenderer
982  * @xalign: (out) (allow-none): location to fill in with the x alignment of the cell, or %NULL
983  * @yalign: (out) (allow-none): location to fill in with the y alignment of the cell, or %NULL
984  *
985  * Fills in @xalign and @yalign with the appropriate values of @cell.
986  *
987  * Since: 2.18
988  **/
989 void
990 gtk_cell_renderer_get_alignment (GtkCellRenderer *cell,
991                                  gfloat          *xalign,
992                                  gfloat          *yalign)
993 {
994   GtkCellRendererPrivate *priv;
995
996   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
997
998   priv = cell->priv;
999
1000   if (xalign)
1001     *xalign = priv->xalign;
1002   if (yalign)
1003     *yalign = priv->yalign;
1004 }
1005
1006 /**
1007  * gtk_cell_renderer_set_padding:
1008  * @cell: A #GtkCellRenderer
1009  * @xpad: the x padding of the cell renderer
1010  * @ypad: the y padding of the cell renderer
1011  *
1012  * Sets the renderer's padding.
1013  *
1014  * Since: 2.18
1015  **/
1016 void
1017 gtk_cell_renderer_set_padding (GtkCellRenderer *cell,
1018                                gint             xpad,
1019                                gint             ypad)
1020 {
1021   GtkCellRendererPrivate *priv;
1022
1023   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1024   g_return_if_fail (xpad >= 0 && xpad >= 0);
1025
1026   priv = cell->priv;
1027
1028   if ((xpad != priv->xpad) || (ypad != priv->ypad))
1029     {
1030       g_object_freeze_notify (G_OBJECT (cell));
1031
1032       if (xpad != priv->xpad)
1033         {
1034           priv->xpad = xpad;
1035           g_object_notify (G_OBJECT (cell), "xpad");
1036         }
1037
1038       if (ypad != priv->ypad)
1039         {
1040           priv->ypad = ypad;
1041           g_object_notify (G_OBJECT (cell), "ypad");
1042         }
1043
1044       g_object_thaw_notify (G_OBJECT (cell));
1045     }
1046 }
1047
1048 /**
1049  * gtk_cell_renderer_get_padding:
1050  * @cell: A #GtkCellRenderer
1051  * @xpad: (out) (allow-none): location to fill in with the x padding of the cell, or %NULL
1052  * @ypad: (out) (allow-none): location to fill in with the y padding of the cell, or %NULL
1053  *
1054  * Fills in @xpad and @ypad with the appropriate values of @cell.
1055  *
1056  * Since: 2.18
1057  **/
1058 void
1059 gtk_cell_renderer_get_padding (GtkCellRenderer *cell,
1060                                gint            *xpad,
1061                                gint            *ypad)
1062 {
1063   GtkCellRendererPrivate *priv;
1064
1065   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1066
1067   priv = cell->priv;
1068
1069   if (xpad)
1070     *xpad = priv->xpad;
1071   if (ypad)
1072     *ypad = priv->ypad;
1073 }
1074
1075 /**
1076  * gtk_cell_renderer_set_visible:
1077  * @cell: A #GtkCellRenderer
1078  * @visible: the visibility of the cell
1079  *
1080  * Sets the cell renderer's visibility.
1081  *
1082  * Since: 2.18
1083  **/
1084 void
1085 gtk_cell_renderer_set_visible (GtkCellRenderer *cell,
1086                                gboolean         visible)
1087 {
1088   GtkCellRendererPrivate *priv;
1089
1090   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1091
1092   priv = cell->priv;
1093
1094   if (priv->visible != visible)
1095     {
1096       priv->visible = visible ? TRUE : FALSE;
1097       g_object_notify (G_OBJECT (cell), "visible");
1098     }
1099 }
1100
1101 /**
1102  * gtk_cell_renderer_get_visible:
1103  * @cell: A #GtkCellRenderer
1104  *
1105  * Returns the cell renderer's visibility.
1106  *
1107  * Returns: %TRUE if the cell renderer is visible
1108  *
1109  * Since: 2.18
1110  */
1111 gboolean
1112 gtk_cell_renderer_get_visible (GtkCellRenderer *cell)
1113 {
1114   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1115
1116   return cell->priv->visible;
1117 }
1118
1119 /**
1120  * gtk_cell_renderer_set_sensitive:
1121  * @cell: A #GtkCellRenderer
1122  * @sensitive: the sensitivity of the cell
1123  *
1124  * Sets the cell renderer's sensitivity.
1125  *
1126  * Since: 2.18
1127  **/
1128 void
1129 gtk_cell_renderer_set_sensitive (GtkCellRenderer *cell,
1130                                  gboolean         sensitive)
1131 {
1132   GtkCellRendererPrivate *priv;
1133
1134   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1135
1136   priv = cell->priv;
1137
1138   if (priv->sensitive != sensitive)
1139     {
1140       priv->sensitive = sensitive ? TRUE : FALSE;
1141       g_object_notify (G_OBJECT (cell), "sensitive");
1142     }
1143 }
1144
1145 /**
1146  * gtk_cell_renderer_get_sensitive:
1147  * @cell: A #GtkCellRenderer
1148  *
1149  * Returns the cell renderer's sensitivity.
1150  *
1151  * Returns: %TRUE if the cell renderer is sensitive
1152  *
1153  * Since: 2.18
1154  */
1155 gboolean
1156 gtk_cell_renderer_get_sensitive (GtkCellRenderer *cell)
1157 {
1158   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1159
1160   return cell->priv->sensitive;
1161 }
1162
1163
1164 /**
1165  * gtk_cell_renderer_is_activatable:
1166  * @cell: A #GtkCellRenderer
1167  *
1168  * Checks whether the cell renderer can do something when activated.
1169  *
1170  * Returns: %TRUE if the cell renderer can do anything when activated
1171  *
1172  * Since: 3.0
1173  */
1174 gboolean
1175 gtk_cell_renderer_is_activatable (GtkCellRenderer *cell)
1176 {
1177   GtkCellRendererPrivate *priv;
1178
1179   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1180
1181   priv = cell->priv;
1182
1183   return (priv->visible &&
1184           (priv->mode == GTK_CELL_RENDERER_MODE_EDITABLE ||
1185            priv->mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE));
1186 }
1187
1188
1189 /**
1190  * gtk_cell_renderer_stop_editing:
1191  * @cell: A #GtkCellRenderer
1192  * @canceled: %TRUE if the editing has been canceled
1193  * 
1194  * Informs the cell renderer that the editing is stopped.
1195  * If @canceled is %TRUE, the cell renderer will emit the 
1196  * #GtkCellRenderer::editing-canceled signal. 
1197  *
1198  * This function should be called by cell renderer implementations 
1199  * in response to the #GtkCellEditable::editing-done signal of 
1200  * #GtkCellEditable.
1201  *
1202  * Since: 2.6
1203  **/
1204 void
1205 gtk_cell_renderer_stop_editing (GtkCellRenderer *cell,
1206                                 gboolean         canceled)
1207 {
1208   GtkCellRendererPrivate *priv;
1209
1210   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1211
1212   priv = cell->priv;
1213
1214   if (priv->editing)
1215     {
1216       priv->editing = FALSE;
1217       if (canceled)
1218         g_signal_emit (cell, cell_renderer_signals[EDITING_CANCELED], 0);
1219     }
1220 }
1221
1222 static void
1223 gtk_cell_renderer_real_get_preferred_size (GtkCellRenderer   *cell,
1224                                            GtkWidget         *widget,
1225                                            GtkOrientation     orientation,
1226                                            gint              *minimum_size,
1227                                            gint              *natural_size)
1228 {
1229   GtkRequisition min_req;
1230
1231   /* Fallback on the old API to get the size. */
1232   if (GTK_CELL_RENDERER_GET_CLASS (cell)->get_size)
1233     GTK_CELL_RENDERER_GET_CLASS (cell)->get_size (GTK_CELL_RENDERER (cell), widget, NULL, NULL, NULL,
1234                                                   &min_req.width, &min_req.height);
1235   else
1236     {
1237       min_req.width = 0;
1238       min_req.height = 0;
1239     }
1240
1241   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1242     {
1243       if (minimum_size)
1244         *minimum_size = min_req.width;
1245
1246       if (natural_size)
1247         *natural_size = min_req.width;
1248     }
1249   else
1250     {
1251       if (minimum_size)
1252         *minimum_size = min_req.height;
1253
1254       if (natural_size)
1255         *natural_size = min_req.height;
1256     }
1257 }
1258
1259 static GtkSizeRequestMode 
1260 gtk_cell_renderer_real_get_request_mode (GtkCellRenderer *cell)
1261 {
1262   /* By default cell renderers are height-for-width. */
1263   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1264 }
1265
1266 static void
1267 gtk_cell_renderer_real_get_preferred_width (GtkCellRenderer *cell,
1268                                             GtkWidget       *widget,
1269                                             gint            *minimum_size,
1270                                             gint            *natural_size)
1271 {
1272   gtk_cell_renderer_real_get_preferred_size (cell, widget, GTK_ORIENTATION_HORIZONTAL, 
1273                                              minimum_size, natural_size);
1274 }
1275
1276 static void
1277 gtk_cell_renderer_real_get_preferred_height (GtkCellRenderer *cell,
1278                                              GtkWidget       *widget,
1279                                              gint            *minimum_size,
1280                                              gint            *natural_size)
1281 {
1282   gtk_cell_renderer_real_get_preferred_size (cell, widget, GTK_ORIENTATION_VERTICAL, 
1283                                              minimum_size, natural_size);
1284 }
1285
1286
1287 static void
1288 gtk_cell_renderer_real_get_preferred_height_for_width (GtkCellRenderer *cell,
1289                                                        GtkWidget       *widget,
1290                                                        gint             width,
1291                                                        gint            *minimum_height,
1292                                                        gint            *natural_height)
1293 {
1294   /* Fall back on the height reported from ->get_size() */
1295   gtk_cell_renderer_get_preferred_height (cell, widget, minimum_height, natural_height);
1296 }
1297
1298 static void
1299 gtk_cell_renderer_real_get_preferred_width_for_height (GtkCellRenderer *cell,
1300                                                        GtkWidget       *widget,
1301                                                        gint             height,
1302                                                        gint            *minimum_width,
1303                                                        gint            *natural_width)
1304 {
1305   /* Fall back on the width reported from ->get_size() */
1306   gtk_cell_renderer_get_preferred_width (cell, widget, minimum_width, natural_width);
1307 }
1308
1309
1310 /* Default implementation assumes that a cell renderer will never use more
1311  * space than its natural size (this is fine for toggles and pixbufs etc
1312  * but needs to be overridden from wrapping/ellipsizing text renderers) */
1313 static void
1314 gtk_cell_renderer_real_get_aligned_area (GtkCellRenderer         *cell,
1315                                          GtkWidget               *widget,
1316                                          GtkCellRendererState     flags,
1317                                          const GdkRectangle      *cell_area,
1318                                          GdkRectangle            *aligned_area)
1319 {
1320   gint opposite_size, x_offset, y_offset;
1321   gint natural_size;
1322
1323   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1324   g_return_if_fail (GTK_IS_WIDGET (widget));
1325   g_return_if_fail (cell_area != NULL);
1326   g_return_if_fail (aligned_area != NULL);
1327
1328   *aligned_area = *cell_area;
1329
1330   /* Trim up the aligned size */
1331   if (gtk_cell_renderer_get_request_mode (cell) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1332     {
1333       gtk_cell_renderer_get_preferred_width (cell, widget, 
1334                                              NULL, &natural_size);
1335
1336       aligned_area->width = MIN (aligned_area->width, natural_size);
1337
1338       gtk_cell_renderer_get_preferred_height_for_width (cell, widget, 
1339                                                         aligned_area->width, 
1340                                                         NULL, &opposite_size);
1341
1342       aligned_area->height = MIN (opposite_size, aligned_area->height);
1343     }
1344   else
1345     {
1346       gtk_cell_renderer_get_preferred_height (cell, widget, 
1347                                               NULL, &natural_size);
1348
1349       aligned_area->height = MIN (aligned_area->width, natural_size);
1350
1351       gtk_cell_renderer_get_preferred_width_for_height (cell, widget, 
1352                                                         aligned_area->height, 
1353                                                         NULL, &opposite_size);
1354
1355       aligned_area->width = MIN (opposite_size, aligned_area->width);
1356     }
1357
1358   /* offset the cell position */
1359   _gtk_cell_renderer_calc_offset (cell, cell_area, 
1360                                   gtk_widget_get_direction (widget),
1361                                   aligned_area->width, 
1362                                   aligned_area->height,
1363                                   &x_offset, &y_offset);
1364
1365   aligned_area->x += x_offset;
1366   aligned_area->y += y_offset;
1367 }
1368
1369
1370 /* An internal convenience function for some containers to peek at the
1371  * cell alignment in a target allocation (used to draw focus and align
1372  * cells in the icon view).
1373  *
1374  * Note this is only a trivial 'align * (allocation - request)' operation.
1375  */
1376 void
1377 _gtk_cell_renderer_calc_offset    (GtkCellRenderer      *cell,
1378                                    const GdkRectangle   *cell_area,
1379                                    GtkTextDirection      direction,
1380                                    gint                  width,
1381                                    gint                  height,
1382                                    gint                 *x_offset,
1383                                    gint                 *y_offset)
1384
1385   GtkCellRendererPrivate *priv;
1386
1387   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1388   g_return_if_fail (cell_area != NULL);
1389   g_return_if_fail (x_offset || y_offset);
1390
1391   priv = cell->priv;
1392
1393   if (x_offset)
1394     {
1395       *x_offset = (((direction == GTK_TEXT_DIR_RTL) ?
1396                     (1.0 - priv->xalign) : priv->xalign) * 
1397                    (cell_area->width - width));
1398       *x_offset = MAX (*x_offset, 0);
1399     }
1400   if (y_offset)
1401     {
1402       *y_offset = (priv->yalign *
1403                    (cell_area->height - height));
1404       *y_offset = MAX (*y_offset, 0);
1405     }
1406 }
1407
1408 /**
1409  * gtk_cell_renderer_get_request_mode:
1410  * @cell: a #GtkCellRenderer    instance
1411  *
1412  * Gets whether the cell renderer prefers a height-for-width layout
1413  * or a width-for-height layout.
1414  *
1415  * Returns: The #GtkSizeRequestMode preferred by this renderer.
1416  *
1417  * Since: 3.0
1418  */
1419 GtkSizeRequestMode
1420 gtk_cell_renderer_get_request_mode (GtkCellRenderer *cell)
1421 {
1422   g_return_val_if_fail (GTK_IS_CELL_RENDERER (cell), FALSE);
1423
1424   return GTK_CELL_RENDERER_GET_CLASS (cell)->get_request_mode (cell);
1425 }
1426
1427 /**
1428  * gtk_cell_renderer_get_preferred_width:
1429  * @cell: a #GtkCellRenderer instance
1430  * @widget: the #GtkWidget this cell will be rendering to
1431  * @minimum_size: (out) (allow-none): location to store the minimum size, or %NULL
1432  * @natural_size: (out) (allow-none): location to store the natural size, or %NULL
1433  *
1434  * Retreives a renderer's natural size when rendered to @widget.
1435  *
1436  * Since: 3.0
1437  */
1438 void
1439 gtk_cell_renderer_get_preferred_width (GtkCellRenderer *cell,
1440                                        GtkWidget       *widget,
1441                                        gint            *minimum_size,
1442                                        gint            *natural_size)
1443 {
1444   GtkCellRendererClass *klass;
1445   gint width;
1446
1447   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1448   g_return_if_fail (GTK_IS_WIDGET (widget));
1449   g_return_if_fail (NULL != minimum_size || NULL != natural_size);
1450
1451   gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), &width, NULL);
1452
1453   if (width < 0)
1454     {
1455       klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1456       klass->get_preferred_width (cell, widget, minimum_size, natural_size);
1457     }
1458   else
1459     {
1460       if (minimum_size)
1461         *minimum_size = width;
1462       if (natural_size)
1463         *natural_size = width;
1464     }
1465
1466 #if DEBUG_CELL_SIZE_REQUEST
1467   g_message ("%s returning minimum width: %d and natural width: %d",
1468              G_OBJECT_TYPE_NAME (cell), 
1469              minimum_size ? *minimum_size : 20000, 
1470              natural_size ? *natural_size : 20000);
1471 #endif
1472 }
1473
1474
1475 /**
1476  * gtk_cell_renderer_get_preferred_height:
1477  * @cell: a #GtkCellRenderer instance
1478  * @widget: the #GtkWidget this cell will be rendering to
1479  * @minimum_size: (out) (allow-none): location to store the minimum size, or %NULL
1480  * @natural_size: (out) (allow-none): location to store the natural size, or %NULL
1481  *
1482  * Retreives a renderer's natural size when rendered to @widget.
1483  *
1484  * Since: 3.0
1485  */
1486 void
1487 gtk_cell_renderer_get_preferred_height (GtkCellRenderer *cell,
1488                                         GtkWidget       *widget,
1489                                         gint            *minimum_size,
1490                                         gint            *natural_size)
1491 {
1492   GtkCellRendererClass *klass;
1493   gint height;
1494
1495   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1496   g_return_if_fail (GTK_IS_WIDGET (widget));
1497   g_return_if_fail (NULL != minimum_size || NULL != natural_size);
1498
1499   gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), NULL, &height);
1500
1501   if (height < 0)
1502     {
1503       klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1504       klass->get_preferred_height (cell, widget, minimum_size, natural_size);
1505     }
1506   else
1507     {
1508       if (minimum_size)
1509         *minimum_size = height;
1510       if (natural_size)
1511         *natural_size = height;
1512     }
1513
1514 #if DEBUG_CELL_SIZE_REQUEST
1515   g_message ("%s returning minimum height: %d and natural height: %d",
1516              G_OBJECT_TYPE_NAME (cell), 
1517              minimum_size ? *minimum_size : 20000, 
1518              natural_size ? *natural_size : 20000);
1519 #endif
1520 }
1521
1522
1523 /**
1524  * gtk_cell_renderer_get_preferred_width_for_height:
1525  * @cell: a #GtkCellRenderer instance
1526  * @widget: the #GtkWidget this cell will be rendering to
1527  * @height: the size which is available for allocation
1528  * @minimum_width: (out) (allow-none): location for storing the minimum size, or %NULL
1529  * @natural_width: (out) (allow-none): location for storing the preferred size, or %NULL
1530  *
1531  * Retreives a cell renderers's minimum and natural width if it were rendered to 
1532  * @widget with the specified @height.
1533  *
1534  * Since: 3.0
1535  */
1536 void
1537 gtk_cell_renderer_get_preferred_width_for_height (GtkCellRenderer *cell,
1538                                                   GtkWidget       *widget,
1539                                                   gint             height,
1540                                                   gint            *minimum_width,
1541                                                   gint            *natural_width)
1542 {
1543   GtkCellRendererClass *klass;
1544   gint width;
1545
1546   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1547   g_return_if_fail (GTK_IS_WIDGET (widget));
1548   g_return_if_fail (NULL != minimum_width || NULL != natural_width);
1549
1550   gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), &width, NULL);
1551
1552   if (width < 0)
1553     {
1554       klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1555       klass->get_preferred_width_for_height (cell, widget, height, minimum_width, natural_width);
1556     }
1557   else
1558     {
1559       if (minimum_width)
1560         *minimum_width = width;
1561       if (natural_width)
1562         *natural_width = width;
1563     }
1564
1565 #if DEBUG_CELL_SIZE_REQUEST
1566   g_message ("%s width for height: %d is minimum %d and natural: %d",
1567              G_OBJECT_TYPE_NAME (cell), height,
1568              minimum_width ? *minimum_width : 20000, 
1569              natural_width ? *natural_width : 20000);
1570 #endif
1571 }
1572
1573 /**
1574  * gtk_cell_renderer_get_preferred_height_for_width:
1575  * @cell: a #GtkCellRenderer instance
1576  * @widget: the #GtkWidget this cell will be rendering to
1577  * @width: the size which is available for allocation
1578  * @minimum_height: (out) (allow-none): location for storing the minimum size, or %NULL
1579  * @natural_height: (out) (allow-none): location for storing the preferred size, or %NULL
1580  *
1581  * Retreives a cell renderers's minimum and natural height if it were rendered to 
1582  * @widget with the specified @width.
1583  *
1584  * Since: 3.0
1585  */
1586 void
1587 gtk_cell_renderer_get_preferred_height_for_width (GtkCellRenderer *cell,
1588                                                   GtkWidget       *widget,
1589                                                   gint             width,
1590                                                   gint            *minimum_height,
1591                                                   gint            *natural_height)
1592 {
1593   GtkCellRendererClass *klass;
1594   gint height;
1595
1596   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1597   g_return_if_fail (GTK_IS_WIDGET (widget));
1598   g_return_if_fail (NULL != minimum_height || NULL != natural_height);
1599
1600   gtk_cell_renderer_get_fixed_size (GTK_CELL_RENDERER (cell), NULL, &height);
1601
1602   if (height < 0)
1603     {
1604       klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1605       klass->get_preferred_height_for_width (cell, widget, width, minimum_height, natural_height);
1606     }
1607   else
1608     {
1609       if (minimum_height)
1610         *minimum_height = height;
1611       if (natural_height)
1612         *natural_height = height;
1613     }
1614
1615 #if DEBUG_CELL_SIZE_REQUEST
1616   g_message ("%s height for width: %d is minimum %d and natural: %d",
1617              G_OBJECT_TYPE_NAME (cell), width,
1618              minimum_height ? *minimum_height : 20000, 
1619              natural_height ? *natural_height : 20000);
1620 #endif
1621 }
1622
1623 /**
1624  * gtk_cell_renderer_get_preferred_size:
1625  * @cell: a #GtkCellRenderer instance
1626  * @widget: the #GtkWidget this cell will be rendering to
1627  * @minimum_size: (out) (allow-none): location for storing the minimum size, or %NULL
1628  * @natural_size: (out) (allow-none): location for storing the natural size, or %NULL
1629  *
1630  * Retrieves the minimum and natural size of a cell taking
1631  * into account the widget's preference for height-for-width management.
1632  *
1633  * Since: 3.0
1634  */
1635 void
1636 gtk_cell_renderer_get_preferred_size (GtkCellRenderer *cell,
1637                                       GtkWidget       *widget,
1638                                       GtkRequisition  *minimum_size,
1639                                       GtkRequisition  *natural_size)
1640 {
1641   gint min_width, nat_width;
1642   gint min_height, nat_height;
1643
1644   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1645
1646   if (gtk_cell_renderer_get_request_mode (cell) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
1647     {
1648       gtk_cell_renderer_get_preferred_width (cell, widget, &min_width, &nat_width);
1649
1650       if (minimum_size)
1651         {
1652           minimum_size->width = min_width;
1653           gtk_cell_renderer_get_preferred_height_for_width (cell, widget, min_width,
1654                                                             &minimum_size->height, NULL);
1655         }
1656
1657       if (natural_size)
1658         {
1659           natural_size->width = nat_width;
1660           gtk_cell_renderer_get_preferred_height_for_width (cell, widget, nat_width,
1661                                                             NULL, &natural_size->height);
1662         }
1663     }
1664   else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
1665     {
1666       gtk_cell_renderer_get_preferred_height (cell, widget, &min_height, &nat_height);
1667
1668       if (minimum_size)
1669         {
1670           minimum_size->height = min_height;
1671           gtk_cell_renderer_get_preferred_width_for_height (cell, widget, min_height,
1672                                                             &minimum_size->width, NULL);
1673         }
1674
1675       if (natural_size)
1676         {
1677           natural_size->height = nat_height;
1678           gtk_cell_renderer_get_preferred_width_for_height (cell, widget, nat_height,
1679                                                             NULL, &natural_size->width);
1680         }
1681     }
1682 }
1683
1684 /**
1685  * gtk_cell_renderer_get_aligned_area:
1686  * @cell: a #GtkCellRenderer instance
1687  * @widget: the #GtkWidget this cell will be rendering to
1688  * @flags: render flags
1689  * @cell_area: cell area which would be passed to gtk_cell_renderer_render()
1690  * @aligned_area: (out): the return location for the space inside @cell_area
1691  *                that would acually be used to render.
1692  *
1693  * Gets the aligned area used by @cell inside @cell_area. Used for finding
1694  * the appropriate edit and focus rectangle.
1695  *
1696  * Since: 3.0
1697  */
1698 void
1699 gtk_cell_renderer_get_aligned_area (GtkCellRenderer      *cell,
1700                                     GtkWidget            *widget,
1701                                     GtkCellRendererState  flags,
1702                                     const GdkRectangle   *cell_area,
1703                                     GdkRectangle         *aligned_area)
1704 {
1705   GtkCellRendererClass *klass;
1706
1707   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
1708   g_return_if_fail (GTK_IS_WIDGET (widget));
1709   g_return_if_fail (cell_area != NULL);
1710   g_return_if_fail (aligned_area != NULL);
1711
1712   klass = GTK_CELL_RENDERER_GET_CLASS (cell);
1713   klass->get_aligned_area (cell, widget, flags, cell_area, aligned_area);
1714
1715   g_assert (aligned_area->x >= cell_area->x && aligned_area->x <= cell_area->x + cell_area->width);
1716   g_assert (aligned_area->y >= cell_area->y && aligned_area->y <= cell_area->y + cell_area->height);
1717   g_assert ((aligned_area->x - cell_area->x) + aligned_area->width <= cell_area->width);
1718   g_assert ((aligned_area->y - cell_area->y) + aligned_area->height <= cell_area->height);
1719 }
1720
1721 /**
1722  * gtk_cell_renderer_get_state:
1723  * @cell: a #GtkCellRenderer, or %NULL
1724  * @widget: a #GtkWidget, or %NULL
1725  * @cell_state: cell renderer state
1726  *
1727  * Translates the cell renderer state to #GtkStateFlags,
1728  * based on the cell renderer and widget sensitivity, and
1729  * the given #GtkCellRendererState.
1730  *
1731  * Returns: the widget state flags applying to @cell
1732  *
1733  * Since: 3.0
1734  **/
1735 GtkStateFlags
1736 gtk_cell_renderer_get_state (GtkCellRenderer      *cell,
1737                              GtkWidget            *widget,
1738                              GtkCellRendererState  cell_state)
1739 {
1740   GtkStateFlags state = 0;
1741
1742   g_return_val_if_fail (!cell || GTK_IS_CELL_RENDERER (cell), 0);
1743   g_return_val_if_fail (!widget || GTK_IS_WIDGET (widget), 0);
1744
1745   if (widget)
1746     state |= gtk_widget_get_state_flags (widget);
1747
1748   state &= ~(GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_SELECTED);
1749
1750   if ((state & GTK_STATE_FLAG_INSENSITIVE) != 0 ||
1751       (cell && !gtk_cell_renderer_get_sensitive (cell)) ||
1752       (cell_state & GTK_CELL_RENDERER_INSENSITIVE) != 0)
1753     {
1754       state |= GTK_STATE_FLAG_INSENSITIVE;
1755     }
1756   else
1757     {
1758       if ((widget && gtk_widget_has_focus (widget)) &&
1759           (cell_state & GTK_CELL_RENDERER_FOCUSED) != 0)
1760         state |= GTK_STATE_FLAG_FOCUSED;
1761
1762       if ((cell_state & GTK_CELL_RENDERER_PRELIT) != 0)
1763         state |= GTK_STATE_FLAG_PRELIGHT;
1764     }
1765
1766   if ((cell_state & GTK_CELL_RENDERER_SELECTED) != 0)
1767     state |= GTK_STATE_FLAG_SELECTED;
1768
1769   return state;
1770 }
1771
1772 /*
1773  * _gtk_cell_renderer_class_set_accessible_type:
1774  * @renderer_class: class to set the accessible type for
1775  * @type: The object type that implements the accessible for @widget_class.
1776  *     The type must be a subtype of #GtkRendererCellAccessible
1777  *
1778  * Sets the type to be used for creating accessibles for cells rendered by
1779  * cell renderers of @renderer_class. Note that multiple accessibles will
1780  * be created.
1781  *
1782  * This function should only be called from class init functions of cell
1783  * renderers.
1784  **/
1785 void
1786 _gtk_cell_renderer_class_set_accessible_type (GtkCellRendererClass *renderer_class,
1787                                               GType                 type)
1788 {
1789   GtkCellRendererClassPrivate *priv;
1790
1791   g_return_if_fail (GTK_IS_CELL_RENDERER_CLASS (renderer_class));
1792   g_return_if_fail (g_type_is_a (type, GTK_TYPE_RENDERER_CELL_ACCESSIBLE));
1793
1794   priv = renderer_class->priv;
1795
1796   priv->accessible_type = type;
1797 }
1798
1799 GType
1800 _gtk_cell_renderer_get_accessible_type (GtkCellRenderer *renderer)
1801 {
1802   g_return_val_if_fail (GTK_IS_CELL_RENDERER (renderer), GTK_TYPE_RENDERER_CELL_ACCESSIBLE);
1803
1804   return GTK_CELL_RENDERER_GET_CLASS (renderer)->priv->accessible_type;
1805 }
1806