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"
63 typedef struct _GtkRcSet GtkRcSet;
64 typedef struct _GtkRcNode GtkRcNode;
65 typedef struct _GtkRcFile GtkRcFile;
100 gchar *canonical_name;
103 guint is_string : 1; /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
109 GHashTable *rc_style_ht;
110 GtkSettings *settings;
111 GSList *rc_sets_widget;
112 GSList *rc_sets_widget_class;
113 GSList *rc_sets_class;
115 /* The files we have parsed, to reread later if necessary */
119 gboolean prefer_dark_theme;
120 gchar *key_theme_name;
125 gint default_priority;
126 GtkStyle *default_style;
128 GHashTable *color_hash;
133 #define GTK_RC_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RC_STYLE, GtkRcStylePrivate))
135 typedef struct _GtkRcStylePrivate GtkRcStylePrivate;
137 struct _GtkRcStylePrivate
139 GSList *color_hashes;
142 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
144 static guint gtk_rc_style_hash (const gchar *name);
145 static gboolean gtk_rc_style_equal (const gchar *a,
147 static guint gtk_rc_styles_hash (const GSList *rc_styles);
148 static gboolean gtk_rc_styles_equal (const GSList *a,
150 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
152 static GSList * gtk_rc_styles_match (GSList *rc_styles,
156 gchar *path_reversed);
157 static GtkStyle * gtk_rc_style_to_style (GtkRcContext *context,
158 GtkRcStyle *rc_style);
159 static GtkStyle* gtk_rc_init_style (GtkRcContext *context,
161 static void gtk_rc_parse_default_files (GtkRcContext *context);
162 static gboolean gtk_rc_parse_named (GtkRcContext *context,
165 const gchar *variant);
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,
222 static void gtk_rc_clear_hash_node (gpointer key,
225 static void gtk_rc_clear_styles (GtkRcContext *context);
226 static void gtk_rc_add_initial_default_files (void);
228 static void gtk_rc_style_finalize (GObject *object);
229 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
231 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
232 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
233 static void gtk_rc_style_copy_icons_and_colors(GtkRcStyle *rc_style,
234 GtkRcStyle *src_style,
235 GtkRcContext *context);
236 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
237 gconstpointer bsearch_node2);
238 static void gtk_rc_set_free (GtkRcSet *rc_set);
240 static void insert_rc_property (GtkRcStyle *style,
241 GtkRcProperty *property,
245 static const GScannerConfig gtk_rc_scanner_config =
249 ) /* cset_skip_characters */,
254 ) /* cset_identifier_first */,
260 ) /* cset_identifier_nth */,
261 ( "#\n" ) /* cpair_comment_single */,
263 TRUE /* case_sensitive */,
265 TRUE /* skip_comment_multi */,
266 TRUE /* skip_comment_single */,
267 TRUE /* scan_comment_multi */,
268 TRUE /* scan_identifier */,
269 FALSE /* scan_identifier_1char */,
270 FALSE /* scan_identifier_NULL */,
271 TRUE /* scan_symbols */,
272 TRUE /* scan_binary */,
273 TRUE /* scan_octal */,
274 TRUE /* scan_float */,
276 TRUE /* scan_hex_dollar */,
277 TRUE /* scan_string_sq */,
278 TRUE /* scan_string_dq */,
279 TRUE /* numbers_2_int */,
280 FALSE /* int_2_float */,
281 FALSE /* identifier_2_string */,
282 TRUE /* char_2_token */,
283 TRUE /* symbol_2_token */,
284 FALSE /* scope_0_fallback */,
287 static const gchar symbol_names[] =
331 { 0, GTK_RC_TOKEN_INCLUDE },
332 { 8, GTK_RC_TOKEN_NORMAL },
333 { 15, GTK_RC_TOKEN_ACTIVE },
334 { 22, GTK_RC_TOKEN_PRELIGHT },
335 { 31, GTK_RC_TOKEN_SELECTED },
336 { 40, GTK_RC_TOKEN_INSENSITIVE },
337 { 52, GTK_RC_TOKEN_FG },
338 { 55, GTK_RC_TOKEN_BG },
339 { 58, GTK_RC_TOKEN_TEXT },
340 { 63, GTK_RC_TOKEN_BASE },
341 { 68, GTK_RC_TOKEN_XTHICKNESS },
342 { 79, GTK_RC_TOKEN_YTHICKNESS },
343 { 90, GTK_RC_TOKEN_FONT },
344 { 95, GTK_RC_TOKEN_FONTSET },
345 { 103, GTK_RC_TOKEN_FONT_NAME },
346 { 113, GTK_RC_TOKEN_BG_PIXMAP },
347 { 123, GTK_RC_TOKEN_PIXMAP_PATH },
348 { 135, GTK_RC_TOKEN_STYLE },
349 { 141, GTK_RC_TOKEN_BINDING },
350 { 149, GTK_RC_TOKEN_BIND },
351 { 154, GTK_RC_TOKEN_WIDGET },
352 { 161, GTK_RC_TOKEN_WIDGET_CLASS },
353 { 174, GTK_RC_TOKEN_CLASS },
354 { 180, GTK_RC_TOKEN_LOWEST },
355 { 187, GTK_RC_TOKEN_GTK },
356 { 191, GTK_RC_TOKEN_APPLICATION },
357 { 203, GTK_RC_TOKEN_THEME },
358 { 209, GTK_RC_TOKEN_RC },
359 { 212, GTK_RC_TOKEN_HIGHEST },
360 { 220, GTK_RC_TOKEN_ENGINE },
361 { 227, GTK_RC_TOKEN_MODULE_PATH },
362 { 239, GTK_RC_TOKEN_STOCK },
363 { 245, GTK_RC_TOKEN_IM_MODULE_FILE },
364 { 260, GTK_RC_TOKEN_LTR },
365 { 264, GTK_RC_TOKEN_RTL },
366 { 268, GTK_RC_TOKEN_COLOR },
367 { 274, GTK_RC_TOKEN_UNBIND }
370 static GHashTable *realized_style_ht = NULL;
372 static gchar *im_module_file = NULL;
374 static gint max_default_files = 0;
375 static gchar **gtk_rc_default_files = NULL;
377 /* A stack of information of RC files we are parsing currently.
378 * The directories for these files are implicitely added to the end of
381 static GSList *current_files_stack = NULL;
383 /* RC files and strings that are parsed for every context
385 static GSList *global_rc_files = NULL;
387 /* Keep list of all current RC contexts for convenience
389 static GSList *rc_contexts;
391 /* RC file handling */
394 gtk_rc_make_default_dir (const gchar *type)
399 var = g_getenv ("GTK_EXE_PREFIX");
402 path = g_build_filename (var, "lib", "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
404 path = g_build_filename (GTK_LIBDIR, "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
410 * gtk_rc_get_im_module_path:
411 * @returns: a newly-allocated string containing the path in which to
412 * look for IM modules.
414 * Obtains the path in which to look for IM modules. See the documentation
415 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
416 * environment variable for more details about looking up modules. This
417 * function is useful solely for utilities supplied with GTK+ and should
418 * not be used by applications under normal circumstances.
421 gtk_rc_get_im_module_path (void)
423 gchar **paths = _gtk_get_module_path ("immodules");
424 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
431 * gtk_rc_get_im_module_file:
432 * @returns: a newly-allocated string containing the name of the file
433 * listing the IM modules available for loading
435 * Obtains the path to the IM modules file. See the documentation
436 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
437 * environment variable for more details.
440 gtk_rc_get_im_module_file (void)
442 const gchar *var = g_getenv ("GTK_IM_MODULE_FILE");
443 gchar *result = NULL;
446 result = g_strdup (var);
451 result = g_strdup (im_module_file);
453 result = gtk_rc_make_default_dir ("immodules.cache");
460 gtk_rc_get_theme_dir (void)
465 var = g_getenv ("GTK_DATA_PREFIX");
468 path = g_build_filename (var, "share", "themes", NULL);
470 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
476 * gtk_rc_get_module_dir:
478 * Returns a directory in which GTK+ looks for theme engines.
479 * For full information about the search for theme engines,
480 * see the docs for <envar>GTK_PATH</envar> in
481 * <xref linkend="gtk-running"/>.
483 * return value: the directory. (Must be freed with g_free())
486 gtk_rc_get_module_dir (void)
488 return gtk_rc_make_default_dir ("engines");
492 gtk_rc_add_initial_default_files (void)
494 static gint init = FALSE;
503 gtk_rc_default_files = g_new (gchar*, 10);
504 max_default_files = 10;
506 gtk_rc_default_files[0] = NULL;
509 var = g_getenv ("GTK2_RC_FILES");
513 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, -1);
517 gtk_rc_add_default_file (files[i]);
525 str = g_build_filename (GTK_SYSCONFDIR, "gtk-3.0", "gtkrc", NULL);
527 gtk_rc_add_default_file (str);
530 home = g_get_home_dir ();
533 str = g_build_filename (home, ".gtkrc-3.0", NULL);
534 gtk_rc_add_default_file (str);
541 * gtk_rc_add_default_file:
542 * @filename: the pathname to the file. If @filename is not absolute, it
543 * is searched in the current directory.
545 * Adds a file to the list of files to be parsed at the
549 gtk_rc_add_default_file (const gchar *filename)
553 gtk_rc_add_initial_default_files ();
555 for (n = 0; n < max_default_files; n++)
557 if (gtk_rc_default_files[n] == NULL)
561 if (n == max_default_files)
563 max_default_files += 10;
564 gtk_rc_default_files = g_renew (gchar*, gtk_rc_default_files, max_default_files);
567 gtk_rc_default_files[n++] = g_strdup (filename);
568 gtk_rc_default_files[n] = NULL;
572 * gtk_rc_set_default_files:
573 * @filenames: A %NULL-terminated list of filenames.
575 * Sets the list of files that GTK+ will read at the
579 gtk_rc_set_default_files (gchar **filenames)
583 gtk_rc_add_initial_default_files ();
586 while (gtk_rc_default_files[i])
588 g_free (gtk_rc_default_files[i]);
592 gtk_rc_default_files[0] = NULL;
595 while (filenames[i] != NULL)
597 gtk_rc_add_default_file (filenames[i]);
603 * gtk_rc_get_default_files:
605 * Retrieves the current list of RC files that will be parsed
606 * at the end of gtk_init().
608 * Return value: A %NULL-terminated array of filenames. This memory
609 * is owned by GTK+ and must not be freed by the application.
610 * If you want to store this information, you should make a copy.
613 gtk_rc_get_default_files (void)
615 gtk_rc_add_initial_default_files ();
617 return gtk_rc_default_files;
621 gtk_rc_settings_changed (GtkSettings *settings,
623 GtkRcContext *context)
625 gchar *new_theme_name;
626 gchar *new_key_theme_name;
627 gboolean new_prefer_dark_theme;
629 if (context->reloading)
632 g_object_get (settings,
633 "gtk-theme-name", &new_theme_name,
634 "gtk-key-theme-name", &new_key_theme_name,
635 "gtk-application-prefer-dark-theme", &new_prefer_dark_theme,
638 if ((new_theme_name != context->theme_name &&
639 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
640 (new_key_theme_name != context->key_theme_name &&
641 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)) ||
642 new_prefer_dark_theme != context->prefer_dark_theme)
644 gtk_rc_reparse_all_for_settings (settings, TRUE);
647 g_free (new_theme_name);
648 g_free (new_key_theme_name);
652 gtk_rc_font_name_changed (GtkSettings *settings,
654 GtkRcContext *context)
656 if (!context->reloading)
657 _gtk_rc_context_get_default_font_name (settings);
661 gtk_rc_color_hash_changed (GtkSettings *settings,
663 GtkRcContext *context)
665 GHashTable *old_hash;
667 old_hash = context->color_hash;
669 g_object_get (settings, "color-hash", &context->color_hash, NULL);
672 g_hash_table_unref (old_hash);
674 gtk_rc_reparse_all_for_settings (settings, TRUE);
677 static GtkRcContext *
678 gtk_rc_context_get (GtkSettings *settings)
680 if (!settings->rc_context)
682 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
684 context->settings = settings;
685 context->rc_style_ht = NULL;
686 context->rc_sets_widget = NULL;
687 context->rc_sets_widget_class = NULL;
688 context->rc_sets_class = NULL;
689 context->rc_files = NULL;
690 context->default_style = NULL;
691 context->reloading = FALSE;
693 g_object_get (settings,
694 "gtk-theme-name", &context->theme_name,
695 "gtk-key-theme-name", &context->key_theme_name,
696 "gtk-font-name", &context->font_name,
697 "color-hash", &context->color_hash,
698 "gtk-application-prefer-dark-theme", &context->prefer_dark_theme,
701 g_signal_connect (settings,
702 "notify::gtk-theme-name",
703 G_CALLBACK (gtk_rc_settings_changed),
705 g_signal_connect (settings,
706 "notify::gtk-key-theme-name",
707 G_CALLBACK (gtk_rc_settings_changed),
709 g_signal_connect (settings,
710 "notify::gtk-font-name",
711 G_CALLBACK (gtk_rc_font_name_changed),
713 g_signal_connect (settings,
714 "notify::color-hash",
715 G_CALLBACK (gtk_rc_color_hash_changed),
717 g_signal_connect (settings,
718 "notify::gtk-application-prefer-dark-theme",
719 G_CALLBACK (gtk_rc_settings_changed),
722 context->pixmap_path = NULL;
724 context->default_priority = GTK_PATH_PRIO_RC;
726 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
729 return settings->rc_context;
733 gtk_rc_clear_rc_files (GtkRcContext *context)
737 list = context->rc_files;
740 GtkRcFile *rc_file = list->data;
742 if (rc_file->canonical_name != rc_file->name)
743 g_free (rc_file->canonical_name);
744 g_free (rc_file->directory);
745 g_free (rc_file->name);
751 g_slist_free (context->rc_files);
752 context->rc_files = NULL;
756 _gtk_rc_context_destroy (GtkSettings *settings)
758 GtkRcContext *context;
760 g_return_if_fail (GTK_IS_SETTINGS (settings));
762 context = settings->rc_context;
766 _gtk_settings_reset_rc_values (context->settings);
767 gtk_rc_clear_styles (context);
768 gtk_rc_clear_rc_files (context);
770 if (context->default_style)
771 g_object_unref (context->default_style);
773 g_strfreev (context->pixmap_path);
775 g_free (context->theme_name);
776 g_free (context->key_theme_name);
777 g_free (context->font_name);
779 if (context->color_hash)
780 g_hash_table_unref (context->color_hash);
782 g_signal_handlers_disconnect_by_func (settings,
783 gtk_rc_settings_changed, context);
784 g_signal_handlers_disconnect_by_func (settings,
785 gtk_rc_font_name_changed, context);
786 g_signal_handlers_disconnect_by_func (settings,
787 gtk_rc_color_hash_changed, context);
789 rc_contexts = g_slist_remove (rc_contexts, context);
793 settings->rc_context = NULL;
797 gtk_rc_parse_named (GtkRcContext *context,
800 const gchar *variant)
803 const gchar *home_dir;
810 subpath = g_strconcat ("gtk-3.0-", type,
811 G_DIR_SEPARATOR_S "gtkrc",
814 subpath = g_strconcat ("gtk-3.0" G_DIR_SEPARATOR_S "gtkrc",
817 /* First look in the users home directory
819 home_dir = g_get_home_dir ();
822 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
823 if (!g_file_test (path, G_FILE_TEST_EXISTS))
832 gchar *theme_dir = gtk_rc_get_theme_dir ();
833 path = g_build_filename (theme_dir, name, subpath, NULL);
836 if (!g_file_test (path, G_FILE_TEST_EXISTS))
845 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
856 gtk_rc_parse_default_files (GtkRcContext *context)
860 gtk_rc_add_initial_default_files ();
862 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
863 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
869 static gboolean initialized = FALSE;
875 gtk_rc_add_initial_default_files ();
878 /* Default RC string */
879 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
880 " bg[NORMAL] = \"#eee1b3\"\n"
881 " fg[NORMAL] = \"#000000\"\n"
884 "style \"gtk-default-progress-bar-style\" {\n"
885 " bg[PRELIGHT] = \"#4b6983\"\n"
886 " fg[PRELIGHT] = \"#ffffff\"\n"
887 " bg[NORMAL] = \"#c4c2bd\"\n"
890 "style \"gtk-default-entry-style\" {\n"
891 " bg[SELECTED] = \"#b7c3cd\"\n"
892 " fg[SELECTED] = \"#000000\"\n"
895 "style \"gtk-default-menu-bar-item-style\" {\n"
896 " GtkMenuItem::horizontal_padding = 5\n"
899 "style \"gtk-default-menu-item-style\" {\n"
900 " bg[PRELIGHT] = \"#4b6983\"\n"
901 " fg[PRELIGHT] = \"#ffffff\"\n"
902 " base[PRELIGHT] = \"#4b6983\"\n"
903 " text[PRELIGHT] = \"#ffffff\"\n"
906 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
907 "class \"GtkEntry\" style : gtk \"gtk-default-entry-style\"\n"
908 "widget \"gtk-tooltip*\" style : gtk \"gtk-default-tooltips-style\"\n"
909 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
910 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
915 gtk_rc_context_parse_string (GtkRcContext *context,
916 const gchar *rc_string)
918 gtk_rc_parse_any (context, "-", -1, rc_string);
922 gtk_rc_parse_string (const gchar *rc_string)
927 g_return_if_fail (rc_string != NULL);
929 rc_file = g_new (GtkRcFile, 1);
930 rc_file->is_string = TRUE;
931 rc_file->name = g_strdup (rc_string);
932 rc_file->canonical_name = NULL;
933 rc_file->directory = NULL;
935 rc_file->reload = TRUE;
937 global_rc_files = g_slist_append (global_rc_files, rc_file);
939 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
940 gtk_rc_context_parse_string (tmp_list->data, rc_string);
944 add_to_rc_file_list (GSList **rc_file_list,
945 const char *filename,
951 tmp_list = *rc_file_list;
954 rc_file = tmp_list->data;
955 if (!strcmp (rc_file->name, filename))
958 tmp_list = tmp_list->next;
961 rc_file = g_new (GtkRcFile, 1);
962 rc_file->is_string = FALSE;
963 rc_file->name = g_strdup (filename);
964 rc_file->canonical_name = NULL;
965 rc_file->directory = NULL;
967 rc_file->reload = reload;
969 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
975 gtk_rc_context_parse_one_file (GtkRcContext *context,
976 const gchar *filename,
984 g_return_if_fail (filename != NULL);
986 saved_priority = context->default_priority;
987 context->default_priority = priority;
989 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
991 if (!rc_file->canonical_name)
993 /* Get the absolute pathname */
995 if (g_path_is_absolute (rc_file->name))
996 rc_file->canonical_name = rc_file->name;
1001 cwd = g_get_current_dir ();
1002 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
1006 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
1009 /* If the file is already being parsed (recursion), do nothing
1011 if (g_slist_find (current_files_stack, rc_file))
1014 if (!g_lstat (rc_file->canonical_name, &statbuf))
1018 rc_file->mtime = statbuf.st_mtime;
1020 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1024 /* Temporarily push information for this file on
1025 * a stack of current files while parsing it.
1027 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1028 gtk_rc_parse_any (context, filename, fd, NULL);
1029 current_files_stack = g_slist_delete_link (current_files_stack,
1030 current_files_stack);
1036 context->default_priority = saved_priority;
1040 strchr_len (const gchar *str, gint len, char c)
1045 return (gchar *)str;
1054 gtk_rc_context_parse_file (GtkRcContext *context,
1055 const gchar *filename,
1059 gchar *locale_suffixes[2];
1060 gint n_locale_suffixes = 0;
1064 gboolean found = FALSE;
1066 locale = _gtk_get_lc_ctype ();
1068 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1070 /* Determine locale-specific suffixes for RC files.
1072 length = strlen (locale);
1074 p = strchr (locale, '@');
1076 length = p - locale;
1078 p = strchr_len (locale, length, '.');
1080 length = p - locale;
1082 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1084 p = strchr_len (locale, length, '_');
1087 length = p - locale;
1088 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1094 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1095 for (j = 0; j < n_locale_suffixes; j++)
1099 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1100 if (g_file_test (name, G_FILE_TEST_EXISTS))
1102 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1109 g_free (locale_suffixes[j]);
1114 gtk_rc_parse (const gchar *filename)
1118 g_return_if_fail (filename != NULL);
1120 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1122 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1123 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1126 /* Handling of RC styles */
1128 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1131 gtk_rc_style_init (GtkRcStyle *style)
1133 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1137 style->font_desc = NULL;
1139 for (i = 0; i < 5; i++)
1141 static const GdkColor init_color = { 0, 0, 0, 0, };
1143 style->bg_pixmap_name[i] = NULL;
1144 style->color_flags[i] = 0;
1145 style->fg[i] = init_color;
1146 style->bg[i] = init_color;
1147 style->text[i] = init_color;
1148 style->base[i] = init_color;
1150 style->xthickness = -1;
1151 style->ythickness = -1;
1152 style->rc_properties = NULL;
1154 style->rc_style_lists = NULL;
1155 style->icon_factories = NULL;
1157 priv->color_hashes = NULL;
1161 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1163 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1165 object_class->finalize = gtk_rc_style_finalize;
1167 klass->parse = NULL;
1168 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1169 klass->merge = gtk_rc_style_real_merge;
1170 klass->create_style = gtk_rc_style_real_create_style;
1172 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1176 gtk_rc_style_finalize (GObject *object)
1178 GSList *tmp_list1, *tmp_list2;
1179 GtkRcStyle *rc_style;
1180 GtkRcStylePrivate *rc_priv;
1183 rc_style = GTK_RC_STYLE (object);
1184 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1186 g_free (rc_style->name);
1187 if (rc_style->font_desc)
1188 pango_font_description_free (rc_style->font_desc);
1190 for (i = 0; i < 5; i++)
1191 g_free (rc_style->bg_pixmap_name[i]);
1193 /* Now remove all references to this rc_style from
1196 tmp_list1 = rc_style->rc_style_lists;
1199 GSList *rc_styles = tmp_list1->data;
1200 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1201 g_object_unref (style);
1203 /* Remove the list of styles from the other rc_styles
1206 tmp_list2 = rc_styles;
1209 GtkRcStyle *other_style = tmp_list2->data;
1211 if (other_style != rc_style)
1212 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1214 tmp_list2 = tmp_list2->next;
1217 /* And from the hash table itself
1219 g_hash_table_remove (realized_style_ht, rc_styles);
1220 g_slist_free (rc_styles);
1222 tmp_list1 = tmp_list1->next;
1224 g_slist_free (rc_style->rc_style_lists);
1226 if (rc_style->rc_properties)
1230 for (i = 0; i < rc_style->rc_properties->len; i++)
1232 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1234 g_free (node->origin);
1235 g_value_unset (&node->value);
1237 g_array_free (rc_style->rc_properties, TRUE);
1238 rc_style->rc_properties = NULL;
1241 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1242 g_slist_free (rc_style->icon_factories);
1244 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1245 g_slist_free (rc_priv->color_hashes);
1247 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1251 gtk_rc_style_new (void)
1255 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1261 * gtk_rc_style_copy:
1262 * @orig: the style to copy
1264 * Makes a copy of the specified #GtkRcStyle. This function
1265 * will correctly copy an RC style that is a member of a class
1266 * derived from #GtkRcStyle.
1268 * Return value: the resulting #GtkRcStyle
1271 gtk_rc_style_copy (GtkRcStyle *orig)
1275 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1277 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1278 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1280 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1286 _gtk_rc_style_set_rc_property (GtkRcStyle *rc_style,
1287 GtkRcProperty *property)
1289 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1290 g_return_if_fail (property != NULL);
1292 insert_rc_property (rc_style, property, TRUE);
1296 _gtk_rc_style_unset_rc_property (GtkRcStyle *rc_style,
1298 GQuark property_name)
1300 GtkRcProperty *node;
1302 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1304 node = (GtkRcProperty *) _gtk_rc_style_lookup_rc_property (rc_style,
1310 guint index = node - (GtkRcProperty *) rc_style->rc_properties->data;
1311 g_value_unset (&node->value);
1312 g_free (node->origin);
1313 g_array_remove_index (rc_style->rc_properties, index);
1318 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1320 return g_object_new (G_OBJECT_TYPE (style), NULL);
1324 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1326 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1328 return priv->color_hashes;
1331 static void gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style);
1334 _gtk_rc_style_set_symbolic_color (GtkRcStyle *rc_style,
1336 const GdkColor *color)
1338 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1339 GHashTable *our_hash = NULL;
1341 if (priv->color_hashes)
1342 our_hash = priv->color_hashes->data;
1344 if (our_hash == NULL)
1349 gtk_rc_style_prepend_empty_color_hash (rc_style);
1350 our_hash = priv->color_hashes->data;
1354 g_hash_table_insert (our_hash, g_strdup (name), gdk_color_copy (color));
1356 g_hash_table_remove (our_hash, name);
1360 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1361 gconstpointer bsearch_node2)
1363 const GtkRcProperty *prop1 = bsearch_node1;
1364 const GtkRcProperty *prop2 = bsearch_node2;
1366 if (prop1->type_name == prop2->type_name)
1367 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1369 return prop1->type_name < prop2->type_name ? -1 : 1;
1373 insert_rc_property (GtkRcStyle *style,
1374 GtkRcProperty *property,
1378 GtkRcProperty *new_property = NULL;
1379 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1381 key.type_name = property->type_name;
1382 key.property_name = property->property_name;
1384 if (!style->rc_properties)
1385 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1388 while (i < style->rc_properties->len)
1390 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1396 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1398 g_free (new_property->origin);
1399 g_value_unset (&new_property->value);
1401 *new_property = key;
1415 g_array_insert_val (style->rc_properties, i, key);
1416 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1419 new_property->origin = g_strdup (property->origin);
1420 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1421 g_value_copy (&property->value, &new_property->value);
1425 gtk_rc_style_real_merge (GtkRcStyle *dest,
1430 for (i = 0; i < 5; i++)
1432 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1433 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1435 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1436 src->color_flags[i] & GTK_RC_FG)
1438 dest->fg[i] = src->fg[i];
1439 dest->color_flags[i] |= GTK_RC_FG;
1441 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1442 src->color_flags[i] & GTK_RC_BG)
1444 dest->bg[i] = src->bg[i];
1445 dest->color_flags[i] |= GTK_RC_BG;
1447 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1448 src->color_flags[i] & GTK_RC_TEXT)
1450 dest->text[i] = src->text[i];
1451 dest->color_flags[i] |= GTK_RC_TEXT;
1453 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1454 src->color_flags[i] & GTK_RC_BASE)
1456 dest->base[i] = src->base[i];
1457 dest->color_flags[i] |= GTK_RC_BASE;
1461 if (dest->xthickness < 0 && src->xthickness >= 0)
1462 dest->xthickness = src->xthickness;
1463 if (dest->ythickness < 0 && src->ythickness >= 0)
1464 dest->ythickness = src->ythickness;
1468 if (!dest->font_desc)
1469 dest->font_desc = pango_font_description_copy (src->font_desc);
1471 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1474 if (src->rc_properties)
1478 for (i = 0; i < src->rc_properties->len; i++)
1479 insert_rc_property (dest,
1480 &g_array_index (src->rc_properties, GtkRcProperty, i),
1486 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1488 return gtk_style_new ();
1492 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1494 GtkIconFactory *factory = gtk_icon_factory_new ();
1496 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1500 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1502 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1503 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1505 (GDestroyNotify) gdk_color_free);
1507 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1511 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1512 GtkRcStyle *src_style)
1514 GSList *concat = g_slist_copy (src_style->icon_factories);
1516 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1518 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1522 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1523 GtkRcStyle *src_style)
1525 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1526 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1527 GSList *concat = g_slist_copy (src_priv->color_hashes);
1529 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1531 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1535 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1536 GtkRcStyle *src_style,
1537 GtkRcContext *context)
1539 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1543 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1545 /* Append src_style's factories, adding a ref to them */
1546 if (src_style->icon_factories != NULL)
1548 /* Add a factory for ourselves if we have none,
1549 * in case we end up defining more stock icons.
1550 * I see no real way around this; we need to maintain
1551 * the invariant that the first factory in the list
1552 * is always our_factory, the one belonging to us,
1553 * and if we put src_style factories in the list we can't
1554 * do that if the style is reopened.
1556 if (rc_style->icon_factories == NULL)
1557 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1559 gtk_rc_style_append_icon_factories (rc_style, src_style);
1562 /* Also append src_style's color hashes, adding a ref to them */
1563 if (src_priv->color_hashes != NULL)
1565 /* See comment above .. */
1566 if (priv->color_hashes == NULL)
1567 gtk_rc_style_prepend_empty_color_hash (rc_style);
1569 gtk_rc_style_append_color_hashes (rc_style, src_style);
1573 /* if we didn't get color hashes from the src_style, initialize
1574 * the list with the settings' color scheme (if it exists)
1576 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1578 gtk_rc_style_prepend_empty_color_hash (rc_style);
1580 priv->color_hashes = g_slist_append (priv->color_hashes,
1581 g_hash_table_ref (context->color_hash));
1586 gtk_rc_clear_hash_node (gpointer key,
1590 g_object_unref (data);
1594 gtk_rc_free_rc_sets (GSList *slist)
1600 rc_set = slist->data;
1601 gtk_rc_set_free (rc_set);
1603 slist = slist->next;
1608 gtk_rc_clear_styles (GtkRcContext *context)
1610 /* Clear out all old rc_styles */
1612 if (context->rc_style_ht)
1614 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1615 g_hash_table_destroy (context->rc_style_ht);
1616 context->rc_style_ht = NULL;
1619 gtk_rc_free_rc_sets (context->rc_sets_widget);
1620 g_slist_free (context->rc_sets_widget);
1621 context->rc_sets_widget = NULL;
1623 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1624 g_slist_free (context->rc_sets_widget_class);
1625 context->rc_sets_widget_class = NULL;
1627 gtk_rc_free_rc_sets (context->rc_sets_class);
1628 g_slist_free (context->rc_sets_class);
1629 context->rc_sets_class = NULL;
1632 /* Reset all our widgets. Also, we have to invalidate cached icons in
1633 * icon sets so they get re-rendered.
1636 gtk_rc_reset_widgets (GtkSettings *settings)
1638 GList *list, *toplevels;
1640 _gtk_icon_set_invalidate_caches ();
1642 toplevels = gtk_window_list_toplevels ();
1643 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1645 for (list = toplevels; list; list = list->next)
1647 if (gtk_widget_get_screen (list->data) == settings->screen)
1649 gtk_widget_reset_rc_styles (list->data);
1652 g_object_unref (list->data);
1654 g_list_free (toplevels);
1658 gtk_rc_clear_realized_style (gpointer key,
1662 GSList *rc_styles = key;
1663 GtkStyle *style = value;
1664 GSList *tmp_list = rc_styles;
1666 g_object_unref (style);
1670 GtkRcStyle *rc_style = tmp_list->data;
1672 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1674 tmp_list = tmp_list->next;
1677 g_slist_free (rc_styles);
1681 * gtk_rc_reset_styles:
1682 * @settings: a #GtkSettings
1684 * This function recomputes the styles for all widgets that use a
1685 * particular #GtkSettings object. (There is one #GtkSettings object
1686 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1687 * when some global parameter has changed that affects the appearance
1688 * of all widgets, because when a widget gets a new style, it will
1689 * both redraw and recompute any cached information about its
1690 * appearance. As an example, it is used when the default font size
1691 * set by the operating system changes. Note that this function
1692 * doesn't affect widgets that have a style set explicitely on them
1693 * with gtk_widget_set_style().
1698 gtk_rc_reset_styles (GtkSettings *settings)
1700 GtkRcContext *context;
1701 gboolean reset = FALSE;
1703 g_return_if_fail (GTK_IS_SETTINGS (settings));
1705 context = gtk_rc_context_get (settings);
1707 if (context->default_style)
1709 g_object_unref (context->default_style);
1710 context->default_style = NULL;
1714 /* Clear out styles that have been looked up already
1716 if (realized_style_ht)
1718 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1719 g_hash_table_destroy (realized_style_ht);
1720 realized_style_ht = NULL;
1725 gtk_rc_reset_widgets (settings);
1729 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1731 GtkRcContext *context;
1732 gchar *new_font_name;
1734 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1736 context = gtk_rc_context_get (settings);
1738 g_object_get (context->settings,
1739 "gtk-font-name", &new_font_name,
1742 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1744 g_free (context->font_name);
1745 context->font_name = g_strdup (new_font_name);
1747 gtk_rc_reset_styles (settings);
1750 g_free (new_font_name);
1752 return context->font_name;
1756 * gtk_rc_reparse_all_for_settings:
1757 * @settings: a #GtkSettings
1758 * @force_load: load whether or not anything changed
1760 * If the modification time on any previously read file
1761 * for the given #GtkSettings has changed, discard all style information
1762 * and then reread all previously read RC files.
1764 * Return value: %TRUE if the files were reread.
1767 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1768 gboolean force_load)
1770 gboolean mtime_modified = FALSE;
1773 GtkRcContext *context;
1774 struct stat statbuf;
1776 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1778 context = gtk_rc_context_get (settings);
1780 if (context->reloading)
1785 /* Check through and see if any of the RC's have had their
1786 * mtime modified. If so, reparse everything.
1788 tmp_list = context->rc_files;
1791 rc_file = tmp_list->data;
1793 if (!rc_file->is_string)
1795 if (!g_lstat (rc_file->name, &statbuf) &&
1796 (statbuf.st_mtime != rc_file->mtime))
1798 mtime_modified = TRUE;
1803 tmp_list = tmp_list->next;
1807 if (force_load || mtime_modified)
1809 _gtk_binding_reset_parsed ();
1810 gtk_rc_clear_styles (context);
1811 context->reloading = TRUE;
1813 _gtk_settings_reset_rc_values (context->settings);
1814 gtk_rc_clear_rc_files (context);
1816 gtk_rc_parse_default_files (context);
1818 tmp_list = global_rc_files;
1821 rc_file = tmp_list->data;
1823 if (rc_file->is_string)
1824 gtk_rc_context_parse_string (context, rc_file->name);
1826 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1828 tmp_list = tmp_list->next;
1831 g_free (context->theme_name);
1832 g_free (context->key_theme_name);
1834 g_object_get (context->settings,
1835 "gtk-theme-name", &context->theme_name,
1836 "gtk-key-theme-name", &context->key_theme_name,
1837 "gtk-application-prefer-dark-theme", &context->prefer_dark_theme,
1840 if (context->theme_name && context->theme_name[0])
1842 if (context->prefer_dark_theme)
1844 if (!gtk_rc_parse_named (context, context->theme_name, NULL, "-dark"))
1845 gtk_rc_parse_named (context, context->theme_name, NULL, NULL);
1849 gtk_rc_parse_named (context, context->theme_name, NULL, NULL);
1852 if (context->key_theme_name && context->key_theme_name[0])
1853 gtk_rc_parse_named (context, context->key_theme_name, "key", NULL);
1855 context->reloading = FALSE;
1857 gtk_rc_reset_widgets (context->settings);
1860 return force_load || mtime_modified;
1864 * gtk_rc_reparse_all:
1866 * If the modification time on any previously read file for the
1867 * default #GtkSettings has changed, discard all style information
1868 * and then reread all previously read RC files.
1870 * Return value: %TRUE if the files were reread.
1873 gtk_rc_reparse_all (void)
1876 gboolean result = FALSE;
1878 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1880 GtkRcContext *context = tmp_list->data;
1881 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1889 gtk_rc_styles_match (GSList *rc_styles,
1893 gchar *path_reversed)
1900 rc_set = sets->data;
1903 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1905 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1906 rc_styles = g_slist_append (rc_styles, rc_set);
1910 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1911 rc_styles = g_slist_append (rc_styles, rc_set);
1919 rc_set_compare (gconstpointer a, gconstpointer b)
1921 const GtkRcSet *set_a = a;
1922 const GtkRcSet *set_b = b;
1924 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1928 sort_and_dereference_sets (GSList *styles)
1932 /* At this point, the list of sets is ordered by:
1934 * a) 'widget' patterns are earlier than 'widget_class' patterns
1935 * which are ealier than 'class' patterns.
1936 * a) For two matches for class patterns, a match to a child type
1937 * is before a match to a parent type
1938 * c) a match later in the RC file (or in a later RC file) is before a
1939 * match earlier in the RC file.
1941 * With a) taking precedence over b) which takes precendence over c).
1943 * Now sort by priority, which has the highest precendence for sort order
1945 styles = g_slist_sort (styles, rc_set_compare);
1947 /* Make styles->data = styles->data->rc_style
1952 GtkRcSet *set = tmp_list->data;
1953 tmp_list->data = set->rc_style;
1954 tmp_list = tmp_list->next;
1962 * @widget: a #GtkWidget
1964 * Finds all matching RC styles for a given widget,
1965 * composites them together, and then creates a
1966 * #GtkStyle representing the composite appearance.
1967 * (GTK+ actually keeps a cache of previously
1968 * created styles, so a new style may not be
1971 * Returns: the resulting style. No refcount is added
1972 * to the returned style, so if you want to save this
1973 * style around, you should add a reference yourself.
1976 gtk_rc_get_style (GtkWidget *widget)
1978 GtkRcStyle *widget_rc_style;
1979 GSList *rc_styles = NULL;
1980 GtkRcContext *context;
1982 static guint rc_style_key_id = 0;
1984 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1986 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1988 /* We allow the specification of a single rc style to be bound
1989 * tightly to a widget, for application modifications
1991 if (!rc_style_key_id)
1992 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1994 if (context->rc_sets_widget)
1996 gchar *path, *path_reversed;
1999 gtk_widget_path (widget, &path_length, &path, &path_reversed);
2000 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2002 g_free (path_reversed);
2005 if (context->rc_sets_widget_class)
2007 gchar *path, *path_reversed;
2010 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
2011 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2013 g_free (path_reversed);
2016 if (context->rc_sets_class)
2020 type = G_TYPE_FROM_INSTANCE (widget);
2024 gchar *path_reversed;
2027 path = g_strdup (g_type_name (type));
2028 path_length = strlen (path);
2029 path_reversed = g_strdup (path);
2030 g_strreverse (path_reversed);
2032 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2034 g_free (path_reversed);
2036 type = g_type_parent (type);
2040 rc_styles = sort_and_dereference_sets (rc_styles);
2042 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
2044 if (widget_rc_style)
2045 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
2048 return gtk_rc_init_style (context, rc_styles);
2051 if (!context->default_style)
2053 context->default_style = gtk_style_new ();
2054 _gtk_style_init_for_settings (context->default_style, context->settings);
2057 return context->default_style;
2062 * gtk_rc_get_style_by_paths:
2063 * @settings: a #GtkSettings object
2064 * @widget_path: (allow-none): the widget path to use when looking up the style, or %NULL
2065 * if no matching against the widget path should be done
2066 * @class_path: (allow-none): the class path to use when looking up the style, or %NULL
2067 * if no matching against the class path should be done.
2068 * @type: a type that will be used along with parent types of this type
2069 * when matching against class styles, or #G_TYPE_NONE
2071 * Creates up a #GtkStyle from styles defined in a RC file by providing
2072 * the raw components used in matching. This function may be useful
2073 * when creating pseudo-widgets that should be themed like widgets but
2074 * don't actually have corresponding GTK+ widgets. An example of this
2075 * would be items inside a GNOME canvas widget.
2077 * The action of gtk_rc_get_style() is similar to:
2079 * gtk_widget_path (widget, NULL, &path, NULL);
2080 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2081 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
2083 * G_OBJECT_TYPE (widget));
2086 * Return value: A style created by matching with the supplied paths,
2087 * or %NULL if nothing matching was specified and the default style should
2088 * be used. The returned value is owned by GTK+ as part of an internal cache,
2089 * so you must call g_object_ref() on the returned value if you want to
2090 * keep a reference to it.
2093 gtk_rc_get_style_by_paths (GtkSettings *settings,
2094 const char *widget_path,
2095 const char *class_path,
2098 /* We duplicate the code from above to avoid slowing down the above
2099 * by generating paths when we don't need them. I don't know if
2100 * this is really worth it.
2102 GSList *rc_styles = NULL;
2103 GtkRcContext *context;
2105 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2107 context = gtk_rc_context_get (settings);
2109 if (widget_path && context->rc_sets_widget)
2112 gchar *path_reversed;
2115 path_length = strlen (widget_path);
2116 path = g_strdup (widget_path);
2117 path_reversed = g_strdup (widget_path);
2118 g_strreverse (path_reversed);
2120 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2122 g_free (path_reversed);
2125 if (class_path && context->rc_sets_widget_class)
2128 gchar *path_reversed;
2131 path = g_strdup (class_path);
2132 path_length = strlen (class_path);
2133 path_reversed = g_strdup (class_path);
2134 g_strreverse (path_reversed);
2136 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2138 g_free (path_reversed);
2141 if (type != G_TYPE_NONE && context->rc_sets_class)
2146 gchar *path_reversed;
2149 path = g_strdup (g_type_name (type));
2150 path_length = strlen (path);
2151 path_reversed = g_strdup (path);
2152 g_strreverse (path_reversed);
2154 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2156 g_free (path_reversed);
2158 type = g_type_parent (type);
2162 rc_styles = sort_and_dereference_sets (rc_styles);
2165 return gtk_rc_init_style (context, rc_styles);
2171 gtk_rc_scanner_new (void)
2173 return g_scanner_new (>k_rc_scanner_config);
2177 gtk_rc_parse_any (GtkRcContext *context,
2178 const gchar *input_name,
2180 const gchar *input_string)
2186 scanner = gtk_rc_scanner_new ();
2190 g_assert (input_string == NULL);
2192 g_scanner_input_file (scanner, input_fd);
2196 g_assert (input_string != NULL);
2198 g_scanner_input_text (scanner, input_string, strlen (input_string));
2200 scanner->input_name = input_name;
2202 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2203 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2207 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2211 guint expected_token;
2213 expected_token = gtk_rc_parse_statement (context, scanner);
2215 if (expected_token != G_TOKEN_NONE)
2217 const gchar *symbol_name = NULL;
2220 if (scanner->scope_id == 0)
2222 /* if we are in scope 0, we know the symbol names
2223 * that are associated with certain token values.
2224 * so we look them up to make the error messages
2227 if (expected_token > GTK_RC_TOKEN_INVALID &&
2228 expected_token < GTK_RC_TOKEN_LAST)
2230 const gchar *sym = NULL;
2232 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2233 if (symbols[i].token == expected_token)
2234 sym = symbol_names + symbols[i].name_offset;
2237 msg = g_strconcat ("e.g. `", sym, "'", NULL);
2240 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2241 scanner->token < GTK_RC_TOKEN_LAST)
2243 symbol_name = "???";
2244 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2245 if (symbols[i].token == scanner->token)
2246 symbol_name = symbol_names + symbols[i].name_offset;
2250 g_scanner_unexp_token (scanner,
2263 g_scanner_destroy (scanner);
2267 gtk_rc_styles_hash (const GSList *rc_styles)
2274 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2275 rc_styles = rc_styles->next;
2282 gtk_rc_styles_equal (const GSList *a,
2287 if (a->data != b->data)
2297 gtk_rc_style_hash (const gchar *name)
2303 result += (result << 3) + *name++;
2309 gtk_rc_style_equal (const gchar *a,
2312 return (strcmp (a, b) == 0);
2316 gtk_rc_style_find (GtkRcContext *context,
2319 if (context->rc_style_ht)
2320 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2326 gtk_rc_style_to_style (GtkRcContext *context,
2327 GtkRcStyle *rc_style)
2331 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2332 _gtk_style_init_for_settings (style, context->settings);
2334 style->rc_style = g_object_ref (rc_style);
2336 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2341 /* Reuses or frees rc_styles */
2343 gtk_rc_init_style (GtkRcContext *context,
2346 GtkStyle *style = NULL;
2349 g_return_val_if_fail (rc_styles != NULL, NULL);
2351 if (!realized_style_ht)
2352 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2353 (GEqualFunc) gtk_rc_styles_equal);
2355 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2359 GtkRcStyle *base_style = NULL;
2360 GtkRcStyle *proto_style;
2361 GtkRcStyleClass *proto_style_class;
2363 GType rc_style_type = GTK_TYPE_RC_STYLE;
2365 /* Find the first style where the RC file specified engine "" {}
2366 * or the first derived style and use that to create the
2367 * merged style. If we only have raw GtkRcStyles, use the first
2368 * style to create the merged style.
2370 base_style = rc_styles->data;
2371 tmp_styles = rc_styles;
2374 GtkRcStyle *rc_style = tmp_styles->data;
2376 if (rc_style->engine_specified ||
2377 G_OBJECT_TYPE (rc_style) != rc_style_type)
2379 base_style = rc_style;
2383 tmp_styles = tmp_styles->next;
2386 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2387 proto_style = proto_style_class->create_rc_style (base_style);
2389 tmp_styles = rc_styles;
2392 GtkRcStyle *rc_style = tmp_styles->data;
2394 proto_style_class->merge (proto_style, rc_style);
2396 /* Point from each rc_style to the list of styles */
2397 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2398 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2400 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2401 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2403 tmp_styles = tmp_styles->next;
2406 for (i = 0; i < 5; i++)
2407 if (proto_style->bg_pixmap_name[i] &&
2408 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2410 g_free (proto_style->bg_pixmap_name[i]);
2411 proto_style->bg_pixmap_name[i] = NULL;
2414 style = gtk_rc_style_to_style (context, proto_style);
2415 g_object_unref (proto_style);
2417 g_hash_table_insert (realized_style_ht, rc_styles, style);
2420 g_slist_free (rc_styles);
2425 /*********************
2426 * Parsing functions *
2427 *********************/
2430 lookup_color (GtkRcStyle *style,
2431 const char *color_name,
2434 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2437 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2439 GHashTable *hash = iter->data;
2440 GdkColor *match = g_hash_table_lookup (hash, color_name);
2444 color->red = match->red;
2445 color->green = match->green;
2446 color->blue = match->blue;
2455 rc_parse_token_or_compound (GScanner *scanner,
2458 GTokenType delimiter)
2460 guint token = g_scanner_get_next_token (scanner);
2462 /* we either scan a single token (skipping comments)
2463 * or a compund statement.
2464 * compunds are enclosed in (), [] or {} braces, we read
2465 * them in via deep recursion.
2472 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2475 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2477 case G_TOKEN_STRING:
2478 string = g_strescape (scanner->value.v_string, NULL);
2479 g_string_append (gstring, " \"");
2480 g_string_append (gstring, string);
2481 g_string_append_c (gstring, '"');
2484 case G_TOKEN_IDENTIFIER:
2485 g_string_append_c (gstring, ' ');
2486 g_string_append (gstring, scanner->value.v_identifier);
2488 case G_TOKEN_COMMENT_SINGLE:
2489 case G_TOKEN_COMMENT_MULTI:
2490 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2491 case G_TOKEN_LEFT_PAREN:
2492 g_string_append_c (gstring, ' ');
2493 g_string_append_c (gstring, token);
2494 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2495 if (token != G_TOKEN_NONE)
2498 case G_TOKEN_LEFT_CURLY:
2499 g_string_append_c (gstring, ' ');
2500 g_string_append_c (gstring, token);
2501 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2502 if (token != G_TOKEN_NONE)
2505 case G_TOKEN_LEFT_BRACE:
2506 g_string_append_c (gstring, ' ');
2507 g_string_append_c (gstring, token);
2508 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2509 if (token != G_TOKEN_NONE)
2513 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2516 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2517 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2518 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2520 g_scanner_get_next_token (scanner);
2522 if (!style || !lookup_color (style, scanner->value.v_identifier,
2525 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2526 scanner->value.v_identifier);
2527 return G_TOKEN_IDENTIFIER;
2531 g_string_append_printf (gstring, " { %s, %s, %s }",
2532 g_ascii_formatd (rbuf, sizeof (rbuf),
2534 color.red / 65535.0),
2535 g_ascii_formatd (gbuf, sizeof (gbuf),
2537 color.green / 65535.0),
2538 g_ascii_formatd (bbuf, sizeof (bbuf),
2540 color.blue / 65535.0));
2544 return G_TOKEN_IDENTIFIER;
2546 if (token >= 256 || token < 1)
2547 return delimiter ? delimiter : G_TOKEN_STRING;
2548 g_string_append_c (gstring, ' ');
2549 g_string_append_c (gstring, token);
2550 if (token == delimiter)
2551 return G_TOKEN_NONE;
2555 return G_TOKEN_NONE;
2557 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2561 gtk_rc_parse_assignment (GScanner *scanner,
2563 GtkRcProperty *prop)
2565 #define MY_SCAN_IDENTIFIER TRUE
2566 #define MY_SCAN_SYMBOLS FALSE
2567 #define MY_IDENTIFIER_2_STRING FALSE
2568 #define MY_CHAR_2_TOKEN TRUE
2569 #define MY_SCAN_IDENTIFIER_NULL FALSE
2570 #define MY_NUMBERS_2_INT TRUE
2572 gboolean scan_identifier = scanner->config->scan_identifier;
2573 gboolean scan_symbols = scanner->config->scan_symbols;
2574 gboolean identifier_2_string = scanner->config->identifier_2_string;
2575 gboolean char_2_token = scanner->config->char_2_token;
2576 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2577 gboolean numbers_2_int = scanner->config->numbers_2_int;
2578 gboolean negate = FALSE;
2579 gboolean is_color = FALSE;
2582 /* check that this is an assignment */
2583 if (g_scanner_get_next_token (scanner) != '=')
2586 /* adjust scanner mode */
2587 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2588 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2589 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2590 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2591 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2592 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2594 /* record location */
2595 if (g_getenv ("GTK_DEBUG"))
2596 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2598 prop->origin = NULL;
2600 /* parse optional symbolic color prefix */
2601 if (g_scanner_peek_next_token (scanner) == '@')
2603 g_scanner_get_next_token (scanner); /* eat color prefix */
2607 /* parse optional sign */
2608 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2610 g_scanner_get_next_token (scanner); /* eat sign */
2614 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2617 token = g_scanner_peek_next_token (scanner);
2619 if (is_color && token != G_TOKEN_IDENTIFIER)
2621 token = G_TOKEN_IDENTIFIER;
2628 g_scanner_get_next_token (scanner);
2629 g_value_init (&prop->value, G_TYPE_LONG);
2630 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2631 token = G_TOKEN_NONE;
2634 g_scanner_get_next_token (scanner);
2635 g_value_init (&prop->value, G_TYPE_DOUBLE);
2636 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2637 token = G_TOKEN_NONE;
2639 case G_TOKEN_STRING:
2640 g_scanner_get_next_token (scanner);
2642 token = G_TOKEN_INT;
2645 g_value_init (&prop->value, G_TYPE_STRING);
2646 g_value_set_string (&prop->value, scanner->value.v_string);
2647 token = G_TOKEN_NONE;
2650 case G_TOKEN_IDENTIFIER:
2654 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2655 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2656 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2659 g_scanner_get_next_token (scanner);
2661 if (!style || !lookup_color (style, scanner->value.v_identifier,
2664 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2665 scanner->value.v_identifier);
2666 token = G_TOKEN_IDENTIFIER;
2670 gstring = g_string_new (NULL);
2672 g_string_append_printf (gstring, " { %s, %s, %s }",
2673 g_ascii_formatd (rbuf, sizeof (rbuf),
2675 color.red / 65535.0),
2676 g_ascii_formatd (gbuf, sizeof (gbuf),
2678 color.green / 65535.0),
2679 g_ascii_formatd (bbuf, sizeof (bbuf),
2681 color.blue / 65535.0));
2683 g_value_init (&prop->value, G_TYPE_GSTRING);
2684 g_value_take_boxed (&prop->value, gstring);
2685 token = G_TOKEN_NONE;
2689 case G_TOKEN_LEFT_PAREN:
2690 case G_TOKEN_LEFT_CURLY:
2691 case G_TOKEN_LEFT_BRACE:
2694 GString *gstring = g_string_new (NULL);
2695 gboolean parse_on = TRUE;
2697 /* allow identifier(foobar) to support color expressions */
2698 if (token == G_TOKEN_IDENTIFIER)
2700 g_scanner_get_next_token (scanner);
2702 g_string_append_c (gstring, ' ');
2703 g_string_append (gstring, scanner->value.v_identifier);
2705 /* temporarily reset scanner mode to default, so we
2706 * don't peek the next token in a mode that only makes
2707 * sense in this function; because if anything but
2708 * G_TOKEN_LEFT_PAREN follows, the next token will be
2709 * parsed by our caller.
2711 * FIXME: right fix would be to call g_scanner_unget()
2712 * but that doesn't exist
2714 scanner->config->scan_identifier = scan_identifier;
2715 scanner->config->scan_symbols = scan_symbols;
2716 scanner->config->identifier_2_string = identifier_2_string;
2717 scanner->config->char_2_token = char_2_token;
2718 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2719 scanner->config->numbers_2_int = numbers_2_int;
2721 token = g_scanner_peek_next_token (scanner);
2723 /* restore adjusted scanner mode */
2724 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2725 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2726 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2727 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2728 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2729 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2731 if (token != G_TOKEN_LEFT_PAREN)
2733 token = G_TOKEN_NONE;
2739 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2741 if (token == G_TOKEN_NONE)
2743 g_string_append_c (gstring, ' ');
2744 g_value_init (&prop->value, G_TYPE_GSTRING);
2745 g_value_take_boxed (&prop->value, gstring);
2748 g_string_free (gstring, TRUE);
2753 g_scanner_get_next_token (scanner);
2754 token = G_TOKEN_INT;
2760 /* restore scanner mode */
2761 scanner->config->scan_identifier = scan_identifier;
2762 scanner->config->scan_symbols = scan_symbols;
2763 scanner->config->identifier_2_string = identifier_2_string;
2764 scanner->config->char_2_token = char_2_token;
2765 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2766 scanner->config->numbers_2_int = numbers_2_int;
2772 is_c_identifier (const gchar *string)
2775 gboolean is_varname;
2777 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2778 for (p = string + 1; *p && is_varname; p++)
2779 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2785 parse_include_file (GtkRcContext *context,
2787 const gchar *filename)
2789 char *to_parse = NULL;
2791 if (g_path_is_absolute (filename))
2793 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2794 * don't print an error in this case.
2796 to_parse = g_strdup (filename);
2800 /* if a relative path, we look relative to all the RC files in the
2801 * include stack. We require the file to be found in this case
2802 * so we can give meaningful error messages, and because on reparsing
2803 * non-absolute paths don't make sense.
2805 GSList *tmp_list = current_files_stack;
2808 GtkRcFile *curfile = tmp_list->data;
2809 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2811 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2819 tmp_list = tmp_list->next;
2825 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2830 g_scanner_warn (scanner,
2831 _("Unable to find include file: \"%s\""),
2838 gtk_rc_parse_statement (GtkRcContext *context,
2843 token = g_scanner_peek_next_token (scanner);
2846 case GTK_RC_TOKEN_INCLUDE:
2847 token = g_scanner_get_next_token (scanner);
2848 if (token != GTK_RC_TOKEN_INCLUDE)
2849 return GTK_RC_TOKEN_INCLUDE;
2850 token = g_scanner_get_next_token (scanner);
2851 if (token != G_TOKEN_STRING)
2852 return G_TOKEN_STRING;
2853 parse_include_file (context, scanner, scanner->value.v_string);
2854 return G_TOKEN_NONE;
2856 case GTK_RC_TOKEN_STYLE:
2857 return gtk_rc_parse_style (context, scanner);
2859 case GTK_RC_TOKEN_BINDING:
2860 return _gtk_binding_parse_binding (scanner);
2862 case GTK_RC_TOKEN_PIXMAP_PATH:
2863 return gtk_rc_parse_pixmap_path (context, scanner);
2865 case GTK_RC_TOKEN_WIDGET:
2866 return gtk_rc_parse_path_pattern (context, scanner);
2868 case GTK_RC_TOKEN_WIDGET_CLASS:
2869 return gtk_rc_parse_path_pattern (context, scanner);
2871 case GTK_RC_TOKEN_CLASS:
2872 return gtk_rc_parse_path_pattern (context, scanner);
2874 case GTK_RC_TOKEN_MODULE_PATH:
2875 return gtk_rc_parse_module_path (scanner);
2877 case GTK_RC_TOKEN_IM_MODULE_FILE:
2878 return gtk_rc_parse_im_module_file (scanner);
2880 case G_TOKEN_IDENTIFIER:
2881 if (is_c_identifier (scanner->next_value.v_identifier))
2883 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2886 g_scanner_get_next_token (scanner); /* eat identifier */
2887 name = g_strdup (scanner->value.v_identifier);
2889 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2890 if (token == G_TOKEN_NONE)
2892 GtkSettingsValue svalue;
2894 svalue.origin = prop.origin;
2895 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2896 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2897 _gtk_settings_set_property_value_from_rc (context->settings,
2901 g_free (prop.origin);
2902 if (G_VALUE_TYPE (&prop.value))
2903 g_value_unset (&prop.value);
2910 g_scanner_get_next_token (scanner);
2911 return G_TOKEN_IDENTIFIER;
2914 g_scanner_get_next_token (scanner);
2915 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2920 fixup_rc_set (GSList *list,
2926 GtkRcSet *set = list->data;
2927 if (set->rc_style == orig)
2928 set->rc_style = new;
2934 fixup_rc_sets (GtkRcContext *context,
2938 fixup_rc_set (context->rc_sets_widget, orig, new);
2939 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2940 fixup_rc_set (context->rc_sets_class, orig, new);
2944 gtk_rc_parse_style (GtkRcContext *context,
2947 GtkRcStyle *rc_style;
2948 GtkRcStyle *orig_style;
2949 GtkRcStyle *parent_style = NULL;
2950 GtkRcStylePrivate *rc_priv = NULL;
2953 GtkIconFactory *our_factory = NULL;
2954 GHashTable *our_hash = NULL;
2956 token = g_scanner_get_next_token (scanner);
2957 if (token != GTK_RC_TOKEN_STYLE)
2958 return GTK_RC_TOKEN_STYLE;
2960 token = g_scanner_get_next_token (scanner);
2961 if (token != G_TOKEN_STRING)
2962 return G_TOKEN_STRING;
2964 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2966 orig_style = g_object_ref (rc_style);
2972 rc_style = gtk_rc_style_new ();
2973 rc_style->name = g_strdup (scanner->value.v_string);
2975 for (i = 0; i < 5; i++)
2976 rc_style->bg_pixmap_name[i] = NULL;
2978 for (i = 0; i < 5; i++)
2979 rc_style->color_flags[i] = 0;
2982 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
2984 /* If there's a list, its first member is always the factory belonging
2987 if (rc_style->icon_factories)
2988 our_factory = rc_style->icon_factories->data;
2989 if (rc_priv->color_hashes)
2990 our_hash = rc_priv->color_hashes->data;
2992 token = g_scanner_peek_next_token (scanner);
2993 if (token == G_TOKEN_EQUAL_SIGN)
2995 token = g_scanner_get_next_token (scanner);
2997 token = g_scanner_get_next_token (scanner);
2998 if (token != G_TOKEN_STRING)
3000 token = G_TOKEN_STRING;
3004 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3007 for (i = 0; i < 5; i++)
3009 rc_style->color_flags[i] = parent_style->color_flags[i];
3010 rc_style->fg[i] = parent_style->fg[i];
3011 rc_style->bg[i] = parent_style->bg[i];
3012 rc_style->text[i] = parent_style->text[i];
3013 rc_style->base[i] = parent_style->base[i];
3016 rc_style->xthickness = parent_style->xthickness;
3017 rc_style->ythickness = parent_style->ythickness;
3019 if (parent_style->font_desc)
3021 if (rc_style->font_desc)
3022 pango_font_description_free (rc_style->font_desc);
3023 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3026 if (parent_style->rc_properties)
3030 for (i = 0; i < parent_style->rc_properties->len; i++)
3031 insert_rc_property (rc_style,
3032 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3036 for (i = 0; i < 5; i++)
3038 g_free (rc_style->bg_pixmap_name[i]);
3039 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3044 /* get icon_factories and color_hashes from the parent style;
3045 * if the parent_style doesn't have color_hashes, initializes
3046 * the color_hashes with the settings' color scheme (if it exists)
3048 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3050 if (rc_style->icon_factories)
3051 our_factory = rc_style->icon_factories->data;
3052 if (rc_priv->color_hashes)
3053 our_hash = rc_priv->color_hashes->data;
3055 token = g_scanner_get_next_token (scanner);
3056 if (token != G_TOKEN_LEFT_CURLY)
3058 token = G_TOKEN_LEFT_CURLY;
3062 token = g_scanner_peek_next_token (scanner);
3063 while (token != G_TOKEN_RIGHT_CURLY)
3067 case GTK_RC_TOKEN_BG:
3068 token = gtk_rc_parse_bg (scanner, rc_style);
3070 case GTK_RC_TOKEN_FG:
3071 token = gtk_rc_parse_fg (scanner, rc_style);
3073 case GTK_RC_TOKEN_TEXT:
3074 token = gtk_rc_parse_text (scanner, rc_style);
3076 case GTK_RC_TOKEN_BASE:
3077 token = gtk_rc_parse_base (scanner, rc_style);
3079 case GTK_RC_TOKEN_XTHICKNESS:
3080 token = gtk_rc_parse_xthickness (scanner, rc_style);
3082 case GTK_RC_TOKEN_YTHICKNESS:
3083 token = gtk_rc_parse_ythickness (scanner, rc_style);
3085 case GTK_RC_TOKEN_BG_PIXMAP:
3086 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3088 case GTK_RC_TOKEN_FONT:
3089 token = gtk_rc_parse_font (scanner, rc_style);
3091 case GTK_RC_TOKEN_FONTSET:
3092 token = gtk_rc_parse_fontset (scanner, rc_style);
3094 case GTK_RC_TOKEN_FONT_NAME:
3095 token = gtk_rc_parse_font_name (scanner, rc_style);
3097 case GTK_RC_TOKEN_ENGINE:
3098 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3100 case GTK_RC_TOKEN_STOCK:
3101 if (our_factory == NULL)
3102 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3103 our_factory = rc_style->icon_factories->data;
3104 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3106 case GTK_RC_TOKEN_COLOR:
3107 if (our_hash == NULL)
3109 gtk_rc_style_prepend_empty_color_hash (rc_style);
3110 our_hash = rc_priv->color_hashes->data;
3112 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3114 case G_TOKEN_IDENTIFIER:
3115 if (is_c_identifier (scanner->next_value.v_identifier))
3117 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3120 g_scanner_get_next_token (scanner); /* eat type name */
3121 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3122 if (g_scanner_get_next_token (scanner) != ':' ||
3123 g_scanner_get_next_token (scanner) != ':')
3128 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3129 !is_c_identifier (scanner->value.v_identifier))
3131 token = G_TOKEN_IDENTIFIER;
3135 /* it's important that we do the same canonification as GParamSpecPool here */
3136 name = g_strdup (scanner->value.v_identifier);
3137 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3138 prop.property_name = g_quark_from_string (name);
3141 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3142 if (token == G_TOKEN_NONE)
3144 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3145 insert_rc_property (rc_style, &prop, TRUE);
3148 g_free (prop.origin);
3149 if (G_VALUE_TYPE (&prop.value))
3150 g_value_unset (&prop.value);
3154 g_scanner_get_next_token (scanner);
3155 token = G_TOKEN_IDENTIFIER;
3159 g_scanner_get_next_token (scanner);
3160 token = G_TOKEN_RIGHT_CURLY;
3164 if (token != G_TOKEN_NONE)
3167 token = g_scanner_peek_next_token (scanner);
3168 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3170 token = g_scanner_get_next_token (scanner);
3171 if (token != G_TOKEN_RIGHT_CURLY)
3173 token = G_TOKEN_RIGHT_CURLY;
3177 if (rc_style != orig_style)
3179 if (!context->rc_style_ht)
3180 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3181 (GEqualFunc) gtk_rc_style_equal);
3183 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3185 /* If we copied the data into a new rc style, fix up references to the old rc style
3186 * in bindings that we have.
3189 fixup_rc_sets (context, orig_style, rc_style);
3193 g_object_unref (orig_style);
3195 return G_TOKEN_NONE;
3198 if (rc_style != orig_style)
3199 g_object_unref (rc_style);
3202 g_object_unref (orig_style);
3207 const GtkRcProperty*
3208 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3210 GQuark property_name)
3212 GtkRcProperty *node = NULL;
3214 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3216 if (rc_style->rc_properties)
3220 key.type_name = type_name;
3221 key.property_name = property_name;
3223 node = bsearch (&key,
3224 rc_style->rc_properties->data, rc_style->rc_properties->len,
3225 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3232 gtk_rc_parse_bg (GScanner *scanner,
3238 token = g_scanner_get_next_token (scanner);
3239 if (token != GTK_RC_TOKEN_BG)
3240 return GTK_RC_TOKEN_BG;
3242 token = gtk_rc_parse_state (scanner, &state);
3243 if (token != G_TOKEN_NONE)
3246 token = g_scanner_get_next_token (scanner);
3247 if (token != G_TOKEN_EQUAL_SIGN)
3248 return G_TOKEN_EQUAL_SIGN;
3250 style->color_flags[state] |= GTK_RC_BG;
3251 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3255 gtk_rc_parse_fg (GScanner *scanner,
3261 token = g_scanner_get_next_token (scanner);
3262 if (token != GTK_RC_TOKEN_FG)
3263 return GTK_RC_TOKEN_FG;
3265 token = gtk_rc_parse_state (scanner, &state);
3266 if (token != G_TOKEN_NONE)
3269 token = g_scanner_get_next_token (scanner);
3270 if (token != G_TOKEN_EQUAL_SIGN)
3271 return G_TOKEN_EQUAL_SIGN;
3273 style->color_flags[state] |= GTK_RC_FG;
3274 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3278 gtk_rc_parse_text (GScanner *scanner,
3284 token = g_scanner_get_next_token (scanner);
3285 if (token != GTK_RC_TOKEN_TEXT)
3286 return GTK_RC_TOKEN_TEXT;
3288 token = gtk_rc_parse_state (scanner, &state);
3289 if (token != G_TOKEN_NONE)
3292 token = g_scanner_get_next_token (scanner);
3293 if (token != G_TOKEN_EQUAL_SIGN)
3294 return G_TOKEN_EQUAL_SIGN;
3296 style->color_flags[state] |= GTK_RC_TEXT;
3297 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3301 gtk_rc_parse_base (GScanner *scanner,
3307 token = g_scanner_get_next_token (scanner);
3308 if (token != GTK_RC_TOKEN_BASE)
3309 return GTK_RC_TOKEN_BASE;
3311 token = gtk_rc_parse_state (scanner, &state);
3312 if (token != G_TOKEN_NONE)
3315 token = g_scanner_get_next_token (scanner);
3316 if (token != G_TOKEN_EQUAL_SIGN)
3317 return G_TOKEN_EQUAL_SIGN;
3319 style->color_flags[state] |= GTK_RC_BASE;
3320 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3324 gtk_rc_parse_xthickness (GScanner *scanner,
3327 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3328 return GTK_RC_TOKEN_XTHICKNESS;
3330 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3331 return G_TOKEN_EQUAL_SIGN;
3333 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3336 style->xthickness = scanner->value.v_int;
3338 return G_TOKEN_NONE;
3342 gtk_rc_parse_ythickness (GScanner *scanner,
3345 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3346 return GTK_RC_TOKEN_YTHICKNESS;
3348 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3349 return G_TOKEN_EQUAL_SIGN;
3351 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3354 style->ythickness = scanner->value.v_int;
3356 return G_TOKEN_NONE;
3360 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3362 GtkRcStyle *rc_style)
3368 token = g_scanner_get_next_token (scanner);
3369 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3370 return GTK_RC_TOKEN_BG_PIXMAP;
3372 token = gtk_rc_parse_state (scanner, &state);
3373 if (token != G_TOKEN_NONE)
3376 token = g_scanner_get_next_token (scanner);
3377 if (token != G_TOKEN_EQUAL_SIGN)
3378 return G_TOKEN_EQUAL_SIGN;
3380 token = g_scanner_get_next_token (scanner);
3381 if (token != G_TOKEN_STRING)
3382 return G_TOKEN_STRING;
3384 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3385 (strcmp (scanner->value.v_string, "<none>") == 0))
3386 pixmap_file = g_strdup (scanner->value.v_string);
3388 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3389 scanner, scanner->value.v_string);
3393 g_free (rc_style->bg_pixmap_name[state]);
3394 rc_style->bg_pixmap_name[state] = pixmap_file;
3397 return G_TOKEN_NONE;
3401 gtk_rc_check_pixmap_dir (const gchar *dir,
3402 const gchar *pixmap_file)
3406 buf = g_build_filename (dir, pixmap_file, NULL);
3408 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3417 * gtk_rc_find_pixmap_in_path:
3418 * @settings: a #GtkSettings
3419 * @scanner: Scanner used to get line number information for the
3420 * warning message, or %NULL
3421 * @pixmap_file: name of the pixmap file to locate.
3423 * Looks up a file in pixmap path for the specified #GtkSettings.
3424 * If the file is not found, it outputs a warning message using
3425 * g_warning() and returns %NULL.
3427 * Return value: the filename.
3430 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3432 const gchar *pixmap_file)
3438 GtkRcContext *context = gtk_rc_context_get (settings);
3440 if (context->pixmap_path)
3441 for (i = 0; context->pixmap_path[i] != NULL; i++)
3443 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3448 tmp_list = current_files_stack;
3451 GtkRcFile *curfile = tmp_list->data;
3452 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3456 tmp_list = tmp_list->next;
3460 g_scanner_warn (scanner,
3461 _("Unable to locate image file in pixmap_path: \"%s\""),
3464 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3471 * gtk_rc_find_module_in_path:
3472 * @module_file: name of a theme engine
3474 * Searches for a theme engine in the GTK+ search path. This function
3475 * is not useful for applications and should not be used.
3477 * Return value: The filename, if found (must be freed with g_free()),
3481 gtk_rc_find_module_in_path (const gchar *module_file)
3483 return _gtk_find_module (module_file, "engines");
3487 gtk_rc_parse_font (GScanner *scanner,
3488 GtkRcStyle *rc_style)
3492 token = g_scanner_get_next_token (scanner);
3493 if (token != GTK_RC_TOKEN_FONT)
3494 return GTK_RC_TOKEN_FONT;
3496 token = g_scanner_get_next_token (scanner);
3497 if (token != G_TOKEN_EQUAL_SIGN)
3498 return G_TOKEN_EQUAL_SIGN;
3500 token = g_scanner_get_next_token (scanner);
3501 if (token != G_TOKEN_STRING)
3502 return G_TOKEN_STRING;
3504 /* Ignore, do nothing */
3506 return G_TOKEN_NONE;
3510 gtk_rc_parse_fontset (GScanner *scanner,
3511 GtkRcStyle *rc_style)
3515 token = g_scanner_get_next_token (scanner);
3516 if (token != GTK_RC_TOKEN_FONTSET)
3517 return GTK_RC_TOKEN_FONTSET;
3519 token = g_scanner_get_next_token (scanner);
3520 if (token != G_TOKEN_EQUAL_SIGN)
3521 return G_TOKEN_EQUAL_SIGN;
3523 token = g_scanner_get_next_token (scanner);
3524 if (token != G_TOKEN_STRING)
3525 return G_TOKEN_STRING;
3527 /* Do nothing - silently ignore */
3529 return G_TOKEN_NONE;
3533 gtk_rc_parse_font_name (GScanner *scanner,
3534 GtkRcStyle *rc_style)
3538 token = g_scanner_get_next_token (scanner);
3539 if (token != GTK_RC_TOKEN_FONT_NAME)
3540 return GTK_RC_TOKEN_FONT;
3542 token = g_scanner_get_next_token (scanner);
3543 if (token != G_TOKEN_EQUAL_SIGN)
3544 return G_TOKEN_EQUAL_SIGN;
3546 token = g_scanner_get_next_token (scanner);
3547 if (token != G_TOKEN_STRING)
3548 return G_TOKEN_STRING;
3550 if (rc_style->font_desc)
3551 pango_font_description_free (rc_style->font_desc);
3553 rc_style->font_desc =
3554 pango_font_description_from_string (scanner->value.v_string);
3556 return G_TOKEN_NONE;
3560 gtk_rc_parse_engine (GtkRcContext *context,
3562 GtkRcStyle **rc_style)
3565 GtkThemeEngine *engine;
3566 guint result = G_TOKEN_NONE;
3567 GtkRcStyle *new_style = NULL;
3568 gboolean parsed_curlies = FALSE;
3569 GtkRcStylePrivate *rc_priv, *new_priv;
3571 token = g_scanner_get_next_token (scanner);
3572 if (token != GTK_RC_TOKEN_ENGINE)
3573 return GTK_RC_TOKEN_ENGINE;
3575 token = g_scanner_get_next_token (scanner);
3576 if (token != G_TOKEN_STRING)
3577 return G_TOKEN_STRING;
3579 if (!scanner->value.v_string[0])
3581 /* Support engine "" {} to mean override to the default engine
3583 token = g_scanner_get_next_token (scanner);
3584 if (token != G_TOKEN_LEFT_CURLY)
3585 return G_TOKEN_LEFT_CURLY;
3587 token = g_scanner_get_next_token (scanner);
3588 if (token != G_TOKEN_RIGHT_CURLY)
3589 return G_TOKEN_RIGHT_CURLY;
3591 parsed_curlies = TRUE;
3593 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3595 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3597 new_style = gtk_rc_style_new ();
3598 gtk_rc_style_real_merge (new_style, *rc_style);
3600 new_style->name = g_strdup ((*rc_style)->name);
3602 /* take over icon factories and color hashes
3603 * from the to-be-deleted style
3605 new_style->icon_factories = (*rc_style)->icon_factories;
3606 (*rc_style)->icon_factories = NULL;
3607 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3608 new_priv->color_hashes = rc_priv->color_hashes;
3609 rc_priv->color_hashes = NULL;
3612 (*rc_style)->engine_specified = TRUE;
3616 engine = gtk_theme_engine_get (scanner->value.v_string);
3618 token = g_scanner_get_next_token (scanner);
3619 if (token != G_TOKEN_LEFT_CURLY)
3620 return G_TOKEN_LEFT_CURLY;
3624 GtkRcStyleClass *new_class;
3626 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3627 new_style = gtk_theme_engine_create_rc_style (engine);
3628 g_type_module_unuse (G_TYPE_MODULE (engine));
3630 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3632 new_class->merge (new_style, *rc_style);
3634 new_style->name = g_strdup ((*rc_style)->name);
3636 /* take over icon factories and color hashes
3637 * from the to-be-deleted style
3639 new_style->icon_factories = (*rc_style)->icon_factories;
3640 (*rc_style)->icon_factories = NULL;
3641 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3642 new_priv->color_hashes = rc_priv->color_hashes;
3643 rc_priv->color_hashes = NULL;
3645 if (new_class->parse)
3647 parsed_curlies = TRUE;
3648 result = new_class->parse (new_style, context->settings, scanner);
3650 if (result != G_TOKEN_NONE)
3652 /* copy icon factories and color hashes back
3654 (*rc_style)->icon_factories = new_style->icon_factories;
3655 new_style->icon_factories = NULL;
3656 rc_priv->color_hashes = new_priv->color_hashes;
3657 new_priv->color_hashes = NULL;
3659 g_object_unref (new_style);
3666 if (!parsed_curlies)
3668 /* Skip over remainder, looking for nested {}'s
3672 result = G_TOKEN_RIGHT_CURLY;
3673 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3675 if (token == G_TOKEN_LEFT_CURLY)
3677 else if (token == G_TOKEN_RIGHT_CURLY)
3682 result = G_TOKEN_NONE;
3690 new_style->engine_specified = TRUE;
3692 g_object_unref (*rc_style);
3693 *rc_style = new_style;
3700 gtk_rc_parse_state (GScanner *scanner,
3701 GtkStateType *state)
3706 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3707 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3709 /* we don't know where we got called from, so we reset the scope here.
3710 * if we bail out due to errors, we *don't* reset the scope, so the
3711 * error messaging code can make sense of our tokens.
3713 old_scope = g_scanner_set_scope (scanner, 0);
3715 token = g_scanner_get_next_token (scanner);
3716 if (token != G_TOKEN_LEFT_BRACE)
3717 return G_TOKEN_LEFT_BRACE;
3719 token = g_scanner_get_next_token (scanner);
3722 case GTK_RC_TOKEN_ACTIVE:
3723 *state = GTK_STATE_ACTIVE;
3725 case GTK_RC_TOKEN_INSENSITIVE:
3726 *state = GTK_STATE_INSENSITIVE;
3728 case GTK_RC_TOKEN_NORMAL:
3729 *state = GTK_STATE_NORMAL;
3731 case GTK_RC_TOKEN_PRELIGHT:
3732 *state = GTK_STATE_PRELIGHT;
3734 case GTK_RC_TOKEN_SELECTED:
3735 *state = GTK_STATE_SELECTED;
3738 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3741 token = g_scanner_get_next_token (scanner);
3742 if (token != G_TOKEN_RIGHT_BRACE)
3743 return G_TOKEN_RIGHT_BRACE;
3745 g_scanner_set_scope (scanner, old_scope);
3747 return G_TOKEN_NONE;
3751 gtk_rc_parse_priority (GScanner *scanner,
3752 GtkPathPriorityType *priority)
3757 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3758 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3760 /* we don't know where we got called from, so we reset the scope here.
3761 * if we bail out due to errors, we *don't* reset the scope, so the
3762 * error messaging code can make sense of our tokens.
3764 old_scope = g_scanner_set_scope (scanner, 0);
3766 token = g_scanner_get_next_token (scanner);
3770 token = g_scanner_get_next_token (scanner);
3773 case GTK_RC_TOKEN_LOWEST:
3774 *priority = GTK_PATH_PRIO_LOWEST;
3776 case GTK_RC_TOKEN_GTK:
3777 *priority = GTK_PATH_PRIO_GTK;
3779 case GTK_RC_TOKEN_APPLICATION:
3780 *priority = GTK_PATH_PRIO_APPLICATION;
3782 case GTK_RC_TOKEN_THEME:
3783 *priority = GTK_PATH_PRIO_THEME;
3785 case GTK_RC_TOKEN_RC:
3786 *priority = GTK_PATH_PRIO_RC;
3788 case GTK_RC_TOKEN_HIGHEST:
3789 *priority = GTK_PATH_PRIO_HIGHEST;
3792 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3795 g_scanner_set_scope (scanner, old_scope);
3797 return G_TOKEN_NONE;
3801 * gtk_rc_parse_color:
3802 * @scanner: a #GScanner
3803 * @color: a pointer to a #GdkColor structure in which to store the result
3805 * Parses a color in the <link linkend="color=format">format</link> expected
3808 * Note that theme engines should use gtk_rc_parse_color_full() in
3809 * order to support symbolic colors.
3811 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3812 * that was expected but not found
3815 gtk_rc_parse_color (GScanner *scanner,
3818 return gtk_rc_parse_color_full (scanner, NULL, color);
3822 * gtk_rc_parse_color_full:
3823 * @scanner: a #GScanner
3824 * @style: (allow-none): a #GtkRcStyle, or %NULL
3825 * @color: a pointer to a #GdkColor structure in which to store the result
3827 * Parses a color in the <link linkend="color=format">format</link> expected
3828 * in a RC file. If @style is not %NULL, it will be consulted to resolve
3829 * references to symbolic colors.
3831 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3832 * that was expected but not found
3837 gtk_rc_parse_color_full (GScanner *scanner,
3843 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3845 /* we don't need to set our own scope here, because
3846 * we don't need own symbols
3849 token = g_scanner_get_next_token (scanner);
3857 case G_TOKEN_LEFT_CURLY:
3858 token = g_scanner_get_next_token (scanner);
3859 if (token == G_TOKEN_INT)
3860 token_int = scanner->value.v_int;
3861 else if (token == G_TOKEN_FLOAT)
3862 token_int = scanner->value.v_float * 65535.0;
3864 return G_TOKEN_FLOAT;
3865 color->red = CLAMP (token_int, 0, 65535);
3867 token = g_scanner_get_next_token (scanner);
3868 if (token != G_TOKEN_COMMA)
3869 return G_TOKEN_COMMA;
3871 token = g_scanner_get_next_token (scanner);
3872 if (token == G_TOKEN_INT)
3873 token_int = scanner->value.v_int;
3874 else if (token == G_TOKEN_FLOAT)
3875 token_int = scanner->value.v_float * 65535.0;
3877 return G_TOKEN_FLOAT;
3878 color->green = CLAMP (token_int, 0, 65535);
3880 token = g_scanner_get_next_token (scanner);
3881 if (token != G_TOKEN_COMMA)
3882 return G_TOKEN_COMMA;
3884 token = g_scanner_get_next_token (scanner);
3885 if (token == G_TOKEN_INT)
3886 token_int = scanner->value.v_int;
3887 else if (token == G_TOKEN_FLOAT)
3888 token_int = scanner->value.v_float * 65535.0;
3890 return G_TOKEN_FLOAT;
3891 color->blue = CLAMP (token_int, 0, 65535);
3893 token = g_scanner_get_next_token (scanner);
3894 if (token != G_TOKEN_RIGHT_CURLY)
3895 return G_TOKEN_RIGHT_CURLY;
3896 return G_TOKEN_NONE;
3898 case G_TOKEN_STRING:
3899 if (!gdk_color_parse (scanner->value.v_string, color))
3901 g_scanner_warn (scanner, "Invalid color constant '%s'",
3902 scanner->value.v_string);
3903 return G_TOKEN_STRING;
3905 return G_TOKEN_NONE;
3908 token = g_scanner_get_next_token (scanner);
3909 if (token != G_TOKEN_IDENTIFIER)
3910 return G_TOKEN_IDENTIFIER;
3912 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3914 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3915 scanner->value.v_identifier);
3916 return G_TOKEN_IDENTIFIER;
3919 return G_TOKEN_NONE;
3921 case G_TOKEN_IDENTIFIER:
3922 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3924 token = g_scanner_get_next_token (scanner);
3925 if (token != G_TOKEN_LEFT_PAREN)
3926 return G_TOKEN_LEFT_PAREN;
3929 if (g_scanner_peek_next_token (scanner) == '-')
3931 g_scanner_get_next_token (scanner); /* eat sign */
3935 token = g_scanner_get_next_token (scanner);
3936 if (token != G_TOKEN_FLOAT)
3937 return G_TOKEN_FLOAT;
3939 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3941 token = g_scanner_get_next_token (scanner);
3942 if (token != G_TOKEN_COMMA)
3943 return G_TOKEN_COMMA;
3945 token = gtk_rc_parse_color_full (scanner, style, &c1);
3946 if (token != G_TOKEN_NONE)
3949 token = g_scanner_get_next_token (scanner);
3950 if (token != G_TOKEN_COMMA)
3951 return G_TOKEN_COMMA;
3953 token = gtk_rc_parse_color_full (scanner, style, &c2);
3954 if (token != G_TOKEN_NONE)
3957 token = g_scanner_get_next_token (scanner);
3958 if (token != G_TOKEN_RIGHT_PAREN)
3959 return G_TOKEN_RIGHT_PAREN;
3961 color->red = l * c1.red + (1.0 - l) * c2.red;
3962 color->green = l * c1.green + (1.0 - l) * c2.green;
3963 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3965 return G_TOKEN_NONE;
3967 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
3969 token = g_scanner_get_next_token (scanner);
3970 if (token != G_TOKEN_LEFT_PAREN)
3971 return G_TOKEN_LEFT_PAREN;
3974 if (g_scanner_peek_next_token (scanner) == '-')
3976 g_scanner_get_next_token (scanner); /* eat sign */
3980 token = g_scanner_get_next_token (scanner);
3981 if (token != G_TOKEN_FLOAT)
3982 return G_TOKEN_FLOAT;
3984 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3986 token = g_scanner_get_next_token (scanner);
3987 if (token != G_TOKEN_COMMA)
3988 return G_TOKEN_COMMA;
3990 token = gtk_rc_parse_color_full (scanner, style, &c1);
3991 if (token != G_TOKEN_NONE)
3994 token = g_scanner_get_next_token (scanner);
3995 if (token != G_TOKEN_RIGHT_PAREN)
3996 return G_TOKEN_RIGHT_PAREN;
3998 _gtk_style_shade (&c1, color, l);
4000 return G_TOKEN_NONE;
4002 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4003 strcmp (scanner->value.v_identifier, "darker") == 0)
4005 if (scanner->value.v_identifier[0] == 'l')
4010 token = g_scanner_get_next_token (scanner);
4011 if (token != G_TOKEN_LEFT_PAREN)
4012 return G_TOKEN_LEFT_PAREN;
4014 token = gtk_rc_parse_color_full (scanner, style, &c1);
4015 if (token != G_TOKEN_NONE)
4018 token = g_scanner_get_next_token (scanner);
4019 if (token != G_TOKEN_RIGHT_PAREN)
4020 return G_TOKEN_RIGHT_PAREN;
4022 _gtk_style_shade (&c1, color, l);
4024 return G_TOKEN_NONE;
4027 return G_TOKEN_IDENTIFIER;
4030 return G_TOKEN_STRING;
4035 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4040 token = g_scanner_get_next_token (scanner);
4041 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4042 return GTK_RC_TOKEN_PIXMAP_PATH;
4044 token = g_scanner_get_next_token (scanner);
4045 if (token != G_TOKEN_STRING)
4046 return G_TOKEN_STRING;
4048 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4050 return G_TOKEN_NONE;
4054 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4056 const gchar *pix_path)
4058 g_strfreev (context->pixmap_path);
4059 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4063 gtk_rc_parse_module_path (GScanner *scanner)
4067 token = g_scanner_get_next_token (scanner);
4068 if (token != GTK_RC_TOKEN_MODULE_PATH)
4069 return GTK_RC_TOKEN_MODULE_PATH;
4071 token = g_scanner_get_next_token (scanner);
4072 if (token != G_TOKEN_STRING)
4073 return G_TOKEN_STRING;
4075 g_warning ("module_path directive is now ignored\n");
4077 return G_TOKEN_NONE;
4081 gtk_rc_parse_im_module_file (GScanner *scanner)
4085 token = g_scanner_get_next_token (scanner);
4086 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4087 return GTK_RC_TOKEN_IM_MODULE_FILE;
4089 token = g_scanner_get_next_token (scanner);
4090 if (token != G_TOKEN_STRING)
4091 return G_TOKEN_STRING;
4093 g_free (im_module_file);
4095 im_module_file = g_strdup (scanner->value.v_string);
4097 return G_TOKEN_NONE;
4101 gtk_rc_parse_path_pattern (GtkRcContext *context,
4105 GtkPathType path_type;
4107 gboolean is_binding;
4108 GtkPathPriorityType priority = context->default_priority;
4110 token = g_scanner_get_next_token (scanner);
4113 case GTK_RC_TOKEN_WIDGET:
4114 path_type = GTK_PATH_WIDGET;
4116 case GTK_RC_TOKEN_WIDGET_CLASS:
4117 path_type = GTK_PATH_WIDGET_CLASS;
4119 case GTK_RC_TOKEN_CLASS:
4120 path_type = GTK_PATH_CLASS;
4123 return GTK_RC_TOKEN_WIDGET_CLASS;
4126 token = g_scanner_get_next_token (scanner);
4127 if (token != G_TOKEN_STRING)
4128 return G_TOKEN_STRING;
4130 pattern = g_strdup (scanner->value.v_string);
4132 token = g_scanner_get_next_token (scanner);
4133 if (token == GTK_RC_TOKEN_STYLE)
4135 else if (token == GTK_RC_TOKEN_BINDING)
4140 return GTK_RC_TOKEN_STYLE;
4143 if (g_scanner_peek_next_token (scanner) == ':')
4145 token = gtk_rc_parse_priority (scanner, &priority);
4146 if (token != G_TOKEN_NONE)
4153 token = g_scanner_get_next_token (scanner);
4154 if (token != G_TOKEN_STRING)
4157 return G_TOKEN_STRING;
4162 GtkBindingSet *binding;
4164 binding = gtk_binding_set_find (scanner->value.v_string);
4168 return G_TOKEN_STRING;
4170 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4174 GtkRcStyle *rc_style;
4177 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4182 return G_TOKEN_STRING;
4185 rc_set = g_new (GtkRcSet, 1);
4186 rc_set->type = path_type;
4188 if (path_type == GTK_PATH_WIDGET_CLASS)
4190 rc_set->pspec = NULL;
4191 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4195 rc_set->pspec = g_pattern_spec_new (pattern);
4196 rc_set->path = NULL;
4199 rc_set->rc_style = rc_style;
4200 rc_set->priority = priority;
4202 if (path_type == GTK_PATH_WIDGET)
4203 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4204 else if (path_type == GTK_PATH_WIDGET_CLASS)
4205 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4207 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4211 return G_TOKEN_NONE;
4215 gtk_rc_parse_hash_key (GScanner *scanner,
4220 token = g_scanner_get_next_token (scanner);
4221 if (token != G_TOKEN_LEFT_BRACE)
4222 return G_TOKEN_LEFT_BRACE;
4224 token = g_scanner_get_next_token (scanner);
4226 if (token != G_TOKEN_STRING)
4227 return G_TOKEN_STRING;
4229 *hash_key = g_strdup (scanner->value.v_string);
4231 token = g_scanner_get_next_token (scanner);
4232 if (token != G_TOKEN_RIGHT_BRACE)
4235 return G_TOKEN_RIGHT_BRACE;
4238 return G_TOKEN_NONE;
4242 gtk_rc_parse_icon_source (GtkRcContext *context,
4244 GtkIconSet *icon_set,
4245 gboolean *icon_set_valid)
4248 gchar *full_filename;
4249 GtkIconSource *source = NULL;
4251 token = g_scanner_get_next_token (scanner);
4252 if (token != G_TOKEN_LEFT_CURLY)
4253 return G_TOKEN_LEFT_CURLY;
4255 token = g_scanner_get_next_token (scanner);
4257 if (token != G_TOKEN_STRING && token != '@')
4258 return G_TOKEN_STRING;
4260 if (token == G_TOKEN_STRING)
4264 source = gtk_icon_source_new ();
4265 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4268 gtk_icon_source_set_filename (source, full_filename);
4269 g_free (full_filename);
4276 token = g_scanner_get_next_token (scanner);
4278 if (token != G_TOKEN_STRING)
4279 return G_TOKEN_STRING;
4281 source = gtk_icon_source_new ();
4282 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4285 /* We continue parsing even if we didn't find the pixmap so that rest of the
4286 * file is read, even if the syntax is bad. However we don't validate the
4287 * icon_set so the caller can choose not to install it.
4289 token = g_scanner_get_next_token (scanner);
4291 if (token == G_TOKEN_RIGHT_CURLY)
4293 else if (token != G_TOKEN_COMMA)
4295 gtk_icon_source_free (source);
4296 return G_TOKEN_COMMA;
4299 /* Get the direction */
4301 token = g_scanner_get_next_token (scanner);
4305 case GTK_RC_TOKEN_RTL:
4306 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4307 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4310 case GTK_RC_TOKEN_LTR:
4311 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4312 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4319 gtk_icon_source_free (source);
4320 return GTK_RC_TOKEN_RTL;
4324 token = g_scanner_get_next_token (scanner);
4326 if (token == G_TOKEN_RIGHT_CURLY)
4328 else if (token != G_TOKEN_COMMA)
4330 gtk_icon_source_free (source);
4331 return G_TOKEN_COMMA;
4336 token = g_scanner_get_next_token (scanner);
4340 case GTK_RC_TOKEN_NORMAL:
4341 gtk_icon_source_set_state_wildcarded (source, FALSE);
4342 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4345 case GTK_RC_TOKEN_PRELIGHT:
4346 gtk_icon_source_set_state_wildcarded (source, FALSE);
4347 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4351 case GTK_RC_TOKEN_INSENSITIVE:
4352 gtk_icon_source_set_state_wildcarded (source, FALSE);
4353 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4356 case GTK_RC_TOKEN_ACTIVE:
4357 gtk_icon_source_set_state_wildcarded (source, FALSE);
4358 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4361 case GTK_RC_TOKEN_SELECTED:
4362 gtk_icon_source_set_state_wildcarded (source, FALSE);
4363 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4370 gtk_icon_source_free (source);
4371 return GTK_RC_TOKEN_PRELIGHT;
4375 token = g_scanner_get_next_token (scanner);
4377 if (token == G_TOKEN_RIGHT_CURLY)
4379 else if (token != G_TOKEN_COMMA)
4381 gtk_icon_source_free (source);
4382 return G_TOKEN_COMMA;
4387 token = g_scanner_get_next_token (scanner);
4393 if (token != G_TOKEN_STRING)
4395 gtk_icon_source_free (source);
4396 return G_TOKEN_STRING;
4399 size = gtk_icon_size_from_name (scanner->value.v_string);
4401 if (size != GTK_ICON_SIZE_INVALID)
4403 gtk_icon_source_set_size_wildcarded (source, FALSE);
4404 gtk_icon_source_set_size (source, size);
4408 /* Check the close brace */
4410 token = g_scanner_get_next_token (scanner);
4411 if (token != G_TOKEN_RIGHT_CURLY)
4413 gtk_icon_source_free (source);
4414 return G_TOKEN_RIGHT_CURLY;
4418 if (gtk_icon_source_get_filename (source) ||
4419 gtk_icon_source_get_icon_name (source))
4421 gtk_icon_set_add_source (icon_set, source);
4422 *icon_set_valid = TRUE;
4424 gtk_icon_source_free (source);
4426 return G_TOKEN_NONE;
4430 gtk_rc_parse_stock (GtkRcContext *context,
4432 GtkRcStyle *rc_style,
4433 GtkIconFactory *factory)
4435 GtkIconSet *icon_set = NULL;
4436 gboolean icon_set_valid = FALSE;
4437 gchar *stock_id = NULL;
4440 token = g_scanner_get_next_token (scanner);
4441 if (token != GTK_RC_TOKEN_STOCK)
4442 return GTK_RC_TOKEN_STOCK;
4444 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4445 if (token != G_TOKEN_NONE)
4448 token = g_scanner_get_next_token (scanner);
4449 if (token != G_TOKEN_EQUAL_SIGN)
4452 return G_TOKEN_EQUAL_SIGN;
4455 token = g_scanner_get_next_token (scanner);
4456 if (token != G_TOKEN_LEFT_CURLY)
4459 return G_TOKEN_LEFT_CURLY;
4462 token = g_scanner_peek_next_token (scanner);
4463 while (token != G_TOKEN_RIGHT_CURLY)
4465 if (icon_set == NULL)
4466 icon_set = gtk_icon_set_new ();
4468 token = gtk_rc_parse_icon_source (context,
4469 scanner, icon_set, &icon_set_valid);
4470 if (token != G_TOKEN_NONE)
4473 gtk_icon_set_unref (icon_set);
4477 token = g_scanner_get_next_token (scanner);
4479 if (token != G_TOKEN_COMMA &&
4480 token != G_TOKEN_RIGHT_CURLY)
4483 gtk_icon_set_unref (icon_set);
4484 return G_TOKEN_RIGHT_CURLY;
4491 gtk_icon_factory_add (factory,
4495 gtk_icon_set_unref (icon_set);
4500 return G_TOKEN_NONE;
4504 gtk_rc_parse_logical_color (GScanner *scanner,
4505 GtkRcStyle *rc_style,
4508 gchar *color_id = NULL;
4512 token = g_scanner_get_next_token (scanner);
4513 if (token != GTK_RC_TOKEN_COLOR)
4514 return GTK_RC_TOKEN_COLOR;
4516 token = gtk_rc_parse_hash_key (scanner, &color_id);
4517 if (token != G_TOKEN_NONE)
4520 token = g_scanner_get_next_token (scanner);
4521 if (token != G_TOKEN_EQUAL_SIGN)
4524 return G_TOKEN_EQUAL_SIGN;
4527 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4528 if (token != G_TOKEN_NONE)
4534 /* Because the hash is created with destroy functions,
4535 * g_hash_table_insert will free any old values for us,
4536 * if a mapping with the specified key already exists.
4538 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4540 return G_TOKEN_NONE;
4545 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4549 const gchar *current;
4550 const gchar *class_start;
4551 const gchar *class_end;
4552 const gchar *pattern_end;
4553 const gchar *pattern_start;
4558 while ((class_start = strchr (current, '<')) &&
4559 (class_end = strchr (class_start, '>')))
4561 /* Add patterns, but ignore single dots */
4562 if (!(class_start == current ||
4563 (class_start == current + 1 && current[0] == '.')))
4565 pattern_end = class_start - 1;
4566 pattern_start = current;
4568 path_elt = g_new (PathElt, 1);
4570 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4571 path_elt->type = PATH_ELT_PSPEC;
4572 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4573 g_free (sub_pattern);
4575 result = g_slist_prepend (result, path_elt);
4578 path_elt = g_new (PathElt, 1);
4580 /* The < > need to be removed from the string. */
4581 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4583 path_elt->type = PATH_ELT_UNRESOLVED;
4584 path_elt->elt.class_name = sub_pattern;
4586 result = g_slist_prepend (result, path_elt);
4588 current = class_end + 1;
4591 /* Add the rest, if anything is left */
4592 if (strlen (current) > 0)
4594 path_elt = g_new (PathElt, 1);
4595 path_elt->type = PATH_ELT_PSPEC;
4596 path_elt->elt.pspec = g_pattern_spec_new (current);
4598 result = g_slist_prepend (result, path_elt);
4601 return g_slist_reverse (result);
4605 free_path_elt (gpointer data,
4608 PathElt *path_elt = data;
4610 switch (path_elt->type)
4612 case PATH_ELT_PSPEC:
4613 g_pattern_spec_free (path_elt->elt.pspec);
4615 case PATH_ELT_UNRESOLVED:
4616 g_free (path_elt->elt.class_name);
4621 g_assert_not_reached ();
4628 _gtk_rc_free_widget_class_path (GSList *list)
4630 g_slist_foreach (list, free_path_elt, NULL);
4631 g_slist_free (list);
4635 gtk_rc_set_free (GtkRcSet *rc_set)
4638 g_pattern_spec_free (rc_set->pspec);
4640 _gtk_rc_free_widget_class_path (rc_set->path);
4646 match_class (PathElt *path_elt,
4651 if (path_elt->type == PATH_ELT_UNRESOLVED)
4653 type = g_type_from_name (path_elt->elt.class_name);
4654 if (type != G_TYPE_INVALID)
4656 g_free (path_elt->elt.class_name);
4657 path_elt->elt.class_type = type;
4658 path_elt->type = PATH_ELT_TYPE;
4661 return g_str_equal (type_name, path_elt->elt.class_name);
4664 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4668 match_widget_class_recursive (GSList *list,
4671 gchar *path_reversed)
4675 /* break out if we cannot match anymore. */
4684 /* there are two possibilities:
4685 * 1. The next pattern should match the class.
4686 * 2. First normal matching, and then maybe a class */
4688 path_elt = list->data;
4690 if (path_elt->type != PATH_ELT_PSPEC)
4692 gchar *class_start = path;
4695 /* ignore leading dot */
4696 if (class_start[0] == '.')
4698 class_end = strchr (class_start, '.');
4700 if (class_end == NULL)
4702 if (!match_class (path_elt, class_start))
4705 return match_widget_class_recursive (list->next, 0, "", "");
4709 class_end[0] = '\0';
4710 if (!match_class (path_elt, class_start))
4718 gint new_length = length - (class_end - path);
4719 gchar old_char = path_reversed[new_length];
4723 path_reversed[new_length] = '\0';
4724 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4725 path_reversed[new_length] = old_char;
4736 gboolean result = FALSE;
4738 /* If there is nothing after this (ie. no class match),
4739 * just compare the pspec.
4741 if (list->next == NULL)
4742 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4744 class_elt = (PathElt *)list->next->data;
4745 g_assert (class_elt->type != PATH_ELT_PSPEC);
4748 if (class_start[0] == '.')
4753 class_end = strchr (class_start, '.');
4755 /* It should be cheaper to match the class first. (either the pattern
4756 * is simple, and will match most of the times, or it may be complex
4757 * and matching is slow)
4759 if (class_end == NULL)
4761 result = match_class (class_elt, class_start);
4765 class_end[0] = '\0';
4766 result = match_class (class_elt, class_start);
4775 /* terminate the string in front of the class. It does not matter
4776 * that the class becomes unusable, because it is not needed
4777 * inside the recursion
4779 old_char = class_start[0];
4780 class_start[0] = '\0';
4782 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4784 if (class_end != NULL)
4786 gint new_length = length - (class_end - path);
4787 gchar path_reversed_char = path_reversed[new_length];
4789 path_reversed[new_length] = '\0';
4791 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4793 path_reversed[new_length] = path_reversed_char;
4796 result = match_widget_class_recursive (list->next->next, 0, "", "");
4799 class_start[0] = old_char;
4805 /* get next class in path, or break out */
4806 if (class_end != NULL)
4807 class_start = class_end + 1;
4815 _gtk_rc_match_widget_class (GSList *list,
4818 gchar *path_reversed)
4820 return match_widget_class_recursive (list, length, path, path_reversed);
4823 #if defined (G_OS_WIN32) && !defined (_WIN64)
4825 /* DLL ABI stability backward compatibility versions */
4827 #undef gtk_rc_add_default_file
4830 gtk_rc_add_default_file (const gchar *filename)
4832 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4834 gtk_rc_add_default_file_utf8 (utf8_filename);
4836 g_free (utf8_filename);
4839 #undef gtk_rc_set_default_files
4842 gtk_rc_set_default_files (gchar **filenames)
4844 gchar **utf8_filenames;
4847 while (filenames[n++] != NULL)
4850 utf8_filenames = g_new (gchar *, n + 1);
4852 for (i = 0; i < n; i++)
4853 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4855 utf8_filenames[n] = NULL;
4857 gtk_rc_set_default_files_utf8 (utf8_filenames);
4859 g_strfreev (utf8_filenames);
4865 gtk_rc_parse (const gchar *filename)
4867 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4869 gtk_rc_parse_utf8 (utf8_filename);
4871 g_free (utf8_filename);