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 "gtkcssshorthandpropertyprivate.h"
25 #include <cairo-gobject.h>
28 #include "gtkborderimageprivate.h"
29 #include "gtkcssstylefuncsprivate.h"
30 #include "gtkcsstypesprivate.h"
32 /* this is in case round() is not provided by the compiler,
33 * such as in the case of C89 compilers, like MSVC
35 #include "fallback-c89.c"
40 border_image_value_parse (GtkCssParser *parser,
44 GValue temp = G_VALUE_INIT;
45 cairo_pattern_t *pattern = NULL;
46 gconstpointer *boxed = NULL;
48 GtkBorder slice, *width = NULL, *parsed_slice;
49 GtkCssBorderImageRepeat repeat, *parsed_repeat;
50 gboolean retval = FALSE;
51 GtkBorderImage *image = NULL;
53 if (_gtk_css_parser_try (parser, "none", TRUE))
56 g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);
58 if (!_gtk_css_style_parse_value (&temp, parser, base))
61 boxed_type = G_VALUE_TYPE (&temp);
62 if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
63 boxed = g_value_dup_boxed (&temp);
65 pattern = g_value_dup_boxed (&temp);
67 g_value_unset (&temp);
68 g_value_init (&temp, GTK_TYPE_BORDER);
70 if (!_gtk_css_style_parse_value (&temp, parser, base))
73 parsed_slice = g_value_get_boxed (&temp);
74 slice = *parsed_slice;
76 if (_gtk_css_parser_try (parser, "/", TRUE))
78 g_value_unset (&temp);
79 g_value_init (&temp, GTK_TYPE_BORDER);
81 if (!_gtk_css_style_parse_value (&temp, parser, base))
84 width = g_value_dup_boxed (&temp);
87 g_value_unset (&temp);
88 g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
90 if (!_gtk_css_style_parse_value (&temp, parser, base))
93 parsed_repeat = g_value_get_boxed (&temp);
94 repeat = *parsed_repeat;
96 g_value_unset (&temp);
99 image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
100 else if (pattern != NULL)
101 image = _gtk_border_image_new (pattern, &slice, width, &repeat);
106 g_value_take_boxed (value, image);
111 cairo_pattern_destroy (pattern);
114 g_boxed_free (boxed_type, boxed);
117 gtk_border_free (width);
123 border_radius_value_parse (GtkCssParser *parser,
127 GtkCssBorderRadius border;
129 if (!_gtk_css_parser_try_double (parser, &border.top_left.horizontal))
131 _gtk_css_parser_error (parser, "Expected a number");
134 else if (border.top_left.horizontal < 0)
137 if (_gtk_css_parser_try_double (parser, &border.top_right.horizontal))
139 if (border.top_right.horizontal < 0)
141 if (_gtk_css_parser_try_double (parser, &border.bottom_right.horizontal))
143 if (border.bottom_right.horizontal < 0)
145 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.horizontal))
146 border.bottom_left.horizontal = border.top_right.horizontal;
147 else if (border.bottom_left.horizontal < 0)
152 border.bottom_right.horizontal = border.top_left.horizontal;
153 border.bottom_left.horizontal = border.top_right.horizontal;
158 border.top_right.horizontal = border.top_left.horizontal;
159 border.bottom_right.horizontal = border.top_left.horizontal;
160 border.bottom_left.horizontal = border.top_left.horizontal;
163 if (_gtk_css_parser_try (parser, "/", TRUE))
165 if (!_gtk_css_parser_try_double (parser, &border.top_left.vertical))
167 _gtk_css_parser_error (parser, "Expected a number");
170 else if (border.top_left.vertical < 0)
173 if (_gtk_css_parser_try_double (parser, &border.top_right.vertical))
175 if (border.top_right.vertical < 0)
177 if (_gtk_css_parser_try_double (parser, &border.bottom_right.vertical))
179 if (border.bottom_right.vertical < 0)
181 if (!_gtk_css_parser_try_double (parser, &border.bottom_left.vertical))
182 border.bottom_left.vertical = border.top_right.vertical;
183 else if (border.bottom_left.vertical < 0)
188 border.bottom_right.vertical = border.top_left.vertical;
189 border.bottom_left.vertical = border.top_right.vertical;
194 border.top_right.vertical = border.top_left.vertical;
195 border.bottom_right.vertical = border.top_left.vertical;
196 border.bottom_left.vertical = border.top_left.vertical;
201 border.top_left.vertical = border.top_left.horizontal;
202 border.top_right.vertical = border.top_right.horizontal;
203 border.bottom_right.vertical = border.bottom_right.horizontal;
204 border.bottom_left.vertical = border.bottom_left.horizontal;
207 /* border-radius is an int property for backwards-compat reasons */
208 g_value_unset (value);
209 g_value_init (value, GTK_TYPE_CSS_BORDER_RADIUS);
210 g_value_set_boxed (value, &border);
215 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
220 border_color_shorthand_value_parse (GtkCssParser *parser,
224 GtkSymbolicColor *symbolic;
227 array = g_ptr_array_new_with_free_func ((GDestroyNotify) gtk_symbolic_color_unref);
231 if (_gtk_css_parser_try (parser, "transparent", TRUE))
233 GdkRGBA transparent = { 0, 0, 0, 0 };
235 symbolic = gtk_symbolic_color_new_literal (&transparent);
239 symbolic = _gtk_css_parser_read_symbolic_color (parser);
241 if (symbolic == NULL)
245 g_ptr_array_add (array, symbolic);
247 while (array->len < 4 &&
248 !_gtk_css_parser_is_eof (parser) &&
249 !_gtk_css_parser_begins_with (parser, ';') &&
250 !_gtk_css_parser_begins_with (parser, '}'));
255 g_assert_not_reached ();
258 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
261 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 0)));
264 g_ptr_array_add (array, gtk_symbolic_color_ref (g_ptr_array_index (array, 1)));
270 g_value_unset (value);
271 g_value_init (value, G_TYPE_PTR_ARRAY);
272 g_value_take_boxed (value, array);
280 unpack_border (const GValue *value,
287 GParameter *parameter = g_new0 (GParameter, 4);
288 GtkBorder *border = g_value_get_boxed (value);
290 parameter[0].name = top;
291 g_value_init (¶meter[0].value, G_TYPE_INT);
292 g_value_set_int (¶meter[0].value, border->top);
293 parameter[1].name = left;
294 g_value_init (¶meter[1].value, G_TYPE_INT);
295 g_value_set_int (¶meter[1].value, border->left);
296 parameter[2].name = bottom;
297 g_value_init (¶meter[2].value, G_TYPE_INT);
298 g_value_set_int (¶meter[2].value, border->bottom);
299 parameter[3].name = right;
300 g_value_init (¶meter[3].value, G_TYPE_INT);
301 g_value_set_int (¶meter[3].value, border->right);
308 pack_border (GValue *value,
309 GtkStyleProperties *props,
319 gtk_style_properties_get (props,
332 g_value_set_boxed (value, &border);
336 unpack_border_width (const GValue *value,
339 return unpack_border (value, n_params,
340 "border-top-width", "border-left-width",
341 "border-bottom-width", "border-right-width");
345 pack_border_width (GValue *value,
346 GtkStyleProperties *props,
348 GtkStylePropertyContext *context)
350 pack_border (value, props, state,
351 "border-top-width", "border-left-width",
352 "border-bottom-width", "border-right-width");
356 unpack_padding (const GValue *value,
359 return unpack_border (value, n_params,
360 "padding-top", "padding-left",
361 "padding-bottom", "padding-right");
365 pack_padding (GValue *value,
366 GtkStyleProperties *props,
368 GtkStylePropertyContext *context)
370 pack_border (value, props, state,
371 "padding-top", "padding-left",
372 "padding-bottom", "padding-right");
376 unpack_margin (const GValue *value,
379 return unpack_border (value, n_params,
380 "margin-top", "margin-left",
381 "margin-bottom", "margin-right");
385 pack_margin (GValue *value,
386 GtkStyleProperties *props,
388 GtkStylePropertyContext *context)
390 pack_border (value, props, state,
391 "margin-top", "margin-left",
392 "margin-bottom", "margin-right");
396 unpack_border_radius (const GValue *value,
399 GParameter *parameter = g_new0 (GParameter, 4);
400 GtkCssBorderRadius *border;
402 if (G_VALUE_HOLDS_BOXED (value))
403 border = g_value_get_boxed (value);
407 parameter[0].name = "border-top-left-radius";
408 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
409 parameter[1].name = "border-top-right-radius";
410 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
411 parameter[2].name = "border-bottom-right-radius";
412 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
413 parameter[3].name = "border-bottom-left-radius";
414 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
417 g_value_set_boxed (¶meter[0].value, &border->top_left);
418 g_value_set_boxed (¶meter[1].value, &border->top_right);
419 g_value_set_boxed (¶meter[2].value, &border->bottom_right);
420 g_value_set_boxed (¶meter[3].value, &border->bottom_left);
428 pack_border_radius (GValue *value,
429 GtkStyleProperties *props,
431 GtkStylePropertyContext *context)
433 GtkCssBorderCornerRadius *top_left;
435 /* NB: We are an int property, so we have to resolve to an int here.
436 * So we just resolve to an int. We pick one and stick to it.
437 * Lesson learned: Don't query border-radius shorthand, query the
438 * real properties instead. */
439 gtk_style_properties_get (props,
441 "border-top-left-radius", &top_left,
445 g_value_set_int (value, top_left->horizontal);
451 unpack_font_description (const GValue *value,
454 GParameter *parameter = g_new0 (GParameter, 5);
455 PangoFontDescription *description;
459 /* For backwards compat, we only unpack values that are indeed set.
460 * For strict CSS conformance we need to unpack all of them.
461 * Note that we do set all of them in the parse function, so it
462 * will not have effects when parsing CSS files. It will though
463 * for custom style providers.
466 description = g_value_get_boxed (value);
470 mask = pango_font_description_get_set_fields (description);
474 if (mask & PANGO_FONT_MASK_FAMILY)
476 GPtrArray *strv = g_ptr_array_new ();
478 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
479 g_ptr_array_add (strv, NULL);
480 parameter[n].name = "font-family";
481 g_value_init (¶meter[n].value, G_TYPE_STRV);
482 g_value_take_boxed (¶meter[n].value,
483 g_ptr_array_free (strv, FALSE));
487 if (mask & PANGO_FONT_MASK_STYLE)
489 parameter[n].name = "font-style";
490 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
491 g_value_set_enum (¶meter[n].value,
492 pango_font_description_get_style (description));
496 if (mask & PANGO_FONT_MASK_VARIANT)
498 parameter[n].name = "font-variant";
499 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
500 g_value_set_enum (¶meter[n].value,
501 pango_font_description_get_variant (description));
505 if (mask & PANGO_FONT_MASK_WEIGHT)
507 parameter[n].name = "font-weight";
508 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
509 g_value_set_enum (¶meter[n].value,
510 pango_font_description_get_weight (description));
514 if (mask & PANGO_FONT_MASK_SIZE)
516 parameter[n].name = "font-size";
517 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
518 g_value_set_double (¶meter[n].value,
519 (double) pango_font_description_get_size (description) / PANGO_SCALE);
529 pack_font_description (GValue *value,
530 GtkStyleProperties *props,
532 GtkStylePropertyContext *context)
534 PangoFontDescription *description;
537 PangoVariant variant;
541 gtk_style_properties_get (props,
543 "font-family", &families,
544 "font-style", &style,
545 "font-variant", &variant,
546 "font-weight", &weight,
550 description = pango_font_description_new ();
551 /* xxx: Can we set all the families here somehow? */
553 pango_font_description_set_family (description, families[0]);
554 pango_font_description_set_size (description, round (size * PANGO_SCALE));
555 pango_font_description_set_style (description, style);
556 pango_font_description_set_variant (description, variant);
557 pango_font_description_set_weight (description, weight);
559 g_strfreev (families);
561 g_value_take_boxed (value, description);
565 unpack_border_color (const GValue *value,
568 GParameter *parameter = g_new0 (GParameter, 4);
571 type = G_VALUE_TYPE (value);
572 if (type == G_TYPE_PTR_ARRAY)
573 type = GTK_TYPE_SYMBOLIC_COLOR;
575 parameter[0].name = "border-top-color";
576 g_value_init (¶meter[0].value, type);
577 parameter[1].name = "border-right-color";
578 g_value_init (¶meter[1].value, type);
579 parameter[2].name = "border-bottom-color";
580 g_value_init (¶meter[2].value, type);
581 parameter[3].name = "border-left-color";
582 g_value_init (¶meter[3].value, type);
584 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
586 GPtrArray *array = g_value_get_boxed (value);
589 for (i = 0; i < 4; i++)
590 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
594 /* can be RGBA or symbolic color */
595 gpointer p = g_value_get_boxed (value);
597 g_value_set_boxed (¶meter[0].value, p);
598 g_value_set_boxed (¶meter[1].value, p);
599 g_value_set_boxed (¶meter[2].value, p);
600 g_value_set_boxed (¶meter[3].value, p);
608 pack_border_color (GValue *value,
609 GtkStyleProperties *props,
611 GtkStylePropertyContext *context)
613 /* NB: We are a color property, so we have to resolve to a color here.
614 * So we just resolve to a color. We pick one and stick to it.
615 * Lesson learned: Don't query border-color shorthand, query the
616 * real properties instead. */
617 g_value_unset (value);
618 gtk_style_properties_get_property (props, "border-top-color", state, value);
622 _gtk_css_shorthand_property_register (const char *name,
624 const char **subproperties,
625 GtkCssShorthandPropertyParseFunc parse_func,
626 GtkStyleUnpackFunc unpack_func,
627 GtkStylePackFunc pack_func,
628 GtkStyleParseFunc old_parse_func)
630 GtkStyleProperty *node;
632 g_return_if_fail (pack_func != NULL);
633 g_return_if_fail (unpack_func != NULL);
635 node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
637 "value-type", value_type,
638 "subproperties", subproperties,
642 GTK_CSS_SHORTHAND_PROPERTY (node)->parse = parse_func;
643 node->pack_func = pack_func;
644 node->unpack_func = unpack_func;
645 node->parse_func = old_parse_func;
649 _gtk_css_shorthand_property_init_properties (void)
651 const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
652 const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
653 const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
654 const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
655 const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
656 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
657 const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
658 const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
660 _gtk_css_shorthand_property_register ("font",
661 PANGO_TYPE_FONT_DESCRIPTION,
664 unpack_font_description,
665 pack_font_description,
667 _gtk_css_shorthand_property_register ("margin",
669 margin_subproperties,
674 _gtk_css_shorthand_property_register ("padding",
676 padding_subproperties,
681 _gtk_css_shorthand_property_register ("border-width",
683 border_width_subproperties,
688 _gtk_css_shorthand_property_register ("border-radius",
690 border_radius_subproperties,
692 unpack_border_radius,
694 border_radius_value_parse);
695 _gtk_css_shorthand_property_register ("border-color",
697 border_color_subproperties,
701 border_color_shorthand_value_parse);
702 _gtk_css_shorthand_property_register ("border-image",
703 GTK_TYPE_BORDER_IMAGE,
704 border_image_subproperties,
706 _gtk_border_image_unpack,
707 _gtk_border_image_pack,
708 border_image_value_parse);