1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
44 #include <glib/gstdio.h>
45 #include "gdkconfig.h"
47 #include "gtkversion.h"
49 #include "gtkbindings.h"
50 #include "gtkthemes.h"
52 #include "gtkiconfactory.h"
54 #include "gtkmodules.h"
55 #include "gtkprivate.h"
56 #include "gtksettings.h"
57 #include "gtkwindow.h"
65 typedef struct _GtkRcSet GtkRcSet;
66 typedef struct _GtkRcNode GtkRcNode;
67 typedef struct _GtkRcFile GtkRcFile;
102 gchar *canonical_name;
105 guint is_string : 1; /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
111 GHashTable *rc_style_ht;
112 GtkSettings *settings;
113 GSList *rc_sets_widget;
114 GSList *rc_sets_widget_class;
115 GSList *rc_sets_class;
117 /* The files we have parsed, to reread later if necessary */
121 gchar *key_theme_name;
126 gint default_priority;
127 GtkStyle *default_style;
129 GHashTable *color_hash;
134 #define GTK_RC_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RC_STYLE, GtkRcStylePrivate))
136 typedef struct _GtkRcStylePrivate GtkRcStylePrivate;
138 struct _GtkRcStylePrivate
140 GSList *color_hashes;
143 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
145 static guint gtk_rc_style_hash (const gchar *name);
146 static gboolean gtk_rc_style_equal (const gchar *a,
148 static guint gtk_rc_styles_hash (const GSList *rc_styles);
149 static gboolean gtk_rc_styles_equal (const GSList *a,
151 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
153 static GSList * gtk_rc_styles_match (GSList *rc_styles,
157 gchar *path_reversed);
158 static GtkStyle * gtk_rc_style_to_style (GtkRcContext *context,
159 GtkRcStyle *rc_style);
160 static GtkStyle* gtk_rc_init_style (GtkRcContext *context,
162 static void gtk_rc_parse_default_files (GtkRcContext *context);
163 static void gtk_rc_parse_named (GtkRcContext *context,
166 static void gtk_rc_context_parse_file (GtkRcContext *context,
167 const gchar *filename,
170 static void gtk_rc_parse_any (GtkRcContext *context,
171 const gchar *input_name,
173 const gchar *input_string);
174 static guint gtk_rc_parse_statement (GtkRcContext *context,
176 static guint gtk_rc_parse_style (GtkRcContext *context,
178 static guint gtk_rc_parse_assignment (GScanner *scanner,
180 GtkRcProperty *prop);
181 static guint gtk_rc_parse_bg (GScanner *scanner,
183 static guint gtk_rc_parse_fg (GScanner *scanner,
185 static guint gtk_rc_parse_text (GScanner *scanner,
187 static guint gtk_rc_parse_base (GScanner *scanner,
189 static guint gtk_rc_parse_xthickness (GScanner *scanner,
191 static guint gtk_rc_parse_ythickness (GScanner *scanner,
193 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
195 GtkRcStyle *rc_style);
196 static guint gtk_rc_parse_font (GScanner *scanner,
197 GtkRcStyle *rc_style);
198 static guint gtk_rc_parse_fontset (GScanner *scanner,
199 GtkRcStyle *rc_style);
200 static guint gtk_rc_parse_font_name (GScanner *scanner,
201 GtkRcStyle *rc_style);
202 static guint gtk_rc_parse_engine (GtkRcContext *context,
204 GtkRcStyle **rc_style);
205 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
207 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
209 const gchar *pix_path);
210 static guint gtk_rc_parse_module_path (GScanner *scanner);
211 static guint gtk_rc_parse_im_module_file (GScanner *scanner);
212 static guint gtk_rc_parse_path_pattern (GtkRcContext *context,
214 static guint gtk_rc_parse_stock (GtkRcContext *context,
216 GtkRcStyle *rc_style,
217 GtkIconFactory *factory);
218 static guint gtk_rc_parse_logical_color (GScanner *scanner,
219 GtkRcStyle *rc_style,
222 static void gtk_rc_clear_hash_node (gpointer key,
225 static void gtk_rc_clear_styles (GtkRcContext *context);
226 static void gtk_rc_add_initial_default_files (void);
228 static void gtk_rc_style_finalize (GObject *object);
229 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
231 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
232 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
233 static void gtk_rc_style_copy_icons_and_colors(GtkRcStyle *rc_style,
234 GtkRcStyle *src_style,
235 GtkRcContext *context);
236 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
237 gconstpointer bsearch_node2);
238 static void gtk_rc_set_free (GtkRcSet *rc_set);
240 static void insert_rc_property (GtkRcStyle *style,
241 GtkRcProperty *property,
245 static const GScannerConfig gtk_rc_scanner_config =
249 ) /* cset_skip_characters */,
254 ) /* cset_identifier_first */,
260 ) /* cset_identifier_nth */,
261 ( "#\n" ) /* cpair_comment_single */,
263 TRUE /* case_sensitive */,
265 TRUE /* skip_comment_multi */,
266 TRUE /* skip_comment_single */,
267 TRUE /* scan_comment_multi */,
268 TRUE /* scan_identifier */,
269 FALSE /* scan_identifier_1char */,
270 FALSE /* scan_identifier_NULL */,
271 TRUE /* scan_symbols */,
272 TRUE /* scan_binary */,
273 TRUE /* scan_octal */,
274 TRUE /* scan_float */,
276 TRUE /* scan_hex_dollar */,
277 TRUE /* scan_string_sq */,
278 TRUE /* scan_string_dq */,
279 TRUE /* numbers_2_int */,
280 FALSE /* int_2_float */,
281 FALSE /* identifier_2_string */,
282 TRUE /* char_2_token */,
283 TRUE /* symbol_2_token */,
284 FALSE /* scope_0_fallback */,
287 static const gchar symbol_names[] =
331 { 0, GTK_RC_TOKEN_INCLUDE },
332 { 8, GTK_RC_TOKEN_NORMAL },
333 { 15, GTK_RC_TOKEN_ACTIVE },
334 { 22, GTK_RC_TOKEN_PRELIGHT },
335 { 31, GTK_RC_TOKEN_SELECTED },
336 { 40, GTK_RC_TOKEN_INSENSITIVE },
337 { 52, GTK_RC_TOKEN_FG },
338 { 55, GTK_RC_TOKEN_BG },
339 { 58, GTK_RC_TOKEN_TEXT },
340 { 63, GTK_RC_TOKEN_BASE },
341 { 68, GTK_RC_TOKEN_XTHICKNESS },
342 { 79, GTK_RC_TOKEN_YTHICKNESS },
343 { 90, GTK_RC_TOKEN_FONT },
344 { 95, GTK_RC_TOKEN_FONTSET },
345 { 103, GTK_RC_TOKEN_FONT_NAME },
346 { 113, GTK_RC_TOKEN_BG_PIXMAP },
347 { 123, GTK_RC_TOKEN_PIXMAP_PATH },
348 { 135, GTK_RC_TOKEN_STYLE },
349 { 141, GTK_RC_TOKEN_BINDING },
350 { 149, GTK_RC_TOKEN_BIND },
351 { 154, GTK_RC_TOKEN_WIDGET },
352 { 161, GTK_RC_TOKEN_WIDGET_CLASS },
353 { 174, GTK_RC_TOKEN_CLASS },
354 { 180, GTK_RC_TOKEN_LOWEST },
355 { 187, GTK_RC_TOKEN_GTK },
356 { 191, GTK_RC_TOKEN_APPLICATION },
357 { 203, GTK_RC_TOKEN_THEME },
358 { 209, GTK_RC_TOKEN_RC },
359 { 212, GTK_RC_TOKEN_HIGHEST },
360 { 220, GTK_RC_TOKEN_ENGINE },
361 { 227, GTK_RC_TOKEN_MODULE_PATH },
362 { 239, GTK_RC_TOKEN_STOCK },
363 { 245, GTK_RC_TOKEN_IM_MODULE_FILE },
364 { 260, GTK_RC_TOKEN_LTR },
365 { 264, GTK_RC_TOKEN_RTL },
366 { 268, GTK_RC_TOKEN_COLOR },
367 { 274, GTK_RC_TOKEN_UNBIND }
370 static GHashTable *realized_style_ht = NULL;
372 static gchar *im_module_file = NULL;
374 static gint max_default_files = 0;
375 static gchar **gtk_rc_default_files = NULL;
377 /* A stack of information of RC files we are parsing currently.
378 * The directories for these files are implicitely added to the end of
381 static GSList *current_files_stack = NULL;
383 /* RC files and strings that are parsed for every context
385 static GSList *global_rc_files = NULL;
387 /* Keep list of all current RC contexts for convenience
389 static GSList *rc_contexts;
391 /* RC file handling */
394 gtk_rc_make_default_dir (const gchar *type)
399 var = g_getenv ("GTK_EXE_PREFIX");
402 path = g_build_filename (var, "lib", "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
404 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
410 * gtk_rc_get_im_module_path:
411 * @returns: a newly-allocated string containing the path in which to
412 * look for IM modules.
414 * Obtains the path in which to look for IM modules. See the documentation
415 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
416 * environment variable for more details about looking up modules. This
417 * function is useful solely for utilities supplied with GTK+ and should
418 * not be used by applications under normal circumstances.
421 gtk_rc_get_im_module_path (void)
423 gchar **paths = _gtk_get_module_path ("immodules");
424 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
431 * gtk_rc_get_im_module_file:
432 * @returns: a newly-allocated string containing the name of the file
433 * listing the IM modules available for loading
435 * Obtains the path to the IM modules file. See the documentation
436 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
437 * environment variable for more details.
440 gtk_rc_get_im_module_file (void)
442 const gchar *var = g_getenv ("GTK_IM_MODULE_FILE");
443 gchar *result = NULL;
446 result = g_strdup (var);
451 result = g_strdup (im_module_file);
453 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
460 gtk_rc_get_theme_dir (void)
465 var = g_getenv ("GTK_DATA_PREFIX");
468 path = g_build_filename (var, "share", "themes", NULL);
470 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
476 * gtk_rc_get_module_dir:
478 * Returns a directory in which GTK+ looks for theme engines.
479 * For full information about the search for theme engines,
480 * see the docs for <envar>GTK_PATH</envar> in
481 * <xref linkend="gtk-running"/>.
483 * return value: the directory. (Must be freed with g_free())
486 gtk_rc_get_module_dir (void)
488 return gtk_rc_make_default_dir ("engines");
492 gtk_rc_add_initial_default_files (void)
494 static gint init = FALSE;
503 gtk_rc_default_files = g_new (gchar*, 10);
504 max_default_files = 10;
506 gtk_rc_default_files[0] = NULL;
509 var = g_getenv ("GTK2_RC_FILES");
513 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, -1);
517 gtk_rc_add_default_file (files[i]);
525 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
527 gtk_rc_add_default_file (str);
530 home = g_get_home_dir ();
533 str = g_build_filename (home, ".gtkrc-2.0", NULL);
534 gtk_rc_add_default_file (str);
541 * gtk_rc_add_default_file:
542 * @filename: the pathname to the file. If @filename is not absolute, it
543 * is searched in the current directory.
545 * Adds a file to the list of files to be parsed at the
549 gtk_rc_add_default_file (const gchar *filename)
553 gtk_rc_add_initial_default_files ();
555 for (n = 0; n < max_default_files; n++)
557 if (gtk_rc_default_files[n] == NULL)
561 if (n == max_default_files)
563 max_default_files += 10;
564 gtk_rc_default_files = g_renew (gchar*, gtk_rc_default_files, max_default_files);
567 gtk_rc_default_files[n++] = g_strdup (filename);
568 gtk_rc_default_files[n] = NULL;
572 * gtk_rc_set_default_files:
573 * @filenames: A %NULL-terminated list of filenames.
575 * Sets the list of files that GTK+ will read at the
579 gtk_rc_set_default_files (gchar **filenames)
583 gtk_rc_add_initial_default_files ();
586 while (gtk_rc_default_files[i])
588 g_free (gtk_rc_default_files[i]);
592 gtk_rc_default_files[0] = NULL;
595 while (filenames[i] != NULL)
597 gtk_rc_add_default_file (filenames[i]);
603 * gtk_rc_get_default_files:
605 * Retrieves the current list of RC files that will be parsed
606 * at the end of gtk_init().
608 * Return value: A %NULL-terminated array of filenames. This memory
609 * is owned by GTK+ and must not be freed by the application.
610 * If you want to store this information, you should make a copy.
613 gtk_rc_get_default_files (void)
615 gtk_rc_add_initial_default_files ();
617 return gtk_rc_default_files;
621 gtk_rc_settings_changed (GtkSettings *settings,
623 GtkRcContext *context)
625 gchar *new_theme_name;
626 gchar *new_key_theme_name;
628 if (context->reloading)
631 g_object_get (settings,
632 "gtk-theme-name", &new_theme_name,
633 "gtk-key-theme-name", &new_key_theme_name,
636 if ((new_theme_name != context->theme_name &&
637 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
638 (new_key_theme_name != context->key_theme_name &&
639 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
641 gtk_rc_reparse_all_for_settings (settings, TRUE);
644 g_free (new_theme_name);
645 g_free (new_key_theme_name);
649 gtk_rc_font_name_changed (GtkSettings *settings,
651 GtkRcContext *context)
653 if (!context->reloading)
654 _gtk_rc_context_get_default_font_name (settings);
658 gtk_rc_color_hash_changed (GtkSettings *settings,
660 GtkRcContext *context)
662 if (context->color_hash)
663 g_hash_table_unref (context->color_hash);
665 g_object_get (settings, "color-hash", &context->color_hash, NULL);
667 gtk_rc_reparse_all_for_settings (settings, TRUE);
670 static GtkRcContext *
671 gtk_rc_context_get (GtkSettings *settings)
673 if (!settings->rc_context)
675 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
677 context->settings = settings;
678 context->rc_style_ht = NULL;
679 context->rc_sets_widget = NULL;
680 context->rc_sets_widget_class = NULL;
681 context->rc_sets_class = NULL;
682 context->rc_files = NULL;
683 context->default_style = NULL;
684 context->reloading = FALSE;
686 g_object_get (settings,
687 "gtk-theme-name", &context->theme_name,
688 "gtk-key-theme-name", &context->key_theme_name,
689 "gtk-font-name", &context->font_name,
690 "color-hash", &context->color_hash,
693 g_signal_connect (settings,
694 "notify::gtk-theme-name",
695 G_CALLBACK (gtk_rc_settings_changed),
697 g_signal_connect (settings,
698 "notify::gtk-key-theme-name",
699 G_CALLBACK (gtk_rc_settings_changed),
701 g_signal_connect (settings,
702 "notify::gtk-font-name",
703 G_CALLBACK (gtk_rc_font_name_changed),
705 g_signal_connect (settings,
706 "notify::color-hash",
707 G_CALLBACK (gtk_rc_color_hash_changed),
710 context->pixmap_path = NULL;
712 context->default_priority = GTK_PATH_PRIO_RC;
714 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
717 return settings->rc_context;
721 gtk_rc_clear_rc_files (GtkRcContext *context)
725 list = context->rc_files;
728 GtkRcFile *rc_file = list->data;
730 if (rc_file->canonical_name != rc_file->name)
731 g_free (rc_file->canonical_name);
732 g_free (rc_file->directory);
733 g_free (rc_file->name);
739 g_slist_free (context->rc_files);
740 context->rc_files = NULL;
744 _gtk_rc_context_destroy (GtkSettings *settings)
746 GtkRcContext *context;
748 g_return_if_fail (GTK_IS_SETTINGS (settings));
750 context = settings->rc_context;
754 _gtk_settings_reset_rc_values (context->settings);
755 gtk_rc_clear_styles (context);
756 gtk_rc_clear_rc_files (context);
758 if (context->default_style)
759 g_object_unref (context->default_style);
761 g_strfreev (context->pixmap_path);
763 g_free (context->theme_name);
764 g_free (context->key_theme_name);
765 g_free (context->font_name);
767 if (context->color_hash)
768 g_hash_table_unref (context->color_hash);
770 g_signal_handlers_disconnect_by_func (settings,
771 gtk_rc_settings_changed, context);
772 g_signal_handlers_disconnect_by_func (settings,
773 gtk_rc_font_name_changed, context);
774 g_signal_handlers_disconnect_by_func (settings,
775 gtk_rc_color_hash_changed, context);
777 rc_contexts = g_slist_remove (rc_contexts, context);
781 settings->rc_context = NULL;
785 gtk_rc_parse_named (GtkRcContext *context,
790 const gchar *home_dir;
794 subpath = g_strconcat ("gtk-2.0-", type,
795 G_DIR_SEPARATOR_S "gtkrc",
798 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
800 /* First look in the users home directory
802 home_dir = g_get_home_dir ();
805 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
806 if (!g_file_test (path, G_FILE_TEST_EXISTS))
815 gchar *theme_dir = gtk_rc_get_theme_dir ();
816 path = g_build_filename (theme_dir, name, subpath, NULL);
819 if (!g_file_test (path, G_FILE_TEST_EXISTS))
828 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
836 gtk_rc_parse_default_files (GtkRcContext *context)
840 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
841 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
847 static gboolean initialized = FALSE;
853 gtk_rc_add_initial_default_files ();
856 /* Default RC string */
857 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
858 " bg[NORMAL] = \"#eee1b3\"\n"
859 " fg[NORMAL] = \"#000000\"\n"
862 "style \"gtk-default-progress-bar-style\" {\n"
863 " bg[PRELIGHT] = \"#4b6983\"\n"
864 " fg[PRELIGHT] = \"#ffffff\"\n"
865 " bg[NORMAL] = \"#c4c2bd\"\n"
868 "style \"gtk-default-menu-bar-item-style\" {\n"
869 " GtkMenuItem::horizontal_padding = 5\n"
872 "style \"gtk-default-menu-item-style\" {\n"
873 " bg[PRELIGHT] = \"#4b6983\"\n"
874 " fg[PRELIGHT] = \"#ffffff\"\n"
875 " base[PRELIGHT] = \"#4b6983\"\n"
876 " text[PRELIGHT] = \"#ffffff\"\n"
879 /* Make transparent tray icons work */
880 "style \"gtk-default-tray-icon-style\" {\n"
881 " bg_pixmap[NORMAL] = \"<parent>\"\n"
884 /* Work around clipping of accelerator underlines */
885 "style \"gtk-default-label-style\" {\n"
886 " GtkWidget::draw-border = {0,0,0,1}\n"
889 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
890 "class \"GtkTrayIcon\" style : gtk \"gtk-default-tray-icon-style\"\n"
891 "widget \"gtk-tooltip*\" style : gtk \"gtk-default-tooltips-style\"\n"
892 "widget_class \"*<GtkMenuItem>*\" style : gtk \"gtk-default-menu-item-style\"\n"
893 "widget_class \"*<GtkMenuBar>*<GtkMenuItem>\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
894 "class \"GtkLabel\" style : gtk \"gtk-default-label-style\"\n"
899 gtk_rc_context_parse_string (GtkRcContext *context,
900 const gchar *rc_string)
902 gtk_rc_parse_any (context, "-", -1, rc_string);
906 gtk_rc_parse_string (const gchar *rc_string)
911 g_return_if_fail (rc_string != NULL);
913 rc_file = g_new (GtkRcFile, 1);
914 rc_file->is_string = TRUE;
915 rc_file->name = g_strdup (rc_string);
916 rc_file->canonical_name = NULL;
917 rc_file->directory = NULL;
919 rc_file->reload = TRUE;
921 global_rc_files = g_slist_append (global_rc_files, rc_file);
923 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
924 gtk_rc_context_parse_string (tmp_list->data, rc_string);
928 add_to_rc_file_list (GSList **rc_file_list,
929 const char *filename,
935 tmp_list = *rc_file_list;
938 rc_file = tmp_list->data;
939 if (!strcmp (rc_file->name, filename))
942 tmp_list = tmp_list->next;
945 rc_file = g_new (GtkRcFile, 1);
946 rc_file->is_string = FALSE;
947 rc_file->name = g_strdup (filename);
948 rc_file->canonical_name = NULL;
949 rc_file->directory = NULL;
951 rc_file->reload = reload;
953 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
959 gtk_rc_context_parse_one_file (GtkRcContext *context,
960 const gchar *filename,
968 g_return_if_fail (filename != NULL);
970 saved_priority = context->default_priority;
971 context->default_priority = priority;
973 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
975 if (!rc_file->canonical_name)
977 /* Get the absolute pathname */
979 if (g_path_is_absolute (rc_file->name))
980 rc_file->canonical_name = rc_file->name;
985 cwd = g_get_current_dir ();
986 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
990 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
993 /* If the file is already being parsed (recursion), do nothing
995 if (g_slist_find (current_files_stack, rc_file))
998 if (!g_lstat (rc_file->canonical_name, &statbuf))
1002 rc_file->mtime = statbuf.st_mtime;
1004 fd = g_open (rc_file->canonical_name, O_RDONLY, 0);
1008 /* Temporarily push information for this file on
1009 * a stack of current files while parsing it.
1011 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
1012 gtk_rc_parse_any (context, filename, fd, NULL);
1013 current_files_stack = g_slist_delete_link (current_files_stack,
1014 current_files_stack);
1020 context->default_priority = saved_priority;
1024 strchr_len (const gchar *str, gint len, char c)
1029 return (gchar *)str;
1038 gtk_rc_context_parse_file (GtkRcContext *context,
1039 const gchar *filename,
1043 gchar *locale_suffixes[2];
1044 gint n_locale_suffixes = 0;
1048 gboolean found = FALSE;
1050 locale = _gtk_get_lc_ctype ();
1052 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
1054 /* Determine locale-specific suffixes for RC files.
1056 length = strlen (locale);
1058 p = strchr (locale, '@');
1060 length = p - locale;
1062 p = strchr_len (locale, length, '.');
1064 length = p - locale;
1066 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1068 p = strchr_len (locale, length, '_');
1071 length = p - locale;
1072 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
1078 gtk_rc_context_parse_one_file (context, filename, priority, reload);
1079 for (j = 0; j < n_locale_suffixes; j++)
1083 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
1084 if (g_file_test (name, G_FILE_TEST_EXISTS))
1086 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
1093 g_free (locale_suffixes[j]);
1098 gtk_rc_parse (const gchar *filename)
1102 g_return_if_fail (filename != NULL);
1104 add_to_rc_file_list (&global_rc_files, filename, TRUE);
1106 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1107 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
1110 /* Handling of RC styles */
1112 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
1115 gtk_rc_style_init (GtkRcStyle *style)
1117 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
1121 style->font_desc = NULL;
1123 for (i = 0; i < 5; i++)
1125 static const GdkColor init_color = { 0, 0, 0, 0, };
1127 style->bg_pixmap_name[i] = NULL;
1128 style->color_flags[i] = 0;
1129 style->fg[i] = init_color;
1130 style->bg[i] = init_color;
1131 style->text[i] = init_color;
1132 style->base[i] = init_color;
1134 style->xthickness = -1;
1135 style->ythickness = -1;
1136 style->rc_properties = NULL;
1138 style->rc_style_lists = NULL;
1139 style->icon_factories = NULL;
1141 priv->color_hashes = NULL;
1145 gtk_rc_style_class_init (GtkRcStyleClass *klass)
1147 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1149 object_class->finalize = gtk_rc_style_finalize;
1151 klass->parse = NULL;
1152 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
1153 klass->merge = gtk_rc_style_real_merge;
1154 klass->create_style = gtk_rc_style_real_create_style;
1156 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
1160 gtk_rc_style_finalize (GObject *object)
1162 GSList *tmp_list1, *tmp_list2;
1163 GtkRcStyle *rc_style;
1164 GtkRcStylePrivate *rc_priv;
1167 rc_style = GTK_RC_STYLE (object);
1168 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1170 g_free (rc_style->name);
1171 if (rc_style->font_desc)
1172 pango_font_description_free (rc_style->font_desc);
1174 for (i = 0; i < 5; i++)
1175 g_free (rc_style->bg_pixmap_name[i]);
1177 /* Now remove all references to this rc_style from
1180 tmp_list1 = rc_style->rc_style_lists;
1183 GSList *rc_styles = tmp_list1->data;
1184 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1185 g_object_unref (style);
1187 /* Remove the list of styles from the other rc_styles
1190 tmp_list2 = rc_styles;
1193 GtkRcStyle *other_style = tmp_list2->data;
1195 if (other_style != rc_style)
1196 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1198 tmp_list2 = tmp_list2->next;
1201 /* And from the hash table itself
1203 g_hash_table_remove (realized_style_ht, rc_styles);
1204 g_slist_free (rc_styles);
1206 tmp_list1 = tmp_list1->next;
1208 g_slist_free (rc_style->rc_style_lists);
1210 if (rc_style->rc_properties)
1214 for (i = 0; i < rc_style->rc_properties->len; i++)
1216 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1218 g_free (node->origin);
1219 g_value_unset (&node->value);
1221 g_array_free (rc_style->rc_properties, TRUE);
1222 rc_style->rc_properties = NULL;
1225 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
1226 g_slist_free (rc_style->icon_factories);
1228 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1229 g_slist_free (rc_priv->color_hashes);
1231 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
1235 gtk_rc_style_new (void)
1239 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1245 * gtk_rc_style_copy:
1246 * @orig: the style to copy
1248 * Makes a copy of the specified #GtkRcStyle. This function
1249 * will correctly copy an RC style that is a member of a class
1250 * derived from #GtkRcStyle.
1252 * Return value: the resulting #GtkRcStyle
1255 gtk_rc_style_copy (GtkRcStyle *orig)
1259 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1261 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1262 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1264 gtk_rc_style_copy_icons_and_colors (style, orig, NULL);
1270 _gtk_rc_style_set_rc_property (GtkRcStyle *rc_style,
1271 GtkRcProperty *property)
1273 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1274 g_return_if_fail (property != NULL);
1276 insert_rc_property (rc_style, property, TRUE);
1280 _gtk_rc_style_unset_rc_property (GtkRcStyle *rc_style,
1282 GQuark property_name)
1284 GtkRcProperty *node;
1286 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1288 node = _gtk_rc_style_lookup_rc_property (rc_style, type_name, property_name);
1292 guint index = node - (GtkRcProperty *) rc_style->rc_properties->data;
1293 g_value_unset (&node->value);
1294 g_free (node->origin);
1295 g_array_remove_index (rc_style->rc_properties, index);
1300 gtk_rc_style_ref (GtkRcStyle *rc_style)
1302 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1304 g_object_ref (rc_style);
1308 gtk_rc_style_unref (GtkRcStyle *rc_style)
1310 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1312 g_object_unref (rc_style);
1316 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1318 return g_object_new (G_OBJECT_TYPE (style), NULL);
1322 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1324 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1326 return priv->color_hashes;
1330 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1331 gconstpointer bsearch_node2)
1333 const GtkRcProperty *prop1 = bsearch_node1;
1334 const GtkRcProperty *prop2 = bsearch_node2;
1336 if (prop1->type_name == prop2->type_name)
1337 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1339 return prop1->type_name < prop2->type_name ? -1 : 1;
1343 insert_rc_property (GtkRcStyle *style,
1344 GtkRcProperty *property,
1348 GtkRcProperty *new_property = NULL;
1349 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1351 key.type_name = property->type_name;
1352 key.property_name = property->property_name;
1354 if (!style->rc_properties)
1355 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1358 while (i < style->rc_properties->len)
1360 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1366 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1368 g_free (new_property->origin);
1369 g_value_unset (&new_property->value);
1371 *new_property = key;
1385 g_array_insert_val (style->rc_properties, i, key);
1386 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1389 new_property->origin = g_strdup (property->origin);
1390 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1391 g_value_copy (&property->value, &new_property->value);
1395 gtk_rc_style_real_merge (GtkRcStyle *dest,
1400 for (i = 0; i < 5; i++)
1402 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1403 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1405 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1406 src->color_flags[i] & GTK_RC_FG)
1408 dest->fg[i] = src->fg[i];
1409 dest->color_flags[i] |= GTK_RC_FG;
1411 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1412 src->color_flags[i] & GTK_RC_BG)
1414 dest->bg[i] = src->bg[i];
1415 dest->color_flags[i] |= GTK_RC_BG;
1417 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1418 src->color_flags[i] & GTK_RC_TEXT)
1420 dest->text[i] = src->text[i];
1421 dest->color_flags[i] |= GTK_RC_TEXT;
1423 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1424 src->color_flags[i] & GTK_RC_BASE)
1426 dest->base[i] = src->base[i];
1427 dest->color_flags[i] |= GTK_RC_BASE;
1431 if (dest->xthickness < 0 && src->xthickness >= 0)
1432 dest->xthickness = src->xthickness;
1433 if (dest->ythickness < 0 && src->ythickness >= 0)
1434 dest->ythickness = src->ythickness;
1438 if (!dest->font_desc)
1439 dest->font_desc = pango_font_description_copy (src->font_desc);
1441 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1444 if (src->rc_properties)
1448 for (i = 0; i < src->rc_properties->len; i++)
1449 insert_rc_property (dest,
1450 &g_array_index (src->rc_properties, GtkRcProperty, i),
1456 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1458 return gtk_style_new ();
1462 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1464 GtkIconFactory *factory = gtk_icon_factory_new ();
1466 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1470 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1472 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1473 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1475 (GDestroyNotify) gdk_color_free);
1477 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1481 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1482 GtkRcStyle *src_style)
1484 GSList *concat = g_slist_copy (src_style->icon_factories);
1486 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1488 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1492 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1493 GtkRcStyle *src_style)
1495 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1496 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1497 GSList *concat = g_slist_copy (src_priv->color_hashes);
1499 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1501 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1505 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1506 GtkRcStyle *src_style,
1507 GtkRcContext *context)
1509 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1513 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1515 /* Append src_style's factories, adding a ref to them */
1516 if (src_style->icon_factories != NULL)
1518 /* Add a factory for ourselves if we have none,
1519 * in case we end up defining more stock icons.
1520 * I see no real way around this; we need to maintain
1521 * the invariant that the first factory in the list
1522 * is always our_factory, the one belonging to us,
1523 * and if we put src_style factories in the list we can't
1524 * do that if the style is reopened.
1526 if (rc_style->icon_factories == NULL)
1527 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1529 gtk_rc_style_append_icon_factories (rc_style, src_style);
1532 /* Also append src_style's color hashes, adding a ref to them */
1533 if (src_priv->color_hashes != NULL)
1535 /* See comment above .. */
1536 if (priv->color_hashes == NULL)
1537 gtk_rc_style_prepend_empty_color_hash (rc_style);
1539 gtk_rc_style_append_color_hashes (rc_style, src_style);
1543 /* if we didn't get color hashes from the src_style, initialize
1544 * the list with the settings' color scheme (if it exists)
1546 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1548 gtk_rc_style_prepend_empty_color_hash (rc_style);
1550 priv->color_hashes = g_slist_append (priv->color_hashes,
1551 g_hash_table_ref (context->color_hash));
1556 gtk_rc_clear_hash_node (gpointer key,
1560 gtk_rc_style_unref (data);
1564 gtk_rc_free_rc_sets (GSList *slist)
1570 rc_set = slist->data;
1571 gtk_rc_set_free (rc_set);
1573 slist = slist->next;
1578 gtk_rc_clear_styles (GtkRcContext *context)
1580 /* Clear out all old rc_styles */
1582 if (context->rc_style_ht)
1584 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1585 g_hash_table_destroy (context->rc_style_ht);
1586 context->rc_style_ht = NULL;
1589 gtk_rc_free_rc_sets (context->rc_sets_widget);
1590 g_slist_free (context->rc_sets_widget);
1591 context->rc_sets_widget = NULL;
1593 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1594 g_slist_free (context->rc_sets_widget_class);
1595 context->rc_sets_widget_class = NULL;
1597 gtk_rc_free_rc_sets (context->rc_sets_class);
1598 g_slist_free (context->rc_sets_class);
1599 context->rc_sets_class = NULL;
1602 /* Reset all our widgets. Also, we have to invalidate cached icons in
1603 * icon sets so they get re-rendered.
1606 gtk_rc_reset_widgets (GtkSettings *settings)
1608 GList *list, *toplevels;
1610 _gtk_icon_set_invalidate_caches ();
1612 toplevels = gtk_window_list_toplevels ();
1613 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1615 for (list = toplevels; list; list = list->next)
1617 if (gtk_widget_get_screen (list->data) == settings->screen)
1619 gtk_widget_reset_rc_styles (list->data);
1622 g_object_unref (list->data);
1624 g_list_free (toplevels);
1628 gtk_rc_clear_realized_style (gpointer key,
1632 GSList *rc_styles = key;
1633 GtkStyle *style = value;
1634 GSList *tmp_list = rc_styles;
1636 g_object_unref (style);
1640 GtkRcStyle *rc_style = tmp_list->data;
1642 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1644 tmp_list = tmp_list->next;
1647 g_slist_free (rc_styles);
1651 * gtk_rc_reset_styles:
1652 * @settings: a #GtkSettings
1654 * This function recomputes the styles for all widgets that use a
1655 * particular #GtkSettings object. (There is one #GtkSettings object
1656 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1657 * when some global parameter has changed that affects the appearance
1658 * of all widgets, because when a widget gets a new style, it will
1659 * both redraw and recompute any cached information about its
1660 * appearance. As an example, it is used when the default font size
1661 * set by the operating system changes. Note that this function
1662 * doesn't affect widgets that have a style set explicitely on them
1663 * with gtk_widget_set_style().
1668 gtk_rc_reset_styles (GtkSettings *settings)
1670 GtkRcContext *context;
1671 gboolean reset = FALSE;
1673 g_return_if_fail (GTK_IS_SETTINGS (settings));
1675 context = gtk_rc_context_get (settings);
1677 if (context->default_style)
1679 g_object_unref (context->default_style);
1680 context->default_style = NULL;
1684 /* Clear out styles that have been looked up already
1686 if (realized_style_ht)
1688 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1689 g_hash_table_destroy (realized_style_ht);
1690 realized_style_ht = NULL;
1695 gtk_rc_reset_widgets (settings);
1699 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1701 GtkRcContext *context;
1702 gchar *new_font_name;
1704 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1706 context = gtk_rc_context_get (settings);
1708 g_object_get (context->settings,
1709 "gtk-font-name", &new_font_name,
1712 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1714 g_free (context->font_name);
1715 context->font_name = g_strdup (new_font_name);
1717 gtk_rc_reset_styles (settings);
1720 g_free (new_font_name);
1722 return context->font_name;
1726 * gtk_rc_reparse_all_for_settings:
1727 * @settings: a #GtkSettings
1728 * @force_load: load whether or not anything changed
1730 * If the modification time on any previously read file
1731 * for the given #GtkSettings has changed, discard all style information
1732 * and then reread all previously read RC files.
1734 * Return value: %TRUE if the files were reread.
1737 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1738 gboolean force_load)
1740 gboolean mtime_modified = FALSE;
1743 GtkRcContext *context;
1744 struct stat statbuf;
1746 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1748 context = gtk_rc_context_get (settings);
1750 if (context->reloading)
1755 /* Check through and see if any of the RC's have had their
1756 * mtime modified. If so, reparse everything.
1758 tmp_list = context->rc_files;
1761 rc_file = tmp_list->data;
1763 if (!rc_file->is_string)
1765 if (!g_lstat (rc_file->name, &statbuf) &&
1766 (statbuf.st_mtime != rc_file->mtime))
1768 mtime_modified = TRUE;
1773 tmp_list = tmp_list->next;
1777 if (force_load || mtime_modified)
1779 _gtk_binding_reset_parsed ();
1780 gtk_rc_clear_styles (context);
1781 context->reloading = TRUE;
1783 _gtk_settings_reset_rc_values (context->settings);
1784 gtk_rc_clear_rc_files (context);
1786 gtk_rc_parse_default_files (context);
1788 tmp_list = global_rc_files;
1791 rc_file = tmp_list->data;
1793 if (rc_file->is_string)
1794 gtk_rc_context_parse_string (context, rc_file->name);
1796 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1798 tmp_list = tmp_list->next;
1801 g_free (context->theme_name);
1802 g_free (context->key_theme_name);
1804 g_object_get (context->settings,
1805 "gtk-theme-name", &context->theme_name,
1806 "gtk-key-theme-name", &context->key_theme_name,
1809 if (context->theme_name && context->theme_name[0])
1810 gtk_rc_parse_named (context, context->theme_name, NULL);
1811 if (context->key_theme_name && context->key_theme_name[0])
1812 gtk_rc_parse_named (context, context->key_theme_name, "key");
1814 context->reloading = FALSE;
1816 gtk_rc_reset_widgets (context->settings);
1819 return force_load || mtime_modified;
1823 * gtk_rc_reparse_all:
1825 * If the modification time on any previously read file for the
1826 * default #GtkSettings has changed, discard all style information
1827 * and then reread all previously read RC files.
1829 * Return value: %TRUE if the files were reread.
1832 gtk_rc_reparse_all (void)
1835 gboolean result = FALSE;
1837 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1839 GtkRcContext *context = tmp_list->data;
1840 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1848 gtk_rc_styles_match (GSList *rc_styles,
1852 gchar *path_reversed)
1859 rc_set = sets->data;
1862 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1864 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1865 rc_styles = g_slist_append (rc_styles, rc_set);
1869 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1870 rc_styles = g_slist_append (rc_styles, rc_set);
1878 rc_set_compare (gconstpointer a, gconstpointer b)
1880 const GtkRcSet *set_a = a;
1881 const GtkRcSet *set_b = b;
1883 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1887 sort_and_dereference_sets (GSList *styles)
1891 /* At this point, the list of sets is ordered by:
1893 * a) 'widget' patterns are earlier than 'widget_class' patterns
1894 * which are ealier than 'class' patterns.
1895 * a) For two matches for class patterns, a match to a child type
1896 * is before a match to a parent type
1897 * c) a match later in the RC file (or in a later RC file) is before a
1898 * match earlier in the RC file.
1900 * With a) taking precedence over b) which takes precendence over c).
1902 * Now sort by priority, which has the highest precendence for sort order
1904 styles = g_slist_sort (styles, rc_set_compare);
1906 /* Make styles->data = styles->data->rc_style
1911 GtkRcSet *set = tmp_list->data;
1912 tmp_list->data = set->rc_style;
1913 tmp_list = tmp_list->next;
1921 * @widget: a #GtkWidget
1923 * Finds all matching RC styles for a given widget,
1924 * composites them together, and then creates a
1925 * #GtkStyle representing the composite appearance.
1926 * (GTK+ actually keeps a cache of previously
1927 * created styles, so a new style may not be
1930 * Returns: the resulting style. No refcount is added
1931 * to the returned style, so if you want to save this
1932 * style around, you should add a reference yourself.
1935 gtk_rc_get_style (GtkWidget *widget)
1937 GtkRcStyle *widget_rc_style;
1938 GSList *rc_styles = NULL;
1939 GtkRcContext *context;
1941 static guint rc_style_key_id = 0;
1943 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1945 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1947 /* We allow the specification of a single rc style to be bound
1948 * tightly to a widget, for application modifications
1950 if (!rc_style_key_id)
1951 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1953 if (context->rc_sets_widget)
1955 gchar *path, *path_reversed;
1958 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1959 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1961 g_free (path_reversed);
1964 if (context->rc_sets_widget_class)
1966 gchar *path, *path_reversed;
1969 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1970 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1972 g_free (path_reversed);
1975 if (context->rc_sets_class)
1979 type = G_TYPE_FROM_INSTANCE (widget);
1983 gchar *path_reversed;
1986 path = g_strdup (g_type_name (type));
1987 path_length = strlen (path);
1988 path_reversed = g_strdup (path);
1989 g_strreverse (path_reversed);
1991 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1993 g_free (path_reversed);
1995 type = g_type_parent (type);
1999 rc_styles = sort_and_dereference_sets (rc_styles);
2001 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
2003 if (widget_rc_style)
2004 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
2007 return gtk_rc_init_style (context, rc_styles);
2010 if (!context->default_style)
2012 context->default_style = gtk_style_new ();
2013 _gtk_style_init_for_settings (context->default_style, context->settings);
2016 return context->default_style;
2021 * gtk_rc_get_style_by_paths:
2022 * @settings: a #GtkSettings object
2023 * @widget_path: the widget path to use when looking up the style, or %NULL
2024 * if no matching against the widget path should be done
2025 * @class_path: the class path to use when looking up the style, or %NULL
2026 * if no matching against the class path should be done.
2027 * @type: a type that will be used along with parent types of this type
2028 * when matching against class styles, or #G_TYPE_NONE
2030 * Creates up a #GtkStyle from styles defined in a RC file by providing
2031 * the raw components used in matching. This function may be useful
2032 * when creating pseudo-widgets that should be themed like widgets but
2033 * don't actually have corresponding GTK+ widgets. An example of this
2034 * would be items inside a GNOME canvas widget.
2036 * The action of gtk_rc_get_style() is similar to:
2037 * <informalexample><programlisting>
2038 * gtk_widget_path (widget, NULL, &path, NULL);
2039 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2040 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
2041 * G_OBJECT_TYPE (widget));
2042 * </programlisting></informalexample>
2044 * Return value: A style created by matching with the supplied paths,
2045 * or %NULL if nothing matching was specified and the default style should
2046 * be used. The returned value is owned by GTK+ as part of an internal cache,
2047 * so you must call g_object_ref() on the returned value if you want to
2048 * keep a reference to it.
2051 gtk_rc_get_style_by_paths (GtkSettings *settings,
2052 const char *widget_path,
2053 const char *class_path,
2056 /* We duplicate the code from above to avoid slowing down the above
2057 * by generating paths when we don't need them. I don't know if
2058 * this is really worth it.
2060 GSList *rc_styles = NULL;
2061 GtkRcContext *context;
2063 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2065 context = gtk_rc_context_get (settings);
2067 if (widget_path && context->rc_sets_widget)
2070 gchar *path_reversed;
2073 path_length = strlen (widget_path);
2074 path = g_strdup (widget_path);
2075 path_reversed = g_strdup (widget_path);
2076 g_strreverse (path_reversed);
2078 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2080 g_free (path_reversed);
2083 if (class_path && context->rc_sets_widget_class)
2086 gchar *path_reversed;
2089 path = g_strdup (class_path);
2090 path_length = strlen (class_path);
2091 path_reversed = g_strdup (class_path);
2092 g_strreverse (path_reversed);
2094 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2096 g_free (path_reversed);
2099 if (type != G_TYPE_NONE && context->rc_sets_class)
2104 gchar *path_reversed;
2107 path = g_strdup (g_type_name (type));
2108 path_length = strlen (path);
2109 path_reversed = g_strdup (path);
2110 g_strreverse (path_reversed);
2112 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2114 g_free (path_reversed);
2116 type = g_type_parent (type);
2120 rc_styles = sort_and_dereference_sets (rc_styles);
2123 return gtk_rc_init_style (context, rc_styles);
2129 gtk_rc_add_rc_sets (GSList *slist,
2130 GtkRcStyle *rc_style,
2131 const gchar *pattern,
2132 GtkPathType path_type)
2134 GtkRcStyle *new_style;
2138 new_style = gtk_rc_style_new ();
2139 *new_style = *rc_style;
2140 new_style->name = g_strdup (rc_style->name);
2141 if (rc_style->font_desc)
2142 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
2144 for (i = 0; i < 5; i++)
2145 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
2147 rc_set = g_new (GtkRcSet, 1);
2148 rc_set->type = path_type;
2150 if (path_type == GTK_PATH_WIDGET_CLASS)
2152 rc_set->pspec = NULL;
2153 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
2157 rc_set->pspec = g_pattern_spec_new (pattern);
2158 rc_set->path = NULL;
2161 rc_set->rc_style = rc_style;
2163 return g_slist_prepend (slist, rc_set);
2167 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
2168 const gchar *pattern)
2170 GtkRcContext *context;
2172 g_return_if_fail (rc_style != NULL);
2173 g_return_if_fail (pattern != NULL);
2175 context = gtk_rc_context_get (gtk_settings_get_default ());
2177 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern, GTK_PATH_WIDGET);
2181 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
2182 const gchar *pattern)
2184 GtkRcContext *context;
2186 g_return_if_fail (rc_style != NULL);
2187 g_return_if_fail (pattern != NULL);
2189 context = gtk_rc_context_get (gtk_settings_get_default ());
2191 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern, GTK_PATH_WIDGET_CLASS);
2195 gtk_rc_add_class_style (GtkRcStyle *rc_style,
2196 const gchar *pattern)
2198 GtkRcContext *context;
2200 g_return_if_fail (rc_style != NULL);
2201 g_return_if_fail (pattern != NULL);
2203 context = gtk_rc_context_get (gtk_settings_get_default ());
2205 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern, GTK_PATH_CLASS);
2209 gtk_rc_scanner_new (void)
2211 return g_scanner_new (>k_rc_scanner_config);
2215 gtk_rc_parse_any (GtkRcContext *context,
2216 const gchar *input_name,
2218 const gchar *input_string)
2224 scanner = gtk_rc_scanner_new ();
2228 g_assert (input_string == NULL);
2230 g_scanner_input_file (scanner, input_fd);
2234 g_assert (input_string != NULL);
2236 g_scanner_input_text (scanner, input_string, strlen (input_string));
2238 scanner->input_name = input_name;
2240 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2241 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2245 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2249 guint expected_token;
2251 expected_token = gtk_rc_parse_statement (context, scanner);
2253 if (expected_token != G_TOKEN_NONE)
2260 if (scanner->scope_id == 0)
2262 /* if we are in scope 0, we know the symbol names
2263 * that are associated with certain token values.
2264 * so we look them up to make the error messages
2267 if (expected_token > GTK_RC_TOKEN_INVALID &&
2268 expected_token < GTK_RC_TOKEN_LAST)
2270 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2271 if (symbols[i].token == expected_token)
2272 msg = symbol_names + symbols[i].name_offset;
2274 msg = g_strconcat ("e.g. `", msg, "'", NULL);
2276 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2277 scanner->token < GTK_RC_TOKEN_LAST)
2279 symbol_name = "???";
2280 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2281 if (symbols[i].token == scanner->token)
2282 symbol_name = symbol_names + symbols[i].name_offset;
2285 g_scanner_unexp_token (scanner,
2298 g_scanner_destroy (scanner);
2302 gtk_rc_styles_hash (const GSList *rc_styles)
2309 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2310 rc_styles = rc_styles->next;
2317 gtk_rc_styles_equal (const GSList *a,
2322 if (a->data != b->data)
2332 gtk_rc_style_hash (const gchar *name)
2338 result += (result << 3) + *name++;
2344 gtk_rc_style_equal (const gchar *a,
2347 return (strcmp (a, b) == 0);
2351 gtk_rc_style_find (GtkRcContext *context,
2354 if (context->rc_style_ht)
2355 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2361 gtk_rc_style_to_style (GtkRcContext *context,
2362 GtkRcStyle *rc_style)
2366 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2367 _gtk_style_init_for_settings (style, context->settings);
2369 style->rc_style = g_object_ref (rc_style);
2371 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2376 /* Reuses or frees rc_styles */
2378 gtk_rc_init_style (GtkRcContext *context,
2381 GtkStyle *style = NULL;
2384 g_return_val_if_fail (rc_styles != NULL, NULL);
2386 if (!realized_style_ht)
2387 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2388 (GEqualFunc) gtk_rc_styles_equal);
2390 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2394 GtkRcStyle *base_style = NULL;
2395 GtkRcStyle *proto_style;
2396 GtkRcStyleClass *proto_style_class;
2398 GType rc_style_type = GTK_TYPE_RC_STYLE;
2400 /* Find the first style where the RC file specified engine "" {}
2401 * or the first derived style and use that to create the
2402 * merged style. If we only have raw GtkRcStyles, use the first
2403 * style to create the merged style.
2405 base_style = rc_styles->data;
2406 tmp_styles = rc_styles;
2409 GtkRcStyle *rc_style = tmp_styles->data;
2411 if (rc_style->engine_specified ||
2412 G_OBJECT_TYPE (rc_style) != rc_style_type)
2414 base_style = rc_style;
2418 tmp_styles = tmp_styles->next;
2421 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2422 proto_style = proto_style_class->create_rc_style (base_style);
2424 tmp_styles = rc_styles;
2427 GtkRcStyle *rc_style = tmp_styles->data;
2429 proto_style_class->merge (proto_style, rc_style);
2431 /* Point from each rc_style to the list of styles */
2432 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2433 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2435 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2436 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2438 tmp_styles = tmp_styles->next;
2441 for (i = 0; i < 5; i++)
2442 if (proto_style->bg_pixmap_name[i] &&
2443 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2445 g_free (proto_style->bg_pixmap_name[i]);
2446 proto_style->bg_pixmap_name[i] = NULL;
2449 style = gtk_rc_style_to_style (context, proto_style);
2450 gtk_rc_style_unref (proto_style);
2452 g_hash_table_insert (realized_style_ht, rc_styles, style);
2455 g_slist_free (rc_styles);
2460 /*********************
2461 * Parsing functions *
2462 *********************/
2465 lookup_color (GtkRcStyle *style,
2466 const char *color_name,
2469 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2472 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2474 GHashTable *hash = iter->data;
2475 GdkColor *match = g_hash_table_lookup (hash, color_name);
2479 color->red = match->red;
2480 color->green = match->green;
2481 color->blue = match->blue;
2490 rc_parse_token_or_compound (GScanner *scanner,
2493 GTokenType delimiter)
2495 guint token = g_scanner_get_next_token (scanner);
2497 /* we either scan a single token (skipping comments)
2498 * or a compund statement.
2499 * compunds are enclosed in (), [] or {} braces, we read
2500 * them in via deep recursion.
2507 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2510 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2512 case G_TOKEN_STRING:
2513 string = g_strescape (scanner->value.v_string, NULL);
2514 g_string_append (gstring, " \"");
2515 g_string_append (gstring, string);
2516 g_string_append_c (gstring, '"');
2519 case G_TOKEN_IDENTIFIER:
2520 g_string_append_c (gstring, ' ');
2521 g_string_append (gstring, scanner->value.v_identifier);
2523 case G_TOKEN_COMMENT_SINGLE:
2524 case G_TOKEN_COMMENT_MULTI:
2525 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2526 case G_TOKEN_LEFT_PAREN:
2527 g_string_append_c (gstring, ' ');
2528 g_string_append_c (gstring, token);
2529 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2530 if (token != G_TOKEN_NONE)
2533 case G_TOKEN_LEFT_CURLY:
2534 g_string_append_c (gstring, ' ');
2535 g_string_append_c (gstring, token);
2536 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2537 if (token != G_TOKEN_NONE)
2540 case G_TOKEN_LEFT_BRACE:
2541 g_string_append_c (gstring, ' ');
2542 g_string_append_c (gstring, token);
2543 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2544 if (token != G_TOKEN_NONE)
2548 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2551 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2552 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2553 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2555 g_scanner_get_next_token (scanner);
2557 if (!style || !lookup_color (style, scanner->value.v_identifier,
2560 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2561 scanner->value.v_identifier);
2562 return G_TOKEN_IDENTIFIER;
2566 g_string_append_printf (gstring, " { %s, %s, %s }",
2567 g_ascii_formatd (rbuf, sizeof (rbuf),
2569 color.red / 65535.0),
2570 g_ascii_formatd (gbuf, sizeof (gbuf),
2572 color.green / 65535.0),
2573 g_ascii_formatd (bbuf, sizeof (bbuf),
2575 color.blue / 65535.0));
2579 return G_TOKEN_IDENTIFIER;
2581 if (token >= 256 || token < 1)
2582 return delimiter ? delimiter : G_TOKEN_STRING;
2583 g_string_append_c (gstring, ' ');
2584 g_string_append_c (gstring, token);
2585 if (token == delimiter)
2586 return G_TOKEN_NONE;
2590 return G_TOKEN_NONE;
2592 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2596 gtk_rc_parse_assignment (GScanner *scanner,
2598 GtkRcProperty *prop)
2600 #define MY_SCAN_IDENTIFIER TRUE
2601 #define MY_SCAN_SYMBOLS FALSE
2602 #define MY_IDENTIFIER_2_STRING FALSE
2603 #define MY_CHAR_2_TOKEN TRUE
2604 #define MY_SCAN_IDENTIFIER_NULL FALSE
2605 #define MY_NUMBERS_2_INT TRUE
2607 gboolean scan_identifier = scanner->config->scan_identifier;
2608 gboolean scan_symbols = scanner->config->scan_symbols;
2609 gboolean identifier_2_string = scanner->config->identifier_2_string;
2610 gboolean char_2_token = scanner->config->char_2_token;
2611 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2612 gboolean numbers_2_int = scanner->config->numbers_2_int;
2613 gboolean negate = FALSE;
2614 gboolean is_color = FALSE;
2617 /* check that this is an assignment */
2618 if (g_scanner_get_next_token (scanner) != '=')
2621 /* adjust scanner mode */
2622 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2623 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2624 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2625 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2626 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2627 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2629 /* record location */
2630 if (g_getenv ("GTK_DEBUG"))
2631 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2633 prop->origin = NULL;
2635 /* parse optional symbolic color prefix */
2636 if (g_scanner_peek_next_token (scanner) == '@')
2638 g_scanner_get_next_token (scanner); /* eat color prefix */
2642 /* parse optional sign */
2643 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2645 g_scanner_get_next_token (scanner); /* eat sign */
2649 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2652 token = g_scanner_peek_next_token (scanner);
2654 if (is_color && token != G_TOKEN_IDENTIFIER)
2656 token = G_TOKEN_IDENTIFIER;
2663 g_scanner_get_next_token (scanner);
2664 g_value_init (&prop->value, G_TYPE_LONG);
2665 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2666 token = G_TOKEN_NONE;
2669 g_scanner_get_next_token (scanner);
2670 g_value_init (&prop->value, G_TYPE_DOUBLE);
2671 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2672 token = G_TOKEN_NONE;
2674 case G_TOKEN_STRING:
2675 g_scanner_get_next_token (scanner);
2677 token = G_TOKEN_INT;
2680 g_value_init (&prop->value, G_TYPE_STRING);
2681 g_value_set_string (&prop->value, scanner->value.v_string);
2682 token = G_TOKEN_NONE;
2685 case G_TOKEN_IDENTIFIER:
2689 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2690 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2691 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2694 g_scanner_get_next_token (scanner);
2696 if (!style || !lookup_color (style, scanner->value.v_identifier,
2699 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2700 scanner->value.v_identifier);
2701 token = G_TOKEN_IDENTIFIER;
2705 gstring = g_string_new (NULL);
2707 g_string_append_printf (gstring, " { %s, %s, %s }",
2708 g_ascii_formatd (rbuf, sizeof (rbuf),
2710 color.red / 65535.0),
2711 g_ascii_formatd (gbuf, sizeof (gbuf),
2713 color.green / 65535.0),
2714 g_ascii_formatd (bbuf, sizeof (bbuf),
2716 color.blue / 65535.0));
2718 g_value_init (&prop->value, G_TYPE_GSTRING);
2719 g_value_take_boxed (&prop->value, gstring);
2720 token = G_TOKEN_NONE;
2724 case G_TOKEN_LEFT_PAREN:
2725 case G_TOKEN_LEFT_CURLY:
2726 case G_TOKEN_LEFT_BRACE:
2729 GString *gstring = g_string_new (NULL);
2730 gboolean parse_on = TRUE;
2732 /* allow identifier(foobar) to support color expressions */
2733 if (token == G_TOKEN_IDENTIFIER)
2735 g_scanner_get_next_token (scanner);
2737 g_string_append_c (gstring, ' ');
2738 g_string_append (gstring, scanner->value.v_identifier);
2740 /* temporarily reset scanner mode to default, so we
2741 * don't peek the next token in a mode that only makes
2742 * sense in this function; because if anything but
2743 * G_TOKEN_LEFT_PAREN follows, the next token will be
2744 * parsed by our caller.
2746 * FIXME: right fix would be to call g_scanner_unget()
2747 * but that doesn't exist
2749 scanner->config->scan_identifier = scan_identifier;
2750 scanner->config->scan_symbols = scan_symbols;
2751 scanner->config->identifier_2_string = identifier_2_string;
2752 scanner->config->char_2_token = char_2_token;
2753 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2754 scanner->config->numbers_2_int = numbers_2_int;
2756 token = g_scanner_peek_next_token (scanner);
2758 /* restore adjusted scanner mode */
2759 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2760 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2761 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2762 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2763 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2764 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2766 if (token != G_TOKEN_LEFT_PAREN)
2768 token = G_TOKEN_NONE;
2774 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2776 if (token == G_TOKEN_NONE)
2778 g_string_append_c (gstring, ' ');
2779 g_value_init (&prop->value, G_TYPE_GSTRING);
2780 g_value_take_boxed (&prop->value, gstring);
2783 g_string_free (gstring, TRUE);
2788 g_scanner_get_next_token (scanner);
2789 token = G_TOKEN_INT;
2795 /* restore scanner mode */
2796 scanner->config->scan_identifier = scan_identifier;
2797 scanner->config->scan_symbols = scan_symbols;
2798 scanner->config->identifier_2_string = identifier_2_string;
2799 scanner->config->char_2_token = char_2_token;
2800 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2801 scanner->config->numbers_2_int = numbers_2_int;
2807 is_c_identifier (const gchar *string)
2810 gboolean is_varname;
2812 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2813 for (p = string + 1; *p && is_varname; p++)
2814 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2820 parse_include_file (GtkRcContext *context,
2822 const gchar *filename)
2824 char *to_parse = NULL;
2826 if (g_path_is_absolute (filename))
2828 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2829 * don't print an error in this case.
2831 to_parse = g_strdup (filename);
2835 /* if a relative path, we look relative to all the RC files in the
2836 * include stack. We require the file to be found in this case
2837 * so we can give meaningful error messages, and because on reparsing
2838 * non-absolute paths don't make sense.
2840 GSList *tmp_list = current_files_stack;
2843 GtkRcFile *curfile = tmp_list->data;
2844 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2846 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2854 tmp_list = tmp_list->next;
2860 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2865 g_scanner_warn (scanner,
2866 _("Unable to find include file: \"%s\""),
2873 gtk_rc_parse_statement (GtkRcContext *context,
2878 token = g_scanner_peek_next_token (scanner);
2881 case GTK_RC_TOKEN_INCLUDE:
2882 token = g_scanner_get_next_token (scanner);
2883 if (token != GTK_RC_TOKEN_INCLUDE)
2884 return GTK_RC_TOKEN_INCLUDE;
2885 token = g_scanner_get_next_token (scanner);
2886 if (token != G_TOKEN_STRING)
2887 return G_TOKEN_STRING;
2888 parse_include_file (context, scanner, scanner->value.v_string);
2889 return G_TOKEN_NONE;
2891 case GTK_RC_TOKEN_STYLE:
2892 return gtk_rc_parse_style (context, scanner);
2894 case GTK_RC_TOKEN_BINDING:
2895 return _gtk_binding_parse_binding (scanner);
2897 case GTK_RC_TOKEN_PIXMAP_PATH:
2898 return gtk_rc_parse_pixmap_path (context, scanner);
2900 case GTK_RC_TOKEN_WIDGET:
2901 return gtk_rc_parse_path_pattern (context, scanner);
2903 case GTK_RC_TOKEN_WIDGET_CLASS:
2904 return gtk_rc_parse_path_pattern (context, scanner);
2906 case GTK_RC_TOKEN_CLASS:
2907 return gtk_rc_parse_path_pattern (context, scanner);
2909 case GTK_RC_TOKEN_MODULE_PATH:
2910 return gtk_rc_parse_module_path (scanner);
2912 case GTK_RC_TOKEN_IM_MODULE_FILE:
2913 return gtk_rc_parse_im_module_file (scanner);
2915 case G_TOKEN_IDENTIFIER:
2916 if (is_c_identifier (scanner->next_value.v_identifier))
2918 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2921 g_scanner_get_next_token (scanner); /* eat identifier */
2922 name = g_strdup (scanner->value.v_identifier);
2924 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2925 if (token == G_TOKEN_NONE)
2927 GtkSettingsValue svalue;
2929 svalue.origin = prop.origin;
2930 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2931 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2932 _gtk_settings_set_property_value_from_rc (context->settings,
2936 g_free (prop.origin);
2937 if (G_VALUE_TYPE (&prop.value))
2938 g_value_unset (&prop.value);
2945 g_scanner_get_next_token (scanner);
2946 return G_TOKEN_IDENTIFIER;
2949 g_scanner_get_next_token (scanner);
2950 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2955 fixup_rc_set (GSList *list,
2961 GtkRcSet *set = list->data;
2962 if (set->rc_style == orig)
2963 set->rc_style = new;
2969 fixup_rc_sets (GtkRcContext *context,
2973 fixup_rc_set (context->rc_sets_widget, orig, new);
2974 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2975 fixup_rc_set (context->rc_sets_class, orig, new);
2979 gtk_rc_parse_style (GtkRcContext *context,
2982 GtkRcStyle *rc_style;
2983 GtkRcStyle *orig_style;
2984 GtkRcStyle *parent_style = NULL;
2985 GtkRcStylePrivate *rc_priv = NULL;
2988 GtkIconFactory *our_factory = NULL;
2989 GHashTable *our_hash = NULL;
2991 token = g_scanner_get_next_token (scanner);
2992 if (token != GTK_RC_TOKEN_STYLE)
2993 return GTK_RC_TOKEN_STYLE;
2995 token = g_scanner_get_next_token (scanner);
2996 if (token != G_TOKEN_STRING)
2997 return G_TOKEN_STRING;
2999 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3001 orig_style = g_object_ref (rc_style);
3007 rc_style = gtk_rc_style_new ();
3008 rc_style->name = g_strdup (scanner->value.v_string);
3010 for (i = 0; i < 5; i++)
3011 rc_style->bg_pixmap_name[i] = NULL;
3013 for (i = 0; i < 5; i++)
3014 rc_style->color_flags[i] = 0;
3017 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
3019 /* If there's a list, its first member is always the factory belonging
3022 if (rc_style->icon_factories)
3023 our_factory = rc_style->icon_factories->data;
3024 if (rc_priv->color_hashes)
3025 our_hash = rc_priv->color_hashes->data;
3027 token = g_scanner_peek_next_token (scanner);
3028 if (token == G_TOKEN_EQUAL_SIGN)
3030 token = g_scanner_get_next_token (scanner);
3032 token = g_scanner_get_next_token (scanner);
3033 if (token != G_TOKEN_STRING)
3035 token = G_TOKEN_STRING;
3039 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3042 for (i = 0; i < 5; i++)
3044 rc_style->color_flags[i] = parent_style->color_flags[i];
3045 rc_style->fg[i] = parent_style->fg[i];
3046 rc_style->bg[i] = parent_style->bg[i];
3047 rc_style->text[i] = parent_style->text[i];
3048 rc_style->base[i] = parent_style->base[i];
3051 rc_style->xthickness = parent_style->xthickness;
3052 rc_style->ythickness = parent_style->ythickness;
3054 if (parent_style->font_desc)
3056 if (rc_style->font_desc)
3057 pango_font_description_free (rc_style->font_desc);
3058 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3061 if (parent_style->rc_properties)
3065 for (i = 0; i < parent_style->rc_properties->len; i++)
3066 insert_rc_property (rc_style,
3067 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3071 for (i = 0; i < 5; i++)
3073 g_free (rc_style->bg_pixmap_name[i]);
3074 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3079 /* get icon_factories and color_hashes from the parent style;
3080 * if the parent_style doesn't have color_hashes, initializes
3081 * the color_hashes with the settings' color scheme (if it exists)
3083 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3085 if (rc_style->icon_factories)
3086 our_factory = rc_style->icon_factories->data;
3087 if (rc_priv->color_hashes)
3088 our_hash = rc_priv->color_hashes->data;
3090 token = g_scanner_get_next_token (scanner);
3091 if (token != G_TOKEN_LEFT_CURLY)
3093 token = G_TOKEN_LEFT_CURLY;
3097 token = g_scanner_peek_next_token (scanner);
3098 while (token != G_TOKEN_RIGHT_CURLY)
3102 case GTK_RC_TOKEN_BG:
3103 token = gtk_rc_parse_bg (scanner, rc_style);
3105 case GTK_RC_TOKEN_FG:
3106 token = gtk_rc_parse_fg (scanner, rc_style);
3108 case GTK_RC_TOKEN_TEXT:
3109 token = gtk_rc_parse_text (scanner, rc_style);
3111 case GTK_RC_TOKEN_BASE:
3112 token = gtk_rc_parse_base (scanner, rc_style);
3114 case GTK_RC_TOKEN_XTHICKNESS:
3115 token = gtk_rc_parse_xthickness (scanner, rc_style);
3117 case GTK_RC_TOKEN_YTHICKNESS:
3118 token = gtk_rc_parse_ythickness (scanner, rc_style);
3120 case GTK_RC_TOKEN_BG_PIXMAP:
3121 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3123 case GTK_RC_TOKEN_FONT:
3124 token = gtk_rc_parse_font (scanner, rc_style);
3126 case GTK_RC_TOKEN_FONTSET:
3127 token = gtk_rc_parse_fontset (scanner, rc_style);
3129 case GTK_RC_TOKEN_FONT_NAME:
3130 token = gtk_rc_parse_font_name (scanner, rc_style);
3132 case GTK_RC_TOKEN_ENGINE:
3133 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3135 case GTK_RC_TOKEN_STOCK:
3136 if (our_factory == NULL)
3137 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3138 our_factory = rc_style->icon_factories->data;
3139 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3141 case GTK_RC_TOKEN_COLOR:
3142 if (our_hash == NULL)
3143 gtk_rc_style_prepend_empty_color_hash (rc_style);
3144 our_hash = rc_priv->color_hashes->data;
3145 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3147 case G_TOKEN_IDENTIFIER:
3148 if (is_c_identifier (scanner->next_value.v_identifier) &&
3149 scanner->next_value.v_identifier[0] >= 'A' &&
3150 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
3152 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3155 g_scanner_get_next_token (scanner); /* eat type name */
3156 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3157 if (g_scanner_get_next_token (scanner) != ':' ||
3158 g_scanner_get_next_token (scanner) != ':')
3163 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3164 !is_c_identifier (scanner->value.v_identifier))
3166 token = G_TOKEN_IDENTIFIER;
3170 /* it's important that we do the same canonification as GParamSpecPool here */
3171 name = g_strdup (scanner->value.v_identifier);
3172 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3173 prop.property_name = g_quark_from_string (name);
3176 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3177 if (token == G_TOKEN_NONE)
3179 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3180 insert_rc_property (rc_style, &prop, TRUE);
3183 g_free (prop.origin);
3184 if (G_VALUE_TYPE (&prop.value))
3185 g_value_unset (&prop.value);
3189 g_scanner_get_next_token (scanner);
3190 token = G_TOKEN_IDENTIFIER;
3194 g_scanner_get_next_token (scanner);
3195 token = G_TOKEN_RIGHT_CURLY;
3199 if (token != G_TOKEN_NONE)
3202 token = g_scanner_peek_next_token (scanner);
3203 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3205 token = g_scanner_get_next_token (scanner);
3206 if (token != G_TOKEN_RIGHT_CURLY)
3208 token = G_TOKEN_RIGHT_CURLY;
3212 if (rc_style != orig_style)
3214 if (!context->rc_style_ht)
3215 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3216 (GEqualFunc) gtk_rc_style_equal);
3218 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3220 /* If we copied the data into a new rc style, fix up references to the old rc style
3221 * in bindings that we have.
3224 fixup_rc_sets (context, orig_style, rc_style);
3228 g_object_unref (orig_style);
3230 return G_TOKEN_NONE;
3233 if (rc_style != orig_style)
3234 gtk_rc_style_unref (rc_style);
3237 g_object_unref (orig_style);
3242 const GtkRcProperty*
3243 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3245 GQuark property_name)
3247 GtkRcProperty *node = NULL;
3249 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3251 if (rc_style->rc_properties)
3255 key.type_name = type_name;
3256 key.property_name = property_name;
3258 node = bsearch (&key,
3259 rc_style->rc_properties->data, rc_style->rc_properties->len,
3260 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3267 gtk_rc_parse_bg (GScanner *scanner,
3273 token = g_scanner_get_next_token (scanner);
3274 if (token != GTK_RC_TOKEN_BG)
3275 return GTK_RC_TOKEN_BG;
3277 token = gtk_rc_parse_state (scanner, &state);
3278 if (token != G_TOKEN_NONE)
3281 token = g_scanner_get_next_token (scanner);
3282 if (token != G_TOKEN_EQUAL_SIGN)
3283 return G_TOKEN_EQUAL_SIGN;
3285 style->color_flags[state] |= GTK_RC_BG;
3286 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3290 gtk_rc_parse_fg (GScanner *scanner,
3296 token = g_scanner_get_next_token (scanner);
3297 if (token != GTK_RC_TOKEN_FG)
3298 return GTK_RC_TOKEN_FG;
3300 token = gtk_rc_parse_state (scanner, &state);
3301 if (token != G_TOKEN_NONE)
3304 token = g_scanner_get_next_token (scanner);
3305 if (token != G_TOKEN_EQUAL_SIGN)
3306 return G_TOKEN_EQUAL_SIGN;
3308 style->color_flags[state] |= GTK_RC_FG;
3309 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3313 gtk_rc_parse_text (GScanner *scanner,
3319 token = g_scanner_get_next_token (scanner);
3320 if (token != GTK_RC_TOKEN_TEXT)
3321 return GTK_RC_TOKEN_TEXT;
3323 token = gtk_rc_parse_state (scanner, &state);
3324 if (token != G_TOKEN_NONE)
3327 token = g_scanner_get_next_token (scanner);
3328 if (token != G_TOKEN_EQUAL_SIGN)
3329 return G_TOKEN_EQUAL_SIGN;
3331 style->color_flags[state] |= GTK_RC_TEXT;
3332 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3336 gtk_rc_parse_base (GScanner *scanner,
3342 token = g_scanner_get_next_token (scanner);
3343 if (token != GTK_RC_TOKEN_BASE)
3344 return GTK_RC_TOKEN_BASE;
3346 token = gtk_rc_parse_state (scanner, &state);
3347 if (token != G_TOKEN_NONE)
3350 token = g_scanner_get_next_token (scanner);
3351 if (token != G_TOKEN_EQUAL_SIGN)
3352 return G_TOKEN_EQUAL_SIGN;
3354 style->color_flags[state] |= GTK_RC_BASE;
3355 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3359 gtk_rc_parse_xthickness (GScanner *scanner,
3362 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3363 return GTK_RC_TOKEN_XTHICKNESS;
3365 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3366 return G_TOKEN_EQUAL_SIGN;
3368 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3371 style->xthickness = scanner->value.v_int;
3373 return G_TOKEN_NONE;
3377 gtk_rc_parse_ythickness (GScanner *scanner,
3380 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3381 return GTK_RC_TOKEN_YTHICKNESS;
3383 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3384 return G_TOKEN_EQUAL_SIGN;
3386 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3389 style->ythickness = scanner->value.v_int;
3391 return G_TOKEN_NONE;
3395 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3397 GtkRcStyle *rc_style)
3403 token = g_scanner_get_next_token (scanner);
3404 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3405 return GTK_RC_TOKEN_BG_PIXMAP;
3407 token = gtk_rc_parse_state (scanner, &state);
3408 if (token != G_TOKEN_NONE)
3411 token = g_scanner_get_next_token (scanner);
3412 if (token != G_TOKEN_EQUAL_SIGN)
3413 return G_TOKEN_EQUAL_SIGN;
3415 token = g_scanner_get_next_token (scanner);
3416 if (token != G_TOKEN_STRING)
3417 return G_TOKEN_STRING;
3419 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3420 (strcmp (scanner->value.v_string, "<none>") == 0))
3421 pixmap_file = g_strdup (scanner->value.v_string);
3423 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3424 scanner, scanner->value.v_string);
3428 g_free (rc_style->bg_pixmap_name[state]);
3429 rc_style->bg_pixmap_name[state] = pixmap_file;
3432 return G_TOKEN_NONE;
3436 gtk_rc_check_pixmap_dir (const gchar *dir,
3437 const gchar *pixmap_file)
3441 buf = g_build_filename (dir, pixmap_file, NULL);
3443 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3452 * gtk_rc_find_pixmap_in_path:
3453 * @settings: a #GtkSettings
3454 * @scanner: Scanner used to get line number information for the
3455 * warning message, or %NULL
3456 * @pixmap_file: name of the pixmap file to locate.
3458 * Looks up a file in pixmap path for the specified #GtkSettings.
3459 * If the file is not found, it outputs a warning message using
3460 * g_warning() and returns %NULL.
3462 * Return value: the filename.
3465 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3467 const gchar *pixmap_file)
3473 GtkRcContext *context = gtk_rc_context_get (settings);
3475 if (context->pixmap_path)
3476 for (i = 0; context->pixmap_path[i] != NULL; i++)
3478 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3483 tmp_list = current_files_stack;
3486 GtkRcFile *curfile = tmp_list->data;
3487 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3491 tmp_list = tmp_list->next;
3495 g_scanner_warn (scanner,
3496 _("Unable to locate image file in pixmap_path: \"%s\""),
3499 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3506 * gtk_rc_find_module_in_path:
3507 * @module_file: name of a theme engine
3509 * Searches for a theme engine in the GTK+ search path. This function
3510 * is not useful for applications and should not be used.
3512 * Return value: The filename, if found (must be freed with g_free()),
3516 gtk_rc_find_module_in_path (const gchar *module_file)
3518 return _gtk_find_module (module_file, "engines");
3522 gtk_rc_parse_font (GScanner *scanner,
3523 GtkRcStyle *rc_style)
3527 token = g_scanner_get_next_token (scanner);
3528 if (token != GTK_RC_TOKEN_FONT)
3529 return GTK_RC_TOKEN_FONT;
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 /* Ignore, do nothing */
3541 return G_TOKEN_NONE;
3545 gtk_rc_parse_fontset (GScanner *scanner,
3546 GtkRcStyle *rc_style)
3550 token = g_scanner_get_next_token (scanner);
3551 if (token != GTK_RC_TOKEN_FONTSET)
3552 return GTK_RC_TOKEN_FONTSET;
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 /* Do nothing - silently ignore */
3564 return G_TOKEN_NONE;
3568 gtk_rc_parse_font_name (GScanner *scanner,
3569 GtkRcStyle *rc_style)
3573 token = g_scanner_get_next_token (scanner);
3574 if (token != GTK_RC_TOKEN_FONT_NAME)
3575 return GTK_RC_TOKEN_FONT;
3577 token = g_scanner_get_next_token (scanner);
3578 if (token != G_TOKEN_EQUAL_SIGN)
3579 return G_TOKEN_EQUAL_SIGN;
3581 token = g_scanner_get_next_token (scanner);
3582 if (token != G_TOKEN_STRING)
3583 return G_TOKEN_STRING;
3585 if (rc_style->font_desc)
3586 pango_font_description_free (rc_style->font_desc);
3588 rc_style->font_desc =
3589 pango_font_description_from_string (scanner->value.v_string);
3591 return G_TOKEN_NONE;
3595 gtk_rc_parse_engine (GtkRcContext *context,
3597 GtkRcStyle **rc_style)
3600 GtkThemeEngine *engine;
3601 guint result = G_TOKEN_NONE;
3602 GtkRcStyle *new_style = NULL;
3603 gboolean parsed_curlies = FALSE;
3604 GtkRcStylePrivate *rc_priv, *new_priv;
3606 token = g_scanner_get_next_token (scanner);
3607 if (token != GTK_RC_TOKEN_ENGINE)
3608 return GTK_RC_TOKEN_ENGINE;
3610 token = g_scanner_get_next_token (scanner);
3611 if (token != G_TOKEN_STRING)
3612 return G_TOKEN_STRING;
3614 if (!scanner->value.v_string[0])
3616 /* Support engine "" {} to mean override to the default engine
3618 token = g_scanner_get_next_token (scanner);
3619 if (token != G_TOKEN_LEFT_CURLY)
3620 return G_TOKEN_LEFT_CURLY;
3622 token = g_scanner_get_next_token (scanner);
3623 if (token != G_TOKEN_RIGHT_CURLY)
3624 return G_TOKEN_RIGHT_CURLY;
3626 parsed_curlies = TRUE;
3628 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3630 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3632 new_style = gtk_rc_style_new ();
3633 gtk_rc_style_real_merge (new_style, *rc_style);
3635 new_style->name = g_strdup ((*rc_style)->name);
3637 /* take over icon factories and color hashes
3638 * from the to-be-deleted style
3640 new_style->icon_factories = (*rc_style)->icon_factories;
3641 (*rc_style)->icon_factories = NULL;
3642 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3643 new_priv->color_hashes = rc_priv->color_hashes;
3644 rc_priv->color_hashes = NULL;
3647 (*rc_style)->engine_specified = TRUE;
3651 engine = gtk_theme_engine_get (scanner->value.v_string);
3653 token = g_scanner_get_next_token (scanner);
3654 if (token != G_TOKEN_LEFT_CURLY)
3655 return G_TOKEN_LEFT_CURLY;
3659 GtkRcStyleClass *new_class;
3661 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3662 new_style = gtk_theme_engine_create_rc_style (engine);
3663 g_type_module_unuse (G_TYPE_MODULE (engine));
3665 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3667 new_class->merge (new_style, *rc_style);
3669 new_style->name = g_strdup ((*rc_style)->name);
3671 /* take over icon factories and color hashes
3672 * from the to-be-deleted style
3674 new_style->icon_factories = (*rc_style)->icon_factories;
3675 (*rc_style)->icon_factories = NULL;
3676 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3677 new_priv->color_hashes = rc_priv->color_hashes;
3678 rc_priv->color_hashes = NULL;
3680 if (new_class->parse)
3682 parsed_curlies = TRUE;
3683 result = new_class->parse (new_style, context->settings, scanner);
3685 if (result != G_TOKEN_NONE)
3687 /* copy icon factories and color hashes back
3689 (*rc_style)->icon_factories = new_style->icon_factories;
3690 new_style->icon_factories = NULL;
3691 rc_priv->color_hashes = new_priv->color_hashes;
3692 new_priv->color_hashes = NULL;
3694 g_object_unref (new_style);
3701 if (!parsed_curlies)
3703 /* Skip over remainder, looking for nested {}'s
3707 result = G_TOKEN_RIGHT_CURLY;
3708 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3710 if (token == G_TOKEN_LEFT_CURLY)
3712 else if (token == G_TOKEN_RIGHT_CURLY)
3717 result = G_TOKEN_NONE;
3725 new_style->engine_specified = TRUE;
3727 g_object_unref (*rc_style);
3728 *rc_style = new_style;
3735 gtk_rc_parse_state (GScanner *scanner,
3736 GtkStateType *state)
3741 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3742 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3744 /* we don't know where we got called from, so we reset the scope here.
3745 * if we bail out due to errors, we *don't* reset the scope, so the
3746 * error messaging code can make sense of our tokens.
3748 old_scope = g_scanner_set_scope (scanner, 0);
3750 token = g_scanner_get_next_token (scanner);
3751 if (token != G_TOKEN_LEFT_BRACE)
3752 return G_TOKEN_LEFT_BRACE;
3754 token = g_scanner_get_next_token (scanner);
3757 case GTK_RC_TOKEN_ACTIVE:
3758 *state = GTK_STATE_ACTIVE;
3760 case GTK_RC_TOKEN_INSENSITIVE:
3761 *state = GTK_STATE_INSENSITIVE;
3763 case GTK_RC_TOKEN_NORMAL:
3764 *state = GTK_STATE_NORMAL;
3766 case GTK_RC_TOKEN_PRELIGHT:
3767 *state = GTK_STATE_PRELIGHT;
3769 case GTK_RC_TOKEN_SELECTED:
3770 *state = GTK_STATE_SELECTED;
3773 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3776 token = g_scanner_get_next_token (scanner);
3777 if (token != G_TOKEN_RIGHT_BRACE)
3778 return G_TOKEN_RIGHT_BRACE;
3780 g_scanner_set_scope (scanner, old_scope);
3782 return G_TOKEN_NONE;
3786 gtk_rc_parse_priority (GScanner *scanner,
3787 GtkPathPriorityType *priority)
3792 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3793 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3795 /* we don't know where we got called from, so we reset the scope here.
3796 * if we bail out due to errors, we *don't* reset the scope, so the
3797 * error messaging code can make sense of our tokens.
3799 old_scope = g_scanner_set_scope (scanner, 0);
3801 token = g_scanner_get_next_token (scanner);
3805 token = g_scanner_get_next_token (scanner);
3808 case GTK_RC_TOKEN_LOWEST:
3809 *priority = GTK_PATH_PRIO_LOWEST;
3811 case GTK_RC_TOKEN_GTK:
3812 *priority = GTK_PATH_PRIO_GTK;
3814 case GTK_RC_TOKEN_APPLICATION:
3815 *priority = GTK_PATH_PRIO_APPLICATION;
3817 case GTK_RC_TOKEN_THEME:
3818 *priority = GTK_PATH_PRIO_THEME;
3820 case GTK_RC_TOKEN_RC:
3821 *priority = GTK_PATH_PRIO_RC;
3823 case GTK_RC_TOKEN_HIGHEST:
3824 *priority = GTK_PATH_PRIO_HIGHEST;
3827 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3830 g_scanner_set_scope (scanner, old_scope);
3832 return G_TOKEN_NONE;
3836 * gtk_rc_parse_color:
3837 * @scanner: a #GScanner
3838 * @color: a pointer to a #GtkColor structure in which to store the result
3840 * Parses a color in the <link linkend="color=format">format</link> expected
3843 * Note that theme engines should use gtk_rc_parse_color_full() in
3844 * order to support symbolic colors.
3846 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3847 * that was expected but not found
3850 gtk_rc_parse_color (GScanner *scanner,
3853 return gtk_rc_parse_color_full (scanner, NULL, color);
3857 * gtk_rc_parse_color_full:
3858 * @scanner: a #GScanner
3859 * @style: a #GtkRcStyle, or %NULL
3860 * @color: a pointer to a #GtkColor structure in which to store the result
3862 * Parses a color in the <link linkend="color=format">format</link> expected
3863 * in a RC file. If @style is not %NULL, it will be consulted to resolve
3864 * references to symbolic colors.
3866 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3867 * that was expected but not found
3872 gtk_rc_parse_color_full (GScanner *scanner,
3878 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3880 /* we don't need to set our own scope here, because
3881 * we don't need own symbols
3884 token = g_scanner_get_next_token (scanner);
3892 case G_TOKEN_LEFT_CURLY:
3893 token = g_scanner_get_next_token (scanner);
3894 if (token == G_TOKEN_INT)
3895 token_int = scanner->value.v_int;
3896 else if (token == G_TOKEN_FLOAT)
3897 token_int = scanner->value.v_float * 65535.0;
3899 return G_TOKEN_FLOAT;
3900 color->red = CLAMP (token_int, 0, 65535);
3902 token = g_scanner_get_next_token (scanner);
3903 if (token != G_TOKEN_COMMA)
3904 return G_TOKEN_COMMA;
3906 token = g_scanner_get_next_token (scanner);
3907 if (token == G_TOKEN_INT)
3908 token_int = scanner->value.v_int;
3909 else if (token == G_TOKEN_FLOAT)
3910 token_int = scanner->value.v_float * 65535.0;
3912 return G_TOKEN_FLOAT;
3913 color->green = CLAMP (token_int, 0, 65535);
3915 token = g_scanner_get_next_token (scanner);
3916 if (token != G_TOKEN_COMMA)
3917 return G_TOKEN_COMMA;
3919 token = g_scanner_get_next_token (scanner);
3920 if (token == G_TOKEN_INT)
3921 token_int = scanner->value.v_int;
3922 else if (token == G_TOKEN_FLOAT)
3923 token_int = scanner->value.v_float * 65535.0;
3925 return G_TOKEN_FLOAT;
3926 color->blue = CLAMP (token_int, 0, 65535);
3928 token = g_scanner_get_next_token (scanner);
3929 if (token != G_TOKEN_RIGHT_CURLY)
3930 return G_TOKEN_RIGHT_CURLY;
3931 return G_TOKEN_NONE;
3933 case G_TOKEN_STRING:
3934 if (!gdk_color_parse (scanner->value.v_string, color))
3936 g_scanner_warn (scanner, "Invalid color constant '%s'",
3937 scanner->value.v_string);
3938 return G_TOKEN_STRING;
3940 return G_TOKEN_NONE;
3943 token = g_scanner_get_next_token (scanner);
3944 if (token != G_TOKEN_IDENTIFIER)
3945 return G_TOKEN_IDENTIFIER;
3947 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3949 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3950 scanner->value.v_identifier);
3951 return G_TOKEN_IDENTIFIER;
3954 return G_TOKEN_NONE;
3956 case G_TOKEN_IDENTIFIER:
3957 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3959 token = g_scanner_get_next_token (scanner);
3960 if (token != G_TOKEN_LEFT_PAREN)
3961 return G_TOKEN_LEFT_PAREN;
3964 if (g_scanner_peek_next_token (scanner) == '-')
3966 g_scanner_get_next_token (scanner); /* eat sign */
3970 token = g_scanner_get_next_token (scanner);
3971 if (token != G_TOKEN_FLOAT)
3972 return G_TOKEN_FLOAT;
3974 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3976 token = g_scanner_get_next_token (scanner);
3977 if (token != G_TOKEN_COMMA)
3978 return G_TOKEN_COMMA;
3980 token = gtk_rc_parse_color_full (scanner, style, &c1);
3981 if (token != G_TOKEN_NONE)
3984 token = g_scanner_get_next_token (scanner);
3985 if (token != G_TOKEN_COMMA)
3986 return G_TOKEN_COMMA;
3988 token = gtk_rc_parse_color_full (scanner, style, &c2);
3989 if (token != G_TOKEN_NONE)
3992 token = g_scanner_get_next_token (scanner);
3993 if (token != G_TOKEN_RIGHT_PAREN)
3994 return G_TOKEN_RIGHT_PAREN;
3996 color->red = l * c1.red + (1.0 - l) * c2.red;
3997 color->green = l * c1.green + (1.0 - l) * c2.green;
3998 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
4000 return G_TOKEN_NONE;
4002 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
4004 token = g_scanner_get_next_token (scanner);
4005 if (token != G_TOKEN_LEFT_PAREN)
4006 return G_TOKEN_LEFT_PAREN;
4009 if (g_scanner_peek_next_token (scanner) == '-')
4011 g_scanner_get_next_token (scanner); /* eat sign */
4015 token = g_scanner_get_next_token (scanner);
4016 if (token != G_TOKEN_FLOAT)
4017 return G_TOKEN_FLOAT;
4019 l = negate ? -scanner->value.v_float : scanner->value.v_float;
4021 token = g_scanner_get_next_token (scanner);
4022 if (token != G_TOKEN_COMMA)
4023 return G_TOKEN_COMMA;
4025 token = gtk_rc_parse_color_full (scanner, style, &c1);
4026 if (token != G_TOKEN_NONE)
4029 token = g_scanner_get_next_token (scanner);
4030 if (token != G_TOKEN_RIGHT_PAREN)
4031 return G_TOKEN_RIGHT_PAREN;
4033 _gtk_style_shade (&c1, color, l);
4035 return G_TOKEN_NONE;
4037 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4038 strcmp (scanner->value.v_identifier, "darker") == 0)
4040 if (scanner->value.v_identifier[0] == 'l')
4045 token = g_scanner_get_next_token (scanner);
4046 if (token != G_TOKEN_LEFT_PAREN)
4047 return G_TOKEN_LEFT_PAREN;
4049 token = gtk_rc_parse_color_full (scanner, style, &c1);
4050 if (token != G_TOKEN_NONE)
4053 token = g_scanner_get_next_token (scanner);
4054 if (token != G_TOKEN_RIGHT_PAREN)
4055 return G_TOKEN_RIGHT_PAREN;
4057 _gtk_style_shade (&c1, color, l);
4059 return G_TOKEN_NONE;
4062 return G_TOKEN_IDENTIFIER;
4065 return G_TOKEN_STRING;
4070 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4075 token = g_scanner_get_next_token (scanner);
4076 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4077 return GTK_RC_TOKEN_PIXMAP_PATH;
4079 token = g_scanner_get_next_token (scanner);
4080 if (token != G_TOKEN_STRING)
4081 return G_TOKEN_STRING;
4083 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4085 return G_TOKEN_NONE;
4089 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4091 const gchar *pix_path)
4093 g_strfreev (context->pixmap_path);
4094 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4098 gtk_rc_parse_module_path (GScanner *scanner)
4102 token = g_scanner_get_next_token (scanner);
4103 if (token != GTK_RC_TOKEN_MODULE_PATH)
4104 return GTK_RC_TOKEN_MODULE_PATH;
4106 token = g_scanner_get_next_token (scanner);
4107 if (token != G_TOKEN_STRING)
4108 return G_TOKEN_STRING;
4110 g_warning ("module_path directive is now ignored\n");
4112 return G_TOKEN_NONE;
4116 gtk_rc_parse_im_module_file (GScanner *scanner)
4120 token = g_scanner_get_next_token (scanner);
4121 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4122 return GTK_RC_TOKEN_IM_MODULE_FILE;
4124 token = g_scanner_get_next_token (scanner);
4125 if (token != G_TOKEN_STRING)
4126 return G_TOKEN_STRING;
4128 g_free (im_module_file);
4130 im_module_file = g_strdup (scanner->value.v_string);
4132 return G_TOKEN_NONE;
4136 gtk_rc_parse_path_pattern (GtkRcContext *context,
4140 GtkPathType path_type;
4142 gboolean is_binding;
4143 GtkPathPriorityType priority = context->default_priority;
4145 token = g_scanner_get_next_token (scanner);
4148 case GTK_RC_TOKEN_WIDGET:
4149 path_type = GTK_PATH_WIDGET;
4151 case GTK_RC_TOKEN_WIDGET_CLASS:
4152 path_type = GTK_PATH_WIDGET_CLASS;
4154 case GTK_RC_TOKEN_CLASS:
4155 path_type = GTK_PATH_CLASS;
4158 return GTK_RC_TOKEN_WIDGET_CLASS;
4161 token = g_scanner_get_next_token (scanner);
4162 if (token != G_TOKEN_STRING)
4163 return G_TOKEN_STRING;
4165 pattern = g_strdup (scanner->value.v_string);
4167 token = g_scanner_get_next_token (scanner);
4168 if (token == GTK_RC_TOKEN_STYLE)
4170 else if (token == GTK_RC_TOKEN_BINDING)
4175 return GTK_RC_TOKEN_STYLE;
4178 if (g_scanner_peek_next_token (scanner) == ':')
4180 token = gtk_rc_parse_priority (scanner, &priority);
4181 if (token != G_TOKEN_NONE)
4188 token = g_scanner_get_next_token (scanner);
4189 if (token != G_TOKEN_STRING)
4192 return G_TOKEN_STRING;
4197 GtkBindingSet *binding;
4199 binding = gtk_binding_set_find (scanner->value.v_string);
4203 return G_TOKEN_STRING;
4205 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4209 GtkRcStyle *rc_style;
4212 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4217 return G_TOKEN_STRING;
4220 rc_set = g_new (GtkRcSet, 1);
4221 rc_set->type = path_type;
4223 if (path_type == GTK_PATH_WIDGET_CLASS)
4225 rc_set->pspec = NULL;
4226 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4230 rc_set->pspec = g_pattern_spec_new (pattern);
4231 rc_set->path = NULL;
4234 rc_set->rc_style = rc_style;
4235 rc_set->priority = priority;
4237 if (path_type == GTK_PATH_WIDGET)
4238 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4239 else if (path_type == GTK_PATH_WIDGET_CLASS)
4240 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4242 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4246 return G_TOKEN_NONE;
4250 gtk_rc_parse_hash_key (GScanner *scanner,
4255 token = g_scanner_get_next_token (scanner);
4256 if (token != G_TOKEN_LEFT_BRACE)
4257 return G_TOKEN_LEFT_BRACE;
4259 token = g_scanner_get_next_token (scanner);
4261 if (token != G_TOKEN_STRING)
4262 return G_TOKEN_STRING;
4264 *hash_key = g_strdup (scanner->value.v_string);
4266 token = g_scanner_get_next_token (scanner);
4267 if (token != G_TOKEN_RIGHT_BRACE)
4270 return G_TOKEN_RIGHT_BRACE;
4273 return G_TOKEN_NONE;
4277 gtk_rc_parse_icon_source (GtkRcContext *context,
4279 GtkIconSet *icon_set,
4280 gboolean *icon_set_valid)
4283 gchar *full_filename;
4284 GtkIconSource *source = NULL;
4286 token = g_scanner_get_next_token (scanner);
4287 if (token != G_TOKEN_LEFT_CURLY)
4288 return G_TOKEN_LEFT_CURLY;
4290 token = g_scanner_get_next_token (scanner);
4292 if (token != G_TOKEN_STRING && token != '@')
4293 return G_TOKEN_STRING;
4295 if (token == G_TOKEN_STRING)
4299 source = gtk_icon_source_new ();
4300 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4303 gtk_icon_source_set_filename (source, full_filename);
4304 g_free (full_filename);
4311 token = g_scanner_get_next_token (scanner);
4313 if (token != G_TOKEN_STRING)
4314 return G_TOKEN_STRING;
4316 source = gtk_icon_source_new ();
4317 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4320 /* We continue parsing even if we didn't find the pixmap so that rest of the
4321 * file is read, even if the syntax is bad. However we don't validate the
4322 * icon_set so the caller can choose not to install it.
4324 token = g_scanner_get_next_token (scanner);
4326 if (token == G_TOKEN_RIGHT_CURLY)
4328 else if (token != G_TOKEN_COMMA)
4330 gtk_icon_source_free (source);
4331 return G_TOKEN_COMMA;
4334 /* Get the direction */
4336 token = g_scanner_get_next_token (scanner);
4340 case GTK_RC_TOKEN_RTL:
4341 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4342 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4345 case GTK_RC_TOKEN_LTR:
4346 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4347 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4354 gtk_icon_source_free (source);
4355 return GTK_RC_TOKEN_RTL;
4359 token = g_scanner_get_next_token (scanner);
4361 if (token == G_TOKEN_RIGHT_CURLY)
4363 else if (token != G_TOKEN_COMMA)
4365 gtk_icon_source_free (source);
4366 return G_TOKEN_COMMA;
4371 token = g_scanner_get_next_token (scanner);
4375 case GTK_RC_TOKEN_NORMAL:
4376 gtk_icon_source_set_state_wildcarded (source, FALSE);
4377 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4380 case GTK_RC_TOKEN_PRELIGHT:
4381 gtk_icon_source_set_state_wildcarded (source, FALSE);
4382 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4386 case GTK_RC_TOKEN_INSENSITIVE:
4387 gtk_icon_source_set_state_wildcarded (source, FALSE);
4388 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4391 case GTK_RC_TOKEN_ACTIVE:
4392 gtk_icon_source_set_state_wildcarded (source, FALSE);
4393 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4396 case GTK_RC_TOKEN_SELECTED:
4397 gtk_icon_source_set_state_wildcarded (source, FALSE);
4398 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4405 gtk_icon_source_free (source);
4406 return GTK_RC_TOKEN_PRELIGHT;
4410 token = g_scanner_get_next_token (scanner);
4412 if (token == G_TOKEN_RIGHT_CURLY)
4414 else if (token != G_TOKEN_COMMA)
4416 gtk_icon_source_free (source);
4417 return G_TOKEN_COMMA;
4422 token = g_scanner_get_next_token (scanner);
4428 if (token != G_TOKEN_STRING)
4430 gtk_icon_source_free (source);
4431 return G_TOKEN_STRING;
4434 size = gtk_icon_size_from_name (scanner->value.v_string);
4436 if (size != GTK_ICON_SIZE_INVALID)
4438 gtk_icon_source_set_size_wildcarded (source, FALSE);
4439 gtk_icon_source_set_size (source, size);
4443 /* Check the close brace */
4445 token = g_scanner_get_next_token (scanner);
4446 if (token != G_TOKEN_RIGHT_CURLY)
4448 gtk_icon_source_free (source);
4449 return G_TOKEN_RIGHT_CURLY;
4453 if (gtk_icon_source_get_filename (source) ||
4454 gtk_icon_source_get_icon_name (source))
4456 gtk_icon_set_add_source (icon_set, source);
4457 *icon_set_valid = TRUE;
4459 gtk_icon_source_free (source);
4461 return G_TOKEN_NONE;
4465 gtk_rc_parse_stock (GtkRcContext *context,
4467 GtkRcStyle *rc_style,
4468 GtkIconFactory *factory)
4470 GtkIconSet *icon_set = NULL;
4471 gboolean icon_set_valid = FALSE;
4472 gchar *stock_id = NULL;
4475 token = g_scanner_get_next_token (scanner);
4476 if (token != GTK_RC_TOKEN_STOCK)
4477 return GTK_RC_TOKEN_STOCK;
4479 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4480 if (token != G_TOKEN_NONE)
4483 token = g_scanner_get_next_token (scanner);
4484 if (token != G_TOKEN_EQUAL_SIGN)
4487 return G_TOKEN_EQUAL_SIGN;
4490 token = g_scanner_get_next_token (scanner);
4491 if (token != G_TOKEN_LEFT_CURLY)
4494 return G_TOKEN_LEFT_CURLY;
4497 token = g_scanner_peek_next_token (scanner);
4498 while (token != G_TOKEN_RIGHT_CURLY)
4500 if (icon_set == NULL)
4501 icon_set = gtk_icon_set_new ();
4503 token = gtk_rc_parse_icon_source (context,
4504 scanner, icon_set, &icon_set_valid);
4505 if (token != G_TOKEN_NONE)
4508 gtk_icon_set_unref (icon_set);
4512 token = g_scanner_get_next_token (scanner);
4514 if (token != G_TOKEN_COMMA &&
4515 token != G_TOKEN_RIGHT_CURLY)
4518 gtk_icon_set_unref (icon_set);
4519 return G_TOKEN_RIGHT_CURLY;
4526 gtk_icon_factory_add (factory,
4530 gtk_icon_set_unref (icon_set);
4535 return G_TOKEN_NONE;
4539 gtk_rc_parse_logical_color (GScanner *scanner,
4540 GtkRcStyle *rc_style,
4543 gchar *color_id = NULL;
4547 token = g_scanner_get_next_token (scanner);
4548 if (token != GTK_RC_TOKEN_COLOR)
4549 return GTK_RC_TOKEN_COLOR;
4551 token = gtk_rc_parse_hash_key (scanner, &color_id);
4552 if (token != G_TOKEN_NONE)
4555 token = g_scanner_get_next_token (scanner);
4556 if (token != G_TOKEN_EQUAL_SIGN)
4559 return G_TOKEN_EQUAL_SIGN;
4562 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4563 if (token != G_TOKEN_NONE)
4569 /* Because the hash is created with destroy functions,
4570 * g_hash_table_insert will free any old values for us,
4571 * if a mapping with the specified key already exists.
4573 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4575 return G_TOKEN_NONE;
4580 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4584 const gchar *current;
4585 const gchar *class_start;
4586 const gchar *class_end;
4587 const gchar *pattern_end;
4588 const gchar *pattern_start;
4593 while ((class_start = strchr (current, '<')) &&
4594 (class_end = strchr (class_start, '>')))
4596 /* Add patterns, but ignore single dots */
4597 if (!(class_start == current ||
4598 (class_start == current + 1 && current[0] == '.')))
4600 pattern_end = class_start - 1;
4601 pattern_start = current;
4603 path_elt = g_new (PathElt, 1);
4605 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4606 path_elt->type = PATH_ELT_PSPEC;
4607 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4608 g_free (sub_pattern);
4610 result = g_slist_prepend (result, path_elt);
4613 path_elt = g_new (PathElt, 1);
4615 /* The < > need to be removed from the string. */
4616 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4618 path_elt->type = PATH_ELT_UNRESOLVED;
4619 path_elt->elt.class_name = sub_pattern;
4621 result = g_slist_prepend (result, path_elt);
4623 current = class_end + 1;
4626 /* Add the rest, if anything is left */
4627 if (strlen (current) > 0)
4629 path_elt = g_new (PathElt, 1);
4630 path_elt->type = PATH_ELT_PSPEC;
4631 path_elt->elt.pspec = g_pattern_spec_new (current);
4633 result = g_slist_prepend (result, path_elt);
4636 return g_slist_reverse (result);
4640 free_path_elt (gpointer data,
4643 PathElt *path_elt = data;
4645 switch (path_elt->type)
4647 case PATH_ELT_PSPEC:
4648 g_pattern_spec_free (path_elt->elt.pspec);
4650 case PATH_ELT_UNRESOLVED:
4651 g_free (path_elt->elt.class_name);
4656 g_assert_not_reached ();
4663 _gtk_rc_free_widget_class_path (GSList *list)
4665 g_slist_foreach (list, free_path_elt, NULL);
4666 g_slist_free (list);
4670 gtk_rc_set_free (GtkRcSet *rc_set)
4673 g_pattern_spec_free (rc_set->pspec);
4675 _gtk_rc_free_widget_class_path (rc_set->path);
4681 match_class (PathElt *path_elt,
4686 if (path_elt->type == PATH_ELT_UNRESOLVED)
4688 type = g_type_from_name (path_elt->elt.class_name);
4689 if (type != G_TYPE_INVALID)
4691 g_free (path_elt->elt.class_name);
4692 path_elt->elt.class_type = type;
4693 path_elt->type = PATH_ELT_TYPE;
4696 return g_str_equal (type_name, path_elt->elt.class_name);
4699 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4703 match_widget_class_recursive (GSList *list,
4706 gchar *path_reversed)
4710 /* break out if we cannot match anymore. */
4719 /* there are two possibilities:
4720 * 1. The next pattern should match the class.
4721 * 2. First normal matching, and then maybe a class */
4723 path_elt = list->data;
4725 if (path_elt->type != PATH_ELT_PSPEC)
4727 gchar *class_start = path;
4730 /* ignore leading dot */
4731 if (class_start[0] == '.')
4733 class_end = strchr (class_start, '.');
4735 if (class_end == NULL)
4737 if (!match_class (path_elt, class_start))
4740 return match_widget_class_recursive (list->next, 0, "", "");
4744 class_end[0] = '\0';
4745 if (!match_class (path_elt, class_start))
4753 gint new_length = length - (class_end - path);
4754 gchar old_char = path_reversed[new_length];
4758 path_reversed[new_length] = '\0';
4759 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4760 path_reversed[new_length] = old_char;
4771 gboolean result = FALSE;
4773 /* If there is nothing after this (ie. no class match),
4774 * just compare the pspec.
4776 if (list->next == NULL)
4777 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4779 class_elt = (PathElt *)list->next->data;
4780 g_assert (class_elt->type != PATH_ELT_PSPEC);
4783 if (class_start[0] == '.')
4788 class_end = strchr (class_start, '.');
4790 /* It should be cheaper to match the class first. (either the pattern
4791 * is simple, and will match most of the times, or it may be complex
4792 * and matching is slow)
4794 if (class_end == NULL)
4796 result = match_class (class_elt, class_start);
4800 class_end[0] = '\0';
4801 result = match_class (class_elt, class_start);
4810 /* terminate the string in front of the class. It does not matter
4811 * that the class becomes unusable, because it is not needed
4812 * inside the recursion
4814 old_char = class_start[0];
4815 class_start[0] = '\0';
4817 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4819 if (class_end != NULL)
4821 gint new_length = length - (class_end - path);
4822 gchar path_reversed_char = path_reversed[new_length];
4824 path_reversed[new_length] = '\0';
4826 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4828 path_reversed[new_length] = path_reversed_char;
4831 result = match_widget_class_recursive (list->next->next, 0, "", "");
4834 class_start[0] = old_char;
4840 /* get next class in path, or break out */
4841 if (class_end != NULL)
4842 class_start = class_end + 1;
4850 _gtk_rc_match_widget_class (GSList *list,
4853 gchar *path_reversed)
4855 return match_widget_class_recursive (list, length, path, path_reversed);
4860 /* DLL ABI stability backward compatibility versions */
4862 #undef gtk_rc_add_default_file
4865 gtk_rc_add_default_file (const gchar *filename)
4867 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4869 gtk_rc_add_default_file_utf8 (utf8_filename);
4871 g_free (utf8_filename);
4874 #undef gtk_rc_set_default_files
4877 gtk_rc_set_default_files (gchar **filenames)
4879 gchar **utf8_filenames;
4882 while (filenames[n++] != NULL)
4885 utf8_filenames = g_new (gchar *, n + 1);
4887 for (i = 0; i < n; i++)
4888 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4890 utf8_filenames[n] = NULL;
4892 gtk_rc_set_default_files_utf8 (utf8_filenames);
4894 g_strfreev (utf8_filenames);
4900 gtk_rc_parse (const gchar *filename)
4902 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4904 gtk_rc_parse_utf8 (utf8_filename);
4906 g_free (utf8_filename);
4911 #define __GTK_RC_C__
4912 #include "gtkaliasdef.c"