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 "gtkcssparserprivate.h"
31 #include "gtkcssselectorprivate.h"
32 #include "gtksymboliccolor.h"
33 #include "gtkstyleprovider.h"
34 #include "gtkstylecontextprivate.h"
35 #include "gtkstylepropertiesprivate.h"
36 #include "gtkstylepropertyprivate.h"
37 #include "gtkbindings.h"
38 #include "gtkmarshalers.h"
39 #include "gtkprivate.h"
43 * SECTION:gtkcssprovider
44 * @Short_description: CSS-like styling for widgets
45 * @Title: GtkCssProvider
46 * @See_also: #GtkStyleContext, #GtkStyleProvider
48 * GtkCssProvider is an object implementing the #GtkStyleProvider interface.
49 * It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
50 * input in order to style widgets.
52 * <refsect2 id="gtkcssprovider-files">
53 * <title>Default files</title>
55 * An application can cause GTK+ to parse a specific CSS style sheet by
56 * calling gtk_css_provider_load_from_file() and adding the provider with
57 * gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
58 * In addition, certain files will be read when GTK+ is initialized. First,
59 * the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
60 * is loaded if it exists. Then, GTK+ tries to load
61 * <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
63 * <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
64 * where <replaceable>theme-name</replaceable> is the name of the current theme
65 * (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
66 * is the prefix configured when GTK+ was compiled, unless overridden by the
67 * <envar>GTK_DATA_PREFIX</envar> environment variable.
70 * <refsect2 id="gtkcssprovider-stylesheets">
71 * <title>Style sheets</title>
73 * The basic structure of the style sheets understood by this provider is
74 * a series of statements, which are either rule sets or '@-rules', separated
78 * A rule set consists of a selector and a declaration block, which is
79 * a series of declarations enclosed in curly braces ({ and }). The
80 * declarations are separated by semicolons (;). Multiple selectors can
81 * share the same declaration block, by putting all the separators in
82 * front of the block, separated by commas.
84 * <example><title>A rule set with two selectors</title>
85 * <programlisting language="text">
86 * GtkButton, GtkEntry {
93 * <refsect2 id="gtkcssprovider-selectors">
94 * <title>Selectors</title>
96 * Selectors work very similar to the way they do in CSS, with widget class
97 * names taking the role of element names, and widget names taking the role
98 * of IDs. When used in a selector, widget names must be prefixed with a
99 * '#' character. The '*' character represents the so-called universal
100 * selector, which matches any widget.
103 * To express more complicated situations, selectors can be combined in
106 * <listitem><para>To require that a widget satisfies several conditions,
107 * combine several selectors into one by concatenating them. E.g.
108 * <literal>GtkButton#button1</literal> matches a GtkButton widget
109 * with the name button1.</para></listitem>
110 * <listitem><para>To only match a widget when it occurs inside some other
111 * widget, write the two selectors after each other, separated by whitespace.
112 * E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
113 * that occur inside a GtkToolBar.</para></listitem>
114 * <listitem><para>In the previous example, the GtkButton is matched even
115 * if it occurs deeply nested inside the toolbar. To restrict the match
116 * to direct children of the parent widget, insert a '>' character between
117 * the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
118 * GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
122 * <title>Widget classes and names in selectors</title>
123 * <programlisting language="text">
124 * /* Theme labels that are descendants of a window */
125 * GtkWindow GtkLabel {
126 * background-color: #898989
129 * /* Theme notebooks, and anything that's within these */
131 * background-color: #a939f0
134 * /* Theme combo boxes, and entries that
135 * are direct children of a notebook */
137 * GtkNotebook > GtkEntry {
139 * background-color: #1209a2
142 * /* Theme any widget within a GtkBin */
147 * /* Theme a label named title-label */
148 * GtkLabel#title-label {
152 * /* Theme any widget named main-entry */
154 * background-color: #f0a810
159 * Widgets may also define style classes, which can be used for matching.
160 * When used in a selector, style classes must be prefixed with a '.'
164 * Refer to the documentation of individual widgets to learn which
165 * style classes they define and see <xref linkend="gtkstylecontext-classes"/>
166 * for a list of all style classes used by GTK+ widgets.
169 * Note that there is some ambiguity in the selector syntax when it comes
170 * to differentiation widget class names from regions. GTK+ currently treats
171 * a string as a widget class name if it contains any uppercase characters
172 * (which should work for more widgets with names like GtkLabel).
175 * <title>Style classes in selectors</title>
176 * <programlisting language="text">
177 * /* Theme all widgets defining the class entry */
179 * color: #39f1f9;
182 * /* Theme spinbuttons' entry */
183 * GtkSpinButton.entry {
189 * In complicated widgets like e.g. a GtkNotebook, it may be desirable
190 * to style different parts of the widget differently. To make this
191 * possible, container widgets may define regions, whose names
192 * may be used for matching in selectors.
195 * Some containers allow to further differentiate between regions by
196 * applying so-called pseudo-classes to the region. For example, the
197 * tab region in GtkNotebook allows to single out the first or last
198 * tab by using the :first-child or :last-child pseudo-class.
199 * When used in selectors, pseudo-classes must be prefixed with a
203 * Refer to the documentation of individual widgets to learn which
204 * regions and pseudo-classes they define and see
205 * <xref linkend="gtkstylecontext-classes"/> for a list of all regions
206 * used by GTK+ widgets.
209 * <title>Regions in selectors</title>
210 * <programlisting language="text">
211 * /* Theme any label within a notebook */
212 * GtkNotebook GtkLabel {
213 * color: #f90192;
216 * /* Theme labels within notebook tabs */
217 * GtkNotebook tab GtkLabel {
218 * color: #703910;
221 * /* Theme labels in the any first notebook
222 * tab, both selectors are equivalent */
223 * GtkNotebook tab:nth-child(first) GtkLabel,
224 * GtkNotebook tab:first-child GtkLabel {
225 * color: #89d012;
230 * Another use of pseudo-classes is to match widgets depending on their
231 * state. This is conceptually similar to the :hover, :active or :focus
232 * pseudo-classes in CSS. The available pseudo-classes for widget states
233 * are :active, :prelight (or :hover), :insensitive, :selected, :focused
237 * <title>Styling specific widget states</title>
238 * <programlisting language="text">
239 * /* Theme active (pressed) buttons */
241 * background-color: #0274d9;
244 * /* Theme buttons with the mouse pointer on it,
245 * both are equivalent */
247 * GtkButton:prelight {
248 * background-color: #3085a9;
251 * /* Theme insensitive widgets, both are equivalent */
254 * background-color: #320a91;
257 * /* Theme selection colors in entries */
258 * GtkEntry:selected {
259 * background-color: #56f9a0;
262 * /* Theme focused labels */
264 * background-color: #b4940f;
267 * /* Theme inconsistent checkbuttons */
268 * GtkCheckButton:inconsistent {
269 * background-color: #20395a;
274 * Widget state pseudoclasses may only apply to the last element
278 * To determine the effective style for a widget, all the matching rule
279 * sets are merged. As in CSS, rules apply by specificity, so the rules
280 * whose selectors more closely match a widget path will take precedence
284 * <refsect2 id="gtkcssprovider-rules">
285 * <title>@ Rules</title>
287 * GTK+'s CSS supports the @import rule, in order to load another
288 * CSS style sheet in addition to the currently parsed one.
291 * <title>Using the @import rule</title>
292 * <programlisting language="text">
293 * @import url ("path/to/common.css");
296 * <para id="css-binding-set">
297 * In order to extend key bindings affecting different widgets, GTK+
298 * supports the @binding-set rule to parse a set of bind/unbind
299 * directives, see #GtkBindingSet for the supported syntax. Note that
300 * the binding sets defined in this way must be associated with rule sets
301 * by setting the gtk-key-bindings style property.
304 * Customized key bindings are typically defined in a separate
305 * <filename>gtk-keys.css</filename> CSS file and GTK+ loads this file
306 * according to the current key theme, which is defined by the
307 * #GtkSettings:gtk-key-theme-name setting.
310 * <title>Using the @binding rule</title>
311 * <programlisting language="text">
312 * @binding-set binding-set1 {
313 * bind "<alt>Left" { "move-cursor" (visual-positions, -3, 0) };
317 * @binding-set binding-set2 {
318 * bind "<alt>Right" { "move-cursor" (visual-positions, 3, 0) };
319 * bind "<alt>KP_space" { "delete-from-cursor" (whitespace, 1)
320 * "insert-at-cursor" (" ") };
324 * gtk-key-bindings: binding-set1, binding-set2;
329 * GTK+ also supports an additional @define-color rule, in order
330 * to define a color name which may be used instead of color numeric
331 * representations. Also see the #GtkSettings:gtk-color-scheme setting
332 * for a way to override the values of these named colors.
335 * <title>Defining colors</title>
336 * <programlisting language="text">
337 * @define-color bg_color #f9a039;
340 * background-color: @bg_color;
345 * <refsect2 id="gtkcssprovider-symbolic-colors">
346 * <title>Symbolic colors</title>
348 * Besides being able to define color names, the CSS parser is also able
349 * to read different color expressions, which can also be nested, providing
350 * a rich language to define colors which are derived from a set of base
354 * <title>Using symbolic colors</title>
355 * <programlisting language="text">
356 * @define-color entry-color shade (@bg_color, 0.7);
359 * background-color: @entry-color;
363 * background-color: mix (@entry-color,
364 * shade (#fff, 0.5),
370 * The various ways to express colors in GTK+ CSS are:
376 * <entry>Syntax</entry>
377 * <entry>Explanation</entry>
378 * <entry>Examples</entry>
383 * <entry>rgb(@r, @g, @b)</entry>
384 * <entry>An opaque color; @r, @g, @b can be either integers between
385 * 0 and 255 or percentages</entry>
386 * <entry><literallayout>rgb(128, 10, 54)
387 * rgb(20%, 30%, 0%)</literallayout></entry>
390 * <entry>rgba(@r, @g, @b, @a)</entry>
391 * <entry>A translucent color; @r, @g, @b are as in the previous row,
392 * @a is a floating point number between 0 and 1</entry>
393 * <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
396 * <entry>#@xxyyzz</entry>
397 * <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
398 * specifying @r, @g, @b variants with between 1 and 4
399 * hexadecimal digits per component are allowed</entry>
400 * <entry><literallayout>#ff12ab
401 * #f0c</literallayout></entry>
404 * <entry>@name</entry>
405 * <entry>Reference to a color that has been defined with
406 * @define-color
408 * <entry>@bg_color</entry>
411 * <entry>mix(@color1, @color2, @f)</entry>
412 * <entry>A linear combination of @color1 and @color2. @f is a
413 * floating point number between 0 and 1.</entry>
414 * <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
417 * <entry>shade(@color, @f)</entry>
418 * <entry>A lighter or darker variant of @color. @f is a
419 * floating point number.
421 * <entry>shade(@fg_color, 0.5)</entry>
424 * <entry>lighter(@color)</entry>
425 * <entry>A lighter variant of @color</entry>
428 * <entry>darker(@color)</entry>
429 * <entry>A darker variant of @color</entry>
435 * <refsect2 id="gtkcssprovider-gradients">
436 * <title>Gradients</title>
438 * Linear or radial Gradients can be used as background images.
441 * A linear gradient along the line from (@start_x, @start_y) to
442 * (@end_x, @end_y) is specified using the syntax
443 * <literallayout>-gtk-gradient (linear,
444 * @start_x @start_y, @end_x @end_y,
445 * color-stop (@position, @color),
446 * ...)</literallayout>
447 * where @start_x and @end_x can be either a floating point number between
448 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
449 * and @end_y can be either a floating point number between 0 and 1 or one
450 * of the special values 'top', 'bottom' or 'center', @position is a floating
451 * point number between 0 and 1 and @color is a color expression (see above).
452 * The color-stop can be repeated multiple times to add more than one color
453 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
454 * color stops with position 0 and 1, respectively.
457 * <title>A linear gradient</title>
458 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
459 * <para>This gradient was specified with
460 * <literallayout>-gtk-gradient (linear,
461 * left top, right bottom,
462 * from(@yellow), to(@blue))</literallayout></para>
465 * <title>Another linear gradient</title>
466 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
467 * <para>This gradient was specified with
468 * <literallayout>-gtk-gradient (linear,
470 * color-stop(0, @yellow),
471 * color-stop(0.2, @blue),
472 * color-stop(1, #0f0))</literallayout></para>
475 * A radial gradient along the two circles defined by (@start_x, @start_y,
476 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
478 * <literallayout>-gtk-gradient (radial,
479 * @start_x @start_y, @start_radius,
480 * @end_x @end_y, @end_radius,
481 * color-stop (@position, @color),
482 * ...)</literallayout>
483 * where @start_radius and @end_radius are floating point numbers and
484 * the other parameters are as before.
487 * <title>A radial gradient</title>
488 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
489 * <para>This gradient was specified with
490 * <literallayout>-gtk-gradient (radial,
493 * from(@yellow), to(@green))</literallayout></para>
496 * <title>Another radial gradient</title>
497 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
498 * <para>This gradient was specified with
499 * <literallayout>-gtk-gradient (radial,
502 * color-stop (0, #f00),
503 * color-stop (0.1, #a0f),
504 * color-stop (0.2, @yellow),
505 * color-stop (1, @green))</literallayout></para>
508 * <refsect2 id="gtkcssprovider-shadows">
509 * <title>Text shadow</title>
511 * A shadow list can be applied to text or symbolic icons, using the CSS3
512 * text-shadow syntax, as defined in
513 * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
516 * A text shadow is specified using the syntax
517 * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
518 * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
519 * parameters. The optional blur radius is parsed, but it is currently not rendered by
520 * the GTK+ theming engine.
523 * To set multiple shadows on an element, you can specify a comma-separated list
524 * of shadow elements in the text-shadow property. Shadows are always rendered
525 * front-back, i.e. the first shadow specified is on top of the others. Shadows
526 * can thus overlay each other, but they can never overlay the text itself,
527 * which is always rendered on top of the shadow layer.
530 * <refsect2 id="gtkcssprovider-slices">
531 * <title>Border images</title>
533 * Images can be used in 'slices' for the purpose of creating scalable
536 * <inlinegraphic fileref="slices.png" format="PNG"/>
538 * The syntax for specifying border images of this kind is:
539 * <literallayout>url(@path) @top @right @bottom @left [repeat|stretch]? [repeat|stretch]?</literallayout>
540 * The sizes of the 'cut off' portions are specified
541 * with the @top, @right, @bottom and @left parameters.
542 * The 'middle' sections can be repeated or stretched to create
543 * the desired effect, by adding the 'repeat' or 'stretch' options after
544 * the dimensions. If two options are specified, the first one affects
545 * the horizontal behaviour and the second one the vertical behaviour.
546 * If only one option is specified, it affects both.
549 * <title>A border image</title>
550 * <inlinegraphic fileref="border1.png" format="PNG"/>
551 * <para>This border image was specified with
552 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
556 * <title>A repeating border image</title>
557 * <inlinegraphic fileref="border2.png" format="PNG"/>
558 * <para>This border image was specified with
559 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
563 * <title>A stretched border image</title>
564 * <inlinegraphic fileref="border3.png" format="PNG"/>
565 * <para>This border image was specified with
566 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
570 * <refsect2 id="gtkcssprovider-transitions">
571 * <para>Styles can specify transitions that will be used to create a gradual
572 * change in the appearance when a widget state changes. The following
573 * syntax is used to specify transitions:
574 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
575 * The @duration is the amount of time that the animation will take for
576 * a complete cycle from start to end. If the loop option is given, the
577 * animation will be repated until the state changes again.
578 * The option after the duration determines the transition function from a
579 * small set of predefined functions.
580 * <figure><title>Linear transition</title>
581 * <graphic fileref="linear.png" format="PNG"/>
583 * <figure><title>Ease transition</title>
584 * <graphic fileref="ease.png" format="PNG"/>
586 * <figure><title>Ease-in-out transition</title>
587 * <graphic fileref="ease-in-out.png" format="PNG"/>
589 * <figure><title>Ease-in transition</title>
590 * <graphic fileref="ease-in.png" format="PNG"/>
592 * <figure><title>Ease-out transition</title>
593 * <graphic fileref="ease-out.png" format="PNG"/>
597 * <refsect2 id="gtkcssprovider-properties">
598 * <title>Supported properties</title>
600 * Properties are the part that differ the most to common CSS,
601 * not all properties are supported (some are planned to be
602 * supported eventually, some others are meaningless or don't
603 * map intuitively in a widget based environment).
606 * There is also a difference in shorthand properties, for
607 * example in common CSS it is fine to define a font through
608 * the different @font-family, @font-style, @font-size
609 * properties, meanwhile in GTK+'s CSS only the canonical
610 * @font property is supported.
613 * The currently supported properties are:
619 * <entry>Property name</entry>
620 * <entry>Syntax</entry>
621 * <entry>Maps to</entry>
622 * <entry>Examples</entry>
627 * <entry>engine</entry>
628 * <entry>engine-name</entry>
629 * <entry>#GtkThemingEngine</entry>
630 * <entry>engine: clearlooks;
631 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
634 * <entry>background-color</entry>
635 * <entry morerows="2">color (see above)</entry>
636 * <entry morerows="2">#GdkRGBA</entry>
637 * <entry morerows="2"><literallayout>background-color: #fff;
638 * color: &color1;
639 * background-color: shade (&color1, 0.5);
640 * color: mix (&color1, #f0f, 0.8);</literallayout>
644 * <entry>color</entry>
647 * <entry>border-color</entry>
650 * <entry>font</entry>
651 * <entry>@family [@style] [@size]</entry>
652 * <entry>#PangoFontDescription</entry>
653 * <entry>font: Sans 15;</entry>
656 * <entry>margin-top</entry>
657 * <entry>integer</entry>
658 * <entry>#gint</entry>
659 * <entry>margin-top: 0;</entry>
662 * <entry>margin-left</entry>
663 * <entry>integer</entry>
664 * <entry>#gint</entry>
665 * <entry>margin-left: 1;</entry>
668 * <entry>margin-bottom</entry>
669 * <entry>integer</entry>
670 * <entry>#gint</entry>
671 * <entry>margin-bottom: 2;</entry>
674 * <entry>margin-right</entry>
675 * <entry>integer</entry>
676 * <entry>#gint</entry>
677 * <entry>margin-right: 4;</entry>
680 * <entry>margin</entry>
681 * <entry morerows="1"><literallayout>@width
682 * @vertical_width @horizontal_width
683 * @top_width @horizontal_width @bottom_width
684 * @top_width @right_width @bottom_width @left_width</literallayout>
686 * <entry morerows="1">#GtkBorder</entry>
687 * <entry morerows="1"><literallayout>margin: 5;
690 * margin: 5 10 3 5;</literallayout>
694 * <entry>padding-top</entry>
695 * <entry>integer</entry>
696 * <entry>#gint</entry>
697 * <entry>padding-top: 5;</entry>
700 * <entry>padding-left</entry>
701 * <entry>integer</entry>
702 * <entry>#gint</entry>
703 * <entry>padding-left: 5;</entry>
706 * <entry>padding-bottom</entry>
707 * <entry>integer</entry>
708 * <entry>#gint</entry>
709 * <entry>padding-bottom: 5;</entry>
712 * <entry>padding-right</entry>
713 * <entry>integer</entry>
714 * <entry>#gint</entry>
715 * <entry>padding-right: 5;</entry>
718 * <entry>padding</entry>
721 * <entry>background-image</entry>
722 * <entry><literallayout>gradient (see above) or
723 * url(@path)</literallayout></entry>
724 * <entry>#cairo_pattern_t</entry>
725 * <entry><literallayout>-gtk-gradient (linear,
726 * left top, right top,
727 * from (#fff), to (#000));
728 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
730 * color-stop (0.5, #f00),
732 * -gtk-gradient (radial,
733 * center center, 0.2,
734 * center center, 0.8,
735 * color-stop (0.0, #fff),
736 * color-stop (1.0, #000));
737 * url ('background.png');</literallayout>
741 * <entry>border-top-width</entry>
742 * <entry>integer</entry>
743 * <entry>#gint</entry>
744 * <entry>border-top-width: 5;</entry>
747 * <entry>border-left-width</entry>
748 * <entry>integer</entry>
749 * <entry>#gint</entry>
750 * <entry>border-left-width: 5;</entry>
753 * <entry>border-bottom-width</entry>
754 * <entry>integer</entry>
755 * <entry>#gint</entry>
756 * <entry>border-bottom-width: 5;</entry>
759 * <entry>border-right-width</entry>
760 * <entry>integer</entry>
761 * <entry>#gint</entry>
762 * <entry>border-right-width: 5;</entry>
765 * <entry>border-width</entry>
766 * <entry morerows="1">#GtkBorder</entry>
767 * <entry morerows="1"><literallayout>border-width: 1;
769 * border-width: 1 2 3;
770 * border-width: 1 2 3 5;</literallayout>
774 * <entry>border-radius</entry>
775 * <entry>integer</entry>
776 * <entry>#gint</entry>
777 * <entry>border-radius: 5;</entry>
780 * <entry>border-style</entry>
781 * <entry>[none|solid|inset|outset]</entry>
782 * <entry>#GtkBorderStyle</entry>
783 * <entry>border-style: solid;</entry>
786 * <entry>border-image</entry>
787 * <entry><literallayout>border image (see above)</literallayout></entry>
788 * <entry>internal use only</entry>
789 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
790 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
794 * <entry>text-shadow</entry>
795 * <entry>shadow list (see above)</entry>
796 * <entry>#GtkTextShadow</entry>
797 * <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
800 * <entry>transition</entry>
801 * <entry>transition (see above)</entry>
802 * <entry>internal use only</entry>
803 * <entry><literallayout>transition: 150ms ease-in-out;
804 * transition: 1s linear loop;</literallayout>
808 * <entry>gtk-key-bindings</entry>
809 * <entry>binding set name list</entry>
810 * <entry>internal use only</entry>
811 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
818 * GtkThemingEngines can register their own, engine-specific style properties
819 * with the function gtk_theming_engine_register_property(). These properties
820 * can be set in CSS like other properties, using a name of the form
821 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
822 * the name of the theming engine, and <replaceable>name</replaceable> is the
823 * name of the property. Style properties that have been registered by widgets
824 * using gtk_widget_class_install_style_property() can also be set in this
825 * way, using the widget class name for <replaceable>namespace</replaceable>.
828 * <title>Using engine-specific style properties</title>
831 * engine: clearlooks;
833 * -GtkPaned-handle-size: 6;
834 * -clearlooks-colorize-scrollbar: false;
841 typedef struct GtkCssRuleset GtkCssRuleset;
842 typedef struct _GtkCssScanner GtkCssScanner;
843 typedef enum ParserScope ParserScope;
844 typedef enum ParserSymbol ParserSymbol;
848 GtkCssSelector *selector;
849 GHashTable *widget_style;
852 guint has_inherit :1;
855 struct _GtkCssScanner
857 GtkCssProvider *provider;
858 GtkCssParser *parser;
859 GtkCssScanner *parent;
863 GSList *cur_selectors;
864 GHashTable *cur_properties;
867 struct _GtkCssProviderPrivate
871 GHashTable *symbolic_colors;
885 /* Extend GtkStateType, since these
886 * values are also used as symbols
889 /* Scope: pseudo-class */
890 SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
895 /* Scope: nth-child */
896 SYMBOL_NTH_CHILD_EVEN,
897 SYMBOL_NTH_CHILD_ODD,
898 SYMBOL_NTH_CHILD_FIRST,
899 SYMBOL_NTH_CHILD_LAST
907 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
909 static void gtk_css_provider_finalize (GObject *object);
910 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
913 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
914 GtkCssScanner *scanner,
921 gtk_css_provider_error_quark (void)
923 return g_quark_from_static_string ("gtk-css-provider-error-quark");
926 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
927 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
928 gtk_css_style_provider_iface_init));
931 gtk_css_provider_parsing_error (GtkCssProvider *provider,
935 const GError * error)
937 /* Only emit a warning when we have no error handlers. This is our
938 * default handlers. And in this case erroneous CSS files are a bug
939 * and should be fixed.
940 * Note that these warnings can also be triggered by a broken theme
941 * that people installed from some weird location on the internets.
943 if (!g_signal_has_handler_pending (provider,
944 css_provider_signals[PARSING_ERROR],
948 g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
953 gtk_css_provider_class_init (GtkCssProviderClass *klass)
955 GObjectClass *object_class = G_OBJECT_CLASS (klass);
958 * GtkCssProvider::parsing-error:
959 * @provider: the provider that had a parsing error
960 * @path: path to the parsed file or %NULL if the file cannot be
961 * identified or the data was not loaded from a file
962 * @line: line in the file or data or 0 if unknown
963 * @position: offset into the current line or 0 if unknown or the
964 * whole line is affected
965 * @error: The parsing error
967 * Signals that a parsing error occured. the @path, @line and @position
968 * describe the actual location of the error as accurately as possible.
970 * Parsing errors are never fatal, so the parsing will resume after
971 * the error. Errors may however cause parts of the given
972 * data or even all of it to not be parsed at all. So it is a useful idea
973 * to check that the parsing succeeds by connecting to this signal.
975 * Note that this signal may be emitted at any time as the css provider
976 * may opt to defer parsing parts or all of the input to a later time
977 * than when a loading function was called.
979 css_provider_signals[PARSING_ERROR] =
980 g_signal_new (I_("parsing-error"),
981 G_TYPE_FROM_CLASS (object_class),
983 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
985 _gtk_marshal_VOID__STRING_UINT_UINT_BOXED,
987 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR);
989 object_class->finalize = gtk_css_provider_finalize;
991 klass->parsing_error = gtk_css_provider_parsing_error;
993 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
997 gtk_css_provider_take_error_full (GtkCssProvider *provider,
1006 filename = g_file_get_path (file);
1010 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1011 filename, line, position, error);
1014 g_error_free (error);
1018 gtk_css_ruleset_init_copy (GtkCssRuleset *new,
1019 const GtkCssRuleset *ruleset,
1020 GtkCssSelector *selector)
1022 memcpy (new, ruleset, sizeof (GtkCssRuleset));
1024 new->selector = selector;
1025 if (new->widget_style)
1026 g_hash_table_ref (new->widget_style);
1028 g_hash_table_ref (new->style);
1032 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
1035 g_hash_table_unref (ruleset->style);
1036 if (ruleset->widget_style)
1037 g_hash_table_unref (ruleset->widget_style);
1038 if (ruleset->selector)
1039 _gtk_css_selector_free (ruleset->selector);
1041 memset (ruleset, 0, sizeof (GtkCssRuleset));
1045 property_value_free (GValue *value)
1047 if (G_IS_VALUE (value))
1048 g_value_unset (value);
1050 g_slice_free (GValue, value);
1054 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
1058 if (ruleset->widget_style == NULL)
1059 ruleset->widget_style = g_hash_table_new_full (g_str_hash,
1061 (GDestroyNotify) g_free,
1062 (GDestroyNotify) property_value_free);
1064 g_hash_table_insert (ruleset->widget_style, name, value);
1068 gtk_css_ruleset_add (GtkCssRuleset *ruleset,
1069 const GtkStyleProperty *prop,
1072 if (ruleset->style == NULL)
1073 ruleset->style = g_hash_table_new_full (g_direct_hash,
1076 (GDestroyNotify) property_value_free);
1078 if (_gtk_style_property_is_shorthand (prop))
1080 GParameter *parameters;
1081 guint i, n_parameters;
1083 parameters = _gtk_style_property_unpack (prop, value, &n_parameters);
1085 for (i = 0; i < n_parameters; i++)
1087 const GtkStyleProperty *child;
1090 child = _gtk_style_property_lookup (parameters[i].name);
1091 value = g_slice_dup (GValue, ¶meters[i].value);
1092 gtk_css_ruleset_add (ruleset, child, value);
1094 g_free (parameters);
1095 property_value_free (value);
1099 ruleset->has_inherit |= gtk_style_param_get_inherit (prop->pspec);
1100 g_hash_table_insert (ruleset->style, (gpointer) prop, value);
1104 gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
1105 GtkWidgetPath *path,
1108 return _gtk_css_selector_matches (ruleset->selector, path, length);
1112 gtk_css_scanner_reset (GtkCssScanner *scanner)
1114 g_slist_free (scanner->state);
1115 scanner->state = NULL;
1117 g_slist_free_full (scanner->cur_selectors, (GDestroyNotify) _gtk_css_selector_free);
1118 scanner->cur_selectors = NULL;
1120 if (scanner->cur_properties)
1121 g_hash_table_unref (scanner->cur_properties);
1123 scanner ->cur_properties = g_hash_table_new_full (g_str_hash,
1125 (GDestroyNotify) g_free,
1126 (GDestroyNotify) property_value_free);
1130 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1132 gtk_css_scanner_reset (scanner);
1134 g_object_unref (scanner->provider);
1136 g_object_unref (scanner->file);
1137 g_object_unref (scanner->base);
1138 g_hash_table_destroy (scanner->cur_properties);
1139 _gtk_css_parser_free (scanner->parser);
1141 g_slice_free (GtkCssScanner, scanner);
1145 gtk_css_scanner_parser_error (GtkCssParser *parser,
1146 const GError *error,
1149 GtkCssScanner *scanner = user_data;
1151 gtk_css_provider_take_error_full (scanner->provider,
1153 _gtk_css_parser_get_line (scanner->parser),
1154 _gtk_css_parser_get_position (scanner->parser),
1155 g_error_copy (error));
1158 static GtkCssScanner *
1159 gtk_css_scanner_new (GtkCssProvider *provider,
1160 GtkCssScanner *parent,
1165 GtkCssScanner *scanner;
1167 g_assert (data[length] == 0);
1169 scanner = g_slice_new0 (GtkCssScanner);
1171 g_object_ref (provider);
1172 scanner->provider = provider;
1173 scanner->parent = parent;
1177 scanner->file = g_object_ref (file);
1178 scanner->base = g_file_get_parent (file);
1182 char *dir = g_get_current_dir ();
1183 scanner->base = g_file_new_for_path (dir);
1187 scanner->cur_properties = g_hash_table_new_full (g_str_hash,
1189 (GDestroyNotify) g_free,
1190 (GDestroyNotify) property_value_free);
1192 scanner->parser = _gtk_css_parser_new (data,
1193 gtk_css_scanner_parser_error,
1200 gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
1202 return scanner->base;
1206 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1211 if (scanner->file && g_file_equal (scanner->file, file))
1214 scanner = scanner->parent;
1221 gtk_css_provider_init (GtkCssProvider *css_provider)
1223 GtkCssProviderPrivate *priv;
1225 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1226 GTK_TYPE_CSS_PROVIDER,
1227 GtkCssProviderPrivate);
1229 priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1231 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1232 (GDestroyNotify) g_free,
1233 (GDestroyNotify) gtk_symbolic_color_unref);
1237 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1238 GtkStyleProperties *props)
1240 GtkCssProviderPrivate *priv;
1241 GHashTableIter iter;
1242 gpointer key, value;
1244 priv = css_provider->priv;
1245 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1247 while (g_hash_table_iter_next (&iter, &key, &value))
1250 GtkSymbolicColor *color;
1255 gtk_style_properties_map_color (props, name, color);
1259 static GtkStyleProperties *
1260 gtk_css_provider_get_style (GtkStyleProvider *provider,
1261 GtkWidgetPath *path)
1263 GtkCssProvider *css_provider;
1264 GtkCssProviderPrivate *priv;
1265 GtkStyleProperties *props;
1268 css_provider = GTK_CSS_PROVIDER (provider);
1269 priv = css_provider->priv;
1270 length = gtk_widget_path_length (path);
1271 props = gtk_style_properties_new ();
1273 css_provider_dump_symbolic_colors (css_provider, props);
1275 for (l = 1; l <= length; l++)
1277 for (i = 0; i < priv->rulesets->len; i++)
1279 GtkCssRuleset *ruleset;
1280 GHashTableIter iter;
1281 gpointer key, value;
1283 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1285 if (ruleset->style == NULL)
1288 if (l < length && (!ruleset->has_inherit || _gtk_css_selector_get_state_flags (ruleset->selector)))
1291 if (!gtk_css_ruleset_matches (ruleset, path, l))
1294 g_hash_table_iter_init (&iter, ruleset->style);
1296 while (g_hash_table_iter_next (&iter, &key, &value))
1298 GtkStyleProperty *prop = key;
1300 if (l != length && !gtk_style_param_get_inherit (prop->pspec))
1303 _gtk_style_properties_set_property_by_property (props,
1305 _gtk_css_selector_get_state_flags (ruleset->selector),
1315 gtk_css_provider_parser_error (GtkCssParser *parser,
1316 const GError *error,
1319 GtkCssProvider *provider = user_data;
1321 gtk_css_provider_take_error_full (provider,
1323 _gtk_css_parser_get_line (parser),
1324 _gtk_css_parser_get_position (parser),
1325 g_error_copy (error));
1329 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1330 GtkWidgetPath *path,
1331 GtkStateFlags state,
1335 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1336 GtkCssProviderPrivate *priv = css_provider->priv;
1338 gboolean found = FALSE;
1342 prop_name = g_strdup_printf ("-%s-%s",
1343 g_type_name (pspec->owner_type),
1346 for (i = priv->rulesets->len - 1; i >= 0; i--)
1348 GtkCssRuleset *ruleset;
1349 GtkStateFlags selector_state;
1351 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1353 if (ruleset->widget_style == NULL)
1356 if (!gtk_css_ruleset_matches (ruleset, path, gtk_widget_path_length (path)))
1359 selector_state = _gtk_css_selector_get_state_flags (ruleset->selector);
1360 val = g_hash_table_lookup (ruleset->widget_style, prop_name);
1363 (selector_state == 0 ||
1364 selector_state == state ||
1365 ((selector_state & state) != 0 &&
1366 (selector_state & ~(state)) == 0)))
1368 GtkCssParser *parser;
1370 parser = _gtk_css_parser_new (g_value_get_string (val),
1371 gtk_css_provider_parser_error,
1374 found = _gtk_style_property_parse_value (NULL,
1379 _gtk_css_parser_free (parser);
1392 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1394 iface->get_style = gtk_css_provider_get_style;
1395 iface->get_style_property = gtk_css_provider_get_style_property;
1399 gtk_css_provider_finalize (GObject *object)
1401 GtkCssProvider *css_provider;
1402 GtkCssProviderPrivate *priv;
1405 css_provider = GTK_CSS_PROVIDER (object);
1406 priv = css_provider->priv;
1408 for (i = 0; i < priv->rulesets->len; i++)
1409 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1411 g_array_free (priv->rulesets, TRUE);
1413 if (priv->symbolic_colors)
1414 g_hash_table_destroy (priv->symbolic_colors);
1416 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1420 * gtk_css_provider_new:
1422 * Returns a newly created #GtkCssProvider.
1424 * Returns: A new #GtkCssProvider
1427 gtk_css_provider_new (void)
1429 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1433 gtk_css_provider_take_error (GtkCssProvider *provider,
1434 GtkCssScanner *scanner,
1437 gtk_css_provider_take_error_full (provider,
1439 _gtk_css_parser_get_line (scanner->parser),
1440 _gtk_css_parser_get_position (scanner->parser),
1445 gtk_css_provider_error_literal (GtkCssProvider *provider,
1446 GtkCssScanner *scanner,
1449 const char *message)
1451 gtk_css_provider_take_error (provider,
1453 g_error_new_literal (domain, code, message));
1457 gtk_css_provider_error (GtkCssProvider *provider,
1458 GtkCssScanner *scanner,
1462 ...) G_GNUC_PRINTF (5, 6);
1464 gtk_css_provider_error (GtkCssProvider *provider,
1465 GtkCssScanner *scanner,
1474 va_start (args, format);
1475 error = g_error_new_valist (domain, code, format, args);
1478 gtk_css_provider_take_error (provider, scanner, error);
1482 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1483 GtkCssScanner *scanner,
1484 const char *expected)
1486 gtk_css_provider_error (provider,
1488 GTK_CSS_PROVIDER_ERROR,
1489 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1490 "expected a valid %s", expected);
1494 css_provider_commit (GtkCssProvider *css_provider,
1496 GtkCssRuleset *ruleset)
1498 GtkCssProviderPrivate *priv;
1501 priv = css_provider->priv;
1503 if (ruleset->style == NULL && ruleset->widget_style == NULL)
1505 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1509 for (l = selectors; l; l = l->next)
1513 gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1515 g_array_append_val (priv->rulesets, new);
1518 g_slist_free (selectors);
1522 gtk_css_provider_reset (GtkCssProvider *css_provider)
1524 GtkCssProviderPrivate *priv;
1527 priv = css_provider->priv;
1529 for (i = 0; i < priv->rulesets->len; i++)
1530 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1531 g_array_set_size (priv->rulesets, 0);
1535 gtk_css_provider_propagate_error (GtkCssProvider *provider,
1539 const GError *error,
1540 GError **propagate_to)
1542 /* don't fail for deprecations */
1543 if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
1545 g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
1549 /* we already set an error. And we'd like to keep the first one */
1553 *propagate_to = g_error_copy (error);
1554 g_prefix_error (propagate_to, "%s:%u:%u: ", path ? path : "<unknown>", line, position);
1558 parse_import (GtkCssScanner *scanner)
1563 uri = _gtk_css_parser_read_uri (scanner->parser);
1566 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1570 file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
1573 if (gtk_css_scanner_would_recurse (scanner, file))
1575 char *path = g_file_get_path (file);
1576 gtk_css_provider_error (scanner->provider,
1578 GTK_CSS_PROVIDER_ERROR,
1579 GTK_CSS_PROVIDER_ERROR_IMPORT,
1580 "Loading '%s' would recurse",
1586 gtk_css_provider_load_internal (scanner->provider,
1593 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1595 g_object_unref (file);
1596 gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1597 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1601 g_object_unref (file);
1605 parse_color_definition (GtkCssScanner *scanner)
1607 GtkSymbolicColor *symbolic;
1610 name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1613 gtk_css_provider_error_literal (scanner->provider,
1615 GTK_CSS_PROVIDER_ERROR,
1616 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1617 "Not a valid color name");
1618 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1622 symbolic = _gtk_css_parser_read_symbolic_color (scanner->parser);
1623 if (symbolic == NULL)
1626 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1630 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1633 gtk_symbolic_color_unref (symbolic);
1634 gtk_css_provider_error_literal (scanner->provider,
1636 GTK_CSS_PROVIDER_ERROR,
1637 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1638 "Missing semicolon at end of color definition");
1639 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1643 g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
1647 parse_binding_set (GtkCssScanner *scanner)
1649 GtkBindingSet *binding_set;
1652 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1655 gtk_css_provider_error_literal (scanner->provider,
1657 GTK_CSS_PROVIDER_ERROR,
1658 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1659 "Expected name for binding set");
1660 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1661 goto skip_semicolon;
1664 binding_set = gtk_binding_set_find (name);
1667 binding_set = gtk_binding_set_new (name);
1668 binding_set->parsed = TRUE;
1672 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
1674 gtk_css_provider_error_literal (scanner->provider,
1676 GTK_CSS_PROVIDER_ERROR,
1677 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1678 "Expected '{' for binding set");
1679 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1680 goto skip_semicolon;
1683 while (!_gtk_css_parser_is_eof (scanner->parser) &&
1684 !_gtk_css_parser_begins_with (scanner->parser, '}'))
1686 name = _gtk_css_parser_read_value (scanner->parser);
1689 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1693 gtk_binding_entry_add_signal_from_string (binding_set, name);
1696 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1698 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
1699 !_gtk_css_parser_is_eof (scanner->parser))
1701 gtk_css_provider_error_literal (scanner->provider,
1703 GTK_CSS_PROVIDER_ERROR,
1704 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1705 "Expected semicolon");
1706 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1711 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
1713 gtk_css_provider_error_literal (scanner->provider,
1715 GTK_CSS_PROVIDER_ERROR,
1716 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1717 "expected '}' after declarations");
1718 if (!_gtk_css_parser_is_eof (scanner->parser))
1719 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
1723 if (_gtk_css_parser_try (scanner->parser, ";", TRUE))
1724 gtk_css_provider_error_literal (scanner->provider,
1726 GTK_CSS_PROVIDER_ERROR,
1727 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
1728 "Nonstandard semicolon at end of binding set");
1732 parse_at_keyword (GtkCssScanner *scanner)
1734 if (_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1735 parse_import (scanner);
1736 else if (_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1737 parse_color_definition (scanner);
1738 else if (_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
1739 parse_binding_set (scanner);
1742 gtk_css_provider_error_literal (scanner->provider,
1744 GTK_CSS_PROVIDER_ERROR,
1745 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1747 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1752 parse_selector_class (GtkCssScanner *scanner, GArray *classes)
1757 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1761 gtk_css_provider_error_literal (scanner->provider,
1763 GTK_CSS_PROVIDER_ERROR,
1764 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1765 "Expected a valid name for class");
1769 qname = g_quark_from_string (name);
1770 g_array_append_val (classes, qname);
1776 parse_selector_name (GtkCssScanner *scanner, GArray *names)
1781 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1785 gtk_css_provider_error_literal (scanner->provider,
1787 GTK_CSS_PROVIDER_ERROR,
1788 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1789 "Expected a valid name for id");
1793 qname = g_quark_from_string (name);
1794 g_array_append_val (names, qname);
1800 parse_selector_pseudo_class (GtkCssScanner *scanner,
1801 GtkRegionFlags *region_to_modify,
1802 GtkStateFlags *state_to_modify)
1806 GtkRegionFlags region_flag;
1807 GtkStateFlags state_flag;
1808 } pseudo_classes[] = {
1809 { "first", GTK_REGION_FIRST, 0 },
1810 { "last", GTK_REGION_LAST, 0 },
1811 { "sorted", GTK_REGION_SORTED, 0 },
1812 { "active", 0, GTK_STATE_FLAG_ACTIVE },
1813 { "prelight", 0, GTK_STATE_FLAG_PRELIGHT },
1814 { "hover", 0, GTK_STATE_FLAG_PRELIGHT },
1815 { "selected", 0, GTK_STATE_FLAG_SELECTED },
1816 { "insensitive", 0, GTK_STATE_FLAG_INSENSITIVE },
1817 { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
1818 { "focused", 0, GTK_STATE_FLAG_FOCUSED },
1819 { "focus", 0, GTK_STATE_FLAG_FOCUSED },
1821 }, nth_child_classes[] = {
1822 { "first", GTK_REGION_FIRST, 0 },
1823 { "last", GTK_REGION_LAST, 0 },
1824 { "even", GTK_REGION_EVEN, 0 },
1825 { "odd", GTK_REGION_ODD, 0 },
1831 name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
1834 gtk_css_provider_error_literal (scanner->provider,
1836 GTK_CSS_PROVIDER_ERROR,
1837 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1838 "Missing name of pseudo-class");
1842 if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
1844 char *function = name;
1846 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1847 if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
1849 gtk_css_provider_error_literal (scanner->provider,
1851 GTK_CSS_PROVIDER_ERROR,
1852 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1853 "Missing closing bracket for pseudo-class");
1857 if (g_ascii_strcasecmp (function, "nth-child") != 0)
1859 gtk_css_provider_error (scanner->provider,
1861 GTK_CSS_PROVIDER_ERROR,
1862 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1863 "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
1873 gtk_css_provider_error (scanner->provider,
1875 GTK_CSS_PROVIDER_ERROR,
1876 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1877 "nth-child() requires an argument");
1881 classes = nth_child_classes;
1884 classes = pseudo_classes;
1886 for (i = 0; classes[i].name != NULL; i++)
1888 if (g_ascii_strcasecmp (name, classes[i].name) == 0)
1890 if ((*region_to_modify & classes[i].region_flag) ||
1891 (*state_to_modify & classes[i].state_flag))
1893 if (classes == nth_child_classes)
1894 gtk_css_provider_error (scanner->provider,
1896 GTK_CSS_PROVIDER_ERROR,
1897 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1898 "Duplicate pseudo-class 'nth-child(%s)'", name);
1900 gtk_css_provider_error (scanner->provider,
1902 GTK_CSS_PROVIDER_ERROR,
1903 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1904 "Duplicate pseudo-class '%s'", name);
1906 *region_to_modify |= classes[i].region_flag;
1907 *state_to_modify |= classes[i].state_flag;
1914 if (classes == nth_child_classes)
1915 gtk_css_provider_error (scanner->provider,
1917 GTK_CSS_PROVIDER_ERROR,
1918 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1919 "Unknown pseudo-class 'nth-child(%s)'", name);
1921 gtk_css_provider_error (scanner->provider,
1923 GTK_CSS_PROVIDER_ERROR,
1924 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1925 "Unknown pseudo-class '%s'", name);
1931 parse_simple_selector (GtkCssScanner *scanner,
1935 GtkRegionFlags *pseudo_classes,
1936 GtkStateFlags *state)
1938 gboolean parsed_something;
1940 *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
1942 parsed_something = TRUE;
1944 parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
1947 if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
1949 if (!parse_selector_name (scanner, ids))
1952 else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
1954 if (!parse_selector_class (scanner, classes))
1957 else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
1959 if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
1962 else if (!parsed_something)
1964 gtk_css_provider_error_literal (scanner->provider,
1966 GTK_CSS_PROVIDER_ERROR,
1967 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1968 "Expected a valid selector");
1974 parsed_something = TRUE;
1976 while (!_gtk_css_parser_is_eof (scanner->parser));
1978 _gtk_css_parser_skip_whitespace (scanner->parser);
1982 static GtkCssSelector *
1983 parse_selector (GtkCssScanner *scanner)
1985 GtkCssSelector *selector = NULL;
1989 GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
1990 GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
1991 GtkRegionFlags pseudo_classes = 0;
1992 GtkStateFlags state = 0;
1993 GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
1997 if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
1998 combine = GTK_CSS_COMBINE_CHILD;
2001 if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
2003 g_array_free (ids, TRUE);
2004 g_array_free (classes, TRUE);
2006 _gtk_css_selector_free (selector);
2010 selector = _gtk_css_selector_new (selector,
2013 (GQuark *) g_array_free (ids, ids->len == 0),
2014 (GQuark *) g_array_free (classes, classes->len == 0),
2019 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2020 !_gtk_css_parser_begins_with (scanner->parser, ',') &&
2021 !_gtk_css_parser_begins_with (scanner->parser, '{'));
2027 parse_selector_list (GtkCssScanner *scanner)
2029 GSList *selectors = NULL;
2032 GtkCssSelector *select = parse_selector (scanner);
2036 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2037 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2041 selectors = g_slist_prepend (selectors, select);
2043 while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
2049 parse_declaration (GtkCssScanner *scanner,
2050 GtkCssRuleset *ruleset)
2052 const GtkStyleProperty *property;
2055 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2057 goto check_for_semicolon;
2059 property = _gtk_style_property_lookup (name);
2060 if (property == NULL && name[0] != '-')
2062 gtk_css_provider_error (scanner->provider,
2064 GTK_CSS_PROVIDER_ERROR,
2065 GTK_CSS_PROVIDER_ERROR_NAME,
2066 "'%s' is not a valid property name",
2068 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2073 if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
2075 gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
2076 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2087 val = g_slice_new0 (GValue);
2088 g_value_init (val, property->pspec->value_type);
2090 if (_gtk_style_property_parse_value (property,
2093 gtk_css_scanner_get_base_url (scanner)))
2095 if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
2096 _gtk_css_parser_begins_with (scanner->parser, '}') ||
2097 _gtk_css_parser_is_eof (scanner->parser))
2099 gtk_css_ruleset_add (ruleset, property, val);
2103 gtk_css_provider_error_literal (scanner->provider,
2105 GTK_CSS_PROVIDER_ERROR,
2106 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2107 "Junk at end of value");
2108 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2109 g_value_unset (val);
2110 g_slice_free (GValue, val);
2116 g_value_unset (val);
2117 g_slice_free (GValue, val);
2118 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2122 else if (name[0] == '-')
2126 value_str = _gtk_css_parser_read_value (scanner->parser);
2131 val = g_slice_new0 (GValue);
2132 g_value_init (val, G_TYPE_STRING);
2133 g_value_take_string (val, value_str);
2135 gtk_css_ruleset_add_style (ruleset, name, val);
2139 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2146 check_for_semicolon:
2147 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2149 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2150 !_gtk_css_parser_is_eof (scanner->parser))
2152 gtk_css_provider_error_literal (scanner->provider,
2154 GTK_CSS_PROVIDER_ERROR,
2155 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2156 "Expected semicolon");
2157 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2163 parse_declarations (GtkCssScanner *scanner,
2164 GtkCssRuleset *ruleset)
2166 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2167 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2169 parse_declaration (scanner, ruleset);
2174 parse_ruleset (GtkCssScanner *scanner)
2177 GtkCssRuleset ruleset = { 0, };
2179 selectors = parse_selector_list (scanner);
2180 if (selectors == NULL)
2183 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2185 gtk_css_provider_error_literal (scanner->provider,
2187 GTK_CSS_PROVIDER_ERROR,
2188 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2189 "expected '{' after selectors");
2190 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2191 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2195 parse_declarations (scanner, &ruleset);
2197 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2199 gtk_css_provider_error_literal (scanner->provider,
2201 GTK_CSS_PROVIDER_ERROR,
2202 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2203 "expected '}' after declarations");
2204 if (!_gtk_css_parser_is_eof (scanner->parser))
2206 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2207 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2208 gtk_css_ruleset_clear (&ruleset);
2213 css_provider_commit (scanner->provider, selectors, &ruleset);
2214 gtk_css_ruleset_clear (&ruleset);
2218 parse_statement (GtkCssScanner *scanner)
2220 if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2221 parse_at_keyword (scanner);
2223 parse_ruleset (scanner);
2227 parse_stylesheet (GtkCssScanner *scanner)
2229 _gtk_css_parser_skip_whitespace (scanner->parser);
2231 while (!_gtk_css_parser_is_eof (scanner->parser))
2233 if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2234 _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2237 parse_statement (scanner);
2242 gtk_css_provider_compare_rule (gconstpointer a_,
2245 const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2246 const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2249 compare = _gtk_css_selector_compare (a->selector, b->selector);
2253 /* compare pointers in array to ensure a stable sort */
2264 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2266 GtkCssProviderPrivate *priv = css_provider->priv;
2268 g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2272 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2273 GtkCssScanner *parent,
2279 GtkCssScanner *scanner;
2280 gulong error_handler;
2281 char *free_data = NULL;
2284 error_handler = g_signal_connect (css_provider,
2286 G_CALLBACK (gtk_css_provider_propagate_error),
2289 error_handler = 0; /* silence gcc */
2293 GError *load_error = NULL;
2295 if (g_file_load_contents (file, NULL,
2296 &free_data, &length,
2305 gtk_css_provider_error (css_provider,
2307 GTK_CSS_PROVIDER_ERROR,
2308 GTK_CSS_PROVIDER_ERROR_IMPORT,
2309 "Failed to import: %s",
2310 load_error->message);
2311 g_error_free (load_error);
2315 gtk_css_provider_take_error_full (css_provider,
2325 scanner = gtk_css_scanner_new (css_provider, parent, file, data, length);
2327 parse_stylesheet (scanner);
2329 gtk_css_scanner_destroy (scanner);
2332 gtk_css_provider_postprocess (css_provider);
2339 g_signal_handler_disconnect (css_provider, error_handler);
2343 /* We clear all contents from the provider for backwards compat reasons */
2344 gtk_css_provider_reset (css_provider);
2353 * gtk_css_provider_load_from_data:
2354 * @css_provider: a #GtkCssProvider
2355 * @data: (array length=length) (element-type guint8): CSS data loaded in memory
2356 * @length: the length of @data in bytes, or -1 for NUL terminated strings
2357 * @error: (out) (allow-none): return location for a #GError, or %NULL
2359 * Loads @data into @css_provider, making it clear any previously loaded
2362 * Returns: %TRUE if the data could be loaded.
2365 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2370 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2371 g_return_val_if_fail (data != NULL, FALSE);
2374 length = strlen (data);
2376 gtk_css_provider_reset (css_provider);
2378 return gtk_css_provider_load_internal (css_provider, NULL, NULL, data, length, error);
2382 * gtk_css_provider_load_from_file:
2383 * @css_provider: a #GtkCssProvider
2384 * @file: #GFile pointing to a file to load
2385 * @error: (out) (allow-none): return location for a #GError, or %NULL
2387 * Loads the data contained in @file into @css_provider, making it
2388 * clear any previously loaded information.
2390 * Returns: %TRUE if the data could be loaded.
2393 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2397 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2398 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2400 gtk_css_provider_reset (css_provider);
2402 return gtk_css_provider_load_internal (css_provider, NULL, file, NULL, 0, error);
2406 * gtk_css_provider_load_from_path:
2407 * @css_provider: a #GtkCssProvider
2408 * @path: the path of a filename to load, in the GLib filename encoding
2409 * @error: (out) (allow-none): return location for a #GError, or %NULL
2411 * Loads the data contained in @path into @css_provider, making it clear
2412 * any previously loaded information.
2414 * Returns: %TRUE if the data could be loaded.
2417 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2424 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2425 g_return_val_if_fail (path != NULL, FALSE);
2427 file = g_file_new_for_path (path);
2429 result = gtk_css_provider_load_from_file (css_provider, file, error);
2431 g_object_unref (file);
2437 * gtk_css_provider_get_default:
2439 * Returns the provider containing the style settings used as a
2440 * fallback for all widgets.
2442 * Returns: (transfer none): The provider used for fallback styling.
2443 * This memory is owned by GTK+, and you must not free it.
2446 gtk_css_provider_get_default (void)
2448 static GtkCssProvider *provider;
2450 if (G_UNLIKELY (!provider))
2453 "@define-color fg_color #000; \n"
2454 "@define-color bg_color #dcdad5; \n"
2455 "@define-color text_color #000; \n"
2456 "@define-color base_color #fff; \n"
2457 "@define-color selected_bg_color #4b6983; \n"
2458 "@define-color selected_fg_color #fff; \n"
2459 "@define-color tooltip_bg_color #eee1b3; \n"
2460 "@define-color tooltip_fg_color #000; \n"
2461 "@define-color placeholder_text_color #808080; \n"
2463 "@define-color info_fg_color rgb (181, 171, 156);\n"
2464 "@define-color info_bg_color rgb (252, 252, 189);\n"
2465 "@define-color warning_fg_color rgb (173, 120, 41);\n"
2466 "@define-color warning_bg_color rgb (250, 173, 61);\n"
2467 "@define-color question_fg_color rgb (97, 122, 214);\n"
2468 "@define-color question_bg_color rgb (138, 173, 212);\n"
2469 "@define-color error_fg_color rgb (166, 38, 38);\n"
2470 "@define-color error_bg_color rgb (237, 54, 54);\n"
2473 " background-color: @bg_color;\n"
2474 " color: @fg_color;\n"
2475 " border-color: shade (@bg_color, 0.6);\n"
2477 " border-width: 0;\n"
2481 " background-color: shade (@bg_color, 1.05);\n"
2482 " color: shade (@fg_color, 1.3);\n"
2486 " background-color: @selected_bg_color;\n"
2487 " color: @selected_fg_color;\n"
2490 ".expander, GtkTreeView.view.expander {\n"
2494 ".expander:prelight,\n"
2495 "GtkTreeView.view.expander:selected:prelight {\n"
2496 " color: @text_color;\n"
2499 ".expander:active {\n"
2500 " transition: 200ms linear;\n"
2504 " border-color: shade (@bg_color, 0.7);\n"
2505 " background-color: shade (@bg_color, 0.9);\n"
2506 " color: shade (@bg_color, 0.7);\n"
2510 " border-width: 0;\n"
2511 " border-radius: 0;\n"
2512 " background-color: @base_color;\n"
2513 " color: @text_color;\n"
2515 ".view:selected {\n"
2516 " background-color: shade (@bg_color, 0.9);\n"
2517 " color: @fg_color;\n"
2520 ".view:selected:focused {\n"
2521 " background-color: @selected_bg_color;\n"
2522 " color: @selected_fg_color;\n"
2525 ".view column:sorted row,\n"
2526 ".view column:sorted row:prelight {\n"
2527 " background-color: shade (@bg_color, 0.85);\n"
2530 ".view column:sorted row:nth-child(odd),\n"
2531 ".view column:sorted row:nth-child(odd):prelight {\n"
2532 " background-color: shade (@bg_color, 0.8);\n"
2536 ".view row:prelight {\n"
2537 " background-color: @base_color;\n"
2538 " color: @text_color;\n"
2541 ".view row:nth-child(odd),\n"
2542 ".view row:nth-child(odd):prelight {\n"
2543 " background-color: shade (@base_color, 0.93); \n"
2546 ".view row:selected:focused {\n"
2547 " background-color: @selected_bg_color;\n"
2550 ".view row:selected {\n"
2551 " background-color: darker (@bg_color);\n"
2552 " color: @selected_fg_color;\n"
2555 ".view.cell.trough,\n"
2556 ".view.cell.trough:hover,\n"
2557 ".view.cell.trough:selected,\n"
2558 ".view.cell.trough:selected:focused {\n"
2559 " background-color: @bg_color;\n"
2560 " color: @fg_color;\n"
2563 ".view.cell.progressbar,\n"
2564 ".view.cell.progressbar:hover,\n"
2565 ".view.cell.progressbar:selected,\n"
2566 ".view.cell.progressbar:selected:focused {\n"
2567 " background-color: @selected_bg_color;\n"
2568 " color: @selected_fg_color;\n"
2572 " background-color: alpha (@fg_color, 0.25);\n"
2573 " border-color: @fg_color;\n"
2574 " border-style: solid;\n"
2575 " border-width: 1;\n"
2579 " background-color: @tooltip_bg_color; \n"
2580 " color: @tooltip_fg_color; \n"
2581 " border-color: @tooltip_fg_color; \n"
2582 " border-width: 1;\n"
2583 " border-style: solid;\n"
2588 " border-style: outset; \n"
2589 " border-width: 2; \n"
2592 ".button:active {\n"
2593 " background-color: shade (@bg_color, 0.7);\n"
2594 " border-style: inset; \n"
2597 ".button:prelight,\n"
2598 ".slider:prelight {\n"
2599 " background-color: @selected_bg_color;\n"
2600 " color: @selected_fg_color;\n"
2601 " border-color: shade (@selected_bg_color, 0.7);\n"
2605 " background-color: darker (@bg_color);\n"
2606 " border-style: inset;\n"
2607 " border-width: 1;\n"
2612 " border-style: inset;\n"
2613 " border-width: 2;\n"
2614 " background-color: @base_color;\n"
2615 " color: @text_color;\n"
2618 ".entry:insensitive {\n"
2619 " background-color: shade (@base_color, 0.9);\n"
2620 " color: shade (@base_color, 0.7);\n"
2623 " background-color: #c4c2bd;\n"
2628 ".entry.progressbar, \n"
2629 ".cell.progressbar {\n"
2630 " background-color: @selected_bg_color;\n"
2631 " border-color: shade (@selected_bg_color, 0.7);\n"
2632 " color: @selected_fg_color;\n"
2633 " border-style: outset;\n"
2634 " border-width: 1;\n"
2637 "GtkCheckButton:hover,\n"
2638 "GtkCheckButton:selected,\n"
2639 "GtkRadioButton:hover,\n"
2640 "GtkRadioButton:selected {\n"
2641 " background-color: shade (@bg_color, 1.05);\n"
2645 ".cell.check, .cell.radio,\n"
2646 ".cell.check:hover, .cell.radio:hover {\n"
2647 " border-style: solid;\n"
2648 " border-width: 1;\n"
2649 " background-color: @base_color;\n"
2650 " border-color: @fg_color;\n"
2653 ".check:active, .radio:active,\n"
2654 ".check:hover, .radio:hover {\n"
2655 " background-color: @base_color;\n"
2656 " border-color: @fg_color;\n"
2657 " color: @text_color;\n"
2660 ".check:selected, .radio:selected {\n"
2661 " background-color: darker (@bg_color);\n"
2662 " color: @selected_fg_color;\n"
2663 " border-color: @selected_fg_color;\n"
2666 ".check:selected:focused, .radio:selected:focused {\n"
2667 " background-color: @selected_bg_color;\n"
2670 ".menu.check, .menu.radio {\n"
2671 " color: @fg_color;\n"
2672 " border-style: none;\n"
2673 " border-width: 0;\n"
2677 " border-style: outset;\n"
2678 " border-width: 1;\n"
2682 " border-style: inset;\n"
2683 " border-width: 2;\n"
2687 " border-style: outset;\n"
2688 " border-width: 1;\n"
2692 " border-style: inset;\n"
2693 " border-width: 1;\n"
2696 "GtkScrolledWindow.frame {\n"
2703 " border-style: outset;\n"
2704 " border-width: 1;\n"
2709 ".menu.check:hover,\n"
2710 ".menu.radio:hover {\n"
2711 " background-color: @selected_bg_color;\n"
2712 " color: @selected_fg_color;\n"
2715 "GtkSpinButton.button {\n"
2716 " border-width: 1;\n"
2719 ".scale.slider:hover,\n"
2720 "GtkSpinButton.button:hover {\n"
2721 " background-color: shade (@bg_color, 1.05);\n"
2722 " border-color: shade (@bg_color, 0.8);\n"
2725 "GtkSwitch.trough:active {\n"
2726 " background-color: @selected_bg_color;\n"
2727 " color: @selected_fg_color;\n"
2730 "GtkToggleButton.button:inconsistent {\n"
2731 " border-style: outset;\n"
2732 " border-width: 1px;\n"
2733 " background-color: shade (@bg_color, 0.9);\n"
2734 " border-color: shade (@bg_color, 0.7);\n"
2737 "GtkLabel:selected {\n"
2738 " background-color: shade (@bg_color, 0.9);\n"
2741 "GtkLabel:selected:focused {\n"
2742 " background-color: @selected_bg_color;\n"
2745 ".spinner:active {\n"
2746 " transition: 750ms linear loop;\n"
2750 " background-color: @info_bg_color;\n"
2751 " color: @info_fg_color;\n"
2755 " background-color: @warning_bg_color;\n"
2756 " color: @warning_fg_color;\n"
2760 " background-color: @question_bg_color;\n"
2761 " color: @question_fg_color;\n"
2765 " background-color: @error_bg_color;\n"
2766 " color: @error_fg_color;\n"
2770 " background-color: @selected_bg_color;\n"
2771 " color: @selected_fg_color;\n"
2774 ".light-area-focus {\n"
2778 ".dark-area-focus {\n"
2781 "GtkCalendar.view {\n"
2782 " border-width: 1;\n"
2783 " border-style: inset;\n"
2787 "GtkCalendar.view:inconsistent {\n"
2788 " color: darker (@bg_color);\n"
2791 "GtkCalendar.header {\n"
2792 " background-color: @bg_color;\n"
2793 " border-style: outset;\n"
2794 " border-width: 2;\n"
2797 "GtkCalendar.highlight {\n"
2798 " border-width: 0;\n"
2801 "GtkCalendar.button {\n"
2802 " background-color: @bg_color;\n"
2805 "GtkCalendar.button:hover {\n"
2806 " background-color: lighter (@bg_color);\n"
2807 " color: @fg_color;\n"
2811 " border-width: 0;\n"
2816 provider = gtk_css_provider_new ();
2817 if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
2819 g_error ("Failed to load the internal default CSS.");
2827 _gtk_css_provider_get_theme_dir (void)
2832 var = g_getenv ("GTK_DATA_PREFIX");
2835 path = g_build_filename (var, "share", "themes", NULL);
2837 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
2843 * gtk_css_provider_get_named:
2844 * @name: A theme name
2845 * @variant: (allow-none): variant to load, for example, "dark", or
2846 * %NULL for the default
2848 * Loads a theme from the usual theme paths
2850 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
2851 * This memory is owned by GTK+, and you must not free it.
2854 gtk_css_provider_get_named (const gchar *name,
2855 const gchar *variant)
2857 static GHashTable *themes = NULL;
2858 GtkCssProvider *provider;
2861 if (G_UNLIKELY (!themes))
2862 themes = g_hash_table_new (g_str_hash, g_str_equal);
2864 if (variant == NULL)
2865 key = (gchar *)name;
2867 key = g_strconcat (name, "-", variant, NULL);
2869 provider = g_hash_table_lookup (themes, key);
2873 const gchar *home_dir;
2874 gchar *subpath, *path = NULL;
2877 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
2879 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
2881 /* First look in the users home directory
2883 home_dir = g_get_home_dir ();
2886 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
2888 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2899 theme_dir = _gtk_css_provider_get_theme_dir ();
2900 path = g_build_filename (theme_dir, name, subpath, NULL);
2903 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2914 provider = gtk_css_provider_new ();
2916 if (!gtk_css_provider_load_from_path (provider, path, NULL))
2918 g_object_unref (provider);
2922 g_hash_table_insert (themes, g_strdup (key), provider);
2935 compare_properties (gconstpointer a, gconstpointer b)
2937 return strcmp (((const GtkStyleProperty *) a)->pspec->name,
2938 ((const GtkStyleProperty *) b)->pspec->name);
2942 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
2947 _gtk_css_selector_print (ruleset->selector, str);
2949 g_string_append (str, " {\n");
2953 keys = g_hash_table_get_keys (ruleset->style);
2954 /* so the output is identical for identical selector styles */
2955 keys = g_list_sort (keys, compare_properties);
2957 for (walk = keys; walk; walk = walk->next)
2959 GtkStyleProperty *prop = walk->data;
2960 const GValue *value = g_hash_table_lookup (ruleset->style, prop);
2962 g_string_append (str, " ");
2963 g_string_append (str, prop->pspec->name);
2964 g_string_append (str, ": ");
2965 _gtk_style_property_print_value (prop, value, str);
2966 g_string_append (str, ";\n");
2972 if (ruleset->widget_style)
2974 keys = g_hash_table_get_keys (ruleset->widget_style);
2975 /* so the output is identical for identical selector styles */
2976 keys = g_list_sort (keys, (GCompareFunc) strcmp);
2978 for (walk = keys; walk; walk = walk->next)
2980 const char *name = walk->data;
2981 const GValue *value = g_hash_table_lookup (ruleset->widget_style, (gpointer) name);
2983 g_string_append (str, " ");
2984 g_string_append (str, name);
2985 g_string_append (str, ": ");
2986 g_string_append (str, g_value_get_string (value));
2987 g_string_append (str, ";\n");
2993 g_string_append (str, "}\n");
2997 gtk_css_provider_print_colors (GHashTable *colors,
3003 keys = g_hash_table_get_keys (colors);
3004 /* so the output is identical for identical styles */
3005 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3007 for (walk = keys; walk; walk = walk->next)
3009 const char *name = walk->data;
3010 GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
3012 g_string_append (str, "@define-color ");
3013 g_string_append (str, name);
3014 g_string_append (str, " ");
3015 s = gtk_symbolic_color_to_string (symbolic);
3016 g_string_append (str, s);
3018 g_string_append (str, ";\n");
3025 * gtk_css_provider_to_string:
3026 * @provider: the provider to write to a string
3028 * Convertes the @provider into a string representation in CSS
3031 * Using gtk_css_provider_load_from_data() with the return value
3032 * from this function on a new provider created with
3033 * gtk_css_provider_new() will basicallu create a duplicate of
3036 * Returns: a new string representing the @provider.
3039 gtk_css_provider_to_string (GtkCssProvider *provider)
3041 GtkCssProviderPrivate *priv;
3045 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
3047 priv = provider->priv;
3049 str = g_string_new ("");
3051 gtk_css_provider_print_colors (priv->symbolic_colors, str);
3053 for (i = 0; i < priv->rulesets->len; i++)
3056 g_string_append (str, "\n");
3057 gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
3060 return g_string_free (str, FALSE);