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"
22 #include "gtkcssnumbervalueprivate.h"
23 #include "gtkstylecontextprivate.h"
25 #define DEFINE_BOXED_TYPE_WITH_COPY_FUNC(TypeName, type_name) \
28 type_name ## _copy (const TypeName *foo) \
30 return g_memdup (foo, sizeof (TypeName)); \
33 G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name ## _copy, g_free)
35 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBackgroundSize, _gtk_css_background_size)
36 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBackgroundPosition, _gtk_css_background_position)
37 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderCornerRadius, _gtk_css_border_corner_radius)
38 DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBorderImageRepeat, _gtk_css_border_image_repeat)
40 typedef struct _GtkCssChangeTranslation GtkCssChangeTranslation;
41 struct _GtkCssChangeTranslation {
47 gtk_css_change_translate (GtkCssChange match,
48 const GtkCssChangeTranslation *translations,
51 GtkCssChange result = match;
54 for (i = 0; i < n_translations; i++)
56 if (match & translations[i].from)
58 result &= ~translations[i].from;
59 result |= translations[i].to;
67 _gtk_css_change_for_sibling (GtkCssChange match)
69 static const GtkCssChangeTranslation table[] = {
70 { GTK_CSS_CHANGE_CLASS, GTK_CSS_CHANGE_SIBLING_CLASS },
71 { GTK_CSS_CHANGE_NAME, GTK_CSS_CHANGE_SIBLING_NAME },
72 { GTK_CSS_CHANGE_POSITION, GTK_CSS_CHANGE_SIBLING_POSITION },
73 { GTK_CSS_CHANGE_STATE, GTK_CSS_CHANGE_SIBLING_STATE },
76 return gtk_css_change_translate (match, table, G_N_ELEMENTS (table));
80 _gtk_css_change_for_child (GtkCssChange match)
82 static const GtkCssChangeTranslation table[] = {
83 { GTK_CSS_CHANGE_CLASS, GTK_CSS_CHANGE_PARENT_CLASS },
84 { GTK_CSS_CHANGE_NAME, GTK_CSS_CHANGE_PARENT_NAME },
85 { GTK_CSS_CHANGE_POSITION, GTK_CSS_CHANGE_PARENT_POSITION },
86 { GTK_CSS_CHANGE_STATE, GTK_CSS_CHANGE_PARENT_STATE },
87 { GTK_CSS_CHANGE_SIBLING_CLASS, GTK_CSS_CHANGE_PARENT_SIBLING_CLASS },
88 { GTK_CSS_CHANGE_SIBLING_NAME, GTK_CSS_CHANGE_PARENT_SIBLING_NAME },
89 { GTK_CSS_CHANGE_SIBLING_POSITION, GTK_CSS_CHANGE_PARENT_SIBLING_POSITION },
90 { GTK_CSS_CHANGE_SIBLING_STATE, GTK_CSS_CHANGE_PARENT_SIBLING_STATE }
93 return gtk_css_change_translate (match, table, G_N_ELEMENTS (table));
97 _gtk_css_number_init (GtkCssNumber *number,
101 number->value = value;
106 _gtk_css_number_equal (const GtkCssNumber *one,
107 const GtkCssNumber *two)
109 return one->unit == two->unit &&
110 one->value == two->value;
114 _gtk_css_number_get (const GtkCssNumber *number,
115 double one_hundred_percent)
117 if (number->unit == GTK_CSS_PERCENT)
118 return number->value * one_hundred_percent * 0.01;
120 return number->value;
124 _gtk_css_number_compute (GtkCssNumber *dest,
125 const GtkCssNumber *src,
126 GtkStyleContext *context)
131 g_assert_not_reached();
133 case GTK_CSS_PERCENT:
137 dest->value = src->value;
138 dest->unit = src->unit;
141 dest->value = src->value * 96.0 / 72.0;
142 dest->unit = GTK_CSS_PX;
145 dest->value = src->value * 96.0 / 72.0 * 12.0;
146 dest->unit = GTK_CSS_PX;
149 dest->value = src->value * 96.0;
150 dest->unit = GTK_CSS_PX;
153 dest->value = src->value * 96.0 * 0.39370078740157477;
154 dest->unit = GTK_CSS_PX;
157 dest->value = src->value * 96.0 * 0.039370078740157477;
158 dest->unit = GTK_CSS_PX;
161 dest->value = src->value * _gtk_css_number_value_get (_gtk_style_context_peek_property (context, "font-size"), 100);
162 dest->unit = GTK_CSS_PX;
165 /* for now we pretend ex is half of em */
166 dest->value = src->value * _gtk_css_number_value_get (_gtk_style_context_peek_property (context, "font-size"), 100);
167 dest->unit = GTK_CSS_PX;
170 dest->value = 360 * src->value / (2 * G_PI);
171 dest->unit = GTK_CSS_DEG;
174 dest->value = 360 * src->value / 400.0;
175 dest->unit = GTK_CSS_DEG;
178 dest->value = 360 * src->value;
179 dest->unit = GTK_CSS_DEG;
183 return !_gtk_css_number_equal (src, dest);
187 _gtk_css_number_print (const GtkCssNumber *number,
190 char buf[G_ASCII_DTOSTR_BUF_SIZE];
192 const char *names[] = {
193 /* [GTK_CSS_NUMBER] = */ "",
194 /* [GTK_CSS_PERCENT] = */ "%",
195 /* [GTK_CSS_PX] = */ "px",
196 /* [GTK_CSS_PT] = */ "pt",
197 /* [GTK_CSS_EM] = */ "em",
198 /* [GTK_CSS_EX] = */ "ex",
199 /* [GTK_CSS_PC] = */ "pc",
200 /* [GTK_CSS_IN] = */ "in",
201 /* [GTK_CSS_CM] = */ "cm",
202 /* [GTK_CSS_MM] = */ "mm",
203 /* [GTK_CSS_RAD] = */ "rad",
204 /* [GTK_CSS_DEG] = */ "deg",
205 /* [GTK_CSS_GRAD] = */ "grad",
206 /* [GTK_CSS_TURN] = */ "turn",
209 g_return_if_fail (number != NULL);
210 g_return_if_fail (string != NULL);
212 g_ascii_dtostr (buf, sizeof (buf), number->value);
213 g_string_append (string, buf);
214 if (number->value != 0.0)
215 g_string_append (string, names[number->unit]);