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