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