]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcssshorthandpropertyimpl.c
Add border-{top|left|bottom|right}-style properties
[~andy/gtk] / gtk / gtkcssshorthandpropertyimpl.c
index f565e7f9ed0dc71c946491443fb5a9390ce03dfd..5d93dfd32e428cb5888b406f343be8b1dbe366fa 100644 (file)
 #include <cairo-gobject.h>
 #include <math.h>
 
-#include "gtkborderimageprivate.h"
+#include "gtkcssimageprivate.h"
 #include "gtkcssstylefuncsprivate.h"
 #include "gtkcsstypesprivate.h"
+#include "gtkprivatetypebuiltins.h"
+#include "gtktypebuiltins.h"
 
 /* this is in case round() is not provided by the compiler, 
  * such as in the case of C89 compilers, like MSVC
 
 /*** PARSING ***/
 
+static gboolean
+value_is_done_parsing (GtkCssParser *parser)
+{
+  return _gtk_css_parser_is_eof (parser) ||
+         _gtk_css_parser_begins_with (parser, ';') ||
+         _gtk_css_parser_begins_with (parser, '}');
+}
+
 static gboolean
 parse_border (GtkCssShorthandProperty *shorthand,
               GValue                  *values,
@@ -155,9 +165,7 @@ parse_border_color (GtkCssShorthandProperty *shorthand,
       g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR);
       g_value_set_boxed (&values[i], symbolic);
 
-      if (_gtk_css_parser_is_eof (parser) ||
-          _gtk_css_parser_begins_with (parser, ';') ||
-          _gtk_css_parser_begins_with (parser, '}'))
+      if (value_is_done_parsing (parser))
         break;
     }
 
@@ -170,89 +178,219 @@ parse_border_color (GtkCssShorthandProperty *shorthand,
   return TRUE;
 }
 
-/*** OLD PARSING ***/
-
 static gboolean
-border_image_value_parse (GtkCssParser *parser,
-                          GFile *base,
-                          GValue *value)
+parse_border_style (GtkCssShorthandProperty *shorthand,
+                    GValue                  *values,
+                    GtkCssParser            *parser,
+                    GFile                   *base)
 {
-  GValue temp = G_VALUE_INIT;
-  cairo_pattern_t *pattern = NULL;
-  gconstpointer *boxed = NULL;
-  GType boxed_type;
-  GtkBorder slice, *width = NULL, *parsed_slice;
-  GtkCssBorderImageRepeat repeat, *parsed_repeat;
-  gboolean retval = FALSE;
-  GtkBorderImage *image = NULL;
+  GtkBorderStyle styles[4];
+  guint i;
 
-  if (_gtk_css_parser_try (parser, "none", TRUE))
-    return TRUE;
+  for (i = 0; i < 4; i++)
+    {
+      if (!_gtk_css_parser_try_enum (parser, GTK_TYPE_BORDER_STYLE, (int *)&styles[i]))
+        break;
+    }
 
-  g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
+  if (i == 0)
+    {
+      _gtk_css_parser_error (parser, "Expected a border style");
+      return FALSE;
+    }
 
-  if (!_gtk_css_style_parse_value (&temp, parser, base))
-    return FALSE;
+  for (; i < G_N_ELEMENTS (styles); i++)
+    styles[i] = styles[(i - 1) >> 1];
 
-  boxed_type = G_VALUE_TYPE (&temp);
-  if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
-    boxed = g_value_dup_boxed (&temp);
-  else
-    pattern = g_value_dup_boxed (&temp);
+  for (i = 0; i < G_N_ELEMENTS (styles); i++)
+    {
+      g_value_init (&values[i], GTK_TYPE_BORDER_STYLE);
+      g_value_set_enum (&values[i], styles[i]);
+    }
 
-  g_value_unset (&temp);
-  g_value_init (&temp, GTK_TYPE_BORDER);
+  return TRUE;
+}
 
-  if (!_gtk_css_style_parse_value (&temp, parser, base))
-    goto out;
+static gboolean
+parse_border_image (GtkCssShorthandProperty *shorthand,
+                    GValue                  *values,
+                    GtkCssParser            *parser,
+                    GFile                   *base)
+{
+  GtkCssImage *image;
+  
+  if (_gtk_css_parser_try (parser, "none", TRUE))
+    image = NULL;
+  else
+    {
+      image = _gtk_css_image_new_parse (parser, base);
+      if (!image)
+        return FALSE;
+    }
+  g_value_init (&values[0], GTK_TYPE_CSS_IMAGE);
+  g_value_set_object (&values[0], image);
+
+  if (value_is_done_parsing (parser))
+    return TRUE;
 
-  parsed_slice = g_value_get_boxed (&temp);
-  slice = *parsed_slice;
+  g_value_init (&values[1], GTK_TYPE_BORDER);
+  if (!_gtk_css_style_parse_value (&values[1], parser, base))
+    return FALSE;
 
   if (_gtk_css_parser_try (parser, "/", TRUE))
     {
-      g_value_unset (&temp);
-      g_value_init (&temp, GTK_TYPE_BORDER);
+      g_value_init (&values[2], GTK_TYPE_BORDER);
+      if (!_gtk_css_style_parse_value (&values[2], parser, base))
+        return FALSE;
+    }
 
-      if (!_gtk_css_style_parse_value (&temp, parser, base))
-        goto out;
+  if (value_is_done_parsing (parser))
+    return TRUE;
 
-      width = g_value_dup_boxed (&temp);
-    }
+  g_value_init (&values[3], GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
+  if (!_gtk_css_style_parse_value (&values[3], parser, base))
+    return FALSE;
 
-  g_value_unset (&temp);
-  g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
+  return TRUE;
+}
 
-  if (!_gtk_css_style_parse_value (&temp, parser, base))
-    goto out;
+static gboolean
+parse_font (GtkCssShorthandProperty *shorthand,
+            GValue                  *values,
+            GtkCssParser            *parser,
+            GFile                   *base)
+{
+  PangoFontDescription *desc;
+  guint mask;
+  char *str;
 
-  parsed_repeat = g_value_get_boxed (&temp);
-  repeat = *parsed_repeat;
+  str = _gtk_css_parser_read_value (parser);
+  if (str == NULL)
+    return FALSE;
 
-  g_value_unset (&temp);
+  desc = pango_font_description_from_string (str);
+  g_free (str);
 
-  if (boxed != NULL)
-    image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
-  else if (pattern != NULL)
-    image = _gtk_border_image_new (pattern, &slice, width, &repeat);
+  mask = pango_font_description_get_set_fields (desc);
 
-  if (image != NULL)
+  if (mask & PANGO_FONT_MASK_FAMILY)
     {
-      retval = TRUE;
-      g_value_take_boxed (value, image);
+      GPtrArray *strv = g_ptr_array_new ();
+
+      g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (desc)));
+      g_ptr_array_add (strv, NULL);
+      g_value_init (&values[0], G_TYPE_STRV);
+      g_value_take_boxed (&values[0], g_ptr_array_free (strv, FALSE));
     }
+  if (mask & PANGO_FONT_MASK_STYLE)
+    {
+      g_value_init (&values[1], PANGO_TYPE_STYLE);
+      g_value_set_enum (&values[1], pango_font_description_get_style (desc));
+    }
+  if (mask & PANGO_FONT_MASK_VARIANT)
+    {
+      g_value_init (&values[2], PANGO_TYPE_VARIANT);
+      g_value_set_enum (&values[2], pango_font_description_get_variant (desc));
+    }
+  if (mask & PANGO_FONT_MASK_WEIGHT)
+    {
+      g_value_init (&values[3], PANGO_TYPE_WEIGHT);
+      g_value_set_enum (&values[3], pango_font_description_get_weight (desc));
+    }
+  if (mask & PANGO_FONT_MASK_SIZE)
+    {
+      g_value_init (&values[4], G_TYPE_DOUBLE);
+      g_value_set_double (&values[4],
+                          (double) pango_font_description_get_size (desc) / PANGO_SCALE);
+    }
+
+  pango_font_description_free (desc);
+
+  return TRUE;
+}
+
+static gboolean
+parse_background (GtkCssShorthandProperty *shorthand,
+                  GValue                  *values,
+                  GtkCssParser            *parser,
+                  GFile                   *base)
+{
+  int enum_value;
 
- out:
-  if (pattern != NULL)
-    cairo_pattern_destroy (pattern);
+  do
+    {
+      /* the image part */
+      if (!G_IS_VALUE (&values[0]) &&
+          (_gtk_css_parser_has_prefix (parser, "none") ||
+           _gtk_css_image_can_parse (parser)))
+        {
+          GtkCssImage *image;
 
-  if (boxed != NULL)
-    g_boxed_free (boxed_type, boxed);
+          if (_gtk_css_parser_try (parser, "none", TRUE))
+            image = NULL;
+          else
+            {
+              image = _gtk_css_image_new_parse (parser, base);
+              if (image == NULL)
+                return FALSE;
+            }
 
-  if (width != NULL)
-    gtk_border_free (width);
+          g_value_init (&values[0], GTK_TYPE_CSS_IMAGE);
+          g_value_take_object (&values[0], image);
+        }
+      else if (!G_IS_VALUE (&values[1]) &&
+               _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &enum_value))
+        {
+          if (enum_value <= GTK_CSS_BACKGROUND_REPEAT_MASK)
+            {
+              int vertical;
+
+              if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical))
+                {
+                  if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK)
+                    {
+                      _gtk_css_parser_error (parser, "Not a valid 2nd value for border-repeat");
+                      return FALSE;
+                    }
+                  else
+                    enum_value |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
+                }
+              else
+                enum_value |= enum_value << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
+            }
 
-  return retval;
+          g_value_init (&values[1], GTK_TYPE_CSS_BACKGROUND_REPEAT);
+          g_value_set_enum (&values[1], enum_value);
+        }
+      else if ((!G_IS_VALUE (&values[2]) || !G_IS_VALUE (&values[3])) &&
+               _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_AREA, &enum_value))
+        {
+          guint idx = !G_IS_VALUE (&values[2]) ? 2 : 3;
+          g_value_init (&values[idx], GTK_TYPE_CSS_AREA);
+          g_value_set_enum (&values[idx], enum_value);
+        }
+      else if (!G_IS_VALUE (&values[4]))
+        {
+          GtkSymbolicColor *symbolic;
+          
+          symbolic = _gtk_css_parser_read_symbolic_color (parser);
+          if (symbolic == NULL)
+            return FALSE;
+
+          g_value_init (&values[4], GTK_TYPE_SYMBOLIC_COLOR);
+          g_value_take_boxed (&values[4], symbolic);
+        }
+      else
+        {
+          /* We parsed everything and there's still stuff left?
+           * Pretend we didn't notice and let the normal code produce
+           * a 'junk at end of value' error */
+          break;
+        }
+    }
+  while (!value_is_done_parsing (parser));
+
+  return TRUE;
 }
 
 /*** PACKING ***/
@@ -325,8 +463,7 @@ unpack_border_width (const GValue *value,
 static void
 pack_border_width (GValue             *value,
                    GtkStyleProperties *props,
-                   GtkStateFlags       state,
-                  GtkStylePropertyContext *context)
+                   GtkStateFlags       state)
 {
   pack_border (value, props, state,
                "border-top-width", "border-left-width",
@@ -345,8 +482,7 @@ unpack_padding (const GValue *value,
 static void
 pack_padding (GValue             *value,
               GtkStyleProperties *props,
-              GtkStateFlags       state,
-             GtkStylePropertyContext *context)
+              GtkStateFlags       state)
 {
   pack_border (value, props, state,
                "padding-top", "padding-left",
@@ -365,8 +501,7 @@ unpack_margin (const GValue *value,
 static void
 pack_margin (GValue             *value,
              GtkStyleProperties *props,
-             GtkStateFlags       state,
-            GtkStylePropertyContext *context)
+             GtkStateFlags       state)
 {
   pack_border (value, props, state,
                "margin-top", "margin-left",
@@ -402,8 +537,7 @@ unpack_border_radius (const GValue *value,
 static void
 pack_border_radius (GValue             *value,
                     GtkStyleProperties *props,
-                    GtkStateFlags       state,
-                   GtkStylePropertyContext *context)
+                    GtkStateFlags       state)
 {
   GtkCssBorderCornerRadius *top_left;
 
@@ -503,8 +637,7 @@ unpack_font_description (const GValue *value,
 static void
 pack_font_description (GValue             *value,
                        GtkStyleProperties *props,
-                       GtkStateFlags       state,
-                      GtkStylePropertyContext *context)
+                       GtkStateFlags       state)
 {
   PangoFontDescription *description;
   char **families;
@@ -582,8 +715,7 @@ unpack_border_color (const GValue *value,
 static void
 pack_border_color (GValue             *value,
                    GtkStyleProperties *props,
-                   GtkStateFlags       state,
-                  GtkStylePropertyContext *context)
+                   GtkStateFlags       state)
 {
   /* NB: We are a color property, so we have to resolve to a color here.
    * So we just resolve to a color. We pick one and stick to it.
@@ -593,31 +725,63 @@ pack_border_color (GValue             *value,
   gtk_style_properties_get_property (props, "border-top-color", state, value);
 }
 
+static GParameter *
+unpack_border_style (const GValue *value,
+                     guint        *n_params)
+{
+  GParameter *parameter = g_new0 (GParameter, 4);
+  GtkBorderStyle style;
+
+  style = g_value_get_enum (value);
+
+  parameter[0].name = "border-top-style";
+  g_value_init (&parameter[0].value, GTK_TYPE_BORDER_STYLE);
+  g_value_set_enum (&parameter[0].value, style);
+  parameter[1].name = "border-right-style";
+  g_value_init (&parameter[1].value, GTK_TYPE_BORDER_STYLE);
+  g_value_set_enum (&parameter[1].value, style);
+  parameter[2].name = "border-bottom-style";
+  g_value_init (&parameter[2].value, GTK_TYPE_BORDER_STYLE);
+  g_value_set_enum (&parameter[2].value, style);
+  parameter[3].name = "border-left-style";
+  g_value_init (&parameter[3].value, GTK_TYPE_BORDER_STYLE);
+  g_value_set_enum (&parameter[3].value, style);
+
+  *n_params = 4;
+  return parameter;
+}
+
+static void
+pack_border_style (GValue             *value,
+                   GtkStyleProperties *props,
+                   GtkStateFlags       state)
+{
+  /* NB: We can just resolve to a style. We pick one and stick to it.
+   * Lesson learned: Don't query border-style shorthand, query the
+   * real properties instead. */
+  g_value_unset (value);
+  gtk_style_properties_get_property (props, "border-top-style", state, value);
+}
+
 static void
 _gtk_css_shorthand_property_register (const char                        *name,
                                       GType                              value_type,
                                       const char                       **subproperties,
                                       GtkCssShorthandPropertyParseFunc   parse_func,
                                       GtkStyleUnpackFunc                 unpack_func,
-                                      GtkStylePackFunc                   pack_func,
-                                      GtkStyleParseFunc                  old_parse_func)
+                                      GtkStylePackFunc                   pack_func)
 {
   GtkStyleProperty *node;
 
-  g_return_if_fail (pack_func != NULL);
-  g_return_if_fail (unpack_func != NULL);
-
   node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
                        "name", name,
                        "value-type", value_type,
                        "subproperties", subproperties,
                        NULL);
 
-  if (parse_func)
-    GTK_CSS_SHORTHAND_PROPERTY (node)->parse = parse_func;
+  GTK_CSS_SHORTHAND_PROPERTY (node)->parse = parse_func;
   node->pack_func = pack_func;
   node->unpack_func = unpack_func;
-  node->parse_func = old_parse_func;
 }
 
 void
@@ -631,55 +795,63 @@ _gtk_css_shorthand_property_init_properties (void)
   const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
                                                 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
   const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
+  const char *border_style_subproperties[] = { "border-top-style", "border-right-style", "border-bottom-style", "border-left-style", NULL };
   const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
+  const char *background_subproperties[] = { "background-image", "background-repeat", "background-clip", "background-origin",
+                                             "background-color", NULL };
 
   _gtk_css_shorthand_property_register   ("font",
                                           PANGO_TYPE_FONT_DESCRIPTION,
                                           font_subproperties,
-                                          NULL,
+                                          parse_font,
                                           unpack_font_description,
-                                          pack_font_description,
-                                          NULL);
+                                          pack_font_description);
   _gtk_css_shorthand_property_register   ("margin",
                                           GTK_TYPE_BORDER,
                                           margin_subproperties,
                                           parse_border,
                                           unpack_margin,
-                                          pack_margin,
-                                          NULL);
+                                          pack_margin);
   _gtk_css_shorthand_property_register   ("padding",
                                           GTK_TYPE_BORDER,
                                           padding_subproperties,
                                           parse_border,
                                           unpack_padding,
-                                          pack_padding,
-                                          NULL);
+                                          pack_padding);
   _gtk_css_shorthand_property_register   ("border-width",
                                           GTK_TYPE_BORDER,
                                           border_width_subproperties,
                                           parse_border,
                                           unpack_border_width,
-                                          pack_border_width,
-                                          NULL);
+                                          pack_border_width);
   _gtk_css_shorthand_property_register   ("border-radius",
                                           G_TYPE_INT,
                                           border_radius_subproperties,
                                           parse_border_radius,
                                           unpack_border_radius,
-                                          pack_border_radius,
-                                          NULL);
+                                          pack_border_radius);
   _gtk_css_shorthand_property_register   ("border-color",
                                           GDK_TYPE_RGBA,
                                           border_color_subproperties,
                                           parse_border_color,
                                           unpack_border_color,
-                                          pack_border_color,
-                                          NULL);
+                                          pack_border_color);
+  _gtk_css_shorthand_property_register   ("border-style",
+                                          GTK_TYPE_BORDER_STYLE,
+                                          border_style_subproperties,
+                                          parse_border_style,
+                                          unpack_border_style,
+                                          pack_border_style);
   _gtk_css_shorthand_property_register   ("border-image",
-                                          GTK_TYPE_BORDER_IMAGE,
+                                          G_TYPE_NONE,
                                           border_image_subproperties,
+                                          parse_border_image,
                                           NULL,
-                                          _gtk_border_image_unpack,
-                                          _gtk_border_image_pack,
-                                          border_image_value_parse);
+                                          NULL);
+  _gtk_css_shorthand_property_register   ("background",
+                                          G_TYPE_NONE,
+                                          background_subproperties,
+                                          parse_background,
+                                          NULL,
+                                          NULL);
 }