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 "gtkorientable.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 void gtk_cell_view_get_preferred_width (GtkWidget *widget,
99 static void gtk_cell_view_get_preferred_height (GtkWidget *widget,
102 static void gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
106 static void gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
111 static void context_size_changed_cb (GtkCellAreaContext *context,
114 static void row_changed_cb (GtkTreeModel *model,
120 struct _GtkCellViewPrivate
123 GtkTreeRowReference *displayed_row;
126 GtkCellAreaContext *context;
128 GtkOrientation orientation;
131 gboolean background_set;
133 gulong size_changed_id;
134 gulong row_changed_id;
136 guint32 draw_sensitive : 1;
137 guint32 fit_model : 1;
140 static GtkBuildableIface *parent_buildable_iface;
148 PROP_BACKGROUND_RGBA,
152 PROP_CELL_AREA_CONTEXT,
157 G_DEFINE_TYPE_WITH_CODE (GtkCellView, gtk_cell_view, GTK_TYPE_WIDGET,
158 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
159 gtk_cell_view_cell_layout_init)
160 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
161 gtk_cell_view_buildable_init)
162 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
165 gtk_cell_view_class_init (GtkCellViewClass *klass)
167 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
168 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
170 gobject_class->constructor = gtk_cell_view_constructor;
171 gobject_class->get_property = gtk_cell_view_get_property;
172 gobject_class->set_property = gtk_cell_view_set_property;
173 gobject_class->finalize = gtk_cell_view_finalize;
174 gobject_class->dispose = gtk_cell_view_dispose;
176 widget_class->draw = gtk_cell_view_draw;
177 widget_class->size_allocate = gtk_cell_view_size_allocate;
178 widget_class->get_preferred_width = gtk_cell_view_get_preferred_width;
179 widget_class->get_preferred_height = gtk_cell_view_get_preferred_height;
180 widget_class->get_preferred_width_for_height = gtk_cell_view_get_preferred_width_for_height;
181 widget_class->get_preferred_height_for_width = gtk_cell_view_get_preferred_height_for_width;
184 g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
186 g_object_class_install_property (gobject_class,
188 g_param_spec_string ("background",
189 P_("Background color name"),
190 P_("Background color as a string"),
192 GTK_PARAM_WRITABLE));
193 g_object_class_install_property (gobject_class,
195 g_param_spec_boxed ("background-gdk",
196 P_("Background color"),
197 P_("Background color as a GdkColor"),
199 GTK_PARAM_READWRITE));
201 * GtkCellView:background-rgba
203 * The background color as a #GdkRGBA
207 g_object_class_install_property (gobject_class,
208 PROP_BACKGROUND_RGBA,
209 g_param_spec_boxed ("background-rgba",
210 P_("Background RGBA color"),
211 P_("Background color as a GdkRGBA"),
213 GTK_PARAM_READWRITE));
218 * The model for cell view
222 g_object_class_install_property (gobject_class,
224 g_param_spec_object ("model",
225 P_("CellView model"),
226 P_("The model for cell view"),
228 GTK_PARAM_READWRITE));
232 * GtkCellView:cell-area
234 * The #GtkCellArea rendering cells
238 g_object_class_install_property (gobject_class,
240 g_param_spec_object ("cell-area",
242 P_("The GtkCellArea used to layout cells"),
244 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
247 * GtkCellView:cell-area-context
249 * The #GtkCellAreaContext used to compute the geometry of the cell view.
251 * A group of cell views can be assigned the same context in order to
252 * ensure the sizes and cell alignments match across all the views with
255 * #GtkComboBox menus uses this to assign the same context to all cell views
256 * in the menu items for a single menu (each submenu creates it's own
257 * context since the size of each submenu does not depend on parent
262 g_object_class_install_property (gobject_class,
263 PROP_CELL_AREA_CONTEXT,
264 g_param_spec_object ("cell-area-context",
265 P_("Cell Area Context"),
266 P_("The GtkCellAreaContext used to "
267 "compute the geometry of the cell view"),
268 GTK_TYPE_CELL_AREA_CONTEXT,
269 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
272 * GtkCellView:draw-sensitive
274 * Whether all cells should be draw as sensitive for this view regardless
275 * of the actual cell properties (used to make menus with submenus appear
276 * sensitive when the items in submenus might be insensitive).
280 g_object_class_install_property (gobject_class,
282 g_param_spec_boolean ("draw-sensitive",
283 P_("Draw Sensitive"),
284 P_("Whether to force cells to be drawn in a "
287 GTK_PARAM_READWRITE));
290 * GtkCellView:fit-model
292 * Whether the view should request enough space to always fit
293 * the size of every row in the model (used by the combo box to
294 * ensure the combo box size doesnt change when different items
299 g_object_class_install_property (gobject_class,
301 g_param_spec_boolean ("fit-model",
303 P_("Whether to request enough space for "
304 "every row in the model"),
306 GTK_PARAM_READWRITE));
309 #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))
311 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
312 P_("Background set"),
313 P_("Whether this tag affects the background color"));
315 g_type_class_add_private (gobject_class, sizeof (GtkCellViewPrivate));
319 gtk_cell_view_buildable_init (GtkBuildableIface *iface)
321 parent_buildable_iface = g_type_interface_peek_parent (iface);
322 iface->add_child = _gtk_cell_layout_buildable_add_child;
323 iface->custom_tag_start = gtk_cell_view_buildable_custom_tag_start;
324 iface->custom_tag_end = gtk_cell_view_buildable_custom_tag_end;
328 gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface)
330 iface->get_area = gtk_cell_view_cell_layout_get_area;
334 gtk_cell_view_constructor (GType type,
335 guint n_construct_properties,
336 GObjectConstructParam *construct_properties)
340 GtkCellViewPrivate *priv;
342 object = G_OBJECT_CLASS (gtk_cell_view_parent_class)->constructor
343 (type, n_construct_properties, construct_properties);
345 view = GTK_CELL_VIEW (object);
350 GtkCellArea *area = gtk_cell_area_box_new ();
352 priv->area = g_object_ref_sink (area);
356 priv->context = gtk_cell_area_create_context (priv->area);
358 priv->size_changed_id =
359 g_signal_connect (priv->context, "notify",
360 G_CALLBACK (context_size_changed_cb), view);
366 gtk_cell_view_get_property (GObject *object,
371 GtkCellView *view = GTK_CELL_VIEW (object);
375 case PROP_ORIENTATION:
376 g_value_set_enum (value, view->priv->orientation);
378 case PROP_BACKGROUND_GDK:
382 color.red = (guint) (view->priv->background.red * 65535);
383 color.green = (guint) (view->priv->background.green * 65535);
384 color.blue = (guint) (view->priv->background.blue * 65535);
387 g_value_set_boxed (value, &color);
390 case PROP_BACKGROUND_RGBA:
391 g_value_set_boxed (value, &view->priv->background);
393 case PROP_BACKGROUND_SET:
394 g_value_set_boolean (value, view->priv->background_set);
397 g_value_set_object (value, view->priv->model);
400 g_value_set_object (value, view->priv->area);
402 case PROP_CELL_AREA_CONTEXT:
403 g_value_set_object (value, view->priv->context);
405 case PROP_DRAW_SENSITIVE:
406 g_value_set_boolean (value, view->priv->draw_sensitive);
409 g_value_set_boolean (value, view->priv->fit_model);
412 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
418 gtk_cell_view_set_property (GObject *object,
423 GtkCellView *view = GTK_CELL_VIEW (object);
425 GtkCellAreaContext *context;
429 case PROP_ORIENTATION:
430 view->priv->orientation = g_value_get_enum (value);
431 if (view->priv->context)
432 gtk_cell_area_context_reset (view->priv->context);
434 _gtk_orientable_set_style_classes (GTK_ORIENTABLE (object));
436 case PROP_BACKGROUND:
440 if (!g_value_get_string (value))
441 gtk_cell_view_set_background_color (view, NULL);
442 else if (gdk_color_parse (g_value_get_string (value), &color))
443 gtk_cell_view_set_background_color (view, &color);
445 g_warning ("Don't know color `%s'", g_value_get_string (value));
447 g_object_notify (object, "background-gdk");
450 case PROP_BACKGROUND_GDK:
451 gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
453 case PROP_BACKGROUND_RGBA:
454 gtk_cell_view_set_background_rgba (view, g_value_get_boxed (value));
456 case PROP_BACKGROUND_SET:
457 view->priv->background_set = g_value_get_boolean (value);
460 gtk_cell_view_set_model (view, g_value_get_object (value));
463 /* Construct-only, can only be assigned once */
464 area = g_value_get_object (value);
467 view->priv->area = g_object_ref_sink (area);
469 case PROP_CELL_AREA_CONTEXT:
470 /* Construct-only, can only be assigned once */
471 context = g_value_get_object (value);
474 view->priv->context = g_object_ref (context);
477 case PROP_DRAW_SENSITIVE:
478 gtk_cell_view_set_draw_sensitive (view, g_value_get_boolean (value));
482 gtk_cell_view_set_fit_model (view, g_value_get_boolean (value));
486 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
492 gtk_cell_view_init (GtkCellView *cellview)
494 GtkCellViewPrivate *priv;
496 cellview->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellview,
499 priv = cellview->priv;
501 gtk_widget_set_has_window (GTK_WIDGET (cellview), FALSE);
503 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
507 gtk_cell_view_finalize (GObject *object)
509 GtkCellView *cellview = GTK_CELL_VIEW (object);
511 if (cellview->priv->displayed_row)
512 gtk_tree_row_reference_free (cellview->priv->displayed_row);
514 G_OBJECT_CLASS (gtk_cell_view_parent_class)->finalize (object);
518 gtk_cell_view_dispose (GObject *object)
520 GtkCellView *cellview = GTK_CELL_VIEW (object);
522 gtk_cell_view_set_model (cellview, NULL);
524 if (cellview->priv->area)
526 g_object_unref (cellview->priv->area);
527 cellview->priv->area = NULL;
530 if (cellview->priv->context)
532 g_signal_handler_disconnect (cellview->priv->context, cellview->priv->size_changed_id);
534 g_object_unref (cellview->priv->context);
535 cellview->priv->context = NULL;
536 cellview->priv->size_changed_id = 0;
539 G_OBJECT_CLASS (gtk_cell_view_parent_class)->dispose (object);
543 gtk_cell_view_size_allocate (GtkWidget *widget,
544 GtkAllocation *allocation)
546 GtkCellView *cellview = GTK_CELL_VIEW (widget);
547 GtkCellViewPrivate *priv = cellview->priv;
548 gint alloc_width, alloc_height;
550 gtk_widget_set_allocation (widget, allocation);
552 gtk_cell_area_context_get_allocation (priv->context, &alloc_width, &alloc_height);
554 /* The first cell view in context is responsible for allocating the context at allocate time
555 * (or the cellview has its own context and is not grouped with any other cell views)
557 * If the cellview is in "fit model" mode, we assume its not in context and needs to
558 * allocate every time.
561 gtk_cell_area_context_allocate (priv->context, allocation->width, allocation->height);
562 else if (alloc_width != allocation->width && priv->orientation == GTK_ORIENTATION_HORIZONTAL)
563 gtk_cell_area_context_allocate (priv->context, allocation->width, -1);
564 else if (alloc_height != allocation->height && priv->orientation == GTK_ORIENTATION_VERTICAL)
565 gtk_cell_area_context_allocate (priv->context, -1, allocation->height);
569 gtk_cell_view_request_model (GtkCellView *cellview,
571 GtkOrientation orientation,
576 GtkCellViewPrivate *priv = cellview->priv;
580 valid = gtk_tree_model_iter_children (priv->model, &iter, parent);
585 gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
587 if (orientation == GTK_ORIENTATION_HORIZONTAL)
590 gtk_cell_area_get_preferred_width (priv->area, priv->context,
591 GTK_WIDGET (cellview), &min, &nat);
593 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context,
594 GTK_WIDGET (cellview), for_size, &min, &nat);
599 gtk_cell_area_get_preferred_height (priv->area, priv->context,
600 GTK_WIDGET (cellview), &min, &nat);
602 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context,
603 GTK_WIDGET (cellview), for_size, &min, &nat);
606 *minimum_size = MAX (min, *minimum_size);
607 *natural_size = MAX (nat, *natural_size);
609 /* Recurse into children when they exist */
610 gtk_cell_view_request_model (cellview, &iter, orientation, for_size, minimum_size, natural_size);
612 valid = gtk_tree_model_iter_next (priv->model, &iter);
617 gtk_cell_view_get_preferred_width (GtkWidget *widget,
621 GtkCellView *cellview = GTK_CELL_VIEW (widget);
622 GtkCellViewPrivate *priv = cellview->priv;
624 g_signal_handler_block (priv->context, priv->size_changed_id);
628 gint min = 0, nat = 0;
629 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, -1, &min, &nat);
633 if (cellview->priv->displayed_row)
634 gtk_cell_view_set_cell_data (cellview);
636 gtk_cell_area_get_preferred_width (priv->area, priv->context, widget, NULL, NULL);
639 gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
641 g_signal_handler_unblock (priv->context, priv->size_changed_id);
645 gtk_cell_view_get_preferred_height (GtkWidget *widget,
649 GtkCellView *cellview = GTK_CELL_VIEW (widget);
650 GtkCellViewPrivate *priv = cellview->priv;
652 g_signal_handler_block (priv->context, priv->size_changed_id);
656 gint min = 0, nat = 0;
657 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, -1, &min, &nat);
661 if (cellview->priv->displayed_row)
662 gtk_cell_view_set_cell_data (cellview);
664 gtk_cell_area_get_preferred_height (priv->area, priv->context, widget, NULL, NULL);
667 gtk_cell_area_context_get_preferred_height (priv->context, minimum_size, natural_size);
669 g_signal_handler_unblock (priv->context, priv->size_changed_id);
673 gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
678 GtkCellView *cellview = GTK_CELL_VIEW (widget);
679 GtkCellViewPrivate *priv = cellview->priv;
683 gint min = 0, nat = 0;
684 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, for_size, &min, &nat);
691 if (cellview->priv->displayed_row)
692 gtk_cell_view_set_cell_data (cellview);
694 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context, widget,
695 for_size, minimum_size, natural_size);
700 gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
705 GtkCellView *cellview = GTK_CELL_VIEW (widget);
706 GtkCellViewPrivate *priv = cellview->priv;
710 gint min = 0, nat = 0;
711 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, for_size, &min, &nat);
718 if (cellview->priv->displayed_row)
719 gtk_cell_view_set_cell_data (cellview);
721 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context, widget,
722 for_size, minimum_size, natural_size);
727 gtk_cell_view_draw (GtkWidget *widget,
730 GtkCellView *cellview;
732 GtkCellRendererState state;
734 cellview = GTK_CELL_VIEW (widget);
739 area.width = gtk_widget_get_allocated_width (widget);
740 area.height = gtk_widget_get_allocated_height (widget);
742 /* "blank" background */
743 if (cellview->priv->background_set)
745 gdk_cairo_rectangle (cr, &area);
746 gdk_cairo_set_source_rgba (cr, &cellview->priv->background);
750 /* set cell data (if available) */
751 if (cellview->priv->displayed_row)
752 gtk_cell_view_set_cell_data (cellview);
753 else if (cellview->priv->model)
756 if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT)
757 state = GTK_CELL_RENDERER_PRELIT;
758 else if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_INSENSITIVE)
759 state = GTK_CELL_RENDERER_INSENSITIVE;
763 /* Render the cells */
764 gtk_cell_area_render (cellview->priv->area, cellview->priv->context,
765 widget, cr, &area, &area, state, FALSE);
771 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
776 g_return_if_fail (cell_view->priv->displayed_row != NULL);
778 path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
782 gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
783 gtk_tree_path_free (path);
785 gtk_cell_area_apply_attributes (cell_view->priv->area,
786 cell_view->priv->model,
787 &iter, FALSE, FALSE);
789 if (cell_view->priv->draw_sensitive)
792 gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view->priv->area));
794 for (l = cells; l; l = l->next)
796 GObject *renderer = l->data;
798 g_object_set (renderer, "sensitive", TRUE, NULL);
804 /* GtkCellLayout implementation */
806 gtk_cell_view_cell_layout_get_area (GtkCellLayout *layout)
808 GtkCellView *cellview = GTK_CELL_VIEW (layout);
810 return cellview->priv->area;
813 /* GtkBuildable implementation */
815 gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
818 const gchar *tagname,
819 GMarkupParser *parser,
822 if (parent_buildable_iface->custom_tag_start &&
823 parent_buildable_iface->custom_tag_start (buildable, builder, child,
824 tagname, parser, data))
827 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
828 tagname, parser, data);
832 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
835 const gchar *tagname,
838 if (_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
841 else if (parent_buildable_iface->custom_tag_end)
842 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
847 context_size_changed_cb (GtkCellAreaContext *context,
851 if (!strcmp (pspec->name, "minimum-width") ||
852 !strcmp (pspec->name, "natural-width") ||
853 !strcmp (pspec->name, "minimum-height") ||
854 !strcmp (pspec->name, "natural-height"))
855 gtk_widget_queue_resize (view);
859 row_changed_cb (GtkTreeModel *model,
864 GtkTreePath *row_path;
866 if (view->priv->displayed_row)
869 gtk_tree_row_reference_get_path (view->priv->displayed_row);
873 /* Resize everything in our context if our row changed */
874 if (gtk_tree_path_compare (row_path, path) == 0)
875 gtk_cell_area_context_reset (view->priv->context);
877 gtk_tree_path_free (row_path);
885 * Creates a new #GtkCellView widget.
887 * Return value: A newly created #GtkCellView widget.
892 gtk_cell_view_new (void)
894 GtkCellView *cellview;
896 cellview = g_object_new (gtk_cell_view_get_type (), NULL);
898 return GTK_WIDGET (cellview);
903 * gtk_cell_view_new_with_context:
904 * @area: the #GtkCellArea to layout cells
905 * @context: the #GtkCellAreaContext in which to calculate cell geometry
907 * Creates a new #GtkCellView widget with a specific #GtkCellArea
908 * to layout cells and a specific #GtkCellAreaContext.
910 * Specifying the same context for a handfull of cells lets
911 * the underlying area synchronize the geometry for those cells,
912 * in this way alignments with cellviews for other rows are
915 * Return value: A newly created #GtkCellView widget.
920 gtk_cell_view_new_with_context (GtkCellArea *area,
921 GtkCellAreaContext *context)
923 g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
924 g_return_val_if_fail (context == NULL || GTK_IS_CELL_AREA_CONTEXT (context), NULL);
926 return (GtkWidget *)g_object_new (GTK_TYPE_CELL_VIEW,
928 "cell-area-context", context,
933 * gtk_cell_view_new_with_text:
934 * @text: the text to display in the cell view
936 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
937 * to it, and makes its show @text.
939 * Return value: A newly created #GtkCellView widget.
944 gtk_cell_view_new_with_text (const gchar *text)
946 GtkCellView *cellview;
947 GtkCellRenderer *renderer;
948 GValue value = {0, };
950 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
952 renderer = gtk_cell_renderer_text_new ();
953 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
956 g_value_init (&value, G_TYPE_STRING);
957 g_value_set_string (&value, text);
958 gtk_cell_view_set_value (cellview, renderer, "text", &value);
959 g_value_unset (&value);
961 return GTK_WIDGET (cellview);
965 * gtk_cell_view_new_with_markup:
966 * @markup: the text to display in the cell view
968 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
969 * to it, and makes it show @markup. The text can be
970 * marked up with the <link linkend="PangoMarkupFormat">Pango text
971 * markup language</link>.
973 * Return value: A newly created #GtkCellView widget.
978 gtk_cell_view_new_with_markup (const gchar *markup)
980 GtkCellView *cellview;
981 GtkCellRenderer *renderer;
982 GValue value = {0, };
984 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
986 renderer = gtk_cell_renderer_text_new ();
987 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
990 g_value_init (&value, G_TYPE_STRING);
991 g_value_set_string (&value, markup);
992 gtk_cell_view_set_value (cellview, renderer, "markup", &value);
993 g_value_unset (&value);
995 return GTK_WIDGET (cellview);
999 * gtk_cell_view_new_with_pixbuf:
1000 * @pixbuf: the image to display in the cell view
1002 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
1003 * to it, and makes its show @pixbuf.
1005 * Return value: A newly created #GtkCellView widget.
1010 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
1012 GtkCellView *cellview;
1013 GtkCellRenderer *renderer;
1014 GValue value = {0, };
1016 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
1018 renderer = gtk_cell_renderer_pixbuf_new ();
1019 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
1022 g_value_init (&value, GDK_TYPE_PIXBUF);
1023 g_value_set_object (&value, pixbuf);
1024 gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
1025 g_value_unset (&value);
1027 return GTK_WIDGET (cellview);
1031 * gtk_cell_view_set_value:
1032 * @cell_view: a #GtkCellView widget
1033 * @renderer: one of the renderers of @cell_view
1034 * @property: the name of the property of @renderer to set
1035 * @value: the new value to set the property to
1037 * Sets a property of a cell renderer of @cell_view, and
1038 * makes sure the display of @cell_view is updated.
1043 gtk_cell_view_set_value (GtkCellView *cell_view,
1044 GtkCellRenderer *renderer,
1048 g_object_set_property (G_OBJECT (renderer), property, value);
1050 /* force resize and redraw */
1051 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1052 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1056 * gtk_cell_view_set_model:
1057 * @cell_view: a #GtkCellView
1058 * @model: (allow-none): a #GtkTreeModel
1060 * Sets the model for @cell_view. If @cell_view already has a model
1061 * set, it will remove it before setting the new model. If @model is
1062 * %NULL, then it will unset the old model.
1067 gtk_cell_view_set_model (GtkCellView *cell_view,
1068 GtkTreeModel *model)
1070 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1071 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1073 if (cell_view->priv->model)
1075 g_signal_handler_disconnect (cell_view->priv->model,
1076 cell_view->priv->row_changed_id);
1077 cell_view->priv->row_changed_id = 0;
1079 if (cell_view->priv->displayed_row)
1080 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1081 cell_view->priv->displayed_row = NULL;
1083 g_object_unref (cell_view->priv->model);
1084 cell_view->priv->model = NULL;
1087 cell_view->priv->model = model;
1089 if (cell_view->priv->model)
1091 g_object_ref (cell_view->priv->model);
1093 cell_view->priv->row_changed_id =
1094 g_signal_connect (cell_view->priv->model, "row-changed",
1095 G_CALLBACK (row_changed_cb), cell_view);
1100 * gtk_cell_view_get_model:
1101 * @cell_view: a #GtkCellView
1103 * Returns the model for @cell_view. If no model is used %NULL is
1106 * Returns: (transfer none): a #GtkTreeModel used or %NULL
1111 gtk_cell_view_get_model (GtkCellView *cell_view)
1113 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1115 return cell_view->priv->model;
1119 * gtk_cell_view_set_displayed_row:
1120 * @cell_view: a #GtkCellView
1121 * @path: (allow-none): a #GtkTreePath or %NULL to unset.
1123 * Sets the row of the model that is currently displayed
1124 * by the #GtkCellView. If the path is unset, then the
1125 * contents of the cellview "stick" at their last value;
1126 * this is not normally a desired result, but may be
1127 * a needed intermediate state if say, the model for
1128 * the #GtkCellView becomes temporarily empty.
1133 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
1136 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1137 g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
1139 if (cell_view->priv->displayed_row)
1140 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1144 cell_view->priv->displayed_row =
1145 gtk_tree_row_reference_new (cell_view->priv->model, path);
1148 cell_view->priv->displayed_row = NULL;
1150 /* force resize and redraw */
1151 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1152 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1156 * gtk_cell_view_get_displayed_row:
1157 * @cell_view: a #GtkCellView
1159 * Returns a #GtkTreePath referring to the currently
1160 * displayed row. If no row is currently displayed,
1161 * %NULL is returned.
1163 * Returns: the currently displayed row or %NULL
1168 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
1170 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1172 if (!cell_view->priv->displayed_row)
1175 return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
1179 * gtk_cell_view_get_size_of_row:
1180 * @cell_view: a #GtkCellView
1181 * @path: a #GtkTreePath
1182 * @requisition: (out): return location for the size
1184 * Sets @requisition to the size needed by @cell_view to display
1185 * the model row pointed to by @path.
1187 * Return value: %TRUE
1191 * Deprecated: 3.0: Combo box formerly used this to calculate the
1192 * sizes for cellviews, now you can achieve this by either using
1193 * the #GtkCellView:fit-model property or by setting the currently
1194 * displayed row of the #GtkCellView and using gtk_widget_get_preferred_size().
1197 gtk_cell_view_get_size_of_row (GtkCellView *cell_view,
1199 GtkRequisition *requisition)
1201 GtkTreeRowReference *tmp;
1204 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1205 g_return_val_if_fail (path != NULL, FALSE);
1207 tmp = cell_view->priv->displayed_row;
1208 cell_view->priv->displayed_row =
1209 gtk_tree_row_reference_new (cell_view->priv->model, path);
1211 gtk_widget_get_preferred_width (GTK_WIDGET (cell_view), &req.width, NULL);
1212 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (cell_view), req.width, &req.height, NULL);
1214 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1215 cell_view->priv->displayed_row = tmp;
1224 * gtk_cell_view_set_background_color:
1225 * @cell_view: a #GtkCellView
1226 * @color: the new background color
1228 * Sets the background color of @view.
1233 gtk_cell_view_set_background_color (GtkCellView *cell_view,
1234 const GdkColor *color)
1236 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1240 if (!cell_view->priv->background_set)
1242 cell_view->priv->background_set = TRUE;
1243 g_object_notify (G_OBJECT (cell_view), "background-set");
1246 cell_view->priv->background.red = color->red / 65535.;
1247 cell_view->priv->background.green = color->green / 65535.;
1248 cell_view->priv->background.blue = color->blue / 65535.;
1249 cell_view->priv->background.alpha = 1;
1253 if (cell_view->priv->background_set)
1255 cell_view->priv->background_set = FALSE;
1256 g_object_notify (G_OBJECT (cell_view), "background-set");
1260 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1264 * gtk_cell_view_set_background_rgba:
1265 * @cell_view: a #GtkCellView
1266 * @rgba: the new background color
1268 * Sets the background color of @cell_view.
1273 gtk_cell_view_set_background_rgba (GtkCellView *cell_view,
1274 const GdkRGBA *rgba)
1276 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1280 if (!cell_view->priv->background_set)
1282 cell_view->priv->background_set = TRUE;
1283 g_object_notify (G_OBJECT (cell_view), "background-set");
1286 cell_view->priv->background = *rgba;
1290 if (cell_view->priv->background_set)
1292 cell_view->priv->background_set = FALSE;
1293 g_object_notify (G_OBJECT (cell_view), "background-set");
1297 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1301 * gtk_cell_view_get_draw_sensitive:
1302 * @cell_view: a #GtkCellView
1304 * Gets whether @cell_view is configured to draw all of it's
1305 * cells in a sensitive state.
1307 * Return value: whether @cell_view draws all of it's
1308 * cells in a sensitive state
1313 gtk_cell_view_get_draw_sensitive (GtkCellView *cell_view)
1315 GtkCellViewPrivate *priv;
1317 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1319 priv = cell_view->priv;
1321 return priv->draw_sensitive;
1325 * gtk_cell_view_set_draw_sensitive:
1326 * @cell_view: a #GtkCellView
1327 * @draw_sensitive: whether to draw all cells in a sensitive state.
1329 * Sets whether @cell_view should draw all of it's
1330 * cells in a sensitive state, this is used by #GtkComboBox menus
1331 * to ensure that rows with insensitive cells that contain
1332 * children appear sensitive in the parent menu item.
1337 gtk_cell_view_set_draw_sensitive (GtkCellView *cell_view,
1338 gboolean draw_sensitive)
1340 GtkCellViewPrivate *priv;
1342 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1344 priv = cell_view->priv;
1346 if (priv->draw_sensitive != draw_sensitive)
1348 priv->draw_sensitive = draw_sensitive;
1350 g_object_notify (G_OBJECT (cell_view), "draw-sensitive");
1355 * gtk_cell_view_get_fit_model:
1356 * @cell_view: a #GtkCellView
1358 * Gets whether @cell_view is configured to request space
1359 * to fit the entire #GtkTreeModel.
1361 * Return value: whether @cell_view requests space to fit
1362 * the entire #GtkTreeModel.
1367 gtk_cell_view_get_fit_model (GtkCellView *cell_view)
1369 GtkCellViewPrivate *priv;
1371 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1373 priv = cell_view->priv;
1375 return priv->fit_model;
1379 * gtk_cell_view_set_fit_model:
1380 * @cell_view: a #GtkCellView
1381 * @fit_model: whether @cell_view should request space for the whole model.
1383 * Sets whether @cell_view should request space to fit the entire #GtkTreeModel.
1385 * This is used by #GtkComboBox to ensure that the cell view displayed on
1386 * the combo box's button always gets enough space and does not resize
1387 * when selection changes.
1392 gtk_cell_view_set_fit_model (GtkCellView *cell_view,
1395 GtkCellViewPrivate *priv;
1397 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1399 priv = cell_view->priv;
1401 if (priv->fit_model != fit_model)
1403 priv->fit_model = fit_model;
1405 gtk_cell_area_context_reset (cell_view->priv->context);
1407 g_object_notify (G_OBJECT (cell_view), "fit-model");