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 = (GtkRcProperty *) _gtk_rc_style_lookup_rc_property (rc_style,
1294 guint index = node - (GtkRcProperty *) rc_style->rc_properties->data;
1295 g_value_unset (&node->value);
1296 g_free (node->origin);
1297 g_array_remove_index (rc_style->rc_properties, index);
1302 gtk_rc_style_ref (GtkRcStyle *rc_style)
1304 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1306 g_object_ref (rc_style);
1310 gtk_rc_style_unref (GtkRcStyle *rc_style)
1312 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1314 g_object_unref (rc_style);
1318 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1320 return g_object_new (G_OBJECT_TYPE (style), NULL);
1324 _gtk_rc_style_get_color_hashes (GtkRcStyle *rc_style)
1326 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1328 return priv->color_hashes;
1332 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1333 gconstpointer bsearch_node2)
1335 const GtkRcProperty *prop1 = bsearch_node1;
1336 const GtkRcProperty *prop2 = bsearch_node2;
1338 if (prop1->type_name == prop2->type_name)
1339 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1341 return prop1->type_name < prop2->type_name ? -1 : 1;
1345 insert_rc_property (GtkRcStyle *style,
1346 GtkRcProperty *property,
1350 GtkRcProperty *new_property = NULL;
1351 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1353 key.type_name = property->type_name;
1354 key.property_name = property->property_name;
1356 if (!style->rc_properties)
1357 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1360 while (i < style->rc_properties->len)
1362 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1368 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1370 g_free (new_property->origin);
1371 g_value_unset (&new_property->value);
1373 *new_property = key;
1387 g_array_insert_val (style->rc_properties, i, key);
1388 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1391 new_property->origin = g_strdup (property->origin);
1392 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1393 g_value_copy (&property->value, &new_property->value);
1397 gtk_rc_style_real_merge (GtkRcStyle *dest,
1402 for (i = 0; i < 5; i++)
1404 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1405 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1407 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1408 src->color_flags[i] & GTK_RC_FG)
1410 dest->fg[i] = src->fg[i];
1411 dest->color_flags[i] |= GTK_RC_FG;
1413 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1414 src->color_flags[i] & GTK_RC_BG)
1416 dest->bg[i] = src->bg[i];
1417 dest->color_flags[i] |= GTK_RC_BG;
1419 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1420 src->color_flags[i] & GTK_RC_TEXT)
1422 dest->text[i] = src->text[i];
1423 dest->color_flags[i] |= GTK_RC_TEXT;
1425 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1426 src->color_flags[i] & GTK_RC_BASE)
1428 dest->base[i] = src->base[i];
1429 dest->color_flags[i] |= GTK_RC_BASE;
1433 if (dest->xthickness < 0 && src->xthickness >= 0)
1434 dest->xthickness = src->xthickness;
1435 if (dest->ythickness < 0 && src->ythickness >= 0)
1436 dest->ythickness = src->ythickness;
1440 if (!dest->font_desc)
1441 dest->font_desc = pango_font_description_copy (src->font_desc);
1443 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1446 if (src->rc_properties)
1450 for (i = 0; i < src->rc_properties->len; i++)
1451 insert_rc_property (dest,
1452 &g_array_index (src->rc_properties, GtkRcProperty, i),
1458 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1460 return gtk_style_new ();
1464 gtk_rc_style_prepend_empty_icon_factory (GtkRcStyle *rc_style)
1466 GtkIconFactory *factory = gtk_icon_factory_new ();
1468 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories, factory);
1472 gtk_rc_style_prepend_empty_color_hash (GtkRcStyle *rc_style)
1474 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1475 GHashTable *hash = g_hash_table_new_full (g_str_hash, g_str_equal,
1477 (GDestroyNotify) gdk_color_free);
1479 priv->color_hashes = g_slist_prepend (priv->color_hashes, hash);
1483 gtk_rc_style_append_icon_factories (GtkRcStyle *rc_style,
1484 GtkRcStyle *src_style)
1486 GSList *concat = g_slist_copy (src_style->icon_factories);
1488 g_slist_foreach (concat, (GFunc) g_object_ref, NULL);
1490 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories, concat);
1494 gtk_rc_style_append_color_hashes (GtkRcStyle *rc_style,
1495 GtkRcStyle *src_style)
1497 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1498 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1499 GSList *concat = g_slist_copy (src_priv->color_hashes);
1501 g_slist_foreach (concat, (GFunc) g_hash_table_ref, NULL);
1503 priv->color_hashes = g_slist_concat (priv->color_hashes, concat);
1507 gtk_rc_style_copy_icons_and_colors (GtkRcStyle *rc_style,
1508 GtkRcStyle *src_style,
1509 GtkRcContext *context)
1511 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
1515 GtkRcStylePrivate *src_priv = GTK_RC_STYLE_GET_PRIVATE (src_style);
1517 /* Append src_style's factories, adding a ref to them */
1518 if (src_style->icon_factories != NULL)
1520 /* Add a factory for ourselves if we have none,
1521 * in case we end up defining more stock icons.
1522 * I see no real way around this; we need to maintain
1523 * the invariant that the first factory in the list
1524 * is always our_factory, the one belonging to us,
1525 * and if we put src_style factories in the list we can't
1526 * do that if the style is reopened.
1528 if (rc_style->icon_factories == NULL)
1529 gtk_rc_style_prepend_empty_icon_factory (rc_style);
1531 gtk_rc_style_append_icon_factories (rc_style, src_style);
1534 /* Also append src_style's color hashes, adding a ref to them */
1535 if (src_priv->color_hashes != NULL)
1537 /* See comment above .. */
1538 if (priv->color_hashes == NULL)
1539 gtk_rc_style_prepend_empty_color_hash (rc_style);
1541 gtk_rc_style_append_color_hashes (rc_style, src_style);
1545 /* if we didn't get color hashes from the src_style, initialize
1546 * the list with the settings' color scheme (if it exists)
1548 if (priv->color_hashes == NULL && context && context->color_hash != NULL)
1550 gtk_rc_style_prepend_empty_color_hash (rc_style);
1552 priv->color_hashes = g_slist_append (priv->color_hashes,
1553 g_hash_table_ref (context->color_hash));
1558 gtk_rc_clear_hash_node (gpointer key,
1562 gtk_rc_style_unref (data);
1566 gtk_rc_free_rc_sets (GSList *slist)
1572 rc_set = slist->data;
1573 gtk_rc_set_free (rc_set);
1575 slist = slist->next;
1580 gtk_rc_clear_styles (GtkRcContext *context)
1582 /* Clear out all old rc_styles */
1584 if (context->rc_style_ht)
1586 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1587 g_hash_table_destroy (context->rc_style_ht);
1588 context->rc_style_ht = NULL;
1591 gtk_rc_free_rc_sets (context->rc_sets_widget);
1592 g_slist_free (context->rc_sets_widget);
1593 context->rc_sets_widget = NULL;
1595 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1596 g_slist_free (context->rc_sets_widget_class);
1597 context->rc_sets_widget_class = NULL;
1599 gtk_rc_free_rc_sets (context->rc_sets_class);
1600 g_slist_free (context->rc_sets_class);
1601 context->rc_sets_class = NULL;
1604 /* Reset all our widgets. Also, we have to invalidate cached icons in
1605 * icon sets so they get re-rendered.
1608 gtk_rc_reset_widgets (GtkSettings *settings)
1610 GList *list, *toplevels;
1612 _gtk_icon_set_invalidate_caches ();
1614 toplevels = gtk_window_list_toplevels ();
1615 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1617 for (list = toplevels; list; list = list->next)
1619 if (gtk_widget_get_screen (list->data) == settings->screen)
1621 gtk_widget_reset_rc_styles (list->data);
1624 g_object_unref (list->data);
1626 g_list_free (toplevels);
1630 gtk_rc_clear_realized_style (gpointer key,
1634 GSList *rc_styles = key;
1635 GtkStyle *style = value;
1636 GSList *tmp_list = rc_styles;
1638 g_object_unref (style);
1642 GtkRcStyle *rc_style = tmp_list->data;
1644 rc_style->rc_style_lists = g_slist_remove_all (rc_style->rc_style_lists,
1646 tmp_list = tmp_list->next;
1649 g_slist_free (rc_styles);
1653 * gtk_rc_reset_styles:
1654 * @settings: a #GtkSettings
1656 * This function recomputes the styles for all widgets that use a
1657 * particular #GtkSettings object. (There is one #GtkSettings object
1658 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
1659 * when some global parameter has changed that affects the appearance
1660 * of all widgets, because when a widget gets a new style, it will
1661 * both redraw and recompute any cached information about its
1662 * appearance. As an example, it is used when the default font size
1663 * set by the operating system changes. Note that this function
1664 * doesn't affect widgets that have a style set explicitely on them
1665 * with gtk_widget_set_style().
1670 gtk_rc_reset_styles (GtkSettings *settings)
1672 GtkRcContext *context;
1673 gboolean reset = FALSE;
1675 g_return_if_fail (GTK_IS_SETTINGS (settings));
1677 context = gtk_rc_context_get (settings);
1679 if (context->default_style)
1681 g_object_unref (context->default_style);
1682 context->default_style = NULL;
1686 /* Clear out styles that have been looked up already
1688 if (realized_style_ht)
1690 g_hash_table_foreach (realized_style_ht, gtk_rc_clear_realized_style, NULL);
1691 g_hash_table_destroy (realized_style_ht);
1692 realized_style_ht = NULL;
1697 gtk_rc_reset_widgets (settings);
1701 _gtk_rc_context_get_default_font_name (GtkSettings *settings)
1703 GtkRcContext *context;
1704 gchar *new_font_name;
1706 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1708 context = gtk_rc_context_get (settings);
1710 g_object_get (context->settings,
1711 "gtk-font-name", &new_font_name,
1714 if (new_font_name != context->font_name && !(new_font_name && strcmp (context->font_name, new_font_name) == 0))
1716 g_free (context->font_name);
1717 context->font_name = g_strdup (new_font_name);
1719 gtk_rc_reset_styles (settings);
1722 g_free (new_font_name);
1724 return context->font_name;
1728 * gtk_rc_reparse_all_for_settings:
1729 * @settings: a #GtkSettings
1730 * @force_load: load whether or not anything changed
1732 * If the modification time on any previously read file
1733 * for the given #GtkSettings has changed, discard all style information
1734 * and then reread all previously read RC files.
1736 * Return value: %TRUE if the files were reread.
1739 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1740 gboolean force_load)
1742 gboolean mtime_modified = FALSE;
1745 GtkRcContext *context;
1746 struct stat statbuf;
1748 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1750 context = gtk_rc_context_get (settings);
1752 if (context->reloading)
1757 /* Check through and see if any of the RC's have had their
1758 * mtime modified. If so, reparse everything.
1760 tmp_list = context->rc_files;
1763 rc_file = tmp_list->data;
1765 if (!rc_file->is_string)
1767 if (!g_lstat (rc_file->name, &statbuf) &&
1768 (statbuf.st_mtime != rc_file->mtime))
1770 mtime_modified = TRUE;
1775 tmp_list = tmp_list->next;
1779 if (force_load || mtime_modified)
1781 _gtk_binding_reset_parsed ();
1782 gtk_rc_clear_styles (context);
1783 context->reloading = TRUE;
1785 _gtk_settings_reset_rc_values (context->settings);
1786 gtk_rc_clear_rc_files (context);
1788 gtk_rc_parse_default_files (context);
1790 tmp_list = global_rc_files;
1793 rc_file = tmp_list->data;
1795 if (rc_file->is_string)
1796 gtk_rc_context_parse_string (context, rc_file->name);
1798 gtk_rc_context_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, FALSE);
1800 tmp_list = tmp_list->next;
1803 g_free (context->theme_name);
1804 g_free (context->key_theme_name);
1806 g_object_get (context->settings,
1807 "gtk-theme-name", &context->theme_name,
1808 "gtk-key-theme-name", &context->key_theme_name,
1811 if (context->theme_name && context->theme_name[0])
1812 gtk_rc_parse_named (context, context->theme_name, NULL);
1813 if (context->key_theme_name && context->key_theme_name[0])
1814 gtk_rc_parse_named (context, context->key_theme_name, "key");
1816 context->reloading = FALSE;
1818 gtk_rc_reset_widgets (context->settings);
1821 return force_load || mtime_modified;
1825 * gtk_rc_reparse_all:
1827 * If the modification time on any previously read file for the
1828 * default #GtkSettings has changed, discard all style information
1829 * and then reread all previously read RC files.
1831 * Return value: %TRUE if the files were reread.
1834 gtk_rc_reparse_all (void)
1837 gboolean result = FALSE;
1839 for (tmp_list = rc_contexts; tmp_list; tmp_list = tmp_list->next)
1841 GtkRcContext *context = tmp_list->data;
1842 if (gtk_rc_reparse_all_for_settings (context->settings, FALSE))
1850 gtk_rc_styles_match (GSList *rc_styles,
1854 gchar *path_reversed)
1861 rc_set = sets->data;
1864 if (rc_set->type == GTK_PATH_WIDGET_CLASS)
1866 if (_gtk_rc_match_widget_class (rc_set->path, path_length, path, path_reversed))
1867 rc_styles = g_slist_append (rc_styles, rc_set);
1871 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1872 rc_styles = g_slist_append (rc_styles, rc_set);
1880 rc_set_compare (gconstpointer a, gconstpointer b)
1882 const GtkRcSet *set_a = a;
1883 const GtkRcSet *set_b = b;
1885 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1889 sort_and_dereference_sets (GSList *styles)
1893 /* At this point, the list of sets is ordered by:
1895 * a) 'widget' patterns are earlier than 'widget_class' patterns
1896 * which are ealier than 'class' patterns.
1897 * a) For two matches for class patterns, a match to a child type
1898 * is before a match to a parent type
1899 * c) a match later in the RC file (or in a later RC file) is before a
1900 * match earlier in the RC file.
1902 * With a) taking precedence over b) which takes precendence over c).
1904 * Now sort by priority, which has the highest precendence for sort order
1906 styles = g_slist_sort (styles, rc_set_compare);
1908 /* Make styles->data = styles->data->rc_style
1913 GtkRcSet *set = tmp_list->data;
1914 tmp_list->data = set->rc_style;
1915 tmp_list = tmp_list->next;
1923 * @widget: a #GtkWidget
1925 * Finds all matching RC styles for a given widget,
1926 * composites them together, and then creates a
1927 * #GtkStyle representing the composite appearance.
1928 * (GTK+ actually keeps a cache of previously
1929 * created styles, so a new style may not be
1932 * Returns: the resulting style. No refcount is added
1933 * to the returned style, so if you want to save this
1934 * style around, you should add a reference yourself.
1937 gtk_rc_get_style (GtkWidget *widget)
1939 GtkRcStyle *widget_rc_style;
1940 GSList *rc_styles = NULL;
1941 GtkRcContext *context;
1943 static guint rc_style_key_id = 0;
1945 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1947 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1949 /* We allow the specification of a single rc style to be bound
1950 * tightly to a widget, for application modifications
1952 if (!rc_style_key_id)
1953 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1955 if (context->rc_sets_widget)
1957 gchar *path, *path_reversed;
1960 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1961 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1963 g_free (path_reversed);
1966 if (context->rc_sets_widget_class)
1968 gchar *path, *path_reversed;
1971 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1972 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1974 g_free (path_reversed);
1977 if (context->rc_sets_class)
1981 type = G_TYPE_FROM_INSTANCE (widget);
1985 gchar *path_reversed;
1988 path = g_strdup (g_type_name (type));
1989 path_length = strlen (path);
1990 path_reversed = g_strdup (path);
1991 g_strreverse (path_reversed);
1993 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1995 g_free (path_reversed);
1997 type = g_type_parent (type);
2001 rc_styles = sort_and_dereference_sets (rc_styles);
2003 widget_rc_style = g_object_get_qdata (G_OBJECT (widget), rc_style_key_id);
2005 if (widget_rc_style)
2006 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
2009 return gtk_rc_init_style (context, rc_styles);
2012 if (!context->default_style)
2014 context->default_style = gtk_style_new ();
2015 _gtk_style_init_for_settings (context->default_style, context->settings);
2018 return context->default_style;
2023 * gtk_rc_get_style_by_paths:
2024 * @settings: a #GtkSettings object
2025 * @widget_path: the widget path to use when looking up the style, or %NULL
2026 * if no matching against the widget path should be done
2027 * @class_path: the class path to use when looking up the style, or %NULL
2028 * if no matching against the class path should be done.
2029 * @type: a type that will be used along with parent types of this type
2030 * when matching against class styles, or #G_TYPE_NONE
2032 * Creates up a #GtkStyle from styles defined in a RC file by providing
2033 * the raw components used in matching. This function may be useful
2034 * when creating pseudo-widgets that should be themed like widgets but
2035 * don't actually have corresponding GTK+ widgets. An example of this
2036 * would be items inside a GNOME canvas widget.
2038 * The action of gtk_rc_get_style() is similar to:
2040 * gtk_widget_path (widget, NULL, &path, NULL);
2041 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
2042 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
2044 * G_OBJECT_TYPE (widget));
2047 * Return value: A style created by matching with the supplied paths,
2048 * or %NULL if nothing matching was specified and the default style should
2049 * be used. The returned value is owned by GTK+ as part of an internal cache,
2050 * so you must call g_object_ref() on the returned value if you want to
2051 * keep a reference to it.
2054 gtk_rc_get_style_by_paths (GtkSettings *settings,
2055 const char *widget_path,
2056 const char *class_path,
2059 /* We duplicate the code from above to avoid slowing down the above
2060 * by generating paths when we don't need them. I don't know if
2061 * this is really worth it.
2063 GSList *rc_styles = NULL;
2064 GtkRcContext *context;
2066 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
2068 context = gtk_rc_context_get (settings);
2070 if (widget_path && context->rc_sets_widget)
2073 gchar *path_reversed;
2076 path_length = strlen (widget_path);
2077 path = g_strdup (widget_path);
2078 path_reversed = g_strdup (widget_path);
2079 g_strreverse (path_reversed);
2081 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
2083 g_free (path_reversed);
2086 if (class_path && context->rc_sets_widget_class)
2089 gchar *path_reversed;
2092 path = g_strdup (class_path);
2093 path_length = strlen (class_path);
2094 path_reversed = g_strdup (class_path);
2095 g_strreverse (path_reversed);
2097 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
2099 g_free (path_reversed);
2102 if (type != G_TYPE_NONE && context->rc_sets_class)
2107 gchar *path_reversed;
2110 path = g_strdup (g_type_name (type));
2111 path_length = strlen (path);
2112 path_reversed = g_strdup (path);
2113 g_strreverse (path_reversed);
2115 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
2117 g_free (path_reversed);
2119 type = g_type_parent (type);
2123 rc_styles = sort_and_dereference_sets (rc_styles);
2126 return gtk_rc_init_style (context, rc_styles);
2132 gtk_rc_add_rc_sets (GSList *slist,
2133 GtkRcStyle *rc_style,
2134 const gchar *pattern,
2135 GtkPathType path_type)
2137 GtkRcStyle *new_style;
2141 new_style = gtk_rc_style_new ();
2142 *new_style = *rc_style;
2143 new_style->name = g_strdup (rc_style->name);
2144 if (rc_style->font_desc)
2145 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
2147 for (i = 0; i < 5; i++)
2148 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
2150 rc_set = g_new (GtkRcSet, 1);
2151 rc_set->type = path_type;
2153 if (path_type == GTK_PATH_WIDGET_CLASS)
2155 rc_set->pspec = NULL;
2156 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
2160 rc_set->pspec = g_pattern_spec_new (pattern);
2161 rc_set->path = NULL;
2164 rc_set->rc_style = rc_style;
2166 return g_slist_prepend (slist, rc_set);
2170 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
2171 const gchar *pattern)
2173 GtkRcContext *context;
2175 g_return_if_fail (rc_style != NULL);
2176 g_return_if_fail (pattern != NULL);
2178 context = gtk_rc_context_get (gtk_settings_get_default ());
2180 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern, GTK_PATH_WIDGET);
2184 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
2185 const gchar *pattern)
2187 GtkRcContext *context;
2189 g_return_if_fail (rc_style != NULL);
2190 g_return_if_fail (pattern != NULL);
2192 context = gtk_rc_context_get (gtk_settings_get_default ());
2194 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern, GTK_PATH_WIDGET_CLASS);
2198 gtk_rc_add_class_style (GtkRcStyle *rc_style,
2199 const gchar *pattern)
2201 GtkRcContext *context;
2203 g_return_if_fail (rc_style != NULL);
2204 g_return_if_fail (pattern != NULL);
2206 context = gtk_rc_context_get (gtk_settings_get_default ());
2208 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern, GTK_PATH_CLASS);
2212 gtk_rc_scanner_new (void)
2214 return g_scanner_new (>k_rc_scanner_config);
2218 gtk_rc_parse_any (GtkRcContext *context,
2219 const gchar *input_name,
2221 const gchar *input_string)
2227 scanner = gtk_rc_scanner_new ();
2231 g_assert (input_string == NULL);
2233 g_scanner_input_file (scanner, input_fd);
2237 g_assert (input_string != NULL);
2239 g_scanner_input_text (scanner, input_string, strlen (input_string));
2241 scanner->input_name = input_name;
2243 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2244 g_scanner_scope_add_symbol (scanner, 0, symbol_names + symbols[i].name_offset, GINT_TO_POINTER (symbols[i].token));
2248 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
2252 guint expected_token;
2254 expected_token = gtk_rc_parse_statement (context, scanner);
2256 if (expected_token != G_TOKEN_NONE)
2258 const gchar *symbol_name = NULL;
2261 if (scanner->scope_id == 0)
2263 /* if we are in scope 0, we know the symbol names
2264 * that are associated with certain token values.
2265 * so we look them up to make the error messages
2268 if (expected_token > GTK_RC_TOKEN_INVALID &&
2269 expected_token < GTK_RC_TOKEN_LAST)
2271 const gchar *sym = NULL;
2273 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2274 if (symbols[i].token == expected_token)
2275 sym = symbol_names + symbols[i].name_offset;
2278 msg = g_strconcat ("e.g. `", sym, "'", NULL);
2281 if (scanner->token > GTK_RC_TOKEN_INVALID &&
2282 scanner->token < GTK_RC_TOKEN_LAST)
2284 symbol_name = "???";
2285 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
2286 if (symbols[i].token == scanner->token)
2287 symbol_name = symbol_names + symbols[i].name_offset;
2291 g_scanner_unexp_token (scanner,
2304 g_scanner_destroy (scanner);
2308 gtk_rc_styles_hash (const GSList *rc_styles)
2315 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
2316 rc_styles = rc_styles->next;
2323 gtk_rc_styles_equal (const GSList *a,
2328 if (a->data != b->data)
2338 gtk_rc_style_hash (const gchar *name)
2344 result += (result << 3) + *name++;
2350 gtk_rc_style_equal (const gchar *a,
2353 return (strcmp (a, b) == 0);
2357 gtk_rc_style_find (GtkRcContext *context,
2360 if (context->rc_style_ht)
2361 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
2367 gtk_rc_style_to_style (GtkRcContext *context,
2368 GtkRcStyle *rc_style)
2372 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
2373 _gtk_style_init_for_settings (style, context->settings);
2375 style->rc_style = g_object_ref (rc_style);
2377 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
2382 /* Reuses or frees rc_styles */
2384 gtk_rc_init_style (GtkRcContext *context,
2387 GtkStyle *style = NULL;
2390 g_return_val_if_fail (rc_styles != NULL, NULL);
2392 if (!realized_style_ht)
2393 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
2394 (GEqualFunc) gtk_rc_styles_equal);
2396 style = g_hash_table_lookup (realized_style_ht, rc_styles);
2400 GtkRcStyle *base_style = NULL;
2401 GtkRcStyle *proto_style;
2402 GtkRcStyleClass *proto_style_class;
2404 GType rc_style_type = GTK_TYPE_RC_STYLE;
2406 /* Find the first style where the RC file specified engine "" {}
2407 * or the first derived style and use that to create the
2408 * merged style. If we only have raw GtkRcStyles, use the first
2409 * style to create the merged style.
2411 base_style = rc_styles->data;
2412 tmp_styles = rc_styles;
2415 GtkRcStyle *rc_style = tmp_styles->data;
2417 if (rc_style->engine_specified ||
2418 G_OBJECT_TYPE (rc_style) != rc_style_type)
2420 base_style = rc_style;
2424 tmp_styles = tmp_styles->next;
2427 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
2428 proto_style = proto_style_class->create_rc_style (base_style);
2430 tmp_styles = rc_styles;
2433 GtkRcStyle *rc_style = tmp_styles->data;
2435 proto_style_class->merge (proto_style, rc_style);
2437 /* Point from each rc_style to the list of styles */
2438 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
2439 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
2441 gtk_rc_style_append_icon_factories (proto_style, rc_style);
2442 gtk_rc_style_append_color_hashes (proto_style, rc_style);
2444 tmp_styles = tmp_styles->next;
2447 for (i = 0; i < 5; i++)
2448 if (proto_style->bg_pixmap_name[i] &&
2449 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
2451 g_free (proto_style->bg_pixmap_name[i]);
2452 proto_style->bg_pixmap_name[i] = NULL;
2455 style = gtk_rc_style_to_style (context, proto_style);
2456 gtk_rc_style_unref (proto_style);
2458 g_hash_table_insert (realized_style_ht, rc_styles, style);
2461 g_slist_free (rc_styles);
2466 /*********************
2467 * Parsing functions *
2468 *********************/
2471 lookup_color (GtkRcStyle *style,
2472 const char *color_name,
2475 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
2478 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
2480 GHashTable *hash = iter->data;
2481 GdkColor *match = g_hash_table_lookup (hash, color_name);
2485 color->red = match->red;
2486 color->green = match->green;
2487 color->blue = match->blue;
2496 rc_parse_token_or_compound (GScanner *scanner,
2499 GTokenType delimiter)
2501 guint token = g_scanner_get_next_token (scanner);
2503 /* we either scan a single token (skipping comments)
2504 * or a compund statement.
2505 * compunds are enclosed in (), [] or {} braces, we read
2506 * them in via deep recursion.
2513 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
2516 g_string_append_printf (gstring, " %f", scanner->value.v_float);
2518 case G_TOKEN_STRING:
2519 string = g_strescape (scanner->value.v_string, NULL);
2520 g_string_append (gstring, " \"");
2521 g_string_append (gstring, string);
2522 g_string_append_c (gstring, '"');
2525 case G_TOKEN_IDENTIFIER:
2526 g_string_append_c (gstring, ' ');
2527 g_string_append (gstring, scanner->value.v_identifier);
2529 case G_TOKEN_COMMENT_SINGLE:
2530 case G_TOKEN_COMMENT_MULTI:
2531 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2532 case G_TOKEN_LEFT_PAREN:
2533 g_string_append_c (gstring, ' ');
2534 g_string_append_c (gstring, token);
2535 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_PAREN);
2536 if (token != G_TOKEN_NONE)
2539 case G_TOKEN_LEFT_CURLY:
2540 g_string_append_c (gstring, ' ');
2541 g_string_append_c (gstring, token);
2542 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_CURLY);
2543 if (token != G_TOKEN_NONE)
2546 case G_TOKEN_LEFT_BRACE:
2547 g_string_append_c (gstring, ' ');
2548 g_string_append_c (gstring, token);
2549 token = rc_parse_token_or_compound (scanner, style, gstring, G_TOKEN_RIGHT_BRACE);
2550 if (token != G_TOKEN_NONE)
2554 if (g_scanner_peek_next_token (scanner) == G_TOKEN_IDENTIFIER)
2557 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2558 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2559 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2561 g_scanner_get_next_token (scanner);
2563 if (!style || !lookup_color (style, scanner->value.v_identifier,
2566 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2567 scanner->value.v_identifier);
2568 return G_TOKEN_IDENTIFIER;
2572 g_string_append_printf (gstring, " { %s, %s, %s }",
2573 g_ascii_formatd (rbuf, sizeof (rbuf),
2575 color.red / 65535.0),
2576 g_ascii_formatd (gbuf, sizeof (gbuf),
2578 color.green / 65535.0),
2579 g_ascii_formatd (bbuf, sizeof (bbuf),
2581 color.blue / 65535.0));
2585 return G_TOKEN_IDENTIFIER;
2587 if (token >= 256 || token < 1)
2588 return delimiter ? delimiter : G_TOKEN_STRING;
2589 g_string_append_c (gstring, ' ');
2590 g_string_append_c (gstring, token);
2591 if (token == delimiter)
2592 return G_TOKEN_NONE;
2596 return G_TOKEN_NONE;
2598 return rc_parse_token_or_compound (scanner, style, gstring, delimiter);
2602 gtk_rc_parse_assignment (GScanner *scanner,
2604 GtkRcProperty *prop)
2606 #define MY_SCAN_IDENTIFIER TRUE
2607 #define MY_SCAN_SYMBOLS FALSE
2608 #define MY_IDENTIFIER_2_STRING FALSE
2609 #define MY_CHAR_2_TOKEN TRUE
2610 #define MY_SCAN_IDENTIFIER_NULL FALSE
2611 #define MY_NUMBERS_2_INT TRUE
2613 gboolean scan_identifier = scanner->config->scan_identifier;
2614 gboolean scan_symbols = scanner->config->scan_symbols;
2615 gboolean identifier_2_string = scanner->config->identifier_2_string;
2616 gboolean char_2_token = scanner->config->char_2_token;
2617 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2618 gboolean numbers_2_int = scanner->config->numbers_2_int;
2619 gboolean negate = FALSE;
2620 gboolean is_color = FALSE;
2623 /* check that this is an assignment */
2624 if (g_scanner_get_next_token (scanner) != '=')
2627 /* adjust scanner mode */
2628 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2629 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2630 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2631 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2632 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2633 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2635 /* record location */
2636 if (g_getenv ("GTK_DEBUG"))
2637 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2639 prop->origin = NULL;
2641 /* parse optional symbolic color prefix */
2642 if (g_scanner_peek_next_token (scanner) == '@')
2644 g_scanner_get_next_token (scanner); /* eat color prefix */
2648 /* parse optional sign */
2649 if (!is_color && g_scanner_peek_next_token (scanner) == '-')
2651 g_scanner_get_next_token (scanner); /* eat sign */
2655 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an
2658 token = g_scanner_peek_next_token (scanner);
2660 if (is_color && token != G_TOKEN_IDENTIFIER)
2662 token = G_TOKEN_IDENTIFIER;
2669 g_scanner_get_next_token (scanner);
2670 g_value_init (&prop->value, G_TYPE_LONG);
2671 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2672 token = G_TOKEN_NONE;
2675 g_scanner_get_next_token (scanner);
2676 g_value_init (&prop->value, G_TYPE_DOUBLE);
2677 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2678 token = G_TOKEN_NONE;
2680 case G_TOKEN_STRING:
2681 g_scanner_get_next_token (scanner);
2683 token = G_TOKEN_INT;
2686 g_value_init (&prop->value, G_TYPE_STRING);
2687 g_value_set_string (&prop->value, scanner->value.v_string);
2688 token = G_TOKEN_NONE;
2691 case G_TOKEN_IDENTIFIER:
2695 gchar rbuf[G_ASCII_DTOSTR_BUF_SIZE];
2696 gchar gbuf[G_ASCII_DTOSTR_BUF_SIZE];
2697 gchar bbuf[G_ASCII_DTOSTR_BUF_SIZE];
2700 g_scanner_get_next_token (scanner);
2702 if (!style || !lookup_color (style, scanner->value.v_identifier,
2705 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
2706 scanner->value.v_identifier);
2707 token = G_TOKEN_IDENTIFIER;
2711 gstring = g_string_new (NULL);
2713 g_string_append_printf (gstring, " { %s, %s, %s }",
2714 g_ascii_formatd (rbuf, sizeof (rbuf),
2716 color.red / 65535.0),
2717 g_ascii_formatd (gbuf, sizeof (gbuf),
2719 color.green / 65535.0),
2720 g_ascii_formatd (bbuf, sizeof (bbuf),
2722 color.blue / 65535.0));
2724 g_value_init (&prop->value, G_TYPE_GSTRING);
2725 g_value_take_boxed (&prop->value, gstring);
2726 token = G_TOKEN_NONE;
2730 case G_TOKEN_LEFT_PAREN:
2731 case G_TOKEN_LEFT_CURLY:
2732 case G_TOKEN_LEFT_BRACE:
2735 GString *gstring = g_string_new (NULL);
2736 gboolean parse_on = TRUE;
2738 /* allow identifier(foobar) to support color expressions */
2739 if (token == G_TOKEN_IDENTIFIER)
2741 g_scanner_get_next_token (scanner);
2743 g_string_append_c (gstring, ' ');
2744 g_string_append (gstring, scanner->value.v_identifier);
2746 /* temporarily reset scanner mode to default, so we
2747 * don't peek the next token in a mode that only makes
2748 * sense in this function; because if anything but
2749 * G_TOKEN_LEFT_PAREN follows, the next token will be
2750 * parsed by our caller.
2752 * FIXME: right fix would be to call g_scanner_unget()
2753 * but that doesn't exist
2755 scanner->config->scan_identifier = scan_identifier;
2756 scanner->config->scan_symbols = scan_symbols;
2757 scanner->config->identifier_2_string = identifier_2_string;
2758 scanner->config->char_2_token = char_2_token;
2759 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2760 scanner->config->numbers_2_int = numbers_2_int;
2762 token = g_scanner_peek_next_token (scanner);
2764 /* restore adjusted scanner mode */
2765 scanner->config->scan_identifier = MY_SCAN_IDENTIFIER;
2766 scanner->config->scan_symbols = MY_SCAN_SYMBOLS;
2767 scanner->config->identifier_2_string = MY_IDENTIFIER_2_STRING;
2768 scanner->config->char_2_token = MY_CHAR_2_TOKEN;
2769 scanner->config->scan_identifier_NULL = MY_SCAN_IDENTIFIER_NULL;
2770 scanner->config->numbers_2_int = MY_NUMBERS_2_INT;
2772 if (token != G_TOKEN_LEFT_PAREN)
2774 token = G_TOKEN_NONE;
2780 token = rc_parse_token_or_compound (scanner, style, gstring, 0);
2782 if (token == G_TOKEN_NONE)
2784 g_string_append_c (gstring, ' ');
2785 g_value_init (&prop->value, G_TYPE_GSTRING);
2786 g_value_take_boxed (&prop->value, gstring);
2789 g_string_free (gstring, TRUE);
2794 g_scanner_get_next_token (scanner);
2795 token = G_TOKEN_INT;
2801 /* restore scanner mode */
2802 scanner->config->scan_identifier = scan_identifier;
2803 scanner->config->scan_symbols = scan_symbols;
2804 scanner->config->identifier_2_string = identifier_2_string;
2805 scanner->config->char_2_token = char_2_token;
2806 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2807 scanner->config->numbers_2_int = numbers_2_int;
2813 is_c_identifier (const gchar *string)
2816 gboolean is_varname;
2818 is_varname = strchr ("_" G_CSET_a_2_z G_CSET_A_2_Z, string[0]) != NULL;
2819 for (p = string + 1; *p && is_varname; p++)
2820 is_varname &= strchr (G_CSET_DIGITS "-_" G_CSET_a_2_z G_CSET_A_2_Z, *p) != NULL;
2826 parse_include_file (GtkRcContext *context,
2828 const gchar *filename)
2830 char *to_parse = NULL;
2832 if (g_path_is_absolute (filename))
2834 /* For abolute paths, we call gtk_rc_context_parse_file unconditionally. We
2835 * don't print an error in this case.
2837 to_parse = g_strdup (filename);
2841 /* if a relative path, we look relative to all the RC files in the
2842 * include stack. We require the file to be found in this case
2843 * so we can give meaningful error messages, and because on reparsing
2844 * non-absolute paths don't make sense.
2846 GSList *tmp_list = current_files_stack;
2849 GtkRcFile *curfile = tmp_list->data;
2850 gchar *tmpname = g_build_filename (curfile->directory, filename, NULL);
2852 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2860 tmp_list = tmp_list->next;
2866 gtk_rc_context_parse_file (context, to_parse, context->default_priority, FALSE);
2871 g_scanner_warn (scanner,
2872 _("Unable to find include file: \"%s\""),
2879 gtk_rc_parse_statement (GtkRcContext *context,
2884 token = g_scanner_peek_next_token (scanner);
2887 case GTK_RC_TOKEN_INCLUDE:
2888 token = g_scanner_get_next_token (scanner);
2889 if (token != GTK_RC_TOKEN_INCLUDE)
2890 return GTK_RC_TOKEN_INCLUDE;
2891 token = g_scanner_get_next_token (scanner);
2892 if (token != G_TOKEN_STRING)
2893 return G_TOKEN_STRING;
2894 parse_include_file (context, scanner, scanner->value.v_string);
2895 return G_TOKEN_NONE;
2897 case GTK_RC_TOKEN_STYLE:
2898 return gtk_rc_parse_style (context, scanner);
2900 case GTK_RC_TOKEN_BINDING:
2901 return _gtk_binding_parse_binding (scanner);
2903 case GTK_RC_TOKEN_PIXMAP_PATH:
2904 return gtk_rc_parse_pixmap_path (context, scanner);
2906 case GTK_RC_TOKEN_WIDGET:
2907 return gtk_rc_parse_path_pattern (context, scanner);
2909 case GTK_RC_TOKEN_WIDGET_CLASS:
2910 return gtk_rc_parse_path_pattern (context, scanner);
2912 case GTK_RC_TOKEN_CLASS:
2913 return gtk_rc_parse_path_pattern (context, scanner);
2915 case GTK_RC_TOKEN_MODULE_PATH:
2916 return gtk_rc_parse_module_path (scanner);
2918 case GTK_RC_TOKEN_IM_MODULE_FILE:
2919 return gtk_rc_parse_im_module_file (scanner);
2921 case G_TOKEN_IDENTIFIER:
2922 if (is_c_identifier (scanner->next_value.v_identifier))
2924 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2927 g_scanner_get_next_token (scanner); /* eat identifier */
2928 name = g_strdup (scanner->value.v_identifier);
2930 token = gtk_rc_parse_assignment (scanner, NULL, &prop);
2931 if (token == G_TOKEN_NONE)
2933 GtkSettingsValue svalue;
2935 svalue.origin = prop.origin;
2936 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2937 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
2938 _gtk_settings_set_property_value_from_rc (context->settings,
2942 g_free (prop.origin);
2943 if (G_VALUE_TYPE (&prop.value))
2944 g_value_unset (&prop.value);
2951 g_scanner_get_next_token (scanner);
2952 return G_TOKEN_IDENTIFIER;
2955 g_scanner_get_next_token (scanner);
2956 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2961 fixup_rc_set (GSList *list,
2967 GtkRcSet *set = list->data;
2968 if (set->rc_style == orig)
2969 set->rc_style = new;
2975 fixup_rc_sets (GtkRcContext *context,
2979 fixup_rc_set (context->rc_sets_widget, orig, new);
2980 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2981 fixup_rc_set (context->rc_sets_class, orig, new);
2985 gtk_rc_parse_style (GtkRcContext *context,
2988 GtkRcStyle *rc_style;
2989 GtkRcStyle *orig_style;
2990 GtkRcStyle *parent_style = NULL;
2991 GtkRcStylePrivate *rc_priv = NULL;
2994 GtkIconFactory *our_factory = NULL;
2995 GHashTable *our_hash = NULL;
2997 token = g_scanner_get_next_token (scanner);
2998 if (token != GTK_RC_TOKEN_STYLE)
2999 return GTK_RC_TOKEN_STYLE;
3001 token = g_scanner_get_next_token (scanner);
3002 if (token != G_TOKEN_STRING)
3003 return G_TOKEN_STRING;
3005 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3007 orig_style = g_object_ref (rc_style);
3013 rc_style = gtk_rc_style_new ();
3014 rc_style->name = g_strdup (scanner->value.v_string);
3016 for (i = 0; i < 5; i++)
3017 rc_style->bg_pixmap_name[i] = NULL;
3019 for (i = 0; i < 5; i++)
3020 rc_style->color_flags[i] = 0;
3023 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
3025 /* If there's a list, its first member is always the factory belonging
3028 if (rc_style->icon_factories)
3029 our_factory = rc_style->icon_factories->data;
3030 if (rc_priv->color_hashes)
3031 our_hash = rc_priv->color_hashes->data;
3033 token = g_scanner_peek_next_token (scanner);
3034 if (token == G_TOKEN_EQUAL_SIGN)
3036 token = g_scanner_get_next_token (scanner);
3038 token = g_scanner_get_next_token (scanner);
3039 if (token != G_TOKEN_STRING)
3041 token = G_TOKEN_STRING;
3045 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
3048 for (i = 0; i < 5; i++)
3050 rc_style->color_flags[i] = parent_style->color_flags[i];
3051 rc_style->fg[i] = parent_style->fg[i];
3052 rc_style->bg[i] = parent_style->bg[i];
3053 rc_style->text[i] = parent_style->text[i];
3054 rc_style->base[i] = parent_style->base[i];
3057 rc_style->xthickness = parent_style->xthickness;
3058 rc_style->ythickness = parent_style->ythickness;
3060 if (parent_style->font_desc)
3062 if (rc_style->font_desc)
3063 pango_font_description_free (rc_style->font_desc);
3064 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
3067 if (parent_style->rc_properties)
3071 for (i = 0; i < parent_style->rc_properties->len; i++)
3072 insert_rc_property (rc_style,
3073 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
3077 for (i = 0; i < 5; i++)
3079 g_free (rc_style->bg_pixmap_name[i]);
3080 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
3085 /* get icon_factories and color_hashes from the parent style;
3086 * if the parent_style doesn't have color_hashes, initializes
3087 * the color_hashes with the settings' color scheme (if it exists)
3089 gtk_rc_style_copy_icons_and_colors (rc_style, parent_style, context);
3091 if (rc_style->icon_factories)
3092 our_factory = rc_style->icon_factories->data;
3093 if (rc_priv->color_hashes)
3094 our_hash = rc_priv->color_hashes->data;
3096 token = g_scanner_get_next_token (scanner);
3097 if (token != G_TOKEN_LEFT_CURLY)
3099 token = G_TOKEN_LEFT_CURLY;
3103 token = g_scanner_peek_next_token (scanner);
3104 while (token != G_TOKEN_RIGHT_CURLY)
3108 case GTK_RC_TOKEN_BG:
3109 token = gtk_rc_parse_bg (scanner, rc_style);
3111 case GTK_RC_TOKEN_FG:
3112 token = gtk_rc_parse_fg (scanner, rc_style);
3114 case GTK_RC_TOKEN_TEXT:
3115 token = gtk_rc_parse_text (scanner, rc_style);
3117 case GTK_RC_TOKEN_BASE:
3118 token = gtk_rc_parse_base (scanner, rc_style);
3120 case GTK_RC_TOKEN_XTHICKNESS:
3121 token = gtk_rc_parse_xthickness (scanner, rc_style);
3123 case GTK_RC_TOKEN_YTHICKNESS:
3124 token = gtk_rc_parse_ythickness (scanner, rc_style);
3126 case GTK_RC_TOKEN_BG_PIXMAP:
3127 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
3129 case GTK_RC_TOKEN_FONT:
3130 token = gtk_rc_parse_font (scanner, rc_style);
3132 case GTK_RC_TOKEN_FONTSET:
3133 token = gtk_rc_parse_fontset (scanner, rc_style);
3135 case GTK_RC_TOKEN_FONT_NAME:
3136 token = gtk_rc_parse_font_name (scanner, rc_style);
3138 case GTK_RC_TOKEN_ENGINE:
3139 token = gtk_rc_parse_engine (context, scanner, &rc_style);
3141 case GTK_RC_TOKEN_STOCK:
3142 if (our_factory == NULL)
3143 gtk_rc_style_prepend_empty_icon_factory (rc_style);
3144 our_factory = rc_style->icon_factories->data;
3145 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
3147 case GTK_RC_TOKEN_COLOR:
3148 if (our_hash == NULL)
3149 gtk_rc_style_prepend_empty_color_hash (rc_style);
3150 our_hash = rc_priv->color_hashes->data;
3151 token = gtk_rc_parse_logical_color (scanner, rc_style, our_hash);
3153 case G_TOKEN_IDENTIFIER:
3154 if (is_c_identifier (scanner->next_value.v_identifier))
3156 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
3159 g_scanner_get_next_token (scanner); /* eat type name */
3160 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
3161 if (g_scanner_get_next_token (scanner) != ':' ||
3162 g_scanner_get_next_token (scanner) != ':')
3167 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
3168 !is_c_identifier (scanner->value.v_identifier))
3170 token = G_TOKEN_IDENTIFIER;
3174 /* it's important that we do the same canonification as GParamSpecPool here */
3175 name = g_strdup (scanner->value.v_identifier);
3176 g_strcanon (name, G_CSET_DIGITS "-" G_CSET_a_2_z G_CSET_A_2_Z, '-');
3177 prop.property_name = g_quark_from_string (name);
3180 token = gtk_rc_parse_assignment (scanner, rc_style, &prop);
3181 if (token == G_TOKEN_NONE)
3183 g_return_val_if_fail (G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
3184 insert_rc_property (rc_style, &prop, TRUE);
3187 g_free (prop.origin);
3188 if (G_VALUE_TYPE (&prop.value))
3189 g_value_unset (&prop.value);
3193 g_scanner_get_next_token (scanner);
3194 token = G_TOKEN_IDENTIFIER;
3198 g_scanner_get_next_token (scanner);
3199 token = G_TOKEN_RIGHT_CURLY;
3203 if (token != G_TOKEN_NONE)
3206 token = g_scanner_peek_next_token (scanner);
3207 } /* while (token != G_TOKEN_RIGHT_CURLY) */
3209 token = g_scanner_get_next_token (scanner);
3210 if (token != G_TOKEN_RIGHT_CURLY)
3212 token = G_TOKEN_RIGHT_CURLY;
3216 if (rc_style != orig_style)
3218 if (!context->rc_style_ht)
3219 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
3220 (GEqualFunc) gtk_rc_style_equal);
3222 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
3224 /* If we copied the data into a new rc style, fix up references to the old rc style
3225 * in bindings that we have.
3228 fixup_rc_sets (context, orig_style, rc_style);
3232 g_object_unref (orig_style);
3234 return G_TOKEN_NONE;
3237 if (rc_style != orig_style)
3238 gtk_rc_style_unref (rc_style);
3241 g_object_unref (orig_style);
3246 const GtkRcProperty*
3247 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
3249 GQuark property_name)
3251 GtkRcProperty *node = NULL;
3253 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
3255 if (rc_style->rc_properties)
3259 key.type_name = type_name;
3260 key.property_name = property_name;
3262 node = bsearch (&key,
3263 rc_style->rc_properties->data, rc_style->rc_properties->len,
3264 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
3271 gtk_rc_parse_bg (GScanner *scanner,
3277 token = g_scanner_get_next_token (scanner);
3278 if (token != GTK_RC_TOKEN_BG)
3279 return GTK_RC_TOKEN_BG;
3281 token = gtk_rc_parse_state (scanner, &state);
3282 if (token != G_TOKEN_NONE)
3285 token = g_scanner_get_next_token (scanner);
3286 if (token != G_TOKEN_EQUAL_SIGN)
3287 return G_TOKEN_EQUAL_SIGN;
3289 style->color_flags[state] |= GTK_RC_BG;
3290 return gtk_rc_parse_color_full (scanner, style, &style->bg[state]);
3294 gtk_rc_parse_fg (GScanner *scanner,
3300 token = g_scanner_get_next_token (scanner);
3301 if (token != GTK_RC_TOKEN_FG)
3302 return GTK_RC_TOKEN_FG;
3304 token = gtk_rc_parse_state (scanner, &state);
3305 if (token != G_TOKEN_NONE)
3308 token = g_scanner_get_next_token (scanner);
3309 if (token != G_TOKEN_EQUAL_SIGN)
3310 return G_TOKEN_EQUAL_SIGN;
3312 style->color_flags[state] |= GTK_RC_FG;
3313 return gtk_rc_parse_color_full (scanner, style, &style->fg[state]);
3317 gtk_rc_parse_text (GScanner *scanner,
3323 token = g_scanner_get_next_token (scanner);
3324 if (token != GTK_RC_TOKEN_TEXT)
3325 return GTK_RC_TOKEN_TEXT;
3327 token = gtk_rc_parse_state (scanner, &state);
3328 if (token != G_TOKEN_NONE)
3331 token = g_scanner_get_next_token (scanner);
3332 if (token != G_TOKEN_EQUAL_SIGN)
3333 return G_TOKEN_EQUAL_SIGN;
3335 style->color_flags[state] |= GTK_RC_TEXT;
3336 return gtk_rc_parse_color_full (scanner, style, &style->text[state]);
3340 gtk_rc_parse_base (GScanner *scanner,
3346 token = g_scanner_get_next_token (scanner);
3347 if (token != GTK_RC_TOKEN_BASE)
3348 return GTK_RC_TOKEN_BASE;
3350 token = gtk_rc_parse_state (scanner, &state);
3351 if (token != G_TOKEN_NONE)
3354 token = g_scanner_get_next_token (scanner);
3355 if (token != G_TOKEN_EQUAL_SIGN)
3356 return G_TOKEN_EQUAL_SIGN;
3358 style->color_flags[state] |= GTK_RC_BASE;
3359 return gtk_rc_parse_color_full (scanner, style, &style->base[state]);
3363 gtk_rc_parse_xthickness (GScanner *scanner,
3366 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
3367 return GTK_RC_TOKEN_XTHICKNESS;
3369 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3370 return G_TOKEN_EQUAL_SIGN;
3372 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3375 style->xthickness = scanner->value.v_int;
3377 return G_TOKEN_NONE;
3381 gtk_rc_parse_ythickness (GScanner *scanner,
3384 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
3385 return GTK_RC_TOKEN_YTHICKNESS;
3387 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
3388 return G_TOKEN_EQUAL_SIGN;
3390 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
3393 style->ythickness = scanner->value.v_int;
3395 return G_TOKEN_NONE;
3399 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
3401 GtkRcStyle *rc_style)
3407 token = g_scanner_get_next_token (scanner);
3408 if (token != GTK_RC_TOKEN_BG_PIXMAP)
3409 return GTK_RC_TOKEN_BG_PIXMAP;
3411 token = gtk_rc_parse_state (scanner, &state);
3412 if (token != G_TOKEN_NONE)
3415 token = g_scanner_get_next_token (scanner);
3416 if (token != G_TOKEN_EQUAL_SIGN)
3417 return G_TOKEN_EQUAL_SIGN;
3419 token = g_scanner_get_next_token (scanner);
3420 if (token != G_TOKEN_STRING)
3421 return G_TOKEN_STRING;
3423 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
3424 (strcmp (scanner->value.v_string, "<none>") == 0))
3425 pixmap_file = g_strdup (scanner->value.v_string);
3427 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
3428 scanner, scanner->value.v_string);
3432 g_free (rc_style->bg_pixmap_name[state]);
3433 rc_style->bg_pixmap_name[state] = pixmap_file;
3436 return G_TOKEN_NONE;
3440 gtk_rc_check_pixmap_dir (const gchar *dir,
3441 const gchar *pixmap_file)
3445 buf = g_build_filename (dir, pixmap_file, NULL);
3447 if (g_file_test (buf, G_FILE_TEST_EXISTS))
3456 * gtk_rc_find_pixmap_in_path:
3457 * @settings: a #GtkSettings
3458 * @scanner: Scanner used to get line number information for the
3459 * warning message, or %NULL
3460 * @pixmap_file: name of the pixmap file to locate.
3462 * Looks up a file in pixmap path for the specified #GtkSettings.
3463 * If the file is not found, it outputs a warning message using
3464 * g_warning() and returns %NULL.
3466 * Return value: the filename.
3469 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
3471 const gchar *pixmap_file)
3477 GtkRcContext *context = gtk_rc_context_get (settings);
3479 if (context->pixmap_path)
3480 for (i = 0; context->pixmap_path[i] != NULL; i++)
3482 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
3487 tmp_list = current_files_stack;
3490 GtkRcFile *curfile = tmp_list->data;
3491 filename = gtk_rc_check_pixmap_dir (curfile->directory, pixmap_file);
3495 tmp_list = tmp_list->next;
3499 g_scanner_warn (scanner,
3500 _("Unable to locate image file in pixmap_path: \"%s\""),
3503 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
3510 * gtk_rc_find_module_in_path:
3511 * @module_file: name of a theme engine
3513 * Searches for a theme engine in the GTK+ search path. This function
3514 * is not useful for applications and should not be used.
3516 * Return value: The filename, if found (must be freed with g_free()),
3520 gtk_rc_find_module_in_path (const gchar *module_file)
3522 return _gtk_find_module (module_file, "engines");
3526 gtk_rc_parse_font (GScanner *scanner,
3527 GtkRcStyle *rc_style)
3531 token = g_scanner_get_next_token (scanner);
3532 if (token != GTK_RC_TOKEN_FONT)
3533 return GTK_RC_TOKEN_FONT;
3535 token = g_scanner_get_next_token (scanner);
3536 if (token != G_TOKEN_EQUAL_SIGN)
3537 return G_TOKEN_EQUAL_SIGN;
3539 token = g_scanner_get_next_token (scanner);
3540 if (token != G_TOKEN_STRING)
3541 return G_TOKEN_STRING;
3543 /* Ignore, do nothing */
3545 return G_TOKEN_NONE;
3549 gtk_rc_parse_fontset (GScanner *scanner,
3550 GtkRcStyle *rc_style)
3554 token = g_scanner_get_next_token (scanner);
3555 if (token != GTK_RC_TOKEN_FONTSET)
3556 return GTK_RC_TOKEN_FONTSET;
3558 token = g_scanner_get_next_token (scanner);
3559 if (token != G_TOKEN_EQUAL_SIGN)
3560 return G_TOKEN_EQUAL_SIGN;
3562 token = g_scanner_get_next_token (scanner);
3563 if (token != G_TOKEN_STRING)
3564 return G_TOKEN_STRING;
3566 /* Do nothing - silently ignore */
3568 return G_TOKEN_NONE;
3572 gtk_rc_parse_font_name (GScanner *scanner,
3573 GtkRcStyle *rc_style)
3577 token = g_scanner_get_next_token (scanner);
3578 if (token != GTK_RC_TOKEN_FONT_NAME)
3579 return GTK_RC_TOKEN_FONT;
3581 token = g_scanner_get_next_token (scanner);
3582 if (token != G_TOKEN_EQUAL_SIGN)
3583 return G_TOKEN_EQUAL_SIGN;
3585 token = g_scanner_get_next_token (scanner);
3586 if (token != G_TOKEN_STRING)
3587 return G_TOKEN_STRING;
3589 if (rc_style->font_desc)
3590 pango_font_description_free (rc_style->font_desc);
3592 rc_style->font_desc =
3593 pango_font_description_from_string (scanner->value.v_string);
3595 return G_TOKEN_NONE;
3599 gtk_rc_parse_engine (GtkRcContext *context,
3601 GtkRcStyle **rc_style)
3604 GtkThemeEngine *engine;
3605 guint result = G_TOKEN_NONE;
3606 GtkRcStyle *new_style = NULL;
3607 gboolean parsed_curlies = FALSE;
3608 GtkRcStylePrivate *rc_priv, *new_priv;
3610 token = g_scanner_get_next_token (scanner);
3611 if (token != GTK_RC_TOKEN_ENGINE)
3612 return GTK_RC_TOKEN_ENGINE;
3614 token = g_scanner_get_next_token (scanner);
3615 if (token != G_TOKEN_STRING)
3616 return G_TOKEN_STRING;
3618 if (!scanner->value.v_string[0])
3620 /* Support engine "" {} to mean override to the default engine
3622 token = g_scanner_get_next_token (scanner);
3623 if (token != G_TOKEN_LEFT_CURLY)
3624 return G_TOKEN_LEFT_CURLY;
3626 token = g_scanner_get_next_token (scanner);
3627 if (token != G_TOKEN_RIGHT_CURLY)
3628 return G_TOKEN_RIGHT_CURLY;
3630 parsed_curlies = TRUE;
3632 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3634 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
3636 new_style = gtk_rc_style_new ();
3637 gtk_rc_style_real_merge (new_style, *rc_style);
3639 new_style->name = g_strdup ((*rc_style)->name);
3641 /* take over icon factories and color hashes
3642 * from the to-be-deleted style
3644 new_style->icon_factories = (*rc_style)->icon_factories;
3645 (*rc_style)->icon_factories = NULL;
3646 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3647 new_priv->color_hashes = rc_priv->color_hashes;
3648 rc_priv->color_hashes = NULL;
3651 (*rc_style)->engine_specified = TRUE;
3655 engine = gtk_theme_engine_get (scanner->value.v_string);
3657 token = g_scanner_get_next_token (scanner);
3658 if (token != G_TOKEN_LEFT_CURLY)
3659 return G_TOKEN_LEFT_CURLY;
3663 GtkRcStyleClass *new_class;
3665 rc_priv = GTK_RC_STYLE_GET_PRIVATE (*rc_style);
3666 new_style = gtk_theme_engine_create_rc_style (engine);
3667 g_type_module_unuse (G_TYPE_MODULE (engine));
3669 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
3671 new_class->merge (new_style, *rc_style);
3673 new_style->name = g_strdup ((*rc_style)->name);
3675 /* take over icon factories and color hashes
3676 * from the to-be-deleted style
3678 new_style->icon_factories = (*rc_style)->icon_factories;
3679 (*rc_style)->icon_factories = NULL;
3680 new_priv = GTK_RC_STYLE_GET_PRIVATE (new_style);
3681 new_priv->color_hashes = rc_priv->color_hashes;
3682 rc_priv->color_hashes = NULL;
3684 if (new_class->parse)
3686 parsed_curlies = TRUE;
3687 result = new_class->parse (new_style, context->settings, scanner);
3689 if (result != G_TOKEN_NONE)
3691 /* copy icon factories and color hashes back
3693 (*rc_style)->icon_factories = new_style->icon_factories;
3694 new_style->icon_factories = NULL;
3695 rc_priv->color_hashes = new_priv->color_hashes;
3696 new_priv->color_hashes = NULL;
3698 g_object_unref (new_style);
3705 if (!parsed_curlies)
3707 /* Skip over remainder, looking for nested {}'s
3711 result = G_TOKEN_RIGHT_CURLY;
3712 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3714 if (token == G_TOKEN_LEFT_CURLY)
3716 else if (token == G_TOKEN_RIGHT_CURLY)
3721 result = G_TOKEN_NONE;
3729 new_style->engine_specified = TRUE;
3731 g_object_unref (*rc_style);
3732 *rc_style = new_style;
3739 gtk_rc_parse_state (GScanner *scanner,
3740 GtkStateType *state)
3745 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3746 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3748 /* we don't know where we got called from, so we reset the scope here.
3749 * if we bail out due to errors, we *don't* reset the scope, so the
3750 * error messaging code can make sense of our tokens.
3752 old_scope = g_scanner_set_scope (scanner, 0);
3754 token = g_scanner_get_next_token (scanner);
3755 if (token != G_TOKEN_LEFT_BRACE)
3756 return G_TOKEN_LEFT_BRACE;
3758 token = g_scanner_get_next_token (scanner);
3761 case GTK_RC_TOKEN_ACTIVE:
3762 *state = GTK_STATE_ACTIVE;
3764 case GTK_RC_TOKEN_INSENSITIVE:
3765 *state = GTK_STATE_INSENSITIVE;
3767 case GTK_RC_TOKEN_NORMAL:
3768 *state = GTK_STATE_NORMAL;
3770 case GTK_RC_TOKEN_PRELIGHT:
3771 *state = GTK_STATE_PRELIGHT;
3773 case GTK_RC_TOKEN_SELECTED:
3774 *state = GTK_STATE_SELECTED;
3777 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3780 token = g_scanner_get_next_token (scanner);
3781 if (token != G_TOKEN_RIGHT_BRACE)
3782 return G_TOKEN_RIGHT_BRACE;
3784 g_scanner_set_scope (scanner, old_scope);
3786 return G_TOKEN_NONE;
3790 gtk_rc_parse_priority (GScanner *scanner,
3791 GtkPathPriorityType *priority)
3796 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3797 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3799 /* we don't know where we got called from, so we reset the scope here.
3800 * if we bail out due to errors, we *don't* reset the scope, so the
3801 * error messaging code can make sense of our tokens.
3803 old_scope = g_scanner_set_scope (scanner, 0);
3805 token = g_scanner_get_next_token (scanner);
3809 token = g_scanner_get_next_token (scanner);
3812 case GTK_RC_TOKEN_LOWEST:
3813 *priority = GTK_PATH_PRIO_LOWEST;
3815 case GTK_RC_TOKEN_GTK:
3816 *priority = GTK_PATH_PRIO_GTK;
3818 case GTK_RC_TOKEN_APPLICATION:
3819 *priority = GTK_PATH_PRIO_APPLICATION;
3821 case GTK_RC_TOKEN_THEME:
3822 *priority = GTK_PATH_PRIO_THEME;
3824 case GTK_RC_TOKEN_RC:
3825 *priority = GTK_PATH_PRIO_RC;
3827 case GTK_RC_TOKEN_HIGHEST:
3828 *priority = GTK_PATH_PRIO_HIGHEST;
3831 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3834 g_scanner_set_scope (scanner, old_scope);
3836 return G_TOKEN_NONE;
3840 * gtk_rc_parse_color:
3841 * @scanner: a #GScanner
3842 * @color: a pointer to a #GdkColor structure in which to store the result
3844 * Parses a color in the <link linkend="color=format">format</link> expected
3847 * Note that theme engines should use gtk_rc_parse_color_full() in
3848 * order to support symbolic colors.
3850 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3851 * that was expected but not found
3854 gtk_rc_parse_color (GScanner *scanner,
3857 return gtk_rc_parse_color_full (scanner, NULL, color);
3861 * gtk_rc_parse_color_full:
3862 * @scanner: a #GScanner
3863 * @style: a #GtkRcStyle, or %NULL
3864 * @color: a pointer to a #GdkColor structure in which to store the result
3866 * Parses a color in the <link linkend="color=format">format</link> expected
3867 * in a RC file. If @style is not %NULL, it will be consulted to resolve
3868 * references to symbolic colors.
3870 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
3871 * that was expected but not found
3876 gtk_rc_parse_color_full (GScanner *scanner,
3882 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3884 /* we don't need to set our own scope here, because
3885 * we don't need own symbols
3888 token = g_scanner_get_next_token (scanner);
3896 case G_TOKEN_LEFT_CURLY:
3897 token = g_scanner_get_next_token (scanner);
3898 if (token == G_TOKEN_INT)
3899 token_int = scanner->value.v_int;
3900 else if (token == G_TOKEN_FLOAT)
3901 token_int = scanner->value.v_float * 65535.0;
3903 return G_TOKEN_FLOAT;
3904 color->red = CLAMP (token_int, 0, 65535);
3906 token = g_scanner_get_next_token (scanner);
3907 if (token != G_TOKEN_COMMA)
3908 return G_TOKEN_COMMA;
3910 token = g_scanner_get_next_token (scanner);
3911 if (token == G_TOKEN_INT)
3912 token_int = scanner->value.v_int;
3913 else if (token == G_TOKEN_FLOAT)
3914 token_int = scanner->value.v_float * 65535.0;
3916 return G_TOKEN_FLOAT;
3917 color->green = CLAMP (token_int, 0, 65535);
3919 token = g_scanner_get_next_token (scanner);
3920 if (token != G_TOKEN_COMMA)
3921 return G_TOKEN_COMMA;
3923 token = g_scanner_get_next_token (scanner);
3924 if (token == G_TOKEN_INT)
3925 token_int = scanner->value.v_int;
3926 else if (token == G_TOKEN_FLOAT)
3927 token_int = scanner->value.v_float * 65535.0;
3929 return G_TOKEN_FLOAT;
3930 color->blue = CLAMP (token_int, 0, 65535);
3932 token = g_scanner_get_next_token (scanner);
3933 if (token != G_TOKEN_RIGHT_CURLY)
3934 return G_TOKEN_RIGHT_CURLY;
3935 return G_TOKEN_NONE;
3937 case G_TOKEN_STRING:
3938 if (!gdk_color_parse (scanner->value.v_string, color))
3940 g_scanner_warn (scanner, "Invalid color constant '%s'",
3941 scanner->value.v_string);
3942 return G_TOKEN_STRING;
3944 return G_TOKEN_NONE;
3947 token = g_scanner_get_next_token (scanner);
3948 if (token != G_TOKEN_IDENTIFIER)
3949 return G_TOKEN_IDENTIFIER;
3951 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
3953 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
3954 scanner->value.v_identifier);
3955 return G_TOKEN_IDENTIFIER;
3958 return G_TOKEN_NONE;
3960 case G_TOKEN_IDENTIFIER:
3961 if (strcmp (scanner->value.v_identifier, "mix") == 0)
3963 token = g_scanner_get_next_token (scanner);
3964 if (token != G_TOKEN_LEFT_PAREN)
3965 return G_TOKEN_LEFT_PAREN;
3968 if (g_scanner_peek_next_token (scanner) == '-')
3970 g_scanner_get_next_token (scanner); /* eat sign */
3974 token = g_scanner_get_next_token (scanner);
3975 if (token != G_TOKEN_FLOAT)
3976 return G_TOKEN_FLOAT;
3978 l = negate ? -scanner->value.v_float : scanner->value.v_float;
3980 token = g_scanner_get_next_token (scanner);
3981 if (token != G_TOKEN_COMMA)
3982 return G_TOKEN_COMMA;
3984 token = gtk_rc_parse_color_full (scanner, style, &c1);
3985 if (token != G_TOKEN_NONE)
3988 token = g_scanner_get_next_token (scanner);
3989 if (token != G_TOKEN_COMMA)
3990 return G_TOKEN_COMMA;
3992 token = gtk_rc_parse_color_full (scanner, style, &c2);
3993 if (token != G_TOKEN_NONE)
3996 token = g_scanner_get_next_token (scanner);
3997 if (token != G_TOKEN_RIGHT_PAREN)
3998 return G_TOKEN_RIGHT_PAREN;
4000 color->red = l * c1.red + (1.0 - l) * c2.red;
4001 color->green = l * c1.green + (1.0 - l) * c2.green;
4002 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
4004 return G_TOKEN_NONE;
4006 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
4008 token = g_scanner_get_next_token (scanner);
4009 if (token != G_TOKEN_LEFT_PAREN)
4010 return G_TOKEN_LEFT_PAREN;
4013 if (g_scanner_peek_next_token (scanner) == '-')
4015 g_scanner_get_next_token (scanner); /* eat sign */
4019 token = g_scanner_get_next_token (scanner);
4020 if (token != G_TOKEN_FLOAT)
4021 return G_TOKEN_FLOAT;
4023 l = negate ? -scanner->value.v_float : scanner->value.v_float;
4025 token = g_scanner_get_next_token (scanner);
4026 if (token != G_TOKEN_COMMA)
4027 return G_TOKEN_COMMA;
4029 token = gtk_rc_parse_color_full (scanner, style, &c1);
4030 if (token != G_TOKEN_NONE)
4033 token = g_scanner_get_next_token (scanner);
4034 if (token != G_TOKEN_RIGHT_PAREN)
4035 return G_TOKEN_RIGHT_PAREN;
4037 _gtk_style_shade (&c1, color, l);
4039 return G_TOKEN_NONE;
4041 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
4042 strcmp (scanner->value.v_identifier, "darker") == 0)
4044 if (scanner->value.v_identifier[0] == 'l')
4049 token = g_scanner_get_next_token (scanner);
4050 if (token != G_TOKEN_LEFT_PAREN)
4051 return G_TOKEN_LEFT_PAREN;
4053 token = gtk_rc_parse_color_full (scanner, style, &c1);
4054 if (token != G_TOKEN_NONE)
4057 token = g_scanner_get_next_token (scanner);
4058 if (token != G_TOKEN_RIGHT_PAREN)
4059 return G_TOKEN_RIGHT_PAREN;
4061 _gtk_style_shade (&c1, color, l);
4063 return G_TOKEN_NONE;
4066 return G_TOKEN_IDENTIFIER;
4069 return G_TOKEN_STRING;
4074 gtk_rc_parse_pixmap_path (GtkRcContext *context,
4079 token = g_scanner_get_next_token (scanner);
4080 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
4081 return GTK_RC_TOKEN_PIXMAP_PATH;
4083 token = g_scanner_get_next_token (scanner);
4084 if (token != G_TOKEN_STRING)
4085 return G_TOKEN_STRING;
4087 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
4089 return G_TOKEN_NONE;
4093 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
4095 const gchar *pix_path)
4097 g_strfreev (context->pixmap_path);
4098 context->pixmap_path = g_strsplit (pix_path, G_SEARCHPATH_SEPARATOR_S, -1);
4102 gtk_rc_parse_module_path (GScanner *scanner)
4106 token = g_scanner_get_next_token (scanner);
4107 if (token != GTK_RC_TOKEN_MODULE_PATH)
4108 return GTK_RC_TOKEN_MODULE_PATH;
4110 token = g_scanner_get_next_token (scanner);
4111 if (token != G_TOKEN_STRING)
4112 return G_TOKEN_STRING;
4114 g_warning ("module_path directive is now ignored\n");
4116 return G_TOKEN_NONE;
4120 gtk_rc_parse_im_module_file (GScanner *scanner)
4124 token = g_scanner_get_next_token (scanner);
4125 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
4126 return GTK_RC_TOKEN_IM_MODULE_FILE;
4128 token = g_scanner_get_next_token (scanner);
4129 if (token != G_TOKEN_STRING)
4130 return G_TOKEN_STRING;
4132 g_free (im_module_file);
4134 im_module_file = g_strdup (scanner->value.v_string);
4136 return G_TOKEN_NONE;
4140 gtk_rc_parse_path_pattern (GtkRcContext *context,
4144 GtkPathType path_type;
4146 gboolean is_binding;
4147 GtkPathPriorityType priority = context->default_priority;
4149 token = g_scanner_get_next_token (scanner);
4152 case GTK_RC_TOKEN_WIDGET:
4153 path_type = GTK_PATH_WIDGET;
4155 case GTK_RC_TOKEN_WIDGET_CLASS:
4156 path_type = GTK_PATH_WIDGET_CLASS;
4158 case GTK_RC_TOKEN_CLASS:
4159 path_type = GTK_PATH_CLASS;
4162 return GTK_RC_TOKEN_WIDGET_CLASS;
4165 token = g_scanner_get_next_token (scanner);
4166 if (token != G_TOKEN_STRING)
4167 return G_TOKEN_STRING;
4169 pattern = g_strdup (scanner->value.v_string);
4171 token = g_scanner_get_next_token (scanner);
4172 if (token == GTK_RC_TOKEN_STYLE)
4174 else if (token == GTK_RC_TOKEN_BINDING)
4179 return GTK_RC_TOKEN_STYLE;
4182 if (g_scanner_peek_next_token (scanner) == ':')
4184 token = gtk_rc_parse_priority (scanner, &priority);
4185 if (token != G_TOKEN_NONE)
4192 token = g_scanner_get_next_token (scanner);
4193 if (token != G_TOKEN_STRING)
4196 return G_TOKEN_STRING;
4201 GtkBindingSet *binding;
4203 binding = gtk_binding_set_find (scanner->value.v_string);
4207 return G_TOKEN_STRING;
4209 gtk_binding_set_add_path (binding, path_type, pattern, priority);
4213 GtkRcStyle *rc_style;
4216 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
4221 return G_TOKEN_STRING;
4224 rc_set = g_new (GtkRcSet, 1);
4225 rc_set->type = path_type;
4227 if (path_type == GTK_PATH_WIDGET_CLASS)
4229 rc_set->pspec = NULL;
4230 rc_set->path = _gtk_rc_parse_widget_class_path (pattern);
4234 rc_set->pspec = g_pattern_spec_new (pattern);
4235 rc_set->path = NULL;
4238 rc_set->rc_style = rc_style;
4239 rc_set->priority = priority;
4241 if (path_type == GTK_PATH_WIDGET)
4242 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
4243 else if (path_type == GTK_PATH_WIDGET_CLASS)
4244 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
4246 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
4250 return G_TOKEN_NONE;
4254 gtk_rc_parse_hash_key (GScanner *scanner,
4259 token = g_scanner_get_next_token (scanner);
4260 if (token != G_TOKEN_LEFT_BRACE)
4261 return G_TOKEN_LEFT_BRACE;
4263 token = g_scanner_get_next_token (scanner);
4265 if (token != G_TOKEN_STRING)
4266 return G_TOKEN_STRING;
4268 *hash_key = g_strdup (scanner->value.v_string);
4270 token = g_scanner_get_next_token (scanner);
4271 if (token != G_TOKEN_RIGHT_BRACE)
4274 return G_TOKEN_RIGHT_BRACE;
4277 return G_TOKEN_NONE;
4281 gtk_rc_parse_icon_source (GtkRcContext *context,
4283 GtkIconSet *icon_set,
4284 gboolean *icon_set_valid)
4287 gchar *full_filename;
4288 GtkIconSource *source = NULL;
4290 token = g_scanner_get_next_token (scanner);
4291 if (token != G_TOKEN_LEFT_CURLY)
4292 return G_TOKEN_LEFT_CURLY;
4294 token = g_scanner_get_next_token (scanner);
4296 if (token != G_TOKEN_STRING && token != '@')
4297 return G_TOKEN_STRING;
4299 if (token == G_TOKEN_STRING)
4303 source = gtk_icon_source_new ();
4304 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
4307 gtk_icon_source_set_filename (source, full_filename);
4308 g_free (full_filename);
4315 token = g_scanner_get_next_token (scanner);
4317 if (token != G_TOKEN_STRING)
4318 return G_TOKEN_STRING;
4320 source = gtk_icon_source_new ();
4321 gtk_icon_source_set_icon_name (source, scanner->value.v_string);
4324 /* We continue parsing even if we didn't find the pixmap so that rest of the
4325 * file is read, even if the syntax is bad. However we don't validate the
4326 * icon_set so the caller can choose not to install it.
4328 token = g_scanner_get_next_token (scanner);
4330 if (token == G_TOKEN_RIGHT_CURLY)
4332 else if (token != G_TOKEN_COMMA)
4334 gtk_icon_source_free (source);
4335 return G_TOKEN_COMMA;
4338 /* Get the direction */
4340 token = g_scanner_get_next_token (scanner);
4344 case GTK_RC_TOKEN_RTL:
4345 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4346 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
4349 case GTK_RC_TOKEN_LTR:
4350 gtk_icon_source_set_direction_wildcarded (source, FALSE);
4351 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
4358 gtk_icon_source_free (source);
4359 return GTK_RC_TOKEN_RTL;
4363 token = g_scanner_get_next_token (scanner);
4365 if (token == G_TOKEN_RIGHT_CURLY)
4367 else if (token != G_TOKEN_COMMA)
4369 gtk_icon_source_free (source);
4370 return G_TOKEN_COMMA;
4375 token = g_scanner_get_next_token (scanner);
4379 case GTK_RC_TOKEN_NORMAL:
4380 gtk_icon_source_set_state_wildcarded (source, FALSE);
4381 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
4384 case GTK_RC_TOKEN_PRELIGHT:
4385 gtk_icon_source_set_state_wildcarded (source, FALSE);
4386 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
4390 case GTK_RC_TOKEN_INSENSITIVE:
4391 gtk_icon_source_set_state_wildcarded (source, FALSE);
4392 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
4395 case GTK_RC_TOKEN_ACTIVE:
4396 gtk_icon_source_set_state_wildcarded (source, FALSE);
4397 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
4400 case GTK_RC_TOKEN_SELECTED:
4401 gtk_icon_source_set_state_wildcarded (source, FALSE);
4402 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
4409 gtk_icon_source_free (source);
4410 return GTK_RC_TOKEN_PRELIGHT;
4414 token = g_scanner_get_next_token (scanner);
4416 if (token == G_TOKEN_RIGHT_CURLY)
4418 else if (token != G_TOKEN_COMMA)
4420 gtk_icon_source_free (source);
4421 return G_TOKEN_COMMA;
4426 token = g_scanner_get_next_token (scanner);
4432 if (token != G_TOKEN_STRING)
4434 gtk_icon_source_free (source);
4435 return G_TOKEN_STRING;
4438 size = gtk_icon_size_from_name (scanner->value.v_string);
4440 if (size != GTK_ICON_SIZE_INVALID)
4442 gtk_icon_source_set_size_wildcarded (source, FALSE);
4443 gtk_icon_source_set_size (source, size);
4447 /* Check the close brace */
4449 token = g_scanner_get_next_token (scanner);
4450 if (token != G_TOKEN_RIGHT_CURLY)
4452 gtk_icon_source_free (source);
4453 return G_TOKEN_RIGHT_CURLY;
4457 if (gtk_icon_source_get_filename (source) ||
4458 gtk_icon_source_get_icon_name (source))
4460 gtk_icon_set_add_source (icon_set, source);
4461 *icon_set_valid = TRUE;
4463 gtk_icon_source_free (source);
4465 return G_TOKEN_NONE;
4469 gtk_rc_parse_stock (GtkRcContext *context,
4471 GtkRcStyle *rc_style,
4472 GtkIconFactory *factory)
4474 GtkIconSet *icon_set = NULL;
4475 gboolean icon_set_valid = FALSE;
4476 gchar *stock_id = NULL;
4479 token = g_scanner_get_next_token (scanner);
4480 if (token != GTK_RC_TOKEN_STOCK)
4481 return GTK_RC_TOKEN_STOCK;
4483 token = gtk_rc_parse_hash_key (scanner, &stock_id);
4484 if (token != G_TOKEN_NONE)
4487 token = g_scanner_get_next_token (scanner);
4488 if (token != G_TOKEN_EQUAL_SIGN)
4491 return G_TOKEN_EQUAL_SIGN;
4494 token = g_scanner_get_next_token (scanner);
4495 if (token != G_TOKEN_LEFT_CURLY)
4498 return G_TOKEN_LEFT_CURLY;
4501 token = g_scanner_peek_next_token (scanner);
4502 while (token != G_TOKEN_RIGHT_CURLY)
4504 if (icon_set == NULL)
4505 icon_set = gtk_icon_set_new ();
4507 token = gtk_rc_parse_icon_source (context,
4508 scanner, icon_set, &icon_set_valid);
4509 if (token != G_TOKEN_NONE)
4512 gtk_icon_set_unref (icon_set);
4516 token = g_scanner_get_next_token (scanner);
4518 if (token != G_TOKEN_COMMA &&
4519 token != G_TOKEN_RIGHT_CURLY)
4522 gtk_icon_set_unref (icon_set);
4523 return G_TOKEN_RIGHT_CURLY;
4530 gtk_icon_factory_add (factory,
4534 gtk_icon_set_unref (icon_set);
4539 return G_TOKEN_NONE;
4543 gtk_rc_parse_logical_color (GScanner *scanner,
4544 GtkRcStyle *rc_style,
4547 gchar *color_id = NULL;
4551 token = g_scanner_get_next_token (scanner);
4552 if (token != GTK_RC_TOKEN_COLOR)
4553 return GTK_RC_TOKEN_COLOR;
4555 token = gtk_rc_parse_hash_key (scanner, &color_id);
4556 if (token != G_TOKEN_NONE)
4559 token = g_scanner_get_next_token (scanner);
4560 if (token != G_TOKEN_EQUAL_SIGN)
4563 return G_TOKEN_EQUAL_SIGN;
4566 token = gtk_rc_parse_color_full (scanner, rc_style, &color);
4567 if (token != G_TOKEN_NONE)
4573 /* Because the hash is created with destroy functions,
4574 * g_hash_table_insert will free any old values for us,
4575 * if a mapping with the specified key already exists.
4577 g_hash_table_insert (hash, color_id, gdk_color_copy (&color));
4579 return G_TOKEN_NONE;
4584 _gtk_rc_parse_widget_class_path (const gchar *pattern)
4588 const gchar *current;
4589 const gchar *class_start;
4590 const gchar *class_end;
4591 const gchar *pattern_end;
4592 const gchar *pattern_start;
4597 while ((class_start = strchr (current, '<')) &&
4598 (class_end = strchr (class_start, '>')))
4600 /* Add patterns, but ignore single dots */
4601 if (!(class_start == current ||
4602 (class_start == current + 1 && current[0] == '.')))
4604 pattern_end = class_start - 1;
4605 pattern_start = current;
4607 path_elt = g_new (PathElt, 1);
4609 sub_pattern = g_strndup (pattern_start, pattern_end - pattern_start + 1);
4610 path_elt->type = PATH_ELT_PSPEC;
4611 path_elt->elt.pspec = g_pattern_spec_new (sub_pattern);
4612 g_free (sub_pattern);
4614 result = g_slist_prepend (result, path_elt);
4617 path_elt = g_new (PathElt, 1);
4619 /* The < > need to be removed from the string. */
4620 sub_pattern = g_strndup (class_start + 1, class_end - class_start - 1);
4622 path_elt->type = PATH_ELT_UNRESOLVED;
4623 path_elt->elt.class_name = sub_pattern;
4625 result = g_slist_prepend (result, path_elt);
4627 current = class_end + 1;
4630 /* Add the rest, if anything is left */
4631 if (strlen (current) > 0)
4633 path_elt = g_new (PathElt, 1);
4634 path_elt->type = PATH_ELT_PSPEC;
4635 path_elt->elt.pspec = g_pattern_spec_new (current);
4637 result = g_slist_prepend (result, path_elt);
4640 return g_slist_reverse (result);
4644 free_path_elt (gpointer data,
4647 PathElt *path_elt = data;
4649 switch (path_elt->type)
4651 case PATH_ELT_PSPEC:
4652 g_pattern_spec_free (path_elt->elt.pspec);
4654 case PATH_ELT_UNRESOLVED:
4655 g_free (path_elt->elt.class_name);
4660 g_assert_not_reached ();
4667 _gtk_rc_free_widget_class_path (GSList *list)
4669 g_slist_foreach (list, free_path_elt, NULL);
4670 g_slist_free (list);
4674 gtk_rc_set_free (GtkRcSet *rc_set)
4677 g_pattern_spec_free (rc_set->pspec);
4679 _gtk_rc_free_widget_class_path (rc_set->path);
4685 match_class (PathElt *path_elt,
4690 if (path_elt->type == PATH_ELT_UNRESOLVED)
4692 type = g_type_from_name (path_elt->elt.class_name);
4693 if (type != G_TYPE_INVALID)
4695 g_free (path_elt->elt.class_name);
4696 path_elt->elt.class_type = type;
4697 path_elt->type = PATH_ELT_TYPE;
4700 return g_str_equal (type_name, path_elt->elt.class_name);
4703 return g_type_is_a (g_type_from_name (type_name), path_elt->elt.class_type);
4707 match_widget_class_recursive (GSList *list,
4710 gchar *path_reversed)
4714 /* break out if we cannot match anymore. */
4723 /* there are two possibilities:
4724 * 1. The next pattern should match the class.
4725 * 2. First normal matching, and then maybe a class */
4727 path_elt = list->data;
4729 if (path_elt->type != PATH_ELT_PSPEC)
4731 gchar *class_start = path;
4734 /* ignore leading dot */
4735 if (class_start[0] == '.')
4737 class_end = strchr (class_start, '.');
4739 if (class_end == NULL)
4741 if (!match_class (path_elt, class_start))
4744 return match_widget_class_recursive (list->next, 0, "", "");
4748 class_end[0] = '\0';
4749 if (!match_class (path_elt, class_start))
4757 gint new_length = length - (class_end - path);
4758 gchar old_char = path_reversed[new_length];
4762 path_reversed[new_length] = '\0';
4763 result = match_widget_class_recursive (list->next, new_length, class_end, path_reversed);
4764 path_reversed[new_length] = old_char;
4775 gboolean result = FALSE;
4777 /* If there is nothing after this (ie. no class match),
4778 * just compare the pspec.
4780 if (list->next == NULL)
4781 return g_pattern_match (path_elt->elt.pspec, length, path, path_reversed);
4783 class_elt = (PathElt *)list->next->data;
4784 g_assert (class_elt->type != PATH_ELT_PSPEC);
4787 if (class_start[0] == '.')
4792 class_end = strchr (class_start, '.');
4794 /* It should be cheaper to match the class first. (either the pattern
4795 * is simple, and will match most of the times, or it may be complex
4796 * and matching is slow)
4798 if (class_end == NULL)
4800 result = match_class (class_elt, class_start);
4804 class_end[0] = '\0';
4805 result = match_class (class_elt, class_start);
4814 /* terminate the string in front of the class. It does not matter
4815 * that the class becomes unusable, because it is not needed
4816 * inside the recursion
4818 old_char = class_start[0];
4819 class_start[0] = '\0';
4821 if (g_pattern_match (path_elt->elt.pspec, class_start - path, path, path_reversed + length - (class_start - path)))
4823 if (class_end != NULL)
4825 gint new_length = length - (class_end - path);
4826 gchar path_reversed_char = path_reversed[new_length];
4828 path_reversed[new_length] = '\0';
4830 result = match_widget_class_recursive (list->next->next, new_length, class_end, path_reversed);
4832 path_reversed[new_length] = path_reversed_char;
4835 result = match_widget_class_recursive (list->next->next, 0, "", "");
4838 class_start[0] = old_char;
4844 /* get next class in path, or break out */
4845 if (class_end != NULL)
4846 class_start = class_end + 1;
4854 _gtk_rc_match_widget_class (GSList *list,
4857 gchar *path_reversed)
4859 return match_widget_class_recursive (list, length, path, path_reversed);
4864 /* DLL ABI stability backward compatibility versions */
4866 #undef gtk_rc_add_default_file
4869 gtk_rc_add_default_file (const gchar *filename)
4871 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4873 gtk_rc_add_default_file_utf8 (utf8_filename);
4875 g_free (utf8_filename);
4878 #undef gtk_rc_set_default_files
4881 gtk_rc_set_default_files (gchar **filenames)
4883 gchar **utf8_filenames;
4886 while (filenames[n++] != NULL)
4889 utf8_filenames = g_new (gchar *, n + 1);
4891 for (i = 0; i < n; i++)
4892 utf8_filenames[i] = g_locale_to_utf8 (filenames[i], -1, NULL, NULL, NULL);
4894 utf8_filenames[n] = NULL;
4896 gtk_rc_set_default_files_utf8 (utf8_filenames);
4898 g_strfreev (utf8_filenames);
4904 gtk_rc_parse (const gchar *filename)
4906 gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, NULL);
4908 gtk_rc_parse_utf8 (utf8_filename);
4910 g_free (utf8_filename);
4915 #define __GTK_RC_C__
4916 #include "gtkaliasdef.c"