]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssstyleproperty.c
styleproperty: 3rd hotfix for a 3-line patch
[~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 void
302 _gtk_css_style_property_init (GtkCssStyleProperty *property)
303 {
304   property->parse_value = gtk_css_style_property_real_parse_value;
305   property->print_value = gtk_css_style_property_real_print_value;
306   property->compute_value = gtk_css_style_property_real_compute_value;
307 }
308
309 /**
310  * _gtk_css_style_property_get_n_properties:
311  *
312  * Gets the number of style properties. This number can increase when new
313  * theme engines are loaded. Shorthand properties are not included here.
314  *
315  * Returns: The number of style properties.
316  **/
317 guint
318 _gtk_css_style_property_get_n_properties (void)
319 {
320   GtkCssStylePropertyClass *klass;
321
322   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
323   if (G_UNLIKELY (klass == NULL))
324     {
325       _gtk_style_property_init_properties ();
326       klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
327       g_assert (klass);
328     }
329
330   return klass->style_properties->len;
331 }
332
333 /**
334  * _gtk_css_style_property_lookup_by_id:
335  * @id: the id of the property
336  *
337  * Gets the style property with the given id. All style properties (but not
338  * shorthand properties) are indexable by id so that it's easy to use arrays
339  * when doing style lookups.
340  *
341  * Returns: (transfer none): The style property with the given id
342  **/
343 GtkCssStyleProperty *
344 _gtk_css_style_property_lookup_by_id (guint id)
345 {
346   GtkCssStylePropertyClass *klass;
347
348   klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
349   if (G_UNLIKELY (klass == NULL))
350     {
351       _gtk_style_property_init_properties ();
352       klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
353       g_assert (klass);
354     }
355   g_return_val_if_fail (id < klass->style_properties->len, NULL);
356
357   return g_ptr_array_index (klass->style_properties, id);
358 }
359
360 /**
361  * _gtk_css_style_property_is_inherit:
362  * @property: the property
363  *
364  * Queries if the given @property is inherited. See
365  * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
366  * the CSS documentation</ulink> for an explanation of this concept.
367  *
368  * Returns: %TRUE if the property is inherited by default.
369  **/
370 gboolean
371 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
372 {
373   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
374
375   return property->inherit;
376 }
377
378 /**
379  * _gtk_css_style_property_get_id:
380  * @property: the property
381  *
382  * Gets the id for the given property. IDs are used to allow using arrays
383  * for style lookups.
384  *
385  * Returns: The id of the property
386  **/
387 guint
388 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
389 {
390   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
391
392   return property->id;
393 }
394
395 /**
396  * _gtk_css_style_property_get_initial_value:
397  * @property: the property
398  *
399  * Queries the initial value of the given @property. See
400  * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
401  * the CSS documentation</ulink> for an explanation of this concept.
402  *
403  * Returns: a reference to the initial value. The value will never change.
404  **/
405 GtkCssValue *
406 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
407 {
408   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
409
410   return property->initial_value;
411 }
412
413 /**
414  * _gtk_css_style_property_get_computed_type:
415  * @property: the property to query
416  *
417  * Gets the #GType used for values for this property after a CSS lookup has
418  * happened. _gtk_css_style_property_compute_value() will convert values to
419  * this type.
420  *
421  * Returns: the #GType used for computed values.
422  **/
423 GType
424 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
425 {
426   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
427
428   return property->computed_type;
429 }
430
431 /**
432  * _gtk_css_style_property_get_specified_type:
433  * @property: the property to query
434  *
435  * Gets the #GType used for values for this property after CSS parsing if
436  * the value is not a special keyword. _gtk_css_style_property_compute_value()
437  * will convert values of this type to the computed type.
438  *
439  * The initial value returned by _gtk_css_style_property_get_initial_value()
440  * will be of this type.
441  *
442  * Returns: the #GType used for specified values.
443  **/
444 GType
445 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
446 {
447   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
448
449   return _gtk_css_value_get_content_type (property->initial_value);
450 }
451
452 gboolean
453 _gtk_css_style_property_is_specified_type (GtkCssStyleProperty *property,
454                                            GType                type)
455 {
456   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
457
458   /* If it's our specified type, of course it's valid */
459   if (type == _gtk_css_value_get_content_type (property->initial_value))
460     return TRUE;
461
462   /* The special values 'inherit' and 'initial' are always valid */
463   if (type == GTK_TYPE_CSS_SPECIAL_VALUE)
464     return TRUE;
465
466   /* XXX: Someone needs to fix that legacy */
467   if ((_gtk_css_value_holds (property->initial_value, GDK_TYPE_RGBA) ||
468        _gtk_css_value_holds (property->initial_value, GDK_TYPE_COLOR)) &&
469       type == GTK_TYPE_SYMBOLIC_COLOR)
470     return TRUE;
471   if (_gtk_css_value_holds (property->initial_value, CAIRO_GOBJECT_TYPE_PATTERN) &&
472       type == GTK_TYPE_GRADIENT)
473     return TRUE;
474
475   return FALSE;
476 }
477
478 /**
479  * _gtk_css_style_property_compute_value:
480  * @property: the property
481  * @computed: (out): an uninitialized value to be filled with the result
482  * @context: the context to use for resolving
483  * @specified: the value to compute from
484  *
485  * Converts the @specified value into the @computed value using the
486  * information in @context. This step is explained in detail in
487  * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
488  * the CSS documentation</ulink>.
489  **/
490 GtkCssValue *
491 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
492                                        GtkStyleContext     *context,
493                                        GtkCssValue         *specified)
494 {
495   g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
496   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
497
498   return property->compute_value (property, context, specified);
499 }
500
501 /**
502  * _gtk_css_style_property_print_value:
503  * @property: the property
504  * @value: the value to print
505  * @string: the string to print to
506  *
507  * Prints @value to the given @string in CSS format. The @value must be a
508  * valid specified value as parsed using the parse functions or as assigned
509  * via _gtk_style_property_assign().
510  **/
511 void
512 _gtk_css_style_property_print_value (GtkCssStyleProperty    *property,
513                                      GtkCssValue            *css_value,
514                                      GString                *string)
515 {
516   g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
517   g_return_if_fail (css_value != NULL);
518   g_return_if_fail (string != NULL);
519
520   if (_gtk_css_value_is_special (css_value))
521     {
522       GEnumClass *enum_class;
523       GEnumValue *enum_value;
524
525       enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
526       enum_value = g_enum_get_value (enum_class, _gtk_css_value_get_special_kind (css_value));
527
528       g_string_append (string, enum_value->value_nick);
529
530       g_type_class_unref (enum_class);
531     }
532   else
533     {
534       GValue value = G_VALUE_INIT;
535       _gtk_css_value_init_gvalue (css_value, &value);
536       property->print_value (property, &value, string);
537       g_value_unset (&value);
538     }
539 }
540