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