1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
44 #include <glib/gstdio.h>
45 #include "gdkconfig.h"
47 #include "gtkversion.h"
49 #include "gtkbindings.h"
50 #include "gtkthemes.h"
52 #include "gtkiconfactory.h"
54 #include "gtkmodules.h"
55 #include "gtkprivate.h"
56 #include "gtksettings.h"
57 #include "gtkwindow.h"
65 typedef struct _GtkRcSet GtkRcSet;
66 typedef struct _GtkRcNode GtkRcNode;
67 typedef struct _GtkRcFile GtkRcFile;
102 gchar *canonical_name;
105 guint is_string : 1; /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
111 GHashTable *rc_style_ht;
112 GtkSettings *settings;
113 GSList *rc_sets_widget;
114 GSList *rc_sets_widget_class;
115 GSList *rc_sets_class;
117 /* The files we have parsed, to reread later if necessary */
121 gchar *key_theme_name;
126 gint default_priority;
127 GtkStyle *default_style;
129 GHashTable *color_hash;
134 #define GTK_RC_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RC_STYLE, GtkRcStylePrivate))
136 typedef struct _GtkRcStylePrivate GtkRcStylePrivate;
138 struct _GtkRcStylePrivate
140 GSList *color_hashes;
143 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
145 static guint gtk_rc_style_hash (const gchar *name);
146 static gboolean gtk_rc_style_equal (const gchar *a,
148 static guint gtk_rc_styles_hash (const GSList *rc_styles);
149 static gboolean gtk_rc_styles_equal (const GSList *a,
151 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
153 static GSList * gtk_rc_styles_match (GSList *rc_styles,
157 gchar *path_reversed);
158 static GtkStyle * gtk_rc_style_to_style (GtkRcContext *context,
159 GtkRcStyle *rc_style);
160 static GtkStyle* gtk_rc_init_style (GtkRcContext *context,
162 static void gtk_rc_parse_default_files (GtkRcContext *context);
163 static void gtk_rc_parse_named (GtkRcContext *context,
166 static void gtk_rc_context_parse_file (GtkRcContext *context,
167 const gchar *filename,
170 static void gtk_rc_parse_any (GtkRcContext *context,
171 const gchar *input_name,
173 const gchar *input_string);
174 static guint gtk_rc_parse_statement (GtkRcContext *context,
176 static guint gtk_rc_parse_style (GtkRcContext *context,
178 static guint gtk_rc_parse_assignment (GScanner *scanner,
180 GtkRcProperty *prop);
181 static guint gtk_rc_parse_bg (GScanner *scanner,
183 static guint gtk_rc_parse_fg (GScanner *scanner,
185 static guint gtk_rc_parse_text (GScanner *scanner,
187 static guint gtk_rc_parse_base (GScanner *scanner,
189 static guint gtk_rc_parse_xthickness (GScanner *scanner,
191 static guint gtk_rc_parse_ythickness (GScanner *scanner,
193 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
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,
204 GtkRcStyle **rc_style);
205 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
207 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
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,
214 static guint gtk_rc_parse_stock (GtkRcContext *context,
216 GtkRcStyle *rc_style,
217 GtkIconFactory *factory);
218 static guint gtk_rc_parse_logical_color (GScanner *scanner,
219 GtkRcStyle *rc_style,
221 static guint gtk_rc_parse_color_full (GScanner *scanner,
225 static void gtk_rc_clear_hash_node (gpointer key,
228 static void gtk_rc_clear_styles (GtkRcContext *context);
229 static void gtk_rc_add_initial_default_files (void);
231 static void gtk_rc_style_finalize (GObject *object);
232 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
234 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
235 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
236 static void gtk_rc_style_copy_icons_and_colors(GtkRcStyle *rc_style,
237 GtkRcStyle *src_style,
238 GtkRcContext *context);
239 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
240 gconstpointer bsearch_node2);
241 static void gtk_rc_set_free (GtkRcSet *rc_set);
244 static const GScannerConfig gtk_rc_scanner_config =
248 ) /* cset_skip_characters */,
253 ) /* cset_identifier_first */,
259 ) /* cset_identifier_nth */,
260 ( "#\n" ) /* cpair_comment_single */,
262 TRUE /* case_sensitive */,
264 TRUE /* skip_comment_multi */,
265 TRUE /* skip_comment_single */,
266 TRUE /* scan_comment_multi */,
267 TRUE /* scan_identifier */,
268 FALSE /* scan_identifier_1char */,
269 FALSE /* scan_identifier_NULL */,
270 TRUE /* scan_symbols */,
271 TRUE /* scan_binary */,
272 TRUE /* scan_octal */,
273 TRUE /* scan_float */,
275 TRUE /* scan_hex_dollar */,
276 TRUE /* scan_string_sq */,
277 TRUE /* scan_string_dq */,
278 TRUE /* numbers_2_int */,
279 FALSE /* int_2_float */,
280 FALSE /* identifier_2_string */,
281 TRUE /* char_2_token */,
282 TRUE /* symbol_2_token */,
283 FALSE /* scope_0_fallback */,
286 static const gchar symbol_names[] =
330 { 0, GTK_RC_TOKEN_INCLUDE },
331 { 8, GTK_RC_TOKEN_NORMAL },
332 { 15, GTK_RC_TOKEN_ACTIVE },
333 { 22, GTK_RC_TOKEN_PRELIGHT },
334 { 31, GTK_RC_TOKEN_SELECTED },
335 { 40, GTK_RC_TOKEN_INSENSITIVE },
336 { 52, GTK_RC_TOKEN_FG },
337 { 55, GTK_RC_TOKEN_BG },
338 { 58, GTK_RC_TOKEN_TEXT },
339 { 63, GTK_RC_TOKEN_BASE },
340 { 68, GTK_RC_TOKEN_XTHICKNESS },
341 { 79, GTK_RC_TOKEN_YTHICKNESS },
342 { 90, GTK_RC_TOKEN_FONT },
343 { 95, GTK_RC_TOKEN_FONTSET },
344 { 103, GTK_RC_TOKEN_FONT_NAME },
345 { 113, GTK_RC_TOKEN_BG_PIXMAP },
346 { 123, GTK_RC_TOKEN_PIXMAP_PATH },
347 { 135, GTK_RC_TOKEN_STYLE },
348 { 141, GTK_RC_TOKEN_BINDING },
349 { 149, GTK_RC_TOKEN_BIND },
350 { 154, GTK_RC_TOKEN_WIDGET },
351 { 161, GTK_RC_TOKEN_WIDGET_CLASS },
352 { 174, GTK_RC_TOKEN_CLASS },
353 { 180, GTK_RC_TOKEN_LOWEST },
354 { 187, GTK_RC_TOKEN_GTK },
355 { 191, GTK_RC_TOKEN_APPLICATION },
356 { 203, GTK_RC_TOKEN_THEME },
357 { 209, GTK_RC_TOKEN_RC },
358 { 212, GTK_RC_TOKEN_HIGHEST },
359 { 220, GTK_RC_TOKEN_ENGINE },
360 { 227, GTK_RC_TOKEN_MODULE_PATH },
361 { 239, GTK_RC_TOKEN_STOCK },
362 { 245, GTK_RC_TOKEN_IM_MODULE_FILE },
363 { 260, GTK_RC_TOKEN_LTR },
364 { 264, GTK_RC_TOKEN_RTL },
365 { 268, GTK_RC_TOKEN_COLOR },
366 { 274, GTK_RC_TOKEN_UNBIND }
369 static GHashTable *realized_style_ht = NULL;
371 static gchar *im_module_file = NULL;
373 static gint max_default_files = 0;
374 static gchar **gtk_rc_default_files = NULL;
376 /* A stack of information of RC files we are parsing currently.
377 * The directories for these files are implicitely added to the end of
380 static GSList *current_files_stack = NULL;
382 /* RC files and strings that are parsed for every context
384 static GSList *global_rc_files = NULL;
386 /* Keep list of all current RC contexts for convenience
388 static GSList *rc_contexts;
390 /* RC file handling */
393 gtk_rc_make_default_dir (const gchar *type)
398 var = g_getenv ("GTK_EXE_PREFIX");
401 path = g_build_filename (var, "lib", "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
403 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
409 * gtk_rc_get_im_module_path:
410 * @returns: a newly-allocated string containing the path in which to
411 * look for IM modules.
413 * Obtains the path in which to look for IM modules. See the documentation
414 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
415 * environment variable for more details about looking up modules. This
416 * function is useful solely for utilities supplied with GTK+ and should
417 * not be used by applications under normal circumstances.
420 gtk_rc_get_im_module_path (void)
422 gchar **paths = _gtk_get_module_path ("immodules");
423 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
430 * gtk_rc_get_im_module_file:
431 * @returns: a newly-allocated string containing the name of the file
432 * listing the IM modules available for loading
434 * Obtains the path to the IM modules file. See the documentation
435 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
436 * environment variable for more details.
439 gtk_rc_get_im_module_file (void)
441 const gchar *var = g_getenv ("GTK_IM_MODULE_FILE");
442 gchar *result = NULL;
445 result = g_strdup (var);
450 result = g_strdup (im_module_file);
452 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
459 gtk_rc_get_theme_dir (void)
464 var = g_getenv ("GTK_DATA_PREFIX");
467 path = g_build_filename (var, "share", "themes", NULL);
469 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
475 * gtk_rc_get_module_dir:
477 * Returns a directory in which GTK+ looks for theme engines.
478 * For full information about the search for theme engines,
479 * see the docs for <envar>GTK_PATH</envar> in
480 * <xref linkend="gtk-running"/>.
482 * return value: the directory. (Must be freed with g_free())
485 gtk_rc_get_module_dir (void)
487 return gtk_rc_make_default_dir ("engines");
491 gtk_rc_add_initial_default_files (void)
493 static gint init = FALSE;
502 gtk_rc_default_files = g_new (gchar*, 10);
503 max_default_files = 10;
505 gtk_rc_default_files[0] = NULL;
508 var = g_getenv ("GTK2_RC_FILES");
512 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, -1);
516 gtk_rc_add_default_file (files[i]);
524 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
526 gtk_rc_add_default_file (str);
529 home = g_get_home_dir ();
532 str = g_build_filename (home, ".gtkrc-2.0", NULL);
533 gtk_rc_add_default_file (str);
540 * gtk_rc_add_default_file:
541 * @filename: the pathname to the file. If @filename is not absolute, it
542 * is searched in the current directory.
544 * Adds a file to the list of files to be parsed at the
548 gtk_rc_add_default_file (const gchar *filename)
552 gtk_rc_add_initial_default_files ();
554 for (n = 0; n < max_default_files; n++)
556 if (gtk_rc_default_files[n] == NULL)
560 if (n == max_default_files)
562 max_default_files += 10;
563 gtk_rc_default_files = g_renew (gchar*, gtk_rc_default_files, max_default_files);
566 gtk_rc_default_files[n++] = g_strdup (filename);
567 gtk_rc_default_files[n] = NULL;
571 * gtk_rc_set_default_files:
572 * @filenames: A %NULL-terminated list of filenames.
574 * Sets the list of files that GTK+ will read at the
578 gtk_rc_set_default_files (gchar **filenames)
582 gtk_rc_add_initial_default_files ();
585 while (gtk_rc_default_files[i])
587 g_free (gtk_rc_default_files[i]);
591 gtk_rc_default_files[0] = NULL;
594 while (filenames[i] != NULL)
596 gtk_rc_add_default_file (filenames[i]);
602 * gtk_rc_get_default_files:
604 * Retrieves the current list of RC files that will be parsed
605 * at the end of gtk_init().
607 * Return value: A %NULL-terminated array of filenames. This memory
608 * is owned by GTK+ and must not be freed by the application.
609 * If you want to store this information, you should make a copy.
612 gtk_rc_get_default_files (void)
614 gtk_rc_add_initial_default_files ();
616 return gtk_rc_default_files;
620 gtk_rc_settings_changed (GtkSettings *settings,
622 GtkRcContext *context)
624 gchar *new_theme_name;
625 gchar *new_key_theme_name;
627 if (context->reloading)
630 g_object_get (settings,
631 "gtk-theme-name", &new_theme_name,
632 "gtk-key-theme-name", &new_key_theme_name,
635 if ((new_theme_name != context->theme_name &&
636 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
637 (new_key_theme_name != context->key_theme_name &&
638 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
640 gtk_rc_reparse_all_for_settings (settings, TRUE);
643 g_free (new_theme_name);
644 g_free (new_key_theme_name);
648 gtk_rc_font_name_changed (GtkSettings *settings,
650 GtkRcContext *context)
652 if (!context->reloading)
653 _gtk_rc_context_get_default_font_name (settings);
657 gtk_rc_color_hash_changed (GtkSettings *settings,
659 GtkRcContext *context)
661 if (context->color_hash)
662 g_hash_table_unref (context->color_hash);
664 g_object_get (settings, "color-hash", &context->color_hash, NULL);
666 if (context->color_hash)
667 g_hash_table_ref (context->color_hash);
669 if (!context->reloading)
670 gtk_rc_reparse_all_for_settings (settings, TRUE);
673 static GtkRcContext *
674 gtk_rc_context_get (GtkSettings *settings)
676 if (!settings->rc_context)
678 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
680 context->settings = settings;
681 context->rc_style_ht = NULL;
682 context->rc_sets_widget = NULL;
683 context->rc_sets_widget_class = NULL;
684 context->rc_sets_class = NULL;
685 context->rc_files = NULL;
686 context->default_style = NULL;
688 g_object_get (settings,
689 "gtk-theme-name", &context->theme_name,
690 "gtk-key-theme-name", &context->key_theme_name,
691 "gtk-font-name", &context->font_name,
692 "color-hash", &context->color_hash,
695 if (context->color_hash)
696 g_hash_table_ref (context->color_hash);
698 g_signal_connect (settings,
699 "notify::gtk-theme-name",
700 G_CALLBACK (gtk_rc_settings_changed),
702 g_signal_connect (settings,
703 "notify::gtk-key-theme-name",
704 G_CALLBACK (gtk_rc_settings_changed),
706 g_signal_connect (settings,
707 "notify::gtk-font-name",
708 G_CALLBACK (gtk_rc_font_name_changed),
710 g_signal_connect (settings,
711 "notify::color-hash",
712 G_CALLBACK (gtk_rc_color_hash_changed),
715 context->pixmap_path = NULL;
717 context->default_priority = GTK_PATH_PRIO_RC;
719 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
722 return settings->rc_context;
726 gtk_rc_clear_rc_files (GtkRcContext *context)
730 list = context->rc_files;
733 GtkRcFile *rc_file = list->data;
735 if (rc_file->canonical_name != rc_file->name)
736 g_free (rc_file->canonical_name);
737 g_free (rc_file->directory);
738 g_free (rc_file->name);
744 g_slist_free (context->rc_files);
745 context->rc_files = NULL;
749 _gtk_rc_context_destroy (GtkSettings *settings)
751 GtkRcContext *context;
753 g_return_if_fail (GTK_IS_SETTINGS (settings));
755 context = settings->rc_context;
759 _gtk_settings_reset_rc_values (context->settings);
760 gtk_rc_clear_styles (context);
761 gtk_rc_clear_rc_files (context);
763 if (context->default_style)
764 g_object_unref (context->default_style);
766 g_strfreev (context->pixmap_path);
768 g_free (context->theme_name);
769 g_free (context->key_theme_name);
770 g_free (context->font_name);
772 if (context->color_hash)
773 g_hash_table_unref (context->color_hash);
775 g_signal_handlers_disconnect_by_func (settings,
776 gtk_rc_settings_changed, context);
777 g_signal_handlers_disconnect_by_func (settings,
778 gtk_rc_font_name_changed, context);
779 g_signal_handlers_disconnect_by_func (settings,
780 gtk_rc_color_hash_changed, context);
782 rc_contexts = g_slist_remove (rc_contexts, context);
786 settings->rc_context = NULL;
790 gtk_rc_parse_named (GtkRcContext *context,
795 const gchar *home_dir;
799 subpath = g_strconcat ("gtk-2.0-", type,
800 G_DIR_SEPARATOR_S "gtkrc",
803 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
805 /* First look in the users home directory
807 home_dir = g_get_home_dir ();
810 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
811 if (!g_file_test (path, G_FILE_TEST_EXISTS))
820 gchar *theme_dir = gtk_rc_get_theme_dir ();
821 path = g_build_filename (theme_dir, name, subpath, NULL);
824 if (!g_file_test (path, G_FILE_TEST_EXISTS))
833 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
841 gtk_rc_parse_default_files (GtkRcContext *context)
845 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
846 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
852 static gboolean initialized = FALSE;
858 gtk_rc_add_initial_default_files ();
861 /* Default RC string */
862 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
863 " bg[NORMAL] = \"#eee1b3\"\n"
864 " fg[NORMAL] = \"#000000\"\n"
867 "style \"gtk-default-progress-bar-style\" {\n"
868 " bg[PRELIGHT] = \"#4b6983\"\n"
869 " fg[PRELIGHT] = \"#ffffff\"\n"
870 " bg[NORMAL] = \"#c4c2bd\"\n"
873 "style \"gtk-default-menu-bar-item-style\" {\n"
874 " GtkMenuItem::horizontal_padding = 5\n"
877 "style \"gtk-default-menu-item-style\" {\n"
878 " bg[PRELIGHT] = \"#4b6983\"\n"
879 " fg[PRELIGHT] = \"#ffffff\"\n"
880 " base[PRELIGHT] = \"#4b6983\"\n"
881 " text[PRELIGHT] = \"#ffffff\"\n"
884 "style \"gtk-default-tray-icon-style\" {\n"
885 " bg_pixmap[NORMAL] = \"<parent>\"\n"
888 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
889 "class \"GtkTrayIcon\" style : gtk \"gtk-default-tray-icon-style\"\n"
890 "widget \"gtk-tooltips*\" style : gtk \"gtk-default-tooltips-style\"\n"
891 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
892 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
897 gtk_rc_context_parse_string (GtkRcContext *context,
898 const gchar *rc_string)
900 gtk_rc_parse_any (context, "-", -1, rc_string);
904 gtk_rc_parse_string (const gchar *rc_string)
909 g_return_if_fail (rc_string != NULL);
911 rc_file = g_new (GtkRcFile, 1);
912 rc_file->is_string = TRUE;
913 rc_file->name = g_strdup (rc_string);
914 rc_file->canonical_name = NULL;
915 rc_file->directory = NULL;
917 rc_file->reload = TRUE;
919 global_rc_files = g_slist_append (global_rc_files, rc_file);
921 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
922 gtk_rc_context_parse_string (tmp_list->data, rc_string);
926 add_to_rc_file_list (GSList **rc_file_list,
927 const char *filename,
933 tmp_list = *rc_file_list;
936 rc_file = tmp_list->data;
937 if (!strcmp (rc_file->name, filename))
940 tmp_list = tmp_list->next;
943 rc_file = g_new (GtkRcFile, 1);
944 rc_file->is_string = FALSE;
945 rc_file->name = g_strdup (filename);
946 rc_file->canonical_name = NULL;
947 rc_file->directory = NULL;
949 rc_file->reload = reload;
951 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
957 gtk_rc_context_parse_one_file (GtkRcContext *context,
958 const gchar *filename,
966 g_return_if_fail (filename != NULL);
968 saved_priority = context->default_priority;
969 context->default_priority = priority;
971 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
973 if (!rc_file->canonical_name)
975 /* Get the absolute pathname */
977 if (g_path_is_absolute (rc_file->name))
978 rc_file->canonical_name = rc_file->name;
983 cwd = g_get_current_dir ();
984 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
988 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
991 /* If the file is already being parsed (recursion), do nothing
993 if (g_slist_find (current_files_stack, rc_file))
996 if (!g_lstat (rc_file->canonical_name, &statbuf))
1000 rc_file->mtime = statbuf.st_mtime;
1002 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1006 /* Temporarily push information for this file on
1007 * a stack of current files while parsing it.
1009 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1010 gtk_rc_parse_any (context, filename, fd, NULL);
1011 current_files_stack = g_slist_delete_link (current_files_stack,
1012 current_files_stack);
1018 context->default_priority = saved_priority;
1022 strchr_len (const gchar *str, gint len, char c)
1027 return (gchar *)str;
1036 gtk_rc_context_parse_file (GtkRcContext *context,
1037 const gchar *filename,
1041 gchar *locale_suffixes[2];
1042 gint n_locale_suffixes = 0;
1046 gboolean found = FALSE;
1048 locale = _gtk_get_lc_ctype ();
1050 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1052 /* Determine locale-specific suffixes for RC files.
1054 length = strlen (locale);
1056 p = strchr (locale, '@');
1058 length = p - locale;
1060 p = strchr_len (locale, length, '.');
1062 length = p - locale;
1064 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1066 p = strchr_len (locale, length, '_');
1069 length = p - locale;
1070 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1076 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1077 for (j = 0; j < n_locale_suffixes; j++)
1081 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1082 if (g_file_test (name, G_FILE_TEST_EXISTS))
1084 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1091 g_free (locale_suffixes[j]);
1096 gtk_rc_parse (const gchar *filename)
1100 g_return_if_fail (filename != NULL);
1102 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1104 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1105 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1108 /* Handling of RC styles */
1110 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1113 gtk_rc_style_init (GtkRcStyle *style)
1115 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1119 style->font_desc = NULL;
1121 for (i = 0; i < 5; i++)
1123 static const GdkColor init_color = { 0, 0, 0, 0, };
1125 style->bg_pixmap_name[i] = NULL;
1126 style->color_flags[i] = 0;
1127 style->fg[i] = init_color;
1128 style->bg[i] = init_color;
1129 style->text[i] = init_color;
1130 style->base[i] = init_color;
1132 style->xthickness = -1;
1133 style->ythickness = -1;
1134 style->rc_properties = NULL;
1136 style->rc_style_lists = NULL;
1137 style->icon_factories = NULL;
1139 priv->color_hashes = NULL;
1143 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1145 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1147 object_class->finalize = gtk_rc_style_finalize;
1149 klass->parse = NULL;
1150 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1151 klass->merge = gtk_rc_style_real_merge;
1152 klass->create_style = gtk_rc_style_real_create_style;
1154 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1158 gtk_rc_style_finalize (GObject *object)
1160 GSList *tmp_list1, *tmp_list2;
1161 GtkRcStyle *rc_style;
1162 GtkRcStylePrivate *rc_priv;
1165 rc_style = GTK_RC_STYLE (object);
1166 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1169 g_free (rc_style->name);
1170 if (rc_style->font_desc)
1171 pango_font_description_free (rc_style->font_desc);
1173 for (i = 0; i < 5; i++)
1174 if (rc_style->bg_pixmap_name[i])
1175 g_free (rc_style->bg_pixmap_name[i]);
1177 /* Now remove all references to this rc_style from
1180 tmp_list1 = rc_style->rc_style_lists;
1183 GSList *rc_styles = tmp_list1->data;
1184 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1185 g_object_unref (style);
1187 /* Remove the list of styles from the other rc_styles
1190 tmp_list2 = rc_styles;
1193 GtkRcStyle *other_style = tmp_list2->data;
1195 if (other_style != rc_style)
1196 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1198 tmp_list2 = tmp_list2->next;
1201 /* And from the hash table itself
1203 g_hash_table_remove (realized_style_ht, rc_styles);
1204 g_slist_free (rc_styles);
1206 tmp_list1 = tmp_list1->next;
1208 g_slist_free (rc_style->rc_style_lists);
1210 if (rc_style->rc_properties)
1214 for (i = 0; i < rc_style->rc_properties->len; i++)
1216 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1218 g_free (node->origin);
1219 g_value_unset (&node->value);
1221 g_array_free (rc_style->rc_properties, TRUE);
1222 rc_style->rc_properties = NULL;
1225 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1226 g_slist_free (rc_style->icon_factories);
1228 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1229 g_slist_free (rc_priv->color_hashes);
1231 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1235 gtk_rc_style_new (void)
1239 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1245 * gtk_rc_style_copy:
1246 * @orig: the style to copy
1248 * Makes a copy of the specified #GtkRcStyle. This function
1249 * will correctly copy an RC style that is a member of a class
1250 * derived from #GtkRcStyle.
1252 * Return value: the resulting #GtkRcStyle
1255 gtk_rc_style_copy (GtkRcStyle *orig)
1259 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1261 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1262 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1264 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1270 gtk_rc_style_ref (GtkRcStyle *rc_style)
1272 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1274 g_object_ref (rc_style);
1278 gtk_rc_style_unref (GtkRcStyle *rc_style)
1280 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1282 g_object_unref (rc_style);
1286 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1288 return g_object_new (G_OBJECT_TYPE (style), NULL);
1292 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1294 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1296 return priv->color_hashes;
1300 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1301 gconstpointer bsearch_node2)
1303 const GtkRcProperty *prop1 = bsearch_node1;
1304 const GtkRcProperty *prop2 = bsearch_node2;
1306 if (prop1->type_name == prop2->type_name)
1307 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1309 return prop1->type_name < prop2->type_name ? -1 : 1;
1313 insert_rc_property (GtkRcStyle *style,
1314 GtkRcProperty *property,
1318 GtkRcProperty *new_property = NULL;
1319 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1321 key.type_name = property->type_name;
1322 key.property_name = property->property_name;
1324 if (!style->rc_properties)
1325 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1328 while (i < style->rc_properties->len)
1330 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1336 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1338 g_free (new_property->origin);
1339 g_value_unset (&new_property->value);
1341 *new_property = key;
1355 g_array_insert_val (style->rc_properties, i, key);
1356 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1359 new_property->origin = g_strdup (property->origin);
1360 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1361 g_value_copy (&property->value, &new_property->value);
1365 gtk_rc_style_real_merge (GtkRcStyle *dest,
1370 for (i = 0; i < 5; i++)
1372 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1373 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1375 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1376 src->color_flags[i] & GTK_RC_FG)
1378 dest->fg[i] = src->fg[i];
1379 dest->color_flags[i] |= GTK_RC_FG;
1381 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1382 src->color_flags[i] & GTK_RC_BG)
1384 dest->bg[i] = src->bg[i];
1385 dest->color_flags[i] |= GTK_RC_BG;
1387 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1388 src->color_flags[i] & GTK_RC_TEXT)
1390 dest->text[i] = src->text[i];
1391 dest->color_flags[i] |= GTK_RC_TEXT;
1393 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1394 src->color_flags[i] & GTK_RC_BASE)
1396 dest->base[i] = src->base[i];
1397 dest->color_flags[i] |= GTK_RC_BASE;
1401 if (dest->xthickness < 0 && src->xthickness >= 0)
1402 dest->xthickness = src->xthickness;
1403 if (dest->ythickness < 0 && src->ythickness >= 0)
1404 dest->ythickness = src->ythickness;
1408 if (!dest->font_desc)
1409 dest->font_desc = pango_font_description_copy (src->font_desc);
1411 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1414 if (src->rc_properties)
1418 for (i = 0; i < src->rc_properties->len; i++)
1419 insert_rc_property (dest,
1420 &g_array_index (src->rc_properties, GtkRcProperty, i),
1426 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1428 return gtk_style_new ();
1432 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1434 GtkIconFactory *factory = gtk_icon_factory_new ();
1436 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1440 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1442 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1443 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1445 (GDestroyNotify) gdk_color_free);
1447 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1451 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1452 GtkRcStyle *src_style)
1454 GSList *concat = g_slist_copy (src_style->icon_factories);
1456 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1458 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1462 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1463 GtkRcStyle *src_style)
1465 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1466 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1467 GSList *concat = g_slist_copy (src_priv->color_hashes);
1469 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1471 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1475 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1476 GtkRcStyle *src_style,
1477 GtkRcContext *context)
1479 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1483 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1485 /* Append src_style's factories, adding a ref to them */
1486 if (src_style->icon_factories != NULL)
1488 /* Add a factory for ourselves if we have none,
1489 * in case we end up defining more stock icons.
1490 * I see no real way around this; we need to maintain
1491 * the invariant that the first factory in the list
1492 * is always our_factory, the one belonging to us,
1493 * and if we put src_style factories in the list we can't
1494 * do that if the style is reopened.
1496 if (rc_style->icon_factories == NULL)
1497 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1499 gtk_rc_style_append_icon_factories (rc_style, src_style);
1502 /* Also append src_style's color hashes, adding a ref to them */
1503 if (src_priv->color_hashes != NULL)
1505 /* See comment above .. */
1506 if (priv->color_hashes == NULL)
1507 gtk_rc_style_prepend_empty_color_hash (rc_style);
1509 gtk_rc_style_append_color_hashes (rc_style, src_style);
1513 /* if we didn't get color hashes from the src_style, initialize
1514 * the list with the settings' color scheme (if it exists)
1516 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1518 gtk_rc_style_prepend_empty_color_hash (rc_style);
1520 priv->color_hashes =
1521 g_slist_append (priv->color_hashes,
1522 g_hash_table_ref (context->color_hash));
1527 gtk_rc_clear_hash_node (gpointer key,
1531 gtk_rc_style_unref (data);
1535 gtk_rc_free_rc_sets (GSList *slist)
1541 rc_set = slist->data;
1542 gtk_rc_set_free (rc_set);
1544 slist = slist->next;
1549 gtk_rc_clear_styles (GtkRcContext *context)
1551 /* Clear out all old rc_styles */
1553 if (context->rc_style_ht)
1555 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1556 g_hash_table_destroy (context->rc_style_ht);
1557 context->rc_style_ht = NULL;
1560 gtk_rc_free_rc_sets (context->rc_sets_widget);
1561 g_slist_free (context->rc_sets_widget);
1562 context->rc_sets_widget = NULL;
1564 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1565 g_slist_free (context->rc_sets_widget_class);
1566 context->rc_sets_widget_class = NULL;
1568 gtk_rc_free_rc_sets (context->rc_sets_class);
1569 g_slist_free (context->rc_sets_class);
1570 context->rc_sets_class = NULL;
1573 /* Reset all our widgets. Also, we have to invalidate cached icons in
1574 * icon sets so they get re-rendered.
1577 gtk_rc_reset_widgets (GtkSettings *settings)
1579 GList *list, *toplevels;
1581 _gtk_icon_set_invalidate_caches ();
1583 toplevels = gtk_window_list_toplevels ();
1584 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1586 for (list = toplevels; list; list = list->next)
1588 if (gtk_widget_get_screen (list->data) == settings->screen)
1590 gtk_widget_reset_rc_styles (list->data);
1593 g_object_unref (list->data);
1595 g_list_free (toplevels);
1599 gtk_rc_clear_realized_style (gpointer key,
1603 GSList *rc_styles = key;
1604 GtkStyle *style = value;
1605 GSList *tmp_list = rc_styles;
1607 g_object_unref (style);
1611 GtkRcStyle *rc_style = tmp_list->data;
1613 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1615 tmp_list = tmp_list->next;
1618 g_slist_free (rc_styles);
1622 * gtk_rc_reset_styles:
1623 * @settings: a #GtkSettings
1625 * This function recomputes the styles for all widgets that use a
1626 * particular #GtkSettings object. (There is one #GtkSettings object
1627 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1628 * when some global parameter has changed that affects the appearance
1629 * of all widgets, because when a widget gets a new style, it will
1630 * both redraw and recompute any cached information about its
1631 * appearance. As an example, it is used when the default font size
1632 * set by the operating system changes. Note that this function
1633 * doesn't affect widgets that have a style set explicitely on them
1634 * with gtk_widget_set_style().
1639 gtk_rc_reset_styles (GtkSettings *settings)
1641 GtkRcContext *context;
1642 gboolean reset = FALSE;
1644 g_return_if_fail (GTK_IS_SETTINGS (settings));
1646 context = gtk_rc_context_get (settings);
1648 if (context->default_style)
1650 g_object_unref (context->default_style);
1651 context->default_style = NULL;
1655 /* Clear out styles that have been looked up already
1657 if (realized_style_ht)
1659 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1660 g_hash_table_destroy (realized_style_ht);
1661 realized_style_ht = NULL;
1666 gtk_rc_reset_widgets (settings);
1670 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1672 GtkRcContext *context;
1673 gchar *new_font_name;
1675 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1677 context = gtk_rc_context_get (settings);
1679 g_object_get (context->settings,
1680 "gtk-font-name", &new_font_name,
1683 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1685 g_free (context->font_name);
1686 context->font_name = g_strdup (new_font_name);
1688 gtk_rc_reset_styles (settings);
1691 g_free (new_font_name);
1693 return context->font_name;
1697 * gtk_rc_reparse_all_for_settings:
1698 * @settings: a #GtkSettings
1699 * @force_load: load whether or not anything changed
1701 * If the modification time on any previously read file
1702 * for the given #GtkSettings has changed, discard all style information
1703 * and then reread all previously read RC files.
1705 * Return value: %TRUE if the files were reread.
1708 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1709 gboolean force_load)
1711 gboolean mtime_modified = FALSE;
1714 GtkRcContext *context;
1715 struct stat statbuf;
1717 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1719 context = gtk_rc_context_get (settings);
1723 /* Check through and see if any of the RC's have had their
1724 * mtime modified. If so, reparse everything.
1726 tmp_list = context->rc_files;
1729 rc_file = tmp_list->data;
1731 if (!rc_file->is_string)
1733 if (!g_lstat (rc_file->name, &statbuf) &&
1734 (statbuf.st_mtime != rc_file->mtime))
1736 mtime_modified = TRUE;
1741 tmp_list = tmp_list->next;
1745 if (force_load || mtime_modified)
1747 _gtk_binding_reset_parsed ();
1748 gtk_rc_clear_styles (context);
1749 context->reloading = TRUE;
1751 _gtk_settings_reset_rc_values (context->settings);
1752 gtk_rc_clear_rc_files (context);
1754 gtk_rc_parse_default_files (context);
1756 tmp_list = global_rc_files;
1759 rc_file = tmp_list->data;
1761 if (rc_file->is_string)
1762 gtk_rc_context_parse_string (context, rc_file->name);
1764 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1766 tmp_list = tmp_list->next;
1769 g_free (context->theme_name);
1770 g_free (context->key_theme_name);
1772 g_object_get (context->settings,
1773 "gtk-theme-name", &context->theme_name,
1774 "gtk-key-theme-name", &context->key_theme_name,
1777 if (context->theme_name && context->theme_name[0])
1778 gtk_rc_parse_named (context, context->theme_name, NULL);
1779 if (context->key_theme_name && context->key_theme_name[0])
1780 gtk_rc_parse_named (context, context->key_theme_name, "key");
1782 context->reloading = FALSE;
1784 gtk_rc_reset_widgets (context->settings);
1787 return force_load || mtime_modified;
1791 * gtk_rc_reparse_all:
1793 * If the modification time on any previously read file for the
1794 * default #GtkSettings has changed, discard all style information
1795 * and then reread all previously read RC files.
1797 * Return value: %TRUE if the files were reread.
1800 gtk_rc_reparse_all (void)
1803 gboolean result = FALSE;
1805 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1807 GtkRcContext *context = tmp_list->data;
1808 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1816 gtk_rc_styles_match (GSList *rc_styles,
1820 gchar *path_reversed)
1827 rc_set = sets->data;
1830 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1832 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1833 rc_styles = g_slist_append (rc_styles, rc_set);
1837 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1838 rc_styles = g_slist_append (rc_styles, rc_set);
1846 rc_set_compare (gconstpointer a, gconstpointer b)
1848 const GtkRcSet *set_a = a;
1849 const GtkRcSet *set_b = b;
1851 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1855 sort_and_dereference_sets (GSList *styles)
1859 /* At this point, the list of sets is ordered by:
1861 * a) 'widget' patterns are earlier than 'widget_class' patterns
1862 * which are ealier than 'class' patterns.
1863 * a) For two matches for class patterns, a match to a child type
1864 * is before a match to a parent type
1865 * c) a match later in the RC file (or in a later RC file) is before a
1866 * match earlier in the RC file.
1868 * With a) taking precedence over b) which takes precendence over c).
1870 * Now sort by priority, which has the highest precendence for sort order
1872 styles = g_slist_sort (styles, rc_set_compare);
1874 /* Make styles->data = styles->data->rc_style
1879 GtkRcSet *set = tmp_list->data;
1880 tmp_list->data = set->rc_style;
1881 tmp_list = tmp_list->next;
1889 * @widget: a #GtkWidget
1891 * Finds all matching RC styles for a given widget,
1892 * composites them together, and then creates a
1893 * #GtkStyle representing the composite appearance.
1894 * (GTK+ actually keeps a cache of previously
1895 * created styles, so a new style may not be
1898 * Returns: the resulting style. No refcount is added
1899 * to the returned style, so if you want to save this
1900 * style around, you should add a reference yourself.
1903 gtk_rc_get_style (GtkWidget *widget)
1905 GtkRcStyle *widget_rc_style;
1906 GSList *rc_styles = NULL;
1907 GtkRcContext *context;
1909 static guint rc_style_key_id = 0;
1911 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1913 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1915 /* We allow the specification of a single rc style to be bound
1916 * tightly to a widget, for application modifications
1918 if (!rc_style_key_id)
1919 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1921 if (context->rc_sets_widget)
1923 gchar *path, *path_reversed;
1926 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1927 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1929 g_free (path_reversed);
1932 if (context->rc_sets_widget_class)
1934 gchar *path, *path_reversed;
1937 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1938 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1940 g_free (path_reversed);
1943 if (context->rc_sets_class)
1947 type = G_TYPE_FROM_INSTANCE (widget);
1951 gchar *path_reversed;
1954 path = g_strdup (g_type_name (type));
1955 path_length = strlen (path);
1956 path_reversed = g_strdup (path);
1957 g_strreverse (path_reversed);
1959 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1961 g_free (path_reversed);
1963 type = g_type_parent (type);
1967 rc_styles = sort_and_dereference_sets (rc_styles);
1969 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
1971 if (widget_rc_style)
1972 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1975 return gtk_rc_init_style (context, rc_styles);
1978 if (!context->default_style)
1980 context->default_style = gtk_style_new ();
1981 _gtk_style_init_for_settings (context->default_style, context->settings);
1984 return context->default_style;
1989 * gtk_rc_get_style_by_paths:
1990 * @settings: a #GtkSettings object
1991 * @widget_path: the widget path to use when looking up the style, or %NULL
1992 * if no matching against the widget path should be done
1993 * @class_path: the class path to use when looking up the style, or %NULL
1994 * if no matching against the class path should be done.
1995 * @type: a type that will be used along with parent types of this type
1996 * when matching against class styles, or #G_TYPE_NONE
1998 * Creates up a #GtkStyle from styles defined in a RC file by providing
1999 * the raw components used in matching. This function may be useful
2000 * when creating pseudo-widgets that should be themed like widgets but
2001 * don't actually have corresponding GTK+ widgets. An example of this
2002 * would be items inside a GNOME canvas widget.
2004 * The action of gtk_rc_get_style() is similar to:
2005 * <informalexample><programlisting>
2006 * gtk_widget_path (widget, NULL, &path, NULL);
2007 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2008 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
2009 * G_OBJECT_TYPE (widget));
2010 * </programlisting></informalexample>
2012 * Return value: A style created by matching with the supplied paths,
2013 * or %NULL if nothing matching was specified and the default style should
2014 * be used. The returned value is owned by GTK+ as part of an internal cache,
2015 * so you must call g_object_ref() on the returned value if you want to
2016 * keep a reference to it.
2019 gtk_rc_get_style_by_paths (GtkSettings *settings,
2020 const char *widget_path,
2021 const char *class_path,
2024 /* We duplicate the code from above to avoid slowing down the above
2025 * by generating paths when we don't need them. I don't know if
2026 * this is really worth it.
2028 GSList *rc_styles = NULL;
2029 GtkRcContext *context;
2031 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2033 context = gtk_rc_context_get (settings);
2035 if (widget_path && context->rc_sets_widget)
2038 gchar *path_reversed;
2041 path_length = strlen (widget_path);
2042 path = g_strdup (widget_path);
2043 path_reversed = g_strdup (widget_path);
2044 g_strreverse (path_reversed);
2046 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2048 g_free (path_reversed);
2051 if (class_path && context->rc_sets_widget_class)
2054 gchar *path_reversed;
2057 path = g_strdup (class_path);
2058 path_length = strlen (class_path);
2059 path_reversed = g_strdup (class_path);
2060 g_strreverse (path_reversed);
2062 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2064 g_free (path_reversed);
2067 if (type != G_TYPE_NONE && context->rc_sets_class)
2072 gchar *path_reversed;
2075 path = g_strdup (g_type_name (type));
2076 path_length = strlen (path);
2077 path_reversed = g_strdup (path);
2078 g_strreverse (path_reversed);
2080 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2082 g_free (path_reversed);
2084 type = g_type_parent (type);
2088 rc_styles = sort_and_dereference_sets (rc_styles);
2091 return gtk_rc_init_style (context, rc_styles);
2097 gtk_rc_add_rc_sets (GSList *slist,
2098 GtkRcStyle *rc_style,
2099 const gchar *pattern,
2100 GtkPathType path_type)
2102 GtkRcStyle *new_style;
2106 new_style = gtk_rc_style_new ();
2107 *new_style = *rc_style;
2108 new_style->name = g_strdup (rc_style->name);
2109 if (rc_style->font_desc)
2110 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
2112 for (i = 0; i < 5; i++)
2113 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
2115 rc_set = g_new (GtkRcSet, 1);
2116 rc_set->type = path_type;
2118 if (path_type == GTK_PATH_WIDGET_CLASS)
2120 rc_set->pspec = NULL;
2121 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
2125 rc_set->pspec = g_pattern_spec_new (pattern);
2126 rc_set->path = NULL;
2129 rc_set->rc_style = rc_style;
2131 return g_slist_prepend (slist, rc_set);
2135 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
2136 const gchar *pattern)
2138 GtkRcContext *context;
2140 g_return_if_fail (rc_style != NULL);
2141 g_return_if_fail (pattern != NULL);
2143 context = gtk_rc_context_get (gtk_settings_get_default ());
2145 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern, GTK_PATH_WIDGET);
2149 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
2150 const gchar *pattern)
2152 GtkRcContext *context;
2154 g_return_if_fail (rc_style != NULL);
2155 g_return_if_fail (pattern != NULL);
2157 context = gtk_rc_context_get (gtk_settings_get_default ());
2159 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern, GTK_PATH_WIDGET_CLASS);
2163 gtk_rc_add_class_style (GtkRcStyle *rc_style,
2164 const gchar *pattern)
2166 GtkRcContext *context;
2168 g_return_if_fail (rc_style != NULL);
2169 g_return_if_fail (pattern != NULL);
2171 context = gtk_rc_context_get (gtk_settings_get_default ());
2173 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern, GTK_PATH_CLASS);
2177 gtk_rc_scanner_new (void)
2179 return g_scanner_new (>k_rc_scanner_config);
2183 gtk_rc_parse_any (GtkRcContext *context,
2184 const gchar *input_name,
2186 const gchar *input_string)
2192 scanner = gtk_rc_scanner_new ();
2196 g_assert (input_string == NULL);
2198 g_scanner_input_file (scanner, input_fd);
2202 g_assert (input_string != NULL);
2204 g_scanner_input_text (scanner, input_string, strlen (input_string));
2206 scanner->input_name = input_name;
2208 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2209 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2213 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2217 guint expected_token;
2219 expected_token = gtk_rc_parse_statement (context, scanner);
2221 if (expected_token != G_TOKEN_NONE)
2228 if (scanner->scope_id == 0)
2230 /* if we are in scope 0, we know the symbol names
2231 * that are associated with certain token values.
2232 * so we look them up to make the error messages
2235 if (expected_token > GTK_RC_TOKEN_INVALID &&
2236 expected_token < GTK_RC_TOKEN_LAST)
2238 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2239 if (symbols[i].token == expected_token)
2240 msg = symbol_names + symbols[i].name_offset;
2242 msg = g_strconcat ("e.g. `", msg, "'", NULL);
2244 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2245 scanner->token < GTK_RC_TOKEN_LAST)
2247 symbol_name = "???";
2248 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2249 if (symbols[i].token == scanner->token)
2250 symbol_name = symbol_names + symbols[i].name_offset;
2253 g_scanner_unexp_token (scanner,
2266 g_scanner_destroy (scanner);
2270 gtk_rc_styles_hash (const GSList *rc_styles)
2277 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2278 rc_styles = rc_styles->next;
2285 gtk_rc_styles_equal (const GSList *a,
2290 if (a->data != b->data)
2300 gtk_rc_style_hash (const gchar *name)
2306 result += (result << 3) + *name++;
2312 gtk_rc_style_equal (const gchar *a,
2315 return (strcmp (a, b) == 0);
2319 gtk_rc_style_find (GtkRcContext *context,
2322 if (context->rc_style_ht)
2323 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2329 gtk_rc_style_to_style (GtkRcContext *context,
2330 GtkRcStyle *rc_style)
2334 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2335 _gtk_style_init_for_settings (style, context->settings);
2337 style->rc_style = g_object_ref (rc_style);
2339 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2344 /* Reuses or frees rc_styles */
2346 gtk_rc_init_style (GtkRcContext *context,
2349 GtkStyle *style = NULL;
2352 g_return_val_if_fail (rc_styles != NULL, NULL);
2354 if (!realized_style_ht)
2355 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2356 (GEqualFunc) gtk_rc_styles_equal);
2358 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2362 GtkRcStyle *base_style = NULL;
2363 GtkRcStyle *proto_style;
2364 GtkRcStyleClass *proto_style_class;
2366 GType rc_style_type = GTK_TYPE_RC_STYLE;
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.
2373 base_style = rc_styles->data;
2374 tmp_styles = rc_styles;
2377 GtkRcStyle *rc_style = tmp_styles->data;
2379 if (rc_style->engine_specified ||
2380 G_OBJECT_TYPE (rc_style) != rc_style_type)
2382 base_style = rc_style;
2386 tmp_styles = tmp_styles->next;
2389 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2390 proto_style = proto_style_class->create_rc_style (base_style);
2392 tmp_styles = rc_styles;
2395 GtkRcStyle *rc_style = tmp_styles->data;
2397 proto_style_class->merge (proto_style, rc_style);
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);
2403 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2404 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2406 tmp_styles = tmp_styles->next;
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))
2413 g_free (proto_style->bg_pixmap_name[i]);
2414 proto_style->bg_pixmap_name[i] = NULL;
2417 style = gtk_rc_style_to_style (context, proto_style);
2418 gtk_rc_style_unref (proto_style);
2420 g_hash_table_insert (realized_style_ht, rc_styles, style);
2423 g_slist_free (rc_styles);
2428 /*********************
2429 * Parsing functions *
2430 *********************/
2433 lookup_color (GtkRcStyle *style,
2434 const char *color_name,
2437 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2440 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2442 GHashTable *hash = iter->data;
2443 GdkColor *match = g_hash_table_lookup (hash, color_name);
2447 color->red = match->red;
2448 color->green = match->green;
2449 color->blue = match->blue;
2458 rc_parse_token_or_compound (GScanner *scanner,
2461 GTokenType delimiter)
2463 guint token = g_scanner_get_next_token (scanner);
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.
2475 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2478 g_string_append_printf (gstring, " %f", scanner->value.v_float);
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, '"');
2487 case G_TOKEN_IDENTIFIER:
2488 g_string_append_c (gstring, ' ');
2489 g_string_append (gstring, scanner->value.v_identifier);
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)
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)
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)
2516 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2519 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2520 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2521 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2523 g_scanner_get_next_token (scanner);
2525 if (!style || !lookup_color (style, scanner->value.v_identifier,
2528 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2529 scanner->value.v_identifier);
2530 return G_TOKEN_IDENTIFIER;
2534 g_string_append_printf (gstring, " { %s, %s, %s }",
2535 g_ascii_formatd (rbuf, sizeof (rbuf),
2537 color.red / 65535.0),
2538 g_ascii_formatd (gbuf, sizeof (gbuf),
2540 color.green / 65535.0),
2541 g_ascii_formatd (bbuf, sizeof (bbuf),
2543 color.blue / 65535.0));
2547 return G_TOKEN_IDENTIFIER;
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;
2558 return G_TOKEN_NONE;
2560 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2564 gtk_rc_parse_assignment (GScanner *scanner,
2566 GtkRcProperty *prop)
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
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;
2585 /* check that this is an assignment */
2586 if (g_scanner_get_next_token (scanner) != '=')
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;
2597 /* record location */
2598 if (g_getenv ("GTK_DEBUG"))
2599 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2601 prop->origin = NULL;
2603 /* parse optional symbolic color prefix */
2604 if (g_scanner_peek_next_token (scanner) == '@')
2606 g_scanner_get_next_token (scanner); /* eat color prefix */
2610 /* parse optional sign */
2611 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2613 g_scanner_get_next_token (scanner); /* eat sign */
2617 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2620 token = g_scanner_peek_next_token (scanner);
2622 if (is_color && token != G_TOKEN_IDENTIFIER)
2624 token = G_TOKEN_IDENTIFIER;
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;
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;
2642 case G_TOKEN_STRING:
2643 g_scanner_get_next_token (scanner);
2645 token = G_TOKEN_INT;
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;
2653 case G_TOKEN_IDENTIFIER:
2657 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2658 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2659 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2662 g_scanner_get_next_token (scanner);
2664 if (!style || !lookup_color (style, scanner->value.v_identifier,
2667 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2668 scanner->value.v_identifier);
2669 token = G_TOKEN_IDENTIFIER;
2673 gstring = g_string_new (NULL);
2675 g_string_append_printf (gstring, " { %s, %s, %s }",
2676 g_ascii_formatd (rbuf, sizeof (rbuf),
2678 color.red / 65535.0),
2679 g_ascii_formatd (gbuf, sizeof (gbuf),
2681 color.green / 65535.0),
2682 g_ascii_formatd (bbuf, sizeof (bbuf),
2684 color.blue / 65535.0));
2686 g_value_init (&prop->value, G_TYPE_GSTRING);
2687 g_value_take_boxed (&prop->value, gstring);
2688 token = G_TOKEN_NONE;
2692 case G_TOKEN_LEFT_PAREN:
2693 case G_TOKEN_LEFT_CURLY:
2694 case G_TOKEN_LEFT_BRACE:
2697 GString *gstring = g_string_new (NULL);
2698 gboolean parse_on = TRUE;
2700 /* allow identifier(foobar) to support color expressions */
2701 if (token == G_TOKEN_IDENTIFIER)
2703 g_scanner_get_next_token (scanner);
2705 g_string_append_c (gstring, ' ');
2706 g_string_append (gstring, scanner->value.v_identifier);
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.
2714 * FIXME: right fix would be to call g_scanner_unget()
2715 * but that doesn't exist
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;
2724 token = g_scanner_peek_next_token (scanner);
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;
2734 if (token != G_TOKEN_LEFT_PAREN)
2736 token = G_TOKEN_NONE;
2742 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2744 if (token == G_TOKEN_NONE)
2746 g_string_append_c (gstring, ' ');
2747 g_value_init (&prop->value, G_TYPE_GSTRING);
2748 g_value_take_boxed (&prop->value, gstring);
2751 g_string_free (gstring, TRUE);
2756 g_scanner_get_next_token (scanner);
2757 token = G_TOKEN_INT;
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;
2775 is_c_identifier (const gchar *string)
2778 gboolean is_varname;
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;
2788 parse_include_file (GtkRcContext *context,
2790 const gchar *filename)
2792 char *to_parse = NULL;
2794 if (g_path_is_absolute (filename))
2796 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2797 * don't print an error in this case.
2799 to_parse = g_strdup (filename);
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.
2808 GSList *tmp_list = current_files_stack;
2811 GtkRcFile *curfile = tmp_list->data;
2812 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2814 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2822 tmp_list = tmp_list->next;
2828 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2833 g_scanner_warn (scanner,
2834 _("Unable to find include file: \"%s\""),
2841 gtk_rc_parse_statement (GtkRcContext *context,
2846 token = g_scanner_peek_next_token (scanner);
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;
2859 case GTK_RC_TOKEN_STYLE:
2860 return gtk_rc_parse_style (context, scanner);
2862 case GTK_RC_TOKEN_BINDING:
2863 return _gtk_binding_parse_binding (scanner);
2865 case GTK_RC_TOKEN_PIXMAP_PATH:
2866 return gtk_rc_parse_pixmap_path (context, scanner);
2868 case GTK_RC_TOKEN_WIDGET:
2869 return gtk_rc_parse_path_pattern (context, scanner);
2871 case GTK_RC_TOKEN_WIDGET_CLASS:
2872 return gtk_rc_parse_path_pattern (context, scanner);
2874 case GTK_RC_TOKEN_CLASS:
2875 return gtk_rc_parse_path_pattern (context, scanner);
2877 case GTK_RC_TOKEN_MODULE_PATH:
2878 return gtk_rc_parse_module_path (scanner);
2880 case GTK_RC_TOKEN_IM_MODULE_FILE:
2881 return gtk_rc_parse_im_module_file (scanner);
2883 case G_TOKEN_IDENTIFIER:
2884 if (is_c_identifier (scanner->next_value.v_identifier))
2886 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2889 g_scanner_get_next_token (scanner); /* eat identifier */
2890 name = g_strdup (scanner->value.v_identifier);
2892 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2893 if (token == G_TOKEN_NONE)
2895 GtkSettingsValue svalue;
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,
2904 g_free (prop.origin);
2905 if (G_VALUE_TYPE (&prop.value))
2906 g_value_unset (&prop.value);
2913 g_scanner_get_next_token (scanner);
2914 return G_TOKEN_IDENTIFIER;
2917 g_scanner_get_next_token (scanner);
2918 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2923 fixup_rc_set (GSList *list,
2929 GtkRcSet *set = list->data;
2930 if (set->rc_style == orig)
2931 set->rc_style = new;
2937 fixup_rc_sets (GtkRcContext *context,
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);
2947 gtk_rc_parse_style (GtkRcContext *context,
2950 GtkRcStyle *rc_style;
2951 GtkRcStyle *orig_style;
2952 GtkRcStyle *parent_style = NULL;
2953 GtkRcStylePrivate *rc_priv = NULL;
2956 GtkIconFactory *our_factory = NULL;
2957 GHashTable *our_hash = NULL;
2959 token = g_scanner_get_next_token (scanner);
2960 if (token != GTK_RC_TOKEN_STYLE)
2961 return GTK_RC_TOKEN_STYLE;
2963 token = g_scanner_get_next_token (scanner);
2964 if (token != G_TOKEN_STRING)
2965 return G_TOKEN_STRING;
2967 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2969 orig_style = g_object_ref (rc_style);
2975 rc_style = gtk_rc_style_new ();
2976 rc_style->name = g_strdup (scanner->value.v_string);
2978 for (i = 0; i < 5; i++)
2979 rc_style->bg_pixmap_name[i] = NULL;
2981 for (i = 0; i < 5; i++)
2982 rc_style->color_flags[i] = 0;
2985 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
2987 /* If there's a list, its first member is always the factory belonging
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;
2995 token = g_scanner_peek_next_token (scanner);
2996 if (token == G_TOKEN_EQUAL_SIGN)
2998 token = g_scanner_get_next_token (scanner);
3000 token = g_scanner_get_next_token (scanner);
3001 if (token != G_TOKEN_STRING)
3003 token = G_TOKEN_STRING;
3007 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3010 for (i = 0; i < 5; i++)
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];
3019 rc_style->xthickness = parent_style->xthickness;
3020 rc_style->ythickness = parent_style->ythickness;
3022 if (parent_style->font_desc)
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);
3029 if (parent_style->rc_properties)
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),
3039 for (i = 0; i < 5; i++)
3041 if (rc_style->bg_pixmap_name[i])
3042 g_free (rc_style->bg_pixmap_name[i]);
3043 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3048 /* get icon_factories and color_hashes from the parent style;
3049 * if the parent_style doesn't have color_hashes, initializes
3050 * the color_hashes with the settings' color scheme (if it exists)
3052 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3054 if (rc_style->icon_factories)
3055 our_factory = rc_style->icon_factories->data;
3056 if (rc_priv->color_hashes)
3057 our_hash = rc_priv->color_hashes->data;
3059 token = g_scanner_get_next_token (scanner);
3060 if (token != G_TOKEN_LEFT_CURLY)
3062 token = G_TOKEN_LEFT_CURLY;
3066 token = g_scanner_peek_next_token (scanner);
3067 while (token != G_TOKEN_RIGHT_CURLY)
3071 case GTK_RC_TOKEN_BG:
3072 token = gtk_rc_parse_bg (scanner, rc_style);
3074 case GTK_RC_TOKEN_FG:
3075 token = gtk_rc_parse_fg (scanner, rc_style);
3077 case GTK_RC_TOKEN_TEXT:
3078 token = gtk_rc_parse_text (scanner, rc_style);
3080 case GTK_RC_TOKEN_BASE:
3081 token = gtk_rc_parse_base (scanner, rc_style);
3083 case GTK_RC_TOKEN_XTHICKNESS:
3084 token = gtk_rc_parse_xthickness (scanner, rc_style);
3086 case GTK_RC_TOKEN_YTHICKNESS:
3087 token = gtk_rc_parse_ythickness (scanner, rc_style);
3089 case GTK_RC_TOKEN_BG_PIXMAP:
3090 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3092 case GTK_RC_TOKEN_FONT:
3093 token = gtk_rc_parse_font (scanner, rc_style);
3095 case GTK_RC_TOKEN_FONTSET:
3096 token = gtk_rc_parse_fontset (scanner, rc_style);
3098 case GTK_RC_TOKEN_FONT_NAME:
3099 token = gtk_rc_parse_font_name (scanner, rc_style);
3101 case GTK_RC_TOKEN_ENGINE:
3102 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3104 case GTK_RC_TOKEN_STOCK:
3105 if (our_factory == NULL)
3106 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3107 our_factory = rc_style->icon_factories->data;
3108 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3110 case GTK_RC_TOKEN_COLOR:
3111 if (our_hash == NULL)
3112 gtk_rc_style_prepend_empty_color_hash (rc_style);
3113 our_hash = rc_priv->color_hashes->data;
3114 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3116 case G_TOKEN_IDENTIFIER:
3117 if (is_c_identifier (scanner->next_value.v_identifier) &&
3118 scanner->next_value.v_identifier[0] >= 'A' &&
3119 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
3121 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3124 g_scanner_get_next_token (scanner); /* eat type name */
3125 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3126 if (g_scanner_get_next_token (scanner) != ':' ||
3127 g_scanner_get_next_token (scanner) != ':')
3132 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3133 !is_c_identifier (scanner->value.v_identifier))
3135 token = G_TOKEN_IDENTIFIER;
3139 /* it's important that we do the same canonification as GParamSpecPool here */
3140 name = g_strdup (scanner->value.v_identifier);
3141 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3142 prop.property_name = g_quark_from_string (name);
3145 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3146 if (token == G_TOKEN_NONE)
3148 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3149 insert_rc_property (rc_style, &prop, TRUE);
3152 g_free (prop.origin);
3153 if (G_VALUE_TYPE (&prop.value))
3154 g_value_unset (&prop.value);
3158 g_scanner_get_next_token (scanner);
3159 token = G_TOKEN_IDENTIFIER;
3163 g_scanner_get_next_token (scanner);
3164 token = G_TOKEN_RIGHT_CURLY;
3168 if (token != G_TOKEN_NONE)
3171 token = g_scanner_peek_next_token (scanner);
3172 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3174 token = g_scanner_get_next_token (scanner);
3175 if (token != G_TOKEN_RIGHT_CURLY)
3177 token = G_TOKEN_RIGHT_CURLY;
3181 if (rc_style != orig_style)
3183 if (!context->rc_style_ht)
3184 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3185 (GEqualFunc) gtk_rc_style_equal);
3187 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3189 /* If we copied the data into a new rc style, fix up references to the old rc style
3190 * in bindings that we have.
3193 fixup_rc_sets (context, orig_style, rc_style);
3197 g_object_unref (orig_style);
3199 return G_TOKEN_NONE;
3202 if (rc_style != orig_style)
3203 gtk_rc_style_unref (rc_style);
3206 g_object_unref (orig_style);
3211 const GtkRcProperty*
3212 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3214 GQuark property_name)
3216 GtkRcProperty *node = NULL;
3218 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3220 if (rc_style->rc_properties)
3224 key.type_name = type_name;
3225 key.property_name = property_name;
3227 node = bsearch (&key,
3228 rc_style->rc_properties->data, rc_style->rc_properties->len,
3229 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3236 gtk_rc_parse_bg (GScanner *scanner,
3242 token = g_scanner_get_next_token (scanner);
3243 if (token != GTK_RC_TOKEN_BG)
3244 return GTK_RC_TOKEN_BG;
3246 token = gtk_rc_parse_state (scanner, &state);
3247 if (token != G_TOKEN_NONE)
3250 token = g_scanner_get_next_token (scanner);
3251 if (token != G_TOKEN_EQUAL_SIGN)
3252 return G_TOKEN_EQUAL_SIGN;
3254 style->color_flags[state] |= GTK_RC_BG;
3255 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3259 gtk_rc_parse_fg (GScanner *scanner,
3265 token = g_scanner_get_next_token (scanner);
3266 if (token != GTK_RC_TOKEN_FG)
3267 return GTK_RC_TOKEN_FG;
3269 token = gtk_rc_parse_state (scanner, &state);
3270 if (token != G_TOKEN_NONE)
3273 token = g_scanner_get_next_token (scanner);
3274 if (token != G_TOKEN_EQUAL_SIGN)
3275 return G_TOKEN_EQUAL_SIGN;
3277 style->color_flags[state] |= GTK_RC_FG;
3278 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3282 gtk_rc_parse_text (GScanner *scanner,
3288 token = g_scanner_get_next_token (scanner);
3289 if (token != GTK_RC_TOKEN_TEXT)
3290 return GTK_RC_TOKEN_TEXT;
3292 token = gtk_rc_parse_state (scanner, &state);
3293 if (token != G_TOKEN_NONE)
3296 token = g_scanner_get_next_token (scanner);
3297 if (token != G_TOKEN_EQUAL_SIGN)
3298 return G_TOKEN_EQUAL_SIGN;
3300 style->color_flags[state] |= GTK_RC_TEXT;
3301 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3305 gtk_rc_parse_base (GScanner *scanner,
3311 token = g_scanner_get_next_token (scanner);
3312 if (token != GTK_RC_TOKEN_BASE)
3313 return GTK_RC_TOKEN_BASE;
3315 token = gtk_rc_parse_state (scanner, &state);
3316 if (token != G_TOKEN_NONE)
3319 token = g_scanner_get_next_token (scanner);
3320 if (token != G_TOKEN_EQUAL_SIGN)
3321 return G_TOKEN_EQUAL_SIGN;
3323 style->color_flags[state] |= GTK_RC_BASE;
3324 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3328 gtk_rc_parse_xthickness (GScanner *scanner,
3331 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3332 return GTK_RC_TOKEN_XTHICKNESS;
3334 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3335 return G_TOKEN_EQUAL_SIGN;
3337 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3340 style->xthickness = scanner->value.v_int;
3342 return G_TOKEN_NONE;
3346 gtk_rc_parse_ythickness (GScanner *scanner,
3349 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3350 return GTK_RC_TOKEN_YTHICKNESS;
3352 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3353 return G_TOKEN_EQUAL_SIGN;
3355 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3358 style->ythickness = scanner->value.v_int;
3360 return G_TOKEN_NONE;
3364 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3366 GtkRcStyle *rc_style)
3372 token = g_scanner_get_next_token (scanner);
3373 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3374 return GTK_RC_TOKEN_BG_PIXMAP;
3376 token = gtk_rc_parse_state (scanner, &state);
3377 if (token != G_TOKEN_NONE)
3380 token = g_scanner_get_next_token (scanner);
3381 if (token != G_TOKEN_EQUAL_SIGN)
3382 return G_TOKEN_EQUAL_SIGN;
3384 token = g_scanner_get_next_token (scanner);
3385 if (token != G_TOKEN_STRING)
3386 return G_TOKEN_STRING;
3388 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3389 (strcmp (scanner->value.v_string, "<none>") == 0))
3390 pixmap_file = g_strdup (scanner->value.v_string);
3392 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3393 scanner, scanner->value.v_string);
3397 if (rc_style->bg_pixmap_name[state])
3398 g_free (rc_style->bg_pixmap_name[state]);
3399 rc_style->bg_pixmap_name[state] = pixmap_file;
3402 return G_TOKEN_NONE;
3406 gtk_rc_check_pixmap_dir (const gchar *dir,
3407 const gchar *pixmap_file)
3411 buf = g_build_filename (dir, pixmap_file, NULL);
3413 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3422 * gtk_rc_find_pixmap_in_path:
3423 * @settings: a #GtkSettings
3424 * @scanner: Scanner used to get line number information for the
3425 * warning message, or %NULL
3426 * @pixmap_file: name of the pixmap file to locate.
3428 * Looks up a file in pixmap path for the specified #GtkSettings.
3429 * If the file is not found, it outputs a warning message using
3430 * g_warning() and returns %NULL.
3432 * Return value: the filename.
3435 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3437 const gchar *pixmap_file)
3443 GtkRcContext *context = gtk_rc_context_get (settings);
3445 if (context->pixmap_path)
3446 for (i = 0; context->pixmap_path[i] != NULL; i++)
3448 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3453 tmp_list = current_files_stack;
3456 GtkRcFile *curfile = tmp_list->data;
3457 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3461 tmp_list = tmp_list->next;
3465 g_scanner_warn (scanner,
3466 _("Unable to locate image file in pixmap_path: \"%s\""),
3469 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3476 * gtk_rc_find_module_in_path:
3477 * @module_file: name of a theme engine
3479 * Searches for a theme engine in the GTK+ search path. This function
3480 * is not useful for applications and should not be used.
3482 * Return value: The filename, if found (must be freed with g_free()),
3486 gtk_rc_find_module_in_path (const gchar *module_file)
3488 return _gtk_find_module (module_file, "engines");
3492 gtk_rc_parse_font (GScanner *scanner,
3493 GtkRcStyle *rc_style)
3497 token = g_scanner_get_next_token (scanner);
3498 if (token != GTK_RC_TOKEN_FONT)
3499 return GTK_RC_TOKEN_FONT;
3501 token = g_scanner_get_next_token (scanner);
3502 if (token != G_TOKEN_EQUAL_SIGN)
3503 return G_TOKEN_EQUAL_SIGN;
3505 token = g_scanner_get_next_token (scanner);
3506 if (token != G_TOKEN_STRING)
3507 return G_TOKEN_STRING;
3509 /* Ignore, do nothing */
3511 return G_TOKEN_NONE;
3515 gtk_rc_parse_fontset (GScanner *scanner,
3516 GtkRcStyle *rc_style)
3520 token = g_scanner_get_next_token (scanner);
3521 if (token != GTK_RC_TOKEN_FONTSET)
3522 return GTK_RC_TOKEN_FONTSET;
3524 token = g_scanner_get_next_token (scanner);
3525 if (token != G_TOKEN_EQUAL_SIGN)
3526 return G_TOKEN_EQUAL_SIGN;
3528 token = g_scanner_get_next_token (scanner);
3529 if (token != G_TOKEN_STRING)
3530 return G_TOKEN_STRING;
3532 /* Do nothing - silently ignore */
3534 return G_TOKEN_NONE;
3538 gtk_rc_parse_font_name (GScanner *scanner,
3539 GtkRcStyle *rc_style)
3543 token = g_scanner_get_next_token (scanner);
3544 if (token != GTK_RC_TOKEN_FONT_NAME)
3545 return GTK_RC_TOKEN_FONT;
3547 token = g_scanner_get_next_token (scanner);
3548 if (token != G_TOKEN_EQUAL_SIGN)
3549 return G_TOKEN_EQUAL_SIGN;
3551 token = g_scanner_get_next_token (scanner);
3552 if (token != G_TOKEN_STRING)
3553 return G_TOKEN_STRING;
3555 if (rc_style->font_desc)
3556 pango_font_description_free (rc_style->font_desc);
3558 rc_style->font_desc =
3559 pango_font_description_from_string (scanner->value.v_string);
3561 return G_TOKEN_NONE;
3565 gtk_rc_parse_engine (GtkRcContext *context,
3567 GtkRcStyle **rc_style)
3570 GtkThemeEngine *engine;
3571 guint result = G_TOKEN_NONE;
3572 GtkRcStyle *new_style = NULL;
3573 gboolean parsed_curlies = FALSE;
3575 token = g_scanner_get_next_token (scanner);
3576 if (token != GTK_RC_TOKEN_ENGINE)
3577 return GTK_RC_TOKEN_ENGINE;
3579 token = g_scanner_get_next_token (scanner);
3580 if (token != G_TOKEN_STRING)
3581 return G_TOKEN_STRING;
3583 if (!scanner->value.v_string[0])
3585 /* Support engine "" {} to mean override to the default engine
3587 token = g_scanner_get_next_token (scanner);
3588 if (token != G_TOKEN_LEFT_CURLY)
3589 return G_TOKEN_LEFT_CURLY;
3591 token = g_scanner_get_next_token (scanner);
3592 if (token != G_TOKEN_RIGHT_CURLY)
3593 return G_TOKEN_RIGHT_CURLY;
3595 parsed_curlies = TRUE;
3597 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3599 new_style = gtk_rc_style_new ();
3600 gtk_rc_style_real_merge (new_style, *rc_style);
3602 if ((*rc_style)->name)
3603 new_style->name = g_strdup ((*rc_style)->name);
3606 (*rc_style)->engine_specified = TRUE;
3610 engine = gtk_theme_engine_get (scanner->value.v_string);
3612 token = g_scanner_get_next_token (scanner);
3613 if (token != G_TOKEN_LEFT_CURLY)
3614 return G_TOKEN_LEFT_CURLY;
3618 GtkRcStyleClass *new_class;
3620 new_style = gtk_theme_engine_create_rc_style (engine);
3621 g_type_module_unuse (G_TYPE_MODULE (engine));
3623 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3625 new_class->merge (new_style, *rc_style);
3626 if ((*rc_style)->name)
3627 new_style->name = g_strdup ((*rc_style)->name);
3629 if (new_class->parse)
3631 parsed_curlies = TRUE;
3632 result = new_class->parse (new_style, context->settings, scanner);
3634 if (result != G_TOKEN_NONE)
3636 g_object_unref (new_style);
3643 if (!parsed_curlies)
3645 /* Skip over remainder, looking for nested {}'s
3649 result = G_TOKEN_RIGHT_CURLY;
3650 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3652 if (token == G_TOKEN_LEFT_CURLY)
3654 else if (token == G_TOKEN_RIGHT_CURLY)
3659 result = G_TOKEN_NONE;
3667 GtkRcStylePrivate *rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3668 GtkRcStylePrivate *new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3670 /* take over icon factories and color hashes from the to-be-deleted style
3673 new_style->icon_factories = (*rc_style)->icon_factories;
3674 (*rc_style)->icon_factories = NULL;
3676 new_priv->color_hashes = rc_priv->color_hashes;
3677 rc_priv->color_hashes = NULL;
3679 new_style->engine_specified = TRUE;
3681 g_object_unref (*rc_style);
3682 *rc_style = new_style;
3689 gtk_rc_parse_state (GScanner *scanner,
3690 GtkStateType *state)
3695 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3696 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3698 /* we don't know where we got called from, so we reset the scope here.
3699 * if we bail out due to errors, we *don't* reset the scope, so the
3700 * error messaging code can make sense of our tokens.
3702 old_scope = g_scanner_set_scope (scanner, 0);
3704 token = g_scanner_get_next_token (scanner);
3705 if (token != G_TOKEN_LEFT_BRACE)
3706 return G_TOKEN_LEFT_BRACE;
3708 token = g_scanner_get_next_token (scanner);
3711 case GTK_RC_TOKEN_ACTIVE:
3712 *state = GTK_STATE_ACTIVE;
3714 case GTK_RC_TOKEN_INSENSITIVE:
3715 *state = GTK_STATE_INSENSITIVE;
3717 case GTK_RC_TOKEN_NORMAL:
3718 *state = GTK_STATE_NORMAL;
3720 case GTK_RC_TOKEN_PRELIGHT:
3721 *state = GTK_STATE_PRELIGHT;
3723 case GTK_RC_TOKEN_SELECTED:
3724 *state = GTK_STATE_SELECTED;
3727 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3730 token = g_scanner_get_next_token (scanner);
3731 if (token != G_TOKEN_RIGHT_BRACE)
3732 return G_TOKEN_RIGHT_BRACE;
3734 g_scanner_set_scope (scanner, old_scope);
3736 return G_TOKEN_NONE;
3740 gtk_rc_parse_priority (GScanner *scanner,
3741 GtkPathPriorityType *priority)
3746 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3747 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3749 /* we don't know where we got called from, so we reset the scope here.
3750 * if we bail out due to errors, we *don't* reset the scope, so the
3751 * error messaging code can make sense of our tokens.
3753 old_scope = g_scanner_set_scope (scanner, 0);
3755 token = g_scanner_get_next_token (scanner);
3759 token = g_scanner_get_next_token (scanner);
3762 case GTK_RC_TOKEN_LOWEST:
3763 *priority = GTK_PATH_PRIO_LOWEST;
3765 case GTK_RC_TOKEN_GTK:
3766 *priority = GTK_PATH_PRIO_GTK;
3768 case GTK_RC_TOKEN_APPLICATION:
3769 *priority = GTK_PATH_PRIO_APPLICATION;
3771 case GTK_RC_TOKEN_THEME:
3772 *priority = GTK_PATH_PRIO_THEME;
3774 case GTK_RC_TOKEN_RC:
3775 *priority = GTK_PATH_PRIO_RC;
3777 case GTK_RC_TOKEN_HIGHEST:
3778 *priority = GTK_PATH_PRIO_HIGHEST;
3781 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3784 g_scanner_set_scope (scanner, old_scope);
3786 return G_TOKEN_NONE;
3790 gtk_rc_parse_color (GScanner *scanner,
3793 return gtk_rc_parse_color_full (scanner, NULL, color);
3797 gtk_rc_parse_color_full (GScanner *scanner,
3803 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3805 /* we don't need to set our own scope here, because
3806 * we don't need own symbols
3809 token = g_scanner_get_next_token (scanner);
3817 case G_TOKEN_LEFT_CURLY:
3818 token = g_scanner_get_next_token (scanner);
3819 if (token == G_TOKEN_INT)
3820 token_int = scanner->value.v_int;
3821 else if (token == G_TOKEN_FLOAT)
3822 token_int = scanner->value.v_float * 65535.0;
3824 return G_TOKEN_FLOAT;
3825 color->red = CLAMP (token_int, 0, 65535);
3827 token = g_scanner_get_next_token (scanner);
3828 if (token != G_TOKEN_COMMA)
3829 return G_TOKEN_COMMA;
3831 token = g_scanner_get_next_token (scanner);
3832 if (token == G_TOKEN_INT)
3833 token_int = scanner->value.v_int;
3834 else if (token == G_TOKEN_FLOAT)
3835 token_int = scanner->value.v_float * 65535.0;
3837 return G_TOKEN_FLOAT;
3838 color->green = CLAMP (token_int, 0, 65535);
3840 token = g_scanner_get_next_token (scanner);
3841 if (token != G_TOKEN_COMMA)
3842 return G_TOKEN_COMMA;
3844 token = g_scanner_get_next_token (scanner);
3845 if (token == G_TOKEN_INT)
3846 token_int = scanner->value.v_int;
3847 else if (token == G_TOKEN_FLOAT)
3848 token_int = scanner->value.v_float * 65535.0;
3850 return G_TOKEN_FLOAT;
3851 color->blue = CLAMP (token_int, 0, 65535);
3853 token = g_scanner_get_next_token (scanner);
3854 if (token != G_TOKEN_RIGHT_CURLY)
3855 return G_TOKEN_RIGHT_CURLY;
3856 return G_TOKEN_NONE;
3858 case G_TOKEN_STRING:
3859 if (!gdk_color_parse (scanner->value.v_string, color))
3861 g_scanner_warn (scanner, "Invalid color constant '%s'",
3862 scanner->value.v_string);
3863 return G_TOKEN_STRING;
3865 return G_TOKEN_NONE;
3868 token = g_scanner_get_next_token (scanner);
3869 if (token != G_TOKEN_IDENTIFIER)
3870 return G_TOKEN_IDENTIFIER;
3872 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3874 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3875 scanner->value.v_identifier);
3876 return G_TOKEN_IDENTIFIER;
3879 return G_TOKEN_NONE;
3881 case G_TOKEN_IDENTIFIER:
3882 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3884 token = g_scanner_get_next_token (scanner);
3885 if (token != G_TOKEN_LEFT_PAREN)
3886 return G_TOKEN_LEFT_PAREN;
3889 if (g_scanner_peek_next_token (scanner) == '-')
3891 g_scanner_get_next_token (scanner); /* eat sign */
3895 token = g_scanner_get_next_token (scanner);
3896 if (token != G_TOKEN_FLOAT)
3897 return G_TOKEN_FLOAT;
3899 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3901 token = g_scanner_get_next_token (scanner);
3902 if (token != G_TOKEN_COMMA)
3903 return G_TOKEN_COMMA;
3905 token = gtk_rc_parse_color_full (scanner, style, &c1);
3906 if (token != G_TOKEN_NONE)
3909 token = g_scanner_get_next_token (scanner);
3910 if (token != G_TOKEN_COMMA)
3911 return G_TOKEN_COMMA;
3913 token = gtk_rc_parse_color_full (scanner, style, &c2);
3914 if (token != G_TOKEN_NONE)
3917 token = g_scanner_get_next_token (scanner);
3918 if (token != G_TOKEN_RIGHT_PAREN)
3919 return G_TOKEN_RIGHT_PAREN;
3921 color->red = l * c1.red + (1.0 - l) * c2.red;
3922 color->green = l * c1.green + (1.0 - l) * c2.green;
3923 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3925 return G_TOKEN_NONE;
3927 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
3929 token = g_scanner_get_next_token (scanner);
3930 if (token != G_TOKEN_LEFT_PAREN)
3931 return G_TOKEN_LEFT_PAREN;
3934 if (g_scanner_peek_next_token (scanner) == '-')
3936 g_scanner_get_next_token (scanner); /* eat sign */
3940 token = g_scanner_get_next_token (scanner);
3941 if (token != G_TOKEN_FLOAT)
3942 return G_TOKEN_FLOAT;
3944 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3946 token = g_scanner_get_next_token (scanner);
3947 if (token != G_TOKEN_COMMA)
3948 return G_TOKEN_COMMA;
3950 token = gtk_rc_parse_color_full (scanner, style, &c1);
3951 if (token != G_TOKEN_NONE)
3954 token = g_scanner_get_next_token (scanner);
3955 if (token != G_TOKEN_RIGHT_PAREN)
3956 return G_TOKEN_RIGHT_PAREN;
3958 _gtk_style_shade (&c1, color, l);
3960 return G_TOKEN_NONE;
3962 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
3963 strcmp (scanner->value.v_identifier, "darker") == 0)
3965 if (scanner->value.v_identifier[0] == 'l')
3970 token = g_scanner_get_next_token (scanner);
3971 if (token != G_TOKEN_LEFT_PAREN)
3972 return G_TOKEN_LEFT_PAREN;
3974 token = gtk_rc_parse_color_full (scanner, style, &c1);
3975 if (token != G_TOKEN_NONE)
3978 token = g_scanner_get_next_token (scanner);
3979 if (token != G_TOKEN_RIGHT_PAREN)
3980 return G_TOKEN_RIGHT_PAREN;
3982 _gtk_style_shade (&c1, color, l);
3984 return G_TOKEN_NONE;
3987 return G_TOKEN_IDENTIFIER;
3990 return G_TOKEN_STRING;
3995 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4000 token = g_scanner_get_next_token (scanner);
4001 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4002 return GTK_RC_TOKEN_PIXMAP_PATH;
4004 token = g_scanner_get_next_token (scanner);
4005 if (token != G_TOKEN_STRING)
4006 return G_TOKEN_STRING;
4008 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4010 return G_TOKEN_NONE;
4014 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4016 const gchar *pix_path)
4018 g_strfreev (context->pixmap_path);
4019 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4023 gtk_rc_parse_module_path (GScanner *scanner)
4027 token = g_scanner_get_next_token (scanner);
4028 if (token != GTK_RC_TOKEN_MODULE_PATH)
4029 return GTK_RC_TOKEN_MODULE_PATH;
4031 token = g_scanner_get_next_token (scanner);
4032 if (token != G_TOKEN_STRING)
4033 return G_TOKEN_STRING;
4035 g_warning ("module_path directive is now ignored\n");
4037 return G_TOKEN_NONE;
4041 gtk_rc_parse_im_module_file (GScanner *scanner)
4045 token = g_scanner_get_next_token (scanner);
4046 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4047 return GTK_RC_TOKEN_IM_MODULE_FILE;
4049 token = g_scanner_get_next_token (scanner);
4050 if (token != G_TOKEN_STRING)
4051 return G_TOKEN_STRING;
4054 g_free (im_module_file);
4056 im_module_file = g_strdup (scanner->value.v_string);
4058 return G_TOKEN_NONE;
4062 gtk_rc_parse_path_pattern (GtkRcContext *context,
4066 GtkPathType path_type;
4068 gboolean is_binding;
4069 GtkPathPriorityType priority = context->default_priority;
4071 token = g_scanner_get_next_token (scanner);
4074 case GTK_RC_TOKEN_WIDGET:
4075 path_type = GTK_PATH_WIDGET;
4077 case GTK_RC_TOKEN_WIDGET_CLASS:
4078 path_type = GTK_PATH_WIDGET_CLASS;
4080 case GTK_RC_TOKEN_CLASS:
4081 path_type = GTK_PATH_CLASS;
4084 return GTK_RC_TOKEN_WIDGET_CLASS;
4087 token = g_scanner_get_next_token (scanner);
4088 if (token != G_TOKEN_STRING)
4089 return G_TOKEN_STRING;
4091 pattern = g_strdup (scanner->value.v_string);
4093 token = g_scanner_get_next_token (scanner);
4094 if (token == GTK_RC_TOKEN_STYLE)
4096 else if (token == GTK_RC_TOKEN_BINDING)
4101 return GTK_RC_TOKEN_STYLE;
4104 if (g_scanner_peek_next_token (scanner) == ':')
4106 token = gtk_rc_parse_priority (scanner, &priority);
4107 if (token != G_TOKEN_NONE)
4114 token = g_scanner_get_next_token (scanner);
4115 if (token != G_TOKEN_STRING)
4118 return G_TOKEN_STRING;
4123 GtkBindingSet *binding;
4125 binding = gtk_binding_set_find (scanner->value.v_string);
4129 return G_TOKEN_STRING;
4131 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4135 GtkRcStyle *rc_style;
4138 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4143 return G_TOKEN_STRING;
4146 rc_set = g_new (GtkRcSet, 1);
4147 rc_set->type = path_type;
4149 if (path_type == GTK_PATH_WIDGET_CLASS)
4151 rc_set->pspec = NULL;
4152 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4156 rc_set->pspec = g_pattern_spec_new (pattern);
4157 rc_set->path = NULL;
4160 rc_set->rc_style = rc_style;
4161 rc_set->priority = priority;
4163 if (path_type == GTK_PATH_WIDGET)
4164 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4165 else if (path_type == GTK_PATH_WIDGET_CLASS)
4166 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4168 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4172 return G_TOKEN_NONE;
4176 gtk_rc_parse_hash_key (GScanner *scanner,
4181 token = g_scanner_get_next_token (scanner);
4182 if (token != G_TOKEN_LEFT_BRACE)
4183 return G_TOKEN_LEFT_BRACE;
4185 token = g_scanner_get_next_token (scanner);
4187 if (token != G_TOKEN_STRING)
4188 return G_TOKEN_STRING;
4190 *hash_key = g_strdup (scanner->value.v_string);
4192 token = g_scanner_get_next_token (scanner);
4193 if (token != G_TOKEN_RIGHT_BRACE)
4196 return G_TOKEN_RIGHT_BRACE;
4199 return G_TOKEN_NONE;
4203 gtk_rc_parse_icon_source (GtkRcContext *context,
4205 GtkIconSet *icon_set,
4206 gboolean *icon_set_valid)
4209 gchar *full_filename;
4210 GtkIconSource *source = NULL;
4212 token = g_scanner_get_next_token (scanner);
4213 if (token != G_TOKEN_LEFT_CURLY)
4214 return G_TOKEN_LEFT_CURLY;
4216 token = g_scanner_get_next_token (scanner);
4218 if (token != G_TOKEN_STRING && token != '@')
4219 return G_TOKEN_STRING;
4221 if (token == G_TOKEN_STRING)
4225 source = gtk_icon_source_new ();
4226 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4229 gtk_icon_source_set_filename (source, full_filename);
4230 g_free (full_filename);
4237 token = g_scanner_get_next_token (scanner);
4239 if (token != G_TOKEN_STRING)
4240 return G_TOKEN_STRING;
4242 source = gtk_icon_source_new ();
4243 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4246 /* We continue parsing even if we didn't find the pixmap so that rest of the
4247 * file is read, even if the syntax is bad. However we don't validate the
4248 * icon_set so the caller can choose not to install it.
4250 token = g_scanner_get_next_token (scanner);
4252 if (token == G_TOKEN_RIGHT_CURLY)
4254 else if (token != G_TOKEN_COMMA)
4256 gtk_icon_source_free (source);
4257 return G_TOKEN_COMMA;
4260 /* Get the direction */
4262 token = g_scanner_get_next_token (scanner);
4266 case GTK_RC_TOKEN_RTL:
4267 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4268 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4271 case GTK_RC_TOKEN_LTR:
4272 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4273 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4280 gtk_icon_source_free (source);
4281 return GTK_RC_TOKEN_RTL;
4285 token = g_scanner_get_next_token (scanner);
4287 if (token == G_TOKEN_RIGHT_CURLY)
4289 else if (token != G_TOKEN_COMMA)
4291 gtk_icon_source_free (source);
4292 return G_TOKEN_COMMA;
4297 token = g_scanner_get_next_token (scanner);
4301 case GTK_RC_TOKEN_NORMAL:
4302 gtk_icon_source_set_state_wildcarded (source, FALSE);
4303 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4306 case GTK_RC_TOKEN_PRELIGHT:
4307 gtk_icon_source_set_state_wildcarded (source, FALSE);
4308 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4312 case GTK_RC_TOKEN_INSENSITIVE:
4313 gtk_icon_source_set_state_wildcarded (source, FALSE);
4314 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4317 case GTK_RC_TOKEN_ACTIVE:
4318 gtk_icon_source_set_state_wildcarded (source, FALSE);
4319 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4322 case GTK_RC_TOKEN_SELECTED:
4323 gtk_icon_source_set_state_wildcarded (source, FALSE);
4324 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4331 gtk_icon_source_free (source);
4332 return GTK_RC_TOKEN_PRELIGHT;
4336 token = g_scanner_get_next_token (scanner);
4338 if (token == G_TOKEN_RIGHT_CURLY)
4340 else if (token != G_TOKEN_COMMA)
4342 gtk_icon_source_free (source);
4343 return G_TOKEN_COMMA;
4348 token = g_scanner_get_next_token (scanner);
4354 if (token != G_TOKEN_STRING)
4356 gtk_icon_source_free (source);
4357 return G_TOKEN_STRING;
4360 size = gtk_icon_size_from_name (scanner->value.v_string);
4362 if (size != GTK_ICON_SIZE_INVALID)
4364 gtk_icon_source_set_size_wildcarded (source, FALSE);
4365 gtk_icon_source_set_size (source, size);
4369 /* Check the close brace */
4371 token = g_scanner_get_next_token (scanner);
4372 if (token != G_TOKEN_RIGHT_CURLY)
4374 gtk_icon_source_free (source);
4375 return G_TOKEN_RIGHT_CURLY;
4379 if (gtk_icon_source_get_filename (source) ||
4380 gtk_icon_source_get_icon_name (source))
4382 gtk_icon_set_add_source (icon_set, source);
4383 *icon_set_valid = TRUE;
4385 gtk_icon_source_free (source);
4387 return G_TOKEN_NONE;
4391 gtk_rc_parse_stock (GtkRcContext *context,
4393 GtkRcStyle *rc_style,
4394 GtkIconFactory *factory)
4396 GtkIconSet *icon_set = NULL;
4397 gboolean icon_set_valid = FALSE;
4398 gchar *stock_id = NULL;
4401 token = g_scanner_get_next_token (scanner);
4402 if (token != GTK_RC_TOKEN_STOCK)
4403 return GTK_RC_TOKEN_STOCK;
4405 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4406 if (token != G_TOKEN_NONE)
4409 token = g_scanner_get_next_token (scanner);
4410 if (token != G_TOKEN_EQUAL_SIGN)
4413 return G_TOKEN_EQUAL_SIGN;
4416 token = g_scanner_get_next_token (scanner);
4417 if (token != G_TOKEN_LEFT_CURLY)
4420 return G_TOKEN_LEFT_CURLY;
4423 token = g_scanner_peek_next_token (scanner);
4424 while (token != G_TOKEN_RIGHT_CURLY)
4426 if (icon_set == NULL)
4427 icon_set = gtk_icon_set_new ();
4429 token = gtk_rc_parse_icon_source (context,
4430 scanner, icon_set, &icon_set_valid);
4431 if (token != G_TOKEN_NONE)
4434 gtk_icon_set_unref (icon_set);
4438 token = g_scanner_get_next_token (scanner);
4440 if (token != G_TOKEN_COMMA &&
4441 token != G_TOKEN_RIGHT_CURLY)
4444 gtk_icon_set_unref (icon_set);
4445 return G_TOKEN_RIGHT_CURLY;
4452 gtk_icon_factory_add (factory,
4456 gtk_icon_set_unref (icon_set);
4461 return G_TOKEN_NONE;
4465 gtk_rc_parse_logical_color (GScanner *scanner,
4466 GtkRcStyle *rc_style,
4469 gchar *color_id = NULL;
4473 token = g_scanner_get_next_token (scanner);
4474 if (token != GTK_RC_TOKEN_COLOR)
4475 return GTK_RC_TOKEN_COLOR;
4477 token = gtk_rc_parse_hash_key (scanner, &color_id);
4478 if (token != G_TOKEN_NONE)
4481 token = g_scanner_get_next_token (scanner);
4482 if (token != G_TOKEN_EQUAL_SIGN)
4485 return G_TOKEN_EQUAL_SIGN;
4488 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4489 if (token != G_TOKEN_NONE)
4495 /* Because the hash is created with destroy functions,
4496 * g_hash_table_insert will free any old values for us,
4497 * if a mapping with the specified key already exists.
4499 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4501 return G_TOKEN_NONE;
4506 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4510 const gchar *current;
4511 const gchar *class_start;
4512 const gchar *class_end;
4513 const gchar *pattern_end;
4514 const gchar *pattern_start;
4519 while ((class_start = strchr (current, '<')) &&
4520 (class_end = strchr (class_start, '>')))
4522 /* Add patterns, but ignore single dots */
4523 if (!(class_start == current ||
4524 (class_start == current + 1 && current[0] == '.')))
4526 pattern_end = class_start - 1;
4527 pattern_start = current;
4529 path_elt = g_new (PathElt, 1);
4531 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4532 path_elt->type = PATH_ELT_PSPEC;
4533 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4534 g_free (sub_pattern);
4536 result = g_slist_prepend (result, path_elt);
4539 path_elt = g_new (PathElt, 1);
4541 /* The < > need to be removed from the string. */
4542 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4544 path_elt->type = PATH_ELT_UNRESOLVED;
4545 path_elt->elt.class_name = sub_pattern;
4547 result = g_slist_prepend (result, path_elt);
4549 current = class_end + 1;
4552 /* Add the rest, if anything is left */
4553 if (strlen (current) > 0)
4555 path_elt = g_new (PathElt, 1);
4556 path_elt->type = PATH_ELT_PSPEC;
4557 path_elt->elt.pspec = g_pattern_spec_new (current);
4559 result = g_slist_prepend (result, path_elt);
4562 return g_slist_reverse (result);
4566 free_path_elt (gpointer data,
4569 PathElt *path_elt = data;
4571 switch (path_elt->type)
4573 case PATH_ELT_PSPEC:
4574 g_pattern_spec_free (path_elt->elt.pspec);
4576 case PATH_ELT_UNRESOLVED:
4577 g_free (path_elt->elt.class_name);
4582 g_assert_not_reached ();
4589 _gtk_rc_free_widget_class_path (GSList *list)
4591 g_slist_foreach (list, free_path_elt, NULL);
4592 g_slist_free (list);
4596 gtk_rc_set_free (GtkRcSet *rc_set)
4599 g_pattern_spec_free (rc_set->pspec);
4601 _gtk_rc_free_widget_class_path (rc_set->path);
4607 match_class (PathElt *path_elt,
4612 if (path_elt->type == PATH_ELT_UNRESOLVED)
4614 type = g_type_from_name (path_elt->elt.class_name);
4615 if (type != G_TYPE_INVALID)
4617 g_free (path_elt->elt.class_name);
4618 path_elt->elt.class_type = type;
4619 path_elt->type = PATH_ELT_TYPE;
4622 return g_str_equal (type_name, path_elt->elt.class_name);
4625 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4629 match_widget_class_recursive (GSList *list,
4632 gchar *path_reversed)
4636 /* break out if we cannot match anymore. */
4645 /* there are two possibilities:
4646 * 1. The next pattern should match the class.
4647 * 2. First normal matching, and then maybe a class */
4649 path_elt = list->data;
4651 if (path_elt->type != PATH_ELT_PSPEC)
4653 gchar *class_start = path;
4656 /* ignore leading dot */
4657 if (class_start[0] == '.')
4659 class_end = strchr (class_start, '.');
4661 if (class_end == NULL)
4663 if (!match_class (path_elt, class_start))
4666 return match_widget_class_recursive (list->next, 0, "", "");
4670 class_end[0] = '\0';
4671 if (!match_class (path_elt, class_start))
4679 gint new_length = length - (class_end - path);
4680 gchar old_char = path_reversed[new_length];
4684 path_reversed[new_length] = '\0';
4685 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4686 path_reversed[new_length] = old_char;
4697 gboolean result = FALSE;
4699 /* If there is nothing after this (ie. no class match),
4700 * just compare the pspec.
4702 if (list->next == NULL)
4703 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4705 class_elt = (PathElt *)list->next->data;
4706 g_assert (class_elt->type != PATH_ELT_PSPEC);
4709 if (class_start[0] == '.')
4714 class_end = strchr (class_start, '.');
4716 /* It should be cheaper to match the class first. (either the pattern
4717 * is simple, and will match most of the times, or it may be complex
4718 * and matching is slow)
4720 if (class_end == NULL)
4722 result = match_class (class_elt, class_start);
4726 class_end[0] = '\0';
4727 result = match_class (class_elt, class_start);
4736 /* terminate the string in front of the class. It does not matter
4737 * that the class becomes unusable, because it is not needed
4738 * inside the recursion
4740 old_char = class_start[0];
4741 class_start[0] = '\0';
4743 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4745 if (class_end != NULL)
4747 gint new_length = length - (class_end - path);
4748 gchar path_reversed_char = path_reversed[new_length];
4750 path_reversed[new_length] = '\0';
4752 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4754 path_reversed[new_length] = path_reversed_char;
4757 result = match_widget_class_recursive (list->next->next, 0, "", "");
4760 class_start[0] = old_char;
4766 /* get next class in path, or break out */
4767 if (class_end != NULL)
4768 class_start = class_end + 1;
4776 _gtk_rc_match_widget_class (GSList *list,
4779 gchar *path_reversed)
4781 return match_widget_class_recursive (list, length, path, path_reversed);
4786 /* DLL ABI stability backward compatibility versions */
4788 #undef gtk_rc_add_default_file
4791 gtk_rc_add_default_file (const gchar *filename)
4793 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4795 gtk_rc_add_default_file_utf8 (utf8_filename);
4797 g_free (utf8_filename);
4800 #undef gtk_rc_set_default_files
4803 gtk_rc_set_default_files (gchar **filenames)
4805 gchar **utf8_filenames;
4808 while (filenames[n++] != NULL)
4811 utf8_filenames = g_new (gchar *, n + 1);
4813 for (i = 0; i < n; i++)
4814 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4816 utf8_filenames[n] = NULL;
4818 gtk_rc_set_default_files_utf8 (utf8_filenames);
4820 g_strfreev (utf8_filenames);
4826 gtk_rc_parse (const gchar *filename)
4828 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4830 gtk_rc_parse_utf8 (utf8_filename);
4832 g_free (utf8_filename);
4837 #define __GTK_RC_C__
4838 #include "gtkaliasdef.c"