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