[gtk+] css: Implement padding as numbers



commit e84af235ee0d99134f929855cba312e4c5fe7d31
Author: Benjamin Otte <otte redhat com>
Date:   Tue Jan 24 17:49:29 2012 +0100

    css: Implement padding as numbers
    
    Also remove the now unused border parsing function for shorthands.

 gtk/gtk-default.css                               |    4 +-
 gtk/gtk-win32.css                                 |    2 +-
 gtk/gtkcssshorthandpropertyimpl.c                 |   58 ++++++-----------
 gtk/gtkcssstyleproperty.c                         |   10 +++
 gtk/gtkcssstylepropertyimpl.c                     |   73 +++++++++++++++------
 tests/reftests/background-area.css                |    2 +-
 tests/reftests/background-origin.css              |    2 +-
 tests/reftests/css-match-style-property-order.css |    2 +-
 tests/reftests/entry-progress-coloring.css        |    2 +-
 9 files changed, 90 insertions(+), 65 deletions(-)
---
diff --git a/gtk/gtk-default.css b/gtk/gtk-default.css
index 9a7fe4e..a27239e 100644
--- a/gtk/gtk-default.css
+++ b/gtk/gtk-default.css
@@ -332,7 +332,7 @@ GtkLabel:selected:focused {
 GtkCalendar.view {
   border-width: 1px;
   border-style: inset;
-  padding: 1;
+  padding: 1px;
 }
 
 GtkCalendar.view:inconsistent {
@@ -360,5 +360,5 @@ GtkCalendar.button:hover {
 
 .menu * {
   border-width: 0;
-  padding: 2;
+  padding: 2px;
 }
diff --git a/gtk/gtk-win32.css b/gtk/gtk-win32.css
index e1782fc..e1d604a 100644
--- a/gtk/gtk-win32.css
+++ b/gtk/gtk-win32.css
@@ -607,7 +607,7 @@ GtkComboBox.combobox-entry .button:insensitive {
 
 .notebook tab:active {
     background-image: -gtk-win32-theme-part(tab, 1 3, margins(0 0 -1 0));
-    padding: 4;
+    padding: 4px;
 }
 
 .notebook tab:last-child {
diff --git a/gtk/gtkcssshorthandpropertyimpl.c b/gtk/gtkcssshorthandpropertyimpl.c
index 3a012ee..cbff861 100644
--- a/gtk/gtkcssshorthandpropertyimpl.c
+++ b/gtk/gtkcssshorthandpropertyimpl.c
@@ -49,38 +49,6 @@ value_is_done_parsing (GtkCssParser *parser)
 }
 
 static gboolean
-parse_border_width (GtkCssShorthandProperty *shorthand,
-                    GValue                  *values,
-                    GtkCssParser            *parser,
-                    GFile                   *base)
-{
-  GValue temp = G_VALUE_INIT;
-  GtkBorder *border;
-
-  g_value_init (&temp, GTK_TYPE_BORDER);
-  if (!_gtk_css_style_parse_value (&temp, parser, base))
-    {
-      g_value_unset (&temp);
-      return FALSE;
-    }
-
-  border = g_value_get_boxed (&temp);
-
-  g_value_init (&values[0], G_TYPE_INT);
-  g_value_init (&values[1], G_TYPE_INT);
-  g_value_init (&values[2], G_TYPE_INT);
-  g_value_init (&values[3], G_TYPE_INT);
-  g_value_set_int (&values[0], border->top);
-  g_value_set_int (&values[1], border->right);
-  g_value_set_int (&values[2], border->bottom);
-  g_value_set_int (&values[3], border->left);
-
-  g_value_unset (&temp);
-
-  return TRUE;
-}
-
-static gboolean
 parse_four_numbers (GtkCssShorthandProperty *shorthand,
                     GValue                  *values,
                     GtkCssParser            *parser,
@@ -134,10 +102,24 @@ parse_margin (GtkCssShorthandProperty *shorthand,
 }
 
 static gboolean
-parse_border_width_really (GtkCssShorthandProperty *shorthand,
-                           GValue                  *values,
-                           GtkCssParser            *parser,
-                           GFile                   *base)
+parse_padding (GtkCssShorthandProperty *shorthand,
+               GValue                  *values,
+               GtkCssParser            *parser,
+               GFile                   *base)
+{
+  return parse_four_numbers (shorthand,
+                             values,
+                             parser,
+                             GTK_CSS_POSITIVE_ONLY
+                             | GTK_CSS_NUMBER_AS_PIXELS
+                             | GTK_CSS_PARSE_LENGTH);
+}
+
+static gboolean
+parse_border_width (GtkCssShorthandProperty *shorthand,
+                    GValue                  *values,
+                    GtkCssParser            *parser,
+                    GFile                   *base)
 {
   return parse_four_numbers (shorthand,
                              values,
@@ -923,13 +905,13 @@ _gtk_css_shorthand_property_init_properties (void)
   _gtk_css_shorthand_property_register   ("padding",
                                           GTK_TYPE_BORDER,
                                           padding_subproperties,
-                                          parse_border_width,
+                                          parse_padding,
                                           unpack_border,
                                           pack_border);
   _gtk_css_shorthand_property_register   ("border-width",
                                           GTK_TYPE_BORDER,
                                           border_width_subproperties,
-                                          parse_border_width_really,
+                                          parse_border_width,
                                           unpack_border,
                                           pack_border);
   _gtk_css_shorthand_property_register   ("border-radius",
diff --git a/gtk/gtkcssstyleproperty.c b/gtk/gtkcssstyleproperty.c
index ad713ec..6e4f23a 100644
--- a/gtk/gtkcssstyleproperty.c
+++ b/gtk/gtkcssstyleproperty.c
@@ -28,10 +28,16 @@
 #include "gtkprivatetypebuiltins.h"
 #include "gtkstylepropertiesprivate.h"
 
+#include <math.h>
 #include <cairo-gobject.h>
 #include "gtkcssimagegradientprivate.h"
 #include "gtkcssimageprivate.h"
 
+/* this is in case round() is not provided by the compiler, 
+ * such as in the case of C89 compilers, like MSVC
+ */
+#include "fallback-c89.c"
+
 enum {
   PROP_0,
   PROP_ID,
@@ -165,6 +171,10 @@ _gtk_css_style_property_query (GtkStyleProperty   *property,
               g_value_take_boxed (value, pattern);
             }
         }
+      else if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_NUMBER)
+        {
+          g_value_set_int (value, round (_gtk_css_number_get (g_value_get_boxed (val), 100)));
+        }
       else
         g_value_copy (val, value);
     }
diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c
index 529483b..74c25bc 100644
--- a/gtk/gtkcssstylepropertyimpl.c
+++ b/gtk/gtkcssstylepropertyimpl.c
@@ -444,6 +444,39 @@ compute_margin (GtkCssStyleProperty *property,
 }
 
 static gboolean 
+parse_padding (GtkCssStyleProperty *property,
+               GValue              *value,
+               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;
+
+  g_value_set_boxed (value, &number);
+  return TRUE;
+}
+
+static void
+compute_padding (GtkCssStyleProperty *property,
+                 GValue              *computed,
+                 GtkStyleContext     *context,
+                 const GValue        *specified)
+{
+  GtkCssNumber number;
+  
+  _gtk_css_number_compute (&number,
+                           g_value_get_boxed (specified),
+                           context);
+  g_value_set_boxed (computed, &number);
+}
+
+static gboolean 
 parse_border_width (GtkCssStyleProperty *property,
                     GValue              *value,
                     GtkCssParser        *parser,
@@ -815,41 +848,41 @@ _gtk_css_style_property_init_properties (void)
                                           compute_margin,
                                           &number);
   gtk_css_style_property_register        ("padding-top",
-                                          G_TYPE_INT,
-                                          G_TYPE_INT,
+                                          GTK_TYPE_CSS_NUMBER,
+                                          GTK_TYPE_CSS_NUMBER,
                                           G_TYPE_INT,
                                           0,
+                                          parse_padding,
                                           NULL,
-                                          NULL,
-                                          NULL,
-                                          0);
+                                          compute_padding,
+                                          &number);
   gtk_css_style_property_register        ("padding-left",
-                                          G_TYPE_INT,
-                                          G_TYPE_INT,
+                                          GTK_TYPE_CSS_NUMBER,
+                                          GTK_TYPE_CSS_NUMBER,
                                           G_TYPE_INT,
                                           0,
+                                          parse_padding,
                                           NULL,
-                                          NULL,
-                                          NULL,
-                                          0);
+                                          compute_padding,
+                                          &number);
   gtk_css_style_property_register        ("padding-bottom",
-                                          G_TYPE_INT,
-                                          G_TYPE_INT,
+                                          GTK_TYPE_CSS_NUMBER,
+                                          GTK_TYPE_CSS_NUMBER,
                                           G_TYPE_INT,
                                           0,
+                                          parse_padding,
                                           NULL,
-                                          NULL,
-                                          NULL,
-                                          0);
+                                          compute_padding,
+                                          &number);
   gtk_css_style_property_register        ("padding-right",
-                                          G_TYPE_INT,
-                                          G_TYPE_INT,
+                                          GTK_TYPE_CSS_NUMBER,
+                                          GTK_TYPE_CSS_NUMBER,
                                           G_TYPE_INT,
                                           0,
+                                          parse_padding,
                                           NULL,
-                                          NULL,
-                                          NULL,
-                                          0);
+                                          compute_padding,
+                                          &number);
   /* IMPORTANT: compute_border_width() requires that the border-width
    * properties be immeditaly followed by the border-style properties
    */
diff --git a/tests/reftests/background-area.css b/tests/reftests/background-area.css
index 73d6bd9..8c4e611 100644
--- a/tests/reftests/background-area.css
+++ b/tests/reftests/background-area.css
@@ -15,7 +15,7 @@ GtkButton {
   border-width: 5px;
   border-style: solid;
   border-color: rgba(255,0,0,0.6);
-  padding: 10;
+  padding: 10px;
   background-color: rgb(0,0,255);
 
   /* Make sure children of button are only offset by padding */
diff --git a/tests/reftests/background-origin.css b/tests/reftests/background-origin.css
index f665eb9..397f595 100644
--- a/tests/reftests/background-origin.css
+++ b/tests/reftests/background-origin.css
@@ -16,7 +16,7 @@ GtkButton {
   border-width: 5px;
   border-style: solid;
   border-color: rgba(255,0,0,0.5);
-  padding: 10;
+  padding: 10px;
   background-color: rgb(0,0,255);
 
   background-image: url("green-20x20.png");
diff --git a/tests/reftests/css-match-style-property-order.css b/tests/reftests/css-match-style-property-order.css
index f41a725..bd7a31a 100644
--- a/tests/reftests/css-match-style-property-order.css
+++ b/tests/reftests/css-match-style-property-order.css
@@ -8,5 +8,5 @@
 }
 
 #reference {
-  padding: 20
+  padding: 20px
 }
diff --git a/tests/reftests/entry-progress-coloring.css b/tests/reftests/entry-progress-coloring.css
index 6359b9f..f643409 100644
--- a/tests/reftests/entry-progress-coloring.css
+++ b/tests/reftests/entry-progress-coloring.css
@@ -9,7 +9,7 @@
 
 GtkEntry #padded {
   -GtkWidget-interior-focus: true;
-  padding: 10;
+  padding: 10px;
 }
 
 .progressbar {



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