1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include "gtkcssarrayvalueprivate.h"
27 GtkCssValue *values[1];
31 gtk_css_value_array_free (GtkCssValue *value)
35 for (i = 0; i < value->n_values; i++)
37 _gtk_css_value_unref (value->values[i]);
40 g_slice_free1 (sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (value->n_values - 1), value);
44 gtk_css_value_array_equal (const GtkCssValue *value1,
45 const GtkCssValue *value2)
49 if (value1->n_values != value2->n_values)
52 for (i = 0; i < value1->n_values; i++)
54 if (!_gtk_css_value_equal (value1->values[i],
63 gtk_css_value_array_transition (GtkCssValue *start,
71 gtk_css_value_array_print (const GtkCssValue *value,
76 if (value->n_values == 0)
78 g_string_append (string, "none");
82 for (i = 0; i < value->n_values; i++)
85 g_string_append (string, ", ");
86 _gtk_css_value_print (value->values[i], string);
90 static const GtkCssValueClass GTK_CSS_VALUE_ARRAY = {
91 gtk_css_value_array_free,
92 gtk_css_value_array_equal,
93 gtk_css_value_array_transition,
94 gtk_css_value_array_print
97 static GtkCssValue none_singleton = { >K_CSS_VALUE_ARRAY, 1, 0, { NULL } };
100 _gtk_css_array_value_new (GtkCssValue *content)
103 return _gtk_css_value_ref (&none_singleton);
105 return _gtk_css_array_value_new_from_array (&content, 1);
109 _gtk_css_array_value_new_from_array (GtkCssValue **values,
114 g_return_val_if_fail (values != NULL, NULL);
115 g_return_val_if_fail (n_values > 0, NULL);
117 result = _gtk_css_value_alloc (>K_CSS_VALUE_ARRAY, sizeof (GtkCssValue) + sizeof (GtkCssValue *) * (n_values - 1));
118 result->n_values = n_values;
119 memcpy (&result->values[0], values, sizeof (GtkCssValue *) * n_values);
125 _gtk_css_array_value_parse (GtkCssParser *parser,
126 GtkCssValue *(* parse_func) (GtkCssParser *parser),
129 GtkCssValue *value, *result;
133 _gtk_css_parser_try (parser, "none", TRUE))
134 return _gtk_css_value_ref (&none_singleton);
136 values = g_ptr_array_new ();
139 value = parse_func (parser);
143 g_ptr_array_set_free_func (values, (GDestroyNotify) _gtk_css_value_unref);
144 g_ptr_array_free (values, TRUE);
148 g_ptr_array_add (values, value);
149 } while (_gtk_css_parser_try (parser, ",", TRUE));
151 result = _gtk_css_array_value_new_from_array ((GtkCssValue **) values->pdata, values->len);
152 g_ptr_array_free (values, TRUE);
157 _gtk_css_array_value_compute (GtkCssValue *value,
158 GtkCssValue * (* compute_func) (GtkCssValue *, GtkStyleContext *),
159 GtkStyleContext *context)
162 gboolean changed = FALSE;
165 g_return_val_if_fail (value->class == >K_CSS_VALUE_ARRAY, NULL);
166 g_return_val_if_fail (compute_func != NULL, NULL);
168 if (value->n_values == 0)
169 return _gtk_css_value_ref (value);
171 result = _gtk_css_array_value_new_from_array (value->values, value->n_values);
172 for (i = 0; i < value->n_values; i++)
174 result->values[i] = (* compute_func) (value->values[i], context);
175 changed |= (result->values[i] != value->values[i]);
180 _gtk_css_value_unref (result);
181 return _gtk_css_value_ref (value);
188 _gtk_css_array_value_get_nth (const GtkCssValue *value,
191 g_return_val_if_fail (value != NULL, NULL);
192 g_return_val_if_fail (value->class == >K_CSS_VALUE_ARRAY, NULL);
193 g_return_val_if_fail (value->n_values > 0, NULL);
195 return value->values[i % value->n_values];
199 _gtk_css_array_value_get_n_values (const GtkCssValue *value)
201 g_return_val_if_fail (value != NULL, 0);
202 g_return_val_if_fail (value->class == >K_CSS_VALUE_ARRAY, 0);
204 return value->n_values;