]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcellview.c
Minor documentation improvements
[~andy/gtk] / gtk / gtkcellview.c
index 3c31d9aa63a8154a5d52f8cd19558b2273d44d56..cc2daf6107e33ad098961a13a6dab37bfeba0f33 100644 (file)
@@ -26,7 +26,7 @@
 #include "gtkcellrenderertext.h"
 #include "gtkcellrendererpixbuf.h"
 #include "gtkprivate.h"
-#include "gtksizerequest.h"
+#include "gtkorientableprivate.h"
 #include <gobject/gmarshal.h>
 #include "gtkbuildable.h"
 
  * @Short_description: A widget displaying a single row of a GtkTreeModel
  * @Title: GtkCellView
  *
- * A #GtkCellView displays a single row of a #GtkTreeModel, using
- * cell renderers just like #GtkTreeView. #GtkCellView doesn't support
- * some of the more complex features of #GtkTreeView, like cell editing
- * and drag and drop.
+ * A #GtkCellView displays a single row of a #GtkTreeModel using a #GtkCellArea
+ * and #GtkCellAreaContext. A #GtkCellAreaContext can be provided to the 
+ * #GtkCellView at construction time in order to keep the cellview in context
+ * of a group of cell views, this ensures that the renderers displayed will
+ * be properly aligned with eachother (like the aligned cells in the menus
+ * of #GtkComboBox).
+ *
+ * #GtkCellView is #GtkOrientable in order to decide in which orientation
+ * the underlying #GtkCellAreaContext should be allocated. Taking the #GtkComboBox
+ * menu as an example, cellviews should be oriented horizontally if the menus are
+ * listed top-to-bottom and thus all share the same width but may have separate
+ * individual heights (left-to-right menus should be allocated vertically since
+ * they all share the same height but may have variable widths).
  */
 
 static GObject    *gtk_cell_view_constructor              (GType                  type,
@@ -116,6 +125,8 @@ struct _GtkCellViewPrivate
   GtkCellArea         *area;
   GtkCellAreaContext  *context;
 
+  GtkOrientation       orientation;
+
   GdkRGBA              background;
   gboolean             background_set;
 
@@ -131,6 +142,7 @@ static GtkBuildableIface *parent_buildable_iface;
 enum
 {
   PROP_0,
+  PROP_ORIENTATION,
   PROP_BACKGROUND,
   PROP_BACKGROUND_GDK,
   PROP_BACKGROUND_RGBA,
@@ -146,7 +158,8 @@ G_DEFINE_TYPE_WITH_CODE (GtkCellView, gtk_cell_view, GTK_TYPE_WIDGET,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
                                                gtk_cell_view_cell_layout_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
-                                               gtk_cell_view_buildable_init))
+                                               gtk_cell_view_buildable_init)
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
 
 static void
 gtk_cell_view_class_init (GtkCellViewClass *klass)
@@ -168,6 +181,8 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
   widget_class->get_preferred_height_for_width = gtk_cell_view_get_preferred_height_for_width;
 
   /* properties */
+  g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
+
   g_object_class_install_property (gobject_class,
                                    PROP_BACKGROUND,
                                    g_param_spec_string ("background",
@@ -233,6 +248,15 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
    *
    * The #GtkCellAreaContext used to compute the geometry of the cell view.
    *
+   * A group of cell views can be assigned the same context in order to
+   * ensure the sizes and cell alignments match across all the views with
+   * the same context.
+   *
+   * #GtkComboBox menus uses this to assign the same context to all cell views
+   * in the menu items for a single menu (each submenu creates its own
+   * context since the size of each submenu does not depend on parent
+   * or sibling menus).
+   *
    * since 3.0
    */
    g_object_class_install_property (gobject_class,
@@ -323,15 +347,14 @@ gtk_cell_view_constructor (GType                  type,
 
   if (!priv->area)
     {
-      GtkCellArea *area = gtk_cell_area_box_new ();
-
-      priv->area = g_object_ref_sink (area);
+      priv->area = gtk_cell_area_box_new ();
+      g_object_ref_sink (priv->area);
     }
 
   if (!priv->context)
     priv->context = gtk_cell_area_create_context (priv->area);
 
-  priv->size_changed_id = 
+  priv->size_changed_id =
     g_signal_connect (priv->context, "notify",
                      G_CALLBACK (context_size_changed_cb), view);
 
@@ -348,6 +371,9 @@ gtk_cell_view_get_property (GObject    *object,
 
   switch (param_id)
     {
+    case PROP_ORIENTATION:
+      g_value_set_enum (value, view->priv->orientation);
+      break;
     case PROP_BACKGROUND_GDK:
       {
        GdkColor color;
@@ -394,11 +420,19 @@ gtk_cell_view_set_property (GObject      *object,
                             GParamSpec   *pspec)
 {
   GtkCellView *view = GTK_CELL_VIEW (object);
+  GtkCellViewPrivate *priv = view->priv;
   GtkCellArea *area;
   GtkCellAreaContext *context;
 
   switch (param_id)
     {
+    case PROP_ORIENTATION:
+      priv->orientation = g_value_get_enum (value);
+      if (priv->context)
+        gtk_cell_area_context_reset (priv->context);
+
+      _gtk_orientable_set_style_classes (GTK_ORIENTABLE (object));
+      break;
     case PROP_BACKGROUND:
       {
        GdkColor color;
@@ -428,16 +462,34 @@ gtk_cell_view_set_property (GObject      *object,
     case PROP_CELL_AREA:
       /* Construct-only, can only be assigned once */
       area = g_value_get_object (value);
-      
+
       if (area)
-       view->priv->area = g_object_ref_sink (area);
+        {
+          if (priv->area != NULL)
+            {
+              g_warning ("cell-area has already been set, ignoring construct property");
+              g_object_ref_sink (area);
+              g_object_unref (area);
+            }
+          else
+            priv->area = g_object_ref_sink (area);
+        }
       break;
     case PROP_CELL_AREA_CONTEXT:
       /* Construct-only, can only be assigned once */
       context = g_value_get_object (value);
-      
+
       if (context)
-       view->priv->context = g_object_ref (context);
+        {
+          if (priv->context != NULL)
+            {
+              g_warning ("cell-area-context has already been set, ignoring construct property");
+              g_object_ref_sink (context);
+              g_object_unref (context);
+            }
+          else
+            priv->context = g_object_ref (context);
+        }
       break;
 
     case PROP_DRAW_SENSITIVE:
@@ -465,6 +517,8 @@ gtk_cell_view_init (GtkCellView *cellview)
   priv = cellview->priv;
 
   gtk_widget_set_has_window (GTK_WIDGET (cellview), FALSE);
+
+  priv->orientation = GTK_ORIENTATION_HORIZONTAL;
 }
 
 static void
@@ -521,11 +575,12 @@ gtk_cell_view_size_allocate (GtkWidget     *widget,
    * If the cellview is in "fit model" mode, we assume its not in context and needs to
    * allocate every time.
    */
-  if ((alloc_width <= 0 && alloc_height <= 0) || priv->fit_model)
-    {
-      gtk_cell_area_context_allocate_width (priv->context, allocation->width);
-      gtk_cell_area_context_allocate_height (priv->context, allocation->height);
-    }
+  if (priv->fit_model)
+    gtk_cell_area_context_allocate (priv->context, allocation->width, allocation->height);
+  else if (alloc_width != allocation->width && priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+    gtk_cell_area_context_allocate (priv->context, allocation->width, -1);
+  else if (alloc_height != allocation->height && priv->orientation == GTK_ORIENTATION_VERTICAL)
+    gtk_cell_area_context_allocate (priv->context, -1, allocation->height);
 }
 
 static void
@@ -540,6 +595,9 @@ gtk_cell_view_request_model (GtkCellView        *cellview,
   GtkTreeIter         iter;
   gboolean            valid;
 
+  if (!priv->model)
+    return;
+
   valid = gtk_tree_model_iter_children (priv->model, &iter, parent);
   while (valid)
     {
@@ -599,7 +657,6 @@ gtk_cell_view_get_preferred_width  (GtkWidget *widget,
       gtk_cell_area_get_preferred_width (priv->area, priv->context, widget, NULL, NULL);
     }
 
-  gtk_cell_area_context_sum_preferred_width (priv->context);
   gtk_cell_area_context_get_preferred_width (priv->context, minimum_size, natural_size);
 
   g_signal_handler_unblock (priv->context, priv->size_changed_id);
@@ -628,7 +685,6 @@ gtk_cell_view_get_preferred_height (GtkWidget *widget,
       gtk_cell_area_get_preferred_height (priv->area, priv->context, widget, NULL, NULL);
     }
 
-  gtk_cell_area_context_sum_preferred_height (priv->context);
   gtk_cell_area_context_get_preferred_height (priv->context, minimum_size, natural_size);
 
   g_signal_handler_unblock (priv->context, priv->size_changed_id);
@@ -771,8 +827,15 @@ static GtkCellArea *
 gtk_cell_view_cell_layout_get_area (GtkCellLayout   *layout)
 {
   GtkCellView *cellview = GTK_CELL_VIEW (layout);
+  GtkCellViewPrivate *priv = cellview->priv;
+
+  if (G_UNLIKELY (!priv->area))
+    {
+      priv->area = gtk_cell_area_box_new ();
+      g_object_ref_sink (priv->area);
+    }
 
-  return cellview->priv->area;
+  return priv->area;
 }
 
 /* GtkBuildable implementation */
@@ -800,9 +863,9 @@ gtk_cell_view_buildable_custom_tag_end (GtkBuildable *buildable,
                                        const gchar  *tagname,
                                        gpointer     *data)
 {
-  if (strcmp (tagname, "attributes") == 0 || strcmp (tagname, "cell-packing") == 0)
-    _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
-                                              data);
+  if (_gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
+                                                data))
+    return;
   else if (parent_buildable_iface->custom_tag_end)
     parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
                                            data);
@@ -837,7 +900,7 @@ row_changed_cb (GtkTreeModel         *model,
        {
          /* Resize everything in our context if our row changed */
          if (gtk_tree_path_compare (row_path, path) == 0)
-           gtk_cell_area_context_flush (view->priv->context);
+           gtk_cell_area_context_reset (view->priv->context);
 
          gtk_tree_path_free (row_path);
        }
@@ -1046,7 +1109,6 @@ gtk_cell_view_set_model (GtkCellView  *cell_view,
       cell_view->priv->displayed_row = NULL;
 
       g_object_unref (cell_view->priv->model);
-      cell_view->priv->model = NULL;
     }
 
   cell_view->priv->model = model;
@@ -1144,7 +1206,7 @@ gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
  * gtk_cell_view_get_size_of_row:
  * @cell_view: a #GtkCellView
  * @path: a #GtkTreePath 
- * @requisition: return location for the size 
+ * @requisition: (out): return location for the size 
  *
  * Sets @requisition to the size needed by @cell_view to display 
  * the model row pointed to by @path.
@@ -1153,107 +1215,36 @@ gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
  *
  * Since: 2.6
  * 
- * Deprecated: 3.0: Use gtk_cell_view_get_desired_width_of_row() and
- * gtk_cell_view_get_desired_height_for_width_of_row() instead.
+ * Deprecated: 3.0: Combo box formerly used this to calculate the
+ * sizes for cellviews, now you can achieve this by either using
+ * the #GtkCellView:fit-model property or by setting the currently
+ * displayed row of the #GtkCellView and using gtk_widget_get_preferred_size().
  */
 gboolean
 gtk_cell_view_get_size_of_row (GtkCellView    *cell_view,
                                GtkTreePath    *path,
                                GtkRequisition *requisition)
 {
+  GtkTreeRowReference *tmp;
   GtkRequisition req;
 
   g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
   g_return_val_if_fail (path != NULL, FALSE);
 
-  /* Return the minimum height for the minimum width */
-  gtk_cell_view_get_desired_width_of_row (cell_view, path, &req.width, NULL);
-  gtk_cell_view_get_desired_height_for_width_of_row (cell_view, path, req.width, &req.height, NULL);
-
-  if (requisition)
-    *requisition = req;
-
-  return TRUE;
-}
-
-
-/**
- * gtk_cell_view_get_desired_width_of_row:
- * @cell_view: a #GtkCellView
- * @path: a #GtkTreePath 
- * @minimum_size: location to store the minimum size 
- * @natural_size: location to store the natural size 
- *
- * Sets @minimum_size and @natural_size to the width desired by @cell_view 
- * to display the model row pointed to by @path.
- * 
- * Since: 3.0
- */
-void
-gtk_cell_view_get_desired_width_of_row (GtkCellView     *cell_view,
-                                       GtkTreePath     *path,
-                                       gint            *minimum_size,
-                                       gint            *natural_size)
-{
-  GtkTreeRowReference *tmp;
-
-  g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
-  g_return_if_fail (path != NULL);
-  g_return_if_fail (minimum_size != NULL || natural_size != NULL);
-
   tmp = cell_view->priv->displayed_row;
   cell_view->priv->displayed_row =
     gtk_tree_row_reference_new (cell_view->priv->model, path);
 
-  gtk_cell_view_get_preferred_width (GTK_WIDGET (cell_view), minimum_size, natural_size);
+  gtk_widget_get_preferred_width (GTK_WIDGET (cell_view), &req.width, NULL);
+  gtk_widget_get_preferred_height_for_width (GTK_WIDGET (cell_view), req.width, &req.height, NULL);
 
   gtk_tree_row_reference_free (cell_view->priv->displayed_row);
   cell_view->priv->displayed_row = tmp;
 
-  /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
-  gtk_cell_view_get_preferred_width (GTK_WIDGET (cell_view), NULL, NULL);
-}
-
-
-/**
- * gtk_cell_view_get_desired_height_for_width_of_row:
- * @cell_view: a #GtkCellView
- * @path: a #GtkTreePath 
- * @avail_size: available width
- * @minimum_size: location to store the minimum height 
- * @natural_size: location to store the natural height
- *
- * Sets @minimum_size and @natural_size to the height desired by @cell_view 
- * if it were allocated a width of @avail_size to display the model row 
- * pointed to by @path.
- * 
- * Since: 3.0
- */
-void
-gtk_cell_view_get_desired_height_for_width_of_row (GtkCellView     *cell_view,
-                                                  GtkTreePath     *path,
-                                                  gint             avail_size,
-                                                  gint            *minimum_size,
-                                                  gint            *natural_size)
-{
-  GtkTreeRowReference *tmp;
-
-  g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
-  g_return_if_fail (path != NULL);
-  g_return_if_fail (minimum_size != NULL || natural_size != NULL);
-
-  tmp = cell_view->priv->displayed_row;
-  cell_view->priv->displayed_row =
-    gtk_tree_row_reference_new (cell_view->priv->model, path);
-
-  /* Then get the collective height_for_width based on the cached values */
-  gtk_cell_view_get_preferred_height_for_width (GTK_WIDGET (cell_view), avail_size, minimum_size, natural_size);
-
-  gtk_tree_row_reference_free (cell_view->priv->displayed_row);
-  cell_view->priv->displayed_row = tmp;
+  if (requisition)
+    *requisition = req;
 
-  /* Restore active size (this will restore the cellrenderer info->width/requested_width's) */
-  gtk_cell_view_get_preferred_width (GTK_WIDGET (cell_view), NULL, NULL);
+  return TRUE;
 }
 
 /**
@@ -1333,6 +1324,18 @@ gtk_cell_view_set_background_rgba (GtkCellView   *cell_view,
   gtk_widget_queue_draw (GTK_WIDGET (cell_view));
 }
 
+/**
+ * gtk_cell_view_get_draw_sensitive:
+ * @cell_view: a #GtkCellView
+ *
+ * Gets whether @cell_view is configured to draw all of its
+ * cells in a sensitive state.
+ *
+ * Return value: whether @cell_view draws all of its
+ * cells in a sensitive state
+ *
+ * Since: 3.0
+ */
 gboolean
 gtk_cell_view_get_draw_sensitive (GtkCellView     *cell_view)
 {
@@ -1345,6 +1348,18 @@ gtk_cell_view_get_draw_sensitive (GtkCellView     *cell_view)
   return priv->draw_sensitive;
 }
 
+/**
+ * gtk_cell_view_set_draw_sensitive:
+ * @cell_view: a #GtkCellView
+ * @draw_sensitive: whether to draw all cells in a sensitive state.
+ *
+ * Sets whether @cell_view should draw all of its
+ * cells in a sensitive state, this is used by #GtkComboBox menus
+ * to ensure that rows with insensitive cells that contain
+ * children appear sensitive in the parent menu item.
+ *
+ * Since: 3.0
+ */
 void
 gtk_cell_view_set_draw_sensitive (GtkCellView     *cell_view,
                                  gboolean         draw_sensitive)
@@ -1363,6 +1378,18 @@ gtk_cell_view_set_draw_sensitive (GtkCellView     *cell_view,
     }
 }
 
+/**
+ * gtk_cell_view_get_fit_model:
+ * @cell_view: a #GtkCellView
+ *
+ * Gets whether @cell_view is configured to request space
+ * to fit the entire #GtkTreeModel.
+ *
+ * Return value: whether @cell_view requests space to fit
+ * the entire #GtkTreeModel.
+ *
+ * Since: 3.0
+ */
 gboolean
 gtk_cell_view_get_fit_model (GtkCellView     *cell_view)
 {
@@ -1375,9 +1402,22 @@ gtk_cell_view_get_fit_model (GtkCellView     *cell_view)
   return priv->fit_model;
 }
 
+/**
+ * gtk_cell_view_set_fit_model:
+ * @cell_view: a #GtkCellView
+ * @fit_model: whether @cell_view should request space for the whole model.
+ *
+ * Sets whether @cell_view should request space to fit the entire #GtkTreeModel.
+ *
+ * This is used by #GtkComboBox to ensure that the cell view displayed on
+ * the combo box's button always gets enough space and does not resize
+ * when selection changes.
+ *
+ * Since: 3.0
+ */
 void
-gtk_cell_view_set_fit_model (GtkCellView     *cell_view,
-                            gboolean         fit_model)
+gtk_cell_view_set_fit_model (GtkCellView *cell_view,
+                             gboolean     fit_model)
 {
   GtkCellViewPrivate *priv;
 
@@ -1389,7 +1429,7 @@ gtk_cell_view_set_fit_model (GtkCellView     *cell_view,
     {
       priv->fit_model = fit_model;
 
-      gtk_cell_area_context_flush (cell_view->priv->context);
+      gtk_cell_area_context_reset (cell_view->priv->context);
 
       g_object_notify (G_OBJECT (cell_view), "fit-model");
     }