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