]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssnumbervalue.c
cssvalue: Add _gtk_css_value_transition()
[~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   };
80
81   g_ascii_dtostr (buf, sizeof (buf), number->value);
82   g_string_append (string, buf);
83   if (number->value != 0.0)
84     g_string_append (string, names[number->unit]);
85 }
86
87 static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
88   gtk_css_value_number_free,
89   gtk_css_value_number_equal,
90   gtk_css_value_number_transition,
91   gtk_css_value_number_print
92 };
93
94 GtkCssValue *
95 _gtk_css_number_value_new (double     value,
96                            GtkCssUnit unit)
97 {
98   static GtkCssValue zero_singleton = { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
99   static GtkCssValue px_singletons[] = {
100     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
101     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
102     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
103     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
104     { &GTK_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
105   };
106   GtkCssValue *result;
107
108   if (unit == GTK_CSS_NUMBER && value == 0)
109     return _gtk_css_value_ref (&zero_singleton);
110
111   if (unit == GTK_CSS_PX &&
112       (value == 0 ||
113        value == 1 ||
114        value == 2 ||
115        value == 3 ||
116        value == 4))
117     {
118       return _gtk_css_value_ref (&px_singletons[(int) value]);
119     }
120
121   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_NUMBER);
122   result->unit = unit;
123   result->value = value;
124
125   return result;
126 }
127
128 GtkCssValue *
129 _gtk_css_number_value_parse (GtkCssParser           *parser,
130                              GtkCssNumberParseFlags  flags)
131 {
132   GtkCssNumber number;
133
134   g_return_val_if_fail (parser != NULL, NULL);
135
136   if (!_gtk_css_parser_read_number (parser, &number, flags))
137     return NULL;
138   
139   return _gtk_css_number_value_new (number.value, number.unit);
140 }
141
142 double
143 _gtk_css_number_value_get (const GtkCssValue *number,
144                            double             one_hundred_percent)
145 {
146   g_return_val_if_fail (number != NULL, 0.0);
147   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, 0.0);
148
149   if (number->unit == GTK_CSS_PERCENT)
150     return number->value * one_hundred_percent / 100;
151   else
152     return number->value;
153 }
154
155 GtkCssValue *
156 _gtk_css_number_value_compute (GtkCssValue     *number,
157                                GtkStyleContext *context)
158 {
159   g_return_val_if_fail (number->class == &GTK_CSS_VALUE_NUMBER, NULL);
160
161   switch (number->unit)
162     {
163     default:
164       g_assert_not_reached();
165       /* fall through */
166     case GTK_CSS_PERCENT:
167     case GTK_CSS_NUMBER:
168     case GTK_CSS_PX:
169     case GTK_CSS_DEG:
170       return _gtk_css_value_ref (number);
171     case GTK_CSS_PT:
172       return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
173                                         GTK_CSS_PX);
174     case GTK_CSS_PC:
175       return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
176                                         GTK_CSS_PX);
177       break;
178     case GTK_CSS_IN:
179       return _gtk_css_number_value_new (number->value * 96.0,
180                                         GTK_CSS_PX);
181       break;
182     case GTK_CSS_CM:
183       return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
184                                         GTK_CSS_PX);
185       break;
186     case GTK_CSS_MM:
187       return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
188                                         GTK_CSS_PX);
189       break;
190     case GTK_CSS_EM:
191       return _gtk_css_number_value_new (number->value *
192                                         _gtk_css_number_value_get (_gtk_style_context_peek_property (context, "font-size"), 100),
193                                         GTK_CSS_PX);
194       break;
195     case GTK_CSS_EX:
196       /* for now we pretend ex is half of em */
197       return _gtk_css_number_value_new (number->value * 0.5 * 
198                                         _gtk_css_number_value_get (_gtk_style_context_peek_property (context, "font-size"), 100),
199                                         GTK_CSS_PX);
200     case GTK_CSS_RAD:
201       return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
202                                         GTK_CSS_DEG);
203     case GTK_CSS_GRAD:
204       return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
205                                         GTK_CSS_DEG);
206     case GTK_CSS_TURN:
207       return _gtk_css_number_value_new (number->value * 360.0,
208                                         GTK_CSS_DEG);
209     }
210 }
211