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