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