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