]> Pileus Git - ~andy/gtk/blob - gtk/gtkcellview.c
cc8bdbfc5b09f805def7e8ab685eb869405a032c
[~andy/gtk] / gtk / gtkcellview.c
1 /* gtkellview.c
2  * Copyright (C) 2002, 2003  Kristian Rietveld <kris@gtk.org>
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 <string.h>
22 #include "gtkcellview.h"
23 #include "gtkcelllayout.h"
24 #include "gtkcellareabox.h"
25 #include "gtkintl.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkprivate.h"
29 #include "gtksizerequest.h"
30 #include <gobject/gmarshal.h>
31 #include "gtkbuildable.h"
32
33
34 /**
35  * SECTION:gtkcellview
36  * @Short_description: A widget displaying a single row of a GtkTreeModel
37  * @Title: GtkCellView
38  *
39  * A #GtkCellView displays a single row of a #GtkTreeModel, using
40  * cell renderers just like #GtkTreeView. #GtkCellView doesn't support
41  * some of the more complex features of #GtkTreeView, like cell editing
42  * and drag and drop.
43  */
44
45 static GObject    *gtk_cell_view_constructor              (GType                  type,
46                                                            guint                  n_construct_properties,
47                                                            GObjectConstructParam *construct_properties);
48 static void        gtk_cell_view_get_property             (GObject           *object,
49                                                            guint             param_id,
50                                                            GValue           *value,
51                                                            GParamSpec       *pspec);
52 static void        gtk_cell_view_set_property             (GObject          *object,
53                                                            guint             param_id,
54                                                            const GValue     *value,
55                                                            GParamSpec       *pspec);
56 static void        gtk_cell_view_finalize                 (GObject          *object);
57 static void        gtk_cell_view_dispose                  (GObject          *object);
58 static void        gtk_cell_view_size_allocate            (GtkWidget        *widget,
59                                                            GtkAllocation    *allocation);
60 static gboolean    gtk_cell_view_draw                     (GtkWidget        *widget,
61                                                            cairo_t          *cr);
62 static void        gtk_cell_view_set_value                (GtkCellView     *cell_view,
63                                                            GtkCellRenderer *renderer,
64                                                            gchar           *property,
65                                                            GValue          *value);
66 static void        gtk_cell_view_set_cell_data            (GtkCellView      *cell_view);
67
68 /* celllayout */
69 static void        gtk_cell_view_cell_layout_init         (GtkCellLayoutIface *iface);
70 static GtkCellArea *gtk_cell_view_cell_layout_get_area         (GtkCellLayout         *layout);
71
72
73 /* buildable */
74 static void       gtk_cell_view_buildable_init                 (GtkBuildableIface     *iface);
75 static gboolean   gtk_cell_view_buildable_custom_tag_start     (GtkBuildable          *buildable,
76                                                                 GtkBuilder            *builder,
77                                                                 GObject               *child,
78                                                                 const gchar           *tagname,
79                                                                 GMarkupParser         *parser,
80                                                                 gpointer              *data);
81 static void       gtk_cell_view_buildable_custom_tag_end       (GtkBuildable          *buildable,
82                                                                 GtkBuilder            *builder,
83                                                                 GObject               *child,
84                                                                 const gchar           *tagname,
85                                                                 gpointer              *data);
86
87 static void       gtk_cell_view_get_preferred_width            (GtkWidget             *widget,
88                                                                 gint                  *minimum_size,
89                                                                 gint                  *natural_size);
90 static void       gtk_cell_view_get_preferred_height           (GtkWidget             *widget,
91                                                                 gint                  *minimum_size,
92                                                                 gint                  *natural_size);
93 static void       gtk_cell_view_get_preferred_width_for_height (GtkWidget             *widget,
94                                                                 gint                   avail_size,
95                                                                 gint                  *minimum_size,
96                                                                 gint                  *natural_size);
97 static void       gtk_cell_view_get_preferred_height_for_width (GtkWidget             *widget,
98                                                                 gint                   avail_size,
99                                                                 gint                  *minimum_size,
100                                                                 gint                  *natural_size);
101
102 static void       context_size_changed_cb                      (GtkCellAreaContext   *context,
103                                                                 GParamSpec           *pspec,
104                                                                 GtkWidget            *view);
105 static void       row_changed_cb                               (GtkTreeModel         *model,
106                                                                 GtkTreePath          *path,
107                                                                 GtkTreeIter          *iter,
108                                                                 GtkCellView          *view);
109
110
111 struct _GtkCellViewPrivate
112 {
113   GtkTreeModel        *model;
114   GtkTreeRowReference *displayed_row;
115
116   GtkCellArea         *area;
117   GtkCellAreaContext  *context;
118
119   GdkRGBA              background;
120   gboolean             background_set;
121
122   gulong               size_changed_id;
123   gulong               row_changed_id;
124
125   guint32              draw_sensitive : 1;
126   guint32              fit_model      : 1;
127 };
128
129 static GtkBuildableIface *parent_buildable_iface;
130
131 enum
132 {
133   PROP_0,
134   PROP_BACKGROUND,
135   PROP_BACKGROUND_GDK,
136   PROP_BACKGROUND_RGBA,
137   PROP_BACKGROUND_SET,
138   PROP_MODEL,
139   PROP_CELL_AREA,
140   PROP_CELL_AREA_CONTEXT,
141   PROP_DRAW_SENSITIVE,
142   PROP_FIT_MODEL
143 };
144
145 G_DEFINE_TYPE_WITH_CODE (GtkCellView, gtk_cell_view, GTK_TYPE_WIDGET, 
146                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
147                                                 gtk_cell_view_cell_layout_init)
148                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
149                                                 gtk_cell_view_buildable_init))
150
151 static void
152 gtk_cell_view_class_init (GtkCellViewClass *klass)
153 {
154   GObjectClass   *gobject_class = G_OBJECT_CLASS (klass);
155   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
156
157   gobject_class->constructor = gtk_cell_view_constructor;
158   gobject_class->get_property = gtk_cell_view_get_property;
159   gobject_class->set_property = gtk_cell_view_set_property;
160   gobject_class->finalize = gtk_cell_view_finalize;
161   gobject_class->dispose = gtk_cell_view_dispose;
162
163   widget_class->draw                           = gtk_cell_view_draw;
164   widget_class->size_allocate                  = gtk_cell_view_size_allocate;
165   widget_class->get_preferred_width            = gtk_cell_view_get_preferred_width;
166   widget_class->get_preferred_height           = gtk_cell_view_get_preferred_height;
167   widget_class->get_preferred_width_for_height = gtk_cell_view_get_preferred_width_for_height;
168   widget_class->get_preferred_height_for_width = gtk_cell_view_get_preferred_height_for_width;
169
170   /* properties */
171   g_object_class_install_property (gobject_class,
172                                    PROP_BACKGROUND,
173                                    g_param_spec_string ("background",
174                                                         P_("Background color name"),
175                                                         P_("Background color as a string"),
176                                                         NULL,
177                                                         GTK_PARAM_WRITABLE));
178   g_object_class_install_property (gobject_class,
179                                    PROP_BACKGROUND_GDK,
180                                    g_param_spec_boxed ("background-gdk",
181                                                       P_("Background color"),
182                                                       P_("Background color as a GdkColor"),
183                                                       GDK_TYPE_COLOR,
184                                                       GTK_PARAM_READWRITE));
185   /**
186    * GtkCellView:background-rgba
187    *
188    * The background color as a #GdkRGBA
189    *
190    * Since: 3.0
191    */
192   g_object_class_install_property (gobject_class,
193                                    PROP_BACKGROUND_RGBA,
194                                    g_param_spec_boxed ("background-rgba",
195                                                       P_("Background RGBA color"),
196                                                       P_("Background color as a GdkRGBA"),
197                                                       GDK_TYPE_RGBA,
198                                                       GTK_PARAM_READWRITE));
199
200   /**
201    * GtkCellView:model
202    *
203    * The model for cell view
204    *
205    * since 2.10
206    */
207   g_object_class_install_property (gobject_class,
208                                    PROP_MODEL,
209                                    g_param_spec_object  ("model",
210                                                          P_("CellView model"),
211                                                          P_("The model for cell view"),
212                                                          GTK_TYPE_TREE_MODEL,
213                                                          GTK_PARAM_READWRITE));
214
215
216   /**
217    * GtkCellView:cell-area
218    *
219    * The #GtkCellArea rendering cells
220    *
221    * since 3.0
222    */
223    g_object_class_install_property (gobject_class,
224                                     PROP_CELL_AREA,
225                                     g_param_spec_object ("cell-area",
226                                                          P_("Cell Area"),
227                                                          P_("The GtkCellArea used to layout cells"),
228                                                          GTK_TYPE_CELL_AREA,
229                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
230
231   /**
232    * GtkCellView:cell-area-context
233    *
234    * The #GtkCellAreaContext used to compute the geometry of the cell view.
235    *
236    * since 3.0
237    */
238    g_object_class_install_property (gobject_class,
239                                     PROP_CELL_AREA_CONTEXT,
240                                     g_param_spec_object ("cell-area-context",
241                                                          P_("Cell Area Context"),
242                                                          P_("The GtkCellAreaContext used to "
243                                                             "compute the geometry of the cell view"),
244                                                          GTK_TYPE_CELL_AREA_CONTEXT,
245                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
246
247   /**
248    * GtkCellView:draw-sensitive
249    *
250    * Whether all cells should be draw as sensitive for this view regardless
251    * of the actual cell properties (used to make menus with submenus appear
252    * sensitive when the items in submenus might be insensitive).
253    *
254    * since 3.0
255    */
256    g_object_class_install_property (gobject_class,
257                                     PROP_DRAW_SENSITIVE,
258                                     g_param_spec_boolean ("draw-sensitive",
259                                                           P_("Draw Sensitive"),
260                                                           P_("Whether to force cells to be drawn in a "
261                                                              "sensitive state"),
262                                                           FALSE,
263                                                           GTK_PARAM_READWRITE));
264
265   /**
266    * GtkCellView:fit-model
267    *
268    * Whether the view should request enough space to always fit
269    * the size of every row in the model (used by the combo box to
270    * ensure the combo box size doesnt change when different items
271    * are selected).
272    *
273    * since 3.0
274    */
275    g_object_class_install_property (gobject_class,
276                                     PROP_FIT_MODEL,
277                                     g_param_spec_boolean ("fit-model",
278                                                           P_("Fit Model"),
279                                                           P_("Whether to request enough space for "
280                                                              "every row in the model"),
281                                                           FALSE,
282                                                           GTK_PARAM_READWRITE));
283
284   
285 #define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (gobject_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, GTK_PARAM_READWRITE))
286
287   ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
288                 P_("Background set"),
289                 P_("Whether this tag affects the background color"));
290
291   g_type_class_add_private (gobject_class, sizeof (GtkCellViewPrivate));
292 }
293
294 static void
295 gtk_cell_view_buildable_init (GtkBuildableIface *iface)
296 {
297   parent_buildable_iface = g_type_interface_peek_parent (iface);
298   iface->add_child = _gtk_cell_layout_buildable_add_child;
299   iface->custom_tag_start = gtk_cell_view_buildable_custom_tag_start;
300   iface->custom_tag_end = gtk_cell_view_buildable_custom_tag_end;
301 }
302
303 static void
304 gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface)
305 {
306   iface->get_area = gtk_cell_view_cell_layout_get_area;
307 }
308
309 static GObject *
310 gtk_cell_view_constructor (GType                  type,
311                            guint                  n_construct_properties,
312                            GObjectConstructParam *construct_properties)
313 {
314   GObject            *object;
315   GtkCellView        *view;
316   GtkCellViewPrivate *priv;
317
318   object = G_OBJECT_CLASS (gtk_cell_view_parent_class)->constructor
319     (type, n_construct_properties, construct_properties);
320
321   view = GTK_CELL_VIEW (object);
322   priv = view->priv;
323
324   if (!priv->area)
325     {
326       GtkCellArea *area = gtk_cell_area_box_new ();
327
328       priv->area = g_object_ref_sink (area);
329     }
330
331   if (!priv->context)
332     priv->context = gtk_cell_area_create_context (priv->area);
333
334   priv->size_changed_id = 
335     g_signal_connect (priv->context, "notify",
336                       G_CALLBACK (context_size_changed_cb), view);
337
338   return object;
339 }
340
341 static void
342 gtk_cell_view_get_property (GObject    *object,
343                             guint       param_id,
344                             GValue     *value,
345                             GParamSpec *pspec)
346 {
347   GtkCellView *view = GTK_CELL_VIEW (object);
348
349   switch (param_id)
350     {
351     case PROP_BACKGROUND_GDK:
352       {
353         GdkColor color;
354         
355         color.red = (guint) (view->priv->background.red * 65535);
356         color.green = (guint) (view->priv->background.green * 65535);
357         color.blue = (guint) (view->priv->background.blue * 65535);
358         color.pixel = 0;
359         
360         g_value_set_boxed (value, &color);
361       }
362       break;
363     case PROP_BACKGROUND_RGBA:
364       g_value_set_boxed (value, &view->priv->background);
365       break;
366     case PROP_BACKGROUND_SET:
367       g_value_set_boolean (value, view->priv->background_set);
368       break;
369     case PROP_MODEL:
370       g_value_set_object (value, view->priv->model);
371       break;
372     case PROP_CELL_AREA:
373       g_value_set_object (value, view->priv->area);
374       break;
375     case PROP_CELL_AREA_CONTEXT:
376       g_value_set_object (value, view->priv->context);
377       break;
378     case PROP_DRAW_SENSITIVE:
379       g_value_set_boolean (value, view->priv->draw_sensitive);
380       break;
381     case PROP_FIT_MODEL:
382       g_value_set_boolean (value, view->priv->fit_model);
383       break;
384     default:
385       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
386       break;
387     }
388 }
389
390 static void
391 gtk_cell_view_set_property (GObject      *object,
392                             guint         param_id,
393                             const GValue *value,
394                             GParamSpec   *pspec)
395 {
396   GtkCellView *view = GTK_CELL_VIEW (object);
397   GtkCellArea *area;
398   GtkCellAreaContext *context;
399
400   switch (param_id)
401     {
402     case PROP_BACKGROUND:
403       {
404         GdkColor color;
405         
406         if (!g_value_get_string (value))
407           gtk_cell_view_set_background_color (view, NULL);
408         else if (gdk_color_parse (g_value_get_string (value), &color))
409           gtk_cell_view_set_background_color (view, &color);
410         else
411           g_warning ("Don't know color `%s'", g_value_get_string (value));
412         
413         g_object_notify (object, "background-gdk");
414       }
415       break;
416     case PROP_BACKGROUND_GDK:
417       gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
418       break;
419     case PROP_BACKGROUND_RGBA:
420       gtk_cell_view_set_background_rgba (view, g_value_get_boxed (value));
421       break;
422     case PROP_BACKGROUND_SET:
423       view->priv->background_set = g_value_get_boolean (value);
424       break;
425     case PROP_MODEL:
426       gtk_cell_view_set_model (view, g_value_get_object (value));
427       break;
428     case PROP_CELL_AREA:
429       /* Construct-only, can only be assigned once */
430       area = g_value_get_object (value);
431       
432       if (area)
433         view->priv->area = g_object_ref_sink (area);
434       break;
435     case PROP_CELL_AREA_CONTEXT:
436       /* Construct-only, can only be assigned once */
437       context = g_value_get_object (value);
438       
439       if (context)
440         view->priv->context = g_object_ref (context);
441       break;
442
443     case PROP_DRAW_SENSITIVE:
444       gtk_cell_view_set_draw_sensitive (view, g_value_get_boolean (value));
445       break;
446
447     case PROP_FIT_MODEL:
448       gtk_cell_view_set_fit_model (view, g_value_get_boolean (value));
449       break;
450
451     default:
452         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
453         break;
454     }
455 }
456
457 static void
458 gtk_cell_view_init (GtkCellView *cellview)
459 {
460   GtkCellViewPrivate *priv;
461
462   cellview->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellview,
463                                                 GTK_TYPE_CELL_VIEW,
464                                                 GtkCellViewPrivate);
465   priv = cellview->priv;
466
467   gtk_widget_set_has_window (GTK_WIDGET (cellview), FALSE);
468 }
469
470 static void
471 gtk_cell_view_finalize (GObject *object)
472 {
473   GtkCellView *cellview = GTK_CELL_VIEW (object);
474
475   if (cellview->priv->displayed_row)
476      gtk_tree_row_reference_free (cellview->priv->displayed_row);
477
478   G_OBJECT_CLASS (gtk_cell_view_parent_class)->finalize (object);
479 }
480
481 static void
482 gtk_cell_view_dispose (GObject *object)
483 {
484   GtkCellView *cellview = GTK_CELL_VIEW (object);
485
486   gtk_cell_view_set_model (cellview, NULL);
487
488   if (cellview->priv->area)
489     {
490       g_object_unref (cellview->priv->area);
491       cellview->priv->area = NULL;
492     }
493
494   if (cellview->priv->context)
495     {
496       g_signal_handler_disconnect (cellview->priv->context, cellview->priv->size_changed_id);
497
498       g_object_unref (cellview->priv->context);
499       cellview->priv->context = NULL;
500       cellview->priv->size_changed_id = 0;
501     }
502
503   G_OBJECT_CLASS (gtk_cell_view_parent_class)->dispose (object);
504 }
505
506 static void
507 gtk_cell_view_size_allocate (GtkWidget     *widget,
508                              GtkAllocation *allocation)
509 {
510   GtkCellView        *cellview = GTK_CELL_VIEW (widget);
511   GtkCellViewPrivate *priv = cellview->priv;
512   gint                alloc_width, alloc_height;
513
514   gtk_widget_set_allocation (widget, allocation);
515
516   gtk_cell_area_context_get_allocation (priv->context, &alloc_width, &alloc_height);
517
518   /* The first cell view in context is responsible for allocating the context at allocate time 
519    * (or the cellview has its own context and is not grouped with any other cell views) 
520    *
521    * If the cellview is in "fit model" mode, we assume its not in context and needs to
522    * allocate every time.
523    */
524   if ((alloc_width <= 0 && alloc_height <= 0) || priv->fit_model)
525     {
526       gtk_cell_area_context_allocate_width (priv->context, allocation->width);
527       gtk_cell_area_context_allocate_height (priv->context, allocation->height);
528     }
529 }
530
531 static void
532 gtk_cell_view_request_model (GtkCellView        *cellview,
533                              GtkTreeIter        *parent,
534                              GtkOrientation      orientation,
535                              gint                for_size,
536                              gint               *minimum_size,
537                              gint               *natural_size)
538 {
539   GtkCellViewPrivate *priv = cellview->priv;
540   GtkTreeIter         iter;
541   gboolean            valid;
542
543   valid = gtk_tree_model_iter_children (priv->model, &iter, parent);
544   while (valid)
545     {
546       gint min, nat;
547
548       gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
549
550       if (orientation == GTK_ORIENTATION_HORIZONTAL)
551         {
552           if (for_size < 0)
553             gtk_cell_area_get_preferred_width (priv->area, priv->context, 
554                                                GTK_WIDGET (cellview), &min, &nat);
555           else
556             gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context, 
557                                                           GTK_WIDGET (cellview), for_size, &min, &nat);
558         }
559       else
560         {
561           if (for_size < 0)
562             gtk_cell_area_get_preferred_height (priv->area, priv->context, 
563                                                 GTK_WIDGET (cellview), &min, &nat);
564           else
565             gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context, 
566                                                           GTK_WIDGET (cellview), for_size, &min, &nat);
567         }
568
569       *minimum_size = MAX (min, *minimum_size);
570       *natural_size = MAX (nat, *natural_size);
571
572       /* Recurse into children when they exist */
573       gtk_cell_view_request_model (cellview, &iter, orientation, for_size, minimum_size, natural_size);
574
575       valid = gtk_tree_model_iter_next (priv->model, &iter);
576     }
577 }
578
579 static void
580 gtk_cell_view_get_preferred_width  (GtkWidget *widget,
581                                     gint      *minimum_size,
582                                     gint      *natural_size)
583 {
584   GtkCellView        *cellview = GTK_CELL_VIEW (widget);
585   GtkCellViewPrivate *priv = cellview->priv;
586
587   g_signal_handler_block (priv->context, priv->size_changed_id);
588
589   if (priv->fit_model)
590     {
591       gint min = 0, nat = 0;
592       gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, -1, &min, &nat);
593     }
594   else
595     {
596       if (cellview->priv->displayed_row)
597         gtk_cell_view_set_cell_data (cellview);
598
599       gtk_cell_area_get_preferred_width (priv->area, priv->context, widget, NULL, NULL);
600     }
601
602   gtk_cell_area_context_sum_preferred_width (priv->context);
603   gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
604
605   g_signal_handler_unblock (priv->context, priv->size_changed_id);
606 }
607
608 static void       
609 gtk_cell_view_get_preferred_height (GtkWidget *widget,
610                                     gint      *minimum_size,
611                                     gint      *natural_size)
612 {
613   GtkCellView        *cellview = GTK_CELL_VIEW (widget);
614   GtkCellViewPrivate *priv = cellview->priv;
615
616   g_signal_handler_block (priv->context, priv->size_changed_id);
617
618   if (priv->fit_model)
619     {
620       gint min = 0, nat = 0;
621       gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, -1, &min, &nat);
622     }
623   else
624     {
625       if (cellview->priv->displayed_row)
626         gtk_cell_view_set_cell_data (cellview);
627       
628       gtk_cell_area_get_preferred_height (priv->area, priv->context, widget, NULL, NULL);
629     }
630
631   gtk_cell_area_context_sum_preferred_height (priv->context);
632   gtk_cell_area_context_get_preferred_height (priv->context, minimum_size, natural_size);
633
634   g_signal_handler_unblock (priv->context, priv->size_changed_id);
635 }
636
637 static void       
638 gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
639                                               gint       for_size,
640                                               gint      *minimum_size,
641                                               gint      *natural_size)
642 {
643   GtkCellView        *cellview = GTK_CELL_VIEW (widget);
644   GtkCellViewPrivate *priv = cellview->priv;
645
646   if (priv->fit_model)
647     {
648       gint min = 0, nat = 0;
649       gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, for_size, &min, &nat);
650
651       *minimum_size = min;
652       *natural_size = nat;
653     }
654   else
655     {
656       if (cellview->priv->displayed_row)
657         gtk_cell_view_set_cell_data (cellview);
658
659       gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context, widget, 
660                                                     for_size, minimum_size, natural_size);
661     }
662 }
663
664 static void       
665 gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
666                                               gint       for_size,
667                                               gint      *minimum_size,
668                                               gint      *natural_size)
669 {
670   GtkCellView        *cellview = GTK_CELL_VIEW (widget);
671   GtkCellViewPrivate *priv = cellview->priv;
672
673   if (priv->fit_model)
674     {
675       gint min = 0, nat = 0;
676       gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, for_size, &min, &nat);
677
678       *minimum_size = min;
679       *natural_size = nat;
680     }
681   else
682     {
683       if (cellview->priv->displayed_row)
684         gtk_cell_view_set_cell_data (cellview);
685
686       gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context, widget, 
687                                                     for_size, minimum_size, natural_size);
688     }
689 }
690
691 static gboolean
692 gtk_cell_view_draw (GtkWidget *widget,
693                     cairo_t   *cr)
694 {
695   GtkCellView *cellview;
696   GdkRectangle area;
697   GtkCellRendererState state;
698
699   cellview = GTK_CELL_VIEW (widget);
700
701   /* render cells */
702   area.x = 0;
703   area.y = 0;
704   area.width  = gtk_widget_get_allocated_width (widget);
705   area.height = gtk_widget_get_allocated_height (widget);
706
707   /* "blank" background */
708   if (cellview->priv->background_set)
709     {
710       gdk_cairo_rectangle (cr, &area);
711       gdk_cairo_set_source_rgba (cr, &cellview->priv->background);
712       cairo_fill (cr);
713     }
714
715   /* set cell data (if available) */
716   if (cellview->priv->displayed_row)
717     gtk_cell_view_set_cell_data (cellview);
718   else if (cellview->priv->model)
719     return FALSE;
720
721   if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT)
722     state = GTK_CELL_RENDERER_PRELIT;
723   else if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_INSENSITIVE)
724     state = GTK_CELL_RENDERER_INSENSITIVE;
725   else
726     state = 0;
727       
728   /* Render the cells */
729   gtk_cell_area_render (cellview->priv->area, cellview->priv->context, 
730                         widget, cr, &area, &area, state, FALSE);
731
732   return FALSE;
733 }
734
735 static void
736 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
737 {
738   GtkTreeIter iter;
739   GtkTreePath *path;
740
741   g_return_if_fail (cell_view->priv->displayed_row != NULL);
742
743   path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
744   if (!path)
745     return;
746
747   gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
748   gtk_tree_path_free (path);
749
750   gtk_cell_area_apply_attributes (cell_view->priv->area, 
751                                   cell_view->priv->model, 
752                                   &iter, FALSE, FALSE);
753
754   if (cell_view->priv->draw_sensitive)
755     {
756       GList *l, *cells = 
757         gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view->priv->area));
758
759       for (l = cells; l; l = l->next)
760         {
761           GObject *renderer = l->data;
762
763           g_object_set (renderer, "sensitive", TRUE, NULL);
764         }
765       g_list_free (cells);
766     }
767 }
768
769 /* GtkCellLayout implementation */
770 static GtkCellArea *
771 gtk_cell_view_cell_layout_get_area (GtkCellLayout   *layout)
772 {
773   GtkCellView *cellview = GTK_CELL_VIEW (layout);
774
775   return cellview->priv->area;
776 }
777
778 /* GtkBuildable implementation */
779 static gboolean
780 gtk_cell_view_buildable_custom_tag_start (GtkBuildable  *buildable,
781                                           GtkBuilder    *builder,
782                                           GObject       *child,
783                                           const gchar   *tagname,
784                                           GMarkupParser *parser,
785                                           gpointer      *data)
786 {
787   if (parent_buildable_iface->custom_tag_start &&
788       parent_buildable_iface->custom_tag_start (buildable, builder, child,
789                                                 tagname, parser, data))
790     return TRUE;
791
792   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
793                                                       tagname, parser, data);
794 }
795
796 static void
797 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
798                                         GtkBuilder   *builder,
799                                         GObject      *child,
800                                         const gchar  *tagname,
801                                         gpointer     *data)
802 {
803   if (strcmp (tagname, "attributes") == 0 || strcmp (tagname, "cell-packing") == 0)
804     _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
805                                                data);
806   else if (parent_buildable_iface->custom_tag_end)
807     parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
808                                             data);
809 }
810
811 static void
812 context_size_changed_cb (GtkCellAreaContext  *context,
813                          GParamSpec          *pspec,
814                          GtkWidget           *view)
815 {
816   if (!strcmp (pspec->name, "minimum-width") ||
817       !strcmp (pspec->name, "natural-width") ||
818       !strcmp (pspec->name, "minimum-height") ||
819       !strcmp (pspec->name, "natural-height"))
820     gtk_widget_queue_resize (view);
821 }
822
823 static void
824 row_changed_cb (GtkTreeModel         *model,
825                 GtkTreePath          *path,
826                 GtkTreeIter          *iter,
827                 GtkCellView          *view)
828 {
829   GtkTreePath *row_path;
830
831   if (view->priv->displayed_row)
832     {
833       row_path = 
834         gtk_tree_row_reference_get_path (view->priv->displayed_row);
835
836       if (row_path)
837         {
838           /* Resize everything in our context if our row changed */
839           if (gtk_tree_path_compare (row_path, path) == 0)
840             gtk_cell_area_context_flush (view->priv->context);
841
842           gtk_tree_path_free (row_path);
843         }
844     }
845 }
846
847 /**
848  * gtk_cell_view_new:
849  *
850  * Creates a new #GtkCellView widget.
851  *
852  * Return value: A newly created #GtkCellView widget.
853  *
854  * Since: 2.6
855  */
856 GtkWidget *
857 gtk_cell_view_new (void)
858 {
859   GtkCellView *cellview;
860
861   cellview = g_object_new (gtk_cell_view_get_type (), NULL);
862
863   return GTK_WIDGET (cellview);
864 }
865
866
867 /**
868  * gtk_cell_view_new_with_context:
869  * @area: the #GtkCellArea to layout cells
870  * @context: the #GtkCellAreaContext in which to calculate cell geometry
871  *
872  * Creates a new #GtkCellView widget with a specific #GtkCellArea
873  * to layout cells and a specific #GtkCellAreaContext.
874  *
875  * Specifying the same context for a handfull of cells lets
876  * the underlying area synchronize the geometry for those cells,
877  * in this way alignments with cellviews for other rows are
878  * possible.
879  *
880  * Return value: A newly created #GtkCellView widget.
881  *
882  * Since: 2.6
883  */
884 GtkWidget *
885 gtk_cell_view_new_with_context (GtkCellArea        *area,
886                                 GtkCellAreaContext *context)
887 {
888   g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
889   g_return_val_if_fail (context == NULL || GTK_IS_CELL_AREA_CONTEXT (context), NULL);
890
891   return (GtkWidget *)g_object_new (GTK_TYPE_CELL_VIEW, 
892                                     "cell-area", area,
893                                     "cell-area-context", context,
894                                     NULL);
895 }
896
897 /**
898  * gtk_cell_view_new_with_text:
899  * @text: the text to display in the cell view
900  *
901  * Creates a new #GtkCellView widget, adds a #GtkCellRendererText 
902  * to it, and makes its show @text.
903  *
904  * Return value: A newly created #GtkCellView widget.
905  *
906  * Since: 2.6
907  */
908 GtkWidget *
909 gtk_cell_view_new_with_text (const gchar *text)
910 {
911   GtkCellView *cellview;
912   GtkCellRenderer *renderer;
913   GValue value = {0, };
914
915   cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
916
917   renderer = gtk_cell_renderer_text_new ();
918   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
919                               renderer, TRUE);
920
921   g_value_init (&value, G_TYPE_STRING);
922   g_value_set_string (&value, text);
923   gtk_cell_view_set_value (cellview, renderer, "text", &value);
924   g_value_unset (&value);
925
926   return GTK_WIDGET (cellview);
927 }
928
929 /**
930  * gtk_cell_view_new_with_markup:
931  * @markup: the text to display in the cell view
932  *
933  * Creates a new #GtkCellView widget, adds a #GtkCellRendererText 
934  * to it, and makes it show @markup. The text can be
935  * marked up with the <link linkend="PangoMarkupFormat">Pango text 
936  * markup language</link>.
937  *
938  * Return value: A newly created #GtkCellView widget.
939  *
940  * Since: 2.6
941  */
942 GtkWidget *
943 gtk_cell_view_new_with_markup (const gchar *markup)
944 {
945   GtkCellView *cellview;
946   GtkCellRenderer *renderer;
947   GValue value = {0, };
948
949   cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
950
951   renderer = gtk_cell_renderer_text_new ();
952   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
953                               renderer, TRUE);
954
955   g_value_init (&value, G_TYPE_STRING);
956   g_value_set_string (&value, markup);
957   gtk_cell_view_set_value (cellview, renderer, "markup", &value);
958   g_value_unset (&value);
959
960   return GTK_WIDGET (cellview);
961 }
962
963 /**
964  * gtk_cell_view_new_with_pixbuf:
965  * @pixbuf: the image to display in the cell view
966  *
967  * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf 
968  * to it, and makes its show @pixbuf. 
969  *
970  * Return value: A newly created #GtkCellView widget.
971  *
972  * Since: 2.6
973  */
974 GtkWidget *
975 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
976 {
977   GtkCellView *cellview;
978   GtkCellRenderer *renderer;
979   GValue value = {0, };
980
981   cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
982
983   renderer = gtk_cell_renderer_pixbuf_new ();
984   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
985                               renderer, TRUE);
986
987   g_value_init (&value, GDK_TYPE_PIXBUF);
988   g_value_set_object (&value, pixbuf);
989   gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
990   g_value_unset (&value);
991
992   return GTK_WIDGET (cellview);
993 }
994
995 /**
996  * gtk_cell_view_set_value:
997  * @cell_view: a #GtkCellView widget
998  * @renderer: one of the renderers of @cell_view
999  * @property: the name of the property of @renderer to set
1000  * @value: the new value to set the property to
1001  * 
1002  * Sets a property of a cell renderer of @cell_view, and
1003  * makes sure the display of @cell_view is updated.
1004  *
1005  * Since: 2.6
1006  */
1007 static void
1008 gtk_cell_view_set_value (GtkCellView     *cell_view,
1009                          GtkCellRenderer *renderer,
1010                          gchar           *property,
1011                          GValue          *value)
1012 {
1013   g_object_set_property (G_OBJECT (renderer), property, value);
1014
1015   /* force resize and redraw */
1016   gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1017   gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1018 }
1019
1020 /**
1021  * gtk_cell_view_set_model:
1022  * @cell_view: a #GtkCellView
1023  * @model: (allow-none): a #GtkTreeModel
1024  *
1025  * Sets the model for @cell_view.  If @cell_view already has a model
1026  * set, it will remove it before setting the new model.  If @model is
1027  * %NULL, then it will unset the old model.
1028  *
1029  * Since: 2.6
1030  */
1031 void
1032 gtk_cell_view_set_model (GtkCellView  *cell_view,
1033                          GtkTreeModel *model)
1034 {
1035   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1036   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1037
1038   if (cell_view->priv->model)
1039     {
1040       g_signal_handler_disconnect (cell_view->priv->model, 
1041                                    cell_view->priv->row_changed_id);
1042       cell_view->priv->row_changed_id = 0;
1043
1044       if (cell_view->priv->displayed_row)
1045         gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1046       cell_view->priv->displayed_row = NULL;
1047
1048       g_object_unref (cell_view->priv->model);
1049       cell_view->priv->model = NULL;
1050     }
1051
1052   cell_view->priv->model = model;
1053
1054   if (cell_view->priv->model)
1055     {
1056       g_object_ref (cell_view->priv->model);
1057
1058       cell_view->priv->row_changed_id = 
1059         g_signal_connect (cell_view->priv->model, "row-changed",
1060                           G_CALLBACK (row_changed_cb), cell_view);
1061     }
1062 }
1063
1064 /**
1065  * gtk_cell_view_get_model:
1066  * @cell_view: a #GtkCellView
1067  *
1068  * Returns the model for @cell_view. If no model is used %NULL is
1069  * returned.
1070  *
1071  * Returns: (transfer none): a #GtkTreeModel used or %NULL
1072  *
1073  * Since: 2.16
1074  **/
1075 GtkTreeModel *
1076 gtk_cell_view_get_model (GtkCellView *cell_view)
1077 {
1078   g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1079
1080   return cell_view->priv->model;
1081 }
1082
1083 /**
1084  * gtk_cell_view_set_displayed_row:
1085  * @cell_view: a #GtkCellView
1086  * @path: (allow-none): a #GtkTreePath or %NULL to unset.
1087  *
1088  * Sets the row of the model that is currently displayed
1089  * by the #GtkCellView. If the path is unset, then the
1090  * contents of the cellview "stick" at their last value;
1091  * this is not normally a desired result, but may be
1092  * a needed intermediate state if say, the model for
1093  * the #GtkCellView becomes temporarily empty.
1094  *
1095  * Since: 2.6
1096  **/
1097 void
1098 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
1099                                  GtkTreePath *path)
1100 {
1101   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1102   g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
1103
1104   if (cell_view->priv->displayed_row)
1105     gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1106
1107   if (path)
1108     {
1109       cell_view->priv->displayed_row =
1110         gtk_tree_row_reference_new (cell_view->priv->model, path);
1111     }
1112   else
1113     cell_view->priv->displayed_row = NULL;
1114
1115   /* force resize and redraw */
1116   gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1117   gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1118 }
1119
1120 /**
1121  * gtk_cell_view_get_displayed_row:
1122  * @cell_view: a #GtkCellView
1123  *
1124  * Returns a #GtkTreePath referring to the currently 
1125  * displayed row. If no row is currently displayed, 
1126  * %NULL is returned.
1127  *
1128  * Returns: the currently displayed row or %NULL
1129  *
1130  * Since: 2.6
1131  */
1132 GtkTreePath *
1133 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
1134 {
1135   g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1136
1137   if (!cell_view->priv->displayed_row)
1138     return NULL;
1139
1140   return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
1141 }
1142
1143 /**
1144  * gtk_cell_view_get_size_of_row:
1145  * @cell_view: a #GtkCellView
1146  * @path: a #GtkTreePath 
1147  * @requisition: return location for the size 
1148  *
1149  * Sets @requisition to the size needed by @cell_view to display 
1150  * the model row pointed to by @path.
1151  * 
1152  * Return value: %TRUE
1153  *
1154  * Since: 2.6
1155  * 
1156  * Deprecated: 3.0: Combo box formerly used this to calculate the
1157  * sizes for cellviews, now you can achieve this by either using
1158  * the #GtkCellView:fit-model property or by setting the currently
1159  * displayed row of the #GtkCellView and using gtk_widget_get_preferred_size().
1160  */
1161 gboolean
1162 gtk_cell_view_get_size_of_row (GtkCellView    *cell_view,
1163                                GtkTreePath    *path,
1164                                GtkRequisition *requisition)
1165 {
1166   GtkTreeRowReference *tmp;
1167   GtkRequisition req;
1168
1169   g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1170   g_return_val_if_fail (path != NULL, FALSE);
1171
1172   tmp = cell_view->priv->displayed_row;
1173   cell_view->priv->displayed_row =
1174     gtk_tree_row_reference_new (cell_view->priv->model, path);
1175
1176   gtk_widget_get_preferred_width (GTK_WIDGET (cell_view), &req.width, NULL);
1177   gtk_widget_get_preferred_height_for_width (GTK_WIDGET (cell_view), req.width, &req.height, NULL);
1178
1179   gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1180   cell_view->priv->displayed_row = tmp;
1181
1182   if (requisition)
1183     *requisition = req;
1184
1185   return TRUE;
1186 }
1187
1188 /**
1189  * gtk_cell_view_set_background_color:
1190  * @cell_view: a #GtkCellView
1191  * @color: the new background color
1192  *
1193  * Sets the background color of @view.
1194  *
1195  * Since: 2.6
1196  */
1197 void
1198 gtk_cell_view_set_background_color (GtkCellView    *cell_view,
1199                                     const GdkColor *color)
1200 {
1201   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1202
1203   if (color)
1204     {
1205       if (!cell_view->priv->background_set)
1206         {
1207           cell_view->priv->background_set = TRUE;
1208           g_object_notify (G_OBJECT (cell_view), "background-set");
1209         }
1210
1211       cell_view->priv->background.red = color->red / 65535.;
1212       cell_view->priv->background.green = color->green / 65535.;
1213       cell_view->priv->background.blue = color->blue / 65535.;
1214       cell_view->priv->background.alpha = 1;
1215     }
1216   else
1217     {
1218       if (cell_view->priv->background_set)
1219         {
1220           cell_view->priv->background_set = FALSE;
1221           g_object_notify (G_OBJECT (cell_view), "background-set");
1222         }
1223     }
1224
1225   gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1226 }
1227
1228 /**
1229  * gtk_cell_view_set_background_rgba:
1230  * @cell_view: a #GtkCellView
1231  * @rgba: the new background color
1232  *
1233  * Sets the background color of @cell_view.
1234  *
1235  * Since: 3.0
1236  */
1237 void
1238 gtk_cell_view_set_background_rgba (GtkCellView   *cell_view,
1239                                    const GdkRGBA *rgba)
1240 {
1241   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1242
1243   if (rgba)
1244     {
1245       if (!cell_view->priv->background_set)
1246         {
1247           cell_view->priv->background_set = TRUE;
1248           g_object_notify (G_OBJECT (cell_view), "background-set");
1249         }
1250
1251       cell_view->priv->background = *rgba;
1252     }
1253   else
1254     {
1255       if (cell_view->priv->background_set)
1256         {
1257           cell_view->priv->background_set = FALSE;
1258           g_object_notify (G_OBJECT (cell_view), "background-set");
1259         }
1260     }
1261
1262   gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1263 }
1264
1265 gboolean
1266 gtk_cell_view_get_draw_sensitive (GtkCellView     *cell_view)
1267 {
1268   GtkCellViewPrivate *priv;
1269
1270   g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1271
1272   priv = cell_view->priv;
1273
1274   return priv->draw_sensitive;
1275 }
1276
1277 void
1278 gtk_cell_view_set_draw_sensitive (GtkCellView     *cell_view,
1279                                   gboolean         draw_sensitive)
1280 {
1281   GtkCellViewPrivate *priv;
1282
1283   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1284
1285   priv = cell_view->priv;
1286
1287   if (priv->draw_sensitive != draw_sensitive)
1288     {
1289       priv->draw_sensitive = draw_sensitive;
1290
1291       g_object_notify (G_OBJECT (cell_view), "draw-sensitive");
1292     }
1293 }
1294
1295 gboolean
1296 gtk_cell_view_get_fit_model (GtkCellView     *cell_view)
1297 {
1298   GtkCellViewPrivate *priv;
1299
1300   g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1301
1302   priv = cell_view->priv;
1303
1304   return priv->fit_model;
1305 }
1306
1307 void
1308 gtk_cell_view_set_fit_model (GtkCellView     *cell_view,
1309                              gboolean         fit_model)
1310 {
1311   GtkCellViewPrivate *priv;
1312
1313   g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1314
1315   priv = cell_view->priv;
1316
1317   if (priv->fit_model != fit_model)
1318     {
1319       priv->fit_model = fit_model;
1320
1321       gtk_cell_area_context_flush (cell_view->priv->context);
1322
1323       g_object_notify (G_OBJECT (cell_view), "fit-model");
1324     }
1325 }