]> Pileus Git - ~andy/gtk/blob - gtk/gtkrc.c
- Replace GdkPointerHooks with a multihead-safe GdkDisplayPointerHooks,
[~andy/gtk] / gtk / gtkrc.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include <locale.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <sys/stat.h>
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
36 #endif
37 #include <fcntl.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41
42 #ifndef HAVE_LSTAT
43 #define lstat stat
44 #endif
45
46 #include <glib.h>
47 #include "gdkconfig.h"
48
49 #include "gtkversion.h"
50 #include "gtkrc.h"
51 #include "gtkbindings.h"
52 #include "gtkthemes.h"
53 #include "gtkintl.h"
54 #include "gtkiconfactory.h"
55 #include "gtkmain.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
58 #include "gtkwindow.h"
59
60 #ifdef G_OS_WIN32
61 #include <io.h>
62 #endif
63
64 typedef struct _GtkRcSet    GtkRcSet;
65 typedef struct _GtkRcNode   GtkRcNode;
66 typedef struct _GtkRcFile   GtkRcFile;
67
68 struct _GtkRcSet
69 {
70   GPatternSpec *pspec;
71   GtkRcStyle *rc_style;
72   gint priority;
73 };
74
75 struct _GtkRcFile
76 {
77   gboolean is_string;           /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
78   time_t mtime;
79   gchar *name;
80   gchar *canonical_name;
81   guint reload;
82 };
83
84 #define GTK_RC_MAX_PIXMAP_PATHS 128
85
86 struct _GtkRcContext
87 {
88   GHashTable *rc_style_ht;
89   GtkSettings *settings;
90   GSList *rc_sets_widget;
91   GSList *rc_sets_widget_class;
92   GSList *rc_sets_class;
93
94   /* The files we have parsed, to reread later if necessary */
95   GSList *rc_files;
96
97   gchar *theme_name;
98   gchar *key_theme_name;
99   gchar *font_name;
100   
101   gchar *pixmap_path[GTK_RC_MAX_PIXMAP_PATHS];
102
103   gint default_priority;
104   GtkStyle *default_style;
105 };
106
107 static GtkRcContext *gtk_rc_context_get              (GtkSettings     *settings);
108
109 static guint       gtk_rc_style_hash                 (const gchar     *name);
110 static gboolean    gtk_rc_style_equal                (const gchar     *a,
111                                                       const gchar     *b);
112 static guint       gtk_rc_styles_hash                (const GSList    *rc_styles);
113 static gboolean    gtk_rc_styles_equal               (const GSList    *a,
114                                                       const GSList    *b);
115 static GtkRcStyle* gtk_rc_style_find                 (GtkRcContext    *context,
116                                                       const gchar     *name);
117 static GSList *    gtk_rc_styles_match               (GSList          *rc_styles,
118                                                       GSList          *sets,
119                                                       guint            path_length,
120                                                       const gchar     *path,
121                                                       const gchar     *path_reversed);
122 static GtkStyle *  gtk_rc_style_to_style             (GtkRcContext    *context,
123                                                       GtkRcStyle      *rc_style);
124 static GtkStyle*   gtk_rc_init_style                 (GtkRcContext    *context,
125                                                       GSList          *rc_styles);
126 static void        gtk_rc_parse_default_files        (GtkRcContext    *context);
127 static void        gtk_rc_parse_named                (GtkRcContext    *context,
128                                                       const gchar     *name,
129                                                       const gchar     *type);
130 static void        gtk_rc_context_parse_file         (GtkRcContext    *context,
131                                                       const gchar     *filename,
132                                                       gint             priority,
133                                                       gboolean         reload);
134 static void        gtk_rc_parse_any                  (GtkRcContext    *context,
135                                                       const gchar     *input_name,
136                                                       gint             input_fd,
137                                                       const gchar     *input_string);
138 static guint       gtk_rc_parse_statement            (GtkRcContext    *context,
139                                                       GScanner        *scanner);
140 static guint       gtk_rc_parse_style                (GtkRcContext    *context,
141                                                       GScanner        *scanner);
142 static guint       gtk_rc_parse_assignment           (GScanner        *scanner,
143                                                       GtkRcProperty   *prop);
144 static guint       gtk_rc_parse_bg                   (GScanner        *scanner,
145                                                       GtkRcStyle      *style);
146 static guint       gtk_rc_parse_fg                   (GScanner        *scanner,
147                                                       GtkRcStyle      *style);
148 static guint       gtk_rc_parse_text                 (GScanner        *scanner,
149                                                       GtkRcStyle      *style);
150 static guint       gtk_rc_parse_base                 (GScanner        *scanner,
151                                                       GtkRcStyle      *style);
152 static guint       gtk_rc_parse_xthickness           (GScanner        *scanner,
153                                                       GtkRcStyle      *style);
154 static guint       gtk_rc_parse_ythickness           (GScanner        *scanner,
155                                                       GtkRcStyle      *style);
156 static guint       gtk_rc_parse_bg_pixmap            (GtkRcContext    *context,
157                                                       GScanner        *scanner,
158                                                       GtkRcStyle      *rc_style);
159 static guint       gtk_rc_parse_font                 (GScanner        *scanner,
160                                                       GtkRcStyle      *rc_style);
161 static guint       gtk_rc_parse_fontset              (GScanner        *scanner,
162                                                       GtkRcStyle      *rc_style);
163 static guint       gtk_rc_parse_font_name            (GScanner        *scanner,
164                                                       GtkRcStyle      *rc_style);
165 static guint       gtk_rc_parse_engine               (GtkRcContext    *context,
166                                                       GScanner        *scanner,
167                                                       GtkRcStyle     **rc_style);
168 static guint       gtk_rc_parse_pixmap_path          (GtkRcContext    *context,
169                                                       GScanner        *scanner);
170 static void        gtk_rc_parse_pixmap_path_string   (GtkRcContext    *context,
171                                                       GScanner        *scanner,
172                                                       const gchar     *pix_path);
173 static guint       gtk_rc_parse_module_path          (GScanner        *scanner);
174 static guint       gtk_rc_parse_im_module_file       (GScanner        *scanner);
175 static guint       gtk_rc_parse_path_pattern         (GtkRcContext    *context,
176                                                       GScanner        *scanner);
177 static guint       gtk_rc_parse_stock                (GtkRcContext    *context,
178                                                       GScanner        *scanner,
179                                                       GtkRcStyle      *rc_style,
180                                                       GtkIconFactory  *factory);
181 static void        gtk_rc_clear_hash_node            (gpointer         key,
182                                                       gpointer         data,
183                                                       gpointer         user_data);
184 static void        gtk_rc_clear_styles               (GtkRcContext    *context);
185 static void        gtk_rc_add_initial_default_files  (void);
186
187 static void        gtk_rc_style_init                 (GtkRcStyle      *style);
188 static void        gtk_rc_style_class_init           (GtkRcStyleClass *klass);
189 static void        gtk_rc_style_finalize             (GObject         *object);
190 static void        gtk_rc_style_real_merge           (GtkRcStyle      *dest,
191                                                       GtkRcStyle      *src);
192 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle      *rc_style);
193 static GtkStyle*   gtk_rc_style_real_create_style    (GtkRcStyle      *rc_style);
194 static gint        gtk_rc_properties_cmp             (gconstpointer    bsearch_node1,
195                                                       gconstpointer    bsearch_node2);
196
197 static gpointer parent_class = NULL;
198
199 static const GScannerConfig gtk_rc_scanner_config =
200 {
201   (
202    " \t\r\n"
203    )                    /* cset_skip_characters */,
204   (
205    G_CSET_a_2_z
206    "_"
207    G_CSET_A_2_Z
208    )                    /* cset_identifier_first */,
209   (
210    G_CSET_a_2_z
211    "_-0123456789"
212    G_CSET_A_2_Z
213    )                    /* cset_identifier_nth */,
214   ( "#\n" )             /* cpair_comment_single */,
215   
216   TRUE                  /* case_sensitive */,
217   
218   TRUE                  /* skip_comment_multi */,
219   TRUE                  /* skip_comment_single */,
220   TRUE                  /* scan_comment_multi */,
221   TRUE                  /* scan_identifier */,
222   FALSE                 /* scan_identifier_1char */,
223   FALSE                 /* scan_identifier_NULL */,
224   TRUE                  /* scan_symbols */,
225   TRUE                  /* scan_binary */,
226   TRUE                  /* scan_octal */,
227   TRUE                  /* scan_float */,
228   TRUE                  /* scan_hex */,
229   TRUE                  /* scan_hex_dollar */,
230   TRUE                  /* scan_string_sq */,
231   TRUE                  /* scan_string_dq */,
232   TRUE                  /* numbers_2_int */,
233   FALSE                 /* int_2_float */,
234   FALSE                 /* identifier_2_string */,
235   TRUE                  /* char_2_token */,
236   TRUE                  /* symbol_2_token */,
237   FALSE                 /* scope_0_fallback */,
238 };
239
240 static const struct
241 {
242   gchar *name;
243   guint token;
244 } symbols[] = {
245   { "include", GTK_RC_TOKEN_INCLUDE },
246   { "NORMAL", GTK_RC_TOKEN_NORMAL },
247   { "ACTIVE", GTK_RC_TOKEN_ACTIVE },
248   { "PRELIGHT", GTK_RC_TOKEN_PRELIGHT },
249   { "SELECTED", GTK_RC_TOKEN_SELECTED },
250   { "INSENSITIVE", GTK_RC_TOKEN_INSENSITIVE },
251   { "fg", GTK_RC_TOKEN_FG },
252   { "bg", GTK_RC_TOKEN_BG },
253   { "text", GTK_RC_TOKEN_TEXT },
254   { "base", GTK_RC_TOKEN_BASE },
255   { "xthickness", GTK_RC_TOKEN_XTHICKNESS },
256   { "ythickness", GTK_RC_TOKEN_YTHICKNESS },
257   { "font", GTK_RC_TOKEN_FONT },
258   { "fontset", GTK_RC_TOKEN_FONTSET },
259   { "font_name", GTK_RC_TOKEN_FONT_NAME },
260   { "bg_pixmap", GTK_RC_TOKEN_BG_PIXMAP },
261   { "pixmap_path", GTK_RC_TOKEN_PIXMAP_PATH },
262   { "style", GTK_RC_TOKEN_STYLE },
263   { "binding", GTK_RC_TOKEN_BINDING },
264   { "bind", GTK_RC_TOKEN_BIND },
265   { "widget", GTK_RC_TOKEN_WIDGET },
266   { "widget_class", GTK_RC_TOKEN_WIDGET_CLASS },
267   { "class", GTK_RC_TOKEN_CLASS },
268   { "lowest", GTK_RC_TOKEN_LOWEST },
269   { "gtk", GTK_RC_TOKEN_GTK },
270   { "application", GTK_RC_TOKEN_APPLICATION },
271   { "theme", GTK_RC_TOKEN_THEME },
272   { "rc", GTK_RC_TOKEN_RC },
273   { "highest", GTK_RC_TOKEN_HIGHEST },
274   { "engine", GTK_RC_TOKEN_ENGINE },
275   { "module_path", GTK_RC_TOKEN_MODULE_PATH },
276   { "stock", GTK_RC_TOKEN_STOCK },
277   { "im_module_file", GTK_RC_TOKEN_IM_MODULE_FILE },
278   { "LTR", GTK_RC_TOKEN_LTR },
279   { "RTL", GTK_RC_TOKEN_RTL }
280 };
281
282 static GHashTable *realized_style_ht = NULL;
283
284 static gchar *im_module_file = NULL;
285
286 #define GTK_RC_MAX_DEFAULT_FILES 128
287 static gchar *gtk_rc_default_files[GTK_RC_MAX_DEFAULT_FILES];
288
289 /* A stack of directories for RC files we are parsing currently.
290  * these are implicitely added to the end of PIXMAP_PATHS
291  */
292 static GSList *rc_dir_stack = NULL;
293
294 /* RC files and strings that are parsed for every context
295  */
296 static GSList *global_rc_files = NULL;
297
298 /* Keep list of all current RC contexts for convenience
299  */
300 static GSList *rc_contexts;
301
302 /* RC file handling */
303
304 static gchar *
305 gtk_rc_make_default_dir (const gchar *type)
306 {
307   const gchar *var;
308   gchar *path;
309
310   var = g_getenv ("GTK_EXE_PREFIX");
311   if (var)
312     path = g_build_filename (var, "lib", "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
313   else
314     path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
315
316   return path;
317 }
318
319 /**
320  * gtk_rc_get_im_module_path:
321  * @returns: a newly-allocated string containing the path in which to 
322  *    look for IM modules.
323  *
324  * Obtains the path in which to look for IM modules. See the documentation
325  * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
326  * environment variable for more details about looking up modules. This
327  * function is useful solely for utilities supplied with GTK+ and should
328  * not be used by applications under normal circumstances.
329  */
330 gchar *
331 gtk_rc_get_im_module_path (void)
332 {
333   gchar **paths = _gtk_get_module_path ("immodules");
334   gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
335   g_strfreev (paths);
336
337   return result;
338 }
339
340 /**
341  * gtk_rc_get_im_module_file:
342  * @returns: a newly-allocated string containing the name of the file
343  * listing the IM modules available for loading
344  *
345  * Obtains the path to the IM modules file. See the documentation
346  * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
347  * environment variable for more details.
348  */
349 gchar *
350 gtk_rc_get_im_module_file (void)
351 {
352   gchar *result = g_strdup (g_getenv ("GTK_IM_MODULE_FILE"));
353
354   if (!result)
355     {
356       if (im_module_file)
357         result = g_strdup (im_module_file);
358       else
359         result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
360     }
361
362   return result;
363 }
364
365 gchar *
366 gtk_rc_get_theme_dir (void)
367 {
368   const gchar *var;
369   gchar *path;
370
371   var = g_getenv ("GTK_DATA_PREFIX");
372   if (var)
373     path = g_build_filename (var, "share", "themes", NULL);
374   else
375     path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
376
377   return path;
378 }
379
380 /**
381  * gtk_rc_get_module_dir:
382  * 
383  * Returns a directory in which GTK+ looks for theme engines.
384  * For full information about the search for theme engines,
385  * see the docs for <envar>GTK_PATH</envar> in
386  * <xref linkend="gtk-running"/>.
387  * 
388  * return value: the directory. (Must be freed with g_free())
389  **/
390 gchar *
391 gtk_rc_get_module_dir (void)
392 {
393   return gtk_rc_make_default_dir ("engines");
394 }
395
396 static void
397 gtk_rc_add_initial_default_files (void)
398 {
399   static gint init = FALSE;
400   const gchar *var;
401   gchar *str;
402   gchar **files;
403   gint i;
404
405   if (init)
406     return;
407   
408   gtk_rc_default_files[0] = NULL;
409   init = TRUE;
410
411   var = g_getenv ("GTK2_RC_FILES");
412   if (var)
413     {
414       files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, 128);
415       i=0;
416       while (files[i])
417         {
418           gtk_rc_add_default_file (files[i]);
419           i++;
420         }
421       g_strfreev (files);
422     }
423   else
424     {
425       str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
426
427       gtk_rc_add_default_file (str);
428       g_free (str);
429
430       var = g_get_home_dir ();
431       if (var)
432         {
433           str = g_build_filename (var, ".gtkrc-2.0", NULL);
434           gtk_rc_add_default_file (str);
435           g_free (str);
436         }
437     }
438 }
439
440 /**
441  * gtk_rc_add_default_file:
442  * @filename: the pathname to the file. If @filename is not absolute, it
443  *    is searched in the current directory.
444  * 
445  * Adds a file to the list of files to be parsed at the
446  * end of gtk_init().
447  **/
448 void
449 gtk_rc_add_default_file (const gchar *filename)
450 {
451   guint n;
452   
453   gtk_rc_add_initial_default_files ();
454
455   for (n = 0; gtk_rc_default_files[n]; n++) ;
456   if (n >= GTK_RC_MAX_DEFAULT_FILES - 1)
457     return;
458   
459   gtk_rc_default_files[n++] = g_strdup (filename);
460   gtk_rc_default_files[n] = NULL;
461 }
462
463 /**
464  * gtk_rc_set_default_files:
465  * @filenames: A %NULL-terminated list of filenames.
466  * 
467  * Sets the list of files that GTK+ will read at the
468  * end of gtk_init().
469  **/
470 void
471 gtk_rc_set_default_files (gchar **filenames)
472 {
473   gint i;
474
475   gtk_rc_add_initial_default_files ();
476
477   i = 0;
478   while (gtk_rc_default_files[i])
479     {
480       g_free (gtk_rc_default_files[i]);
481       i++;
482     }
483     
484   gtk_rc_default_files[0] = NULL;
485
486   i = 0;
487   while (filenames[i] != NULL)
488     {
489       gtk_rc_add_default_file (filenames[i]);
490       i++;
491     }
492 }
493
494 /**
495  * gtk_rc_get_default_files:
496  * 
497  * Retrieves the current list of RC files that will be parsed
498  * at the end of gtk_init().
499  * 
500  * Return value: A %NULL-terminated array of filenames. This memory
501  * is owned by GTK+ and must not be freed by the application.
502  * If you want to store this information, you should make a copy.
503  **/
504 gchar **
505 gtk_rc_get_default_files (void)
506 {
507   gtk_rc_add_initial_default_files ();
508
509   return gtk_rc_default_files;
510 }
511
512 static void
513 gtk_rc_settings_changed (GtkSettings  *settings,
514                          GParamSpec   *pspec,
515                          GtkRcContext *context)
516 {
517   gchar *new_theme_name;
518   gchar *new_key_theme_name;
519
520   g_object_get (settings,
521                 "gtk-theme-name", &new_theme_name,
522                 "gtk-key-theme-name", &new_key_theme_name,
523                 NULL);
524
525   if ((new_theme_name != context->theme_name && 
526        !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
527       (new_key_theme_name != context->key_theme_name &&
528        !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
529     {
530       gtk_rc_reparse_all_for_settings (settings, TRUE);
531     }
532
533   g_free (new_theme_name);
534   g_free (new_key_theme_name);
535 }
536
537 static void
538 gtk_rc_font_name_changed (GtkSettings  *settings,
539                           GParamSpec   *pspec,
540                           GtkRcContext *context)
541 {
542   _gtk_rc_context_get_default_font_name (settings);
543 }
544
545 static GtkRcContext *
546 gtk_rc_context_get (GtkSettings *settings)
547 {
548   if (!settings->rc_context)
549     {
550       GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
551
552       context->settings = settings;
553       context->rc_style_ht = NULL;
554       context->rc_sets_widget = NULL;
555       context->rc_sets_widget_class = NULL;
556       context->rc_sets_class = NULL;
557       context->rc_files = NULL;
558       context->default_style = NULL;
559
560       g_object_get (settings,
561                     "gtk-theme-name", &context->theme_name,
562                     "gtk-key-theme-name", &context->key_theme_name,
563                     "gtk-font-name", &context->font_name,
564                     NULL);
565
566       g_signal_connect (settings,
567                         "notify::gtk-theme-name",
568                         G_CALLBACK (gtk_rc_settings_changed),
569                         context);
570       g_signal_connect (settings,
571                         "notify::gtk-key-theme-name",
572                         G_CALLBACK (gtk_rc_settings_changed),
573                         context);
574       g_signal_connect (settings,
575                         "notify::gtk-font-name",
576                         G_CALLBACK (gtk_rc_font_name_changed),
577                         context);
578
579       
580       context->pixmap_path[0] = NULL;
581
582       context->default_priority = GTK_PATH_PRIO_RC;
583
584       rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
585     }
586
587   return settings->rc_context;
588 }
589
590 static void
591 gtk_rc_parse_named (GtkRcContext *context,
592                     const gchar  *name,
593                     const gchar  *type)
594 {
595   gchar *path = NULL;
596   const gchar *home_dir;
597   gchar *subpath;
598
599   if (type)
600     subpath = g_strconcat ("gtk-2.0-", type,
601                            G_DIR_SEPARATOR_S "gtkrc",
602                            NULL);
603   else
604     subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
605   
606   /* First look in the users home directory
607    */
608   home_dir = g_get_home_dir ();
609   if (home_dir)
610     {
611       path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
612       if (!g_file_test (path, G_FILE_TEST_EXISTS))
613         {
614           g_free (path);
615           path = NULL;
616         }
617     }
618
619   if (!path)
620     {
621       gchar *theme_dir = gtk_rc_get_theme_dir ();
622       path = g_build_filename (theme_dir, name, subpath, NULL);
623       g_free (theme_dir);
624       
625       if (!g_file_test (path, G_FILE_TEST_EXISTS))
626         {
627           g_free (path);
628           path = NULL;
629         }
630     }
631
632   if (path)
633     {
634       gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
635       g_free (path);
636     }
637
638   g_free (subpath);
639 }
640
641 static void
642 gtk_rc_parse_default_files (GtkRcContext *context)
643 {
644   gint i;
645
646   for (i = 0; gtk_rc_default_files[i] != NULL; i++)
647     gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
648 }
649
650 void
651 _gtk_rc_init (void)
652 {
653   static gboolean initialized = FALSE;
654
655   if (!initialized)
656     {
657       initialized = TRUE;
658       
659       gtk_rc_add_initial_default_files ();
660     }
661   
662   /* Default RC string */
663   gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
664                        "  bg[NORMAL] = \"#ffffc0\"\n"
665                        "  fg[NORMAL] = \"#000000\"\n"
666                        "}\n"
667                        "\n"
668                        "widget \"gtk-tooltips*\" style : gtk \"gtk-default-tooltips-style\"\n");
669 }
670   
671 static void
672 gtk_rc_context_parse_string (GtkRcContext *context,
673                              const gchar  *rc_string)
674 {
675   gtk_rc_parse_any (context, "-", -1, rc_string);
676 }
677
678 void
679 gtk_rc_parse_string (const gchar *rc_string)
680 {
681   GtkRcFile *rc_file;
682   GSList *tmp_list;
683       
684   g_return_if_fail (rc_string != NULL);
685
686   rc_file = g_new (GtkRcFile, 1);
687   rc_file->is_string = TRUE;
688   rc_file->name = g_strdup (rc_string);
689   rc_file->canonical_name = NULL;
690   rc_file->mtime = 0;
691   rc_file->reload = TRUE;
692
693   global_rc_files = g_slist_append (global_rc_files, rc_file);
694
695   for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
696     gtk_rc_context_parse_string (tmp_list->data, rc_string);
697 }
698
699 static GtkRcFile *
700 add_to_rc_file_list (GSList     **rc_file_list,
701                      const char  *filename,
702                      gboolean     reload)
703 {
704   GSList *tmp_list;
705   GtkRcFile *rc_file;
706   
707   tmp_list = *rc_file_list;
708   while (tmp_list)
709     {
710       rc_file = tmp_list->data;
711       if (!strcmp (rc_file->name, filename))
712         return rc_file;
713       
714       tmp_list = tmp_list->next;
715     }
716
717   rc_file = g_new (GtkRcFile, 1);
718   rc_file->is_string = FALSE;
719   rc_file->name = g_strdup (filename);
720   rc_file->canonical_name = NULL;
721   rc_file->mtime = 0;
722   rc_file->reload = reload;
723   
724   *rc_file_list = g_slist_append (*rc_file_list, rc_file);
725   
726   return rc_file;
727 }
728
729 static void
730 gtk_rc_context_parse_one_file (GtkRcContext *context,
731                                const gchar  *filename,
732                                gint          priority,
733                                gboolean      reload)
734 {
735   GtkRcFile *rc_file;
736   struct stat statbuf;
737   gint saved_priority;
738
739   g_return_if_fail (filename != NULL);
740
741   saved_priority = context->default_priority;
742   context->default_priority = priority;
743
744   rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
745   
746   if (!rc_file->canonical_name)
747     {
748       /* Get the absolute pathname */
749
750       if (g_path_is_absolute (rc_file->name))
751         rc_file->canonical_name = rc_file->name;
752       else
753         {
754           gchar *cwd;
755
756           cwd = g_get_current_dir ();
757           rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
758           g_free (cwd);
759         }
760     }
761
762   if (!lstat (rc_file->canonical_name, &statbuf))
763     {
764       gint fd;
765       GSList *tmp_list;
766
767       rc_file->mtime = statbuf.st_mtime;
768
769       fd = open (rc_file->canonical_name, O_RDONLY);
770       if (fd < 0)
771         goto out;
772
773       /* Temporarily push directory name for this file on
774        * a stack of directory names while parsing it
775        */
776       rc_dir_stack = 
777         g_slist_prepend (rc_dir_stack,
778                          g_path_get_dirname (rc_file->canonical_name));
779       gtk_rc_parse_any (context, filename, fd, NULL);
780  
781       tmp_list = rc_dir_stack;
782       rc_dir_stack = rc_dir_stack->next;
783  
784       g_free (tmp_list->data);
785       g_slist_free_1 (tmp_list);
786
787       close (fd);
788     }
789
790  out:
791   context->default_priority = saved_priority;
792 }
793
794 static gchar *
795 strchr_len (const gchar *str, gint len, char c)
796 {
797   while (len--)
798     {
799       if (*str == c)
800         return (gchar *)str;
801
802       str++;
803     }
804
805   return NULL;
806 }
807
808 static void
809 gtk_rc_context_parse_file (GtkRcContext *context,
810                            const gchar  *filename,
811                            gint          priority,
812                            gboolean      reload)
813 {
814   gchar *locale_suffixes[2];
815   gint n_locale_suffixes = 0;
816   gchar *p;
817   const gchar *locale;
818   gint length, j;
819   gboolean found = FALSE;
820
821 #ifdef G_OS_WIN32      
822   locale = g_win32_getlocale ();
823 #else      
824   locale = setlocale (LC_CTYPE, NULL);
825 #endif      
826
827   if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
828     {
829       /* Determine locale-specific suffixes for RC files.
830        */
831       length = strlen (locale);
832       
833       p = strchr (locale, '@');
834       if (p)
835         length = p - locale;
836
837       p = strchr_len (locale, length, '.');
838       if (p)
839         length = p - locale;
840       
841       locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
842       
843       p = strchr_len (locale, length, '_');
844       if (p)
845         {
846           length = p - locale;
847           locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
848         }
849     }
850   
851   gtk_rc_context_parse_one_file (context, filename, priority, reload);
852   for (j = 0; j < n_locale_suffixes; j++)
853     {
854       if (!found)
855         {
856           gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
857           if (g_file_test (name, G_FILE_TEST_EXISTS))
858             {
859               gtk_rc_context_parse_one_file (context, name, priority, FALSE);
860               found = TRUE;
861             }
862               
863           g_free (name);
864         }
865       
866       g_free (locale_suffixes[j]);
867     }
868 }
869
870 void
871 gtk_rc_parse (const gchar *filename)
872 {
873   GSList *tmp_list;
874   
875   g_return_if_fail (filename != NULL);
876
877   add_to_rc_file_list (&global_rc_files, filename, TRUE);
878   
879   for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
880     gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
881 }
882
883 /* Handling of RC styles */
884
885 GType
886 gtk_rc_style_get_type (void)
887 {
888   static GType object_type = 0;
889
890   if (!object_type)
891     {
892       static const GTypeInfo object_info =
893       {
894         sizeof (GtkRcStyleClass),
895         (GBaseInitFunc) NULL,
896         (GBaseFinalizeFunc) NULL,
897         (GClassInitFunc) gtk_rc_style_class_init,
898         NULL,           /* class_finalize */
899         NULL,           /* class_data */
900         sizeof (GtkRcStyle),
901         0,              /* n_preallocs */
902         (GInstanceInitFunc) gtk_rc_style_init,
903       };
904       
905       object_type = g_type_register_static (G_TYPE_OBJECT,
906                                             "GtkRcStyle",
907                                             &object_info, 0);
908     }
909   
910   return object_type;
911 }
912
913 static void
914 gtk_rc_style_init (GtkRcStyle *style)
915 {
916   guint i;
917
918   style->name = NULL;
919   style->font_desc = NULL;
920
921   for (i = 0; i < 5; i++)
922     {
923       static const GdkColor init_color = { 0, 0, 0, 0, };
924
925       style->bg_pixmap_name[i] = NULL;
926       style->color_flags[i] = 0;
927       style->fg[i] = init_color;
928       style->bg[i] = init_color;
929       style->text[i] = init_color;
930       style->base[i] = init_color;
931     }
932   style->xthickness = -1;
933   style->ythickness = -1;
934   style->rc_properties = NULL;
935
936   style->rc_style_lists = NULL;
937   style->icon_factories = NULL;
938 }
939
940 static void
941 gtk_rc_style_class_init (GtkRcStyleClass *klass)
942 {
943   GObjectClass *object_class = G_OBJECT_CLASS (klass);
944   
945   parent_class = g_type_class_peek_parent (klass);
946
947   object_class->finalize = gtk_rc_style_finalize;
948
949   klass->parse = NULL;
950   klass->create_rc_style = gtk_rc_style_real_create_rc_style;
951   klass->merge = gtk_rc_style_real_merge;
952   klass->create_style = gtk_rc_style_real_create_style;
953 }
954
955 static void
956 gtk_rc_style_finalize (GObject *object)
957 {
958   GSList *tmp_list1, *tmp_list2;
959   GtkRcStyle *rc_style;
960   gint i;
961
962   rc_style = GTK_RC_STYLE (object);
963   
964   if (rc_style->name)
965     g_free (rc_style->name);
966   if (rc_style->font_desc)
967     pango_font_description_free (rc_style->font_desc);
968       
969   for (i = 0; i < 5; i++)
970     if (rc_style->bg_pixmap_name[i])
971       g_free (rc_style->bg_pixmap_name[i]);
972   
973   /* Now remove all references to this rc_style from
974    * realized_style_ht
975    */
976   tmp_list1 = rc_style->rc_style_lists;
977   while (tmp_list1)
978     {
979       GSList *rc_styles = tmp_list1->data;
980       GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
981       gtk_style_unref (style);
982
983       /* Remove the list of styles from the other rc_styles
984        * in the list
985        */
986       tmp_list2 = rc_styles;
987       while (tmp_list2)
988         {
989           GtkRcStyle *other_style = tmp_list2->data;
990
991           if (other_style != rc_style)
992             other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
993                                                               rc_styles);
994           tmp_list2 = tmp_list2->next;
995         }
996
997       /* And from the hash table itself
998        */
999       g_hash_table_remove (realized_style_ht, rc_styles);
1000       g_slist_free (rc_styles);
1001
1002       tmp_list1 = tmp_list1->next;
1003     }
1004   g_slist_free (rc_style->rc_style_lists);
1005
1006   if (rc_style->rc_properties)
1007     {
1008       guint i;
1009
1010       for (i = 0; i < rc_style->rc_properties->len; i++)
1011         {
1012           GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1013
1014           g_free (node->origin);
1015           g_value_unset (&node->value);
1016         }
1017       g_array_free (rc_style->rc_properties, TRUE);
1018       rc_style->rc_properties = NULL;
1019     }
1020
1021   tmp_list1 = rc_style->icon_factories;
1022   while (tmp_list1)
1023     {
1024       g_object_unref (G_OBJECT (tmp_list1->data));
1025
1026       tmp_list1 = tmp_list1->next;
1027     }
1028   g_slist_free (rc_style->icon_factories);
1029   
1030   G_OBJECT_CLASS (parent_class)->finalize (object);
1031 }
1032
1033 GtkRcStyle *
1034 gtk_rc_style_new (void)
1035 {
1036   GtkRcStyle *style;
1037   
1038   style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1039   
1040   return style;
1041 }
1042
1043 /**
1044  * gtk_rc_style_copy:
1045  * @orig: the style to copy
1046  * 
1047  * Makes a copy of the specified #GtkRcStyle. This function
1048  * will correctly copy an RC style that is a member of a class
1049  * derived from #GtkRcStyle.
1050  * 
1051  * Return value: the resulting #GtkRcStyle
1052  **/
1053 GtkRcStyle *
1054 gtk_rc_style_copy (GtkRcStyle *orig)
1055 {
1056   GtkRcStyle *style;
1057
1058   g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1059   
1060   style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1061   GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1062
1063   return style;
1064 }
1065
1066 void      
1067 gtk_rc_style_ref (GtkRcStyle  *rc_style)
1068 {
1069   g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1070
1071   g_object_ref (G_OBJECT (rc_style));
1072 }
1073
1074 void      
1075 gtk_rc_style_unref (GtkRcStyle  *rc_style)
1076 {
1077   g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1078
1079   g_object_unref (G_OBJECT (rc_style));
1080 }
1081
1082 static GtkRcStyle *
1083 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1084 {
1085   return GTK_RC_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
1086 }
1087
1088 static gint
1089 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1090                        gconstpointer bsearch_node2)
1091 {
1092   const GtkRcProperty *prop1 = bsearch_node1;
1093   const GtkRcProperty *prop2 = bsearch_node2;
1094
1095   if (prop1->type_name == prop2->type_name)
1096     return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1097   else
1098     return prop1->type_name < prop2->type_name ? -1 : 1;
1099 }
1100
1101 static void
1102 insert_rc_property (GtkRcStyle    *style,
1103                     GtkRcProperty *property,
1104                     gboolean       replace)
1105 {
1106   guint i;
1107   GtkRcProperty *new_property = NULL;
1108   GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1109
1110   key.type_name = property->type_name;
1111   key.property_name = property->property_name;
1112
1113   if (!style->rc_properties)
1114     style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1115
1116   i = 0;
1117   while (i < style->rc_properties->len)
1118     {
1119       gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1120
1121       if (cmp == 0)
1122         {
1123           if (replace)
1124             {
1125               new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1126               
1127               g_free (new_property->origin);
1128               g_value_unset (&new_property->value);
1129               
1130               *new_property = key;
1131               break;
1132             }
1133           else
1134             return;
1135         }
1136       else if (cmp < 0)
1137         break;
1138
1139       i++;
1140     }
1141
1142   if (!new_property)
1143     {
1144       g_array_insert_val (style->rc_properties, i, key);
1145       new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1146     }
1147
1148   new_property->origin = g_strdup (property->origin);
1149   g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1150   g_value_copy (&property->value, &new_property->value);
1151 }
1152
1153 static void
1154 gtk_rc_style_real_merge (GtkRcStyle *dest,
1155                          GtkRcStyle *src)
1156 {
1157   gint i;
1158   
1159   for (i = 0; i < 5; i++)
1160     {
1161       if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1162         dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1163       
1164       if (!(dest->color_flags[i] & GTK_RC_FG) && 
1165           src->color_flags[i] & GTK_RC_FG)
1166         {
1167           dest->fg[i] = src->fg[i];
1168           dest->color_flags[i] |= GTK_RC_FG;
1169         }
1170       if (!(dest->color_flags[i] & GTK_RC_BG) && 
1171           src->color_flags[i] & GTK_RC_BG)
1172         {
1173           dest->bg[i] = src->bg[i];
1174           dest->color_flags[i] |= GTK_RC_BG;
1175         }
1176       if (!(dest->color_flags[i] & GTK_RC_TEXT) && 
1177           src->color_flags[i] & GTK_RC_TEXT)
1178         {
1179           dest->text[i] = src->text[i];
1180           dest->color_flags[i] |= GTK_RC_TEXT;
1181         }
1182       if (!(dest->color_flags[i] & GTK_RC_BASE) && 
1183           src->color_flags[i] & GTK_RC_BASE)
1184         {
1185           dest->base[i] = src->base[i];
1186           dest->color_flags[i] |= GTK_RC_BASE;
1187         }
1188     }
1189
1190   if (dest->xthickness < 0 && src->xthickness >= 0)
1191     dest->xthickness = src->xthickness;
1192   if (dest->ythickness < 0 && src->ythickness >= 0)
1193     dest->ythickness = src->ythickness;
1194
1195   if (src->font_desc)
1196     {
1197       if (!dest->font_desc)
1198         dest->font_desc = pango_font_description_copy (src->font_desc);
1199       else
1200         pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1201     }
1202
1203   if (src->rc_properties)
1204     {
1205       guint i;
1206
1207       for (i = 0; i < src->rc_properties->len; i++)
1208         insert_rc_property (dest,
1209                             &g_array_index (src->rc_properties, GtkRcProperty, i),
1210                             FALSE);
1211     }
1212 }
1213
1214 static GtkStyle *
1215 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1216 {
1217   return gtk_style_new ();
1218 }
1219
1220 static void
1221 gtk_rc_clear_hash_node (gpointer key, 
1222                         gpointer data, 
1223                         gpointer user_data)
1224 {
1225   gtk_rc_style_unref (data);
1226 }
1227
1228 static void
1229 gtk_rc_free_rc_sets (GSList *slist)
1230 {
1231   while (slist)
1232     {
1233       GtkRcSet *rc_set;
1234
1235       rc_set = slist->data;
1236       g_pattern_spec_free (rc_set->pspec);
1237       g_free (rc_set);
1238
1239       slist = slist->next;
1240     }
1241 }
1242
1243 static void
1244 gtk_rc_clear_styles (GtkRcContext *context)
1245 {
1246   /* Clear out all old rc_styles */
1247
1248   if (context->rc_style_ht)
1249     {
1250       g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1251       g_hash_table_destroy (context->rc_style_ht);
1252       context->rc_style_ht = NULL;
1253     }
1254
1255   gtk_rc_free_rc_sets (context->rc_sets_widget);
1256   g_slist_free (context->rc_sets_widget);
1257   context->rc_sets_widget = NULL;
1258
1259   gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1260   g_slist_free (context->rc_sets_widget_class);
1261   context->rc_sets_widget_class = NULL;
1262
1263   gtk_rc_free_rc_sets (context->rc_sets_class);
1264   g_slist_free (context->rc_sets_class);
1265   context->rc_sets_class = NULL;
1266 }
1267
1268 /* Reset all our widgets. Also, we have to invalidate cached icons in
1269  * icon sets so they get re-rendered.
1270  */
1271 static void
1272 gtk_rc_reset_widgets (GtkSettings *settings)
1273 {
1274   GList *list, *toplevels;
1275
1276   _gtk_icon_set_invalidate_caches ();
1277   
1278   toplevels = gtk_window_list_toplevels ();
1279   g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1280   
1281   for (list = toplevels; list; list = list->next)
1282     {
1283       if (gtk_widget_get_screen (list->data) == settings->screen)
1284         {
1285           gtk_widget_reset_rc_styles (list->data);
1286           gtk_widget_unref (list->data);
1287         }
1288     }
1289   g_list_free (toplevels);
1290 }
1291
1292 static void
1293 gtk_rc_clear_realized_style (gpointer key,
1294                              gpointer value,
1295                              gpointer data)
1296 {
1297   GSList *rc_styles = key;
1298   GSList *tmp_list = rc_styles;
1299
1300   while (tmp_list)
1301     {
1302       GtkRcStyle *rc_style = tmp_list->data;
1303       
1304       rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1305                                                      rc_styles);
1306       tmp_list = tmp_list->next;
1307     }
1308
1309   g_slist_free (rc_styles);
1310 }
1311
1312 /**
1313  * _gtk_rc_reset_styles:
1314  * @settings: a #GtkSettings
1315  * 
1316  * This setting resets all of our styles; we use it when the font
1317  * rendering parameters or the icon sizes have changed. It's both less
1318  * and more comprehensive then we actually need:
1319  *
1320  * Less comprehensive: it doesn't affect widgets that have a style
1321  *   set on them.
1322  *
1323  * More comprehensive: it resets the styles, but the styles haven't
1324  *   changed. The main reason for resetting the styles is becaues
1325  *   most widgets will redo all their font stuff when their style
1326  *   change.
1327  **/
1328 void
1329 _gtk_rc_reset_styles (GtkSettings *settings)
1330 {
1331   GtkRcContext *context;
1332   gboolean reset = FALSE;
1333
1334   g_return_if_fail (GTK_IS_SETTINGS (settings));
1335
1336   context = gtk_rc_context_get (settings);
1337   
1338   if (context->default_style)
1339     {
1340       g_object_unref (G_OBJECT (context->default_style));
1341       context->default_style = NULL;
1342       reset = TRUE;
1343     }
1344   
1345   /* Clear out styles that have been looked up already
1346    */
1347   if (realized_style_ht)
1348     {
1349       g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1350       g_hash_table_destroy (realized_style_ht);
1351       realized_style_ht = NULL;
1352       reset = TRUE;
1353     }
1354   
1355   if (reset)
1356     gtk_rc_reset_widgets (settings);
1357 }
1358
1359 const gchar*
1360 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1361 {
1362   GtkRcContext *context;
1363   gchar *new_font_name;
1364   
1365   g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1366
1367   context = gtk_rc_context_get (settings);
1368
1369   g_object_get (context->settings,
1370                 "gtk-font-name", &new_font_name,
1371                 NULL);
1372
1373   if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1374     {
1375        g_free (context->font_name);
1376        context->font_name = g_strdup (new_font_name);
1377  
1378        _gtk_rc_reset_styles (settings);
1379     }
1380           
1381   g_free (new_font_name);
1382
1383   return context->font_name;
1384 }
1385
1386 /**
1387  * gtk_rc_reparse_all_for_settings:
1388  * @settings: a #GtkSettings
1389  * @force_load: load whether or not anything changed
1390  * 
1391  * If the modification time on any previously read file
1392  * for the given #GtkSettings has changed, discard all style information
1393  * and then reread all previously read RC files.
1394  * 
1395  * Return value: %TRUE if the files were reread.
1396  **/
1397 gboolean
1398 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1399                                  gboolean     force_load)
1400 {
1401   gboolean mtime_modified = FALSE;
1402   GtkRcFile *rc_file;
1403   GSList *tmp_list;
1404   GtkRcContext *context;
1405
1406   struct stat statbuf;
1407
1408   g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1409
1410   context = gtk_rc_context_get (settings);
1411
1412   if (!force_load)
1413     {
1414       /* Check through and see if any of the RC's have had their
1415        * mtime modified. If so, reparse everything.
1416        */
1417       tmp_list = context->rc_files;
1418       while (tmp_list)
1419         {
1420           rc_file = tmp_list->data;
1421
1422           if (!rc_file->is_string)
1423             {
1424               if (!lstat (rc_file->name, &statbuf) && 
1425                   (statbuf.st_mtime > rc_file->mtime))
1426                 {
1427                   mtime_modified = TRUE;
1428                   break;
1429                 }
1430             }
1431           
1432           tmp_list = tmp_list->next;
1433         }
1434     }
1435       
1436   if (force_load || mtime_modified)
1437     {
1438       _gtk_binding_reset_parsed ();
1439       gtk_rc_clear_styles (context);
1440       g_object_freeze_notify (G_OBJECT (context->settings));
1441
1442       _gtk_settings_reset_rc_values (context->settings);
1443       tmp_list = context->rc_files;
1444       while (tmp_list)
1445         {
1446           rc_file = tmp_list->data;
1447
1448           if (rc_file->canonical_name != rc_file->name)
1449             g_free (rc_file->canonical_name);
1450           g_free (rc_file->name);
1451           g_free (rc_file);
1452
1453           tmp_list = tmp_list->next;
1454         }
1455
1456       g_slist_free (context->rc_files);
1457       context->rc_files = NULL;
1458
1459       gtk_rc_parse_default_files (context);
1460
1461       tmp_list = global_rc_files;
1462       while (tmp_list)
1463         {
1464           rc_file = tmp_list->data;
1465
1466           if (rc_file->is_string)
1467             gtk_rc_context_parse_string (context, rc_file->name);
1468           else
1469             gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1470
1471           tmp_list = tmp_list->next;
1472         }
1473
1474       g_free (context->theme_name);
1475       g_free (context->key_theme_name);
1476
1477       g_object_get (context->settings,
1478                     "gtk-theme-name", &context->theme_name,
1479                     "gtk-key-theme-name", &context->key_theme_name,
1480                     NULL);
1481
1482       if (context->theme_name && context->theme_name[0])
1483         gtk_rc_parse_named (context, context->theme_name, NULL);
1484       if (context->key_theme_name && context->key_theme_name[0])
1485         gtk_rc_parse_named (context, context->key_theme_name, "key");
1486       
1487       g_object_thaw_notify (G_OBJECT (context->settings));
1488
1489       gtk_rc_reset_widgets (context->settings);
1490     }
1491
1492   return force_load || mtime_modified;
1493 }
1494
1495 /**
1496  * gtk_rc_reparse_all:
1497  * 
1498  * If the modification time on any previously read file for the
1499  * default #GtkSettings has changed, discard all style information
1500  * and then reread all previously read RC files.
1501  * 
1502  * Return value:  %TRUE if the files were reread.
1503  **/
1504 gboolean
1505 gtk_rc_reparse_all (void)
1506 {
1507   GSList *tmp_list;
1508   gboolean result = FALSE;
1509
1510   for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1511     {
1512       GtkRcContext *context = tmp_list->data;
1513       if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1514         result = TRUE;
1515     }
1516
1517   return result;
1518 }
1519
1520 static GSList *
1521 gtk_rc_styles_match (GSList       *rc_styles,
1522                      GSList       *sets,
1523                      guint         path_length,
1524                      const gchar  *path,
1525                      const gchar  *path_reversed)
1526                      
1527 {
1528   GtkRcSet *rc_set;
1529
1530   while (sets)
1531     {
1532       rc_set = sets->data;
1533       sets = sets->next;
1534
1535       if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1536         rc_styles = g_slist_append (rc_styles, rc_set);
1537     }
1538   
1539   return rc_styles;
1540 }
1541
1542 static gint
1543 rc_set_compare (gconstpointer a, gconstpointer b)
1544 {
1545   const GtkRcSet *set_a = a;
1546   const GtkRcSet *set_b = b;
1547
1548   return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1549 }
1550
1551 static GSList *
1552 sort_and_dereference_sets (GSList *styles)
1553 {
1554   GSList *tmp_list;
1555   
1556   /* At this point, the list of sets is ordered by:
1557    *
1558    * a) 'widget' patterns are earlier than 'widget_class' patterns
1559    *    which are ealier than 'class' patterns.
1560    * a) For two matches for class patterns, a match to a child type
1561    *    is before a match to a parent type
1562    * c) a match later in the RC file (or in a later RC file) is before a
1563    *    match earlier in the RC file.
1564    *
1565    * With a) taking precedence over b) which takes precendence over c).
1566    *
1567    * Now sort by priority, which has the highest precendence for sort order
1568    */
1569   styles = g_slist_sort (styles, rc_set_compare);
1570
1571   /* Make styles->data = styles->data->rc_style
1572    */
1573   tmp_list = styles;
1574   while (tmp_list)
1575     {
1576       GtkRcSet *set = tmp_list->data;
1577       tmp_list->data = set->rc_style;
1578       tmp_list = tmp_list->next;
1579     }
1580
1581   return styles;
1582 }
1583
1584 /**
1585  * gtk_rc_get_style:
1586  * @widget: a #GtkWidget
1587  * 
1588  * Finds all matching RC styles for a given widget,
1589  * composites them together, and then creates a 
1590  * #GtkStyle representing the composite appearance.
1591  * (GTK+ actually keeps a cache of previously 
1592  * created styles, so a new style may not be
1593  * created.)
1594  * 
1595  * Returns: the resulting style. No refcount is added
1596  *   to the returned style, so if you want to save this
1597  *   style around, you should add a reference yourself.
1598  **/
1599 GtkStyle *
1600 gtk_rc_get_style (GtkWidget *widget)
1601 {
1602   GtkRcStyle *widget_rc_style;
1603   GSList *rc_styles = NULL;
1604   GtkRcContext *context;
1605
1606   static guint rc_style_key_id = 0;
1607
1608   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1609
1610   context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1611
1612   /* We allow the specification of a single rc style to be bound
1613    * tightly to a widget, for application modifications
1614    */
1615   if (!rc_style_key_id)
1616     rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1617
1618   if (context->rc_sets_widget)
1619     {
1620       gchar *path, *path_reversed;
1621       guint path_length;
1622
1623       gtk_widget_path (widget, &path_length, &path, &path_reversed);
1624       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1625       g_free (path);
1626       g_free (path_reversed);
1627     }
1628   
1629   if (context->rc_sets_widget_class)
1630     {
1631       gchar *path, *path_reversed;
1632       guint path_length;
1633
1634       gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1635       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1636       g_free (path);
1637       g_free (path_reversed);
1638     }
1639
1640   if (context->rc_sets_class)
1641     {
1642       GtkType type;
1643
1644       type = GTK_OBJECT_TYPE (widget);
1645       while (type)
1646         {
1647           const gchar *path;
1648           gchar *path_reversed;
1649           guint path_length;
1650
1651           path = gtk_type_name (type);
1652           path_length = strlen (path);
1653           path_reversed = g_strdup (path);
1654           g_strreverse (path_reversed);
1655           
1656           rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1657           g_free (path_reversed);
1658       
1659           type = gtk_type_parent (type);
1660         }
1661     }
1662   
1663   rc_styles = sort_and_dereference_sets (rc_styles);
1664   
1665   widget_rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
1666                                                rc_style_key_id);
1667
1668   if (widget_rc_style)
1669     rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1670
1671   if (rc_styles)
1672     return gtk_rc_init_style (context, rc_styles);
1673   else
1674     {
1675       if (!context->default_style)
1676         {
1677           context->default_style = gtk_style_new ();
1678           _gtk_style_init_for_settings (context->default_style, context->settings);
1679         }
1680
1681       return context->default_style;
1682     }
1683 }
1684
1685 /**
1686  * gtk_rc_get_style_by_paths:
1687  * @settings: a #GtkSettings object
1688  * @widget_path: the widget path to use when looking up the style, or %NULL
1689  *               if no matching against the widget path should be done
1690  * @class_path: the class path to use when looking up the style, or %NULL
1691  *               if no matching against the class path should be done.
1692  * @type: a type that will be used along with parent types of this type
1693  *        when matching against class styles, or #G_TYPE_NONE
1694  * 
1695  * Creates up a #GtkStyle from styles defined in a RC file by providing
1696  * the raw components used in matching. This function may be useful
1697  * when creating pseudo-widgets that should be themed like widgets but
1698  * don't actually have corresponding GTK+ widgets. An example of this
1699  * would be items inside a GNOME canvas widget.
1700  *
1701  * The action of gtk_rc_get_style() is similar to:
1702  * <informalexample><programlisting>
1703  *  gtk_widget_path (widget, NULL, &amp;path, NULL);
1704  *  gtk_widget_class_path (widget, NULL, &amp;class_path, NULL);
1705  *  gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
1706  *                             G_OBJECT_TYPE (widget));
1707  * </programlisting></informalexample>
1708  * 
1709  * Return value: A style created by matching with the supplied paths,
1710  *   or %NULL if nothing matching was specified and the default style should
1711  *   be used. The returned value is owned by GTK+ as part of an internal cache,
1712  *   so you must call g_object_ref() on the returned value if you want to
1713  *   keep a reference to it.
1714  **/
1715 GtkStyle *
1716 gtk_rc_get_style_by_paths (GtkSettings *settings,
1717                            const char  *widget_path,
1718                            const char  *class_path,
1719                            GType        type)
1720 {
1721   /* We duplicate the code from above to avoid slowing down the above
1722    * by generating paths when we don't need them. I don't know if
1723    * this is really worth it.
1724    */
1725   GSList *rc_styles = NULL;
1726   GtkRcContext *context;
1727
1728   g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1729
1730   context = gtk_rc_context_get (settings);
1731
1732   if (widget_path && context->rc_sets_widget)
1733     {
1734       gchar *path_reversed;
1735       guint path_length;
1736
1737       path_length = strlen (widget_path);
1738       path_reversed = g_strdup (widget_path);
1739       g_strreverse (path_reversed);
1740
1741       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, widget_path, path_reversed);
1742       g_free (path_reversed);
1743     }
1744   
1745   if (class_path && context->rc_sets_widget_class)
1746     {
1747       gchar *path_reversed;
1748       guint path_length;
1749
1750       path_length = strlen (class_path);
1751       path_reversed = g_strdup (class_path);
1752       g_strreverse (path_reversed);
1753
1754       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, class_path, path_reversed);
1755       g_free (path_reversed);
1756     }
1757
1758   if (type != G_TYPE_NONE && context->rc_sets_class)
1759     {
1760       while (type)
1761         {
1762           const gchar *path;
1763           gchar *path_reversed;
1764           guint path_length;
1765
1766           path = g_type_name (type);
1767           path_length = strlen (path);
1768           path_reversed = g_strdup (path);
1769           g_strreverse (path_reversed);
1770           
1771           rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1772           g_free (path_reversed);
1773       
1774           type = g_type_parent (type);
1775         }
1776     }
1777  
1778   rc_styles = sort_and_dereference_sets (rc_styles);
1779   
1780   if (rc_styles)
1781     return gtk_rc_init_style (context, rc_styles);
1782
1783   return NULL;
1784 }
1785
1786 static GSList *
1787 gtk_rc_add_rc_sets (GSList      *slist,
1788                     GtkRcStyle  *rc_style,
1789                     const gchar *pattern)
1790 {
1791   GtkRcStyle *new_style;
1792   GtkRcSet *rc_set;
1793   guint i;
1794   
1795   new_style = gtk_rc_style_new ();
1796   *new_style = *rc_style;
1797   new_style->name = g_strdup (rc_style->name);
1798   if (rc_style->font_desc)
1799     new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
1800   
1801   for (i = 0; i < 5; i++)
1802     new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1803   
1804   rc_set = g_new (GtkRcSet, 1);
1805   rc_set->pspec = g_pattern_spec_new (pattern);
1806   rc_set->rc_style = rc_style;
1807   
1808   return g_slist_prepend (slist, rc_set);
1809 }
1810
1811 void
1812 gtk_rc_add_widget_name_style (GtkRcStyle  *rc_style,
1813                               const gchar *pattern)
1814 {
1815   GtkRcContext *context;
1816   
1817   g_return_if_fail (rc_style != NULL);
1818   g_return_if_fail (pattern != NULL);
1819
1820   context = gtk_rc_context_get (gtk_settings_get_default ());
1821   
1822   context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern);
1823 }
1824
1825 void
1826 gtk_rc_add_widget_class_style (GtkRcStyle  *rc_style,
1827                                const gchar *pattern)
1828 {
1829   GtkRcContext *context;
1830   
1831   g_return_if_fail (rc_style != NULL);
1832   g_return_if_fail (pattern != NULL);
1833
1834   context = gtk_rc_context_get (gtk_settings_get_default ());
1835   
1836   context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern);
1837 }
1838
1839 void
1840 gtk_rc_add_class_style (GtkRcStyle  *rc_style,
1841                         const gchar *pattern)
1842 {
1843   GtkRcContext *context;
1844   
1845   g_return_if_fail (rc_style != NULL);
1846   g_return_if_fail (pattern != NULL);
1847
1848   context = gtk_rc_context_get (gtk_settings_get_default ());
1849   
1850   context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern);
1851 }
1852
1853 GScanner*
1854 gtk_rc_scanner_new (void)
1855 {
1856   return g_scanner_new (&gtk_rc_scanner_config);
1857 }
1858
1859 static void
1860 gtk_rc_parse_any (GtkRcContext *context,
1861                   const gchar  *input_name,
1862                   gint          input_fd,
1863                   const gchar  *input_string)
1864 {
1865   GScanner *scanner;
1866   guint    i;
1867   gboolean done;
1868
1869   scanner = gtk_rc_scanner_new ();
1870   
1871   if (input_fd >= 0)
1872     {
1873       g_assert (input_string == NULL);
1874       
1875       g_scanner_input_file (scanner, input_fd);
1876     }
1877   else
1878     {
1879       g_assert (input_string != NULL);
1880       
1881       g_scanner_input_text (scanner, input_string, strlen (input_string));
1882     }
1883   scanner->input_name = input_name;
1884
1885   for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1886     g_scanner_scope_add_symbol (scanner, 0, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1887   
1888   done = FALSE;
1889   while (!done)
1890     {
1891       if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1892         done = TRUE;
1893       else
1894         {
1895           guint expected_token;
1896           
1897           expected_token = gtk_rc_parse_statement (context, scanner);
1898
1899           if (expected_token != G_TOKEN_NONE)
1900             {
1901               gchar *symbol_name;
1902               gchar *msg;
1903               
1904               msg = NULL;
1905               symbol_name = NULL;
1906               if (scanner->scope_id == 0)
1907                 {
1908                   /* if we are in scope 0, we know the symbol names
1909                    * that are associated with certain token values.
1910                    * so we look them up to make the error messages
1911                    * more readable.
1912                    */
1913                   if (expected_token > GTK_RC_TOKEN_INVALID &&
1914                       expected_token < GTK_RC_TOKEN_LAST)
1915                     {
1916                       for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1917                         if (symbols[i].token == expected_token)
1918                           msg = symbols[i].name;
1919                       if (msg)
1920                         msg = g_strconcat ("e.g. `", msg, "'", NULL);
1921                     }
1922                   if (scanner->token > GTK_RC_TOKEN_INVALID &&
1923                       scanner->token < GTK_RC_TOKEN_LAST)
1924                     {
1925                       symbol_name = "???";
1926                       for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1927                         if (symbols[i].token == scanner->token)
1928                           symbol_name = symbols[i].name;
1929                     }
1930                 }
1931               g_scanner_unexp_token (scanner,
1932                                      expected_token,
1933                                      NULL,
1934                                      "keyword",
1935                                      symbol_name,
1936                                      msg,
1937                                      TRUE);
1938               g_free (msg);
1939               done = TRUE;
1940             }
1941         }
1942     }
1943   
1944   g_scanner_destroy (scanner);
1945 }
1946
1947 static guint       
1948 gtk_rc_styles_hash (const GSList *rc_styles)
1949 {
1950   guint result;
1951   
1952   result = 0;
1953   while (rc_styles)
1954     {
1955       result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
1956       rc_styles = rc_styles->next;
1957     }
1958   
1959   return result;
1960 }
1961
1962 static gboolean
1963 gtk_rc_styles_equal (const GSList *a,
1964                      const GSList *b)
1965 {
1966   while (a && b)
1967     {
1968       if (a->data != b->data)
1969         return FALSE;
1970       a = a->next;
1971       b = b->next;
1972     }
1973   
1974   return (a == b);
1975 }
1976
1977 static guint
1978 gtk_rc_style_hash (const gchar *name)
1979 {
1980   guint result;
1981   
1982   result = 0;
1983   while (*name)
1984     result += (result << 3) + *name++;
1985   
1986   return result;
1987 }
1988
1989 static gboolean
1990 gtk_rc_style_equal (const gchar *a,
1991                     const gchar *b)
1992 {
1993   return (strcmp (a, b) == 0);
1994 }
1995
1996 static GtkRcStyle*
1997 gtk_rc_style_find (GtkRcContext *context,
1998                    const gchar  *name)
1999 {
2000   if (context->rc_style_ht)
2001     return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2002   else
2003     return NULL;
2004 }
2005
2006 static GtkStyle *
2007 gtk_rc_style_to_style (GtkRcContext *context,
2008                        GtkRcStyle   *rc_style)
2009 {
2010   GtkStyle *style;
2011
2012   style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2013   _gtk_style_init_for_settings (style, context->settings);
2014
2015   style->rc_style = rc_style;
2016
2017   gtk_rc_style_ref (rc_style);
2018   
2019   GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);  
2020
2021   return style;
2022 }
2023
2024 /* Reuses or frees rc_styles */
2025 static GtkStyle *
2026 gtk_rc_init_style (GtkRcContext *context,
2027                    GSList       *rc_styles)
2028 {
2029   GtkStyle *style = NULL;
2030   gint i;
2031
2032   g_return_val_if_fail (rc_styles != NULL, NULL);
2033   
2034   if (!realized_style_ht)
2035     realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2036  (GEqualFunc) gtk_rc_styles_equal);
2037
2038   style = g_hash_table_lookup (realized_style_ht, rc_styles);
2039
2040   if (!style)
2041     {
2042       GtkRcStyle *base_style = NULL;
2043       GtkRcStyle *proto_style;
2044       GtkRcStyleClass *proto_style_class;
2045       GSList *tmp_styles;
2046       GType rc_style_type = GTK_TYPE_RC_STYLE;
2047
2048       /* Find the the first style where the RC file specified engine "" {}
2049        * or the first derived style and use that to create the
2050        * merged style. If we only have raw GtkRcStyles, use the first
2051        * style to create the merged style.
2052        */
2053       base_style = rc_styles->data;
2054       tmp_styles = rc_styles;
2055       while (tmp_styles)
2056         {
2057           GtkRcStyle *rc_style = tmp_styles->data;
2058           
2059           if (rc_style->engine_specified ||
2060               G_OBJECT_TYPE (rc_style) != rc_style_type)
2061             {
2062               base_style = rc_style;
2063               break;
2064             }
2065           
2066           tmp_styles = tmp_styles->next;
2067         }
2068       
2069       proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2070       proto_style = proto_style_class->create_rc_style (base_style);
2071       
2072       tmp_styles = rc_styles;
2073       while (tmp_styles)
2074         {
2075           GtkRcStyle *rc_style = tmp_styles->data;
2076           GSList *factories;
2077           
2078           proto_style_class->merge (proto_style, rc_style);       
2079           
2080           /* Point from each rc_style to the list of styles */
2081           if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2082             rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2083
2084           factories = g_slist_copy (rc_style->icon_factories);
2085           if (factories)
2086             {
2087               GSList *iter;
2088               
2089               iter = factories;
2090               while (iter != NULL)
2091                 {
2092                   g_object_ref (G_OBJECT (iter->data));
2093                   iter = g_slist_next (iter);
2094                 }
2095
2096               proto_style->icon_factories = g_slist_concat (proto_style->icon_factories,
2097                                                             factories);
2098
2099             }
2100           
2101           tmp_styles = tmp_styles->next;
2102         }
2103
2104       for (i = 0; i < 5; i++)
2105         if (proto_style->bg_pixmap_name[i] &&
2106             (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2107           {
2108             g_free (proto_style->bg_pixmap_name[i]);
2109             proto_style->bg_pixmap_name[i] = NULL;
2110           }
2111
2112       style = gtk_rc_style_to_style (context, proto_style);
2113       gtk_rc_style_unref (proto_style);
2114
2115       g_hash_table_insert (realized_style_ht, rc_styles, style);
2116     }
2117   else
2118     g_slist_free (rc_styles);
2119
2120   return style;
2121 }
2122
2123 /*********************
2124  * Parsing functions *
2125  *********************/
2126
2127 static guint
2128 rc_parse_token_or_compound (GScanner  *scanner,
2129                             GString   *gstring,
2130                             GTokenType delimiter)
2131 {
2132   guint token = g_scanner_get_next_token (scanner);
2133
2134   /* we either scan a single token (skipping comments)
2135    * or a compund statement.
2136    * compunds are enclosed in (), [] or {} braces, we read
2137    * them in via deep recursion.
2138    */
2139
2140   switch (token)
2141     {
2142       gchar *string;
2143     case G_TOKEN_INT:
2144       g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2145       break;
2146     case G_TOKEN_FLOAT:
2147       g_string_append_printf (gstring, " %f", scanner->value.v_float);
2148       break;
2149     case G_TOKEN_STRING:
2150       string = g_strescape (scanner->value.v_string, NULL);
2151       g_string_append (gstring, " \"");
2152       g_string_append (gstring, string);
2153       g_string_append_c (gstring, '"');
2154       g_free (string);
2155       break;
2156     case G_TOKEN_IDENTIFIER:
2157       g_string_append_c (gstring, ' ');
2158       g_string_append (gstring, scanner->value.v_identifier);
2159       break;
2160     case G_TOKEN_COMMENT_SINGLE:
2161     case G_TOKEN_COMMENT_MULTI:
2162       return rc_parse_token_or_compound (scanner, gstring, delimiter);
2163     case G_TOKEN_LEFT_PAREN:
2164       g_string_append_c (gstring, ' ');
2165       g_string_append_c (gstring, token);
2166       token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_PAREN);
2167       if (token != G_TOKEN_NONE)
2168         return token;
2169       break;
2170     case G_TOKEN_LEFT_CURLY:
2171       g_string_append_c (gstring, ' ');
2172       g_string_append_c (gstring, token);
2173       token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_CURLY);
2174       if (token != G_TOKEN_NONE)
2175         return token;
2176       break;
2177     case G_TOKEN_LEFT_BRACE:
2178       g_string_append_c (gstring, ' ');
2179       g_string_append_c (gstring, token);
2180       token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_BRACE);
2181       if (token != G_TOKEN_NONE)
2182         return token;
2183       break;
2184     default:
2185       if (token >= 256 || token < 1)
2186         return delimiter ? delimiter : G_TOKEN_STRING;
2187       g_string_append_c (gstring, ' ');
2188       g_string_append_c (gstring, token);
2189       if (token == delimiter)
2190         return G_TOKEN_NONE;
2191       break;
2192     }
2193   if (!delimiter)
2194     return G_TOKEN_NONE;
2195   else
2196     return rc_parse_token_or_compound (scanner, gstring, delimiter);
2197 }
2198
2199 static guint
2200 gtk_rc_parse_assignment (GScanner      *scanner,
2201                          GtkRcProperty *prop)
2202 {
2203   gboolean scan_identifier = scanner->config->scan_identifier;
2204   gboolean scan_symbols = scanner->config->scan_symbols;
2205   gboolean identifier_2_string = scanner->config->identifier_2_string;
2206   gboolean char_2_token = scanner->config->char_2_token;
2207   gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2208   gboolean numbers_2_int = scanner->config->numbers_2_int;
2209   gboolean negate = FALSE;
2210   guint token;
2211
2212   /* check that this is an assignment */
2213   if (g_scanner_get_next_token (scanner) != '=')
2214     return '=';
2215
2216   /* adjust scanner mode */
2217   scanner->config->scan_identifier = TRUE;
2218   scanner->config->scan_symbols = FALSE;
2219   scanner->config->identifier_2_string = FALSE;
2220   scanner->config->char_2_token = TRUE;
2221   scanner->config->scan_identifier_NULL = FALSE;
2222   scanner->config->numbers_2_int = TRUE;
2223
2224   /* record location */
2225   prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2226
2227   /* parse optional sign */
2228   if (g_scanner_peek_next_token (scanner) == '-')
2229     {
2230       g_scanner_get_next_token (scanner); /* eat sign */
2231       negate = TRUE;
2232     }
2233
2234   /* parse one of LONG, DOUBLE and STRING or, if that fails, create an unparsed compund */
2235   token = g_scanner_peek_next_token (scanner);
2236   switch (token)
2237     {
2238     case G_TOKEN_INT:
2239       g_scanner_get_next_token (scanner);
2240       g_value_init (&prop->value, G_TYPE_LONG);
2241       g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2242       token = G_TOKEN_NONE;
2243       break;
2244     case G_TOKEN_FLOAT:
2245       g_scanner_get_next_token (scanner);
2246       g_value_init (&prop->value, G_TYPE_DOUBLE);
2247       g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2248       token = G_TOKEN_NONE;
2249       break;
2250     case G_TOKEN_STRING:
2251       g_scanner_get_next_token (scanner);
2252       if (negate)
2253         token = G_TOKEN_INT;
2254       else
2255         {
2256           g_value_init (&prop->value, G_TYPE_STRING);
2257           g_value_set_string (&prop->value, scanner->value.v_string);
2258           token = G_TOKEN_NONE;
2259         }
2260       break;
2261     case G_TOKEN_IDENTIFIER:
2262     case G_TOKEN_LEFT_PAREN:
2263     case G_TOKEN_LEFT_CURLY:
2264     case G_TOKEN_LEFT_BRACE:
2265       if (!negate)
2266         {
2267           GString *gstring = g_string_new ("");
2268
2269           token = rc_parse_token_or_compound (scanner, gstring, 0);
2270           if (token == G_TOKEN_NONE)
2271             {
2272               g_string_append_c (gstring, ' ');
2273               g_value_init (&prop->value, G_TYPE_GSTRING);
2274               g_value_set_static_boxed (&prop->value, gstring);
2275             }
2276           else
2277             g_string_free (gstring, TRUE);
2278           break;
2279         }
2280       /* fall through */
2281     default:
2282       g_scanner_get_next_token (scanner);
2283       token = G_TOKEN_INT;
2284       break;
2285     }
2286
2287   /* restore scanner mode */
2288   scanner->config->scan_identifier = scan_identifier;
2289   scanner->config->scan_symbols = scan_symbols;
2290   scanner->config->identifier_2_string = identifier_2_string;
2291   scanner->config->char_2_token = char_2_token;
2292   scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2293   scanner->config->numbers_2_int = numbers_2_int;
2294
2295   return token;
2296 }
2297
2298 static gboolean
2299 is_c_identifier (const gchar *string)
2300 {
2301   const gchar *p;
2302   gboolean is_varname;
2303
2304   is_varname = strchr (G_CSET_a_2_z G_CSET_A_2_Z "_", string[0]) != NULL;
2305   for (p = string + 1; *p && is_varname; p++)
2306     is_varname &= strchr (G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_-", *p) != NULL;
2307
2308   return is_varname;
2309 }
2310
2311 static void
2312 parse_include_file (GtkRcContext *context,
2313                     GScanner     *scanner,
2314                     const gchar  *filename)
2315 {
2316   char *to_parse = NULL;
2317   
2318   if (g_path_is_absolute (filename))
2319     {
2320       /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2321        * don't print an error in this case.
2322        */
2323       to_parse = g_strdup (filename);
2324     }
2325   else
2326     {
2327       /* if a relative path, we look relative to all the RC files in the
2328        * include stack. We require the file to be found in this case
2329        * so we can give meaningful error messages, and because on reparsing
2330        * non-absolute paths don't make sense.
2331        */
2332       GSList *tmp_list = rc_dir_stack;
2333       while (tmp_list)
2334         {
2335           gchar *tmpname = g_build_filename (tmp_list->data, filename, NULL);
2336
2337           if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2338             {
2339               to_parse = tmpname;
2340               break;
2341             }
2342
2343           g_free (tmpname);
2344           
2345           tmp_list = tmp_list->next;
2346         }
2347     }
2348
2349   if (to_parse)
2350     {
2351       gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2352       g_free (to_parse);
2353     }
2354   else
2355     {
2356       g_scanner_warn (scanner, 
2357                       _("Unable to find include file: \"%s\""),
2358                       filename);
2359     }
2360
2361 }
2362
2363 static guint
2364 gtk_rc_parse_statement (GtkRcContext *context,
2365                         GScanner     *scanner)
2366 {
2367   guint token;
2368   
2369   token = g_scanner_peek_next_token (scanner);
2370   switch (token)
2371     {
2372     case GTK_RC_TOKEN_INCLUDE:
2373       token = g_scanner_get_next_token (scanner);
2374       if (token != GTK_RC_TOKEN_INCLUDE)
2375         return GTK_RC_TOKEN_INCLUDE;
2376       token = g_scanner_get_next_token (scanner);
2377       if (token != G_TOKEN_STRING)
2378         return G_TOKEN_STRING;
2379       parse_include_file (context, scanner, scanner->value.v_string);
2380       return G_TOKEN_NONE;
2381       
2382     case GTK_RC_TOKEN_STYLE:
2383       return gtk_rc_parse_style (context, scanner);
2384       
2385     case GTK_RC_TOKEN_BINDING:
2386       return gtk_binding_parse_binding (scanner);
2387       
2388     case GTK_RC_TOKEN_PIXMAP_PATH:
2389       return gtk_rc_parse_pixmap_path (context, scanner);
2390       
2391     case GTK_RC_TOKEN_WIDGET:
2392       return gtk_rc_parse_path_pattern (context, scanner);
2393       
2394     case GTK_RC_TOKEN_WIDGET_CLASS:
2395       return gtk_rc_parse_path_pattern (context, scanner);
2396       
2397     case GTK_RC_TOKEN_CLASS:
2398       return gtk_rc_parse_path_pattern (context, scanner);
2399       
2400     case GTK_RC_TOKEN_MODULE_PATH:
2401       return gtk_rc_parse_module_path (scanner);
2402       
2403     case GTK_RC_TOKEN_IM_MODULE_FILE:
2404       return gtk_rc_parse_im_module_file (scanner);
2405
2406     case G_TOKEN_IDENTIFIER:
2407       if (is_c_identifier (scanner->next_value.v_identifier))
2408         {
2409           GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2410           gchar *name;
2411           
2412           g_scanner_get_next_token (scanner); /* eat identifier */
2413           name = g_strdup (scanner->value.v_identifier);
2414           
2415           token = gtk_rc_parse_assignment (scanner, &prop);
2416           if (token == G_TOKEN_NONE)
2417             {
2418               GtkSettingsValue svalue;
2419
2420               svalue.origin = prop.origin;
2421               memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2422               g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2423               _gtk_settings_set_property_value_from_rc (context->settings,
2424                                                         name,
2425                                                         &svalue);
2426             }
2427           g_free (prop.origin);
2428           if (G_VALUE_TYPE (&prop.value))
2429             g_value_unset (&prop.value);
2430           g_free (name);
2431           
2432           return token;
2433         }
2434       else
2435         {
2436           g_scanner_get_next_token (scanner);
2437           return G_TOKEN_IDENTIFIER;
2438         }
2439     default:
2440       g_scanner_get_next_token (scanner);
2441       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2442     }
2443 }
2444
2445 static void
2446 fixup_rc_set (GSList     *list,
2447               GtkRcStyle *orig,
2448               GtkRcStyle *new)
2449 {
2450   while (list)
2451     {
2452       GtkRcSet *set = list->data;
2453       if (set->rc_style == orig)
2454         set->rc_style = new;
2455       list = list->next;
2456     }
2457 }
2458
2459 static void
2460 fixup_rc_sets (GtkRcContext *context,
2461                GtkRcStyle   *orig,
2462                GtkRcStyle   *new)
2463 {
2464   fixup_rc_set (context->rc_sets_widget, orig, new);
2465   fixup_rc_set (context->rc_sets_widget_class, orig, new);
2466   fixup_rc_set (context->rc_sets_class, orig, new);
2467 }
2468
2469 static guint
2470 gtk_rc_parse_style (GtkRcContext *context,
2471                     GScanner     *scanner)
2472 {
2473   GtkRcStyle *rc_style;
2474   GtkRcStyle *orig_style;
2475   GtkRcStyle *parent_style;
2476   guint token;
2477   gint i;
2478   GtkIconFactory *our_factory = NULL;
2479   
2480   token = g_scanner_get_next_token (scanner);
2481   if (token != GTK_RC_TOKEN_STYLE)
2482     return GTK_RC_TOKEN_STYLE;
2483   
2484   token = g_scanner_get_next_token (scanner);
2485   if (token != G_TOKEN_STRING)
2486     return G_TOKEN_STRING;
2487   
2488   rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2489   if (rc_style)
2490     orig_style = g_object_ref (rc_style);
2491   else
2492     orig_style = NULL;
2493
2494   /* If there's a list, its first member is always the factory belonging
2495    * to this RcStyle
2496    */
2497   if (rc_style && rc_style->icon_factories)
2498     our_factory = rc_style->icon_factories->data;
2499   
2500   if (!rc_style)
2501     {
2502       rc_style = gtk_rc_style_new ();
2503       rc_style->name = g_strdup (scanner->value.v_string);
2504       
2505       for (i = 0; i < 5; i++)
2506         rc_style->bg_pixmap_name[i] = NULL;
2507
2508       for (i = 0; i < 5; i++)
2509         rc_style->color_flags[i] = 0;
2510     }
2511
2512   token = g_scanner_peek_next_token (scanner);
2513   if (token == G_TOKEN_EQUAL_SIGN)
2514     {
2515       token = g_scanner_get_next_token (scanner);
2516       
2517       token = g_scanner_get_next_token (scanner);
2518       if (token != G_TOKEN_STRING)
2519         {
2520           token = G_TOKEN_STRING;
2521           goto err;
2522         }
2523       
2524       parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2525       if (parent_style)
2526         {
2527           GSList *factories;
2528           
2529           for (i = 0; i < 5; i++)
2530             {
2531               rc_style->color_flags[i] = parent_style->color_flags[i];
2532               rc_style->fg[i] = parent_style->fg[i];
2533               rc_style->bg[i] = parent_style->bg[i];
2534               rc_style->text[i] = parent_style->text[i];
2535               rc_style->base[i] = parent_style->base[i];
2536             }
2537
2538           rc_style->xthickness = parent_style->xthickness;
2539           rc_style->ythickness = parent_style->ythickness;
2540           
2541           if (parent_style->font_desc)
2542             {
2543               if (rc_style->font_desc)
2544                 pango_font_description_free (rc_style->font_desc);
2545               rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
2546             }
2547
2548           if (parent_style->rc_properties)
2549             {
2550               guint i;
2551
2552               for (i = 0; i < parent_style->rc_properties->len; i++)
2553                 insert_rc_property (rc_style,
2554                                     &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
2555                                     TRUE);
2556             }
2557           
2558           for (i = 0; i < 5; i++)
2559             {
2560               if (rc_style->bg_pixmap_name[i])
2561                 g_free (rc_style->bg_pixmap_name[i]);
2562               rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
2563             }
2564           
2565           /* Append parent's factories, adding a ref to them */
2566           if (parent_style->icon_factories != NULL)
2567             {
2568               /* Add a factory for ourselves if we have none,
2569                * in case we end up defining more stock icons.
2570                * I see no real way around this; we need to maintain
2571                * the invariant that the first factory in the list
2572                * is always our_factory, the one belonging to us,
2573                * and if we put parent factories in the list we can't
2574                * do that if the style is reopened.
2575                */
2576               if (our_factory == NULL)
2577                 {
2578                   our_factory = gtk_icon_factory_new ();
2579                   rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2580                                                               our_factory);
2581                 }
2582               
2583               rc_style->icon_factories = g_slist_concat (rc_style->icon_factories,
2584                                                          g_slist_copy (parent_style->icon_factories));
2585               
2586               factories = parent_style->icon_factories;
2587               while (factories != NULL)
2588                 {
2589                   g_object_ref (G_OBJECT (factories->data));
2590                   factories = factories->next;
2591                 }
2592             }
2593         }
2594     }
2595   
2596   token = g_scanner_get_next_token (scanner);
2597   if (token != G_TOKEN_LEFT_CURLY)
2598     {
2599       token = G_TOKEN_LEFT_CURLY;
2600       goto err;
2601     }
2602   
2603   token = g_scanner_peek_next_token (scanner);
2604   while (token != G_TOKEN_RIGHT_CURLY)
2605     {
2606       switch (token)
2607         {
2608         case GTK_RC_TOKEN_BG:
2609           token = gtk_rc_parse_bg (scanner, rc_style);
2610           break;
2611         case GTK_RC_TOKEN_FG:
2612           token = gtk_rc_parse_fg (scanner, rc_style);
2613           break;
2614         case GTK_RC_TOKEN_TEXT:
2615           token = gtk_rc_parse_text (scanner, rc_style);
2616           break;
2617         case GTK_RC_TOKEN_BASE:
2618           token = gtk_rc_parse_base (scanner, rc_style);
2619           break;
2620         case GTK_RC_TOKEN_XTHICKNESS:
2621           token = gtk_rc_parse_xthickness (scanner, rc_style);
2622           break;
2623         case GTK_RC_TOKEN_YTHICKNESS:
2624           token = gtk_rc_parse_ythickness (scanner, rc_style);
2625           break;
2626         case GTK_RC_TOKEN_BG_PIXMAP:
2627           token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
2628           break;
2629         case GTK_RC_TOKEN_FONT:
2630           token = gtk_rc_parse_font (scanner, rc_style);
2631           break;
2632         case GTK_RC_TOKEN_FONTSET:
2633           token = gtk_rc_parse_fontset (scanner, rc_style);
2634           break;
2635         case GTK_RC_TOKEN_FONT_NAME:
2636           token = gtk_rc_parse_font_name (scanner, rc_style);
2637           break;
2638         case GTK_RC_TOKEN_ENGINE:
2639           token = gtk_rc_parse_engine (context, scanner, &rc_style);
2640           break;
2641         case GTK_RC_TOKEN_STOCK:
2642           if (our_factory == NULL)
2643             {
2644               our_factory = gtk_icon_factory_new ();
2645               rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2646                                                           our_factory);
2647             }
2648           token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
2649           break;
2650         case G_TOKEN_IDENTIFIER:
2651           if (is_c_identifier (scanner->next_value.v_identifier) &&
2652               scanner->next_value.v_identifier[0] >= 'A' &&
2653               scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
2654             {
2655               GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2656               
2657               g_scanner_get_next_token (scanner); /* eat type name */
2658               prop.type_name = g_quark_from_string (scanner->value.v_identifier);
2659               if (g_scanner_get_next_token (scanner) != ':' ||
2660                   g_scanner_get_next_token (scanner) != ':')
2661                 {
2662                   token = ':';
2663                   break;
2664                 }
2665               if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
2666                   !is_c_identifier (scanner->value.v_identifier))
2667                 {
2668                   token = G_TOKEN_IDENTIFIER;
2669                   break;
2670                 }
2671
2672               /* it's important that we do the same canonification as GParamSpecPool here */
2673               g_strcanon (scanner->value.v_identifier, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2674               prop.property_name = g_quark_from_string (scanner->value.v_identifier);
2675
2676               token = gtk_rc_parse_assignment (scanner, &prop);
2677               if (token == G_TOKEN_NONE)
2678                 {
2679                   g_return_val_if_fail (prop.origin != NULL && G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
2680                   insert_rc_property (rc_style, &prop, TRUE);
2681                 }
2682               
2683               g_free (prop.origin);
2684               if (G_VALUE_TYPE (&prop.value))
2685                 g_value_unset (&prop.value);
2686             }
2687           else
2688             {
2689               g_scanner_get_next_token (scanner);
2690               token = G_TOKEN_IDENTIFIER;
2691             }
2692           break;
2693         default:
2694           g_scanner_get_next_token (scanner);
2695           token = G_TOKEN_RIGHT_CURLY;
2696           break;
2697         }
2698
2699       if (token != G_TOKEN_NONE)
2700         goto err;
2701
2702       token = g_scanner_peek_next_token (scanner);
2703     } /* while (token != G_TOKEN_RIGHT_CURLY) */
2704   
2705   token = g_scanner_get_next_token (scanner);
2706   if (token != G_TOKEN_RIGHT_CURLY)
2707     {
2708       token = G_TOKEN_RIGHT_CURLY;
2709       goto err;
2710     }
2711   
2712   if (rc_style != orig_style)
2713     {
2714       if (!context->rc_style_ht)
2715         context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
2716                                                  (GEqualFunc) gtk_rc_style_equal);
2717       
2718       g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
2719
2720       /* If we copied the data into a new rc style, fix up references to the old rc style
2721        * in bindings that we have.
2722        */
2723       if (orig_style)
2724         fixup_rc_sets (context, orig_style, rc_style);
2725     }
2726
2727   if (orig_style)
2728     g_object_unref (orig_style);
2729   
2730   return G_TOKEN_NONE;
2731
2732  err:
2733   if (rc_style != orig_style)
2734     gtk_rc_style_unref (rc_style);
2735
2736   if (orig_style)
2737     g_object_unref (orig_style);
2738   
2739   return token;
2740 }
2741
2742 const GtkRcProperty*
2743 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
2744                                   GQuark      type_name,
2745                                   GQuark      property_name)
2746 {
2747   GtkRcProperty *node = NULL;
2748
2749   g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
2750
2751   if (rc_style->rc_properties)
2752     {
2753       GtkRcProperty key;
2754
2755       key.type_name = type_name;
2756       key.property_name = property_name;
2757
2758       node = bsearch (&key,
2759                       rc_style->rc_properties->data, rc_style->rc_properties->len,
2760                       sizeof (GtkRcProperty), gtk_rc_properties_cmp);
2761     }
2762
2763   return node;
2764 }
2765
2766 static guint
2767 gtk_rc_parse_bg (GScanner   *scanner,
2768                  GtkRcStyle *style)
2769 {
2770   GtkStateType state;
2771   guint token;
2772   
2773   token = g_scanner_get_next_token (scanner);
2774   if (token != GTK_RC_TOKEN_BG)
2775     return GTK_RC_TOKEN_BG;
2776   
2777   token = gtk_rc_parse_state (scanner, &state);
2778   if (token != G_TOKEN_NONE)
2779     return token;
2780   
2781   token = g_scanner_get_next_token (scanner);
2782   if (token != G_TOKEN_EQUAL_SIGN)
2783     return G_TOKEN_EQUAL_SIGN;
2784
2785   style->color_flags[state] |= GTK_RC_BG;
2786   return gtk_rc_parse_color (scanner, &style->bg[state]);
2787 }
2788
2789 static guint
2790 gtk_rc_parse_fg (GScanner   *scanner,
2791                  GtkRcStyle *style)
2792 {
2793   GtkStateType state;
2794   guint token;
2795   
2796   token = g_scanner_get_next_token (scanner);
2797   if (token != GTK_RC_TOKEN_FG)
2798     return GTK_RC_TOKEN_FG;
2799   
2800   token = gtk_rc_parse_state (scanner, &state);
2801   if (token != G_TOKEN_NONE)
2802     return token;
2803   
2804   token = g_scanner_get_next_token (scanner);
2805   if (token != G_TOKEN_EQUAL_SIGN)
2806     return G_TOKEN_EQUAL_SIGN;
2807   
2808   style->color_flags[state] |= GTK_RC_FG;
2809   return gtk_rc_parse_color (scanner, &style->fg[state]);
2810 }
2811
2812 static guint
2813 gtk_rc_parse_text (GScanner   *scanner,
2814                    GtkRcStyle *style)
2815 {
2816   GtkStateType state;
2817   guint token;
2818   
2819   token = g_scanner_get_next_token (scanner);
2820   if (token != GTK_RC_TOKEN_TEXT)
2821     return GTK_RC_TOKEN_TEXT;
2822   
2823   token = gtk_rc_parse_state (scanner, &state);
2824   if (token != G_TOKEN_NONE)
2825     return token;
2826   
2827   token = g_scanner_get_next_token (scanner);
2828   if (token != G_TOKEN_EQUAL_SIGN)
2829     return G_TOKEN_EQUAL_SIGN;
2830   
2831   style->color_flags[state] |= GTK_RC_TEXT;
2832   return gtk_rc_parse_color (scanner, &style->text[state]);
2833 }
2834
2835 static guint
2836 gtk_rc_parse_base (GScanner   *scanner,
2837                    GtkRcStyle *style)
2838 {
2839   GtkStateType state;
2840   guint token;
2841   
2842   token = g_scanner_get_next_token (scanner);
2843   if (token != GTK_RC_TOKEN_BASE)
2844     return GTK_RC_TOKEN_BASE;
2845   
2846   token = gtk_rc_parse_state (scanner, &state);
2847   if (token != G_TOKEN_NONE)
2848     return token;
2849   
2850   token = g_scanner_get_next_token (scanner);
2851   if (token != G_TOKEN_EQUAL_SIGN)
2852     return G_TOKEN_EQUAL_SIGN;
2853
2854   style->color_flags[state] |= GTK_RC_BASE;
2855   return gtk_rc_parse_color (scanner, &style->base[state]);
2856 }
2857
2858 static guint
2859 gtk_rc_parse_xthickness (GScanner   *scanner,
2860                          GtkRcStyle *style)
2861 {
2862   if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
2863     return GTK_RC_TOKEN_XTHICKNESS;
2864
2865   if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2866     return G_TOKEN_EQUAL_SIGN;
2867
2868   if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2869     return G_TOKEN_INT;
2870
2871   style->xthickness = scanner->value.v_int;
2872
2873   return G_TOKEN_NONE;
2874 }
2875
2876 static guint
2877 gtk_rc_parse_ythickness (GScanner   *scanner,
2878                          GtkRcStyle *style)
2879 {
2880   if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
2881     return GTK_RC_TOKEN_YTHICKNESS;
2882
2883   if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2884     return G_TOKEN_EQUAL_SIGN;
2885
2886   if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2887     return G_TOKEN_INT;
2888
2889   style->ythickness = scanner->value.v_int;
2890
2891   return G_TOKEN_NONE;
2892 }
2893
2894 static guint
2895 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
2896                         GScanner     *scanner,
2897                         GtkRcStyle   *rc_style)
2898 {
2899   GtkStateType state;
2900   guint token;
2901   gchar *pixmap_file;
2902   
2903   token = g_scanner_get_next_token (scanner);
2904   if (token != GTK_RC_TOKEN_BG_PIXMAP)
2905     return GTK_RC_TOKEN_BG_PIXMAP;
2906   
2907   token = gtk_rc_parse_state (scanner, &state);
2908   if (token != G_TOKEN_NONE)
2909     return token;
2910   
2911   token = g_scanner_get_next_token (scanner);
2912   if (token != G_TOKEN_EQUAL_SIGN)
2913     return G_TOKEN_EQUAL_SIGN;
2914   
2915   token = g_scanner_get_next_token (scanner);
2916   if (token != G_TOKEN_STRING)
2917     return G_TOKEN_STRING;
2918   
2919   if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
2920       (strcmp (scanner->value.v_string, "<none>") == 0))
2921     pixmap_file = g_strdup (scanner->value.v_string);
2922   else
2923     pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
2924                                               scanner, scanner->value.v_string);
2925   
2926   if (pixmap_file)
2927     {
2928       if (rc_style->bg_pixmap_name[state])
2929         g_free (rc_style->bg_pixmap_name[state]);
2930       rc_style->bg_pixmap_name[state] = pixmap_file;
2931     }
2932   
2933   return G_TOKEN_NONE;
2934 }
2935
2936 static gchar*
2937 gtk_rc_check_pixmap_dir (const gchar *dir, const gchar *pixmap_file)
2938 {
2939   gchar *buf;
2940   gint fd;
2941
2942   buf = g_build_filename (dir, pixmap_file, NULL);
2943   
2944   fd = open (buf, O_RDONLY);
2945   if (fd >= 0)
2946     {
2947       close (fd);
2948       return buf;
2949     }
2950    
2951   g_free (buf);
2952  
2953    return NULL;
2954  }
2955
2956 /**
2957  * gtk_rc_find_pixmap_in_path:
2958  * @settings: a #GtkSettings
2959  * @scanner: Scanner used to get line number information for the
2960  *   warning message, or %NULL
2961  * @pixmap_file: name of the pixmap file to locate.
2962  * 
2963  * Looks up a file in pixmap path for the specified #GtkSettings.
2964  * If the file is not found, it outputs a warning message using
2965  * g_warning() and returns %NULL.
2966  *
2967  * Return value: the filename. 
2968  **/
2969 gchar*
2970 gtk_rc_find_pixmap_in_path (GtkSettings  *settings,
2971                             GScanner     *scanner,
2972                             const gchar  *pixmap_file)
2973 {
2974   gint i;
2975   gchar *filename;
2976   GSList *tmp_list;
2977
2978   GtkRcContext *context = gtk_rc_context_get (settings);
2979     
2980   for (i = 0; (i < GTK_RC_MAX_PIXMAP_PATHS) && (context->pixmap_path[i] != NULL); i++)
2981     {
2982       filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
2983       if (filename)
2984         return filename;
2985     }
2986  
2987   tmp_list = rc_dir_stack;
2988   while (tmp_list)
2989     {
2990       filename = gtk_rc_check_pixmap_dir (tmp_list->data, pixmap_file);
2991       if (filename)
2992         return filename;
2993        
2994       tmp_list = tmp_list->next;
2995     }
2996   
2997   if (scanner)
2998     g_scanner_warn (scanner, 
2999                     _("Unable to locate image file in pixmap_path: \"%s\""),
3000                     pixmap_file);
3001   else
3002     g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3003                pixmap_file);
3004     
3005   return NULL;
3006 }
3007
3008 /**
3009  * gtk_rc_find_module_in_path:
3010  * @module_file: name of a theme engine
3011  * 
3012  * Searches for a theme engine in the GTK+ search path. This function
3013  * is not useful for applications and should not be used.
3014  * 
3015  * Return value: The filename, if found (must be freed with g_free()),
3016  *   otherwise %NULL.
3017  **/
3018 gchar*
3019 gtk_rc_find_module_in_path (const gchar *module_file)
3020 {
3021   return _gtk_find_module (module_file, "engines");
3022 }
3023
3024 static guint
3025 gtk_rc_parse_font (GScanner   *scanner,
3026                    GtkRcStyle *rc_style)
3027 {
3028   guint token;
3029   
3030   token = g_scanner_get_next_token (scanner);
3031   if (token != GTK_RC_TOKEN_FONT)
3032     return GTK_RC_TOKEN_FONT;
3033   
3034   token = g_scanner_get_next_token (scanner);
3035   if (token != G_TOKEN_EQUAL_SIGN)
3036     return G_TOKEN_EQUAL_SIGN;
3037   
3038   token = g_scanner_get_next_token (scanner);
3039   if (token != G_TOKEN_STRING)
3040     return G_TOKEN_STRING;
3041
3042   /* Ignore, do nothing */
3043   
3044   return G_TOKEN_NONE;
3045 }
3046
3047 static guint
3048 gtk_rc_parse_fontset (GScanner   *scanner,
3049                       GtkRcStyle *rc_style)
3050 {
3051   guint token;
3052   
3053   token = g_scanner_get_next_token (scanner);
3054   if (token != GTK_RC_TOKEN_FONTSET)
3055     return GTK_RC_TOKEN_FONTSET;
3056   
3057   token = g_scanner_get_next_token (scanner);
3058   if (token != G_TOKEN_EQUAL_SIGN)
3059     return G_TOKEN_EQUAL_SIGN;
3060   
3061   token = g_scanner_get_next_token (scanner);
3062   if (token != G_TOKEN_STRING)
3063     return G_TOKEN_STRING;
3064
3065   /* Do nothing - silently ignore */
3066   
3067   return G_TOKEN_NONE;
3068 }
3069
3070 static guint
3071 gtk_rc_parse_font_name (GScanner   *scanner,
3072                         GtkRcStyle *rc_style)
3073 {
3074   guint token;
3075   
3076   token = g_scanner_get_next_token (scanner);
3077   if (token != GTK_RC_TOKEN_FONT_NAME)
3078     return GTK_RC_TOKEN_FONT;
3079   
3080   token = g_scanner_get_next_token (scanner);
3081   if (token != G_TOKEN_EQUAL_SIGN)
3082     return G_TOKEN_EQUAL_SIGN;
3083   
3084   token = g_scanner_get_next_token (scanner);
3085   if (token != G_TOKEN_STRING)
3086     return G_TOKEN_STRING;
3087
3088   if (rc_style->font_desc)
3089     pango_font_description_free (rc_style->font_desc);
3090
3091   rc_style->font_desc = 
3092     pango_font_description_from_string (scanner->value.v_string);
3093   
3094   return G_TOKEN_NONE;
3095 }
3096
3097 static guint       
3098 gtk_rc_parse_engine (GtkRcContext *context,
3099                      GScanner     *scanner,
3100                      GtkRcStyle  **rc_style)
3101 {
3102   guint token;
3103   GtkThemeEngine *engine;
3104   guint result = G_TOKEN_NONE;
3105   GtkRcStyle *new_style = NULL;
3106   gboolean parsed_curlies = FALSE;
3107   
3108   token = g_scanner_get_next_token (scanner);
3109   if (token != GTK_RC_TOKEN_ENGINE)
3110     return GTK_RC_TOKEN_ENGINE;
3111
3112   token = g_scanner_get_next_token (scanner);
3113   if (token != G_TOKEN_STRING)
3114     return G_TOKEN_STRING;
3115
3116   if (!scanner->value.v_string[0])
3117     {
3118       /* Support engine "" {} to mean override to the default engine
3119        */
3120       token = g_scanner_get_next_token (scanner);
3121       if (token != G_TOKEN_LEFT_CURLY)
3122         return G_TOKEN_LEFT_CURLY;
3123       
3124       token = g_scanner_get_next_token (scanner);
3125       if (token != G_TOKEN_RIGHT_CURLY)
3126         return G_TOKEN_RIGHT_CURLY;
3127
3128       parsed_curlies = TRUE;
3129
3130       if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3131         {
3132           new_style = gtk_rc_style_new ();
3133           gtk_rc_style_real_merge (new_style, *rc_style);
3134           
3135           if ((*rc_style)->name)
3136             new_style->name = g_strdup ((*rc_style)->name);
3137         }
3138       else
3139         (*rc_style)->engine_specified = TRUE;
3140     }
3141   else
3142     {
3143       engine = gtk_theme_engine_get (scanner->value.v_string);
3144       
3145       token = g_scanner_get_next_token (scanner);
3146       if (token != G_TOKEN_LEFT_CURLY)
3147         return G_TOKEN_LEFT_CURLY;
3148       
3149       if (engine)
3150         {
3151           GtkRcStyleClass *new_class;
3152           
3153           new_style = gtk_theme_engine_create_rc_style (engine);
3154           g_type_module_unuse (G_TYPE_MODULE (engine));
3155           
3156           new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3157           
3158           new_class->merge (new_style, *rc_style);
3159           if ((*rc_style)->name)
3160             new_style->name = g_strdup ((*rc_style)->name);
3161           
3162           if (new_class->parse)
3163             {
3164               parsed_curlies = TRUE;
3165               result = new_class->parse (new_style, context->settings, scanner);
3166               
3167               if (result != G_TOKEN_NONE)
3168                 {
3169                   g_object_unref (G_OBJECT (new_style));
3170                   new_style = NULL;
3171                 }
3172             }
3173         }
3174     }
3175
3176   if (!parsed_curlies)
3177     {
3178       /* Skip over remainder, looking for nested {}'s
3179        */
3180       guint count = 1;
3181       
3182       result = G_TOKEN_RIGHT_CURLY;
3183       while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3184         {
3185           if (token == G_TOKEN_LEFT_CURLY)
3186             count++;
3187           else if (token == G_TOKEN_RIGHT_CURLY)
3188             count--;
3189           
3190           if (count == 0)
3191             {
3192               result = G_TOKEN_NONE;
3193               break;
3194             }
3195         }
3196     }
3197
3198   if (new_style)
3199     {
3200       new_style->engine_specified = TRUE;
3201       
3202       g_object_unref (G_OBJECT (*rc_style));
3203       *rc_style = new_style;
3204     }
3205
3206   return result;
3207 }
3208
3209 guint
3210 gtk_rc_parse_state (GScanner     *scanner,
3211                     GtkStateType *state)
3212 {
3213   guint old_scope;
3214   guint token;
3215
3216   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3217   g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3218   
3219   /* we don't know where we got called from, so we reset the scope here.
3220    * if we bail out due to errors, we *don't* reset the scope, so the
3221    * error messaging code can make sense of our tokens.
3222    */
3223   old_scope = g_scanner_set_scope (scanner, 0);
3224   
3225   token = g_scanner_get_next_token (scanner);
3226   if (token != G_TOKEN_LEFT_BRACE)
3227     return G_TOKEN_LEFT_BRACE;
3228   
3229   token = g_scanner_get_next_token (scanner);
3230   switch (token)
3231     {
3232     case GTK_RC_TOKEN_ACTIVE:
3233       *state = GTK_STATE_ACTIVE;
3234       break;
3235     case GTK_RC_TOKEN_INSENSITIVE:
3236       *state = GTK_STATE_INSENSITIVE;
3237       break;
3238     case GTK_RC_TOKEN_NORMAL:
3239       *state = GTK_STATE_NORMAL;
3240       break;
3241     case GTK_RC_TOKEN_PRELIGHT:
3242       *state = GTK_STATE_PRELIGHT;
3243       break;
3244     case GTK_RC_TOKEN_SELECTED:
3245       *state = GTK_STATE_SELECTED;
3246       break;
3247     default:
3248       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3249     }
3250   
3251   token = g_scanner_get_next_token (scanner);
3252   if (token != G_TOKEN_RIGHT_BRACE)
3253     return G_TOKEN_RIGHT_BRACE;
3254   
3255   g_scanner_set_scope (scanner, old_scope);
3256
3257   return G_TOKEN_NONE;
3258 }
3259
3260 guint
3261 gtk_rc_parse_priority (GScanner            *scanner,
3262                        GtkPathPriorityType *priority)
3263 {
3264   guint old_scope;
3265   guint token;
3266
3267   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3268   g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3269
3270   /* we don't know where we got called from, so we reset the scope here.
3271    * if we bail out due to errors, we *don't* reset the scope, so the
3272    * error messaging code can make sense of our tokens.
3273    */
3274   old_scope = g_scanner_set_scope (scanner, 0);
3275   
3276   token = g_scanner_get_next_token (scanner);
3277   if (token != ':')
3278     return ':';
3279   
3280   token = g_scanner_get_next_token (scanner);
3281   switch (token)
3282     {
3283     case GTK_RC_TOKEN_LOWEST:
3284       *priority = GTK_PATH_PRIO_LOWEST;
3285       break;
3286     case GTK_RC_TOKEN_GTK:
3287       *priority = GTK_PATH_PRIO_GTK;
3288       break;
3289     case GTK_RC_TOKEN_APPLICATION:
3290       *priority = GTK_PATH_PRIO_APPLICATION;
3291       break;
3292     case GTK_RC_TOKEN_THEME:
3293       *priority = GTK_PATH_PRIO_THEME;
3294       break;
3295     case GTK_RC_TOKEN_RC:
3296       *priority = GTK_PATH_PRIO_RC;
3297       break;
3298     case GTK_RC_TOKEN_HIGHEST:
3299       *priority = GTK_PATH_PRIO_HIGHEST;
3300       break;
3301     default:
3302       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3303     }
3304   
3305   g_scanner_set_scope (scanner, old_scope);
3306
3307   return G_TOKEN_NONE;
3308 }
3309
3310 guint
3311 gtk_rc_parse_color (GScanner *scanner,
3312                     GdkColor *color)
3313 {
3314   guint token;
3315
3316   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3317
3318   /* we don't need to set our own scope here, because
3319    * we don't need own symbols
3320    */
3321   
3322   token = g_scanner_get_next_token (scanner);
3323   switch (token)
3324     {
3325       gint token_int;
3326       
3327     case G_TOKEN_LEFT_CURLY:
3328       token = g_scanner_get_next_token (scanner);
3329       if (token == G_TOKEN_INT)
3330         token_int = scanner->value.v_int;
3331       else if (token == G_TOKEN_FLOAT)
3332         token_int = scanner->value.v_float * 65535.0;
3333       else
3334         return G_TOKEN_FLOAT;
3335       color->red = CLAMP (token_int, 0, 65535);
3336       
3337       token = g_scanner_get_next_token (scanner);
3338       if (token != G_TOKEN_COMMA)
3339         return G_TOKEN_COMMA;
3340       
3341       token = g_scanner_get_next_token (scanner);
3342       if (token == G_TOKEN_INT)
3343         token_int = scanner->value.v_int;
3344       else if (token == G_TOKEN_FLOAT)
3345         token_int = scanner->value.v_float * 65535.0;
3346       else
3347         return G_TOKEN_FLOAT;
3348       color->green = CLAMP (token_int, 0, 65535);
3349       
3350       token = g_scanner_get_next_token (scanner);
3351       if (token != G_TOKEN_COMMA)
3352         return G_TOKEN_COMMA;
3353       
3354       token = g_scanner_get_next_token (scanner);
3355       if (token == G_TOKEN_INT)
3356         token_int = scanner->value.v_int;
3357       else if (token == G_TOKEN_FLOAT)
3358         token_int = scanner->value.v_float * 65535.0;
3359       else
3360         return G_TOKEN_FLOAT;
3361       color->blue = CLAMP (token_int, 0, 65535);
3362       
3363       token = g_scanner_get_next_token (scanner);
3364       if (token != G_TOKEN_RIGHT_CURLY)
3365         return G_TOKEN_RIGHT_CURLY;
3366       return G_TOKEN_NONE;
3367       
3368     case G_TOKEN_STRING:
3369       if (!gdk_color_parse (scanner->value.v_string, color))
3370         {
3371           g_scanner_warn (scanner, "Invalid color constant '%s'",
3372                           scanner->value.v_string);
3373           return G_TOKEN_STRING;
3374         }
3375       else
3376         return G_TOKEN_NONE;
3377       
3378     default:
3379       return G_TOKEN_STRING;
3380     }
3381 }
3382
3383 static guint
3384 gtk_rc_parse_pixmap_path (GtkRcContext *context,
3385                           GScanner     *scanner)
3386 {
3387   guint token;
3388   
3389   token = g_scanner_get_next_token (scanner);
3390   if (token != GTK_RC_TOKEN_PIXMAP_PATH)
3391     return GTK_RC_TOKEN_PIXMAP_PATH;
3392   
3393   token = g_scanner_get_next_token (scanner);
3394   if (token != G_TOKEN_STRING)
3395     return G_TOKEN_STRING;
3396   
3397   gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
3398   
3399   return G_TOKEN_NONE;
3400 }
3401
3402 static void
3403 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
3404                                  GScanner     *scanner,
3405                                  const gchar  *pix_path)
3406 {
3407   gint end_offset;
3408   gint start_offset = 0;
3409   gint path_len;
3410   gint path_num;
3411   
3412   /* free the old one, or just add to the old one ? */
3413   for (path_num = 0; context->pixmap_path[path_num]; path_num++)
3414     {
3415       g_free (context->pixmap_path[path_num]);
3416       context->pixmap_path[path_num] = NULL;
3417     }
3418   
3419   path_num = 0;
3420   
3421   path_len = strlen (pix_path);
3422   
3423   for (end_offset = 0; end_offset <= path_len; end_offset++)
3424     {
3425       if ((pix_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3426           (end_offset == path_len))
3427         {
3428           gchar *path_element = g_strndup (pix_path + start_offset, end_offset - start_offset);
3429           if (g_path_is_absolute (path_element))
3430             {
3431               context->pixmap_path[path_num] = path_element;
3432               path_num++;
3433               context->pixmap_path[path_num] = NULL;
3434             }
3435           else
3436             {
3437               g_warning (_("Pixmap path element: \"%s\" must be absolute, %s, line %d"),
3438                          path_element, scanner->input_name, scanner->line);
3439               g_free (path_element);
3440             }
3441
3442           start_offset = end_offset + 1;
3443         }
3444     }
3445 }
3446
3447 static guint
3448 gtk_rc_parse_module_path (GScanner *scanner)
3449 {
3450   guint token;
3451   
3452   token = g_scanner_get_next_token (scanner);
3453   if (token != GTK_RC_TOKEN_MODULE_PATH)
3454     return GTK_RC_TOKEN_MODULE_PATH;
3455   
3456   token = g_scanner_get_next_token (scanner);
3457   if (token != G_TOKEN_STRING)
3458     return G_TOKEN_STRING;
3459
3460   g_warning ("module_path directive is now ignored\n");
3461
3462   return G_TOKEN_NONE;
3463 }
3464
3465 static guint
3466 gtk_rc_parse_im_module_file (GScanner *scanner)
3467 {
3468   guint token;
3469   
3470   token = g_scanner_get_next_token (scanner);
3471   if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3472     return GTK_RC_TOKEN_IM_MODULE_FILE;
3473   
3474   token = g_scanner_get_next_token (scanner);
3475   if (token != G_TOKEN_STRING)
3476     return G_TOKEN_STRING;
3477
3478   if (im_module_file)
3479     g_free (im_module_file);
3480     
3481   im_module_file = g_strdup (scanner->value.v_string);
3482
3483   return G_TOKEN_NONE;
3484 }
3485
3486 static guint
3487 gtk_rc_parse_path_pattern (GtkRcContext *context,
3488                            GScanner     *scanner)
3489 {
3490   guint token;
3491   GtkPathType path_type;
3492   gchar *pattern;
3493   gboolean is_binding;
3494   GtkPathPriorityType priority = context->default_priority;
3495   
3496   token = g_scanner_get_next_token (scanner);
3497   switch (token)
3498     {
3499     case GTK_RC_TOKEN_WIDGET:
3500       path_type = GTK_PATH_WIDGET;
3501       break;
3502     case GTK_RC_TOKEN_WIDGET_CLASS:
3503       path_type = GTK_PATH_WIDGET_CLASS;
3504       break;
3505     case GTK_RC_TOKEN_CLASS:
3506       path_type = GTK_PATH_CLASS;
3507       break;
3508     default:
3509       return GTK_RC_TOKEN_WIDGET_CLASS;
3510     }
3511
3512   token = g_scanner_get_next_token (scanner);
3513   if (token != G_TOKEN_STRING)
3514     return G_TOKEN_STRING;
3515
3516   pattern = g_strdup (scanner->value.v_string);
3517
3518   token = g_scanner_get_next_token (scanner);
3519   if (token == GTK_RC_TOKEN_STYLE)
3520     is_binding = FALSE;
3521   else if (token == GTK_RC_TOKEN_BINDING)
3522     is_binding = TRUE;
3523   else
3524     {
3525       g_free (pattern);
3526       return GTK_RC_TOKEN_STYLE;
3527     }
3528   
3529   if (g_scanner_peek_next_token (scanner) == ':')
3530     {
3531       token = gtk_rc_parse_priority (scanner, &priority);
3532       if (token != G_TOKEN_NONE)
3533         {
3534           g_free (pattern);
3535           return token;
3536         }
3537     }
3538   
3539   token = g_scanner_get_next_token (scanner);
3540   if (token != G_TOKEN_STRING)
3541     {
3542       g_free (pattern);
3543       return G_TOKEN_STRING;
3544     }
3545
3546   if (is_binding)
3547     {
3548       GtkBindingSet *binding;
3549
3550       binding = gtk_binding_set_find (scanner->value.v_string);
3551       if (!binding)
3552         {
3553           g_free (pattern);
3554           return G_TOKEN_STRING;
3555         }
3556       gtk_binding_set_add_path (binding, path_type, pattern, priority);
3557     }
3558   else
3559     {
3560       GtkRcStyle *rc_style;
3561       GtkRcSet *rc_set;
3562
3563       rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3564       
3565       if (!rc_style)
3566         {
3567           g_free (pattern);
3568           return G_TOKEN_STRING;
3569         }
3570
3571       rc_set = g_new (GtkRcSet, 1);
3572       rc_set->pspec = g_pattern_spec_new (pattern);
3573       rc_set->rc_style = rc_style;
3574       rc_set->priority = priority;
3575
3576       if (path_type == GTK_PATH_WIDGET)
3577         context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
3578       else if (path_type == GTK_PATH_WIDGET_CLASS)
3579         context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
3580       else
3581         context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
3582     }
3583
3584   g_free (pattern);
3585   return G_TOKEN_NONE;
3586 }
3587
3588 static guint
3589 gtk_rc_parse_stock_id (GScanner  *scanner,
3590                        gchar    **stock_id)
3591 {
3592   guint token;
3593   
3594   token = g_scanner_get_next_token (scanner);
3595   if (token != G_TOKEN_LEFT_BRACE)
3596     return G_TOKEN_LEFT_BRACE;
3597
3598   token = g_scanner_get_next_token (scanner);
3599   
3600   if (token != G_TOKEN_STRING)
3601     return G_TOKEN_STRING;
3602   
3603   *stock_id = g_strdup (scanner->value.v_string);
3604   
3605   token = g_scanner_get_next_token (scanner);
3606   if (token != G_TOKEN_RIGHT_BRACE)
3607     {
3608       g_free (*stock_id);
3609       return G_TOKEN_RIGHT_BRACE;
3610     }
3611   
3612   return G_TOKEN_NONE;
3613 }
3614
3615 static guint
3616 gtk_rc_parse_icon_source (GtkRcContext   *context,
3617                           GScanner       *scanner,
3618                           GtkIconSet     *icon_set,
3619                           gboolean       *icon_set_valid)
3620 {
3621   guint token;
3622   GtkIconSource *source;
3623   gchar *full_filename;
3624   
3625   token = g_scanner_get_next_token (scanner);
3626   if (token != G_TOKEN_LEFT_CURLY)
3627     return G_TOKEN_LEFT_CURLY;
3628
3629   token = g_scanner_get_next_token (scanner);
3630   
3631   if (token != G_TOKEN_STRING)
3632     return G_TOKEN_STRING;
3633
3634   
3635   source = gtk_icon_source_new ();
3636   
3637   full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
3638   if (full_filename)
3639     {
3640       gtk_icon_source_set_filename (source, full_filename);
3641       g_free (full_filename);
3642     }
3643
3644   /* We continue parsing even if we didn't find the pixmap so that rest of the
3645    * file is read, even if the syntax is bad. However we don't validate the 
3646    * icon_set so the caller can choose not to install it.
3647    */
3648   token = g_scanner_get_next_token (scanner);
3649
3650   if (token == G_TOKEN_RIGHT_CURLY)
3651     goto done;
3652   else if (token != G_TOKEN_COMMA)
3653     {
3654       gtk_icon_source_free (source);
3655       return G_TOKEN_COMMA;
3656     }
3657
3658   /* Get the direction */
3659   
3660   token = g_scanner_get_next_token (scanner);
3661
3662   switch (token)
3663     {
3664     case GTK_RC_TOKEN_RTL:
3665       gtk_icon_source_set_direction_wildcarded (source, FALSE);
3666       gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
3667       break;
3668
3669     case GTK_RC_TOKEN_LTR:
3670       gtk_icon_source_set_direction_wildcarded (source, FALSE);
3671       gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
3672       break;
3673       
3674     case '*':
3675       break;
3676       
3677     default:
3678       gtk_icon_source_free (source);
3679       return GTK_RC_TOKEN_RTL;
3680       break;
3681     }
3682
3683   token = g_scanner_get_next_token (scanner);
3684
3685   if (token == G_TOKEN_RIGHT_CURLY)
3686     goto done;
3687   else if (token != G_TOKEN_COMMA)
3688     {
3689       gtk_icon_source_free (source);
3690       return G_TOKEN_COMMA;
3691     }
3692
3693   /* Get the state */
3694   
3695   token = g_scanner_get_next_token (scanner);
3696   
3697   switch (token)
3698     {
3699     case GTK_RC_TOKEN_NORMAL:
3700       gtk_icon_source_set_state_wildcarded (source, FALSE);
3701       gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
3702       break;
3703
3704     case GTK_RC_TOKEN_PRELIGHT:
3705       gtk_icon_source_set_state_wildcarded (source, FALSE);
3706       gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
3707       break;
3708       
3709
3710     case GTK_RC_TOKEN_INSENSITIVE:
3711       gtk_icon_source_set_state_wildcarded (source, FALSE);
3712       gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
3713       break;
3714
3715     case GTK_RC_TOKEN_ACTIVE:
3716       gtk_icon_source_set_state_wildcarded (source, FALSE);
3717       gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
3718       break;
3719
3720     case GTK_RC_TOKEN_SELECTED:
3721       gtk_icon_source_set_state_wildcarded (source, FALSE);
3722       gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
3723       break;
3724
3725     case '*':
3726       break;
3727       
3728     default:
3729       gtk_icon_source_free (source);
3730       return GTK_RC_TOKEN_PRELIGHT;
3731       break;
3732     }  
3733
3734   token = g_scanner_get_next_token (scanner);
3735
3736   if (token == G_TOKEN_RIGHT_CURLY)
3737     goto done;
3738   else if (token != G_TOKEN_COMMA)
3739     {
3740       gtk_icon_source_free (source);
3741       return G_TOKEN_COMMA;
3742     }
3743   
3744   /* Get the size */
3745   
3746   token = g_scanner_get_next_token (scanner);
3747
3748   if (token != '*')
3749     {
3750       GtkIconSize size;
3751       
3752       if (token != G_TOKEN_STRING)
3753         {
3754           gtk_icon_source_free (source);
3755           return G_TOKEN_STRING;
3756         }
3757
3758       size = gtk_icon_size_from_name (scanner->value.v_string);
3759
3760       if (size != GTK_ICON_SIZE_INVALID)
3761         {
3762           gtk_icon_source_set_size_wildcarded (source, FALSE);
3763           gtk_icon_source_set_size (source, size);
3764         }
3765     }
3766
3767   /* Check the close brace */
3768   
3769   token = g_scanner_get_next_token (scanner);
3770   if (token != G_TOKEN_RIGHT_CURLY)
3771     {
3772       gtk_icon_source_free (source);
3773       return G_TOKEN_RIGHT_CURLY;
3774     }
3775
3776  done:
3777   if (gtk_icon_source_get_filename (source))
3778     {
3779       gtk_icon_set_add_source (icon_set, source);
3780       *icon_set_valid = TRUE;
3781     }
3782   gtk_icon_source_free (source);
3783   
3784   return G_TOKEN_NONE;
3785 }
3786
3787 static guint
3788 gtk_rc_parse_stock (GtkRcContext   *context,
3789                     GScanner       *scanner,
3790                     GtkRcStyle     *rc_style,
3791                     GtkIconFactory *factory)
3792 {
3793   GtkIconSet *icon_set = NULL;
3794   gboolean icon_set_valid = FALSE;
3795   gchar *stock_id = NULL;
3796   guint token;
3797   
3798   token = g_scanner_get_next_token (scanner);
3799   if (token != GTK_RC_TOKEN_STOCK)
3800     return GTK_RC_TOKEN_STOCK;
3801   
3802   token = gtk_rc_parse_stock_id (scanner, &stock_id);
3803   if (token != G_TOKEN_NONE)
3804     return token;
3805   
3806   token = g_scanner_get_next_token (scanner);
3807   if (token != G_TOKEN_EQUAL_SIGN)
3808     {
3809       g_free (stock_id);
3810       return G_TOKEN_EQUAL_SIGN;
3811     }
3812
3813   token = g_scanner_get_next_token (scanner);
3814   if (token != G_TOKEN_LEFT_CURLY)
3815     {
3816       g_free (stock_id);
3817       return G_TOKEN_LEFT_CURLY;
3818     }
3819
3820   token = g_scanner_peek_next_token (scanner);
3821   while (token != G_TOKEN_RIGHT_CURLY)
3822     {
3823       if (icon_set == NULL)
3824         icon_set = gtk_icon_set_new ();
3825       
3826       token = gtk_rc_parse_icon_source (context, 
3827                                         scanner, icon_set, &icon_set_valid);
3828       if (token != G_TOKEN_NONE)
3829         {
3830           g_free (stock_id);
3831           gtk_icon_set_unref (icon_set);
3832           return token;
3833         }
3834
3835       token = g_scanner_get_next_token (scanner);
3836       
3837       if (token != G_TOKEN_COMMA &&
3838           token != G_TOKEN_RIGHT_CURLY)
3839         {
3840           g_free (stock_id);
3841           gtk_icon_set_unref (icon_set);
3842           return G_TOKEN_RIGHT_CURLY;
3843         }
3844     }
3845
3846   if (icon_set)
3847     {
3848       if (icon_set_valid)
3849         gtk_icon_factory_add (factory,
3850                               stock_id,
3851                               icon_set);
3852
3853       gtk_icon_set_unref (icon_set);
3854     }
3855   
3856   g_free (stock_id);
3857
3858   return G_TOKEN_NONE;
3859 }