[gtk+/wip/cssvalue: 119/137] cssvalue: Convert border-image-width to border value



commit 24e32c7cea003e6a54ca0714e26242ec2abe0cca
Author: Benjamin Otte <otte redhat com>
Date:   Wed Apr 4 17:42:45 2012 +0200

    cssvalue: Convert border-image-width to border value

 gtk/gtkborderimage.c              |   63 +++++++++++++++++++++----------------
 gtk/gtkborderimageprivate.h       |    3 +-
 gtk/gtkcssnumbervalue.c           |    8 +++++
 gtk/gtkcssnumbervalueprivate.h    |    1 +
 gtk/gtkcssshorthandpropertyimpl.c |   16 +++++-----
 gtk/gtkcssstylepropertyimpl.c     |   32 ++++++++----------
 6 files changed, 68 insertions(+), 55 deletions(-)
---
diff --git a/gtk/gtkborderimage.c b/gtk/gtkborderimage.c
index f75d7e0..337dba3 100644
--- a/gtk/gtkborderimage.c
+++ b/gtk/gtkborderimage.c
@@ -41,22 +41,12 @@ gboolean
 _gtk_border_image_init (GtkBorderImage   *image,
                         GtkThemingEngine *engine)
 {
-  GtkBorder *width;
-
   image->source = _gtk_css_image_value_get_image (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_SOURCE));
   if (image->source == NULL)
     return FALSE;
 
   image->slice = _gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_SLICE);
-  width = _gtk_css_value_get_boxed (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_WIDTH));
-  if (width)
-    {
-      image->width = *width;
-      image->has_width = TRUE;
-    }
-  else
-    image->has_width = FALSE;
-
+  image->width = _gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_WIDTH);
   image->repeat = _gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_BORDER_IMAGE_REPEAT);
 
   return TRUE;
@@ -69,20 +59,38 @@ struct _GtkBorderImageSliceSize {
 };
 
 static void
-gtk_border_image_compute_border_size (GtkBorderImageSliceSize sizes[3],
-                                      double                  offset,
-                                      double                  area_size,
-                                      int                     start_border,
-                                      int                     end_border)
+gtk_border_image_compute_border_size (GtkBorderImageSliceSize  sizes[3],
+                                      double                   offset,
+                                      double                   area_size,
+                                      int                      start_border_width,
+                                      int                      end_border_width,
+                                      const GtkCssValue       *start_border,
+                                      const GtkCssValue       *end_border)
 {
-  /* This code assumes area_size >= start_border + end_border */
+  double start, end;
+
+  if (_gtk_css_number_value_get_unit (start_border) == GTK_CSS_NUMBER)
+    start = start_border_width * _gtk_css_number_value_get (start_border, 100);
+  else
+    start = _gtk_css_number_value_get (start_border, area_size);
+  if (_gtk_css_number_value_get_unit (end_border) == GTK_CSS_NUMBER)
+    end = end_border_width * _gtk_css_number_value_get (end_border, 100);
+  else
+    end = _gtk_css_number_value_get (end_border, area_size);
+
+  /* XXX: reduce vertical and horizontal by the same factor */
+  if (start + end > area_size)
+    {
+      start = start * area_size / (start + end);
+      end = end * area_size / (start + end);
+    }
 
   sizes[0].offset = offset;
-  sizes[0].size = start_border;
-  sizes[1].offset = offset + start_border;
-  sizes[1].size = area_size - start_border - end_border;
-  sizes[2].offset = offset + area_size - end_border;
-  sizes[2].size = end_border;
+  sizes[0].size = start;
+  sizes[1].offset = offset + start;
+  sizes[1].size = area_size - start - end;
+  sizes[2].offset = offset + area_size - end;
+  sizes[2].size = end;
 }
 
 static void
@@ -240,9 +248,6 @@ _gtk_border_image_render (GtkBorderImage   *image,
   double source_width, source_height;
   int h, v;
 
-  if (image->has_width)
-    border_width = &image->width;
-
   _gtk_css_image_get_concrete_size (image->source,
                                     0, 0,
                                     width, height,
@@ -266,12 +271,16 @@ _gtk_border_image_render (GtkBorderImage   *image,
                                         x,
                                         width,
                                         border_width->left,
-                                        border_width->right);
+                                        border_width->right,
+                                        _gtk_css_border_value_get_left (image->width),
+                                        _gtk_css_border_value_get_right (image->width));
   gtk_border_image_compute_border_size (vertical_border,
                                         y,
                                         height,
                                         border_width->top,
-                                        border_width->bottom);
+                                        border_width->bottom,
+                                        _gtk_css_border_value_get_top (image->width),
+                                        _gtk_css_border_value_get_bottom(image->width));
   
   for (v = 0; v < 3; v++)
     {
diff --git a/gtk/gtkborderimageprivate.h b/gtk/gtkborderimageprivate.h
index f57149c..65ed8dd 100644
--- a/gtk/gtkborderimageprivate.h
+++ b/gtk/gtkborderimageprivate.h
@@ -35,8 +35,7 @@ struct _GtkBorderImage {
   GtkCssImage *source;
 
   GtkCssValue *slice;
-  gboolean has_width;
-  GtkBorder width;
+  GtkCssValue *width;
   GtkCssValue *repeat;
 };
 
diff --git a/gtk/gtkcssnumbervalue.c b/gtk/gtkcssnumbervalue.c
index e7c04d9..531942b 100644
--- a/gtk/gtkcssnumbervalue.c
+++ b/gtk/gtkcssnumbervalue.c
@@ -141,6 +141,14 @@ _gtk_css_number_value_parse (GtkCssParser           *parser,
   return _gtk_css_number_value_new (number.value, number.unit);
 }
 
+GtkCssUnit
+_gtk_css_number_value_get_unit (const GtkCssValue *value)
+{
+  g_return_val_if_fail (value->class == &GTK_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
+
+  return value->unit;
+}
+
 double
 _gtk_css_number_value_get (const GtkCssValue *number,
                            double             one_hundred_percent)
diff --git a/gtk/gtkcssnumbervalueprivate.h b/gtk/gtkcssnumbervalueprivate.h
index a864d44..965b06d 100644
--- a/gtk/gtkcssnumbervalueprivate.h
+++ b/gtk/gtkcssnumbervalueprivate.h
@@ -31,6 +31,7 @@ GtkCssValue *   _gtk_css_number_value_new           (double                  val
 GtkCssValue *   _gtk_css_number_value_parse         (GtkCssParser           *parser,
                                                      GtkCssNumberParseFlags  flags);
 
+GtkCssUnit      _gtk_css_number_value_get_unit      (const GtkCssValue      *value);
 double          _gtk_css_number_value_get           (const GtkCssValue      *number,
                                                      double                  one_hundred_percent);
 GtkCssValue *   _gtk_css_number_value_compute       (GtkCssValue            *number,
diff --git a/gtk/gtkcssshorthandpropertyimpl.c b/gtk/gtkcssshorthandpropertyimpl.c
index 3a26a28..03d84e8 100644
--- a/gtk/gtkcssshorthandpropertyimpl.c
+++ b/gtk/gtkcssshorthandpropertyimpl.c
@@ -34,9 +34,7 @@
 #include "gtkcssrepeatvalueprivate.h"
 #include "gtkcssstringvalueprivate.h"
 #include "gtkcssstylefuncsprivate.h"
-#include "gtkcsstypesprivate.h"
 #include "gtkcssvalueprivate.h"
-#include "gtkprivatetypebuiltins.h"
 #include "gtkstylepropertiesprivate.h"
 #include "gtksymboliccolorprivate.h"
 #include "gtktypebuiltins.h"
@@ -316,13 +314,15 @@ parse_border_image (GtkCssShorthandProperty  *shorthand,
 
           if (_gtk_css_parser_try (parser, "/", TRUE))
             {
-              GValue value = G_VALUE_INIT;
-
-              g_value_init (&value, GTK_TYPE_BORDER);
-              if (!_gtk_css_style_parse_value (&value, parser, base))
+              values[2] = _gtk_css_border_value_parse (parser,
+                                                       GTK_CSS_PARSE_PERCENT
+                                                       | GTK_CSS_PARSE_LENGTH
+                                                       | GTK_CSS_PARSE_NUMBER
+                                                       | GTK_CSS_POSITIVE_ONLY,
+                                                       TRUE,
+                                                       FALSE);
+              if (values[2] == NULL)
                 return FALSE;
-              values[2] = _gtk_css_value_new_from_gvalue (&value);
-              g_value_unset (&value);
             }
         }
       else
diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c
index 63b3b3e..17a5a19 100644
--- a/gtk/gtkcssstylepropertyimpl.c
+++ b/gtk/gtkcssstylepropertyimpl.c
@@ -719,20 +719,13 @@ border_image_width_parse (GtkCssStyleProperty *property,
                           GtkCssParser        *parser,
                           GFile               *base)
 {
-  GValue value = G_VALUE_INIT;
-  GtkCssValue *result;
-
-  g_value_init (&value, GTK_TYPE_BORDER);
-  if (!_gtk_css_style_parse_value (&value, parser, base))
-    {
-      g_value_unset (&value);
-      return NULL;
-    }
-
-  result = _gtk_css_value_new_from_gvalue (&value);
-  g_value_unset (&value);
-
-  return result;
+  return _gtk_css_border_value_parse (parser,
+                                      GTK_CSS_PARSE_PERCENT
+                                      | GTK_CSS_PARSE_LENGTH
+                                      | GTK_CSS_PARSE_NUMBER
+                                      | GTK_CSS_POSITIVE_ONLY,
+                                      TRUE,
+                                      FALSE);
 }
 
 static GtkCssValue *
@@ -1533,11 +1526,14 @@ _gtk_css_style_property_init_properties (void)
                                           0,
                                           border_image_width_parse,
                                           NULL,
+                                          compute_border,
+                                          query_border,
+                                          assign_border,
                                           NULL,
-                                          query_simple,
-                                          assign_simple,
-                                          NULL,
-                                          _gtk_css_value_new_from_boxed (GTK_TYPE_BORDER, NULL));
+                                          _gtk_css_border_value_new (_gtk_css_number_value_new (1, GTK_CSS_NUMBER),
+                                                                     _gtk_css_number_value_new (1, GTK_CSS_NUMBER),
+                                                                     _gtk_css_number_value_new (1, GTK_CSS_NUMBER),
+                                                                     _gtk_css_number_value_new (1, GTK_CSS_NUMBER)));
 
   gtk_css_style_property_register        ("transition-property",
                                           GTK_CSS_PROPERTY_TRANSITION_PROPERTY,



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