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 "gtkcssstylepropertyprivate.h"
25 #include "gtkcsstypesprivate.h"
26 #include "gtkprivatetypebuiltins.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_class_init (GtkCssComputedValuesClass *klass)
52 GObjectClass *object_class = G_OBJECT_CLASS (klass);
54 object_class->dispose = gtk_css_computed_values_dispose;
58 _gtk_css_computed_values_init (GtkCssComputedValues *computed_values)
63 GtkCssComputedValues *
64 _gtk_css_computed_values_new (void)
66 return g_object_new (GTK_TYPE_CSS_COMPUTED_VALUES, NULL);
70 maybe_unref_section (gpointer section)
73 gtk_css_section_unref (section);
77 _gtk_css_computed_values_compute_value (GtkCssComputedValues *values,
78 GtkStyleContext *context,
80 GtkCssValue *specified,
81 GtkCssSection *section)
83 GtkCssStyleProperty *prop;
84 GtkStyleContext *parent;
86 g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
87 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
89 prop = _gtk_css_style_property_lookup_by_id (id);
90 parent = gtk_style_context_get_parent (context);
92 if (values->values == NULL)
93 values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
94 if (id <= values->values->len)
95 g_ptr_array_set_size (values->values, id + 1);
97 /* http://www.w3.org/TR/css3-cascade/#cascade
98 * Then, for every element, the value for each property can be found
99 * by following this pseudo-algorithm:
100 * 1) Identify all declarations that apply to the element
102 if (specified != NULL)
104 if (_gtk_css_value_is_special (specified))
106 switch (_gtk_css_value_get_special_kind (specified))
108 case GTK_CSS_INHERIT:
109 /* 3) if the value of the winning declaration is ‘inherit’,
110 * the inherited value (see below) becomes the specified value.
114 case GTK_CSS_INITIAL:
115 /* if the value of the winning declaration is ‘initial’,
116 * the initial value (see below) becomes the specified value.
118 specified = _gtk_css_style_property_get_initial_value (prop);
121 /* This is part of (2) above */
126 /* 2) If the cascading process (described below) yields a winning
127 * declaration and the value of the winning declaration is not
128 * ‘initial’ or ‘inherit’, the value of the winning declaration
129 * becomes the specified value.
134 if (_gtk_css_style_property_is_inherit (prop))
136 /* 4) if the property is inherited, the inherited value becomes
137 * the specified value.
143 /* 5) Otherwise, the initial value becomes the specified value.
145 specified = _gtk_css_style_property_get_initial_value (prop);
149 if (specified == NULL && parent == NULL)
151 /* If the ‘inherit’ value is set on the root element, the property is
152 * assigned its initial value. */
153 specified = _gtk_css_style_property_get_initial_value (prop);
158 g_ptr_array_index (values->values, id) =
159 _gtk_css_style_property_compute_value (prop,
165 GtkCssValue *parent_value;
166 /* Set NULL here and do the inheritance upon lookup? */
167 parent_value = _gtk_style_context_peek_property (parent,
168 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop)));
170 g_ptr_array_index (values->values, id) = _gtk_css_value_ref (parent_value);
175 if (values->sections == NULL)
176 values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
177 if (values->sections->len <= id)
178 g_ptr_array_set_size (values->sections, id + 1);
180 g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
185 _gtk_css_computed_values_set_value (GtkCssComputedValues *values,
188 GtkCssSection *section)
190 g_return_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values));
192 if (values->values == NULL)
193 values->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
194 if (id <= values->values->len)
195 g_ptr_array_set_size (values->values, id + 1);
197 g_ptr_array_index (values->values, id) = _gtk_css_value_ref (value);
201 if (values->sections == NULL)
202 values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
203 if (values->sections->len <= id)
204 g_ptr_array_set_size (values->sections, id + 1);
206 g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
211 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
214 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
216 if (values->values == NULL ||
217 id >= values->values->len)
220 return g_ptr_array_index (values->values, id);
224 _gtk_css_computed_values_get_value_by_name (GtkCssComputedValues *values,
227 GtkStyleProperty *prop;
229 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
230 g_return_val_if_fail (name != NULL, NULL);
232 prop = _gtk_style_property_lookup (name);
233 g_assert (GTK_IS_CSS_STYLE_PROPERTY (prop));
235 return _gtk_css_computed_values_get_value (values, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
239 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
242 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
244 if (values->sections == NULL ||
245 id >= values->sections->len)
248 return g_ptr_array_index (values->sections, id);