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)
191 x = _gtk_css_number_value_parse (parser,
192 GTK_CSS_PARSE_PERCENT
193 | GTK_CSS_PARSE_LENGTH);
198 for (second = 0; names[second].name != NULL; second++)
200 if (_gtk_css_parser_try (parser, names[second].name, TRUE))
202 *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
207 if (names[second].name == NULL)
209 if (_gtk_css_parser_has_number (parser))
213 _gtk_css_parser_error (parser, "Invalid combination of values");
214 _gtk_css_value_unref (y);
217 y = _gtk_css_number_value_parse (parser,
218 GTK_CSS_PARSE_PERCENT
219 | GTK_CSS_PARSE_LENGTH);
222 _gtk_css_value_unref (x);
229 *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
234 if ((names[first].horizontal && !names[second].vertical) ||
235 (!names[first].horizontal && !names[second].horizontal))
237 _gtk_css_parser_error (parser, "Invalid combination of values");
238 _gtk_css_value_unref (x);
239 _gtk_css_value_unref (y);
244 return _gtk_css_position_value_new (x, y);
248 _gtk_css_position_value_get_x (const GtkCssValue *position,
249 double one_hundred_percent)
251 g_return_val_if_fail (position != NULL, 0.0);
252 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
254 return _gtk_css_number_value_get (position->x, one_hundred_percent);
258 _gtk_css_position_value_get_y (const GtkCssValue *position,
259 double one_hundred_percent)
261 g_return_val_if_fail (position != NULL, 0.0);
262 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
264 return _gtk_css_number_value_get (position->y, one_hundred_percent);
268 _gtk_css_position_value_compute (GtkCssValue *position,
269 GtkStyleContext *context)
273 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, NULL);
275 x = _gtk_css_number_value_compute (position->x, context);
276 y = _gtk_css_number_value_compute (position->y, context);
277 if (x == position->x && y == position->y)
279 _gtk_css_value_unref (x);
280 _gtk_css_value_unref (y);
281 return _gtk_css_value_ref (position);
284 return _gtk_css_position_value_new (x, y);