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