]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcellview.c
label: refactor code
[~andy/gtk] / gtk / gtkcellview.c
index 87c56ed8bcdaf51a6054da5532e2cdfc8f8a1224..d5d33ede3221e98c238da93eb3607c0dba842931 100644 (file)
@@ -12,9 +12,7 @@
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
@@ -26,7 +24,7 @@
 #include "gtkcellrenderertext.h"
 #include "gtkcellrendererpixbuf.h"
 #include "gtkprivate.h"
-#include "gtkorientable.h"
+#include "gtkorientableprivate.h"
 #include <gobject/gmarshal.h>
 #include "gtkbuildable.h"
 
@@ -93,6 +91,7 @@ static void       gtk_cell_view_buildable_custom_tag_end       (GtkBuildable
                                                                const gchar           *tagname,
                                                                gpointer              *data);
 
+static GtkSizeRequestMode gtk_cell_view_get_request_mode       (GtkWidget             *widget);
 static void       gtk_cell_view_get_preferred_width            (GtkWidget             *widget,
                                                                gint                  *minimum_size,
                                                                gint                  *natural_size);
@@ -125,16 +124,16 @@ struct _GtkCellViewPrivate
   GtkCellArea         *area;
   GtkCellAreaContext  *context;
 
-  GtkOrientation       orientation;
-
   GdkRGBA              background;
-  gboolean             background_set;
 
   gulong               size_changed_id;
   gulong               row_changed_id;
 
-  guint32              draw_sensitive : 1;
-  guint32              fit_model      : 1;
+  GtkOrientation       orientation;
+
+  guint                background_set : 1;
+  guint                draw_sensitive : 1;
+  guint                fit_model      : 1;
 };
 
 static GtkBuildableIface *parent_buildable_iface;
@@ -175,6 +174,7 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
 
   widget_class->draw                           = gtk_cell_view_draw;
   widget_class->size_allocate                  = gtk_cell_view_size_allocate;
+  widget_class->get_request_mode               = gtk_cell_view_get_request_mode;
   widget_class->get_preferred_width            = gtk_cell_view_get_preferred_width;
   widget_class->get_preferred_height           = gtk_cell_view_get_preferred_height;
   widget_class->get_preferred_width_for_height = gtk_cell_view_get_preferred_width_for_height;
@@ -190,15 +190,23 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
                                                         P_("Background color as a string"),
                                                         NULL,
                                                         GTK_PARAM_WRITABLE));
+
+  /**
+   * GtkCellView:background-gdk:
+   *
+   * The background color as a #GdkColor
+   *
+   * Deprecated: 3.4: Use #GtkCellView:background-rgba instead.
+   */
   g_object_class_install_property (gobject_class,
                                    PROP_BACKGROUND_GDK,
                                    g_param_spec_boxed ("background-gdk",
                                                       P_("Background color"),
                                                       P_("Background color as a GdkColor"),
                                                       GDK_TYPE_COLOR,
-                                                      GTK_PARAM_READWRITE));
+                                                      GTK_PARAM_READWRITE | G_PARAM_DEPRECATED));
   /**
-   * GtkCellView:background-rgba
+   * GtkCellView:background-rgba:
    *
    * The background color as a #GdkRGBA
    *
@@ -213,7 +221,7 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
                                                       GTK_PARAM_READWRITE));
 
   /**
-   * GtkCellView:model
+   * GtkCellView:model:
    *
    * The model for cell view
    *
@@ -229,10 +237,13 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
 
 
   /**
-   * GtkCellView:cell-area
+   * GtkCellView:cell-area:
    *
    * The #GtkCellArea rendering cells
    *
+   * If no area is specified when creating the cell view with gtk_cell_view_new_with_context() 
+   * a horizontally oriented #GtkCellAreaBox will be used.
+   *
    * since 3.0
    */
    g_object_class_install_property (gobject_class,
@@ -244,7 +255,7 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
   /**
-   * GtkCellView:cell-area-context
+   * GtkCellView:cell-area-context:
    *
    * The #GtkCellAreaContext used to compute the geometry of the cell view.
    *
@@ -253,7 +264,7 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
    * 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 it's own
+   * 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).
    *
@@ -269,7 +280,7 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
   /**
-   * GtkCellView:draw-sensitive
+   * GtkCellView:draw-sensitive:
    *
    * Whether all cells should be draw as sensitive for this view regardless
    * of the actual cell properties (used to make menus with submenus appear
@@ -287,7 +298,7 @@ gtk_cell_view_class_init (GtkCellViewClass *klass)
                                                          GTK_PARAM_READWRITE));
 
   /**
-   * GtkCellView:fit-model
+   * GtkCellView:fit-model:
    *
    * Whether the view should request enough space to always fit
    * the size of every row in the model (used by the combo box to
@@ -347,15 +358,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);
 
@@ -414,25 +424,6 @@ gtk_cell_view_get_property (GObject    *object,
     }
 }
 
-static void
-reset_orientation_style (GtkCellView *view)
-{
-  GtkStyleContext *context;
-
-  context = gtk_widget_get_style_context (GTK_WIDGET (view));
-
-  if (view->priv->orientation == GTK_ORIENTATION_VERTICAL)
-    {
-      gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
-      gtk_style_context_remove_class (context, GTK_STYLE_CLASS_HORIZONTAL);
-    }
-  else
-    {
-      gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
-      gtk_style_context_remove_class (context, GTK_STYLE_CLASS_VERTICAL);
-    }
-}
-
 static void
 gtk_cell_view_set_property (GObject      *object,
                             guint         param_id,
@@ -440,34 +431,48 @@ 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:
-      view->priv->orientation = g_value_get_enum (value);
-      if (view->priv->context)
-       gtk_cell_area_context_reset (view->priv->context);
+      priv->orientation = g_value_get_enum (value);
+      if (priv->context)
+        gtk_cell_area_context_reset (priv->context);
 
-      reset_orientation_style (view);
+      _gtk_orientable_set_style_classes (GTK_ORIENTABLE (object));
       break;
     case PROP_BACKGROUND:
       {
-       GdkColor color;
-       
+        GdkRGBA color;
+
        if (!g_value_get_string (value))
-         gtk_cell_view_set_background_color (view, NULL);
-       else if (gdk_color_parse (g_value_get_string (value), &color))
-         gtk_cell_view_set_background_color (view, &color);
+          gtk_cell_view_set_background_rgba (view, NULL);
+       else if (gdk_rgba_parse (&color, g_value_get_string (value)))
+          gtk_cell_view_set_background_rgba (view, &color);
        else
          g_warning ("Don't know color `%s'", g_value_get_string (value));
-       
-       g_object_notify (object, "background-gdk");
+
+        g_object_notify (object, "background-rgba");
+        g_object_notify (object, "background-gdk");
       }
       break;
     case PROP_BACKGROUND_GDK:
-      gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
+      {
+        GdkColor *color;
+        GdkRGBA rgba;
+
+        color = g_value_get_boxed (value);
+
+        rgba.red = color->red / 65535.0;
+        rgba.green = color->green / 65535.0;
+        rgba.blue = color->blue / 65535.0;
+        rgba.alpha = 1.0;
+
+        gtk_cell_view_set_background_rgba (view, &rgba);
+      }
       break;
     case PROP_BACKGROUND_RGBA:
       gtk_cell_view_set_background_rgba (view, g_value_get_boxed (value));
@@ -481,16 +486,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:
@@ -573,7 +596,7 @@ gtk_cell_view_size_allocate (GtkWidget     *widget,
   /* The first cell view in context is responsible for allocating the context at allocate time 
    * (or the cellview has its own context and is not grouped with any other cell views) 
    *
-   * If the cellview is in "fit model" mode, we assume its not in context and needs to
+   * If the cellview is in "fit model" mode, we assume it's not in context and needs to
    * allocate every time.
    */
   if (priv->fit_model)
@@ -596,6 +619,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)
     {
@@ -632,6 +658,15 @@ gtk_cell_view_request_model (GtkCellView        *cellview,
     }
 }
 
+static GtkSizeRequestMode 
+gtk_cell_view_get_request_mode (GtkWidget *widget)
+{
+  GtkCellView        *cellview = GTK_CELL_VIEW (widget);
+  GtkCellViewPrivate *priv = cellview->priv;
+
+  return gtk_cell_area_get_request_mode (priv->area);
+}
+
 static void
 gtk_cell_view_get_preferred_width  (GtkWidget *widget,
                                     gint      *minimum_size,
@@ -774,8 +809,6 @@ gtk_cell_view_draw (GtkWidget *widget,
 
   if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_PRELIGHT)
     state = GTK_CELL_RENDERER_PRELIT;
-  else if (gtk_widget_get_state_flags (widget) & GTK_STATE_FLAG_INSENSITIVE)
-    state = GTK_CELL_RENDERER_INSENSITIVE;
   else
     state = 0;
       
@@ -825,8 +858,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 */
@@ -953,7 +993,7 @@ gtk_cell_view_new_with_context (GtkCellArea        *area,
  * @text: the text to display in the cell view
  *
  * Creates a new #GtkCellView widget, adds a #GtkCellRendererText 
- * to it, and makes its show @text.
+ * to it, and makes it show @text.
  *
  * Return value: A newly created #GtkCellView widget.
  *
@@ -964,7 +1004,7 @@ gtk_cell_view_new_with_text (const gchar *text)
 {
   GtkCellView *cellview;
   GtkCellRenderer *renderer;
-  GValue value = {0, };
+  GValue value = G_VALUE_INIT;
 
   cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
 
@@ -998,7 +1038,7 @@ gtk_cell_view_new_with_markup (const gchar *markup)
 {
   GtkCellView *cellview;
   GtkCellRenderer *renderer;
-  GValue value = {0, };
+  GValue value = G_VALUE_INIT;
 
   cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
 
@@ -1018,8 +1058,8 @@ gtk_cell_view_new_with_markup (const gchar *markup)
  * gtk_cell_view_new_with_pixbuf:
  * @pixbuf: the image to display in the cell view
  *
- * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf 
- * to it, and makes its show @pixbuf. 
+ * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf
+ * to it, and makes it show @pixbuf.
  *
  * Return value: A newly created #GtkCellView widget.
  *
@@ -1030,7 +1070,7 @@ gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
 {
   GtkCellView *cellview;
   GtkCellRenderer *renderer;
-  GValue value = {0, };
+  GValue value = G_VALUE_INIT;
 
   cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
 
@@ -1100,7 +1140,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;
@@ -1198,7 +1237,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.
@@ -1247,6 +1286,8 @@ gtk_cell_view_get_size_of_row (GtkCellView    *cell_view,
  * Sets the background color of @view.
  *
  * Since: 2.6
+ *
+ * Deprecated: 3.4: Use gtk_cell_view_set_background_rgba() instead.
  */
 void
 gtk_cell_view_set_background_color (GtkCellView    *cell_view,
@@ -1320,10 +1361,10 @@ gtk_cell_view_set_background_rgba (GtkCellView   *cell_view,
  * gtk_cell_view_get_draw_sensitive:
  * @cell_view: a #GtkCellView
  *
- * Gets whether @cell_view is configured to draw all of it's
+ * Gets whether @cell_view is configured to draw all of its
  * cells in a sensitive state.
  *
- * Return value: whether @cell_view draws all of it's
+ * Return value: whether @cell_view draws all of its
  * cells in a sensitive state
  *
  * Since: 3.0
@@ -1345,7 +1386,7 @@ gtk_cell_view_get_draw_sensitive (GtkCellView     *cell_view)
  * @cell_view: a #GtkCellView
  * @draw_sensitive: whether to draw all cells in a sensitive state.
  *
- * Sets whether @cell_view should draw all of it's
+ * 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.
@@ -1408,8 +1449,8 @@ gtk_cell_view_get_fit_model (GtkCellView     *cell_view)
  * 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;