[gtk+/wip/css: 16/24] styleproperty: Pass initial value explicitly



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

    styleproperty: Pass initial value explicitly

 gtk/gtkstyleproperty.c        |  138 +++++++++++++++++++++++-----------------
 gtk/gtkstylepropertyprivate.h |    7 +--
 2 files changed, 81 insertions(+), 64 deletions(-)
---
diff --git a/gtk/gtkstyleproperty.c b/gtk/gtkstyleproperty.c
index fee3bba..d915821 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
@@ -2586,7 +2541,17 @@ _gtk_style_property_resolve (const GtkStyleProperty *property,
 {
   if (G_VALUE_TYPE (val) == GTK_TYPE_SYMBOLIC_COLOR)
     {
-      if (property->pspec->value_type == GDK_TYPE_RGBA)
+      if (property->pspec->value_type == GTK_TYPE_CSS_SPECIAL_VALUE)
+        {
+          if (g_value_get_enum (val) == GTK_CSS_CURRENT_COLOR)
+            {
+              g_value_unset (val);
+              gtk_style_properties_get_property (props, "color", state, val);
+            }
+          else
+            goto out;
+        }
+      else if (property->pspec->value_type == GDK_TYPE_RGBA)
         {
           if (resolve_color (props, val))
             goto out;
@@ -2669,9 +2634,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 +2656,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 +2669,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 +2683,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 +2699,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 +2742,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 +2754,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 +2977,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 +2989,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 +3001,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 +3013,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 +3025,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 +3067,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 +3078,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 +3135,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 +3161,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]