]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssnumbervalue.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcssnumbervalue.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 "gtkcssnumbervalueprivate.h"
21
22 #include "gtkcssenumvalueprivate.h"
23 #include "gtkcssinitialvalueprivate.h"
24 #include "gtkstylepropertyprivate.h"
25
26 struct _GtkCssValue {
27   GTK_CSS_VALUE_BASE
28   GtkCssUnit unit;
29   double value;
30 };
31
32 static void
33 gtk_css_value_number_free (GtkCssValue *value)
34 {
35   g_slice_free (GtkCssValue, value);
36 }
37
38 static double
39 get_base_font_size (guint                    property_id,
40                     GtkStyleProviderPrivate *provider,
41                     GtkCssComputedValues    *values,
42                     GtkCssComputedValues    *parent_values,
43                     GtkCssDependencies      *dependencies)
44 {
45   if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
46     {
47       *dependencies = GTK_CSS_DEPENDS_ON_PARENT;
48       if (parent_values)
49         return _gtk_css_number_value_get (_gtk_css_computed_values_get_value (parent_values, GTK_CSS_PROPERTY_FONT_SIZE), 100);
50       else
51         return _gtk_css_font_size_get_default (provider);
52     }
53
54   *dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
55   return _gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100);
56 }
57                     
58 static GtkCssValue *
59 gtk_css_value_number_compute (GtkCssValue             *number,
60                               guint                    property_id,
61                               GtkStyleProviderPrivate *provider,
62                               GtkCssComputedValues    *values,
63                               GtkCssComputedValues    *parent_values,
64                               GtkCssDependencies      *dependencies)
65 {
66   switch (number->unit)
67     {
68     default:
69       g_assert_not_reached();
70       /* fall through */
71     case GTK_CSS_PERCENT:
72       /* percentages for font sizes are computed, other percentages aren't */
73       if (property_id == GTK_CSS_PROPERTY_FONT_SIZE)
74         return _gtk_css_number_value_new (number->value / 100.0 * 
75                                           get_base_font_size (property_id, provider, values, parent_values, dependencies),
76                                           GTK_CSS_PX);
77     case GTK_CSS_NUMBER:
78     case GTK_CSS_PX:
79     case GTK_CSS_DEG:
80     case GTK_CSS_S:
81       return _gtk_css_value_ref (number);
82     case GTK_CSS_PT:
83       return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
84                                         GTK_CSS_PX);
85     case GTK_CSS_PC:
86       return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
87                                         GTK_CSS_PX);
88       break;
89     case GTK_CSS_IN:
90       return _gtk_css_number_value_new (number->value * 96.0,
91                                         GTK_CSS_PX);
92       break;
93     case GTK_CSS_CM:
94       return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
95                                         GTK_CSS_PX);
96       break;
97     case GTK_CSS_MM:
98       return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
99                                         GTK_CSS_PX);
100       break;
101     case GTK_CSS_EM:
102       return _gtk_css_number_value_new (number->value *
103                                         get_base_font_size (property_id, provider, values, parent_values, dependencies),
104                                         GTK_CSS_PX);
105       break;
106     case GTK_CSS_EX:
107       /* for now we pretend ex is half of em */
108       return _gtk_css_number_value_new (number->value * 0.5 * 
109                                         get_base_font_size (property_id, provider, values, parent_values, dependencies),
110                                         GTK_CSS_PX);
111     case GTK_CSS_RAD:
112       return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
113                                         GTK_CSS_DEG);
114     case GTK_CSS_GRAD:
115       return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
116                                         GTK_CSS_DEG);
117     case GTK_CSS_TURN:
118       return _gtk_css_number_value_new (number->value * 360.0,
119                                         GTK_CSS_DEG);
120     case GTK_CSS_MS:
121       return _gtk_css_number_value_new (number->value / 1000.0,
122                                         GTK_CSS_S);
123     }
124 }
125
126 static gboolean
127 gtk_css_value_number_equal (const GtkCssValue *number1,
128                             const GtkCssValue *number2)
129 {
130   return number1->unit == number2->unit &&
131          number1->value == number2->value;
132 }
133
134 static GtkCssValue *
135 gtk_css_value_number_transition (GtkCssValue *start,
136                                  GtkCssValue *end,
137                                  guint        property_id,
138                                  double       progress)
139 {
140   /* FIXME: This needs to be supported at least for percentages,
141    * but for that we kinda need to support calc(5px + 50%) */
142   if (start->unit != end->unit)
143     return NULL;
144
145   return _gtk_css_number_value_new (start->value + (end->value - start->value) * progress,
146                                     start->unit);
147 }
148
149 static void
150 gtk_css_value_number_print (const GtkCssValue *number,
151                             GString           *string)
152 {
153   char buf[G_ASCII_DTOSTR_BUF_SIZE];
154
155   const char *names[] = {
156     /* [GTK_CSS_NUMBER] = */ "",
157     /* [GTK_CSS_PERCENT] = */ "%",
158     /* [GTK_CSS_PX] = */ "px",
159     /* [GTK_CSS_PT] = */ "pt",
160     /* [GTK_CSS_EM] = */ "em",
161     /* [GTK_CSS_EX] = */ "ex",
162     /* [GTK_CSS_PC] = */ "pc",
163     /* [GTK_CSS_IN] = */ "in",
164     /* [GTK_CSS_CM] = */ "cm",
165     /* [GTK_CSS_MM] = */ "mm",
166     /* [GTK_CSS_RAD] = */ "rad",
167     /* [GTK_CSS_DEG] = */ "deg",
168     /* [GTK_CSS_GRAD] = */ "grad",
169     /* [GTK_CSS_TURN] = */ "turn",
170     /* [GTK_CSS_S] = */ "s",
171     /* [GTK_CSS_MS] = */ "ms",
172   };
173
174   g_ascii_dtostr (buf, sizeof (buf), number->value);
175   g_string_append (string, buf);
176   if (number->value != 0.0)
177     g_string_append (string, names[number->unit]);
178 }
179
180 static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
181   gtk_css_value_number_free,
182   gtk_css_value_number_compute,
183   gtk_css_value_number_equal,
184   gtk_css_value_number_transition,
185   gtk_css_value_number_print
186 };
187
188 GtkCssValue *
189 _gtk_css_number_value_new (double     value,
190                            GtkCssUnit unit)
191 {
192   static GtkCssValue number_singletons[] = {
193     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 },
194     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 1 },
195   };
196   static GtkCssValue px_singletons[] = {
197     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
198     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
199     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
200     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
201     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
202   };
203   GtkCssValue *result;
204
205   if (unit == GTK_CSS_NUMBER && (value == 0 || value == 1))
206     return _gtk_css_value_ref (&number_singletons[(int) value]);
207
208   if (unit == GTK_CSS_PX &&
209       (value == 0 ||
210        value == 1 ||
211        value == 2 ||
212        value == 3 ||
213        value == 4))
214     {
215       return _gtk_css_value_ref (&px_singletons[(int) value]);
216     }
217
218   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_NUMBER);
219   result->unit = unit;
220   result->value = value;
221
222   return result;
223 }
224
225 GtkCssUnit
226 _gtk_css_number_value_get_unit (const GtkCssValue *value)
227 {
228   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
229
230   return value->unit;
231 }
232
233 double
234 _gtk_css_number_value_get (const GtkCssValue *number,
235                            double             one_hundred_percent)
236 {
237   g_return_val_if_fail (number != NULL, 0.0);
238   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, 0.0);
239
240   if (number->unit == GTK_CSS_PERCENT)
241     return number->value * one_hundred_percent / 100;
242   else
243     return number->value;
244 }
245