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