]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsspositionvalue.c
notebook: restore previous behaviour wrt. unparenting of tab labels
[~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       if (_gtk_css_parser_has_number (parser))
191         {
192           missing = &y;
193           x = _gtk_css_number_value_parse (parser,
194                                            GTK_CSS_PARSE_PERCENT
195                                            | GTK_CSS_PARSE_LENGTH);
196
197           if (x == NULL)
198             return NULL;
199         }
200       else
201         return NULL;
202     }
203
204   for (second = 0; names[second].name != NULL; second++)
205     {
206       if (_gtk_css_parser_try (parser, names[second].name, TRUE))
207         {
208           *missing = _gtk_css_number_value_new (names[second].percentage, GTK_CSS_PERCENT);
209           break;
210         }
211     }
212
213   if (names[second].name == NULL)
214     {
215       if (_gtk_css_parser_has_number (parser))
216         {
217           if (missing != &y)
218             {
219               _gtk_css_parser_error (parser, "Invalid combination of values");
220               _gtk_css_value_unref (y);
221               return NULL;
222             }
223           y = _gtk_css_number_value_parse (parser,
224                                            GTK_CSS_PARSE_PERCENT
225                                            | GTK_CSS_PARSE_LENGTH);
226           if (y == NULL)
227             {
228               _gtk_css_value_unref (x);
229               return NULL;
230             }
231         }
232       else
233         {
234           second++;
235           *missing = _gtk_css_number_value_new (50, GTK_CSS_PERCENT);
236         }
237     }
238   else
239     {
240       if ((names[first].horizontal && !names[second].vertical) ||
241           (!names[first].horizontal && !names[second].horizontal))
242         {
243           _gtk_css_parser_error (parser, "Invalid combination of values");
244           _gtk_css_value_unref (x);
245           _gtk_css_value_unref (y);
246           return NULL;
247         }
248     }
249
250   return _gtk_css_position_value_new (x, y);
251 }
252
253 double
254 _gtk_css_position_value_get_x (const GtkCssValue *position,
255                                double             one_hundred_percent)
256 {
257   g_return_val_if_fail (position != NULL, 0.0);
258   g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);
259
260   return _gtk_css_number_value_get (position->x, one_hundred_percent);
261 }
262
263 double
264 _gtk_css_position_value_get_y (const GtkCssValue *position,
265                                double             one_hundred_percent)
266 {
267   g_return_val_if_fail (position != NULL, 0.0);
268   g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, 0.0);
269
270   return _gtk_css_number_value_get (position->y, one_hundred_percent);
271 }
272
273 GtkCssValue *
274 _gtk_css_position_value_compute (GtkCssValue     *position,
275                                  GtkStyleContext *context)
276 {
277   GtkCssValue *x, *y;
278
279   g_return_val_if_fail (position->class == &GTK_CSS_VALUE_POSITION, NULL);
280
281   x = _gtk_css_number_value_compute (position->x, context);
282   y = _gtk_css_number_value_compute (position->y, context);
283   if (x == position->x && y == position->y)
284     {
285       _gtk_css_value_unref (x);
286       _gtk_css_value_unref (y);
287       return _gtk_css_value_ref (position);
288     }
289
290   return _gtk_css_position_value_new (x, y);
291 }
292