2 * Copyright © 2012 Red Hat Inc.
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.
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.
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/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
22 #include "gtkcsscomputedvaluesprivate.h"
24 #include "gtkcssinheritvalueprivate.h"
25 #include "gtkcssinitialvalueprivate.h"
26 #include "gtkcssstylepropertyprivate.h"
28 G_DEFINE_TYPE (GtkCssComputedValues, _gtk_css_computed_values, G_TYPE_OBJECT)
31 gtk_css_computed_values_dispose (GObject *object)
33 GtkCssComputedValues *values = GTK_CSS_COMPUTED_VALUES (object);
37 g_ptr_array_unref (values->values);
38 values->values = NULL;
42 g_ptr_array_unref (values->sections);
43 values->sections = NULL;
46 G_OBJECT_CLASS (_gtk_css_computed_values_parent_class)->dispose (object);
50 gtk_css_computed_values_finalize (GObject *object)
52 GtkCssComputedValues *values = GTK_CSS_COMPUTED_VALUES (object);
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);
59 G_OBJECT_CLASS (_gtk_css_computed_values_parent_class)->finalize (object);
63 _gtk_css_computed_values_class_init (GtkCssComputedValuesClass *klass)
65 GObjectClass *object_class = G_OBJECT_CLASS (klass);
67 object_class->dispose = gtk_css_computed_values_dispose;
68 object_class->finalize = gtk_css_computed_values_finalize;
72 _gtk_css_computed_values_init (GtkCssComputedValues *values)
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 ();
80 GtkCssComputedValues *
81 _gtk_css_computed_values_new (void)
83 return g_object_new (GTK_TYPE_CSS_COMPUTED_VALUES, NULL);
87 maybe_unref_section (gpointer section)
90 gtk_css_section_unref (section);
94 _gtk_css_computed_values_compute_value (GtkCssComputedValues *values,
95 GtkStyleContext *context,
97 GtkCssValue *specified,
98 GtkCssSection *section)
100 GtkCssDependencies dependencies;
103 g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
104 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
106 /* http://www.w3.org/TR/css3-cascade/#cascade
107 * Then, for every element, the value for each property can be found
108 * by following this pseudo-algorithm:
109 * 1) Identify all declarations that apply to the element
111 if (specified == NULL)
113 GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (id);
115 if (_gtk_css_style_property_is_inherit (prop))
116 specified = _gtk_css_inherit_value_new ();
118 specified = _gtk_css_initial_value_new ();
121 _gtk_css_value_ref (specified);
123 value = _gtk_css_value_compute (specified, id, context, &dependencies);
125 _gtk_css_computed_values_set_value (values, id, value, dependencies, section);
127 _gtk_css_value_unref (value);
128 _gtk_css_value_unref (specified);
132 _gtk_css_computed_values_set_value (GtkCssComputedValues *values,
135 GtkCssDependencies dependencies,
136 GtkCssSection *section)
138 g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
140 if (values->values == NULL)
141 values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
142 if (id >= values->values->len)
143 g_ptr_array_set_size (values->values, id + 1);
145 if (g_ptr_array_index (values->values, id))
146 _gtk_css_value_unref (g_ptr_array_index (values->values, id));
147 g_ptr_array_index (values->values, id) = _gtk_css_value_ref (value);
149 if (dependencies & (GTK_CSS_DEPENDS_ON_PARENT | GTK_CSS_EQUALS_PARENT))
150 values->depends_on_parent = _gtk_bitmask_set (values->depends_on_parent, id, TRUE);
151 if (dependencies & (GTK_CSS_EQUALS_PARENT))
152 values->equals_parent = _gtk_bitmask_set (values->equals_parent, id, TRUE);
153 if (dependencies & (GTK_CSS_DEPENDS_ON_COLOR))
154 values->depends_on_color = _gtk_bitmask_set (values->depends_on_color, id, TRUE);
155 if (dependencies & (GTK_CSS_DEPENDS_ON_FONT_SIZE))
156 values->depends_on_font_size = _gtk_bitmask_set (values->depends_on_font_size, id, TRUE);
160 if (values->sections == NULL)
161 values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
162 if (values->sections->len <= id)
163 g_ptr_array_set_size (values->sections, id + 1);
165 g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
170 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
173 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
175 if (values->values == NULL ||
176 id >= values->values->len)
179 return g_ptr_array_index (values->values, id);
183 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
186 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
188 if (values->sections == NULL ||
189 id >= values->sections->len)
192 return g_ptr_array_index (values->sections, id);
196 _gtk_css_computed_values_get_difference (GtkCssComputedValues *values,
197 GtkCssComputedValues *other)
202 len = MIN (values->values->len, other->values->len);
203 result = _gtk_bitmask_new ();
204 if (values->values->len != other->values->len)
205 result = _gtk_bitmask_invert_range (result, len, MAX (values->values->len, other->values->len));
207 for (i = 0; i < len; i++)
209 if (!_gtk_css_value_equal (g_ptr_array_index (values->values, i),
210 g_ptr_array_index (other->values, i)))
211 result = _gtk_bitmask_set (result, i, TRUE);