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 GtkStyleProviderPrivate *provider,
41 GtkCssComputedValues *values,
42 GtkCssComputedValues *parent_values,
43 GtkCssDependencies *dependencies)
45 GtkBorderStyle border_style;
47 /* I don't like this special case being here in this generic code path, but no idea where else to put it. */
50 case GTK_CSS_PROPERTY_BORDER_TOP_WIDTH:
51 border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
52 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
53 return _gtk_css_number_value_new (0, GTK_CSS_PX);
55 case GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH:
56 border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE));
57 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
58 return _gtk_css_number_value_new (0, GTK_CSS_PX);
60 case GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH:
61 border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE));
62 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
63 return _gtk_css_number_value_new (0, GTK_CSS_PX);
65 case GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH:
66 border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_BORDER_LEFT_STYLE));
67 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
68 return _gtk_css_number_value_new (0, GTK_CSS_PX);
70 case GTK_CSS_PROPERTY_OUTLINE_WIDTH:
71 border_style = _gtk_css_border_style_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_OUTLINE_STYLE));
72 if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
73 return _gtk_css_number_value_new (0, GTK_CSS_PX);
82 g_assert_not_reached();
89 return _gtk_css_value_ref (number);
91 return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
94 return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
98 return _gtk_css_number_value_new (number->value * 96.0,
102 return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
106 return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
110 *dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
111 return _gtk_css_number_value_new (number->value *
112 _gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100),
116 /* for now we pretend ex is half of em */
117 *dependencies = GTK_CSS_DEPENDS_ON_FONT_SIZE;
118 return _gtk_css_number_value_new (number->value * 0.5 *
119 _gtk_css_number_value_get (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_FONT_SIZE), 100),
122 return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
125 return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
128 return _gtk_css_number_value_new (number->value * 360.0,
131 return _gtk_css_number_value_new (number->value / 1000.0,
137 gtk_css_value_number_equal (const GtkCssValue *number1,
138 const GtkCssValue *number2)
140 return number1->unit == number2->unit &&
141 number1->value == number2->value;
145 gtk_css_value_number_transition (GtkCssValue *start,
150 /* FIXME: This needs to be supported at least for percentages,
151 * but for that we kinda need to support calc(5px + 50%) */
152 if (start->unit != end->unit)
155 return _gtk_css_number_value_new (start->value + (end->value - start->value) * progress,
160 gtk_css_value_number_print (const GtkCssValue *number,
163 char buf[G_ASCII_DTOSTR_BUF_SIZE];
165 const char *names[] = {
166 /* [GTK_CSS_NUMBER] = */ "",
167 /* [GTK_CSS_PERCENT] = */ "%",
168 /* [GTK_CSS_PX] = */ "px",
169 /* [GTK_CSS_PT] = */ "pt",
170 /* [GTK_CSS_EM] = */ "em",
171 /* [GTK_CSS_EX] = */ "ex",
172 /* [GTK_CSS_PC] = */ "pc",
173 /* [GTK_CSS_IN] = */ "in",
174 /* [GTK_CSS_CM] = */ "cm",
175 /* [GTK_CSS_MM] = */ "mm",
176 /* [GTK_CSS_RAD] = */ "rad",
177 /* [GTK_CSS_DEG] = */ "deg",
178 /* [GTK_CSS_GRAD] = */ "grad",
179 /* [GTK_CSS_TURN] = */ "turn",
180 /* [GTK_CSS_S] = */ "s",
181 /* [GTK_CSS_MS] = */ "ms",
184 g_ascii_dtostr (buf, sizeof (buf), number->value);
185 g_string_append (string, buf);
186 if (number->value != 0.0)
187 g_string_append (string, names[number->unit]);
190 static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
191 gtk_css_value_number_free,
192 gtk_css_value_number_compute,
193 gtk_css_value_number_equal,
194 gtk_css_value_number_transition,
195 gtk_css_value_number_print
199 _gtk_css_number_value_new (double value,
202 static GtkCssValue zero_singleton = { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
203 static GtkCssValue px_singletons[] = {
204 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
205 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
206 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
207 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
208 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
212 if (unit == GTK_CSS_NUMBER && value == 0)
213 return _gtk_css_value_ref (&zero_singleton);
215 if (unit == GTK_CSS_PX &&
222 return _gtk_css_value_ref (&px_singletons[(int) value]);
225 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_NUMBER);
227 result->value = value;
233 _gtk_css_number_value_get_unit (const GtkCssValue *value)
235 g_return_val_if_fail (value->class == >K_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
241 _gtk_css_number_value_get (const GtkCssValue *number,
242 double one_hundred_percent)
244 g_return_val_if_fail (number != NULL, 0.0);
245 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, 0.0);
247 if (number->unit == GTK_CSS_PERCENT)
248 return number->value * one_hundred_percent / 100;
250 return number->value;