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.
252 g_object_class_install_property (gobject_class,
253 PROP_CELL_AREA_CONTEXT,
254 g_param_spec_object ("cell-area-context",
255 P_("Cell Area Context"),
256 P_("The GtkCellAreaContext used to "
257 "compute the geometry of the cell view"),
258 GTK_TYPE_CELL_AREA_CONTEXT,
259 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
262 * GtkCellView:draw-sensitive
264 * Whether all cells should be draw as sensitive for this view regardless
265 * of the actual cell properties (used to make menus with submenus appear
266 * sensitive when the items in submenus might be insensitive).
270 g_object_class_install_property (gobject_class,
272 g_param_spec_boolean ("draw-sensitive",
273 P_("Draw Sensitive"),
274 P_("Whether to force cells to be drawn in a "
277 GTK_PARAM_READWRITE));
280 * GtkCellView:fit-model
282 * Whether the view should request enough space to always fit
283 * the size of every row in the model (used by the combo box to
284 * ensure the combo box size doesnt change when different items
289 g_object_class_install_property (gobject_class,
291 g_param_spec_boolean ("fit-model",
293 P_("Whether to request enough space for "
294 "every row in the model"),
296 GTK_PARAM_READWRITE));
299 #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))
301 ADD_SET_PROP ("background-set", PROP_BACKGROUND_SET,
302 P_("Background set"),
303 P_("Whether this tag affects the background color"));
305 g_type_class_add_private (gobject_class, sizeof (GtkCellViewPrivate));
309 gtk_cell_view_buildable_init (GtkBuildableIface *iface)
311 parent_buildable_iface = g_type_interface_peek_parent (iface);
312 iface->add_child = _gtk_cell_layout_buildable_add_child;
313 iface->custom_tag_start = gtk_cell_view_buildable_custom_tag_start;
314 iface->custom_tag_end = gtk_cell_view_buildable_custom_tag_end;
318 gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface)
320 iface->get_area = gtk_cell_view_cell_layout_get_area;
324 gtk_cell_view_constructor (GType type,
325 guint n_construct_properties,
326 GObjectConstructParam *construct_properties)
330 GtkCellViewPrivate *priv;
332 object = G_OBJECT_CLASS (gtk_cell_view_parent_class)->constructor
333 (type, n_construct_properties, construct_properties);
335 view = GTK_CELL_VIEW (object);
340 GtkCellArea *area = gtk_cell_area_box_new ();
342 priv->area = g_object_ref_sink (area);
346 priv->context = gtk_cell_area_create_context (priv->area);
348 priv->size_changed_id =
349 g_signal_connect (priv->context, "notify",
350 G_CALLBACK (context_size_changed_cb), view);
356 gtk_cell_view_get_property (GObject *object,
361 GtkCellView *view = GTK_CELL_VIEW (object);
365 case PROP_ORIENTATION:
366 g_value_set_enum (value, view->priv->orientation);
368 case PROP_BACKGROUND_GDK:
372 color.red = (guint) (view->priv->background.red * 65535);
373 color.green = (guint) (view->priv->background.green * 65535);
374 color.blue = (guint) (view->priv->background.blue * 65535);
377 g_value_set_boxed (value, &color);
380 case PROP_BACKGROUND_RGBA:
381 g_value_set_boxed (value, &view->priv->background);
383 case PROP_BACKGROUND_SET:
384 g_value_set_boolean (value, view->priv->background_set);
387 g_value_set_object (value, view->priv->model);
390 g_value_set_object (value, view->priv->area);
392 case PROP_CELL_AREA_CONTEXT:
393 g_value_set_object (value, view->priv->context);
395 case PROP_DRAW_SENSITIVE:
396 g_value_set_boolean (value, view->priv->draw_sensitive);
399 g_value_set_boolean (value, view->priv->fit_model);
402 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
408 gtk_cell_view_set_property (GObject *object,
413 GtkCellView *view = GTK_CELL_VIEW (object);
415 GtkCellAreaContext *context;
419 case PROP_ORIENTATION:
420 view->priv->orientation = g_value_get_enum (value);
421 if (view->priv->context)
422 gtk_cell_area_context_flush (view->priv->context);
424 case PROP_BACKGROUND:
428 if (!g_value_get_string (value))
429 gtk_cell_view_set_background_color (view, NULL);
430 else if (gdk_color_parse (g_value_get_string (value), &color))
431 gtk_cell_view_set_background_color (view, &color);
433 g_warning ("Don't know color `%s'", g_value_get_string (value));
435 g_object_notify (object, "background-gdk");
438 case PROP_BACKGROUND_GDK:
439 gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
441 case PROP_BACKGROUND_RGBA:
442 gtk_cell_view_set_background_rgba (view, g_value_get_boxed (value));
444 case PROP_BACKGROUND_SET:
445 view->priv->background_set = g_value_get_boolean (value);
448 gtk_cell_view_set_model (view, g_value_get_object (value));
451 /* Construct-only, can only be assigned once */
452 area = g_value_get_object (value);
455 view->priv->area = g_object_ref_sink (area);
457 case PROP_CELL_AREA_CONTEXT:
458 /* Construct-only, can only be assigned once */
459 context = g_value_get_object (value);
462 view->priv->context = g_object_ref (context);
465 case PROP_DRAW_SENSITIVE:
466 gtk_cell_view_set_draw_sensitive (view, g_value_get_boolean (value));
470 gtk_cell_view_set_fit_model (view, g_value_get_boolean (value));
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
480 gtk_cell_view_init (GtkCellView *cellview)
482 GtkCellViewPrivate *priv;
484 cellview->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellview,
487 priv = cellview->priv;
489 gtk_widget_set_has_window (GTK_WIDGET (cellview), FALSE);
491 priv->orientation = GTK_ORIENTATION_HORIZONTAL;
495 gtk_cell_view_finalize (GObject *object)
497 GtkCellView *cellview = GTK_CELL_VIEW (object);
499 if (cellview->priv->displayed_row)
500 gtk_tree_row_reference_free (cellview->priv->displayed_row);
502 G_OBJECT_CLASS (gtk_cell_view_parent_class)->finalize (object);
506 gtk_cell_view_dispose (GObject *object)
508 GtkCellView *cellview = GTK_CELL_VIEW (object);
510 gtk_cell_view_set_model (cellview, NULL);
512 if (cellview->priv->area)
514 g_object_unref (cellview->priv->area);
515 cellview->priv->area = NULL;
518 if (cellview->priv->context)
520 g_signal_handler_disconnect (cellview->priv->context, cellview->priv->size_changed_id);
522 g_object_unref (cellview->priv->context);
523 cellview->priv->context = NULL;
524 cellview->priv->size_changed_id = 0;
527 G_OBJECT_CLASS (gtk_cell_view_parent_class)->dispose (object);
531 gtk_cell_view_size_allocate (GtkWidget *widget,
532 GtkAllocation *allocation)
534 GtkCellView *cellview = GTK_CELL_VIEW (widget);
535 GtkCellViewPrivate *priv = cellview->priv;
536 gint alloc_width, alloc_height;
538 gtk_widget_set_allocation (widget, allocation);
540 gtk_cell_area_context_get_allocation (priv->context, &alloc_width, &alloc_height);
542 /* The first cell view in context is responsible for allocating the context at allocate time
543 * (or the cellview has its own context and is not grouped with any other cell views)
545 * If the cellview is in "fit model" mode, we assume its not in context and needs to
546 * allocate every time.
550 gtk_cell_area_context_allocate_width (priv->context, allocation->width);
551 gtk_cell_area_context_allocate_height (priv->context, allocation->height);
553 else if (alloc_width <= 0 && priv->orientation == GTK_ORIENTATION_HORIZONTAL)
554 gtk_cell_area_context_allocate_width (priv->context, allocation->width);
555 else if (alloc_height <= 0 && priv->orientation == GTK_ORIENTATION_VERTICAL)
556 gtk_cell_area_context_allocate_height (priv->context, allocation->height);
560 gtk_cell_view_request_model (GtkCellView *cellview,
562 GtkOrientation orientation,
567 GtkCellViewPrivate *priv = cellview->priv;
571 valid = gtk_tree_model_iter_children (priv->model, &iter, parent);
576 gtk_cell_area_apply_attributes (priv->area, priv->model, &iter, FALSE, FALSE);
578 if (orientation == GTK_ORIENTATION_HORIZONTAL)
581 gtk_cell_area_get_preferred_width (priv->area, priv->context,
582 GTK_WIDGET (cellview), &min, &nat);
584 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context,
585 GTK_WIDGET (cellview), for_size, &min, &nat);
590 gtk_cell_area_get_preferred_height (priv->area, priv->context,
591 GTK_WIDGET (cellview), &min, &nat);
593 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context,
594 GTK_WIDGET (cellview), for_size, &min, &nat);
597 *minimum_size = MAX (min, *minimum_size);
598 *natural_size = MAX (nat, *natural_size);
600 /* Recurse into children when they exist */
601 gtk_cell_view_request_model (cellview, &iter, orientation, for_size, minimum_size, natural_size);
603 valid = gtk_tree_model_iter_next (priv->model, &iter);
608 gtk_cell_view_get_preferred_width (GtkWidget *widget,
612 GtkCellView *cellview = GTK_CELL_VIEW (widget);
613 GtkCellViewPrivate *priv = cellview->priv;
615 g_signal_handler_block (priv->context, priv->size_changed_id);
619 gint min = 0, nat = 0;
620 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, -1, &min, &nat);
624 if (cellview->priv->displayed_row)
625 gtk_cell_view_set_cell_data (cellview);
627 gtk_cell_area_get_preferred_width (priv->area, priv->context, widget, NULL, NULL);
630 gtk_cell_area_context_sum_preferred_width (priv->context);
631 gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
633 g_signal_handler_unblock (priv->context, priv->size_changed_id);
637 gtk_cell_view_get_preferred_height (GtkWidget *widget,
641 GtkCellView *cellview = GTK_CELL_VIEW (widget);
642 GtkCellViewPrivate *priv = cellview->priv;
644 g_signal_handler_block (priv->context, priv->size_changed_id);
648 gint min = 0, nat = 0;
649 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, -1, &min, &nat);
653 if (cellview->priv->displayed_row)
654 gtk_cell_view_set_cell_data (cellview);
656 gtk_cell_area_get_preferred_height (priv->area, priv->context, widget, NULL, NULL);
659 gtk_cell_area_context_sum_preferred_height (priv->context);
660 gtk_cell_area_context_get_preferred_height (priv->context, minimum_size, natural_size);
662 g_signal_handler_unblock (priv->context, priv->size_changed_id);
666 gtk_cell_view_get_preferred_width_for_height (GtkWidget *widget,
671 GtkCellView *cellview = GTK_CELL_VIEW (widget);
672 GtkCellViewPrivate *priv = cellview->priv;
676 gint min = 0, nat = 0;
677 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_HORIZONTAL, for_size, &min, &nat);
684 if (cellview->priv->displayed_row)
685 gtk_cell_view_set_cell_data (cellview);
687 gtk_cell_area_get_preferred_width_for_height (priv->area, priv->context, widget,
688 for_size, minimum_size, natural_size);
693 gtk_cell_view_get_preferred_height_for_width (GtkWidget *widget,
698 GtkCellView *cellview = GTK_CELL_VIEW (widget);
699 GtkCellViewPrivate *priv = cellview->priv;
703 gint min = 0, nat = 0;
704 gtk_cell_view_request_model (cellview, NULL, GTK_ORIENTATION_VERTICAL, for_size, &min, &nat);
711 if (cellview->priv->displayed_row)
712 gtk_cell_view_set_cell_data (cellview);
714 gtk_cell_area_get_preferred_height_for_width (priv->area, priv->context, widget,
715 for_size, minimum_size, natural_size);
720 gtk_cell_view_draw (GtkWidget *widget,
723 GtkCellView *cellview;
725 GtkCellRendererState state;
727 cellview = GTK_CELL_VIEW (widget);
732 area.width = gtk_widget_get_allocated_width (widget);
733 area.height = gtk_widget_get_allocated_height (widget);
735 /* "blank" background */
736 if (cellview->priv->background_set)
738 gdk_cairo_rectangle (cr, &area);
739 gdk_cairo_set_source_rgba (cr, &cellview->priv->background);
743 /* set cell data (if available) */
744 if (cellview->priv->displayed_row)
745 gtk_cell_view_set_cell_data (cellview);
746 else if (cellview->priv->model)
749 if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT)
750 state = GTK_CELL_RENDERER_PRELIT;
751 else if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_INSENSITIVE)
752 state = GTK_CELL_RENDERER_INSENSITIVE;
756 /* Render the cells */
757 gtk_cell_area_render (cellview->priv->area, cellview->priv->context,
758 widget, cr, &area, &area, state, FALSE);
764 gtk_cell_view_set_cell_data (GtkCellView *cell_view)
769 g_return_if_fail (cell_view->priv->displayed_row != NULL);
771 path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
775 gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
776 gtk_tree_path_free (path);
778 gtk_cell_area_apply_attributes (cell_view->priv->area,
779 cell_view->priv->model,
780 &iter, FALSE, FALSE);
782 if (cell_view->priv->draw_sensitive)
785 gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_view->priv->area));
787 for (l = cells; l; l = l->next)
789 GObject *renderer = l->data;
791 g_object_set (renderer, "sensitive", TRUE, NULL);
797 /* GtkCellLayout implementation */
799 gtk_cell_view_cell_layout_get_area (GtkCellLayout *layout)
801 GtkCellView *cellview = GTK_CELL_VIEW (layout);
803 return cellview->priv->area;
806 /* GtkBuildable implementation */
808 gtk_cell_view_buildable_custom_tag_start (GtkBuildable *buildable,
811 const gchar *tagname,
812 GMarkupParser *parser,
815 if (parent_buildable_iface->custom_tag_start &&
816 parent_buildable_iface->custom_tag_start (buildable, builder, child,
817 tagname, parser, data))
820 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
821 tagname, parser, data);
825 gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
828 const gchar *tagname,
831 if (strcmp (tagname, "attributes") == 0 || strcmp (tagname, "cell-packing") == 0)
832 _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
834 else if (parent_buildable_iface->custom_tag_end)
835 parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
840 context_size_changed_cb (GtkCellAreaContext *context,
844 if (!strcmp (pspec->name, "minimum-width") ||
845 !strcmp (pspec->name, "natural-width") ||
846 !strcmp (pspec->name, "minimum-height") ||
847 !strcmp (pspec->name, "natural-height"))
848 gtk_widget_queue_resize (view);
852 row_changed_cb (GtkTreeModel *model,
857 GtkTreePath *row_path;
859 if (view->priv->displayed_row)
862 gtk_tree_row_reference_get_path (view->priv->displayed_row);
866 /* Resize everything in our context if our row changed */
867 if (gtk_tree_path_compare (row_path, path) == 0)
868 gtk_cell_area_context_flush (view->priv->context);
870 gtk_tree_path_free (row_path);
878 * Creates a new #GtkCellView widget.
880 * Return value: A newly created #GtkCellView widget.
885 gtk_cell_view_new (void)
887 GtkCellView *cellview;
889 cellview = g_object_new (gtk_cell_view_get_type (), NULL);
891 return GTK_WIDGET (cellview);
896 * gtk_cell_view_new_with_context:
897 * @area: the #GtkCellArea to layout cells
898 * @context: the #GtkCellAreaContext in which to calculate cell geometry
900 * Creates a new #GtkCellView widget with a specific #GtkCellArea
901 * to layout cells and a specific #GtkCellAreaContext.
903 * Specifying the same context for a handfull of cells lets
904 * the underlying area synchronize the geometry for those cells,
905 * in this way alignments with cellviews for other rows are
908 * Return value: A newly created #GtkCellView widget.
913 gtk_cell_view_new_with_context (GtkCellArea *area,
914 GtkCellAreaContext *context)
916 g_return_val_if_fail (GTK_IS_CELL_AREA (area), NULL);
917 g_return_val_if_fail (context == NULL || GTK_IS_CELL_AREA_CONTEXT (context), NULL);
919 return (GtkWidget *)g_object_new (GTK_TYPE_CELL_VIEW,
921 "cell-area-context", context,
926 * gtk_cell_view_new_with_text:
927 * @text: the text to display in the cell view
929 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
930 * to it, and makes its show @text.
932 * Return value: A newly created #GtkCellView widget.
937 gtk_cell_view_new_with_text (const gchar *text)
939 GtkCellView *cellview;
940 GtkCellRenderer *renderer;
941 GValue value = {0, };
943 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
945 renderer = gtk_cell_renderer_text_new ();
946 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
949 g_value_init (&value, G_TYPE_STRING);
950 g_value_set_string (&value, text);
951 gtk_cell_view_set_value (cellview, renderer, "text", &value);
952 g_value_unset (&value);
954 return GTK_WIDGET (cellview);
958 * gtk_cell_view_new_with_markup:
959 * @markup: the text to display in the cell view
961 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText
962 * to it, and makes it show @markup. The text can be
963 * marked up with the <link linkend="PangoMarkupFormat">Pango text
964 * markup language</link>.
966 * Return value: A newly created #GtkCellView widget.
971 gtk_cell_view_new_with_markup (const gchar *markup)
973 GtkCellView *cellview;
974 GtkCellRenderer *renderer;
975 GValue value = {0, };
977 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
979 renderer = gtk_cell_renderer_text_new ();
980 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
983 g_value_init (&value, G_TYPE_STRING);
984 g_value_set_string (&value, markup);
985 gtk_cell_view_set_value (cellview, renderer, "markup", &value);
986 g_value_unset (&value);
988 return GTK_WIDGET (cellview);
992 * gtk_cell_view_new_with_pixbuf:
993 * @pixbuf: the image to display in the cell view
995 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
996 * to it, and makes its show @pixbuf.
998 * Return value: A newly created #GtkCellView widget.
1003 gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
1005 GtkCellView *cellview;
1006 GtkCellRenderer *renderer;
1007 GValue value = {0, };
1009 cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
1011 renderer = gtk_cell_renderer_pixbuf_new ();
1012 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
1015 g_value_init (&value, GDK_TYPE_PIXBUF);
1016 g_value_set_object (&value, pixbuf);
1017 gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
1018 g_value_unset (&value);
1020 return GTK_WIDGET (cellview);
1024 * gtk_cell_view_set_value:
1025 * @cell_view: a #GtkCellView widget
1026 * @renderer: one of the renderers of @cell_view
1027 * @property: the name of the property of @renderer to set
1028 * @value: the new value to set the property to
1030 * Sets a property of a cell renderer of @cell_view, and
1031 * makes sure the display of @cell_view is updated.
1036 gtk_cell_view_set_value (GtkCellView *cell_view,
1037 GtkCellRenderer *renderer,
1041 g_object_set_property (G_OBJECT (renderer), property, value);
1043 /* force resize and redraw */
1044 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1045 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1049 * gtk_cell_view_set_model:
1050 * @cell_view: a #GtkCellView
1051 * @model: (allow-none): a #GtkTreeModel
1053 * Sets the model for @cell_view. If @cell_view already has a model
1054 * set, it will remove it before setting the new model. If @model is
1055 * %NULL, then it will unset the old model.
1060 gtk_cell_view_set_model (GtkCellView *cell_view,
1061 GtkTreeModel *model)
1063 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1064 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
1066 if (cell_view->priv->model)
1068 g_signal_handler_disconnect (cell_view->priv->model,
1069 cell_view->priv->row_changed_id);
1070 cell_view->priv->row_changed_id = 0;
1072 if (cell_view->priv->displayed_row)
1073 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1074 cell_view->priv->displayed_row = NULL;
1076 g_object_unref (cell_view->priv->model);
1077 cell_view->priv->model = NULL;
1080 cell_view->priv->model = model;
1082 if (cell_view->priv->model)
1084 g_object_ref (cell_view->priv->model);
1086 cell_view->priv->row_changed_id =
1087 g_signal_connect (cell_view->priv->model, "row-changed",
1088 G_CALLBACK (row_changed_cb), cell_view);
1093 * gtk_cell_view_get_model:
1094 * @cell_view: a #GtkCellView
1096 * Returns the model for @cell_view. If no model is used %NULL is
1099 * Returns: (transfer none): a #GtkTreeModel used or %NULL
1104 gtk_cell_view_get_model (GtkCellView *cell_view)
1106 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1108 return cell_view->priv->model;
1112 * gtk_cell_view_set_displayed_row:
1113 * @cell_view: a #GtkCellView
1114 * @path: (allow-none): a #GtkTreePath or %NULL to unset.
1116 * Sets the row of the model that is currently displayed
1117 * by the #GtkCellView. If the path is unset, then the
1118 * contents of the cellview "stick" at their last value;
1119 * this is not normally a desired result, but may be
1120 * a needed intermediate state if say, the model for
1121 * the #GtkCellView becomes temporarily empty.
1126 gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
1129 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1130 g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
1132 if (cell_view->priv->displayed_row)
1133 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1137 cell_view->priv->displayed_row =
1138 gtk_tree_row_reference_new (cell_view->priv->model, path);
1141 cell_view->priv->displayed_row = NULL;
1143 /* force resize and redraw */
1144 gtk_widget_queue_resize (GTK_WIDGET (cell_view));
1145 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1149 * gtk_cell_view_get_displayed_row:
1150 * @cell_view: a #GtkCellView
1152 * Returns a #GtkTreePath referring to the currently
1153 * displayed row. If no row is currently displayed,
1154 * %NULL is returned.
1156 * Returns: the currently displayed row or %NULL
1161 gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
1163 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
1165 if (!cell_view->priv->displayed_row)
1168 return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
1172 * gtk_cell_view_get_size_of_row:
1173 * @cell_view: a #GtkCellView
1174 * @path: a #GtkTreePath
1175 * @requisition: return location for the size
1177 * Sets @requisition to the size needed by @cell_view to display
1178 * the model row pointed to by @path.
1180 * Return value: %TRUE
1184 * Deprecated: 3.0: Combo box formerly used this to calculate the
1185 * sizes for cellviews, now you can achieve this by either using
1186 * the #GtkCellView:fit-model property or by setting the currently
1187 * displayed row of the #GtkCellView and using gtk_widget_get_preferred_size().
1190 gtk_cell_view_get_size_of_row (GtkCellView *cell_view,
1192 GtkRequisition *requisition)
1194 GtkTreeRowReference *tmp;
1197 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1198 g_return_val_if_fail (path != NULL, FALSE);
1200 tmp = cell_view->priv->displayed_row;
1201 cell_view->priv->displayed_row =
1202 gtk_tree_row_reference_new (cell_view->priv->model, path);
1204 gtk_widget_get_preferred_width (GTK_WIDGET (cell_view), &req.width, NULL);
1205 gtk_widget_get_preferred_height_for_width (GTK_WIDGET (cell_view), req.width, &req.height, NULL);
1207 gtk_tree_row_reference_free (cell_view->priv->displayed_row);
1208 cell_view->priv->displayed_row = tmp;
1217 * gtk_cell_view_set_background_color:
1218 * @cell_view: a #GtkCellView
1219 * @color: the new background color
1221 * Sets the background color of @view.
1226 gtk_cell_view_set_background_color (GtkCellView *cell_view,
1227 const GdkColor *color)
1229 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1233 if (!cell_view->priv->background_set)
1235 cell_view->priv->background_set = TRUE;
1236 g_object_notify (G_OBJECT (cell_view), "background-set");
1239 cell_view->priv->background.red = color->red / 65535.;
1240 cell_view->priv->background.green = color->green / 65535.;
1241 cell_view->priv->background.blue = color->blue / 65535.;
1242 cell_view->priv->background.alpha = 1;
1246 if (cell_view->priv->background_set)
1248 cell_view->priv->background_set = FALSE;
1249 g_object_notify (G_OBJECT (cell_view), "background-set");
1253 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1257 * gtk_cell_view_set_background_rgba:
1258 * @cell_view: a #GtkCellView
1259 * @rgba: the new background color
1261 * Sets the background color of @cell_view.
1266 gtk_cell_view_set_background_rgba (GtkCellView *cell_view,
1267 const GdkRGBA *rgba)
1269 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1273 if (!cell_view->priv->background_set)
1275 cell_view->priv->background_set = TRUE;
1276 g_object_notify (G_OBJECT (cell_view), "background-set");
1279 cell_view->priv->background = *rgba;
1283 if (cell_view->priv->background_set)
1285 cell_view->priv->background_set = FALSE;
1286 g_object_notify (G_OBJECT (cell_view), "background-set");
1290 gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1294 gtk_cell_view_get_draw_sensitive (GtkCellView *cell_view)
1296 GtkCellViewPrivate *priv;
1298 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1300 priv = cell_view->priv;
1302 return priv->draw_sensitive;
1306 gtk_cell_view_set_draw_sensitive (GtkCellView *cell_view,
1307 gboolean draw_sensitive)
1309 GtkCellViewPrivate *priv;
1311 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1313 priv = cell_view->priv;
1315 if (priv->draw_sensitive != draw_sensitive)
1317 priv->draw_sensitive = draw_sensitive;
1319 g_object_notify (G_OBJECT (cell_view), "draw-sensitive");
1324 gtk_cell_view_get_fit_model (GtkCellView *cell_view)
1326 GtkCellViewPrivate *priv;
1328 g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
1330 priv = cell_view->priv;
1332 return priv->fit_model;
1336 gtk_cell_view_set_fit_model (GtkCellView *cell_view,
1339 GtkCellViewPrivate *priv;
1341 g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1343 priv = cell_view->priv;
1345 if (priv->fit_model != fit_model)
1347 priv->fit_model = fit_model;
1349 gtk_cell_area_context_flush (cell_view->priv->context);
1351 g_object_notify (G_OBJECT (cell_view), "fit-model");