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