]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssshorthandproperty.c
shorthand: Redo shorthand value parsing
[~andy/gtk] / gtk / gtkcssshorthandproperty.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 "gtkcssstylefuncsprivate.h"
26 #include "gtkcsstypesprivate.h"
27 #include "gtkintl.h"
28 #include "gtkprivatetypebuiltins.h"
29
30 enum {
31   PROP_0,
32   PROP_SUBPROPERTIES,
33 };
34
35 G_DEFINE_TYPE (GtkCssShorthandProperty, _gtk_css_shorthand_property, GTK_TYPE_STYLE_PROPERTY)
36
37 static void
38 gtk_css_shorthand_property_set_property (GObject      *object,
39                                          guint         prop_id,
40                                          const GValue *value,
41                                          GParamSpec   *pspec)
42 {
43   GtkCssShorthandProperty *property = GTK_CSS_SHORTHAND_PROPERTY (object);
44   const char **subproperties;
45   guint i;
46
47   switch (prop_id)
48     {
49     case PROP_SUBPROPERTIES:
50       subproperties = g_value_get_boxed (value);
51       g_assert (subproperties);
52       for (i = 0; subproperties[i] != NULL; i++)
53         {
54           GtkStyleProperty *subproperty = _gtk_style_property_lookup (subproperties[i]);
55           g_assert (GTK_IS_CSS_STYLE_PROPERTY (subproperty));
56           g_ptr_array_add (property->subproperties, subproperty);
57         }
58       break;
59     default:
60       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
61       break;
62     }
63 }
64
65 static void
66 _gtk_css_shorthand_property_assign (GtkStyleProperty   *property,
67                                     GtkStyleProperties *props,
68                                     GtkStateFlags       state,
69                                     const GValue       *value)
70 {
71   GParameter *parameters;
72   guint i, n_parameters;
73
74   parameters = _gtk_style_property_unpack (property, value, &n_parameters);
75
76   for (i = 0; i < n_parameters; i++)
77     {
78       _gtk_style_property_assign (_gtk_style_property_lookup (parameters[i].name),
79                                   props,
80                                   state,
81                                   &parameters[i].value);
82       g_value_unset (&parameters[i].value);
83     }
84   g_free (parameters);
85 }
86
87 static void
88 _gtk_css_shorthand_property_query (GtkStyleProperty   *property,
89                                    GtkStyleProperties *props,
90                                    GtkStateFlags       state,
91                                    GtkStylePropertyContext *context,
92                                    GValue             *value)
93 {
94   property->pack_func (value, props, state, context);
95 }
96
97 static gboolean
98 gtk_css_shorthand_property_parse_value (GtkStyleProperty *property,
99                                         GValue           *value,
100                                         GtkCssParser     *parser,
101                                         GFile            *base)
102 {
103   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
104   GValueArray *array;
105   guint i;
106
107   array = g_value_array_new (shorthand->subproperties->len);
108   for (i = 0; i < shorthand->subproperties->len; i++)
109     g_value_array_append (array, NULL);
110
111   if (_gtk_css_parser_try (parser, "initial", TRUE))
112     {
113       /* the initial value can be explicitly specified with the
114        * ‘initial’ keyword which all properties accept.
115        */
116       for (i = 0; i < shorthand->subproperties->len; i++)
117         {
118           GValue *val = g_value_array_get_nth (array, i);
119           g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
120           g_value_set_enum (val, GTK_CSS_INITIAL);
121         }
122     }
123   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
124     {
125       /* All properties accept the ‘inherit’ value which
126        * explicitly specifies that the value will be determined
127        * by inheritance. The ‘inherit’ value can be used to
128        * strengthen inherited values in the cascade, and it can
129        * also be used on properties that are not normally inherited.
130        */
131       for (i = 0; i < shorthand->subproperties->len; i++)
132         {
133           GValue *val = g_value_array_get_nth (array, i);
134           g_value_init (val, GTK_TYPE_CSS_SPECIAL_VALUE);
135           g_value_set_enum (val, GTK_CSS_INHERIT);
136         }
137     }
138   else if (!shorthand->parse (shorthand, array->values, parser, base))
139     {
140       g_value_array_free (array);
141       return FALSE;
142     }
143
144   g_value_unset (value);
145   g_value_init (value, G_TYPE_VALUE_ARRAY);
146   g_value_set_boxed (value, array);
147   return TRUE;
148 }
149
150 static void
151 _gtk_css_shorthand_property_class_init (GtkCssShorthandPropertyClass *klass)
152 {
153   GObjectClass *object_class = G_OBJECT_CLASS (klass);
154   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
155
156   object_class->set_property = gtk_css_shorthand_property_set_property;
157
158   g_object_class_install_property (object_class,
159                                    PROP_SUBPROPERTIES,
160                                    g_param_spec_boxed ("subproperties",
161                                                        P_("Subproperties"),
162                                                        P_("The list of subproperties"),
163                                                        G_TYPE_STRV,
164                                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
165
166   property_class->assign = _gtk_css_shorthand_property_assign;
167   property_class->query = _gtk_css_shorthand_property_query;
168   property_class->parse_value = gtk_css_shorthand_property_parse_value;
169 }
170
171 /* XXX: This function is compat only, don't read it */
172 static gboolean
173 gtk_css_shorthand_property_parse (GtkCssShorthandProperty *shorthand,
174                                   GValue                  *values,
175                                   GtkCssParser            *parser,
176                                   GFile                   *base)
177 {
178   GtkStyleProperty *property = GTK_STYLE_PROPERTY (shorthand);
179   GParameter *parameters;
180   guint i, j, n_parameters;
181
182   GValue val = G_VALUE_INIT;
183
184   g_value_init (&val, _gtk_style_property_get_value_type (property));
185   if (property->parse_func)
186     {
187       if (!(* property->parse_func) (parser, base, &val))
188         {
189           g_value_unset (&val);
190           return FALSE;
191         }
192     }
193   else if (!_gtk_css_style_parse_value (&val, parser, base))
194     {
195       g_value_unset (&val);
196       return FALSE;
197     }
198
199   parameters = _gtk_style_property_unpack (property, &val, &n_parameters);
200   g_value_unset (&val);
201
202   for (i = 0; i < shorthand->subproperties->len; i++)
203     {
204       for (j = 0; j < n_parameters; j++)
205         {
206           if (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, i))
207               == _gtk_style_property_lookup (parameters[j].name))
208             {
209               g_value_init (&values[i], G_VALUE_TYPE (&parameters[j].value));
210               g_value_copy (&parameters[j].value, &values[i]);
211               g_value_unset (&parameters[j].value);
212               break;
213             }
214         }
215       g_assert (j < n_parameters);
216     }
217   
218   g_free (parameters);
219
220   return TRUE;
221 }
222
223 static void
224 _gtk_css_shorthand_property_init (GtkCssShorthandProperty *shorthand)
225 {
226   shorthand->subproperties = g_ptr_array_new_with_free_func (g_object_unref);
227
228   shorthand->parse = gtk_css_shorthand_property_parse;
229 }
230
231 GtkCssStyleProperty *
232 _gtk_css_shorthand_property_get_subproperty (GtkCssShorthandProperty *shorthand,
233                                              guint                    property)
234 {
235   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), NULL);
236   g_return_val_if_fail (property < shorthand->subproperties->len, NULL);
237
238   return g_ptr_array_index (shorthand->subproperties, property);
239 }
240
241 guint
242 _gtk_css_shorthand_property_get_n_subproperties (GtkCssShorthandProperty *shorthand)
243 {
244   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), 0);
245   
246   return shorthand->subproperties->len;
247 }
248