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 of #GtkTreeMenu).
45 * #GtkCellView is #GtkOrientable in order to decide in which orientation
46 * the underlying #GtkCellAreaContext should be allocated. Taking the #GtkTreeMenu
47 * as an example, cellviews should be oriented horizontally if the menus are
48 * listed top-to-bottom and thus all share the same width but may have separate
49 * individual heights (left-to-right menus should be allocated vertically since
50 * they all share the same height but may have variable widths).
53 static GObject *gtk_cell_view_constructor (GType type,
54 guint n_construct_properties,
55 GObjectConstructParam *construct_properties);
56 static void gtk_cell_view_get_property (GObject *object,
60 static void gtk_cell_view_set_property (GObject *object,
64 static void gtk_cell_view_finalize (GObject *object);
65 static void gtk_cell_view_dispose (GObject *object);
66 static void gtk_cell_view_size_allocate (GtkWidget *widget,
67 GtkAllocation *allocation);
68 static gboolean gtk_cell_view_draw (GtkWidget *widget,
70 static void gtk_cell_view_set_value (GtkCellView *cell_view,
71 GtkCellRenderer *renderer,
74 static void gtk_cell_view_set_cell_data (GtkCellView *cell_view);
77 static void gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface);
78 static GtkCellArea *gtk_cell_view_cell_layout_get_area (GtkCellLayout *layout);
82 static void gtk_cell_view_buildable_init (GtkBuildableIface *iface);
83 static gboolean gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
87 GMarkupParser *parser,
89 static void gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
95 static void gtk_cell_view_get_preferred_width (GtkWidget *widget,
98 static void gtk_cell_view_get_preferred_height (GtkWidget *widget,
101 static void gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
105 static void gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
110 static void context_size_changed_cb (GtkCellAreaContext *context,
113 static void row_changed_cb (GtkTreeModel *model,
119 struct _GtkCellViewPrivate
122 GtkTreeRowReference *displayed_row;
125 GtkCellAreaContext *context;
127 GtkOrientation orientation;
130 gboolean background_set;
132 gulong size_changed_id;
133 gulong row_changed_id;
135 guint32 draw_sensitive : 1;
136 guint32 fit_model : 1;
139 static GtkBuildableIface *parent_buildable_iface;
147 PROP_BACKGROUND_RGBA,
151 PROP_CELL_AREA_CONTEXT,
156 G_DEFINE_TYPE_WITH_CODE (GtkCellView, gtk_cell_view, GTK_TYPE_WIDGET,
157 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
158 gtk_cell_view_cell_layout_init)
159 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
160 gtk_cell_view_buildable_init)
161 G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
164 gtk_cell_view_class_init (GtkCellViewClass *klass)
166 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
167 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
169 gobject_class->constructor = gtk_cell_view_constructor;
170 gobject_class->get_property = gtk_cell_view_get_property;
171 gobject_class->set_property = gtk_cell_view_set_property;
172 gobject_class->finalize = gtk_cell_view_finalize;
173 gobject_class->dispose = gtk_cell_view_dispose;
175 widget_class->draw = gtk_cell_view_draw;
176 widget_class->size_allocate = gtk_cell_view_size_allocate;
177 widget_class->get_preferred_width = gtk_cell_view_get_preferred_width;
178 widget_class->get_preferred_height = gtk_cell_view_get_preferred_height;
179 widget_class->get_preferred_width_for_height = gtk_cell_view_get_preferred_width_for_height;
180 widget_class->get_preferred_height_for_width = gtk_cell_view_get_preferred_height_for_width;
183 g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
185 g_object_class_install_property (gobject_class,
187 g_param_spec_string ("background",
188 P_("Background color name"),
189 P_("Background color as a string"),
191 GTK_PARAM_WRITABLE));
192 g_object_class_install_property (gobject_class,
194 g_param_spec_boxed ("background-gdk",
195 P_("Background color"),
196 P_("Background color as a GdkColor"),
198 GTK_PARAM_READWRITE));
200 * GtkCellView:background-rgba
202 * The background color as a #GdkRGBA
206 g_object_class_install_property (gobject_class,
207 PROP_BACKGROUND_RGBA,
208 g_param_spec_boxed ("background-rgba",
209 P_("Background RGBA color"),
210 P_("Background color as a GdkRGBA"),
212 GTK_PARAM_READWRITE));
217 * The model for cell view
221 g_object_class_install_property (gobject_class,
223 g_param_spec_object ("model",
224 P_("CellView model"),
225 P_("The model for cell view"),
227 GTK_PARAM_READWRITE));
231 * GtkCellView:cell-area
233 * The #GtkCellArea rendering cells
237 g_object_class_install_property (gobject_class,
239 g_param_spec_object ("cell-area",
241 P_("The GtkCellArea used to layout cells"),
243 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
246 * GtkCellView:cell-area-context
248 * The #GtkCellAreaContext used to compute the geometry of the cell view.
250 * A group of cell views can be assigned the same context in order to
251 * ensure the sizes and cell alignments match across all the views with
254 * #GtkTreeMenu uses this to assign the same context to all cell views
255 * in the menu items for a single menu (each submenu creates it's own
256 * context since the size of each submenu does not depend on parent
261 g_object_class_install_property (gobject_class,
262 PROP_CELL_AREA_CONTEXT,
263 g_param_spec_object ("cell-area-context",
264 P_("Cell Area Context"),
265 P_("The GtkCellAreaContext used to "
266 "compute the geometry of the cell view"),
267 GTK_TYPE_CELL_AREA_CONTEXT,
268 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
271 * GtkCellView:draw-sensitive
273 * Whether all cells should be draw as sensitive for this view regardless
274 * of the actual cell properties (used to make menus with submenus appear
275 * sensitive when the items in submenus might be insensitive).
279 g_object_class_install_property (gobject_class,
281 g_param_spec_boolean ("draw-sensitive",
282 P_("Draw Sensitive"),
283 P_("Whether to force cells to be drawn in a "
286 GTK_PARAM_READWRITE));
289 * GtkCellView:fit-model
291 * Whether the view should request enough space to always fit
292 * the size of every row in the model (used by the combo box to
293 * ensure the combo box size doesnt change when different items
298 g_object_class_install_property (gobject_class,
300 g_param_spec_boolean ("fit-model",
302 P_("Whether to request enough space for "
303 "every row in the model"),
305 GTK_PARAM_READWRITE));
308 #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))
310 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
311 P_("Background set"),
312 P_("Whether this tag affects the background color"));
314 g_type_class_add_private (gobject_class, sizeof (GtkCellViewPrivate));
318 gtk_cell_view_buildable_init (GtkBuildableIface *iface)
320 parent_buildable_iface = g_type_interface_peek_parent (iface);
321 iface->add_child = _gtk_cell_layout_buildable_add_child;
322 iface->custom_tag_start = gtk_cell_view_buildable_custom_tag_start;
323 iface->custom_tag_end = gtk_cell_view_buildable_custom_tag_end;
327 gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface)
329 iface->get_area = gtk_cell_view_cell_layout_get_area;
333 gtk_cell_view_constructor (GType type,
334 guint n_construct_properties,
335 GObjectConstructParam *construct_properties)
339 GtkCellViewPrivate *priv;
341 object = G_OBJECT_CLASS (gtk_cell_view_parent_class)->constructor
342 (type, n_construct_properties, construct_properties);
344 view = GTK_CELL_VIEW (object);
349 GtkCellArea *area = gtk_cell_area_box_new ();
351 priv->area = g_object_ref_sink (area);
355 priv->context = gtk_cell_area_create_context (priv->area);
357 priv->size_changed_id =
358 g_signal_connect (priv->context, "notify",
359 G_CALLBACK (context_size_changed_cb), view);
365 gtk_cell_view_get_property (GObject *object,
370 GtkCellView *view = GTK_CELL_VIEW (object);
374 case PROP_ORIENTATION:
375 g_value_set_enum (value, view->priv->orientation);
377 case PROP_BACKGROUND_GDK:
381 color.red = (guint) (view->priv->background.red * 65535);
382 color.green = (guint) (view->priv->background.green * 65535);
383 color.blue = (guint) (view->priv->background.blue * 65535);
386 g_value_set_boxed (value, &color);
389 case PROP_BACKGROUND_RGBA:
390 g_value_set_boxed (value, &view->priv->background);
392 case PROP_BACKGROUND_SET:
393 g_value_set_boolean (value, view->priv->background_set);
396 g_value_set_object (value, view->priv->model);
399 g_value_set_object (value, view->priv->area);
401 case PROP_CELL_AREA_CONTEXT:
402 g_value_set_object (value, view->priv->context);
404 case PROP_DRAW_SENSITIVE:
405 g_value_set_boolean (value, view->priv->draw_sensitive);
408 g_value_set_boolean (value, view->priv->fit_model);
411 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
417 gtk_cell_view_set_property (GObject *object,
422 GtkCellView *view = GTK_CELL_VIEW (object);
424 GtkCellAreaContext *context;
428 case PROP_ORIENTATION:
429 view->priv->orientation = g_value_get_enum (value);
430 if (view->priv->context)
431 gtk_cell_area_context_reset (view->priv->context);
433 case PROP_BACKGROUND:
437 if (!g_value_get_string (value))
438 gtk_cell_view_set_background_color (view, NULL);
439 else if (gdk_color_parse (g_value_get_string (value), &color))
440 gtk_cell_view_set_background_color (view, &color);
442 g_warning ("Don't know color `%s'", g_value_get_string (value));
444 g_object_notify (object, "background-gdk");
447 case PROP_BACKGROUND_GDK:
448 gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
450 case PROP_BACKGROUND_RGBA:
451 gtk_cell_view_set_background_rgba (view, g_value_get_boxed (value));
453 case PROP_BACKGROUND_SET:
454 view->priv->background_set = g_value_get_boolean (value);
457 gtk_cell_view_set_model (view, g_value_get_object (value));
460 /* Construct-only, can only be assigned once */
461 area = g_value_get_object (value);
464 view->priv->area = g_object_ref_sink (area);
466 case PROP_CELL_AREA_CONTEXT:
467 /* Construct-only, can only be assigned once */
468 context = g_value_get_object (value);
471 view->priv->context = g_object_ref (context);
474 case PROP_DRAW_SENSITIVE:
475 gtk_cell_view_set_draw_sensitive (view, g_value_get_boolean (value));
479 gtk_cell_view_set_fit_model (view, g_value_get_boolean (value));
483 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
489 gtk_cell_view_init (GtkCellView *cellview)
491 GtkCellViewPrivate *priv;
493 cellview->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellview,
496 priv = cellview->priv;
498 gtk_widget_set_has_window (GTK_WIDGET (cellview), FALSE);
500 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
504 gtk_cell_view_finalize (GObject *object)
506 GtkCellView *cellview = GTK_CELL_VIEW (object);
508 if (cellview->priv->displayed_row)
509 gtk_tree_row_reference_free (cellview->priv->displayed_row);
511 G_OBJECT_CLASS (gtk_cell_view_parent_class)->finalize (object);
515 gtk_cell_view_dispose (GObject *object)
517 GtkCellView *cellview = GTK_CELL_VIEW (object);
519 gtk_cell_view_set_model (cellview, NULL);
521 if (cellview->priv->area)
523 g_object_unref (cellview->priv->area);
524 cellview->priv->area = NULL;
527 if (cellview->priv->context)
529 g_signal_handler_disconnect (cellview->priv->context, cellview->priv->size_changed_id);
531 g_object_unref (cellview->priv->context);
532 cellview->priv->context = NULL;
533 cellview->priv->size_changed_id = 0;
536 G_OBJECT_CLASS (gtk_cell_view_parent_class)->dispose (object);
540 gtk_cell_view_size_allocate (GtkWidget *widget,
541 GtkAllocation *allocation)
543 GtkCellView *cellview = GTK_CELL_VIEW (widget);
544 GtkCellViewPrivate *priv = cellview->priv;
545 gint alloc_width, alloc_height;
547 gtk_widget_set_allocation (widget, allocation);
549 gtk_cell_area_context_get_allocation (priv->context, &alloc_width, &alloc_height);
551 /* The first cell view in context is responsible for allocating the context at allocate time
552 * (or the cellview has its own context and is not grouped with any other cell views)
554 * If the cellview is in "fit model" mode, we assume its not in context and needs to
555 * allocate every time.
558 gtk_cell_area_context_allocate (priv->context, allocation->width, allocation->height);
559 else if (alloc_width != allocation->width && priv->orientation == GTK_ORIENTATION_HORIZONTAL)
560 gtk_cell_area_context_allocate (priv->context, allocation->width, -1);
561 else if (alloc_height != allocation->height && priv->orientation == GTK_ORIENTATION_VERTICAL)
562 gtk_cell_area_context_allocate (priv->context, -1, allocation->height);
566 gtk_cell_view_request_model (GtkCellView *cellview,
568 GtkOrientation orientation,
573 GtkCellViewPrivate *priv = cellview->priv;
577 valid = gtk_tree_model_iter_children (priv->model, &iter, parent);
582 gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
584 if (orientation == GTK_ORIENTATION_HORIZONTAL)
587 gtk_cell_area_get_preferred_width (priv->area, priv->context,
588 GTK_WIDGET (cellview), &min, &nat);
590 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context,
591 GTK_WIDGET (cellview), for_size, &min, &nat);
596 gtk_cell_area_get_preferred_height (priv->area, priv->context,
597 GTK_WIDGET (cellview), &min, &nat);
599 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context,
600 GTK_WIDGET (cellview), for_size, &min, &nat);
603 *minimum_size = MAX (min, *minimum_size);
604 *natural_size = MAX (nat, *natural_size);
606 /* Recurse into children when they exist */
607 gtk_cell_view_request_model (cellview, &iter, orientation, for_size, minimum_size, natural_size);
609 valid = gtk_tree_model_iter_next (priv->model, &iter);
614 gtk_cell_view_get_preferred_width (GtkWidget *widget,
618 GtkCellView *cellview = GTK_CELL_VIEW (widget);
619 GtkCellViewPrivate *priv = cellview->priv;
621 g_signal_handler_block (priv->context, priv->size_changed_id);
625 gint min = 0, nat = 0;
626 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, -1, &min, &nat);
630 if (cellview->priv->displayed_row)
631 gtk_cell_view_set_cell_data (cellview);
633 gtk_cell_area_get_preferred_width (priv->area, priv->context, widget, NULL, NULL);
636 gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
638 g_signal_handler_unblock (priv->context, priv->size_changed_id);
642 gtk_cell_view_get_preferred_height (GtkWidget *widget,
646 GtkCellView *cellview = GTK_CELL_VIEW (widget);
647 GtkCellViewPrivate *priv = cellview->priv;
649 g_signal_handler_block (priv->context, priv->size_changed_id);
653 gint min = 0, nat = 0;
654 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, -1, &min, &nat);
658 if (cellview->priv->displayed_row)
659 gtk_cell_view_set_cell_data (cellview);
661 gtk_cell_area_get_preferred_height (priv->area, priv->context, widget, NULL, NULL);
664 gtk_cell_area_context_get_preferred_height (priv->context, minimum_size, natural_size);
666 g_signal_handler_unblock (priv->context, priv->size_changed_id);
670 gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
675 GtkCellView *cellview = GTK_CELL_VIEW (widget);
676 GtkCellViewPrivate *priv = cellview->priv;
680 gint min = 0, nat = 0;
681 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, for_size, &min, &nat);
688 if (cellview->priv->displayed_row)
689 gtk_cell_view_set_cell_data (cellview);
691 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context, widget,
692 for_size, minimum_size, natural_size);
697 gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
702 GtkCellView *cellview = GTK_CELL_VIEW (widget);
703 GtkCellViewPrivate *priv = cellview->priv;
707 gint min = 0, nat = 0;
708 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, for_size, &min, &nat);
715 if (cellview->priv->displayed_row)
716 gtk_cell_view_set_cell_data (cellview);
718 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context, widget,
719 for_size, minimum_size, natural_size);
724 gtk_cell_view_draw (GtkWidget *widget,
727 GtkCellView *cellview;
729 GtkCellRendererState state;
731 cellview = GTK_CELL_VIEW (widget);
736 area.width = gtk_widget_get_allocated_width (widget);
737 area.height = gtk_widget_get_allocated_height (widget);
739 /* "blank" background */
740 if (cellview->priv->background_set)
742 gdk_cairo_rectangle (cr, &area);
743 gdk_cairo_set_source_rgba (cr, &cellview->priv->background);
747 /* set cell data (if available) */
748 if (cellview->priv->displayed_row)
749 gtk_cell_view_set_cell_data (cellview);
750 else if (cellview->priv->model)
753 if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT)
754 state = GTK_CELL_RENDERER_PRELIT;
755 else if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_INSENSITIVE)
756 state = GTK_CELL_RENDERER_INSENSITIVE;
760 /* Render the cells */
761 gtk_cell_area_render (cellview->priv->area, cellview->priv->context,
762 widget, cr, &area, &area, state, FALSE);
768 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
773 g_return_if_fail (cell_view->priv->displayed_row != NULL);
775 path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
779 gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
780 gtk_tree_path_free (path);
782 gtk_cell_area_apply_attributes (cell_view->priv->area,
783 cell_view->priv->model,
784 &iter, FALSE, FALSE);
786 if (cell_view->priv->draw_sensitive)
789 gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view->priv->area));
791 for (l = cells; l; l = l->next)
793 GObject *renderer = l->data;
795 g_object_set (renderer, "sensitive", TRUE, NULL);
801 /* GtkCellLayout implementation */
803 gtk_cell_view_cell_layout_get_area (GtkCellLayout *layout)
805 GtkCellView *cellview = GTK_CELL_VIEW (layout);
807 return cellview->priv->area;
810 /* GtkBuildable implementation */
812 gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
815 const gchar *tagname,
816 GMarkupParser *parser,
819 if (parent_buildable_iface->custom_tag_start &&
820 parent_buildable_iface->custom_tag_start (buildable, builder, child,
821 tagname, parser, data))
824 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
825 tagname, parser, data);
829 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
832 const gchar *tagname,
835 if (strcmp (tagname, "attributes") == 0 || strcmp (tagname, "cell-packing") == 0)
836 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
838 else if (parent_buildable_iface->custom_tag_end)
839 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
844 context_size_changed_cb (GtkCellAreaContext *context,
848 if (!strcmp (pspec->name, "minimum-width") ||
849 !strcmp (pspec->name, "natural-width") ||
850 !strcmp (pspec->name, "minimum-height") ||
851 !strcmp (pspec->name, "natural-height"))
852 gtk_widget_queue_resize (view);
856 row_changed_cb (GtkTreeModel *model,
861 GtkTreePath *row_path;
863 if (view->priv->displayed_row)
866 gtk_tree_row_reference_get_path (view->priv->displayed_row);
870 /* Resize everything in our context if our row changed */
871 if (gtk_tree_path_compare (row_path, path) == 0)
872 gtk_cell_area_context_reset (view->priv->context);
874 gtk_tree_path_free (row_path);
882 * Creates a new #GtkCellView widget.
884 * Return value: A newly created #GtkCellView widget.
889 gtk_cell_view_new (void)
891 GtkCellView *cellview;
893 cellview = g_object_new (gtk_cell_view_get_type (), NULL);
895 return GTK_WIDGET (cellview);
900 * gtk_cell_view_new_with_context:
901 * @area: the #GtkCellArea to layout cells
902 * @context: the #GtkCellAreaContext in which to calculate cell geometry
904 * Creates a new #GtkCellView widget with a specific #GtkCellArea
905 * to layout cells and a specific #GtkCellAreaContext.
907 * Specifying the same context for a handfull of cells lets
908 * the underlying area synchronize the geometry for those cells,
909 * in this way alignments with cellviews for other rows are
912 * Return value: A newly created #GtkCellView widget.
917 gtk_cell_view_new_with_context (GtkCellArea *area,
918 GtkCellAreaContext *context)
920 g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
921 g_return_val_if_fail (context == NULL || GTK_IS_CELL_AREA_CONTEXT (context), NULL);
923 return (GtkWidget *)g_object_new (GTK_TYPE_CELL_VIEW,
925 "cell-area-context", context,
930 * gtk_cell_view_new_with_text:
931 * @text: the text to display in the cell view
933 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
934 * to it, and makes its show @text.
936 * Return value: A newly created #GtkCellView widget.
941 gtk_cell_view_new_with_text (const gchar *text)
943 GtkCellView *cellview;
944 GtkCellRenderer *renderer;
945 GValue value = {0, };
947 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
949 renderer = gtk_cell_renderer_text_new ();
950 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
953 g_value_init (&value, G_TYPE_STRING);
954 g_value_set_string (&value, text);
955 gtk_cell_view_set_value (cellview, renderer, "text", &value);
956 g_value_unset (&value);
958 return GTK_WIDGET (cellview);
962 * gtk_cell_view_new_with_markup:
963 * @markup: the text to display in the cell view
965 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
966 * to it, and makes it show @markup. The text can be
967 * marked up with the <link linkend="PangoMarkupFormat">Pango text
968 * markup language</link>.
970 * Return value: A newly created #GtkCellView widget.
975 gtk_cell_view_new_with_markup (const gchar *markup)
977 GtkCellView *cellview;
978 GtkCellRenderer *renderer;
979 GValue value = {0, };
981 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
983 renderer = gtk_cell_renderer_text_new ();
984 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
987 g_value_init (&value, G_TYPE_STRING);
988 g_value_set_string (&value, markup);
989 gtk_cell_view_set_value (cellview, renderer, "markup", &value);
990 g_value_unset (&value);
992 return GTK_WIDGET (cellview);
996 * gtk_cell_view_new_with_pixbuf:
997 * @pixbuf: the image to display in the cell view
999 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
1000 * to it, and makes its show @pixbuf.
1002 * Return value: A newly created #GtkCellView widget.
1007 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
1009 GtkCellView *cellview;
1010 GtkCellRenderer *renderer;
1011 GValue value = {0, };
1013 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
1015 renderer = gtk_cell_renderer_pixbuf_new ();
1016 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
1019 g_value_init (&value, GDK_TYPE_PIXBUF);
1020 g_value_set_object (&value, pixbuf);
1021 gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
1022 g_value_unset (&value);
1024 return GTK_WIDGET (cellview);
1028 * gtk_cell_view_set_value:
1029 * @cell_view: a #GtkCellView widget
1030 * @renderer: one of the renderers of @cell_view
1031 * @property: the name of the property of @renderer to set
1032 * @value: the new value to set the property to
1034 * Sets a property of a cell renderer of @cell_view, and
1035 * makes sure the display of @cell_view is updated.
1040 gtk_cell_view_set_value (GtkCellView *cell_view,
1041 GtkCellRenderer *renderer,
1045 g_object_set_property (G_OBJECT (renderer), property, value);
1047 /* force resize and redraw */
1048 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1049 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1053 * gtk_cell_view_set_model:
1054 * @cell_view: a #GtkCellView
1055 * @model: (allow-none): a #GtkTreeModel
1057 * Sets the model for @cell_view. If @cell_view already has a model
1058 * set, it will remove it before setting the new model. If @model is
1059 * %NULL, then it will unset the old model.
1064 gtk_cell_view_set_model (GtkCellView *cell_view,
1065 GtkTreeModel *model)
1067 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1068 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1070 if (cell_view->priv->model)
1072 g_signal_handler_disconnect (cell_view->priv->model,
1073 cell_view->priv->row_changed_id);
1074 cell_view->priv->row_changed_id = 0;
1076 if (cell_view->priv->displayed_row)
1077 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1078 cell_view->priv->displayed_row = NULL;
1080 g_object_unref (cell_view->priv->model);
1081 cell_view->priv->model = NULL;
1084 cell_view->priv->model = model;
1086 if (cell_view->priv->model)
1088 g_object_ref (cell_view->priv->model);
1090 cell_view->priv->row_changed_id =
1091 g_signal_connect (cell_view->priv->model, "row-changed",
1092 G_CALLBACK (row_changed_cb), cell_view);
1097 * gtk_cell_view_get_model:
1098 * @cell_view: a #GtkCellView
1100 * Returns the model for @cell_view. If no model is used %NULL is
1103 * Returns: (transfer none): a #GtkTreeModel used or %NULL
1108 gtk_cell_view_get_model (GtkCellView *cell_view)
1110 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1112 return cell_view->priv->model;
1116 * gtk_cell_view_set_displayed_row:
1117 * @cell_view: a #GtkCellView
1118 * @path: (allow-none): a #GtkTreePath or %NULL to unset.
1120 * Sets the row of the model that is currently displayed
1121 * by the #GtkCellView. If the path is unset, then the
1122 * contents of the cellview "stick" at their last value;
1123 * this is not normally a desired result, but may be
1124 * a needed intermediate state if say, the model for
1125 * the #GtkCellView becomes temporarily empty.
1130 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
1133 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1134 g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
1136 if (cell_view->priv->displayed_row)
1137 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1141 cell_view->priv->displayed_row =
1142 gtk_tree_row_reference_new (cell_view->priv->model, path);
1145 cell_view->priv->displayed_row = NULL;
1147 /* force resize and redraw */
1148 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1149 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1153 * gtk_cell_view_get_displayed_row:
1154 * @cell_view: a #GtkCellView
1156 * Returns a #GtkTreePath referring to the currently
1157 * displayed row. If no row is currently displayed,
1158 * %NULL is returned.
1160 * Returns: the currently displayed row or %NULL
1165 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
1167 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1169 if (!cell_view->priv->displayed_row)
1172 return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
1176 * gtk_cell_view_get_size_of_row:
1177 * @cell_view: a #GtkCellView
1178 * @path: a #GtkTreePath
1179 * @requisition: return location for the size
1181 * Sets @requisition to the size needed by @cell_view to display
1182 * the model row pointed to by @path.
1184 * Return value: %TRUE
1188 * Deprecated: 3.0: Combo box formerly used this to calculate the
1189 * sizes for cellviews, now you can achieve this by either using
1190 * the #GtkCellView:fit-model property or by setting the currently
1191 * displayed row of the #GtkCellView and using gtk_widget_get_preferred_size().
1194 gtk_cell_view_get_size_of_row (GtkCellView *cell_view,
1196 GtkRequisition *requisition)
1198 GtkTreeRowReference *tmp;
1201 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1202 g_return_val_if_fail (path != NULL, FALSE);
1204 tmp = cell_view->priv->displayed_row;
1205 cell_view->priv->displayed_row =
1206 gtk_tree_row_reference_new (cell_view->priv->model, path);
1208 gtk_widget_get_preferred_width (GTK_WIDGET (cell_view), &req.width, NULL);
1209 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (cell_view), req.width, &req.height, NULL);
1211 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1212 cell_view->priv->displayed_row = tmp;
1221 * gtk_cell_view_set_background_color:
1222 * @cell_view: a #GtkCellView
1223 * @color: the new background color
1225 * Sets the background color of @view.
1230 gtk_cell_view_set_background_color (GtkCellView *cell_view,
1231 const GdkColor *color)
1233 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1237 if (!cell_view->priv->background_set)
1239 cell_view->priv->background_set = TRUE;
1240 g_object_notify (G_OBJECT (cell_view), "background-set");
1243 cell_view->priv->background.red = color->red / 65535.;
1244 cell_view->priv->background.green = color->green / 65535.;
1245 cell_view->priv->background.blue = color->blue / 65535.;
1246 cell_view->priv->background.alpha = 1;
1250 if (cell_view->priv->background_set)
1252 cell_view->priv->background_set = FALSE;
1253 g_object_notify (G_OBJECT (cell_view), "background-set");
1257 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1261 * gtk_cell_view_set_background_rgba:
1262 * @cell_view: a #GtkCellView
1263 * @rgba: the new background color
1265 * Sets the background color of @cell_view.
1270 gtk_cell_view_set_background_rgba (GtkCellView *cell_view,
1271 const GdkRGBA *rgba)
1273 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1277 if (!cell_view->priv->background_set)
1279 cell_view->priv->background_set = TRUE;
1280 g_object_notify (G_OBJECT (cell_view), "background-set");
1283 cell_view->priv->background = *rgba;
1287 if (cell_view->priv->background_set)
1289 cell_view->priv->background_set = FALSE;
1290 g_object_notify (G_OBJECT (cell_view), "background-set");
1294 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1298 * gtk_cell_view_get_draw_sensitive:
1299 * @cell_view: a #GtkCellView
1301 * Gets whether @cell_view is configured to draw all of it's
1302 * cells in a sensitive state.
1304 * Return value: whether @cell_view draws all of it's
1305 * cells in a sensitive state
1310 gtk_cell_view_get_draw_sensitive (GtkCellView *cell_view)
1312 GtkCellViewPrivate *priv;
1314 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1316 priv = cell_view->priv;
1318 return priv->draw_sensitive;
1322 * gtk_cell_view_set_draw_sensitive:
1323 * @cell_view: a #GtkCellView
1324 * @draw_sensitive: whether to draw all cells in a sensitive state.
1326 * Sets whether @cell_view should draw all of it's
1327 * cells in a sensitive state, this is used by #GtkTreeMenu
1328 * to ensure that rows with insensitive cells that contain
1329 * children appear sensitive in the parent menu item.
1334 gtk_cell_view_set_draw_sensitive (GtkCellView *cell_view,
1335 gboolean draw_sensitive)
1337 GtkCellViewPrivate *priv;
1339 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1341 priv = cell_view->priv;
1343 if (priv->draw_sensitive != draw_sensitive)
1345 priv->draw_sensitive = draw_sensitive;
1347 g_object_notify (G_OBJECT (cell_view), "draw-sensitive");
1352 * gtk_cell_view_get_fit_model:
1353 * @cell_view: a #GtkCellView
1355 * Gets whether @cell_view is configured to request space
1356 * to fit the entire #GtkTreeModel.
1358 * Return value: whether @cell_view requests space to fit
1359 * the entire #GtkTreeModel.
1364 gtk_cell_view_get_fit_model (GtkCellView *cell_view)
1366 GtkCellViewPrivate *priv;
1368 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1370 priv = cell_view->priv;
1372 return priv->fit_model;
1376 * gtk_cell_view_set_fit_model:
1377 * @cell_view: a #GtkCellView
1378 * @fit_model: whether @cell_view should request space for the whole model.
1380 * Sets whether @cell_view should request space to fit the entire #GtkTreeModel.
1382 * This is used by #GtkComboBox to ensure that the cell view displayed on
1383 * the combo box's button always gets enough space and does not resize
1384 * when selection changes.
1389 gtk_cell_view_set_fit_model (GtkCellView *cell_view,
1392 GtkCellViewPrivate *priv;
1394 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1396 priv = cell_view->priv;
1398 if (priv->fit_model != fit_model)
1400 priv->fit_model = fit_model;
1402 gtk_cell_area_context_reset (cell_view->priv->context);
1404 g_object_notify (G_OBJECT (cell_view), "fit-model");