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