2 * Copyright (C) 2002, 2003 Kristian Rietveld <kris@gtk.org>
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.
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.
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.
22 #include "gtkcellview.h"
23 #include "gtkcelllayout.h"
25 #include "gtkcellrenderertext.h"
26 #include "gtkcellrendererpixbuf.h"
27 #include "gtksizerequest.h"
28 #include "gtkcellsizerequest.h"
29 #include "gtkprivate.h"
30 #include <gobject/gmarshal.h>
31 #include "gtkbuildable.h"
34 typedef struct _GtkCellViewCellInfo GtkCellViewCellInfo;
35 struct _GtkCellViewCellInfo
37 GtkCellRenderer *cell;
47 GtkCellLayoutDataFunc func;
49 GDestroyNotify destroy;
52 struct _GtkCellViewPrivate
55 GtkTreeRowReference *displayed_row;
60 gboolean background_set;
64 static void gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface);
65 static void gtk_cell_view_get_property (GObject *object,
69 static void gtk_cell_view_set_property (GObject *object,
73 static void gtk_cell_view_finalize (GObject *object);
74 static void gtk_cell_view_size_allocate (GtkWidget *widget,
75 GtkAllocation *allocation);
76 static gboolean gtk_cell_view_expose (GtkWidget *widget,
77 GdkEventExpose *event);
78 static void gtk_cell_view_set_value (GtkCellView *cell_view,
79 GtkCellRenderer *renderer,
82 static GtkCellViewCellInfo *gtk_cell_view_get_cell_info (GtkCellView *cellview,
83 GtkCellRenderer *renderer);
84 static void gtk_cell_view_set_cell_data (GtkCellView *cell_view);
87 static void gtk_cell_view_cell_layout_pack_start (GtkCellLayout *layout,
88 GtkCellRenderer *renderer,
90 static void gtk_cell_view_cell_layout_pack_end (GtkCellLayout *layout,
91 GtkCellRenderer *renderer,
93 static void gtk_cell_view_cell_layout_add_attribute (GtkCellLayout *layout,
94 GtkCellRenderer *renderer,
95 const gchar *attribute,
97 static void gtk_cell_view_cell_layout_clear (GtkCellLayout *layout);
98 static void gtk_cell_view_cell_layout_clear_attributes (GtkCellLayout *layout,
99 GtkCellRenderer *renderer);
100 static void gtk_cell_view_cell_layout_set_cell_data_func (GtkCellLayout *layout,
101 GtkCellRenderer *cell,
102 GtkCellLayoutDataFunc func,
104 GDestroyNotify destroy);
105 static void gtk_cell_view_cell_layout_reorder (GtkCellLayout *layout,
106 GtkCellRenderer *cell,
108 static GList * gtk_cell_view_cell_layout_get_cells (GtkCellLayout *layout);
111 static void gtk_cell_view_buildable_init (GtkBuildableIface *iface);
112 static gboolean gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
115 const gchar *tagname,
116 GMarkupParser *parser,
118 static void gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
121 const gchar *tagname,
124 static void gtk_cell_view_size_request_init (GtkSizeRequestIface *iface);
125 static void gtk_cell_view_get_width (GtkSizeRequest *widget,
128 static void gtk_cell_view_get_height (GtkSizeRequest *widget,
131 static void gtk_cell_view_get_width_for_height (GtkSizeRequest *widget,
135 static void gtk_cell_view_get_height_for_width (GtkSizeRequest *widget,
140 static GtkBuildableIface *parent_buildable_iface;
152 G_DEFINE_TYPE_WITH_CODE (GtkCellView, gtk_cell_view, GTK_TYPE_WIDGET,
153 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
154 gtk_cell_view_cell_layout_init)
155 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
156 gtk_cell_view_buildable_init)
157 G_IMPLEMENT_INTERFACE (GTK_TYPE_SIZE_REQUEST,
158 gtk_cell_view_size_request_init))
162 gtk_cell_view_class_init (GtkCellViewClass *klass)
164 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
165 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
167 gobject_class->get_property = gtk_cell_view_get_property;
168 gobject_class->set_property = gtk_cell_view_set_property;
169 gobject_class->finalize = gtk_cell_view_finalize;
171 widget_class->expose_event = gtk_cell_view_expose;
172 widget_class->size_allocate = gtk_cell_view_size_allocate;
175 g_object_class_install_property (gobject_class,
177 g_param_spec_string ("background",
178 P_("Background color name"),
179 P_("Background color as a string"),
181 GTK_PARAM_WRITABLE));
182 g_object_class_install_property (gobject_class,
184 g_param_spec_boxed ("background-gdk",
185 P_("Background color"),
186 P_("Background color as a GdkColor"),
188 GTK_PARAM_READWRITE));
193 * The model for cell view
197 g_object_class_install_property (gobject_class,
199 g_param_spec_object ("model",
200 P_("CellView model"),
201 P_("The model for cell view"),
203 GTK_PARAM_READWRITE));
205 #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))
207 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
208 P_("Background set"),
209 P_("Whether this tag affects the background color"));
211 g_type_class_add_private (gobject_class, sizeof (GtkCellViewPrivate));
215 gtk_cell_view_buildable_init (GtkBuildableIface *iface)
217 parent_buildable_iface = g_type_interface_peek_parent (iface);
218 iface->add_child = _gtk_cell_layout_buildable_add_child;
219 iface->custom_tag_start = gtk_cell_view_buildable_custom_tag_start;
220 iface->custom_tag_end = gtk_cell_view_buildable_custom_tag_end;
224 gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface)
226 iface->pack_start = gtk_cell_view_cell_layout_pack_start;
227 iface->pack_end = gtk_cell_view_cell_layout_pack_end;
228 iface->clear = gtk_cell_view_cell_layout_clear;
229 iface->add_attribute = gtk_cell_view_cell_layout_add_attribute;
230 iface->set_cell_data_func = gtk_cell_view_cell_layout_set_cell_data_func;
231 iface->clear_attributes = gtk_cell_view_cell_layout_clear_attributes;
232 iface->reorder = gtk_cell_view_cell_layout_reorder;
233 iface->get_cells = gtk_cell_view_cell_layout_get_cells;
237 gtk_cell_view_get_property (GObject *object,
242 GtkCellView *view = GTK_CELL_VIEW (object);
246 case PROP_BACKGROUND_GDK:
250 color = view->priv->background;
252 g_value_set_boxed (value, &color);
255 case PROP_BACKGROUND_SET:
256 g_value_set_boolean (value, view->priv->background_set);
259 g_value_set_object (value, view->priv->model);
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
268 gtk_cell_view_set_property (GObject *object,
273 GtkCellView *view = GTK_CELL_VIEW (object);
277 case PROP_BACKGROUND:
281 if (!g_value_get_string (value))
282 gtk_cell_view_set_background_color (view, NULL);
283 else if (gdk_color_parse (g_value_get_string (value), &color))
284 gtk_cell_view_set_background_color (view, &color);
286 g_warning ("Don't know color `%s'", g_value_get_string (value));
288 g_object_notify (object, "background-gdk");
291 case PROP_BACKGROUND_GDK:
292 gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
294 case PROP_BACKGROUND_SET:
295 view->priv->background_set = g_value_get_boolean (value);
298 gtk_cell_view_set_model (view, g_value_get_object (value));
301 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
307 gtk_cell_view_init (GtkCellView *cellview)
309 GtkCellViewPrivate *priv;
311 cellview->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellview,
314 priv = cellview->priv;
316 gtk_widget_set_has_window (GTK_WIDGET (cellview), FALSE);
320 gtk_cell_view_finalize (GObject *object)
322 GtkCellView *cellview = GTK_CELL_VIEW (object);
324 gtk_cell_view_cell_layout_clear (GTK_CELL_LAYOUT (cellview));
326 if (cellview->priv->model)
327 g_object_unref (cellview->priv->model);
329 if (cellview->priv->displayed_row)
330 gtk_tree_row_reference_free (cellview->priv->displayed_row);
332 G_OBJECT_CLASS (gtk_cell_view_parent_class)->finalize (object);
336 gtk_cell_view_size_allocate (GtkWidget *widget,
337 GtkAllocation *allocation)
339 GtkCellView *cellview;
340 GtkRequestedSize *sizes;
342 gint n_visible_cells, n_expand_cells;
343 gint avail_width = 0;
344 gint extra_per_cell, extra_extra, i;
345 gboolean first_cell = TRUE;
347 widget->allocation = *allocation;
349 cellview = GTK_CELL_VIEW (widget);
351 avail_width = allocation->width;
353 /* Count visible/expand children */
354 for (n_visible_cells = 0, n_expand_cells = 0, list = cellview->priv->cell_list;
355 list; list = list->next)
357 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
365 sizes = g_new0 (GtkRequestedSize, n_visible_cells);
367 /* checking how much extra space we have */
368 for (i = 0, list = cellview->priv->cell_list; list; list = list->next)
370 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
372 if (!gtk_cell_renderer_get_visible (info->cell))
375 sizes[i].data = info;
376 sizes[i].minimum_size = info->requested_width;
377 sizes[i].natural_size = info->natural_width;
380 avail_width -= cellview->priv->spacing;
382 avail_width -= sizes[i].minimum_size;
389 avail_width = gtk_distribute_natural_allocation (MAX (0, avail_width), n_visible_cells, sizes);
391 /* Deal with any expand space... */
392 if (n_expand_cells > 0)
394 extra_per_cell = avail_width / n_expand_cells;
395 extra_extra = avail_width % n_expand_cells;
398 /* Everything just left-aligned if no cells expand */
399 extra_per_cell = extra_extra = 0;
401 for (i = 0, list = cellview->priv->cell_list; list; list = list->next)
403 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
405 if (!gtk_cell_renderer_get_visible (info->cell))
408 info->real_width = sizes[i].minimum_size;
412 info->real_width += extra_per_cell;
421 /* increment index into sizes for visible children */
429 gtk_cell_view_expose (GtkWidget *widget,
430 GdkEventExpose *event)
433 GtkCellView *cellview;
435 GtkCellRendererState state;
436 gboolean rtl = (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL);
439 cellview = GTK_CELL_VIEW (widget);
441 if (!gtk_widget_is_drawable (widget))
444 /* "blank" background */
445 if (cellview->priv->background_set)
447 cairo_t *cr = gdk_cairo_create (GTK_WIDGET (cellview)->window);
449 gdk_cairo_rectangle (cr, &widget->allocation);
450 cairo_set_source_rgb (cr,
451 cellview->priv->background.red / 65535.,
452 cellview->priv->background.green / 65535.,
453 cellview->priv->background.blue / 65535.);
459 /* set cell data (if available) */
460 if (cellview->priv->displayed_row)
461 gtk_cell_view_set_cell_data (cellview);
462 else if (cellview->priv->model)
466 area = widget->allocation;
468 /* we draw on our very own window, initialize x and y to zero */
469 area.y = widget->allocation.y;
471 if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
472 state = GTK_CELL_RENDERER_PRELIT;
473 else if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
474 state = GTK_CELL_RENDERER_INSENSITIVE;
478 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
480 if (packing == GTK_PACK_START)
481 area.x = widget->allocation.x + (rtl ? widget->allocation.width : 0);
483 area.x = rtl ? widget->allocation.x : (widget->allocation.x + widget->allocation.width);
485 for (list = cellview->priv->cell_list; list; list = list->next)
487 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
489 if (info->pack != packing)
492 if (!gtk_cell_renderer_get_visible (info->cell))
495 area.width = info->real_width;
497 if ((packing == GTK_PACK_START && rtl) ||
498 (packing == GTK_PACK_END && !rtl))
499 area.x -= area.width;
501 gtk_cell_renderer_render (info->cell,
505 &area, &area, &event->area, state);
507 if ((packing == GTK_PACK_START && !rtl) ||
508 (packing == GTK_PACK_END && rtl))
510 area.x += area.width;
511 area.x += cellview->priv->spacing;
514 area.x -= cellview->priv->spacing;
521 static GtkCellViewCellInfo *
522 gtk_cell_view_get_cell_info (GtkCellView *cellview,
523 GtkCellRenderer *renderer)
527 for (i = cellview->priv->cell_list; i; i = i->next)
529 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;
531 if (info->cell == renderer)
539 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
545 g_return_if_fail (cell_view->priv->displayed_row != NULL);
547 path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
551 gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
552 gtk_tree_path_free (path);
554 for (i = cell_view->priv->cell_list; i; i = i->next)
557 GtkCellViewCellInfo *info = i->data;
559 g_object_freeze_notify (G_OBJECT (info->cell));
561 for (j = info->attributes; j && j->next; j = j->next->next)
563 gchar *property = j->data;
564 gint column = GPOINTER_TO_INT (j->next->data);
565 GValue value = {0, };
567 gtk_tree_model_get_value (cell_view->priv->model, &iter,
569 g_object_set_property (G_OBJECT (info->cell),
571 g_value_unset (&value);
575 (* info->func) (GTK_CELL_LAYOUT (cell_view),
577 cell_view->priv->model,
581 g_object_thaw_notify (G_OBJECT (info->cell));
585 /* GtkCellLayout implementation */
587 gtk_cell_view_cell_layout_pack_start (GtkCellLayout *layout,
588 GtkCellRenderer *renderer,
591 GtkCellViewCellInfo *info;
592 GtkCellView *cellview = GTK_CELL_VIEW (layout);
594 g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));
596 g_object_ref_sink (renderer);
598 info = g_slice_new0 (GtkCellViewCellInfo);
599 info->cell = renderer;
600 info->expand = expand ? TRUE : FALSE;
601 info->pack = GTK_PACK_START;
603 cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
605 gtk_widget_queue_resize (GTK_WIDGET (cellview));
609 gtk_cell_view_cell_layout_pack_end (GtkCellLayout *layout,
610 GtkCellRenderer *renderer,
613 GtkCellViewCellInfo *info;
614 GtkCellView *cellview = GTK_CELL_VIEW (layout);
616 g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));
618 g_object_ref_sink (renderer);
620 info = g_slice_new0 (GtkCellViewCellInfo);
621 info->cell = renderer;
622 info->expand = expand ? TRUE : FALSE;
623 info->pack = GTK_PACK_END;
625 cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
627 gtk_widget_queue_resize (GTK_WIDGET (cellview));
631 gtk_cell_view_cell_layout_add_attribute (GtkCellLayout *layout,
632 GtkCellRenderer *renderer,
633 const gchar *attribute,
636 GtkCellViewCellInfo *info;
637 GtkCellView *cellview = GTK_CELL_VIEW (layout);
639 info = gtk_cell_view_get_cell_info (cellview, renderer);
640 g_return_if_fail (info != NULL);
642 info->attributes = g_slist_prepend (info->attributes,
643 GINT_TO_POINTER (column));
644 info->attributes = g_slist_prepend (info->attributes,
645 g_strdup (attribute));
649 gtk_cell_view_cell_layout_clear (GtkCellLayout *layout)
651 GtkCellView *cellview = GTK_CELL_VIEW (layout);
653 while (cellview->priv->cell_list)
655 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)cellview->priv->cell_list->data;
657 gtk_cell_view_cell_layout_clear_attributes (layout, info->cell);
658 g_object_unref (info->cell);
659 g_slice_free (GtkCellViewCellInfo, info);
660 cellview->priv->cell_list = g_list_delete_link (cellview->priv->cell_list,
661 cellview->priv->cell_list);
666 gtk_cell_view_cell_layout_set_cell_data_func (GtkCellLayout *layout,
667 GtkCellRenderer *cell,
668 GtkCellLayoutDataFunc func,
670 GDestroyNotify destroy)
672 GtkCellView *cellview = GTK_CELL_VIEW (layout);
673 GtkCellViewCellInfo *info;
675 info = gtk_cell_view_get_cell_info (cellview, cell);
676 g_return_if_fail (info != NULL);
680 GDestroyNotify d = info->destroy;
682 info->destroy = NULL;
687 info->func_data = func_data;
688 info->destroy = destroy;
692 gtk_cell_view_cell_layout_clear_attributes (GtkCellLayout *layout,
693 GtkCellRenderer *renderer)
695 GtkCellView *cellview = GTK_CELL_VIEW (layout);
696 GtkCellViewCellInfo *info;
699 info = gtk_cell_view_get_cell_info (cellview, renderer);
702 list = info->attributes;
703 while (list && list->next)
706 list = list->next->next;
709 g_slist_free (info->attributes);
710 info->attributes = NULL;
715 gtk_cell_view_cell_layout_reorder (GtkCellLayout *layout,
716 GtkCellRenderer *cell,
719 GtkCellView *cellview = GTK_CELL_VIEW (layout);
720 GtkCellViewCellInfo *info;
723 info = gtk_cell_view_get_cell_info (cellview, cell);
725 g_return_if_fail (info != NULL);
726 g_return_if_fail (position >= 0);
728 link = g_list_find (cellview->priv->cell_list, info);
730 g_return_if_fail (link != NULL);
732 cellview->priv->cell_list = g_list_delete_link (cellview->priv->cell_list,
734 cellview->priv->cell_list = g_list_insert (cellview->priv->cell_list,
737 gtk_widget_queue_draw (GTK_WIDGET (cellview));
743 * Creates a new #GtkCellView widget.
745 * Return value: A newly created #GtkCellView widget.
750 gtk_cell_view_new (void)
752 GtkCellView *cellview;
754 cellview = g_object_new (gtk_cell_view_get_type (), NULL);
756 return GTK_WIDGET (cellview);
760 * gtk_cell_view_new_with_text:
761 * @text: the text to display in the cell view
763 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
764 * to it, and makes its show @text.
766 * Return value: A newly created #GtkCellView widget.
771 gtk_cell_view_new_with_text (const gchar *text)
773 GtkCellView *cellview;
774 GtkCellRenderer *renderer;
775 GValue value = {0, };
777 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
779 renderer = gtk_cell_renderer_text_new ();
780 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
783 g_value_init (&value, G_TYPE_STRING);
784 g_value_set_string (&value, text);
785 gtk_cell_view_set_value (cellview, renderer, "text", &value);
786 g_value_unset (&value);
788 return GTK_WIDGET (cellview);
792 * gtk_cell_view_new_with_markup:
793 * @markup: the text to display in the cell view
795 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
796 * to it, and makes it show @markup. The text can be
797 * marked up with the <link linkend="PangoMarkupFormat">Pango text
798 * markup language</link>.
800 * Return value: A newly created #GtkCellView widget.
805 gtk_cell_view_new_with_markup (const gchar *markup)
807 GtkCellView *cellview;
808 GtkCellRenderer *renderer;
809 GValue value = {0, };
811 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
813 renderer = gtk_cell_renderer_text_new ();
814 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
817 g_value_init (&value, G_TYPE_STRING);
818 g_value_set_string (&value, markup);
819 gtk_cell_view_set_value (cellview, renderer, "markup", &value);
820 g_value_unset (&value);
822 return GTK_WIDGET (cellview);
826 * gtk_cell_view_new_with_pixbuf:
827 * @pixbuf: the image to display in the cell view
829 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
830 * to it, and makes its show @pixbuf.
832 * Return value: A newly created #GtkCellView widget.
837 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
839 GtkCellView *cellview;
840 GtkCellRenderer *renderer;
841 GValue value = {0, };
843 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
845 renderer = gtk_cell_renderer_pixbuf_new ();
846 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
849 g_value_init (&value, GDK_TYPE_PIXBUF);
850 g_value_set_object (&value, pixbuf);
851 gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
852 g_value_unset (&value);
854 return GTK_WIDGET (cellview);
858 * gtk_cell_view_set_value:
859 * @cell_view: a #GtkCellView widget
860 * @renderer: one of the renderers of @cell_view
861 * @property: the name of the property of @renderer to set
862 * @value: the new value to set the property to
864 * Sets a property of a cell renderer of @cell_view, and
865 * makes sure the display of @cell_view is updated.
870 gtk_cell_view_set_value (GtkCellView *cell_view,
871 GtkCellRenderer *renderer,
875 g_object_set_property (G_OBJECT (renderer), property, value);
877 /* force resize and redraw */
878 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
879 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
883 * gtk_cell_view_set_model:
884 * @cell_view: a #GtkCellView
885 * @model: (allow-none): a #GtkTreeModel
887 * Sets the model for @cell_view. If @cell_view already has a model
888 * set, it will remove it before setting the new model. If @model is
889 * %NULL, then it will unset the old model.
894 gtk_cell_view_set_model (GtkCellView *cell_view,
897 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
898 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
900 if (cell_view->priv->model)
902 if (cell_view->priv->displayed_row)
903 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
904 cell_view->priv->displayed_row = NULL;
906 g_object_unref (cell_view->priv->model);
907 cell_view->priv->model = NULL;
910 cell_view->priv->model = model;
912 if (cell_view->priv->model)
913 g_object_ref (cell_view->priv->model);
917 * gtk_cell_view_get_model:
918 * @cell_view: a #GtkCellView
920 * Returns the model for @cell_view. If no model is used %NULL is
923 * Returns: a #GtkTreeModel used or %NULL
928 gtk_cell_view_get_model (GtkCellView *cell_view)
930 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
932 return cell_view->priv->model;
936 * gtk_cell_view_set_displayed_row:
937 * @cell_view: a #GtkCellView
938 * @path: (allow-none): a #GtkTreePath or %NULL to unset.
940 * Sets the row of the model that is currently displayed
941 * by the #GtkCellView. If the path is unset, then the
942 * contents of the cellview "stick" at their last value;
943 * this is not normally a desired result, but may be
944 * a needed intermediate state if say, the model for
945 * the #GtkCellView becomes temporarily empty.
950 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
953 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
954 g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
956 if (cell_view->priv->displayed_row)
957 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
961 cell_view->priv->displayed_row =
962 gtk_tree_row_reference_new (cell_view->priv->model, path);
965 cell_view->priv->displayed_row = NULL;
967 /* force resize and redraw */
968 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
969 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
973 * gtk_cell_view_get_displayed_row:
974 * @cell_view: a #GtkCellView
976 * Returns a #GtkTreePath referring to the currently
977 * displayed row. If no row is currently displayed,
980 * Returns: the currently displayed row or %NULL
985 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
987 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
989 if (!cell_view->priv->displayed_row)
992 return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
996 * gtk_cell_view_get_size_of_row:
997 * @cell_view: a #GtkCellView
998 * @path: a #GtkTreePath
999 * @requisition: return location for the size
1001 * Sets @requisition to the size needed by @cell_view to display
1002 * the model row pointed to by @path.
1004 * Return value: %TRUE
1008 * Deprecated: 3.0: Use gtk_cell_view_get_desired_width_of_row() and
1009 * gtk_cell_view_get_desired_height_for_width_of_row() instead.
1012 gtk_cell_view_get_size_of_row (GtkCellView *cell_view,
1014 GtkRequisition *requisition)
1018 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1019 g_return_val_if_fail (path != NULL, FALSE);
1021 /* Return the minimum height for the minimum width */
1022 gtk_cell_view_get_desired_width_of_row (cell_view, path, &req.width, NULL);
1023 gtk_cell_view_get_desired_height_for_width_of_row (cell_view, path, req.width, &req.height, NULL);
1033 * gtk_cell_view_get_desired_width_of_row:
1034 * @cell_view: a #GtkCellView
1035 * @path: a #GtkTreePath
1036 * @minimum_size: location to store the minimum size
1037 * @natural_size: location to store the natural size
1039 * Sets @minimum_size and @natural_size to the width desired by @cell_view
1040 * to display the model row pointed to by @path.
1045 gtk_cell_view_get_desired_width_of_row (GtkCellView *cell_view,
1050 GtkTreeRowReference *tmp;
1052 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1053 g_return_if_fail (path != NULL);
1054 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
1056 tmp = cell_view->priv->displayed_row;
1057 cell_view->priv->displayed_row =
1058 gtk_tree_row_reference_new (cell_view->priv->model, path);
1060 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), minimum_size, natural_size);
1062 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1063 cell_view->priv->displayed_row = tmp;
1065 /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
1066 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), NULL, NULL);
1071 * gtk_cell_view_get_desired_height_for_width_of_row:
1072 * @cell_view: a #GtkCellView
1073 * @path: a #GtkTreePath
1074 * @avail_size: available width
1075 * @minimum_size: location to store the minimum height
1076 * @natural_size: location to store the natural height
1078 * Sets @minimum_size and @natural_size to the height desired by @cell_view
1079 * if it were allocated a width of @avail_size to display the model row
1080 * pointed to by @path.
1085 gtk_cell_view_get_desired_height_for_width_of_row (GtkCellView *cell_view,
1091 GtkTreeRowReference *tmp;
1093 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1094 g_return_if_fail (path != NULL);
1095 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
1097 tmp = cell_view->priv->displayed_row;
1098 cell_view->priv->displayed_row =
1099 gtk_tree_row_reference_new (cell_view->priv->model, path);
1101 /* Then get the collective height_for_width based on the cached values */
1102 gtk_cell_view_get_height_for_width (GTK_SIZE_REQUEST (cell_view), avail_size, minimum_size, natural_size);
1104 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1105 cell_view->priv->displayed_row = tmp;
1107 /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
1108 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), NULL, NULL);
1112 * gtk_cell_view_set_background_color:
1113 * @cell_view: a #GtkCellView
1114 * @color: the new background color
1116 * Sets the background color of @view.
1121 gtk_cell_view_set_background_color (GtkCellView *cell_view,
1122 const GdkColor *color)
1124 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1128 if (!cell_view->priv->background_set)
1130 cell_view->priv->background_set = TRUE;
1131 g_object_notify (G_OBJECT (cell_view), "background-set");
1134 cell_view->priv->background = *color;
1138 if (cell_view->priv->background_set)
1140 cell_view->priv->background_set = FALSE;
1141 g_object_notify (G_OBJECT (cell_view), "background-set");
1145 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1149 gtk_cell_view_cell_layout_get_cells (GtkCellLayout *layout)
1151 GtkCellView *cell_view = GTK_CELL_VIEW (layout);
1152 GList *retval = NULL, *list;
1154 g_return_val_if_fail (cell_view != NULL, NULL);
1156 gtk_cell_view_set_cell_data (cell_view);
1158 for (list = cell_view->priv->cell_list; list; list = list->next)
1160 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1162 retval = g_list_prepend (retval, info->cell);
1165 return g_list_reverse (retval);
1169 gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
1170 GtkBuilder *builder,
1172 const gchar *tagname,
1173 GMarkupParser *parser,
1176 if (parent_buildable_iface->custom_tag_start &&
1177 parent_buildable_iface->custom_tag_start (buildable, builder, child,
1178 tagname, parser, data))
1181 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
1182 tagname, parser, data);
1186 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
1187 GtkBuilder *builder,
1189 const gchar *tagname,
1192 if (strcmp (tagname, "attributes") == 0)
1193 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
1195 else if (parent_buildable_iface->custom_tag_end)
1196 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
1201 gtk_cell_view_size_request_init (GtkSizeRequestIface *iface)
1203 iface->get_width = gtk_cell_view_get_width;
1204 iface->get_height = gtk_cell_view_get_height;
1205 iface->get_width_for_height = gtk_cell_view_get_width_for_height;
1206 iface->get_height_for_width = gtk_cell_view_get_height_for_width;
1210 gtk_cell_view_get_width (GtkSizeRequest *widget,
1215 gint cell_min, cell_nat;
1216 gboolean first_cell = TRUE;
1217 GtkCellView *cellview = GTK_CELL_VIEW (widget);
1218 gint minimum, natural;
1220 minimum = natural = 0;
1222 if (cellview->priv->displayed_row)
1223 gtk_cell_view_set_cell_data (cellview);
1225 for (list = cellview->priv->cell_list; list; list = list->next)
1227 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1229 if (gtk_cell_renderer_get_visible (info->cell))
1234 minimum += cellview->priv->spacing;
1235 natural += cellview->priv->spacing;
1238 gtk_cell_size_request_get_width (GTK_CELL_SIZE_REQUEST (info->cell),
1239 GTK_WIDGET (cellview), &cell_min, &cell_nat);
1241 info->requested_width = cell_min;
1242 info->natural_width = cell_nat;
1244 minimum += info->requested_width;
1245 natural += info->natural_width;
1252 *minimum_size = minimum;
1255 *natural_size = natural;
1259 gtk_cell_view_get_height (GtkSizeRequest *widget,
1265 /* CellViews only need to respond to height-for-width mode (cellview is pretty much
1266 * an implementation detail of GtkComboBox) */
1267 gtk_cell_view_get_width (widget, &minimum_width, NULL);
1268 gtk_cell_view_get_height_for_width (widget, minimum_width, minimum_size, natural_size);
1272 gtk_cell_view_get_width_for_height (GtkSizeRequest *widget,
1277 /* CellViews only need to respond to height-for-width mode (cellview is pretty much
1278 * an implementation detail of GtkComboBox) */
1279 gtk_cell_view_get_width (widget, minimum_size, natural_size);
1283 gtk_cell_view_get_height_for_width (GtkSizeRequest *widget,
1288 GtkCellView *cellview = GTK_CELL_VIEW (widget);
1290 GtkRequestedSize *sizes;
1292 gint minimum, natural, avail_size;
1293 gboolean first_cell = TRUE;
1294 gint n_expand_cells = 0;
1295 gint extra_per_cell, extra_extra, i;
1297 minimum = natural = 0;
1298 avail_size = for_size;
1300 array = g_array_new (0, TRUE, sizeof (GtkRequestedSize));
1302 if (cellview->priv->displayed_row)
1303 gtk_cell_view_set_cell_data (cellview);
1305 /* First allocate the right width to all cells */
1306 for (list = cellview->priv->cell_list; list; list = list->next)
1308 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1310 if (gtk_cell_renderer_get_visible (info->cell))
1312 GtkRequestedSize requested;
1314 gtk_cell_size_request_get_width (GTK_CELL_SIZE_REQUEST (info->cell),
1315 GTK_WIDGET (cellview),
1316 &requested.minimum_size,
1317 &requested.natural_size);
1319 requested.data = info;
1320 g_array_append_val (array, requested);
1322 avail_size -= requested.minimum_size;
1325 avail_size -= cellview->priv->spacing;
1334 sizes = (GtkRequestedSize *)array->data;
1335 avail_size = gtk_distribute_natural_allocation (MAX (0, avail_size), array->len, sizes);
1337 /* Deal with any expand space... */
1338 if (n_expand_cells > 0)
1340 extra_per_cell = avail_size / n_expand_cells;
1341 extra_extra = avail_size % n_expand_cells;
1344 /* Everything just left-aligned if no cells expand */
1345 extra_per_cell = extra_extra = 0;
1347 /* Now get the height for the real width of each cell */
1348 for (i = 0, list = cellview->priv->cell_list; list; list = list->next)
1350 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1351 gint cell_minimum, cell_natural;
1353 if (gtk_cell_renderer_get_visible (info->cell))
1355 gint cell_width = sizes[i].minimum_size;
1357 g_assert (sizes[i].data == info);
1361 cell_width += extra_per_cell;
1369 /* Get the height for the real width of this cell */
1370 gtk_cell_size_request_get_height_for_width (GTK_CELL_SIZE_REQUEST (info->cell),
1371 GTK_WIDGET (widget),
1372 cell_width, &cell_minimum, &cell_natural);
1374 minimum = MAX (minimum, cell_minimum);
1375 natural = MAX (natural, cell_natural);
1377 /* increment sizes[] index for visible cells */
1382 g_array_free (array, TRUE);
1385 *minimum_size = minimum;
1387 *natural_size = natural;