[metacity] Make color constants work without warnings



commit e3f1212b6d64a887e17974ebffba8f021e4a9c2f
Author: Owen W. Taylor <otaylor fishsoup net>
Date:   Wed Oct 13 23:56:38 2010 -0400

    Make color constants work without warnings
    
    The code for defining a color as a constant had broken logic: it
    would try to parse the color first as an double, then as an integer;
    the second attempt would produce an error about overwriting the
    already-set-GError. Then it would clear the error and store the constant
    as a color.
    
    Use the fact that colors have to start with a letter or #, divide the
    space of constants into:
    
     - Integers
     - Doubles
     - Colors
    
    so we get good error messages. Based on a patch by
    William Jon McCann <jmccann redhat com>.
    
    Note that this breaks the ability to specify an integer constant as
    identical to another integer constant (the same didn't work for doubles.)
    I think this was an accidental side effect of the code and not something
    that was intentional or people were relying on
    
    https://bugzilla.gnome.org/show_bug.cgi?id=632116

 src/ui/theme-parser.c |   55 +++++++++++++++++++++++++++++-------------------
 1 files changed, 33 insertions(+), 22 deletions(-)
---
diff --git a/src/ui/theme-parser.c b/src/ui/theme-parser.c
index c56e318..08f9de6 100644
--- a/src/ui/theme-parser.c
+++ b/src/ui/theme-parser.c
@@ -830,36 +830,47 @@ parse_toplevel_element (GMarkupParseContext  *context,
                               NULL))
         return;
 
-      if (strchr (value, '.') && parse_double (value, &dval, context, error))
+      /* We don't know how a a constant is going to be used, so we have guess its
+       * type from its contents:
+       *
+       *  - Starts like a number and contains a '.': float constant
+       *  - Starts like a number and doesn't contain a '.': int constant
+       *  - Starts with anything else: a color constant.
+       *    (colors always start with # or a letter)
+       */
+      if (value[0] == '.' || value[0] == '+' || value[0] == '-' || (value[0] >= '0' && value[0] <= '9'))
         {
-          g_clear_error (error);
-
-          if (!meta_theme_define_float_constant (info->theme,
-                                                 name,
-                                                 dval,
-                                                 error))
+          if (strchr (value, '.'))
             {
-              add_context_to_error (error, context);
-              return;
-            }
-        }
-      else if (parse_positive_integer (value, &ival, context, info->theme, error))
-        {
-          g_clear_error (error);
+              if (!parse_double (value, &dval, context, error))
+                return;
 
-          if (!meta_theme_define_int_constant (info->theme,
-                                               name,
-                                               ival,
-                                               error))
+              if (!meta_theme_define_float_constant (info->theme,
+                                                     name,
+                                                     dval,
+                                                     error))
+                {
+                  add_context_to_error (error, context);
+                  return;
+                }
+            }
+          else
             {
-              add_context_to_error (error, context);
-              return;
+              if (!parse_positive_integer (value, &ival, context, info->theme, error))
+                return;
+
+              if (!meta_theme_define_int_constant (info->theme,
+                                                   name,
+                                                   ival,
+                                                   error))
+                {
+                  add_context_to_error (error, context);
+                  return;
+                }
             }
         }
       else
         {
-          g_clear_error (error);
-
           if (!meta_theme_define_color_constant (info->theme,
                                                  name,
                                                  value,



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