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