]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssnumbervalue.c
[Bug 675501] gtkquartz.h is not in the gtk+-3.5.2.tar.xz archive
[~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 GtkCssUnit
131 _gtk_css_number_value_get_unit (const GtkCssValue *value)
132 {
133   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
134
135   return value->unit;
136 }
137
138 double
139 _gtk_css_number_value_get (const GtkCssValue *number,
140                            double             one_hundred_percent)
141 {
142   g_return_val_if_fail (number != NULL, 0.0);
143   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, 0.0);
144
145   if (number->unit == GTK_CSS_PERCENT)
146     return number->value * one_hundred_percent / 100;
147   else
148     return number->value;
149 }
150
151 GtkCssValue *
152 _gtk_css_number_value_compute (GtkCssValue     *number,
153                                GtkStyleContext *context)
154 {
155   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, NULL);
156
157   switch (number->unit)
158     {
159     default:
160       g_assert_not_reached();
161       /* fall through */
162     case GTK_CSS_PERCENT:
163     case GTK_CSS_NUMBER:
164     case GTK_CSS_PX:
165     case GTK_CSS_DEG:
166     case GTK_CSS_S:
167       return _gtk_css_value_ref (number);
168     case GTK_CSS_PT:
169       return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
170                                         GTK_CSS_PX);
171     case GTK_CSS_PC:
172       return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
173                                         GTK_CSS_PX);
174       break;
175     case GTK_CSS_IN:
176       return _gtk_css_number_value_new (number->value * 96.0,
177                                         GTK_CSS_PX);
178       break;
179     case GTK_CSS_CM:
180       return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
181                                         GTK_CSS_PX);
182       break;
183     case GTK_CSS_MM:
184       return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
185                                         GTK_CSS_PX);
186       break;
187     case GTK_CSS_EM:
188       return _gtk_css_number_value_new (number->value *
189                                         _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100),
190                                         GTK_CSS_PX);
191       break;
192     case GTK_CSS_EX:
193       /* for now we pretend ex is half of em */
194       return _gtk_css_number_value_new (number->value * 0.5 * 
195                                         _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100),
196                                         GTK_CSS_PX);
197     case GTK_CSS_RAD:
198       return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
199                                         GTK_CSS_DEG);
200     case GTK_CSS_GRAD:
201       return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
202                                         GTK_CSS_DEG);
203     case GTK_CSS_TURN:
204       return _gtk_css_number_value_new (number->value * 360.0,
205                                         GTK_CSS_DEG);
206     case GTK_CSS_MS:
207       return _gtk_css_number_value_new (number->value / 1000.0,
208                                         GTK_CSS_S);
209     }
210 }
211