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