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