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