]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsstypes.c
cssvalue: Do a hacky conversion of font-size to a number value
[~andy/gtk] / gtk / gtkcsstypes.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Benjamin Otte <otte@gnome.org>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcsstypesprivate.h"
21
22 #include "gtkcssnumbervalueprivate.h"
23 #include "gtkstylecontextprivate.h"
24
25 #define DEFINE_BOXED_TYPE_WITH_COPY_FUNC(TypeName, type_name) \
26 \
27 static TypeName * \
28 type_name ## _copy (const TypeName *foo) \
29 { \
30   return g_memdup (foo, sizeof (TypeName)); \
31 } \
32 \
33 G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name ## _copy, g_free)
34
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)
39
40 typedef struct _GtkCssChangeTranslation GtkCssChangeTranslation;
41 struct _GtkCssChangeTranslation {
42   GtkCssChange from;
43   GtkCssChange to;
44 };
45
46 static GtkCssChange
47 gtk_css_change_translate (GtkCssChange                   match,
48                          const GtkCssChangeTranslation *translations,
49                          guint                         n_translations)
50 {
51   GtkCssChange result = match;
52   guint i;
53
54   for (i = 0; i < n_translations; i++)
55     {
56       if (match & translations[i].from)
57         {
58           result &= ~translations[i].from;
59           result |= translations[i].to;
60         }
61     }
62
63   return result;
64 }
65
66 GtkCssChange
67 _gtk_css_change_for_sibling (GtkCssChange match)
68 {
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 },
74   };
75
76   return gtk_css_change_translate (match, table, G_N_ELEMENTS (table)); 
77 }
78
79 GtkCssChange
80 _gtk_css_change_for_child (GtkCssChange match)
81 {
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 }
91   };
92
93   return gtk_css_change_translate (match, table, G_N_ELEMENTS (table)); 
94 }
95
96 void
97 _gtk_css_number_init (GtkCssNumber *number,
98                       double        value,
99                       GtkCssUnit    unit)
100 {
101   number->value = value;
102   number->unit = unit;
103 }
104
105 gboolean
106 _gtk_css_number_equal (const GtkCssNumber *one,
107                        const GtkCssNumber *two)
108 {
109   return one->unit == two->unit &&
110          one->value == two->value;
111 }
112
113 double
114 _gtk_css_number_get (const GtkCssNumber *number,
115                      double              one_hundred_percent)
116 {
117   if (number->unit == GTK_CSS_PERCENT)
118     return number->value * one_hundred_percent * 0.01;
119   else
120     return number->value;
121 }
122
123 gboolean
124 _gtk_css_number_compute (GtkCssNumber       *dest,
125                          const GtkCssNumber *src,
126                          GtkStyleContext    *context)
127 {
128   switch (src->unit)
129     {
130     default:
131       g_assert_not_reached();
132       /* fall through */
133     case GTK_CSS_PERCENT:
134     case GTK_CSS_NUMBER:
135     case GTK_CSS_PX:
136     case GTK_CSS_DEG:
137       dest->value = src->value;
138       dest->unit = src->unit;
139       break;
140     case GTK_CSS_PT:
141       dest->value = src->value * 96.0 / 72.0;
142       dest->unit = GTK_CSS_PX;
143       break;
144     case GTK_CSS_PC:
145       dest->value = src->value * 96.0 / 72.0 * 12.0;
146       dest->unit = GTK_CSS_PX;
147       break;
148     case GTK_CSS_IN:
149       dest->value = src->value * 96.0;
150       dest->unit = GTK_CSS_PX;
151       break;
152     case GTK_CSS_CM:
153       dest->value = src->value * 96.0 * 0.39370078740157477;
154       dest->unit = GTK_CSS_PX;
155       break;
156     case GTK_CSS_MM:
157       dest->value = src->value * 96.0 * 0.039370078740157477;
158       dest->unit = GTK_CSS_PX;
159       break;
160     case GTK_CSS_EM:
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;
163       break;
164     case GTK_CSS_EX:
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;
168       break;
169     case GTK_CSS_RAD:
170       dest->value = 360 * src->value / (2 * G_PI);
171       dest->unit = GTK_CSS_DEG;
172       break;
173     case GTK_CSS_GRAD:
174       dest->value = 360 * src->value / 400.0;
175       dest->unit = GTK_CSS_DEG;
176       break;
177     case GTK_CSS_TURN:
178       dest->value = 360 * src->value;
179       dest->unit = GTK_CSS_DEG;
180       break;
181     }
182
183   return !_gtk_css_number_equal (src, dest);
184 }
185
186 void
187 _gtk_css_number_print (const GtkCssNumber *number,
188                        GString            *string)
189 {
190   char buf[G_ASCII_DTOSTR_BUF_SIZE];
191
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",
207   };
208
209   g_return_if_fail (number != NULL);
210   g_return_if_fail (string != NULL);
211
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]);
216 }