]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssnumbervalue.c
cssvalue: Split number values into their own class
[~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 void
45 gtk_css_value_number_print (const GtkCssValue *number,
46                             GString           *string)
47 {
48   char buf[G_ASCII_DTOSTR_BUF_SIZE];
49
50   const char *names[] = {
51     /* [GTK_CSS_NUMBER] = */ "",
52     /* [GTK_CSS_PERCENT] = */ "%",
53     /* [GTK_CSS_PX] = */ "px",
54     /* [GTK_CSS_PT] = */ "pt",
55     /* [GTK_CSS_EM] = */ "em",
56     /* [GTK_CSS_EX] = */ "ex",
57     /* [GTK_CSS_PC] = */ "pc",
58     /* [GTK_CSS_IN] = */ "in",
59     /* [GTK_CSS_CM] = */ "cm",
60     /* [GTK_CSS_MM] = */ "mm",
61     /* [GTK_CSS_RAD] = */ "rad",
62     /* [GTK_CSS_DEG] = */ "deg",
63     /* [GTK_CSS_GRAD] = */ "grad",
64     /* [GTK_CSS_TURN] = */ "turn",
65   };
66
67   g_ascii_dtostr (buf, sizeof (buf), number->value);
68   g_string_append (string, buf);
69   if (number->value != 0.0)
70     g_string_append (string, names[number->unit]);
71 }
72
73 static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
74   gtk_css_value_number_free,
75   gtk_css_value_number_equal,
76   gtk_css_value_number_print
77 };
78
79 GtkCssValue *
80 _gtk_css_number_value_new (double     value,
81                            GtkCssUnit unit)
82 {
83   static GtkCssValue zero_singleton = { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
84   static GtkCssValue px_singletons[] = {
85     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
86     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
87     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
88     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
89     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
90   };
91   GtkCssValue *result;
92
93   if (unit == GTK_CSS_NUMBER && value == 0)
94     return _gtk_css_value_ref (&zero_singleton);
95
96   if (unit == GTK_CSS_PX &&
97       (value == 0 ||
98        value == 1 ||
99        value == 2 ||
100        value == 3 ||
101        value == 4))
102     {
103       return _gtk_css_value_ref (&px_singletons[(int) value]);
104     }
105
106   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_NUMBER);
107   result->unit = unit;
108   result->value = value;
109
110   return result;
111 }
112
113 GtkCssValue *
114 _gtk_css_number_value_parse (GtkCssParser           *parser,
115                              GtkCssNumberParseFlags  flags)
116 {
117   GtkCssNumber number;
118
119   g_return_val_if_fail (parser != NULL, NULL);
120
121   if (!_gtk_css_parser_read_number (parser, &number, flags))
122     return NULL;
123   
124   return _gtk_css_number_value_new (number.value, number.unit);
125 }
126
127 double
128 _gtk_css_number_value_get (const GtkCssValue *number,
129                            double             one_hundred_percent)
130 {
131   g_return_val_if_fail (number != NULL, 0.0);
132   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, 0.0);
133
134   if (number->unit == GTK_CSS_PERCENT)
135     return number->value * one_hundred_percent / 100;
136   else
137     return number->value;
138 }
139
140 GtkCssValue *
141 _gtk_css_number_value_compute (GtkCssValue     *number,
142                                GtkStyleContext *context)
143 {
144   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, NULL);
145
146   switch (number->unit)
147     {
148     default:
149       g_assert_not_reached();
150       /* fall through */
151     case GTK_CSS_PERCENT:
152     case GTK_CSS_NUMBER:
153     case GTK_CSS_PX:
154     case GTK_CSS_DEG:
155       return _gtk_css_value_ref (number);
156     case GTK_CSS_PT:
157       return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
158                                         GTK_CSS_PX);
159     case GTK_CSS_PC:
160       return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
161                                         GTK_CSS_PX);
162       break;
163     case GTK_CSS_IN:
164       return _gtk_css_number_value_new (number->value * 96.0,
165                                         GTK_CSS_PX);
166       break;
167     case GTK_CSS_CM:
168       return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
169                                         GTK_CSS_PX);
170       break;
171     case GTK_CSS_MM:
172       return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
173                                         GTK_CSS_PX);
174       break;
175     case GTK_CSS_EM:
176       return _gtk_css_number_value_new (number->value * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size")),
177                                         GTK_CSS_PX);
178       break;
179     case GTK_CSS_EX:
180       /* for now we pretend ex is half of em */
181       return _gtk_css_number_value_new (number->value * 0.5 * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size")),
182                                         GTK_CSS_PX);
183     case GTK_CSS_RAD:
184       return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
185                                         GTK_CSS_DEG);
186     case GTK_CSS_GRAD:
187       return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
188                                         GTK_CSS_DEG);
189     case GTK_CSS_TURN:
190       return _gtk_css_number_value_new (number->value * 360.0,
191                                         GTK_CSS_DEG);
192     }
193 }
194