]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyleproperties.c
styleproperty: Remove unuse args from register()
[~andy/gtk] / gtk / gtkstyleproperties.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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 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
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include "gtkstylepropertiesprivate.h"
23
24 #include <stdlib.h>
25 #include <gobject/gvaluecollector.h>
26 #include <cairo-gobject.h>
27
28 #include "gtkstyleprovider.h"
29 #include "gtksymboliccolor.h"
30 #include "gtkthemingengine.h"
31 #include "gtkanimationdescription.h"
32 #include "gtkgradient.h"
33 #include "gtkshadowprivate.h"
34 #include "gtkcssshorthandpropertyprivate.h"
35 #include "gtkcsstypesprivate.h"
36 #include "gtkborderimageprivate.h"
37
38 #include "gtkprivatetypebuiltins.h"
39 #include "gtkstylepropertyprivate.h"
40 #include "gtkstyleproviderprivate.h"
41 #include "gtkintl.h"
42
43 #include "gtkwin32themeprivate.h"
44
45 /**
46  * SECTION:gtkstyleproperties
47  * @Short_description: Store for style property information
48  * @Title: GtkStyleProperties
49  *
50  * GtkStyleProperties provides the storage for style information
51  * that is used by #GtkStyleContext and other #GtkStyleProvider
52  * implementations.
53  *
54  * Before style properties can be stored in GtkStyleProperties, they
55  * must be registered with gtk_style_properties_register_property().
56  *
57  * Unless you are writing a #GtkStyleProvider implementation, you
58  * are unlikely to use this API directly, as gtk_style_context_get()
59  * and its variants are the preferred way to access styling information
60  * from widget implementations and theming engine implementations
61  * should use the APIs provided by #GtkThemingEngine instead.
62  */
63
64 typedef struct PropertyData PropertyData;
65 typedef struct ValueData ValueData;
66
67 struct ValueData
68 {
69   GtkStateFlags state;
70   GValue value;
71 };
72
73 struct PropertyData
74 {
75   GArray *values;
76 };
77
78 struct _GtkStylePropertiesPrivate
79 {
80   GHashTable *color_map;
81   GHashTable *properties;
82   GtkSymbolicColorLookupFunc color_lookup_func;
83   gpointer color_lookup_data;
84 };
85
86 static void gtk_style_properties_provider_init         (GtkStyleProviderIface            *iface);
87 static void gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface);
88 static void gtk_style_properties_finalize              (GObject                          *object);
89
90
91 G_DEFINE_TYPE_EXTENDED (GtkStyleProperties, gtk_style_properties, G_TYPE_OBJECT, 0,
92                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
93                                                gtk_style_properties_provider_init)
94                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
95                                                gtk_style_properties_provider_private_init));
96
97 static void
98 gtk_style_properties_class_init (GtkStylePropertiesClass *klass)
99 {
100   GObjectClass *object_class = G_OBJECT_CLASS (klass);
101
102   object_class->finalize = gtk_style_properties_finalize;
103
104   g_type_class_add_private (object_class, sizeof (GtkStylePropertiesPrivate));
105 }
106
107 static PropertyData *
108 property_data_new (void)
109 {
110   PropertyData *data;
111
112   data = g_slice_new0 (PropertyData);
113   data->values = g_array_new (FALSE, FALSE, sizeof (ValueData));
114
115   return data;
116 }
117
118 static void
119 property_data_remove_values (PropertyData *data)
120 {
121   guint i;
122
123   for (i = 0; i < data->values->len; i++)
124     {
125       ValueData *value_data;
126
127       value_data = &g_array_index (data->values, ValueData, i);
128
129       if (G_IS_VALUE (&value_data->value))
130         g_value_unset (&value_data->value);
131     }
132
133   if (data->values->len > 0)
134     g_array_remove_range (data->values, 0, data->values->len);
135 }
136
137 static void
138 property_data_free (PropertyData *data)
139 {
140   property_data_remove_values (data);
141   g_array_free (data->values, TRUE);
142   g_slice_free (PropertyData, data);
143 }
144
145 static gboolean
146 property_data_find_position (PropertyData  *data,
147                              GtkStateFlags  state,
148                              guint         *pos)
149 {
150   gint min, max, mid;
151   gboolean found = FALSE;
152   guint position;
153
154   if (pos)
155     *pos = 0;
156
157   if (data->values->len == 0)
158     return FALSE;
159
160   /* Find position for the given state, or the position where
161    * it would be if not found, the array is ordered by the
162    * state flags.
163    */
164   min = 0;
165   max = data->values->len - 1;
166
167   do
168     {
169       ValueData *value_data;
170
171       mid = (min + max) / 2;
172       value_data = &g_array_index (data->values, ValueData, mid);
173
174       if (value_data->state == state)
175         {
176           found = TRUE;
177           position = mid;
178         }
179       else if (value_data->state < state)
180           position = min = mid + 1;
181       else
182         {
183           max = mid - 1;
184           position = mid;
185         }
186     }
187   while (!found && min <= max);
188
189   if (pos)
190     *pos = position;
191
192   return found;
193 }
194
195 static GValue *
196 property_data_get_value (PropertyData  *data,
197                          GtkStateFlags  state)
198 {
199   ValueData *val_data;
200   guint pos;
201
202   if (!property_data_find_position (data, state, &pos))
203     {
204       ValueData new = { 0 };
205
206       new.state = state;
207       g_array_insert_val (data->values, pos, new);
208     }
209
210   val_data = &g_array_index (data->values, ValueData, pos);
211
212   return &val_data->value;
213 }
214
215 static GValue *
216 property_data_match_state (PropertyData  *data,
217                            GtkStateFlags  state)
218 {
219   guint pos;
220   gint i;
221
222   if (property_data_find_position (data, state, &pos))
223     {
224       ValueData *val_data;
225
226       /* Exact match */
227       val_data = &g_array_index (data->values, ValueData, pos);
228       return &val_data->value;
229     }
230
231   if (pos >= data->values->len)
232     pos = data->values->len - 1;
233
234   /* No exact match, go downwards the list to find
235    * the closest match to the given state flags, as
236    * a side effect, there is an implicit precedence
237    * of higher flags over the smaller ones.
238    */
239   for (i = pos; i >= 0; i--)
240     {
241       ValueData *val_data;
242
243       val_data = &g_array_index (data->values, ValueData, i);
244
245        /* Check whether any of the requested
246         * flags are set, and no other flags are.
247         *
248         * Also, no flags acts as a wildcard, such
249         * value should be always in the first position
250         * in the array (if present) anyways.
251         */
252       if (val_data->state == 0 ||
253           ((val_data->state & state) != 0 &&
254            (val_data->state & ~state) == 0))
255         return &val_data->value;
256     }
257
258   return NULL;
259 }
260
261 static void
262 gtk_style_properties_init (GtkStyleProperties *props)
263 {
264   GtkStylePropertiesPrivate *priv;
265
266   priv = props->priv = G_TYPE_INSTANCE_GET_PRIVATE (props,
267                                                     GTK_TYPE_STYLE_PROPERTIES,
268                                                     GtkStylePropertiesPrivate);
269
270   priv->properties = g_hash_table_new_full (NULL, NULL, NULL,
271                                             (GDestroyNotify) property_data_free);
272 }
273
274 static void
275 gtk_style_properties_finalize (GObject *object)
276 {
277   GtkStylePropertiesPrivate *priv;
278   GtkStyleProperties *props;
279
280   props = GTK_STYLE_PROPERTIES (object);
281   priv = props->priv;
282   g_hash_table_destroy (priv->properties);
283
284   if (priv->color_map)
285     g_hash_table_destroy (priv->color_map);
286
287   G_OBJECT_CLASS (gtk_style_properties_parent_class)->finalize (object);
288 }
289
290 GtkStyleProperties *
291 gtk_style_properties_get_style (GtkStyleProvider *provider,
292                                 GtkWidgetPath    *path)
293 {
294   /* Return style set itself */
295   return g_object_ref (provider);
296 }
297
298 static void
299 gtk_style_properties_provider_init (GtkStyleProviderIface *iface)
300 {
301   iface->get_style = gtk_style_properties_get_style;
302 }
303
304 static GtkSymbolicColor *
305 gtk_style_properties_provider_get_color (GtkStyleProviderPrivate *provider,
306                                          const char              *name)
307 {
308   return gtk_style_properties_lookup_color (GTK_STYLE_PROPERTIES (provider), name);
309 }
310
311 static void
312 gtk_style_properties_provider_lookup (GtkStyleProviderPrivate *provider,
313                                       GtkWidgetPath           *path,
314                                       GtkStateFlags            state,
315                                       GtkCssLookup            *lookup)
316 {
317   GtkStyleProperties *props;
318   GtkStylePropertiesPrivate *priv;
319   GHashTableIter iter;
320   gpointer key, value;
321
322   props = GTK_STYLE_PROPERTIES (provider);
323   priv = props->priv;
324
325   /* Merge symbolic style properties */
326   g_hash_table_iter_init (&iter, priv->properties);
327
328   while (g_hash_table_iter_next (&iter, &key, &value))
329     {
330       GtkStyleProperty *prop = key;
331       PropertyData *data = value;
332       const GValue *value;
333       guint id;
334
335       id = _gtk_style_property_get_id (prop);
336
337       if (!_gtk_css_lookup_is_missing (lookup, id))
338           continue;
339
340       value = property_data_match_state (data, state);
341       if (value == NULL)
342         continue;
343
344       _gtk_css_lookup_set (lookup, id, value);
345     }
346 }
347
348 static void
349 gtk_style_properties_provider_private_init (GtkStyleProviderPrivateInterface *iface)
350 {
351   iface->get_color = gtk_style_properties_provider_get_color;
352   iface->lookup = gtk_style_properties_provider_lookup;
353 }
354
355 /* Property registration functions */
356
357 /**
358  * gtk_style_properties_register_property: (skip)
359  * @parse_func: parsing function to use, or %NULL
360  * @pspec: the #GParamSpec for the new property
361  *
362  * Registers a property so it can be used in the CSS file format.
363  * This function is the low-level equivalent of
364  * gtk_theming_engine_register_property(), if you are implementing
365  * a theming engine, you want to use that function instead.
366  *
367  * Since: 3.0
368  **/
369 void
370 gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
371                                         GParamSpec             *pspec)
372 {
373   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
374
375   _gtk_style_property_register (pspec,
376                                 0,
377                                 parse_func,
378                                 NULL,
379                                 NULL,
380                                 NULL);
381 }
382
383 /**
384  * gtk_style_properties_lookup_property: (skip)
385  * @property_name: property name to look up
386  * @parse_func: (out): return location for the parse function
387  * @pspec: (out) (transfer none): return location for the #GParamSpec
388  *
389  * Returns %TRUE if a property has been registered, if @pspec or
390  * @parse_func are not %NULL, the #GParamSpec and parsing function
391  * will be respectively returned.
392  *
393  * Returns: %TRUE if the property is registered, %FALSE otherwise
394  *
395  * Since: 3.0
396  **/
397 gboolean
398 gtk_style_properties_lookup_property (const gchar             *property_name,
399                                       GtkStylePropertyParser  *parse_func,
400                                       GParamSpec             **pspec)
401 {
402   const GtkStyleProperty *node;
403   gboolean found = FALSE;
404
405   g_return_val_if_fail (property_name != NULL, FALSE);
406
407   node = _gtk_style_property_lookup (property_name);
408
409   if (node)
410     {
411       if (pspec)
412         *pspec = node->pspec;
413
414       if (parse_func)
415         *parse_func = node->property_parse_func;
416
417       found = TRUE;
418     }
419
420   return found;
421 }
422
423 /* GtkStyleProperties methods */
424
425 /**
426  * gtk_style_properties_new:
427  *
428  * Returns a newly created #GtkStyleProperties
429  *
430  * Returns: a new #GtkStyleProperties
431  **/
432 GtkStyleProperties *
433 gtk_style_properties_new (void)
434 {
435   return g_object_new (GTK_TYPE_STYLE_PROPERTIES, NULL);
436 }
437
438 void
439 _gtk_style_properties_set_color_lookup_func (GtkStyleProperties         *props,
440                                              GtkSymbolicColorLookupFunc  func,
441                                              gpointer                    data)
442 {
443   GtkStylePropertiesPrivate *priv;
444
445   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
446   g_return_if_fail (func != NULL);
447
448   priv = props->priv;
449   g_return_if_fail (priv->color_map == NULL);
450
451   priv->color_lookup_func = func;
452   priv->color_lookup_data = data;
453 }
454
455 /**
456  * gtk_style_properties_map_color:
457  * @props: a #GtkStyleProperties
458  * @name: color name
459  * @color: #GtkSymbolicColor to map @name to
460  *
461  * Maps @color so it can be referenced by @name. See
462  * gtk_style_properties_lookup_color()
463  *
464  * Since: 3.0
465  **/
466 void
467 gtk_style_properties_map_color (GtkStyleProperties *props,
468                                 const gchar        *name,
469                                 GtkSymbolicColor   *color)
470 {
471   GtkStylePropertiesPrivate *priv;
472
473   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
474   g_return_if_fail (name != NULL);
475   g_return_if_fail (color != NULL);
476
477   priv = props->priv;
478   g_return_if_fail (priv->color_lookup_func == NULL);
479
480   if (G_UNLIKELY (!priv->color_map))
481     priv->color_map = g_hash_table_new_full (g_str_hash,
482                                              g_str_equal,
483                                              (GDestroyNotify) g_free,
484                                              (GDestroyNotify) gtk_symbolic_color_unref);
485
486   g_hash_table_replace (priv->color_map,
487                         g_strdup (name),
488                         gtk_symbolic_color_ref (color));
489 }
490
491 /**
492  * gtk_style_properties_lookup_color:
493  * @props: a #GtkStyleProperties
494  * @name: color name to lookup
495  *
496  * Returns the symbolic color that is mapped
497  * to @name.
498  *
499  * Returns: (transfer none): The mapped color
500  *
501  * Since: 3.0
502  **/
503 GtkSymbolicColor *
504 gtk_style_properties_lookup_color (GtkStyleProperties *props,
505                                    const gchar        *name)
506 {
507   GtkStylePropertiesPrivate *priv;
508
509   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), NULL);
510   g_return_val_if_fail (name != NULL, NULL);
511
512   priv = props->priv;
513
514   if (priv->color_lookup_func)
515     return priv->color_lookup_func (priv->color_lookup_data, name);
516
517   if (!priv->color_map)
518     return NULL;
519
520   return g_hash_table_lookup (priv->color_map, name);
521 }
522
523 void
524 _gtk_style_properties_set_property_by_property (GtkStyleProperties     *props,
525                                                 const GtkStyleProperty *style_prop,
526                                                 GtkStateFlags           state,
527                                                 const GValue           *value)
528 {
529   GtkStylePropertiesPrivate *priv;
530   PropertyData *prop;
531   GType value_type;
532   GValue *val;
533
534   value_type = G_VALUE_TYPE (value);
535
536   if (style_prop->pspec->value_type == GDK_TYPE_RGBA ||
537       style_prop->pspec->value_type == GDK_TYPE_COLOR)
538     {
539       /* Allow GtkSymbolicColor and special values as well */
540       g_return_if_fail (value_type == GDK_TYPE_RGBA ||
541                         value_type == GDK_TYPE_COLOR ||
542                         value_type == GTK_TYPE_CSS_SPECIAL_VALUE ||
543                         value_type == GTK_TYPE_SYMBOLIC_COLOR);
544     }
545   else if (style_prop->pspec->value_type == CAIRO_GOBJECT_TYPE_PATTERN)
546     {
547       /* Allow GtkGradient and theme part as a substitute */
548       g_return_if_fail (value_type == CAIRO_GOBJECT_TYPE_PATTERN ||
549                         value_type == GTK_TYPE_GRADIENT ||
550                         value_type == GTK_TYPE_WIN32_THEME_PART);
551     }
552   else if (style_prop->pspec->value_type == G_TYPE_INT)
553     {
554       g_return_if_fail (value_type == G_TYPE_INT ||
555                         value_type == GTK_TYPE_CSS_BORDER_RADIUS);
556     }
557   else
558     g_return_if_fail (style_prop->pspec->value_type == value_type);
559
560   if (GTK_IS_CSS_SHORTHAND_PROPERTY (style_prop))
561     {
562       GParameter *parameters;
563       guint i, n_parameters;
564
565       parameters = _gtk_style_property_unpack (style_prop, value, &n_parameters);
566
567       for (i = 0; i < n_parameters; i++)
568         {
569           gtk_style_properties_set_property (props,
570                                              parameters[i].name,
571                                              state,
572                                              &parameters[i].value);
573           g_value_unset (&parameters[i].value);
574         }
575       g_free (parameters);
576       return;
577     }
578
579   priv = props->priv;
580   prop = g_hash_table_lookup (priv->properties, style_prop);
581
582   if (!prop)
583     {
584       prop = property_data_new ();
585       g_hash_table_insert (priv->properties, (gpointer) style_prop, prop);
586     }
587
588   val = property_data_get_value (prop, state);
589
590   if (G_VALUE_TYPE (val) == value_type)
591     g_value_reset (val);
592   else
593     {
594       if (G_IS_VALUE (val))
595         g_value_unset (val);
596
597       g_value_init (val, value_type);
598     }
599
600   g_value_copy (value, val);
601   if (style_prop->pspec->value_type == value_type)
602     g_param_value_validate (style_prop->pspec, val);
603 }
604
605 /**
606  * gtk_style_properties_set_property:
607  * @props: a #GtkStyleProperties
608  * @property: styling property to set
609  * @state: state to set the value for
610  * @value: new value for the property
611  *
612  * Sets a styling property in @props.
613  *
614  * Since: 3.0
615  **/
616 void
617 gtk_style_properties_set_property (GtkStyleProperties *props,
618                                    const gchar        *property,
619                                    GtkStateFlags       state,
620                                    const GValue       *value)
621 {
622   const GtkStyleProperty *node;
623
624   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
625   g_return_if_fail (property != NULL);
626   g_return_if_fail (value != NULL);
627
628   node = _gtk_style_property_lookup (property);
629
630   if (!node)
631     {
632       g_warning ("Style property \"%s\" is not registered", property);
633       return;
634     }
635
636   _gtk_style_properties_set_property_by_property (props,
637                                                   node,
638                                                   state,
639                                                   value);
640 }
641
642 /**
643  * gtk_style_properties_set_valist:
644  * @props: a #GtkStyleProperties
645  * @state: state to set the values for
646  * @args: va_list of property name/value pairs, followed by %NULL
647  *
648  * Sets several style properties on @props.
649  *
650  * Since: 3.0
651  **/
652 void
653 gtk_style_properties_set_valist (GtkStyleProperties *props,
654                                  GtkStateFlags       state,
655                                  va_list             args)
656 {
657   const gchar *property_name;
658
659   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
660
661   property_name = va_arg (args, const gchar *);
662
663   while (property_name)
664     {
665       const GtkStyleProperty *node;
666       gchar *error = NULL;
667       GValue val = G_VALUE_INIT;
668
669       node = _gtk_style_property_lookup (property_name);
670
671       if (!node)
672         {
673           g_warning ("Style property \"%s\" is not registered", property_name);
674           break;
675         }
676
677       G_VALUE_COLLECT_INIT (&val, node->pspec->value_type,
678                             args, 0, &error);
679       if (error)
680         {
681           g_warning ("Could not set style property \"%s\": %s", property_name, error);
682           g_value_unset (&val);
683           g_free (error);
684           break;
685         }
686
687       _gtk_style_properties_set_property_by_property (props, node, state, &val);
688       g_value_unset (&val);
689
690       property_name = va_arg (args, const gchar *);
691     }
692 }
693
694 /**
695  * gtk_style_properties_set:
696  * @props: a #GtkStyleProperties
697  * @state: state to set the values for
698  * @...: property name/value pairs, followed by %NULL
699  *
700  * Sets several style properties on @props.
701  *
702  * Since: 3.0
703  **/
704 void
705 gtk_style_properties_set (GtkStyleProperties *props,
706                           GtkStateFlags       state,
707                           ...)
708 {
709   va_list args;
710
711   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
712
713   va_start (args, state);
714   gtk_style_properties_set_valist (props, state, args);
715   va_end (args);
716 }
717
718 const GValue *
719 _gtk_style_properties_peek_property (GtkStyleProperties      *props,
720                                      const GtkStyleProperty  *property,
721                                      GtkStateFlags            state)
722 {
723   GtkStylePropertiesPrivate *priv;
724   PropertyData *prop;
725
726   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
727   g_return_val_if_fail (property != NULL, FALSE);
728
729   priv = props->priv;
730   prop = g_hash_table_lookup (priv->properties, property);
731   if (prop == NULL)
732     return NULL;
733
734   return property_data_match_state (prop, state);
735 }
736
737 gboolean
738 _gtk_style_properties_get_property (GtkStyleProperties *props,
739                                     const gchar        *property,
740                                     GtkStateFlags       state,
741                                     GtkStylePropertyContext *context,
742                                     GValue             *value)
743 {
744   const GtkStyleProperty *node;
745
746   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
747   g_return_val_if_fail (property != NULL, FALSE);
748   g_return_val_if_fail (value != NULL, FALSE);
749
750   node = _gtk_style_property_lookup (property);
751   if (!node)
752     {
753       g_warning ("Style property \"%s\" is not registered", property);
754       return FALSE;
755     }
756
757   _gtk_style_property_query (node, props, state, context, value);
758   return TRUE;
759 }
760
761 /**
762  * gtk_style_properties_get_property:
763  * @props: a #GtkStyleProperties
764  * @property: style property name
765  * @state: state to retrieve the property value for
766  * @value: (out) (transfer full):  return location for the style property value.
767  *
768  * Gets a style property from @props for the given state. When done with @value,
769  * g_value_unset() needs to be called to free any allocated memory.
770  *
771  * Returns: %TRUE if the property exists in @props, %FALSE otherwise
772  *
773  * Since: 3.0
774  **/
775 gboolean
776 gtk_style_properties_get_property (GtkStyleProperties *props,
777                                    const gchar        *property,
778                                    GtkStateFlags       state,
779                                    GValue             *value)
780 {
781   GtkStylePropertyContext context = { 100, 100};
782
783   g_return_val_if_fail (GTK_IS_STYLE_PROPERTIES (props), FALSE);
784   g_return_val_if_fail (property != NULL, FALSE);
785   g_return_val_if_fail (value != NULL, FALSE);
786
787   return _gtk_style_properties_get_property (props,
788                                              property,
789                                              state, &context, value);
790 }
791
792 void
793 _gtk_style_properties_get_valist (GtkStyleProperties *props,
794                                   GtkStateFlags       state,
795                                   GtkStylePropertyContext *context,
796                                   va_list             args)
797 {
798   const gchar *property_name;
799
800   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
801
802   property_name = va_arg (args, const gchar *);
803
804   while (property_name)
805     {
806       gchar *error = NULL;
807       GValue value = G_VALUE_INIT;
808
809       if (!_gtk_style_properties_get_property (props,
810                                                property_name,
811                                                state,
812                                                context,
813                                                &value))
814         break;
815
816       G_VALUE_LCOPY (&value, args, 0, &error);
817       g_value_unset (&value);
818
819       if (error)
820         {
821           g_warning ("Could not get style property \"%s\": %s", property_name, error);
822           g_free (error);
823           break;
824         }
825
826       property_name = va_arg (args, const gchar *);
827     }
828 }
829
830 /**
831  * gtk_style_properties_get_valist:
832  * @props: a #GtkStyleProperties
833  * @state: state to retrieve the property values for
834  * @args: va_list of property name/return location pairs, followed by %NULL
835  *
836  * Retrieves several style property values from @props for a given state.
837  *
838  * Since: 3.0
839  **/
840 void
841 gtk_style_properties_get_valist (GtkStyleProperties *props,
842                                  GtkStateFlags       state,
843                                  va_list             args)
844 {
845   GtkStylePropertyContext context = { 100, 100};
846   
847   return _gtk_style_properties_get_valist (props, state, &context, args);
848 }
849
850 void
851 _gtk_style_properties_get (GtkStyleProperties *props,
852                            GtkStateFlags       state,
853                            GtkStylePropertyContext *context,
854                            ...)
855 {
856   va_list args;
857
858   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
859
860   va_start (args, context);
861   _gtk_style_properties_get_valist (props, state, context, args);
862   va_end (args);
863 }
864
865 /**
866  * gtk_style_properties_get:
867  * @props: a #GtkStyleProperties
868  * @state: state to retrieve the property values for
869  * @...: property name /return value pairs, followed by %NULL
870  *
871  * Retrieves several style property values from @props for a
872  * given state.
873  *
874  * Since: 3.0
875  **/
876 void
877 gtk_style_properties_get (GtkStyleProperties *props,
878                           GtkStateFlags       state,
879                           ...)
880 {
881   va_list args;
882
883   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
884
885   va_start (args, state);
886   gtk_style_properties_get_valist (props, state, args);
887   va_end (args);
888 }
889
890 /**
891  * gtk_style_properties_unset_property:
892  * @props: a #GtkStyleProperties
893  * @property: property to unset
894  * @state: state to unset
895  *
896  * Unsets a style property in @props.
897  *
898  * Since: 3.0
899  **/
900 void
901 gtk_style_properties_unset_property (GtkStyleProperties *props,
902                                      const gchar        *property,
903                                      GtkStateFlags       state)
904 {
905   GtkStylePropertiesPrivate *priv;
906   const GtkStyleProperty *node;
907   PropertyData *prop;
908   guint pos;
909
910   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
911   g_return_if_fail (property != NULL);
912
913   node = _gtk_style_property_lookup (property);
914
915   if (!node)
916     {
917       g_warning ("Style property \"%s\" is not registered", property);
918       return;
919     }
920
921   if (node->unset_func)
922     {
923       node->unset_func (props, state);
924       return;
925     }
926
927   priv = props->priv;
928   prop = g_hash_table_lookup (priv->properties, node);
929
930   if (!prop)
931     return;
932
933   if (property_data_find_position (prop, state, &pos))
934     {
935       ValueData *data;
936
937       data = &g_array_index (prop->values, ValueData, pos);
938
939       if (G_IS_VALUE (&data->value))
940         g_value_unset (&data->value);
941
942       g_array_remove_index (prop->values, pos);
943     }
944 }
945
946 /**
947  * gtk_style_properties_clear:
948  * @props: a #GtkStyleProperties
949  *
950  * Clears all style information from @props.
951  **/
952 void
953 gtk_style_properties_clear (GtkStyleProperties *props)
954 {
955   GtkStylePropertiesPrivate *priv;
956
957   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
958
959   priv = props->priv;
960   g_hash_table_remove_all (priv->properties);
961 }
962
963 /**
964  * gtk_style_properties_merge:
965  * @props: a #GtkStyleProperties
966  * @props_to_merge: a second #GtkStyleProperties
967  * @replace: whether to replace values or not
968  *
969  * Merges into @props all the style information contained
970  * in @props_to_merge. If @replace is %TRUE, the values
971  * will be overwritten, if it is %FALSE, the older values
972  * will prevail.
973  *
974  * Since: 3.0
975  **/
976 void
977 gtk_style_properties_merge (GtkStyleProperties       *props,
978                             const GtkStyleProperties *props_to_merge,
979                             gboolean                  replace)
980 {
981   GtkStylePropertiesPrivate *priv, *priv_to_merge;
982   GHashTableIter iter;
983   gpointer key, value;
984
985   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
986   g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props_to_merge));
987
988   priv = props->priv;
989   priv_to_merge = props_to_merge->priv;
990
991   /* Merge symbolic color map */
992   if (priv_to_merge->color_map)
993     {
994       g_hash_table_iter_init (&iter, priv_to_merge->color_map);
995
996       while (g_hash_table_iter_next (&iter, &key, &value))
997         {
998           const gchar *name;
999           GtkSymbolicColor *color;
1000
1001           name = key;
1002           color = value;
1003
1004           if (!replace &&
1005               g_hash_table_lookup (priv->color_map, name))
1006             continue;
1007
1008           gtk_style_properties_map_color (props, name, color);
1009         }
1010     }
1011
1012   /* Merge symbolic style properties */
1013   g_hash_table_iter_init (&iter, priv_to_merge->properties);
1014
1015   while (g_hash_table_iter_next (&iter, &key, &value))
1016     {
1017       PropertyData *prop_to_merge = value;
1018       PropertyData *prop;
1019       guint i;
1020
1021       prop = g_hash_table_lookup (priv->properties, key);
1022
1023       if (!prop)
1024         {
1025           prop = property_data_new ();
1026           g_hash_table_insert (priv->properties, key, prop);
1027         }
1028
1029       for (i = 0; i < prop_to_merge->values->len; i++)
1030         {
1031           ValueData *data;
1032           GValue *value;
1033
1034           data = &g_array_index (prop_to_merge->values, ValueData, i);
1035
1036           if (replace && data->state == GTK_STATE_FLAG_NORMAL &&
1037               G_VALUE_TYPE (&data->value) != PANGO_TYPE_FONT_DESCRIPTION)
1038             {
1039               /* Let normal state override all states
1040                * previously set in the original set
1041                */
1042               property_data_remove_values (prop);
1043             }
1044
1045           value = property_data_get_value (prop, data->state);
1046
1047           if (G_VALUE_TYPE (&data->value) == PANGO_TYPE_FONT_DESCRIPTION &&
1048               G_IS_VALUE (value))
1049             {
1050               PangoFontDescription *font_desc;
1051               PangoFontDescription *font_desc_to_merge;
1052
1053               /* Handle merging of font descriptions */
1054               font_desc = g_value_get_boxed (value);
1055               font_desc_to_merge = g_value_get_boxed (&data->value);
1056
1057               pango_font_description_merge (font_desc, font_desc_to_merge, replace);
1058             }
1059           else if (G_VALUE_TYPE (&data->value) == G_TYPE_PTR_ARRAY &&
1060                    G_IS_VALUE (value))
1061             {
1062               GPtrArray *array, *array_to_merge;
1063               gint i;
1064
1065               /* Append the array, mainly thought
1066                * for the gtk-key-bindings property
1067                */
1068               array = g_value_get_boxed (value);
1069               array_to_merge = g_value_get_boxed (&data->value);
1070
1071               for (i = 0; i < array_to_merge->len; i++)
1072                 g_ptr_array_add (array, g_ptr_array_index (array_to_merge, i));
1073             }
1074           else if (replace || !G_IS_VALUE (value))
1075             {
1076               if (!G_IS_VALUE (value))
1077                 g_value_init (value, G_VALUE_TYPE (&data->value));
1078               else if (G_VALUE_TYPE (value) != G_VALUE_TYPE (&data->value))
1079                 {
1080                   g_value_unset (value);
1081                   g_value_init (value, G_VALUE_TYPE (&data->value));
1082                 }
1083
1084               g_value_copy (&data->value, value);
1085             }
1086         }
1087     }
1088 }