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_value_array_free (values->values);
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)
95 values->values = g_value_array_new (id + 1);
96 while (values->values->n_values <= id)
97 g_value_array_append (values->values, NULL);
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
104 if (specified != NULL)
106 if (G_VALUE_HOLDS (specified, GTK_TYPE_CSS_SPECIAL_VALUE))
108 switch (g_value_get_enum (specified))
110 case GTK_CSS_INHERIT:
111 /* 3) if the value of the winning declaration is ‘inherit’,
112 * the inherited value (see below) becomes the specified value.
116 case GTK_CSS_INITIAL:
117 /* if the value of the winning declaration is ‘initial’,
118 * the initial value (see below) becomes the specified value.
120 specified = _gtk_css_style_property_get_initial_value (prop);
123 /* This is part of (2) above */
128 /* 2) If the cascading process (described below) yields a winning
129 * declaration and the value of the winning declaration is not
130 * ‘initial’ or ‘inherit’, the value of the winning declaration
131 * becomes the specified value.
136 if (_gtk_css_style_property_is_inherit (prop))
138 /* 4) if the property is inherited, the inherited value becomes
139 * the specified value.
145 /* 5) Otherwise, the initial value becomes the specified value.
147 specified = _gtk_css_style_property_get_initial_value (prop);
151 if (specified == NULL && parent == NULL)
153 /* If the ‘inherit’ value is set on the root element, the property is
154 * assigned its initial value. */
155 specified = _gtk_css_style_property_get_initial_value (prop);
160 _gtk_css_style_property_compute_value (prop,
161 g_value_array_get_nth (values->values, id),
167 const GValue *parent_value;
168 GValue *value = g_value_array_get_nth (values->values, id);
169 /* Set NULL here and do the inheritance upon lookup? */
170 parent_value = _gtk_style_context_peek_property (parent,
171 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop)));
172 g_value_init (value, G_VALUE_TYPE (parent_value));
173 g_value_copy (parent_value, value);
178 if (values->sections == NULL)
179 values->sections = g_ptr_array_new_with_free_func (maybe_unref_section);
180 if (values->sections->len <= id)
181 g_ptr_array_set_size (values->sections, id + 1);
183 g_ptr_array_index (values->sections, id) = gtk_css_section_ref (section);
188 _gtk_css_computed_values_get_value (GtkCssComputedValues *values,
193 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
195 if (values->values == NULL ||
196 id >= values->values->n_values)
199 v = g_value_array_get_nth (values->values, id);
207 _gtk_css_computed_values_get_value_by_name (GtkCssComputedValues *values,
210 GtkStyleProperty *prop;
212 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
213 g_return_val_if_fail (name != NULL, NULL);
215 prop = _gtk_style_property_lookup (name);
216 g_assert (GTK_IS_CSS_STYLE_PROPERTY (prop));
218 return _gtk_css_computed_values_get_value (values, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
222 _gtk_css_computed_values_get_section (GtkCssComputedValues *values,
225 g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
227 if (values->sections == NULL ||
228 id >= values->sections->len)
231 return g_ptr_array_index (values->sections, id);