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)
46 x = _gtk_css_value_compute (position->x, property_id, context);
47 y = _gtk_css_value_compute (position->y, property_id, context);
48 if (x == position->x && y == position->y)
50 _gtk_css_value_unref (x);
51 _gtk_css_value_unref (y);
52 return _gtk_css_value_ref (position);
55 return _gtk_css_position_value_new (x, y);
59 gtk_css_value_position_equal (const GtkCssValue *position1,
60 const GtkCssValue *position2)
62 return _gtk_css_value_equal (position1->x, position2->x)
63 && _gtk_css_value_equal (position1->y, position2->y);
67 gtk_css_value_position_transition (GtkCssValue *start,
73 x = _gtk_css_value_transition (start->x, end->x, progress);
76 y = _gtk_css_value_transition (start->y, end->y, progress);
79 _gtk_css_value_unref (x);
83 return _gtk_css_position_value_new (x, y);
87 gtk_css_value_position_print (const GtkCssValue *position,
95 { "left", "top", _gtk_css_number_value_new (0, GTK_CSS_PERCENT) },
96 { "right", "bottom", _gtk_css_number_value_new (100, GTK_CSS_PERCENT) }
98 GtkCssValue *center = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
101 if (_gtk_css_value_equal (position->x, center))
103 if (_gtk_css_value_equal (position->y, center))
105 g_string_append (string, "center");
111 for (i = 0; i < G_N_ELEMENTS (values); i++)
113 if (_gtk_css_value_equal (position->x, values[i].number))
115 g_string_append (string, values[i].x_name);
119 if (i == G_N_ELEMENTS (values))
120 _gtk_css_value_print (position->x, string);
122 if (_gtk_css_value_equal (position->y, center))
125 g_string_append_c (string, ' ');
128 for (i = 0; i < G_N_ELEMENTS (values); i++)
130 if (_gtk_css_value_equal (position->y, values[i].number))
132 g_string_append (string, values[i].y_name);
136 if (i == G_N_ELEMENTS (values))
138 if (_gtk_css_value_equal (position->x, center))
139 g_string_append (string, "center ");
140 _gtk_css_value_print (position->y, string);
144 for (i = 0; i < G_N_ELEMENTS (values); i++)
145 _gtk_css_value_unref (values[i].number);
146 _gtk_css_value_unref (center);
149 static const GtkCssValueClass GTK_CSS_VALUE_POSITION = {
150 gtk_css_value_position_free,
151 gtk_css_value_position_compute,
152 gtk_css_value_position_equal,
153 gtk_css_value_position_transition,
154 gtk_css_value_position_print
158 _gtk_css_position_value_new (GtkCssValue *x,
163 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_POSITION);
171 _gtk_css_position_value_parse (GtkCssParser *parser)
173 static const struct {
179 { "left", 0, TRUE, FALSE },
180 { "right", 100, TRUE, FALSE },
181 { "center", 50, TRUE, TRUE },
182 { "top", 0, FALSE, TRUE },
183 { "bottom", 100, FALSE, TRUE },
184 { NULL , 0, TRUE, FALSE }, /* used for numbers */
185 { NULL , 50, TRUE, TRUE } /* used for no value */
188 GtkCssValue **missing;
191 for (first = 0; names[first].name != NULL; first++)
193 if (_gtk_css_parser_try (parser, names[first].name, TRUE))
195 if (names[first].horizontal)
197 x = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
202 y = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
208 if (names[first].name == NULL)
210 if (_gtk_css_parser_has_number (parser))
213 x = _gtk_css_number_value_parse (parser,
214 GTK_CSS_PARSE_PERCENT
215 | GTK_CSS_PARSE_LENGTH);
224 for (second = 0; names[second].name != NULL; second++)
226 if (_gtk_css_parser_try (parser, names[second].name, TRUE))
228 *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
233 if (names[second].name == NULL)
235 if (_gtk_css_parser_has_number (parser))
239 _gtk_css_parser_error (parser, "Invalid combination of values");
240 _gtk_css_value_unref (y);
243 y = _gtk_css_number_value_parse (parser,
244 GTK_CSS_PARSE_PERCENT
245 | GTK_CSS_PARSE_LENGTH);
248 _gtk_css_value_unref (x);
255 *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
260 if ((names[first].horizontal && !names[second].vertical) ||
261 (!names[first].horizontal && !names[second].horizontal))
263 _gtk_css_parser_error (parser, "Invalid combination of values");
264 _gtk_css_value_unref (x);
265 _gtk_css_value_unref (y);
270 return _gtk_css_position_value_new (x, y);
274 _gtk_css_position_value_get_x (const GtkCssValue *position,
275 double one_hundred_percent)
277 g_return_val_if_fail (position != NULL, 0.0);
278 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
280 return _gtk_css_number_value_get (position->x, one_hundred_percent);
284 _gtk_css_position_value_get_y (const GtkCssValue *position,
285 double one_hundred_percent)
287 g_return_val_if_fail (position != NULL, 0.0);
288 g_return_val_if_fail (position->class == >K_CSS_VALUE_POSITION, 0.0);
290 return _gtk_css_number_value_get (position->y, one_hundred_percent);