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 "gtkcssarrayvalueprivate.h"
25 #include "gtkcssinheritvalueprivate.h"
26 #include "gtkcssinitialvalueprivate.h"
27 #include "gtkcssstylefuncsprivate.h"
28 #include "gtkcssunsetvalueprivate.h"
29 #include "gtkintl.h"
30 
31 enum {
32   PROP_0,
33   PROP_SUBPROPERTIES,
34 };
35 
G_DEFINE_TYPE(GtkCssShorthandProperty,_gtk_css_shorthand_property,GTK_TYPE_STYLE_PROPERTY)36 G_DEFINE_TYPE (GtkCssShorthandProperty, _gtk_css_shorthand_property, GTK_TYPE_STYLE_PROPERTY)
37 
38 static void
39 gtk_css_shorthand_property_set_property (GObject      *object,
40                                          guint         prop_id,
41                                          const GValue *value,
42                                          GParamSpec   *pspec)
43 {
44   GtkCssShorthandProperty *property = GTK_CSS_SHORTHAND_PROPERTY (object);
45   const char **subproperties;
46   guint i;
47 
48   switch (prop_id)
49     {
50     case PROP_SUBPROPERTIES:
51       subproperties = g_value_get_boxed (value);
52       g_assert (subproperties);
53       for (i = 0; subproperties[i] != NULL; i++)
54         {
55           GtkStyleProperty *subproperty = _gtk_style_property_lookup (subproperties[i]);
56           g_assert (GTK_IS_CSS_STYLE_PROPERTY (subproperty));
57           g_ptr_array_add (property->subproperties, subproperty);
58         }
59       break;
60     default:
61       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
62       break;
63     }
64 }
65 
66 static void
_gtk_css_shorthand_property_assign(GtkStyleProperty * property,GtkStyleProperties * props,GtkStateFlags state,const GValue * value)67 _gtk_css_shorthand_property_assign (GtkStyleProperty   *property,
68                                     GtkStyleProperties *props,
69                                     GtkStateFlags       state,
70                                     const GValue       *value)
71 {
72   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
73 
74   shorthand->assign (shorthand, props, state, value);
75 }
76 
77 static void
_gtk_css_shorthand_property_query(GtkStyleProperty * property,GValue * value,GtkStyleQueryFunc query_func,gpointer query_data)78 _gtk_css_shorthand_property_query (GtkStyleProperty   *property,
79                                    GValue             *value,
80                                    GtkStyleQueryFunc   query_func,
81                                    gpointer            query_data)
82 {
83   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
84 
85   shorthand->query (shorthand, value, query_func, query_data);
86 }
87 
88 static GtkCssValue *
gtk_css_shorthand_property_parse_value(GtkStyleProperty * property,GtkCssParser * parser)89 gtk_css_shorthand_property_parse_value (GtkStyleProperty *property,
90                                         GtkCssParser     *parser)
91 {
92   GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
93   GtkCssValue **data;
94   GtkCssValue *result;
95   guint i;
96 
97   data = g_new0 (GtkCssValue *, shorthand->subproperties->len);
98 
99   if (_gtk_css_parser_try (parser, "initial", TRUE))
100     {
101       /* the initial value can be explicitly specified with the
102        * ‘initial’ keyword which all properties accept.
103        */
104       for (i = 0; i < shorthand->subproperties->len; i++)
105         {
106           data[i] = _gtk_css_initial_value_new ();
107         }
108     }
109   else if (_gtk_css_parser_try (parser, "inherit", TRUE))
110     {
111       /* All properties accept the ‘inherit’ value which
112        * explicitly specifies that the value will be determined
113        * by inheritance. The ‘inherit’ value can be used to
114        * strengthen inherited values in the cascade, and it can
115        * also be used on properties that are not normally inherited.
116        */
117       for (i = 0; i < shorthand->subproperties->len; i++)
118         {
119           data[i] = _gtk_css_inherit_value_new ();
120         }
121     }
122   else if (_gtk_css_parser_try (parser, "unset", TRUE))
123     {
124       /* If the cascaded value of a property is the unset keyword,
125        * then if it is an inherited property, this is treated as
126        * inherit, and if it is not, this is treated as initial.
127        */
128       for (i = 0; i < shorthand->subproperties->len; i++)
129         {
130           data[i] = _gtk_css_unset_value_new ();
131         }
132     }
133   else if (!shorthand->parse (shorthand, data, parser))
134     {
135       for (i = 0; i < shorthand->subproperties->len; i++)
136         {
137           if (data[i] != NULL)
138             _gtk_css_value_unref (data[i]);
139         }
140       g_free (data);
141       return NULL;
142     }
143 
144   /* All values that aren't set by the parse func are set to their
145    * default values here.
146    * XXX: Is the default always initial or can it be inherit? */
147   for (i = 0; i < shorthand->subproperties->len; i++)
148     {
149       if (data[i] == NULL)
150         data[i] = _gtk_css_initial_value_new ();
151     }
152 
153   result = _gtk_css_array_value_new_from_array (data, shorthand->subproperties->len);
154   g_free (data);
155 
156   return result;
157 }
158 
159 static void
_gtk_css_shorthand_property_class_init(GtkCssShorthandPropertyClass * klass)160 _gtk_css_shorthand_property_class_init (GtkCssShorthandPropertyClass *klass)
161 {
162   GObjectClass *object_class = G_OBJECT_CLASS (klass);
163   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
164 
165   object_class->set_property = gtk_css_shorthand_property_set_property;
166 
167   g_object_class_install_property (object_class,
168                                    PROP_SUBPROPERTIES,
169                                    g_param_spec_boxed ("subproperties",
170                                                        P_("Subproperties"),
171                                                        P_("The list of subproperties"),
172                                                        G_TYPE_STRV,
173                                                        G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
174 
175   property_class->assign = _gtk_css_shorthand_property_assign;
176   property_class->query = _gtk_css_shorthand_property_query;
177   property_class->parse_value = gtk_css_shorthand_property_parse_value;
178 }
179 
180 static void
_gtk_css_shorthand_property_init(GtkCssShorthandProperty * shorthand)181 _gtk_css_shorthand_property_init (GtkCssShorthandProperty *shorthand)
182 {
183   shorthand->subproperties = g_ptr_array_new_with_free_func (g_object_unref);
184 }
185 
186 GtkCssStyleProperty *
_gtk_css_shorthand_property_get_subproperty(GtkCssShorthandProperty * shorthand,guint property)187 _gtk_css_shorthand_property_get_subproperty (GtkCssShorthandProperty *shorthand,
188                                              guint                    property)
189 {
190   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), NULL);
191   g_return_val_if_fail (property < shorthand->subproperties->len, NULL);
192 
193   return g_ptr_array_index (shorthand->subproperties, property);
194 }
195 
196 guint
_gtk_css_shorthand_property_get_n_subproperties(GtkCssShorthandProperty * shorthand)197 _gtk_css_shorthand_property_get_n_subproperties (GtkCssShorthandProperty *shorthand)
198 {
199   g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), 0);
200 
201   return shorthand->subproperties->len;
202 }
203 
204