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