]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsspositionvalue.c
cssvalue: Pass property ID to transition function
[~andy/gtk] / gtk / gtkcsspositionvalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcsspositionvalueprivate.h"
21
22 #include "gtkcssnumbervalueprivate.h"
23
24 struct _GtkCssValue {
25   GTK_CSS_VALUE_BASE
26   GtkCssValue *x;
27   GtkCssValue *y;
28 };
29
30 static void
31 gtk_css_value_position_free (GtkCssValue *value)
32 {
33   _gtk_css_value_unref (value->x);
34   _gtk_css_value_unref (value->y);
35
36   g_slice_free (GtkCssValue, value);
37 }
38
39 static GtkCssValue *
40 gtk_css_value_position_compute (GtkCssValue        *position,
41                                 guint               property_id,
42                                 GtkStyleContext    *context,
43                                 GtkCssDependencies *dependencies)
44 {
45   GtkCssValue *x, *y;
46   GtkCssDependencies x_deps, y_deps;
47
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)
52     {
53       _gtk_css_value_unref (x);
54       _gtk_css_value_unref (y);
55       return _gtk_css_value_ref (position);
56     }
57
58   return _gtk_css_position_value_new (x, y);
59 }
60
61 static gboolean
62 gtk_css_value_position_equal (const GtkCssValue *position1,
63                               const GtkCssValue *position2)
64 {
65   return _gtk_css_value_equal (position1->x, position2->x)
66       && _gtk_css_value_equal (position1->y, position2->y);
67 }
68
69 static GtkCssValue *
70 gtk_css_value_position_transition (GtkCssValue *start,
71                                    GtkCssValue *end,
72                                    guint        property_id,
73                                    double       progress)
74 {
75   GtkCssValue *x, *y;
76
77   x = _gtk_css_value_transition (start->x, end->x, property_id, progress);
78   if (x == NULL)
79     return NULL;
80   y = _gtk_css_value_transition (start->y, end->y, property_id, progress);
81   if (y == NULL)
82     {
83       _gtk_css_value_unref (x);
84       return NULL;
85     }
86
87   return _gtk_css_position_value_new (x, y);
88 }
89
90 static void
91 gtk_css_value_position_print (const GtkCssValue *position,
92                               GString           *string)
93 {
94   struct {
95     const char *x_name;
96     const char *y_name;
97     GtkCssValue *number;
98   } values[] = { 
99     { "left",   "top",    _gtk_css_number_value_new (0, GTK_CSS_PERCENT) },
100     { "right",  "bottom", _gtk_css_number_value_new (100, GTK_CSS_PERCENT) }
101   };
102   GtkCssValue *center = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
103   guint i;
104
105   if (_gtk_css_value_equal (position->x, center))
106     {
107       if (_gtk_css_value_equal (position->y, center))
108         {
109           g_string_append (string, "center");
110           goto done;
111         }
112     }
113   else
114     {
115       for (i = 0; i < G_N_ELEMENTS (values); i++)
116         {
117           if (_gtk_css_value_equal (position->x, values[i].number))
118             {
119               g_string_append (string, values[i].x_name);
120               break;
121             }
122         }
123       if (i == G_N_ELEMENTS (values))
124         _gtk_css_value_print (position->x, string);
125
126       if (_gtk_css_value_equal (position->y, center))
127         goto done;
128
129       g_string_append_c (string, ' ');
130     }
131
132   for (i = 0; i < G_N_ELEMENTS (values); i++)
133     {
134       if (_gtk_css_value_equal (position->y, values[i].number))
135         {
136           g_string_append (string, values[i].y_name);
137           goto done;
138         }
139     }
140   if (i == G_N_ELEMENTS (values))
141     {
142       if (_gtk_css_value_equal (position->x, center))
143         g_string_append (string, "center ");
144       _gtk_css_value_print (position->y, string);
145     }
146
147 done:
148   for (i = 0; i < G_N_ELEMENTS (values); i++)
149     _gtk_css_value_unref (values[i].number);
150   _gtk_css_value_unref (center);
151 }
152
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
159 };
160
161 GtkCssValue *
162 _gtk_css_position_value_new (GtkCssValue *x,
163                              GtkCssValue *y)
164 {
165   GtkCssValue *result;
166
167   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_POSITION);
168   result->x = x;
169   result->y = y;
170
171   return result;
172 }
173
174 GtkCssValue *
175 _gtk_css_position_value_parse (GtkCssParser *parser)
176 {
177   static const struct {
178     const char *name;
179     guint       percentage;
180     gboolean    horizontal;
181     gboolean    vertical;
182   } names[] = {
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 */
190   };
191   GtkCssValue *x, *y;
192   GtkCssValue **missing;
193   guint first, second;
194
195   for (first = 0; names[first].name != NULL; first++)
196     {
197       if (_gtk_css_parser_try (parser, names[first].name, TRUE))
198         {
199           if (names[first].horizontal)
200             {
201               x = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
202               missing = &y;
203             }
204           else
205             {
206               y = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
207               missing = &x;
208             }
209           break;
210         }
211     }
212   if (names[first].name == NULL)
213     {
214       if (_gtk_css_parser_has_number (parser))
215         {
216           missing = &y;
217           x = _gtk_css_number_value_parse (parser,
218                                            GTK_CSS_PARSE_PERCENT
219                                            | GTK_CSS_PARSE_LENGTH);
220
221           if (x == NULL)
222             return NULL;
223         }
224       else
225         return NULL;
226     }
227
228   for (second = 0; names[second].name != NULL; second++)
229     {
230       if (_gtk_css_parser_try (parser, names[second].name, TRUE))
231         {
232           *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
233           break;
234         }
235     }
236
237   if (names[second].name == NULL)
238     {
239       if (_gtk_css_parser_has_number (parser))
240         {
241           if (missing != &y)
242             {
243               _gtk_css_parser_error (parser, "Invalid combination of values");
244               _gtk_css_value_unref (y);
245               return NULL;
246             }
247           y = _gtk_css_number_value_parse (parser,
248                                            GTK_CSS_PARSE_PERCENT
249                                            | GTK_CSS_PARSE_LENGTH);
250           if (y == NULL)
251             {
252               _gtk_css_value_unref (x);
253               return NULL;
254             }
255         }
256       else
257         {
258           second++;
259           *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
260         }
261     }
262   else
263     {
264       if ((names[first].horizontal && !names[second].vertical) ||
265           (!names[first].horizontal && !names[second].horizontal))
266         {
267           _gtk_css_parser_error (parser, "Invalid combination of values");
268           _gtk_css_value_unref (x);
269           _gtk_css_value_unref (y);
270           return NULL;
271         }
272     }
273
274   return _gtk_css_position_value_new (x, y);
275 }
276
277 double
278 _gtk_css_position_value_get_x (const GtkCssValue *position,
279                                double             one_hundred_percent)
280 {
281   g_return_val_if_fail (position != NULL, 0.0);
282   g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);
283
284   return _gtk_css_number_value_get (position->x, one_hundred_percent);
285 }
286
287 double
288 _gtk_css_position_value_get_y (const GtkCssValue *position,
289                                double             one_hundred_percent)
290 {
291   g_return_val_if_fail (position != NULL, 0.0);
292   g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);
293
294   return _gtk_css_number_value_get (position->y, one_hundred_percent);
295 }
296