]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsspositionvalue.c
9f4b0d3f0ab6c062f039c36112c70b898cc5ccd5
[~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 {
44   GtkCssValue *x, *y;
45
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)
49     {
50       _gtk_css_value_unref (x);
51       _gtk_css_value_unref (y);
52       return _gtk_css_value_ref (position);
53     }
54
55   return _gtk_css_position_value_new (x, y);
56 }
57
58 static gboolean
59 gtk_css_value_position_equal (const GtkCssValue *position1,
60                               const GtkCssValue *position2)
61 {
62   return _gtk_css_value_equal (position1->x, position2->x)
63       && _gtk_css_value_equal (position1->y, position2->y);
64 }
65
66 static GtkCssValue *
67 gtk_css_value_position_transition (GtkCssValue *start,
68                                    GtkCssValue *end,
69                                    double       progress)
70 {
71   GtkCssValue *x, *y;
72
73   x = _gtk_css_value_transition (start->x, end->x, progress);
74   if (x == NULL)
75     return NULL;
76   y = _gtk_css_value_transition (start->y, end->y, progress);
77   if (y == NULL)
78     {
79       _gtk_css_value_unref (x);
80       return NULL;
81     }
82
83   return _gtk_css_position_value_new (x, y);
84 }
85
86 static void
87 gtk_css_value_position_print (const GtkCssValue *position,
88                               GString           *string)
89 {
90   struct {
91     const char *x_name;
92     const char *y_name;
93     GtkCssValue *number;
94   } values[] = { 
95     { "left",   "top",    _gtk_css_number_value_new (0, GTK_CSS_PERCENT) },
96     { "right",  "bottom", _gtk_css_number_value_new (100, GTK_CSS_PERCENT) }
97   };
98   GtkCssValue *center = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
99   guint i;
100
101   if (_gtk_css_value_equal (position->x, center))
102     {
103       if (_gtk_css_value_equal (position->y, center))
104         {
105           g_string_append (string, "center");
106           goto done;
107         }
108     }
109   else
110     {
111       for (i = 0; i < G_N_ELEMENTS (values); i++)
112         {
113           if (_gtk_css_value_equal (position->x, values[i].number))
114             {
115               g_string_append (string, values[i].x_name);
116               break;
117             }
118         }
119       if (i == G_N_ELEMENTS (values))
120         _gtk_css_value_print (position->x, string);
121
122       if (_gtk_css_value_equal (position->y, center))
123         goto done;
124
125       g_string_append_c (string, ' ');
126     }
127
128   for (i = 0; i < G_N_ELEMENTS (values); i++)
129     {
130       if (_gtk_css_value_equal (position->y, values[i].number))
131         {
132           g_string_append (string, values[i].y_name);
133           goto done;
134         }
135     }
136   if (i == G_N_ELEMENTS (values))
137     {
138       if (_gtk_css_value_equal (position->x, center))
139         g_string_append (string, "center ");
140       _gtk_css_value_print (position->y, string);
141     }
142
143 done:
144   for (i = 0; i < G_N_ELEMENTS (values); i++)
145     _gtk_css_value_unref (values[i].number);
146   _gtk_css_value_unref (center);
147 }
148
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
155 };
156
157 GtkCssValue *
158 _gtk_css_position_value_new (GtkCssValue *x,
159                              GtkCssValue *y)
160 {
161   GtkCssValue *result;
162
163   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_POSITION);
164   result->x = x;
165   result->y = y;
166
167   return result;
168 }
169
170 GtkCssValue *
171 _gtk_css_position_value_parse (GtkCssParser *parser)
172 {
173   static const struct {
174     const char *name;
175     guint       percentage;
176     gboolean    horizontal;
177     gboolean    vertical;
178   } names[] = {
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 */
186   };
187   GtkCssValue *x, *y;
188   GtkCssValue **missing;
189   guint first, second;
190
191   for (first = 0; names[first].name != NULL; first++)
192     {
193       if (_gtk_css_parser_try (parser, names[first].name, TRUE))
194         {
195           if (names[first].horizontal)
196             {
197               x = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
198               missing = &y;
199             }
200           else
201             {
202               y = _gtk_css_number_value_new (names[first].percentage, GTK_CSS_PERCENT);
203               missing = &x;
204             }
205           break;
206         }
207     }
208   if (names[first].name == NULL)
209     {
210       if (_gtk_css_parser_has_number (parser))
211         {
212           missing = &y;
213           x = _gtk_css_number_value_parse (parser,
214                                            GTK_CSS_PARSE_PERCENT
215                                            | GTK_CSS_PARSE_LENGTH);
216
217           if (x == NULL)
218             return NULL;
219         }
220       else
221         return NULL;
222     }
223
224   for (second = 0; names[second].name != NULL; second++)
225     {
226       if (_gtk_css_parser_try (parser, names[second].name, TRUE))
227         {
228           *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
229           break;
230         }
231     }
232
233   if (names[second].name == NULL)
234     {
235       if (_gtk_css_parser_has_number (parser))
236         {
237           if (missing != &y)
238             {
239               _gtk_css_parser_error (parser, "Invalid combination of values");
240               _gtk_css_value_unref (y);
241               return NULL;
242             }
243           y = _gtk_css_number_value_parse (parser,
244                                            GTK_CSS_PARSE_PERCENT
245                                            | GTK_CSS_PARSE_LENGTH);
246           if (y == NULL)
247             {
248               _gtk_css_value_unref (x);
249               return NULL;
250             }
251         }
252       else
253         {
254           second++;
255           *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
256         }
257     }
258   else
259     {
260       if ((names[first].horizontal && !names[second].vertical) ||
261           (!names[first].horizontal && !names[second].horizontal))
262         {
263           _gtk_css_parser_error (parser, "Invalid combination of values");
264           _gtk_css_value_unref (x);
265           _gtk_css_value_unref (y);
266           return NULL;
267         }
268     }
269
270   return _gtk_css_position_value_new (x, y);
271 }
272
273 double
274 _gtk_css_position_value_get_x (const GtkCssValue *position,
275                                double             one_hundred_percent)
276 {
277   g_return_val_if_fail (position != NULL, 0.0);
278   g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);
279
280   return _gtk_css_number_value_get (position->x, one_hundred_percent);
281 }
282
283 double
284 _gtk_css_position_value_get_y (const GtkCssValue *position,
285                                double             one_hundred_percent)
286 {
287   g_return_val_if_fail (position != NULL, 0.0);
288   g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);
289
290   return _gtk_css_number_value_get (position->y, one_hundred_percent);
291 }
292