]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsstypes.c
cssvalue: Add a value for border-radius corner properties
[~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 (GtkCssBorderImageRepeat, _gtk_css_border_image_repeat)
38
39 typedef struct _GtkCssChangeTranslation GtkCssChangeTranslation;
40 struct _GtkCssChangeTranslation {
41   GtkCssChange from;
42   GtkCssChange to;
43 };
44
45 static GtkCssChange
46 gtk_css_change_translate (GtkCssChange                   match,
47                          const GtkCssChangeTranslation *translations,
48                          guint                         n_translations)
49 {
50   GtkCssChange result = match;
51   guint i;
52
53   for (i = 0; i < n_translations; i++)
54     {
55       if (match & translations[i].from)
56         {
57           result &= ~translations[i].from;
58           result |= translations[i].to;
59         }
60     }
61
62   return result;
63 }
64
65 GtkCssChange
66 _gtk_css_change_for_sibling (GtkCssChange match)
67 {
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 },
73   };
74
75   return gtk_css_change_translate (match, table, G_N_ELEMENTS (table)); 
76 }
77
78 GtkCssChange
79 _gtk_css_change_for_child (GtkCssChange match)
80 {
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 }
90   };
91
92   return gtk_css_change_translate (match, table, G_N_ELEMENTS (table)); 
93 }
94
95 void
96 _gtk_css_number_init (GtkCssNumber *number,
97                       double        value,
98                       GtkCssUnit    unit)
99 {
100   number->value = value;
101   number->unit = unit;
102 }
103
104 gboolean
105 _gtk_css_number_equal (const GtkCssNumber *one,
106                        const GtkCssNumber *two)
107 {
108   return one->unit == two->unit &&
109          one->value == two->value;
110 }
111
112 double
113 _gtk_css_number_get (const GtkCssNumber *number,
114                      double              one_hundred_percent)
115 {
116   if (number->unit == GTK_CSS_PERCENT)
117     return number->value * one_hundred_percent * 0.01;
118   else
119     return number->value;
120 }
121
122 gboolean
123 _gtk_css_number_compute (GtkCssNumber       *dest,
124                          const GtkCssNumber *src,
125                          GtkStyleContext    *context)
126 {
127   switch (src->unit)
128     {
129     default:
130       g_assert_not_reached();
131       /* fall through */
132     case GTK_CSS_PERCENT:
133     case GTK_CSS_NUMBER:
134     case GTK_CSS_PX:
135     case GTK_CSS_DEG:
136       dest->value = src->value;
137       dest->unit = src->unit;
138       break;
139     case GTK_CSS_PT:
140       dest->value = src->value * 96.0 / 72.0;
141       dest->unit = GTK_CSS_PX;
142       break;
143     case GTK_CSS_PC:
144       dest->value = src->value * 96.0 / 72.0 * 12.0;
145       dest->unit = GTK_CSS_PX;
146       break;
147     case GTK_CSS_IN:
148       dest->value = src->value * 96.0;
149       dest->unit = GTK_CSS_PX;
150       break;
151     case GTK_CSS_CM:
152       dest->value = src->value * 96.0 * 0.39370078740157477;
153       dest->unit = GTK_CSS_PX;
154       break;
155     case GTK_CSS_MM:
156       dest->value = src->value * 96.0 * 0.039370078740157477;
157       dest->unit = GTK_CSS_PX;
158       break;
159     case GTK_CSS_EM:
160       dest->value = src->value * _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100);
161       dest->unit = GTK_CSS_PX;
162       break;
163     case GTK_CSS_EX:
164       /* for now we pretend ex is half of em */
165       dest->value = src->value * _gtk_css_number_value_get (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_FONT_SIZE), 100);
166       dest->unit = GTK_CSS_PX;
167       break;
168     case GTK_CSS_RAD:
169       dest->value = 360 * src->value / (2 * G_PI);
170       dest->unit = GTK_CSS_DEG;
171       break;
172     case GTK_CSS_GRAD:
173       dest->value = 360 * src->value / 400.0;
174       dest->unit = GTK_CSS_DEG;
175       break;
176     case GTK_CSS_TURN:
177       dest->value = 360 * src->value;
178       dest->unit = GTK_CSS_DEG;
179       break;
180     }
181
182   return !_gtk_css_number_equal (src, dest);
183 }
184
185 void
186 _gtk_css_number_print (const GtkCssNumber *number,
187                        GString            *string)
188 {
189   char buf[G_ASCII_DTOSTR_BUF_SIZE];
190
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",
206   };
207
208   g_return_if_fail (number != NULL);
209   g_return_if_fail (string != NULL);
210
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]);
215 }