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,
77 x = _gtk_css_value_transition (start->x, end->x, property_id, progress);
80 y = _gtk_css_value_transition (start->y, end->y, property_id, progress);
83 _gtk_css_value_unref (x);
87 return _gtk_css_position_value_new (x, y);
91 gtk_css_value_position_print (const GtkCssValue *position,
99 { "left", "top", _gtk_css_number_value_new (0, GTK_CSS_PERCENT) },
100 { "right", "bottom", _gtk_css_number_value_new (100, GTK_CSS_PERCENT) }
102 GtkCssValue *center = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
105 if (_gtk_css_value_equal (position->x, center))
107 if (_gtk_css_value_equal (position->y, center))
109 g_string_append (string, "center");
115 for (i = 0; i < G_N_ELEMENTS (values); i++)
117 if (_gtk_css_value_equal (position->x, values[i].number))
119 g_string_append (string, values[i].x_name);
123 if (i == G_N_ELEMENTS (values))
124 _gtk_css_value_print (position->x, string);
126 if (_gtk_css_value_equal (position->y, center))
129 g_string_append_c (string, ' ');
132 for (i = 0; i < G_N_ELEMENTS (values); i++)
134 if (_gtk_css_value_equal (position->y, values[i].number))
136 g_string_append (string, values[i].y_name);
140 if (i == G_N_ELEMENTS (values))
142 if (_gtk_css_value_equal (position->x, center))
143 g_string_append (string, "center ");
144 _gtk_css_value_print (position->y, string);
148 for (i = 0; i < G_N_ELEMENTS (values); i++)
149 _gtk_css_value_unref (values[i].number);
150 _gtk_css_value_unref (center);
153 static const GtkCssValueClass GTK_CSS_VALUE_POSITION = {
154 gtk_css_value_position_free,
155 gtk_css_value_position_compute,
156 gtk_css_value_position_equal,
157 gtk_css_value_position_transition,
158 gtk_css_value_position_print
162 _gtk_css_position_value_new (GtkCssValue *x,
167 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_POSITION);
175 _gtk_css_position_value_parse (GtkCssParser *parser)
177 static const struct {
183 { "left", 0, TRUE, FALSE },
184 { "right", 100, TRUE, FALSE },
185 { "center", 50, TRUE, TRUE },
186 { "top", 0, FALSE, TRUE },
187 { "bottom", 100, FALSE, TRUE },
188 { NULL , 0, TRUE, FALSE }, /* used for numbers */
189 { NULL , 50, TRUE, TRUE } /* used for no value */
192 GtkCssValue **missing;
195 for (first = 0; names[first].name != NULL; first++)
197 if (_gtk_css_parser_try (parser, names[first].name, TRUE))
199 if (names[first].horizontal)
201 x = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
206 y = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
212 if (names[first].name == NULL)
214 if (_gtk_css_parser_has_number (parser))
217 x = _gtk_css_number_value_parse (parser,
218 GTK_CSS_PARSE_PERCENT
219 | GTK_CSS_PARSE_LENGTH);
228 for (second = 0; names[second].name != NULL; second++)
230 if (_gtk_css_parser_try (parser, names[second].name, TRUE))
232 *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
237 if (names[second].name == NULL)
239 if (_gtk_css_parser_has_number (parser))
243 _gtk_css_parser_error (parser, "Invalid combination of values");
244 _gtk_css_value_unref (y);
247 y = _gtk_css_number_value_parse (parser,
248 GTK_CSS_PARSE_PERCENT
249 | GTK_CSS_PARSE_LENGTH);
252 _gtk_css_value_unref (x);
259 *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
264 if ((names[first].horizontal && !names[second].vertical) ||
265 (!names[first].horizontal && !names[second].horizontal))
267 _gtk_css_parser_error (parser, "Invalid combination of values");
268 _gtk_css_value_unref (x);
269 _gtk_css_value_unref (y);
274 return _gtk_css_position_value_new (x, y);
278 _gtk_css_position_value_get_x (const GtkCssValue *position,
279 double one_hundred_percent)
281 g_return_val_if_fail (position != NULL, 0.0);
282 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
284 return _gtk_css_number_value_get (position->x, one_hundred_percent);
288 _gtk_css_position_value_get_y (const GtkCssValue *position,
289 double one_hundred_percent)
291 g_return_val_if_fail (position != NULL, 0.0);
292 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
294 return _gtk_css_number_value_get (position->y, one_hundred_percent);