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