]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandpropertyimpl.c
shorthand: Unify unpack_border()
[~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 (GtkCssShorthandProperty *shorthand,
516                const GValue            *value,
517                guint                   *n_params)
518 {
519   GParameter *parameter = g_new0 (GParameter, 4);
520   GtkBorder *border = g_value_get_boxed (value);
521
522   parameter[0].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0)));
523   g_value_init (&parameter[0].value, G_TYPE_INT);
524   g_value_set_int (&parameter[0].value, border->top);
525   parameter[1].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1)));
526   g_value_init (&parameter[1].value, G_TYPE_INT);
527   g_value_set_int (&parameter[1].value, border->right);
528   parameter[2].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2)));
529   g_value_init (&parameter[2].value, G_TYPE_INT);
530   g_value_set_int (&parameter[2].value, border->bottom);
531   parameter[3].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3)));
532   g_value_init (&parameter[3].value, G_TYPE_INT);
533   g_value_set_int (&parameter[3].value, border->left);
534
535   *n_params = 4;
536   return parameter;
537 }
538
539 static void
540 pack_border (GtkCssShorthandProperty *shorthand,
541              GValue                  *value,
542              GtkStyleProperties      *props,
543              GtkStateFlags            state)
544 {
545   GtkCssStyleProperty *prop;
546   GtkBorder border;
547   const GValue *v;
548
549   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
550   v = _gtk_style_properties_peek_property (props, prop, state);
551   if (v)
552     border.top = g_value_get_int (v);
553   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 1);
554   v = _gtk_style_properties_peek_property (props, prop, state);
555   if (v)
556     border.right = g_value_get_int (v);
557   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 2);
558   v = _gtk_style_properties_peek_property (props, prop, state);
559   if (v)
560     border.bottom = g_value_get_int (v);
561   prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 3);
562   v = _gtk_style_properties_peek_property (props, prop, state);
563   if (v)
564     border.left = g_value_get_int (v);
565
566   g_value_set_boxed (value, &border);
567 }
568
569 static GParameter *
570 unpack_border_radius (GtkCssShorthandProperty *shorthand,
571                       const GValue            *value,
572                       guint                   *n_params)
573 {
574   GParameter *parameter = g_new0 (GParameter, 4);
575   GtkCssBorderCornerRadius border;
576   
577   border.horizontal = border.vertical = g_value_get_int (value);
578
579   parameter[0].name = "border-top-left-radius";
580   g_value_init (&parameter[0].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
581   g_value_set_boxed (&parameter[0].value, &border);
582   parameter[1].name = "border-top-right-radius";
583   g_value_init (&parameter[1].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
584   g_value_set_boxed (&parameter[1].value, &border);
585   parameter[2].name = "border-bottom-right-radius";
586   g_value_init (&parameter[2].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
587   g_value_set_boxed (&parameter[2].value, &border);
588   parameter[3].name = "border-bottom-left-radius";
589   g_value_init (&parameter[3].value, GTK_TYPE_CSS_BORDER_CORNER_RADIUS);
590   g_value_set_boxed (&parameter[3].value, &border);
591
592   *n_params = 4;
593   return parameter;
594 }
595
596 static void
597 pack_border_radius (GtkCssShorthandProperty *shorthand,
598                     GValue                  *value,
599                     GtkStyleProperties      *props,
600                     GtkStateFlags            state)
601 {
602   GtkCssBorderCornerRadius *top_left;
603
604   /* NB: We are an int property, so we have to resolve to an int here.
605    * So we just resolve to an int. We pick one and stick to it.
606    * Lesson learned: Don't query border-radius shorthand, query the 
607    * real properties instead. */
608   gtk_style_properties_get (props,
609                             state,
610                             "border-top-left-radius", &top_left,
611                             NULL);
612
613   if (top_left)
614     g_value_set_int (value, top_left->horizontal);
615
616   g_free (top_left);
617 }
618
619 static GParameter *
620 unpack_font_description (GtkCssShorthandProperty *shorthand,
621                          const GValue            *value,
622                          guint                   *n_params)
623 {
624   GParameter *parameter = g_new0 (GParameter, 5);
625   PangoFontDescription *description;
626   PangoFontMask mask;
627   guint n;
628   
629   /* For backwards compat, we only unpack values that are indeed set.
630    * For strict CSS conformance we need to unpack all of them.
631    * Note that we do set all of them in the parse function, so it
632    * will not have effects when parsing CSS files. It will though
633    * for custom style providers.
634    */
635
636   description = g_value_get_boxed (value);
637   n = 0;
638
639   if (description)
640     mask = pango_font_description_get_set_fields (description);
641   else
642     mask = 0;
643
644   if (mask & PANGO_FONT_MASK_FAMILY)
645     {
646       GPtrArray *strv = g_ptr_array_new ();
647
648       g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
649       g_ptr_array_add (strv, NULL);
650       parameter[n].name = "font-family";
651       g_value_init (&parameter[n].value, G_TYPE_STRV);
652       g_value_take_boxed (&parameter[n].value,
653                           g_ptr_array_free (strv, FALSE));
654       n++;
655     }
656
657   if (mask & PANGO_FONT_MASK_STYLE)
658     {
659       parameter[n].name = "font-style";
660       g_value_init (&parameter[n].value, PANGO_TYPE_STYLE);
661       g_value_set_enum (&parameter[n].value,
662                         pango_font_description_get_style (description));
663       n++;
664     }
665
666   if (mask & PANGO_FONT_MASK_VARIANT)
667     {
668       parameter[n].name = "font-variant";
669       g_value_init (&parameter[n].value, PANGO_TYPE_VARIANT);
670       g_value_set_enum (&parameter[n].value,
671                         pango_font_description_get_variant (description));
672       n++;
673     }
674
675   if (mask & PANGO_FONT_MASK_WEIGHT)
676     {
677       parameter[n].name = "font-weight";
678       g_value_init (&parameter[n].value, PANGO_TYPE_WEIGHT);
679       g_value_set_enum (&parameter[n].value,
680                         pango_font_description_get_weight (description));
681       n++;
682     }
683
684   if (mask & PANGO_FONT_MASK_SIZE)
685     {
686       parameter[n].name = "font-size";
687       g_value_init (&parameter[n].value, G_TYPE_DOUBLE);
688       g_value_set_double (&parameter[n].value,
689                           (double) pango_font_description_get_size (description) / PANGO_SCALE);
690       n++;
691     }
692
693   *n_params = n;
694
695   return parameter;
696 }
697
698 static void
699 pack_font_description (GtkCssShorthandProperty *shorthand,
700                        GValue                  *value,
701                        GtkStyleProperties      *props,
702                        GtkStateFlags            state)
703 {
704   PangoFontDescription *description;
705   char **families;
706   PangoStyle style;
707   PangoVariant variant;
708   PangoWeight weight;
709   double size;
710
711   gtk_style_properties_get (props,
712                             state,
713                             "font-family", &families,
714                             "font-style", &style,
715                             "font-variant", &variant,
716                             "font-weight", &weight,
717                             "font-size", &size,
718                             NULL);
719
720   description = pango_font_description_new ();
721   /* xxx: Can we set all the families here somehow? */
722   if (families)
723     pango_font_description_set_family (description, families[0]);
724   pango_font_description_set_size (description, round (size * PANGO_SCALE));
725   pango_font_description_set_style (description, style);
726   pango_font_description_set_variant (description, variant);
727   pango_font_description_set_weight (description, weight);
728
729   g_strfreev (families);
730
731   g_value_take_boxed (value, description);
732 }
733
734 static GParameter *
735 unpack_to_everything (GtkCssShorthandProperty *shorthand,
736                       const GValue            *value,
737                       guint                   *n_params)
738 {
739   GtkCssStyleProperty *prop;
740   GParameter *parameter;
741   guint i, n;
742   GType type;
743   
744   n = _gtk_css_shorthand_property_get_n_subproperties (shorthand);
745   parameter = g_new0 (GParameter, n);
746   type = G_VALUE_TYPE (value);
747
748   for (i = 0; i < n; i++)
749     {
750       prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
751       parameter[i].name = _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop));
752       g_value_init (&parameter[i].value, type);
753       g_value_copy (value, &parameter[i].value);
754     }
755
756   *n_params = n;
757   return parameter;
758 }
759
760 static void
761 pack_first_element (GtkCssShorthandProperty *shorthand,
762                     GValue                  *value,
763                     GtkStyleProperties      *props,
764                     GtkStateFlags            state)
765 {
766   GtkCssStyleProperty *prop;
767   const GValue *v;
768   guint i;
769
770   /* NB: This is a fallback for properties that originally were
771    * not used as shorthand. We just pick the first subproperty
772    * as a representative.
773    * Lesson learned: Don't query the shorthand, query the 
774    * real properties instead. */
775   for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
776     {
777       prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
778       v = _gtk_style_properties_peek_property (props, prop, state);
779       if (v)
780         {
781           g_value_copy (v, value);
782           return;
783         }
784     }
785 }
786
787 static void
788 _gtk_css_shorthand_property_register (const char                        *name,
789                                       GType                              value_type,
790                                       const char                       **subproperties,
791                                       GtkCssShorthandPropertyParseFunc   parse_func,
792                                       GtkCssShorthandPropertyAssignFunc  assign_func,
793                                       GtkCssShorthandPropertyQueryFunc   query_func)
794 {
795   GtkCssShorthandProperty *node;
796
797   node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
798                        "name", name,
799                        "value-type", value_type,
800                        "subproperties", subproperties,
801                        NULL);
802
803   node->parse = parse_func;
804   node->assign = assign_func;
805   node->query = query_func;
806 }
807
808 void
809 _gtk_css_shorthand_property_init_properties (void)
810 {
811   /* The order is important here, be careful when changing it */
812   const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
813   const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
814   const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
815   const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
816   const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
817                                                 "border-bottom-right-radius", "border-bottom-left-radius", NULL };
818   const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
819   const char *border_style_subproperties[] = { "border-top-style", "border-right-style", "border-bottom-style", "border-left-style", NULL };
820   const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
821   const char *border_top_subproperties[] = { "border-top-width", "border-top-style", "border-top-color", NULL };
822   const char *border_right_subproperties[] = { "border-right-width", "border-right-style", "border-right-color", NULL };
823   const char *border_bottom_subproperties[] = { "border-bottom-width", "border-bottom-style", "border-bottom-color", NULL };
824   const char *border_left_subproperties[] = { "border-left-width", "border-left-style", "border-left-color", NULL };
825   const char *border_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
826                                          "border-top-style", "border-right-style", "border-bottom-style", "border-left-style",
827                                          "border-top-color", "border-right-color", "border-bottom-color", "border-left-color",
828                                          "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
829   const char *outline_subproperties[] = { "outline-width", "outline-style", "outline-color", NULL };
830   const char *background_subproperties[] = { "background-image", "background-repeat", "background-clip", "background-origin",
831                                              "background-color", NULL };
832
833   _gtk_css_shorthand_property_register   ("font",
834                                           PANGO_TYPE_FONT_DESCRIPTION,
835                                           font_subproperties,
836                                           parse_font,
837                                           unpack_font_description,
838                                           pack_font_description);
839   _gtk_css_shorthand_property_register   ("margin",
840                                           GTK_TYPE_BORDER,
841                                           margin_subproperties,
842                                           parse_border_width,
843                                           unpack_border,
844                                           pack_border);
845   _gtk_css_shorthand_property_register   ("padding",
846                                           GTK_TYPE_BORDER,
847                                           padding_subproperties,
848                                           parse_border_width,
849                                           unpack_border,
850                                           pack_border);
851   _gtk_css_shorthand_property_register   ("border-width",
852                                           GTK_TYPE_BORDER,
853                                           border_width_subproperties,
854                                           parse_border_width,
855                                           unpack_border,
856                                           pack_border);
857   _gtk_css_shorthand_property_register   ("border-radius",
858                                           G_TYPE_INT,
859                                           border_radius_subproperties,
860                                           parse_border_radius,
861                                           unpack_border_radius,
862                                           pack_border_radius);
863   _gtk_css_shorthand_property_register   ("border-color",
864                                           GDK_TYPE_RGBA,
865                                           border_color_subproperties,
866                                           parse_border_color,
867                                           unpack_to_everything,
868                                           pack_first_element);
869   _gtk_css_shorthand_property_register   ("border-style",
870                                           GTK_TYPE_BORDER_STYLE,
871                                           border_style_subproperties,
872                                           parse_border_style,
873                                           unpack_to_everything,
874                                           pack_first_element);
875   _gtk_css_shorthand_property_register   ("border-image",
876                                           G_TYPE_NONE,
877                                           border_image_subproperties,
878                                           parse_border_image,
879                                           NULL,
880                                           NULL);
881   _gtk_css_shorthand_property_register   ("border-top",
882                                           G_TYPE_NONE,
883                                           border_top_subproperties,
884                                           parse_border_side,
885                                           NULL,
886                                           NULL);
887   _gtk_css_shorthand_property_register   ("border-right",
888                                           G_TYPE_NONE,
889                                           border_right_subproperties,
890                                           parse_border_side,
891                                           NULL,
892                                           NULL);
893   _gtk_css_shorthand_property_register   ("border-bottom",
894                                           G_TYPE_NONE,
895                                           border_bottom_subproperties,
896                                           parse_border_side,
897                                           NULL,
898                                           NULL);
899   _gtk_css_shorthand_property_register   ("border-left",
900                                           G_TYPE_NONE,
901                                           border_left_subproperties,
902                                           parse_border_side,
903                                           NULL,
904                                           NULL);
905   _gtk_css_shorthand_property_register   ("border",
906                                           G_TYPE_NONE,
907                                           border_subproperties,
908                                           parse_border,
909                                           NULL,
910                                           NULL);
911   _gtk_css_shorthand_property_register   ("outline",
912                                           G_TYPE_NONE,
913                                           outline_subproperties,
914                                           parse_border_side,
915                                           NULL,
916                                           NULL);
917   _gtk_css_shorthand_property_register   ("background",
918                                           G_TYPE_NONE,
919                                           background_subproperties,
920                                           parse_background,
921                                           NULL,
922                                           NULL);
923 }