1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
44 #include <glib/gstdio.h>
45 #include "gdkconfig.h"
47 #include "gtkversion.h"
49 #include "gtkbindings.h"
50 #include "gtkthemes.h"
52 #include "gtkiconfactory.h"
54 #include "gtkmodules.h"
55 #include "gtkprivate.h"
56 #include "gtksettings.h"
57 #include "gtkwindow.h"
63 typedef struct _GtkRcSet GtkRcSet;
64 typedef struct _GtkRcNode GtkRcNode;
65 typedef struct _GtkRcFile GtkRcFile;
100 gchar *canonical_name;
103 guint is_string : 1; /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
109 GHashTable *rc_style_ht;
110 GtkSettings *settings;
111 GSList *rc_sets_widget;
112 GSList *rc_sets_widget_class;
113 GSList *rc_sets_class;
115 /* The files we have parsed, to reread later if necessary */
119 gboolean prefer_dark_theme;
120 gchar *key_theme_name;
125 gint default_priority;
126 GtkStyle *default_style;
128 GHashTable *color_hash;
133 #define GTK_RC_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RC_STYLE, GtkRcStylePrivate))
135 typedef struct _GtkRcStylePrivate GtkRcStylePrivate;
137 struct _GtkRcStylePrivate
139 GSList *color_hashes;
142 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
144 static guint gtk_rc_style_hash (const gchar *name);
145 static gboolean gtk_rc_style_equal (const gchar *a,
147 static guint gtk_rc_styles_hash (const GSList *rc_styles);
148 static gboolean gtk_rc_styles_equal (const GSList *a,
150 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
152 static GSList * gtk_rc_styles_match (GSList *rc_styles,
156 gchar *path_reversed);
157 static GtkStyle * gtk_rc_style_to_style (GtkRcContext *context,
158 GtkRcStyle *rc_style);
159 static GtkStyle* gtk_rc_init_style (GtkRcContext *context,
161 static void gtk_rc_parse_default_files (GtkRcContext *context);
162 static gboolean gtk_rc_parse_named (GtkRcContext *context,
165 const gchar *variant);
166 static void gtk_rc_context_parse_file (GtkRcContext *context,
167 const gchar *filename,
170 static void gtk_rc_parse_any (GtkRcContext *context,
171 const gchar *input_name,
173 const gchar *input_string);
174 static guint gtk_rc_parse_statement (GtkRcContext *context,
176 static guint gtk_rc_parse_style (GtkRcContext *context,
178 static guint gtk_rc_parse_assignment (GScanner *scanner,
180 GtkRcProperty *prop);
181 static guint gtk_rc_parse_bg (GScanner *scanner,
183 static guint gtk_rc_parse_fg (GScanner *scanner,
185 static guint gtk_rc_parse_text (GScanner *scanner,
187 static guint gtk_rc_parse_base (GScanner *scanner,
189 static guint gtk_rc_parse_xthickness (GScanner *scanner,
191 static guint gtk_rc_parse_ythickness (GScanner *scanner,
193 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
195 GtkRcStyle *rc_style);
196 static guint gtk_rc_parse_font (GScanner *scanner,
197 GtkRcStyle *rc_style);
198 static guint gtk_rc_parse_fontset (GScanner *scanner,
199 GtkRcStyle *rc_style);
200 static guint gtk_rc_parse_font_name (GScanner *scanner,
201 GtkRcStyle *rc_style);
202 static guint gtk_rc_parse_engine (GtkRcContext *context,
204 GtkRcStyle **rc_style);
205 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
207 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
209 const gchar *pix_path);
210 static guint gtk_rc_parse_module_path (GScanner *scanner);
211 static guint gtk_rc_parse_im_module_file (GScanner *scanner);
212 static guint gtk_rc_parse_path_pattern (GtkRcContext *context,
214 static guint gtk_rc_parse_stock (GtkRcContext *context,
216 GtkRcStyle *rc_style,
217 GtkIconFactory *factory);
218 static guint gtk_rc_parse_logical_color (GScanner *scanner,
219 GtkRcStyle *rc_style,
222 static void gtk_rc_clear_hash_node (gpointer key,
225 static void gtk_rc_clear_styles (GtkRcContext *context);
226 static void gtk_rc_add_initial_default_files (void);
228 static void gtk_rc_style_finalize (GObject *object);
229 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
231 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
232 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
233 static void gtk_rc_style_copy_icons_and_colors(GtkRcStyle *rc_style,
234 GtkRcStyle *src_style,
235 GtkRcContext *context);
236 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
237 gconstpointer bsearch_node2);
238 static void gtk_rc_set_free (GtkRcSet *rc_set);
240 static void insert_rc_property (GtkRcStyle *style,
241 GtkRcProperty *property,
245 static const GScannerConfig gtk_rc_scanner_config =
249 ) /* cset_skip_characters */,
254 ) /* cset_identifier_first */,
260 ) /* cset_identifier_nth */,
261 ( "#\n" ) /* cpair_comment_single */,
263 TRUE /* case_sensitive */,
265 TRUE /* skip_comment_multi */,
266 TRUE /* skip_comment_single */,
267 TRUE /* scan_comment_multi */,
268 TRUE /* scan_identifier */,
269 FALSE /* scan_identifier_1char */,
270 FALSE /* scan_identifier_NULL */,
271 TRUE /* scan_symbols */,
272 TRUE /* scan_binary */,
273 TRUE /* scan_octal */,
274 TRUE /* scan_float */,
276 TRUE /* scan_hex_dollar */,
277 TRUE /* scan_string_sq */,
278 TRUE /* scan_string_dq */,
279 TRUE /* numbers_2_int */,
280 FALSE /* int_2_float */,
281 FALSE /* identifier_2_string */,
282 TRUE /* char_2_token */,
283 TRUE /* symbol_2_token */,
284 FALSE /* scope_0_fallback */,
287 static const gchar symbol_names[] =
331 { 0, GTK_RC_TOKEN_INCLUDE },
332 { 8, GTK_RC_TOKEN_NORMAL },
333 { 15, GTK_RC_TOKEN_ACTIVE },
334 { 22, GTK_RC_TOKEN_PRELIGHT },
335 { 31, GTK_RC_TOKEN_SELECTED },
336 { 40, GTK_RC_TOKEN_INSENSITIVE },
337 { 52, GTK_RC_TOKEN_FG },
338 { 55, GTK_RC_TOKEN_BG },
339 { 58, GTK_RC_TOKEN_TEXT },
340 { 63, GTK_RC_TOKEN_BASE },
341 { 68, GTK_RC_TOKEN_XTHICKNESS },
342 { 79, GTK_RC_TOKEN_YTHICKNESS },
343 { 90, GTK_RC_TOKEN_FONT },
344 { 95, GTK_RC_TOKEN_FONTSET },
345 { 103, GTK_RC_TOKEN_FONT_NAME },
346 { 113, GTK_RC_TOKEN_BG_PIXMAP },
347 { 123, GTK_RC_TOKEN_PIXMAP_PATH },
348 { 135, GTK_RC_TOKEN_STYLE },
349 { 141, GTK_RC_TOKEN_BINDING },
350 { 149, GTK_RC_TOKEN_BIND },
351 { 154, GTK_RC_TOKEN_WIDGET },
352 { 161, GTK_RC_TOKEN_WIDGET_CLASS },
353 { 174, GTK_RC_TOKEN_CLASS },
354 { 180, GTK_RC_TOKEN_LOWEST },
355 { 187, GTK_RC_TOKEN_GTK },
356 { 191, GTK_RC_TOKEN_APPLICATION },
357 { 203, GTK_RC_TOKEN_THEME },
358 { 209, GTK_RC_TOKEN_RC },
359 { 212, GTK_RC_TOKEN_HIGHEST },
360 { 220, GTK_RC_TOKEN_ENGINE },
361 { 227, GTK_RC_TOKEN_MODULE_PATH },
362 { 239, GTK_RC_TOKEN_STOCK },
363 { 245, GTK_RC_TOKEN_IM_MODULE_FILE },
364 { 260, GTK_RC_TOKEN_LTR },
365 { 264, GTK_RC_TOKEN_RTL },
366 { 268, GTK_RC_TOKEN_COLOR },
367 { 274, GTK_RC_TOKEN_UNBIND }
370 static GHashTable *realized_style_ht = NULL;
372 static gchar *im_module_file = NULL;
374 static gint max_default_files = 0;
375 static gchar **gtk_rc_default_files = NULL;
377 /* A stack of information of RC files we are parsing currently.
378 * The directories for these files are implicitely added to the end of
381 static GSList *current_files_stack = NULL;
383 /* RC files and strings that are parsed for every context
385 static GSList *global_rc_files = NULL;
387 /* Keep list of all current RC contexts for convenience
389 static GSList *rc_contexts;
391 /* RC file handling */
394 gtk_rc_make_default_dir (const gchar *type)
399 var = g_getenv ("GTK_EXE_PREFIX");
402 path = g_build_filename (var, "lib", "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
404 path = g_build_filename (GTK_LIBDIR, "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
410 * gtk_rc_get_im_module_path:
411 * @returns: a newly-allocated string containing the path in which to
412 * look for IM modules.
414 * Obtains the path in which to look for IM modules. See the documentation
415 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
416 * environment variable for more details about looking up modules. This
417 * function is useful solely for utilities supplied with GTK+ and should
418 * not be used by applications under normal circumstances.
421 gtk_rc_get_im_module_path (void)
423 gchar **paths = _gtk_get_module_path ("immodules");
424 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
431 * gtk_rc_get_im_module_file:
432 * @returns: a newly-allocated string containing the name of the file
433 * listing the IM modules available for loading
435 * Obtains the path to the IM modules file. See the documentation
436 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
437 * environment variable for more details.
440 gtk_rc_get_im_module_file (void)
442 const gchar *var = g_getenv ("GTK_IM_MODULE_FILE");
443 gchar *result = NULL;
446 result = g_strdup (var);
451 result = g_strdup (im_module_file);
453 result = gtk_rc_make_default_dir ("immodules.cache");
460 gtk_rc_get_theme_dir (void)
465 var = g_getenv ("GTK_DATA_PREFIX");
468 path = g_build_filename (var, "share", "themes", NULL);
470 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
476 * gtk_rc_get_module_dir:
478 * Returns a directory in which GTK+ looks for theme engines.
479 * For full information about the search for theme engines,
480 * see the docs for <envar>GTK_PATH</envar> in
481 * <xref linkend="gtk-running"/>.
483 * return value: the directory. (Must be freed with g_free())
486 gtk_rc_get_module_dir (void)
488 return gtk_rc_make_default_dir ("engines");
492 gtk_rc_add_initial_default_files (void)
494 static gint init = FALSE;
503 gtk_rc_default_files = g_new (gchar*, 10);
504 max_default_files = 10;
506 gtk_rc_default_files[0] = NULL;
509 var = g_getenv ("GTK2_RC_FILES");
513 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, -1);
517 gtk_rc_add_default_file (files[i]);
525 str = g_build_filename (GTK_SYSCONFDIR, "gtk-3.0", "gtkrc", NULL);
527 gtk_rc_add_default_file (str);
530 home = g_get_home_dir ();
533 str = g_build_filename (home, ".gtkrc-3.0", NULL);
534 gtk_rc_add_default_file (str);
541 * gtk_rc_add_default_file:
542 * @filename: the pathname to the file. If @filename is not absolute, it
543 * is searched in the current directory.
545 * Adds a file to the list of files to be parsed at the
549 gtk_rc_add_default_file (const gchar *filename)
553 gtk_rc_add_initial_default_files ();
555 for (n = 0; n < max_default_files; n++)
557 if (gtk_rc_default_files[n] == NULL)
561 if (n == max_default_files)
563 max_default_files += 10;
564 gtk_rc_default_files = g_renew (gchar*, gtk_rc_default_files, max_default_files);
567 gtk_rc_default_files[n++] = g_strdup (filename);
568 gtk_rc_default_files[n] = NULL;
572 * gtk_rc_set_default_files:
573 * @filenames: A %NULL-terminated list of filenames.
575 * Sets the list of files that GTK+ will read at the
579 gtk_rc_set_default_files (gchar **filenames)
583 gtk_rc_add_initial_default_files ();
586 while (gtk_rc_default_files[i])
588 g_free (gtk_rc_default_files[i]);
592 gtk_rc_default_files[0] = NULL;
595 while (filenames[i] != NULL)
597 gtk_rc_add_default_file (filenames[i]);
603 * gtk_rc_get_default_files:
605 * Retrieves the current list of RC files that will be parsed
606 * at the end of gtk_init().
608 * Return value: A %NULL-terminated array of filenames. This memory
609 * is owned by GTK+ and must not be freed by the application.
610 * If you want to store this information, you should make a copy.
613 gtk_rc_get_default_files (void)
615 gtk_rc_add_initial_default_files ();
617 return gtk_rc_default_files;
621 gtk_rc_settings_changed (GtkSettings *settings,
623 GtkRcContext *context)
625 gchar *new_theme_name;
626 gchar *new_key_theme_name;
627 gboolean new_prefer_dark_theme;
629 if (context->reloading)
632 g_object_get (settings,
633 "gtk-theme-name", &new_theme_name,
634 "gtk-key-theme-name", &new_key_theme_name,
635 "gtk-application-prefer-dark-theme", &new_prefer_dark_theme,
638 if ((new_theme_name != context->theme_name &&
639 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
640 (new_key_theme_name != context->key_theme_name &&
641 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)) ||
642 new_prefer_dark_theme != context->prefer_dark_theme)
644 gtk_rc_reparse_all_for_settings (settings, TRUE);
647 g_free (new_theme_name);
648 g_free (new_key_theme_name);
652 gtk_rc_font_name_changed (GtkSettings *settings,
654 GtkRcContext *context)
656 if (!context->reloading)
657 _gtk_rc_context_get_default_font_name (settings);
661 gtk_rc_color_hash_changed (GtkSettings *settings,
663 GtkRcContext *context)
665 GHashTable *old_hash;
667 old_hash = context->color_hash;
669 g_object_get (settings, "color-hash", &context->color_hash, NULL);
672 g_hash_table_unref (old_hash);
674 gtk_rc_reparse_all_for_settings (settings, TRUE);
677 static GtkRcContext *
678 gtk_rc_context_get (GtkSettings *settings)
680 if (!settings->rc_context)
682 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
684 context->settings = settings;
685 context->rc_style_ht = NULL;
686 context->rc_sets_widget = NULL;
687 context->rc_sets_widget_class = NULL;
688 context->rc_sets_class = NULL;
689 context->rc_files = NULL;
690 context->default_style = NULL;
691 context->reloading = FALSE;
693 g_object_get (settings,
694 "gtk-theme-name", &context->theme_name,
695 "gtk-key-theme-name", &context->key_theme_name,
696 "gtk-font-name", &context->font_name,
697 "color-hash", &context->color_hash,
698 "gtk-application-prefer-dark-theme", &context->prefer_dark_theme,
701 g_signal_connect (settings,
702 "notify::gtk-theme-name",
703 G_CALLBACK (gtk_rc_settings_changed),
705 g_signal_connect (settings,
706 "notify::gtk-key-theme-name",
707 G_CALLBACK (gtk_rc_settings_changed),
709 g_signal_connect (settings,
710 "notify::gtk-font-name",
711 G_CALLBACK (gtk_rc_font_name_changed),
713 g_signal_connect (settings,
714 "notify::color-hash",
715 G_CALLBACK (gtk_rc_color_hash_changed),
717 g_signal_connect (settings,
718 "notify::gtk-application-prefer-dark-theme",
719 G_CALLBACK (gtk_rc_settings_changed),
722 context->pixmap_path = NULL;
724 context->default_priority = GTK_PATH_PRIO_RC;
726 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
729 return settings->rc_context;
733 gtk_rc_clear_rc_files (GtkRcContext *context)
737 list = context->rc_files;
740 GtkRcFile *rc_file = list->data;
742 if (rc_file->canonical_name != rc_file->name)
743 g_free (rc_file->canonical_name);
744 g_free (rc_file->directory);
745 g_free (rc_file->name);
751 g_slist_free (context->rc_files);
752 context->rc_files = NULL;
756 _gtk_rc_context_destroy (GtkSettings *settings)
758 GtkRcContext *context;
760 g_return_if_fail (GTK_IS_SETTINGS (settings));
762 context = settings->rc_context;
766 _gtk_settings_reset_rc_values (context->settings);
767 gtk_rc_clear_styles (context);
768 gtk_rc_clear_rc_files (context);
770 if (context->default_style)
771 g_object_unref (context->default_style);
773 g_strfreev (context->pixmap_path);
775 g_free (context->theme_name);
776 g_free (context->key_theme_name);
777 g_free (context->font_name);
779 if (context->color_hash)
780 g_hash_table_unref (context->color_hash);
782 g_signal_handlers_disconnect_by_func (settings,
783 gtk_rc_settings_changed, context);
784 g_signal_handlers_disconnect_by_func (settings,
785 gtk_rc_font_name_changed, context);
786 g_signal_handlers_disconnect_by_func (settings,
787 gtk_rc_color_hash_changed, context);
789 rc_contexts = g_slist_remove (rc_contexts, context);
793 settings->rc_context = NULL;
797 gtk_rc_parse_named (GtkRcContext *context,
800 const gchar *variant)
803 const gchar *home_dir;
810 subpath = g_strconcat ("gtk-3.0-", type,
811 G_DIR_SEPARATOR_S "gtkrc",
814 subpath = g_strconcat ("gtk-3.0" G_DIR_SEPARATOR_S "gtkrc",
817 /* First look in the users home directory
819 home_dir = g_get_home_dir ();
822 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
823 if (!g_file_test (path, G_FILE_TEST_EXISTS))
832 gchar *theme_dir = gtk_rc_get_theme_dir ();
833 path = g_build_filename (theme_dir, name, subpath, NULL);
836 if (!g_file_test (path, G_FILE_TEST_EXISTS))
845 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
856 gtk_rc_parse_default_files (GtkRcContext *context)
860 gtk_rc_add_initial_default_files ();
862 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
863 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
869 static gboolean initialized = FALSE;
875 gtk_rc_add_initial_default_files ();
878 /* Default RC string */
879 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
880 " bg[NORMAL] = \"#eee1b3\"\n"
881 " fg[NORMAL] = \"#000000\"\n"
884 "style \"gtk-default-progress-bar-style\" {\n"
885 " bg[PRELIGHT] = \"#4b6983\"\n"
886 " fg[PRELIGHT] = \"#ffffff\"\n"
887 " bg[NORMAL] = \"#c4c2bd\"\n"
890 "style \"gtk-default-entry-style\" {\n"
891 " bg[SELECTED] = \"#b7c3cd\"\n"
892 " fg[SELECTED] = \"#000000\"\n"
895 "style \"gtk-default-menu-bar-item-style\" {\n"
896 " GtkMenuItem::horizontal_padding = 5\n"
899 "style \"gtk-default-menu-item-style\" {\n"
900 " bg[PRELIGHT] = \"#4b6983\"\n"
901 " fg[PRELIGHT] = \"#ffffff\"\n"
902 " base[PRELIGHT] = \"#4b6983\"\n"
903 " text[PRELIGHT] = \"#ffffff\"\n"
906 /* Work around clipping of accelerator underlines */
907 "style \"gtk-default-label-style\" {\n"
908 " GtkWidget::draw-border = {0,0,0,1}\n"
911 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
912 "class \"GtkEntry\" style : gtk \"gtk-default-entry-style\"\n"
913 "widget \"gtk-tooltip*\" style : gtk \"gtk-default-tooltips-style\"\n"
914 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
915 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
916 "class \"GtkLabel\" style : gtk \"gtk-default-label-style\"\n"
921 gtk_rc_context_parse_string (GtkRcContext *context,
922 const gchar *rc_string)
924 gtk_rc_parse_any (context, "-", -1, rc_string);
928 gtk_rc_parse_string (const gchar *rc_string)
933 g_return_if_fail (rc_string != NULL);
935 rc_file = g_new (GtkRcFile, 1);
936 rc_file->is_string = TRUE;
937 rc_file->name = g_strdup (rc_string);
938 rc_file->canonical_name = NULL;
939 rc_file->directory = NULL;
941 rc_file->reload = TRUE;
943 global_rc_files = g_slist_append (global_rc_files, rc_file);
945 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
946 gtk_rc_context_parse_string (tmp_list->data, rc_string);
950 add_to_rc_file_list (GSList **rc_file_list,
951 const char *filename,
957 tmp_list = *rc_file_list;
960 rc_file = tmp_list->data;
961 if (!strcmp (rc_file->name, filename))
964 tmp_list = tmp_list->next;
967 rc_file = g_new (GtkRcFile, 1);
968 rc_file->is_string = FALSE;
969 rc_file->name = g_strdup (filename);
970 rc_file->canonical_name = NULL;
971 rc_file->directory = NULL;
973 rc_file->reload = reload;
975 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
981 gtk_rc_context_parse_one_file (GtkRcContext *context,
982 const gchar *filename,
990 g_return_if_fail (filename != NULL);
992 saved_priority = context->default_priority;
993 context->default_priority = priority;
995 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
997 if (!rc_file->canonical_name)
999 /* Get the absolute pathname */
1001 if (g_path_is_absolute (rc_file->name))
1002 rc_file->canonical_name = rc_file->name;
1007 cwd = g_get_current_dir ();
1008 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
1012 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
1015 /* If the file is already being parsed (recursion), do nothing
1017 if (g_slist_find (current_files_stack, rc_file))
1020 if (!g_lstat (rc_file->canonical_name, &statbuf))
1024 rc_file->mtime = statbuf.st_mtime;
1026 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1030 /* Temporarily push information for this file on
1031 * a stack of current files while parsing it.
1033 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1034 gtk_rc_parse_any (context, filename, fd, NULL);
1035 current_files_stack = g_slist_delete_link (current_files_stack,
1036 current_files_stack);
1042 context->default_priority = saved_priority;
1046 strchr_len (const gchar *str, gint len, char c)
1051 return (gchar *)str;
1060 gtk_rc_context_parse_file (GtkRcContext *context,
1061 const gchar *filename,
1065 gchar *locale_suffixes[2];
1066 gint n_locale_suffixes = 0;
1070 gboolean found = FALSE;
1072 locale = _gtk_get_lc_ctype ();
1074 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1076 /* Determine locale-specific suffixes for RC files.
1078 length = strlen (locale);
1080 p = strchr (locale, '@');
1082 length = p - locale;
1084 p = strchr_len (locale, length, '.');
1086 length = p - locale;
1088 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1090 p = strchr_len (locale, length, '_');
1093 length = p - locale;
1094 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1100 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1101 for (j = 0; j < n_locale_suffixes; j++)
1105 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1106 if (g_file_test (name, G_FILE_TEST_EXISTS))
1108 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1115 g_free (locale_suffixes[j]);
1120 gtk_rc_parse (const gchar *filename)
1124 g_return_if_fail (filename != NULL);
1126 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1128 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1129 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1132 /* Handling of RC styles */
1134 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1137 gtk_rc_style_init (GtkRcStyle *style)
1139 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1143 style->font_desc = NULL;
1145 for (i = 0; i < 5; i++)
1147 static const GdkColor init_color = { 0, 0, 0, 0, };
1149 style->bg_pixmap_name[i] = NULL;
1150 style->color_flags[i] = 0;
1151 style->fg[i] = init_color;
1152 style->bg[i] = init_color;
1153 style->text[i] = init_color;
1154 style->base[i] = init_color;
1156 style->xthickness = -1;
1157 style->ythickness = -1;
1158 style->rc_properties = NULL;
1160 style->rc_style_lists = NULL;
1161 style->icon_factories = NULL;
1163 priv->color_hashes = NULL;
1167 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1169 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1171 object_class->finalize = gtk_rc_style_finalize;
1173 klass->parse = NULL;
1174 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1175 klass->merge = gtk_rc_style_real_merge;
1176 klass->create_style = gtk_rc_style_real_create_style;
1178 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1182 gtk_rc_style_finalize (GObject *object)
1184 GSList *tmp_list1, *tmp_list2;
1185 GtkRcStyle *rc_style;
1186 GtkRcStylePrivate *rc_priv;
1189 rc_style = GTK_RC_STYLE (object);
1190 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1192 g_free (rc_style->name);
1193 if (rc_style->font_desc)
1194 pango_font_description_free (rc_style->font_desc);
1196 for (i = 0; i < 5; i++)
1197 g_free (rc_style->bg_pixmap_name[i]);
1199 /* Now remove all references to this rc_style from
1202 tmp_list1 = rc_style->rc_style_lists;
1205 GSList *rc_styles = tmp_list1->data;
1206 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1207 g_object_unref (style);
1209 /* Remove the list of styles from the other rc_styles
1212 tmp_list2 = rc_styles;
1215 GtkRcStyle *other_style = tmp_list2->data;
1217 if (other_style != rc_style)
1218 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1220 tmp_list2 = tmp_list2->next;
1223 /* And from the hash table itself
1225 g_hash_table_remove (realized_style_ht, rc_styles);
1226 g_slist_free (rc_styles);
1228 tmp_list1 = tmp_list1->next;
1230 g_slist_free (rc_style->rc_style_lists);
1232 if (rc_style->rc_properties)
1236 for (i = 0; i < rc_style->rc_properties->len; i++)
1238 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1240 g_free (node->origin);
1241 g_value_unset (&node->value);
1243 g_array_free (rc_style->rc_properties, TRUE);
1244 rc_style->rc_properties = NULL;
1247 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1248 g_slist_free (rc_style->icon_factories);
1250 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1251 g_slist_free (rc_priv->color_hashes);
1253 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1257 gtk_rc_style_new (void)
1261 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1267 * gtk_rc_style_copy:
1268 * @orig: the style to copy
1270 * Makes a copy of the specified #GtkRcStyle. This function
1271 * will correctly copy an RC style that is a member of a class
1272 * derived from #GtkRcStyle.
1274 * Return value: the resulting #GtkRcStyle
1277 gtk_rc_style_copy (GtkRcStyle *orig)
1281 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1283 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1284 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1286 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1292 _gtk_rc_style_set_rc_property (GtkRcStyle *rc_style,
1293 GtkRcProperty *property)
1295 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1296 g_return_if_fail (property != NULL);
1298 insert_rc_property (rc_style, property, TRUE);
1302 _gtk_rc_style_unset_rc_property (GtkRcStyle *rc_style,
1304 GQuark property_name)
1306 GtkRcProperty *node;
1308 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1310 node = (GtkRcProperty *) _gtk_rc_style_lookup_rc_property (rc_style,
1316 guint index = node - (GtkRcProperty *) rc_style->rc_properties->data;
1317 g_value_unset (&node->value);
1318 g_free (node->origin);
1319 g_array_remove_index (rc_style->rc_properties, index);
1324 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1326 return g_object_new (G_OBJECT_TYPE (style), NULL);
1330 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1332 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1334 return priv->color_hashes;
1337 static void gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style);
1340 _gtk_rc_style_set_symbolic_color (GtkRcStyle *rc_style,
1342 const GdkColor *color)
1344 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1345 GHashTable *our_hash = NULL;
1347 if (priv->color_hashes)
1348 our_hash = priv->color_hashes->data;
1350 if (our_hash == NULL)
1355 gtk_rc_style_prepend_empty_color_hash (rc_style);
1356 our_hash = priv->color_hashes->data;
1360 g_hash_table_insert (our_hash, g_strdup (name), gdk_color_copy (color));
1362 g_hash_table_remove (our_hash, name);
1366 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1367 gconstpointer bsearch_node2)
1369 const GtkRcProperty *prop1 = bsearch_node1;
1370 const GtkRcProperty *prop2 = bsearch_node2;
1372 if (prop1->type_name == prop2->type_name)
1373 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1375 return prop1->type_name < prop2->type_name ? -1 : 1;
1379 insert_rc_property (GtkRcStyle *style,
1380 GtkRcProperty *property,
1384 GtkRcProperty *new_property = NULL;
1385 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1387 key.type_name = property->type_name;
1388 key.property_name = property->property_name;
1390 if (!style->rc_properties)
1391 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1394 while (i < style->rc_properties->len)
1396 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1402 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1404 g_free (new_property->origin);
1405 g_value_unset (&new_property->value);
1407 *new_property = key;
1421 g_array_insert_val (style->rc_properties, i, key);
1422 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1425 new_property->origin = g_strdup (property->origin);
1426 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1427 g_value_copy (&property->value, &new_property->value);
1431 gtk_rc_style_real_merge (GtkRcStyle *dest,
1436 for (i = 0; i < 5; i++)
1438 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1439 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1441 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1442 src->color_flags[i] & GTK_RC_FG)
1444 dest->fg[i] = src->fg[i];
1445 dest->color_flags[i] |= GTK_RC_FG;
1447 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1448 src->color_flags[i] & GTK_RC_BG)
1450 dest->bg[i] = src->bg[i];
1451 dest->color_flags[i] |= GTK_RC_BG;
1453 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1454 src->color_flags[i] & GTK_RC_TEXT)
1456 dest->text[i] = src->text[i];
1457 dest->color_flags[i] |= GTK_RC_TEXT;
1459 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1460 src->color_flags[i] & GTK_RC_BASE)
1462 dest->base[i] = src->base[i];
1463 dest->color_flags[i] |= GTK_RC_BASE;
1467 if (dest->xthickness < 0 && src->xthickness >= 0)
1468 dest->xthickness = src->xthickness;
1469 if (dest->ythickness < 0 && src->ythickness >= 0)
1470 dest->ythickness = src->ythickness;
1474 if (!dest->font_desc)
1475 dest->font_desc = pango_font_description_copy (src->font_desc);
1477 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1480 if (src->rc_properties)
1484 for (i = 0; i < src->rc_properties->len; i++)
1485 insert_rc_property (dest,
1486 &g_array_index (src->rc_properties, GtkRcProperty, i),
1492 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1494 return gtk_style_new ();
1498 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1500 GtkIconFactory *factory = gtk_icon_factory_new ();
1502 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1506 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1508 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1509 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1511 (GDestroyNotify) gdk_color_free);
1513 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1517 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1518 GtkRcStyle *src_style)
1520 GSList *concat = g_slist_copy (src_style->icon_factories);
1522 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1524 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1528 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1529 GtkRcStyle *src_style)
1531 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1532 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1533 GSList *concat = g_slist_copy (src_priv->color_hashes);
1535 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1537 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1541 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1542 GtkRcStyle *src_style,
1543 GtkRcContext *context)
1545 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1549 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1551 /* Append src_style's factories, adding a ref to them */
1552 if (src_style->icon_factories != NULL)
1554 /* Add a factory for ourselves if we have none,
1555 * in case we end up defining more stock icons.
1556 * I see no real way around this; we need to maintain
1557 * the invariant that the first factory in the list
1558 * is always our_factory, the one belonging to us,
1559 * and if we put src_style factories in the list we can't
1560 * do that if the style is reopened.
1562 if (rc_style->icon_factories == NULL)
1563 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1565 gtk_rc_style_append_icon_factories (rc_style, src_style);
1568 /* Also append src_style's color hashes, adding a ref to them */
1569 if (src_priv->color_hashes != NULL)
1571 /* See comment above .. */
1572 if (priv->color_hashes == NULL)
1573 gtk_rc_style_prepend_empty_color_hash (rc_style);
1575 gtk_rc_style_append_color_hashes (rc_style, src_style);
1579 /* if we didn't get color hashes from the src_style, initialize
1580 * the list with the settings' color scheme (if it exists)
1582 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1584 gtk_rc_style_prepend_empty_color_hash (rc_style);
1586 priv->color_hashes = g_slist_append (priv->color_hashes,
1587 g_hash_table_ref (context->color_hash));
1592 gtk_rc_clear_hash_node (gpointer key,
1596 g_object_unref (data);
1600 gtk_rc_free_rc_sets (GSList *slist)
1606 rc_set = slist->data;
1607 gtk_rc_set_free (rc_set);
1609 slist = slist->next;
1614 gtk_rc_clear_styles (GtkRcContext *context)
1616 /* Clear out all old rc_styles */
1618 if (context->rc_style_ht)
1620 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1621 g_hash_table_destroy (context->rc_style_ht);
1622 context->rc_style_ht = NULL;
1625 gtk_rc_free_rc_sets (context->rc_sets_widget);
1626 g_slist_free (context->rc_sets_widget);
1627 context->rc_sets_widget = NULL;
1629 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1630 g_slist_free (context->rc_sets_widget_class);
1631 context->rc_sets_widget_class = NULL;
1633 gtk_rc_free_rc_sets (context->rc_sets_class);
1634 g_slist_free (context->rc_sets_class);
1635 context->rc_sets_class = NULL;
1638 /* Reset all our widgets. Also, we have to invalidate cached icons in
1639 * icon sets so they get re-rendered.
1642 gtk_rc_reset_widgets (GtkSettings *settings)
1644 GList *list, *toplevels;
1646 _gtk_icon_set_invalidate_caches ();
1648 toplevels = gtk_window_list_toplevels ();
1649 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1651 for (list = toplevels; list; list = list->next)
1653 if (gtk_widget_get_screen (list->data) == settings->screen)
1655 gtk_widget_reset_rc_styles (list->data);
1658 g_object_unref (list->data);
1660 g_list_free (toplevels);
1664 gtk_rc_clear_realized_style (gpointer key,
1668 GSList *rc_styles = key;
1669 GtkStyle *style = value;
1670 GSList *tmp_list = rc_styles;
1672 g_object_unref (style);
1676 GtkRcStyle *rc_style = tmp_list->data;
1678 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1680 tmp_list = tmp_list->next;
1683 g_slist_free (rc_styles);
1687 * gtk_rc_reset_styles:
1688 * @settings: a #GtkSettings
1690 * This function recomputes the styles for all widgets that use a
1691 * particular #GtkSettings object. (There is one #GtkSettings object
1692 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1693 * when some global parameter has changed that affects the appearance
1694 * of all widgets, because when a widget gets a new style, it will
1695 * both redraw and recompute any cached information about its
1696 * appearance. As an example, it is used when the default font size
1697 * set by the operating system changes. Note that this function
1698 * doesn't affect widgets that have a style set explicitely on them
1699 * with gtk_widget_set_style().
1704 gtk_rc_reset_styles (GtkSettings *settings)
1706 GtkRcContext *context;
1707 gboolean reset = FALSE;
1709 g_return_if_fail (GTK_IS_SETTINGS (settings));
1711 context = gtk_rc_context_get (settings);
1713 if (context->default_style)
1715 g_object_unref (context->default_style);
1716 context->default_style = NULL;
1720 /* Clear out styles that have been looked up already
1722 if (realized_style_ht)
1724 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1725 g_hash_table_destroy (realized_style_ht);
1726 realized_style_ht = NULL;
1731 gtk_rc_reset_widgets (settings);
1735 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1737 GtkRcContext *context;
1738 gchar *new_font_name;
1740 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1742 context = gtk_rc_context_get (settings);
1744 g_object_get (context->settings,
1745 "gtk-font-name", &new_font_name,
1748 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1750 g_free (context->font_name);
1751 context->font_name = g_strdup (new_font_name);
1753 gtk_rc_reset_styles (settings);
1756 g_free (new_font_name);
1758 return context->font_name;
1762 * gtk_rc_reparse_all_for_settings:
1763 * @settings: a #GtkSettings
1764 * @force_load: load whether or not anything changed
1766 * If the modification time on any previously read file
1767 * for the given #GtkSettings has changed, discard all style information
1768 * and then reread all previously read RC files.
1770 * Return value: %TRUE if the files were reread.
1773 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1774 gboolean force_load)
1776 gboolean mtime_modified = FALSE;
1779 GtkRcContext *context;
1780 struct stat statbuf;
1782 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1784 context = gtk_rc_context_get (settings);
1786 if (context->reloading)
1791 /* Check through and see if any of the RC's have had their
1792 * mtime modified. If so, reparse everything.
1794 tmp_list = context->rc_files;
1797 rc_file = tmp_list->data;
1799 if (!rc_file->is_string)
1801 if (!g_lstat (rc_file->name, &statbuf) &&
1802 (statbuf.st_mtime != rc_file->mtime))
1804 mtime_modified = TRUE;
1809 tmp_list = tmp_list->next;
1813 if (force_load || mtime_modified)
1815 _gtk_binding_reset_parsed ();
1816 gtk_rc_clear_styles (context);
1817 context->reloading = TRUE;
1819 _gtk_settings_reset_rc_values (context->settings);
1820 gtk_rc_clear_rc_files (context);
1822 gtk_rc_parse_default_files (context);
1824 tmp_list = global_rc_files;
1827 rc_file = tmp_list->data;
1829 if (rc_file->is_string)
1830 gtk_rc_context_parse_string (context, rc_file->name);
1832 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1834 tmp_list = tmp_list->next;
1837 g_free (context->theme_name);
1838 g_free (context->key_theme_name);
1840 g_object_get (context->settings,
1841 "gtk-theme-name", &context->theme_name,
1842 "gtk-key-theme-name", &context->key_theme_name,
1843 "gtk-application-prefer-dark-theme", &context->prefer_dark_theme,
1846 if (context->theme_name && context->theme_name[0])
1848 if (context->prefer_dark_theme)
1850 if (!gtk_rc_parse_named (context, context->theme_name, NULL, "-dark"))
1851 gtk_rc_parse_named (context, context->theme_name, NULL, NULL);
1855 gtk_rc_parse_named (context, context->theme_name, NULL, NULL);
1858 if (context->key_theme_name && context->key_theme_name[0])
1859 gtk_rc_parse_named (context, context->key_theme_name, "key", NULL);
1861 context->reloading = FALSE;
1863 gtk_rc_reset_widgets (context->settings);
1866 return force_load || mtime_modified;
1870 * gtk_rc_reparse_all:
1872 * If the modification time on any previously read file for the
1873 * default #GtkSettings has changed, discard all style information
1874 * and then reread all previously read RC files.
1876 * Return value: %TRUE if the files were reread.
1879 gtk_rc_reparse_all (void)
1882 gboolean result = FALSE;
1884 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1886 GtkRcContext *context = tmp_list->data;
1887 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1895 gtk_rc_styles_match (GSList *rc_styles,
1899 gchar *path_reversed)
1906 rc_set = sets->data;
1909 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1911 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1912 rc_styles = g_slist_append (rc_styles, rc_set);
1916 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1917 rc_styles = g_slist_append (rc_styles, rc_set);
1925 rc_set_compare (gconstpointer a, gconstpointer b)
1927 const GtkRcSet *set_a = a;
1928 const GtkRcSet *set_b = b;
1930 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1934 sort_and_dereference_sets (GSList *styles)
1938 /* At this point, the list of sets is ordered by:
1940 * a) 'widget' patterns are earlier than 'widget_class' patterns
1941 * which are ealier than 'class' patterns.
1942 * a) For two matches for class patterns, a match to a child type
1943 * is before a match to a parent type
1944 * c) a match later in the RC file (or in a later RC file) is before a
1945 * match earlier in the RC file.
1947 * With a) taking precedence over b) which takes precendence over c).
1949 * Now sort by priority, which has the highest precendence for sort order
1951 styles = g_slist_sort (styles, rc_set_compare);
1953 /* Make styles->data = styles->data->rc_style
1958 GtkRcSet *set = tmp_list->data;
1959 tmp_list->data = set->rc_style;
1960 tmp_list = tmp_list->next;
1968 * @widget: a #GtkWidget
1970 * Finds all matching RC styles for a given widget,
1971 * composites them together, and then creates a
1972 * #GtkStyle representing the composite appearance.
1973 * (GTK+ actually keeps a cache of previously
1974 * created styles, so a new style may not be
1977 * Returns: the resulting style. No refcount is added
1978 * to the returned style, so if you want to save this
1979 * style around, you should add a reference yourself.
1982 gtk_rc_get_style (GtkWidget *widget)
1984 GtkRcStyle *widget_rc_style;
1985 GSList *rc_styles = NULL;
1986 GtkRcContext *context;
1988 static guint rc_style_key_id = 0;
1990 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1992 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1994 /* We allow the specification of a single rc style to be bound
1995 * tightly to a widget, for application modifications
1997 if (!rc_style_key_id)
1998 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
2000 if (context->rc_sets_widget)
2002 gchar *path, *path_reversed;
2005 gtk_widget_path (widget, &path_length, &path, &path_reversed);
2006 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2008 g_free (path_reversed);
2011 if (context->rc_sets_widget_class)
2013 gchar *path, *path_reversed;
2016 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
2017 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2019 g_free (path_reversed);
2022 if (context->rc_sets_class)
2026 type = G_TYPE_FROM_INSTANCE (widget);
2030 gchar *path_reversed;
2033 path = g_strdup (g_type_name (type));
2034 path_length = strlen (path);
2035 path_reversed = g_strdup (path);
2036 g_strreverse (path_reversed);
2038 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2040 g_free (path_reversed);
2042 type = g_type_parent (type);
2046 rc_styles = sort_and_dereference_sets (rc_styles);
2048 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
2050 if (widget_rc_style)
2051 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
2054 return gtk_rc_init_style (context, rc_styles);
2057 if (!context->default_style)
2059 context->default_style = gtk_style_new ();
2060 _gtk_style_init_for_settings (context->default_style, context->settings);
2063 return context->default_style;
2068 * gtk_rc_get_style_by_paths:
2069 * @settings: a #GtkSettings object
2070 * @widget_path: (allow-none): the widget path to use when looking up the style, or %NULL
2071 * if no matching against the widget path should be done
2072 * @class_path: (allow-none): the class path to use when looking up the style, or %NULL
2073 * if no matching against the class path should be done.
2074 * @type: a type that will be used along with parent types of this type
2075 * when matching against class styles, or #G_TYPE_NONE
2077 * Creates up a #GtkStyle from styles defined in a RC file by providing
2078 * the raw components used in matching. This function may be useful
2079 * when creating pseudo-widgets that should be themed like widgets but
2080 * don't actually have corresponding GTK+ widgets. An example of this
2081 * would be items inside a GNOME canvas widget.
2083 * The action of gtk_rc_get_style() is similar to:
2085 * gtk_widget_path (widget, NULL, &path, NULL);
2086 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2087 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
2089 * G_OBJECT_TYPE (widget));
2092 * Return value: A style created by matching with the supplied paths,
2093 * or %NULL if nothing matching was specified and the default style should
2094 * be used. The returned value is owned by GTK+ as part of an internal cache,
2095 * so you must call g_object_ref() on the returned value if you want to
2096 * keep a reference to it.
2099 gtk_rc_get_style_by_paths (GtkSettings *settings,
2100 const char *widget_path,
2101 const char *class_path,
2104 /* We duplicate the code from above to avoid slowing down the above
2105 * by generating paths when we don't need them. I don't know if
2106 * this is really worth it.
2108 GSList *rc_styles = NULL;
2109 GtkRcContext *context;
2111 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2113 context = gtk_rc_context_get (settings);
2115 if (widget_path && context->rc_sets_widget)
2118 gchar *path_reversed;
2121 path_length = strlen (widget_path);
2122 path = g_strdup (widget_path);
2123 path_reversed = g_strdup (widget_path);
2124 g_strreverse (path_reversed);
2126 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2128 g_free (path_reversed);
2131 if (class_path && context->rc_sets_widget_class)
2134 gchar *path_reversed;
2137 path = g_strdup (class_path);
2138 path_length = strlen (class_path);
2139 path_reversed = g_strdup (class_path);
2140 g_strreverse (path_reversed);
2142 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2144 g_free (path_reversed);
2147 if (type != G_TYPE_NONE && context->rc_sets_class)
2152 gchar *path_reversed;
2155 path = g_strdup (g_type_name (type));
2156 path_length = strlen (path);
2157 path_reversed = g_strdup (path);
2158 g_strreverse (path_reversed);
2160 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2162 g_free (path_reversed);
2164 type = g_type_parent (type);
2168 rc_styles = sort_and_dereference_sets (rc_styles);
2171 return gtk_rc_init_style (context, rc_styles);
2177 gtk_rc_scanner_new (void)
2179 return g_scanner_new (>k_rc_scanner_config);
2183 gtk_rc_parse_any (GtkRcContext *context,
2184 const gchar *input_name,
2186 const gchar *input_string)
2192 scanner = gtk_rc_scanner_new ();
2196 g_assert (input_string == NULL);
2198 g_scanner_input_file (scanner, input_fd);
2202 g_assert (input_string != NULL);
2204 g_scanner_input_text (scanner, input_string, strlen (input_string));
2206 scanner->input_name = input_name;
2208 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2209 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2213 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2217 guint expected_token;
2219 expected_token = gtk_rc_parse_statement (context, scanner);
2221 if (expected_token != G_TOKEN_NONE)
2223 const gchar *symbol_name = NULL;
2226 if (scanner->scope_id == 0)
2228 /* if we are in scope 0, we know the symbol names
2229 * that are associated with certain token values.
2230 * so we look them up to make the error messages
2233 if (expected_token > GTK_RC_TOKEN_INVALID &&
2234 expected_token < GTK_RC_TOKEN_LAST)
2236 const gchar *sym = NULL;
2238 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2239 if (symbols[i].token == expected_token)
2240 sym = symbol_names + symbols[i].name_offset;
2243 msg = g_strconcat ("e.g. `", sym, "'", NULL);
2246 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2247 scanner->token < GTK_RC_TOKEN_LAST)
2249 symbol_name = "???";
2250 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2251 if (symbols[i].token == scanner->token)
2252 symbol_name = symbol_names + symbols[i].name_offset;
2256 g_scanner_unexp_token (scanner,
2269 g_scanner_destroy (scanner);
2273 gtk_rc_styles_hash (const GSList *rc_styles)
2280 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2281 rc_styles = rc_styles->next;
2288 gtk_rc_styles_equal (const GSList *a,
2293 if (a->data != b->data)
2303 gtk_rc_style_hash (const gchar *name)
2309 result += (result << 3) + *name++;
2315 gtk_rc_style_equal (const gchar *a,
2318 return (strcmp (a, b) == 0);
2322 gtk_rc_style_find (GtkRcContext *context,
2325 if (context->rc_style_ht)
2326 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2332 gtk_rc_style_to_style (GtkRcContext *context,
2333 GtkRcStyle *rc_style)
2337 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2338 _gtk_style_init_for_settings (style, context->settings);
2340 style->rc_style = g_object_ref (rc_style);
2342 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2347 /* Reuses or frees rc_styles */
2349 gtk_rc_init_style (GtkRcContext *context,
2352 GtkStyle *style = NULL;
2355 g_return_val_if_fail (rc_styles != NULL, NULL);
2357 if (!realized_style_ht)
2358 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2359 (GEqualFunc) gtk_rc_styles_equal);
2361 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2365 GtkRcStyle *base_style = NULL;
2366 GtkRcStyle *proto_style;
2367 GtkRcStyleClass *proto_style_class;
2369 GType rc_style_type = GTK_TYPE_RC_STYLE;
2371 /* Find the first style where the RC file specified engine "" {}
2372 * or the first derived style and use that to create the
2373 * merged style. If we only have raw GtkRcStyles, use the first
2374 * style to create the merged style.
2376 base_style = rc_styles->data;
2377 tmp_styles = rc_styles;
2380 GtkRcStyle *rc_style = tmp_styles->data;
2382 if (rc_style->engine_specified ||
2383 G_OBJECT_TYPE (rc_style) != rc_style_type)
2385 base_style = rc_style;
2389 tmp_styles = tmp_styles->next;
2392 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2393 proto_style = proto_style_class->create_rc_style (base_style);
2395 tmp_styles = rc_styles;
2398 GtkRcStyle *rc_style = tmp_styles->data;
2400 proto_style_class->merge (proto_style, rc_style);
2402 /* Point from each rc_style to the list of styles */
2403 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2404 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2406 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2407 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2409 tmp_styles = tmp_styles->next;
2412 for (i = 0; i < 5; i++)
2413 if (proto_style->bg_pixmap_name[i] &&
2414 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2416 g_free (proto_style->bg_pixmap_name[i]);
2417 proto_style->bg_pixmap_name[i] = NULL;
2420 style = gtk_rc_style_to_style (context, proto_style);
2421 g_object_unref (proto_style);
2423 g_hash_table_insert (realized_style_ht, rc_styles, style);
2426 g_slist_free (rc_styles);
2431 /*********************
2432 * Parsing functions *
2433 *********************/
2436 lookup_color (GtkRcStyle *style,
2437 const char *color_name,
2440 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2443 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2445 GHashTable *hash = iter->data;
2446 GdkColor *match = g_hash_table_lookup (hash, color_name);
2450 color->red = match->red;
2451 color->green = match->green;
2452 color->blue = match->blue;
2461 rc_parse_token_or_compound (GScanner *scanner,
2464 GTokenType delimiter)
2466 guint token = g_scanner_get_next_token (scanner);
2468 /* we either scan a single token (skipping comments)
2469 * or a compund statement.
2470 * compunds are enclosed in (), [] or {} braces, we read
2471 * them in via deep recursion.
2478 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2481 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2483 case G_TOKEN_STRING:
2484 string = g_strescape (scanner->value.v_string, NULL);
2485 g_string_append (gstring, " \"");
2486 g_string_append (gstring, string);
2487 g_string_append_c (gstring, '"');
2490 case G_TOKEN_IDENTIFIER:
2491 g_string_append_c (gstring, ' ');
2492 g_string_append (gstring, scanner->value.v_identifier);
2494 case G_TOKEN_COMMENT_SINGLE:
2495 case G_TOKEN_COMMENT_MULTI:
2496 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2497 case G_TOKEN_LEFT_PAREN:
2498 g_string_append_c (gstring, ' ');
2499 g_string_append_c (gstring, token);
2500 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2501 if (token != G_TOKEN_NONE)
2504 case G_TOKEN_LEFT_CURLY:
2505 g_string_append_c (gstring, ' ');
2506 g_string_append_c (gstring, token);
2507 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2508 if (token != G_TOKEN_NONE)
2511 case G_TOKEN_LEFT_BRACE:
2512 g_string_append_c (gstring, ' ');
2513 g_string_append_c (gstring, token);
2514 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2515 if (token != G_TOKEN_NONE)
2519 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2522 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2523 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2524 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2526 g_scanner_get_next_token (scanner);
2528 if (!style || !lookup_color (style, scanner->value.v_identifier,
2531 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2532 scanner->value.v_identifier);
2533 return G_TOKEN_IDENTIFIER;
2537 g_string_append_printf (gstring, " { %s, %s, %s }",
2538 g_ascii_formatd (rbuf, sizeof (rbuf),
2540 color.red / 65535.0),
2541 g_ascii_formatd (gbuf, sizeof (gbuf),
2543 color.green / 65535.0),
2544 g_ascii_formatd (bbuf, sizeof (bbuf),
2546 color.blue / 65535.0));
2550 return G_TOKEN_IDENTIFIER;
2552 if (token >= 256 || token < 1)
2553 return delimiter ? delimiter : G_TOKEN_STRING;
2554 g_string_append_c (gstring, ' ');
2555 g_string_append_c (gstring, token);
2556 if (token == delimiter)
2557 return G_TOKEN_NONE;
2561 return G_TOKEN_NONE;
2563 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2567 gtk_rc_parse_assignment (GScanner *scanner,
2569 GtkRcProperty *prop)
2571 #define MY_SCAN_IDENTIFIER TRUE
2572 #define MY_SCAN_SYMBOLS FALSE
2573 #define MY_IDENTIFIER_2_STRING FALSE
2574 #define MY_CHAR_2_TOKEN TRUE
2575 #define MY_SCAN_IDENTIFIER_NULL FALSE
2576 #define MY_NUMBERS_2_INT TRUE
2578 gboolean scan_identifier = scanner->config->scan_identifier;
2579 gboolean scan_symbols = scanner->config->scan_symbols;
2580 gboolean identifier_2_string = scanner->config->identifier_2_string;
2581 gboolean char_2_token = scanner->config->char_2_token;
2582 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2583 gboolean numbers_2_int = scanner->config->numbers_2_int;
2584 gboolean negate = FALSE;
2585 gboolean is_color = FALSE;
2588 /* check that this is an assignment */
2589 if (g_scanner_get_next_token (scanner) != '=')
2592 /* adjust scanner mode */
2593 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2594 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2595 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2596 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2597 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2598 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2600 /* record location */
2601 if (g_getenv ("GTK_DEBUG"))
2602 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2604 prop->origin = NULL;
2606 /* parse optional symbolic color prefix */
2607 if (g_scanner_peek_next_token (scanner) == '@')
2609 g_scanner_get_next_token (scanner); /* eat color prefix */
2613 /* parse optional sign */
2614 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2616 g_scanner_get_next_token (scanner); /* eat sign */
2620 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2623 token = g_scanner_peek_next_token (scanner);
2625 if (is_color && token != G_TOKEN_IDENTIFIER)
2627 token = G_TOKEN_IDENTIFIER;
2634 g_scanner_get_next_token (scanner);
2635 g_value_init (&prop->value, G_TYPE_LONG);
2636 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2637 token = G_TOKEN_NONE;
2640 g_scanner_get_next_token (scanner);
2641 g_value_init (&prop->value, G_TYPE_DOUBLE);
2642 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2643 token = G_TOKEN_NONE;
2645 case G_TOKEN_STRING:
2646 g_scanner_get_next_token (scanner);
2648 token = G_TOKEN_INT;
2651 g_value_init (&prop->value, G_TYPE_STRING);
2652 g_value_set_string (&prop->value, scanner->value.v_string);
2653 token = G_TOKEN_NONE;
2656 case G_TOKEN_IDENTIFIER:
2660 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2661 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2662 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2665 g_scanner_get_next_token (scanner);
2667 if (!style || !lookup_color (style, scanner->value.v_identifier,
2670 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2671 scanner->value.v_identifier);
2672 token = G_TOKEN_IDENTIFIER;
2676 gstring = g_string_new (NULL);
2678 g_string_append_printf (gstring, " { %s, %s, %s }",
2679 g_ascii_formatd (rbuf, sizeof (rbuf),
2681 color.red / 65535.0),
2682 g_ascii_formatd (gbuf, sizeof (gbuf),
2684 color.green / 65535.0),
2685 g_ascii_formatd (bbuf, sizeof (bbuf),
2687 color.blue / 65535.0));
2689 g_value_init (&prop->value, G_TYPE_GSTRING);
2690 g_value_take_boxed (&prop->value, gstring);
2691 token = G_TOKEN_NONE;
2695 case G_TOKEN_LEFT_PAREN:
2696 case G_TOKEN_LEFT_CURLY:
2697 case G_TOKEN_LEFT_BRACE:
2700 GString *gstring = g_string_new (NULL);
2701 gboolean parse_on = TRUE;
2703 /* allow identifier(foobar) to support color expressions */
2704 if (token == G_TOKEN_IDENTIFIER)
2706 g_scanner_get_next_token (scanner);
2708 g_string_append_c (gstring, ' ');
2709 g_string_append (gstring, scanner->value.v_identifier);
2711 /* temporarily reset scanner mode to default, so we
2712 * don't peek the next token in a mode that only makes
2713 * sense in this function; because if anything but
2714 * G_TOKEN_LEFT_PAREN follows, the next token will be
2715 * parsed by our caller.
2717 * FIXME: right fix would be to call g_scanner_unget()
2718 * but that doesn't exist
2720 scanner->config->scan_identifier = scan_identifier;
2721 scanner->config->scan_symbols = scan_symbols;
2722 scanner->config->identifier_2_string = identifier_2_string;
2723 scanner->config->char_2_token = char_2_token;
2724 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2725 scanner->config->numbers_2_int = numbers_2_int;
2727 token = g_scanner_peek_next_token (scanner);
2729 /* restore adjusted scanner mode */
2730 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2731 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2732 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2733 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2734 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2735 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2737 if (token != G_TOKEN_LEFT_PAREN)
2739 token = G_TOKEN_NONE;
2745 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2747 if (token == G_TOKEN_NONE)
2749 g_string_append_c (gstring, ' ');
2750 g_value_init (&prop->value, G_TYPE_GSTRING);
2751 g_value_take_boxed (&prop->value, gstring);
2754 g_string_free (gstring, TRUE);
2759 g_scanner_get_next_token (scanner);
2760 token = G_TOKEN_INT;
2766 /* restore scanner mode */
2767 scanner->config->scan_identifier = scan_identifier;
2768 scanner->config->scan_symbols = scan_symbols;
2769 scanner->config->identifier_2_string = identifier_2_string;
2770 scanner->config->char_2_token = char_2_token;
2771 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2772 scanner->config->numbers_2_int = numbers_2_int;
2778 is_c_identifier (const gchar *string)
2781 gboolean is_varname;
2783 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2784 for (p = string + 1; *p && is_varname; p++)
2785 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2791 parse_include_file (GtkRcContext *context,
2793 const gchar *filename)
2795 char *to_parse = NULL;
2797 if (g_path_is_absolute (filename))
2799 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2800 * don't print an error in this case.
2802 to_parse = g_strdup (filename);
2806 /* if a relative path, we look relative to all the RC files in the
2807 * include stack. We require the file to be found in this case
2808 * so we can give meaningful error messages, and because on reparsing
2809 * non-absolute paths don't make sense.
2811 GSList *tmp_list = current_files_stack;
2814 GtkRcFile *curfile = tmp_list->data;
2815 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2817 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2825 tmp_list = tmp_list->next;
2831 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2836 g_scanner_warn (scanner,
2837 _("Unable to find include file: \"%s\""),
2844 gtk_rc_parse_statement (GtkRcContext *context,
2849 token = g_scanner_peek_next_token (scanner);
2852 case GTK_RC_TOKEN_INCLUDE:
2853 token = g_scanner_get_next_token (scanner);
2854 if (token != GTK_RC_TOKEN_INCLUDE)
2855 return GTK_RC_TOKEN_INCLUDE;
2856 token = g_scanner_get_next_token (scanner);
2857 if (token != G_TOKEN_STRING)
2858 return G_TOKEN_STRING;
2859 parse_include_file (context, scanner, scanner->value.v_string);
2860 return G_TOKEN_NONE;
2862 case GTK_RC_TOKEN_STYLE:
2863 return gtk_rc_parse_style (context, scanner);
2865 case GTK_RC_TOKEN_BINDING:
2866 return _gtk_binding_parse_binding (scanner);
2868 case GTK_RC_TOKEN_PIXMAP_PATH:
2869 return gtk_rc_parse_pixmap_path (context, scanner);
2871 case GTK_RC_TOKEN_WIDGET:
2872 return gtk_rc_parse_path_pattern (context, scanner);
2874 case GTK_RC_TOKEN_WIDGET_CLASS:
2875 return gtk_rc_parse_path_pattern (context, scanner);
2877 case GTK_RC_TOKEN_CLASS:
2878 return gtk_rc_parse_path_pattern (context, scanner);
2880 case GTK_RC_TOKEN_MODULE_PATH:
2881 return gtk_rc_parse_module_path (scanner);
2883 case GTK_RC_TOKEN_IM_MODULE_FILE:
2884 return gtk_rc_parse_im_module_file (scanner);
2886 case G_TOKEN_IDENTIFIER:
2887 if (is_c_identifier (scanner->next_value.v_identifier))
2889 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2892 g_scanner_get_next_token (scanner); /* eat identifier */
2893 name = g_strdup (scanner->value.v_identifier);
2895 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2896 if (token == G_TOKEN_NONE)
2898 GtkSettingsValue svalue;
2900 svalue.origin = prop.origin;
2901 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2902 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2903 _gtk_settings_set_property_value_from_rc (context->settings,
2907 g_free (prop.origin);
2908 if (G_VALUE_TYPE (&prop.value))
2909 g_value_unset (&prop.value);
2916 g_scanner_get_next_token (scanner);
2917 return G_TOKEN_IDENTIFIER;
2920 g_scanner_get_next_token (scanner);
2921 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2926 fixup_rc_set (GSList *list,
2932 GtkRcSet *set = list->data;
2933 if (set->rc_style == orig)
2934 set->rc_style = new;
2940 fixup_rc_sets (GtkRcContext *context,
2944 fixup_rc_set (context->rc_sets_widget, orig, new);
2945 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2946 fixup_rc_set (context->rc_sets_class, orig, new);
2950 gtk_rc_parse_style (GtkRcContext *context,
2953 GtkRcStyle *rc_style;
2954 GtkRcStyle *orig_style;
2955 GtkRcStyle *parent_style = NULL;
2956 GtkRcStylePrivate *rc_priv = NULL;
2959 GtkIconFactory *our_factory = NULL;
2960 GHashTable *our_hash = NULL;
2962 token = g_scanner_get_next_token (scanner);
2963 if (token != GTK_RC_TOKEN_STYLE)
2964 return GTK_RC_TOKEN_STYLE;
2966 token = g_scanner_get_next_token (scanner);
2967 if (token != G_TOKEN_STRING)
2968 return G_TOKEN_STRING;
2970 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2972 orig_style = g_object_ref (rc_style);
2978 rc_style = gtk_rc_style_new ();
2979 rc_style->name = g_strdup (scanner->value.v_string);
2981 for (i = 0; i < 5; i++)
2982 rc_style->bg_pixmap_name[i] = NULL;
2984 for (i = 0; i < 5; i++)
2985 rc_style->color_flags[i] = 0;
2988 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
2990 /* If there's a list, its first member is always the factory belonging
2993 if (rc_style->icon_factories)
2994 our_factory = rc_style->icon_factories->data;
2995 if (rc_priv->color_hashes)
2996 our_hash = rc_priv->color_hashes->data;
2998 token = g_scanner_peek_next_token (scanner);
2999 if (token == G_TOKEN_EQUAL_SIGN)
3001 token = g_scanner_get_next_token (scanner);
3003 token = g_scanner_get_next_token (scanner);
3004 if (token != G_TOKEN_STRING)
3006 token = G_TOKEN_STRING;
3010 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3013 for (i = 0; i < 5; i++)
3015 rc_style->color_flags[i] = parent_style->color_flags[i];
3016 rc_style->fg[i] = parent_style->fg[i];
3017 rc_style->bg[i] = parent_style->bg[i];
3018 rc_style->text[i] = parent_style->text[i];
3019 rc_style->base[i] = parent_style->base[i];
3022 rc_style->xthickness = parent_style->xthickness;
3023 rc_style->ythickness = parent_style->ythickness;
3025 if (parent_style->font_desc)
3027 if (rc_style->font_desc)
3028 pango_font_description_free (rc_style->font_desc);
3029 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3032 if (parent_style->rc_properties)
3036 for (i = 0; i < parent_style->rc_properties->len; i++)
3037 insert_rc_property (rc_style,
3038 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3042 for (i = 0; i < 5; i++)
3044 g_free (rc_style->bg_pixmap_name[i]);
3045 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3050 /* get icon_factories and color_hashes from the parent style;
3051 * if the parent_style doesn't have color_hashes, initializes
3052 * the color_hashes with the settings' color scheme (if it exists)
3054 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3056 if (rc_style->icon_factories)
3057 our_factory = rc_style->icon_factories->data;
3058 if (rc_priv->color_hashes)
3059 our_hash = rc_priv->color_hashes->data;
3061 token = g_scanner_get_next_token (scanner);
3062 if (token != G_TOKEN_LEFT_CURLY)
3064 token = G_TOKEN_LEFT_CURLY;
3068 token = g_scanner_peek_next_token (scanner);
3069 while (token != G_TOKEN_RIGHT_CURLY)
3073 case GTK_RC_TOKEN_BG:
3074 token = gtk_rc_parse_bg (scanner, rc_style);
3076 case GTK_RC_TOKEN_FG:
3077 token = gtk_rc_parse_fg (scanner, rc_style);
3079 case GTK_RC_TOKEN_TEXT:
3080 token = gtk_rc_parse_text (scanner, rc_style);
3082 case GTK_RC_TOKEN_BASE:
3083 token = gtk_rc_parse_base (scanner, rc_style);
3085 case GTK_RC_TOKEN_XTHICKNESS:
3086 token = gtk_rc_parse_xthickness (scanner, rc_style);
3088 case GTK_RC_TOKEN_YTHICKNESS:
3089 token = gtk_rc_parse_ythickness (scanner, rc_style);
3091 case GTK_RC_TOKEN_BG_PIXMAP:
3092 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3094 case GTK_RC_TOKEN_FONT:
3095 token = gtk_rc_parse_font (scanner, rc_style);
3097 case GTK_RC_TOKEN_FONTSET:
3098 token = gtk_rc_parse_fontset (scanner, rc_style);
3100 case GTK_RC_TOKEN_FONT_NAME:
3101 token = gtk_rc_parse_font_name (scanner, rc_style);
3103 case GTK_RC_TOKEN_ENGINE:
3104 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3106 case GTK_RC_TOKEN_STOCK:
3107 if (our_factory == NULL)
3108 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3109 our_factory = rc_style->icon_factories->data;
3110 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3112 case GTK_RC_TOKEN_COLOR:
3113 if (our_hash == NULL)
3115 gtk_rc_style_prepend_empty_color_hash (rc_style);
3116 our_hash = rc_priv->color_hashes->data;
3118 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3120 case G_TOKEN_IDENTIFIER:
3121 if (is_c_identifier (scanner->next_value.v_identifier))
3123 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3126 g_scanner_get_next_token (scanner); /* eat type name */
3127 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3128 if (g_scanner_get_next_token (scanner) != ':' ||
3129 g_scanner_get_next_token (scanner) != ':')
3134 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3135 !is_c_identifier (scanner->value.v_identifier))
3137 token = G_TOKEN_IDENTIFIER;
3141 /* it's important that we do the same canonification as GParamSpecPool here */
3142 name = g_strdup (scanner->value.v_identifier);
3143 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3144 prop.property_name = g_quark_from_string (name);
3147 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3148 if (token == G_TOKEN_NONE)
3150 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3151 insert_rc_property (rc_style, &prop, TRUE);
3154 g_free (prop.origin);
3155 if (G_VALUE_TYPE (&prop.value))
3156 g_value_unset (&prop.value);
3160 g_scanner_get_next_token (scanner);
3161 token = G_TOKEN_IDENTIFIER;
3165 g_scanner_get_next_token (scanner);
3166 token = G_TOKEN_RIGHT_CURLY;
3170 if (token != G_TOKEN_NONE)
3173 token = g_scanner_peek_next_token (scanner);
3174 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3176 token = g_scanner_get_next_token (scanner);
3177 if (token != G_TOKEN_RIGHT_CURLY)
3179 token = G_TOKEN_RIGHT_CURLY;
3183 if (rc_style != orig_style)
3185 if (!context->rc_style_ht)
3186 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3187 (GEqualFunc) gtk_rc_style_equal);
3189 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3191 /* If we copied the data into a new rc style, fix up references to the old rc style
3192 * in bindings that we have.
3195 fixup_rc_sets (context, orig_style, rc_style);
3199 g_object_unref (orig_style);
3201 return G_TOKEN_NONE;
3204 if (rc_style != orig_style)
3205 g_object_unref (rc_style);
3208 g_object_unref (orig_style);
3213 const GtkRcProperty*
3214 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3216 GQuark property_name)
3218 GtkRcProperty *node = NULL;
3220 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3222 if (rc_style->rc_properties)
3226 key.type_name = type_name;
3227 key.property_name = property_name;
3229 node = bsearch (&key,
3230 rc_style->rc_properties->data, rc_style->rc_properties->len,
3231 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3238 gtk_rc_parse_bg (GScanner *scanner,
3244 token = g_scanner_get_next_token (scanner);
3245 if (token != GTK_RC_TOKEN_BG)
3246 return GTK_RC_TOKEN_BG;
3248 token = gtk_rc_parse_state (scanner, &state);
3249 if (token != G_TOKEN_NONE)
3252 token = g_scanner_get_next_token (scanner);
3253 if (token != G_TOKEN_EQUAL_SIGN)
3254 return G_TOKEN_EQUAL_SIGN;
3256 style->color_flags[state] |= GTK_RC_BG;
3257 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3261 gtk_rc_parse_fg (GScanner *scanner,
3267 token = g_scanner_get_next_token (scanner);
3268 if (token != GTK_RC_TOKEN_FG)
3269 return GTK_RC_TOKEN_FG;
3271 token = gtk_rc_parse_state (scanner, &state);
3272 if (token != G_TOKEN_NONE)
3275 token = g_scanner_get_next_token (scanner);
3276 if (token != G_TOKEN_EQUAL_SIGN)
3277 return G_TOKEN_EQUAL_SIGN;
3279 style->color_flags[state] |= GTK_RC_FG;
3280 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3284 gtk_rc_parse_text (GScanner *scanner,
3290 token = g_scanner_get_next_token (scanner);
3291 if (token != GTK_RC_TOKEN_TEXT)
3292 return GTK_RC_TOKEN_TEXT;
3294 token = gtk_rc_parse_state (scanner, &state);
3295 if (token != G_TOKEN_NONE)
3298 token = g_scanner_get_next_token (scanner);
3299 if (token != G_TOKEN_EQUAL_SIGN)
3300 return G_TOKEN_EQUAL_SIGN;
3302 style->color_flags[state] |= GTK_RC_TEXT;
3303 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3307 gtk_rc_parse_base (GScanner *scanner,
3313 token = g_scanner_get_next_token (scanner);
3314 if (token != GTK_RC_TOKEN_BASE)
3315 return GTK_RC_TOKEN_BASE;
3317 token = gtk_rc_parse_state (scanner, &state);
3318 if (token != G_TOKEN_NONE)
3321 token = g_scanner_get_next_token (scanner);
3322 if (token != G_TOKEN_EQUAL_SIGN)
3323 return G_TOKEN_EQUAL_SIGN;
3325 style->color_flags[state] |= GTK_RC_BASE;
3326 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3330 gtk_rc_parse_xthickness (GScanner *scanner,
3333 if (g_scanner_get_next_token (scanner) != 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,
3351 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3352 return GTK_RC_TOKEN_YTHICKNESS;
3354 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3355 return G_TOKEN_EQUAL_SIGN;
3357 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3360 style->ythickness = scanner->value.v_int;
3362 return G_TOKEN_NONE;
3366 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3368 GtkRcStyle *rc_style)
3374 token = g_scanner_get_next_token (scanner);
3375 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3376 return GTK_RC_TOKEN_BG_PIXMAP;
3378 token = gtk_rc_parse_state (scanner, &state);
3379 if (token != G_TOKEN_NONE)
3382 token = g_scanner_get_next_token (scanner);
3383 if (token != G_TOKEN_EQUAL_SIGN)
3384 return G_TOKEN_EQUAL_SIGN;
3386 token = g_scanner_get_next_token (scanner);
3387 if (token != G_TOKEN_STRING)
3388 return G_TOKEN_STRING;
3390 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3391 (strcmp (scanner->value.v_string, "<none>") == 0))
3392 pixmap_file = g_strdup (scanner->value.v_string);
3394 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3395 scanner, scanner->value.v_string);
3399 g_free (rc_style->bg_pixmap_name[state]);
3400 rc_style->bg_pixmap_name[state] = pixmap_file;
3403 return G_TOKEN_NONE;
3407 gtk_rc_check_pixmap_dir (const gchar *dir,
3408 const gchar *pixmap_file)
3412 buf = g_build_filename (dir, pixmap_file, NULL);
3414 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3423 * gtk_rc_find_pixmap_in_path:
3424 * @settings: a #GtkSettings
3425 * @scanner: Scanner used to get line number information for the
3426 * warning message, or %NULL
3427 * @pixmap_file: name of the pixmap file to locate.
3429 * Looks up a file in pixmap path for the specified #GtkSettings.
3430 * If the file is not found, it outputs a warning message using
3431 * g_warning() and returns %NULL.
3433 * Return value: the filename.
3436 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3438 const gchar *pixmap_file)
3444 GtkRcContext *context = gtk_rc_context_get (settings);
3446 if (context->pixmap_path)
3447 for (i = 0; context->pixmap_path[i] != NULL; i++)
3449 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3454 tmp_list = current_files_stack;
3457 GtkRcFile *curfile = tmp_list->data;
3458 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3462 tmp_list = tmp_list->next;
3466 g_scanner_warn (scanner,
3467 _("Unable to locate image file in pixmap_path: \"%s\""),
3470 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3477 * gtk_rc_find_module_in_path:
3478 * @module_file: name of a theme engine
3480 * Searches for a theme engine in the GTK+ search path. This function
3481 * is not useful for applications and should not be used.
3483 * Return value: The filename, if found (must be freed with g_free()),
3487 gtk_rc_find_module_in_path (const gchar *module_file)
3489 return _gtk_find_module (module_file, "engines");
3493 gtk_rc_parse_font (GScanner *scanner,
3494 GtkRcStyle *rc_style)
3498 token = g_scanner_get_next_token (scanner);
3499 if (token != GTK_RC_TOKEN_FONT)
3500 return GTK_RC_TOKEN_FONT;
3502 token = g_scanner_get_next_token (scanner);
3503 if (token != G_TOKEN_EQUAL_SIGN)
3504 return G_TOKEN_EQUAL_SIGN;
3506 token = g_scanner_get_next_token (scanner);
3507 if (token != G_TOKEN_STRING)
3508 return G_TOKEN_STRING;
3510 /* Ignore, do nothing */
3512 return G_TOKEN_NONE;
3516 gtk_rc_parse_fontset (GScanner *scanner,
3517 GtkRcStyle *rc_style)
3521 token = g_scanner_get_next_token (scanner);
3522 if (token != GTK_RC_TOKEN_FONTSET)
3523 return GTK_RC_TOKEN_FONTSET;
3525 token = g_scanner_get_next_token (scanner);
3526 if (token != G_TOKEN_EQUAL_SIGN)
3527 return G_TOKEN_EQUAL_SIGN;
3529 token = g_scanner_get_next_token (scanner);
3530 if (token != G_TOKEN_STRING)
3531 return G_TOKEN_STRING;
3533 /* Do nothing - silently ignore */
3535 return G_TOKEN_NONE;
3539 gtk_rc_parse_font_name (GScanner *scanner,
3540 GtkRcStyle *rc_style)
3544 token = g_scanner_get_next_token (scanner);
3545 if (token != GTK_RC_TOKEN_FONT_NAME)
3546 return GTK_RC_TOKEN_FONT;
3548 token = g_scanner_get_next_token (scanner);
3549 if (token != G_TOKEN_EQUAL_SIGN)
3550 return G_TOKEN_EQUAL_SIGN;
3552 token = g_scanner_get_next_token (scanner);
3553 if (token != G_TOKEN_STRING)
3554 return G_TOKEN_STRING;
3556 if (rc_style->font_desc)
3557 pango_font_description_free (rc_style->font_desc);
3559 rc_style->font_desc =
3560 pango_font_description_from_string (scanner->value.v_string);
3562 return G_TOKEN_NONE;
3566 gtk_rc_parse_engine (GtkRcContext *context,
3568 GtkRcStyle **rc_style)
3571 GtkThemeEngine *engine;
3572 guint result = G_TOKEN_NONE;
3573 GtkRcStyle *new_style = NULL;
3574 gboolean parsed_curlies = FALSE;
3575 GtkRcStylePrivate *rc_priv, *new_priv;
3577 token = g_scanner_get_next_token (scanner);
3578 if (token != GTK_RC_TOKEN_ENGINE)
3579 return GTK_RC_TOKEN_ENGINE;
3581 token = g_scanner_get_next_token (scanner);
3582 if (token != G_TOKEN_STRING)
3583 return G_TOKEN_STRING;
3585 if (!scanner->value.v_string[0])
3587 /* Support engine "" {} to mean override to the default engine
3589 token = g_scanner_get_next_token (scanner);
3590 if (token != G_TOKEN_LEFT_CURLY)
3591 return G_TOKEN_LEFT_CURLY;
3593 token = g_scanner_get_next_token (scanner);
3594 if (token != G_TOKEN_RIGHT_CURLY)
3595 return G_TOKEN_RIGHT_CURLY;
3597 parsed_curlies = TRUE;
3599 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3601 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3603 new_style = gtk_rc_style_new ();
3604 gtk_rc_style_real_merge (new_style, *rc_style);
3606 new_style->name = g_strdup ((*rc_style)->name);
3608 /* take over icon factories and color hashes
3609 * from the to-be-deleted style
3611 new_style->icon_factories = (*rc_style)->icon_factories;
3612 (*rc_style)->icon_factories = NULL;
3613 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3614 new_priv->color_hashes = rc_priv->color_hashes;
3615 rc_priv->color_hashes = NULL;
3618 (*rc_style)->engine_specified = TRUE;
3622 engine = gtk_theme_engine_get (scanner->value.v_string);
3624 token = g_scanner_get_next_token (scanner);
3625 if (token != G_TOKEN_LEFT_CURLY)
3626 return G_TOKEN_LEFT_CURLY;
3630 GtkRcStyleClass *new_class;
3632 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3633 new_style = gtk_theme_engine_create_rc_style (engine);
3634 g_type_module_unuse (G_TYPE_MODULE (engine));
3636 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3638 new_class->merge (new_style, *rc_style);
3640 new_style->name = g_strdup ((*rc_style)->name);
3642 /* take over icon factories and color hashes
3643 * from the to-be-deleted style
3645 new_style->icon_factories = (*rc_style)->icon_factories;
3646 (*rc_style)->icon_factories = NULL;
3647 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3648 new_priv->color_hashes = rc_priv->color_hashes;
3649 rc_priv->color_hashes = NULL;
3651 if (new_class->parse)
3653 parsed_curlies = TRUE;
3654 result = new_class->parse (new_style, context->settings, scanner);
3656 if (result != G_TOKEN_NONE)
3658 /* copy icon factories and color hashes back
3660 (*rc_style)->icon_factories = new_style->icon_factories;
3661 new_style->icon_factories = NULL;
3662 rc_priv->color_hashes = new_priv->color_hashes;
3663 new_priv->color_hashes = NULL;
3665 g_object_unref (new_style);
3672 if (!parsed_curlies)
3674 /* Skip over remainder, looking for nested {}'s
3678 result = G_TOKEN_RIGHT_CURLY;
3679 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3681 if (token == G_TOKEN_LEFT_CURLY)
3683 else if (token == G_TOKEN_RIGHT_CURLY)
3688 result = G_TOKEN_NONE;
3696 new_style->engine_specified = TRUE;
3698 g_object_unref (*rc_style);
3699 *rc_style = new_style;
3706 gtk_rc_parse_state (GScanner *scanner,
3707 GtkStateType *state)
3712 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3713 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3715 /* we don't know where we got called from, so we reset the scope here.
3716 * if we bail out due to errors, we *don't* reset the scope, so the
3717 * error messaging code can make sense of our tokens.
3719 old_scope = g_scanner_set_scope (scanner, 0);
3721 token = g_scanner_get_next_token (scanner);
3722 if (token != G_TOKEN_LEFT_BRACE)
3723 return G_TOKEN_LEFT_BRACE;
3725 token = g_scanner_get_next_token (scanner);
3728 case GTK_RC_TOKEN_ACTIVE:
3729 *state = GTK_STATE_ACTIVE;
3731 case GTK_RC_TOKEN_INSENSITIVE:
3732 *state = GTK_STATE_INSENSITIVE;
3734 case GTK_RC_TOKEN_NORMAL:
3735 *state = GTK_STATE_NORMAL;
3737 case GTK_RC_TOKEN_PRELIGHT:
3738 *state = GTK_STATE_PRELIGHT;
3740 case GTK_RC_TOKEN_SELECTED:
3741 *state = GTK_STATE_SELECTED;
3744 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3747 token = g_scanner_get_next_token (scanner);
3748 if (token != G_TOKEN_RIGHT_BRACE)
3749 return G_TOKEN_RIGHT_BRACE;
3751 g_scanner_set_scope (scanner, old_scope);
3753 return G_TOKEN_NONE;
3757 gtk_rc_parse_priority (GScanner *scanner,
3758 GtkPathPriorityType *priority)
3763 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3764 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3766 /* we don't know where we got called from, so we reset the scope here.
3767 * if we bail out due to errors, we *don't* reset the scope, so the
3768 * error messaging code can make sense of our tokens.
3770 old_scope = g_scanner_set_scope (scanner, 0);
3772 token = g_scanner_get_next_token (scanner);
3776 token = g_scanner_get_next_token (scanner);
3779 case GTK_RC_TOKEN_LOWEST:
3780 *priority = GTK_PATH_PRIO_LOWEST;
3782 case GTK_RC_TOKEN_GTK:
3783 *priority = GTK_PATH_PRIO_GTK;
3785 case GTK_RC_TOKEN_APPLICATION:
3786 *priority = GTK_PATH_PRIO_APPLICATION;
3788 case GTK_RC_TOKEN_THEME:
3789 *priority = GTK_PATH_PRIO_THEME;
3791 case GTK_RC_TOKEN_RC:
3792 *priority = GTK_PATH_PRIO_RC;
3794 case GTK_RC_TOKEN_HIGHEST:
3795 *priority = GTK_PATH_PRIO_HIGHEST;
3798 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3801 g_scanner_set_scope (scanner, old_scope);
3803 return G_TOKEN_NONE;
3807 * gtk_rc_parse_color:
3808 * @scanner: a #GScanner
3809 * @color: a pointer to a #GdkColor structure in which to store the result
3811 * Parses a color in the <link linkend="color=format">format</link> expected
3814 * Note that theme engines should use gtk_rc_parse_color_full() in
3815 * order to support symbolic colors.
3817 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3818 * that was expected but not found
3821 gtk_rc_parse_color (GScanner *scanner,
3824 return gtk_rc_parse_color_full (scanner, NULL, color);
3828 * gtk_rc_parse_color_full:
3829 * @scanner: a #GScanner
3830 * @style: (allow-none): a #GtkRcStyle, or %NULL
3831 * @color: a pointer to a #GdkColor structure in which to store the result
3833 * Parses a color in the <link linkend="color=format">format</link> expected
3834 * in a RC file. If @style is not %NULL, it will be consulted to resolve
3835 * references to symbolic colors.
3837 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3838 * that was expected but not found
3843 gtk_rc_parse_color_full (GScanner *scanner,
3849 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3851 /* we don't need to set our own scope here, because
3852 * we don't need own symbols
3855 token = g_scanner_get_next_token (scanner);
3863 case G_TOKEN_LEFT_CURLY:
3864 token = g_scanner_get_next_token (scanner);
3865 if (token == G_TOKEN_INT)
3866 token_int = scanner->value.v_int;
3867 else if (token == G_TOKEN_FLOAT)
3868 token_int = scanner->value.v_float * 65535.0;
3870 return G_TOKEN_FLOAT;
3871 color->red = CLAMP (token_int, 0, 65535);
3873 token = g_scanner_get_next_token (scanner);
3874 if (token != G_TOKEN_COMMA)
3875 return G_TOKEN_COMMA;
3877 token = g_scanner_get_next_token (scanner);
3878 if (token == G_TOKEN_INT)
3879 token_int = scanner->value.v_int;
3880 else if (token == G_TOKEN_FLOAT)
3881 token_int = scanner->value.v_float * 65535.0;
3883 return G_TOKEN_FLOAT;
3884 color->green = CLAMP (token_int, 0, 65535);
3886 token = g_scanner_get_next_token (scanner);
3887 if (token != G_TOKEN_COMMA)
3888 return G_TOKEN_COMMA;
3890 token = g_scanner_get_next_token (scanner);
3891 if (token == G_TOKEN_INT)
3892 token_int = scanner->value.v_int;
3893 else if (token == G_TOKEN_FLOAT)
3894 token_int = scanner->value.v_float * 65535.0;
3896 return G_TOKEN_FLOAT;
3897 color->blue = CLAMP (token_int, 0, 65535);
3899 token = g_scanner_get_next_token (scanner);
3900 if (token != G_TOKEN_RIGHT_CURLY)
3901 return G_TOKEN_RIGHT_CURLY;
3902 return G_TOKEN_NONE;
3904 case G_TOKEN_STRING:
3905 if (!gdk_color_parse (scanner->value.v_string, color))
3907 g_scanner_warn (scanner, "Invalid color constant '%s'",
3908 scanner->value.v_string);
3909 return G_TOKEN_STRING;
3911 return G_TOKEN_NONE;
3914 token = g_scanner_get_next_token (scanner);
3915 if (token != G_TOKEN_IDENTIFIER)
3916 return G_TOKEN_IDENTIFIER;
3918 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3920 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3921 scanner->value.v_identifier);
3922 return G_TOKEN_IDENTIFIER;
3925 return G_TOKEN_NONE;
3927 case G_TOKEN_IDENTIFIER:
3928 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3930 token = g_scanner_get_next_token (scanner);
3931 if (token != G_TOKEN_LEFT_PAREN)
3932 return G_TOKEN_LEFT_PAREN;
3935 if (g_scanner_peek_next_token (scanner) == '-')
3937 g_scanner_get_next_token (scanner); /* eat sign */
3941 token = g_scanner_get_next_token (scanner);
3942 if (token != G_TOKEN_FLOAT)
3943 return G_TOKEN_FLOAT;
3945 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3947 token = g_scanner_get_next_token (scanner);
3948 if (token != G_TOKEN_COMMA)
3949 return G_TOKEN_COMMA;
3951 token = gtk_rc_parse_color_full (scanner, style, &c1);
3952 if (token != G_TOKEN_NONE)
3955 token = g_scanner_get_next_token (scanner);
3956 if (token != G_TOKEN_COMMA)
3957 return G_TOKEN_COMMA;
3959 token = gtk_rc_parse_color_full (scanner, style, &c2);
3960 if (token != G_TOKEN_NONE)
3963 token = g_scanner_get_next_token (scanner);
3964 if (token != G_TOKEN_RIGHT_PAREN)
3965 return G_TOKEN_RIGHT_PAREN;
3967 color->red = l * c1.red + (1.0 - l) * c2.red;
3968 color->green = l * c1.green + (1.0 - l) * c2.green;
3969 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3971 return G_TOKEN_NONE;
3973 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
3975 token = g_scanner_get_next_token (scanner);
3976 if (token != G_TOKEN_LEFT_PAREN)
3977 return G_TOKEN_LEFT_PAREN;
3980 if (g_scanner_peek_next_token (scanner) == '-')
3982 g_scanner_get_next_token (scanner); /* eat sign */
3986 token = g_scanner_get_next_token (scanner);
3987 if (token != G_TOKEN_FLOAT)
3988 return G_TOKEN_FLOAT;
3990 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3992 token = g_scanner_get_next_token (scanner);
3993 if (token != G_TOKEN_COMMA)
3994 return G_TOKEN_COMMA;
3996 token = gtk_rc_parse_color_full (scanner, style, &c1);
3997 if (token != G_TOKEN_NONE)
4000 token = g_scanner_get_next_token (scanner);
4001 if (token != G_TOKEN_RIGHT_PAREN)
4002 return G_TOKEN_RIGHT_PAREN;
4004 _gtk_style_shade (&c1, color, l);
4006 return G_TOKEN_NONE;
4008 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4009 strcmp (scanner->value.v_identifier, "darker") == 0)
4011 if (scanner->value.v_identifier[0] == 'l')
4016 token = g_scanner_get_next_token (scanner);
4017 if (token != G_TOKEN_LEFT_PAREN)
4018 return G_TOKEN_LEFT_PAREN;
4020 token = gtk_rc_parse_color_full (scanner, style, &c1);
4021 if (token != G_TOKEN_NONE)
4024 token = g_scanner_get_next_token (scanner);
4025 if (token != G_TOKEN_RIGHT_PAREN)
4026 return G_TOKEN_RIGHT_PAREN;
4028 _gtk_style_shade (&c1, color, l);
4030 return G_TOKEN_NONE;
4033 return G_TOKEN_IDENTIFIER;
4036 return G_TOKEN_STRING;
4041 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4046 token = g_scanner_get_next_token (scanner);
4047 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4048 return GTK_RC_TOKEN_PIXMAP_PATH;
4050 token = g_scanner_get_next_token (scanner);
4051 if (token != G_TOKEN_STRING)
4052 return G_TOKEN_STRING;
4054 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4056 return G_TOKEN_NONE;
4060 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4062 const gchar *pix_path)
4064 g_strfreev (context->pixmap_path);
4065 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4069 gtk_rc_parse_module_path (GScanner *scanner)
4073 token = g_scanner_get_next_token (scanner);
4074 if (token != GTK_RC_TOKEN_MODULE_PATH)
4075 return GTK_RC_TOKEN_MODULE_PATH;
4077 token = g_scanner_get_next_token (scanner);
4078 if (token != G_TOKEN_STRING)
4079 return G_TOKEN_STRING;
4081 g_warning ("module_path directive is now ignored\n");
4083 return G_TOKEN_NONE;
4087 gtk_rc_parse_im_module_file (GScanner *scanner)
4091 token = g_scanner_get_next_token (scanner);
4092 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4093 return GTK_RC_TOKEN_IM_MODULE_FILE;
4095 token = g_scanner_get_next_token (scanner);
4096 if (token != G_TOKEN_STRING)
4097 return G_TOKEN_STRING;
4099 g_free (im_module_file);
4101 im_module_file = g_strdup (scanner->value.v_string);
4103 return G_TOKEN_NONE;
4107 gtk_rc_parse_path_pattern (GtkRcContext *context,
4111 GtkPathType path_type;
4113 gboolean is_binding;
4114 GtkPathPriorityType priority = context->default_priority;
4116 token = g_scanner_get_next_token (scanner);
4119 case GTK_RC_TOKEN_WIDGET:
4120 path_type = GTK_PATH_WIDGET;
4122 case GTK_RC_TOKEN_WIDGET_CLASS:
4123 path_type = GTK_PATH_WIDGET_CLASS;
4125 case GTK_RC_TOKEN_CLASS:
4126 path_type = GTK_PATH_CLASS;
4129 return GTK_RC_TOKEN_WIDGET_CLASS;
4132 token = g_scanner_get_next_token (scanner);
4133 if (token != G_TOKEN_STRING)
4134 return G_TOKEN_STRING;
4136 pattern = g_strdup (scanner->value.v_string);
4138 token = g_scanner_get_next_token (scanner);
4139 if (token == GTK_RC_TOKEN_STYLE)
4141 else if (token == GTK_RC_TOKEN_BINDING)
4146 return GTK_RC_TOKEN_STYLE;
4149 if (g_scanner_peek_next_token (scanner) == ':')
4151 token = gtk_rc_parse_priority (scanner, &priority);
4152 if (token != G_TOKEN_NONE)
4159 token = g_scanner_get_next_token (scanner);
4160 if (token != G_TOKEN_STRING)
4163 return G_TOKEN_STRING;
4168 GtkBindingSet *binding;
4170 binding = gtk_binding_set_find (scanner->value.v_string);
4174 return G_TOKEN_STRING;
4176 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4180 GtkRcStyle *rc_style;
4183 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4188 return G_TOKEN_STRING;
4191 rc_set = g_new (GtkRcSet, 1);
4192 rc_set->type = path_type;
4194 if (path_type == GTK_PATH_WIDGET_CLASS)
4196 rc_set->pspec = NULL;
4197 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4201 rc_set->pspec = g_pattern_spec_new (pattern);
4202 rc_set->path = NULL;
4205 rc_set->rc_style = rc_style;
4206 rc_set->priority = priority;
4208 if (path_type == GTK_PATH_WIDGET)
4209 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4210 else if (path_type == GTK_PATH_WIDGET_CLASS)
4211 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4213 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4217 return G_TOKEN_NONE;
4221 gtk_rc_parse_hash_key (GScanner *scanner,
4226 token = g_scanner_get_next_token (scanner);
4227 if (token != G_TOKEN_LEFT_BRACE)
4228 return G_TOKEN_LEFT_BRACE;
4230 token = g_scanner_get_next_token (scanner);
4232 if (token != G_TOKEN_STRING)
4233 return G_TOKEN_STRING;
4235 *hash_key = g_strdup (scanner->value.v_string);
4237 token = g_scanner_get_next_token (scanner);
4238 if (token != G_TOKEN_RIGHT_BRACE)
4241 return G_TOKEN_RIGHT_BRACE;
4244 return G_TOKEN_NONE;
4248 gtk_rc_parse_icon_source (GtkRcContext *context,
4250 GtkIconSet *icon_set,
4251 gboolean *icon_set_valid)
4254 gchar *full_filename;
4255 GtkIconSource *source = NULL;
4257 token = g_scanner_get_next_token (scanner);
4258 if (token != G_TOKEN_LEFT_CURLY)
4259 return G_TOKEN_LEFT_CURLY;
4261 token = g_scanner_get_next_token (scanner);
4263 if (token != G_TOKEN_STRING && token != '@')
4264 return G_TOKEN_STRING;
4266 if (token == G_TOKEN_STRING)
4270 source = gtk_icon_source_new ();
4271 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4274 gtk_icon_source_set_filename (source, full_filename);
4275 g_free (full_filename);
4282 token = g_scanner_get_next_token (scanner);
4284 if (token != G_TOKEN_STRING)
4285 return G_TOKEN_STRING;
4287 source = gtk_icon_source_new ();
4288 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4291 /* We continue parsing even if we didn't find the pixmap so that rest of the
4292 * file is read, even if the syntax is bad. However we don't validate the
4293 * icon_set so the caller can choose not to install it.
4295 token = g_scanner_get_next_token (scanner);
4297 if (token == G_TOKEN_RIGHT_CURLY)
4299 else if (token != G_TOKEN_COMMA)
4301 gtk_icon_source_free (source);
4302 return G_TOKEN_COMMA;
4305 /* Get the direction */
4307 token = g_scanner_get_next_token (scanner);
4311 case GTK_RC_TOKEN_RTL:
4312 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4313 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4316 case GTK_RC_TOKEN_LTR:
4317 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4318 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4325 gtk_icon_source_free (source);
4326 return GTK_RC_TOKEN_RTL;
4330 token = g_scanner_get_next_token (scanner);
4332 if (token == G_TOKEN_RIGHT_CURLY)
4334 else if (token != G_TOKEN_COMMA)
4336 gtk_icon_source_free (source);
4337 return G_TOKEN_COMMA;
4342 token = g_scanner_get_next_token (scanner);
4346 case GTK_RC_TOKEN_NORMAL:
4347 gtk_icon_source_set_state_wildcarded (source, FALSE);
4348 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4351 case GTK_RC_TOKEN_PRELIGHT:
4352 gtk_icon_source_set_state_wildcarded (source, FALSE);
4353 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4357 case GTK_RC_TOKEN_INSENSITIVE:
4358 gtk_icon_source_set_state_wildcarded (source, FALSE);
4359 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4362 case GTK_RC_TOKEN_ACTIVE:
4363 gtk_icon_source_set_state_wildcarded (source, FALSE);
4364 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4367 case GTK_RC_TOKEN_SELECTED:
4368 gtk_icon_source_set_state_wildcarded (source, FALSE);
4369 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4376 gtk_icon_source_free (source);
4377 return GTK_RC_TOKEN_PRELIGHT;
4381 token = g_scanner_get_next_token (scanner);
4383 if (token == G_TOKEN_RIGHT_CURLY)
4385 else if (token != G_TOKEN_COMMA)
4387 gtk_icon_source_free (source);
4388 return G_TOKEN_COMMA;
4393 token = g_scanner_get_next_token (scanner);
4399 if (token != G_TOKEN_STRING)
4401 gtk_icon_source_free (source);
4402 return G_TOKEN_STRING;
4405 size = gtk_icon_size_from_name (scanner->value.v_string);
4407 if (size != GTK_ICON_SIZE_INVALID)
4409 gtk_icon_source_set_size_wildcarded (source, FALSE);
4410 gtk_icon_source_set_size (source, size);
4414 /* Check the close brace */
4416 token = g_scanner_get_next_token (scanner);
4417 if (token != G_TOKEN_RIGHT_CURLY)
4419 gtk_icon_source_free (source);
4420 return G_TOKEN_RIGHT_CURLY;
4424 if (gtk_icon_source_get_filename (source) ||
4425 gtk_icon_source_get_icon_name (source))
4427 gtk_icon_set_add_source (icon_set, source);
4428 *icon_set_valid = TRUE;
4430 gtk_icon_source_free (source);
4432 return G_TOKEN_NONE;
4436 gtk_rc_parse_stock (GtkRcContext *context,
4438 GtkRcStyle *rc_style,
4439 GtkIconFactory *factory)
4441 GtkIconSet *icon_set = NULL;
4442 gboolean icon_set_valid = FALSE;
4443 gchar *stock_id = NULL;
4446 token = g_scanner_get_next_token (scanner);
4447 if (token != GTK_RC_TOKEN_STOCK)
4448 return GTK_RC_TOKEN_STOCK;
4450 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4451 if (token != G_TOKEN_NONE)
4454 token = g_scanner_get_next_token (scanner);
4455 if (token != G_TOKEN_EQUAL_SIGN)
4458 return G_TOKEN_EQUAL_SIGN;
4461 token = g_scanner_get_next_token (scanner);
4462 if (token != G_TOKEN_LEFT_CURLY)
4465 return G_TOKEN_LEFT_CURLY;
4468 token = g_scanner_peek_next_token (scanner);
4469 while (token != G_TOKEN_RIGHT_CURLY)
4471 if (icon_set == NULL)
4472 icon_set = gtk_icon_set_new ();
4474 token = gtk_rc_parse_icon_source (context,
4475 scanner, icon_set, &icon_set_valid);
4476 if (token != G_TOKEN_NONE)
4479 gtk_icon_set_unref (icon_set);
4483 token = g_scanner_get_next_token (scanner);
4485 if (token != G_TOKEN_COMMA &&
4486 token != G_TOKEN_RIGHT_CURLY)
4489 gtk_icon_set_unref (icon_set);
4490 return G_TOKEN_RIGHT_CURLY;
4497 gtk_icon_factory_add (factory,
4501 gtk_icon_set_unref (icon_set);
4506 return G_TOKEN_NONE;
4510 gtk_rc_parse_logical_color (GScanner *scanner,
4511 GtkRcStyle *rc_style,
4514 gchar *color_id = NULL;
4518 token = g_scanner_get_next_token (scanner);
4519 if (token != GTK_RC_TOKEN_COLOR)
4520 return GTK_RC_TOKEN_COLOR;
4522 token = gtk_rc_parse_hash_key (scanner, &color_id);
4523 if (token != G_TOKEN_NONE)
4526 token = g_scanner_get_next_token (scanner);
4527 if (token != G_TOKEN_EQUAL_SIGN)
4530 return G_TOKEN_EQUAL_SIGN;
4533 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4534 if (token != G_TOKEN_NONE)
4540 /* Because the hash is created with destroy functions,
4541 * g_hash_table_insert will free any old values for us,
4542 * if a mapping with the specified key already exists.
4544 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4546 return G_TOKEN_NONE;
4551 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4555 const gchar *current;
4556 const gchar *class_start;
4557 const gchar *class_end;
4558 const gchar *pattern_end;
4559 const gchar *pattern_start;
4564 while ((class_start = strchr (current, '<')) &&
4565 (class_end = strchr (class_start, '>')))
4567 /* Add patterns, but ignore single dots */
4568 if (!(class_start == current ||
4569 (class_start == current + 1 && current[0] == '.')))
4571 pattern_end = class_start - 1;
4572 pattern_start = current;
4574 path_elt = g_new (PathElt, 1);
4576 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4577 path_elt->type = PATH_ELT_PSPEC;
4578 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4579 g_free (sub_pattern);
4581 result = g_slist_prepend (result, path_elt);
4584 path_elt = g_new (PathElt, 1);
4586 /* The < > need to be removed from the string. */
4587 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4589 path_elt->type = PATH_ELT_UNRESOLVED;
4590 path_elt->elt.class_name = sub_pattern;
4592 result = g_slist_prepend (result, path_elt);
4594 current = class_end + 1;
4597 /* Add the rest, if anything is left */
4598 if (strlen (current) > 0)
4600 path_elt = g_new (PathElt, 1);
4601 path_elt->type = PATH_ELT_PSPEC;
4602 path_elt->elt.pspec = g_pattern_spec_new (current);
4604 result = g_slist_prepend (result, path_elt);
4607 return g_slist_reverse (result);
4611 free_path_elt (gpointer data,
4614 PathElt *path_elt = data;
4616 switch (path_elt->type)
4618 case PATH_ELT_PSPEC:
4619 g_pattern_spec_free (path_elt->elt.pspec);
4621 case PATH_ELT_UNRESOLVED:
4622 g_free (path_elt->elt.class_name);
4627 g_assert_not_reached ();
4634 _gtk_rc_free_widget_class_path (GSList *list)
4636 g_slist_foreach (list, free_path_elt, NULL);
4637 g_slist_free (list);
4641 gtk_rc_set_free (GtkRcSet *rc_set)
4644 g_pattern_spec_free (rc_set->pspec);
4646 _gtk_rc_free_widget_class_path (rc_set->path);
4652 match_class (PathElt *path_elt,
4657 if (path_elt->type == PATH_ELT_UNRESOLVED)
4659 type = g_type_from_name (path_elt->elt.class_name);
4660 if (type != G_TYPE_INVALID)
4662 g_free (path_elt->elt.class_name);
4663 path_elt->elt.class_type = type;
4664 path_elt->type = PATH_ELT_TYPE;
4667 return g_str_equal (type_name, path_elt->elt.class_name);
4670 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4674 match_widget_class_recursive (GSList *list,
4677 gchar *path_reversed)
4681 /* break out if we cannot match anymore. */
4690 /* there are two possibilities:
4691 * 1. The next pattern should match the class.
4692 * 2. First normal matching, and then maybe a class */
4694 path_elt = list->data;
4696 if (path_elt->type != PATH_ELT_PSPEC)
4698 gchar *class_start = path;
4701 /* ignore leading dot */
4702 if (class_start[0] == '.')
4704 class_end = strchr (class_start, '.');
4706 if (class_end == NULL)
4708 if (!match_class (path_elt, class_start))
4711 return match_widget_class_recursive (list->next, 0, "", "");
4715 class_end[0] = '\0';
4716 if (!match_class (path_elt, class_start))
4724 gint new_length = length - (class_end - path);
4725 gchar old_char = path_reversed[new_length];
4729 path_reversed[new_length] = '\0';
4730 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4731 path_reversed[new_length] = old_char;
4742 gboolean result = FALSE;
4744 /* If there is nothing after this (ie. no class match),
4745 * just compare the pspec.
4747 if (list->next == NULL)
4748 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4750 class_elt = (PathElt *)list->next->data;
4751 g_assert (class_elt->type != PATH_ELT_PSPEC);
4754 if (class_start[0] == '.')
4759 class_end = strchr (class_start, '.');
4761 /* It should be cheaper to match the class first. (either the pattern
4762 * is simple, and will match most of the times, or it may be complex
4763 * and matching is slow)
4765 if (class_end == NULL)
4767 result = match_class (class_elt, class_start);
4771 class_end[0] = '\0';
4772 result = match_class (class_elt, class_start);
4781 /* terminate the string in front of the class. It does not matter
4782 * that the class becomes unusable, because it is not needed
4783 * inside the recursion
4785 old_char = class_start[0];
4786 class_start[0] = '\0';
4788 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4790 if (class_end != NULL)
4792 gint new_length = length - (class_end - path);
4793 gchar path_reversed_char = path_reversed[new_length];
4795 path_reversed[new_length] = '\0';
4797 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4799 path_reversed[new_length] = path_reversed_char;
4802 result = match_widget_class_recursive (list->next->next, 0, "", "");
4805 class_start[0] = old_char;
4811 /* get next class in path, or break out */
4812 if (class_end != NULL)
4813 class_start = class_end + 1;
4821 _gtk_rc_match_widget_class (GSList *list,
4824 gchar *path_reversed)
4826 return match_widget_class_recursive (list, length, path, path_reversed);
4829 #if defined (G_OS_WIN32) && !defined (_WIN64)
4831 /* DLL ABI stability backward compatibility versions */
4833 #undef gtk_rc_add_default_file
4836 gtk_rc_add_default_file (const gchar *filename)
4838 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4840 gtk_rc_add_default_file_utf8 (utf8_filename);
4842 g_free (utf8_filename);
4845 #undef gtk_rc_set_default_files
4848 gtk_rc_set_default_files (gchar **filenames)
4850 gchar **utf8_filenames;
4853 while (filenames[n++] != NULL)
4856 utf8_filenames = g_new (gchar *, n + 1);
4858 for (i = 0; i < n; i++)
4859 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4861 utf8_filenames[n] = NULL;
4863 gtk_rc_set_default_files_utf8 (utf8_filenames);
4865 g_strfreev (utf8_filenames);
4871 gtk_rc_parse (const gchar *filename)
4873 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4875 gtk_rc_parse_utf8 (utf8_filename);
4877 g_free (utf8_filename);