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,
221 static guint gtk_rc_parse_color_full (GScanner *scanner,
225 static void gtk_rc_clear_hash_node (gpointer key,
228 static void gtk_rc_clear_styles (GtkRcContext *context);
229 static void gtk_rc_add_initial_default_files (void);
231 static void gtk_rc_style_finalize (GObject *object);
232 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
234 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
235 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
236 static void gtk_rc_style_copy_icons_and_colors(GtkRcStyle *rc_style,
237 GtkRcStyle *src_style,
238 GtkRcContext *context);
239 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
240 gconstpointer bsearch_node2);
241 static void gtk_rc_set_free (GtkRcSet *rc_set);
244 static const GScannerConfig gtk_rc_scanner_config =
248 ) /* cset_skip_characters */,
253 ) /* cset_identifier_first */,
259 ) /* cset_identifier_nth */,
260 ( "#\n" ) /* cpair_comment_single */,
262 TRUE /* case_sensitive */,
264 TRUE /* skip_comment_multi */,
265 TRUE /* skip_comment_single */,
266 TRUE /* scan_comment_multi */,
267 TRUE /* scan_identifier */,
268 FALSE /* scan_identifier_1char */,
269 FALSE /* scan_identifier_NULL */,
270 TRUE /* scan_symbols */,
271 TRUE /* scan_binary */,
272 TRUE /* scan_octal */,
273 TRUE /* scan_float */,
275 TRUE /* scan_hex_dollar */,
276 TRUE /* scan_string_sq */,
277 TRUE /* scan_string_dq */,
278 TRUE /* numbers_2_int */,
279 FALSE /* int_2_float */,
280 FALSE /* identifier_2_string */,
281 TRUE /* char_2_token */,
282 TRUE /* symbol_2_token */,
283 FALSE /* scope_0_fallback */,
286 static const gchar symbol_names[] =
330 { 0, GTK_RC_TOKEN_INCLUDE },
331 { 8, GTK_RC_TOKEN_NORMAL },
332 { 15, GTK_RC_TOKEN_ACTIVE },
333 { 22, GTK_RC_TOKEN_PRELIGHT },
334 { 31, GTK_RC_TOKEN_SELECTED },
335 { 40, GTK_RC_TOKEN_INSENSITIVE },
336 { 52, GTK_RC_TOKEN_FG },
337 { 55, GTK_RC_TOKEN_BG },
338 { 58, GTK_RC_TOKEN_TEXT },
339 { 63, GTK_RC_TOKEN_BASE },
340 { 68, GTK_RC_TOKEN_XTHICKNESS },
341 { 79, GTK_RC_TOKEN_YTHICKNESS },
342 { 90, GTK_RC_TOKEN_FONT },
343 { 95, GTK_RC_TOKEN_FONTSET },
344 { 103, GTK_RC_TOKEN_FONT_NAME },
345 { 113, GTK_RC_TOKEN_BG_PIXMAP },
346 { 123, GTK_RC_TOKEN_PIXMAP_PATH },
347 { 135, GTK_RC_TOKEN_STYLE },
348 { 141, GTK_RC_TOKEN_BINDING },
349 { 149, GTK_RC_TOKEN_BIND },
350 { 154, GTK_RC_TOKEN_WIDGET },
351 { 161, GTK_RC_TOKEN_WIDGET_CLASS },
352 { 174, GTK_RC_TOKEN_CLASS },
353 { 180, GTK_RC_TOKEN_LOWEST },
354 { 187, GTK_RC_TOKEN_GTK },
355 { 191, GTK_RC_TOKEN_APPLICATION },
356 { 203, GTK_RC_TOKEN_THEME },
357 { 209, GTK_RC_TOKEN_RC },
358 { 212, GTK_RC_TOKEN_HIGHEST },
359 { 220, GTK_RC_TOKEN_ENGINE },
360 { 227, GTK_RC_TOKEN_MODULE_PATH },
361 { 239, GTK_RC_TOKEN_STOCK },
362 { 245, GTK_RC_TOKEN_IM_MODULE_FILE },
363 { 260, GTK_RC_TOKEN_LTR },
364 { 264, GTK_RC_TOKEN_RTL },
365 { 268, GTK_RC_TOKEN_COLOR },
366 { 274, GTK_RC_TOKEN_UNBIND }
369 static GHashTable *realized_style_ht = NULL;
371 static gchar *im_module_file = NULL;
373 static gint max_default_files = 0;
374 static gchar **gtk_rc_default_files = NULL;
376 /* A stack of information of RC files we are parsing currently.
377 * The directories for these files are implicitely added to the end of
380 static GSList *current_files_stack = NULL;
382 /* RC files and strings that are parsed for every context
384 static GSList *global_rc_files = NULL;
386 /* Keep list of all current RC contexts for convenience
388 static GSList *rc_contexts;
390 /* RC file handling */
393 gtk_rc_make_default_dir (const gchar *type)
398 var = g_getenv ("GTK_EXE_PREFIX");
401 path = g_build_filename (var, "lib", "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
403 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
409 * gtk_rc_get_im_module_path:
410 * @returns: a newly-allocated string containing the path in which to
411 * look for IM modules.
413 * Obtains the path in which to look for IM modules. See the documentation
414 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
415 * environment variable for more details about looking up modules. This
416 * function is useful solely for utilities supplied with GTK+ and should
417 * not be used by applications under normal circumstances.
420 gtk_rc_get_im_module_path (void)
422 gchar **paths = _gtk_get_module_path ("immodules");
423 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
430 * gtk_rc_get_im_module_file:
431 * @returns: a newly-allocated string containing the name of the file
432 * listing the IM modules available for loading
434 * Obtains the path to the IM modules file. See the documentation
435 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
436 * environment variable for more details.
439 gtk_rc_get_im_module_file (void)
441 const gchar *var = g_getenv ("GTK_IM_MODULE_FILE");
442 gchar *result = NULL;
445 result = g_strdup (var);
450 result = g_strdup (im_module_file);
452 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
459 gtk_rc_get_theme_dir (void)
464 var = g_getenv ("GTK_DATA_PREFIX");
467 path = g_build_filename (var, "share", "themes", NULL);
469 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
475 * gtk_rc_get_module_dir:
477 * Returns a directory in which GTK+ looks for theme engines.
478 * For full information about the search for theme engines,
479 * see the docs for <envar>GTK_PATH</envar> in
480 * <xref linkend="gtk-running"/>.
482 * return value: the directory. (Must be freed with g_free())
485 gtk_rc_get_module_dir (void)
487 return gtk_rc_make_default_dir ("engines");
491 gtk_rc_add_initial_default_files (void)
493 static gint init = FALSE;
502 gtk_rc_default_files = g_new (gchar*, 10);
503 max_default_files = 10;
505 gtk_rc_default_files[0] = NULL;
508 var = g_getenv ("GTK2_RC_FILES");
512 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, -1);
516 gtk_rc_add_default_file (files[i]);
524 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
526 gtk_rc_add_default_file (str);
529 home = g_get_home_dir ();
532 str = g_build_filename (home, ".gtkrc-2.0", NULL);
533 gtk_rc_add_default_file (str);
540 * gtk_rc_add_default_file:
541 * @filename: the pathname to the file. If @filename is not absolute, it
542 * is searched in the current directory.
544 * Adds a file to the list of files to be parsed at the
548 gtk_rc_add_default_file (const gchar *filename)
552 gtk_rc_add_initial_default_files ();
554 for (n = 0; n < max_default_files; n++)
556 if (gtk_rc_default_files[n] == NULL)
560 if (n == max_default_files)
562 max_default_files += 10;
563 gtk_rc_default_files = g_renew (gchar*, gtk_rc_default_files, max_default_files);
566 gtk_rc_default_files[n++] = g_strdup (filename);
567 gtk_rc_default_files[n] = NULL;
571 * gtk_rc_set_default_files:
572 * @filenames: A %NULL-terminated list of filenames.
574 * Sets the list of files that GTK+ will read at the
578 gtk_rc_set_default_files (gchar **filenames)
582 gtk_rc_add_initial_default_files ();
585 while (gtk_rc_default_files[i])
587 g_free (gtk_rc_default_files[i]);
591 gtk_rc_default_files[0] = NULL;
594 while (filenames[i] != NULL)
596 gtk_rc_add_default_file (filenames[i]);
602 * gtk_rc_get_default_files:
604 * Retrieves the current list of RC files that will be parsed
605 * at the end of gtk_init().
607 * Return value: A %NULL-terminated array of filenames. This memory
608 * is owned by GTK+ and must not be freed by the application.
609 * If you want to store this information, you should make a copy.
612 gtk_rc_get_default_files (void)
614 gtk_rc_add_initial_default_files ();
616 return gtk_rc_default_files;
620 gtk_rc_settings_changed (GtkSettings *settings,
622 GtkRcContext *context)
624 gchar *new_theme_name;
625 gchar *new_key_theme_name;
627 if (context->reloading)
630 g_object_get (settings,
631 "gtk-theme-name", &new_theme_name,
632 "gtk-key-theme-name", &new_key_theme_name,
635 if ((new_theme_name != context->theme_name &&
636 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
637 (new_key_theme_name != context->key_theme_name &&
638 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
640 gtk_rc_reparse_all_for_settings (settings, TRUE);
643 g_free (new_theme_name);
644 g_free (new_key_theme_name);
648 gtk_rc_font_name_changed (GtkSettings *settings,
650 GtkRcContext *context)
652 if (!context->reloading)
653 _gtk_rc_context_get_default_font_name (settings);
657 gtk_rc_color_hash_changed (GtkSettings *settings,
659 GtkRcContext *context)
661 if (context->color_hash)
662 g_hash_table_unref (context->color_hash);
664 g_object_get (settings, "color-hash", &context->color_hash, NULL);
666 gtk_rc_reparse_all_for_settings (settings, TRUE);
669 static GtkRcContext *
670 gtk_rc_context_get (GtkSettings *settings)
672 if (!settings->rc_context)
674 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
676 context->settings = settings;
677 context->rc_style_ht = NULL;
678 context->rc_sets_widget = NULL;
679 context->rc_sets_widget_class = NULL;
680 context->rc_sets_class = NULL;
681 context->rc_files = NULL;
682 context->default_style = NULL;
683 context->reloading = FALSE;
685 g_object_get (settings,
686 "gtk-theme-name", &context->theme_name,
687 "gtk-key-theme-name", &context->key_theme_name,
688 "gtk-font-name", &context->font_name,
689 "color-hash", &context->color_hash,
692 g_signal_connect (settings,
693 "notify::gtk-theme-name",
694 G_CALLBACK (gtk_rc_settings_changed),
696 g_signal_connect (settings,
697 "notify::gtk-key-theme-name",
698 G_CALLBACK (gtk_rc_settings_changed),
700 g_signal_connect (settings,
701 "notify::gtk-font-name",
702 G_CALLBACK (gtk_rc_font_name_changed),
704 g_signal_connect (settings,
705 "notify::color-hash",
706 G_CALLBACK (gtk_rc_color_hash_changed),
709 context->pixmap_path = NULL;
711 context->default_priority = GTK_PATH_PRIO_RC;
713 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
716 return settings->rc_context;
720 gtk_rc_clear_rc_files (GtkRcContext *context)
724 list = context->rc_files;
727 GtkRcFile *rc_file = list->data;
729 if (rc_file->canonical_name != rc_file->name)
730 g_free (rc_file->canonical_name);
731 g_free (rc_file->directory);
732 g_free (rc_file->name);
738 g_slist_free (context->rc_files);
739 context->rc_files = NULL;
743 _gtk_rc_context_destroy (GtkSettings *settings)
745 GtkRcContext *context;
747 g_return_if_fail (GTK_IS_SETTINGS (settings));
749 context = settings->rc_context;
753 _gtk_settings_reset_rc_values (context->settings);
754 gtk_rc_clear_styles (context);
755 gtk_rc_clear_rc_files (context);
757 if (context->default_style)
758 g_object_unref (context->default_style);
760 g_strfreev (context->pixmap_path);
762 g_free (context->theme_name);
763 g_free (context->key_theme_name);
764 g_free (context->font_name);
766 if (context->color_hash)
767 g_hash_table_unref (context->color_hash);
769 g_signal_handlers_disconnect_by_func (settings,
770 gtk_rc_settings_changed, context);
771 g_signal_handlers_disconnect_by_func (settings,
772 gtk_rc_font_name_changed, context);
773 g_signal_handlers_disconnect_by_func (settings,
774 gtk_rc_color_hash_changed, context);
776 rc_contexts = g_slist_remove (rc_contexts, context);
780 settings->rc_context = NULL;
784 gtk_rc_parse_named (GtkRcContext *context,
789 const gchar *home_dir;
793 subpath = g_strconcat ("gtk-2.0-", type,
794 G_DIR_SEPARATOR_S "gtkrc",
797 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
799 /* First look in the users home directory
801 home_dir = g_get_home_dir ();
804 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
805 if (!g_file_test (path, G_FILE_TEST_EXISTS))
814 gchar *theme_dir = gtk_rc_get_theme_dir ();
815 path = g_build_filename (theme_dir, name, subpath, NULL);
818 if (!g_file_test (path, G_FILE_TEST_EXISTS))
827 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
835 gtk_rc_parse_default_files (GtkRcContext *context)
839 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
840 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
846 static gboolean initialized = FALSE;
852 gtk_rc_add_initial_default_files ();
855 /* Default RC string */
856 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
857 " bg[NORMAL] = \"#eee1b3\"\n"
858 " fg[NORMAL] = \"#000000\"\n"
861 "style \"gtk-default-progress-bar-style\" {\n"
862 " bg[PRELIGHT] = \"#4b6983\"\n"
863 " fg[PRELIGHT] = \"#ffffff\"\n"
864 " bg[NORMAL] = \"#c4c2bd\"\n"
867 "style \"gtk-default-menu-bar-item-style\" {\n"
868 " GtkMenuItem::horizontal_padding = 5\n"
871 "style \"gtk-default-menu-item-style\" {\n"
872 " bg[PRELIGHT] = \"#4b6983\"\n"
873 " fg[PRELIGHT] = \"#ffffff\"\n"
874 " base[PRELIGHT] = \"#4b6983\"\n"
875 " text[PRELIGHT] = \"#ffffff\"\n"
878 /* Make transparent tray icons work */
879 "style \"gtk-default-tray-icon-style\" {\n"
880 " bg_pixmap[NORMAL] = \"<parent>\"\n"
883 /* Work around clipping of accelerator underlines */
884 "style \"gtk-default-label-style\" {\n"
885 " GtkWidget::draw-border = {0,0,0,1}\n"
888 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
889 "class \"GtkTrayIcon\" style : gtk \"gtk-default-tray-icon-style\"\n"
890 "widget \"gtk-tooltip*\" style : gtk \"gtk-default-tooltips-style\"\n"
891 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
892 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
893 "class \"GtkLabel\" style : gtk \"gtk-default-label-style\"\n"
898 gtk_rc_context_parse_string (GtkRcContext *context,
899 const gchar *rc_string)
901 gtk_rc_parse_any (context, "-", -1, rc_string);
905 gtk_rc_parse_string (const gchar *rc_string)
910 g_return_if_fail (rc_string != NULL);
912 rc_file = g_new (GtkRcFile, 1);
913 rc_file->is_string = TRUE;
914 rc_file->name = g_strdup (rc_string);
915 rc_file->canonical_name = NULL;
916 rc_file->directory = NULL;
918 rc_file->reload = TRUE;
920 global_rc_files = g_slist_append (global_rc_files, rc_file);
922 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
923 gtk_rc_context_parse_string (tmp_list->data, rc_string);
927 add_to_rc_file_list (GSList **rc_file_list,
928 const char *filename,
934 tmp_list = *rc_file_list;
937 rc_file = tmp_list->data;
938 if (!strcmp (rc_file->name, filename))
941 tmp_list = tmp_list->next;
944 rc_file = g_new (GtkRcFile, 1);
945 rc_file->is_string = FALSE;
946 rc_file->name = g_strdup (filename);
947 rc_file->canonical_name = NULL;
948 rc_file->directory = NULL;
950 rc_file->reload = reload;
952 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
958 gtk_rc_context_parse_one_file (GtkRcContext *context,
959 const gchar *filename,
967 g_return_if_fail (filename != NULL);
969 saved_priority = context->default_priority;
970 context->default_priority = priority;
972 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
974 if (!rc_file->canonical_name)
976 /* Get the absolute pathname */
978 if (g_path_is_absolute (rc_file->name))
979 rc_file->canonical_name = rc_file->name;
984 cwd = g_get_current_dir ();
985 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
989 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
992 /* If the file is already being parsed (recursion), do nothing
994 if (g_slist_find (current_files_stack, rc_file))
997 if (!g_lstat (rc_file->canonical_name, &statbuf))
1001 rc_file->mtime = statbuf.st_mtime;
1003 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1007 /* Temporarily push information for this file on
1008 * a stack of current files while parsing it.
1010 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1011 gtk_rc_parse_any (context, filename, fd, NULL);
1012 current_files_stack = g_slist_delete_link (current_files_stack,
1013 current_files_stack);
1019 context->default_priority = saved_priority;
1023 strchr_len (const gchar *str, gint len, char c)
1028 return (gchar *)str;
1037 gtk_rc_context_parse_file (GtkRcContext *context,
1038 const gchar *filename,
1042 gchar *locale_suffixes[2];
1043 gint n_locale_suffixes = 0;
1047 gboolean found = FALSE;
1049 locale = _gtk_get_lc_ctype ();
1051 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1053 /* Determine locale-specific suffixes for RC files.
1055 length = strlen (locale);
1057 p = strchr (locale, '@');
1059 length = p - locale;
1061 p = strchr_len (locale, length, '.');
1063 length = p - locale;
1065 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1067 p = strchr_len (locale, length, '_');
1070 length = p - locale;
1071 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1077 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1078 for (j = 0; j < n_locale_suffixes; j++)
1082 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1083 if (g_file_test (name, G_FILE_TEST_EXISTS))
1085 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1092 g_free (locale_suffixes[j]);
1097 gtk_rc_parse (const gchar *filename)
1101 g_return_if_fail (filename != NULL);
1103 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1105 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1106 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1109 /* Handling of RC styles */
1111 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1114 gtk_rc_style_init (GtkRcStyle *style)
1116 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1120 style->font_desc = NULL;
1122 for (i = 0; i < 5; i++)
1124 static const GdkColor init_color = { 0, 0, 0, 0, };
1126 style->bg_pixmap_name[i] = NULL;
1127 style->color_flags[i] = 0;
1128 style->fg[i] = init_color;
1129 style->bg[i] = init_color;
1130 style->text[i] = init_color;
1131 style->base[i] = init_color;
1133 style->xthickness = -1;
1134 style->ythickness = -1;
1135 style->rc_properties = NULL;
1137 style->rc_style_lists = NULL;
1138 style->icon_factories = NULL;
1140 priv->color_hashes = NULL;
1144 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1146 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1148 object_class->finalize = gtk_rc_style_finalize;
1150 klass->parse = NULL;
1151 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1152 klass->merge = gtk_rc_style_real_merge;
1153 klass->create_style = gtk_rc_style_real_create_style;
1155 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1159 gtk_rc_style_finalize (GObject *object)
1161 GSList *tmp_list1, *tmp_list2;
1162 GtkRcStyle *rc_style;
1163 GtkRcStylePrivate *rc_priv;
1166 rc_style = GTK_RC_STYLE (object);
1167 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1169 g_free (rc_style->name);
1170 if (rc_style->font_desc)
1171 pango_font_description_free (rc_style->font_desc);
1173 for (i = 0; i < 5; i++)
1174 g_free (rc_style->bg_pixmap_name[i]);
1176 /* Now remove all references to this rc_style from
1179 tmp_list1 = rc_style->rc_style_lists;
1182 GSList *rc_styles = tmp_list1->data;
1183 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1184 g_object_unref (style);
1186 /* Remove the list of styles from the other rc_styles
1189 tmp_list2 = rc_styles;
1192 GtkRcStyle *other_style = tmp_list2->data;
1194 if (other_style != rc_style)
1195 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1197 tmp_list2 = tmp_list2->next;
1200 /* And from the hash table itself
1202 g_hash_table_remove (realized_style_ht, rc_styles);
1203 g_slist_free (rc_styles);
1205 tmp_list1 = tmp_list1->next;
1207 g_slist_free (rc_style->rc_style_lists);
1209 if (rc_style->rc_properties)
1213 for (i = 0; i < rc_style->rc_properties->len; i++)
1215 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1217 g_free (node->origin);
1218 g_value_unset (&node->value);
1220 g_array_free (rc_style->rc_properties, TRUE);
1221 rc_style->rc_properties = NULL;
1224 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1225 g_slist_free (rc_style->icon_factories);
1227 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1228 g_slist_free (rc_priv->color_hashes);
1230 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1234 gtk_rc_style_new (void)
1238 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1244 * gtk_rc_style_copy:
1245 * @orig: the style to copy
1247 * Makes a copy of the specified #GtkRcStyle. This function
1248 * will correctly copy an RC style that is a member of a class
1249 * derived from #GtkRcStyle.
1251 * Return value: the resulting #GtkRcStyle
1254 gtk_rc_style_copy (GtkRcStyle *orig)
1258 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1260 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1261 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1263 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1269 gtk_rc_style_ref (GtkRcStyle *rc_style)
1271 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1273 g_object_ref (rc_style);
1277 gtk_rc_style_unref (GtkRcStyle *rc_style)
1279 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1281 g_object_unref (rc_style);
1285 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1287 return g_object_new (G_OBJECT_TYPE (style), NULL);
1291 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1293 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1295 return priv->color_hashes;
1299 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1300 gconstpointer bsearch_node2)
1302 const GtkRcProperty *prop1 = bsearch_node1;
1303 const GtkRcProperty *prop2 = bsearch_node2;
1305 if (prop1->type_name == prop2->type_name)
1306 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1308 return prop1->type_name < prop2->type_name ? -1 : 1;
1312 insert_rc_property (GtkRcStyle *style,
1313 GtkRcProperty *property,
1317 GtkRcProperty *new_property = NULL;
1318 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1320 key.type_name = property->type_name;
1321 key.property_name = property->property_name;
1323 if (!style->rc_properties)
1324 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1327 while (i < style->rc_properties->len)
1329 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1335 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1337 g_free (new_property->origin);
1338 g_value_unset (&new_property->value);
1340 *new_property = key;
1354 g_array_insert_val (style->rc_properties, i, key);
1355 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1358 new_property->origin = g_strdup (property->origin);
1359 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1360 g_value_copy (&property->value, &new_property->value);
1364 gtk_rc_style_real_merge (GtkRcStyle *dest,
1369 for (i = 0; i < 5; i++)
1371 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1372 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1374 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1375 src->color_flags[i] & GTK_RC_FG)
1377 dest->fg[i] = src->fg[i];
1378 dest->color_flags[i] |= GTK_RC_FG;
1380 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1381 src->color_flags[i] & GTK_RC_BG)
1383 dest->bg[i] = src->bg[i];
1384 dest->color_flags[i] |= GTK_RC_BG;
1386 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1387 src->color_flags[i] & GTK_RC_TEXT)
1389 dest->text[i] = src->text[i];
1390 dest->color_flags[i] |= GTK_RC_TEXT;
1392 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1393 src->color_flags[i] & GTK_RC_BASE)
1395 dest->base[i] = src->base[i];
1396 dest->color_flags[i] |= GTK_RC_BASE;
1400 if (dest->xthickness < 0 && src->xthickness >= 0)
1401 dest->xthickness = src->xthickness;
1402 if (dest->ythickness < 0 && src->ythickness >= 0)
1403 dest->ythickness = src->ythickness;
1407 if (!dest->font_desc)
1408 dest->font_desc = pango_font_description_copy (src->font_desc);
1410 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1413 if (src->rc_properties)
1417 for (i = 0; i < src->rc_properties->len; i++)
1418 insert_rc_property (dest,
1419 &g_array_index (src->rc_properties, GtkRcProperty, i),
1425 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1427 return gtk_style_new ();
1431 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1433 GtkIconFactory *factory = gtk_icon_factory_new ();
1435 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1439 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1441 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1442 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1444 (GDestroyNotify) gdk_color_free);
1446 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1450 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1451 GtkRcStyle *src_style)
1453 GSList *concat = g_slist_copy (src_style->icon_factories);
1455 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1457 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1461 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1462 GtkRcStyle *src_style)
1464 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1465 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1466 GSList *concat = g_slist_copy (src_priv->color_hashes);
1468 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1470 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1474 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1475 GtkRcStyle *src_style,
1476 GtkRcContext *context)
1478 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1482 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1484 /* Append src_style's factories, adding a ref to them */
1485 if (src_style->icon_factories != NULL)
1487 /* Add a factory for ourselves if we have none,
1488 * in case we end up defining more stock icons.
1489 * I see no real way around this; we need to maintain
1490 * the invariant that the first factory in the list
1491 * is always our_factory, the one belonging to us,
1492 * and if we put src_style factories in the list we can't
1493 * do that if the style is reopened.
1495 if (rc_style->icon_factories == NULL)
1496 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1498 gtk_rc_style_append_icon_factories (rc_style, src_style);
1501 /* Also append src_style's color hashes, adding a ref to them */
1502 if (src_priv->color_hashes != NULL)
1504 /* See comment above .. */
1505 if (priv->color_hashes == NULL)
1506 gtk_rc_style_prepend_empty_color_hash (rc_style);
1508 gtk_rc_style_append_color_hashes (rc_style, src_style);
1512 /* if we didn't get color hashes from the src_style, initialize
1513 * the list with the settings' color scheme (if it exists)
1515 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1517 gtk_rc_style_prepend_empty_color_hash (rc_style);
1519 priv->color_hashes =
1520 g_slist_append (priv->color_hashes,
1521 g_hash_table_ref (context->color_hash));
1526 gtk_rc_clear_hash_node (gpointer key,
1530 gtk_rc_style_unref (data);
1534 gtk_rc_free_rc_sets (GSList *slist)
1540 rc_set = slist->data;
1541 gtk_rc_set_free (rc_set);
1543 slist = slist->next;
1548 gtk_rc_clear_styles (GtkRcContext *context)
1550 /* Clear out all old rc_styles */
1552 if (context->rc_style_ht)
1554 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1555 g_hash_table_destroy (context->rc_style_ht);
1556 context->rc_style_ht = NULL;
1559 gtk_rc_free_rc_sets (context->rc_sets_widget);
1560 g_slist_free (context->rc_sets_widget);
1561 context->rc_sets_widget = NULL;
1563 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1564 g_slist_free (context->rc_sets_widget_class);
1565 context->rc_sets_widget_class = NULL;
1567 gtk_rc_free_rc_sets (context->rc_sets_class);
1568 g_slist_free (context->rc_sets_class);
1569 context->rc_sets_class = NULL;
1572 /* Reset all our widgets. Also, we have to invalidate cached icons in
1573 * icon sets so they get re-rendered.
1576 gtk_rc_reset_widgets (GtkSettings *settings)
1578 GList *list, *toplevels;
1580 _gtk_icon_set_invalidate_caches ();
1582 toplevels = gtk_window_list_toplevels ();
1583 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1585 for (list = toplevels; list; list = list->next)
1587 if (gtk_widget_get_screen (list->data) == settings->screen)
1589 gtk_widget_reset_rc_styles (list->data);
1592 g_object_unref (list->data);
1594 g_list_free (toplevels);
1598 gtk_rc_clear_realized_style (gpointer key,
1602 GSList *rc_styles = key;
1603 GtkStyle *style = value;
1604 GSList *tmp_list = rc_styles;
1606 g_object_unref (style);
1610 GtkRcStyle *rc_style = tmp_list->data;
1612 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1614 tmp_list = tmp_list->next;
1617 g_slist_free (rc_styles);
1621 * gtk_rc_reset_styles:
1622 * @settings: a #GtkSettings
1624 * This function recomputes the styles for all widgets that use a
1625 * particular #GtkSettings object. (There is one #GtkSettings object
1626 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1627 * when some global parameter has changed that affects the appearance
1628 * of all widgets, because when a widget gets a new style, it will
1629 * both redraw and recompute any cached information about its
1630 * appearance. As an example, it is used when the default font size
1631 * set by the operating system changes. Note that this function
1632 * doesn't affect widgets that have a style set explicitely on them
1633 * with gtk_widget_set_style().
1638 gtk_rc_reset_styles (GtkSettings *settings)
1640 GtkRcContext *context;
1641 gboolean reset = FALSE;
1643 g_return_if_fail (GTK_IS_SETTINGS (settings));
1645 context = gtk_rc_context_get (settings);
1647 if (context->default_style)
1649 g_object_unref (context->default_style);
1650 context->default_style = NULL;
1654 /* Clear out styles that have been looked up already
1656 if (realized_style_ht)
1658 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1659 g_hash_table_destroy (realized_style_ht);
1660 realized_style_ht = NULL;
1665 gtk_rc_reset_widgets (settings);
1669 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1671 GtkRcContext *context;
1672 gchar *new_font_name;
1674 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1676 context = gtk_rc_context_get (settings);
1678 g_object_get (context->settings,
1679 "gtk-font-name", &new_font_name,
1682 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1684 g_free (context->font_name);
1685 context->font_name = g_strdup (new_font_name);
1687 gtk_rc_reset_styles (settings);
1690 g_free (new_font_name);
1692 return context->font_name;
1696 * gtk_rc_reparse_all_for_settings:
1697 * @settings: a #GtkSettings
1698 * @force_load: load whether or not anything changed
1700 * If the modification time on any previously read file
1701 * for the given #GtkSettings has changed, discard all style information
1702 * and then reread all previously read RC files.
1704 * Return value: %TRUE if the files were reread.
1707 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1708 gboolean force_load)
1710 gboolean mtime_modified = FALSE;
1713 GtkRcContext *context;
1714 struct stat statbuf;
1716 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1718 context = gtk_rc_context_get (settings);
1720 if (context->reloading)
1725 /* Check through and see if any of the RC's have had their
1726 * mtime modified. If so, reparse everything.
1728 tmp_list = context->rc_files;
1731 rc_file = tmp_list->data;
1733 if (!rc_file->is_string)
1735 if (!g_lstat (rc_file->name, &statbuf) &&
1736 (statbuf.st_mtime != rc_file->mtime))
1738 mtime_modified = TRUE;
1743 tmp_list = tmp_list->next;
1747 if (force_load || mtime_modified)
1749 _gtk_binding_reset_parsed ();
1750 gtk_rc_clear_styles (context);
1751 context->reloading = TRUE;
1753 _gtk_settings_reset_rc_values (context->settings);
1754 gtk_rc_clear_rc_files (context);
1756 gtk_rc_parse_default_files (context);
1758 tmp_list = global_rc_files;
1761 rc_file = tmp_list->data;
1763 if (rc_file->is_string)
1764 gtk_rc_context_parse_string (context, rc_file->name);
1766 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1768 tmp_list = tmp_list->next;
1771 g_free (context->theme_name);
1772 g_free (context->key_theme_name);
1774 g_object_get (context->settings,
1775 "gtk-theme-name", &context->theme_name,
1776 "gtk-key-theme-name", &context->key_theme_name,
1779 if (context->theme_name && context->theme_name[0])
1780 gtk_rc_parse_named (context, context->theme_name, NULL);
1781 if (context->key_theme_name && context->key_theme_name[0])
1782 gtk_rc_parse_named (context, context->key_theme_name, "key");
1784 context->reloading = FALSE;
1786 gtk_rc_reset_widgets (context->settings);
1789 return force_load || mtime_modified;
1793 * gtk_rc_reparse_all:
1795 * If the modification time on any previously read file for the
1796 * default #GtkSettings has changed, discard all style information
1797 * and then reread all previously read RC files.
1799 * Return value: %TRUE if the files were reread.
1802 gtk_rc_reparse_all (void)
1805 gboolean result = FALSE;
1807 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1809 GtkRcContext *context = tmp_list->data;
1810 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1818 gtk_rc_styles_match (GSList *rc_styles,
1822 gchar *path_reversed)
1829 rc_set = sets->data;
1832 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1834 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1835 rc_styles = g_slist_append (rc_styles, rc_set);
1839 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1840 rc_styles = g_slist_append (rc_styles, rc_set);
1848 rc_set_compare (gconstpointer a, gconstpointer b)
1850 const GtkRcSet *set_a = a;
1851 const GtkRcSet *set_b = b;
1853 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1857 sort_and_dereference_sets (GSList *styles)
1861 /* At this point, the list of sets is ordered by:
1863 * a) 'widget' patterns are earlier than 'widget_class' patterns
1864 * which are ealier than 'class' patterns.
1865 * a) For two matches for class patterns, a match to a child type
1866 * is before a match to a parent type
1867 * c) a match later in the RC file (or in a later RC file) is before a
1868 * match earlier in the RC file.
1870 * With a) taking precedence over b) which takes precendence over c).
1872 * Now sort by priority, which has the highest precendence for sort order
1874 styles = g_slist_sort (styles, rc_set_compare);
1876 /* Make styles->data = styles->data->rc_style
1881 GtkRcSet *set = tmp_list->data;
1882 tmp_list->data = set->rc_style;
1883 tmp_list = tmp_list->next;
1891 * @widget: a #GtkWidget
1893 * Finds all matching RC styles for a given widget,
1894 * composites them together, and then creates a
1895 * #GtkStyle representing the composite appearance.
1896 * (GTK+ actually keeps a cache of previously
1897 * created styles, so a new style may not be
1900 * Returns: the resulting style. No refcount is added
1901 * to the returned style, so if you want to save this
1902 * style around, you should add a reference yourself.
1905 gtk_rc_get_style (GtkWidget *widget)
1907 GtkRcStyle *widget_rc_style;
1908 GSList *rc_styles = NULL;
1909 GtkRcContext *context;
1911 static guint rc_style_key_id = 0;
1913 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1915 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1917 /* We allow the specification of a single rc style to be bound
1918 * tightly to a widget, for application modifications
1920 if (!rc_style_key_id)
1921 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1923 if (context->rc_sets_widget)
1925 gchar *path, *path_reversed;
1928 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1929 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1931 g_free (path_reversed);
1934 if (context->rc_sets_widget_class)
1936 gchar *path, *path_reversed;
1939 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1940 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1942 g_free (path_reversed);
1945 if (context->rc_sets_class)
1949 type = G_TYPE_FROM_INSTANCE (widget);
1953 gchar *path_reversed;
1956 path = g_strdup (g_type_name (type));
1957 path_length = strlen (path);
1958 path_reversed = g_strdup (path);
1959 g_strreverse (path_reversed);
1961 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1963 g_free (path_reversed);
1965 type = g_type_parent (type);
1969 rc_styles = sort_and_dereference_sets (rc_styles);
1971 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
1973 if (widget_rc_style)
1974 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1977 return gtk_rc_init_style (context, rc_styles);
1980 if (!context->default_style)
1982 context->default_style = gtk_style_new ();
1983 _gtk_style_init_for_settings (context->default_style, context->settings);
1986 return context->default_style;
1991 * gtk_rc_get_style_by_paths:
1992 * @settings: a #GtkSettings object
1993 * @widget_path: the widget path to use when looking up the style, or %NULL
1994 * if no matching against the widget path should be done
1995 * @class_path: the class path to use when looking up the style, or %NULL
1996 * if no matching against the class path should be done.
1997 * @type: a type that will be used along with parent types of this type
1998 * when matching against class styles, or #G_TYPE_NONE
2000 * Creates up a #GtkStyle from styles defined in a RC file by providing
2001 * the raw components used in matching. This function may be useful
2002 * when creating pseudo-widgets that should be themed like widgets but
2003 * don't actually have corresponding GTK+ widgets. An example of this
2004 * would be items inside a GNOME canvas widget.
2006 * The action of gtk_rc_get_style() is similar to:
2007 * <informalexample><programlisting>
2008 * gtk_widget_path (widget, NULL, &path, NULL);
2009 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2010 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
2011 * G_OBJECT_TYPE (widget));
2012 * </programlisting></informalexample>
2014 * Return value: A style created by matching with the supplied paths,
2015 * or %NULL if nothing matching was specified and the default style should
2016 * be used. The returned value is owned by GTK+ as part of an internal cache,
2017 * so you must call g_object_ref() on the returned value if you want to
2018 * keep a reference to it.
2021 gtk_rc_get_style_by_paths (GtkSettings *settings,
2022 const char *widget_path,
2023 const char *class_path,
2026 /* We duplicate the code from above to avoid slowing down the above
2027 * by generating paths when we don't need them. I don't know if
2028 * this is really worth it.
2030 GSList *rc_styles = NULL;
2031 GtkRcContext *context;
2033 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2035 context = gtk_rc_context_get (settings);
2037 if (widget_path && context->rc_sets_widget)
2040 gchar *path_reversed;
2043 path_length = strlen (widget_path);
2044 path = g_strdup (widget_path);
2045 path_reversed = g_strdup (widget_path);
2046 g_strreverse (path_reversed);
2048 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2050 g_free (path_reversed);
2053 if (class_path && context->rc_sets_widget_class)
2056 gchar *path_reversed;
2059 path = g_strdup (class_path);
2060 path_length = strlen (class_path);
2061 path_reversed = g_strdup (class_path);
2062 g_strreverse (path_reversed);
2064 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2066 g_free (path_reversed);
2069 if (type != G_TYPE_NONE && context->rc_sets_class)
2074 gchar *path_reversed;
2077 path = g_strdup (g_type_name (type));
2078 path_length = strlen (path);
2079 path_reversed = g_strdup (path);
2080 g_strreverse (path_reversed);
2082 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2084 g_free (path_reversed);
2086 type = g_type_parent (type);
2090 rc_styles = sort_and_dereference_sets (rc_styles);
2093 return gtk_rc_init_style (context, rc_styles);
2099 gtk_rc_add_rc_sets (GSList *slist,
2100 GtkRcStyle *rc_style,
2101 const gchar *pattern,
2102 GtkPathType path_type)
2104 GtkRcStyle *new_style;
2108 new_style = gtk_rc_style_new ();
2109 *new_style = *rc_style;
2110 new_style->name = g_strdup (rc_style->name);
2111 if (rc_style->font_desc)
2112 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
2114 for (i = 0; i < 5; i++)
2115 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
2117 rc_set = g_new (GtkRcSet, 1);
2118 rc_set->type = path_type;
2120 if (path_type == GTK_PATH_WIDGET_CLASS)
2122 rc_set->pspec = NULL;
2123 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
2127 rc_set->pspec = g_pattern_spec_new (pattern);
2128 rc_set->path = NULL;
2131 rc_set->rc_style = rc_style;
2133 return g_slist_prepend (slist, rc_set);
2137 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
2138 const gchar *pattern)
2140 GtkRcContext *context;
2142 g_return_if_fail (rc_style != NULL);
2143 g_return_if_fail (pattern != NULL);
2145 context = gtk_rc_context_get (gtk_settings_get_default ());
2147 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern, GTK_PATH_WIDGET);
2151 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
2152 const gchar *pattern)
2154 GtkRcContext *context;
2156 g_return_if_fail (rc_style != NULL);
2157 g_return_if_fail (pattern != NULL);
2159 context = gtk_rc_context_get (gtk_settings_get_default ());
2161 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern, GTK_PATH_WIDGET_CLASS);
2165 gtk_rc_add_class_style (GtkRcStyle *rc_style,
2166 const gchar *pattern)
2168 GtkRcContext *context;
2170 g_return_if_fail (rc_style != NULL);
2171 g_return_if_fail (pattern != NULL);
2173 context = gtk_rc_context_get (gtk_settings_get_default ());
2175 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern, GTK_PATH_CLASS);
2179 gtk_rc_scanner_new (void)
2181 return g_scanner_new (>k_rc_scanner_config);
2185 gtk_rc_parse_any (GtkRcContext *context,
2186 const gchar *input_name,
2188 const gchar *input_string)
2194 scanner = gtk_rc_scanner_new ();
2198 g_assert (input_string == NULL);
2200 g_scanner_input_file (scanner, input_fd);
2204 g_assert (input_string != NULL);
2206 g_scanner_input_text (scanner, input_string, strlen (input_string));
2208 scanner->input_name = input_name;
2210 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2211 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2215 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2219 guint expected_token;
2221 expected_token = gtk_rc_parse_statement (context, scanner);
2223 if (expected_token != G_TOKEN_NONE)
2230 if (scanner->scope_id == 0)
2232 /* if we are in scope 0, we know the symbol names
2233 * that are associated with certain token values.
2234 * so we look them up to make the error messages
2237 if (expected_token > GTK_RC_TOKEN_INVALID &&
2238 expected_token < GTK_RC_TOKEN_LAST)
2240 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2241 if (symbols[i].token == expected_token)
2242 msg = symbol_names + symbols[i].name_offset;
2244 msg = g_strconcat ("e.g. `", msg, "'", NULL);
2246 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2247 scanner->token < GTK_RC_TOKEN_LAST)
2249 symbol_name = "???";
2250 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2251 if (symbols[i].token == scanner->token)
2252 symbol_name = symbol_names + symbols[i].name_offset;
2255 g_scanner_unexp_token (scanner,
2268 g_scanner_destroy (scanner);
2272 gtk_rc_styles_hash (const GSList *rc_styles)
2279 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2280 rc_styles = rc_styles->next;
2287 gtk_rc_styles_equal (const GSList *a,
2292 if (a->data != b->data)
2302 gtk_rc_style_hash (const gchar *name)
2308 result += (result << 3) + *name++;
2314 gtk_rc_style_equal (const gchar *a,
2317 return (strcmp (a, b) == 0);
2321 gtk_rc_style_find (GtkRcContext *context,
2324 if (context->rc_style_ht)
2325 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2331 gtk_rc_style_to_style (GtkRcContext *context,
2332 GtkRcStyle *rc_style)
2336 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2337 _gtk_style_init_for_settings (style, context->settings);
2339 style->rc_style = g_object_ref (rc_style);
2341 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2346 /* Reuses or frees rc_styles */
2348 gtk_rc_init_style (GtkRcContext *context,
2351 GtkStyle *style = NULL;
2354 g_return_val_if_fail (rc_styles != NULL, NULL);
2356 if (!realized_style_ht)
2357 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2358 (GEqualFunc) gtk_rc_styles_equal);
2360 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2364 GtkRcStyle *base_style = NULL;
2365 GtkRcStyle *proto_style;
2366 GtkRcStyleClass *proto_style_class;
2368 GType rc_style_type = GTK_TYPE_RC_STYLE;
2370 /* Find the first style where the RC file specified engine "" {}
2371 * or the first derived style and use that to create the
2372 * merged style. If we only have raw GtkRcStyles, use the first
2373 * style to create the merged style.
2375 base_style = rc_styles->data;
2376 tmp_styles = rc_styles;
2379 GtkRcStyle *rc_style = tmp_styles->data;
2381 if (rc_style->engine_specified ||
2382 G_OBJECT_TYPE (rc_style) != rc_style_type)
2384 base_style = rc_style;
2388 tmp_styles = tmp_styles->next;
2391 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2392 proto_style = proto_style_class->create_rc_style (base_style);
2394 tmp_styles = rc_styles;
2397 GtkRcStyle *rc_style = tmp_styles->data;
2399 proto_style_class->merge (proto_style, rc_style);
2401 /* Point from each rc_style to the list of styles */
2402 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2403 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2405 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2406 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2408 tmp_styles = tmp_styles->next;
2411 for (i = 0; i < 5; i++)
2412 if (proto_style->bg_pixmap_name[i] &&
2413 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2415 g_free (proto_style->bg_pixmap_name[i]);
2416 proto_style->bg_pixmap_name[i] = NULL;
2419 style = gtk_rc_style_to_style (context, proto_style);
2420 gtk_rc_style_unref (proto_style);
2422 g_hash_table_insert (realized_style_ht, rc_styles, style);
2425 g_slist_free (rc_styles);
2430 /*********************
2431 * Parsing functions *
2432 *********************/
2435 lookup_color (GtkRcStyle *style,
2436 const char *color_name,
2439 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2442 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2444 GHashTable *hash = iter->data;
2445 GdkColor *match = g_hash_table_lookup (hash, color_name);
2449 color->red = match->red;
2450 color->green = match->green;
2451 color->blue = match->blue;
2460 rc_parse_token_or_compound (GScanner *scanner,
2463 GTokenType delimiter)
2465 guint token = g_scanner_get_next_token (scanner);
2467 /* we either scan a single token (skipping comments)
2468 * or a compund statement.
2469 * compunds are enclosed in (), [] or {} braces, we read
2470 * them in via deep recursion.
2477 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2480 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2482 case G_TOKEN_STRING:
2483 string = g_strescape (scanner->value.v_string, NULL);
2484 g_string_append (gstring, " \"");
2485 g_string_append (gstring, string);
2486 g_string_append_c (gstring, '"');
2489 case G_TOKEN_IDENTIFIER:
2490 g_string_append_c (gstring, ' ');
2491 g_string_append (gstring, scanner->value.v_identifier);
2493 case G_TOKEN_COMMENT_SINGLE:
2494 case G_TOKEN_COMMENT_MULTI:
2495 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2496 case G_TOKEN_LEFT_PAREN:
2497 g_string_append_c (gstring, ' ');
2498 g_string_append_c (gstring, token);
2499 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2500 if (token != G_TOKEN_NONE)
2503 case G_TOKEN_LEFT_CURLY:
2504 g_string_append_c (gstring, ' ');
2505 g_string_append_c (gstring, token);
2506 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2507 if (token != G_TOKEN_NONE)
2510 case G_TOKEN_LEFT_BRACE:
2511 g_string_append_c (gstring, ' ');
2512 g_string_append_c (gstring, token);
2513 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2514 if (token != G_TOKEN_NONE)
2518 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2521 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2522 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2523 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2525 g_scanner_get_next_token (scanner);
2527 if (!style || !lookup_color (style, scanner->value.v_identifier,
2530 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2531 scanner->value.v_identifier);
2532 return G_TOKEN_IDENTIFIER;
2536 g_string_append_printf (gstring, " { %s, %s, %s }",
2537 g_ascii_formatd (rbuf, sizeof (rbuf),
2539 color.red / 65535.0),
2540 g_ascii_formatd (gbuf, sizeof (gbuf),
2542 color.green / 65535.0),
2543 g_ascii_formatd (bbuf, sizeof (bbuf),
2545 color.blue / 65535.0));
2549 return G_TOKEN_IDENTIFIER;
2551 if (token >= 256 || token < 1)
2552 return delimiter ? delimiter : G_TOKEN_STRING;
2553 g_string_append_c (gstring, ' ');
2554 g_string_append_c (gstring, token);
2555 if (token == delimiter)
2556 return G_TOKEN_NONE;
2560 return G_TOKEN_NONE;
2562 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2566 gtk_rc_parse_assignment (GScanner *scanner,
2568 GtkRcProperty *prop)
2570 #define MY_SCAN_IDENTIFIER TRUE
2571 #define MY_SCAN_SYMBOLS FALSE
2572 #define MY_IDENTIFIER_2_STRING FALSE
2573 #define MY_CHAR_2_TOKEN TRUE
2574 #define MY_SCAN_IDENTIFIER_NULL FALSE
2575 #define MY_NUMBERS_2_INT TRUE
2577 gboolean scan_identifier = scanner->config->scan_identifier;
2578 gboolean scan_symbols = scanner->config->scan_symbols;
2579 gboolean identifier_2_string = scanner->config->identifier_2_string;
2580 gboolean char_2_token = scanner->config->char_2_token;
2581 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2582 gboolean numbers_2_int = scanner->config->numbers_2_int;
2583 gboolean negate = FALSE;
2584 gboolean is_color = FALSE;
2587 /* check that this is an assignment */
2588 if (g_scanner_get_next_token (scanner) != '=')
2591 /* adjust scanner mode */
2592 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2593 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2594 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2595 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2596 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2597 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2599 /* record location */
2600 if (g_getenv ("GTK_DEBUG"))
2601 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2603 prop->origin = NULL;
2605 /* parse optional symbolic color prefix */
2606 if (g_scanner_peek_next_token (scanner) == '@')
2608 g_scanner_get_next_token (scanner); /* eat color prefix */
2612 /* parse optional sign */
2613 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2615 g_scanner_get_next_token (scanner); /* eat sign */
2619 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2622 token = g_scanner_peek_next_token (scanner);
2624 if (is_color && token != G_TOKEN_IDENTIFIER)
2626 token = G_TOKEN_IDENTIFIER;
2633 g_scanner_get_next_token (scanner);
2634 g_value_init (&prop->value, G_TYPE_LONG);
2635 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2636 token = G_TOKEN_NONE;
2639 g_scanner_get_next_token (scanner);
2640 g_value_init (&prop->value, G_TYPE_DOUBLE);
2641 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2642 token = G_TOKEN_NONE;
2644 case G_TOKEN_STRING:
2645 g_scanner_get_next_token (scanner);
2647 token = G_TOKEN_INT;
2650 g_value_init (&prop->value, G_TYPE_STRING);
2651 g_value_set_string (&prop->value, scanner->value.v_string);
2652 token = G_TOKEN_NONE;
2655 case G_TOKEN_IDENTIFIER:
2659 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2660 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2661 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2664 g_scanner_get_next_token (scanner);
2666 if (!style || !lookup_color (style, scanner->value.v_identifier,
2669 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2670 scanner->value.v_identifier);
2671 token = G_TOKEN_IDENTIFIER;
2675 gstring = g_string_new (NULL);
2677 g_string_append_printf (gstring, " { %s, %s, %s }",
2678 g_ascii_formatd (rbuf, sizeof (rbuf),
2680 color.red / 65535.0),
2681 g_ascii_formatd (gbuf, sizeof (gbuf),
2683 color.green / 65535.0),
2684 g_ascii_formatd (bbuf, sizeof (bbuf),
2686 color.blue / 65535.0));
2688 g_value_init (&prop->value, G_TYPE_GSTRING);
2689 g_value_take_boxed (&prop->value, gstring);
2690 token = G_TOKEN_NONE;
2694 case G_TOKEN_LEFT_PAREN:
2695 case G_TOKEN_LEFT_CURLY:
2696 case G_TOKEN_LEFT_BRACE:
2699 GString *gstring = g_string_new (NULL);
2700 gboolean parse_on = TRUE;
2702 /* allow identifier(foobar) to support color expressions */
2703 if (token == G_TOKEN_IDENTIFIER)
2705 g_scanner_get_next_token (scanner);
2707 g_string_append_c (gstring, ' ');
2708 g_string_append (gstring, scanner->value.v_identifier);
2710 /* temporarily reset scanner mode to default, so we
2711 * don't peek the next token in a mode that only makes
2712 * sense in this function; because if anything but
2713 * G_TOKEN_LEFT_PAREN follows, the next token will be
2714 * parsed by our caller.
2716 * FIXME: right fix would be to call g_scanner_unget()
2717 * but that doesn't exist
2719 scanner->config->scan_identifier = scan_identifier;
2720 scanner->config->scan_symbols = scan_symbols;
2721 scanner->config->identifier_2_string = identifier_2_string;
2722 scanner->config->char_2_token = char_2_token;
2723 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2724 scanner->config->numbers_2_int = numbers_2_int;
2726 token = g_scanner_peek_next_token (scanner);
2728 /* restore adjusted scanner mode */
2729 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2730 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2731 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2732 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2733 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2734 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2736 if (token != G_TOKEN_LEFT_PAREN)
2738 token = G_TOKEN_NONE;
2744 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2746 if (token == G_TOKEN_NONE)
2748 g_string_append_c (gstring, ' ');
2749 g_value_init (&prop->value, G_TYPE_GSTRING);
2750 g_value_take_boxed (&prop->value, gstring);
2753 g_string_free (gstring, TRUE);
2758 g_scanner_get_next_token (scanner);
2759 token = G_TOKEN_INT;
2765 /* restore scanner mode */
2766 scanner->config->scan_identifier = scan_identifier;
2767 scanner->config->scan_symbols = scan_symbols;
2768 scanner->config->identifier_2_string = identifier_2_string;
2769 scanner->config->char_2_token = char_2_token;
2770 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2771 scanner->config->numbers_2_int = numbers_2_int;
2777 is_c_identifier (const gchar *string)
2780 gboolean is_varname;
2782 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2783 for (p = string + 1; *p && is_varname; p++)
2784 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2790 parse_include_file (GtkRcContext *context,
2792 const gchar *filename)
2794 char *to_parse = NULL;
2796 if (g_path_is_absolute (filename))
2798 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2799 * don't print an error in this case.
2801 to_parse = g_strdup (filename);
2805 /* if a relative path, we look relative to all the RC files in the
2806 * include stack. We require the file to be found in this case
2807 * so we can give meaningful error messages, and because on reparsing
2808 * non-absolute paths don't make sense.
2810 GSList *tmp_list = current_files_stack;
2813 GtkRcFile *curfile = tmp_list->data;
2814 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2816 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2824 tmp_list = tmp_list->next;
2830 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2835 g_scanner_warn (scanner,
2836 _("Unable to find include file: \"%s\""),
2843 gtk_rc_parse_statement (GtkRcContext *context,
2848 token = g_scanner_peek_next_token (scanner);
2851 case GTK_RC_TOKEN_INCLUDE:
2852 token = g_scanner_get_next_token (scanner);
2853 if (token != GTK_RC_TOKEN_INCLUDE)
2854 return GTK_RC_TOKEN_INCLUDE;
2855 token = g_scanner_get_next_token (scanner);
2856 if (token != G_TOKEN_STRING)
2857 return G_TOKEN_STRING;
2858 parse_include_file (context, scanner, scanner->value.v_string);
2859 return G_TOKEN_NONE;
2861 case GTK_RC_TOKEN_STYLE:
2862 return gtk_rc_parse_style (context, scanner);
2864 case GTK_RC_TOKEN_BINDING:
2865 return _gtk_binding_parse_binding (scanner);
2867 case GTK_RC_TOKEN_PIXMAP_PATH:
2868 return gtk_rc_parse_pixmap_path (context, scanner);
2870 case GTK_RC_TOKEN_WIDGET:
2871 return gtk_rc_parse_path_pattern (context, scanner);
2873 case GTK_RC_TOKEN_WIDGET_CLASS:
2874 return gtk_rc_parse_path_pattern (context, scanner);
2876 case GTK_RC_TOKEN_CLASS:
2877 return gtk_rc_parse_path_pattern (context, scanner);
2879 case GTK_RC_TOKEN_MODULE_PATH:
2880 return gtk_rc_parse_module_path (scanner);
2882 case GTK_RC_TOKEN_IM_MODULE_FILE:
2883 return gtk_rc_parse_im_module_file (scanner);
2885 case G_TOKEN_IDENTIFIER:
2886 if (is_c_identifier (scanner->next_value.v_identifier))
2888 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2891 g_scanner_get_next_token (scanner); /* eat identifier */
2892 name = g_strdup (scanner->value.v_identifier);
2894 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2895 if (token == G_TOKEN_NONE)
2897 GtkSettingsValue svalue;
2899 svalue.origin = prop.origin;
2900 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2901 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2902 _gtk_settings_set_property_value_from_rc (context->settings,
2906 g_free (prop.origin);
2907 if (G_VALUE_TYPE (&prop.value))
2908 g_value_unset (&prop.value);
2915 g_scanner_get_next_token (scanner);
2916 return G_TOKEN_IDENTIFIER;
2919 g_scanner_get_next_token (scanner);
2920 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2925 fixup_rc_set (GSList *list,
2931 GtkRcSet *set = list->data;
2932 if (set->rc_style == orig)
2933 set->rc_style = new;
2939 fixup_rc_sets (GtkRcContext *context,
2943 fixup_rc_set (context->rc_sets_widget, orig, new);
2944 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2945 fixup_rc_set (context->rc_sets_class, orig, new);
2949 gtk_rc_parse_style (GtkRcContext *context,
2952 GtkRcStyle *rc_style;
2953 GtkRcStyle *orig_style;
2954 GtkRcStyle *parent_style = NULL;
2955 GtkRcStylePrivate *rc_priv = NULL;
2958 GtkIconFactory *our_factory = NULL;
2959 GHashTable *our_hash = NULL;
2961 token = g_scanner_get_next_token (scanner);
2962 if (token != GTK_RC_TOKEN_STYLE)
2963 return GTK_RC_TOKEN_STYLE;
2965 token = g_scanner_get_next_token (scanner);
2966 if (token != G_TOKEN_STRING)
2967 return G_TOKEN_STRING;
2969 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2971 orig_style = g_object_ref (rc_style);
2977 rc_style = gtk_rc_style_new ();
2978 rc_style->name = g_strdup (scanner->value.v_string);
2980 for (i = 0; i < 5; i++)
2981 rc_style->bg_pixmap_name[i] = NULL;
2983 for (i = 0; i < 5; i++)
2984 rc_style->color_flags[i] = 0;
2987 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
2989 /* If there's a list, its first member is always the factory belonging
2992 if (rc_style->icon_factories)
2993 our_factory = rc_style->icon_factories->data;
2994 if (rc_priv->color_hashes)
2995 our_hash = rc_priv->color_hashes->data;
2997 token = g_scanner_peek_next_token (scanner);
2998 if (token == G_TOKEN_EQUAL_SIGN)
3000 token = g_scanner_get_next_token (scanner);
3002 token = g_scanner_get_next_token (scanner);
3003 if (token != G_TOKEN_STRING)
3005 token = G_TOKEN_STRING;
3009 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3012 for (i = 0; i < 5; i++)
3014 rc_style->color_flags[i] = parent_style->color_flags[i];
3015 rc_style->fg[i] = parent_style->fg[i];
3016 rc_style->bg[i] = parent_style->bg[i];
3017 rc_style->text[i] = parent_style->text[i];
3018 rc_style->base[i] = parent_style->base[i];
3021 rc_style->xthickness = parent_style->xthickness;
3022 rc_style->ythickness = parent_style->ythickness;
3024 if (parent_style->font_desc)
3026 if (rc_style->font_desc)
3027 pango_font_description_free (rc_style->font_desc);
3028 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3031 if (parent_style->rc_properties)
3035 for (i = 0; i < parent_style->rc_properties->len; i++)
3036 insert_rc_property (rc_style,
3037 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3041 for (i = 0; i < 5; i++)
3043 g_free (rc_style->bg_pixmap_name[i]);
3044 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3049 /* get icon_factories and color_hashes from the parent style;
3050 * if the parent_style doesn't have color_hashes, initializes
3051 * the color_hashes with the settings' color scheme (if it exists)
3053 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3055 if (rc_style->icon_factories)
3056 our_factory = rc_style->icon_factories->data;
3057 if (rc_priv->color_hashes)
3058 our_hash = rc_priv->color_hashes->data;
3060 token = g_scanner_get_next_token (scanner);
3061 if (token != G_TOKEN_LEFT_CURLY)
3063 token = G_TOKEN_LEFT_CURLY;
3067 token = g_scanner_peek_next_token (scanner);
3068 while (token != G_TOKEN_RIGHT_CURLY)
3072 case GTK_RC_TOKEN_BG:
3073 token = gtk_rc_parse_bg (scanner, rc_style);
3075 case GTK_RC_TOKEN_FG:
3076 token = gtk_rc_parse_fg (scanner, rc_style);
3078 case GTK_RC_TOKEN_TEXT:
3079 token = gtk_rc_parse_text (scanner, rc_style);
3081 case GTK_RC_TOKEN_BASE:
3082 token = gtk_rc_parse_base (scanner, rc_style);
3084 case GTK_RC_TOKEN_XTHICKNESS:
3085 token = gtk_rc_parse_xthickness (scanner, rc_style);
3087 case GTK_RC_TOKEN_YTHICKNESS:
3088 token = gtk_rc_parse_ythickness (scanner, rc_style);
3090 case GTK_RC_TOKEN_BG_PIXMAP:
3091 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3093 case GTK_RC_TOKEN_FONT:
3094 token = gtk_rc_parse_font (scanner, rc_style);
3096 case GTK_RC_TOKEN_FONTSET:
3097 token = gtk_rc_parse_fontset (scanner, rc_style);
3099 case GTK_RC_TOKEN_FONT_NAME:
3100 token = gtk_rc_parse_font_name (scanner, rc_style);
3102 case GTK_RC_TOKEN_ENGINE:
3103 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3105 case GTK_RC_TOKEN_STOCK:
3106 if (our_factory == NULL)
3107 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3108 our_factory = rc_style->icon_factories->data;
3109 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3111 case GTK_RC_TOKEN_COLOR:
3112 if (our_hash == NULL)
3113 gtk_rc_style_prepend_empty_color_hash (rc_style);
3114 our_hash = rc_priv->color_hashes->data;
3115 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3117 case G_TOKEN_IDENTIFIER:
3118 if (is_c_identifier (scanner->next_value.v_identifier) &&
3119 scanner->next_value.v_identifier[0] >= 'A' &&
3120 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
3122 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3125 g_scanner_get_next_token (scanner); /* eat type name */
3126 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3127 if (g_scanner_get_next_token (scanner) != ':' ||
3128 g_scanner_get_next_token (scanner) != ':')
3133 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3134 !is_c_identifier (scanner->value.v_identifier))
3136 token = G_TOKEN_IDENTIFIER;
3140 /* it's important that we do the same canonification as GParamSpecPool here */
3141 name = g_strdup (scanner->value.v_identifier);
3142 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3143 prop.property_name = g_quark_from_string (name);
3146 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3147 if (token == G_TOKEN_NONE)
3149 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3150 insert_rc_property (rc_style, &prop, TRUE);
3153 g_free (prop.origin);
3154 if (G_VALUE_TYPE (&prop.value))
3155 g_value_unset (&prop.value);
3159 g_scanner_get_next_token (scanner);
3160 token = G_TOKEN_IDENTIFIER;
3164 g_scanner_get_next_token (scanner);
3165 token = G_TOKEN_RIGHT_CURLY;
3169 if (token != G_TOKEN_NONE)
3172 token = g_scanner_peek_next_token (scanner);
3173 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3175 token = g_scanner_get_next_token (scanner);
3176 if (token != G_TOKEN_RIGHT_CURLY)
3178 token = G_TOKEN_RIGHT_CURLY;
3182 if (rc_style != orig_style)
3184 if (!context->rc_style_ht)
3185 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3186 (GEqualFunc) gtk_rc_style_equal);
3188 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3190 /* If we copied the data into a new rc style, fix up references to the old rc style
3191 * in bindings that we have.
3194 fixup_rc_sets (context, orig_style, rc_style);
3198 g_object_unref (orig_style);
3200 return G_TOKEN_NONE;
3203 if (rc_style != orig_style)
3204 gtk_rc_style_unref (rc_style);
3207 g_object_unref (orig_style);
3212 const GtkRcProperty*
3213 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3215 GQuark property_name)
3217 GtkRcProperty *node = NULL;
3219 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3221 if (rc_style->rc_properties)
3225 key.type_name = type_name;
3226 key.property_name = property_name;
3228 node = bsearch (&key,
3229 rc_style->rc_properties->data, rc_style->rc_properties->len,
3230 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3237 gtk_rc_parse_bg (GScanner *scanner,
3243 token = g_scanner_get_next_token (scanner);
3244 if (token != GTK_RC_TOKEN_BG)
3245 return GTK_RC_TOKEN_BG;
3247 token = gtk_rc_parse_state (scanner, &state);
3248 if (token != G_TOKEN_NONE)
3251 token = g_scanner_get_next_token (scanner);
3252 if (token != G_TOKEN_EQUAL_SIGN)
3253 return G_TOKEN_EQUAL_SIGN;
3255 style->color_flags[state] |= GTK_RC_BG;
3256 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3260 gtk_rc_parse_fg (GScanner *scanner,
3266 token = g_scanner_get_next_token (scanner);
3267 if (token != GTK_RC_TOKEN_FG)
3268 return GTK_RC_TOKEN_FG;
3270 token = gtk_rc_parse_state (scanner, &state);
3271 if (token != G_TOKEN_NONE)
3274 token = g_scanner_get_next_token (scanner);
3275 if (token != G_TOKEN_EQUAL_SIGN)
3276 return G_TOKEN_EQUAL_SIGN;
3278 style->color_flags[state] |= GTK_RC_FG;
3279 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3283 gtk_rc_parse_text (GScanner *scanner,
3289 token = g_scanner_get_next_token (scanner);
3290 if (token != GTK_RC_TOKEN_TEXT)
3291 return GTK_RC_TOKEN_TEXT;
3293 token = gtk_rc_parse_state (scanner, &state);
3294 if (token != G_TOKEN_NONE)
3297 token = g_scanner_get_next_token (scanner);
3298 if (token != G_TOKEN_EQUAL_SIGN)
3299 return G_TOKEN_EQUAL_SIGN;
3301 style->color_flags[state] |= GTK_RC_TEXT;
3302 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3306 gtk_rc_parse_base (GScanner *scanner,
3312 token = g_scanner_get_next_token (scanner);
3313 if (token != GTK_RC_TOKEN_BASE)
3314 return GTK_RC_TOKEN_BASE;
3316 token = gtk_rc_parse_state (scanner, &state);
3317 if (token != G_TOKEN_NONE)
3320 token = g_scanner_get_next_token (scanner);
3321 if (token != G_TOKEN_EQUAL_SIGN)
3322 return G_TOKEN_EQUAL_SIGN;
3324 style->color_flags[state] |= GTK_RC_BASE;
3325 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3329 gtk_rc_parse_xthickness (GScanner *scanner,
3332 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3333 return GTK_RC_TOKEN_XTHICKNESS;
3335 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3336 return G_TOKEN_EQUAL_SIGN;
3338 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3341 style->xthickness = scanner->value.v_int;
3343 return G_TOKEN_NONE;
3347 gtk_rc_parse_ythickness (GScanner *scanner,
3350 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3351 return GTK_RC_TOKEN_YTHICKNESS;
3353 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3354 return G_TOKEN_EQUAL_SIGN;
3356 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3359 style->ythickness = scanner->value.v_int;
3361 return G_TOKEN_NONE;
3365 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3367 GtkRcStyle *rc_style)
3373 token = g_scanner_get_next_token (scanner);
3374 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3375 return GTK_RC_TOKEN_BG_PIXMAP;
3377 token = gtk_rc_parse_state (scanner, &state);
3378 if (token != G_TOKEN_NONE)
3381 token = g_scanner_get_next_token (scanner);
3382 if (token != G_TOKEN_EQUAL_SIGN)
3383 return G_TOKEN_EQUAL_SIGN;
3385 token = g_scanner_get_next_token (scanner);
3386 if (token != G_TOKEN_STRING)
3387 return G_TOKEN_STRING;
3389 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3390 (strcmp (scanner->value.v_string, "<none>") == 0))
3391 pixmap_file = g_strdup (scanner->value.v_string);
3393 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3394 scanner, scanner->value.v_string);
3398 g_free (rc_style->bg_pixmap_name[state]);
3399 rc_style->bg_pixmap_name[state] = pixmap_file;
3402 return G_TOKEN_NONE;
3406 gtk_rc_check_pixmap_dir (const gchar *dir,
3407 const gchar *pixmap_file)
3411 buf = g_build_filename (dir, pixmap_file, NULL);
3413 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3422 * gtk_rc_find_pixmap_in_path:
3423 * @settings: a #GtkSettings
3424 * @scanner: Scanner used to get line number information for the
3425 * warning message, or %NULL
3426 * @pixmap_file: name of the pixmap file to locate.
3428 * Looks up a file in pixmap path for the specified #GtkSettings.
3429 * If the file is not found, it outputs a warning message using
3430 * g_warning() and returns %NULL.
3432 * Return value: the filename.
3435 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3437 const gchar *pixmap_file)
3443 GtkRcContext *context = gtk_rc_context_get (settings);
3445 if (context->pixmap_path)
3446 for (i = 0; context->pixmap_path[i] != NULL; i++)
3448 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3453 tmp_list = current_files_stack;
3456 GtkRcFile *curfile = tmp_list->data;
3457 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3461 tmp_list = tmp_list->next;
3465 g_scanner_warn (scanner,
3466 _("Unable to locate image file in pixmap_path: \"%s\""),
3469 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3476 * gtk_rc_find_module_in_path:
3477 * @module_file: name of a theme engine
3479 * Searches for a theme engine in the GTK+ search path. This function
3480 * is not useful for applications and should not be used.
3482 * Return value: The filename, if found (must be freed with g_free()),
3486 gtk_rc_find_module_in_path (const gchar *module_file)
3488 return _gtk_find_module (module_file, "engines");
3492 gtk_rc_parse_font (GScanner *scanner,
3493 GtkRcStyle *rc_style)
3497 token = g_scanner_get_next_token (scanner);
3498 if (token != GTK_RC_TOKEN_FONT)
3499 return GTK_RC_TOKEN_FONT;
3501 token = g_scanner_get_next_token (scanner);
3502 if (token != G_TOKEN_EQUAL_SIGN)
3503 return G_TOKEN_EQUAL_SIGN;
3505 token = g_scanner_get_next_token (scanner);
3506 if (token != G_TOKEN_STRING)
3507 return G_TOKEN_STRING;
3509 /* Ignore, do nothing */
3511 return G_TOKEN_NONE;
3515 gtk_rc_parse_fontset (GScanner *scanner,
3516 GtkRcStyle *rc_style)
3520 token = g_scanner_get_next_token (scanner);
3521 if (token != GTK_RC_TOKEN_FONTSET)
3522 return GTK_RC_TOKEN_FONTSET;
3524 token = g_scanner_get_next_token (scanner);
3525 if (token != G_TOKEN_EQUAL_SIGN)
3526 return G_TOKEN_EQUAL_SIGN;
3528 token = g_scanner_get_next_token (scanner);
3529 if (token != G_TOKEN_STRING)
3530 return G_TOKEN_STRING;
3532 /* Do nothing - silently ignore */
3534 return G_TOKEN_NONE;
3538 gtk_rc_parse_font_name (GScanner *scanner,
3539 GtkRcStyle *rc_style)
3543 token = g_scanner_get_next_token (scanner);
3544 if (token != GTK_RC_TOKEN_FONT_NAME)
3545 return GTK_RC_TOKEN_FONT;
3547 token = g_scanner_get_next_token (scanner);
3548 if (token != G_TOKEN_EQUAL_SIGN)
3549 return G_TOKEN_EQUAL_SIGN;
3551 token = g_scanner_get_next_token (scanner);
3552 if (token != G_TOKEN_STRING)
3553 return G_TOKEN_STRING;
3555 if (rc_style->font_desc)
3556 pango_font_description_free (rc_style->font_desc);
3558 rc_style->font_desc =
3559 pango_font_description_from_string (scanner->value.v_string);
3561 return G_TOKEN_NONE;
3565 gtk_rc_parse_engine (GtkRcContext *context,
3567 GtkRcStyle **rc_style)
3570 GtkThemeEngine *engine;
3571 guint result = G_TOKEN_NONE;
3572 GtkRcStyle *new_style = NULL;
3573 gboolean parsed_curlies = FALSE;
3575 token = g_scanner_get_next_token (scanner);
3576 if (token != GTK_RC_TOKEN_ENGINE)
3577 return GTK_RC_TOKEN_ENGINE;
3579 token = g_scanner_get_next_token (scanner);
3580 if (token != G_TOKEN_STRING)
3581 return G_TOKEN_STRING;
3583 if (!scanner->value.v_string[0])
3585 /* Support engine "" {} to mean override to the default engine
3587 token = g_scanner_get_next_token (scanner);
3588 if (token != G_TOKEN_LEFT_CURLY)
3589 return G_TOKEN_LEFT_CURLY;
3591 token = g_scanner_get_next_token (scanner);
3592 if (token != G_TOKEN_RIGHT_CURLY)
3593 return G_TOKEN_RIGHT_CURLY;
3595 parsed_curlies = TRUE;
3597 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3599 new_style = gtk_rc_style_new ();
3600 gtk_rc_style_real_merge (new_style, *rc_style);
3602 if ((*rc_style)->name)
3603 new_style->name = g_strdup ((*rc_style)->name);
3606 (*rc_style)->engine_specified = TRUE;
3610 engine = gtk_theme_engine_get (scanner->value.v_string);
3612 token = g_scanner_get_next_token (scanner);
3613 if (token != G_TOKEN_LEFT_CURLY)
3614 return G_TOKEN_LEFT_CURLY;
3618 GtkRcStyleClass *new_class;
3620 new_style = gtk_theme_engine_create_rc_style (engine);
3621 g_type_module_unuse (G_TYPE_MODULE (engine));
3623 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3625 new_class->merge (new_style, *rc_style);
3626 if ((*rc_style)->name)
3627 new_style->name = g_strdup ((*rc_style)->name);
3629 if (new_class->parse)
3631 parsed_curlies = TRUE;
3632 result = new_class->parse (new_style, context->settings, scanner);
3634 if (result != G_TOKEN_NONE)
3636 g_object_unref (new_style);
3643 if (!parsed_curlies)
3645 /* Skip over remainder, looking for nested {}'s
3649 result = G_TOKEN_RIGHT_CURLY;
3650 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3652 if (token == G_TOKEN_LEFT_CURLY)
3654 else if (token == G_TOKEN_RIGHT_CURLY)
3659 result = G_TOKEN_NONE;
3667 GtkRcStylePrivate *rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3668 GtkRcStylePrivate *new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3670 /* take over icon factories and color hashes from the to-be-deleted style
3673 new_style->icon_factories = (*rc_style)->icon_factories;
3674 (*rc_style)->icon_factories = NULL;
3676 new_priv->color_hashes = rc_priv->color_hashes;
3677 rc_priv->color_hashes = NULL;
3679 new_style->engine_specified = TRUE;
3681 g_object_unref (*rc_style);
3682 *rc_style = new_style;
3689 gtk_rc_parse_state (GScanner *scanner,
3690 GtkStateType *state)
3695 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3696 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3698 /* we don't know where we got called from, so we reset the scope here.
3699 * if we bail out due to errors, we *don't* reset the scope, so the
3700 * error messaging code can make sense of our tokens.
3702 old_scope = g_scanner_set_scope (scanner, 0);
3704 token = g_scanner_get_next_token (scanner);
3705 if (token != G_TOKEN_LEFT_BRACE)
3706 return G_TOKEN_LEFT_BRACE;
3708 token = g_scanner_get_next_token (scanner);
3711 case GTK_RC_TOKEN_ACTIVE:
3712 *state = GTK_STATE_ACTIVE;
3714 case GTK_RC_TOKEN_INSENSITIVE:
3715 *state = GTK_STATE_INSENSITIVE;
3717 case GTK_RC_TOKEN_NORMAL:
3718 *state = GTK_STATE_NORMAL;
3720 case GTK_RC_TOKEN_PRELIGHT:
3721 *state = GTK_STATE_PRELIGHT;
3723 case GTK_RC_TOKEN_SELECTED:
3724 *state = GTK_STATE_SELECTED;
3727 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3730 token = g_scanner_get_next_token (scanner);
3731 if (token != G_TOKEN_RIGHT_BRACE)
3732 return G_TOKEN_RIGHT_BRACE;
3734 g_scanner_set_scope (scanner, old_scope);
3736 return G_TOKEN_NONE;
3740 gtk_rc_parse_priority (GScanner *scanner,
3741 GtkPathPriorityType *priority)
3746 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3747 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3749 /* we don't know where we got called from, so we reset the scope here.
3750 * if we bail out due to errors, we *don't* reset the scope, so the
3751 * error messaging code can make sense of our tokens.
3753 old_scope = g_scanner_set_scope (scanner, 0);
3755 token = g_scanner_get_next_token (scanner);
3759 token = g_scanner_get_next_token (scanner);
3762 case GTK_RC_TOKEN_LOWEST:
3763 *priority = GTK_PATH_PRIO_LOWEST;
3765 case GTK_RC_TOKEN_GTK:
3766 *priority = GTK_PATH_PRIO_GTK;
3768 case GTK_RC_TOKEN_APPLICATION:
3769 *priority = GTK_PATH_PRIO_APPLICATION;
3771 case GTK_RC_TOKEN_THEME:
3772 *priority = GTK_PATH_PRIO_THEME;
3774 case GTK_RC_TOKEN_RC:
3775 *priority = GTK_PATH_PRIO_RC;
3777 case GTK_RC_TOKEN_HIGHEST:
3778 *priority = GTK_PATH_PRIO_HIGHEST;
3781 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3784 g_scanner_set_scope (scanner, old_scope);
3786 return G_TOKEN_NONE;
3790 gtk_rc_parse_color (GScanner *scanner,
3793 return gtk_rc_parse_color_full (scanner, NULL, color);
3797 gtk_rc_parse_color_full (GScanner *scanner,
3803 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3805 /* we don't need to set our own scope here, because
3806 * we don't need own symbols
3809 token = g_scanner_get_next_token (scanner);
3817 case G_TOKEN_LEFT_CURLY:
3818 token = g_scanner_get_next_token (scanner);
3819 if (token == G_TOKEN_INT)
3820 token_int = scanner->value.v_int;
3821 else if (token == G_TOKEN_FLOAT)
3822 token_int = scanner->value.v_float * 65535.0;
3824 return G_TOKEN_FLOAT;
3825 color->red = CLAMP (token_int, 0, 65535);
3827 token = g_scanner_get_next_token (scanner);
3828 if (token != G_TOKEN_COMMA)
3829 return G_TOKEN_COMMA;
3831 token = g_scanner_get_next_token (scanner);
3832 if (token == G_TOKEN_INT)
3833 token_int = scanner->value.v_int;
3834 else if (token == G_TOKEN_FLOAT)
3835 token_int = scanner->value.v_float * 65535.0;
3837 return G_TOKEN_FLOAT;
3838 color->green = CLAMP (token_int, 0, 65535);
3840 token = g_scanner_get_next_token (scanner);
3841 if (token != G_TOKEN_COMMA)
3842 return G_TOKEN_COMMA;
3844 token = g_scanner_get_next_token (scanner);
3845 if (token == G_TOKEN_INT)
3846 token_int = scanner->value.v_int;
3847 else if (token == G_TOKEN_FLOAT)
3848 token_int = scanner->value.v_float * 65535.0;
3850 return G_TOKEN_FLOAT;
3851 color->blue = CLAMP (token_int, 0, 65535);
3853 token = g_scanner_get_next_token (scanner);
3854 if (token != G_TOKEN_RIGHT_CURLY)
3855 return G_TOKEN_RIGHT_CURLY;
3856 return G_TOKEN_NONE;
3858 case G_TOKEN_STRING:
3859 if (!gdk_color_parse (scanner->value.v_string, color))
3861 g_scanner_warn (scanner, "Invalid color constant '%s'",
3862 scanner->value.v_string);
3863 return G_TOKEN_STRING;
3865 return G_TOKEN_NONE;
3868 token = g_scanner_get_next_token (scanner);
3869 if (token != G_TOKEN_IDENTIFIER)
3870 return G_TOKEN_IDENTIFIER;
3872 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3874 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3875 scanner->value.v_identifier);
3876 return G_TOKEN_IDENTIFIER;
3879 return G_TOKEN_NONE;
3881 case G_TOKEN_IDENTIFIER:
3882 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3884 token = g_scanner_get_next_token (scanner);
3885 if (token != G_TOKEN_LEFT_PAREN)
3886 return G_TOKEN_LEFT_PAREN;
3889 if (g_scanner_peek_next_token (scanner) == '-')
3891 g_scanner_get_next_token (scanner); /* eat sign */
3895 token = g_scanner_get_next_token (scanner);
3896 if (token != G_TOKEN_FLOAT)
3897 return G_TOKEN_FLOAT;
3899 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3901 token = g_scanner_get_next_token (scanner);
3902 if (token != G_TOKEN_COMMA)
3903 return G_TOKEN_COMMA;
3905 token = gtk_rc_parse_color_full (scanner, style, &c1);
3906 if (token != G_TOKEN_NONE)
3909 token = g_scanner_get_next_token (scanner);
3910 if (token != G_TOKEN_COMMA)
3911 return G_TOKEN_COMMA;
3913 token = gtk_rc_parse_color_full (scanner, style, &c2);
3914 if (token != G_TOKEN_NONE)
3917 token = g_scanner_get_next_token (scanner);
3918 if (token != G_TOKEN_RIGHT_PAREN)
3919 return G_TOKEN_RIGHT_PAREN;
3921 color->red = l * c1.red + (1.0 - l) * c2.red;
3922 color->green = l * c1.green + (1.0 - l) * c2.green;
3923 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3925 return G_TOKEN_NONE;
3927 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
3929 token = g_scanner_get_next_token (scanner);
3930 if (token != G_TOKEN_LEFT_PAREN)
3931 return G_TOKEN_LEFT_PAREN;
3934 if (g_scanner_peek_next_token (scanner) == '-')
3936 g_scanner_get_next_token (scanner); /* eat sign */
3940 token = g_scanner_get_next_token (scanner);
3941 if (token != G_TOKEN_FLOAT)
3942 return G_TOKEN_FLOAT;
3944 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3946 token = g_scanner_get_next_token (scanner);
3947 if (token != G_TOKEN_COMMA)
3948 return G_TOKEN_COMMA;
3950 token = gtk_rc_parse_color_full (scanner, style, &c1);
3951 if (token != G_TOKEN_NONE)
3954 token = g_scanner_get_next_token (scanner);
3955 if (token != G_TOKEN_RIGHT_PAREN)
3956 return G_TOKEN_RIGHT_PAREN;
3958 _gtk_style_shade (&c1, color, l);
3960 return G_TOKEN_NONE;
3962 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
3963 strcmp (scanner->value.v_identifier, "darker") == 0)
3965 if (scanner->value.v_identifier[0] == 'l')
3970 token = g_scanner_get_next_token (scanner);
3971 if (token != G_TOKEN_LEFT_PAREN)
3972 return G_TOKEN_LEFT_PAREN;
3974 token = gtk_rc_parse_color_full (scanner, style, &c1);
3975 if (token != G_TOKEN_NONE)
3978 token = g_scanner_get_next_token (scanner);
3979 if (token != G_TOKEN_RIGHT_PAREN)
3980 return G_TOKEN_RIGHT_PAREN;
3982 _gtk_style_shade (&c1, color, l);
3984 return G_TOKEN_NONE;
3987 return G_TOKEN_IDENTIFIER;
3990 return G_TOKEN_STRING;
3995 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4000 token = g_scanner_get_next_token (scanner);
4001 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4002 return GTK_RC_TOKEN_PIXMAP_PATH;
4004 token = g_scanner_get_next_token (scanner);
4005 if (token != G_TOKEN_STRING)
4006 return G_TOKEN_STRING;
4008 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4010 return G_TOKEN_NONE;
4014 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4016 const gchar *pix_path)
4018 g_strfreev (context->pixmap_path);
4019 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4023 gtk_rc_parse_module_path (GScanner *scanner)
4027 token = g_scanner_get_next_token (scanner);
4028 if (token != GTK_RC_TOKEN_MODULE_PATH)
4029 return GTK_RC_TOKEN_MODULE_PATH;
4031 token = g_scanner_get_next_token (scanner);
4032 if (token != G_TOKEN_STRING)
4033 return G_TOKEN_STRING;
4035 g_warning ("module_path directive is now ignored\n");
4037 return G_TOKEN_NONE;
4041 gtk_rc_parse_im_module_file (GScanner *scanner)
4045 token = g_scanner_get_next_token (scanner);
4046 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4047 return GTK_RC_TOKEN_IM_MODULE_FILE;
4049 token = g_scanner_get_next_token (scanner);
4050 if (token != G_TOKEN_STRING)
4051 return G_TOKEN_STRING;
4053 g_free (im_module_file);
4055 im_module_file = g_strdup (scanner->value.v_string);
4057 return G_TOKEN_NONE;
4061 gtk_rc_parse_path_pattern (GtkRcContext *context,
4065 GtkPathType path_type;
4067 gboolean is_binding;
4068 GtkPathPriorityType priority = context->default_priority;
4070 token = g_scanner_get_next_token (scanner);
4073 case GTK_RC_TOKEN_WIDGET:
4074 path_type = GTK_PATH_WIDGET;
4076 case GTK_RC_TOKEN_WIDGET_CLASS:
4077 path_type = GTK_PATH_WIDGET_CLASS;
4079 case GTK_RC_TOKEN_CLASS:
4080 path_type = GTK_PATH_CLASS;
4083 return GTK_RC_TOKEN_WIDGET_CLASS;
4086 token = g_scanner_get_next_token (scanner);
4087 if (token != G_TOKEN_STRING)
4088 return G_TOKEN_STRING;
4090 pattern = g_strdup (scanner->value.v_string);
4092 token = g_scanner_get_next_token (scanner);
4093 if (token == GTK_RC_TOKEN_STYLE)
4095 else if (token == GTK_RC_TOKEN_BINDING)
4100 return GTK_RC_TOKEN_STYLE;
4103 if (g_scanner_peek_next_token (scanner) == ':')
4105 token = gtk_rc_parse_priority (scanner, &priority);
4106 if (token != G_TOKEN_NONE)
4113 token = g_scanner_get_next_token (scanner);
4114 if (token != G_TOKEN_STRING)
4117 return G_TOKEN_STRING;
4122 GtkBindingSet *binding;
4124 binding = gtk_binding_set_find (scanner->value.v_string);
4128 return G_TOKEN_STRING;
4130 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4134 GtkRcStyle *rc_style;
4137 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4142 return G_TOKEN_STRING;
4145 rc_set = g_new (GtkRcSet, 1);
4146 rc_set->type = path_type;
4148 if (path_type == GTK_PATH_WIDGET_CLASS)
4150 rc_set->pspec = NULL;
4151 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4155 rc_set->pspec = g_pattern_spec_new (pattern);
4156 rc_set->path = NULL;
4159 rc_set->rc_style = rc_style;
4160 rc_set->priority = priority;
4162 if (path_type == GTK_PATH_WIDGET)
4163 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4164 else if (path_type == GTK_PATH_WIDGET_CLASS)
4165 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4167 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4171 return G_TOKEN_NONE;
4175 gtk_rc_parse_hash_key (GScanner *scanner,
4180 token = g_scanner_get_next_token (scanner);
4181 if (token != G_TOKEN_LEFT_BRACE)
4182 return G_TOKEN_LEFT_BRACE;
4184 token = g_scanner_get_next_token (scanner);
4186 if (token != G_TOKEN_STRING)
4187 return G_TOKEN_STRING;
4189 *hash_key = g_strdup (scanner->value.v_string);
4191 token = g_scanner_get_next_token (scanner);
4192 if (token != G_TOKEN_RIGHT_BRACE)
4195 return G_TOKEN_RIGHT_BRACE;
4198 return G_TOKEN_NONE;
4202 gtk_rc_parse_icon_source (GtkRcContext *context,
4204 GtkIconSet *icon_set,
4205 gboolean *icon_set_valid)
4208 gchar *full_filename;
4209 GtkIconSource *source = NULL;
4211 token = g_scanner_get_next_token (scanner);
4212 if (token != G_TOKEN_LEFT_CURLY)
4213 return G_TOKEN_LEFT_CURLY;
4215 token = g_scanner_get_next_token (scanner);
4217 if (token != G_TOKEN_STRING && token != '@')
4218 return G_TOKEN_STRING;
4220 if (token == G_TOKEN_STRING)
4224 source = gtk_icon_source_new ();
4225 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4228 gtk_icon_source_set_filename (source, full_filename);
4229 g_free (full_filename);
4236 token = g_scanner_get_next_token (scanner);
4238 if (token != G_TOKEN_STRING)
4239 return G_TOKEN_STRING;
4241 source = gtk_icon_source_new ();
4242 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4245 /* We continue parsing even if we didn't find the pixmap so that rest of the
4246 * file is read, even if the syntax is bad. However we don't validate the
4247 * icon_set so the caller can choose not to install it.
4249 token = g_scanner_get_next_token (scanner);
4251 if (token == G_TOKEN_RIGHT_CURLY)
4253 else if (token != G_TOKEN_COMMA)
4255 gtk_icon_source_free (source);
4256 return G_TOKEN_COMMA;
4259 /* Get the direction */
4261 token = g_scanner_get_next_token (scanner);
4265 case GTK_RC_TOKEN_RTL:
4266 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4267 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4270 case GTK_RC_TOKEN_LTR:
4271 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4272 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4279 gtk_icon_source_free (source);
4280 return GTK_RC_TOKEN_RTL;
4284 token = g_scanner_get_next_token (scanner);
4286 if (token == G_TOKEN_RIGHT_CURLY)
4288 else if (token != G_TOKEN_COMMA)
4290 gtk_icon_source_free (source);
4291 return G_TOKEN_COMMA;
4296 token = g_scanner_get_next_token (scanner);
4300 case GTK_RC_TOKEN_NORMAL:
4301 gtk_icon_source_set_state_wildcarded (source, FALSE);
4302 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4305 case GTK_RC_TOKEN_PRELIGHT:
4306 gtk_icon_source_set_state_wildcarded (source, FALSE);
4307 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4311 case GTK_RC_TOKEN_INSENSITIVE:
4312 gtk_icon_source_set_state_wildcarded (source, FALSE);
4313 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4316 case GTK_RC_TOKEN_ACTIVE:
4317 gtk_icon_source_set_state_wildcarded (source, FALSE);
4318 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4321 case GTK_RC_TOKEN_SELECTED:
4322 gtk_icon_source_set_state_wildcarded (source, FALSE);
4323 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4330 gtk_icon_source_free (source);
4331 return GTK_RC_TOKEN_PRELIGHT;
4335 token = g_scanner_get_next_token (scanner);
4337 if (token == G_TOKEN_RIGHT_CURLY)
4339 else if (token != G_TOKEN_COMMA)
4341 gtk_icon_source_free (source);
4342 return G_TOKEN_COMMA;
4347 token = g_scanner_get_next_token (scanner);
4353 if (token != G_TOKEN_STRING)
4355 gtk_icon_source_free (source);
4356 return G_TOKEN_STRING;
4359 size = gtk_icon_size_from_name (scanner->value.v_string);
4361 if (size != GTK_ICON_SIZE_INVALID)
4363 gtk_icon_source_set_size_wildcarded (source, FALSE);
4364 gtk_icon_source_set_size (source, size);
4368 /* Check the close brace */
4370 token = g_scanner_get_next_token (scanner);
4371 if (token != G_TOKEN_RIGHT_CURLY)
4373 gtk_icon_source_free (source);
4374 return G_TOKEN_RIGHT_CURLY;
4378 if (gtk_icon_source_get_filename (source) ||
4379 gtk_icon_source_get_icon_name (source))
4381 gtk_icon_set_add_source (icon_set, source);
4382 *icon_set_valid = TRUE;
4384 gtk_icon_source_free (source);
4386 return G_TOKEN_NONE;
4390 gtk_rc_parse_stock (GtkRcContext *context,
4392 GtkRcStyle *rc_style,
4393 GtkIconFactory *factory)
4395 GtkIconSet *icon_set = NULL;
4396 gboolean icon_set_valid = FALSE;
4397 gchar *stock_id = NULL;
4400 token = g_scanner_get_next_token (scanner);
4401 if (token != GTK_RC_TOKEN_STOCK)
4402 return GTK_RC_TOKEN_STOCK;
4404 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4405 if (token != G_TOKEN_NONE)
4408 token = g_scanner_get_next_token (scanner);
4409 if (token != G_TOKEN_EQUAL_SIGN)
4412 return G_TOKEN_EQUAL_SIGN;
4415 token = g_scanner_get_next_token (scanner);
4416 if (token != G_TOKEN_LEFT_CURLY)
4419 return G_TOKEN_LEFT_CURLY;
4422 token = g_scanner_peek_next_token (scanner);
4423 while (token != G_TOKEN_RIGHT_CURLY)
4425 if (icon_set == NULL)
4426 icon_set = gtk_icon_set_new ();
4428 token = gtk_rc_parse_icon_source (context,
4429 scanner, icon_set, &icon_set_valid);
4430 if (token != G_TOKEN_NONE)
4433 gtk_icon_set_unref (icon_set);
4437 token = g_scanner_get_next_token (scanner);
4439 if (token != G_TOKEN_COMMA &&
4440 token != G_TOKEN_RIGHT_CURLY)
4443 gtk_icon_set_unref (icon_set);
4444 return G_TOKEN_RIGHT_CURLY;
4451 gtk_icon_factory_add (factory,
4455 gtk_icon_set_unref (icon_set);
4460 return G_TOKEN_NONE;
4464 gtk_rc_parse_logical_color (GScanner *scanner,
4465 GtkRcStyle *rc_style,
4468 gchar *color_id = NULL;
4472 token = g_scanner_get_next_token (scanner);
4473 if (token != GTK_RC_TOKEN_COLOR)
4474 return GTK_RC_TOKEN_COLOR;
4476 token = gtk_rc_parse_hash_key (scanner, &color_id);
4477 if (token != G_TOKEN_NONE)
4480 token = g_scanner_get_next_token (scanner);
4481 if (token != G_TOKEN_EQUAL_SIGN)
4484 return G_TOKEN_EQUAL_SIGN;
4487 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4488 if (token != G_TOKEN_NONE)
4494 /* Because the hash is created with destroy functions,
4495 * g_hash_table_insert will free any old values for us,
4496 * if a mapping with the specified key already exists.
4498 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4500 return G_TOKEN_NONE;
4505 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4509 const gchar *current;
4510 const gchar *class_start;
4511 const gchar *class_end;
4512 const gchar *pattern_end;
4513 const gchar *pattern_start;
4518 while ((class_start = strchr (current, '<')) &&
4519 (class_end = strchr (class_start, '>')))
4521 /* Add patterns, but ignore single dots */
4522 if (!(class_start == current ||
4523 (class_start == current + 1 && current[0] == '.')))
4525 pattern_end = class_start - 1;
4526 pattern_start = current;
4528 path_elt = g_new (PathElt, 1);
4530 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4531 path_elt->type = PATH_ELT_PSPEC;
4532 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4533 g_free (sub_pattern);
4535 result = g_slist_prepend (result, path_elt);
4538 path_elt = g_new (PathElt, 1);
4540 /* The < > need to be removed from the string. */
4541 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4543 path_elt->type = PATH_ELT_UNRESOLVED;
4544 path_elt->elt.class_name = sub_pattern;
4546 result = g_slist_prepend (result, path_elt);
4548 current = class_end + 1;
4551 /* Add the rest, if anything is left */
4552 if (strlen (current) > 0)
4554 path_elt = g_new (PathElt, 1);
4555 path_elt->type = PATH_ELT_PSPEC;
4556 path_elt->elt.pspec = g_pattern_spec_new (current);
4558 result = g_slist_prepend (result, path_elt);
4561 return g_slist_reverse (result);
4565 free_path_elt (gpointer data,
4568 PathElt *path_elt = data;
4570 switch (path_elt->type)
4572 case PATH_ELT_PSPEC:
4573 g_pattern_spec_free (path_elt->elt.pspec);
4575 case PATH_ELT_UNRESOLVED:
4576 g_free (path_elt->elt.class_name);
4581 g_assert_not_reached ();
4588 _gtk_rc_free_widget_class_path (GSList *list)
4590 g_slist_foreach (list, free_path_elt, NULL);
4591 g_slist_free (list);
4595 gtk_rc_set_free (GtkRcSet *rc_set)
4598 g_pattern_spec_free (rc_set->pspec);
4600 _gtk_rc_free_widget_class_path (rc_set->path);
4606 match_class (PathElt *path_elt,
4611 if (path_elt->type == PATH_ELT_UNRESOLVED)
4613 type = g_type_from_name (path_elt->elt.class_name);
4614 if (type != G_TYPE_INVALID)
4616 g_free (path_elt->elt.class_name);
4617 path_elt->elt.class_type = type;
4618 path_elt->type = PATH_ELT_TYPE;
4621 return g_str_equal (type_name, path_elt->elt.class_name);
4624 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4628 match_widget_class_recursive (GSList *list,
4631 gchar *path_reversed)
4635 /* break out if we cannot match anymore. */
4644 /* there are two possibilities:
4645 * 1. The next pattern should match the class.
4646 * 2. First normal matching, and then maybe a class */
4648 path_elt = list->data;
4650 if (path_elt->type != PATH_ELT_PSPEC)
4652 gchar *class_start = path;
4655 /* ignore leading dot */
4656 if (class_start[0] == '.')
4658 class_end = strchr (class_start, '.');
4660 if (class_end == NULL)
4662 if (!match_class (path_elt, class_start))
4665 return match_widget_class_recursive (list->next, 0, "", "");
4669 class_end[0] = '\0';
4670 if (!match_class (path_elt, class_start))
4678 gint new_length = length - (class_end - path);
4679 gchar old_char = path_reversed[new_length];
4683 path_reversed[new_length] = '\0';
4684 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4685 path_reversed[new_length] = old_char;
4696 gboolean result = FALSE;
4698 /* If there is nothing after this (ie. no class match),
4699 * just compare the pspec.
4701 if (list->next == NULL)
4702 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4704 class_elt = (PathElt *)list->next->data;
4705 g_assert (class_elt->type != PATH_ELT_PSPEC);
4708 if (class_start[0] == '.')
4713 class_end = strchr (class_start, '.');
4715 /* It should be cheaper to match the class first. (either the pattern
4716 * is simple, and will match most of the times, or it may be complex
4717 * and matching is slow)
4719 if (class_end == NULL)
4721 result = match_class (class_elt, class_start);
4725 class_end[0] = '\0';
4726 result = match_class (class_elt, class_start);
4735 /* terminate the string in front of the class. It does not matter
4736 * that the class becomes unusable, because it is not needed
4737 * inside the recursion
4739 old_char = class_start[0];
4740 class_start[0] = '\0';
4742 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4744 if (class_end != NULL)
4746 gint new_length = length - (class_end - path);
4747 gchar path_reversed_char = path_reversed[new_length];
4749 path_reversed[new_length] = '\0';
4751 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4753 path_reversed[new_length] = path_reversed_char;
4756 result = match_widget_class_recursive (list->next->next, 0, "", "");
4759 class_start[0] = old_char;
4765 /* get next class in path, or break out */
4766 if (class_end != NULL)
4767 class_start = class_end + 1;
4775 _gtk_rc_match_widget_class (GSList *list,
4778 gchar *path_reversed)
4780 return match_widget_class_recursive (list, length, path, path_reversed);
4785 /* DLL ABI stability backward compatibility versions */
4787 #undef gtk_rc_add_default_file
4790 gtk_rc_add_default_file (const gchar *filename)
4792 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4794 gtk_rc_add_default_file_utf8 (utf8_filename);
4796 g_free (utf8_filename);
4799 #undef gtk_rc_set_default_files
4802 gtk_rc_set_default_files (gchar **filenames)
4804 gchar **utf8_filenames;
4807 while (filenames[n++] != NULL)
4810 utf8_filenames = g_new (gchar *, n + 1);
4812 for (i = 0; i < n; i++)
4813 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4815 utf8_filenames[n] = NULL;
4817 gtk_rc_set_default_files_utf8 (utf8_filenames);
4819 g_strfreev (utf8_filenames);
4825 gtk_rc_parse (const gchar *filename)
4827 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4829 gtk_rc_parse_utf8 (utf8_filename);
4831 g_free (utf8_filename);
4836 #define __GTK_RC_C__
4837 #include "gtkaliasdef.c"