]> Pileus Git - ~andy/gtk/blob - gtk/gtkcsscomputedvalues.c
cssomputedvalues: Track dependencies
[~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_finalize (GObject *object)
51 {
52   GtkCssComputedValues *values = GTK_CSS_COMPUTED_VALUES (object);
53
54   _gtk_bitmask_free (values->depends_on_parent);
55   _gtk_bitmask_free (values->equals_parent);
56   _gtk_bitmask_free (values->depends_on_color);
57   _gtk_bitmask_free (values->depends_on_font_size);
58
59   G_OBJECT_CLASS (_gtk_css_computed_values_parent_class)->finalize (object);
60 }
61
62 static void
63 _gtk_css_computed_values_class_init (GtkCssComputedValuesClass *klass)
64 {
65   GObjectClass *object_class = G_OBJECT_CLASS (klass);
66
67   object_class->dispose = gtk_css_computed_values_dispose;
68   object_class->finalize = gtk_css_computed_values_finalize;
69 }
70
71 static void
72 _gtk_css_computed_values_init (GtkCssComputedValues *values)
73 {
74   values->depends_on_parent = _gtk_bitmask_new ();
75   values->equals_parent = _gtk_bitmask_new ();
76   values->depends_on_color = _gtk_bitmask_new ();
77   values->depends_on_font_size = _gtk_bitmask_new ();
78 }
79
80 GtkCssComputedValues *
81 _gtk_css_computed_values_new (void)
82 {
83   return g_object_new (GTK_TYPE_CSS_COMPUTED_VALUES, NULL);
84 }
85
86 static void
87 maybe_unref_section (gpointer section)
88 {
89   if (section)
90     gtk_css_section_unref (section);
91 }
92
93 static void
94 gtk_css_computed_values_ensure_array (GtkCssComputedValues *values,
95                                       guint                 at_least_size)
96 {
97   if (values->values == NULL)
98     values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
99   if (at_least_size > values->values->len)
100    g_ptr_array_set_size (values->values, at_least_size);
101 }
102
103 void
104 _gtk_css_computed_values_compute_value (GtkCssComputedValues *values,
105                                         GtkStyleContext      *context,
106                                         guint                 id,
107                                         GtkCssValue          *specified,
108                                         GtkCssSection        *section)
109 {
110   GtkCssDependencies dependencies;
111
112   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
113   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
114
115   gtk_css_computed_values_ensure_array (values, id + 1);
116
117   /* http://www.w3.org/TR/css3-cascade/#cascade
118    * Then, for every element, the value for each property can be found
119    * by following this pseudo-algorithm:
120    * 1) Identify all declarations that apply to the element
121    */
122   if (specified == NULL)
123     {
124       GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (id);
125
126       if (_gtk_css_style_property_is_inherit (prop))
127         specified = _gtk_css_inherit_value_new ();
128       else
129         specified = _gtk_css_initial_value_new ();
130     }
131   else
132     _gtk_css_value_ref (specified);
133
134   g_ptr_array_index (values->values, id) = _gtk_css_value_compute (specified, id, context, &dependencies);
135
136   if (dependencies & (GTK_CSS_DEPENDS_ON_PARENT | GTK_CSS_EQUALS_PARENT))
137     values->depends_on_parent = _gtk_bitmask_set (values->depends_on_parent, id, TRUE);
138   if (dependencies & (GTK_CSS_EQUALS_PARENT))
139     values->equals_parent = _gtk_bitmask_set (values->equals_parent, id, TRUE);
140   if (dependencies & (GTK_CSS_DEPENDS_ON_COLOR))
141     values->depends_on_color = _gtk_bitmask_set (values->depends_on_color, id, TRUE);
142   if (dependencies & (GTK_CSS_DEPENDS_ON_FONT_SIZE))
143     values->depends_on_font_size = _gtk_bitmask_set (values->depends_on_font_size, id, TRUE);
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   _gtk_css_value_unref (specified);
156 }
157                                     
158 void
159 _gtk_css_computed_values_set_value (GtkCssComputedValues *values,
160                                     guint                 id,
161                                     GtkCssValue          *value,
162                                     GtkCssSection        *section)
163 {
164   g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
165
166   gtk_css_computed_values_ensure_array (values, id + 1);
167
168   if (g_ptr_array_index (values->values, id))
169     _gtk_css_value_unref (g_ptr_array_index (values->values, id));
170   g_ptr_array_index (values->values, id) = _gtk_css_value_ref (value);
171
172   if (section)
173     {
174       if (values->sections == NULL)
175         values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
176       if (values->sections->len <= id)
177         g_ptr_array_set_size (values->sections, id + 1);
178
179       g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
180     }
181 }
182
183 GtkCssValue *
184 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
185                                     guint                 id)
186 {
187   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
188
189   if (values->values == NULL ||
190       id >= values->values->len)
191     return NULL;
192
193   return g_ptr_array_index (values->values, id);
194 }
195
196 GtkCssSection *
197 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
198                                       guint                 id)
199 {
200   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
201
202   if (values->sections == NULL ||
203       id >= values->sections->len)
204     return NULL;
205
206   return g_ptr_array_index (values->sections, id);
207 }
208
209 GtkBitmask *
210 _gtk_css_computed_values_get_difference (GtkCssComputedValues *values,
211                                          GtkCssComputedValues *other)
212 {
213   GtkBitmask *result;
214   guint i, len;
215
216   len = MIN (values->values->len, other->values->len);
217   result = _gtk_bitmask_new ();
218   if (values->values->len != other->values->len)
219     result = _gtk_bitmask_invert_range (result, len, MAX (values->values->len, other->values->len));
220   
221   for (i = 0; i < len; i++)
222     {
223       if (!_gtk_css_value_equal (g_ptr_array_index (values->values, i),
224                                  g_ptr_array_index (other->values, i)))
225         result = _gtk_bitmask_set (result, i, TRUE);
226     }
227
228   return result;
229 }
230