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