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 value_is_done_parsing (GtkCssParser *parser)
42 return _gtk_css_parser_is_eof (parser) ||
43 _gtk_css_parser_begins_with (parser, ';') ||
44 _gtk_css_parser_begins_with (parser, '}');
48 parse_border (GtkCssShorthandProperty *shorthand,
53 GValue temp = G_VALUE_INIT;
56 g_value_init (&temp, GTK_TYPE_BORDER);
57 if (!_gtk_css_style_parse_value (&temp, parser, base))
59 g_value_unset (&temp);
63 border = g_value_get_boxed (&temp);
65 g_value_init (&values[0], G_TYPE_INT);
66 g_value_init (&values[1], G_TYPE_INT);
67 g_value_init (&values[2], G_TYPE_INT);
68 g_value_init (&values[3], G_TYPE_INT);
69 g_value_set_int (&values[0], border->top);
70 g_value_set_int (&values[1], border->right);
71 g_value_set_int (&values[2], border->bottom);
72 g_value_set_int (&values[3], border->left);
74 g_value_unset (&temp);
80 parse_border_radius (GtkCssShorthandProperty *shorthand,
85 GtkCssBorderCornerRadius borders[4];
88 for (i = 0; i < G_N_ELEMENTS (borders); i++)
90 if (!_gtk_css_parser_try_double (parser, &borders[i].horizontal))
92 if (borders[i].horizontal < 0)
94 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
101 _gtk_css_parser_error (parser, "Expected a number");
105 /* The magic (i - 1) >> 1 below makes it take the correct value
106 * according to spec. Feel free to check the 4 cases */
107 for (; i < G_N_ELEMENTS (borders); i++)
108 borders[i].horizontal = borders[(i - 1) >> 1].horizontal;
110 if (_gtk_css_parser_try (parser, "/", TRUE))
112 for (i = 0; i < G_N_ELEMENTS (borders); i++)
114 if (!_gtk_css_parser_try_double (parser, &borders[i].vertical))
116 if (borders[i].vertical < 0)
118 _gtk_css_parser_error (parser, "Border radius values cannot be negative");
125 _gtk_css_parser_error (parser, "Expected a number");
129 for (; i < G_N_ELEMENTS (borders); i++)
130 borders[i].vertical = borders[(i - 1) >> 1].vertical;
135 for (i = 0; i < G_N_ELEMENTS (borders); i++)
136 borders[i].vertical = borders[i].horizontal;
139 for (i = 0; i < G_N_ELEMENTS (borders); i++)
141 g_value_init (&values[i], GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
142 g_value_set_boxed (&values[i], &borders[i]);
149 parse_border_color (GtkCssShorthandProperty *shorthand,
151 GtkCssParser *parser,
154 GtkSymbolicColor *symbolic;
157 for (i = 0; i < 4; i++)
159 symbolic = _gtk_css_parser_read_symbolic_color (parser);
160 if (symbolic == NULL)
163 g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR);
164 g_value_set_boxed (&values[i], symbolic);
166 if (value_is_done_parsing (parser))
170 for (i++; i < 4; i++)
172 g_value_init (&values[i], GTK_TYPE_SYMBOLIC_COLOR);
173 g_value_copy (&values[(i - 1) >> 1], &values[i]);
180 parse_border_image (GtkCssShorthandProperty *shorthand,
182 GtkCssParser *parser,
185 g_value_init (&values[0], CAIRO_GOBJECT_TYPE_PATTERN);
186 if (!_gtk_css_style_parse_value (&values[0], parser, base))
189 if (value_is_done_parsing (parser))
192 g_value_init (&values[1], GTK_TYPE_BORDER);
193 if (!_gtk_css_style_parse_value (&values[1], parser, base))
196 if (_gtk_css_parser_try (parser, "/", TRUE))
198 g_value_init (&values[2], GTK_TYPE_BORDER);
199 if (!_gtk_css_style_parse_value (&values[2], parser, base))
203 if (value_is_done_parsing (parser))
206 g_value_init (&values[3], GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
207 if (!_gtk_css_style_parse_value (&values[3], parser, base))
214 parse_font (GtkCssShorthandProperty *shorthand,
216 GtkCssParser *parser,
219 PangoFontDescription *desc;
223 str = _gtk_css_parser_read_value (parser);
227 desc = pango_font_description_from_string (str);
230 mask = pango_font_description_get_set_fields (desc);
232 if (mask & PANGO_FONT_MASK_FAMILY)
234 GPtrArray *strv = g_ptr_array_new ();
236 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (desc)));
237 g_ptr_array_add (strv, NULL);
238 g_value_init (&values[0], G_TYPE_STRV);
239 g_value_take_boxed (&values[0], g_ptr_array_free (strv, FALSE));
241 if (mask & PANGO_FONT_MASK_STYLE)
243 g_value_init (&values[1], PANGO_TYPE_STYLE);
244 g_value_set_enum (&values[1], pango_font_description_get_style (desc));
246 if (mask & PANGO_FONT_MASK_VARIANT)
248 g_value_init (&values[2], PANGO_TYPE_VARIANT);
249 g_value_set_enum (&values[2], pango_font_description_get_variant (desc));
251 if (mask & PANGO_FONT_MASK_WEIGHT)
253 g_value_init (&values[3], PANGO_TYPE_WEIGHT);
254 g_value_set_enum (&values[3], pango_font_description_get_weight (desc));
256 if (mask & PANGO_FONT_MASK_SIZE)
258 g_value_init (&values[4], G_TYPE_DOUBLE);
259 g_value_set_double (&values[4],
260 (double) pango_font_description_get_size (desc) / PANGO_SCALE);
263 pango_font_description_free (desc);
271 unpack_border (const GValue *value,
278 GParameter *parameter = g_new0 (GParameter, 4);
279 GtkBorder *border = g_value_get_boxed (value);
281 parameter[0].name = top;
282 g_value_init (¶meter[0].value, G_TYPE_INT);
283 g_value_set_int (¶meter[0].value, border->top);
284 parameter[1].name = left;
285 g_value_init (¶meter[1].value, G_TYPE_INT);
286 g_value_set_int (¶meter[1].value, border->left);
287 parameter[2].name = bottom;
288 g_value_init (¶meter[2].value, G_TYPE_INT);
289 g_value_set_int (¶meter[2].value, border->bottom);
290 parameter[3].name = right;
291 g_value_init (¶meter[3].value, G_TYPE_INT);
292 g_value_set_int (¶meter[3].value, border->right);
299 pack_border (GValue *value,
300 GtkStyleProperties *props,
310 gtk_style_properties_get (props,
323 g_value_set_boxed (value, &border);
327 unpack_border_width (const GValue *value,
330 return unpack_border (value, n_params,
331 "border-top-width", "border-left-width",
332 "border-bottom-width", "border-right-width");
336 pack_border_width (GValue *value,
337 GtkStyleProperties *props,
339 GtkStylePropertyContext *context)
341 pack_border (value, props, state,
342 "border-top-width", "border-left-width",
343 "border-bottom-width", "border-right-width");
347 unpack_padding (const GValue *value,
350 return unpack_border (value, n_params,
351 "padding-top", "padding-left",
352 "padding-bottom", "padding-right");
356 pack_padding (GValue *value,
357 GtkStyleProperties *props,
359 GtkStylePropertyContext *context)
361 pack_border (value, props, state,
362 "padding-top", "padding-left",
363 "padding-bottom", "padding-right");
367 unpack_margin (const GValue *value,
370 return unpack_border (value, n_params,
371 "margin-top", "margin-left",
372 "margin-bottom", "margin-right");
376 pack_margin (GValue *value,
377 GtkStyleProperties *props,
379 GtkStylePropertyContext *context)
381 pack_border (value, props, state,
382 "margin-top", "margin-left",
383 "margin-bottom", "margin-right");
387 unpack_border_radius (const GValue *value,
390 GParameter *parameter = g_new0 (GParameter, 4);
391 GtkCssBorderCornerRadius border;
393 border.horizontal = border.vertical = g_value_get_int (value);
395 parameter[0].name = "border-top-left-radius";
396 g_value_init (¶meter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
397 g_value_set_boxed (¶meter[0].value, &border);
398 parameter[1].name = "border-top-right-radius";
399 g_value_init (¶meter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
400 g_value_set_boxed (¶meter[1].value, &border);
401 parameter[2].name = "border-bottom-right-radius";
402 g_value_init (¶meter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
403 g_value_set_boxed (¶meter[2].value, &border);
404 parameter[3].name = "border-bottom-left-radius";
405 g_value_init (¶meter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
406 g_value_set_boxed (¶meter[3].value, &border);
413 pack_border_radius (GValue *value,
414 GtkStyleProperties *props,
416 GtkStylePropertyContext *context)
418 GtkCssBorderCornerRadius *top_left;
420 /* NB: We are an int property, so we have to resolve to an int here.
421 * So we just resolve to an int. We pick one and stick to it.
422 * Lesson learned: Don't query border-radius shorthand, query the
423 * real properties instead. */
424 gtk_style_properties_get (props,
426 "border-top-left-radius", &top_left,
430 g_value_set_int (value, top_left->horizontal);
436 unpack_font_description (const GValue *value,
439 GParameter *parameter = g_new0 (GParameter, 5);
440 PangoFontDescription *description;
444 /* For backwards compat, we only unpack values that are indeed set.
445 * For strict CSS conformance we need to unpack all of them.
446 * Note that we do set all of them in the parse function, so it
447 * will not have effects when parsing CSS files. It will though
448 * for custom style providers.
451 description = g_value_get_boxed (value);
455 mask = pango_font_description_get_set_fields (description);
459 if (mask & PANGO_FONT_MASK_FAMILY)
461 GPtrArray *strv = g_ptr_array_new ();
463 g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
464 g_ptr_array_add (strv, NULL);
465 parameter[n].name = "font-family";
466 g_value_init (¶meter[n].value, G_TYPE_STRV);
467 g_value_take_boxed (¶meter[n].value,
468 g_ptr_array_free (strv, FALSE));
472 if (mask & PANGO_FONT_MASK_STYLE)
474 parameter[n].name = "font-style";
475 g_value_init (¶meter[n].value, PANGO_TYPE_STYLE);
476 g_value_set_enum (¶meter[n].value,
477 pango_font_description_get_style (description));
481 if (mask & PANGO_FONT_MASK_VARIANT)
483 parameter[n].name = "font-variant";
484 g_value_init (¶meter[n].value, PANGO_TYPE_VARIANT);
485 g_value_set_enum (¶meter[n].value,
486 pango_font_description_get_variant (description));
490 if (mask & PANGO_FONT_MASK_WEIGHT)
492 parameter[n].name = "font-weight";
493 g_value_init (¶meter[n].value, PANGO_TYPE_WEIGHT);
494 g_value_set_enum (¶meter[n].value,
495 pango_font_description_get_weight (description));
499 if (mask & PANGO_FONT_MASK_SIZE)
501 parameter[n].name = "font-size";
502 g_value_init (¶meter[n].value, G_TYPE_DOUBLE);
503 g_value_set_double (¶meter[n].value,
504 (double) pango_font_description_get_size (description) / PANGO_SCALE);
514 pack_font_description (GValue *value,
515 GtkStyleProperties *props,
517 GtkStylePropertyContext *context)
519 PangoFontDescription *description;
522 PangoVariant variant;
526 gtk_style_properties_get (props,
528 "font-family", &families,
529 "font-style", &style,
530 "font-variant", &variant,
531 "font-weight", &weight,
535 description = pango_font_description_new ();
536 /* xxx: Can we set all the families here somehow? */
538 pango_font_description_set_family (description, families[0]);
539 pango_font_description_set_size (description, round (size * PANGO_SCALE));
540 pango_font_description_set_style (description, style);
541 pango_font_description_set_variant (description, variant);
542 pango_font_description_set_weight (description, weight);
544 g_strfreev (families);
546 g_value_take_boxed (value, description);
550 unpack_border_color (const GValue *value,
553 GParameter *parameter = g_new0 (GParameter, 4);
556 type = G_VALUE_TYPE (value);
557 if (type == G_TYPE_PTR_ARRAY)
558 type = GTK_TYPE_SYMBOLIC_COLOR;
560 parameter[0].name = "border-top-color";
561 g_value_init (¶meter[0].value, type);
562 parameter[1].name = "border-right-color";
563 g_value_init (¶meter[1].value, type);
564 parameter[2].name = "border-bottom-color";
565 g_value_init (¶meter[2].value, type);
566 parameter[3].name = "border-left-color";
567 g_value_init (¶meter[3].value, type);
569 if (G_VALUE_TYPE (value) == G_TYPE_PTR_ARRAY)
571 GPtrArray *array = g_value_get_boxed (value);
574 for (i = 0; i < 4; i++)
575 g_value_set_boxed (¶meter[i].value, g_ptr_array_index (array, i));
579 /* can be RGBA or symbolic color */
580 gpointer p = g_value_get_boxed (value);
582 g_value_set_boxed (¶meter[0].value, p);
583 g_value_set_boxed (¶meter[1].value, p);
584 g_value_set_boxed (¶meter[2].value, p);
585 g_value_set_boxed (¶meter[3].value, p);
593 pack_border_color (GValue *value,
594 GtkStyleProperties *props,
596 GtkStylePropertyContext *context)
598 /* NB: We are a color property, so we have to resolve to a color here.
599 * So we just resolve to a color. We pick one and stick to it.
600 * Lesson learned: Don't query border-color shorthand, query the
601 * real properties instead. */
602 g_value_unset (value);
603 gtk_style_properties_get_property (props, "border-top-color", state, value);
607 _gtk_css_shorthand_property_register (const char *name,
609 const char **subproperties,
610 GtkCssShorthandPropertyParseFunc parse_func,
611 GtkStyleUnpackFunc unpack_func,
612 GtkStylePackFunc pack_func)
614 GtkStyleProperty *node;
616 g_return_if_fail (pack_func != NULL);
617 g_return_if_fail (unpack_func != NULL);
619 node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
621 "value-type", value_type,
622 "subproperties", subproperties,
625 GTK_CSS_SHORTHAND_PROPERTY (node)->parse = parse_func;
626 node->pack_func = pack_func;
627 node->unpack_func = unpack_func;
631 _gtk_css_shorthand_property_init_properties (void)
633 /* The order is important here, be careful when changing it */
634 const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
635 const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
636 const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
637 const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
638 const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
639 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
640 const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
641 const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
643 _gtk_css_shorthand_property_register ("font",
644 PANGO_TYPE_FONT_DESCRIPTION,
647 unpack_font_description,
648 pack_font_description);
649 _gtk_css_shorthand_property_register ("margin",
651 margin_subproperties,
655 _gtk_css_shorthand_property_register ("padding",
657 padding_subproperties,
661 _gtk_css_shorthand_property_register ("border-width",
663 border_width_subproperties,
667 _gtk_css_shorthand_property_register ("border-radius",
669 border_radius_subproperties,
671 unpack_border_radius,
673 _gtk_css_shorthand_property_register ("border-color",
675 border_color_subproperties,
679 _gtk_css_shorthand_property_register ("border-image",
680 GTK_TYPE_BORDER_IMAGE,
681 border_image_subproperties,
683 _gtk_border_image_unpack,
684 _gtk_border_image_pack);