]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandpropertyimpl.c
cssvalue: Add enum values for the pango enums
[~andy/gtk] / gtk / gtkcssshorthandpropertyimpl.c
1 /*
2  * Copyright © 2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Authors: Benjamin Otte <otte@gnome.org>
18  */
19
20 #include "config.h"
21
22 #include "gtkcssshorthandpropertyprivate.h"
23
24 #include <cairo-gobject.h>
25 #include <math.h>
26
27 #include "gtkcssenumvalueprivate.h"
28 #include "gtkcssimageprivate.h"
29 #include "gtkcssnumbervalueprivate.h"
30 #include "gtkcssstylefuncsprivate.h"
31 #include "gtkcsstypesprivate.h"
32 #include "gtkcssvalueprivate.h"
33 #include "gtkprivatetypebuiltins.h"
34 #include "gtkstylepropertiesprivate.h"
35 #include "gtksymboliccolorprivate.h"
36 #include "gtktypebuiltins.h"
37
38 /* this is in case round() is not provided by the compiler, 
39  * such as in the case of C89 compilers, like MSVC
40  */
41 #include "fallback-c89.c"
42
43 /*** PARSING ***/
44
45 static gboolean
46 value_is_done_parsing (GtkCssParser *parser)
47 {
48   return _gtk_css_parser_is_eof (parser) ||
49          _gtk_css_parser_begins_with (parser, ';') ||
50          _gtk_css_parser_begins_with (parser, '}');
51 }
52
53 static gboolean
54 parse_four_numbers (GtkCssShorthandProperty  *shorthand,
55                     GtkCssValue             **values,
56                     GtkCssParser             *parser,
57                     GtkCssNumberParseFlags    flags)
58 {
59   guint i;
60
61   for (i = 0; i < 4; i++)
62     {
63       if (!_gtk_css_parser_has_number (parser))
64         break;
65
66       values[i] = _gtk_css_number_value_parse (parser, flags);
67       if (values[i] == NULL)
68         return FALSE;
69     }
70
71   if (i == 0)
72     {
73       _gtk_css_parser_error (parser, "Expected a length");
74       return FALSE;
75     }
76
77   for (; i < 4; i++)
78     {
79       values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
80     }
81
82   return TRUE;
83 }
84
85 static gboolean
86 parse_margin (GtkCssShorthandProperty  *shorthand,
87               GtkCssValue             **values,
88               GtkCssParser             *parser,
89               GFile                    *base)
90 {
91   return parse_four_numbers (shorthand,
92                              values,
93                              parser,
94                              GTK_CSS_NUMBER_AS_PIXELS
95                              | GTK_CSS_PARSE_LENGTH);
96 }
97
98 static gboolean
99 parse_padding (GtkCssShorthandProperty  *shorthand,
100                GtkCssValue             **values,
101                GtkCssParser             *parser,
102                GFile                    *base)
103 {
104   return parse_four_numbers (shorthand,
105                              values,
106                              parser,
107                              GTK_CSS_POSITIVE_ONLY
108                              | GTK_CSS_NUMBER_AS_PIXELS
109                              | GTK_CSS_PARSE_LENGTH);
110 }
111
112 static gboolean
113 parse_border_width (GtkCssShorthandProperty  *shorthand,
114                     GtkCssValue             **values,
115                     GtkCssParser             *parser,
116                     GFile                    *base)
117 {
118   return parse_four_numbers (shorthand,
119                              values,
120                              parser,
121                              GTK_CSS_POSITIVE_ONLY
122                              | GTK_CSS_NUMBER_AS_PIXELS
123                              | GTK_CSS_PARSE_LENGTH);
124 }
125
126 static gboolean 
127 parse_border_radius (GtkCssShorthandProperty  *shorthand,
128                      GtkCssValue             **values,
129                      GtkCssParser             *parser,
130                      GFile                    *base)
131 {
132   GtkCssBorderCornerRadius borders[4];
133   guint i;
134
135   for (i = 0; i < G_N_ELEMENTS (borders); i++)
136     {
137       if (!_gtk_css_parser_has_number (parser))
138         break;
139       if (!_gtk_css_parser_read_number (parser,
140                                         &borders[i].horizontal,
141                                         GTK_CSS_POSITIVE_ONLY
142                                         | GTK_CSS_PARSE_PERCENT
143                                         | GTK_CSS_NUMBER_AS_PIXELS
144                                         | GTK_CSS_PARSE_LENGTH))
145         return FALSE;
146     }
147
148   if (i == 0)
149     {
150       _gtk_css_parser_error (parser, "Expected a number");
151       return FALSE;
152     }
153
154   /* The magic (i - 1) >> 1 below makes it take the correct value
155    * according to spec. Feel free to check the 4 cases */
156   for (; i < G_N_ELEMENTS (borders); i++)
157     borders[i].horizontal = borders[(i - 1) >> 1].horizontal;
158
159   if (_gtk_css_parser_try (parser, "/", TRUE))
160     {
161       for (i = 0; i < G_N_ELEMENTS (borders); i++)
162         {
163           if (!_gtk_css_parser_has_number (parser))
164             break;
165           if (!_gtk_css_parser_read_number (parser,
166                                             &borders[i].vertical,
167                                             GTK_CSS_POSITIVE_ONLY
168                                             | GTK_CSS_PARSE_PERCENT
169                                             | GTK_CSS_NUMBER_AS_PIXELS
170                                             | GTK_CSS_PARSE_LENGTH))
171             return FALSE;
172         }
173
174       if (i == 0)
175         {
176           _gtk_css_parser_error (parser, "Expected a number");
177           return FALSE;
178         }
179
180       for (; i < G_N_ELEMENTS (borders); i++)
181         borders[i].vertical = borders[(i - 1) >> 1].vertical;
182
183     }
184   else
185     {
186       for (i = 0; i < G_N_ELEMENTS (borders); i++)
187         borders[i].vertical = borders[i].horizontal;
188     }
189
190   for (i = 0; i < G_N_ELEMENTS (borders); i++)
191     {
192       values[i] = _gtk_css_value_new_from_border_corner_radius (&borders[i]);
193     }
194
195   return TRUE;
196 }
197
198 static gboolean 
199 parse_border_color (GtkCssShorthandProperty  *shorthand,
200                     GtkCssValue             **values,
201                     GtkCssParser             *parser,
202                     GFile                    *base)
203 {
204   GtkSymbolicColor *symbolic;
205   guint i;
206
207   for (i = 0; i < 4; i++)
208     {
209       if (_gtk_css_parser_try (parser, "currentcolor", TRUE))
210         {
211           symbolic = gtk_symbolic_color_ref (_gtk_symbolic_color_get_current_color ());
212         }
213       else
214         {
215           symbolic = _gtk_css_parser_read_symbolic_color (parser);
216           if (symbolic == NULL)
217             return FALSE;
218         }
219
220       values[i] = _gtk_css_value_new_take_symbolic_color (symbolic);
221
222       if (value_is_done_parsing (parser))
223         break;
224     }
225
226   for (i++; i < 4; i++)
227     {
228       values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
229     }
230
231   return TRUE;
232 }
233
234 static gboolean
235 parse_border_style (GtkCssShorthandProperty  *shorthand,
236                     GtkCssValue             **values,
237                     GtkCssParser             *parser,
238                     GFile                    *base)
239 {
240   guint i;
241
242   for (i = 0; i < 4; i++)
243     {
244       values[i] = _gtk_css_border_style_value_try_parse (parser);
245       if (values[i] == NULL)
246         break;
247     }
248
249   if (i == 0)
250     {
251       _gtk_css_parser_error (parser, "Expected a border style");
252       return FALSE;
253     }
254
255   for (; i < 4; i++)
256     values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
257
258   return TRUE;
259 }
260
261 static gboolean
262 parse_border_image (GtkCssShorthandProperty  *shorthand,
263                     GtkCssValue             **values,
264                     GtkCssParser             *parser,
265                     GFile                    *base)
266 {
267   GValue value = G_VALUE_INIT;
268   GtkCssImage *image;
269   
270   if (_gtk_css_parser_try (parser, "none", TRUE))
271     image = NULL;
272   else
273     {
274       image = _gtk_css_image_new_parse (parser, base);
275       if (!image)
276         return FALSE;
277     }
278   values[0] = _gtk_css_value_new_take_image (image);
279
280   if (value_is_done_parsing (parser))
281     return TRUE;
282
283   g_value_init (&value, GTK_TYPE_BORDER);
284   if (!_gtk_css_style_parse_value (&value, parser, base))
285     return FALSE;
286   values[1] = _gtk_css_value_new_from_gvalue (&value);
287   g_value_unset (&value);
288
289   if (_gtk_css_parser_try (parser, "/", TRUE))
290     {
291       g_value_init (&value, GTK_TYPE_BORDER);
292       if (!_gtk_css_style_parse_value (&value, parser, base))
293         return FALSE;
294       values[2] = _gtk_css_value_new_from_gvalue (&value);
295       g_value_unset (&value);
296     }
297
298   if (value_is_done_parsing (parser))
299     return TRUE;
300
301   g_value_init (&value, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
302   if (!_gtk_css_style_parse_value (&value, parser, base))
303     return FALSE;
304   values[3] = _gtk_css_value_new_from_gvalue (&value);
305   g_value_unset (&value);
306
307   return TRUE;
308 }
309
310 static gboolean
311 parse_border_side (GtkCssShorthandProperty  *shorthand,
312                    GtkCssValue             **values,
313                    GtkCssParser             *parser,
314                    GFile                    *base)
315 {
316   do
317   {
318     if (values[0] == NULL &&
319          _gtk_css_parser_has_number (parser))
320       {
321         values[0] = _gtk_css_number_value_parse (parser,
322                                                  GTK_CSS_POSITIVE_ONLY
323                                                  | GTK_CSS_NUMBER_AS_PIXELS
324                                                  | GTK_CSS_PARSE_LENGTH);
325         if (values[0] == NULL)
326           return FALSE;
327       }
328     else if (values[1] == NULL &&
329              (values[1] = _gtk_css_border_style_value_try_parse (parser)))
330       {
331         /* Nothing to do */
332       }
333     else if (values[2] == NULL)
334       {
335         GtkSymbolicColor *symbolic;
336
337         symbolic = _gtk_css_parser_read_symbolic_color (parser);
338         if (symbolic == NULL)
339           return FALSE;
340
341         values[2] = _gtk_css_value_new_take_symbolic_color (symbolic);
342       }
343     else
344       {
345         /* We parsed everything and there's still stuff left?
346          * Pretend we didn't notice and let the normal code produce
347          * a 'junk at end of value' error */
348         break;
349       }
350   }
351   while (!value_is_done_parsing (parser));
352
353   return TRUE;
354 }
355
356 static gboolean
357 parse_border (GtkCssShorthandProperty  *shorthand,
358               GtkCssValue             **values,
359               GtkCssParser             *parser,
360               GFile                    *base)
361 {
362   do
363   {
364     if (values[0] == NULL &&
365          _gtk_css_parser_has_number (parser))
366       {
367         values[0] = _gtk_css_number_value_parse (parser,
368                                                  GTK_CSS_POSITIVE_ONLY
369                                                  | GTK_CSS_NUMBER_AS_PIXELS
370                                                  | GTK_CSS_PARSE_LENGTH);
371         if (values[0] == NULL)
372           return FALSE;
373         values[1] = _gtk_css_value_ref (values[0]);
374         values[2] = _gtk_css_value_ref (values[0]);
375         values[3] = _gtk_css_value_ref (values[0]);
376       }
377     else if (values[4] == NULL &&
378              (values[4] = _gtk_css_border_style_value_try_parse (parser)))
379       {
380         values[5] = _gtk_css_value_ref (values[4]);
381         values[6] = _gtk_css_value_ref (values[4]);
382         values[7] = _gtk_css_value_ref (values[4]);
383       }
384     else if (!G_IS_VALUE (&values[8]))
385       {
386         GtkSymbolicColor *symbolic;
387
388         symbolic = _gtk_css_parser_read_symbolic_color (parser);
389         if (symbolic == NULL)
390           return FALSE;
391
392         values[8] = _gtk_css_value_new_take_symbolic_color (symbolic);
393         values[9] = _gtk_css_value_ref (values[8]);
394         values[10] = _gtk_css_value_ref (values[8]);
395         values[11] = _gtk_css_value_ref (values[8]);
396       }
397     else
398       {
399         /* We parsed everything and there's still stuff left?
400          * Pretend we didn't notice and let the normal code produce
401          * a 'junk at end of value' error */
402         break;
403       }
404   }
405   while (!value_is_done_parsing (parser));
406
407   /* Note that border-image values are not set: according to the spec
408      they just need to be reset when using the border shorthand */
409
410   return TRUE;
411 }
412
413 static gboolean
414 parse_font (GtkCssShorthandProperty  *shorthand,
415             GtkCssValue             **values,
416             GtkCssParser             *parser,
417             GFile                    *base)
418 {
419   PangoFontDescription *desc;
420   guint mask;
421   char *str;
422
423   str = _gtk_css_parser_read_value (parser);
424   if (str == NULL)
425     return FALSE;
426
427   desc = pango_font_description_from_string (str);
428   g_free (str);
429
430   mask = pango_font_description_get_set_fields (desc);
431
432   if (mask & PANGO_FONT_MASK_FAMILY)
433     {
434       char **strv = g_new0 (char *, 2);
435
436       strv[0] = g_strdup (pango_font_description_get_family (desc));
437       values[0] = _gtk_css_value_new_take_strv (strv);
438     }
439   if (mask & PANGO_FONT_MASK_STYLE)
440     {
441       values[1] = _gtk_css_font_style_value_new (pango_font_description_get_style (desc));
442     }
443   if (mask & PANGO_FONT_MASK_VARIANT)
444     {
445       values[2] = _gtk_css_font_variant_value_new (pango_font_description_get_variant (desc));
446     }
447   if (mask & PANGO_FONT_MASK_WEIGHT)
448     {
449       values[3] = _gtk_css_font_weight_value_new (pango_font_description_get_weight (desc));
450     }
451   if (mask & PANGO_FONT_MASK_SIZE)
452     {
453       values[4] = _gtk_css_value_new_from_double ((double) pango_font_description_get_size (desc) / PANGO_SCALE);
454     }
455
456   pango_font_description_free (desc);
457
458   return TRUE;
459 }
460
461 static gboolean
462 parse_background (GtkCssShorthandProperty  *shorthand,
463                   GtkCssValue             **values,
464                   GtkCssParser             *parser,
465                   GFile                    *base)
466 {
467   int enum_value;
468
469   do
470     {
471       /* the image part */
472       if (values[0] == NULL &&
473           (_gtk_css_parser_has_prefix (parser, "none") ||
474            _gtk_css_image_can_parse (parser)))
475         {
476           GtkCssImage *image;
477
478           if (_gtk_css_parser_try (parser, "none", TRUE))
479             image = NULL;
480           else
481             {
482               image = _gtk_css_image_new_parse (parser, base);
483               if (image == NULL)
484                 return FALSE;
485             }
486
487           values[0] = _gtk_css_value_new_take_image (image);
488         }
489       else if (values[1] == NULL &&
490                _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &enum_value))
491         {
492           if (enum_value <= GTK_CSS_BACKGROUND_REPEAT_MASK)
493             {
494               int vertical;
495
496               if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT, &vertical))
497                 {
498                   if (vertical >= GTK_CSS_BACKGROUND_REPEAT_MASK)
499                     {
500                       _gtk_css_parser_error (parser, "Not a valid 2nd value for border-repeat");
501                       return FALSE;
502                     }
503                   else
504                     enum_value |= vertical << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
505                 }
506               else
507                 enum_value |= enum_value << GTK_CSS_BACKGROUND_REPEAT_SHIFT;
508             }
509
510           values[1] = _gtk_css_value_new_from_enum (GTK_TYPE_CSS_BACKGROUND_REPEAT, enum_value);
511         }
512       else if ((values[2] == NULL || values[3] == NULL) &&
513                _gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_AREA, &enum_value))
514         {
515           guint idx = values[2] == NULL ? 2 : 3;
516           values[idx] = _gtk_css_value_new_from_enum (GTK_TYPE_CSS_AREA, enum_value);
517         }
518       else if (values[4] == NULL)
519         {
520           GtkSymbolicColor *symbolic;
521           
522           symbolic = _gtk_css_parser_read_symbolic_color (parser);
523           if (symbolic == NULL)
524             return FALSE;
525
526           values[4] = _gtk_css_value_new_take_symbolic_color (symbolic);
527         }
528       else
529         {
530           /* We parsed everything and there's still stuff left?
531            * Pretend we didn't notice and let the normal code produce
532            * a 'junk at end of value' error */
533           break;
534         }
535     }
536   while (!value_is_done_parsing (parser));
537
538   return TRUE;
539 }
540
541 /*** PACKING ***/
542
543 static void
544 unpack_border (GtkCssShorthandProperty *shorthand,
545                GtkStyleProperties      *props,
546                GtkStateFlags            state,
547                const GValue            *value)
548 {
549   GValue v = G_VALUE_INIT;
550   GtkBorder *border = g_value_get_boxed (value);
551
552   g_value_init (&v, G_TYPE_INT);
553
554   g_value_set_int (&v, border->top);
555   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0)), props, state, &v);
556   g_value_set_int (&v, border->right);
557   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1)), props, state, &v);
558   g_value_set_int (&v, border->bottom);
559   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2)), props, state, &v);
560   g_value_set_int (&v, border->left);
561   _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3)), props, state, &v);
562
563   g_value_unset (&v);
564 }
565
566 static void
567 pack_border (GtkCssShorthandProperty *shorthand,
568              GValue                  *value,
569              GtkStyleQueryFunc        query_func,
570              gpointer                 query_data)
571 {
572   GtkCssStyleProperty *prop;
573   GtkBorder border;
574   GtkCssValue *v;
575
576   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
577   v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
578   if (v)
579     border.top = _gtk_css_value_get_int (v);
580   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 1);
581   v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
582   if (v)
583     border.right = _gtk_css_value_get_int (v);
584   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 2);
585   v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
586   if (v)
587     border.bottom = _gtk_css_value_get_int (v);
588   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 3);
589   v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
590   if (v)
591     border.left = _gtk_css_value_get_int (v);
592
593   g_value_init (value, GTK_TYPE_BORDER);
594   g_value_set_boxed (value, &border);
595 }
596
597 static void
598 unpack_border_radius (GtkCssShorthandProperty *shorthand,
599                       GtkStyleProperties      *props,
600                       GtkStateFlags            state,
601                       const GValue            *value)
602 {
603   GtkCssBorderCornerRadius border;
604   GValue v = G_VALUE_INIT;
605   guint i;
606   
607   _gtk_css_number_init (&border.horizontal, g_value_get_int (value), GTK_CSS_PX);
608   border.vertical = border.horizontal;
609   g_value_init (&v, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
610   g_value_set_boxed (&v, &border);
611
612   for (i = 0; i < 4; i++)
613     _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, i)), props, state, &v);
614
615   g_value_unset (&v);
616 }
617
618 static void
619 pack_border_radius (GtkCssShorthandProperty *shorthand,
620                     GValue                  *value,
621                     GtkStyleQueryFunc        query_func,
622                     gpointer                 query_data)
623 {
624   const GtkCssBorderCornerRadius *top_left;
625   GtkCssStyleProperty *prop;
626   GtkCssValue *v;
627   int i = 0;
628
629   prop = GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("border-top-left-radius"));
630   v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
631   if (v)
632     {
633       top_left = _gtk_css_value_get_border_corner_radius (v);
634       if (top_left)
635         i = top_left->horizontal.value;
636     }
637
638   g_value_init (value, G_TYPE_INT);
639   g_value_set_int (value, i);
640 }
641
642 static void
643 unpack_font_description (GtkCssShorthandProperty *shorthand,
644                          GtkStyleProperties      *props,
645                          GtkStateFlags            state,
646                          const GValue            *value)
647 {
648   GtkStyleProperty *prop;
649   PangoFontDescription *description;
650   PangoFontMask mask;
651   GValue v = G_VALUE_INIT;
652   
653   /* For backwards compat, we only unpack values that are indeed set.
654    * For strict CSS conformance we need to unpack all of them.
655    * Note that we do set all of them in the parse function, so it
656    * will not have effects when parsing CSS files. It will though
657    * for custom style providers.
658    */
659
660   description = g_value_get_boxed (value);
661
662   if (description)
663     mask = pango_font_description_get_set_fields (description);
664   else
665     mask = 0;
666
667   if (mask & PANGO_FONT_MASK_FAMILY)
668     {
669       GPtrArray *strv = g_ptr_array_new ();
670
671       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
672       g_ptr_array_add (strv, NULL);
673       g_value_init (&v, G_TYPE_STRV);
674       g_value_take_boxed (&v, g_ptr_array_free (strv, FALSE));
675
676       prop = _gtk_style_property_lookup ("font-family");
677       _gtk_style_property_assign (prop, props, state, &v);
678       g_value_unset (&v);
679     }
680
681   if (mask & PANGO_FONT_MASK_STYLE)
682     {
683       g_value_init (&v, PANGO_TYPE_STYLE);
684       g_value_set_enum (&v, pango_font_description_get_style (description));
685
686       prop = _gtk_style_property_lookup ("font-style");
687       _gtk_style_property_assign (prop, props, state, &v);
688       g_value_unset (&v);
689     }
690
691   if (mask & PANGO_FONT_MASK_VARIANT)
692     {
693       g_value_init (&v, PANGO_TYPE_VARIANT);
694       g_value_set_enum (&v, pango_font_description_get_variant (description));
695
696       prop = _gtk_style_property_lookup ("font-variant");
697       _gtk_style_property_assign (prop, props, state, &v);
698       g_value_unset (&v);
699     }
700
701   if (mask & PANGO_FONT_MASK_WEIGHT)
702     {
703       g_value_init (&v, PANGO_TYPE_WEIGHT);
704       g_value_set_enum (&v, pango_font_description_get_weight (description));
705
706       prop = _gtk_style_property_lookup ("font-weight");
707       _gtk_style_property_assign (prop, props, state, &v);
708       g_value_unset (&v);
709     }
710
711   if (mask & PANGO_FONT_MASK_SIZE)
712     {
713       g_value_init (&v, G_TYPE_DOUBLE);
714       g_value_set_double (&v, (double) pango_font_description_get_size (description) / PANGO_SCALE);
715
716       prop = _gtk_style_property_lookup ("font-size");
717       _gtk_style_property_assign (prop, props, state, &v);
718       g_value_unset (&v);
719     }
720 }
721
722 static void
723 pack_font_description (GtkCssShorthandProperty *shorthand,
724                        GValue                  *value,
725                        GtkStyleQueryFunc        query_func,
726                        gpointer                 query_data)
727 {
728   PangoFontDescription *description;
729   GtkCssValue *v;
730
731   description = pango_font_description_new ();
732
733   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-family"))), query_data);
734   if (v)
735     {
736       const char **families = _gtk_css_value_get_strv (v);
737       /* xxx: Can we set all the families here somehow? */
738       if (families)
739         pango_font_description_set_family (description, families[0]);
740     }
741
742   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-size"))), query_data);
743   if (v)
744     pango_font_description_set_size (description, round (_gtk_css_value_get_double (v) * PANGO_SCALE));
745
746   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-style"))), query_data);
747   if (v)
748     pango_font_description_set_style (description, _gtk_css_font_style_value_get (v));
749
750   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-variant"))), query_data);
751   if (v)
752     pango_font_description_set_variant (description, _gtk_css_font_variant_value_get (v));
753
754   v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-weight"))), query_data);
755   if (v)
756     pango_font_description_set_weight (description, _gtk_css_font_weight_value_get (v));
757
758   g_value_init (value, PANGO_TYPE_FONT_DESCRIPTION);
759   g_value_take_boxed (value, description);
760 }
761
762 static void
763 unpack_to_everything (GtkCssShorthandProperty *shorthand,
764                       GtkStyleProperties      *props,
765                       GtkStateFlags            state,
766                       const GValue            *value)
767 {
768   GtkCssStyleProperty *prop;
769   guint i, n;
770   
771   n = _gtk_css_shorthand_property_get_n_subproperties (shorthand);
772
773   for (i = 0; i < n; i++)
774     {
775       prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
776       _gtk_style_property_assign (GTK_STYLE_PROPERTY (prop), props, state, value);
777     }
778 }
779
780 static void
781 pack_first_element (GtkCssShorthandProperty *shorthand,
782                     GValue                  *value,
783                     GtkStyleQueryFunc        query_func,
784                     gpointer                 query_data)
785 {
786   GtkCssStyleProperty *prop;
787
788   /* NB: This is a fallback for properties that originally were
789    * not used as shorthand. We just pick the first subproperty
790    * as a representative.
791    * Lesson learned: Don't query the shorthand, query the 
792    * real properties instead. */
793   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
794   _gtk_style_property_query (GTK_STYLE_PROPERTY (prop),
795                              value,
796                              query_func,
797                              query_data);
798 }
799
800 static void
801 _gtk_css_shorthand_property_register (const char                        *name,
802                                       GType                              value_type,
803                                       const char                       **subproperties,
804                                       GtkCssShorthandPropertyParseFunc   parse_func,
805                                       GtkCssShorthandPropertyAssignFunc  assign_func,
806                                       GtkCssShorthandPropertyQueryFunc   query_func)
807 {
808   GtkCssShorthandProperty *node;
809
810   node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
811                        "name", name,
812                        "value-type", value_type,
813                        "subproperties", subproperties,
814                        NULL);
815
816   node->parse = parse_func;
817   node->assign = assign_func;
818   node->query = query_func;
819 }
820
821 void
822 _gtk_css_shorthand_property_init_properties (void)
823 {
824   /* The order is important here, be careful when changing it */
825   const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
826   const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
827   const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
828   const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
829   const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
830                                                 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
831   const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
832   const char *border_style_subproperties[] = { "border-top-style", "border-right-style", "border-bottom-style", "border-left-style", NULL };
833   const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
834   const char *border_top_subproperties[] = { "border-top-width", "border-top-style", "border-top-color", NULL };
835   const char *border_right_subproperties[] = { "border-right-width", "border-right-style", "border-right-color", NULL };
836   const char *border_bottom_subproperties[] = { "border-bottom-width", "border-bottom-style", "border-bottom-color", NULL };
837   const char *border_left_subproperties[] = { "border-left-width", "border-left-style", "border-left-color", NULL };
838   const char *border_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
839                                          "border-top-style", "border-right-style", "border-bottom-style", "border-left-style",
840                                          "border-top-color", "border-right-color", "border-bottom-color", "border-left-color",
841                                          "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
842   const char *outline_subproperties[] = { "outline-width", "outline-style", "outline-color", NULL };
843   const char *background_subproperties[] = { "background-image", "background-repeat", "background-clip", "background-origin",
844                                              "background-color", NULL };
845
846   _gtk_css_shorthand_property_register   ("font",
847                                           PANGO_TYPE_FONT_DESCRIPTION,
848                                           font_subproperties,
849                                           parse_font,
850                                           unpack_font_description,
851                                           pack_font_description);
852   _gtk_css_shorthand_property_register   ("margin",
853                                           GTK_TYPE_BORDER,
854                                           margin_subproperties,
855                                           parse_margin,
856                                           unpack_border,
857                                           pack_border);
858   _gtk_css_shorthand_property_register   ("padding",
859                                           GTK_TYPE_BORDER,
860                                           padding_subproperties,
861                                           parse_padding,
862                                           unpack_border,
863                                           pack_border);
864   _gtk_css_shorthand_property_register   ("border-width",
865                                           GTK_TYPE_BORDER,
866                                           border_width_subproperties,
867                                           parse_border_width,
868                                           unpack_border,
869                                           pack_border);
870   _gtk_css_shorthand_property_register   ("border-radius",
871                                           G_TYPE_INT,
872                                           border_radius_subproperties,
873                                           parse_border_radius,
874                                           unpack_border_radius,
875                                           pack_border_radius);
876   _gtk_css_shorthand_property_register   ("border-color",
877                                           GDK_TYPE_RGBA,
878                                           border_color_subproperties,
879                                           parse_border_color,
880                                           unpack_to_everything,
881                                           pack_first_element);
882   _gtk_css_shorthand_property_register   ("border-style",
883                                           GTK_TYPE_BORDER_STYLE,
884                                           border_style_subproperties,
885                                           parse_border_style,
886                                           unpack_to_everything,
887                                           pack_first_element);
888   _gtk_css_shorthand_property_register   ("border-image",
889                                           G_TYPE_NONE,
890                                           border_image_subproperties,
891                                           parse_border_image,
892                                           NULL,
893                                           NULL);
894   _gtk_css_shorthand_property_register   ("border-top",
895                                           G_TYPE_NONE,
896                                           border_top_subproperties,
897                                           parse_border_side,
898                                           NULL,
899                                           NULL);
900   _gtk_css_shorthand_property_register   ("border-right",
901                                           G_TYPE_NONE,
902                                           border_right_subproperties,
903                                           parse_border_side,
904                                           NULL,
905                                           NULL);
906   _gtk_css_shorthand_property_register   ("border-bottom",
907                                           G_TYPE_NONE,
908                                           border_bottom_subproperties,
909                                           parse_border_side,
910                                           NULL,
911                                           NULL);
912   _gtk_css_shorthand_property_register   ("border-left",
913                                           G_TYPE_NONE,
914                                           border_left_subproperties,
915                                           parse_border_side,
916                                           NULL,
917                                           NULL);
918   _gtk_css_shorthand_property_register   ("border",
919                                           G_TYPE_NONE,
920                                           border_subproperties,
921                                           parse_border,
922                                           NULL,
923                                           NULL);
924   _gtk_css_shorthand_property_register   ("outline",
925                                           G_TYPE_NONE,
926                                           outline_subproperties,
927                                           parse_border_side,
928                                           NULL,
929                                           NULL);
930   _gtk_css_shorthand_property_register   ("background",
931                                           G_TYPE_NONE,
932                                           background_subproperties,
933                                           parse_background,
934                                           NULL,
935                                           NULL);
936 }