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