1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
47 #include "gdkconfig.h"
49 #include "gtkversion.h"
51 #include "gtkbindings.h"
52 #include "gtkthemes.h"
54 #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;
77 gboolean is_string; /* If TRUE, name is a string to parse with gtk_rc_parse_string() */
80 gchar *canonical_name;
84 #define GTK_RC_MAX_PIXMAP_PATHS 128
88 GHashTable *rc_style_ht;
89 GtkSettings *settings;
90 GSList *rc_sets_widget;
91 GSList *rc_sets_widget_class;
92 GSList *rc_sets_class;
94 /* The files we have parsed, to reread later if necessary */
98 gchar *key_theme_name;
100 gchar *pixmap_path[GTK_RC_MAX_PIXMAP_PATHS];
102 gint default_priority;
105 static GtkRcContext *gtk_rc_context_get (GtkSettings *settings);
107 static guint gtk_rc_style_hash (const gchar *name);
108 static gboolean gtk_rc_style_equal (const gchar *a,
110 static guint gtk_rc_styles_hash (const GSList *rc_styles);
111 static gboolean gtk_rc_styles_equal (const GSList *a,
113 static GtkRcStyle* gtk_rc_style_find (GtkRcContext *context,
115 static GSList * gtk_rc_styles_match (GSList *rc_styles,
119 const gchar *path_reversed);
120 static GtkStyle * gtk_rc_style_to_style (GtkRcStyle *rc_style);
121 static GtkStyle* gtk_rc_init_style (GSList *rc_styles);
122 static void gtk_rc_parse_default_files (GtkRcContext *context);
123 static void gtk_rc_parse_named (GtkRcContext *context,
126 static void gtk_rc_parse_file (GtkRcContext *context,
127 const gchar *filename,
130 static void gtk_rc_parse_any (GtkRcContext *context,
131 const gchar *input_name,
133 const gchar *input_string);
134 static guint gtk_rc_parse_statement (GtkRcContext *context,
136 static guint gtk_rc_parse_style (GtkRcContext *context,
138 static guint gtk_rc_parse_assignment (GScanner *scanner,
139 GtkRcProperty *prop);
140 static guint gtk_rc_parse_bg (GScanner *scanner,
142 static guint gtk_rc_parse_fg (GScanner *scanner,
144 static guint gtk_rc_parse_text (GScanner *scanner,
146 static guint gtk_rc_parse_base (GScanner *scanner,
148 static guint gtk_rc_parse_xthickness (GScanner *scanner,
150 static guint gtk_rc_parse_ythickness (GScanner *scanner,
152 static guint gtk_rc_parse_bg_pixmap (GtkRcContext *context,
154 GtkRcStyle *rc_style);
155 static guint gtk_rc_parse_font (GScanner *scanner,
156 GtkRcStyle *rc_style);
157 static guint gtk_rc_parse_fontset (GScanner *scanner,
158 GtkRcStyle *rc_style);
159 static guint gtk_rc_parse_font_name (GScanner *scanner,
160 GtkRcStyle *rc_style);
161 static guint gtk_rc_parse_engine (GtkRcContext *context,
163 GtkRcStyle **rc_style);
164 static guint gtk_rc_parse_pixmap_path (GtkRcContext *context,
166 static void gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
168 const gchar *pix_path);
169 static guint gtk_rc_parse_module_path (GScanner *scanner);
170 static guint gtk_rc_parse_im_module_file (GScanner *scanner);
171 static guint gtk_rc_parse_path_pattern (GtkRcContext *context,
173 static guint gtk_rc_parse_stock (GtkRcContext *context,
175 GtkRcStyle *rc_style,
176 GtkIconFactory *factory);
177 static void gtk_rc_clear_hash_node (gpointer key,
180 static void gtk_rc_clear_styles (GtkRcContext *context);
181 static void gtk_rc_add_initial_default_files (void);
183 static void gtk_rc_style_init (GtkRcStyle *style);
184 static void gtk_rc_style_class_init (GtkRcStyleClass *klass);
185 static void gtk_rc_style_finalize (GObject *object);
186 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
188 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
189 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
190 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
191 gconstpointer bsearch_node2);
193 static gpointer parent_class = NULL;
195 static const GScannerConfig gtk_rc_scanner_config =
199 ) /* cset_skip_characters */,
204 ) /* cset_identifier_first */,
209 ) /* cset_identifier_nth */,
210 ( "#\n" ) /* cpair_comment_single */,
212 TRUE /* case_sensitive */,
214 TRUE /* skip_comment_multi */,
215 TRUE /* skip_comment_single */,
216 TRUE /* scan_comment_multi */,
217 TRUE /* scan_identifier */,
218 FALSE /* scan_identifier_1char */,
219 FALSE /* scan_identifier_NULL */,
220 TRUE /* scan_symbols */,
221 TRUE /* scan_binary */,
222 TRUE /* scan_octal */,
223 TRUE /* scan_float */,
225 TRUE /* scan_hex_dollar */,
226 TRUE /* scan_string_sq */,
227 TRUE /* scan_string_dq */,
228 TRUE /* numbers_2_int */,
229 FALSE /* int_2_float */,
230 FALSE /* identifier_2_string */,
231 TRUE /* char_2_token */,
232 TRUE /* symbol_2_token */,
233 FALSE /* scope_0_fallback */,
241 { "include", GTK_RC_TOKEN_INCLUDE },
242 { "NORMAL", GTK_RC_TOKEN_NORMAL },
243 { "ACTIVE", GTK_RC_TOKEN_ACTIVE },
244 { "PRELIGHT", GTK_RC_TOKEN_PRELIGHT },
245 { "SELECTED", GTK_RC_TOKEN_SELECTED },
246 { "INSENSITIVE", GTK_RC_TOKEN_INSENSITIVE },
247 { "fg", GTK_RC_TOKEN_FG },
248 { "bg", GTK_RC_TOKEN_BG },
249 { "text", GTK_RC_TOKEN_TEXT },
250 { "base", GTK_RC_TOKEN_BASE },
251 { "xthickness", GTK_RC_TOKEN_XTHICKNESS },
252 { "ythickness", GTK_RC_TOKEN_YTHICKNESS },
253 { "font", GTK_RC_TOKEN_FONT },
254 { "fontset", GTK_RC_TOKEN_FONTSET },
255 { "font_name", GTK_RC_TOKEN_FONT_NAME },
256 { "bg_pixmap", GTK_RC_TOKEN_BG_PIXMAP },
257 { "pixmap_path", GTK_RC_TOKEN_PIXMAP_PATH },
258 { "style", GTK_RC_TOKEN_STYLE },
259 { "binding", GTK_RC_TOKEN_BINDING },
260 { "bind", GTK_RC_TOKEN_BIND },
261 { "widget", GTK_RC_TOKEN_WIDGET },
262 { "widget_class", GTK_RC_TOKEN_WIDGET_CLASS },
263 { "class", GTK_RC_TOKEN_CLASS },
264 { "lowest", GTK_RC_TOKEN_LOWEST },
265 { "gtk", GTK_RC_TOKEN_GTK },
266 { "application", GTK_RC_TOKEN_APPLICATION },
267 { "theme", GTK_RC_TOKEN_THEME },
268 { "rc", GTK_RC_TOKEN_RC },
269 { "highest", GTK_RC_TOKEN_HIGHEST },
270 { "engine", GTK_RC_TOKEN_ENGINE },
271 { "module_path", GTK_RC_TOKEN_MODULE_PATH },
272 { "stock", GTK_RC_TOKEN_STOCK },
273 { "im_module_file", GTK_RC_TOKEN_IM_MODULE_FILE },
274 { "LTR", GTK_RC_TOKEN_LTR },
275 { "RTL", GTK_RC_TOKEN_RTL }
278 static GHashTable *realized_style_ht = NULL;
280 static gchar *im_module_file = NULL;
282 #define GTK_RC_MAX_DEFAULT_FILES 128
283 static gchar *gtk_rc_default_files[GTK_RC_MAX_DEFAULT_FILES];
285 /* A stack of directories for RC files we are parsing currently.
286 * these are implicitely added to the end of PIXMAP_PATHS
288 static GSList *rc_dir_stack = NULL;
290 /* RC file handling */
293 gtk_rc_make_default_dir (const gchar *type)
298 var = g_getenv ("GTK_EXE_PREFIX");
300 path = g_build_filename (var, "lib", "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
302 path = g_build_filename (GTK_LIBDIR, "gtk-2.0", type, GTK_BINARY_VERSION, NULL);
308 * gtk_rc_get_im_module_path:
309 * @returns: a newly-allocated string containing the path in which to
310 * look for IM modules.
312 * Obtains the path in which to look for IM modules. See the documentation
313 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
314 * environment variable for more details about looking up modules. This
315 * function is useful solely for utilities supplied with GTK+ and should
316 * not be used by applications under normal circumstances.
319 gtk_rc_get_im_module_path (void)
321 gchar **paths = _gtk_get_module_path ("immodules");
322 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
329 * gtk_rc_get_im_module_file:
330 * @returns: a newly-allocated string containing the name of the file
331 * listing the IM modules available for loading
333 * Obtains the path to the IM modules file. See the documentation
334 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
335 * environment variable for more details.
338 gtk_rc_get_im_module_file (void)
340 gchar *result = g_strdup (g_getenv ("GTK_IM_MODULE_FILE"));
345 result = g_strdup (im_module_file);
347 result = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtk.immodules", NULL);
354 gtk_rc_get_theme_dir (void)
359 var = g_getenv ("GTK_DATA_PREFIX");
361 path = g_build_filename (var, "share", "themes", NULL);
363 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
369 gtk_rc_get_module_dir (void)
371 return gtk_rc_make_default_dir ("engines");
375 gtk_rc_add_initial_default_files (void)
377 static gint init = FALSE;
386 gtk_rc_default_files[0] = NULL;
389 var = g_getenv ("GTK_RC_FILES");
392 files = g_strsplit (var, G_SEARCHPATH_SEPARATOR_S, 128);
396 gtk_rc_add_default_file (files[i]);
403 str = g_build_filename (GTK_SYSCONFDIR, "gtk-2.0", "gtkrc", NULL);
405 gtk_rc_add_default_file (str);
408 var = g_get_home_dir ();
411 str = g_build_filename (var, ".gtkrc-2.0", NULL);
412 gtk_rc_add_default_file (str);
419 * gtk_rc_add_default_file:
420 * @filename: the pathname to the file. If @filename is not absolute, it
421 * is searched in the current directory.
423 * Adds a file to the list of files to be parsed at the
427 gtk_rc_add_default_file (const gchar *filename)
431 gtk_rc_add_initial_default_files ();
433 for (n = 0; gtk_rc_default_files[n]; n++) ;
434 if (n >= GTK_RC_MAX_DEFAULT_FILES - 1)
437 gtk_rc_default_files[n++] = g_strdup (filename);
438 gtk_rc_default_files[n] = NULL;
442 * gtk_rc_set_default_files:
443 * @filenames: A %NULL-terminated list of filenames.
445 * Sets the list of files that GTK+ will read at the
449 gtk_rc_set_default_files (gchar **filenames)
453 gtk_rc_add_initial_default_files ();
456 while (gtk_rc_default_files[i])
458 g_free (gtk_rc_default_files[i]);
462 gtk_rc_default_files[0] = NULL;
465 while (filenames[i] != NULL)
467 gtk_rc_add_default_file (filenames[i]);
473 * gtk_rc_get_default_files:
475 * Retrieves the current list of RC files that will be parsed
476 * at the end of gtk_init().
478 * Return value: A %NULL-terminated array of filenames. This memory
479 * is owned by GTK+ and must not be freed by the application.
480 * If you want to store this information, you should make a copy.
483 gtk_rc_get_default_files (void)
485 gtk_rc_add_initial_default_files ();
487 return gtk_rc_default_files;
491 gtk_rc_settings_changed (GtkSettings *settings,
493 GtkRcContext *context)
495 gchar *new_theme_name;
496 gchar *new_key_theme_name;
498 g_object_get (settings,
499 "gtk-theme-name", &new_theme_name,
500 "gtk-key-theme-name", &new_key_theme_name,
503 if ((new_theme_name != context->theme_name &&
504 !(new_theme_name && context->theme_name && strcmp (new_theme_name, context->theme_name) == 0)) ||
505 (new_key_theme_name != context->key_theme_name &&
506 !(new_key_theme_name && context->key_theme_name && strcmp (new_key_theme_name, context->key_theme_name) == 0)))
508 gtk_rc_reparse_all_for_settings (settings, TRUE);
511 g_free (new_theme_name);
512 g_free (new_key_theme_name);
515 static GtkRcContext *
516 gtk_rc_context_get (GtkSettings *settings)
518 if (!settings->rc_context)
520 GtkRcContext *context = settings->rc_context = g_new (GtkRcContext, 1);
522 context->settings = settings;
523 context->rc_style_ht = NULL;
524 context->rc_sets_widget = NULL;
525 context->rc_sets_widget_class = NULL;
526 context->rc_sets_class = NULL;
527 context->rc_files = NULL;
529 g_object_get (settings,
530 "gtk-theme-name", &context->theme_name,
531 "gtk-key-theme-name", &context->key_theme_name,
534 g_signal_connect (settings,
535 "notify::gtk-theme-name",
536 G_CALLBACK (gtk_rc_settings_changed),
538 g_signal_connect (settings,
539 "notify::gtk-key-theme-name",
540 G_CALLBACK (gtk_rc_settings_changed),
543 context->pixmap_path[0] = NULL;
545 context->default_priority = GTK_PATH_PRIO_RC;
548 return settings->rc_context;
552 gtk_rc_parse_named (GtkRcContext *context,
557 const gchar *home_dir;
561 subpath = g_strconcat ("gtk-2.0-", type,
562 G_DIR_SEPARATOR_S "gtkrc",
565 subpath = g_strdup ("gtk-2.0" G_DIR_SEPARATOR_S "gtkrc");
567 /* First look in the users home directory
569 home_dir = g_get_home_dir ();
572 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
573 if (!g_file_test (path, G_FILE_TEST_EXISTS))
582 gchar *theme_dir = gtk_rc_get_theme_dir ();
583 path = g_build_filename (theme_dir, name, subpath, NULL);
586 if (!g_file_test (path, G_FILE_TEST_EXISTS))
595 gtk_rc_parse_file (context, path, GTK_PATH_PRIO_THEME, FALSE);
603 gtk_rc_parse_default_files (GtkRcContext *context)
607 for (i = 0; gtk_rc_default_files[i] != NULL; i++)
608 gtk_rc_parse_file (context, gtk_rc_default_files[i], GTK_PATH_PRIO_RC, FALSE);
614 static gboolean initialized = FALSE;
620 gtk_rc_add_initial_default_files ();
623 gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), TRUE);
625 /* Default RC string */
626 gtk_rc_parse_string ("style \"gtk-default-tooltips-style\" {\n"
627 " bg[NORMAL] = \"#ffffc0\"\n"
628 " fg[NORMAL] = \"#000000\"\n"
631 "widget \"gtk-tooltips*\" style : gtk \"gtk-default-tooltips-style\"\n");
635 gtk_rc_parse_string (const gchar *rc_string)
638 /* This is wrong; once we have meaningful multiple RC contexts, we need to parse the
639 * string in all contexts, and in fact, in future contexts as well.
641 GtkRcContext *context = gtk_rc_context_get (gtk_settings_get_default ());
643 g_return_if_fail (rc_string != NULL);
645 rc_file = g_new (GtkRcFile, 1);
646 rc_file->is_string = TRUE;
647 rc_file->name = g_strdup (rc_string);
648 rc_file->canonical_name = NULL;
650 rc_file->reload = TRUE;
652 context->rc_files = g_slist_append (context->rc_files, rc_file);
654 gtk_rc_parse_any (context, "-", -1, rc_string);
658 gtk_rc_parse_one_file (GtkRcContext *context,
659 const gchar *filename,
663 GtkRcFile *rc_file = NULL;
668 g_return_if_fail (filename != NULL);
670 saved_priority = context->default_priority;
671 context->default_priority = priority;
673 tmp_list = context->rc_files;
676 rc_file = tmp_list->data;
677 if (!strcmp (rc_file->name, filename))
680 tmp_list = tmp_list->next;
685 rc_file = g_new (GtkRcFile, 1);
686 rc_file->is_string = FALSE;
687 rc_file->name = g_strdup (filename);
688 rc_file->canonical_name = NULL;
690 rc_file->reload = reload;
692 context->rc_files = g_slist_append (context->rc_files, rc_file);
695 if (!rc_file->canonical_name)
697 /* Get the absolute pathname */
699 if (g_path_is_absolute (rc_file->name))
700 rc_file->canonical_name = rc_file->name;
705 cwd = g_get_current_dir ();
706 rc_file->canonical_name = g_build_filename (cwd, rc_file->name, NULL);
711 if (!lstat (rc_file->canonical_name, &statbuf))
716 rc_file->mtime = statbuf.st_mtime;
718 fd = open (rc_file->canonical_name, O_RDONLY);
722 /* Temporarily push directory name for this file on
723 * a stack of directory names while parsing it
726 g_slist_prepend (rc_dir_stack,
727 g_path_get_dirname (rc_file->canonical_name));
728 gtk_rc_parse_any (context, filename, fd, NULL);
730 tmp_list = rc_dir_stack;
731 rc_dir_stack = rc_dir_stack->next;
733 g_free (tmp_list->data);
734 g_slist_free_1 (tmp_list);
740 context->default_priority = saved_priority;
744 strchr_len (const gchar *str, gint len, char c)
758 gtk_rc_parse_file (GtkRcContext *context,
759 const gchar *filename,
763 gchar *locale_suffixes[2];
764 gint n_locale_suffixes = 0;
768 gboolean found = FALSE;
771 locale = g_win32_getlocale ();
773 locale = setlocale (LC_CTYPE, NULL);
776 if (strcmp (locale, "C") && strcmp (locale, "POSIX"))
778 /* Determine locale-specific suffixes for RC files.
780 length = strlen (locale);
782 p = strchr (locale, '@');
786 p = strchr_len (locale, length, '.');
790 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
792 p = strchr_len (locale, length, '_');
796 locale_suffixes[n_locale_suffixes++] = g_strndup (locale, length);
800 gtk_rc_parse_one_file (context, filename, priority, reload);
801 for (j = 0; j < n_locale_suffixes; j++)
805 gchar *name = g_strconcat (filename, ".", locale_suffixes[j], NULL);
806 if (g_file_test (name, G_FILE_TEST_EXISTS))
808 gtk_rc_parse_one_file (context, name, priority, FALSE);
815 g_free (locale_suffixes[j]);
820 gtk_rc_parse (const gchar *filename)
822 g_return_if_fail (filename != NULL);
824 /* This is wrong; once we have meaningful multiple RC contexts, we need to parse the
825 * file in all contexts, and in fact, in future contexts as well.
827 gtk_rc_parse_file (gtk_rc_context_get (gtk_settings_get_default ()),
828 filename, GTK_PATH_PRIO_RC, TRUE);
831 /* Handling of RC styles */
834 gtk_rc_style_get_type (void)
836 static GType object_type = 0;
840 static const GTypeInfo object_info =
842 sizeof (GtkRcStyleClass),
843 (GBaseInitFunc) NULL,
844 (GBaseFinalizeFunc) NULL,
845 (GClassInitFunc) gtk_rc_style_class_init,
846 NULL, /* class_finalize */
847 NULL, /* class_data */
850 (GInstanceInitFunc) gtk_rc_style_init,
853 object_type = g_type_register_static (G_TYPE_OBJECT,
862 gtk_rc_style_init (GtkRcStyle *style)
867 style->font_desc = NULL;
869 for (i = 0; i < 5; i++)
871 static const GdkColor init_color = { 0, 0, 0, 0, };
873 style->bg_pixmap_name[i] = NULL;
874 style->color_flags[i] = 0;
875 style->fg[i] = init_color;
876 style->bg[i] = init_color;
877 style->text[i] = init_color;
878 style->base[i] = init_color;
880 style->xthickness = -1;
881 style->ythickness = -1;
882 style->rc_properties = NULL;
884 style->rc_style_lists = NULL;
885 style->icon_factories = NULL;
889 gtk_rc_style_class_init (GtkRcStyleClass *klass)
891 GObjectClass *object_class = G_OBJECT_CLASS (klass);
893 parent_class = g_type_class_peek_parent (klass);
895 object_class->finalize = gtk_rc_style_finalize;
898 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
899 klass->merge = gtk_rc_style_real_merge;
900 klass->create_style = gtk_rc_style_real_create_style;
904 gtk_rc_style_finalize (GObject *object)
906 GSList *tmp_list1, *tmp_list2;
907 GtkRcStyle *rc_style;
910 rc_style = GTK_RC_STYLE (object);
913 g_free (rc_style->name);
914 if (rc_style->font_desc)
915 pango_font_description_free (rc_style->font_desc);
917 for (i = 0; i < 5; i++)
918 if (rc_style->bg_pixmap_name[i])
919 g_free (rc_style->bg_pixmap_name[i]);
921 /* Now remove all references to this rc_style from
924 tmp_list1 = rc_style->rc_style_lists;
927 GSList *rc_styles = tmp_list1->data;
928 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
929 gtk_style_unref (style);
931 /* Remove the list of styles from the other rc_styles
934 tmp_list2 = rc_styles;
937 GtkRcStyle *other_style = tmp_list2->data;
939 if (other_style != rc_style)
940 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
942 tmp_list2 = tmp_list2->next;
945 /* And from the hash table itself
947 g_hash_table_remove (realized_style_ht, rc_styles);
948 g_slist_free (rc_styles);
950 tmp_list1 = tmp_list1->next;
952 g_slist_free (rc_style->rc_style_lists);
954 if (rc_style->rc_properties)
958 for (i = 0; i < rc_style->rc_properties->len; i++)
960 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
962 g_free (node->origin);
963 g_value_unset (&node->value);
965 g_array_free (rc_style->rc_properties, TRUE);
966 rc_style->rc_properties = NULL;
969 tmp_list1 = rc_style->icon_factories;
972 g_object_unref (G_OBJECT (tmp_list1->data));
974 tmp_list1 = tmp_list1->next;
976 g_slist_free (rc_style->icon_factories);
978 G_OBJECT_CLASS (parent_class)->finalize (object);
982 gtk_rc_style_new (void)
986 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
993 * @orig: the style to copy
995 * Makes a copy of the specified #GtkRcStyle. This function
996 * will correctly copy an RC style that is a member of a class
997 * derived from #GtkRcStyle.
999 * Return value: the resulting #GtkRcStyle
1002 gtk_rc_style_copy (GtkRcStyle *orig)
1006 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
1008 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
1009 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
1015 gtk_rc_style_ref (GtkRcStyle *rc_style)
1017 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1019 g_object_ref (G_OBJECT (rc_style));
1023 gtk_rc_style_unref (GtkRcStyle *rc_style)
1025 g_return_if_fail (GTK_IS_RC_STYLE (rc_style));
1027 g_object_unref (G_OBJECT (rc_style));
1031 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
1033 return GTK_RC_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
1037 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
1038 gconstpointer bsearch_node2)
1040 const GtkRcProperty *prop1 = bsearch_node1;
1041 const GtkRcProperty *prop2 = bsearch_node2;
1043 if (prop1->type_name == prop2->type_name)
1044 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
1046 return prop1->type_name < prop2->type_name ? -1 : 1;
1050 insert_rc_property (GtkRcStyle *style,
1051 GtkRcProperty *property,
1055 GtkRcProperty *new_property = NULL;
1056 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
1058 key.type_name = property->type_name;
1059 key.property_name = property->property_name;
1061 if (!style->rc_properties)
1062 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
1065 while (i < style->rc_properties->len)
1067 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
1073 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1075 g_free (new_property->origin);
1076 g_value_unset (&new_property->value);
1078 *new_property = key;
1092 g_array_insert_val (style->rc_properties, i, key);
1093 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
1096 new_property->origin = g_strdup (property->origin);
1097 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
1098 g_value_copy (&property->value, &new_property->value);
1102 gtk_rc_style_real_merge (GtkRcStyle *dest,
1107 for (i = 0; i < 5; i++)
1109 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
1110 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
1112 if (!(dest->color_flags[i] & GTK_RC_FG) &&
1113 src->color_flags[i] & GTK_RC_FG)
1115 dest->fg[i] = src->fg[i];
1116 dest->color_flags[i] |= GTK_RC_FG;
1118 if (!(dest->color_flags[i] & GTK_RC_BG) &&
1119 src->color_flags[i] & GTK_RC_BG)
1121 dest->bg[i] = src->bg[i];
1122 dest->color_flags[i] |= GTK_RC_BG;
1124 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
1125 src->color_flags[i] & GTK_RC_TEXT)
1127 dest->text[i] = src->text[i];
1128 dest->color_flags[i] |= GTK_RC_TEXT;
1130 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
1131 src->color_flags[i] & GTK_RC_BASE)
1133 dest->base[i] = src->base[i];
1134 dest->color_flags[i] |= GTK_RC_BASE;
1138 if (dest->xthickness < 0 && src->xthickness >= 0)
1139 dest->xthickness = src->xthickness;
1140 if (dest->ythickness < 0 && src->ythickness >= 0)
1141 dest->ythickness = src->ythickness;
1145 if (!dest->font_desc)
1146 dest->font_desc = pango_font_description_copy (src->font_desc);
1148 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
1151 if (src->rc_properties)
1155 for (i = 0; i < src->rc_properties->len; i++)
1156 insert_rc_property (dest,
1157 &g_array_index (src->rc_properties, GtkRcProperty, i),
1163 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
1165 return gtk_style_new ();
1169 gtk_rc_clear_hash_node (gpointer key,
1173 gtk_rc_style_unref (data);
1177 gtk_rc_free_rc_sets (GSList *slist)
1183 rc_set = slist->data;
1184 g_pattern_spec_free (rc_set->pspec);
1187 slist = slist->next;
1192 gtk_rc_clear_styles (GtkRcContext *context)
1194 /* Clear out all old rc_styles */
1196 if (context->rc_style_ht)
1198 g_hash_table_foreach (context->rc_style_ht, gtk_rc_clear_hash_node, NULL);
1199 g_hash_table_destroy (context->rc_style_ht);
1200 context->rc_style_ht = NULL;
1203 gtk_rc_free_rc_sets (context->rc_sets_widget);
1204 g_slist_free (context->rc_sets_widget);
1205 context->rc_sets_widget = NULL;
1207 gtk_rc_free_rc_sets (context->rc_sets_widget_class);
1208 g_slist_free (context->rc_sets_widget_class);
1209 context->rc_sets_widget_class = NULL;
1211 gtk_rc_free_rc_sets (context->rc_sets_class);
1212 g_slist_free (context->rc_sets_class);
1213 context->rc_sets_class = NULL;
1216 /* Reset all our widgets. Also, we have to invalidate cached icons in
1217 * icon sets so they get re-rendered.
1220 gtk_rc_reset_widgets (GtkRcContext *context)
1222 GList *list, *toplevels;
1224 _gtk_icon_set_invalidate_caches ();
1226 toplevels = gtk_window_list_toplevels ();
1227 g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
1229 for (list = toplevels; list; list = list->next)
1231 gtk_widget_reset_rc_styles (list->data);
1232 gtk_widget_unref (list->data);
1234 g_list_free (toplevels);
1238 * gtk_rc_reparse_all_for_settings:
1239 * @settings: a #GtkSettings
1240 * @force_load: load whether or not anything changed
1242 * If the modification time on any previously read file
1243 * for the given #GtkSettings has changed, discard all style information
1244 * and then reread all previously read RC files.
1246 * Return value: %TRUE if the files were reread.
1249 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
1250 gboolean force_load)
1252 gboolean mtime_modified = FALSE;
1255 GtkRcContext *context;
1257 struct stat statbuf;
1259 g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);
1261 context = gtk_rc_context_get (settings);
1265 /* Check through and see if any of the RC's have had their
1266 * mtime modified. If so, reparse everything.
1268 tmp_list = context->rc_files;
1271 rc_file = tmp_list->data;
1273 if (!rc_file->is_string)
1275 if (!lstat (rc_file->name, &statbuf) &&
1276 (statbuf.st_mtime > rc_file->mtime))
1278 mtime_modified = TRUE;
1283 tmp_list = tmp_list->next;
1287 if (force_load || mtime_modified)
1291 _gtk_binding_reset_parsed ();
1292 gtk_rc_clear_styles (context);
1293 g_object_freeze_notify (G_OBJECT (context->settings));
1295 old_files = context->rc_files;
1296 context->rc_files = NULL;
1298 gtk_rc_parse_default_files (context);
1300 tmp_list = old_files;
1303 rc_file = tmp_list->data;
1304 if (rc_file->reload)
1306 if (rc_file->is_string)
1307 gtk_rc_parse_string (rc_file->name);
1309 gtk_rc_parse_file (context, rc_file->name, GTK_PATH_PRIO_RC, TRUE);
1312 if (rc_file->canonical_name != rc_file->name)
1313 g_free (rc_file->canonical_name);
1314 g_free (rc_file->name);
1317 tmp_list = tmp_list->next;
1320 g_slist_free (old_files);;
1322 g_free (context->theme_name);
1323 g_free (context->key_theme_name);
1324 g_object_get (context->settings,
1325 "gtk-theme-name", &context->theme_name,
1326 "gtk-key-theme-name", &context->key_theme_name,
1329 if (context->theme_name && context->theme_name[0])
1330 gtk_rc_parse_named (context, context->theme_name, NULL);
1331 if (context->key_theme_name && context->key_theme_name[0])
1332 gtk_rc_parse_named (context, context->key_theme_name, "key");
1334 g_object_thaw_notify (G_OBJECT (context->settings));
1336 gtk_rc_reset_widgets (context);
1339 return mtime_modified;
1343 * gtk_rc_reparse_all:
1345 * If the modification time on any previously read file for the
1346 * default #GtkSettings has changed, discard all style information
1347 * and then reread all previously read RC files.
1349 * Return value: %TRUE if the files were reread.
1352 gtk_rc_reparse_all (void)
1354 return gtk_rc_reparse_all_for_settings (gtk_settings_get_default (), FALSE);
1358 gtk_rc_styles_match (GSList *rc_styles,
1362 const gchar *path_reversed)
1369 rc_set = sets->data;
1372 if (g_pattern_match (rc_set->pspec, path_length, path, path_reversed))
1373 rc_styles = g_slist_append (rc_styles, rc_set);
1380 rc_set_compare (gconstpointer a, gconstpointer b)
1382 const GtkRcSet *set_a = a;
1383 const GtkRcSet *set_b = b;
1385 return (set_a->priority < set_b->priority) ? 1 : (set_a->priority == set_b->priority ? 0 : -1);
1389 sort_and_dereference_sets (GSList *styles)
1393 /* At this point, the list of sets is ordered by:
1395 * a) 'widget' patterns are earlier than 'widget_class' patterns
1396 * which are ealier than 'class' patterns.
1397 * a) For two matches for class patterns, a match to a child type
1398 * is before a match to a parent type
1399 * c) a match later in the RC file (or in a later RC file) is before a
1400 * match earlier in the RC file.
1402 * With a) taking precedence over b) which takes precendence over c).
1404 * Now sort by priority, which has the highest precendence for sort order
1406 styles = g_slist_sort (styles, rc_set_compare);
1408 /* Make styles->data = styles->data->rc_style
1413 GtkRcSet *set = tmp_list->data;
1414 tmp_list->data = set->rc_style;
1415 tmp_list = tmp_list->next;
1423 * @widget: a #GtkWidget
1425 * Finds all matching RC styles for a given widget,
1426 * composites them together, and then creates a
1427 * #GtkStyle representing the composite appearance.
1428 * (GTK+ actually keeps a cache of previously
1429 * created styles, so a new style may not be
1432 * Returns: the resulting style. No refcount is added
1433 * to the returned style, so if you want to save this
1434 * style around, you should add a reference yourself.
1437 gtk_rc_get_style (GtkWidget *widget)
1439 GtkRcStyle *widget_rc_style;
1440 GSList *rc_styles = NULL;
1441 GtkRcContext *context;
1443 static guint rc_style_key_id = 0;
1445 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
1447 context = gtk_rc_context_get (gtk_widget_get_settings (widget));
1449 /* We allow the specification of a single rc style to be bound
1450 * tightly to a widget, for application modifications
1452 if (!rc_style_key_id)
1453 rc_style_key_id = g_quark_from_static_string ("gtk-rc-style");
1455 if (context->rc_sets_widget)
1457 gchar *path, *path_reversed;
1460 gtk_widget_path (widget, &path_length, &path, &path_reversed);
1461 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, path, path_reversed);
1463 g_free (path_reversed);
1466 if (context->rc_sets_widget_class)
1468 gchar *path, *path_reversed;
1471 gtk_widget_class_path (widget, &path_length, &path, &path_reversed);
1472 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, path, path_reversed);
1474 g_free (path_reversed);
1477 if (context->rc_sets_class)
1481 type = GTK_OBJECT_TYPE (widget);
1485 gchar *path_reversed;
1488 path = gtk_type_name (type);
1489 path_length = strlen (path);
1490 path_reversed = g_strdup (path);
1491 g_strreverse (path_reversed);
1493 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1494 g_free (path_reversed);
1496 type = gtk_type_parent (type);
1500 rc_styles = sort_and_dereference_sets (rc_styles);
1502 widget_rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
1505 if (widget_rc_style)
1506 rc_styles = g_slist_prepend (rc_styles, widget_rc_style);
1509 return gtk_rc_init_style (rc_styles);
1515 * gtk_rc_get_style_by_paths:
1516 * @settings: a #GtkSettings object
1517 * @widget_path: the widget path to use when looking up the style, or %NULL
1518 * @class_path: the class path to use when looking up the style, or %NULL
1519 * @type: a type that will be used along with parent types of this type
1520 * when matching against class styles, or #G_TYPE_NONE
1522 * Creates up a #GtkStyle from styles defined in a RC file by providing
1523 * the raw components used in matching. This function may be useful
1524 * when creating pseudo-widgets that should be themed like widgets but
1525 * don't actually have corresponding GTK+ widgets. An example of this
1526 * would be items inside a GNOME canvas widget.
1528 * The action of gtk_rc_get_style() is similar to:
1529 * <informalexample><programlisting>
1530 * <!> gtk_widget_path (widget, NULL, &path, NULL);
1531 * <!> gtk_widget_class_path (widget, NULL, &class_path, NULL);
1532 * <!> gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget), path, class_path,
1533 * <!> G_OBJECT_TYPE (widget));
1534 * </programlisting></informalexample>
1536 * Return value: A style created by matching with the supplied paths,
1537 * or %NULL if nothing matching was specified and the default style should
1538 * be used. The returned value is owned by GTK+ as part of an internal cache,
1539 * so you must call g_object_ref() on the returned value if you want to
1540 * keep a reference to it.
1543 gtk_rc_get_style_by_paths (GtkSettings *settings,
1544 const char *widget_path,
1545 const char *class_path,
1548 /* We duplicate the code from above to avoid slowing down the above
1549 * by generating paths when we don't need them. I don't know if
1550 * this is really worth it.
1552 GSList *rc_styles = NULL;
1553 GtkRcContext *context;
1555 g_return_val_if_fail (GTK_IS_SETTINGS (settings), NULL);
1557 context = gtk_rc_context_get (settings);
1559 if (context->rc_sets_widget)
1561 gchar *path_reversed;
1564 path_length = strlen (widget_path);
1565 path_reversed = g_strdup (widget_path);
1566 g_strreverse (path_reversed);
1568 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget, path_length, widget_path, path_reversed);
1569 g_free (path_reversed);
1572 if (context->rc_sets_widget_class)
1574 gchar *path_reversed;
1577 path_length = strlen (class_path);
1578 path_reversed = g_strdup (class_path);
1579 g_strreverse (path_reversed);
1581 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_widget_class, path_length, class_path, path_reversed);
1582 g_free (path_reversed);
1585 if (type != G_TYPE_NONE && context->rc_sets_class)
1590 gchar *path_reversed;
1593 path = g_type_name (type);
1594 path_length = strlen (path);
1595 path_reversed = g_strdup (path);
1596 g_strreverse (path_reversed);
1598 rc_styles = gtk_rc_styles_match (rc_styles, context->rc_sets_class, path_length, path, path_reversed);
1599 g_free (path_reversed);
1601 type = g_type_parent (type);
1605 rc_styles = sort_and_dereference_sets (rc_styles);
1608 return gtk_rc_init_style (rc_styles);
1614 gtk_rc_add_rc_sets (GSList *slist,
1615 GtkRcStyle *rc_style,
1616 const gchar *pattern)
1618 GtkRcStyle *new_style;
1622 new_style = gtk_rc_style_new ();
1623 *new_style = *rc_style;
1624 new_style->name = g_strdup (rc_style->name);
1625 if (rc_style->font_desc)
1626 new_style->font_desc = pango_font_description_copy (rc_style->font_desc);
1628 for (i = 0; i < 5; i++)
1629 new_style->bg_pixmap_name[i] = g_strdup (rc_style->bg_pixmap_name[i]);
1631 rc_set = g_new (GtkRcSet, 1);
1632 rc_set->pspec = g_pattern_spec_new (pattern);
1633 rc_set->rc_style = rc_style;
1635 return g_slist_prepend (slist, rc_set);
1639 gtk_rc_add_widget_name_style (GtkRcStyle *rc_style,
1640 const gchar *pattern)
1642 GtkRcContext *context;
1644 g_return_if_fail (rc_style != NULL);
1645 g_return_if_fail (pattern != NULL);
1647 context = gtk_rc_context_get (gtk_settings_get_default ());
1649 context->rc_sets_widget = gtk_rc_add_rc_sets (context->rc_sets_widget, rc_style, pattern);
1653 gtk_rc_add_widget_class_style (GtkRcStyle *rc_style,
1654 const gchar *pattern)
1656 GtkRcContext *context;
1658 g_return_if_fail (rc_style != NULL);
1659 g_return_if_fail (pattern != NULL);
1661 context = gtk_rc_context_get (gtk_settings_get_default ());
1663 context->rc_sets_widget_class = gtk_rc_add_rc_sets (context->rc_sets_widget_class, rc_style, pattern);
1667 gtk_rc_add_class_style (GtkRcStyle *rc_style,
1668 const gchar *pattern)
1670 GtkRcContext *context;
1672 g_return_if_fail (rc_style != NULL);
1673 g_return_if_fail (pattern != NULL);
1675 context = gtk_rc_context_get (gtk_settings_get_default ());
1677 context->rc_sets_class = gtk_rc_add_rc_sets (context->rc_sets_class, rc_style, pattern);
1681 gtk_rc_scanner_new (void)
1683 return g_scanner_new (>k_rc_scanner_config);
1687 gtk_rc_parse_any (GtkRcContext *context,
1688 const gchar *input_name,
1690 const gchar *input_string)
1696 scanner = gtk_rc_scanner_new ();
1700 g_assert (input_string == NULL);
1702 g_scanner_input_file (scanner, input_fd);
1706 g_assert (input_string != NULL);
1708 g_scanner_input_text (scanner, input_string, strlen (input_string));
1710 scanner->input_name = input_name;
1712 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1713 g_scanner_scope_add_symbol (scanner, 0, symbols[i].name, GINT_TO_POINTER (symbols[i].token));
1718 if (g_scanner_peek_next_token (scanner) == G_TOKEN_EOF)
1722 guint expected_token;
1724 expected_token = gtk_rc_parse_statement (context, scanner);
1726 if (expected_token != G_TOKEN_NONE)
1733 if (scanner->scope_id == 0)
1735 /* if we are in scope 0, we know the symbol names
1736 * that are associated with certain token values.
1737 * so we look them up to make the error messages
1740 if (expected_token > GTK_RC_TOKEN_INVALID &&
1741 expected_token < GTK_RC_TOKEN_LAST)
1743 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1744 if (symbols[i].token == expected_token)
1745 msg = symbols[i].name;
1747 msg = g_strconcat ("e.g. `", msg, "'", NULL);
1749 if (scanner->token > GTK_RC_TOKEN_INVALID &&
1750 scanner->token < GTK_RC_TOKEN_LAST)
1752 symbol_name = "???";
1753 for (i = 0; i < G_N_ELEMENTS (symbols); i++)
1754 if (symbols[i].token == scanner->token)
1755 symbol_name = symbols[i].name;
1758 g_scanner_unexp_token (scanner,
1771 g_scanner_destroy (scanner);
1775 gtk_rc_styles_hash (const GSList *rc_styles)
1782 result += (result << 9) + GPOINTER_TO_UINT (rc_styles->data);
1783 rc_styles = rc_styles->next;
1790 gtk_rc_styles_equal (const GSList *a,
1795 if (a->data != b->data)
1805 gtk_rc_style_hash (const gchar *name)
1811 result += (result << 3) + *name++;
1817 gtk_rc_style_equal (const gchar *a,
1820 return (strcmp (a, b) == 0);
1824 gtk_rc_style_find (GtkRcContext *context,
1827 if (context->rc_style_ht)
1828 return g_hash_table_lookup (context->rc_style_ht, (gpointer) name);
1834 gtk_rc_style_to_style (GtkRcStyle *rc_style)
1838 style = GTK_RC_STYLE_GET_CLASS (rc_style)->create_style (rc_style);
1840 style->rc_style = rc_style;
1842 gtk_rc_style_ref (rc_style);
1844 GTK_STYLE_GET_CLASS (style)->init_from_rc (style, rc_style);
1849 /* Reuses or frees rc_styles */
1851 gtk_rc_init_style (GSList *rc_styles)
1853 GtkStyle *style = NULL;
1856 g_return_val_if_fail (rc_styles != NULL, NULL);
1858 if (!realized_style_ht)
1859 realized_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_styles_hash,
1860 (GEqualFunc) gtk_rc_styles_equal);
1862 style = g_hash_table_lookup (realized_style_ht, rc_styles);
1866 GtkRcStyle *base_style = NULL;
1867 GtkRcStyle *proto_style;
1868 GtkRcStyleClass *proto_style_class;
1870 GType rc_style_type = GTK_TYPE_RC_STYLE;
1872 /* Find the the first style where the RC file specified engine "" {}
1873 * or the first derived style and use that to create the
1874 * merged style. If we only have raw GtkRcStyles, use the first
1875 * style to create the merged style.
1877 base_style = rc_styles->data;
1878 tmp_styles = rc_styles;
1881 GtkRcStyle *rc_style = tmp_styles->data;
1883 if (rc_style->engine_specified ||
1884 G_OBJECT_TYPE (rc_style) != rc_style_type)
1886 base_style = rc_style;
1890 tmp_styles = tmp_styles->next;
1893 proto_style_class = GTK_RC_STYLE_GET_CLASS (base_style);
1894 proto_style = proto_style_class->create_rc_style (base_style);
1896 tmp_styles = rc_styles;
1899 GtkRcStyle *rc_style = tmp_styles->data;
1902 proto_style_class->merge (proto_style, rc_style);
1904 /* Point from each rc_style to the list of styles */
1905 if (!g_slist_find (rc_style->rc_style_lists, rc_styles))
1906 rc_style->rc_style_lists = g_slist_prepend (rc_style->rc_style_lists, rc_styles);
1908 factories = g_slist_copy (rc_style->icon_factories);
1914 while (iter != NULL)
1916 g_object_ref (G_OBJECT (iter->data));
1917 iter = g_slist_next (iter);
1920 proto_style->icon_factories = g_slist_concat (proto_style->icon_factories,
1925 tmp_styles = tmp_styles->next;
1928 for (i = 0; i < 5; i++)
1929 if (proto_style->bg_pixmap_name[i] &&
1930 (strcmp (proto_style->bg_pixmap_name[i], "<none>") == 0))
1932 g_free (proto_style->bg_pixmap_name[i]);
1933 proto_style->bg_pixmap_name[i] = NULL;
1936 style = gtk_rc_style_to_style (proto_style);
1937 gtk_rc_style_unref (proto_style);
1939 g_hash_table_insert (realized_style_ht, rc_styles, style);
1942 g_slist_free (rc_styles);
1947 /*********************
1948 * Parsing functions *
1949 *********************/
1952 rc_parse_token_or_compound (GScanner *scanner,
1954 GTokenType delimiter)
1956 guint token = g_scanner_get_next_token (scanner);
1958 /* we either scan a single token (skipping comments)
1959 * or a compund statement.
1960 * compunds are enclosed in (), [] or {} braces, we read
1961 * them in via deep recursion.
1968 g_string_append_printf (gstring, " 0x%lx", scanner->value.v_int);
1971 g_string_append_printf (gstring, " %f", scanner->value.v_float);
1973 case G_TOKEN_STRING:
1974 string = g_strescape (scanner->value.v_string, NULL);
1975 g_string_append (gstring, " \"");
1976 g_string_append (gstring, string);
1977 g_string_append_c (gstring, '"');
1980 case G_TOKEN_IDENTIFIER:
1981 g_string_append_c (gstring, ' ');
1982 g_string_append (gstring, scanner->value.v_identifier);
1984 case G_TOKEN_COMMENT_SINGLE:
1985 case G_TOKEN_COMMENT_MULTI:
1986 return rc_parse_token_or_compound (scanner, gstring, delimiter);
1987 case G_TOKEN_LEFT_PAREN:
1988 g_string_append_c (gstring, ' ');
1989 g_string_append_c (gstring, token);
1990 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_PAREN);
1991 if (token != G_TOKEN_NONE)
1994 case G_TOKEN_LEFT_CURLY:
1995 g_string_append_c (gstring, ' ');
1996 g_string_append_c (gstring, token);
1997 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_CURLY);
1998 if (token != G_TOKEN_NONE)
2001 case G_TOKEN_LEFT_BRACE:
2002 g_string_append_c (gstring, ' ');
2003 g_string_append_c (gstring, token);
2004 token = rc_parse_token_or_compound (scanner, gstring, G_TOKEN_RIGHT_BRACE);
2005 if (token != G_TOKEN_NONE)
2009 if (token >= 256 || token < 1)
2010 return delimiter ? delimiter : G_TOKEN_STRING;
2011 g_string_append_c (gstring, ' ');
2012 g_string_append_c (gstring, token);
2013 if (token == delimiter)
2014 return G_TOKEN_NONE;
2018 return G_TOKEN_NONE;
2020 return rc_parse_token_or_compound (scanner, gstring, delimiter);
2024 gtk_rc_parse_assignment (GScanner *scanner,
2025 GtkRcProperty *prop)
2027 gboolean scan_identifier = scanner->config->scan_identifier;
2028 gboolean scan_symbols = scanner->config->scan_symbols;
2029 gboolean identifier_2_string = scanner->config->identifier_2_string;
2030 gboolean char_2_token = scanner->config->char_2_token;
2031 gboolean scan_identifier_NULL = scanner->config->scan_identifier_NULL;
2032 gboolean numbers_2_int = scanner->config->numbers_2_int;
2033 gboolean negate = FALSE;
2036 /* check that this is an assignment */
2037 if (g_scanner_get_next_token (scanner) != '=')
2040 /* adjust scanner mode */
2041 scanner->config->scan_identifier = TRUE;
2042 scanner->config->scan_symbols = FALSE;
2043 scanner->config->identifier_2_string = FALSE;
2044 scanner->config->char_2_token = TRUE;
2045 scanner->config->scan_identifier_NULL = FALSE;
2046 scanner->config->numbers_2_int = TRUE;
2048 /* record location */
2049 prop->origin = g_strdup_printf ("%s:%u", scanner->input_name, scanner->line);
2051 /* parse optional sign */
2052 if (g_scanner_peek_next_token (scanner) == '-')
2054 g_scanner_get_next_token (scanner); /* eat sign */
2058 /* parse one of LONG, DOUBLE and STRING or, if that fails, create an unparsed compund */
2059 token = g_scanner_peek_next_token (scanner);
2063 g_scanner_get_next_token (scanner);
2064 g_value_init (&prop->value, G_TYPE_LONG);
2065 g_value_set_long (&prop->value, negate ? -scanner->value.v_int : scanner->value.v_int);
2066 token = G_TOKEN_NONE;
2069 g_scanner_get_next_token (scanner);
2070 g_value_init (&prop->value, G_TYPE_DOUBLE);
2071 g_value_set_double (&prop->value, negate ? -scanner->value.v_float : scanner->value.v_float);
2072 token = G_TOKEN_NONE;
2074 case G_TOKEN_STRING:
2075 g_scanner_get_next_token (scanner);
2077 token = G_TOKEN_INT;
2080 g_value_init (&prop->value, G_TYPE_STRING);
2081 g_value_set_string (&prop->value, scanner->value.v_string);
2082 token = G_TOKEN_NONE;
2085 case G_TOKEN_IDENTIFIER:
2086 case G_TOKEN_LEFT_PAREN:
2087 case G_TOKEN_LEFT_CURLY:
2088 case G_TOKEN_LEFT_BRACE:
2091 GString *gstring = g_string_new ("");
2093 token = rc_parse_token_or_compound (scanner, gstring, 0);
2094 if (token == G_TOKEN_NONE)
2096 g_string_append_c (gstring, ' ');
2097 g_value_init (&prop->value, G_TYPE_GSTRING);
2098 g_value_set_static_boxed (&prop->value, gstring);
2101 g_string_free (gstring, TRUE);
2106 g_scanner_get_next_token (scanner);
2107 token = G_TOKEN_INT;
2111 /* restore scanner mode */
2112 scanner->config->scan_identifier = scan_identifier;
2113 scanner->config->scan_symbols = scan_symbols;
2114 scanner->config->identifier_2_string = identifier_2_string;
2115 scanner->config->char_2_token = char_2_token;
2116 scanner->config->scan_identifier_NULL = scan_identifier_NULL;
2117 scanner->config->numbers_2_int = numbers_2_int;
2123 is_c_identifier (const gchar *string)
2126 gboolean is_varname;
2128 is_varname = strchr (G_CSET_a_2_z G_CSET_A_2_Z "_", string[0]) != NULL;
2129 for (p = string + 1; *p && is_varname; p++)
2130 is_varname &= strchr (G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_-", *p) != NULL;
2136 parse_include_file (GtkRcContext *context,
2138 const gchar *filename)
2140 char *to_parse = NULL;
2142 if (g_path_is_absolute (filename))
2144 /* For abolute paths, we call gtk_rc_parse_file unconditionally. We
2145 * don't print an error in this case.
2147 to_parse = g_strdup (filename);
2151 /* if a relative path, we look relative to all the RC files in the
2152 * include stack. We require the file to be found in this case
2153 * so we can give meaningful error messages, and because on reparsing
2154 * non-absolute paths don't make sense.
2156 GSList *tmp_list = rc_dir_stack;
2159 gchar *tmpname = g_build_filename (tmp_list->data, filename, NULL);
2161 if (g_file_test (tmpname, G_FILE_TEST_EXISTS))
2169 tmp_list = tmp_list->next;
2175 gtk_rc_parse_file (context, to_parse, context->default_priority, FALSE);
2180 g_scanner_warn (scanner,
2181 _("Unable to find include file: \"%s\""),
2188 gtk_rc_parse_statement (GtkRcContext *context,
2193 token = g_scanner_peek_next_token (scanner);
2196 case GTK_RC_TOKEN_INCLUDE:
2197 token = g_scanner_get_next_token (scanner);
2198 if (token != GTK_RC_TOKEN_INCLUDE)
2199 return GTK_RC_TOKEN_INCLUDE;
2200 token = g_scanner_get_next_token (scanner);
2201 if (token != G_TOKEN_STRING)
2202 return G_TOKEN_STRING;
2203 parse_include_file (context, scanner, scanner->value.v_string);
2204 return G_TOKEN_NONE;
2206 case GTK_RC_TOKEN_STYLE:
2207 return gtk_rc_parse_style (context, scanner);
2209 case GTK_RC_TOKEN_BINDING:
2210 return gtk_binding_parse_binding (scanner);
2212 case GTK_RC_TOKEN_PIXMAP_PATH:
2213 return gtk_rc_parse_pixmap_path (context, scanner);
2215 case GTK_RC_TOKEN_WIDGET:
2216 return gtk_rc_parse_path_pattern (context, scanner);
2218 case GTK_RC_TOKEN_WIDGET_CLASS:
2219 return gtk_rc_parse_path_pattern (context, scanner);
2221 case GTK_RC_TOKEN_CLASS:
2222 return gtk_rc_parse_path_pattern (context, scanner);
2224 case GTK_RC_TOKEN_MODULE_PATH:
2225 return gtk_rc_parse_module_path (scanner);
2227 case GTK_RC_TOKEN_IM_MODULE_FILE:
2228 return gtk_rc_parse_im_module_file (scanner);
2230 case G_TOKEN_IDENTIFIER:
2231 if (is_c_identifier (scanner->next_value.v_identifier))
2233 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2236 g_scanner_get_next_token (scanner); /* eat identifier */
2237 name = g_strdup (scanner->value.v_identifier);
2239 token = gtk_rc_parse_assignment (scanner, &prop);
2240 if (token == G_TOKEN_NONE)
2242 GtkSettingsValue svalue;
2244 svalue.origin = prop.origin;
2245 memcpy (&svalue.value, &prop.value, sizeof (prop.value));
2246 g_strcanon (name, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2247 gtk_settings_set_property_value (context->settings,
2251 g_free (prop.origin);
2252 if (G_VALUE_TYPE (&prop.value))
2253 g_value_unset (&prop.value);
2260 g_scanner_get_next_token (scanner);
2261 return G_TOKEN_IDENTIFIER;
2264 g_scanner_get_next_token (scanner);
2265 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_STYLE;
2270 fixup_rc_set (GSList *list,
2276 GtkRcSet *set = list->data;
2277 if (set->rc_style == orig)
2278 set->rc_style = new;
2284 fixup_rc_sets (GtkRcContext *context,
2288 fixup_rc_set (context->rc_sets_widget, orig, new);
2289 fixup_rc_set (context->rc_sets_widget_class, orig, new);
2290 fixup_rc_set (context->rc_sets_class, orig, new);
2294 gtk_rc_parse_style (GtkRcContext *context,
2297 GtkRcStyle *rc_style;
2298 GtkRcStyle *orig_style;
2299 GtkRcStyle *parent_style;
2302 GtkIconFactory *our_factory = NULL;
2304 token = g_scanner_get_next_token (scanner);
2305 if (token != GTK_RC_TOKEN_STYLE)
2306 return GTK_RC_TOKEN_STYLE;
2308 token = g_scanner_get_next_token (scanner);
2309 if (token != G_TOKEN_STRING)
2310 return G_TOKEN_STRING;
2312 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
2314 orig_style = g_object_ref (rc_style);
2318 /* If there's a list, its first member is always the factory belonging
2321 if (rc_style && rc_style->icon_factories)
2322 our_factory = rc_style->icon_factories->data;
2326 rc_style = gtk_rc_style_new ();
2327 rc_style->name = g_strdup (scanner->value.v_string);
2329 for (i = 0; i < 5; i++)
2330 rc_style->bg_pixmap_name[i] = NULL;
2332 for (i = 0; i < 5; i++)
2333 rc_style->color_flags[i] = 0;
2336 token = g_scanner_peek_next_token (scanner);
2337 if (token == G_TOKEN_EQUAL_SIGN)
2339 token = g_scanner_get_next_token (scanner);
2341 token = g_scanner_get_next_token (scanner);
2342 if (token != G_TOKEN_STRING)
2344 token = G_TOKEN_STRING;
2348 parent_style = gtk_rc_style_find (context, scanner->value.v_string);
2353 for (i = 0; i < 5; i++)
2355 rc_style->color_flags[i] = parent_style->color_flags[i];
2356 rc_style->fg[i] = parent_style->fg[i];
2357 rc_style->bg[i] = parent_style->bg[i];
2358 rc_style->text[i] = parent_style->text[i];
2359 rc_style->base[i] = parent_style->base[i];
2362 rc_style->xthickness = parent_style->xthickness;
2363 rc_style->ythickness = parent_style->ythickness;
2365 if (parent_style->font_desc)
2367 if (rc_style->font_desc)
2368 pango_font_description_free (rc_style->font_desc);
2369 rc_style->font_desc = pango_font_description_copy (parent_style->font_desc);
2372 if (parent_style->rc_properties)
2376 for (i = 0; i < parent_style->rc_properties->len; i++)
2377 insert_rc_property (rc_style,
2378 &g_array_index (parent_style->rc_properties, GtkRcProperty, i),
2382 for (i = 0; i < 5; i++)
2384 if (rc_style->bg_pixmap_name[i])
2385 g_free (rc_style->bg_pixmap_name[i]);
2386 rc_style->bg_pixmap_name[i] = g_strdup (parent_style->bg_pixmap_name[i]);
2389 /* Append parent's factories, adding a ref to them */
2390 if (parent_style->icon_factories != NULL)
2392 /* Add a factory for ourselves if we have none,
2393 * in case we end up defining more stock icons.
2394 * I see no real way around this; we need to maintain
2395 * the invariant that the first factory in the list
2396 * is always our_factory, the one belonging to us,
2397 * and if we put parent factories in the list we can't
2398 * do that if the style is reopened.
2400 if (our_factory == NULL)
2402 our_factory = gtk_icon_factory_new ();
2403 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2407 rc_style->icon_factories = g_slist_concat (rc_style->icon_factories,
2408 g_slist_copy (parent_style->icon_factories));
2410 factories = parent_style->icon_factories;
2411 while (factories != NULL)
2413 g_object_ref (G_OBJECT (factories->data));
2414 factories = factories->next;
2420 token = g_scanner_get_next_token (scanner);
2421 if (token != G_TOKEN_LEFT_CURLY)
2423 token = G_TOKEN_LEFT_CURLY;
2427 token = g_scanner_peek_next_token (scanner);
2428 while (token != G_TOKEN_RIGHT_CURLY)
2432 case GTK_RC_TOKEN_BG:
2433 token = gtk_rc_parse_bg (scanner, rc_style);
2435 case GTK_RC_TOKEN_FG:
2436 token = gtk_rc_parse_fg (scanner, rc_style);
2438 case GTK_RC_TOKEN_TEXT:
2439 token = gtk_rc_parse_text (scanner, rc_style);
2441 case GTK_RC_TOKEN_BASE:
2442 token = gtk_rc_parse_base (scanner, rc_style);
2444 case GTK_RC_TOKEN_XTHICKNESS:
2445 token = gtk_rc_parse_xthickness (scanner, rc_style);
2447 case GTK_RC_TOKEN_YTHICKNESS:
2448 token = gtk_rc_parse_ythickness (scanner, rc_style);
2450 case GTK_RC_TOKEN_BG_PIXMAP:
2451 token = gtk_rc_parse_bg_pixmap (context, scanner, rc_style);
2453 case GTK_RC_TOKEN_FONT:
2454 token = gtk_rc_parse_font (scanner, rc_style);
2456 case GTK_RC_TOKEN_FONTSET:
2457 token = gtk_rc_parse_fontset (scanner, rc_style);
2459 case GTK_RC_TOKEN_FONT_NAME:
2460 token = gtk_rc_parse_font_name (scanner, rc_style);
2462 case GTK_RC_TOKEN_ENGINE:
2463 token = gtk_rc_parse_engine (context, scanner, &rc_style);
2465 case GTK_RC_TOKEN_STOCK:
2466 if (our_factory == NULL)
2468 our_factory = gtk_icon_factory_new ();
2469 rc_style->icon_factories = g_slist_prepend (rc_style->icon_factories,
2472 token = gtk_rc_parse_stock (context, scanner, rc_style, our_factory);
2474 case G_TOKEN_IDENTIFIER:
2475 if (is_c_identifier (scanner->next_value.v_identifier) &&
2476 scanner->next_value.v_identifier[0] >= 'A' &&
2477 scanner->next_value.v_identifier[0] <= 'Z') /* match namespaced type names */
2479 GtkRcProperty prop = { 0, 0, NULL, { 0, }, };
2481 g_scanner_get_next_token (scanner); /* eat type name */
2482 prop.type_name = g_quark_from_string (scanner->value.v_identifier);
2483 if (g_scanner_get_next_token (scanner) != ':' ||
2484 g_scanner_get_next_token (scanner) != ':')
2489 if (g_scanner_get_next_token (scanner) != G_TOKEN_IDENTIFIER ||
2490 !is_c_identifier (scanner->value.v_identifier))
2492 token = G_TOKEN_IDENTIFIER;
2496 /* it's important that we do the same canonification as GParamSpecPool here */
2497 g_strcanon (scanner->value.v_identifier, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
2498 prop.property_name = g_quark_from_string (scanner->value.v_identifier);
2500 token = gtk_rc_parse_assignment (scanner, &prop);
2501 if (token == G_TOKEN_NONE)
2503 g_return_val_if_fail (prop.origin != NULL && G_VALUE_TYPE (&prop.value) != 0, G_TOKEN_ERROR);
2504 insert_rc_property (rc_style, &prop, TRUE);
2507 g_free (prop.origin);
2508 if (G_VALUE_TYPE (&prop.value))
2509 g_value_unset (&prop.value);
2513 g_scanner_get_next_token (scanner);
2514 token = G_TOKEN_IDENTIFIER;
2518 g_scanner_get_next_token (scanner);
2519 token = G_TOKEN_RIGHT_CURLY;
2523 if (token != G_TOKEN_NONE)
2526 token = g_scanner_peek_next_token (scanner);
2527 } /* while (token != G_TOKEN_RIGHT_CURLY) */
2529 token = g_scanner_get_next_token (scanner);
2530 if (token != G_TOKEN_RIGHT_CURLY)
2532 token = G_TOKEN_RIGHT_CURLY;
2536 if (rc_style != orig_style)
2538 if (!context->rc_style_ht)
2539 context->rc_style_ht = g_hash_table_new ((GHashFunc) gtk_rc_style_hash,
2540 (GEqualFunc) gtk_rc_style_equal);
2542 g_hash_table_replace (context->rc_style_ht, rc_style->name, rc_style);
2544 /* If we copied the data into a new rc style, fix up references to the old rc style
2545 * in bindings that we have.
2548 fixup_rc_sets (context, orig_style, rc_style);
2552 g_object_unref (orig_style);
2554 return G_TOKEN_NONE;
2557 if (rc_style != orig_style)
2558 gtk_rc_style_unref (rc_style);
2561 g_object_unref (orig_style);
2566 const GtkRcProperty*
2567 _gtk_rc_style_lookup_rc_property (GtkRcStyle *rc_style,
2569 GQuark property_name)
2571 GtkRcProperty *node = NULL;
2573 g_return_val_if_fail (GTK_IS_RC_STYLE (rc_style), NULL);
2575 if (rc_style->rc_properties)
2579 key.type_name = type_name;
2580 key.property_name = property_name;
2582 node = bsearch (&key,
2583 rc_style->rc_properties->data, rc_style->rc_properties->len,
2584 sizeof (GtkRcProperty), gtk_rc_properties_cmp);
2591 gtk_rc_parse_bg (GScanner *scanner,
2597 token = g_scanner_get_next_token (scanner);
2598 if (token != GTK_RC_TOKEN_BG)
2599 return GTK_RC_TOKEN_BG;
2601 token = gtk_rc_parse_state (scanner, &state);
2602 if (token != G_TOKEN_NONE)
2605 token = g_scanner_get_next_token (scanner);
2606 if (token != G_TOKEN_EQUAL_SIGN)
2607 return G_TOKEN_EQUAL_SIGN;
2609 style->color_flags[state] |= GTK_RC_BG;
2610 return gtk_rc_parse_color (scanner, &style->bg[state]);
2614 gtk_rc_parse_fg (GScanner *scanner,
2620 token = g_scanner_get_next_token (scanner);
2621 if (token != GTK_RC_TOKEN_FG)
2622 return GTK_RC_TOKEN_FG;
2624 token = gtk_rc_parse_state (scanner, &state);
2625 if (token != G_TOKEN_NONE)
2628 token = g_scanner_get_next_token (scanner);
2629 if (token != G_TOKEN_EQUAL_SIGN)
2630 return G_TOKEN_EQUAL_SIGN;
2632 style->color_flags[state] |= GTK_RC_FG;
2633 return gtk_rc_parse_color (scanner, &style->fg[state]);
2637 gtk_rc_parse_text (GScanner *scanner,
2643 token = g_scanner_get_next_token (scanner);
2644 if (token != GTK_RC_TOKEN_TEXT)
2645 return GTK_RC_TOKEN_TEXT;
2647 token = gtk_rc_parse_state (scanner, &state);
2648 if (token != G_TOKEN_NONE)
2651 token = g_scanner_get_next_token (scanner);
2652 if (token != G_TOKEN_EQUAL_SIGN)
2653 return G_TOKEN_EQUAL_SIGN;
2655 style->color_flags[state] |= GTK_RC_TEXT;
2656 return gtk_rc_parse_color (scanner, &style->text[state]);
2660 gtk_rc_parse_base (GScanner *scanner,
2666 token = g_scanner_get_next_token (scanner);
2667 if (token != GTK_RC_TOKEN_BASE)
2668 return GTK_RC_TOKEN_BASE;
2670 token = gtk_rc_parse_state (scanner, &state);
2671 if (token != G_TOKEN_NONE)
2674 token = g_scanner_get_next_token (scanner);
2675 if (token != G_TOKEN_EQUAL_SIGN)
2676 return G_TOKEN_EQUAL_SIGN;
2678 style->color_flags[state] |= GTK_RC_BASE;
2679 return gtk_rc_parse_color (scanner, &style->base[state]);
2683 gtk_rc_parse_xthickness (GScanner *scanner,
2686 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_XTHICKNESS)
2687 return GTK_RC_TOKEN_XTHICKNESS;
2689 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2690 return G_TOKEN_EQUAL_SIGN;
2692 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2695 style->xthickness = scanner->value.v_int;
2697 return G_TOKEN_NONE;
2701 gtk_rc_parse_ythickness (GScanner *scanner,
2704 if (g_scanner_get_next_token (scanner) != GTK_RC_TOKEN_YTHICKNESS)
2705 return GTK_RC_TOKEN_YTHICKNESS;
2707 if (g_scanner_get_next_token (scanner) != G_TOKEN_EQUAL_SIGN)
2708 return G_TOKEN_EQUAL_SIGN;
2710 if (g_scanner_get_next_token (scanner) != G_TOKEN_INT)
2713 style->ythickness = scanner->value.v_int;
2715 return G_TOKEN_NONE;
2719 gtk_rc_parse_bg_pixmap (GtkRcContext *context,
2721 GtkRcStyle *rc_style)
2727 token = g_scanner_get_next_token (scanner);
2728 if (token != GTK_RC_TOKEN_BG_PIXMAP)
2729 return GTK_RC_TOKEN_BG_PIXMAP;
2731 token = gtk_rc_parse_state (scanner, &state);
2732 if (token != G_TOKEN_NONE)
2735 token = g_scanner_get_next_token (scanner);
2736 if (token != G_TOKEN_EQUAL_SIGN)
2737 return G_TOKEN_EQUAL_SIGN;
2739 token = g_scanner_get_next_token (scanner);
2740 if (token != G_TOKEN_STRING)
2741 return G_TOKEN_STRING;
2743 if ((strcmp (scanner->value.v_string, "<parent>") == 0) ||
2744 (strcmp (scanner->value.v_string, "<none>") == 0))
2745 pixmap_file = g_strdup (scanner->value.v_string);
2747 pixmap_file = gtk_rc_find_pixmap_in_path (context->settings,
2748 scanner, scanner->value.v_string);
2752 if (rc_style->bg_pixmap_name[state])
2753 g_free (rc_style->bg_pixmap_name[state]);
2754 rc_style->bg_pixmap_name[state] = pixmap_file;
2757 return G_TOKEN_NONE;
2761 gtk_rc_check_pixmap_dir (const gchar *dir, const gchar *pixmap_file)
2766 buf = g_build_filename (dir, pixmap_file, NULL);
2768 fd = open (buf, O_RDONLY);
2781 * gtk_rc_find_pixmap_in_path:
2782 * @settings: a #GtkSettings
2783 * @scanner: Scanner used to get line number information for the
2784 * warning message, or %NULL
2785 * @pixmap_file: name of the pixmap file to locate.
2787 * Looks up a file in pixmap path for the specified #GtkSettings.
2788 * If the file is not found, it outputs a warning message using
2789 * g_warning() and returns %NULL.
2791 * Return value: the filename.
2794 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
2796 const gchar *pixmap_file)
2802 GtkRcContext *context = gtk_rc_context_get (settings);
2804 for (i = 0; (i < GTK_RC_MAX_PIXMAP_PATHS) && (context->pixmap_path[i] != NULL); i++)
2806 filename = gtk_rc_check_pixmap_dir (context->pixmap_path[i], pixmap_file);
2811 tmp_list = rc_dir_stack;
2814 filename = gtk_rc_check_pixmap_dir (tmp_list->data, pixmap_file);
2818 tmp_list = tmp_list->next;
2822 g_scanner_warn (scanner,
2823 _("Unable to locate image file in pixmap_path: \"%s\""),
2826 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
2833 * gtk_rc_find_module_in_path:
2834 * @module_file: name of a theme engine
2836 * Searches for a theme engine in the GTK+ search path. This function
2837 * is not useful for applications and should not be used.
2839 * Return value: The filename, if found (must be freed with g_free()),
2843 gtk_rc_find_module_in_path (const gchar *module_file)
2845 return _gtk_find_module (module_file, "engines");
2849 gtk_rc_parse_font (GScanner *scanner,
2850 GtkRcStyle *rc_style)
2854 token = g_scanner_get_next_token (scanner);
2855 if (token != GTK_RC_TOKEN_FONT)
2856 return GTK_RC_TOKEN_FONT;
2858 token = g_scanner_get_next_token (scanner);
2859 if (token != G_TOKEN_EQUAL_SIGN)
2860 return G_TOKEN_EQUAL_SIGN;
2862 token = g_scanner_get_next_token (scanner);
2863 if (token != G_TOKEN_STRING)
2864 return G_TOKEN_STRING;
2866 /* Ignore, do nothing */
2868 return G_TOKEN_NONE;
2872 gtk_rc_parse_fontset (GScanner *scanner,
2873 GtkRcStyle *rc_style)
2877 token = g_scanner_get_next_token (scanner);
2878 if (token != GTK_RC_TOKEN_FONTSET)
2879 return GTK_RC_TOKEN_FONTSET;
2881 token = g_scanner_get_next_token (scanner);
2882 if (token != G_TOKEN_EQUAL_SIGN)
2883 return G_TOKEN_EQUAL_SIGN;
2885 token = g_scanner_get_next_token (scanner);
2886 if (token != G_TOKEN_STRING)
2887 return G_TOKEN_STRING;
2889 /* Do nothing - silently ignore */
2891 return G_TOKEN_NONE;
2895 gtk_rc_parse_font_name (GScanner *scanner,
2896 GtkRcStyle *rc_style)
2900 token = g_scanner_get_next_token (scanner);
2901 if (token != GTK_RC_TOKEN_FONT_NAME)
2902 return GTK_RC_TOKEN_FONT;
2904 token = g_scanner_get_next_token (scanner);
2905 if (token != G_TOKEN_EQUAL_SIGN)
2906 return G_TOKEN_EQUAL_SIGN;
2908 token = g_scanner_get_next_token (scanner);
2909 if (token != G_TOKEN_STRING)
2910 return G_TOKEN_STRING;
2912 rc_style->font_desc = pango_font_description_from_string (scanner->value.v_string);
2914 return G_TOKEN_NONE;
2918 gtk_rc_parse_engine (GtkRcContext *context,
2920 GtkRcStyle **rc_style)
2923 GtkThemeEngine *engine;
2924 guint result = G_TOKEN_NONE;
2925 GtkRcStyle *new_style = NULL;
2926 gboolean parsed_curlies = FALSE;
2928 token = g_scanner_get_next_token (scanner);
2929 if (token != GTK_RC_TOKEN_ENGINE)
2930 return GTK_RC_TOKEN_ENGINE;
2932 token = g_scanner_get_next_token (scanner);
2933 if (token != G_TOKEN_STRING)
2934 return G_TOKEN_STRING;
2936 if (!scanner->value.v_string[0])
2938 /* Support engine "" {} to mean override to the default engine
2940 token = g_scanner_get_next_token (scanner);
2941 if (token != G_TOKEN_LEFT_CURLY)
2942 return G_TOKEN_LEFT_CURLY;
2944 token = g_scanner_get_next_token (scanner);
2945 if (token != G_TOKEN_RIGHT_CURLY)
2946 return G_TOKEN_RIGHT_CURLY;
2948 parsed_curlies = TRUE;
2950 if (G_OBJECT_TYPE (*rc_style) != GTK_TYPE_RC_STYLE)
2952 new_style = gtk_rc_style_new ();
2953 gtk_rc_style_real_merge (new_style, *rc_style);
2955 if ((*rc_style)->name)
2956 new_style->name = g_strdup ((*rc_style)->name);
2959 (*rc_style)->engine_specified = TRUE;
2963 engine = gtk_theme_engine_get (scanner->value.v_string);
2965 token = g_scanner_get_next_token (scanner);
2966 if (token != G_TOKEN_LEFT_CURLY)
2967 return G_TOKEN_LEFT_CURLY;
2971 GtkRcStyleClass *new_class;
2973 new_style = gtk_theme_engine_create_rc_style (engine);
2974 g_type_module_unuse (G_TYPE_MODULE (engine));
2976 new_class = GTK_RC_STYLE_GET_CLASS (new_style);
2978 new_class->merge (new_style, *rc_style);
2979 if ((*rc_style)->name)
2980 new_style->name = g_strdup ((*rc_style)->name);
2982 if (new_class->parse)
2984 parsed_curlies = TRUE;
2985 result = new_class->parse (new_style, context->settings, scanner);
2987 if (result != G_TOKEN_NONE)
2989 g_object_unref (G_OBJECT (new_style));
2996 if (!parsed_curlies)
2998 /* Skip over remainder, looking for nested {}'s
3002 result = G_TOKEN_RIGHT_CURLY;
3003 while ((token = g_scanner_get_next_token (scanner)) != G_TOKEN_EOF)
3005 if (token == G_TOKEN_LEFT_CURLY)
3007 else if (token == G_TOKEN_RIGHT_CURLY)
3012 result = G_TOKEN_NONE;
3020 new_style->engine_specified = TRUE;
3022 g_object_unref (G_OBJECT (*rc_style));
3023 *rc_style = new_style;
3030 gtk_rc_parse_state (GScanner *scanner,
3031 GtkStateType *state)
3036 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3037 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
3039 /* we don't know where we got called from, so we reset the scope here.
3040 * if we bail out due to errors, we *don't* reset the scope, so the
3041 * error messaging code can make sense of our tokens.
3043 old_scope = g_scanner_set_scope (scanner, 0);
3045 token = g_scanner_get_next_token (scanner);
3046 if (token != G_TOKEN_LEFT_BRACE)
3047 return G_TOKEN_LEFT_BRACE;
3049 token = g_scanner_get_next_token (scanner);
3052 case GTK_RC_TOKEN_ACTIVE:
3053 *state = GTK_STATE_ACTIVE;
3055 case GTK_RC_TOKEN_INSENSITIVE:
3056 *state = GTK_STATE_INSENSITIVE;
3058 case GTK_RC_TOKEN_NORMAL:
3059 *state = GTK_STATE_NORMAL;
3061 case GTK_RC_TOKEN_PRELIGHT:
3062 *state = GTK_STATE_PRELIGHT;
3064 case GTK_RC_TOKEN_SELECTED:
3065 *state = GTK_STATE_SELECTED;
3068 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
3071 token = g_scanner_get_next_token (scanner);
3072 if (token != G_TOKEN_RIGHT_BRACE)
3073 return G_TOKEN_RIGHT_BRACE;
3075 g_scanner_set_scope (scanner, old_scope);
3077 return G_TOKEN_NONE;
3081 gtk_rc_parse_priority (GScanner *scanner,
3082 GtkPathPriorityType *priority)
3087 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3088 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
3090 /* we don't know where we got called from, so we reset the scope here.
3091 * if we bail out due to errors, we *don't* reset the scope, so the
3092 * error messaging code can make sense of our tokens.
3094 old_scope = g_scanner_set_scope (scanner, 0);
3096 token = g_scanner_get_next_token (scanner);
3100 token = g_scanner_get_next_token (scanner);
3103 case GTK_RC_TOKEN_LOWEST:
3104 *priority = GTK_PATH_PRIO_LOWEST;
3106 case GTK_RC_TOKEN_GTK:
3107 *priority = GTK_PATH_PRIO_GTK;
3109 case GTK_RC_TOKEN_APPLICATION:
3110 *priority = GTK_PATH_PRIO_APPLICATION;
3112 case GTK_RC_TOKEN_THEME:
3113 *priority = GTK_PATH_PRIO_THEME;
3115 case GTK_RC_TOKEN_RC:
3116 *priority = GTK_PATH_PRIO_RC;
3118 case GTK_RC_TOKEN_HIGHEST:
3119 *priority = GTK_PATH_PRIO_HIGHEST;
3122 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
3125 g_scanner_set_scope (scanner, old_scope);
3127 return G_TOKEN_NONE;
3131 gtk_rc_parse_color (GScanner *scanner,
3136 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
3138 /* we don't need to set our own scope here, because
3139 * we don't need own symbols
3142 token = g_scanner_get_next_token (scanner);
3147 case G_TOKEN_LEFT_CURLY:
3148 token = g_scanner_get_next_token (scanner);
3149 if (token == G_TOKEN_INT)
3150 token_int = scanner->value.v_int;
3151 else if (token == G_TOKEN_FLOAT)
3152 token_int = scanner->value.v_float * 65535.0;
3154 return G_TOKEN_FLOAT;
3155 color->red = CLAMP (token_int, 0, 65535);
3157 token = g_scanner_get_next_token (scanner);
3158 if (token != G_TOKEN_COMMA)
3159 return G_TOKEN_COMMA;
3161 token = g_scanner_get_next_token (scanner);
3162 if (token == G_TOKEN_INT)
3163 token_int = scanner->value.v_int;
3164 else if (token == G_TOKEN_FLOAT)
3165 token_int = scanner->value.v_float * 65535.0;
3167 return G_TOKEN_FLOAT;
3168 color->green = CLAMP (token_int, 0, 65535);
3170 token = g_scanner_get_next_token (scanner);
3171 if (token != G_TOKEN_COMMA)
3172 return G_TOKEN_COMMA;
3174 token = g_scanner_get_next_token (scanner);
3175 if (token == G_TOKEN_INT)
3176 token_int = scanner->value.v_int;
3177 else if (token == G_TOKEN_FLOAT)
3178 token_int = scanner->value.v_float * 65535.0;
3180 return G_TOKEN_FLOAT;
3181 color->blue = CLAMP (token_int, 0, 65535);
3183 token = g_scanner_get_next_token (scanner);
3184 if (token != G_TOKEN_RIGHT_CURLY)
3185 return G_TOKEN_RIGHT_CURLY;
3186 return G_TOKEN_NONE;
3188 case G_TOKEN_STRING:
3189 if (!gdk_color_parse (scanner->value.v_string, color))
3191 g_scanner_warn (scanner, "Invalid color constant '%s'",
3192 scanner->value.v_string);
3193 return G_TOKEN_STRING;
3196 return G_TOKEN_NONE;
3199 return G_TOKEN_STRING;
3204 gtk_rc_parse_pixmap_path (GtkRcContext *context,
3209 token = g_scanner_get_next_token (scanner);
3210 if (token != GTK_RC_TOKEN_PIXMAP_PATH)
3211 return GTK_RC_TOKEN_PIXMAP_PATH;
3213 token = g_scanner_get_next_token (scanner);
3214 if (token != G_TOKEN_STRING)
3215 return G_TOKEN_STRING;
3217 gtk_rc_parse_pixmap_path_string (context, scanner, scanner->value.v_string);
3219 return G_TOKEN_NONE;
3223 gtk_rc_parse_pixmap_path_string (GtkRcContext *context,
3225 const gchar *pix_path)
3228 gint start_offset = 0;
3232 /* free the old one, or just add to the old one ? */
3233 for (path_num = 0; context->pixmap_path[path_num]; path_num++)
3235 g_free (context->pixmap_path[path_num]);
3236 context->pixmap_path[path_num] = NULL;
3241 path_len = strlen (pix_path);
3243 for (end_offset = 0; end_offset <= path_len; end_offset++)
3245 if ((pix_path[end_offset] == G_SEARCHPATH_SEPARATOR) ||
3246 (end_offset == path_len))
3248 gchar *path_element = g_strndup (pix_path + start_offset, end_offset - start_offset);
3249 if (g_path_is_absolute (path_element))
3251 context->pixmap_path[path_num] = path_element;
3253 context->pixmap_path[path_num] = NULL;
3257 g_warning (_("Pixmap path element: \"%s\" must be absolute, %s, line %d"),
3258 path_element, scanner->input_name, scanner->line);
3259 g_free (path_element);
3262 start_offset = end_offset + 1;
3268 gtk_rc_parse_module_path (GScanner *scanner)
3272 token = g_scanner_get_next_token (scanner);
3273 if (token != GTK_RC_TOKEN_MODULE_PATH)
3274 return GTK_RC_TOKEN_MODULE_PATH;
3276 token = g_scanner_get_next_token (scanner);
3277 if (token != G_TOKEN_STRING)
3278 return G_TOKEN_STRING;
3280 g_warning ("module_path directive is now ignored\n");
3282 return G_TOKEN_NONE;
3286 gtk_rc_parse_im_module_file (GScanner *scanner)
3290 token = g_scanner_get_next_token (scanner);
3291 if (token != GTK_RC_TOKEN_IM_MODULE_FILE)
3292 return GTK_RC_TOKEN_IM_MODULE_FILE;
3294 token = g_scanner_get_next_token (scanner);
3295 if (token != G_TOKEN_STRING)
3296 return G_TOKEN_STRING;
3299 g_free (im_module_file);
3301 im_module_file = g_strdup (scanner->value.v_string);
3303 return G_TOKEN_NONE;
3307 gtk_rc_parse_path_pattern (GtkRcContext *context,
3311 GtkPathType path_type;
3313 gboolean is_binding;
3314 GtkPathPriorityType priority = context->default_priority;
3316 token = g_scanner_get_next_token (scanner);
3319 case GTK_RC_TOKEN_WIDGET:
3320 path_type = GTK_PATH_WIDGET;
3322 case GTK_RC_TOKEN_WIDGET_CLASS:
3323 path_type = GTK_PATH_WIDGET_CLASS;
3325 case GTK_RC_TOKEN_CLASS:
3326 path_type = GTK_PATH_CLASS;
3329 return GTK_RC_TOKEN_WIDGET_CLASS;
3332 token = g_scanner_get_next_token (scanner);
3333 if (token != G_TOKEN_STRING)
3334 return G_TOKEN_STRING;
3336 pattern = g_strdup (scanner->value.v_string);
3338 token = g_scanner_get_next_token (scanner);
3339 if (token == GTK_RC_TOKEN_STYLE)
3341 else if (token == GTK_RC_TOKEN_BINDING)
3346 return GTK_RC_TOKEN_STYLE;
3349 if (g_scanner_peek_next_token (scanner) == ':')
3351 token = gtk_rc_parse_priority (scanner, &priority);
3352 if (token != G_TOKEN_NONE)
3359 token = g_scanner_get_next_token (scanner);
3360 if (token != G_TOKEN_STRING)
3363 return G_TOKEN_STRING;
3368 GtkBindingSet *binding;
3370 binding = gtk_binding_set_find (scanner->value.v_string);
3374 return G_TOKEN_STRING;
3376 gtk_binding_set_add_path (binding, path_type, pattern, priority);
3380 GtkRcStyle *rc_style;
3383 rc_style = gtk_rc_style_find (context, scanner->value.v_string);
3388 return G_TOKEN_STRING;
3391 rc_set = g_new (GtkRcSet, 1);
3392 rc_set->pspec = g_pattern_spec_new (pattern);
3393 rc_set->rc_style = rc_style;
3394 rc_set->priority = priority;
3396 if (path_type == GTK_PATH_WIDGET)
3397 context->rc_sets_widget = g_slist_prepend (context->rc_sets_widget, rc_set);
3398 else if (path_type == GTK_PATH_WIDGET_CLASS)
3399 context->rc_sets_widget_class = g_slist_prepend (context->rc_sets_widget_class, rc_set);
3401 context->rc_sets_class = g_slist_prepend (context->rc_sets_class, rc_set);
3405 return G_TOKEN_NONE;
3409 gtk_rc_parse_stock_id (GScanner *scanner,
3414 token = g_scanner_get_next_token (scanner);
3415 if (token != G_TOKEN_LEFT_BRACE)
3416 return G_TOKEN_LEFT_BRACE;
3418 token = g_scanner_get_next_token (scanner);
3420 if (token != G_TOKEN_STRING)
3421 return G_TOKEN_STRING;
3423 *stock_id = g_strdup (scanner->value.v_string);
3425 token = g_scanner_get_next_token (scanner);
3426 if (token != G_TOKEN_RIGHT_BRACE)
3429 return G_TOKEN_RIGHT_BRACE;
3432 return G_TOKEN_NONE;
3436 gtk_rc_parse_icon_source (GtkRcContext *context,
3438 GtkIconSet *icon_set,
3439 gboolean *icon_set_valid)
3442 GtkIconSource *source;
3443 gchar *full_filename;
3445 token = g_scanner_get_next_token (scanner);
3446 if (token != G_TOKEN_LEFT_CURLY)
3447 return G_TOKEN_LEFT_CURLY;
3449 token = g_scanner_get_next_token (scanner);
3451 if (token != G_TOKEN_STRING)
3452 return G_TOKEN_STRING;
3455 source = gtk_icon_source_new ();
3457 full_filename = gtk_rc_find_pixmap_in_path (context->settings, scanner, scanner->value.v_string);
3460 gtk_icon_source_set_filename (source, full_filename);
3461 g_free (full_filename);
3464 /* We continue parsing even if we didn't find the pixmap so that rest of the
3465 * file is read, even if the syntax is bad. However we don't validate the
3466 * icon_set so the caller can choose not to install it.
3468 token = g_scanner_get_next_token (scanner);
3470 if (token == G_TOKEN_RIGHT_CURLY)
3472 else if (token != G_TOKEN_COMMA)
3474 gtk_icon_source_free (source);
3475 return G_TOKEN_COMMA;
3478 /* Get the direction */
3480 token = g_scanner_get_next_token (scanner);
3484 case GTK_RC_TOKEN_RTL:
3485 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3486 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_RTL);
3489 case GTK_RC_TOKEN_LTR:
3490 gtk_icon_source_set_direction_wildcarded (source, FALSE);
3491 gtk_icon_source_set_direction (source, GTK_TEXT_DIR_LTR);
3498 gtk_icon_source_free (source);
3499 return GTK_RC_TOKEN_RTL;
3503 token = g_scanner_get_next_token (scanner);
3505 if (token == G_TOKEN_RIGHT_CURLY)
3507 else if (token != G_TOKEN_COMMA)
3509 gtk_icon_source_free (source);
3510 return G_TOKEN_COMMA;
3515 token = g_scanner_get_next_token (scanner);
3519 case GTK_RC_TOKEN_NORMAL:
3520 gtk_icon_source_set_state_wildcarded (source, FALSE);
3521 gtk_icon_source_set_state (source, GTK_STATE_NORMAL);
3524 case GTK_RC_TOKEN_PRELIGHT:
3525 gtk_icon_source_set_state_wildcarded (source, FALSE);
3526 gtk_icon_source_set_state (source, GTK_STATE_PRELIGHT);
3530 case GTK_RC_TOKEN_INSENSITIVE:
3531 gtk_icon_source_set_state_wildcarded (source, FALSE);
3532 gtk_icon_source_set_state (source, GTK_STATE_INSENSITIVE);
3535 case GTK_RC_TOKEN_ACTIVE:
3536 gtk_icon_source_set_state_wildcarded (source, FALSE);
3537 gtk_icon_source_set_state (source, GTK_STATE_ACTIVE);
3540 case GTK_RC_TOKEN_SELECTED:
3541 gtk_icon_source_set_state_wildcarded (source, FALSE);
3542 gtk_icon_source_set_state (source, GTK_STATE_SELECTED);
3549 gtk_icon_source_free (source);
3550 return GTK_RC_TOKEN_PRELIGHT;
3554 token = g_scanner_get_next_token (scanner);
3556 if (token == G_TOKEN_RIGHT_CURLY)
3558 else if (token != G_TOKEN_COMMA)
3560 gtk_icon_source_free (source);
3561 return G_TOKEN_COMMA;
3566 token = g_scanner_get_next_token (scanner);
3572 if (token != G_TOKEN_STRING)
3574 gtk_icon_source_free (source);
3575 return G_TOKEN_STRING;
3578 size = gtk_icon_size_from_name (scanner->value.v_string);
3580 if (size != GTK_ICON_SIZE_INVALID)
3582 gtk_icon_source_set_size_wildcarded (source, FALSE);
3583 gtk_icon_source_set_size (source, size);
3587 /* Check the close brace */
3589 token = g_scanner_get_next_token (scanner);
3590 if (token != G_TOKEN_RIGHT_CURLY)
3592 gtk_icon_source_free (source);
3593 return G_TOKEN_RIGHT_CURLY;
3597 if (gtk_icon_source_get_filename (source))
3599 gtk_icon_set_add_source (icon_set, source);
3600 *icon_set_valid = TRUE;
3602 gtk_icon_source_free (source);
3604 return G_TOKEN_NONE;
3608 gtk_rc_parse_stock (GtkRcContext *context,
3610 GtkRcStyle *rc_style,
3611 GtkIconFactory *factory)
3613 GtkIconSet *icon_set = NULL;
3614 gboolean icon_set_valid = FALSE;
3615 gchar *stock_id = NULL;
3618 token = g_scanner_get_next_token (scanner);
3619 if (token != GTK_RC_TOKEN_STOCK)
3620 return GTK_RC_TOKEN_STOCK;
3622 token = gtk_rc_parse_stock_id (scanner, &stock_id);
3623 if (token != G_TOKEN_NONE)
3626 token = g_scanner_get_next_token (scanner);
3627 if (token != G_TOKEN_EQUAL_SIGN)
3630 return G_TOKEN_EQUAL_SIGN;
3633 token = g_scanner_get_next_token (scanner);
3634 if (token != G_TOKEN_LEFT_CURLY)
3637 return G_TOKEN_LEFT_CURLY;
3640 token = g_scanner_peek_next_token (scanner);
3641 while (token != G_TOKEN_RIGHT_CURLY)
3643 if (icon_set == NULL)
3644 icon_set = gtk_icon_set_new ();
3646 token = gtk_rc_parse_icon_source (context,
3647 scanner, icon_set, &icon_set_valid);
3648 if (token != G_TOKEN_NONE)
3651 gtk_icon_set_unref (icon_set);
3655 token = g_scanner_get_next_token (scanner);
3657 if (token != G_TOKEN_COMMA &&
3658 token != G_TOKEN_RIGHT_CURLY)
3661 gtk_icon_set_unref (icon_set);
3662 return G_TOKEN_RIGHT_CURLY;
3666 if (icon_set && icon_set_valid)
3668 gtk_icon_factory_add (factory,
3672 gtk_icon_set_unref (icon_set);
3677 return G_TOKEN_NONE;