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