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)
305 var = g_getenv ("GTK_EXE_PREFIX");
307 path = g_build_filename (var, "lib", "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
309 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
315 gtk_rc_get_im_module_path (void)
317 const gchar *result = g_getenv ("GTK_IM_MODULE_PATH");
322 result = im_module_path;
324 return gtk_rc_make_default_dir ("immodules");
327 return g_strdup (result);
331 gtk_rc_get_im_module_file (void)
333 gchar *result = g_strdup (g_getenv ("GTK_IM_MODULE_FILE"));
338 result = g_strdup (im_module_file);
340 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
347 gtk_rc_get_theme_dir (void)
352 var = g_getenv ("GTK_DATA_PREFIX");
354 path = g_build_filename (var, "share", "themes", NULL);
356 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
362 gtk_rc_get_module_dir (void)
364 return gtk_rc_make_default_dir ("engines");
368 gtk_rc_append_default_module_path (void)
374 for (n = 0; module_path[n]; n++) ;
375 if (n >= GTK_RC_MAX_MODULE_PATHS - 1)
378 var = g_getenv ("GTK_EXE_PREFIX");
380 path = g_build_filename (var, "lib", "gtk-2.0", GTK_VERSION, "engines", NULL);
382 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_VERSION, "engines", NULL);
383 module_path[n++] = path;
385 var = g_get_home_dir ();
388 path = g_build_filename (var, ".gtk-2.0", GTK_VERSION, "engines", NULL);
389 module_path[n++] = path;
391 module_path[n] = NULL;
395 gtk_rc_add_initial_default_files (void)
397 static gint init = FALSE;
406 gtk_rc_default_files[0] = NULL;
409 var = g_getenv ("GTK_RC_FILES");
412 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, 128);
416 gtk_rc_add_default_file (files[i]);
423 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
425 gtk_rc_add_default_file (str);
428 var = g_get_home_dir ();
431 str = g_build_filename (var, ".gtkrc-2.0", NULL);
432 gtk_rc_add_default_file (str);
439 * gtk_rc_add_default_file:
440 * @filename: the pathname to the file.
442 * Adds a file to the list of files to be parsed at the
446 gtk_rc_add_default_file (const gchar *filename)
450 gtk_rc_add_initial_default_files ();
452 for (n = 0; gtk_rc_default_files[n]; n++) ;
453 if (n >= GTK_RC_MAX_DEFAULT_FILES - 1)
456 gtk_rc_default_files[n++] = g_strdup (filename);
457 gtk_rc_default_files[n] = NULL;
461 * gtk_rc_set_default_files:
462 * @filenames: A %NULL-terminated list of filenames.
464 * Sets the list of files that GTK+ will read at the
468 gtk_rc_set_default_files (gchar **filenames)
472 gtk_rc_add_initial_default_files ();
475 while (gtk_rc_default_files[i])
477 g_free (gtk_rc_default_files[i]);
481 gtk_rc_default_files[0] = NULL;
484 while (filenames[i] != NULL)
486 gtk_rc_add_default_file (filenames[i]);
492 * gtk_rc_get_default_files:
494 * Retrieves the current list of RC files that will be parsed
495 * at the end of gtk_init().
497 * Return value: A %NULL-terminated array of filenames. This memory
498 * is owned by GTK+ and must not be freed by the application.
499 * If you want to store this information, you should make a copy.
502 gtk_rc_get_default_files (void)
504 gtk_rc_add_initial_default_files ();
506 return gtk_rc_default_files;
509 /* The following routine is based on _nl_normalize_codeset from
510 * the GNU C library. Contributed by
512 * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
513 * Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
515 * Normalize codeset name. There is no standard for the codeset
516 * names. Normalization allows the user to use any of the common
520 _gtk_normalize_codeset (const gchar *codeset, gint name_len)
528 for (cnt = 0; cnt < name_len; ++cnt)
529 if (isalnum (codeset[cnt]))
533 if (isalpha (codeset[cnt]))
537 retval = g_malloc ((only_digit ? 3 : 0) + len + 1);
541 memcpy (retval, "iso", 4);
547 for (cnt = 0; cnt < name_len; ++cnt)
548 if (isalpha (codeset[cnt]))
549 *wp++ = isupper(codeset[cnt]) ? tolower (codeset[cnt]) : codeset[cnt];
550 else if (isdigit (codeset[cnt]))
551 *wp++ = codeset[cnt];
559 gtk_rc_settings_changed (GtkSettings *settings,
561 GtkRcContext *context)
563 gchar *new_theme_name;
564 gchar *new_key_theme_name;
566 g_object_get (settings,
567 "gtk-theme-name", &new_theme_name,
568 "gtk-key-theme-name", &new_key_theme_name,
571 if ((new_theme_name != context->theme_name &&
572 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
573 (new_key_theme_name != context->key_theme_name &&
574 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
576 gtk_rc_reparse_all_for_settings (settings, TRUE);
579 g_free (new_theme_name);
580 g_free (new_key_theme_name);
583 static GtkRcContext *
584 gtk_rc_context_get (GtkSettings *settings)
586 if (!settings->rc_context)
588 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
590 context->settings = settings;
591 context->rc_style_ht = NULL;
592 context->rc_sets_widget = NULL;
593 context->rc_sets_widget_class = NULL;
594 context->rc_sets_class = NULL;
595 context->rc_files = NULL;
597 g_object_get (settings,
598 "gtk-theme-name", &context->theme_name,
599 "gtk-key-theme-name", &context->key_theme_name,
602 g_signal_connect (settings,
603 "notify::gtk-theme-name",
604 G_CALLBACK (gtk_rc_settings_changed),
606 g_signal_connect (settings,
607 "notify::gtk-key-theme-name",
608 G_CALLBACK (gtk_rc_settings_changed),
611 context->pixmap_path[0] = NULL;
613 context->default_priority = GTK_PATH_PRIO_RC;
616 return settings->rc_context;
620 gtk_rc_parse_named (GtkRcContext *context,
625 const gchar *home_dir;
629 subpath = g_strconcat ("gtk-2.0-", type,
630 G_DIR_SEPARATOR_S "gtkrc",
633 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
635 /* First look in the users home directory
637 home_dir = g_get_home_dir ();
640 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
641 if (!g_file_test (path, G_FILE_TEST_EXISTS))
650 gchar *theme_dir = gtk_rc_get_theme_dir ();
651 path = g_build_filename (theme_dir, name, subpath, NULL);
654 if (!g_file_test (path, G_FILE_TEST_EXISTS))
663 gtk_rc_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
671 gtk_rc_parse_default_files (GtkRcContext *context)
673 gchar *locale_suffixes[3];
674 gint n_locale_suffixes = 0;
681 locale = g_win32_getlocale ();
683 locale = setlocale (LC_CTYPE, NULL);
686 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
688 /* Determine locale-specific suffixes for RC files
690 * We normalize the charset into a standard form,
691 * which has all '-' and '_' characters removed,
694 gchar *normalized_locale;
696 p = strchr (locale, '@');
697 length = p ? (p - locale) : strlen (locale);
699 p = strchr (locale, '.');
702 gchar *tmp1 = g_strndup (locale, p - locale + 1);
703 gchar *tmp2 = _gtk_normalize_codeset (p + 1, length - (p - locale + 1));
705 normalized_locale = g_strconcat (tmp1, tmp2, NULL);
709 locale_suffixes[n_locale_suffixes++] = g_strdup (normalized_locale);
713 normalized_locale = g_strndup (locale, length);
715 p = strchr (normalized_locale, '_');
718 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
719 length = p - normalized_locale;
722 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
724 g_free (normalized_locale);
727 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
729 /* Try to find a locale specific RC file corresponding to the
730 * current locale to parse before the default file.
732 for (j = n_locale_suffixes - 1; j >= 0; j--)
734 gchar *name = g_strconcat (gtk_rc_default_files[i],
738 gtk_rc_parse_file (context, name, GTK_PATH_PRIO_RC, FALSE);
741 gtk_rc_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
744 for (j = 0; j < n_locale_suffixes; j++)
745 g_free (locale_suffixes[j]);
751 static gboolean initialized = FALSE;
757 module_path[0] = NULL;
758 gtk_rc_append_default_module_path();
760 gtk_rc_add_initial_default_files ();
763 gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), TRUE);
767 gtk_rc_parse_string (const gchar *rc_string)
769 g_return_if_fail (rc_string != NULL);
771 gtk_rc_parse_any (gtk_rc_context_get (gtk_settings_get_default ()),
772 "-", -1, rc_string); /* FIXME */
776 gtk_rc_parse_file (GtkRcContext *context,
777 const gchar *filename,
781 GtkRcFile *rc_file = NULL;
786 g_return_if_fail (filename != NULL);
788 saved_priority = context->default_priority;
789 context->default_priority = priority;
791 tmp_list = context->rc_files;
794 rc_file = tmp_list->data;
795 if (!strcmp (rc_file->name, filename))
798 tmp_list = tmp_list->next;
803 rc_file = g_new (GtkRcFile, 1);
804 rc_file->name = g_strdup (filename);
805 rc_file->canonical_name = NULL;
807 rc_file->reload = reload;
809 context->rc_files = g_slist_append (context->rc_files, rc_file);
812 if (!rc_file->canonical_name)
814 /* Get the absolute pathname */
816 if (g_path_is_absolute (rc_file->name))
817 rc_file->canonical_name = rc_file->name;
822 cwd = g_get_current_dir ();
823 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
828 if (!lstat (rc_file->canonical_name, &statbuf))
833 rc_file->mtime = statbuf.st_mtime;
835 fd = open (rc_file->canonical_name, O_RDONLY);
839 /* Temporarily push directory name for this file on
840 * a stack of directory names while parsing it
843 g_slist_prepend (rc_dir_stack,
844 g_path_get_dirname (rc_file->canonical_name));
845 gtk_rc_parse_any (context, filename, fd, NULL);
847 tmp_list = rc_dir_stack;
848 rc_dir_stack = rc_dir_stack->next;
850 g_free (tmp_list->data);
851 g_slist_free_1 (tmp_list);
857 context->default_priority = saved_priority;
861 gtk_rc_parse (const gchar *filename)
863 g_return_if_fail (filename != NULL);
865 gtk_rc_parse_file (gtk_rc_context_get (gtk_settings_get_default ()),
866 filename, GTK_PATH_PRIO_RC, TRUE); /* FIXME */
869 /* Handling of RC styles */
872 gtk_rc_style_get_type (void)
874 static GType object_type = 0;
878 static const GTypeInfo object_info =
880 sizeof (GtkRcStyleClass),
881 (GBaseInitFunc) NULL,
882 (GBaseFinalizeFunc) NULL,
883 (GClassInitFunc) gtk_rc_style_class_init,
884 NULL, /* class_finalize */
885 NULL, /* class_data */
888 (GInstanceInitFunc) gtk_rc_style_init,
891 object_type = g_type_register_static (G_TYPE_OBJECT,
900 gtk_rc_style_init (GtkRcStyle *style)
905 style->font_desc = NULL;
907 for (i = 0; i < 5; i++)
909 static const GdkColor init_color = { 0, 0, 0, 0, };
911 style->bg_pixmap_name[i] = NULL;
912 style->color_flags[i] = 0;
913 style->fg[i] = init_color;
914 style->bg[i] = init_color;
915 style->text[i] = init_color;
916 style->base[i] = init_color;
918 style->xthickness = -1;
919 style->ythickness = -1;
920 style->rc_properties = NULL;
922 style->rc_style_lists = NULL;
923 style->icon_factories = NULL;
927 gtk_rc_style_class_init (GtkRcStyleClass *klass)
929 GObjectClass *object_class = G_OBJECT_CLASS (klass);
931 parent_class = g_type_class_peek_parent (klass);
933 object_class->finalize = gtk_rc_style_finalize;
936 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
937 klass->merge = gtk_rc_style_real_merge;
938 klass->create_style = gtk_rc_style_real_create_style;
942 gtk_rc_style_finalize (GObject *object)
944 GSList *tmp_list1, *tmp_list2;
945 GtkRcStyle *rc_style;
948 rc_style = GTK_RC_STYLE (object);
951 g_free (rc_style->name);
952 if (rc_style->font_desc)
953 pango_font_description_free (rc_style->font_desc);
955 for (i = 0; i < 5; i++)
956 if (rc_style->bg_pixmap_name[i])
957 g_free (rc_style->bg_pixmap_name[i]);
959 /* Now remove all references to this rc_style from
962 tmp_list1 = rc_style->rc_style_lists;
965 GSList *rc_styles = tmp_list1->data;
966 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
967 gtk_style_unref (style);
969 /* Remove the list of styles from the other rc_styles
972 tmp_list2 = rc_styles;
975 GtkRcStyle *other_style = tmp_list2->data;
977 if (other_style != rc_style)
978 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
980 tmp_list2 = tmp_list2->next;
983 /* And from the hash table itself
985 g_hash_table_remove (realized_style_ht, rc_styles);
986 g_slist_free (rc_styles);
988 tmp_list1 = tmp_list1->next;
990 g_slist_free (rc_style->rc_style_lists);
992 if (rc_style->rc_properties)
996 for (i = 0; i < rc_style->rc_properties->len; i++)
998 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
1000 g_free (node->origin);
1001 g_value_unset (&node->value);
1003 g_array_free (rc_style->rc_properties, TRUE);
1004 rc_style->rc_properties = NULL;
1007 tmp_list1 = rc_style->icon_factories;
1010 g_object_unref (G_OBJECT (tmp_list1->data));
1012 tmp_list1 = tmp_list1->next;
1014 g_slist_free (rc_style->icon_factories);
1016 G_OBJECT_CLASS (parent_class)->finalize (object);
1020 gtk_rc_style_new (void)
1024 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1030 * gtk_rc_style_copy:
1031 * @orig: the style to copy
1033 * Makes a copy of the specified #GtkRcStyle. This function
1034 * will correctly copy an RC style that is a member of a class
1035 * derived from #GtkRcStyle.
1037 * Return value: the resulting #GtkRcStyle
1040 gtk_rc_style_copy (GtkRcStyle *orig)
1044 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1046 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1047 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1053 gtk_rc_style_ref (GtkRcStyle *rc_style)
1055 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1057 g_object_ref (G_OBJECT (rc_style));
1061 gtk_rc_style_unref (GtkRcStyle *rc_style)
1063 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1065 g_object_unref (G_OBJECT (rc_style));
1069 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1071 return GTK_RC_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
1075 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1076 gconstpointer bsearch_node2)
1078 const GtkRcProperty *prop1 = bsearch_node1;
1079 const GtkRcProperty *prop2 = bsearch_node2;
1081 if (prop1->type_name == prop2->type_name)
1082 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1084 return prop1->type_name < prop2->type_name ? -1 : 1;
1088 insert_rc_property (GtkRcStyle *style,
1089 GtkRcProperty *property,
1093 GtkRcProperty *new_property = NULL;
1094 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1096 key.type_name = property->type_name;
1097 key.property_name = property->property_name;
1099 if (!style->rc_properties)
1100 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1103 while (i < style->rc_properties->len)
1105 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1111 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1113 g_free (new_property->origin);
1114 g_value_unset (&new_property->value);
1116 *new_property = key;
1130 g_array_insert_val (style->rc_properties, i, key);
1131 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1134 new_property->origin = g_strdup (property->origin);
1135 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1136 g_value_copy (&property->value, &new_property->value);
1140 gtk_rc_style_real_merge (GtkRcStyle *dest,
1145 for (i = 0; i < 5; i++)
1147 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1148 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1150 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1151 src->color_flags[i] & GTK_RC_FG)
1153 dest->fg[i] = src->fg[i];
1154 dest->color_flags[i] |= GTK_RC_FG;
1156 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1157 src->color_flags[i] & GTK_RC_BG)
1159 dest->bg[i] = src->bg[i];
1160 dest->color_flags[i] |= GTK_RC_BG;
1162 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1163 src->color_flags[i] & GTK_RC_TEXT)
1165 dest->text[i] = src->text[i];
1166 dest->color_flags[i] |= GTK_RC_TEXT;
1168 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1169 src->color_flags[i] & GTK_RC_BASE)
1171 dest->base[i] = src->base[i];
1172 dest->color_flags[i] |= GTK_RC_BASE;
1176 if (dest->xthickness < 0 && src->xthickness >= 0)
1177 dest->xthickness = src->xthickness;
1178 if (dest->ythickness < 0 && src->ythickness >= 0)
1179 dest->ythickness = src->ythickness;
1183 if (!dest->font_desc)
1184 dest->font_desc = pango_font_description_copy (src->font_desc);
1186 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1189 if (src->rc_properties)
1193 for (i = 0; i < src->rc_properties->len; i++)
1194 insert_rc_property (dest,
1195 &g_array_index (src->rc_properties, GtkRcProperty, i),
1201 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1203 return gtk_style_new ();
1207 gtk_rc_clear_hash_node (gpointer key,
1211 gtk_rc_style_unref (data);
1215 gtk_rc_free_rc_sets (GSList *slist)
1221 rc_set = slist->data;
1222 g_pattern_spec_free (rc_set->pspec);
1225 slist = slist->next;
1230 gtk_rc_clear_styles (GtkRcContext *context)
1232 /* Clear out all old rc_styles */
1234 if (context->rc_style_ht)
1236 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1237 g_hash_table_destroy (context->rc_style_ht);
1238 context->rc_style_ht = NULL;
1241 gtk_rc_free_rc_sets (context->rc_sets_widget);
1242 g_slist_free (context->rc_sets_widget);
1243 context->rc_sets_widget = NULL;
1245 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1246 g_slist_free (context->rc_sets_widget_class);
1247 context->rc_sets_widget_class = NULL;
1249 gtk_rc_free_rc_sets (context->rc_sets_class);
1250 g_slist_free (context->rc_sets_class);
1251 context->rc_sets_class = NULL;
1254 /* Reset all our widgets. Also, we have to invalidate cached icons in
1255 * icon sets so they get re-rendered.
1258 gtk_rc_reset_widgets (GtkRcContext *context)
1260 GList *list, *toplevels;
1262 _gtk_icon_set_invalidate_caches ();
1264 toplevels = gtk_window_list_toplevels ();
1265 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1267 for (list = toplevels; list; list = list->next)
1269 gtk_widget_reset_rc_styles (list->data);
1270 gtk_widget_unref (list->data);
1272 g_list_free (toplevels);
1276 * gtk_rc_reparse_all_for_settings:
1277 * @settings: a #GtkSettings
1278 * @force_load: load whether or not anything changed
1280 * If the modification time on any previously read file
1281 * for the given #GtkSettings has changed, discard all style information
1282 * and then reread all previously read RC files.
1284 * Return value: %TRUE if the files were reread.
1287 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1288 gboolean force_load)
1290 gboolean mtime_modified = FALSE;
1293 GtkRcContext *context;
1295 struct stat statbuf;
1297 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1299 context = gtk_rc_context_get (settings);
1303 /* Check through and see if any of the RC's have had their
1304 * mtime modified. If so, reparse everything.
1306 tmp_list = context->rc_files;
1309 rc_file = tmp_list->data;
1311 if (!lstat (rc_file->name, &statbuf) &&
1312 (statbuf.st_mtime > rc_file->mtime))
1314 mtime_modified = TRUE;
1318 tmp_list = tmp_list->next;
1322 if (force_load || mtime_modified)
1326 gtk_rc_clear_styles (context);
1327 g_object_freeze_notify (G_OBJECT (context->settings));
1329 old_files = context->rc_files;
1330 context->rc_files = NULL;
1332 gtk_rc_parse_default_files (context);
1334 tmp_list = old_files;
1337 rc_file = tmp_list->data;
1338 if (rc_file->reload)
1339 gtk_rc_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, TRUE);
1341 if (rc_file->canonical_name != rc_file->name)
1342 g_free (rc_file->canonical_name);
1343 g_free (rc_file->name);
1346 tmp_list = tmp_list->next;
1349 g_slist_free (old_files);;
1351 g_free (context->theme_name);
1352 g_free (context->key_theme_name);
1353 g_object_get (context->settings,
1354 "gtk-theme-name", &context->theme_name,
1355 "gtk-key-theme-name", &context->key_theme_name,
1358 if (context->theme_name && context->theme_name[0])
1359 gtk_rc_parse_named (context, context->theme_name, NULL);
1360 if (context->key_theme_name && context->key_theme_name[0])
1361 gtk_rc_parse_named (context, context->key_theme_name, "key");
1363 g_object_thaw_notify (G_OBJECT (context->settings));
1365 gtk_rc_reset_widgets (context);
1368 return mtime_modified;
1372 * gtk_rc_reparse_all:
1374 * If the modification time on any previously read file for the
1375 * default #GtkSettings has changed, discard all style information
1376 * and then reread all previously read RC files.
1378 * Return value: %TRUE if the files were reread.
1381 gtk_rc_reparse_all (void)
1383 return gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), FALSE);
1387 gtk_rc_styles_match (GSList *rc_styles,
1391 const gchar *path_reversed)
1398 rc_set = sets->data;
1401 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1402 rc_styles = g_slist_append (rc_styles, rc_set->rc_style);
1410 * @widget: a #GtkWidget
1412 * Finds all matching RC styles for a given widget,
1413 * composites them together, and then creates a
1414 * #GtkStyle representing the composite appearance.
1415 * (GTK+ actually keeps a cache of previously
1416 * created styles, so a new style may not be
1419 * Returns: the resulting style. No refcount is added
1420 * to the returned style, so if you want to save this
1421 * style around, you should add a reference yourself.
1424 gtk_rc_get_style (GtkWidget *widget)
1426 GtkRcStyle *widget_rc_style;
1427 GSList *rc_styles = NULL;
1428 GtkRcContext *context;
1430 static guint rc_style_key_id = 0;
1432 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1434 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1436 /* We allow the specification of a single rc style to be bound
1437 * tightly to a widget, for application modifications
1439 if (!rc_style_key_id)
1440 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1442 widget_rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
1445 if (widget_rc_style)
1446 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1448 if (context->rc_sets_widget)
1450 gchar *path, *path_reversed;
1453 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1454 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1456 g_free (path_reversed);
1459 if (context->rc_sets_widget_class)
1461 gchar *path, *path_reversed;
1464 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1465 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1467 g_free (path_reversed);
1470 if (context->rc_sets_class)
1474 type = GTK_OBJECT_TYPE (widget);
1478 gchar *path_reversed;
1481 path = gtk_type_name (type);
1482 path_length = strlen (path);
1483 path_reversed = g_strdup (path);
1484 g_strreverse (path_reversed);
1486 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1487 g_free (path_reversed);
1489 type = gtk_type_parent (type);
1494 return gtk_rc_init_style (rc_styles);
1500 * gtk_rc_get_style_by_paths:
1501 * @settings: a #GtkSettings object
1502 * @widget_path: the widget path to use when looking up the style, or %NULL
1503 * @class_path: the class path to use when looking up the style, or %NULL
1504 * @type: a type that will be used along with parent types of this type
1505 * when matching against class styles, or #G_TYPE_NONE
1507 * Creates up a #GtkStyle from styles defined in a RC file by providing
1508 * the raw components used in matching. This function may be useful
1509 * when creating pseudo-widgets that should be themed like widgets but
1510 * don't actually have corresponding GTK+ widgets. An example of this
1511 * would be items inside a GNOME canvas widget.
1513 * The action of gtk_rc_get_style() is similar to:
1514 * <informalexample><programlisting>
1515 * <!> gtk_widget_path (widget, NULL, &path, NULL);
1516 * <!> gtk_widget_class_path (widget, NULL, &class_path, NULL);
1517 * <!> gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
1518 * <!> G_OBJECT_TYPE (widget));
1519 * </programlisting></informalexample>
1521 * Return value: A style created by matching with the supplied paths,
1522 * or %NULL if nothing matching was specified and the default style should
1523 * be used. The returned value is owned by GTK+ as part of an internal cache,
1524 * so you must call g_object_ref() on the returned value if you want to
1525 * keep a reference to it.
1528 gtk_rc_get_style_by_paths (GtkSettings *settings,
1529 const char *widget_path,
1530 const char *class_path,
1533 /* We duplicate the code from above to avoid slowing down the above
1534 * by generating paths when we don't need them. I don't know if
1535 * this is really worth it.
1537 GSList *rc_styles = NULL;
1538 GtkRcContext *context;
1540 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1542 context = gtk_rc_context_get (settings);
1544 if (context->rc_sets_widget)
1546 gchar *path_reversed;
1549 path_length = strlen (widget_path);
1550 path_reversed = g_strdup (widget_path);
1551 g_strreverse (path_reversed);
1553 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, widget_path, path_reversed);
1554 g_free (path_reversed);
1557 if (context->rc_sets_widget_class)
1559 gchar *path_reversed;
1562 path_length = strlen (class_path);
1563 path_reversed = g_strdup (class_path);
1564 g_strreverse (path_reversed);
1566 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, class_path, path_reversed);
1567 g_free (path_reversed);
1570 if (type != G_TYPE_NONE && context->rc_sets_class)
1575 gchar *path_reversed;
1578 path = g_type_name (type);
1579 path_length = strlen (path);
1580 path_reversed = g_strdup (path);
1581 g_strreverse (path_reversed);
1583 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1584 g_free (path_reversed);
1586 type = g_type_parent (type);
1591 return gtk_rc_init_style (rc_styles);
1597 gtk_rc_add_rc_sets (GSList *slist,
1598 GtkRcStyle *rc_style,
1599 const gchar *pattern)
1601 GtkRcStyle *new_style;
1605 new_style = gtk_rc_style_new ();
1606 *new_style = *rc_style;
1607 new_style->name = g_strdup (rc_style->name);
1608 if (rc_style->font_desc)
1609 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
1611 for (i = 0; i < 5; i++)
1612 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1614 rc_set = g_new (GtkRcSet, 1);
1615 rc_set->pspec = g_pattern_spec_new (pattern);
1616 rc_set->rc_style = rc_style;
1618 return g_slist_prepend (slist, rc_set);
1622 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
1623 const gchar *pattern)
1625 GtkRcContext *context;
1627 g_return_if_fail (rc_style != NULL);
1628 g_return_if_fail (pattern != NULL);
1630 context = gtk_rc_context_get (gtk_settings_get_default ());
1632 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern);
1636 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
1637 const gchar *pattern)
1639 GtkRcContext *context;
1641 g_return_if_fail (rc_style != NULL);
1642 g_return_if_fail (pattern != NULL);
1644 context = gtk_rc_context_get (gtk_settings_get_default ());
1646 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern);
1650 gtk_rc_add_class_style (GtkRcStyle *rc_style,
1651 const gchar *pattern)
1653 GtkRcContext *context;
1655 g_return_if_fail (rc_style != NULL);
1656 g_return_if_fail (pattern != NULL);
1658 context = gtk_rc_context_get (gtk_settings_get_default ());
1660 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern);
1664 gtk_rc_scanner_new (void)
1666 return g_scanner_new (>k_rc_scanner_config);
1670 gtk_rc_parse_any (GtkRcContext *context,
1671 const gchar *input_name,
1673 const gchar *input_string)
1679 scanner = gtk_rc_scanner_new ();
1683 g_assert (input_string == NULL);
1685 g_scanner_input_file (scanner, input_fd);
1689 g_assert (input_string != NULL);
1691 g_scanner_input_text (scanner, input_string, strlen (input_string));
1693 scanner->input_name = input_name;
1695 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1696 g_scanner_scope_add_symbol (scanner, 0, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1701 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1705 guint expected_token;
1707 expected_token = gtk_rc_parse_statement (context, scanner);
1709 if (expected_token != G_TOKEN_NONE)
1716 if (scanner->scope_id == 0)
1718 /* if we are in scope 0, we know the symbol names
1719 * that are associated with certain token values.
1720 * so we look them up to make the error messages
1723 if (expected_token > GTK_RC_TOKEN_INVALID &&
1724 expected_token < GTK_RC_TOKEN_LAST)
1726 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1727 if (symbols[i].token == expected_token)
1728 msg = symbols[i].name;
1730 msg = g_strconcat ("e.g. `", msg, "'", NULL);
1732 if (scanner->token > GTK_RC_TOKEN_INVALID &&
1733 scanner->token < GTK_RC_TOKEN_LAST)
1735 symbol_name = "???";
1736 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1737 if (symbols[i].token == scanner->token)
1738 symbol_name = symbols[i].name;
1741 g_scanner_unexp_token (scanner,
1754 g_scanner_destroy (scanner);
1758 gtk_rc_styles_hash (const GSList *rc_styles)
1765 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
1766 rc_styles = rc_styles->next;
1773 gtk_rc_styles_equal (const GSList *a,
1778 if (a->data != b->data)
1788 gtk_rc_style_hash (const gchar *name)
1794 result += (result << 3) + *name++;
1800 gtk_rc_style_equal (const gchar *a,
1803 return (strcmp (a, b) == 0);
1807 gtk_rc_style_find (GtkRcContext *context,
1810 if (context->rc_style_ht)
1811 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
1817 gtk_rc_style_to_style (GtkRcStyle *rc_style)
1821 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
1823 style->rc_style = rc_style;
1825 gtk_rc_style_ref (rc_style);
1827 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
1832 /* Reuses or frees rc_styles */
1834 gtk_rc_init_style (GSList *rc_styles)
1836 GtkStyle *style = NULL;
1839 g_return_val_if_fail (rc_styles != NULL, NULL);
1841 if (!realized_style_ht)
1842 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
1843 (GEqualFunc) gtk_rc_styles_equal);
1845 style = g_hash_table_lookup (realized_style_ht, rc_styles);
1849 GtkRcStyle *base_style = NULL;
1850 GtkRcStyle *proto_style;
1851 GtkRcStyleClass *proto_style_class;
1853 GType rc_style_type = GTK_TYPE_RC_STYLE;
1855 /* Find the first derived style in the list, and use that to
1856 * create the merged style. If we only have raw GtkRcStyles, use
1857 * the first style to create the merged style.
1859 base_style = rc_styles->data;
1860 tmp_styles = rc_styles;
1863 GtkRcStyle *rc_style = tmp_styles->data;
1865 if (G_OBJECT_TYPE (rc_style) != rc_style_type)
1867 base_style = rc_style;
1871 tmp_styles = tmp_styles->next;
1874 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
1875 proto_style = proto_style_class->create_rc_style (base_style);
1877 tmp_styles = rc_styles;
1880 GtkRcStyle *rc_style = tmp_styles->data;
1883 proto_style_class->merge (proto_style, rc_style);
1885 /* Point from each rc_style to the list of styles */
1886 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
1887 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
1889 factories = g_slist_copy (rc_style->icon_factories);
1895 while (iter != NULL)
1897 g_object_ref (G_OBJECT (iter->data));
1898 iter = g_slist_next (iter);
1901 proto_style->icon_factories = g_slist_concat (proto_style->icon_factories,
1906 tmp_styles = tmp_styles->next;
1909 for (i = 0; i < 5; i++)
1910 if (proto_style->bg_pixmap_name[i] &&
1911 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
1913 g_free (proto_style->bg_pixmap_name[i]);
1914 proto_style->bg_pixmap_name[i] = NULL;
1917 style = gtk_rc_style_to_style (proto_style);
1918 gtk_rc_style_unref (proto_style);
1920 g_hash_table_insert (realized_style_ht, rc_styles, style);
1923 g_slist_free (rc_styles);
1928 /*********************
1929 * Parsing functions *
1930 *********************/
1933 rc_parse_token_or_compound (GScanner *scanner,
1935 GTokenType delimiter)
1937 guint token = g_scanner_get_next_token (scanner);
1939 /* we either scan a single token (skipping comments)
1940 * or a compund statement.
1941 * compunds are enclosed in (), [] or {} braces, we read
1942 * them in via deep recursion.
1949 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
1952 g_string_append_printf (gstring, " %f", scanner->value.v_float);
1954 case G_TOKEN_STRING:
1955 string = g_strescape (scanner->value.v_string, NULL);
1956 g_string_append (gstring, " \"");
1957 g_string_append (gstring, string);
1958 g_string_append_c (gstring, '"');
1961 case G_TOKEN_IDENTIFIER:
1962 g_string_append_c (gstring, ' ');
1963 g_string_append (gstring, scanner->value.v_identifier);
1965 case G_TOKEN_COMMENT_SINGLE:
1966 case G_TOKEN_COMMENT_MULTI:
1967 return rc_parse_token_or_compound (scanner, gstring, delimiter);
1968 case G_TOKEN_LEFT_PAREN:
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_PAREN);
1972 if (token != G_TOKEN_NONE)
1975 case G_TOKEN_LEFT_CURLY:
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_CURLY);
1979 if (token != G_TOKEN_NONE)
1982 case G_TOKEN_LEFT_BRACE:
1983 g_string_append_c (gstring, ' ');
1984 g_string_append_c (gstring, token);
1985 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_BRACE);
1986 if (token != G_TOKEN_NONE)
1990 if (token >= 256 || token < 1)
1991 return delimiter ? delimiter : G_TOKEN_STRING;
1992 g_string_append_c (gstring, ' ');
1993 g_string_append_c (gstring, token);
1994 if (token == delimiter)
1995 return G_TOKEN_NONE;
1999 return G_TOKEN_NONE;
2001 return rc_parse_token_or_compound (scanner, gstring, delimiter);
2005 gtk_rc_parse_assignment (GScanner *scanner,
2006 GtkRcProperty *prop)
2008 gboolean scan_identifier = scanner->config->scan_identifier;
2009 gboolean scan_symbols = scanner->config->scan_symbols;
2010 gboolean identifier_2_string = scanner->config->identifier_2_string;
2011 gboolean char_2_token = scanner->config->char_2_token;
2012 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2013 gboolean numbers_2_int = scanner->config->numbers_2_int;
2014 gboolean negate = FALSE;
2017 /* check that this is an assignment */
2018 if (g_scanner_get_next_token (scanner) != '=')
2021 /* adjust scanner mode */
2022 scanner->config->scan_identifier = TRUE;
2023 scanner->config->scan_symbols = FALSE;
2024 scanner->config->identifier_2_string = FALSE;
2025 scanner->config->char_2_token = TRUE;
2026 scanner->config->scan_identifier_NULL = FALSE;
2027 scanner->config->numbers_2_int = TRUE;
2029 /* record location */
2030 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2032 /* parse optional sign */
2033 if (g_scanner_peek_next_token (scanner) == '-')
2035 g_scanner_get_next_token (scanner); /* eat sign */
2039 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an unparsed compund */
2040 token = g_scanner_peek_next_token (scanner);
2044 g_scanner_get_next_token (scanner);
2045 g_value_init (&prop->value, G_TYPE_LONG);
2046 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2047 token = G_TOKEN_NONE;
2050 g_scanner_get_next_token (scanner);
2051 g_value_init (&prop->value, G_TYPE_DOUBLE);
2052 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2053 token = G_TOKEN_NONE;
2055 case G_TOKEN_STRING:
2056 g_scanner_get_next_token (scanner);
2058 token = G_TOKEN_INT;
2061 g_value_init (&prop->value, G_TYPE_STRING);
2062 g_value_set_string (&prop->value, scanner->value.v_string);
2063 token = G_TOKEN_NONE;
2066 case G_TOKEN_IDENTIFIER:
2067 case G_TOKEN_LEFT_PAREN:
2068 case G_TOKEN_LEFT_CURLY:
2069 case G_TOKEN_LEFT_BRACE:
2072 GString *gstring = g_string_new ("");
2074 token = rc_parse_token_or_compound (scanner, gstring, 0);
2075 if (token == G_TOKEN_NONE)
2077 g_string_append_c (gstring, ' ');
2078 g_value_init (&prop->value, G_TYPE_GSTRING);
2079 g_value_set_static_boxed (&prop->value, gstring);
2082 g_string_free (gstring, TRUE);
2087 g_scanner_get_next_token (scanner);
2088 token = G_TOKEN_INT;
2092 /* restore scanner mode */
2093 scanner->config->scan_identifier = scan_identifier;
2094 scanner->config->scan_symbols = scan_symbols;
2095 scanner->config->identifier_2_string = identifier_2_string;
2096 scanner->config->char_2_token = char_2_token;
2097 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2098 scanner->config->numbers_2_int = numbers_2_int;
2104 is_c_identifier (const gchar *string)
2107 gboolean is_varname;
2109 is_varname = strchr (G_CSET_a_2_z G_CSET_A_2_Z "_", string[0]) != NULL;
2110 for (p = string + 1; *p && is_varname; p++)
2111 is_varname &= strchr (G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_-", *p) != NULL;
2117 gtk_rc_parse_statement (GtkRcContext *context,
2122 token = g_scanner_peek_next_token (scanner);
2125 case GTK_RC_TOKEN_INCLUDE:
2126 token = g_scanner_get_next_token (scanner);
2127 if (token != GTK_RC_TOKEN_INCLUDE)
2128 return GTK_RC_TOKEN_INCLUDE;
2129 token = g_scanner_get_next_token (scanner);
2130 if (token != G_TOKEN_STRING)
2131 return G_TOKEN_STRING;
2132 gtk_rc_parse_file (context, scanner->value.v_string, context->default_priority, FALSE);
2133 return G_TOKEN_NONE;
2135 case GTK_RC_TOKEN_STYLE:
2136 return gtk_rc_parse_style (context, scanner);
2138 case GTK_RC_TOKEN_BINDING:
2139 return gtk_binding_parse_binding (scanner);
2141 case GTK_RC_TOKEN_PIXMAP_PATH:
2142 return gtk_rc_parse_pixmap_path (context, scanner);
2144 case GTK_RC_TOKEN_WIDGET:
2145 return gtk_rc_parse_path_pattern (context, scanner);
2147 case GTK_RC_TOKEN_WIDGET_CLASS:
2148 return gtk_rc_parse_path_pattern (context, scanner);
2150 case GTK_RC_TOKEN_CLASS:
2151 return gtk_rc_parse_path_pattern (context, scanner);
2153 case GTK_RC_TOKEN_MODULE_PATH:
2154 return gtk_rc_parse_module_path (scanner);
2156 case GTK_RC_TOKEN_IM_MODULE_PATH:
2157 return gtk_rc_parse_im_module_path (scanner);
2159 case GTK_RC_TOKEN_IM_MODULE_FILE:
2160 return gtk_rc_parse_im_module_file (scanner);
2162 case G_TOKEN_IDENTIFIER:
2163 if (is_c_identifier (scanner->next_value.v_identifier))
2165 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2168 g_scanner_get_next_token (scanner); /* eat identifier */
2169 name = g_strdup (scanner->value.v_identifier);
2171 token = gtk_rc_parse_assignment (scanner, &prop);
2172 if (token == G_TOKEN_NONE)
2174 GtkSettingsValue svalue;
2176 svalue.origin = prop.origin;
2177 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2178 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2179 gtk_settings_set_property_value (context->settings,
2183 g_free (prop.origin);
2184 if (G_VALUE_TYPE (&prop.value))
2185 g_value_unset (&prop.value);
2192 g_scanner_get_next_token (scanner);
2193 return G_TOKEN_IDENTIFIER;
2196 g_scanner_get_next_token (scanner);
2197 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2202 gtk_rc_parse_style (GtkRcContext *context,
2205 GtkRcStyle *rc_style;
2206 GtkRcStyle *parent_style;
2210 GtkIconFactory *our_factory = NULL;
2212 token = g_scanner_get_next_token (scanner);
2213 if (token != GTK_RC_TOKEN_STYLE)
2214 return GTK_RC_TOKEN_STYLE;
2216 token = g_scanner_get_next_token (scanner);
2217 if (token != G_TOKEN_STRING)
2218 return G_TOKEN_STRING;
2221 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2223 /* If there's a list, its first member is always the factory belonging
2226 if (rc_style && rc_style->icon_factories)
2227 our_factory = rc_style->icon_factories->data;
2232 rc_style = gtk_rc_style_new ();
2233 rc_style->name = g_strdup (scanner->value.v_string);
2235 for (i = 0; i < 5; i++)
2236 rc_style->bg_pixmap_name[i] = NULL;
2238 for (i = 0; i < 5; i++)
2239 rc_style->color_flags[i] = 0;
2242 token = g_scanner_peek_next_token (scanner);
2243 if (token == G_TOKEN_EQUAL_SIGN)
2245 token = g_scanner_get_next_token (scanner);
2247 token = g_scanner_get_next_token (scanner);
2248 if (token != G_TOKEN_STRING)
2253 return G_TOKEN_STRING;
2256 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2261 for (i = 0; i < 5; i++)
2263 rc_style->color_flags[i] = parent_style->color_flags[i];
2264 rc_style->fg[i] = parent_style->fg[i];
2265 rc_style->bg[i] = parent_style->bg[i];
2266 rc_style->text[i] = parent_style->text[i];
2267 rc_style->base[i] = parent_style->base[i];
2270 rc_style->xthickness = parent_style->xthickness;
2271 rc_style->ythickness = parent_style->ythickness;
2273 if (parent_style->font_desc)
2275 if (rc_style->font_desc)
2276 pango_font_description_free (rc_style->font_desc);
2277 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
2280 if (parent_style->rc_properties)
2284 for (i = 0; i < parent_style->rc_properties->len; i++)
2285 insert_rc_property (rc_style,
2286 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
2290 for (i = 0; i < 5; i++)
2292 if (rc_style->bg_pixmap_name[i])
2293 g_free (rc_style->bg_pixmap_name[i]);
2294 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
2297 /* Append parent's factories, adding a ref to them */
2298 if (parent_style->icon_factories != NULL)
2300 /* Add a factory for ourselves if we have none,
2301 * in case we end up defining more stock icons.
2302 * I see no real way around this; we need to maintain
2303 * the invariant that the first factory in the list
2304 * is always our_factory, the one belonging to us,
2305 * and if we put parent factories in the list we can't
2306 * do that if the style is reopened.
2308 if (our_factory == NULL)
2310 our_factory = gtk_icon_factory_new ();
2311 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2315 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories,
2316 g_slist_copy (parent_style->icon_factories));
2318 factories = parent_style->icon_factories;
2319 while (factories != NULL)
2321 g_object_ref (G_OBJECT (factories->data));
2322 factories = factories->next;
2328 token = g_scanner_get_next_token (scanner);
2329 if (token != G_TOKEN_LEFT_CURLY)
2334 return G_TOKEN_LEFT_CURLY;
2337 token = g_scanner_peek_next_token (scanner);
2338 while (token != G_TOKEN_RIGHT_CURLY)
2342 case GTK_RC_TOKEN_BG:
2343 token = gtk_rc_parse_bg (scanner, rc_style);
2345 case GTK_RC_TOKEN_FG:
2346 token = gtk_rc_parse_fg (scanner, rc_style);
2348 case GTK_RC_TOKEN_TEXT:
2349 token = gtk_rc_parse_text (scanner, rc_style);
2351 case GTK_RC_TOKEN_BASE:
2352 token = gtk_rc_parse_base (scanner, rc_style);
2354 case GTK_RC_TOKEN_XTHICKNESS:
2355 token = gtk_rc_parse_xthickness (scanner, rc_style);
2357 case GTK_RC_TOKEN_YTHICKNESS:
2358 token = gtk_rc_parse_ythickness (scanner, rc_style);
2360 case GTK_RC_TOKEN_BG_PIXMAP:
2361 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
2363 case GTK_RC_TOKEN_FONT:
2364 token = gtk_rc_parse_font (scanner, rc_style);
2366 case GTK_RC_TOKEN_FONTSET:
2367 token = gtk_rc_parse_fontset (scanner, rc_style);
2369 case GTK_RC_TOKEN_FONT_NAME:
2370 token = gtk_rc_parse_font_name (scanner, rc_style);
2372 case GTK_RC_TOKEN_ENGINE:
2373 token = gtk_rc_parse_engine (context, scanner, &rc_style);
2375 case GTK_RC_TOKEN_STOCK:
2376 if (our_factory == NULL)
2378 our_factory = gtk_icon_factory_new ();
2379 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2382 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
2384 case G_TOKEN_IDENTIFIER:
2385 if (is_c_identifier (scanner->next_value.v_identifier) &&
2386 scanner->next_value.v_identifier[0] >= 'A' &&
2387 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
2389 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2391 g_scanner_get_next_token (scanner); /* eat type name */
2392 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
2393 if (g_scanner_get_next_token (scanner) != ':' ||
2394 g_scanner_get_next_token (scanner) != ':')
2399 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
2400 !is_c_identifier (scanner->value.v_identifier))
2402 token = G_TOKEN_IDENTIFIER;
2406 /* it's important that we do the same canonification as GParamSpecPool here */
2407 g_strcanon (scanner->value.v_identifier, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2408 prop.property_name = g_quark_from_string (scanner->value.v_identifier);
2410 token = gtk_rc_parse_assignment (scanner, &prop);
2411 if (token == G_TOKEN_NONE)
2413 g_return_val_if_fail (prop.origin != NULL && G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
2414 insert_rc_property (rc_style, &prop, TRUE);
2417 g_free (prop.origin);
2418 if (G_VALUE_TYPE (&prop.value))
2419 g_value_unset (&prop.value);
2423 g_scanner_get_next_token (scanner);
2424 token = G_TOKEN_IDENTIFIER;
2428 g_scanner_get_next_token (scanner);
2429 token = G_TOKEN_RIGHT_CURLY;
2433 if (token != G_TOKEN_NONE)
2436 gtk_rc_style_unref (rc_style);
2440 token = g_scanner_peek_next_token (scanner);
2441 } /* while (token != G_TOKEN_RIGHT_CURLY) */
2443 token = g_scanner_get_next_token (scanner);
2444 if (token != G_TOKEN_RIGHT_CURLY)
2448 if (rc_style->font_desc)
2449 pango_font_description_free (rc_style->font_desc);
2451 for (i = 0; i < 5; i++)
2452 if (rc_style->bg_pixmap_name[i])
2453 g_free (rc_style->bg_pixmap_name[i]);
2457 return G_TOKEN_RIGHT_CURLY;
2462 if (!context->rc_style_ht)
2463 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
2464 (GEqualFunc) gtk_rc_style_equal);
2466 g_hash_table_insert (context->rc_style_ht, rc_style->name, rc_style);
2469 return G_TOKEN_NONE;
2472 const GtkRcProperty*
2473 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
2475 GQuark property_name)
2477 GtkRcProperty *node = NULL;
2479 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
2481 if (rc_style->rc_properties)
2485 key.type_name = type_name;
2486 key.property_name = property_name;
2488 node = bsearch (&key,
2489 rc_style->rc_properties->data, rc_style->rc_properties->len,
2490 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
2497 gtk_rc_parse_bg (GScanner *scanner,
2503 token = g_scanner_get_next_token (scanner);
2504 if (token != GTK_RC_TOKEN_BG)
2505 return GTK_RC_TOKEN_BG;
2507 token = gtk_rc_parse_state (scanner, &state);
2508 if (token != G_TOKEN_NONE)
2511 token = g_scanner_get_next_token (scanner);
2512 if (token != G_TOKEN_EQUAL_SIGN)
2513 return G_TOKEN_EQUAL_SIGN;
2515 style->color_flags[state] |= GTK_RC_BG;
2516 return gtk_rc_parse_color (scanner, &style->bg[state]);
2520 gtk_rc_parse_fg (GScanner *scanner,
2526 token = g_scanner_get_next_token (scanner);
2527 if (token != GTK_RC_TOKEN_FG)
2528 return GTK_RC_TOKEN_FG;
2530 token = gtk_rc_parse_state (scanner, &state);
2531 if (token != G_TOKEN_NONE)
2534 token = g_scanner_get_next_token (scanner);
2535 if (token != G_TOKEN_EQUAL_SIGN)
2536 return G_TOKEN_EQUAL_SIGN;
2538 style->color_flags[state] |= GTK_RC_FG;
2539 return gtk_rc_parse_color (scanner, &style->fg[state]);
2543 gtk_rc_parse_text (GScanner *scanner,
2549 token = g_scanner_get_next_token (scanner);
2550 if (token != GTK_RC_TOKEN_TEXT)
2551 return GTK_RC_TOKEN_TEXT;
2553 token = gtk_rc_parse_state (scanner, &state);
2554 if (token != G_TOKEN_NONE)
2557 token = g_scanner_get_next_token (scanner);
2558 if (token != G_TOKEN_EQUAL_SIGN)
2559 return G_TOKEN_EQUAL_SIGN;
2561 style->color_flags[state] |= GTK_RC_TEXT;
2562 return gtk_rc_parse_color (scanner, &style->text[state]);
2566 gtk_rc_parse_base (GScanner *scanner,
2572 token = g_scanner_get_next_token (scanner);
2573 if (token != GTK_RC_TOKEN_BASE)
2574 return GTK_RC_TOKEN_BASE;
2576 token = gtk_rc_parse_state (scanner, &state);
2577 if (token != G_TOKEN_NONE)
2580 token = g_scanner_get_next_token (scanner);
2581 if (token != G_TOKEN_EQUAL_SIGN)
2582 return G_TOKEN_EQUAL_SIGN;
2584 style->color_flags[state] |= GTK_RC_BASE;
2585 return gtk_rc_parse_color (scanner, &style->base[state]);
2589 gtk_rc_parse_xthickness (GScanner *scanner,
2592 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
2593 return GTK_RC_TOKEN_XTHICKNESS;
2595 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2596 return G_TOKEN_EQUAL_SIGN;
2598 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2601 style->xthickness = scanner->value.v_int;
2603 return G_TOKEN_NONE;
2607 gtk_rc_parse_ythickness (GScanner *scanner,
2610 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
2611 return GTK_RC_TOKEN_YTHICKNESS;
2613 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2614 return G_TOKEN_EQUAL_SIGN;
2616 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2619 style->ythickness = scanner->value.v_int;
2621 return G_TOKEN_NONE;
2625 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
2627 GtkRcStyle *rc_style)
2633 token = g_scanner_get_next_token (scanner);
2634 if (token != GTK_RC_TOKEN_BG_PIXMAP)
2635 return GTK_RC_TOKEN_BG_PIXMAP;
2637 token = gtk_rc_parse_state (scanner, &state);
2638 if (token != G_TOKEN_NONE)
2641 token = g_scanner_get_next_token (scanner);
2642 if (token != G_TOKEN_EQUAL_SIGN)
2643 return G_TOKEN_EQUAL_SIGN;
2645 token = g_scanner_get_next_token (scanner);
2646 if (token != G_TOKEN_STRING)
2647 return G_TOKEN_STRING;
2649 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
2650 (strcmp (scanner->value.v_string, "<none>") == 0))
2651 pixmap_file = g_strdup (scanner->value.v_string);
2653 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
2654 scanner, scanner->value.v_string);
2658 if (rc_style->bg_pixmap_name[state])
2659 g_free (rc_style->bg_pixmap_name[state]);
2660 rc_style->bg_pixmap_name[state] = pixmap_file;
2663 return G_TOKEN_NONE;
2667 gtk_rc_check_pixmap_dir (const gchar *dir, const gchar *pixmap_file)
2672 buf = g_build_filename (dir, pixmap_file, NULL);
2674 fd = open (buf, O_RDONLY);
2687 * gtk_rc_find_pixmap_in_path:
2688 * @settings: a #GtkSettings
2689 * @scanner: Scanner used to get line number information for the
2690 * warning message, or %NULL
2691 * @pixmap_file: name of the pixmap file to locate.
2693 * Looks up a file in pixmap path for the specified #GtkSettings.
2694 * If the file is not found, it outputs a warning message using
2695 * g_warning() and returns %NULL.
2697 * Return value: the filename.
2700 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
2702 const gchar *pixmap_file)
2708 GtkRcContext *context = gtk_rc_context_get (settings);
2710 for (i = 0; (i < GTK_RC_MAX_PIXMAP_PATHS) && (context->pixmap_path[i] != NULL); i++)
2712 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
2717 tmp_list = rc_dir_stack;
2720 filename = gtk_rc_check_pixmap_dir (tmp_list->data, pixmap_file);
2724 tmp_list = tmp_list->next;
2728 g_warning (_("Unable to locate image file in pixmap_path: \"%s\" line %d"),
2729 pixmap_file, scanner->line);
2731 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
2738 gtk_rc_find_module_in_path (const gchar *module_file)
2744 for (i = 0; (i < GTK_RC_MAX_MODULE_PATHS) && (module_path[i] != NULL); i++)
2746 buf = g_build_filename (module_path[i], module_file, NULL);
2748 fd = open (buf, O_RDONLY);
2762 gtk_rc_parse_font (GScanner *scanner,
2763 GtkRcStyle *rc_style)
2767 token = g_scanner_get_next_token (scanner);
2768 if (token != GTK_RC_TOKEN_FONT)
2769 return GTK_RC_TOKEN_FONT;
2771 token = g_scanner_get_next_token (scanner);
2772 if (token != G_TOKEN_EQUAL_SIGN)
2773 return G_TOKEN_EQUAL_SIGN;
2775 token = g_scanner_get_next_token (scanner);
2776 if (token != G_TOKEN_STRING)
2777 return G_TOKEN_STRING;
2779 /* Ignore, do nothing */
2781 return G_TOKEN_NONE;
2785 gtk_rc_parse_fontset (GScanner *scanner,
2786 GtkRcStyle *rc_style)
2790 token = g_scanner_get_next_token (scanner);
2791 if (token != GTK_RC_TOKEN_FONTSET)
2792 return GTK_RC_TOKEN_FONTSET;
2794 token = g_scanner_get_next_token (scanner);
2795 if (token != G_TOKEN_EQUAL_SIGN)
2796 return G_TOKEN_EQUAL_SIGN;
2798 token = g_scanner_get_next_token (scanner);
2799 if (token != G_TOKEN_STRING)
2800 return G_TOKEN_STRING;
2802 /* Do nothing - silently ignore */
2804 return G_TOKEN_NONE;
2808 gtk_rc_parse_font_name (GScanner *scanner,
2809 GtkRcStyle *rc_style)
2813 token = g_scanner_get_next_token (scanner);
2814 if (token != GTK_RC_TOKEN_FONT_NAME)
2815 return GTK_RC_TOKEN_FONT;
2817 token = g_scanner_get_next_token (scanner);
2818 if (token != G_TOKEN_EQUAL_SIGN)
2819 return G_TOKEN_EQUAL_SIGN;
2821 token = g_scanner_get_next_token (scanner);
2822 if (token != G_TOKEN_STRING)
2823 return G_TOKEN_STRING;
2825 rc_style->font_desc = pango_font_description_from_string (scanner->value.v_string);
2827 return G_TOKEN_NONE;
2831 gtk_rc_parse_engine (GtkRcContext *context,
2833 GtkRcStyle **rc_style)
2836 GtkThemeEngine *engine;
2837 guint result = G_TOKEN_NONE;
2838 GtkRcStyle *new_style = NULL;
2839 gboolean parsed_curlies = FALSE;
2841 token = g_scanner_get_next_token (scanner);
2842 if (token != GTK_RC_TOKEN_ENGINE)
2843 return GTK_RC_TOKEN_ENGINE;
2845 token = g_scanner_get_next_token (scanner);
2846 if (token != G_TOKEN_STRING)
2847 return G_TOKEN_STRING;
2849 engine = gtk_theme_engine_get (scanner->value.v_string);
2851 token = g_scanner_get_next_token (scanner);
2852 if (token != G_TOKEN_LEFT_CURLY)
2853 return G_TOKEN_LEFT_CURLY;
2857 GtkRcStyleClass *new_class;
2859 new_style = gtk_theme_engine_create_rc_style (engine);
2860 g_type_module_unuse (G_TYPE_MODULE (engine));
2862 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
2864 new_class->merge (new_style, *rc_style);
2865 if ((*rc_style)->name)
2866 new_style->name = g_strdup ((*rc_style)->name);
2868 if (new_class->parse)
2870 parsed_curlies = TRUE;
2871 result = new_class->parse (new_style, context->settings, scanner);
2873 if (result != G_TOKEN_NONE)
2875 g_object_unref (G_OBJECT (new_style));
2881 if (!parsed_curlies)
2883 /* Skip over remainder, looking for nested {}'s
2887 result = G_TOKEN_RIGHT_CURLY;
2888 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
2890 if (token == G_TOKEN_LEFT_CURLY)
2892 else if (token == G_TOKEN_RIGHT_CURLY)
2897 result = G_TOKEN_NONE;
2905 g_object_unref (G_OBJECT (*rc_style));
2906 *rc_style = new_style;
2913 gtk_rc_parse_state (GScanner *scanner,
2914 GtkStateType *state)
2919 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2920 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
2922 /* we don't know where we got called from, so we reset the scope here.
2923 * if we bail out due to errors, we *don't* reset the scope, so the
2924 * error messaging code can make sense of our tokens.
2926 old_scope = g_scanner_set_scope (scanner, 0);
2928 token = g_scanner_get_next_token (scanner);
2929 if (token != G_TOKEN_LEFT_BRACE)
2930 return G_TOKEN_LEFT_BRACE;
2932 token = g_scanner_get_next_token (scanner);
2935 case GTK_RC_TOKEN_ACTIVE:
2936 *state = GTK_STATE_ACTIVE;
2938 case GTK_RC_TOKEN_INSENSITIVE:
2939 *state = GTK_STATE_INSENSITIVE;
2941 case GTK_RC_TOKEN_NORMAL:
2942 *state = GTK_STATE_NORMAL;
2944 case GTK_RC_TOKEN_PRELIGHT:
2945 *state = GTK_STATE_PRELIGHT;
2947 case GTK_RC_TOKEN_SELECTED:
2948 *state = GTK_STATE_SELECTED;
2951 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
2954 token = g_scanner_get_next_token (scanner);
2955 if (token != G_TOKEN_RIGHT_BRACE)
2956 return G_TOKEN_RIGHT_BRACE;
2958 g_scanner_set_scope (scanner, old_scope);
2960 return G_TOKEN_NONE;
2964 gtk_rc_parse_priority (GScanner *scanner,
2965 GtkPathPriorityType *priority)
2970 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2971 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
2973 /* we don't know where we got called from, so we reset the scope here.
2974 * if we bail out due to errors, we *don't* reset the scope, so the
2975 * error messaging code can make sense of our tokens.
2977 old_scope = g_scanner_set_scope (scanner, 0);
2979 token = g_scanner_get_next_token (scanner);
2983 token = g_scanner_get_next_token (scanner);
2986 case GTK_RC_TOKEN_LOWEST:
2987 *priority = GTK_PATH_PRIO_LOWEST;
2989 case GTK_RC_TOKEN_GTK:
2990 *priority = GTK_PATH_PRIO_GTK;
2992 case GTK_RC_TOKEN_APPLICATION:
2993 *priority = GTK_PATH_PRIO_APPLICATION;
2995 case GTK_RC_TOKEN_THEME:
2996 *priority = GTK_PATH_PRIO_THEME;
2998 case GTK_RC_TOKEN_RC:
2999 *priority = GTK_PATH_PRIO_RC;
3001 case GTK_RC_TOKEN_HIGHEST:
3002 *priority = GTK_PATH_PRIO_HIGHEST;
3005 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3008 g_scanner_set_scope (scanner, old_scope);
3010 return G_TOKEN_NONE;
3014 gtk_rc_parse_color (GScanner *scanner,
3019 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3021 /* we don't need to set our own scope here, because
3022 * we don't need own symbols
3025 token = g_scanner_get_next_token (scanner);
3030 case G_TOKEN_LEFT_CURLY:
3031 token = g_scanner_get_next_token (scanner);
3032 if (token == G_TOKEN_INT)
3033 token_int = scanner->value.v_int;
3034 else if (token == G_TOKEN_FLOAT)
3035 token_int = scanner->value.v_float * 65535.0;
3037 return G_TOKEN_FLOAT;
3038 color->red = CLAMP (token_int, 0, 65535);
3040 token = g_scanner_get_next_token (scanner);
3041 if (token != G_TOKEN_COMMA)
3042 return G_TOKEN_COMMA;
3044 token = g_scanner_get_next_token (scanner);
3045 if (token == G_TOKEN_INT)
3046 token_int = scanner->value.v_int;
3047 else if (token == G_TOKEN_FLOAT)
3048 token_int = scanner->value.v_float * 65535.0;
3050 return G_TOKEN_FLOAT;
3051 color->green = CLAMP (token_int, 0, 65535);
3053 token = g_scanner_get_next_token (scanner);
3054 if (token != G_TOKEN_COMMA)
3055 return G_TOKEN_COMMA;
3057 token = g_scanner_get_next_token (scanner);
3058 if (token == G_TOKEN_INT)
3059 token_int = scanner->value.v_int;
3060 else if (token == G_TOKEN_FLOAT)
3061 token_int = scanner->value.v_float * 65535.0;
3063 return G_TOKEN_FLOAT;
3064 color->blue = CLAMP (token_int, 0, 65535);
3066 token = g_scanner_get_next_token (scanner);
3067 if (token != G_TOKEN_RIGHT_CURLY)
3068 return G_TOKEN_RIGHT_CURLY;
3069 return G_TOKEN_NONE;
3071 case G_TOKEN_STRING:
3072 if (!gdk_color_parse (scanner->value.v_string, color))
3074 g_scanner_warn (scanner, "Invalid color constant '%s'",
3075 scanner->value.v_string);
3076 return G_TOKEN_STRING;
3079 return G_TOKEN_NONE;
3082 return G_TOKEN_STRING;
3087 gtk_rc_parse_pixmap_path (GtkRcContext *context,
3092 token = g_scanner_get_next_token (scanner);
3093 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
3094 return GTK_RC_TOKEN_PIXMAP_PATH;
3096 token = g_scanner_get_next_token (scanner);
3097 if (token != G_TOKEN_STRING)
3098 return G_TOKEN_STRING;
3100 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
3102 return G_TOKEN_NONE;
3106 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
3108 const gchar *pix_path)
3111 gint start_offset = 0;
3115 /* free the old one, or just add to the old one ? */
3116 for (path_num = 0; context->pixmap_path[path_num]; path_num++)
3118 g_free (context->pixmap_path[path_num]);
3119 context->pixmap_path[path_num] = NULL;
3124 path_len = strlen (pix_path);
3126 for (end_offset = 0; end_offset <= path_len; end_offset++)
3128 if ((pix_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3129 (end_offset == path_len))
3131 gchar *path_element = g_strndup (pix_path + start_offset, end_offset - start_offset);
3132 if (g_path_is_absolute (path_element))
3134 context->pixmap_path[path_num] = path_element;
3136 context->pixmap_path[path_num] = NULL;
3140 g_warning (_("Pixmap path element: \"%s\" must be absolute, %s, line %d"),
3141 path_element, scanner->input_name, scanner->line);
3142 g_free (path_element);
3145 start_offset = end_offset + 1;
3151 gtk_rc_parse_module_path (GScanner *scanner)
3155 token = g_scanner_get_next_token (scanner);
3156 if (token != GTK_RC_TOKEN_MODULE_PATH)
3157 return GTK_RC_TOKEN_MODULE_PATH;
3159 token = g_scanner_get_next_token (scanner);
3160 if (token != G_TOKEN_STRING)
3161 return G_TOKEN_STRING;
3163 gtk_rc_parse_module_path_string (scanner->value.v_string);
3165 return G_TOKEN_NONE;
3169 gtk_rc_parse_im_module_path (GScanner *scanner)
3173 token = g_scanner_get_next_token (scanner);
3174 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3175 return GTK_RC_TOKEN_IM_MODULE_FILE;
3177 token = g_scanner_get_next_token (scanner);
3178 if (token != G_TOKEN_STRING)
3179 return G_TOKEN_STRING;
3182 g_free (im_module_path);
3184 im_module_path = g_strdup (scanner->value.v_string);
3186 return G_TOKEN_NONE;
3190 gtk_rc_parse_im_module_file (GScanner *scanner)
3194 token = g_scanner_get_next_token (scanner);
3195 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3196 return GTK_RC_TOKEN_IM_MODULE_FILE;
3198 token = g_scanner_get_next_token (scanner);
3199 if (token != G_TOKEN_STRING)
3200 return G_TOKEN_STRING;
3203 g_free (im_module_file);
3205 im_module_file = g_strdup (scanner->value.v_string);
3207 return G_TOKEN_NONE;
3211 gtk_rc_parse_module_path_string (const gchar *mod_path)
3214 gint start_offset = 0;
3218 /* free the old one, or just add to the old one ? */
3219 for (path_num=0; module_path[path_num]; path_num++)
3221 g_free (module_path[path_num]);
3222 module_path[path_num] = NULL;
3227 path_len = strlen (mod_path);
3229 for (end_offset = 0; end_offset <= path_len; end_offset++)
3231 if ((mod_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3232 (end_offset == path_len))
3234 module_path[path_num] = g_strndup (mod_path + start_offset, end_offset - start_offset);
3236 module_path[path_num] = NULL;
3237 start_offset = end_offset + 1;
3240 gtk_rc_append_default_module_path();
3244 rc_set_compare (gconstpointer a, gconstpointer b)
3246 const GtkRcSet *set_a = a;
3247 const GtkRcSet *set_b = b;
3249 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
3253 insert_rc_set (GSList *list, GtkRcSet *set)
3255 return g_slist_insert_sorted (list, set, rc_set_compare);
3259 gtk_rc_parse_path_pattern (GtkRcContext *context,
3263 GtkPathType path_type;
3265 gboolean is_binding;
3266 GtkPathPriorityType priority = context->default_priority;
3268 token = g_scanner_get_next_token (scanner);
3271 case GTK_RC_TOKEN_WIDGET:
3272 path_type = GTK_PATH_WIDGET;
3274 case GTK_RC_TOKEN_WIDGET_CLASS:
3275 path_type = GTK_PATH_WIDGET_CLASS;
3277 case GTK_RC_TOKEN_CLASS:
3278 path_type = GTK_PATH_CLASS;
3281 return GTK_RC_TOKEN_WIDGET_CLASS;
3284 token = g_scanner_get_next_token (scanner);
3285 if (token != G_TOKEN_STRING)
3286 return G_TOKEN_STRING;
3288 pattern = g_strdup (scanner->value.v_string);
3290 token = g_scanner_get_next_token (scanner);
3291 if (token == GTK_RC_TOKEN_STYLE)
3293 else if (token == GTK_RC_TOKEN_BINDING)
3298 return GTK_RC_TOKEN_STYLE;
3301 if (g_scanner_peek_next_token (scanner) == ':')
3303 token = gtk_rc_parse_priority (scanner, &priority);
3304 if (token != G_TOKEN_NONE)
3311 token = g_scanner_get_next_token (scanner);
3312 if (token != G_TOKEN_STRING)
3315 return G_TOKEN_STRING;
3320 GtkBindingSet *binding;
3322 binding = gtk_binding_set_find (scanner->value.v_string);
3326 return G_TOKEN_STRING;
3328 gtk_binding_set_add_path (binding, path_type, pattern, priority);
3332 GtkRcStyle *rc_style;
3335 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3340 return G_TOKEN_STRING;
3343 rc_set = g_new (GtkRcSet, 1);
3344 rc_set->pspec = g_pattern_spec_new (pattern);
3345 rc_set->rc_style = rc_style;
3346 rc_set->priority = priority;
3348 if (path_type == GTK_PATH_WIDGET)
3349 context->rc_sets_widget = insert_rc_set (context->rc_sets_widget, rc_set);
3350 else if (path_type == GTK_PATH_WIDGET_CLASS)
3351 context->rc_sets_widget_class = insert_rc_set (context->rc_sets_widget_class, rc_set);
3353 context->rc_sets_class = insert_rc_set (context->rc_sets_class, rc_set);
3357 return G_TOKEN_NONE;
3361 gtk_rc_parse_stock_id (GScanner *scanner,
3366 token = g_scanner_get_next_token (scanner);
3367 if (token != G_TOKEN_LEFT_BRACE)
3368 return G_TOKEN_LEFT_BRACE;
3370 token = g_scanner_get_next_token (scanner);
3372 if (token != G_TOKEN_STRING)
3373 return G_TOKEN_STRING;
3375 *stock_id = g_strdup (scanner->value.v_string);
3377 token = g_scanner_get_next_token (scanner);
3378 if (token != G_TOKEN_RIGHT_BRACE)
3381 return G_TOKEN_RIGHT_BRACE;
3384 return G_TOKEN_NONE;
3388 gtk_rc_parse_icon_source (GtkRcContext *context,
3390 GtkIconSet *icon_set)
3393 GtkIconSource *source;
3394 gchar *full_filename;
3396 token = g_scanner_get_next_token (scanner);
3397 if (token != G_TOKEN_LEFT_CURLY)
3398 return G_TOKEN_LEFT_CURLY;
3400 token = g_scanner_get_next_token (scanner);
3402 if (token != G_TOKEN_STRING)
3403 return G_TOKEN_STRING;
3406 source = gtk_icon_source_new ();
3408 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
3411 gtk_icon_source_set_filename (source, full_filename);
3412 g_free (full_filename);
3415 /* We continue parsing even if we didn't find the pixmap so that rest of the
3416 * file is read, even if the syntax is bad
3418 token = g_scanner_get_next_token (scanner);
3420 if (token == G_TOKEN_RIGHT_CURLY)
3422 else if (token != G_TOKEN_COMMA)
3424 gtk_icon_source_free (source);
3425 return G_TOKEN_COMMA;
3428 /* Get the direction */
3430 token = g_scanner_get_next_token (scanner);
3434 case GTK_RC_TOKEN_RTL:
3435 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3436 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
3439 case GTK_RC_TOKEN_LTR:
3440 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3441 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
3448 gtk_icon_source_free (source);
3449 return GTK_RC_TOKEN_RTL;
3453 token = g_scanner_get_next_token (scanner);
3455 if (token == G_TOKEN_RIGHT_CURLY)
3457 else if (token != G_TOKEN_COMMA)
3459 gtk_icon_source_free (source);
3460 return G_TOKEN_COMMA;
3465 token = g_scanner_get_next_token (scanner);
3469 case GTK_RC_TOKEN_NORMAL:
3470 gtk_icon_source_set_state_wildcarded (source, FALSE);
3471 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
3474 case GTK_RC_TOKEN_PRELIGHT:
3475 gtk_icon_source_set_state_wildcarded (source, FALSE);
3476 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
3480 case GTK_RC_TOKEN_INSENSITIVE:
3481 gtk_icon_source_set_state_wildcarded (source, FALSE);
3482 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
3485 case GTK_RC_TOKEN_ACTIVE:
3486 gtk_icon_source_set_state_wildcarded (source, FALSE);
3487 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
3490 case GTK_RC_TOKEN_SELECTED:
3491 gtk_icon_source_set_state_wildcarded (source, FALSE);
3492 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
3499 gtk_icon_source_free (source);
3500 return GTK_RC_TOKEN_PRELIGHT;
3504 token = g_scanner_get_next_token (scanner);
3506 if (token == G_TOKEN_RIGHT_CURLY)
3508 else if (token != G_TOKEN_COMMA)
3510 gtk_icon_source_free (source);
3511 return G_TOKEN_COMMA;
3516 token = g_scanner_get_next_token (scanner);
3522 if (token != G_TOKEN_STRING)
3524 gtk_icon_source_free (source);
3525 return G_TOKEN_STRING;
3528 size = gtk_icon_size_from_name (scanner->value.v_string);
3530 if (size != GTK_ICON_SIZE_INVALID)
3532 gtk_icon_source_set_size_wildcarded (source, FALSE);
3533 gtk_icon_source_set_size (source, size);
3537 /* Check the close brace */
3539 token = g_scanner_get_next_token (scanner);
3540 if (token != G_TOKEN_RIGHT_CURLY)
3542 gtk_icon_source_free (source);
3543 return G_TOKEN_RIGHT_CURLY;
3547 if (gtk_icon_source_get_filename (source))
3548 gtk_icon_set_add_source (icon_set, source);
3549 gtk_icon_source_free (source);
3551 return G_TOKEN_NONE;
3555 gtk_rc_parse_stock (GtkRcContext *context,
3557 GtkRcStyle *rc_style,
3558 GtkIconFactory *factory)
3560 GtkIconSet *icon_set = NULL;
3561 gchar *stock_id = NULL;
3564 token = g_scanner_get_next_token (scanner);
3565 if (token != GTK_RC_TOKEN_STOCK)
3566 return GTK_RC_TOKEN_STOCK;
3568 token = gtk_rc_parse_stock_id (scanner, &stock_id);
3569 if (token != G_TOKEN_NONE)
3572 token = g_scanner_get_next_token (scanner);
3573 if (token != G_TOKEN_EQUAL_SIGN)
3576 return G_TOKEN_EQUAL_SIGN;
3579 token = g_scanner_get_next_token (scanner);
3580 if (token != G_TOKEN_LEFT_CURLY)
3583 return G_TOKEN_LEFT_CURLY;
3586 token = g_scanner_peek_next_token (scanner);
3587 while (token != G_TOKEN_RIGHT_CURLY)
3589 if (icon_set == NULL)
3590 icon_set = gtk_icon_set_new ();
3592 token = gtk_rc_parse_icon_source (context, scanner, icon_set);
3593 if (token != G_TOKEN_NONE)
3596 gtk_icon_set_unref (icon_set);
3600 token = g_scanner_get_next_token (scanner);
3602 if (token != G_TOKEN_COMMA &&
3603 token != G_TOKEN_RIGHT_CURLY)
3606 gtk_icon_set_unref (icon_set);
3607 return G_TOKEN_RIGHT_CURLY;
3613 gtk_icon_factory_add (factory,
3617 gtk_icon_set_unref (icon_set);
3622 return G_TOKEN_NONE;