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