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/.
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
48 #include "gdkconfig.h"
50 #include "gtkversion.h"
52 #include "gtkbindings.h"
53 #include "gtkthemes.h"
55 #include "gtkiconfactory.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
58 #include "gtkwindow.h"
64 typedef struct _GtkRcSet GtkRcSet;
65 typedef struct _GtkRcNode GtkRcNode;
66 typedef struct _GtkRcFile GtkRcFile;
79 gchar *canonical_name;
83 #define GTK_RC_MAX_PIXMAP_PATHS 128
87 GHashTable *rc_style_ht;
88 GtkSettings *settings;
89 GSList *rc_sets_widget;
90 GSList *rc_sets_widget_class;
91 GSList *rc_sets_class;
93 /* The files we have parsed, to reread later if necessary */
97 gchar *key_theme_name;
99 gchar *pixmap_path[GTK_RC_MAX_PIXMAP_PATHS];
101 gint default_priority;
104 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
106 static guint gtk_rc_style_hash (const gchar *name);
107 static gboolean gtk_rc_style_equal (const gchar *a,
109 static guint gtk_rc_styles_hash (const GSList *rc_styles);
110 static gboolean gtk_rc_styles_equal (const GSList *a,
112 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
114 static GSList * gtk_rc_styles_match (GSList *rc_styles,
118 const gchar *path_reversed);
119 static GtkStyle * gtk_rc_style_to_style (GtkRcStyle *rc_style);
120 static GtkStyle* gtk_rc_init_style (GSList *rc_styles);
121 static void gtk_rc_parse_default_files (GtkRcContext *context);
122 static void gtk_rc_parse_named (GtkRcContext *context,
125 static void gtk_rc_parse_file (GtkRcContext *context,
126 const gchar *filename,
129 static void gtk_rc_parse_any (GtkRcContext *context,
130 const gchar *input_name,
132 const gchar *input_string);
133 static guint gtk_rc_parse_statement (GtkRcContext *context,
135 static guint gtk_rc_parse_style (GtkRcContext *context,
137 static guint gtk_rc_parse_assignment (GScanner *scanner,
138 GtkRcProperty *prop);
139 static guint gtk_rc_parse_bg (GScanner *scanner,
141 static guint gtk_rc_parse_fg (GScanner *scanner,
143 static guint gtk_rc_parse_text (GScanner *scanner,
145 static guint gtk_rc_parse_base (GScanner *scanner,
147 static guint gtk_rc_parse_xthickness (GScanner *scanner,
149 static guint gtk_rc_parse_ythickness (GScanner *scanner,
151 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
153 GtkRcStyle *rc_style);
154 static guint gtk_rc_parse_font (GScanner *scanner,
155 GtkRcStyle *rc_style);
156 static guint gtk_rc_parse_fontset (GScanner *scanner,
157 GtkRcStyle *rc_style);
158 static guint gtk_rc_parse_font_name (GScanner *scanner,
159 GtkRcStyle *rc_style);
160 static guint gtk_rc_parse_engine (GtkRcContext *context,
162 GtkRcStyle **rc_style);
163 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
165 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
167 const gchar *pix_path);
168 static guint gtk_rc_parse_module_path (GScanner *scanner);
169 static void gtk_rc_parse_module_path_string (const gchar *mod_path);
170 static guint gtk_rc_parse_im_module_path (GScanner *scanner);
171 static guint gtk_rc_parse_im_module_file (GScanner *scanner);
172 static guint gtk_rc_parse_path_pattern (GtkRcContext *context,
174 static guint gtk_rc_parse_stock (GtkRcContext *context,
176 GtkRcStyle *rc_style,
177 GtkIconFactory *factory);
178 static void gtk_rc_clear_hash_node (gpointer key,
181 static void gtk_rc_clear_styles (GtkRcContext *context);
182 static void gtk_rc_append_default_module_path (void);
183 static void gtk_rc_add_initial_default_files (void);
185 static void gtk_rc_style_init (GtkRcStyle *style);
186 static void gtk_rc_style_class_init (GtkRcStyleClass *klass);
187 static void gtk_rc_style_finalize (GObject *object);
188 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
190 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
191 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
192 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
193 gconstpointer bsearch_node2);
195 static gpointer parent_class = NULL;
197 static const GScannerConfig gtk_rc_scanner_config =
201 ) /* cset_skip_characters */,
206 ) /* cset_identifier_first */,
211 ) /* cset_identifier_nth */,
212 ( "#\n" ) /* cpair_comment_single */,
214 TRUE /* case_sensitive */,
216 TRUE /* skip_comment_multi */,
217 TRUE /* skip_comment_single */,
218 TRUE /* scan_comment_multi */,
219 TRUE /* scan_identifier */,
220 FALSE /* scan_identifier_1char */,
221 FALSE /* scan_identifier_NULL */,
222 TRUE /* scan_symbols */,
223 TRUE /* scan_binary */,
224 TRUE /* scan_octal */,
225 TRUE /* scan_float */,
227 TRUE /* scan_hex_dollar */,
228 TRUE /* scan_string_sq */,
229 TRUE /* scan_string_dq */,
230 TRUE /* numbers_2_int */,
231 FALSE /* int_2_float */,
232 FALSE /* identifier_2_string */,
233 TRUE /* char_2_token */,
234 TRUE /* symbol_2_token */,
235 FALSE /* scope_0_fallback */,
243 { "include", GTK_RC_TOKEN_INCLUDE },
244 { "NORMAL", GTK_RC_TOKEN_NORMAL },
245 { "ACTIVE", GTK_RC_TOKEN_ACTIVE },
246 { "PRELIGHT", GTK_RC_TOKEN_PRELIGHT },
247 { "SELECTED", GTK_RC_TOKEN_SELECTED },
248 { "INSENSITIVE", GTK_RC_TOKEN_INSENSITIVE },
249 { "fg", GTK_RC_TOKEN_FG },
250 { "bg", GTK_RC_TOKEN_BG },
251 { "text", GTK_RC_TOKEN_TEXT },
252 { "base", GTK_RC_TOKEN_BASE },
253 { "xthickness", GTK_RC_TOKEN_XTHICKNESS },
254 { "ythickness", GTK_RC_TOKEN_YTHICKNESS },
255 { "font", GTK_RC_TOKEN_FONT },
256 { "fontset", GTK_RC_TOKEN_FONTSET },
257 { "font_name", GTK_RC_TOKEN_FONT_NAME },
258 { "bg_pixmap", GTK_RC_TOKEN_BG_PIXMAP },
259 { "pixmap_path", GTK_RC_TOKEN_PIXMAP_PATH },
260 { "style", GTK_RC_TOKEN_STYLE },
261 { "binding", GTK_RC_TOKEN_BINDING },
262 { "bind", GTK_RC_TOKEN_BIND },
263 { "widget", GTK_RC_TOKEN_WIDGET },
264 { "widget_class", GTK_RC_TOKEN_WIDGET_CLASS },
265 { "class", GTK_RC_TOKEN_CLASS },
266 { "lowest", GTK_RC_TOKEN_LOWEST },
267 { "gtk", GTK_RC_TOKEN_GTK },
268 { "application", GTK_RC_TOKEN_APPLICATION },
269 { "theme", GTK_RC_TOKEN_THEME },
270 { "rc", GTK_RC_TOKEN_RC },
271 { "highest", GTK_RC_TOKEN_HIGHEST },
272 { "engine", GTK_RC_TOKEN_ENGINE },
273 { "module_path", GTK_RC_TOKEN_MODULE_PATH },
274 { "stock", GTK_RC_TOKEN_STOCK },
275 { "im_module_path", GTK_RC_TOKEN_IM_MODULE_PATH },
276 { "im_module_file", GTK_RC_TOKEN_IM_MODULE_FILE },
277 { "LTR", GTK_RC_TOKEN_LTR },
278 { "RTL", GTK_RC_TOKEN_RTL }
281 static GHashTable *realized_style_ht = NULL;
283 static gchar *im_module_path = NULL;
284 static gchar *im_module_file = NULL;
286 #define GTK_RC_MAX_DEFAULT_FILES 128
287 static gchar *gtk_rc_default_files[GTK_RC_MAX_DEFAULT_FILES];
289 #define GTK_RC_MAX_MODULE_PATHS 128
290 static gchar *module_path[GTK_RC_MAX_MODULE_PATHS];
292 /* A stack of directories for RC files we are parsing currently.
293 * these are implicitely added to the end of PIXMAP_PATHS
295 static GSList *rc_dir_stack = NULL;
297 /* RC file handling */
300 gtk_rc_make_default_dir (const gchar *type)
304 var = getenv("GTK_EXE_PREFIX");
306 path = g_build_filename (var, "lib", "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
308 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
314 gtk_rc_get_im_module_path (void)
316 const gchar *result = g_getenv ("GTK_IM_MODULE_PATH");
321 result = im_module_path;
323 return gtk_rc_make_default_dir ("immodules");
326 return g_strdup (result);
330 gtk_rc_get_im_module_file (void)
332 gchar *result = g_strdup (g_getenv ("GTK_IM_MODULE_FILE"));
337 result = g_strdup (im_module_file);
339 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
346 gtk_rc_get_theme_dir (void)
350 var = getenv("GTK_DATA_PREFIX");
352 path = g_build_filename (var, "share", "themes", NULL);
354 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
360 gtk_rc_get_module_dir (void)
362 return gtk_rc_make_default_dir ("engines");
366 gtk_rc_append_default_module_path(void)
372 for (n = 0; module_path[n]; n++) ;
373 if (n >= GTK_RC_MAX_MODULE_PATHS - 1)
376 var = getenv("GTK_EXE_PREFIX");
378 path = g_build_filename (var, "lib", "gtk-2.0", GTK_VERSION, "engines", NULL);
380 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_VERSION, "engines", NULL);
381 module_path[n++] = path;
383 var = g_get_home_dir ();
386 path = g_build_filename (var, ".gtk-2.0", GTK_VERSION, "engines", NULL);
387 module_path[n++] = path;
389 module_path[n] = NULL;
393 gtk_rc_add_initial_default_files (void)
395 static gint init = FALSE;
404 gtk_rc_default_files[0] = NULL;
407 var = g_getenv("GTK_RC_FILES");
410 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, 128);
414 gtk_rc_add_default_file (files[i]);
421 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
423 gtk_rc_add_default_file (str);
426 var = g_get_home_dir ();
429 str = g_build_filename (var, ".gtkrc-2.0", NULL);
430 gtk_rc_add_default_file (str);
437 * gtk_rc_add_default_file:
438 * @filename: the pathname to the file.
440 * Adds a file to the list of files to be parsed at the
444 gtk_rc_add_default_file (const gchar *filename)
448 gtk_rc_add_initial_default_files ();
450 for (n = 0; gtk_rc_default_files[n]; n++) ;
451 if (n >= GTK_RC_MAX_DEFAULT_FILES - 1)
454 gtk_rc_default_files[n++] = g_strdup (filename);
455 gtk_rc_default_files[n] = NULL;
459 * gtk_rc_set_default_files:
460 * @filenames: A %NULL-terminated list of filenames.
462 * Sets the list of files that GTK+ will read at the
466 gtk_rc_set_default_files (gchar **filenames)
470 gtk_rc_add_initial_default_files ();
473 while (gtk_rc_default_files[i])
475 g_free (gtk_rc_default_files[i]);
479 gtk_rc_default_files[0] = NULL;
482 while (filenames[i] != NULL)
484 gtk_rc_add_default_file (filenames[i]);
490 * gtk_rc_get_default_files:
492 * Retrieves the current list of RC files that will be parsed
493 * at the end of gtk_init().
495 * Return value: A %NULL-terminated array of filenames. This memory
496 * is owned by GTK+ and must not be freed by the application.
497 * If you want to store this information, you should make a copy.
500 gtk_rc_get_default_files (void)
502 gtk_rc_add_initial_default_files ();
504 return gtk_rc_default_files;
507 /* The following routine is based on _nl_normalize_codeset from
508 * the GNU C library. Contributed by
510 * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
511 * Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
513 * Normalize codeset name. There is no standard for the codeset
514 * names. Normalization allows the user to use any of the common
518 _gtk_normalize_codeset (const gchar *codeset, gint name_len)
526 for (cnt = 0; cnt < name_len; ++cnt)
527 if (isalnum (codeset[cnt]))
531 if (isalpha (codeset[cnt]))
535 retval = g_malloc ((only_digit ? 3 : 0) + len + 1);
539 memcpy (retval, "iso", 4);
545 for (cnt = 0; cnt < name_len; ++cnt)
546 if (isalpha (codeset[cnt]))
547 *wp++ = isupper(codeset[cnt]) ? tolower (codeset[cnt]) : codeset[cnt];
548 else if (isdigit (codeset[cnt]))
549 *wp++ = codeset[cnt];
557 gtk_rc_settings_changed (GtkSettings *settings,
559 GtkRcContext *context)
561 gchar *new_theme_name;
562 gchar *new_key_theme_name;
564 g_object_get (settings,
565 "gtk-theme-name", &new_theme_name,
566 "gtk-key-theme-name", &new_key_theme_name,
569 if ((new_theme_name != context->theme_name &&
570 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
571 (new_key_theme_name != context->key_theme_name &&
572 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
574 gtk_rc_reparse_all_for_settings (settings, TRUE);
577 g_free (new_theme_name);
578 g_free (new_key_theme_name);
581 static GtkRcContext *
582 gtk_rc_context_get (GtkSettings *settings)
584 if (!settings->rc_context)
586 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
588 context->settings = settings;
589 context->rc_style_ht = NULL;
590 context->rc_sets_widget = NULL;
591 context->rc_sets_widget_class = NULL;
592 context->rc_sets_class = NULL;
593 context->rc_files = NULL;
595 g_object_get (settings,
596 "gtk-theme-name", &context->theme_name,
597 "gtk-key-theme-name", &context->key_theme_name,
600 g_signal_connect (settings,
601 "notify::gtk-theme-name",
602 G_CALLBACK (gtk_rc_settings_changed),
604 g_signal_connect (settings,
605 "notify::gtk-key-theme-name",
606 G_CALLBACK (gtk_rc_settings_changed),
609 context->pixmap_path[0] = NULL;
611 context->default_priority = GTK_PATH_PRIO_RC;
614 return settings->rc_context;
618 gtk_rc_parse_named (GtkRcContext *context,
623 const gchar *home_dir;
627 subpath = g_strconcat ("gtk-2.0-", type,
628 G_DIR_SEPARATOR_S "gtkrc",
631 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
633 /* First look in the users home directory
635 home_dir = g_get_home_dir ();
638 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
639 if (!g_file_test (path, G_FILE_TEST_EXISTS))
648 gchar *theme_dir = gtk_rc_get_theme_dir ();
649 path = g_build_filename (theme_dir, name, subpath, NULL);
652 if (!g_file_test (path, G_FILE_TEST_EXISTS))
661 gtk_rc_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
669 gtk_rc_parse_default_files (GtkRcContext *context)
671 gchar *locale_suffixes[3];
672 gint n_locale_suffixes = 0;
679 locale = g_win32_getlocale ();
681 locale = setlocale (LC_CTYPE, NULL);
684 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
686 /* Determine locale-specific suffixes for RC files
688 * We normalize the charset into a standard form,
689 * which has all '-' and '_' characters removed,
692 gchar *normalized_locale;
694 p = strchr (locale, '@');
695 length = p ? (p -locale) : strlen (locale);
697 p = strchr (locale, '.');
700 gchar *tmp1 = g_strndup (locale, p - locale + 1);
701 gchar *tmp2 = _gtk_normalize_codeset (p + 1, length - (p - locale + 1));
703 normalized_locale = g_strconcat (tmp1, tmp2, NULL);
707 locale_suffixes[n_locale_suffixes++] = g_strdup (normalized_locale);
711 normalized_locale = g_strndup (locale, length);
713 p = strchr (normalized_locale, '_');
716 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
717 length = p - normalized_locale;
720 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
722 g_free (normalized_locale);
725 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
727 /* Try to find a locale specific RC file corresponding to the
728 * current locale to parse before the default file.
730 for (j = n_locale_suffixes - 1; j >= 0; j--)
732 gchar *name = g_strconcat (gtk_rc_default_files[i],
736 gtk_rc_parse_file (context, name, GTK_PATH_PRIO_RC, FALSE);
739 gtk_rc_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
742 for (j = 0; j < n_locale_suffixes; j++)
743 g_free (locale_suffixes[j]);
749 static gboolean initialized = FALSE;
755 module_path[0] = NULL;
756 gtk_rc_append_default_module_path();
758 gtk_rc_add_initial_default_files ();
761 gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), TRUE);
765 gtk_rc_parse_string (const gchar *rc_string)
767 g_return_if_fail (rc_string != NULL);
769 gtk_rc_parse_any (gtk_rc_context_get (gtk_settings_get_default ()),
770 "-", -1, rc_string); /* FIXME */
774 gtk_rc_parse_file (GtkRcContext *context,
775 const gchar *filename,
779 GtkRcFile *rc_file = NULL;
784 g_return_if_fail (filename != NULL);
786 saved_priority = context->default_priority;
787 context->default_priority = priority;
789 tmp_list = context->rc_files;
792 rc_file = tmp_list->data;
793 if (!strcmp (rc_file->name, filename))
796 tmp_list = tmp_list->next;
801 rc_file = g_new (GtkRcFile, 1);
802 rc_file->name = g_strdup (filename);
803 rc_file->canonical_name = NULL;
805 rc_file->reload = reload;
807 context->rc_files = g_slist_append (context->rc_files, rc_file);
810 if (!rc_file->canonical_name)
812 /* Get the absolute pathname */
814 if (g_path_is_absolute (rc_file->name))
815 rc_file->canonical_name = rc_file->name;
820 cwd = g_get_current_dir ();
821 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
826 if (!lstat (rc_file->canonical_name, &statbuf))
831 rc_file->mtime = statbuf.st_mtime;
833 fd = open (rc_file->canonical_name, O_RDONLY);
837 /* Temporarily push directory name for this file on
838 * a stack of directory names while parsing it
841 g_slist_prepend (rc_dir_stack,
842 g_path_get_dirname (rc_file->canonical_name));
843 gtk_rc_parse_any (context, filename, fd, NULL);
845 tmp_list = rc_dir_stack;
846 rc_dir_stack = rc_dir_stack->next;
848 g_free (tmp_list->data);
849 g_slist_free_1 (tmp_list);
855 context->default_priority = saved_priority;
859 gtk_rc_parse (const gchar *filename)
861 g_return_if_fail (filename != NULL);
863 gtk_rc_parse_file (gtk_rc_context_get (gtk_settings_get_default ()),
864 filename, GTK_PATH_PRIO_RC, TRUE); /* FIXME */
867 /* Handling of RC styles */
870 gtk_rc_style_get_type (void)
872 static GType object_type = 0;
876 static const GTypeInfo object_info =
878 sizeof (GtkRcStyleClass),
879 (GBaseInitFunc) NULL,
880 (GBaseFinalizeFunc) NULL,
881 (GClassInitFunc) gtk_rc_style_class_init,
882 NULL, /* class_finalize */
883 NULL, /* class_data */
886 (GInstanceInitFunc) gtk_rc_style_init,
889 object_type = g_type_register_static (G_TYPE_OBJECT,
898 gtk_rc_style_init (GtkRcStyle *style)
903 style->font_desc = NULL;
905 for (i = 0; i < 5; i++)
907 static const GdkColor init_color = { 0, 0, 0, 0, };
909 style->bg_pixmap_name[i] = NULL;
910 style->color_flags[i] = 0;
911 style->fg[i] = init_color;
912 style->bg[i] = init_color;
913 style->text[i] = init_color;
914 style->base[i] = init_color;
916 style->xthickness = -1;
917 style->ythickness = -1;
918 style->rc_properties = NULL;
920 style->rc_style_lists = NULL;
921 style->icon_factories = NULL;
925 gtk_rc_style_class_init (GtkRcStyleClass *klass)
927 GObjectClass *object_class = G_OBJECT_CLASS (klass);
929 parent_class = g_type_class_peek_parent (klass);
931 object_class->finalize = gtk_rc_style_finalize;
934 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
935 klass->merge = gtk_rc_style_real_merge;
936 klass->create_style = gtk_rc_style_real_create_style;
940 gtk_rc_style_finalize (GObject *object)
942 GSList *tmp_list1, *tmp_list2;
943 GtkRcStyle *rc_style;
946 rc_style = GTK_RC_STYLE (object);
949 g_free (rc_style->name);
950 if (rc_style->font_desc)
951 pango_font_description_free (rc_style->font_desc);
953 for (i = 0; i < 5; i++)
954 if (rc_style->bg_pixmap_name[i])
955 g_free (rc_style->bg_pixmap_name[i]);
957 /* Now remove all references to this rc_style from
960 tmp_list1 = rc_style->rc_style_lists;
963 GSList *rc_styles = tmp_list1->data;
964 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
965 gtk_style_unref (style);
967 /* Remove the list of styles from the other rc_styles
970 tmp_list2 = rc_styles;
973 GtkRcStyle *other_style = tmp_list2->data;
975 if (other_style != rc_style)
976 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
978 tmp_list2 = tmp_list2->next;
981 /* And from the hash table itself
983 g_hash_table_remove (realized_style_ht, rc_styles);
984 g_slist_free (rc_styles);
986 tmp_list1 = tmp_list1->next;
988 g_slist_free (rc_style->rc_style_lists);
990 if (rc_style->rc_properties)
994 for (i = 0; i < rc_style->rc_properties->len; i++)
996 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
998 g_free (node->origin);
999 g_value_unset (&node->value);
1001 g_array_free (rc_style->rc_properties, TRUE);
1002 rc_style->rc_properties = NULL;
1005 tmp_list1 = rc_style->icon_factories;
1008 g_object_unref (G_OBJECT (tmp_list1->data));
1010 tmp_list1 = tmp_list1->next;
1012 g_slist_free (rc_style->icon_factories);
1014 G_OBJECT_CLASS (parent_class)->finalize (object);
1018 gtk_rc_style_new (void)
1022 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1028 * gtk_rc_style_copy:
1029 * @orig: the style to copy
1031 * Makes a copy of the specified #GtkRcStyle. This function
1032 * will correctly copy an rc style that is a member of a class
1033 * derived from #GtkRcStyle.
1035 * Return value: the resulting #GtkRcStyle
1038 gtk_rc_style_copy (GtkRcStyle *orig)
1042 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1044 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1045 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1051 gtk_rc_style_ref (GtkRcStyle *rc_style)
1053 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1055 g_object_ref (G_OBJECT (rc_style));
1059 gtk_rc_style_unref (GtkRcStyle *rc_style)
1061 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1063 g_object_unref (G_OBJECT (rc_style));
1067 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1069 return GTK_RC_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
1073 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1074 gconstpointer bsearch_node2)
1076 const GtkRcProperty *prop1 = bsearch_node1;
1077 const GtkRcProperty *prop2 = bsearch_node2;
1079 if (prop1->type_name == prop2->type_name)
1080 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1082 return prop1->type_name < prop2->type_name ? -1 : 1;
1086 insert_rc_property (GtkRcStyle *style,
1087 GtkRcProperty *property,
1091 GtkRcProperty *new_property = NULL;
1092 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1094 key.type_name = property->type_name;
1095 key.property_name = property->property_name;
1097 if (!style->rc_properties)
1098 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1101 while (i < style->rc_properties->len)
1103 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1109 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1111 g_free (new_property->origin);
1112 g_value_unset (&new_property->value);
1114 *new_property = key;
1128 g_array_insert_val (style->rc_properties, i, key);
1129 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1132 new_property->origin = g_strdup (property->origin);
1133 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1134 g_value_copy (&property->value, &new_property->value);
1138 gtk_rc_style_real_merge (GtkRcStyle *dest,
1143 for (i = 0; i < 5; i++)
1145 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1146 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1148 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1149 src->color_flags[i] & GTK_RC_FG)
1151 dest->fg[i] = src->fg[i];
1152 dest->color_flags[i] |= GTK_RC_FG;
1154 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1155 src->color_flags[i] & GTK_RC_BG)
1157 dest->bg[i] = src->bg[i];
1158 dest->color_flags[i] |= GTK_RC_BG;
1160 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1161 src->color_flags[i] & GTK_RC_TEXT)
1163 dest->text[i] = src->text[i];
1164 dest->color_flags[i] |= GTK_RC_TEXT;
1166 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1167 src->color_flags[i] & GTK_RC_BASE)
1169 dest->base[i] = src->base[i];
1170 dest->color_flags[i] |= GTK_RC_BASE;
1174 if (dest->xthickness < 0 && src->xthickness >= 0)
1175 dest->xthickness = src->xthickness;
1176 if (dest->ythickness < 0 && src->ythickness >= 0)
1177 dest->ythickness = src->ythickness;
1179 if (!dest->font_desc && src->font_desc)
1180 dest->font_desc = pango_font_description_copy (src->font_desc);
1182 if (src->rc_properties)
1186 for (i = 0; i < src->rc_properties->len; i++)
1187 insert_rc_property (dest,
1188 &g_array_index (src->rc_properties, GtkRcProperty, i),
1194 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1196 return gtk_style_new ();
1200 gtk_rc_clear_hash_node (gpointer key,
1204 gtk_rc_style_unref (data);
1208 gtk_rc_free_rc_sets (GSList *slist)
1214 rc_set = slist->data;
1215 g_pattern_spec_free (rc_set->pspec);
1218 slist = slist->next;
1223 gtk_rc_clear_styles (GtkRcContext *context)
1225 /* Clear out all old rc_styles */
1227 if (context->rc_style_ht)
1229 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1230 g_hash_table_destroy (context->rc_style_ht);
1231 context->rc_style_ht = NULL;
1234 gtk_rc_free_rc_sets (context->rc_sets_widget);
1235 g_slist_free (context->rc_sets_widget);
1236 context->rc_sets_widget = NULL;
1238 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1239 g_slist_free (context->rc_sets_widget_class);
1240 context->rc_sets_widget_class = NULL;
1242 gtk_rc_free_rc_sets (context->rc_sets_class);
1243 g_slist_free (context->rc_sets_class);
1244 context->rc_sets_class = NULL;
1247 /* Reset all our widgets. Also, we have to invalidate cached icons in
1248 * icon sets so they get re-rendered.
1251 gtk_rc_reset_widgets (GtkRcContext *context)
1253 GList *list, *toplevels;
1255 _gtk_icon_set_invalidate_caches ();
1257 toplevels = gtk_window_list_toplevels ();
1258 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1260 for (list = toplevels; list; list = list->next)
1262 gtk_widget_reset_rc_styles (list->data);
1263 gtk_widget_unref (list->data);
1265 g_list_free (toplevels);
1269 * gtk_rc_reparse_all_for_settings:
1270 * @settings: a #GtkSettings
1271 * @force_load: load whether or not anything changed
1273 * If the modification time on any previously read file
1274 * for the given #GtkSettings has changed, discard all style information
1275 * and then reread all previously read RC files.
1277 * Return value: %TRUE if the files were reread.
1280 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1281 gboolean force_load)
1283 gboolean mtime_modified = FALSE;
1286 GtkRcContext *context;
1288 struct stat statbuf;
1290 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1292 context = gtk_rc_context_get (settings);
1296 /* Check through and see if any of the RC's have had their
1297 * mtime modified. If so, reparse everything.
1299 tmp_list = context->rc_files;
1302 rc_file = tmp_list->data;
1304 if (!lstat (rc_file->name, &statbuf) &&
1305 (statbuf.st_mtime > rc_file->mtime))
1307 mtime_modified = TRUE;
1311 tmp_list = tmp_list->next;
1315 if (force_load || mtime_modified)
1319 gtk_rc_clear_styles (context);
1320 g_object_freeze_notify (G_OBJECT (context->settings));
1322 old_files = context->rc_files;
1323 context->rc_files = NULL;
1325 gtk_rc_parse_default_files (context);
1327 tmp_list = old_files;
1330 rc_file = tmp_list->data;
1331 if (rc_file->reload)
1332 gtk_rc_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, TRUE);
1334 if (rc_file->canonical_name != rc_file->name)
1335 g_free (rc_file->canonical_name);
1336 g_free (rc_file->name);
1339 tmp_list = tmp_list->next;
1342 g_slist_free (old_files);;
1344 g_free (context->theme_name);
1345 g_free (context->key_theme_name);
1346 g_object_get (context->settings,
1347 "gtk-theme-name", &context->theme_name,
1348 "gtk-key-theme-name", &context->key_theme_name,
1351 if (context->theme_name && context->theme_name[0])
1352 gtk_rc_parse_named (context, context->theme_name, NULL);
1353 if (context->key_theme_name && context->key_theme_name[0])
1354 gtk_rc_parse_named (context, context->key_theme_name, "key");
1356 g_object_thaw_notify (G_OBJECT (context->settings));
1358 gtk_rc_reset_widgets (context);
1361 return mtime_modified;
1365 * gtk_rc_reparse_all:
1367 * If the modification time on any previously read file for the
1368 * default #GtkSettings has changed, discard all style information
1369 * and then reread all previously read RC files.
1371 * Return value: %TRUE if the files were reread.
1374 gtk_rc_reparse_all (void)
1376 return gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), FALSE);
1380 gtk_rc_styles_match (GSList *rc_styles,
1384 const gchar *path_reversed)
1391 rc_set = sets->data;
1394 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1395 rc_styles = g_slist_append (rc_styles, rc_set->rc_style);
1403 * @widget: a #GtkWidget
1405 * Finds all matching RC styles for a given widget,
1406 * composites them together, and then creates a
1407 * #GtkStyle representing the composite appearance.
1408 * (GTK+ actually keeps a cache of previously
1409 * created styles, so a new style may not be
1412 * Returns: the resulting style. No refcount is added
1413 * to the returned style, so if you want to save this
1414 * style around, you should add a reference yourself.
1417 gtk_rc_get_style (GtkWidget *widget)
1419 GtkRcStyle *widget_rc_style;
1420 GSList *rc_styles = NULL;
1421 GtkRcContext *context;
1423 static guint rc_style_key_id = 0;
1425 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1427 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1429 /* We allow the specification of a single rc style to be bound
1430 * tightly to a widget, for application modifications
1432 if (!rc_style_key_id)
1433 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1435 widget_rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
1438 if (widget_rc_style)
1439 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1441 if (context->rc_sets_widget)
1443 gchar *path, *path_reversed;
1446 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1447 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1449 g_free (path_reversed);
1452 if (context->rc_sets_widget_class)
1454 gchar *path, *path_reversed;
1457 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1458 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1460 g_free (path_reversed);
1463 if (context->rc_sets_class)
1467 type = GTK_OBJECT_TYPE (widget);
1471 gchar *path_reversed;
1474 path = gtk_type_name (type);
1475 path_length = strlen (path);
1476 path_reversed = g_strdup (path);
1477 g_strreverse (path_reversed);
1479 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1480 g_free (path_reversed);
1482 type = gtk_type_parent (type);
1487 return gtk_rc_init_style (rc_styles);
1493 * gtk_rc_get_style_by_paths:
1494 * @settings: a #GtkSettings object
1495 * @widget_path: the widget path to use when looking up the style, or %NULL
1496 * @class_path: the class path to use when looking up the style, or %NULL
1497 * @type: a type that will be used along with parent types of this type
1498 * when matching against class styles, or #G_TYPE_NONE
1500 * Creates up a #GtkStyle from styles defined in a RC file by providing
1501 * the raw components used in matching. This function may be useful
1502 * when creating pseudo-widgets that should be themed like widgets but
1503 * don't actually have corresponding GTK+ widgets. An example of this
1504 * would be items inside a GNOME canvas widget.
1506 * The action of gtk_rc_get_style() is similar to:
1507 * <informalexample><programlisting>
1508 * gtk_widget_path (widget, NULL, &path, NULL);
1509 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
1510 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
1511 * G_OBJECT_TYPE (widget));
1512 * </programlisting></informalexample>
1514 * Return value: A style created by matching with the supplied paths,
1515 * or %NULL if nothing matching was specified and the default style should
1516 * be used. The returned value is owned by GTK+ as part of an internal cache,
1517 * so you must call g_object_ref() on the returned value if you want to
1518 * keep a reference to it.
1521 gtk_rc_get_style_by_paths (GtkSettings *settings,
1522 const char *widget_path,
1523 const char *class_path,
1526 /* We duplicate the code from above to avoid slowing down the above
1527 * by generating paths when we don't need them. I don't know if
1528 * this is really worth it.
1530 GSList *rc_styles = NULL;
1531 GtkRcContext *context;
1533 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1535 context = gtk_rc_context_get (settings);
1537 if (context->rc_sets_widget)
1539 gchar *path_reversed;
1542 path_length = strlen (widget_path);
1543 path_reversed = g_strdup (widget_path);
1544 g_strreverse (path_reversed);
1546 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, widget_path, path_reversed);
1547 g_free (path_reversed);
1550 if (context->rc_sets_widget_class)
1552 gchar *path_reversed;
1555 path_length = strlen (class_path);
1556 path_reversed = g_strdup (class_path);
1557 g_strreverse (path_reversed);
1559 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, class_path, path_reversed);
1560 g_free (path_reversed);
1563 if (type != G_TYPE_NONE && context->rc_sets_class)
1568 gchar *path_reversed;
1571 path = g_type_name (type);
1572 path_length = strlen (path);
1573 path_reversed = g_strdup (path);
1574 g_strreverse (path_reversed);
1576 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1577 g_free (path_reversed);
1579 type = g_type_parent (type);
1584 return gtk_rc_init_style (rc_styles);
1590 gtk_rc_add_rc_sets (GSList *slist,
1591 GtkRcStyle *rc_style,
1592 const gchar *pattern)
1594 GtkRcStyle *new_style;
1598 new_style = gtk_rc_style_new ();
1599 *new_style = *rc_style;
1600 new_style->name = g_strdup (rc_style->name);
1601 if (rc_style->font_desc)
1602 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
1604 for (i = 0; i < 5; i++)
1605 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1607 rc_set = g_new (GtkRcSet, 1);
1608 rc_set->pspec = g_pattern_spec_new (pattern);
1609 rc_set->rc_style = rc_style;
1611 return g_slist_prepend (slist, rc_set);
1615 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
1616 const gchar *pattern)
1618 GtkRcContext *context;
1620 g_return_if_fail (rc_style != NULL);
1621 g_return_if_fail (pattern != NULL);
1623 context = gtk_rc_context_get (gtk_settings_get_default ());
1625 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern);
1629 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
1630 const gchar *pattern)
1632 GtkRcContext *context;
1634 g_return_if_fail (rc_style != NULL);
1635 g_return_if_fail (pattern != NULL);
1637 context = gtk_rc_context_get (gtk_settings_get_default ());
1639 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern);
1643 gtk_rc_add_class_style (GtkRcStyle *rc_style,
1644 const gchar *pattern)
1646 GtkRcContext *context;
1648 g_return_if_fail (rc_style != NULL);
1649 g_return_if_fail (pattern != NULL);
1651 context = gtk_rc_context_get (gtk_settings_get_default ());
1653 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern);
1657 gtk_rc_scanner_new (void)
1659 return g_scanner_new (>k_rc_scanner_config);
1663 gtk_rc_parse_any (GtkRcContext *context,
1664 const gchar *input_name,
1666 const gchar *input_string)
1672 scanner = gtk_rc_scanner_new ();
1676 g_assert (input_string == NULL);
1678 g_scanner_input_file (scanner, input_fd);
1682 g_assert (input_string != NULL);
1684 g_scanner_input_text (scanner, input_string, strlen (input_string));
1686 scanner->input_name = input_name;
1688 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1689 g_scanner_scope_add_symbol (scanner, 0, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1694 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1698 guint expected_token;
1700 expected_token = gtk_rc_parse_statement (context, scanner);
1702 if (expected_token != G_TOKEN_NONE)
1709 if (scanner->scope_id == 0)
1711 /* if we are in scope 0, we know the symbol names
1712 * that are associated with certaintoken values.
1713 * so we look them up to make the error messages
1716 if (expected_token > GTK_RC_TOKEN_INVALID &&
1717 expected_token < GTK_RC_TOKEN_LAST)
1719 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1720 if (symbols[i].token == expected_token)
1721 msg = symbols[i].name;
1723 msg = g_strconcat ("e.g. `", msg, "'", NULL);
1725 if (scanner->token > GTK_RC_TOKEN_INVALID &&
1726 scanner->token < GTK_RC_TOKEN_LAST)
1728 symbol_name = "???";
1729 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1730 if (symbols[i].token == scanner->token)
1731 symbol_name = symbols[i].name;
1734 g_scanner_unexp_token (scanner,
1747 g_scanner_destroy (scanner);
1751 gtk_rc_styles_hash (const GSList *rc_styles)
1758 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
1759 rc_styles = rc_styles->next;
1766 gtk_rc_styles_equal (const GSList *a,
1771 if (a->data != b->data)
1781 gtk_rc_style_hash (const gchar *name)
1787 result += (result << 3) + *name++;
1793 gtk_rc_style_equal (const gchar *a,
1796 return (strcmp (a, b) == 0);
1800 gtk_rc_style_find (GtkRcContext *context,
1803 if (context->rc_style_ht)
1804 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
1810 gtk_rc_style_to_style (GtkRcStyle *rc_style)
1814 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
1816 style->rc_style = rc_style;
1818 gtk_rc_style_ref (rc_style);
1820 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
1825 /* Reuses or frees rc_styles */
1827 gtk_rc_init_style (GSList *rc_styles)
1829 GtkStyle *style = NULL;
1832 g_return_val_if_fail (rc_styles != NULL, NULL);
1834 if (!realized_style_ht)
1835 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
1836 (GEqualFunc) gtk_rc_styles_equal);
1838 style = g_hash_table_lookup (realized_style_ht, rc_styles);
1842 GtkRcStyle *base_style = NULL;
1843 GtkRcStyle *proto_style;
1844 GtkRcStyleClass *proto_style_class;
1846 GType rc_style_type = GTK_TYPE_RC_STYLE;
1848 /* Find the first derived style in the list, and use that to
1849 * create the merged style. If we only have raw GtkRcStyles, use
1850 * the first style to create the merged style.
1852 base_style = rc_styles->data;
1853 tmp_styles = rc_styles;
1856 GtkRcStyle *rc_style = tmp_styles->data;
1858 if (G_OBJECT_TYPE (rc_style) != rc_style_type)
1860 base_style = rc_style;
1864 tmp_styles = tmp_styles->next;
1867 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
1868 proto_style = proto_style_class->create_rc_style (base_style);
1870 tmp_styles = rc_styles;
1873 GtkRcStyle *rc_style = tmp_styles->data;
1876 proto_style_class->merge (proto_style, rc_style);
1878 /* Point from each rc_style to the list of styles */
1879 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
1880 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
1882 factories = g_slist_copy (rc_style->icon_factories);
1888 while (iter != NULL)
1890 g_object_ref (G_OBJECT (iter->data));
1891 iter = g_slist_next (iter);
1894 proto_style->icon_factories = g_slist_concat (proto_style->icon_factories,
1899 tmp_styles = tmp_styles->next;
1902 for (i = 0; i < 5; i++)
1903 if (proto_style->bg_pixmap_name[i] &&
1904 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
1906 g_free (proto_style->bg_pixmap_name[i]);
1907 proto_style->bg_pixmap_name[i] = NULL;
1910 style = gtk_rc_style_to_style (proto_style);
1911 gtk_rc_style_unref (proto_style);
1913 g_hash_table_insert (realized_style_ht, rc_styles, style);
1916 g_slist_free (rc_styles);
1921 /*********************
1922 * Parsing functions *
1923 *********************/
1926 rc_parse_token_or_compound (GScanner *scanner,
1928 GTokenType delimiter)
1930 guint token = g_scanner_get_next_token (scanner);
1932 /* we either scan a single token (skipping comments)
1933 * or a compund statement.
1934 * compunds are enclosed in (), [] or {} braces, we read
1935 * them in via deep recursion.
1942 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
1945 g_string_append_printf (gstring, " %f", scanner->value.v_float);
1947 case G_TOKEN_STRING:
1948 string = g_strescape (scanner->value.v_string, NULL);
1949 g_string_append (gstring, " \"");
1950 g_string_append (gstring, string);
1951 g_string_append_c (gstring, '"');
1954 case G_TOKEN_IDENTIFIER:
1955 g_string_append_c (gstring, ' ');
1956 g_string_append (gstring, scanner->value.v_identifier);
1958 case G_TOKEN_COMMENT_SINGLE:
1959 case G_TOKEN_COMMENT_MULTI:
1960 return rc_parse_token_or_compound (scanner, gstring, delimiter);
1961 case G_TOKEN_LEFT_PAREN:
1962 g_string_append_c (gstring, ' ');
1963 g_string_append_c (gstring, token);
1964 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_PAREN);
1965 if (token != G_TOKEN_NONE)
1968 case G_TOKEN_LEFT_CURLY:
1969 g_string_append_c (gstring, ' ');
1970 g_string_append_c (gstring, token);
1971 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_CURLY);
1972 if (token != G_TOKEN_NONE)
1975 case G_TOKEN_LEFT_BRACE:
1976 g_string_append_c (gstring, ' ');
1977 g_string_append_c (gstring, token);
1978 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_BRACE);
1979 if (token != G_TOKEN_NONE)
1983 if (token >= 256 || token < 1)
1984 return delimiter ? delimiter : G_TOKEN_STRING;
1985 g_string_append_c (gstring, ' ');
1986 g_string_append_c (gstring, token);
1987 if (token == delimiter)
1988 return G_TOKEN_NONE;
1992 return G_TOKEN_NONE;
1994 return rc_parse_token_or_compound (scanner, gstring, delimiter);
1998 gtk_rc_parse_assignment (GScanner *scanner,
1999 GtkRcProperty *prop)
2001 gboolean scan_identifier = scanner->config->scan_identifier;
2002 gboolean scan_symbols = scanner->config->scan_symbols;
2003 gboolean identifier_2_string = scanner->config->identifier_2_string;
2004 gboolean char_2_token = scanner->config->char_2_token;
2005 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2006 gboolean numbers_2_int = scanner->config->numbers_2_int;
2007 gboolean negate = FALSE;
2010 /* check that this is an assignment */
2011 if (g_scanner_get_next_token (scanner) != '=')
2014 /* adjust scanner mode */
2015 scanner->config->scan_identifier = TRUE;
2016 scanner->config->scan_symbols = FALSE;
2017 scanner->config->identifier_2_string = FALSE;
2018 scanner->config->char_2_token = TRUE;
2019 scanner->config->scan_identifier_NULL = FALSE;
2020 scanner->config->numbers_2_int = TRUE;
2022 /* record location */
2023 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2025 /* parse optional sign */
2026 if (g_scanner_peek_next_token (scanner) == '-')
2028 g_scanner_get_next_token (scanner); /* eat sign */
2032 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an unparsed compund */
2033 token = g_scanner_peek_next_token (scanner);
2037 g_scanner_get_next_token (scanner);
2038 g_value_init (&prop->value, G_TYPE_LONG);
2039 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2040 token = G_TOKEN_NONE;
2043 g_scanner_get_next_token (scanner);
2044 g_value_init (&prop->value, G_TYPE_DOUBLE);
2045 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2046 token = G_TOKEN_NONE;
2048 case G_TOKEN_STRING:
2049 g_scanner_get_next_token (scanner);
2051 token = G_TOKEN_INT;
2054 g_value_init (&prop->value, G_TYPE_STRING);
2055 g_value_set_string (&prop->value, scanner->value.v_string);
2056 token = G_TOKEN_NONE;
2059 case G_TOKEN_IDENTIFIER:
2060 case G_TOKEN_LEFT_PAREN:
2061 case G_TOKEN_LEFT_CURLY:
2062 case G_TOKEN_LEFT_BRACE:
2065 GString *gstring = g_string_new ("");
2067 token = rc_parse_token_or_compound (scanner, gstring, 0);
2068 if (token == G_TOKEN_NONE)
2070 g_string_append_c (gstring, ' ');
2071 g_value_init (&prop->value, G_TYPE_GSTRING);
2072 g_value_set_static_boxed (&prop->value, gstring);
2075 g_string_free (gstring, TRUE);
2080 g_scanner_get_next_token (scanner);
2081 token = G_TOKEN_INT;
2085 /* restore scanner mode */
2086 scanner->config->scan_identifier = scan_identifier;
2087 scanner->config->scan_symbols = scan_symbols;
2088 scanner->config->identifier_2_string = identifier_2_string;
2089 scanner->config->char_2_token = char_2_token;
2090 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2091 scanner->config->numbers_2_int = numbers_2_int;
2097 is_c_identifier (const gchar *string)
2100 gboolean is_varname;
2102 is_varname = strchr (G_CSET_a_2_z G_CSET_A_2_Z "_", string[0]) != NULL;
2103 for (p = string + 1; *p && is_varname; p++)
2104 is_varname &= strchr (G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_-", *p) != NULL;
2110 gtk_rc_parse_statement (GtkRcContext *context,
2115 token = g_scanner_peek_next_token (scanner);
2118 case GTK_RC_TOKEN_INCLUDE:
2119 token = g_scanner_get_next_token (scanner);
2120 if (token != GTK_RC_TOKEN_INCLUDE)
2121 return GTK_RC_TOKEN_INCLUDE;
2122 token = g_scanner_get_next_token (scanner);
2123 if (token != G_TOKEN_STRING)
2124 return G_TOKEN_STRING;
2125 gtk_rc_parse_file (context, scanner->value.v_string, context->default_priority, FALSE);
2126 return G_TOKEN_NONE;
2128 case GTK_RC_TOKEN_STYLE:
2129 return gtk_rc_parse_style (context, scanner);
2131 case GTK_RC_TOKEN_BINDING:
2132 return gtk_binding_parse_binding (scanner);
2134 case GTK_RC_TOKEN_PIXMAP_PATH:
2135 return gtk_rc_parse_pixmap_path (context, scanner);
2137 case GTK_RC_TOKEN_WIDGET:
2138 return gtk_rc_parse_path_pattern (context, scanner);
2140 case GTK_RC_TOKEN_WIDGET_CLASS:
2141 return gtk_rc_parse_path_pattern (context, scanner);
2143 case GTK_RC_TOKEN_CLASS:
2144 return gtk_rc_parse_path_pattern (context, scanner);
2146 case GTK_RC_TOKEN_MODULE_PATH:
2147 return gtk_rc_parse_module_path (scanner);
2149 case GTK_RC_TOKEN_IM_MODULE_PATH:
2150 return gtk_rc_parse_im_module_path (scanner);
2152 case GTK_RC_TOKEN_IM_MODULE_FILE:
2153 return gtk_rc_parse_im_module_file (scanner);
2155 case G_TOKEN_IDENTIFIER:
2156 if (is_c_identifier (scanner->next_value.v_identifier))
2158 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2161 g_scanner_get_next_token (scanner); /* eat identifier */
2162 name = g_strdup (scanner->value.v_identifier);
2164 token = gtk_rc_parse_assignment (scanner, &prop);
2165 if (token == G_TOKEN_NONE)
2167 GtkSettingsValue svalue;
2169 svalue.origin = prop.origin;
2170 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2171 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2172 gtk_settings_set_property_value (context->settings,
2176 g_free (prop.origin);
2177 if (G_VALUE_TYPE (&prop.value))
2178 g_value_unset (&prop.value);
2185 g_scanner_get_next_token (scanner);
2186 return G_TOKEN_IDENTIFIER;
2189 g_scanner_get_next_token (scanner);
2190 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2195 gtk_rc_parse_style (GtkRcContext *context,
2198 GtkRcStyle *rc_style;
2199 GtkRcStyle *parent_style;
2203 GtkIconFactory *our_factory = NULL;
2205 token = g_scanner_get_next_token (scanner);
2206 if (token != GTK_RC_TOKEN_STYLE)
2207 return GTK_RC_TOKEN_STYLE;
2209 token = g_scanner_get_next_token (scanner);
2210 if (token != G_TOKEN_STRING)
2211 return G_TOKEN_STRING;
2214 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2216 /* If there's a list, its first member is always the factory belonging
2219 if (rc_style && rc_style->icon_factories)
2220 our_factory = rc_style->icon_factories->data;
2225 rc_style = gtk_rc_style_new ();
2226 rc_style->name = g_strdup (scanner->value.v_string);
2228 for (i = 0; i < 5; i++)
2229 rc_style->bg_pixmap_name[i] = NULL;
2231 for (i = 0; i < 5; i++)
2232 rc_style->color_flags[i] = 0;
2235 token = g_scanner_peek_next_token (scanner);
2236 if (token == G_TOKEN_EQUAL_SIGN)
2238 token = g_scanner_get_next_token (scanner);
2240 token = g_scanner_get_next_token (scanner);
2241 if (token != G_TOKEN_STRING)
2246 return G_TOKEN_STRING;
2249 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2254 for (i = 0; i < 5; i++)
2256 rc_style->color_flags[i] = parent_style->color_flags[i];
2257 rc_style->fg[i] = parent_style->fg[i];
2258 rc_style->bg[i] = parent_style->bg[i];
2259 rc_style->text[i] = parent_style->text[i];
2260 rc_style->base[i] = parent_style->base[i];
2263 rc_style->xthickness = parent_style->xthickness;
2264 rc_style->ythickness = parent_style->ythickness;
2266 if (parent_style->font_desc)
2268 if (rc_style->font_desc)
2269 pango_font_description_free (rc_style->font_desc);
2270 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
2273 if (parent_style->rc_properties)
2277 for (i = 0; i < parent_style->rc_properties->len; i++)
2278 insert_rc_property (rc_style,
2279 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
2283 for (i = 0; i < 5; i++)
2285 if (rc_style->bg_pixmap_name[i])
2286 g_free (rc_style->bg_pixmap_name[i]);
2287 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
2290 /* Append parent's factories, adding a ref to them */
2291 if (parent_style->icon_factories != NULL)
2293 /* Add a factory for ourselves if we have none,
2294 * in case we end up defining more stock icons.
2295 * I see no real way around this; we need to maintain
2296 * the invariant that the first factory in the list
2297 * is always our_factory, the one belonging to us,
2298 * and if we put parent factories in the list we can't
2299 * do that if the style is reopened.
2301 if (our_factory == NULL)
2303 our_factory = gtk_icon_factory_new ();
2304 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2308 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories,
2309 g_slist_copy (parent_style->icon_factories));
2311 factories = parent_style->icon_factories;
2312 while (factories != NULL)
2314 g_object_ref (G_OBJECT (factories->data));
2315 factories = factories->next;
2321 token = g_scanner_get_next_token (scanner);
2322 if (token != G_TOKEN_LEFT_CURLY)
2327 return G_TOKEN_LEFT_CURLY;
2330 token = g_scanner_peek_next_token (scanner);
2331 while (token != G_TOKEN_RIGHT_CURLY)
2335 case GTK_RC_TOKEN_BG:
2336 token = gtk_rc_parse_bg (scanner, rc_style);
2338 case GTK_RC_TOKEN_FG:
2339 token = gtk_rc_parse_fg (scanner, rc_style);
2341 case GTK_RC_TOKEN_TEXT:
2342 token = gtk_rc_parse_text (scanner, rc_style);
2344 case GTK_RC_TOKEN_BASE:
2345 token = gtk_rc_parse_base (scanner, rc_style);
2347 case GTK_RC_TOKEN_XTHICKNESS:
2348 token = gtk_rc_parse_xthickness (scanner, rc_style);
2350 case GTK_RC_TOKEN_YTHICKNESS:
2351 token = gtk_rc_parse_ythickness (scanner, rc_style);
2353 case GTK_RC_TOKEN_BG_PIXMAP:
2354 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
2356 case GTK_RC_TOKEN_FONT:
2357 token = gtk_rc_parse_font (scanner, rc_style);
2359 case GTK_RC_TOKEN_FONTSET:
2360 token = gtk_rc_parse_fontset (scanner, rc_style);
2362 case GTK_RC_TOKEN_FONT_NAME:
2363 token = gtk_rc_parse_font_name (scanner, rc_style);
2365 case GTK_RC_TOKEN_ENGINE:
2366 token = gtk_rc_parse_engine (context, scanner, &rc_style);
2368 case GTK_RC_TOKEN_STOCK:
2369 if (our_factory == NULL)
2371 our_factory = gtk_icon_factory_new ();
2372 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2375 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
2377 case G_TOKEN_IDENTIFIER:
2378 if (is_c_identifier (scanner->next_value.v_identifier) &&
2379 scanner->next_value.v_identifier[0] >= 'A' &&
2380 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
2382 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2384 g_scanner_get_next_token (scanner); /* eat type name */
2385 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
2386 if (g_scanner_get_next_token (scanner) != ':' ||
2387 g_scanner_get_next_token (scanner) != ':')
2392 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
2393 !is_c_identifier (scanner->value.v_identifier))
2395 token = G_TOKEN_IDENTIFIER;
2399 /* it's important that we do the same canonification as GParamSpecPool here */
2400 g_strcanon (scanner->value.v_identifier, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2401 prop.property_name = g_quark_from_string (scanner->value.v_identifier);
2403 token = gtk_rc_parse_assignment (scanner, &prop);
2404 if (token == G_TOKEN_NONE)
2406 g_return_val_if_fail (prop.origin != NULL && G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
2407 insert_rc_property (rc_style, &prop, TRUE);
2410 g_free (prop.origin);
2411 if (G_VALUE_TYPE (&prop.value))
2412 g_value_unset (&prop.value);
2416 g_scanner_get_next_token (scanner);
2417 token = G_TOKEN_IDENTIFIER;
2421 g_scanner_get_next_token (scanner);
2422 token = G_TOKEN_RIGHT_CURLY;
2426 if (token != G_TOKEN_NONE)
2429 gtk_rc_style_unref (rc_style);
2433 token = g_scanner_peek_next_token (scanner);
2434 } /* while (token != G_TOKEN_RIGHT_CURLY) */
2436 token = g_scanner_get_next_token (scanner);
2437 if (token != G_TOKEN_RIGHT_CURLY)
2441 if (rc_style->font_desc)
2442 pango_font_description_free (rc_style->font_desc);
2444 for (i = 0; i < 5; i++)
2445 if (rc_style->bg_pixmap_name[i])
2446 g_free (rc_style->bg_pixmap_name[i]);
2450 return G_TOKEN_RIGHT_CURLY;
2455 if (!context->rc_style_ht)
2456 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
2457 (GEqualFunc) gtk_rc_style_equal);
2459 g_hash_table_insert (context->rc_style_ht, rc_style->name, rc_style);
2462 return G_TOKEN_NONE;
2465 const GtkRcProperty*
2466 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
2468 GQuark property_name)
2470 GtkRcProperty *node = NULL;
2472 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
2474 if (rc_style->rc_properties)
2478 key.type_name = type_name;
2479 key.property_name = property_name;
2481 node = bsearch (&key,
2482 rc_style->rc_properties->data, rc_style->rc_properties->len,
2483 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
2490 gtk_rc_parse_bg (GScanner *scanner,
2496 token = g_scanner_get_next_token (scanner);
2497 if (token != GTK_RC_TOKEN_BG)
2498 return GTK_RC_TOKEN_BG;
2500 token = gtk_rc_parse_state (scanner, &state);
2501 if (token != G_TOKEN_NONE)
2504 token = g_scanner_get_next_token (scanner);
2505 if (token != G_TOKEN_EQUAL_SIGN)
2506 return G_TOKEN_EQUAL_SIGN;
2508 style->color_flags[state] |= GTK_RC_BG;
2509 return gtk_rc_parse_color (scanner, &style->bg[state]);
2513 gtk_rc_parse_fg (GScanner *scanner,
2519 token = g_scanner_get_next_token (scanner);
2520 if (token != GTK_RC_TOKEN_FG)
2521 return GTK_RC_TOKEN_FG;
2523 token = gtk_rc_parse_state (scanner, &state);
2524 if (token != G_TOKEN_NONE)
2527 token = g_scanner_get_next_token (scanner);
2528 if (token != G_TOKEN_EQUAL_SIGN)
2529 return G_TOKEN_EQUAL_SIGN;
2531 style->color_flags[state] |= GTK_RC_FG;
2532 return gtk_rc_parse_color (scanner, &style->fg[state]);
2536 gtk_rc_parse_text (GScanner *scanner,
2542 token = g_scanner_get_next_token (scanner);
2543 if (token != GTK_RC_TOKEN_TEXT)
2544 return GTK_RC_TOKEN_TEXT;
2546 token = gtk_rc_parse_state (scanner, &state);
2547 if (token != G_TOKEN_NONE)
2550 token = g_scanner_get_next_token (scanner);
2551 if (token != G_TOKEN_EQUAL_SIGN)
2552 return G_TOKEN_EQUAL_SIGN;
2554 style->color_flags[state] |= GTK_RC_TEXT;
2555 return gtk_rc_parse_color (scanner, &style->text[state]);
2559 gtk_rc_parse_base (GScanner *scanner,
2565 token = g_scanner_get_next_token (scanner);
2566 if (token != GTK_RC_TOKEN_BASE)
2567 return GTK_RC_TOKEN_BASE;
2569 token = gtk_rc_parse_state (scanner, &state);
2570 if (token != G_TOKEN_NONE)
2573 token = g_scanner_get_next_token (scanner);
2574 if (token != G_TOKEN_EQUAL_SIGN)
2575 return G_TOKEN_EQUAL_SIGN;
2577 style->color_flags[state] |= GTK_RC_BASE;
2578 return gtk_rc_parse_color (scanner, &style->base[state]);
2582 gtk_rc_parse_xthickness (GScanner *scanner,
2585 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
2586 return GTK_RC_TOKEN_XTHICKNESS;
2588 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2589 return G_TOKEN_EQUAL_SIGN;
2591 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2594 style->xthickness = scanner->value.v_int;
2596 return G_TOKEN_NONE;
2600 gtk_rc_parse_ythickness (GScanner *scanner,
2603 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
2604 return GTK_RC_TOKEN_YTHICKNESS;
2606 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2607 return G_TOKEN_EQUAL_SIGN;
2609 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2612 style->ythickness = scanner->value.v_int;
2614 return G_TOKEN_NONE;
2618 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
2620 GtkRcStyle *rc_style)
2626 token = g_scanner_get_next_token (scanner);
2627 if (token != GTK_RC_TOKEN_BG_PIXMAP)
2628 return GTK_RC_TOKEN_BG_PIXMAP;
2630 token = gtk_rc_parse_state (scanner, &state);
2631 if (token != G_TOKEN_NONE)
2634 token = g_scanner_get_next_token (scanner);
2635 if (token != G_TOKEN_EQUAL_SIGN)
2636 return G_TOKEN_EQUAL_SIGN;
2638 token = g_scanner_get_next_token (scanner);
2639 if (token != G_TOKEN_STRING)
2640 return G_TOKEN_STRING;
2642 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
2643 (strcmp (scanner->value.v_string, "<none>") == 0))
2644 pixmap_file = g_strdup (scanner->value.v_string);
2646 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
2647 scanner, scanner->value.v_string);
2651 if (rc_style->bg_pixmap_name[state])
2652 g_free (rc_style->bg_pixmap_name[state]);
2653 rc_style->bg_pixmap_name[state] = pixmap_file;
2656 return G_TOKEN_NONE;
2660 gtk_rc_check_pixmap_dir (const gchar *dir, const gchar *pixmap_file)
2665 buf = g_build_filename (dir, pixmap_file, NULL);
2667 fd = open (buf, O_RDONLY);
2680 * gtk_rc_find_pixmap_in_path:
2681 * @settings: a #GtkSettinsg
2682 * @scanner: Scanner used to get line number information for the
2683 * warning message, or %NULL
2684 * @pixmap_file: name of the pixmap file to locate.
2686 * Looks up a file in pixmap path for the specified #GtkSettings.
2687 * If the file is not found, it outputs a warning message using
2688 * g_warning() and returns %NULL.
2690 * Return value: the filename.
2693 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
2695 const gchar *pixmap_file)
2701 GtkRcContext *context = gtk_rc_context_get (settings);
2703 for (i = 0; (i < GTK_RC_MAX_PIXMAP_PATHS) && (context->pixmap_path[i] != NULL); i++)
2705 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
2710 tmp_list = rc_dir_stack;
2713 filename = gtk_rc_check_pixmap_dir (tmp_list->data, pixmap_file);
2717 tmp_list = tmp_list->next;
2721 g_warning (_("Unable to locate image file in pixmap_path: \"%s\" line %d"),
2722 pixmap_file, scanner->line);
2724 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
2731 gtk_rc_find_module_in_path (const gchar *module_file)
2737 for (i = 0; (i < GTK_RC_MAX_MODULE_PATHS) && (module_path[i] != NULL); i++)
2739 buf = g_build_filename (module_path[i], module_file, NULL);
2741 fd = open (buf, O_RDONLY);
2755 gtk_rc_parse_font (GScanner *scanner,
2756 GtkRcStyle *rc_style)
2760 token = g_scanner_get_next_token (scanner);
2761 if (token != GTK_RC_TOKEN_FONT)
2762 return GTK_RC_TOKEN_FONT;
2764 token = g_scanner_get_next_token (scanner);
2765 if (token != G_TOKEN_EQUAL_SIGN)
2766 return G_TOKEN_EQUAL_SIGN;
2768 token = g_scanner_get_next_token (scanner);
2769 if (token != G_TOKEN_STRING)
2770 return G_TOKEN_STRING;
2772 /* Ignore, do nothing */
2774 return G_TOKEN_NONE;
2778 gtk_rc_parse_fontset (GScanner *scanner,
2779 GtkRcStyle *rc_style)
2783 token = g_scanner_get_next_token (scanner);
2784 if (token != GTK_RC_TOKEN_FONTSET)
2785 return GTK_RC_TOKEN_FONTSET;
2787 token = g_scanner_get_next_token (scanner);
2788 if (token != G_TOKEN_EQUAL_SIGN)
2789 return G_TOKEN_EQUAL_SIGN;
2791 token = g_scanner_get_next_token (scanner);
2792 if (token != G_TOKEN_STRING)
2793 return G_TOKEN_STRING;
2795 /* Do nothing - silently ignore */
2797 return G_TOKEN_NONE;
2801 gtk_rc_parse_font_name (GScanner *scanner,
2802 GtkRcStyle *rc_style)
2806 token = g_scanner_get_next_token (scanner);
2807 if (token != GTK_RC_TOKEN_FONT_NAME)
2808 return GTK_RC_TOKEN_FONT;
2810 token = g_scanner_get_next_token (scanner);
2811 if (token != G_TOKEN_EQUAL_SIGN)
2812 return G_TOKEN_EQUAL_SIGN;
2814 token = g_scanner_get_next_token (scanner);
2815 if (token != G_TOKEN_STRING)
2816 return G_TOKEN_STRING;
2818 rc_style->font_desc = pango_font_description_from_string (scanner->value.v_string);
2820 return G_TOKEN_NONE;
2824 gtk_rc_parse_engine (GtkRcContext *context,
2826 GtkRcStyle **rc_style)
2829 GtkThemeEngine *engine;
2830 guint result = G_TOKEN_NONE;
2831 GtkRcStyle *new_style = NULL;
2832 gboolean parsed_curlies = FALSE;
2834 token = g_scanner_get_next_token (scanner);
2835 if (token != GTK_RC_TOKEN_ENGINE)
2836 return GTK_RC_TOKEN_ENGINE;
2838 token = g_scanner_get_next_token (scanner);
2839 if (token != G_TOKEN_STRING)
2840 return G_TOKEN_STRING;
2842 engine = gtk_theme_engine_get (scanner->value.v_string);
2844 token = g_scanner_get_next_token (scanner);
2845 if (token != G_TOKEN_LEFT_CURLY)
2846 return G_TOKEN_LEFT_CURLY;
2850 GtkRcStyleClass *new_class;
2852 new_style = gtk_theme_engine_create_rc_style (engine);
2853 g_type_module_unuse (G_TYPE_MODULE (engine));
2855 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
2857 new_class->merge (new_style, *rc_style);
2858 if ((*rc_style)->name)
2859 new_style->name = g_strdup ((*rc_style)->name);
2861 if (new_class->parse)
2863 parsed_curlies = TRUE;
2864 result = new_class->parse (new_style, context->settings, scanner);
2866 if (result != G_TOKEN_NONE)
2868 g_object_unref (G_OBJECT (new_style));
2874 if (!parsed_curlies)
2876 /* Skip over remainder, looking for nested {}'s
2880 result = G_TOKEN_RIGHT_CURLY;
2881 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
2883 if (token == G_TOKEN_LEFT_CURLY)
2885 else if (token == G_TOKEN_RIGHT_CURLY)
2890 result = G_TOKEN_NONE;
2898 g_object_unref (G_OBJECT (*rc_style));
2899 *rc_style = new_style;
2906 gtk_rc_parse_state (GScanner *scanner,
2907 GtkStateType *state)
2912 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2913 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
2915 /* we don't know where we got called from, so we reset the scope here.
2916 * if we bail out due to errors, we *don't* reset the scope, so the
2917 * error messaging code can make sense of our tokens.
2919 old_scope = g_scanner_set_scope (scanner, 0);
2921 token = g_scanner_get_next_token (scanner);
2922 if (token != G_TOKEN_LEFT_BRACE)
2923 return G_TOKEN_LEFT_BRACE;
2925 token = g_scanner_get_next_token (scanner);
2928 case GTK_RC_TOKEN_ACTIVE:
2929 *state = GTK_STATE_ACTIVE;
2931 case GTK_RC_TOKEN_INSENSITIVE:
2932 *state = GTK_STATE_INSENSITIVE;
2934 case GTK_RC_TOKEN_NORMAL:
2935 *state = GTK_STATE_NORMAL;
2937 case GTK_RC_TOKEN_PRELIGHT:
2938 *state = GTK_STATE_PRELIGHT;
2940 case GTK_RC_TOKEN_SELECTED:
2941 *state = GTK_STATE_SELECTED;
2944 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
2947 token = g_scanner_get_next_token (scanner);
2948 if (token != G_TOKEN_RIGHT_BRACE)
2949 return G_TOKEN_RIGHT_BRACE;
2951 g_scanner_set_scope (scanner, old_scope);
2953 return G_TOKEN_NONE;
2957 gtk_rc_parse_priority (GScanner *scanner,
2958 GtkPathPriorityType *priority)
2963 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2964 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
2966 /* we don't know where we got called from, so we reset the scope here.
2967 * if we bail out due to errors, we *don't* reset the scope, so the
2968 * error messaging code can make sense of our tokens.
2970 old_scope = g_scanner_set_scope (scanner, 0);
2972 token = g_scanner_get_next_token (scanner);
2976 token = g_scanner_get_next_token (scanner);
2979 case GTK_RC_TOKEN_LOWEST:
2980 *priority = GTK_PATH_PRIO_LOWEST;
2982 case GTK_RC_TOKEN_GTK:
2983 *priority = GTK_PATH_PRIO_GTK;
2985 case GTK_RC_TOKEN_APPLICATION:
2986 *priority = GTK_PATH_PRIO_APPLICATION;
2988 case GTK_RC_TOKEN_THEME:
2989 *priority = GTK_PATH_PRIO_THEME;
2991 case GTK_RC_TOKEN_RC:
2992 *priority = GTK_PATH_PRIO_RC;
2994 case GTK_RC_TOKEN_HIGHEST:
2995 *priority = GTK_PATH_PRIO_HIGHEST;
2998 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3001 g_scanner_set_scope (scanner, old_scope);
3003 return G_TOKEN_NONE;
3007 gtk_rc_parse_color (GScanner *scanner,
3012 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3014 /* we don't need to set our own scope here, because
3015 * we don't need own symbols
3018 token = g_scanner_get_next_token (scanner);
3023 case G_TOKEN_LEFT_CURLY:
3024 token = g_scanner_get_next_token (scanner);
3025 if (token == G_TOKEN_INT)
3026 token_int = scanner->value.v_int;
3027 else if (token == G_TOKEN_FLOAT)
3028 token_int = scanner->value.v_float * 65535.0;
3030 return G_TOKEN_FLOAT;
3031 color->red = CLAMP (token_int, 0, 65535);
3033 token = g_scanner_get_next_token (scanner);
3034 if (token != G_TOKEN_COMMA)
3035 return G_TOKEN_COMMA;
3037 token = g_scanner_get_next_token (scanner);
3038 if (token == G_TOKEN_INT)
3039 token_int = scanner->value.v_int;
3040 else if (token == G_TOKEN_FLOAT)
3041 token_int = scanner->value.v_float * 65535.0;
3043 return G_TOKEN_FLOAT;
3044 color->green = CLAMP (token_int, 0, 65535);
3046 token = g_scanner_get_next_token (scanner);
3047 if (token != G_TOKEN_COMMA)
3048 return G_TOKEN_COMMA;
3050 token = g_scanner_get_next_token (scanner);
3051 if (token == G_TOKEN_INT)
3052 token_int = scanner->value.v_int;
3053 else if (token == G_TOKEN_FLOAT)
3054 token_int = scanner->value.v_float * 65535.0;
3056 return G_TOKEN_FLOAT;
3057 color->blue = CLAMP (token_int, 0, 65535);
3059 token = g_scanner_get_next_token (scanner);
3060 if (token != G_TOKEN_RIGHT_CURLY)
3061 return G_TOKEN_RIGHT_CURLY;
3062 return G_TOKEN_NONE;
3064 case G_TOKEN_STRING:
3065 if (!gdk_color_parse (scanner->value.v_string, color))
3067 g_scanner_warn (scanner, "Invalid color constant '%s'",
3068 scanner->value.v_string);
3069 return G_TOKEN_STRING;
3072 return G_TOKEN_NONE;
3075 return G_TOKEN_STRING;
3080 gtk_rc_parse_pixmap_path (GtkRcContext *context,
3085 token = g_scanner_get_next_token (scanner);
3086 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
3087 return GTK_RC_TOKEN_PIXMAP_PATH;
3089 token = g_scanner_get_next_token (scanner);
3090 if (token != G_TOKEN_STRING)
3091 return G_TOKEN_STRING;
3093 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
3095 return G_TOKEN_NONE;
3099 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
3101 const gchar *pix_path)
3104 gint start_offset = 0;
3108 /* free the old one, or just add to the old one ? */
3109 for (path_num = 0; context->pixmap_path[path_num]; path_num++)
3111 g_free (context->pixmap_path[path_num]);
3112 context->pixmap_path[path_num] = NULL;
3117 path_len = strlen (pix_path);
3119 for (end_offset = 0; end_offset <= path_len; end_offset++)
3121 if ((pix_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3122 (end_offset == path_len))
3124 gchar *path_element = g_strndup (pix_path + start_offset, end_offset - start_offset);
3125 if (g_path_is_absolute (path_element))
3127 context->pixmap_path[path_num] = path_element;
3129 context->pixmap_path[path_num] = NULL;
3133 g_warning (_("Pixmap path element: \"%s\" must be absolute, %s, line %d"),
3134 path_element, scanner->input_name, scanner->line);
3135 g_free (path_element);
3138 start_offset = end_offset + 1;
3144 gtk_rc_parse_module_path (GScanner *scanner)
3148 token = g_scanner_get_next_token (scanner);
3149 if (token != GTK_RC_TOKEN_MODULE_PATH)
3150 return GTK_RC_TOKEN_MODULE_PATH;
3152 token = g_scanner_get_next_token (scanner);
3153 if (token != G_TOKEN_STRING)
3154 return G_TOKEN_STRING;
3156 gtk_rc_parse_module_path_string (scanner->value.v_string);
3158 return G_TOKEN_NONE;
3162 gtk_rc_parse_im_module_path (GScanner *scanner)
3166 token = g_scanner_get_next_token (scanner);
3167 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3168 return GTK_RC_TOKEN_IM_MODULE_FILE;
3170 token = g_scanner_get_next_token (scanner);
3171 if (token != G_TOKEN_STRING)
3172 return G_TOKEN_STRING;
3175 g_free (im_module_path);
3177 im_module_path = g_strdup (scanner->value.v_string);
3179 return G_TOKEN_NONE;
3183 gtk_rc_parse_im_module_file (GScanner *scanner)
3187 token = g_scanner_get_next_token (scanner);
3188 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3189 return GTK_RC_TOKEN_IM_MODULE_FILE;
3191 token = g_scanner_get_next_token (scanner);
3192 if (token != G_TOKEN_STRING)
3193 return G_TOKEN_STRING;
3196 g_free (im_module_file);
3198 im_module_file = g_strdup (scanner->value.v_string);
3200 return G_TOKEN_NONE;
3204 gtk_rc_parse_module_path_string (const gchar *mod_path)
3207 gint start_offset = 0;
3211 /* free the old one, or just add to the old one ? */
3212 for (path_num=0; module_path[path_num]; path_num++)
3214 g_free (module_path[path_num]);
3215 module_path[path_num] = NULL;
3220 path_len = strlen (mod_path);
3222 for (end_offset = 0; end_offset <= path_len; end_offset++)
3224 if ((mod_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3225 (end_offset == path_len))
3227 module_path[path_num] = g_strndup (mod_path + start_offset, end_offset - start_offset);
3229 module_path[path_num] = NULL;
3230 start_offset = end_offset + 1;
3233 gtk_rc_append_default_module_path();
3237 rc_set_compare (gconstpointer a, gconstpointer b)
3239 const GtkRcSet *set_a = a;
3240 const GtkRcSet *set_b = b;
3242 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
3246 insert_rc_set (GSList *list, GtkRcSet *set)
3248 return g_slist_insert_sorted (list, set, rc_set_compare);
3252 gtk_rc_parse_path_pattern (GtkRcContext *context,
3256 GtkPathType path_type;
3258 gboolean is_binding;
3259 GtkPathPriorityType priority = context->default_priority;
3261 token = g_scanner_get_next_token (scanner);
3264 case GTK_RC_TOKEN_WIDGET:
3265 path_type = GTK_PATH_WIDGET;
3267 case GTK_RC_TOKEN_WIDGET_CLASS:
3268 path_type = GTK_PATH_WIDGET_CLASS;
3270 case GTK_RC_TOKEN_CLASS:
3271 path_type = GTK_PATH_CLASS;
3274 return GTK_RC_TOKEN_WIDGET_CLASS;
3277 token = g_scanner_get_next_token (scanner);
3278 if (token != G_TOKEN_STRING)
3279 return G_TOKEN_STRING;
3281 pattern = g_strdup (scanner->value.v_string);
3283 token = g_scanner_get_next_token (scanner);
3284 if (token == GTK_RC_TOKEN_STYLE)
3286 else if (token == GTK_RC_TOKEN_BINDING)
3291 return GTK_RC_TOKEN_STYLE;
3294 if (g_scanner_peek_next_token (scanner) == ':')
3296 token = gtk_rc_parse_priority (scanner, &priority);
3297 if (token != G_TOKEN_NONE)
3304 token = g_scanner_get_next_token (scanner);
3305 if (token != G_TOKEN_STRING)
3308 return G_TOKEN_STRING;
3313 GtkBindingSet *binding;
3315 binding = gtk_binding_set_find (scanner->value.v_string);
3319 return G_TOKEN_STRING;
3321 gtk_binding_set_add_path (binding, path_type, pattern, priority);
3325 GtkRcStyle *rc_style;
3328 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3333 return G_TOKEN_STRING;
3336 rc_set = g_new (GtkRcSet, 1);
3337 rc_set->pspec = g_pattern_spec_new (pattern);
3338 rc_set->rc_style = rc_style;
3339 rc_set->priority = priority;
3341 if (path_type == GTK_PATH_WIDGET)
3342 context->rc_sets_widget = insert_rc_set (context->rc_sets_widget, rc_set);
3343 else if (path_type == GTK_PATH_WIDGET_CLASS)
3344 context->rc_sets_widget_class = insert_rc_set (context->rc_sets_widget_class, rc_set);
3346 context->rc_sets_class = insert_rc_set (context->rc_sets_class, rc_set);
3350 return G_TOKEN_NONE;
3354 gtk_rc_parse_stock_id (GScanner *scanner,
3359 token = g_scanner_get_next_token (scanner);
3360 if (token != G_TOKEN_LEFT_BRACE)
3361 return G_TOKEN_LEFT_BRACE;
3363 token = g_scanner_get_next_token (scanner);
3365 if (token != G_TOKEN_STRING)
3366 return G_TOKEN_STRING;
3368 *stock_id = g_strdup (scanner->value.v_string);
3370 token = g_scanner_get_next_token (scanner);
3371 if (token != G_TOKEN_RIGHT_BRACE)
3374 return G_TOKEN_RIGHT_BRACE;
3377 return G_TOKEN_NONE;
3381 gtk_rc_parse_icon_source (GtkRcContext *context,
3383 GtkIconSet *icon_set)
3386 GtkIconSource *source;
3387 gchar *full_filename;
3389 token = g_scanner_get_next_token (scanner);
3390 if (token != G_TOKEN_LEFT_CURLY)
3391 return G_TOKEN_LEFT_CURLY;
3393 token = g_scanner_get_next_token (scanner);
3395 if (token != G_TOKEN_STRING)
3396 return G_TOKEN_STRING;
3399 source = gtk_icon_source_new ();
3401 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
3404 gtk_icon_source_set_filename (source, full_filename);
3405 g_free (full_filename);
3408 /* We continue parsing even if we didn't find the pixmap so that rest of the
3409 * file is read, even if the syntax is bad
3411 token = g_scanner_get_next_token (scanner);
3413 if (token == G_TOKEN_RIGHT_CURLY)
3415 else if (token != G_TOKEN_COMMA)
3417 gtk_icon_source_free (source);
3418 return G_TOKEN_COMMA;
3421 /* Get the direction */
3423 token = g_scanner_get_next_token (scanner);
3427 case GTK_RC_TOKEN_RTL:
3428 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3429 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
3432 case GTK_RC_TOKEN_LTR:
3433 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3434 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
3441 gtk_icon_source_free (source);
3442 return GTK_RC_TOKEN_RTL;
3446 token = g_scanner_get_next_token (scanner);
3448 if (token == G_TOKEN_RIGHT_CURLY)
3450 else if (token != G_TOKEN_COMMA)
3452 gtk_icon_source_free (source);
3453 return G_TOKEN_COMMA;
3458 token = g_scanner_get_next_token (scanner);
3462 case GTK_RC_TOKEN_NORMAL:
3463 gtk_icon_source_set_state_wildcarded (source, FALSE);
3464 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
3467 case GTK_RC_TOKEN_PRELIGHT:
3468 gtk_icon_source_set_state_wildcarded (source, FALSE);
3469 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
3473 case GTK_RC_TOKEN_INSENSITIVE:
3474 gtk_icon_source_set_state_wildcarded (source, FALSE);
3475 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
3478 case GTK_RC_TOKEN_ACTIVE:
3479 gtk_icon_source_set_state_wildcarded (source, FALSE);
3480 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
3483 case GTK_RC_TOKEN_SELECTED:
3484 gtk_icon_source_set_state_wildcarded (source, FALSE);
3485 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
3492 gtk_icon_source_free (source);
3493 return GTK_RC_TOKEN_PRELIGHT;
3497 token = g_scanner_get_next_token (scanner);
3499 if (token == G_TOKEN_RIGHT_CURLY)
3501 else if (token != G_TOKEN_COMMA)
3503 gtk_icon_source_free (source);
3504 return G_TOKEN_COMMA;
3509 token = g_scanner_get_next_token (scanner);
3515 if (token != G_TOKEN_STRING)
3517 gtk_icon_source_free (source);
3518 return G_TOKEN_STRING;
3521 size = gtk_icon_size_from_name (scanner->value.v_string);
3523 if (size != GTK_ICON_SIZE_INVALID)
3525 gtk_icon_source_set_size_wildcarded (source, FALSE);
3526 gtk_icon_source_set_size (source, size);
3530 /* Check the close brace */
3532 token = g_scanner_get_next_token (scanner);
3533 if (token != G_TOKEN_RIGHT_CURLY)
3535 gtk_icon_source_free (source);
3536 return G_TOKEN_RIGHT_CURLY;
3540 if (gtk_icon_source_get_filename (source))
3541 gtk_icon_set_add_source (icon_set, source);
3542 gtk_icon_source_free (source);
3544 return G_TOKEN_NONE;
3548 gtk_rc_parse_stock (GtkRcContext *context,
3550 GtkRcStyle *rc_style,
3551 GtkIconFactory *factory)
3553 GtkIconSet *icon_set = NULL;
3554 gchar *stock_id = NULL;
3557 token = g_scanner_get_next_token (scanner);
3558 if (token != GTK_RC_TOKEN_STOCK)
3559 return GTK_RC_TOKEN_STOCK;
3561 token = gtk_rc_parse_stock_id (scanner, &stock_id);
3562 if (token != G_TOKEN_NONE)
3565 token = g_scanner_get_next_token (scanner);
3566 if (token != G_TOKEN_EQUAL_SIGN)
3569 return G_TOKEN_EQUAL_SIGN;
3572 token = g_scanner_get_next_token (scanner);
3573 if (token != G_TOKEN_LEFT_CURLY)
3576 return G_TOKEN_LEFT_CURLY;
3579 token = g_scanner_peek_next_token (scanner);
3580 while (token != G_TOKEN_RIGHT_CURLY)
3582 if (icon_set == NULL)
3583 icon_set = gtk_icon_set_new ();
3585 token = gtk_rc_parse_icon_source (context, scanner, icon_set);
3586 if (token != G_TOKEN_NONE)
3589 gtk_icon_set_unref (icon_set);
3593 token = g_scanner_get_next_token (scanner);
3595 if (token != G_TOKEN_COMMA &&
3596 token != G_TOKEN_RIGHT_CURLY)
3599 gtk_icon_set_unref (icon_set);
3600 return G_TOKEN_RIGHT_CURLY;
3606 gtk_icon_factory_add (factory,
3610 gtk_icon_set_unref (icon_set);
3615 return G_TOKEN_NONE;