[gtk+/wip/cssvalue: 63/142] cssvalue: Split number values into their own class



commit 32a8e0621a28526c78de712bfe547dc7fc2f0860
Author: Benjamin Otte <otte redhat com>
Date:   Wed Mar 28 00:04:29 2012 +0200

    cssvalue: Split number values into their own class

 gtk/Makefile.am                   |    2 +
 gtk/gtkcssnumbervalue.c           |  194 +++++++++++++++++++++++++++++++++++++
 gtk/gtkcssnumbervalueprivate.h    |   42 ++++++++
 gtk/gtkcssshorthandpropertyimpl.c |   42 +++-----
 gtk/gtkcssstylefuncs.c            |   11 --
 gtk/gtkcssstylepropertyimpl.c     |  101 ++++++--------------
 gtk/gtkcsstypes.c                 |    1 -
 gtk/gtkcsstypesprivate.h          |    2 -
 gtk/gtkcssvalue.c                 |   52 ----------
 gtk/gtkcssvalueprivate.h          |    2 -
 gtk/gtkstylecontext.c             |    3 +-
 11 files changed, 285 insertions(+), 167 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 6209b4d..d37a3af 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -435,6 +435,7 @@ gtk_private_h_sources =		\
 	gtkcssinitialvalueprivate.h	\
 	gtkcsslookupprivate.h	\
 	gtkcssmatcherprivate.h	\
+	gtkcssnumbervalueprivate.h	\
 	gtkcssparserprivate.h	\
 	gtkcssproviderprivate.h	\
 	gtkcsssectionprivate.h 	\
@@ -635,6 +636,7 @@ gtk_base_c_sources = 		\
 	gtkcssinitialvalue.c	\
 	gtkcsslookup.c		\
 	gtkcssmatcher.c		\
+	gtkcssnumbervalue.c	\
 	gtkcssparser.c		\
 	gtkcssprovider.c	\
 	gtkcsssection.c 	\
diff --git a/gtk/gtkcssnumbervalue.c b/gtk/gtkcssnumbervalue.c
new file mode 100644
index 0000000..e845a68
--- /dev/null
+++ b/gtk/gtkcssnumbervalue.c
@@ -0,0 +1,194 @@
+/* GTK - The GIMP Toolkit
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gtkcssnumbervalueprivate.h"
+
+#include "gtkstylepropertyprivate.h"
+
+struct _GtkCssValue {
+  GTK_CSS_VALUE_BASE
+  GtkCssUnit unit;
+  double value;
+};
+
+static void
+gtk_css_value_number_free (GtkCssValue *value)
+{
+  g_slice_free (GtkCssValue, value);
+}
+
+static gboolean
+gtk_css_value_number_equal (const GtkCssValue *number1,
+                            const GtkCssValue *number2)
+{
+  return number1->unit == number2->unit &&
+         number1->value == number2->value;
+}
+
+static void
+gtk_css_value_number_print (const GtkCssValue *number,
+                            GString           *string)
+{
+  char buf[G_ASCII_DTOSTR_BUF_SIZE];
+
+  const char *names[] = {
+    /* [GTK_CSS_NUMBER] = */ "",
+    /* [GTK_CSS_PERCENT] = */ "%",
+    /* [GTK_CSS_PX] = */ "px",
+    /* [GTK_CSS_PT] = */ "pt",
+    /* [GTK_CSS_EM] = */ "em",
+    /* [GTK_CSS_EX] = */ "ex",
+    /* [GTK_CSS_PC] = */ "pc",
+    /* [GTK_CSS_IN] = */ "in",
+    /* [GTK_CSS_CM] = */ "cm",
+    /* [GTK_CSS_MM] = */ "mm",
+    /* [GTK_CSS_RAD] = */ "rad",
+    /* [GTK_CSS_DEG] = */ "deg",
+    /* [GTK_CSS_GRAD] = */ "grad",
+    /* [GTK_CSS_TURN] = */ "turn",
+  };
+
+  g_ascii_dtostr (buf, sizeof (buf), number->value);
+  g_string_append (string, buf);
+  if (number->value != 0.0)
+    g_string_append (string, names[number->unit]);
+}
+
+static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
+  gtk_css_value_number_free,
+  gtk_css_value_number_equal,
+  gtk_css_value_number_print
+};
+
+GtkCssValue *
+_gtk_css_number_value_new (double     value,
+                           GtkCssUnit unit)
+{
+  static GtkCssValue zero_singleton = { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
+  static GtkCssValue px_singletons[] = {
+    { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
+    { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
+    { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
+    { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
+    { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
+  };
+  GtkCssValue *result;
+
+  if (unit == GTK_CSS_NUMBER && value == 0)
+    return _gtk_css_value_ref (&zero_singleton);
+
+  if (unit == GTK_CSS_PX &&
+      (value == 0 ||
+       value == 1 ||
+       value == 2 ||
+       value == 3 ||
+       value == 4))
+    {
+      return _gtk_css_value_ref (&px_singletons[(int) value]);
+    }
+
+  result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_NUMBER);
+  result->unit = unit;
+  result->value = value;
+
+  return result;
+}
+
+GtkCssValue *
+_gtk_css_number_value_parse (GtkCssParser           *parser,
+                             GtkCssNumberParseFlags  flags)
+{
+  GtkCssNumber number;
+
+  g_return_val_if_fail (parser != NULL, NULL);
+
+  if (!_gtk_css_parser_read_number (parser, &number, flags))
+    return NULL;
+  
+  return _gtk_css_number_value_new (number.value, number.unit);
+}
+
+double
+_gtk_css_number_value_get (const GtkCssValue *number,
+                           double             one_hundred_percent)
+{
+  g_return_val_if_fail (number != NULL, 0.0);
+  g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, 0.0);
+
+  if (number->unit == GTK_CSS_PERCENT)
+    return number->value * one_hundred_percent / 100;
+  else
+    return number->value;
+}
+
+GtkCssValue *
+_gtk_css_number_value_compute (GtkCssValue     *number,
+                               GtkStyleContext *context)
+{
+  g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, NULL);
+
+  switch (number->unit)
+    {
+    default:
+      g_assert_not_reached();
+      /* fall through */
+    case GTK_CSS_PERCENT:
+    case GTK_CSS_NUMBER:
+    case GTK_CSS_PX:
+    case GTK_CSS_DEG:
+      return _gtk_css_value_ref (number);
+    case GTK_CSS_PT:
+      return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
+                                        GTK_CSS_PX);
+    case GTK_CSS_PC:
+      return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
+                                        GTK_CSS_PX);
+      break;
+    case GTK_CSS_IN:
+      return _gtk_css_number_value_new (number->value * 96.0,
+                                        GTK_CSS_PX);
+      break;
+    case GTK_CSS_CM:
+      return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
+                                        GTK_CSS_PX);
+      break;
+    case GTK_CSS_MM:
+      return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
+                                        GTK_CSS_PX);
+      break;
+    case GTK_CSS_EM:
+      return _gtk_css_number_value_new (number->value * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size")),
+                                        GTK_CSS_PX);
+      break;
+    case GTK_CSS_EX:
+      /* for now we pretend ex is half of em */
+      return _gtk_css_number_value_new (number->value * 0.5 * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size")),
+                                        GTK_CSS_PX);
+    case GTK_CSS_RAD:
+      return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
+                                        GTK_CSS_DEG);
+    case GTK_CSS_GRAD:
+      return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
+                                        GTK_CSS_DEG);
+    case GTK_CSS_TURN:
+      return _gtk_css_number_value_new (number->value * 360.0,
+                                        GTK_CSS_DEG);
+    }
+}
+
diff --git a/gtk/gtkcssnumbervalueprivate.h b/gtk/gtkcssnumbervalueprivate.h
new file mode 100644
index 0000000..a864d44
--- /dev/null
+++ b/gtk/gtkcssnumbervalueprivate.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright  2012 Red Hat Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Alexander Larsson <alexl gnome org>
+ */
+
+#ifndef __GTK_CSS_NUMBER_VALUE_PRIVATE_H__
+#define __GTK_CSS_NUMBER_VALUE_PRIVATE_H__
+
+#include "gtkcssparserprivate.h"
+#include "gtkcsstypesprivate.h"
+#include "gtkcssvalueprivate.h"
+
+G_BEGIN_DECLS
+
+GtkCssValue *   _gtk_css_number_value_new           (double                  value,
+                                                     GtkCssUnit              unit);
+GtkCssValue *   _gtk_css_number_value_parse         (GtkCssParser           *parser,
+                                                     GtkCssNumberParseFlags  flags);
+
+double          _gtk_css_number_value_get           (const GtkCssValue      *number,
+                                                     double                  one_hundred_percent);
+GtkCssValue *   _gtk_css_number_value_compute       (GtkCssValue            *number,
+                                                     GtkStyleContext        *context);
+
+
+G_END_DECLS
+
+#endif /* __GTK_CSS_NUMBER_VALUE_PRIVATE_H__ */
diff --git a/gtk/gtkcssshorthandpropertyimpl.c b/gtk/gtkcssshorthandpropertyimpl.c
index 8e0bdf0..b5d8dcc 100644
--- a/gtk/gtkcssshorthandpropertyimpl.c
+++ b/gtk/gtkcssshorthandpropertyimpl.c
@@ -25,13 +25,14 @@
 #include <math.h>
 
 #include "gtkcssimageprivate.h"
+#include "gtkcssnumbervalueprivate.h"
 #include "gtkcssstylefuncsprivate.h"
 #include "gtkcsstypesprivate.h"
+#include "gtkcssvalueprivate.h"
 #include "gtkprivatetypebuiltins.h"
 #include "gtkstylepropertiesprivate.h"
 #include "gtksymboliccolorprivate.h"
 #include "gtktypebuiltins.h"
-#include "gtkcssvalueprivate.h"
 
 /* this is in case round() is not provided by the compiler, 
  * such as in the case of C89 compilers, like MSVC
@@ -54,7 +55,6 @@ parse_four_numbers (GtkCssShorthandProperty  *shorthand,
                     GtkCssParser             *parser,
                     GtkCssNumberParseFlags    flags)
 {
-  GtkCssNumber numbers[4];
   guint i;
 
   for (i = 0; i < 4; i++)
@@ -62,9 +62,8 @@ parse_four_numbers (GtkCssShorthandProperty  *shorthand,
       if (!_gtk_css_parser_has_number (parser))
         break;
 
-      if (!_gtk_css_parser_read_number (parser,
-                                        &numbers[i], 
-                                        flags))
+      values[i] = _gtk_css_number_value_parse (parser, flags);
+      if (values[i] == NULL)
         return FALSE;
     }
 
@@ -76,12 +75,7 @@ parse_four_numbers (GtkCssShorthandProperty  *shorthand,
 
   for (; i < 4; i++)
     {
-      numbers[i] = numbers[(i - 1) >> 1];
-    }
-
-  for (i = 0; i < 4; i++)
-    {
-      values[i] = _gtk_css_value_new_from_number (&numbers[i]);
+      values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
     }
 
   return TRUE;
@@ -330,15 +324,12 @@ parse_border_side (GtkCssShorthandProperty  *shorthand,
     if (values[0] == NULL &&
          _gtk_css_parser_has_number (parser))
       {
-        GtkCssNumber number;
-        if (!_gtk_css_parser_read_number (parser,
-                                          &number,
-                                          GTK_CSS_POSITIVE_ONLY
-                                          | GTK_CSS_NUMBER_AS_PIXELS
-                                          | GTK_CSS_PARSE_LENGTH))
+        values[0] = _gtk_css_number_value_parse (parser,
+                                                 GTK_CSS_POSITIVE_ONLY
+                                                 | GTK_CSS_NUMBER_AS_PIXELS
+                                                 | GTK_CSS_PARSE_LENGTH);
+        if (values[0] == NULL)
           return FALSE;
-
-        values[0] = _gtk_css_value_new_from_number (&number);
       }
     else if (values[1] == NULL &&
              _gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &style))
@@ -381,15 +372,12 @@ parse_border (GtkCssShorthandProperty  *shorthand,
     if (values[0] == NULL &&
          _gtk_css_parser_has_number (parser))
       {
-        GtkCssNumber number;
-        if (!_gtk_css_parser_read_number (parser,
-                                          &number,
-                                          GTK_CSS_POSITIVE_ONLY
-                                          | GTK_CSS_NUMBER_AS_PIXELS
-                                          | GTK_CSS_PARSE_LENGTH))
+        values[0] = _gtk_css_number_value_parse (parser,
+                                                 GTK_CSS_POSITIVE_ONLY
+                                                 | GTK_CSS_NUMBER_AS_PIXELS
+                                                 | GTK_CSS_PARSE_LENGTH);
+        if (values[0] == NULL)
           return FALSE;
-
-        values[0] = _gtk_css_value_new_from_number (&number);
         values[1] = _gtk_css_value_ref (values[0]);
         values[2] = _gtk_css_value_ref (values[0]);
         values[3] = _gtk_css_value_ref (values[0]);
diff --git a/gtk/gtkcssstylefuncs.c b/gtk/gtkcssstylefuncs.c
index 6a0bed7..f716e59 100644
--- a/gtk/gtkcssstylefuncs.c
+++ b/gtk/gtkcssstylefuncs.c
@@ -935,13 +935,6 @@ border_image_repeat_value_print (const GValue *value,
     }
 }
 
-static void
-css_number_print (const GValue *value,
-                  GString      *string)
-{
-  _gtk_css_number_print (g_value_get_boxed (value), string);
-}
-
 static gboolean 
 enum_value_parse (GtkCssParser *parser,
                   GFile        *base,
@@ -1113,10 +1106,6 @@ gtk_css_style_funcs_init (void)
                                 border_image_repeat_value_parse,
                                 border_image_repeat_value_print,
                                 NULL);
-  register_conversion_function (GTK_TYPE_CSS_NUMBER,
-                                NULL,
-                                css_number_print,
-                                NULL);
   register_conversion_function (G_TYPE_ENUM,
                                 enum_value_parse,
                                 enum_value_print,
diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c
index 8bfc4c8..7861968 100644
--- a/gtk/gtkcssstylepropertyimpl.c
+++ b/gtk/gtkcssstylepropertyimpl.c
@@ -43,6 +43,7 @@
 #include "gtkcssimagegradientprivate.h"
 #include "gtkcssimageprivate.h"
 #include "gtkcssimageprivate.h"
+#include "gtkcssnumbervalueprivate.h"
 #include "gtkgradient.h"
 #include "gtkshadowprivate.h"
 #include "gtksymboliccolorprivate.h"
@@ -143,7 +144,7 @@ query_length_as_int (GtkCssStyleProperty *property,
                      GValue              *value)
 {
   g_value_init (value, G_TYPE_INT);
-  g_value_set_int (value, round (_gtk_css_number_get (_gtk_css_value_get_number (css_value), 100)));
+  g_value_set_int (value, round (_gtk_css_number_value_get (css_value, 100)));
 }
 
 static GtkCssValue *
@@ -804,15 +805,9 @@ parse_margin (GtkCssStyleProperty *property,
               GtkCssParser        *parser,
               GFile               *base)
 {
-  GtkCssNumber number;
-
-  if (!_gtk_css_parser_read_number (parser,
-                                    &number, 
-                                    GTK_CSS_NUMBER_AS_PIXELS
-                                    | GTK_CSS_PARSE_LENGTH))
-    return NULL;
-
-  return _gtk_css_value_new_from_number (&number);
+  return _gtk_css_number_value_parse (parser,
+                                      GTK_CSS_NUMBER_AS_PIXELS
+                                      | GTK_CSS_PARSE_LENGTH);
 }
 
 static GtkCssValue *
@@ -820,15 +815,7 @@ compute_margin (GtkCssStyleProperty *property,
                 GtkStyleContext     *context,
                 GtkCssValue         *specified)
 {
-  GtkCssNumber number;
-  
-  if (_gtk_css_number_compute (&number,
-			       _gtk_css_value_get_number (specified),
-			       context))
-    {
-      return _gtk_css_value_new_from_number (&number);
-    }
-  return  _gtk_css_value_ref (specified);
+  return _gtk_css_number_value_compute (specified, context);
 }
 
 static GtkCssValue *
@@ -836,16 +823,10 @@ parse_padding (GtkCssStyleProperty *property,
                GtkCssParser        *parser,
                GFile               *base)
 {
-  GtkCssNumber number;
-
-  if (!_gtk_css_parser_read_number (parser,
-                                    &number, 
-                                    GTK_CSS_POSITIVE_ONLY
-                                    | GTK_CSS_NUMBER_AS_PIXELS
-                                    | GTK_CSS_PARSE_LENGTH))
-    return NULL;
-
-  return _gtk_css_value_new_from_number (&number);
+  return _gtk_css_number_value_parse (parser,
+                                      GTK_CSS_POSITIVE_ONLY
+                                      | GTK_CSS_NUMBER_AS_PIXELS
+                                      | GTK_CSS_PARSE_LENGTH);
 }
 
 static GtkCssValue *
@@ -853,13 +834,7 @@ compute_padding (GtkCssStyleProperty *property,
                  GtkStyleContext     *context,
                  GtkCssValue         *specified)
 {
-  GtkCssNumber number;
-
-  if (_gtk_css_number_compute (&number,
-			       _gtk_css_value_get_number (specified),
-			       context))
-    return _gtk_css_value_new_from_number (&number);
-  return _gtk_css_value_ref (specified);
+  return _gtk_css_number_value_compute (specified, context);
 }
 
 static GtkCssValue *
@@ -867,16 +842,10 @@ parse_border_width (GtkCssStyleProperty *property,
                     GtkCssParser        *parser,
                     GFile               *base)
 {
-  GtkCssNumber number;
-
-  if (!_gtk_css_parser_read_number (parser,
-                                    &number, 
-                                    GTK_CSS_POSITIVE_ONLY
-                                    | GTK_CSS_NUMBER_AS_PIXELS
-                                    | GTK_CSS_PARSE_LENGTH))
-    return FALSE;
-
-  return _gtk_css_value_new_from_number (&number);
+  return _gtk_css_number_value_parse (parser,
+                                      GTK_CSS_POSITIVE_ONLY
+                                      | GTK_CSS_NUMBER_AS_PIXELS
+                                      | GTK_CSS_PARSE_LENGTH);
 }
 
 static GtkCssValue *
@@ -886,7 +855,6 @@ compute_border_width (GtkCssStyleProperty    *property,
 {
   GtkCssStyleProperty *style;
   GtkBorderStyle border_style;
-  GtkCssNumber number;
   
   /* The -1 is magic that is only true because we register the style
    * properties directly after the width properties.
@@ -897,16 +865,9 @@ compute_border_width (GtkCssStyleProperty    *property,
 
   if (border_style == GTK_BORDER_STYLE_NONE ||
       border_style == GTK_BORDER_STYLE_HIDDEN)
-    {
-      _gtk_css_number_init (&number, 0, GTK_CSS_PX);
-    }
+    return _gtk_css_number_value_new (0, GTK_CSS_PX);
   else
-    {
-      _gtk_css_number_compute (&number,
-			       _gtk_css_value_get_number (specified),
-			       context);
-    }
-  return _gtk_css_value_new_from_number (&number);
+    return _gtk_css_number_value_compute (specified, context);
 }
 
 static GtkCssValue *
@@ -1251,7 +1212,6 @@ void
 _gtk_css_style_property_init_properties (void)
 {
   char *default_font_family[] = { "Sans", NULL };
-  GtkCssNumber number;
   GtkCssBackgroundSize default_background_size = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), FALSE, FALSE };
   GtkCssBackgroundPosition default_background_position = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PERCENT), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PERCENT)};
   GtkCssBorderCornerRadius no_corner_radius = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX) };
@@ -1366,7 +1326,6 @@ _gtk_css_style_property_init_properties (void)
                                           NULL,
                                           _gtk_css_value_new_take_shadow (NULL));
 
-  _gtk_css_number_init (&number, 0, GTK_CSS_PX);
   gtk_css_style_property_register        ("margin-top",
                                           G_TYPE_INT,
                                           0,
@@ -1375,7 +1334,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_margin,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("margin-left",
                                           G_TYPE_INT,
                                           0,
@@ -1384,7 +1343,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_margin,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("margin-bottom",
                                           G_TYPE_INT,
                                           0,
@@ -1393,7 +1352,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_margin,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("margin-right",
                                           G_TYPE_INT,
                                           0,
@@ -1402,7 +1361,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_margin,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("padding-top",
                                           G_TYPE_INT,
                                           0,
@@ -1411,7 +1370,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_padding,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("padding-left",
                                           G_TYPE_INT,
                                           0,
@@ -1420,7 +1379,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_padding,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("padding-bottom",
                                           G_TYPE_INT,
                                           0,
@@ -1429,7 +1388,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_padding,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("padding-right",
                                           G_TYPE_INT,
                                           0,
@@ -1438,7 +1397,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_padding,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   /* IMPORTANT: compute_border_width() requires that the border-width
    * properties be immeditaly followed by the border-style properties
    */
@@ -1459,7 +1418,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_border_width,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("border-left-style",
                                           GTK_TYPE_BORDER_STYLE,
                                           0,
@@ -1477,7 +1436,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_border_width,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("border-bottom-style",
                                           GTK_TYPE_BORDER_STYLE,
                                           0,
@@ -1495,7 +1454,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_border_width,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("border-right-style",
                                           GTK_TYPE_BORDER_STYLE,
                                           0,
@@ -1513,7 +1472,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_border_width,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
 
   gtk_css_style_property_register        ("border-top-left-radius",
                                           GTK_TYPE_CSS_BORDER_CORNER_RADIUS,
@@ -1569,7 +1528,7 @@ _gtk_css_style_property_init_properties (void)
                                           compute_border_width,
                                           query_length_as_int,
                                           NULL,
-                                          _gtk_css_value_new_from_number (&number));
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
   gtk_css_style_property_register        ("outline-offset",
                                           G_TYPE_INT,
                                           0,
diff --git a/gtk/gtkcsstypes.c b/gtk/gtkcsstypes.c
index 0133c72..a47f8d3 100644
--- a/gtk/gtkcsstypes.c
+++ b/gtk/gtkcsstypes.c
@@ -34,7 +34,6 @@ DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBackgroundSize, _gtk_css_background_size
 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBackgroundPosition, _gtk_css_background_position)
 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderCornerRadius, _gtk_css_border_corner_radius)
 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderImageRepeat, _gtk_css_border_image_repeat)
-DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssNumber, _gtk_css_number)
 
 typedef struct _GtkCssChangeTranslation GtkCssChangeTranslation;
 struct _GtkCssChangeTranslation {
diff --git a/gtk/gtkcsstypesprivate.h b/gtk/gtkcsstypesprivate.h
index 62bd29d..08e18ee 100644
--- a/gtk/gtkcsstypesprivate.h
+++ b/gtk/gtkcsstypesprivate.h
@@ -160,13 +160,11 @@ struct _GtkCssBorderImageRepeat {
 #define GTK_TYPE_CSS_BACKGROUND_POSITION _gtk_css_background_position_get_type ()
 #define GTK_TYPE_CSS_BORDER_CORNER_RADIUS _gtk_css_border_corner_radius_get_type ()
 #define GTK_TYPE_CSS_BORDER_IMAGE_REPEAT _gtk_css_border_image_repeat_get_type ()
-#define GTK_TYPE_CSS_NUMBER _gtk_css_number_get_type ()
 
 GType           _gtk_css_background_size_get_type               (void);
 GType           _gtk_css_background_position_get_type           (void);
 GType           _gtk_css_border_corner_radius_get_type          (void);
 GType           _gtk_css_border_image_repeat_get_type           (void);
-GType           _gtk_css_number_get_type                        (void);
 
 GtkCssChange    _gtk_css_change_for_sibling                      (GtkCssChange       match);
 GtkCssChange    _gtk_css_change_for_child                        (GtkCssChange       match);
diff --git a/gtk/gtkcssvalue.c b/gtk/gtkcssvalue.c
index 5d0f44b..4302820 100644
--- a/gtk/gtkcssvalue.c
+++ b/gtk/gtkcssvalue.c
@@ -105,8 +105,6 @@ _gtk_css_value_new_from_gvalue (const GValue *g_value)
   /* Make sure we reuse the int/number singletons */
   if (type == G_TYPE_INT)
     value = _gtk_css_value_new_from_int (g_value_get_int (g_value));
-  else if (type == GTK_TYPE_CSS_NUMBER)
-    value = _gtk_css_value_new_from_number (g_value_get_boxed (g_value));
   else
     {
       value = gtk_css_value_new (type);
@@ -287,49 +285,6 @@ _gtk_css_value_new_take_binding_sets (GPtrArray *array)
 }
 
 GtkCssValue *
-_gtk_css_value_new_from_number (const GtkCssNumber *v)
-{
-  GtkCssValue *value;
-  static GtkCssValue *zero_singleton = NULL;
-  static GtkCssValue *px_singletons[5] = {NULL};
-
-  if (v->unit == GTK_CSS_NUMBER &&
-      v->value == 0)
-    {
-      if (zero_singleton == NULL)
-	{
-	  value = gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
-	  value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
-	  zero_singleton = value;
-	}
-      return _gtk_css_value_ref (zero_singleton);
-    }
-
-  if (v->unit == GTK_CSS_PX &&
-      (v->value == 0 ||
-       v->value == 1 ||
-       v->value == 2 ||
-       v->value == 3 ||
-       v->value == 4))
-    {
-      int i = round (v->value);
-      if (px_singletons[i] == NULL)
-	{
-	  value = gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
-	  value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
-	  px_singletons[i] = value;
-	}
-
-      return _gtk_css_value_ref (px_singletons[i]);
-    }
-
-  value = gtk_css_value_new (GTK_TYPE_CSS_NUMBER);
-  value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_NUMBER, v);
-
-  return value;
-}
-
-GtkCssValue *
 _gtk_css_value_new_from_rgba (const GdkRGBA *v)
 {
   GtkCssValue *value;
@@ -531,13 +486,6 @@ _gtk_css_value_init_gvalue (const GtkCssValue *value,
     }
 }
 
-const GtkCssNumber *
-_gtk_css_value_get_number (const GtkCssValue *value)
-{
-  g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_NUMBER), NULL);
-  return value->u.ptr;
-}
-
 GtkSymbolicColor *
 _gtk_css_value_get_symbolic_color (const GtkCssValue *value)
 {
diff --git a/gtk/gtkcssvalueprivate.h b/gtk/gtkcssvalueprivate.h
index f30385a..e962326 100644
--- a/gtk/gtkcssvalueprivate.h
+++ b/gtk/gtkcssvalueprivate.h
@@ -88,7 +88,6 @@ GtkCssValue *_gtk_css_value_new_take_pattern          (cairo_pattern_t
 GtkCssValue *_gtk_css_value_new_take_shadow           (GtkShadow                  *v);
 GtkCssValue *_gtk_css_value_new_take_image            (GtkCssImage                *v);
 GtkCssValue *_gtk_css_value_new_from_theming_engine   (GtkThemingEngine           *v);
-GtkCssValue *_gtk_css_value_new_from_number           (const GtkCssNumber         *v);
 GtkCssValue *_gtk_css_value_new_take_binding_sets     (GPtrArray                  *array);
 GtkCssValue *_gtk_css_value_new_from_background_size  (const GtkCssBackgroundSize *v);
 GtkCssValue *_gtk_css_value_new_from_background_position (const GtkCssBackgroundPosition *v);
@@ -106,7 +105,6 @@ gpointer                        _gtk_css_value_dup_object                 (const
 gpointer                        _gtk_css_value_get_object                 (const GtkCssValue *value);
 gpointer                        _gtk_css_value_get_boxed                  (const GtkCssValue *value);
 const char **                   _gtk_css_value_get_strv                   (const GtkCssValue *value);
-const GtkCssNumber             *_gtk_css_value_get_number                 (const GtkCssValue *value);
 GtkSymbolicColor               *_gtk_css_value_get_symbolic_color         (const GtkCssValue *value);
 GtkCssImage                    *_gtk_css_value_get_image                  (const GtkCssValue *value);
 GtkBorderStyle                  _gtk_css_value_get_border_style           (const GtkCssValue *value);
diff --git a/gtk/gtkstylecontext.c b/gtk/gtkstylecontext.c
index f2bb8eb..cab170d 100644
--- a/gtk/gtkstylecontext.c
+++ b/gtk/gtkstylecontext.c
@@ -32,6 +32,7 @@
 #include "gtkprivate.h"
 #include "gtksymboliccolorprivate.h"
 #include "gtkanimationdescription.h"
+#include "gtkcssnumbervalueprivate.h"
 #include "gtktimeline.h"
 #include "gtkiconfactory.h"
 #include "gtkwidgetpath.h"
@@ -2264,7 +2265,7 @@ _gtk_style_context_get_number (GtkStyleContext *context,
   GtkCssValue *value;
   
   value = _gtk_style_context_peek_property (context, property_name);
-  return _gtk_css_number_get (_gtk_css_value_get_number (value), one_hundred_percent);
+  return _gtk_css_number_value_get (value, one_hundred_percent);
 }
 
 const GValue *



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