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