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)
38 typedef struct _GtkCssChangeTranslation GtkCssChangeTranslation;
39 struct _GtkCssChangeTranslation {
45 gtk_css_change_translate (GtkCssChange match,
46 const GtkCssChangeTranslation *translations,
49 GtkCssChange result = match;
52 for (i = 0; i < n_translations; i++)
54 if (match & translations[i].from)
56 result &= ~translations[i].from;
57 result |= translations[i].to;
65 _gtk_css_change_for_sibling (GtkCssChange match)
67 static const GtkCssChangeTranslation table[] = {
68 { GTK_CSS_CHANGE_CLASS, GTK_CSS_CHANGE_SIBLING_CLASS },
69 { GTK_CSS_CHANGE_NAME, GTK_CSS_CHANGE_SIBLING_NAME },
70 { GTK_CSS_CHANGE_POSITION, GTK_CSS_CHANGE_SIBLING_POSITION },
71 { GTK_CSS_CHANGE_STATE, GTK_CSS_CHANGE_SIBLING_STATE },
74 return gtk_css_change_translate (match, table, G_N_ELEMENTS (table));
78 _gtk_css_change_for_child (GtkCssChange match)
80 static const GtkCssChangeTranslation table[] = {
81 { GTK_CSS_CHANGE_CLASS, GTK_CSS_CHANGE_PARENT_CLASS },
82 { GTK_CSS_CHANGE_NAME, GTK_CSS_CHANGE_PARENT_NAME },
83 { GTK_CSS_CHANGE_POSITION, GTK_CSS_CHANGE_PARENT_POSITION },
84 { GTK_CSS_CHANGE_STATE, GTK_CSS_CHANGE_PARENT_STATE },
85 { GTK_CSS_CHANGE_SIBLING_CLASS, GTK_CSS_CHANGE_PARENT_SIBLING_CLASS },
86 { GTK_CSS_CHANGE_SIBLING_NAME, GTK_CSS_CHANGE_PARENT_SIBLING_NAME },
87 { GTK_CSS_CHANGE_SIBLING_POSITION, GTK_CSS_CHANGE_PARENT_SIBLING_POSITION },
88 { GTK_CSS_CHANGE_SIBLING_STATE, GTK_CSS_CHANGE_PARENT_SIBLING_STATE }
91 return gtk_css_change_translate (match, table, G_N_ELEMENTS (table));
95 _gtk_css_number_init (GtkCssNumber *number,
99 number->value = value;
104 _gtk_css_number_equal (const GtkCssNumber *one,
105 const GtkCssNumber *two)
107 return one->unit == two->unit &&
108 one->value == two->value;
112 _gtk_css_number_get (const GtkCssNumber *number,
113 double one_hundred_percent)
115 if (number->unit == GTK_CSS_PERCENT)
116 return number->value * one_hundred_percent * 0.01;
118 return number->value;
122 _gtk_css_number_compute (GtkCssNumber *dest,
123 const GtkCssNumber *src,
124 GtkStyleContext *context)
129 g_assert_not_reached();
131 case GTK_CSS_PERCENT:
135 dest->value = src->value;
136 dest->unit = src->unit;
139 dest->value = src->value * 96.0 / 72.0;
140 dest->unit = GTK_CSS_PX;
143 dest->value = src->value * 96.0 / 72.0 * 12.0;
144 dest->unit = GTK_CSS_PX;
147 dest->value = src->value * 96.0;
148 dest->unit = GTK_CSS_PX;
151 dest->value = src->value * 96.0 * 0.39370078740157477;
152 dest->unit = GTK_CSS_PX;
155 dest->value = src->value * 96.0 * 0.039370078740157477;
156 dest->unit = GTK_CSS_PX;
159 dest->value = src->value * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
160 dest->unit = GTK_CSS_PX;
163 /* for now we pretend ex is half of em */
164 dest->value = src->value * _gtk_css_value_get_double (_gtk_style_context_peek_property (context, "font-size"));
165 dest->unit = GTK_CSS_PX;
168 dest->value = 360 * src->value / (2 * G_PI);
169 dest->unit = GTK_CSS_DEG;
172 dest->value = 360 * src->value / 400.0;
173 dest->unit = GTK_CSS_DEG;
176 dest->value = 360 * src->value;
177 dest->unit = GTK_CSS_DEG;
181 return !_gtk_css_number_equal (src, dest);
185 _gtk_css_number_print (const GtkCssNumber *number,
188 char buf[G_ASCII_DTOSTR_BUF_SIZE];
190 const char *names[] = {
191 /* [GTK_CSS_NUMBER] = */ "",
192 /* [GTK_CSS_PERCENT] = */ "%",
193 /* [GTK_CSS_PX] = */ "px",
194 /* [GTK_CSS_PT] = */ "pt",
195 /* [GTK_CSS_EM] = */ "em",
196 /* [GTK_CSS_EX] = */ "ex",
197 /* [GTK_CSS_PC] = */ "pc",
198 /* [GTK_CSS_IN] = */ "in",
199 /* [GTK_CSS_CM] = */ "cm",
200 /* [GTK_CSS_MM] = */ "mm",
201 /* [GTK_CSS_RAD] = */ "rad",
202 /* [GTK_CSS_DEG] = */ "deg",
203 /* [GTK_CSS_GRAD] = */ "grad",
204 /* [GTK_CSS_TURN] = */ "turn",
207 g_return_if_fail (number != NULL);
208 g_return_if_fail (string != NULL);
210 g_ascii_dtostr (buf, sizeof (buf), number->value);
211 g_string_append (string, buf);
212 if (number->value != 0.0)
213 g_string_append (string, names[number->unit]);