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