1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Red Hat, Inc.
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.
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.
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/>.
20 #include "gtkcssnumbervalueprivate.h"
22 #include "gtkcssenumvalueprivate.h"
23 #include "gtkstylepropertyprivate.h"
32 gtk_css_value_number_free (GtkCssValue *value)
34 g_slice_free (GtkCssValue, value);
38 gtk_css_value_number_compute (GtkCssValue *number,
40 GtkStyleContext *context,
41 GtkCssDependencies *dependencies)
43 GtkBorderStyle border_style;
45 /* I don't like this special case being here in this generic code path, but no idea where else to put it. */
48 case GTK_CSS_PROPERTY_BORDER_TOP_WIDTH:
49 border_style = _gtk_css_border_style_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
50 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
51 return _gtk_css_number_value_new (0, GTK_CSS_PX);
53 case GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH:
54 border_style = _gtk_css_border_style_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE));
55 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
56 return _gtk_css_number_value_new (0, GTK_CSS_PX);
58 case GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH:
59 border_style = _gtk_css_border_style_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE));
60 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
61 return _gtk_css_number_value_new (0, GTK_CSS_PX);
63 case GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH:
64 border_style = _gtk_css_border_style_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_LEFT_STYLE));
65 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
66 return _gtk_css_number_value_new (0, GTK_CSS_PX);
68 case GTK_CSS_PROPERTY_OUTLINE_WIDTH:
69 border_style = _gtk_css_border_style_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_OUTLINE_STYLE));
70 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
71 return _gtk_css_number_value_new (0, GTK_CSS_PX);
80 g_assert_not_reached();
87 return _gtk_css_value_ref (number);
89 return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
92 return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
96 return _gtk_css_number_value_new (number->value * 96.0,
100 return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
104 return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
108 *dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
109 return _gtk_css_number_value_new (number->value *
110 _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100),
114 /* for now we pretend ex is half of em */
115 *dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
116 return _gtk_css_number_value_new (number->value * 0.5 *
117 _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100),
120 return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
123 return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
126 return _gtk_css_number_value_new (number->value * 360.0,
129 return _gtk_css_number_value_new (number->value / 1000.0,
135 gtk_css_value_number_equal (const GtkCssValue *number1,
136 const GtkCssValue *number2)
138 return number1->unit == number2->unit &&
139 number1->value == number2->value;
143 gtk_css_value_number_transition (GtkCssValue *start,
147 /* FIXME: This needs to be supported at least for percentages,
148 * but for that we kinda need to support calc(5px + 50%) */
149 if (start->unit != end->unit)
152 return _gtk_css_number_value_new (start->value + (end->value - start->value) * progress,
157 gtk_css_value_number_print (const GtkCssValue *number,
160 char buf[G_ASCII_DTOSTR_BUF_SIZE];
162 const char *names[] = {
163 /* [GTK_CSS_NUMBER] = */ "",
164 /* [GTK_CSS_PERCENT] = */ "%",
165 /* [GTK_CSS_PX] = */ "px",
166 /* [GTK_CSS_PT] = */ "pt",
167 /* [GTK_CSS_EM] = */ "em",
168 /* [GTK_CSS_EX] = */ "ex",
169 /* [GTK_CSS_PC] = */ "pc",
170 /* [GTK_CSS_IN] = */ "in",
171 /* [GTK_CSS_CM] = */ "cm",
172 /* [GTK_CSS_MM] = */ "mm",
173 /* [GTK_CSS_RAD] = */ "rad",
174 /* [GTK_CSS_DEG] = */ "deg",
175 /* [GTK_CSS_GRAD] = */ "grad",
176 /* [GTK_CSS_TURN] = */ "turn",
177 /* [GTK_CSS_S] = */ "s",
178 /* [GTK_CSS_MS] = */ "ms",
181 g_ascii_dtostr (buf, sizeof (buf), number->value);
182 g_string_append (string, buf);
183 if (number->value != 0.0)
184 g_string_append (string, names[number->unit]);
187 static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
188 gtk_css_value_number_free,
189 gtk_css_value_number_compute,
190 gtk_css_value_number_equal,
191 gtk_css_value_number_transition,
192 gtk_css_value_number_print
196 _gtk_css_number_value_new (double value,
199 static GtkCssValue zero_singleton = { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
200 static GtkCssValue px_singletons[] = {
201 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
202 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
203 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
204 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
205 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
209 if (unit == GTK_CSS_NUMBER && value == 0)
210 return _gtk_css_value_ref (&zero_singleton);
212 if (unit == GTK_CSS_PX &&
219 return _gtk_css_value_ref (&px_singletons[(int) value]);
222 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_NUMBER);
224 result->value = value;
230 _gtk_css_number_value_get_unit (const GtkCssValue *value)
232 g_return_val_if_fail (value->class == >K_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
238 _gtk_css_number_value_get (const GtkCssValue *number,
239 double one_hundred_percent)
241 g_return_val_if_fail (number != NULL, 0.0);
242 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, 0.0);
244 if (number->unit == GTK_CSS_PERCENT)
245 return number->value * one_hundred_percent / 100;
247 return number->value;