From 21616f6e2eea2889e6560033d08720d8dd928574 Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Sun, 31 Mar 2019 19:24:53 +0200 Subject: [PATCH] cssimagescaled: Use gtk_css_parser_consume_function() As part of that, adapt the syntax from -gtk-scaled( [, ?]# ) to -gtk-scaled( [ ?]# ) because the commas should be used to separate distinct elements. Note that almost nobody specifies the scale anyway. --- gtk/gtkcssimagescaled.c | 96 +++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 56 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); } +typedef struct +{ + GPtrArray *images; + GArray *scales; +} GtkCssImageScaledParseData; + +static guint +gtk_css_image_scaled_parse_arg (GtkCssParser *parser, + guint arg, + gpointer data_) +{ + GtkCssImageScaledParseData *data = data_; + GtkCssImage *child; + int scale; + + child = _gtk_css_image_new_parse (parser); + if (child == NULL) + 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; +} static gboolean gtk_css_image_scaled_parse (GtkCssImage *image, GtkCssParser *parser) { - GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image); - GPtrArray *images; - GArray *scales; - int last_scale; - GtkCssImage *child; + GtkCssImageScaled *self = GTK_CSS_IMAGE_SCALED (image); + GtkCssImageScaledParseData data; - if (!_gtk_css_parser_try (parser, "-gtk-scaled", TRUE)) + if (!gtk_css_parser_has_function (parser, "-gtk-scaled")) { - _gtk_css_parser_error (parser, "'-gtk-scaled'"); + _gtk_css_parser_error (parser, "Expected '-gtk-scaled('"); return FALSE; } - 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)) { - _gtk_css_parser_error (parser, - "Expected '(' after '-gtk-scaled'"); + g_ptr_array_unref (data.images); + g_array_unref (data.scales); return FALSE; } - images = g_ptr_array_new_with_free_func (g_object_unref); - scales = g_array_new (FALSE, FALSE, sizeof (int)); - - last_scale = 0; - do - { - 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); - } - } - while (TRUE); - - if (!_gtk_css_parser_try (parser, ")", TRUE)) - { - g_ptr_array_free (images, TRUE); - g_array_free (scales, TRUE); - _gtk_css_parser_error (parser, - "Expected ')' at end of '-gtk-scaled'"); - 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; }