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