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