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>
44 #include <glib/gstdio.h>
45 #include "gdkconfig.h"
47 #include "gtkversion.h"
49 #include "gtkbindings.h"
50 #include "gtkthemes.h"
52 #include "gtkiconfactory.h"
54 #include "gtkmodules.h"
55 #include "gtkprivate.h"
56 #include "gtksettings.h"
57 #include "gtkwindow.h"
81 #define GTK_RC_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_RC_STYLE, GtkRcStylePrivate))
83 typedef struct _GtkRcStylePrivate GtkRcStylePrivate;
85 struct _GtkRcStylePrivate
90 static void gtk_rc_style_finalize (GObject *object);
91 static void gtk_rc_style_real_merge (GtkRcStyle *dest,
93 static GtkRcStyle* gtk_rc_style_real_create_rc_style (GtkRcStyle *rc_style);
94 static GtkStyle* gtk_rc_style_real_create_style (GtkRcStyle *rc_style);
95 static gint gtk_rc_properties_cmp (gconstpointer bsearch_node1,
96 gconstpointer bsearch_node2);
98 static void insert_rc_property (GtkRcStyle *style,
99 GtkRcProperty *property,
103 static const GScannerConfig gtk_rc_scanner_config =
107 ) /* cset_skip_characters */,
112 ) /* cset_identifier_first */,
118 ) /* cset_identifier_nth */,
119 ( "#\n" ) /* cpair_comment_single */,
121 TRUE /* case_sensitive */,
123 TRUE /* skip_comment_multi */,
124 TRUE /* skip_comment_single */,
125 TRUE /* scan_comment_multi */,
126 TRUE /* scan_identifier */,
127 FALSE /* scan_identifier_1char */,
128 FALSE /* scan_identifier_NULL */,
129 TRUE /* scan_symbols */,
130 TRUE /* scan_binary */,
131 TRUE /* scan_octal */,
132 TRUE /* scan_float */,
134 TRUE /* scan_hex_dollar */,
135 TRUE /* scan_string_sq */,
136 TRUE /* scan_string_dq */,
137 TRUE /* numbers_2_int */,
138 FALSE /* int_2_float */,
139 FALSE /* identifier_2_string */,
140 TRUE /* char_2_token */,
141 TRUE /* symbol_2_token */,
142 FALSE /* scope_0_fallback */,
145 static const gchar symbol_names[] =
189 { 0, GTK_RC_TOKEN_INCLUDE },
190 { 8, GTK_RC_TOKEN_NORMAL },
191 { 15, GTK_RC_TOKEN_ACTIVE },
192 { 22, GTK_RC_TOKEN_PRELIGHT },
193 { 31, GTK_RC_TOKEN_SELECTED },
194 { 40, GTK_RC_TOKEN_INSENSITIVE },
195 { 52, GTK_RC_TOKEN_FG },
196 { 55, GTK_RC_TOKEN_BG },
197 { 58, GTK_RC_TOKEN_TEXT },
198 { 63, GTK_RC_TOKEN_BASE },
199 { 68, GTK_RC_TOKEN_XTHICKNESS },
200 { 79, GTK_RC_TOKEN_YTHICKNESS },
201 { 90, GTK_RC_TOKEN_FONT },
202 { 95, GTK_RC_TOKEN_FONTSET },
203 { 103, GTK_RC_TOKEN_FONT_NAME },
204 { 113, GTK_RC_TOKEN_BG_PIXMAP },
205 { 123, GTK_RC_TOKEN_PIXMAP_PATH },
206 { 135, GTK_RC_TOKEN_STYLE },
207 { 141, GTK_RC_TOKEN_BINDING },
208 { 149, GTK_RC_TOKEN_BIND },
209 { 154, GTK_RC_TOKEN_WIDGET },
210 { 161, GTK_RC_TOKEN_WIDGET_CLASS },
211 { 174, GTK_RC_TOKEN_CLASS },
212 { 180, GTK_RC_TOKEN_LOWEST },
213 { 187, GTK_RC_TOKEN_GTK },
214 { 191, GTK_RC_TOKEN_APPLICATION },
215 { 203, GTK_RC_TOKEN_THEME },
216 { 209, GTK_RC_TOKEN_RC },
217 { 212, GTK_RC_TOKEN_HIGHEST },
218 { 220, GTK_RC_TOKEN_ENGINE },
219 { 227, GTK_RC_TOKEN_MODULE_PATH },
220 { 239, GTK_RC_TOKEN_STOCK },
221 { 245, GTK_RC_TOKEN_IM_MODULE_FILE },
222 { 260, GTK_RC_TOKEN_LTR },
223 { 264, GTK_RC_TOKEN_RTL },
224 { 268, GTK_RC_TOKEN_COLOR },
225 { 274, GTK_RC_TOKEN_UNBIND }
228 static GHashTable *realized_style_ht = NULL;
230 static gchar *im_module_file = NULL;
232 static gchar **gtk_rc_default_files = NULL;
234 /* RC file handling */
237 gtk_rc_make_default_dir (const gchar *type)
242 var = g_getenv ("GTK_EXE_PREFIX");
245 path = g_build_filename (var, "lib", "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
247 path = g_build_filename (GTK_LIBDIR, "gtk-3.0", GTK_BINARY_VERSION, type, NULL);
253 * gtk_rc_get_im_module_path:
254 * @returns: a newly-allocated string containing the path in which to
255 * look for IM modules.
257 * Obtains the path in which to look for IM modules. See the documentation
258 * of the <link linkend="im-module-path"><envar>GTK_PATH</envar></link>
259 * environment variable for more details about looking up modules. This
260 * function is useful solely for utilities supplied with GTK+ and should
261 * not be used by applications under normal circumstances.
264 gtk_rc_get_im_module_path (void)
266 gchar **paths = _gtk_get_module_path ("immodules");
267 gchar *result = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, paths);
274 * gtk_rc_get_im_module_file:
275 * @returns: a newly-allocated string containing the name of the file
276 * listing the IM modules available for loading
278 * Obtains the path to the IM modules file. See the documentation
279 * of the <link linkend="im-module-file"><envar>GTK_IM_MODULE_FILE</envar></link>
280 * environment variable for more details.
283 gtk_rc_get_im_module_file (void)
285 const gchar *var = g_getenv ("GTK_IM_MODULE_FILE");
286 gchar *result = NULL;
289 result = g_strdup (var);
294 result = g_strdup (im_module_file);
296 result = gtk_rc_make_default_dir ("immodules.cache");
303 gtk_rc_get_theme_dir (void)
308 var = g_getenv ("GTK_DATA_PREFIX");
311 path = g_build_filename (var, "share", "themes", NULL);
313 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
319 * gtk_rc_get_module_dir:
321 * Returns a directory in which GTK+ looks for theme engines.
322 * For full information about the search for theme engines,
323 * see the docs for <envar>GTK_PATH</envar> in
324 * <xref linkend="gtk-running"/>.
326 * return value: the directory. (Must be freed with g_free())
329 gtk_rc_get_module_dir (void)
331 return gtk_rc_make_default_dir ("engines");
335 * gtk_rc_add_default_file:
336 * @filename: the pathname to the file. If @filename is not absolute, it
337 * is searched in the current directory.
339 * Adds a file to the list of files to be parsed at the
342 * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
345 gtk_rc_add_default_file (const gchar *filename)
350 * gtk_rc_set_default_files:
351 * @filenames: A %NULL-terminated list of filenames.
353 * Sets the list of files that GTK+ will read at the
356 * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
359 gtk_rc_set_default_files (gchar **filenames)
364 * gtk_rc_get_default_files:
366 * Retrieves the current list of RC files that will be parsed
367 * at the end of gtk_init().
369 * Return value: (transfer none): A %NULL-terminated array of filenames.
370 * This memory is owned by GTK+ and must not be freed by the application.
371 * If you want to store this information, you should make a copy.
373 * Deprecated:3.0: Use #GtkStyleContext instead
376 gtk_rc_get_default_files (void)
378 return gtk_rc_default_files;
382 gtk_rc_parse_string (const gchar *rc_string)
384 g_return_if_fail (rc_string != NULL);
388 gtk_rc_parse (const gchar *filename)
390 g_return_if_fail (filename != NULL);
393 /* Handling of RC styles */
395 G_DEFINE_TYPE (GtkRcStyle, gtk_rc_style, G_TYPE_OBJECT)
398 gtk_rc_style_init (GtkRcStyle *style)
400 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
404 style->font_desc = NULL;
406 for (i = 0; i < 5; i++)
408 static const GdkColor init_color = { 0, 0, 0, 0, };
410 style->bg_pixmap_name[i] = NULL;
411 style->color_flags[i] = 0;
412 style->fg[i] = init_color;
413 style->bg[i] = init_color;
414 style->text[i] = init_color;
415 style->base[i] = init_color;
417 style->xthickness = -1;
418 style->ythickness = -1;
419 style->rc_properties = NULL;
421 style->rc_style_lists = NULL;
422 style->icon_factories = NULL;
424 priv->color_hashes = NULL;
428 gtk_rc_style_class_init (GtkRcStyleClass *klass)
430 GObjectClass *object_class = G_OBJECT_CLASS (klass);
432 object_class->finalize = gtk_rc_style_finalize;
435 klass->create_rc_style = gtk_rc_style_real_create_rc_style;
436 klass->merge = gtk_rc_style_real_merge;
437 klass->create_style = gtk_rc_style_real_create_style;
439 g_type_class_add_private (object_class, sizeof (GtkRcStylePrivate));
443 gtk_rc_style_finalize (GObject *object)
445 GSList *tmp_list1, *tmp_list2;
446 GtkRcStyle *rc_style;
447 GtkRcStylePrivate *rc_priv;
450 rc_style = GTK_RC_STYLE (object);
451 rc_priv = GTK_RC_STYLE_GET_PRIVATE (rc_style);
453 g_free (rc_style->name);
454 if (rc_style->font_desc)
455 pango_font_description_free (rc_style->font_desc);
457 for (i = 0; i < 5; i++)
458 g_free (rc_style->bg_pixmap_name[i]);
460 /* Now remove all references to this rc_style from
463 tmp_list1 = rc_style->rc_style_lists;
466 GSList *rc_styles = tmp_list1->data;
467 GtkStyle *style = g_hash_table_lookup (realized_style_ht, rc_styles);
468 g_object_unref (style);
470 /* Remove the list of styles from the other rc_styles
473 tmp_list2 = rc_styles;
476 GtkRcStyle *other_style = tmp_list2->data;
478 if (other_style != rc_style)
479 other_style->rc_style_lists = g_slist_remove_all (other_style->rc_style_lists,
481 tmp_list2 = tmp_list2->next;
484 /* And from the hash table itself
486 g_hash_table_remove (realized_style_ht, rc_styles);
487 g_slist_free (rc_styles);
489 tmp_list1 = tmp_list1->next;
491 g_slist_free (rc_style->rc_style_lists);
493 if (rc_style->rc_properties)
497 for (i = 0; i < rc_style->rc_properties->len; i++)
499 GtkRcProperty *node = &g_array_index (rc_style->rc_properties, GtkRcProperty, i);
501 g_free (node->origin);
502 g_value_unset (&node->value);
504 g_array_free (rc_style->rc_properties, TRUE);
505 rc_style->rc_properties = NULL;
508 g_slist_foreach (rc_style->icon_factories, (GFunc) g_object_unref, NULL);
509 g_slist_free (rc_style->icon_factories);
511 g_slist_foreach (rc_priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
512 g_slist_free (rc_priv->color_hashes);
514 G_OBJECT_CLASS (gtk_rc_style_parent_class)->finalize (object);
518 gtk_rc_style_new (void)
522 style = g_object_new (GTK_TYPE_RC_STYLE, NULL);
529 * @orig: the style to copy
531 * Makes a copy of the specified #GtkRcStyle. This function
532 * will correctly copy an RC style that is a member of a class
533 * derived from #GtkRcStyle.
535 * Return value: (transfer full): the resulting #GtkRcStyle
538 gtk_rc_style_copy (GtkRcStyle *orig)
542 g_return_val_if_fail (GTK_IS_RC_STYLE (orig), NULL);
544 style = GTK_RC_STYLE_GET_CLASS (orig)->create_rc_style (orig);
545 GTK_RC_STYLE_GET_CLASS (style)->merge (style, orig);
551 gtk_rc_style_real_create_rc_style (GtkRcStyle *style)
553 return g_object_new (G_OBJECT_TYPE (style), NULL);
557 gtk_rc_properties_cmp (gconstpointer bsearch_node1,
558 gconstpointer bsearch_node2)
560 const GtkRcProperty *prop1 = bsearch_node1;
561 const GtkRcProperty *prop2 = bsearch_node2;
563 if (prop1->type_name == prop2->type_name)
564 return prop1->property_name < prop2->property_name ? -1 : prop1->property_name == prop2->property_name ? 0 : 1;
566 return prop1->type_name < prop2->type_name ? -1 : 1;
570 insert_rc_property (GtkRcStyle *style,
571 GtkRcProperty *property,
575 GtkRcProperty *new_property = NULL;
576 GtkRcProperty key = { 0, 0, NULL, { 0, }, };
578 key.type_name = property->type_name;
579 key.property_name = property->property_name;
581 if (!style->rc_properties)
582 style->rc_properties = g_array_new (FALSE, FALSE, sizeof (GtkRcProperty));
585 while (i < style->rc_properties->len)
587 gint cmp = gtk_rc_properties_cmp (&key, &g_array_index (style->rc_properties, GtkRcProperty, i));
593 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
595 g_free (new_property->origin);
596 g_value_unset (&new_property->value);
612 g_array_insert_val (style->rc_properties, i, key);
613 new_property = &g_array_index (style->rc_properties, GtkRcProperty, i);
616 new_property->origin = g_strdup (property->origin);
617 g_value_init (&new_property->value, G_VALUE_TYPE (&property->value));
618 g_value_copy (&property->value, &new_property->value);
622 gtk_rc_style_real_merge (GtkRcStyle *dest,
627 for (i = 0; i < 5; i++)
629 if (!dest->bg_pixmap_name[i] && src->bg_pixmap_name[i])
630 dest->bg_pixmap_name[i] = g_strdup (src->bg_pixmap_name[i]);
632 if (!(dest->color_flags[i] & GTK_RC_FG) &&
633 src->color_flags[i] & GTK_RC_FG)
635 dest->fg[i] = src->fg[i];
636 dest->color_flags[i] |= GTK_RC_FG;
638 if (!(dest->color_flags[i] & GTK_RC_BG) &&
639 src->color_flags[i] & GTK_RC_BG)
641 dest->bg[i] = src->bg[i];
642 dest->color_flags[i] |= GTK_RC_BG;
644 if (!(dest->color_flags[i] & GTK_RC_TEXT) &&
645 src->color_flags[i] & GTK_RC_TEXT)
647 dest->text[i] = src->text[i];
648 dest->color_flags[i] |= GTK_RC_TEXT;
650 if (!(dest->color_flags[i] & GTK_RC_BASE) &&
651 src->color_flags[i] & GTK_RC_BASE)
653 dest->base[i] = src->base[i];
654 dest->color_flags[i] |= GTK_RC_BASE;
658 if (dest->xthickness < 0 && src->xthickness >= 0)
659 dest->xthickness = src->xthickness;
660 if (dest->ythickness < 0 && src->ythickness >= 0)
661 dest->ythickness = src->ythickness;
665 if (!dest->font_desc)
666 dest->font_desc = pango_font_description_copy (src->font_desc);
668 pango_font_description_merge (dest->font_desc, src->font_desc, FALSE);
671 if (src->rc_properties)
675 for (i = 0; i < src->rc_properties->len; i++)
676 insert_rc_property (dest,
677 &g_array_index (src->rc_properties, GtkRcProperty, i),
683 gtk_rc_style_real_create_style (GtkRcStyle *rc_style)
685 return gtk_style_new ();
689 * gtk_rc_reset_styles:
690 * @settings: a #GtkSettings
692 * This function recomputes the styles for all widgets that use a
693 * particular #GtkSettings object. (There is one #GtkSettings object
694 * per #GdkScreen, see gtk_settings_get_for_screen()); It is useful
695 * when some global parameter has changed that affects the appearance
696 * of all widgets, because when a widget gets a new style, it will
697 * both redraw and recompute any cached information about its
698 * appearance. As an example, it is used when the default font size
699 * set by the operating system changes. Note that this function
700 * doesn't affect widgets that have a style set explicitely on them
701 * with gtk_widget_set_style().
706 gtk_rc_reset_styles (GtkSettings *settings)
708 gtk_style_context_reset_widgets (_gtk_settings_get_screen (settings));
712 * gtk_rc_reparse_all_for_settings:
713 * @settings: a #GtkSettings
714 * @force_load: load whether or not anything changed
716 * If the modification time on any previously read file
717 * for the given #GtkSettings has changed, discard all style information
718 * and then reread all previously read RC files.
720 * Return value: %TRUE if the files were reread.
723 gtk_rc_reparse_all_for_settings (GtkSettings *settings,
730 * gtk_rc_reparse_all:
732 * If the modification time on any previously read file for the
733 * default #GtkSettings has changed, discard all style information
734 * and then reread all previously read RC files.
736 * Return value: %TRUE if the files were reread.
739 gtk_rc_reparse_all (void)
746 * @widget: a #GtkWidget
748 * Finds all matching RC styles for a given widget,
749 * composites them together, and then creates a
750 * #GtkStyle representing the composite appearance.
751 * (GTK+ actually keeps a cache of previously
752 * created styles, so a new style may not be
755 * Returns: the resulting style. No refcount is added
756 * to the returned style, so if you want to save this
757 * style around, you should add a reference yourself.
759 * Deprecated:3.0: Use #GtkStyleContext instead
762 gtk_rc_get_style (GtkWidget *widget)
764 g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
766 gtk_widget_ensure_style (widget);
768 return gtk_widget_get_style (widget);
772 * gtk_rc_get_style_by_paths:
773 * @settings: a #GtkSettings object
774 * @widget_path: (allow-none): the widget path to use when looking up the
775 * style, or %NULL if no matching against the widget path should be done
776 * @class_path: (allow-none): the class path to use when looking up the style,
777 * or %NULL if no matching against the class path should be done.
778 * @type: a type that will be used along with parent types of this type
779 * when matching against class styles, or #G_TYPE_NONE
781 * Creates up a #GtkStyle from styles defined in a RC file by providing
782 * the raw components used in matching. This function may be useful
783 * when creating pseudo-widgets that should be themed like widgets but
784 * don't actually have corresponding GTK+ widgets. An example of this
785 * would be items inside a GNOME canvas widget.
787 * The action of gtk_rc_get_style() is similar to:
789 * gtk_widget_path (widget, NULL, &path, NULL);
790 * gtk_widget_class_path (widget, NULL, &class_path, NULL);
791 * gtk_rc_get_style_by_paths (gtk_widget_get_settings (widget),
793 * G_OBJECT_TYPE (widget));
796 * Return value: (transfer none): A style created by matching with the
797 * supplied paths, or %NULL if nothing matching was specified and the
798 * default style should be used. The returned value is owned by GTK+
799 * as part of an internal cache, so you must call g_object_ref() on
800 * the returned value if you want to keep a reference to it.
802 * Deprecated:3.0: Use #GtkStyleContext instead
805 gtk_rc_get_style_by_paths (GtkSettings *settings,
806 const char *widget_path,
807 const char *class_path,
814 * gtk_rc_scanner_new:
816 * Deprecated:3.0: Use #GtkCssProvider instead
819 gtk_rc_scanner_new (void)
821 return g_scanner_new (>k_rc_scanner_config);
824 /*********************
825 * Parsing functions *
826 *********************/
829 lookup_color (GtkRcStyle *style,
830 const char *color_name,
833 GtkRcStylePrivate *priv = GTK_RC_STYLE_GET_PRIVATE (style);
836 for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
838 GHashTable *hash = iter->data;
839 GdkColor *match = g_hash_table_lookup (hash, color_name);
843 color->red = match->red;
844 color->green = match->green;
845 color->blue = match->blue;
854 * gtk_rc_find_pixmap_in_path:
855 * @settings: a #GtkSettings
856 * @scanner: Scanner used to get line number information for the
857 * warning message, or %NULL
858 * @pixmap_file: name of the pixmap file to locate.
860 * Looks up a file in pixmap path for the specified #GtkSettings.
861 * If the file is not found, it outputs a warning message using
862 * g_warning() and returns %NULL.
864 * Return value: the filename.
867 gtk_rc_find_pixmap_in_path (GtkSettings *settings,
869 const gchar *pixmap_file)
871 g_warning (_("Unable to locate image file in pixmap_path: \"%s\""),
877 * gtk_rc_find_module_in_path:
878 * @module_file: name of a theme engine
880 * Searches for a theme engine in the GTK+ search path. This function
881 * is not useful for applications and should not be used.
883 * Return value: The filename, if found (must be freed with g_free()),
887 gtk_rc_find_module_in_path (const gchar *module_file)
889 return _gtk_find_module (module_file, "engines");
893 * gtk_rc_parse_state:
897 * Deprecated:3.0: Use #GtkCssProvider instead
900 gtk_rc_parse_state (GScanner *scanner,
906 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
907 g_return_val_if_fail (state != NULL, G_TOKEN_ERROR);
909 /* we don't know where we got called from, so we reset the scope here.
910 * if we bail out due to errors, we *don't* reset the scope, so the
911 * error messaging code can make sense of our tokens.
913 old_scope = g_scanner_set_scope (scanner, 0);
915 token = g_scanner_get_next_token (scanner);
916 if (token != G_TOKEN_LEFT_BRACE)
917 return G_TOKEN_LEFT_BRACE;
919 token = g_scanner_get_next_token (scanner);
922 case GTK_RC_TOKEN_ACTIVE:
923 *state = GTK_STATE_ACTIVE;
925 case GTK_RC_TOKEN_INSENSITIVE:
926 *state = GTK_STATE_INSENSITIVE;
928 case GTK_RC_TOKEN_NORMAL:
929 *state = GTK_STATE_NORMAL;
931 case GTK_RC_TOKEN_PRELIGHT:
932 *state = GTK_STATE_PRELIGHT;
934 case GTK_RC_TOKEN_SELECTED:
935 *state = GTK_STATE_SELECTED;
938 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_NORMAL;
941 token = g_scanner_get_next_token (scanner);
942 if (token != G_TOKEN_RIGHT_BRACE)
943 return G_TOKEN_RIGHT_BRACE;
945 g_scanner_set_scope (scanner, old_scope);
951 * gtk_rc_parse_priority:
955 * Deprecated:3.0: Use #GtkCssProvider instead
958 gtk_rc_parse_priority (GScanner *scanner,
959 GtkPathPriorityType *priority)
964 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
965 g_return_val_if_fail (priority != NULL, G_TOKEN_ERROR);
967 /* we don't know where we got called from, so we reset the scope here.
968 * if we bail out due to errors, we *don't* reset the scope, so the
969 * error messaging code can make sense of our tokens.
971 old_scope = g_scanner_set_scope (scanner, 0);
973 token = g_scanner_get_next_token (scanner);
977 token = g_scanner_get_next_token (scanner);
980 case GTK_RC_TOKEN_LOWEST:
981 *priority = GTK_PATH_PRIO_LOWEST;
983 case GTK_RC_TOKEN_GTK:
984 *priority = GTK_PATH_PRIO_GTK;
986 case GTK_RC_TOKEN_APPLICATION:
987 *priority = GTK_PATH_PRIO_APPLICATION;
989 case GTK_RC_TOKEN_THEME:
990 *priority = GTK_PATH_PRIO_THEME;
992 case GTK_RC_TOKEN_RC:
993 *priority = GTK_PATH_PRIO_RC;
995 case GTK_RC_TOKEN_HIGHEST:
996 *priority = GTK_PATH_PRIO_HIGHEST;
999 return /* G_TOKEN_SYMBOL */ GTK_RC_TOKEN_APPLICATION;
1002 g_scanner_set_scope (scanner, old_scope);
1004 return G_TOKEN_NONE;
1008 * gtk_rc_parse_color:
1009 * @scanner: a #GScanner
1010 * @color: a pointer to a #GdkColor structure in which to store the result
1012 * Parses a color in the <link linkend="color=format">format</link> expected
1015 * Note that theme engines should use gtk_rc_parse_color_full() in
1016 * order to support symbolic colors.
1018 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
1019 * that was expected but not found
1021 * Deprecated:3.0: Use #GtkCssProvider instead
1024 gtk_rc_parse_color (GScanner *scanner,
1027 return gtk_rc_parse_color_full (scanner, NULL, color);
1031 * gtk_rc_parse_color_full:
1032 * @scanner: a #GScanner
1033 * @style: (allow-none): a #GtkRcStyle, or %NULL
1034 * @color: a pointer to a #GdkColor structure in which to store the result
1036 * Parses a color in the <link linkend="color=format">format</link> expected
1037 * in a RC file. If @style is not %NULL, it will be consulted to resolve
1038 * references to symbolic colors.
1040 * Returns: %G_TOKEN_NONE if parsing succeeded, otherwise the token
1041 * that was expected but not found
1045 * Deprecated:3.0: Use #GtkCssProvider instead
1048 gtk_rc_parse_color_full (GScanner *scanner,
1054 g_return_val_if_fail (scanner != NULL, G_TOKEN_ERROR);
1056 /* we don't need to set our own scope here, because
1057 * we don't need own symbols
1060 token = g_scanner_get_next_token (scanner);
1068 case G_TOKEN_LEFT_CURLY:
1069 token = g_scanner_get_next_token (scanner);
1070 if (token == G_TOKEN_INT)
1071 token_int = scanner->value.v_int;
1072 else if (token == G_TOKEN_FLOAT)
1073 token_int = scanner->value.v_float * 65535.0;
1075 return G_TOKEN_FLOAT;
1076 color->red = CLAMP (token_int, 0, 65535);
1078 token = g_scanner_get_next_token (scanner);
1079 if (token != G_TOKEN_COMMA)
1080 return G_TOKEN_COMMA;
1082 token = g_scanner_get_next_token (scanner);
1083 if (token == G_TOKEN_INT)
1084 token_int = scanner->value.v_int;
1085 else if (token == G_TOKEN_FLOAT)
1086 token_int = scanner->value.v_float * 65535.0;
1088 return G_TOKEN_FLOAT;
1089 color->green = CLAMP (token_int, 0, 65535);
1091 token = g_scanner_get_next_token (scanner);
1092 if (token != G_TOKEN_COMMA)
1093 return G_TOKEN_COMMA;
1095 token = g_scanner_get_next_token (scanner);
1096 if (token == G_TOKEN_INT)
1097 token_int = scanner->value.v_int;
1098 else if (token == G_TOKEN_FLOAT)
1099 token_int = scanner->value.v_float * 65535.0;
1101 return G_TOKEN_FLOAT;
1102 color->blue = CLAMP (token_int, 0, 65535);
1104 token = g_scanner_get_next_token (scanner);
1105 if (token != G_TOKEN_RIGHT_CURLY)
1106 return G_TOKEN_RIGHT_CURLY;
1107 return G_TOKEN_NONE;
1109 case G_TOKEN_STRING:
1110 if (!gdk_color_parse (scanner->value.v_string, color))
1112 g_scanner_warn (scanner, "Invalid color constant '%s'",
1113 scanner->value.v_string);
1114 return G_TOKEN_STRING;
1116 return G_TOKEN_NONE;
1119 token = g_scanner_get_next_token (scanner);
1120 if (token != G_TOKEN_IDENTIFIER)
1121 return G_TOKEN_IDENTIFIER;
1123 if (!style || !lookup_color (style, scanner->value.v_identifier, color))
1125 g_scanner_warn (scanner, "Invalid symbolic color '%s'",
1126 scanner->value.v_identifier);
1127 return G_TOKEN_IDENTIFIER;
1130 return G_TOKEN_NONE;
1132 case G_TOKEN_IDENTIFIER:
1133 if (strcmp (scanner->value.v_identifier, "mix") == 0)
1135 token = g_scanner_get_next_token (scanner);
1136 if (token != G_TOKEN_LEFT_PAREN)
1137 return G_TOKEN_LEFT_PAREN;
1140 if (g_scanner_peek_next_token (scanner) == '-')
1142 g_scanner_get_next_token (scanner); /* eat sign */
1146 token = g_scanner_get_next_token (scanner);
1147 if (token != G_TOKEN_FLOAT)
1148 return G_TOKEN_FLOAT;
1150 l = negate ? -scanner->value.v_float : scanner->value.v_float;
1152 token = g_scanner_get_next_token (scanner);
1153 if (token != G_TOKEN_COMMA)
1154 return G_TOKEN_COMMA;
1156 token = gtk_rc_parse_color_full (scanner, style, &c1);
1157 if (token != G_TOKEN_NONE)
1160 token = g_scanner_get_next_token (scanner);
1161 if (token != G_TOKEN_COMMA)
1162 return G_TOKEN_COMMA;
1164 token = gtk_rc_parse_color_full (scanner, style, &c2);
1165 if (token != G_TOKEN_NONE)
1168 token = g_scanner_get_next_token (scanner);
1169 if (token != G_TOKEN_RIGHT_PAREN)
1170 return G_TOKEN_RIGHT_PAREN;
1172 color->red = l * c1.red + (1.0 - l) * c2.red;
1173 color->green = l * c1.green + (1.0 - l) * c2.green;
1174 color->blue = l * c1.blue + (1.0 - l) * c2.blue;
1176 return G_TOKEN_NONE;
1178 else if (strcmp (scanner->value.v_identifier, "shade") == 0)
1180 token = g_scanner_get_next_token (scanner);
1181 if (token != G_TOKEN_LEFT_PAREN)
1182 return G_TOKEN_LEFT_PAREN;
1185 if (g_scanner_peek_next_token (scanner) == '-')
1187 g_scanner_get_next_token (scanner); /* eat sign */
1191 token = g_scanner_get_next_token (scanner);
1192 if (token != G_TOKEN_FLOAT)
1193 return G_TOKEN_FLOAT;
1195 l = negate ? -scanner->value.v_float : scanner->value.v_float;
1197 token = g_scanner_get_next_token (scanner);
1198 if (token != G_TOKEN_COMMA)
1199 return G_TOKEN_COMMA;
1201 token = gtk_rc_parse_color_full (scanner, style, &c1);
1202 if (token != G_TOKEN_NONE)
1205 token = g_scanner_get_next_token (scanner);
1206 if (token != G_TOKEN_RIGHT_PAREN)
1207 return G_TOKEN_RIGHT_PAREN;
1209 _gtk_style_shade (&c1, color, l);
1211 return G_TOKEN_NONE;
1213 else if (strcmp (scanner->value.v_identifier, "lighter") == 0 ||
1214 strcmp (scanner->value.v_identifier, "darker") == 0)
1216 if (scanner->value.v_identifier[0] == 'l')
1221 token = g_scanner_get_next_token (scanner);
1222 if (token != G_TOKEN_LEFT_PAREN)
1223 return G_TOKEN_LEFT_PAREN;
1225 token = gtk_rc_parse_color_full (scanner, style, &c1);
1226 if (token != G_TOKEN_NONE)
1229 token = g_scanner_get_next_token (scanner);
1230 if (token != G_TOKEN_RIGHT_PAREN)
1231 return G_TOKEN_RIGHT_PAREN;
1233 _gtk_style_shade (&c1, color, l);
1235 return G_TOKEN_NONE;
1238 return G_TOKEN_IDENTIFIER;
1241 return G_TOKEN_STRING;