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