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