[gtk+/gtk-style-context: 174/540] GtkStyleSet: Allow custom parsers on registered properties.



commit f31b9ba6c6547b5730fd4dce0a1c7999f863e3f1
Author: Carlos Garnacho <carlosg gnome org>
Date:   Wed Aug 4 17:49:15 2010 +0200

    GtkStyleSet: Allow custom parsers on registered properties.

 gtk/gtkcssprovider.c |   13 +++++++++++--
 gtk/gtkstyleset.c    |   35 ++++++++++++++++++++++-------------
 gtk/gtkstyleset.h    |   16 +++++++++++-----
 3 files changed, 44 insertions(+), 20 deletions(-)
---
diff --git a/gtk/gtkcssprovider.c b/gtk/gtkcssprovider.c
index b0d41b4..2cb9044 100644
--- a/gtk/gtkcssprovider.c
+++ b/gtk/gtkcssprovider.c
@@ -1398,7 +1398,9 @@ parse_rule (GtkCssProvider *css_provider,
   while (scanner->token == G_TOKEN_IDENTIFIER)
     {
       const gchar *value_str = NULL;
+      GtkStylePropertyParser parse_func = NULL;
       GType prop_type;
+      GError *error = NULL;
       gchar *prop;
 
       prop = g_strdup (scanner->value.v_identifier);
@@ -1432,7 +1434,7 @@ parse_rule (GtkCssProvider *css_provider,
 
           g_hash_table_insert (priv->cur_properties, prop, val);
         }
-      else if (gtk_style_set_lookup_property (prop, &prop_type))
+      else if (gtk_style_set_lookup_property (prop, &prop_type, &parse_func))
         {
           GValue *val;
 
@@ -1444,10 +1446,17 @@ parse_rule (GtkCssProvider *css_provider,
               g_value_set_string (val, value_str);
               g_hash_table_insert (priv->cur_properties, prop, val);
             }
-          else if (css_provider_parse_value (value_str, val))
+          else if ((parse_func && (parse_func) (value_str, val, &error)) ||
+                   (!parse_func && css_provider_parse_value (value_str, val)))
             g_hash_table_insert (priv->cur_properties, prop, val);
           else
             {
+              if (error)
+                {
+                  g_warning ("Error parsing property value: %s\n", error->message);
+                  g_error_free (error);
+                }
+
               g_value_unset (val);
               g_slice_free (GValue, val);
               g_free (prop);
diff --git a/gtk/gtkstyleset.c b/gtk/gtkstyleset.c
index f1b0b35..d2053db 100644
--- a/gtk/gtkstyleset.c
+++ b/gtk/gtkstyleset.c
@@ -39,6 +39,7 @@ struct PropertyNode
   GQuark property_quark;
   GType property_type;
   GValue default_value;
+  GtkStylePropertyParser parse_func;
 };
 
 struct PropertyData
@@ -72,19 +73,19 @@ gtk_style_set_class_init (GtkStyleSetClass *klass)
   object_class->finalize = gtk_style_set_finalize;
 
   /* Initialize default property set */
-  gtk_style_set_register_property ("foreground-color", GDK_TYPE_COLOR, NULL);
-  gtk_style_set_register_property ("background-color", GDK_TYPE_COLOR, NULL);
-  gtk_style_set_register_property ("text-color", GDK_TYPE_COLOR, NULL);
-  gtk_style_set_register_property ("base-color", GDK_TYPE_COLOR, NULL);
+  gtk_style_set_register_property ("foreground-color", GDK_TYPE_COLOR, NULL, NULL);
+  gtk_style_set_register_property ("background-color", GDK_TYPE_COLOR, NULL, NULL);
+  gtk_style_set_register_property ("text-color", GDK_TYPE_COLOR, NULL, NULL);
+  gtk_style_set_register_property ("base-color", GDK_TYPE_COLOR, NULL, NULL);
 
-  gtk_style_set_register_property ("font", PANGO_TYPE_FONT_DESCRIPTION, NULL);
+  gtk_style_set_register_property ("font", PANGO_TYPE_FONT_DESCRIPTION, NULL, NULL);
 
-  gtk_style_set_register_property ("padding", GTK_TYPE_BORDER, NULL);
-  gtk_style_set_register_property ("border", G_TYPE_INT, NULL);
+  gtk_style_set_register_property ("padding", GTK_TYPE_BORDER, NULL, NULL);
+  gtk_style_set_register_property ("border", G_TYPE_INT, NULL, NULL);
 
   g_value_init (&val, GTK_TYPE_THEMING_ENGINE);
   g_value_set_object (&val, (GObject *) gtk_theming_engine_load (NULL));
-  gtk_style_set_register_property ("engine", GTK_TYPE_THEMING_ENGINE, &val);
+  gtk_style_set_register_property ("engine", GTK_TYPE_THEMING_ENGINE, &val, NULL);
   g_value_unset (&val);
 
   g_type_class_add_private (object_class, sizeof (GtkStyleSetPrivate));
@@ -186,9 +187,10 @@ property_node_lookup (GQuark quark)
 
 /* Property registration functions */
 void
-gtk_style_set_register_property (const gchar  *property_name,
-                                 GType         type,
-                                 const GValue *default_value)
+gtk_style_set_register_property (const gchar            *property_name,
+                                 GType                   type,
+                                 const GValue           *default_value,
+                                 GtkStylePropertyParser  parse_func)
 {
   PropertyNode *node, new = { 0 };
   GQuark quark;
@@ -220,6 +222,9 @@ gtk_style_set_register_property (const gchar  *property_name,
       g_value_copy (default_value, &new.default_value);
     }
 
+  if (parse_func)
+    new.parse_func = parse_func;
+
   for (i = 0; i < properties->len; i++)
     {
       node = &g_array_index (properties, PropertyNode, i);
@@ -232,8 +237,9 @@ gtk_style_set_register_property (const gchar  *property_name,
 }
 
 gboolean
-gtk_style_set_lookup_property (const gchar *property_name,
-                               GType       *type)
+gtk_style_set_lookup_property (const gchar            *property_name,
+                               GType                  *type,
+                               GtkStylePropertyParser *parse_func)
 {
   PropertyNode *node;
   GtkStyleSetClass *klass;
@@ -261,6 +267,9 @@ gtk_style_set_lookup_property (const gchar *property_name,
           if (type)
             *type = node->property_type;
 
+          if (parse_func)
+            *parse_func = node->parse_func;
+
           found = TRUE;
           break;
         }
diff --git a/gtk/gtkstyleset.h b/gtk/gtkstyleset.h
index 6b82834..cf46a2f 100644
--- a/gtk/gtkstyleset.h
+++ b/gtk/gtkstyleset.h
@@ -48,14 +48,20 @@ struct GtkStyleSetClass
   GObjectClass parent_class;
 };
 
+typedef gboolean (* GtkStylePropertyParser) (const gchar  *string,
+                                             GValue       *value,
+                                             GError      **error);
+
 GType gtk_style_set_get_type (void) G_GNUC_CONST;
 
 /* Functions to register style properties */
-void     gtk_style_set_register_property (const gchar  *property_name,
-                                          GType         type,
-                                          const GValue *default_value);
-gboolean gtk_style_set_lookup_property   (const gchar  *property_name,
-                                          GType        *type);
+void     gtk_style_set_register_property (const gchar            *property_name,
+                                          GType                   type,
+                                          const GValue           *default_value,
+                                          GtkStylePropertyParser  parse_func);
+gboolean gtk_style_set_lookup_property   (const gchar            *property_name,
+                                          GType                  *type,
+                                          GtkStylePropertyParser *parse_func);
 
 GtkStyleSet * gtk_style_set_new (void);
 



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