]> Pileus Git - ~andy/gtk/blob - gtk/gtkrc.c
Accept class names starting with lowercase letters for style property
[~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             {
3150               GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3151               gchar *name;
3152
3153               g_scanner_get_next_token (scanner); /* eat type name */
3154               prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3155               if (g_scanner_get_next_token (scanner) != ':' ||
3156                   g_scanner_get_next_token (scanner) != ':')
3157                 {
3158                   token = ':';
3159                   break;
3160                 }
3161               if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3162                   !is_c_identifier (scanner->value.v_identifier))
3163                 {
3164                   token = G_TOKEN_IDENTIFIER;
3165                   break;
3166                 }
3167
3168               /* it's important that we do the same canonification as GParamSpecPool here */
3169               name = g_strdup (scanner->value.v_identifier);
3170               g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3171               prop.property_name = g_quark_from_string (name);
3172               g_free (name);
3173
3174               token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3175               if (token == G_TOKEN_NONE)
3176                 {
3177                   g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3178                   insert_rc_property (rc_style, &prop, TRUE);
3179                 }
3180               
3181               g_free (prop.origin);
3182               if (G_VALUE_TYPE (&prop.value))
3183                 g_value_unset (&prop.value);
3184             }
3185           else
3186             {
3187               g_scanner_get_next_token (scanner);
3188               token = G_TOKEN_IDENTIFIER;
3189             }
3190           break;
3191         default:
3192           g_scanner_get_next_token (scanner);
3193           token = G_TOKEN_RIGHT_CURLY;
3194           break;
3195         }
3196
3197       if (token != G_TOKEN_NONE)
3198         goto err;
3199
3200       token = g_scanner_peek_next_token (scanner);
3201     } /* while (token != G_TOKEN_RIGHT_CURLY) */
3202   
3203   token = g_scanner_get_next_token (scanner);
3204   if (token != G_TOKEN_RIGHT_CURLY)
3205     {
3206       token = G_TOKEN_RIGHT_CURLY;
3207       goto err;
3208     }
3209   
3210   if (rc_style != orig_style)
3211     {
3212       if (!context->rc_style_ht)
3213         context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3214                                                  (GEqualFunc) gtk_rc_style_equal);
3215       
3216       g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3217
3218       /* If we copied the data into a new rc style, fix up references to the old rc style
3219        * in bindings that we have.
3220        */
3221       if (orig_style)
3222         fixup_rc_sets (context, orig_style, rc_style);
3223     }
3224
3225   if (orig_style)
3226     g_object_unref (orig_style);
3227   
3228   return G_TOKEN_NONE;
3229
3230  err:
3231   if (rc_style != orig_style)
3232     gtk_rc_style_unref (rc_style);
3233
3234   if (orig_style)
3235     g_object_unref (orig_style);
3236   
3237   return token;
3238 }
3239
3240 const GtkRcProperty*
3241 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3242                                   GQuark      type_name,
3243                                   GQuark      property_name)
3244 {
3245   GtkRcProperty *node = NULL;
3246
3247   g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3248
3249   if (rc_style->rc_properties)
3250     {
3251       GtkRcProperty key;
3252
3253       key.type_name = type_name;
3254       key.property_name = property_name;
3255
3256       node = bsearch (&key,
3257                       rc_style->rc_properties->data, rc_style->rc_properties->len,
3258                       sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3259     }
3260
3261   return node;
3262 }
3263
3264 static guint
3265 gtk_rc_parse_bg (GScanner   *scanner,
3266                  GtkRcStyle *style)
3267 {
3268   GtkStateType state;
3269   guint token;
3270   
3271   token = g_scanner_get_next_token (scanner);
3272   if (token != GTK_RC_TOKEN_BG)
3273     return GTK_RC_TOKEN_BG;
3274   
3275   token = gtk_rc_parse_state (scanner, &state);
3276   if (token != G_TOKEN_NONE)
3277     return token;
3278   
3279   token = g_scanner_get_next_token (scanner);
3280   if (token != G_TOKEN_EQUAL_SIGN)
3281     return G_TOKEN_EQUAL_SIGN;
3282
3283   style->color_flags[state] |= GTK_RC_BG;
3284   return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3285 }
3286
3287 static guint
3288 gtk_rc_parse_fg (GScanner   *scanner,
3289                  GtkRcStyle *style)
3290 {
3291   GtkStateType state;
3292   guint token;
3293   
3294   token = g_scanner_get_next_token (scanner);
3295   if (token != GTK_RC_TOKEN_FG)
3296     return GTK_RC_TOKEN_FG;
3297   
3298   token = gtk_rc_parse_state (scanner, &state);
3299   if (token != G_TOKEN_NONE)
3300     return token;
3301   
3302   token = g_scanner_get_next_token (scanner);
3303   if (token != G_TOKEN_EQUAL_SIGN)
3304     return G_TOKEN_EQUAL_SIGN;
3305   
3306   style->color_flags[state] |= GTK_RC_FG;
3307   return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3308 }
3309
3310 static guint
3311 gtk_rc_parse_text (GScanner   *scanner,
3312                    GtkRcStyle *style)
3313 {
3314   GtkStateType state;
3315   guint token;
3316   
3317   token = g_scanner_get_next_token (scanner);
3318   if (token != GTK_RC_TOKEN_TEXT)
3319     return GTK_RC_TOKEN_TEXT;
3320   
3321   token = gtk_rc_parse_state (scanner, &state);
3322   if (token != G_TOKEN_NONE)
3323     return token;
3324   
3325   token = g_scanner_get_next_token (scanner);
3326   if (token != G_TOKEN_EQUAL_SIGN)
3327     return G_TOKEN_EQUAL_SIGN;
3328   
3329   style->color_flags[state] |= GTK_RC_TEXT;
3330   return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3331 }
3332
3333 static guint
3334 gtk_rc_parse_base (GScanner   *scanner,
3335                    GtkRcStyle *style)
3336 {
3337   GtkStateType state;
3338   guint token;
3339   
3340   token = g_scanner_get_next_token (scanner);
3341   if (token != GTK_RC_TOKEN_BASE)
3342     return GTK_RC_TOKEN_BASE;
3343   
3344   token = gtk_rc_parse_state (scanner, &state);
3345   if (token != G_TOKEN_NONE)
3346     return token;
3347   
3348   token = g_scanner_get_next_token (scanner);
3349   if (token != G_TOKEN_EQUAL_SIGN)
3350     return G_TOKEN_EQUAL_SIGN;
3351
3352   style->color_flags[state] |= GTK_RC_BASE;
3353   return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3354 }
3355
3356 static guint
3357 gtk_rc_parse_xthickness (GScanner   *scanner,
3358                          GtkRcStyle *style)
3359 {
3360   if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3361     return GTK_RC_TOKEN_XTHICKNESS;
3362
3363   if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3364     return G_TOKEN_EQUAL_SIGN;
3365
3366   if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3367     return G_TOKEN_INT;
3368
3369   style->xthickness = scanner->value.v_int;
3370
3371   return G_TOKEN_NONE;
3372 }
3373
3374 static guint
3375 gtk_rc_parse_ythickness (GScanner   *scanner,
3376                          GtkRcStyle *style)
3377 {
3378   if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3379     return GTK_RC_TOKEN_YTHICKNESS;
3380
3381   if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3382     return G_TOKEN_EQUAL_SIGN;
3383
3384   if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3385     return G_TOKEN_INT;
3386
3387   style->ythickness = scanner->value.v_int;
3388
3389   return G_TOKEN_NONE;
3390 }
3391
3392 static guint
3393 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3394                         GScanner     *scanner,
3395                         GtkRcStyle   *rc_style)
3396 {
3397   GtkStateType state;
3398   guint token;
3399   gchar *pixmap_file;
3400   
3401   token = g_scanner_get_next_token (scanner);
3402   if (token != GTK_RC_TOKEN_BG_PIXMAP)
3403     return GTK_RC_TOKEN_BG_PIXMAP;
3404   
3405   token = gtk_rc_parse_state (scanner, &state);
3406   if (token != G_TOKEN_NONE)
3407     return token;
3408   
3409   token = g_scanner_get_next_token (scanner);
3410   if (token != G_TOKEN_EQUAL_SIGN)
3411     return G_TOKEN_EQUAL_SIGN;
3412   
3413   token = g_scanner_get_next_token (scanner);
3414   if (token != G_TOKEN_STRING)
3415     return G_TOKEN_STRING;
3416   
3417   if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3418       (strcmp (scanner->value.v_string, "<none>") == 0))
3419     pixmap_file = g_strdup (scanner->value.v_string);
3420   else
3421     pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3422                                               scanner, scanner->value.v_string);
3423   
3424   if (pixmap_file)
3425     {
3426       g_free (rc_style->bg_pixmap_name[state]);
3427       rc_style->bg_pixmap_name[state] = pixmap_file;
3428     }
3429   
3430   return G_TOKEN_NONE;
3431 }
3432
3433 static gchar*
3434 gtk_rc_check_pixmap_dir (const gchar *dir, 
3435                          const gchar *pixmap_file)
3436 {
3437   gchar *buf;
3438
3439   buf = g_build_filename (dir, pixmap_file, NULL);
3440
3441   if (g_file_test (buf, G_FILE_TEST_EXISTS))
3442     return buf;
3443    
3444   g_free (buf);
3445  
3446    return NULL;
3447  }
3448
3449 /**
3450  * gtk_rc_find_pixmap_in_path:
3451  * @settings: a #GtkSettings
3452  * @scanner: Scanner used to get line number information for the
3453  *   warning message, or %NULL
3454  * @pixmap_file: name of the pixmap file to locate.
3455  * 
3456  * Looks up a file in pixmap path for the specified #GtkSettings.
3457  * If the file is not found, it outputs a warning message using
3458  * g_warning() and returns %NULL.
3459  *
3460  * Return value: the filename. 
3461  **/
3462 gchar*
3463 gtk_rc_find_pixmap_in_path (GtkSettings  *settings,
3464                             GScanner     *scanner,
3465                             const gchar  *pixmap_file)
3466 {
3467   gint i;
3468   gchar *filename;
3469   GSList *tmp_list;
3470
3471   GtkRcContext *context = gtk_rc_context_get (settings);
3472     
3473   if (context->pixmap_path)
3474     for (i = 0; context->pixmap_path[i] != NULL; i++)
3475       {
3476         filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3477         if (filename)
3478           return filename;
3479       }
3480   
3481   tmp_list = current_files_stack;
3482   while (tmp_list)
3483     {
3484       GtkRcFile *curfile = tmp_list->data;
3485       filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3486       if (filename)
3487         return filename;
3488        
3489       tmp_list = tmp_list->next;
3490     }
3491   
3492   if (scanner)
3493     g_scanner_warn (scanner, 
3494                     _("Unable to locate image file in pixmap_path: \"%s\""),
3495                     pixmap_file);
3496   else
3497     g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3498                pixmap_file);
3499     
3500   return NULL;
3501 }
3502
3503 /**
3504  * gtk_rc_find_module_in_path:
3505  * @module_file: name of a theme engine
3506  * 
3507  * Searches for a theme engine in the GTK+ search path. This function
3508  * is not useful for applications and should not be used.
3509  * 
3510  * Return value: The filename, if found (must be freed with g_free()),
3511  *   otherwise %NULL.
3512  **/
3513 gchar*
3514 gtk_rc_find_module_in_path (const gchar *module_file)
3515 {
3516   return _gtk_find_module (module_file, "engines");
3517 }
3518
3519 static guint
3520 gtk_rc_parse_font (GScanner   *scanner,
3521                    GtkRcStyle *rc_style)
3522 {
3523   guint token;
3524   
3525   token = g_scanner_get_next_token (scanner);
3526   if (token != GTK_RC_TOKEN_FONT)
3527     return GTK_RC_TOKEN_FONT;
3528   
3529   token = g_scanner_get_next_token (scanner);
3530   if (token != G_TOKEN_EQUAL_SIGN)
3531     return G_TOKEN_EQUAL_SIGN;
3532   
3533   token = g_scanner_get_next_token (scanner);
3534   if (token != G_TOKEN_STRING)
3535     return G_TOKEN_STRING;
3536
3537   /* Ignore, do nothing */
3538   
3539   return G_TOKEN_NONE;
3540 }
3541
3542 static guint
3543 gtk_rc_parse_fontset (GScanner   *scanner,
3544                       GtkRcStyle *rc_style)
3545 {
3546   guint token;
3547   
3548   token = g_scanner_get_next_token (scanner);
3549   if (token != GTK_RC_TOKEN_FONTSET)
3550     return GTK_RC_TOKEN_FONTSET;
3551   
3552   token = g_scanner_get_next_token (scanner);
3553   if (token != G_TOKEN_EQUAL_SIGN)
3554     return G_TOKEN_EQUAL_SIGN;
3555   
3556   token = g_scanner_get_next_token (scanner);
3557   if (token != G_TOKEN_STRING)
3558     return G_TOKEN_STRING;
3559
3560   /* Do nothing - silently ignore */
3561   
3562   return G_TOKEN_NONE;
3563 }
3564
3565 static guint
3566 gtk_rc_parse_font_name (GScanner   *scanner,
3567                         GtkRcStyle *rc_style)
3568 {
3569   guint token;
3570   
3571   token = g_scanner_get_next_token (scanner);
3572   if (token != GTK_RC_TOKEN_FONT_NAME)
3573     return GTK_RC_TOKEN_FONT;
3574   
3575   token = g_scanner_get_next_token (scanner);
3576   if (token != G_TOKEN_EQUAL_SIGN)
3577     return G_TOKEN_EQUAL_SIGN;
3578   
3579   token = g_scanner_get_next_token (scanner);
3580   if (token != G_TOKEN_STRING)
3581     return G_TOKEN_STRING;
3582
3583   if (rc_style->font_desc)
3584     pango_font_description_free (rc_style->font_desc);
3585
3586   rc_style->font_desc = 
3587     pango_font_description_from_string (scanner->value.v_string);
3588   
3589   return G_TOKEN_NONE;
3590 }
3591
3592 static guint       
3593 gtk_rc_parse_engine (GtkRcContext *context,
3594                      GScanner     *scanner,
3595                      GtkRcStyle  **rc_style)
3596 {
3597   guint token;
3598   GtkThemeEngine *engine;
3599   guint result = G_TOKEN_NONE;
3600   GtkRcStyle *new_style = NULL;
3601   gboolean parsed_curlies = FALSE;
3602   GtkRcStylePrivate *rc_priv, *new_priv;
3603   
3604   token = g_scanner_get_next_token (scanner);
3605   if (token != GTK_RC_TOKEN_ENGINE)
3606     return GTK_RC_TOKEN_ENGINE;
3607
3608   token = g_scanner_get_next_token (scanner);
3609   if (token != G_TOKEN_STRING)
3610     return G_TOKEN_STRING;
3611
3612   if (!scanner->value.v_string[0])
3613     {
3614       /* Support engine "" {} to mean override to the default engine
3615        */
3616       token = g_scanner_get_next_token (scanner);
3617       if (token != G_TOKEN_LEFT_CURLY)
3618         return G_TOKEN_LEFT_CURLY;
3619       
3620       token = g_scanner_get_next_token (scanner);
3621       if (token != G_TOKEN_RIGHT_CURLY)
3622         return G_TOKEN_RIGHT_CURLY;
3623
3624       parsed_curlies = TRUE;
3625
3626       rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3627
3628       if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3629         {
3630           new_style = gtk_rc_style_new ();
3631           gtk_rc_style_real_merge (new_style, *rc_style);
3632
3633           new_style->name = g_strdup ((*rc_style)->name);
3634
3635           /* take over icon factories and color hashes 
3636            * from the to-be-deleted style
3637            */
3638           new_style->icon_factories = (*rc_style)->icon_factories;
3639           (*rc_style)->icon_factories = NULL;
3640           new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3641           new_priv->color_hashes = rc_priv->color_hashes;
3642           rc_priv->color_hashes = NULL;
3643         }
3644       else
3645         (*rc_style)->engine_specified = TRUE;
3646     }
3647   else
3648     {
3649       engine = gtk_theme_engine_get (scanner->value.v_string);
3650       
3651       token = g_scanner_get_next_token (scanner);
3652       if (token != G_TOKEN_LEFT_CURLY)
3653         return G_TOKEN_LEFT_CURLY;
3654       
3655       if (engine)
3656         {
3657           GtkRcStyleClass *new_class;
3658           
3659           rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3660           new_style = gtk_theme_engine_create_rc_style (engine);
3661           g_type_module_unuse (G_TYPE_MODULE (engine));
3662           
3663           new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3664
3665           new_class->merge (new_style, *rc_style);
3666
3667           new_style->name = g_strdup ((*rc_style)->name);
3668
3669           /* take over icon factories and color hashes 
3670            * from the to-be-deleted style
3671            */
3672           new_style->icon_factories = (*rc_style)->icon_factories;
3673           (*rc_style)->icon_factories = NULL;
3674           new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3675           new_priv->color_hashes = rc_priv->color_hashes;
3676           rc_priv->color_hashes = NULL;
3677           
3678           if (new_class->parse)
3679             {
3680               parsed_curlies = TRUE;
3681               result = new_class->parse (new_style, context->settings, scanner);
3682               
3683               if (result != G_TOKEN_NONE)
3684                 {
3685                   /* copy icon factories and color hashes back
3686                    */
3687                   (*rc_style)->icon_factories = new_style->icon_factories;
3688                   new_style->icon_factories = NULL;
3689                   rc_priv->color_hashes = new_priv->color_hashes;
3690                   new_priv->color_hashes = NULL;
3691
3692                   g_object_unref (new_style);
3693                   new_style = NULL;
3694                 }
3695             }
3696         }
3697     }
3698
3699   if (!parsed_curlies)
3700     {
3701       /* Skip over remainder, looking for nested {}'s
3702        */
3703       guint count = 1;
3704       
3705       result = G_TOKEN_RIGHT_CURLY;
3706       while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3707         {
3708           if (token == G_TOKEN_LEFT_CURLY)
3709             count++;
3710           else if (token == G_TOKEN_RIGHT_CURLY)
3711             count--;
3712           
3713           if (count == 0)
3714             {
3715               result = G_TOKEN_NONE;
3716               break;
3717             }
3718         }
3719     }
3720
3721   if (new_style)
3722     {
3723       new_style->engine_specified = TRUE;
3724
3725       g_object_unref (*rc_style);
3726       *rc_style = new_style;
3727     }
3728
3729   return result;
3730 }
3731
3732 guint
3733 gtk_rc_parse_state (GScanner     *scanner,
3734                     GtkStateType *state)
3735 {
3736   guint old_scope;
3737   guint token;
3738
3739   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3740   g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3741   
3742   /* we don't know where we got called from, so we reset the scope here.
3743    * if we bail out due to errors, we *don't* reset the scope, so the
3744    * error messaging code can make sense of our tokens.
3745    */
3746   old_scope = g_scanner_set_scope (scanner, 0);
3747   
3748   token = g_scanner_get_next_token (scanner);
3749   if (token != G_TOKEN_LEFT_BRACE)
3750     return G_TOKEN_LEFT_BRACE;
3751   
3752   token = g_scanner_get_next_token (scanner);
3753   switch (token)
3754     {
3755     case GTK_RC_TOKEN_ACTIVE:
3756       *state = GTK_STATE_ACTIVE;
3757       break;
3758     case GTK_RC_TOKEN_INSENSITIVE:
3759       *state = GTK_STATE_INSENSITIVE;
3760       break;
3761     case GTK_RC_TOKEN_NORMAL:
3762       *state = GTK_STATE_NORMAL;
3763       break;
3764     case GTK_RC_TOKEN_PRELIGHT:
3765       *state = GTK_STATE_PRELIGHT;
3766       break;
3767     case GTK_RC_TOKEN_SELECTED:
3768       *state = GTK_STATE_SELECTED;
3769       break;
3770     default:
3771       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3772     }
3773   
3774   token = g_scanner_get_next_token (scanner);
3775   if (token != G_TOKEN_RIGHT_BRACE)
3776     return G_TOKEN_RIGHT_BRACE;
3777   
3778   g_scanner_set_scope (scanner, old_scope);
3779
3780   return G_TOKEN_NONE;
3781 }
3782
3783 guint
3784 gtk_rc_parse_priority (GScanner            *scanner,
3785                        GtkPathPriorityType *priority)
3786 {
3787   guint old_scope;
3788   guint token;
3789
3790   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3791   g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3792
3793   /* we don't know where we got called from, so we reset the scope here.
3794    * if we bail out due to errors, we *don't* reset the scope, so the
3795    * error messaging code can make sense of our tokens.
3796    */
3797   old_scope = g_scanner_set_scope (scanner, 0);
3798   
3799   token = g_scanner_get_next_token (scanner);
3800   if (token != ':')
3801     return ':';
3802   
3803   token = g_scanner_get_next_token (scanner);
3804   switch (token)
3805     {
3806     case GTK_RC_TOKEN_LOWEST:
3807       *priority = GTK_PATH_PRIO_LOWEST;
3808       break;
3809     case GTK_RC_TOKEN_GTK:
3810       *priority = GTK_PATH_PRIO_GTK;
3811       break;
3812     case GTK_RC_TOKEN_APPLICATION:
3813       *priority = GTK_PATH_PRIO_APPLICATION;
3814       break;
3815     case GTK_RC_TOKEN_THEME:
3816       *priority = GTK_PATH_PRIO_THEME;
3817       break;
3818     case GTK_RC_TOKEN_RC:
3819       *priority = GTK_PATH_PRIO_RC;
3820       break;
3821     case GTK_RC_TOKEN_HIGHEST:
3822       *priority = GTK_PATH_PRIO_HIGHEST;
3823       break;
3824     default:
3825       return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3826     }
3827   
3828   g_scanner_set_scope (scanner, old_scope);
3829
3830   return G_TOKEN_NONE;
3831 }
3832
3833 /**
3834  * gtk_rc_parse_color:
3835  * @scanner: a #GScanner
3836  * @color: a pointer to a #GtkColor structure in which to store the result
3837  *
3838  * Parses a color in the <link linkend="color=format">format</link> expected
3839  * in a RC file. 
3840  *
3841  * Note that theme engines should use gtk_rc_parse_color_full() in 
3842  * order to support symbolic colors.
3843  *
3844  * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3845  *     that was expected but not found
3846  */
3847 guint
3848 gtk_rc_parse_color (GScanner *scanner,
3849                     GdkColor *color)
3850 {
3851   return gtk_rc_parse_color_full (scanner, NULL, color);
3852 }
3853
3854 /**
3855  * gtk_rc_parse_color_full:
3856  * @scanner: a #GScanner
3857  * @style: a #GtkRcStyle, or %NULL
3858  * @color: a pointer to a #GtkColor structure in which to store the result
3859  *
3860  * Parses a color in the <link linkend="color=format">format</link> expected
3861  * in a RC file. If @style is not %NULL, it will be consulted to resolve
3862  * references to symbolic colors.
3863  *
3864  * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3865  *     that was expected but not found
3866  *
3867  * Since: 2.12
3868  */
3869 guint
3870 gtk_rc_parse_color_full (GScanner   *scanner,
3871                          GtkRcStyle *style,
3872                          GdkColor   *color)
3873 {
3874   guint token;
3875
3876   g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3877
3878   /* we don't need to set our own scope here, because
3879    * we don't need own symbols
3880    */
3881   
3882   token = g_scanner_get_next_token (scanner);
3883   switch (token)
3884     {
3885       gint token_int;
3886       GdkColor c1, c2;
3887       gboolean negate;
3888       gdouble l;
3889
3890     case G_TOKEN_LEFT_CURLY:
3891       token = g_scanner_get_next_token (scanner);
3892       if (token == G_TOKEN_INT)
3893         token_int = scanner->value.v_int;
3894       else if (token == G_TOKEN_FLOAT)
3895         token_int = scanner->value.v_float * 65535.0;
3896       else
3897         return G_TOKEN_FLOAT;
3898       color->red = CLAMP (token_int, 0, 65535);
3899       
3900       token = g_scanner_get_next_token (scanner);
3901       if (token != G_TOKEN_COMMA)
3902         return G_TOKEN_COMMA;
3903       
3904       token = g_scanner_get_next_token (scanner);
3905       if (token == G_TOKEN_INT)
3906         token_int = scanner->value.v_int;
3907       else if (token == G_TOKEN_FLOAT)
3908         token_int = scanner->value.v_float * 65535.0;
3909       else
3910         return G_TOKEN_FLOAT;
3911       color->green = CLAMP (token_int, 0, 65535);
3912       
3913       token = g_scanner_get_next_token (scanner);
3914       if (token != G_TOKEN_COMMA)
3915         return G_TOKEN_COMMA;
3916       
3917       token = g_scanner_get_next_token (scanner);
3918       if (token == G_TOKEN_INT)
3919         token_int = scanner->value.v_int;
3920       else if (token == G_TOKEN_FLOAT)
3921         token_int = scanner->value.v_float * 65535.0;
3922       else
3923         return G_TOKEN_FLOAT;
3924       color->blue = CLAMP (token_int, 0, 65535);
3925       
3926       token = g_scanner_get_next_token (scanner);
3927       if (token != G_TOKEN_RIGHT_CURLY)
3928         return G_TOKEN_RIGHT_CURLY;
3929       return G_TOKEN_NONE;
3930       
3931     case G_TOKEN_STRING:
3932       if (!gdk_color_parse (scanner->value.v_string, color))
3933         {
3934           g_scanner_warn (scanner, "Invalid color constant '%s'",
3935                           scanner->value.v_string);
3936           return G_TOKEN_STRING;
3937         }
3938       return G_TOKEN_NONE;
3939
3940     case '@':
3941       token = g_scanner_get_next_token (scanner);
3942       if (token != G_TOKEN_IDENTIFIER)
3943         return G_TOKEN_IDENTIFIER;
3944
3945       if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3946         {
3947           g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3948                           scanner->value.v_identifier);
3949           return G_TOKEN_IDENTIFIER;
3950         }
3951
3952       return G_TOKEN_NONE;
3953
3954     case G_TOKEN_IDENTIFIER:
3955       if (strcmp (scanner->value.v_identifier, "mix") == 0)
3956         {
3957           token = g_scanner_get_next_token (scanner);
3958           if (token != G_TOKEN_LEFT_PAREN)
3959             return G_TOKEN_LEFT_PAREN;
3960
3961           negate = FALSE;
3962           if (g_scanner_peek_next_token (scanner) == '-')
3963             {
3964               g_scanner_get_next_token (scanner); /* eat sign */
3965               negate = TRUE;
3966             }
3967
3968           token = g_scanner_get_next_token (scanner);
3969           if (token != G_TOKEN_FLOAT)
3970             return G_TOKEN_FLOAT;
3971
3972           l = negate ? -scanner->value.v_float : scanner->value.v_float;
3973
3974           token = g_scanner_get_next_token (scanner);
3975           if (token != G_TOKEN_COMMA)
3976             return G_TOKEN_COMMA;
3977
3978           token = gtk_rc_parse_color_full (scanner, style, &c1);
3979           if (token != G_TOKEN_NONE)
3980             return token;
3981
3982           token = g_scanner_get_next_token (scanner);
3983           if (token != G_TOKEN_COMMA)
3984             return G_TOKEN_COMMA;
3985
3986           token = gtk_rc_parse_color_full (scanner, style, &c2);
3987           if (token != G_TOKEN_NONE)
3988             return token;
3989
3990           token = g_scanner_get_next_token (scanner);
3991           if (token != G_TOKEN_RIGHT_PAREN)
3992             return G_TOKEN_RIGHT_PAREN;
3993
3994           color->red   = l * c1.red   + (1.0 - l) * c2.red;
3995           color->green = l * c1.green + (1.0 - l) * c2.green;
3996           color->blue  = l * c1.blue  + (1.0 - l) * c2.blue;
3997
3998           return G_TOKEN_NONE;
3999         }
4000       else if (strcmp (scanner->value.v_identifier, "shade") == 0)
4001         {
4002           token = g_scanner_get_next_token (scanner);
4003           if (token != G_TOKEN_LEFT_PAREN)
4004             return G_TOKEN_LEFT_PAREN;
4005
4006           negate = FALSE;
4007           if (g_scanner_peek_next_token (scanner) == '-')
4008             {
4009               g_scanner_get_next_token (scanner); /* eat sign */
4010               negate = TRUE;
4011             }
4012
4013           token = g_scanner_get_next_token (scanner);
4014           if (token != G_TOKEN_FLOAT)
4015             return G_TOKEN_FLOAT;
4016
4017           l = negate ? -scanner->value.v_float : scanner->value.v_float;
4018
4019           token = g_scanner_get_next_token (scanner);
4020           if (token != G_TOKEN_COMMA)
4021             return G_TOKEN_COMMA;
4022
4023           token = gtk_rc_parse_color_full (scanner, style, &c1);
4024           if (token != G_TOKEN_NONE)
4025             return token;
4026
4027           token = g_scanner_get_next_token (scanner);
4028           if (token != G_TOKEN_RIGHT_PAREN)
4029             return G_TOKEN_RIGHT_PAREN;
4030
4031           _gtk_style_shade (&c1, color, l);
4032
4033           return G_TOKEN_NONE;
4034         }
4035       else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4036                strcmp (scanner->value.v_identifier, "darker") == 0)
4037         {
4038           if (scanner->value.v_identifier[0] == 'l')
4039             l = 1.3;
4040           else
4041             l = 0.7;
4042
4043           token = g_scanner_get_next_token (scanner);
4044           if (token != G_TOKEN_LEFT_PAREN)
4045             return G_TOKEN_LEFT_PAREN;
4046
4047           token = gtk_rc_parse_color_full (scanner, style, &c1);
4048           if (token != G_TOKEN_NONE)
4049             return token;
4050
4051           token = g_scanner_get_next_token (scanner);
4052           if (token != G_TOKEN_RIGHT_PAREN)
4053             return G_TOKEN_RIGHT_PAREN;
4054
4055           _gtk_style_shade (&c1, color, l);
4056
4057           return G_TOKEN_NONE;
4058         }
4059       else
4060         return G_TOKEN_IDENTIFIER;
4061
4062     default:
4063       return G_TOKEN_STRING;
4064     }
4065 }
4066
4067 static guint
4068 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4069                           GScanner     *scanner)
4070 {
4071   guint token;
4072   
4073   token = g_scanner_get_next_token (scanner);
4074   if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4075     return GTK_RC_TOKEN_PIXMAP_PATH;
4076   
4077   token = g_scanner_get_next_token (scanner);
4078   if (token != G_TOKEN_STRING)
4079     return G_TOKEN_STRING;
4080   
4081   gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4082   
4083   return G_TOKEN_NONE;
4084 }
4085
4086 static void
4087 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4088                                  GScanner     *scanner,
4089                                  const gchar  *pix_path)
4090 {
4091   g_strfreev (context->pixmap_path);
4092   context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4093 }
4094
4095 static guint
4096 gtk_rc_parse_module_path (GScanner *scanner)
4097 {
4098   guint token;
4099   
4100   token = g_scanner_get_next_token (scanner);
4101   if (token != GTK_RC_TOKEN_MODULE_PATH)
4102     return GTK_RC_TOKEN_MODULE_PATH;
4103   
4104   token = g_scanner_get_next_token (scanner);
4105   if (token != G_TOKEN_STRING)
4106     return G_TOKEN_STRING;
4107
4108   g_warning ("module_path directive is now ignored\n");
4109
4110   return G_TOKEN_NONE;
4111 }
4112
4113 static guint
4114 gtk_rc_parse_im_module_file (GScanner *scanner)
4115 {
4116   guint token;
4117   
4118   token = g_scanner_get_next_token (scanner);
4119   if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4120     return GTK_RC_TOKEN_IM_MODULE_FILE;
4121   
4122   token = g_scanner_get_next_token (scanner);
4123   if (token != G_TOKEN_STRING)
4124     return G_TOKEN_STRING;
4125
4126   g_free (im_module_file);
4127     
4128   im_module_file = g_strdup (scanner->value.v_string);
4129
4130   return G_TOKEN_NONE;
4131 }
4132
4133 static guint
4134 gtk_rc_parse_path_pattern (GtkRcContext *context,
4135                            GScanner     *scanner)
4136 {
4137   guint token;
4138   GtkPathType path_type;
4139   gchar *pattern;
4140   gboolean is_binding;
4141   GtkPathPriorityType priority = context->default_priority;
4142   
4143   token = g_scanner_get_next_token (scanner);
4144   switch (token)
4145     {
4146     case GTK_RC_TOKEN_WIDGET:
4147       path_type = GTK_PATH_WIDGET;
4148       break;
4149     case GTK_RC_TOKEN_WIDGET_CLASS:
4150       path_type = GTK_PATH_WIDGET_CLASS;
4151       break;
4152     case GTK_RC_TOKEN_CLASS:
4153       path_type = GTK_PATH_CLASS;
4154       break;
4155     default:
4156       return GTK_RC_TOKEN_WIDGET_CLASS;
4157     }
4158
4159   token = g_scanner_get_next_token (scanner);
4160   if (token != G_TOKEN_STRING)
4161     return G_TOKEN_STRING;
4162
4163   pattern = g_strdup (scanner->value.v_string);
4164
4165   token = g_scanner_get_next_token (scanner);
4166   if (token == GTK_RC_TOKEN_STYLE)
4167     is_binding = FALSE;
4168   else if (token == GTK_RC_TOKEN_BINDING)
4169     is_binding = TRUE;
4170   else
4171     {
4172       g_free (pattern);
4173       return GTK_RC_TOKEN_STYLE;
4174     }
4175   
4176   if (g_scanner_peek_next_token (scanner) == ':')
4177     {
4178       token = gtk_rc_parse_priority (scanner, &priority);
4179       if (token != G_TOKEN_NONE)
4180         {
4181           g_free (pattern);
4182           return token;
4183         }
4184     }
4185   
4186   token = g_scanner_get_next_token (scanner);
4187   if (token != G_TOKEN_STRING)
4188     {
4189       g_free (pattern);
4190       return G_TOKEN_STRING;
4191     }
4192
4193   if (is_binding)
4194     {
4195       GtkBindingSet *binding;
4196
4197       binding = gtk_binding_set_find (scanner->value.v_string);
4198       if (!binding)
4199         {
4200           g_free (pattern);
4201           return G_TOKEN_STRING;
4202         }
4203       gtk_binding_set_add_path (binding, path_type, pattern, priority);
4204     }
4205   else
4206     {
4207       GtkRcStyle *rc_style;
4208       GtkRcSet *rc_set;
4209
4210       rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4211       
4212       if (!rc_style)
4213         {
4214           g_free (pattern);
4215           return G_TOKEN_STRING;
4216         }
4217
4218       rc_set = g_new (GtkRcSet, 1);
4219       rc_set->type = path_type;
4220       
4221       if (path_type == GTK_PATH_WIDGET_CLASS)
4222         {
4223           rc_set->pspec = NULL;
4224           rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4225         }
4226       else
4227         {
4228           rc_set->pspec = g_pattern_spec_new (pattern);
4229           rc_set->path = NULL;
4230         }
4231       
4232       rc_set->rc_style = rc_style;
4233       rc_set->priority = priority;
4234
4235       if (path_type == GTK_PATH_WIDGET)
4236         context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4237       else if (path_type == GTK_PATH_WIDGET_CLASS)
4238         context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4239       else
4240         context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4241     }
4242
4243   g_free (pattern);
4244   return G_TOKEN_NONE;
4245 }
4246
4247 static guint
4248 gtk_rc_parse_hash_key (GScanner  *scanner,
4249                        gchar    **hash_key)
4250 {
4251   guint token;
4252   
4253   token = g_scanner_get_next_token (scanner);
4254   if (token != G_TOKEN_LEFT_BRACE)
4255     return G_TOKEN_LEFT_BRACE;
4256
4257   token = g_scanner_get_next_token (scanner);
4258   
4259   if (token != G_TOKEN_STRING)
4260     return G_TOKEN_STRING;
4261   
4262   *hash_key = g_strdup (scanner->value.v_string);
4263   
4264   token = g_scanner_get_next_token (scanner);
4265   if (token != G_TOKEN_RIGHT_BRACE)
4266     {
4267       g_free (*hash_key);
4268       return G_TOKEN_RIGHT_BRACE;
4269     }
4270   
4271   return G_TOKEN_NONE;
4272 }
4273
4274 static guint
4275 gtk_rc_parse_icon_source (GtkRcContext   *context,
4276                           GScanner       *scanner,
4277                           GtkIconSet     *icon_set,
4278                           gboolean       *icon_set_valid)
4279 {
4280   guint token;
4281   gchar *full_filename;
4282   GtkIconSource *source = NULL;
4283
4284   token = g_scanner_get_next_token (scanner);
4285   if (token != G_TOKEN_LEFT_CURLY)
4286     return G_TOKEN_LEFT_CURLY;
4287
4288   token = g_scanner_get_next_token (scanner);
4289   
4290   if (token != G_TOKEN_STRING && token != '@')
4291     return G_TOKEN_STRING;
4292   
4293   if (token == G_TOKEN_STRING)
4294     {
4295       /* Filename */
4296
4297       source = gtk_icon_source_new ();      
4298       full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4299       if (full_filename)
4300         {
4301           gtk_icon_source_set_filename (source, full_filename);
4302           g_free (full_filename);
4303         }
4304     }
4305   else
4306     {
4307       /* Icon name */
4308       
4309       token = g_scanner_get_next_token (scanner);
4310   
4311       if (token != G_TOKEN_STRING)
4312         return G_TOKEN_STRING;
4313
4314       source = gtk_icon_source_new ();
4315       gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4316     }
4317
4318   /* We continue parsing even if we didn't find the pixmap so that rest of the
4319    * file is read, even if the syntax is bad. However we don't validate the 
4320    * icon_set so the caller can choose not to install it.
4321    */
4322   token = g_scanner_get_next_token (scanner);
4323
4324   if (token == G_TOKEN_RIGHT_CURLY)
4325     goto done;
4326   else if (token != G_TOKEN_COMMA)
4327     {
4328       gtk_icon_source_free (source);
4329       return G_TOKEN_COMMA;
4330     }
4331
4332   /* Get the direction */
4333   
4334   token = g_scanner_get_next_token (scanner);
4335
4336   switch (token)
4337     {
4338     case GTK_RC_TOKEN_RTL:
4339       gtk_icon_source_set_direction_wildcarded (source, FALSE);
4340       gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4341       break;
4342
4343     case GTK_RC_TOKEN_LTR:
4344       gtk_icon_source_set_direction_wildcarded (source, FALSE);
4345       gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4346       break;
4347       
4348     case '*':
4349       break;
4350       
4351     default:
4352       gtk_icon_source_free (source);
4353       return GTK_RC_TOKEN_RTL;
4354       break;
4355     }
4356
4357   token = g_scanner_get_next_token (scanner);
4358
4359   if (token == G_TOKEN_RIGHT_CURLY)
4360     goto done;
4361   else if (token != G_TOKEN_COMMA)
4362     {
4363       gtk_icon_source_free (source);
4364       return G_TOKEN_COMMA;
4365     }
4366
4367   /* Get the state */
4368   
4369   token = g_scanner_get_next_token (scanner);
4370   
4371   switch (token)
4372     {
4373     case GTK_RC_TOKEN_NORMAL:
4374       gtk_icon_source_set_state_wildcarded (source, FALSE);
4375       gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4376       break;
4377
4378     case GTK_RC_TOKEN_PRELIGHT:
4379       gtk_icon_source_set_state_wildcarded (source, FALSE);
4380       gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4381       break;
4382       
4383
4384     case GTK_RC_TOKEN_INSENSITIVE:
4385       gtk_icon_source_set_state_wildcarded (source, FALSE);
4386       gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4387       break;
4388
4389     case GTK_RC_TOKEN_ACTIVE:
4390       gtk_icon_source_set_state_wildcarded (source, FALSE);
4391       gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4392       break;
4393
4394     case GTK_RC_TOKEN_SELECTED:
4395       gtk_icon_source_set_state_wildcarded (source, FALSE);
4396       gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4397       break;
4398
4399     case '*':
4400       break;
4401       
4402     default:
4403       gtk_icon_source_free (source);
4404       return GTK_RC_TOKEN_PRELIGHT;
4405       break;
4406     }  
4407
4408   token = g_scanner_get_next_token (scanner);
4409
4410   if (token == G_TOKEN_RIGHT_CURLY)
4411     goto done;
4412   else if (token != G_TOKEN_COMMA)
4413     {
4414       gtk_icon_source_free (source);
4415       return G_TOKEN_COMMA;
4416     }
4417   
4418   /* Get the size */
4419   
4420   token = g_scanner_get_next_token (scanner);
4421
4422   if (token != '*')
4423     {
4424       GtkIconSize size;
4425       
4426       if (token != G_TOKEN_STRING)
4427         {
4428           gtk_icon_source_free (source);
4429           return G_TOKEN_STRING;
4430         }
4431
4432       size = gtk_icon_size_from_name (scanner->value.v_string);
4433
4434       if (size != GTK_ICON_SIZE_INVALID)
4435         {
4436           gtk_icon_source_set_size_wildcarded (source, FALSE);
4437           gtk_icon_source_set_size (source, size);
4438         }
4439     }
4440
4441   /* Check the close brace */
4442   
4443   token = g_scanner_get_next_token (scanner);
4444   if (token != G_TOKEN_RIGHT_CURLY)
4445     {
4446       gtk_icon_source_free (source);
4447       return G_TOKEN_RIGHT_CURLY;
4448     }
4449
4450  done:
4451   if (gtk_icon_source_get_filename (source) ||
4452       gtk_icon_source_get_icon_name (source))
4453     {
4454       gtk_icon_set_add_source (icon_set, source);
4455       *icon_set_valid = TRUE;
4456     }
4457   gtk_icon_source_free (source);
4458   
4459   return G_TOKEN_NONE;
4460 }
4461
4462 static guint
4463 gtk_rc_parse_stock (GtkRcContext   *context,
4464                     GScanner       *scanner,
4465                     GtkRcStyle     *rc_style,
4466                     GtkIconFactory *factory)
4467 {
4468   GtkIconSet *icon_set = NULL;
4469   gboolean icon_set_valid = FALSE;
4470   gchar *stock_id = NULL;
4471   guint token;
4472   
4473   token = g_scanner_get_next_token (scanner);
4474   if (token != GTK_RC_TOKEN_STOCK)
4475     return GTK_RC_TOKEN_STOCK;
4476   
4477   token = gtk_rc_parse_hash_key (scanner, &stock_id);
4478   if (token != G_TOKEN_NONE)
4479     return token;
4480   
4481   token = g_scanner_get_next_token (scanner);
4482   if (token != G_TOKEN_EQUAL_SIGN)
4483     {
4484       g_free (stock_id);
4485       return G_TOKEN_EQUAL_SIGN;
4486     }
4487
4488   token = g_scanner_get_next_token (scanner);
4489   if (token != G_TOKEN_LEFT_CURLY)
4490     {
4491       g_free (stock_id);
4492       return G_TOKEN_LEFT_CURLY;
4493     }
4494
4495   token = g_scanner_peek_next_token (scanner);
4496   while (token != G_TOKEN_RIGHT_CURLY)
4497     {
4498       if (icon_set == NULL)
4499         icon_set = gtk_icon_set_new ();
4500       
4501       token = gtk_rc_parse_icon_source (context, 
4502                                         scanner, icon_set, &icon_set_valid);
4503       if (token != G_TOKEN_NONE)
4504         {
4505           g_free (stock_id);
4506           gtk_icon_set_unref (icon_set);
4507           return token;
4508         }
4509
4510       token = g_scanner_get_next_token (scanner);
4511       
4512       if (token != G_TOKEN_COMMA &&
4513           token != G_TOKEN_RIGHT_CURLY)
4514         {
4515           g_free (stock_id);
4516           gtk_icon_set_unref (icon_set);
4517           return G_TOKEN_RIGHT_CURLY;
4518         }
4519     }
4520
4521   if (icon_set)
4522     {
4523       if (icon_set_valid)
4524         gtk_icon_factory_add (factory,
4525                               stock_id,
4526                               icon_set);
4527
4528       gtk_icon_set_unref (icon_set);
4529     }
4530   
4531   g_free (stock_id);
4532
4533   return G_TOKEN_NONE;
4534 }
4535
4536 static guint
4537 gtk_rc_parse_logical_color (GScanner   *scanner,
4538                             GtkRcStyle *rc_style,
4539                             GHashTable *hash)
4540 {
4541   gchar *color_id = NULL;
4542   guint token;
4543   GdkColor color;
4544
4545   token = g_scanner_get_next_token (scanner);
4546   if (token != GTK_RC_TOKEN_COLOR)
4547     return GTK_RC_TOKEN_COLOR;
4548
4549   token = gtk_rc_parse_hash_key (scanner, &color_id);
4550   if (token != G_TOKEN_NONE)
4551     return token;
4552
4553   token = g_scanner_get_next_token (scanner);
4554   if (token != G_TOKEN_EQUAL_SIGN)
4555     {
4556       g_free (color_id);
4557       return G_TOKEN_EQUAL_SIGN;
4558     }
4559
4560   token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4561   if (token != G_TOKEN_NONE)
4562     {
4563       g_free (color_id);
4564       return token;
4565     }
4566
4567   /* Because the hash is created with destroy functions,
4568    * g_hash_table_insert will free any old values for us,
4569    * if a mapping with the specified key already exists.
4570    */
4571   g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4572
4573   return G_TOKEN_NONE;
4574 }
4575
4576
4577 GSList *
4578 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4579 {
4580   GSList *result;
4581   PathElt *path_elt;
4582   const gchar *current;
4583   const gchar *class_start;
4584   const gchar *class_end;
4585   const gchar *pattern_end;
4586   const gchar *pattern_start;
4587   gchar *sub_pattern;
4588
4589   result = NULL;
4590   current = pattern;
4591   while ((class_start = strchr (current, '<')) && 
4592          (class_end = strchr (class_start, '>')))
4593     {
4594       /* Add patterns, but ignore single dots */
4595       if (!(class_start == current || 
4596             (class_start == current + 1 && current[0] == '.')))
4597         {
4598           pattern_end = class_start - 1;
4599           pattern_start = current;
4600           
4601           path_elt = g_new (PathElt, 1);
4602           
4603           sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4604           path_elt->type = PATH_ELT_PSPEC;
4605           path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4606           g_free (sub_pattern);
4607           
4608           result = g_slist_prepend (result, path_elt);
4609         }
4610       
4611       path_elt = g_new (PathElt, 1);
4612       
4613       /* The < > need to be removed from the string. */
4614       sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4615       
4616       path_elt->type = PATH_ELT_UNRESOLVED;
4617       path_elt->elt.class_name = sub_pattern;
4618       
4619       result = g_slist_prepend (result, path_elt);
4620       
4621       current = class_end + 1;
4622     }
4623   
4624   /* Add the rest, if anything is left */
4625   if (strlen (current) > 0)
4626     {
4627       path_elt = g_new (PathElt, 1);
4628       path_elt->type = PATH_ELT_PSPEC;
4629       path_elt->elt.pspec = g_pattern_spec_new (current);
4630       
4631       result = g_slist_prepend (result, path_elt);
4632     }
4633   
4634   return g_slist_reverse (result);
4635 }
4636
4637 static void
4638 free_path_elt (gpointer data, 
4639                gpointer user_data)
4640 {
4641   PathElt *path_elt = data;
4642
4643   switch (path_elt->type)
4644     {
4645     case PATH_ELT_PSPEC:
4646       g_pattern_spec_free (path_elt->elt.pspec);
4647       break;
4648     case PATH_ELT_UNRESOLVED:
4649       g_free (path_elt->elt.class_name);
4650       break;
4651     case PATH_ELT_TYPE:
4652       break;
4653     default:
4654       g_assert_not_reached ();
4655     }
4656
4657   g_free (path_elt);
4658 }
4659
4660 void
4661 _gtk_rc_free_widget_class_path (GSList *list)
4662 {
4663   g_slist_foreach (list, free_path_elt, NULL);
4664   g_slist_free (list);
4665 }
4666
4667 static void
4668 gtk_rc_set_free (GtkRcSet *rc_set)
4669 {
4670   if (rc_set->pspec)
4671     g_pattern_spec_free (rc_set->pspec);
4672
4673   _gtk_rc_free_widget_class_path (rc_set->path);
4674   
4675   g_free (rc_set);
4676 }
4677
4678 static gboolean
4679 match_class (PathElt *path_elt, 
4680              gchar   *type_name)
4681 {
4682   GType type;
4683   
4684   if (path_elt->type == PATH_ELT_UNRESOLVED)
4685     {
4686       type = g_type_from_name (path_elt->elt.class_name);
4687       if (type != G_TYPE_INVALID)
4688         {
4689           g_free (path_elt->elt.class_name);
4690           path_elt->elt.class_type = type;
4691           path_elt->type = PATH_ELT_TYPE;
4692         }
4693       else
4694         return g_str_equal (type_name, path_elt->elt.class_name);
4695     }
4696   
4697   return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4698 }
4699
4700 static gboolean
4701 match_widget_class_recursive (GSList *list, 
4702                               guint   length, 
4703                               gchar  *path, 
4704                               gchar  *path_reversed)
4705 {
4706   PathElt *path_elt;
4707   
4708   /* break out if we cannot match anymore. */
4709   if (list == NULL)
4710     {
4711       if (length > 0)
4712         return FALSE;
4713       else
4714         return TRUE;
4715     }
4716
4717   /* there are two possibilities:
4718    *  1. The next pattern should match the class.
4719    *  2. First normal matching, and then maybe a class */
4720   
4721   path_elt = list->data;
4722
4723   if (path_elt->type != PATH_ELT_PSPEC)
4724     {
4725       gchar *class_start = path;
4726       gchar *class_end;
4727       
4728       /* ignore leading dot */
4729       if (class_start[0] == '.')
4730         class_start++;
4731       class_end = strchr (class_start, '.');
4732
4733       if (class_end == NULL)
4734         {
4735           if (!match_class (path_elt, class_start))
4736             return FALSE;
4737           else
4738             return match_widget_class_recursive (list->next, 0, "", "");
4739         }
4740       else
4741         {
4742           class_end[0] = '\0';
4743           if (!match_class (path_elt, class_start))
4744             {
4745               class_end[0] = '.';
4746               return FALSE;
4747             }
4748           else
4749             {
4750               gboolean result;
4751               gint new_length = length - (class_end - path);
4752               gchar old_char = path_reversed[new_length];
4753               
4754               class_end[0] = '.';
4755               
4756               path_reversed[new_length] = '\0';
4757               result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4758               path_reversed[new_length] = old_char;
4759               
4760               return result;
4761             }
4762         }
4763     }
4764   else
4765     {
4766       PathElt *class_elt;
4767       gchar *class_start;
4768       gchar *class_end;
4769       gboolean result = FALSE;
4770       
4771       /* If there is nothing after this (ie. no class match), 
4772        * just compare the pspec. 
4773        */
4774       if (list->next == NULL)
4775         return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4776       
4777       class_elt = (PathElt *)list->next->data;
4778       g_assert (class_elt->type != PATH_ELT_PSPEC);
4779       
4780       class_start = path;
4781       if (class_start[0] == '.')
4782         class_start++;
4783       
4784       while (TRUE)
4785         {
4786           class_end = strchr (class_start, '.');
4787           
4788           /* It should be cheaper to match the class first. (either the pattern
4789            * is simple, and will match most of the times, or it may be complex
4790            * and matching is slow) 
4791            */
4792           if (class_end == NULL)
4793             {
4794               result = match_class (class_elt, class_start);
4795             }
4796           else
4797             {
4798               class_end[0] = '\0';
4799               result = match_class (class_elt, class_start);
4800               class_end[0] = '.';
4801             }
4802           
4803           if (result)
4804             {
4805               gchar old_char;
4806               result = FALSE;
4807               
4808               /* terminate the string in front of the class. It does not matter
4809                * that the class becomes unusable, because it is not needed 
4810                * inside the recursion 
4811                */
4812               old_char = class_start[0];
4813               class_start[0] = '\0';
4814               
4815               if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4816                 {
4817                   if (class_end != NULL)
4818                     {
4819                       gint new_length = length - (class_end - path);
4820                       gchar path_reversed_char = path_reversed[new_length];
4821                       
4822                       path_reversed[new_length] = '\0';
4823                       
4824                       result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4825                       
4826                       path_reversed[new_length] = path_reversed_char;
4827                     }
4828                   else
4829                     result = match_widget_class_recursive (list->next->next, 0, "", "");
4830                 }
4831                 
4832               class_start[0] = old_char;
4833             }
4834           
4835           if (result)
4836             return TRUE;
4837           
4838           /* get next class in path, or break out */
4839           if (class_end != NULL)
4840             class_start = class_end + 1;
4841           else
4842             return FALSE;
4843         }
4844     }
4845 }
4846
4847 gboolean
4848 _gtk_rc_match_widget_class (GSList  *list,
4849                             gint     length,
4850                             gchar   *path,
4851                             gchar   *path_reversed)
4852 {
4853   return match_widget_class_recursive (list, length, path, path_reversed);
4854 }
4855
4856 #ifdef G_OS_WIN32
4857
4858 /* DLL ABI stability backward compatibility versions */
4859
4860 #undef gtk_rc_add_default_file
4861
4862 void
4863 gtk_rc_add_default_file (const gchar *filename)
4864 {
4865   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4866
4867   gtk_rc_add_default_file_utf8 (utf8_filename);
4868
4869   g_free (utf8_filename);
4870 }
4871
4872 #undef gtk_rc_set_default_files
4873
4874 void
4875 gtk_rc_set_default_files (gchar **filenames)
4876 {
4877   gchar **utf8_filenames;
4878   int n = 0, i;
4879
4880   while (filenames[n++] != NULL)
4881     ;
4882
4883   utf8_filenames = g_new (gchar *, n + 1);
4884
4885   for (i = 0; i < n; i++)
4886     utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4887
4888   utf8_filenames[n] = NULL;
4889
4890   gtk_rc_set_default_files_utf8 (utf8_filenames);
4891
4892   g_strfreev (utf8_filenames);
4893 }
4894
4895 #undef gtk_rc_parse
4896
4897 void
4898 gtk_rc_parse (const gchar *filename)
4899 {
4900   gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4901
4902   gtk_rc_parse_utf8 (utf8_filename);
4903
4904   g_free (utf8_filename);
4905 }
4906
4907 #endif
4908
4909 #define __GTK_RC_C__
4910 #include "gtkaliasdef.c"