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