1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
48 #include "gdkconfig.h"
50 #include "gtkversion.h"
52 #include "gtkbindings.h"
53 #include "gtkthemes.h"
55 #include "gtkiconfactory.h"
56 #include "gtkprivate.h"
57 #include "gtksettings.h"
58 #include "gtkwindow.h"
64 typedef struct _GtkRcSet GtkRcSet;
65 typedef struct _GtkRcNode GtkRcNode;
66 typedef struct _GtkRcFile GtkRcFile;
79 gchar *canonical_name;
83 #define GTK_RC_MAX_PIXMAP_PATHS 128
87 GHashTable *rc_style_ht;
88 GtkSettings *settings;
89 GSList *rc_sets_widget;
90 GSList *rc_sets_widget_class;
91 GSList *rc_sets_class;
93 /* The files we have parsed, to reread later if necessary */
97 gchar *key_theme_name;
99 gchar *pixmap_path[GTK_RC_MAX_PIXMAP_PATHS];
101 gint default_priority;
104 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
106 static guint gtk_rc_style_hash (const gchar *name);
107 static gboolean gtk_rc_style_equal (const gchar *a,
109 static guint gtk_rc_styles_hash (const GSList *rc_styles);
110 static gboolean gtk_rc_styles_equal (const GSList *a,
112 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
114 static GSList * gtk_rc_styles_match (GSList *rc_styles,
118 const gchar *path_reversed);
119 static GtkStyle * gtk_rc_style_to_style (GtkRcStyle *rc_style);
120 static GtkStyle* gtk_rc_init_style (GSList *rc_styles);
121 static void gtk_rc_parse_default_files (GtkRcContext *context);
122 static void gtk_rc_parse_named (GtkRcContext *context,
125 static void gtk_rc_parse_file (GtkRcContext *context,
126 const gchar *filename,
129 static void gtk_rc_parse_any (GtkRcContext *context,
130 const gchar *input_name,
132 const gchar *input_string);
133 static guint gtk_rc_parse_statement (GtkRcContext *context,
135 static guint gtk_rc_parse_style (GtkRcContext *context,
137 static guint gtk_rc_parse_assignment (GScanner *scanner,
138 GtkRcProperty *prop);
139 static guint gtk_rc_parse_bg (GScanner *scanner,
141 static guint gtk_rc_parse_fg (GScanner *scanner,
143 static guint gtk_rc_parse_text (GScanner *scanner,
145 static guint gtk_rc_parse_base (GScanner *scanner,
147 static guint gtk_rc_parse_xthickness (GScanner *scanner,
149 static guint gtk_rc_parse_ythickness (GScanner *scanner,
151 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
153 GtkRcStyle *rc_style);
154 static guint gtk_rc_parse_font (GScanner *scanner,
155 GtkRcStyle *rc_style);
156 static guint gtk_rc_parse_fontset (GScanner *scanner,
157 GtkRcStyle *rc_style);
158 static guint gtk_rc_parse_font_name (GScanner *scanner,
159 GtkRcStyle *rc_style);
160 static guint gtk_rc_parse_engine (GtkRcContext *context,
162 GtkRcStyle **rc_style);
163 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
165 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
167 const gchar *pix_path);
168 static guint gtk_rc_parse_module_path (GScanner *scanner);
169 static void gtk_rc_parse_module_path_string (const gchar *mod_path);
170 static guint gtk_rc_parse_im_module_path (GScanner *scanner);
171 static guint gtk_rc_parse_im_module_file (GScanner *scanner);
172 static guint gtk_rc_parse_path_pattern (GtkRcContext *context,
174 static guint gtk_rc_parse_stock (GtkRcContext *context,
176 GtkRcStyle *rc_style,
177 GtkIconFactory *factory);
178 static void gtk_rc_clear_hash_node (gpointer key,
181 static void gtk_rc_clear_styles (GtkRcContext *context);
182 static void gtk_rc_append_default_module_path (void);
183 static void gtk_rc_add_initial_default_files (void);
185 static void gtk_rc_style_init (GtkRcStyle *style);
186 static void gtk_rc_style_class_init (GtkRcStyleClass *klass);
187 static void gtk_rc_style_finalize (GObject *object);
188 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
190 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
191 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
192 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
193 gconstpointer bsearch_node2);
195 static gpointer parent_class = NULL;
197 static const GScannerConfig gtk_rc_scanner_config =
201 ) /* cset_skip_characters */,
206 ) /* cset_identifier_first */,
211 ) /* cset_identifier_nth */,
212 ( "#\n" ) /* cpair_comment_single */,
214 TRUE /* case_sensitive */,
216 TRUE /* skip_comment_multi */,
217 TRUE /* skip_comment_single */,
218 TRUE /* scan_comment_multi */,
219 TRUE /* scan_identifier */,
220 FALSE /* scan_identifier_1char */,
221 FALSE /* scan_identifier_NULL */,
222 TRUE /* scan_symbols */,
223 TRUE /* scan_binary */,
224 TRUE /* scan_octal */,
225 TRUE /* scan_float */,
227 TRUE /* scan_hex_dollar */,
228 TRUE /* scan_string_sq */,
229 TRUE /* scan_string_dq */,
230 TRUE /* numbers_2_int */,
231 FALSE /* int_2_float */,
232 FALSE /* identifier_2_string */,
233 TRUE /* char_2_token */,
234 TRUE /* symbol_2_token */,
235 FALSE /* scope_0_fallback */,
243 { "include", GTK_RC_TOKEN_INCLUDE },
244 { "NORMAL", GTK_RC_TOKEN_NORMAL },
245 { "ACTIVE", GTK_RC_TOKEN_ACTIVE },
246 { "PRELIGHT", GTK_RC_TOKEN_PRELIGHT },
247 { "SELECTED", GTK_RC_TOKEN_SELECTED },
248 { "INSENSITIVE", GTK_RC_TOKEN_INSENSITIVE },
249 { "fg", GTK_RC_TOKEN_FG },
250 { "bg", GTK_RC_TOKEN_BG },
251 { "text", GTK_RC_TOKEN_TEXT },
252 { "base", GTK_RC_TOKEN_BASE },
253 { "xthickness", GTK_RC_TOKEN_XTHICKNESS },
254 { "ythickness", GTK_RC_TOKEN_YTHICKNESS },
255 { "font", GTK_RC_TOKEN_FONT },
256 { "fontset", GTK_RC_TOKEN_FONTSET },
257 { "font_name", GTK_RC_TOKEN_FONT_NAME },
258 { "bg_pixmap", GTK_RC_TOKEN_BG_PIXMAP },
259 { "pixmap_path", GTK_RC_TOKEN_PIXMAP_PATH },
260 { "style", GTK_RC_TOKEN_STYLE },
261 { "binding", GTK_RC_TOKEN_BINDING },
262 { "bind", GTK_RC_TOKEN_BIND },
263 { "widget", GTK_RC_TOKEN_WIDGET },
264 { "widget_class", GTK_RC_TOKEN_WIDGET_CLASS },
265 { "class", GTK_RC_TOKEN_CLASS },
266 { "lowest", GTK_RC_TOKEN_LOWEST },
267 { "gtk", GTK_RC_TOKEN_GTK },
268 { "application", GTK_RC_TOKEN_APPLICATION },
269 { "theme", GTK_RC_TOKEN_THEME },
270 { "rc", GTK_RC_TOKEN_RC },
271 { "highest", GTK_RC_TOKEN_HIGHEST },
272 { "engine", GTK_RC_TOKEN_ENGINE },
273 { "module_path", GTK_RC_TOKEN_MODULE_PATH },
274 { "stock", GTK_RC_TOKEN_STOCK },
275 { "im_module_path", GTK_RC_TOKEN_IM_MODULE_PATH },
276 { "im_module_file", GTK_RC_TOKEN_IM_MODULE_FILE },
277 { "LTR", GTK_RC_TOKEN_LTR },
278 { "RTL", GTK_RC_TOKEN_RTL }
281 static GHashTable *realized_style_ht = NULL;
283 static gchar *im_module_path = NULL;
284 static gchar *im_module_file = NULL;
286 #define GTK_RC_MAX_DEFAULT_FILES 128
287 static gchar *gtk_rc_default_files[GTK_RC_MAX_DEFAULT_FILES];
289 #define GTK_RC_MAX_MODULE_PATHS 128
290 static gchar *module_path[GTK_RC_MAX_MODULE_PATHS];
292 /* A stack of directories for RC files we are parsing currently.
293 * these are implicitely added to the end of PIXMAP_PATHS
295 static GSList *rc_dir_stack = NULL;
297 /* RC file handling */
300 gtk_rc_make_default_dir (const gchar *type)
304 var = getenv("GTK_EXE_PREFIX");
306 path = g_build_filename (var, "lib", "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
308 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
314 gtk_rc_get_im_module_path (void)
316 const gchar *result = g_getenv ("GTK_IM_MODULE_PATH");
321 result = im_module_path;
323 return gtk_rc_make_default_dir ("immodules");
326 return g_strdup (result);
330 gtk_rc_get_im_module_file (void)
332 gchar *result = g_strdup (g_getenv ("GTK_IM_MODULE_FILE"));
337 result = g_strdup (im_module_file);
339 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
346 gtk_rc_get_theme_dir (void)
350 var = getenv("GTK_DATA_PREFIX");
352 path = g_build_filename (var, "share", "themes", NULL);
354 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
360 gtk_rc_get_module_dir (void)
362 return gtk_rc_make_default_dir ("engines");
366 gtk_rc_append_default_module_path(void)
372 for (n = 0; module_path[n]; n++) ;
373 if (n >= GTK_RC_MAX_MODULE_PATHS - 1)
376 var = getenv("GTK_EXE_PREFIX");
378 path = g_build_filename (var, "lib", "gtk-2.0", GTK_VERSION, "engines", NULL);
380 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", GTK_VERSION, "engines", NULL);
381 module_path[n++] = path;
383 var = g_get_home_dir ();
386 path = g_build_filename (var, ".gtk-2.0", GTK_VERSION, "engines", NULL);
387 module_path[n++] = path;
389 module_path[n] = NULL;
393 gtk_rc_add_initial_default_files (void)
395 static gint init = FALSE;
404 gtk_rc_default_files[0] = NULL;
407 var = g_getenv("GTK_RC_FILES");
410 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, 128);
414 gtk_rc_add_default_file (files[i]);
421 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
423 gtk_rc_add_default_file (str);
426 var = g_get_home_dir ();
429 str = g_build_filename (var, ".gtkrc-2.0", NULL);
430 gtk_rc_add_default_file (str);
437 * gtk_rc_add_default_file:
438 * @filename: the pathname to the file.
440 * Adds a file to the list of files to be parsed at the
444 gtk_rc_add_default_file (const gchar *filename)
448 gtk_rc_add_initial_default_files ();
450 for (n = 0; gtk_rc_default_files[n]; n++) ;
451 if (n >= GTK_RC_MAX_DEFAULT_FILES - 1)
454 gtk_rc_default_files[n++] = g_strdup (filename);
455 gtk_rc_default_files[n] = NULL;
459 * gtk_rc_set_default_files:
460 * @filenames: A %NULL-terminated list of filenames.
462 * Sets the list of files that GTK+ will read at the
466 gtk_rc_set_default_files (gchar **filenames)
470 gtk_rc_add_initial_default_files ();
473 while (gtk_rc_default_files[i])
475 g_free (gtk_rc_default_files[i]);
479 gtk_rc_default_files[0] = NULL;
482 while (filenames[i] != NULL)
484 gtk_rc_add_default_file (filenames[i]);
490 * gtk_rc_get_default_files:
492 * Retrieves the current list of RC files that will be parsed
493 * at the end of gtk_init().
495 * Return value: A %NULL-terminated array of filenames. This memory
496 * is owned by GTK+ and must not be freed by the application.
497 * If you want to store this information, you should make a copy.
500 gtk_rc_get_default_files (void)
502 gtk_rc_add_initial_default_files ();
504 return gtk_rc_default_files;
507 /* The following routine is based on _nl_normalize_codeset from
508 * the GNU C library. Contributed by
510 * Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
511 * Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
513 * Normalize codeset name. There is no standard for the codeset
514 * names. Normalization allows the user to use any of the common
518 _gtk_normalize_codeset (const gchar *codeset, gint name_len)
526 for (cnt = 0; cnt < name_len; ++cnt)
527 if (isalnum (codeset[cnt]))
531 if (isalpha (codeset[cnt]))
535 retval = g_malloc ((only_digit ? 3 : 0) + len + 1);
539 memcpy (retval, "iso", 4);
545 for (cnt = 0; cnt < name_len; ++cnt)
546 if (isalpha (codeset[cnt]))
547 *wp++ = isupper(codeset[cnt]) ? tolower (codeset[cnt]) : codeset[cnt];
548 else if (isdigit (codeset[cnt]))
549 *wp++ = codeset[cnt];
557 gtk_rc_settings_changed (GtkSettings *settings,
559 GtkRcContext *context)
561 gchar *new_theme_name;
562 gchar *new_key_theme_name;
564 g_object_get (settings,
565 "gtk-theme-name", &new_theme_name,
566 "gtk-key-theme-name", &new_key_theme_name,
569 if ((new_theme_name != context->theme_name &&
570 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
571 (new_key_theme_name != context->key_theme_name &&
572 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
574 gtk_rc_reparse_all_for_settings (settings, TRUE);
577 g_free (new_theme_name);
578 g_free (new_key_theme_name);
581 static GtkRcContext *
582 gtk_rc_context_get (GtkSettings *settings)
584 if (!settings->rc_context)
586 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
588 context->settings = settings;
589 context->rc_style_ht = NULL;
590 context->rc_sets_widget = NULL;
591 context->rc_sets_widget_class = NULL;
592 context->rc_sets_class = NULL;
593 context->rc_files = NULL;
595 g_object_get (settings,
596 "gtk-theme-name", &context->theme_name,
597 "gtk-key-theme-name", &context->key_theme_name,
600 g_signal_connect (settings,
601 "notify::gtk-theme-name",
602 G_CALLBACK (gtk_rc_settings_changed),
604 g_signal_connect (settings,
605 "notify::gtk-key-theme-name",
606 G_CALLBACK (gtk_rc_settings_changed),
609 context->pixmap_path[0] = NULL;
611 context->default_priority = GTK_PATH_PRIO_RC;
614 return settings->rc_context;
618 gtk_rc_parse_named (GtkRcContext *context,
623 const gchar *home_dir;
627 subpath = g_strconcat ("gtk-2.0-", type,
628 G_DIR_SEPARATOR_S "gtkrc",
631 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
633 /* First look in the users home directory
635 home_dir = g_get_home_dir ();
638 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
639 if (!g_file_test (path, G_FILE_TEST_EXISTS))
648 gchar *theme_dir = gtk_rc_get_theme_dir ();
649 path = g_build_filename (theme_dir, name, subpath, NULL);
652 if (!g_file_test (path, G_FILE_TEST_EXISTS))
661 gtk_rc_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
669 gtk_rc_parse_default_files (GtkRcContext *context)
671 gchar *locale_suffixes[3];
672 gint n_locale_suffixes = 0;
679 locale = g_win32_getlocale ();
681 locale = setlocale (LC_CTYPE, NULL);
684 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
686 /* Determine locale-specific suffixes for RC files
688 * We normalize the charset into a standard form,
689 * which has all '-' and '_' characters removed,
692 gchar *normalized_locale;
694 p = strchr (locale, '@');
695 length = p ? (p -locale) : strlen (locale);
697 p = strchr (locale, '.');
700 gchar *tmp1 = g_strndup (locale, p - locale + 1);
701 gchar *tmp2 = _gtk_normalize_codeset (p + 1, length - (p - locale + 1));
703 normalized_locale = g_strconcat (tmp1, tmp2, NULL);
707 locale_suffixes[n_locale_suffixes++] = g_strdup (normalized_locale);
711 normalized_locale = g_strndup (locale, length);
713 p = strchr (normalized_locale, '_');
716 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
717 length = p - normalized_locale;
720 locale_suffixes[n_locale_suffixes++] = g_strndup (normalized_locale, length);
722 g_free (normalized_locale);
725 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
727 /* Try to find a locale specific RC file corresponding to the
728 * current locale to parse before the default file.
730 for (j = n_locale_suffixes - 1; j >= 0; j--)
732 gchar *name = g_strconcat (gtk_rc_default_files[i],
736 gtk_rc_parse_file (context, name, GTK_PATH_PRIO_RC, FALSE);
739 gtk_rc_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
742 for (j = 0; j < n_locale_suffixes; j++)
743 g_free (locale_suffixes[j]);
749 static gboolean initialized = FALSE;
755 module_path[0] = NULL;
756 gtk_rc_append_default_module_path();
758 gtk_rc_add_initial_default_files ();
761 gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), TRUE);
765 gtk_rc_parse_string (const gchar *rc_string)
767 g_return_if_fail (rc_string != NULL);
769 gtk_rc_parse_any (gtk_rc_context_get (gtk_settings_get_default ()),
770 "-", -1, rc_string); /* FIXME */
774 gtk_rc_parse_file (GtkRcContext *context,
775 const gchar *filename,
779 GtkRcFile *rc_file = NULL;
784 g_return_if_fail (filename != NULL);
786 saved_priority = context->default_priority;
787 context->default_priority = priority;
789 tmp_list = context->rc_files;
792 rc_file = tmp_list->data;
793 if (!strcmp (rc_file->name, filename))
796 tmp_list = tmp_list->next;
801 rc_file = g_new (GtkRcFile, 1);
802 rc_file->name = g_strdup (filename);
803 rc_file->canonical_name = NULL;
805 rc_file->reload = reload;
807 context->rc_files = g_slist_append (context->rc_files, rc_file);
810 if (!rc_file->canonical_name)
812 /* Get the absolute pathname */
814 if (g_path_is_absolute (rc_file->name))
815 rc_file->canonical_name = rc_file->name;
820 cwd = g_get_current_dir ();
821 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
826 if (!lstat (rc_file->canonical_name, &statbuf))
831 rc_file->mtime = statbuf.st_mtime;
833 fd = open (rc_file->canonical_name, O_RDONLY);
837 /* Temporarily push directory name for this file on
838 * a stack of directory names while parsing it
841 g_slist_prepend (rc_dir_stack,
842 g_path_get_dirname (rc_file->canonical_name));
843 gtk_rc_parse_any (context, filename, fd, NULL);
845 tmp_list = rc_dir_stack;
846 rc_dir_stack = rc_dir_stack->next;
848 g_free (tmp_list->data);
849 g_slist_free_1 (tmp_list);
855 context->default_priority = saved_priority;
859 gtk_rc_parse (const gchar *filename)
861 g_return_if_fail (filename != NULL);
863 gtk_rc_parse_file (gtk_rc_context_get (gtk_settings_get_default ()),
864 filename, GTK_PATH_PRIO_RC, TRUE); /* FIXME */
867 /* Handling of RC styles */
870 gtk_rc_style_get_type (void)
872 static GType object_type = 0;
876 static const GTypeInfo object_info =
878 sizeof (GtkRcStyleClass),
879 (GBaseInitFunc) NULL,
880 (GBaseFinalizeFunc) NULL,
881 (GClassInitFunc) gtk_rc_style_class_init,
882 NULL, /* class_finalize */
883 NULL, /* class_data */
886 (GInstanceInitFunc) gtk_rc_style_init,
889 object_type = g_type_register_static (G_TYPE_OBJECT,
898 gtk_rc_style_init (GtkRcStyle *style)
903 style->font_desc = NULL;
905 for (i = 0; i < 5; i++)
907 static const GdkColor init_color = { 0, 0, 0, 0, };
909 style->bg_pixmap_name[i] = NULL;
910 style->color_flags[i] = 0;
911 style->fg[i] = init_color;
912 style->bg[i] = init_color;
913 style->text[i] = init_color;
914 style->base[i] = init_color;
916 style->xthickness = -1;
917 style->ythickness = -1;
918 style->rc_properties = NULL;
920 style->rc_style_lists = NULL;
921 style->icon_factories = NULL;
925 gtk_rc_style_class_init (GtkRcStyleClass *klass)
927 GObjectClass *object_class = G_OBJECT_CLASS (klass);
929 parent_class = g_type_class_peek_parent (klass);
931 object_class->finalize = gtk_rc_style_finalize;
934 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
935 klass->merge = gtk_rc_style_real_merge;
936 klass->create_style = gtk_rc_style_real_create_style;
940 gtk_rc_style_finalize (GObject *object)
942 GSList *tmp_list1, *tmp_list2;
943 GtkRcStyle *rc_style;
946 rc_style = GTK_RC_STYLE (object);
949 g_free (rc_style->name);
950 if (rc_style->font_desc)
951 pango_font_description_free (rc_style->font_desc);
953 for (i = 0; i < 5; i++)
954 if (rc_style->bg_pixmap_name[i])
955 g_free (rc_style->bg_pixmap_name[i]);
957 /* Now remove all references to this rc_style from
960 tmp_list1 = rc_style->rc_style_lists;
963 GSList *rc_styles = tmp_list1->data;
964 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
965 gtk_style_unref (style);
967 /* Remove the list of styles from the other rc_styles
970 tmp_list2 = rc_styles;
973 GtkRcStyle *other_style = tmp_list2->data;
975 if (other_style != rc_style)
976 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
978 tmp_list2 = tmp_list2->next;
981 /* And from the hash table itself
983 g_hash_table_remove (realized_style_ht, rc_styles);
984 g_slist_free (rc_styles);
986 tmp_list1 = tmp_list1->next;
988 g_slist_free (rc_style->rc_style_lists);
990 if (rc_style->rc_properties)
994 for (i = 0; i < rc_style->rc_properties->len; i++)
996 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
998 g_free (node->origin);
999 g_value_unset (&node->value);
1001 g_array_free (rc_style->rc_properties, TRUE);
1002 rc_style->rc_properties = NULL;
1005 tmp_list1 = rc_style->icon_factories;
1008 g_object_unref (G_OBJECT (tmp_list1->data));
1010 tmp_list1 = tmp_list1->next;
1012 g_slist_free (rc_style->icon_factories);
1014 G_OBJECT_CLASS (parent_class)->finalize (object);
1018 gtk_rc_style_new (void)
1022 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
1028 * gtk_rc_style_copy:
1029 * @orig: the style to copy
1031 * Makes a copy of the specified #GtkRcStyle. This function
1032 * will correctly copy an rc style that is a member of a class
1033 * derived from #GtkRcStyle.
1035 * Return value: the resulting #GtkRcStyle
1038 gtk_rc_style_copy (GtkRcStyle *orig)
1042 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1044 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1045 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1051 gtk_rc_style_ref (GtkRcStyle *rc_style)
1053 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1055 g_object_ref (G_OBJECT (rc_style));
1059 gtk_rc_style_unref (GtkRcStyle *rc_style)
1061 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1063 g_object_unref (G_OBJECT (rc_style));
1067 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1069 return GTK_RC_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
1073 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1074 gconstpointer bsearch_node2)
1076 const GtkRcProperty *prop1 = bsearch_node1;
1077 const GtkRcProperty *prop2 = bsearch_node2;
1079 if (prop1->type_name == prop2->type_name)
1080 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1082 return prop1->type_name < prop2->type_name ? -1 : 1;
1086 insert_rc_property (GtkRcStyle *style,
1087 GtkRcProperty *property,
1091 GtkRcProperty *new_property = NULL;
1092 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1094 key.type_name = property->type_name;
1095 key.property_name = property->property_name;
1097 if (!style->rc_properties)
1098 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1101 while (i < style->rc_properties->len)
1103 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1109 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1111 g_free (new_property->origin);
1112 g_value_unset (&new_property->value);
1114 *new_property = key;
1128 g_array_insert_val (style->rc_properties, i, key);
1129 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1132 new_property->origin = g_strdup (property->origin);
1133 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1134 g_value_copy (&property->value, &new_property->value);
1138 gtk_rc_style_real_merge (GtkRcStyle *dest,
1143 for (i = 0; i < 5; i++)
1145 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1146 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1148 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1149 src->color_flags[i] & GTK_RC_FG)
1151 dest->fg[i] = src->fg[i];
1152 dest->color_flags[i] |= GTK_RC_FG;
1154 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1155 src->color_flags[i] & GTK_RC_BG)
1157 dest->bg[i] = src->bg[i];
1158 dest->color_flags[i] |= GTK_RC_BG;
1160 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1161 src->color_flags[i] & GTK_RC_TEXT)
1163 dest->text[i] = src->text[i];
1164 dest->color_flags[i] |= GTK_RC_TEXT;
1166 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1167 src->color_flags[i] & GTK_RC_BASE)
1169 dest->base[i] = src->base[i];
1170 dest->color_flags[i] |= GTK_RC_BASE;
1174 if (dest->xthickness < 0 && src->xthickness >= 0)
1175 dest->xthickness = src->xthickness;
1176 if (dest->ythickness < 0 && src->ythickness >= 0)
1177 dest->ythickness = src->ythickness;
1181 if (!dest->font_desc)
1182 dest->font_desc = pango_font_description_copy (src->font_desc);
1184 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1187 if (src->rc_properties)
1191 for (i = 0; i < src->rc_properties->len; i++)
1192 insert_rc_property (dest,
1193 &g_array_index (src->rc_properties, GtkRcProperty, i),
1199 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1201 return gtk_style_new ();
1205 gtk_rc_clear_hash_node (gpointer key,
1209 gtk_rc_style_unref (data);
1213 gtk_rc_free_rc_sets (GSList *slist)
1219 rc_set = slist->data;
1220 g_pattern_spec_free (rc_set->pspec);
1223 slist = slist->next;
1228 gtk_rc_clear_styles (GtkRcContext *context)
1230 /* Clear out all old rc_styles */
1232 if (context->rc_style_ht)
1234 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1235 g_hash_table_destroy (context->rc_style_ht);
1236 context->rc_style_ht = NULL;
1239 gtk_rc_free_rc_sets (context->rc_sets_widget);
1240 g_slist_free (context->rc_sets_widget);
1241 context->rc_sets_widget = NULL;
1243 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1244 g_slist_free (context->rc_sets_widget_class);
1245 context->rc_sets_widget_class = NULL;
1247 gtk_rc_free_rc_sets (context->rc_sets_class);
1248 g_slist_free (context->rc_sets_class);
1249 context->rc_sets_class = NULL;
1252 /* Reset all our widgets. Also, we have to invalidate cached icons in
1253 * icon sets so they get re-rendered.
1256 gtk_rc_reset_widgets (GtkRcContext *context)
1258 GList *list, *toplevels;
1260 _gtk_icon_set_invalidate_caches ();
1262 toplevels = gtk_window_list_toplevels ();
1263 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1265 for (list = toplevels; list; list = list->next)
1267 gtk_widget_reset_rc_styles (list->data);
1268 gtk_widget_unref (list->data);
1270 g_list_free (toplevels);
1274 * gtk_rc_reparse_all_for_settings:
1275 * @settings: a #GtkSettings
1276 * @force_load: load whether or not anything changed
1278 * If the modification time on any previously read file
1279 * for the given #GtkSettings has changed, discard all style information
1280 * and then reread all previously read RC files.
1282 * Return value: %TRUE if the files were reread.
1285 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1286 gboolean force_load)
1288 gboolean mtime_modified = FALSE;
1291 GtkRcContext *context;
1293 struct stat statbuf;
1295 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1297 context = gtk_rc_context_get (settings);
1301 /* Check through and see if any of the RC's have had their
1302 * mtime modified. If so, reparse everything.
1304 tmp_list = context->rc_files;
1307 rc_file = tmp_list->data;
1309 if (!lstat (rc_file->name, &statbuf) &&
1310 (statbuf.st_mtime > rc_file->mtime))
1312 mtime_modified = TRUE;
1316 tmp_list = tmp_list->next;
1320 if (force_load || mtime_modified)
1324 gtk_rc_clear_styles (context);
1325 g_object_freeze_notify (G_OBJECT (context->settings));
1327 old_files = context->rc_files;
1328 context->rc_files = NULL;
1330 gtk_rc_parse_default_files (context);
1332 tmp_list = old_files;
1335 rc_file = tmp_list->data;
1336 if (rc_file->reload)
1337 gtk_rc_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, TRUE);
1339 if (rc_file->canonical_name != rc_file->name)
1340 g_free (rc_file->canonical_name);
1341 g_free (rc_file->name);
1344 tmp_list = tmp_list->next;
1347 g_slist_free (old_files);;
1349 g_free (context->theme_name);
1350 g_free (context->key_theme_name);
1351 g_object_get (context->settings,
1352 "gtk-theme-name", &context->theme_name,
1353 "gtk-key-theme-name", &context->key_theme_name,
1356 if (context->theme_name && context->theme_name[0])
1357 gtk_rc_parse_named (context, context->theme_name, NULL);
1358 if (context->key_theme_name && context->key_theme_name[0])
1359 gtk_rc_parse_named (context, context->key_theme_name, "key");
1361 g_object_thaw_notify (G_OBJECT (context->settings));
1363 gtk_rc_reset_widgets (context);
1366 return mtime_modified;
1370 * gtk_rc_reparse_all:
1372 * If the modification time on any previously read file for the
1373 * default #GtkSettings has changed, discard all style information
1374 * and then reread all previously read RC files.
1376 * Return value: %TRUE if the files were reread.
1379 gtk_rc_reparse_all (void)
1381 return gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), FALSE);
1385 gtk_rc_styles_match (GSList *rc_styles,
1389 const gchar *path_reversed)
1396 rc_set = sets->data;
1399 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1400 rc_styles = g_slist_append (rc_styles, rc_set->rc_style);
1408 * @widget: a #GtkWidget
1410 * Finds all matching RC styles for a given widget,
1411 * composites them together, and then creates a
1412 * #GtkStyle representing the composite appearance.
1413 * (GTK+ actually keeps a cache of previously
1414 * created styles, so a new style may not be
1417 * Returns: the resulting style. No refcount is added
1418 * to the returned style, so if you want to save this
1419 * style around, you should add a reference yourself.
1422 gtk_rc_get_style (GtkWidget *widget)
1424 GtkRcStyle *widget_rc_style;
1425 GSList *rc_styles = NULL;
1426 GtkRcContext *context;
1428 static guint rc_style_key_id = 0;
1430 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1432 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1434 /* We allow the specification of a single rc style to be bound
1435 * tightly to a widget, for application modifications
1437 if (!rc_style_key_id)
1438 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1440 widget_rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
1443 if (widget_rc_style)
1444 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1446 if (context->rc_sets_widget)
1448 gchar *path, *path_reversed;
1451 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1452 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1454 g_free (path_reversed);
1457 if (context->rc_sets_widget_class)
1459 gchar *path, *path_reversed;
1462 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1463 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1465 g_free (path_reversed);
1468 if (context->rc_sets_class)
1472 type = GTK_OBJECT_TYPE (widget);
1476 gchar *path_reversed;
1479 path = gtk_type_name (type);
1480 path_length = strlen (path);
1481 path_reversed = g_strdup (path);
1482 g_strreverse (path_reversed);
1484 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1485 g_free (path_reversed);
1487 type = gtk_type_parent (type);
1492 return gtk_rc_init_style (rc_styles);
1498 * gtk_rc_get_style_by_paths:
1499 * @settings: a #GtkSettings object
1500 * @widget_path: the widget path to use when looking up the style, or %NULL
1501 * @class_path: the class path to use when looking up the style, or %NULL
1502 * @type: a type that will be used along with parent types of this type
1503 * when matching against class styles, or #G_TYPE_NONE
1505 * Creates up a #GtkStyle from styles defined in a RC file by providing
1506 * the raw components used in matching. This function may be useful
1507 * when creating pseudo-widgets that should be themed like widgets but
1508 * don't actually have corresponding GTK+ widgets. An example of this
1509 * would be items inside a GNOME canvas widget.
1511 * The action of gtk_rc_get_style() is similar to:
1512 * <informalexample><programlisting>
1513 * gtk_widget_path (widget, NULL, &path, NULL);
1514 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
1515 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
1516 * G_OBJECT_TYPE (widget));
1517 * </programlisting></informalexample>
1519 * Return value: A style created by matching with the supplied paths,
1520 * or %NULL if nothing matching was specified and the default style should
1521 * be used. The returned value is owned by GTK+ as part of an internal cache,
1522 * so you must call g_object_ref() on the returned value if you want to
1523 * keep a reference to it.
1526 gtk_rc_get_style_by_paths (GtkSettings *settings,
1527 const char *widget_path,
1528 const char *class_path,
1531 /* We duplicate the code from above to avoid slowing down the above
1532 * by generating paths when we don't need them. I don't know if
1533 * this is really worth it.
1535 GSList *rc_styles = NULL;
1536 GtkRcContext *context;
1538 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1540 context = gtk_rc_context_get (settings);
1542 if (context->rc_sets_widget)
1544 gchar *path_reversed;
1547 path_length = strlen (widget_path);
1548 path_reversed = g_strdup (widget_path);
1549 g_strreverse (path_reversed);
1551 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, widget_path, path_reversed);
1552 g_free (path_reversed);
1555 if (context->rc_sets_widget_class)
1557 gchar *path_reversed;
1560 path_length = strlen (class_path);
1561 path_reversed = g_strdup (class_path);
1562 g_strreverse (path_reversed);
1564 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, class_path, path_reversed);
1565 g_free (path_reversed);
1568 if (type != G_TYPE_NONE && context->rc_sets_class)
1573 gchar *path_reversed;
1576 path = g_type_name (type);
1577 path_length = strlen (path);
1578 path_reversed = g_strdup (path);
1579 g_strreverse (path_reversed);
1581 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1582 g_free (path_reversed);
1584 type = g_type_parent (type);
1589 return gtk_rc_init_style (rc_styles);
1595 gtk_rc_add_rc_sets (GSList *slist,
1596 GtkRcStyle *rc_style,
1597 const gchar *pattern)
1599 GtkRcStyle *new_style;
1603 new_style = gtk_rc_style_new ();
1604 *new_style = *rc_style;
1605 new_style->name = g_strdup (rc_style->name);
1606 if (rc_style->font_desc)
1607 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
1609 for (i = 0; i < 5; i++)
1610 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1612 rc_set = g_new (GtkRcSet, 1);
1613 rc_set->pspec = g_pattern_spec_new (pattern);
1614 rc_set->rc_style = rc_style;
1616 return g_slist_prepend (slist, rc_set);
1620 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
1621 const gchar *pattern)
1623 GtkRcContext *context;
1625 g_return_if_fail (rc_style != NULL);
1626 g_return_if_fail (pattern != NULL);
1628 context = gtk_rc_context_get (gtk_settings_get_default ());
1630 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern);
1634 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
1635 const gchar *pattern)
1637 GtkRcContext *context;
1639 g_return_if_fail (rc_style != NULL);
1640 g_return_if_fail (pattern != NULL);
1642 context = gtk_rc_context_get (gtk_settings_get_default ());
1644 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern);
1648 gtk_rc_add_class_style (GtkRcStyle *rc_style,
1649 const gchar *pattern)
1651 GtkRcContext *context;
1653 g_return_if_fail (rc_style != NULL);
1654 g_return_if_fail (pattern != NULL);
1656 context = gtk_rc_context_get (gtk_settings_get_default ());
1658 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern);
1662 gtk_rc_scanner_new (void)
1664 return g_scanner_new (>k_rc_scanner_config);
1668 gtk_rc_parse_any (GtkRcContext *context,
1669 const gchar *input_name,
1671 const gchar *input_string)
1677 scanner = gtk_rc_scanner_new ();
1681 g_assert (input_string == NULL);
1683 g_scanner_input_file (scanner, input_fd);
1687 g_assert (input_string != NULL);
1689 g_scanner_input_text (scanner, input_string, strlen (input_string));
1691 scanner->input_name = input_name;
1693 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1694 g_scanner_scope_add_symbol (scanner, 0, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1699 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1703 guint expected_token;
1705 expected_token = gtk_rc_parse_statement (context, scanner);
1707 if (expected_token != G_TOKEN_NONE)
1714 if (scanner->scope_id == 0)
1716 /* if we are in scope 0, we know the symbol names
1717 * that are associated with certaintoken values.
1718 * so we look them up to make the error messages
1721 if (expected_token > GTK_RC_TOKEN_INVALID &&
1722 expected_token < GTK_RC_TOKEN_LAST)
1724 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1725 if (symbols[i].token == expected_token)
1726 msg = symbols[i].name;
1728 msg = g_strconcat ("e.g. `", msg, "'", NULL);
1730 if (scanner->token > GTK_RC_TOKEN_INVALID &&
1731 scanner->token < GTK_RC_TOKEN_LAST)
1733 symbol_name = "???";
1734 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1735 if (symbols[i].token == scanner->token)
1736 symbol_name = symbols[i].name;
1739 g_scanner_unexp_token (scanner,
1752 g_scanner_destroy (scanner);
1756 gtk_rc_styles_hash (const GSList *rc_styles)
1763 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
1764 rc_styles = rc_styles->next;
1771 gtk_rc_styles_equal (const GSList *a,
1776 if (a->data != b->data)
1786 gtk_rc_style_hash (const gchar *name)
1792 result += (result << 3) + *name++;
1798 gtk_rc_style_equal (const gchar *a,
1801 return (strcmp (a, b) == 0);
1805 gtk_rc_style_find (GtkRcContext *context,
1808 if (context->rc_style_ht)
1809 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
1815 gtk_rc_style_to_style (GtkRcStyle *rc_style)
1819 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
1821 style->rc_style = rc_style;
1823 gtk_rc_style_ref (rc_style);
1825 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
1830 /* Reuses or frees rc_styles */
1832 gtk_rc_init_style (GSList *rc_styles)
1834 GtkStyle *style = NULL;
1837 g_return_val_if_fail (rc_styles != NULL, NULL);
1839 if (!realized_style_ht)
1840 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
1841 (GEqualFunc) gtk_rc_styles_equal);
1843 style = g_hash_table_lookup (realized_style_ht, rc_styles);
1847 GtkRcStyle *base_style = NULL;
1848 GtkRcStyle *proto_style;
1849 GtkRcStyleClass *proto_style_class;
1851 GType rc_style_type = GTK_TYPE_RC_STYLE;
1853 /* Find the first derived style in the list, and use that to
1854 * create the merged style. If we only have raw GtkRcStyles, use
1855 * the first style to create the merged style.
1857 base_style = rc_styles->data;
1858 tmp_styles = rc_styles;
1861 GtkRcStyle *rc_style = tmp_styles->data;
1863 if (G_OBJECT_TYPE (rc_style) != rc_style_type)
1865 base_style = rc_style;
1869 tmp_styles = tmp_styles->next;
1872 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
1873 proto_style = proto_style_class->create_rc_style (base_style);
1875 tmp_styles = rc_styles;
1878 GtkRcStyle *rc_style = tmp_styles->data;
1881 proto_style_class->merge (proto_style, rc_style);
1883 /* Point from each rc_style to the list of styles */
1884 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
1885 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
1887 factories = g_slist_copy (rc_style->icon_factories);
1893 while (iter != NULL)
1895 g_object_ref (G_OBJECT (iter->data));
1896 iter = g_slist_next (iter);
1899 proto_style->icon_factories = g_slist_concat (proto_style->icon_factories,
1904 tmp_styles = tmp_styles->next;
1907 for (i = 0; i < 5; i++)
1908 if (proto_style->bg_pixmap_name[i] &&
1909 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
1911 g_free (proto_style->bg_pixmap_name[i]);
1912 proto_style->bg_pixmap_name[i] = NULL;
1915 style = gtk_rc_style_to_style (proto_style);
1916 gtk_rc_style_unref (proto_style);
1918 g_hash_table_insert (realized_style_ht, rc_styles, style);
1921 g_slist_free (rc_styles);
1926 /*********************
1927 * Parsing functions *
1928 *********************/
1931 rc_parse_token_or_compound (GScanner *scanner,
1933 GTokenType delimiter)
1935 guint token = g_scanner_get_next_token (scanner);
1937 /* we either scan a single token (skipping comments)
1938 * or a compund statement.
1939 * compunds are enclosed in (), [] or {} braces, we read
1940 * them in via deep recursion.
1947 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
1950 g_string_append_printf (gstring, " %f", scanner->value.v_float);
1952 case G_TOKEN_STRING:
1953 string = g_strescape (scanner->value.v_string, NULL);
1954 g_string_append (gstring, " \"");
1955 g_string_append (gstring, string);
1956 g_string_append_c (gstring, '"');
1959 case G_TOKEN_IDENTIFIER:
1960 g_string_append_c (gstring, ' ');
1961 g_string_append (gstring, scanner->value.v_identifier);
1963 case G_TOKEN_COMMENT_SINGLE:
1964 case G_TOKEN_COMMENT_MULTI:
1965 return rc_parse_token_or_compound (scanner, gstring, delimiter);
1966 case G_TOKEN_LEFT_PAREN:
1967 g_string_append_c (gstring, ' ');
1968 g_string_append_c (gstring, token);
1969 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_PAREN);
1970 if (token != G_TOKEN_NONE)
1973 case G_TOKEN_LEFT_CURLY:
1974 g_string_append_c (gstring, ' ');
1975 g_string_append_c (gstring, token);
1976 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_CURLY);
1977 if (token != G_TOKEN_NONE)
1980 case G_TOKEN_LEFT_BRACE:
1981 g_string_append_c (gstring, ' ');
1982 g_string_append_c (gstring, token);
1983 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_BRACE);
1984 if (token != G_TOKEN_NONE)
1988 if (token >= 256 || token < 1)
1989 return delimiter ? delimiter : G_TOKEN_STRING;
1990 g_string_append_c (gstring, ' ');
1991 g_string_append_c (gstring, token);
1992 if (token == delimiter)
1993 return G_TOKEN_NONE;
1997 return G_TOKEN_NONE;
1999 return rc_parse_token_or_compound (scanner, gstring, delimiter);
2003 gtk_rc_parse_assignment (GScanner *scanner,
2004 GtkRcProperty *prop)
2006 gboolean scan_identifier = scanner->config->scan_identifier;
2007 gboolean scan_symbols = scanner->config->scan_symbols;
2008 gboolean identifier_2_string = scanner->config->identifier_2_string;
2009 gboolean char_2_token = scanner->config->char_2_token;
2010 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2011 gboolean numbers_2_int = scanner->config->numbers_2_int;
2012 gboolean negate = FALSE;
2015 /* check that this is an assignment */
2016 if (g_scanner_get_next_token (scanner) != '=')
2019 /* adjust scanner mode */
2020 scanner->config->scan_identifier = TRUE;
2021 scanner->config->scan_symbols = FALSE;
2022 scanner->config->identifier_2_string = FALSE;
2023 scanner->config->char_2_token = TRUE;
2024 scanner->config->scan_identifier_NULL = FALSE;
2025 scanner->config->numbers_2_int = TRUE;
2027 /* record location */
2028 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2030 /* parse optional sign */
2031 if (g_scanner_peek_next_token (scanner) == '-')
2033 g_scanner_get_next_token (scanner); /* eat sign */
2037 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an unparsed compund */
2038 token = g_scanner_peek_next_token (scanner);
2042 g_scanner_get_next_token (scanner);
2043 g_value_init (&prop->value, G_TYPE_LONG);
2044 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2045 token = G_TOKEN_NONE;
2048 g_scanner_get_next_token (scanner);
2049 g_value_init (&prop->value, G_TYPE_DOUBLE);
2050 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2051 token = G_TOKEN_NONE;
2053 case G_TOKEN_STRING:
2054 g_scanner_get_next_token (scanner);
2056 token = G_TOKEN_INT;
2059 g_value_init (&prop->value, G_TYPE_STRING);
2060 g_value_set_string (&prop->value, scanner->value.v_string);
2061 token = G_TOKEN_NONE;
2064 case G_TOKEN_IDENTIFIER:
2065 case G_TOKEN_LEFT_PAREN:
2066 case G_TOKEN_LEFT_CURLY:
2067 case G_TOKEN_LEFT_BRACE:
2070 GString *gstring = g_string_new ("");
2072 token = rc_parse_token_or_compound (scanner, gstring, 0);
2073 if (token == G_TOKEN_NONE)
2075 g_string_append_c (gstring, ' ');
2076 g_value_init (&prop->value, G_TYPE_GSTRING);
2077 g_value_set_static_boxed (&prop->value, gstring);
2080 g_string_free (gstring, TRUE);
2085 g_scanner_get_next_token (scanner);
2086 token = G_TOKEN_INT;
2090 /* restore scanner mode */
2091 scanner->config->scan_identifier = scan_identifier;
2092 scanner->config->scan_symbols = scan_symbols;
2093 scanner->config->identifier_2_string = identifier_2_string;
2094 scanner->config->char_2_token = char_2_token;
2095 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2096 scanner->config->numbers_2_int = numbers_2_int;
2102 is_c_identifier (const gchar *string)
2105 gboolean is_varname;
2107 is_varname = strchr (G_CSET_a_2_z G_CSET_A_2_Z "_", string[0]) != NULL;
2108 for (p = string + 1; *p && is_varname; p++)
2109 is_varname &= strchr (G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_-", *p) != NULL;
2115 gtk_rc_parse_statement (GtkRcContext *context,
2120 token = g_scanner_peek_next_token (scanner);
2123 case GTK_RC_TOKEN_INCLUDE:
2124 token = g_scanner_get_next_token (scanner);
2125 if (token != GTK_RC_TOKEN_INCLUDE)
2126 return GTK_RC_TOKEN_INCLUDE;
2127 token = g_scanner_get_next_token (scanner);
2128 if (token != G_TOKEN_STRING)
2129 return G_TOKEN_STRING;
2130 gtk_rc_parse_file (context, scanner->value.v_string, context->default_priority, FALSE);
2131 return G_TOKEN_NONE;
2133 case GTK_RC_TOKEN_STYLE:
2134 return gtk_rc_parse_style (context, scanner);
2136 case GTK_RC_TOKEN_BINDING:
2137 return gtk_binding_parse_binding (scanner);
2139 case GTK_RC_TOKEN_PIXMAP_PATH:
2140 return gtk_rc_parse_pixmap_path (context, scanner);
2142 case GTK_RC_TOKEN_WIDGET:
2143 return gtk_rc_parse_path_pattern (context, scanner);
2145 case GTK_RC_TOKEN_WIDGET_CLASS:
2146 return gtk_rc_parse_path_pattern (context, scanner);
2148 case GTK_RC_TOKEN_CLASS:
2149 return gtk_rc_parse_path_pattern (context, scanner);
2151 case GTK_RC_TOKEN_MODULE_PATH:
2152 return gtk_rc_parse_module_path (scanner);
2154 case GTK_RC_TOKEN_IM_MODULE_PATH:
2155 return gtk_rc_parse_im_module_path (scanner);
2157 case GTK_RC_TOKEN_IM_MODULE_FILE:
2158 return gtk_rc_parse_im_module_file (scanner);
2160 case G_TOKEN_IDENTIFIER:
2161 if (is_c_identifier (scanner->next_value.v_identifier))
2163 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2166 g_scanner_get_next_token (scanner); /* eat identifier */
2167 name = g_strdup (scanner->value.v_identifier);
2169 token = gtk_rc_parse_assignment (scanner, &prop);
2170 if (token == G_TOKEN_NONE)
2172 GtkSettingsValue svalue;
2174 svalue.origin = prop.origin;
2175 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2176 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2177 gtk_settings_set_property_value (context->settings,
2181 g_free (prop.origin);
2182 if (G_VALUE_TYPE (&prop.value))
2183 g_value_unset (&prop.value);
2190 g_scanner_get_next_token (scanner);
2191 return G_TOKEN_IDENTIFIER;
2194 g_scanner_get_next_token (scanner);
2195 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2200 gtk_rc_parse_style (GtkRcContext *context,
2203 GtkRcStyle *rc_style;
2204 GtkRcStyle *parent_style;
2208 GtkIconFactory *our_factory = NULL;
2210 token = g_scanner_get_next_token (scanner);
2211 if (token != GTK_RC_TOKEN_STYLE)
2212 return GTK_RC_TOKEN_STYLE;
2214 token = g_scanner_get_next_token (scanner);
2215 if (token != G_TOKEN_STRING)
2216 return G_TOKEN_STRING;
2219 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2221 /* If there's a list, its first member is always the factory belonging
2224 if (rc_style && rc_style->icon_factories)
2225 our_factory = rc_style->icon_factories->data;
2230 rc_style = gtk_rc_style_new ();
2231 rc_style->name = g_strdup (scanner->value.v_string);
2233 for (i = 0; i < 5; i++)
2234 rc_style->bg_pixmap_name[i] = NULL;
2236 for (i = 0; i < 5; i++)
2237 rc_style->color_flags[i] = 0;
2240 token = g_scanner_peek_next_token (scanner);
2241 if (token == G_TOKEN_EQUAL_SIGN)
2243 token = g_scanner_get_next_token (scanner);
2245 token = g_scanner_get_next_token (scanner);
2246 if (token != G_TOKEN_STRING)
2251 return G_TOKEN_STRING;
2254 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2259 for (i = 0; i < 5; i++)
2261 rc_style->color_flags[i] = parent_style->color_flags[i];
2262 rc_style->fg[i] = parent_style->fg[i];
2263 rc_style->bg[i] = parent_style->bg[i];
2264 rc_style->text[i] = parent_style->text[i];
2265 rc_style->base[i] = parent_style->base[i];
2268 rc_style->xthickness = parent_style->xthickness;
2269 rc_style->ythickness = parent_style->ythickness;
2271 if (parent_style->font_desc)
2273 if (rc_style->font_desc)
2274 pango_font_description_free (rc_style->font_desc);
2275 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
2278 if (parent_style->rc_properties)
2282 for (i = 0; i < parent_style->rc_properties->len; i++)
2283 insert_rc_property (rc_style,
2284 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
2288 for (i = 0; i < 5; i++)
2290 if (rc_style->bg_pixmap_name[i])
2291 g_free (rc_style->bg_pixmap_name[i]);
2292 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
2295 /* Append parent's factories, adding a ref to them */
2296 if (parent_style->icon_factories != NULL)
2298 /* Add a factory for ourselves if we have none,
2299 * in case we end up defining more stock icons.
2300 * I see no real way around this; we need to maintain
2301 * the invariant that the first factory in the list
2302 * is always our_factory, the one belonging to us,
2303 * and if we put parent factories in the list we can't
2304 * do that if the style is reopened.
2306 if (our_factory == NULL)
2308 our_factory = gtk_icon_factory_new ();
2309 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2313 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories,
2314 g_slist_copy (parent_style->icon_factories));
2316 factories = parent_style->icon_factories;
2317 while (factories != NULL)
2319 g_object_ref (G_OBJECT (factories->data));
2320 factories = factories->next;
2326 token = g_scanner_get_next_token (scanner);
2327 if (token != G_TOKEN_LEFT_CURLY)
2332 return G_TOKEN_LEFT_CURLY;
2335 token = g_scanner_peek_next_token (scanner);
2336 while (token != G_TOKEN_RIGHT_CURLY)
2340 case GTK_RC_TOKEN_BG:
2341 token = gtk_rc_parse_bg (scanner, rc_style);
2343 case GTK_RC_TOKEN_FG:
2344 token = gtk_rc_parse_fg (scanner, rc_style);
2346 case GTK_RC_TOKEN_TEXT:
2347 token = gtk_rc_parse_text (scanner, rc_style);
2349 case GTK_RC_TOKEN_BASE:
2350 token = gtk_rc_parse_base (scanner, rc_style);
2352 case GTK_RC_TOKEN_XTHICKNESS:
2353 token = gtk_rc_parse_xthickness (scanner, rc_style);
2355 case GTK_RC_TOKEN_YTHICKNESS:
2356 token = gtk_rc_parse_ythickness (scanner, rc_style);
2358 case GTK_RC_TOKEN_BG_PIXMAP:
2359 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
2361 case GTK_RC_TOKEN_FONT:
2362 token = gtk_rc_parse_font (scanner, rc_style);
2364 case GTK_RC_TOKEN_FONTSET:
2365 token = gtk_rc_parse_fontset (scanner, rc_style);
2367 case GTK_RC_TOKEN_FONT_NAME:
2368 token = gtk_rc_parse_font_name (scanner, rc_style);
2370 case GTK_RC_TOKEN_ENGINE:
2371 token = gtk_rc_parse_engine (context, scanner, &rc_style);
2373 case GTK_RC_TOKEN_STOCK:
2374 if (our_factory == NULL)
2376 our_factory = gtk_icon_factory_new ();
2377 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2380 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
2382 case G_TOKEN_IDENTIFIER:
2383 if (is_c_identifier (scanner->next_value.v_identifier) &&
2384 scanner->next_value.v_identifier[0] >= 'A' &&
2385 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
2387 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2389 g_scanner_get_next_token (scanner); /* eat type name */
2390 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
2391 if (g_scanner_get_next_token (scanner) != ':' ||
2392 g_scanner_get_next_token (scanner) != ':')
2397 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
2398 !is_c_identifier (scanner->value.v_identifier))
2400 token = G_TOKEN_IDENTIFIER;
2404 /* it's important that we do the same canonification as GParamSpecPool here */
2405 g_strcanon (scanner->value.v_identifier, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2406 prop.property_name = g_quark_from_string (scanner->value.v_identifier);
2408 token = gtk_rc_parse_assignment (scanner, &prop);
2409 if (token == G_TOKEN_NONE)
2411 g_return_val_if_fail (prop.origin != NULL && G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
2412 insert_rc_property (rc_style, &prop, TRUE);
2415 g_free (prop.origin);
2416 if (G_VALUE_TYPE (&prop.value))
2417 g_value_unset (&prop.value);
2421 g_scanner_get_next_token (scanner);
2422 token = G_TOKEN_IDENTIFIER;
2426 g_scanner_get_next_token (scanner);
2427 token = G_TOKEN_RIGHT_CURLY;
2431 if (token != G_TOKEN_NONE)
2434 gtk_rc_style_unref (rc_style);
2438 token = g_scanner_peek_next_token (scanner);
2439 } /* while (token != G_TOKEN_RIGHT_CURLY) */
2441 token = g_scanner_get_next_token (scanner);
2442 if (token != G_TOKEN_RIGHT_CURLY)
2446 if (rc_style->font_desc)
2447 pango_font_description_free (rc_style->font_desc);
2449 for (i = 0; i < 5; i++)
2450 if (rc_style->bg_pixmap_name[i])
2451 g_free (rc_style->bg_pixmap_name[i]);
2455 return G_TOKEN_RIGHT_CURLY;
2460 if (!context->rc_style_ht)
2461 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
2462 (GEqualFunc) gtk_rc_style_equal);
2464 g_hash_table_insert (context->rc_style_ht, rc_style->name, rc_style);
2467 return G_TOKEN_NONE;
2470 const GtkRcProperty*
2471 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
2473 GQuark property_name)
2475 GtkRcProperty *node = NULL;
2477 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
2479 if (rc_style->rc_properties)
2483 key.type_name = type_name;
2484 key.property_name = property_name;
2486 node = bsearch (&key,
2487 rc_style->rc_properties->data, rc_style->rc_properties->len,
2488 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
2495 gtk_rc_parse_bg (GScanner *scanner,
2501 token = g_scanner_get_next_token (scanner);
2502 if (token != GTK_RC_TOKEN_BG)
2503 return GTK_RC_TOKEN_BG;
2505 token = gtk_rc_parse_state (scanner, &state);
2506 if (token != G_TOKEN_NONE)
2509 token = g_scanner_get_next_token (scanner);
2510 if (token != G_TOKEN_EQUAL_SIGN)
2511 return G_TOKEN_EQUAL_SIGN;
2513 style->color_flags[state] |= GTK_RC_BG;
2514 return gtk_rc_parse_color (scanner, &style->bg[state]);
2518 gtk_rc_parse_fg (GScanner *scanner,
2524 token = g_scanner_get_next_token (scanner);
2525 if (token != GTK_RC_TOKEN_FG)
2526 return GTK_RC_TOKEN_FG;
2528 token = gtk_rc_parse_state (scanner, &state);
2529 if (token != G_TOKEN_NONE)
2532 token = g_scanner_get_next_token (scanner);
2533 if (token != G_TOKEN_EQUAL_SIGN)
2534 return G_TOKEN_EQUAL_SIGN;
2536 style->color_flags[state] |= GTK_RC_FG;
2537 return gtk_rc_parse_color (scanner, &style->fg[state]);
2541 gtk_rc_parse_text (GScanner *scanner,
2547 token = g_scanner_get_next_token (scanner);
2548 if (token != GTK_RC_TOKEN_TEXT)
2549 return GTK_RC_TOKEN_TEXT;
2551 token = gtk_rc_parse_state (scanner, &state);
2552 if (token != G_TOKEN_NONE)
2555 token = g_scanner_get_next_token (scanner);
2556 if (token != G_TOKEN_EQUAL_SIGN)
2557 return G_TOKEN_EQUAL_SIGN;
2559 style->color_flags[state] |= GTK_RC_TEXT;
2560 return gtk_rc_parse_color (scanner, &style->text[state]);
2564 gtk_rc_parse_base (GScanner *scanner,
2570 token = g_scanner_get_next_token (scanner);
2571 if (token != GTK_RC_TOKEN_BASE)
2572 return GTK_RC_TOKEN_BASE;
2574 token = gtk_rc_parse_state (scanner, &state);
2575 if (token != G_TOKEN_NONE)
2578 token = g_scanner_get_next_token (scanner);
2579 if (token != G_TOKEN_EQUAL_SIGN)
2580 return G_TOKEN_EQUAL_SIGN;
2582 style->color_flags[state] |= GTK_RC_BASE;
2583 return gtk_rc_parse_color (scanner, &style->base[state]);
2587 gtk_rc_parse_xthickness (GScanner *scanner,
2590 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
2591 return GTK_RC_TOKEN_XTHICKNESS;
2593 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2594 return G_TOKEN_EQUAL_SIGN;
2596 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2599 style->xthickness = scanner->value.v_int;
2601 return G_TOKEN_NONE;
2605 gtk_rc_parse_ythickness (GScanner *scanner,
2608 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
2609 return GTK_RC_TOKEN_YTHICKNESS;
2611 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2612 return G_TOKEN_EQUAL_SIGN;
2614 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2617 style->ythickness = scanner->value.v_int;
2619 return G_TOKEN_NONE;
2623 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
2625 GtkRcStyle *rc_style)
2631 token = g_scanner_get_next_token (scanner);
2632 if (token != GTK_RC_TOKEN_BG_PIXMAP)
2633 return GTK_RC_TOKEN_BG_PIXMAP;
2635 token = gtk_rc_parse_state (scanner, &state);
2636 if (token != G_TOKEN_NONE)
2639 token = g_scanner_get_next_token (scanner);
2640 if (token != G_TOKEN_EQUAL_SIGN)
2641 return G_TOKEN_EQUAL_SIGN;
2643 token = g_scanner_get_next_token (scanner);
2644 if (token != G_TOKEN_STRING)
2645 return G_TOKEN_STRING;
2647 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
2648 (strcmp (scanner->value.v_string, "<none>") == 0))
2649 pixmap_file = g_strdup (scanner->value.v_string);
2651 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
2652 scanner, scanner->value.v_string);
2656 if (rc_style->bg_pixmap_name[state])
2657 g_free (rc_style->bg_pixmap_name[state]);
2658 rc_style->bg_pixmap_name[state] = pixmap_file;
2661 return G_TOKEN_NONE;
2665 gtk_rc_check_pixmap_dir (const gchar *dir, const gchar *pixmap_file)
2670 buf = g_build_filename (dir, pixmap_file, NULL);
2672 fd = open (buf, O_RDONLY);
2685 * gtk_rc_find_pixmap_in_path:
2686 * @settings: a #GtkSettinsg
2687 * @scanner: Scanner used to get line number information for the
2688 * warning message, or %NULL
2689 * @pixmap_file: name of the pixmap file to locate.
2691 * Looks up a file in pixmap path for the specified #GtkSettings.
2692 * If the file is not found, it outputs a warning message using
2693 * g_warning() and returns %NULL.
2695 * Return value: the filename.
2698 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
2700 const gchar *pixmap_file)
2706 GtkRcContext *context = gtk_rc_context_get (settings);
2708 for (i = 0; (i < GTK_RC_MAX_PIXMAP_PATHS) && (context->pixmap_path[i] != NULL); i++)
2710 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
2715 tmp_list = rc_dir_stack;
2718 filename = gtk_rc_check_pixmap_dir (tmp_list->data, pixmap_file);
2722 tmp_list = tmp_list->next;
2726 g_warning (_("Unable to locate image file in pixmap_path: \"%s\" line %d"),
2727 pixmap_file, scanner->line);
2729 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
2736 gtk_rc_find_module_in_path (const gchar *module_file)
2742 for (i = 0; (i < GTK_RC_MAX_MODULE_PATHS) && (module_path[i] != NULL); i++)
2744 buf = g_build_filename (module_path[i], module_file, NULL);
2746 fd = open (buf, O_RDONLY);
2760 gtk_rc_parse_font (GScanner *scanner,
2761 GtkRcStyle *rc_style)
2765 token = g_scanner_get_next_token (scanner);
2766 if (token != GTK_RC_TOKEN_FONT)
2767 return GTK_RC_TOKEN_FONT;
2769 token = g_scanner_get_next_token (scanner);
2770 if (token != G_TOKEN_EQUAL_SIGN)
2771 return G_TOKEN_EQUAL_SIGN;
2773 token = g_scanner_get_next_token (scanner);
2774 if (token != G_TOKEN_STRING)
2775 return G_TOKEN_STRING;
2777 /* Ignore, do nothing */
2779 return G_TOKEN_NONE;
2783 gtk_rc_parse_fontset (GScanner *scanner,
2784 GtkRcStyle *rc_style)
2788 token = g_scanner_get_next_token (scanner);
2789 if (token != GTK_RC_TOKEN_FONTSET)
2790 return GTK_RC_TOKEN_FONTSET;
2792 token = g_scanner_get_next_token (scanner);
2793 if (token != G_TOKEN_EQUAL_SIGN)
2794 return G_TOKEN_EQUAL_SIGN;
2796 token = g_scanner_get_next_token (scanner);
2797 if (token != G_TOKEN_STRING)
2798 return G_TOKEN_STRING;
2800 /* Do nothing - silently ignore */
2802 return G_TOKEN_NONE;
2806 gtk_rc_parse_font_name (GScanner *scanner,
2807 GtkRcStyle *rc_style)
2811 token = g_scanner_get_next_token (scanner);
2812 if (token != GTK_RC_TOKEN_FONT_NAME)
2813 return GTK_RC_TOKEN_FONT;
2815 token = g_scanner_get_next_token (scanner);
2816 if (token != G_TOKEN_EQUAL_SIGN)
2817 return G_TOKEN_EQUAL_SIGN;
2819 token = g_scanner_get_next_token (scanner);
2820 if (token != G_TOKEN_STRING)
2821 return G_TOKEN_STRING;
2823 rc_style->font_desc = pango_font_description_from_string (scanner->value.v_string);
2825 return G_TOKEN_NONE;
2829 gtk_rc_parse_engine (GtkRcContext *context,
2831 GtkRcStyle **rc_style)
2834 GtkThemeEngine *engine;
2835 guint result = G_TOKEN_NONE;
2836 GtkRcStyle *new_style = NULL;
2837 gboolean parsed_curlies = FALSE;
2839 token = g_scanner_get_next_token (scanner);
2840 if (token != GTK_RC_TOKEN_ENGINE)
2841 return GTK_RC_TOKEN_ENGINE;
2843 token = g_scanner_get_next_token (scanner);
2844 if (token != G_TOKEN_STRING)
2845 return G_TOKEN_STRING;
2847 engine = gtk_theme_engine_get (scanner->value.v_string);
2849 token = g_scanner_get_next_token (scanner);
2850 if (token != G_TOKEN_LEFT_CURLY)
2851 return G_TOKEN_LEFT_CURLY;
2855 GtkRcStyleClass *new_class;
2857 new_style = gtk_theme_engine_create_rc_style (engine);
2858 g_type_module_unuse (G_TYPE_MODULE (engine));
2860 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
2862 new_class->merge (new_style, *rc_style);
2863 if ((*rc_style)->name)
2864 new_style->name = g_strdup ((*rc_style)->name);
2866 if (new_class->parse)
2868 parsed_curlies = TRUE;
2869 result = new_class->parse (new_style, context->settings, scanner);
2871 if (result != G_TOKEN_NONE)
2873 g_object_unref (G_OBJECT (new_style));
2879 if (!parsed_curlies)
2881 /* Skip over remainder, looking for nested {}'s
2885 result = G_TOKEN_RIGHT_CURLY;
2886 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
2888 if (token == G_TOKEN_LEFT_CURLY)
2890 else if (token == G_TOKEN_RIGHT_CURLY)
2895 result = G_TOKEN_NONE;
2903 g_object_unref (G_OBJECT (*rc_style));
2904 *rc_style = new_style;
2911 gtk_rc_parse_state (GScanner *scanner,
2912 GtkStateType *state)
2917 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2918 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
2920 /* we don't know where we got called from, so we reset the scope here.
2921 * if we bail out due to errors, we *don't* reset the scope, so the
2922 * error messaging code can make sense of our tokens.
2924 old_scope = g_scanner_set_scope (scanner, 0);
2926 token = g_scanner_get_next_token (scanner);
2927 if (token != G_TOKEN_LEFT_BRACE)
2928 return G_TOKEN_LEFT_BRACE;
2930 token = g_scanner_get_next_token (scanner);
2933 case GTK_RC_TOKEN_ACTIVE:
2934 *state = GTK_STATE_ACTIVE;
2936 case GTK_RC_TOKEN_INSENSITIVE:
2937 *state = GTK_STATE_INSENSITIVE;
2939 case GTK_RC_TOKEN_NORMAL:
2940 *state = GTK_STATE_NORMAL;
2942 case GTK_RC_TOKEN_PRELIGHT:
2943 *state = GTK_STATE_PRELIGHT;
2945 case GTK_RC_TOKEN_SELECTED:
2946 *state = GTK_STATE_SELECTED;
2949 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
2952 token = g_scanner_get_next_token (scanner);
2953 if (token != G_TOKEN_RIGHT_BRACE)
2954 return G_TOKEN_RIGHT_BRACE;
2956 g_scanner_set_scope (scanner, old_scope);
2958 return G_TOKEN_NONE;
2962 gtk_rc_parse_priority (GScanner *scanner,
2963 GtkPathPriorityType *priority)
2968 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
2969 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
2971 /* we don't know where we got called from, so we reset the scope here.
2972 * if we bail out due to errors, we *don't* reset the scope, so the
2973 * error messaging code can make sense of our tokens.
2975 old_scope = g_scanner_set_scope (scanner, 0);
2977 token = g_scanner_get_next_token (scanner);
2981 token = g_scanner_get_next_token (scanner);
2984 case GTK_RC_TOKEN_LOWEST:
2985 *priority = GTK_PATH_PRIO_LOWEST;
2987 case GTK_RC_TOKEN_GTK:
2988 *priority = GTK_PATH_PRIO_GTK;
2990 case GTK_RC_TOKEN_APPLICATION:
2991 *priority = GTK_PATH_PRIO_APPLICATION;
2993 case GTK_RC_TOKEN_THEME:
2994 *priority = GTK_PATH_PRIO_THEME;
2996 case GTK_RC_TOKEN_RC:
2997 *priority = GTK_PATH_PRIO_RC;
2999 case GTK_RC_TOKEN_HIGHEST:
3000 *priority = GTK_PATH_PRIO_HIGHEST;
3003 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3006 g_scanner_set_scope (scanner, old_scope);
3008 return G_TOKEN_NONE;
3012 gtk_rc_parse_color (GScanner *scanner,
3017 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3019 /* we don't need to set our own scope here, because
3020 * we don't need own symbols
3023 token = g_scanner_get_next_token (scanner);
3028 case G_TOKEN_LEFT_CURLY:
3029 token = g_scanner_get_next_token (scanner);
3030 if (token == G_TOKEN_INT)
3031 token_int = scanner->value.v_int;
3032 else if (token == G_TOKEN_FLOAT)
3033 token_int = scanner->value.v_float * 65535.0;
3035 return G_TOKEN_FLOAT;
3036 color->red = CLAMP (token_int, 0, 65535);
3038 token = g_scanner_get_next_token (scanner);
3039 if (token != G_TOKEN_COMMA)
3040 return G_TOKEN_COMMA;
3042 token = g_scanner_get_next_token (scanner);
3043 if (token == G_TOKEN_INT)
3044 token_int = scanner->value.v_int;
3045 else if (token == G_TOKEN_FLOAT)
3046 token_int = scanner->value.v_float * 65535.0;
3048 return G_TOKEN_FLOAT;
3049 color->green = CLAMP (token_int, 0, 65535);
3051 token = g_scanner_get_next_token (scanner);
3052 if (token != G_TOKEN_COMMA)
3053 return G_TOKEN_COMMA;
3055 token = g_scanner_get_next_token (scanner);
3056 if (token == G_TOKEN_INT)
3057 token_int = scanner->value.v_int;
3058 else if (token == G_TOKEN_FLOAT)
3059 token_int = scanner->value.v_float * 65535.0;
3061 return G_TOKEN_FLOAT;
3062 color->blue = CLAMP (token_int, 0, 65535);
3064 token = g_scanner_get_next_token (scanner);
3065 if (token != G_TOKEN_RIGHT_CURLY)
3066 return G_TOKEN_RIGHT_CURLY;
3067 return G_TOKEN_NONE;
3069 case G_TOKEN_STRING:
3070 if (!gdk_color_parse (scanner->value.v_string, color))
3072 g_scanner_warn (scanner, "Invalid color constant '%s'",
3073 scanner->value.v_string);
3074 return G_TOKEN_STRING;
3077 return G_TOKEN_NONE;
3080 return G_TOKEN_STRING;
3085 gtk_rc_parse_pixmap_path (GtkRcContext *context,
3090 token = g_scanner_get_next_token (scanner);
3091 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
3092 return GTK_RC_TOKEN_PIXMAP_PATH;
3094 token = g_scanner_get_next_token (scanner);
3095 if (token != G_TOKEN_STRING)
3096 return G_TOKEN_STRING;
3098 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
3100 return G_TOKEN_NONE;
3104 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
3106 const gchar *pix_path)
3109 gint start_offset = 0;
3113 /* free the old one, or just add to the old one ? */
3114 for (path_num = 0; context->pixmap_path[path_num]; path_num++)
3116 g_free (context->pixmap_path[path_num]);
3117 context->pixmap_path[path_num] = NULL;
3122 path_len = strlen (pix_path);
3124 for (end_offset = 0; end_offset <= path_len; end_offset++)
3126 if ((pix_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3127 (end_offset == path_len))
3129 gchar *path_element = g_strndup (pix_path + start_offset, end_offset - start_offset);
3130 if (g_path_is_absolute (path_element))
3132 context->pixmap_path[path_num] = path_element;
3134 context->pixmap_path[path_num] = NULL;
3138 g_warning (_("Pixmap path element: \"%s\" must be absolute, %s, line %d"),
3139 path_element, scanner->input_name, scanner->line);
3140 g_free (path_element);
3143 start_offset = end_offset + 1;
3149 gtk_rc_parse_module_path (GScanner *scanner)
3153 token = g_scanner_get_next_token (scanner);
3154 if (token != GTK_RC_TOKEN_MODULE_PATH)
3155 return GTK_RC_TOKEN_MODULE_PATH;
3157 token = g_scanner_get_next_token (scanner);
3158 if (token != G_TOKEN_STRING)
3159 return G_TOKEN_STRING;
3161 gtk_rc_parse_module_path_string (scanner->value.v_string);
3163 return G_TOKEN_NONE;
3167 gtk_rc_parse_im_module_path (GScanner *scanner)
3171 token = g_scanner_get_next_token (scanner);
3172 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3173 return GTK_RC_TOKEN_IM_MODULE_FILE;
3175 token = g_scanner_get_next_token (scanner);
3176 if (token != G_TOKEN_STRING)
3177 return G_TOKEN_STRING;
3180 g_free (im_module_path);
3182 im_module_path = g_strdup (scanner->value.v_string);
3184 return G_TOKEN_NONE;
3188 gtk_rc_parse_im_module_file (GScanner *scanner)
3192 token = g_scanner_get_next_token (scanner);
3193 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3194 return GTK_RC_TOKEN_IM_MODULE_FILE;
3196 token = g_scanner_get_next_token (scanner);
3197 if (token != G_TOKEN_STRING)
3198 return G_TOKEN_STRING;
3201 g_free (im_module_file);
3203 im_module_file = g_strdup (scanner->value.v_string);
3205 return G_TOKEN_NONE;
3209 gtk_rc_parse_module_path_string (const gchar *mod_path)
3212 gint start_offset = 0;
3216 /* free the old one, or just add to the old one ? */
3217 for (path_num=0; module_path[path_num]; path_num++)
3219 g_free (module_path[path_num]);
3220 module_path[path_num] = NULL;
3225 path_len = strlen (mod_path);
3227 for (end_offset = 0; end_offset <= path_len; end_offset++)
3229 if ((mod_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3230 (end_offset == path_len))
3232 module_path[path_num] = g_strndup (mod_path + start_offset, end_offset - start_offset);
3234 module_path[path_num] = NULL;
3235 start_offset = end_offset + 1;
3238 gtk_rc_append_default_module_path();
3242 rc_set_compare (gconstpointer a, gconstpointer b)
3244 const GtkRcSet *set_a = a;
3245 const GtkRcSet *set_b = b;
3247 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
3251 insert_rc_set (GSList *list, GtkRcSet *set)
3253 return g_slist_insert_sorted (list, set, rc_set_compare);
3257 gtk_rc_parse_path_pattern (GtkRcContext *context,
3261 GtkPathType path_type;
3263 gboolean is_binding;
3264 GtkPathPriorityType priority = context->default_priority;
3266 token = g_scanner_get_next_token (scanner);
3269 case GTK_RC_TOKEN_WIDGET:
3270 path_type = GTK_PATH_WIDGET;
3272 case GTK_RC_TOKEN_WIDGET_CLASS:
3273 path_type = GTK_PATH_WIDGET_CLASS;
3275 case GTK_RC_TOKEN_CLASS:
3276 path_type = GTK_PATH_CLASS;
3279 return GTK_RC_TOKEN_WIDGET_CLASS;
3282 token = g_scanner_get_next_token (scanner);
3283 if (token != G_TOKEN_STRING)
3284 return G_TOKEN_STRING;
3286 pattern = g_strdup (scanner->value.v_string);
3288 token = g_scanner_get_next_token (scanner);
3289 if (token == GTK_RC_TOKEN_STYLE)
3291 else if (token == GTK_RC_TOKEN_BINDING)
3296 return GTK_RC_TOKEN_STYLE;
3299 if (g_scanner_peek_next_token (scanner) == ':')
3301 token = gtk_rc_parse_priority (scanner, &priority);
3302 if (token != G_TOKEN_NONE)
3309 token = g_scanner_get_next_token (scanner);
3310 if (token != G_TOKEN_STRING)
3313 return G_TOKEN_STRING;
3318 GtkBindingSet *binding;
3320 binding = gtk_binding_set_find (scanner->value.v_string);
3324 return G_TOKEN_STRING;
3326 gtk_binding_set_add_path (binding, path_type, pattern, priority);
3330 GtkRcStyle *rc_style;
3333 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3338 return G_TOKEN_STRING;
3341 rc_set = g_new (GtkRcSet, 1);
3342 rc_set->pspec = g_pattern_spec_new (pattern);
3343 rc_set->rc_style = rc_style;
3344 rc_set->priority = priority;
3346 if (path_type == GTK_PATH_WIDGET)
3347 context->rc_sets_widget = insert_rc_set (context->rc_sets_widget, rc_set);
3348 else if (path_type == GTK_PATH_WIDGET_CLASS)
3349 context->rc_sets_widget_class = insert_rc_set (context->rc_sets_widget_class, rc_set);
3351 context->rc_sets_class = insert_rc_set (context->rc_sets_class, rc_set);
3355 return G_TOKEN_NONE;
3359 gtk_rc_parse_stock_id (GScanner *scanner,
3364 token = g_scanner_get_next_token (scanner);
3365 if (token != G_TOKEN_LEFT_BRACE)
3366 return G_TOKEN_LEFT_BRACE;
3368 token = g_scanner_get_next_token (scanner);
3370 if (token != G_TOKEN_STRING)
3371 return G_TOKEN_STRING;
3373 *stock_id = g_strdup (scanner->value.v_string);
3375 token = g_scanner_get_next_token (scanner);
3376 if (token != G_TOKEN_RIGHT_BRACE)
3379 return G_TOKEN_RIGHT_BRACE;
3382 return G_TOKEN_NONE;
3386 gtk_rc_parse_icon_source (GtkRcContext *context,
3388 GtkIconSet *icon_set)
3391 GtkIconSource *source;
3392 gchar *full_filename;
3394 token = g_scanner_get_next_token (scanner);
3395 if (token != G_TOKEN_LEFT_CURLY)
3396 return G_TOKEN_LEFT_CURLY;
3398 token = g_scanner_get_next_token (scanner);
3400 if (token != G_TOKEN_STRING)
3401 return G_TOKEN_STRING;
3404 source = gtk_icon_source_new ();
3406 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
3409 gtk_icon_source_set_filename (source, full_filename);
3410 g_free (full_filename);
3413 /* We continue parsing even if we didn't find the pixmap so that rest of the
3414 * file is read, even if the syntax is bad
3416 token = g_scanner_get_next_token (scanner);
3418 if (token == G_TOKEN_RIGHT_CURLY)
3420 else if (token != G_TOKEN_COMMA)
3422 gtk_icon_source_free (source);
3423 return G_TOKEN_COMMA;
3426 /* Get the direction */
3428 token = g_scanner_get_next_token (scanner);
3432 case GTK_RC_TOKEN_RTL:
3433 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3434 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
3437 case GTK_RC_TOKEN_LTR:
3438 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3439 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
3446 gtk_icon_source_free (source);
3447 return GTK_RC_TOKEN_RTL;
3451 token = g_scanner_get_next_token (scanner);
3453 if (token == G_TOKEN_RIGHT_CURLY)
3455 else if (token != G_TOKEN_COMMA)
3457 gtk_icon_source_free (source);
3458 return G_TOKEN_COMMA;
3463 token = g_scanner_get_next_token (scanner);
3467 case GTK_RC_TOKEN_NORMAL:
3468 gtk_icon_source_set_state_wildcarded (source, FALSE);
3469 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
3472 case GTK_RC_TOKEN_PRELIGHT:
3473 gtk_icon_source_set_state_wildcarded (source, FALSE);
3474 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
3478 case GTK_RC_TOKEN_INSENSITIVE:
3479 gtk_icon_source_set_state_wildcarded (source, FALSE);
3480 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
3483 case GTK_RC_TOKEN_ACTIVE:
3484 gtk_icon_source_set_state_wildcarded (source, FALSE);
3485 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
3488 case GTK_RC_TOKEN_SELECTED:
3489 gtk_icon_source_set_state_wildcarded (source, FALSE);
3490 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
3497 gtk_icon_source_free (source);
3498 return GTK_RC_TOKEN_PRELIGHT;
3502 token = g_scanner_get_next_token (scanner);
3504 if (token == G_TOKEN_RIGHT_CURLY)
3506 else if (token != G_TOKEN_COMMA)
3508 gtk_icon_source_free (source);
3509 return G_TOKEN_COMMA;
3514 token = g_scanner_get_next_token (scanner);
3520 if (token != G_TOKEN_STRING)
3522 gtk_icon_source_free (source);
3523 return G_TOKEN_STRING;
3526 size = gtk_icon_size_from_name (scanner->value.v_string);
3528 if (size != GTK_ICON_SIZE_INVALID)
3530 gtk_icon_source_set_size_wildcarded (source, FALSE);
3531 gtk_icon_source_set_size (source, size);
3535 /* Check the close brace */
3537 token = g_scanner_get_next_token (scanner);
3538 if (token != G_TOKEN_RIGHT_CURLY)
3540 gtk_icon_source_free (source);
3541 return G_TOKEN_RIGHT_CURLY;
3545 if (gtk_icon_source_get_filename (source))
3546 gtk_icon_set_add_source (icon_set, source);
3547 gtk_icon_source_free (source);
3549 return G_TOKEN_NONE;
3553 gtk_rc_parse_stock (GtkRcContext *context,
3555 GtkRcStyle *rc_style,
3556 GtkIconFactory *factory)
3558 GtkIconSet *icon_set = NULL;
3559 gchar *stock_id = NULL;
3562 token = g_scanner_get_next_token (scanner);
3563 if (token != GTK_RC_TOKEN_STOCK)
3564 return GTK_RC_TOKEN_STOCK;
3566 token = gtk_rc_parse_stock_id (scanner, &stock_id);
3567 if (token != G_TOKEN_NONE)
3570 token = g_scanner_get_next_token (scanner);
3571 if (token != G_TOKEN_EQUAL_SIGN)
3574 return G_TOKEN_EQUAL_SIGN;
3577 token = g_scanner_get_next_token (scanner);
3578 if (token != G_TOKEN_LEFT_CURLY)
3581 return G_TOKEN_LEFT_CURLY;
3584 token = g_scanner_peek_next_token (scanner);
3585 while (token != G_TOKEN_RIGHT_CURLY)
3587 if (icon_set == NULL)
3588 icon_set = gtk_icon_set_new ();
3590 token = gtk_rc_parse_icon_source (context, scanner, icon_set);
3591 if (token != G_TOKEN_NONE)
3594 gtk_icon_set_unref (icon_set);
3598 token = g_scanner_get_next_token (scanner);
3600 if (token != G_TOKEN_COMMA &&
3601 token != G_TOKEN_RIGHT_CURLY)
3604 gtk_icon_set_unref (icon_set);
3605 return G_TOKEN_RIGHT_CURLY;
3611 gtk_icon_factory_add (factory,
3615 gtk_icon_set_unref (icon_set);
3620 return G_TOKEN_NONE;