]> Pileus Git - ~andy/gtk/blob - gtk/gtksettings.c
Bug 590959 - Set child_has_focus flag properly (second try)
[~andy/gtk] / gtk / gtksettings.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 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 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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #define PANGO_ENABLE_BACKEND /* for pango_fc_font_map_cache_clear() */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include "gtkmodules.h"
26 #include "gtksettings.h"
27 #include "gtkrc.h"
28 #include "gtkintl.h"
29 #include "gtkwidget.h"
30 #include "gtkprivate.h"
31 #include "gtkalias.h"
32
33 #ifdef GDK_WINDOWING_X11
34 #include "x11/gdkx.h"
35 #include <pango/pangofc-fontmap.h>
36 #endif
37
38 #define DEFAULT_TIMEOUT_INITIAL 200
39 #define DEFAULT_TIMEOUT_REPEAT   20
40 #define DEFAULT_TIMEOUT_EXPAND  500
41
42 typedef struct _GtkSettingsValuePrivate GtkSettingsValuePrivate;
43
44 typedef enum
45 {
46   GTK_SETTINGS_SOURCE_DEFAULT,
47   GTK_SETTINGS_SOURCE_RC_FILE,
48   GTK_SETTINGS_SOURCE_XSETTING,
49   GTK_SETTINGS_SOURCE_APPLICATION
50 } GtkSettingsSource;
51
52 struct _GtkSettingsValuePrivate
53 {
54   GtkSettingsValue public;
55   GtkSettingsSource source;
56 };
57
58 struct _GtkSettingsPropertyValue
59 {
60   GValue value;
61   GtkSettingsSource source;
62 };
63
64 enum {
65   PROP_0,
66   PROP_DOUBLE_CLICK_TIME,
67   PROP_DOUBLE_CLICK_DISTANCE,
68   PROP_CURSOR_BLINK,
69   PROP_CURSOR_BLINK_TIME,
70   PROP_CURSOR_BLINK_TIMEOUT,
71   PROP_SPLIT_CURSOR,
72   PROP_THEME_NAME,
73   PROP_ICON_THEME_NAME,
74   PROP_FALLBACK_ICON_THEME,
75   PROP_KEY_THEME_NAME,
76   PROP_MENU_BAR_ACCEL,
77   PROP_DND_DRAG_THRESHOLD,
78   PROP_FONT_NAME,
79   PROP_ICON_SIZES,
80   PROP_MODULES,
81 #ifdef GDK_WINDOWING_X11
82   PROP_XFT_ANTIALIAS,
83   PROP_XFT_HINTING,
84   PROP_XFT_HINTSTYLE,
85   PROP_XFT_RGBA,
86   PROP_XFT_DPI,
87   PROP_CURSOR_THEME_NAME,
88   PROP_CURSOR_THEME_SIZE,
89 #endif
90   PROP_ALTERNATIVE_BUTTON_ORDER,
91   PROP_ALTERNATIVE_SORT_ARROWS,
92   PROP_SHOW_INPUT_METHOD_MENU,
93   PROP_SHOW_UNICODE_MENU,
94   PROP_TIMEOUT_INITIAL,
95   PROP_TIMEOUT_REPEAT,
96   PROP_TIMEOUT_EXPAND,
97   PROP_COLOR_SCHEME,
98   PROP_ENABLE_ANIMATIONS,
99   PROP_TOUCHSCREEN_MODE,
100   PROP_TOOLTIP_TIMEOUT,
101   PROP_TOOLTIP_BROWSE_TIMEOUT,
102   PROP_TOOLTIP_BROWSE_MODE_TIMEOUT,
103   PROP_KEYNAV_CURSOR_ONLY,
104   PROP_KEYNAV_WRAP_AROUND,
105   PROP_ERROR_BELL,
106   PROP_COLOR_HASH,
107   PROP_FILE_CHOOSER_BACKEND,
108   PROP_PRINT_BACKENDS,
109   PROP_PRINT_PREVIEW_COMMAND,
110   PROP_ENABLE_MNEMONICS,
111   PROP_ENABLE_ACCELS,
112   PROP_RECENT_FILES_LIMIT,
113   PROP_IM_MODULE,
114   PROP_RECENT_FILES_MAX_AGE,
115   PROP_FONTCONFIG_TIMESTAMP,
116   PROP_SOUND_THEME_NAME,
117   PROP_ENABLE_INPUT_FEEDBACK_SOUNDS,
118   PROP_ENABLE_EVENT_SOUNDS,
119   PROP_ENABLE_TOOLTIPS
120 };
121
122
123 /* --- prototypes --- */
124 static void     gtk_settings_finalize            (GObject               *object);
125 static void     gtk_settings_get_property        (GObject               *object,
126                                                   guint                  property_id,
127                                                   GValue                *value,
128                                                   GParamSpec            *pspec);
129 static void     gtk_settings_set_property        (GObject               *object,
130                                                   guint                  property_id,
131                                                   const GValue          *value,
132                                                   GParamSpec            *pspec);
133 static void     gtk_settings_notify              (GObject               *object,
134                                                   GParamSpec            *pspec);
135 static guint    settings_install_property_parser (GtkSettingsClass      *class,
136                                                   GParamSpec            *pspec,
137                                                   GtkRcPropertyParser    parser);
138 static void    settings_update_double_click      (GtkSettings           *settings);
139 static void    settings_update_modules           (GtkSettings           *settings);
140
141 #ifdef GDK_WINDOWING_X11
142 static void    settings_update_cursor_theme      (GtkSettings           *settings);
143 static void    settings_update_resolution        (GtkSettings           *settings);
144 static void    settings_update_font_options      (GtkSettings           *settings);
145 static gboolean settings_update_fontconfig       (GtkSettings           *settings);
146 #endif
147 static void    settings_update_color_scheme      (GtkSettings *settings);
148
149 static void    merge_color_scheme                (GtkSettings           *settings, 
150                                                   const GValue          *value, 
151                                                   GtkSettingsSource      source);
152 static gchar  *get_color_scheme                  (GtkSettings           *settings);
153 static GHashTable *get_color_hash                (GtkSettings           *settings);
154
155
156 /* --- variables --- */
157 static GQuark            quark_property_parser = 0;
158 static GSList           *object_list = NULL;
159 static guint             class_n_properties = 0;
160
161
162 G_DEFINE_TYPE (GtkSettings, gtk_settings, G_TYPE_OBJECT)
163
164 /* --- functions --- */
165 static void
166 gtk_settings_init (GtkSettings *settings)
167 {
168   GParamSpec **pspecs, **p;
169   guint i = 0;
170   
171   g_datalist_init (&settings->queued_settings);
172   object_list = g_slist_prepend (object_list, settings);
173
174   /* build up property array for all yet existing properties and queue
175    * notification for them (at least notification for internal properties
176    * will instantly be caught)
177    */
178   pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (settings), NULL);
179   for (p = pspecs; *p; p++)
180     if ((*p)->owner_type == G_OBJECT_TYPE (settings))
181       i++;
182   settings->property_values = g_new0 (GtkSettingsPropertyValue, i);
183   i = 0;
184   g_object_freeze_notify (G_OBJECT (settings));
185   for (p = pspecs; *p; p++)
186     {
187       GParamSpec *pspec = *p;
188
189       if (pspec->owner_type != G_OBJECT_TYPE (settings))
190         continue;
191       g_value_init (&settings->property_values[i].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
192       g_param_value_set_default (pspec, &settings->property_values[i].value);
193       g_object_notify (G_OBJECT (settings), pspec->name);
194       settings->property_values[i].source = GTK_SETTINGS_SOURCE_DEFAULT;
195       i++;
196     }
197   g_object_thaw_notify (G_OBJECT (settings));
198   g_free (pspecs);
199 }
200
201 static void
202 gtk_settings_class_init (GtkSettingsClass *class)
203 {
204   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
205   guint result;
206   
207   gobject_class->finalize = gtk_settings_finalize;
208   gobject_class->get_property = gtk_settings_get_property;
209   gobject_class->set_property = gtk_settings_set_property;
210   gobject_class->notify = gtk_settings_notify;
211
212   quark_property_parser = g_quark_from_static_string ("gtk-rc-property-parser");
213   result = settings_install_property_parser (class,
214                                              g_param_spec_int ("gtk-double-click-time",
215                                                                P_("Double Click Time"),
216                                                                P_("Maximum time allowed between two clicks for them to be considered a double click (in milliseconds)"),
217                                                                0, G_MAXINT, 250,
218                                                                GTK_PARAM_READWRITE),
219                                              NULL);
220   g_assert (result == PROP_DOUBLE_CLICK_TIME);
221   result = settings_install_property_parser (class,
222                                              g_param_spec_int ("gtk-double-click-distance",
223                                                                P_("Double Click Distance"),
224                                                                P_("Maximum distance allowed between two clicks for them to be considered a double click (in pixels)"),
225                                                                0, G_MAXINT, 5,
226                                                                GTK_PARAM_READWRITE),
227                                              NULL);
228   g_assert (result == PROP_DOUBLE_CLICK_DISTANCE);
229
230   /**
231    * GtkSettings:gtk-cursor-blink:
232    *
233    * Whether the cursor should blink. 
234    *
235    * Also see the #GtkSettings:gtk-cursor-blink-timeout setting, 
236    * which allows more flexible control over cursor blinking.
237    */
238   result = settings_install_property_parser (class,
239                                              g_param_spec_boolean ("gtk-cursor-blink",
240                                                                    P_("Cursor Blink"),
241                                                                    P_("Whether the cursor should blink"),
242                                                                    TRUE,
243                                                                    GTK_PARAM_READWRITE),
244                                              NULL);
245   g_assert (result == PROP_CURSOR_BLINK);
246   result = settings_install_property_parser (class,
247                                              g_param_spec_int ("gtk-cursor-blink-time",
248                                                                P_("Cursor Blink Time"),
249                                                                P_("Length of the cursor blink cycle, in milliseconds"),
250                                                                100, G_MAXINT, 1200,
251                                                                GTK_PARAM_READWRITE),
252                                              NULL);
253   g_assert (result == PROP_CURSOR_BLINK_TIME);
254  
255   /**
256    * GtkSettings:gtk-cursor-blink-timeout:
257    *
258    * Time after which the cursor stops blinking, in seconds.
259    * The timer is reset after each user interaction.
260    *
261    * Setting this to zero has the same effect as setting
262    * #GtkSettings:gtk-cursor-blink to %FALSE. 
263    *
264    * Since: 2.12
265    */
266   result = settings_install_property_parser (class,
267                                              g_param_spec_int ("gtk-cursor-blink-timeout",
268                                                                P_("Cursor Blink Timeout"),
269                                                                P_("Time after which the cursor stops blinking, in seconds"),
270                                                                1, G_MAXINT, G_MAXINT,
271                                                                GTK_PARAM_READWRITE),
272                                              NULL);
273   g_assert (result == PROP_CURSOR_BLINK_TIMEOUT);
274   result = settings_install_property_parser (class,
275                                              g_param_spec_boolean ("gtk-split-cursor",
276                                                                    P_("Split Cursor"),
277                                                                    P_("Whether two cursors should be displayed for mixed left-to-right and right-to-left text"),
278                                                                    TRUE,
279                                                                    GTK_PARAM_READWRITE),
280                                              NULL);
281   g_assert (result == PROP_SPLIT_CURSOR);
282   result = settings_install_property_parser (class,
283                                              g_param_spec_string ("gtk-theme-name",
284                                                                    P_("Theme Name"),
285                                                                    P_("Name of theme RC file to load"),
286                                                                   "Raleigh",
287                                                                   GTK_PARAM_READWRITE),
288                                              NULL);
289   g_assert (result == PROP_THEME_NAME);
290
291   result = settings_install_property_parser (class,
292                                              g_param_spec_string ("gtk-icon-theme-name",
293                                                                   P_("Icon Theme Name"),
294                                                                   P_("Name of icon theme to use"),
295                                                                   "hicolor",
296                                                                   GTK_PARAM_READWRITE),
297                                              NULL);
298   g_assert (result == PROP_ICON_THEME_NAME);
299
300   result = settings_install_property_parser (class,
301                                              g_param_spec_string ("gtk-fallback-icon-theme",
302                                                                   P_("Fallback Icon Theme Name"),
303                                                                   P_("Name of a icon theme to fall back to"),
304                                                                   NULL,
305                                                                   GTK_PARAM_READWRITE),
306                                              NULL);
307   g_assert (result == PROP_FALLBACK_ICON_THEME);
308   
309   result = settings_install_property_parser (class,
310                                              g_param_spec_string ("gtk-key-theme-name",
311                                                                   P_("Key Theme Name"),
312                                                                   P_("Name of key theme RC file to load"),
313                                                                   NULL,
314                                                                   GTK_PARAM_READWRITE),
315                                              NULL);
316   g_assert (result == PROP_KEY_THEME_NAME);    
317
318   result = settings_install_property_parser (class,
319                                              g_param_spec_string ("gtk-menu-bar-accel",
320                                                                   P_("Menu bar accelerator"),
321                                                                   P_("Keybinding to activate the menu bar"),
322                                                                   "F10",
323                                                                   GTK_PARAM_READWRITE),
324                                              NULL);
325   g_assert (result == PROP_MENU_BAR_ACCEL);
326
327   result = settings_install_property_parser (class,
328                                              g_param_spec_int ("gtk-dnd-drag-threshold",
329                                                                P_("Drag threshold"),
330                                                                P_("Number of pixels the cursor can move before dragging"),
331                                                                1, G_MAXINT, 8,
332                                                                GTK_PARAM_READWRITE),
333                                              NULL);
334   g_assert (result == PROP_DND_DRAG_THRESHOLD);
335
336   result = settings_install_property_parser (class,
337                                              g_param_spec_string ("gtk-font-name",
338                                                                    P_("Font Name"),
339                                                                    P_("Name of default font to use"),
340                                                                   "Sans 10",
341                                                                   GTK_PARAM_READWRITE),
342                                              NULL);
343   g_assert (result == PROP_FONT_NAME);
344
345   /**
346    * GtkSettings:gtk-icon-sizes:
347    *
348    * A list of icon sizes. The list is separated by colons, and
349    * item has the form:
350    *
351    * <replaceable>size-name</replaceable> = <replaceable>width</replaceable> , <replaceable>height</replaceable>
352    *
353    * E.g. "gtk-menu=16,16:gtk-button=20,20:gtk-dialog=48,48". 
354    * GTK+ itself use the following named icon sizes: gtk-menu, 
355    * gtk-button, gtk-small-toolbar, gtk-large-toolbar, gtk-dnd, 
356    * gtk-dialog. Applications can register their own named icon 
357    * sizes with gtk_icon_size_register().
358    */
359   result = settings_install_property_parser (class,
360                                              g_param_spec_string ("gtk-icon-sizes",
361                                                                    P_("Icon Sizes"),
362                                                                    P_("List of icon sizes (gtk-menu=16,16:gtk-button=20,20..."),
363                                                                   NULL,
364                                                                   GTK_PARAM_READWRITE),
365                                              NULL);
366   g_assert (result == PROP_ICON_SIZES);
367
368   result = settings_install_property_parser (class,
369                                              g_param_spec_string ("gtk-modules",
370                                                                   P_("GTK Modules"),
371                                                                   P_("List of currently active GTK modules"),
372                                                                   NULL,
373                                                                   GTK_PARAM_READWRITE),
374                                              NULL);
375   g_assert (result == PROP_MODULES);
376
377 #ifdef GDK_WINDOWING_X11
378   result = settings_install_property_parser (class,
379                                              g_param_spec_int ("gtk-xft-antialias",
380                                                                P_("Xft Antialias"),
381                                                                P_("Whether to antialias Xft fonts; 0=no, 1=yes, -1=default"),
382                                                                -1, 1, -1,
383                                                                GTK_PARAM_READWRITE),
384                                              NULL);
385  
386   g_assert (result == PROP_XFT_ANTIALIAS);
387   
388   result = settings_install_property_parser (class,
389                                              g_param_spec_int ("gtk-xft-hinting",
390                                                                P_("Xft Hinting"),
391                                                                P_("Whether to hint Xft fonts; 0=no, 1=yes, -1=default"),
392                                                                -1, 1, -1,
393                                                                GTK_PARAM_READWRITE),
394                                              NULL);
395   
396   g_assert (result == PROP_XFT_HINTING);
397   
398   result = settings_install_property_parser (class,
399                                              g_param_spec_string ("gtk-xft-hintstyle",
400                                                                   P_("Xft Hint Style"),
401                                                                   P_("What degree of hinting to use; hintnone, hintslight, hintmedium, or hintfull"),
402                                                                   NULL,
403                                                                   GTK_PARAM_READWRITE),
404                                               NULL);
405   
406   g_assert (result == PROP_XFT_HINTSTYLE);
407   
408   result = settings_install_property_parser (class,
409                                              g_param_spec_string ("gtk-xft-rgba",
410                                                                   P_("Xft RGBA"),
411                                                                   P_("Type of subpixel antialiasing; none, rgb, bgr, vrgb, vbgr"),
412                                                                   NULL,
413                                                                   GTK_PARAM_READWRITE),
414                                              NULL);
415   
416   g_assert (result == PROP_XFT_RGBA);
417   
418   result = settings_install_property_parser (class,
419                                              g_param_spec_int ("gtk-xft-dpi",
420                                                                P_("Xft DPI"),
421                                                                P_("Resolution for Xft, in 1024 * dots/inch. -1 to use default value"),
422                                                                -1, 1024*1024, -1,
423                                                                GTK_PARAM_READWRITE),
424                                              NULL);
425   
426   g_assert (result == PROP_XFT_DPI);
427
428   result = settings_install_property_parser (class,
429                                              g_param_spec_string ("gtk-cursor-theme-name",
430                                                                   P_("Cursor theme name"),
431                                                                   P_("Name of the cursor theme to use, or NULL to use the default theme"),
432                                                                   NULL,
433                                                                   GTK_PARAM_READWRITE),
434                                              NULL);
435   g_assert (result == PROP_CURSOR_THEME_NAME);
436
437   result = settings_install_property_parser (class,
438                                              g_param_spec_int ("gtk-cursor-theme-size",
439                                                                P_("Cursor theme size"),
440                                                                P_("Size to use for cursors, or 0 to use the default size"),
441                                                                0, 128, 0,
442                                                                GTK_PARAM_READWRITE),
443                                              NULL);
444   
445   g_assert (result == PROP_CURSOR_THEME_SIZE);
446
447 #endif  /* GDK_WINDOWING_X11 */
448   result = settings_install_property_parser (class,
449                                              g_param_spec_boolean ("gtk-alternative-button-order",
450                                                                    P_("Alternative button order"),
451                                                                    P_("Whether buttons in dialogs should use the alternative button order"),
452                                                                    FALSE,
453                                                                    GTK_PARAM_READWRITE),
454                                              NULL);
455   g_assert (result == PROP_ALTERNATIVE_BUTTON_ORDER);
456
457   /**
458    * GtkSettings:gtk-alternative-sort-arrows:
459    *
460    * Controls the direction of the sort indicators in sorted list and tree
461    * views. By default an arrow pointing down means the column is sorted
462    * in ascending order. When set to %TRUE, this order will be inverted.
463    *
464    * Since: 2.12
465    */
466   result = settings_install_property_parser (class,
467                                              g_param_spec_boolean ("gtk-alternative-sort-arrows",
468                                                                    P_("Alternative sort indicator direction"),
469                                                                    P_("Whether the direction of the sort indicators in list and tree views is inverted compared to the default (where down means ascending)"),
470                                                                    FALSE,
471                                                                    GTK_PARAM_READWRITE),
472                                              NULL);
473   g_assert (result == PROP_ALTERNATIVE_SORT_ARROWS);
474
475   result = settings_install_property_parser (class,
476                                              g_param_spec_boolean ("gtk-show-input-method-menu",
477                                                                    P_("Show the 'Input Methods' menu"),
478                                                                    P_("Whether the context menus of entries and text views should offer to change the input method"),
479                                                                    TRUE,
480                                                                    GTK_PARAM_READWRITE),
481                                              NULL);
482   g_assert (result == PROP_SHOW_INPUT_METHOD_MENU);
483
484   result = settings_install_property_parser (class,
485                                              g_param_spec_boolean ("gtk-show-unicode-menu",
486                                                                    P_("Show the 'Insert Unicode Control Character' menu"),
487                                                                    P_("Whether the context menus of entries and text views should offer to insert control characters"),
488                                                                    TRUE,
489                                                                    GTK_PARAM_READWRITE),
490                                              NULL);
491   g_assert (result == PROP_SHOW_UNICODE_MENU);
492
493   result = settings_install_property_parser (class,
494                                              g_param_spec_int ("gtk-timeout-initial",
495                                                                P_("Start timeout"),
496                                                                P_("Starting value for timeouts, when button is pressed"),
497                                                                0, G_MAXINT, DEFAULT_TIMEOUT_INITIAL,
498                                                                GTK_PARAM_READWRITE),
499                                              NULL);
500
501   g_assert (result == PROP_TIMEOUT_INITIAL);
502
503   result = settings_install_property_parser (class,
504                                              g_param_spec_int ("gtk-timeout-repeat",
505                                                                P_("Repeat timeout"),
506                                                                P_("Repeat value for timeouts, when button is pressed"),
507                                                                0, G_MAXINT, DEFAULT_TIMEOUT_REPEAT,
508                                                                GTK_PARAM_READWRITE),
509                                              NULL);
510
511   g_assert (result == PROP_TIMEOUT_REPEAT);
512
513   result = settings_install_property_parser (class,
514                                              g_param_spec_int ("gtk-timeout-expand",
515                                                                P_("Expand timeout"),
516                                                                P_("Expand value for timeouts, when a widget is expanding a new region"),
517                                                                0, G_MAXINT, DEFAULT_TIMEOUT_EXPAND,
518                                                                GTK_PARAM_READWRITE),
519                                              NULL);
520
521   g_assert (result == PROP_TIMEOUT_EXPAND);
522
523   /**
524    * GtkSettings:gtk-color-scheme:
525    *
526    * A palette of named colors for use in themes. The format of the string is
527    * <programlisting>
528    * name1: color1
529    * name2: color2
530    * ...
531    * </programlisting>
532    * Color names must be acceptable as identifiers in the 
533    * <link linkend="gtk-Resource-Files">gtkrc</link> syntax, and
534    * color specifications must be in the format accepted by
535    * gdk_color_parse().
536    * 
537    * Note that due to the way the color tables from different sources are
538    * merged, color specifications will be converted to hexadecimal form
539    * when getting this property.
540    *
541    * Starting with GTK+ 2.12, the entries can alternatively be separated
542    * by ';' instead of newlines:
543    * <programlisting>
544    * name1: color1; name2: color2; ...
545    * </programlisting>
546    *
547    * Since: 2.10
548    */
549   result = settings_install_property_parser (class,
550                                              g_param_spec_string ("gtk-color-scheme",
551                                                                   P_("Color scheme"),
552                                                                   P_("A palette of named colors for use in themes"),
553                                                                   "",
554                                                                   GTK_PARAM_READWRITE),
555                                              NULL);
556
557   g_assert (result == PROP_COLOR_SCHEME);
558
559   result = settings_install_property_parser (class,
560                                              g_param_spec_boolean ("gtk-enable-animations",
561                                                                    P_("Enable Animations"),
562                                                                    P_("Whether to enable toolkit-wide animations."),
563                                                                    TRUE,
564                                                                    GTK_PARAM_READWRITE),
565                                              NULL);
566
567   g_assert (result == PROP_ENABLE_ANIMATIONS);
568
569   /**
570    * GtkSettings:gtk-touchscreen-mode:
571    *
572    * When %TRUE, there are no motion notify events delivered on this screen,
573    * and widgets can't use the pointer hovering them for any essential
574    * functionality.
575    *
576    * Since: 2.10
577    */
578   result = settings_install_property_parser (class,
579                                              g_param_spec_boolean ("gtk-touchscreen-mode",
580                                                                    P_("Enable Touchscreen Mode"),
581                                                                    P_("When TRUE, there are no motion notify events delivered on this screen"),
582                                                                    FALSE,
583                                                                    GTK_PARAM_READWRITE),
584                                              NULL);
585
586   g_assert (result == PROP_TOUCHSCREEN_MODE);
587
588   /**
589    * GtkSettings:gtk-tooltip-timeout:
590    *
591    * Time, in milliseconds, after which a tooltip could appear if the
592    * cursor is hovering on top of a widget.
593    *
594    * Since: 2.12
595    */
596   result = settings_install_property_parser (class,
597                                              g_param_spec_int ("gtk-tooltip-timeout",
598                                                                P_("Tooltip timeout"),
599                                                                P_("Timeout before tooltip is shown"),
600                                                                0, G_MAXINT,
601                                                                500,
602                                                                GTK_PARAM_READWRITE),
603                                              NULL);
604
605   g_assert (result == PROP_TOOLTIP_TIMEOUT);
606
607   /**
608    * GtkSettings:gtk-tooltip-browse-timeout:
609    *
610    * Controls the time after which tooltips will appear when
611    * browse mode is enabled, in milliseconds.
612    *
613    * Browse mode is enabled when the mouse pointer moves off an object
614    * where a tooltip was currently being displayed. If the mouse pointer
615    * hits another object before the browse mode timeout expires (see
616    * #GtkSettings:gtk-tooltip-browse-mode-timeout), it will take the 
617    * amount of milliseconds specified by this setting to popup the tooltip
618    * for the new object.
619    *
620    * Since: 2.12
621    */
622   result = settings_install_property_parser (class,
623                                              g_param_spec_int ("gtk-tooltip-browse-timeout",
624                                                                P_("Tooltip browse timeout"),
625                                                                P_("Timeout before tooltip is shown when browse mode is enabled"),
626                                                                0, G_MAXINT,
627                                                                60,
628                                                                GTK_PARAM_READWRITE),
629                                              NULL);
630
631   g_assert (result == PROP_TOOLTIP_BROWSE_TIMEOUT);
632
633   /**
634    * GtkSettings:gtk-tooltip-browse-mode-timeout:
635    *
636    * Amount of time, in milliseconds, after which the browse mode
637    * will be disabled.
638    *
639    * See #GtkSettings:gtk-tooltip-browse-timeout for more information
640    * about browse mode.
641    *
642    * Since: 2.12
643    */
644   result = settings_install_property_parser (class,
645                                              g_param_spec_int ("gtk-tooltip-browse-mode-timeout",
646                                                                P_("Tooltip browse mode timeout"),
647                                                                P_("Timeout after which browse mode is disabled"),
648                                                                0, G_MAXINT,
649                                                                500,
650                                                                GTK_PARAM_READWRITE),
651                                              NULL);
652
653   g_assert (result == PROP_TOOLTIP_BROWSE_MODE_TIMEOUT);
654
655   /**
656    * GtkSettings:gtk-keynav-cursor-only:
657    *
658    * When %TRUE, keyboard navigation should be able to reach all widgets
659    * by using the cursor keys only. Tab, Shift etc. keys can't be expected
660    * to be present on the used input device.
661    *
662    * Since: 2.12
663    */
664   result = settings_install_property_parser (class,
665                                              g_param_spec_boolean ("gtk-keynav-cursor-only",
666                                                                    P_("Keynav Cursor Only"),
667                                                                    P_("When TRUE, there are only cursor keys available to navigate widgets"),
668                                                                    FALSE,
669                                                                    GTK_PARAM_READWRITE),
670                                              NULL);
671
672   g_assert (result == PROP_KEYNAV_CURSOR_ONLY);
673
674   /**
675    * GtkSettings:gtk-keynav-wrap-around:
676    *
677    * When %TRUE, some widgets will wrap around when doing keyboard
678    * navigation, such as menus, menubars and notebooks.
679    *
680    * Since: 2.12
681    */
682   result = settings_install_property_parser (class,
683                                              g_param_spec_boolean ("gtk-keynav-wrap-around",
684                                                                    P_("Keynav Wrap Around"),
685                                                                    P_("Whether to wrap around when keyboard-navigating widgets"),
686                                                                    TRUE,
687                                                                    GTK_PARAM_READWRITE),
688                                              NULL);
689
690   g_assert (result == PROP_KEYNAV_WRAP_AROUND);
691
692   /**
693    * GtkSettings:gtk-error-bell:
694    *
695    * When %TRUE, keyboard navigation and other input-related errors
696    * will cause a beep. Since the error bell is implemented using
697    * gdk_window_beep(), the windowing system may offer ways to
698    * configure the error bell in many ways, such as flashing the
699    * window or similar visual effects.
700    *
701    * Since: 2.12
702    */
703   result = settings_install_property_parser (class,
704                                              g_param_spec_boolean ("gtk-error-bell",
705                                                                    P_("Error Bell"),
706                                                                    P_("When TRUE, keyboard navigation and other errors will cause a beep"),
707                                                                    TRUE,
708                                                                    GTK_PARAM_READWRITE),
709                                              NULL);
710
711   g_assert (result == PROP_ERROR_BELL);
712
713   /**
714    * GtkSettings:color-hash:
715    *
716    * Holds a hash table representation of the #GtkSettings:gtk-color-scheme 
717    * setting, mapping color names to #GdkColor<!-- -->s. 
718    *
719    * Since: 2.10
720    */
721   result = settings_install_property_parser (class, 
722                                              g_param_spec_boxed ("color-hash",
723                                                                  P_("Color Hash"),
724                                                                  P_("A hash table representation of the color scheme."),
725                                                                  G_TYPE_HASH_TABLE,
726                                                                  GTK_PARAM_READABLE),
727                                              NULL);
728   g_assert (result == PROP_COLOR_HASH);
729
730   result = settings_install_property_parser (class, 
731                                              g_param_spec_string ("gtk-file-chooser-backend",
732                                                                   P_("Default file chooser backend"),
733                                                                   P_("Name of the GtkFileChooser backend to use by default"),
734                                                                   NULL,
735                                                                   GTK_PARAM_READWRITE),
736                                              NULL);
737   g_assert (result == PROP_FILE_CHOOSER_BACKEND);
738
739   /**
740    * GtkSettings:gtk-print-backends:
741    *
742    * A comma-separated list of print backends to use in the print
743    * dialog. Available print backends depend on the GTK+ installation,
744    * and may include "file", "cups", "lpr" or "papi".
745    *
746    * Since: 2.10
747    */
748   result = settings_install_property_parser (class,
749                                              g_param_spec_string ("gtk-print-backends",
750                                                                   P_("Default print backend"),
751                                                                   P_("List of the GtkPrintBackend backends to use by default"),
752                                                                   GTK_PRINT_BACKENDS,
753                                                                   GTK_PARAM_READWRITE),
754                                              NULL);
755   g_assert (result == PROP_PRINT_BACKENDS);
756
757   /**
758    * GtkSettings:gtk-print-preview-command:
759    *
760    * A command to run for displaying the print preview. The command
761    * should contain a %f placeholder, which will get replaced by
762    * the path to the pdf file. The command may also contain a %s
763    * placeholder, which will get replaced by the path to a file
764    * containing the print settings in the format produced by 
765    * gtk_print_settings_to_file().
766    *
767    * The preview application is responsible for removing the pdf file
768    * and the print settings file when it is done.
769    *
770    * Since: 2.10
771    */
772   result = settings_install_property_parser (class,
773                                              g_param_spec_string ("gtk-print-preview-command",
774                                                                   P_("Default command to run when displaying a print preview"),
775                                                                   P_("Command to run when displaying a print preview"),
776                                                                   GTK_PRINT_PREVIEW_COMMAND,
777                                                                   GTK_PARAM_READWRITE),
778                                              NULL); 
779   g_assert (result == PROP_PRINT_PREVIEW_COMMAND);
780
781   /**
782    * GtkSettings:gtk-enable-mnemonics:
783    *
784    * Whether labels and menu items should have visible mnemonics which
785    * can be activated.
786    *
787    * Since: 2.12
788    */
789   result = settings_install_property_parser (class,
790                                              g_param_spec_boolean ("gtk-enable-mnemonics",
791                                                                    P_("Enable Mnemonics"),
792                                                                    P_("Whether labels should have mnemonics"),
793                                                                    TRUE,
794                                                                    GTK_PARAM_READWRITE),
795                                              NULL);
796   g_assert (result == PROP_ENABLE_MNEMONICS);
797
798   /**
799    * GtkSettings:gtk-enable-accels:
800    *
801    * Whether menu items should have visible accelerators which can be
802    * activated.
803    *
804    * Since: 2.12
805    */
806   result = settings_install_property_parser (class,
807                                              g_param_spec_boolean ("gtk-enable-accels",
808                                                                    P_("Enable Accelerators"),
809                                                                    P_("Whether menu items should have accelerators"),
810                                                                    TRUE,
811                                                                    GTK_PARAM_READWRITE),
812                                              NULL);
813   g_assert (result == PROP_ENABLE_ACCELS);
814
815   /**
816    * GtkSettings:gtk-recent-files-limit:
817    *
818    * The number of recently used files that should be displayed by default by
819    * #GtkRecentChooser implementations and by the #GtkFileChooser. A value of
820    * -1 means every recently used file stored.
821    *
822    * Since: 2.12
823    */
824   result = settings_install_property_parser (class,
825                                              g_param_spec_int ("gtk-recent-files-limit",
826                                                                P_("Recent Files Limit"),
827                                                                P_("Number of recently used files"),
828                                                                -1, G_MAXINT,
829                                                                50,
830                                                                GTK_PARAM_READWRITE),
831                                              NULL);
832   g_assert (result == PROP_RECENT_FILES_LIMIT);
833
834   /**
835    * GtkSettings:gtk-im-module:
836    *
837    * Which IM (input method) module should be used by default. This is the 
838    * input method that will be used if the user has not explicitly chosen 
839    * another input method from the IM context menu.  
840    *
841    * See #GtkIMContext and see the #GtkSettings:gtk-show-input-method-menu property.
842    */
843   result = settings_install_property_parser (class,
844                                              g_param_spec_string ("gtk-im-module",
845                                                                   P_("Default IM module"),
846                                                                   P_("Which IM module should be used by default"),
847                                                                   NULL,
848                                                                   GTK_PARAM_READWRITE),
849                                              NULL);
850   g_assert (result == PROP_IM_MODULE);
851
852   /**
853    * GtkSettings:gtk-recent-files-max-age:
854    *
855    * The maximum age, in days, of the items inside the recently used
856    * resources list. Items older than this setting will be excised
857    * from the list. If set to 0, the list will always be empty; if
858    * set to -1, no item will be removed.
859    *
860    * Since: 2.14
861    */
862   result = settings_install_property_parser (class,
863                                              g_param_spec_int ("gtk-recent-files-max-age",
864                                                                P_("Recent Files Max Age"),
865                                                                P_("Maximum age of recently used files, in days"),
866                                                                -1, G_MAXINT,
867                                                                30,
868                                                                GTK_PARAM_READWRITE),
869                                              NULL);
870   g_assert (result == PROP_RECENT_FILES_MAX_AGE);
871
872   result = settings_install_property_parser (class,
873                                              g_param_spec_uint ("gtk-fontconfig-timestamp",
874                                                                 P_("Fontconfig configuration timestamp"),
875                                                                 P_("Timestamp of current fontconfig configuration"),
876                                                                 0, G_MAXUINT, 0,
877                                                                 GTK_PARAM_READWRITE),
878                                              NULL);
879   
880   g_assert (result == PROP_FONTCONFIG_TIMESTAMP);
881
882   /**
883    * GtkSettings:gtk-sound-theme-name:
884    *
885    * The XDG sound theme to use for event sounds.
886    *
887    * See the <ulink url="http://www.freedesktop.org/wiki/Specifications/sound-theme-spec">Sound Theme spec</ulink> 
888    * for more information on event sounds and sound themes.
889    *
890    * GTK+ itself does not support event sounds, you have to use a loadable 
891    * module like the one that comes with libcanberra.
892    *
893    * Since: 2.14
894    */
895   result = settings_install_property_parser (class,
896                                              g_param_spec_string ("gtk-sound-theme-name",
897                                                                   P_("Sound Theme Name"),
898                                                                   P_("XDG sound theme name"),
899                                                                   "freedesktop",
900                                                                   GTK_PARAM_READWRITE),
901                                              NULL);
902   g_assert (result == PROP_SOUND_THEME_NAME);
903
904   /**
905    * GtkSettings:gtk-enable-input-feedback-sounds:
906    *
907    * Whether to play event sounds as feedback to user input.
908    *
909    * See the <ulink url="http://www.freedesktop.org/wiki/Specifications/sound-theme-spec">Sound Theme spec</ulink> 
910    * for more information on event sounds and sound themes.
911    *
912    * GTK+ itself does not support event sounds, you have to use a loadable 
913    * module like the one that comes with libcanberra.
914    *
915    * Since: 2.14
916    */
917   result = settings_install_property_parser (class,
918                                              g_param_spec_boolean ("gtk-enable-input-feedback-sounds",
919                                                                    /* Translators: this means sounds that are played as feedback to user input */
920                                                                    P_("Audible Input Feedback"),
921                                                                    P_("Whether to play event sounds as feedback to user input"),
922                                                                    TRUE,
923                                                                    GTK_PARAM_READWRITE),
924                                              NULL);
925   g_assert (result == PROP_ENABLE_INPUT_FEEDBACK_SOUNDS);
926
927   /**
928    * GtkSettings:gtk-enable-event-sounds:
929    *
930    * Whether to play any event sounds at all.
931    *
932    * See the <ulink url="http://www.freedesktop.org/wiki/Specifications/sound-theme-spec">Sound Theme spec</ulink> 
933    * for more information on event sounds and sound themes.
934    *
935    * GTK+ itself does not support event sounds, you have to use a loadable 
936    * module like the one that comes with libcanberra.
937    *
938    * Since: 2.14
939    */
940   result = settings_install_property_parser (class,
941                                              g_param_spec_boolean ("gtk-enable-event-sounds",
942                                                                    P_("Enable Event Sounds"),
943                                                                    P_("Whether to play any event sounds at all"),
944                                                                    TRUE,
945                                                                    GTK_PARAM_READWRITE),
946                                              NULL);
947   g_assert (result == PROP_ENABLE_EVENT_SOUNDS);
948
949   /**
950    * GtkSettings:gtk-enable-tooltips:
951    *
952    * Whether tooltips should be shown on widgets.
953    *
954    * Since: 2.14
955    */
956   result = settings_install_property_parser (class,
957                                              g_param_spec_boolean ("gtk-enable-tooltips",
958                                                                    P_("Enable Tooltips"),
959                                                                    P_("Whether tooltips should be shown on widgets"),
960                                                                    TRUE,
961                                                                    GTK_PARAM_READWRITE),
962                                              NULL);
963   g_assert (result == PROP_ENABLE_TOOLTIPS);
964 }
965
966 static void
967 gtk_settings_finalize (GObject *object)
968 {
969   GtkSettings *settings = GTK_SETTINGS (object);
970   guint i;
971
972   object_list = g_slist_remove (object_list, settings);
973
974   _gtk_rc_context_destroy (settings);
975
976   for (i = 0; i < class_n_properties; i++)
977     g_value_unset (&settings->property_values[i].value);
978   g_free (settings->property_values);
979   
980   g_datalist_clear (&settings->queued_settings);
981
982   G_OBJECT_CLASS (gtk_settings_parent_class)->finalize (object);
983 }
984
985 /**
986  * gtk_settings_get_for_screen:
987  * @screen : a #GdkScreen.
988  * 
989  * Gets the #GtkSettings object for @screen, creating it if necessary.
990  *
991  * Return value: a #GtkSettings object.
992  *
993  * Since: 2.2
994  */
995 GtkSettings*
996 gtk_settings_get_for_screen (GdkScreen *screen)
997 {
998   GtkSettings *settings;
999   
1000   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
1001   
1002   settings = g_object_get_data (G_OBJECT (screen), "gtk-settings");
1003   if (!settings)
1004     {
1005       settings = g_object_new (GTK_TYPE_SETTINGS, NULL);
1006       settings->screen = screen;
1007       g_object_set_data_full (G_OBJECT (screen), I_("gtk-settings"), 
1008                               settings, g_object_unref);
1009
1010       gtk_rc_reparse_all_for_settings (settings, TRUE);
1011       settings_update_double_click (settings);
1012 #ifdef GDK_WINDOWING_X11
1013       settings_update_cursor_theme (settings);
1014       settings_update_resolution (settings);
1015       settings_update_font_options (settings);
1016 #endif
1017       settings_update_color_scheme (settings);
1018     }
1019   
1020   return settings;
1021 }
1022
1023 /**
1024  * gtk_settings_get_default:
1025  * 
1026  * Gets the #GtkSettings object for the default GDK screen, creating
1027  * it if necessary. See gtk_settings_get_for_screen().
1028  * 
1029  * Return value: a #GtkSettings object. If there is no default
1030  *  screen, then returns %NULL.
1031  **/
1032 GtkSettings*
1033 gtk_settings_get_default (void)
1034 {
1035   GdkScreen *screen = gdk_screen_get_default ();
1036
1037   if (screen)
1038     return gtk_settings_get_for_screen (screen);
1039   else
1040     return NULL;
1041 }
1042
1043 static void
1044 gtk_settings_set_property (GObject      *object,
1045                            guint         property_id,
1046                            const GValue *value,
1047                            GParamSpec   *pspec)
1048 {
1049   GtkSettings *settings = GTK_SETTINGS (object);
1050
1051   g_value_copy (value, &settings->property_values[property_id - 1].value);
1052   settings->property_values[property_id - 1].source = GTK_SETTINGS_SOURCE_APPLICATION;
1053   
1054   if (pspec->param_id == PROP_COLOR_SCHEME)
1055     merge_color_scheme (settings, value, GTK_SETTINGS_SOURCE_APPLICATION);
1056 }
1057
1058 static void
1059 gtk_settings_get_property (GObject     *object,
1060                            guint        property_id,
1061                            GValue      *value,
1062                            GParamSpec  *pspec)
1063 {
1064   GtkSettings *settings = GTK_SETTINGS (object);
1065   GType value_type = G_VALUE_TYPE (value);
1066   GType fundamental_type = G_TYPE_FUNDAMENTAL (value_type);
1067
1068   /* handle internal properties */
1069   switch (property_id)
1070     {
1071     case PROP_COLOR_HASH:
1072       g_value_set_boxed (value, get_color_hash (settings));
1073       return;
1074     case PROP_COLOR_SCHEME:
1075       g_value_take_string (value, get_color_scheme (settings));
1076       return;
1077     default: ;
1078     }
1079
1080   /* For enums and strings, we need to get the value as a string,
1081    * not as an int, since we support using names/nicks as the setting
1082    * value.
1083    */
1084   if ((g_value_type_transformable (G_TYPE_INT, value_type) &&
1085        !(fundamental_type == G_TYPE_ENUM || fundamental_type == G_TYPE_FLAGS)) ||
1086       g_value_type_transformable (G_TYPE_STRING, G_VALUE_TYPE (value)) ||
1087       g_value_type_transformable (GDK_TYPE_COLOR, G_VALUE_TYPE (value)))
1088     {
1089       if (settings->property_values[property_id - 1].source == GTK_SETTINGS_SOURCE_APPLICATION ||
1090           !gdk_screen_get_setting (settings->screen, pspec->name, value))
1091         g_value_copy (&settings->property_values[property_id - 1].value, value);
1092       else 
1093         g_param_value_validate (pspec, value);
1094     }
1095   else
1096     {
1097       GValue val = { 0, };
1098
1099       /* Try to get xsetting as a string and parse it. */
1100       
1101       g_value_init (&val, G_TYPE_STRING);
1102
1103       if (settings->property_values[property_id - 1].source == GTK_SETTINGS_SOURCE_APPLICATION ||
1104           !gdk_screen_get_setting (settings->screen, pspec->name, &val))
1105         {
1106           g_value_copy (&settings->property_values[property_id - 1].value, value);
1107         }
1108       else
1109         {
1110           GValue tmp_value = { 0, };
1111           GValue gstring_value = { 0, };
1112           GtkRcPropertyParser parser = (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser);
1113           
1114           g_value_init (&gstring_value, G_TYPE_GSTRING);
1115           g_value_take_boxed (&gstring_value,
1116                               g_string_new (g_value_get_string (&val)));
1117
1118           g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1119
1120           if (parser && _gtk_settings_parse_convert (parser, &gstring_value,
1121                                                      pspec, &tmp_value))
1122             {
1123               g_value_copy (&tmp_value, value);
1124               g_param_value_validate (pspec, value);
1125             }
1126           else
1127             {
1128               g_value_copy (&settings->property_values[property_id - 1].value, value);
1129             }
1130
1131           g_value_unset (&gstring_value);
1132           g_value_unset (&tmp_value);
1133         }
1134
1135       g_value_unset (&val);
1136     }
1137 }
1138
1139 static void
1140 gtk_settings_notify (GObject    *object,
1141                      GParamSpec *pspec)
1142 {
1143   GtkSettings *settings = GTK_SETTINGS (object);
1144   guint property_id = pspec->param_id;
1145
1146   if (settings->screen == NULL) /* initialization */
1147     return;
1148
1149   switch (property_id)
1150     {
1151     case PROP_MODULES:
1152       settings_update_modules (settings);
1153       break;
1154     case PROP_DOUBLE_CLICK_TIME:
1155     case PROP_DOUBLE_CLICK_DISTANCE:
1156       settings_update_double_click (settings);
1157       break;
1158     case PROP_COLOR_SCHEME:
1159       settings_update_color_scheme (settings);
1160       break;
1161 #ifdef GDK_WINDOWING_X11
1162     case PROP_XFT_DPI:
1163       settings_update_resolution (settings);
1164       /* This is a hack because with gtk_rc_reset_styles() doesn't get
1165        * widgets with gtk_widget_style_set(), and also causes more
1166        * recomputation than necessary.
1167        */
1168       gtk_rc_reset_styles (GTK_SETTINGS (object));
1169       break;
1170     case PROP_XFT_ANTIALIAS:
1171     case PROP_XFT_HINTING:
1172     case PROP_XFT_HINTSTYLE:
1173     case PROP_XFT_RGBA:
1174       settings_update_font_options (settings);
1175       gtk_rc_reset_styles (GTK_SETTINGS (object));
1176       break;
1177     case PROP_FONTCONFIG_TIMESTAMP:
1178       if (settings_update_fontconfig (settings))
1179         gtk_rc_reset_styles (GTK_SETTINGS (object));
1180       break;
1181     case PROP_CURSOR_THEME_NAME:
1182     case PROP_CURSOR_THEME_SIZE:
1183       settings_update_cursor_theme (settings);
1184       break;
1185 #endif /* GDK_WINDOWING_X11 */
1186     }
1187 }
1188
1189 gboolean
1190 _gtk_settings_parse_convert (GtkRcPropertyParser parser,
1191                              const GValue       *src_value,
1192                              GParamSpec         *pspec,
1193                              GValue             *dest_value)
1194 {
1195   gboolean success = FALSE;
1196
1197   g_return_val_if_fail (G_VALUE_HOLDS (dest_value, G_PARAM_SPEC_VALUE_TYPE (pspec)), FALSE);
1198
1199   if (parser)
1200     {
1201       GString *gstring;
1202       gboolean free_gstring = TRUE;
1203       
1204       if (G_VALUE_HOLDS (src_value, G_TYPE_GSTRING))
1205         {
1206           gstring = g_value_get_boxed (src_value);
1207           free_gstring = FALSE;
1208         }
1209       else if (G_VALUE_HOLDS_LONG (src_value))
1210         {
1211           gstring = g_string_new (NULL);
1212           g_string_append_printf (gstring, "%ld", g_value_get_long (src_value));
1213         }
1214       else if (G_VALUE_HOLDS_DOUBLE (src_value))
1215         {
1216           gstring = g_string_new (NULL);
1217           g_string_append_printf (gstring, "%f", g_value_get_double (src_value));
1218         }
1219       else if (G_VALUE_HOLDS_STRING (src_value))
1220         {
1221           gchar *tstr = g_strescape (g_value_get_string (src_value), NULL);
1222           
1223           gstring = g_string_new ("\"");
1224           g_string_append (gstring, tstr);
1225           g_string_append_c (gstring, '\"');
1226           g_free (tstr);
1227         }
1228       else
1229         {
1230           g_return_val_if_fail (G_VALUE_HOLDS (src_value, G_TYPE_GSTRING), FALSE);
1231           gstring = NULL; /* silence compiler */
1232         }
1233
1234       success = (parser (pspec, gstring, dest_value) &&
1235                  !g_param_value_validate (pspec, dest_value));
1236
1237       if (free_gstring)
1238         g_string_free (gstring, TRUE);
1239     }
1240   else if (G_VALUE_HOLDS (src_value, G_TYPE_GSTRING))
1241     {
1242       if (G_VALUE_HOLDS (dest_value, G_TYPE_STRING))
1243         {
1244           GString *gstring = g_value_get_boxed (src_value);
1245
1246           g_value_set_string (dest_value, gstring ? gstring->str : NULL);
1247           success = !g_param_value_validate (pspec, dest_value);
1248         }
1249     }
1250   else if (g_value_type_transformable (G_VALUE_TYPE (src_value), G_VALUE_TYPE (dest_value)))
1251     success = g_param_value_convert (pspec, src_value, dest_value, TRUE);
1252
1253   return success;
1254 }
1255
1256 static void
1257 apply_queued_setting (GtkSettings             *data,
1258                       GParamSpec              *pspec,
1259                       GtkSettingsValuePrivate *qvalue)
1260 {
1261   GValue tmp_value = { 0, };
1262   GtkRcPropertyParser parser = (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser);
1263
1264   g_value_init (&tmp_value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1265   if (_gtk_settings_parse_convert (parser, &qvalue->public.value,
1266                                    pspec, &tmp_value))
1267     {
1268       if (pspec->param_id == PROP_COLOR_SCHEME) 
1269         merge_color_scheme (data, &tmp_value, qvalue->source);
1270
1271       if (data->property_values[pspec->param_id - 1].source <= qvalue->source)
1272         {
1273           g_value_copy (&tmp_value, &data->property_values[pspec->param_id - 1].value);
1274           data->property_values[pspec->param_id - 1].source = qvalue->source;
1275           g_object_notify (G_OBJECT (data), g_param_spec_get_name (pspec));
1276         }
1277
1278     }
1279   else
1280     {
1281       gchar *debug = g_strdup_value_contents (&qvalue->public.value);
1282       
1283       g_message ("%s: failed to retrieve property `%s' of type `%s' from rc file value \"%s\" of type `%s'",
1284                  qvalue->public.origin ? qvalue->public.origin : "(for origin information, set GTK_DEBUG)",
1285                  pspec->name,
1286                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1287                  debug,
1288                  G_VALUE_TYPE_NAME (&tmp_value));
1289       g_free (debug);
1290     }
1291   g_value_unset (&tmp_value);
1292 }
1293
1294 static guint
1295 settings_install_property_parser (GtkSettingsClass   *class,
1296                                   GParamSpec         *pspec,
1297                                   GtkRcPropertyParser parser)
1298 {
1299   GSList *node, *next;
1300
1301   switch (G_TYPE_FUNDAMENTAL (G_PARAM_SPEC_VALUE_TYPE (pspec)))
1302     {
1303     case G_TYPE_BOOLEAN:
1304     case G_TYPE_UCHAR:
1305     case G_TYPE_CHAR:
1306     case G_TYPE_UINT:
1307     case G_TYPE_INT:
1308     case G_TYPE_ULONG:
1309     case G_TYPE_LONG:
1310     case G_TYPE_FLOAT:
1311     case G_TYPE_DOUBLE:
1312     case G_TYPE_STRING:
1313       break;
1314     case G_TYPE_BOXED:
1315       if (strcmp (g_param_spec_get_name (pspec), "color-hash") == 0)
1316         {
1317           break;
1318         }
1319       /* fall through */
1320     default:
1321       if (!parser)
1322         {
1323           g_warning (G_STRLOC ": parser needs to be specified for property \"%s\" of type `%s'",
1324                      pspec->name, g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)));
1325           return 0;
1326         }
1327     }
1328   if (g_object_class_find_property (G_OBJECT_CLASS (class), pspec->name))
1329     {
1330       g_warning (G_STRLOC ": an rc-data property \"%s\" already exists",
1331                  pspec->name);
1332       return 0;
1333     }
1334   
1335   for (node = object_list; node; node = node->next)
1336     g_object_freeze_notify (node->data);
1337
1338   g_object_class_install_property (G_OBJECT_CLASS (class), ++class_n_properties, pspec);
1339   g_param_spec_set_qdata (pspec, quark_property_parser, (gpointer) parser);
1340
1341   for (node = object_list; node; node = node->next)
1342     {
1343       GtkSettings *settings = node->data;
1344       GtkSettingsValuePrivate *qvalue;
1345       
1346       settings->property_values = g_renew (GtkSettingsPropertyValue, settings->property_values, class_n_properties);
1347       settings->property_values[class_n_properties - 1].value.g_type = 0;
1348       g_value_init (&settings->property_values[class_n_properties - 1].value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1349       g_param_value_set_default (pspec, &settings->property_values[class_n_properties - 1].value);
1350       settings->property_values[class_n_properties - 1].source = GTK_SETTINGS_SOURCE_DEFAULT;
1351       g_object_notify (G_OBJECT (settings), pspec->name);
1352       
1353       qvalue = g_datalist_get_data (&settings->queued_settings, pspec->name);
1354       if (qvalue)
1355         apply_queued_setting (settings, pspec, qvalue);
1356     }
1357
1358   for (node = object_list; node; node = next)
1359     {
1360       next = node->next;
1361       g_object_thaw_notify (node->data);
1362     }
1363
1364   return class_n_properties;
1365 }
1366
1367 GtkRcPropertyParser
1368 _gtk_rc_property_parser_from_type (GType type)
1369 {
1370   if (type == GDK_TYPE_COLOR)
1371     return gtk_rc_property_parse_color;
1372   else if (type == GTK_TYPE_REQUISITION)
1373     return gtk_rc_property_parse_requisition;
1374   else if (type == GTK_TYPE_BORDER)
1375     return gtk_rc_property_parse_border;
1376   else if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_ENUM && G_TYPE_IS_DERIVED (type))
1377     return gtk_rc_property_parse_enum;
1378   else if (G_TYPE_FUNDAMENTAL (type) == G_TYPE_FLAGS && G_TYPE_IS_DERIVED (type))
1379     return gtk_rc_property_parse_flags;
1380   else
1381     return NULL;
1382 }
1383
1384 void
1385 gtk_settings_install_property (GParamSpec *pspec)
1386 {
1387   static GtkSettingsClass *klass = NULL;
1388
1389   GtkRcPropertyParser parser;
1390
1391   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1392
1393   if (! klass)
1394     klass = g_type_class_ref (GTK_TYPE_SETTINGS);
1395
1396   parser = _gtk_rc_property_parser_from_type (G_PARAM_SPEC_VALUE_TYPE (pspec));
1397
1398   settings_install_property_parser (klass, pspec, parser);
1399 }
1400
1401 void
1402 gtk_settings_install_property_parser (GParamSpec          *pspec,
1403                                       GtkRcPropertyParser  parser)
1404 {
1405   static GtkSettingsClass *klass = NULL;
1406
1407   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
1408   g_return_if_fail (parser != NULL);
1409
1410   if (! klass)
1411     klass = g_type_class_ref (GTK_TYPE_SETTINGS);
1412
1413   settings_install_property_parser (klass, pspec, parser);
1414 }
1415
1416 static void
1417 free_value (gpointer data)
1418 {
1419   GtkSettingsValuePrivate *qvalue = data;
1420   
1421   g_value_unset (&qvalue->public.value);
1422   g_free (qvalue->public.origin);
1423   g_slice_free (GtkSettingsValuePrivate, qvalue);
1424 }
1425
1426 static void
1427 gtk_settings_set_property_value_internal (GtkSettings            *settings,
1428                                           const gchar            *prop_name,
1429                                           const GtkSettingsValue *new_value,
1430                                           GtkSettingsSource       source)
1431 {
1432   GtkSettingsValuePrivate *qvalue;
1433   GParamSpec *pspec;
1434   gchar *name;
1435   GQuark name_quark;
1436
1437   if (!G_VALUE_HOLDS_LONG (&new_value->value) &&
1438       !G_VALUE_HOLDS_DOUBLE (&new_value->value) &&
1439       !G_VALUE_HOLDS_STRING (&new_value->value) &&
1440       !G_VALUE_HOLDS (&new_value->value, G_TYPE_GSTRING))
1441     {
1442       g_warning (G_STRLOC ": value type invalid");
1443       return;
1444     }
1445   
1446   name = g_strdup (prop_name);
1447   g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
1448   name_quark = g_quark_from_string (name);
1449   g_free (name);
1450
1451   qvalue = g_datalist_id_get_data (&settings->queued_settings, name_quark);
1452   if (!qvalue)
1453     {
1454       qvalue = g_slice_new0 (GtkSettingsValuePrivate);
1455       g_datalist_id_set_data_full (&settings->queued_settings, name_quark, qvalue, free_value);
1456     }
1457   else
1458     {
1459       g_free (qvalue->public.origin);
1460       g_value_unset (&qvalue->public.value);
1461     }
1462   qvalue->public.origin = g_strdup (new_value->origin);
1463   g_value_init (&qvalue->public.value, G_VALUE_TYPE (&new_value->value));
1464   g_value_copy (&new_value->value, &qvalue->public.value);
1465   qvalue->source = source;
1466   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (settings), g_quark_to_string (name_quark));
1467   if (pspec)
1468     apply_queued_setting (settings, pspec, qvalue);
1469 }
1470
1471 void
1472 gtk_settings_set_property_value (GtkSettings            *settings,
1473                                  const gchar            *prop_name,
1474                                  const GtkSettingsValue *new_value)
1475 {
1476   g_return_if_fail (GTK_SETTINGS (settings));
1477   g_return_if_fail (prop_name != NULL);
1478   g_return_if_fail (new_value != NULL);
1479
1480   gtk_settings_set_property_value_internal (settings, prop_name, new_value,
1481                                             GTK_SETTINGS_SOURCE_APPLICATION);
1482 }
1483
1484 void
1485 _gtk_settings_set_property_value_from_rc (GtkSettings            *settings,
1486                                           const gchar            *prop_name,
1487                                           const GtkSettingsValue *new_value)
1488 {
1489   g_return_if_fail (GTK_SETTINGS (settings));
1490   g_return_if_fail (prop_name != NULL);
1491   g_return_if_fail (new_value != NULL);
1492
1493   gtk_settings_set_property_value_internal (settings, prop_name, new_value,
1494                                             GTK_SETTINGS_SOURCE_RC_FILE);
1495 }
1496
1497 void
1498 gtk_settings_set_string_property (GtkSettings *settings,
1499                                   const gchar *name,
1500                                   const gchar *v_string,
1501                                   const gchar *origin)
1502 {
1503   GtkSettingsValue svalue = { NULL, { 0, }, };
1504
1505   g_return_if_fail (GTK_SETTINGS (settings));
1506   g_return_if_fail (name != NULL);
1507   g_return_if_fail (v_string != NULL);
1508
1509   svalue.origin = (gchar*) origin;
1510   g_value_init (&svalue.value, G_TYPE_STRING);
1511   g_value_set_static_string (&svalue.value, v_string);
1512   gtk_settings_set_property_value (settings, name, &svalue);
1513   g_value_unset (&svalue.value);
1514 }
1515
1516 void
1517 gtk_settings_set_long_property (GtkSettings *settings,
1518                                 const gchar *name,
1519                                 glong        v_long,
1520                                 const gchar *origin)
1521 {
1522   GtkSettingsValue svalue = { NULL, { 0, }, };
1523   
1524   g_return_if_fail (GTK_SETTINGS (settings));
1525   g_return_if_fail (name != NULL);
1526
1527   svalue.origin = (gchar*) origin;
1528   g_value_init (&svalue.value, G_TYPE_LONG);
1529   g_value_set_long (&svalue.value, v_long);
1530   gtk_settings_set_property_value (settings, name, &svalue);
1531   g_value_unset (&svalue.value);
1532 }
1533
1534 void
1535 gtk_settings_set_double_property (GtkSettings *settings,
1536                                   const gchar *name,
1537                                   gdouble      v_double,
1538                                   const gchar *origin)
1539 {
1540   GtkSettingsValue svalue = { NULL, { 0, }, };
1541
1542   g_return_if_fail (GTK_SETTINGS (settings));
1543   g_return_if_fail (name != NULL);
1544
1545   svalue.origin = (gchar*) origin;
1546   g_value_init (&svalue.value, G_TYPE_DOUBLE);
1547   g_value_set_double (&svalue.value, v_double);
1548   gtk_settings_set_property_value (settings, name, &svalue);
1549   g_value_unset (&svalue.value);
1550 }
1551
1552 /**
1553  * gtk_rc_property_parse_color:
1554  * @pspec: a #GParamSpec
1555  * @gstring: the #GString to be parsed
1556  * @property_value: a #GValue which must hold #GdkColor values.
1557  * 
1558  * A #GtkRcPropertyParser for use with gtk_settings_install_property_parser()
1559  * or gtk_widget_class_install_style_property_parser() which parses a
1560  * color given either by its name or in the form 
1561  * <literal>{ red, green, blue }</literal> where %red, %green and
1562  * %blue are integers between 0 and 65535 or floating-point numbers
1563  * between 0 and 1.
1564  * 
1565  * Return value: %TRUE if @gstring could be parsed and @property_value
1566  * has been set to the resulting #GdkColor.
1567  **/
1568 gboolean
1569 gtk_rc_property_parse_color (const GParamSpec *pspec,
1570                              const GString    *gstring,
1571                              GValue           *property_value)
1572 {
1573   GdkColor color = { 0, 0, 0, 0, };
1574   GScanner *scanner;
1575   gboolean success;
1576
1577   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1578   g_return_val_if_fail (G_VALUE_HOLDS (property_value, GDK_TYPE_COLOR), FALSE);
1579
1580   scanner = gtk_rc_scanner_new ();
1581   g_scanner_input_text (scanner, gstring->str, gstring->len);
1582   if (gtk_rc_parse_color (scanner, &color) == G_TOKEN_NONE &&
1583       g_scanner_get_next_token (scanner) == G_TOKEN_EOF)
1584     {
1585       g_value_set_boxed (property_value, &color);
1586       success = TRUE;
1587     }
1588   else
1589     success = FALSE;
1590   g_scanner_destroy (scanner);
1591
1592   return success;
1593 }
1594
1595 /**
1596  * gtk_rc_property_parse_enum:
1597  * @pspec: a #GParamSpec
1598  * @gstring: the #GString to be parsed
1599  * @property_value: a #GValue which must hold enum values.
1600  * 
1601  * A #GtkRcPropertyParser for use with gtk_settings_install_property_parser()
1602  * or gtk_widget_class_install_style_property_parser() which parses a single
1603  * enumeration value.
1604  *
1605  * The enumeration value can be specified by its name, its nickname or
1606  * its numeric value. For consistency with flags parsing, the value
1607  * may be surrounded by parentheses.
1608  * 
1609  * Return value: %TRUE if @gstring could be parsed and @property_value
1610  * has been set to the resulting #GEnumValue.
1611  **/
1612 gboolean
1613 gtk_rc_property_parse_enum (const GParamSpec *pspec,
1614                             const GString    *gstring,
1615                             GValue           *property_value)
1616 {
1617   gboolean need_closing_brace = FALSE, success = FALSE;
1618   GScanner *scanner;
1619   GEnumValue *enum_value = NULL;
1620
1621   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1622   g_return_val_if_fail (G_VALUE_HOLDS_ENUM (property_value), FALSE);
1623
1624   scanner = gtk_rc_scanner_new ();
1625   g_scanner_input_text (scanner, gstring->str, gstring->len);
1626
1627   /* we just want to parse _one_ value, but for consistency with flags parsing
1628    * we support optional parenthesis
1629    */
1630   g_scanner_get_next_token (scanner);
1631   if (scanner->token == '(')
1632     {
1633       need_closing_brace = TRUE;
1634       g_scanner_get_next_token (scanner);
1635     }
1636   if (scanner->token == G_TOKEN_IDENTIFIER)
1637     {
1638       GEnumClass *class = G_PARAM_SPEC_ENUM (pspec)->enum_class;
1639       
1640       enum_value = g_enum_get_value_by_name (class, scanner->value.v_identifier);
1641       if (!enum_value)
1642         enum_value = g_enum_get_value_by_nick (class, scanner->value.v_identifier);
1643       if (enum_value)
1644         {
1645           g_value_set_enum (property_value, enum_value->value);
1646           success = TRUE;
1647         }
1648     }
1649   else if (scanner->token == G_TOKEN_INT)
1650     {
1651       g_value_set_enum (property_value, scanner->value.v_int);
1652       success = TRUE;
1653     }
1654   if (need_closing_brace && g_scanner_get_next_token (scanner) != ')')
1655     success = FALSE;
1656   if (g_scanner_get_next_token (scanner) != G_TOKEN_EOF)
1657     success = FALSE;
1658
1659   g_scanner_destroy (scanner);
1660
1661   return success;
1662 }
1663
1664 static guint
1665 parse_flags_value (GScanner    *scanner,
1666                    GFlagsClass *class,
1667                    guint       *number)
1668 {
1669   g_scanner_get_next_token (scanner);
1670   if (scanner->token == G_TOKEN_IDENTIFIER)
1671     {
1672       GFlagsValue *flags_value;
1673
1674       flags_value = g_flags_get_value_by_name (class, scanner->value.v_identifier);
1675       if (!flags_value)
1676         flags_value = g_flags_get_value_by_nick (class, scanner->value.v_identifier);
1677       if (flags_value)
1678         {
1679           *number |= flags_value->value;
1680           return G_TOKEN_NONE;
1681         }
1682     }
1683   else if (scanner->token == G_TOKEN_INT)
1684     {
1685       *number |= scanner->value.v_int;
1686       return G_TOKEN_NONE;
1687     }
1688   return G_TOKEN_IDENTIFIER;
1689 }
1690
1691 /**
1692  * gtk_rc_property_parse_flags:
1693  * @pspec: a #GParamSpec
1694  * @gstring: the #GString to be parsed
1695  * @property_value: a #GValue which must hold flags values.
1696  * 
1697  * A #GtkRcPropertyParser for use with gtk_settings_install_property_parser()
1698  * or gtk_widget_class_install_style_property_parser() which parses flags. 
1699  * 
1700  * Flags can be specified by their name, their nickname or
1701  * numerically. Multiple flags can be specified in the form 
1702  * <literal>"( flag1 | flag2 | ... )"</literal>.
1703  * 
1704  * Return value: %TRUE if @gstring could be parsed and @property_value
1705  * has been set to the resulting flags value.
1706  **/
1707 gboolean
1708 gtk_rc_property_parse_flags (const GParamSpec *pspec,
1709                              const GString    *gstring,
1710                              GValue           *property_value)
1711 {
1712   GFlagsClass *class;
1713    gboolean success = FALSE;
1714   GScanner *scanner;
1715
1716   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1717   g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (property_value), FALSE);
1718
1719   class = G_PARAM_SPEC_FLAGS (pspec)->flags_class;
1720   scanner = gtk_rc_scanner_new ();
1721   g_scanner_input_text (scanner, gstring->str, gstring->len);
1722
1723   /* parse either a single flags value or a "\( ... [ \| ... ] \)" compound */
1724   if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER ||
1725       scanner->next_token == G_TOKEN_INT)
1726     {
1727       guint token, flags_value = 0;
1728       
1729       token = parse_flags_value (scanner, class, &flags_value);
1730
1731       if (token == G_TOKEN_NONE && g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1732         {
1733           success = TRUE;
1734           g_value_set_flags (property_value, flags_value);
1735         }
1736       
1737     }
1738   else if (g_scanner_get_next_token (scanner) == '(')
1739     {
1740       guint token, flags_value = 0;
1741
1742       /* parse first value */
1743       token = parse_flags_value (scanner, class, &flags_value);
1744
1745       /* parse nth values, preceeded by '|' */
1746       while (token == G_TOKEN_NONE && g_scanner_get_next_token (scanner) == '|')
1747         token = parse_flags_value (scanner, class, &flags_value);
1748
1749       /* done, last token must have closed expression */
1750       if (token == G_TOKEN_NONE && scanner->token == ')' &&
1751           g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1752         {
1753           g_value_set_flags (property_value, flags_value);
1754           success = TRUE;
1755         }
1756     }
1757   g_scanner_destroy (scanner);
1758
1759   return success;
1760 }
1761
1762 static gboolean
1763 get_braced_int (GScanner *scanner,
1764                 gboolean  first,
1765                 gboolean  last,
1766                 gint     *value)
1767 {
1768   if (first)
1769     {
1770       g_scanner_get_next_token (scanner);
1771       if (scanner->token != '{')
1772         return FALSE;
1773     }
1774
1775   g_scanner_get_next_token (scanner);
1776   if (scanner->token != G_TOKEN_INT)
1777     return FALSE;
1778
1779   *value = scanner->value.v_int;
1780
1781   if (last)
1782     {
1783       g_scanner_get_next_token (scanner);
1784       if (scanner->token != '}')
1785         return FALSE;
1786     }
1787   else
1788     {
1789       g_scanner_get_next_token (scanner);
1790       if (scanner->token != ',')
1791         return FALSE;
1792     }
1793
1794   return TRUE;
1795 }
1796
1797 /**
1798  * gtk_rc_property_parse_requisition:
1799  * @pspec: a #GParamSpec
1800  * @gstring: the #GString to be parsed
1801  * @property_value: a #GValue which must hold boxed values.
1802  * 
1803  * A #GtkRcPropertyParser for use with gtk_settings_install_property_parser()
1804  * or gtk_widget_class_install_style_property_parser() which parses a
1805  * requisition in the form 
1806  * <literal>"{ width, height }"</literal> for integers %width and %height.
1807  * 
1808  * Return value: %TRUE if @gstring could be parsed and @property_value
1809  * has been set to the resulting #GtkRequisition.
1810  **/
1811 gboolean
1812 gtk_rc_property_parse_requisition  (const GParamSpec *pspec,
1813                                     const GString    *gstring,
1814                                     GValue           *property_value)
1815 {
1816   GtkRequisition requisition;
1817   GScanner *scanner;
1818   gboolean success = FALSE;
1819
1820   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1821   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (property_value), FALSE);
1822
1823   scanner = gtk_rc_scanner_new ();
1824   g_scanner_input_text (scanner, gstring->str, gstring->len);
1825
1826   if (get_braced_int (scanner, TRUE, FALSE, &requisition.width) &&
1827       get_braced_int (scanner, FALSE, TRUE, &requisition.height))
1828     {
1829       g_value_set_boxed (property_value, &requisition);
1830       success = TRUE;
1831     }
1832
1833   g_scanner_destroy (scanner);
1834
1835   return success;
1836 }
1837
1838 /**
1839  * gtk_rc_property_parse_border:
1840  * @pspec: a #GParamSpec
1841  * @gstring: the #GString to be parsed
1842  * @property_value: a #GValue which must hold boxed values.
1843  * 
1844  * A #GtkRcPropertyParser for use with gtk_settings_install_property_parser()
1845  * or gtk_widget_class_install_style_property_parser() which parses
1846  * borders in the form 
1847  * <literal>"{ left, right, top, bottom }"</literal> for integers 
1848  * %left, %right, %top and %bottom.
1849  * 
1850  * Return value: %TRUE if @gstring could be parsed and @property_value
1851  * has been set to the resulting #GtkBorder.
1852  **/
1853 gboolean
1854 gtk_rc_property_parse_border (const GParamSpec *pspec,
1855                               const GString    *gstring,
1856                               GValue           *property_value)
1857 {
1858   GtkBorder border;
1859   GScanner *scanner;
1860   gboolean success = FALSE;
1861
1862   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), FALSE);
1863   g_return_val_if_fail (G_VALUE_HOLDS_BOXED (property_value), FALSE);
1864
1865   scanner = gtk_rc_scanner_new ();
1866   g_scanner_input_text (scanner, gstring->str, gstring->len);
1867
1868   if (get_braced_int (scanner, TRUE, FALSE, &border.left) &&
1869       get_braced_int (scanner, FALSE, FALSE, &border.right) &&
1870       get_braced_int (scanner, FALSE, FALSE, &border.top) &&
1871       get_braced_int (scanner, FALSE, TRUE, &border.bottom))
1872     {
1873       g_value_set_boxed (property_value, &border);
1874       success = TRUE;
1875     }
1876
1877   g_scanner_destroy (scanner);
1878
1879   return success;
1880 }
1881
1882 void
1883 _gtk_settings_handle_event (GdkEventSetting *event)
1884 {
1885   GtkSettings *settings;
1886   GParamSpec *pspec;
1887   guint property_id;
1888
1889   settings = gtk_settings_get_for_screen (gdk_drawable_get_screen (event->window));
1890   pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (settings), event->name);
1891  
1892   if (pspec) 
1893     {
1894       property_id = pspec->param_id;
1895
1896       if (property_id == PROP_COLOR_SCHEME)
1897         {
1898           GValue value = { 0, };
1899  
1900           g_value_init (&value, G_TYPE_STRING);
1901           if (!gdk_screen_get_setting (settings->screen, pspec->name, &value))
1902             g_value_set_static_string (&value, "");
1903           merge_color_scheme (settings, &value, GTK_SETTINGS_SOURCE_XSETTING);
1904           g_value_unset (&value);
1905         }
1906       g_object_notify (G_OBJECT (settings), pspec->name);
1907    }
1908 }
1909
1910 static void
1911 reset_rc_values_foreach (GQuark    key_id,
1912                          gpointer  data,
1913                          gpointer  user_data)
1914 {
1915   GtkSettingsValuePrivate *qvalue = data;
1916   GSList **to_reset = user_data;
1917
1918   if (qvalue->source == GTK_SETTINGS_SOURCE_RC_FILE)
1919     *to_reset = g_slist_prepend (*to_reset, GUINT_TO_POINTER (key_id));
1920 }
1921
1922 void
1923 _gtk_settings_reset_rc_values (GtkSettings *settings)
1924 {
1925   GSList *to_reset = NULL;
1926   GSList *tmp_list;
1927   GParamSpec **pspecs, **p;
1928   gint i;
1929
1930   /* Remove any queued settings
1931    */
1932   g_datalist_foreach (&settings->queued_settings,
1933                       reset_rc_values_foreach,
1934                       &to_reset);
1935
1936   for (tmp_list = to_reset; tmp_list; tmp_list = tmp_list->next)
1937     {
1938       GQuark key_id = GPOINTER_TO_UINT (tmp_list->data);
1939       g_datalist_id_remove_data (&settings->queued_settings, key_id);
1940     }
1941
1942    g_slist_free (to_reset);
1943
1944   /* Now reset the active settings
1945    */
1946   pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (settings), NULL);
1947   i = 0;
1948
1949   g_object_freeze_notify (G_OBJECT (settings));
1950   for (p = pspecs; *p; p++)
1951     {
1952       if (settings->property_values[i].source == GTK_SETTINGS_SOURCE_RC_FILE)
1953         {
1954           GParamSpec *pspec = *p;
1955
1956           g_param_value_set_default (pspec, &settings->property_values[i].value);
1957           g_object_notify (G_OBJECT (settings), pspec->name);
1958         }
1959       i++;
1960     }
1961   g_object_thaw_notify (G_OBJECT (settings));
1962   g_free (pspecs);
1963 }
1964
1965 static void
1966 settings_update_double_click (GtkSettings *settings)
1967 {
1968   if (gdk_screen_get_number (settings->screen) == 0)
1969     {
1970       GdkDisplay *display = gdk_screen_get_display (settings->screen);
1971       gint double_click_time;
1972       gint double_click_distance;
1973   
1974       g_object_get (settings, 
1975                     "gtk-double-click-time", &double_click_time, 
1976                     "gtk-double-click-distance", &double_click_distance,
1977                     NULL);
1978       
1979       gdk_display_set_double_click_time (display, double_click_time);
1980       gdk_display_set_double_click_distance (display, double_click_distance);
1981     }
1982 }
1983
1984 static void
1985 settings_update_modules (GtkSettings *settings)
1986 {
1987   gchar *modules;
1988   
1989   g_object_get (settings, 
1990                 "gtk-modules", &modules,
1991                 NULL);
1992   
1993   _gtk_modules_settings_changed (settings, modules);
1994   
1995   g_free (modules);
1996 }
1997
1998 #ifdef GDK_WINDOWING_X11
1999 static void
2000 settings_update_cursor_theme (GtkSettings *settings)
2001 {
2002   GdkDisplay *display = gdk_screen_get_display (settings->screen);
2003   gchar *theme = NULL;
2004   gint size = 0;
2005   
2006   g_object_get (settings, 
2007                 "gtk-cursor-theme-name", &theme,
2008                 "gtk-cursor-theme-size", &size,
2009                 NULL);
2010   
2011   gdk_x11_display_set_cursor_theme (display, theme, size);
2012
2013   g_free (theme);
2014 }
2015
2016 static void
2017 settings_update_font_options (GtkSettings *settings)
2018 {
2019   gint hinting;
2020   gchar *hint_style_str;
2021   cairo_hint_style_t hint_style = CAIRO_HINT_STYLE_NONE;
2022   gint antialias;
2023   cairo_antialias_t antialias_mode = CAIRO_ANTIALIAS_GRAY;
2024   gchar *rgba_str;
2025   cairo_subpixel_order_t subpixel_order = CAIRO_SUBPIXEL_ORDER_DEFAULT;
2026   cairo_font_options_t *options;
2027   
2028   g_object_get (settings,
2029                 "gtk-xft-antialias", &antialias,
2030                 "gtk-xft-hinting", &hinting,
2031                 "gtk-xft-hintstyle", &hint_style_str,
2032                 "gtk-xft-rgba", &rgba_str,
2033                 NULL);
2034
2035   options = cairo_font_options_create ();
2036
2037   cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_ON);
2038   
2039   if (hinting >= 0 && !hinting)
2040     {
2041       hint_style = CAIRO_HINT_STYLE_NONE;
2042     }
2043   else if (hint_style_str)
2044     {
2045       if (strcmp (hint_style_str, "hintnone") == 0)
2046         hint_style = CAIRO_HINT_STYLE_NONE;
2047       else if (strcmp (hint_style_str, "hintslight") == 0)
2048         hint_style = CAIRO_HINT_STYLE_SLIGHT;
2049       else if (strcmp (hint_style_str, "hintmedium") == 0)
2050         hint_style = CAIRO_HINT_STYLE_MEDIUM;
2051       else if (strcmp (hint_style_str, "hintfull") == 0)
2052         hint_style = CAIRO_HINT_STYLE_FULL;
2053     }
2054
2055   g_free (hint_style_str);
2056
2057   cairo_font_options_set_hint_style (options, hint_style);
2058
2059   if (rgba_str)
2060     {
2061       if (strcmp (rgba_str, "rgb") == 0)
2062         subpixel_order = CAIRO_SUBPIXEL_ORDER_RGB;
2063       else if (strcmp (rgba_str, "bgr") == 0)
2064         subpixel_order = CAIRO_SUBPIXEL_ORDER_BGR;
2065       else if (strcmp (rgba_str, "vrgb") == 0)
2066         subpixel_order = CAIRO_SUBPIXEL_ORDER_VRGB;
2067       else if (strcmp (rgba_str, "vbgr") == 0)
2068         subpixel_order = CAIRO_SUBPIXEL_ORDER_VBGR;
2069
2070       g_free (rgba_str);
2071     }
2072
2073   cairo_font_options_set_subpixel_order (options, subpixel_order);
2074   
2075   if (antialias >= 0 && !antialias)
2076     antialias_mode = CAIRO_ANTIALIAS_NONE;
2077   else if (subpixel_order != CAIRO_SUBPIXEL_ORDER_DEFAULT)
2078     antialias_mode = CAIRO_ANTIALIAS_SUBPIXEL;
2079   else if (antialias >= 0)
2080     antialias_mode = CAIRO_ANTIALIAS_GRAY;
2081   
2082   cairo_font_options_set_antialias (options, antialias_mode);
2083
2084   gdk_screen_set_font_options (settings->screen, options);
2085   
2086   cairo_font_options_destroy (options);
2087 }
2088
2089 #ifdef GDK_WINDOWING_X11
2090 static gboolean
2091 settings_update_fontconfig (GtkSettings *settings)
2092 {
2093   static guint    last_update_timestamp;
2094   static gboolean last_update_needed;
2095
2096   guint timestamp;
2097
2098   g_object_get (settings,
2099                 "gtk-fontconfig-timestamp", &timestamp,
2100                 NULL);
2101
2102   /* if timestamp is the same as last_update_timestamp, we already have
2103    * updated fontconig on this timestamp (another screen requested it perhaps?),
2104    * just return the cached result.*/
2105
2106   if (timestamp != last_update_timestamp)
2107     {
2108       PangoFontMap *fontmap = pango_cairo_font_map_get_default ();
2109       gboolean update_needed = FALSE;
2110
2111       /* bug 547680 */
2112       if (PANGO_IS_FC_FONT_MAP (fontmap) && !FcConfigUptoDate (NULL))
2113         {
2114           pango_fc_font_map_cache_clear (PANGO_FC_FONT_MAP (fontmap));
2115           if (FcInitReinitialize ())
2116             update_needed = TRUE;
2117         }
2118
2119       last_update_timestamp = timestamp;
2120       last_update_needed = update_needed;
2121     }
2122
2123   return last_update_needed;
2124 }
2125 #endif /* GDK_WINDOWING_X11 */
2126
2127 static void
2128 settings_update_resolution (GtkSettings *settings)
2129 {
2130   gint dpi_int;
2131   double dpi;
2132   
2133   g_object_get (settings,
2134                 "gtk-xft-dpi", &dpi_int,
2135                 NULL);
2136
2137   if (dpi_int > 0)
2138     dpi = dpi_int / 1024.;
2139   else
2140     dpi = -1.;
2141
2142   gdk_screen_set_resolution (settings->screen, dpi);
2143 }
2144 #endif
2145
2146 typedef struct {
2147   GHashTable *color_hash;
2148   GHashTable *tables[GTK_SETTINGS_SOURCE_APPLICATION + 1];
2149   gchar *lastentry[GTK_SETTINGS_SOURCE_APPLICATION + 1];
2150 } ColorSchemeData;
2151
2152 static void
2153 color_scheme_data_free (ColorSchemeData *data)
2154 {
2155   gint i;
2156
2157   g_hash_table_unref (data->color_hash);
2158
2159   for (i = 0; i <= GTK_SETTINGS_SOURCE_APPLICATION; i++)
2160     {
2161       if (data->tables[i])
2162         g_hash_table_unref (data->tables[i]);
2163       g_free (data->lastentry[i]);
2164     }
2165
2166   g_slice_free (ColorSchemeData, data);
2167 }
2168
2169 static void
2170 settings_update_color_scheme (GtkSettings *settings)
2171 {
2172   if (!g_object_get_data (G_OBJECT (settings), "gtk-color-scheme"))
2173     {
2174       ColorSchemeData *data;
2175       GValue value = { 0, };
2176
2177       data = g_slice_new0 (ColorSchemeData);
2178       data->color_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2179                                                 (GDestroyNotify) gdk_color_free);
2180       g_object_set_data_full (G_OBJECT (settings), "gtk-color-scheme",
2181                               data, (GDestroyNotify) color_scheme_data_free); 
2182
2183       g_value_init (&value, G_TYPE_STRING);
2184       if (gdk_screen_get_setting (settings->screen, "gtk-color-scheme", &value))
2185         {
2186           merge_color_scheme (settings, &value, GTK_SETTINGS_SOURCE_XSETTING);
2187           g_value_unset (&value);
2188         }
2189    }
2190 }
2191
2192 static gboolean
2193 add_color_to_hash (gchar      *name, 
2194                    GdkColor   *color, 
2195                    GHashTable *target)
2196 {
2197   GdkColor *old;
2198
2199   old = g_hash_table_lookup (target, name);
2200   if (!old || !gdk_color_equal (old, color))
2201     {
2202       g_hash_table_insert (target, g_strdup (name), gdk_color_copy (color));
2203       
2204       return TRUE;
2205     }
2206
2207   return FALSE;
2208 }
2209
2210 static gboolean
2211 add_colors_to_hash_from_string (GHashTable  *hash, 
2212                                 const gchar *colors)
2213 {
2214   gchar *s, *p, *name;
2215   GdkColor color;
2216   gboolean changed = FALSE;
2217   gchar *copy;
2218
2219   copy = g_strdup (colors);
2220   s = copy;
2221   while (s && *s)
2222     {
2223       name = s;
2224       p = strchr (s, ':');
2225       if (p)
2226         {
2227           *p = '\0';
2228           p++;
2229         }
2230       else
2231         break;
2232
2233       while (*p == ' ')
2234         p++;
2235
2236       s = p;
2237       while (*s) 
2238         {
2239           if (*s == '\n' || *s == ';')
2240             {
2241               *s = '\0';
2242               s++;
2243               break;
2244             }
2245           s++;
2246         }
2247
2248       if (gdk_color_parse (p, &color))
2249         changed |= add_color_to_hash (name, &color, hash);
2250     }
2251
2252   g_free (copy);
2253
2254   return changed;
2255 }
2256
2257 static gboolean
2258 update_color_hash (ColorSchemeData   *data,
2259                    const gchar       *str, 
2260                    GtkSettingsSource  source)
2261 {
2262   gboolean changed = FALSE;
2263   gint i;
2264   GHashTable *old_hash;
2265
2266   if ((str == NULL || *str == '\0') && 
2267       (data->lastentry[source] == NULL || data->lastentry[source][0] == '\0'))
2268     return FALSE;
2269
2270   if (str && data->lastentry[source] && strcmp (str, data->lastentry[source]) == 0)
2271     return FALSE;
2272
2273   /* For the RC_FILE source we merge the values rather than over-writing 
2274    * them, since multiple rc files might define independent sets of colors
2275    */
2276   if ((source != GTK_SETTINGS_SOURCE_RC_FILE) && 
2277       data->tables[source] && g_hash_table_size (data->tables[source]) > 0)
2278     {
2279       g_hash_table_unref (data->tables[source]);
2280       data->tables[source] = NULL;
2281       changed = TRUE; /* We can't rely on the code below since str might be "" */
2282     }
2283
2284   if (data->tables[source] == NULL)
2285     data->tables[source] = g_hash_table_new_full (g_str_hash, g_str_equal, 
2286                                                   g_free,
2287                                                   (GDestroyNotify) gdk_color_free);
2288
2289   g_free (data->lastentry[source]);
2290   data->lastentry[source] = g_strdup (str);
2291   
2292   changed |= add_colors_to_hash_from_string (data->tables[source], str);
2293
2294   if (!changed)
2295     return FALSE;
2296     
2297   /* Rebuild the merged hash table. */
2298   old_hash = data->color_hash;
2299   data->color_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free,
2300                                             (GDestroyNotify) gdk_color_free);
2301   for (i = 0; i <= GTK_SETTINGS_SOURCE_APPLICATION; i++)
2302     {
2303       if (data->tables[i])
2304         g_hash_table_foreach (data->tables[i], (GHFunc) add_color_to_hash,
2305                               data->color_hash);
2306     }
2307
2308   if (old_hash)
2309     {
2310       /* now check if the merged hash has changed */
2311       changed = FALSE;
2312       if (g_hash_table_size (old_hash) != g_hash_table_size (data->color_hash))
2313         changed = TRUE;
2314       else
2315         {
2316           GHashTableIter iter;
2317           gpointer key, value, new_value;
2318
2319           g_hash_table_iter_init (&iter, old_hash);
2320           while (g_hash_table_iter_next (&iter, &key, &value))
2321             {
2322               new_value = g_hash_table_lookup (data->color_hash, key);
2323               if (!new_value || !gdk_color_equal (value, new_value))
2324                 {
2325                   changed = TRUE;
2326                   break;
2327                 } 
2328             }
2329         }
2330
2331       g_hash_table_unref (old_hash);
2332     }
2333   else 
2334     changed = TRUE;
2335
2336   return changed;
2337 }
2338
2339 static void
2340 merge_color_scheme (GtkSettings       *settings, 
2341                     const GValue      *value, 
2342                     GtkSettingsSource  source)
2343 {
2344   ColorSchemeData *data;
2345   const gchar *colors;
2346
2347   g_object_freeze_notify (G_OBJECT (settings));
2348
2349   colors = g_value_get_string (value);
2350
2351   settings_update_color_scheme (settings);
2352
2353   data = (ColorSchemeData *) g_object_get_data (G_OBJECT (settings),
2354                                                 "gtk-color-scheme");
2355   
2356   if (update_color_hash (data, colors, source))
2357     g_object_notify (G_OBJECT (settings), "color-hash");
2358
2359   g_object_thaw_notify (G_OBJECT (settings));
2360 }
2361
2362 static GHashTable *
2363 get_color_hash (GtkSettings *settings)
2364 {
2365   ColorSchemeData *data;
2366
2367   settings_update_color_scheme (settings);
2368   
2369   data = (ColorSchemeData *)g_object_get_data (G_OBJECT (settings), 
2370                                                "gtk-color-scheme");
2371
2372   return data->color_hash;
2373 }
2374
2375 static void 
2376 append_color_scheme (gpointer key,
2377                      gpointer value,
2378                      gpointer data)
2379 {
2380   gchar *name = (gchar *)key;
2381   GdkColor *color = (GdkColor *)value;
2382   GString *string = (GString *)data;
2383
2384   g_string_append_printf (string, "%s: #%04x%04x%04x\n",
2385                           name, color->red, color->green, color->blue);
2386 }
2387
2388 static gchar *
2389 get_color_scheme (GtkSettings *settings)
2390 {
2391   ColorSchemeData *data;
2392   GString *string;
2393   
2394   settings_update_color_scheme (settings);
2395
2396   data = (ColorSchemeData *) g_object_get_data (G_OBJECT (settings),
2397                                                 "gtk-color-scheme");
2398
2399   string = g_string_new ("");
2400
2401   g_hash_table_foreach (data->color_hash, append_color_scheme, string);
2402
2403   return g_string_free (string, FALSE);
2404 }
2405
2406
2407 #define __GTK_SETTINGS_C__
2408 #include "gtkaliasdef.c"