]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscomputedvalues.c
44a7f97e258e82986bbc2c43f6fa1481cc0e4d4e
[~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 "gtkcssinheritvalueprivate.h"
25 #include "gtkcssinitialvalueprivate.h"
26 #include "gtkcssstylepropertyprivate.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_ptr_array_unref (values->values);
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                                         GtkCssValue          *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
89   prop = _gtk_css_style_property_lookup_by_id (id);
90   parent = gtk_style_context_get_parent (context);
91
92   if (values->values == NULL)
93     values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
94   if (id <= values->values->len)
95    g_ptr_array_set_size (values->values, id + 1);
96
97   /* http://www.w3.org/TR/css3-cascade/#cascade
98    * Then, for every element, the value for each property can be found
99    * by following this pseudo-algorithm:
100    * 1) Identify all declarations that apply to the element
101    */
102   if (specified != NULL)
103     {
104       if (_gtk_css_value_is_inherit (specified))
105         {
106           /* 3) if the value of the winning declaration is ‘inherit’,
107            * the inherited value (see below) becomes the specified value.
108            */
109           specified = NULL;
110         }
111       else if (_gtk_css_value_is_initial (specified))
112         {
113           /* if the value of the winning declaration is ‘initial’,
114            * the initial value (see below) becomes the specified value.
115            */
116           specified = _gtk_css_style_property_get_initial_value (prop);
117         }
118
119       /* 2) If the cascading process (described below) yields a winning
120        * declaration and the value of the winning declaration is not
121        * ‘initial’ or ‘inherit’, the value of the winning declaration
122        * becomes the specified value.
123        */
124     }
125   else
126     {
127       if (_gtk_css_style_property_is_inherit (prop))
128         {
129           /* 4) if the property is inherited, the inherited value becomes
130            * the specified value.
131            */
132           specified = NULL;
133         }
134       else
135         {
136           /* 5) Otherwise, the initial value becomes the specified value.
137            */
138           specified = _gtk_css_style_property_get_initial_value (prop);
139         }
140     }
141
142   if (specified == NULL && parent == NULL)
143     {
144       /* If the ‘inherit’ value is set on the root element, the property is
145        * assigned its initial value. */
146       specified = _gtk_css_style_property_get_initial_value (prop);
147     }
148
149   if (specified)
150     {
151       g_ptr_array_index (values->values, id) =
152         _gtk_css_style_property_compute_value (prop,
153                                                context,
154                                                specified);
155     }
156   else
157     {
158       GtkCssValue *parent_value;
159       /* Set NULL here and do the inheritance upon lookup? */
160       parent_value = _gtk_style_context_peek_property (parent, id);
161
162       g_ptr_array_index (values->values, id) = _gtk_css_value_ref (parent_value);
163     }
164
165   if (section)
166     {
167       if (values->sections == NULL)
168         values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
169       if (values->sections->len <= id)
170         g_ptr_array_set_size (values->sections, id + 1);
171
172       g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
173     }
174 }
175                                     
176 void
177 _gtk_css_computed_values_set_value (GtkCssComputedValues *values,
178                                     guint                 id,
179                                     GtkCssValue          *value,
180                                     GtkCssSection        *section)
181 {
182   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
183
184   if (values->values == NULL)
185     values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
186   if (id <= values->values->len)
187    g_ptr_array_set_size (values->values, id + 1);
188
189   if (g_ptr_array_index (values->values, id))
190     _gtk_css_value_unref (g_ptr_array_index (values->values, id));
191   g_ptr_array_index (values->values, id) = _gtk_css_value_ref (value);
192
193   if (section)
194     {
195       if (values->sections == NULL)
196         values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
197       if (values->sections->len <= id)
198         g_ptr_array_set_size (values->sections, id + 1);
199
200       g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
201     }
202 }
203
204 GtkCssValue *
205 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
206                                     guint                 id)
207 {
208   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
209
210   if (values->values == NULL ||
211       id >= values->values->len)
212     return NULL;
213
214   return g_ptr_array_index (values->values, id);
215 }
216
217 GtkCssValue *
218 _gtk_css_computed_values_get_value_by_name (GtkCssComputedValues *values,
219                                             const char           *name)
220 {
221   GtkStyleProperty *prop;
222
223   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
224   g_return_val_if_fail (name != NULL, NULL);
225
226   prop = _gtk_style_property_lookup (name);
227   g_assert (GTK_IS_CSS_STYLE_PROPERTY (prop));
228   
229   return _gtk_css_computed_values_get_value (values, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
230 }
231
232 GtkCssSection *
233 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
234                                       guint                 id)
235 {
236   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
237
238   if (values->sections == NULL ||
239       id >= values->sections->len)
240     return NULL;
241
242   return g_ptr_array_index (values->sections, id);
243 }
244