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_print (const GtkCssValue *number,
48 char buf[G_ASCII_DTOSTR_BUF_SIZE];
50 const char *names[] = {
51 /* [GTK_CSS_NUMBER] = */ "",
52 /* [GTK_CSS_PERCENT] = */ "%",
53 /* [GTK_CSS_PX] = */ "px",
54 /* [GTK_CSS_PT] = */ "pt",
55 /* [GTK_CSS_EM] = */ "em",
56 /* [GTK_CSS_EX] = */ "ex",
57 /* [GTK_CSS_PC] = */ "pc",
58 /* [GTK_CSS_IN] = */ "in",
59 /* [GTK_CSS_CM] = */ "cm",
60 /* [GTK_CSS_MM] = */ "mm",
61 /* [GTK_CSS_RAD] = */ "rad",
62 /* [GTK_CSS_DEG] = */ "deg",
63 /* [GTK_CSS_GRAD] = */ "grad",
64 /* [GTK_CSS_TURN] = */ "turn",
67 g_ascii_dtostr (buf, sizeof (buf), number->value);
68 g_string_append (string, buf);
69 if (number->value != 0.0)
70 g_string_append (string, names[number->unit]);
73 static const GtkCssValueClass GTK_CSS_VALUE_NUMBER = {
74 gtk_css_value_number_free,
75 gtk_css_value_number_equal,
76 gtk_css_value_number_print
80 _gtk_css_number_value_new (double value,
83 static GtkCssValue zero_singleton = { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_NUMBER, 0 };
84 static GtkCssValue px_singletons[] = {
85 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 0 },
86 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 1 },
87 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 2 },
88 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 3 },
89 { >K_CSS_VALUE_NUMBER, 1, GTK_CSS_PX, 4 },
93 if (unit == GTK_CSS_NUMBER && value == 0)
94 return _gtk_css_value_ref (&zero_singleton);
96 if (unit == GTK_CSS_PX &&
103 return _gtk_css_value_ref (&px_singletons[(int) value]);
106 result = _gtk_css_value_new (GtkCssValue, >K_CSS_VALUE_NUMBER);
108 result->value = value;
114 _gtk_css_number_value_parse (GtkCssParser *parser,
115 GtkCssNumberParseFlags flags)
119 g_return_val_if_fail (parser != NULL, NULL);
121 if (!_gtk_css_parser_read_number (parser, &number, flags))
124 return _gtk_css_number_value_new (number.value, number.unit);
128 _gtk_css_number_value_get (const GtkCssValue *number,
129 double one_hundred_percent)
131 g_return_val_if_fail (number != NULL, 0.0);
132 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, 0.0);
134 if (number->unit == GTK_CSS_PERCENT)
135 return number->value * one_hundred_percent / 100;
137 return number->value;
141 _gtk_css_number_value_compute (GtkCssValue *number,
142 GtkStyleContext *context)
144 g_return_val_if_fail (number->class == >K_CSS_VALUE_NUMBER, NULL);
146 switch (number->unit)
149 g_assert_not_reached();
151 case GTK_CSS_PERCENT:
155 return _gtk_css_value_ref (number);
157 return _gtk_css_number_value_new (number->value * 96.0 / 72.0,
160 return _gtk_css_number_value_new (number->value * 96.0 / 72.0 * 12.0,
164 return _gtk_css_number_value_new (number->value * 96.0,
168 return _gtk_css_number_value_new (number->value * 96.0 * 0.39370078740157477,
172 return _gtk_css_number_value_new (number->value * 96.0 * 0.039370078740157477,
176 return _gtk_css_number_value_new (number->value * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size")),
180 /* for now we pretend ex is half of em */
181 return _gtk_css_number_value_new (number->value * 0.5 * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size")),
184 return _gtk_css_number_value_new (number->value * 360.0 / (2 * G_PI),
187 return _gtk_css_number_value_new (number->value * 360.0 / 400.0,
190 return _gtk_css_number_value_new (number->value * 360.0,