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 if (context->color_hash)
667 g_hash_table_ref (context->color_hash);
669 if (!context->reloading)
670 gtk_rc_reparse_all_for_settings (settings, TRUE);
673 static GtkRcContext *
674 gtk_rc_context_get (GtkSettings *settings)
676 if (!settings->rc_context)
678 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
680 context->settings = settings;
681 context->rc_style_ht = NULL;
682 context->rc_sets_widget = NULL;
683 context->rc_sets_widget_class = NULL;
684 context->rc_sets_class = NULL;
685 context->rc_files = NULL;
686 context->default_style = NULL;
688 g_object_get (settings,
689 "gtk-theme-name", &context->theme_name,
690 "gtk-key-theme-name", &context->key_theme_name,
691 "gtk-font-name", &context->font_name,
692 "color-hash", &context->color_hash,
695 if (context->color_hash)
696 g_hash_table_ref (context->color_hash);
698 g_signal_connect (settings,
699 "notify::gtk-theme-name",
700 G_CALLBACK (gtk_rc_settings_changed),
702 g_signal_connect (settings,
703 "notify::gtk-key-theme-name",
704 G_CALLBACK (gtk_rc_settings_changed),
706 g_signal_connect (settings,
707 "notify::gtk-font-name",
708 G_CALLBACK (gtk_rc_font_name_changed),
710 g_signal_connect (settings,
711 "notify::color-hash",
712 G_CALLBACK (gtk_rc_color_hash_changed),
715 context->pixmap_path = NULL;
717 context->default_priority = GTK_PATH_PRIO_RC;
719 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
722 return settings->rc_context;
726 gtk_rc_clear_rc_files (GtkRcContext *context)
730 list = context->rc_files;
733 GtkRcFile *rc_file = list->data;
735 if (rc_file->canonical_name != rc_file->name)
736 g_free (rc_file->canonical_name);
737 g_free (rc_file->directory);
738 g_free (rc_file->name);
744 g_slist_free (context->rc_files);
745 context->rc_files = NULL;
749 _gtk_rc_context_destroy (GtkSettings *settings)
751 GtkRcContext *context;
753 g_return_if_fail (GTK_IS_SETTINGS (settings));
755 context = settings->rc_context;
759 _gtk_settings_reset_rc_values (context->settings);
760 gtk_rc_clear_styles (context);
761 gtk_rc_clear_rc_files (context);
763 if (context->default_style)
764 g_object_unref (context->default_style);
766 g_strfreev (context->pixmap_path);
768 g_free (context->theme_name);
769 g_free (context->key_theme_name);
770 g_free (context->font_name);
772 if (context->color_hash)
773 g_hash_table_unref (context->color_hash);
775 g_signal_handlers_disconnect_by_func (settings,
776 gtk_rc_settings_changed, context);
777 g_signal_handlers_disconnect_by_func (settings,
778 gtk_rc_font_name_changed, context);
779 g_signal_handlers_disconnect_by_func (settings,
780 gtk_rc_color_hash_changed, context);
782 rc_contexts = g_slist_remove (rc_contexts, context);
786 settings->rc_context = NULL;
790 gtk_rc_parse_named (GtkRcContext *context,
795 const gchar *home_dir;
799 subpath = g_strconcat ("gtk-2.0-", type,
800 G_DIR_SEPARATOR_S "gtkrc",
803 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
805 /* First look in the users home directory
807 home_dir = g_get_home_dir ();
810 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
811 if (!g_file_test (path, G_FILE_TEST_EXISTS))
820 gchar *theme_dir = gtk_rc_get_theme_dir ();
821 path = g_build_filename (theme_dir, name, subpath, NULL);
824 if (!g_file_test (path, G_FILE_TEST_EXISTS))
833 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
841 gtk_rc_parse_default_files (GtkRcContext *context)
845 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
846 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
852 static gboolean initialized = FALSE;
858 gtk_rc_add_initial_default_files ();
861 /* Default RC string */
862 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
863 " bg[NORMAL] = \"#eee1b3\"\n"
864 " fg[NORMAL] = \"#000000\"\n"
867 "style \"gtk-default-progress-bar-style\" {\n"
868 " bg[PRELIGHT] = \"#4b6983\"\n"
869 " fg[PRELIGHT] = \"#ffffff\"\n"
870 " bg[NORMAL] = \"#c4c2bd\"\n"
873 "style \"gtk-default-menu-bar-item-style\" {\n"
874 " GtkMenuItem::horizontal_padding = 5\n"
877 "style \"gtk-default-menu-item-style\" {\n"
878 " bg[PRELIGHT] = \"#4b6983\"\n"
879 " fg[PRELIGHT] = \"#ffffff\"\n"
880 " base[PRELIGHT] = \"#4b6983\"\n"
881 " text[PRELIGHT] = \"#ffffff\"\n"
884 /* Make transparent tray icons work */
885 "style \"gtk-default-tray-icon-style\" {\n"
886 " bg_pixmap[NORMAL] = \"<parent>\"\n"
889 /* Work around clipping of accelerator underlines */
890 "style \"gtk-default-label-style\" {\n"
891 " GtkWidget::draw-border = {0,0,0,1}\n"
894 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
895 "class \"GtkTrayIcon\" style : gtk \"gtk-default-tray-icon-style\"\n"
896 "widget \"gtk-tooltips*\" style : gtk \"gtk-default-tooltips-style\"\n"
897 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
898 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
899 "class \"GtkLabel\" style : gtk \"gtk-default-label-style\"\n"
904 gtk_rc_context_parse_string (GtkRcContext *context,
905 const gchar *rc_string)
907 gtk_rc_parse_any (context, "-", -1, rc_string);
911 gtk_rc_parse_string (const gchar *rc_string)
916 g_return_if_fail (rc_string != NULL);
918 rc_file = g_new (GtkRcFile, 1);
919 rc_file->is_string = TRUE;
920 rc_file->name = g_strdup (rc_string);
921 rc_file->canonical_name = NULL;
922 rc_file->directory = NULL;
924 rc_file->reload = TRUE;
926 global_rc_files = g_slist_append (global_rc_files, rc_file);
928 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
929 gtk_rc_context_parse_string (tmp_list->data, rc_string);
933 add_to_rc_file_list (GSList **rc_file_list,
934 const char *filename,
940 tmp_list = *rc_file_list;
943 rc_file = tmp_list->data;
944 if (!strcmp (rc_file->name, filename))
947 tmp_list = tmp_list->next;
950 rc_file = g_new (GtkRcFile, 1);
951 rc_file->is_string = FALSE;
952 rc_file->name = g_strdup (filename);
953 rc_file->canonical_name = NULL;
954 rc_file->directory = NULL;
956 rc_file->reload = reload;
958 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
964 gtk_rc_context_parse_one_file (GtkRcContext *context,
965 const gchar *filename,
973 g_return_if_fail (filename != NULL);
975 saved_priority = context->default_priority;
976 context->default_priority = priority;
978 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
980 if (!rc_file->canonical_name)
982 /* Get the absolute pathname */
984 if (g_path_is_absolute (rc_file->name))
985 rc_file->canonical_name = rc_file->name;
990 cwd = g_get_current_dir ();
991 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
995 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
998 /* If the file is already being parsed (recursion), do nothing
1000 if (g_slist_find (current_files_stack, rc_file))
1003 if (!g_lstat (rc_file->canonical_name, &statbuf))
1007 rc_file->mtime = statbuf.st_mtime;
1009 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1013 /* Temporarily push information for this file on
1014 * a stack of current files while parsing it.
1016 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1017 gtk_rc_parse_any (context, filename, fd, NULL);
1018 current_files_stack = g_slist_delete_link (current_files_stack,
1019 current_files_stack);
1025 context->default_priority = saved_priority;
1029 strchr_len (const gchar *str, gint len, char c)
1034 return (gchar *)str;
1043 gtk_rc_context_parse_file (GtkRcContext *context,
1044 const gchar *filename,
1048 gchar *locale_suffixes[2];
1049 gint n_locale_suffixes = 0;
1053 gboolean found = FALSE;
1055 locale = _gtk_get_lc_ctype ();
1057 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1059 /* Determine locale-specific suffixes for RC files.
1061 length = strlen (locale);
1063 p = strchr (locale, '@');
1065 length = p - locale;
1067 p = strchr_len (locale, length, '.');
1069 length = p - locale;
1071 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1073 p = strchr_len (locale, length, '_');
1076 length = p - locale;
1077 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1083 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1084 for (j = 0; j < n_locale_suffixes; j++)
1088 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1089 if (g_file_test (name, G_FILE_TEST_EXISTS))
1091 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1098 g_free (locale_suffixes[j]);
1103 gtk_rc_parse (const gchar *filename)
1107 g_return_if_fail (filename != NULL);
1109 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1111 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1112 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1115 /* Handling of RC styles */
1117 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1120 gtk_rc_style_init (GtkRcStyle *style)
1122 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1126 style->font_desc = NULL;
1128 for (i = 0; i < 5; i++)
1130 static const GdkColor init_color = { 0, 0, 0, 0, };
1132 style->bg_pixmap_name[i] = NULL;
1133 style->color_flags[i] = 0;
1134 style->fg[i] = init_color;
1135 style->bg[i] = init_color;
1136 style->text[i] = init_color;
1137 style->base[i] = init_color;
1139 style->xthickness = -1;
1140 style->ythickness = -1;
1141 style->rc_properties = NULL;
1143 style->rc_style_lists = NULL;
1144 style->icon_factories = NULL;
1146 priv->color_hashes = NULL;
1150 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1152 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1154 object_class->finalize = gtk_rc_style_finalize;
1156 klass->parse = NULL;
1157 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1158 klass->merge = gtk_rc_style_real_merge;
1159 klass->create_style = gtk_rc_style_real_create_style;
1161 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1165 gtk_rc_style_finalize (GObject *object)
1167 GSList *tmp_list1, *tmp_list2;
1168 GtkRcStyle *rc_style;
1169 GtkRcStylePrivate *rc_priv;
1172 rc_style = GTK_RC_STYLE (object);
1173 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1176 g_free (rc_style->name);
1177 if (rc_style->font_desc)
1178 pango_font_description_free (rc_style->font_desc);
1180 for (i = 0; i < 5; i++)
1181 if (rc_style->bg_pixmap_name[i])
1182 g_free (rc_style->bg_pixmap_name[i]);
1184 /* Now remove all references to this rc_style from
1187 tmp_list1 = rc_style->rc_style_lists;
1190 GSList *rc_styles = tmp_list1->data;
1191 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1192 g_object_unref (style);
1194 /* Remove the list of styles from the other rc_styles
1197 tmp_list2 = rc_styles;
1200 GtkRcStyle *other_style = tmp_list2->data;
1202 if (other_style != rc_style)
1203 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1205 tmp_list2 = tmp_list2->next;
1208 /* And from the hash table itself
1210 g_hash_table_remove (realized_style_ht, rc_styles);
1211 g_slist_free (rc_styles);
1213 tmp_list1 = tmp_list1->next;
1215 g_slist_free (rc_style->rc_style_lists);
1217 if (rc_style->rc_properties)
1221 for (i = 0; i < rc_style->rc_properties->len; i++)
1223 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1225 g_free (node->origin);
1226 g_value_unset (&node->value);
1228 g_array_free (rc_style->rc_properties, TRUE);
1229 rc_style->rc_properties = NULL;
1232 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1233 g_slist_free (rc_style->icon_factories);
1235 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1236 g_slist_free (rc_priv->color_hashes);
1238 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1242 gtk_rc_style_new (void)
1246 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1252 * gtk_rc_style_copy:
1253 * @orig: the style to copy
1255 * Makes a copy of the specified #GtkRcStyle. This function
1256 * will correctly copy an RC style that is a member of a class
1257 * derived from #GtkRcStyle.
1259 * Return value: the resulting #GtkRcStyle
1262 gtk_rc_style_copy (GtkRcStyle *orig)
1266 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1268 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1269 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1271 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1277 gtk_rc_style_ref (GtkRcStyle *rc_style)
1279 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1281 g_object_ref (rc_style);
1285 gtk_rc_style_unref (GtkRcStyle *rc_style)
1287 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1289 g_object_unref (rc_style);
1293 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1295 return g_object_new (G_OBJECT_TYPE (style), NULL);
1299 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1301 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1303 return priv->color_hashes;
1307 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1308 gconstpointer bsearch_node2)
1310 const GtkRcProperty *prop1 = bsearch_node1;
1311 const GtkRcProperty *prop2 = bsearch_node2;
1313 if (prop1->type_name == prop2->type_name)
1314 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1316 return prop1->type_name < prop2->type_name ? -1 : 1;
1320 insert_rc_property (GtkRcStyle *style,
1321 GtkRcProperty *property,
1325 GtkRcProperty *new_property = NULL;
1326 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1328 key.type_name = property->type_name;
1329 key.property_name = property->property_name;
1331 if (!style->rc_properties)
1332 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1335 while (i < style->rc_properties->len)
1337 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1343 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1345 g_free (new_property->origin);
1346 g_value_unset (&new_property->value);
1348 *new_property = key;
1362 g_array_insert_val (style->rc_properties, i, key);
1363 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1366 new_property->origin = g_strdup (property->origin);
1367 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1368 g_value_copy (&property->value, &new_property->value);
1372 gtk_rc_style_real_merge (GtkRcStyle *dest,
1377 for (i = 0; i < 5; i++)
1379 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1380 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1382 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1383 src->color_flags[i] & GTK_RC_FG)
1385 dest->fg[i] = src->fg[i];
1386 dest->color_flags[i] |= GTK_RC_FG;
1388 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1389 src->color_flags[i] & GTK_RC_BG)
1391 dest->bg[i] = src->bg[i];
1392 dest->color_flags[i] |= GTK_RC_BG;
1394 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1395 src->color_flags[i] & GTK_RC_TEXT)
1397 dest->text[i] = src->text[i];
1398 dest->color_flags[i] |= GTK_RC_TEXT;
1400 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1401 src->color_flags[i] & GTK_RC_BASE)
1403 dest->base[i] = src->base[i];
1404 dest->color_flags[i] |= GTK_RC_BASE;
1408 if (dest->xthickness < 0 && src->xthickness >= 0)
1409 dest->xthickness = src->xthickness;
1410 if (dest->ythickness < 0 && src->ythickness >= 0)
1411 dest->ythickness = src->ythickness;
1415 if (!dest->font_desc)
1416 dest->font_desc = pango_font_description_copy (src->font_desc);
1418 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1421 if (src->rc_properties)
1425 for (i = 0; i < src->rc_properties->len; i++)
1426 insert_rc_property (dest,
1427 &g_array_index (src->rc_properties, GtkRcProperty, i),
1433 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1435 return gtk_style_new ();
1439 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1441 GtkIconFactory *factory = gtk_icon_factory_new ();
1443 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1447 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1449 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1450 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1452 (GDestroyNotify) gdk_color_free);
1454 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1458 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1459 GtkRcStyle *src_style)
1461 GSList *concat = g_slist_copy (src_style->icon_factories);
1463 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1465 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1469 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1470 GtkRcStyle *src_style)
1472 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1473 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1474 GSList *concat = g_slist_copy (src_priv->color_hashes);
1476 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1478 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1482 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1483 GtkRcStyle *src_style,
1484 GtkRcContext *context)
1486 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1490 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1492 /* Append src_style's factories, adding a ref to them */
1493 if (src_style->icon_factories != NULL)
1495 /* Add a factory for ourselves if we have none,
1496 * in case we end up defining more stock icons.
1497 * I see no real way around this; we need to maintain
1498 * the invariant that the first factory in the list
1499 * is always our_factory, the one belonging to us,
1500 * and if we put src_style factories in the list we can't
1501 * do that if the style is reopened.
1503 if (rc_style->icon_factories == NULL)
1504 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1506 gtk_rc_style_append_icon_factories (rc_style, src_style);
1509 /* Also append src_style's color hashes, adding a ref to them */
1510 if (src_priv->color_hashes != NULL)
1512 /* See comment above .. */
1513 if (priv->color_hashes == NULL)
1514 gtk_rc_style_prepend_empty_color_hash (rc_style);
1516 gtk_rc_style_append_color_hashes (rc_style, src_style);
1520 /* if we didn't get color hashes from the src_style, initialize
1521 * the list with the settings' color scheme (if it exists)
1523 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1525 gtk_rc_style_prepend_empty_color_hash (rc_style);
1527 priv->color_hashes =
1528 g_slist_append (priv->color_hashes,
1529 g_hash_table_ref (context->color_hash));
1534 gtk_rc_clear_hash_node (gpointer key,
1538 gtk_rc_style_unref (data);
1542 gtk_rc_free_rc_sets (GSList *slist)
1548 rc_set = slist->data;
1549 gtk_rc_set_free (rc_set);
1551 slist = slist->next;
1556 gtk_rc_clear_styles (GtkRcContext *context)
1558 /* Clear out all old rc_styles */
1560 if (context->rc_style_ht)
1562 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1563 g_hash_table_destroy (context->rc_style_ht);
1564 context->rc_style_ht = NULL;
1567 gtk_rc_free_rc_sets (context->rc_sets_widget);
1568 g_slist_free (context->rc_sets_widget);
1569 context->rc_sets_widget = NULL;
1571 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1572 g_slist_free (context->rc_sets_widget_class);
1573 context->rc_sets_widget_class = NULL;
1575 gtk_rc_free_rc_sets (context->rc_sets_class);
1576 g_slist_free (context->rc_sets_class);
1577 context->rc_sets_class = NULL;
1580 /* Reset all our widgets. Also, we have to invalidate cached icons in
1581 * icon sets so they get re-rendered.
1584 gtk_rc_reset_widgets (GtkSettings *settings)
1586 GList *list, *toplevels;
1588 _gtk_icon_set_invalidate_caches ();
1590 toplevels = gtk_window_list_toplevels ();
1591 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1593 for (list = toplevels; list; list = list->next)
1595 if (gtk_widget_get_screen (list->data) == settings->screen)
1597 gtk_widget_reset_rc_styles (list->data);
1600 g_object_unref (list->data);
1602 g_list_free (toplevels);
1606 gtk_rc_clear_realized_style (gpointer key,
1610 GSList *rc_styles = key;
1611 GtkStyle *style = value;
1612 GSList *tmp_list = rc_styles;
1614 g_object_unref (style);
1618 GtkRcStyle *rc_style = tmp_list->data;
1620 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1622 tmp_list = tmp_list->next;
1625 g_slist_free (rc_styles);
1629 * gtk_rc_reset_styles:
1630 * @settings: a #GtkSettings
1632 * This function recomputes the styles for all widgets that use a
1633 * particular #GtkSettings object. (There is one #GtkSettings object
1634 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1635 * when some global parameter has changed that affects the appearance
1636 * of all widgets, because when a widget gets a new style, it will
1637 * both redraw and recompute any cached information about its
1638 * appearance. As an example, it is used when the default font size
1639 * set by the operating system changes. Note that this function
1640 * doesn't affect widgets that have a style set explicitely on them
1641 * with gtk_widget_set_style().
1646 gtk_rc_reset_styles (GtkSettings *settings)
1648 GtkRcContext *context;
1649 gboolean reset = FALSE;
1651 g_return_if_fail (GTK_IS_SETTINGS (settings));
1653 context = gtk_rc_context_get (settings);
1655 if (context->default_style)
1657 g_object_unref (context->default_style);
1658 context->default_style = NULL;
1662 /* Clear out styles that have been looked up already
1664 if (realized_style_ht)
1666 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1667 g_hash_table_destroy (realized_style_ht);
1668 realized_style_ht = NULL;
1673 gtk_rc_reset_widgets (settings);
1677 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1679 GtkRcContext *context;
1680 gchar *new_font_name;
1682 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1684 context = gtk_rc_context_get (settings);
1686 g_object_get (context->settings,
1687 "gtk-font-name", &new_font_name,
1690 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1692 g_free (context->font_name);
1693 context->font_name = g_strdup (new_font_name);
1695 gtk_rc_reset_styles (settings);
1698 g_free (new_font_name);
1700 return context->font_name;
1704 * gtk_rc_reparse_all_for_settings:
1705 * @settings: a #GtkSettings
1706 * @force_load: load whether or not anything changed
1708 * If the modification time on any previously read file
1709 * for the given #GtkSettings has changed, discard all style information
1710 * and then reread all previously read RC files.
1712 * Return value: %TRUE if the files were reread.
1715 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1716 gboolean force_load)
1718 gboolean mtime_modified = FALSE;
1721 GtkRcContext *context;
1722 struct stat statbuf;
1724 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1726 context = gtk_rc_context_get (settings);
1730 /* Check through and see if any of the RC's have had their
1731 * mtime modified. If so, reparse everything.
1733 tmp_list = context->rc_files;
1736 rc_file = tmp_list->data;
1738 if (!rc_file->is_string)
1740 if (!g_lstat (rc_file->name, &statbuf) &&
1741 (statbuf.st_mtime != rc_file->mtime))
1743 mtime_modified = TRUE;
1748 tmp_list = tmp_list->next;
1752 if (force_load || mtime_modified)
1754 _gtk_binding_reset_parsed ();
1755 gtk_rc_clear_styles (context);
1756 context->reloading = TRUE;
1758 _gtk_settings_reset_rc_values (context->settings);
1759 gtk_rc_clear_rc_files (context);
1761 gtk_rc_parse_default_files (context);
1763 tmp_list = global_rc_files;
1766 rc_file = tmp_list->data;
1768 if (rc_file->is_string)
1769 gtk_rc_context_parse_string (context, rc_file->name);
1771 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1773 tmp_list = tmp_list->next;
1776 g_free (context->theme_name);
1777 g_free (context->key_theme_name);
1779 g_object_get (context->settings,
1780 "gtk-theme-name", &context->theme_name,
1781 "gtk-key-theme-name", &context->key_theme_name,
1784 if (context->theme_name && context->theme_name[0])
1785 gtk_rc_parse_named (context, context->theme_name, NULL);
1786 if (context->key_theme_name && context->key_theme_name[0])
1787 gtk_rc_parse_named (context, context->key_theme_name, "key");
1789 context->reloading = FALSE;
1791 gtk_rc_reset_widgets (context->settings);
1794 return force_load || mtime_modified;
1798 * gtk_rc_reparse_all:
1800 * If the modification time on any previously read file for the
1801 * default #GtkSettings has changed, discard all style information
1802 * and then reread all previously read RC files.
1804 * Return value: %TRUE if the files were reread.
1807 gtk_rc_reparse_all (void)
1810 gboolean result = FALSE;
1812 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1814 GtkRcContext *context = tmp_list->data;
1815 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1823 gtk_rc_styles_match (GSList *rc_styles,
1827 gchar *path_reversed)
1834 rc_set = sets->data;
1837 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1839 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1840 rc_styles = g_slist_append (rc_styles, rc_set);
1844 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1845 rc_styles = g_slist_append (rc_styles, rc_set);
1853 rc_set_compare (gconstpointer a, gconstpointer b)
1855 const GtkRcSet *set_a = a;
1856 const GtkRcSet *set_b = b;
1858 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1862 sort_and_dereference_sets (GSList *styles)
1866 /* At this point, the list of sets is ordered by:
1868 * a) 'widget' patterns are earlier than 'widget_class' patterns
1869 * which are ealier than 'class' patterns.
1870 * a) For two matches for class patterns, a match to a child type
1871 * is before a match to a parent type
1872 * c) a match later in the RC file (or in a later RC file) is before a
1873 * match earlier in the RC file.
1875 * With a) taking precedence over b) which takes precendence over c).
1877 * Now sort by priority, which has the highest precendence for sort order
1879 styles = g_slist_sort (styles, rc_set_compare);
1881 /* Make styles->data = styles->data->rc_style
1886 GtkRcSet *set = tmp_list->data;
1887 tmp_list->data = set->rc_style;
1888 tmp_list = tmp_list->next;
1896 * @widget: a #GtkWidget
1898 * Finds all matching RC styles for a given widget,
1899 * composites them together, and then creates a
1900 * #GtkStyle representing the composite appearance.
1901 * (GTK+ actually keeps a cache of previously
1902 * created styles, so a new style may not be
1905 * Returns: the resulting style. No refcount is added
1906 * to the returned style, so if you want to save this
1907 * style around, you should add a reference yourself.
1910 gtk_rc_get_style (GtkWidget *widget)
1912 GtkRcStyle *widget_rc_style;
1913 GSList *rc_styles = NULL;
1914 GtkRcContext *context;
1916 static guint rc_style_key_id = 0;
1918 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1920 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1922 /* We allow the specification of a single rc style to be bound
1923 * tightly to a widget, for application modifications
1925 if (!rc_style_key_id)
1926 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1928 if (context->rc_sets_widget)
1930 gchar *path, *path_reversed;
1933 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1934 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1936 g_free (path_reversed);
1939 if (context->rc_sets_widget_class)
1941 gchar *path, *path_reversed;
1944 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1945 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1947 g_free (path_reversed);
1950 if (context->rc_sets_class)
1954 type = G_TYPE_FROM_INSTANCE (widget);
1958 gchar *path_reversed;
1961 path = g_strdup (g_type_name (type));
1962 path_length = strlen (path);
1963 path_reversed = g_strdup (path);
1964 g_strreverse (path_reversed);
1966 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1968 g_free (path_reversed);
1970 type = g_type_parent (type);
1974 rc_styles = sort_and_dereference_sets (rc_styles);
1976 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
1978 if (widget_rc_style)
1979 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1982 return gtk_rc_init_style (context, rc_styles);
1985 if (!context->default_style)
1987 context->default_style = gtk_style_new ();
1988 _gtk_style_init_for_settings (context->default_style, context->settings);
1991 return context->default_style;
1996 * gtk_rc_get_style_by_paths:
1997 * @settings: a #GtkSettings object
1998 * @widget_path: the widget path to use when looking up the style, or %NULL
1999 * if no matching against the widget path should be done
2000 * @class_path: the class path to use when looking up the style, or %NULL
2001 * if no matching against the class path should be done.
2002 * @type: a type that will be used along with parent types of this type
2003 * when matching against class styles, or #G_TYPE_NONE
2005 * Creates up a #GtkStyle from styles defined in a RC file by providing
2006 * the raw components used in matching. This function may be useful
2007 * when creating pseudo-widgets that should be themed like widgets but
2008 * don't actually have corresponding GTK+ widgets. An example of this
2009 * would be items inside a GNOME canvas widget.
2011 * The action of gtk_rc_get_style() is similar to:
2012 * <informalexample><programlisting>
2013 * gtk_widget_path (widget, NULL, &path, NULL);
2014 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2015 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
2016 * G_OBJECT_TYPE (widget));
2017 * </programlisting></informalexample>
2019 * Return value: A style created by matching with the supplied paths,
2020 * or %NULL if nothing matching was specified and the default style should
2021 * be used. The returned value is owned by GTK+ as part of an internal cache,
2022 * so you must call g_object_ref() on the returned value if you want to
2023 * keep a reference to it.
2026 gtk_rc_get_style_by_paths (GtkSettings *settings,
2027 const char *widget_path,
2028 const char *class_path,
2031 /* We duplicate the code from above to avoid slowing down the above
2032 * by generating paths when we don't need them. I don't know if
2033 * this is really worth it.
2035 GSList *rc_styles = NULL;
2036 GtkRcContext *context;
2038 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2040 context = gtk_rc_context_get (settings);
2042 if (widget_path && context->rc_sets_widget)
2045 gchar *path_reversed;
2048 path_length = strlen (widget_path);
2049 path = g_strdup (widget_path);
2050 path_reversed = g_strdup (widget_path);
2051 g_strreverse (path_reversed);
2053 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2055 g_free (path_reversed);
2058 if (class_path && context->rc_sets_widget_class)
2061 gchar *path_reversed;
2064 path = g_strdup (class_path);
2065 path_length = strlen (class_path);
2066 path_reversed = g_strdup (class_path);
2067 g_strreverse (path_reversed);
2069 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2071 g_free (path_reversed);
2074 if (type != G_TYPE_NONE && context->rc_sets_class)
2079 gchar *path_reversed;
2082 path = g_strdup (g_type_name (type));
2083 path_length = strlen (path);
2084 path_reversed = g_strdup (path);
2085 g_strreverse (path_reversed);
2087 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2089 g_free (path_reversed);
2091 type = g_type_parent (type);
2095 rc_styles = sort_and_dereference_sets (rc_styles);
2098 return gtk_rc_init_style (context, rc_styles);
2104 gtk_rc_add_rc_sets (GSList *slist,
2105 GtkRcStyle *rc_style,
2106 const gchar *pattern,
2107 GtkPathType path_type)
2109 GtkRcStyle *new_style;
2113 new_style = gtk_rc_style_new ();
2114 *new_style = *rc_style;
2115 new_style->name = g_strdup (rc_style->name);
2116 if (rc_style->font_desc)
2117 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
2119 for (i = 0; i < 5; i++)
2120 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
2122 rc_set = g_new (GtkRcSet, 1);
2123 rc_set->type = path_type;
2125 if (path_type == GTK_PATH_WIDGET_CLASS)
2127 rc_set->pspec = NULL;
2128 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
2132 rc_set->pspec = g_pattern_spec_new (pattern);
2133 rc_set->path = NULL;
2136 rc_set->rc_style = rc_style;
2138 return g_slist_prepend (slist, rc_set);
2142 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
2143 const gchar *pattern)
2145 GtkRcContext *context;
2147 g_return_if_fail (rc_style != NULL);
2148 g_return_if_fail (pattern != NULL);
2150 context = gtk_rc_context_get (gtk_settings_get_default ());
2152 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern, GTK_PATH_WIDGET);
2156 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
2157 const gchar *pattern)
2159 GtkRcContext *context;
2161 g_return_if_fail (rc_style != NULL);
2162 g_return_if_fail (pattern != NULL);
2164 context = gtk_rc_context_get (gtk_settings_get_default ());
2166 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern, GTK_PATH_WIDGET_CLASS);
2170 gtk_rc_add_class_style (GtkRcStyle *rc_style,
2171 const gchar *pattern)
2173 GtkRcContext *context;
2175 g_return_if_fail (rc_style != NULL);
2176 g_return_if_fail (pattern != NULL);
2178 context = gtk_rc_context_get (gtk_settings_get_default ());
2180 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern, GTK_PATH_CLASS);
2184 gtk_rc_scanner_new (void)
2186 return g_scanner_new (>k_rc_scanner_config);
2190 gtk_rc_parse_any (GtkRcContext *context,
2191 const gchar *input_name,
2193 const gchar *input_string)
2199 scanner = gtk_rc_scanner_new ();
2203 g_assert (input_string == NULL);
2205 g_scanner_input_file (scanner, input_fd);
2209 g_assert (input_string != NULL);
2211 g_scanner_input_text (scanner, input_string, strlen (input_string));
2213 scanner->input_name = input_name;
2215 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2216 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2220 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2224 guint expected_token;
2226 expected_token = gtk_rc_parse_statement (context, scanner);
2228 if (expected_token != G_TOKEN_NONE)
2235 if (scanner->scope_id == 0)
2237 /* if we are in scope 0, we know the symbol names
2238 * that are associated with certain token values.
2239 * so we look them up to make the error messages
2242 if (expected_token > GTK_RC_TOKEN_INVALID &&
2243 expected_token < GTK_RC_TOKEN_LAST)
2245 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2246 if (symbols[i].token == expected_token)
2247 msg = symbol_names + symbols[i].name_offset;
2249 msg = g_strconcat ("e.g. `", msg, "'", NULL);
2251 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2252 scanner->token < GTK_RC_TOKEN_LAST)
2254 symbol_name = "???";
2255 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2256 if (symbols[i].token == scanner->token)
2257 symbol_name = symbol_names + symbols[i].name_offset;
2260 g_scanner_unexp_token (scanner,
2273 g_scanner_destroy (scanner);
2277 gtk_rc_styles_hash (const GSList *rc_styles)
2284 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2285 rc_styles = rc_styles->next;
2292 gtk_rc_styles_equal (const GSList *a,
2297 if (a->data != b->data)
2307 gtk_rc_style_hash (const gchar *name)
2313 result += (result << 3) + *name++;
2319 gtk_rc_style_equal (const gchar *a,
2322 return (strcmp (a, b) == 0);
2326 gtk_rc_style_find (GtkRcContext *context,
2329 if (context->rc_style_ht)
2330 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2336 gtk_rc_style_to_style (GtkRcContext *context,
2337 GtkRcStyle *rc_style)
2341 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2342 _gtk_style_init_for_settings (style, context->settings);
2344 style->rc_style = g_object_ref (rc_style);
2346 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2351 /* Reuses or frees rc_styles */
2353 gtk_rc_init_style (GtkRcContext *context,
2356 GtkStyle *style = NULL;
2359 g_return_val_if_fail (rc_styles != NULL, NULL);
2361 if (!realized_style_ht)
2362 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2363 (GEqualFunc) gtk_rc_styles_equal);
2365 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2369 GtkRcStyle *base_style = NULL;
2370 GtkRcStyle *proto_style;
2371 GtkRcStyleClass *proto_style_class;
2373 GType rc_style_type = GTK_TYPE_RC_STYLE;
2375 /* Find the first style where the RC file specified engine "" {}
2376 * or the first derived style and use that to create the
2377 * merged style. If we only have raw GtkRcStyles, use the first
2378 * style to create the merged style.
2380 base_style = rc_styles->data;
2381 tmp_styles = rc_styles;
2384 GtkRcStyle *rc_style = tmp_styles->data;
2386 if (rc_style->engine_specified ||
2387 G_OBJECT_TYPE (rc_style) != rc_style_type)
2389 base_style = rc_style;
2393 tmp_styles = tmp_styles->next;
2396 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2397 proto_style = proto_style_class->create_rc_style (base_style);
2399 tmp_styles = rc_styles;
2402 GtkRcStyle *rc_style = tmp_styles->data;
2404 proto_style_class->merge (proto_style, rc_style);
2406 /* Point from each rc_style to the list of styles */
2407 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2408 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2410 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2411 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2413 tmp_styles = tmp_styles->next;
2416 for (i = 0; i < 5; i++)
2417 if (proto_style->bg_pixmap_name[i] &&
2418 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2420 g_free (proto_style->bg_pixmap_name[i]);
2421 proto_style->bg_pixmap_name[i] = NULL;
2424 style = gtk_rc_style_to_style (context, proto_style);
2425 gtk_rc_style_unref (proto_style);
2427 g_hash_table_insert (realized_style_ht, rc_styles, style);
2430 g_slist_free (rc_styles);
2435 /*********************
2436 * Parsing functions *
2437 *********************/
2440 lookup_color (GtkRcStyle *style,
2441 const char *color_name,
2444 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2447 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2449 GHashTable *hash = iter->data;
2450 GdkColor *match = g_hash_table_lookup (hash, color_name);
2454 color->red = match->red;
2455 color->green = match->green;
2456 color->blue = match->blue;
2465 rc_parse_token_or_compound (GScanner *scanner,
2468 GTokenType delimiter)
2470 guint token = g_scanner_get_next_token (scanner);
2472 /* we either scan a single token (skipping comments)
2473 * or a compund statement.
2474 * compunds are enclosed in (), [] or {} braces, we read
2475 * them in via deep recursion.
2482 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2485 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2487 case G_TOKEN_STRING:
2488 string = g_strescape (scanner->value.v_string, NULL);
2489 g_string_append (gstring, " \"");
2490 g_string_append (gstring, string);
2491 g_string_append_c (gstring, '"');
2494 case G_TOKEN_IDENTIFIER:
2495 g_string_append_c (gstring, ' ');
2496 g_string_append (gstring, scanner->value.v_identifier);
2498 case G_TOKEN_COMMENT_SINGLE:
2499 case G_TOKEN_COMMENT_MULTI:
2500 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2501 case G_TOKEN_LEFT_PAREN:
2502 g_string_append_c (gstring, ' ');
2503 g_string_append_c (gstring, token);
2504 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2505 if (token != G_TOKEN_NONE)
2508 case G_TOKEN_LEFT_CURLY:
2509 g_string_append_c (gstring, ' ');
2510 g_string_append_c (gstring, token);
2511 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2512 if (token != G_TOKEN_NONE)
2515 case G_TOKEN_LEFT_BRACE:
2516 g_string_append_c (gstring, ' ');
2517 g_string_append_c (gstring, token);
2518 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2519 if (token != G_TOKEN_NONE)
2523 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2526 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2527 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2528 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2530 g_scanner_get_next_token (scanner);
2532 if (!style || !lookup_color (style, scanner->value.v_identifier,
2535 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2536 scanner->value.v_identifier);
2537 return G_TOKEN_IDENTIFIER;
2541 g_string_append_printf (gstring, " { %s, %s, %s }",
2542 g_ascii_formatd (rbuf, sizeof (rbuf),
2544 color.red / 65535.0),
2545 g_ascii_formatd (gbuf, sizeof (gbuf),
2547 color.green / 65535.0),
2548 g_ascii_formatd (bbuf, sizeof (bbuf),
2550 color.blue / 65535.0));
2554 return G_TOKEN_IDENTIFIER;
2556 if (token >= 256 || token < 1)
2557 return delimiter ? delimiter : G_TOKEN_STRING;
2558 g_string_append_c (gstring, ' ');
2559 g_string_append_c (gstring, token);
2560 if (token == delimiter)
2561 return G_TOKEN_NONE;
2565 return G_TOKEN_NONE;
2567 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2571 gtk_rc_parse_assignment (GScanner *scanner,
2573 GtkRcProperty *prop)
2575 #define MY_SCAN_IDENTIFIER TRUE
2576 #define MY_SCAN_SYMBOLS FALSE
2577 #define MY_IDENTIFIER_2_STRING FALSE
2578 #define MY_CHAR_2_TOKEN TRUE
2579 #define MY_SCAN_IDENTIFIER_NULL FALSE
2580 #define MY_NUMBERS_2_INT TRUE
2582 gboolean scan_identifier = scanner->config->scan_identifier;
2583 gboolean scan_symbols = scanner->config->scan_symbols;
2584 gboolean identifier_2_string = scanner->config->identifier_2_string;
2585 gboolean char_2_token = scanner->config->char_2_token;
2586 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2587 gboolean numbers_2_int = scanner->config->numbers_2_int;
2588 gboolean negate = FALSE;
2589 gboolean is_color = FALSE;
2592 /* check that this is an assignment */
2593 if (g_scanner_get_next_token (scanner) != '=')
2596 /* adjust scanner mode */
2597 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2598 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2599 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2600 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2601 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2602 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2604 /* record location */
2605 if (g_getenv ("GTK_DEBUG"))
2606 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2608 prop->origin = NULL;
2610 /* parse optional symbolic color prefix */
2611 if (g_scanner_peek_next_token (scanner) == '@')
2613 g_scanner_get_next_token (scanner); /* eat color prefix */
2617 /* parse optional sign */
2618 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2620 g_scanner_get_next_token (scanner); /* eat sign */
2624 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2627 token = g_scanner_peek_next_token (scanner);
2629 if (is_color && token != G_TOKEN_IDENTIFIER)
2631 token = G_TOKEN_IDENTIFIER;
2638 g_scanner_get_next_token (scanner);
2639 g_value_init (&prop->value, G_TYPE_LONG);
2640 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2641 token = G_TOKEN_NONE;
2644 g_scanner_get_next_token (scanner);
2645 g_value_init (&prop->value, G_TYPE_DOUBLE);
2646 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2647 token = G_TOKEN_NONE;
2649 case G_TOKEN_STRING:
2650 g_scanner_get_next_token (scanner);
2652 token = G_TOKEN_INT;
2655 g_value_init (&prop->value, G_TYPE_STRING);
2656 g_value_set_string (&prop->value, scanner->value.v_string);
2657 token = G_TOKEN_NONE;
2660 case G_TOKEN_IDENTIFIER:
2664 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2665 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2666 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2669 g_scanner_get_next_token (scanner);
2671 if (!style || !lookup_color (style, scanner->value.v_identifier,
2674 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2675 scanner->value.v_identifier);
2676 token = G_TOKEN_IDENTIFIER;
2680 gstring = g_string_new (NULL);
2682 g_string_append_printf (gstring, " { %s, %s, %s }",
2683 g_ascii_formatd (rbuf, sizeof (rbuf),
2685 color.red / 65535.0),
2686 g_ascii_formatd (gbuf, sizeof (gbuf),
2688 color.green / 65535.0),
2689 g_ascii_formatd (bbuf, sizeof (bbuf),
2691 color.blue / 65535.0));
2693 g_value_init (&prop->value, G_TYPE_GSTRING);
2694 g_value_take_boxed (&prop->value, gstring);
2695 token = G_TOKEN_NONE;
2699 case G_TOKEN_LEFT_PAREN:
2700 case G_TOKEN_LEFT_CURLY:
2701 case G_TOKEN_LEFT_BRACE:
2704 GString *gstring = g_string_new (NULL);
2705 gboolean parse_on = TRUE;
2707 /* allow identifier(foobar) to support color expressions */
2708 if (token == G_TOKEN_IDENTIFIER)
2710 g_scanner_get_next_token (scanner);
2712 g_string_append_c (gstring, ' ');
2713 g_string_append (gstring, scanner->value.v_identifier);
2715 /* temporarily reset scanner mode to default, so we
2716 * don't peek the next token in a mode that only makes
2717 * sense in this function; because if anything but
2718 * G_TOKEN_LEFT_PAREN follows, the next token will be
2719 * parsed by our caller.
2721 * FIXME: right fix would be to call g_scanner_unget()
2722 * but that doesn't exist
2724 scanner->config->scan_identifier = scan_identifier;
2725 scanner->config->scan_symbols = scan_symbols;
2726 scanner->config->identifier_2_string = identifier_2_string;
2727 scanner->config->char_2_token = char_2_token;
2728 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2729 scanner->config->numbers_2_int = numbers_2_int;
2731 token = g_scanner_peek_next_token (scanner);
2733 /* restore adjusted scanner mode */
2734 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2735 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2736 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2737 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2738 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2739 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2741 if (token != G_TOKEN_LEFT_PAREN)
2743 token = G_TOKEN_NONE;
2749 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2751 if (token == G_TOKEN_NONE)
2753 g_string_append_c (gstring, ' ');
2754 g_value_init (&prop->value, G_TYPE_GSTRING);
2755 g_value_take_boxed (&prop->value, gstring);
2758 g_string_free (gstring, TRUE);
2763 g_scanner_get_next_token (scanner);
2764 token = G_TOKEN_INT;
2770 /* restore scanner mode */
2771 scanner->config->scan_identifier = scan_identifier;
2772 scanner->config->scan_symbols = scan_symbols;
2773 scanner->config->identifier_2_string = identifier_2_string;
2774 scanner->config->char_2_token = char_2_token;
2775 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2776 scanner->config->numbers_2_int = numbers_2_int;
2782 is_c_identifier (const gchar *string)
2785 gboolean is_varname;
2787 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2788 for (p = string + 1; *p && is_varname; p++)
2789 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2795 parse_include_file (GtkRcContext *context,
2797 const gchar *filename)
2799 char *to_parse = NULL;
2801 if (g_path_is_absolute (filename))
2803 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2804 * don't print an error in this case.
2806 to_parse = g_strdup (filename);
2810 /* if a relative path, we look relative to all the RC files in the
2811 * include stack. We require the file to be found in this case
2812 * so we can give meaningful error messages, and because on reparsing
2813 * non-absolute paths don't make sense.
2815 GSList *tmp_list = current_files_stack;
2818 GtkRcFile *curfile = tmp_list->data;
2819 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2821 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2829 tmp_list = tmp_list->next;
2835 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2840 g_scanner_warn (scanner,
2841 _("Unable to find include file: \"%s\""),
2848 gtk_rc_parse_statement (GtkRcContext *context,
2853 token = g_scanner_peek_next_token (scanner);
2856 case GTK_RC_TOKEN_INCLUDE:
2857 token = g_scanner_get_next_token (scanner);
2858 if (token != GTK_RC_TOKEN_INCLUDE)
2859 return GTK_RC_TOKEN_INCLUDE;
2860 token = g_scanner_get_next_token (scanner);
2861 if (token != G_TOKEN_STRING)
2862 return G_TOKEN_STRING;
2863 parse_include_file (context, scanner, scanner->value.v_string);
2864 return G_TOKEN_NONE;
2866 case GTK_RC_TOKEN_STYLE:
2867 return gtk_rc_parse_style (context, scanner);
2869 case GTK_RC_TOKEN_BINDING:
2870 return _gtk_binding_parse_binding (scanner);
2872 case GTK_RC_TOKEN_PIXMAP_PATH:
2873 return gtk_rc_parse_pixmap_path (context, scanner);
2875 case GTK_RC_TOKEN_WIDGET:
2876 return gtk_rc_parse_path_pattern (context, scanner);
2878 case GTK_RC_TOKEN_WIDGET_CLASS:
2879 return gtk_rc_parse_path_pattern (context, scanner);
2881 case GTK_RC_TOKEN_CLASS:
2882 return gtk_rc_parse_path_pattern (context, scanner);
2884 case GTK_RC_TOKEN_MODULE_PATH:
2885 return gtk_rc_parse_module_path (scanner);
2887 case GTK_RC_TOKEN_IM_MODULE_FILE:
2888 return gtk_rc_parse_im_module_file (scanner);
2890 case G_TOKEN_IDENTIFIER:
2891 if (is_c_identifier (scanner->next_value.v_identifier))
2893 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2896 g_scanner_get_next_token (scanner); /* eat identifier */
2897 name = g_strdup (scanner->value.v_identifier);
2899 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2900 if (token == G_TOKEN_NONE)
2902 GtkSettingsValue svalue;
2904 svalue.origin = prop.origin;
2905 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2906 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2907 _gtk_settings_set_property_value_from_rc (context->settings,
2911 g_free (prop.origin);
2912 if (G_VALUE_TYPE (&prop.value))
2913 g_value_unset (&prop.value);
2920 g_scanner_get_next_token (scanner);
2921 return G_TOKEN_IDENTIFIER;
2924 g_scanner_get_next_token (scanner);
2925 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2930 fixup_rc_set (GSList *list,
2936 GtkRcSet *set = list->data;
2937 if (set->rc_style == orig)
2938 set->rc_style = new;
2944 fixup_rc_sets (GtkRcContext *context,
2948 fixup_rc_set (context->rc_sets_widget, orig, new);
2949 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2950 fixup_rc_set (context->rc_sets_class, orig, new);
2954 gtk_rc_parse_style (GtkRcContext *context,
2957 GtkRcStyle *rc_style;
2958 GtkRcStyle *orig_style;
2959 GtkRcStyle *parent_style = NULL;
2960 GtkRcStylePrivate *rc_priv = NULL;
2963 GtkIconFactory *our_factory = NULL;
2964 GHashTable *our_hash = NULL;
2966 token = g_scanner_get_next_token (scanner);
2967 if (token != GTK_RC_TOKEN_STYLE)
2968 return GTK_RC_TOKEN_STYLE;
2970 token = g_scanner_get_next_token (scanner);
2971 if (token != G_TOKEN_STRING)
2972 return G_TOKEN_STRING;
2974 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2976 orig_style = g_object_ref (rc_style);
2982 rc_style = gtk_rc_style_new ();
2983 rc_style->name = g_strdup (scanner->value.v_string);
2985 for (i = 0; i < 5; i++)
2986 rc_style->bg_pixmap_name[i] = NULL;
2988 for (i = 0; i < 5; i++)
2989 rc_style->color_flags[i] = 0;
2992 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
2994 /* If there's a list, its first member is always the factory belonging
2997 if (rc_style->icon_factories)
2998 our_factory = rc_style->icon_factories->data;
2999 if (rc_priv->color_hashes)
3000 our_hash = rc_priv->color_hashes->data;
3002 token = g_scanner_peek_next_token (scanner);
3003 if (token == G_TOKEN_EQUAL_SIGN)
3005 token = g_scanner_get_next_token (scanner);
3007 token = g_scanner_get_next_token (scanner);
3008 if (token != G_TOKEN_STRING)
3010 token = G_TOKEN_STRING;
3014 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3017 for (i = 0; i < 5; i++)
3019 rc_style->color_flags[i] = parent_style->color_flags[i];
3020 rc_style->fg[i] = parent_style->fg[i];
3021 rc_style->bg[i] = parent_style->bg[i];
3022 rc_style->text[i] = parent_style->text[i];
3023 rc_style->base[i] = parent_style->base[i];
3026 rc_style->xthickness = parent_style->xthickness;
3027 rc_style->ythickness = parent_style->ythickness;
3029 if (parent_style->font_desc)
3031 if (rc_style->font_desc)
3032 pango_font_description_free (rc_style->font_desc);
3033 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3036 if (parent_style->rc_properties)
3040 for (i = 0; i < parent_style->rc_properties->len; i++)
3041 insert_rc_property (rc_style,
3042 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3046 for (i = 0; i < 5; i++)
3048 if (rc_style->bg_pixmap_name[i])
3049 g_free (rc_style->bg_pixmap_name[i]);
3050 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3055 /* get icon_factories and color_hashes from the parent style;
3056 * if the parent_style doesn't have color_hashes, initializes
3057 * the color_hashes with the settings' color scheme (if it exists)
3059 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3061 if (rc_style->icon_factories)
3062 our_factory = rc_style->icon_factories->data;
3063 if (rc_priv->color_hashes)
3064 our_hash = rc_priv->color_hashes->data;
3066 token = g_scanner_get_next_token (scanner);
3067 if (token != G_TOKEN_LEFT_CURLY)
3069 token = G_TOKEN_LEFT_CURLY;
3073 token = g_scanner_peek_next_token (scanner);
3074 while (token != G_TOKEN_RIGHT_CURLY)
3078 case GTK_RC_TOKEN_BG:
3079 token = gtk_rc_parse_bg (scanner, rc_style);
3081 case GTK_RC_TOKEN_FG:
3082 token = gtk_rc_parse_fg (scanner, rc_style);
3084 case GTK_RC_TOKEN_TEXT:
3085 token = gtk_rc_parse_text (scanner, rc_style);
3087 case GTK_RC_TOKEN_BASE:
3088 token = gtk_rc_parse_base (scanner, rc_style);
3090 case GTK_RC_TOKEN_XTHICKNESS:
3091 token = gtk_rc_parse_xthickness (scanner, rc_style);
3093 case GTK_RC_TOKEN_YTHICKNESS:
3094 token = gtk_rc_parse_ythickness (scanner, rc_style);
3096 case GTK_RC_TOKEN_BG_PIXMAP:
3097 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3099 case GTK_RC_TOKEN_FONT:
3100 token = gtk_rc_parse_font (scanner, rc_style);
3102 case GTK_RC_TOKEN_FONTSET:
3103 token = gtk_rc_parse_fontset (scanner, rc_style);
3105 case GTK_RC_TOKEN_FONT_NAME:
3106 token = gtk_rc_parse_font_name (scanner, rc_style);
3108 case GTK_RC_TOKEN_ENGINE:
3109 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3111 case GTK_RC_TOKEN_STOCK:
3112 if (our_factory == NULL)
3113 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3114 our_factory = rc_style->icon_factories->data;
3115 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3117 case GTK_RC_TOKEN_COLOR:
3118 if (our_hash == NULL)
3119 gtk_rc_style_prepend_empty_color_hash (rc_style);
3120 our_hash = rc_priv->color_hashes->data;
3121 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3123 case G_TOKEN_IDENTIFIER:
3124 if (is_c_identifier (scanner->next_value.v_identifier) &&
3125 scanner->next_value.v_identifier[0] >= 'A' &&
3126 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
3128 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3131 g_scanner_get_next_token (scanner); /* eat type name */
3132 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3133 if (g_scanner_get_next_token (scanner) != ':' ||
3134 g_scanner_get_next_token (scanner) != ':')
3139 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3140 !is_c_identifier (scanner->value.v_identifier))
3142 token = G_TOKEN_IDENTIFIER;
3146 /* it's important that we do the same canonification as GParamSpecPool here */
3147 name = g_strdup (scanner->value.v_identifier);
3148 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3149 prop.property_name = g_quark_from_string (name);
3152 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3153 if (token == G_TOKEN_NONE)
3155 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3156 insert_rc_property (rc_style, &prop, TRUE);
3159 g_free (prop.origin);
3160 if (G_VALUE_TYPE (&prop.value))
3161 g_value_unset (&prop.value);
3165 g_scanner_get_next_token (scanner);
3166 token = G_TOKEN_IDENTIFIER;
3170 g_scanner_get_next_token (scanner);
3171 token = G_TOKEN_RIGHT_CURLY;
3175 if (token != G_TOKEN_NONE)
3178 token = g_scanner_peek_next_token (scanner);
3179 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3181 token = g_scanner_get_next_token (scanner);
3182 if (token != G_TOKEN_RIGHT_CURLY)
3184 token = G_TOKEN_RIGHT_CURLY;
3188 if (rc_style != orig_style)
3190 if (!context->rc_style_ht)
3191 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3192 (GEqualFunc) gtk_rc_style_equal);
3194 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3196 /* If we copied the data into a new rc style, fix up references to the old rc style
3197 * in bindings that we have.
3200 fixup_rc_sets (context, orig_style, rc_style);
3204 g_object_unref (orig_style);
3206 return G_TOKEN_NONE;
3209 if (rc_style != orig_style)
3210 gtk_rc_style_unref (rc_style);
3213 g_object_unref (orig_style);
3218 const GtkRcProperty*
3219 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3221 GQuark property_name)
3223 GtkRcProperty *node = NULL;
3225 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3227 if (rc_style->rc_properties)
3231 key.type_name = type_name;
3232 key.property_name = property_name;
3234 node = bsearch (&key,
3235 rc_style->rc_properties->data, rc_style->rc_properties->len,
3236 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3243 gtk_rc_parse_bg (GScanner *scanner,
3249 token = g_scanner_get_next_token (scanner);
3250 if (token != GTK_RC_TOKEN_BG)
3251 return GTK_RC_TOKEN_BG;
3253 token = gtk_rc_parse_state (scanner, &state);
3254 if (token != G_TOKEN_NONE)
3257 token = g_scanner_get_next_token (scanner);
3258 if (token != G_TOKEN_EQUAL_SIGN)
3259 return G_TOKEN_EQUAL_SIGN;
3261 style->color_flags[state] |= GTK_RC_BG;
3262 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3266 gtk_rc_parse_fg (GScanner *scanner,
3272 token = g_scanner_get_next_token (scanner);
3273 if (token != GTK_RC_TOKEN_FG)
3274 return GTK_RC_TOKEN_FG;
3276 token = gtk_rc_parse_state (scanner, &state);
3277 if (token != G_TOKEN_NONE)
3280 token = g_scanner_get_next_token (scanner);
3281 if (token != G_TOKEN_EQUAL_SIGN)
3282 return G_TOKEN_EQUAL_SIGN;
3284 style->color_flags[state] |= GTK_RC_FG;
3285 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3289 gtk_rc_parse_text (GScanner *scanner,
3295 token = g_scanner_get_next_token (scanner);
3296 if (token != GTK_RC_TOKEN_TEXT)
3297 return GTK_RC_TOKEN_TEXT;
3299 token = gtk_rc_parse_state (scanner, &state);
3300 if (token != G_TOKEN_NONE)
3303 token = g_scanner_get_next_token (scanner);
3304 if (token != G_TOKEN_EQUAL_SIGN)
3305 return G_TOKEN_EQUAL_SIGN;
3307 style->color_flags[state] |= GTK_RC_TEXT;
3308 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3312 gtk_rc_parse_base (GScanner *scanner,
3318 token = g_scanner_get_next_token (scanner);
3319 if (token != GTK_RC_TOKEN_BASE)
3320 return GTK_RC_TOKEN_BASE;
3322 token = gtk_rc_parse_state (scanner, &state);
3323 if (token != G_TOKEN_NONE)
3326 token = g_scanner_get_next_token (scanner);
3327 if (token != G_TOKEN_EQUAL_SIGN)
3328 return G_TOKEN_EQUAL_SIGN;
3330 style->color_flags[state] |= GTK_RC_BASE;
3331 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3335 gtk_rc_parse_xthickness (GScanner *scanner,
3338 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3339 return GTK_RC_TOKEN_XTHICKNESS;
3341 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3342 return G_TOKEN_EQUAL_SIGN;
3344 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3347 style->xthickness = scanner->value.v_int;
3349 return G_TOKEN_NONE;
3353 gtk_rc_parse_ythickness (GScanner *scanner,
3356 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3357 return GTK_RC_TOKEN_YTHICKNESS;
3359 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3360 return G_TOKEN_EQUAL_SIGN;
3362 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3365 style->ythickness = scanner->value.v_int;
3367 return G_TOKEN_NONE;
3371 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3373 GtkRcStyle *rc_style)
3379 token = g_scanner_get_next_token (scanner);
3380 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3381 return GTK_RC_TOKEN_BG_PIXMAP;
3383 token = gtk_rc_parse_state (scanner, &state);
3384 if (token != G_TOKEN_NONE)
3387 token = g_scanner_get_next_token (scanner);
3388 if (token != G_TOKEN_EQUAL_SIGN)
3389 return G_TOKEN_EQUAL_SIGN;
3391 token = g_scanner_get_next_token (scanner);
3392 if (token != G_TOKEN_STRING)
3393 return G_TOKEN_STRING;
3395 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3396 (strcmp (scanner->value.v_string, "<none>") == 0))
3397 pixmap_file = g_strdup (scanner->value.v_string);
3399 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3400 scanner, scanner->value.v_string);
3404 if (rc_style->bg_pixmap_name[state])
3405 g_free (rc_style->bg_pixmap_name[state]);
3406 rc_style->bg_pixmap_name[state] = pixmap_file;
3409 return G_TOKEN_NONE;
3413 gtk_rc_check_pixmap_dir (const gchar *dir,
3414 const gchar *pixmap_file)
3418 buf = g_build_filename (dir, pixmap_file, NULL);
3420 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3429 * gtk_rc_find_pixmap_in_path:
3430 * @settings: a #GtkSettings
3431 * @scanner: Scanner used to get line number information for the
3432 * warning message, or %NULL
3433 * @pixmap_file: name of the pixmap file to locate.
3435 * Looks up a file in pixmap path for the specified #GtkSettings.
3436 * If the file is not found, it outputs a warning message using
3437 * g_warning() and returns %NULL.
3439 * Return value: the filename.
3442 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3444 const gchar *pixmap_file)
3450 GtkRcContext *context = gtk_rc_context_get (settings);
3452 if (context->pixmap_path)
3453 for (i = 0; context->pixmap_path[i] != NULL; i++)
3455 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3460 tmp_list = current_files_stack;
3463 GtkRcFile *curfile = tmp_list->data;
3464 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3468 tmp_list = tmp_list->next;
3472 g_scanner_warn (scanner,
3473 _("Unable to locate image file in pixmap_path: \"%s\""),
3476 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3483 * gtk_rc_find_module_in_path:
3484 * @module_file: name of a theme engine
3486 * Searches for a theme engine in the GTK+ search path. This function
3487 * is not useful for applications and should not be used.
3489 * Return value: The filename, if found (must be freed with g_free()),
3493 gtk_rc_find_module_in_path (const gchar *module_file)
3495 return _gtk_find_module (module_file, "engines");
3499 gtk_rc_parse_font (GScanner *scanner,
3500 GtkRcStyle *rc_style)
3504 token = g_scanner_get_next_token (scanner);
3505 if (token != GTK_RC_TOKEN_FONT)
3506 return GTK_RC_TOKEN_FONT;
3508 token = g_scanner_get_next_token (scanner);
3509 if (token != G_TOKEN_EQUAL_SIGN)
3510 return G_TOKEN_EQUAL_SIGN;
3512 token = g_scanner_get_next_token (scanner);
3513 if (token != G_TOKEN_STRING)
3514 return G_TOKEN_STRING;
3516 /* Ignore, do nothing */
3518 return G_TOKEN_NONE;
3522 gtk_rc_parse_fontset (GScanner *scanner,
3523 GtkRcStyle *rc_style)
3527 token = g_scanner_get_next_token (scanner);
3528 if (token != GTK_RC_TOKEN_FONTSET)
3529 return GTK_RC_TOKEN_FONTSET;
3531 token = g_scanner_get_next_token (scanner);
3532 if (token != G_TOKEN_EQUAL_SIGN)
3533 return G_TOKEN_EQUAL_SIGN;
3535 token = g_scanner_get_next_token (scanner);
3536 if (token != G_TOKEN_STRING)
3537 return G_TOKEN_STRING;
3539 /* Do nothing - silently ignore */
3541 return G_TOKEN_NONE;
3545 gtk_rc_parse_font_name (GScanner *scanner,
3546 GtkRcStyle *rc_style)
3550 token = g_scanner_get_next_token (scanner);
3551 if (token != GTK_RC_TOKEN_FONT_NAME)
3552 return GTK_RC_TOKEN_FONT;
3554 token = g_scanner_get_next_token (scanner);
3555 if (token != G_TOKEN_EQUAL_SIGN)
3556 return G_TOKEN_EQUAL_SIGN;
3558 token = g_scanner_get_next_token (scanner);
3559 if (token != G_TOKEN_STRING)
3560 return G_TOKEN_STRING;
3562 if (rc_style->font_desc)
3563 pango_font_description_free (rc_style->font_desc);
3565 rc_style->font_desc =
3566 pango_font_description_from_string (scanner->value.v_string);
3568 return G_TOKEN_NONE;
3572 gtk_rc_parse_engine (GtkRcContext *context,
3574 GtkRcStyle **rc_style)
3577 GtkThemeEngine *engine;
3578 guint result = G_TOKEN_NONE;
3579 GtkRcStyle *new_style = NULL;
3580 gboolean parsed_curlies = FALSE;
3582 token = g_scanner_get_next_token (scanner);
3583 if (token != GTK_RC_TOKEN_ENGINE)
3584 return GTK_RC_TOKEN_ENGINE;
3586 token = g_scanner_get_next_token (scanner);
3587 if (token != G_TOKEN_STRING)
3588 return G_TOKEN_STRING;
3590 if (!scanner->value.v_string[0])
3592 /* Support engine "" {} to mean override to the default engine
3594 token = g_scanner_get_next_token (scanner);
3595 if (token != G_TOKEN_LEFT_CURLY)
3596 return G_TOKEN_LEFT_CURLY;
3598 token = g_scanner_get_next_token (scanner);
3599 if (token != G_TOKEN_RIGHT_CURLY)
3600 return G_TOKEN_RIGHT_CURLY;
3602 parsed_curlies = TRUE;
3604 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3606 new_style = gtk_rc_style_new ();
3607 gtk_rc_style_real_merge (new_style, *rc_style);
3609 if ((*rc_style)->name)
3610 new_style->name = g_strdup ((*rc_style)->name);
3613 (*rc_style)->engine_specified = TRUE;
3617 engine = gtk_theme_engine_get (scanner->value.v_string);
3619 token = g_scanner_get_next_token (scanner);
3620 if (token != G_TOKEN_LEFT_CURLY)
3621 return G_TOKEN_LEFT_CURLY;
3625 GtkRcStyleClass *new_class;
3627 new_style = gtk_theme_engine_create_rc_style (engine);
3628 g_type_module_unuse (G_TYPE_MODULE (engine));
3630 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3632 new_class->merge (new_style, *rc_style);
3633 if ((*rc_style)->name)
3634 new_style->name = g_strdup ((*rc_style)->name);
3636 if (new_class->parse)
3638 parsed_curlies = TRUE;
3639 result = new_class->parse (new_style, context->settings, scanner);
3641 if (result != G_TOKEN_NONE)
3643 g_object_unref (new_style);
3650 if (!parsed_curlies)
3652 /* Skip over remainder, looking for nested {}'s
3656 result = G_TOKEN_RIGHT_CURLY;
3657 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3659 if (token == G_TOKEN_LEFT_CURLY)
3661 else if (token == G_TOKEN_RIGHT_CURLY)
3666 result = G_TOKEN_NONE;
3674 GtkRcStylePrivate *rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3675 GtkRcStylePrivate *new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3677 /* take over icon factories and color hashes from the to-be-deleted style
3680 new_style->icon_factories = (*rc_style)->icon_factories;
3681 (*rc_style)->icon_factories = NULL;
3683 new_priv->color_hashes = rc_priv->color_hashes;
3684 rc_priv->color_hashes = NULL;
3686 new_style->engine_specified = TRUE;
3688 g_object_unref (*rc_style);
3689 *rc_style = new_style;
3696 gtk_rc_parse_state (GScanner *scanner,
3697 GtkStateType *state)
3702 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3703 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3705 /* we don't know where we got called from, so we reset the scope here.
3706 * if we bail out due to errors, we *don't* reset the scope, so the
3707 * error messaging code can make sense of our tokens.
3709 old_scope = g_scanner_set_scope (scanner, 0);
3711 token = g_scanner_get_next_token (scanner);
3712 if (token != G_TOKEN_LEFT_BRACE)
3713 return G_TOKEN_LEFT_BRACE;
3715 token = g_scanner_get_next_token (scanner);
3718 case GTK_RC_TOKEN_ACTIVE:
3719 *state = GTK_STATE_ACTIVE;
3721 case GTK_RC_TOKEN_INSENSITIVE:
3722 *state = GTK_STATE_INSENSITIVE;
3724 case GTK_RC_TOKEN_NORMAL:
3725 *state = GTK_STATE_NORMAL;
3727 case GTK_RC_TOKEN_PRELIGHT:
3728 *state = GTK_STATE_PRELIGHT;
3730 case GTK_RC_TOKEN_SELECTED:
3731 *state = GTK_STATE_SELECTED;
3734 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3737 token = g_scanner_get_next_token (scanner);
3738 if (token != G_TOKEN_RIGHT_BRACE)
3739 return G_TOKEN_RIGHT_BRACE;
3741 g_scanner_set_scope (scanner, old_scope);
3743 return G_TOKEN_NONE;
3747 gtk_rc_parse_priority (GScanner *scanner,
3748 GtkPathPriorityType *priority)
3753 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3754 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3756 /* we don't know where we got called from, so we reset the scope here.
3757 * if we bail out due to errors, we *don't* reset the scope, so the
3758 * error messaging code can make sense of our tokens.
3760 old_scope = g_scanner_set_scope (scanner, 0);
3762 token = g_scanner_get_next_token (scanner);
3766 token = g_scanner_get_next_token (scanner);
3769 case GTK_RC_TOKEN_LOWEST:
3770 *priority = GTK_PATH_PRIO_LOWEST;
3772 case GTK_RC_TOKEN_GTK:
3773 *priority = GTK_PATH_PRIO_GTK;
3775 case GTK_RC_TOKEN_APPLICATION:
3776 *priority = GTK_PATH_PRIO_APPLICATION;
3778 case GTK_RC_TOKEN_THEME:
3779 *priority = GTK_PATH_PRIO_THEME;
3781 case GTK_RC_TOKEN_RC:
3782 *priority = GTK_PATH_PRIO_RC;
3784 case GTK_RC_TOKEN_HIGHEST:
3785 *priority = GTK_PATH_PRIO_HIGHEST;
3788 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3791 g_scanner_set_scope (scanner, old_scope);
3793 return G_TOKEN_NONE;
3797 gtk_rc_parse_color (GScanner *scanner,
3800 return gtk_rc_parse_color_full (scanner, NULL, color);
3804 gtk_rc_parse_color_full (GScanner *scanner,
3810 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3812 /* we don't need to set our own scope here, because
3813 * we don't need own symbols
3816 token = g_scanner_get_next_token (scanner);
3824 case G_TOKEN_LEFT_CURLY:
3825 token = g_scanner_get_next_token (scanner);
3826 if (token == G_TOKEN_INT)
3827 token_int = scanner->value.v_int;
3828 else if (token == G_TOKEN_FLOAT)
3829 token_int = scanner->value.v_float * 65535.0;
3831 return G_TOKEN_FLOAT;
3832 color->red = CLAMP (token_int, 0, 65535);
3834 token = g_scanner_get_next_token (scanner);
3835 if (token != G_TOKEN_COMMA)
3836 return G_TOKEN_COMMA;
3838 token = g_scanner_get_next_token (scanner);
3839 if (token == G_TOKEN_INT)
3840 token_int = scanner->value.v_int;
3841 else if (token == G_TOKEN_FLOAT)
3842 token_int = scanner->value.v_float * 65535.0;
3844 return G_TOKEN_FLOAT;
3845 color->green = CLAMP (token_int, 0, 65535);
3847 token = g_scanner_get_next_token (scanner);
3848 if (token != G_TOKEN_COMMA)
3849 return G_TOKEN_COMMA;
3851 token = g_scanner_get_next_token (scanner);
3852 if (token == G_TOKEN_INT)
3853 token_int = scanner->value.v_int;
3854 else if (token == G_TOKEN_FLOAT)
3855 token_int = scanner->value.v_float * 65535.0;
3857 return G_TOKEN_FLOAT;
3858 color->blue = CLAMP (token_int, 0, 65535);
3860 token = g_scanner_get_next_token (scanner);
3861 if (token != G_TOKEN_RIGHT_CURLY)
3862 return G_TOKEN_RIGHT_CURLY;
3863 return G_TOKEN_NONE;
3865 case G_TOKEN_STRING:
3866 if (!gdk_color_parse (scanner->value.v_string, color))
3868 g_scanner_warn (scanner, "Invalid color constant '%s'",
3869 scanner->value.v_string);
3870 return G_TOKEN_STRING;
3872 return G_TOKEN_NONE;
3875 token = g_scanner_get_next_token (scanner);
3876 if (token != G_TOKEN_IDENTIFIER)
3877 return G_TOKEN_IDENTIFIER;
3879 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3881 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3882 scanner->value.v_identifier);
3883 return G_TOKEN_IDENTIFIER;
3886 return G_TOKEN_NONE;
3888 case G_TOKEN_IDENTIFIER:
3889 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3891 token = g_scanner_get_next_token (scanner);
3892 if (token != G_TOKEN_LEFT_PAREN)
3893 return G_TOKEN_LEFT_PAREN;
3896 if (g_scanner_peek_next_token (scanner) == '-')
3898 g_scanner_get_next_token (scanner); /* eat sign */
3902 token = g_scanner_get_next_token (scanner);
3903 if (token != G_TOKEN_FLOAT)
3904 return G_TOKEN_FLOAT;
3906 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3908 token = g_scanner_get_next_token (scanner);
3909 if (token != G_TOKEN_COMMA)
3910 return G_TOKEN_COMMA;
3912 token = gtk_rc_parse_color_full (scanner, style, &c1);
3913 if (token != G_TOKEN_NONE)
3916 token = g_scanner_get_next_token (scanner);
3917 if (token != G_TOKEN_COMMA)
3918 return G_TOKEN_COMMA;
3920 token = gtk_rc_parse_color_full (scanner, style, &c2);
3921 if (token != G_TOKEN_NONE)
3924 token = g_scanner_get_next_token (scanner);
3925 if (token != G_TOKEN_RIGHT_PAREN)
3926 return G_TOKEN_RIGHT_PAREN;
3928 color->red = l * c1.red + (1.0 - l) * c2.red;
3929 color->green = l * c1.green + (1.0 - l) * c2.green;
3930 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
3932 return G_TOKEN_NONE;
3934 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
3936 token = g_scanner_get_next_token (scanner);
3937 if (token != G_TOKEN_LEFT_PAREN)
3938 return G_TOKEN_LEFT_PAREN;
3941 if (g_scanner_peek_next_token (scanner) == '-')
3943 g_scanner_get_next_token (scanner); /* eat sign */
3947 token = g_scanner_get_next_token (scanner);
3948 if (token != G_TOKEN_FLOAT)
3949 return G_TOKEN_FLOAT;
3951 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3953 token = g_scanner_get_next_token (scanner);
3954 if (token != G_TOKEN_COMMA)
3955 return G_TOKEN_COMMA;
3957 token = gtk_rc_parse_color_full (scanner, style, &c1);
3958 if (token != G_TOKEN_NONE)
3961 token = g_scanner_get_next_token (scanner);
3962 if (token != G_TOKEN_RIGHT_PAREN)
3963 return G_TOKEN_RIGHT_PAREN;
3965 _gtk_style_shade (&c1, color, l);
3967 return G_TOKEN_NONE;
3969 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
3970 strcmp (scanner->value.v_identifier, "darker") == 0)
3972 if (scanner->value.v_identifier[0] == 'l')
3977 token = g_scanner_get_next_token (scanner);
3978 if (token != G_TOKEN_LEFT_PAREN)
3979 return G_TOKEN_LEFT_PAREN;
3981 token = gtk_rc_parse_color_full (scanner, style, &c1);
3982 if (token != G_TOKEN_NONE)
3985 token = g_scanner_get_next_token (scanner);
3986 if (token != G_TOKEN_RIGHT_PAREN)
3987 return G_TOKEN_RIGHT_PAREN;
3989 _gtk_style_shade (&c1, color, l);
3991 return G_TOKEN_NONE;
3994 return G_TOKEN_IDENTIFIER;
3997 return G_TOKEN_STRING;
4002 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4007 token = g_scanner_get_next_token (scanner);
4008 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4009 return GTK_RC_TOKEN_PIXMAP_PATH;
4011 token = g_scanner_get_next_token (scanner);
4012 if (token != G_TOKEN_STRING)
4013 return G_TOKEN_STRING;
4015 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4017 return G_TOKEN_NONE;
4021 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4023 const gchar *pix_path)
4025 g_strfreev (context->pixmap_path);
4026 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4030 gtk_rc_parse_module_path (GScanner *scanner)
4034 token = g_scanner_get_next_token (scanner);
4035 if (token != GTK_RC_TOKEN_MODULE_PATH)
4036 return GTK_RC_TOKEN_MODULE_PATH;
4038 token = g_scanner_get_next_token (scanner);
4039 if (token != G_TOKEN_STRING)
4040 return G_TOKEN_STRING;
4042 g_warning ("module_path directive is now ignored\n");
4044 return G_TOKEN_NONE;
4048 gtk_rc_parse_im_module_file (GScanner *scanner)
4052 token = g_scanner_get_next_token (scanner);
4053 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4054 return GTK_RC_TOKEN_IM_MODULE_FILE;
4056 token = g_scanner_get_next_token (scanner);
4057 if (token != G_TOKEN_STRING)
4058 return G_TOKEN_STRING;
4061 g_free (im_module_file);
4063 im_module_file = g_strdup (scanner->value.v_string);
4065 return G_TOKEN_NONE;
4069 gtk_rc_parse_path_pattern (GtkRcContext *context,
4073 GtkPathType path_type;
4075 gboolean is_binding;
4076 GtkPathPriorityType priority = context->default_priority;
4078 token = g_scanner_get_next_token (scanner);
4081 case GTK_RC_TOKEN_WIDGET:
4082 path_type = GTK_PATH_WIDGET;
4084 case GTK_RC_TOKEN_WIDGET_CLASS:
4085 path_type = GTK_PATH_WIDGET_CLASS;
4087 case GTK_RC_TOKEN_CLASS:
4088 path_type = GTK_PATH_CLASS;
4091 return GTK_RC_TOKEN_WIDGET_CLASS;
4094 token = g_scanner_get_next_token (scanner);
4095 if (token != G_TOKEN_STRING)
4096 return G_TOKEN_STRING;
4098 pattern = g_strdup (scanner->value.v_string);
4100 token = g_scanner_get_next_token (scanner);
4101 if (token == GTK_RC_TOKEN_STYLE)
4103 else if (token == GTK_RC_TOKEN_BINDING)
4108 return GTK_RC_TOKEN_STYLE;
4111 if (g_scanner_peek_next_token (scanner) == ':')
4113 token = gtk_rc_parse_priority (scanner, &priority);
4114 if (token != G_TOKEN_NONE)
4121 token = g_scanner_get_next_token (scanner);
4122 if (token != G_TOKEN_STRING)
4125 return G_TOKEN_STRING;
4130 GtkBindingSet *binding;
4132 binding = gtk_binding_set_find (scanner->value.v_string);
4136 return G_TOKEN_STRING;
4138 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4142 GtkRcStyle *rc_style;
4145 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4150 return G_TOKEN_STRING;
4153 rc_set = g_new (GtkRcSet, 1);
4154 rc_set->type = path_type;
4156 if (path_type == GTK_PATH_WIDGET_CLASS)
4158 rc_set->pspec = NULL;
4159 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4163 rc_set->pspec = g_pattern_spec_new (pattern);
4164 rc_set->path = NULL;
4167 rc_set->rc_style = rc_style;
4168 rc_set->priority = priority;
4170 if (path_type == GTK_PATH_WIDGET)
4171 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4172 else if (path_type == GTK_PATH_WIDGET_CLASS)
4173 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4175 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4179 return G_TOKEN_NONE;
4183 gtk_rc_parse_hash_key (GScanner *scanner,
4188 token = g_scanner_get_next_token (scanner);
4189 if (token != G_TOKEN_LEFT_BRACE)
4190 return G_TOKEN_LEFT_BRACE;
4192 token = g_scanner_get_next_token (scanner);
4194 if (token != G_TOKEN_STRING)
4195 return G_TOKEN_STRING;
4197 *hash_key = g_strdup (scanner->value.v_string);
4199 token = g_scanner_get_next_token (scanner);
4200 if (token != G_TOKEN_RIGHT_BRACE)
4203 return G_TOKEN_RIGHT_BRACE;
4206 return G_TOKEN_NONE;
4210 gtk_rc_parse_icon_source (GtkRcContext *context,
4212 GtkIconSet *icon_set,
4213 gboolean *icon_set_valid)
4216 gchar *full_filename;
4217 GtkIconSource *source = NULL;
4219 token = g_scanner_get_next_token (scanner);
4220 if (token != G_TOKEN_LEFT_CURLY)
4221 return G_TOKEN_LEFT_CURLY;
4223 token = g_scanner_get_next_token (scanner);
4225 if (token != G_TOKEN_STRING && token != '@')
4226 return G_TOKEN_STRING;
4228 if (token == G_TOKEN_STRING)
4232 source = gtk_icon_source_new ();
4233 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4236 gtk_icon_source_set_filename (source, full_filename);
4237 g_free (full_filename);
4244 token = g_scanner_get_next_token (scanner);
4246 if (token != G_TOKEN_STRING)
4247 return G_TOKEN_STRING;
4249 source = gtk_icon_source_new ();
4250 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4253 /* We continue parsing even if we didn't find the pixmap so that rest of the
4254 * file is read, even if the syntax is bad. However we don't validate the
4255 * icon_set so the caller can choose not to install it.
4257 token = g_scanner_get_next_token (scanner);
4259 if (token == G_TOKEN_RIGHT_CURLY)
4261 else if (token != G_TOKEN_COMMA)
4263 gtk_icon_source_free (source);
4264 return G_TOKEN_COMMA;
4267 /* Get the direction */
4269 token = g_scanner_get_next_token (scanner);
4273 case GTK_RC_TOKEN_RTL:
4274 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4275 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4278 case GTK_RC_TOKEN_LTR:
4279 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4280 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4287 gtk_icon_source_free (source);
4288 return GTK_RC_TOKEN_RTL;
4292 token = g_scanner_get_next_token (scanner);
4294 if (token == G_TOKEN_RIGHT_CURLY)
4296 else if (token != G_TOKEN_COMMA)
4298 gtk_icon_source_free (source);
4299 return G_TOKEN_COMMA;
4304 token = g_scanner_get_next_token (scanner);
4308 case GTK_RC_TOKEN_NORMAL:
4309 gtk_icon_source_set_state_wildcarded (source, FALSE);
4310 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4313 case GTK_RC_TOKEN_PRELIGHT:
4314 gtk_icon_source_set_state_wildcarded (source, FALSE);
4315 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4319 case GTK_RC_TOKEN_INSENSITIVE:
4320 gtk_icon_source_set_state_wildcarded (source, FALSE);
4321 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4324 case GTK_RC_TOKEN_ACTIVE:
4325 gtk_icon_source_set_state_wildcarded (source, FALSE);
4326 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4329 case GTK_RC_TOKEN_SELECTED:
4330 gtk_icon_source_set_state_wildcarded (source, FALSE);
4331 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4338 gtk_icon_source_free (source);
4339 return GTK_RC_TOKEN_PRELIGHT;
4343 token = g_scanner_get_next_token (scanner);
4345 if (token == G_TOKEN_RIGHT_CURLY)
4347 else if (token != G_TOKEN_COMMA)
4349 gtk_icon_source_free (source);
4350 return G_TOKEN_COMMA;
4355 token = g_scanner_get_next_token (scanner);
4361 if (token != G_TOKEN_STRING)
4363 gtk_icon_source_free (source);
4364 return G_TOKEN_STRING;
4367 size = gtk_icon_size_from_name (scanner->value.v_string);
4369 if (size != GTK_ICON_SIZE_INVALID)
4371 gtk_icon_source_set_size_wildcarded (source, FALSE);
4372 gtk_icon_source_set_size (source, size);
4376 /* Check the close brace */
4378 token = g_scanner_get_next_token (scanner);
4379 if (token != G_TOKEN_RIGHT_CURLY)
4381 gtk_icon_source_free (source);
4382 return G_TOKEN_RIGHT_CURLY;
4386 if (gtk_icon_source_get_filename (source) ||
4387 gtk_icon_source_get_icon_name (source))
4389 gtk_icon_set_add_source (icon_set, source);
4390 *icon_set_valid = TRUE;
4392 gtk_icon_source_free (source);
4394 return G_TOKEN_NONE;
4398 gtk_rc_parse_stock (GtkRcContext *context,
4400 GtkRcStyle *rc_style,
4401 GtkIconFactory *factory)
4403 GtkIconSet *icon_set = NULL;
4404 gboolean icon_set_valid = FALSE;
4405 gchar *stock_id = NULL;
4408 token = g_scanner_get_next_token (scanner);
4409 if (token != GTK_RC_TOKEN_STOCK)
4410 return GTK_RC_TOKEN_STOCK;
4412 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4413 if (token != G_TOKEN_NONE)
4416 token = g_scanner_get_next_token (scanner);
4417 if (token != G_TOKEN_EQUAL_SIGN)
4420 return G_TOKEN_EQUAL_SIGN;
4423 token = g_scanner_get_next_token (scanner);
4424 if (token != G_TOKEN_LEFT_CURLY)
4427 return G_TOKEN_LEFT_CURLY;
4430 token = g_scanner_peek_next_token (scanner);
4431 while (token != G_TOKEN_RIGHT_CURLY)
4433 if (icon_set == NULL)
4434 icon_set = gtk_icon_set_new ();
4436 token = gtk_rc_parse_icon_source (context,
4437 scanner, icon_set, &icon_set_valid);
4438 if (token != G_TOKEN_NONE)
4441 gtk_icon_set_unref (icon_set);
4445 token = g_scanner_get_next_token (scanner);
4447 if (token != G_TOKEN_COMMA &&
4448 token != G_TOKEN_RIGHT_CURLY)
4451 gtk_icon_set_unref (icon_set);
4452 return G_TOKEN_RIGHT_CURLY;
4459 gtk_icon_factory_add (factory,
4463 gtk_icon_set_unref (icon_set);
4468 return G_TOKEN_NONE;
4472 gtk_rc_parse_logical_color (GScanner *scanner,
4473 GtkRcStyle *rc_style,
4476 gchar *color_id = NULL;
4480 token = g_scanner_get_next_token (scanner);
4481 if (token != GTK_RC_TOKEN_COLOR)
4482 return GTK_RC_TOKEN_COLOR;
4484 token = gtk_rc_parse_hash_key (scanner, &color_id);
4485 if (token != G_TOKEN_NONE)
4488 token = g_scanner_get_next_token (scanner);
4489 if (token != G_TOKEN_EQUAL_SIGN)
4492 return G_TOKEN_EQUAL_SIGN;
4495 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4496 if (token != G_TOKEN_NONE)
4502 /* Because the hash is created with destroy functions,
4503 * g_hash_table_insert will free any old values for us,
4504 * if a mapping with the specified key already exists.
4506 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4508 return G_TOKEN_NONE;
4513 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4517 const gchar *current;
4518 const gchar *class_start;
4519 const gchar *class_end;
4520 const gchar *pattern_end;
4521 const gchar *pattern_start;
4526 while ((class_start = strchr (current, '<')) &&
4527 (class_end = strchr (class_start, '>')))
4529 /* Add patterns, but ignore single dots */
4530 if (!(class_start == current ||
4531 (class_start == current + 1 && current[0] == '.')))
4533 pattern_end = class_start - 1;
4534 pattern_start = current;
4536 path_elt = g_new (PathElt, 1);
4538 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4539 path_elt->type = PATH_ELT_PSPEC;
4540 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4541 g_free (sub_pattern);
4543 result = g_slist_prepend (result, path_elt);
4546 path_elt = g_new (PathElt, 1);
4548 /* The < > need to be removed from the string. */
4549 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4551 path_elt->type = PATH_ELT_UNRESOLVED;
4552 path_elt->elt.class_name = sub_pattern;
4554 result = g_slist_prepend (result, path_elt);
4556 current = class_end + 1;
4559 /* Add the rest, if anything is left */
4560 if (strlen (current) > 0)
4562 path_elt = g_new (PathElt, 1);
4563 path_elt->type = PATH_ELT_PSPEC;
4564 path_elt->elt.pspec = g_pattern_spec_new (current);
4566 result = g_slist_prepend (result, path_elt);
4569 return g_slist_reverse (result);
4573 free_path_elt (gpointer data,
4576 PathElt *path_elt = data;
4578 switch (path_elt->type)
4580 case PATH_ELT_PSPEC:
4581 g_pattern_spec_free (path_elt->elt.pspec);
4583 case PATH_ELT_UNRESOLVED:
4584 g_free (path_elt->elt.class_name);
4589 g_assert_not_reached ();
4596 _gtk_rc_free_widget_class_path (GSList *list)
4598 g_slist_foreach (list, free_path_elt, NULL);
4599 g_slist_free (list);
4603 gtk_rc_set_free (GtkRcSet *rc_set)
4606 g_pattern_spec_free (rc_set->pspec);
4608 _gtk_rc_free_widget_class_path (rc_set->path);
4614 match_class (PathElt *path_elt,
4619 if (path_elt->type == PATH_ELT_UNRESOLVED)
4621 type = g_type_from_name (path_elt->elt.class_name);
4622 if (type != G_TYPE_INVALID)
4624 g_free (path_elt->elt.class_name);
4625 path_elt->elt.class_type = type;
4626 path_elt->type = PATH_ELT_TYPE;
4629 return g_str_equal (type_name, path_elt->elt.class_name);
4632 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4636 match_widget_class_recursive (GSList *list,
4639 gchar *path_reversed)
4643 /* break out if we cannot match anymore. */
4652 /* there are two possibilities:
4653 * 1. The next pattern should match the class.
4654 * 2. First normal matching, and then maybe a class */
4656 path_elt = list->data;
4658 if (path_elt->type != PATH_ELT_PSPEC)
4660 gchar *class_start = path;
4663 /* ignore leading dot */
4664 if (class_start[0] == '.')
4666 class_end = strchr (class_start, '.');
4668 if (class_end == NULL)
4670 if (!match_class (path_elt, class_start))
4673 return match_widget_class_recursive (list->next, 0, "", "");
4677 class_end[0] = '\0';
4678 if (!match_class (path_elt, class_start))
4686 gint new_length = length - (class_end - path);
4687 gchar old_char = path_reversed[new_length];
4691 path_reversed[new_length] = '\0';
4692 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4693 path_reversed[new_length] = old_char;
4704 gboolean result = FALSE;
4706 /* If there is nothing after this (ie. no class match),
4707 * just compare the pspec.
4709 if (list->next == NULL)
4710 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4712 class_elt = (PathElt *)list->next->data;
4713 g_assert (class_elt->type != PATH_ELT_PSPEC);
4716 if (class_start[0] == '.')
4721 class_end = strchr (class_start, '.');
4723 /* It should be cheaper to match the class first. (either the pattern
4724 * is simple, and will match most of the times, or it may be complex
4725 * and matching is slow)
4727 if (class_end == NULL)
4729 result = match_class (class_elt, class_start);
4733 class_end[0] = '\0';
4734 result = match_class (class_elt, class_start);
4743 /* terminate the string in front of the class. It does not matter
4744 * that the class becomes unusable, because it is not needed
4745 * inside the recursion
4747 old_char = class_start[0];
4748 class_start[0] = '\0';
4750 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4752 if (class_end != NULL)
4754 gint new_length = length - (class_end - path);
4755 gchar path_reversed_char = path_reversed[new_length];
4757 path_reversed[new_length] = '\0';
4759 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4761 path_reversed[new_length] = path_reversed_char;
4764 result = match_widget_class_recursive (list->next->next, 0, "", "");
4767 class_start[0] = old_char;
4773 /* get next class in path, or break out */
4774 if (class_end != NULL)
4775 class_start = class_end + 1;
4783 _gtk_rc_match_widget_class (GSList *list,
4786 gchar *path_reversed)
4788 return match_widget_class_recursive (list, length, path, path_reversed);
4793 /* DLL ABI stability backward compatibility versions */
4795 #undef gtk_rc_add_default_file
4798 gtk_rc_add_default_file (const gchar *filename)
4800 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4802 gtk_rc_add_default_file_utf8 (utf8_filename);
4804 g_free (utf8_filename);
4807 #undef gtk_rc_set_default_files
4810 gtk_rc_set_default_files (gchar **filenames)
4812 gchar **utf8_filenames;
4815 while (filenames[n++] != NULL)
4818 utf8_filenames = g_new (gchar *, n + 1);
4820 for (i = 0; i < n; i++)
4821 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4823 utf8_filenames[n] = NULL;
4825 gtk_rc_set_default_files_utf8 (utf8_filenames);
4827 g_strfreev (utf8_filenames);
4833 gtk_rc_parse (const gchar *filename)
4835 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4837 gtk_rc_parse_utf8 (utf8_filename);
4839 g_free (utf8_filename);
4844 #define __GTK_RC_C__
4845 #include "gtkaliasdef.c"