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