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 "gtkcssstringfuncsprivate.h"
31 #include "gtksymboliccolor.h"
32 #include "gtkstyleprovider.h"
33 #include "gtkstylecontextprivate.h"
34 #include "gtkbindings.h"
35 #include "gtkmarshalers.h"
36 #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 struct _GtkCssScannerPrivate GtkCssScannerPrivate;
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 _GtkCssScannerPrivate
793 GSList *cur_selectors;
794 GHashTable *cur_properties;
797 struct _GtkCssProviderPrivate
801 GHashTable *symbolic_colors;
803 GPtrArray *selectors_info;
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
837 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
839 static void gtk_css_provider_finalize (GObject *object);
840 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
842 static void scanner_apply_scope (GScanner *scanner,
844 static gboolean gtk_css_provider_load_from_path_internal (GtkCssProvider *css_provider,
848 static void gtk_css_provider_take_error (GtkCssProvider *provider,
853 gtk_css_provider_error_quark (void)
855 return g_quark_from_static_string ("gtk-css-provider-error-quark");
858 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
859 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
860 gtk_css_style_provider_iface_init));
863 gtk_css_provider_parsing_error (GtkCssProvider *provider,
867 const GError * error)
869 /* Only emit a warning when we have no error handlers. This is our
870 * default handlers. And in this case erroneous CSS files are a bug
871 * and should be fixed.
872 * Note that these warnings can also be triggered by a broken theme
873 * that people installed from some weird location on the internets.
875 if (!g_signal_has_handler_pending (provider,
876 css_provider_signals[PARSING_ERROR],
880 g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
885 gtk_css_provider_class_init (GtkCssProviderClass *klass)
887 GObjectClass *object_class = G_OBJECT_CLASS (klass);
890 * GtkCssProvider::parsing-error:
891 * @provider: the provider that had a parsing error
892 * @path: path to the parsed file or %NULL if the file cannot be
893 * identified or the data was not loaded from a file
894 * @line: line in the file or data or 0 if unknown
895 * @position: offset into the current line or 0 if unknown or the
896 * whole line is affected
897 * @error: The parsing error
899 * Signals that a parsing error occured. the @path, @line and @position
900 * describe the actual location of the error as accurately as possible.
902 * Parsing errors are never fatal, so the parsing will resume after
903 * the error. Errors may however cause parts of the given
904 * data or even all of it to not be parsed at all. So it is a useful idea
905 * to check that the parsing succeeds by connecting to this signal.
907 * Note that this signal may be emitted at any time as the css provider
908 * may opt to defer parsing parts or all of the input to a later time
909 * than when a loading function was called.
911 css_provider_signals[PARSING_ERROR] =
912 g_signal_new (I_("parsing-error"),
913 G_TYPE_FROM_CLASS (object_class),
915 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
917 _gtk_marshal_VOID__STRING_UINT_UINT_BOXED,
919 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR);
921 object_class->finalize = gtk_css_provider_finalize;
923 klass->parsing_error = gtk_css_provider_parsing_error;
925 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
928 static SelectorPath *
929 selector_path_new (void)
933 path = g_slice_new0 (SelectorPath);
939 static SelectorPath *
940 selector_path_ref (SelectorPath *path)
947 selector_path_unref (SelectorPath *path)
951 if (path->ref_count > 0)
954 while (path->elements)
956 g_slice_free (SelectorElement, path->elements->data);
957 path->elements = g_slist_delete_link (path->elements, path->elements);
960 g_slice_free (SelectorPath, path);
964 selector_path_prepend_type (SelectorPath *path,
965 const gchar *type_name)
967 SelectorElement *elem;
970 elem = g_slice_new (SelectorElement);
971 elem->combinator = COMBINATOR_DESCENDANT;
972 type = g_type_from_name (type_name);
974 if (type == G_TYPE_INVALID)
976 elem->elem_type = SELECTOR_TYPE_NAME;
977 elem->name = g_quark_from_string (type_name);
981 elem->elem_type = SELECTOR_GTYPE;
985 path->elements = g_slist_prepend (path->elements, elem);
989 selector_path_prepend_glob (SelectorPath *path)
991 SelectorElement *elem;
993 elem = g_slice_new (SelectorElement);
994 elem->elem_type = SELECTOR_GLOB;
995 elem->combinator = COMBINATOR_DESCENDANT;
997 path->elements = g_slist_prepend (path->elements, elem);
1001 selector_path_prepend_region (SelectorPath *path,
1003 GtkRegionFlags flags)
1005 SelectorElement *elem;
1007 elem = g_slice_new (SelectorElement);
1008 elem->combinator = COMBINATOR_DESCENDANT;
1009 elem->elem_type = SELECTOR_REGION;
1011 elem->region.name = g_quark_from_string (name);
1012 elem->region.flags = flags;
1014 path->elements = g_slist_prepend (path->elements, elem);
1018 selector_path_prepend_name (SelectorPath *path,
1021 SelectorElement *elem;
1023 elem = g_slice_new (SelectorElement);
1024 elem->combinator = COMBINATOR_DESCENDANT;
1025 elem->elem_type = SELECTOR_NAME;
1027 elem->name = g_quark_from_string (name);
1029 path->elements = g_slist_prepend (path->elements, elem);
1033 selector_path_prepend_class (SelectorPath *path,
1036 SelectorElement *elem;
1038 elem = g_slice_new (SelectorElement);
1039 elem->combinator = COMBINATOR_DESCENDANT;
1040 elem->elem_type = SELECTOR_CLASS;
1042 elem->name = g_quark_from_string (name);
1044 path->elements = g_slist_prepend (path->elements, elem);
1048 selector_path_prepend_combinator (SelectorPath *path,
1049 CombinatorType combinator)
1051 SelectorElement *elem;
1053 g_assert (path->elements != NULL);
1055 /* It is actually stored in the last element */
1056 elem = path->elements->data;
1057 elem->combinator = combinator;
1061 selector_path_depth (SelectorPath *path)
1063 return g_slist_length (path->elements);
1066 static SelectorStyleInfo *
1067 selector_style_info_new (SelectorPath *path)
1069 SelectorStyleInfo *info;
1071 info = g_slice_new0 (SelectorStyleInfo);
1072 info->path = selector_path_ref (path);
1078 selector_style_info_free (SelectorStyleInfo *info)
1081 g_hash_table_unref (info->style);
1084 selector_path_unref (info->path);
1086 g_slice_free (SelectorStyleInfo, info);
1090 selector_style_info_set_style (SelectorStyleInfo *info,
1094 g_hash_table_unref (info->style);
1097 info->style = g_hash_table_ref (style);
1103 property_value_free (GValue *value)
1105 if (G_IS_VALUE (value))
1106 g_value_unset (value);
1108 g_slice_free (GValue, value);
1112 gtk_css_scanner_reset (GScanner *scanner)
1114 GtkCssScannerPrivate *priv = scanner->user_data;
1116 g_slist_free (priv->state);
1119 g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
1120 g_slist_free (priv->cur_selectors);
1121 priv->cur_selectors = NULL;
1123 if (priv->cur_properties)
1124 g_hash_table_unref (priv->cur_properties);
1126 priv->cur_properties = g_hash_table_new_full (g_str_hash,
1128 (GDestroyNotify) g_free,
1129 (GDestroyNotify) property_value_free);
1131 scanner_apply_scope (scanner, SCOPE_SELECTOR);
1135 gtk_css_scanner_destroy (GScanner *scanner)
1137 GtkCssScannerPrivate *priv = scanner->user_data;
1139 gtk_css_scanner_reset (scanner);
1142 g_object_unref (priv->file);
1143 g_hash_table_destroy (priv->cur_properties);
1144 g_slice_free (GtkCssScannerPrivate, priv);
1146 g_scanner_destroy (scanner);
1150 gtk_css_scanner_new (GFile *file,
1154 GtkCssScannerPrivate *priv;
1157 scanner = g_scanner_new (NULL);
1159 priv = scanner->user_data = g_slice_new0 (GtkCssScannerPrivate);
1162 priv->file = g_object_ref (file);
1164 priv->cur_properties = g_hash_table_new_full (g_str_hash,
1166 (GDestroyNotify) g_free,
1167 (GDestroyNotify) property_value_free);
1169 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "active", GUINT_TO_POINTER (GTK_STATE_ACTIVE));
1170 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "prelight", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
1171 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "hover", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
1172 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "selected", GUINT_TO_POINTER (GTK_STATE_SELECTED));
1173 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "insensitive", GUINT_TO_POINTER (GTK_STATE_INSENSITIVE));
1174 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "inconsistent", GUINT_TO_POINTER (GTK_STATE_INCONSISTENT));
1175 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focused", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
1176 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focus", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
1178 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "nth-child", GUINT_TO_POINTER (SYMBOL_NTH_CHILD));
1179 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "first-child", GUINT_TO_POINTER (SYMBOL_FIRST_CHILD));
1180 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "last-child", GUINT_TO_POINTER (SYMBOL_LAST_CHILD));
1181 g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "sorted", GUINT_TO_POINTER (SYMBOL_SORTED_CHILD));
1183 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "even", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_EVEN));
1184 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "odd", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_ODD));
1185 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "first", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_FIRST));
1186 g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "last", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_LAST));
1188 scanner_apply_scope (scanner, SCOPE_SELECTOR);
1190 if (length > G_MAXUINT32)
1191 g_warning ("CSS file too large, truncating");
1193 g_scanner_input_text (scanner, data, length);
1199 gtk_css_provider_init (GtkCssProvider *css_provider)
1201 GtkCssProviderPrivate *priv;
1203 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1204 GTK_TYPE_CSS_PROVIDER,
1205 GtkCssProviderPrivate);
1207 priv->selectors_info = g_ptr_array_new_with_free_func ((GDestroyNotify) selector_style_info_free);
1209 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1210 (GDestroyNotify) g_free,
1211 (GDestroyNotify) gtk_symbolic_color_unref);
1214 typedef struct ComparePathData ComparePathData;
1216 struct ComparePathData
1224 compare_selector_element (GtkWidgetPath *path,
1226 SelectorElement *elem,
1231 if (elem->elem_type == SELECTOR_TYPE_NAME)
1233 const gchar *type_name;
1234 GType resolved_type;
1236 /* Resolve the type name */
1237 type_name = g_quark_to_string (elem->name);
1238 resolved_type = g_type_from_name (type_name);
1240 if (resolved_type == G_TYPE_INVALID)
1242 /* Type couldn't be resolved, so the selector
1243 * clearly doesn't affect the given widget path
1248 elem->elem_type = SELECTOR_GTYPE;
1249 elem->type = resolved_type;
1252 if (elem->elem_type == SELECTOR_GTYPE)
1256 type = gtk_widget_path_iter_get_object_type (path, index);
1258 if (!g_type_is_a (type, elem->type))
1261 if (type == elem->type)
1265 guint diff = g_type_depth (type) - g_type_depth (elem->type);
1267 if (G_UNLIKELY (diff > 0xE))
1269 g_warning ("Hierarchy is higher than expected.");
1273 *score = 0XF - diff;
1278 else if (elem->elem_type == SELECTOR_REGION)
1280 GtkRegionFlags flags;
1282 if (!gtk_widget_path_iter_has_qregion (path, index,
1287 if (elem->region.flags != 0 &&
1288 (flags & elem->region.flags) == 0)
1294 else if (elem->elem_type == SELECTOR_GLOB)
1296 /* Treat as lowest matching type */
1300 else if (elem->elem_type == SELECTOR_NAME)
1302 if (!gtk_widget_path_iter_has_qname (path, index, elem->name))
1308 else if (elem->elem_type == SELECTOR_CLASS)
1310 if (!gtk_widget_path_iter_has_qclass (path, index, elem->name))
1321 compare_selector (GtkWidgetPath *path,
1322 SelectorPath *selector)
1324 GSList *elements = selector->elements;
1325 gboolean match = TRUE, first = TRUE, first_match = FALSE;
1329 i = gtk_widget_path_length (path) - 1;
1331 while (elements && match && i >= 0)
1333 SelectorElement *elem;
1336 elem = elements->data;
1338 match = compare_selector_element (path, i, elem, &elem_score);
1343 /* Only move on to the next index if there is no match
1344 * with the current element (whether to continue or not
1345 * handled right after in the combinator check), or a
1346 * GType or glob has just been matched.
1348 * Region and widget names do not trigger this because
1349 * the next element in the selector path could also be
1350 * related to the same index.
1353 (elem->elem_type == SELECTOR_GTYPE ||
1354 elem->elem_type == SELECTOR_GLOB))
1358 elem->elem_type != SELECTOR_TYPE_NAME &&
1359 elem->combinator == COMBINATOR_DESCENDANT)
1361 /* With descendant combinators there may
1362 * be intermediate chidren in the hierarchy
1367 elements = elements->next;
1371 /* Only 4 bits are actually used */
1373 score |= elem_score;
1379 /* If there are pending selector
1380 * elements to compare, it's not
1388 else if (first_match)
1390 /* Assign more weight to these selectors
1391 * that matched right from the first element.
1399 typedef struct StylePriorityInfo StylePriorityInfo;
1401 struct StylePriorityInfo
1405 GtkStateFlags state;
1409 css_provider_get_selectors (GtkCssProvider *css_provider,
1410 GtkWidgetPath *path)
1412 GtkCssProviderPrivate *priv;
1413 GArray *priority_info;
1416 priv = css_provider->priv;
1417 priority_info = g_array_new (FALSE, FALSE, sizeof (StylePriorityInfo));
1419 for (i = 0; i < priv->selectors_info->len; i++)
1421 SelectorStyleInfo *info;
1422 StylePriorityInfo new;
1423 gboolean added = FALSE;
1426 info = g_ptr_array_index (priv->selectors_info, i);
1427 score = compare_selector (path, info->path);
1433 new.style = info->style;
1434 new.state = info->path->state;
1436 for (j = 0; j < priority_info->len; j++)
1438 StylePriorityInfo *cur;
1440 cur = &g_array_index (priority_info, StylePriorityInfo, j);
1442 if (cur->score > new.score)
1444 g_array_insert_val (priority_info, j, new);
1451 g_array_append_val (priority_info, new);
1454 return priority_info;
1458 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1459 GtkStyleProperties *props)
1461 GtkCssProviderPrivate *priv;
1462 GHashTableIter iter;
1463 gpointer key, value;
1465 priv = css_provider->priv;
1466 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1468 while (g_hash_table_iter_next (&iter, &key, &value))
1471 GtkSymbolicColor *color;
1476 gtk_style_properties_map_color (props, name, color);
1480 static GtkStyleProperties *
1481 gtk_css_provider_get_style (GtkStyleProvider *provider,
1482 GtkWidgetPath *path)
1484 GtkCssProvider *css_provider;
1485 GtkStyleProperties *props;
1486 GArray *priority_info;
1489 css_provider = GTK_CSS_PROVIDER (provider);
1490 props = gtk_style_properties_new ();
1492 css_provider_dump_symbolic_colors (css_provider, props);
1493 priority_info = css_provider_get_selectors (css_provider, path);
1495 for (i = 0; i < priority_info->len; i++)
1497 StylePriorityInfo *info;
1498 GHashTableIter iter;
1499 gpointer key, value;
1501 info = &g_array_index (priority_info, StylePriorityInfo, i);
1502 g_hash_table_iter_init (&iter, info->style);
1504 while (g_hash_table_iter_next (&iter, &key, &value))
1508 /* Properties starting with '-' may be both widget style properties
1509 * or custom properties from the theming engine, so check whether
1510 * the type is registered or not.
1512 if (prop[0] == '-' &&
1513 !gtk_style_properties_lookup_property (prop, NULL, NULL))
1516 gtk_style_properties_set_property (props, key, info->state, value);
1520 g_array_free (priority_info, TRUE);
1526 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1527 GtkWidgetPath *path,
1528 GtkStateFlags state,
1532 GArray *priority_info;
1533 gboolean found = FALSE;
1537 prop_name = g_strdup_printf ("-%s-%s",
1538 g_type_name (pspec->owner_type),
1541 priority_info = css_provider_get_selectors (GTK_CSS_PROVIDER (provider), path);
1543 for (i = priority_info->len - 1; i >= 0; i--)
1545 StylePriorityInfo *info;
1548 info = &g_array_index (priority_info, StylePriorityInfo, i);
1549 val = g_hash_table_lookup (info->style, prop_name);
1552 (info->state == 0 ||
1553 info->state == state ||
1554 ((info->state & state) != 0 &&
1555 (info->state & ~(state)) == 0)))
1557 GError *error = NULL;
1559 found = _gtk_css_value_from_string (value,
1561 g_value_get_string (val),
1567 gtk_css_provider_take_error (GTK_CSS_PROVIDER (provider), NULL, error);
1571 g_array_free (priority_info, TRUE);
1578 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1580 iface->get_style = gtk_css_provider_get_style;
1581 iface->get_style_property = gtk_css_provider_get_style_property;
1585 gtk_css_provider_finalize (GObject *object)
1587 GtkCssProvider *css_provider;
1588 GtkCssProviderPrivate *priv;
1590 css_provider = GTK_CSS_PROVIDER (object);
1591 priv = css_provider->priv;
1593 g_ptr_array_free (priv->selectors_info, TRUE);
1595 if (priv->symbolic_colors)
1596 g_hash_table_destroy (priv->symbolic_colors);
1598 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1602 * gtk_css_provider_new:
1604 * Returns a newly created #GtkCssProvider.
1606 * Returns: A new #GtkCssProvider
1609 gtk_css_provider_new (void)
1611 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1615 gtk_css_provider_take_error (GtkCssProvider *provider,
1620 guint line, position;
1624 GtkCssScannerPrivate *priv = scanner->user_data;
1626 filename = g_file_get_path (priv->file);
1629 line = scanner->line;
1630 position = scanner->position;
1639 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1640 filename, line, position, error);
1643 g_error_free (error);
1647 gtk_css_provider_error_literal (GtkCssProvider *provider,
1651 const char *message)
1653 gtk_css_provider_take_error (provider,
1655 g_error_new_literal (domain, code, message));
1659 gtk_css_provider_error (GtkCssProvider *provider,
1664 ...) G_GNUC_PRINTF (5, 6);
1666 gtk_css_provider_error (GtkCssProvider *provider,
1676 va_start (args, format);
1677 error = g_error_new_valist (domain, code, format, args);
1680 gtk_css_provider_take_error (provider, scanner, error);
1684 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1686 const char *expected)
1688 gtk_css_provider_error (provider,
1690 GTK_CSS_PROVIDER_ERROR,
1691 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1692 "expected a valid %s", expected);
1696 scanner_apply_scope (GScanner *scanner,
1699 g_scanner_set_scope (scanner, scope);
1701 if (scope == SCOPE_VALUE)
1703 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
1704 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(),.%\t\n'/\"";
1705 scanner->config->scan_identifier_1char = TRUE;
1707 else if (scope == SCOPE_BINDING_SET)
1709 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
1710 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(){}<>,.%\t\n'/\"";
1711 scanner->config->scan_identifier_1char = TRUE;
1713 else if (scope == SCOPE_SELECTOR)
1715 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "*@";
1716 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_#.";
1717 scanner->config->scan_identifier_1char = TRUE;
1719 else if (scope == SCOPE_PSEUDO_CLASS ||
1720 scope == SCOPE_NTH_CHILD ||
1721 scope == SCOPE_DECLARATION)
1723 scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "-_";
1724 scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_";
1725 scanner->config->scan_identifier_1char = FALSE;
1728 g_assert_not_reached ();
1730 scanner->config->scan_float = FALSE;
1731 scanner->config->cpair_comment_single = NULL;
1735 gtk_css_scanner_push_scope (GScanner *scanner,
1738 GtkCssScannerPrivate *priv;
1740 priv = scanner->user_data;
1741 priv->state = g_slist_prepend (priv->state, GUINT_TO_POINTER (scope));
1743 scanner_apply_scope (scanner, scope);
1747 gtk_css_scanner_pop_scope (GScanner *scanner)
1749 GtkCssScannerPrivate *priv;
1750 ParserScope scope = SCOPE_SELECTOR;
1752 priv = scanner->user_data;
1756 g_warning ("Push/pop calls to parser scope aren't paired");
1757 scanner_apply_scope (scanner, SCOPE_SELECTOR);
1761 priv->state = g_slist_delete_link (priv->state, priv->state);
1763 /* Fetch new scope */
1765 scope = GPOINTER_TO_INT (priv->state->data);
1767 scanner_apply_scope (scanner, scope);
1771 css_provider_commit (GtkCssProvider *css_provider,
1774 GtkCssScannerPrivate *scanner_priv;
1775 GtkCssProviderPrivate *priv;
1778 priv = css_provider->priv;
1779 scanner_priv = scanner->user_data;
1781 l = scanner_priv->cur_selectors;
1783 if (g_hash_table_size (scanner_priv->cur_properties) == 0)
1788 SelectorPath *path = l->data;
1789 SelectorStyleInfo *info;
1791 info = selector_style_info_new (path);
1792 selector_style_info_set_style (info, scanner_priv->cur_properties);
1794 g_ptr_array_add (priv->selectors_info, info);
1800 parse_nth_child (GtkCssProvider *css_provider,
1802 GtkRegionFlags *flags)
1804 ParserSymbol symbol;
1806 g_scanner_get_next_token (scanner);
1808 if (scanner->token != G_TOKEN_SYMBOL)
1809 return G_TOKEN_SYMBOL;
1811 symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
1813 if (symbol == SYMBOL_NTH_CHILD)
1815 g_scanner_get_next_token (scanner);
1817 if (scanner->token != G_TOKEN_LEFT_PAREN)
1818 return G_TOKEN_LEFT_PAREN;
1820 gtk_css_scanner_push_scope (scanner, SCOPE_NTH_CHILD);
1821 g_scanner_get_next_token (scanner);
1823 if (scanner->token != G_TOKEN_SYMBOL)
1824 return G_TOKEN_SYMBOL;
1826 symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
1830 case SYMBOL_NTH_CHILD_EVEN:
1831 *flags = GTK_REGION_EVEN;
1833 case SYMBOL_NTH_CHILD_ODD:
1834 *flags = GTK_REGION_ODD;
1836 case SYMBOL_NTH_CHILD_FIRST:
1837 *flags = GTK_REGION_FIRST;
1839 case SYMBOL_NTH_CHILD_LAST:
1840 *flags = GTK_REGION_LAST;
1846 g_scanner_get_next_token (scanner);
1848 if (scanner->token != G_TOKEN_RIGHT_PAREN)
1849 return G_TOKEN_RIGHT_PAREN;
1851 gtk_css_scanner_pop_scope (scanner);
1853 else if (symbol == SYMBOL_FIRST_CHILD)
1854 *flags = GTK_REGION_FIRST;
1855 else if (symbol == SYMBOL_LAST_CHILD)
1856 *flags = GTK_REGION_LAST;
1857 else if (symbol == SYMBOL_SORTED_CHILD)
1858 *flags = GTK_REGION_SORTED;
1862 return G_TOKEN_SYMBOL;
1865 return G_TOKEN_NONE;
1869 parse_pseudo_class (GtkCssProvider *css_provider,
1871 SelectorPath *selector)
1875 g_scanner_get_next_token (scanner);
1877 if (scanner->token != G_TOKEN_SYMBOL)
1878 return G_TOKEN_SYMBOL;
1880 state = GPOINTER_TO_INT (scanner->value.v_symbol);
1884 case GTK_STATE_ACTIVE:
1885 selector->state |= GTK_STATE_FLAG_ACTIVE;
1887 case GTK_STATE_PRELIGHT:
1888 selector->state |= GTK_STATE_FLAG_PRELIGHT;
1890 case GTK_STATE_SELECTED:
1891 selector->state |= GTK_STATE_FLAG_SELECTED;
1893 case GTK_STATE_INSENSITIVE:
1894 selector->state |= GTK_STATE_FLAG_INSENSITIVE;
1896 case GTK_STATE_INCONSISTENT:
1897 selector->state |= GTK_STATE_FLAG_INCONSISTENT;
1899 case GTK_STATE_FOCUSED:
1900 selector->state |= GTK_STATE_FLAG_FOCUSED;
1903 return G_TOKEN_SYMBOL;
1906 return G_TOKEN_NONE;
1909 /* Parses a number of concatenated classes */
1911 parse_classes (SelectorPath *path,
1916 if ((pos = strchr (str, '.')) != NULL)
1918 /* Leave the last class to the call after the loop */
1922 selector_path_prepend_class (path, str);
1925 pos = strchr (str, '.');
1929 selector_path_prepend_class (path, str);
1933 is_widget_class_name (const gchar *str)
1935 /* Do a pretty lax check here, not all
1936 * widget class names contain only CamelCase
1937 * (gtkmm widgets don't), but at least part of
1938 * the name will be CamelCase, so check for
1939 * the first uppercase char
1943 if (g_ascii_isupper (*str))
1953 parse_selector (GtkCssProvider *css_provider,
1955 SelectorPath **selector_out)
1959 path = selector_path_new ();
1960 *selector_out = path;
1962 if (scanner->token != ':' &&
1963 scanner->token != '#' &&
1964 scanner->token != '.' &&
1965 scanner->token != G_TOKEN_IDENTIFIER)
1966 return G_TOKEN_IDENTIFIER;
1968 while (scanner->token == '#' ||
1969 scanner->token == '.' ||
1970 scanner->token == G_TOKEN_IDENTIFIER)
1972 if (scanner->token == '#' ||
1973 scanner->token == '.')
1978 is_class = (scanner->token == '.');
1980 g_scanner_get_next_token (scanner);
1982 if (scanner->token != G_TOKEN_IDENTIFIER)
1983 return G_TOKEN_IDENTIFIER;
1985 selector_path_prepend_glob (path);
1986 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
1989 parse_classes (path, scanner->value.v_identifier);
1992 if ((pos = strchr (scanner->value.v_identifier, '.')) != NULL)
1995 selector_path_prepend_name (path, scanner->value.v_identifier);
1997 /* Parse any remaining classes */
1999 parse_classes (path, pos + 1);
2002 else if (is_widget_class_name (scanner->value.v_identifier))
2006 if ((pos = strchr (scanner->value.v_identifier, '#')) != NULL ||
2007 (pos = strchr (scanner->value.v_identifier, '.')) != NULL)
2009 gchar *type_name, *name;
2012 is_class = (*pos == '.');
2014 /* Widget type and name/class put together */
2017 type_name = scanner->value.v_identifier;
2019 selector_path_prepend_type (path, type_name);
2021 /* This is only so there is a direct relationship
2022 * between widget type and its name.
2024 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
2027 parse_classes (path, name);
2030 if ((pos = strchr (name, '.')) != NULL)
2033 selector_path_prepend_name (path, name);
2035 /* Parse any remaining classes */
2037 parse_classes (path, pos + 1);
2041 selector_path_prepend_type (path, scanner->value.v_identifier);
2043 else if (_gtk_style_context_check_region_name (scanner->value.v_identifier))
2045 GtkRegionFlags flags = 0;
2048 region_name = g_strdup (scanner->value.v_identifier);
2050 if (g_scanner_peek_next_token (scanner) == ':')
2052 ParserSymbol symbol;
2054 g_scanner_get_next_token (scanner);
2055 gtk_css_scanner_push_scope (scanner, SCOPE_PSEUDO_CLASS);
2057 /* Check for the next token being nth-child, parse in that
2058 * case, and fallback into common state parsing if not.
2060 if (g_scanner_peek_next_token (scanner) != G_TOKEN_SYMBOL)
2061 return G_TOKEN_SYMBOL;
2063 symbol = GPOINTER_TO_INT (scanner->next_value.v_symbol);
2065 if (symbol == SYMBOL_FIRST_CHILD ||
2066 symbol == SYMBOL_LAST_CHILD ||
2067 symbol == SYMBOL_NTH_CHILD ||
2068 symbol == SYMBOL_SORTED_CHILD)
2072 if ((token = parse_nth_child (css_provider, scanner, &flags)) != G_TOKEN_NONE)
2075 gtk_css_scanner_pop_scope (scanner);
2079 gtk_css_scanner_pop_scope (scanner);
2080 selector_path_prepend_region (path, region_name, 0);
2081 g_free (region_name);
2086 selector_path_prepend_region (path, region_name, flags);
2087 g_free (region_name);
2089 else if (scanner->value.v_identifier[0] == '*')
2090 selector_path_prepend_glob (path);
2092 return G_TOKEN_IDENTIFIER;
2094 g_scanner_get_next_token (scanner);
2096 if (scanner->token == '>')
2098 selector_path_prepend_combinator (path, COMBINATOR_CHILD);
2099 g_scanner_get_next_token (scanner);
2103 if (scanner->token == ':')
2105 /* Add glob selector if path is empty */
2106 if (selector_path_depth (path) == 0)
2107 selector_path_prepend_glob (path);
2109 gtk_css_scanner_push_scope (scanner, SCOPE_PSEUDO_CLASS);
2111 while (scanner->token == ':')
2115 if ((token = parse_pseudo_class (css_provider, scanner, path)) != G_TOKEN_NONE)
2118 g_scanner_get_next_token (scanner);
2121 gtk_css_scanner_pop_scope (scanner);
2124 return G_TOKEN_NONE;
2128 resolve_binding_sets (const gchar *value_str,
2132 gchar **bindings, **str;
2134 bindings = g_strsplit (value_str, ",", -1);
2135 array = g_ptr_array_new ();
2137 for (str = bindings; *str; str++)
2139 GtkBindingSet *binding_set;
2141 binding_set = gtk_binding_set_find (g_strstrip (*str));
2146 g_ptr_array_add (array, binding_set);
2149 g_value_take_boxed (value, array);
2150 g_strfreev (bindings);
2154 parse_rule (GtkCssProvider *css_provider,
2157 GtkCssScannerPrivate *priv;
2158 GTokenType expected_token;
2159 SelectorPath *selector;
2161 priv = scanner->user_data;
2163 gtk_css_scanner_push_scope (scanner, SCOPE_SELECTOR);
2165 /* Handle directives */
2166 if (scanner->token == G_TOKEN_IDENTIFIER &&
2167 scanner->value.v_identifier[0] == '@')
2171 directive = &scanner->value.v_identifier[1];
2173 if (strcmp (directive, "define-color") == 0)
2175 GtkSymbolicColor *color;
2176 gchar *color_name, *color_str;
2177 GError *error = NULL;
2179 /* Directive is a color mapping */
2180 g_scanner_get_next_token (scanner);
2182 if (scanner->token != G_TOKEN_IDENTIFIER)
2184 gtk_css_provider_invalid_token (css_provider, scanner, "Color name");
2185 return G_TOKEN_IDENTIFIER;
2188 color_name = g_strdup (scanner->value.v_identifier);
2189 gtk_css_scanner_push_scope (scanner, SCOPE_VALUE);
2190 g_scanner_get_next_token (scanner);
2192 if (scanner->token != G_TOKEN_IDENTIFIER)
2194 gtk_css_provider_invalid_token (css_provider, scanner, "Color definition");
2195 return G_TOKEN_IDENTIFIER;
2198 color_str = g_strstrip (scanner->value.v_identifier);
2199 color = _gtk_css_parse_symbolic_color (color_str, &error);
2202 gtk_css_provider_take_error (css_provider, scanner, error);
2203 return G_TOKEN_IDENTIFIER;
2206 g_hash_table_insert (css_provider->priv->symbolic_colors, color_name, color);
2208 gtk_css_scanner_pop_scope (scanner);
2209 g_scanner_get_next_token (scanner);
2211 if (scanner->token != ';')
2214 return G_TOKEN_NONE;
2216 else if (strcmp (directive, "import") == 0)
2220 GFile *base, *actual;
2222 GError *error = NULL;
2224 gtk_css_scanner_push_scope (scanner, SCOPE_VALUE);
2225 g_scanner_get_next_token (scanner);
2227 if (scanner->token == G_TOKEN_IDENTIFIER &&
2228 g_str_has_prefix (scanner->value.v_identifier, "url"))
2229 path = g_strstrip (scanner->value.v_identifier);
2230 else if (scanner->token == G_TOKEN_STRING)
2231 path = g_strstrip (scanner->value.v_string);
2234 gtk_css_provider_invalid_token (css_provider, scanner, "File URL");
2235 return G_TOKEN_IDENTIFIER;
2238 if (scanner->input_name)
2239 dirname = g_path_get_dirname (scanner->input_name);
2241 dirname = g_get_current_dir ();
2243 base = g_file_new_for_path (dirname);
2246 actual = _gtk_css_parse_url (base, path, NULL, &error);
2247 g_object_unref (base);
2251 gtk_css_provider_take_error (css_provider, scanner, error);
2252 return G_TOKEN_IDENTIFIER;
2255 gtk_css_scanner_pop_scope (scanner);
2256 g_scanner_get_next_token (scanner);
2258 if (scanner->token != ';')
2260 g_object_unref (actual);
2264 path = g_file_get_path (actual);
2265 g_object_unref (actual);
2267 /* FIXME: Avoid recursive importing */
2268 loaded = gtk_css_provider_load_from_path_internal (css_provider, path,
2271 /* Restore previous state */
2275 return G_TOKEN_IDENTIFIER;
2277 return G_TOKEN_NONE;
2279 else if (strcmp (directive, "binding-set") == 0)
2281 GtkBindingSet *binding_set;
2282 gchar *binding_set_name;
2284 g_scanner_get_next_token (scanner);
2286 if (scanner->token != G_TOKEN_IDENTIFIER)
2288 gtk_css_provider_invalid_token (css_provider, scanner, "Binding name");
2289 return G_TOKEN_IDENTIFIER;
2292 binding_set_name = scanner->value.v_identifier;
2293 binding_set = gtk_binding_set_find (binding_set_name);
2297 binding_set = gtk_binding_set_new (binding_set_name);
2298 binding_set->parsed = TRUE;
2301 g_scanner_get_next_token (scanner);
2303 if (scanner->token != G_TOKEN_LEFT_CURLY)
2304 return G_TOKEN_LEFT_CURLY;
2306 gtk_css_scanner_push_scope (scanner, SCOPE_BINDING_SET);
2307 g_scanner_get_next_token (scanner);
2313 if (scanner->token != G_TOKEN_IDENTIFIER)
2315 gtk_css_provider_invalid_token (css_provider, scanner, "Binding definition");
2316 return G_TOKEN_IDENTIFIER;
2319 ret = gtk_binding_entry_add_signal_from_string (binding_set,
2320 scanner->value.v_identifier);
2321 if (ret != G_TOKEN_NONE)
2323 gtk_css_provider_invalid_token (css_provider, scanner, "Binding definition");
2327 g_scanner_get_next_token (scanner);
2329 if (scanner->token != ';')
2332 g_scanner_get_next_token (scanner);
2334 while (scanner->token != G_TOKEN_RIGHT_CURLY);
2336 gtk_css_scanner_pop_scope (scanner);
2337 g_scanner_get_next_token (scanner);
2339 return G_TOKEN_NONE;
2343 gtk_css_provider_invalid_token (css_provider, scanner, "Directive");
2344 return G_TOKEN_IDENTIFIER;
2348 expected_token = parse_selector (css_provider, scanner, &selector);
2350 if (expected_token != G_TOKEN_NONE)
2352 selector_path_unref (selector);
2353 gtk_css_provider_invalid_token (css_provider, scanner, "Selector");
2354 return expected_token;
2357 priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
2359 while (scanner->token == ',')
2361 g_scanner_get_next_token (scanner);
2363 expected_token = parse_selector (css_provider, scanner, &selector);
2365 if (expected_token != G_TOKEN_NONE)
2367 selector_path_unref (selector);
2368 gtk_css_provider_invalid_token (css_provider, scanner, "Selector");
2369 return expected_token;
2372 priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
2375 gtk_css_scanner_pop_scope (scanner);
2377 if (scanner->token != G_TOKEN_LEFT_CURLY)
2378 return G_TOKEN_LEFT_CURLY;
2380 /* Declarations parsing */
2381 gtk_css_scanner_push_scope (scanner, SCOPE_DECLARATION);
2383 g_scanner_get_next_token (scanner);
2385 while (scanner->token != G_TOKEN_RIGHT_CURLY &&
2386 !g_scanner_eof (scanner))
2388 gchar *value_str = NULL;
2389 GtkStylePropertyParser parse_func = NULL;
2390 GParamSpec *pspec = NULL;;
2393 if (scanner->token == ';')
2395 g_scanner_get_next_token (scanner);
2399 if (scanner->token != G_TOKEN_IDENTIFIER)
2401 gtk_css_provider_error_literal (css_provider,
2403 GTK_CSS_PROVIDER_ERROR,
2404 GTK_CSS_PROVIDER_ERROR_PROPERTY_NAME,
2405 "Expected a valid property name");
2406 goto find_end_of_declaration;
2409 prop = g_strdup (scanner->value.v_identifier);
2411 if (!gtk_style_properties_lookup_property (prop, &parse_func, &pspec) &&
2414 gtk_css_provider_error (css_provider,
2416 GTK_CSS_PROVIDER_ERROR,
2417 GTK_CSS_PROVIDER_ERROR_PROPERTY_NAME,
2418 "'%s' is not a valid property name",
2421 goto find_end_of_declaration;
2424 g_scanner_get_next_token (scanner);
2426 if (scanner->token != ':')
2429 gtk_css_provider_invalid_token (css_provider, scanner, "':'");
2430 goto find_end_of_declaration;
2433 gtk_css_scanner_push_scope (scanner, SCOPE_VALUE);
2434 g_scanner_get_next_token (scanner);
2436 if (scanner->token != G_TOKEN_IDENTIFIER)
2439 /* the error value here is hacky. But strings should be used for
2440 * strings really, so a string is not a syntax error but a broken
2441 * value for everything that we support. */
2442 gtk_css_provider_error (css_provider,
2444 GTK_CSS_PROVIDER_ERROR,
2445 scanner->token == G_TOKEN_STRING ? GTK_CSS_PROVIDER_ERROR_PROPERTY_VALUE
2446 : GTK_CSS_PROVIDER_ERROR_SYNTAX,
2447 "Not a property value");
2448 gtk_css_scanner_pop_scope (scanner);
2449 goto find_end_of_declaration;
2452 value_str = scanner->value.v_identifier;
2453 g_strchomp (value_str);
2455 gtk_css_scanner_pop_scope (scanner);
2456 g_scanner_peek_next_token (scanner);
2458 if (scanner->next_token != ';' &&
2459 scanner->next_token != G_TOKEN_RIGHT_CURLY &&
2460 scanner->next_token != G_TOKEN_EOF)
2462 gtk_css_provider_invalid_token (css_provider, scanner, "';'");
2464 goto find_end_of_declaration;
2471 val = g_slice_new0 (GValue);
2472 g_value_init (val, pspec->value_type);
2474 if (strcmp (value_str, "none") == 0)
2476 /* Insert the default value, so it has an opportunity
2477 * to override other style providers when merged
2479 g_param_value_set_default (pspec, val);
2480 g_hash_table_insert (priv->cur_properties, prop, val);
2482 else if (strcmp (prop, "gtk-key-bindings") == 0)
2484 /* Private property holding the binding sets */
2485 resolve_binding_sets (value_str, val);
2486 g_hash_table_insert (priv->cur_properties, prop, val);
2488 else if (pspec->value_type == G_TYPE_STRING)
2490 g_value_set_string (val, value_str);
2491 g_hash_table_insert (priv->cur_properties, prop, val);
2493 else if (parse_func)
2495 GError *error = NULL;
2497 if ((*parse_func) (value_str, val, &error))
2498 g_hash_table_insert (priv->cur_properties, prop, val);
2500 gtk_css_provider_take_error (css_provider, scanner, error);
2504 GError *error = NULL;
2509 if (scanner->input_name)
2510 dirname = g_path_get_dirname (scanner->input_name);
2512 dirname = g_get_current_dir ();
2514 base = g_file_new_for_path (dirname);
2517 success = _gtk_css_value_from_string (val,
2522 g_object_unref (base);
2526 g_hash_table_insert (priv->cur_properties, prop, val);
2530 g_value_unset (val);
2531 g_slice_free (GValue, val);
2534 gtk_css_provider_take_error (css_provider, scanner, error);
2538 else if (prop[0] == '-')
2542 val = g_slice_new0 (GValue);
2543 g_value_init (val, G_TYPE_STRING);
2544 g_value_set_string (val, value_str);
2546 g_hash_table_insert (priv->cur_properties, prop, val);
2551 g_scanner_get_next_token (scanner);
2553 if (g_scanner_eof (scanner))
2555 gtk_css_provider_invalid_token (css_provider, scanner, "}");
2559 find_end_of_declaration:
2560 while (scanner->token != ';' &&
2561 scanner->token != G_TOKEN_RIGHT_CURLY &&
2562 !g_scanner_eof (scanner))
2563 g_scanner_get_next_token (scanner);
2566 gtk_css_scanner_pop_scope (scanner);
2568 return G_TOKEN_NONE;
2572 gtk_css_provider_reset (GtkCssProvider *css_provider)
2574 GtkCssProviderPrivate *priv;
2576 priv = css_provider->priv;
2578 if (priv->selectors_info->len > 0)
2579 g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
2583 gtk_css_provider_propagate_error (GtkCssProvider *provider,
2587 const GError *error,
2588 GError **propagate_to)
2590 /* we already set an error. And we'd like to keep the first one */
2594 *propagate_to = g_error_copy (error);
2595 g_prefix_error (propagate_to, "%s:%u:%u: ", path ? path : "<unknown>", line, position);
2599 parse_stylesheet (GtkCssProvider *css_provider,
2603 gulong error_handler;
2606 error_handler = g_signal_connect (css_provider,
2608 G_CALLBACK (gtk_css_provider_propagate_error),
2611 error_handler = 0; /* silence gcc */
2613 g_scanner_get_next_token (scanner);
2615 while (!g_scanner_eof (scanner))
2617 GTokenType expected_token;
2619 expected_token = parse_rule (css_provider, scanner);
2621 if (expected_token != G_TOKEN_NONE)
2623 while (!g_scanner_eof (scanner) &&
2624 scanner->token != G_TOKEN_RIGHT_CURLY)
2625 g_scanner_get_next_token (scanner);
2628 css_provider_commit (css_provider, scanner);
2630 g_scanner_get_next_token (scanner);
2632 gtk_css_scanner_reset (scanner);
2637 g_signal_handler_disconnect (css_provider, error_handler);
2641 /* We clear all contents from the provider for backwards compat reasons */
2642 gtk_css_provider_reset (css_provider);
2651 * gtk_css_provider_load_from_data:
2652 * @css_provider: a #GtkCssProvider
2653 * @data: CSS data loaded in memory
2654 * @length: the length of @data in bytes, or -1 for NUL terminated strings
2655 * @error: (out) (allow-none): return location for a #GError, or %NULL
2657 * Loads @data into @css_provider, making it clear any previously loaded
2660 * Returns: %TRUE if the data could be loaded.
2663 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2671 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2672 g_return_val_if_fail (data != NULL, FALSE);
2675 length = strlen (data);
2677 gtk_css_provider_reset (css_provider);
2679 scanner = gtk_css_scanner_new (NULL, data, length);
2681 result = parse_stylesheet (css_provider, scanner, error);
2683 gtk_css_scanner_destroy (scanner);
2689 * gtk_css_provider_load_from_file:
2690 * @css_provider: a #GtkCssProvider
2691 * @file: #GFile pointing to a file to load
2692 * @error: (out) (allow-none): return location for a #GError, or %NULL
2694 * Loads the data contained in @file into @css_provider, making it
2695 * clear any previously loaded information.
2697 * Returns: %TRUE if the data could be loaded.
2700 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2704 GError *internal_error = NULL;
2710 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2711 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2713 if (!g_file_load_contents (file, NULL,
2715 NULL, &internal_error))
2717 g_propagate_error (error, internal_error);
2721 gtk_css_provider_reset (css_provider);
2723 scanner = gtk_css_scanner_new (file, data, length);
2725 ret = parse_stylesheet (css_provider, scanner, error);
2728 gtk_css_scanner_destroy (scanner);
2734 gtk_css_provider_load_from_path_internal (GtkCssProvider *css_provider,
2739 GtkCssProviderPrivate *priv;
2740 GError *internal_error = NULL;
2741 GMappedFile *mapped_file;
2748 priv = css_provider->priv;
2750 mapped_file = g_mapped_file_new (path, FALSE, &internal_error);
2754 g_propagate_error (error, internal_error);
2758 length = g_mapped_file_get_length (mapped_file);
2759 data = g_mapped_file_get_contents (mapped_file);
2765 gtk_css_provider_reset (css_provider);
2767 file = g_file_new_for_path (path);
2768 scanner = gtk_css_scanner_new (file, data, length);
2769 g_object_unref (file);
2771 ret = parse_stylesheet (css_provider, scanner, error);
2773 gtk_css_scanner_destroy (scanner);
2775 g_mapped_file_unref (mapped_file);
2781 * gtk_css_provider_load_from_path:
2782 * @css_provider: a #GtkCssProvider
2783 * @path: the path of a filename to load, in the GLib filename encoding
2784 * @error: (out) (allow-none): return location for a #GError, or %NULL
2786 * Loads the data contained in @path into @css_provider, making it clear
2787 * any previously loaded information.
2789 * Returns: %TRUE if the data could be loaded.
2792 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2796 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2797 g_return_val_if_fail (path != NULL, FALSE);
2799 return gtk_css_provider_load_from_path_internal (css_provider, path,
2804 * gtk_css_provider_get_default:
2806 * Returns the provider containing the style settings used as a
2807 * fallback for all widgets.
2809 * Returns: (transfer none): The provider used for fallback styling.
2810 * This memory is owned by GTK+, and you must not free it.
2813 gtk_css_provider_get_default (void)
2815 static GtkCssProvider *provider;
2817 if (G_UNLIKELY (!provider))
2820 "@define-color fg_color #000; \n"
2821 "@define-color bg_color #dcdad5; \n"
2822 "@define-color text_color #000; \n"
2823 "@define-color base_color #fff; \n"
2824 "@define-color selected_bg_color #4b6983; \n"
2825 "@define-color selected_fg_color #fff; \n"
2826 "@define-color tooltip_bg_color #eee1b3; \n"
2827 "@define-color tooltip_fg_color #000; \n"
2828 "@define-color placeholder_text_color #808080; \n"
2830 "@define-color info_fg_color rgb (181, 171, 156);\n"
2831 "@define-color info_bg_color rgb (252, 252, 189);\n"
2832 "@define-color warning_fg_color rgb (173, 120, 41);\n"
2833 "@define-color warning_bg_color rgb (250, 173, 61);\n"
2834 "@define-color question_fg_color rgb (97, 122, 214);\n"
2835 "@define-color question_bg_color rgb (138, 173, 212);\n"
2836 "@define-color error_fg_color rgb (166, 38, 38);\n"
2837 "@define-color error_bg_color rgb (237, 54, 54);\n"
2840 " background-color: @bg_color;\n"
2841 " color: @fg_color;\n"
2842 " border-color: shade (@bg_color, 0.6);\n"
2844 " border-width: 0;\n"
2848 " background-color: shade (@bg_color, 1.05);\n"
2849 " color: shade (@fg_color, 1.3);\n"
2853 " background-color: @selected_bg_color;\n"
2854 " color: @selected_fg_color;\n"
2857 ".expander, GtkTreeView.view.expander {\n"
2861 ".expander:prelight,\n"
2862 "GtkTreeView.view.expander:selected:prelight {\n"
2863 " color: @text_color;\n"
2866 ".expander:active {\n"
2867 " transition: 200ms linear;\n"
2871 " border-color: shade (@bg_color, 0.7);\n"
2872 " background-color: shade (@bg_color, 0.9);\n"
2873 " color: shade (@bg_color, 0.7);\n"
2877 " border-width: 0;\n"
2878 " border-radius: 0;\n"
2879 " background-color: @base_color;\n"
2880 " color: @text_color;\n"
2882 ".view:selected {\n"
2883 " background-color: shade (@bg_color, 0.9);\n"
2884 " color: @fg_color;\n"
2887 ".view:selected:focused {\n"
2888 " background-color: @selected_bg_color;\n"
2889 " color: @selected_fg_color;\n"
2892 ".view column:sorted row,\n"
2893 ".view column:sorted row:prelight {\n"
2894 " background-color: shade (@bg_color, 0.85);\n"
2897 ".view column:sorted row:nth-child(odd),\n"
2898 ".view column:sorted row:nth-child(odd):prelight {\n"
2899 " background-color: shade (@bg_color, 0.8);\n"
2903 ".view row:prelight {\n"
2904 " background-color: @base_color;\n"
2905 " color: @text_color;\n"
2908 ".view row:nth-child(odd),\n"
2909 ".view row:nth-child(odd):prelight {\n"
2910 " background-color: shade (@base_color, 0.93); \n"
2913 ".view row:selected:focused {\n"
2914 " background-color: @selected_bg_color;\n"
2917 ".view row:selected {\n"
2918 " background-color: darker (@bg_color);\n"
2919 " color: @selected_fg_color;\n"
2922 ".view.cell.trough,\n"
2923 ".view.cell.trough:hover,\n"
2924 ".view.cell.trough:selected,\n"
2925 ".view.cell.trough:selected:focused {\n"
2926 " background-color: @bg_color;\n"
2927 " color: @fg_color;\n"
2930 ".view.cell.progressbar,\n"
2931 ".view.cell.progressbar:hover,\n"
2932 ".view.cell.progressbar:selected,\n"
2933 ".view.cell.progressbar:selected:focused {\n"
2934 " background-color: @selected_bg_color;\n"
2935 " color: @selected_fg_color;\n"
2939 " background-color: alpha (@fg_color, 0.25);\n"
2940 " border-color: @fg_color;\n"
2941 " border-style: solid;\n"
2942 " border-width: 1;\n"
2946 " background-color: @tooltip_bg_color; \n"
2947 " color: @tooltip_fg_color; \n"
2948 " border-color: @tooltip_fg_color; \n"
2949 " border-width: 1;\n"
2950 " border-style: solid;\n"
2955 " border-style: outset; \n"
2956 " border-width: 2; \n"
2959 ".button:active {\n"
2960 " background-color: shade (@bg_color, 0.7);\n"
2961 " border-style: inset; \n"
2964 ".button:prelight,\n"
2965 ".slider:prelight {\n"
2966 " background-color: @selected_bg_color;\n"
2967 " color: @selected_fg_color;\n"
2968 " border-color: shade (@selected_bg_color, 0.7);\n"
2972 " background-color: darker (@bg_color);\n"
2973 " border-style: inset;\n"
2974 " border-width: 1;\n"
2979 " border-style: inset;\n"
2980 " border-width: 2;\n"
2981 " background-color: @base_color;\n"
2982 " color: @text_color;\n"
2985 ".entry:insensitive {\n"
2986 " background-color: shade (@base_color, 0.9);\n"
2987 " color: shade (@base_color, 0.7);\n"
2990 " background-color: #c4c2bd;\n"
2995 ".entry.progressbar, \n"
2996 ".cell.progressbar {\n"
2997 " background-color: @selected_bg_color;\n"
2998 " border-color: shade (@selected_bg_color, 0.7);\n"
2999 " color: @selected_fg_color;\n"
3000 " border-style: outset;\n"
3001 " border-width: 1;\n"
3004 "GtkCheckButton:hover,\n"
3005 "GtkCheckButton:selected,\n"
3006 "GtkRadioButton:hover,\n"
3007 "GtkRadioButton:selected {\n"
3008 " background-color: shade (@bg_color, 1.05);\n"
3012 ".cell.check, .cell.radio,\n"
3013 ".cell.check:hover, .cell.radio:hover {\n"
3014 " border-style: solid;\n"
3015 " border-width: 1;\n"
3016 " background-color: @base_color;\n"
3017 " border-color: @fg_color;\n"
3020 ".check:active, .radio:active,\n"
3021 ".check:hover, .radio:hover {\n"
3022 " background-color: @base_color;\n"
3023 " border-color: @fg_color;\n"
3024 " color: @text_color;\n"
3027 ".check:selected, .radio:selected {\n"
3028 " background-color: darker (@bg_color);\n"
3029 " color: @selected_fg_color;\n"
3030 " border-color: @selected_fg_color;\n"
3033 ".check:selected:focused, .radio:selected:focused {\n"
3034 " background-color: @selected_bg_color;\n"
3037 ".menu.check, .menu.radio {\n"
3038 " color: @fg_color;\n"
3039 " border-style: none;\n"
3040 " border-width: 0;\n"
3044 " border-style: outset;\n"
3045 " border-width: 1;\n"
3049 " border-style: inset;\n"
3050 " border-width: 2;\n"
3054 " border-style: outset;\n"
3055 " border-width: 1;\n"
3059 " border-style: inset;\n"
3060 " border-width: 1;\n"
3063 "GtkScrolledWindow.frame {\n"
3070 " border-style: outset;\n"
3071 " border-width: 1;\n"
3076 ".menu.check:hover,\n"
3077 ".menu.radio:hover {\n"
3078 " background-color: @selected_bg_color;\n"
3079 " color: @selected_fg_color;\n"
3082 "GtkSpinButton.button {\n"
3083 " border-width: 1;\n"
3086 ".scale.slider:hover,\n"
3087 "GtkSpinButton.button:hover {\n"
3088 " background-color: shade (@bg_color, 1.05);\n"
3089 " border-color: shade (@bg_color, 0.8);\n"
3092 "GtkSwitch.trough:active {\n"
3093 " background-color: @selected_bg_color;\n"
3094 " color: @selected_fg_color;\n"
3097 "GtkToggleButton.button:inconsistent {\n"
3098 " border-style: outset;\n"
3099 " border-width: 1px;\n"
3100 " background-color: shade (@bg_color, 0.9);\n"
3101 " border-color: shade (@bg_color, 0.7);\n"
3104 "GtkLabel:selected {\n"
3105 " background-color: shade (@bg_color, 0.9);\n"
3108 "GtkLabel:selected:focused {\n"
3109 " background-color: @selected_bg_color;\n"
3112 ".spinner:active {\n"
3113 " transition: 750ms linear loop;\n"
3117 " background-color: @info_bg_color;\n"
3118 " color: @info_fg_color;\n"
3122 " background-color: @warning_bg_color;\n"
3123 " color: @warning_fg_color;\n"
3127 " background-color: @question_bg_color;\n"
3128 " color: @question_fg_color;\n"
3132 " background-color: @error_bg_color;\n"
3133 " color: @error_fg_color;\n"
3137 " background-color: @selected_bg_color;\n"
3138 " color: @selected_fg_color;\n"
3141 ".light-area-focus {\n"
3145 ".dark-area-focus {\n"
3148 "GtkCalendar.view {\n"
3149 " border-width: 1;\n"
3150 " border-style: inset;\n"
3154 "GtkCalendar.view:inconsistent {\n"
3155 " color: darker (@bg_color);\n"
3158 "GtkCalendar.header {\n"
3159 " background-color: @bg_color;\n"
3160 " border-style: outset;\n"
3161 " border-width: 2;\n"
3164 "GtkCalendar.highlight {\n"
3165 " border-width: 0;\n"
3168 "GtkCalendar.button {\n"
3169 " background-color: @bg_color;\n"
3172 "GtkCalendar.button:hover {\n"
3173 " background-color: lighter (@bg_color);\n"
3174 " color: @fg_color;\n"
3178 " border-width: 0;\n"
3183 provider = gtk_css_provider_new ();
3184 if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
3186 g_error ("Failed to load the internal default CSS.");
3194 _gtk_css_provider_get_theme_dir (void)
3199 var = g_getenv ("GTK_DATA_PREFIX");
3202 path = g_build_filename (var, "share", "themes", NULL);
3204 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
3210 * gtk_css_provider_get_named:
3211 * @name: A theme name
3212 * @variant: (allow-none): variant to load, for example, "dark", or
3213 * %NULL for the default
3215 * Loads a theme from the usual theme paths
3217 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
3218 * This memory is owned by GTK+, and you must not free it.
3221 gtk_css_provider_get_named (const gchar *name,
3222 const gchar *variant)
3224 static GHashTable *themes = NULL;
3225 GtkCssProvider *provider;
3228 if (G_UNLIKELY (!themes))
3229 themes = g_hash_table_new (g_str_hash, g_str_equal);
3231 if (variant == NULL)
3232 key = (gchar *)name;
3234 key = g_strconcat (name, "-", variant, NULL);
3236 provider = g_hash_table_lookup (themes, key);
3240 const gchar *home_dir;
3241 gchar *subpath, *path = NULL;
3244 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
3246 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
3248 /* First look in the users home directory
3250 home_dir = g_get_home_dir ();
3253 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
3255 if (!g_file_test (path, G_FILE_TEST_EXISTS))
3266 theme_dir = _gtk_css_provider_get_theme_dir ();
3267 path = g_build_filename (theme_dir, name, subpath, NULL);
3270 if (!g_file_test (path, G_FILE_TEST_EXISTS))
3283 provider = gtk_css_provider_new ();
3285 if (!gtk_css_provider_load_from_path (provider, path, &error))
3287 g_warning ("Could not load named theme \"%s\": %s", name, error->message);
3288 g_error_free (error);
3290 g_object_unref (provider);
3294 g_hash_table_insert (themes, g_strdup (key), provider);
3307 selector_path_print (const SelectorPath *path,
3310 GSList *walk, *reverse;
3312 reverse = g_slist_copy (path->elements);
3313 reverse = g_slist_reverse (reverse);
3315 for (walk = reverse; walk; walk = walk->next)
3317 SelectorElement *elem = walk->data;
3319 switch (elem->elem_type)
3321 case SELECTOR_TYPE_NAME:
3323 g_string_append (str, g_quark_to_string (elem->name));
3325 case SELECTOR_GTYPE:
3326 g_string_append (str, g_type_name (elem->type));
3328 case SELECTOR_REGION:
3329 g_string_append (str, g_quark_to_string (elem->region.name));
3330 if (elem->region.flags)
3332 char * flag_names[] = {
3341 for (i = 0; i < G_N_ELEMENTS (flag_names); i++)
3343 if (elem->region.flags & (1 << i))
3345 g_string_append_c (str, ':');
3346 g_string_append (str, flag_names[i]);
3351 case SELECTOR_CLASS:
3352 g_string_append_c (str, '.');
3353 g_string_append (str, g_quark_to_string (elem->name));
3356 if (walk->next == NULL ||
3357 elem->combinator != COMBINATOR_CHILD ||
3358 ((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
3359 g_string_append (str, "*");
3362 g_assert_not_reached ();
3367 switch (elem->combinator)
3369 case COMBINATOR_DESCENDANT:
3370 if (elem->elem_type != SELECTOR_CLASS ||
3371 ((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
3372 g_string_append_c (str, ' ');
3374 case COMBINATOR_CHILD:
3375 if (((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
3376 g_string_append (str, " > ");
3379 g_assert_not_reached ();
3387 char * state_names[] = {
3397 for (i = 0; i < G_N_ELEMENTS (state_names); i++)
3399 if (path->state & (1 << i))
3401 g_string_append_c (str, ':');
3402 g_string_append (str, state_names[i]);
3407 g_slist_free (reverse);
3411 selector_style_info_print (const SelectorStyleInfo *info,
3417 selector_path_print (info->path, str);
3419 g_string_append (str, " {\n");
3421 keys = g_hash_table_get_keys (info->style);
3422 /* so the output is identical for identical selector styles */
3423 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3425 for (walk = keys; walk; walk = walk->next)
3427 const char *name = walk->data;
3428 const GValue *value = g_hash_table_lookup (info->style, (gpointer) name);
3430 g_string_append (str, " ");
3431 g_string_append (str, name);
3432 g_string_append (str, ": ");
3433 s = _gtk_css_value_to_string (value);
3434 g_string_append (str, s);
3436 g_string_append (str, ";\n");
3441 g_string_append (str, "}\n");
3445 gtk_css_provider_print_colors (GHashTable *colors,
3451 keys = g_hash_table_get_keys (colors);
3452 /* so the output is identical for identical styles */
3453 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3455 for (walk = keys; walk; walk = walk->next)
3457 const char *name = walk->data;
3458 GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
3460 g_string_append (str, "@define-color ");
3461 g_string_append (str, name);
3462 g_string_append (str, " ");
3463 s = gtk_symbolic_color_to_string (symbolic);
3464 g_string_append (str, s);
3466 g_string_append (str, ";\n");
3473 * gtk_css_provider_to_string:
3474 * @provider: the provider to write to a string
3476 * Convertes the @provider into a string representation in CSS
3479 * Using gtk_css_provider_load_from_data() with the return value
3480 * from this function on a new provider created with
3481 * gtk_css_provider_new() will basicallu create a duplicate of
3484 * Returns: a new string representing the @provider.
3487 gtk_css_provider_to_string (GtkCssProvider *provider)
3489 GtkCssProviderPrivate *priv;
3493 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
3495 priv = provider->priv;
3497 str = g_string_new ("");
3499 gtk_css_provider_print_colors (priv->symbolic_colors, str);
3501 for (i = 0; i < priv->selectors_info->len; i++)
3504 g_string_append (str, "\n");
3505 selector_style_info_print (g_ptr_array_index (priv->selectors_info, i),
3509 return g_string_free (str, FALSE);