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 if (context->color_hash)
663 g_hash_table_unref (context->color_hash);
665 g_object_get (settings, "color-hash", &context->color_hash, NULL);
667 gtk_rc_reparse_all_for_settings (settings, TRUE);
670 static GtkRcContext *
671 gtk_rc_context_get (GtkSettings *settings)
673 if (!settings->rc_context)
675 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
677 context->settings = settings;
678 context->rc_style_ht = NULL;
679 context->rc_sets_widget = NULL;
680 context->rc_sets_widget_class = NULL;
681 context->rc_sets_class = NULL;
682 context->rc_files = NULL;
683 context->default_style = NULL;
684 context->reloading = FALSE;
686 g_object_get (settings,
687 "gtk-theme-name", &context->theme_name,
688 "gtk-key-theme-name", &context->key_theme_name,
689 "gtk-font-name", &context->font_name,
690 "color-hash", &context->color_hash,
693 g_signal_connect (settings,
694 "notify::gtk-theme-name",
695 G_CALLBACK (gtk_rc_settings_changed),
697 g_signal_connect (settings,
698 "notify::gtk-key-theme-name",
699 G_CALLBACK (gtk_rc_settings_changed),
701 g_signal_connect (settings,
702 "notify::gtk-font-name",
703 G_CALLBACK (gtk_rc_font_name_changed),
705 g_signal_connect (settings,
706 "notify::color-hash",
707 G_CALLBACK (gtk_rc_color_hash_changed),
710 context->pixmap_path = NULL;
712 context->default_priority = GTK_PATH_PRIO_RC;
714 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
717 return settings->rc_context;
721 gtk_rc_clear_rc_files (GtkRcContext *context)
725 list = context->rc_files;
728 GtkRcFile *rc_file = list->data;
730 if (rc_file->canonical_name != rc_file->name)
731 g_free (rc_file->canonical_name);
732 g_free (rc_file->directory);
733 g_free (rc_file->name);
739 g_slist_free (context->rc_files);
740 context->rc_files = NULL;
744 _gtk_rc_context_destroy (GtkSettings *settings)
746 GtkRcContext *context;
748 g_return_if_fail (GTK_IS_SETTINGS (settings));
750 context = settings->rc_context;
754 _gtk_settings_reset_rc_values (context->settings);
755 gtk_rc_clear_styles (context);
756 gtk_rc_clear_rc_files (context);
758 if (context->default_style)
759 g_object_unref (context->default_style);
761 g_strfreev (context->pixmap_path);
763 g_free (context->theme_name);
764 g_free (context->key_theme_name);
765 g_free (context->font_name);
767 if (context->color_hash)
768 g_hash_table_unref (context->color_hash);
770 g_signal_handlers_disconnect_by_func (settings,
771 gtk_rc_settings_changed, context);
772 g_signal_handlers_disconnect_by_func (settings,
773 gtk_rc_font_name_changed, context);
774 g_signal_handlers_disconnect_by_func (settings,
775 gtk_rc_color_hash_changed, context);
777 rc_contexts = g_slist_remove (rc_contexts, context);
781 settings->rc_context = NULL;
785 gtk_rc_parse_named (GtkRcContext *context,
790 const gchar *home_dir;
794 subpath = g_strconcat ("gtk-2.0-", type,
795 G_DIR_SEPARATOR_S "gtkrc",
798 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
800 /* First look in the users home directory
802 home_dir = g_get_home_dir ();
805 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
806 if (!g_file_test (path, G_FILE_TEST_EXISTS))
815 gchar *theme_dir = gtk_rc_get_theme_dir ();
816 path = g_build_filename (theme_dir, name, subpath, NULL);
819 if (!g_file_test (path, G_FILE_TEST_EXISTS))
828 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
836 gtk_rc_parse_default_files (GtkRcContext *context)
840 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
841 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
847 static gboolean initialized = FALSE;
853 gtk_rc_add_initial_default_files ();
856 /* Default RC string */
857 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
858 " bg[NORMAL] = \"#eee1b3\"\n"
859 " fg[NORMAL] = \"#000000\"\n"
862 "style \"gtk-default-progress-bar-style\" {\n"
863 " bg[PRELIGHT] = \"#4b6983\"\n"
864 " fg[PRELIGHT] = \"#ffffff\"\n"
865 " bg[NORMAL] = \"#c4c2bd\"\n"
868 "style \"gtk-default-menu-bar-item-style\" {\n"
869 " GtkMenuItem::horizontal_padding = 5\n"
872 "style \"gtk-default-menu-item-style\" {\n"
873 " bg[PRELIGHT] = \"#4b6983\"\n"
874 " fg[PRELIGHT] = \"#ffffff\"\n"
875 " base[PRELIGHT] = \"#4b6983\"\n"
876 " text[PRELIGHT] = \"#ffffff\"\n"
879 /* Work around clipping of accelerator underlines */
880 "style \"gtk-default-label-style\" {\n"
881 " GtkWidget::draw-border = {0,0,0,1}\n"
884 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
885 "widget \"gtk-tooltip*\" style : gtk \"gtk-default-tooltips-style\"\n"
886 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
887 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
888 "class \"GtkLabel\" style : gtk \"gtk-default-label-style\"\n"
893 gtk_rc_context_parse_string (GtkRcContext *context,
894 const gchar *rc_string)
896 gtk_rc_parse_any (context, "-", -1, rc_string);
900 gtk_rc_parse_string (const gchar *rc_string)
905 g_return_if_fail (rc_string != NULL);
907 rc_file = g_new (GtkRcFile, 1);
908 rc_file->is_string = TRUE;
909 rc_file->name = g_strdup (rc_string);
910 rc_file->canonical_name = NULL;
911 rc_file->directory = NULL;
913 rc_file->reload = TRUE;
915 global_rc_files = g_slist_append (global_rc_files, rc_file);
917 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
918 gtk_rc_context_parse_string (tmp_list->data, rc_string);
922 add_to_rc_file_list (GSList **rc_file_list,
923 const char *filename,
929 tmp_list = *rc_file_list;
932 rc_file = tmp_list->data;
933 if (!strcmp (rc_file->name, filename))
936 tmp_list = tmp_list->next;
939 rc_file = g_new (GtkRcFile, 1);
940 rc_file->is_string = FALSE;
941 rc_file->name = g_strdup (filename);
942 rc_file->canonical_name = NULL;
943 rc_file->directory = NULL;
945 rc_file->reload = reload;
947 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
953 gtk_rc_context_parse_one_file (GtkRcContext *context,
954 const gchar *filename,
962 g_return_if_fail (filename != NULL);
964 saved_priority = context->default_priority;
965 context->default_priority = priority;
967 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
969 if (!rc_file->canonical_name)
971 /* Get the absolute pathname */
973 if (g_path_is_absolute (rc_file->name))
974 rc_file->canonical_name = rc_file->name;
979 cwd = g_get_current_dir ();
980 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
984 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
987 /* If the file is already being parsed (recursion), do nothing
989 if (g_slist_find (current_files_stack, rc_file))
992 if (!g_lstat (rc_file->canonical_name, &statbuf))
996 rc_file->mtime = statbuf.st_mtime;
998 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1002 /* Temporarily push information for this file on
1003 * a stack of current files while parsing it.
1005 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1006 gtk_rc_parse_any (context, filename, fd, NULL);
1007 current_files_stack = g_slist_delete_link (current_files_stack,
1008 current_files_stack);
1014 context->default_priority = saved_priority;
1018 strchr_len (const gchar *str, gint len, char c)
1023 return (gchar *)str;
1032 gtk_rc_context_parse_file (GtkRcContext *context,
1033 const gchar *filename,
1037 gchar *locale_suffixes[2];
1038 gint n_locale_suffixes = 0;
1042 gboolean found = FALSE;
1044 locale = _gtk_get_lc_ctype ();
1046 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1048 /* Determine locale-specific suffixes for RC files.
1050 length = strlen (locale);
1052 p = strchr (locale, '@');
1054 length = p - locale;
1056 p = strchr_len (locale, length, '.');
1058 length = p - locale;
1060 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1062 p = strchr_len (locale, length, '_');
1065 length = p - locale;
1066 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1072 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1073 for (j = 0; j < n_locale_suffixes; j++)
1077 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1078 if (g_file_test (name, G_FILE_TEST_EXISTS))
1080 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1087 g_free (locale_suffixes[j]);
1092 gtk_rc_parse (const gchar *filename)
1096 g_return_if_fail (filename != NULL);
1098 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1100 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1101 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1104 /* Handling of RC styles */
1106 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1109 gtk_rc_style_init (GtkRcStyle *style)
1111 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1115 style->font_desc = NULL;
1117 for (i = 0; i < 5; i++)
1119 static const GdkColor init_color = { 0, 0, 0, 0, };
1121 style->bg_pixmap_name[i] = NULL;
1122 style->color_flags[i] = 0;
1123 style->fg[i] = init_color;
1124 style->bg[i] = init_color;
1125 style->text[i] = init_color;
1126 style->base[i] = init_color;
1128 style->xthickness = -1;
1129 style->ythickness = -1;
1130 style->rc_properties = NULL;
1132 style->rc_style_lists = NULL;
1133 style->icon_factories = NULL;
1135 priv->color_hashes = NULL;
1139 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1141 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1143 object_class->finalize = gtk_rc_style_finalize;
1145 klass->parse = NULL;
1146 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1147 klass->merge = gtk_rc_style_real_merge;
1148 klass->create_style = gtk_rc_style_real_create_style;
1150 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1154 gtk_rc_style_finalize (GObject *object)
1156 GSList *tmp_list1, *tmp_list2;
1157 GtkRcStyle *rc_style;
1158 GtkRcStylePrivate *rc_priv;
1161 rc_style = GTK_RC_STYLE (object);
1162 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1164 g_free (rc_style->name);
1165 if (rc_style->font_desc)
1166 pango_font_description_free (rc_style->font_desc);
1168 for (i = 0; i < 5; i++)
1169 g_free (rc_style->bg_pixmap_name[i]);
1171 /* Now remove all references to this rc_style from
1174 tmp_list1 = rc_style->rc_style_lists;
1177 GSList *rc_styles = tmp_list1->data;
1178 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1179 g_object_unref (style);
1181 /* Remove the list of styles from the other rc_styles
1184 tmp_list2 = rc_styles;
1187 GtkRcStyle *other_style = tmp_list2->data;
1189 if (other_style != rc_style)
1190 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1192 tmp_list2 = tmp_list2->next;
1195 /* And from the hash table itself
1197 g_hash_table_remove (realized_style_ht, rc_styles);
1198 g_slist_free (rc_styles);
1200 tmp_list1 = tmp_list1->next;
1202 g_slist_free (rc_style->rc_style_lists);
1204 if (rc_style->rc_properties)
1208 for (i = 0; i < rc_style->rc_properties->len; i++)
1210 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1212 g_free (node->origin);
1213 g_value_unset (&node->value);
1215 g_array_free (rc_style->rc_properties, TRUE);
1216 rc_style->rc_properties = NULL;
1219 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1220 g_slist_free (rc_style->icon_factories);
1222 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1223 g_slist_free (rc_priv->color_hashes);
1225 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1229 gtk_rc_style_new (void)
1233 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1239 * gtk_rc_style_copy:
1240 * @orig: the style to copy
1242 * Makes a copy of the specified #GtkRcStyle. This function
1243 * will correctly copy an RC style that is a member of a class
1244 * derived from #GtkRcStyle.
1246 * Return value: the resulting #GtkRcStyle
1249 gtk_rc_style_copy (GtkRcStyle *orig)
1253 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1255 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1256 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1258 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1264 _gtk_rc_style_set_rc_property (GtkRcStyle *rc_style,
1265 GtkRcProperty *property)
1267 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1268 g_return_if_fail (property != NULL);
1270 insert_rc_property (rc_style, property, TRUE);
1274 _gtk_rc_style_unset_rc_property (GtkRcStyle *rc_style,
1276 GQuark property_name)
1278 GtkRcProperty *node;
1280 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1282 node = (GtkRcProperty *) _gtk_rc_style_lookup_rc_property (rc_style,
1288 guint index = node - (GtkRcProperty *) rc_style->rc_properties->data;
1289 g_value_unset (&node->value);
1290 g_free (node->origin);
1291 g_array_remove_index (rc_style->rc_properties, index);
1296 gtk_rc_style_ref (GtkRcStyle *rc_style)
1298 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1300 g_object_ref (rc_style);
1304 gtk_rc_style_unref (GtkRcStyle *rc_style)
1306 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1308 g_object_unref (rc_style);
1312 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1314 return g_object_new (G_OBJECT_TYPE (style), NULL);
1318 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1320 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1322 return priv->color_hashes;
1326 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1327 gconstpointer bsearch_node2)
1329 const GtkRcProperty *prop1 = bsearch_node1;
1330 const GtkRcProperty *prop2 = bsearch_node2;
1332 if (prop1->type_name == prop2->type_name)
1333 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1335 return prop1->type_name < prop2->type_name ? -1 : 1;
1339 insert_rc_property (GtkRcStyle *style,
1340 GtkRcProperty *property,
1344 GtkRcProperty *new_property = NULL;
1345 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1347 key.type_name = property->type_name;
1348 key.property_name = property->property_name;
1350 if (!style->rc_properties)
1351 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1354 while (i < style->rc_properties->len)
1356 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1362 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1364 g_free (new_property->origin);
1365 g_value_unset (&new_property->value);
1367 *new_property = key;
1381 g_array_insert_val (style->rc_properties, i, key);
1382 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1385 new_property->origin = g_strdup (property->origin);
1386 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1387 g_value_copy (&property->value, &new_property->value);
1391 gtk_rc_style_real_merge (GtkRcStyle *dest,
1396 for (i = 0; i < 5; i++)
1398 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1399 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1401 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1402 src->color_flags[i] & GTK_RC_FG)
1404 dest->fg[i] = src->fg[i];
1405 dest->color_flags[i] |= GTK_RC_FG;
1407 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1408 src->color_flags[i] & GTK_RC_BG)
1410 dest->bg[i] = src->bg[i];
1411 dest->color_flags[i] |= GTK_RC_BG;
1413 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1414 src->color_flags[i] & GTK_RC_TEXT)
1416 dest->text[i] = src->text[i];
1417 dest->color_flags[i] |= GTK_RC_TEXT;
1419 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1420 src->color_flags[i] & GTK_RC_BASE)
1422 dest->base[i] = src->base[i];
1423 dest->color_flags[i] |= GTK_RC_BASE;
1427 if (dest->xthickness < 0 && src->xthickness >= 0)
1428 dest->xthickness = src->xthickness;
1429 if (dest->ythickness < 0 && src->ythickness >= 0)
1430 dest->ythickness = src->ythickness;
1434 if (!dest->font_desc)
1435 dest->font_desc = pango_font_description_copy (src->font_desc);
1437 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1440 if (src->rc_properties)
1444 for (i = 0; i < src->rc_properties->len; i++)
1445 insert_rc_property (dest,
1446 &g_array_index (src->rc_properties, GtkRcProperty, i),
1452 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1454 return gtk_style_new ();
1458 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1460 GtkIconFactory *factory = gtk_icon_factory_new ();
1462 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1466 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1468 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1469 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1471 (GDestroyNotify) gdk_color_free);
1473 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1477 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1478 GtkRcStyle *src_style)
1480 GSList *concat = g_slist_copy (src_style->icon_factories);
1482 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1484 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1488 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1489 GtkRcStyle *src_style)
1491 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1492 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1493 GSList *concat = g_slist_copy (src_priv->color_hashes);
1495 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1497 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1501 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1502 GtkRcStyle *src_style,
1503 GtkRcContext *context)
1505 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1509 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1511 /* Append src_style's factories, adding a ref to them */
1512 if (src_style->icon_factories != NULL)
1514 /* Add a factory for ourselves if we have none,
1515 * in case we end up defining more stock icons.
1516 * I see no real way around this; we need to maintain
1517 * the invariant that the first factory in the list
1518 * is always our_factory, the one belonging to us,
1519 * and if we put src_style factories in the list we can't
1520 * do that if the style is reopened.
1522 if (rc_style->icon_factories == NULL)
1523 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1525 gtk_rc_style_append_icon_factories (rc_style, src_style);
1528 /* Also append src_style's color hashes, adding a ref to them */
1529 if (src_priv->color_hashes != NULL)
1531 /* See comment above .. */
1532 if (priv->color_hashes == NULL)
1533 gtk_rc_style_prepend_empty_color_hash (rc_style);
1535 gtk_rc_style_append_color_hashes (rc_style, src_style);
1539 /* if we didn't get color hashes from the src_style, initialize
1540 * the list with the settings' color scheme (if it exists)
1542 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1544 gtk_rc_style_prepend_empty_color_hash (rc_style);
1546 priv->color_hashes = g_slist_append (priv->color_hashes,
1547 g_hash_table_ref (context->color_hash));
1552 gtk_rc_clear_hash_node (gpointer key,
1556 g_object_unref (data);
1560 gtk_rc_free_rc_sets (GSList *slist)
1566 rc_set = slist->data;
1567 gtk_rc_set_free (rc_set);
1569 slist = slist->next;
1574 gtk_rc_clear_styles (GtkRcContext *context)
1576 /* Clear out all old rc_styles */
1578 if (context->rc_style_ht)
1580 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1581 g_hash_table_destroy (context->rc_style_ht);
1582 context->rc_style_ht = NULL;
1585 gtk_rc_free_rc_sets (context->rc_sets_widget);
1586 g_slist_free (context->rc_sets_widget);
1587 context->rc_sets_widget = NULL;
1589 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1590 g_slist_free (context->rc_sets_widget_class);
1591 context->rc_sets_widget_class = NULL;
1593 gtk_rc_free_rc_sets (context->rc_sets_class);
1594 g_slist_free (context->rc_sets_class);
1595 context->rc_sets_class = NULL;
1598 /* Reset all our widgets. Also, we have to invalidate cached icons in
1599 * icon sets so they get re-rendered.
1602 gtk_rc_reset_widgets (GtkSettings *settings)
1604 GList *list, *toplevels;
1606 _gtk_icon_set_invalidate_caches ();
1608 toplevels = gtk_window_list_toplevels ();
1609 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1611 for (list = toplevels; list; list = list->next)
1613 if (gtk_widget_get_screen (list->data) == settings->screen)
1615 gtk_widget_reset_rc_styles (list->data);
1618 g_object_unref (list->data);
1620 g_list_free (toplevels);
1624 gtk_rc_clear_realized_style (gpointer key,
1628 GSList *rc_styles = key;
1629 GtkStyle *style = value;
1630 GSList *tmp_list = rc_styles;
1632 g_object_unref (style);
1636 GtkRcStyle *rc_style = tmp_list->data;
1638 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1640 tmp_list = tmp_list->next;
1643 g_slist_free (rc_styles);
1647 * gtk_rc_reset_styles:
1648 * @settings: a #GtkSettings
1650 * This function recomputes the styles for all widgets that use a
1651 * particular #GtkSettings object. (There is one #GtkSettings object
1652 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1653 * when some global parameter has changed that affects the appearance
1654 * of all widgets, because when a widget gets a new style, it will
1655 * both redraw and recompute any cached information about its
1656 * appearance. As an example, it is used when the default font size
1657 * set by the operating system changes. Note that this function
1658 * doesn't affect widgets that have a style set explicitely on them
1659 * with gtk_widget_set_style().
1664 gtk_rc_reset_styles (GtkSettings *settings)
1666 GtkRcContext *context;
1667 gboolean reset = FALSE;
1669 g_return_if_fail (GTK_IS_SETTINGS (settings));
1671 context = gtk_rc_context_get (settings);
1673 if (context->default_style)
1675 g_object_unref (context->default_style);
1676 context->default_style = NULL;
1680 /* Clear out styles that have been looked up already
1682 if (realized_style_ht)
1684 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1685 g_hash_table_destroy (realized_style_ht);
1686 realized_style_ht = NULL;
1691 gtk_rc_reset_widgets (settings);
1695 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1697 GtkRcContext *context;
1698 gchar *new_font_name;
1700 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1702 context = gtk_rc_context_get (settings);
1704 g_object_get (context->settings,
1705 "gtk-font-name", &new_font_name,
1708 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1710 g_free (context->font_name);
1711 context->font_name = g_strdup (new_font_name);
1713 gtk_rc_reset_styles (settings);
1716 g_free (new_font_name);
1718 return context->font_name;
1722 * gtk_rc_reparse_all_for_settings:
1723 * @settings: a #GtkSettings
1724 * @force_load: load whether or not anything changed
1726 * If the modification time on any previously read file
1727 * for the given #GtkSettings has changed, discard all style information
1728 * and then reread all previously read RC files.
1730 * Return value: %TRUE if the files were reread.
1733 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1734 gboolean force_load)
1736 gboolean mtime_modified = FALSE;
1739 GtkRcContext *context;
1740 struct stat statbuf;
1742 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1744 context = gtk_rc_context_get (settings);
1746 if (context->reloading)
1751 /* Check through and see if any of the RC's have had their
1752 * mtime modified. If so, reparse everything.
1754 tmp_list = context->rc_files;
1757 rc_file = tmp_list->data;
1759 if (!rc_file->is_string)
1761 if (!g_lstat (rc_file->name, &statbuf) &&
1762 (statbuf.st_mtime != rc_file->mtime))
1764 mtime_modified = TRUE;
1769 tmp_list = tmp_list->next;
1773 if (force_load || mtime_modified)
1775 _gtk_binding_reset_parsed ();
1776 gtk_rc_clear_styles (context);
1777 context->reloading = TRUE;
1779 _gtk_settings_reset_rc_values (context->settings);
1780 gtk_rc_clear_rc_files (context);
1782 gtk_rc_parse_default_files (context);
1784 tmp_list = global_rc_files;
1787 rc_file = tmp_list->data;
1789 if (rc_file->is_string)
1790 gtk_rc_context_parse_string (context, rc_file->name);
1792 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1794 tmp_list = tmp_list->next;
1797 g_free (context->theme_name);
1798 g_free (context->key_theme_name);
1800 g_object_get (context->settings,
1801 "gtk-theme-name", &context->theme_name,
1802 "gtk-key-theme-name", &context->key_theme_name,
1805 if (context->theme_name && context->theme_name[0])
1806 gtk_rc_parse_named (context, context->theme_name, NULL);
1807 if (context->key_theme_name && context->key_theme_name[0])
1808 gtk_rc_parse_named (context, context->key_theme_name, "key");
1810 context->reloading = FALSE;
1812 gtk_rc_reset_widgets (context->settings);
1815 return force_load || mtime_modified;
1819 * gtk_rc_reparse_all:
1821 * If the modification time on any previously read file for the
1822 * default #GtkSettings has changed, discard all style information
1823 * and then reread all previously read RC files.
1825 * Return value: %TRUE if the files were reread.
1828 gtk_rc_reparse_all (void)
1831 gboolean result = FALSE;
1833 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1835 GtkRcContext *context = tmp_list->data;
1836 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1844 gtk_rc_styles_match (GSList *rc_styles,
1848 gchar *path_reversed)
1855 rc_set = sets->data;
1858 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1860 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1861 rc_styles = g_slist_append (rc_styles, rc_set);
1865 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1866 rc_styles = g_slist_append (rc_styles, rc_set);
1874 rc_set_compare (gconstpointer a, gconstpointer b)
1876 const GtkRcSet *set_a = a;
1877 const GtkRcSet *set_b = b;
1879 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1883 sort_and_dereference_sets (GSList *styles)
1887 /* At this point, the list of sets is ordered by:
1889 * a) 'widget' patterns are earlier than 'widget_class' patterns
1890 * which are ealier than 'class' patterns.
1891 * a) For two matches for class patterns, a match to a child type
1892 * is before a match to a parent type
1893 * c) a match later in the RC file (or in a later RC file) is before a
1894 * match earlier in the RC file.
1896 * With a) taking precedence over b) which takes precendence over c).
1898 * Now sort by priority, which has the highest precendence for sort order
1900 styles = g_slist_sort (styles, rc_set_compare);
1902 /* Make styles->data = styles->data->rc_style
1907 GtkRcSet *set = tmp_list->data;
1908 tmp_list->data = set->rc_style;
1909 tmp_list = tmp_list->next;
1917 * @widget: a #GtkWidget
1919 * Finds all matching RC styles for a given widget,
1920 * composites them together, and then creates a
1921 * #GtkStyle representing the composite appearance.
1922 * (GTK+ actually keeps a cache of previously
1923 * created styles, so a new style may not be
1926 * Returns: the resulting style. No refcount is added
1927 * to the returned style, so if you want to save this
1928 * style around, you should add a reference yourself.
1931 gtk_rc_get_style (GtkWidget *widget)
1933 GtkRcStyle *widget_rc_style;
1934 GSList *rc_styles = NULL;
1935 GtkRcContext *context;
1937 static guint rc_style_key_id = 0;
1939 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1941 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1943 /* We allow the specification of a single rc style to be bound
1944 * tightly to a widget, for application modifications
1946 if (!rc_style_key_id)
1947 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1949 if (context->rc_sets_widget)
1951 gchar *path, *path_reversed;
1954 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1955 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1957 g_free (path_reversed);
1960 if (context->rc_sets_widget_class)
1962 gchar *path, *path_reversed;
1965 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1966 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1968 g_free (path_reversed);
1971 if (context->rc_sets_class)
1975 type = G_TYPE_FROM_INSTANCE (widget);
1979 gchar *path_reversed;
1982 path = g_strdup (g_type_name (type));
1983 path_length = strlen (path);
1984 path_reversed = g_strdup (path);
1985 g_strreverse (path_reversed);
1987 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1989 g_free (path_reversed);
1991 type = g_type_parent (type);
1995 rc_styles = sort_and_dereference_sets (rc_styles);
1997 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
1999 if (widget_rc_style)
2000 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
2003 return gtk_rc_init_style (context, rc_styles);
2006 if (!context->default_style)
2008 context->default_style = gtk_style_new ();
2009 _gtk_style_init_for_settings (context->default_style, context->settings);
2012 return context->default_style;
2017 * gtk_rc_get_style_by_paths:
2018 * @settings: a #GtkSettings object
2019 * @widget_path: the widget path to use when looking up the style, or %NULL
2020 * if no matching against the widget path should be done
2021 * @class_path: the class path to use when looking up the style, or %NULL
2022 * if no matching against the class path should be done.
2023 * @type: a type that will be used along with parent types of this type
2024 * when matching against class styles, or #G_TYPE_NONE
2026 * Creates up a #GtkStyle from styles defined in a RC file by providing
2027 * the raw components used in matching. This function may be useful
2028 * when creating pseudo-widgets that should be themed like widgets but
2029 * don't actually have corresponding GTK+ widgets. An example of this
2030 * would be items inside a GNOME canvas widget.
2032 * The action of gtk_rc_get_style() is similar to:
2034 * gtk_widget_path (widget, NULL, &path, NULL);
2035 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2036 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
2038 * G_OBJECT_TYPE (widget));
2041 * Return value: A style created by matching with the supplied paths,
2042 * or %NULL if nothing matching was specified and the default style should
2043 * be used. The returned value is owned by GTK+ as part of an internal cache,
2044 * so you must call g_object_ref() on the returned value if you want to
2045 * keep a reference to it.
2048 gtk_rc_get_style_by_paths (GtkSettings *settings,
2049 const char *widget_path,
2050 const char *class_path,
2053 /* We duplicate the code from above to avoid slowing down the above
2054 * by generating paths when we don't need them. I don't know if
2055 * this is really worth it.
2057 GSList *rc_styles = NULL;
2058 GtkRcContext *context;
2060 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2062 context = gtk_rc_context_get (settings);
2064 if (widget_path && context->rc_sets_widget)
2067 gchar *path_reversed;
2070 path_length = strlen (widget_path);
2071 path = g_strdup (widget_path);
2072 path_reversed = g_strdup (widget_path);
2073 g_strreverse (path_reversed);
2075 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2077 g_free (path_reversed);
2080 if (class_path && context->rc_sets_widget_class)
2083 gchar *path_reversed;
2086 path = g_strdup (class_path);
2087 path_length = strlen (class_path);
2088 path_reversed = g_strdup (class_path);
2089 g_strreverse (path_reversed);
2091 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2093 g_free (path_reversed);
2096 if (type != G_TYPE_NONE && context->rc_sets_class)
2101 gchar *path_reversed;
2104 path = g_strdup (g_type_name (type));
2105 path_length = strlen (path);
2106 path_reversed = g_strdup (path);
2107 g_strreverse (path_reversed);
2109 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2111 g_free (path_reversed);
2113 type = g_type_parent (type);
2117 rc_styles = sort_and_dereference_sets (rc_styles);
2120 return gtk_rc_init_style (context, rc_styles);
2126 gtk_rc_add_rc_sets (GSList *slist,
2127 GtkRcStyle *rc_style,
2128 const gchar *pattern,
2129 GtkPathType path_type)
2131 GtkRcStyle *new_style;
2135 new_style = gtk_rc_style_new ();
2136 *new_style = *rc_style;
2137 new_style->name = g_strdup (rc_style->name);
2138 if (rc_style->font_desc)
2139 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
2141 for (i = 0; i < 5; i++)
2142 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
2144 rc_set = g_new (GtkRcSet, 1);
2145 rc_set->type = path_type;
2147 if (path_type == GTK_PATH_WIDGET_CLASS)
2149 rc_set->pspec = NULL;
2150 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
2154 rc_set->pspec = g_pattern_spec_new (pattern);
2155 rc_set->path = NULL;
2158 rc_set->rc_style = rc_style;
2160 return g_slist_prepend (slist, rc_set);
2164 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
2165 const gchar *pattern)
2167 GtkRcContext *context;
2169 g_return_if_fail (rc_style != NULL);
2170 g_return_if_fail (pattern != NULL);
2172 context = gtk_rc_context_get (gtk_settings_get_default ());
2174 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern, GTK_PATH_WIDGET);
2178 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
2179 const gchar *pattern)
2181 GtkRcContext *context;
2183 g_return_if_fail (rc_style != NULL);
2184 g_return_if_fail (pattern != NULL);
2186 context = gtk_rc_context_get (gtk_settings_get_default ());
2188 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern, GTK_PATH_WIDGET_CLASS);
2192 gtk_rc_add_class_style (GtkRcStyle *rc_style,
2193 const gchar *pattern)
2195 GtkRcContext *context;
2197 g_return_if_fail (rc_style != NULL);
2198 g_return_if_fail (pattern != NULL);
2200 context = gtk_rc_context_get (gtk_settings_get_default ());
2202 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern, GTK_PATH_CLASS);
2206 gtk_rc_scanner_new (void)
2208 return g_scanner_new (>k_rc_scanner_config);
2212 gtk_rc_parse_any (GtkRcContext *context,
2213 const gchar *input_name,
2215 const gchar *input_string)
2221 scanner = gtk_rc_scanner_new ();
2225 g_assert (input_string == NULL);
2227 g_scanner_input_file (scanner, input_fd);
2231 g_assert (input_string != NULL);
2233 g_scanner_input_text (scanner, input_string, strlen (input_string));
2235 scanner->input_name = input_name;
2237 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2238 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2242 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2246 guint expected_token;
2248 expected_token = gtk_rc_parse_statement (context, scanner);
2250 if (expected_token != G_TOKEN_NONE)
2252 const gchar *symbol_name = NULL;
2255 if (scanner->scope_id == 0)
2257 /* if we are in scope 0, we know the symbol names
2258 * that are associated with certain token values.
2259 * so we look them up to make the error messages
2262 if (expected_token > GTK_RC_TOKEN_INVALID &&
2263 expected_token < GTK_RC_TOKEN_LAST)
2265 const gchar *sym = NULL;
2267 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2268 if (symbols[i].token == expected_token)
2269 sym = symbol_names + symbols[i].name_offset;
2272 msg = g_strconcat ("e.g. `", sym, "'", NULL);
2275 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2276 scanner->token < GTK_RC_TOKEN_LAST)
2278 symbol_name = "???";
2279 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2280 if (symbols[i].token == scanner->token)
2281 symbol_name = symbol_names + symbols[i].name_offset;
2285 g_scanner_unexp_token (scanner,
2298 g_scanner_destroy (scanner);
2302 gtk_rc_styles_hash (const GSList *rc_styles)
2309 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2310 rc_styles = rc_styles->next;
2317 gtk_rc_styles_equal (const GSList *a,
2322 if (a->data != b->data)
2332 gtk_rc_style_hash (const gchar *name)
2338 result += (result << 3) + *name++;
2344 gtk_rc_style_equal (const gchar *a,
2347 return (strcmp (a, b) == 0);
2351 gtk_rc_style_find (GtkRcContext *context,
2354 if (context->rc_style_ht)
2355 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2361 gtk_rc_style_to_style (GtkRcContext *context,
2362 GtkRcStyle *rc_style)
2366 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2367 _gtk_style_init_for_settings (style, context->settings);
2369 style->rc_style = g_object_ref (rc_style);
2371 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2376 /* Reuses or frees rc_styles */
2378 gtk_rc_init_style (GtkRcContext *context,
2381 GtkStyle *style = NULL;
2384 g_return_val_if_fail (rc_styles != NULL, NULL);
2386 if (!realized_style_ht)
2387 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2388 (GEqualFunc) gtk_rc_styles_equal);
2390 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2394 GtkRcStyle *base_style = NULL;
2395 GtkRcStyle *proto_style;
2396 GtkRcStyleClass *proto_style_class;
2398 GType rc_style_type = GTK_TYPE_RC_STYLE;
2400 /* Find the first style where the RC file specified engine "" {}
2401 * or the first derived style and use that to create the
2402 * merged style. If we only have raw GtkRcStyles, use the first
2403 * style to create the merged style.
2405 base_style = rc_styles->data;
2406 tmp_styles = rc_styles;
2409 GtkRcStyle *rc_style = tmp_styles->data;
2411 if (rc_style->engine_specified ||
2412 G_OBJECT_TYPE (rc_style) != rc_style_type)
2414 base_style = rc_style;
2418 tmp_styles = tmp_styles->next;
2421 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2422 proto_style = proto_style_class->create_rc_style (base_style);
2424 tmp_styles = rc_styles;
2427 GtkRcStyle *rc_style = tmp_styles->data;
2429 proto_style_class->merge (proto_style, rc_style);
2431 /* Point from each rc_style to the list of styles */
2432 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2433 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2435 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2436 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2438 tmp_styles = tmp_styles->next;
2441 for (i = 0; i < 5; i++)
2442 if (proto_style->bg_pixmap_name[i] &&
2443 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2445 g_free (proto_style->bg_pixmap_name[i]);
2446 proto_style->bg_pixmap_name[i] = NULL;
2449 style = gtk_rc_style_to_style (context, proto_style);
2450 g_object_unref (proto_style);
2452 g_hash_table_insert (realized_style_ht, rc_styles, style);
2455 g_slist_free (rc_styles);
2460 /*********************
2461 * Parsing functions *
2462 *********************/
2465 lookup_color (GtkRcStyle *style,
2466 const char *color_name,
2469 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2472 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2474 GHashTable *hash = iter->data;
2475 GdkColor *match = g_hash_table_lookup (hash, color_name);
2479 color->red = match->red;
2480 color->green = match->green;
2481 color->blue = match->blue;
2490 rc_parse_token_or_compound (GScanner *scanner,
2493 GTokenType delimiter)
2495 guint token = g_scanner_get_next_token (scanner);
2497 /* we either scan a single token (skipping comments)
2498 * or a compund statement.
2499 * compunds are enclosed in (), [] or {} braces, we read
2500 * them in via deep recursion.
2507 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2510 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2512 case G_TOKEN_STRING:
2513 string = g_strescape (scanner->value.v_string, NULL);
2514 g_string_append (gstring, " \"");
2515 g_string_append (gstring, string);
2516 g_string_append_c (gstring, '"');
2519 case G_TOKEN_IDENTIFIER:
2520 g_string_append_c (gstring, ' ');
2521 g_string_append (gstring, scanner->value.v_identifier);
2523 case G_TOKEN_COMMENT_SINGLE:
2524 case G_TOKEN_COMMENT_MULTI:
2525 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2526 case G_TOKEN_LEFT_PAREN:
2527 g_string_append_c (gstring, ' ');
2528 g_string_append_c (gstring, token);
2529 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2530 if (token != G_TOKEN_NONE)
2533 case G_TOKEN_LEFT_CURLY:
2534 g_string_append_c (gstring, ' ');
2535 g_string_append_c (gstring, token);
2536 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2537 if (token != G_TOKEN_NONE)
2540 case G_TOKEN_LEFT_BRACE:
2541 g_string_append_c (gstring, ' ');
2542 g_string_append_c (gstring, token);
2543 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2544 if (token != G_TOKEN_NONE)
2548 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2551 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2552 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2553 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2555 g_scanner_get_next_token (scanner);
2557 if (!style || !lookup_color (style, scanner->value.v_identifier,
2560 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2561 scanner->value.v_identifier);
2562 return G_TOKEN_IDENTIFIER;
2566 g_string_append_printf (gstring, " { %s, %s, %s }",
2567 g_ascii_formatd (rbuf, sizeof (rbuf),
2569 color.red / 65535.0),
2570 g_ascii_formatd (gbuf, sizeof (gbuf),
2572 color.green / 65535.0),
2573 g_ascii_formatd (bbuf, sizeof (bbuf),
2575 color.blue / 65535.0));
2579 return G_TOKEN_IDENTIFIER;
2581 if (token >= 256 || token < 1)
2582 return delimiter ? delimiter : G_TOKEN_STRING;
2583 g_string_append_c (gstring, ' ');
2584 g_string_append_c (gstring, token);
2585 if (token == delimiter)
2586 return G_TOKEN_NONE;
2590 return G_TOKEN_NONE;
2592 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2596 gtk_rc_parse_assignment (GScanner *scanner,
2598 GtkRcProperty *prop)
2600 #define MY_SCAN_IDENTIFIER TRUE
2601 #define MY_SCAN_SYMBOLS FALSE
2602 #define MY_IDENTIFIER_2_STRING FALSE
2603 #define MY_CHAR_2_TOKEN TRUE
2604 #define MY_SCAN_IDENTIFIER_NULL FALSE
2605 #define MY_NUMBERS_2_INT TRUE
2607 gboolean scan_identifier = scanner->config->scan_identifier;
2608 gboolean scan_symbols = scanner->config->scan_symbols;
2609 gboolean identifier_2_string = scanner->config->identifier_2_string;
2610 gboolean char_2_token = scanner->config->char_2_token;
2611 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2612 gboolean numbers_2_int = scanner->config->numbers_2_int;
2613 gboolean negate = FALSE;
2614 gboolean is_color = FALSE;
2617 /* check that this is an assignment */
2618 if (g_scanner_get_next_token (scanner) != '=')
2621 /* adjust scanner mode */
2622 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2623 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2624 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2625 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2626 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2627 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2629 /* record location */
2630 if (g_getenv ("GTK_DEBUG"))
2631 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2633 prop->origin = NULL;
2635 /* parse optional symbolic color prefix */
2636 if (g_scanner_peek_next_token (scanner) == '@')
2638 g_scanner_get_next_token (scanner); /* eat color prefix */
2642 /* parse optional sign */
2643 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2645 g_scanner_get_next_token (scanner); /* eat sign */
2649 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2652 token = g_scanner_peek_next_token (scanner);
2654 if (is_color && token != G_TOKEN_IDENTIFIER)
2656 token = G_TOKEN_IDENTIFIER;
2663 g_scanner_get_next_token (scanner);
2664 g_value_init (&prop->value, G_TYPE_LONG);
2665 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2666 token = G_TOKEN_NONE;
2669 g_scanner_get_next_token (scanner);
2670 g_value_init (&prop->value, G_TYPE_DOUBLE);
2671 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2672 token = G_TOKEN_NONE;
2674 case G_TOKEN_STRING:
2675 g_scanner_get_next_token (scanner);
2677 token = G_TOKEN_INT;
2680 g_value_init (&prop->value, G_TYPE_STRING);
2681 g_value_set_string (&prop->value, scanner->value.v_string);
2682 token = G_TOKEN_NONE;
2685 case G_TOKEN_IDENTIFIER:
2689 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2690 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2691 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2694 g_scanner_get_next_token (scanner);
2696 if (!style || !lookup_color (style, scanner->value.v_identifier,
2699 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2700 scanner->value.v_identifier);
2701 token = G_TOKEN_IDENTIFIER;
2705 gstring = g_string_new (NULL);
2707 g_string_append_printf (gstring, " { %s, %s, %s }",
2708 g_ascii_formatd (rbuf, sizeof (rbuf),
2710 color.red / 65535.0),
2711 g_ascii_formatd (gbuf, sizeof (gbuf),
2713 color.green / 65535.0),
2714 g_ascii_formatd (bbuf, sizeof (bbuf),
2716 color.blue / 65535.0));
2718 g_value_init (&prop->value, G_TYPE_GSTRING);
2719 g_value_take_boxed (&prop->value, gstring);
2720 token = G_TOKEN_NONE;
2724 case G_TOKEN_LEFT_PAREN:
2725 case G_TOKEN_LEFT_CURLY:
2726 case G_TOKEN_LEFT_BRACE:
2729 GString *gstring = g_string_new (NULL);
2730 gboolean parse_on = TRUE;
2732 /* allow identifier(foobar) to support color expressions */
2733 if (token == G_TOKEN_IDENTIFIER)
2735 g_scanner_get_next_token (scanner);
2737 g_string_append_c (gstring, ' ');
2738 g_string_append (gstring, scanner->value.v_identifier);
2740 /* temporarily reset scanner mode to default, so we
2741 * don't peek the next token in a mode that only makes
2742 * sense in this function; because if anything but
2743 * G_TOKEN_LEFT_PAREN follows, the next token will be
2744 * parsed by our caller.
2746 * FIXME: right fix would be to call g_scanner_unget()
2747 * but that doesn't exist
2749 scanner->config->scan_identifier = scan_identifier;
2750 scanner->config->scan_symbols = scan_symbols;
2751 scanner->config->identifier_2_string = identifier_2_string;
2752 scanner->config->char_2_token = char_2_token;
2753 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2754 scanner->config->numbers_2_int = numbers_2_int;
2756 token = g_scanner_peek_next_token (scanner);
2758 /* restore adjusted scanner mode */
2759 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2760 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2761 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2762 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2763 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2764 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2766 if (token != G_TOKEN_LEFT_PAREN)
2768 token = G_TOKEN_NONE;
2774 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2776 if (token == G_TOKEN_NONE)
2778 g_string_append_c (gstring, ' ');
2779 g_value_init (&prop->value, G_TYPE_GSTRING);
2780 g_value_take_boxed (&prop->value, gstring);
2783 g_string_free (gstring, TRUE);
2788 g_scanner_get_next_token (scanner);
2789 token = G_TOKEN_INT;
2795 /* restore scanner mode */
2796 scanner->config->scan_identifier = scan_identifier;
2797 scanner->config->scan_symbols = scan_symbols;
2798 scanner->config->identifier_2_string = identifier_2_string;
2799 scanner->config->char_2_token = char_2_token;
2800 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2801 scanner->config->numbers_2_int = numbers_2_int;
2807 is_c_identifier (const gchar *string)
2810 gboolean is_varname;
2812 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2813 for (p = string + 1; *p && is_varname; p++)
2814 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2820 parse_include_file (GtkRcContext *context,
2822 const gchar *filename)
2824 char *to_parse = NULL;
2826 if (g_path_is_absolute (filename))
2828 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2829 * don't print an error in this case.
2831 to_parse = g_strdup (filename);
2835 /* if a relative path, we look relative to all the RC files in the
2836 * include stack. We require the file to be found in this case
2837 * so we can give meaningful error messages, and because on reparsing
2838 * non-absolute paths don't make sense.
2840 GSList *tmp_list = current_files_stack;
2843 GtkRcFile *curfile = tmp_list->data;
2844 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2846 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2854 tmp_list = tmp_list->next;
2860 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2865 g_scanner_warn (scanner,
2866 _("Unable to find include file: \"%s\""),
2873 gtk_rc_parse_statement (GtkRcContext *context,
2878 token = g_scanner_peek_next_token (scanner);
2881 case GTK_RC_TOKEN_INCLUDE:
2882 token = g_scanner_get_next_token (scanner);
2883 if (token != GTK_RC_TOKEN_INCLUDE)
2884 return GTK_RC_TOKEN_INCLUDE;
2885 token = g_scanner_get_next_token (scanner);
2886 if (token != G_TOKEN_STRING)
2887 return G_TOKEN_STRING;
2888 parse_include_file (context, scanner, scanner->value.v_string);
2889 return G_TOKEN_NONE;
2891 case GTK_RC_TOKEN_STYLE:
2892 return gtk_rc_parse_style (context, scanner);
2894 case GTK_RC_TOKEN_BINDING:
2895 return _gtk_binding_parse_binding (scanner);
2897 case GTK_RC_TOKEN_PIXMAP_PATH:
2898 return gtk_rc_parse_pixmap_path (context, scanner);
2900 case GTK_RC_TOKEN_WIDGET:
2901 return gtk_rc_parse_path_pattern (context, scanner);
2903 case GTK_RC_TOKEN_WIDGET_CLASS:
2904 return gtk_rc_parse_path_pattern (context, scanner);
2906 case GTK_RC_TOKEN_CLASS:
2907 return gtk_rc_parse_path_pattern (context, scanner);
2909 case GTK_RC_TOKEN_MODULE_PATH:
2910 return gtk_rc_parse_module_path (scanner);
2912 case GTK_RC_TOKEN_IM_MODULE_FILE:
2913 return gtk_rc_parse_im_module_file (scanner);
2915 case G_TOKEN_IDENTIFIER:
2916 if (is_c_identifier (scanner->next_value.v_identifier))
2918 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2921 g_scanner_get_next_token (scanner); /* eat identifier */
2922 name = g_strdup (scanner->value.v_identifier);
2924 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2925 if (token == G_TOKEN_NONE)
2927 GtkSettingsValue svalue;
2929 svalue.origin = prop.origin;
2930 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2931 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2932 _gtk_settings_set_property_value_from_rc (context->settings,
2936 g_free (prop.origin);
2937 if (G_VALUE_TYPE (&prop.value))
2938 g_value_unset (&prop.value);
2945 g_scanner_get_next_token (scanner);
2946 return G_TOKEN_IDENTIFIER;
2949 g_scanner_get_next_token (scanner);
2950 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2955 fixup_rc_set (GSList *list,
2961 GtkRcSet *set = list->data;
2962 if (set->rc_style == orig)
2963 set->rc_style = new;
2969 fixup_rc_sets (GtkRcContext *context,
2973 fixup_rc_set (context->rc_sets_widget, orig, new);
2974 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2975 fixup_rc_set (context->rc_sets_class, orig, new);
2979 gtk_rc_parse_style (GtkRcContext *context,
2982 GtkRcStyle *rc_style;
2983 GtkRcStyle *orig_style;
2984 GtkRcStyle *parent_style = NULL;
2985 GtkRcStylePrivate *rc_priv = NULL;
2988 GtkIconFactory *our_factory = NULL;
2989 GHashTable *our_hash = NULL;
2991 token = g_scanner_get_next_token (scanner);
2992 if (token != GTK_RC_TOKEN_STYLE)
2993 return GTK_RC_TOKEN_STYLE;
2995 token = g_scanner_get_next_token (scanner);
2996 if (token != G_TOKEN_STRING)
2997 return G_TOKEN_STRING;
2999 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3001 orig_style = g_object_ref (rc_style);
3007 rc_style = gtk_rc_style_new ();
3008 rc_style->name = g_strdup (scanner->value.v_string);
3010 for (i = 0; i < 5; i++)
3011 rc_style->bg_pixmap_name[i] = NULL;
3013 for (i = 0; i < 5; i++)
3014 rc_style->color_flags[i] = 0;
3017 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
3019 /* If there's a list, its first member is always the factory belonging
3022 if (rc_style->icon_factories)
3023 our_factory = rc_style->icon_factories->data;
3024 if (rc_priv->color_hashes)
3025 our_hash = rc_priv->color_hashes->data;
3027 token = g_scanner_peek_next_token (scanner);
3028 if (token == G_TOKEN_EQUAL_SIGN)
3030 token = g_scanner_get_next_token (scanner);
3032 token = g_scanner_get_next_token (scanner);
3033 if (token != G_TOKEN_STRING)
3035 token = G_TOKEN_STRING;
3039 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3042 for (i = 0; i < 5; i++)
3044 rc_style->color_flags[i] = parent_style->color_flags[i];
3045 rc_style->fg[i] = parent_style->fg[i];
3046 rc_style->bg[i] = parent_style->bg[i];
3047 rc_style->text[i] = parent_style->text[i];
3048 rc_style->base[i] = parent_style->base[i];
3051 rc_style->xthickness = parent_style->xthickness;
3052 rc_style->ythickness = parent_style->ythickness;
3054 if (parent_style->font_desc)
3056 if (rc_style->font_desc)
3057 pango_font_description_free (rc_style->font_desc);
3058 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3061 if (parent_style->rc_properties)
3065 for (i = 0; i < parent_style->rc_properties->len; i++)
3066 insert_rc_property (rc_style,
3067 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3071 for (i = 0; i < 5; i++)
3073 g_free (rc_style->bg_pixmap_name[i]);
3074 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3079 /* get icon_factories and color_hashes from the parent style;
3080 * if the parent_style doesn't have color_hashes, initializes
3081 * the color_hashes with the settings' color scheme (if it exists)
3083 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3085 if (rc_style->icon_factories)
3086 our_factory = rc_style->icon_factories->data;
3087 if (rc_priv->color_hashes)
3088 our_hash = rc_priv->color_hashes->data;
3090 token = g_scanner_get_next_token (scanner);
3091 if (token != G_TOKEN_LEFT_CURLY)
3093 token = G_TOKEN_LEFT_CURLY;
3097 token = g_scanner_peek_next_token (scanner);
3098 while (token != G_TOKEN_RIGHT_CURLY)
3102 case GTK_RC_TOKEN_BG:
3103 token = gtk_rc_parse_bg (scanner, rc_style);
3105 case GTK_RC_TOKEN_FG:
3106 token = gtk_rc_parse_fg (scanner, rc_style);
3108 case GTK_RC_TOKEN_TEXT:
3109 token = gtk_rc_parse_text (scanner, rc_style);
3111 case GTK_RC_TOKEN_BASE:
3112 token = gtk_rc_parse_base (scanner, rc_style);
3114 case GTK_RC_TOKEN_XTHICKNESS:
3115 token = gtk_rc_parse_xthickness (scanner, rc_style);
3117 case GTK_RC_TOKEN_YTHICKNESS:
3118 token = gtk_rc_parse_ythickness (scanner, rc_style);
3120 case GTK_RC_TOKEN_BG_PIXMAP:
3121 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3123 case GTK_RC_TOKEN_FONT:
3124 token = gtk_rc_parse_font (scanner, rc_style);
3126 case GTK_RC_TOKEN_FONTSET:
3127 token = gtk_rc_parse_fontset (scanner, rc_style);
3129 case GTK_RC_TOKEN_FONT_NAME:
3130 token = gtk_rc_parse_font_name (scanner, rc_style);
3132 case GTK_RC_TOKEN_ENGINE:
3133 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3135 case GTK_RC_TOKEN_STOCK:
3136 if (our_factory == NULL)
3137 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3138 our_factory = rc_style->icon_factories->data;
3139 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3141 case GTK_RC_TOKEN_COLOR:
3142 if (our_hash == NULL)
3143 gtk_rc_style_prepend_empty_color_hash (rc_style);
3144 our_hash = rc_priv->color_hashes->data;
3145 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3147 case G_TOKEN_IDENTIFIER:
3148 if (is_c_identifier (scanner->next_value.v_identifier))
3150 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3153 g_scanner_get_next_token (scanner); /* eat type name */
3154 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3155 if (g_scanner_get_next_token (scanner) != ':' ||
3156 g_scanner_get_next_token (scanner) != ':')
3161 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3162 !is_c_identifier (scanner->value.v_identifier))
3164 token = G_TOKEN_IDENTIFIER;
3168 /* it's important that we do the same canonification as GParamSpecPool here */
3169 name = g_strdup (scanner->value.v_identifier);
3170 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3171 prop.property_name = g_quark_from_string (name);
3174 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3175 if (token == G_TOKEN_NONE)
3177 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3178 insert_rc_property (rc_style, &prop, TRUE);
3181 g_free (prop.origin);
3182 if (G_VALUE_TYPE (&prop.value))
3183 g_value_unset (&prop.value);
3187 g_scanner_get_next_token (scanner);
3188 token = G_TOKEN_IDENTIFIER;
3192 g_scanner_get_next_token (scanner);
3193 token = G_TOKEN_RIGHT_CURLY;
3197 if (token != G_TOKEN_NONE)
3200 token = g_scanner_peek_next_token (scanner);
3201 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3203 token = g_scanner_get_next_token (scanner);
3204 if (token != G_TOKEN_RIGHT_CURLY)
3206 token = G_TOKEN_RIGHT_CURLY;
3210 if (rc_style != orig_style)
3212 if (!context->rc_style_ht)
3213 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3214 (GEqualFunc) gtk_rc_style_equal);
3216 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3218 /* If we copied the data into a new rc style, fix up references to the old rc style
3219 * in bindings that we have.
3222 fixup_rc_sets (context, orig_style, rc_style);
3226 g_object_unref (orig_style);
3228 return G_TOKEN_NONE;
3231 if (rc_style != orig_style)
3232 g_object_unref (rc_style);
3235 g_object_unref (orig_style);
3240 const GtkRcProperty*
3241 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3243 GQuark property_name)
3245 GtkRcProperty *node = NULL;
3247 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3249 if (rc_style->rc_properties)
3253 key.type_name = type_name;
3254 key.property_name = property_name;
3256 node = bsearch (&key,
3257 rc_style->rc_properties->data, rc_style->rc_properties->len,
3258 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3265 gtk_rc_parse_bg (GScanner *scanner,
3271 token = g_scanner_get_next_token (scanner);
3272 if (token != GTK_RC_TOKEN_BG)
3273 return GTK_RC_TOKEN_BG;
3275 token = gtk_rc_parse_state (scanner, &state);
3276 if (token != G_TOKEN_NONE)
3279 token = g_scanner_get_next_token (scanner);
3280 if (token != G_TOKEN_EQUAL_SIGN)
3281 return G_TOKEN_EQUAL_SIGN;
3283 style->color_flags[state] |= GTK_RC_BG;
3284 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3288 gtk_rc_parse_fg (GScanner *scanner,
3294 token = g_scanner_get_next_token (scanner);
3295 if (token != GTK_RC_TOKEN_FG)
3296 return GTK_RC_TOKEN_FG;
3298 token = gtk_rc_parse_state (scanner, &state);
3299 if (token != G_TOKEN_NONE)
3302 token = g_scanner_get_next_token (scanner);
3303 if (token != G_TOKEN_EQUAL_SIGN)
3304 return G_TOKEN_EQUAL_SIGN;
3306 style->color_flags[state] |= GTK_RC_FG;
3307 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3311 gtk_rc_parse_text (GScanner *scanner,
3317 token = g_scanner_get_next_token (scanner);
3318 if (token != GTK_RC_TOKEN_TEXT)
3319 return GTK_RC_TOKEN_TEXT;
3321 token = gtk_rc_parse_state (scanner, &state);
3322 if (token != G_TOKEN_NONE)
3325 token = g_scanner_get_next_token (scanner);
3326 if (token != G_TOKEN_EQUAL_SIGN)
3327 return G_TOKEN_EQUAL_SIGN;
3329 style->color_flags[state] |= GTK_RC_TEXT;
3330 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3334 gtk_rc_parse_base (GScanner *scanner,
3340 token = g_scanner_get_next_token (scanner);
3341 if (token != GTK_RC_TOKEN_BASE)
3342 return GTK_RC_TOKEN_BASE;
3344 token = gtk_rc_parse_state (scanner, &state);
3345 if (token != G_TOKEN_NONE)
3348 token = g_scanner_get_next_token (scanner);
3349 if (token != G_TOKEN_EQUAL_SIGN)
3350 return G_TOKEN_EQUAL_SIGN;
3352 style->color_flags[state] |= GTK_RC_BASE;
3353 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3357 gtk_rc_parse_xthickness (GScanner *scanner,
3360 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3361 return GTK_RC_TOKEN_XTHICKNESS;
3363 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3364 return G_TOKEN_EQUAL_SIGN;
3366 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3369 style->xthickness = scanner->value.v_int;
3371 return G_TOKEN_NONE;
3375 gtk_rc_parse_ythickness (GScanner *scanner,
3378 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3379 return GTK_RC_TOKEN_YTHICKNESS;
3381 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3382 return G_TOKEN_EQUAL_SIGN;
3384 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3387 style->ythickness = scanner->value.v_int;
3389 return G_TOKEN_NONE;
3393 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3395 GtkRcStyle *rc_style)
3401 token = g_scanner_get_next_token (scanner);
3402 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3403 return GTK_RC_TOKEN_BG_PIXMAP;
3405 token = gtk_rc_parse_state (scanner, &state);
3406 if (token != G_TOKEN_NONE)
3409 token = g_scanner_get_next_token (scanner);
3410 if (token != G_TOKEN_EQUAL_SIGN)
3411 return G_TOKEN_EQUAL_SIGN;
3413 token = g_scanner_get_next_token (scanner);
3414 if (token != G_TOKEN_STRING)
3415 return G_TOKEN_STRING;
3417 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3418 (strcmp (scanner->value.v_string, "<none>") == 0))
3419 pixmap_file = g_strdup (scanner->value.v_string);
3421 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3422 scanner, scanner->value.v_string);
3426 g_free (rc_style->bg_pixmap_name[state]);
3427 rc_style->bg_pixmap_name[state] = pixmap_file;
3430 return G_TOKEN_NONE;
3434 gtk_rc_check_pixmap_dir (const gchar *dir,
3435 const gchar *pixmap_file)
3439 buf = g_build_filename (dir, pixmap_file, NULL);
3441 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3450 * gtk_rc_find_pixmap_in_path:
3451 * @settings: a #GtkSettings
3452 * @scanner: Scanner used to get line number information for the
3453 * warning message, or %NULL
3454 * @pixmap_file: name of the pixmap file to locate.
3456 * Looks up a file in pixmap path for the specified #GtkSettings.
3457 * If the file is not found, it outputs a warning message using
3458 * g_warning() and returns %NULL.
3460 * Return value: the filename.
3463 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3465 const gchar *pixmap_file)
3471 GtkRcContext *context = gtk_rc_context_get (settings);
3473 if (context->pixmap_path)
3474 for (i = 0; context->pixmap_path[i] != NULL; i++)
3476 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3481 tmp_list = current_files_stack;
3484 GtkRcFile *curfile = tmp_list->data;
3485 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3489 tmp_list = tmp_list->next;
3493 g_scanner_warn (scanner,
3494 _("Unable to locate image file in pixmap_path: \"%s\""),
3497 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3504 * gtk_rc_find_module_in_path:
3505 * @module_file: name of a theme engine
3507 * Searches for a theme engine in the GTK+ search path. This function
3508 * is not useful for applications and should not be used.
3510 * Return value: The filename, if found (must be freed with g_free()),
3514 gtk_rc_find_module_in_path (const gchar *module_file)
3516 return _gtk_find_module (module_file, "engines");
3520 gtk_rc_parse_font (GScanner *scanner,
3521 GtkRcStyle *rc_style)
3525 token = g_scanner_get_next_token (scanner);
3526 if (token != GTK_RC_TOKEN_FONT)
3527 return GTK_RC_TOKEN_FONT;
3529 token = g_scanner_get_next_token (scanner);
3530 if (token != G_TOKEN_EQUAL_SIGN)
3531 return G_TOKEN_EQUAL_SIGN;
3533 token = g_scanner_get_next_token (scanner);
3534 if (token != G_TOKEN_STRING)
3535 return G_TOKEN_STRING;
3537 /* Ignore, do nothing */
3539 return G_TOKEN_NONE;
3543 gtk_rc_parse_fontset (GScanner *scanner,
3544 GtkRcStyle *rc_style)
3548 token = g_scanner_get_next_token (scanner);
3549 if (token != GTK_RC_TOKEN_FONTSET)
3550 return GTK_RC_TOKEN_FONTSET;
3552 token = g_scanner_get_next_token (scanner);
3553 if (token != G_TOKEN_EQUAL_SIGN)
3554 return G_TOKEN_EQUAL_SIGN;
3556 token = g_scanner_get_next_token (scanner);
3557 if (token != G_TOKEN_STRING)
3558 return G_TOKEN_STRING;
3560 /* Do nothing - silently ignore */
3562 return G_TOKEN_NONE;
3566 gtk_rc_parse_font_name (GScanner *scanner,
3567 GtkRcStyle *rc_style)
3571 token = g_scanner_get_next_token (scanner);
3572 if (token != GTK_RC_TOKEN_FONT_NAME)
3573 return GTK_RC_TOKEN_FONT;
3575 token = g_scanner_get_next_token (scanner);
3576 if (token != G_TOKEN_EQUAL_SIGN)
3577 return G_TOKEN_EQUAL_SIGN;
3579 token = g_scanner_get_next_token (scanner);
3580 if (token != G_TOKEN_STRING)
3581 return G_TOKEN_STRING;
3583 if (rc_style->font_desc)
3584 pango_font_description_free (rc_style->font_desc);
3586 rc_style->font_desc =
3587 pango_font_description_from_string (scanner->value.v_string);
3589 return G_TOKEN_NONE;
3593 gtk_rc_parse_engine (GtkRcContext *context,
3595 GtkRcStyle **rc_style)
3598 GtkThemeEngine *engine;
3599 guint result = G_TOKEN_NONE;
3600 GtkRcStyle *new_style = NULL;
3601 gboolean parsed_curlies = FALSE;
3602 GtkRcStylePrivate *rc_priv, *new_priv;
3604 token = g_scanner_get_next_token (scanner);
3605 if (token != GTK_RC_TOKEN_ENGINE)
3606 return GTK_RC_TOKEN_ENGINE;
3608 token = g_scanner_get_next_token (scanner);
3609 if (token != G_TOKEN_STRING)
3610 return G_TOKEN_STRING;
3612 if (!scanner->value.v_string[0])
3614 /* Support engine "" {} to mean override to the default engine
3616 token = g_scanner_get_next_token (scanner);
3617 if (token != G_TOKEN_LEFT_CURLY)
3618 return G_TOKEN_LEFT_CURLY;
3620 token = g_scanner_get_next_token (scanner);
3621 if (token != G_TOKEN_RIGHT_CURLY)
3622 return G_TOKEN_RIGHT_CURLY;
3624 parsed_curlies = TRUE;
3626 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3628 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3630 new_style = gtk_rc_style_new ();
3631 gtk_rc_style_real_merge (new_style, *rc_style);
3633 new_style->name = g_strdup ((*rc_style)->name);
3635 /* take over icon factories and color hashes
3636 * from the to-be-deleted style
3638 new_style->icon_factories = (*rc_style)->icon_factories;
3639 (*rc_style)->icon_factories = NULL;
3640 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3641 new_priv->color_hashes = rc_priv->color_hashes;
3642 rc_priv->color_hashes = NULL;
3645 (*rc_style)->engine_specified = TRUE;
3649 engine = gtk_theme_engine_get (scanner->value.v_string);
3651 token = g_scanner_get_next_token (scanner);
3652 if (token != G_TOKEN_LEFT_CURLY)
3653 return G_TOKEN_LEFT_CURLY;
3657 GtkRcStyleClass *new_class;
3659 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3660 new_style = gtk_theme_engine_create_rc_style (engine);
3661 g_type_module_unuse (G_TYPE_MODULE (engine));
3663 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3665 new_class->merge (new_style, *rc_style);
3667 new_style->name = g_strdup ((*rc_style)->name);
3669 /* take over icon factories and color hashes
3670 * from the to-be-deleted style
3672 new_style->icon_factories = (*rc_style)->icon_factories;
3673 (*rc_style)->icon_factories = NULL;
3674 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3675 new_priv->color_hashes = rc_priv->color_hashes;
3676 rc_priv->color_hashes = NULL;
3678 if (new_class->parse)
3680 parsed_curlies = TRUE;
3681 result = new_class->parse (new_style, context->settings, scanner);
3683 if (result != G_TOKEN_NONE)
3685 /* copy icon factories and color hashes back
3687 (*rc_style)->icon_factories = new_style->icon_factories;
3688 new_style->icon_factories = NULL;
3689 rc_priv->color_hashes = new_priv->color_hashes;
3690 new_priv->color_hashes = NULL;
3692 g_object_unref (new_style);
3699 if (!parsed_curlies)
3701 /* Skip over remainder, looking for nested {}'s
3705 result = G_TOKEN_RIGHT_CURLY;
3706 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3708 if (token == G_TOKEN_LEFT_CURLY)
3710 else if (token == G_TOKEN_RIGHT_CURLY)
3715 result = G_TOKEN_NONE;
3723 new_style->engine_specified = TRUE;
3725 g_object_unref (*rc_style);
3726 *rc_style = new_style;
3733 gtk_rc_parse_state (GScanner *scanner,
3734 GtkStateType *state)
3739 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3740 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3742 /* we don't know where we got called from, so we reset the scope here.
3743 * if we bail out due to errors, we *don't* reset the scope, so the
3744 * error messaging code can make sense of our tokens.
3746 old_scope = g_scanner_set_scope (scanner, 0);
3748 token = g_scanner_get_next_token (scanner);
3749 if (token != G_TOKEN_LEFT_BRACE)
3750 return G_TOKEN_LEFT_BRACE;
3752 token = g_scanner_get_next_token (scanner);
3755 case GTK_RC_TOKEN_ACTIVE:
3756 *state = GTK_STATE_ACTIVE;
3758 case GTK_RC_TOKEN_INSENSITIVE:
3759 *state = GTK_STATE_INSENSITIVE;
3761 case GTK_RC_TOKEN_NORMAL:
3762 *state = GTK_STATE_NORMAL;
3764 case GTK_RC_TOKEN_PRELIGHT:
3765 *state = GTK_STATE_PRELIGHT;
3767 case GTK_RC_TOKEN_SELECTED:
3768 *state = GTK_STATE_SELECTED;
3771 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3774 token = g_scanner_get_next_token (scanner);
3775 if (token != G_TOKEN_RIGHT_BRACE)
3776 return G_TOKEN_RIGHT_BRACE;
3778 g_scanner_set_scope (scanner, old_scope);
3780 return G_TOKEN_NONE;
3784 gtk_rc_parse_priority (GScanner *scanner,
3785 GtkPathPriorityType *priority)
3790 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3791 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3793 /* we don't know where we got called from, so we reset the scope here.
3794 * if we bail out due to errors, we *don't* reset the scope, so the
3795 * error messaging code can make sense of our tokens.
3797 old_scope = g_scanner_set_scope (scanner, 0);
3799 token = g_scanner_get_next_token (scanner);
3803 token = g_scanner_get_next_token (scanner);
3806 case GTK_RC_TOKEN_LOWEST:
3807 *priority = GTK_PATH_PRIO_LOWEST;
3809 case GTK_RC_TOKEN_GTK:
3810 *priority = GTK_PATH_PRIO_GTK;
3812 case GTK_RC_TOKEN_APPLICATION:
3813 *priority = GTK_PATH_PRIO_APPLICATION;
3815 case GTK_RC_TOKEN_THEME:
3816 *priority = GTK_PATH_PRIO_THEME;
3818 case GTK_RC_TOKEN_RC:
3819 *priority = GTK_PATH_PRIO_RC;
3821 case GTK_RC_TOKEN_HIGHEST:
3822 *priority = GTK_PATH_PRIO_HIGHEST;
3825 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3828 g_scanner_set_scope (scanner, old_scope);
3830 return G_TOKEN_NONE;
3834 * gtk_rc_parse_color:
3835 * @scanner: a #GScanner
3836 * @color: a pointer to a #GdkColor structure in which to store the result
3838 * Parses a color in the <link linkend="color=format">format</link> expected
3841 * Note that theme engines should use gtk_rc_parse_color_full() in
3842 * order to support symbolic colors.
3844 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3845 * that was expected but not found
3848 gtk_rc_parse_color (GScanner *scanner,
3851 return gtk_rc_parse_color_full (scanner, NULL, color);
3855 * gtk_rc_parse_color_full:
3856 * @scanner: a #GScanner
3857 * @style: a #GtkRcStyle, or %NULL
3858 * @color: a pointer to a #GdkColor structure in which to store the result
3860 * Parses a color in the <link linkend="color=format">format</link> expected
3861 * in a RC file. If @style is not %NULL, it will be consulted to resolve
3862 * references to symbolic colors.
3864 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3865 * that was expected but not found
3870 gtk_rc_parse_color_full (GScanner *scanner,
3876 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3878 /* we don't need to set our own scope here, because
3879 * we don't need own symbols
3882 token = g_scanner_get_next_token (scanner);
3890 case G_TOKEN_LEFT_CURLY:
3891 token = g_scanner_get_next_token (scanner);
3892 if (token == G_TOKEN_INT)
3893 token_int = scanner->value.v_int;
3894 else if (token == G_TOKEN_FLOAT)
3895 token_int = scanner->value.v_float * 65535.0;
3897 return G_TOKEN_FLOAT;
3898 color->red = CLAMP (token_int, 0, 65535);
3900 token = g_scanner_get_next_token (scanner);
3901 if (token != G_TOKEN_COMMA)
3902 return G_TOKEN_COMMA;
3904 token = g_scanner_get_next_token (scanner);
3905 if (token == G_TOKEN_INT)
3906 token_int = scanner->value.v_int;
3907 else if (token == G_TOKEN_FLOAT)
3908 token_int = scanner->value.v_float * 65535.0;
3910 return G_TOKEN_FLOAT;
3911 color->green = CLAMP (token_int, 0, 65535);
3913 token = g_scanner_get_next_token (scanner);
3914 if (token != G_TOKEN_COMMA)
3915 return G_TOKEN_COMMA;
3917 token = g_scanner_get_next_token (scanner);
3918 if (token == G_TOKEN_INT)
3919 token_int = scanner->value.v_int;
3920 else if (token == G_TOKEN_FLOAT)
3921 token_int = scanner->value.v_float * 65535.0;
3923 return G_TOKEN_FLOAT;
3924 color->blue = CLAMP (token_int, 0, 65535);
3926 token = g_scanner_get_next_token (scanner);
3927 if (token != G_TOKEN_RIGHT_CURLY)
3928 return G_TOKEN_RIGHT_CURLY;
3929 return G_TOKEN_NONE;
3931 case G_TOKEN_STRING:
3932 if (!gdk_color_parse (scanner->value.v_string, color))
3934 g_scanner_warn (scanner, "Invalid color constant '%s'",
3935 scanner->value.v_string);
3936 return G_TOKEN_STRING;
3938 return G_TOKEN_NONE;
3941 token = g_scanner_get_next_token (scanner);
3942 if (token != G_TOKEN_IDENTIFIER)
3943 return G_TOKEN_IDENTIFIER;
3945 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3947 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3948 scanner->value.v_identifier);
3949 return G_TOKEN_IDENTIFIER;
3952 return G_TOKEN_NONE;
3954 case G_TOKEN_IDENTIFIER:
3955 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3957 token = g_scanner_get_next_token (scanner);
3958 if (token != G_TOKEN_LEFT_PAREN)
3959 return G_TOKEN_LEFT_PAREN;
3962 if (g_scanner_peek_next_token (scanner) == '-')
3964 g_scanner_get_next_token (scanner); /* eat sign */
3968 token = g_scanner_get_next_token (scanner);
3969 if (token != G_TOKEN_FLOAT)
3970 return G_TOKEN_FLOAT;
3972 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3974 token = g_scanner_get_next_token (scanner);
3975 if (token != G_TOKEN_COMMA)
3976 return G_TOKEN_COMMA;
3978 token = gtk_rc_parse_color_full (scanner, style, &c1);
3979 if (token != G_TOKEN_NONE)
3982 token = g_scanner_get_next_token (scanner);
3983 if (token != G_TOKEN_COMMA)
3984 return G_TOKEN_COMMA;
3986 token = gtk_rc_parse_color_full (scanner, style, &c2);
3987 if (token != G_TOKEN_NONE)
3990 token = g_scanner_get_next_token (scanner);
3991 if (token != G_TOKEN_RIGHT_PAREN)
3992 return G_TOKEN_RIGHT_PAREN;
3994 color->red = l * c1.red + (1.0 - l) * c2.red;
3995 color->green = l * c1.green + (1.0 - l) * c2.green;
3996 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3998 return G_TOKEN_NONE;
4000 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
4002 token = g_scanner_get_next_token (scanner);
4003 if (token != G_TOKEN_LEFT_PAREN)
4004 return G_TOKEN_LEFT_PAREN;
4007 if (g_scanner_peek_next_token (scanner) == '-')
4009 g_scanner_get_next_token (scanner); /* eat sign */
4013 token = g_scanner_get_next_token (scanner);
4014 if (token != G_TOKEN_FLOAT)
4015 return G_TOKEN_FLOAT;
4017 l = negate ? -scanner->value.v_float : scanner->value.v_float;
4019 token = g_scanner_get_next_token (scanner);
4020 if (token != G_TOKEN_COMMA)
4021 return G_TOKEN_COMMA;
4023 token = gtk_rc_parse_color_full (scanner, style, &c1);
4024 if (token != G_TOKEN_NONE)
4027 token = g_scanner_get_next_token (scanner);
4028 if (token != G_TOKEN_RIGHT_PAREN)
4029 return G_TOKEN_RIGHT_PAREN;
4031 _gtk_style_shade (&c1, color, l);
4033 return G_TOKEN_NONE;
4035 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4036 strcmp (scanner->value.v_identifier, "darker") == 0)
4038 if (scanner->value.v_identifier[0] == 'l')
4043 token = g_scanner_get_next_token (scanner);
4044 if (token != G_TOKEN_LEFT_PAREN)
4045 return G_TOKEN_LEFT_PAREN;
4047 token = gtk_rc_parse_color_full (scanner, style, &c1);
4048 if (token != G_TOKEN_NONE)
4051 token = g_scanner_get_next_token (scanner);
4052 if (token != G_TOKEN_RIGHT_PAREN)
4053 return G_TOKEN_RIGHT_PAREN;
4055 _gtk_style_shade (&c1, color, l);
4057 return G_TOKEN_NONE;
4060 return G_TOKEN_IDENTIFIER;
4063 return G_TOKEN_STRING;
4068 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4073 token = g_scanner_get_next_token (scanner);
4074 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4075 return GTK_RC_TOKEN_PIXMAP_PATH;
4077 token = g_scanner_get_next_token (scanner);
4078 if (token != G_TOKEN_STRING)
4079 return G_TOKEN_STRING;
4081 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4083 return G_TOKEN_NONE;
4087 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4089 const gchar *pix_path)
4091 g_strfreev (context->pixmap_path);
4092 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4096 gtk_rc_parse_module_path (GScanner *scanner)
4100 token = g_scanner_get_next_token (scanner);
4101 if (token != GTK_RC_TOKEN_MODULE_PATH)
4102 return GTK_RC_TOKEN_MODULE_PATH;
4104 token = g_scanner_get_next_token (scanner);
4105 if (token != G_TOKEN_STRING)
4106 return G_TOKEN_STRING;
4108 g_warning ("module_path directive is now ignored\n");
4110 return G_TOKEN_NONE;
4114 gtk_rc_parse_im_module_file (GScanner *scanner)
4118 token = g_scanner_get_next_token (scanner);
4119 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4120 return GTK_RC_TOKEN_IM_MODULE_FILE;
4122 token = g_scanner_get_next_token (scanner);
4123 if (token != G_TOKEN_STRING)
4124 return G_TOKEN_STRING;
4126 g_free (im_module_file);
4128 im_module_file = g_strdup (scanner->value.v_string);
4130 return G_TOKEN_NONE;
4134 gtk_rc_parse_path_pattern (GtkRcContext *context,
4138 GtkPathType path_type;
4140 gboolean is_binding;
4141 GtkPathPriorityType priority = context->default_priority;
4143 token = g_scanner_get_next_token (scanner);
4146 case GTK_RC_TOKEN_WIDGET:
4147 path_type = GTK_PATH_WIDGET;
4149 case GTK_RC_TOKEN_WIDGET_CLASS:
4150 path_type = GTK_PATH_WIDGET_CLASS;
4152 case GTK_RC_TOKEN_CLASS:
4153 path_type = GTK_PATH_CLASS;
4156 return GTK_RC_TOKEN_WIDGET_CLASS;
4159 token = g_scanner_get_next_token (scanner);
4160 if (token != G_TOKEN_STRING)
4161 return G_TOKEN_STRING;
4163 pattern = g_strdup (scanner->value.v_string);
4165 token = g_scanner_get_next_token (scanner);
4166 if (token == GTK_RC_TOKEN_STYLE)
4168 else if (token == GTK_RC_TOKEN_BINDING)
4173 return GTK_RC_TOKEN_STYLE;
4176 if (g_scanner_peek_next_token (scanner) == ':')
4178 token = gtk_rc_parse_priority (scanner, &priority);
4179 if (token != G_TOKEN_NONE)
4186 token = g_scanner_get_next_token (scanner);
4187 if (token != G_TOKEN_STRING)
4190 return G_TOKEN_STRING;
4195 GtkBindingSet *binding;
4197 binding = gtk_binding_set_find (scanner->value.v_string);
4201 return G_TOKEN_STRING;
4203 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4207 GtkRcStyle *rc_style;
4210 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4215 return G_TOKEN_STRING;
4218 rc_set = g_new (GtkRcSet, 1);
4219 rc_set->type = path_type;
4221 if (path_type == GTK_PATH_WIDGET_CLASS)
4223 rc_set->pspec = NULL;
4224 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4228 rc_set->pspec = g_pattern_spec_new (pattern);
4229 rc_set->path = NULL;
4232 rc_set->rc_style = rc_style;
4233 rc_set->priority = priority;
4235 if (path_type == GTK_PATH_WIDGET)
4236 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4237 else if (path_type == GTK_PATH_WIDGET_CLASS)
4238 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4240 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4244 return G_TOKEN_NONE;
4248 gtk_rc_parse_hash_key (GScanner *scanner,
4253 token = g_scanner_get_next_token (scanner);
4254 if (token != G_TOKEN_LEFT_BRACE)
4255 return G_TOKEN_LEFT_BRACE;
4257 token = g_scanner_get_next_token (scanner);
4259 if (token != G_TOKEN_STRING)
4260 return G_TOKEN_STRING;
4262 *hash_key = g_strdup (scanner->value.v_string);
4264 token = g_scanner_get_next_token (scanner);
4265 if (token != G_TOKEN_RIGHT_BRACE)
4268 return G_TOKEN_RIGHT_BRACE;
4271 return G_TOKEN_NONE;
4275 gtk_rc_parse_icon_source (GtkRcContext *context,
4277 GtkIconSet *icon_set,
4278 gboolean *icon_set_valid)
4281 gchar *full_filename;
4282 GtkIconSource *source = NULL;
4284 token = g_scanner_get_next_token (scanner);
4285 if (token != G_TOKEN_LEFT_CURLY)
4286 return G_TOKEN_LEFT_CURLY;
4288 token = g_scanner_get_next_token (scanner);
4290 if (token != G_TOKEN_STRING && token != '@')
4291 return G_TOKEN_STRING;
4293 if (token == G_TOKEN_STRING)
4297 source = gtk_icon_source_new ();
4298 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4301 gtk_icon_source_set_filename (source, full_filename);
4302 g_free (full_filename);
4309 token = g_scanner_get_next_token (scanner);
4311 if (token != G_TOKEN_STRING)
4312 return G_TOKEN_STRING;
4314 source = gtk_icon_source_new ();
4315 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4318 /* We continue parsing even if we didn't find the pixmap so that rest of the
4319 * file is read, even if the syntax is bad. However we don't validate the
4320 * icon_set so the caller can choose not to install it.
4322 token = g_scanner_get_next_token (scanner);
4324 if (token == G_TOKEN_RIGHT_CURLY)
4326 else if (token != G_TOKEN_COMMA)
4328 gtk_icon_source_free (source);
4329 return G_TOKEN_COMMA;
4332 /* Get the direction */
4334 token = g_scanner_get_next_token (scanner);
4338 case GTK_RC_TOKEN_RTL:
4339 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4340 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4343 case GTK_RC_TOKEN_LTR:
4344 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4345 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4352 gtk_icon_source_free (source);
4353 return GTK_RC_TOKEN_RTL;
4357 token = g_scanner_get_next_token (scanner);
4359 if (token == G_TOKEN_RIGHT_CURLY)
4361 else if (token != G_TOKEN_COMMA)
4363 gtk_icon_source_free (source);
4364 return G_TOKEN_COMMA;
4369 token = g_scanner_get_next_token (scanner);
4373 case GTK_RC_TOKEN_NORMAL:
4374 gtk_icon_source_set_state_wildcarded (source, FALSE);
4375 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4378 case GTK_RC_TOKEN_PRELIGHT:
4379 gtk_icon_source_set_state_wildcarded (source, FALSE);
4380 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4384 case GTK_RC_TOKEN_INSENSITIVE:
4385 gtk_icon_source_set_state_wildcarded (source, FALSE);
4386 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4389 case GTK_RC_TOKEN_ACTIVE:
4390 gtk_icon_source_set_state_wildcarded (source, FALSE);
4391 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4394 case GTK_RC_TOKEN_SELECTED:
4395 gtk_icon_source_set_state_wildcarded (source, FALSE);
4396 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4403 gtk_icon_source_free (source);
4404 return GTK_RC_TOKEN_PRELIGHT;
4408 token = g_scanner_get_next_token (scanner);
4410 if (token == G_TOKEN_RIGHT_CURLY)
4412 else if (token != G_TOKEN_COMMA)
4414 gtk_icon_source_free (source);
4415 return G_TOKEN_COMMA;
4420 token = g_scanner_get_next_token (scanner);
4426 if (token != G_TOKEN_STRING)
4428 gtk_icon_source_free (source);
4429 return G_TOKEN_STRING;
4432 size = gtk_icon_size_from_name (scanner->value.v_string);
4434 if (size != GTK_ICON_SIZE_INVALID)
4436 gtk_icon_source_set_size_wildcarded (source, FALSE);
4437 gtk_icon_source_set_size (source, size);
4441 /* Check the close brace */
4443 token = g_scanner_get_next_token (scanner);
4444 if (token != G_TOKEN_RIGHT_CURLY)
4446 gtk_icon_source_free (source);
4447 return G_TOKEN_RIGHT_CURLY;
4451 if (gtk_icon_source_get_filename (source) ||
4452 gtk_icon_source_get_icon_name (source))
4454 gtk_icon_set_add_source (icon_set, source);
4455 *icon_set_valid = TRUE;
4457 gtk_icon_source_free (source);
4459 return G_TOKEN_NONE;
4463 gtk_rc_parse_stock (GtkRcContext *context,
4465 GtkRcStyle *rc_style,
4466 GtkIconFactory *factory)
4468 GtkIconSet *icon_set = NULL;
4469 gboolean icon_set_valid = FALSE;
4470 gchar *stock_id = NULL;
4473 token = g_scanner_get_next_token (scanner);
4474 if (token != GTK_RC_TOKEN_STOCK)
4475 return GTK_RC_TOKEN_STOCK;
4477 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4478 if (token != G_TOKEN_NONE)
4481 token = g_scanner_get_next_token (scanner);
4482 if (token != G_TOKEN_EQUAL_SIGN)
4485 return G_TOKEN_EQUAL_SIGN;
4488 token = g_scanner_get_next_token (scanner);
4489 if (token != G_TOKEN_LEFT_CURLY)
4492 return G_TOKEN_LEFT_CURLY;
4495 token = g_scanner_peek_next_token (scanner);
4496 while (token != G_TOKEN_RIGHT_CURLY)
4498 if (icon_set == NULL)
4499 icon_set = gtk_icon_set_new ();
4501 token = gtk_rc_parse_icon_source (context,
4502 scanner, icon_set, &icon_set_valid);
4503 if (token != G_TOKEN_NONE)
4506 gtk_icon_set_unref (icon_set);
4510 token = g_scanner_get_next_token (scanner);
4512 if (token != G_TOKEN_COMMA &&
4513 token != G_TOKEN_RIGHT_CURLY)
4516 gtk_icon_set_unref (icon_set);
4517 return G_TOKEN_RIGHT_CURLY;
4524 gtk_icon_factory_add (factory,
4528 gtk_icon_set_unref (icon_set);
4533 return G_TOKEN_NONE;
4537 gtk_rc_parse_logical_color (GScanner *scanner,
4538 GtkRcStyle *rc_style,
4541 gchar *color_id = NULL;
4545 token = g_scanner_get_next_token (scanner);
4546 if (token != GTK_RC_TOKEN_COLOR)
4547 return GTK_RC_TOKEN_COLOR;
4549 token = gtk_rc_parse_hash_key (scanner, &color_id);
4550 if (token != G_TOKEN_NONE)
4553 token = g_scanner_get_next_token (scanner);
4554 if (token != G_TOKEN_EQUAL_SIGN)
4557 return G_TOKEN_EQUAL_SIGN;
4560 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4561 if (token != G_TOKEN_NONE)
4567 /* Because the hash is created with destroy functions,
4568 * g_hash_table_insert will free any old values for us,
4569 * if a mapping with the specified key already exists.
4571 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4573 return G_TOKEN_NONE;
4578 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4582 const gchar *current;
4583 const gchar *class_start;
4584 const gchar *class_end;
4585 const gchar *pattern_end;
4586 const gchar *pattern_start;
4591 while ((class_start = strchr (current, '<')) &&
4592 (class_end = strchr (class_start, '>')))
4594 /* Add patterns, but ignore single dots */
4595 if (!(class_start == current ||
4596 (class_start == current + 1 && current[0] == '.')))
4598 pattern_end = class_start - 1;
4599 pattern_start = current;
4601 path_elt = g_new (PathElt, 1);
4603 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4604 path_elt->type = PATH_ELT_PSPEC;
4605 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4606 g_free (sub_pattern);
4608 result = g_slist_prepend (result, path_elt);
4611 path_elt = g_new (PathElt, 1);
4613 /* The < > need to be removed from the string. */
4614 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4616 path_elt->type = PATH_ELT_UNRESOLVED;
4617 path_elt->elt.class_name = sub_pattern;
4619 result = g_slist_prepend (result, path_elt);
4621 current = class_end + 1;
4624 /* Add the rest, if anything is left */
4625 if (strlen (current) > 0)
4627 path_elt = g_new (PathElt, 1);
4628 path_elt->type = PATH_ELT_PSPEC;
4629 path_elt->elt.pspec = g_pattern_spec_new (current);
4631 result = g_slist_prepend (result, path_elt);
4634 return g_slist_reverse (result);
4638 free_path_elt (gpointer data,
4641 PathElt *path_elt = data;
4643 switch (path_elt->type)
4645 case PATH_ELT_PSPEC:
4646 g_pattern_spec_free (path_elt->elt.pspec);
4648 case PATH_ELT_UNRESOLVED:
4649 g_free (path_elt->elt.class_name);
4654 g_assert_not_reached ();
4661 _gtk_rc_free_widget_class_path (GSList *list)
4663 g_slist_foreach (list, free_path_elt, NULL);
4664 g_slist_free (list);
4668 gtk_rc_set_free (GtkRcSet *rc_set)
4671 g_pattern_spec_free (rc_set->pspec);
4673 _gtk_rc_free_widget_class_path (rc_set->path);
4679 match_class (PathElt *path_elt,
4684 if (path_elt->type == PATH_ELT_UNRESOLVED)
4686 type = g_type_from_name (path_elt->elt.class_name);
4687 if (type != G_TYPE_INVALID)
4689 g_free (path_elt->elt.class_name);
4690 path_elt->elt.class_type = type;
4691 path_elt->type = PATH_ELT_TYPE;
4694 return g_str_equal (type_name, path_elt->elt.class_name);
4697 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4701 match_widget_class_recursive (GSList *list,
4704 gchar *path_reversed)
4708 /* break out if we cannot match anymore. */
4717 /* there are two possibilities:
4718 * 1. The next pattern should match the class.
4719 * 2. First normal matching, and then maybe a class */
4721 path_elt = list->data;
4723 if (path_elt->type != PATH_ELT_PSPEC)
4725 gchar *class_start = path;
4728 /* ignore leading dot */
4729 if (class_start[0] == '.')
4731 class_end = strchr (class_start, '.');
4733 if (class_end == NULL)
4735 if (!match_class (path_elt, class_start))
4738 return match_widget_class_recursive (list->next, 0, "", "");
4742 class_end[0] = '\0';
4743 if (!match_class (path_elt, class_start))
4751 gint new_length = length - (class_end - path);
4752 gchar old_char = path_reversed[new_length];
4756 path_reversed[new_length] = '\0';
4757 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4758 path_reversed[new_length] = old_char;
4769 gboolean result = FALSE;
4771 /* If there is nothing after this (ie. no class match),
4772 * just compare the pspec.
4774 if (list->next == NULL)
4775 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4777 class_elt = (PathElt *)list->next->data;
4778 g_assert (class_elt->type != PATH_ELT_PSPEC);
4781 if (class_start[0] == '.')
4786 class_end = strchr (class_start, '.');
4788 /* It should be cheaper to match the class first. (either the pattern
4789 * is simple, and will match most of the times, or it may be complex
4790 * and matching is slow)
4792 if (class_end == NULL)
4794 result = match_class (class_elt, class_start);
4798 class_end[0] = '\0';
4799 result = match_class (class_elt, class_start);
4808 /* terminate the string in front of the class. It does not matter
4809 * that the class becomes unusable, because it is not needed
4810 * inside the recursion
4812 old_char = class_start[0];
4813 class_start[0] = '\0';
4815 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4817 if (class_end != NULL)
4819 gint new_length = length - (class_end - path);
4820 gchar path_reversed_char = path_reversed[new_length];
4822 path_reversed[new_length] = '\0';
4824 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4826 path_reversed[new_length] = path_reversed_char;
4829 result = match_widget_class_recursive (list->next->next, 0, "", "");
4832 class_start[0] = old_char;
4838 /* get next class in path, or break out */
4839 if (class_end != NULL)
4840 class_start = class_end + 1;
4848 _gtk_rc_match_widget_class (GSList *list,
4851 gchar *path_reversed)
4853 return match_widget_class_recursive (list, length, path, path_reversed);
4856 #if defined (G_OS_WIN32) && !defined (_WIN64)
4858 /* DLL ABI stability backward compatibility versions */
4860 #undef gtk_rc_add_default_file
4863 gtk_rc_add_default_file (const gchar *filename)
4865 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4867 gtk_rc_add_default_file_utf8 (utf8_filename);
4869 g_free (utf8_filename);
4872 #undef gtk_rc_set_default_files
4875 gtk_rc_set_default_files (gchar **filenames)
4877 gchar **utf8_filenames;
4880 while (filenames[n++] != NULL)
4883 utf8_filenames = g_new (gchar *, n + 1);
4885 for (i = 0; i < n; i++)
4886 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4888 utf8_filenames[n] = NULL;
4890 gtk_rc_set_default_files_utf8 (utf8_filenames);
4892 g_strfreev (utf8_filenames);
4898 gtk_rc_parse (const gchar *filename)
4900 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4902 gtk_rc_parse_utf8 (utf8_filename);
4904 g_free (utf8_filename);
4909 #define __GTK_RC_C__
4910 #include "gtkaliasdef.c"