2 * Copyright © 2011 Red Hat Inc.
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
23 #include "gtkcssstylepropertyprivate.h"
25 #include "gtkcssstylefuncsprivate.h"
26 #include "gtkcsstypesprivate.h"
28 #include "gtkprivatetypebuiltins.h"
29 #include "gtkstylepropertiesprivate.h"
32 #include <cairo-gobject.h>
33 #include "gtkcssimagegradientprivate.h"
34 #include "gtkcssimageprivate.h"
36 /* this is in case round() is not provided by the compiler,
37 * such as in the case of C89 compilers, like MSVC
39 #include "fallback-c89.c"
50 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
53 gtk_css_style_property_constructed (GObject *object)
55 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
56 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
58 property->id = klass->style_properties->len;
59 g_ptr_array_add (klass->style_properties, property);
61 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
65 gtk_css_style_property_set_property (GObject *object,
70 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
71 const GValue *initial;
76 property->inherit = g_value_get_boolean (value);
79 initial = g_value_get_boxed (value);
81 g_value_init (&property->initial_value, G_VALUE_TYPE (initial));
82 g_value_copy (initial, &property->initial_value);
84 case PROP_COMPUTED_TYPE:
85 property->computed_type = g_value_get_gtype (value);
86 g_assert (property->computed_type != G_TYPE_NONE);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 gtk_css_style_property_get_property (GObject *object,
100 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
104 case PROP_SPECIFIED_TYPE:
105 g_value_set_gtype (value, G_VALUE_TYPE (&property->initial_value));
107 case PROP_COMPUTED_TYPE:
108 g_value_set_gtype (value, property->computed_type);
111 g_value_set_boolean (value, property->id);
114 g_value_set_boolean (value, property->inherit);
117 g_value_set_boxed (value, &property->initial_value);
120 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
126 _gtk_css_style_property_assign (GtkStyleProperty *property,
127 GtkStyleProperties *props,
131 _gtk_style_properties_set_property_by_property (props,
132 GTK_CSS_STYLE_PROPERTY (property),
138 _gtk_css_style_property_query (GtkStyleProperty *property,
140 GtkStyleQueryFunc query_func,
145 val = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
148 /* Somebody make this a vfunc */
149 if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_IMAGE)
151 GtkCssImage *image = g_value_get_object (val);
152 cairo_pattern_t *pattern;
153 cairo_surface_t *surface;
154 cairo_matrix_t matrix;
157 g_value_set_boxed (value, NULL);
158 else if (GTK_IS_CSS_IMAGE_GRADIENT (image))
159 g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
162 double width, height;
164 /* the 100, 100 is rather random */
165 _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
166 surface = _gtk_css_image_get_surface (image, NULL, width, height);
167 pattern = cairo_pattern_create_for_surface (surface);
168 cairo_matrix_init_scale (&matrix, width, height);
169 cairo_pattern_set_matrix (pattern, &matrix);
170 cairo_surface_destroy (surface);
171 g_value_take_boxed (value, pattern);
174 else if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_NUMBER)
176 g_value_set_int (value, round (_gtk_css_number_get (g_value_get_boxed (val), 100)));
179 g_value_copy (val, value);
182 g_value_copy (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)), value);
186 gtk_css_style_property_parse_value (GtkStyleProperty *property,
188 GtkCssParser *parser,
191 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
193 if (_gtk_css_parser_try (parser, "initial", TRUE))
195 /* the initial value can be explicitly specified with the
196 * ‘initial’ keyword which all properties accept.
198 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
199 g_value_set_enum (value, GTK_CSS_INITIAL);
202 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
204 /* All properties accept the ‘inherit’ value which
205 * explicitly specifies that the value will be determined
206 * by inheritance. The ‘inherit’ value can be used to
207 * strengthen inherited values in the cascade, and it can
208 * also be used on properties that are not normally inherited.
210 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
211 g_value_set_enum (value, GTK_CSS_INHERIT);
215 g_value_init (value, _gtk_css_style_property_get_specified_type (style_property));
216 if (!(* style_property->parse_value) (style_property, value, parser, base))
218 g_value_unset (value);
226 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
228 GObjectClass *object_class = G_OBJECT_CLASS (klass);
229 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
231 object_class->constructed = gtk_css_style_property_constructed;
232 object_class->set_property = gtk_css_style_property_set_property;
233 object_class->get_property = gtk_css_style_property_get_property;
235 g_object_class_install_property (object_class,
237 g_param_spec_uint ("id",
239 P_("The numeric id for quick access"),
242 g_object_class_install_property (object_class,
244 g_param_spec_gtype ("specified-type",
245 P_("Specified type"),
246 P_("The type of values after parsing"),
249 g_object_class_install_property (object_class,
251 g_param_spec_gtype ("computed-type",
253 P_("The type of values after style lookup"),
255 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
256 g_object_class_install_property (object_class,
258 g_param_spec_boolean ("inherit",
260 P_("Set if the value is inherited by default"),
262 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
263 g_object_class_install_property (object_class,
265 g_param_spec_boxed ("initial-value",
267 P_("The initial specified value used for this property"),
269 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
271 property_class->assign = _gtk_css_style_property_assign;
272 property_class->query = _gtk_css_style_property_query;
273 property_class->parse_value = gtk_css_style_property_parse_value;
275 klass->style_properties = g_ptr_array_new ();
279 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
281 GtkCssParser *parser,
284 return _gtk_css_style_parse_value (value, parser, base);
288 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
292 _gtk_css_style_print_value (value, string);
296 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
298 GtkStyleContext *context,
299 const GValue *specified)
301 _gtk_css_style_compute_value (computed, context, specified);
305 _gtk_css_style_property_init (GtkCssStyleProperty *property)
307 property->parse_value = gtk_css_style_property_real_parse_value;
308 property->print_value = gtk_css_style_property_real_print_value;
309 property->compute_value = gtk_css_style_property_real_compute_value;
313 * _gtk_css_style_property_get_n_properties:
315 * Gets the number of style properties. This number can increase when new
316 * theme engines are loaded. Shorthand properties are not included here.
318 * Returns: The number of style properties.
321 _gtk_css_style_property_get_n_properties (void)
323 GtkCssStylePropertyClass *klass;
325 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
326 if (G_UNLIKELY (klass == NULL))
328 _gtk_style_property_init_properties ();
329 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
333 return klass->style_properties->len;
337 * _gtk_css_style_property_lookup_by_id:
338 * @id: the id of the property
340 * Gets the style property with the given id. All style properties (but not
341 * shorthand properties) are indexable by id so that it's easy to use arrays
342 * when doing style lookups.
344 * Returns: (transfer none): The style property with the given id
346 GtkCssStyleProperty *
347 _gtk_css_style_property_lookup_by_id (guint id)
349 GtkCssStylePropertyClass *klass;
351 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
352 if (G_UNLIKELY (klass == NULL))
354 _gtk_style_property_init_properties ();
355 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
358 g_return_val_if_fail (id < klass->style_properties->len, NULL);
360 return g_ptr_array_index (klass->style_properties, id);
364 * _gtk_css_style_property_is_inherit:
365 * @property: the property
367 * Queries if the given @property is inherited. See
368 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
369 * the CSS documentation</ulink> for an explanation of this concept.
371 * Returns: %TRUE if the property is inherited by default.
374 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
376 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
378 return property->inherit;
382 * _gtk_css_style_property_get_id:
383 * @property: the property
385 * Gets the id for the given property. IDs are used to allow using arrays
388 * Returns: The id of the property
391 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
393 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
399 * _gtk_css_style_property_get_initial_value:
400 * @property: the property
402 * Queries the initial value of the given @property. See
403 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
404 * the CSS documentation</ulink> for an explanation of this concept.
406 * Returns: a reference to the initial value. The value will never change.
409 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
411 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
413 return &property->initial_value;
417 * _gtk_css_style_property_get_computed_type:
418 * @property: the property to query
420 * Gets the #GType used for values for this property after a CSS lookup has
421 * happened. _gtk_css_style_property_compute_value() will convert values to
424 * Returns: the #GType used for computed values.
427 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
429 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
431 return property->computed_type;
435 * _gtk_css_style_property_get_specified_type:
436 * @property: the property to query
438 * Gets the #GType used for values for this property after CSS parsing if
439 * the value is not a special keyword. _gtk_css_style_property_compute_value()
440 * will convert values of this type to the computed type.
442 * The initial value returned by _gtk_css_style_property_get_initial_value()
443 * will be of this type.
445 * Returns: the #GType used for specified values.
448 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
450 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
452 return G_VALUE_TYPE (&property->initial_value);
456 _gtk_css_style_property_is_specified_type (GtkCssStyleProperty *property,
459 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
461 /* If it's our specified type, of course it's valid */
462 if (type == G_VALUE_TYPE (&property->initial_value))
465 /* The special values 'inherit' and 'initial' are always valid */
466 if (type == GTK_TYPE_CSS_SPECIAL_VALUE)
469 /* XXX: Someone needs to fix that legacy */
470 if (G_VALUE_TYPE (&property->initial_value) == CAIRO_GOBJECT_TYPE_PATTERN &&
471 type == GTK_TYPE_GRADIENT)
478 * _gtk_css_style_property_compute_value:
479 * @property: the property
480 * @computed: (out): an uninitialized value to be filled with the result
481 * @context: the context to use for resolving
482 * @specified: the value to compute from
484 * Converts the @specified value into the @computed value using the
485 * information in @context. This step is explained in detail in
486 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
487 * the CSS documentation</ulink>.
490 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
492 GtkStyleContext *context,
493 const GValue *specified)
495 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
496 g_return_if_fail (computed != NULL && !G_IS_VALUE (computed));
497 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
498 g_return_if_fail (G_IS_VALUE (specified));
500 g_value_init (computed, _gtk_css_style_property_get_computed_type (property));
502 property->compute_value (property, computed, context, specified);
506 * _gtk_css_style_property_print_value:
507 * @property: the property
508 * @value: the value to print
509 * @string: the string to print to
511 * Prints @value to the given @string in CSS format. The @value must be a
512 * valid specified value as parsed using the parse functions or as assigned
513 * via _gtk_style_property_assign().
516 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
520 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
521 g_return_if_fail (value != NULL);
522 g_return_if_fail (string != NULL);
524 if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
526 GEnumClass *enum_class;
527 GEnumValue *enum_value;
529 enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
530 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
532 g_string_append (string, enum_value->value_nick);
534 g_type_class_unref (enum_class);
537 property->print_value (property, value, string);