1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
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 "gtkcsstypesprivate.h"
21 #include "gtkstylecontextprivate.h"
23 #define DEFINE_BOXED_TYPE_WITH_COPY_FUNC(TypeName, type_name) \
26 type_name ## _copy (const TypeName *foo) \
28 return g_memdup (foo, sizeof (TypeName)); \
31 G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name ## _copy, g_free)
33 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBackgroundSize, _gtk_css_background_size)
34 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderCornerRadius, _gtk_css_border_corner_radius)
35 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderImageRepeat, _gtk_css_border_image_repeat)
36 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssNumber, _gtk_css_number)
39 _gtk_css_number_init (GtkCssNumber *number,
43 number->value = value;
48 _gtk_css_number_equal (const GtkCssNumber *one,
49 const GtkCssNumber *two)
51 return one->unit == two->unit &&
52 one->value == two->value;
56 _gtk_css_number_get (const GtkCssNumber *number,
57 double one_hundred_percent)
59 if (number->unit == GTK_CSS_PERCENT)
60 return number->value * one_hundred_percent * 0.01;
66 _gtk_css_number_compute (GtkCssNumber *dest,
67 const GtkCssNumber *src,
68 GtkStyleContext *context)
73 g_assert_not_reached();
79 dest->value = src->value;
80 dest->unit = src->unit;
83 dest->value = src->value * 96.0 / 72.0;
84 dest->unit = GTK_CSS_PX;
87 dest->value = src->value * 96.0 / 72.0 * 12.0;
88 dest->unit = GTK_CSS_PX;
91 dest->value = src->value * 96.0;
92 dest->unit = GTK_CSS_PX;
95 dest->value = src->value * 96.0 * 0.39370078740157477;
96 dest->unit = GTK_CSS_PX;
99 dest->value = src->value * 96.0 * 0.039370078740157477;
100 dest->unit = GTK_CSS_PX;
103 dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
104 dest->unit = GTK_CSS_PX;
107 /* for now we pretend ex is half of em */
108 dest->value = src->value * g_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
109 dest->unit = GTK_CSS_PX;
112 dest->value = 360 * src->value / (2 * G_PI);
113 dest->unit = GTK_CSS_DEG;
116 dest->value = 360 * src->value / 400.0;
117 dest->unit = GTK_CSS_DEG;
120 dest->value = 360 * src->value;
121 dest->unit = GTK_CSS_DEG;
127 _gtk_css_number_print (const GtkCssNumber *number,
130 char buf[G_ASCII_DTOSTR_BUF_SIZE];
132 const char *names[] = {
133 /* [GTK_CSS_NUMBER] = */ "",
134 /* [GTK_CSS_PERCENT] = */ "%",
135 /* [GTK_CSS_PX] = */ "px",
136 /* [GTK_CSS_PT] = */ "pt",
137 /* [GTK_CSS_EM] = */ "em",
138 /* [GTK_CSS_EX] = */ "ex",
139 /* [GTK_CSS_PC] = */ "pc",
140 /* [GTK_CSS_IN] = */ "in",
141 /* [GTK_CSS_CM] = */ "cm",
142 /* [GTK_CSS_MM] = */ "mm",
143 /* [GTK_CSS_RAD] = */ "rad",
144 /* [GTK_CSS_DEG] = */ "deg",
145 /* [GTK_CSS_GRAD] = */ "grad",
146 /* [GTK_CSS_TURN] = */ "turn",
149 g_return_if_fail (number != NULL);
150 g_return_if_fail (string != NULL);
152 g_ascii_dtostr (buf, sizeof (buf), number->value);
153 g_string_append (string, buf);
154 if (number->value != 0.0)
155 g_string_append (string, names[number->unit]);