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"
24 #include "gtkcellareabox.h"
26 #include "gtkcellrenderertext.h"
27 #include "gtkcellrendererpixbuf.h"
28 #include "gtkprivate.h"
29 #include "gtkorientableprivate.h"
30 #include <gobject/gmarshal.h>
31 #include "gtkbuildable.h"
36 * @Short_description: A widget displaying a single row of a GtkTreeModel
39 * A #GtkCellView displays a single row of a #GtkTreeModel using a #GtkCellArea
40 * and #GtkCellAreaContext. A #GtkCellAreaContext can be provided to the
41 * #GtkCellView at construction time in order to keep the cellview in context
42 * of a group of cell views, this ensures that the renderers displayed will
43 * be properly aligned with eachother (like the aligned cells in the menus
46 * #GtkCellView is #GtkOrientable in order to decide in which orientation
47 * the underlying #GtkCellAreaContext should be allocated. Taking the #GtkComboBox
48 * menu as an example, cellviews should be oriented horizontally if the menus are
49 * listed top-to-bottom and thus all share the same width but may have separate
50 * individual heights (left-to-right menus should be allocated vertically since
51 * they all share the same height but may have variable widths).
54 static GObject *gtk_cell_view_constructor (GType type,
55 guint n_construct_properties,
56 GObjectConstructParam *construct_properties);
57 static void gtk_cell_view_get_property (GObject *object,
61 static void gtk_cell_view_set_property (GObject *object,
65 static void gtk_cell_view_finalize (GObject *object);
66 static void gtk_cell_view_dispose (GObject *object);
67 static void gtk_cell_view_size_allocate (GtkWidget *widget,
68 GtkAllocation *allocation);
69 static gboolean gtk_cell_view_draw (GtkWidget *widget,
71 static void gtk_cell_view_set_value (GtkCellView *cell_view,
72 GtkCellRenderer *renderer,
75 static void gtk_cell_view_set_cell_data (GtkCellView *cell_view);
78 static void gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface);
79 static GtkCellArea *gtk_cell_view_cell_layout_get_area (GtkCellLayout *layout);
83 static void gtk_cell_view_buildable_init (GtkBuildableIface *iface);
84 static gboolean gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
88 GMarkupParser *parser,
90 static void gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
96 static GtkSizeRequestMode gtk_cell_view_get_request_mode (GtkWidget *widget);
97 static void gtk_cell_view_get_preferred_width (GtkWidget *widget,
100 static void gtk_cell_view_get_preferred_height (GtkWidget *widget,
103 static void gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
107 static void gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
112 static void context_size_changed_cb (GtkCellAreaContext *context,
115 static void row_changed_cb (GtkTreeModel *model,
121 struct _GtkCellViewPrivate
124 GtkTreeRowReference *displayed_row;
127 GtkCellAreaContext *context;
131 gulong size_changed_id;
132 gulong row_changed_id;
134 GtkOrientation orientation;
136 guint background_set : 1;
137 guint draw_sensitive : 1;
141 static GtkBuildableIface *parent_buildable_iface;
149 PROP_BACKGROUND_RGBA,
153 PROP_CELL_AREA_CONTEXT,
158 G_DEFINE_TYPE_WITH_CODE (GtkCellView, gtk_cell_view, GTK_TYPE_WIDGET,
159 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
160 gtk_cell_view_cell_layout_init)
161 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
162 gtk_cell_view_buildable_init)
163 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
166 gtk_cell_view_class_init (GtkCellViewClass *klass)
168 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
169 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
171 gobject_class->constructor = gtk_cell_view_constructor;
172 gobject_class->get_property = gtk_cell_view_get_property;
173 gobject_class->set_property = gtk_cell_view_set_property;
174 gobject_class->finalize = gtk_cell_view_finalize;
175 gobject_class->dispose = gtk_cell_view_dispose;
177 widget_class->draw = gtk_cell_view_draw;
178 widget_class->size_allocate = gtk_cell_view_size_allocate;
179 widget_class->get_request_mode = gtk_cell_view_get_request_mode;
180 widget_class->get_preferred_width = gtk_cell_view_get_preferred_width;
181 widget_class->get_preferred_height = gtk_cell_view_get_preferred_height;
182 widget_class->get_preferred_width_for_height = gtk_cell_view_get_preferred_width_for_height;
183 widget_class->get_preferred_height_for_width = gtk_cell_view_get_preferred_height_for_width;
186 g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
188 g_object_class_install_property (gobject_class,
190 g_param_spec_string ("background",
191 P_("Background color name"),
192 P_("Background color as a string"),
194 GTK_PARAM_WRITABLE));
197 * GtkCellView:background-gdk
199 * The background color as a #GdkColor
201 * Deprecated: 3.4: Use #GtkCellView:background-rgba instead.
203 g_object_class_install_property (gobject_class,
205 g_param_spec_boxed ("background-gdk",
206 P_("Background color"),
207 P_("Background color as a GdkColor"),
209 GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
211 * GtkCellView:background-rgba
213 * The background color as a #GdkRGBA
217 g_object_class_install_property (gobject_class,
218 PROP_BACKGROUND_RGBA,
219 g_param_spec_boxed ("background-rgba",
220 P_("Background RGBA color"),
221 P_("Background color as a GdkRGBA"),
223 GTK_PARAM_READWRITE));
228 * The model for cell view
232 g_object_class_install_property (gobject_class,
234 g_param_spec_object ("model",
235 P_("CellView model"),
236 P_("The model for cell view"),
238 GTK_PARAM_READWRITE));
242 * GtkCellView:cell-area
244 * The #GtkCellArea rendering cells
246 * If no area is specified when creating the cell view with gtk_cell_view_new_with_context()
247 * a horizontally oriented #GtkCellAreaBox will be used.
251 g_object_class_install_property (gobject_class,
253 g_param_spec_object ("cell-area",
255 P_("The GtkCellArea used to layout cells"),
257 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
260 * GtkCellView:cell-area-context
262 * The #GtkCellAreaContext used to compute the geometry of the cell view.
264 * A group of cell views can be assigned the same context in order to
265 * ensure the sizes and cell alignments match across all the views with
268 * #GtkComboBox menus uses this to assign the same context to all cell views
269 * in the menu items for a single menu (each submenu creates its own
270 * context since the size of each submenu does not depend on parent
275 g_object_class_install_property (gobject_class,
276 PROP_CELL_AREA_CONTEXT,
277 g_param_spec_object ("cell-area-context",
278 P_("Cell Area Context"),
279 P_("The GtkCellAreaContext used to "
280 "compute the geometry of the cell view"),
281 GTK_TYPE_CELL_AREA_CONTEXT,
282 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
285 * GtkCellView:draw-sensitive
287 * Whether all cells should be draw as sensitive for this view regardless
288 * of the actual cell properties (used to make menus with submenus appear
289 * sensitive when the items in submenus might be insensitive).
293 g_object_class_install_property (gobject_class,
295 g_param_spec_boolean ("draw-sensitive",
296 P_("Draw Sensitive"),
297 P_("Whether to force cells to be drawn in a "
300 GTK_PARAM_READWRITE));
303 * GtkCellView:fit-model
305 * Whether the view should request enough space to always fit
306 * the size of every row in the model (used by the combo box to
307 * ensure the combo box size doesnt change when different items
312 g_object_class_install_property (gobject_class,
314 g_param_spec_boolean ("fit-model",
316 P_("Whether to request enough space for "
317 "every row in the model"),
319 GTK_PARAM_READWRITE));
322 #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))
324 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
325 P_("Background set"),
326 P_("Whether this tag affects the background color"));
328 g_type_class_add_private (gobject_class, sizeof (GtkCellViewPrivate));
332 gtk_cell_view_buildable_init (GtkBuildableIface *iface)
334 parent_buildable_iface = g_type_interface_peek_parent (iface);
335 iface->add_child = _gtk_cell_layout_buildable_add_child;
336 iface->custom_tag_start = gtk_cell_view_buildable_custom_tag_start;
337 iface->custom_tag_end = gtk_cell_view_buildable_custom_tag_end;
341 gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface)
343 iface->get_area = gtk_cell_view_cell_layout_get_area;
347 gtk_cell_view_constructor (GType type,
348 guint n_construct_properties,
349 GObjectConstructParam *construct_properties)
353 GtkCellViewPrivate *priv;
355 object = G_OBJECT_CLASS (gtk_cell_view_parent_class)->constructor
356 (type, n_construct_properties, construct_properties);
358 view = GTK_CELL_VIEW (object);
363 priv->area = gtk_cell_area_box_new ();
364 g_object_ref_sink (priv->area);
368 priv->context = gtk_cell_area_create_context (priv->area);
370 priv->size_changed_id =
371 g_signal_connect (priv->context, "notify",
372 G_CALLBACK (context_size_changed_cb), view);
378 gtk_cell_view_get_property (GObject *object,
383 GtkCellView *view = GTK_CELL_VIEW (object);
387 case PROP_ORIENTATION:
388 g_value_set_enum (value, view->priv->orientation);
390 case PROP_BACKGROUND_GDK:
394 color.red = (guint) (view->priv->background.red * 65535);
395 color.green = (guint) (view->priv->background.green * 65535);
396 color.blue = (guint) (view->priv->background.blue * 65535);
399 g_value_set_boxed (value, &color);
402 case PROP_BACKGROUND_RGBA:
403 g_value_set_boxed (value, &view->priv->background);
405 case PROP_BACKGROUND_SET:
406 g_value_set_boolean (value, view->priv->background_set);
409 g_value_set_object (value, view->priv->model);
412 g_value_set_object (value, view->priv->area);
414 case PROP_CELL_AREA_CONTEXT:
415 g_value_set_object (value, view->priv->context);
417 case PROP_DRAW_SENSITIVE:
418 g_value_set_boolean (value, view->priv->draw_sensitive);
421 g_value_set_boolean (value, view->priv->fit_model);
424 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
430 gtk_cell_view_set_property (GObject *object,
435 GtkCellView *view = GTK_CELL_VIEW (object);
436 GtkCellViewPrivate *priv = view->priv;
438 GtkCellAreaContext *context;
442 case PROP_ORIENTATION:
443 priv->orientation = g_value_get_enum (value);
445 gtk_cell_area_context_reset (priv->context);
447 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (object));
449 case PROP_BACKGROUND:
453 if (!g_value_get_string (value))
454 gtk_cell_view_set_background_rgba (view, NULL);
455 else if (gdk_rgba_parse (&color, g_value_get_string (value)))
456 gtk_cell_view_set_background_rgba (view, &color);
458 g_warning ("Don't know color `%s'", g_value_get_string (value));
460 g_object_notify (object, "background-rgba");
461 g_object_notify (object, "background-gdk");
464 case PROP_BACKGROUND_GDK:
465 gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
467 case PROP_BACKGROUND_RGBA:
468 gtk_cell_view_set_background_rgba (view, g_value_get_boxed (value));
470 case PROP_BACKGROUND_SET:
471 view->priv->background_set = g_value_get_boolean (value);
474 gtk_cell_view_set_model (view, g_value_get_object (value));
477 /* Construct-only, can only be assigned once */
478 area = g_value_get_object (value);
482 if (priv->area != NULL)
484 g_warning ("cell-area has already been set, ignoring construct property");
485 g_object_ref_sink (area);
486 g_object_unref (area);
489 priv->area = g_object_ref_sink (area);
492 case PROP_CELL_AREA_CONTEXT:
493 /* Construct-only, can only be assigned once */
494 context = g_value_get_object (value);
498 if (priv->context != NULL)
500 g_warning ("cell-area-context has already been set, ignoring construct property");
501 g_object_ref_sink (context);
502 g_object_unref (context);
505 priv->context = g_object_ref (context);
509 case PROP_DRAW_SENSITIVE:
510 gtk_cell_view_set_draw_sensitive (view, g_value_get_boolean (value));
514 gtk_cell_view_set_fit_model (view, g_value_get_boolean (value));
518 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
524 gtk_cell_view_init (GtkCellView *cellview)
526 GtkCellViewPrivate *priv;
528 cellview->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellview,
531 priv = cellview->priv;
533 gtk_widget_set_has_window (GTK_WIDGET (cellview), FALSE);
535 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
539 gtk_cell_view_finalize (GObject *object)
541 GtkCellView *cellview = GTK_CELL_VIEW (object);
543 if (cellview->priv->displayed_row)
544 gtk_tree_row_reference_free (cellview->priv->displayed_row);
546 G_OBJECT_CLASS (gtk_cell_view_parent_class)->finalize (object);
550 gtk_cell_view_dispose (GObject *object)
552 GtkCellView *cellview = GTK_CELL_VIEW (object);
554 gtk_cell_view_set_model (cellview, NULL);
556 if (cellview->priv->area)
558 g_object_unref (cellview->priv->area);
559 cellview->priv->area = NULL;
562 if (cellview->priv->context)
564 g_signal_handler_disconnect (cellview->priv->context, cellview->priv->size_changed_id);
566 g_object_unref (cellview->priv->context);
567 cellview->priv->context = NULL;
568 cellview->priv->size_changed_id = 0;
571 G_OBJECT_CLASS (gtk_cell_view_parent_class)->dispose (object);
575 gtk_cell_view_size_allocate (GtkWidget *widget,
576 GtkAllocation *allocation)
578 GtkCellView *cellview = GTK_CELL_VIEW (widget);
579 GtkCellViewPrivate *priv = cellview->priv;
580 gint alloc_width, alloc_height;
582 gtk_widget_set_allocation (widget, allocation);
584 gtk_cell_area_context_get_allocation (priv->context, &alloc_width, &alloc_height);
586 /* The first cell view in context is responsible for allocating the context at allocate time
587 * (or the cellview has its own context and is not grouped with any other cell views)
589 * If the cellview is in "fit model" mode, we assume its not in context and needs to
590 * allocate every time.
593 gtk_cell_area_context_allocate (priv->context, allocation->width, allocation->height);
594 else if (alloc_width != allocation->width && priv->orientation == GTK_ORIENTATION_HORIZONTAL)
595 gtk_cell_area_context_allocate (priv->context, allocation->width, -1);
596 else if (alloc_height != allocation->height && priv->orientation == GTK_ORIENTATION_VERTICAL)
597 gtk_cell_area_context_allocate (priv->context, -1, allocation->height);
601 gtk_cell_view_request_model (GtkCellView *cellview,
603 GtkOrientation orientation,
608 GtkCellViewPrivate *priv = cellview->priv;
615 valid = gtk_tree_model_iter_children (priv->model, &iter, parent);
620 gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
622 if (orientation == GTK_ORIENTATION_HORIZONTAL)
625 gtk_cell_area_get_preferred_width (priv->area, priv->context,
626 GTK_WIDGET (cellview), &min, &nat);
628 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context,
629 GTK_WIDGET (cellview), for_size, &min, &nat);
634 gtk_cell_area_get_preferred_height (priv->area, priv->context,
635 GTK_WIDGET (cellview), &min, &nat);
637 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context,
638 GTK_WIDGET (cellview), for_size, &min, &nat);
641 *minimum_size = MAX (min, *minimum_size);
642 *natural_size = MAX (nat, *natural_size);
644 /* Recurse into children when they exist */
645 gtk_cell_view_request_model (cellview, &iter, orientation, for_size, minimum_size, natural_size);
647 valid = gtk_tree_model_iter_next (priv->model, &iter);
651 static GtkSizeRequestMode
652 gtk_cell_view_get_request_mode (GtkWidget *widget)
654 GtkCellView *cellview = GTK_CELL_VIEW (widget);
655 GtkCellViewPrivate *priv = cellview->priv;
657 return gtk_cell_area_get_request_mode (priv->area);
661 gtk_cell_view_get_preferred_width (GtkWidget *widget,
665 GtkCellView *cellview = GTK_CELL_VIEW (widget);
666 GtkCellViewPrivate *priv = cellview->priv;
668 g_signal_handler_block (priv->context, priv->size_changed_id);
672 gint min = 0, nat = 0;
673 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, -1, &min, &nat);
677 if (cellview->priv->displayed_row)
678 gtk_cell_view_set_cell_data (cellview);
680 gtk_cell_area_get_preferred_width (priv->area, priv->context, widget, NULL, NULL);
683 gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
685 g_signal_handler_unblock (priv->context, priv->size_changed_id);
689 gtk_cell_view_get_preferred_height (GtkWidget *widget,
693 GtkCellView *cellview = GTK_CELL_VIEW (widget);
694 GtkCellViewPrivate *priv = cellview->priv;
696 g_signal_handler_block (priv->context, priv->size_changed_id);
700 gint min = 0, nat = 0;
701 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, -1, &min, &nat);
705 if (cellview->priv->displayed_row)
706 gtk_cell_view_set_cell_data (cellview);
708 gtk_cell_area_get_preferred_height (priv->area, priv->context, widget, NULL, NULL);
711 gtk_cell_area_context_get_preferred_height (priv->context, minimum_size, natural_size);
713 g_signal_handler_unblock (priv->context, priv->size_changed_id);
717 gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
722 GtkCellView *cellview = GTK_CELL_VIEW (widget);
723 GtkCellViewPrivate *priv = cellview->priv;
727 gint min = 0, nat = 0;
728 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, for_size, &min, &nat);
735 if (cellview->priv->displayed_row)
736 gtk_cell_view_set_cell_data (cellview);
738 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context, widget,
739 for_size, minimum_size, natural_size);
744 gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
749 GtkCellView *cellview = GTK_CELL_VIEW (widget);
750 GtkCellViewPrivate *priv = cellview->priv;
754 gint min = 0, nat = 0;
755 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, for_size, &min, &nat);
762 if (cellview->priv->displayed_row)
763 gtk_cell_view_set_cell_data (cellview);
765 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context, widget,
766 for_size, minimum_size, natural_size);
771 gtk_cell_view_draw (GtkWidget *widget,
774 GtkCellView *cellview;
776 GtkCellRendererState state;
778 cellview = GTK_CELL_VIEW (widget);
783 area.width = gtk_widget_get_allocated_width (widget);
784 area.height = gtk_widget_get_allocated_height (widget);
786 /* "blank" background */
787 if (cellview->priv->background_set)
789 gdk_cairo_rectangle (cr, &area);
790 gdk_cairo_set_source_rgba (cr, &cellview->priv->background);
794 /* set cell data (if available) */
795 if (cellview->priv->displayed_row)
796 gtk_cell_view_set_cell_data (cellview);
797 else if (cellview->priv->model)
800 if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT)
801 state = GTK_CELL_RENDERER_PRELIT;
805 /* Render the cells */
806 gtk_cell_area_render (cellview->priv->area, cellview->priv->context,
807 widget, cr, &area, &area, state, FALSE);
813 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
818 g_return_if_fail (cell_view->priv->displayed_row != NULL);
820 path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
824 gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
825 gtk_tree_path_free (path);
827 gtk_cell_area_apply_attributes (cell_view->priv->area,
828 cell_view->priv->model,
829 &iter, FALSE, FALSE);
831 if (cell_view->priv->draw_sensitive)
834 gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view->priv->area));
836 for (l = cells; l; l = l->next)
838 GObject *renderer = l->data;
840 g_object_set (renderer, "sensitive", TRUE, NULL);
846 /* GtkCellLayout implementation */
848 gtk_cell_view_cell_layout_get_area (GtkCellLayout *layout)
850 GtkCellView *cellview = GTK_CELL_VIEW (layout);
851 GtkCellViewPrivate *priv = cellview->priv;
853 if (G_UNLIKELY (!priv->area))
855 priv->area = gtk_cell_area_box_new ();
856 g_object_ref_sink (priv->area);
862 /* GtkBuildable implementation */
864 gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
867 const gchar *tagname,
868 GMarkupParser *parser,
871 if (parent_buildable_iface->custom_tag_start &&
872 parent_buildable_iface->custom_tag_start (buildable, builder, child,
873 tagname, parser, data))
876 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
877 tagname, parser, data);
881 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
884 const gchar *tagname,
887 if (_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
890 else if (parent_buildable_iface->custom_tag_end)
891 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
896 context_size_changed_cb (GtkCellAreaContext *context,
900 if (!strcmp (pspec->name, "minimum-width") ||
901 !strcmp (pspec->name, "natural-width") ||
902 !strcmp (pspec->name, "minimum-height") ||
903 !strcmp (pspec->name, "natural-height"))
904 gtk_widget_queue_resize (view);
908 row_changed_cb (GtkTreeModel *model,
913 GtkTreePath *row_path;
915 if (view->priv->displayed_row)
918 gtk_tree_row_reference_get_path (view->priv->displayed_row);
922 /* Resize everything in our context if our row changed */
923 if (gtk_tree_path_compare (row_path, path) == 0)
924 gtk_cell_area_context_reset (view->priv->context);
926 gtk_tree_path_free (row_path);
934 * Creates a new #GtkCellView widget.
936 * Return value: A newly created #GtkCellView widget.
941 gtk_cell_view_new (void)
943 GtkCellView *cellview;
945 cellview = g_object_new (gtk_cell_view_get_type (), NULL);
947 return GTK_WIDGET (cellview);
952 * gtk_cell_view_new_with_context:
953 * @area: the #GtkCellArea to layout cells
954 * @context: the #GtkCellAreaContext in which to calculate cell geometry
956 * Creates a new #GtkCellView widget with a specific #GtkCellArea
957 * to layout cells and a specific #GtkCellAreaContext.
959 * Specifying the same context for a handfull of cells lets
960 * the underlying area synchronize the geometry for those cells,
961 * in this way alignments with cellviews for other rows are
964 * Return value: A newly created #GtkCellView widget.
969 gtk_cell_view_new_with_context (GtkCellArea *area,
970 GtkCellAreaContext *context)
972 g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
973 g_return_val_if_fail (context == NULL || GTK_IS_CELL_AREA_CONTEXT (context), NULL);
975 return (GtkWidget *)g_object_new (GTK_TYPE_CELL_VIEW,
977 "cell-area-context", context,
982 * gtk_cell_view_new_with_text:
983 * @text: the text to display in the cell view
985 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
986 * to it, and makes its show @text.
988 * Return value: A newly created #GtkCellView widget.
993 gtk_cell_view_new_with_text (const gchar *text)
995 GtkCellView *cellview;
996 GtkCellRenderer *renderer;
997 GValue value = G_VALUE_INIT;
999 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
1001 renderer = gtk_cell_renderer_text_new ();
1002 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
1005 g_value_init (&value, G_TYPE_STRING);
1006 g_value_set_string (&value, text);
1007 gtk_cell_view_set_value (cellview, renderer, "text", &value);
1008 g_value_unset (&value);
1010 return GTK_WIDGET (cellview);
1014 * gtk_cell_view_new_with_markup:
1015 * @markup: the text to display in the cell view
1017 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
1018 * to it, and makes it show @markup. The text can be
1019 * marked up with the <link linkend="PangoMarkupFormat">Pango text
1020 * markup language</link>.
1022 * Return value: A newly created #GtkCellView widget.
1027 gtk_cell_view_new_with_markup (const gchar *markup)
1029 GtkCellView *cellview;
1030 GtkCellRenderer *renderer;
1031 GValue value = G_VALUE_INIT;
1033 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
1035 renderer = gtk_cell_renderer_text_new ();
1036 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
1039 g_value_init (&value, G_TYPE_STRING);
1040 g_value_set_string (&value, markup);
1041 gtk_cell_view_set_value (cellview, renderer, "markup", &value);
1042 g_value_unset (&value);
1044 return GTK_WIDGET (cellview);
1048 * gtk_cell_view_new_with_pixbuf:
1049 * @pixbuf: the image to display in the cell view
1051 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
1052 * to it, and makes its show @pixbuf.
1054 * Return value: A newly created #GtkCellView widget.
1059 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
1061 GtkCellView *cellview;
1062 GtkCellRenderer *renderer;
1063 GValue value = G_VALUE_INIT;
1065 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
1067 renderer = gtk_cell_renderer_pixbuf_new ();
1068 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
1071 g_value_init (&value, GDK_TYPE_PIXBUF);
1072 g_value_set_object (&value, pixbuf);
1073 gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
1074 g_value_unset (&value);
1076 return GTK_WIDGET (cellview);
1080 * gtk_cell_view_set_value:
1081 * @cell_view: a #GtkCellView widget
1082 * @renderer: one of the renderers of @cell_view
1083 * @property: the name of the property of @renderer to set
1084 * @value: the new value to set the property to
1086 * Sets a property of a cell renderer of @cell_view, and
1087 * makes sure the display of @cell_view is updated.
1092 gtk_cell_view_set_value (GtkCellView *cell_view,
1093 GtkCellRenderer *renderer,
1097 g_object_set_property (G_OBJECT (renderer), property, value);
1099 /* force resize and redraw */
1100 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1101 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1105 * gtk_cell_view_set_model:
1106 * @cell_view: a #GtkCellView
1107 * @model: (allow-none): a #GtkTreeModel
1109 * Sets the model for @cell_view. If @cell_view already has a model
1110 * set, it will remove it before setting the new model. If @model is
1111 * %NULL, then it will unset the old model.
1116 gtk_cell_view_set_model (GtkCellView *cell_view,
1117 GtkTreeModel *model)
1119 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1120 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1122 if (cell_view->priv->model)
1124 g_signal_handler_disconnect (cell_view->priv->model,
1125 cell_view->priv->row_changed_id);
1126 cell_view->priv->row_changed_id = 0;
1128 if (cell_view->priv->displayed_row)
1129 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1130 cell_view->priv->displayed_row = NULL;
1132 g_object_unref (cell_view->priv->model);
1135 cell_view->priv->model = model;
1137 if (cell_view->priv->model)
1139 g_object_ref (cell_view->priv->model);
1141 cell_view->priv->row_changed_id =
1142 g_signal_connect (cell_view->priv->model, "row-changed",
1143 G_CALLBACK (row_changed_cb), cell_view);
1148 * gtk_cell_view_get_model:
1149 * @cell_view: a #GtkCellView
1151 * Returns the model for @cell_view. If no model is used %NULL is
1154 * Returns: (transfer none): a #GtkTreeModel used or %NULL
1159 gtk_cell_view_get_model (GtkCellView *cell_view)
1161 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1163 return cell_view->priv->model;
1167 * gtk_cell_view_set_displayed_row:
1168 * @cell_view: a #GtkCellView
1169 * @path: (allow-none): a #GtkTreePath or %NULL to unset.
1171 * Sets the row of the model that is currently displayed
1172 * by the #GtkCellView. If the path is unset, then the
1173 * contents of the cellview "stick" at their last value;
1174 * this is not normally a desired result, but may be
1175 * a needed intermediate state if say, the model for
1176 * the #GtkCellView becomes temporarily empty.
1181 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
1184 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1185 g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
1187 if (cell_view->priv->displayed_row)
1188 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1192 cell_view->priv->displayed_row =
1193 gtk_tree_row_reference_new (cell_view->priv->model, path);
1196 cell_view->priv->displayed_row = NULL;
1198 /* force resize and redraw */
1199 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1200 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1204 * gtk_cell_view_get_displayed_row:
1205 * @cell_view: a #GtkCellView
1207 * Returns a #GtkTreePath referring to the currently
1208 * displayed row. If no row is currently displayed,
1209 * %NULL is returned.
1211 * Returns: the currently displayed row or %NULL
1216 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
1218 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1220 if (!cell_view->priv->displayed_row)
1223 return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
1227 * gtk_cell_view_get_size_of_row:
1228 * @cell_view: a #GtkCellView
1229 * @path: a #GtkTreePath
1230 * @requisition: (out): return location for the size
1232 * Sets @requisition to the size needed by @cell_view to display
1233 * the model row pointed to by @path.
1235 * Return value: %TRUE
1239 * Deprecated: 3.0: Combo box formerly used this to calculate the
1240 * sizes for cellviews, now you can achieve this by either using
1241 * the #GtkCellView:fit-model property or by setting the currently
1242 * displayed row of the #GtkCellView and using gtk_widget_get_preferred_size().
1245 gtk_cell_view_get_size_of_row (GtkCellView *cell_view,
1247 GtkRequisition *requisition)
1249 GtkTreeRowReference *tmp;
1252 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1253 g_return_val_if_fail (path != NULL, FALSE);
1255 tmp = cell_view->priv->displayed_row;
1256 cell_view->priv->displayed_row =
1257 gtk_tree_row_reference_new (cell_view->priv->model, path);
1259 gtk_widget_get_preferred_width (GTK_WIDGET (cell_view), &req.width, NULL);
1260 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (cell_view), req.width, &req.height, NULL);
1262 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1263 cell_view->priv->displayed_row = tmp;
1272 * gtk_cell_view_set_background_color:
1273 * @cell_view: a #GtkCellView
1274 * @color: the new background color
1276 * Sets the background color of @view.
1280 * Deprecated: 3.4: Use gtk_cell_view_set_background_rgba() instead.
1283 gtk_cell_view_set_background_color (GtkCellView *cell_view,
1284 const GdkColor *color)
1286 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1290 if (!cell_view->priv->background_set)
1292 cell_view->priv->background_set = TRUE;
1293 g_object_notify (G_OBJECT (cell_view), "background-set");
1296 cell_view->priv->background.red = color->red / 65535.;
1297 cell_view->priv->background.green = color->green / 65535.;
1298 cell_view->priv->background.blue = color->blue / 65535.;
1299 cell_view->priv->background.alpha = 1;
1303 if (cell_view->priv->background_set)
1305 cell_view->priv->background_set = FALSE;
1306 g_object_notify (G_OBJECT (cell_view), "background-set");
1310 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1314 * gtk_cell_view_set_background_rgba:
1315 * @cell_view: a #GtkCellView
1316 * @rgba: the new background color
1318 * Sets the background color of @cell_view.
1323 gtk_cell_view_set_background_rgba (GtkCellView *cell_view,
1324 const GdkRGBA *rgba)
1326 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1330 if (!cell_view->priv->background_set)
1332 cell_view->priv->background_set = TRUE;
1333 g_object_notify (G_OBJECT (cell_view), "background-set");
1336 cell_view->priv->background = *rgba;
1340 if (cell_view->priv->background_set)
1342 cell_view->priv->background_set = FALSE;
1343 g_object_notify (G_OBJECT (cell_view), "background-set");
1347 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1351 * gtk_cell_view_get_draw_sensitive:
1352 * @cell_view: a #GtkCellView
1354 * Gets whether @cell_view is configured to draw all of its
1355 * cells in a sensitive state.
1357 * Return value: whether @cell_view draws all of its
1358 * cells in a sensitive state
1363 gtk_cell_view_get_draw_sensitive (GtkCellView *cell_view)
1365 GtkCellViewPrivate *priv;
1367 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1369 priv = cell_view->priv;
1371 return priv->draw_sensitive;
1375 * gtk_cell_view_set_draw_sensitive:
1376 * @cell_view: a #GtkCellView
1377 * @draw_sensitive: whether to draw all cells in a sensitive state.
1379 * Sets whether @cell_view should draw all of its
1380 * cells in a sensitive state, this is used by #GtkComboBox menus
1381 * to ensure that rows with insensitive cells that contain
1382 * children appear sensitive in the parent menu item.
1387 gtk_cell_view_set_draw_sensitive (GtkCellView *cell_view,
1388 gboolean draw_sensitive)
1390 GtkCellViewPrivate *priv;
1392 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1394 priv = cell_view->priv;
1396 if (priv->draw_sensitive != draw_sensitive)
1398 priv->draw_sensitive = draw_sensitive;
1400 g_object_notify (G_OBJECT (cell_view), "draw-sensitive");
1405 * gtk_cell_view_get_fit_model:
1406 * @cell_view: a #GtkCellView
1408 * Gets whether @cell_view is configured to request space
1409 * to fit the entire #GtkTreeModel.
1411 * Return value: whether @cell_view requests space to fit
1412 * the entire #GtkTreeModel.
1417 gtk_cell_view_get_fit_model (GtkCellView *cell_view)
1419 GtkCellViewPrivate *priv;
1421 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1423 priv = cell_view->priv;
1425 return priv->fit_model;
1429 * gtk_cell_view_set_fit_model:
1430 * @cell_view: a #GtkCellView
1431 * @fit_model: whether @cell_view should request space for the whole model.
1433 * Sets whether @cell_view should request space to fit the entire #GtkTreeModel.
1435 * This is used by #GtkComboBox to ensure that the cell view displayed on
1436 * the combo box's button always gets enough space and does not resize
1437 * when selection changes.
1442 gtk_cell_view_set_fit_model (GtkCellView *cell_view,
1445 GtkCellViewPrivate *priv;
1447 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1449 priv = cell_view->priv;
1451 if (priv->fit_model != fit_model)
1453 priv->fit_model = fit_model;
1455 gtk_cell_area_context_reset (cell_view->priv->context);
1457 g_object_notify (G_OBJECT (cell_view), "fit-model");