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 "gtkcsssectionprivate.h"
32 #include "gtkcssselectorprivate.h"
33 #include "gtksymboliccolor.h"
34 #include "gtkstyleprovider.h"
35 #include "gtkstylecontextprivate.h"
36 #include "gtkstylepropertiesprivate.h"
37 #include "gtkstylepropertyprivate.h"
38 #include "gtkbindings.h"
39 #include "gtkmarshalers.h"
40 #include "gtkprivate.h"
44 * SECTION:gtkcssprovider
45 * @Short_description: CSS-like styling for widgets
46 * @Title: GtkCssProvider
47 * @See_also: #GtkStyleContext, #GtkStyleProvider
49 * GtkCssProvider is an object implementing the #GtkStyleProvider interface.
50 * It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
51 * input in order to style widgets.
53 * <refsect2 id="gtkcssprovider-files">
54 * <title>Default files</title>
56 * An application can cause GTK+ to parse a specific CSS style sheet by
57 * calling gtk_css_provider_load_from_file() and adding the provider with
58 * gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
59 * In addition, certain files will be read when GTK+ is initialized. First,
60 * the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
61 * is loaded if it exists. Then, GTK+ tries to load
62 * <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
64 * <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
65 * where <replaceable>theme-name</replaceable> is the name of the current theme
66 * (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
67 * is the prefix configured when GTK+ was compiled, unless overridden by the
68 * <envar>GTK_DATA_PREFIX</envar> environment variable.
71 * <refsect2 id="gtkcssprovider-stylesheets">
72 * <title>Style sheets</title>
74 * The basic structure of the style sheets understood by this provider is
75 * a series of statements, which are either rule sets or '@-rules', separated
79 * A rule set consists of a selector and a declaration block, which is
80 * a series of declarations enclosed in curly braces ({ and }). The
81 * declarations are separated by semicolons (;). Multiple selectors can
82 * share the same declaration block, by putting all the separators in
83 * front of the block, separated by commas.
85 * <example><title>A rule set with two selectors</title>
86 * <programlisting language="text">
87 * GtkButton, GtkEntry {
94 * <refsect2 id="gtkcssprovider-selectors">
95 * <title>Selectors</title>
97 * Selectors work very similar to the way they do in CSS, with widget class
98 * names taking the role of element names, and widget names taking the role
99 * of IDs. When used in a selector, widget names must be prefixed with a
100 * '#' character. The '*' character represents the so-called universal
101 * selector, which matches any widget.
104 * To express more complicated situations, selectors can be combined in
107 * <listitem><para>To require that a widget satisfies several conditions,
108 * combine several selectors into one by concatenating them. E.g.
109 * <literal>GtkButton#button1</literal> matches a GtkButton widget
110 * with the name button1.</para></listitem>
111 * <listitem><para>To only match a widget when it occurs inside some other
112 * widget, write the two selectors after each other, separated by whitespace.
113 * E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
114 * that occur inside a GtkToolBar.</para></listitem>
115 * <listitem><para>In the previous example, the GtkButton is matched even
116 * if it occurs deeply nested inside the toolbar. To restrict the match
117 * to direct children of the parent widget, insert a '>' character between
118 * the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
119 * GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
123 * <title>Widget classes and names in selectors</title>
124 * <programlisting language="text">
125 * /* Theme labels that are descendants of a window */
126 * GtkWindow GtkLabel {
127 * background-color: #898989
130 * /* Theme notebooks, and anything that's within these */
132 * background-color: #a939f0
135 * /* Theme combo boxes, and entries that
136 * are direct children of a notebook */
138 * GtkNotebook > GtkEntry {
140 * background-color: #1209a2
143 * /* Theme any widget within a GtkBin */
148 * /* Theme a label named title-label */
149 * GtkLabel#title-label {
153 * /* Theme any widget named main-entry */
155 * background-color: #f0a810
160 * Widgets may also define style classes, which can be used for matching.
161 * When used in a selector, style classes must be prefixed with a '.'
165 * Refer to the documentation of individual widgets to learn which
166 * style classes they define and see <xref linkend="gtkstylecontext-classes"/>
167 * for a list of all style classes used by GTK+ widgets.
170 * Note that there is some ambiguity in the selector syntax when it comes
171 * to differentiation widget class names from regions. GTK+ currently treats
172 * a string as a widget class name if it contains any uppercase characters
173 * (which should work for more widgets with names like GtkLabel).
176 * <title>Style classes in selectors</title>
177 * <programlisting language="text">
178 * /* Theme all widgets defining the class entry */
180 * color: #39f1f9;
183 * /* Theme spinbuttons' entry */
184 * GtkSpinButton.entry {
190 * In complicated widgets like e.g. a GtkNotebook, it may be desirable
191 * to style different parts of the widget differently. To make this
192 * possible, container widgets may define regions, whose names
193 * may be used for matching in selectors.
196 * Some containers allow to further differentiate between regions by
197 * applying so-called pseudo-classes to the region. For example, the
198 * tab region in GtkNotebook allows to single out the first or last
199 * tab by using the :first-child or :last-child pseudo-class.
200 * When used in selectors, pseudo-classes must be prefixed with a
204 * Refer to the documentation of individual widgets to learn which
205 * regions and pseudo-classes they define and see
206 * <xref linkend="gtkstylecontext-classes"/> for a list of all regions
207 * used by GTK+ widgets.
210 * <title>Regions in selectors</title>
211 * <programlisting language="text">
212 * /* Theme any label within a notebook */
213 * GtkNotebook GtkLabel {
214 * color: #f90192;
217 * /* Theme labels within notebook tabs */
218 * GtkNotebook tab GtkLabel {
219 * color: #703910;
222 * /* Theme labels in the any first notebook
223 * tab, both selectors are equivalent */
224 * GtkNotebook tab:nth-child(first) GtkLabel,
225 * GtkNotebook tab:first-child GtkLabel {
226 * color: #89d012;
231 * Another use of pseudo-classes is to match widgets depending on their
232 * state. This is conceptually similar to the :hover, :active or :focus
233 * pseudo-classes in CSS. The available pseudo-classes for widget states
234 * are :active, :prelight (or :hover), :insensitive, :selected, :focused
238 * <title>Styling specific widget states</title>
239 * <programlisting language="text">
240 * /* Theme active (pressed) buttons */
242 * background-color: #0274d9;
245 * /* Theme buttons with the mouse pointer on it,
246 * both are equivalent */
248 * GtkButton:prelight {
249 * background-color: #3085a9;
252 * /* Theme insensitive widgets, both are equivalent */
255 * background-color: #320a91;
258 * /* Theme selection colors in entries */
259 * GtkEntry:selected {
260 * background-color: #56f9a0;
263 * /* Theme focused labels */
265 * background-color: #b4940f;
268 * /* Theme inconsistent checkbuttons */
269 * GtkCheckButton:inconsistent {
270 * background-color: #20395a;
275 * Widget state pseudoclasses may only apply to the last element
279 * To determine the effective style for a widget, all the matching rule
280 * sets are merged. As in CSS, rules apply by specificity, so the rules
281 * whose selectors more closely match a widget path will take precedence
285 * <refsect2 id="gtkcssprovider-rules">
286 * <title>@ Rules</title>
288 * GTK+'s CSS supports the @import rule, in order to load another
289 * CSS style sheet in addition to the currently parsed one.
292 * <title>Using the @import rule</title>
293 * <programlisting language="text">
294 * @import url ("path/to/common.css");
297 * <para id="css-binding-set">
298 * In order to extend key bindings affecting different widgets, GTK+
299 * supports the @binding-set rule to parse a set of bind/unbind
300 * directives, see #GtkBindingSet for the supported syntax. Note that
301 * the binding sets defined in this way must be associated with rule sets
302 * by setting the gtk-key-bindings style property.
305 * Customized key bindings are typically defined in a separate
306 * <filename>gtk-keys.css</filename> CSS file and GTK+ loads this file
307 * according to the current key theme, which is defined by the
308 * #GtkSettings:gtk-key-theme-name setting.
311 * <title>Using the @binding rule</title>
312 * <programlisting language="text">
313 * @binding-set binding-set1 {
314 * bind "<alt>Left" { "move-cursor" (visual-positions, -3, 0) };
318 * @binding-set binding-set2 {
319 * bind "<alt>Right" { "move-cursor" (visual-positions, 3, 0) };
320 * bind "<alt>KP_space" { "delete-from-cursor" (whitespace, 1)
321 * "insert-at-cursor" (" ") };
325 * gtk-key-bindings: binding-set1, binding-set2;
330 * GTK+ also supports an additional @define-color rule, in order
331 * to define a color name which may be used instead of color numeric
332 * representations. Also see the #GtkSettings:gtk-color-scheme setting
333 * for a way to override the values of these named colors.
336 * <title>Defining colors</title>
337 * <programlisting language="text">
338 * @define-color bg_color #f9a039;
341 * background-color: @bg_color;
346 * <refsect2 id="gtkcssprovider-symbolic-colors">
347 * <title>Symbolic colors</title>
349 * Besides being able to define color names, the CSS parser is also able
350 * to read different color expressions, which can also be nested, providing
351 * a rich language to define colors which are derived from a set of base
355 * <title>Using symbolic colors</title>
356 * <programlisting language="text">
357 * @define-color entry-color shade (@bg_color, 0.7);
360 * background-color: @entry-color;
364 * background-color: mix (@entry-color,
365 * shade (#fff, 0.5),
371 * The various ways to express colors in GTK+ CSS are:
377 * <entry>Syntax</entry>
378 * <entry>Explanation</entry>
379 * <entry>Examples</entry>
384 * <entry>rgb(@r, @g, @b)</entry>
385 * <entry>An opaque color; @r, @g, @b can be either integers between
386 * 0 and 255 or percentages</entry>
387 * <entry><literallayout>rgb(128, 10, 54)
388 * rgb(20%, 30%, 0%)</literallayout></entry>
391 * <entry>rgba(@r, @g, @b, @a)</entry>
392 * <entry>A translucent color; @r, @g, @b are as in the previous row,
393 * @a is a floating point number between 0 and 1</entry>
394 * <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
397 * <entry>#@xxyyzz</entry>
398 * <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
399 * specifying @r, @g, @b variants with between 1 and 4
400 * hexadecimal digits per component are allowed</entry>
401 * <entry><literallayout>#ff12ab
402 * #f0c</literallayout></entry>
405 * <entry>@name</entry>
406 * <entry>Reference to a color that has been defined with
407 * @define-color
409 * <entry>@bg_color</entry>
412 * <entry>mix(@color1, @color2, @f)</entry>
413 * <entry>A linear combination of @color1 and @color2. @f is a
414 * floating point number between 0 and 1.</entry>
415 * <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
418 * <entry>shade(@color, @f)</entry>
419 * <entry>A lighter or darker variant of @color. @f is a
420 * floating point number.
422 * <entry>shade(@fg_color, 0.5)</entry>
425 * <entry>lighter(@color)</entry>
426 * <entry>A lighter variant of @color</entry>
429 * <entry>darker(@color)</entry>
430 * <entry>A darker variant of @color</entry>
436 * <refsect2 id="gtkcssprovider-gradients">
437 * <title>Gradients</title>
439 * Linear or radial Gradients can be used as background images.
442 * A linear gradient along the line from (@start_x, @start_y) to
443 * (@end_x, @end_y) is specified using the syntax
444 * <literallayout>-gtk-gradient (linear,
445 * @start_x @start_y, @end_x @end_y,
446 * color-stop (@position, @color),
447 * ...)</literallayout>
448 * where @start_x and @end_x can be either a floating point number between
449 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
450 * and @end_y can be either a floating point number between 0 and 1 or one
451 * of the special values 'top', 'bottom' or 'center', @position is a floating
452 * point number between 0 and 1 and @color is a color expression (see above).
453 * The color-stop can be repeated multiple times to add more than one color
454 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
455 * color stops with position 0 and 1, respectively.
458 * <title>A linear gradient</title>
459 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
460 * <para>This gradient was specified with
461 * <literallayout>-gtk-gradient (linear,
462 * left top, right bottom,
463 * from(@yellow), to(@blue))</literallayout></para>
466 * <title>Another linear gradient</title>
467 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
468 * <para>This gradient was specified with
469 * <literallayout>-gtk-gradient (linear,
471 * color-stop(0, @yellow),
472 * color-stop(0.2, @blue),
473 * color-stop(1, #0f0))</literallayout></para>
476 * A radial gradient along the two circles defined by (@start_x, @start_y,
477 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
479 * <literallayout>-gtk-gradient (radial,
480 * @start_x @start_y, @start_radius,
481 * @end_x @end_y, @end_radius,
482 * color-stop (@position, @color),
483 * ...)</literallayout>
484 * where @start_radius and @end_radius are floating point numbers and
485 * the other parameters are as before.
488 * <title>A radial gradient</title>
489 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
490 * <para>This gradient was specified with
491 * <literallayout>-gtk-gradient (radial,
494 * from(@yellow), to(@green))</literallayout></para>
497 * <title>Another radial gradient</title>
498 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
499 * <para>This gradient was specified with
500 * <literallayout>-gtk-gradient (radial,
503 * color-stop (0, #f00),
504 * color-stop (0.1, #a0f),
505 * color-stop (0.2, @yellow),
506 * color-stop (1, @green))</literallayout></para>
509 * <refsect2 id="gtkcssprovider-shadows">
510 * <title>Text shadow</title>
512 * A shadow list can be applied to text or symbolic icons, using the CSS3
513 * text-shadow syntax, as defined in
514 * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
517 * A text shadow is specified using the syntax
518 * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
519 * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
520 * parameters. The optional blur radius is parsed, but it is currently not rendered by
521 * the GTK+ theming engine.
524 * To set multiple shadows on an element, you can specify a comma-separated list
525 * of shadow elements in the text-shadow property. Shadows are always rendered
526 * front-back, i.e. the first shadow specified is on top of the others. Shadows
527 * can thus overlay each other, but they can never overlay the text itself,
528 * which is always rendered on top of the shadow layer.
532 * <title>Box shadow</title>
534 * Themes can apply shadows on framed elements using the CSS3 box-shadow syntax,
536 * <ulink url="http://www.w3.org/TR/css3-background/#the-box-shadow">the CSS3 specification</ulink>.
539 * A box shadow is specified using the syntax
540 * <literallayout>box-shadow: [ @inset ] @horizontal_offset @vertical_offset [ @blur_radius ] [ @spread ] @color</literallayout>
541 * A positive offset will draw a shadow that is offset to the right (down) of the box,
542 * a negative offset to the left (top). The optional spread parameter defines an additional
543 * distance to expand the shadow shape in all directions, by the specified radius.
544 * The optional blur radius parameter is parsed, but it is currently not rendered by
545 * the GTK+ theming engine.
546 * The inset parameter defines whether the drop shadow should be rendered inside or outside
547 * the box canvas. Only inset box-shadows are currently supported by the GTK+ theming engine,
548 * non-inset elements are currently ignored.
551 * To set multiple box-shadows on an element, you can specify a comma-separated list
552 * of shadow elements in the box-shadow property. Shadows are always rendered
553 * front-back, i.e. the first shadow specified is on top of the others, so they may
554 * overlap other boxes or other shadows.
557 * <refsect2 id="gtkcssprovider-border-image">
558 * <title>Border images</title>
560 * Images and gradients can also be used in slices for the purpose of creating
562 * For more information, see the CSS3 documentation for the border-image property,
563 * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
565 * <inlinegraphic fileref="slices.png" format="PNG"/>
567 * The parameters of the slicing process are controlled by
568 * four separate properties. Note that you can use the
569 * <literallayout>border-image</literallayout> shorthand property
570 * to set values for the three properties at the same time.
573 * <literallayout>border-image-source: url(@path)
574 * (or border-image-source: -gtk-gradient(...))</literallayout>:
575 * Specifies the source of the border image, and it can either
576 * be an URL or a gradient (see above).
579 * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
580 * The sizes specified by the @top, @right, @bottom and @left parameters
581 * are the offsets, in pixels, from the relevant edge where the image
582 * should be "cut off" to build the slices used for the rendering
586 * <literallayout>border-image-width: @top @right @bottom @left</literallayout>
587 * The sizes specified by the @top, @right, @bottom and @left parameters
588 * are inward distances from the border box edge, used to specify the
589 * rendered size of each slice determined by border-image-slice.
590 * If this property is not specified, the values of border-width will
591 * be used as a fallback.
594 * <literallayout>border-image-repeat: [stretch|repeat|round|space] ?
595 * [stretch|repeat|round|space]</literallayout>
596 * Specifies how the image slices should be rendered in the area
597 * outlined by border-width.
598 * The default (stretch) is to resize the slice to fill in the whole
600 * If the value of this property is 'repeat', the image slice
601 * will be tiled to fill the area.
602 * If the value of this property is 'round', the image slice will
603 * be tiled to fill the area, and scaled to fit it exactly
604 * a whole number of times.
605 * If the value of this property is 'space', the image slice will
606 * be tiled to fill the area, and if it doesn't fit it exactly a whole
607 * number of times, the extra space is distributed as padding around
609 * If two options are specified, the first one affects
610 * the horizontal behaviour and the second one the vertical behaviour.
611 * If only one option is specified, it affects both.
614 * <title>A border image</title>
615 * <inlinegraphic fileref="border1.png" format="PNG"/>
616 * <para>This border image was specified with
617 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
621 * <title>A repeating border image</title>
622 * <inlinegraphic fileref="border2.png" format="PNG"/>
623 * <para>This border image was specified with
624 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
628 * <title>A stretched border image</title>
629 * <inlinegraphic fileref="border3.png" format="PNG"/>
630 * <para>This border image was specified with
631 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
635 * <refsect2 id="gtkcssprovider-transitions">
636 * <para>Styles can specify transitions that will be used to create a gradual
637 * change in the appearance when a widget state changes. The following
638 * syntax is used to specify transitions:
639 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
640 * The @duration is the amount of time that the animation will take for
641 * a complete cycle from start to end. If the loop option is given, the
642 * animation will be repated until the state changes again.
643 * The option after the duration determines the transition function from a
644 * small set of predefined functions.
645 * <figure><title>Linear transition</title>
646 * <graphic fileref="linear.png" format="PNG"/>
648 * <figure><title>Ease transition</title>
649 * <graphic fileref="ease.png" format="PNG"/>
651 * <figure><title>Ease-in-out transition</title>
652 * <graphic fileref="ease-in-out.png" format="PNG"/>
654 * <figure><title>Ease-in transition</title>
655 * <graphic fileref="ease-in.png" format="PNG"/>
657 * <figure><title>Ease-out transition</title>
658 * <graphic fileref="ease-out.png" format="PNG"/>
662 * <refsect2 id="gtkcssprovider-properties">
663 * <title>Supported properties</title>
665 * Properties are the part that differ the most to common CSS,
666 * not all properties are supported (some are planned to be
667 * supported eventually, some others are meaningless or don't
668 * map intuitively in a widget based environment).
671 * The currently supported properties are:
677 * <entry>Property name</entry>
678 * <entry>Syntax</entry>
679 * <entry>Maps to</entry>
680 * <entry>Examples</entry>
685 * <entry>engine</entry>
686 * <entry>engine-name</entry>
687 * <entry>#GtkThemingEngine</entry>
688 * <entry>engine: clearlooks;
689 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
692 * <entry>background-color</entry>
693 * <entry morerows="2">color (see above)</entry>
694 * <entry morerows="7">#GdkRGBA</entry>
695 * <entry morerows="7"><literallayout>background-color: #fff;
696 * color: &color1;
697 * background-color: shade (&color1, 0.5);
698 * color: mix (&color1, #f0f, 0.8);</literallayout>
702 * <entry>color</entry>
705 * <entry>border-top-color</entry>
706 * <entry morerows="4">transparent|color (see above)</entry>
709 * <entry>border-right-color</entry>
712 * <entry>border-bottom-color</entry>
715 * <entry>border-left-color</entry>
718 * <entry>border-color</entry>
719 * <entry>[transparent|color]{1,4}</entry>
722 * <entry>font-family</entry>
723 * <entry>@family [, @family]*</entry>
724 * <entry>#gchararray</entry>
725 * <entry>font-family: Sans, Arial;</entry>
728 * <entry>font-style</entry>
729 * <entry>[normal|oblique|italic]</entry>
730 * <entry>#PANGO_TYPE_STYLE</entry>
731 * <entry>font-style: italic;</entry>
734 * <entry>font-variant</entry>
735 * <entry>[normal|small-caps]</entry>
736 * <entry>#PANGO_TYPE_VARIANT</entry>
737 * <entry>font-variant: normal;</entry>
740 * <entry>font-weight</entry>
741 * <entry>[normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900]</entry>
742 * <entry>#PANGO_TYPE_WEIGHT</entry>
743 * <entry>font-weight: bold;</entry>
746 * <entry>font-size</entry>
747 * <entry>Font size in point</entry>
748 * <entry>#gint</entry>
749 * <entry>font-size: 13;</entry>
752 * <entry>font</entry>
753 * <entry>@family [@style] [@size]</entry>
754 * <entry>#PangoFontDescription</entry>
755 * <entry>font: Sans 15;</entry>
758 * <entry>margin-top</entry>
759 * <entry>integer</entry>
760 * <entry>#gint</entry>
761 * <entry>margin-top: 0;</entry>
764 * <entry>margin-left</entry>
765 * <entry>integer</entry>
766 * <entry>#gint</entry>
767 * <entry>margin-left: 1;</entry>
770 * <entry>margin-bottom</entry>
771 * <entry>integer</entry>
772 * <entry>#gint</entry>
773 * <entry>margin-bottom: 2;</entry>
776 * <entry>margin-right</entry>
777 * <entry>integer</entry>
778 * <entry>#gint</entry>
779 * <entry>margin-right: 4;</entry>
782 * <entry>margin</entry>
783 * <entry morerows="1"><literallayout>@width
784 * @vertical_width @horizontal_width
785 * @top_width @horizontal_width @bottom_width
786 * @top_width @right_width @bottom_width @left_width</literallayout>
788 * <entry morerows="1">#GtkBorder</entry>
789 * <entry morerows="1"><literallayout>margin: 5;
792 * margin: 5 10 3 5;</literallayout>
796 * <entry>padding-top</entry>
797 * <entry>integer</entry>
798 * <entry>#gint</entry>
799 * <entry>padding-top: 5;</entry>
802 * <entry>padding-left</entry>
803 * <entry>integer</entry>
804 * <entry>#gint</entry>
805 * <entry>padding-left: 5;</entry>
808 * <entry>padding-bottom</entry>
809 * <entry>integer</entry>
810 * <entry>#gint</entry>
811 * <entry>padding-bottom: 5;</entry>
814 * <entry>padding-right</entry>
815 * <entry>integer</entry>
816 * <entry>#gint</entry>
817 * <entry>padding-right: 5;</entry>
820 * <entry>padding</entry>
823 * <entry>background-image</entry>
824 * <entry><literallayout>gradient (see above) or
825 * url(@path)</literallayout></entry>
826 * <entry>#cairo_pattern_t</entry>
827 * <entry><literallayout>-gtk-gradient (linear,
828 * left top, right top,
829 * from (#fff), to (#000));
830 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
832 * color-stop (0.5, #f00),
834 * -gtk-gradient (radial,
835 * center center, 0.2,
836 * center center, 0.8,
837 * color-stop (0.0, #fff),
838 * color-stop (1.0, #000));
839 * url ('background.png');</literallayout>
843 * <entry>border-top-width</entry>
844 * <entry>integer</entry>
845 * <entry>#gint</entry>
846 * <entry>border-top-width: 5;</entry>
849 * <entry>border-left-width</entry>
850 * <entry>integer</entry>
851 * <entry>#gint</entry>
852 * <entry>border-left-width: 5;</entry>
855 * <entry>border-bottom-width</entry>
856 * <entry>integer</entry>
857 * <entry>#gint</entry>
858 * <entry>border-bottom-width: 5;</entry>
861 * <entry>border-right-width</entry>
862 * <entry>integer</entry>
863 * <entry>#gint</entry>
864 * <entry>border-right-width: 5;</entry>
867 * <entry>border-width</entry>
868 * <entry morerows="1">#GtkBorder</entry>
869 * <entry morerows="1"><literallayout>border-width: 1;
871 * border-width: 1 2 3;
872 * border-width: 1 2 3 5;</literallayout>
876 * <entry>border-radius</entry>
877 * <entry>integer</entry>
878 * <entry>#gint</entry>
879 * <entry>border-radius: 5;</entry>
882 * <entry>border-style</entry>
883 * <entry>[none|solid|inset|outset]</entry>
884 * <entry>#GtkBorderStyle</entry>
885 * <entry>border-style: solid;</entry>
888 * <entry>border-image</entry>
889 * <entry><literallayout>border image (see above)</literallayout></entry>
890 * <entry>internal use only</entry>
891 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
892 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
896 * <entry>text-shadow</entry>
897 * <entry>shadow list (see above)</entry>
898 * <entry>#GtkTextShadow</entry>
899 * <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
902 * <entry>transition</entry>
903 * <entry>transition (see above)</entry>
904 * <entry>internal use only</entry>
905 * <entry><literallayout>transition: 150ms ease-in-out;
906 * transition: 1s linear loop;</literallayout>
910 * <entry>gtk-key-bindings</entry>
911 * <entry>binding set name list</entry>
912 * <entry>internal use only</entry>
913 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
920 * GtkThemingEngines can register their own, engine-specific style properties
921 * with the function gtk_theming_engine_register_property(). These properties
922 * can be set in CSS like other properties, using a name of the form
923 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
924 * the name of the theming engine, and <replaceable>name</replaceable> is the
925 * name of the property. Style properties that have been registered by widgets
926 * using gtk_widget_class_install_style_property() can also be set in this
927 * way, using the widget class name for <replaceable>namespace</replaceable>.
930 * <title>Using engine-specific style properties</title>
933 * engine: clearlooks;
935 * -GtkPaned-handle-size: 6;
936 * -clearlooks-colorize-scrollbar: false;
943 typedef struct GtkCssRuleset GtkCssRuleset;
944 typedef struct _GtkCssScanner GtkCssScanner;
945 typedef enum ParserScope ParserScope;
946 typedef enum ParserSymbol ParserSymbol;
950 GtkCssSelector *selector;
951 GHashTable *widget_style;
954 guint has_inherit :1;
957 struct _GtkCssScanner
959 GtkCssProvider *provider;
960 GtkCssParser *parser;
961 GtkCssSection *section;
962 GtkCssScanner *parent;
968 struct _GtkCssProviderPrivate
972 GHashTable *symbolic_colors;
986 /* Extend GtkStateType, since these
987 * values are also used as symbols
990 /* Scope: pseudo-class */
991 SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
996 /* Scope: nth-child */
997 SYMBOL_NTH_CHILD_EVEN,
998 SYMBOL_NTH_CHILD_ODD,
999 SYMBOL_NTH_CHILD_FIRST,
1000 SYMBOL_NTH_CHILD_LAST
1008 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
1010 static void gtk_css_provider_finalize (GObject *object);
1011 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
1014 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
1015 GtkCssScanner *scanner,
1021 gtk_css_provider_error_quark (void)
1023 return g_quark_from_static_string ("gtk-css-provider-error-quark");
1026 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
1027 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
1028 gtk_css_style_provider_iface_init));
1031 gtk_css_provider_parsing_error (GtkCssProvider *provider,
1032 GtkCssSection *section,
1033 const GError *error)
1035 /* Only emit a warning when we have no error handlers. This is our
1036 * default handlers. And in this case erroneous CSS files are a bug
1037 * and should be fixed.
1038 * Note that these warnings can also be triggered by a broken theme
1039 * that people installed from some weird location on the internets.
1041 if (!g_signal_has_handler_pending (provider,
1042 css_provider_signals[PARSING_ERROR],
1050 file = gtk_css_section_get_file (section);
1053 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1056 path = g_file_info_get_display_name (info);
1058 path = "<broken file>";
1066 g_warning ("Theme parsing error: %s:%u:%u: %s",
1068 gtk_css_section_get_end_line (section) + 1,
1069 gtk_css_section_get_end_position (section),
1073 g_object_unref (info);
1078 gtk_css_provider_class_init (GtkCssProviderClass *klass)
1080 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1083 * GtkCssProvider::parsing-error:
1084 * @provider: the provider that had a parsing error
1085 * @section: section the error happened in
1086 * @error: The parsing error
1088 * Signals that a parsing error occured. the @path, @line and @position
1089 * describe the actual location of the error as accurately as possible.
1091 * Parsing errors are never fatal, so the parsing will resume after
1092 * the error. Errors may however cause parts of the given
1093 * data or even all of it to not be parsed at all. So it is a useful idea
1094 * to check that the parsing succeeds by connecting to this signal.
1096 * Note that this signal may be emitted at any time as the css provider
1097 * may opt to defer parsing parts or all of the input to a later time
1098 * than when a loading function was called.
1100 css_provider_signals[PARSING_ERROR] =
1101 g_signal_new (I_("parsing-error"),
1102 G_TYPE_FROM_CLASS (object_class),
1104 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
1106 _gtk_marshal_VOID__BOXED_BOXED,
1107 G_TYPE_NONE, 2, GTK_TYPE_CSS_SECTION, G_TYPE_ERROR);
1109 object_class->finalize = gtk_css_provider_finalize;
1111 klass->parsing_error = gtk_css_provider_parsing_error;
1113 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
1117 gtk_css_ruleset_init_copy (GtkCssRuleset *new,
1118 const GtkCssRuleset *ruleset,
1119 GtkCssSelector *selector)
1121 memcpy (new, ruleset, sizeof (GtkCssRuleset));
1123 new->selector = selector;
1124 if (new->widget_style)
1125 g_hash_table_ref (new->widget_style);
1127 g_hash_table_ref (new->style);
1131 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
1134 g_hash_table_unref (ruleset->style);
1135 if (ruleset->widget_style)
1136 g_hash_table_unref (ruleset->widget_style);
1137 if (ruleset->selector)
1138 _gtk_css_selector_free (ruleset->selector);
1140 memset (ruleset, 0, sizeof (GtkCssRuleset));
1143 typedef struct _PropertyValue PropertyValue;
1144 struct _PropertyValue {
1145 GtkCssSection *section;
1149 static PropertyValue *
1150 property_value_new (GtkCssSection *section)
1152 PropertyValue *value;
1154 value = g_slice_new0 (PropertyValue);
1156 value->section = gtk_css_section_ref (section);
1162 property_value_free (PropertyValue *value)
1164 if (G_IS_VALUE (&value->value))
1165 g_value_unset (&value->value);
1167 gtk_css_section_unref (value->section);
1169 g_slice_free (PropertyValue, value);
1173 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
1175 PropertyValue *value)
1177 if (ruleset->widget_style == NULL)
1178 ruleset->widget_style = g_hash_table_new_full (g_str_hash,
1180 (GDestroyNotify) g_free,
1181 (GDestroyNotify) property_value_free);
1183 g_hash_table_insert (ruleset->widget_style, name, value);
1187 gtk_css_ruleset_add (GtkCssRuleset *ruleset,
1188 const GtkStyleProperty *prop,
1189 PropertyValue *value)
1191 if (ruleset->style == NULL)
1192 ruleset->style = g_hash_table_new_full (g_direct_hash,
1195 (GDestroyNotify) property_value_free);
1197 if (_gtk_style_property_is_shorthand (prop))
1199 GParameter *parameters;
1200 guint i, n_parameters;
1202 parameters = _gtk_style_property_unpack (prop, &value->value, &n_parameters);
1204 for (i = 0; i < n_parameters; i++)
1206 const GtkStyleProperty *child;
1209 child = _gtk_style_property_lookup (parameters[i].name);
1210 val = property_value_new (value->section);
1211 memcpy (&val->value, ¶meters[i].value, sizeof (GValue));
1212 gtk_css_ruleset_add (ruleset, child, val);
1214 g_free (parameters);
1215 property_value_free (value);
1219 ruleset->has_inherit |= _gtk_style_property_is_inherit (prop);
1220 g_hash_table_insert (ruleset->style, (gpointer) prop, value);
1224 gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
1225 GtkWidgetPath *path,
1228 return _gtk_css_selector_matches (ruleset->selector, path, length);
1232 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1234 g_object_unref (scanner->provider);
1236 g_object_unref (scanner->file);
1237 g_object_unref (scanner->base);
1238 _gtk_css_parser_free (scanner->parser);
1240 g_slice_free (GtkCssScanner, scanner);
1244 gtk_css_provider_emit_error (GtkCssProvider *provider,
1245 GtkCssScanner *scanner,
1246 const GError *error)
1248 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1249 scanner->section, error);
1253 gtk_css_scanner_parser_error (GtkCssParser *parser,
1254 const GError *error,
1257 GtkCssScanner *scanner = user_data;
1259 gtk_css_provider_emit_error (scanner->provider,
1264 static GtkCssScanner *
1265 gtk_css_scanner_new (GtkCssProvider *provider,
1266 GtkCssScanner *parent,
1267 GtkCssSection *section,
1271 GtkCssScanner *scanner;
1273 scanner = g_slice_new0 (GtkCssScanner);
1275 g_object_ref (provider);
1276 scanner->provider = provider;
1277 scanner->parent = parent;
1279 scanner->section = gtk_css_section_ref (section);
1283 scanner->file = g_object_ref (file);
1284 scanner->base = g_file_get_parent (file);
1288 char *dir = g_get_current_dir ();
1289 scanner->base = g_file_new_for_path (dir);
1293 scanner->parser = _gtk_css_parser_new (text,
1294 gtk_css_scanner_parser_error,
1301 gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
1303 return scanner->base;
1307 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1312 if (scanner->file && g_file_equal (scanner->file, file))
1315 scanner = scanner->parent;
1322 gtk_css_scanner_push_section (GtkCssScanner *scanner,
1323 GtkCssSectionType section_type)
1325 GtkCssSection *section;
1327 section = _gtk_css_section_new (scanner->section,
1332 if (scanner->section)
1333 gtk_css_section_unref (scanner->section);
1334 scanner->section = section;
1338 gtk_css_scanner_pop_section (GtkCssScanner *scanner,
1339 GtkCssSectionType check_type)
1341 GtkCssSection *parent;
1343 g_assert (gtk_css_section_get_section_type (scanner->section) == check_type);
1345 parent = gtk_css_section_get_parent (scanner->section);
1347 gtk_css_section_ref (parent);
1349 _gtk_css_section_end (scanner->section);
1350 gtk_css_section_unref (scanner->section);
1352 scanner->section = parent;
1356 gtk_css_provider_init (GtkCssProvider *css_provider)
1358 GtkCssProviderPrivate *priv;
1360 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1361 GTK_TYPE_CSS_PROVIDER,
1362 GtkCssProviderPrivate);
1364 priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1366 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1367 (GDestroyNotify) g_free,
1368 (GDestroyNotify) gtk_symbolic_color_unref);
1372 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1373 GtkStyleProperties *props)
1375 GtkCssProviderPrivate *priv;
1376 GHashTableIter iter;
1377 gpointer key, value;
1379 priv = css_provider->priv;
1380 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1382 while (g_hash_table_iter_next (&iter, &key, &value))
1385 GtkSymbolicColor *color;
1390 gtk_style_properties_map_color (props, name, color);
1394 static GtkStyleProperties *
1395 gtk_css_provider_get_style (GtkStyleProvider *provider,
1396 GtkWidgetPath *path)
1398 GtkCssProvider *css_provider;
1399 GtkCssProviderPrivate *priv;
1400 GtkStyleProperties *props;
1403 css_provider = GTK_CSS_PROVIDER (provider);
1404 priv = css_provider->priv;
1405 length = gtk_widget_path_length (path);
1406 props = gtk_style_properties_new ();
1408 css_provider_dump_symbolic_colors (css_provider, props);
1410 for (l = 1; l <= length; l++)
1412 for (i = 0; i < priv->rulesets->len; i++)
1414 GtkCssRuleset *ruleset;
1415 GHashTableIter iter;
1418 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1420 if (ruleset->style == NULL)
1423 if (l < length && (!ruleset->has_inherit || _gtk_css_selector_get_state_flags (ruleset->selector)))
1426 if (!gtk_css_ruleset_matches (ruleset, path, l))
1429 g_hash_table_iter_init (&iter, ruleset->style);
1431 while (g_hash_table_iter_next (&iter, &key, &val))
1433 GtkStyleProperty *prop = key;
1434 PropertyValue *value = val;
1436 if (l != length && !_gtk_style_property_is_inherit (prop))
1439 _gtk_style_properties_set_property_by_property (props,
1441 _gtk_css_selector_get_state_flags (ruleset->selector),
1451 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1452 GtkWidgetPath *path,
1453 GtkStateFlags state,
1457 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1458 GtkCssProviderPrivate *priv = css_provider->priv;
1460 gboolean found = FALSE;
1464 prop_name = g_strdup_printf ("-%s-%s",
1465 g_type_name (pspec->owner_type),
1468 for (i = priv->rulesets->len - 1; i >= 0; i--)
1470 GtkCssRuleset *ruleset;
1471 GtkStateFlags selector_state;
1473 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1475 if (ruleset->widget_style == NULL)
1478 if (!gtk_css_ruleset_matches (ruleset, path, gtk_widget_path_length (path)))
1481 selector_state = _gtk_css_selector_get_state_flags (ruleset->selector);
1482 val = g_hash_table_lookup (ruleset->widget_style, prop_name);
1485 (selector_state == 0 ||
1486 selector_state == state ||
1487 ((selector_state & state) != 0 &&
1488 (selector_state & ~(state)) == 0)))
1490 GtkCssScanner *scanner;
1492 scanner = gtk_css_scanner_new (css_provider,
1495 gtk_css_section_get_file (val->section),
1496 g_value_get_string (&val->value));
1498 found = _gtk_style_property_parse_value (NULL,
1503 gtk_css_scanner_destroy (scanner);
1516 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1518 iface->get_style = gtk_css_provider_get_style;
1519 iface->get_style_property = gtk_css_provider_get_style_property;
1523 gtk_css_provider_finalize (GObject *object)
1525 GtkCssProvider *css_provider;
1526 GtkCssProviderPrivate *priv;
1529 css_provider = GTK_CSS_PROVIDER (object);
1530 priv = css_provider->priv;
1532 for (i = 0; i < priv->rulesets->len; i++)
1533 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1535 g_array_free (priv->rulesets, TRUE);
1537 if (priv->symbolic_colors)
1538 g_hash_table_destroy (priv->symbolic_colors);
1540 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1544 * gtk_css_provider_new:
1546 * Returns a newly created #GtkCssProvider.
1548 * Returns: A new #GtkCssProvider
1551 gtk_css_provider_new (void)
1553 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1557 gtk_css_provider_take_error (GtkCssProvider *provider,
1558 GtkCssScanner *scanner,
1561 gtk_css_provider_emit_error (scanner->provider,
1565 g_error_free (error);
1569 gtk_css_provider_error_literal (GtkCssProvider *provider,
1570 GtkCssScanner *scanner,
1573 const char *message)
1575 gtk_css_provider_take_error (provider,
1577 g_error_new_literal (domain, code, message));
1581 gtk_css_provider_error (GtkCssProvider *provider,
1582 GtkCssScanner *scanner,
1586 ...) G_GNUC_PRINTF (5, 6);
1588 gtk_css_provider_error (GtkCssProvider *provider,
1589 GtkCssScanner *scanner,
1598 va_start (args, format);
1599 error = g_error_new_valist (domain, code, format, args);
1602 gtk_css_provider_take_error (provider, scanner, error);
1606 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1607 GtkCssScanner *scanner,
1608 const char *expected)
1610 gtk_css_provider_error (provider,
1612 GTK_CSS_PROVIDER_ERROR,
1613 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1614 "expected a valid %s", expected);
1618 css_provider_commit (GtkCssProvider *css_provider,
1620 GtkCssRuleset *ruleset)
1622 GtkCssProviderPrivate *priv;
1625 priv = css_provider->priv;
1627 if (ruleset->style == NULL && ruleset->widget_style == NULL)
1629 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1633 for (l = selectors; l; l = l->next)
1637 gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1639 g_array_append_val (priv->rulesets, new);
1642 g_slist_free (selectors);
1646 gtk_css_provider_reset (GtkCssProvider *css_provider)
1648 GtkCssProviderPrivate *priv;
1651 priv = css_provider->priv;
1653 g_hash_table_remove_all (priv->symbolic_colors);
1655 for (i = 0; i < priv->rulesets->len; i++)
1656 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1657 g_array_set_size (priv->rulesets, 0);
1661 gtk_css_provider_propagate_error (GtkCssProvider *provider,
1662 GtkCssSection *section,
1663 const GError *error,
1664 GError **propagate_to)
1671 file = gtk_css_section_get_file (section);
1674 info = g_file_query_info (file,G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1677 path = g_file_info_get_display_name (info);
1679 path = "<broken file>";
1687 /* don't fail for deprecations */
1688 if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
1690 g_warning ("Theme parsing error: %s:%u:%u: %s", path,
1691 gtk_css_section_get_end_line (section) + 1,
1692 gtk_css_section_get_end_position (section), error->message);
1696 /* we already set an error. And we'd like to keep the first one */
1700 *propagate_to = g_error_copy (error);
1701 g_prefix_error (propagate_to, "%s:%u:%u: ", path,
1702 gtk_css_section_get_end_line (section) + 1,
1703 gtk_css_section_get_end_position (section));
1706 g_object_unref (info);
1710 parse_import (GtkCssScanner *scanner)
1715 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_IMPORT);
1717 if (!_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1719 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1723 if (_gtk_css_parser_is_string (scanner->parser))
1724 uri = _gtk_css_parser_read_string (scanner->parser);
1726 uri = _gtk_css_parser_read_uri (scanner->parser);
1730 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1731 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1735 file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
1738 if (gtk_css_scanner_would_recurse (scanner, file))
1740 char *path = g_file_get_path (file);
1741 gtk_css_provider_error (scanner->provider,
1743 GTK_CSS_PROVIDER_ERROR,
1744 GTK_CSS_PROVIDER_ERROR_IMPORT,
1745 "Loading '%s' would recurse",
1751 gtk_css_provider_load_internal (scanner->provider,
1758 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1760 gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1761 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1764 g_object_unref (file);
1766 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1771 parse_color_definition (GtkCssScanner *scanner)
1773 GtkSymbolicColor *symbolic;
1776 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1778 if (!_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1780 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1784 name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1787 gtk_css_provider_error_literal (scanner->provider,
1789 GTK_CSS_PROVIDER_ERROR,
1790 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1791 "Not a valid color name");
1792 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1793 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1797 symbolic = _gtk_css_parser_read_symbolic_color (scanner->parser);
1798 if (symbolic == NULL)
1801 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1802 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1806 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1809 gtk_symbolic_color_unref (symbolic);
1810 gtk_css_provider_error_literal (scanner->provider,
1812 GTK_CSS_PROVIDER_ERROR,
1813 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1814 "Missing semicolon at end of color definition");
1815 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1817 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1821 g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
1823 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1828 parse_binding_set (GtkCssScanner *scanner)
1830 GtkBindingSet *binding_set;
1833 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1835 if (!_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
1837 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1841 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1844 gtk_css_provider_error_literal (scanner->provider,
1846 GTK_CSS_PROVIDER_ERROR,
1847 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1848 "Expected name for binding set");
1849 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1850 goto skip_semicolon;
1853 binding_set = gtk_binding_set_find (name);
1856 binding_set = gtk_binding_set_new (name);
1857 binding_set->parsed = TRUE;
1861 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
1863 gtk_css_provider_error_literal (scanner->provider,
1865 GTK_CSS_PROVIDER_ERROR,
1866 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1867 "Expected '{' for binding set");
1868 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1869 goto skip_semicolon;
1872 while (!_gtk_css_parser_is_eof (scanner->parser) &&
1873 !_gtk_css_parser_begins_with (scanner->parser, '}'))
1875 name = _gtk_css_parser_read_value (scanner->parser);
1878 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1882 if (gtk_binding_entry_add_signal_from_string (binding_set, name) != G_TOKEN_NONE)
1884 gtk_css_provider_error_literal (scanner->provider,
1886 GTK_CSS_PROVIDER_ERROR,
1887 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1888 "Failed to parse binding set.");
1893 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1895 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
1896 !_gtk_css_parser_is_eof (scanner->parser))
1898 gtk_css_provider_error_literal (scanner->provider,
1900 GTK_CSS_PROVIDER_ERROR,
1901 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1902 "Expected semicolon");
1903 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1908 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
1910 gtk_css_provider_error_literal (scanner->provider,
1912 GTK_CSS_PROVIDER_ERROR,
1913 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1914 "expected '}' after declarations");
1915 if (!_gtk_css_parser_is_eof (scanner->parser))
1916 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
1920 if (_gtk_css_parser_begins_with (scanner->parser, ';'))
1922 gtk_css_provider_error_literal (scanner->provider,
1924 GTK_CSS_PROVIDER_ERROR,
1925 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
1926 "Nonstandard semicolon at end of binding set");
1927 _gtk_css_parser_try (scanner->parser, ";", TRUE);
1930 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1936 parse_at_keyword (GtkCssScanner *scanner)
1938 if (parse_import (scanner))
1940 if (parse_color_definition (scanner))
1942 if (parse_binding_set (scanner))
1947 gtk_css_provider_error_literal (scanner->provider,
1949 GTK_CSS_PROVIDER_ERROR,
1950 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1952 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1957 parse_selector_class (GtkCssScanner *scanner, GArray *classes)
1962 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1966 gtk_css_provider_error_literal (scanner->provider,
1968 GTK_CSS_PROVIDER_ERROR,
1969 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1970 "Expected a valid name for class");
1974 qname = g_quark_from_string (name);
1975 g_array_append_val (classes, qname);
1981 parse_selector_name (GtkCssScanner *scanner, GArray *names)
1986 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1990 gtk_css_provider_error_literal (scanner->provider,
1992 GTK_CSS_PROVIDER_ERROR,
1993 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1994 "Expected a valid name for id");
1998 qname = g_quark_from_string (name);
1999 g_array_append_val (names, qname);
2005 parse_selector_pseudo_class (GtkCssScanner *scanner,
2006 GtkRegionFlags *region_to_modify,
2007 GtkStateFlags *state_to_modify)
2011 GtkRegionFlags region_flag;
2012 GtkStateFlags state_flag;
2013 } pseudo_classes[] = {
2014 { "first-child", GTK_REGION_FIRST, 0 },
2015 { "last-child", GTK_REGION_LAST, 0 },
2016 { "sorted", GTK_REGION_SORTED, 0 },
2017 { "active", 0, GTK_STATE_FLAG_ACTIVE },
2018 { "prelight", 0, GTK_STATE_FLAG_PRELIGHT },
2019 { "hover", 0, GTK_STATE_FLAG_PRELIGHT },
2020 { "selected", 0, GTK_STATE_FLAG_SELECTED },
2021 { "insensitive", 0, GTK_STATE_FLAG_INSENSITIVE },
2022 { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
2023 { "focused", 0, GTK_STATE_FLAG_FOCUSED },
2024 { "focus", 0, GTK_STATE_FLAG_FOCUSED },
2026 }, nth_child_classes[] = {
2027 { "first", GTK_REGION_FIRST, 0 },
2028 { "last", GTK_REGION_LAST, 0 },
2029 { "even", GTK_REGION_EVEN, 0 },
2030 { "odd", GTK_REGION_ODD, 0 },
2036 name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
2039 gtk_css_provider_error_literal (scanner->provider,
2041 GTK_CSS_PROVIDER_ERROR,
2042 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2043 "Missing name of pseudo-class");
2047 if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
2049 char *function = name;
2051 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2052 if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
2054 gtk_css_provider_error_literal (scanner->provider,
2056 GTK_CSS_PROVIDER_ERROR,
2057 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2058 "Missing closing bracket for pseudo-class");
2062 if (g_ascii_strcasecmp (function, "nth-child") != 0)
2064 gtk_css_provider_error (scanner->provider,
2066 GTK_CSS_PROVIDER_ERROR,
2067 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2068 "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
2078 gtk_css_provider_error (scanner->provider,
2080 GTK_CSS_PROVIDER_ERROR,
2081 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2082 "nth-child() requires an argument");
2086 classes = nth_child_classes;
2089 classes = pseudo_classes;
2091 for (i = 0; classes[i].name != NULL; i++)
2093 if (g_ascii_strcasecmp (name, classes[i].name) == 0)
2095 if ((*region_to_modify & classes[i].region_flag) ||
2096 (*state_to_modify & classes[i].state_flag))
2098 if (classes == nth_child_classes)
2099 gtk_css_provider_error (scanner->provider,
2101 GTK_CSS_PROVIDER_ERROR,
2102 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2103 "Duplicate pseudo-class 'nth-child(%s)'", name);
2105 gtk_css_provider_error (scanner->provider,
2107 GTK_CSS_PROVIDER_ERROR,
2108 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2109 "Duplicate pseudo-class '%s'", name);
2111 *region_to_modify |= classes[i].region_flag;
2112 *state_to_modify |= classes[i].state_flag;
2119 if (classes == nth_child_classes)
2120 gtk_css_provider_error (scanner->provider,
2122 GTK_CSS_PROVIDER_ERROR,
2123 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2124 "Unknown pseudo-class 'nth-child(%s)'", name);
2126 gtk_css_provider_error (scanner->provider,
2128 GTK_CSS_PROVIDER_ERROR,
2129 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2130 "Unknown pseudo-class '%s'", name);
2136 parse_simple_selector (GtkCssScanner *scanner,
2140 GtkRegionFlags *pseudo_classes,
2141 GtkStateFlags *state)
2143 gboolean parsed_something;
2145 *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
2147 parsed_something = TRUE;
2149 parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
2152 if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
2154 if (!parse_selector_name (scanner, ids))
2157 else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
2159 if (!parse_selector_class (scanner, classes))
2162 else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
2164 if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
2167 else if (!parsed_something)
2169 gtk_css_provider_error_literal (scanner->provider,
2171 GTK_CSS_PROVIDER_ERROR,
2172 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2173 "Expected a valid selector");
2179 parsed_something = TRUE;
2181 while (!_gtk_css_parser_is_eof (scanner->parser));
2183 _gtk_css_parser_skip_whitespace (scanner->parser);
2187 static GtkCssSelector *
2188 parse_selector (GtkCssScanner *scanner)
2190 GtkCssSelector *selector = NULL;
2194 GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
2195 GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
2196 GtkRegionFlags pseudo_classes = 0;
2197 GtkStateFlags state = 0;
2198 GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
2202 if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
2203 combine = GTK_CSS_COMBINE_CHILD;
2206 if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
2208 g_array_free (ids, TRUE);
2209 g_array_free (classes, TRUE);
2211 _gtk_css_selector_free (selector);
2215 selector = _gtk_css_selector_new (selector,
2218 (GQuark *) g_array_free (ids, ids->len == 0),
2219 (GQuark *) g_array_free (classes, classes->len == 0),
2224 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2225 !_gtk_css_parser_begins_with (scanner->parser, ',') &&
2226 !_gtk_css_parser_begins_with (scanner->parser, '{'));
2232 parse_selector_list (GtkCssScanner *scanner)
2234 GSList *selectors = NULL;
2236 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_SELECTOR);
2239 GtkCssSelector *select = parse_selector (scanner);
2243 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2244 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2245 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2249 selectors = g_slist_prepend (selectors, select);
2251 while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
2253 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2259 parse_declaration (GtkCssScanner *scanner,
2260 GtkCssRuleset *ruleset)
2262 const GtkStyleProperty *property;
2265 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DECLARATION);
2267 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2269 goto check_for_semicolon;
2271 property = _gtk_style_property_lookup (name);
2272 if (property == NULL && name[0] != '-')
2274 gtk_css_provider_error (scanner->provider,
2276 GTK_CSS_PROVIDER_ERROR,
2277 GTK_CSS_PROVIDER_ERROR_NAME,
2278 "'%s' is not a valid property name",
2280 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2282 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2286 if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
2288 gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
2289 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2291 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2301 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2303 val = property_value_new (scanner->section);
2304 g_value_init (&val->value, property->pspec->value_type);
2306 if (_gtk_style_property_parse_value (property,
2309 gtk_css_scanner_get_base_url (scanner)))
2311 if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
2312 _gtk_css_parser_begins_with (scanner->parser, '}') ||
2313 _gtk_css_parser_is_eof (scanner->parser))
2315 gtk_css_ruleset_add (ruleset, property, val);
2319 gtk_css_provider_error_literal (scanner->provider,
2321 GTK_CSS_PROVIDER_ERROR,
2322 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2323 "Junk at end of value");
2324 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2325 property_value_free (val);
2326 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2327 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2333 property_value_free (val);
2334 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2335 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2336 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2339 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2341 else if (name[0] == '-')
2345 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2347 value_str = _gtk_css_parser_read_value (scanner->parser);
2352 val = property_value_new (scanner->section);
2353 g_value_init (&val->value, G_TYPE_STRING);
2354 g_value_take_string (&val->value, value_str);
2356 gtk_css_ruleset_add_style (ruleset, name, val);
2360 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2361 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2362 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2366 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2371 check_for_semicolon:
2372 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2374 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2376 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2377 !_gtk_css_parser_is_eof (scanner->parser))
2379 gtk_css_provider_error_literal (scanner->provider,
2381 GTK_CSS_PROVIDER_ERROR,
2382 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2383 "Expected semicolon");
2384 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2390 parse_declarations (GtkCssScanner *scanner,
2391 GtkCssRuleset *ruleset)
2393 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2394 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2396 parse_declaration (scanner, ruleset);
2401 parse_ruleset (GtkCssScanner *scanner)
2404 GtkCssRuleset ruleset = { 0, };
2406 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_RULESET);
2408 selectors = parse_selector_list (scanner);
2409 if (selectors == NULL)
2411 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2415 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2417 gtk_css_provider_error_literal (scanner->provider,
2419 GTK_CSS_PROVIDER_ERROR,
2420 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2421 "expected '{' after selectors");
2422 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2423 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2424 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2428 parse_declarations (scanner, &ruleset);
2430 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2432 gtk_css_provider_error_literal (scanner->provider,
2434 GTK_CSS_PROVIDER_ERROR,
2435 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2436 "expected '}' after declarations");
2437 if (!_gtk_css_parser_is_eof (scanner->parser))
2439 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2440 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2441 gtk_css_ruleset_clear (&ruleset);
2442 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2446 css_provider_commit (scanner->provider, selectors, &ruleset);
2447 gtk_css_ruleset_clear (&ruleset);
2448 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2452 parse_statement (GtkCssScanner *scanner)
2454 if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2455 parse_at_keyword (scanner);
2457 parse_ruleset (scanner);
2461 parse_stylesheet (GtkCssScanner *scanner)
2463 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2465 _gtk_css_parser_skip_whitespace (scanner->parser);
2467 while (!_gtk_css_parser_is_eof (scanner->parser))
2469 if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2470 _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2473 parse_statement (scanner);
2476 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2480 gtk_css_provider_compare_rule (gconstpointer a_,
2483 const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2484 const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2487 compare = _gtk_css_selector_compare (a->selector, b->selector);
2491 /* compare pointers in array to ensure a stable sort */
2502 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2504 GtkCssProviderPrivate *priv = css_provider->priv;
2506 g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2510 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2511 GtkCssScanner *parent,
2516 GtkCssScanner *scanner;
2517 gulong error_handler;
2518 char *free_data = NULL;
2521 error_handler = g_signal_connect (css_provider,
2523 G_CALLBACK (gtk_css_provider_propagate_error),
2526 error_handler = 0; /* silence gcc */
2530 GError *load_error = NULL;
2532 if (g_file_load_contents (file, NULL,
2540 GtkCssSection *section;
2543 section = gtk_css_section_ref (parent->section);
2545 section = _gtk_css_section_new_for_file (GTK_CSS_SECTION_DOCUMENT, file);
2547 gtk_css_provider_error (css_provider,
2549 GTK_CSS_PROVIDER_ERROR,
2550 GTK_CSS_PROVIDER_ERROR_IMPORT,
2551 "Failed to import: %s",
2552 load_error->message);
2554 gtk_css_section_unref (section);
2560 scanner = gtk_css_scanner_new (css_provider,
2562 parent ? parent->section : NULL,
2566 parse_stylesheet (scanner);
2568 gtk_css_scanner_destroy (scanner);
2571 gtk_css_provider_postprocess (css_provider);
2578 g_signal_handler_disconnect (css_provider, error_handler);
2582 /* We clear all contents from the provider for backwards compat reasons */
2583 gtk_css_provider_reset (css_provider);
2592 * gtk_css_provider_load_from_data:
2593 * @css_provider: a #GtkCssProvider
2594 * @data: (array length=length) (element-type guint8): CSS data loaded in memory
2595 * @length: the length of @data in bytes, or -1 for NUL terminated strings. If
2596 * @length is not -1, the code will assume it is not NUL terminated and will
2597 * potentially do a copy.
2598 * @error: (out) (allow-none): return location for a #GError, or %NULL
2600 * Loads @data into @css_provider, making it clear any previously loaded
2603 * Returns: %TRUE if the data could be loaded.
2606 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2614 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2615 g_return_val_if_fail (data != NULL, FALSE);
2619 length = strlen (data);
2624 free_data = g_strndup (data, length);
2628 gtk_css_provider_reset (css_provider);
2630 ret = gtk_css_provider_load_internal (css_provider, NULL, NULL, data, error);
2638 * gtk_css_provider_load_from_file:
2639 * @css_provider: a #GtkCssProvider
2640 * @file: #GFile pointing to a file to load
2641 * @error: (out) (allow-none): return location for a #GError, or %NULL
2643 * Loads the data contained in @file into @css_provider, making it
2644 * clear any previously loaded information.
2646 * Returns: %TRUE if the data could be loaded.
2649 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2653 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2654 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2656 gtk_css_provider_reset (css_provider);
2658 return gtk_css_provider_load_internal (css_provider, NULL, file, NULL, error);
2662 * gtk_css_provider_load_from_path:
2663 * @css_provider: a #GtkCssProvider
2664 * @path: the path of a filename to load, in the GLib filename encoding
2665 * @error: (out) (allow-none): return location for a #GError, or %NULL
2667 * Loads the data contained in @path into @css_provider, making it clear
2668 * any previously loaded information.
2670 * Returns: %TRUE if the data could be loaded.
2673 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2680 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2681 g_return_val_if_fail (path != NULL, FALSE);
2683 file = g_file_new_for_path (path);
2685 result = gtk_css_provider_load_from_file (css_provider, file, error);
2687 g_object_unref (file);
2693 * gtk_css_provider_get_default:
2695 * Returns the provider containing the style settings used as a
2696 * fallback for all widgets.
2698 * Returns: (transfer none): The provider used for fallback styling.
2699 * This memory is owned by GTK+, and you must not free it.
2702 gtk_css_provider_get_default (void)
2704 static GtkCssProvider *provider;
2706 if (G_UNLIKELY (!provider))
2709 "@define-color fg_color #000; \n"
2710 "@define-color bg_color #dcdad5; \n"
2711 "@define-color text_color #000; \n"
2712 "@define-color base_color #fff; \n"
2713 "@define-color selected_bg_color #4b6983; \n"
2714 "@define-color selected_fg_color #fff; \n"
2715 "@define-color tooltip_bg_color #eee1b3; \n"
2716 "@define-color tooltip_fg_color #000; \n"
2717 "@define-color placeholder_text_color #808080; \n"
2719 "@define-color info_fg_color rgb (181, 171, 156);\n"
2720 "@define-color info_bg_color rgb (252, 252, 189);\n"
2721 "@define-color warning_fg_color rgb (173, 120, 41);\n"
2722 "@define-color warning_bg_color rgb (250, 173, 61);\n"
2723 "@define-color question_fg_color rgb (97, 122, 214);\n"
2724 "@define-color question_bg_color rgb (138, 173, 212);\n"
2725 "@define-color error_fg_color rgb (166, 38, 38);\n"
2726 "@define-color error_bg_color rgb (237, 54, 54);\n"
2729 " background-color: @bg_color;\n"
2730 " color: @fg_color;\n"
2731 " border-color: shade (@bg_color, 0.6);\n"
2733 " border-width: 0;\n"
2737 " background-color: shade (@bg_color, 1.05);\n"
2738 " color: shade (@fg_color, 1.3);\n"
2742 " background-color: @selected_bg_color;\n"
2743 " color: @selected_fg_color;\n"
2746 ".expander, GtkTreeView.view.expander {\n"
2750 ".expander:prelight,\n"
2751 "GtkTreeView.view.expander:selected:prelight {\n"
2752 " color: @text_color;\n"
2755 ".expander:active {\n"
2756 " transition: 200ms linear;\n"
2760 " border-color: shade (@bg_color, 0.7);\n"
2761 " background-color: shade (@bg_color, 0.9);\n"
2762 " color: shade (@bg_color, 0.7);\n"
2766 " border-width: 0;\n"
2767 " border-radius: 0;\n"
2768 " background-color: @base_color;\n"
2769 " color: @text_color;\n"
2771 ".view:selected {\n"
2772 " background-color: shade (@bg_color, 0.9);\n"
2773 " color: @fg_color;\n"
2776 ".view:selected:focused {\n"
2777 " background-color: @selected_bg_color;\n"
2778 " color: @selected_fg_color;\n"
2781 ".view column:sorted row,\n"
2782 ".view column:sorted row:prelight {\n"
2783 " background-color: shade (@bg_color, 0.85);\n"
2786 ".view column:sorted row:nth-child(odd),\n"
2787 ".view column:sorted row:nth-child(odd):prelight {\n"
2788 " background-color: shade (@bg_color, 0.8);\n"
2792 ".view row:prelight {\n"
2793 " background-color: @base_color;\n"
2794 " color: @text_color;\n"
2797 ".view row:nth-child(odd),\n"
2798 ".view row:nth-child(odd):prelight {\n"
2799 " background-color: shade (@base_color, 0.93); \n"
2802 ".view row:selected:focused {\n"
2803 " background-color: @selected_bg_color;\n"
2806 ".view row:selected {\n"
2807 " background-color: darker (@bg_color);\n"
2808 " color: @selected_fg_color;\n"
2811 ".view.cell.trough,\n"
2812 ".view.cell.trough:hover,\n"
2813 ".view.cell.trough:selected,\n"
2814 ".view.cell.trough:selected:focused {\n"
2815 " background-color: @bg_color;\n"
2816 " color: @fg_color;\n"
2819 ".view.cell.progressbar,\n"
2820 ".view.cell.progressbar:hover,\n"
2821 ".view.cell.progressbar:selected,\n"
2822 ".view.cell.progressbar:selected:focused {\n"
2823 " background-color: @selected_bg_color;\n"
2824 " color: @selected_fg_color;\n"
2828 " background-color: alpha (@fg_color, 0.25);\n"
2829 " border-color: @fg_color;\n"
2830 " border-style: solid;\n"
2831 " border-width: 1;\n"
2836 " background-color: @tooltip_bg_color; \n"
2837 " color: @tooltip_fg_color; \n"
2838 " border-color: @tooltip_fg_color; \n"
2839 " border-width: 1;\n"
2840 " border-style: solid;\n"
2845 " border-style: outset; \n"
2846 " border-width: 2; \n"
2849 ".button:active {\n"
2850 " background-color: shade (@bg_color, 0.7);\n"
2851 " border-style: inset; \n"
2854 ".button:prelight,\n"
2855 ".slider:prelight {\n"
2856 " background-color: @selected_bg_color;\n"
2857 " color: @selected_fg_color;\n"
2858 " border-color: shade (@selected_bg_color, 0.7);\n"
2862 " background-color: darker (@bg_color);\n"
2863 " border-style: inset;\n"
2864 " border-width: 1;\n"
2869 " border-style: inset;\n"
2870 " border-width: 2;\n"
2871 " background-color: @base_color;\n"
2872 " color: @text_color;\n"
2875 ".entry:insensitive {\n"
2876 " background-color: shade (@base_color, 0.9);\n"
2877 " color: shade (@base_color, 0.7);\n"
2880 " background-color: #c4c2bd;\n"
2885 ".entry.progressbar, \n"
2886 ".cell.progressbar {\n"
2887 " background-color: @selected_bg_color;\n"
2888 " border-color: shade (@selected_bg_color, 0.7);\n"
2889 " color: @selected_fg_color;\n"
2890 " border-style: outset;\n"
2891 " border-width: 1;\n"
2894 "GtkCheckButton:hover,\n"
2895 "GtkCheckButton:selected,\n"
2896 "GtkRadioButton:hover,\n"
2897 "GtkRadioButton:selected {\n"
2898 " background-color: shade (@bg_color, 1.05);\n"
2902 ".cell.check, .cell.radio,\n"
2903 ".cell.check:hover, .cell.radio:hover {\n"
2904 " border-style: solid;\n"
2905 " border-width: 1;\n"
2906 " background-color: @base_color;\n"
2907 " border-color: @fg_color;\n"
2910 ".check:active, .radio:active,\n"
2911 ".check:hover, .radio:hover {\n"
2912 " background-color: @base_color;\n"
2913 " border-color: @fg_color;\n"
2914 " color: @text_color;\n"
2917 ".check:selected, .radio:selected {\n"
2918 " background-color: darker (@bg_color);\n"
2919 " color: @selected_fg_color;\n"
2920 " border-color: @selected_fg_color;\n"
2923 ".check:selected:focused, .radio:selected:focused {\n"
2924 " background-color: @selected_bg_color;\n"
2927 ".menuitem.check, .menuitem.radio {\n"
2928 " color: @fg_color;\n"
2929 " border-style: none;\n"
2930 " border-width: 0;\n"
2934 " border-style: outset;\n"
2935 " border-width: 1;\n"
2939 " border-style: inset;\n"
2940 " border-width: 2;\n"
2944 " border-style: outset;\n"
2945 " border-width: 1;\n"
2949 " border-style: inset;\n"
2950 " border-width: 1;\n"
2953 "GtkScrolledWindow.frame {\n"
2960 " border-style: outset;\n"
2961 " border-width: 1;\n"
2966 ".menuitem:hover,\n"
2967 ".menuitem.check:hover,\n"
2968 ".menuitem.radio:hover {\n"
2969 " background-color: @selected_bg_color;\n"
2970 " color: @selected_fg_color;\n"
2973 "GtkSpinButton.button {\n"
2974 " border-width: 1;\n"
2977 ".scale.slider:hover,\n"
2978 "GtkSpinButton.button:hover {\n"
2979 " background-color: shade (@bg_color, 1.05);\n"
2980 " border-color: shade (@bg_color, 0.8);\n"
2983 "GtkSwitch.trough:active {\n"
2984 " background-color: @selected_bg_color;\n"
2985 " color: @selected_fg_color;\n"
2988 "GtkToggleButton.button:inconsistent {\n"
2989 " border-style: outset;\n"
2990 " border-width: 1px;\n"
2991 " background-color: shade (@bg_color, 0.9);\n"
2992 " border-color: shade (@bg_color, 0.7);\n"
2995 "GtkLabel:selected {\n"
2996 " background-color: shade (@bg_color, 0.9);\n"
2999 "GtkLabel:selected:focused {\n"
3000 " background-color: @selected_bg_color;\n"
3003 ".spinner:active {\n"
3004 " transition: 750ms linear loop;\n"
3008 " background-color: @info_bg_color;\n"
3009 " color: @info_fg_color;\n"
3013 " background-color: @warning_bg_color;\n"
3014 " color: @warning_fg_color;\n"
3018 " background-color: @question_bg_color;\n"
3019 " color: @question_fg_color;\n"
3023 " background-color: @error_bg_color;\n"
3024 " color: @error_fg_color;\n"
3028 " background-color: @selected_bg_color;\n"
3029 " color: @selected_fg_color;\n"
3032 ".light-area-focus {\n"
3036 ".dark-area-focus {\n"
3039 "GtkCalendar.view {\n"
3040 " border-width: 1;\n"
3041 " border-style: inset;\n"
3045 "GtkCalendar.view:inconsistent {\n"
3046 " color: darker (@bg_color);\n"
3049 "GtkCalendar.header {\n"
3050 " background-color: @bg_color;\n"
3051 " border-style: outset;\n"
3052 " border-width: 2;\n"
3055 "GtkCalendar.highlight {\n"
3056 " border-width: 0;\n"
3059 "GtkCalendar.button {\n"
3060 " background-color: @bg_color;\n"
3063 "GtkCalendar.button:hover {\n"
3064 " background-color: lighter (@bg_color);\n"
3065 " color: @fg_color;\n"
3069 " border-width: 0;\n"
3074 provider = gtk_css_provider_new ();
3075 if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
3077 g_error ("Failed to load the internal default CSS.");
3085 _gtk_css_provider_get_theme_dir (void)
3090 var = g_getenv ("GTK_DATA_PREFIX");
3093 path = g_build_filename (var, "share", "themes", NULL);
3095 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
3101 * gtk_css_provider_get_named:
3102 * @name: A theme name
3103 * @variant: (allow-none): variant to load, for example, "dark", or
3104 * %NULL for the default
3106 * Loads a theme from the usual theme paths
3108 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
3109 * This memory is owned by GTK+, and you must not free it.
3112 gtk_css_provider_get_named (const gchar *name,
3113 const gchar *variant)
3115 static GHashTable *themes = NULL;
3116 GtkCssProvider *provider;
3119 if (G_UNLIKELY (!themes))
3120 themes = g_hash_table_new (g_str_hash, g_str_equal);
3122 if (variant == NULL)
3123 key = (gchar *)name;
3125 key = g_strconcat (name, "-", variant, NULL);
3127 provider = g_hash_table_lookup (themes, key);
3131 const gchar *home_dir;
3132 gchar *subpath, *path = NULL;
3135 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
3137 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
3139 /* First look in the users home directory
3141 home_dir = g_get_home_dir ();
3144 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
3146 if (!g_file_test (path, G_FILE_TEST_EXISTS))
3157 theme_dir = _gtk_css_provider_get_theme_dir ();
3158 path = g_build_filename (theme_dir, name, subpath, NULL);
3161 if (!g_file_test (path, G_FILE_TEST_EXISTS))
3172 provider = gtk_css_provider_new ();
3174 if (!gtk_css_provider_load_from_path (provider, path, NULL))
3176 g_object_unref (provider);
3180 g_hash_table_insert (themes, g_strdup (key), provider);
3193 compare_properties (gconstpointer a, gconstpointer b)
3195 return strcmp (((const GtkStyleProperty *) a)->pspec->name,
3196 ((const GtkStyleProperty *) b)->pspec->name);
3200 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
3205 _gtk_css_selector_print (ruleset->selector, str);
3207 g_string_append (str, " {\n");
3211 keys = g_hash_table_get_keys (ruleset->style);
3212 /* so the output is identical for identical selector styles */
3213 keys = g_list_sort (keys, compare_properties);
3215 for (walk = keys; walk; walk = walk->next)
3217 GtkStyleProperty *prop = walk->data;
3218 const PropertyValue *value = g_hash_table_lookup (ruleset->style, prop);
3220 g_string_append (str, " ");
3221 g_string_append (str, prop->pspec->name);
3222 g_string_append (str, ": ");
3223 _gtk_style_property_print_value (prop, &value->value, str);
3224 g_string_append (str, ";\n");
3230 if (ruleset->widget_style)
3232 keys = g_hash_table_get_keys (ruleset->widget_style);
3233 /* so the output is identical for identical selector styles */
3234 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3236 for (walk = keys; walk; walk = walk->next)
3238 const char *name = walk->data;
3239 const PropertyValue *value = g_hash_table_lookup (ruleset->widget_style, (gpointer) name);
3241 g_string_append (str, " ");
3242 g_string_append (str, name);
3243 g_string_append (str, ": ");
3244 g_string_append (str, g_value_get_string (&value->value));
3245 g_string_append (str, ";\n");
3251 g_string_append (str, "}\n");
3255 gtk_css_provider_print_colors (GHashTable *colors,
3261 keys = g_hash_table_get_keys (colors);
3262 /* so the output is identical for identical styles */
3263 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3265 for (walk = keys; walk; walk = walk->next)
3267 const char *name = walk->data;
3268 GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
3270 g_string_append (str, "@define-color ");
3271 g_string_append (str, name);
3272 g_string_append (str, " ");
3273 s = gtk_symbolic_color_to_string (symbolic);
3274 g_string_append (str, s);
3276 g_string_append (str, ";\n");
3283 * gtk_css_provider_to_string:
3284 * @provider: the provider to write to a string
3286 * Convertes the @provider into a string representation in CSS
3289 * Using gtk_css_provider_load_from_data() with the return value
3290 * from this function on a new provider created with
3291 * gtk_css_provider_new() will basicallu create a duplicate of
3294 * Returns: a new string representing the @provider.
3297 gtk_css_provider_to_string (GtkCssProvider *provider)
3299 GtkCssProviderPrivate *priv;
3303 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
3305 priv = provider->priv;
3307 str = g_string_new ("");
3309 gtk_css_provider_print_colors (priv->symbolic_colors, str);
3311 for (i = 0; i < priv->rulesets->len; i++)
3314 g_string_append (str, "\n");
3315 gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
3318 return g_string_free (str, FALSE);