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