[gtk+/wip/css: 15/167] styleproperty: Pass initial value explicitly



commit d9bb53cddfde33c25c8c1677ee5de3f68cf31f2d
Author: Benjamin Otte <otte redhat com>
Date:   Thu Dec 29 02:28:32 2011 +0100

    styleproperty: Pass initial value explicitly

 gtk/gtkstyleproperties.c      |    4 +-
 gtk/gtkstyleproperty.c        |  145 ++++++++++++++++++++++++-----------------
 gtk/gtkstylepropertyprivate.h |    7 +--
 3 files changed, 91 insertions(+), 65 deletions(-)
---
diff --git a/gtk/gtkstyleproperties.c b/gtk/gtkstyleproperties.c
index f2b6b7d..26a9665 100644
--- a/gtk/gtkstyleproperties.c
+++ b/gtk/gtkstyleproperties.c
@@ -34,6 +34,7 @@
 #include "gtkcsstypesprivate.h"
 #include "gtkborderimageprivate.h"
 
+#include "gtkprivatetypebuiltins.h"
 #include "gtkstylepropertyprivate.h"
 #include "gtkstyleproviderprivate.h"
 #include "gtkintl.h"
@@ -537,9 +538,10 @@ _gtk_style_properties_set_property_by_property (GtkStyleProperties     *props,
   if (style_prop->pspec->value_type == GDK_TYPE_RGBA ||
       style_prop->pspec->value_type == GDK_TYPE_COLOR)
     {
-      /* Allow GtkSymbolicColor as well */
+      /* Allow GtkSymbolicColor and special values as well */
       g_return_if_fail (value_type == GDK_TYPE_RGBA ||
                         value_type == GDK_TYPE_COLOR ||
+                        value_type == GTK_TYPE_CSS_SPECIAL_VALUE ||
                         value_type == GTK_TYPE_SYMBOLIC_COLOR);
     }
   else if (style_prop->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
diff --git a/gtk/gtkstyleproperty.c b/gtk/gtkstyleproperty.c
index fee3bba..518a0e4 100644
--- a/gtk/gtkstyleproperty.c
+++ b/gtk/gtkstyleproperty.c
@@ -2211,34 +2211,6 @@ unset_border_image (GtkStyleProperties *props,
   gtk_style_properties_unset_property (props, "border-image-width", state);
 }
 
-/*** default values ***/
-
-static void
-border_image_width_default_value (GtkStyleProperties *props,
-                                  GtkStateFlags       state,
-                                  GValue             *value)
-{
-}
-
-static void
-background_color_default_value (GtkStyleProperties *props,
-                                GtkStateFlags       state,
-                                GValue             *value)
-{
-  GdkRGBA transparent_black = { 0, 0, 0, 0 };
-
-  g_value_set_boxed (value, &transparent_black);
-}
-
-static void
-border_color_default_value (GtkStyleProperties *props,
-                            GtkStateFlags       state,
-                            GValue             *value)
-{
-  g_value_unset (value);
-  gtk_style_properties_get_property (props, "color", state, value);
-}
-
 /*** API ***/
 
 guint
@@ -2440,24 +2412,7 @@ _gtk_style_property_default_value (const GtkStyleProperty *property,
                                    GtkStateFlags           state,
                                    GValue                 *value)
 {
-  if (property->default_value_func)
-    property->default_value_func (properties, state, value);
-  else if (property->pspec->value_type == GTK_TYPE_THEMING_ENGINE)
-    g_value_set_object (value, gtk_theming_engine_load (NULL));
-  else if (property->pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
-    g_value_take_boxed (value, pango_font_description_from_string ("Sans 10"));
-  else if (property->pspec->value_type == GDK_TYPE_RGBA)
-    {
-      GdkRGBA color;
-      gdk_rgba_parse (&color, "pink");
-      g_value_set_boxed (value, &color);
-    }
-  else if (property->pspec->value_type == GTK_TYPE_BORDER)
-    {
-      g_value_take_boxed (value, gtk_border_new ());
-    }
-  else
-    g_param_value_set_default (property->pspec, value);
+  g_value_copy (&property->initial_value, value);
 }
 
 gboolean
@@ -2584,7 +2539,24 @@ _gtk_style_property_resolve (const GtkStyleProperty *property,
                              GValue                 *val,
 			     GValue                 *val_out)
 {
-  if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
+  if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_SPECIAL_VALUE)
+    {
+      GtkCssSpecialValue special = g_value_get_enum (val);
+
+      g_value_unset (val);
+      switch (special)
+        {
+        case GTK_CSS_CURRENT_COLOR:
+          g_assert (property->pspec->value_type == GDK_TYPE_RGBA);
+          gtk_style_properties_get_property (props, "color", state, val);
+          break;
+        case GTK_CSS_INHERIT:
+        case GTK_CSS_INITIAL:
+        default:
+          g_assert_not_reached ();
+        }
+    }
+  else if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
     {
       if (property->pspec->value_type == GDK_TYPE_RGBA)
         {
@@ -2669,9 +2641,19 @@ _gtk_style_property_pack (const GtkStyleProperty *property,
   property->pack_func (value, props, state, context);
 }
 
+#define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
+  (rgba)->red = (r); \
+  (rgba)->green = (g); \
+  (rgba)->blue = (b); \
+  (rgba)->alpha = (a); \
+}G_STMT_END
 static void
 gtk_style_property_init (void)
 {
+  GValue value = { 0, };
+  char *default_font_family[] = { "Sans", NULL };
+  GdkRGBA rgba;
+
   if (G_LIKELY (properties))
     return;
 
@@ -2681,6 +2663,9 @@ gtk_style_property_init (void)
   /* note that gtk_style_properties_register_property() calls this function,
    * so make sure we're sanely inited to avoid infloops */
 
+  g_value_init (&value, GDK_TYPE_RGBA);
+  rgba_init (&rgba, 1, 1, 1, 1);
+  g_value_set_boxed (&value, &rgba);
   _gtk_style_property_register           (g_param_spec_boxed ("color",
                                           "Foreground color",
                                           "Foreground color",
@@ -2691,8 +2676,10 @@ gtk_style_property_init (void)
                                           NULL,
                                           NULL,
                                           NULL,
-                                          NULL,
+                                          &value,
                                           NULL);
+  rgba_init (&rgba, 0, 0, 0, 0);
+  g_value_set_boxed (&value, &rgba);
   _gtk_style_property_register           (g_param_spec_boxed ("background-color",
                                           "Background color",
                                           "Background color",
@@ -2703,9 +2690,12 @@ gtk_style_property_init (void)
                                           NULL,
                                           transparent_color_value_parse,
                                           NULL,
-                                          background_color_default_value,
+                                          &value,
                                           NULL);
+  g_value_unset (&value);
 
+  g_value_init (&value, G_TYPE_STRV);
+  g_value_set_boxed (&value, default_font_family);
   _gtk_style_property_register           (g_param_spec_boxed ("font-family",
                                                               "Font family",
                                                               "Font family",
@@ -2716,8 +2706,9 @@ gtk_style_property_init (void)
                                           NULL,
                                           font_family_parse,
                                           font_family_value_print,
-                                          NULL,
+                                          &value,
                                           NULL);
+  g_value_unset (&value);
   _gtk_style_property_register           (g_param_spec_enum ("font-style",
                                                              "Font style",
                                                              "Font style",
@@ -2758,6 +2749,8 @@ gtk_style_property_init (void)
                                           NULL,
                                           NULL,
                                           NULL);
+  g_value_init (&value, G_TYPE_DOUBLE);
+  g_value_set_double (&value, 10);
   _gtk_style_property_register           (g_param_spec_double ("font-size",
                                                                "Font size",
                                                                "Font size",
@@ -2768,8 +2761,9 @@ gtk_style_property_init (void)
                                           NULL,
                                           NULL,
                                           NULL,
-                                          NULL,
+                                          &value,
                                           NULL);
+  g_value_unset (&value);
   _gtk_style_property_register           (g_param_spec_boxed ("font",
                                                               "Font Description",
                                                               "Font Description",
@@ -2990,6 +2984,8 @@ gtk_style_property_init (void)
                                                              "Background origin",
                                                              GTK_TYPE_CSS_AREA,
                                                              GTK_CSS_AREA_PADDING_BOX, 0));
+  g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
+  g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
   _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
                                                               "Border top color",
                                                               "Border top color",
@@ -3000,7 +2996,7 @@ gtk_style_property_init (void)
                                           NULL,
                                           transparent_color_value_parse,
                                           NULL,
-                                          border_color_default_value,
+                                          &value,
                                           NULL);
   _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
                                                               "Border right color",
@@ -3012,7 +3008,7 @@ gtk_style_property_init (void)
                                           NULL,
                                           transparent_color_value_parse,
                                           NULL,
-                                          border_color_default_value,
+                                          &value,
                                           NULL);
   _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
                                                               "Border bottom color",
@@ -3024,7 +3020,7 @@ gtk_style_property_init (void)
                                           NULL,
                                           transparent_color_value_parse,
                                           NULL,
-                                          border_color_default_value,
+                                          &value,
                                           NULL);
   _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
                                                               "Border left color",
@@ -3036,8 +3032,9 @@ gtk_style_property_init (void)
                                           NULL,
                                           transparent_color_value_parse,
                                           NULL,
-                                          border_color_default_value,
+                                          &value,
                                           NULL);
+  g_value_unset (&value);
   _gtk_style_property_register           (g_param_spec_boxed ("border-color",
                                                               "Border color",
                                                               "Border color",
@@ -3077,6 +3074,7 @@ gtk_style_property_init (void)
                                                               "Border image slice",
                                                               "Border image slice",
                                                               GTK_TYPE_BORDER, 0));
+  g_value_init (&value, GTK_TYPE_BORDER);
   _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
                                                               "Border image width",
                                                               "Border image width",
@@ -3087,8 +3085,9 @@ gtk_style_property_init (void)
                                           NULL,
                                           NULL,
                                           NULL,
-                                          border_image_width_default_value,
+                                          &value,
                                           NULL);
+  g_value_unset (&value);
   _gtk_style_property_register           (g_param_spec_boxed ("border-image",
                                                               "Border Image",
                                                               "Border Image",
@@ -3143,7 +3142,7 @@ _gtk_style_property_register (GParamSpec               *pspec,
                               GtkStylePackFunc          pack_func,
                               GtkStyleParseFunc         parse_func,
                               GtkStylePrintFunc         print_func,
-                              GtkStyleDefaultValueFunc  default_value_func,
+                              const GValue *            initial_value,
                               GtkStyleUnsetFunc         unset_func)
 {
   const GtkStyleProperty *existing;
@@ -3169,11 +3168,39 @@ _gtk_style_property_register (GParamSpec               *pspec,
   node->unpack_func = unpack_func;
   node->parse_func = parse_func;
   node->print_func = print_func;
-  node->default_value_func = default_value_func;
   node->unset_func = unset_func;
 
   if (!_gtk_style_property_is_shorthand (node))
-    _gtk_style_property_generate_id (node);
+    {
+      _gtk_style_property_generate_id (node);
+
+      /* initialize the initial value */
+      if (initial_value)
+        {
+          g_value_init (&node->initial_value, G_VALUE_TYPE (initial_value));
+          g_value_copy (initial_value, &node->initial_value);
+        }
+      else
+        {
+          g_value_init (&node->initial_value, pspec->value_type);
+          if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
+            g_value_set_object (&node->initial_value, gtk_theming_engine_load (NULL));
+          else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
+            g_value_take_boxed (&node->initial_value, pango_font_description_from_string ("Sans 10"));
+          else if (pspec->value_type == GDK_TYPE_RGBA)
+            {
+              GdkRGBA color;
+              gdk_rgba_parse (&color, "pink");
+              g_value_set_boxed (&node->initial_value, &color);
+            }
+          else if (pspec->value_type == GTK_TYPE_BORDER)
+            {
+              g_value_take_boxed (&node->initial_value, gtk_border_new ());
+            }
+          else
+            g_param_value_set_default (pspec, &node->initial_value);
+        }
+    }
 
   /* pspec owns name */
   g_hash_table_insert (properties, (gchar *)pspec->name, node);
diff --git a/gtk/gtkstylepropertyprivate.h b/gtk/gtkstylepropertyprivate.h
index 6cabf25..faa0e5b 100644
--- a/gtk/gtkstylepropertyprivate.h
+++ b/gtk/gtkstylepropertyprivate.h
@@ -41,9 +41,6 @@ typedef gboolean         (* GtkStyleParseFunc)             (GtkCssParser
                                                             GValue                 *value);
 typedef void             (* GtkStylePrintFunc)             (const GValue           *value,
                                                             GString                *string);
-typedef void             (* GtkStyleDefaultValueFunc)      (GtkStyleProperties     *props,
-                                                            GtkStateFlags           state,
-                                                            GValue                 *value);
 typedef void             (* GtkStyleUnsetFunc)             (GtkStyleProperties     *props,
                                                             GtkStateFlags           state);
 
@@ -53,13 +50,13 @@ struct _GtkStyleProperty
   GParamSpec               *pspec;
   GtkStylePropertyFlags     flags;
   guint                     id;
+  GValue                    initial_value;
 
   GtkStylePropertyParser    property_parse_func;
   GtkStyleUnpackFunc        unpack_func;
   GtkStylePackFunc          pack_func;
   GtkStyleParseFunc         parse_func;
   GtkStylePrintFunc         print_func;
-  GtkStyleDefaultValueFunc  default_value_func;
   GtkStyleUnsetFunc         unset_func;
 };
 
@@ -75,7 +72,7 @@ void                     _gtk_style_property_register      (GParamSpec
                                                             GtkStylePackFunc        pack_func,
                                                             GtkStyleParseFunc       parse_func,
                                                             GtkStylePrintFunc       print_func,
-                                                            GtkStyleDefaultValueFunc default_value_func,
+                                                            const GValue           *initial_value,
                                                             GtkStyleUnsetFunc       unset_func);
 
 gboolean                 _gtk_style_property_is_inherit    (const GtkStyleProperty *property);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]