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 (GtkCssBackgroundPosition, _gtk_css_background_position)
35 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderCornerRadius, _gtk_css_border_corner_radius)
36 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderImageRepeat, _gtk_css_border_image_repeat)
37 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssNumber, _gtk_css_number)
39 typedef struct _GtkCssChangeTranslation GtkCssChangeTranslation;
40 struct _GtkCssChangeTranslation {
46 gtk_css_change_translate (GtkCssChange match,
47 const GtkCssChangeTranslation *translations,
50 GtkCssChange result = match;
53 for (i = 0; i < n_translations; i++)
55 if (match & translations[i].from)
57 result &= ~translations[i].from;
58 result |= translations[i].to;
66 _gtk_css_change_for_sibling (GtkCssChange match)
68 static const GtkCssChangeTranslation table[] = {
69 { GTK_CSS_CHANGE_CLASS, GTK_CSS_CHANGE_SIBLING_CLASS },
70 { GTK_CSS_CHANGE_NAME, GTK_CSS_CHANGE_SIBLING_NAME },
71 { GTK_CSS_CHANGE_POSITION, GTK_CSS_CHANGE_SIBLING_POSITION },
72 { GTK_CSS_CHANGE_STATE, GTK_CSS_CHANGE_SIBLING_STATE },
75 return gtk_css_change_translate (match, table, G_N_ELEMENTS (table));
79 _gtk_css_change_for_child (GtkCssChange match)
81 static const GtkCssChangeTranslation table[] = {
82 { GTK_CSS_CHANGE_CLASS, GTK_CSS_CHANGE_PARENT_CLASS },
83 { GTK_CSS_CHANGE_NAME, GTK_CSS_CHANGE_PARENT_NAME },
84 { GTK_CSS_CHANGE_POSITION, GTK_CSS_CHANGE_PARENT_POSITION },
85 { GTK_CSS_CHANGE_STATE, GTK_CSS_CHANGE_PARENT_STATE },
86 { GTK_CSS_CHANGE_SIBLING_CLASS, GTK_CSS_CHANGE_PARENT_SIBLING_CLASS },
87 { GTK_CSS_CHANGE_SIBLING_NAME, GTK_CSS_CHANGE_PARENT_SIBLING_NAME },
88 { GTK_CSS_CHANGE_SIBLING_POSITION, GTK_CSS_CHANGE_PARENT_SIBLING_POSITION },
89 { GTK_CSS_CHANGE_SIBLING_STATE, GTK_CSS_CHANGE_PARENT_SIBLING_STATE }
92 return gtk_css_change_translate (match, table, G_N_ELEMENTS (table));
96 _gtk_css_number_init (GtkCssNumber *number,
100 number->value = value;
105 _gtk_css_number_equal (const GtkCssNumber *one,
106 const GtkCssNumber *two)
108 return one->unit == two->unit &&
109 one->value == two->value;
113 _gtk_css_number_get (const GtkCssNumber *number,
114 double one_hundred_percent)
116 if (number->unit == GTK_CSS_PERCENT)
117 return number->value * one_hundred_percent * 0.01;
119 return number->value;
123 _gtk_css_number_compute (GtkCssNumber *dest,
124 const GtkCssNumber *src,
125 GtkStyleContext *context)
130 g_assert_not_reached();
132 case GTK_CSS_PERCENT:
136 dest->value = src->value;
137 dest->unit = src->unit;
140 dest->value = src->value * 96.0 / 72.0;
141 dest->unit = GTK_CSS_PX;
144 dest->value = src->value * 96.0 / 72.0 * 12.0;
145 dest->unit = GTK_CSS_PX;
148 dest->value = src->value * 96.0;
149 dest->unit = GTK_CSS_PX;
152 dest->value = src->value * 96.0 * 0.39370078740157477;
153 dest->unit = GTK_CSS_PX;
156 dest->value = src->value * 96.0 * 0.039370078740157477;
157 dest->unit = GTK_CSS_PX;
160 dest->value = src->value * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
161 dest->unit = GTK_CSS_PX;
164 /* for now we pretend ex is half of em */
165 dest->value = src->value * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
166 dest->unit = GTK_CSS_PX;
169 dest->value = 360 * src->value / (2 * G_PI);
170 dest->unit = GTK_CSS_DEG;
173 dest->value = 360 * src->value / 400.0;
174 dest->unit = GTK_CSS_DEG;
177 dest->value = 360 * src->value;
178 dest->unit = GTK_CSS_DEG;
182 return !_gtk_css_number_equal (src, dest);
186 _gtk_css_number_print (const GtkCssNumber *number,
189 char buf[G_ASCII_DTOSTR_BUF_SIZE];
191 const char *names[] = {
192 /* [GTK_CSS_NUMBER] = */ "",
193 /* [GTK_CSS_PERCENT] = */ "%",
194 /* [GTK_CSS_PX] = */ "px",
195 /* [GTK_CSS_PT] = */ "pt",
196 /* [GTK_CSS_EM] = */ "em",
197 /* [GTK_CSS_EX] = */ "ex",
198 /* [GTK_CSS_PC] = */ "pc",
199 /* [GTK_CSS_IN] = */ "in",
200 /* [GTK_CSS_CM] = */ "cm",
201 /* [GTK_CSS_MM] = */ "mm",
202 /* [GTK_CSS_RAD] = */ "rad",
203 /* [GTK_CSS_DEG] = */ "deg",
204 /* [GTK_CSS_GRAD] = */ "grad",
205 /* [GTK_CSS_TURN] = */ "turn",
208 g_return_if_fail (number != NULL);
209 g_return_if_fail (string != NULL);
211 g_ascii_dtostr (buf, sizeof (buf), number->value);
212 g_string_append (string, buf);
213 if (number->value != 0.0)
214 g_string_append (string, names[number->unit]);