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