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