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