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