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