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 "gtkstylepropertyprivate.h"
31 gtk_css_value_number_free (GtkCssValue *value)
33 g_slice_free (GtkCssValue, value);
37 gtk_css_value_number_equal (const GtkCssValue *number1,
38 const GtkCssValue *number2)
40 return number1->unit == number2->unit &&
41 number1->value == number2->value;
45 gtk_css_value_number_transition (GtkCssValue *start,
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)
54 return _gtk_css_number_value_new (start->value + (end->value - start->value) * progress,
59 gtk_css_value_number_print (const GtkCssValue *number,
62 char buf[G_ASCII_DTOSTR_BUF_SIZE];
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",
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]);
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
95 _gtk_css_number_value_new (double value,
98 static GtkCssValue zero_singleton = { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
99 static GtkCssValue px_singletons[] = {
100 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
101 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
102 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
103 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
104 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
108 if (unit == GTK_CSS_NUMBER && value == 0)
109 return _gtk_css_value_ref (&zero_singleton);
111 if (unit == GTK_CSS_PX &&
118 return _gtk_css_value_ref (&px_singletons[(int) value]);
121 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_NUMBER);
123 result->value = value;
129 _gtk_css_number_value_parse (GtkCssParser *parser,
130 GtkCssNumberParseFlags flags)
134 g_return_val_if_fail (parser != NULL, NULL);
136 if (!_gtk_css_parser_read_number (parser, &number, flags))
139 return _gtk_css_number_value_new (number.value, number.unit);
143 _gtk_css_number_value_get (const GtkCssValue *number,
144 double one_hundred_percent)
146 g_return_val_if_fail (number != NULL, 0.0);
147 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, 0.0);
149 if (number->unit == GTK_CSS_PERCENT)
150 return number->value * one_hundred_percent / 100;
152 return number->value;
156 _gtk_css_number_value_compute (GtkCssValue *number,
157 GtkStyleContext *context)
159 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, NULL);
161 switch (number->unit)
164 g_assert_not_reached();
166 case GTK_CSS_PERCENT:
170 return _gtk_css_value_ref (number);
172 return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
175 return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
179 return _gtk_css_number_value_new (number->value * 96.0,
183 return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
187 return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
191 return _gtk_css_number_value_new (number->value *
192 _gtk_css_number_value_get (_gtk_style_context_peek_property (context, "font-size"), 100),
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),
201 return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
204 return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
207 return _gtk_css_number_value_new (number->value * 360.0,