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",
79 /* [GTK_CSS_S] = */ "s",
80 /* [GTK_CSS_MS] = */ "ms",
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]);
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
97 _gtk_css_number_value_new (double value,
100 static GtkCssValue zero_singleton = { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
101 static GtkCssValue px_singletons[] = {
102 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
103 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
104 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
105 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
106 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
110 if (unit == GTK_CSS_NUMBER && value == 0)
111 return _gtk_css_value_ref (&zero_singleton);
113 if (unit == GTK_CSS_PX &&
120 return _gtk_css_value_ref (&px_singletons[(int) value]);
123 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_NUMBER);
125 result->value = value;
131 _gtk_css_number_value_parse (GtkCssParser *parser,
132 GtkCssNumberParseFlags flags)
136 g_return_val_if_fail (parser != NULL, NULL);
138 if (!_gtk_css_parser_read_number (parser, &number, flags))
141 return _gtk_css_number_value_new (number.value, number.unit);
145 _gtk_css_number_value_get_unit (const GtkCssValue *value)
147 g_return_val_if_fail (value->class == >K_CSS_VALUE_NUMBER, GTK_CSS_NUMBER);
153 _gtk_css_number_value_get (const GtkCssValue *number,
154 double one_hundred_percent)
156 g_return_val_if_fail (number != NULL, 0.0);
157 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, 0.0);
159 if (number->unit == GTK_CSS_PERCENT)
160 return number->value * one_hundred_percent / 100;
162 return number->value;
166 _gtk_css_number_value_compute (GtkCssValue *number,
167 GtkStyleContext *context)
169 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, NULL);
171 switch (number->unit)
174 g_assert_not_reached();
176 case GTK_CSS_PERCENT:
181 return _gtk_css_value_ref (number);
183 return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
186 return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
190 return _gtk_css_number_value_new (number->value * 96.0,
194 return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
198 return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
202 return _gtk_css_number_value_new (number->value *
203 _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100),
207 /* for now we pretend ex is half of em */
208 return _gtk_css_number_value_new (number->value * 0.5 *
209 _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100),
212 return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
215 return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
218 return _gtk_css_number_value_new (number->value * 360.0,
221 return _gtk_css_number_value_new (number->value / 1000.0,