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"
31 #include "gtkcssimagegradientprivate.h"
32 #include "gtkcssimageprivate.h"
41 G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)
44 gtk_css_style_property_constructed (GObject *object)
46 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
47 GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);
49 property->id = klass->style_properties->len;
50 g_ptr_array_add (klass->style_properties, property);
52 G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
56 gtk_css_style_property_set_property (GObject *object,
61 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
62 const GValue *initial;
67 property->inherit = g_value_get_boolean (value);
70 initial = g_value_get_boxed (value);
72 g_value_init (&property->initial_value, G_VALUE_TYPE (initial));
73 g_value_copy (initial, &property->initial_value);
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
82 gtk_css_style_property_get_property (GObject *object,
87 GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
92 g_value_set_boolean (value, property->id);
95 g_value_set_boolean (value, property->inherit);
98 g_value_set_boxed (value, &property->initial_value);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107 _gtk_css_style_property_assign (GtkStyleProperty *property,
108 GtkStyleProperties *props,
112 _gtk_style_properties_set_property_by_property (props,
113 GTK_CSS_STYLE_PROPERTY (property),
119 _gtk_style_property_default_value (GtkStyleProperty *property,
120 GtkStyleProperties *properties,
124 g_value_copy (_gtk_css_style_property_get_initial_value (GTK_CSS_STYLE_PROPERTY (property)), value);
128 _gtk_css_style_property_query (GtkStyleProperty *property,
129 GtkStyleProperties *props,
135 val = _gtk_style_properties_peek_property (props, GTK_CSS_STYLE_PROPERTY (property), state);
138 /* Somebody make this a vfunc */
139 if (G_VALUE_TYPE (val) == GTK_TYPE_CSS_IMAGE)
141 GtkCssImage *image = g_value_get_object (val);
142 cairo_pattern_t *pattern;
143 cairo_surface_t *surface;
144 cairo_matrix_t matrix;
147 g_value_set_boxed (value, NULL);
148 else if (GTK_IS_CSS_IMAGE_GRADIENT (image))
149 g_value_set_boxed (value, GTK_CSS_IMAGE_GRADIENT (image)->pattern);
152 double width, height;
154 /* the 100, 100 is rather random */
155 _gtk_css_image_get_concrete_size (image, 0, 0, 100, 100, &width, &height);
156 surface = _gtk_css_image_get_surface (image, NULL, width, height);
157 pattern = cairo_pattern_create_for_surface (surface);
158 cairo_matrix_init_scale (&matrix, width, height);
159 cairo_pattern_set_matrix (pattern, &matrix);
160 cairo_surface_destroy (surface);
161 g_value_take_boxed (value, pattern);
165 g_value_copy (val, value);
168 _gtk_style_property_default_value (property, props, state, value);
172 gtk_css_style_property_parse_value (GtkStyleProperty *property,
174 GtkCssParser *parser,
177 GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
179 if (_gtk_css_parser_try (parser, "initial", TRUE))
181 /* the initial value can be explicitly specified with the
182 * ‘initial’ keyword which all properties accept.
184 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
185 g_value_set_enum (value, GTK_CSS_INITIAL);
188 else if (_gtk_css_parser_try (parser, "inherit", TRUE))
190 /* All properties accept the ‘inherit’ value which
191 * explicitly specifies that the value will be determined
192 * by inheritance. The ‘inherit’ value can be used to
193 * strengthen inherited values in the cascade, and it can
194 * also be used on properties that are not normally inherited.
196 g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
197 g_value_set_enum (value, GTK_CSS_INHERIT);
201 g_value_init (value, _gtk_style_property_get_value_type (property));
202 if (!(* style_property->parse_value) (style_property, value, parser, base))
204 g_value_unset (value);
212 _gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
214 GObjectClass *object_class = G_OBJECT_CLASS (klass);
215 GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
217 object_class->constructed = gtk_css_style_property_constructed;
218 object_class->set_property = gtk_css_style_property_set_property;
219 object_class->get_property = gtk_css_style_property_get_property;
221 g_object_class_install_property (object_class,
223 g_param_spec_uint ("id",
225 P_("The numeric id for quick access"),
228 g_object_class_install_property (object_class,
230 g_param_spec_boolean ("inherit",
232 P_("Set if the value is inherited by default"),
234 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
235 g_object_class_install_property (object_class,
237 g_param_spec_boxed ("initial-value",
239 P_("The initial specified value used for this property"),
241 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
243 property_class->assign = _gtk_css_style_property_assign;
244 property_class->query = _gtk_css_style_property_query;
245 property_class->parse_value = gtk_css_style_property_parse_value;
247 klass->style_properties = g_ptr_array_new ();
251 gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
253 GtkCssParser *parser,
256 return _gtk_css_style_parse_value (value, parser, base);
260 gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
264 _gtk_css_style_print_value (value, string);
268 gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
270 GtkStyleContext *context,
271 const GValue *specified)
273 g_value_init (computed, _gtk_style_property_get_value_type (GTK_STYLE_PROPERTY (property)));
274 _gtk_css_style_compute_value (computed, context, specified);
278 _gtk_css_style_property_init (GtkCssStyleProperty *property)
280 property->parse_value = gtk_css_style_property_real_parse_value;
281 property->print_value = gtk_css_style_property_real_print_value;
282 property->compute_value = gtk_css_style_property_real_compute_value;
286 * _gtk_css_style_property_get_n_properties:
288 * Gets the number of style properties. This number can increase when new
289 * theme engines are loaded. Shorthand properties are not included here.
291 * Returns: The number of style properties.
294 _gtk_css_style_property_get_n_properties (void)
296 GtkCssStylePropertyClass *klass;
298 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
300 return klass->style_properties->len;
304 * _gtk_css_style_property_lookup_by_id:
305 * @id: the id of the property
307 * Gets the style property with the given id. All style properties (but not
308 * shorthand properties) are indexable by id so that it's easy to use arrays
309 * when doing style lookups.
311 * Returns: (transfer none): The style property with the given id
313 GtkCssStyleProperty *
314 _gtk_css_style_property_lookup_by_id (guint id)
316 GtkCssStylePropertyClass *klass;
318 klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
320 return g_ptr_array_index (klass->style_properties, id);
324 * _gtk_css_style_property_is_inherit:
325 * @property: the property
327 * Queries if the given @property is inherited. See
328 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
329 * the CSS documentation</ulink> for an explanation of this concept.
331 * Returns: %TRUE if the property is inherited by default.
334 _gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
336 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
338 return property->inherit;
342 * _gtk_css_style_property_get_id:
343 * @property: the property
345 * Gets the id for the given property. IDs are used to allow using arrays
348 * Returns: The id of the property
351 _gtk_css_style_property_get_id (GtkCssStyleProperty *property)
353 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);
359 * _gtk_css_style_property_get_initial_value:
360 * @property: the property
362 * Queries the initial value of the given @property. See
363 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
364 * the CSS documentation</ulink> for an explanation of this concept.
366 * Returns: a reference to the initial value. The value will never change.
369 _gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
371 g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
373 return &property->initial_value;
377 * _gtk_css_style_property_compute_value:
378 * @property: the property
379 * @computed: (out): an uninitialized value to be filled with the result
380 * @context: the context to use for resolving
381 * @specified: the value to compute from
383 * Converts the @specified value into the @computed value using the
384 * information in @context. This step is explained in detail in
385 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
386 * the CSS documentation</ulink>.
389 _gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
391 GtkStyleContext *context,
392 const GValue *specified)
394 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
395 g_return_if_fail (computed != NULL && !G_IS_VALUE (computed));
396 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
397 g_return_if_fail (G_IS_VALUE (specified));
399 property->compute_value (property, computed, context, specified);
403 * _gtk_css_style_property_print_value:
404 * @property: the property
405 * @value: the value to print
406 * @string: the string to print to
408 * Prints @value to the given @string in CSS format. The @value must be a
409 * valid specified value as parsed using the parse functions or as assigned
410 * via _gtk_style_property_assign().
413 _gtk_css_style_property_print_value (GtkCssStyleProperty *property,
417 g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
418 g_return_if_fail (value != NULL);
419 g_return_if_fail (string != NULL);
421 if (G_VALUE_HOLDS (value, GTK_TYPE_CSS_SPECIAL_VALUE))
423 GEnumClass *enum_class;
424 GEnumValue *enum_value;
426 enum_class = g_type_class_ref (GTK_TYPE_CSS_SPECIAL_VALUE);
427 enum_value = g_enum_get_value (enum_class, g_value_get_enum (value));
429 g_string_append (string, enum_value->value_nick);
431 g_type_class_unref (enum_class);
434 property->print_value (property, value, string);