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