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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
23 #include "gtkcsscomputedvaluesprivate.h"
25 #include "gtkcssstylepropertyprivate.h"
26 #include "gtkcsstypesprivate.h"
27 #include "gtkprivatetypebuiltins.h"
29 G_DEFINE_TYPE (GtkCssComputedValues, _gtk_css_computed_values, G_TYPE_OBJECT)
32 gtk_css_computed_values_dispose (GObject *object)
34 GtkCssComputedValues *values = GTK_CSS_COMPUTED_VALUES (object);
38 g_array_free (values->values, TRUE);
39 values->values = NULL;
43 g_ptr_array_unref (values->sections);
44 values->sections = NULL;
47 G_OBJECT_CLASS (_gtk_css_computed_values_parent_class)->dispose (object);
51 _gtk_css_computed_values_class_init (GtkCssComputedValuesClass *klass)
53 GObjectClass *object_class = G_OBJECT_CLASS (klass);
55 object_class->dispose = gtk_css_computed_values_dispose;
59 _gtk_css_computed_values_init (GtkCssComputedValues *computed_values)
64 GtkCssComputedValues *
65 _gtk_css_computed_values_new (void)
67 return g_object_new (GTK_TYPE_CSS_COMPUTED_VALUES, NULL);
71 maybe_unref_section (gpointer section)
74 gtk_css_section_unref (section);
78 _gtk_css_computed_values_compute_value (GtkCssComputedValues *values,
79 GtkStyleContext *context,
81 const GValue *specified,
82 GtkCssSection *section)
84 GtkCssStyleProperty *prop;
85 GtkStyleContext *parent;
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));
91 prop = _gtk_css_style_property_lookup_by_id (id);
92 parent = gtk_style_context_get_parent (context);
94 if (values->values == NULL)
96 values->values = g_array_new (FALSE, TRUE, sizeof (GValue));
97 g_array_set_clear_func (values->values, (GDestroyNotify) g_value_unset);
99 if (id <= values->values->len)
100 g_array_set_size (values->values, id + 1);
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
107 if (specified != NULL)
109 if (G_VALUE_HOLDS (specified, GTK_TYPE_CSS_SPECIAL_VALUE))
111 switch (g_value_get_enum (specified))
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.
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.
123 specified = _gtk_css_style_property_get_initial_value (prop);
126 /* This is part of (2) above */
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.
139 if (_gtk_css_style_property_is_inherit (prop))
141 /* 4) if the property is inherited, the inherited value becomes
142 * the specified value.
148 /* 5) Otherwise, the initial value becomes the specified value.
150 specified = _gtk_css_style_property_get_initial_value (prop);
154 if (specified == NULL && parent == NULL)
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);
163 _gtk_css_style_property_compute_value (prop,
164 &g_array_index (values->values, GValue, id),
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);
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);
186 g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
191 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
196 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
198 if (values->values == NULL ||
199 id >= values->values->len)
202 v = &g_array_index (values->values, GValue, id);
210 _gtk_css_computed_values_get_value_by_name (GtkCssComputedValues *values,
213 GtkStyleProperty *prop;
215 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
216 g_return_val_if_fail (name != NULL, NULL);
218 prop = _gtk_style_property_lookup (name);
219 g_assert (GTK_IS_CSS_STYLE_PROPERTY (prop));
221 return _gtk_css_computed_values_get_value (values, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
225 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
228 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
230 if (values->sections == NULL ||
231 id >= values->sections->len)
234 return g_ptr_array_index (values->sections, id);