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 GtkCssProviderPrivate GtkCssProviderPrivate;
736 typedef struct SelectorElement SelectorElement;
737 typedef struct SelectorPath SelectorPath;
738 typedef struct SelectorStyleInfo SelectorStyleInfo;
739 typedef enum SelectorElementType SelectorElementType;
740 typedef enum CombinatorType CombinatorType;
741 typedef enum ParserScope ParserScope;
742 typedef enum ParserSymbol ParserSymbol;
744 enum SelectorElementType {
753 enum CombinatorType {
754 COMBINATOR_DESCENDANT, /* No direct relation needed */
755 COMBINATOR_CHILD /* Direct child */
758 struct SelectorElement
760 SelectorElementType elem_type;
761 CombinatorType combinator;
771 GtkRegionFlags flags;
783 struct SelectorStyleInfo
789 struct GtkCssProviderPrivate
795 const gchar *value_pos;
797 GHashTable *symbolic_colors;
799 GPtrArray *selectors_info;
801 /* Current parser state */
803 GSList *cur_selectors;
804 GHashTable *cur_properties;
816 /* Extend GtkStateType, since these
817 * values are also used as symbols
820 /* Scope: pseudo-class */
821 SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
826 /* Scope: nth-child */
827 SYMBOL_NTH_CHILD_EVEN,
828 SYMBOL_NTH_CHILD_ODD,
829 SYMBOL_NTH_CHILD_FIRST,
830 SYMBOL_NTH_CHILD_LAST
833 static void gtk_css_provider_finalize (GObject *object);
834 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
836 static void scanner_apply_scope (GScanner *scanner,
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 GType parent = type;
1144 while ((parent = g_type_parent (parent)) != G_TYPE_INVALID)
1146 if (parent == elem->type)
1153 g_warning ("Hierarchy is higher than expected.");
1161 else if (elem->elem_type == SELECTOR_REGION)
1163 GtkRegionFlags flags;
1165 if (!gtk_widget_path_iter_has_qregion (path, index,
1170 if (elem->region.flags != 0 &&
1171 (flags & elem->region.flags) == 0)
1177 else if (elem->elem_type == SELECTOR_GLOB)
1179 /* Treat as lowest matching type */
1183 else if (elem->elem_type == SELECTOR_NAME)
1185 if (!gtk_widget_path_iter_has_qname (path, index, elem->name))
1191 else if (elem->elem_type == SELECTOR_CLASS)
1193 if (!gtk_widget_path_iter_has_qclass (path, index, elem->name))
1204 compare_selector (GtkWidgetPath *path,
1205 SelectorPath *selector)
1207 GSList *elements = selector->elements;
1208 gboolean match = TRUE, first = TRUE, first_match = FALSE;
1212 i = gtk_widget_path_length (path) - 1;
1214 while (elements && match && i >= 0)
1216 SelectorElement *elem;
1219 elem = elements->data;
1221 match = compare_selector_element (path, i, elem, &elem_score);
1226 /* Only move on to the next index if there is no match
1227 * with the current element (whether to continue or not
1228 * handled right after in the combinator check), or a
1229 * GType or glob has just been matched.
1231 * Region and widget names do not trigger this because
1232 * the next element in the selector path could also be
1233 * related to the same index.
1236 (elem->elem_type == SELECTOR_GTYPE ||
1237 elem->elem_type == SELECTOR_GLOB))
1241 elem->elem_type != SELECTOR_TYPE_NAME &&
1242 elem->combinator == COMBINATOR_DESCENDANT)
1244 /* With descendant combinators there may
1245 * be intermediate chidren in the hierarchy
1250 elements = elements->next;
1254 /* Only 4 bits are actually used */
1256 score |= elem_score;
1262 /* If there are pending selector
1263 * elements to compare, it's not
1271 else if (first_match)
1273 /* Assign more weight to these selectors
1274 * that matched right from the first element.
1282 typedef struct StylePriorityInfo StylePriorityInfo;
1284 struct StylePriorityInfo
1288 GtkStateFlags state;
1292 css_provider_get_selectors (GtkCssProvider *css_provider,
1293 GtkWidgetPath *path)
1295 GtkCssProviderPrivate *priv;
1296 GArray *priority_info;
1299 priv = css_provider->priv;
1300 priority_info = g_array_new (FALSE, FALSE, sizeof (StylePriorityInfo));
1302 for (i = 0; i < priv->selectors_info->len; i++)
1304 SelectorStyleInfo *info;
1305 StylePriorityInfo new;
1306 gboolean added = FALSE;
1309 info = g_ptr_array_index (priv->selectors_info, i);
1310 score = compare_selector (path, info->path);
1316 new.style = info->style;
1317 new.state = info->path->state;
1319 for (j = 0; j < priority_info->len; j++)
1321 StylePriorityInfo *cur;
1323 cur = &g_array_index (priority_info, StylePriorityInfo, j);
1325 if (cur->score > new.score)
1327 g_array_insert_val (priority_info, j, new);
1334 g_array_append_val (priority_info, new);
1337 return priority_info;
1341 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1342 GtkStyleProperties *props)
1344 GtkCssProviderPrivate *priv;
1345 GHashTableIter iter;
1346 gpointer key, value;
1348 priv = css_provider->priv;
1349 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1351 while (g_hash_table_iter_next (&iter, &key, &value))
1354 GtkSymbolicColor *color;
1359 gtk_style_properties_map_color (props, name, color);
1363 static GtkStyleProperties *
1364 gtk_css_provider_get_style (GtkStyleProvider *provider,
1365 GtkWidgetPath *path)
1367 GtkCssProvider *css_provider;
1368 GtkStyleProperties *props;
1369 GArray *priority_info;
1372 css_provider = GTK_CSS_PROVIDER (provider);
1373 props = gtk_style_properties_new ();
1375 css_provider_dump_symbolic_colors (css_provider, props);
1376 priority_info = css_provider_get_selectors (css_provider, path);
1378 for (i = 0; i < priority_info->len; i++)
1380 StylePriorityInfo *info;
1381 GHashTableIter iter;
1382 gpointer key, value;
1384 info = &g_array_index (priority_info, StylePriorityInfo, i);
1385 g_hash_table_iter_init (&iter, info->style);
1387 while (g_hash_table_iter_next (&iter, &key, &value))
1391 /* Properties starting with '-' may be both widget style properties
1392 * or custom properties from the theming engine, so check whether
1393 * the type is registered or not.
1395 if (prop[0] == '-' &&
1396 !gtk_style_properties_lookup_property (prop, NULL, NULL))
1399 gtk_style_properties_set_property (props, key, info->state, value);
1403 g_array_free (priority_info, TRUE);
1409 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1410 GtkWidgetPath *path,
1411 GtkStateFlags state,
1415 GArray *priority_info;
1416 gboolean found = FALSE;
1420 prop_name = g_strdup_printf ("-%s-%s",
1421 g_type_name (pspec->owner_type),
1424 priority_info = css_provider_get_selectors (GTK_CSS_PROVIDER (provider), path);
1426 for (i = priority_info->len - 1; i >= 0; i--)
1428 StylePriorityInfo *info;
1431 info = &g_array_index (priority_info, StylePriorityInfo, i);
1432 val = g_hash_table_lookup (info->style, prop_name);
1435 (info->state == 0 ||
1436 info->state == state ||
1437 ((info->state & state) != 0 &&
1438 (info->state & ~(state)) == 0)))
1440 const gchar *val_str;
1442 val_str = g_value_get_string (val);
1445 css_provider_parse_value (GTK_CSS_PROVIDER (provider), val_str, value, NULL);
1450 g_array_free (priority_info, TRUE);
1457 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1459 iface->get_style = gtk_css_provider_get_style;
1460 iface->get_style_property = gtk_css_provider_get_style_property;
1464 gtk_css_provider_finalize (GObject *object)
1466 GtkCssProvider *css_provider;
1467 GtkCssProviderPrivate *priv;
1469 css_provider = GTK_CSS_PROVIDER (object);
1470 priv = css_provider->priv;
1472 g_scanner_destroy (priv->scanner);
1473 g_free (priv->filename);
1475 g_ptr_array_free (priv->selectors_info, TRUE);
1477 g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
1478 g_slist_free (priv->cur_selectors);
1480 if (priv->cur_properties)
1481 g_hash_table_unref (priv->cur_properties);
1482 if (priv->symbolic_colors)
1483 g_hash_table_destroy (priv->symbolic_colors);
1485 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1489 * gtk_css_provider_new:
1491 * Returns a newly created #GtkCssProvider.
1493 * Returns: A new #GtkCssProvider
1496 gtk_css_provider_new (void)
1498 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1502 property_value_free (GValue *value)
1504 if (G_IS_VALUE (value))
1505 g_value_unset (value);
1507 g_slice_free (GValue, value);
1511 scanner_apply_scope (GScanner *scanner,
1514 g_scanner_set_scope (scanner, scope);
1516 if (scope == SCOPE_VALUE)
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_BINDING_SET)
1524 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
1525 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(){}<>,.%\t\n'/\"";
1526 scanner->config->scan_identifier_1char = TRUE;
1528 else if (scope == SCOPE_SELECTOR)
1530 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "*@";
1531 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_#.";
1532 scanner->config->scan_identifier_1char = TRUE;
1534 else if (scope == SCOPE_PSEUDO_CLASS ||
1535 scope == SCOPE_NTH_CHILD ||
1536 scope == SCOPE_DECLARATION)
1538 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "-_";
1539 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_";
1540 scanner->config->scan_identifier_1char = FALSE;
1543 g_assert_not_reached ();
1545 scanner->config->scan_float = FALSE;
1546 scanner->config->cpair_comment_single = NULL;
1550 css_provider_push_scope (GtkCssProvider *css_provider,
1553 GtkCssProviderPrivate *priv;
1555 priv = css_provider->priv;
1556 priv->state = g_slist_prepend (priv->state, GUINT_TO_POINTER (scope));
1558 scanner_apply_scope (priv->scanner, scope);
1562 css_provider_pop_scope (GtkCssProvider *css_provider)
1564 GtkCssProviderPrivate *priv;
1565 ParserScope scope = SCOPE_SELECTOR;
1567 priv = css_provider->priv;
1571 g_warning ("Push/pop calls to parser scope aren't paired");
1572 scanner_apply_scope (priv->scanner, SCOPE_SELECTOR);
1573 return SCOPE_SELECTOR;
1576 priv->state = g_slist_delete_link (priv->state, priv->state);
1578 /* Fetch new scope */
1580 scope = GPOINTER_TO_INT (priv->state->data);
1582 scanner_apply_scope (priv->scanner, scope);
1588 css_provider_reset_parser (GtkCssProvider *css_provider)
1590 GtkCssProviderPrivate *priv;
1592 priv = css_provider->priv;
1594 g_slist_free (priv->state);
1597 scanner_apply_scope (priv->scanner, SCOPE_SELECTOR);
1598 priv->scanner->user_data = NULL;
1599 priv->value_pos = NULL;
1601 g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
1602 g_slist_free (priv->cur_selectors);
1603 priv->cur_selectors = NULL;
1605 if (priv->cur_properties)
1606 g_hash_table_unref (priv->cur_properties);
1608 priv->cur_properties = g_hash_table_new_full (g_str_hash,
1610 (GDestroyNotify) g_free,
1611 (GDestroyNotify) property_value_free);
1615 css_provider_commit (GtkCssProvider *css_provider)
1617 GtkCssProviderPrivate *priv;
1620 priv = css_provider->priv;
1621 l = priv->cur_selectors;
1625 SelectorPath *path = l->data;
1626 SelectorStyleInfo *info;
1628 info = selector_style_info_new (path);
1629 selector_style_info_set_style (info, priv->cur_properties);
1631 g_ptr_array_add (priv->selectors_info, info);
1637 parse_nth_child (GtkCssProvider *css_provider,
1639 GtkRegionFlags *flags)
1641 ParserSymbol symbol;
1643 g_scanner_get_next_token (scanner);
1645 if (scanner->token != G_TOKEN_SYMBOL)
1646 return G_TOKEN_SYMBOL;
1648 symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
1650 if (symbol == SYMBOL_NTH_CHILD)
1652 g_scanner_get_next_token (scanner);
1654 if (scanner->token != G_TOKEN_LEFT_PAREN)
1655 return G_TOKEN_LEFT_PAREN;
1657 css_provider_push_scope (css_provider, SCOPE_NTH_CHILD);
1658 g_scanner_get_next_token (scanner);
1660 if (scanner->token != G_TOKEN_SYMBOL)
1661 return G_TOKEN_SYMBOL;
1663 symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
1667 case SYMBOL_NTH_CHILD_EVEN:
1668 *flags = GTK_REGION_EVEN;
1670 case SYMBOL_NTH_CHILD_ODD:
1671 *flags = GTK_REGION_ODD;
1673 case SYMBOL_NTH_CHILD_FIRST:
1674 *flags = GTK_REGION_FIRST;
1676 case SYMBOL_NTH_CHILD_LAST:
1677 *flags = GTK_REGION_LAST;
1683 g_scanner_get_next_token (scanner);
1685 if (scanner->token != G_TOKEN_RIGHT_PAREN)
1686 return G_TOKEN_RIGHT_PAREN;
1688 css_provider_pop_scope (css_provider);
1690 else if (symbol == SYMBOL_FIRST_CHILD)
1691 *flags = GTK_REGION_FIRST;
1692 else if (symbol == SYMBOL_LAST_CHILD)
1693 *flags = GTK_REGION_LAST;
1694 else if (symbol == SYMBOL_SORTED_CHILD)
1695 *flags = GTK_REGION_SORTED;
1699 return G_TOKEN_SYMBOL;
1702 return G_TOKEN_NONE;
1706 parse_pseudo_class (GtkCssProvider *css_provider,
1708 SelectorPath *selector)
1712 g_scanner_get_next_token (scanner);
1714 if (scanner->token != G_TOKEN_SYMBOL)
1715 return G_TOKEN_SYMBOL;
1717 state = GPOINTER_TO_INT (scanner->value.v_symbol);
1721 case GTK_STATE_ACTIVE:
1722 selector->state |= GTK_STATE_FLAG_ACTIVE;
1724 case GTK_STATE_PRELIGHT:
1725 selector->state |= GTK_STATE_FLAG_PRELIGHT;
1727 case GTK_STATE_SELECTED:
1728 selector->state |= GTK_STATE_FLAG_SELECTED;
1730 case GTK_STATE_INSENSITIVE:
1731 selector->state |= GTK_STATE_FLAG_INSENSITIVE;
1733 case GTK_STATE_INCONSISTENT:
1734 selector->state |= GTK_STATE_FLAG_INCONSISTENT;
1736 case GTK_STATE_FOCUSED:
1737 selector->state |= GTK_STATE_FLAG_FOCUSED;
1740 return G_TOKEN_SYMBOL;
1743 return G_TOKEN_NONE;
1746 /* Parses a number of concatenated classes */
1748 parse_classes (SelectorPath *path,
1753 if ((pos = strchr (str, '.')) != NULL)
1755 /* Leave the last class to the call after the loop */
1759 selector_path_prepend_class (path, str);
1762 pos = strchr (str, '.');
1766 selector_path_prepend_class (path, str);
1770 is_widget_class_name (const gchar *str)
1772 /* Do a pretty lax check here, not all
1773 * widget class names contain only CamelCase
1774 * (gtkmm widgets don't), but at least part of
1775 * the name will be CamelCase, so check for
1776 * the first uppercase char
1780 if (g_ascii_isupper (*str))
1790 parse_selector (GtkCssProvider *css_provider,
1792 SelectorPath **selector_out)
1796 path = selector_path_new ();
1797 *selector_out = path;
1799 if (scanner->token != ':' &&
1800 scanner->token != '#' &&
1801 scanner->token != '.' &&
1802 scanner->token != G_TOKEN_IDENTIFIER)
1803 return G_TOKEN_IDENTIFIER;
1805 while (scanner->token == '#' ||
1806 scanner->token == '.' ||
1807 scanner->token == G_TOKEN_IDENTIFIER)
1809 if (scanner->token == '#' ||
1810 scanner->token == '.')
1815 is_class = (scanner->token == '.');
1817 g_scanner_get_next_token (scanner);
1819 if (scanner->token != G_TOKEN_IDENTIFIER)
1820 return G_TOKEN_IDENTIFIER;
1822 selector_path_prepend_glob (path);
1823 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
1826 parse_classes (path, scanner->value.v_identifier);
1829 if ((pos = strchr (scanner->value.v_identifier, '.')) != NULL)
1832 selector_path_prepend_name (path, scanner->value.v_identifier);
1834 /* Parse any remaining classes */
1836 parse_classes (path, pos + 1);
1839 else if (is_widget_class_name (scanner->value.v_identifier))
1843 if ((pos = strchr (scanner->value.v_identifier, '#')) != NULL ||
1844 (pos = strchr (scanner->value.v_identifier, '.')) != NULL)
1846 gchar *type_name, *name;
1849 is_class = (*pos == '.');
1851 /* Widget type and name/class put together */
1854 type_name = scanner->value.v_identifier;
1856 selector_path_prepend_type (path, type_name);
1858 /* This is only so there is a direct relationship
1859 * between widget type and its name.
1861 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
1864 parse_classes (path, name);
1867 if ((pos = strchr (name, '.')) != NULL)
1870 selector_path_prepend_name (path, name);
1872 /* Parse any remaining classes */
1874 parse_classes (path, pos + 1);
1878 selector_path_prepend_type (path, scanner->value.v_identifier);
1880 else if (_gtk_style_context_check_region_name (scanner->value.v_identifier))
1882 GtkRegionFlags flags = 0;
1885 region_name = g_strdup (scanner->value.v_identifier);
1887 if (g_scanner_peek_next_token (scanner) == ':')
1889 ParserSymbol symbol;
1891 g_scanner_get_next_token (scanner);
1892 css_provider_push_scope (css_provider, SCOPE_PSEUDO_CLASS);
1894 /* Check for the next token being nth-child, parse in that
1895 * case, and fallback into common state parsing if not.
1897 if (g_scanner_peek_next_token (scanner) != G_TOKEN_SYMBOL)
1898 return G_TOKEN_SYMBOL;
1900 symbol = GPOINTER_TO_INT (scanner->next_value.v_symbol);
1902 if (symbol == SYMBOL_FIRST_CHILD ||
1903 symbol == SYMBOL_LAST_CHILD ||
1904 symbol == SYMBOL_NTH_CHILD ||
1905 symbol == SYMBOL_SORTED_CHILD)
1909 if ((token = parse_nth_child (css_provider, scanner, &flags)) != G_TOKEN_NONE)
1912 css_provider_pop_scope (css_provider);
1916 css_provider_pop_scope (css_provider);
1917 selector_path_prepend_region (path, region_name, 0);
1918 g_free (region_name);
1923 selector_path_prepend_region (path, region_name, flags);
1924 g_free (region_name);
1926 else if (scanner->value.v_identifier[0] == '*')
1927 selector_path_prepend_glob (path);
1929 return G_TOKEN_IDENTIFIER;
1931 g_scanner_get_next_token (scanner);
1933 if (scanner->token == '>')
1935 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
1936 g_scanner_get_next_token (scanner);
1940 if (scanner->token == ':')
1942 /* Add glob selector if path is empty */
1943 if (selector_path_depth (path) == 0)
1944 selector_path_prepend_glob (path);
1946 css_provider_push_scope (css_provider, SCOPE_PSEUDO_CLASS);
1948 while (scanner->token == ':')
1952 if ((token = parse_pseudo_class (css_provider, scanner, path)) != G_TOKEN_NONE)
1955 g_scanner_get_next_token (scanner);
1958 css_provider_pop_scope (css_provider);
1961 return G_TOKEN_NONE;
1964 #define SKIP_SPACES(s) while (s[0] == ' ' || s[0] == '\t' || s[0] == '\n') s++;
1965 #define SKIP_SPACES_BACK(s) while (s[0] == ' ' || s[0] == '\t' || s[0] == '\n') s--;
1967 static GtkSymbolicColor *
1968 symbolic_color_parse_str (const gchar *string,
1971 GtkSymbolicColor *symbolic_color = NULL;
1974 str = (gchar *) string;
1985 while (*end == '-' || *end == '_' || g_ascii_isalpha (*end))
1988 name = g_strndup (str, end - str);
1989 symbolic_color = gtk_symbolic_color_new_name (name);
1992 *end_ptr = (gchar *) end;
1994 else if (g_str_has_prefix (str, "lighter") ||
1995 g_str_has_prefix (str, "darker"))
1997 GtkSymbolicColor *param_color;
1998 gboolean is_lighter = FALSE;
2000 is_lighter = g_str_has_prefix (str, "lighter");
2003 str += strlen ("lighter");
2005 str += strlen ("darker");
2011 *end_ptr = (gchar *) str;
2017 param_color = symbolic_color_parse_str (str, end_ptr);
2024 *end_ptr = (gchar *) str;
2028 gtk_symbolic_color_unref (param_color);
2033 symbolic_color = gtk_symbolic_color_new_shade (param_color, 1.3);
2035 symbolic_color = gtk_symbolic_color_new_shade (param_color, 0.7);
2037 gtk_symbolic_color_unref (param_color);
2040 else if (g_str_has_prefix (str, "shade") ||
2041 g_str_has_prefix (str, "alpha"))
2043 GtkSymbolicColor *param_color;
2044 gboolean is_shade = FALSE;
2047 is_shade = g_str_has_prefix (str, "shade");
2050 str += strlen ("shade");
2052 str += strlen ("alpha");
2058 *end_ptr = (gchar *) str;
2064 param_color = symbolic_color_parse_str (str, end_ptr);
2074 gtk_symbolic_color_unref (param_color);
2075 *end_ptr = (gchar *) str;
2081 factor = g_ascii_strtod (str, end_ptr);
2085 *end_ptr = (gchar *) str;
2089 gtk_symbolic_color_unref (param_color);
2094 symbolic_color = gtk_symbolic_color_new_shade (param_color, factor);
2096 symbolic_color = gtk_symbolic_color_new_alpha (param_color, factor);
2098 gtk_symbolic_color_unref (param_color);
2101 else if (g_str_has_prefix (str, "mix"))
2103 GtkSymbolicColor *color1, *color2;
2106 str += strlen ("mix");
2111 *end_ptr = (gchar *) str;
2117 color1 = symbolic_color_parse_str (str, end_ptr);
2127 gtk_symbolic_color_unref (color1);
2128 *end_ptr = (gchar *) str;
2134 color2 = symbolic_color_parse_str (str, end_ptr);
2136 if (!color2 || *end_ptr[0] != ',')
2138 gtk_symbolic_color_unref (color1);
2147 gtk_symbolic_color_unref (color1);
2148 gtk_symbolic_color_unref (color2);
2149 *end_ptr = (gchar *) str;
2155 factor = g_ascii_strtod (str, end_ptr);
2159 *end_ptr = (gchar *) str;
2163 gtk_symbolic_color_unref (color1);
2164 gtk_symbolic_color_unref (color2);
2168 symbolic_color = gtk_symbolic_color_new_mix (color1, color2, factor);
2169 gtk_symbolic_color_unref (color1);
2170 gtk_symbolic_color_unref (color2);
2183 /* Color in hex format */
2184 while (g_ascii_isxdigit (*end))
2187 else if (g_str_has_prefix (str, "rgb"))
2189 /* color in rgb/rgba format */
2190 while (*end != ')' && *end != '\0')
2199 while (*end != '\0' &&
2200 (g_ascii_isalnum (*end) || *end == ' '))
2204 color_str = g_strndup (str, end - str);
2205 *end_ptr = (gchar *) end;
2207 if (!gdk_rgba_parse (&color, color_str))
2213 symbolic_color = gtk_symbolic_color_new_literal (&color);
2217 return symbolic_color;
2220 static GtkSymbolicColor *
2221 symbolic_color_parse (const gchar *str,
2224 GtkSymbolicColor *color;
2227 color = symbolic_color_parse_str (str, &end);
2231 g_set_error_literal (error,
2232 GTK_CSS_PROVIDER_ERROR,
2233 GTK_CSS_PROVIDER_ERROR_FAILED,
2234 "Could not parse symbolic color");
2238 gtk_symbolic_color_unref (color);
2246 static GtkGradient *
2247 gradient_parse_str (const gchar *str,
2250 GtkGradient *gradient = NULL;
2255 if (g_str_has_prefix (str, "-gtk-gradient"))
2257 cairo_pattern_type_t type;
2259 str += strlen ("-gtk-gradient");
2264 *end_ptr = (gchar *) str;
2271 /* Parse gradient type */
2272 if (g_str_has_prefix (str, "linear"))
2274 type = CAIRO_PATTERN_TYPE_LINEAR;
2275 str += strlen ("linear");
2277 else if (g_str_has_prefix (str, "radial"))
2279 type = CAIRO_PATTERN_TYPE_RADIAL;
2280 str += strlen ("radial");
2284 *end_ptr = (gchar *) str;
2290 /* Parse start/stop position parameters */
2291 for (i = 0; i < 2; i++)
2295 *end_ptr = (gchar *) str;
2302 if (strncmp (str, "left", 4) == 0)
2305 str += strlen ("left");
2307 else if (strncmp (str, "right", 5) == 0)
2310 str += strlen ("right");
2312 else if (strncmp (str, "center", 6) == 0)
2314 coords[i * 3] = 0.5;
2315 str += strlen ("center");
2319 coords[i * 3] = g_ascii_strtod (str, &end);
2323 *end_ptr = (gchar *) str;
2332 if (strncmp (str, "top", 3) == 0)
2334 coords[(i * 3) + 1] = 0;
2335 str += strlen ("top");
2337 else if (strncmp (str, "bottom", 6) == 0)
2339 coords[(i * 3) + 1] = 1;
2340 str += strlen ("bottom");
2342 else if (strncmp (str, "center", 6) == 0)
2344 coords[(i * 3) + 1] = 0.5;
2345 str += strlen ("center");
2349 coords[(i * 3) + 1] = g_ascii_strtod (str, &end);
2353 *end_ptr = (gchar *) str;
2362 if (type == CAIRO_PATTERN_TYPE_RADIAL)
2367 *end_ptr = (gchar *) str;
2374 coords[(i * 3) + 2] = g_ascii_strtod (str, &end);
2381 if (type == CAIRO_PATTERN_TYPE_LINEAR)
2382 gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
2384 gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
2385 coords[3], coords[4], coords[5]);
2389 GtkSymbolicColor *color;
2394 *end_ptr = (gchar *) str;
2401 if (g_str_has_prefix (str, "from"))
2404 str += strlen ("from");
2409 *end_ptr = (gchar *) str;
2413 else if (g_str_has_prefix (str, "to"))
2416 str += strlen ("to");
2421 *end_ptr = (gchar *) str;
2425 else if (g_str_has_prefix (str, "color-stop"))
2427 str += strlen ("color-stop");
2432 *end_ptr = (gchar *) str;
2439 position = g_ascii_strtod (str, &end);
2446 *end_ptr = (gchar *) str;
2452 *end_ptr = (gchar *) str;
2459 color = symbolic_color_parse_str (str, &end);
2466 *end_ptr = (gchar *) str;
2475 gtk_gradient_add_color_stop (gradient, position, color);
2476 gtk_symbolic_color_unref (color);
2482 *end_ptr = (gchar *) str;
2489 *end_ptr = (gchar *) str;
2495 path_parse_str (GtkCssProvider *css_provider,
2501 const gchar *start, *end;
2504 if (g_str_has_prefix (str, "url"))
2506 str += strlen ("url");
2511 *end_ptr = (gchar *) str;
2515 chr = strchr (str, ')');
2518 *end_ptr = (gchar *) str;
2527 if (*str == '"' || *str == '\'')
2534 SKIP_SPACES_BACK (chr);
2536 if (*chr != *p || chr == p)
2538 *end_ptr = (gchar *)str;
2544 *end_ptr = (gchar *)str;
2548 path = g_strndup (str, chr - str);
2551 *end_ptr = (gchar *)end;
2555 path = g_strdup (str);
2556 *end_ptr = (gchar *)str + strlen (str);
2559 /* Always return an absolute path */
2560 if (!g_path_is_absolute (path))
2562 GtkCssProviderPrivate *priv;
2563 gchar *dirname, *full_path;
2565 priv = css_provider->priv;
2567 /* Use relative path to the current CSS file path, if any */
2569 dirname = g_path_get_dirname (priv->filename);
2571 dirname = g_get_current_dir ();
2573 full_path = g_build_filename (dirname, path, NULL);
2580 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR))
2582 g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_EXIST,
2583 "File doesn't exist: %s", path);
2586 *end_ptr = (gchar *)start;
2593 path_parse (GtkCssProvider *css_provider,
2600 path = path_parse_str (css_provider, str, &end, error);
2607 g_set_error_literal (error,
2608 GTK_CSS_PROVIDER_ERROR,
2609 GTK_CSS_PROVIDER_ERROR_FAILED,
2610 "Error parsing path");
2619 slice_parse_str (GtkCssProvider *css_provider,
2624 gdouble distance_top, distance_bottom;
2625 gdouble distance_left, distance_right;
2626 GtkSliceSideModifier mods[2];
2634 /* Parse image url */
2635 path = path_parse_str (css_provider, str, end_ptr, error);
2643 /* Parse top/left/bottom/right distances */
2644 distance_top = g_ascii_strtod (str, end_ptr);
2649 distance_right = g_ascii_strtod (str, end_ptr);
2654 distance_bottom = g_ascii_strtod (str, end_ptr);
2659 distance_left = g_ascii_strtod (str, end_ptr);
2664 while (*str && i < 2)
2666 if (g_str_has_prefix (str, "stretch"))
2668 str += strlen ("stretch");
2669 mods[i] = GTK_SLICE_STRETCH;
2671 else if (g_str_has_prefix (str, "repeat"))
2673 str += strlen ("repeat");
2674 mods[i] = GTK_SLICE_REPEAT;
2679 *end_ptr = (gchar *) str;
2687 *end_ptr = (gchar *) str;
2697 /* Fill in second modifier, same as the first */
2701 pixbuf = gdk_pixbuf_new_from_file (path, error);
2706 *end_ptr = (gchar *) str;
2710 slice = _gtk_9slice_new (pixbuf,
2711 distance_top, distance_bottom,
2712 distance_left, distance_right,
2714 g_object_unref (pixbuf);
2720 unit_parse_str (const gchar *str,
2726 unit = g_ascii_strtod (str, end_str);
2729 /* Now parse the unit type, if any. We
2730 * don't admit spaces between these.
2732 if (*str != ' ' && *str != '\0')
2734 while (**end_str != ' ' && **end_str != '\0')
2737 /* Only handle pixels at the moment */
2738 if (strncmp (str, "px", 2) != 0)
2742 type = g_strndup (str, *end_str - str);
2743 g_warning ("Unknown unit '%s', only pixel units are "
2744 "currently supported in CSS style", type);
2753 border_parse_str (const gchar *str,
2756 gdouble first, second, third, fourth;
2759 border = gtk_border_new ();
2762 if (!g_ascii_isdigit (*str) && *str != '-')
2765 first = unit_parse_str (str, end_str);
2769 if (!g_ascii_isdigit (*str) && *str != '-')
2771 border->left = border->right = border->top = border->bottom = (gint) first;
2772 *end_str = (gchar *) str;
2776 second = unit_parse_str (str, end_str);
2780 if (!g_ascii_isdigit (*str) && *str != '-')
2782 border->top = border->bottom = (gint) first;
2783 border->left = border->right = (gint) second;
2784 *end_str = (gchar *) str;
2788 third = unit_parse_str (str, end_str);
2792 if (!g_ascii_isdigit (*str) && *str != '-')
2794 border->top = (gint) first;
2795 border->left = border->right = (gint) second;
2796 border->bottom = (gint) third;
2797 *end_str = (gchar *) str;
2801 fourth = unit_parse_str (str, end_str);
2803 border->top = (gint) first;
2804 border->right = (gint) second;
2805 border->bottom = (gint) third;
2806 border->left = (gint) fourth;
2812 resolve_binding_sets (const gchar *value_str,
2816 gchar **bindings, **str;
2818 bindings = g_strsplit (value_str, ",", -1);
2819 array = g_ptr_array_new ();
2821 for (str = bindings; *str; str++)
2823 GtkBindingSet *binding_set;
2825 binding_set = gtk_binding_set_find (g_strstrip (*str));
2830 g_ptr_array_add (array, binding_set);
2833 g_value_take_boxed (value, array);
2834 g_strfreev (bindings);
2838 css_provider_parse_value (GtkCssProvider *css_provider,
2839 const gchar *value_str,
2843 GtkCssProviderPrivate *priv;
2845 gboolean parsed = TRUE;
2848 priv = css_provider->priv;
2849 type = G_VALUE_TYPE (value);
2851 if (type == GDK_TYPE_RGBA ||
2852 type == GDK_TYPE_COLOR)
2857 if (type == GDK_TYPE_RGBA &&
2858 gdk_rgba_parse (&rgba, value_str))
2859 g_value_set_boxed (value, &rgba);
2860 else if (type == GDK_TYPE_COLOR &&
2861 gdk_color_parse (value_str, &color))
2862 g_value_set_boxed (value, &color);
2865 GtkSymbolicColor *symbolic_color;
2867 symbolic_color = symbolic_color_parse_str (value_str, &end);
2871 g_value_unset (value);
2872 g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
2873 g_value_take_boxed (value, symbolic_color);
2879 else if (type == PANGO_TYPE_FONT_DESCRIPTION)
2881 PangoFontDescription *font_desc;
2883 font_desc = pango_font_description_from_string (value_str);
2884 g_value_take_boxed (value, font_desc);
2886 else if (type == G_TYPE_BOOLEAN)
2888 if (value_str[0] == '1' ||
2889 g_ascii_strcasecmp (value_str, "true") == 0)
2890 g_value_set_boolean (value, TRUE);
2892 g_value_set_boolean (value, FALSE);
2894 else if (type == G_TYPE_INT)
2895 g_value_set_int (value, atoi (value_str));
2896 else if (type == G_TYPE_UINT)
2897 g_value_set_uint (value, (guint) atoi (value_str));
2898 else if (type == G_TYPE_DOUBLE)
2899 g_value_set_double (value, g_ascii_strtod (value_str, NULL));
2900 else if (type == G_TYPE_FLOAT)
2901 g_value_set_float (value, (gfloat) g_ascii_strtod (value_str, NULL));
2902 else if (type == GTK_TYPE_THEMING_ENGINE)
2904 GtkThemingEngine *engine;
2906 engine = gtk_theming_engine_load (value_str);
2908 g_value_set_object (value, engine);
2912 else if (type == GTK_TYPE_ANIMATION_DESCRIPTION)
2914 GtkAnimationDescription *desc;
2916 desc = _gtk_animation_description_from_string (value_str);
2919 g_value_take_boxed (value, desc);
2923 else if (type == GTK_TYPE_BORDER)
2927 border = border_parse_str (value_str, &end);
2928 g_value_take_boxed (value, border);
2930 else if (type == CAIRO_GOBJECT_TYPE_PATTERN)
2932 GtkGradient *gradient;
2934 gradient = gradient_parse_str (value_str, &end);
2938 g_value_unset (value);
2939 g_value_init (value, GTK_TYPE_GRADIENT);
2940 g_value_take_boxed (value, gradient);
2947 g_clear_error (error);
2948 path = path_parse_str (css_provider, value_str, &end, error);
2952 pixbuf = gdk_pixbuf_new_from_file (path, NULL);
2957 cairo_surface_t *surface;
2958 cairo_pattern_t *pattern;
2960 cairo_matrix_t matrix;
2962 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2963 gdk_pixbuf_get_width (pixbuf),
2964 gdk_pixbuf_get_height (pixbuf));
2965 cr = cairo_create (surface);
2966 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
2968 pattern = cairo_pattern_create_for_surface (surface);
2970 cairo_matrix_init_scale (&matrix,
2971 gdk_pixbuf_get_width (pixbuf),
2972 gdk_pixbuf_get_height (pixbuf));
2973 cairo_pattern_set_matrix (pattern, &matrix);
2975 cairo_surface_destroy (surface);
2977 g_object_unref (pixbuf);
2979 g_value_take_boxed (value, pattern);
2988 else if (G_TYPE_IS_ENUM (type))
2990 GEnumClass *enum_class;
2991 GEnumValue *enum_value;
2993 enum_class = g_type_class_ref (type);
2994 enum_value = g_enum_get_value_by_nick (enum_class, value_str);
2999 GTK_CSS_PROVIDER_ERROR,
3000 GTK_CSS_PROVIDER_ERROR_FAILED,
3001 "Unknown value '%s' for enum type '%s'",
3002 value_str, g_type_name (type));
3006 g_value_set_enum (value, enum_value->value);
3008 g_type_class_unref (enum_class);
3010 else if (G_TYPE_IS_FLAGS (type))
3012 GFlagsClass *flags_class;
3013 GFlagsValue *flag_value;
3017 flags_class = g_type_class_ref (type);
3019 /* Parse comma separated values */
3020 ptr = strchr (value_str, ',');
3022 while (ptr && parsed)
3029 flag_str = (gchar *) value_str;
3030 flag_value = g_flags_get_value_by_nick (flags_class,
3031 g_strstrip (flag_str));
3036 GTK_CSS_PROVIDER_ERROR,
3037 GTK_CSS_PROVIDER_ERROR_FAILED,
3038 "Unknown flag '%s' for type '%s'",
3039 value_str, g_type_name (type));
3043 flags |= flag_value->value;
3046 ptr = strchr (value_str, ',');
3049 /* Store last/only value */
3050 flag_value = g_flags_get_value_by_nick (flags_class, value_str);
3055 GTK_CSS_PROVIDER_ERROR,
3056 GTK_CSS_PROVIDER_ERROR_FAILED,
3057 "Unknown flag '%s' for type '%s'",
3058 value_str, g_type_name (type));
3062 flags |= flag_value->value;
3065 g_value_set_enum (value, flags);
3067 g_type_class_unref (flags_class);
3069 else if (type == GTK_TYPE_9SLICE)
3073 slice = slice_parse_str (css_provider, value_str, &end, error);
3076 g_value_take_boxed (value, slice);
3083 GTK_CSS_PROVIDER_ERROR,
3084 GTK_CSS_PROVIDER_ERROR_FAILED,
3085 "Cannot parse string '%s' for type %s",
3086 value_str, g_type_name (type));
3092 /* Set error position in the scanner
3093 * according to what we've parsed so far
3095 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 while (!g_scanner_eof (priv->scanner) &&
3559 priv->scanner->token != G_TOKEN_RIGHT_CURLY)
3560 g_scanner_get_next_token (priv->scanner);
3563 css_provider_commit (css_provider);
3565 g_scanner_get_next_token (priv->scanner);
3572 * gtk_css_provider_load_from_data:
3573 * @css_provider: a #GtkCssProvider
3574 * @data: CSS data loaded in memory
3575 * @length: the length of @data in bytes, or -1 for NUL terminated strings
3576 * @error: (out) (allow-none): return location for a #GError, or %NULL
3578 * Loads @data into @css_provider, making it clear any previously loaded
3581 * Returns: %TRUE if the data could be loaded.
3584 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
3589 GtkCssProviderPrivate *priv;
3591 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
3592 g_return_val_if_fail (data != NULL, FALSE);
3594 priv = css_provider->priv;
3597 length = strlen (data);
3599 if (priv->selectors_info->len > 0)
3600 g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
3602 priv->scanner->input_name = "-";
3603 priv->buffer = data;
3604 g_scanner_input_text (priv->scanner, data, (guint) length);
3606 g_free (priv->filename);
3607 priv->filename = NULL;
3608 priv->buffer = NULL;
3610 return parse_stylesheet (css_provider, error);
3614 * gtk_css_provider_load_from_file:
3615 * @css_provider: a #GtkCssProvider
3616 * @file: #GFile pointing to a file to load
3617 * @error: (out) (allow-none): return location for a #GError, or %NULL
3619 * Loads the data contained in @file into @css_provider, making it
3620 * clear any previously loaded information.
3622 * Returns: %TRUE if the data could be loaded.
3625 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
3629 GtkCssProviderPrivate *priv;
3630 GError *internal_error = NULL;
3635 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
3636 g_return_val_if_fail (G_IS_FILE (file), FALSE);
3638 priv = css_provider->priv;
3640 if (!g_file_load_contents (file, NULL,
3642 NULL, &internal_error))
3644 g_propagate_error (error, internal_error);
3648 if (priv->selectors_info->len > 0)
3649 g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
3651 g_free (priv->filename);
3652 priv->filename = g_file_get_path (file);
3654 priv->scanner->input_name = priv->filename;
3655 priv->buffer = data;
3656 g_scanner_input_text (priv->scanner, data, (guint) length);
3658 ret = parse_stylesheet (css_provider, error);
3660 priv->buffer = NULL;
3667 gtk_css_provider_load_from_path_internal (GtkCssProvider *css_provider,
3672 GtkCssProviderPrivate *priv;
3673 GError *internal_error = NULL;
3674 GMappedFile *mapped_file;
3679 priv = css_provider->priv;
3681 mapped_file = g_mapped_file_new (path, FALSE, &internal_error);
3685 g_propagate_error (error, internal_error);
3689 length = g_mapped_file_get_length (mapped_file);
3690 data = g_mapped_file_get_contents (mapped_file);
3697 if (priv->selectors_info->len > 0)
3698 g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
3700 g_free (priv->filename);
3701 priv->filename = g_strdup (path);
3704 priv->scanner->input_name = priv->filename;
3705 priv->buffer = data;
3706 g_scanner_input_text (priv->scanner, data, (guint) length);
3708 ret = parse_stylesheet (css_provider, error);
3710 priv->buffer = NULL;
3711 g_mapped_file_unref (mapped_file);
3717 * gtk_css_provider_load_from_path:
3718 * @css_provider: a #GtkCssProvider
3719 * @path: the path of a filename to load, in the GLib filename encoding
3720 * @error: (out) (allow-none): return location for a #GError, or %NULL
3722 * Loads the data contained in @path into @css_provider, making it clear
3723 * any previously loaded information.
3725 * Returns: %TRUE if the data could be loaded.
3728 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
3732 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
3733 g_return_val_if_fail (path != NULL, FALSE);
3735 return gtk_css_provider_load_from_path_internal (css_provider, path,
3740 * gtk_css_provider_get_default:
3742 * Returns the provider containing the style settings used as a
3743 * fallback for all widgets.
3745 * Returns: (transfer none): The provider used for fallback styling.
3746 * This memory is owned by GTK+, and you must not free it.
3749 gtk_css_provider_get_default (void)
3751 static GtkCssProvider *provider;
3753 if (G_UNLIKELY (!provider))
3756 "@define-color fg_color #000; \n"
3757 "@define-color bg_color #dcdad5; \n"
3758 "@define-color text_color #000; \n"
3759 "@define-color base_color #fff; \n"
3760 "@define-color selected_bg_color #4b6983; \n"
3761 "@define-color selected_fg_color #fff; \n"
3762 "@define-color tooltip_bg_color #eee1b3; \n"
3763 "@define-color tooltip_fg_color #000; \n"
3765 "@define-color info_fg_color rgb (181, 171, 156);\n"
3766 "@define-color info_bg_color rgb (252, 252, 189);\n"
3767 "@define-color warning_fg_color rgb (173, 120, 41);\n"
3768 "@define-color warning_bg_color rgb (250, 173, 61);\n"
3769 "@define-color question_fg_color rgb (97, 122, 214);\n"
3770 "@define-color question_bg_color rgb (138, 173, 212);\n"
3771 "@define-color error_fg_color rgb (166, 38, 38);\n"
3772 "@define-color error_bg_color rgb (237, 54, 54);\n"
3775 " background-color: @bg_color;\n"
3776 " color: @fg_color;\n"
3777 " border-color: shade (@bg_color, 0.6);\n"
3779 " border-width: 0;\n"
3783 " background-color: shade (@bg_color, 1.05);\n"
3784 " color: shade (@fg_color, 1.3);\n"
3788 " background-color: @selected_bg_color;\n"
3789 " color: @selected_fg_color;\n"
3792 ".expander, GtkTreeView.view.expander {\n"
3796 ".expander:prelight {\n"
3797 " color: @text_color;\n"
3800 ".expander:active {\n"
3801 " transition: 200ms linear;\n"
3805 " border-color: shade (@bg_color, 0.7);\n"
3806 " background-color: shade (@bg_color, 0.9);\n"
3807 " color: shade (@bg_color, 0.7);\n"
3811 " border-width: 0;\n"
3812 " border-radius: 0;\n"
3813 " background-color: @base_color;\n"
3814 " color: @text_color;\n"
3816 ".view:selected {\n"
3817 " background-color: shade (@bg_color, 0.9);\n"
3818 " color: @fg_color;\n"
3821 ".view:selected:focused {\n"
3822 " background-color: @selected_bg_color;\n"
3823 " color: @selected_fg_color;\n"
3826 ".view column:sorted row,\n"
3827 ".view column:sorted row:prelight {\n"
3828 " background-color: shade (@bg_color, 0.85);\n"
3831 ".view column:sorted row:nth-child(odd),\n"
3832 ".view column:sorted row:nth-child(odd):prelight {\n"
3833 " background-color: shade (@bg_color, 0.8);\n"
3837 ".view row:prelight {\n"
3838 " background-color: @base_color;\n"
3839 " color: @text_color;\n"
3842 ".view row:nth-child(odd),\n"
3843 ".view row:nth-child(odd):prelight {\n"
3844 " background-color: shade (@base_color, 0.93); \n"
3847 ".view row:selected:focused {\n"
3848 " background-color: @selected_bg_color;\n"
3851 ".view row:selected {\n"
3852 " background-color: darker (@bg_color);\n"
3853 " color: @selected_fg_color;\n"
3856 ".view.cell.trough,\n"
3857 ".view.cell.trough:hover,\n"
3858 ".view.cell.trough:selected,\n"
3859 ".view.cell.trough:selected:focused {\n"
3860 " background-color: @bg_color;\n"
3861 " color: @fg_color;\n"
3864 ".view.cell.progressbar,\n"
3865 ".view.cell.progressbar:hover,\n"
3866 ".view.cell.progressbar:selected,\n"
3867 ".view.cell.progressbar:selected:focused {\n"
3868 " background-color: @selected_bg_color;\n"
3869 " color: @selected_fg_color;\n"
3873 " background-color: alpha (@fg_color, 0.25);\n"
3874 " border-color: @fg_color;\n"
3875 " border-style: solid;\n"
3876 " border-width: 1;\n"
3880 " background-color: @tooltip_bg_color; \n"
3881 " color: @tooltip_fg_color; \n"
3882 " border-color: @tooltip_fg_color; \n"
3883 " border-width: 1;\n"
3884 " border-style: solid;\n"
3889 " border-style: outset; \n"
3890 " border-width: 2; \n"
3893 ".button:active {\n"
3894 " background-color: shade (@bg_color, 0.7);\n"
3895 " border-style: inset; \n"
3898 ".button:prelight,\n"
3899 ".slider:prelight {\n"
3900 " background-color: @selected_bg_color;\n"
3901 " color: @selected_fg_color;\n"
3902 " border-color: shade (@selected_bg_color, 0.7);\n"
3906 " border-style: inset;\n"
3907 " border-width: 1;\n"
3912 " border-style: inset;\n"
3913 " border-width: 2;\n"
3914 " background-color: @base_color;\n"
3915 " color: @text_color;\n"
3918 ".entry:insensitive {\n"
3919 " background-color: shade (@base_color, 0.9);\n"
3920 " color: shade (@base_color, 0.7);\n"
3923 " background-color: #c4c2bd;\n"
3928 ".entry.progressbar, \n"
3929 ".cell.progressbar {\n"
3930 " background-color: @selected_bg_color;\n"
3931 " border-color: shade (@selected_bg_color, 0.7);\n"
3932 " color: @selected_fg_color;\n"
3933 " border-style: outset;\n"
3934 " border-width: 1;\n"
3937 "GtkCheckButton:hover,\n"
3938 "GtkCheckButton:selected,\n"
3939 "GtkRadioButton:hover,\n"
3940 "GtkRadioButton:selected {\n"
3941 " background-color: shade (@bg_color, 1.05);\n"
3945 ".cell.check, .cell.radio,\n"
3946 ".cell.check:hover, .cell.radio:hover {\n"
3947 " border-style: solid;\n"
3948 " border-width: 1;\n"
3949 " background-color: @base_color;\n"
3950 " border-color: @fg_color;\n"
3953 ".check:active, .radio:active,\n"
3954 ".check:hover, .radio:hover {\n"
3955 " background-color: @base_color;\n"
3956 " border-color: @fg_color;\n"
3957 " color: @text_color;\n"
3960 ".check:selected, .radio:selected {\n"
3961 " background-color: darker (@bg_color);\n"
3962 " color: @selected_fg_color;\n"
3963 " border-color: @selected_fg_color;\n"
3966 ".check:selected:focused, .radio:selected:focused {\n"
3967 " background-color: @selected_bg_color;\n"
3970 ".menu.check, .menu.radio {\n"
3971 " color: @fg_color;\n"
3972 " border-style: none;\n"
3973 " border-width: 0;\n"
3977 " border-style: outset;\n"
3978 " border-width: 1;\n"
3982 " border-style: inset;\n"
3983 " border-width: 2;\n"
3987 " border-style: outset;\n"
3988 " border-width: 1;\n"
3992 " border-style: inset;\n"
3993 " border-width: 1;\n"
3996 "GtkScrolledWindow.frame {\n"
4003 " border-style: outset;\n"
4004 " border-width: 1;\n"
4009 ".menu.check:hover,\n"
4010 ".menu.radio:hover {\n"
4011 " background-color: @selected_bg_color;\n"
4012 " color: @selected_fg_color;\n"
4015 "GtkSpinButton.button {\n"
4016 " border-width: 1;\n"
4019 ".scale.slider:hover,\n"
4020 "GtkSpinButton.button:hover {\n"
4021 " background-color: shade (@bg_color, 1.05);\n"
4022 " border-color: shade (@bg_color, 0.8);\n"
4025 "GtkSwitch.trough:active {\n"
4026 " background-color: @selected_bg_color;\n"
4027 " color: @selected_fg_color;\n"
4030 "GtkToggleButton.button:inconsistent {\n"
4031 " border-style: outset;\n"
4032 " border-width: 1px;\n"
4033 " background-color: shade (@bg_color, 0.9);\n"
4034 " border-color: shade (@bg_color, 0.7);\n"
4037 "GtkLabel:selected {\n"
4038 " background-color: shade (@bg_color, 0.9);\n"
4039 " color: @fg_color;\n"
4042 "GtkLabel:selected:focused {\n"
4043 " background-color: @selected_bg_color;\n"
4044 " color: @selected_fg_color;\n"
4047 ".spinner:active {\n"
4048 " transition: 750ms linear loop;\n"
4052 " background-color: @info_bg_color;\n"
4053 " color: @info_fg_color;\n"
4057 " background-color: @warning_bg_color;\n"
4058 " color: @warning_fg_color;\n"
4062 " background-color: @question_bg_color;\n"
4063 " color: @question_fg_color;\n"
4067 " background-color: @error_bg_color;\n"
4068 " color: @error_fg_color;\n"
4072 " background-color: @selected_bg_color;\n"
4073 " color: @selected_fg_color;\n"
4076 ".light-area-focus {\n"
4080 ".dark-area-focus {\n"
4083 "GtkCalendar.view {\n"
4084 " border-width: 1;\n"
4085 " border-style: inset;\n"
4089 "GtkCalendar.view:inconsistent {\n"
4090 " color: darker (@bg_color);\n"
4093 "GtkCalendar.header {\n"
4094 " background-color: @bg_color;\n"
4095 " border-style: outset;\n"
4096 " border-width: 2;\n"
4099 "GtkCalendar.highlight {\n"
4100 " border-width: 0;\n"
4103 "GtkCalendar.button {\n"
4104 " background-color: @bg_color;\n"
4107 "GtkCalendar.button:hover {\n"
4108 " background-color: lighter (@bg_color);\n"
4109 " color: @fg_color;\n"
4113 " border-width: 1;\n"
4118 " border-width: 0;\n"
4123 provider = gtk_css_provider_new ();
4124 if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
4126 g_error ("Failed to load the internal default CSS.");
4134 _gtk_css_provider_get_theme_dir (void)
4139 var = g_getenv ("GTK_DATA_PREFIX");
4142 path = g_build_filename (var, "share", "themes", NULL);
4144 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
4150 * gtk_css_provider_get_named:
4151 * @name: A theme name
4152 * @variant: (allow-none): variant to load, for example, "dark", or
4153 * %NULL for the default
4155 * Loads a theme from the usual theme paths
4157 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
4158 * This memory is owned by GTK+, and you must not free it.
4161 gtk_css_provider_get_named (const gchar *name,
4162 const gchar *variant)
4164 static GHashTable *themes = NULL;
4165 GtkCssProvider *provider;
4168 if (G_UNLIKELY (!themes))
4169 themes = g_hash_table_new (g_str_hash, g_str_equal);
4171 if (variant == NULL)
4172 key = (gchar *)name;
4174 key = g_strconcat (name, "-", variant, NULL);
4176 provider = g_hash_table_lookup (themes, key);
4180 const gchar *home_dir;
4181 gchar *subpath, *path = NULL;
4184 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
4186 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
4188 /* First look in the users home directory
4190 home_dir = g_get_home_dir ();
4193 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
4195 if (!g_file_test (path, G_FILE_TEST_EXISTS))
4206 theme_dir = _gtk_css_provider_get_theme_dir ();
4207 path = g_build_filename (theme_dir, name, subpath, NULL);
4210 if (!g_file_test (path, G_FILE_TEST_EXISTS))
4223 provider = gtk_css_provider_new ();
4225 if (!gtk_css_provider_load_from_path (provider, path, &error))
4227 g_warning ("Could not load named theme \"%s\": %s", name, error->message);
4228 g_error_free (error);
4230 g_object_unref (provider);
4234 g_hash_table_insert (themes, g_strdup (key), provider);