]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscomputedvalues.c
Require XInput2.h in X11 backend
[~andy/gtk] / gtk / gtkcsscomputedvalues.c
1 /*
2  * Copyright © 2012 Red Hat Inc.
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.1 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  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include "gtkcsscomputedvaluesprivate.h"
23
24 #include "gtkcssstylepropertyprivate.h"
25 #include "gtkcsstypesprivate.h"
26 #include "gtkprivatetypebuiltins.h"
27
28 G_DEFINE_TYPE (GtkCssComputedValues, _gtk_css_computed_values, G_TYPE_OBJECT)
29
30 static void
31 gtk_css_computed_values_dispose (GObject *object)
32 {
33   GtkCssComputedValues *values = GTK_CSS_COMPUTED_VALUES (object);
34
35   if (values->values)
36     {
37       g_array_free (values->values, TRUE);
38       values->values = NULL;
39     }
40   if (values->sections)
41     {
42       g_ptr_array_unref (values->sections);
43       values->sections = NULL;
44     }
45
46   G_OBJECT_CLASS (_gtk_css_computed_values_parent_class)->dispose (object);
47 }
48
49 static void
50 _gtk_css_computed_values_class_init (GtkCssComputedValuesClass *klass)
51 {
52   GObjectClass *object_class = G_OBJECT_CLASS (klass);
53
54   object_class->dispose = gtk_css_computed_values_dispose;
55 }
56
57 static void
58 _gtk_css_computed_values_init (GtkCssComputedValues *computed_values)
59 {
60   
61 }
62
63 GtkCssComputedValues *
64 _gtk_css_computed_values_new (void)
65 {
66   return g_object_new (GTK_TYPE_CSS_COMPUTED_VALUES, NULL);
67 }
68
69 static void
70 maybe_unref_section (gpointer section)
71 {
72   if (section)
73     gtk_css_section_unref (section);
74 }
75
76 void
77 _gtk_css_computed_values_compute_value (GtkCssComputedValues *values,
78                                         GtkStyleContext      *context,
79                                         guint                 id,
80                                         const GValue         *specified,
81                                         GtkCssSection        *section)
82 {
83   GtkCssStyleProperty *prop;
84   GtkStyleContext *parent;
85
86   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
87   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
88   g_return_if_fail (specified == NULL || G_IS_VALUE (specified));
89
90   prop = _gtk_css_style_property_lookup_by_id (id);
91   parent = gtk_style_context_get_parent (context);
92
93   if (values->values == NULL)
94     {
95       values->values = g_array_new (FALSE, TRUE, sizeof (GValue));
96       g_array_set_clear_func (values->values, (GDestroyNotify) g_value_unset);
97     }
98   if (id <= values->values->len)
99    g_array_set_size (values->values, id + 1);
100
101   /* http://www.w3.org/TR/css3-cascade/#cascade
102    * Then, for every element, the value for each property can be found
103    * by following this pseudo-algorithm:
104    * 1) Identify all declarations that apply to the element
105    */
106   if (specified != NULL)
107     {
108       if (G_VALUE_HOLDS (specified, GTK_TYPE_CSS_SPECIAL_VALUE))
109         {
110           switch (g_value_get_enum (specified))
111             {
112             case GTK_CSS_INHERIT:
113               /* 3) if the value of the winning declaration is ‘inherit’,
114                * the inherited value (see below) becomes the specified value.
115                */
116               specified = NULL;
117               break;
118             case GTK_CSS_INITIAL:
119               /* if the value of the winning declaration is ‘initial’,
120                * the initial value (see below) becomes the specified value.
121                */
122               specified = _gtk_css_style_property_get_initial_value (prop);
123               break;
124             default:
125               /* This is part of (2) above */
126               break;
127             }
128         }
129
130       /* 2) If the cascading process (described below) yields a winning
131        * declaration and the value of the winning declaration is not
132        * ‘initial’ or ‘inherit’, the value of the winning declaration
133        * becomes the specified value.
134        */
135     }
136   else
137     {
138       if (_gtk_css_style_property_is_inherit (prop))
139         {
140           /* 4) if the property is inherited, the inherited value becomes
141            * the specified value.
142            */
143           specified = NULL;
144         }
145       else
146         {
147           /* 5) Otherwise, the initial value becomes the specified value.
148            */
149           specified = _gtk_css_style_property_get_initial_value (prop);
150         }
151     }
152
153   if (specified == NULL && parent == NULL)
154     {
155       /* If the ‘inherit’ value is set on the root element, the property is
156        * assigned its initial value. */
157       specified = _gtk_css_style_property_get_initial_value (prop);
158     }
159
160   if (specified)
161     {
162       _gtk_css_style_property_compute_value (prop,
163                                              &g_array_index (values->values, GValue, id),
164                                              context,
165                                              specified);
166     }
167   else
168     {
169       const GValue *parent_value;
170       GValue *value = &g_array_index (values->values, GValue, id);
171       /* Set NULL here and do the inheritance upon lookup? */
172       parent_value = _gtk_style_context_peek_property (parent,
173                                                        _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop)));
174       g_value_init (value, G_VALUE_TYPE (parent_value));
175       g_value_copy (parent_value, value);
176     }
177
178   if (section)
179     {
180       if (values->sections == NULL)
181         values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
182       if (values->sections->len <= id)
183         g_ptr_array_set_size (values->sections, id + 1);
184
185       g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
186     }
187 }
188                                     
189 void
190 _gtk_css_computed_values_set_value (GtkCssComputedValues *values,
191                                     guint                 id,
192                                     const GValue         *value,
193                                     GtkCssSection        *section)
194 {
195   GValue *set;
196
197   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
198   g_return_if_fail (value == NULL || G_IS_VALUE (value));
199
200   if (values->values == NULL)
201     {
202       values->values = g_array_new (FALSE, TRUE, sizeof (GValue));
203       g_array_set_clear_func (values->values, (GDestroyNotify) g_value_unset);
204     }
205   if (id <= values->values->len)
206    g_array_set_size (values->values, id + 1);
207
208
209   set = &g_array_index (values->values, GValue, id);
210   g_value_init (set, G_VALUE_TYPE (value));
211   g_value_copy (value, set);
212
213   if (section)
214     {
215       if (values->sections == NULL)
216         values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
217       if (values->sections->len <= id)
218         g_ptr_array_set_size (values->sections, id + 1);
219
220       g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
221     }
222 }
223
224 const GValue *
225 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
226                                     guint                 id)
227 {
228   const GValue *v;
229
230   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
231
232   if (values->values == NULL ||
233       id >= values->values->len)
234     return NULL;
235
236   v = &g_array_index (values->values, GValue, id);
237   if (!G_IS_VALUE (v))
238     return NULL;
239
240   return v;
241 }
242
243 const GValue *
244 _gtk_css_computed_values_get_value_by_name (GtkCssComputedValues *values,
245                                             const char           *name)
246 {
247   GtkStyleProperty *prop;
248
249   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
250   g_return_val_if_fail (name != NULL, NULL);
251
252   prop = _gtk_style_property_lookup (name);
253   g_assert (GTK_IS_CSS_STYLE_PROPERTY (prop));
254   
255   return _gtk_css_computed_values_get_value (values, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
256 }
257
258 GtkCssSection *
259 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
260                                       guint                 id)
261 {
262   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
263
264   if (values->sections == NULL ||
265       id >= values->sections->len)
266     return NULL;
267
268   return g_ptr_array_index (values->sections, id);
269 }
270