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