[gtk/wip/otte/css: 25/59] cssimagescaled: Use gtk_css_parser_consume_function()



commit 5fbc816b1b3282412d0e111d21f42c62414f04ba
Author: Benjamin Otte <otte redhat com>
Date:   Sun Mar 31 19:24:53 2019 +0200

    cssimagescaled: Use gtk_css_parser_consume_function()
    
    As part of that, adapt the syntax from
      -gtk-scaled( [<image>, <int>?]# )
    to
      -gtk-scaled( [<image> <int>?]# )
    
    because the commas should be used to separate distinct elements.
    
    Note that almost nobody specifies the scale anyway.

 gtk/gtkcssimagescaled.c | 94 ++++++++++++++++++++-----------------------------
 1 file changed, 39 insertions(+), 55 deletions(-)
---
diff --git a/gtk/gtkcssimagescaled.c b/gtk/gtkcssimagescaled.c
index 14108845f1..a5d4e22898 100644
--- a/gtk/gtkcssimagescaled.c
+++ b/gtk/gtkcssimagescaled.c
@@ -143,76 +143,60 @@ gtk_css_image_scaled_compute (GtkCssImage      *image,
   return GTK_CSS_IMAGE (res);
 }
 
-
-static gboolean
-gtk_css_image_scaled_parse (GtkCssImage  *image,
-                            GtkCssParser *parser)
+typedef struct
 {
-  GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
   GPtrArray *images;
   GArray *scales;
-  int last_scale;
+} GtkCssImageScaledParseData;
+
+static guint
+gtk_css_image_scaled_parse_arg (GtkCssParser *parser,
+                                guint         arg,
+                                gpointer      data_)
+{
+  GtkCssImageScaledParseData *data = data_;
   GtkCssImage *child;
+  int scale;
 
-  if (!_gtk_css_parser_try (parser, "-gtk-scaled", TRUE))
-    {
-      _gtk_css_parser_error (parser, "'-gtk-scaled'");
-      return FALSE;
-    }
+  child = _gtk_css_image_new_parse (parser);
+  if (child == NULL)
+    return FALSE;
 
-  if (!_gtk_css_parser_try (parser, "(", TRUE))
-    {
-      _gtk_css_parser_error (parser,
-                             "Expected '(' after '-gtk-scaled'");
-      return FALSE;
-    }
+  if (!_gtk_css_parser_try_int (parser, &scale))
+    scale = arg > 0 ? g_array_index (data->scales, int, arg - 1) + 1 : 1;
+
+  g_ptr_array_add (data->images, child);
+  g_array_append_val (data->scales, scale);
+
+  return 1;
+}
 
-  images = g_ptr_array_new_with_free_func (g_object_unref);
-  scales = g_array_new (FALSE, FALSE, sizeof (int));
+static gboolean
+gtk_css_image_scaled_parse (GtkCssImage  *image,
+                            GtkCssParser *parser)
+{
+  GtkCssImageScaled *self = GTK_CSS_IMAGE_SCALED (image);
+  GtkCssImageScaledParseData data;
 
-  last_scale = 0;
-  do
+  if (!gtk_css_parser_has_function (parser, "-gtk-scaled"))
     {
-      child = _gtk_css_image_new_parse (parser);
-      if (child == NULL)
-        {
-          g_ptr_array_free (images, TRUE);
-          g_array_free (scales, TRUE);
-          return FALSE;
-        }
-      g_ptr_array_add (images, child);
-      if (!_gtk_css_parser_try (parser, ",", TRUE))
-        {
-          last_scale += 1;
-          g_array_append_val (scales, last_scale);
-          break;
-        }
-      else if (_gtk_css_parser_try_int (parser, &last_scale))
-        {
-          g_array_append_val (scales, last_scale);
-          if (!_gtk_css_parser_try (parser, ",", TRUE))
-            break;
-        }
-      else
-        {
-          last_scale += 1;
-          g_array_append_val (scales, last_scale);
-        }
+      _gtk_css_parser_error (parser, "Expected '-gtk-scaled('");
+      return FALSE;
     }
-  while (TRUE);
 
-  if (!_gtk_css_parser_try (parser, ")", TRUE))
+  data.images = g_ptr_array_new_with_free_func (g_object_unref);
+  data.scales = g_array_new (FALSE, FALSE, sizeof (int));
+
+  if (!gtk_css_parser_consume_function (parser, 1, G_MAXUINT, gtk_css_image_scaled_parse_arg, &data))
     {
-      g_ptr_array_free (images, TRUE);
-      g_array_free (scales, TRUE);
-      _gtk_css_parser_error (parser,
-                             "Expected ')' at end of '-gtk-scaled'");
+      g_ptr_array_unref (data.images);
+      g_array_unref (data.scales);
       return FALSE;
     }
 
-  scaled->n_images = images->len;
-  scaled->images = (GtkCssImage **) g_ptr_array_free (images, FALSE);
-  scaled->scales = (int *) g_array_free (scales, FALSE);
+  self->n_images = data.images->len;
+  self->images = (GtkCssImage **) g_ptr_array_free (data.images, FALSE);
+  self->scales = (int *) g_array_free (data.scales, FALSE);
 
   return TRUE;
 }


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