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