]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssvalue.c
css: Huge refactoring to avoid computing wrong values
[~andy/gtk] / gtk / gtkcssvalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 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 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
18 #include "config.h"
19
20 #include "gtkcssvalueprivate.h"
21
22 #include "gtkcsscomputedvaluesprivate.h"
23 #include "gtkstyleproviderprivate.h"
24
25 struct _GtkCssValue {
26   GTK_CSS_VALUE_BASE
27 };
28
29 G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref)
30
31 GtkCssValue *
32 _gtk_css_value_alloc (const GtkCssValueClass *klass,
33                       gsize                   size)
34 {
35   GtkCssValue *value;
36
37   value = g_slice_alloc0 (size);
38
39   value->class = klass;
40   value->ref_count = 1;
41
42   return value;
43 }
44
45 GtkCssValue *
46 _gtk_css_value_ref (GtkCssValue *value)
47 {
48   g_return_val_if_fail (value != NULL, NULL);
49
50   g_atomic_int_add (&value->ref_count, 1);
51
52   return value;
53 }
54
55 void
56 _gtk_css_value_unref (GtkCssValue *value)
57 {
58   if (value == NULL)
59     return;
60
61   if (!g_atomic_int_dec_and_test (&value->ref_count))
62     return;
63
64   value->class->free (value);
65 }
66
67 /**
68  * _gtk_css_value_compute:
69  * @value: the value to compute from
70  * @property_id: the ID of the property to compute
71  * @provider: Style provider for looking up extra information
72  * @values: values to compute for
73  * @parent_values: parent values to use for inherited values
74  * @dependencies: (out) (allow-none): Set to the dependencies of the
75  *     computed values that indicate when this value needs to be
76  *     recomputed and how.
77  *
78  * Converts the specified @value into the computed value for the CSS
79  * property given by @property_id using the information in @context.
80  * This step is explained in detail in
81  * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
82  * the CSS documentation</ulink>.
83  *
84  * Returns: the computed value
85  **/
86 GtkCssValue *
87 _gtk_css_value_compute (GtkCssValue             *value,
88                         guint                    property_id,
89                         GtkStyleProviderPrivate *provider,
90                         GtkCssComputedValues    *values,
91                         GtkCssComputedValues    *parent_values,
92                         GtkCssDependencies      *dependencies)
93 {
94   GtkCssDependencies fallback;
95
96   g_return_val_if_fail (value != NULL, NULL);
97   g_return_val_if_fail (GTK_IS_STYLE_PROVIDER_PRIVATE (provider), NULL);
98   g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
99   g_return_val_if_fail (parent_values == NULL || GTK_IS_CSS_COMPUTED_VALUES (parent_values), NULL);
100
101   if (dependencies == NULL)
102     dependencies = &fallback;
103   *dependencies = 0;
104
105   return value->class->compute (value, property_id, provider, values, parent_values, dependencies);
106 }
107
108 gboolean
109 _gtk_css_value_equal (const GtkCssValue *value1,
110                       const GtkCssValue *value2)
111 {
112   g_return_val_if_fail (value1 != NULL, FALSE);
113   g_return_val_if_fail (value2 != NULL, FALSE);
114
115   if (value1 == value2)
116     return TRUE;
117
118   if (value1->class != value2->class)
119     return FALSE;
120
121   return value1->class->equal (value1, value2);
122 }
123
124 gboolean
125 _gtk_css_value_equal0 (const GtkCssValue *value1,
126                        const GtkCssValue *value2)
127 {
128   /* Inclues both values being NULL */
129   if (value1 == value2)
130     return TRUE;
131
132   if (value1 == NULL || value2 == NULL)
133     return FALSE;
134
135   return _gtk_css_value_equal (value1, value2);
136 }
137
138 GtkCssValue *
139 _gtk_css_value_transition (GtkCssValue *start,
140                            GtkCssValue *end,
141                            guint        property_id,
142                            double       progress)
143 {
144   g_return_val_if_fail (start != NULL, FALSE);
145   g_return_val_if_fail (end != NULL, FALSE);
146
147   if (start->class != end->class)
148     return NULL;
149
150   return start->class->transition (start, end, property_id, progress);
151 }
152
153 char *
154 _gtk_css_value_to_string (const GtkCssValue *value)
155 {
156   GString *string;
157
158   g_return_val_if_fail (value != NULL, NULL);
159
160   string = g_string_new (NULL);
161   _gtk_css_value_print (value, string);
162   return g_string_free (string, FALSE);
163 }
164
165 /**
166  * _gtk_css_value_print:
167  * @value: the value to print
168  * @string: the string to print to
169  *
170  * Prints @value to the given @string in CSS format. The @value must be a
171  * valid specified value as parsed using the parse functions or as assigned
172  * via _gtk_style_property_assign().
173  **/
174 void
175 _gtk_css_value_print (const GtkCssValue *value,
176                       GString           *string)
177 {
178   g_return_if_fail (value != NULL);
179   g_return_if_fail (string != NULL);
180
181   value->class->print (value, string);
182 }
183