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 "gtkcsscustompropertyprivate.h"
23 
24 #include <string.h>
25 
26 #include "gtkcssstylefuncsprivate.h"
27 #include "gtkcsstypedvalueprivate.h"
28 #include "deprecated/gtkstylepropertiesprivate.h"
29 
30 #include "deprecated/gtkthemingengine.h"
31 
32 #include "deprecated/gtksymboliccolor.h"
33 
G_DEFINE_TYPE(GtkCssCustomProperty,_gtk_css_custom_property,GTK_TYPE_CSS_STYLE_PROPERTY)34 G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)
35 
36 static GtkCssValue *
37 gtk_css_custom_property_parse_value (GtkStyleProperty *property,
38                                      GtkCssParser     *parser)
39 {
40   _gtk_css_parser_error_full (parser,
41                               GTK_CSS_PROVIDER_ERROR_NAME,
42                               "Custom CSS properties are no longer supported.");
43   return NULL;
44 }
45 
46 static void
gtk_css_custom_property_query(GtkStyleProperty * property,GValue * value,GtkStyleQueryFunc query_func,gpointer query_data)47 gtk_css_custom_property_query (GtkStyleProperty   *property,
48                                GValue             *value,
49                                GtkStyleQueryFunc   query_func,
50                                gpointer            query_data)
51 {
52   GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (property);
53   GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
54   GtkCssValue *css_value;
55 
56   css_value = (* query_func) (_gtk_css_style_property_get_id (style), query_data);
57   if (css_value == NULL)
58     css_value = _gtk_css_style_property_get_initial_value (style);
59 
60   g_value_init (value, custom->pspec->value_type);
61   g_value_copy (_gtk_css_typed_value_get (css_value), value);
62 }
63 
64 static void
gtk_css_custom_property_assign(GtkStyleProperty * property,GtkStyleProperties * props,GtkStateFlags state,const GValue * value)65 gtk_css_custom_property_assign (GtkStyleProperty   *property,
66                                 GtkStyleProperties *props,
67                                 GtkStateFlags       state,
68                                 const GValue       *value)
69 {
70   GtkCssValue *css_value = _gtk_css_typed_value_new (value);
71   _gtk_style_properties_set_property_by_property (props,
72                                                   GTK_CSS_STYLE_PROPERTY (property),
73                                                   state,
74                                                   css_value);
75   _gtk_css_value_unref (css_value);
76 }
77 
78 static void
_gtk_css_custom_property_class_init(GtkCssCustomPropertyClass * klass)79 _gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
80 {
81   GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
82 
83   property_class->parse_value = gtk_css_custom_property_parse_value;
84   property_class->query = gtk_css_custom_property_query;
85   property_class->assign = gtk_css_custom_property_assign;
86 }
87 
88 static void
_gtk_css_custom_property_init(GtkCssCustomProperty * custom)89 _gtk_css_custom_property_init (GtkCssCustomProperty *custom)
90 {
91 }
92 
93 static GtkCssValue *
gtk_css_custom_property_create_initial_value(GParamSpec * pspec)94 gtk_css_custom_property_create_initial_value (GParamSpec *pspec)
95 {
96   GValue value = G_VALUE_INIT;
97   GtkCssValue *result;
98 
99   g_value_init (&value, pspec->value_type);
100 
101 
102 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
103   if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
104     g_value_set_object (&value, gtk_theming_engine_load (NULL));
105   else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
106     g_value_take_boxed (&value, pango_font_description_from_string ("Sans 10"));
107   else if (pspec->value_type == GDK_TYPE_RGBA)
108     {
109       GdkRGBA color;
110       gdk_rgba_parse (&color, "pink");
111       g_value_set_boxed (&value, &color);
112     }
113   else if (pspec->value_type == g_type_from_name ("GdkColor"))
114     {
115       GdkColor color;
116       gdk_color_parse ("pink", &color);
117       g_value_set_boxed (&value, &color);
118     }
119   else if (pspec->value_type == GTK_TYPE_BORDER)
120     {
121       g_value_take_boxed (&value, gtk_border_new ());
122     }
123   else
124     g_param_value_set_default (pspec, &value);
125 G_GNUC_END_IGNORE_DEPRECATIONS
126 
127   result = _gtk_css_typed_value_new (&value);
128   g_value_unset (&value);
129 
130   return result;
131 }
132 
133 /* Property registration functions */
134 
135 /**
136  * gtk_theming_engine_register_property: (skip)
137  * @name_space: namespace for the property name
138  * @parse_func: (nullable): parsing function to use, or %NULL
139  * @pspec: the #GParamSpec for the new property
140  *
141  * Registers a property so it can be used in the CSS file format,
142  * on the CSS file the property will look like
143  * "-${@name_space}-${property_name}". being
144  * ${property_name} the given to @pspec. @name_space will usually
145  * be the theme engine name.
146  *
147  * For any type a @parse_func may be provided, being this function
148  * used for turning any property value (between “:” and “;”) in
149  * CSS to the #GValue needed. For basic types there is already
150  * builtin parsing support, so %NULL may be provided for these
151  * cases.
152  *
153  * Engines must ensure property registration happens exactly once,
154  * usually GTK+ deals with theming engines as singletons, so this
155  * should be guaranteed to happen once, but bear this in mind
156  * when creating #GtkThemeEngines yourself.
157  *
158  * In order to make use of the custom registered properties in
159  * the CSS file, make sure the engine is loaded first by specifying
160  * the engine property, either in a previous rule or within the same
161  * one.
162  * |[
163  * * {
164  *     engine: someengine;
165  *     -SomeEngine-custom-property: 2;
166  * }
167  * ]|
168  *
169  * Since: 3.0
170  *
171  * Deprecated: 3.8: Code should use the default properties provided by CSS.
172  **/
173 void
gtk_theming_engine_register_property(const gchar * name_space,GtkStylePropertyParser parse_func,GParamSpec * pspec)174 gtk_theming_engine_register_property (const gchar            *name_space,
175                                       GtkStylePropertyParser  parse_func,
176                                       GParamSpec             *pspec)
177 {
178   GtkCssCustomProperty *node;
179   GtkCssValue *initial;
180   gchar *name;
181 
182   g_return_if_fail (name_space != NULL);
183   g_return_if_fail (strchr (name_space, ' ') == NULL);
184   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
185 
186   name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
187 
188   /* This also initializes the default properties */
189   if (_gtk_style_property_lookup (pspec->name))
190     {
191       g_warning ("a property with name '%s' already exists", name);
192       g_free (name);
193       return;
194     }
195 
196   initial = gtk_css_custom_property_create_initial_value (pspec);
197 
198   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
199                        "initial-value", initial,
200                        "name", name,
201                        "value-type", pspec->value_type,
202                        NULL);
203   node->pspec = pspec;
204   node->property_parse_func = parse_func;
205 
206   _gtk_css_value_unref (initial);
207   g_free (name);
208 }
209 
210 /**
211  * gtk_style_properties_register_property: (skip)
212  * @parse_func: (nullable): parsing function to use, or %NULL
213  * @pspec: the #GParamSpec for the new property
214  *
215  * Registers a property so it can be used in the CSS file format.
216  * This function is the low-level equivalent of
217  * gtk_theming_engine_register_property(), if you are implementing
218  * a theming engine, you want to use that function instead.
219  *
220  * Since: 3.0
221  *
222  * Deprecated: 3.8: Code should use the default properties provided by CSS.
223  **/
224 void
gtk_style_properties_register_property(GtkStylePropertyParser parse_func,GParamSpec * pspec)225 gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
226                                         GParamSpec             *pspec)
227 {
228   GtkCssCustomProperty *node;
229   GtkCssValue *initial;
230 
231   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
232 
233   /* This also initializes the default properties */
234   if (_gtk_style_property_lookup (pspec->name))
235     {
236       g_warning ("a property with name '%s' already exists", pspec->name);
237       return;
238     }
239 
240   initial = gtk_css_custom_property_create_initial_value (pspec);
241 
242   node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
243                        "initial-value", initial,
244                        "name", pspec->name,
245                        "value-type", pspec->value_type,
246                        NULL);
247   node->pspec = pspec;
248   node->property_parse_func = parse_func;
249 
250   _gtk_css_value_unref (initial);
251 }
252 
253 /**
254  * gtk_style_properties_lookup_property: (skip)
255  * @property_name: property name to look up
256  * @parse_func: (out): return location for the parse function
257  * @pspec: (out) (transfer none): return location for the #GParamSpec
258  *
259  * Returns %TRUE if a property has been registered, if @pspec or
260  * @parse_func are not %NULL, the #GParamSpec and parsing function
261  * will be respectively returned.
262  *
263  * Returns: %TRUE if the property is registered, %FALSE otherwise
264  *
265  * Since: 3.0
266  *
267  * Deprecated: 3.8: This code could only look up custom properties and
268  *     those are deprecated.
269  **/
270 gboolean
gtk_style_properties_lookup_property(const gchar * property_name,GtkStylePropertyParser * parse_func,GParamSpec ** pspec)271 gtk_style_properties_lookup_property (const gchar             *property_name,
272                                       GtkStylePropertyParser  *parse_func,
273                                       GParamSpec             **pspec)
274 {
275   GtkStyleProperty *node;
276   gboolean found = FALSE;
277 
278   g_return_val_if_fail (property_name != NULL, FALSE);
279 
280   node = _gtk_style_property_lookup (property_name);
281 
282   if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
283     {
284       GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);
285 
286       if (pspec)
287         *pspec = custom->pspec;
288 
289       if (parse_func)
290         *parse_func = custom->property_parse_func;
291 
292       found = TRUE;
293     }
294 
295   return found;
296 }
297 
298