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