]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscomputedvalues.c
css: Move computing of initial and inherit values
[~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 static void
77 gtk_css_computed_values_ensure_array (GtkCssComputedValues *values,
78                                       guint                 at_least_size)
79 {
80   if (values->values == NULL)
81     values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
82   if (at_least_size > values->values->len)
83    g_ptr_array_set_size (values->values, at_least_size);
84 }
85
86 void
87 _gtk_css_computed_values_compute_value (GtkCssComputedValues *values,
88                                         GtkStyleContext      *context,
89                                         guint                 id,
90                                         GtkCssValue          *specified,
91                                         GtkCssSection        *section)
92 {
93
94   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
95   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
96
97   gtk_css_computed_values_ensure_array (values, id + 1);
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       GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (id);
107
108       if (_gtk_css_style_property_is_inherit (prop))
109         specified = _gtk_css_inherit_value_new ();
110       else
111         specified = _gtk_css_initial_value_new ();
112     }
113   else
114     _gtk_css_value_ref (specified);
115
116   g_ptr_array_index (values->values, id) = _gtk_css_value_compute (specified, id, context);
117
118   if (section)
119     {
120       if (values->sections == NULL)
121         values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
122       if (values->sections->len <= id)
123         g_ptr_array_set_size (values->sections, id + 1);
124
125       g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
126     }
127   
128   _gtk_css_value_unref (specified);
129 }
130                                     
131 void
132 _gtk_css_computed_values_set_value (GtkCssComputedValues *values,
133                                     guint                 id,
134                                     GtkCssValue          *value,
135                                     GtkCssSection        *section)
136 {
137   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
138
139   gtk_css_computed_values_ensure_array (values, id + 1);
140
141   if (g_ptr_array_index (values->values, id))
142     _gtk_css_value_unref (g_ptr_array_index (values->values, id));
143   g_ptr_array_index (values->values, id) = _gtk_css_value_ref (value);
144
145   if (section)
146     {
147       if (values->sections == NULL)
148         values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
149       if (values->sections->len <= id)
150         g_ptr_array_set_size (values->sections, id + 1);
151
152       g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
153     }
154 }
155
156 GtkCssValue *
157 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
158                                     guint                 id)
159 {
160   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
161
162   if (values->values == NULL ||
163       id >= values->values->len)
164     return NULL;
165
166   return g_ptr_array_index (values->values, id);
167 }
168
169 GtkCssSection *
170 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
171                                       guint                 id)
172 {
173   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
174
175   if (values->sections == NULL ||
176       id >= values->sections->len)
177     return NULL;
178
179   return g_ptr_array_index (values->sections, id);
180 }
181
182 GtkBitmask *
183 _gtk_css_computed_values_get_difference (GtkCssComputedValues *values,
184                                          GtkCssComputedValues *other)
185 {
186   GtkBitmask *result;
187   guint i, len;
188
189   len = MIN (values->values->len, other->values->len);
190   result = _gtk_bitmask_new ();
191   if (values->values->len != other->values->len)
192     result = _gtk_bitmask_invert_range (result, len, MAX (values->values->len, other->values->len));
193   
194   for (i = 0; i < len; i++)
195     {
196       if (!_gtk_css_value_equal (g_ptr_array_index (values->values, i),
197                                  g_ptr_array_index (other->values, i)))
198         result = _gtk_bitmask_set (result, i, TRUE);
199     }
200
201   return result;
202 }
203