]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssstyleproperty.c
css: Redo value resolving
[~andy/gtk] / gtk / gtkcssstyleproperty.c
1 /*
2  * Copyright © 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.1 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Benjamin Otte <otte@gnome.org>
19  */
20
21 #include "config.h"
22
23 #include "gtkcssstylepropertyprivate.h"
24
25 #include "gtkcssstylefuncsprivate.h"
26 #include "gtkcsstypesprivate.h"
27 #include "gtkintl.h"
28 #include "gtkprivatetypebuiltins.h"
29 #include "gtkstylepropertiesprivate.h"
30
31 enum {
32   PROP_0,
33   PROP_ID,
34   PROP_INHERIT,
35   PROP_INITIAL
36 };
37
38 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
39
40 static void
41 gtk_css_style_property_constructed (GObject *object)
42 {
43   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
44   GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
45
46   property->id = klass->style_properties->len;
47   g_ptr_array_add (klass->style_properties, property);
48
49   G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
50 }
51
52 static void
53 gtk_css_style_property_set_property (GObject      *object,
54                                      guint         prop_id,
55                                      const GValue *value,
56                                      GParamSpec   *pspec)
57 {
58   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
59   const GValue *initial;
60
61   switch (prop_id)
62     {
63     case PROP_INHERIT:
64       property->inherit = g_value_get_boolean (value);
65       break;
66     case PROP_INITIAL:
67       initial = g_value_get_boxed (value);
68       g_assert (initial);
69       g_value_init (&property->initial_value, G_VALUE_TYPE (initial));
70       g_value_copy (initial, &property->initial_value);
71       break;
72     default:
73       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
74       break;
75     }
76 }
77
78 static void
79 gtk_css_style_property_get_property (GObject    *object,
80                                      guint       prop_id,
81                                      GValue     *value,
82                                      GParamSpec *pspec)
83 {
84   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
85
86   switch (prop_id)
87     {
88     case PROP_ID:
89       g_value_set_boolean (value, property->id);
90       break;
91     case PROP_INHERIT:
92       g_value_set_boolean (value, property->inherit);
93       break;
94     case PROP_INITIAL:
95       g_value_set_boxed (value, &property->initial_value);
96       break;
97     default:
98       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
99       break;
100     }
101 }
102
103 static void
104 _gtk_css_style_property_assign (GtkStyleProperty   *property,
105                                 GtkStyleProperties *props,
106                                 GtkStateFlags       state,
107                                 const GValue       *value)
108 {
109   _gtk_style_properties_set_property_by_property (props,
110                                                   GTK_CSS_STYLE_PROPERTY (property),
111                                                   state,
112                                                   value);
113 }
114
115 static void
116 _gtk_style_property_default_value (GtkStyleProperty   *property,
117                                    GtkStyleProperties *properties,
118                                    GtkStateFlags       state,
119                                    GValue             *value)
120 {
121   g_value_copy (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)), value);
122 }
123
124 static void
125 _gtk_css_style_property_query (GtkStyleProperty   *property,
126                                GtkStyleProperties *props,
127                                GtkStateFlags       state,
128                                GtkStylePropertyContext *context,
129                                GValue             *value)
130 {
131   const GValue *val;
132   
133   val = _gtk_style_properties_peek_property (props, GTK_CSS_STYLE_PROPERTY (property), state);
134   if (val)
135     g_value_copy (val, value);
136   else
137     _gtk_style_property_default_value (property, props, state, value);
138 }
139
140 static gboolean
141 gtk_css_style_property_parse_value (GtkStyleProperty *property,
142                                     GValue           *value,
143                                     GtkCssParser     *parser,
144                                     GFile            *base)
145 {
146   gboolean success;
147
148   if (_gtk_css_parser_try (parser, "initial", TRUE))
149     {
150       /* the initial value can be explicitly specified with the
151        * ‘initial’ keyword which all properties accept.
152        */
153       g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
154       g_value_set_enum (value, GTK_CSS_INITIAL);
155       return TRUE;
156     }
157   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
158     {
159       /* All properties accept the ‘inherit’ value which
160        * explicitly specifies that the value will be determined
161        * by inheritance. The ‘inherit’ value can be used to
162        * strengthen inherited values in the cascade, and it can
163        * also be used on properties that are not normally inherited.
164        */
165       g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
166       g_value_set_enum (value, GTK_CSS_INHERIT);
167       return TRUE;
168     }
169
170   g_value_init (value, _gtk_style_property_get_value_type (property));
171   if (property->parse_func)
172     success = (* property->parse_func) (parser, base, value);
173   else
174     success = _gtk_css_style_parse_value (value, parser, base);
175
176   if (!success)
177     g_value_unset (value);
178
179   return success;
180 }
181
182 static void
183 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
184 {
185   GObjectClass *object_class = G_OBJECT_CLASS (klass);
186   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
187
188   object_class->constructed = gtk_css_style_property_constructed;
189   object_class->set_property = gtk_css_style_property_set_property;
190   object_class->get_property = gtk_css_style_property_get_property;
191
192   g_object_class_install_property (object_class,
193                                    PROP_ID,
194                                    g_param_spec_uint ("id",
195                                                       P_("ID"),
196                                                       P_("The numeric id for quick access"),
197                                                       0, G_MAXUINT, 0,
198                                                       G_PARAM_READABLE));
199   g_object_class_install_property (object_class,
200                                    PROP_INHERIT,
201                                    g_param_spec_boolean ("inherit",
202                                                          P_("Inherit"),
203                                                          P_("Set if the value is inherited by default"),
204                                                          FALSE,
205                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
206   g_object_class_install_property (object_class,
207                                    PROP_INITIAL,
208                                    g_param_spec_boxed ("initial-value",
209                                                        P_("Initial value"),
210                                                        P_("The initial specified value used for this property"),
211                                                        G_TYPE_VALUE,
212                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
213
214   property_class->assign = _gtk_css_style_property_assign;
215   property_class->query = _gtk_css_style_property_query;
216   property_class->parse_value = gtk_css_style_property_parse_value;
217
218   klass->style_properties = g_ptr_array_new ();
219 }
220
221
222 static void
223 _gtk_css_style_property_init (GtkCssStyleProperty *style_property)
224 {
225 }
226
227 /**
228  * _gtk_css_style_property_get_n_properties:
229  *
230  * Gets the number of style properties. This number can increase when new
231  * theme engines are loaded. Shorthand properties are not included here.
232  *
233  * Returns: The number of style properties.
234  **/
235 guint
236 _gtk_css_style_property_get_n_properties (void)
237 {
238   GtkCssStylePropertyClass *klass;
239
240   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
241
242   return klass->style_properties->len;
243 }
244
245 /**
246  * _gtk_css_style_property_lookup_by_id:
247  * @id: the id of the property
248  *
249  * Gets the style property with the given id. All style properties (but not
250  * shorthand properties) are indexable by id so that it's easy to use arrays
251  * when doing style lookups.
252  *
253  * Returns: (transfer none): The style property with the given id
254  **/
255 GtkCssStyleProperty *
256 _gtk_css_style_property_lookup_by_id (guint id)
257 {
258   GtkCssStylePropertyClass *klass;
259
260   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
261
262   return g_ptr_array_index (klass->style_properties, id);
263 }
264
265 /**
266  * _gtk_css_style_property_is_inherit:
267  * @property: the property
268  *
269  * Queries if the given @property is inherited. See
270  * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
271  * the CSS documentation</ulink> for an explanation of this concept.
272  *
273  * Returns: %TRUE if the property is inherited by default.
274  **/
275 gboolean
276 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
277 {
278   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
279
280   return property->inherit;
281 }
282
283 /**
284  * _gtk_css_style_property_get_id:
285  * @property: the property
286  *
287  * Gets the id for the given property. IDs are used to allow using arrays
288  * for style lookups.
289  *
290  * Returns: The id of the property
291  **/
292 guint
293 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
294 {
295   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
296
297   return property->id;
298 }
299
300 /**
301  * _gtk_css_style_property_get_initial_value:
302  * @property: the property
303  *
304  * Queries the initial value of the given @property. See
305  * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
306  * the CSS documentation</ulink> for an explanation of this concept.
307  *
308  * Returns: a reference to the initial value. The value will never change.
309  **/
310 const GValue *
311 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
312 {
313   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
314
315   return &property->initial_value;
316 }
317
318 /**
319  * _gtk_css_style_property_compute_value:
320  * @property: the property
321  * @computed: (out): an uninitialized value to be filled with the result
322  * @context: the context to use for resolving
323  * @specified: the value to compute from
324  *
325  * Converts the @specified value into the @computed value using the
326  * information in @context. This step is explained in detail in
327  * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
328  * the CSS documentation</ulink>.
329  **/
330 void
331 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
332                                        GValue              *computed,
333                                        GtkStyleContext     *context,
334                                        const GValue        *specified)
335 {
336   g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
337   g_return_if_fail (computed != NULL && !G_IS_VALUE (computed));
338   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
339   g_return_if_fail (G_IS_VALUE (specified));
340
341   g_value_init (computed, _gtk_style_property_get_value_type (GTK_STYLE_PROPERTY (property)));
342   _gtk_css_style_compute_value (computed, context, specified);
343 }
344
345 /**
346  * _gtk_css_style_property_print_value:
347  * @property: the property
348  * @value: the value to print
349  * @string: the string to print to
350  *
351  * Prints @value to the given @string in CSS format. The @value must be a
352  * valid specified value as parsed using the parse functions or as assigned
353  * via _gtk_style_property_assign().
354  **/
355 void
356 _gtk_css_style_property_print_value (GtkCssStyleProperty    *property,
357                                      const GValue           *value,
358                                      GString                *string)
359 {
360   g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
361   g_return_if_fail (value != NULL);
362   g_return_if_fail (string != NULL);
363
364   if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
365     {
366       GEnumClass *enum_class;
367       GEnumValue *enum_value;
368
369       enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
370       enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
371
372       g_string_append (string, enum_value->value_nick);
373
374       g_type_class_unref (enum_class);
375     }
376   else if (GTK_STYLE_PROPERTY (property)->print_func)
377     (* GTK_STYLE_PROPERTY (property)->print_func) (value, string);
378   else
379     _gtk_css_style_print_value (value, string);
380 }
381