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