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 "gtkcsspositionvalueprivate.h"
22 #include "gtkcssnumbervalueprivate.h"
31 gtk_css_value_position_free (GtkCssValue *value)
33 _gtk_css_value_unref (value->x);
34 _gtk_css_value_unref (value->y);
36 g_slice_free (GtkCssValue, value);
40 gtk_css_value_position_compute (GtkCssValue *position,
42 GtkStyleContext *context,
43 GtkCssDependencies *dependencies)
46 GtkCssDependencies x_deps, y_deps;
48 x = _gtk_css_value_compute (position->x, property_id, context, &x_deps);
49 y = _gtk_css_value_compute (position->y, property_id, context, &y_deps);
50 *dependencies = _gtk_css_dependencies_union (x_deps, y_deps);
51 if (x == position->x && y == position->y)
53 _gtk_css_value_unref (x);
54 _gtk_css_value_unref (y);
55 return _gtk_css_value_ref (position);
58 return _gtk_css_position_value_new (x, y);
62 gtk_css_value_position_equal (const GtkCssValue *position1,
63 const GtkCssValue *position2)
65 return _gtk_css_value_equal (position1->x, position2->x)
66 && _gtk_css_value_equal (position1->y, position2->y);
70 gtk_css_value_position_transition (GtkCssValue *start,
76 x = _gtk_css_value_transition (start->x, end->x, progress);
79 y = _gtk_css_value_transition (start->y, end->y, progress);
82 _gtk_css_value_unref (x);
86 return _gtk_css_position_value_new (x, y);
90 gtk_css_value_position_print (const GtkCssValue *position,
98 { "left", "top", _gtk_css_number_value_new (0, GTK_CSS_PERCENT) },
99 { "right", "bottom", _gtk_css_number_value_new (100, GTK_CSS_PERCENT) }
101 GtkCssValue *center = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
104 if (_gtk_css_value_equal (position->x, center))
106 if (_gtk_css_value_equal (position->y, center))
108 g_string_append (string, "center");
114 for (i = 0; i < G_N_ELEMENTS (values); i++)
116 if (_gtk_css_value_equal (position->x, values[i].number))
118 g_string_append (string, values[i].x_name);
122 if (i == G_N_ELEMENTS (values))
123 _gtk_css_value_print (position->x, string);
125 if (_gtk_css_value_equal (position->y, center))
128 g_string_append_c (string, ' ');
131 for (i = 0; i < G_N_ELEMENTS (values); i++)
133 if (_gtk_css_value_equal (position->y, values[i].number))
135 g_string_append (string, values[i].y_name);
139 if (i == G_N_ELEMENTS (values))
141 if (_gtk_css_value_equal (position->x, center))
142 g_string_append (string, "center ");
143 _gtk_css_value_print (position->y, string);
147 for (i = 0; i < G_N_ELEMENTS (values); i++)
148 _gtk_css_value_unref (values[i].number);
149 _gtk_css_value_unref (center);
152 static const GtkCssValueClass GTK_CSS_VALUE_POSITION = {
153 gtk_css_value_position_free,
154 gtk_css_value_position_compute,
155 gtk_css_value_position_equal,
156 gtk_css_value_position_transition,
157 gtk_css_value_position_print
161 _gtk_css_position_value_new (GtkCssValue *x,
166 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_POSITION);
174 _gtk_css_position_value_parse (GtkCssParser *parser)
176 static const struct {
182 { "left", 0, TRUE, FALSE },
183 { "right", 100, TRUE, FALSE },
184 { "center", 50, TRUE, TRUE },
185 { "top", 0, FALSE, TRUE },
186 { "bottom", 100, FALSE, TRUE },
187 { NULL , 0, TRUE, FALSE }, /* used for numbers */
188 { NULL , 50, TRUE, TRUE } /* used for no value */
191 GtkCssValue **missing;
194 for (first = 0; names[first].name != NULL; first++)
196 if (_gtk_css_parser_try (parser, names[first].name, TRUE))
198 if (names[first].horizontal)
200 x = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
205 y = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
211 if (names[first].name == NULL)
213 if (_gtk_css_parser_has_number (parser))
216 x = _gtk_css_number_value_parse (parser,
217 GTK_CSS_PARSE_PERCENT
218 | GTK_CSS_PARSE_LENGTH);
227 for (second = 0; names[second].name != NULL; second++)
229 if (_gtk_css_parser_try (parser, names[second].name, TRUE))
231 *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
236 if (names[second].name == NULL)
238 if (_gtk_css_parser_has_number (parser))
242 _gtk_css_parser_error (parser, "Invalid combination of values");
243 _gtk_css_value_unref (y);
246 y = _gtk_css_number_value_parse (parser,
247 GTK_CSS_PARSE_PERCENT
248 | GTK_CSS_PARSE_LENGTH);
251 _gtk_css_value_unref (x);
258 *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
263 if ((names[first].horizontal && !names[second].vertical) ||
264 (!names[first].horizontal && !names[second].horizontal))
266 _gtk_css_parser_error (parser, "Invalid combination of values");
267 _gtk_css_value_unref (x);
268 _gtk_css_value_unref (y);
273 return _gtk_css_position_value_new (x, y);
277 _gtk_css_position_value_get_x (const GtkCssValue *position,
278 double one_hundred_percent)
280 g_return_val_if_fail (position != NULL, 0.0);
281 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
283 return _gtk_css_number_value_get (position->x, one_hundred_percent);
287 _gtk_css_position_value_get_y (const GtkCssValue *position,
288 double one_hundred_percent)
290 g_return_val_if_fail (position != NULL, 0.0);
291 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
293 return _gtk_css_number_value_get (position->y, one_hundred_percent);