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, see <http://www.gnu.org/licenses/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
22 #include "gtkcssstylepropertyprivate.h"
24 #include "gtkcssstylefuncsprivate.h"
25 #include "gtkcsstypesprivate.h"
27 #include "gtkprivatetypebuiltins.h"
28 #include "gtkstylepropertiesprivate.h"
31 #include <cairo-gobject.h>
32 #include "gtkcssimagegradientprivate.h"
33 #include "gtkcssimageprivate.h"
35 /* this is in case round() is not provided by the compiler,
36 * such as in the case of C89 compilers, like MSVC
38 #include "fallback-c89.c"
49 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
52 gtk_css_style_property_constructed (GObject *object)
54 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
55 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
57 property->id = klass->style_properties->len;
58 g_ptr_array_add (klass->style_properties, property);
60 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
64 gtk_css_style_property_set_property (GObject *object,
69 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
70 const GValue *initial;
75 property->inherit = g_value_get_boolean (value);
78 initial = g_value_get_boxed (value);
80 g_value_init (&property->initial_value, G_VALUE_TYPE (initial));
81 g_value_copy (initial, &property->initial_value);
83 case PROP_COMPUTED_TYPE:
84 property->computed_type = g_value_get_gtype (value);
85 g_assert (property->computed_type != G_TYPE_NONE);
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94 gtk_css_style_property_get_property (GObject *object,
99 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
103 case PROP_SPECIFIED_TYPE:
104 g_value_set_gtype (value, G_VALUE_TYPE (&property->initial_value));
106 case PROP_COMPUTED_TYPE:
107 g_value_set_gtype (value, property->computed_type);
110 g_value_set_boolean (value, property->id);
113 g_value_set_boolean (value, property->inherit);
116 g_value_set_boxed (value, &property->initial_value);
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125 _gtk_css_style_property_assign (GtkStyleProperty *property,
126 GtkStyleProperties *props,
130 _gtk_style_properties_set_property_by_property (props,
131 GTK_CSS_STYLE_PROPERTY (property),
137 _gtk_css_style_property_query (GtkStyleProperty *property,
139 GtkStyleQueryFunc query_func,
144 val = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
147 /* Somebody make this a vfunc */
148 if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_IMAGE)
150 GtkCssImage *image = g_value_get_object (val);
151 cairo_pattern_t *pattern;
152 cairo_surface_t *surface;
153 cairo_matrix_t matrix;
156 g_value_set_boxed (value, NULL);
157 else if (GTK_IS_CSS_IMAGE_GRADIENT (image))
158 g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
161 double width, height;
163 /* the 100, 100 is rather random */
164 _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
165 surface = _gtk_css_image_get_surface (image, NULL, width, height);
166 pattern = cairo_pattern_create_for_surface (surface);
167 cairo_matrix_init_scale (&matrix, width, height);
168 cairo_pattern_set_matrix (pattern, &matrix);
169 cairo_surface_destroy (surface);
170 g_value_take_boxed (value, pattern);
173 else if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_NUMBER)
175 g_value_set_int (value, round (_gtk_css_number_get (g_value_get_boxed (val), 100)));
178 g_value_copy (val, value);
181 g_value_copy (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)), value);
185 gtk_css_style_property_parse_value (GtkStyleProperty *property,
187 GtkCssParser *parser,
190 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
192 if (_gtk_css_parser_try (parser, "initial", TRUE))
194 /* the initial value can be explicitly specified with the
195 * ‘initial’ keyword which all properties accept.
197 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
198 g_value_set_enum (value, GTK_CSS_INITIAL);
201 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
203 /* All properties accept the ‘inherit’ value which
204 * explicitly specifies that the value will be determined
205 * by inheritance. The ‘inherit’ value can be used to
206 * strengthen inherited values in the cascade, and it can
207 * also be used on properties that are not normally inherited.
209 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
210 g_value_set_enum (value, GTK_CSS_INHERIT);
214 g_value_init (value, _gtk_css_style_property_get_specified_type (style_property));
215 if (!(* style_property->parse_value) (style_property, value, parser, base))
217 g_value_unset (value);
225 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
227 GObjectClass *object_class = G_OBJECT_CLASS (klass);
228 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
230 object_class->constructed = gtk_css_style_property_constructed;
231 object_class->set_property = gtk_css_style_property_set_property;
232 object_class->get_property = gtk_css_style_property_get_property;
234 g_object_class_install_property (object_class,
236 g_param_spec_uint ("id",
238 P_("The numeric id for quick access"),
241 g_object_class_install_property (object_class,
243 g_param_spec_gtype ("specified-type",
244 P_("Specified type"),
245 P_("The type of values after parsing"),
248 g_object_class_install_property (object_class,
250 g_param_spec_gtype ("computed-type",
252 P_("The type of values after style lookup"),
254 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
255 g_object_class_install_property (object_class,
257 g_param_spec_boolean ("inherit",
259 P_("Set if the value is inherited by default"),
261 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
262 g_object_class_install_property (object_class,
264 g_param_spec_boxed ("initial-value",
266 P_("The initial specified value used for this property"),
268 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
270 property_class->assign = _gtk_css_style_property_assign;
271 property_class->query = _gtk_css_style_property_query;
272 property_class->parse_value = gtk_css_style_property_parse_value;
274 klass->style_properties = g_ptr_array_new ();
278 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
280 GtkCssParser *parser,
283 return _gtk_css_style_parse_value (value, parser, base);
287 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
291 _gtk_css_style_print_value (value, string);
295 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
297 GtkStyleContext *context,
298 const GValue *specified)
300 _gtk_css_style_compute_value (computed, context, specified);
304 _gtk_css_style_property_init (GtkCssStyleProperty *property)
306 property->parse_value = gtk_css_style_property_real_parse_value;
307 property->print_value = gtk_css_style_property_real_print_value;
308 property->compute_value = gtk_css_style_property_real_compute_value;
312 * _gtk_css_style_property_get_n_properties:
314 * Gets the number of style properties. This number can increase when new
315 * theme engines are loaded. Shorthand properties are not included here.
317 * Returns: The number of style properties.
320 _gtk_css_style_property_get_n_properties (void)
322 GtkCssStylePropertyClass *klass;
324 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
325 if (G_UNLIKELY (klass == NULL))
327 _gtk_style_property_init_properties ();
328 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
332 return klass->style_properties->len;
336 * _gtk_css_style_property_lookup_by_id:
337 * @id: the id of the property
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.
343 * Returns: (transfer none): The style property with the given id
345 GtkCssStyleProperty *
346 _gtk_css_style_property_lookup_by_id (guint id)
348 GtkCssStylePropertyClass *klass;
350 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
351 if (G_UNLIKELY (klass == NULL))
353 _gtk_style_property_init_properties ();
354 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
357 g_return_val_if_fail (id < klass->style_properties->len, NULL);
359 return g_ptr_array_index (klass->style_properties, id);
363 * _gtk_css_style_property_is_inherit:
364 * @property: the property
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.
370 * Returns: %TRUE if the property is inherited by default.
373 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
375 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
377 return property->inherit;
381 * _gtk_css_style_property_get_id:
382 * @property: the property
384 * Gets the id for the given property. IDs are used to allow using arrays
387 * Returns: The id of the property
390 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
392 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
398 * _gtk_css_style_property_get_initial_value:
399 * @property: the property
401 * Queries the initial value of the given @property. See
402 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
403 * the CSS documentation</ulink> for an explanation of this concept.
405 * Returns: a reference to the initial value. The value will never change.
408 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
410 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
412 return &property->initial_value;
416 * _gtk_css_style_property_get_computed_type:
417 * @property: the property to query
419 * Gets the #GType used for values for this property after a CSS lookup has
420 * happened. _gtk_css_style_property_compute_value() will convert values to
423 * Returns: the #GType used for computed values.
426 _gtk_css_style_property_get_computed_type (GtkCssStyleProperty *property)
428 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
430 return property->computed_type;
434 * _gtk_css_style_property_get_specified_type:
435 * @property: the property to query
437 * Gets the #GType used for values for this property after CSS parsing if
438 * the value is not a special keyword. _gtk_css_style_property_compute_value()
439 * will convert values of this type to the computed type.
441 * The initial value returned by _gtk_css_style_property_get_initial_value()
442 * will be of this type.
444 * Returns: the #GType used for specified values.
447 _gtk_css_style_property_get_specified_type (GtkCssStyleProperty *property)
449 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), G_TYPE_NONE);
451 return G_VALUE_TYPE (&property->initial_value);
455 _gtk_css_style_property_is_specified_type (GtkCssStyleProperty *property,
458 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
460 /* If it's our specified type, of course it's valid */
461 if (type == G_VALUE_TYPE (&property->initial_value))
464 /* The special values 'inherit' and 'initial' are always valid */
465 if (type == GTK_TYPE_CSS_SPECIAL_VALUE)
468 /* XXX: Someone needs to fix that legacy */
469 if (G_VALUE_TYPE (&property->initial_value) == CAIRO_GOBJECT_TYPE_PATTERN &&
470 type == GTK_TYPE_GRADIENT)
477 * _gtk_css_style_property_compute_value:
478 * @property: the property
479 * @computed: (out): an uninitialized value to be filled with the result
480 * @context: the context to use for resolving
481 * @specified: the value to compute from
483 * Converts the @specified value into the @computed value using the
484 * information in @context. This step is explained in detail in
485 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
486 * the CSS documentation</ulink>.
489 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
491 GtkStyleContext *context,
492 const GValue *specified)
494 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
495 g_return_if_fail (computed != NULL && !G_IS_VALUE (computed));
496 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
497 g_return_if_fail (G_IS_VALUE (specified));
499 g_value_init (computed, _gtk_css_style_property_get_computed_type (property));
501 property->compute_value (property, computed, context, specified);
505 * _gtk_css_style_property_print_value:
506 * @property: the property
507 * @value: the value to print
508 * @string: the string to print to
510 * Prints @value to the given @string in CSS format. The @value must be a
511 * valid specified value as parsed using the parse functions or as assigned
512 * via _gtk_style_property_assign().
515 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
519 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
520 g_return_if_fail (value != NULL);
521 g_return_if_fail (string != NULL);
523 if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
525 GEnumClass *enum_class;
526 GEnumValue *enum_value;
528 enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
529 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
531 g_string_append (string, enum_value->value_nick);
533 g_type_class_unref (enum_class);
536 property->print_value (property, value, string);