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>
49 #include "gdkconfig.h"
51 #include "gtkversion.h"
53 #include "gtkbindings.h"
54 #include "gtkthemes.h"
56 #include "gtkiconfactory.h"
58 #include "gtkmodules.h"
59 #include "gtkprivate.h"
60 #include "gtksettings.h"
61 #include "gtkwindow.h"
67 typedef struct _GtkRcSet GtkRcSet;
68 typedef struct _GtkRcNode GtkRcNode;
69 typedef struct _GtkRcFile GtkRcFile;
80 gboolean is_string; /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
83 gchar *canonical_name;
88 #define GTK_RC_MAX_PIXMAP_PATHS 128
92 GHashTable *rc_style_ht;
93 GtkSettings *settings;
94 GSList *rc_sets_widget;
95 GSList *rc_sets_widget_class;
96 GSList *rc_sets_class;
98 /* The files we have parsed, to reread later if necessary */
102 gchar *key_theme_name;
105 gchar *pixmap_path[GTK_RC_MAX_PIXMAP_PATHS];
107 gint default_priority;
108 GtkStyle *default_style;
111 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
113 static guint gtk_rc_style_hash (const gchar *name);
114 static gboolean gtk_rc_style_equal (const gchar *a,
116 static guint gtk_rc_styles_hash (const GSList *rc_styles);
117 static gboolean gtk_rc_styles_equal (const GSList *a,
119 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
121 static GSList * gtk_rc_styles_match (GSList *rc_styles,
125 const gchar *path_reversed);
126 static GtkStyle * gtk_rc_style_to_style (GtkRcContext *context,
127 GtkRcStyle *rc_style);
128 static GtkStyle* gtk_rc_init_style (GtkRcContext *context,
130 static void gtk_rc_parse_default_files (GtkRcContext *context);
131 static void gtk_rc_parse_named (GtkRcContext *context,
134 static void gtk_rc_context_parse_file (GtkRcContext *context,
135 const gchar *filename,
138 static void gtk_rc_parse_any (GtkRcContext *context,
139 const gchar *input_name,
141 const gchar *input_string);
142 static guint gtk_rc_parse_statement (GtkRcContext *context,
144 static guint gtk_rc_parse_style (GtkRcContext *context,
146 static guint gtk_rc_parse_assignment (GScanner *scanner,
147 GtkRcProperty *prop);
148 static guint gtk_rc_parse_bg (GScanner *scanner,
150 static guint gtk_rc_parse_fg (GScanner *scanner,
152 static guint gtk_rc_parse_text (GScanner *scanner,
154 static guint gtk_rc_parse_base (GScanner *scanner,
156 static guint gtk_rc_parse_xthickness (GScanner *scanner,
158 static guint gtk_rc_parse_ythickness (GScanner *scanner,
160 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
162 GtkRcStyle *rc_style);
163 static guint gtk_rc_parse_font (GScanner *scanner,
164 GtkRcStyle *rc_style);
165 static guint gtk_rc_parse_fontset (GScanner *scanner,
166 GtkRcStyle *rc_style);
167 static guint gtk_rc_parse_font_name (GScanner *scanner,
168 GtkRcStyle *rc_style);
169 static guint gtk_rc_parse_engine (GtkRcContext *context,
171 GtkRcStyle **rc_style);
172 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
174 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
176 const gchar *pix_path);
177 static guint gtk_rc_parse_module_path (GScanner *scanner);
178 static guint gtk_rc_parse_im_module_file (GScanner *scanner);
179 static guint gtk_rc_parse_path_pattern (GtkRcContext *context,
181 static guint gtk_rc_parse_stock (GtkRcContext *context,
183 GtkRcStyle *rc_style,
184 GtkIconFactory *factory);
185 static void gtk_rc_clear_hash_node (gpointer key,
188 static void gtk_rc_clear_styles (GtkRcContext *context);
189 static void gtk_rc_add_initial_default_files (void);
191 static void gtk_rc_style_init (GtkRcStyle *style);
192 static void gtk_rc_style_class_init (GtkRcStyleClass *klass);
193 static void gtk_rc_style_finalize (GObject *object);
194 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
196 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
197 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
198 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
199 gconstpointer bsearch_node2);
201 static gpointer parent_class = NULL;
203 static const GScannerConfig gtk_rc_scanner_config =
207 ) /* cset_skip_characters */,
212 ) /* cset_identifier_first */,
217 ) /* cset_identifier_nth */,
218 ( "#\n" ) /* cpair_comment_single */,
220 TRUE /* case_sensitive */,
222 TRUE /* skip_comment_multi */,
223 TRUE /* skip_comment_single */,
224 TRUE /* scan_comment_multi */,
225 TRUE /* scan_identifier */,
226 FALSE /* scan_identifier_1char */,
227 FALSE /* scan_identifier_NULL */,
228 TRUE /* scan_symbols */,
229 TRUE /* scan_binary */,
230 TRUE /* scan_octal */,
231 TRUE /* scan_float */,
233 TRUE /* scan_hex_dollar */,
234 TRUE /* scan_string_sq */,
235 TRUE /* scan_string_dq */,
236 TRUE /* numbers_2_int */,
237 FALSE /* int_2_float */,
238 FALSE /* identifier_2_string */,
239 TRUE /* char_2_token */,
240 TRUE /* symbol_2_token */,
241 FALSE /* scope_0_fallback */,
249 { "include", GTK_RC_TOKEN_INCLUDE },
250 { "NORMAL", GTK_RC_TOKEN_NORMAL },
251 { "ACTIVE", GTK_RC_TOKEN_ACTIVE },
252 { "PRELIGHT", GTK_RC_TOKEN_PRELIGHT },
253 { "SELECTED", GTK_RC_TOKEN_SELECTED },
254 { "INSENSITIVE", GTK_RC_TOKEN_INSENSITIVE },
255 { "fg", GTK_RC_TOKEN_FG },
256 { "bg", GTK_RC_TOKEN_BG },
257 { "text", GTK_RC_TOKEN_TEXT },
258 { "base", GTK_RC_TOKEN_BASE },
259 { "xthickness", GTK_RC_TOKEN_XTHICKNESS },
260 { "ythickness", GTK_RC_TOKEN_YTHICKNESS },
261 { "font", GTK_RC_TOKEN_FONT },
262 { "fontset", GTK_RC_TOKEN_FONTSET },
263 { "font_name", GTK_RC_TOKEN_FONT_NAME },
264 { "bg_pixmap", GTK_RC_TOKEN_BG_PIXMAP },
265 { "pixmap_path", GTK_RC_TOKEN_PIXMAP_PATH },
266 { "style", GTK_RC_TOKEN_STYLE },
267 { "binding", GTK_RC_TOKEN_BINDING },
268 { "bind", GTK_RC_TOKEN_BIND },
269 { "widget", GTK_RC_TOKEN_WIDGET },
270 { "widget_class", GTK_RC_TOKEN_WIDGET_CLASS },
271 { "class", GTK_RC_TOKEN_CLASS },
272 { "lowest", GTK_RC_TOKEN_LOWEST },
273 { "gtk", GTK_RC_TOKEN_GTK },
274 { "application", GTK_RC_TOKEN_APPLICATION },
275 { "theme", GTK_RC_TOKEN_THEME },
276 { "rc", GTK_RC_TOKEN_RC },
277 { "highest", GTK_RC_TOKEN_HIGHEST },
278 { "engine", GTK_RC_TOKEN_ENGINE },
279 { "module_path", GTK_RC_TOKEN_MODULE_PATH },
280 { "stock", GTK_RC_TOKEN_STOCK },
281 { "im_module_file", GTK_RC_TOKEN_IM_MODULE_FILE },
282 { "LTR", GTK_RC_TOKEN_LTR },
283 { "RTL", GTK_RC_TOKEN_RTL }
286 static GHashTable *realized_style_ht = NULL;
288 static gchar *im_module_file = NULL;
290 #define GTK_RC_MAX_DEFAULT_FILES 128
291 static gchar *gtk_rc_default_files[GTK_RC_MAX_DEFAULT_FILES];
293 /* A stack of information of RC files we are parsing currently.
294 * The directories for these files are implicitely added to the end of
297 static GSList *current_files_stack = NULL;
299 /* RC files and strings that are parsed for every context
301 static GSList *global_rc_files = NULL;
303 /* Keep list of all current RC contexts for convenience
305 static GSList *rc_contexts;
307 /* RC file handling */
310 gtk_rc_make_default_dir (const gchar *type)
315 var = g_getenv ("GTK_EXE_PREFIX");
317 path = g_build_filename (var, "lib", "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
319 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_BINARY_VERSION, type, NULL);
325 * gtk_rc_get_im_module_path:
326 * @returns: a newly-allocated string containing the path in which to
327 * look for IM modules.
329 * Obtains the path in which to look for IM modules. See the documentation
330 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
331 * environment variable for more details about looking up modules. This
332 * function is useful solely for utilities supplied with GTK+ and should
333 * not be used by applications under normal circumstances.
336 gtk_rc_get_im_module_path (void)
338 gchar **paths = _gtk_get_module_path ("immodules");
339 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
346 * gtk_rc_get_im_module_file:
347 * @returns: a newly-allocated string containing the name of the file
348 * listing the IM modules available for loading
350 * Obtains the path to the IM modules file. See the documentation
351 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
352 * environment variable for more details.
355 gtk_rc_get_im_module_file (void)
357 gchar *result = g_strdup (g_getenv ("GTK_IM_MODULE_FILE"));
362 result = g_strdup (im_module_file);
364 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
371 gtk_rc_get_theme_dir (void)
376 var = g_getenv ("GTK_DATA_PREFIX");
378 path = g_build_filename (var, "share", "themes", NULL);
380 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
386 * gtk_rc_get_module_dir:
388 * Returns a directory in which GTK+ looks for theme engines.
389 * For full information about the search for theme engines,
390 * see the docs for <envar>GTK_PATH</envar> in
391 * <xref linkend="gtk-running"/>.
393 * return value: the directory. (Must be freed with g_free())
396 gtk_rc_get_module_dir (void)
398 return gtk_rc_make_default_dir ("engines");
402 gtk_rc_add_initial_default_files (void)
404 static gint init = FALSE;
413 gtk_rc_default_files[0] = NULL;
416 var = g_getenv ("GTK2_RC_FILES");
419 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, 128);
423 gtk_rc_add_default_file (files[i]);
430 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
432 gtk_rc_add_default_file (str);
435 var = g_get_home_dir ();
438 str = g_build_filename (var, ".gtkrc-2.0", NULL);
439 gtk_rc_add_default_file (str);
446 * gtk_rc_add_default_file:
447 * @filename: the pathname to the file. If @filename is not absolute, it
448 * is searched in the current directory.
450 * Adds a file to the list of files to be parsed at the
454 gtk_rc_add_default_file (const gchar *filename)
458 gtk_rc_add_initial_default_files ();
460 for (n = 0; gtk_rc_default_files[n]; n++) ;
461 if (n >= GTK_RC_MAX_DEFAULT_FILES - 1)
464 gtk_rc_default_files[n++] = g_strdup (filename);
465 gtk_rc_default_files[n] = NULL;
469 * gtk_rc_set_default_files:
470 * @filenames: A %NULL-terminated list of filenames.
472 * Sets the list of files that GTK+ will read at the
476 gtk_rc_set_default_files (gchar **filenames)
480 gtk_rc_add_initial_default_files ();
483 while (gtk_rc_default_files[i])
485 g_free (gtk_rc_default_files[i]);
489 gtk_rc_default_files[0] = NULL;
492 while (filenames[i] != NULL)
494 gtk_rc_add_default_file (filenames[i]);
500 * gtk_rc_get_default_files:
502 * Retrieves the current list of RC files that will be parsed
503 * at the end of gtk_init().
505 * Return value: A %NULL-terminated array of filenames. This memory
506 * is owned by GTK+ and must not be freed by the application.
507 * If you want to store this information, you should make a copy.
510 gtk_rc_get_default_files (void)
512 gtk_rc_add_initial_default_files ();
514 return gtk_rc_default_files;
518 gtk_rc_settings_changed (GtkSettings *settings,
520 GtkRcContext *context)
522 gchar *new_theme_name;
523 gchar *new_key_theme_name;
525 g_object_get (settings,
526 "gtk-theme-name", &new_theme_name,
527 "gtk-key-theme-name", &new_key_theme_name,
530 if ((new_theme_name != context->theme_name &&
531 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
532 (new_key_theme_name != context->key_theme_name &&
533 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
535 gtk_rc_reparse_all_for_settings (settings, TRUE);
538 g_free (new_theme_name);
539 g_free (new_key_theme_name);
543 gtk_rc_font_name_changed (GtkSettings *settings,
545 GtkRcContext *context)
547 _gtk_rc_context_get_default_font_name (settings);
550 static GtkRcContext *
551 gtk_rc_context_get (GtkSettings *settings)
553 if (!settings->rc_context)
555 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
557 context->settings = settings;
558 context->rc_style_ht = NULL;
559 context->rc_sets_widget = NULL;
560 context->rc_sets_widget_class = NULL;
561 context->rc_sets_class = NULL;
562 context->rc_files = NULL;
563 context->default_style = NULL;
565 g_object_get (settings,
566 "gtk-theme-name", &context->theme_name,
567 "gtk-key-theme-name", &context->key_theme_name,
568 "gtk-font-name", &context->font_name,
571 g_signal_connect (settings,
572 "notify::gtk-theme-name",
573 G_CALLBACK (gtk_rc_settings_changed),
575 g_signal_connect (settings,
576 "notify::gtk-key-theme-name",
577 G_CALLBACK (gtk_rc_settings_changed),
579 g_signal_connect (settings,
580 "notify::gtk-font-name",
581 G_CALLBACK (gtk_rc_font_name_changed),
585 context->pixmap_path[0] = NULL;
587 context->default_priority = GTK_PATH_PRIO_RC;
589 rc_contexts = g_slist_prepend (rc_contexts, settings->rc_context);
592 return settings->rc_context;
596 gtk_rc_parse_named (GtkRcContext *context,
601 const gchar *home_dir;
605 subpath = g_strconcat ("gtk-2.0-", type,
606 G_DIR_SEPARATOR_S "gtkrc",
609 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
611 /* First look in the users home directory
613 home_dir = g_get_home_dir ();
616 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
617 if (!g_file_test (path, G_FILE_TEST_EXISTS))
626 gchar *theme_dir = gtk_rc_get_theme_dir ();
627 path = g_build_filename (theme_dir, name, subpath, NULL);
630 if (!g_file_test (path, G_FILE_TEST_EXISTS))
639 gtk_rc_context_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
647 gtk_rc_parse_default_files (GtkRcContext *context)
651 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
652 gtk_rc_context_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
658 static gboolean initialized = FALSE;
664 gtk_rc_add_initial_default_files ();
667 /* Default RC string */
668 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
669 " bg[NORMAL] = \"#eee1b3\"\n"
670 " fg[NORMAL] = \"#000000\"\n"
673 "style \"gtk-default-progress-bar-style\" {\n"
674 " bg[PRELIGHT] = \"#4b6983\"\n"
675 " fg[PRELIGHT] = \"#ffffff\"\n"
676 " bg[NORMAL] = \"#c4c2bd\"\n"
679 "style \"gtk-default-menu-bar-item-style\" {\n"
680 " GtkMenuItem::horizontal_padding = 5\n"
683 "style \"gtk-default-menu-item-style\" {\n"
684 " bg[PRELIGHT] = \"#4b6983\"\n"
685 " fg[PRELIGHT] = \"#ffffff\"\n"
686 " base[PRELIGHT] = \"#4b6983\"\n"
687 " text[PRELIGHT] = \"#ffffff\"\n"
690 "class \"GtkProgressBar\" style : gtk \"gtk-default-progress-bar-style\"\n"
691 "widget \"gtk-tooltips*\" style : gtk \"gtk-default-tooltips-style\"\n"
692 "class \"GtkMenuItem\" style : gtk \"gtk-default-menu-item-style\"\n"
693 "widget_class \"*.GtkMenuItem.*\" style : gtk \"gtk-default-menu-item-style\"\n"
694 "widget_class \"*.GtkAccelMenuItem.*\" style : gtk \"gtk-default-menu-item-style\"\n"
695 "widget_class \"*.GtkRadioMenuItem.*\" style : gtk \"gtk-default-menu-item-style\"\n"
696 "widget_class \"*.GtkCheckMenuItem.*\" style : gtk \"gtk-default-menu-item-style\"\n"
697 "widget_class \"*.GtkImageMenuItem.*\" style : gtk \"gtk-default-menu-item-style\"\n"
698 "widget_class \"*.GtkSeparatorMenuItem.*\" style : gtk \"gtk-default-menu-item-style\"\n"
699 "widget_class \"*.GtkCellViewMenuItem.*\" style : gtk \"gtk-default-menu-item-style\"\n"
700 "widget_class \"*GtkMenuBar*GtkMenuItem\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
701 "widget_class \"*GtkMenuBar*GtkAccelMenuItem\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
702 "widget_class \"*GtkMenuBar*GtkRadioMenuItem\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
703 "widget_class \"*GtkMenuBar*GtkCheckMenuItem\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
704 "widget_class \"*GtkMenuBar*GtkImageMenuItem\" style : gtk \"gtk-default-menu-bar-item-style\"\n"
709 gtk_rc_context_parse_string (GtkRcContext *context,
710 const gchar *rc_string)
712 gtk_rc_parse_any (context, "-", -1, rc_string);
716 gtk_rc_parse_string (const gchar *rc_string)
721 g_return_if_fail (rc_string != NULL);
723 rc_file = g_new (GtkRcFile, 1);
724 rc_file->is_string = TRUE;
725 rc_file->name = g_strdup (rc_string);
726 rc_file->canonical_name = NULL;
727 rc_file->directory = NULL;
729 rc_file->reload = TRUE;
731 global_rc_files = g_slist_append (global_rc_files, rc_file);
733 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
734 gtk_rc_context_parse_string (tmp_list->data, rc_string);
738 add_to_rc_file_list (GSList **rc_file_list,
739 const char *filename,
745 tmp_list = *rc_file_list;
748 rc_file = tmp_list->data;
749 if (!strcmp (rc_file->name, filename))
752 tmp_list = tmp_list->next;
755 rc_file = g_new (GtkRcFile, 1);
756 rc_file->is_string = FALSE;
757 rc_file->name = g_strdup (filename);
758 rc_file->canonical_name = NULL;
759 rc_file->directory = NULL;
761 rc_file->reload = reload;
763 *rc_file_list = g_slist_append (*rc_file_list, rc_file);
769 gtk_rc_context_parse_one_file (GtkRcContext *context,
770 const gchar *filename,
778 g_return_if_fail (filename != NULL);
780 saved_priority = context->default_priority;
781 context->default_priority = priority;
783 rc_file = add_to_rc_file_list (&context->rc_files, filename, reload);
785 if (!rc_file->canonical_name)
787 /* Get the absolute pathname */
789 if (g_path_is_absolute (rc_file->name))
790 rc_file->canonical_name = rc_file->name;
795 cwd = g_get_current_dir ();
796 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
800 rc_file->directory = g_path_get_dirname (rc_file->canonical_name);
803 /* If the file is already being parsed (recursion), do nothing
805 if (g_slist_find (current_files_stack, rc_file))
808 if (!lstat (rc_file->canonical_name, &statbuf))
812 rc_file->mtime = statbuf.st_mtime;
814 fd = open (rc_file->canonical_name, O_RDONLY);
818 /* Temporarily push information for this file on
819 * a stack of current files while parsing it.
821 current_files_stack = g_slist_prepend (current_files_stack, rc_file);
822 gtk_rc_parse_any (context, filename, fd, NULL);
823 current_files_stack = g_slist_delete_link (current_files_stack,
824 current_files_stack);
830 context->default_priority = saved_priority;
834 strchr_len (const gchar *str, gint len, char c)
848 gtk_rc_context_parse_file (GtkRcContext *context,
849 const gchar *filename,
853 gchar *locale_suffixes[2];
854 gint n_locale_suffixes = 0;
858 gboolean found = FALSE;
860 locale = _gtk_get_lc_ctype ();
862 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
864 /* Determine locale-specific suffixes for RC files.
866 length = strlen (locale);
868 p = strchr (locale, '@');
872 p = strchr_len (locale, length, '.');
876 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
878 p = strchr_len (locale, length, '_');
882 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
888 gtk_rc_context_parse_one_file (context, filename, priority, reload);
889 for (j = 0; j < n_locale_suffixes; j++)
893 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
894 if (g_file_test (name, G_FILE_TEST_EXISTS))
896 gtk_rc_context_parse_one_file (context, name, priority, FALSE);
903 g_free (locale_suffixes[j]);
908 gtk_rc_parse (const gchar *filename)
912 g_return_if_fail (filename != NULL);
914 add_to_rc_file_list (&global_rc_files, filename, TRUE);
916 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
917 gtk_rc_context_parse_file (tmp_list->data, filename, GTK_PATH_PRIO_RC, TRUE);
920 /* Handling of RC styles */
923 gtk_rc_style_get_type (void)
925 static GType rc_style_type = 0;
929 static const GTypeInfo rc_style_info =
931 sizeof (GtkRcStyleClass),
932 (GBaseInitFunc) NULL,
933 (GBaseFinalizeFunc) NULL,
934 (GClassInitFunc) gtk_rc_style_class_init,
935 NULL, /* class_finalize */
936 NULL, /* class_data */
939 (GInstanceInitFunc) gtk_rc_style_init,
942 rc_style_type = g_type_register_static (G_TYPE_OBJECT, "GtkRcStyle",
946 return rc_style_type;
950 gtk_rc_style_init (GtkRcStyle *style)
955 style->font_desc = NULL;
957 for (i = 0; i < 5; i++)
959 static const GdkColor init_color = { 0, 0, 0, 0, };
961 style->bg_pixmap_name[i] = NULL;
962 style->color_flags[i] = 0;
963 style->fg[i] = init_color;
964 style->bg[i] = init_color;
965 style->text[i] = init_color;
966 style->base[i] = init_color;
968 style->xthickness = -1;
969 style->ythickness = -1;
970 style->rc_properties = NULL;
972 style->rc_style_lists = NULL;
973 style->icon_factories = NULL;
977 gtk_rc_style_class_init (GtkRcStyleClass *klass)
979 GObjectClass *object_class = G_OBJECT_CLASS (klass);
981 parent_class = g_type_class_peek_parent (klass);
983 object_class->finalize = gtk_rc_style_finalize;
986 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
987 klass->merge = gtk_rc_style_real_merge;
988 klass->create_style = gtk_rc_style_real_create_style;
992 gtk_rc_style_finalize (GObject *object)
994 GSList *tmp_list1, *tmp_list2;
995 GtkRcStyle *rc_style;
998 rc_style = GTK_RC_STYLE (object);
1001 g_free (rc_style->name);
1002 if (rc_style->font_desc)
1003 pango_font_description_free (rc_style->font_desc);
1005 for (i = 0; i < 5; i++)
1006 if (rc_style->bg_pixmap_name[i])
1007 g_free (rc_style->bg_pixmap_name[i]);
1009 /* Now remove all references to this rc_style from
1012 tmp_list1 = rc_style->rc_style_lists;
1015 GSList *rc_styles = tmp_list1->data;
1016 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
1017 g_object_unref (style);
1019 /* Remove the list of styles from the other rc_styles
1022 tmp_list2 = rc_styles;
1025 GtkRcStyle *other_style = tmp_list2->data;
1027 if (other_style != rc_style)
1028 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
1030 tmp_list2 = tmp_list2->next;
1033 /* And from the hash table itself
1035 g_hash_table_remove (realized_style_ht, rc_styles);
1036 g_slist_free (rc_styles);
1038 tmp_list1 = tmp_list1->next;
1040 g_slist_free (rc_style->rc_style_lists);
1042 if (rc_style->rc_properties)
1046 for (i = 0; i < rc_style->rc_properties->len; i++)
1048 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1050 g_free (node->origin);
1051 g_value_unset (&node->value);
1053 g_array_free (rc_style->rc_properties, TRUE);
1054 rc_style->rc_properties = NULL;
1057 tmp_list1 = rc_style->icon_factories;
1060 g_object_unref (tmp_list1->data);
1061 tmp_list1 = tmp_list1->next;
1063 g_slist_free (rc_style->icon_factories);
1065 G_OBJECT_CLASS (parent_class)->finalize (object);
1069 gtk_rc_style_new (void)
1073 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1079 * gtk_rc_style_copy:
1080 * @orig: the style to copy
1082 * Makes a copy of the specified #GtkRcStyle. This function
1083 * will correctly copy an RC style that is a member of a class
1084 * derived from #GtkRcStyle.
1086 * Return value: the resulting #GtkRcStyle
1089 gtk_rc_style_copy (GtkRcStyle *orig)
1093 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1095 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1096 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1102 gtk_rc_style_ref (GtkRcStyle *rc_style)
1104 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1106 g_object_ref (rc_style);
1110 gtk_rc_style_unref (GtkRcStyle *rc_style)
1112 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1114 g_object_unref (rc_style);
1118 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1120 return g_object_new (G_OBJECT_TYPE (style), NULL);
1124 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1125 gconstpointer bsearch_node2)
1127 const GtkRcProperty *prop1 = bsearch_node1;
1128 const GtkRcProperty *prop2 = bsearch_node2;
1130 if (prop1->type_name == prop2->type_name)
1131 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1133 return prop1->type_name < prop2->type_name ? -1 : 1;
1137 insert_rc_property (GtkRcStyle *style,
1138 GtkRcProperty *property,
1142 GtkRcProperty *new_property = NULL;
1143 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1145 key.type_name = property->type_name;
1146 key.property_name = property->property_name;
1148 if (!style->rc_properties)
1149 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1152 while (i < style->rc_properties->len)
1154 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1160 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1162 g_free (new_property->origin);
1163 g_value_unset (&new_property->value);
1165 *new_property = key;
1179 g_array_insert_val (style->rc_properties, i, key);
1180 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1183 new_property->origin = g_strdup (property->origin);
1184 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1185 g_value_copy (&property->value, &new_property->value);
1189 gtk_rc_style_real_merge (GtkRcStyle *dest,
1194 for (i = 0; i < 5; i++)
1196 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1197 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1199 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1200 src->color_flags[i] & GTK_RC_FG)
1202 dest->fg[i] = src->fg[i];
1203 dest->color_flags[i] |= GTK_RC_FG;
1205 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1206 src->color_flags[i] & GTK_RC_BG)
1208 dest->bg[i] = src->bg[i];
1209 dest->color_flags[i] |= GTK_RC_BG;
1211 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1212 src->color_flags[i] & GTK_RC_TEXT)
1214 dest->text[i] = src->text[i];
1215 dest->color_flags[i] |= GTK_RC_TEXT;
1217 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1218 src->color_flags[i] & GTK_RC_BASE)
1220 dest->base[i] = src->base[i];
1221 dest->color_flags[i] |= GTK_RC_BASE;
1225 if (dest->xthickness < 0 && src->xthickness >= 0)
1226 dest->xthickness = src->xthickness;
1227 if (dest->ythickness < 0 && src->ythickness >= 0)
1228 dest->ythickness = src->ythickness;
1232 if (!dest->font_desc)
1233 dest->font_desc = pango_font_description_copy (src->font_desc);
1235 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1238 if (src->rc_properties)
1242 for (i = 0; i < src->rc_properties->len; i++)
1243 insert_rc_property (dest,
1244 &g_array_index (src->rc_properties, GtkRcProperty, i),
1250 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1252 return gtk_style_new ();
1256 gtk_rc_clear_hash_node (gpointer key,
1260 gtk_rc_style_unref (data);
1264 gtk_rc_free_rc_sets (GSList *slist)
1270 rc_set = slist->data;
1271 g_pattern_spec_free (rc_set->pspec);
1274 slist = slist->next;
1279 gtk_rc_clear_styles (GtkRcContext *context)
1281 /* Clear out all old rc_styles */
1283 if (context->rc_style_ht)
1285 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1286 g_hash_table_destroy (context->rc_style_ht);
1287 context->rc_style_ht = NULL;
1290 gtk_rc_free_rc_sets (context->rc_sets_widget);
1291 g_slist_free (context->rc_sets_widget);
1292 context->rc_sets_widget = NULL;
1294 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1295 g_slist_free (context->rc_sets_widget_class);
1296 context->rc_sets_widget_class = NULL;
1298 gtk_rc_free_rc_sets (context->rc_sets_class);
1299 g_slist_free (context->rc_sets_class);
1300 context->rc_sets_class = NULL;
1303 /* Reset all our widgets. Also, we have to invalidate cached icons in
1304 * icon sets so they get re-rendered.
1307 gtk_rc_reset_widgets (GtkSettings *settings)
1309 GList *list, *toplevels;
1311 _gtk_icon_set_invalidate_caches ();
1313 toplevels = gtk_window_list_toplevels ();
1314 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1316 for (list = toplevels; list; list = list->next)
1318 if (gtk_widget_get_screen (list->data) == settings->screen)
1320 gtk_widget_reset_rc_styles (list->data);
1321 g_object_unref (list->data);
1324 g_list_free (toplevels);
1328 gtk_rc_clear_realized_style (gpointer key,
1332 GSList *rc_styles = key;
1333 GSList *tmp_list = rc_styles;
1337 GtkRcStyle *rc_style = tmp_list->data;
1339 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1341 tmp_list = tmp_list->next;
1344 g_slist_free (rc_styles);
1348 * gtk_rc_reset_styles:
1349 * @settings: a #GtkSettings
1351 * This function recomputes the styles for all widgets that use a
1352 * particular #GtkSettings object. (There is one #GtkSettings object
1353 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1354 * when some global parameter has changed that affects the appearance
1355 * of all widgets, because when a widget gets a new style, it will
1356 * both redraw and recompute any cached information about its
1357 * appearance. As an example, it is used when the default font size
1358 * set by the operating system changes. Note that this function
1359 * doesn't affect widgets that have a style set explicitely on them
1360 * with gtk_widget_set_style().
1365 gtk_rc_reset_styles (GtkSettings *settings)
1367 GtkRcContext *context;
1368 gboolean reset = FALSE;
1370 g_return_if_fail (GTK_IS_SETTINGS (settings));
1372 context = gtk_rc_context_get (settings);
1374 if (context->default_style)
1376 g_object_unref (context->default_style);
1377 context->default_style = NULL;
1381 /* Clear out styles that have been looked up already
1383 if (realized_style_ht)
1385 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1386 g_hash_table_destroy (realized_style_ht);
1387 realized_style_ht = NULL;
1392 gtk_rc_reset_widgets (settings);
1396 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1398 GtkRcContext *context;
1399 gchar *new_font_name;
1401 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1403 context = gtk_rc_context_get (settings);
1405 g_object_get (context->settings,
1406 "gtk-font-name", &new_font_name,
1409 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1411 g_free (context->font_name);
1412 context->font_name = g_strdup (new_font_name);
1414 gtk_rc_reset_styles (settings);
1417 g_free (new_font_name);
1419 return context->font_name;
1423 * gtk_rc_reparse_all_for_settings:
1424 * @settings: a #GtkSettings
1425 * @force_load: load whether or not anything changed
1427 * If the modification time on any previously read file
1428 * for the given #GtkSettings has changed, discard all style information
1429 * and then reread all previously read RC files.
1431 * Return value: %TRUE if the files were reread.
1434 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1435 gboolean force_load)
1437 gboolean mtime_modified = FALSE;
1440 GtkRcContext *context;
1442 struct stat statbuf;
1444 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1446 context = gtk_rc_context_get (settings);
1450 /* Check through and see if any of the RC's have had their
1451 * mtime modified. If so, reparse everything.
1453 tmp_list = context->rc_files;
1456 rc_file = tmp_list->data;
1458 if (!rc_file->is_string)
1460 if (!lstat (rc_file->name, &statbuf) &&
1461 (statbuf.st_mtime > rc_file->mtime))
1463 mtime_modified = TRUE;
1468 tmp_list = tmp_list->next;
1472 if (force_load || mtime_modified)
1474 _gtk_binding_reset_parsed ();
1475 gtk_rc_clear_styles (context);
1476 g_object_freeze_notify (G_OBJECT (context->settings));
1478 _gtk_settings_reset_rc_values (context->settings);
1479 tmp_list = context->rc_files;
1482 rc_file = tmp_list->data;
1484 if (rc_file->canonical_name != rc_file->name)
1485 g_free (rc_file->canonical_name);
1486 g_free (rc_file->directory);
1487 g_free (rc_file->name);
1490 tmp_list = tmp_list->next;
1493 g_slist_free (context->rc_files);
1494 context->rc_files = NULL;
1496 gtk_rc_parse_default_files (context);
1498 tmp_list = global_rc_files;
1501 rc_file = tmp_list->data;
1503 if (rc_file->is_string)
1504 gtk_rc_context_parse_string (context, rc_file->name);
1506 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1508 tmp_list = tmp_list->next;
1511 g_free (context->theme_name);
1512 g_free (context->key_theme_name);
1514 g_object_get (context->settings,
1515 "gtk-theme-name", &context->theme_name,
1516 "gtk-key-theme-name", &context->key_theme_name,
1519 if (context->theme_name && context->theme_name[0])
1520 gtk_rc_parse_named (context, context->theme_name, NULL);
1521 if (context->key_theme_name && context->key_theme_name[0])
1522 gtk_rc_parse_named (context, context->key_theme_name, "key");
1524 g_object_thaw_notify (G_OBJECT (context->settings));
1526 gtk_rc_reset_widgets (context->settings);
1529 return force_load || mtime_modified;
1533 * gtk_rc_reparse_all:
1535 * If the modification time on any previously read file for the
1536 * default #GtkSettings has changed, discard all style information
1537 * and then reread all previously read RC files.
1539 * Return value: %TRUE if the files were reread.
1542 gtk_rc_reparse_all (void)
1545 gboolean result = FALSE;
1547 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1549 GtkRcContext *context = tmp_list->data;
1550 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1558 gtk_rc_styles_match (GSList *rc_styles,
1562 const gchar *path_reversed)
1569 rc_set = sets->data;
1572 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1573 rc_styles = g_slist_append (rc_styles, rc_set);
1580 rc_set_compare (gconstpointer a, gconstpointer b)
1582 const GtkRcSet *set_a = a;
1583 const GtkRcSet *set_b = b;
1585 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1589 sort_and_dereference_sets (GSList *styles)
1593 /* At this point, the list of sets is ordered by:
1595 * a) 'widget' patterns are earlier than 'widget_class' patterns
1596 * which are ealier than 'class' patterns.
1597 * a) For two matches for class patterns, a match to a child type
1598 * is before a match to a parent type
1599 * c) a match later in the RC file (or in a later RC file) is before a
1600 * match earlier in the RC file.
1602 * With a) taking precedence over b) which takes precendence over c).
1604 * Now sort by priority, which has the highest precendence for sort order
1606 styles = g_slist_sort (styles, rc_set_compare);
1608 /* Make styles->data = styles->data->rc_style
1613 GtkRcSet *set = tmp_list->data;
1614 tmp_list->data = set->rc_style;
1615 tmp_list = tmp_list->next;
1623 * @widget: a #GtkWidget
1625 * Finds all matching RC styles for a given widget,
1626 * composites them together, and then creates a
1627 * #GtkStyle representing the composite appearance.
1628 * (GTK+ actually keeps a cache of previously
1629 * created styles, so a new style may not be
1632 * Returns: the resulting style. No refcount is added
1633 * to the returned style, so if you want to save this
1634 * style around, you should add a reference yourself.
1637 gtk_rc_get_style (GtkWidget *widget)
1639 GtkRcStyle *widget_rc_style;
1640 GSList *rc_styles = NULL;
1641 GtkRcContext *context;
1643 static guint rc_style_key_id = 0;
1645 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1647 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1649 /* We allow the specification of a single rc style to be bound
1650 * tightly to a widget, for application modifications
1652 if (!rc_style_key_id)
1653 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1655 if (context->rc_sets_widget)
1657 gchar *path, *path_reversed;
1660 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1661 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1663 g_free (path_reversed);
1666 if (context->rc_sets_widget_class)
1668 gchar *path, *path_reversed;
1671 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1672 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1674 g_free (path_reversed);
1677 if (context->rc_sets_class)
1681 type = G_TYPE_FROM_INSTANCE (widget);
1685 gchar *path_reversed;
1688 path = g_type_name (type);
1689 path_length = strlen (path);
1690 path_reversed = g_strdup (path);
1691 g_strreverse (path_reversed);
1693 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1694 g_free (path_reversed);
1696 type = g_type_parent (type);
1700 rc_styles = sort_and_dereference_sets (rc_styles);
1702 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
1704 if (widget_rc_style)
1705 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1708 return gtk_rc_init_style (context, rc_styles);
1711 if (!context->default_style)
1713 context->default_style = gtk_style_new ();
1714 _gtk_style_init_for_settings (context->default_style, context->settings);
1717 return context->default_style;
1722 * gtk_rc_get_style_by_paths:
1723 * @settings: a #GtkSettings object
1724 * @widget_path: the widget path to use when looking up the style, or %NULL
1725 * if no matching against the widget path should be done
1726 * @class_path: the class path to use when looking up the style, or %NULL
1727 * if no matching against the class path should be done.
1728 * @type: a type that will be used along with parent types of this type
1729 * when matching against class styles, or #G_TYPE_NONE
1731 * Creates up a #GtkStyle from styles defined in a RC file by providing
1732 * the raw components used in matching. This function may be useful
1733 * when creating pseudo-widgets that should be themed like widgets but
1734 * don't actually have corresponding GTK+ widgets. An example of this
1735 * would be items inside a GNOME canvas widget.
1737 * The action of gtk_rc_get_style() is similar to:
1738 * <informalexample><programlisting>
1739 * gtk_widget_path (widget, NULL, &path, NULL);
1740 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
1741 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
1742 * G_OBJECT_TYPE (widget));
1743 * </programlisting></informalexample>
1745 * Return value: A style created by matching with the supplied paths,
1746 * or %NULL if nothing matching was specified and the default style should
1747 * be used. The returned value is owned by GTK+ as part of an internal cache,
1748 * so you must call g_object_ref() on the returned value if you want to
1749 * keep a reference to it.
1752 gtk_rc_get_style_by_paths (GtkSettings *settings,
1753 const char *widget_path,
1754 const char *class_path,
1757 /* We duplicate the code from above to avoid slowing down the above
1758 * by generating paths when we don't need them. I don't know if
1759 * this is really worth it.
1761 GSList *rc_styles = NULL;
1762 GtkRcContext *context;
1764 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1766 context = gtk_rc_context_get (settings);
1768 if (widget_path && context->rc_sets_widget)
1770 gchar *path_reversed;
1773 path_length = strlen (widget_path);
1774 path_reversed = g_strdup (widget_path);
1775 g_strreverse (path_reversed);
1777 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, widget_path, path_reversed);
1778 g_free (path_reversed);
1781 if (class_path && context->rc_sets_widget_class)
1783 gchar *path_reversed;
1786 path_length = strlen (class_path);
1787 path_reversed = g_strdup (class_path);
1788 g_strreverse (path_reversed);
1790 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, class_path, path_reversed);
1791 g_free (path_reversed);
1794 if (type != G_TYPE_NONE && context->rc_sets_class)
1799 gchar *path_reversed;
1802 path = g_type_name (type);
1803 path_length = strlen (path);
1804 path_reversed = g_strdup (path);
1805 g_strreverse (path_reversed);
1807 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1808 g_free (path_reversed);
1810 type = g_type_parent (type);
1814 rc_styles = sort_and_dereference_sets (rc_styles);
1817 return gtk_rc_init_style (context, rc_styles);
1823 gtk_rc_add_rc_sets (GSList *slist,
1824 GtkRcStyle *rc_style,
1825 const gchar *pattern)
1827 GtkRcStyle *new_style;
1831 new_style = gtk_rc_style_new ();
1832 *new_style = *rc_style;
1833 new_style->name = g_strdup (rc_style->name);
1834 if (rc_style->font_desc)
1835 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
1837 for (i = 0; i < 5; i++)
1838 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1840 rc_set = g_new (GtkRcSet, 1);
1841 rc_set->pspec = g_pattern_spec_new (pattern);
1842 rc_set->rc_style = rc_style;
1844 return g_slist_prepend (slist, rc_set);
1848 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
1849 const gchar *pattern)
1851 GtkRcContext *context;
1853 g_return_if_fail (rc_style != NULL);
1854 g_return_if_fail (pattern != NULL);
1856 context = gtk_rc_context_get (gtk_settings_get_default ());
1858 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern);
1862 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
1863 const gchar *pattern)
1865 GtkRcContext *context;
1867 g_return_if_fail (rc_style != NULL);
1868 g_return_if_fail (pattern != NULL);
1870 context = gtk_rc_context_get (gtk_settings_get_default ());
1872 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern);
1876 gtk_rc_add_class_style (GtkRcStyle *rc_style,
1877 const gchar *pattern)
1879 GtkRcContext *context;
1881 g_return_if_fail (rc_style != NULL);
1882 g_return_if_fail (pattern != NULL);
1884 context = gtk_rc_context_get (gtk_settings_get_default ());
1886 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern);
1890 gtk_rc_scanner_new (void)
1892 return g_scanner_new (>k_rc_scanner_config);
1896 gtk_rc_parse_any (GtkRcContext *context,
1897 const gchar *input_name,
1899 const gchar *input_string)
1905 scanner = gtk_rc_scanner_new ();
1909 g_assert (input_string == NULL);
1911 g_scanner_input_file (scanner, input_fd);
1915 g_assert (input_string != NULL);
1917 g_scanner_input_text (scanner, input_string, strlen (input_string));
1919 scanner->input_name = input_name;
1921 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1922 g_scanner_scope_add_symbol (scanner, 0, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1927 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1931 guint expected_token;
1933 expected_token = gtk_rc_parse_statement (context, scanner);
1935 if (expected_token != G_TOKEN_NONE)
1942 if (scanner->scope_id == 0)
1944 /* if we are in scope 0, we know the symbol names
1945 * that are associated with certain token values.
1946 * so we look them up to make the error messages
1949 if (expected_token > GTK_RC_TOKEN_INVALID &&
1950 expected_token < GTK_RC_TOKEN_LAST)
1952 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1953 if (symbols[i].token == expected_token)
1954 msg = symbols[i].name;
1956 msg = g_strconcat ("e.g. `", msg, "'", NULL);
1958 if (scanner->token > GTK_RC_TOKEN_INVALID &&
1959 scanner->token < GTK_RC_TOKEN_LAST)
1961 symbol_name = "???";
1962 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1963 if (symbols[i].token == scanner->token)
1964 symbol_name = symbols[i].name;
1967 g_scanner_unexp_token (scanner,
1980 g_scanner_destroy (scanner);
1984 gtk_rc_styles_hash (const GSList *rc_styles)
1991 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
1992 rc_styles = rc_styles->next;
1999 gtk_rc_styles_equal (const GSList *a,
2004 if (a->data != b->data)
2014 gtk_rc_style_hash (const gchar *name)
2020 result += (result << 3) + *name++;
2026 gtk_rc_style_equal (const gchar *a,
2029 return (strcmp (a, b) == 0);
2033 gtk_rc_style_find (GtkRcContext *context,
2036 if (context->rc_style_ht)
2037 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2043 gtk_rc_style_to_style (GtkRcContext *context,
2044 GtkRcStyle *rc_style)
2048 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2049 _gtk_style_init_for_settings (style, context->settings);
2051 style->rc_style = rc_style;
2053 gtk_rc_style_ref (rc_style);
2055 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2060 /* Reuses or frees rc_styles */
2062 gtk_rc_init_style (GtkRcContext *context,
2065 GtkStyle *style = NULL;
2068 g_return_val_if_fail (rc_styles != NULL, NULL);
2070 if (!realized_style_ht)
2071 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2072 (GEqualFunc) gtk_rc_styles_equal);
2074 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2078 GtkRcStyle *base_style = NULL;
2079 GtkRcStyle *proto_style;
2080 GtkRcStyleClass *proto_style_class;
2082 GType rc_style_type = GTK_TYPE_RC_STYLE;
2084 /* Find the the first style where the RC file specified engine "" {}
2085 * or the first derived style and use that to create the
2086 * merged style. If we only have raw GtkRcStyles, use the first
2087 * style to create the merged style.
2089 base_style = rc_styles->data;
2090 tmp_styles = rc_styles;
2093 GtkRcStyle *rc_style = tmp_styles->data;
2095 if (rc_style->engine_specified ||
2096 G_OBJECT_TYPE (rc_style) != rc_style_type)
2098 base_style = rc_style;
2102 tmp_styles = tmp_styles->next;
2105 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2106 proto_style = proto_style_class->create_rc_style (base_style);
2108 tmp_styles = rc_styles;
2111 GtkRcStyle *rc_style = tmp_styles->data;
2114 proto_style_class->merge (proto_style, rc_style);
2116 /* Point from each rc_style to the list of styles */
2117 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2118 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2120 factories = g_slist_copy (rc_style->icon_factories);
2126 while (iter != NULL)
2128 g_object_ref (iter->data);
2129 iter = g_slist_next (iter);
2132 proto_style->icon_factories = g_slist_concat (proto_style->icon_factories,
2137 tmp_styles = tmp_styles->next;
2140 for (i = 0; i < 5; i++)
2141 if (proto_style->bg_pixmap_name[i] &&
2142 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2144 g_free (proto_style->bg_pixmap_name[i]);
2145 proto_style->bg_pixmap_name[i] = NULL;
2148 style = gtk_rc_style_to_style (context, proto_style);
2149 gtk_rc_style_unref (proto_style);
2151 g_hash_table_insert (realized_style_ht, rc_styles, style);
2154 g_slist_free (rc_styles);
2159 /*********************
2160 * Parsing functions *
2161 *********************/
2164 rc_parse_token_or_compound (GScanner *scanner,
2166 GTokenType delimiter)
2168 guint token = g_scanner_get_next_token (scanner);
2170 /* we either scan a single token (skipping comments)
2171 * or a compund statement.
2172 * compunds are enclosed in (), [] or {} braces, we read
2173 * them in via deep recursion.
2180 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2183 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2185 case G_TOKEN_STRING:
2186 string = g_strescape (scanner->value.v_string, NULL);
2187 g_string_append (gstring, " \"");
2188 g_string_append (gstring, string);
2189 g_string_append_c (gstring, '"');
2192 case G_TOKEN_IDENTIFIER:
2193 g_string_append_c (gstring, ' ');
2194 g_string_append (gstring, scanner->value.v_identifier);
2196 case G_TOKEN_COMMENT_SINGLE:
2197 case G_TOKEN_COMMENT_MULTI:
2198 return rc_parse_token_or_compound (scanner, gstring, delimiter);
2199 case G_TOKEN_LEFT_PAREN:
2200 g_string_append_c (gstring, ' ');
2201 g_string_append_c (gstring, token);
2202 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_PAREN);
2203 if (token != G_TOKEN_NONE)
2206 case G_TOKEN_LEFT_CURLY:
2207 g_string_append_c (gstring, ' ');
2208 g_string_append_c (gstring, token);
2209 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_CURLY);
2210 if (token != G_TOKEN_NONE)
2213 case G_TOKEN_LEFT_BRACE:
2214 g_string_append_c (gstring, ' ');
2215 g_string_append_c (gstring, token);
2216 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_BRACE);
2217 if (token != G_TOKEN_NONE)
2221 if (token >= 256 || token < 1)
2222 return delimiter ? delimiter : G_TOKEN_STRING;
2223 g_string_append_c (gstring, ' ');
2224 g_string_append_c (gstring, token);
2225 if (token == delimiter)
2226 return G_TOKEN_NONE;
2230 return G_TOKEN_NONE;
2232 return rc_parse_token_or_compound (scanner, gstring, delimiter);
2236 gtk_rc_parse_assignment (GScanner *scanner,
2237 GtkRcProperty *prop)
2239 gboolean scan_identifier = scanner->config->scan_identifier;
2240 gboolean scan_symbols = scanner->config->scan_symbols;
2241 gboolean identifier_2_string = scanner->config->identifier_2_string;
2242 gboolean char_2_token = scanner->config->char_2_token;
2243 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2244 gboolean numbers_2_int = scanner->config->numbers_2_int;
2245 gboolean negate = FALSE;
2248 /* check that this is an assignment */
2249 if (g_scanner_get_next_token (scanner) != '=')
2252 /* adjust scanner mode */
2253 scanner->config->scan_identifier = TRUE;
2254 scanner->config->scan_symbols = FALSE;
2255 scanner->config->identifier_2_string = FALSE;
2256 scanner->config->char_2_token = TRUE;
2257 scanner->config->scan_identifier_NULL = FALSE;
2258 scanner->config->numbers_2_int = TRUE;
2260 /* record location */
2261 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2263 /* parse optional sign */
2264 if (g_scanner_peek_next_token (scanner) == '-')
2266 g_scanner_get_next_token (scanner); /* eat sign */
2270 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an unparsed compund */
2271 token = g_scanner_peek_next_token (scanner);
2275 g_scanner_get_next_token (scanner);
2276 g_value_init (&prop->value, G_TYPE_LONG);
2277 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2278 token = G_TOKEN_NONE;
2281 g_scanner_get_next_token (scanner);
2282 g_value_init (&prop->value, G_TYPE_DOUBLE);
2283 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2284 token = G_TOKEN_NONE;
2286 case G_TOKEN_STRING:
2287 g_scanner_get_next_token (scanner);
2289 token = G_TOKEN_INT;
2292 g_value_init (&prop->value, G_TYPE_STRING);
2293 g_value_set_string (&prop->value, scanner->value.v_string);
2294 token = G_TOKEN_NONE;
2297 case G_TOKEN_IDENTIFIER:
2298 case G_TOKEN_LEFT_PAREN:
2299 case G_TOKEN_LEFT_CURLY:
2300 case G_TOKEN_LEFT_BRACE:
2303 GString *gstring = g_string_new (NULL);
2305 token = rc_parse_token_or_compound (scanner, gstring, 0);
2306 if (token == G_TOKEN_NONE)
2308 g_string_append_c (gstring, ' ');
2309 g_value_init (&prop->value, G_TYPE_GSTRING);
2310 g_value_set_static_boxed (&prop->value, gstring);
2313 g_string_free (gstring, TRUE);
2318 g_scanner_get_next_token (scanner);
2319 token = G_TOKEN_INT;
2323 /* restore scanner mode */
2324 scanner->config->scan_identifier = scan_identifier;
2325 scanner->config->scan_symbols = scan_symbols;
2326 scanner->config->identifier_2_string = identifier_2_string;
2327 scanner->config->char_2_token = char_2_token;
2328 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2329 scanner->config->numbers_2_int = numbers_2_int;
2335 is_c_identifier (const gchar *string)
2338 gboolean is_varname;
2340 is_varname = strchr (G_CSET_a_2_z G_CSET_A_2_Z "_", string[0]) != NULL;
2341 for (p = string + 1; *p && is_varname; p++)
2342 is_varname &= strchr (G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_-", *p) != NULL;
2348 parse_include_file (GtkRcContext *context,
2350 const gchar *filename)
2352 char *to_parse = NULL;
2354 if (g_path_is_absolute (filename))
2356 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2357 * don't print an error in this case.
2359 to_parse = g_strdup (filename);
2363 /* if a relative path, we look relative to all the RC files in the
2364 * include stack. We require the file to be found in this case
2365 * so we can give meaningful error messages, and because on reparsing
2366 * non-absolute paths don't make sense.
2368 GSList *tmp_list = current_files_stack;
2371 GtkRcFile *curfile = tmp_list->data;
2372 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2374 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2382 tmp_list = tmp_list->next;
2388 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2393 g_scanner_warn (scanner,
2394 _("Unable to find include file: \"%s\""),
2401 gtk_rc_parse_statement (GtkRcContext *context,
2406 token = g_scanner_peek_next_token (scanner);
2409 case GTK_RC_TOKEN_INCLUDE:
2410 token = g_scanner_get_next_token (scanner);
2411 if (token != GTK_RC_TOKEN_INCLUDE)
2412 return GTK_RC_TOKEN_INCLUDE;
2413 token = g_scanner_get_next_token (scanner);
2414 if (token != G_TOKEN_STRING)
2415 return G_TOKEN_STRING;
2416 parse_include_file (context, scanner, scanner->value.v_string);
2417 return G_TOKEN_NONE;
2419 case GTK_RC_TOKEN_STYLE:
2420 return gtk_rc_parse_style (context, scanner);
2422 case GTK_RC_TOKEN_BINDING:
2423 return gtk_binding_parse_binding (scanner);
2425 case GTK_RC_TOKEN_PIXMAP_PATH:
2426 return gtk_rc_parse_pixmap_path (context, scanner);
2428 case GTK_RC_TOKEN_WIDGET:
2429 return gtk_rc_parse_path_pattern (context, scanner);
2431 case GTK_RC_TOKEN_WIDGET_CLASS:
2432 return gtk_rc_parse_path_pattern (context, scanner);
2434 case GTK_RC_TOKEN_CLASS:
2435 return gtk_rc_parse_path_pattern (context, scanner);
2437 case GTK_RC_TOKEN_MODULE_PATH:
2438 return gtk_rc_parse_module_path (scanner);
2440 case GTK_RC_TOKEN_IM_MODULE_FILE:
2441 return gtk_rc_parse_im_module_file (scanner);
2443 case G_TOKEN_IDENTIFIER:
2444 if (is_c_identifier (scanner->next_value.v_identifier))
2446 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2449 g_scanner_get_next_token (scanner); /* eat identifier */
2450 name = g_strdup (scanner->value.v_identifier);
2452 token = gtk_rc_parse_assignment (scanner, &prop);
2453 if (token == G_TOKEN_NONE)
2455 GtkSettingsValue svalue;
2457 svalue.origin = prop.origin;
2458 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2459 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2460 _gtk_settings_set_property_value_from_rc (context->settings,
2464 g_free (prop.origin);
2465 if (G_VALUE_TYPE (&prop.value))
2466 g_value_unset (&prop.value);
2473 g_scanner_get_next_token (scanner);
2474 return G_TOKEN_IDENTIFIER;
2477 g_scanner_get_next_token (scanner);
2478 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2483 fixup_rc_set (GSList *list,
2489 GtkRcSet *set = list->data;
2490 if (set->rc_style == orig)
2491 set->rc_style = new;
2497 fixup_rc_sets (GtkRcContext *context,
2501 fixup_rc_set (context->rc_sets_widget, orig, new);
2502 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2503 fixup_rc_set (context->rc_sets_class, orig, new);
2507 gtk_rc_parse_style (GtkRcContext *context,
2510 GtkRcStyle *rc_style;
2511 GtkRcStyle *orig_style;
2512 GtkRcStyle *parent_style;
2515 GtkIconFactory *our_factory = NULL;
2517 token = g_scanner_get_next_token (scanner);
2518 if (token != GTK_RC_TOKEN_STYLE)
2519 return GTK_RC_TOKEN_STYLE;
2521 token = g_scanner_get_next_token (scanner);
2522 if (token != G_TOKEN_STRING)
2523 return G_TOKEN_STRING;
2525 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2527 orig_style = g_object_ref (rc_style);
2531 /* If there's a list, its first member is always the factory belonging
2534 if (rc_style && rc_style->icon_factories)
2535 our_factory = rc_style->icon_factories->data;
2539 rc_style = gtk_rc_style_new ();
2540 rc_style->name = g_strdup (scanner->value.v_string);
2542 for (i = 0; i < 5; i++)
2543 rc_style->bg_pixmap_name[i] = NULL;
2545 for (i = 0; i < 5; i++)
2546 rc_style->color_flags[i] = 0;
2549 token = g_scanner_peek_next_token (scanner);
2550 if (token == G_TOKEN_EQUAL_SIGN)
2552 token = g_scanner_get_next_token (scanner);
2554 token = g_scanner_get_next_token (scanner);
2555 if (token != G_TOKEN_STRING)
2557 token = G_TOKEN_STRING;
2561 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2566 for (i = 0; i < 5; i++)
2568 rc_style->color_flags[i] = parent_style->color_flags[i];
2569 rc_style->fg[i] = parent_style->fg[i];
2570 rc_style->bg[i] = parent_style->bg[i];
2571 rc_style->text[i] = parent_style->text[i];
2572 rc_style->base[i] = parent_style->base[i];
2575 rc_style->xthickness = parent_style->xthickness;
2576 rc_style->ythickness = parent_style->ythickness;
2578 if (parent_style->font_desc)
2580 if (rc_style->font_desc)
2581 pango_font_description_free (rc_style->font_desc);
2582 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
2585 if (parent_style->rc_properties)
2589 for (i = 0; i < parent_style->rc_properties->len; i++)
2590 insert_rc_property (rc_style,
2591 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
2595 for (i = 0; i < 5; i++)
2597 if (rc_style->bg_pixmap_name[i])
2598 g_free (rc_style->bg_pixmap_name[i]);
2599 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
2602 /* Append parent's factories, adding a ref to them */
2603 if (parent_style->icon_factories != NULL)
2605 /* Add a factory for ourselves if we have none,
2606 * in case we end up defining more stock icons.
2607 * I see no real way around this; we need to maintain
2608 * the invariant that the first factory in the list
2609 * is always our_factory, the one belonging to us,
2610 * and if we put parent factories in the list we can't
2611 * do that if the style is reopened.
2613 if (our_factory == NULL)
2615 our_factory = gtk_icon_factory_new ();
2616 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2620 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories,
2621 g_slist_copy (parent_style->icon_factories));
2623 factories = parent_style->icon_factories;
2624 while (factories != NULL)
2626 g_object_ref (factories->data);
2627 factories = factories->next;
2633 token = g_scanner_get_next_token (scanner);
2634 if (token != G_TOKEN_LEFT_CURLY)
2636 token = G_TOKEN_LEFT_CURLY;
2640 token = g_scanner_peek_next_token (scanner);
2641 while (token != G_TOKEN_RIGHT_CURLY)
2645 case GTK_RC_TOKEN_BG:
2646 token = gtk_rc_parse_bg (scanner, rc_style);
2648 case GTK_RC_TOKEN_FG:
2649 token = gtk_rc_parse_fg (scanner, rc_style);
2651 case GTK_RC_TOKEN_TEXT:
2652 token = gtk_rc_parse_text (scanner, rc_style);
2654 case GTK_RC_TOKEN_BASE:
2655 token = gtk_rc_parse_base (scanner, rc_style);
2657 case GTK_RC_TOKEN_XTHICKNESS:
2658 token = gtk_rc_parse_xthickness (scanner, rc_style);
2660 case GTK_RC_TOKEN_YTHICKNESS:
2661 token = gtk_rc_parse_ythickness (scanner, rc_style);
2663 case GTK_RC_TOKEN_BG_PIXMAP:
2664 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
2666 case GTK_RC_TOKEN_FONT:
2667 token = gtk_rc_parse_font (scanner, rc_style);
2669 case GTK_RC_TOKEN_FONTSET:
2670 token = gtk_rc_parse_fontset (scanner, rc_style);
2672 case GTK_RC_TOKEN_FONT_NAME:
2673 token = gtk_rc_parse_font_name (scanner, rc_style);
2675 case GTK_RC_TOKEN_ENGINE:
2676 token = gtk_rc_parse_engine (context, scanner, &rc_style);
2678 case GTK_RC_TOKEN_STOCK:
2679 if (our_factory == NULL)
2681 our_factory = gtk_icon_factory_new ();
2682 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2685 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
2687 case G_TOKEN_IDENTIFIER:
2688 if (is_c_identifier (scanner->next_value.v_identifier) &&
2689 scanner->next_value.v_identifier[0] >= 'A' &&
2690 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
2692 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2694 g_scanner_get_next_token (scanner); /* eat type name */
2695 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
2696 if (g_scanner_get_next_token (scanner) != ':' ||
2697 g_scanner_get_next_token (scanner) != ':')
2702 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
2703 !is_c_identifier (scanner->value.v_identifier))
2705 token = G_TOKEN_IDENTIFIER;
2709 /* it's important that we do the same canonification as GParamSpecPool here */
2710 g_strcanon (scanner->value.v_identifier, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2711 prop.property_name = g_quark_from_string (scanner->value.v_identifier);
2713 token = gtk_rc_parse_assignment (scanner, &prop);
2714 if (token == G_TOKEN_NONE)
2716 g_return_val_if_fail (prop.origin != NULL && G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
2717 insert_rc_property (rc_style, &prop, TRUE);
2720 g_free (prop.origin);
2721 if (G_VALUE_TYPE (&prop.value))
2722 g_value_unset (&prop.value);
2726 g_scanner_get_next_token (scanner);
2727 token = G_TOKEN_IDENTIFIER;
2731 g_scanner_get_next_token (scanner);
2732 token = G_TOKEN_RIGHT_CURLY;
2736 if (token != G_TOKEN_NONE)
2739 token = g_scanner_peek_next_token (scanner);
2740 } /* while (token != G_TOKEN_RIGHT_CURLY) */
2742 token = g_scanner_get_next_token (scanner);
2743 if (token != G_TOKEN_RIGHT_CURLY)
2745 token = G_TOKEN_RIGHT_CURLY;
2749 if (rc_style != orig_style)
2751 if (!context->rc_style_ht)
2752 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
2753 (GEqualFunc) gtk_rc_style_equal);
2755 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
2757 /* If we copied the data into a new rc style, fix up references to the old rc style
2758 * in bindings that we have.
2761 fixup_rc_sets (context, orig_style, rc_style);
2765 g_object_unref (orig_style);
2767 return G_TOKEN_NONE;
2770 if (rc_style != orig_style)
2771 gtk_rc_style_unref (rc_style);
2774 g_object_unref (orig_style);
2779 const GtkRcProperty*
2780 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
2782 GQuark property_name)
2784 GtkRcProperty *node = NULL;
2786 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
2788 if (rc_style->rc_properties)
2792 key.type_name = type_name;
2793 key.property_name = property_name;
2795 node = bsearch (&key,
2796 rc_style->rc_properties->data, rc_style->rc_properties->len,
2797 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
2804 gtk_rc_parse_bg (GScanner *scanner,
2810 token = g_scanner_get_next_token (scanner);
2811 if (token != GTK_RC_TOKEN_BG)
2812 return GTK_RC_TOKEN_BG;
2814 token = gtk_rc_parse_state (scanner, &state);
2815 if (token != G_TOKEN_NONE)
2818 token = g_scanner_get_next_token (scanner);
2819 if (token != G_TOKEN_EQUAL_SIGN)
2820 return G_TOKEN_EQUAL_SIGN;
2822 style->color_flags[state] |= GTK_RC_BG;
2823 return gtk_rc_parse_color (scanner, &style->bg[state]);
2827 gtk_rc_parse_fg (GScanner *scanner,
2833 token = g_scanner_get_next_token (scanner);
2834 if (token != GTK_RC_TOKEN_FG)
2835 return GTK_RC_TOKEN_FG;
2837 token = gtk_rc_parse_state (scanner, &state);
2838 if (token != G_TOKEN_NONE)
2841 token = g_scanner_get_next_token (scanner);
2842 if (token != G_TOKEN_EQUAL_SIGN)
2843 return G_TOKEN_EQUAL_SIGN;
2845 style->color_flags[state] |= GTK_RC_FG;
2846 return gtk_rc_parse_color (scanner, &style->fg[state]);
2850 gtk_rc_parse_text (GScanner *scanner,
2856 token = g_scanner_get_next_token (scanner);
2857 if (token != GTK_RC_TOKEN_TEXT)
2858 return GTK_RC_TOKEN_TEXT;
2860 token = gtk_rc_parse_state (scanner, &state);
2861 if (token != G_TOKEN_NONE)
2864 token = g_scanner_get_next_token (scanner);
2865 if (token != G_TOKEN_EQUAL_SIGN)
2866 return G_TOKEN_EQUAL_SIGN;
2868 style->color_flags[state] |= GTK_RC_TEXT;
2869 return gtk_rc_parse_color (scanner, &style->text[state]);
2873 gtk_rc_parse_base (GScanner *scanner,
2879 token = g_scanner_get_next_token (scanner);
2880 if (token != GTK_RC_TOKEN_BASE)
2881 return GTK_RC_TOKEN_BASE;
2883 token = gtk_rc_parse_state (scanner, &state);
2884 if (token != G_TOKEN_NONE)
2887 token = g_scanner_get_next_token (scanner);
2888 if (token != G_TOKEN_EQUAL_SIGN)
2889 return G_TOKEN_EQUAL_SIGN;
2891 style->color_flags[state] |= GTK_RC_BASE;
2892 return gtk_rc_parse_color (scanner, &style->base[state]);
2896 gtk_rc_parse_xthickness (GScanner *scanner,
2899 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
2900 return GTK_RC_TOKEN_XTHICKNESS;
2902 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2903 return G_TOKEN_EQUAL_SIGN;
2905 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2908 style->xthickness = scanner->value.v_int;
2910 return G_TOKEN_NONE;
2914 gtk_rc_parse_ythickness (GScanner *scanner,
2917 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
2918 return GTK_RC_TOKEN_YTHICKNESS;
2920 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2921 return G_TOKEN_EQUAL_SIGN;
2923 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2926 style->ythickness = scanner->value.v_int;
2928 return G_TOKEN_NONE;
2932 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
2934 GtkRcStyle *rc_style)
2940 token = g_scanner_get_next_token (scanner);
2941 if (token != GTK_RC_TOKEN_BG_PIXMAP)
2942 return GTK_RC_TOKEN_BG_PIXMAP;
2944 token = gtk_rc_parse_state (scanner, &state);
2945 if (token != G_TOKEN_NONE)
2948 token = g_scanner_get_next_token (scanner);
2949 if (token != G_TOKEN_EQUAL_SIGN)
2950 return G_TOKEN_EQUAL_SIGN;
2952 token = g_scanner_get_next_token (scanner);
2953 if (token != G_TOKEN_STRING)
2954 return G_TOKEN_STRING;
2956 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
2957 (strcmp (scanner->value.v_string, "<none>") == 0))
2958 pixmap_file = g_strdup (scanner->value.v_string);
2960 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
2961 scanner, scanner->value.v_string);
2965 if (rc_style->bg_pixmap_name[state])
2966 g_free (rc_style->bg_pixmap_name[state]);
2967 rc_style->bg_pixmap_name[state] = pixmap_file;
2970 return G_TOKEN_NONE;
2974 gtk_rc_check_pixmap_dir (const gchar *dir,
2975 const gchar *pixmap_file)
2979 buf = g_build_filename (dir, pixmap_file, NULL);
2981 if (g_file_test (buf, G_FILE_TEST_EXISTS))
2990 * gtk_rc_find_pixmap_in_path:
2991 * @settings: a #GtkSettings
2992 * @scanner: Scanner used to get line number information for the
2993 * warning message, or %NULL
2994 * @pixmap_file: name of the pixmap file to locate.
2996 * Looks up a file in pixmap path for the specified #GtkSettings.
2997 * If the file is not found, it outputs a warning message using
2998 * g_warning() and returns %NULL.
3000 * Return value: the filename.
3003 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3005 const gchar *pixmap_file)
3011 GtkRcContext *context = gtk_rc_context_get (settings);
3013 for (i = 0; (i < GTK_RC_MAX_PIXMAP_PATHS) && (context->pixmap_path[i] != NULL); i++)
3015 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3020 tmp_list = current_files_stack;
3023 GtkRcFile *curfile = tmp_list->data;
3024 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3028 tmp_list = tmp_list->next;
3032 g_scanner_warn (scanner,
3033 _("Unable to locate image file in pixmap_path: \"%s\""),
3036 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3043 * gtk_rc_find_module_in_path:
3044 * @module_file: name of a theme engine
3046 * Searches for a theme engine in the GTK+ search path. This function
3047 * is not useful for applications and should not be used.
3049 * Return value: The filename, if found (must be freed with g_free()),
3053 gtk_rc_find_module_in_path (const gchar *module_file)
3055 return _gtk_find_module (module_file, "engines");
3059 gtk_rc_parse_font (GScanner *scanner,
3060 GtkRcStyle *rc_style)
3064 token = g_scanner_get_next_token (scanner);
3065 if (token != GTK_RC_TOKEN_FONT)
3066 return GTK_RC_TOKEN_FONT;
3068 token = g_scanner_get_next_token (scanner);
3069 if (token != G_TOKEN_EQUAL_SIGN)
3070 return G_TOKEN_EQUAL_SIGN;
3072 token = g_scanner_get_next_token (scanner);
3073 if (token != G_TOKEN_STRING)
3074 return G_TOKEN_STRING;
3076 /* Ignore, do nothing */
3078 return G_TOKEN_NONE;
3082 gtk_rc_parse_fontset (GScanner *scanner,
3083 GtkRcStyle *rc_style)
3087 token = g_scanner_get_next_token (scanner);
3088 if (token != GTK_RC_TOKEN_FONTSET)
3089 return GTK_RC_TOKEN_FONTSET;
3091 token = g_scanner_get_next_token (scanner);
3092 if (token != G_TOKEN_EQUAL_SIGN)
3093 return G_TOKEN_EQUAL_SIGN;
3095 token = g_scanner_get_next_token (scanner);
3096 if (token != G_TOKEN_STRING)
3097 return G_TOKEN_STRING;
3099 /* Do nothing - silently ignore */
3101 return G_TOKEN_NONE;
3105 gtk_rc_parse_font_name (GScanner *scanner,
3106 GtkRcStyle *rc_style)
3110 token = g_scanner_get_next_token (scanner);
3111 if (token != GTK_RC_TOKEN_FONT_NAME)
3112 return GTK_RC_TOKEN_FONT;
3114 token = g_scanner_get_next_token (scanner);
3115 if (token != G_TOKEN_EQUAL_SIGN)
3116 return G_TOKEN_EQUAL_SIGN;
3118 token = g_scanner_get_next_token (scanner);
3119 if (token != G_TOKEN_STRING)
3120 return G_TOKEN_STRING;
3122 if (rc_style->font_desc)
3123 pango_font_description_free (rc_style->font_desc);
3125 rc_style->font_desc =
3126 pango_font_description_from_string (scanner->value.v_string);
3128 return G_TOKEN_NONE;
3132 gtk_rc_parse_engine (GtkRcContext *context,
3134 GtkRcStyle **rc_style)
3137 GtkThemeEngine *engine;
3138 guint result = G_TOKEN_NONE;
3139 GtkRcStyle *new_style = NULL;
3140 gboolean parsed_curlies = FALSE;
3142 token = g_scanner_get_next_token (scanner);
3143 if (token != GTK_RC_TOKEN_ENGINE)
3144 return GTK_RC_TOKEN_ENGINE;
3146 token = g_scanner_get_next_token (scanner);
3147 if (token != G_TOKEN_STRING)
3148 return G_TOKEN_STRING;
3150 if (!scanner->value.v_string[0])
3152 /* Support engine "" {} to mean override to the default engine
3154 token = g_scanner_get_next_token (scanner);
3155 if (token != G_TOKEN_LEFT_CURLY)
3156 return G_TOKEN_LEFT_CURLY;
3158 token = g_scanner_get_next_token (scanner);
3159 if (token != G_TOKEN_RIGHT_CURLY)
3160 return G_TOKEN_RIGHT_CURLY;
3162 parsed_curlies = TRUE;
3164 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3166 new_style = gtk_rc_style_new ();
3167 gtk_rc_style_real_merge (new_style, *rc_style);
3169 if ((*rc_style)->name)
3170 new_style->name = g_strdup ((*rc_style)->name);
3173 (*rc_style)->engine_specified = TRUE;
3177 engine = gtk_theme_engine_get (scanner->value.v_string);
3179 token = g_scanner_get_next_token (scanner);
3180 if (token != G_TOKEN_LEFT_CURLY)
3181 return G_TOKEN_LEFT_CURLY;
3185 GtkRcStyleClass *new_class;
3187 new_style = gtk_theme_engine_create_rc_style (engine);
3188 g_type_module_unuse (G_TYPE_MODULE (engine));
3190 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3192 new_class->merge (new_style, *rc_style);
3193 if ((*rc_style)->name)
3194 new_style->name = g_strdup ((*rc_style)->name);
3196 if (new_class->parse)
3198 parsed_curlies = TRUE;
3199 result = new_class->parse (new_style, context->settings, scanner);
3201 if (result != G_TOKEN_NONE)
3203 g_object_unref (new_style);
3210 if (!parsed_curlies)
3212 /* Skip over remainder, looking for nested {}'s
3216 result = G_TOKEN_RIGHT_CURLY;
3217 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3219 if (token == G_TOKEN_LEFT_CURLY)
3221 else if (token == G_TOKEN_RIGHT_CURLY)
3226 result = G_TOKEN_NONE;
3234 new_style->engine_specified = TRUE;
3236 g_object_unref (*rc_style);
3237 *rc_style = new_style;
3244 gtk_rc_parse_state (GScanner *scanner,
3245 GtkStateType *state)
3250 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3251 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3253 /* we don't know where we got called from, so we reset the scope here.
3254 * if we bail out due to errors, we *don't* reset the scope, so the
3255 * error messaging code can make sense of our tokens.
3257 old_scope = g_scanner_set_scope (scanner, 0);
3259 token = g_scanner_get_next_token (scanner);
3260 if (token != G_TOKEN_LEFT_BRACE)
3261 return G_TOKEN_LEFT_BRACE;
3263 token = g_scanner_get_next_token (scanner);
3266 case GTK_RC_TOKEN_ACTIVE:
3267 *state = GTK_STATE_ACTIVE;
3269 case GTK_RC_TOKEN_INSENSITIVE:
3270 *state = GTK_STATE_INSENSITIVE;
3272 case GTK_RC_TOKEN_NORMAL:
3273 *state = GTK_STATE_NORMAL;
3275 case GTK_RC_TOKEN_PRELIGHT:
3276 *state = GTK_STATE_PRELIGHT;
3278 case GTK_RC_TOKEN_SELECTED:
3279 *state = GTK_STATE_SELECTED;
3282 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3285 token = g_scanner_get_next_token (scanner);
3286 if (token != G_TOKEN_RIGHT_BRACE)
3287 return G_TOKEN_RIGHT_BRACE;
3289 g_scanner_set_scope (scanner, old_scope);
3291 return G_TOKEN_NONE;
3295 gtk_rc_parse_priority (GScanner *scanner,
3296 GtkPathPriorityType *priority)
3301 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3302 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3304 /* we don't know where we got called from, so we reset the scope here.
3305 * if we bail out due to errors, we *don't* reset the scope, so the
3306 * error messaging code can make sense of our tokens.
3308 old_scope = g_scanner_set_scope (scanner, 0);
3310 token = g_scanner_get_next_token (scanner);
3314 token = g_scanner_get_next_token (scanner);
3317 case GTK_RC_TOKEN_LOWEST:
3318 *priority = GTK_PATH_PRIO_LOWEST;
3320 case GTK_RC_TOKEN_GTK:
3321 *priority = GTK_PATH_PRIO_GTK;
3323 case GTK_RC_TOKEN_APPLICATION:
3324 *priority = GTK_PATH_PRIO_APPLICATION;
3326 case GTK_RC_TOKEN_THEME:
3327 *priority = GTK_PATH_PRIO_THEME;
3329 case GTK_RC_TOKEN_RC:
3330 *priority = GTK_PATH_PRIO_RC;
3332 case GTK_RC_TOKEN_HIGHEST:
3333 *priority = GTK_PATH_PRIO_HIGHEST;
3336 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3339 g_scanner_set_scope (scanner, old_scope);
3341 return G_TOKEN_NONE;
3345 gtk_rc_parse_color (GScanner *scanner,
3350 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3352 /* we don't need to set our own scope here, because
3353 * we don't need own symbols
3356 token = g_scanner_get_next_token (scanner);
3361 case G_TOKEN_LEFT_CURLY:
3362 token = g_scanner_get_next_token (scanner);
3363 if (token == G_TOKEN_INT)
3364 token_int = scanner->value.v_int;
3365 else if (token == G_TOKEN_FLOAT)
3366 token_int = scanner->value.v_float * 65535.0;
3368 return G_TOKEN_FLOAT;
3369 color->red = CLAMP (token_int, 0, 65535);
3371 token = g_scanner_get_next_token (scanner);
3372 if (token != G_TOKEN_COMMA)
3373 return G_TOKEN_COMMA;
3375 token = g_scanner_get_next_token (scanner);
3376 if (token == G_TOKEN_INT)
3377 token_int = scanner->value.v_int;
3378 else if (token == G_TOKEN_FLOAT)
3379 token_int = scanner->value.v_float * 65535.0;
3381 return G_TOKEN_FLOAT;
3382 color->green = CLAMP (token_int, 0, 65535);
3384 token = g_scanner_get_next_token (scanner);
3385 if (token != G_TOKEN_COMMA)
3386 return G_TOKEN_COMMA;
3388 token = g_scanner_get_next_token (scanner);
3389 if (token == G_TOKEN_INT)
3390 token_int = scanner->value.v_int;
3391 else if (token == G_TOKEN_FLOAT)
3392 token_int = scanner->value.v_float * 65535.0;
3394 return G_TOKEN_FLOAT;
3395 color->blue = CLAMP (token_int, 0, 65535);
3397 token = g_scanner_get_next_token (scanner);
3398 if (token != G_TOKEN_RIGHT_CURLY)
3399 return G_TOKEN_RIGHT_CURLY;
3400 return G_TOKEN_NONE;
3402 case G_TOKEN_STRING:
3403 if (!gdk_color_parse (scanner->value.v_string, color))
3405 g_scanner_warn (scanner, "Invalid color constant '%s'",
3406 scanner->value.v_string);
3407 return G_TOKEN_STRING;
3410 return G_TOKEN_NONE;
3413 return G_TOKEN_STRING;
3418 gtk_rc_parse_pixmap_path (GtkRcContext *context,
3423 token = g_scanner_get_next_token (scanner);
3424 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
3425 return GTK_RC_TOKEN_PIXMAP_PATH;
3427 token = g_scanner_get_next_token (scanner);
3428 if (token != G_TOKEN_STRING)
3429 return G_TOKEN_STRING;
3431 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
3433 return G_TOKEN_NONE;
3437 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
3439 const gchar *pix_path)
3442 gint start_offset = 0;
3446 /* free the old one, or just add to the old one ? */
3447 for (path_num = 0; context->pixmap_path[path_num]; path_num++)
3449 g_free (context->pixmap_path[path_num]);
3450 context->pixmap_path[path_num] = NULL;
3455 path_len = strlen (pix_path);
3457 for (end_offset = 0; end_offset <= path_len; end_offset++)
3459 if ((pix_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3460 (end_offset == path_len))
3462 gchar *path_element = g_strndup (pix_path + start_offset, end_offset - start_offset);
3463 if (g_path_is_absolute (path_element))
3465 context->pixmap_path[path_num] = path_element;
3467 context->pixmap_path[path_num] = NULL;
3471 g_warning (_("Pixmap path element: \"%s\" must be absolute, %s, line %d"),
3472 path_element, scanner->input_name, scanner->line);
3473 g_free (path_element);
3476 start_offset = end_offset + 1;
3482 gtk_rc_parse_module_path (GScanner *scanner)
3486 token = g_scanner_get_next_token (scanner);
3487 if (token != GTK_RC_TOKEN_MODULE_PATH)
3488 return GTK_RC_TOKEN_MODULE_PATH;
3490 token = g_scanner_get_next_token (scanner);
3491 if (token != G_TOKEN_STRING)
3492 return G_TOKEN_STRING;
3494 g_warning ("module_path directive is now ignored\n");
3496 return G_TOKEN_NONE;
3500 gtk_rc_parse_im_module_file (GScanner *scanner)
3504 token = g_scanner_get_next_token (scanner);
3505 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3506 return GTK_RC_TOKEN_IM_MODULE_FILE;
3508 token = g_scanner_get_next_token (scanner);
3509 if (token != G_TOKEN_STRING)
3510 return G_TOKEN_STRING;
3513 g_free (im_module_file);
3515 im_module_file = g_strdup (scanner->value.v_string);
3517 return G_TOKEN_NONE;
3521 gtk_rc_parse_path_pattern (GtkRcContext *context,
3525 GtkPathType path_type;
3527 gboolean is_binding;
3528 GtkPathPriorityType priority = context->default_priority;
3530 token = g_scanner_get_next_token (scanner);
3533 case GTK_RC_TOKEN_WIDGET:
3534 path_type = GTK_PATH_WIDGET;
3536 case GTK_RC_TOKEN_WIDGET_CLASS:
3537 path_type = GTK_PATH_WIDGET_CLASS;
3539 case GTK_RC_TOKEN_CLASS:
3540 path_type = GTK_PATH_CLASS;
3543 return GTK_RC_TOKEN_WIDGET_CLASS;
3546 token = g_scanner_get_next_token (scanner);
3547 if (token != G_TOKEN_STRING)
3548 return G_TOKEN_STRING;
3550 pattern = g_strdup (scanner->value.v_string);
3552 token = g_scanner_get_next_token (scanner);
3553 if (token == GTK_RC_TOKEN_STYLE)
3555 else if (token == GTK_RC_TOKEN_BINDING)
3560 return GTK_RC_TOKEN_STYLE;
3563 if (g_scanner_peek_next_token (scanner) == ':')
3565 token = gtk_rc_parse_priority (scanner, &priority);
3566 if (token != G_TOKEN_NONE)
3573 token = g_scanner_get_next_token (scanner);
3574 if (token != G_TOKEN_STRING)
3577 return G_TOKEN_STRING;
3582 GtkBindingSet *binding;
3584 binding = gtk_binding_set_find (scanner->value.v_string);
3588 return G_TOKEN_STRING;
3590 gtk_binding_set_add_path (binding, path_type, pattern, priority);
3594 GtkRcStyle *rc_style;
3597 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3602 return G_TOKEN_STRING;
3605 rc_set = g_new (GtkRcSet, 1);
3606 rc_set->pspec = g_pattern_spec_new (pattern);
3607 rc_set->rc_style = rc_style;
3608 rc_set->priority = priority;
3610 if (path_type == GTK_PATH_WIDGET)
3611 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
3612 else if (path_type == GTK_PATH_WIDGET_CLASS)
3613 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
3615 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
3619 return G_TOKEN_NONE;
3623 gtk_rc_parse_stock_id (GScanner *scanner,
3628 token = g_scanner_get_next_token (scanner);
3629 if (token != G_TOKEN_LEFT_BRACE)
3630 return G_TOKEN_LEFT_BRACE;
3632 token = g_scanner_get_next_token (scanner);
3634 if (token != G_TOKEN_STRING)
3635 return G_TOKEN_STRING;
3637 *stock_id = g_strdup (scanner->value.v_string);
3639 token = g_scanner_get_next_token (scanner);
3640 if (token != G_TOKEN_RIGHT_BRACE)
3643 return G_TOKEN_RIGHT_BRACE;
3646 return G_TOKEN_NONE;
3650 gtk_rc_parse_icon_source (GtkRcContext *context,
3652 GtkIconSet *icon_set,
3653 gboolean *icon_set_valid)
3656 GtkIconSource *source;
3657 gchar *full_filename;
3659 token = g_scanner_get_next_token (scanner);
3660 if (token != G_TOKEN_LEFT_CURLY)
3661 return G_TOKEN_LEFT_CURLY;
3663 token = g_scanner_get_next_token (scanner);
3665 if (token != G_TOKEN_STRING && token != '@')
3666 return G_TOKEN_STRING;
3668 source = gtk_icon_source_new ();
3670 if (token == G_TOKEN_STRING)
3674 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
3677 gtk_icon_source_set_filename (source, full_filename);
3678 g_free (full_filename);
3685 token = g_scanner_get_next_token (scanner);
3687 if (token != G_TOKEN_STRING)
3688 return G_TOKEN_STRING;
3690 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
3693 /* We continue parsing even if we didn't find the pixmap so that rest of the
3694 * file is read, even if the syntax is bad. However we don't validate the
3695 * icon_set so the caller can choose not to install it.
3697 token = g_scanner_get_next_token (scanner);
3699 if (token == G_TOKEN_RIGHT_CURLY)
3701 else if (token != G_TOKEN_COMMA)
3703 gtk_icon_source_free (source);
3704 return G_TOKEN_COMMA;
3707 /* Get the direction */
3709 token = g_scanner_get_next_token (scanner);
3713 case GTK_RC_TOKEN_RTL:
3714 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3715 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
3718 case GTK_RC_TOKEN_LTR:
3719 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3720 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
3727 gtk_icon_source_free (source);
3728 return GTK_RC_TOKEN_RTL;
3732 token = g_scanner_get_next_token (scanner);
3734 if (token == G_TOKEN_RIGHT_CURLY)
3736 else if (token != G_TOKEN_COMMA)
3738 gtk_icon_source_free (source);
3739 return G_TOKEN_COMMA;
3744 token = g_scanner_get_next_token (scanner);
3748 case GTK_RC_TOKEN_NORMAL:
3749 gtk_icon_source_set_state_wildcarded (source, FALSE);
3750 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
3753 case GTK_RC_TOKEN_PRELIGHT:
3754 gtk_icon_source_set_state_wildcarded (source, FALSE);
3755 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
3759 case GTK_RC_TOKEN_INSENSITIVE:
3760 gtk_icon_source_set_state_wildcarded (source, FALSE);
3761 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
3764 case GTK_RC_TOKEN_ACTIVE:
3765 gtk_icon_source_set_state_wildcarded (source, FALSE);
3766 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
3769 case GTK_RC_TOKEN_SELECTED:
3770 gtk_icon_source_set_state_wildcarded (source, FALSE);
3771 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
3778 gtk_icon_source_free (source);
3779 return GTK_RC_TOKEN_PRELIGHT;
3783 token = g_scanner_get_next_token (scanner);
3785 if (token == G_TOKEN_RIGHT_CURLY)
3787 else if (token != G_TOKEN_COMMA)
3789 gtk_icon_source_free (source);
3790 return G_TOKEN_COMMA;
3795 token = g_scanner_get_next_token (scanner);
3801 if (token != G_TOKEN_STRING)
3803 gtk_icon_source_free (source);
3804 return G_TOKEN_STRING;
3807 size = gtk_icon_size_from_name (scanner->value.v_string);
3809 if (size != GTK_ICON_SIZE_INVALID)
3811 gtk_icon_source_set_size_wildcarded (source, FALSE);
3812 gtk_icon_source_set_size (source, size);
3816 /* Check the close brace */
3818 token = g_scanner_get_next_token (scanner);
3819 if (token != G_TOKEN_RIGHT_CURLY)
3821 gtk_icon_source_free (source);
3822 return G_TOKEN_RIGHT_CURLY;
3826 if (gtk_icon_source_get_filename (source) ||
3827 gtk_icon_source_get_icon_name (source))
3829 gtk_icon_set_add_source (icon_set, source);
3830 *icon_set_valid = TRUE;
3832 gtk_icon_source_free (source);
3834 return G_TOKEN_NONE;
3838 gtk_rc_parse_stock (GtkRcContext *context,
3840 GtkRcStyle *rc_style,
3841 GtkIconFactory *factory)
3843 GtkIconSet *icon_set = NULL;
3844 gboolean icon_set_valid = FALSE;
3845 gchar *stock_id = NULL;
3848 token = g_scanner_get_next_token (scanner);
3849 if (token != GTK_RC_TOKEN_STOCK)
3850 return GTK_RC_TOKEN_STOCK;
3852 token = gtk_rc_parse_stock_id (scanner, &stock_id);
3853 if (token != G_TOKEN_NONE)
3856 token = g_scanner_get_next_token (scanner);
3857 if (token != G_TOKEN_EQUAL_SIGN)
3860 return G_TOKEN_EQUAL_SIGN;
3863 token = g_scanner_get_next_token (scanner);
3864 if (token != G_TOKEN_LEFT_CURLY)
3867 return G_TOKEN_LEFT_CURLY;
3870 token = g_scanner_peek_next_token (scanner);
3871 while (token != G_TOKEN_RIGHT_CURLY)
3873 if (icon_set == NULL)
3874 icon_set = gtk_icon_set_new ();
3876 token = gtk_rc_parse_icon_source (context,
3877 scanner, icon_set, &icon_set_valid);
3878 if (token != G_TOKEN_NONE)
3881 gtk_icon_set_unref (icon_set);
3885 token = g_scanner_get_next_token (scanner);
3887 if (token != G_TOKEN_COMMA &&
3888 token != G_TOKEN_RIGHT_CURLY)
3891 gtk_icon_set_unref (icon_set);
3892 return G_TOKEN_RIGHT_CURLY;
3899 gtk_icon_factory_add (factory,
3903 gtk_icon_set_unref (icon_set);
3908 return G_TOKEN_NONE;