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