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