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