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_equal (const GtkCssValue *position1,
41 const GtkCssValue *position2)
43 return _gtk_css_value_equal (position1->x, position2->x)
44 && _gtk_css_value_equal (position1->y, position2->y);
48 gtk_css_value_position_transition (GtkCssValue *start,
54 x = _gtk_css_value_transition (start->x, end->x, progress);
57 y = _gtk_css_value_transition (start->y, end->y, progress);
60 _gtk_css_value_unref (x);
64 return _gtk_css_position_value_new (x, y);
68 gtk_css_value_position_print (const GtkCssValue *position,
76 { "left", "top", _gtk_css_number_value_new (0, GTK_CSS_PERCENT) },
77 { "right", "bottom", _gtk_css_number_value_new (100, GTK_CSS_PERCENT) }
79 GtkCssValue *center = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
82 if (_gtk_css_value_equal (position->x, center))
84 if (_gtk_css_value_equal (position->y, center))
86 g_string_append (string, "center");
92 for (i = 0; i < G_N_ELEMENTS (values); i++)
94 if (_gtk_css_value_equal (position->x, values[i].number))
96 g_string_append (string, values[i].x_name);
100 if (i == G_N_ELEMENTS (values))
101 _gtk_css_value_print (position->x, string);
103 if (_gtk_css_value_equal (position->y, center))
106 g_string_append_c (string, ' ');
109 for (i = 0; i < G_N_ELEMENTS (values); i++)
111 if (_gtk_css_value_equal (position->y, values[i].number))
113 g_string_append (string, values[i].y_name);
117 if (i == G_N_ELEMENTS (values))
119 if (_gtk_css_value_equal (position->x, center))
120 g_string_append (string, "center ");
121 _gtk_css_value_print (position->y, string);
125 for (i = 0; i < G_N_ELEMENTS (values); i++)
126 _gtk_css_value_unref (values[i].number);
127 _gtk_css_value_unref (center);
130 static const GtkCssValueClass GTK_CSS_VALUE_POSITION = {
131 gtk_css_value_position_free,
132 gtk_css_value_position_equal,
133 gtk_css_value_position_transition,
134 gtk_css_value_position_print
138 _gtk_css_position_value_new (GtkCssValue *x,
143 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_POSITION);
151 _gtk_css_position_value_parse (GtkCssParser *parser)
153 static const struct {
159 { "left", 0, TRUE, FALSE },
160 { "right", 100, TRUE, FALSE },
161 { "center", 50, TRUE, TRUE },
162 { "top", 0, FALSE, TRUE },
163 { "bottom", 100, FALSE, TRUE },
164 { NULL , 0, TRUE, FALSE }, /* used for numbers */
165 { NULL , 50, TRUE, TRUE } /* used for no value */
168 GtkCssValue **missing;
171 for (first = 0; names[first].name != NULL; first++)
173 if (_gtk_css_parser_try (parser, names[first].name, TRUE))
175 if (names[first].horizontal)
177 x = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
182 y = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
188 if (names[first].name == NULL)
190 if (_gtk_css_parser_has_number (parser))
193 x = _gtk_css_number_value_parse (parser,
194 GTK_CSS_PARSE_PERCENT
195 | GTK_CSS_PARSE_LENGTH);
204 for (second = 0; names[second].name != NULL; second++)
206 if (_gtk_css_parser_try (parser, names[second].name, TRUE))
208 *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
213 if (names[second].name == NULL)
215 if (_gtk_css_parser_has_number (parser))
219 _gtk_css_parser_error (parser, "Invalid combination of values");
220 _gtk_css_value_unref (y);
223 y = _gtk_css_number_value_parse (parser,
224 GTK_CSS_PARSE_PERCENT
225 | GTK_CSS_PARSE_LENGTH);
228 _gtk_css_value_unref (x);
235 *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
240 if ((names[first].horizontal && !names[second].vertical) ||
241 (!names[first].horizontal && !names[second].horizontal))
243 _gtk_css_parser_error (parser, "Invalid combination of values");
244 _gtk_css_value_unref (x);
245 _gtk_css_value_unref (y);
250 return _gtk_css_position_value_new (x, y);
254 _gtk_css_position_value_get_x (const GtkCssValue *position,
255 double one_hundred_percent)
257 g_return_val_if_fail (position != NULL, 0.0);
258 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
260 return _gtk_css_number_value_get (position->x, one_hundred_percent);
264 _gtk_css_position_value_get_y (const GtkCssValue *position,
265 double one_hundred_percent)
267 g_return_val_if_fail (position != NULL, 0.0);
268 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
270 return _gtk_css_number_value_get (position->y, one_hundred_percent);
274 _gtk_css_position_value_compute (GtkCssValue *position,
275 GtkStyleContext *context)
279 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, NULL);
281 x = _gtk_css_number_value_compute (position->x, context);
282 y = _gtk_css_number_value_compute (position->y, context);
283 if (x == position->x && y == position->y)
285 _gtk_css_value_unref (x);
286 _gtk_css_value_unref (y);
287 return _gtk_css_value_ref (position);
290 return _gtk_css_position_value_new (x, y);