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