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