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