1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
44 #include <glib/gstdio.h>
45 #include "gdkconfig.h"
47 #include "gtkversion.h"
49 #include "gtkbindings.h"
50 #include "gtkthemes.h"
52 #include "gtkiconfactory.h"
54 #include "gtkmodules.h"
55 #include "gtkprivate.h"
56 #include "gtksettings.h"
57 #include "gtkwindow.h"
65 typedef struct _GtkRcSet GtkRcSet;
66 typedef struct _GtkRcNode GtkRcNode;
67 typedef struct _GtkRcFile GtkRcFile;
102 gchar *canonical_name;
105 guint is_string : 1; /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
111 GHashTable *rc_style_ht;
112 GtkSettings *settings;
113 GSList *rc_sets_widget;
114 GSList *rc_sets_widget_class;
115 GSList *rc_sets_class;
117 /* The files we have parsed, to reread later if necessary */
121 gchar *key_theme_name;
126 gint default_priority;
127 GtkStyle *default_style;
129 GHashTable *color_hash;
134 #define GTK_RC_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RC_STYLE, GtkRcStylePrivate))
136 typedef struct _GtkRcStylePrivate GtkRcStylePrivate;
138 struct _GtkRcStylePrivate
140 GSList *color_hashes;
143 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
145 static guint gtk_rc_style_hash (const gchar *name);
146 static gboolean gtk_rc_style_equal (const gchar *a,
148 static guint gtk_rc_styles_hash (const GSList *rc_styles);
149 static gboolean gtk_rc_styles_equal (const GSList *a,
151 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
153 static GSList * gtk_rc_styles_match (GSList *rc_styles,
157 gchar *path_reversed);
158 static GtkStyle * gtk_rc_style_to_style (GtkRcContext *context,
159 GtkRcStyle *rc_style);
160 static GtkStyle* gtk_rc_init_style (GtkRcContext *context,
162 static void gtk_rc_parse_default_files (GtkRcContext *context);
163 static void gtk_rc_parse_named (GtkRcContext *context,
166 static void gtk_rc_context_parse_file (GtkRcContext *context,
167 const gchar *filename,
170 static void gtk_rc_parse_any (GtkRcContext *context,
171 const gchar *input_name,
173 const gchar *input_string);
174 static guint gtk_rc_parse_statement (GtkRcContext *context,
176 static guint gtk_rc_parse_style (GtkRcContext *context,
178 static guint gtk_rc_parse_assignment (GScanner *scanner,
180 GtkRcProperty *prop);
181 static guint gtk_rc_parse_bg (GScanner *scanner,
183 static guint gtk_rc_parse_fg (GScanner *scanner,
185 static guint gtk_rc_parse_text (GScanner *scanner,
187 static guint gtk_rc_parse_base (GScanner *scanner,
189 static guint gtk_rc_parse_xthickness (GScanner *scanner,
191 static guint gtk_rc_parse_ythickness (GScanner *scanner,
193 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
195 GtkRcStyle *rc_style);
196 static guint gtk_rc_parse_font (GScanner *scanner,
197 GtkRcStyle *rc_style);
198 static guint gtk_rc_parse_fontset (GScanner *scanner,
199 GtkRcStyle *rc_style);
200 static guint gtk_rc_parse_font_name (GScanner *scanner,
201 GtkRcStyle *rc_style);
202 static guint gtk_rc_parse_engine (GtkRcContext *context,
204 GtkRcStyle **rc_style);
205 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
207 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
209 const gchar *pix_path);
210 static guint gtk_rc_parse_module_path (GScanner *scanner);
211 static guint gtk_rc_parse_im_module_file (GScanner *scanner);
212 static guint gtk_rc_parse_path_pattern (GtkRcContext *context,
214 static guint gtk_rc_parse_stock (GtkRcContext *context,
216 GtkRcStyle *rc_style,
217 GtkIconFactory *factory);
218 static guint gtk_rc_parse_logical_color (GScanner *scanner,
219 GtkRcStyle *rc_style,
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-2.0", GTK_BINARY_VERSION, type, NULL);
404 path = g_build_filename (GTK_LIBDIR, "gtk-2.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 = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
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-2.0", "gtkrc", NULL);
527 gtk_rc_add_default_file (str);
530 home = g_get_home_dir ();
533 str = g_build_filename (home, ".gtkrc-2.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;
628 if (context->reloading)
631 g_object_get (settings,
632 "gtk-theme-name", &new_theme_name,
633 "gtk-key-theme-name", &new_key_theme_name,
636 if ((new_theme_name != context->theme_name &&
637 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
638 (new_key_theme_name != context->key_theme_name &&
639 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
641 gtk_rc_reparse_all_for_settings (settings, TRUE);
644 g_free (new_theme_name);
645 g_free (new_key_theme_name);
649 gtk_rc_font_name_changed (GtkSettings *settings,
651 GtkRcContext *context)
653 if (!context->reloading)
654 _gtk_rc_context_get_default_font_name (settings);
658 gtk_rc_color_hash_changed (GtkSettings *settings,
660 GtkRcContext *context)
662 GHashTable *old_hash;
664 old_hash = context->color_hash;
666 g_object_get (settings, "color-hash", &context->color_hash, NULL);
669 g_hash_table_unref (old_hash);
671 gtk_rc_reparse_all_for_settings (settings, TRUE);
674 static GtkRcContext *
675 gtk_rc_context_get (GtkSettings *settings)
677 if (!settings->rc_context)
679 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
681 context->settings = settings;
682 context->rc_style_ht = NULL;
683 context->rc_sets_widget = NULL;
684 context->rc_sets_widget_class = NULL;
685 context->rc_sets_class = NULL;
686 context->rc_files = NULL;
687 context->default_style = NULL;
688 context->reloading = FALSE;
690 g_object_get (settings,
691 "gtk-theme-name", &context->theme_name,
692 "gtk-key-theme-name", &context->key_theme_name,
693 "gtk-font-name", &context->font_name,
694 "color-hash", &context->color_hash,
697 g_signal_connect (settings,
698 "notify::gtk-theme-name",
699 G_CALLBACK (gtk_rc_settings_changed),
701 g_signal_connect (settings,
702 "notify::gtk-key-theme-name",
703 G_CALLBACK (gtk_rc_settings_changed),
705 g_signal_connect (settings,
706 "notify::gtk-font-name",
707 G_CALLBACK (gtk_rc_font_name_changed),
709 g_signal_connect (settings,
710 "notify::color-hash",
711 G_CALLBACK (gtk_rc_color_hash_changed),
714 context->pixmap_path = NULL;
716 context->default_priority = GTK_PATH_PRIO_RC;
718 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
721 return settings->rc_context;
725 gtk_rc_clear_rc_files (GtkRcContext *context)
729 list = context->rc_files;
732 GtkRcFile *rc_file = list->data;
734 if (rc_file->canonical_name != rc_file->name)
735 g_free (rc_file->canonical_name);
736 g_free (rc_file->directory);
737 g_free (rc_file->name);
743 g_slist_free (context->rc_files);
744 context->rc_files = NULL;
748 _gtk_rc_context_destroy (GtkSettings *settings)
750 GtkRcContext *context;
752 g_return_if_fail (GTK_IS_SETTINGS (settings));
754 context = settings->rc_context;
758 _gtk_settings_reset_rc_values (context->settings);
759 gtk_rc_clear_styles (context);
760 gtk_rc_clear_rc_files (context);
762 if (context->default_style)
763 g_object_unref (context->default_style);
765 g_strfreev (context->pixmap_path);
767 g_free (context->theme_name);
768 g_free (context->key_theme_name);
769 g_free (context->font_name);
771 if (context->color_hash)
772 g_hash_table_unref (context->color_hash);
774 g_signal_handlers_disconnect_by_func (settings,
775 gtk_rc_settings_changed, context);
776 g_signal_handlers_disconnect_by_func (settings,
777 gtk_rc_font_name_changed, context);
778 g_signal_handlers_disconnect_by_func (settings,
779 gtk_rc_color_hash_changed, context);
781 rc_contexts = g_slist_remove (rc_contexts, context);
785 settings->rc_context = NULL;
789 gtk_rc_parse_named (GtkRcContext *context,
794 const gchar *home_dir;
798 subpath = g_strconcat ("gtk-2.0-", type,
799 G_DIR_SEPARATOR_S "gtkrc",
802 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
804 /* First look in the users home directory
806 home_dir = g_get_home_dir ();
809 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
810 if (!g_file_test (path, G_FILE_TEST_EXISTS))
819 gchar *theme_dir = gtk_rc_get_theme_dir ();
820 path = g_build_filename (theme_dir, name, subpath, NULL);
823 if (!g_file_test (path, G_FILE_TEST_EXISTS))
832 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
840 gtk_rc_parse_default_files (GtkRcContext *context)
844 gtk_rc_add_initial_default_files ();
846 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
847 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
853 static gboolean initialized = FALSE;
859 gtk_rc_add_initial_default_files ();
862 /* Default RC string */
863 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
864 " bg[NORMAL] = \"#eee1b3\"\n"
865 " fg[NORMAL] = \"#000000\"\n"
868 "style \"gtk-default-progress-bar-style\" {\n"
869 " bg[PRELIGHT] = \"#4b6983\"\n"
870 " fg[PRELIGHT] = \"#ffffff\"\n"
871 " bg[NORMAL] = \"#c4c2bd\"\n"
874 "style \"gtk-default-entry-style\" {\n"
875 " bg[SELECTED] = \"#b7c3cd\"\n"
876 " fg[SELECTED] = \"#000000\"\n"
879 "style \"gtk-default-menu-bar-item-style\" {\n"
880 " GtkMenuItem::horizontal_padding = 5\n"
883 "style \"gtk-default-menu-item-style\" {\n"
884 " bg[PRELIGHT] = \"#4b6983\"\n"
885 " fg[PRELIGHT] = \"#ffffff\"\n"
886 " base[PRELIGHT] = \"#4b6983\"\n"
887 " text[PRELIGHT] = \"#ffffff\"\n"
890 /* Work around clipping of accelerator underlines */
891 "style \"gtk-default-label-style\" {\n"
892 " GtkWidget::draw-border = {0,0,0,1}\n"
895 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
896 "class \"GtkEntry\" style : gtk \"gtk-default-entry-style\"\n"
897 "widget \"gtk-tooltip*\" style : gtk \"gtk-default-tooltips-style\"\n"
898 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
899 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
900 "class \"GtkLabel\" style : gtk \"gtk-default-label-style\"\n"
905 gtk_rc_context_parse_string (GtkRcContext *context,
906 const gchar *rc_string)
908 gtk_rc_parse_any (context, "-", -1, rc_string);
912 gtk_rc_parse_string (const gchar *rc_string)
917 g_return_if_fail (rc_string != NULL);
919 rc_file = g_new (GtkRcFile, 1);
920 rc_file->is_string = TRUE;
921 rc_file->name = g_strdup (rc_string);
922 rc_file->canonical_name = NULL;
923 rc_file->directory = NULL;
925 rc_file->reload = TRUE;
927 global_rc_files = g_slist_append (global_rc_files, rc_file);
929 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
930 gtk_rc_context_parse_string (tmp_list->data, rc_string);
934 add_to_rc_file_list (GSList **rc_file_list,
935 const char *filename,
941 tmp_list = *rc_file_list;
944 rc_file = tmp_list->data;
945 if (!strcmp (rc_file->name, filename))
948 tmp_list = tmp_list->next;
951 rc_file = g_new (GtkRcFile, 1);
952 rc_file->is_string = FALSE;
953 rc_file->name = g_strdup (filename);
954 rc_file->canonical_name = NULL;
955 rc_file->directory = NULL;
957 rc_file->reload = reload;
959 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
965 gtk_rc_context_parse_one_file (GtkRcContext *context,
966 const gchar *filename,
974 g_return_if_fail (filename != NULL);
976 saved_priority = context->default_priority;
977 context->default_priority = priority;
979 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
981 if (!rc_file->canonical_name)
983 /* Get the absolute pathname */
985 if (g_path_is_absolute (rc_file->name))
986 rc_file->canonical_name = rc_file->name;
991 cwd = g_get_current_dir ();
992 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
996 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
999 /* If the file is already being parsed (recursion), do nothing
1001 if (g_slist_find (current_files_stack, rc_file))
1004 if (!g_lstat (rc_file->canonical_name, &statbuf))
1008 rc_file->mtime = statbuf.st_mtime;
1010 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1014 /* Temporarily push information for this file on
1015 * a stack of current files while parsing it.
1017 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1018 gtk_rc_parse_any (context, filename, fd, NULL);
1019 current_files_stack = g_slist_delete_link (current_files_stack,
1020 current_files_stack);
1026 context->default_priority = saved_priority;
1030 strchr_len (const gchar *str, gint len, char c)
1035 return (gchar *)str;
1044 gtk_rc_context_parse_file (GtkRcContext *context,
1045 const gchar *filename,
1049 gchar *locale_suffixes[2];
1050 gint n_locale_suffixes = 0;
1054 gboolean found = FALSE;
1056 locale = _gtk_get_lc_ctype ();
1058 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1060 /* Determine locale-specific suffixes for RC files.
1062 length = strlen (locale);
1064 p = strchr (locale, '@');
1066 length = p - locale;
1068 p = strchr_len (locale, length, '.');
1070 length = p - locale;
1072 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1074 p = strchr_len (locale, length, '_');
1077 length = p - locale;
1078 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1084 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1085 for (j = 0; j < n_locale_suffixes; j++)
1089 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1090 if (g_file_test (name, G_FILE_TEST_EXISTS))
1092 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1099 g_free (locale_suffixes[j]);
1104 gtk_rc_parse (const gchar *filename)
1108 g_return_if_fail (filename != NULL);
1110 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1112 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1113 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1116 /* Handling of RC styles */
1118 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1121 gtk_rc_style_init (GtkRcStyle *style)
1123 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1127 style->font_desc = NULL;
1129 for (i = 0; i < 5; i++)
1131 static const GdkColor init_color = { 0, 0, 0, 0, };
1133 style->bg_pixmap_name[i] = NULL;
1134 style->color_flags[i] = 0;
1135 style->fg[i] = init_color;
1136 style->bg[i] = init_color;
1137 style->text[i] = init_color;
1138 style->base[i] = init_color;
1140 style->xthickness = -1;
1141 style->ythickness = -1;
1142 style->rc_properties = NULL;
1144 style->rc_style_lists = NULL;
1145 style->icon_factories = NULL;
1147 priv->color_hashes = NULL;
1151 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1153 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1155 object_class->finalize = gtk_rc_style_finalize;
1157 klass->parse = NULL;
1158 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1159 klass->merge = gtk_rc_style_real_merge;
1160 klass->create_style = gtk_rc_style_real_create_style;
1162 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1166 gtk_rc_style_finalize (GObject *object)
1168 GSList *tmp_list1, *tmp_list2;
1169 GtkRcStyle *rc_style;
1170 GtkRcStylePrivate *rc_priv;
1173 rc_style = GTK_RC_STYLE (object);
1174 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1176 g_free (rc_style->name);
1177 if (rc_style->font_desc)
1178 pango_font_description_free (rc_style->font_desc);
1180 for (i = 0; i < 5; i++)
1181 g_free (rc_style->bg_pixmap_name[i]);
1183 /* Now remove all references to this rc_style from
1186 tmp_list1 = rc_style->rc_style_lists;
1189 GSList *rc_styles = tmp_list1->data;
1190 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1191 g_object_unref (style);
1193 /* Remove the list of styles from the other rc_styles
1196 tmp_list2 = rc_styles;
1199 GtkRcStyle *other_style = tmp_list2->data;
1201 if (other_style != rc_style)
1202 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1204 tmp_list2 = tmp_list2->next;
1207 /* And from the hash table itself
1209 g_hash_table_remove (realized_style_ht, rc_styles);
1210 g_slist_free (rc_styles);
1212 tmp_list1 = tmp_list1->next;
1214 g_slist_free (rc_style->rc_style_lists);
1216 if (rc_style->rc_properties)
1220 for (i = 0; i < rc_style->rc_properties->len; i++)
1222 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1224 g_free (node->origin);
1225 g_value_unset (&node->value);
1227 g_array_free (rc_style->rc_properties, TRUE);
1228 rc_style->rc_properties = NULL;
1231 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1232 g_slist_free (rc_style->icon_factories);
1234 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1235 g_slist_free (rc_priv->color_hashes);
1237 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1241 gtk_rc_style_new (void)
1245 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1251 * gtk_rc_style_copy:
1252 * @orig: the style to copy
1254 * Makes a copy of the specified #GtkRcStyle. This function
1255 * will correctly copy an RC style that is a member of a class
1256 * derived from #GtkRcStyle.
1258 * Return value: the resulting #GtkRcStyle
1261 gtk_rc_style_copy (GtkRcStyle *orig)
1265 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1267 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1268 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1270 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1276 _gtk_rc_style_set_rc_property (GtkRcStyle *rc_style,
1277 GtkRcProperty *property)
1279 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1280 g_return_if_fail (property != NULL);
1282 insert_rc_property (rc_style, property, TRUE);
1286 _gtk_rc_style_unset_rc_property (GtkRcStyle *rc_style,
1288 GQuark property_name)
1290 GtkRcProperty *node;
1292 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1294 node = (GtkRcProperty *) _gtk_rc_style_lookup_rc_property (rc_style,
1300 guint index = node - (GtkRcProperty *) rc_style->rc_properties->data;
1301 g_value_unset (&node->value);
1302 g_free (node->origin);
1303 g_array_remove_index (rc_style->rc_properties, index);
1308 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1310 return g_object_new (G_OBJECT_TYPE (style), NULL);
1314 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1316 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1318 return priv->color_hashes;
1321 static void gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style);
1324 _gtk_rc_style_set_symbolic_color (GtkRcStyle *rc_style,
1326 const GdkColor *color)
1328 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1329 GHashTable *our_hash = NULL;
1331 if (priv->color_hashes)
1332 our_hash = priv->color_hashes->data;
1334 if (our_hash == NULL)
1339 gtk_rc_style_prepend_empty_color_hash (rc_style);
1340 our_hash = priv->color_hashes->data;
1344 g_hash_table_insert (our_hash, g_strdup (name), gdk_color_copy (color));
1346 g_hash_table_remove (our_hash, name);
1350 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1351 gconstpointer bsearch_node2)
1353 const GtkRcProperty *prop1 = bsearch_node1;
1354 const GtkRcProperty *prop2 = bsearch_node2;
1356 if (prop1->type_name == prop2->type_name)
1357 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1359 return prop1->type_name < prop2->type_name ? -1 : 1;
1363 insert_rc_property (GtkRcStyle *style,
1364 GtkRcProperty *property,
1368 GtkRcProperty *new_property = NULL;
1369 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1371 key.type_name = property->type_name;
1372 key.property_name = property->property_name;
1374 if (!style->rc_properties)
1375 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1378 while (i < style->rc_properties->len)
1380 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1386 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1388 g_free (new_property->origin);
1389 g_value_unset (&new_property->value);
1391 *new_property = key;
1405 g_array_insert_val (style->rc_properties, i, key);
1406 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1409 new_property->origin = g_strdup (property->origin);
1410 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1411 g_value_copy (&property->value, &new_property->value);
1415 gtk_rc_style_real_merge (GtkRcStyle *dest,
1420 for (i = 0; i < 5; i++)
1422 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1423 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1425 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1426 src->color_flags[i] & GTK_RC_FG)
1428 dest->fg[i] = src->fg[i];
1429 dest->color_flags[i] |= GTK_RC_FG;
1431 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1432 src->color_flags[i] & GTK_RC_BG)
1434 dest->bg[i] = src->bg[i];
1435 dest->color_flags[i] |= GTK_RC_BG;
1437 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1438 src->color_flags[i] & GTK_RC_TEXT)
1440 dest->text[i] = src->text[i];
1441 dest->color_flags[i] |= GTK_RC_TEXT;
1443 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1444 src->color_flags[i] & GTK_RC_BASE)
1446 dest->base[i] = src->base[i];
1447 dest->color_flags[i] |= GTK_RC_BASE;
1451 if (dest->xthickness < 0 && src->xthickness >= 0)
1452 dest->xthickness = src->xthickness;
1453 if (dest->ythickness < 0 && src->ythickness >= 0)
1454 dest->ythickness = src->ythickness;
1458 if (!dest->font_desc)
1459 dest->font_desc = pango_font_description_copy (src->font_desc);
1461 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1464 if (src->rc_properties)
1468 for (i = 0; i < src->rc_properties->len; i++)
1469 insert_rc_property (dest,
1470 &g_array_index (src->rc_properties, GtkRcProperty, i),
1476 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1478 return gtk_style_new ();
1482 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1484 GtkIconFactory *factory = gtk_icon_factory_new ();
1486 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1490 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1492 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1493 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1495 (GDestroyNotify) gdk_color_free);
1497 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1501 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1502 GtkRcStyle *src_style)
1504 GSList *concat = g_slist_copy (src_style->icon_factories);
1506 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1508 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1512 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1513 GtkRcStyle *src_style)
1515 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1516 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1517 GSList *concat = g_slist_copy (src_priv->color_hashes);
1519 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1521 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1525 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1526 GtkRcStyle *src_style,
1527 GtkRcContext *context)
1529 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1533 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1535 /* Append src_style's factories, adding a ref to them */
1536 if (src_style->icon_factories != NULL)
1538 /* Add a factory for ourselves if we have none,
1539 * in case we end up defining more stock icons.
1540 * I see no real way around this; we need to maintain
1541 * the invariant that the first factory in the list
1542 * is always our_factory, the one belonging to us,
1543 * and if we put src_style factories in the list we can't
1544 * do that if the style is reopened.
1546 if (rc_style->icon_factories == NULL)
1547 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1549 gtk_rc_style_append_icon_factories (rc_style, src_style);
1552 /* Also append src_style's color hashes, adding a ref to them */
1553 if (src_priv->color_hashes != NULL)
1555 /* See comment above .. */
1556 if (priv->color_hashes == NULL)
1557 gtk_rc_style_prepend_empty_color_hash (rc_style);
1559 gtk_rc_style_append_color_hashes (rc_style, src_style);
1563 /* if we didn't get color hashes from the src_style, initialize
1564 * the list with the settings' color scheme (if it exists)
1566 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1568 gtk_rc_style_prepend_empty_color_hash (rc_style);
1570 priv->color_hashes = g_slist_append (priv->color_hashes,
1571 g_hash_table_ref (context->color_hash));
1576 gtk_rc_clear_hash_node (gpointer key,
1580 g_object_unref (data);
1584 gtk_rc_free_rc_sets (GSList *slist)
1590 rc_set = slist->data;
1591 gtk_rc_set_free (rc_set);
1593 slist = slist->next;
1598 gtk_rc_clear_styles (GtkRcContext *context)
1600 /* Clear out all old rc_styles */
1602 if (context->rc_style_ht)
1604 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1605 g_hash_table_destroy (context->rc_style_ht);
1606 context->rc_style_ht = NULL;
1609 gtk_rc_free_rc_sets (context->rc_sets_widget);
1610 g_slist_free (context->rc_sets_widget);
1611 context->rc_sets_widget = NULL;
1613 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1614 g_slist_free (context->rc_sets_widget_class);
1615 context->rc_sets_widget_class = NULL;
1617 gtk_rc_free_rc_sets (context->rc_sets_class);
1618 g_slist_free (context->rc_sets_class);
1619 context->rc_sets_class = NULL;
1622 /* Reset all our widgets. Also, we have to invalidate cached icons in
1623 * icon sets so they get re-rendered.
1626 gtk_rc_reset_widgets (GtkSettings *settings)
1628 GList *list, *toplevels;
1630 _gtk_icon_set_invalidate_caches ();
1632 toplevels = gtk_window_list_toplevels ();
1633 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1635 for (list = toplevels; list; list = list->next)
1637 if (gtk_widget_get_screen (list->data) == settings->screen)
1639 gtk_widget_reset_rc_styles (list->data);
1642 g_object_unref (list->data);
1644 g_list_free (toplevels);
1648 gtk_rc_clear_realized_style (gpointer key,
1652 GSList *rc_styles = key;
1653 GtkStyle *style = value;
1654 GSList *tmp_list = rc_styles;
1656 g_object_unref (style);
1660 GtkRcStyle *rc_style = tmp_list->data;
1662 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1664 tmp_list = tmp_list->next;
1667 g_slist_free (rc_styles);
1671 * gtk_rc_reset_styles:
1672 * @settings: a #GtkSettings
1674 * This function recomputes the styles for all widgets that use a
1675 * particular #GtkSettings object. (There is one #GtkSettings object
1676 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1677 * when some global parameter has changed that affects the appearance
1678 * of all widgets, because when a widget gets a new style, it will
1679 * both redraw and recompute any cached information about its
1680 * appearance. As an example, it is used when the default font size
1681 * set by the operating system changes. Note that this function
1682 * doesn't affect widgets that have a style set explicitely on them
1683 * with gtk_widget_set_style().
1688 gtk_rc_reset_styles (GtkSettings *settings)
1690 GtkRcContext *context;
1691 gboolean reset = FALSE;
1693 g_return_if_fail (GTK_IS_SETTINGS (settings));
1695 context = gtk_rc_context_get (settings);
1697 if (context->default_style)
1699 g_object_unref (context->default_style);
1700 context->default_style = NULL;
1704 /* Clear out styles that have been looked up already
1706 if (realized_style_ht)
1708 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1709 g_hash_table_destroy (realized_style_ht);
1710 realized_style_ht = NULL;
1715 gtk_rc_reset_widgets (settings);
1719 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1721 GtkRcContext *context;
1722 gchar *new_font_name;
1724 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1726 context = gtk_rc_context_get (settings);
1728 g_object_get (context->settings,
1729 "gtk-font-name", &new_font_name,
1732 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1734 g_free (context->font_name);
1735 context->font_name = g_strdup (new_font_name);
1737 gtk_rc_reset_styles (settings);
1740 g_free (new_font_name);
1742 return context->font_name;
1746 * gtk_rc_reparse_all_for_settings:
1747 * @settings: a #GtkSettings
1748 * @force_load: load whether or not anything changed
1750 * If the modification time on any previously read file
1751 * for the given #GtkSettings has changed, discard all style information
1752 * and then reread all previously read RC files.
1754 * Return value: %TRUE if the files were reread.
1757 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1758 gboolean force_load)
1760 gboolean mtime_modified = FALSE;
1763 GtkRcContext *context;
1764 struct stat statbuf;
1766 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1768 context = gtk_rc_context_get (settings);
1770 if (context->reloading)
1775 /* Check through and see if any of the RC's have had their
1776 * mtime modified. If so, reparse everything.
1778 tmp_list = context->rc_files;
1781 rc_file = tmp_list->data;
1783 if (!rc_file->is_string)
1785 if (!g_lstat (rc_file->name, &statbuf) &&
1786 (statbuf.st_mtime != rc_file->mtime))
1788 mtime_modified = TRUE;
1793 tmp_list = tmp_list->next;
1797 if (force_load || mtime_modified)
1799 _gtk_binding_reset_parsed ();
1800 gtk_rc_clear_styles (context);
1801 context->reloading = TRUE;
1803 _gtk_settings_reset_rc_values (context->settings);
1804 gtk_rc_clear_rc_files (context);
1806 gtk_rc_parse_default_files (context);
1808 tmp_list = global_rc_files;
1811 rc_file = tmp_list->data;
1813 if (rc_file->is_string)
1814 gtk_rc_context_parse_string (context, rc_file->name);
1816 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1818 tmp_list = tmp_list->next;
1821 g_free (context->theme_name);
1822 g_free (context->key_theme_name);
1824 g_object_get (context->settings,
1825 "gtk-theme-name", &context->theme_name,
1826 "gtk-key-theme-name", &context->key_theme_name,
1829 if (context->theme_name && context->theme_name[0])
1830 gtk_rc_parse_named (context, context->theme_name, NULL);
1831 if (context->key_theme_name && context->key_theme_name[0])
1832 gtk_rc_parse_named (context, context->key_theme_name, "key");
1834 context->reloading = FALSE;
1836 gtk_rc_reset_widgets (context->settings);
1839 return force_load || mtime_modified;
1843 * gtk_rc_reparse_all:
1845 * If the modification time on any previously read file for the
1846 * default #GtkSettings has changed, discard all style information
1847 * and then reread all previously read RC files.
1849 * Return value: %TRUE if the files were reread.
1852 gtk_rc_reparse_all (void)
1855 gboolean result = FALSE;
1857 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1859 GtkRcContext *context = tmp_list->data;
1860 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1868 gtk_rc_styles_match (GSList *rc_styles,
1872 gchar *path_reversed)
1879 rc_set = sets->data;
1882 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1884 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1885 rc_styles = g_slist_append (rc_styles, rc_set);
1889 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1890 rc_styles = g_slist_append (rc_styles, rc_set);
1898 rc_set_compare (gconstpointer a, gconstpointer b)
1900 const GtkRcSet *set_a = a;
1901 const GtkRcSet *set_b = b;
1903 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1907 sort_and_dereference_sets (GSList *styles)
1911 /* At this point, the list of sets is ordered by:
1913 * a) 'widget' patterns are earlier than 'widget_class' patterns
1914 * which are ealier than 'class' patterns.
1915 * a) For two matches for class patterns, a match to a child type
1916 * is before a match to a parent type
1917 * c) a match later in the RC file (or in a later RC file) is before a
1918 * match earlier in the RC file.
1920 * With a) taking precedence over b) which takes precendence over c).
1922 * Now sort by priority, which has the highest precendence for sort order
1924 styles = g_slist_sort (styles, rc_set_compare);
1926 /* Make styles->data = styles->data->rc_style
1931 GtkRcSet *set = tmp_list->data;
1932 tmp_list->data = set->rc_style;
1933 tmp_list = tmp_list->next;
1941 * @widget: a #GtkWidget
1943 * Finds all matching RC styles for a given widget,
1944 * composites them together, and then creates a
1945 * #GtkStyle representing the composite appearance.
1946 * (GTK+ actually keeps a cache of previously
1947 * created styles, so a new style may not be
1950 * Returns: the resulting style. No refcount is added
1951 * to the returned style, so if you want to save this
1952 * style around, you should add a reference yourself.
1955 gtk_rc_get_style (GtkWidget *widget)
1957 GtkRcStyle *widget_rc_style;
1958 GSList *rc_styles = NULL;
1959 GtkRcContext *context;
1961 static guint rc_style_key_id = 0;
1963 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1965 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1967 /* We allow the specification of a single rc style to be bound
1968 * tightly to a widget, for application modifications
1970 if (!rc_style_key_id)
1971 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1973 if (context->rc_sets_widget)
1975 gchar *path, *path_reversed;
1978 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1979 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1981 g_free (path_reversed);
1984 if (context->rc_sets_widget_class)
1986 gchar *path, *path_reversed;
1989 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1990 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1992 g_free (path_reversed);
1995 if (context->rc_sets_class)
1999 type = G_TYPE_FROM_INSTANCE (widget);
2003 gchar *path_reversed;
2006 path = g_strdup (g_type_name (type));
2007 path_length = strlen (path);
2008 path_reversed = g_strdup (path);
2009 g_strreverse (path_reversed);
2011 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2013 g_free (path_reversed);
2015 type = g_type_parent (type);
2019 rc_styles = sort_and_dereference_sets (rc_styles);
2021 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
2023 if (widget_rc_style)
2024 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
2027 return gtk_rc_init_style (context, rc_styles);
2030 if (!context->default_style)
2032 context->default_style = gtk_style_new ();
2033 _gtk_style_init_for_settings (context->default_style, context->settings);
2036 return context->default_style;
2041 * gtk_rc_get_style_by_paths:
2042 * @settings: a #GtkSettings object
2043 * @widget_path: (allow-none): the widget path to use when looking up the style, or %NULL
2044 * if no matching against the widget path should be done
2045 * @class_path: (allow-none): the class path to use when looking up the style, or %NULL
2046 * if no matching against the class path should be done.
2047 * @type: a type that will be used along with parent types of this type
2048 * when matching against class styles, or #G_TYPE_NONE
2050 * Creates up a #GtkStyle from styles defined in a RC file by providing
2051 * the raw components used in matching. This function may be useful
2052 * when creating pseudo-widgets that should be themed like widgets but
2053 * don't actually have corresponding GTK+ widgets. An example of this
2054 * would be items inside a GNOME canvas widget.
2056 * The action of gtk_rc_get_style() is similar to:
2058 * gtk_widget_path (widget, NULL, &path, NULL);
2059 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2060 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
2062 * G_OBJECT_TYPE (widget));
2065 * Return value: A style created by matching with the supplied paths,
2066 * or %NULL if nothing matching was specified and the default style should
2067 * be used. The returned value is owned by GTK+ as part of an internal cache,
2068 * so you must call g_object_ref() on the returned value if you want to
2069 * keep a reference to it.
2072 gtk_rc_get_style_by_paths (GtkSettings *settings,
2073 const char *widget_path,
2074 const char *class_path,
2077 /* We duplicate the code from above to avoid slowing down the above
2078 * by generating paths when we don't need them. I don't know if
2079 * this is really worth it.
2081 GSList *rc_styles = NULL;
2082 GtkRcContext *context;
2084 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2086 context = gtk_rc_context_get (settings);
2088 if (widget_path && context->rc_sets_widget)
2091 gchar *path_reversed;
2094 path_length = strlen (widget_path);
2095 path = g_strdup (widget_path);
2096 path_reversed = g_strdup (widget_path);
2097 g_strreverse (path_reversed);
2099 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2101 g_free (path_reversed);
2104 if (class_path && context->rc_sets_widget_class)
2107 gchar *path_reversed;
2110 path = g_strdup (class_path);
2111 path_length = strlen (class_path);
2112 path_reversed = g_strdup (class_path);
2113 g_strreverse (path_reversed);
2115 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2117 g_free (path_reversed);
2120 if (type != G_TYPE_NONE && context->rc_sets_class)
2125 gchar *path_reversed;
2128 path = g_strdup (g_type_name (type));
2129 path_length = strlen (path);
2130 path_reversed = g_strdup (path);
2131 g_strreverse (path_reversed);
2133 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2135 g_free (path_reversed);
2137 type = g_type_parent (type);
2141 rc_styles = sort_and_dereference_sets (rc_styles);
2144 return gtk_rc_init_style (context, rc_styles);
2150 gtk_rc_scanner_new (void)
2152 return g_scanner_new (>k_rc_scanner_config);
2156 gtk_rc_parse_any (GtkRcContext *context,
2157 const gchar *input_name,
2159 const gchar *input_string)
2165 scanner = gtk_rc_scanner_new ();
2169 g_assert (input_string == NULL);
2171 g_scanner_input_file (scanner, input_fd);
2175 g_assert (input_string != NULL);
2177 g_scanner_input_text (scanner, input_string, strlen (input_string));
2179 scanner->input_name = input_name;
2181 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2182 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2186 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2190 guint expected_token;
2192 expected_token = gtk_rc_parse_statement (context, scanner);
2194 if (expected_token != G_TOKEN_NONE)
2196 const gchar *symbol_name = NULL;
2199 if (scanner->scope_id == 0)
2201 /* if we are in scope 0, we know the symbol names
2202 * that are associated with certain token values.
2203 * so we look them up to make the error messages
2206 if (expected_token > GTK_RC_TOKEN_INVALID &&
2207 expected_token < GTK_RC_TOKEN_LAST)
2209 const gchar *sym = NULL;
2211 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2212 if (symbols[i].token == expected_token)
2213 sym = symbol_names + symbols[i].name_offset;
2216 msg = g_strconcat ("e.g. `", sym, "'", NULL);
2219 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2220 scanner->token < GTK_RC_TOKEN_LAST)
2222 symbol_name = "???";
2223 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2224 if (symbols[i].token == scanner->token)
2225 symbol_name = symbol_names + symbols[i].name_offset;
2229 g_scanner_unexp_token (scanner,
2242 g_scanner_destroy (scanner);
2246 gtk_rc_styles_hash (const GSList *rc_styles)
2253 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2254 rc_styles = rc_styles->next;
2261 gtk_rc_styles_equal (const GSList *a,
2266 if (a->data != b->data)
2276 gtk_rc_style_hash (const gchar *name)
2282 result += (result << 3) + *name++;
2288 gtk_rc_style_equal (const gchar *a,
2291 return (strcmp (a, b) == 0);
2295 gtk_rc_style_find (GtkRcContext *context,
2298 if (context->rc_style_ht)
2299 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2305 gtk_rc_style_to_style (GtkRcContext *context,
2306 GtkRcStyle *rc_style)
2310 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2311 _gtk_style_init_for_settings (style, context->settings);
2313 style->rc_style = g_object_ref (rc_style);
2315 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2320 /* Reuses or frees rc_styles */
2322 gtk_rc_init_style (GtkRcContext *context,
2325 GtkStyle *style = NULL;
2328 g_return_val_if_fail (rc_styles != NULL, NULL);
2330 if (!realized_style_ht)
2331 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2332 (GEqualFunc) gtk_rc_styles_equal);
2334 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2338 GtkRcStyle *base_style = NULL;
2339 GtkRcStyle *proto_style;
2340 GtkRcStyleClass *proto_style_class;
2342 GType rc_style_type = GTK_TYPE_RC_STYLE;
2344 /* Find the first style where the RC file specified engine "" {}
2345 * or the first derived style and use that to create the
2346 * merged style. If we only have raw GtkRcStyles, use the first
2347 * style to create the merged style.
2349 base_style = rc_styles->data;
2350 tmp_styles = rc_styles;
2353 GtkRcStyle *rc_style = tmp_styles->data;
2355 if (rc_style->engine_specified ||
2356 G_OBJECT_TYPE (rc_style) != rc_style_type)
2358 base_style = rc_style;
2362 tmp_styles = tmp_styles->next;
2365 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2366 proto_style = proto_style_class->create_rc_style (base_style);
2368 tmp_styles = rc_styles;
2371 GtkRcStyle *rc_style = tmp_styles->data;
2373 proto_style_class->merge (proto_style, rc_style);
2375 /* Point from each rc_style to the list of styles */
2376 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2377 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2379 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2380 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2382 tmp_styles = tmp_styles->next;
2385 for (i = 0; i < 5; i++)
2386 if (proto_style->bg_pixmap_name[i] &&
2387 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2389 g_free (proto_style->bg_pixmap_name[i]);
2390 proto_style->bg_pixmap_name[i] = NULL;
2393 style = gtk_rc_style_to_style (context, proto_style);
2394 g_object_unref (proto_style);
2396 g_hash_table_insert (realized_style_ht, rc_styles, style);
2399 g_slist_free (rc_styles);
2404 /*********************
2405 * Parsing functions *
2406 *********************/
2409 lookup_color (GtkRcStyle *style,
2410 const char *color_name,
2413 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2416 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2418 GHashTable *hash = iter->data;
2419 GdkColor *match = g_hash_table_lookup (hash, color_name);
2423 color->red = match->red;
2424 color->green = match->green;
2425 color->blue = match->blue;
2434 rc_parse_token_or_compound (GScanner *scanner,
2437 GTokenType delimiter)
2439 guint token = g_scanner_get_next_token (scanner);
2441 /* we either scan a single token (skipping comments)
2442 * or a compund statement.
2443 * compunds are enclosed in (), [] or {} braces, we read
2444 * them in via deep recursion.
2451 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2454 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2456 case G_TOKEN_STRING:
2457 string = g_strescape (scanner->value.v_string, NULL);
2458 g_string_append (gstring, " \"");
2459 g_string_append (gstring, string);
2460 g_string_append_c (gstring, '"');
2463 case G_TOKEN_IDENTIFIER:
2464 g_string_append_c (gstring, ' ');
2465 g_string_append (gstring, scanner->value.v_identifier);
2467 case G_TOKEN_COMMENT_SINGLE:
2468 case G_TOKEN_COMMENT_MULTI:
2469 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2470 case G_TOKEN_LEFT_PAREN:
2471 g_string_append_c (gstring, ' ');
2472 g_string_append_c (gstring, token);
2473 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2474 if (token != G_TOKEN_NONE)
2477 case G_TOKEN_LEFT_CURLY:
2478 g_string_append_c (gstring, ' ');
2479 g_string_append_c (gstring, token);
2480 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2481 if (token != G_TOKEN_NONE)
2484 case G_TOKEN_LEFT_BRACE:
2485 g_string_append_c (gstring, ' ');
2486 g_string_append_c (gstring, token);
2487 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2488 if (token != G_TOKEN_NONE)
2492 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2495 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2496 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2497 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2499 g_scanner_get_next_token (scanner);
2501 if (!style || !lookup_color (style, scanner->value.v_identifier,
2504 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2505 scanner->value.v_identifier);
2506 return G_TOKEN_IDENTIFIER;
2510 g_string_append_printf (gstring, " { %s, %s, %s }",
2511 g_ascii_formatd (rbuf, sizeof (rbuf),
2513 color.red / 65535.0),
2514 g_ascii_formatd (gbuf, sizeof (gbuf),
2516 color.green / 65535.0),
2517 g_ascii_formatd (bbuf, sizeof (bbuf),
2519 color.blue / 65535.0));
2523 return G_TOKEN_IDENTIFIER;
2525 if (token >= 256 || token < 1)
2526 return delimiter ? delimiter : G_TOKEN_STRING;
2527 g_string_append_c (gstring, ' ');
2528 g_string_append_c (gstring, token);
2529 if (token == delimiter)
2530 return G_TOKEN_NONE;
2534 return G_TOKEN_NONE;
2536 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2540 gtk_rc_parse_assignment (GScanner *scanner,
2542 GtkRcProperty *prop)
2544 #define MY_SCAN_IDENTIFIER TRUE
2545 #define MY_SCAN_SYMBOLS FALSE
2546 #define MY_IDENTIFIER_2_STRING FALSE
2547 #define MY_CHAR_2_TOKEN TRUE
2548 #define MY_SCAN_IDENTIFIER_NULL FALSE
2549 #define MY_NUMBERS_2_INT TRUE
2551 gboolean scan_identifier = scanner->config->scan_identifier;
2552 gboolean scan_symbols = scanner->config->scan_symbols;
2553 gboolean identifier_2_string = scanner->config->identifier_2_string;
2554 gboolean char_2_token = scanner->config->char_2_token;
2555 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2556 gboolean numbers_2_int = scanner->config->numbers_2_int;
2557 gboolean negate = FALSE;
2558 gboolean is_color = FALSE;
2561 /* check that this is an assignment */
2562 if (g_scanner_get_next_token (scanner) != '=')
2565 /* adjust scanner mode */
2566 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2567 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2568 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2569 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2570 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2571 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2573 /* record location */
2574 if (g_getenv ("GTK_DEBUG"))
2575 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2577 prop->origin = NULL;
2579 /* parse optional symbolic color prefix */
2580 if (g_scanner_peek_next_token (scanner) == '@')
2582 g_scanner_get_next_token (scanner); /* eat color prefix */
2586 /* parse optional sign */
2587 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2589 g_scanner_get_next_token (scanner); /* eat sign */
2593 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2596 token = g_scanner_peek_next_token (scanner);
2598 if (is_color && token != G_TOKEN_IDENTIFIER)
2600 token = G_TOKEN_IDENTIFIER;
2607 g_scanner_get_next_token (scanner);
2608 g_value_init (&prop->value, G_TYPE_LONG);
2609 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2610 token = G_TOKEN_NONE;
2613 g_scanner_get_next_token (scanner);
2614 g_value_init (&prop->value, G_TYPE_DOUBLE);
2615 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2616 token = G_TOKEN_NONE;
2618 case G_TOKEN_STRING:
2619 g_scanner_get_next_token (scanner);
2621 token = G_TOKEN_INT;
2624 g_value_init (&prop->value, G_TYPE_STRING);
2625 g_value_set_string (&prop->value, scanner->value.v_string);
2626 token = G_TOKEN_NONE;
2629 case G_TOKEN_IDENTIFIER:
2633 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2634 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2635 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2638 g_scanner_get_next_token (scanner);
2640 if (!style || !lookup_color (style, scanner->value.v_identifier,
2643 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2644 scanner->value.v_identifier);
2645 token = G_TOKEN_IDENTIFIER;
2649 gstring = g_string_new (NULL);
2651 g_string_append_printf (gstring, " { %s, %s, %s }",
2652 g_ascii_formatd (rbuf, sizeof (rbuf),
2654 color.red / 65535.0),
2655 g_ascii_formatd (gbuf, sizeof (gbuf),
2657 color.green / 65535.0),
2658 g_ascii_formatd (bbuf, sizeof (bbuf),
2660 color.blue / 65535.0));
2662 g_value_init (&prop->value, G_TYPE_GSTRING);
2663 g_value_take_boxed (&prop->value, gstring);
2664 token = G_TOKEN_NONE;
2668 case G_TOKEN_LEFT_PAREN:
2669 case G_TOKEN_LEFT_CURLY:
2670 case G_TOKEN_LEFT_BRACE:
2673 GString *gstring = g_string_new (NULL);
2674 gboolean parse_on = TRUE;
2676 /* allow identifier(foobar) to support color expressions */
2677 if (token == G_TOKEN_IDENTIFIER)
2679 g_scanner_get_next_token (scanner);
2681 g_string_append_c (gstring, ' ');
2682 g_string_append (gstring, scanner->value.v_identifier);
2684 /* temporarily reset scanner mode to default, so we
2685 * don't peek the next token in a mode that only makes
2686 * sense in this function; because if anything but
2687 * G_TOKEN_LEFT_PAREN follows, the next token will be
2688 * parsed by our caller.
2690 * FIXME: right fix would be to call g_scanner_unget()
2691 * but that doesn't exist
2693 scanner->config->scan_identifier = scan_identifier;
2694 scanner->config->scan_symbols = scan_symbols;
2695 scanner->config->identifier_2_string = identifier_2_string;
2696 scanner->config->char_2_token = char_2_token;
2697 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2698 scanner->config->numbers_2_int = numbers_2_int;
2700 token = g_scanner_peek_next_token (scanner);
2702 /* restore adjusted scanner mode */
2703 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2704 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2705 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2706 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2707 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2708 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2710 if (token != G_TOKEN_LEFT_PAREN)
2712 token = G_TOKEN_NONE;
2718 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2720 if (token == G_TOKEN_NONE)
2722 g_string_append_c (gstring, ' ');
2723 g_value_init (&prop->value, G_TYPE_GSTRING);
2724 g_value_take_boxed (&prop->value, gstring);
2727 g_string_free (gstring, TRUE);
2732 g_scanner_get_next_token (scanner);
2733 token = G_TOKEN_INT;
2739 /* restore scanner mode */
2740 scanner->config->scan_identifier = scan_identifier;
2741 scanner->config->scan_symbols = scan_symbols;
2742 scanner->config->identifier_2_string = identifier_2_string;
2743 scanner->config->char_2_token = char_2_token;
2744 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2745 scanner->config->numbers_2_int = numbers_2_int;
2751 is_c_identifier (const gchar *string)
2754 gboolean is_varname;
2756 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2757 for (p = string + 1; *p && is_varname; p++)
2758 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2764 parse_include_file (GtkRcContext *context,
2766 const gchar *filename)
2768 char *to_parse = NULL;
2770 if (g_path_is_absolute (filename))
2772 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2773 * don't print an error in this case.
2775 to_parse = g_strdup (filename);
2779 /* if a relative path, we look relative to all the RC files in the
2780 * include stack. We require the file to be found in this case
2781 * so we can give meaningful error messages, and because on reparsing
2782 * non-absolute paths don't make sense.
2784 GSList *tmp_list = current_files_stack;
2787 GtkRcFile *curfile = tmp_list->data;
2788 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2790 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2798 tmp_list = tmp_list->next;
2804 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2809 g_scanner_warn (scanner,
2810 _("Unable to find include file: \"%s\""),
2817 gtk_rc_parse_statement (GtkRcContext *context,
2822 token = g_scanner_peek_next_token (scanner);
2825 case GTK_RC_TOKEN_INCLUDE:
2826 token = g_scanner_get_next_token (scanner);
2827 if (token != GTK_RC_TOKEN_INCLUDE)
2828 return GTK_RC_TOKEN_INCLUDE;
2829 token = g_scanner_get_next_token (scanner);
2830 if (token != G_TOKEN_STRING)
2831 return G_TOKEN_STRING;
2832 parse_include_file (context, scanner, scanner->value.v_string);
2833 return G_TOKEN_NONE;
2835 case GTK_RC_TOKEN_STYLE:
2836 return gtk_rc_parse_style (context, scanner);
2838 case GTK_RC_TOKEN_BINDING:
2839 return _gtk_binding_parse_binding (scanner);
2841 case GTK_RC_TOKEN_PIXMAP_PATH:
2842 return gtk_rc_parse_pixmap_path (context, scanner);
2844 case GTK_RC_TOKEN_WIDGET:
2845 return gtk_rc_parse_path_pattern (context, scanner);
2847 case GTK_RC_TOKEN_WIDGET_CLASS:
2848 return gtk_rc_parse_path_pattern (context, scanner);
2850 case GTK_RC_TOKEN_CLASS:
2851 return gtk_rc_parse_path_pattern (context, scanner);
2853 case GTK_RC_TOKEN_MODULE_PATH:
2854 return gtk_rc_parse_module_path (scanner);
2856 case GTK_RC_TOKEN_IM_MODULE_FILE:
2857 return gtk_rc_parse_im_module_file (scanner);
2859 case G_TOKEN_IDENTIFIER:
2860 if (is_c_identifier (scanner->next_value.v_identifier))
2862 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2865 g_scanner_get_next_token (scanner); /* eat identifier */
2866 name = g_strdup (scanner->value.v_identifier);
2868 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2869 if (token == G_TOKEN_NONE)
2871 GtkSettingsValue svalue;
2873 svalue.origin = prop.origin;
2874 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2875 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2876 _gtk_settings_set_property_value_from_rc (context->settings,
2880 g_free (prop.origin);
2881 if (G_VALUE_TYPE (&prop.value))
2882 g_value_unset (&prop.value);
2889 g_scanner_get_next_token (scanner);
2890 return G_TOKEN_IDENTIFIER;
2893 g_scanner_get_next_token (scanner);
2894 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2899 fixup_rc_set (GSList *list,
2905 GtkRcSet *set = list->data;
2906 if (set->rc_style == orig)
2907 set->rc_style = new;
2913 fixup_rc_sets (GtkRcContext *context,
2917 fixup_rc_set (context->rc_sets_widget, orig, new);
2918 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2919 fixup_rc_set (context->rc_sets_class, orig, new);
2923 gtk_rc_parse_style (GtkRcContext *context,
2926 GtkRcStyle *rc_style;
2927 GtkRcStyle *orig_style;
2928 GtkRcStyle *parent_style = NULL;
2929 GtkRcStylePrivate *rc_priv = NULL;
2932 GtkIconFactory *our_factory = NULL;
2933 GHashTable *our_hash = NULL;
2935 token = g_scanner_get_next_token (scanner);
2936 if (token != GTK_RC_TOKEN_STYLE)
2937 return GTK_RC_TOKEN_STYLE;
2939 token = g_scanner_get_next_token (scanner);
2940 if (token != G_TOKEN_STRING)
2941 return G_TOKEN_STRING;
2943 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2945 orig_style = g_object_ref (rc_style);
2951 rc_style = gtk_rc_style_new ();
2952 rc_style->name = g_strdup (scanner->value.v_string);
2954 for (i = 0; i < 5; i++)
2955 rc_style->bg_pixmap_name[i] = NULL;
2957 for (i = 0; i < 5; i++)
2958 rc_style->color_flags[i] = 0;
2961 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
2963 /* If there's a list, its first member is always the factory belonging
2966 if (rc_style->icon_factories)
2967 our_factory = rc_style->icon_factories->data;
2968 if (rc_priv->color_hashes)
2969 our_hash = rc_priv->color_hashes->data;
2971 token = g_scanner_peek_next_token (scanner);
2972 if (token == G_TOKEN_EQUAL_SIGN)
2974 token = g_scanner_get_next_token (scanner);
2976 token = g_scanner_get_next_token (scanner);
2977 if (token != G_TOKEN_STRING)
2979 token = G_TOKEN_STRING;
2983 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2986 for (i = 0; i < 5; i++)
2988 rc_style->color_flags[i] = parent_style->color_flags[i];
2989 rc_style->fg[i] = parent_style->fg[i];
2990 rc_style->bg[i] = parent_style->bg[i];
2991 rc_style->text[i] = parent_style->text[i];
2992 rc_style->base[i] = parent_style->base[i];
2995 rc_style->xthickness = parent_style->xthickness;
2996 rc_style->ythickness = parent_style->ythickness;
2998 if (parent_style->font_desc)
3000 if (rc_style->font_desc)
3001 pango_font_description_free (rc_style->font_desc);
3002 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3005 if (parent_style->rc_properties)
3009 for (i = 0; i < parent_style->rc_properties->len; i++)
3010 insert_rc_property (rc_style,
3011 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3015 for (i = 0; i < 5; i++)
3017 g_free (rc_style->bg_pixmap_name[i]);
3018 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3023 /* get icon_factories and color_hashes from the parent style;
3024 * if the parent_style doesn't have color_hashes, initializes
3025 * the color_hashes with the settings' color scheme (if it exists)
3027 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3029 if (rc_style->icon_factories)
3030 our_factory = rc_style->icon_factories->data;
3031 if (rc_priv->color_hashes)
3032 our_hash = rc_priv->color_hashes->data;
3034 token = g_scanner_get_next_token (scanner);
3035 if (token != G_TOKEN_LEFT_CURLY)
3037 token = G_TOKEN_LEFT_CURLY;
3041 token = g_scanner_peek_next_token (scanner);
3042 while (token != G_TOKEN_RIGHT_CURLY)
3046 case GTK_RC_TOKEN_BG:
3047 token = gtk_rc_parse_bg (scanner, rc_style);
3049 case GTK_RC_TOKEN_FG:
3050 token = gtk_rc_parse_fg (scanner, rc_style);
3052 case GTK_RC_TOKEN_TEXT:
3053 token = gtk_rc_parse_text (scanner, rc_style);
3055 case GTK_RC_TOKEN_BASE:
3056 token = gtk_rc_parse_base (scanner, rc_style);
3058 case GTK_RC_TOKEN_XTHICKNESS:
3059 token = gtk_rc_parse_xthickness (scanner, rc_style);
3061 case GTK_RC_TOKEN_YTHICKNESS:
3062 token = gtk_rc_parse_ythickness (scanner, rc_style);
3064 case GTK_RC_TOKEN_BG_PIXMAP:
3065 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3067 case GTK_RC_TOKEN_FONT:
3068 token = gtk_rc_parse_font (scanner, rc_style);
3070 case GTK_RC_TOKEN_FONTSET:
3071 token = gtk_rc_parse_fontset (scanner, rc_style);
3073 case GTK_RC_TOKEN_FONT_NAME:
3074 token = gtk_rc_parse_font_name (scanner, rc_style);
3076 case GTK_RC_TOKEN_ENGINE:
3077 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3079 case GTK_RC_TOKEN_STOCK:
3080 if (our_factory == NULL)
3081 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3082 our_factory = rc_style->icon_factories->data;
3083 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3085 case GTK_RC_TOKEN_COLOR:
3086 if (our_hash == NULL)
3088 gtk_rc_style_prepend_empty_color_hash (rc_style);
3089 our_hash = rc_priv->color_hashes->data;
3091 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3093 case G_TOKEN_IDENTIFIER:
3094 if (is_c_identifier (scanner->next_value.v_identifier))
3096 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3099 g_scanner_get_next_token (scanner); /* eat type name */
3100 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3101 if (g_scanner_get_next_token (scanner) != ':' ||
3102 g_scanner_get_next_token (scanner) != ':')
3107 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3108 !is_c_identifier (scanner->value.v_identifier))
3110 token = G_TOKEN_IDENTIFIER;
3114 /* it's important that we do the same canonification as GParamSpecPool here */
3115 name = g_strdup (scanner->value.v_identifier);
3116 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3117 prop.property_name = g_quark_from_string (name);
3120 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3121 if (token == G_TOKEN_NONE)
3123 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3124 insert_rc_property (rc_style, &prop, TRUE);
3127 g_free (prop.origin);
3128 if (G_VALUE_TYPE (&prop.value))
3129 g_value_unset (&prop.value);
3133 g_scanner_get_next_token (scanner);
3134 token = G_TOKEN_IDENTIFIER;
3138 g_scanner_get_next_token (scanner);
3139 token = G_TOKEN_RIGHT_CURLY;
3143 if (token != G_TOKEN_NONE)
3146 token = g_scanner_peek_next_token (scanner);
3147 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3149 token = g_scanner_get_next_token (scanner);
3150 if (token != G_TOKEN_RIGHT_CURLY)
3152 token = G_TOKEN_RIGHT_CURLY;
3156 if (rc_style != orig_style)
3158 if (!context->rc_style_ht)
3159 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3160 (GEqualFunc) gtk_rc_style_equal);
3162 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3164 /* If we copied the data into a new rc style, fix up references to the old rc style
3165 * in bindings that we have.
3168 fixup_rc_sets (context, orig_style, rc_style);
3172 g_object_unref (orig_style);
3174 return G_TOKEN_NONE;
3177 if (rc_style != orig_style)
3178 g_object_unref (rc_style);
3181 g_object_unref (orig_style);
3186 const GtkRcProperty*
3187 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3189 GQuark property_name)
3191 GtkRcProperty *node = NULL;
3193 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3195 if (rc_style->rc_properties)
3199 key.type_name = type_name;
3200 key.property_name = property_name;
3202 node = bsearch (&key,
3203 rc_style->rc_properties->data, rc_style->rc_properties->len,
3204 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3211 gtk_rc_parse_bg (GScanner *scanner,
3217 token = g_scanner_get_next_token (scanner);
3218 if (token != GTK_RC_TOKEN_BG)
3219 return GTK_RC_TOKEN_BG;
3221 token = gtk_rc_parse_state (scanner, &state);
3222 if (token != G_TOKEN_NONE)
3225 token = g_scanner_get_next_token (scanner);
3226 if (token != G_TOKEN_EQUAL_SIGN)
3227 return G_TOKEN_EQUAL_SIGN;
3229 style->color_flags[state] |= GTK_RC_BG;
3230 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3234 gtk_rc_parse_fg (GScanner *scanner,
3240 token = g_scanner_get_next_token (scanner);
3241 if (token != GTK_RC_TOKEN_FG)
3242 return GTK_RC_TOKEN_FG;
3244 token = gtk_rc_parse_state (scanner, &state);
3245 if (token != G_TOKEN_NONE)
3248 token = g_scanner_get_next_token (scanner);
3249 if (token != G_TOKEN_EQUAL_SIGN)
3250 return G_TOKEN_EQUAL_SIGN;
3252 style->color_flags[state] |= GTK_RC_FG;
3253 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3257 gtk_rc_parse_text (GScanner *scanner,
3263 token = g_scanner_get_next_token (scanner);
3264 if (token != GTK_RC_TOKEN_TEXT)
3265 return GTK_RC_TOKEN_TEXT;
3267 token = gtk_rc_parse_state (scanner, &state);
3268 if (token != G_TOKEN_NONE)
3271 token = g_scanner_get_next_token (scanner);
3272 if (token != G_TOKEN_EQUAL_SIGN)
3273 return G_TOKEN_EQUAL_SIGN;
3275 style->color_flags[state] |= GTK_RC_TEXT;
3276 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3280 gtk_rc_parse_base (GScanner *scanner,
3286 token = g_scanner_get_next_token (scanner);
3287 if (token != GTK_RC_TOKEN_BASE)
3288 return GTK_RC_TOKEN_BASE;
3290 token = gtk_rc_parse_state (scanner, &state);
3291 if (token != G_TOKEN_NONE)
3294 token = g_scanner_get_next_token (scanner);
3295 if (token != G_TOKEN_EQUAL_SIGN)
3296 return G_TOKEN_EQUAL_SIGN;
3298 style->color_flags[state] |= GTK_RC_BASE;
3299 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3303 gtk_rc_parse_xthickness (GScanner *scanner,
3306 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3307 return GTK_RC_TOKEN_XTHICKNESS;
3309 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3310 return G_TOKEN_EQUAL_SIGN;
3312 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3315 style->xthickness = scanner->value.v_int;
3317 return G_TOKEN_NONE;
3321 gtk_rc_parse_ythickness (GScanner *scanner,
3324 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3325 return GTK_RC_TOKEN_YTHICKNESS;
3327 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3328 return G_TOKEN_EQUAL_SIGN;
3330 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3333 style->ythickness = scanner->value.v_int;
3335 return G_TOKEN_NONE;
3339 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3341 GtkRcStyle *rc_style)
3347 token = g_scanner_get_next_token (scanner);
3348 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3349 return GTK_RC_TOKEN_BG_PIXMAP;
3351 token = gtk_rc_parse_state (scanner, &state);
3352 if (token != G_TOKEN_NONE)
3355 token = g_scanner_get_next_token (scanner);
3356 if (token != G_TOKEN_EQUAL_SIGN)
3357 return G_TOKEN_EQUAL_SIGN;
3359 token = g_scanner_get_next_token (scanner);
3360 if (token != G_TOKEN_STRING)
3361 return G_TOKEN_STRING;
3363 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3364 (strcmp (scanner->value.v_string, "<none>") == 0))
3365 pixmap_file = g_strdup (scanner->value.v_string);
3367 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3368 scanner, scanner->value.v_string);
3372 g_free (rc_style->bg_pixmap_name[state]);
3373 rc_style->bg_pixmap_name[state] = pixmap_file;
3376 return G_TOKEN_NONE;
3380 gtk_rc_check_pixmap_dir (const gchar *dir,
3381 const gchar *pixmap_file)
3385 buf = g_build_filename (dir, pixmap_file, NULL);
3387 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3396 * gtk_rc_find_pixmap_in_path:
3397 * @settings: a #GtkSettings
3398 * @scanner: Scanner used to get line number information for the
3399 * warning message, or %NULL
3400 * @pixmap_file: name of the pixmap file to locate.
3402 * Looks up a file in pixmap path for the specified #GtkSettings.
3403 * If the file is not found, it outputs a warning message using
3404 * g_warning() and returns %NULL.
3406 * Return value: the filename.
3409 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3411 const gchar *pixmap_file)
3417 GtkRcContext *context = gtk_rc_context_get (settings);
3419 if (context->pixmap_path)
3420 for (i = 0; context->pixmap_path[i] != NULL; i++)
3422 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3427 tmp_list = current_files_stack;
3430 GtkRcFile *curfile = tmp_list->data;
3431 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3435 tmp_list = tmp_list->next;
3439 g_scanner_warn (scanner,
3440 _("Unable to locate image file in pixmap_path: \"%s\""),
3443 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3450 * gtk_rc_find_module_in_path:
3451 * @module_file: name of a theme engine
3453 * Searches for a theme engine in the GTK+ search path. This function
3454 * is not useful for applications and should not be used.
3456 * Return value: The filename, if found (must be freed with g_free()),
3460 gtk_rc_find_module_in_path (const gchar *module_file)
3462 return _gtk_find_module (module_file, "engines");
3466 gtk_rc_parse_font (GScanner *scanner,
3467 GtkRcStyle *rc_style)
3471 token = g_scanner_get_next_token (scanner);
3472 if (token != GTK_RC_TOKEN_FONT)
3473 return GTK_RC_TOKEN_FONT;
3475 token = g_scanner_get_next_token (scanner);
3476 if (token != G_TOKEN_EQUAL_SIGN)
3477 return G_TOKEN_EQUAL_SIGN;
3479 token = g_scanner_get_next_token (scanner);
3480 if (token != G_TOKEN_STRING)
3481 return G_TOKEN_STRING;
3483 /* Ignore, do nothing */
3485 return G_TOKEN_NONE;
3489 gtk_rc_parse_fontset (GScanner *scanner,
3490 GtkRcStyle *rc_style)
3494 token = g_scanner_get_next_token (scanner);
3495 if (token != GTK_RC_TOKEN_FONTSET)
3496 return GTK_RC_TOKEN_FONTSET;
3498 token = g_scanner_get_next_token (scanner);
3499 if (token != G_TOKEN_EQUAL_SIGN)
3500 return G_TOKEN_EQUAL_SIGN;
3502 token = g_scanner_get_next_token (scanner);
3503 if (token != G_TOKEN_STRING)
3504 return G_TOKEN_STRING;
3506 /* Do nothing - silently ignore */
3508 return G_TOKEN_NONE;
3512 gtk_rc_parse_font_name (GScanner *scanner,
3513 GtkRcStyle *rc_style)
3517 token = g_scanner_get_next_token (scanner);
3518 if (token != GTK_RC_TOKEN_FONT_NAME)
3519 return GTK_RC_TOKEN_FONT;
3521 token = g_scanner_get_next_token (scanner);
3522 if (token != G_TOKEN_EQUAL_SIGN)
3523 return G_TOKEN_EQUAL_SIGN;
3525 token = g_scanner_get_next_token (scanner);
3526 if (token != G_TOKEN_STRING)
3527 return G_TOKEN_STRING;
3529 if (rc_style->font_desc)
3530 pango_font_description_free (rc_style->font_desc);
3532 rc_style->font_desc =
3533 pango_font_description_from_string (scanner->value.v_string);
3535 return G_TOKEN_NONE;
3539 gtk_rc_parse_engine (GtkRcContext *context,
3541 GtkRcStyle **rc_style)
3544 GtkThemeEngine *engine;
3545 guint result = G_TOKEN_NONE;
3546 GtkRcStyle *new_style = NULL;
3547 gboolean parsed_curlies = FALSE;
3548 GtkRcStylePrivate *rc_priv, *new_priv;
3550 token = g_scanner_get_next_token (scanner);
3551 if (token != GTK_RC_TOKEN_ENGINE)
3552 return GTK_RC_TOKEN_ENGINE;
3554 token = g_scanner_get_next_token (scanner);
3555 if (token != G_TOKEN_STRING)
3556 return G_TOKEN_STRING;
3558 if (!scanner->value.v_string[0])
3560 /* Support engine "" {} to mean override to the default engine
3562 token = g_scanner_get_next_token (scanner);
3563 if (token != G_TOKEN_LEFT_CURLY)
3564 return G_TOKEN_LEFT_CURLY;
3566 token = g_scanner_get_next_token (scanner);
3567 if (token != G_TOKEN_RIGHT_CURLY)
3568 return G_TOKEN_RIGHT_CURLY;
3570 parsed_curlies = TRUE;
3572 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3574 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3576 new_style = gtk_rc_style_new ();
3577 gtk_rc_style_real_merge (new_style, *rc_style);
3579 new_style->name = g_strdup ((*rc_style)->name);
3581 /* take over icon factories and color hashes
3582 * from the to-be-deleted style
3584 new_style->icon_factories = (*rc_style)->icon_factories;
3585 (*rc_style)->icon_factories = NULL;
3586 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3587 new_priv->color_hashes = rc_priv->color_hashes;
3588 rc_priv->color_hashes = NULL;
3591 (*rc_style)->engine_specified = TRUE;
3595 engine = gtk_theme_engine_get (scanner->value.v_string);
3597 token = g_scanner_get_next_token (scanner);
3598 if (token != G_TOKEN_LEFT_CURLY)
3599 return G_TOKEN_LEFT_CURLY;
3603 GtkRcStyleClass *new_class;
3605 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3606 new_style = gtk_theme_engine_create_rc_style (engine);
3607 g_type_module_unuse (G_TYPE_MODULE (engine));
3609 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3611 new_class->merge (new_style, *rc_style);
3613 new_style->name = g_strdup ((*rc_style)->name);
3615 /* take over icon factories and color hashes
3616 * from the to-be-deleted style
3618 new_style->icon_factories = (*rc_style)->icon_factories;
3619 (*rc_style)->icon_factories = NULL;
3620 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3621 new_priv->color_hashes = rc_priv->color_hashes;
3622 rc_priv->color_hashes = NULL;
3624 if (new_class->parse)
3626 parsed_curlies = TRUE;
3627 result = new_class->parse (new_style, context->settings, scanner);
3629 if (result != G_TOKEN_NONE)
3631 /* copy icon factories and color hashes back
3633 (*rc_style)->icon_factories = new_style->icon_factories;
3634 new_style->icon_factories = NULL;
3635 rc_priv->color_hashes = new_priv->color_hashes;
3636 new_priv->color_hashes = NULL;
3638 g_object_unref (new_style);
3645 if (!parsed_curlies)
3647 /* Skip over remainder, looking for nested {}'s
3651 result = G_TOKEN_RIGHT_CURLY;
3652 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3654 if (token == G_TOKEN_LEFT_CURLY)
3656 else if (token == G_TOKEN_RIGHT_CURLY)
3661 result = G_TOKEN_NONE;
3669 new_style->engine_specified = TRUE;
3671 g_object_unref (*rc_style);
3672 *rc_style = new_style;
3679 gtk_rc_parse_state (GScanner *scanner,
3680 GtkStateType *state)
3685 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3686 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3688 /* we don't know where we got called from, so we reset the scope here.
3689 * if we bail out due to errors, we *don't* reset the scope, so the
3690 * error messaging code can make sense of our tokens.
3692 old_scope = g_scanner_set_scope (scanner, 0);
3694 token = g_scanner_get_next_token (scanner);
3695 if (token != G_TOKEN_LEFT_BRACE)
3696 return G_TOKEN_LEFT_BRACE;
3698 token = g_scanner_get_next_token (scanner);
3701 case GTK_RC_TOKEN_ACTIVE:
3702 *state = GTK_STATE_ACTIVE;
3704 case GTK_RC_TOKEN_INSENSITIVE:
3705 *state = GTK_STATE_INSENSITIVE;
3707 case GTK_RC_TOKEN_NORMAL:
3708 *state = GTK_STATE_NORMAL;
3710 case GTK_RC_TOKEN_PRELIGHT:
3711 *state = GTK_STATE_PRELIGHT;
3713 case GTK_RC_TOKEN_SELECTED:
3714 *state = GTK_STATE_SELECTED;
3717 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3720 token = g_scanner_get_next_token (scanner);
3721 if (token != G_TOKEN_RIGHT_BRACE)
3722 return G_TOKEN_RIGHT_BRACE;
3724 g_scanner_set_scope (scanner, old_scope);
3726 return G_TOKEN_NONE;
3730 gtk_rc_parse_priority (GScanner *scanner,
3731 GtkPathPriorityType *priority)
3736 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3737 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3739 /* we don't know where we got called from, so we reset the scope here.
3740 * if we bail out due to errors, we *don't* reset the scope, so the
3741 * error messaging code can make sense of our tokens.
3743 old_scope = g_scanner_set_scope (scanner, 0);
3745 token = g_scanner_get_next_token (scanner);
3749 token = g_scanner_get_next_token (scanner);
3752 case GTK_RC_TOKEN_LOWEST:
3753 *priority = GTK_PATH_PRIO_LOWEST;
3755 case GTK_RC_TOKEN_GTK:
3756 *priority = GTK_PATH_PRIO_GTK;
3758 case GTK_RC_TOKEN_APPLICATION:
3759 *priority = GTK_PATH_PRIO_APPLICATION;
3761 case GTK_RC_TOKEN_THEME:
3762 *priority = GTK_PATH_PRIO_THEME;
3764 case GTK_RC_TOKEN_RC:
3765 *priority = GTK_PATH_PRIO_RC;
3767 case GTK_RC_TOKEN_HIGHEST:
3768 *priority = GTK_PATH_PRIO_HIGHEST;
3771 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3774 g_scanner_set_scope (scanner, old_scope);
3776 return G_TOKEN_NONE;
3780 * gtk_rc_parse_color:
3781 * @scanner: a #GScanner
3782 * @color: a pointer to a #GdkColor structure in which to store the result
3784 * Parses a color in the <link linkend="color=format">format</link> expected
3787 * Note that theme engines should use gtk_rc_parse_color_full() in
3788 * order to support symbolic colors.
3790 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3791 * that was expected but not found
3794 gtk_rc_parse_color (GScanner *scanner,
3797 return gtk_rc_parse_color_full (scanner, NULL, color);
3801 * gtk_rc_parse_color_full:
3802 * @scanner: a #GScanner
3803 * @style: (allow-none): a #GtkRcStyle, or %NULL
3804 * @color: a pointer to a #GdkColor structure in which to store the result
3806 * Parses a color in the <link linkend="color=format">format</link> expected
3807 * in a RC file. If @style is not %NULL, it will be consulted to resolve
3808 * references to symbolic colors.
3810 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3811 * that was expected but not found
3816 gtk_rc_parse_color_full (GScanner *scanner,
3822 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3824 /* we don't need to set our own scope here, because
3825 * we don't need own symbols
3828 token = g_scanner_get_next_token (scanner);
3836 case G_TOKEN_LEFT_CURLY:
3837 token = g_scanner_get_next_token (scanner);
3838 if (token == G_TOKEN_INT)
3839 token_int = scanner->value.v_int;
3840 else if (token == G_TOKEN_FLOAT)
3841 token_int = scanner->value.v_float * 65535.0;
3843 return G_TOKEN_FLOAT;
3844 color->red = CLAMP (token_int, 0, 65535);
3846 token = g_scanner_get_next_token (scanner);
3847 if (token != G_TOKEN_COMMA)
3848 return G_TOKEN_COMMA;
3850 token = g_scanner_get_next_token (scanner);
3851 if (token == G_TOKEN_INT)
3852 token_int = scanner->value.v_int;
3853 else if (token == G_TOKEN_FLOAT)
3854 token_int = scanner->value.v_float * 65535.0;
3856 return G_TOKEN_FLOAT;
3857 color->green = CLAMP (token_int, 0, 65535);
3859 token = g_scanner_get_next_token (scanner);
3860 if (token != G_TOKEN_COMMA)
3861 return G_TOKEN_COMMA;
3863 token = g_scanner_get_next_token (scanner);
3864 if (token == G_TOKEN_INT)
3865 token_int = scanner->value.v_int;
3866 else if (token == G_TOKEN_FLOAT)
3867 token_int = scanner->value.v_float * 65535.0;
3869 return G_TOKEN_FLOAT;
3870 color->blue = CLAMP (token_int, 0, 65535);
3872 token = g_scanner_get_next_token (scanner);
3873 if (token != G_TOKEN_RIGHT_CURLY)
3874 return G_TOKEN_RIGHT_CURLY;
3875 return G_TOKEN_NONE;
3877 case G_TOKEN_STRING:
3878 if (!gdk_color_parse (scanner->value.v_string, color))
3880 g_scanner_warn (scanner, "Invalid color constant '%s'",
3881 scanner->value.v_string);
3882 return G_TOKEN_STRING;
3884 return G_TOKEN_NONE;
3887 token = g_scanner_get_next_token (scanner);
3888 if (token != G_TOKEN_IDENTIFIER)
3889 return G_TOKEN_IDENTIFIER;
3891 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3893 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3894 scanner->value.v_identifier);
3895 return G_TOKEN_IDENTIFIER;
3898 return G_TOKEN_NONE;
3900 case G_TOKEN_IDENTIFIER:
3901 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3903 token = g_scanner_get_next_token (scanner);
3904 if (token != G_TOKEN_LEFT_PAREN)
3905 return G_TOKEN_LEFT_PAREN;
3908 if (g_scanner_peek_next_token (scanner) == '-')
3910 g_scanner_get_next_token (scanner); /* eat sign */
3914 token = g_scanner_get_next_token (scanner);
3915 if (token != G_TOKEN_FLOAT)
3916 return G_TOKEN_FLOAT;
3918 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3920 token = g_scanner_get_next_token (scanner);
3921 if (token != G_TOKEN_COMMA)
3922 return G_TOKEN_COMMA;
3924 token = gtk_rc_parse_color_full (scanner, style, &c1);
3925 if (token != G_TOKEN_NONE)
3928 token = g_scanner_get_next_token (scanner);
3929 if (token != G_TOKEN_COMMA)
3930 return G_TOKEN_COMMA;
3932 token = gtk_rc_parse_color_full (scanner, style, &c2);
3933 if (token != G_TOKEN_NONE)
3936 token = g_scanner_get_next_token (scanner);
3937 if (token != G_TOKEN_RIGHT_PAREN)
3938 return G_TOKEN_RIGHT_PAREN;
3940 color->red = l * c1.red + (1.0 - l) * c2.red;
3941 color->green = l * c1.green + (1.0 - l) * c2.green;
3942 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3944 return G_TOKEN_NONE;
3946 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
3948 token = g_scanner_get_next_token (scanner);
3949 if (token != G_TOKEN_LEFT_PAREN)
3950 return G_TOKEN_LEFT_PAREN;
3953 if (g_scanner_peek_next_token (scanner) == '-')
3955 g_scanner_get_next_token (scanner); /* eat sign */
3959 token = g_scanner_get_next_token (scanner);
3960 if (token != G_TOKEN_FLOAT)
3961 return G_TOKEN_FLOAT;
3963 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3965 token = g_scanner_get_next_token (scanner);
3966 if (token != G_TOKEN_COMMA)
3967 return G_TOKEN_COMMA;
3969 token = gtk_rc_parse_color_full (scanner, style, &c1);
3970 if (token != G_TOKEN_NONE)
3973 token = g_scanner_get_next_token (scanner);
3974 if (token != G_TOKEN_RIGHT_PAREN)
3975 return G_TOKEN_RIGHT_PAREN;
3977 _gtk_style_shade (&c1, color, l);
3979 return G_TOKEN_NONE;
3981 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
3982 strcmp (scanner->value.v_identifier, "darker") == 0)
3984 if (scanner->value.v_identifier[0] == 'l')
3989 token = g_scanner_get_next_token (scanner);
3990 if (token != G_TOKEN_LEFT_PAREN)
3991 return G_TOKEN_LEFT_PAREN;
3993 token = gtk_rc_parse_color_full (scanner, style, &c1);
3994 if (token != G_TOKEN_NONE)
3997 token = g_scanner_get_next_token (scanner);
3998 if (token != G_TOKEN_RIGHT_PAREN)
3999 return G_TOKEN_RIGHT_PAREN;
4001 _gtk_style_shade (&c1, color, l);
4003 return G_TOKEN_NONE;
4006 return G_TOKEN_IDENTIFIER;
4009 return G_TOKEN_STRING;
4014 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4019 token = g_scanner_get_next_token (scanner);
4020 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4021 return GTK_RC_TOKEN_PIXMAP_PATH;
4023 token = g_scanner_get_next_token (scanner);
4024 if (token != G_TOKEN_STRING)
4025 return G_TOKEN_STRING;
4027 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4029 return G_TOKEN_NONE;
4033 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4035 const gchar *pix_path)
4037 g_strfreev (context->pixmap_path);
4038 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4042 gtk_rc_parse_module_path (GScanner *scanner)
4046 token = g_scanner_get_next_token (scanner);
4047 if (token != GTK_RC_TOKEN_MODULE_PATH)
4048 return GTK_RC_TOKEN_MODULE_PATH;
4050 token = g_scanner_get_next_token (scanner);
4051 if (token != G_TOKEN_STRING)
4052 return G_TOKEN_STRING;
4054 g_warning ("module_path directive is now ignored\n");
4056 return G_TOKEN_NONE;
4060 gtk_rc_parse_im_module_file (GScanner *scanner)
4064 token = g_scanner_get_next_token (scanner);
4065 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4066 return GTK_RC_TOKEN_IM_MODULE_FILE;
4068 token = g_scanner_get_next_token (scanner);
4069 if (token != G_TOKEN_STRING)
4070 return G_TOKEN_STRING;
4072 g_free (im_module_file);
4074 im_module_file = g_strdup (scanner->value.v_string);
4076 return G_TOKEN_NONE;
4080 gtk_rc_parse_path_pattern (GtkRcContext *context,
4084 GtkPathType path_type;
4086 gboolean is_binding;
4087 GtkPathPriorityType priority = context->default_priority;
4089 token = g_scanner_get_next_token (scanner);
4092 case GTK_RC_TOKEN_WIDGET:
4093 path_type = GTK_PATH_WIDGET;
4095 case GTK_RC_TOKEN_WIDGET_CLASS:
4096 path_type = GTK_PATH_WIDGET_CLASS;
4098 case GTK_RC_TOKEN_CLASS:
4099 path_type = GTK_PATH_CLASS;
4102 return GTK_RC_TOKEN_WIDGET_CLASS;
4105 token = g_scanner_get_next_token (scanner);
4106 if (token != G_TOKEN_STRING)
4107 return G_TOKEN_STRING;
4109 pattern = g_strdup (scanner->value.v_string);
4111 token = g_scanner_get_next_token (scanner);
4112 if (token == GTK_RC_TOKEN_STYLE)
4114 else if (token == GTK_RC_TOKEN_BINDING)
4119 return GTK_RC_TOKEN_STYLE;
4122 if (g_scanner_peek_next_token (scanner) == ':')
4124 token = gtk_rc_parse_priority (scanner, &priority);
4125 if (token != G_TOKEN_NONE)
4132 token = g_scanner_get_next_token (scanner);
4133 if (token != G_TOKEN_STRING)
4136 return G_TOKEN_STRING;
4141 GtkBindingSet *binding;
4143 binding = gtk_binding_set_find (scanner->value.v_string);
4147 return G_TOKEN_STRING;
4149 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4153 GtkRcStyle *rc_style;
4156 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4161 return G_TOKEN_STRING;
4164 rc_set = g_new (GtkRcSet, 1);
4165 rc_set->type = path_type;
4167 if (path_type == GTK_PATH_WIDGET_CLASS)
4169 rc_set->pspec = NULL;
4170 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4174 rc_set->pspec = g_pattern_spec_new (pattern);
4175 rc_set->path = NULL;
4178 rc_set->rc_style = rc_style;
4179 rc_set->priority = priority;
4181 if (path_type == GTK_PATH_WIDGET)
4182 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4183 else if (path_type == GTK_PATH_WIDGET_CLASS)
4184 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4186 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4190 return G_TOKEN_NONE;
4194 gtk_rc_parse_hash_key (GScanner *scanner,
4199 token = g_scanner_get_next_token (scanner);
4200 if (token != G_TOKEN_LEFT_BRACE)
4201 return G_TOKEN_LEFT_BRACE;
4203 token = g_scanner_get_next_token (scanner);
4205 if (token != G_TOKEN_STRING)
4206 return G_TOKEN_STRING;
4208 *hash_key = g_strdup (scanner->value.v_string);
4210 token = g_scanner_get_next_token (scanner);
4211 if (token != G_TOKEN_RIGHT_BRACE)
4214 return G_TOKEN_RIGHT_BRACE;
4217 return G_TOKEN_NONE;
4221 gtk_rc_parse_icon_source (GtkRcContext *context,
4223 GtkIconSet *icon_set,
4224 gboolean *icon_set_valid)
4227 gchar *full_filename;
4228 GtkIconSource *source = NULL;
4230 token = g_scanner_get_next_token (scanner);
4231 if (token != G_TOKEN_LEFT_CURLY)
4232 return G_TOKEN_LEFT_CURLY;
4234 token = g_scanner_get_next_token (scanner);
4236 if (token != G_TOKEN_STRING && token != '@')
4237 return G_TOKEN_STRING;
4239 if (token == G_TOKEN_STRING)
4243 source = gtk_icon_source_new ();
4244 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4247 gtk_icon_source_set_filename (source, full_filename);
4248 g_free (full_filename);
4255 token = g_scanner_get_next_token (scanner);
4257 if (token != G_TOKEN_STRING)
4258 return G_TOKEN_STRING;
4260 source = gtk_icon_source_new ();
4261 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4264 /* We continue parsing even if we didn't find the pixmap so that rest of the
4265 * file is read, even if the syntax is bad. However we don't validate the
4266 * icon_set so the caller can choose not to install it.
4268 token = g_scanner_get_next_token (scanner);
4270 if (token == G_TOKEN_RIGHT_CURLY)
4272 else if (token != G_TOKEN_COMMA)
4274 gtk_icon_source_free (source);
4275 return G_TOKEN_COMMA;
4278 /* Get the direction */
4280 token = g_scanner_get_next_token (scanner);
4284 case GTK_RC_TOKEN_RTL:
4285 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4286 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4289 case GTK_RC_TOKEN_LTR:
4290 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4291 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4298 gtk_icon_source_free (source);
4299 return GTK_RC_TOKEN_RTL;
4303 token = g_scanner_get_next_token (scanner);
4305 if (token == G_TOKEN_RIGHT_CURLY)
4307 else if (token != G_TOKEN_COMMA)
4309 gtk_icon_source_free (source);
4310 return G_TOKEN_COMMA;
4315 token = g_scanner_get_next_token (scanner);
4319 case GTK_RC_TOKEN_NORMAL:
4320 gtk_icon_source_set_state_wildcarded (source, FALSE);
4321 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4324 case GTK_RC_TOKEN_PRELIGHT:
4325 gtk_icon_source_set_state_wildcarded (source, FALSE);
4326 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4330 case GTK_RC_TOKEN_INSENSITIVE:
4331 gtk_icon_source_set_state_wildcarded (source, FALSE);
4332 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4335 case GTK_RC_TOKEN_ACTIVE:
4336 gtk_icon_source_set_state_wildcarded (source, FALSE);
4337 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4340 case GTK_RC_TOKEN_SELECTED:
4341 gtk_icon_source_set_state_wildcarded (source, FALSE);
4342 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4349 gtk_icon_source_free (source);
4350 return GTK_RC_TOKEN_PRELIGHT;
4354 token = g_scanner_get_next_token (scanner);
4356 if (token == G_TOKEN_RIGHT_CURLY)
4358 else if (token != G_TOKEN_COMMA)
4360 gtk_icon_source_free (source);
4361 return G_TOKEN_COMMA;
4366 token = g_scanner_get_next_token (scanner);
4372 if (token != G_TOKEN_STRING)
4374 gtk_icon_source_free (source);
4375 return G_TOKEN_STRING;
4378 size = gtk_icon_size_from_name (scanner->value.v_string);
4380 if (size != GTK_ICON_SIZE_INVALID)
4382 gtk_icon_source_set_size_wildcarded (source, FALSE);
4383 gtk_icon_source_set_size (source, size);
4387 /* Check the close brace */
4389 token = g_scanner_get_next_token (scanner);
4390 if (token != G_TOKEN_RIGHT_CURLY)
4392 gtk_icon_source_free (source);
4393 return G_TOKEN_RIGHT_CURLY;
4397 if (gtk_icon_source_get_filename (source) ||
4398 gtk_icon_source_get_icon_name (source))
4400 gtk_icon_set_add_source (icon_set, source);
4401 *icon_set_valid = TRUE;
4403 gtk_icon_source_free (source);
4405 return G_TOKEN_NONE;
4409 gtk_rc_parse_stock (GtkRcContext *context,
4411 GtkRcStyle *rc_style,
4412 GtkIconFactory *factory)
4414 GtkIconSet *icon_set = NULL;
4415 gboolean icon_set_valid = FALSE;
4416 gchar *stock_id = NULL;
4419 token = g_scanner_get_next_token (scanner);
4420 if (token != GTK_RC_TOKEN_STOCK)
4421 return GTK_RC_TOKEN_STOCK;
4423 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4424 if (token != G_TOKEN_NONE)
4427 token = g_scanner_get_next_token (scanner);
4428 if (token != G_TOKEN_EQUAL_SIGN)
4431 return G_TOKEN_EQUAL_SIGN;
4434 token = g_scanner_get_next_token (scanner);
4435 if (token != G_TOKEN_LEFT_CURLY)
4438 return G_TOKEN_LEFT_CURLY;
4441 token = g_scanner_peek_next_token (scanner);
4442 while (token != G_TOKEN_RIGHT_CURLY)
4444 if (icon_set == NULL)
4445 icon_set = gtk_icon_set_new ();
4447 token = gtk_rc_parse_icon_source (context,
4448 scanner, icon_set, &icon_set_valid);
4449 if (token != G_TOKEN_NONE)
4452 gtk_icon_set_unref (icon_set);
4456 token = g_scanner_get_next_token (scanner);
4458 if (token != G_TOKEN_COMMA &&
4459 token != G_TOKEN_RIGHT_CURLY)
4462 gtk_icon_set_unref (icon_set);
4463 return G_TOKEN_RIGHT_CURLY;
4470 gtk_icon_factory_add (factory,
4474 gtk_icon_set_unref (icon_set);
4479 return G_TOKEN_NONE;
4483 gtk_rc_parse_logical_color (GScanner *scanner,
4484 GtkRcStyle *rc_style,
4487 gchar *color_id = NULL;
4491 token = g_scanner_get_next_token (scanner);
4492 if (token != GTK_RC_TOKEN_COLOR)
4493 return GTK_RC_TOKEN_COLOR;
4495 token = gtk_rc_parse_hash_key (scanner, &color_id);
4496 if (token != G_TOKEN_NONE)
4499 token = g_scanner_get_next_token (scanner);
4500 if (token != G_TOKEN_EQUAL_SIGN)
4503 return G_TOKEN_EQUAL_SIGN;
4506 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4507 if (token != G_TOKEN_NONE)
4513 /* Because the hash is created with destroy functions,
4514 * g_hash_table_insert will free any old values for us,
4515 * if a mapping with the specified key already exists.
4517 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4519 return G_TOKEN_NONE;
4524 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4528 const gchar *current;
4529 const gchar *class_start;
4530 const gchar *class_end;
4531 const gchar *pattern_end;
4532 const gchar *pattern_start;
4537 while ((class_start = strchr (current, '<')) &&
4538 (class_end = strchr (class_start, '>')))
4540 /* Add patterns, but ignore single dots */
4541 if (!(class_start == current ||
4542 (class_start == current + 1 && current[0] == '.')))
4544 pattern_end = class_start - 1;
4545 pattern_start = current;
4547 path_elt = g_new (PathElt, 1);
4549 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4550 path_elt->type = PATH_ELT_PSPEC;
4551 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4552 g_free (sub_pattern);
4554 result = g_slist_prepend (result, path_elt);
4557 path_elt = g_new (PathElt, 1);
4559 /* The < > need to be removed from the string. */
4560 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4562 path_elt->type = PATH_ELT_UNRESOLVED;
4563 path_elt->elt.class_name = sub_pattern;
4565 result = g_slist_prepend (result, path_elt);
4567 current = class_end + 1;
4570 /* Add the rest, if anything is left */
4571 if (strlen (current) > 0)
4573 path_elt = g_new (PathElt, 1);
4574 path_elt->type = PATH_ELT_PSPEC;
4575 path_elt->elt.pspec = g_pattern_spec_new (current);
4577 result = g_slist_prepend (result, path_elt);
4580 return g_slist_reverse (result);
4584 free_path_elt (gpointer data,
4587 PathElt *path_elt = data;
4589 switch (path_elt->type)
4591 case PATH_ELT_PSPEC:
4592 g_pattern_spec_free (path_elt->elt.pspec);
4594 case PATH_ELT_UNRESOLVED:
4595 g_free (path_elt->elt.class_name);
4600 g_assert_not_reached ();
4607 _gtk_rc_free_widget_class_path (GSList *list)
4609 g_slist_foreach (list, free_path_elt, NULL);
4610 g_slist_free (list);
4614 gtk_rc_set_free (GtkRcSet *rc_set)
4617 g_pattern_spec_free (rc_set->pspec);
4619 _gtk_rc_free_widget_class_path (rc_set->path);
4625 match_class (PathElt *path_elt,
4630 if (path_elt->type == PATH_ELT_UNRESOLVED)
4632 type = g_type_from_name (path_elt->elt.class_name);
4633 if (type != G_TYPE_INVALID)
4635 g_free (path_elt->elt.class_name);
4636 path_elt->elt.class_type = type;
4637 path_elt->type = PATH_ELT_TYPE;
4640 return g_str_equal (type_name, path_elt->elt.class_name);
4643 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4647 match_widget_class_recursive (GSList *list,
4650 gchar *path_reversed)
4654 /* break out if we cannot match anymore. */
4663 /* there are two possibilities:
4664 * 1. The next pattern should match the class.
4665 * 2. First normal matching, and then maybe a class */
4667 path_elt = list->data;
4669 if (path_elt->type != PATH_ELT_PSPEC)
4671 gchar *class_start = path;
4674 /* ignore leading dot */
4675 if (class_start[0] == '.')
4677 class_end = strchr (class_start, '.');
4679 if (class_end == NULL)
4681 if (!match_class (path_elt, class_start))
4684 return match_widget_class_recursive (list->next, 0, "", "");
4688 class_end[0] = '\0';
4689 if (!match_class (path_elt, class_start))
4697 gint new_length = length - (class_end - path);
4698 gchar old_char = path_reversed[new_length];
4702 path_reversed[new_length] = '\0';
4703 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4704 path_reversed[new_length] = old_char;
4715 gboolean result = FALSE;
4717 /* If there is nothing after this (ie. no class match),
4718 * just compare the pspec.
4720 if (list->next == NULL)
4721 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4723 class_elt = (PathElt *)list->next->data;
4724 g_assert (class_elt->type != PATH_ELT_PSPEC);
4727 if (class_start[0] == '.')
4732 class_end = strchr (class_start, '.');
4734 /* It should be cheaper to match the class first. (either the pattern
4735 * is simple, and will match most of the times, or it may be complex
4736 * and matching is slow)
4738 if (class_end == NULL)
4740 result = match_class (class_elt, class_start);
4744 class_end[0] = '\0';
4745 result = match_class (class_elt, class_start);
4754 /* terminate the string in front of the class. It does not matter
4755 * that the class becomes unusable, because it is not needed
4756 * inside the recursion
4758 old_char = class_start[0];
4759 class_start[0] = '\0';
4761 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4763 if (class_end != NULL)
4765 gint new_length = length - (class_end - path);
4766 gchar path_reversed_char = path_reversed[new_length];
4768 path_reversed[new_length] = '\0';
4770 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4772 path_reversed[new_length] = path_reversed_char;
4775 result = match_widget_class_recursive (list->next->next, 0, "", "");
4778 class_start[0] = old_char;
4784 /* get next class in path, or break out */
4785 if (class_end != NULL)
4786 class_start = class_end + 1;
4794 _gtk_rc_match_widget_class (GSList *list,
4797 gchar *path_reversed)
4799 return match_widget_class_recursive (list, length, path, path_reversed);
4802 #if defined (G_OS_WIN32) && !defined (_WIN64)
4804 /* DLL ABI stability backward compatibility versions */
4806 #undef gtk_rc_add_default_file
4809 gtk_rc_add_default_file (const gchar *filename)
4811 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4813 gtk_rc_add_default_file_utf8 (utf8_filename);
4815 g_free (utf8_filename);
4818 #undef gtk_rc_set_default_files
4821 gtk_rc_set_default_files (gchar **filenames)
4823 gchar **utf8_filenames;
4826 while (filenames[n++] != NULL)
4829 utf8_filenames = g_new (gchar *, n + 1);
4831 for (i = 0; i < n; i++)
4832 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4834 utf8_filenames[n] = NULL;
4836 gtk_rc_set_default_files_utf8 (utf8_filenames);
4838 g_strfreev (utf8_filenames);
4844 gtk_rc_parse (const gchar *filename)
4846 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4848 gtk_rc_parse_utf8 (utf8_filename);
4850 g_free (utf8_filename);
4855 #define __GTK_RC_C__
4856 #include "gtkaliasdef.c"