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: (transfer none): A %NULL-terminated array of filenames.
609 * This memory is owned by GTK+ and must not be freed by the application.
610 * If you want to store this information, you should make a copy.
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: (transfer full): 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
2065 * style, or %NULL if no matching against the widget path should be done
2066 * @class_path: (allow-none): the class path to use when looking up the style,
2067 * or %NULL if no matching against the class path should be done.
2068 * @type: a type that will be used along with parent types of this type
2069 * when matching against class styles, or #G_TYPE_NONE
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: (transfer none): A style created by matching with the
2087 * supplied paths, or %NULL if nothing matching was specified and the
2088 * default style should be used. The returned value is owned by GTK+
2089 * as part of an internal cache, so you must call g_object_ref() on
2090 * the returned value if you want to keep a reference to it.
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)
2224 /* if we are in scope 0, we know the symbol names
2225 * that are associated with certain token values.
2226 * so we look them up to make the error messages
2229 if (expected_token > GTK_RC_TOKEN_INVALID &&
2230 expected_token < GTK_RC_TOKEN_LAST)
2232 const gchar *sym = NULL;
2234 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2235 if (symbols[i].token == expected_token)
2236 sym = symbol_names + symbols[i].name_offset;
2239 msg = g_strconcat ("e.g. `", sym, "'", NULL);
2242 token = scanner->token;
2243 if (token > GTK_RC_TOKEN_INVALID &&
2244 token < GTK_RC_TOKEN_LAST)
2246 symbol_name = "???";
2247 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2248 if (symbols[i].token == scanner->token)
2249 symbol_name = symbol_names + symbols[i].name_offset;
2253 g_scanner_unexp_token (scanner,
2266 g_scanner_destroy (scanner);
2270 gtk_rc_styles_hash (const GSList *rc_styles)
2277 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2278 rc_styles = rc_styles->next;
2285 gtk_rc_styles_equal (const GSList *a,
2290 if (a->data != b->data)
2300 gtk_rc_style_hash (const gchar *name)
2306 result += (result << 3) + *name++;
2312 gtk_rc_style_equal (const gchar *a,
2315 return (strcmp (a, b) == 0);
2319 gtk_rc_style_find (GtkRcContext *context,
2322 if (context->rc_style_ht)
2323 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2329 gtk_rc_style_to_style (GtkRcContext *context,
2330 GtkRcStyle *rc_style)
2334 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2335 _gtk_style_init_for_settings (style, context->settings);
2337 style->rc_style = g_object_ref (rc_style);
2339 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2344 /* Reuses or frees rc_styles */
2346 gtk_rc_init_style (GtkRcContext *context,
2349 GtkStyle *style = NULL;
2352 g_return_val_if_fail (rc_styles != NULL, NULL);
2354 if (!realized_style_ht)
2355 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2356 (GEqualFunc) gtk_rc_styles_equal);
2358 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2362 GtkRcStyle *base_style = NULL;
2363 GtkRcStyle *proto_style;
2364 GtkRcStyleClass *proto_style_class;
2366 GType rc_style_type = GTK_TYPE_RC_STYLE;
2368 /* Find the first style where the RC file specified engine "" {}
2369 * or the first derived style and use that to create the
2370 * merged style. If we only have raw GtkRcStyles, use the first
2371 * style to create the merged style.
2373 base_style = rc_styles->data;
2374 tmp_styles = rc_styles;
2377 GtkRcStyle *rc_style = tmp_styles->data;
2379 if (rc_style->engine_specified ||
2380 G_OBJECT_TYPE (rc_style) != rc_style_type)
2382 base_style = rc_style;
2386 tmp_styles = tmp_styles->next;
2389 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2390 proto_style = proto_style_class->create_rc_style (base_style);
2392 tmp_styles = rc_styles;
2395 GtkRcStyle *rc_style = tmp_styles->data;
2397 proto_style_class->merge (proto_style, rc_style);
2399 /* Point from each rc_style to the list of styles */
2400 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2401 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2403 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2404 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2406 tmp_styles = tmp_styles->next;
2409 for (i = 0; i < 5; i++)
2410 if (proto_style->bg_pixmap_name[i] &&
2411 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2413 g_free (proto_style->bg_pixmap_name[i]);
2414 proto_style->bg_pixmap_name[i] = NULL;
2417 style = gtk_rc_style_to_style (context, proto_style);
2418 g_object_unref (proto_style);
2420 g_hash_table_insert (realized_style_ht, rc_styles, style);
2423 g_slist_free (rc_styles);
2428 /*********************
2429 * Parsing functions *
2430 *********************/
2433 lookup_color (GtkRcStyle *style,
2434 const char *color_name,
2437 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2440 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2442 GHashTable *hash = iter->data;
2443 GdkColor *match = g_hash_table_lookup (hash, color_name);
2447 color->red = match->red;
2448 color->green = match->green;
2449 color->blue = match->blue;
2458 rc_parse_token_or_compound (GScanner *scanner,
2461 GTokenType delimiter)
2463 guint token = g_scanner_get_next_token (scanner);
2465 /* we either scan a single token (skipping comments)
2466 * or a compund statement.
2467 * compunds are enclosed in (), [] or {} braces, we read
2468 * them in via deep recursion.
2475 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2478 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2480 case G_TOKEN_STRING:
2481 string = g_strescape (scanner->value.v_string, NULL);
2482 g_string_append (gstring, " \"");
2483 g_string_append (gstring, string);
2484 g_string_append_c (gstring, '"');
2487 case G_TOKEN_IDENTIFIER:
2488 g_string_append_c (gstring, ' ');
2489 g_string_append (gstring, scanner->value.v_identifier);
2491 case G_TOKEN_COMMENT_SINGLE:
2492 case G_TOKEN_COMMENT_MULTI:
2493 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2494 case G_TOKEN_LEFT_PAREN:
2495 g_string_append_c (gstring, ' ');
2496 g_string_append_c (gstring, token);
2497 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2498 if (token != G_TOKEN_NONE)
2501 case G_TOKEN_LEFT_CURLY:
2502 g_string_append_c (gstring, ' ');
2503 g_string_append_c (gstring, token);
2504 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2505 if (token != G_TOKEN_NONE)
2508 case G_TOKEN_LEFT_BRACE:
2509 g_string_append_c (gstring, ' ');
2510 g_string_append_c (gstring, token);
2511 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2512 if (token != G_TOKEN_NONE)
2516 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2519 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2520 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2521 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2523 g_scanner_get_next_token (scanner);
2525 if (!style || !lookup_color (style, scanner->value.v_identifier,
2528 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2529 scanner->value.v_identifier);
2530 return G_TOKEN_IDENTIFIER;
2534 g_string_append_printf (gstring, " { %s, %s, %s }",
2535 g_ascii_formatd (rbuf, sizeof (rbuf),
2537 color.red / 65535.0),
2538 g_ascii_formatd (gbuf, sizeof (gbuf),
2540 color.green / 65535.0),
2541 g_ascii_formatd (bbuf, sizeof (bbuf),
2543 color.blue / 65535.0));
2547 return G_TOKEN_IDENTIFIER;
2549 if (token >= 256 || token < 1)
2550 return delimiter ? delimiter : G_TOKEN_STRING;
2551 g_string_append_c (gstring, ' ');
2552 g_string_append_c (gstring, token);
2553 if (token == delimiter)
2554 return G_TOKEN_NONE;
2558 return G_TOKEN_NONE;
2560 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2564 gtk_rc_parse_assignment (GScanner *scanner,
2566 GtkRcProperty *prop)
2568 #define MY_SCAN_IDENTIFIER TRUE
2569 #define MY_SCAN_SYMBOLS FALSE
2570 #define MY_IDENTIFIER_2_STRING FALSE
2571 #define MY_CHAR_2_TOKEN TRUE
2572 #define MY_SCAN_IDENTIFIER_NULL FALSE
2573 #define MY_NUMBERS_2_INT TRUE
2575 gboolean scan_identifier = scanner->config->scan_identifier;
2576 gboolean scan_symbols = scanner->config->scan_symbols;
2577 gboolean identifier_2_string = scanner->config->identifier_2_string;
2578 gboolean char_2_token = scanner->config->char_2_token;
2579 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2580 gboolean numbers_2_int = scanner->config->numbers_2_int;
2581 gboolean negate = FALSE;
2582 gboolean is_color = FALSE;
2585 /* check that this is an assignment */
2586 if (g_scanner_get_next_token (scanner) != '=')
2589 /* adjust scanner mode */
2590 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2591 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2592 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2593 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2594 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2595 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2597 /* record location */
2598 if (g_getenv ("GTK_DEBUG"))
2599 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2601 prop->origin = NULL;
2603 /* parse optional symbolic color prefix */
2604 if (g_scanner_peek_next_token (scanner) == '@')
2606 g_scanner_get_next_token (scanner); /* eat color prefix */
2610 /* parse optional sign */
2611 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2613 g_scanner_get_next_token (scanner); /* eat sign */
2617 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2620 token = g_scanner_peek_next_token (scanner);
2622 if (is_color && token != G_TOKEN_IDENTIFIER)
2624 token = G_TOKEN_IDENTIFIER;
2631 g_scanner_get_next_token (scanner);
2632 g_value_init (&prop->value, G_TYPE_LONG);
2633 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2634 token = G_TOKEN_NONE;
2637 g_scanner_get_next_token (scanner);
2638 g_value_init (&prop->value, G_TYPE_DOUBLE);
2639 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2640 token = G_TOKEN_NONE;
2642 case G_TOKEN_STRING:
2643 g_scanner_get_next_token (scanner);
2645 token = G_TOKEN_INT;
2648 g_value_init (&prop->value, G_TYPE_STRING);
2649 g_value_set_string (&prop->value, scanner->value.v_string);
2650 token = G_TOKEN_NONE;
2653 case G_TOKEN_IDENTIFIER:
2657 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2658 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2659 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2662 g_scanner_get_next_token (scanner);
2664 if (!style || !lookup_color (style, scanner->value.v_identifier,
2667 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2668 scanner->value.v_identifier);
2669 token = G_TOKEN_IDENTIFIER;
2673 gstring = g_string_new (NULL);
2675 g_string_append_printf (gstring, " { %s, %s, %s }",
2676 g_ascii_formatd (rbuf, sizeof (rbuf),
2678 color.red / 65535.0),
2679 g_ascii_formatd (gbuf, sizeof (gbuf),
2681 color.green / 65535.0),
2682 g_ascii_formatd (bbuf, sizeof (bbuf),
2684 color.blue / 65535.0));
2686 g_value_init (&prop->value, G_TYPE_GSTRING);
2687 g_value_take_boxed (&prop->value, gstring);
2688 token = G_TOKEN_NONE;
2692 case G_TOKEN_LEFT_PAREN:
2693 case G_TOKEN_LEFT_CURLY:
2694 case G_TOKEN_LEFT_BRACE:
2697 GString *gstring = g_string_new (NULL);
2698 gboolean parse_on = TRUE;
2700 /* allow identifier(foobar) to support color expressions */
2701 if (token == G_TOKEN_IDENTIFIER)
2703 g_scanner_get_next_token (scanner);
2705 g_string_append_c (gstring, ' ');
2706 g_string_append (gstring, scanner->value.v_identifier);
2708 /* temporarily reset scanner mode to default, so we
2709 * don't peek the next token in a mode that only makes
2710 * sense in this function; because if anything but
2711 * G_TOKEN_LEFT_PAREN follows, the next token will be
2712 * parsed by our caller.
2714 * FIXME: right fix would be to call g_scanner_unget()
2715 * but that doesn't exist
2717 scanner->config->scan_identifier = scan_identifier;
2718 scanner->config->scan_symbols = scan_symbols;
2719 scanner->config->identifier_2_string = identifier_2_string;
2720 scanner->config->char_2_token = char_2_token;
2721 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2722 scanner->config->numbers_2_int = numbers_2_int;
2724 token = g_scanner_peek_next_token (scanner);
2726 /* restore adjusted scanner mode */
2727 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2728 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2729 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2730 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2731 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2732 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2734 if (token != G_TOKEN_LEFT_PAREN)
2736 token = G_TOKEN_NONE;
2742 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2744 if (token == G_TOKEN_NONE)
2746 g_string_append_c (gstring, ' ');
2747 g_value_init (&prop->value, G_TYPE_GSTRING);
2748 g_value_take_boxed (&prop->value, gstring);
2751 g_string_free (gstring, TRUE);
2756 g_scanner_get_next_token (scanner);
2757 token = G_TOKEN_INT;
2763 /* restore scanner mode */
2764 scanner->config->scan_identifier = scan_identifier;
2765 scanner->config->scan_symbols = scan_symbols;
2766 scanner->config->identifier_2_string = identifier_2_string;
2767 scanner->config->char_2_token = char_2_token;
2768 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2769 scanner->config->numbers_2_int = numbers_2_int;
2775 is_c_identifier (const gchar *string)
2778 gboolean is_varname;
2780 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2781 for (p = string + 1; *p && is_varname; p++)
2782 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2788 parse_include_file (GtkRcContext *context,
2790 const gchar *filename)
2792 char *to_parse = NULL;
2794 if (g_path_is_absolute (filename))
2796 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2797 * don't print an error in this case.
2799 to_parse = g_strdup (filename);
2803 /* if a relative path, we look relative to all the RC files in the
2804 * include stack. We require the file to be found in this case
2805 * so we can give meaningful error messages, and because on reparsing
2806 * non-absolute paths don't make sense.
2808 GSList *tmp_list = current_files_stack;
2811 GtkRcFile *curfile = tmp_list->data;
2812 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2814 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2822 tmp_list = tmp_list->next;
2828 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2833 g_scanner_warn (scanner,
2834 _("Unable to find include file: \"%s\""),
2841 gtk_rc_parse_statement (GtkRcContext *context,
2846 token = g_scanner_peek_next_token (scanner);
2849 case GTK_RC_TOKEN_INCLUDE:
2850 token = g_scanner_get_next_token (scanner);
2851 if (token != GTK_RC_TOKEN_INCLUDE)
2852 return GTK_RC_TOKEN_INCLUDE;
2853 token = g_scanner_get_next_token (scanner);
2854 if (token != G_TOKEN_STRING)
2855 return G_TOKEN_STRING;
2856 parse_include_file (context, scanner, scanner->value.v_string);
2857 return G_TOKEN_NONE;
2859 case GTK_RC_TOKEN_STYLE:
2860 return gtk_rc_parse_style (context, scanner);
2862 case GTK_RC_TOKEN_BINDING:
2863 return _gtk_binding_parse_binding (scanner);
2865 case GTK_RC_TOKEN_PIXMAP_PATH:
2866 return gtk_rc_parse_pixmap_path (context, scanner);
2868 case GTK_RC_TOKEN_WIDGET:
2869 return gtk_rc_parse_path_pattern (context, scanner);
2871 case GTK_RC_TOKEN_WIDGET_CLASS:
2872 return gtk_rc_parse_path_pattern (context, scanner);
2874 case GTK_RC_TOKEN_CLASS:
2875 return gtk_rc_parse_path_pattern (context, scanner);
2877 case GTK_RC_TOKEN_MODULE_PATH:
2878 return gtk_rc_parse_module_path (scanner);
2880 case GTK_RC_TOKEN_IM_MODULE_FILE:
2881 return gtk_rc_parse_im_module_file (scanner);
2883 case G_TOKEN_IDENTIFIER:
2884 if (is_c_identifier (scanner->next_value.v_identifier))
2886 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2889 g_scanner_get_next_token (scanner); /* eat identifier */
2890 name = g_strdup (scanner->value.v_identifier);
2892 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2893 if (token == G_TOKEN_NONE)
2895 GtkSettingsValue svalue;
2897 svalue.origin = prop.origin;
2898 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2899 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2900 _gtk_settings_set_property_value_from_rc (context->settings,
2904 g_free (prop.origin);
2905 if (G_VALUE_TYPE (&prop.value))
2906 g_value_unset (&prop.value);
2913 g_scanner_get_next_token (scanner);
2914 return G_TOKEN_IDENTIFIER;
2917 g_scanner_get_next_token (scanner);
2918 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2923 fixup_rc_set (GSList *list,
2929 GtkRcSet *set = list->data;
2930 if (set->rc_style == orig)
2931 set->rc_style = new;
2937 fixup_rc_sets (GtkRcContext *context,
2941 fixup_rc_set (context->rc_sets_widget, orig, new);
2942 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2943 fixup_rc_set (context->rc_sets_class, orig, new);
2947 gtk_rc_parse_style (GtkRcContext *context,
2950 GtkRcStyle *rc_style;
2951 GtkRcStyle *orig_style;
2952 GtkRcStyle *parent_style = NULL;
2953 GtkRcStylePrivate *rc_priv = NULL;
2956 GtkIconFactory *our_factory = NULL;
2957 GHashTable *our_hash = NULL;
2959 token = g_scanner_get_next_token (scanner);
2960 if (token != GTK_RC_TOKEN_STYLE)
2961 return GTK_RC_TOKEN_STYLE;
2963 token = g_scanner_get_next_token (scanner);
2964 if (token != G_TOKEN_STRING)
2965 return G_TOKEN_STRING;
2967 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2969 orig_style = g_object_ref (rc_style);
2975 rc_style = gtk_rc_style_new ();
2976 rc_style->name = g_strdup (scanner->value.v_string);
2978 for (i = 0; i < 5; i++)
2979 rc_style->bg_pixmap_name[i] = NULL;
2981 for (i = 0; i < 5; i++)
2982 rc_style->color_flags[i] = 0;
2985 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
2987 /* If there's a list, its first member is always the factory belonging
2990 if (rc_style->icon_factories)
2991 our_factory = rc_style->icon_factories->data;
2992 if (rc_priv->color_hashes)
2993 our_hash = rc_priv->color_hashes->data;
2995 token = g_scanner_peek_next_token (scanner);
2996 if (token == G_TOKEN_EQUAL_SIGN)
2998 token = g_scanner_get_next_token (scanner);
3000 token = g_scanner_get_next_token (scanner);
3001 if (token != G_TOKEN_STRING)
3003 token = G_TOKEN_STRING;
3007 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3010 for (i = 0; i < 5; i++)
3012 rc_style->color_flags[i] = parent_style->color_flags[i];
3013 rc_style->fg[i] = parent_style->fg[i];
3014 rc_style->bg[i] = parent_style->bg[i];
3015 rc_style->text[i] = parent_style->text[i];
3016 rc_style->base[i] = parent_style->base[i];
3019 rc_style->xthickness = parent_style->xthickness;
3020 rc_style->ythickness = parent_style->ythickness;
3022 if (parent_style->font_desc)
3024 if (rc_style->font_desc)
3025 pango_font_description_free (rc_style->font_desc);
3026 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3029 if (parent_style->rc_properties)
3033 for (i = 0; i < parent_style->rc_properties->len; i++)
3034 insert_rc_property (rc_style,
3035 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3039 for (i = 0; i < 5; i++)
3041 g_free (rc_style->bg_pixmap_name[i]);
3042 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3047 /* get icon_factories and color_hashes from the parent style;
3048 * if the parent_style doesn't have color_hashes, initializes
3049 * the color_hashes with the settings' color scheme (if it exists)
3051 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3053 if (rc_style->icon_factories)
3054 our_factory = rc_style->icon_factories->data;
3055 if (rc_priv->color_hashes)
3056 our_hash = rc_priv->color_hashes->data;
3058 token = g_scanner_get_next_token (scanner);
3059 if (token != G_TOKEN_LEFT_CURLY)
3061 token = G_TOKEN_LEFT_CURLY;
3065 token = g_scanner_peek_next_token (scanner);
3066 while (token != G_TOKEN_RIGHT_CURLY)
3070 case GTK_RC_TOKEN_BG:
3071 token = gtk_rc_parse_bg (scanner, rc_style);
3073 case GTK_RC_TOKEN_FG:
3074 token = gtk_rc_parse_fg (scanner, rc_style);
3076 case GTK_RC_TOKEN_TEXT:
3077 token = gtk_rc_parse_text (scanner, rc_style);
3079 case GTK_RC_TOKEN_BASE:
3080 token = gtk_rc_parse_base (scanner, rc_style);
3082 case GTK_RC_TOKEN_XTHICKNESS:
3083 token = gtk_rc_parse_xthickness (scanner, rc_style);
3085 case GTK_RC_TOKEN_YTHICKNESS:
3086 token = gtk_rc_parse_ythickness (scanner, rc_style);
3088 case GTK_RC_TOKEN_BG_PIXMAP:
3089 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3091 case GTK_RC_TOKEN_FONT:
3092 token = gtk_rc_parse_font (scanner, rc_style);
3094 case GTK_RC_TOKEN_FONTSET:
3095 token = gtk_rc_parse_fontset (scanner, rc_style);
3097 case GTK_RC_TOKEN_FONT_NAME:
3098 token = gtk_rc_parse_font_name (scanner, rc_style);
3100 case GTK_RC_TOKEN_ENGINE:
3101 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3103 case GTK_RC_TOKEN_STOCK:
3104 if (our_factory == NULL)
3105 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3106 our_factory = rc_style->icon_factories->data;
3107 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3109 case GTK_RC_TOKEN_COLOR:
3110 if (our_hash == NULL)
3112 gtk_rc_style_prepend_empty_color_hash (rc_style);
3113 our_hash = rc_priv->color_hashes->data;
3115 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3117 case G_TOKEN_IDENTIFIER:
3118 if (is_c_identifier (scanner->next_value.v_identifier))
3120 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3123 g_scanner_get_next_token (scanner); /* eat type name */
3124 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3125 if (g_scanner_get_next_token (scanner) != ':' ||
3126 g_scanner_get_next_token (scanner) != ':')
3131 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3132 !is_c_identifier (scanner->value.v_identifier))
3134 token = G_TOKEN_IDENTIFIER;
3138 /* it's important that we do the same canonification as GParamSpecPool here */
3139 name = g_strdup (scanner->value.v_identifier);
3140 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3141 prop.property_name = g_quark_from_string (name);
3144 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3145 if (token == G_TOKEN_NONE)
3147 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3148 insert_rc_property (rc_style, &prop, TRUE);
3151 g_free (prop.origin);
3152 if (G_VALUE_TYPE (&prop.value))
3153 g_value_unset (&prop.value);
3157 g_scanner_get_next_token (scanner);
3158 token = G_TOKEN_IDENTIFIER;
3162 g_scanner_get_next_token (scanner);
3163 token = G_TOKEN_RIGHT_CURLY;
3167 if (token != G_TOKEN_NONE)
3170 token = g_scanner_peek_next_token (scanner);
3171 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3173 token = g_scanner_get_next_token (scanner);
3174 if (token != G_TOKEN_RIGHT_CURLY)
3176 token = G_TOKEN_RIGHT_CURLY;
3180 if (rc_style != orig_style)
3182 if (!context->rc_style_ht)
3183 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3184 (GEqualFunc) gtk_rc_style_equal);
3186 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3188 /* If we copied the data into a new rc style, fix up references to the old rc style
3189 * in bindings that we have.
3192 fixup_rc_sets (context, orig_style, rc_style);
3196 g_object_unref (orig_style);
3198 return G_TOKEN_NONE;
3201 if (rc_style != orig_style)
3202 g_object_unref (rc_style);
3205 g_object_unref (orig_style);
3210 const GtkRcProperty*
3211 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3213 GQuark property_name)
3215 GtkRcProperty *node = NULL;
3217 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3219 if (rc_style->rc_properties)
3223 key.type_name = type_name;
3224 key.property_name = property_name;
3226 node = bsearch (&key,
3227 rc_style->rc_properties->data, rc_style->rc_properties->len,
3228 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3235 gtk_rc_parse_bg (GScanner *scanner,
3241 token = g_scanner_get_next_token (scanner);
3242 if (token != GTK_RC_TOKEN_BG)
3243 return GTK_RC_TOKEN_BG;
3245 token = gtk_rc_parse_state (scanner, &state);
3246 if (token != G_TOKEN_NONE)
3249 token = g_scanner_get_next_token (scanner);
3250 if (token != G_TOKEN_EQUAL_SIGN)
3251 return G_TOKEN_EQUAL_SIGN;
3253 style->color_flags[state] |= GTK_RC_BG;
3254 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3258 gtk_rc_parse_fg (GScanner *scanner,
3264 token = g_scanner_get_next_token (scanner);
3265 if (token != GTK_RC_TOKEN_FG)
3266 return GTK_RC_TOKEN_FG;
3268 token = gtk_rc_parse_state (scanner, &state);
3269 if (token != G_TOKEN_NONE)
3272 token = g_scanner_get_next_token (scanner);
3273 if (token != G_TOKEN_EQUAL_SIGN)
3274 return G_TOKEN_EQUAL_SIGN;
3276 style->color_flags[state] |= GTK_RC_FG;
3277 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3281 gtk_rc_parse_text (GScanner *scanner,
3287 token = g_scanner_get_next_token (scanner);
3288 if (token != GTK_RC_TOKEN_TEXT)
3289 return GTK_RC_TOKEN_TEXT;
3291 token = gtk_rc_parse_state (scanner, &state);
3292 if (token != G_TOKEN_NONE)
3295 token = g_scanner_get_next_token (scanner);
3296 if (token != G_TOKEN_EQUAL_SIGN)
3297 return G_TOKEN_EQUAL_SIGN;
3299 style->color_flags[state] |= GTK_RC_TEXT;
3300 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3304 gtk_rc_parse_base (GScanner *scanner,
3310 token = g_scanner_get_next_token (scanner);
3311 if (token != GTK_RC_TOKEN_BASE)
3312 return GTK_RC_TOKEN_BASE;
3314 token = gtk_rc_parse_state (scanner, &state);
3315 if (token != G_TOKEN_NONE)
3318 token = g_scanner_get_next_token (scanner);
3319 if (token != G_TOKEN_EQUAL_SIGN)
3320 return G_TOKEN_EQUAL_SIGN;
3322 style->color_flags[state] |= GTK_RC_BASE;
3323 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3327 gtk_rc_parse_xthickness (GScanner *scanner,
3332 token = g_scanner_get_next_token (scanner);
3333 if (token != GTK_RC_TOKEN_XTHICKNESS)
3334 return GTK_RC_TOKEN_XTHICKNESS;
3336 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3337 return G_TOKEN_EQUAL_SIGN;
3339 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3342 style->xthickness = scanner->value.v_int;
3344 return G_TOKEN_NONE;
3348 gtk_rc_parse_ythickness (GScanner *scanner,
3353 token = g_scanner_get_next_token (scanner);
3354 if (token != GTK_RC_TOKEN_YTHICKNESS)
3355 return GTK_RC_TOKEN_YTHICKNESS;
3357 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3358 return G_TOKEN_EQUAL_SIGN;
3360 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3363 style->ythickness = scanner->value.v_int;
3365 return G_TOKEN_NONE;
3369 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3371 GtkRcStyle *rc_style)
3377 token = g_scanner_get_next_token (scanner);
3378 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3379 return GTK_RC_TOKEN_BG_PIXMAP;
3381 token = gtk_rc_parse_state (scanner, &state);
3382 if (token != G_TOKEN_NONE)
3385 token = g_scanner_get_next_token (scanner);
3386 if (token != G_TOKEN_EQUAL_SIGN)
3387 return G_TOKEN_EQUAL_SIGN;
3389 token = g_scanner_get_next_token (scanner);
3390 if (token != G_TOKEN_STRING)
3391 return G_TOKEN_STRING;
3393 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3394 (strcmp (scanner->value.v_string, "<none>") == 0))
3395 pixmap_file = g_strdup (scanner->value.v_string);
3397 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3398 scanner, scanner->value.v_string);
3402 g_free (rc_style->bg_pixmap_name[state]);
3403 rc_style->bg_pixmap_name[state] = pixmap_file;
3406 return G_TOKEN_NONE;
3410 gtk_rc_check_pixmap_dir (const gchar *dir,
3411 const gchar *pixmap_file)
3415 buf = g_build_filename (dir, pixmap_file, NULL);
3417 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3426 * gtk_rc_find_pixmap_in_path:
3427 * @settings: a #GtkSettings
3428 * @scanner: Scanner used to get line number information for the
3429 * warning message, or %NULL
3430 * @pixmap_file: name of the pixmap file to locate.
3432 * Looks up a file in pixmap path for the specified #GtkSettings.
3433 * If the file is not found, it outputs a warning message using
3434 * g_warning() and returns %NULL.
3436 * Return value: the filename.
3439 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3441 const gchar *pixmap_file)
3447 GtkRcContext *context = gtk_rc_context_get (settings);
3449 if (context->pixmap_path)
3450 for (i = 0; context->pixmap_path[i] != NULL; i++)
3452 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3457 tmp_list = current_files_stack;
3460 GtkRcFile *curfile = tmp_list->data;
3461 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3465 tmp_list = tmp_list->next;
3469 g_scanner_warn (scanner,
3470 _("Unable to locate image file in pixmap_path: \"%s\""),
3473 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3480 * gtk_rc_find_module_in_path:
3481 * @module_file: name of a theme engine
3483 * Searches for a theme engine in the GTK+ search path. This function
3484 * is not useful for applications and should not be used.
3486 * Return value: The filename, if found (must be freed with g_free()),
3490 gtk_rc_find_module_in_path (const gchar *module_file)
3492 return _gtk_find_module (module_file, "engines");
3496 gtk_rc_parse_font (GScanner *scanner,
3497 GtkRcStyle *rc_style)
3501 token = g_scanner_get_next_token (scanner);
3502 if (token != GTK_RC_TOKEN_FONT)
3503 return GTK_RC_TOKEN_FONT;
3505 token = g_scanner_get_next_token (scanner);
3506 if (token != G_TOKEN_EQUAL_SIGN)
3507 return G_TOKEN_EQUAL_SIGN;
3509 token = g_scanner_get_next_token (scanner);
3510 if (token != G_TOKEN_STRING)
3511 return G_TOKEN_STRING;
3513 /* Ignore, do nothing */
3515 return G_TOKEN_NONE;
3519 gtk_rc_parse_fontset (GScanner *scanner,
3520 GtkRcStyle *rc_style)
3524 token = g_scanner_get_next_token (scanner);
3525 if (token != GTK_RC_TOKEN_FONTSET)
3526 return GTK_RC_TOKEN_FONTSET;
3528 token = g_scanner_get_next_token (scanner);
3529 if (token != G_TOKEN_EQUAL_SIGN)
3530 return G_TOKEN_EQUAL_SIGN;
3532 token = g_scanner_get_next_token (scanner);
3533 if (token != G_TOKEN_STRING)
3534 return G_TOKEN_STRING;
3536 /* Do nothing - silently ignore */
3538 return G_TOKEN_NONE;
3542 gtk_rc_parse_font_name (GScanner *scanner,
3543 GtkRcStyle *rc_style)
3547 token = g_scanner_get_next_token (scanner);
3548 if (token != GTK_RC_TOKEN_FONT_NAME)
3549 return GTK_RC_TOKEN_FONT;
3551 token = g_scanner_get_next_token (scanner);
3552 if (token != G_TOKEN_EQUAL_SIGN)
3553 return G_TOKEN_EQUAL_SIGN;
3555 token = g_scanner_get_next_token (scanner);
3556 if (token != G_TOKEN_STRING)
3557 return G_TOKEN_STRING;
3559 if (rc_style->font_desc)
3560 pango_font_description_free (rc_style->font_desc);
3562 rc_style->font_desc =
3563 pango_font_description_from_string (scanner->value.v_string);
3565 return G_TOKEN_NONE;
3569 gtk_rc_parse_engine (GtkRcContext *context,
3571 GtkRcStyle **rc_style)
3574 GtkThemeEngine *engine;
3575 guint result = G_TOKEN_NONE;
3576 GtkRcStyle *new_style = NULL;
3577 gboolean parsed_curlies = FALSE;
3578 GtkRcStylePrivate *rc_priv, *new_priv;
3580 token = g_scanner_get_next_token (scanner);
3581 if (token != GTK_RC_TOKEN_ENGINE)
3582 return GTK_RC_TOKEN_ENGINE;
3584 token = g_scanner_get_next_token (scanner);
3585 if (token != G_TOKEN_STRING)
3586 return G_TOKEN_STRING;
3588 if (!scanner->value.v_string[0])
3590 /* Support engine "" {} to mean override to the default engine
3592 token = g_scanner_get_next_token (scanner);
3593 if (token != G_TOKEN_LEFT_CURLY)
3594 return G_TOKEN_LEFT_CURLY;
3596 token = g_scanner_get_next_token (scanner);
3597 if (token != G_TOKEN_RIGHT_CURLY)
3598 return G_TOKEN_RIGHT_CURLY;
3600 parsed_curlies = TRUE;
3602 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3604 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3606 new_style = gtk_rc_style_new ();
3607 gtk_rc_style_real_merge (new_style, *rc_style);
3609 new_style->name = g_strdup ((*rc_style)->name);
3611 /* take over icon factories and color hashes
3612 * from the to-be-deleted style
3614 new_style->icon_factories = (*rc_style)->icon_factories;
3615 (*rc_style)->icon_factories = NULL;
3616 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3617 new_priv->color_hashes = rc_priv->color_hashes;
3618 rc_priv->color_hashes = NULL;
3621 (*rc_style)->engine_specified = TRUE;
3625 engine = gtk_theme_engine_get (scanner->value.v_string);
3627 token = g_scanner_get_next_token (scanner);
3628 if (token != G_TOKEN_LEFT_CURLY)
3629 return G_TOKEN_LEFT_CURLY;
3633 GtkRcStyleClass *new_class;
3635 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3636 new_style = gtk_theme_engine_create_rc_style (engine);
3637 g_type_module_unuse (G_TYPE_MODULE (engine));
3639 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3641 new_class->merge (new_style, *rc_style);
3643 new_style->name = g_strdup ((*rc_style)->name);
3645 /* take over icon factories and color hashes
3646 * from the to-be-deleted style
3648 new_style->icon_factories = (*rc_style)->icon_factories;
3649 (*rc_style)->icon_factories = NULL;
3650 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3651 new_priv->color_hashes = rc_priv->color_hashes;
3652 rc_priv->color_hashes = NULL;
3654 if (new_class->parse)
3656 parsed_curlies = TRUE;
3657 result = new_class->parse (new_style, context->settings, scanner);
3659 if (result != G_TOKEN_NONE)
3661 /* copy icon factories and color hashes back
3663 (*rc_style)->icon_factories = new_style->icon_factories;
3664 new_style->icon_factories = NULL;
3665 rc_priv->color_hashes = new_priv->color_hashes;
3666 new_priv->color_hashes = NULL;
3668 g_object_unref (new_style);
3675 if (!parsed_curlies)
3677 /* Skip over remainder, looking for nested {}'s
3681 result = G_TOKEN_RIGHT_CURLY;
3682 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3684 if (token == G_TOKEN_LEFT_CURLY)
3686 else if (token == G_TOKEN_RIGHT_CURLY)
3691 result = G_TOKEN_NONE;
3699 new_style->engine_specified = TRUE;
3701 g_object_unref (*rc_style);
3702 *rc_style = new_style;
3709 gtk_rc_parse_state (GScanner *scanner,
3710 GtkStateType *state)
3715 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3716 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3718 /* we don't know where we got called from, so we reset the scope here.
3719 * if we bail out due to errors, we *don't* reset the scope, so the
3720 * error messaging code can make sense of our tokens.
3722 old_scope = g_scanner_set_scope (scanner, 0);
3724 token = g_scanner_get_next_token (scanner);
3725 if (token != G_TOKEN_LEFT_BRACE)
3726 return G_TOKEN_LEFT_BRACE;
3728 token = g_scanner_get_next_token (scanner);
3731 case GTK_RC_TOKEN_ACTIVE:
3732 *state = GTK_STATE_ACTIVE;
3734 case GTK_RC_TOKEN_INSENSITIVE:
3735 *state = GTK_STATE_INSENSITIVE;
3737 case GTK_RC_TOKEN_NORMAL:
3738 *state = GTK_STATE_NORMAL;
3740 case GTK_RC_TOKEN_PRELIGHT:
3741 *state = GTK_STATE_PRELIGHT;
3743 case GTK_RC_TOKEN_SELECTED:
3744 *state = GTK_STATE_SELECTED;
3747 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3750 token = g_scanner_get_next_token (scanner);
3751 if (token != G_TOKEN_RIGHT_BRACE)
3752 return G_TOKEN_RIGHT_BRACE;
3754 g_scanner_set_scope (scanner, old_scope);
3756 return G_TOKEN_NONE;
3760 gtk_rc_parse_priority (GScanner *scanner,
3761 GtkPathPriorityType *priority)
3766 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3767 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3769 /* we don't know where we got called from, so we reset the scope here.
3770 * if we bail out due to errors, we *don't* reset the scope, so the
3771 * error messaging code can make sense of our tokens.
3773 old_scope = g_scanner_set_scope (scanner, 0);
3775 token = g_scanner_get_next_token (scanner);
3779 token = g_scanner_get_next_token (scanner);
3782 case GTK_RC_TOKEN_LOWEST:
3783 *priority = GTK_PATH_PRIO_LOWEST;
3785 case GTK_RC_TOKEN_GTK:
3786 *priority = GTK_PATH_PRIO_GTK;
3788 case GTK_RC_TOKEN_APPLICATION:
3789 *priority = GTK_PATH_PRIO_APPLICATION;
3791 case GTK_RC_TOKEN_THEME:
3792 *priority = GTK_PATH_PRIO_THEME;
3794 case GTK_RC_TOKEN_RC:
3795 *priority = GTK_PATH_PRIO_RC;
3797 case GTK_RC_TOKEN_HIGHEST:
3798 *priority = GTK_PATH_PRIO_HIGHEST;
3801 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3804 g_scanner_set_scope (scanner, old_scope);
3806 return G_TOKEN_NONE;
3810 * gtk_rc_parse_color:
3811 * @scanner: a #GScanner
3812 * @color: a pointer to a #GdkColor structure in which to store the result
3814 * Parses a color in the <link linkend="color=format">format</link> expected
3817 * Note that theme engines should use gtk_rc_parse_color_full() in
3818 * order to support symbolic colors.
3820 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3821 * that was expected but not found
3824 gtk_rc_parse_color (GScanner *scanner,
3827 return gtk_rc_parse_color_full (scanner, NULL, color);
3831 * gtk_rc_parse_color_full:
3832 * @scanner: a #GScanner
3833 * @style: (allow-none): a #GtkRcStyle, or %NULL
3834 * @color: a pointer to a #GdkColor structure in which to store the result
3836 * Parses a color in the <link linkend="color=format">format</link> expected
3837 * in a RC file. If @style is not %NULL, it will be consulted to resolve
3838 * references to symbolic colors.
3840 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3841 * that was expected but not found
3846 gtk_rc_parse_color_full (GScanner *scanner,
3852 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3854 /* we don't need to set our own scope here, because
3855 * we don't need own symbols
3858 token = g_scanner_get_next_token (scanner);
3866 case G_TOKEN_LEFT_CURLY:
3867 token = g_scanner_get_next_token (scanner);
3868 if (token == G_TOKEN_INT)
3869 token_int = scanner->value.v_int;
3870 else if (token == G_TOKEN_FLOAT)
3871 token_int = scanner->value.v_float * 65535.0;
3873 return G_TOKEN_FLOAT;
3874 color->red = CLAMP (token_int, 0, 65535);
3876 token = g_scanner_get_next_token (scanner);
3877 if (token != G_TOKEN_COMMA)
3878 return G_TOKEN_COMMA;
3880 token = g_scanner_get_next_token (scanner);
3881 if (token == G_TOKEN_INT)
3882 token_int = scanner->value.v_int;
3883 else if (token == G_TOKEN_FLOAT)
3884 token_int = scanner->value.v_float * 65535.0;
3886 return G_TOKEN_FLOAT;
3887 color->green = CLAMP (token_int, 0, 65535);
3889 token = g_scanner_get_next_token (scanner);
3890 if (token != G_TOKEN_COMMA)
3891 return G_TOKEN_COMMA;
3893 token = g_scanner_get_next_token (scanner);
3894 if (token == G_TOKEN_INT)
3895 token_int = scanner->value.v_int;
3896 else if (token == G_TOKEN_FLOAT)
3897 token_int = scanner->value.v_float * 65535.0;
3899 return G_TOKEN_FLOAT;
3900 color->blue = CLAMP (token_int, 0, 65535);
3902 token = g_scanner_get_next_token (scanner);
3903 if (token != G_TOKEN_RIGHT_CURLY)
3904 return G_TOKEN_RIGHT_CURLY;
3905 return G_TOKEN_NONE;
3907 case G_TOKEN_STRING:
3908 if (!gdk_color_parse (scanner->value.v_string, color))
3910 g_scanner_warn (scanner, "Invalid color constant '%s'",
3911 scanner->value.v_string);
3912 return G_TOKEN_STRING;
3914 return G_TOKEN_NONE;
3917 token = g_scanner_get_next_token (scanner);
3918 if (token != G_TOKEN_IDENTIFIER)
3919 return G_TOKEN_IDENTIFIER;
3921 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3923 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3924 scanner->value.v_identifier);
3925 return G_TOKEN_IDENTIFIER;
3928 return G_TOKEN_NONE;
3930 case G_TOKEN_IDENTIFIER:
3931 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3933 token = g_scanner_get_next_token (scanner);
3934 if (token != G_TOKEN_LEFT_PAREN)
3935 return G_TOKEN_LEFT_PAREN;
3938 if (g_scanner_peek_next_token (scanner) == '-')
3940 g_scanner_get_next_token (scanner); /* eat sign */
3944 token = g_scanner_get_next_token (scanner);
3945 if (token != G_TOKEN_FLOAT)
3946 return G_TOKEN_FLOAT;
3948 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3950 token = g_scanner_get_next_token (scanner);
3951 if (token != G_TOKEN_COMMA)
3952 return G_TOKEN_COMMA;
3954 token = gtk_rc_parse_color_full (scanner, style, &c1);
3955 if (token != G_TOKEN_NONE)
3958 token = g_scanner_get_next_token (scanner);
3959 if (token != G_TOKEN_COMMA)
3960 return G_TOKEN_COMMA;
3962 token = gtk_rc_parse_color_full (scanner, style, &c2);
3963 if (token != G_TOKEN_NONE)
3966 token = g_scanner_get_next_token (scanner);
3967 if (token != G_TOKEN_RIGHT_PAREN)
3968 return G_TOKEN_RIGHT_PAREN;
3970 color->red = l * c1.red + (1.0 - l) * c2.red;
3971 color->green = l * c1.green + (1.0 - l) * c2.green;
3972 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3974 return G_TOKEN_NONE;
3976 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
3978 token = g_scanner_get_next_token (scanner);
3979 if (token != G_TOKEN_LEFT_PAREN)
3980 return G_TOKEN_LEFT_PAREN;
3983 if (g_scanner_peek_next_token (scanner) == '-')
3985 g_scanner_get_next_token (scanner); /* eat sign */
3989 token = g_scanner_get_next_token (scanner);
3990 if (token != G_TOKEN_FLOAT)
3991 return G_TOKEN_FLOAT;
3993 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3995 token = g_scanner_get_next_token (scanner);
3996 if (token != G_TOKEN_COMMA)
3997 return G_TOKEN_COMMA;
3999 token = gtk_rc_parse_color_full (scanner, style, &c1);
4000 if (token != G_TOKEN_NONE)
4003 token = g_scanner_get_next_token (scanner);
4004 if (token != G_TOKEN_RIGHT_PAREN)
4005 return G_TOKEN_RIGHT_PAREN;
4007 _gtk_style_shade (&c1, color, l);
4009 return G_TOKEN_NONE;
4011 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4012 strcmp (scanner->value.v_identifier, "darker") == 0)
4014 if (scanner->value.v_identifier[0] == 'l')
4019 token = g_scanner_get_next_token (scanner);
4020 if (token != G_TOKEN_LEFT_PAREN)
4021 return G_TOKEN_LEFT_PAREN;
4023 token = gtk_rc_parse_color_full (scanner, style, &c1);
4024 if (token != G_TOKEN_NONE)
4027 token = g_scanner_get_next_token (scanner);
4028 if (token != G_TOKEN_RIGHT_PAREN)
4029 return G_TOKEN_RIGHT_PAREN;
4031 _gtk_style_shade (&c1, color, l);
4033 return G_TOKEN_NONE;
4036 return G_TOKEN_IDENTIFIER;
4039 return G_TOKEN_STRING;
4044 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4049 token = g_scanner_get_next_token (scanner);
4050 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4051 return GTK_RC_TOKEN_PIXMAP_PATH;
4053 token = g_scanner_get_next_token (scanner);
4054 if (token != G_TOKEN_STRING)
4055 return G_TOKEN_STRING;
4057 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4059 return G_TOKEN_NONE;
4063 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4065 const gchar *pix_path)
4067 g_strfreev (context->pixmap_path);
4068 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4072 gtk_rc_parse_module_path (GScanner *scanner)
4076 token = g_scanner_get_next_token (scanner);
4077 if (token != GTK_RC_TOKEN_MODULE_PATH)
4078 return GTK_RC_TOKEN_MODULE_PATH;
4080 token = g_scanner_get_next_token (scanner);
4081 if (token != G_TOKEN_STRING)
4082 return G_TOKEN_STRING;
4084 g_warning ("module_path directive is now ignored\n");
4086 return G_TOKEN_NONE;
4090 gtk_rc_parse_im_module_file (GScanner *scanner)
4094 token = g_scanner_get_next_token (scanner);
4095 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4096 return GTK_RC_TOKEN_IM_MODULE_FILE;
4098 token = g_scanner_get_next_token (scanner);
4099 if (token != G_TOKEN_STRING)
4100 return G_TOKEN_STRING;
4102 g_free (im_module_file);
4104 im_module_file = g_strdup (scanner->value.v_string);
4106 return G_TOKEN_NONE;
4110 gtk_rc_parse_path_pattern (GtkRcContext *context,
4114 GtkPathType path_type;
4116 gboolean is_binding;
4117 GtkPathPriorityType priority = context->default_priority;
4119 token = g_scanner_get_next_token (scanner);
4122 case GTK_RC_TOKEN_WIDGET:
4123 path_type = GTK_PATH_WIDGET;
4125 case GTK_RC_TOKEN_WIDGET_CLASS:
4126 path_type = GTK_PATH_WIDGET_CLASS;
4128 case GTK_RC_TOKEN_CLASS:
4129 path_type = GTK_PATH_CLASS;
4132 return GTK_RC_TOKEN_WIDGET_CLASS;
4135 token = g_scanner_get_next_token (scanner);
4136 if (token != G_TOKEN_STRING)
4137 return G_TOKEN_STRING;
4139 pattern = g_strdup (scanner->value.v_string);
4141 token = g_scanner_get_next_token (scanner);
4142 if (token == GTK_RC_TOKEN_STYLE)
4144 else if (token == GTK_RC_TOKEN_BINDING)
4149 return GTK_RC_TOKEN_STYLE;
4152 if (g_scanner_peek_next_token (scanner) == ':')
4154 token = gtk_rc_parse_priority (scanner, &priority);
4155 if (token != G_TOKEN_NONE)
4162 token = g_scanner_get_next_token (scanner);
4163 if (token != G_TOKEN_STRING)
4166 return G_TOKEN_STRING;
4171 GtkBindingSet *binding;
4173 binding = gtk_binding_set_find (scanner->value.v_string);
4177 return G_TOKEN_STRING;
4179 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4183 GtkRcStyle *rc_style;
4186 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4191 return G_TOKEN_STRING;
4194 rc_set = g_new (GtkRcSet, 1);
4195 rc_set->type = path_type;
4197 if (path_type == GTK_PATH_WIDGET_CLASS)
4199 rc_set->pspec = NULL;
4200 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4204 rc_set->pspec = g_pattern_spec_new (pattern);
4205 rc_set->path = NULL;
4208 rc_set->rc_style = rc_style;
4209 rc_set->priority = priority;
4211 if (path_type == GTK_PATH_WIDGET)
4212 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4213 else if (path_type == GTK_PATH_WIDGET_CLASS)
4214 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4216 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4220 return G_TOKEN_NONE;
4224 gtk_rc_parse_hash_key (GScanner *scanner,
4229 token = g_scanner_get_next_token (scanner);
4230 if (token != G_TOKEN_LEFT_BRACE)
4231 return G_TOKEN_LEFT_BRACE;
4233 token = g_scanner_get_next_token (scanner);
4235 if (token != G_TOKEN_STRING)
4236 return G_TOKEN_STRING;
4238 *hash_key = g_strdup (scanner->value.v_string);
4240 token = g_scanner_get_next_token (scanner);
4241 if (token != G_TOKEN_RIGHT_BRACE)
4244 return G_TOKEN_RIGHT_BRACE;
4247 return G_TOKEN_NONE;
4251 gtk_rc_parse_icon_source (GtkRcContext *context,
4253 GtkIconSet *icon_set,
4254 gboolean *icon_set_valid)
4257 gchar *full_filename;
4258 GtkIconSource *source = NULL;
4260 token = g_scanner_get_next_token (scanner);
4261 if (token != G_TOKEN_LEFT_CURLY)
4262 return G_TOKEN_LEFT_CURLY;
4264 token = g_scanner_get_next_token (scanner);
4266 if (token != G_TOKEN_STRING && token != '@')
4267 return G_TOKEN_STRING;
4269 if (token == G_TOKEN_STRING)
4273 source = gtk_icon_source_new ();
4274 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4277 gtk_icon_source_set_filename (source, full_filename);
4278 g_free (full_filename);
4285 token = g_scanner_get_next_token (scanner);
4287 if (token != G_TOKEN_STRING)
4288 return G_TOKEN_STRING;
4290 source = gtk_icon_source_new ();
4291 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4294 /* We continue parsing even if we didn't find the pixmap so that rest of the
4295 * file is read, even if the syntax is bad. However we don't validate the
4296 * icon_set so the caller can choose not to install it.
4298 token = g_scanner_get_next_token (scanner);
4300 if (token == G_TOKEN_RIGHT_CURLY)
4302 else if (token != G_TOKEN_COMMA)
4304 gtk_icon_source_free (source);
4305 return G_TOKEN_COMMA;
4308 /* Get the direction */
4310 token = g_scanner_get_next_token (scanner);
4314 case GTK_RC_TOKEN_RTL:
4315 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4316 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4319 case GTK_RC_TOKEN_LTR:
4320 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4321 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4328 gtk_icon_source_free (source);
4329 return GTK_RC_TOKEN_RTL;
4333 token = g_scanner_get_next_token (scanner);
4335 if (token == G_TOKEN_RIGHT_CURLY)
4337 else if (token != G_TOKEN_COMMA)
4339 gtk_icon_source_free (source);
4340 return G_TOKEN_COMMA;
4345 token = g_scanner_get_next_token (scanner);
4349 case GTK_RC_TOKEN_NORMAL:
4350 gtk_icon_source_set_state_wildcarded (source, FALSE);
4351 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4354 case GTK_RC_TOKEN_PRELIGHT:
4355 gtk_icon_source_set_state_wildcarded (source, FALSE);
4356 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4360 case GTK_RC_TOKEN_INSENSITIVE:
4361 gtk_icon_source_set_state_wildcarded (source, FALSE);
4362 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4365 case GTK_RC_TOKEN_ACTIVE:
4366 gtk_icon_source_set_state_wildcarded (source, FALSE);
4367 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4370 case GTK_RC_TOKEN_SELECTED:
4371 gtk_icon_source_set_state_wildcarded (source, FALSE);
4372 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4379 gtk_icon_source_free (source);
4380 return GTK_RC_TOKEN_PRELIGHT;
4384 token = g_scanner_get_next_token (scanner);
4386 if (token == G_TOKEN_RIGHT_CURLY)
4388 else if (token != G_TOKEN_COMMA)
4390 gtk_icon_source_free (source);
4391 return G_TOKEN_COMMA;
4396 token = g_scanner_get_next_token (scanner);
4402 if (token != G_TOKEN_STRING)
4404 gtk_icon_source_free (source);
4405 return G_TOKEN_STRING;
4408 size = gtk_icon_size_from_name (scanner->value.v_string);
4410 if (size != GTK_ICON_SIZE_INVALID)
4412 gtk_icon_source_set_size_wildcarded (source, FALSE);
4413 gtk_icon_source_set_size (source, size);
4417 /* Check the close brace */
4419 token = g_scanner_get_next_token (scanner);
4420 if (token != G_TOKEN_RIGHT_CURLY)
4422 gtk_icon_source_free (source);
4423 return G_TOKEN_RIGHT_CURLY;
4427 if (gtk_icon_source_get_filename (source) ||
4428 gtk_icon_source_get_icon_name (source))
4430 gtk_icon_set_add_source (icon_set, source);
4431 *icon_set_valid = TRUE;
4433 gtk_icon_source_free (source);
4435 return G_TOKEN_NONE;
4439 gtk_rc_parse_stock (GtkRcContext *context,
4441 GtkRcStyle *rc_style,
4442 GtkIconFactory *factory)
4444 GtkIconSet *icon_set = NULL;
4445 gboolean icon_set_valid = FALSE;
4446 gchar *stock_id = NULL;
4449 token = g_scanner_get_next_token (scanner);
4450 if (token != GTK_RC_TOKEN_STOCK)
4451 return GTK_RC_TOKEN_STOCK;
4453 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4454 if (token != G_TOKEN_NONE)
4457 token = g_scanner_get_next_token (scanner);
4458 if (token != G_TOKEN_EQUAL_SIGN)
4461 return G_TOKEN_EQUAL_SIGN;
4464 token = g_scanner_get_next_token (scanner);
4465 if (token != G_TOKEN_LEFT_CURLY)
4468 return G_TOKEN_LEFT_CURLY;
4471 token = g_scanner_peek_next_token (scanner);
4472 while (token != G_TOKEN_RIGHT_CURLY)
4474 if (icon_set == NULL)
4475 icon_set = gtk_icon_set_new ();
4477 token = gtk_rc_parse_icon_source (context,
4478 scanner, icon_set, &icon_set_valid);
4479 if (token != G_TOKEN_NONE)
4482 gtk_icon_set_unref (icon_set);
4486 token = g_scanner_get_next_token (scanner);
4488 if (token != G_TOKEN_COMMA &&
4489 token != G_TOKEN_RIGHT_CURLY)
4492 gtk_icon_set_unref (icon_set);
4493 return G_TOKEN_RIGHT_CURLY;
4500 gtk_icon_factory_add (factory,
4504 gtk_icon_set_unref (icon_set);
4509 return G_TOKEN_NONE;
4513 gtk_rc_parse_logical_color (GScanner *scanner,
4514 GtkRcStyle *rc_style,
4517 gchar *color_id = NULL;
4521 token = g_scanner_get_next_token (scanner);
4522 if (token != GTK_RC_TOKEN_COLOR)
4523 return GTK_RC_TOKEN_COLOR;
4525 token = gtk_rc_parse_hash_key (scanner, &color_id);
4526 if (token != G_TOKEN_NONE)
4529 token = g_scanner_get_next_token (scanner);
4530 if (token != G_TOKEN_EQUAL_SIGN)
4533 return G_TOKEN_EQUAL_SIGN;
4536 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4537 if (token != G_TOKEN_NONE)
4543 /* Because the hash is created with destroy functions,
4544 * g_hash_table_insert will free any old values for us,
4545 * if a mapping with the specified key already exists.
4547 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4549 return G_TOKEN_NONE;
4554 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4558 const gchar *current;
4559 const gchar *class_start;
4560 const gchar *class_end;
4561 const gchar *pattern_end;
4562 const gchar *pattern_start;
4567 while ((class_start = strchr (current, '<')) &&
4568 (class_end = strchr (class_start, '>')))
4570 /* Add patterns, but ignore single dots */
4571 if (!(class_start == current ||
4572 (class_start == current + 1 && current[0] == '.')))
4574 pattern_end = class_start - 1;
4575 pattern_start = current;
4577 path_elt = g_new (PathElt, 1);
4579 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4580 path_elt->type = PATH_ELT_PSPEC;
4581 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4582 g_free (sub_pattern);
4584 result = g_slist_prepend (result, path_elt);
4587 path_elt = g_new (PathElt, 1);
4589 /* The < > need to be removed from the string. */
4590 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4592 path_elt->type = PATH_ELT_UNRESOLVED;
4593 path_elt->elt.class_name = sub_pattern;
4595 result = g_slist_prepend (result, path_elt);
4597 current = class_end + 1;
4600 /* Add the rest, if anything is left */
4601 if (strlen (current) > 0)
4603 path_elt = g_new (PathElt, 1);
4604 path_elt->type = PATH_ELT_PSPEC;
4605 path_elt->elt.pspec = g_pattern_spec_new (current);
4607 result = g_slist_prepend (result, path_elt);
4610 return g_slist_reverse (result);
4614 free_path_elt (gpointer data,
4617 PathElt *path_elt = data;
4619 switch (path_elt->type)
4621 case PATH_ELT_PSPEC:
4622 g_pattern_spec_free (path_elt->elt.pspec);
4624 case PATH_ELT_UNRESOLVED:
4625 g_free (path_elt->elt.class_name);
4630 g_assert_not_reached ();
4637 _gtk_rc_free_widget_class_path (GSList *list)
4639 g_slist_foreach (list, free_path_elt, NULL);
4640 g_slist_free (list);
4644 gtk_rc_set_free (GtkRcSet *rc_set)
4647 g_pattern_spec_free (rc_set->pspec);
4649 _gtk_rc_free_widget_class_path (rc_set->path);
4655 match_class (PathElt *path_elt,
4660 if (path_elt->type == PATH_ELT_UNRESOLVED)
4662 type = g_type_from_name (path_elt->elt.class_name);
4663 if (type != G_TYPE_INVALID)
4665 g_free (path_elt->elt.class_name);
4666 path_elt->elt.class_type = type;
4667 path_elt->type = PATH_ELT_TYPE;
4670 return g_str_equal (type_name, path_elt->elt.class_name);
4673 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4677 match_widget_class_recursive (GSList *list,
4680 gchar *path_reversed)
4684 /* break out if we cannot match anymore. */
4693 /* there are two possibilities:
4694 * 1. The next pattern should match the class.
4695 * 2. First normal matching, and then maybe a class */
4697 path_elt = list->data;
4699 if (path_elt->type != PATH_ELT_PSPEC)
4701 gchar *class_start = path;
4704 /* ignore leading dot */
4705 if (class_start[0] == '.')
4707 class_end = strchr (class_start, '.');
4709 if (class_end == NULL)
4711 if (!match_class (path_elt, class_start))
4714 return match_widget_class_recursive (list->next, 0, "", "");
4718 class_end[0] = '\0';
4719 if (!match_class (path_elt, class_start))
4727 gint new_length = length - (class_end - path);
4728 gchar old_char = path_reversed[new_length];
4732 path_reversed[new_length] = '\0';
4733 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4734 path_reversed[new_length] = old_char;
4745 gboolean result = FALSE;
4747 /* If there is nothing after this (ie. no class match),
4748 * just compare the pspec.
4750 if (list->next == NULL)
4751 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4753 class_elt = (PathElt *)list->next->data;
4754 g_assert (class_elt->type != PATH_ELT_PSPEC);
4757 if (class_start[0] == '.')
4762 class_end = strchr (class_start, '.');
4764 /* It should be cheaper to match the class first. (either the pattern
4765 * is simple, and will match most of the times, or it may be complex
4766 * and matching is slow)
4768 if (class_end == NULL)
4770 result = match_class (class_elt, class_start);
4774 class_end[0] = '\0';
4775 result = match_class (class_elt, class_start);
4784 /* terminate the string in front of the class. It does not matter
4785 * that the class becomes unusable, because it is not needed
4786 * inside the recursion
4788 old_char = class_start[0];
4789 class_start[0] = '\0';
4791 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4793 if (class_end != NULL)
4795 gint new_length = length - (class_end - path);
4796 gchar path_reversed_char = path_reversed[new_length];
4798 path_reversed[new_length] = '\0';
4800 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4802 path_reversed[new_length] = path_reversed_char;
4805 result = match_widget_class_recursive (list->next->next, 0, "", "");
4808 class_start[0] = old_char;
4814 /* get next class in path, or break out */
4815 if (class_end != NULL)
4816 class_start = class_end + 1;
4824 _gtk_rc_match_widget_class (GSList *list,
4827 gchar *path_reversed)
4829 return match_widget_class_recursive (list, length, path, path_reversed);