1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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.
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <cairo-gobject.h>
28 #include "gtkcssproviderprivate.h"
30 #include "gtkanimationdescription.h"
31 #include "gtk9slice.h"
32 #include "gtkgradient.h"
33 #include "gtkthemingengine.h"
34 #include "gtkstyleprovider.h"
35 #include "gtkstylecontextprivate.h"
36 #include "gtkbindings.h"
37 #include "gtkprivate.h"
40 * SECTION:gtkcssprovider
41 * @Short_description: CSS-like styling for widgets
42 * @Title: GtkCssProvider
43 * @See_also: #GtkStyleContext, #GtkStyleProvider
45 * GtkCssProvider is an object implementing the #GtkStyleProvider interface.
46 * It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
47 * input in order to style widgets.
49 * <refsect2 id="gtkcssprovider-files">
50 * <title>Default files</title>
52 * An application can cause GTK+ to parse a specific CSS style sheet by
53 * calling gtk_css_provider_load_from_file() and adding the provider with
54 * gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
55 * In addition, certain files will be read when GTK+ is initialized. First,
56 * the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
57 * is loaded if it exists. Then, GTK+ tries to load
58 * <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
60 * <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
61 * where <replaceable>theme-name</replaceable> is the name of the current theme
62 * (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
63 * is the prefix configured when GTK+ was compiled, unless overridden by the
64 * <envar>GTK_DATA_PREFIX</envar> environment variable.
67 * <refsect2 id="gtkcssprovider-stylesheets">
68 * <title>Style sheets</title>
70 * The basic structure of the style sheets understood by this provider is
71 * a series of statements, which are either rule sets or '@-rules', separated
75 * A rule set consists of a selector and a declaration block, which is
76 * a series of declarations enclosed in curly braces ({ and }). The
77 * declarations are separated by semicolons (;). Multiple selectors can
78 * share the same declaration block, by putting all the separators in
79 * front of the block, separated by commas.
81 * <example><title>A rule set with two selectors</title>
82 * <programlisting language="text">
83 * GtkButton, GtkEntry {
90 * <refsect2 id="gtkcssprovider-selectors">
91 * <title>Selectors</title>
93 * Selectors work very similar to the way they do in CSS, with widget class
94 * names taking the role of element names, and widget names taking the role
95 * of IDs. When used in a selector, widget names must be prefixed with a
96 * '#' character. The '*' character represents the so-called universal
97 * selector, which matches any widget.
100 * To express more complicated situations, selectors can be combined in
103 * <listitem><para>To require that a widget satisfies several conditions,
104 * combine several selectors into one by concatenating them. E.g.
105 * <literal>GtkButton#button1</literal> matches a GtkButton widget
106 * with the name button1.</para></listitem>
107 * <listitem><para>To only match a widget when it occurs inside some other
108 * widget, write the two selectors after each other, separated by whitespace.
109 * E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
110 * that occur inside a GtkToolBar.</para></listitem>
111 * <listitem><para>In the previous example, the GtkButton is matched even
112 * if it occurs deeply nested inside the toolbar. To restrict the match
113 * to direct children of the parent widget, insert a '>' character between
114 * the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
115 * GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
119 * <title>Widget classes and names in selectors</title>
120 * <programlisting language="text">
121 * /* Theme labels that are descendants of a window */
122 * GtkWindow GtkLabel {
123 * background-color: #898989
126 * /* Theme notebooks, and anything that's within these */
128 * background-color: #a939f0
131 * /* Theme combo boxes, and entries that
132 * are direct children of a notebook */
134 * GtkNotebook > GtkEntry {
136 * background-color: #1209a2
139 * /* Theme any widget within a GtkBin */
144 * /* Theme a label named title-label */
145 * GtkLabel#title-label {
149 * /* Theme any widget named main-entry */
151 * background-color: #f0a810
156 * Widgets may also define style classes, which can be used for matching.
157 * When used in a selector, style classes must be prefixed with a '.'
161 * Refer to the documentation of individual widgets to learn which
162 * style classes they define and see <xref linkend="gtkstylecontext-classes"/>
163 * for a list of all style classes used by GTK+ widgets.
166 * Note that there is some ambiguity in the selector syntax when it comes
167 * to differentiation widget class names from regions. GTK+ currently treats
168 * a string as a widget class name if it contains any uppercase characters
169 * (which should work for more widgets with names like GtkLabel).
172 * <title>Style classes in selectors</title>
173 * <programlisting language="text">
174 * /* Theme all widgets defining the class entry */
176 * color: #39f1f9;
179 * /* Theme spinbuttons' entry */
180 * GtkSpinButton.entry {
186 * In complicated widgets like e.g. a GtkNotebook, it may be desirable
187 * to style different parts of the widget differently. To make this
188 * possible, container widgets may define regions, whose names
189 * may be used for matching in selectors.
192 * Some containers allow to further differentiate between regions by
193 * applying so-called pseudo-classes to the region. For example, the
194 * tab region in GtkNotebook allows to single out the first or last
195 * tab by using the :first-child or :last-child pseudo-class.
196 * When used in selectors, pseudo-classes must be prefixed with a
200 * Refer to the documentation of individual widgets to learn which
201 * regions and pseudo-classes they define and see
202 * <xref linkend="gtkstylecontext-classes"/> for a list of all regions
203 * used by GTK+ widgets.
206 * <title>Regions in selectors</title>
207 * <programlisting language="text">
208 * /* Theme any label within a notebook */
209 * GtkNotebook GtkLabel {
210 * color: #f90192;
213 * /* Theme labels within notebook tabs */
214 * GtkNotebook tab GtkLabel {
215 * color: #703910;
218 * /* Theme labels in the any first notebook
219 * tab, both selectors are equivalent */
220 * GtkNotebook tab:nth-child(first) GtkLabel,
221 * GtkNotebook tab:first-child GtkLabel {
222 * color: #89d012;
227 * Another use of pseudo-classes is to match widgets depending on their
228 * state. This is conceptually similar to the :hover, :active or :focus
229 * pseudo-classes in CSS. The available pseudo-classes for widget states
230 * are :active, :prelight (or :hover), :insensitive, :selected, :focused
234 * <title>Styling specific widget states</title>
235 * <programlisting language="text">
236 * /* Theme active (pressed) buttons */
238 * background-color: #0274d9;
241 * /* Theme buttons with the mouse pointer on it,
242 * both are equivalent */
244 * GtkButton:prelight {
245 * background-color: #3085a9;
248 * /* Theme insensitive widgets, both are equivalent */
251 * background-color: #320a91;
254 * /* Theme selection colors in entries */
255 * GtkEntry:selected {
256 * background-color: #56f9a0;
259 * /* Theme focused labels */
261 * background-color: #b4940f;
264 * /* Theme inconsistent checkbuttons */
265 * GtkCheckButton:inconsistent {
266 * background-color: #20395a;
271 * Widget state pseudoclasses may only apply to the last element
275 * To determine the effective style for a widget, all the matching rule
276 * sets are merged. As in CSS, rules apply by specificity, so the rules
277 * whose selectors more closely match a widget path will take precedence
281 * <refsect2 id="gtkcssprovider-rules">
282 * <title>@ Rules</title>
284 * GTK+'s CSS supports the @import rule, in order to load another
285 * CSS style sheet in addition to the currently parsed one.
288 * <title>Using the @import rule</title>
289 * <programlisting language="text">
290 * @import url ("path/to/common.css");
293 * <para id="css-binding-set">
294 * In order to extend key bindings affecting different widgets, GTK+
295 * supports the @binding-set rule to parse a set of bind/unbind
296 * directives, see #GtkBindingSet for the supported syntax. Note that
297 * the binding sets defined in this way must be associated with rule sets
298 * by setting the gtk-key-bindings style property.
301 * Customized key bindings are typically defined in a separate
302 * <filename>gtk-keys.css</filename> CSS file and GTK+ loads this file
303 * according to the current key theme, which is defined by the
304 * #GtkSettings:gtk-key-theme-name setting.
307 * <title>Using the @binding rule</title>
308 * <programlisting language="text">
309 * @binding-set binding-set1 {
310 * bind "<alt>Left" { "move-cursor" (visual-positions, -3, 0) };
314 * @binding-set binding-set2 {
315 * bind "<alt>Right" { "move-cursor" (visual-positions, 3, 0) };
316 * bind "<alt>KP_space" { "delete-from-cursor" (whitespace, 1)
317 * "insert-at-cursor" (" ") };
321 * gtk-key-bindings: binding-set1, binding-set2;
326 * GTK+ also supports an additional @define-color rule, in order
327 * to define a color name which may be used instead of color numeric
328 * representations. Also see the #GtkSettings:gtk-color-scheme setting
329 * for a way to override the values of these named colors.
332 * <title>Defining colors</title>
333 * <programlisting language="text">
334 * @define-color bg_color #f9a039;
337 * background-color: @bg_color;
342 * <refsect2 id="gtkcssprovider-symbolic-colors">
343 * <title>Symbolic colors</title>
345 * Besides being able to define color names, the CSS parser is also able
346 * to read different color expressions, which can also be nested, providing
347 * a rich language to define colors which are derived from a set of base
351 * <title>Using symbolic colors</title>
352 * <programlisting language="text">
353 * @define-color entry-color shade (@bg_color, 0.7);
356 * background-color: @entry-color;
360 * background-color: mix (@entry-color,
361 * shade (#fff, 0.5),
367 * The various ways to express colors in GTK+ CSS are:
373 * <entry>Syntax</entry>
374 * <entry>Explanation</entry>
375 * <entry>Examples</entry>
380 * <entry>rgb(@r, @g, @b)</entry>
381 * <entry>An opaque color; @r, @g, @b can be either integers between
382 * 0 and 255 or percentages</entry>
383 * <entry><literallayout>rgb(128, 10, 54)
384 * rgb(20%, 30%, 0%)</literallayout></entry>
387 * <entry>rgba(@r, @g, @b, @a)</entry>
388 * <entry>A translucent color; @r, @g, @b are as in the previous row,
389 * @a is a floating point number between 0 and 1</entry>
390 * <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
393 * <entry>#@xxyyzz</entry>
394 * <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
395 * specifying @r, @g, @b variants with between 1 and 4
396 * hexadecimal digits per component are allowed</entry>
397 * <entry><literallayout>#ff12ab
398 * #f0c</literallayout></entry>
401 * <entry>@name</entry>
402 * <entry>Reference to a color that has been defined with
403 * @define-color
405 * <entry>@bg_color</entry>
408 * <entry>mix(@color1, @color2, @f)</entry>
409 * <entry>A linear combination of @color1 and @color2. @f is a
410 * floating point number between 0 and 1.</entry>
411 * <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
414 * <entry>shade(@color, @f)</entry>
415 * <entry>A lighter or darker variant of @color. @f is a
416 * floating point number.
418 * <entry>shade(@fg_color, 0.5)</entry>
421 * <entry>lighter(@color)</entry>
422 * <entry>A lighter variant of @color</entry>
425 * <entry>darker(@color)</entry>
426 * <entry>A darker variant of @color</entry>
432 * <refsect2 id="gtkcssprovider-gradients">
433 * <title>Gradients</title>
435 * Linear or radial Gradients can be used as background images.
438 * A linear gradient along the line from (@start_x, @start_y) to
439 * (@end_x, @end_y) is specified using the syntax
440 * <literallayout>-gtk-gradient (linear,
441 * @start_x @start_y, @end_x @end_y,
442 * color-stop (@position, @color),
443 * ...)</literallayout>
444 * where @start_x and @end_x can be either a floating point number between
445 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
446 * and @end_y can be either a floating point number between 0 and 1 or one
447 * of the special values 'top', 'bottom' or 'center', @position is a floating
448 * point number between 0 and 1 and @color is a color expression (see above).
449 * The color-stop can be repeated multiple times to add more than one color
450 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
451 * color stops with position 0 and 1, respectively.
454 * <title>A linear gradient</title>
455 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
456 * <para>This gradient was specified with
457 * <literallayout>-gtk-gradient (linear,
458 * left top, right bottom,
459 * from(@yellow), to(@blue))</literallayout></para>
462 * <title>Another linear gradient</title>
463 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
464 * <para>This gradient was specified with
465 * <literallayout>-gtk-gradient (linear,
467 * color-stop(0, @yellow),
468 * color-stop(0.2, @blue),
469 * color-stop(1, #0f0))</literallayout></para>
472 * A radial gradient along the two circles defined by (@start_x, @start_y,
473 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
475 * <literallayout>-gtk-gradient (radial,
476 * @start_x @start_y, @start_radius,
477 * @end_x @end_y, @end_radius,
478 * color-stop (@position, @color),
479 * ...)</literallayout>
480 * where @start_radius and @end_radius are floating point numbers and
481 * the other parameters are as before.
484 * <title>A radial gradient</title>
485 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
486 * <para>This gradient was specified with
487 * <literallayout>-gtk-gradient (radial,
490 * from(@yellow), to(@green))</literallayout></para>
493 * <title>Another radial gradient</title>
494 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
495 * <para>This gradient was specified with
496 * <literallayout>-gtk-gradient (radial,
499 * color-stop (0, #f00),
500 * color-stop (0.1, #a0f),
501 * color-stop (0.2, @yellow),
502 * color-stop (1, @green))</literallayout></para>
505 * <refsect2 id="gtkcssprovider-slices">
506 * <title>Border images</title>
508 * Images can be used in 'slices' for the purpose of creating scalable
511 * <inlinegraphic fileref="slices.png" format="PNG"/>
513 * The syntax for specifying border images of this kind is:
514 * <literallayout>url(@path) @top @right @bottom @left [repeat|stretch]? [repeat|stretch]?</literallayout>
515 * The sizes of the 'cut off' portions are specified
516 * with the @top, @right, @bottom and @left parameters.
517 * The 'middle' sections can be repeated or stretched to create
518 * the desired effect, by adding the 'repeat' or 'stretch' options after
519 * the dimensions. If two options are specified, the first one affects
520 * the horizontal behaviour and the second one the vertical behaviour.
521 * If only one option is specified, it affects both.
524 * <title>A border image</title>
525 * <inlinegraphic fileref="border1.png" format="PNG"/>
526 * <para>This border image was specified with
527 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
531 * <title>A repeating border image</title>
532 * <inlinegraphic fileref="border2.png" format="PNG"/>
533 * <para>This border image was specified with
534 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
538 * <title>A stretched border image</title>
539 * <inlinegraphic fileref="border3.png" format="PNG"/>
540 * <para>This border image was specified with
541 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
545 * <refsect2 id="gtkcssprovider-transitions">
546 * <para>Styles can specify transitions that will be used to create a gradual
547 * change in the appearance when a widget state changes. The following
548 * syntax is used to specify transitions:
549 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
550 * The @duration is the amount of time that the animation will take for
551 * a complete cycle from start to end. If the loop option is given, the
552 * animation will be repated until the state changes again.
553 * The option after the duration determines the transition function from a
554 * small set of predefined functions.
555 * <figure><title>Linear transition</title>
556 * <graphic fileref="linear.png" format="PNG"/>
558 * <figure><title>Ease transition</title>
559 * <graphic fileref="ease.png" format="PNG"/>
561 * <figure><title>Ease-in-out transition</title>
562 * <graphic fileref="ease-in-out.png" format="PNG"/>
564 * <figure><title>Ease-in transition</title>
565 * <graphic fileref="ease-in.png" format="PNG"/>
567 * <figure><title>Ease-out transition</title>
568 * <graphic fileref="ease-out.png" format="PNG"/>
572 * <refsect2 id="gtkcssprovider-properties">
573 * <title>Supported properties</title>
575 * Properties are the part that differ the most to common CSS,
576 * not all properties are supported (some are planned to be
577 * supported eventually, some others are meaningless or don't
578 * map intuitively in a widget based environment).
581 * There is also a difference in shorthand properties, for
582 * example in common CSS it is fine to define a font through
583 * the different @font-family, @font-style, @font-size
584 * properties, meanwhile in GTK+'s CSS only the canonical
585 * @font property is supported.
588 * The currently supported properties are:
594 * <entry>Property name</entry>
595 * <entry>Syntax</entry>
596 * <entry>Maps to</entry>
597 * <entry>Examples</entry>
602 * <entry>engine</entry>
603 * <entry>engine-name</entry>
604 * <entry>#GtkThemingEngine</entry>
605 * <entry>engine: clearlooks;
606 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
609 * <entry>background-color</entry>
610 * <entry morerows="2">color (see above)</entry>
611 * <entry morerows="2">#GdkRGBA</entry>
612 * <entry morerows="2"><literallayout>background-color: #fff;
613 * color: &color1;
614 * background-color: shade (&color1, 0.5);
615 * color: mix (&color1, #f0f, 0.8);</literallayout>
619 * <entry>color</entry>
622 * <entry>border-color</entry>
625 * <entry>font</entry>
626 * <entry>@family [@style] [@size]</entry>
627 * <entry>#PangoFontDescription</entry>
628 * <entry>font: Sans 15;</entry>
631 * <entry>margin</entry>
632 * <entry morerows="1"><literallayout>@width
633 * @vertical_width @horizontal_width
634 * @top_width @horizontal_width @bottom_width
635 * @top_width @right_width @bottom_width @left_width</literallayout>
637 * <entry morerows="1">#GtkBorder</entry>
638 * <entry morerows="1"><literallayout>margin: 5;
641 * margin: 5 10 3 5;</literallayout>
645 * <entry>padding</entry>
648 * <entry>background-image</entry>
649 * <entry><literallayout>gradient (see above) or
650 * url(@path)</literallayout></entry>
651 * <entry>#cairo_pattern_t</entry>
652 * <entry><literallayout>-gtk-gradient (linear,
653 * left top, right top,
654 * from (#fff), to (#000));
655 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
657 * color-stop (0.5, #f00),
659 * -gtk-gradient (radial,
660 * center center, 0.2,
661 * center center, 0.8,
662 * color-stop (0.0, #fff),
663 * color-stop (1.0, #000));
664 * url ('background.png');</literallayout>
668 * <entry>border-width</entry>
669 * <entry>integer</entry>
670 * <entry>#gint</entry>
671 * <entry>border-width: 5;</entry>
674 * <entry>border-radius</entry>
675 * <entry>integer</entry>
676 * <entry>#gint</entry>
677 * <entry>border-radius: 5;</entry>
680 * <entry>border-style</entry>
681 * <entry>[none|solid|inset|outset]</entry>
682 * <entry>#GtkBorderStyle</entry>
683 * <entry>border-style: solid;</entry>
686 * <entry>border-image</entry>
687 * <entry><literallayout>border image (see above)</literallayout></entry>
688 * <entry>internal use only</entry>
689 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
690 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
694 * <entry>transition</entry>
695 * <entry>transition (see above)</entry>
696 * <entry>internal use only</entry>
697 * <entry><literallayout>transition: 150ms ease-in-out;
698 * transition: 1s linear loop;</literallayout>
702 * <entry>gtk-key-bindings</entry>
703 * <entry>binding set name list</entry>
704 * <entry>internal use only</entry>
705 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
712 * GtkThemingEngines can register their own, engine-specific style properties
713 * with the function gtk_theming_engine_register_property(). These properties
714 * can be set in CSS like other properties, using a name of the form
715 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
716 * the name of the theming engine, and <replaceable>name</replaceable> is the
717 * name of the property. Style properties that have been registered by widgets
718 * using gtk_widget_class_install_style_property() can also be set in this
719 * way, using the widget class name for <replaceable>namespace</replaceable>.
722 * <title>Using engine-specific style properties</title>
725 * engine: clearlooks;
727 * -GtkPaned-handle-size: 6;
728 * -clearlooks-colorize-scrollbar: false;
735 typedef struct SelectorElement SelectorElement;
736 typedef struct SelectorPath SelectorPath;
737 typedef struct SelectorStyleInfo SelectorStyleInfo;
738 typedef enum SelectorElementType SelectorElementType;
739 typedef enum CombinatorType CombinatorType;
740 typedef enum ParserScope ParserScope;
741 typedef enum ParserSymbol ParserSymbol;
743 enum SelectorElementType {
752 enum CombinatorType {
753 COMBINATOR_DESCENDANT, /* No direct relation needed */
754 COMBINATOR_CHILD /* Direct child */
757 struct SelectorElement
759 SelectorElementType elem_type;
760 CombinatorType combinator;
770 GtkRegionFlags flags;
782 struct SelectorStyleInfo
788 struct _GtkCssProviderPrivate
794 const gchar *value_pos;
796 GHashTable *symbolic_colors;
798 GPtrArray *selectors_info;
800 /* Current parser state */
802 GSList *cur_selectors;
803 GHashTable *cur_properties;
815 /* Extend GtkStateType, since these
816 * values are also used as symbols
819 /* Scope: pseudo-class */
820 SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
825 /* Scope: nth-child */
826 SYMBOL_NTH_CHILD_EVEN,
827 SYMBOL_NTH_CHILD_ODD,
828 SYMBOL_NTH_CHILD_FIRST,
829 SYMBOL_NTH_CHILD_LAST
832 static void gtk_css_provider_finalize (GObject *object);
833 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
835 static void scanner_apply_scope (GScanner *scanner,
837 static void css_provider_reset_parser (GtkCssProvider *css_provider);
838 static gboolean css_provider_parse_value (GtkCssProvider *css_provider,
839 const gchar *value_str,
842 static gboolean gtk_css_provider_load_from_path_internal (GtkCssProvider *css_provider,
848 gtk_css_provider_error_quark (void)
850 return g_quark_from_static_string ("gtk-css-provider-error-quark");
853 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
854 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
855 gtk_css_style_provider_iface_init));
858 gtk_css_provider_class_init (GtkCssProviderClass *klass)
860 GObjectClass *object_class = G_OBJECT_CLASS (klass);
862 object_class->finalize = gtk_css_provider_finalize;
864 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
867 static SelectorPath *
868 selector_path_new (void)
872 path = g_slice_new0 (SelectorPath);
878 static SelectorPath *
879 selector_path_ref (SelectorPath *path)
886 selector_path_unref (SelectorPath *path)
890 if (path->ref_count > 0)
893 while (path->elements)
895 g_slice_free (SelectorElement, path->elements->data);
896 path->elements = g_slist_delete_link (path->elements, path->elements);
899 g_slice_free (SelectorPath, path);
903 selector_path_prepend_type (SelectorPath *path,
904 const gchar *type_name)
906 SelectorElement *elem;
909 elem = g_slice_new (SelectorElement);
910 elem->combinator = COMBINATOR_DESCENDANT;
911 type = g_type_from_name (type_name);
913 if (type == G_TYPE_INVALID)
915 elem->elem_type = SELECTOR_TYPE_NAME;
916 elem->name = g_quark_from_string (type_name);
920 elem->elem_type = SELECTOR_GTYPE;
924 path->elements = g_slist_prepend (path->elements, elem);
928 selector_path_prepend_glob (SelectorPath *path)
930 SelectorElement *elem;
932 elem = g_slice_new (SelectorElement);
933 elem->elem_type = SELECTOR_GLOB;
934 elem->combinator = COMBINATOR_DESCENDANT;
936 path->elements = g_slist_prepend (path->elements, elem);
940 selector_path_prepend_region (SelectorPath *path,
942 GtkRegionFlags flags)
944 SelectorElement *elem;
946 elem = g_slice_new (SelectorElement);
947 elem->combinator = COMBINATOR_DESCENDANT;
948 elem->elem_type = SELECTOR_REGION;
950 elem->region.name = g_quark_from_string (name);
951 elem->region.flags = flags;
953 path->elements = g_slist_prepend (path->elements, elem);
957 selector_path_prepend_name (SelectorPath *path,
960 SelectorElement *elem;
962 elem = g_slice_new (SelectorElement);
963 elem->combinator = COMBINATOR_DESCENDANT;
964 elem->elem_type = SELECTOR_NAME;
966 elem->name = g_quark_from_string (name);
968 path->elements = g_slist_prepend (path->elements, elem);
972 selector_path_prepend_class (SelectorPath *path,
975 SelectorElement *elem;
977 elem = g_slice_new (SelectorElement);
978 elem->combinator = COMBINATOR_DESCENDANT;
979 elem->elem_type = SELECTOR_CLASS;
981 elem->name = g_quark_from_string (name);
983 path->elements = g_slist_prepend (path->elements, elem);
987 selector_path_prepend_combinator (SelectorPath *path,
988 CombinatorType combinator)
990 SelectorElement *elem;
992 g_assert (path->elements != NULL);
994 /* It is actually stored in the last element */
995 elem = path->elements->data;
996 elem->combinator = combinator;
1000 selector_path_depth (SelectorPath *path)
1002 return g_slist_length (path->elements);
1005 static SelectorStyleInfo *
1006 selector_style_info_new (SelectorPath *path)
1008 SelectorStyleInfo *info;
1010 info = g_slice_new0 (SelectorStyleInfo);
1011 info->path = selector_path_ref (path);
1017 selector_style_info_free (SelectorStyleInfo *info)
1020 g_hash_table_unref (info->style);
1023 selector_path_unref (info->path);
1025 g_slice_free (SelectorStyleInfo, info);
1029 selector_style_info_set_style (SelectorStyleInfo *info,
1033 g_hash_table_unref (info->style);
1036 info->style = g_hash_table_ref (style);
1042 create_scanner (void)
1046 scanner = g_scanner_new (NULL);
1048 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "active", GUINT_TO_POINTER (GTK_STATE_ACTIVE));
1049 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "prelight", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
1050 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "hover", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
1051 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "selected", GUINT_TO_POINTER (GTK_STATE_SELECTED));
1052 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "insensitive", GUINT_TO_POINTER (GTK_STATE_INSENSITIVE));
1053 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "inconsistent", GUINT_TO_POINTER (GTK_STATE_INCONSISTENT));
1054 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focused", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
1055 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focus", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
1057 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "nth-child", GUINT_TO_POINTER (SYMBOL_NTH_CHILD));
1058 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "first-child", GUINT_TO_POINTER (SYMBOL_FIRST_CHILD));
1059 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "last-child", GUINT_TO_POINTER (SYMBOL_LAST_CHILD));
1060 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "sorted", GUINT_TO_POINTER (SYMBOL_SORTED_CHILD));
1062 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "even", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_EVEN));
1063 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "odd", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_ODD));
1064 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "first", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_FIRST));
1065 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "last", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_LAST));
1067 scanner_apply_scope (scanner, SCOPE_SELECTOR);
1073 gtk_css_provider_init (GtkCssProvider *css_provider)
1075 GtkCssProviderPrivate *priv;
1077 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1078 GTK_TYPE_CSS_PROVIDER,
1079 GtkCssProviderPrivate);
1081 priv->selectors_info = g_ptr_array_new_with_free_func ((GDestroyNotify) selector_style_info_free);
1082 priv->scanner = create_scanner ();
1084 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1085 (GDestroyNotify) g_free,
1086 (GDestroyNotify) gtk_symbolic_color_unref);
1089 typedef struct ComparePathData ComparePathData;
1091 struct ComparePathData
1099 compare_selector_element (GtkWidgetPath *path,
1101 SelectorElement *elem,
1106 if (elem->elem_type == SELECTOR_TYPE_NAME)
1108 const gchar *type_name;
1109 GType resolved_type;
1111 /* Resolve the type name */
1112 type_name = g_quark_to_string (elem->name);
1113 resolved_type = g_type_from_name (type_name);
1115 if (resolved_type == G_TYPE_INVALID)
1117 /* Type couldn't be resolved, so the selector
1118 * clearly doesn't affect the given widget path
1123 elem->elem_type = SELECTOR_GTYPE;
1124 elem->type = resolved_type;
1127 if (elem->elem_type == SELECTOR_GTYPE)
1131 type = gtk_widget_path_iter_get_object_type (path, index);
1133 if (!g_type_is_a (type, elem->type))
1136 if (type == elem->type)
1140 guint diff = g_type_depth (type) - g_type_depth (elem->type);
1142 if (G_UNLIKELY (diff > 0xE))
1144 g_warning ("Hierarchy is higher than expected.");
1148 *score = 0XF - diff;
1153 else if (elem->elem_type == SELECTOR_REGION)
1155 GtkRegionFlags flags;
1157 if (!gtk_widget_path_iter_has_qregion (path, index,
1162 if (elem->region.flags != 0 &&
1163 (flags & elem->region.flags) == 0)
1169 else if (elem->elem_type == SELECTOR_GLOB)
1171 /* Treat as lowest matching type */
1175 else if (elem->elem_type == SELECTOR_NAME)
1177 if (!gtk_widget_path_iter_has_qname (path, index, elem->name))
1183 else if (elem->elem_type == SELECTOR_CLASS)
1185 if (!gtk_widget_path_iter_has_qclass (path, index, elem->name))
1196 compare_selector (GtkWidgetPath *path,
1197 SelectorPath *selector)
1199 GSList *elements = selector->elements;
1200 gboolean match = TRUE, first = TRUE, first_match = FALSE;
1204 i = gtk_widget_path_length (path) - 1;
1206 while (elements && match && i >= 0)
1208 SelectorElement *elem;
1211 elem = elements->data;
1213 match = compare_selector_element (path, i, elem, &elem_score);
1218 /* Only move on to the next index if there is no match
1219 * with the current element (whether to continue or not
1220 * handled right after in the combinator check), or a
1221 * GType or glob has just been matched.
1223 * Region and widget names do not trigger this because
1224 * the next element in the selector path could also be
1225 * related to the same index.
1228 (elem->elem_type == SELECTOR_GTYPE ||
1229 elem->elem_type == SELECTOR_GLOB))
1233 elem->elem_type != SELECTOR_TYPE_NAME &&
1234 elem->combinator == COMBINATOR_DESCENDANT)
1236 /* With descendant combinators there may
1237 * be intermediate chidren in the hierarchy
1242 elements = elements->next;
1246 /* Only 4 bits are actually used */
1248 score |= elem_score;
1254 /* If there are pending selector
1255 * elements to compare, it's not
1263 else if (first_match)
1265 /* Assign more weight to these selectors
1266 * that matched right from the first element.
1274 typedef struct StylePriorityInfo StylePriorityInfo;
1276 struct StylePriorityInfo
1280 GtkStateFlags state;
1284 css_provider_get_selectors (GtkCssProvider *css_provider,
1285 GtkWidgetPath *path)
1287 GtkCssProviderPrivate *priv;
1288 GArray *priority_info;
1291 priv = css_provider->priv;
1292 priority_info = g_array_new (FALSE, FALSE, sizeof (StylePriorityInfo));
1294 for (i = 0; i < priv->selectors_info->len; i++)
1296 SelectorStyleInfo *info;
1297 StylePriorityInfo new;
1298 gboolean added = FALSE;
1301 info = g_ptr_array_index (priv->selectors_info, i);
1302 score = compare_selector (path, info->path);
1308 new.style = info->style;
1309 new.state = info->path->state;
1311 for (j = 0; j < priority_info->len; j++)
1313 StylePriorityInfo *cur;
1315 cur = &g_array_index (priority_info, StylePriorityInfo, j);
1317 if (cur->score > new.score)
1319 g_array_insert_val (priority_info, j, new);
1326 g_array_append_val (priority_info, new);
1329 return priority_info;
1333 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1334 GtkStyleProperties *props)
1336 GtkCssProviderPrivate *priv;
1337 GHashTableIter iter;
1338 gpointer key, value;
1340 priv = css_provider->priv;
1341 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1343 while (g_hash_table_iter_next (&iter, &key, &value))
1346 GtkSymbolicColor *color;
1351 gtk_style_properties_map_color (props, name, color);
1355 static GtkStyleProperties *
1356 gtk_css_provider_get_style (GtkStyleProvider *provider,
1357 GtkWidgetPath *path)
1359 GtkCssProvider *css_provider;
1360 GtkStyleProperties *props;
1361 GArray *priority_info;
1364 css_provider = GTK_CSS_PROVIDER (provider);
1365 props = gtk_style_properties_new ();
1367 css_provider_dump_symbolic_colors (css_provider, props);
1368 priority_info = css_provider_get_selectors (css_provider, path);
1370 for (i = 0; i < priority_info->len; i++)
1372 StylePriorityInfo *info;
1373 GHashTableIter iter;
1374 gpointer key, value;
1376 info = &g_array_index (priority_info, StylePriorityInfo, i);
1377 g_hash_table_iter_init (&iter, info->style);
1379 while (g_hash_table_iter_next (&iter, &key, &value))
1383 /* Properties starting with '-' may be both widget style properties
1384 * or custom properties from the theming engine, so check whether
1385 * the type is registered or not.
1387 if (prop[0] == '-' &&
1388 !gtk_style_properties_lookup_property (prop, NULL, NULL))
1391 gtk_style_properties_set_property (props, key, info->state, value);
1395 g_array_free (priority_info, TRUE);
1401 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1402 GtkWidgetPath *path,
1403 GtkStateFlags state,
1407 GArray *priority_info;
1408 gboolean found = FALSE;
1412 prop_name = g_strdup_printf ("-%s-%s",
1413 g_type_name (pspec->owner_type),
1416 priority_info = css_provider_get_selectors (GTK_CSS_PROVIDER (provider), path);
1418 for (i = priority_info->len - 1; i >= 0; i--)
1420 StylePriorityInfo *info;
1423 info = &g_array_index (priority_info, StylePriorityInfo, i);
1424 val = g_hash_table_lookup (info->style, prop_name);
1427 (info->state == 0 ||
1428 info->state == state ||
1429 ((info->state & state) != 0 &&
1430 (info->state & ~(state)) == 0)))
1432 const gchar *val_str;
1434 val_str = g_value_get_string (val);
1437 css_provider_parse_value (GTK_CSS_PROVIDER (provider), val_str, value, NULL);
1442 g_array_free (priority_info, TRUE);
1449 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1451 iface->get_style = gtk_css_provider_get_style;
1452 iface->get_style_property = gtk_css_provider_get_style_property;
1456 gtk_css_provider_finalize (GObject *object)
1458 GtkCssProvider *css_provider;
1459 GtkCssProviderPrivate *priv;
1461 css_provider = GTK_CSS_PROVIDER (object);
1462 priv = css_provider->priv;
1464 css_provider_reset_parser (css_provider);
1466 g_scanner_destroy (priv->scanner);
1467 g_free (priv->filename);
1469 g_ptr_array_free (priv->selectors_info, TRUE);
1471 g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
1472 g_slist_free (priv->cur_selectors);
1474 if (priv->cur_properties)
1475 g_hash_table_unref (priv->cur_properties);
1476 if (priv->symbolic_colors)
1477 g_hash_table_destroy (priv->symbolic_colors);
1479 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1483 * gtk_css_provider_new:
1485 * Returns a newly created #GtkCssProvider.
1487 * Returns: A new #GtkCssProvider
1490 gtk_css_provider_new (void)
1492 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1496 property_value_free (GValue *value)
1498 if (G_IS_VALUE (value))
1499 g_value_unset (value);
1501 g_slice_free (GValue, value);
1505 scanner_apply_scope (GScanner *scanner,
1508 g_scanner_set_scope (scanner, scope);
1510 if (scope == SCOPE_VALUE)
1512 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
1513 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(),.%\t\n'/\"";
1514 scanner->config->scan_identifier_1char = TRUE;
1516 else if (scope == SCOPE_BINDING_SET)
1518 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
1519 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(){}<>,.%\t\n'/\"";
1520 scanner->config->scan_identifier_1char = TRUE;
1522 else if (scope == SCOPE_SELECTOR)
1524 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "*@";
1525 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_#.";
1526 scanner->config->scan_identifier_1char = TRUE;
1528 else if (scope == SCOPE_PSEUDO_CLASS ||
1529 scope == SCOPE_NTH_CHILD ||
1530 scope == SCOPE_DECLARATION)
1532 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "-_";
1533 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_";
1534 scanner->config->scan_identifier_1char = FALSE;
1537 g_assert_not_reached ();
1539 scanner->config->scan_float = FALSE;
1540 scanner->config->cpair_comment_single = NULL;
1544 css_provider_push_scope (GtkCssProvider *css_provider,
1547 GtkCssProviderPrivate *priv;
1549 priv = css_provider->priv;
1550 priv->state = g_slist_prepend (priv->state, GUINT_TO_POINTER (scope));
1552 scanner_apply_scope (priv->scanner, scope);
1556 css_provider_pop_scope (GtkCssProvider *css_provider)
1558 GtkCssProviderPrivate *priv;
1559 ParserScope scope = SCOPE_SELECTOR;
1561 priv = css_provider->priv;
1565 g_warning ("Push/pop calls to parser scope aren't paired");
1566 scanner_apply_scope (priv->scanner, SCOPE_SELECTOR);
1567 return SCOPE_SELECTOR;
1570 priv->state = g_slist_delete_link (priv->state, priv->state);
1572 /* Fetch new scope */
1574 scope = GPOINTER_TO_INT (priv->state->data);
1576 scanner_apply_scope (priv->scanner, scope);
1582 css_provider_reset_parser (GtkCssProvider *css_provider)
1584 GtkCssProviderPrivate *priv;
1586 priv = css_provider->priv;
1588 g_slist_free (priv->state);
1591 scanner_apply_scope (priv->scanner, SCOPE_SELECTOR);
1592 priv->scanner->user_data = NULL;
1593 priv->value_pos = NULL;
1595 g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
1596 g_slist_free (priv->cur_selectors);
1597 priv->cur_selectors = NULL;
1599 if (priv->cur_properties)
1600 g_hash_table_unref (priv->cur_properties);
1602 priv->cur_properties = g_hash_table_new_full (g_str_hash,
1604 (GDestroyNotify) g_free,
1605 (GDestroyNotify) property_value_free);
1609 css_provider_commit (GtkCssProvider *css_provider)
1611 GtkCssProviderPrivate *priv;
1614 priv = css_provider->priv;
1615 l = priv->cur_selectors;
1619 SelectorPath *path = l->data;
1620 SelectorStyleInfo *info;
1622 info = selector_style_info_new (path);
1623 selector_style_info_set_style (info, priv->cur_properties);
1625 g_ptr_array_add (priv->selectors_info, info);
1631 parse_nth_child (GtkCssProvider *css_provider,
1633 GtkRegionFlags *flags)
1635 ParserSymbol symbol;
1637 g_scanner_get_next_token (scanner);
1639 if (scanner->token != G_TOKEN_SYMBOL)
1640 return G_TOKEN_SYMBOL;
1642 symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
1644 if (symbol == SYMBOL_NTH_CHILD)
1646 g_scanner_get_next_token (scanner);
1648 if (scanner->token != G_TOKEN_LEFT_PAREN)
1649 return G_TOKEN_LEFT_PAREN;
1651 css_provider_push_scope (css_provider, SCOPE_NTH_CHILD);
1652 g_scanner_get_next_token (scanner);
1654 if (scanner->token != G_TOKEN_SYMBOL)
1655 return G_TOKEN_SYMBOL;
1657 symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
1661 case SYMBOL_NTH_CHILD_EVEN:
1662 *flags = GTK_REGION_EVEN;
1664 case SYMBOL_NTH_CHILD_ODD:
1665 *flags = GTK_REGION_ODD;
1667 case SYMBOL_NTH_CHILD_FIRST:
1668 *flags = GTK_REGION_FIRST;
1670 case SYMBOL_NTH_CHILD_LAST:
1671 *flags = GTK_REGION_LAST;
1677 g_scanner_get_next_token (scanner);
1679 if (scanner->token != G_TOKEN_RIGHT_PAREN)
1680 return G_TOKEN_RIGHT_PAREN;
1682 css_provider_pop_scope (css_provider);
1684 else if (symbol == SYMBOL_FIRST_CHILD)
1685 *flags = GTK_REGION_FIRST;
1686 else if (symbol == SYMBOL_LAST_CHILD)
1687 *flags = GTK_REGION_LAST;
1688 else if (symbol == SYMBOL_SORTED_CHILD)
1689 *flags = GTK_REGION_SORTED;
1693 return G_TOKEN_SYMBOL;
1696 return G_TOKEN_NONE;
1700 parse_pseudo_class (GtkCssProvider *css_provider,
1702 SelectorPath *selector)
1706 g_scanner_get_next_token (scanner);
1708 if (scanner->token != G_TOKEN_SYMBOL)
1709 return G_TOKEN_SYMBOL;
1711 state = GPOINTER_TO_INT (scanner->value.v_symbol);
1715 case GTK_STATE_ACTIVE:
1716 selector->state |= GTK_STATE_FLAG_ACTIVE;
1718 case GTK_STATE_PRELIGHT:
1719 selector->state |= GTK_STATE_FLAG_PRELIGHT;
1721 case GTK_STATE_SELECTED:
1722 selector->state |= GTK_STATE_FLAG_SELECTED;
1724 case GTK_STATE_INSENSITIVE:
1725 selector->state |= GTK_STATE_FLAG_INSENSITIVE;
1727 case GTK_STATE_INCONSISTENT:
1728 selector->state |= GTK_STATE_FLAG_INCONSISTENT;
1730 case GTK_STATE_FOCUSED:
1731 selector->state |= GTK_STATE_FLAG_FOCUSED;
1734 return G_TOKEN_SYMBOL;
1737 return G_TOKEN_NONE;
1740 /* Parses a number of concatenated classes */
1742 parse_classes (SelectorPath *path,
1747 if ((pos = strchr (str, '.')) != NULL)
1749 /* Leave the last class to the call after the loop */
1753 selector_path_prepend_class (path, str);
1756 pos = strchr (str, '.');
1760 selector_path_prepend_class (path, str);
1764 is_widget_class_name (const gchar *str)
1766 /* Do a pretty lax check here, not all
1767 * widget class names contain only CamelCase
1768 * (gtkmm widgets don't), but at least part of
1769 * the name will be CamelCase, so check for
1770 * the first uppercase char
1774 if (g_ascii_isupper (*str))
1784 parse_selector (GtkCssProvider *css_provider,
1786 SelectorPath **selector_out)
1790 path = selector_path_new ();
1791 *selector_out = path;
1793 if (scanner->token != ':' &&
1794 scanner->token != '#' &&
1795 scanner->token != '.' &&
1796 scanner->token != G_TOKEN_IDENTIFIER)
1797 return G_TOKEN_IDENTIFIER;
1799 while (scanner->token == '#' ||
1800 scanner->token == '.' ||
1801 scanner->token == G_TOKEN_IDENTIFIER)
1803 if (scanner->token == '#' ||
1804 scanner->token == '.')
1809 is_class = (scanner->token == '.');
1811 g_scanner_get_next_token (scanner);
1813 if (scanner->token != G_TOKEN_IDENTIFIER)
1814 return G_TOKEN_IDENTIFIER;
1816 selector_path_prepend_glob (path);
1817 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
1820 parse_classes (path, scanner->value.v_identifier);
1823 if ((pos = strchr (scanner->value.v_identifier, '.')) != NULL)
1826 selector_path_prepend_name (path, scanner->value.v_identifier);
1828 /* Parse any remaining classes */
1830 parse_classes (path, pos + 1);
1833 else if (is_widget_class_name (scanner->value.v_identifier))
1837 if ((pos = strchr (scanner->value.v_identifier, '#')) != NULL ||
1838 (pos = strchr (scanner->value.v_identifier, '.')) != NULL)
1840 gchar *type_name, *name;
1843 is_class = (*pos == '.');
1845 /* Widget type and name/class put together */
1848 type_name = scanner->value.v_identifier;
1850 selector_path_prepend_type (path, type_name);
1852 /* This is only so there is a direct relationship
1853 * between widget type and its name.
1855 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
1858 parse_classes (path, name);
1861 if ((pos = strchr (name, '.')) != NULL)
1864 selector_path_prepend_name (path, name);
1866 /* Parse any remaining classes */
1868 parse_classes (path, pos + 1);
1872 selector_path_prepend_type (path, scanner->value.v_identifier);
1874 else if (_gtk_style_context_check_region_name (scanner->value.v_identifier))
1876 GtkRegionFlags flags = 0;
1879 region_name = g_strdup (scanner->value.v_identifier);
1881 if (g_scanner_peek_next_token (scanner) == ':')
1883 ParserSymbol symbol;
1885 g_scanner_get_next_token (scanner);
1886 css_provider_push_scope (css_provider, SCOPE_PSEUDO_CLASS);
1888 /* Check for the next token being nth-child, parse in that
1889 * case, and fallback into common state parsing if not.
1891 if (g_scanner_peek_next_token (scanner) != G_TOKEN_SYMBOL)
1892 return G_TOKEN_SYMBOL;
1894 symbol = GPOINTER_TO_INT (scanner->next_value.v_symbol);
1896 if (symbol == SYMBOL_FIRST_CHILD ||
1897 symbol == SYMBOL_LAST_CHILD ||
1898 symbol == SYMBOL_NTH_CHILD ||
1899 symbol == SYMBOL_SORTED_CHILD)
1903 if ((token = parse_nth_child (css_provider, scanner, &flags)) != G_TOKEN_NONE)
1906 css_provider_pop_scope (css_provider);
1910 css_provider_pop_scope (css_provider);
1911 selector_path_prepend_region (path, region_name, 0);
1912 g_free (region_name);
1917 selector_path_prepend_region (path, region_name, flags);
1918 g_free (region_name);
1920 else if (scanner->value.v_identifier[0] == '*')
1921 selector_path_prepend_glob (path);
1923 return G_TOKEN_IDENTIFIER;
1925 g_scanner_get_next_token (scanner);
1927 if (scanner->token == '>')
1929 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
1930 g_scanner_get_next_token (scanner);
1934 if (scanner->token == ':')
1936 /* Add glob selector if path is empty */
1937 if (selector_path_depth (path) == 0)
1938 selector_path_prepend_glob (path);
1940 css_provider_push_scope (css_provider, SCOPE_PSEUDO_CLASS);
1942 while (scanner->token == ':')
1946 if ((token = parse_pseudo_class (css_provider, scanner, path)) != G_TOKEN_NONE)
1949 g_scanner_get_next_token (scanner);
1952 css_provider_pop_scope (css_provider);
1955 return G_TOKEN_NONE;
1958 #define SKIP_SPACES(s) while (s[0] == ' ' || s[0] == '\t' || s[0] == '\n') s++;
1959 #define SKIP_SPACES_BACK(s) while (s[0] == ' ' || s[0] == '\t' || s[0] == '\n') s--;
1961 static GtkSymbolicColor *
1962 symbolic_color_parse_str (const gchar *string,
1965 GtkSymbolicColor *symbolic_color = NULL;
1968 str = (gchar *) string;
1979 while (*end == '-' || *end == '_' || g_ascii_isalnum (*end))
1982 name = g_strndup (str, end - str);
1983 symbolic_color = gtk_symbolic_color_new_name (name);
1986 *end_ptr = (gchar *) end;
1988 else if (g_str_has_prefix (str, "lighter") ||
1989 g_str_has_prefix (str, "darker"))
1991 GtkSymbolicColor *param_color;
1992 gboolean is_lighter = FALSE;
1994 is_lighter = g_str_has_prefix (str, "lighter");
1997 str += strlen ("lighter");
1999 str += strlen ("darker");
2005 *end_ptr = (gchar *) str;
2011 param_color = symbolic_color_parse_str (str, end_ptr);
2018 *end_ptr = (gchar *) str;
2022 gtk_symbolic_color_unref (param_color);
2027 symbolic_color = gtk_symbolic_color_new_shade (param_color, 1.3);
2029 symbolic_color = gtk_symbolic_color_new_shade (param_color, 0.7);
2031 gtk_symbolic_color_unref (param_color);
2034 else if (g_str_has_prefix (str, "shade") ||
2035 g_str_has_prefix (str, "alpha"))
2037 GtkSymbolicColor *param_color;
2038 gboolean is_shade = FALSE;
2041 is_shade = g_str_has_prefix (str, "shade");
2044 str += strlen ("shade");
2046 str += strlen ("alpha");
2052 *end_ptr = (gchar *) str;
2058 param_color = symbolic_color_parse_str (str, end_ptr);
2068 gtk_symbolic_color_unref (param_color);
2069 *end_ptr = (gchar *) str;
2075 factor = g_ascii_strtod (str, end_ptr);
2079 *end_ptr = (gchar *) str;
2083 gtk_symbolic_color_unref (param_color);
2088 symbolic_color = gtk_symbolic_color_new_shade (param_color, factor);
2090 symbolic_color = gtk_symbolic_color_new_alpha (param_color, factor);
2092 gtk_symbolic_color_unref (param_color);
2095 else if (g_str_has_prefix (str, "mix"))
2097 GtkSymbolicColor *color1, *color2;
2100 str += strlen ("mix");
2105 *end_ptr = (gchar *) str;
2111 color1 = symbolic_color_parse_str (str, end_ptr);
2121 gtk_symbolic_color_unref (color1);
2122 *end_ptr = (gchar *) str;
2128 color2 = symbolic_color_parse_str (str, end_ptr);
2130 if (!color2 || *end_ptr[0] != ',')
2132 gtk_symbolic_color_unref (color1);
2141 gtk_symbolic_color_unref (color1);
2142 gtk_symbolic_color_unref (color2);
2143 *end_ptr = (gchar *) str;
2149 factor = g_ascii_strtod (str, end_ptr);
2153 *end_ptr = (gchar *) str;
2157 gtk_symbolic_color_unref (color1);
2158 gtk_symbolic_color_unref (color2);
2162 symbolic_color = gtk_symbolic_color_new_mix (color1, color2, factor);
2163 gtk_symbolic_color_unref (color1);
2164 gtk_symbolic_color_unref (color2);
2177 /* Color in hex format */
2178 while (g_ascii_isxdigit (*end))
2181 else if (g_str_has_prefix (str, "rgb"))
2183 /* color in rgb/rgba format */
2184 while (*end != ')' && *end != '\0')
2193 while (*end != '\0' &&
2194 (g_ascii_isalnum (*end) || *end == ' '))
2198 color_str = g_strndup (str, end - str);
2199 *end_ptr = (gchar *) end;
2201 if (!gdk_rgba_parse (&color, color_str))
2207 symbolic_color = gtk_symbolic_color_new_literal (&color);
2211 return symbolic_color;
2214 static GtkSymbolicColor *
2215 symbolic_color_parse (const gchar *str,
2218 GtkSymbolicColor *color;
2221 color = symbolic_color_parse_str (str, &end);
2225 g_set_error_literal (error,
2226 GTK_CSS_PROVIDER_ERROR,
2227 GTK_CSS_PROVIDER_ERROR_FAILED,
2228 "Could not parse symbolic color");
2232 gtk_symbolic_color_unref (color);
2240 static GtkGradient *
2241 gradient_parse_str (const gchar *str,
2244 GtkGradient *gradient = NULL;
2249 if (g_str_has_prefix (str, "-gtk-gradient"))
2251 cairo_pattern_type_t type;
2253 str += strlen ("-gtk-gradient");
2258 *end_ptr = (gchar *) str;
2265 /* Parse gradient type */
2266 if (g_str_has_prefix (str, "linear"))
2268 type = CAIRO_PATTERN_TYPE_LINEAR;
2269 str += strlen ("linear");
2271 else if (g_str_has_prefix (str, "radial"))
2273 type = CAIRO_PATTERN_TYPE_RADIAL;
2274 str += strlen ("radial");
2278 *end_ptr = (gchar *) str;
2284 /* Parse start/stop position parameters */
2285 for (i = 0; i < 2; i++)
2289 *end_ptr = (gchar *) str;
2296 if (strncmp (str, "left", 4) == 0)
2299 str += strlen ("left");
2301 else if (strncmp (str, "right", 5) == 0)
2304 str += strlen ("right");
2306 else if (strncmp (str, "center", 6) == 0)
2308 coords[i * 3] = 0.5;
2309 str += strlen ("center");
2313 coords[i * 3] = g_ascii_strtod (str, &end);
2317 *end_ptr = (gchar *) str;
2326 if (strncmp (str, "top", 3) == 0)
2328 coords[(i * 3) + 1] = 0;
2329 str += strlen ("top");
2331 else if (strncmp (str, "bottom", 6) == 0)
2333 coords[(i * 3) + 1] = 1;
2334 str += strlen ("bottom");
2336 else if (strncmp (str, "center", 6) == 0)
2338 coords[(i * 3) + 1] = 0.5;
2339 str += strlen ("center");
2343 coords[(i * 3) + 1] = g_ascii_strtod (str, &end);
2347 *end_ptr = (gchar *) str;
2356 if (type == CAIRO_PATTERN_TYPE_RADIAL)
2361 *end_ptr = (gchar *) str;
2368 coords[(i * 3) + 2] = g_ascii_strtod (str, &end);
2375 if (type == CAIRO_PATTERN_TYPE_LINEAR)
2376 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
2378 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
2379 coords[3], coords[4], coords[5]);
2383 GtkSymbolicColor *color;
2388 *end_ptr = (gchar *) str;
2395 if (g_str_has_prefix (str, "from"))
2398 str += strlen ("from");
2403 *end_ptr = (gchar *) str;
2407 else if (g_str_has_prefix (str, "to"))
2410 str += strlen ("to");
2415 *end_ptr = (gchar *) str;
2419 else if (g_str_has_prefix (str, "color-stop"))
2421 str += strlen ("color-stop");
2426 *end_ptr = (gchar *) str;
2433 position = g_ascii_strtod (str, &end);
2440 *end_ptr = (gchar *) str;
2446 *end_ptr = (gchar *) str;
2453 color = symbolic_color_parse_str (str, &end);
2461 gtk_symbolic_color_unref (color);
2462 *end_ptr = (gchar *) str;
2471 gtk_gradient_add_color_stop (gradient, position, color);
2472 gtk_symbolic_color_unref (color);
2478 *end_ptr = (gchar *) str;
2485 *end_ptr = (gchar *) str;
2491 path_parse_str (GtkCssProvider *css_provider,
2497 const gchar *start, *end;
2500 if (g_str_has_prefix (str, "url"))
2502 str += strlen ("url");
2507 *end_ptr = (gchar *) str;
2511 chr = strchr (str, ')');
2514 *end_ptr = (gchar *) str;
2523 if (*str == '"' || *str == '\'')
2530 SKIP_SPACES_BACK (chr);
2532 if (*chr != *p || chr == p)
2534 *end_ptr = (gchar *)str;
2540 *end_ptr = (gchar *)str;
2544 path = g_strndup (str, chr - str);
2547 *end_ptr = (gchar *)end;
2551 path = g_strdup (str);
2552 *end_ptr = (gchar *)str + strlen (str);
2555 /* Always return an absolute path */
2556 if (!g_path_is_absolute (path))
2558 GtkCssProviderPrivate *priv;
2559 gchar *dirname, *full_path;
2561 priv = css_provider->priv;
2563 /* Use relative path to the current CSS file path, if any */
2565 dirname = g_path_get_dirname (priv->filename);
2567 dirname = g_get_current_dir ();
2569 full_path = g_build_filename (dirname, path, NULL);
2576 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
2578 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_EXIST,
2579 "File doesn't exist: %s", path);
2582 *end_ptr = (gchar *)start;
2589 path_parse (GtkCssProvider *css_provider,
2596 path = path_parse_str (css_provider, str, &end, error);
2603 g_set_error_literal (error,
2604 GTK_CSS_PROVIDER_ERROR,
2605 GTK_CSS_PROVIDER_ERROR_FAILED,
2606 "Error parsing path");
2615 slice_parse_str (GtkCssProvider *css_provider,
2620 gdouble distance_top, distance_bottom;
2621 gdouble distance_left, distance_right;
2622 GtkSliceSideModifier mods[2];
2630 /* Parse image url */
2631 path = path_parse_str (css_provider, str, end_ptr, error);
2639 /* Parse top/left/bottom/right distances */
2640 distance_top = g_ascii_strtod (str, end_ptr);
2645 distance_right = g_ascii_strtod (str, end_ptr);
2650 distance_bottom = g_ascii_strtod (str, end_ptr);
2655 distance_left = g_ascii_strtod (str, end_ptr);
2660 while (*str && i < 2)
2662 if (g_str_has_prefix (str, "stretch"))
2664 str += strlen ("stretch");
2665 mods[i] = GTK_SLICE_STRETCH;
2667 else if (g_str_has_prefix (str, "repeat"))
2669 str += strlen ("repeat");
2670 mods[i] = GTK_SLICE_REPEAT;
2675 *end_ptr = (gchar *) str;
2683 *end_ptr = (gchar *) str;
2693 /* Fill in second modifier, same as the first */
2697 pixbuf = gdk_pixbuf_new_from_file (path, error);
2702 *end_ptr = (gchar *) str;
2706 slice = _gtk_9slice_new (pixbuf,
2707 distance_top, distance_bottom,
2708 distance_left, distance_right,
2710 g_object_unref (pixbuf);
2716 unit_parse_str (const gchar *str,
2722 unit = g_ascii_strtod (str, end_str);
2725 /* Now parse the unit type, if any. We
2726 * don't admit spaces between these.
2728 if (*str != ' ' && *str != '\0')
2730 while (**end_str != ' ' && **end_str != '\0')
2733 /* Only handle pixels at the moment */
2734 if (strncmp (str, "px", 2) != 0)
2738 type = g_strndup (str, *end_str - str);
2739 g_warning ("Unknown unit '%s', only pixel units are "
2740 "currently supported in CSS style", type);
2749 border_parse_str (const gchar *str,
2752 gdouble first, second, third, fourth;
2755 border = gtk_border_new ();
2758 if (!g_ascii_isdigit (*str) && *str != '-')
2761 first = unit_parse_str (str, end_str);
2765 if (!g_ascii_isdigit (*str) && *str != '-')
2767 border->left = border->right = border->top = border->bottom = (gint) first;
2768 *end_str = (gchar *) str;
2772 second = unit_parse_str (str, end_str);
2776 if (!g_ascii_isdigit (*str) && *str != '-')
2778 border->top = border->bottom = (gint) first;
2779 border->left = border->right = (gint) second;
2780 *end_str = (gchar *) str;
2784 third = unit_parse_str (str, end_str);
2788 if (!g_ascii_isdigit (*str) && *str != '-')
2790 border->top = (gint) first;
2791 border->left = border->right = (gint) second;
2792 border->bottom = (gint) third;
2793 *end_str = (gchar *) str;
2797 fourth = unit_parse_str (str, end_str);
2799 border->top = (gint) first;
2800 border->right = (gint) second;
2801 border->bottom = (gint) third;
2802 border->left = (gint) fourth;
2808 resolve_binding_sets (const gchar *value_str,
2812 gchar **bindings, **str;
2814 bindings = g_strsplit (value_str, ",", -1);
2815 array = g_ptr_array_new ();
2817 for (str = bindings; *str; str++)
2819 GtkBindingSet *binding_set;
2821 binding_set = gtk_binding_set_find (g_strstrip (*str));
2826 g_ptr_array_add (array, binding_set);
2829 g_value_take_boxed (value, array);
2830 g_strfreev (bindings);
2834 css_provider_parse_value (GtkCssProvider *css_provider,
2835 const gchar *value_str,
2839 GtkCssProviderPrivate *priv;
2841 gboolean parsed = TRUE;
2844 priv = css_provider->priv;
2845 type = G_VALUE_TYPE (value);
2847 if (type == GDK_TYPE_RGBA ||
2848 type == GDK_TYPE_COLOR)
2853 if (type == GDK_TYPE_RGBA &&
2854 gdk_rgba_parse (&rgba, value_str))
2855 g_value_set_boxed (value, &rgba);
2856 else if (type == GDK_TYPE_COLOR &&
2857 gdk_color_parse (value_str, &color))
2858 g_value_set_boxed (value, &color);
2861 GtkSymbolicColor *symbolic_color;
2863 symbolic_color = symbolic_color_parse_str (value_str, &end);
2867 g_value_unset (value);
2868 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
2869 g_value_take_boxed (value, symbolic_color);
2875 else if (type == PANGO_TYPE_FONT_DESCRIPTION)
2877 PangoFontDescription *font_desc;
2879 font_desc = pango_font_description_from_string (value_str);
2880 g_value_take_boxed (value, font_desc);
2882 else if (type == G_TYPE_BOOLEAN)
2884 if (value_str[0] == '1' ||
2885 g_ascii_strcasecmp (value_str, "true") == 0)
2886 g_value_set_boolean (value, TRUE);
2888 g_value_set_boolean (value, FALSE);
2890 else if (type == G_TYPE_INT)
2891 g_value_set_int (value, atoi (value_str));
2892 else if (type == G_TYPE_UINT)
2893 g_value_set_uint (value, (guint) atoi (value_str));
2894 else if (type == G_TYPE_DOUBLE)
2895 g_value_set_double (value, g_ascii_strtod (value_str, NULL));
2896 else if (type == G_TYPE_FLOAT)
2897 g_value_set_float (value, (gfloat) g_ascii_strtod (value_str, NULL));
2898 else if (type == GTK_TYPE_THEMING_ENGINE)
2900 GtkThemingEngine *engine;
2902 engine = gtk_theming_engine_load (value_str);
2904 g_value_set_object (value, engine);
2908 else if (type == GTK_TYPE_ANIMATION_DESCRIPTION)
2910 GtkAnimationDescription *desc;
2912 desc = _gtk_animation_description_from_string (value_str);
2915 g_value_take_boxed (value, desc);
2919 else if (type == GTK_TYPE_BORDER)
2923 border = border_parse_str (value_str, &end);
2924 g_value_take_boxed (value, border);
2926 else if (type == CAIRO_GOBJECT_TYPE_PATTERN)
2928 GtkGradient *gradient;
2930 gradient = gradient_parse_str (value_str, &end);
2934 g_value_unset (value);
2935 g_value_init (value, GTK_TYPE_GRADIENT);
2936 g_value_take_boxed (value, gradient);
2943 g_clear_error (error);
2944 path = path_parse_str (css_provider, value_str, &end, error);
2948 pixbuf = gdk_pixbuf_new_from_file (path, NULL);
2953 cairo_surface_t *surface;
2954 cairo_pattern_t *pattern;
2956 cairo_matrix_t matrix;
2958 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2959 gdk_pixbuf_get_width (pixbuf),
2960 gdk_pixbuf_get_height (pixbuf));
2961 cr = cairo_create (surface);
2962 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
2964 pattern = cairo_pattern_create_for_surface (surface);
2966 cairo_matrix_init_scale (&matrix,
2967 gdk_pixbuf_get_width (pixbuf),
2968 gdk_pixbuf_get_height (pixbuf));
2969 cairo_pattern_set_matrix (pattern, &matrix);
2971 cairo_surface_destroy (surface);
2973 g_object_unref (pixbuf);
2975 g_value_take_boxed (value, pattern);
2984 else if (G_TYPE_IS_ENUM (type))
2986 GEnumClass *enum_class;
2987 GEnumValue *enum_value;
2989 enum_class = g_type_class_ref (type);
2990 enum_value = g_enum_get_value_by_nick (enum_class, value_str);
2995 GTK_CSS_PROVIDER_ERROR,
2996 GTK_CSS_PROVIDER_ERROR_FAILED,
2997 "Unknown value '%s' for enum type '%s'",
2998 value_str, g_type_name (type));
3002 g_value_set_enum (value, enum_value->value);
3004 g_type_class_unref (enum_class);
3006 else if (G_TYPE_IS_FLAGS (type))
3008 GFlagsClass *flags_class;
3009 GFlagsValue *flag_value;
3013 flags_class = g_type_class_ref (type);
3015 /* Parse comma separated values */
3016 ptr = strchr (value_str, ',');
3018 while (ptr && parsed)
3025 flag_str = (gchar *) value_str;
3026 flag_value = g_flags_get_value_by_nick (flags_class,
3027 g_strstrip (flag_str));
3032 GTK_CSS_PROVIDER_ERROR,
3033 GTK_CSS_PROVIDER_ERROR_FAILED,
3034 "Unknown flag '%s' for type '%s'",
3035 value_str, g_type_name (type));
3039 flags |= flag_value->value;
3042 ptr = strchr (value_str, ',');
3045 /* Store last/only value */
3046 flag_value = g_flags_get_value_by_nick (flags_class, value_str);
3051 GTK_CSS_PROVIDER_ERROR,
3052 GTK_CSS_PROVIDER_ERROR_FAILED,
3053 "Unknown flag '%s' for type '%s'",
3054 value_str, g_type_name (type));
3058 flags |= flag_value->value;
3061 g_value_set_enum (value, flags);
3063 g_type_class_unref (flags_class);
3065 else if (type == GTK_TYPE_9SLICE)
3069 slice = slice_parse_str (css_provider, value_str, &end, error);
3072 g_value_take_boxed (value, slice);
3079 GTK_CSS_PROVIDER_ERROR,
3080 GTK_CSS_PROVIDER_ERROR_FAILED,
3081 "Cannot parse string '%s' for type %s",
3082 value_str, g_type_name (type));
3091 /* Set error position in the scanner
3092 * according to what we've parsed so far
3094 priv->value_pos += (end - value_str);
3097 if (error && !*error)
3098 g_set_error_literal (error,
3099 GTK_CSS_PROVIDER_ERROR,
3100 GTK_CSS_PROVIDER_ERROR_FAILED,
3101 "Failed to parse value");
3108 scanner_report_warning (GtkCssProvider *css_provider,
3109 GTokenType expected_token,
3112 GtkCssProviderPrivate *priv;
3113 const gchar *line_end, *line_start;
3114 const gchar *expected_str;
3115 gchar buf[2], *line, *str;
3118 priv = css_provider->priv;
3121 str = g_strdup (error->message);
3124 if (priv->scanner->user_data)
3125 expected_str = priv->scanner->user_data;
3128 switch (expected_token)
3130 case G_TOKEN_SYMBOL:
3131 expected_str = "Symbol";
3132 case G_TOKEN_IDENTIFIER:
3133 expected_str = "Identifier";
3135 buf[0] = expected_token;
3141 str = g_strdup_printf ("Parse error, expecting a %s '%s'",
3142 (expected_str != buf) ? "valid" : "",
3146 if (priv->value_pos)
3147 line_start = priv->value_pos - 1;
3149 line_start = priv->scanner->text - 1;
3151 while (*line_start != '\n' &&
3152 line_start != priv->buffer)
3155 if (*line_start == '\n')
3158 if (priv->value_pos)
3159 pos = priv->value_pos - line_start + 1;
3161 pos = priv->scanner->text - line_start - 1;
3163 line_end = strchr (line_start, '\n');
3166 line = g_strndup (line_start, (line_end - line_start));
3168 line = g_strdup (line_start);
3170 g_message ("CSS: %s\n"
3171 "%s, line %d, char %d:\n"
3174 str, priv->scanner->input_name,
3175 priv->scanner->line, priv->scanner->position,
3184 parse_rule (GtkCssProvider *css_provider,
3188 GtkCssProviderPrivate *priv;
3189 GTokenType expected_token;
3190 SelectorPath *selector;
3192 priv = css_provider->priv;
3194 css_provider_push_scope (css_provider, SCOPE_SELECTOR);
3196 /* Handle directives */
3197 if (scanner->token == G_TOKEN_IDENTIFIER &&
3198 scanner->value.v_identifier[0] == '@')
3202 directive = &scanner->value.v_identifier[1];
3204 if (strcmp (directive, "define-color") == 0)
3206 GtkSymbolicColor *color;
3207 gchar *color_name, *color_str;
3209 /* Directive is a color mapping */
3210 g_scanner_get_next_token (scanner);
3212 if (scanner->token != G_TOKEN_IDENTIFIER)
3214 scanner->user_data = "Color name";
3215 return G_TOKEN_IDENTIFIER;
3218 color_name = g_strdup (scanner->value.v_identifier);
3219 css_provider_push_scope (css_provider, SCOPE_VALUE);
3220 g_scanner_get_next_token (scanner);
3222 if (scanner->token != G_TOKEN_IDENTIFIER)
3224 scanner->user_data = "Color definition";
3225 return G_TOKEN_IDENTIFIER;
3228 color_str = g_strstrip (scanner->value.v_identifier);
3229 color = symbolic_color_parse (color_str, error);
3233 scanner->user_data = "Color definition";
3234 return G_TOKEN_IDENTIFIER;
3237 g_hash_table_insert (priv->symbolic_colors, color_name, color);
3239 css_provider_pop_scope (css_provider);
3240 g_scanner_get_next_token (scanner);
3242 if (scanner->token != ';')
3245 return G_TOKEN_NONE;
3247 else if (strcmp (directive, "import") == 0)
3249 GScanner *scanner_backup;
3250 GSList *state_backup;
3254 css_provider_push_scope (css_provider, SCOPE_VALUE);
3255 g_scanner_get_next_token (scanner);
3257 if (scanner->token == G_TOKEN_IDENTIFIER &&
3258 g_str_has_prefix (scanner->value.v_identifier, "url"))
3259 path = path_parse (css_provider,
3260 g_strstrip (scanner->value.v_identifier),
3262 else if (scanner->token == G_TOKEN_STRING)
3263 path = path_parse (css_provider,
3264 g_strstrip (scanner->value.v_string),
3269 scanner->user_data = "File URL";
3270 return G_TOKEN_IDENTIFIER;
3273 css_provider_pop_scope (css_provider);
3274 g_scanner_get_next_token (scanner);
3276 if (scanner->token != ';')
3282 /* Snapshot current parser state and scanner in order to restore after importing */
3283 state_backup = priv->state;
3284 scanner_backup = priv->scanner;
3287 priv->scanner = create_scanner ();
3289 /* FIXME: Avoid recursive importing */
3290 loaded = gtk_css_provider_load_from_path_internal (css_provider, path,
3293 /* Restore previous state */
3294 css_provider_reset_parser (css_provider);
3295 priv->state = state_backup;
3296 g_scanner_destroy (priv->scanner);
3297 priv->scanner = scanner_backup;
3303 scanner->user_data = "File URL";
3304 return G_TOKEN_IDENTIFIER;
3307 return G_TOKEN_NONE;
3309 else if (strcmp (directive, "binding-set") == 0)
3311 GtkBindingSet *binding_set;
3312 gchar *binding_set_name;
3314 g_scanner_get_next_token (scanner);
3316 if (scanner->token != G_TOKEN_IDENTIFIER)
3318 scanner->user_data = "Binding name";
3319 return G_TOKEN_IDENTIFIER;
3322 binding_set_name = scanner->value.v_identifier;
3323 binding_set = gtk_binding_set_find (binding_set_name);
3327 binding_set = gtk_binding_set_new (binding_set_name);
3328 binding_set->parsed = TRUE;
3331 g_scanner_get_next_token (scanner);
3333 if (scanner->token != G_TOKEN_LEFT_CURLY)
3334 return G_TOKEN_LEFT_CURLY;
3336 css_provider_push_scope (css_provider, SCOPE_BINDING_SET);
3337 g_scanner_get_next_token (scanner);
3343 if (scanner->token != G_TOKEN_IDENTIFIER)
3345 scanner->user_data = "Binding definition";
3346 return G_TOKEN_IDENTIFIER;
3349 ret = gtk_binding_entry_add_signal_from_string (binding_set,
3350 scanner->value.v_identifier);
3351 if (ret != G_TOKEN_NONE)
3353 scanner->user_data = "Binding definition";
3357 g_scanner_get_next_token (scanner);
3359 if (scanner->token != ';')
3362 g_scanner_get_next_token (scanner);
3364 while (scanner->token != G_TOKEN_RIGHT_CURLY);
3366 css_provider_pop_scope (css_provider);
3367 g_scanner_get_next_token (scanner);
3369 return G_TOKEN_NONE;
3373 scanner->user_data = "Directive";
3374 return G_TOKEN_IDENTIFIER;
3378 expected_token = parse_selector (css_provider, scanner, &selector);
3380 if (expected_token != G_TOKEN_NONE)
3382 selector_path_unref (selector);
3383 scanner->user_data = "Selector";
3384 return expected_token;
3387 priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
3389 while (scanner->token == ',')
3391 g_scanner_get_next_token (scanner);
3393 expected_token = parse_selector (css_provider, scanner, &selector);
3395 if (expected_token != G_TOKEN_NONE)
3397 selector_path_unref (selector);
3398 scanner->user_data = "Selector";
3399 return expected_token;
3402 priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
3405 css_provider_pop_scope (css_provider);
3407 if (scanner->token != G_TOKEN_LEFT_CURLY)
3408 return G_TOKEN_LEFT_CURLY;
3410 /* Declarations parsing */
3411 css_provider_push_scope (css_provider, SCOPE_DECLARATION);
3412 g_scanner_get_next_token (scanner);
3414 while (scanner->token == G_TOKEN_IDENTIFIER)
3416 gchar *value_str = NULL;
3417 GtkStylePropertyParser parse_func = NULL;
3421 prop = g_strdup (scanner->value.v_identifier);
3422 g_scanner_get_next_token (scanner);
3424 if (scanner->token != ':')
3430 priv->value_pos = priv->scanner->text;
3432 css_provider_push_scope (css_provider, SCOPE_VALUE);
3433 g_scanner_get_next_token (scanner);
3435 if (scanner->token != G_TOKEN_IDENTIFIER)
3438 scanner->user_data = "Property value";
3439 return G_TOKEN_IDENTIFIER;
3442 value_str = scanner->value.v_identifier;
3443 SKIP_SPACES (value_str);
3444 g_strchomp (value_str);
3446 if (gtk_style_properties_lookup_property (prop, &parse_func, &pspec))
3450 val = g_slice_new0 (GValue);
3451 g_value_init (val, pspec->value_type);
3453 if (strcmp (value_str, "none") == 0)
3455 /* Insert the default value, so it has an opportunity
3456 * to override other style providers when merged
3458 g_param_value_set_default (pspec, val);
3459 g_hash_table_insert (priv->cur_properties, prop, val);
3461 else if (strcmp (prop, "gtk-key-bindings") == 0)
3463 /* Private property holding the binding sets */
3464 resolve_binding_sets (value_str, val);
3465 g_hash_table_insert (priv->cur_properties, prop, val);
3467 else if (pspec->value_type == G_TYPE_STRING)
3469 g_value_set_string (val, value_str);
3470 g_hash_table_insert (priv->cur_properties, prop, val);
3472 else if ((parse_func && (parse_func) (value_str, val, error)) ||
3473 (!parse_func && css_provider_parse_value (css_provider, value_str, val, error)))
3474 g_hash_table_insert (priv->cur_properties, prop, val);
3477 g_value_unset (val);
3478 g_slice_free (GValue, val);
3481 scanner->user_data = "Property value";
3482 return G_TOKEN_IDENTIFIER;
3485 else if (prop[0] == '-')
3489 val = g_slice_new0 (GValue);
3490 g_value_init (val, G_TYPE_STRING);
3491 g_value_set_string (val, value_str);
3493 g_hash_table_insert (priv->cur_properties, prop, val);
3498 css_provider_pop_scope (css_provider);
3499 g_scanner_get_next_token (scanner);
3501 if (scanner->token != ';')
3504 g_scanner_get_next_token (scanner);
3507 if (scanner->token != G_TOKEN_RIGHT_CURLY)
3508 return G_TOKEN_RIGHT_CURLY;
3510 css_provider_pop_scope (css_provider);
3512 return G_TOKEN_NONE;
3516 parse_stylesheet (GtkCssProvider *css_provider,
3519 GtkCssProviderPrivate *priv;
3524 priv = css_provider->priv;
3525 g_scanner_get_next_token (priv->scanner);
3527 while (!g_scanner_eof (priv->scanner))
3529 GTokenType expected_token;
3532 css_provider_reset_parser (css_provider);
3533 expected_token = parse_rule (css_provider, priv->scanner, &err);
3535 if (expected_token != G_TOKEN_NONE)
3537 /* If a GError was passed in, propagate the error and bail out,
3538 * else report a warning and keep going
3544 g_propagate_error (error, err);
3546 g_set_error_literal (error,
3547 GTK_CSS_PROVIDER_ERROR,
3548 GTK_CSS_PROVIDER_ERROR_FAILED,
3549 "Error parsing stylesheet");
3554 scanner_report_warning (css_provider, expected_token, err);
3555 g_clear_error (&err);
3558 css_provider_reset_parser (css_provider);
3560 while (!g_scanner_eof (priv->scanner) &&
3561 priv->scanner->token != G_TOKEN_RIGHT_CURLY)
3562 g_scanner_get_next_token (priv->scanner);
3565 css_provider_commit (css_provider);
3567 g_scanner_get_next_token (priv->scanner);
3574 * gtk_css_provider_load_from_data:
3575 * @css_provider: a #GtkCssProvider
3576 * @data: CSS data loaded in memory
3577 * @length: the length of @data in bytes, or -1 for NUL terminated strings
3578 * @error: (out) (allow-none): return location for a #GError, or %NULL
3580 * Loads @data into @css_provider, making it clear any previously loaded
3583 * Returns: %TRUE if the data could be loaded.
3586 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
3591 GtkCssProviderPrivate *priv;
3593 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
3594 g_return_val_if_fail (data != NULL, FALSE);
3596 priv = css_provider->priv;
3599 length = strlen (data);
3601 if (priv->selectors_info->len > 0)
3602 g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
3604 priv->scanner->input_name = "-";
3605 priv->buffer = data;
3606 g_scanner_input_text (priv->scanner, data, (guint) length);
3608 g_free (priv->filename);
3609 priv->filename = NULL;
3610 priv->buffer = NULL;
3612 return parse_stylesheet (css_provider, error);
3616 * gtk_css_provider_load_from_file:
3617 * @css_provider: a #GtkCssProvider
3618 * @file: #GFile pointing to a file to load
3619 * @error: (out) (allow-none): return location for a #GError, or %NULL
3621 * Loads the data contained in @file into @css_provider, making it
3622 * clear any previously loaded information.
3624 * Returns: %TRUE if the data could be loaded.
3627 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
3631 GtkCssProviderPrivate *priv;
3632 GError *internal_error = NULL;
3637 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
3638 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3640 priv = css_provider->priv;
3642 if (!g_file_load_contents (file, NULL,
3644 NULL, &internal_error))
3646 g_propagate_error (error, internal_error);
3650 if (priv->selectors_info->len > 0)
3651 g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
3653 g_free (priv->filename);
3654 priv->filename = g_file_get_path (file);
3656 priv->scanner->input_name = priv->filename;
3657 priv->buffer = data;
3658 g_scanner_input_text (priv->scanner, data, (guint) length);
3660 ret = parse_stylesheet (css_provider, error);
3662 priv->buffer = NULL;
3669 gtk_css_provider_load_from_path_internal (GtkCssProvider *css_provider,
3674 GtkCssProviderPrivate *priv;
3675 GError *internal_error = NULL;
3676 GMappedFile *mapped_file;
3681 priv = css_provider->priv;
3683 mapped_file = g_mapped_file_new (path, FALSE, &internal_error);
3687 g_propagate_error (error, internal_error);
3691 length = g_mapped_file_get_length (mapped_file);
3692 data = g_mapped_file_get_contents (mapped_file);
3699 if (priv->selectors_info->len > 0)
3700 g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
3702 g_free (priv->filename);
3703 priv->filename = g_strdup (path);
3706 priv->scanner->input_name = priv->filename;
3707 priv->buffer = data;
3708 g_scanner_input_text (priv->scanner, data, (guint) length);
3710 ret = parse_stylesheet (css_provider, error);
3712 priv->buffer = NULL;
3713 g_mapped_file_unref (mapped_file);
3719 * gtk_css_provider_load_from_path:
3720 * @css_provider: a #GtkCssProvider
3721 * @path: the path of a filename to load, in the GLib filename encoding
3722 * @error: (out) (allow-none): return location for a #GError, or %NULL
3724 * Loads the data contained in @path into @css_provider, making it clear
3725 * any previously loaded information.
3727 * Returns: %TRUE if the data could be loaded.
3730 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
3734 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
3735 g_return_val_if_fail (path != NULL, FALSE);
3737 return gtk_css_provider_load_from_path_internal (css_provider, path,
3742 * gtk_css_provider_get_default:
3744 * Returns the provider containing the style settings used as a
3745 * fallback for all widgets.
3747 * Returns: (transfer none): The provider used for fallback styling.
3748 * This memory is owned by GTK+, and you must not free it.
3751 gtk_css_provider_get_default (void)
3753 static GtkCssProvider *provider;
3755 if (G_UNLIKELY (!provider))
3758 "@define-color fg_color #000; \n"
3759 "@define-color bg_color #dcdad5; \n"
3760 "@define-color text_color #000; \n"
3761 "@define-color base_color #fff; \n"
3762 "@define-color selected_bg_color #4b6983; \n"
3763 "@define-color selected_fg_color #fff; \n"
3764 "@define-color tooltip_bg_color #eee1b3; \n"
3765 "@define-color tooltip_fg_color #000; \n"
3766 "@define-color placeholder_text_color #808080; \n"
3768 "@define-color info_fg_color rgb (181, 171, 156);\n"
3769 "@define-color info_bg_color rgb (252, 252, 189);\n"
3770 "@define-color warning_fg_color rgb (173, 120, 41);\n"
3771 "@define-color warning_bg_color rgb (250, 173, 61);\n"
3772 "@define-color question_fg_color rgb (97, 122, 214);\n"
3773 "@define-color question_bg_color rgb (138, 173, 212);\n"
3774 "@define-color error_fg_color rgb (166, 38, 38);\n"
3775 "@define-color error_bg_color rgb (237, 54, 54);\n"
3778 " background-color: @bg_color;\n"
3779 " color: @fg_color;\n"
3780 " border-color: shade (@bg_color, 0.6);\n"
3782 " border-width: 0;\n"
3786 " background-color: shade (@bg_color, 1.05);\n"
3787 " color: shade (@fg_color, 1.3);\n"
3791 " background-color: @selected_bg_color;\n"
3792 " color: @selected_fg_color;\n"
3795 ".expander, GtkTreeView.view.expander {\n"
3799 ".expander:prelight,\n"
3800 "GtkTreeView.view.expander:selected:prelight {\n"
3801 " color: @text_color;\n"
3804 ".expander:active {\n"
3805 " transition: 200ms linear;\n"
3809 " border-color: shade (@bg_color, 0.7);\n"
3810 " background-color: shade (@bg_color, 0.9);\n"
3811 " color: shade (@bg_color, 0.7);\n"
3815 " border-width: 0;\n"
3816 " border-radius: 0;\n"
3817 " background-color: @base_color;\n"
3818 " color: @text_color;\n"
3820 ".view:selected {\n"
3821 " background-color: shade (@bg_color, 0.9);\n"
3822 " color: @fg_color;\n"
3825 ".view:selected:focused {\n"
3826 " background-color: @selected_bg_color;\n"
3827 " color: @selected_fg_color;\n"
3830 ".view column:sorted row,\n"
3831 ".view column:sorted row:prelight {\n"
3832 " background-color: shade (@bg_color, 0.85);\n"
3835 ".view column:sorted row:nth-child(odd),\n"
3836 ".view column:sorted row:nth-child(odd):prelight {\n"
3837 " background-color: shade (@bg_color, 0.8);\n"
3841 ".view row:prelight {\n"
3842 " background-color: @base_color;\n"
3843 " color: @text_color;\n"
3846 ".view row:nth-child(odd),\n"
3847 ".view row:nth-child(odd):prelight {\n"
3848 " background-color: shade (@base_color, 0.93); \n"
3851 ".view row:selected:focused {\n"
3852 " background-color: @selected_bg_color;\n"
3855 ".view row:selected {\n"
3856 " background-color: darker (@bg_color);\n"
3857 " color: @selected_fg_color;\n"
3860 ".view.cell.trough,\n"
3861 ".view.cell.trough:hover,\n"
3862 ".view.cell.trough:selected,\n"
3863 ".view.cell.trough:selected:focused {\n"
3864 " background-color: @bg_color;\n"
3865 " color: @fg_color;\n"
3868 ".view.cell.progressbar,\n"
3869 ".view.cell.progressbar:hover,\n"
3870 ".view.cell.progressbar:selected,\n"
3871 ".view.cell.progressbar:selected:focused {\n"
3872 " background-color: @selected_bg_color;\n"
3873 " color: @selected_fg_color;\n"
3877 " background-color: alpha (@fg_color, 0.25);\n"
3878 " border-color: @fg_color;\n"
3879 " border-style: solid;\n"
3880 " border-width: 1;\n"
3884 " background-color: @tooltip_bg_color; \n"
3885 " color: @tooltip_fg_color; \n"
3886 " border-color: @tooltip_fg_color; \n"
3887 " border-width: 1;\n"
3888 " border-style: solid;\n"
3893 " border-style: outset; \n"
3894 " border-width: 2; \n"
3897 ".button:active {\n"
3898 " background-color: shade (@bg_color, 0.7);\n"
3899 " border-style: inset; \n"
3902 ".button:prelight,\n"
3903 ".slider:prelight {\n"
3904 " background-color: @selected_bg_color;\n"
3905 " color: @selected_fg_color;\n"
3906 " border-color: shade (@selected_bg_color, 0.7);\n"
3910 " background-color: darker (@bg_color);\n"
3911 " border-style: inset;\n"
3912 " border-width: 1;\n"
3917 " border-style: inset;\n"
3918 " border-width: 2;\n"
3919 " background-color: @base_color;\n"
3920 " color: @text_color;\n"
3923 ".entry:insensitive {\n"
3924 " background-color: shade (@base_color, 0.9);\n"
3925 " color: shade (@base_color, 0.7);\n"
3928 " background-color: #c4c2bd;\n"
3933 ".entry.progressbar, \n"
3934 ".cell.progressbar {\n"
3935 " background-color: @selected_bg_color;\n"
3936 " border-color: shade (@selected_bg_color, 0.7);\n"
3937 " color: @selected_fg_color;\n"
3938 " border-style: outset;\n"
3939 " border-width: 1;\n"
3942 "GtkCheckButton:hover,\n"
3943 "GtkCheckButton:selected,\n"
3944 "GtkRadioButton:hover,\n"
3945 "GtkRadioButton:selected {\n"
3946 " background-color: shade (@bg_color, 1.05);\n"
3950 ".cell.check, .cell.radio,\n"
3951 ".cell.check:hover, .cell.radio:hover {\n"
3952 " border-style: solid;\n"
3953 " border-width: 1;\n"
3954 " background-color: @base_color;\n"
3955 " border-color: @fg_color;\n"
3958 ".check:active, .radio:active,\n"
3959 ".check:hover, .radio:hover {\n"
3960 " background-color: @base_color;\n"
3961 " border-color: @fg_color;\n"
3962 " color: @text_color;\n"
3965 ".check:selected, .radio:selected {\n"
3966 " background-color: darker (@bg_color);\n"
3967 " color: @selected_fg_color;\n"
3968 " border-color: @selected_fg_color;\n"
3971 ".check:selected:focused, .radio:selected:focused {\n"
3972 " background-color: @selected_bg_color;\n"
3975 ".menu.check, .menu.radio {\n"
3976 " color: @fg_color;\n"
3977 " border-style: none;\n"
3978 " border-width: 0;\n"
3982 " border-style: outset;\n"
3983 " border-width: 1;\n"
3987 " border-style: inset;\n"
3988 " border-width: 2;\n"
3992 " border-style: outset;\n"
3993 " border-width: 1;\n"
3997 " border-style: inset;\n"
3998 " border-width: 1;\n"
4001 "GtkScrolledWindow.frame {\n"
4008 " border-style: outset;\n"
4009 " border-width: 1;\n"
4014 ".menu.check:hover,\n"
4015 ".menu.radio:hover {\n"
4016 " background-color: @selected_bg_color;\n"
4017 " color: @selected_fg_color;\n"
4020 "GtkSpinButton.button {\n"
4021 " border-width: 1;\n"
4024 ".scale.slider:hover,\n"
4025 "GtkSpinButton.button:hover {\n"
4026 " background-color: shade (@bg_color, 1.05);\n"
4027 " border-color: shade (@bg_color, 0.8);\n"
4030 "GtkSwitch.trough:active {\n"
4031 " background-color: @selected_bg_color;\n"
4032 " color: @selected_fg_color;\n"
4035 "GtkToggleButton.button:inconsistent {\n"
4036 " border-style: outset;\n"
4037 " border-width: 1px;\n"
4038 " background-color: shade (@bg_color, 0.9);\n"
4039 " border-color: shade (@bg_color, 0.7);\n"
4042 "GtkLabel:selected {\n"
4043 " background-color: shade (@bg_color, 0.9);\n"
4046 "GtkLabel:selected:focused {\n"
4047 " background-color: @selected_bg_color;\n"
4050 ".spinner:active {\n"
4051 " transition: 750ms linear loop;\n"
4055 " background-color: @info_bg_color;\n"
4056 " color: @info_fg_color;\n"
4060 " background-color: @warning_bg_color;\n"
4061 " color: @warning_fg_color;\n"
4065 " background-color: @question_bg_color;\n"
4066 " color: @question_fg_color;\n"
4070 " background-color: @error_bg_color;\n"
4071 " color: @error_fg_color;\n"
4075 " background-color: @selected_bg_color;\n"
4076 " color: @selected_fg_color;\n"
4079 ".light-area-focus {\n"
4083 ".dark-area-focus {\n"
4086 "GtkCalendar.view {\n"
4087 " border-width: 1;\n"
4088 " border-style: inset;\n"
4092 "GtkCalendar.view:inconsistent {\n"
4093 " color: darker (@bg_color);\n"
4096 "GtkCalendar.header {\n"
4097 " background-color: @bg_color;\n"
4098 " border-style: outset;\n"
4099 " border-width: 2;\n"
4102 "GtkCalendar.highlight {\n"
4103 " border-width: 0;\n"
4106 "GtkCalendar.button {\n"
4107 " background-color: @bg_color;\n"
4110 "GtkCalendar.button:hover {\n"
4111 " background-color: lighter (@bg_color);\n"
4112 " color: @fg_color;\n"
4116 " border-width: 1;\n"
4121 " border-width: 0;\n"
4126 provider = gtk_css_provider_new ();
4127 if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
4129 g_error ("Failed to load the internal default CSS.");
4137 _gtk_css_provider_get_theme_dir (void)
4142 var = g_getenv ("GTK_DATA_PREFIX");
4145 path = g_build_filename (var, "share", "themes", NULL);
4147 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
4153 * gtk_css_provider_get_named:
4154 * @name: A theme name
4155 * @variant: (allow-none): variant to load, for example, "dark", or
4156 * %NULL for the default
4158 * Loads a theme from the usual theme paths
4160 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
4161 * This memory is owned by GTK+, and you must not free it.
4164 gtk_css_provider_get_named (const gchar *name,
4165 const gchar *variant)
4167 static GHashTable *themes = NULL;
4168 GtkCssProvider *provider;
4171 if (G_UNLIKELY (!themes))
4172 themes = g_hash_table_new (g_str_hash, g_str_equal);
4174 if (variant == NULL)
4175 key = (gchar *)name;
4177 key = g_strconcat (name, "-", variant, NULL);
4179 provider = g_hash_table_lookup (themes, key);
4183 const gchar *home_dir;
4184 gchar *subpath, *path = NULL;
4187 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
4189 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
4191 /* First look in the users home directory
4193 home_dir = g_get_home_dir ();
4196 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
4198 if (!g_file_test (path, G_FILE_TEST_EXISTS))
4209 theme_dir = _gtk_css_provider_get_theme_dir ();
4210 path = g_build_filename (theme_dir, name, subpath, NULL);
4213 if (!g_file_test (path, G_FILE_TEST_EXISTS))
4226 provider = gtk_css_provider_new ();
4228 if (!gtk_css_provider_load_from_path (provider, path, &error))
4230 g_warning ("Could not load named theme \"%s\": %s", name, error->message);
4231 g_error_free (error);
4233 g_object_unref (provider);
4237 g_hash_table_insert (themes, g_strdup (key), provider);