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
501 gtk_rc_get_default_files (void)
503 gtk_rc_add_initial_default_files ();
505 return gtk_rc_default_files;
508 /* The following routine is based on _nl_normalize_codeset from
509 * the GNU C library. Contributed by
511 * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
512 * Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
514 * Normalize codeset name. There is no standard for the codeset
515 * names. Normalization allows the user to use any of the common
519 _gtk_normalize_codeset (const gchar *codeset, gint name_len)
527 for (cnt = 0; cnt < name_len; ++cnt)
528 if (isalnum (codeset[cnt]))
532 if (isalpha (codeset[cnt]))
536 retval = g_malloc ((only_digit ? 3 : 0) + len + 1);
540 memcpy (retval, "iso", 4);
546 for (cnt = 0; cnt < name_len; ++cnt)
547 if (isalpha (codeset[cnt]))
548 *wp++ = isupper(codeset[cnt]) ? tolower (codeset[cnt]) : codeset[cnt];
549 else if (isdigit (codeset[cnt]))
550 *wp++ = codeset[cnt];
558 gtk_rc_settings_changed (GtkSettings *settings,
560 GtkRcContext *context)
562 gchar *new_theme_name;
563 gchar *new_key_theme_name;
565 g_object_get (settings,
566 "gtk-theme-name", &new_theme_name,
567 "gtk-key-theme-name", &new_key_theme_name,
570 if ((new_theme_name != context->theme_name &&
571 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
572 (new_key_theme_name != context->key_theme_name &&
573 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
575 gtk_rc_reparse_all_for_settings (settings, TRUE);
578 g_free (new_theme_name);
579 g_free (new_key_theme_name);
582 static GtkRcContext *
583 gtk_rc_context_get (GtkSettings *settings)
585 if (!settings->rc_context)
587 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
589 context->settings = settings;
590 context->rc_style_ht = NULL;
591 context->rc_sets_widget = NULL;
592 context->rc_sets_widget_class = NULL;
593 context->rc_sets_class = NULL;
594 context->rc_files = NULL;
596 g_object_get (settings,
597 "gtk-theme-name", &context->theme_name,
598 "gtk-key-theme-name", &context->key_theme_name,
601 g_signal_connect (settings,
602 "notify::gtk-theme-name",
603 G_CALLBACK (gtk_rc_settings_changed),
605 g_signal_connect (settings,
606 "notify::gtk-key-theme-name",
607 G_CALLBACK (gtk_rc_settings_changed),
610 context->pixmap_path[0] = NULL;
612 context->default_priority = GTK_PATH_PRIO_RC;
615 return settings->rc_context;
619 gtk_rc_parse_named (GtkRcContext *context,
624 const gchar *home_dir;
628 subpath = g_strconcat ("gtk-2.0-", type,
629 G_DIR_SEPARATOR_S "gtkrc",
632 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
634 /* First look in the users home directory
636 home_dir = g_get_home_dir ();
639 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
640 if (!g_file_test (path, G_FILE_TEST_EXISTS))
649 gchar *theme_dir = gtk_rc_get_theme_dir ();
650 path = g_build_filename (theme_dir, name, subpath, NULL);
653 if (!g_file_test (path, G_FILE_TEST_EXISTS))
662 gtk_rc_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
670 gtk_rc_parse_default_files (GtkRcContext *context)
672 gchar *locale_suffixes[3];
673 gint n_locale_suffixes = 0;
680 locale = g_win32_getlocale ();
682 locale = setlocale (LC_CTYPE, NULL);
685 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
687 /* Determine locale-specific suffixes for RC files
689 * We normalize the charset into a standard form,
690 * which has all '-' and '_' characters removed,
693 gchar *normalized_locale;
695 p = strchr (locale, '@');
696 length = p ? (p -locale) : strlen (locale);
698 p = strchr (locale, '.');
701 gchar *tmp1 = g_strndup (locale, p - locale + 1);
702 gchar *tmp2 = _gtk_normalize_codeset (p + 1, length - (p - locale + 1));
704 normalized_locale = g_strconcat (tmp1, tmp2, NULL);
708 locale_suffixes[n_locale_suffixes++] = g_strdup (normalized_locale);
712 normalized_locale = g_strndup (locale, length);
714 p = strchr (normalized_locale, '_');
717 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
718 length = p - normalized_locale;
721 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
723 g_free (normalized_locale);
726 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
728 /* Try to find a locale specific RC file corresponding to the
729 * current locale to parse before the default file.
731 for (j = n_locale_suffixes - 1; j >= 0; j--)
733 gchar *name = g_strconcat (gtk_rc_default_files[i],
737 gtk_rc_parse_file (context, name, GTK_PATH_PRIO_RC, FALSE);
740 gtk_rc_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
743 for (j = 0; j < n_locale_suffixes; j++)
744 g_free (locale_suffixes[j]);
750 static gboolean initialized = FALSE;
756 module_path[0] = NULL;
757 gtk_rc_append_default_module_path();
759 gtk_rc_add_initial_default_files ();
762 gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), TRUE);
766 gtk_rc_parse_string (const gchar *rc_string)
768 g_return_if_fail (rc_string != NULL);
770 gtk_rc_parse_any (gtk_rc_context_get (gtk_settings_get_default ()),
771 "-", -1, rc_string); /* FIXME */
775 gtk_rc_parse_file (GtkRcContext *context,
776 const gchar *filename,
780 GtkRcFile *rc_file = NULL;
785 g_return_if_fail (filename != NULL);
787 saved_priority = context->default_priority;
788 context->default_priority = priority;
790 tmp_list = context->rc_files;
793 rc_file = tmp_list->data;
794 if (!strcmp (rc_file->name, filename))
797 tmp_list = tmp_list->next;
802 rc_file = g_new (GtkRcFile, 1);
803 rc_file->name = g_strdup (filename);
804 rc_file->canonical_name = NULL;
806 rc_file->reload = reload;
808 context->rc_files = g_slist_append (context->rc_files, rc_file);
811 if (!rc_file->canonical_name)
813 /* Get the absolute pathname */
815 if (g_path_is_absolute (rc_file->name))
816 rc_file->canonical_name = rc_file->name;
821 cwd = g_get_current_dir ();
822 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
827 if (!lstat (rc_file->canonical_name, &statbuf))
832 rc_file->mtime = statbuf.st_mtime;
834 fd = open (rc_file->canonical_name, O_RDONLY);
838 /* Temporarily push directory name for this file on
839 * a stack of directory names while parsing it
842 g_slist_prepend (rc_dir_stack,
843 g_path_get_dirname (rc_file->canonical_name));
844 gtk_rc_parse_any (context, filename, fd, NULL);
846 tmp_list = rc_dir_stack;
847 rc_dir_stack = rc_dir_stack->next;
849 g_free (tmp_list->data);
850 g_slist_free_1 (tmp_list);
856 context->default_priority = saved_priority;
860 gtk_rc_parse (const gchar *filename)
862 g_return_if_fail (filename != NULL);
864 gtk_rc_parse_file (gtk_rc_context_get (gtk_settings_get_default ()),
865 filename, GTK_PATH_PRIO_RC, TRUE); /* FIXME */
868 /* Handling of RC styles */
871 gtk_rc_style_get_type (void)
873 static GType object_type = 0;
877 static const GTypeInfo object_info =
879 sizeof (GtkRcStyleClass),
880 (GBaseInitFunc) NULL,
881 (GBaseFinalizeFunc) NULL,
882 (GClassInitFunc) gtk_rc_style_class_init,
883 NULL, /* class_finalize */
884 NULL, /* class_data */
887 (GInstanceInitFunc) gtk_rc_style_init,
890 object_type = g_type_register_static (G_TYPE_OBJECT,
899 gtk_rc_style_init (GtkRcStyle *style)
904 style->font_desc = NULL;
906 for (i = 0; i < 5; i++)
908 static const GdkColor init_color = { 0, 0, 0, 0, };
910 style->bg_pixmap_name[i] = NULL;
911 style->color_flags[i] = 0;
912 style->fg[i] = init_color;
913 style->bg[i] = init_color;
914 style->text[i] = init_color;
915 style->base[i] = init_color;
917 style->xthickness = -1;
918 style->ythickness = -1;
919 style->rc_properties = NULL;
921 style->rc_style_lists = NULL;
922 style->icon_factories = NULL;
926 gtk_rc_style_class_init (GtkRcStyleClass *klass)
928 GObjectClass *object_class = G_OBJECT_CLASS (klass);
930 parent_class = g_type_class_peek_parent (klass);
932 object_class->finalize = gtk_rc_style_finalize;
935 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
936 klass->merge = gtk_rc_style_real_merge;
937 klass->create_style = gtk_rc_style_real_create_style;
941 gtk_rc_style_finalize (GObject *object)
943 GSList *tmp_list1, *tmp_list2;
944 GtkRcStyle *rc_style;
947 rc_style = GTK_RC_STYLE (object);
950 g_free (rc_style->name);
951 if (rc_style->font_desc)
952 pango_font_description_free (rc_style->font_desc);
954 for (i = 0; i < 5; i++)
955 if (rc_style->bg_pixmap_name[i])
956 g_free (rc_style->bg_pixmap_name[i]);
958 /* Now remove all references to this rc_style from
961 tmp_list1 = rc_style->rc_style_lists;
964 GSList *rc_styles = tmp_list1->data;
965 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
966 gtk_style_unref (style);
968 /* Remove the list of styles from the other rc_styles
971 tmp_list2 = rc_styles;
974 GtkRcStyle *other_style = tmp_list2->data;
976 if (other_style != rc_style)
977 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
979 tmp_list2 = tmp_list2->next;
982 /* And from the hash table itself
984 g_hash_table_remove (realized_style_ht, rc_styles);
985 g_slist_free (rc_styles);
987 tmp_list1 = tmp_list1->next;
989 g_slist_free (rc_style->rc_style_lists);
991 if (rc_style->rc_properties)
995 for (i = 0; i < rc_style->rc_properties->len; i++)
997 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
999 g_free (node->origin);
1000 g_value_unset (&node->value);
1002 g_array_free (rc_style->rc_properties, TRUE);
1003 rc_style->rc_properties = NULL;
1006 tmp_list1 = rc_style->icon_factories;
1009 g_object_unref (G_OBJECT (tmp_list1->data));
1011 tmp_list1 = tmp_list1->next;
1013 g_slist_free (rc_style->icon_factories);
1015 G_OBJECT_CLASS (parent_class)->finalize (object);
1019 gtk_rc_style_new (void)
1023 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1029 * gtk_rc_style_copy:
1030 * @orig: the style to copy
1032 * Make a copy of the specified #GtkRcStyle. This function
1033 * will correctly copy an rc style that is a member of a class
1034 * derived from #GtkRcStyle.
1036 * Return value: the resulting #GtkRcStyle
1039 gtk_rc_style_copy (GtkRcStyle *orig)
1043 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1045 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1046 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1052 gtk_rc_style_ref (GtkRcStyle *rc_style)
1054 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1056 g_object_ref (G_OBJECT (rc_style));
1060 gtk_rc_style_unref (GtkRcStyle *rc_style)
1062 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1064 g_object_unref (G_OBJECT (rc_style));
1068 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1070 return GTK_RC_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
1074 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1075 gconstpointer bsearch_node2)
1077 const GtkRcProperty *prop1 = bsearch_node1;
1078 const GtkRcProperty *prop2 = bsearch_node2;
1080 if (prop1->type_name == prop2->type_name)
1081 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1083 return prop1->type_name < prop2->type_name ? -1 : 1;
1087 insert_rc_property (GtkRcStyle *style,
1088 GtkRcProperty *property,
1092 GtkRcProperty *new_property = NULL;
1093 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1095 key.type_name = property->type_name;
1096 key.property_name = property->property_name;
1098 if (!style->rc_properties)
1099 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1102 while (i < style->rc_properties->len)
1104 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1110 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1112 g_free (new_property->origin);
1113 g_value_unset (&new_property->value);
1115 *new_property = key;
1129 g_array_insert_val (style->rc_properties, i, key);
1130 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1133 new_property->origin = g_strdup (property->origin);
1134 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1135 g_value_copy (&property->value, &new_property->value);
1139 gtk_rc_style_real_merge (GtkRcStyle *dest,
1144 for (i = 0; i < 5; i++)
1146 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1147 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1149 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1150 src->color_flags[i] & GTK_RC_FG)
1152 dest->fg[i] = src->fg[i];
1153 dest->color_flags[i] |= GTK_RC_FG;
1155 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1156 src->color_flags[i] & GTK_RC_BG)
1158 dest->bg[i] = src->bg[i];
1159 dest->color_flags[i] |= GTK_RC_BG;
1161 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1162 src->color_flags[i] & GTK_RC_TEXT)
1164 dest->text[i] = src->text[i];
1165 dest->color_flags[i] |= GTK_RC_TEXT;
1167 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1168 src->color_flags[i] & GTK_RC_BASE)
1170 dest->base[i] = src->base[i];
1171 dest->color_flags[i] |= GTK_RC_BASE;
1175 if (dest->xthickness < 0 && src->xthickness >= 0)
1176 dest->xthickness = src->xthickness;
1177 if (dest->ythickness < 0 && src->ythickness >= 0)
1178 dest->ythickness = src->ythickness;
1180 if (!dest->font_desc && src->font_desc)
1181 dest->font_desc = pango_font_description_copy (src->font_desc);
1183 if (src->rc_properties)
1187 for (i = 0; i < src->rc_properties->len; i++)
1188 insert_rc_property (dest,
1189 &g_array_index (src->rc_properties, GtkRcProperty, i),
1195 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1197 return gtk_style_new ();
1201 gtk_rc_clear_hash_node (gpointer key,
1205 gtk_rc_style_unref (data);
1209 gtk_rc_free_rc_sets (GSList *slist)
1215 rc_set = slist->data;
1216 g_pattern_spec_free (rc_set->pspec);
1219 slist = slist->next;
1224 gtk_rc_clear_styles (GtkRcContext *context)
1226 /* Clear out all old rc_styles */
1228 if (context->rc_style_ht)
1230 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1231 g_hash_table_destroy (context->rc_style_ht);
1232 context->rc_style_ht = NULL;
1235 gtk_rc_free_rc_sets (context->rc_sets_widget);
1236 g_slist_free (context->rc_sets_widget);
1237 context->rc_sets_widget = NULL;
1239 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1240 g_slist_free (context->rc_sets_widget_class);
1241 context->rc_sets_widget_class = NULL;
1243 gtk_rc_free_rc_sets (context->rc_sets_class);
1244 g_slist_free (context->rc_sets_class);
1245 context->rc_sets_class = NULL;
1248 /* Reset all our widgets. Also, we have to invalidate cached icons in
1249 * icon sets so they get re-rendered.
1252 gtk_rc_reset_widgets (GtkRcContext *context)
1254 GList *list, *toplevels;
1256 _gtk_icon_set_invalidate_caches ();
1258 toplevels = gtk_window_list_toplevels ();
1259 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1261 for (list = toplevels; list; list = list->next)
1263 gtk_widget_reset_rc_styles (list->data);
1264 gtk_widget_unref (list->data);
1266 g_list_free (toplevels);
1270 * gtk_rc_reparse_all_for_settings:
1271 * @settings: a #GtkSettings
1272 * @force_load: load whether or not anything changed
1274 * If the modification time on any previously read file
1275 * for the given GtkSettings has changed, discard all style information
1276 * and then reread all previously read RC files.
1278 * Return value: %TRUE if the files were reread.
1281 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1282 gboolean force_load)
1284 gboolean mtime_modified = FALSE;
1287 GtkRcContext *context;
1289 struct stat statbuf;
1291 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1293 context = gtk_rc_context_get (settings);
1297 /* Check through and see if any of the RC's have had their
1298 * mtime modified. If so, reparse everything.
1300 tmp_list = context->rc_files;
1303 rc_file = tmp_list->data;
1305 if (!lstat (rc_file->name, &statbuf) &&
1306 (statbuf.st_mtime > rc_file->mtime))
1308 mtime_modified = TRUE;
1312 tmp_list = tmp_list->next;
1316 if (force_load || mtime_modified)
1320 gtk_rc_clear_styles (context);
1321 g_object_freeze_notify (G_OBJECT (context->settings));
1323 old_files = context->rc_files;
1324 context->rc_files = NULL;
1326 gtk_rc_parse_default_files (context);
1328 tmp_list = old_files;
1331 rc_file = tmp_list->data;
1332 if (rc_file->reload)
1333 gtk_rc_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, TRUE);
1335 if (rc_file->canonical_name != rc_file->name)
1336 g_free (rc_file->canonical_name);
1337 g_free (rc_file->name);
1340 tmp_list = tmp_list->next;
1343 g_slist_free (old_files);;
1345 g_free (context->theme_name);
1346 g_free (context->key_theme_name);
1347 g_object_get (context->settings,
1348 "gtk-theme-name", &context->theme_name,
1349 "gtk-key-theme-name", &context->key_theme_name,
1352 if (context->theme_name && context->theme_name[0])
1353 gtk_rc_parse_named (context, context->theme_name, NULL);
1354 if (context->key_theme_name && context->key_theme_name[0])
1355 gtk_rc_parse_named (context, context->key_theme_name, "key");
1357 g_object_thaw_notify (G_OBJECT (context->settings));
1359 gtk_rc_reset_widgets (context);
1362 return mtime_modified;
1366 * gtk_rc_reparse_all:
1368 * If the modification time on any previously read file for the
1369 * default #GtkSettings has changed, discard all style information
1370 * and then reread all previously read RC files.
1372 * Return value: %TRUE if the files were reread.
1375 gtk_rc_reparse_all (void)
1377 return gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), FALSE);
1381 gtk_rc_styles_match (GSList *rc_styles,
1385 const gchar *path_reversed)
1392 rc_set = sets->data;
1395 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1396 rc_styles = g_slist_append (rc_styles, rc_set->rc_style);
1404 * @widget: a #GtkWidget
1406 * Finds all matching RC styles for a given widget,
1407 * composites them together, and then creates a
1408 * #GtkStyle representing the composite appearance.
1409 * (GTK+ actually keeps a cache of previously
1410 * created styles, so a new style may not be
1413 * Returns: the resulting style. No refcount is added
1414 * to the returned style, so if you want to save this
1415 * style around, you should add a reference yourself.
1418 gtk_rc_get_style (GtkWidget *widget)
1420 GtkRcStyle *widget_rc_style;
1421 GSList *rc_styles = NULL;
1422 GtkRcContext *context;
1424 static guint rc_style_key_id = 0;
1426 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1428 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1430 /* We allow the specification of a single rc style to be bound
1431 * tightly to a widget, for application modifications
1433 if (!rc_style_key_id)
1434 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1436 widget_rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
1439 if (widget_rc_style)
1440 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1442 if (context->rc_sets_widget)
1444 gchar *path, *path_reversed;
1447 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1448 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1450 g_free (path_reversed);
1453 if (context->rc_sets_widget_class)
1455 gchar *path, *path_reversed;
1458 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1459 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1461 g_free (path_reversed);
1464 if (context->rc_sets_class)
1468 type = GTK_OBJECT_TYPE (widget);
1472 gchar *path_reversed;
1475 path = gtk_type_name (type);
1476 path_length = strlen (path);
1477 path_reversed = g_strdup (path);
1478 g_strreverse (path_reversed);
1480 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1481 g_free (path_reversed);
1483 type = gtk_type_parent (type);
1488 return gtk_rc_init_style (rc_styles);
1494 * gtk_rc_get_style_by_paths:
1495 * @settings: a #GtkSettings object
1496 * @widget_path: the widget path to use when looking up the style, or %NULL
1497 * @class_path: the class path to use when looking up the style, or %NULL
1498 * @type: a type that will be used along with parent types of this type
1499 * when matching against class styles, or G_TYPE_NONE
1501 * Creates up a #GtkStyle from styles defined in a RC file by providing
1502 * the raw components used in matching. This function may be useful
1503 * when creating pseudo-widgets that should be themed like widgets but
1504 * don't actually have corresponding GTK+ widgets. An example of this
1505 * would be items inside a GNOME canvas widget.
1507 * The action of gtk_rc_get_style() is similar to:
1510 * gtk_widget_path (widget, NULL, &path, NULL);
1511 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
1512 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
1513 * G_OBJECT_TYPE (widget));
1516 * Return value: A style created by matching with the supplied paths,
1517 * or %NULL if nothign matching was specified and the default style should
1518 * be used. The returned value is owned by GTK+ as part of an internal cache,
1519 * so you must call g_object_ref() on the returned value if you want to
1520 * keep a reference to it.
1523 gtk_rc_get_style_by_paths (GtkSettings *settings,
1524 const char *widget_path,
1525 const char *class_path,
1528 /* We duplicate the code from above to avoid slowing down the above
1529 * by generating paths when we don't need them. I don't know if
1530 * this is really worth it.
1532 GSList *rc_styles = NULL;
1533 GtkRcContext *context;
1535 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1537 context = gtk_rc_context_get (settings);
1539 if (context->rc_sets_widget)
1541 gchar *path_reversed;
1544 path_length = strlen (widget_path);
1545 path_reversed = g_strdup (widget_path);
1546 g_strreverse (path_reversed);
1548 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, widget_path, path_reversed);
1549 g_free (path_reversed);
1552 if (context->rc_sets_widget_class)
1554 gchar *path_reversed;
1557 path_length = strlen (class_path);
1558 path_reversed = g_strdup (class_path);
1559 g_strreverse (path_reversed);
1561 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, class_path, path_reversed);
1562 g_free (path_reversed);
1565 if (type != G_TYPE_NONE && context->rc_sets_class)
1570 gchar *path_reversed;
1573 path = g_type_name (type);
1574 path_length = strlen (path);
1575 path_reversed = g_strdup (path);
1576 g_strreverse (path_reversed);
1578 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1579 g_free (path_reversed);
1581 type = g_type_parent (type);
1586 return gtk_rc_init_style (rc_styles);
1592 gtk_rc_add_rc_sets (GSList *slist,
1593 GtkRcStyle *rc_style,
1594 const gchar *pattern)
1596 GtkRcStyle *new_style;
1600 new_style = gtk_rc_style_new ();
1601 *new_style = *rc_style;
1602 new_style->name = g_strdup (rc_style->name);
1603 if (rc_style->font_desc)
1604 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
1606 for (i = 0; i < 5; i++)
1607 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1609 rc_set = g_new (GtkRcSet, 1);
1610 rc_set->pspec = g_pattern_spec_new (pattern);
1611 rc_set->rc_style = rc_style;
1613 return g_slist_prepend (slist, rc_set);
1617 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
1618 const gchar *pattern)
1620 GtkRcContext *context;
1622 g_return_if_fail (rc_style != NULL);
1623 g_return_if_fail (pattern != NULL);
1625 context = gtk_rc_context_get (gtk_settings_get_default ());
1627 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern);
1631 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
1632 const gchar *pattern)
1634 GtkRcContext *context;
1636 g_return_if_fail (rc_style != NULL);
1637 g_return_if_fail (pattern != NULL);
1639 context = gtk_rc_context_get (gtk_settings_get_default ());
1641 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern);
1645 gtk_rc_add_class_style (GtkRcStyle *rc_style,
1646 const gchar *pattern)
1648 GtkRcContext *context;
1650 g_return_if_fail (rc_style != NULL);
1651 g_return_if_fail (pattern != NULL);
1653 context = gtk_rc_context_get (gtk_settings_get_default ());
1655 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern);
1659 gtk_rc_scanner_new (void)
1661 return g_scanner_new (>k_rc_scanner_config);
1665 gtk_rc_parse_any (GtkRcContext *context,
1666 const gchar *input_name,
1668 const gchar *input_string)
1674 scanner = gtk_rc_scanner_new ();
1678 g_assert (input_string == NULL);
1680 g_scanner_input_file (scanner, input_fd);
1684 g_assert (input_string != NULL);
1686 g_scanner_input_text (scanner, input_string, strlen (input_string));
1688 scanner->input_name = input_name;
1690 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1691 g_scanner_scope_add_symbol (scanner, 0, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1696 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1700 guint expected_token;
1702 expected_token = gtk_rc_parse_statement (context, scanner);
1704 if (expected_token != G_TOKEN_NONE)
1711 if (scanner->scope_id == 0)
1713 /* if we are in scope 0, we know the symbol names
1714 * that are associated with certaintoken values.
1715 * so we look them up to make the error messages
1718 if (expected_token > GTK_RC_TOKEN_INVALID &&
1719 expected_token < GTK_RC_TOKEN_LAST)
1721 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1722 if (symbols[i].token == expected_token)
1723 msg = symbols[i].name;
1725 msg = g_strconcat ("e.g. `", msg, "'", NULL);
1727 if (scanner->token > GTK_RC_TOKEN_INVALID &&
1728 scanner->token < GTK_RC_TOKEN_LAST)
1730 symbol_name = "???";
1731 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1732 if (symbols[i].token == scanner->token)
1733 symbol_name = symbols[i].name;
1736 g_scanner_unexp_token (scanner,
1749 g_scanner_destroy (scanner);
1753 gtk_rc_styles_hash (const GSList *rc_styles)
1760 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
1761 rc_styles = rc_styles->next;
1768 gtk_rc_styles_equal (const GSList *a,
1773 if (a->data != b->data)
1783 gtk_rc_style_hash (const gchar *name)
1789 result += (result << 3) + *name++;
1795 gtk_rc_style_equal (const gchar *a,
1798 return (strcmp (a, b) == 0);
1802 gtk_rc_style_find (GtkRcContext *context,
1805 if (context->rc_style_ht)
1806 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
1812 gtk_rc_style_to_style (GtkRcStyle *rc_style)
1816 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
1818 style->rc_style = rc_style;
1820 gtk_rc_style_ref (rc_style);
1822 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
1827 /* Reuses or frees rc_styles */
1829 gtk_rc_init_style (GSList *rc_styles)
1831 GtkStyle *style = NULL;
1834 g_return_val_if_fail (rc_styles != NULL, NULL);
1836 if (!realized_style_ht)
1837 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
1838 (GEqualFunc) gtk_rc_styles_equal);
1840 style = g_hash_table_lookup (realized_style_ht, rc_styles);
1844 GtkRcStyle *base_style = NULL;
1845 GtkRcStyle *proto_style;
1846 GtkRcStyleClass *proto_style_class;
1848 GType rc_style_type = GTK_TYPE_RC_STYLE;
1850 /* Find the first derived style in the list, and use that to
1851 * create the merged style. If we only have raw GtkRcStyles, use
1852 * the first style to create the merged style.
1854 base_style = rc_styles->data;
1855 tmp_styles = rc_styles;
1858 GtkRcStyle *rc_style = tmp_styles->data;
1860 if (G_OBJECT_TYPE (rc_style) != rc_style_type)
1862 base_style = rc_style;
1866 tmp_styles = tmp_styles->next;
1869 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
1870 proto_style = proto_style_class->create_rc_style (base_style);
1872 tmp_styles = rc_styles;
1875 GtkRcStyle *rc_style = tmp_styles->data;
1878 proto_style_class->merge (proto_style, rc_style);
1880 /* Point from each rc_style to the list of styles */
1881 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
1882 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
1884 factories = g_slist_copy (rc_style->icon_factories);
1890 while (iter != NULL)
1892 g_object_ref (G_OBJECT (iter->data));
1893 iter = g_slist_next (iter);
1896 proto_style->icon_factories = g_slist_concat (proto_style->icon_factories,
1901 tmp_styles = tmp_styles->next;
1904 for (i = 0; i < 5; i++)
1905 if (proto_style->bg_pixmap_name[i] &&
1906 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
1908 g_free (proto_style->bg_pixmap_name[i]);
1909 proto_style->bg_pixmap_name[i] = NULL;
1912 style = gtk_rc_style_to_style (proto_style);
1913 gtk_rc_style_unref (proto_style);
1915 g_hash_table_insert (realized_style_ht, rc_styles, style);
1918 g_slist_free (rc_styles);
1923 /*********************
1924 * Parsing functions *
1925 *********************/
1928 rc_parse_token_or_compound (GScanner *scanner,
1930 GTokenType delimiter)
1932 guint token = g_scanner_get_next_token (scanner);
1934 /* we either scan a single token (skipping comments)
1935 * or a compund statement.
1936 * compunds are enclosed in (), [] or {} braces, we read
1937 * them in via deep recursion.
1944 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
1947 g_string_append_printf (gstring, " %f", scanner->value.v_float);
1949 case G_TOKEN_STRING:
1950 string = g_strescape (scanner->value.v_string, NULL);
1951 g_string_append (gstring, " \"");
1952 g_string_append (gstring, string);
1953 g_string_append_c (gstring, '"');
1956 case G_TOKEN_IDENTIFIER:
1957 g_string_append_c (gstring, ' ');
1958 g_string_append (gstring, scanner->value.v_identifier);
1960 case G_TOKEN_COMMENT_SINGLE:
1961 case G_TOKEN_COMMENT_MULTI:
1962 return rc_parse_token_or_compound (scanner, gstring, delimiter);
1963 case G_TOKEN_LEFT_PAREN:
1964 g_string_append_c (gstring, ' ');
1965 g_string_append_c (gstring, token);
1966 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_PAREN);
1967 if (token != G_TOKEN_NONE)
1970 case G_TOKEN_LEFT_CURLY:
1971 g_string_append_c (gstring, ' ');
1972 g_string_append_c (gstring, token);
1973 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_CURLY);
1974 if (token != G_TOKEN_NONE)
1977 case G_TOKEN_LEFT_BRACE:
1978 g_string_append_c (gstring, ' ');
1979 g_string_append_c (gstring, token);
1980 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_BRACE);
1981 if (token != G_TOKEN_NONE)
1985 if (token >= 256 || token < 1)
1986 return delimiter ? delimiter : G_TOKEN_STRING;
1987 g_string_append_c (gstring, ' ');
1988 g_string_append_c (gstring, token);
1989 if (token == delimiter)
1990 return G_TOKEN_NONE;
1994 return G_TOKEN_NONE;
1996 return rc_parse_token_or_compound (scanner, gstring, delimiter);
2000 gtk_rc_parse_assignment (GScanner *scanner,
2001 GtkRcProperty *prop)
2003 gboolean scan_identifier = scanner->config->scan_identifier;
2004 gboolean scan_symbols = scanner->config->scan_symbols;
2005 gboolean identifier_2_string = scanner->config->identifier_2_string;
2006 gboolean char_2_token = scanner->config->char_2_token;
2007 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2008 gboolean numbers_2_int = scanner->config->numbers_2_int;
2009 gboolean negate = FALSE;
2012 /* check that this is an assignment */
2013 if (g_scanner_get_next_token (scanner) != '=')
2016 /* adjust scanner mode */
2017 scanner->config->scan_identifier = TRUE;
2018 scanner->config->scan_symbols = FALSE;
2019 scanner->config->identifier_2_string = FALSE;
2020 scanner->config->char_2_token = TRUE;
2021 scanner->config->scan_identifier_NULL = FALSE;
2022 scanner->config->numbers_2_int = TRUE;
2024 /* record location */
2025 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2027 /* parse optional sign */
2028 if (g_scanner_peek_next_token (scanner) == '-')
2030 g_scanner_get_next_token (scanner); /* eat sign */
2034 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an unparsed compund */
2035 token = g_scanner_peek_next_token (scanner);
2039 g_scanner_get_next_token (scanner);
2040 g_value_init (&prop->value, G_TYPE_LONG);
2041 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2042 token = G_TOKEN_NONE;
2045 g_scanner_get_next_token (scanner);
2046 g_value_init (&prop->value, G_TYPE_DOUBLE);
2047 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2048 token = G_TOKEN_NONE;
2050 case G_TOKEN_STRING:
2051 g_scanner_get_next_token (scanner);
2053 token = G_TOKEN_INT;
2056 g_value_init (&prop->value, G_TYPE_STRING);
2057 g_value_set_string (&prop->value, scanner->value.v_string);
2058 token = G_TOKEN_NONE;
2061 case G_TOKEN_IDENTIFIER:
2062 case G_TOKEN_LEFT_PAREN:
2063 case G_TOKEN_LEFT_CURLY:
2064 case G_TOKEN_LEFT_BRACE:
2067 GString *gstring = g_string_new ("");
2069 token = rc_parse_token_or_compound (scanner, gstring, 0);
2070 if (token == G_TOKEN_NONE)
2072 g_string_append_c (gstring, ' ');
2073 g_value_init (&prop->value, G_TYPE_GSTRING);
2074 g_value_set_static_boxed (&prop->value, gstring);
2077 g_string_free (gstring, TRUE);
2082 g_scanner_get_next_token (scanner);
2083 token = G_TOKEN_INT;
2087 /* restore scanner mode */
2088 scanner->config->scan_identifier = scan_identifier;
2089 scanner->config->scan_symbols = scan_symbols;
2090 scanner->config->identifier_2_string = identifier_2_string;
2091 scanner->config->char_2_token = char_2_token;
2092 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2093 scanner->config->numbers_2_int = numbers_2_int;
2099 is_c_identifier (const gchar *string)
2102 gboolean is_varname;
2104 is_varname = strchr (G_CSET_a_2_z G_CSET_A_2_Z "_", string[0]) != NULL;
2105 for (p = string + 1; *p && is_varname; p++)
2106 is_varname &= strchr (G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_-", *p) != NULL;
2112 gtk_rc_parse_statement (GtkRcContext *context,
2117 token = g_scanner_peek_next_token (scanner);
2120 case GTK_RC_TOKEN_INCLUDE:
2121 token = g_scanner_get_next_token (scanner);
2122 if (token != GTK_RC_TOKEN_INCLUDE)
2123 return GTK_RC_TOKEN_INCLUDE;
2124 token = g_scanner_get_next_token (scanner);
2125 if (token != G_TOKEN_STRING)
2126 return G_TOKEN_STRING;
2127 gtk_rc_parse_file (context, scanner->value.v_string, context->default_priority, FALSE);
2128 return G_TOKEN_NONE;
2130 case GTK_RC_TOKEN_STYLE:
2131 return gtk_rc_parse_style (context, scanner);
2133 case GTK_RC_TOKEN_BINDING:
2134 return gtk_binding_parse_binding (scanner);
2136 case GTK_RC_TOKEN_PIXMAP_PATH:
2137 return gtk_rc_parse_pixmap_path (context, scanner);
2139 case GTK_RC_TOKEN_WIDGET:
2140 return gtk_rc_parse_path_pattern (context, scanner);
2142 case GTK_RC_TOKEN_WIDGET_CLASS:
2143 return gtk_rc_parse_path_pattern (context, scanner);
2145 case GTK_RC_TOKEN_CLASS:
2146 return gtk_rc_parse_path_pattern (context, scanner);
2148 case GTK_RC_TOKEN_MODULE_PATH:
2149 return gtk_rc_parse_module_path (scanner);
2151 case GTK_RC_TOKEN_IM_MODULE_PATH:
2152 return gtk_rc_parse_im_module_path (scanner);
2154 case GTK_RC_TOKEN_IM_MODULE_FILE:
2155 return gtk_rc_parse_im_module_file (scanner);
2157 case G_TOKEN_IDENTIFIER:
2158 if (is_c_identifier (scanner->next_value.v_identifier))
2160 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2163 g_scanner_get_next_token (scanner); /* eat identifier */
2164 name = g_strdup (scanner->value.v_identifier);
2166 token = gtk_rc_parse_assignment (scanner, &prop);
2167 if (token == G_TOKEN_NONE)
2169 GtkSettingsValue svalue;
2171 svalue.origin = prop.origin;
2172 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2173 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2174 gtk_settings_set_property_value (context->settings,
2178 g_free (prop.origin);
2179 if (G_VALUE_TYPE (&prop.value))
2180 g_value_unset (&prop.value);
2187 g_scanner_get_next_token (scanner);
2188 return G_TOKEN_IDENTIFIER;
2191 g_scanner_get_next_token (scanner);
2192 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2197 gtk_rc_parse_style (GtkRcContext *context,
2200 GtkRcStyle *rc_style;
2201 GtkRcStyle *parent_style;
2205 GtkIconFactory *our_factory = NULL;
2207 token = g_scanner_get_next_token (scanner);
2208 if (token != GTK_RC_TOKEN_STYLE)
2209 return GTK_RC_TOKEN_STYLE;
2211 token = g_scanner_get_next_token (scanner);
2212 if (token != G_TOKEN_STRING)
2213 return G_TOKEN_STRING;
2216 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2218 /* If there's a list, its first member is always the factory belonging
2221 if (rc_style && rc_style->icon_factories)
2222 our_factory = rc_style->icon_factories->data;
2227 rc_style = gtk_rc_style_new ();
2228 rc_style->name = g_strdup (scanner->value.v_string);
2230 for (i = 0; i < 5; i++)
2231 rc_style->bg_pixmap_name[i] = NULL;
2233 for (i = 0; i < 5; i++)
2234 rc_style->color_flags[i] = 0;
2237 token = g_scanner_peek_next_token (scanner);
2238 if (token == G_TOKEN_EQUAL_SIGN)
2240 token = g_scanner_get_next_token (scanner);
2242 token = g_scanner_get_next_token (scanner);
2243 if (token != G_TOKEN_STRING)
2248 return G_TOKEN_STRING;
2251 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2256 for (i = 0; i < 5; i++)
2258 rc_style->color_flags[i] = parent_style->color_flags[i];
2259 rc_style->fg[i] = parent_style->fg[i];
2260 rc_style->bg[i] = parent_style->bg[i];
2261 rc_style->text[i] = parent_style->text[i];
2262 rc_style->base[i] = parent_style->base[i];
2265 rc_style->xthickness = parent_style->xthickness;
2266 rc_style->ythickness = parent_style->ythickness;
2268 if (parent_style->font_desc)
2270 if (rc_style->font_desc)
2271 pango_font_description_free (rc_style->font_desc);
2272 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
2275 if (parent_style->rc_properties)
2279 for (i = 0; i < parent_style->rc_properties->len; i++)
2280 insert_rc_property (rc_style,
2281 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
2285 for (i = 0; i < 5; i++)
2287 if (rc_style->bg_pixmap_name[i])
2288 g_free (rc_style->bg_pixmap_name[i]);
2289 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
2292 /* Append parent's factories, adding a ref to them */
2293 if (parent_style->icon_factories != NULL)
2295 /* Add a factory for ourselves if we have none,
2296 * in case we end up defining more stock icons.
2297 * I see no real way around this; we need to maintain
2298 * the invariant that the first factory in the list
2299 * is always our_factory, the one belonging to us,
2300 * and if we put parent factories in the list we can't
2301 * do that if the style is reopened.
2303 if (our_factory == NULL)
2305 our_factory = gtk_icon_factory_new ();
2306 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2310 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories,
2311 g_slist_copy (parent_style->icon_factories));
2313 factories = parent_style->icon_factories;
2314 while (factories != NULL)
2316 g_object_ref (G_OBJECT (factories->data));
2317 factories = factories->next;
2323 token = g_scanner_get_next_token (scanner);
2324 if (token != G_TOKEN_LEFT_CURLY)
2329 return G_TOKEN_LEFT_CURLY;
2332 token = g_scanner_peek_next_token (scanner);
2333 while (token != G_TOKEN_RIGHT_CURLY)
2337 case GTK_RC_TOKEN_BG:
2338 token = gtk_rc_parse_bg (scanner, rc_style);
2340 case GTK_RC_TOKEN_FG:
2341 token = gtk_rc_parse_fg (scanner, rc_style);
2343 case GTK_RC_TOKEN_TEXT:
2344 token = gtk_rc_parse_text (scanner, rc_style);
2346 case GTK_RC_TOKEN_BASE:
2347 token = gtk_rc_parse_base (scanner, rc_style);
2349 case GTK_RC_TOKEN_XTHICKNESS:
2350 token = gtk_rc_parse_xthickness (scanner, rc_style);
2352 case GTK_RC_TOKEN_YTHICKNESS:
2353 token = gtk_rc_parse_ythickness (scanner, rc_style);
2355 case GTK_RC_TOKEN_BG_PIXMAP:
2356 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
2358 case GTK_RC_TOKEN_FONT:
2359 token = gtk_rc_parse_font (scanner, rc_style);
2361 case GTK_RC_TOKEN_FONTSET:
2362 token = gtk_rc_parse_fontset (scanner, rc_style);
2364 case GTK_RC_TOKEN_FONT_NAME:
2365 token = gtk_rc_parse_font_name (scanner, rc_style);
2367 case GTK_RC_TOKEN_ENGINE:
2368 token = gtk_rc_parse_engine (context, scanner, &rc_style);
2370 case GTK_RC_TOKEN_STOCK:
2371 if (our_factory == NULL)
2373 our_factory = gtk_icon_factory_new ();
2374 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2377 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
2379 case G_TOKEN_IDENTIFIER:
2380 if (is_c_identifier (scanner->next_value.v_identifier) &&
2381 scanner->next_value.v_identifier[0] >= 'A' &&
2382 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
2384 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2386 g_scanner_get_next_token (scanner); /* eat type name */
2387 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
2388 if (g_scanner_get_next_token (scanner) != ':' ||
2389 g_scanner_get_next_token (scanner) != ':')
2394 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
2395 !is_c_identifier (scanner->value.v_identifier))
2397 token = G_TOKEN_IDENTIFIER;
2401 /* it's important that we do the same canonification as GParamSpecPool here */
2402 g_strcanon (scanner->value.v_identifier, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2403 prop.property_name = g_quark_from_string (scanner->value.v_identifier);
2405 token = gtk_rc_parse_assignment (scanner, &prop);
2406 if (token == G_TOKEN_NONE)
2408 g_return_val_if_fail (prop.origin != NULL && G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
2409 insert_rc_property (rc_style, &prop, TRUE);
2412 g_free (prop.origin);
2413 if (G_VALUE_TYPE (&prop.value))
2414 g_value_unset (&prop.value);
2418 g_scanner_get_next_token (scanner);
2419 token = G_TOKEN_IDENTIFIER;
2423 g_scanner_get_next_token (scanner);
2424 token = G_TOKEN_RIGHT_CURLY;
2428 if (token != G_TOKEN_NONE)
2431 gtk_rc_style_unref (rc_style);
2435 token = g_scanner_peek_next_token (scanner);
2436 } /* while (token != G_TOKEN_RIGHT_CURLY) */
2438 token = g_scanner_get_next_token (scanner);
2439 if (token != G_TOKEN_RIGHT_CURLY)
2443 if (rc_style->font_desc)
2444 pango_font_description_free (rc_style->font_desc);
2446 for (i = 0; i < 5; i++)
2447 if (rc_style->bg_pixmap_name[i])
2448 g_free (rc_style->bg_pixmap_name[i]);
2452 return G_TOKEN_RIGHT_CURLY;
2457 if (!context->rc_style_ht)
2458 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
2459 (GEqualFunc) gtk_rc_style_equal);
2461 g_hash_table_insert (context->rc_style_ht, rc_style->name, rc_style);
2464 return G_TOKEN_NONE;
2467 const GtkRcProperty*
2468 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
2470 GQuark property_name)
2472 GtkRcProperty *node = NULL;
2474 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
2476 if (rc_style->rc_properties)
2480 key.type_name = type_name;
2481 key.property_name = property_name;
2483 node = bsearch (&key,
2484 rc_style->rc_properties->data, rc_style->rc_properties->len,
2485 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
2492 gtk_rc_parse_bg (GScanner *scanner,
2498 token = g_scanner_get_next_token (scanner);
2499 if (token != GTK_RC_TOKEN_BG)
2500 return GTK_RC_TOKEN_BG;
2502 token = gtk_rc_parse_state (scanner, &state);
2503 if (token != G_TOKEN_NONE)
2506 token = g_scanner_get_next_token (scanner);
2507 if (token != G_TOKEN_EQUAL_SIGN)
2508 return G_TOKEN_EQUAL_SIGN;
2510 style->color_flags[state] |= GTK_RC_BG;
2511 return gtk_rc_parse_color (scanner, &style->bg[state]);
2515 gtk_rc_parse_fg (GScanner *scanner,
2521 token = g_scanner_get_next_token (scanner);
2522 if (token != GTK_RC_TOKEN_FG)
2523 return GTK_RC_TOKEN_FG;
2525 token = gtk_rc_parse_state (scanner, &state);
2526 if (token != G_TOKEN_NONE)
2529 token = g_scanner_get_next_token (scanner);
2530 if (token != G_TOKEN_EQUAL_SIGN)
2531 return G_TOKEN_EQUAL_SIGN;
2533 style->color_flags[state] |= GTK_RC_FG;
2534 return gtk_rc_parse_color (scanner, &style->fg[state]);
2538 gtk_rc_parse_text (GScanner *scanner,
2544 token = g_scanner_get_next_token (scanner);
2545 if (token != GTK_RC_TOKEN_TEXT)
2546 return GTK_RC_TOKEN_TEXT;
2548 token = gtk_rc_parse_state (scanner, &state);
2549 if (token != G_TOKEN_NONE)
2552 token = g_scanner_get_next_token (scanner);
2553 if (token != G_TOKEN_EQUAL_SIGN)
2554 return G_TOKEN_EQUAL_SIGN;
2556 style->color_flags[state] |= GTK_RC_TEXT;
2557 return gtk_rc_parse_color (scanner, &style->text[state]);
2561 gtk_rc_parse_base (GScanner *scanner,
2567 token = g_scanner_get_next_token (scanner);
2568 if (token != GTK_RC_TOKEN_BASE)
2569 return GTK_RC_TOKEN_BASE;
2571 token = gtk_rc_parse_state (scanner, &state);
2572 if (token != G_TOKEN_NONE)
2575 token = g_scanner_get_next_token (scanner);
2576 if (token != G_TOKEN_EQUAL_SIGN)
2577 return G_TOKEN_EQUAL_SIGN;
2579 style->color_flags[state] |= GTK_RC_BASE;
2580 return gtk_rc_parse_color (scanner, &style->base[state]);
2584 gtk_rc_parse_xthickness (GScanner *scanner,
2587 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
2588 return GTK_RC_TOKEN_XTHICKNESS;
2590 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2591 return G_TOKEN_EQUAL_SIGN;
2593 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2596 style->xthickness = scanner->value.v_int;
2598 return G_TOKEN_NONE;
2602 gtk_rc_parse_ythickness (GScanner *scanner,
2605 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
2606 return GTK_RC_TOKEN_YTHICKNESS;
2608 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2609 return G_TOKEN_EQUAL_SIGN;
2611 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2614 style->ythickness = scanner->value.v_int;
2616 return G_TOKEN_NONE;
2620 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
2622 GtkRcStyle *rc_style)
2628 token = g_scanner_get_next_token (scanner);
2629 if (token != GTK_RC_TOKEN_BG_PIXMAP)
2630 return GTK_RC_TOKEN_BG_PIXMAP;
2632 token = gtk_rc_parse_state (scanner, &state);
2633 if (token != G_TOKEN_NONE)
2636 token = g_scanner_get_next_token (scanner);
2637 if (token != G_TOKEN_EQUAL_SIGN)
2638 return G_TOKEN_EQUAL_SIGN;
2640 token = g_scanner_get_next_token (scanner);
2641 if (token != G_TOKEN_STRING)
2642 return G_TOKEN_STRING;
2644 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
2645 (strcmp (scanner->value.v_string, "<none>") == 0))
2646 pixmap_file = g_strdup (scanner->value.v_string);
2648 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
2649 scanner, scanner->value.v_string);
2653 if (rc_style->bg_pixmap_name[state])
2654 g_free (rc_style->bg_pixmap_name[state]);
2655 rc_style->bg_pixmap_name[state] = pixmap_file;
2658 return G_TOKEN_NONE;
2662 gtk_rc_check_pixmap_dir (const gchar *dir, const gchar *pixmap_file)
2667 buf = g_build_filename (dir, pixmap_file, NULL);
2669 fd = open (buf, O_RDONLY);
2682 * gtk_rc_find_pixmap_in_path:
2683 * @settings: a #GtkSettinsg
2684 * @scanner: Scanner used to get line number information for the
2685 * warning message, or %NULL
2686 * @pixmap_file: name of the pixmap file to locate.
2688 * Looks up a file in pixmap path for the specified #GtkSettings.
2689 * If the file is not found, it outputs a warning message using
2690 * g_warning() and returns %NULL.
2695 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
2697 const gchar *pixmap_file)
2703 GtkRcContext *context = gtk_rc_context_get (settings);
2705 for (i = 0; (i < GTK_RC_MAX_PIXMAP_PATHS) && (context->pixmap_path[i] != NULL); i++)
2707 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
2712 tmp_list = rc_dir_stack;
2715 filename = gtk_rc_check_pixmap_dir (tmp_list->data, pixmap_file);
2719 tmp_list = tmp_list->next;
2723 g_warning (_("Unable to locate image file in pixmap_path: \"%s\" line %d"),
2724 pixmap_file, scanner->line);
2726 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
2733 gtk_rc_find_module_in_path (const gchar *module_file)
2739 for (i = 0; (i < GTK_RC_MAX_MODULE_PATHS) && (module_path[i] != NULL); i++)
2741 buf = g_build_filename (module_path[i], module_file, NULL);
2743 fd = open (buf, O_RDONLY);
2757 gtk_rc_parse_font (GScanner *scanner,
2758 GtkRcStyle *rc_style)
2762 token = g_scanner_get_next_token (scanner);
2763 if (token != GTK_RC_TOKEN_FONT)
2764 return GTK_RC_TOKEN_FONT;
2766 token = g_scanner_get_next_token (scanner);
2767 if (token != G_TOKEN_EQUAL_SIGN)
2768 return G_TOKEN_EQUAL_SIGN;
2770 token = g_scanner_get_next_token (scanner);
2771 if (token != G_TOKEN_STRING)
2772 return G_TOKEN_STRING;
2774 /* Ignore, do nothing */
2776 return G_TOKEN_NONE;
2780 gtk_rc_parse_fontset (GScanner *scanner,
2781 GtkRcStyle *rc_style)
2785 token = g_scanner_get_next_token (scanner);
2786 if (token != GTK_RC_TOKEN_FONTSET)
2787 return GTK_RC_TOKEN_FONTSET;
2789 token = g_scanner_get_next_token (scanner);
2790 if (token != G_TOKEN_EQUAL_SIGN)
2791 return G_TOKEN_EQUAL_SIGN;
2793 token = g_scanner_get_next_token (scanner);
2794 if (token != G_TOKEN_STRING)
2795 return G_TOKEN_STRING;
2797 /* Do nothing - silently ignore */
2799 return G_TOKEN_NONE;
2803 gtk_rc_parse_font_name (GScanner *scanner,
2804 GtkRcStyle *rc_style)
2808 token = g_scanner_get_next_token (scanner);
2809 if (token != GTK_RC_TOKEN_FONT_NAME)
2810 return GTK_RC_TOKEN_FONT;
2812 token = g_scanner_get_next_token (scanner);
2813 if (token != G_TOKEN_EQUAL_SIGN)
2814 return G_TOKEN_EQUAL_SIGN;
2816 token = g_scanner_get_next_token (scanner);
2817 if (token != G_TOKEN_STRING)
2818 return G_TOKEN_STRING;
2820 rc_style->font_desc = pango_font_description_from_string (scanner->value.v_string);
2822 return G_TOKEN_NONE;
2826 gtk_rc_parse_engine (GtkRcContext *context,
2828 GtkRcStyle **rc_style)
2831 GtkThemeEngine *engine;
2832 guint result = G_TOKEN_NONE;
2833 GtkRcStyle *new_style = NULL;
2834 gboolean parsed_curlies = FALSE;
2836 token = g_scanner_get_next_token (scanner);
2837 if (token != GTK_RC_TOKEN_ENGINE)
2838 return GTK_RC_TOKEN_ENGINE;
2840 token = g_scanner_get_next_token (scanner);
2841 if (token != G_TOKEN_STRING)
2842 return G_TOKEN_STRING;
2844 engine = gtk_theme_engine_get (scanner->value.v_string);
2846 token = g_scanner_get_next_token (scanner);
2847 if (token != G_TOKEN_LEFT_CURLY)
2848 return G_TOKEN_LEFT_CURLY;
2852 GtkRcStyleClass *new_class;
2854 new_style = gtk_theme_engine_create_rc_style (engine);
2855 g_type_module_unuse (G_TYPE_MODULE (engine));
2857 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
2859 new_class->merge (new_style, *rc_style);
2860 if ((*rc_style)->name)
2861 new_style->name = g_strdup ((*rc_style)->name);
2863 if (new_class->parse)
2865 parsed_curlies = TRUE;
2866 result = new_class->parse (new_style, context->settings, scanner);
2868 if (result != G_TOKEN_NONE)
2870 g_object_unref (G_OBJECT (new_style));
2876 if (!parsed_curlies)
2878 /* Skip over remainder, looking for nested {}'s
2882 result = G_TOKEN_RIGHT_CURLY;
2883 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
2885 if (token == G_TOKEN_LEFT_CURLY)
2887 else if (token == G_TOKEN_RIGHT_CURLY)
2892 result = G_TOKEN_NONE;
2900 g_object_unref (G_OBJECT (*rc_style));
2901 *rc_style = new_style;
2908 gtk_rc_parse_state (GScanner *scanner,
2909 GtkStateType *state)
2914 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2915 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
2917 /* we don't know where we got called from, so we reset the scope here.
2918 * if we bail out due to errors, we *don't* reset the scope, so the
2919 * error messaging code can make sense of our tokens.
2921 old_scope = g_scanner_set_scope (scanner, 0);
2923 token = g_scanner_get_next_token (scanner);
2924 if (token != G_TOKEN_LEFT_BRACE)
2925 return G_TOKEN_LEFT_BRACE;
2927 token = g_scanner_get_next_token (scanner);
2930 case GTK_RC_TOKEN_ACTIVE:
2931 *state = GTK_STATE_ACTIVE;
2933 case GTK_RC_TOKEN_INSENSITIVE:
2934 *state = GTK_STATE_INSENSITIVE;
2936 case GTK_RC_TOKEN_NORMAL:
2937 *state = GTK_STATE_NORMAL;
2939 case GTK_RC_TOKEN_PRELIGHT:
2940 *state = GTK_STATE_PRELIGHT;
2942 case GTK_RC_TOKEN_SELECTED:
2943 *state = GTK_STATE_SELECTED;
2946 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
2949 token = g_scanner_get_next_token (scanner);
2950 if (token != G_TOKEN_RIGHT_BRACE)
2951 return G_TOKEN_RIGHT_BRACE;
2953 g_scanner_set_scope (scanner, old_scope);
2955 return G_TOKEN_NONE;
2959 gtk_rc_parse_priority (GScanner *scanner,
2960 GtkPathPriorityType *priority)
2965 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2966 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
2968 /* we don't know where we got called from, so we reset the scope here.
2969 * if we bail out due to errors, we *don't* reset the scope, so the
2970 * error messaging code can make sense of our tokens.
2972 old_scope = g_scanner_set_scope (scanner, 0);
2974 token = g_scanner_get_next_token (scanner);
2978 token = g_scanner_get_next_token (scanner);
2981 case GTK_RC_TOKEN_LOWEST:
2982 *priority = GTK_PATH_PRIO_LOWEST;
2984 case GTK_RC_TOKEN_GTK:
2985 *priority = GTK_PATH_PRIO_GTK;
2987 case GTK_RC_TOKEN_APPLICATION:
2988 *priority = GTK_PATH_PRIO_APPLICATION;
2990 case GTK_RC_TOKEN_THEME:
2991 *priority = GTK_PATH_PRIO_THEME;
2993 case GTK_RC_TOKEN_RC:
2994 *priority = GTK_PATH_PRIO_RC;
2996 case GTK_RC_TOKEN_HIGHEST:
2997 *priority = GTK_PATH_PRIO_HIGHEST;
3000 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3003 g_scanner_set_scope (scanner, old_scope);
3005 return G_TOKEN_NONE;
3009 gtk_rc_parse_color (GScanner *scanner,
3014 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3016 /* we don't need to set our own scope here, because
3017 * we don't need own symbols
3020 token = g_scanner_get_next_token (scanner);
3025 case G_TOKEN_LEFT_CURLY:
3026 token = g_scanner_get_next_token (scanner);
3027 if (token == G_TOKEN_INT)
3028 token_int = scanner->value.v_int;
3029 else if (token == G_TOKEN_FLOAT)
3030 token_int = scanner->value.v_float * 65535.0;
3032 return G_TOKEN_FLOAT;
3033 color->red = CLAMP (token_int, 0, 65535);
3035 token = g_scanner_get_next_token (scanner);
3036 if (token != G_TOKEN_COMMA)
3037 return G_TOKEN_COMMA;
3039 token = g_scanner_get_next_token (scanner);
3040 if (token == G_TOKEN_INT)
3041 token_int = scanner->value.v_int;
3042 else if (token == G_TOKEN_FLOAT)
3043 token_int = scanner->value.v_float * 65535.0;
3045 return G_TOKEN_FLOAT;
3046 color->green = CLAMP (token_int, 0, 65535);
3048 token = g_scanner_get_next_token (scanner);
3049 if (token != G_TOKEN_COMMA)
3050 return G_TOKEN_COMMA;
3052 token = g_scanner_get_next_token (scanner);
3053 if (token == G_TOKEN_INT)
3054 token_int = scanner->value.v_int;
3055 else if (token == G_TOKEN_FLOAT)
3056 token_int = scanner->value.v_float * 65535.0;
3058 return G_TOKEN_FLOAT;
3059 color->blue = CLAMP (token_int, 0, 65535);
3061 token = g_scanner_get_next_token (scanner);
3062 if (token != G_TOKEN_RIGHT_CURLY)
3063 return G_TOKEN_RIGHT_CURLY;
3064 return G_TOKEN_NONE;
3066 case G_TOKEN_STRING:
3067 if (!gdk_color_parse (scanner->value.v_string, color))
3069 g_scanner_warn (scanner, "Invalid color constant '%s'",
3070 scanner->value.v_string);
3071 return G_TOKEN_STRING;
3074 return G_TOKEN_NONE;
3077 return G_TOKEN_STRING;
3082 gtk_rc_parse_pixmap_path (GtkRcContext *context,
3087 token = g_scanner_get_next_token (scanner);
3088 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
3089 return GTK_RC_TOKEN_PIXMAP_PATH;
3091 token = g_scanner_get_next_token (scanner);
3092 if (token != G_TOKEN_STRING)
3093 return G_TOKEN_STRING;
3095 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
3097 return G_TOKEN_NONE;
3101 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
3103 const gchar *pix_path)
3106 gint start_offset = 0;
3110 /* free the old one, or just add to the old one ? */
3111 for (path_num = 0; context->pixmap_path[path_num]; path_num++)
3113 g_free (context->pixmap_path[path_num]);
3114 context->pixmap_path[path_num] = NULL;
3119 path_len = strlen (pix_path);
3121 for (end_offset = 0; end_offset <= path_len; end_offset++)
3123 if ((pix_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3124 (end_offset == path_len))
3126 gchar *path_element = g_strndup (pix_path + start_offset, end_offset - start_offset);
3127 if (g_path_is_absolute (path_element))
3129 context->pixmap_path[path_num] = path_element;
3131 context->pixmap_path[path_num] = NULL;
3135 g_warning (_("Pixmap path element: \"%s\" must be absolute, %s, line %d"),
3136 path_element, scanner->input_name, scanner->line);
3137 g_free (path_element);
3140 start_offset = end_offset + 1;
3146 gtk_rc_parse_module_path (GScanner *scanner)
3150 token = g_scanner_get_next_token (scanner);
3151 if (token != GTK_RC_TOKEN_MODULE_PATH)
3152 return GTK_RC_TOKEN_MODULE_PATH;
3154 token = g_scanner_get_next_token (scanner);
3155 if (token != G_TOKEN_STRING)
3156 return G_TOKEN_STRING;
3158 gtk_rc_parse_module_path_string (scanner->value.v_string);
3160 return G_TOKEN_NONE;
3164 gtk_rc_parse_im_module_path (GScanner *scanner)
3168 token = g_scanner_get_next_token (scanner);
3169 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3170 return GTK_RC_TOKEN_IM_MODULE_FILE;
3172 token = g_scanner_get_next_token (scanner);
3173 if (token != G_TOKEN_STRING)
3174 return G_TOKEN_STRING;
3177 g_free (im_module_path);
3179 im_module_path = g_strdup (scanner->value.v_string);
3181 return G_TOKEN_NONE;
3185 gtk_rc_parse_im_module_file (GScanner *scanner)
3189 token = g_scanner_get_next_token (scanner);
3190 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3191 return GTK_RC_TOKEN_IM_MODULE_FILE;
3193 token = g_scanner_get_next_token (scanner);
3194 if (token != G_TOKEN_STRING)
3195 return G_TOKEN_STRING;
3198 g_free (im_module_file);
3200 im_module_file = g_strdup (scanner->value.v_string);
3202 return G_TOKEN_NONE;
3206 gtk_rc_parse_module_path_string (const gchar *mod_path)
3209 gint start_offset = 0;
3213 /* free the old one, or just add to the old one ? */
3214 for (path_num=0; module_path[path_num]; path_num++)
3216 g_free (module_path[path_num]);
3217 module_path[path_num] = NULL;
3222 path_len = strlen (mod_path);
3224 for (end_offset = 0; end_offset <= path_len; end_offset++)
3226 if ((mod_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3227 (end_offset == path_len))
3229 module_path[path_num] = g_strndup (mod_path + start_offset, end_offset - start_offset);
3231 module_path[path_num] = NULL;
3232 start_offset = end_offset + 1;
3235 gtk_rc_append_default_module_path();
3239 rc_set_compare (gconstpointer a, gconstpointer b)
3241 const GtkRcSet *set_a = a;
3242 const GtkRcSet *set_b = b;
3244 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
3248 insert_rc_set (GSList *list, GtkRcSet *set)
3250 return g_slist_insert_sorted (list, set, rc_set_compare);
3254 gtk_rc_parse_path_pattern (GtkRcContext *context,
3258 GtkPathType path_type;
3260 gboolean is_binding;
3261 GtkPathPriorityType priority = context->default_priority;
3263 token = g_scanner_get_next_token (scanner);
3266 case GTK_RC_TOKEN_WIDGET:
3267 path_type = GTK_PATH_WIDGET;
3269 case GTK_RC_TOKEN_WIDGET_CLASS:
3270 path_type = GTK_PATH_WIDGET_CLASS;
3272 case GTK_RC_TOKEN_CLASS:
3273 path_type = GTK_PATH_CLASS;
3276 return GTK_RC_TOKEN_WIDGET_CLASS;
3279 token = g_scanner_get_next_token (scanner);
3280 if (token != G_TOKEN_STRING)
3281 return G_TOKEN_STRING;
3283 pattern = g_strdup (scanner->value.v_string);
3285 token = g_scanner_get_next_token (scanner);
3286 if (token == GTK_RC_TOKEN_STYLE)
3288 else if (token == GTK_RC_TOKEN_BINDING)
3293 return GTK_RC_TOKEN_STYLE;
3296 if (g_scanner_peek_next_token (scanner) == ':')
3298 token = gtk_rc_parse_priority (scanner, &priority);
3299 if (token != G_TOKEN_NONE)
3306 token = g_scanner_get_next_token (scanner);
3307 if (token != G_TOKEN_STRING)
3310 return G_TOKEN_STRING;
3315 GtkBindingSet *binding;
3317 binding = gtk_binding_set_find (scanner->value.v_string);
3321 return G_TOKEN_STRING;
3323 gtk_binding_set_add_path (binding, path_type, pattern, priority);
3327 GtkRcStyle *rc_style;
3330 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3335 return G_TOKEN_STRING;
3338 rc_set = g_new (GtkRcSet, 1);
3339 rc_set->pspec = g_pattern_spec_new (pattern);
3340 rc_set->rc_style = rc_style;
3341 rc_set->priority = priority;
3343 if (path_type == GTK_PATH_WIDGET)
3344 context->rc_sets_widget = insert_rc_set (context->rc_sets_widget, rc_set);
3345 else if (path_type == GTK_PATH_WIDGET_CLASS)
3346 context->rc_sets_widget_class = insert_rc_set (context->rc_sets_widget_class, rc_set);
3348 context->rc_sets_class = insert_rc_set (context->rc_sets_class, rc_set);
3352 return G_TOKEN_NONE;
3356 gtk_rc_parse_stock_id (GScanner *scanner,
3361 token = g_scanner_get_next_token (scanner);
3362 if (token != G_TOKEN_LEFT_BRACE)
3363 return G_TOKEN_LEFT_BRACE;
3365 token = g_scanner_get_next_token (scanner);
3367 if (token != G_TOKEN_STRING)
3368 return G_TOKEN_STRING;
3370 *stock_id = g_strdup (scanner->value.v_string);
3372 token = g_scanner_get_next_token (scanner);
3373 if (token != G_TOKEN_RIGHT_BRACE)
3376 return G_TOKEN_RIGHT_BRACE;
3379 return G_TOKEN_NONE;
3383 gtk_rc_parse_icon_source (GtkRcContext *context,
3385 GtkIconSet *icon_set)
3388 GtkIconSource *source;
3389 gchar *full_filename;
3391 token = g_scanner_get_next_token (scanner);
3392 if (token != G_TOKEN_LEFT_CURLY)
3393 return G_TOKEN_LEFT_CURLY;
3395 token = g_scanner_get_next_token (scanner);
3397 if (token != G_TOKEN_STRING)
3398 return G_TOKEN_STRING;
3401 source = gtk_icon_source_new ();
3403 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
3406 gtk_icon_source_set_filename (source, full_filename);
3407 g_free (full_filename);
3410 /* We continue parsing even if we didn't find the pixmap so that rest of the
3411 * file is read, even if the syntax is bad
3413 token = g_scanner_get_next_token (scanner);
3415 if (token == G_TOKEN_RIGHT_CURLY)
3417 else if (token != G_TOKEN_COMMA)
3419 gtk_icon_source_free (source);
3420 return G_TOKEN_COMMA;
3423 /* Get the direction */
3425 token = g_scanner_get_next_token (scanner);
3429 case GTK_RC_TOKEN_RTL:
3430 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3431 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
3434 case GTK_RC_TOKEN_LTR:
3435 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3436 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
3443 gtk_icon_source_free (source);
3444 return GTK_RC_TOKEN_RTL;
3448 token = g_scanner_get_next_token (scanner);
3450 if (token == G_TOKEN_RIGHT_CURLY)
3452 else if (token != G_TOKEN_COMMA)
3454 gtk_icon_source_free (source);
3455 return G_TOKEN_COMMA;
3460 token = g_scanner_get_next_token (scanner);
3464 case GTK_RC_TOKEN_NORMAL:
3465 gtk_icon_source_set_state_wildcarded (source, FALSE);
3466 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
3469 case GTK_RC_TOKEN_PRELIGHT:
3470 gtk_icon_source_set_state_wildcarded (source, FALSE);
3471 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
3475 case GTK_RC_TOKEN_INSENSITIVE:
3476 gtk_icon_source_set_state_wildcarded (source, FALSE);
3477 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
3480 case GTK_RC_TOKEN_ACTIVE:
3481 gtk_icon_source_set_state_wildcarded (source, FALSE);
3482 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
3485 case GTK_RC_TOKEN_SELECTED:
3486 gtk_icon_source_set_state_wildcarded (source, FALSE);
3487 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
3494 gtk_icon_source_free (source);
3495 return GTK_RC_TOKEN_PRELIGHT;
3499 token = g_scanner_get_next_token (scanner);
3501 if (token == G_TOKEN_RIGHT_CURLY)
3503 else if (token != G_TOKEN_COMMA)
3505 gtk_icon_source_free (source);
3506 return G_TOKEN_COMMA;
3511 token = g_scanner_get_next_token (scanner);
3517 if (token != G_TOKEN_STRING)
3519 gtk_icon_source_free (source);
3520 return G_TOKEN_STRING;
3523 size = gtk_icon_size_from_name (scanner->value.v_string);
3525 if (size != GTK_ICON_SIZE_INVALID)
3527 gtk_icon_source_set_size_wildcarded (source, FALSE);
3528 gtk_icon_source_set_size (source, size);
3532 /* Check the close brace */
3534 token = g_scanner_get_next_token (scanner);
3535 if (token != G_TOKEN_RIGHT_CURLY)
3537 gtk_icon_source_free (source);
3538 return G_TOKEN_RIGHT_CURLY;
3542 if (gtk_icon_source_get_filename (source))
3543 gtk_icon_set_add_source (icon_set, source);
3544 gtk_icon_source_free (source);
3546 return G_TOKEN_NONE;
3550 gtk_rc_parse_stock (GtkRcContext *context,
3552 GtkRcStyle *rc_style,
3553 GtkIconFactory *factory)
3555 GtkIconSet *icon_set = NULL;
3556 gchar *stock_id = NULL;
3559 token = g_scanner_get_next_token (scanner);
3560 if (token != GTK_RC_TOKEN_STOCK)
3561 return GTK_RC_TOKEN_STOCK;
3563 token = gtk_rc_parse_stock_id (scanner, &stock_id);
3564 if (token != G_TOKEN_NONE)
3567 token = g_scanner_get_next_token (scanner);
3568 if (token != G_TOKEN_EQUAL_SIGN)
3571 return G_TOKEN_EQUAL_SIGN;
3574 token = g_scanner_get_next_token (scanner);
3575 if (token != G_TOKEN_LEFT_CURLY)
3578 return G_TOKEN_LEFT_CURLY;
3581 token = g_scanner_peek_next_token (scanner);
3582 while (token != G_TOKEN_RIGHT_CURLY)
3584 if (icon_set == NULL)
3585 icon_set = gtk_icon_set_new ();
3587 token = gtk_rc_parse_icon_source (context, scanner, icon_set);
3588 if (token != G_TOKEN_NONE)
3591 gtk_icon_set_unref (icon_set);
3595 token = g_scanner_get_next_token (scanner);
3597 if (token != G_TOKEN_COMMA &&
3598 token != G_TOKEN_RIGHT_CURLY)
3601 gtk_icon_set_unref (icon_set);
3602 return G_TOKEN_RIGHT_CURLY;
3608 gtk_icon_factory_add (factory,
3612 gtk_icon_set_unref (icon_set);
3617 return G_TOKEN_NONE;