]> Pileus Git - ~andy/gtk/blob - gtk/gtkrc.c
initialize rc_priv in the else branch too. (#445539)
[~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 = _gtk_rc_style_lookup_rc_property (rc_style, type_name, property_name);
1289
1290   if (node != NULL)
1291     {
1292       guint index = node - (GtkRcProperty *) rc_style->rc_properties->data;
1293       g_value_unset (&node->value);
1294       g_free (node->origin);
1295       g_array_remove_index (rc_style->rc_properties, index);
1296     }
1297 }
1298
1299 void      
1300 gtk_rc_style_ref (GtkRcStyle *rc_style)
1301 {
1302   g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1303
1304   g_object_ref (rc_style);
1305 }
1306
1307 void      
1308 gtk_rc_style_unref (GtkRcStyle *rc_style)
1309 {
1310   g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1311
1312   g_object_unref (rc_style);
1313 }
1314
1315 static GtkRcStyle *
1316 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1317 {
1318   return g_object_new (G_OBJECT_TYPE (style), NULL);
1319 }
1320
1321 GSList *
1322 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1323 {
1324   GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1325
1326   return priv->color_hashes;
1327 }
1328
1329 static gint
1330 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1331                        gconstpointer bsearch_node2)
1332 {
1333   const GtkRcProperty *prop1 = bsearch_node1;
1334   const GtkRcProperty *prop2 = bsearch_node2;
1335
1336   if (prop1->type_name == prop2->type_name)
1337     return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1338   else
1339     return prop1->type_name < prop2->type_name ? -1 : 1;
1340 }
1341
1342 static void
1343 insert_rc_property (GtkRcStyle    *style,
1344                     GtkRcProperty *property,
1345                     gboolean       replace)
1346 {
1347   guint i;
1348   GtkRcProperty *new_property = NULL;
1349   GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1350
1351   key.type_name = property->type_name;
1352   key.property_name = property->property_name;
1353
1354   if (!style->rc_properties)
1355     style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1356
1357   i = 0;
1358   while (i < style->rc_properties->len)
1359     {
1360       gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1361
1362       if (cmp == 0)
1363         {
1364           if (replace)
1365             {
1366               new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1367               
1368               g_free (new_property->origin);
1369               g_value_unset (&new_property->value);
1370               
1371               *new_property = key;
1372               break;
1373             }
1374           else
1375             return;
1376         }
1377       else if (cmp < 0)
1378         break;
1379
1380       i++;
1381     }
1382
1383   if (!new_property)
1384     {
1385       g_array_insert_val (style->rc_properties, i, key);
1386       new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1387     }
1388
1389   new_property->origin = g_strdup (property->origin);
1390   g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1391   g_value_copy (&property->value, &new_property->value);
1392 }
1393
1394 static void
1395 gtk_rc_style_real_merge (GtkRcStyle *dest,
1396                          GtkRcStyle *src)
1397 {
1398   gint i;
1399
1400   for (i = 0; i < 5; i++)
1401     {
1402       if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1403         dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1404       
1405       if (!(dest->color_flags[i] & GTK_RC_FG) && 
1406           src->color_flags[i] & GTK_RC_FG)
1407         {
1408           dest->fg[i] = src->fg[i];
1409           dest->color_flags[i] |= GTK_RC_FG;
1410         }
1411       if (!(dest->color_flags[i] & GTK_RC_BG) && 
1412           src->color_flags[i] & GTK_RC_BG)
1413         {
1414           dest->bg[i] = src->bg[i];
1415           dest->color_flags[i] |= GTK_RC_BG;
1416         }
1417       if (!(dest->color_flags[i] & GTK_RC_TEXT) && 
1418           src->color_flags[i] & GTK_RC_TEXT)
1419         {
1420           dest->text[i] = src->text[i];
1421           dest->color_flags[i] |= GTK_RC_TEXT;
1422         }
1423       if (!(dest->color_flags[i] & GTK_RC_BASE) && 
1424           src->color_flags[i] & GTK_RC_BASE)
1425         {
1426           dest->base[i] = src->base[i];
1427           dest->color_flags[i] |= GTK_RC_BASE;
1428         }
1429     }
1430
1431   if (dest->xthickness < 0 && src->xthickness >= 0)
1432     dest->xthickness = src->xthickness;
1433   if (dest->ythickness < 0 && src->ythickness >= 0)
1434     dest->ythickness = src->ythickness;
1435
1436   if (src->font_desc)
1437     {
1438       if (!dest->font_desc)
1439         dest->font_desc = pango_font_description_copy (src->font_desc);
1440       else
1441         pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1442     }
1443
1444   if (src->rc_properties)
1445     {
1446       guint i;
1447
1448       for (i = 0; i < src->rc_properties->len; i++)
1449         insert_rc_property (dest,
1450                             &g_array_index (src->rc_properties, GtkRcProperty, i),
1451                             FALSE);
1452     }
1453 }
1454
1455 static GtkStyle *
1456 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1457 {
1458   return gtk_style_new ();
1459 }
1460
1461 static void
1462 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1463 {
1464   GtkIconFactory *factory = gtk_icon_factory_new ();
1465
1466   rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1467 }
1468
1469 static void
1470 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1471 {
1472   GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1473   GHashTable        *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1474                                                    g_free,
1475                                                    (GDestroyNotify) gdk_color_free);
1476
1477   priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1478 }
1479
1480 static void
1481 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1482                                     GtkRcStyle *src_style)
1483 {
1484   GSList *concat = g_slist_copy (src_style->icon_factories);
1485
1486   g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1487
1488   rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1489 }
1490
1491 static void
1492 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1493                                   GtkRcStyle *src_style)
1494 {
1495   GtkRcStylePrivate *priv     = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1496   GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1497   GSList            *concat   = g_slist_copy (src_priv->color_hashes);
1498
1499   g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1500
1501   priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1502 }
1503
1504 static void
1505 gtk_rc_style_copy_icons_and_colors (GtkRcStyle   *rc_style,
1506                                     GtkRcStyle   *src_style,
1507                                     GtkRcContext *context)
1508 {
1509   GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1510
1511   if (src_style)
1512     {
1513       GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1514
1515       /* Append src_style's factories, adding a ref to them */
1516       if (src_style->icon_factories != NULL)
1517         {
1518           /* Add a factory for ourselves if we have none,
1519            * in case we end up defining more stock icons.
1520            * I see no real way around this; we need to maintain
1521            * the invariant that the first factory in the list
1522            * is always our_factory, the one belonging to us,
1523            * and if we put src_style factories in the list we can't
1524            * do that if the style is reopened.
1525            */
1526           if (rc_style->icon_factories == NULL)
1527             gtk_rc_style_prepend_empty_icon_factory (rc_style);
1528
1529           gtk_rc_style_append_icon_factories (rc_style, src_style);
1530         }
1531
1532       /* Also append src_style's color hashes, adding a ref to them */
1533       if (src_priv->color_hashes != NULL)
1534         {
1535           /* See comment above .. */
1536           if (priv->color_hashes == NULL)
1537             gtk_rc_style_prepend_empty_color_hash (rc_style);
1538
1539           gtk_rc_style_append_color_hashes (rc_style, src_style);
1540         }
1541     }
1542
1543   /*  if we didn't get color hashes from the src_style, initialize
1544    *  the list with the settings' color scheme (if it exists)
1545    */
1546   if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1547     {
1548       gtk_rc_style_prepend_empty_color_hash (rc_style);
1549
1550       priv->color_hashes = g_slist_append (priv->color_hashes,
1551                                            g_hash_table_ref (context->color_hash));
1552     }
1553 }
1554
1555 static void
1556 gtk_rc_clear_hash_node (gpointer key, 
1557                         gpointer data, 
1558                         gpointer user_data)
1559 {
1560   gtk_rc_style_unref (data);
1561 }
1562
1563 static void
1564 gtk_rc_free_rc_sets (GSList *slist)
1565 {
1566   while (slist)
1567     {
1568       GtkRcSet *rc_set;
1569
1570       rc_set = slist->data;
1571       gtk_rc_set_free (rc_set);
1572
1573       slist = slist->next;
1574     }
1575 }
1576
1577 static void
1578 gtk_rc_clear_styles (GtkRcContext *context)
1579 {
1580   /* Clear out all old rc_styles */
1581
1582   if (context->rc_style_ht)
1583     {
1584       g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1585       g_hash_table_destroy (context->rc_style_ht);
1586       context->rc_style_ht = NULL;
1587     }
1588
1589   gtk_rc_free_rc_sets (context->rc_sets_widget);
1590   g_slist_free (context->rc_sets_widget);
1591   context->rc_sets_widget = NULL;
1592
1593   gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1594   g_slist_free (context->rc_sets_widget_class);
1595   context->rc_sets_widget_class = NULL;
1596
1597   gtk_rc_free_rc_sets (context->rc_sets_class);
1598   g_slist_free (context->rc_sets_class);
1599   context->rc_sets_class = NULL;
1600 }
1601
1602 /* Reset all our widgets. Also, we have to invalidate cached icons in
1603  * icon sets so they get re-rendered.
1604  */
1605 static void
1606 gtk_rc_reset_widgets (GtkSettings *settings)
1607 {
1608   GList *list, *toplevels;
1609
1610   _gtk_icon_set_invalidate_caches ();
1611   
1612   toplevels = gtk_window_list_toplevels ();
1613   g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1614   
1615   for (list = toplevels; list; list = list->next)
1616     {
1617       if (gtk_widget_get_screen (list->data) == settings->screen)
1618         {
1619           gtk_widget_reset_rc_styles (list->data);
1620         }
1621
1622       g_object_unref (list->data);
1623     }
1624   g_list_free (toplevels);
1625 }
1626
1627 static void
1628 gtk_rc_clear_realized_style (gpointer key,
1629                              gpointer value,
1630                              gpointer data)
1631 {
1632   GSList *rc_styles = key;
1633   GtkStyle *style = value;
1634   GSList *tmp_list = rc_styles;
1635
1636   g_object_unref (style);
1637  
1638   while (tmp_list)
1639     {
1640       GtkRcStyle *rc_style = tmp_list->data;
1641       
1642       rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1643                                                      rc_styles);
1644       tmp_list = tmp_list->next;
1645     }
1646
1647   g_slist_free (rc_styles);
1648 }
1649
1650 /**
1651  * gtk_rc_reset_styles:
1652  * @settings: a #GtkSettings
1653  * 
1654  * This function recomputes the styles for all widgets that use a
1655  * particular #GtkSettings object. (There is one #GtkSettings object
1656  * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1657  * when some global parameter has changed that affects the appearance
1658  * of all widgets, because when a widget gets a new style, it will
1659  * both redraw and recompute any cached information about its
1660  * appearance. As an example, it is used when the default font size
1661  * set by the operating system changes. Note that this function
1662  * doesn't affect widgets that have a style set explicitely on them
1663  * with gtk_widget_set_style().
1664  *
1665  * Since: 2.4
1666  **/
1667 void
1668 gtk_rc_reset_styles (GtkSettings *settings)
1669 {
1670   GtkRcContext *context;
1671   gboolean reset = FALSE;
1672
1673   g_return_if_fail (GTK_IS_SETTINGS (settings));
1674
1675   context = gtk_rc_context_get (settings);
1676   
1677   if (context->default_style)
1678     {
1679       g_object_unref (context->default_style);
1680       context->default_style = NULL;
1681       reset = TRUE;
1682     }
1683   
1684   /* Clear out styles that have been looked up already
1685    */
1686   if (realized_style_ht)
1687     {
1688       g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1689       g_hash_table_destroy (realized_style_ht);
1690       realized_style_ht = NULL;
1691       reset = TRUE;
1692     }
1693   
1694   if (reset)
1695     gtk_rc_reset_widgets (settings);
1696 }
1697
1698 const gchar*
1699 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1700 {
1701   GtkRcContext *context;
1702   gchar *new_font_name;
1703   
1704   g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1705
1706   context = gtk_rc_context_get (settings);
1707
1708   g_object_get (context->settings,
1709                 "gtk-font-name", &new_font_name,
1710                 NULL);
1711
1712   if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1713     {
1714        g_free (context->font_name);
1715        context->font_name = g_strdup (new_font_name);
1716  
1717        gtk_rc_reset_styles (settings);
1718     }
1719           
1720   g_free (new_font_name);
1721
1722   return context->font_name;
1723 }
1724
1725 /**
1726  * gtk_rc_reparse_all_for_settings:
1727  * @settings: a #GtkSettings
1728  * @force_load: load whether or not anything changed
1729  * 
1730  * If the modification time on any previously read file
1731  * for the given #GtkSettings has changed, discard all style information
1732  * and then reread all previously read RC files.
1733  * 
1734  * Return value: %TRUE if the files were reread.
1735  **/
1736 gboolean
1737 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1738                                  gboolean     force_load)
1739 {
1740   gboolean mtime_modified = FALSE;
1741   GtkRcFile *rc_file;
1742   GSList *tmp_list;
1743   GtkRcContext *context;
1744   struct stat statbuf;
1745
1746   g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1747
1748   context = gtk_rc_context_get (settings);
1749
1750   if (context->reloading)
1751     return FALSE;
1752
1753   if (!force_load)
1754     {
1755       /* Check through and see if any of the RC's have had their
1756        * mtime modified. If so, reparse everything.
1757        */
1758       tmp_list = context->rc_files;
1759       while (tmp_list)
1760         {
1761           rc_file = tmp_list->data;
1762
1763           if (!rc_file->is_string)
1764             {
1765               if (!g_lstat (rc_file->name, &statbuf) && 
1766                   (statbuf.st_mtime != rc_file->mtime))
1767                 {
1768                   mtime_modified = TRUE;
1769                   break;
1770                 }
1771             }
1772           
1773           tmp_list = tmp_list->next;
1774         }
1775     }
1776       
1777   if (force_load || mtime_modified)
1778     {
1779       _gtk_binding_reset_parsed ();
1780       gtk_rc_clear_styles (context);
1781       context->reloading = TRUE;
1782
1783       _gtk_settings_reset_rc_values (context->settings);
1784       gtk_rc_clear_rc_files (context);
1785
1786       gtk_rc_parse_default_files (context);
1787
1788       tmp_list = global_rc_files;
1789       while (tmp_list)
1790         {
1791           rc_file = tmp_list->data;
1792
1793           if (rc_file->is_string)
1794             gtk_rc_context_parse_string (context, rc_file->name);
1795           else
1796             gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1797
1798           tmp_list = tmp_list->next;
1799         }
1800
1801       g_free (context->theme_name);
1802       g_free (context->key_theme_name);
1803
1804       g_object_get (context->settings,
1805                     "gtk-theme-name", &context->theme_name,
1806                     "gtk-key-theme-name", &context->key_theme_name,
1807                     NULL);
1808
1809       if (context->theme_name && context->theme_name[0])
1810         gtk_rc_parse_named (context, context->theme_name, NULL);
1811       if (context->key_theme_name && context->key_theme_name[0])
1812         gtk_rc_parse_named (context, context->key_theme_name, "key");
1813
1814       context->reloading = FALSE;
1815
1816       gtk_rc_reset_widgets (context->settings);
1817     }
1818
1819   return force_load || mtime_modified;
1820 }
1821
1822 /**
1823  * gtk_rc_reparse_all:
1824  * 
1825  * If the modification time on any previously read file for the
1826  * default #GtkSettings has changed, discard all style information
1827  * and then reread all previously read RC files.
1828  * 
1829  * Return value:  %TRUE if the files were reread.
1830  **/
1831 gboolean
1832 gtk_rc_reparse_all (void)
1833 {
1834   GSList *tmp_list;
1835   gboolean result = FALSE;
1836
1837   for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1838     {
1839       GtkRcContext *context = tmp_list->data;
1840       if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1841         result = TRUE;
1842     }
1843
1844   return result;
1845 }
1846
1847 static GSList *
1848 gtk_rc_styles_match (GSList       *rc_styles,
1849                      GSList       *sets,
1850                      guint         path_length,
1851                      gchar        *path,
1852                      gchar        *path_reversed)
1853                      
1854 {
1855   GtkRcSet *rc_set;
1856
1857   while (sets)
1858     {
1859       rc_set = sets->data;
1860       sets = sets->next;
1861
1862       if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1863         {
1864           if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1865             rc_styles = g_slist_append (rc_styles, rc_set);
1866         }
1867       else
1868         {
1869           if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1870             rc_styles = g_slist_append (rc_styles, rc_set);
1871         }
1872     }
1873
1874   return rc_styles;
1875 }
1876
1877 static gint
1878 rc_set_compare (gconstpointer a, gconstpointer b)
1879 {
1880   const GtkRcSet *set_a = a;
1881   const GtkRcSet *set_b = b;
1882
1883   return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1884 }
1885
1886 static GSList *
1887 sort_and_dereference_sets (GSList *styles)
1888 {
1889   GSList *tmp_list;
1890   
1891   /* At this point, the list of sets is ordered by:
1892    *
1893    * a) 'widget' patterns are earlier than 'widget_class' patterns
1894    *    which are ealier than 'class' patterns.
1895    * a) For two matches for class patterns, a match to a child type
1896    *    is before a match to a parent type
1897    * c) a match later in the RC file (or in a later RC file) is before a
1898    *    match earlier in the RC file.
1899    *
1900    * With a) taking precedence over b) which takes precendence over c).
1901    *
1902    * Now sort by priority, which has the highest precendence for sort order
1903    */
1904   styles = g_slist_sort (styles, rc_set_compare);
1905
1906   /* Make styles->data = styles->data->rc_style
1907    */
1908   tmp_list = styles;
1909   while (tmp_list)
1910     {
1911       GtkRcSet *set = tmp_list->data;
1912       tmp_list->data = set->rc_style;
1913       tmp_list = tmp_list->next;
1914     }
1915
1916   return styles;
1917 }
1918
1919 /**
1920  * gtk_rc_get_style:
1921  * @widget: a #GtkWidget
1922  * 
1923  * Finds all matching RC styles for a given widget,
1924  * composites them together, and then creates a 
1925  * #GtkStyle representing the composite appearance.
1926  * (GTK+ actually keeps a cache of previously 
1927  * created styles, so a new style may not be
1928  * created.)
1929  * 
1930  * Returns: the resulting style. No refcount is added
1931  *   to the returned style, so if you want to save this
1932  *   style around, you should add a reference yourself.
1933  **/
1934 GtkStyle *
1935 gtk_rc_get_style (GtkWidget *widget)
1936 {
1937   GtkRcStyle *widget_rc_style;
1938   GSList *rc_styles = NULL;
1939   GtkRcContext *context;
1940
1941   static guint rc_style_key_id = 0;
1942
1943   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1944
1945   context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1946
1947   /* We allow the specification of a single rc style to be bound
1948    * tightly to a widget, for application modifications
1949    */
1950   if (!rc_style_key_id)
1951     rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1952
1953   if (context->rc_sets_widget)
1954     {
1955       gchar *path, *path_reversed;
1956       guint path_length;
1957
1958       gtk_widget_path (widget, &path_length, &path, &path_reversed);
1959       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1960       g_free (path);
1961       g_free (path_reversed);
1962     }
1963   
1964   if (context->rc_sets_widget_class)
1965     {
1966       gchar *path, *path_reversed;
1967       guint path_length;
1968
1969       gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1970       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1971       g_free (path);
1972       g_free (path_reversed);
1973     }
1974
1975   if (context->rc_sets_class)
1976     {
1977       GType type;
1978
1979       type = G_TYPE_FROM_INSTANCE (widget);
1980       while (type)
1981         {
1982           gchar *path;
1983           gchar *path_reversed;
1984           guint path_length;
1985
1986           path = g_strdup (g_type_name (type));
1987           path_length = strlen (path);
1988           path_reversed = g_strdup (path);
1989           g_strreverse (path_reversed);
1990           
1991           rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1992           g_free (path);
1993           g_free (path_reversed);
1994       
1995           type = g_type_parent (type);
1996         }
1997     }
1998   
1999   rc_styles = sort_and_dereference_sets (rc_styles);
2000   
2001   widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
2002
2003   if (widget_rc_style)
2004     rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
2005
2006   if (rc_styles)
2007     return gtk_rc_init_style (context, rc_styles);
2008   else
2009     {
2010       if (!context->default_style)
2011         {
2012           context->default_style = gtk_style_new ();
2013           _gtk_style_init_for_settings (context->default_style, context->settings);
2014         }
2015
2016       return context->default_style;
2017     }
2018 }
2019
2020 /**
2021  * gtk_rc_get_style_by_paths:
2022  * @settings: a #GtkSettings object
2023  * @widget_path: the widget path to use when looking up the style, or %NULL
2024  *               if no matching against the widget path should be done
2025  * @class_path: the class path to use when looking up the style, or %NULL
2026  *               if no matching against the class path should be done.
2027  * @type: a type that will be used along with parent types of this type
2028  *        when matching against class styles, or #G_TYPE_NONE
2029  * 
2030  * Creates up a #GtkStyle from styles defined in a RC file by providing
2031  * the raw components used in matching. This function may be useful
2032  * when creating pseudo-widgets that should be themed like widgets but
2033  * don't actually have corresponding GTK+ widgets. An example of this
2034  * would be items inside a GNOME canvas widget.
2035  *
2036  * The action of gtk_rc_get_style() is similar to:
2037  * <informalexample><programlisting>
2038  *  gtk_widget_path (widget, NULL, &amp;path, NULL);
2039  *  gtk_widget_class_path (widget, NULL, &amp;class_path, NULL);
2040  *  gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
2041  *                             G_OBJECT_TYPE (widget));
2042  * </programlisting></informalexample>
2043  * 
2044  * Return value: A style created by matching with the supplied paths,
2045  *   or %NULL if nothing matching was specified and the default style should
2046  *   be used. The returned value is owned by GTK+ as part of an internal cache,
2047  *   so you must call g_object_ref() on the returned value if you want to
2048  *   keep a reference to it.
2049  **/
2050 GtkStyle *
2051 gtk_rc_get_style_by_paths (GtkSettings *settings,
2052                            const char  *widget_path,
2053                            const char  *class_path,
2054                            GType        type)
2055 {
2056   /* We duplicate the code from above to avoid slowing down the above
2057    * by generating paths when we don't need them. I don't know if
2058    * this is really worth it.
2059    */
2060   GSList *rc_styles = NULL;
2061   GtkRcContext *context;
2062
2063   g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2064
2065   context = gtk_rc_context_get (settings);
2066
2067   if (widget_path && context->rc_sets_widget)
2068     {
2069       gchar *path;
2070       gchar *path_reversed;
2071       guint path_length;
2072
2073       path_length = strlen (widget_path);
2074       path = g_strdup (widget_path);
2075       path_reversed = g_strdup (widget_path);
2076       g_strreverse (path_reversed);
2077
2078       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2079       g_free (path);
2080       g_free (path_reversed);
2081     }
2082   
2083   if (class_path && context->rc_sets_widget_class)
2084     {
2085       gchar *path;
2086       gchar *path_reversed;
2087       guint path_length;
2088
2089       path = g_strdup (class_path);
2090       path_length = strlen (class_path);
2091       path_reversed = g_strdup (class_path);
2092       g_strreverse (path_reversed);
2093
2094       rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2095       g_free (path);
2096       g_free (path_reversed);
2097     }
2098
2099   if (type != G_TYPE_NONE && context->rc_sets_class)
2100     {
2101       while (type)
2102         {
2103           gchar *path;
2104           gchar *path_reversed;
2105           guint path_length;
2106
2107           path = g_strdup (g_type_name (type));
2108           path_length = strlen (path);
2109           path_reversed = g_strdup (path);
2110           g_strreverse (path_reversed);
2111           
2112           rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2113           g_free (path);
2114           g_free (path_reversed);
2115       
2116           type = g_type_parent (type);
2117         }
2118     }
2119  
2120   rc_styles = sort_and_dereference_sets (rc_styles);
2121   
2122   if (rc_styles)
2123     return gtk_rc_init_style (context, rc_styles);
2124
2125   return NULL;
2126 }
2127
2128 static GSList *
2129 gtk_rc_add_rc_sets (GSList      *slist,
2130                     GtkRcStyle  *rc_style,
2131                     const gchar *pattern,
2132                     GtkPathType  path_type)
2133 {
2134   GtkRcStyle *new_style;
2135   GtkRcSet *rc_set;
2136   guint i;
2137   
2138   new_style = gtk_rc_style_new ();
2139   *new_style = *rc_style;
2140   new_style->name = g_strdup (rc_style->name);
2141   if (rc_style->font_desc)
2142     new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
2143   
2144   for (i = 0; i < 5; i++)
2145     new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
2146   
2147   rc_set = g_new (GtkRcSet, 1);
2148   rc_set->type = path_type;
2149   
2150   if (path_type == GTK_PATH_WIDGET_CLASS)
2151     {
2152       rc_set->pspec = NULL;
2153       rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
2154     }
2155   else
2156     {
2157       rc_set->pspec = g_pattern_spec_new (pattern);
2158       rc_set->path = NULL;
2159     }
2160   
2161   rc_set->rc_style = rc_style;
2162   
2163   return g_slist_prepend (slist, rc_set);
2164 }
2165
2166 void
2167 gtk_rc_add_widget_name_style (GtkRcStyle  *rc_style,
2168                               const gchar *pattern)
2169 {
2170   GtkRcContext *context;
2171   
2172   g_return_if_fail (rc_style != NULL);
2173   g_return_if_fail (pattern != NULL);
2174
2175   context = gtk_rc_context_get (gtk_settings_get_default ());
2176   
2177   context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern, GTK_PATH_WIDGET);
2178 }
2179
2180 void
2181 gtk_rc_add_widget_class_style (GtkRcStyle  *rc_style,
2182                                const gchar *pattern)
2183 {
2184   GtkRcContext *context;
2185   
2186   g_return_if_fail (rc_style != NULL);
2187   g_return_if_fail (pattern != NULL);
2188
2189   context = gtk_rc_context_get (gtk_settings_get_default ());
2190   
2191   context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern, GTK_PATH_WIDGET_CLASS);
2192 }
2193
2194 void
2195 gtk_rc_add_class_style (GtkRcStyle  *rc_style,
2196                         const gchar *pattern)
2197 {
2198   GtkRcContext *context;
2199   
2200   g_return_if_fail (rc_style != NULL);
2201   g_return_if_fail (pattern != NULL);
2202
2203   context = gtk_rc_context_get (gtk_settings_get_default ());
2204   
2205   context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern, GTK_PATH_CLASS);
2206 }
2207
2208 GScanner*
2209 gtk_rc_scanner_new (void)
2210 {
2211   return g_scanner_new (&gtk_rc_scanner_config);
2212 }
2213
2214 static void
2215 gtk_rc_parse_any (GtkRcContext *context,
2216                   const gchar  *input_name,
2217                   gint          input_fd,
2218                   const gchar  *input_string)
2219 {
2220   GScanner *scanner;
2221   guint    i;
2222   gboolean done;
2223
2224   scanner = gtk_rc_scanner_new ();
2225   
2226   if (input_fd >= 0)
2227     {
2228       g_assert (input_string == NULL);
2229       
2230       g_scanner_input_file (scanner, input_fd);
2231     }
2232   else
2233     {
2234       g_assert (input_string != NULL);
2235       
2236       g_scanner_input_text (scanner, input_string, strlen (input_string));
2237     }
2238   scanner->input_name = input_name;
2239
2240   for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2241     g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2242   done = FALSE;
2243   while (!done)
2244     {
2245       if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2246         done = TRUE;
2247       else
2248         {
2249           guint expected_token;
2250           
2251           expected_token = gtk_rc_parse_statement (context, scanner);
2252
2253           if (expected_token != G_TOKEN_NONE)
2254             {
2255               gchar *symbol_name;
2256               gchar *msg;
2257               
2258               msg = NULL;
2259               symbol_name = NULL;
2260               if (scanner->scope_id == 0)
2261                 {
2262                   /* if we are in scope 0, we know the symbol names
2263                    * that are associated with certain token values.
2264                    * so we look them up to make the error messages
2265                    * more readable.
2266                    */
2267                   if (expected_token > GTK_RC_TOKEN_INVALID &&
2268                       expected_token < GTK_RC_TOKEN_LAST)
2269                     {
2270                       for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2271                         if (symbols[i].token == expected_token)
2272                           msg = symbol_names + symbols[i].name_offset;
2273                       if (msg)
2274                         msg = g_strconcat ("e.g. `", msg, "'", NULL);
2275                     }
2276                   if (scanner->token > GTK_RC_TOKEN_INVALID &&
2277                       scanner->token < GTK_RC_TOKEN_LAST)
2278                     {
2279                       symbol_name = "???";
2280                       for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2281                         if (symbols[i].token == scanner->token)
2282                           symbol_name = symbol_names + symbols[i].name_offset;
2283                     }
2284                 }
2285               g_scanner_unexp_token (scanner,
2286                                      expected_token,
2287                                      NULL,
2288                                      "keyword",
2289                                      symbol_name,
2290                                      msg,
2291                                      TRUE);
2292               g_free (msg);
2293               done = TRUE;
2294             }
2295         }
2296     }
2297   
2298   g_scanner_destroy (scanner);
2299 }
2300
2301 static guint       
2302 gtk_rc_styles_hash (const GSList *rc_styles)
2303 {
2304   guint result;
2305   
2306   result = 0;
2307   while (rc_styles)
2308     {
2309       result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2310       rc_styles = rc_styles->next;
2311     }
2312   
2313   return result;
2314 }
2315
2316 static gboolean
2317 gtk_rc_styles_equal (const GSList *a,
2318                      const GSList *b)
2319 {
2320   while (a && b)
2321     {
2322       if (a->data != b->data)
2323         return FALSE;
2324       a = a->next;
2325       b = b->next;
2326     }
2327   
2328   return (a == b);
2329 }
2330
2331 static guint
2332 gtk_rc_style_hash (const gchar *name)
2333 {
2334   guint result;
2335   
2336   result = 0;
2337   while (*name)
2338     result += (result << 3) + *name++;
2339   
2340   return result;
2341 }
2342
2343 static gboolean
2344 gtk_rc_style_equal (const gchar *a,
2345                     const gchar *b)
2346 {
2347   return (strcmp (a, b) == 0);
2348 }
2349
2350 static GtkRcStyle*
2351 gtk_rc_style_find (GtkRcContext *context,
2352                    const gchar  *name)
2353 {
2354   if (context->rc_style_ht)
2355     return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2356   else
2357     return NULL;
2358 }
2359
2360 static GtkStyle *
2361 gtk_rc_style_to_style (GtkRcContext *context,
2362                        GtkRcStyle   *rc_style)
2363 {
2364   GtkStyle *style;
2365
2366   style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2367   _gtk_style_init_for_settings (style, context->settings);
2368
2369   style->rc_style = g_object_ref (rc_style);
2370   
2371   GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);  
2372
2373   return style;
2374 }
2375
2376 /* Reuses or frees rc_styles */
2377 static GtkStyle *
2378 gtk_rc_init_style (GtkRcContext *context,
2379                    GSList       *rc_styles)
2380 {
2381   GtkStyle *style = NULL;
2382   gint i;
2383
2384   g_return_val_if_fail (rc_styles != NULL, NULL);
2385   
2386   if (!realized_style_ht)
2387     realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2388  (GEqualFunc) gtk_rc_styles_equal);
2389
2390   style = g_hash_table_lookup (realized_style_ht, rc_styles);
2391
2392   if (!style)
2393     {
2394       GtkRcStyle *base_style = NULL;
2395       GtkRcStyle *proto_style;
2396       GtkRcStyleClass *proto_style_class;
2397       GSList *tmp_styles;
2398       GType rc_style_type = GTK_TYPE_RC_STYLE;
2399
2400       /* Find the first style where the RC file specified engine "" {}
2401        * or the first derived style and use that to create the
2402        * merged style. If we only have raw GtkRcStyles, use the first
2403        * style to create the merged style.
2404        */
2405       base_style = rc_styles->data;
2406       tmp_styles = rc_styles;
2407       while (tmp_styles)
2408         {
2409           GtkRcStyle *rc_style = tmp_styles->data;
2410           
2411           if (rc_style->engine_specified ||
2412               G_OBJECT_TYPE (rc_style) != rc_style_type)
2413             {
2414               base_style = rc_style;
2415               break;
2416             }
2417           
2418           tmp_styles = tmp_styles->next;
2419         }
2420       
2421       proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2422       proto_style = proto_style_class->create_rc_style (base_style);
2423
2424       tmp_styles = rc_styles;
2425       while (tmp_styles)
2426         {
2427           GtkRcStyle *rc_style = tmp_styles->data;
2428
2429           proto_style_class->merge (proto_style, rc_style);       
2430           
2431           /* Point from each rc_style to the list of styles */
2432           if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2433             rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2434
2435           gtk_rc_style_append_icon_factories (proto_style, rc_style);
2436           gtk_rc_style_append_color_hashes (proto_style, rc_style);
2437
2438           tmp_styles = tmp_styles->next;
2439         }
2440
2441       for (i = 0; i < 5; i++)
2442         if (proto_style->bg_pixmap_name[i] &&
2443             (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2444           {
2445             g_free (proto_style->bg_pixmap_name[i]);
2446             proto_style->bg_pixmap_name[i] = NULL;
2447           }
2448
2449       style = gtk_rc_style_to_style (context, proto_style);
2450       gtk_rc_style_unref (proto_style);
2451
2452       g_hash_table_insert (realized_style_ht, rc_styles, style);
2453     }
2454   else
2455     g_slist_free (rc_styles);
2456
2457   return style;
2458 }
2459
2460 /*********************
2461  * Parsing functions *
2462  *********************/
2463
2464 static gboolean
2465 lookup_color (GtkRcStyle *style,
2466               const char *color_name,
2467               GdkColor   *color)
2468 {
2469   GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2470   GSList *iter;
2471
2472   for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2473     {
2474       GHashTable *hash  = iter->data;
2475       GdkColor   *match = g_hash_table_lookup (hash, color_name);
2476
2477       if (match)
2478         {
2479           color->red = match->red;
2480           color->green = match->green;
2481           color->blue = match->blue;
2482           return TRUE;
2483         }
2484     }
2485
2486   return FALSE;
2487 }
2488
2489 static guint
2490 rc_parse_token_or_compound (GScanner   *scanner,
2491                             GtkRcStyle *style,
2492                             GString    *gstring,
2493                             GTokenType  delimiter)
2494 {
2495   guint token = g_scanner_get_next_token (scanner);
2496
2497   /* we either scan a single token (skipping comments)
2498    * or a compund statement.
2499    * compunds are enclosed in (), [] or {} braces, we read
2500    * them in via deep recursion.
2501    */
2502
2503   switch (token)
2504     {
2505       gchar *string;
2506     case G_TOKEN_INT:
2507       g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2508       break;
2509     case G_TOKEN_FLOAT:
2510       g_string_append_printf (gstring, " %f", scanner->value.v_float);
2511       break;
2512     case G_TOKEN_STRING:
2513       string = g_strescape (scanner->value.v_string, NULL);
2514       g_string_append (gstring, " \"");
2515       g_string_append (gstring, string);
2516       g_string_append_c (gstring, '"');
2517       g_free (string);
2518       break;
2519     case G_TOKEN_IDENTIFIER:
2520       g_string_append_c (gstring, ' ');
2521       g_string_append (gstring, scanner->value.v_identifier);
2522       break;
2523     case G_TOKEN_COMMENT_SINGLE:
2524     case G_TOKEN_COMMENT_MULTI:
2525       return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2526     case G_TOKEN_LEFT_PAREN:
2527       g_string_append_c (gstring, ' ');
2528       g_string_append_c (gstring, token);
2529       token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2530       if (token != G_TOKEN_NONE)
2531         return token;
2532       break;
2533     case G_TOKEN_LEFT_CURLY:
2534       g_string_append_c (gstring, ' ');
2535       g_string_append_c (gstring, token);
2536       token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2537       if (token != G_TOKEN_NONE)
2538         return token;
2539       break;
2540     case G_TOKEN_LEFT_BRACE:
2541       g_string_append_c (gstring, ' ');
2542       g_string_append_c (gstring, token);
2543       token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2544       if (token != G_TOKEN_NONE)
2545         return token;
2546       break;
2547     case '@':
2548       if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2549         {
2550           GdkColor color;
2551           gchar    rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2552           gchar    gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2553           gchar    bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2554
2555           g_scanner_get_next_token (scanner);
2556
2557           if (!style || !lookup_color (style, scanner->value.v_identifier,
2558                                        &color))
2559             {
2560               g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2561                               scanner->value.v_identifier);
2562               return G_TOKEN_IDENTIFIER;
2563             }
2564
2565
2566           g_string_append_printf (gstring, " { %s, %s, %s }",
2567                                   g_ascii_formatd (rbuf, sizeof (rbuf),
2568                                                    "%0.4f",
2569                                                    color.red / 65535.0),
2570                                   g_ascii_formatd (gbuf, sizeof (gbuf),
2571                                                    "%0.4f",
2572                                                    color.green / 65535.0),
2573                                   g_ascii_formatd (bbuf, sizeof (bbuf),
2574                                                    "%0.4f",
2575                                                    color.blue / 65535.0));
2576           break;
2577         }
2578       else
2579         return G_TOKEN_IDENTIFIER;
2580     default:
2581       if (token >= 256 || token < 1)
2582         return delimiter ? delimiter : G_TOKEN_STRING;
2583       g_string_append_c (gstring, ' ');
2584       g_string_append_c (gstring, token);
2585       if (token == delimiter)
2586         return G_TOKEN_NONE;
2587       break;
2588     }
2589   if (!delimiter)
2590     return G_TOKEN_NONE;
2591   else
2592     return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2593 }
2594
2595 static guint
2596 gtk_rc_parse_assignment (GScanner      *scanner,
2597                          GtkRcStyle    *style,
2598                          GtkRcProperty *prop)
2599 {
2600 #define MY_SCAN_IDENTIFIER      TRUE
2601 #define MY_SCAN_SYMBOLS         FALSE
2602 #define MY_IDENTIFIER_2_STRING  FALSE
2603 #define MY_CHAR_2_TOKEN         TRUE
2604 #define MY_SCAN_IDENTIFIER_NULL FALSE
2605 #define MY_NUMBERS_2_INT        TRUE
2606
2607   gboolean scan_identifier      = scanner->config->scan_identifier;
2608   gboolean scan_symbols         = scanner->config->scan_symbols;
2609   gboolean identifier_2_string  = scanner->config->identifier_2_string;
2610   gboolean char_2_token         = scanner->config->char_2_token;
2611   gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2612   gboolean numbers_2_int        = scanner->config->numbers_2_int;
2613   gboolean negate = FALSE;
2614   gboolean is_color = FALSE;
2615   guint    token;
2616
2617   /* check that this is an assignment */
2618   if (g_scanner_get_next_token (scanner) != '=')
2619     return '=';
2620
2621   /* adjust scanner mode */
2622   scanner->config->scan_identifier      = MY_SCAN_IDENTIFIER;
2623   scanner->config->scan_symbols         = MY_SCAN_SYMBOLS;
2624   scanner->config->identifier_2_string  = MY_IDENTIFIER_2_STRING;
2625   scanner->config->char_2_token         = MY_CHAR_2_TOKEN;
2626   scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2627   scanner->config->numbers_2_int        = MY_NUMBERS_2_INT;
2628
2629   /* record location */
2630   if (g_getenv ("GTK_DEBUG"))
2631     prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2632   else
2633     prop->origin = NULL;
2634
2635   /* parse optional symbolic color prefix */
2636   if (g_scanner_peek_next_token (scanner) == '@')
2637     {
2638       g_scanner_get_next_token (scanner); /* eat color prefix */
2639       is_color = TRUE;
2640     }
2641
2642   /* parse optional sign */
2643   if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2644     {
2645       g_scanner_get_next_token (scanner); /* eat sign */
2646       negate = TRUE;
2647     }
2648
2649   /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2650    * unparsed compund
2651    */
2652   token = g_scanner_peek_next_token (scanner);
2653
2654   if (is_color && token != G_TOKEN_IDENTIFIER)
2655     {
2656       token = G_TOKEN_IDENTIFIER;
2657       goto out;
2658     }
2659
2660   switch (token)
2661     {
2662     case G_TOKEN_INT:
2663       g_scanner_get_next_token (scanner);
2664       g_value_init (&prop->value, G_TYPE_LONG);
2665       g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2666       token = G_TOKEN_NONE;
2667       break;
2668     case G_TOKEN_FLOAT:
2669       g_scanner_get_next_token (scanner);
2670       g_value_init (&prop->value, G_TYPE_DOUBLE);
2671       g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2672       token = G_TOKEN_NONE;
2673       break;
2674     case G_TOKEN_STRING:
2675       g_scanner_get_next_token (scanner);
2676       if (negate)
2677         token = G_TOKEN_INT;
2678       else
2679         {
2680           g_value_init (&prop->value, G_TYPE_STRING);
2681           g_value_set_string (&prop->value, scanner->value.v_string);
2682           token = G_TOKEN_NONE;
2683         }
2684       break;
2685     case G_TOKEN_IDENTIFIER:
2686       if (is_color)
2687         {
2688           GdkColor  color;
2689           gchar     rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2690           gchar     gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2691           gchar     bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2692           GString  *gstring;
2693
2694           g_scanner_get_next_token (scanner);
2695
2696           if (!style || !lookup_color (style, scanner->value.v_identifier,
2697                                        &color))
2698             {
2699               g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2700                               scanner->value.v_identifier);
2701               token = G_TOKEN_IDENTIFIER;
2702               break;
2703             }
2704
2705           gstring = g_string_new (NULL);
2706
2707           g_string_append_printf (gstring, " { %s, %s, %s }",
2708                                   g_ascii_formatd (rbuf, sizeof (rbuf),
2709                                                    "%0.4f",
2710                                                    color.red / 65535.0),
2711                                   g_ascii_formatd (gbuf, sizeof (gbuf),
2712                                                    "%0.4f",
2713                                                    color.green / 65535.0),
2714                                   g_ascii_formatd (bbuf, sizeof (bbuf),
2715                                                    "%0.4f",
2716                                                    color.blue / 65535.0));
2717
2718           g_value_init (&prop->value, G_TYPE_GSTRING);
2719           g_value_take_boxed (&prop->value, gstring);
2720           token = G_TOKEN_NONE;
2721           break;
2722         }
2723       /* fall through */
2724     case G_TOKEN_LEFT_PAREN:
2725     case G_TOKEN_LEFT_CURLY:
2726     case G_TOKEN_LEFT_BRACE:
2727       if (!negate)
2728         {
2729           GString  *gstring  = g_string_new (NULL);
2730           gboolean  parse_on = TRUE;
2731
2732           /*  allow identifier(foobar) to support color expressions  */
2733           if (token == G_TOKEN_IDENTIFIER)
2734             {
2735               g_scanner_get_next_token (scanner);
2736
2737               g_string_append_c (gstring, ' ');
2738               g_string_append (gstring, scanner->value.v_identifier);
2739
2740               /* temporarily reset scanner mode to default, so we
2741                * don't peek the next token in a mode that only makes
2742                * sense in this function; because if anything but
2743                * G_TOKEN_LEFT_PAREN follows, the next token will be
2744                * parsed by our caller.
2745                *
2746                * FIXME: right fix would be to call g_scanner_unget()
2747                *        but that doesn't exist
2748                */
2749               scanner->config->scan_identifier      = scan_identifier;
2750               scanner->config->scan_symbols         = scan_symbols;
2751               scanner->config->identifier_2_string  = identifier_2_string;
2752               scanner->config->char_2_token         = char_2_token;
2753               scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2754               scanner->config->numbers_2_int        = numbers_2_int;
2755
2756               token = g_scanner_peek_next_token (scanner);
2757
2758               /* restore adjusted scanner mode */
2759               scanner->config->scan_identifier      = MY_SCAN_IDENTIFIER;
2760               scanner->config->scan_symbols         = MY_SCAN_SYMBOLS;
2761               scanner->config->identifier_2_string  = MY_IDENTIFIER_2_STRING;
2762               scanner->config->char_2_token         = MY_CHAR_2_TOKEN;
2763               scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2764               scanner->config->numbers_2_int        = MY_NUMBERS_2_INT;
2765
2766               if (token != G_TOKEN_LEFT_PAREN)
2767                 {
2768                   token = G_TOKEN_NONE;
2769                   parse_on = FALSE;
2770                 }
2771             }
2772
2773           if (parse_on)
2774             token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2775
2776           if (token == G_TOKEN_NONE)
2777             {
2778               g_string_append_c (gstring, ' ');
2779               g_value_init (&prop->value, G_TYPE_GSTRING);
2780               g_value_take_boxed (&prop->value, gstring);
2781             }
2782           else
2783             g_string_free (gstring, TRUE);
2784           break;
2785         }
2786       /* fall through */
2787     default:
2788       g_scanner_get_next_token (scanner);
2789       token = G_TOKEN_INT;
2790       break;
2791     }
2792
2793  out:
2794
2795   /* restore scanner mode */
2796   scanner->config->scan_identifier      = scan_identifier;
2797   scanner->config->scan_symbols         = scan_symbols;
2798   scanner->config->identifier_2_string  = identifier_2_string;
2799   scanner->config->char_2_token         = char_2_token;
2800   scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2801   scanner->config->numbers_2_int        = numbers_2_int;
2802
2803   return token;
2804 }
2805
2806 static gboolean
2807 is_c_identifier (const gchar *string)
2808 {
2809   const gchar *p;
2810   gboolean is_varname;
2811
2812   is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2813   for (p = string + 1; *p && is_varname; p++)
2814     is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2815
2816   return is_varname;
2817 }
2818
2819 static void
2820 parse_include_file (GtkRcContext *context,
2821                     GScanner     *scanner,
2822                     const gchar  *filename)
2823 {
2824   char *to_parse = NULL;
2825   
2826   if (g_path_is_absolute (filename))
2827     {
2828       /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2829        * don't print an error in this case.
2830        */
2831       to_parse = g_strdup (filename);
2832     }
2833   else
2834     {
2835       /* if a relative path, we look relative to all the RC files in the
2836        * include stack. We require the file to be found in this case
2837        * so we can give meaningful error messages, and because on reparsing
2838        * non-absolute paths don't make sense.
2839        */
2840       GSList *tmp_list = current_files_stack;
2841       while (tmp_list)
2842         {
2843           GtkRcFile *curfile = tmp_list->data;
2844           gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2845
2846           if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2847             {
2848               to_parse = tmpname;
2849               break;
2850             }
2851
2852           g_free (tmpname);
2853           
2854           tmp_list = tmp_list->next;
2855         }
2856     }
2857
2858   if (to_parse)
2859     {
2860       gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2861       g_free (to_parse);
2862     }
2863   else
2864     {
2865       g_scanner_warn (scanner, 
2866                       _("Unable to find include file: \"%s\""),
2867                       filename);
2868     }
2869
2870 }
2871
2872 static guint
2873 gtk_rc_parse_statement (GtkRcContext *context,
2874                         GScanner     *scanner)
2875 {
2876   guint token;
2877   
2878   token = g_scanner_peek_next_token (scanner);
2879   switch (token)
2880     {
2881     case GTK_RC_TOKEN_INCLUDE:
2882       token = g_scanner_get_next_token (scanner);
2883       if (token != GTK_RC_TOKEN_INCLUDE)
2884         return GTK_RC_TOKEN_INCLUDE;
2885       token = g_scanner_get_next_token (scanner);
2886       if (token != G_TOKEN_STRING)
2887         return G_TOKEN_STRING;
2888       parse_include_file (context, scanner, scanner->value.v_string);
2889       return G_TOKEN_NONE;
2890       
2891     case GTK_RC_TOKEN_STYLE:
2892       return gtk_rc_parse_style (context, scanner);
2893       
2894     case GTK_RC_TOKEN_BINDING:
2895       return _gtk_binding_parse_binding (scanner);
2896       
2897     case GTK_RC_TOKEN_PIXMAP_PATH:
2898       return gtk_rc_parse_pixmap_path (context, scanner);
2899       
2900     case GTK_RC_TOKEN_WIDGET:
2901       return gtk_rc_parse_path_pattern (context, scanner);
2902       
2903     case GTK_RC_TOKEN_WIDGET_CLASS:
2904       return gtk_rc_parse_path_pattern (context, scanner);
2905       
2906     case GTK_RC_TOKEN_CLASS:
2907       return gtk_rc_parse_path_pattern (context, scanner);
2908       
2909     case GTK_RC_TOKEN_MODULE_PATH:
2910       return gtk_rc_parse_module_path (scanner);
2911       
2912     case GTK_RC_TOKEN_IM_MODULE_FILE:
2913       return gtk_rc_parse_im_module_file (scanner);
2914
2915     case G_TOKEN_IDENTIFIER:
2916       if (is_c_identifier (scanner->next_value.v_identifier))
2917         {
2918           GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2919           gchar *name;
2920           
2921           g_scanner_get_next_token (scanner); /* eat identifier */
2922           name = g_strdup (scanner->value.v_identifier);
2923           
2924           token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2925           if (token == G_TOKEN_NONE)
2926             {
2927               GtkSettingsValue svalue;
2928
2929               svalue.origin = prop.origin;
2930               memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2931               g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2932               _gtk_settings_set_property_value_from_rc (context->settings,
2933                                                         name,
2934                                                         &svalue);
2935             }
2936           g_free (prop.origin);
2937           if (G_VALUE_TYPE (&prop.value))
2938             g_value_unset (&prop.value);
2939           g_free (name);
2940           
2941           return token;
2942         }
2943       else
2944         {
2945           g_scanner_get_next_token (scanner);
2946           return G_TOKEN_IDENTIFIER;
2947         }
2948     default:
2949       g_scanner_get_next_token (scanner);
2950       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2951     }
2952 }
2953
2954 static void
2955 fixup_rc_set (GSList     *list,
2956               GtkRcStyle *orig,
2957               GtkRcStyle *new)
2958 {
2959   while (list)
2960     {
2961       GtkRcSet *set = list->data;
2962       if (set->rc_style == orig)
2963         set->rc_style = new;
2964       list = list->next;
2965     }
2966 }
2967
2968 static void
2969 fixup_rc_sets (GtkRcContext *context,
2970                GtkRcStyle   *orig,
2971                GtkRcStyle   *new)
2972 {
2973   fixup_rc_set (context->rc_sets_widget, orig, new);
2974   fixup_rc_set (context->rc_sets_widget_class, orig, new);
2975   fixup_rc_set (context->rc_sets_class, orig, new);
2976 }
2977
2978 static guint
2979 gtk_rc_parse_style (GtkRcContext *context,
2980                     GScanner     *scanner)
2981 {
2982   GtkRcStyle *rc_style;
2983   GtkRcStyle *orig_style;
2984   GtkRcStyle *parent_style = NULL;
2985   GtkRcStylePrivate *rc_priv = NULL;
2986   guint token;
2987   gint i;
2988   GtkIconFactory *our_factory = NULL;
2989   GHashTable *our_hash = NULL;
2990
2991   token = g_scanner_get_next_token (scanner);
2992   if (token != GTK_RC_TOKEN_STYLE)
2993     return GTK_RC_TOKEN_STYLE;
2994   
2995   token = g_scanner_get_next_token (scanner);
2996   if (token != G_TOKEN_STRING)
2997     return G_TOKEN_STRING;
2998   
2999   rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3000   if (rc_style)
3001     orig_style = g_object_ref (rc_style);
3002   else
3003     orig_style = NULL;
3004
3005   if (!rc_style)
3006     {
3007       rc_style = gtk_rc_style_new ();
3008       rc_style->name = g_strdup (scanner->value.v_string);
3009       
3010       for (i = 0; i < 5; i++)
3011         rc_style->bg_pixmap_name[i] = NULL;
3012
3013       for (i = 0; i < 5; i++)
3014         rc_style->color_flags[i] = 0;
3015     }
3016
3017   rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
3018
3019   /* If there's a list, its first member is always the factory belonging
3020    * to this RcStyle
3021    */
3022   if (rc_style->icon_factories)
3023     our_factory = rc_style->icon_factories->data;
3024   if (rc_priv->color_hashes)
3025     our_hash = rc_priv->color_hashes->data;
3026
3027   token = g_scanner_peek_next_token (scanner);
3028   if (token == G_TOKEN_EQUAL_SIGN)
3029     {
3030       token = g_scanner_get_next_token (scanner);
3031       
3032       token = g_scanner_get_next_token (scanner);
3033       if (token != G_TOKEN_STRING)
3034         {
3035           token = G_TOKEN_STRING;
3036           goto err;
3037         }
3038       
3039       parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3040       if (parent_style)
3041         {
3042           for (i = 0; i < 5; i++)
3043             {
3044               rc_style->color_flags[i] = parent_style->color_flags[i];
3045               rc_style->fg[i] = parent_style->fg[i];
3046               rc_style->bg[i] = parent_style->bg[i];
3047               rc_style->text[i] = parent_style->text[i];
3048               rc_style->base[i] = parent_style->base[i];
3049             }
3050
3051           rc_style->xthickness = parent_style->xthickness;
3052           rc_style->ythickness = parent_style->ythickness;
3053           
3054           if (parent_style->font_desc)
3055             {
3056               if (rc_style->font_desc)
3057                 pango_font_description_free (rc_style->font_desc);
3058               rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3059             }
3060
3061           if (parent_style->rc_properties)
3062             {
3063               guint i;
3064
3065               for (i = 0; i < parent_style->rc_properties->len; i++)
3066                 insert_rc_property (rc_style,
3067                                     &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3068                                     TRUE);
3069             }
3070           
3071           for (i = 0; i < 5; i++)
3072             {
3073               g_free (rc_style->bg_pixmap_name[i]);
3074               rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3075             }
3076         }
3077     }
3078
3079   /*  get icon_factories and color_hashes from the parent style;
3080    *  if the parent_style doesn't have color_hashes, initializes
3081    *  the color_hashes with the settings' color scheme (if it exists)
3082    */
3083   gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3084
3085   if (rc_style->icon_factories)
3086     our_factory = rc_style->icon_factories->data;
3087   if (rc_priv->color_hashes)
3088     our_hash = rc_priv->color_hashes->data;
3089
3090   token = g_scanner_get_next_token (scanner);
3091   if (token != G_TOKEN_LEFT_CURLY)
3092     {
3093       token = G_TOKEN_LEFT_CURLY;
3094       goto err;
3095     }
3096   
3097   token = g_scanner_peek_next_token (scanner);
3098   while (token != G_TOKEN_RIGHT_CURLY)
3099     {
3100       switch (token)
3101         {
3102         case GTK_RC_TOKEN_BG:
3103           token = gtk_rc_parse_bg (scanner, rc_style);
3104           break;
3105         case GTK_RC_TOKEN_FG:
3106           token = gtk_rc_parse_fg (scanner, rc_style);
3107           break;
3108         case GTK_RC_TOKEN_TEXT:
3109           token = gtk_rc_parse_text (scanner, rc_style);
3110           break;
3111         case GTK_RC_TOKEN_BASE:
3112           token = gtk_rc_parse_base (scanner, rc_style);
3113           break;
3114         case GTK_RC_TOKEN_XTHICKNESS:
3115           token = gtk_rc_parse_xthickness (scanner, rc_style);
3116           break;
3117         case GTK_RC_TOKEN_YTHICKNESS:
3118           token = gtk_rc_parse_ythickness (scanner, rc_style);
3119           break;
3120         case GTK_RC_TOKEN_BG_PIXMAP:
3121           token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3122           break;
3123         case GTK_RC_TOKEN_FONT:
3124           token = gtk_rc_parse_font (scanner, rc_style);
3125           break;
3126         case GTK_RC_TOKEN_FONTSET:
3127           token = gtk_rc_parse_fontset (scanner, rc_style);
3128           break;
3129         case GTK_RC_TOKEN_FONT_NAME:
3130           token = gtk_rc_parse_font_name (scanner, rc_style);
3131           break;
3132         case GTK_RC_TOKEN_ENGINE:
3133           token = gtk_rc_parse_engine (context, scanner, &rc_style);
3134           break;
3135         case GTK_RC_TOKEN_STOCK:
3136           if (our_factory == NULL)
3137             gtk_rc_style_prepend_empty_icon_factory (rc_style);
3138           our_factory = rc_style->icon_factories->data;
3139           token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3140           break;
3141         case GTK_RC_TOKEN_COLOR:
3142           if (our_hash == NULL)
3143             gtk_rc_style_prepend_empty_color_hash (rc_style);
3144           our_hash = rc_priv->color_hashes->data;
3145           token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3146           break;
3147         case G_TOKEN_IDENTIFIER:
3148           if (is_c_identifier (scanner->next_value.v_identifier) &&
3149               scanner->next_value.v_identifier[0] >= 'A' &&
3150               scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
3151             {
3152               GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3153               gchar *name;
3154
3155               g_scanner_get_next_token (scanner); /* eat type name */
3156               prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3157               if (g_scanner_get_next_token (scanner) != ':' ||
3158                   g_scanner_get_next_token (scanner) != ':')
3159                 {
3160                   token = ':';
3161                   break;
3162                 }
3163               if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3164                   !is_c_identifier (scanner->value.v_identifier))
3165                 {
3166                   token = G_TOKEN_IDENTIFIER;
3167                   break;
3168                 }
3169
3170               /* it's important that we do the same canonification as GParamSpecPool here */
3171               name = g_strdup (scanner->value.v_identifier);
3172               g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3173               prop.property_name = g_quark_from_string (name);
3174               g_free (name);
3175
3176               token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3177               if (token == G_TOKEN_NONE)
3178                 {
3179                   g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3180                   insert_rc_property (rc_style, &prop, TRUE);
3181                 }
3182               
3183               g_free (prop.origin);
3184               if (G_VALUE_TYPE (&prop.value))
3185                 g_value_unset (&prop.value);
3186             }
3187           else
3188             {
3189               g_scanner_get_next_token (scanner);
3190               token = G_TOKEN_IDENTIFIER;
3191             }
3192           break;
3193         default:
3194           g_scanner_get_next_token (scanner);
3195           token = G_TOKEN_RIGHT_CURLY;
3196           break;
3197         }
3198
3199       if (token != G_TOKEN_NONE)
3200         goto err;
3201
3202       token = g_scanner_peek_next_token (scanner);
3203     } /* while (token != G_TOKEN_RIGHT_CURLY) */
3204   
3205   token = g_scanner_get_next_token (scanner);
3206   if (token != G_TOKEN_RIGHT_CURLY)
3207     {
3208       token = G_TOKEN_RIGHT_CURLY;
3209       goto err;
3210     }
3211   
3212   if (rc_style != orig_style)
3213     {
3214       if (!context->rc_style_ht)
3215         context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3216                                                  (GEqualFunc) gtk_rc_style_equal);
3217       
3218       g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3219
3220       /* If we copied the data into a new rc style, fix up references to the old rc style
3221        * in bindings that we have.
3222        */
3223       if (orig_style)
3224         fixup_rc_sets (context, orig_style, rc_style);
3225     }
3226
3227   if (orig_style)
3228     g_object_unref (orig_style);
3229   
3230   return G_TOKEN_NONE;
3231
3232  err:
3233   if (rc_style != orig_style)
3234     gtk_rc_style_unref (rc_style);
3235
3236   if (orig_style)
3237     g_object_unref (orig_style);
3238   
3239   return token;
3240 }
3241
3242 const GtkRcProperty*
3243 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3244                                   GQuark      type_name,
3245                                   GQuark      property_name)
3246 {
3247   GtkRcProperty *node = NULL;
3248
3249   g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3250
3251   if (rc_style->rc_properties)
3252     {
3253       GtkRcProperty key;
3254
3255       key.type_name = type_name;
3256       key.property_name = property_name;
3257
3258       node = bsearch (&key,
3259                       rc_style->rc_properties->data, rc_style->rc_properties->len,
3260                       sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3261     }
3262
3263   return node;
3264 }
3265
3266 static guint
3267 gtk_rc_parse_bg (GScanner   *scanner,
3268                  GtkRcStyle *style)
3269 {
3270   GtkStateType state;
3271   guint token;
3272   
3273   token = g_scanner_get_next_token (scanner);
3274   if (token != GTK_RC_TOKEN_BG)
3275     return GTK_RC_TOKEN_BG;
3276   
3277   token = gtk_rc_parse_state (scanner, &state);
3278   if (token != G_TOKEN_NONE)
3279     return token;
3280   
3281   token = g_scanner_get_next_token (scanner);
3282   if (token != G_TOKEN_EQUAL_SIGN)
3283     return G_TOKEN_EQUAL_SIGN;
3284
3285   style->color_flags[state] |= GTK_RC_BG;
3286   return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3287 }
3288
3289 static guint
3290 gtk_rc_parse_fg (GScanner   *scanner,
3291                  GtkRcStyle *style)
3292 {
3293   GtkStateType state;
3294   guint token;
3295   
3296   token = g_scanner_get_next_token (scanner);
3297   if (token != GTK_RC_TOKEN_FG)
3298     return GTK_RC_TOKEN_FG;
3299   
3300   token = gtk_rc_parse_state (scanner, &state);
3301   if (token != G_TOKEN_NONE)
3302     return token;
3303   
3304   token = g_scanner_get_next_token (scanner);
3305   if (token != G_TOKEN_EQUAL_SIGN)
3306     return G_TOKEN_EQUAL_SIGN;
3307   
3308   style->color_flags[state] |= GTK_RC_FG;
3309   return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3310 }
3311
3312 static guint
3313 gtk_rc_parse_text (GScanner   *scanner,
3314                    GtkRcStyle *style)
3315 {
3316   GtkStateType state;
3317   guint token;
3318   
3319   token = g_scanner_get_next_token (scanner);
3320   if (token != GTK_RC_TOKEN_TEXT)
3321     return GTK_RC_TOKEN_TEXT;
3322   
3323   token = gtk_rc_parse_state (scanner, &state);
3324   if (token != G_TOKEN_NONE)
3325     return token;
3326   
3327   token = g_scanner_get_next_token (scanner);
3328   if (token != G_TOKEN_EQUAL_SIGN)
3329     return G_TOKEN_EQUAL_SIGN;
3330   
3331   style->color_flags[state] |= GTK_RC_TEXT;
3332   return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3333 }
3334
3335 static guint
3336 gtk_rc_parse_base (GScanner   *scanner,
3337                    GtkRcStyle *style)
3338 {
3339   GtkStateType state;
3340   guint token;
3341   
3342   token = g_scanner_get_next_token (scanner);
3343   if (token != GTK_RC_TOKEN_BASE)
3344     return GTK_RC_TOKEN_BASE;
3345   
3346   token = gtk_rc_parse_state (scanner, &state);
3347   if (token != G_TOKEN_NONE)
3348     return token;
3349   
3350   token = g_scanner_get_next_token (scanner);
3351   if (token != G_TOKEN_EQUAL_SIGN)
3352     return G_TOKEN_EQUAL_SIGN;
3353
3354   style->color_flags[state] |= GTK_RC_BASE;
3355   return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3356 }
3357
3358 static guint
3359 gtk_rc_parse_xthickness (GScanner   *scanner,
3360                          GtkRcStyle *style)
3361 {
3362   if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3363     return GTK_RC_TOKEN_XTHICKNESS;
3364
3365   if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3366     return G_TOKEN_EQUAL_SIGN;
3367
3368   if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3369     return G_TOKEN_INT;
3370
3371   style->xthickness = scanner->value.v_int;
3372
3373   return G_TOKEN_NONE;
3374 }
3375
3376 static guint
3377 gtk_rc_parse_ythickness (GScanner   *scanner,
3378                          GtkRcStyle *style)
3379 {
3380   if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3381     return GTK_RC_TOKEN_YTHICKNESS;
3382
3383   if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3384     return G_TOKEN_EQUAL_SIGN;
3385
3386   if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3387     return G_TOKEN_INT;
3388
3389   style->ythickness = scanner->value.v_int;
3390
3391   return G_TOKEN_NONE;
3392 }
3393
3394 static guint
3395 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3396                         GScanner     *scanner,
3397                         GtkRcStyle   *rc_style)
3398 {
3399   GtkStateType state;
3400   guint token;
3401   gchar *pixmap_file;
3402   
3403   token = g_scanner_get_next_token (scanner);
3404   if (token != GTK_RC_TOKEN_BG_PIXMAP)
3405     return GTK_RC_TOKEN_BG_PIXMAP;
3406   
3407   token = gtk_rc_parse_state (scanner, &state);
3408   if (token != G_TOKEN_NONE)
3409     return token;
3410   
3411   token = g_scanner_get_next_token (scanner);
3412   if (token != G_TOKEN_EQUAL_SIGN)
3413     return G_TOKEN_EQUAL_SIGN;
3414   
3415   token = g_scanner_get_next_token (scanner);
3416   if (token != G_TOKEN_STRING)
3417     return G_TOKEN_STRING;
3418   
3419   if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3420       (strcmp (scanner->value.v_string, "<none>") == 0))
3421     pixmap_file = g_strdup (scanner->value.v_string);
3422   else
3423     pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3424                                               scanner, scanner->value.v_string);
3425   
3426   if (pixmap_file)
3427     {
3428       g_free (rc_style->bg_pixmap_name[state]);
3429       rc_style->bg_pixmap_name[state] = pixmap_file;
3430     }
3431   
3432   return G_TOKEN_NONE;
3433 }
3434
3435 static gchar*
3436 gtk_rc_check_pixmap_dir (const gchar *dir, 
3437                          const gchar *pixmap_file)
3438 {
3439   gchar *buf;
3440
3441   buf = g_build_filename (dir, pixmap_file, NULL);
3442
3443   if (g_file_test (buf, G_FILE_TEST_EXISTS))
3444     return buf;
3445    
3446   g_free (buf);
3447  
3448    return NULL;
3449  }
3450
3451 /**
3452  * gtk_rc_find_pixmap_in_path:
3453  * @settings: a #GtkSettings
3454  * @scanner: Scanner used to get line number information for the
3455  *   warning message, or %NULL
3456  * @pixmap_file: name of the pixmap file to locate.
3457  * 
3458  * Looks up a file in pixmap path for the specified #GtkSettings.
3459  * If the file is not found, it outputs a warning message using
3460  * g_warning() and returns %NULL.
3461  *
3462  * Return value: the filename. 
3463  **/
3464 gchar*
3465 gtk_rc_find_pixmap_in_path (GtkSettings  *settings,
3466                             GScanner     *scanner,
3467                             const gchar  *pixmap_file)
3468 {
3469   gint i;
3470   gchar *filename;
3471   GSList *tmp_list;
3472
3473   GtkRcContext *context = gtk_rc_context_get (settings);
3474     
3475   if (context->pixmap_path)
3476     for (i = 0; context->pixmap_path[i] != NULL; i++)
3477       {
3478         filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3479         if (filename)
3480           return filename;
3481       }
3482   
3483   tmp_list = current_files_stack;
3484   while (tmp_list)
3485     {
3486       GtkRcFile *curfile = tmp_list->data;
3487       filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3488       if (filename)
3489         return filename;
3490        
3491       tmp_list = tmp_list->next;
3492     }
3493   
3494   if (scanner)
3495     g_scanner_warn (scanner, 
3496                     _("Unable to locate image file in pixmap_path: \"%s\""),
3497                     pixmap_file);
3498   else
3499     g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3500                pixmap_file);
3501     
3502   return NULL;
3503 }
3504
3505 /**
3506  * gtk_rc_find_module_in_path:
3507  * @module_file: name of a theme engine
3508  * 
3509  * Searches for a theme engine in the GTK+ search path. This function
3510  * is not useful for applications and should not be used.
3511  * 
3512  * Return value: The filename, if found (must be freed with g_free()),
3513  *   otherwise %NULL.
3514  **/
3515 gchar*
3516 gtk_rc_find_module_in_path (const gchar *module_file)
3517 {
3518   return _gtk_find_module (module_file, "engines");
3519 }
3520
3521 static guint
3522 gtk_rc_parse_font (GScanner   *scanner,
3523                    GtkRcStyle *rc_style)
3524 {
3525   guint token;
3526   
3527   token = g_scanner_get_next_token (scanner);
3528   if (token != GTK_RC_TOKEN_FONT)
3529     return GTK_RC_TOKEN_FONT;
3530   
3531   token = g_scanner_get_next_token (scanner);
3532   if (token != G_TOKEN_EQUAL_SIGN)
3533     return G_TOKEN_EQUAL_SIGN;
3534   
3535   token = g_scanner_get_next_token (scanner);
3536   if (token != G_TOKEN_STRING)
3537     return G_TOKEN_STRING;
3538
3539   /* Ignore, do nothing */
3540   
3541   return G_TOKEN_NONE;
3542 }
3543
3544 static guint
3545 gtk_rc_parse_fontset (GScanner   *scanner,
3546                       GtkRcStyle *rc_style)
3547 {
3548   guint token;
3549   
3550   token = g_scanner_get_next_token (scanner);
3551   if (token != GTK_RC_TOKEN_FONTSET)
3552     return GTK_RC_TOKEN_FONTSET;
3553   
3554   token = g_scanner_get_next_token (scanner);
3555   if (token != G_TOKEN_EQUAL_SIGN)
3556     return G_TOKEN_EQUAL_SIGN;
3557   
3558   token = g_scanner_get_next_token (scanner);
3559   if (token != G_TOKEN_STRING)
3560     return G_TOKEN_STRING;
3561
3562   /* Do nothing - silently ignore */
3563   
3564   return G_TOKEN_NONE;
3565 }
3566
3567 static guint
3568 gtk_rc_parse_font_name (GScanner   *scanner,
3569                         GtkRcStyle *rc_style)
3570 {
3571   guint token;
3572   
3573   token = g_scanner_get_next_token (scanner);
3574   if (token != GTK_RC_TOKEN_FONT_NAME)
3575     return GTK_RC_TOKEN_FONT;
3576   
3577   token = g_scanner_get_next_token (scanner);
3578   if (token != G_TOKEN_EQUAL_SIGN)
3579     return G_TOKEN_EQUAL_SIGN;
3580   
3581   token = g_scanner_get_next_token (scanner);
3582   if (token != G_TOKEN_STRING)
3583     return G_TOKEN_STRING;
3584
3585   if (rc_style->font_desc)
3586     pango_font_description_free (rc_style->font_desc);
3587
3588   rc_style->font_desc = 
3589     pango_font_description_from_string (scanner->value.v_string);
3590   
3591   return G_TOKEN_NONE;
3592 }
3593
3594 static guint       
3595 gtk_rc_parse_engine (GtkRcContext *context,
3596                      GScanner     *scanner,
3597                      GtkRcStyle  **rc_style)
3598 {
3599   guint token;
3600   GtkThemeEngine *engine;
3601   guint result = G_TOKEN_NONE;
3602   GtkRcStyle *new_style = NULL;
3603   gboolean parsed_curlies = FALSE;
3604   GtkRcStylePrivate *rc_priv, *new_priv;
3605   
3606   token = g_scanner_get_next_token (scanner);
3607   if (token != GTK_RC_TOKEN_ENGINE)
3608     return GTK_RC_TOKEN_ENGINE;
3609
3610   token = g_scanner_get_next_token (scanner);
3611   if (token != G_TOKEN_STRING)
3612     return G_TOKEN_STRING;
3613
3614   if (!scanner->value.v_string[0])
3615     {
3616       /* Support engine "" {} to mean override to the default engine
3617        */
3618       token = g_scanner_get_next_token (scanner);
3619       if (token != G_TOKEN_LEFT_CURLY)
3620         return G_TOKEN_LEFT_CURLY;
3621       
3622       token = g_scanner_get_next_token (scanner);
3623       if (token != G_TOKEN_RIGHT_CURLY)
3624         return G_TOKEN_RIGHT_CURLY;
3625
3626       parsed_curlies = TRUE;
3627
3628       rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3629
3630       if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3631         {
3632           new_style = gtk_rc_style_new ();
3633           gtk_rc_style_real_merge (new_style, *rc_style);
3634
3635           new_style->name = g_strdup ((*rc_style)->name);
3636
3637           /* take over icon factories and color hashes 
3638            * from the to-be-deleted style
3639            */
3640           new_style->icon_factories = (*rc_style)->icon_factories;
3641           (*rc_style)->icon_factories = NULL;
3642           new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3643           new_priv->color_hashes = rc_priv->color_hashes;
3644           rc_priv->color_hashes = NULL;
3645         }
3646       else
3647         (*rc_style)->engine_specified = TRUE;
3648     }
3649   else
3650     {
3651       engine = gtk_theme_engine_get (scanner->value.v_string);
3652       
3653       token = g_scanner_get_next_token (scanner);
3654       if (token != G_TOKEN_LEFT_CURLY)
3655         return G_TOKEN_LEFT_CURLY;
3656       
3657       if (engine)
3658         {
3659           GtkRcStyleClass *new_class;
3660           
3661           rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3662           new_style = gtk_theme_engine_create_rc_style (engine);
3663           g_type_module_unuse (G_TYPE_MODULE (engine));
3664           
3665           new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3666
3667           new_class->merge (new_style, *rc_style);
3668
3669           new_style->name = g_strdup ((*rc_style)->name);
3670
3671           /* take over icon factories and color hashes 
3672            * from the to-be-deleted style
3673            */
3674           new_style->icon_factories = (*rc_style)->icon_factories;
3675           (*rc_style)->icon_factories = NULL;
3676           new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3677           new_priv->color_hashes = rc_priv->color_hashes;
3678           rc_priv->color_hashes = NULL;
3679           
3680           if (new_class->parse)
3681             {
3682               parsed_curlies = TRUE;
3683               result = new_class->parse (new_style, context->settings, scanner);
3684               
3685               if (result != G_TOKEN_NONE)
3686                 {
3687                   /* copy icon factories and color hashes back
3688                    */
3689                   (*rc_style)->icon_factories = new_style->icon_factories;
3690                   new_style->icon_factories = NULL;
3691                   rc_priv->color_hashes = new_priv->color_hashes;
3692                   new_priv->color_hashes = NULL;
3693
3694                   g_object_unref (new_style);
3695                   new_style = NULL;
3696                 }
3697             }
3698         }
3699     }
3700
3701   if (!parsed_curlies)
3702     {
3703       /* Skip over remainder, looking for nested {}'s
3704        */
3705       guint count = 1;
3706       
3707       result = G_TOKEN_RIGHT_CURLY;
3708       while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3709         {
3710           if (token == G_TOKEN_LEFT_CURLY)
3711             count++;
3712           else if (token == G_TOKEN_RIGHT_CURLY)
3713             count--;
3714           
3715           if (count == 0)
3716             {
3717               result = G_TOKEN_NONE;
3718               break;
3719             }
3720         }
3721     }
3722
3723   if (new_style)
3724     {
3725       new_style->engine_specified = TRUE;
3726
3727       g_object_unref (*rc_style);
3728       *rc_style = new_style;
3729     }
3730
3731   return result;
3732 }
3733
3734 guint
3735 gtk_rc_parse_state (GScanner     *scanner,
3736                     GtkStateType *state)
3737 {
3738   guint old_scope;
3739   guint token;
3740
3741   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3742   g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3743   
3744   /* we don't know where we got called from, so we reset the scope here.
3745    * if we bail out due to errors, we *don't* reset the scope, so the
3746    * error messaging code can make sense of our tokens.
3747    */
3748   old_scope = g_scanner_set_scope (scanner, 0);
3749   
3750   token = g_scanner_get_next_token (scanner);
3751   if (token != G_TOKEN_LEFT_BRACE)
3752     return G_TOKEN_LEFT_BRACE;
3753   
3754   token = g_scanner_get_next_token (scanner);
3755   switch (token)
3756     {
3757     case GTK_RC_TOKEN_ACTIVE:
3758       *state = GTK_STATE_ACTIVE;
3759       break;
3760     case GTK_RC_TOKEN_INSENSITIVE:
3761       *state = GTK_STATE_INSENSITIVE;
3762       break;
3763     case GTK_RC_TOKEN_NORMAL:
3764       *state = GTK_STATE_NORMAL;
3765       break;
3766     case GTK_RC_TOKEN_PRELIGHT:
3767       *state = GTK_STATE_PRELIGHT;
3768       break;
3769     case GTK_RC_TOKEN_SELECTED:
3770       *state = GTK_STATE_SELECTED;
3771       break;
3772     default:
3773       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3774     }
3775   
3776   token = g_scanner_get_next_token (scanner);
3777   if (token != G_TOKEN_RIGHT_BRACE)
3778     return G_TOKEN_RIGHT_BRACE;
3779   
3780   g_scanner_set_scope (scanner, old_scope);
3781
3782   return G_TOKEN_NONE;
3783 }
3784
3785 guint
3786 gtk_rc_parse_priority (GScanner            *scanner,
3787                        GtkPathPriorityType *priority)
3788 {
3789   guint old_scope;
3790   guint token;
3791
3792   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3793   g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3794
3795   /* we don't know where we got called from, so we reset the scope here.
3796    * if we bail out due to errors, we *don't* reset the scope, so the
3797    * error messaging code can make sense of our tokens.
3798    */
3799   old_scope = g_scanner_set_scope (scanner, 0);
3800   
3801   token = g_scanner_get_next_token (scanner);
3802   if (token != ':')
3803     return ':';
3804   
3805   token = g_scanner_get_next_token (scanner);
3806   switch (token)
3807     {
3808     case GTK_RC_TOKEN_LOWEST:
3809       *priority = GTK_PATH_PRIO_LOWEST;
3810       break;
3811     case GTK_RC_TOKEN_GTK:
3812       *priority = GTK_PATH_PRIO_GTK;
3813       break;
3814     case GTK_RC_TOKEN_APPLICATION:
3815       *priority = GTK_PATH_PRIO_APPLICATION;
3816       break;
3817     case GTK_RC_TOKEN_THEME:
3818       *priority = GTK_PATH_PRIO_THEME;
3819       break;
3820     case GTK_RC_TOKEN_RC:
3821       *priority = GTK_PATH_PRIO_RC;
3822       break;
3823     case GTK_RC_TOKEN_HIGHEST:
3824       *priority = GTK_PATH_PRIO_HIGHEST;
3825       break;
3826     default:
3827       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3828     }
3829   
3830   g_scanner_set_scope (scanner, old_scope);
3831
3832   return G_TOKEN_NONE;
3833 }
3834
3835 /**
3836  * gtk_rc_parse_color:
3837  * @scanner: a #GScanner
3838  * @color: a pointer to a #GtkColor structure in which to store the result
3839  *
3840  * Parses a color in the <link linkend="color=format">format</link> expected
3841  * in a RC file. 
3842  *
3843  * Note that theme engines should use gtk_rc_parse_color_full() in 
3844  * order to support symbolic colors.
3845  *
3846  * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3847  *     that was expected but not found
3848  */
3849 guint
3850 gtk_rc_parse_color (GScanner *scanner,
3851                     GdkColor *color)
3852 {
3853   return gtk_rc_parse_color_full (scanner, NULL, color);
3854 }
3855
3856 /**
3857  * gtk_rc_parse_color_full:
3858  * @scanner: a #GScanner
3859  * @style: a #GtkRcStyle, or %NULL
3860  * @color: a pointer to a #GtkColor structure in which to store the result
3861  *
3862  * Parses a color in the <link linkend="color=format">format</link> expected
3863  * in a RC file. If @style is not %NULL, it will be consulted to resolve
3864  * references to symbolic colors.
3865  *
3866  * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3867  *     that was expected but not found
3868  *
3869  * Since: 2.12
3870  */
3871 guint
3872 gtk_rc_parse_color_full (GScanner   *scanner,
3873                          GtkRcStyle *style,
3874                          GdkColor   *color)
3875 {
3876   guint token;
3877
3878   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3879
3880   /* we don't need to set our own scope here, because
3881    * we don't need own symbols
3882    */
3883   
3884   token = g_scanner_get_next_token (scanner);
3885   switch (token)
3886     {
3887       gint token_int;
3888       GdkColor c1, c2;
3889       gboolean negate;
3890       gdouble l;
3891
3892     case G_TOKEN_LEFT_CURLY:
3893       token = g_scanner_get_next_token (scanner);
3894       if (token == G_TOKEN_INT)
3895         token_int = scanner->value.v_int;
3896       else if (token == G_TOKEN_FLOAT)
3897         token_int = scanner->value.v_float * 65535.0;
3898       else
3899         return G_TOKEN_FLOAT;
3900       color->red = CLAMP (token_int, 0, 65535);
3901       
3902       token = g_scanner_get_next_token (scanner);
3903       if (token != G_TOKEN_COMMA)
3904         return G_TOKEN_COMMA;
3905       
3906       token = g_scanner_get_next_token (scanner);
3907       if (token == G_TOKEN_INT)
3908         token_int = scanner->value.v_int;
3909       else if (token == G_TOKEN_FLOAT)
3910         token_int = scanner->value.v_float * 65535.0;
3911       else
3912         return G_TOKEN_FLOAT;
3913       color->green = CLAMP (token_int, 0, 65535);
3914       
3915       token = g_scanner_get_next_token (scanner);
3916       if (token != G_TOKEN_COMMA)
3917         return G_TOKEN_COMMA;
3918       
3919       token = g_scanner_get_next_token (scanner);
3920       if (token == G_TOKEN_INT)
3921         token_int = scanner->value.v_int;
3922       else if (token == G_TOKEN_FLOAT)
3923         token_int = scanner->value.v_float * 65535.0;
3924       else
3925         return G_TOKEN_FLOAT;
3926       color->blue = CLAMP (token_int, 0, 65535);
3927       
3928       token = g_scanner_get_next_token (scanner);
3929       if (token != G_TOKEN_RIGHT_CURLY)
3930         return G_TOKEN_RIGHT_CURLY;
3931       return G_TOKEN_NONE;
3932       
3933     case G_TOKEN_STRING:
3934       if (!gdk_color_parse (scanner->value.v_string, color))
3935         {
3936           g_scanner_warn (scanner, "Invalid color constant '%s'",
3937                           scanner->value.v_string);
3938           return G_TOKEN_STRING;
3939         }
3940       return G_TOKEN_NONE;
3941
3942     case '@':
3943       token = g_scanner_get_next_token (scanner);
3944       if (token != G_TOKEN_IDENTIFIER)
3945         return G_TOKEN_IDENTIFIER;
3946
3947       if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3948         {
3949           g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3950                           scanner->value.v_identifier);
3951           return G_TOKEN_IDENTIFIER;
3952         }
3953
3954       return G_TOKEN_NONE;
3955
3956     case G_TOKEN_IDENTIFIER:
3957       if (strcmp (scanner->value.v_identifier, "mix") == 0)
3958         {
3959           token = g_scanner_get_next_token (scanner);
3960           if (token != G_TOKEN_LEFT_PAREN)
3961             return G_TOKEN_LEFT_PAREN;
3962
3963           negate = FALSE;
3964           if (g_scanner_peek_next_token (scanner) == '-')
3965             {
3966               g_scanner_get_next_token (scanner); /* eat sign */
3967               negate = TRUE;
3968             }
3969
3970           token = g_scanner_get_next_token (scanner);
3971           if (token != G_TOKEN_FLOAT)
3972             return G_TOKEN_FLOAT;
3973
3974           l = negate ? -scanner->value.v_float : scanner->value.v_float;
3975
3976           token = g_scanner_get_next_token (scanner);
3977           if (token != G_TOKEN_COMMA)
3978             return G_TOKEN_COMMA;
3979
3980           token = gtk_rc_parse_color_full (scanner, style, &c1);
3981           if (token != G_TOKEN_NONE)
3982             return token;
3983
3984           token = g_scanner_get_next_token (scanner);
3985           if (token != G_TOKEN_COMMA)
3986             return G_TOKEN_COMMA;
3987
3988           token = gtk_rc_parse_color_full (scanner, style, &c2);
3989           if (token != G_TOKEN_NONE)
3990             return token;
3991
3992           token = g_scanner_get_next_token (scanner);
3993           if (token != G_TOKEN_RIGHT_PAREN)
3994             return G_TOKEN_RIGHT_PAREN;
3995
3996           color->red   = l * c1.red   + (1.0 - l) * c2.red;
3997           color->green = l * c1.green + (1.0 - l) * c2.green;
3998           color->blue  = l * c1.blue  + (1.0 - l) * c2.blue;
3999
4000           return G_TOKEN_NONE;
4001         }
4002       else if (strcmp (scanner->value.v_identifier, "shade") == 0)
4003         {
4004           token = g_scanner_get_next_token (scanner);
4005           if (token != G_TOKEN_LEFT_PAREN)
4006             return G_TOKEN_LEFT_PAREN;
4007
4008           negate = FALSE;
4009           if (g_scanner_peek_next_token (scanner) == '-')
4010             {
4011               g_scanner_get_next_token (scanner); /* eat sign */
4012               negate = TRUE;
4013             }
4014
4015           token = g_scanner_get_next_token (scanner);
4016           if (token != G_TOKEN_FLOAT)
4017             return G_TOKEN_FLOAT;
4018
4019           l = negate ? -scanner->value.v_float : scanner->value.v_float;
4020
4021           token = g_scanner_get_next_token (scanner);
4022           if (token != G_TOKEN_COMMA)
4023             return G_TOKEN_COMMA;
4024
4025           token = gtk_rc_parse_color_full (scanner, style, &c1);
4026           if (token != G_TOKEN_NONE)
4027             return token;
4028
4029           token = g_scanner_get_next_token (scanner);
4030           if (token != G_TOKEN_RIGHT_PAREN)
4031             return G_TOKEN_RIGHT_PAREN;
4032
4033           _gtk_style_shade (&c1, color, l);
4034
4035           return G_TOKEN_NONE;
4036         }
4037       else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4038                strcmp (scanner->value.v_identifier, "darker") == 0)
4039         {
4040           if (scanner->value.v_identifier[0] == 'l')
4041             l = 1.3;
4042           else
4043             l = 0.7;
4044
4045           token = g_scanner_get_next_token (scanner);
4046           if (token != G_TOKEN_LEFT_PAREN)
4047             return G_TOKEN_LEFT_PAREN;
4048
4049           token = gtk_rc_parse_color_full (scanner, style, &c1);
4050           if (token != G_TOKEN_NONE)
4051             return token;
4052
4053           token = g_scanner_get_next_token (scanner);
4054           if (token != G_TOKEN_RIGHT_PAREN)
4055             return G_TOKEN_RIGHT_PAREN;
4056
4057           _gtk_style_shade (&c1, color, l);
4058
4059           return G_TOKEN_NONE;
4060         }
4061       else
4062         return G_TOKEN_IDENTIFIER;
4063
4064     default:
4065       return G_TOKEN_STRING;
4066     }
4067 }
4068
4069 static guint
4070 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4071                           GScanner     *scanner)
4072 {
4073   guint token;
4074   
4075   token = g_scanner_get_next_token (scanner);
4076   if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4077     return GTK_RC_TOKEN_PIXMAP_PATH;
4078   
4079   token = g_scanner_get_next_token (scanner);
4080   if (token != G_TOKEN_STRING)
4081     return G_TOKEN_STRING;
4082   
4083   gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4084   
4085   return G_TOKEN_NONE;
4086 }
4087
4088 static void
4089 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4090                                  GScanner     *scanner,
4091                                  const gchar  *pix_path)
4092 {
4093   g_strfreev (context->pixmap_path);
4094   context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4095 }
4096
4097 static guint
4098 gtk_rc_parse_module_path (GScanner *scanner)
4099 {
4100   guint token;
4101   
4102   token = g_scanner_get_next_token (scanner);
4103   if (token != GTK_RC_TOKEN_MODULE_PATH)
4104     return GTK_RC_TOKEN_MODULE_PATH;
4105   
4106   token = g_scanner_get_next_token (scanner);
4107   if (token != G_TOKEN_STRING)
4108     return G_TOKEN_STRING;
4109
4110   g_warning ("module_path directive is now ignored\n");
4111
4112   return G_TOKEN_NONE;
4113 }
4114
4115 static guint
4116 gtk_rc_parse_im_module_file (GScanner *scanner)
4117 {
4118   guint token;
4119   
4120   token = g_scanner_get_next_token (scanner);
4121   if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4122     return GTK_RC_TOKEN_IM_MODULE_FILE;
4123   
4124   token = g_scanner_get_next_token (scanner);
4125   if (token != G_TOKEN_STRING)
4126     return G_TOKEN_STRING;
4127
4128   g_free (im_module_file);
4129     
4130   im_module_file = g_strdup (scanner->value.v_string);
4131
4132   return G_TOKEN_NONE;
4133 }
4134
4135 static guint
4136 gtk_rc_parse_path_pattern (GtkRcContext *context,
4137                            GScanner     *scanner)
4138 {
4139   guint token;
4140   GtkPathType path_type;
4141   gchar *pattern;
4142   gboolean is_binding;
4143   GtkPathPriorityType priority = context->default_priority;
4144   
4145   token = g_scanner_get_next_token (scanner);
4146   switch (token)
4147     {
4148     case GTK_RC_TOKEN_WIDGET:
4149       path_type = GTK_PATH_WIDGET;
4150       break;
4151     case GTK_RC_TOKEN_WIDGET_CLASS:
4152       path_type = GTK_PATH_WIDGET_CLASS;
4153       break;
4154     case GTK_RC_TOKEN_CLASS:
4155       path_type = GTK_PATH_CLASS;
4156       break;
4157     default:
4158       return GTK_RC_TOKEN_WIDGET_CLASS;
4159     }
4160
4161   token = g_scanner_get_next_token (scanner);
4162   if (token != G_TOKEN_STRING)
4163     return G_TOKEN_STRING;
4164
4165   pattern = g_strdup (scanner->value.v_string);
4166
4167   token = g_scanner_get_next_token (scanner);
4168   if (token == GTK_RC_TOKEN_STYLE)
4169     is_binding = FALSE;
4170   else if (token == GTK_RC_TOKEN_BINDING)
4171     is_binding = TRUE;
4172   else
4173     {
4174       g_free (pattern);
4175       return GTK_RC_TOKEN_STYLE;
4176     }
4177   
4178   if (g_scanner_peek_next_token (scanner) == ':')
4179     {
4180       token = gtk_rc_parse_priority (scanner, &priority);
4181       if (token != G_TOKEN_NONE)
4182         {
4183           g_free (pattern);
4184           return token;
4185         }
4186     }
4187   
4188   token = g_scanner_get_next_token (scanner);
4189   if (token != G_TOKEN_STRING)
4190     {
4191       g_free (pattern);
4192       return G_TOKEN_STRING;
4193     }
4194
4195   if (is_binding)
4196     {
4197       GtkBindingSet *binding;
4198
4199       binding = gtk_binding_set_find (scanner->value.v_string);
4200       if (!binding)
4201         {
4202           g_free (pattern);
4203           return G_TOKEN_STRING;
4204         }
4205       gtk_binding_set_add_path (binding, path_type, pattern, priority);
4206     }
4207   else
4208     {
4209       GtkRcStyle *rc_style;
4210       GtkRcSet *rc_set;
4211
4212       rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4213       
4214       if (!rc_style)
4215         {
4216           g_free (pattern);
4217           return G_TOKEN_STRING;
4218         }
4219
4220       rc_set = g_new (GtkRcSet, 1);
4221       rc_set->type = path_type;
4222       
4223       if (path_type == GTK_PATH_WIDGET_CLASS)
4224         {
4225           rc_set->pspec = NULL;
4226           rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4227         }
4228       else
4229         {
4230           rc_set->pspec = g_pattern_spec_new (pattern);
4231           rc_set->path = NULL;
4232         }
4233       
4234       rc_set->rc_style = rc_style;
4235       rc_set->priority = priority;
4236
4237       if (path_type == GTK_PATH_WIDGET)
4238         context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4239       else if (path_type == GTK_PATH_WIDGET_CLASS)
4240         context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4241       else
4242         context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4243     }
4244
4245   g_free (pattern);
4246   return G_TOKEN_NONE;
4247 }
4248
4249 static guint
4250 gtk_rc_parse_hash_key (GScanner  *scanner,
4251                        gchar    **hash_key)
4252 {
4253   guint token;
4254   
4255   token = g_scanner_get_next_token (scanner);
4256   if (token != G_TOKEN_LEFT_BRACE)
4257     return G_TOKEN_LEFT_BRACE;
4258
4259   token = g_scanner_get_next_token (scanner);
4260   
4261   if (token != G_TOKEN_STRING)
4262     return G_TOKEN_STRING;
4263   
4264   *hash_key = g_strdup (scanner->value.v_string);
4265   
4266   token = g_scanner_get_next_token (scanner);
4267   if (token != G_TOKEN_RIGHT_BRACE)
4268     {
4269       g_free (*hash_key);
4270       return G_TOKEN_RIGHT_BRACE;
4271     }
4272   
4273   return G_TOKEN_NONE;
4274 }
4275
4276 static guint
4277 gtk_rc_parse_icon_source (GtkRcContext   *context,
4278                           GScanner       *scanner,
4279                           GtkIconSet     *icon_set,
4280                           gboolean       *icon_set_valid)
4281 {
4282   guint token;
4283   gchar *full_filename;
4284   GtkIconSource *source = NULL;
4285
4286   token = g_scanner_get_next_token (scanner);
4287   if (token != G_TOKEN_LEFT_CURLY)
4288     return G_TOKEN_LEFT_CURLY;
4289
4290   token = g_scanner_get_next_token (scanner);
4291   
4292   if (token != G_TOKEN_STRING && token != '@')
4293     return G_TOKEN_STRING;
4294   
4295   if (token == G_TOKEN_STRING)
4296     {
4297       /* Filename */
4298
4299       source = gtk_icon_source_new ();      
4300       full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4301       if (full_filename)
4302         {
4303           gtk_icon_source_set_filename (source, full_filename);
4304           g_free (full_filename);
4305         }
4306     }
4307   else
4308     {
4309       /* Icon name */
4310       
4311       token = g_scanner_get_next_token (scanner);
4312   
4313       if (token != G_TOKEN_STRING)
4314         return G_TOKEN_STRING;
4315
4316       source = gtk_icon_source_new ();
4317       gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4318     }
4319
4320   /* We continue parsing even if we didn't find the pixmap so that rest of the
4321    * file is read, even if the syntax is bad. However we don't validate the 
4322    * icon_set so the caller can choose not to install it.
4323    */
4324   token = g_scanner_get_next_token (scanner);
4325
4326   if (token == G_TOKEN_RIGHT_CURLY)
4327     goto done;
4328   else if (token != G_TOKEN_COMMA)
4329     {
4330       gtk_icon_source_free (source);
4331       return G_TOKEN_COMMA;
4332     }
4333
4334   /* Get the direction */
4335   
4336   token = g_scanner_get_next_token (scanner);
4337
4338   switch (token)
4339     {
4340     case GTK_RC_TOKEN_RTL:
4341       gtk_icon_source_set_direction_wildcarded (source, FALSE);
4342       gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4343       break;
4344
4345     case GTK_RC_TOKEN_LTR:
4346       gtk_icon_source_set_direction_wildcarded (source, FALSE);
4347       gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4348       break;
4349       
4350     case '*':
4351       break;
4352       
4353     default:
4354       gtk_icon_source_free (source);
4355       return GTK_RC_TOKEN_RTL;
4356       break;
4357     }
4358
4359   token = g_scanner_get_next_token (scanner);
4360
4361   if (token == G_TOKEN_RIGHT_CURLY)
4362     goto done;
4363   else if (token != G_TOKEN_COMMA)
4364     {
4365       gtk_icon_source_free (source);
4366       return G_TOKEN_COMMA;
4367     }
4368
4369   /* Get the state */
4370   
4371   token = g_scanner_get_next_token (scanner);
4372   
4373   switch (token)
4374     {
4375     case GTK_RC_TOKEN_NORMAL:
4376       gtk_icon_source_set_state_wildcarded (source, FALSE);
4377       gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4378       break;
4379
4380     case GTK_RC_TOKEN_PRELIGHT:
4381       gtk_icon_source_set_state_wildcarded (source, FALSE);
4382       gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4383       break;
4384       
4385
4386     case GTK_RC_TOKEN_INSENSITIVE:
4387       gtk_icon_source_set_state_wildcarded (source, FALSE);
4388       gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4389       break;
4390
4391     case GTK_RC_TOKEN_ACTIVE:
4392       gtk_icon_source_set_state_wildcarded (source, FALSE);
4393       gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4394       break;
4395
4396     case GTK_RC_TOKEN_SELECTED:
4397       gtk_icon_source_set_state_wildcarded (source, FALSE);
4398       gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4399       break;
4400
4401     case '*':
4402       break;
4403       
4404     default:
4405       gtk_icon_source_free (source);
4406       return GTK_RC_TOKEN_PRELIGHT;
4407       break;
4408     }  
4409
4410   token = g_scanner_get_next_token (scanner);
4411
4412   if (token == G_TOKEN_RIGHT_CURLY)
4413     goto done;
4414   else if (token != G_TOKEN_COMMA)
4415     {
4416       gtk_icon_source_free (source);
4417       return G_TOKEN_COMMA;
4418     }
4419   
4420   /* Get the size */
4421   
4422   token = g_scanner_get_next_token (scanner);
4423
4424   if (token != '*')
4425     {
4426       GtkIconSize size;
4427       
4428       if (token != G_TOKEN_STRING)
4429         {
4430           gtk_icon_source_free (source);
4431           return G_TOKEN_STRING;
4432         }
4433
4434       size = gtk_icon_size_from_name (scanner->value.v_string);
4435
4436       if (size != GTK_ICON_SIZE_INVALID)
4437         {
4438           gtk_icon_source_set_size_wildcarded (source, FALSE);
4439           gtk_icon_source_set_size (source, size);
4440         }
4441     }
4442
4443   /* Check the close brace */
4444   
4445   token = g_scanner_get_next_token (scanner);
4446   if (token != G_TOKEN_RIGHT_CURLY)
4447     {
4448       gtk_icon_source_free (source);
4449       return G_TOKEN_RIGHT_CURLY;
4450     }
4451
4452  done:
4453   if (gtk_icon_source_get_filename (source) ||
4454       gtk_icon_source_get_icon_name (source))
4455     {
4456       gtk_icon_set_add_source (icon_set, source);
4457       *icon_set_valid = TRUE;
4458     }
4459   gtk_icon_source_free (source);
4460   
4461   return G_TOKEN_NONE;
4462 }
4463
4464 static guint
4465 gtk_rc_parse_stock (GtkRcContext   *context,
4466                     GScanner       *scanner,
4467                     GtkRcStyle     *rc_style,
4468                     GtkIconFactory *factory)
4469 {
4470   GtkIconSet *icon_set = NULL;
4471   gboolean icon_set_valid = FALSE;
4472   gchar *stock_id = NULL;
4473   guint token;
4474   
4475   token = g_scanner_get_next_token (scanner);
4476   if (token != GTK_RC_TOKEN_STOCK)
4477     return GTK_RC_TOKEN_STOCK;
4478   
4479   token = gtk_rc_parse_hash_key (scanner, &stock_id);
4480   if (token != G_TOKEN_NONE)
4481     return token;
4482   
4483   token = g_scanner_get_next_token (scanner);
4484   if (token != G_TOKEN_EQUAL_SIGN)
4485     {
4486       g_free (stock_id);
4487       return G_TOKEN_EQUAL_SIGN;
4488     }
4489
4490   token = g_scanner_get_next_token (scanner);
4491   if (token != G_TOKEN_LEFT_CURLY)
4492     {
4493       g_free (stock_id);
4494       return G_TOKEN_LEFT_CURLY;
4495     }
4496
4497   token = g_scanner_peek_next_token (scanner);
4498   while (token != G_TOKEN_RIGHT_CURLY)
4499     {
4500       if (icon_set == NULL)
4501         icon_set = gtk_icon_set_new ();
4502       
4503       token = gtk_rc_parse_icon_source (context, 
4504                                         scanner, icon_set, &icon_set_valid);
4505       if (token != G_TOKEN_NONE)
4506         {
4507           g_free (stock_id);
4508           gtk_icon_set_unref (icon_set);
4509           return token;
4510         }
4511
4512       token = g_scanner_get_next_token (scanner);
4513       
4514       if (token != G_TOKEN_COMMA &&
4515           token != G_TOKEN_RIGHT_CURLY)
4516         {
4517           g_free (stock_id);
4518           gtk_icon_set_unref (icon_set);
4519           return G_TOKEN_RIGHT_CURLY;
4520         }
4521     }
4522
4523   if (icon_set)
4524     {
4525       if (icon_set_valid)
4526         gtk_icon_factory_add (factory,
4527                               stock_id,
4528                               icon_set);
4529
4530       gtk_icon_set_unref (icon_set);
4531     }
4532   
4533   g_free (stock_id);
4534
4535   return G_TOKEN_NONE;
4536 }
4537
4538 static guint
4539 gtk_rc_parse_logical_color (GScanner   *scanner,
4540                             GtkRcStyle *rc_style,
4541                             GHashTable *hash)
4542 {
4543   gchar *color_id = NULL;
4544   guint token;
4545   GdkColor color;
4546
4547   token = g_scanner_get_next_token (scanner);
4548   if (token != GTK_RC_TOKEN_COLOR)
4549     return GTK_RC_TOKEN_COLOR;
4550
4551   token = gtk_rc_parse_hash_key (scanner, &color_id);
4552   if (token != G_TOKEN_NONE)
4553     return token;
4554
4555   token = g_scanner_get_next_token (scanner);
4556   if (token != G_TOKEN_EQUAL_SIGN)
4557     {
4558       g_free (color_id);
4559       return G_TOKEN_EQUAL_SIGN;
4560     }
4561
4562   token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4563   if (token != G_TOKEN_NONE)
4564     {
4565       g_free (color_id);
4566       return token;
4567     }
4568
4569   /* Because the hash is created with destroy functions,
4570    * g_hash_table_insert will free any old values for us,
4571    * if a mapping with the specified key already exists.
4572    */
4573   g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4574
4575   return G_TOKEN_NONE;
4576 }
4577
4578
4579 GSList *
4580 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4581 {
4582   GSList *result;
4583   PathElt *path_elt;
4584   const gchar *current;
4585   const gchar *class_start;
4586   const gchar *class_end;
4587   const gchar *pattern_end;
4588   const gchar *pattern_start;
4589   gchar *sub_pattern;
4590
4591   result = NULL;
4592   current = pattern;
4593   while ((class_start = strchr (current, '<')) && 
4594          (class_end = strchr (class_start, '>')))
4595     {
4596       /* Add patterns, but ignore single dots */
4597       if (!(class_start == current || 
4598             (class_start == current + 1 && current[0] == '.')))
4599         {
4600           pattern_end = class_start - 1;
4601           pattern_start = current;
4602           
4603           path_elt = g_new (PathElt, 1);
4604           
4605           sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4606           path_elt->type = PATH_ELT_PSPEC;
4607           path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4608           g_free (sub_pattern);
4609           
4610           result = g_slist_prepend (result, path_elt);
4611         }
4612       
4613       path_elt = g_new (PathElt, 1);
4614       
4615       /* The < > need to be removed from the string. */
4616       sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4617       
4618       path_elt->type = PATH_ELT_UNRESOLVED;
4619       path_elt->elt.class_name = sub_pattern;
4620       
4621       result = g_slist_prepend (result, path_elt);
4622       
4623       current = class_end + 1;
4624     }
4625   
4626   /* Add the rest, if anything is left */
4627   if (strlen (current) > 0)
4628     {
4629       path_elt = g_new (PathElt, 1);
4630       path_elt->type = PATH_ELT_PSPEC;
4631       path_elt->elt.pspec = g_pattern_spec_new (current);
4632       
4633       result = g_slist_prepend (result, path_elt);
4634     }
4635   
4636   return g_slist_reverse (result);
4637 }
4638
4639 static void
4640 free_path_elt (gpointer data, 
4641                gpointer user_data)
4642 {
4643   PathElt *path_elt = data;
4644
4645   switch (path_elt->type)
4646     {
4647     case PATH_ELT_PSPEC:
4648       g_pattern_spec_free (path_elt->elt.pspec);
4649       break;
4650     case PATH_ELT_UNRESOLVED:
4651       g_free (path_elt->elt.class_name);
4652       break;
4653     case PATH_ELT_TYPE:
4654       break;
4655     default:
4656       g_assert_not_reached ();
4657     }
4658
4659   g_free (path_elt);
4660 }
4661
4662 void
4663 _gtk_rc_free_widget_class_path (GSList *list)
4664 {
4665   g_slist_foreach (list, free_path_elt, NULL);
4666   g_slist_free (list);
4667 }
4668
4669 static void
4670 gtk_rc_set_free (GtkRcSet *rc_set)
4671 {
4672   if (rc_set->pspec)
4673     g_pattern_spec_free (rc_set->pspec);
4674
4675   _gtk_rc_free_widget_class_path (rc_set->path);
4676   
4677   g_free (rc_set);
4678 }
4679
4680 static gboolean
4681 match_class (PathElt *path_elt, 
4682              gchar   *type_name)
4683 {
4684   GType type;
4685   
4686   if (path_elt->type == PATH_ELT_UNRESOLVED)
4687     {
4688       type = g_type_from_name (path_elt->elt.class_name);
4689       if (type != G_TYPE_INVALID)
4690         {
4691           g_free (path_elt->elt.class_name);
4692           path_elt->elt.class_type = type;
4693           path_elt->type = PATH_ELT_TYPE;
4694         }
4695       else
4696         return g_str_equal (type_name, path_elt->elt.class_name);
4697     }
4698   
4699   return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4700 }
4701
4702 static gboolean
4703 match_widget_class_recursive (GSList *list, 
4704                               guint   length, 
4705                               gchar  *path, 
4706                               gchar  *path_reversed)
4707 {
4708   PathElt *path_elt;
4709   
4710   /* break out if we cannot match anymore. */
4711   if (list == NULL)
4712     {
4713       if (length > 0)
4714         return FALSE;
4715       else
4716         return TRUE;
4717     }
4718
4719   /* there are two possibilities:
4720    *  1. The next pattern should match the class.
4721    *  2. First normal matching, and then maybe a class */
4722   
4723   path_elt = list->data;
4724
4725   if (path_elt->type != PATH_ELT_PSPEC)
4726     {
4727       gchar *class_start = path;
4728       gchar *class_end;
4729       
4730       /* ignore leading dot */
4731       if (class_start[0] == '.')
4732         class_start++;
4733       class_end = strchr (class_start, '.');
4734
4735       if (class_end == NULL)
4736         {
4737           if (!match_class (path_elt, class_start))
4738             return FALSE;
4739           else
4740             return match_widget_class_recursive (list->next, 0, "", "");
4741         }
4742       else
4743         {
4744           class_end[0] = '\0';
4745           if (!match_class (path_elt, class_start))
4746             {
4747               class_end[0] = '.';
4748               return FALSE;
4749             }
4750           else
4751             {
4752               gboolean result;
4753               gint new_length = length - (class_end - path);
4754               gchar old_char = path_reversed[new_length];
4755               
4756               class_end[0] = '.';
4757               
4758               path_reversed[new_length] = '\0';
4759               result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4760               path_reversed[new_length] = old_char;
4761               
4762               return result;
4763             }
4764         }
4765     }
4766   else
4767     {
4768       PathElt *class_elt;
4769       gchar *class_start;
4770       gchar *class_end;
4771       gboolean result = FALSE;
4772       
4773       /* If there is nothing after this (ie. no class match), 
4774        * just compare the pspec. 
4775        */
4776       if (list->next == NULL)
4777         return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4778       
4779       class_elt = (PathElt *)list->next->data;
4780       g_assert (class_elt->type != PATH_ELT_PSPEC);
4781       
4782       class_start = path;
4783       if (class_start[0] == '.')
4784         class_start++;
4785       
4786       while (TRUE)
4787         {
4788           class_end = strchr (class_start, '.');
4789           
4790           /* It should be cheaper to match the class first. (either the pattern
4791            * is simple, and will match most of the times, or it may be complex
4792            * and matching is slow) 
4793            */
4794           if (class_end == NULL)
4795             {
4796               result = match_class (class_elt, class_start);
4797             }
4798           else
4799             {
4800               class_end[0] = '\0';
4801               result = match_class (class_elt, class_start);
4802               class_end[0] = '.';
4803             }
4804           
4805           if (result)
4806             {
4807               gchar old_char;
4808               result = FALSE;
4809               
4810               /* terminate the string in front of the class. It does not matter
4811                * that the class becomes unusable, because it is not needed 
4812                * inside the recursion 
4813                */
4814               old_char = class_start[0];
4815               class_start[0] = '\0';
4816               
4817               if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4818                 {
4819                   if (class_end != NULL)
4820                     {
4821                       gint new_length = length - (class_end - path);
4822                       gchar path_reversed_char = path_reversed[new_length];
4823                       
4824                       path_reversed[new_length] = '\0';
4825                       
4826                       result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4827                       
4828                       path_reversed[new_length] = path_reversed_char;
4829                     }
4830                   else
4831                     result = match_widget_class_recursive (list->next->next, 0, "", "");
4832                 }
4833                 
4834               class_start[0] = old_char;
4835             }
4836           
4837           if (result)
4838             return TRUE;
4839           
4840           /* get next class in path, or break out */
4841           if (class_end != NULL)
4842             class_start = class_end + 1;
4843           else
4844             return FALSE;
4845         }
4846     }
4847 }
4848
4849 gboolean
4850 _gtk_rc_match_widget_class (GSList  *list,
4851                             gint     length,
4852                             gchar   *path,
4853                             gchar   *path_reversed)
4854 {
4855   return match_widget_class_recursive (list, length, path, path_reversed);
4856 }
4857
4858 #ifdef G_OS_WIN32
4859
4860 /* DLL ABI stability backward compatibility versions */
4861
4862 #undef gtk_rc_add_default_file
4863
4864 void
4865 gtk_rc_add_default_file (const gchar *filename)
4866 {
4867   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4868
4869   gtk_rc_add_default_file_utf8 (utf8_filename);
4870
4871   g_free (utf8_filename);
4872 }
4873
4874 #undef gtk_rc_set_default_files
4875
4876 void
4877 gtk_rc_set_default_files (gchar **filenames)
4878 {
4879   gchar **utf8_filenames;
4880   int n = 0, i;
4881
4882   while (filenames[n++] != NULL)
4883     ;
4884
4885   utf8_filenames = g_new (gchar *, n + 1);
4886
4887   for (i = 0; i < n; i++)
4888     utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4889
4890   utf8_filenames[n] = NULL;
4891
4892   gtk_rc_set_default_files_utf8 (utf8_filenames);
4893
4894   g_strfreev (utf8_filenames);
4895 }
4896
4897 #undef gtk_rc_parse
4898
4899 void
4900 gtk_rc_parse (const gchar *filename)
4901 {
4902   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4903
4904   gtk_rc_parse_utf8 (utf8_filename);
4905
4906   g_free (utf8_filename);
4907 }
4908
4909 #endif
4910
4911 #define __GTK_RC_C__
4912 #include "gtkaliasdef.c"