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 gtk_widget_set_allocation (widget, 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)
432 GtkAllocation allocation;
434 GtkCellView *cellview;
436 GtkCellRendererState state;
437 gboolean rtl = (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL);
441 cellview = GTK_CELL_VIEW (widget);
443 if (!gtk_widget_is_drawable (widget))
446 gtk_widget_get_allocation (widget, &allocation);
447 cr = gdk_cairo_create (event->window);
448 gdk_cairo_region (cr, event->region);
451 /* "blank" background */
452 if (cellview->priv->background_set)
454 gdk_cairo_rectangle (cr, &allocation);
455 cairo_set_source_rgb (cr,
456 cellview->priv->background.red / 65535.,
457 cellview->priv->background.green / 65535.,
458 cellview->priv->background.blue / 65535.);
462 /* set cell data (if available) */
463 if (cellview->priv->displayed_row)
464 gtk_cell_view_set_cell_data (cellview);
465 else if (cellview->priv->model)
474 /* we draw on our very own window, initialize x and y to zero */
475 area.y = allocation.y;
477 if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
478 state = GTK_CELL_RENDERER_PRELIT;
479 else if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
480 state = GTK_CELL_RENDERER_INSENSITIVE;
484 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
486 if (packing == GTK_PACK_START)
487 area.x = allocation.x + (rtl ? allocation.width : 0);
489 area.x = rtl ? allocation.x : (allocation.x + allocation.width);
491 for (list = cellview->priv->cell_list; list; list = list->next)
493 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
495 if (info->pack != packing)
498 if (!gtk_cell_renderer_get_visible (info->cell))
501 area.width = info->real_width;
503 if ((packing == GTK_PACK_START && rtl) ||
504 (packing == GTK_PACK_END && !rtl))
505 area.x -= area.width;
507 gtk_cell_renderer_render_cairo (info->cell,
511 &area, &area, state);
513 if ((packing == GTK_PACK_START && !rtl) ||
514 (packing == GTK_PACK_END && rtl))
516 area.x += area.width;
517 area.x += cellview->priv->spacing;
520 area.x -= cellview->priv->spacing;
529 static GtkCellViewCellInfo *
530 gtk_cell_view_get_cell_info (GtkCellView *cellview,
531 GtkCellRenderer *renderer)
535 for (i = cellview->priv->cell_list; i; i = i->next)
537 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;
539 if (info->cell == renderer)
547 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
553 g_return_if_fail (cell_view->priv->displayed_row != NULL);
555 path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
559 gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
560 gtk_tree_path_free (path);
562 for (i = cell_view->priv->cell_list; i; i = i->next)
565 GtkCellViewCellInfo *info = i->data;
567 g_object_freeze_notify (G_OBJECT (info->cell));
569 for (j = info->attributes; j && j->next; j = j->next->next)
571 gchar *property = j->data;
572 gint column = GPOINTER_TO_INT (j->next->data);
573 GValue value = {0, };
575 gtk_tree_model_get_value (cell_view->priv->model, &iter,
577 g_object_set_property (G_OBJECT (info->cell),
579 g_value_unset (&value);
583 (* info->func) (GTK_CELL_LAYOUT (cell_view),
585 cell_view->priv->model,
589 g_object_thaw_notify (G_OBJECT (info->cell));
593 /* GtkCellLayout implementation */
595 gtk_cell_view_cell_layout_pack_start (GtkCellLayout *layout,
596 GtkCellRenderer *renderer,
599 GtkCellViewCellInfo *info;
600 GtkCellView *cellview = GTK_CELL_VIEW (layout);
602 g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));
604 g_object_ref_sink (renderer);
606 info = g_slice_new0 (GtkCellViewCellInfo);
607 info->cell = renderer;
608 info->expand = expand ? TRUE : FALSE;
609 info->pack = GTK_PACK_START;
611 cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
613 gtk_widget_queue_resize (GTK_WIDGET (cellview));
617 gtk_cell_view_cell_layout_pack_end (GtkCellLayout *layout,
618 GtkCellRenderer *renderer,
621 GtkCellViewCellInfo *info;
622 GtkCellView *cellview = GTK_CELL_VIEW (layout);
624 g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));
626 g_object_ref_sink (renderer);
628 info = g_slice_new0 (GtkCellViewCellInfo);
629 info->cell = renderer;
630 info->expand = expand ? TRUE : FALSE;
631 info->pack = GTK_PACK_END;
633 cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
635 gtk_widget_queue_resize (GTK_WIDGET (cellview));
639 gtk_cell_view_cell_layout_add_attribute (GtkCellLayout *layout,
640 GtkCellRenderer *renderer,
641 const gchar *attribute,
644 GtkCellViewCellInfo *info;
645 GtkCellView *cellview = GTK_CELL_VIEW (layout);
647 info = gtk_cell_view_get_cell_info (cellview, renderer);
648 g_return_if_fail (info != NULL);
650 info->attributes = g_slist_prepend (info->attributes,
651 GINT_TO_POINTER (column));
652 info->attributes = g_slist_prepend (info->attributes,
653 g_strdup (attribute));
657 gtk_cell_view_cell_layout_clear (GtkCellLayout *layout)
659 GtkCellView *cellview = GTK_CELL_VIEW (layout);
661 while (cellview->priv->cell_list)
663 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)cellview->priv->cell_list->data;
665 gtk_cell_view_cell_layout_clear_attributes (layout, info->cell);
666 g_object_unref (info->cell);
667 g_slice_free (GtkCellViewCellInfo, info);
668 cellview->priv->cell_list = g_list_delete_link (cellview->priv->cell_list,
669 cellview->priv->cell_list);
674 gtk_cell_view_cell_layout_set_cell_data_func (GtkCellLayout *layout,
675 GtkCellRenderer *cell,
676 GtkCellLayoutDataFunc func,
678 GDestroyNotify destroy)
680 GtkCellView *cellview = GTK_CELL_VIEW (layout);
681 GtkCellViewCellInfo *info;
683 info = gtk_cell_view_get_cell_info (cellview, cell);
684 g_return_if_fail (info != NULL);
688 GDestroyNotify d = info->destroy;
690 info->destroy = NULL;
695 info->func_data = func_data;
696 info->destroy = destroy;
700 gtk_cell_view_cell_layout_clear_attributes (GtkCellLayout *layout,
701 GtkCellRenderer *renderer)
703 GtkCellView *cellview = GTK_CELL_VIEW (layout);
704 GtkCellViewCellInfo *info;
707 info = gtk_cell_view_get_cell_info (cellview, renderer);
710 list = info->attributes;
711 while (list && list->next)
714 list = list->next->next;
717 g_slist_free (info->attributes);
718 info->attributes = NULL;
723 gtk_cell_view_cell_layout_reorder (GtkCellLayout *layout,
724 GtkCellRenderer *cell,
727 GtkCellView *cellview = GTK_CELL_VIEW (layout);
728 GtkCellViewCellInfo *info;
731 info = gtk_cell_view_get_cell_info (cellview, cell);
733 g_return_if_fail (info != NULL);
734 g_return_if_fail (position >= 0);
736 link = g_list_find (cellview->priv->cell_list, info);
738 g_return_if_fail (link != NULL);
740 cellview->priv->cell_list = g_list_delete_link (cellview->priv->cell_list,
742 cellview->priv->cell_list = g_list_insert (cellview->priv->cell_list,
745 gtk_widget_queue_draw (GTK_WIDGET (cellview));
751 * Creates a new #GtkCellView widget.
753 * Return value: A newly created #GtkCellView widget.
758 gtk_cell_view_new (void)
760 GtkCellView *cellview;
762 cellview = g_object_new (gtk_cell_view_get_type (), NULL);
764 return GTK_WIDGET (cellview);
768 * gtk_cell_view_new_with_text:
769 * @text: the text to display in the cell view
771 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
772 * to it, and makes its show @text.
774 * Return value: A newly created #GtkCellView widget.
779 gtk_cell_view_new_with_text (const gchar *text)
781 GtkCellView *cellview;
782 GtkCellRenderer *renderer;
783 GValue value = {0, };
785 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
787 renderer = gtk_cell_renderer_text_new ();
788 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
791 g_value_init (&value, G_TYPE_STRING);
792 g_value_set_string (&value, text);
793 gtk_cell_view_set_value (cellview, renderer, "text", &value);
794 g_value_unset (&value);
796 return GTK_WIDGET (cellview);
800 * gtk_cell_view_new_with_markup:
801 * @markup: the text to display in the cell view
803 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
804 * to it, and makes it show @markup. The text can be
805 * marked up with the <link linkend="PangoMarkupFormat">Pango text
806 * markup language</link>.
808 * Return value: A newly created #GtkCellView widget.
813 gtk_cell_view_new_with_markup (const gchar *markup)
815 GtkCellView *cellview;
816 GtkCellRenderer *renderer;
817 GValue value = {0, };
819 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
821 renderer = gtk_cell_renderer_text_new ();
822 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
825 g_value_init (&value, G_TYPE_STRING);
826 g_value_set_string (&value, markup);
827 gtk_cell_view_set_value (cellview, renderer, "markup", &value);
828 g_value_unset (&value);
830 return GTK_WIDGET (cellview);
834 * gtk_cell_view_new_with_pixbuf:
835 * @pixbuf: the image to display in the cell view
837 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
838 * to it, and makes its show @pixbuf.
840 * Return value: A newly created #GtkCellView widget.
845 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
847 GtkCellView *cellview;
848 GtkCellRenderer *renderer;
849 GValue value = {0, };
851 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
853 renderer = gtk_cell_renderer_pixbuf_new ();
854 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
857 g_value_init (&value, GDK_TYPE_PIXBUF);
858 g_value_set_object (&value, pixbuf);
859 gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
860 g_value_unset (&value);
862 return GTK_WIDGET (cellview);
866 * gtk_cell_view_set_value:
867 * @cell_view: a #GtkCellView widget
868 * @renderer: one of the renderers of @cell_view
869 * @property: the name of the property of @renderer to set
870 * @value: the new value to set the property to
872 * Sets a property of a cell renderer of @cell_view, and
873 * makes sure the display of @cell_view is updated.
878 gtk_cell_view_set_value (GtkCellView *cell_view,
879 GtkCellRenderer *renderer,
883 g_object_set_property (G_OBJECT (renderer), property, value);
885 /* force resize and redraw */
886 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
887 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
891 * gtk_cell_view_set_model:
892 * @cell_view: a #GtkCellView
893 * @model: (allow-none): a #GtkTreeModel
895 * Sets the model for @cell_view. If @cell_view already has a model
896 * set, it will remove it before setting the new model. If @model is
897 * %NULL, then it will unset the old model.
902 gtk_cell_view_set_model (GtkCellView *cell_view,
905 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
906 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
908 if (cell_view->priv->model)
910 if (cell_view->priv->displayed_row)
911 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
912 cell_view->priv->displayed_row = NULL;
914 g_object_unref (cell_view->priv->model);
915 cell_view->priv->model = NULL;
918 cell_view->priv->model = model;
920 if (cell_view->priv->model)
921 g_object_ref (cell_view->priv->model);
925 * gtk_cell_view_get_model:
926 * @cell_view: a #GtkCellView
928 * Returns the model for @cell_view. If no model is used %NULL is
931 * Returns: (transfer none): a #GtkTreeModel used or %NULL
936 gtk_cell_view_get_model (GtkCellView *cell_view)
938 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
940 return cell_view->priv->model;
944 * gtk_cell_view_set_displayed_row:
945 * @cell_view: a #GtkCellView
946 * @path: (allow-none): a #GtkTreePath or %NULL to unset.
948 * Sets the row of the model that is currently displayed
949 * by the #GtkCellView. If the path is unset, then the
950 * contents of the cellview "stick" at their last value;
951 * this is not normally a desired result, but may be
952 * a needed intermediate state if say, the model for
953 * the #GtkCellView becomes temporarily empty.
958 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
961 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
962 g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
964 if (cell_view->priv->displayed_row)
965 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
969 cell_view->priv->displayed_row =
970 gtk_tree_row_reference_new (cell_view->priv->model, path);
973 cell_view->priv->displayed_row = NULL;
975 /* force resize and redraw */
976 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
977 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
981 * gtk_cell_view_get_displayed_row:
982 * @cell_view: a #GtkCellView
984 * Returns a #GtkTreePath referring to the currently
985 * displayed row. If no row is currently displayed,
988 * Returns: the currently displayed row or %NULL
993 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
995 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
997 if (!cell_view->priv->displayed_row)
1000 return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
1004 * gtk_cell_view_get_size_of_row:
1005 * @cell_view: a #GtkCellView
1006 * @path: a #GtkTreePath
1007 * @requisition: return location for the size
1009 * Sets @requisition to the size needed by @cell_view to display
1010 * the model row pointed to by @path.
1012 * Return value: %TRUE
1016 * Deprecated: 3.0: Use gtk_cell_view_get_desired_width_of_row() and
1017 * gtk_cell_view_get_desired_height_for_width_of_row() instead.
1020 gtk_cell_view_get_size_of_row (GtkCellView *cell_view,
1022 GtkRequisition *requisition)
1026 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1027 g_return_val_if_fail (path != NULL, FALSE);
1029 /* Return the minimum height for the minimum width */
1030 gtk_cell_view_get_desired_width_of_row (cell_view, path, &req.width, NULL);
1031 gtk_cell_view_get_desired_height_for_width_of_row (cell_view, path, req.width, &req.height, NULL);
1041 * gtk_cell_view_get_desired_width_of_row:
1042 * @cell_view: a #GtkCellView
1043 * @path: a #GtkTreePath
1044 * @minimum_size: location to store the minimum size
1045 * @natural_size: location to store the natural size
1047 * Sets @minimum_size and @natural_size to the width desired by @cell_view
1048 * to display the model row pointed to by @path.
1053 gtk_cell_view_get_desired_width_of_row (GtkCellView *cell_view,
1058 GtkTreeRowReference *tmp;
1060 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1061 g_return_if_fail (path != NULL);
1062 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
1064 tmp = cell_view->priv->displayed_row;
1065 cell_view->priv->displayed_row =
1066 gtk_tree_row_reference_new (cell_view->priv->model, path);
1068 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), minimum_size, natural_size);
1070 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1071 cell_view->priv->displayed_row = tmp;
1073 /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
1074 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), NULL, NULL);
1079 * gtk_cell_view_get_desired_height_for_width_of_row:
1080 * @cell_view: a #GtkCellView
1081 * @path: a #GtkTreePath
1082 * @avail_size: available width
1083 * @minimum_size: location to store the minimum height
1084 * @natural_size: location to store the natural height
1086 * Sets @minimum_size and @natural_size to the height desired by @cell_view
1087 * if it were allocated a width of @avail_size to display the model row
1088 * pointed to by @path.
1093 gtk_cell_view_get_desired_height_for_width_of_row (GtkCellView *cell_view,
1099 GtkTreeRowReference *tmp;
1101 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1102 g_return_if_fail (path != NULL);
1103 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
1105 tmp = cell_view->priv->displayed_row;
1106 cell_view->priv->displayed_row =
1107 gtk_tree_row_reference_new (cell_view->priv->model, path);
1109 /* Then get the collective height_for_width based on the cached values */
1110 gtk_cell_view_get_height_for_width (GTK_SIZE_REQUEST (cell_view), avail_size, minimum_size, natural_size);
1112 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1113 cell_view->priv->displayed_row = tmp;
1115 /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
1116 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), NULL, NULL);
1120 * gtk_cell_view_set_background_color:
1121 * @cell_view: a #GtkCellView
1122 * @color: the new background color
1124 * Sets the background color of @view.
1129 gtk_cell_view_set_background_color (GtkCellView *cell_view,
1130 const GdkColor *color)
1132 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1136 if (!cell_view->priv->background_set)
1138 cell_view->priv->background_set = TRUE;
1139 g_object_notify (G_OBJECT (cell_view), "background-set");
1142 cell_view->priv->background = *color;
1146 if (cell_view->priv->background_set)
1148 cell_view->priv->background_set = FALSE;
1149 g_object_notify (G_OBJECT (cell_view), "background-set");
1153 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1157 gtk_cell_view_cell_layout_get_cells (GtkCellLayout *layout)
1159 GtkCellView *cell_view = GTK_CELL_VIEW (layout);
1160 GList *retval = NULL, *list;
1162 g_return_val_if_fail (cell_view != NULL, NULL);
1164 gtk_cell_view_set_cell_data (cell_view);
1166 for (list = cell_view->priv->cell_list; list; list = list->next)
1168 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1170 retval = g_list_prepend (retval, info->cell);
1173 return g_list_reverse (retval);
1177 gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
1178 GtkBuilder *builder,
1180 const gchar *tagname,
1181 GMarkupParser *parser,
1184 if (parent_buildable_iface->custom_tag_start &&
1185 parent_buildable_iface->custom_tag_start (buildable, builder, child,
1186 tagname, parser, data))
1189 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
1190 tagname, parser, data);
1194 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
1195 GtkBuilder *builder,
1197 const gchar *tagname,
1200 if (strcmp (tagname, "attributes") == 0)
1201 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
1203 else if (parent_buildable_iface->custom_tag_end)
1204 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
1209 gtk_cell_view_size_request_init (GtkSizeRequestIface *iface)
1211 iface->get_width = gtk_cell_view_get_width;
1212 iface->get_height = gtk_cell_view_get_height;
1213 iface->get_width_for_height = gtk_cell_view_get_width_for_height;
1214 iface->get_height_for_width = gtk_cell_view_get_height_for_width;
1218 gtk_cell_view_get_width (GtkSizeRequest *widget,
1223 gint cell_min, cell_nat;
1224 gboolean first_cell = TRUE;
1225 GtkCellView *cellview = GTK_CELL_VIEW (widget);
1226 gint minimum, natural;
1228 minimum = natural = 0;
1230 if (cellview->priv->displayed_row)
1231 gtk_cell_view_set_cell_data (cellview);
1233 for (list = cellview->priv->cell_list; list; list = list->next)
1235 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1237 if (gtk_cell_renderer_get_visible (info->cell))
1242 minimum += cellview->priv->spacing;
1243 natural += cellview->priv->spacing;
1246 gtk_cell_size_request_get_width (GTK_CELL_SIZE_REQUEST (info->cell),
1247 GTK_WIDGET (cellview), &cell_min, &cell_nat);
1249 info->requested_width = cell_min;
1250 info->natural_width = cell_nat;
1252 minimum += info->requested_width;
1253 natural += info->natural_width;
1260 *minimum_size = minimum;
1263 *natural_size = natural;
1267 gtk_cell_view_get_height (GtkSizeRequest *widget,
1273 /* CellViews only need to respond to height-for-width mode (cellview is pretty much
1274 * an implementation detail of GtkComboBox) */
1275 gtk_cell_view_get_width (widget, &minimum_width, NULL);
1276 gtk_cell_view_get_height_for_width (widget, minimum_width, minimum_size, natural_size);
1280 gtk_cell_view_get_width_for_height (GtkSizeRequest *widget,
1285 /* CellViews only need to respond to height-for-width mode (cellview is pretty much
1286 * an implementation detail of GtkComboBox) */
1287 gtk_cell_view_get_width (widget, minimum_size, natural_size);
1291 gtk_cell_view_get_height_for_width (GtkSizeRequest *widget,
1296 GtkCellView *cellview = GTK_CELL_VIEW (widget);
1298 GtkRequestedSize *sizes;
1300 gint minimum, natural, avail_size;
1301 gboolean first_cell = TRUE;
1302 gint n_expand_cells = 0;
1303 gint extra_per_cell, extra_extra, i;
1305 minimum = natural = 0;
1306 avail_size = for_size;
1308 array = g_array_new (0, TRUE, sizeof (GtkRequestedSize));
1310 if (cellview->priv->displayed_row)
1311 gtk_cell_view_set_cell_data (cellview);
1313 /* First allocate the right width to all cells */
1314 for (list = cellview->priv->cell_list; list; list = list->next)
1316 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1318 if (gtk_cell_renderer_get_visible (info->cell))
1320 GtkRequestedSize requested;
1322 gtk_cell_size_request_get_width (GTK_CELL_SIZE_REQUEST (info->cell),
1323 GTK_WIDGET (cellview),
1324 &requested.minimum_size,
1325 &requested.natural_size);
1327 requested.data = info;
1328 g_array_append_val (array, requested);
1330 avail_size -= requested.minimum_size;
1333 avail_size -= cellview->priv->spacing;
1342 sizes = (GtkRequestedSize *)array->data;
1343 avail_size = gtk_distribute_natural_allocation (MAX (0, avail_size), array->len, sizes);
1345 /* Deal with any expand space... */
1346 if (n_expand_cells > 0)
1348 extra_per_cell = avail_size / n_expand_cells;
1349 extra_extra = avail_size % n_expand_cells;
1352 /* Everything just left-aligned if no cells expand */
1353 extra_per_cell = extra_extra = 0;
1355 /* Now get the height for the real width of each cell */
1356 for (i = 0, list = cellview->priv->cell_list; list; list = list->next)
1358 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1359 gint cell_minimum, cell_natural;
1361 if (gtk_cell_renderer_get_visible (info->cell))
1363 gint cell_width = sizes[i].minimum_size;
1365 g_assert (sizes[i].data == info);
1369 cell_width += extra_per_cell;
1377 /* Get the height for the real width of this cell */
1378 gtk_cell_size_request_get_height_for_width (GTK_CELL_SIZE_REQUEST (info->cell),
1379 GTK_WIDGET (widget),
1380 cell_width, &cell_minimum, &cell_natural);
1382 minimum = MAX (minimum, cell_minimum);
1383 natural = MAX (natural, cell_natural);
1385 /* increment sizes[] index for visible cells */
1390 g_array_free (array, TRUE);
1393 *minimum_size = minimum;
1395 *natural_size = natural;