[gtk+/wip/cssvalue: 75/137] cssvalue: Make border styles be their own value



commit 7d808b9e15711068026d15c30067728cac089770
Author: Benjamin Otte <otte redhat com>
Date:   Wed Mar 28 08:19:53 2012 +0200

    cssvalue: Make border styles be their own value
    
    ... and add them via gtkcssenumvalue.[ch] which will be used for all
    enums.

 gtk/Makefile.am                   |    2 +
 gtk/gtkcssenumvalue.c             |  104 +++++++++++++++++++++++++++++++++++++
 gtk/gtkcssenumvalueprivate.h      |   36 +++++++++++++
 gtk/gtkcssshorthandpropertyimpl.c |   25 +++------
 gtk/gtkcssstylepropertyimpl.c     |   60 +++++++++++++--------
 gtk/gtkcssvalueprivate.h          |    2 -
 gtk/gtkthemingengine.c            |    3 +-
 7 files changed, 189 insertions(+), 43 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index c2d7685..1532f00 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -426,6 +426,7 @@ gtk_private_h_sources =		\
 	gtkcssarrayvalueprivate.h	\
 	gtkcsscomputedvaluesprivate.h \
 	gtkcsscustompropertyprivate.h \
+	gtkcssenumvalueprivate.h	\
 	gtkcssimagegradientprivate.h	\
 	gtkcssimagelinearprivate.h	\
 	gtkcssimageprivate.h	\
@@ -628,6 +629,7 @@ gtk_base_c_sources = 		\
 	gtkcssarrayvalue.c	\
 	gtkcsscomputedvalues.c	\
 	gtkcsscustomproperty.c	\
+	gtkcssenumvalue.c	\
 	gtkcssimage.c		\
 	gtkcssimagegradient.c	\
 	gtkcssimagelinear.c	\
diff --git a/gtk/gtkcssenumvalue.c b/gtk/gtkcssenumvalue.c
new file mode 100644
index 0000000..5f59cdd
--- /dev/null
+++ b/gtk/gtkcssenumvalue.c
@@ -0,0 +1,104 @@
+/* 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 "gtkcssenumvalueprivate.h"
+
+#include "gtkstylepropertyprivate.h"
+
+/* repeated API */
+
+struct _GtkCssValue {
+  GTK_CSS_VALUE_BASE
+  int value;
+  const char *name;
+};
+
+static void
+gtk_css_value_enum_free (GtkCssValue *value)
+{
+  g_slice_free (GtkCssValue, value);
+}
+
+static gboolean
+gtk_css_value_enum_equal (const GtkCssValue *enum1,
+                          const GtkCssValue *enum2)
+{
+  return enum1 == enum2;
+}
+
+static void
+gtk_css_value_enum_print (const GtkCssValue *value,
+                          GString           *string)
+{
+  g_string_append (string, value->name);
+}
+
+/* GtkBorderStyle */
+
+static const GtkCssValueClass GTK_CSS_VALUE_BORDER_STYLE = {
+  gtk_css_value_enum_free,
+  gtk_css_value_enum_equal,
+  gtk_css_value_enum_print
+};
+
+static GtkCssValue border_style_values[] = {
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_NONE, "none" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_SOLID, "solid" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_INSET, "inset" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_OUTSET, "outset" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_HIDDEN, "hidden" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_DOTTED, "dotted" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_DASHED, "dashed" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_DOUBLE, "double" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_GROOVE, "groove" },
+  { &GTK_CSS_VALUE_BORDER_STYLE, 1, GTK_BORDER_STYLE_RIDGE, "ridge" }
+};
+
+GtkCssValue *
+_gtk_css_border_style_value_new (GtkBorderStyle border_style)
+{
+  g_return_val_if_fail (border_style < G_N_ELEMENTS (border_style_values), NULL);
+
+  return _gtk_css_value_ref (&border_style_values[border_style]);
+}
+
+GtkCssValue *
+_gtk_css_border_style_value_try_parse (GtkCssParser *parser)
+{
+  guint i;
+
+  g_return_val_if_fail (parser != NULL, NULL);
+
+  for (i = 0; i < G_N_ELEMENTS (border_style_values); i++)
+    {
+      if (_gtk_css_parser_try (parser, border_style_values[i].name, TRUE))
+        return _gtk_css_value_ref (&border_style_values[i]);
+    }
+
+  return NULL;
+}
+
+GtkBorderStyle
+_gtk_css_border_style_value_get (const GtkCssValue *value)
+{
+  g_return_val_if_fail (value->class == &GTK_CSS_VALUE_BORDER_STYLE, GTK_BORDER_STYLE_NONE);
+
+  return value->value;
+}
+
diff --git a/gtk/gtkcssenumvalueprivate.h b/gtk/gtkcssenumvalueprivate.h
new file mode 100644
index 0000000..8801bf4
--- /dev/null
+++ b/gtk/gtkcssenumvalueprivate.h
@@ -0,0 +1,36 @@
+/*
+ * 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_ENUM_VALUE_PRIVATE_H__
+#define __GTK_CSS_ENUM_VALUE_PRIVATE_H__
+
+#include "gtkenums.h"
+#include "gtkcssparserprivate.h"
+#include "gtkcssvalueprivate.h"
+
+G_BEGIN_DECLS
+
+GtkCssValue *   _gtk_css_border_style_value_new       (GtkBorderStyle     border_style);
+GtkCssValue *   _gtk_css_border_style_value_try_parse (GtkCssParser      *parser);
+GtkBorderStyle  _gtk_css_border_style_value_get       (const GtkCssValue *value);
+
+
+G_END_DECLS
+
+#endif /* __GTK_CSS_ENUM_VALUE_PRIVATE_H__ */
diff --git a/gtk/gtkcssshorthandpropertyimpl.c b/gtk/gtkcssshorthandpropertyimpl.c
index b5d8dcc..cb12522 100644
--- a/gtk/gtkcssshorthandpropertyimpl.c
+++ b/gtk/gtkcssshorthandpropertyimpl.c
@@ -24,6 +24,7 @@
 #include <cairo-gobject.h>
 #include <math.h>
 
+#include "gtkcssenumvalueprivate.h"
 #include "gtkcssimageprivate.h"
 #include "gtkcssnumbervalueprivate.h"
 #include "gtkcssstylefuncsprivate.h"
@@ -236,12 +237,12 @@ parse_border_style (GtkCssShorthandProperty  *shorthand,
                     GtkCssParser             *parser,
                     GFile                    *base)
 {
-  GtkBorderStyle styles[4];
   guint i;
 
   for (i = 0; i < 4; i++)
     {
-      if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, (int *)&styles[i]))
+      values[i] = _gtk_css_border_style_value_try_parse (parser);
+      if (values[i] == NULL)
         break;
     }
 
@@ -251,13 +252,8 @@ parse_border_style (GtkCssShorthandProperty  *shorthand,
       return FALSE;
     }
 
-  for (; i < G_N_ELEMENTS (styles); i++)
-    styles[i] = styles[(i - 1) >> 1];
-
-  for (i = 0; i < G_N_ELEMENTS (styles); i++)
-    {
-      values[i] = _gtk_css_value_new_from_enum (GTK_TYPE_BORDER_STYLE, styles[i]);
-    }
+  for (; i < 4; i++)
+    values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
 
   return TRUE;
 }
@@ -317,8 +313,6 @@ parse_border_side (GtkCssShorthandProperty  *shorthand,
                    GtkCssParser             *parser,
                    GFile                    *base)
 {
-  int style;
-
   do
   {
     if (values[0] == NULL &&
@@ -332,9 +326,9 @@ parse_border_side (GtkCssShorthandProperty  *shorthand,
           return FALSE;
       }
     else if (values[1] == NULL &&
-             _gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &style))
+             (values[1] = _gtk_css_border_style_value_try_parse (parser)))
       {
-        values[1] = _gtk_css_value_new_from_enum (GTK_TYPE_BORDER_STYLE, style);
+        /* Nothing to do */
       }
     else if (values[2] == NULL)
       {
@@ -365,8 +359,6 @@ parse_border (GtkCssShorthandProperty  *shorthand,
               GtkCssParser             *parser,
               GFile                    *base)
 {
-  int style;
-
   do
   {
     if (values[0] == NULL &&
@@ -383,9 +375,8 @@ parse_border (GtkCssShorthandProperty  *shorthand,
         values[3] = _gtk_css_value_ref (values[0]);
       }
     else if (values[4] == NULL &&
-             _gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &style))
+             (values[4] = _gtk_css_border_style_value_try_parse (parser)))
       {
-        values[4] = _gtk_css_value_new_from_enum (GTK_TYPE_BORDER_STYLE, style);
         values[5] = _gtk_css_value_ref (values[4]);
         values[6] = _gtk_css_value_ref (values[4]);
         values[7] = _gtk_css_value_ref (values[4]);
diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c
index 88874c3..cad536e 100644
--- a/gtk/gtkcssstylepropertyimpl.c
+++ b/gtk/gtkcssstylepropertyimpl.c
@@ -43,6 +43,7 @@
 #include "gtkcssimagegradientprivate.h"
 #include "gtkcssimageprivate.h"
 #include "gtkcssimageprivate.h"
+#include "gtkcssenumvalueprivate.h"
 #include "gtkcssnumbervalueprivate.h"
 #include "gtkcssrgbavalueprivate.h"
 #include "gtkcssshadowvalueprivate.h"
@@ -348,15 +349,28 @@ parse_border_style (GtkCssStyleProperty *property,
                     GtkCssParser        *parser,
                     GFile               *base)
 {
-  int value;
+  GtkCssValue *value = _gtk_css_border_style_value_try_parse (parser);
+  
+  if (value == NULL)
+    _gtk_css_parser_error (parser, "unknown value for property");
 
-  if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, &value))
-    {
-      _gtk_css_parser_error (parser, "unknown value for property");
-      return NULL;
-    }
+  return value;
+}
+
+static void
+query_border_style (GtkCssStyleProperty *property,
+                    const GtkCssValue   *css_value,
+                    GValue              *value)
+{
+  g_value_init (value, GTK_TYPE_BORDER_STYLE);
+  g_value_set_enum (value, _gtk_css_border_style_value_get (css_value));
+}
 
-  return _gtk_css_value_new_from_enum (GTK_TYPE_BORDER_STYLE, value);
+static GtkCssValue *
+assign_border_style (GtkCssStyleProperty *property,
+                     const GValue        *value)
+{
+  return _gtk_css_border_style_value_new (g_value_get_enum (value));
 }
 
 static GtkCssValue *
@@ -785,7 +799,7 @@ compute_border_width (GtkCssStyleProperty    *property,
    */
   style = _gtk_css_style_property_lookup_by_id (_gtk_css_style_property_get_id (property) - 1);
   
-  border_style = _gtk_css_value_get_border_style (_gtk_style_context_peek_property (context, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (style))));
+  border_style = _gtk_css_border_style_value_get (_gtk_style_context_peek_property (context, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (style))));
 
   if (border_style == GTK_BORDER_STYLE_NONE ||
       border_style == GTK_BORDER_STYLE_HIDDEN)
@@ -1349,10 +1363,10 @@ _gtk_css_style_property_init_properties (void)
                                           parse_border_style,
                                           NULL,
                                           NULL,
-                                          query_simple,
-                                          assign_simple,
+                                          query_border_style,
+                                          assign_border_style,
                                           NULL,
-                                          _gtk_css_value_new_from_border_style (GTK_BORDER_STYLE_NONE));
+                                          _gtk_css_border_style_value_new (GTK_BORDER_STYLE_NONE));
   gtk_css_style_property_register        ("border-top-width",
                                           G_TYPE_INT,
                                           0,
@@ -1369,10 +1383,10 @@ _gtk_css_style_property_init_properties (void)
                                           parse_border_style,
                                           NULL,
                                           NULL,
-                                          query_simple,
-                                          assign_simple,
+                                          query_border_style,
+                                          assign_border_style,
                                           NULL,
-                                          _gtk_css_value_new_from_border_style (GTK_BORDER_STYLE_NONE));
+                                          _gtk_css_border_style_value_new (GTK_BORDER_STYLE_NONE));
   gtk_css_style_property_register        ("border-left-width",
                                           G_TYPE_INT,
                                           0,
@@ -1389,10 +1403,10 @@ _gtk_css_style_property_init_properties (void)
                                           parse_border_style,
                                           NULL,
                                           NULL,
-                                          query_simple,
-                                          assign_simple,
+                                          query_border_style,
+                                          assign_border_style,
                                           NULL,
-                                          _gtk_css_value_new_from_border_style (GTK_BORDER_STYLE_NONE));
+                                          _gtk_css_border_style_value_new (GTK_BORDER_STYLE_NONE));
   gtk_css_style_property_register        ("border-bottom-width",
                                           G_TYPE_INT,
                                           0,
@@ -1409,10 +1423,10 @@ _gtk_css_style_property_init_properties (void)
                                           parse_border_style,
                                           NULL,
                                           NULL,
-                                          query_simple,
-                                          assign_simple,
+                                          query_border_style,
+                                          assign_border_style,
                                           NULL,
-                                          _gtk_css_value_new_from_border_style (GTK_BORDER_STYLE_NONE));
+                                          _gtk_css_border_style_value_new (GTK_BORDER_STYLE_NONE));
   gtk_css_style_property_register        ("border-right-width",
                                           G_TYPE_INT,
                                           0,
@@ -1471,10 +1485,10 @@ _gtk_css_style_property_init_properties (void)
                                           parse_border_style,
                                           NULL,
                                           NULL,
-                                          query_simple,
-                                          assign_simple,
+                                          query_border_style,
+                                          assign_border_style,
                                           NULL,
-                                          _gtk_css_value_new_from_border_style (GTK_BORDER_STYLE_NONE));
+                                          _gtk_css_border_style_value_new (GTK_BORDER_STYLE_NONE));
   gtk_css_style_property_register        ("outline-width",
                                           G_TYPE_INT,
                                           0,
diff --git a/gtk/gtkcssvalueprivate.h b/gtk/gtkcssvalueprivate.h
index 3bedaf4..f8699a7 100644
--- a/gtk/gtkcssvalueprivate.h
+++ b/gtk/gtkcssvalueprivate.h
@@ -91,7 +91,6 @@ GtkCssValue *_gtk_css_value_new_from_background_size  (const GtkCssBackgroundSiz
 GtkCssValue *_gtk_css_value_new_from_background_position (const GtkCssBackgroundPosition *v);
 GtkCssValue *_gtk_css_value_new_from_border_corner_radius (const GtkCssBorderCornerRadius *v);
 GtkCssValue *_gtk_css_value_new_from_border_image_repeat (const GtkCssBorderImageRepeat *v);
-GtkCssValue *_gtk_css_value_new_from_border_style     (GtkBorderStyle              style);
 void         _gtk_css_value_init_gvalue               (const GtkCssValue          *value,
 						       GValue                     *g_value);
 
@@ -105,7 +104,6 @@ gpointer                        _gtk_css_value_get_boxed                  (const
 const char **                   _gtk_css_value_get_strv                   (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);
 const GtkCssBackgroundSize     *_gtk_css_value_get_background_size        (const GtkCssValue *value);
 const GtkCssBackgroundPosition *_gtk_css_value_get_background_position    (const GtkCssValue *value);
 const GtkCssBorderCornerRadius *_gtk_css_value_get_border_corner_radius   (const GtkCssValue *value);
diff --git a/gtk/gtkthemingengine.c b/gtk/gtkthemingengine.c
index 4747f9d..aad10ac 100644
--- a/gtk/gtkthemingengine.c
+++ b/gtk/gtkthemingengine.c
@@ -28,6 +28,7 @@
 #include "gtkmodulesprivate.h"
 #include "gtkborderimageprivate.h"
 #include "gtkpango.h"
+#include "gtkcssenumvalueprivate.h"
 #include "gtkcssrgbavalueprivate.h"
 #include "gtkcssshadowvalueprivate.h"
 #include "gtkcsstypesprivate.h"
@@ -1821,7 +1822,7 @@ render_frame_internal (GtkThemingEngine *engine,
       render_border (cr, &border_box, &border, hidden_side, colors, border_style);
     }
 
-  border_style[0] = _gtk_css_value_get_border_style (_gtk_theming_engine_peek_property (engine, "outline-style"));
+  border_style[0] = _gtk_css_border_style_value_get (_gtk_theming_engine_peek_property (engine, "outline-style"));
   if (border_style[0] != GTK_BORDER_STYLE_NONE)
     {
       int offset;



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