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_draw (GtkWidget *widget,
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->draw = gtk_cell_view_draw;
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_draw (GtkWidget *widget,
433 GtkCellView *cellview;
435 GtkCellRendererState state;
436 gboolean rtl = (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL);
440 cellview = GTK_CELL_VIEW (widget);
445 area.width = width = gtk_widget_get_allocated_width (widget);
446 area.height = gtk_widget_get_allocated_height (widget);
448 /* "blank" background */
449 if (cellview->priv->background_set)
451 gdk_cairo_rectangle (cr, &area);
452 gdk_cairo_set_source_color (cr, &cellview->priv->background);
456 /* set cell data (if available) */
457 if (cellview->priv->displayed_row)
458 gtk_cell_view_set_cell_data (cellview);
459 else if (cellview->priv->model)
462 if (gtk_widget_get_state (widget) == GTK_STATE_PRELIGHT)
463 state = GTK_CELL_RENDERER_PRELIT;
464 else if (gtk_widget_get_state (widget) == GTK_STATE_INSENSITIVE)
465 state = GTK_CELL_RENDERER_INSENSITIVE;
469 for (packing = GTK_PACK_START; packing <= GTK_PACK_END; ++packing)
471 if (packing == GTK_PACK_START)
472 area.x = rtl ? width : 0;
474 area.x = rtl ? 0 : width;
476 for (list = cellview->priv->cell_list; list; list = list->next)
478 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
480 if (info->pack != packing)
483 if (!gtk_cell_renderer_get_visible (info->cell))
486 area.width = info->real_width;
488 if ((packing == GTK_PACK_START && rtl) ||
489 (packing == GTK_PACK_END && !rtl))
490 area.x -= area.width;
492 gtk_cell_renderer_render_cairo (info->cell,
496 &area, &area, state);
498 if ((packing == GTK_PACK_START && !rtl) ||
499 (packing == GTK_PACK_END && rtl))
501 area.x += area.width;
502 area.x += cellview->priv->spacing;
505 area.x -= cellview->priv->spacing;
512 static GtkCellViewCellInfo *
513 gtk_cell_view_get_cell_info (GtkCellView *cellview,
514 GtkCellRenderer *renderer)
518 for (i = cellview->priv->cell_list; i; i = i->next)
520 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;
522 if (info->cell == renderer)
530 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
536 g_return_if_fail (cell_view->priv->displayed_row != NULL);
538 path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
542 gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
543 gtk_tree_path_free (path);
545 for (i = cell_view->priv->cell_list; i; i = i->next)
548 GtkCellViewCellInfo *info = i->data;
550 g_object_freeze_notify (G_OBJECT (info->cell));
552 for (j = info->attributes; j && j->next; j = j->next->next)
554 gchar *property = j->data;
555 gint column = GPOINTER_TO_INT (j->next->data);
556 GValue value = {0, };
558 gtk_tree_model_get_value (cell_view->priv->model, &iter,
560 g_object_set_property (G_OBJECT (info->cell),
562 g_value_unset (&value);
566 (* info->func) (GTK_CELL_LAYOUT (cell_view),
568 cell_view->priv->model,
572 g_object_thaw_notify (G_OBJECT (info->cell));
576 /* GtkCellLayout implementation */
578 gtk_cell_view_cell_layout_pack_start (GtkCellLayout *layout,
579 GtkCellRenderer *renderer,
582 GtkCellViewCellInfo *info;
583 GtkCellView *cellview = GTK_CELL_VIEW (layout);
585 g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));
587 g_object_ref_sink (renderer);
589 info = g_slice_new0 (GtkCellViewCellInfo);
590 info->cell = renderer;
591 info->expand = expand ? TRUE : FALSE;
592 info->pack = GTK_PACK_START;
594 cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
596 gtk_widget_queue_resize (GTK_WIDGET (cellview));
600 gtk_cell_view_cell_layout_pack_end (GtkCellLayout *layout,
601 GtkCellRenderer *renderer,
604 GtkCellViewCellInfo *info;
605 GtkCellView *cellview = GTK_CELL_VIEW (layout);
607 g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));
609 g_object_ref_sink (renderer);
611 info = g_slice_new0 (GtkCellViewCellInfo);
612 info->cell = renderer;
613 info->expand = expand ? TRUE : FALSE;
614 info->pack = GTK_PACK_END;
616 cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
618 gtk_widget_queue_resize (GTK_WIDGET (cellview));
622 gtk_cell_view_cell_layout_add_attribute (GtkCellLayout *layout,
623 GtkCellRenderer *renderer,
624 const gchar *attribute,
627 GtkCellViewCellInfo *info;
628 GtkCellView *cellview = GTK_CELL_VIEW (layout);
630 info = gtk_cell_view_get_cell_info (cellview, renderer);
631 g_return_if_fail (info != NULL);
633 info->attributes = g_slist_prepend (info->attributes,
634 GINT_TO_POINTER (column));
635 info->attributes = g_slist_prepend (info->attributes,
636 g_strdup (attribute));
640 gtk_cell_view_cell_layout_clear (GtkCellLayout *layout)
642 GtkCellView *cellview = GTK_CELL_VIEW (layout);
644 while (cellview->priv->cell_list)
646 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)cellview->priv->cell_list->data;
648 gtk_cell_view_cell_layout_clear_attributes (layout, info->cell);
649 g_object_unref (info->cell);
650 g_slice_free (GtkCellViewCellInfo, info);
651 cellview->priv->cell_list = g_list_delete_link (cellview->priv->cell_list,
652 cellview->priv->cell_list);
657 gtk_cell_view_cell_layout_set_cell_data_func (GtkCellLayout *layout,
658 GtkCellRenderer *cell,
659 GtkCellLayoutDataFunc func,
661 GDestroyNotify destroy)
663 GtkCellView *cellview = GTK_CELL_VIEW (layout);
664 GtkCellViewCellInfo *info;
666 info = gtk_cell_view_get_cell_info (cellview, cell);
667 g_return_if_fail (info != NULL);
671 GDestroyNotify d = info->destroy;
673 info->destroy = NULL;
678 info->func_data = func_data;
679 info->destroy = destroy;
683 gtk_cell_view_cell_layout_clear_attributes (GtkCellLayout *layout,
684 GtkCellRenderer *renderer)
686 GtkCellView *cellview = GTK_CELL_VIEW (layout);
687 GtkCellViewCellInfo *info;
690 info = gtk_cell_view_get_cell_info (cellview, renderer);
693 list = info->attributes;
694 while (list && list->next)
697 list = list->next->next;
700 g_slist_free (info->attributes);
701 info->attributes = NULL;
706 gtk_cell_view_cell_layout_reorder (GtkCellLayout *layout,
707 GtkCellRenderer *cell,
710 GtkCellView *cellview = GTK_CELL_VIEW (layout);
711 GtkCellViewCellInfo *info;
714 info = gtk_cell_view_get_cell_info (cellview, cell);
716 g_return_if_fail (info != NULL);
717 g_return_if_fail (position >= 0);
719 link = g_list_find (cellview->priv->cell_list, info);
721 g_return_if_fail (link != NULL);
723 cellview->priv->cell_list = g_list_delete_link (cellview->priv->cell_list,
725 cellview->priv->cell_list = g_list_insert (cellview->priv->cell_list,
728 gtk_widget_queue_draw (GTK_WIDGET (cellview));
734 * Creates a new #GtkCellView widget.
736 * Return value: A newly created #GtkCellView widget.
741 gtk_cell_view_new (void)
743 GtkCellView *cellview;
745 cellview = g_object_new (gtk_cell_view_get_type (), NULL);
747 return GTK_WIDGET (cellview);
751 * gtk_cell_view_new_with_text:
752 * @text: the text to display in the cell view
754 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
755 * to it, and makes its show @text.
757 * Return value: A newly created #GtkCellView widget.
762 gtk_cell_view_new_with_text (const gchar *text)
764 GtkCellView *cellview;
765 GtkCellRenderer *renderer;
766 GValue value = {0, };
768 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
770 renderer = gtk_cell_renderer_text_new ();
771 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
774 g_value_init (&value, G_TYPE_STRING);
775 g_value_set_string (&value, text);
776 gtk_cell_view_set_value (cellview, renderer, "text", &value);
777 g_value_unset (&value);
779 return GTK_WIDGET (cellview);
783 * gtk_cell_view_new_with_markup:
784 * @markup: the text to display in the cell view
786 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
787 * to it, and makes it show @markup. The text can be
788 * marked up with the <link linkend="PangoMarkupFormat">Pango text
789 * markup language</link>.
791 * Return value: A newly created #GtkCellView widget.
796 gtk_cell_view_new_with_markup (const gchar *markup)
798 GtkCellView *cellview;
799 GtkCellRenderer *renderer;
800 GValue value = {0, };
802 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
804 renderer = gtk_cell_renderer_text_new ();
805 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
808 g_value_init (&value, G_TYPE_STRING);
809 g_value_set_string (&value, markup);
810 gtk_cell_view_set_value (cellview, renderer, "markup", &value);
811 g_value_unset (&value);
813 return GTK_WIDGET (cellview);
817 * gtk_cell_view_new_with_pixbuf:
818 * @pixbuf: the image to display in the cell view
820 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
821 * to it, and makes its show @pixbuf.
823 * Return value: A newly created #GtkCellView widget.
828 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
830 GtkCellView *cellview;
831 GtkCellRenderer *renderer;
832 GValue value = {0, };
834 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
836 renderer = gtk_cell_renderer_pixbuf_new ();
837 gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
840 g_value_init (&value, GDK_TYPE_PIXBUF);
841 g_value_set_object (&value, pixbuf);
842 gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
843 g_value_unset (&value);
845 return GTK_WIDGET (cellview);
849 * gtk_cell_view_set_value:
850 * @cell_view: a #GtkCellView widget
851 * @renderer: one of the renderers of @cell_view
852 * @property: the name of the property of @renderer to set
853 * @value: the new value to set the property to
855 * Sets a property of a cell renderer of @cell_view, and
856 * makes sure the display of @cell_view is updated.
861 gtk_cell_view_set_value (GtkCellView *cell_view,
862 GtkCellRenderer *renderer,
866 g_object_set_property (G_OBJECT (renderer), property, value);
868 /* force resize and redraw */
869 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
870 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
874 * gtk_cell_view_set_model:
875 * @cell_view: a #GtkCellView
876 * @model: (allow-none): a #GtkTreeModel
878 * Sets the model for @cell_view. If @cell_view already has a model
879 * set, it will remove it before setting the new model. If @model is
880 * %NULL, then it will unset the old model.
885 gtk_cell_view_set_model (GtkCellView *cell_view,
888 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
889 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
891 if (cell_view->priv->model)
893 if (cell_view->priv->displayed_row)
894 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
895 cell_view->priv->displayed_row = NULL;
897 g_object_unref (cell_view->priv->model);
898 cell_view->priv->model = NULL;
901 cell_view->priv->model = model;
903 if (cell_view->priv->model)
904 g_object_ref (cell_view->priv->model);
908 * gtk_cell_view_get_model:
909 * @cell_view: a #GtkCellView
911 * Returns the model for @cell_view. If no model is used %NULL is
914 * Returns: (transfer none): a #GtkTreeModel used or %NULL
919 gtk_cell_view_get_model (GtkCellView *cell_view)
921 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
923 return cell_view->priv->model;
927 * gtk_cell_view_set_displayed_row:
928 * @cell_view: a #GtkCellView
929 * @path: (allow-none): a #GtkTreePath or %NULL to unset.
931 * Sets the row of the model that is currently displayed
932 * by the #GtkCellView. If the path is unset, then the
933 * contents of the cellview "stick" at their last value;
934 * this is not normally a desired result, but may be
935 * a needed intermediate state if say, the model for
936 * the #GtkCellView becomes temporarily empty.
941 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
944 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
945 g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
947 if (cell_view->priv->displayed_row)
948 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
952 cell_view->priv->displayed_row =
953 gtk_tree_row_reference_new (cell_view->priv->model, path);
956 cell_view->priv->displayed_row = NULL;
958 /* force resize and redraw */
959 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
960 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
964 * gtk_cell_view_get_displayed_row:
965 * @cell_view: a #GtkCellView
967 * Returns a #GtkTreePath referring to the currently
968 * displayed row. If no row is currently displayed,
971 * Returns: the currently displayed row or %NULL
976 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
978 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
980 if (!cell_view->priv->displayed_row)
983 return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
987 * gtk_cell_view_get_size_of_row:
988 * @cell_view: a #GtkCellView
989 * @path: a #GtkTreePath
990 * @requisition: return location for the size
992 * Sets @requisition to the size needed by @cell_view to display
993 * the model row pointed to by @path.
995 * Return value: %TRUE
999 * Deprecated: 3.0: Use gtk_cell_view_get_desired_width_of_row() and
1000 * gtk_cell_view_get_desired_height_for_width_of_row() instead.
1003 gtk_cell_view_get_size_of_row (GtkCellView *cell_view,
1005 GtkRequisition *requisition)
1009 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1010 g_return_val_if_fail (path != NULL, FALSE);
1012 /* Return the minimum height for the minimum width */
1013 gtk_cell_view_get_desired_width_of_row (cell_view, path, &req.width, NULL);
1014 gtk_cell_view_get_desired_height_for_width_of_row (cell_view, path, req.width, &req.height, NULL);
1024 * gtk_cell_view_get_desired_width_of_row:
1025 * @cell_view: a #GtkCellView
1026 * @path: a #GtkTreePath
1027 * @minimum_size: location to store the minimum size
1028 * @natural_size: location to store the natural size
1030 * Sets @minimum_size and @natural_size to the width desired by @cell_view
1031 * to display the model row pointed to by @path.
1036 gtk_cell_view_get_desired_width_of_row (GtkCellView *cell_view,
1041 GtkTreeRowReference *tmp;
1043 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1044 g_return_if_fail (path != NULL);
1045 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
1047 tmp = cell_view->priv->displayed_row;
1048 cell_view->priv->displayed_row =
1049 gtk_tree_row_reference_new (cell_view->priv->model, path);
1051 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), minimum_size, natural_size);
1053 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1054 cell_view->priv->displayed_row = tmp;
1056 /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
1057 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), NULL, NULL);
1062 * gtk_cell_view_get_desired_height_for_width_of_row:
1063 * @cell_view: a #GtkCellView
1064 * @path: a #GtkTreePath
1065 * @avail_size: available width
1066 * @minimum_size: location to store the minimum height
1067 * @natural_size: location to store the natural height
1069 * Sets @minimum_size and @natural_size to the height desired by @cell_view
1070 * if it were allocated a width of @avail_size to display the model row
1071 * pointed to by @path.
1076 gtk_cell_view_get_desired_height_for_width_of_row (GtkCellView *cell_view,
1082 GtkTreeRowReference *tmp;
1084 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1085 g_return_if_fail (path != NULL);
1086 g_return_if_fail (minimum_size != NULL || natural_size != NULL);
1088 tmp = cell_view->priv->displayed_row;
1089 cell_view->priv->displayed_row =
1090 gtk_tree_row_reference_new (cell_view->priv->model, path);
1092 /* Then get the collective height_for_width based on the cached values */
1093 gtk_cell_view_get_height_for_width (GTK_SIZE_REQUEST (cell_view), avail_size, minimum_size, natural_size);
1095 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1096 cell_view->priv->displayed_row = tmp;
1098 /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
1099 gtk_cell_view_get_width (GTK_SIZE_REQUEST (cell_view), NULL, NULL);
1103 * gtk_cell_view_set_background_color:
1104 * @cell_view: a #GtkCellView
1105 * @color: the new background color
1107 * Sets the background color of @view.
1112 gtk_cell_view_set_background_color (GtkCellView *cell_view,
1113 const GdkColor *color)
1115 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1119 if (!cell_view->priv->background_set)
1121 cell_view->priv->background_set = TRUE;
1122 g_object_notify (G_OBJECT (cell_view), "background-set");
1125 cell_view->priv->background = *color;
1129 if (cell_view->priv->background_set)
1131 cell_view->priv->background_set = FALSE;
1132 g_object_notify (G_OBJECT (cell_view), "background-set");
1136 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1140 gtk_cell_view_cell_layout_get_cells (GtkCellLayout *layout)
1142 GtkCellView *cell_view = GTK_CELL_VIEW (layout);
1143 GList *retval = NULL, *list;
1145 g_return_val_if_fail (cell_view != NULL, NULL);
1147 gtk_cell_view_set_cell_data (cell_view);
1149 for (list = cell_view->priv->cell_list; list; list = list->next)
1151 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1153 retval = g_list_prepend (retval, info->cell);
1156 return g_list_reverse (retval);
1160 gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
1161 GtkBuilder *builder,
1163 const gchar *tagname,
1164 GMarkupParser *parser,
1167 if (parent_buildable_iface->custom_tag_start &&
1168 parent_buildable_iface->custom_tag_start (buildable, builder, child,
1169 tagname, parser, data))
1172 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
1173 tagname, parser, data);
1177 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
1178 GtkBuilder *builder,
1180 const gchar *tagname,
1183 if (strcmp (tagname, "attributes") == 0)
1184 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
1186 else if (parent_buildable_iface->custom_tag_end)
1187 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
1192 gtk_cell_view_size_request_init (GtkSizeRequestIface *iface)
1194 iface->get_width = gtk_cell_view_get_width;
1195 iface->get_height = gtk_cell_view_get_height;
1196 iface->get_width_for_height = gtk_cell_view_get_width_for_height;
1197 iface->get_height_for_width = gtk_cell_view_get_height_for_width;
1201 gtk_cell_view_get_width (GtkSizeRequest *widget,
1206 gint cell_min, cell_nat;
1207 gboolean first_cell = TRUE;
1208 GtkCellView *cellview = GTK_CELL_VIEW (widget);
1209 gint minimum, natural;
1211 minimum = natural = 0;
1213 if (cellview->priv->displayed_row)
1214 gtk_cell_view_set_cell_data (cellview);
1216 for (list = cellview->priv->cell_list; list; list = list->next)
1218 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1220 if (gtk_cell_renderer_get_visible (info->cell))
1225 minimum += cellview->priv->spacing;
1226 natural += cellview->priv->spacing;
1229 gtk_cell_size_request_get_width (GTK_CELL_SIZE_REQUEST (info->cell),
1230 GTK_WIDGET (cellview), &cell_min, &cell_nat);
1232 info->requested_width = cell_min;
1233 info->natural_width = cell_nat;
1235 minimum += info->requested_width;
1236 natural += info->natural_width;
1243 *minimum_size = minimum;
1246 *natural_size = natural;
1250 gtk_cell_view_get_height (GtkSizeRequest *widget,
1256 /* CellViews only need to respond to height-for-width mode (cellview is pretty much
1257 * an implementation detail of GtkComboBox) */
1258 gtk_cell_view_get_width (widget, &minimum_width, NULL);
1259 gtk_cell_view_get_height_for_width (widget, minimum_width, minimum_size, natural_size);
1263 gtk_cell_view_get_width_for_height (GtkSizeRequest *widget,
1268 /* CellViews only need to respond to height-for-width mode (cellview is pretty much
1269 * an implementation detail of GtkComboBox) */
1270 gtk_cell_view_get_width (widget, minimum_size, natural_size);
1274 gtk_cell_view_get_height_for_width (GtkSizeRequest *widget,
1279 GtkCellView *cellview = GTK_CELL_VIEW (widget);
1281 GtkRequestedSize *sizes;
1283 gint minimum, natural, avail_size;
1284 gboolean first_cell = TRUE;
1285 gint n_expand_cells = 0;
1286 gint extra_per_cell, extra_extra, i;
1288 minimum = natural = 0;
1289 avail_size = for_size;
1291 array = g_array_new (0, TRUE, sizeof (GtkRequestedSize));
1293 if (cellview->priv->displayed_row)
1294 gtk_cell_view_set_cell_data (cellview);
1296 /* First allocate the right width to all cells */
1297 for (list = cellview->priv->cell_list; list; list = list->next)
1299 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1301 if (gtk_cell_renderer_get_visible (info->cell))
1303 GtkRequestedSize requested;
1305 gtk_cell_size_request_get_width (GTK_CELL_SIZE_REQUEST (info->cell),
1306 GTK_WIDGET (cellview),
1307 &requested.minimum_size,
1308 &requested.natural_size);
1310 requested.data = info;
1311 g_array_append_val (array, requested);
1313 avail_size -= requested.minimum_size;
1316 avail_size -= cellview->priv->spacing;
1325 sizes = (GtkRequestedSize *)array->data;
1326 avail_size = gtk_distribute_natural_allocation (MAX (0, avail_size), array->len, sizes);
1328 /* Deal with any expand space... */
1329 if (n_expand_cells > 0)
1331 extra_per_cell = avail_size / n_expand_cells;
1332 extra_extra = avail_size % n_expand_cells;
1335 /* Everything just left-aligned if no cells expand */
1336 extra_per_cell = extra_extra = 0;
1338 /* Now get the height for the real width of each cell */
1339 for (i = 0, list = cellview->priv->cell_list; list; list = list->next)
1341 GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;
1342 gint cell_minimum, cell_natural;
1344 if (gtk_cell_renderer_get_visible (info->cell))
1346 gint cell_width = sizes[i].minimum_size;
1348 g_assert (sizes[i].data == info);
1352 cell_width += extra_per_cell;
1360 /* Get the height for the real width of this cell */
1361 gtk_cell_size_request_get_height_for_width (GTK_CELL_SIZE_REQUEST (info->cell),
1362 GTK_WIDGET (widget),
1363 cell_width, &cell_minimum, &cell_natural);
1365 minimum = MAX (minimum, cell_minimum);
1366 natural = MAX (natural, cell_natural);
1368 /* increment sizes[] index for visible cells */
1373 g_array_free (array, TRUE);
1376 *minimum_size = minimum;
1378 *natural_size = natural;