]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssstyleproperty.c
css: Add a check function for specified types
[~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 #include <cairo-gobject.h>
32 #include "gtkcssimagegradientprivate.h"
33 #include "gtkcssimageprivate.h"
34
35 enum {
36   PROP_0,
37   PROP_ID,
38   PROP_SPECIFIED_TYPE,
39   PROP_COMPUTED_TYPE,
40   PROP_INHERIT,
41   PROP_INITIAL
42 };
43
44 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
45
46 static void
47 gtk_css_style_property_constructed (GObject *object)
48 {
49   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
50   GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
51
52   property->id = klass->style_properties->len;
53   g_ptr_array_add (klass->style_properties, property);
54
55   G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
56 }
57
58 static void
59 gtk_css_style_property_set_property (GObject      *object,
60                                      guint         prop_id,
61                                      const GValue *value,
62                                      GParamSpec   *pspec)
63 {
64   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
65   const GValue *initial;
66
67   switch (prop_id)
68     {
69     case PROP_INHERIT:
70       property->inherit = g_value_get_boolean (value);
71       break;
72     case PROP_INITIAL:
73       initial = g_value_get_boxed (value);
74       g_assert (initial);
75       g_value_init (&property->initial_value, G_VALUE_TYPE (initial));
76       g_value_copy (initial, &property->initial_value);
77       break;
78     case PROP_COMPUTED_TYPE:
79       property->computed_type = g_value_get_gtype (value);
80       g_assert (property->computed_type != G_TYPE_NONE);
81       break;
82     default:
83       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84       break;
85     }
86 }
87
88 static void
89 gtk_css_style_property_get_property (GObject    *object,
90                                      guint       prop_id,
91                                      GValue     *value,
92                                      GParamSpec *pspec)
93 {
94   GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
95
96   switch (prop_id)
97     {
98     case PROP_SPECIFIED_TYPE:
99       g_value_set_gtype (value, G_VALUE_TYPE (&property->initial_value));
100       break;
101     case PROP_COMPUTED_TYPE:
102       g_value_set_gtype (value, property->computed_type);
103       break;
104     case PROP_ID:
105       g_value_set_boolean (value, property->id);
106       break;
107     case PROP_INHERIT:
108       g_value_set_boolean (value, property->inherit);
109       break;
110     case PROP_INITIAL:
111       g_value_set_boxed (value, &property->initial_value);
112       break;
113     default:
114       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
115       break;
116     }
117 }
118
119 static void
120 _gtk_css_style_property_assign (GtkStyleProperty   *property,
121                                 GtkStyleProperties *props,
122                                 GtkStateFlags       state,
123                                 const GValue       *value)
124 {
125   _gtk_style_properties_set_property_by_property (props,
126                                                   GTK_CSS_STYLE_PROPERTY (property),
127                                                   state,
128                                                   value);
129 }
130
131 static void
132 _gtk_css_style_property_query (GtkStyleProperty   *property,
133                                GValue             *value,
134                                GtkStyleQueryFunc   query_func,
135                                gpointer            query_data)
136 {
137   const GValue *val;
138   
139   val = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
140   if (val)
141     {
142       /* Somebody make this a vfunc */
143       if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_IMAGE)
144         {
145           GtkCssImage *image = g_value_get_object (val);
146           cairo_pattern_t *pattern;
147           cairo_surface_t *surface;
148           cairo_matrix_t matrix;
149           
150           if (image == NULL)
151             g_value_set_boxed (value, NULL);
152           else if (GTK_IS_CSS_IMAGE_GRADIENT (image))
153             g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
154           else
155             {
156               double width, height;
157
158               /* the 100, 100 is rather random */
159               _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
160               surface = _gtk_css_image_get_surface (image, NULL, width, height);
161               pattern = cairo_pattern_create_for_surface (surface);
162               cairo_matrix_init_scale (&matrix, width, height);
163               cairo_pattern_set_matrix (pattern, &matrix);
164               cairo_surface_destroy (surface);
165               g_value_take_boxed (value, pattern);
166             }
167         }
168       else
169         g_value_copy (val, value);
170     }
171   else
172     g_value_copy (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)), value);
173 }
174
175 static gboolean
176 gtk_css_style_property_parse_value (GtkStyleProperty *property,
177                                     GValue           *value,
178                                     GtkCssParser     *parser,
179                                     GFile            *base)
180 {
181   GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
182
183   if (_gtk_css_parser_try (parser, "initial", TRUE))
184     {
185       /* the initial value can be explicitly specified with the
186        * ‘initial’ keyword which all properties accept.
187        */
188       g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
189       g_value_set_enum (value, GTK_CSS_INITIAL);
190       return TRUE;
191     }
192   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
193     {
194       /* All properties accept the ‘inherit’ value which
195        * explicitly specifies that the value will be determined
196        * by inheritance. The ‘inherit’ value can be used to
197        * strengthen inherited values in the cascade, and it can
198        * also be used on properties that are not normally inherited.
199        */
200       g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
201       g_value_set_enum (value, GTK_CSS_INHERIT);
202       return TRUE;
203     }
204
205   g_value_init (value, _gtk_css_style_property_get_specified_type (style_property));
206   if (!(* style_property->parse_value) (style_property, value, parser, base))
207     {
208       g_value_unset (value);
209       return FALSE;
210     }
211
212   return TRUE;
213 }
214
215 static void
216 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
217 {
218   GObjectClass *object_class = G_OBJECT_CLASS (klass);
219   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
220
221   object_class->constructed = gtk_css_style_property_constructed;
222   object_class->set_property = gtk_css_style_property_set_property;
223   object_class->get_property = gtk_css_style_property_get_property;
224
225   g_object_class_install_property (object_class,
226                                    PROP_ID,
227                                    g_param_spec_uint ("id",
228                                                       P_("ID"),
229                                                       P_("The numeric id for quick access"),
230                                                       0, G_MAXUINT, 0,
231                                                       G_PARAM_READABLE));
232   g_object_class_install_property (object_class,
233                                    PROP_SPECIFIED_TYPE,
234                                    g_param_spec_gtype ("specified-type",
235                                                        P_("Specified type"),
236                                                        P_("The type of values after parsing"),
237                                                        G_TYPE_NONE,
238                                                        G_PARAM_READABLE));
239   g_object_class_install_property (object_class,
240                                    PROP_COMPUTED_TYPE,
241                                    g_param_spec_gtype ("computed-type",
242                                                        P_("Computed type"),
243                                                        P_("The type of values after style lookup"),
244                                                        G_TYPE_NONE,
245                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
246   g_object_class_install_property (object_class,
247                                    PROP_INHERIT,
248                                    g_param_spec_boolean ("inherit",
249                                                          P_("Inherit"),
250                                                          P_("Set if the value is inherited by default"),
251                                                          FALSE,
252                                                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
253   g_object_class_install_property (object_class,
254                                    PROP_INITIAL,
255                                    g_param_spec_boxed ("initial-value",
256                                                        P_("Initial value"),
257                                                        P_("The initial specified value used for this property"),
258                                                        G_TYPE_VALUE,
259                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
260
261   property_class->assign = _gtk_css_style_property_assign;
262   property_class->query = _gtk_css_style_property_query;
263   property_class->parse_value = gtk_css_style_property_parse_value;
264
265   klass->style_properties = g_ptr_array_new ();
266 }
267
268 static gboolean
269 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
270                                          GValue              *value,
271                                          GtkCssParser        *parser,
272                                          GFile               *base)
273 {
274   return _gtk_css_style_parse_value (value, parser, base);
275 }
276
277 static void
278 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
279                                          const GValue        *value,
280                                          GString             *string)
281 {
282   _gtk_css_style_print_value (value, string);
283 }
284
285 static void
286 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
287                                            GValue              *computed,
288                                            GtkStyleContext     *context,
289                                            const GValue        *specified)
290 {
291   _gtk_css_style_compute_value (computed, context, specified);
292 }
293
294 static void
295 _gtk_css_style_property_init (GtkCssStyleProperty *property)
296 {
297   property->parse_value = gtk_css_style_property_real_parse_value;
298   property->print_value = gtk_css_style_property_real_print_value;
299   property->compute_value = gtk_css_style_property_real_compute_value;
300 }
301
302 /**
303  * _gtk_css_style_property_get_n_properties:
304  *
305  * Gets the number of style properties. This number can increase when new
306  * theme engines are loaded. Shorthand properties are not included here.
307  *
308  * Returns: The number of style properties.
309  **/
310 guint
311 _gtk_css_style_property_get_n_properties (void)
312 {
313   GtkCssStylePropertyClass *klass;
314
315   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
316   if (G_UNLIKELY (klass == NULL))
317     {
318       _gtk_style_property_init_properties ();
319       klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
320       g_assert (klass);
321     }
322
323   return klass->style_properties->len;
324 }
325
326 /**
327  * _gtk_css_style_property_lookup_by_id:
328  * @id: the id of the property
329  *
330  * Gets the style property with the given id. All style properties (but not
331  * shorthand properties) are indexable by id so that it's easy to use arrays
332  * when doing style lookups.
333  *
334  * Returns: (transfer none): The style property with the given id
335  **/
336 GtkCssStyleProperty *
337 _gtk_css_style_property_lookup_by_id (guint id)
338 {
339   GtkCssStylePropertyClass *klass;
340
341   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
342   if (G_UNLIKELY (klass == NULL))
343     {
344       _gtk_style_property_init_properties ();
345       klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
346       g_assert (klass);
347     }
348   g_return_val_if_fail (id < klass->style_properties->len, NULL);
349
350   return g_ptr_array_index (klass->style_properties, id);
351 }
352
353 /**
354  * _gtk_css_style_property_is_inherit:
355  * @property: the property
356  *
357  * Queries if the given @property is inherited. See
358  * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
359  * the CSS documentation</ulink> for an explanation of this concept.
360  *
361  * Returns: %TRUE if the property is inherited by default.
362  **/
363 gboolean
364 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
365 {
366   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
367
368   return property->inherit;
369 }
370
371 /**
372  * _gtk_css_style_property_get_id:
373  * @property: the property
374  *
375  * Gets the id for the given property. IDs are used to allow using arrays
376  * for style lookups.
377  *
378  * Returns: The id of the property
379  **/
380 guint
381 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
382 {
383   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
384
385   return property->id;
386 }
387
388 /**
389  * _gtk_css_style_property_get_initial_value:
390  * @property: the property
391  *
392  * Queries the initial value of the given @property. See
393  * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
394  * the CSS documentation</ulink> for an explanation of this concept.
395  *
396  * Returns: a reference to the initial value. The value will never change.
397  **/
398 const GValue *
399 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
400 {
401   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
402
403   return &property->initial_value;
404 }
405
406 /**
407  * _gtk_css_style_property_get_computed_type:
408  * @property: the property to query
409  *
410  * Gets the #GType used for values for this property after a CSS lookup has
411  * happened. _gtk_css_style_property_compute_value() will convert values to
412  * this type.
413  *
414  * Returns: the #GType used for computed values.
415  **/
416 GType
417 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
418 {
419   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
420
421   return property->computed_type;
422 }
423
424 /**
425  * _gtk_css_style_property_get_specified_type:
426  * @property: the property to query
427  *
428  * Gets the #GType used for values for this property after CSS parsing if
429  * the value is not a special keyword. _gtk_css_style_property_compute_value()
430  * will convert values of this type to the computed type.
431  *
432  * The initial value returned by _gtk_css_style_property_get_initial_value()
433  * will be of this type.
434  *
435  * Returns: the #GType used for specified values.
436  **/
437 GType
438 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
439 {
440   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
441
442   return G_VALUE_TYPE (&property->initial_value);
443 }
444
445 gboolean
446 _gtk_css_style_property_is_specified_type (GtkCssStyleProperty *property,
447                                            GType                type)
448 {
449   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
450
451   /* If it's our specified type, of course it's valid */
452   if (type == G_VALUE_TYPE (&property->initial_value))
453     return TRUE;
454
455   /* The special values 'inherit' and 'initial' are always valid */
456   if (type == GTK_TYPE_CSS_SPECIAL_VALUE)
457     return TRUE;
458
459   /* XXX: Someone needs to fix that legacy */
460   if (G_VALUE_TYPE (&property->initial_value) == CAIRO_GOBJECT_TYPE_PATTERN &&
461       type == GTK_TYPE_GRADIENT)
462     return TRUE;
463
464   return FALSE;
465 }
466
467 /**
468  * _gtk_css_style_property_compute_value:
469  * @property: the property
470  * @computed: (out): an uninitialized value to be filled with the result
471  * @context: the context to use for resolving
472  * @specified: the value to compute from
473  *
474  * Converts the @specified value into the @computed value using the
475  * information in @context. This step is explained in detail in
476  * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
477  * the CSS documentation</ulink>.
478  **/
479 void
480 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
481                                        GValue              *computed,
482                                        GtkStyleContext     *context,
483                                        const GValue        *specified)
484 {
485   g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
486   g_return_if_fail (computed != NULL && !G_IS_VALUE (computed));
487   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
488   g_return_if_fail (G_IS_VALUE (specified));
489
490   g_value_init (computed, _gtk_css_style_property_get_computed_type (property));
491
492   property->compute_value (property, computed, context, specified);
493 }
494
495 /**
496  * _gtk_css_style_property_print_value:
497  * @property: the property
498  * @value: the value to print
499  * @string: the string to print to
500  *
501  * Prints @value to the given @string in CSS format. The @value must be a
502  * valid specified value as parsed using the parse functions or as assigned
503  * via _gtk_style_property_assign().
504  **/
505 void
506 _gtk_css_style_property_print_value (GtkCssStyleProperty    *property,
507                                      const GValue           *value,
508                                      GString                *string)
509 {
510   g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
511   g_return_if_fail (value != NULL);
512   g_return_if_fail (string != NULL);
513
514   if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
515     {
516       GEnumClass *enum_class;
517       GEnumValue *enum_value;
518
519       enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
520       enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
521
522       g_string_append (string, enum_value->value_nick);
523
524       g_type_class_unref (enum_class);
525     }
526   else
527     property->print_value (property, value, string);
528 }
529