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, see <http://www.gnu.org/licenses/>.
23 #include <gdk-pixbuf/gdk-pixbuf.h>
24 #include <cairo-gobject.h>
26 #include "gtkcssproviderprivate.h"
28 #include "gtkbitmaskprivate.h"
29 #include "gtkcssstylefuncsprivate.h"
30 #include "gtkcssparserprivate.h"
31 #include "gtkcsssectionprivate.h"
32 #include "gtkcssselectorprivate.h"
33 #include "gtkcssshorthandpropertyprivate.h"
34 #include "gtksymboliccolor.h"
35 #include "gtkstyleprovider.h"
36 #include "gtkstylecontextprivate.h"
37 #include "gtkstylepropertiesprivate.h"
38 #include "gtkstylepropertyprivate.h"
39 #include "gtkstyleproviderprivate.h"
40 #include "gtkbindings.h"
41 #include "gtkmarshalers.h"
42 #include "gtkprivate.h"
46 * SECTION:gtkcssprovider
47 * @Short_description: CSS-like styling for widgets
48 * @Title: GtkCssProvider
49 * @See_also: #GtkStyleContext, #GtkStyleProvider
51 * GtkCssProvider is an object implementing the #GtkStyleProvider interface.
52 * It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
53 * input in order to style widgets.
55 * <refsect2 id="gtkcssprovider-files">
56 * <title>Default files</title>
58 * An application can cause GTK+ to parse a specific CSS style sheet by
59 * calling gtk_css_provider_load_from_file() and adding the provider with
60 * gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
61 * In addition, certain files will be read when GTK+ is initialized. First,
62 * the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
63 * is loaded if it exists. Then, GTK+ tries to load
64 * <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
66 * <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
67 * where <replaceable>theme-name</replaceable> is the name of the current theme
68 * (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
69 * is the prefix configured when GTK+ was compiled, unless overridden by the
70 * <envar>GTK_DATA_PREFIX</envar> environment variable.
73 * <refsect2 id="gtkcssprovider-stylesheets">
74 * <title>Style sheets</title>
76 * The basic structure of the style sheets understood by this provider is
77 * a series of statements, which are either rule sets or '@-rules', separated
81 * A rule set consists of a selector and a declaration block, which is
82 * a series of declarations enclosed in curly braces ({ and }). The
83 * declarations are separated by semicolons (;). Multiple selectors can
84 * share the same declaration block, by putting all the separators in
85 * front of the block, separated by commas.
87 * <example><title>A rule set with two selectors</title>
88 * <programlisting language="text">
89 * GtkButton, GtkEntry {
96 * <refsect2 id="gtkcssprovider-selectors">
97 * <title>Selectors</title>
99 * Selectors work very similar to the way they do in CSS, with widget class
100 * names taking the role of element names, and widget names taking the role
101 * of IDs. When used in a selector, widget names must be prefixed with a
102 * '#' character. The '*' character represents the so-called universal
103 * selector, which matches any widget.
106 * To express more complicated situations, selectors can be combined in
109 * <listitem><para>To require that a widget satisfies several conditions,
110 * combine several selectors into one by concatenating them. E.g.
111 * <literal>GtkButton#button1</literal> matches a GtkButton widget
112 * with the name button1.</para></listitem>
113 * <listitem><para>To only match a widget when it occurs inside some other
114 * widget, write the two selectors after each other, separated by whitespace.
115 * E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
116 * that occur inside a GtkToolBar.</para></listitem>
117 * <listitem><para>In the previous example, the GtkButton is matched even
118 * if it occurs deeply nested inside the toolbar. To restrict the match
119 * to direct children of the parent widget, insert a '>' character between
120 * the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
121 * GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
125 * <title>Widget classes and names in selectors</title>
126 * <programlisting language="text">
127 * /* Theme labels that are descendants of a window */
128 * GtkWindow GtkLabel {
129 * background-color: #898989
132 * /* Theme notebooks, and anything that's within these */
134 * background-color: #a939f0
137 * /* Theme combo boxes, and entries that
138 * are direct children of a notebook */
140 * GtkNotebook > GtkEntry {
142 * background-color: #1209a2
145 * /* Theme any widget within a GtkBin */
150 * /* Theme a label named title-label */
151 * GtkLabel#title-label {
155 * /* Theme any widget named main-entry */
157 * background-color: #f0a810
162 * Widgets may also define style classes, which can be used for matching.
163 * When used in a selector, style classes must be prefixed with a '.'
167 * Refer to the documentation of individual widgets to learn which
168 * style classes they define and see <xref linkend="gtkstylecontext-classes"/>
169 * for a list of all style classes used by GTK+ widgets.
172 * Note that there is some ambiguity in the selector syntax when it comes
173 * to differentiation widget class names from regions. GTK+ currently treats
174 * a string as a widget class name if it contains any uppercase characters
175 * (which should work for more widgets with names like GtkLabel).
178 * <title>Style classes in selectors</title>
179 * <programlisting language="text">
180 * /* Theme all widgets defining the class entry */
182 * color: #39f1f9;
185 * /* Theme spinbuttons' entry */
186 * GtkSpinButton.entry {
192 * In complicated widgets like e.g. a GtkNotebook, it may be desirable
193 * to style different parts of the widget differently. To make this
194 * possible, container widgets may define regions, whose names
195 * may be used for matching in selectors.
198 * Some containers allow to further differentiate between regions by
199 * applying so-called pseudo-classes to the region. For example, the
200 * tab region in GtkNotebook allows to single out the first or last
201 * tab by using the :first-child or :last-child pseudo-class.
202 * When used in selectors, pseudo-classes must be prefixed with a
206 * Refer to the documentation of individual widgets to learn which
207 * regions and pseudo-classes they define and see
208 * <xref linkend="gtkstylecontext-classes"/> for a list of all regions
209 * used by GTK+ widgets.
212 * <title>Regions in selectors</title>
213 * <programlisting language="text">
214 * /* Theme any label within a notebook */
215 * GtkNotebook GtkLabel {
216 * color: #f90192;
219 * /* Theme labels within notebook tabs */
220 * GtkNotebook tab GtkLabel {
221 * color: #703910;
224 * /* Theme labels in the any first notebook
225 * tab, both selectors are equivalent */
226 * GtkNotebook tab:nth-child(first) GtkLabel,
227 * GtkNotebook tab:first-child GtkLabel {
228 * color: #89d012;
233 * Another use of pseudo-classes is to match widgets depending on their
234 * state. This is conceptually similar to the :hover, :active or :focus
235 * pseudo-classes in CSS. The available pseudo-classes for widget states
236 * are :active, :prelight (or :hover), :insensitive, :selected, :focused
240 * <title>Styling specific widget states</title>
241 * <programlisting language="text">
242 * /* Theme active (pressed) buttons */
244 * background-color: #0274d9;
247 * /* Theme buttons with the mouse pointer on it,
248 * both are equivalent */
250 * GtkButton:prelight {
251 * background-color: #3085a9;
254 * /* Theme insensitive widgets, both are equivalent */
257 * background-color: #320a91;
260 * /* Theme selection colors in entries */
261 * GtkEntry:selected {
262 * background-color: #56f9a0;
265 * /* Theme focused labels */
267 * background-color: #b4940f;
270 * /* Theme inconsistent checkbuttons */
271 * GtkCheckButton:inconsistent {
272 * background-color: #20395a;
277 * Widget state pseudoclasses may only apply to the last element
281 * To determine the effective style for a widget, all the matching rule
282 * sets are merged. As in CSS, rules apply by specificity, so the rules
283 * whose selectors more closely match a widget path will take precedence
287 * <refsect2 id="gtkcssprovider-rules">
288 * <title>@ Rules</title>
290 * GTK+'s CSS supports the @import rule, in order to load another
291 * CSS style sheet in addition to the currently parsed one.
294 * <title>Using the @import rule</title>
295 * <programlisting language="text">
296 * @import url ("path/to/common.css");
299 * <para id="css-binding-set">
300 * In order to extend key bindings affecting different widgets, GTK+
301 * supports the @binding-set rule to parse a set of bind/unbind
302 * directives, see #GtkBindingSet for the supported syntax. Note that
303 * the binding sets defined in this way must be associated with rule sets
304 * by setting the gtk-key-bindings style property.
307 * Customized key bindings are typically defined in a separate
308 * <filename>gtk-keys.css</filename> CSS file and GTK+ loads this file
309 * according to the current key theme, which is defined by the
310 * #GtkSettings:gtk-key-theme-name setting.
313 * <title>Using the @binding rule</title>
314 * <programlisting language="text">
315 * @binding-set binding-set1 {
316 * bind "<alt>Left" { "move-cursor" (visual-positions, -3, 0) };
320 * @binding-set binding-set2 {
321 * bind "<alt>Right" { "move-cursor" (visual-positions, 3, 0) };
322 * bind "<alt>KP_space" { "delete-from-cursor" (whitespace, 1)
323 * "insert-at-cursor" (" ") };
327 * gtk-key-bindings: binding-set1, binding-set2;
332 * GTK+ also supports an additional @define-color rule, in order
333 * to define a color name which may be used instead of color numeric
334 * representations. Also see the #GtkSettings:gtk-color-scheme setting
335 * for a way to override the values of these named colors.
338 * <title>Defining colors</title>
339 * <programlisting language="text">
340 * @define-color bg_color #f9a039;
343 * background-color: @bg_color;
348 * <refsect2 id="gtkcssprovider-symbolic-colors">
349 * <title>Symbolic colors</title>
351 * Besides being able to define color names, the CSS parser is also able
352 * to read different color expressions, which can also be nested, providing
353 * a rich language to define colors which are derived from a set of base
357 * <title>Using symbolic colors</title>
358 * <programlisting language="text">
359 * @define-color entry-color shade (@bg_color, 0.7);
362 * background-color: @entry-color;
366 * background-color: mix (@entry-color,
367 * shade (#fff, 0.5),
373 * The various ways to express colors in GTK+ CSS are:
379 * <entry>Syntax</entry>
380 * <entry>Explanation</entry>
381 * <entry>Examples</entry>
386 * <entry>rgb(@r, @g, @b)</entry>
387 * <entry>An opaque color; @r, @g, @b can be either integers between
388 * 0 and 255 or percentages</entry>
389 * <entry><literallayout>rgb(128, 10, 54)
390 * rgb(20%, 30%, 0%)</literallayout></entry>
393 * <entry>rgba(@r, @g, @b, @a)</entry>
394 * <entry>A translucent color; @r, @g, @b are as in the previous row,
395 * @a is a floating point number between 0 and 1</entry>
396 * <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
399 * <entry>#@xxyyzz</entry>
400 * <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
401 * specifying @r, @g, @b variants with between 1 and 4
402 * hexadecimal digits per component are allowed</entry>
403 * <entry><literallayout>#ff12ab
404 * #f0c</literallayout></entry>
407 * <entry>@name</entry>
408 * <entry>Reference to a color that has been defined with
409 * @define-color
411 * <entry>@bg_color</entry>
414 * <entry>mix(@color1, @color2, @f)</entry>
415 * <entry>A linear combination of @color1 and @color2. @f is a
416 * floating point number between 0 and 1.</entry>
417 * <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
420 * <entry>shade(@color, @f)</entry>
421 * <entry>A lighter or darker variant of @color. @f is a
422 * floating point number.
424 * <entry>shade(@fg_color, 0.5)</entry>
427 * <entry>lighter(@color)</entry>
428 * <entry>A lighter variant of @color</entry>
431 * <entry>darker(@color)</entry>
432 * <entry>A darker variant of @color</entry>
435 * <entry>alpha(@color, @f)</entry>
436 * <entry>Modifies passed color's alpha by a factor @f. @f is a
437 * floating point number. @f < 1.0 results in a more transparent
438 * color while @f > 1.0 results in a more opaque color.
440 * <entry>alhpa(blue, 0.5)</entry>
446 * <refsect2 id="gtkcssprovider-gradients">
447 * <title>Gradients</title>
449 * Linear or radial Gradients can be used as background images.
452 * A linear gradient along the line from (@start_x, @start_y) to
453 * (@end_x, @end_y) is specified using the syntax
454 * <literallayout>-gtk-gradient (linear,
455 * @start_x @start_y, @end_x @end_y,
456 * color-stop (@position, @color),
457 * ...)</literallayout>
458 * where @start_x and @end_x can be either a floating point number between
459 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
460 * and @end_y can be either a floating point number between 0 and 1 or one
461 * of the special values 'top', 'bottom' or 'center', @position is a floating
462 * point number between 0 and 1 and @color is a color expression (see above).
463 * The color-stop can be repeated multiple times to add more than one color
464 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
465 * color stops with position 0 and 1, respectively.
468 * <title>A linear gradient</title>
469 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
470 * <para>This gradient was specified with
471 * <literallayout>-gtk-gradient (linear,
472 * left top, right bottom,
473 * from(@yellow), to(@blue))</literallayout></para>
476 * <title>Another linear gradient</title>
477 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
478 * <para>This gradient was specified with
479 * <literallayout>-gtk-gradient (linear,
481 * color-stop(0, @yellow),
482 * color-stop(0.2, @blue),
483 * color-stop(1, #0f0))</literallayout></para>
486 * A radial gradient along the two circles defined by (@start_x, @start_y,
487 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
489 * <literallayout>-gtk-gradient (radial,
490 * @start_x @start_y, @start_radius,
491 * @end_x @end_y, @end_radius,
492 * color-stop (@position, @color),
493 * ...)</literallayout>
494 * where @start_radius and @end_radius are floating point numbers and
495 * the other parameters are as before.
498 * <title>A radial gradient</title>
499 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
500 * <para>This gradient was specified with
501 * <literallayout>-gtk-gradient (radial,
504 * from(@yellow), to(@green))</literallayout></para>
507 * <title>Another radial gradient</title>
508 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
509 * <para>This gradient was specified with
510 * <literallayout>-gtk-gradient (radial,
513 * color-stop (0, #f00),
514 * color-stop (0.1, #a0f),
515 * color-stop (0.2, @yellow),
516 * color-stop (1, @green))</literallayout></para>
519 * <refsect2 id="gtkcssprovider-shadows">
520 * <title>Text shadow</title>
522 * A shadow list can be applied to text or symbolic icons, using the CSS3
523 * text-shadow syntax, as defined in
524 * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
527 * A text shadow is specified using the syntax
528 * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
529 * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
530 * parameters. The optional blur radius is parsed, but it is currently not rendered by
531 * the GTK+ theming engine.
534 * To set multiple shadows on an element, you can specify a comma-separated list
535 * of shadow elements in the text-shadow property. Shadows are always rendered
536 * front-back, i.e. the first shadow specified is on top of the others. Shadows
537 * can thus overlay each other, but they can never overlay the text itself,
538 * which is always rendered on top of the shadow layer.
542 * <title>Box shadow</title>
544 * Themes can apply shadows on framed elements using the CSS3 box-shadow syntax,
546 * <ulink url="http://www.w3.org/TR/css3-background/#the-box-shadow">the CSS3 specification</ulink>.
549 * A box shadow is specified using the syntax
550 * <literallayout>box-shadow: [ @inset ] @horizontal_offset @vertical_offset [ @blur_radius ] [ @spread ] @color</literallayout>
551 * A positive offset will draw a shadow that is offset to the right (down) of the box,
552 * a negative offset to the left (top). The optional spread parameter defines an additional
553 * distance to expand the shadow shape in all directions, by the specified radius.
554 * The optional blur radius parameter is parsed, but it is currently not rendered by
555 * the GTK+ theming engine.
556 * The inset parameter defines whether the drop shadow should be rendered inside or outside
557 * the box canvas. Only inset box-shadows are currently supported by the GTK+ theming engine,
558 * non-inset elements are currently ignored.
561 * To set multiple box-shadows on an element, you can specify a comma-separated list
562 * of shadow elements in the box-shadow property. Shadows are always rendered
563 * front-back, i.e. the first shadow specified is on top of the others, so they may
564 * overlap other boxes or other shadows.
567 * <refsect2 id="gtkcssprovider-border-image">
568 * <title>Border images</title>
570 * Images and gradients can also be used in slices for the purpose of creating
572 * For more information, see the CSS3 documentation for the border-image property,
573 * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
575 * <inlinegraphic fileref="slices.png" format="PNG"/>
577 * The parameters of the slicing process are controlled by
578 * four separate properties. Note that you can use the
579 * <literallayout>border-image</literallayout> shorthand property
580 * to set values for the three properties at the same time.
583 * <literallayout>border-image-source: url(@path)
584 * (or border-image-source: -gtk-gradient(...))</literallayout>:
585 * Specifies the source of the border image, and it can either
586 * be an URL or a gradient (see above).
589 * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
590 * The sizes specified by the @top, @right, @bottom and @left parameters
591 * are the offsets, in pixels, from the relevant edge where the image
592 * should be "cut off" to build the slices used for the rendering
596 * <literallayout>border-image-width: @top @right @bottom @left</literallayout>
597 * The sizes specified by the @top, @right, @bottom and @left parameters
598 * are inward distances from the border box edge, used to specify the
599 * rendered size of each slice determined by border-image-slice.
600 * If this property is not specified, the values of border-width will
601 * be used as a fallback.
604 * <literallayout>border-image-repeat: [stretch|repeat|round|space] ?
605 * [stretch|repeat|round|space]</literallayout>
606 * Specifies how the image slices should be rendered in the area
607 * outlined by border-width.
608 * The default (stretch) is to resize the slice to fill in the whole
610 * If the value of this property is 'repeat', the image slice
611 * will be tiled to fill the area.
612 * If the value of this property is 'round', the image slice will
613 * be tiled to fill the area, and scaled to fit it exactly
614 * a whole number of times.
615 * If the value of this property is 'space', the image slice will
616 * be tiled to fill the area, and if it doesn't fit it exactly a whole
617 * number of times, the extra space is distributed as padding around
619 * If two options are specified, the first one affects
620 * the horizontal behaviour and the second one the vertical behaviour.
621 * If only one option is specified, it affects both.
624 * <title>A border image</title>
625 * <inlinegraphic fileref="border1.png" format="PNG"/>
626 * <para>This border image was specified with
627 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
631 * <title>A repeating border image</title>
632 * <inlinegraphic fileref="border2.png" format="PNG"/>
633 * <para>This border image was specified with
634 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
638 * <title>A stretched border image</title>
639 * <inlinegraphic fileref="border3.png" format="PNG"/>
640 * <para>This border image was specified with
641 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
645 * <refsect2 id="gtkcssprovider-transitions">
646 * <para>Styles can specify transitions that will be used to create a gradual
647 * change in the appearance when a widget state changes. The following
648 * syntax is used to specify transitions:
649 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
650 * The @duration is the amount of time that the animation will take for
651 * a complete cycle from start to end. If the loop option is given, the
652 * animation will be repated until the state changes again.
653 * The option after the duration determines the transition function from a
654 * small set of predefined functions.
655 * <figure><title>Linear transition</title>
656 * <graphic fileref="linear.png" format="PNG"/>
658 * <figure><title>Ease transition</title>
659 * <graphic fileref="ease.png" format="PNG"/>
661 * <figure><title>Ease-in-out transition</title>
662 * <graphic fileref="ease-in-out.png" format="PNG"/>
664 * <figure><title>Ease-in transition</title>
665 * <graphic fileref="ease-in.png" format="PNG"/>
667 * <figure><title>Ease-out transition</title>
668 * <graphic fileref="ease-out.png" format="PNG"/>
672 * <refsect2 id="gtkcssprovider-properties">
673 * <title>Supported properties</title>
675 * Properties are the part that differ the most to common CSS,
676 * not all properties are supported (some are planned to be
677 * supported eventually, some others are meaningless or don't
678 * map intuitively in a widget based environment).
681 * The currently supported properties are:
687 * <entry>Property name</entry>
688 * <entry>Syntax</entry>
689 * <entry>Maps to</entry>
690 * <entry>Examples</entry>
695 * <entry>engine</entry>
696 * <entry>engine-name</entry>
697 * <entry>#GtkThemingEngine</entry>
698 * <entry>engine: clearlooks;
699 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
702 * <entry>background-color</entry>
703 * <entry morerows="2">color (see above)</entry>
704 * <entry morerows="7">#GdkRGBA</entry>
705 * <entry morerows="7"><literallayout>background-color: #fff;
706 * color: &color1;
707 * background-color: shade (&color1, 0.5);
708 * color: mix (&color1, #f0f, 0.8);</literallayout>
712 * <entry>color</entry>
715 * <entry>border-top-color</entry>
716 * <entry morerows="4">transparent|color (see above)</entry>
719 * <entry>border-right-color</entry>
722 * <entry>border-bottom-color</entry>
725 * <entry>border-left-color</entry>
728 * <entry>border-color</entry>
729 * <entry>[transparent|color]{1,4}</entry>
732 * <entry>font-family</entry>
733 * <entry>@family [, @family]*</entry>
734 * <entry>#gchararray</entry>
735 * <entry>font-family: Sans, Arial;</entry>
738 * <entry>font-style</entry>
739 * <entry>[normal|oblique|italic]</entry>
740 * <entry>#PANGO_TYPE_STYLE</entry>
741 * <entry>font-style: italic;</entry>
744 * <entry>font-variant</entry>
745 * <entry>[normal|small-caps]</entry>
746 * <entry>#PANGO_TYPE_VARIANT</entry>
747 * <entry>font-variant: normal;</entry>
750 * <entry>font-weight</entry>
751 * <entry>[normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900]</entry>
752 * <entry>#PANGO_TYPE_WEIGHT</entry>
753 * <entry>font-weight: bold;</entry>
756 * <entry>font-size</entry>
757 * <entry>Font size in point</entry>
758 * <entry>#gint</entry>
759 * <entry>font-size: 13;</entry>
762 * <entry>font</entry>
763 * <entry>@family [@style] [@size]</entry>
764 * <entry>#PangoFontDescription</entry>
765 * <entry>font: Sans 15;</entry>
768 * <entry>margin-top</entry>
769 * <entry>integer</entry>
770 * <entry>#gint</entry>
771 * <entry>margin-top: 0;</entry>
774 * <entry>margin-left</entry>
775 * <entry>integer</entry>
776 * <entry>#gint</entry>
777 * <entry>margin-left: 1;</entry>
780 * <entry>margin-bottom</entry>
781 * <entry>integer</entry>
782 * <entry>#gint</entry>
783 * <entry>margin-bottom: 2;</entry>
786 * <entry>margin-right</entry>
787 * <entry>integer</entry>
788 * <entry>#gint</entry>
789 * <entry>margin-right: 4;</entry>
792 * <entry>margin</entry>
793 * <entry morerows="1"><literallayout>@width
794 * @vertical_width @horizontal_width
795 * @top_width @horizontal_width @bottom_width
796 * @top_width @right_width @bottom_width @left_width</literallayout>
798 * <entry morerows="1">#GtkBorder</entry>
799 * <entry morerows="1"><literallayout>margin: 5;
802 * margin: 5 10 3 5;</literallayout>
806 * <entry>padding-top</entry>
807 * <entry>integer</entry>
808 * <entry>#gint</entry>
809 * <entry>padding-top: 5;</entry>
812 * <entry>padding-left</entry>
813 * <entry>integer</entry>
814 * <entry>#gint</entry>
815 * <entry>padding-left: 5;</entry>
818 * <entry>padding-bottom</entry>
819 * <entry>integer</entry>
820 * <entry>#gint</entry>
821 * <entry>padding-bottom: 5;</entry>
824 * <entry>padding-right</entry>
825 * <entry>integer</entry>
826 * <entry>#gint</entry>
827 * <entry>padding-right: 5;</entry>
830 * <entry>padding</entry>
833 * <entry>background-image</entry>
834 * <entry><literallayout>gradient (see above) or
835 * url(@path)</literallayout></entry>
836 * <entry>#cairo_pattern_t</entry>
837 * <entry><literallayout>-gtk-gradient (linear,
838 * left top, right top,
839 * from (#fff), to (#000));
840 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
842 * color-stop (0.5, #f00),
844 * -gtk-gradient (radial,
845 * center center, 0.2,
846 * center center, 0.8,
847 * color-stop (0.0, #fff),
848 * color-stop (1.0, #000));
849 * url ('background.png');</literallayout>
853 * <entry>background-repeat</entry>
854 * <entry>[repeat|no-repeat]</entry>
855 * <entry>internal</entry>
856 * <entry><literallayout>background-repeat: no-repeat;</literallayout>
857 * If not specified, the style doesn't respect the CSS3
858 * specification, since the background will be
859 * stretched to fill the area.
863 * <entry>border-top-width</entry>
864 * <entry>integer</entry>
865 * <entry>#gint</entry>
866 * <entry>border-top-width: 5;</entry>
869 * <entry>border-left-width</entry>
870 * <entry>integer</entry>
871 * <entry>#gint</entry>
872 * <entry>border-left-width: 5;</entry>
875 * <entry>border-bottom-width</entry>
876 * <entry>integer</entry>
877 * <entry>#gint</entry>
878 * <entry>border-bottom-width: 5;</entry>
881 * <entry>border-right-width</entry>
882 * <entry>integer</entry>
883 * <entry>#gint</entry>
884 * <entry>border-right-width: 5;</entry>
887 * <entry>border-width</entry>
888 * <entry morerows="1">#GtkBorder</entry>
889 * <entry morerows="1"><literallayout>border-width: 1;
891 * border-width: 1 2 3;
892 * border-width: 1 2 3 5;</literallayout>
896 * <entry>border-radius</entry>
897 * <entry>integer</entry>
898 * <entry>#gint</entry>
899 * <entry>border-radius: 5;</entry>
902 * <entry>border-style</entry>
903 * <entry>[none|solid|inset|outset]</entry>
904 * <entry>#GtkBorderStyle</entry>
905 * <entry>border-style: solid;</entry>
908 * <entry>border-image</entry>
909 * <entry><literallayout>border image (see above)</literallayout></entry>
910 * <entry>internal use only</entry>
911 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
912 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
916 * <entry>text-shadow</entry>
917 * <entry>shadow list (see above)</entry>
918 * <entry>#GtkTextShadow</entry>
919 * <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
922 * <entry>transition</entry>
923 * <entry>transition (see above)</entry>
924 * <entry>internal use only</entry>
925 * <entry><literallayout>transition: 150ms ease-in-out;
926 * transition: 1s linear loop;</literallayout>
930 * <entry>gtk-key-bindings</entry>
931 * <entry>binding set name list</entry>
932 * <entry>internal use only</entry>
933 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
940 * GtkThemingEngines can register their own, engine-specific style properties
941 * with the function gtk_theming_engine_register_property(). These properties
942 * can be set in CSS like other properties, using a name of the form
943 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
944 * the name of the theming engine, and <replaceable>name</replaceable> is the
945 * name of the property. Style properties that have been registered by widgets
946 * using gtk_widget_class_install_style_property() can also be set in this
947 * way, using the widget class name for <replaceable>namespace</replaceable>.
950 * <title>Using engine-specific style properties</title>
953 * engine: clearlooks;
955 * -GtkPaned-handle-size: 6;
956 * -clearlooks-colorize-scrollbar: false;
963 typedef struct GtkCssRuleset GtkCssRuleset;
964 typedef struct _GtkCssScanner GtkCssScanner;
965 typedef enum ParserScope ParserScope;
966 typedef enum ParserSymbol ParserSymbol;
970 GtkCssSelector *selector;
971 GHashTable *widget_style;
973 GtkBitmask *set_styles;
976 struct _GtkCssScanner
978 GtkCssProvider *provider;
979 GtkCssParser *parser;
980 GtkCssSection *section;
981 GtkCssScanner *parent;
987 struct _GtkCssProviderPrivate
991 GHashTable *symbolic_colors;
1002 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
1004 static void gtk_css_provider_finalize (GObject *object);
1005 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
1006 static void gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface);
1009 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
1010 GtkCssScanner *scanner,
1016 gtk_css_provider_error_quark (void)
1018 return g_quark_from_static_string ("gtk-css-provider-error-quark");
1021 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
1022 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
1023 gtk_css_style_provider_iface_init)
1024 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
1025 gtk_css_style_provider_private_iface_init));
1028 gtk_css_provider_parsing_error (GtkCssProvider *provider,
1029 GtkCssSection *section,
1030 const GError *error)
1032 /* Only emit a warning when we have no error handlers. This is our
1033 * default handlers. And in this case erroneous CSS files are a bug
1034 * and should be fixed.
1035 * Note that these warnings can also be triggered by a broken theme
1036 * that people installed from some weird location on the internets.
1038 if (!g_signal_has_handler_pending (provider,
1039 css_provider_signals[PARSING_ERROR],
1047 file = gtk_css_section_get_file (section);
1050 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1053 path = g_file_info_get_display_name (info);
1055 path = "<broken file>";
1063 g_warning ("Theme parsing error: %s:%u:%u: %s",
1065 gtk_css_section_get_end_line (section) + 1,
1066 gtk_css_section_get_end_position (section),
1070 g_object_unref (info);
1075 gtk_css_provider_class_init (GtkCssProviderClass *klass)
1077 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1080 * GtkCssProvider::parsing-error:
1081 * @provider: the provider that had a parsing error
1082 * @section: section the error happened in
1083 * @error: The parsing error
1085 * Signals that a parsing error occured. the @path, @line and @position
1086 * describe the actual location of the error as accurately as possible.
1088 * Parsing errors are never fatal, so the parsing will resume after
1089 * the error. Errors may however cause parts of the given
1090 * data or even all of it to not be parsed at all. So it is a useful idea
1091 * to check that the parsing succeeds by connecting to this signal.
1093 * Note that this signal may be emitted at any time as the css provider
1094 * may opt to defer parsing parts or all of the input to a later time
1095 * than when a loading function was called.
1097 css_provider_signals[PARSING_ERROR] =
1098 g_signal_new (I_("parsing-error"),
1099 G_TYPE_FROM_CLASS (object_class),
1101 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
1103 _gtk_marshal_VOID__BOXED_BOXED,
1104 G_TYPE_NONE, 2, GTK_TYPE_CSS_SECTION, G_TYPE_ERROR);
1106 object_class->finalize = gtk_css_provider_finalize;
1108 klass->parsing_error = gtk_css_provider_parsing_error;
1110 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
1114 gtk_css_ruleset_init_copy (GtkCssRuleset *new,
1115 const GtkCssRuleset *ruleset,
1116 GtkCssSelector *selector)
1118 memcpy (new, ruleset, sizeof (GtkCssRuleset));
1120 new->selector = selector;
1121 if (new->widget_style)
1122 g_hash_table_ref (new->widget_style);
1124 g_hash_table_ref (new->style);
1125 if (new->set_styles)
1126 new->set_styles = _gtk_bitmask_copy (new->set_styles);
1130 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
1133 g_hash_table_unref (ruleset->style);
1134 if (ruleset->set_styles)
1135 _gtk_bitmask_free (ruleset->set_styles);
1136 if (ruleset->widget_style)
1137 g_hash_table_unref (ruleset->widget_style);
1138 if (ruleset->selector)
1139 _gtk_css_selector_free (ruleset->selector);
1141 memset (ruleset, 0, sizeof (GtkCssRuleset));
1144 typedef struct _PropertyValue PropertyValue;
1145 struct _PropertyValue {
1146 GtkCssSection *section;
1150 static PropertyValue *
1151 property_value_new (GtkCssSection *section)
1153 PropertyValue *value;
1155 value = g_slice_new0 (PropertyValue);
1157 value->section = gtk_css_section_ref (section);
1163 property_value_free (PropertyValue *value)
1165 if (G_IS_VALUE (&value->value))
1166 g_value_unset (&value->value);
1168 gtk_css_section_unref (value->section);
1170 g_slice_free (PropertyValue, value);
1174 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
1176 PropertyValue *value)
1178 if (ruleset->widget_style == NULL)
1179 ruleset->widget_style = g_hash_table_new_full (g_str_hash,
1181 (GDestroyNotify) g_free,
1182 (GDestroyNotify) property_value_free);
1184 g_hash_table_insert (ruleset->widget_style, name, value);
1188 gtk_css_ruleset_add (GtkCssRuleset *ruleset,
1189 GtkStyleProperty *prop,
1190 PropertyValue *value)
1192 if (ruleset->style == NULL)
1194 ruleset->style = g_hash_table_new_full (g_direct_hash,
1197 (GDestroyNotify) property_value_free);
1198 ruleset->set_styles = _gtk_bitmask_new ();
1201 if (GTK_IS_CSS_SHORTHAND_PROPERTY (prop))
1203 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (prop);
1204 GArray *array = g_value_get_boxed (&value->value);
1207 for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
1209 GtkCssStyleProperty *child = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
1210 const GValue *sub = &g_array_index (array, GValue, i);
1213 val = property_value_new (value->section);
1214 g_value_init (&val->value, G_VALUE_TYPE (sub));
1215 g_value_copy (sub, &val->value);
1216 gtk_css_ruleset_add (ruleset, GTK_STYLE_PROPERTY (child), val);
1218 property_value_free (value);
1220 else if (GTK_IS_CSS_STYLE_PROPERTY (prop))
1222 g_return_if_fail (_gtk_css_style_property_is_specified_type (GTK_CSS_STYLE_PROPERTY (prop),
1223 G_VALUE_TYPE (&value->value)));
1225 _gtk_bitmask_set (ruleset->set_styles,
1226 _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)),
1228 g_hash_table_insert (ruleset->style, prop, value);
1232 g_assert_not_reached ();
1237 gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
1238 GtkWidgetPath *path,
1239 GtkStateFlags state)
1241 return _gtk_css_selector_matches (ruleset->selector, path, state);
1245 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1247 if (scanner->section)
1248 gtk_css_section_unref (scanner->section);
1249 g_object_unref (scanner->provider);
1251 g_object_unref (scanner->file);
1252 g_object_unref (scanner->base);
1253 _gtk_css_parser_free (scanner->parser);
1255 g_slice_free (GtkCssScanner, scanner);
1259 gtk_css_provider_emit_error (GtkCssProvider *provider,
1260 GtkCssScanner *scanner,
1261 const GError *error)
1263 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1264 scanner != NULL ? scanner->section : NULL, error);
1268 gtk_css_scanner_parser_error (GtkCssParser *parser,
1269 const GError *error,
1272 GtkCssScanner *scanner = user_data;
1274 gtk_css_provider_emit_error (scanner->provider,
1279 static GtkCssScanner *
1280 gtk_css_scanner_new (GtkCssProvider *provider,
1281 GtkCssScanner *parent,
1282 GtkCssSection *section,
1286 GtkCssScanner *scanner;
1288 scanner = g_slice_new0 (GtkCssScanner);
1290 g_object_ref (provider);
1291 scanner->provider = provider;
1292 scanner->parent = parent;
1294 scanner->section = gtk_css_section_ref (section);
1298 scanner->file = g_object_ref (file);
1299 scanner->base = g_file_get_parent (file);
1303 char *dir = g_get_current_dir ();
1304 scanner->base = g_file_new_for_path (dir);
1308 scanner->parser = _gtk_css_parser_new (text,
1309 gtk_css_scanner_parser_error,
1316 gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
1318 return scanner->base;
1322 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1327 if (scanner->file && g_file_equal (scanner->file, file))
1330 scanner = scanner->parent;
1337 gtk_css_scanner_push_section (GtkCssScanner *scanner,
1338 GtkCssSectionType section_type)
1340 GtkCssSection *section;
1342 section = _gtk_css_section_new (scanner->section,
1347 if (scanner->section)
1348 gtk_css_section_unref (scanner->section);
1349 scanner->section = section;
1353 gtk_css_scanner_pop_section (GtkCssScanner *scanner,
1354 GtkCssSectionType check_type)
1356 GtkCssSection *parent;
1358 g_assert (gtk_css_section_get_section_type (scanner->section) == check_type);
1360 parent = gtk_css_section_get_parent (scanner->section);
1362 gtk_css_section_ref (parent);
1364 _gtk_css_section_end (scanner->section);
1365 gtk_css_section_unref (scanner->section);
1367 scanner->section = parent;
1371 gtk_css_provider_init (GtkCssProvider *css_provider)
1373 GtkCssProviderPrivate *priv;
1375 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1376 GTK_TYPE_CSS_PROVIDER,
1377 GtkCssProviderPrivate);
1379 priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1381 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1382 (GDestroyNotify) g_free,
1383 (GDestroyNotify) gtk_symbolic_color_unref);
1387 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1388 GtkStyleProperties *props)
1390 GtkCssProviderPrivate *priv;
1391 GHashTableIter iter;
1392 gpointer key, value;
1394 priv = css_provider->priv;
1395 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1397 while (g_hash_table_iter_next (&iter, &key, &value))
1400 GtkSymbolicColor *color;
1405 gtk_style_properties_map_color (props, name, color);
1409 static GtkStyleProperties *
1410 gtk_css_provider_get_style (GtkStyleProvider *provider,
1411 GtkWidgetPath *path)
1413 GtkCssProvider *css_provider;
1414 GtkCssProviderPrivate *priv;
1415 GtkStyleProperties *props;
1418 css_provider = GTK_CSS_PROVIDER (provider);
1419 priv = css_provider->priv;
1420 props = gtk_style_properties_new ();
1422 css_provider_dump_symbolic_colors (css_provider, props);
1424 for (i = 0; i < priv->rulesets->len; i++)
1426 GtkCssRuleset *ruleset;
1427 GHashTableIter iter;
1430 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1432 if (ruleset->style == NULL)
1435 if (!gtk_css_ruleset_matches (ruleset, path, 0))
1438 g_hash_table_iter_init (&iter, ruleset->style);
1440 while (g_hash_table_iter_next (&iter, &key, &val))
1442 GtkCssStyleProperty *prop = key;
1443 PropertyValue *value = val;
1445 _gtk_style_properties_set_property_by_property (props,
1447 _gtk_css_selector_get_state_flags (ruleset->selector),
1456 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1457 GtkWidgetPath *path,
1458 GtkStateFlags state,
1462 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1463 GtkCssProviderPrivate *priv = css_provider->priv;
1465 gboolean found = FALSE;
1469 prop_name = g_strdup_printf ("-%s-%s",
1470 g_type_name (pspec->owner_type),
1473 for (i = priv->rulesets->len - 1; i >= 0; i--)
1475 GtkCssRuleset *ruleset;
1477 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1479 if (ruleset->widget_style == NULL)
1482 if (!gtk_css_ruleset_matches (ruleset, path, state))
1485 val = g_hash_table_lookup (ruleset->widget_style, prop_name);
1489 GtkCssScanner *scanner;
1491 scanner = gtk_css_scanner_new (css_provider,
1494 gtk_css_section_get_file (val->section),
1495 g_value_get_string (&val->value));
1497 found = _gtk_css_style_parse_value (value,
1501 gtk_css_scanner_destroy (scanner);
1514 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1516 iface->get_style = gtk_css_provider_get_style;
1517 iface->get_style_property = gtk_css_provider_get_style_property;
1520 static GtkSymbolicColor *
1521 gtk_css_style_provider_get_color (GtkStyleProviderPrivate *provider,
1524 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1526 return g_hash_table_lookup (css_provider->priv->symbolic_colors, name);
1530 gtk_css_style_provider_lookup (GtkStyleProviderPrivate *provider,
1531 GtkWidgetPath *path,
1532 GtkStateFlags state,
1533 GtkCssLookup *lookup)
1535 GtkCssProvider *css_provider;
1536 GtkCssProviderPrivate *priv;
1539 css_provider = GTK_CSS_PROVIDER (provider);
1540 priv = css_provider->priv;
1542 for (i = priv->rulesets->len - 1; i >= 0; i--)
1544 GtkCssRuleset *ruleset;
1545 GHashTableIter iter;
1548 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1550 if (ruleset->style == NULL)
1553 if (!_gtk_bitmask_intersects (_gtk_css_lookup_get_missing (lookup),
1554 ruleset->set_styles))
1557 if (!gtk_css_ruleset_matches (ruleset, path, state))
1560 g_hash_table_iter_init (&iter, ruleset->style);
1562 while (g_hash_table_iter_next (&iter, &key, &val))
1564 GtkCssStyleProperty *prop = key;
1565 PropertyValue *value = val;
1566 guint id = _gtk_css_style_property_get_id (prop);
1568 if (!_gtk_css_lookup_is_missing (lookup, id))
1571 _gtk_css_lookup_set (lookup, id, value->section, &value->value);
1577 gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface)
1579 iface->get_color = gtk_css_style_provider_get_color;
1580 iface->lookup = gtk_css_style_provider_lookup;
1584 gtk_css_provider_finalize (GObject *object)
1586 GtkCssProvider *css_provider;
1587 GtkCssProviderPrivate *priv;
1590 css_provider = GTK_CSS_PROVIDER (object);
1591 priv = css_provider->priv;
1593 for (i = 0; i < priv->rulesets->len; i++)
1594 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1596 g_array_free (priv->rulesets, TRUE);
1598 if (priv->symbolic_colors)
1599 g_hash_table_destroy (priv->symbolic_colors);
1603 g_resources_unregister (priv->resource);
1604 g_resource_unref (priv->resource);
1605 priv->resource = NULL;
1608 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1612 * gtk_css_provider_new:
1614 * Returns a newly created #GtkCssProvider.
1616 * Returns: A new #GtkCssProvider
1619 gtk_css_provider_new (void)
1621 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1625 gtk_css_provider_take_error (GtkCssProvider *provider,
1626 GtkCssScanner *scanner,
1629 gtk_css_provider_emit_error (provider,
1633 g_error_free (error);
1637 gtk_css_provider_error_literal (GtkCssProvider *provider,
1638 GtkCssScanner *scanner,
1641 const char *message)
1643 gtk_css_provider_take_error (provider,
1645 g_error_new_literal (domain, code, message));
1649 gtk_css_provider_error (GtkCssProvider *provider,
1650 GtkCssScanner *scanner,
1654 ...) G_GNUC_PRINTF (5, 6);
1656 gtk_css_provider_error (GtkCssProvider *provider,
1657 GtkCssScanner *scanner,
1666 va_start (args, format);
1667 error = g_error_new_valist (domain, code, format, args);
1670 gtk_css_provider_take_error (provider, scanner, error);
1674 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1675 GtkCssScanner *scanner,
1676 const char *expected)
1678 gtk_css_provider_error (provider,
1680 GTK_CSS_PROVIDER_ERROR,
1681 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1682 "expected a valid %s", expected);
1686 css_provider_commit (GtkCssProvider *css_provider,
1688 GtkCssRuleset *ruleset)
1690 GtkCssProviderPrivate *priv;
1693 priv = css_provider->priv;
1695 if (ruleset->style == NULL && ruleset->widget_style == NULL)
1697 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1701 for (l = selectors; l; l = l->next)
1705 gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1707 g_array_append_val (priv->rulesets, new);
1710 g_slist_free (selectors);
1714 gtk_css_provider_reset (GtkCssProvider *css_provider)
1716 GtkCssProviderPrivate *priv;
1719 priv = css_provider->priv;
1723 g_resources_unregister (priv->resource);
1724 g_resource_unref (priv->resource);
1725 priv->resource = NULL;
1728 g_hash_table_remove_all (priv->symbolic_colors);
1730 for (i = 0; i < priv->rulesets->len; i++)
1731 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1732 g_array_set_size (priv->rulesets, 0);
1736 gtk_css_provider_propagate_error (GtkCssProvider *provider,
1737 GtkCssSection *section,
1738 const GError *error,
1739 GError **propagate_to)
1746 file = gtk_css_section_get_file (section);
1749 info = g_file_query_info (file,G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1752 path = g_file_info_get_display_name (info);
1754 path = "<broken file>";
1762 /* don't fail for deprecations */
1763 if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
1765 g_warning ("Theme parsing error: %s:%u:%u: %s", path,
1766 gtk_css_section_get_end_line (section) + 1,
1767 gtk_css_section_get_end_position (section), error->message);
1771 /* we already set an error. And we'd like to keep the first one */
1775 *propagate_to = g_error_copy (error);
1776 g_prefix_error (propagate_to, "%s:%u:%u: ", path,
1777 gtk_css_section_get_end_line (section) + 1,
1778 gtk_css_section_get_end_position (section));
1781 g_object_unref (info);
1785 parse_import (GtkCssScanner *scanner)
1789 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_IMPORT);
1791 if (!_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1793 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1797 if (_gtk_css_parser_is_string (scanner->parser))
1801 uri = _gtk_css_parser_read_string (scanner->parser);
1802 file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
1807 file = _gtk_css_parser_read_url (scanner->parser,
1808 gtk_css_scanner_get_base_url (scanner));
1813 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1814 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1818 if (!_gtk_css_parser_try (scanner->parser, ";", FALSE))
1820 gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1821 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1823 else if (gtk_css_scanner_would_recurse (scanner, file))
1825 char *path = g_file_get_path (file);
1826 gtk_css_provider_error (scanner->provider,
1828 GTK_CSS_PROVIDER_ERROR,
1829 GTK_CSS_PROVIDER_ERROR_IMPORT,
1830 "Loading '%s' would recurse",
1836 gtk_css_provider_load_internal (scanner->provider,
1843 g_object_unref (file);
1845 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1846 _gtk_css_parser_skip_whitespace (scanner->parser);
1852 parse_color_definition (GtkCssScanner *scanner)
1854 GtkSymbolicColor *symbolic;
1857 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1859 if (!_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1861 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1865 name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1868 gtk_css_provider_error_literal (scanner->provider,
1870 GTK_CSS_PROVIDER_ERROR,
1871 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1872 "Not a valid color name");
1873 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1874 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1878 symbolic = _gtk_css_parser_read_symbolic_color (scanner->parser);
1879 if (symbolic == NULL)
1882 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1883 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1887 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1890 gtk_symbolic_color_unref (symbolic);
1891 gtk_css_provider_error_literal (scanner->provider,
1893 GTK_CSS_PROVIDER_ERROR,
1894 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1895 "Missing semicolon at end of color definition");
1896 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1898 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1902 g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
1904 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1909 parse_binding_set (GtkCssScanner *scanner)
1911 GtkBindingSet *binding_set;
1914 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1916 if (!_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
1918 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1922 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1925 gtk_css_provider_error_literal (scanner->provider,
1927 GTK_CSS_PROVIDER_ERROR,
1928 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1929 "Expected name for binding set");
1930 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1931 goto skip_semicolon;
1934 binding_set = gtk_binding_set_find (name);
1937 binding_set = gtk_binding_set_new (name);
1938 binding_set->parsed = TRUE;
1942 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
1944 gtk_css_provider_error_literal (scanner->provider,
1946 GTK_CSS_PROVIDER_ERROR,
1947 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1948 "Expected '{' for binding set");
1949 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1950 goto skip_semicolon;
1953 while (!_gtk_css_parser_is_eof (scanner->parser) &&
1954 !_gtk_css_parser_begins_with (scanner->parser, '}'))
1956 name = _gtk_css_parser_read_value (scanner->parser);
1959 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1963 if (gtk_binding_entry_add_signal_from_string (binding_set, name) != G_TOKEN_NONE)
1965 gtk_css_provider_error_literal (scanner->provider,
1967 GTK_CSS_PROVIDER_ERROR,
1968 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1969 "Failed to parse binding set.");
1974 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1976 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
1977 !_gtk_css_parser_is_eof (scanner->parser))
1979 gtk_css_provider_error_literal (scanner->provider,
1981 GTK_CSS_PROVIDER_ERROR,
1982 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1983 "Expected semicolon");
1984 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1989 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
1991 gtk_css_provider_error_literal (scanner->provider,
1993 GTK_CSS_PROVIDER_ERROR,
1994 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1995 "expected '}' after declarations");
1996 if (!_gtk_css_parser_is_eof (scanner->parser))
1997 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2001 if (_gtk_css_parser_begins_with (scanner->parser, ';'))
2003 gtk_css_provider_error_literal (scanner->provider,
2005 GTK_CSS_PROVIDER_ERROR,
2006 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
2007 "Nonstandard semicolon at end of binding set");
2008 _gtk_css_parser_try (scanner->parser, ";", TRUE);
2011 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
2017 parse_at_keyword (GtkCssScanner *scanner)
2019 if (parse_import (scanner))
2021 if (parse_color_definition (scanner))
2023 if (parse_binding_set (scanner))
2028 gtk_css_provider_error_literal (scanner->provider,
2030 GTK_CSS_PROVIDER_ERROR,
2031 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2033 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2038 parse_selector_class (GtkCssScanner *scanner, GArray *classes)
2043 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
2047 gtk_css_provider_error_literal (scanner->provider,
2049 GTK_CSS_PROVIDER_ERROR,
2050 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2051 "Expected a valid name for class");
2055 qname = g_quark_from_string (name);
2056 g_array_append_val (classes, qname);
2062 parse_selector_name (GtkCssScanner *scanner, GArray *names)
2067 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
2071 gtk_css_provider_error_literal (scanner->provider,
2073 GTK_CSS_PROVIDER_ERROR,
2074 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2075 "Expected a valid name for id");
2079 qname = g_quark_from_string (name);
2080 g_array_append_val (names, qname);
2086 parse_selector_pseudo_class (GtkCssScanner *scanner,
2087 GtkRegionFlags *region_to_modify,
2088 GtkStateFlags *state_to_modify)
2092 GtkRegionFlags region_flag;
2093 GtkStateFlags state_flag;
2094 } pseudo_classes[] = {
2095 { "first-child", GTK_REGION_FIRST, 0 },
2096 { "last-child", GTK_REGION_LAST, 0 },
2097 { "only-child", GTK_REGION_ONLY, 0 },
2098 { "sorted", GTK_REGION_SORTED, 0 },
2099 { "active", 0, GTK_STATE_FLAG_ACTIVE },
2100 { "prelight", 0, GTK_STATE_FLAG_PRELIGHT },
2101 { "hover", 0, GTK_STATE_FLAG_PRELIGHT },
2102 { "selected", 0, GTK_STATE_FLAG_SELECTED },
2103 { "insensitive", 0, GTK_STATE_FLAG_INSENSITIVE },
2104 { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
2105 { "focused", 0, GTK_STATE_FLAG_FOCUSED },
2106 { "focus", 0, GTK_STATE_FLAG_FOCUSED },
2107 { "backdrop", 0, GTK_STATE_FLAG_BACKDROP },
2109 }, nth_child_classes[] = {
2110 { "first", GTK_REGION_FIRST, 0 },
2111 { "last", GTK_REGION_LAST, 0 },
2112 { "even", GTK_REGION_EVEN, 0 },
2113 { "odd", GTK_REGION_ODD, 0 },
2119 name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
2122 gtk_css_provider_error_literal (scanner->provider,
2124 GTK_CSS_PROVIDER_ERROR,
2125 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2126 "Missing name of pseudo-class");
2130 if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
2132 char *function = name;
2134 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2135 if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
2137 gtk_css_provider_error_literal (scanner->provider,
2139 GTK_CSS_PROVIDER_ERROR,
2140 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2141 "Missing closing bracket for pseudo-class");
2145 if (g_ascii_strcasecmp (function, "nth-child") != 0)
2147 gtk_css_provider_error (scanner->provider,
2149 GTK_CSS_PROVIDER_ERROR,
2150 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2151 "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
2161 gtk_css_provider_error (scanner->provider,
2163 GTK_CSS_PROVIDER_ERROR,
2164 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2165 "nth-child() requires an argument");
2169 classes = nth_child_classes;
2172 classes = pseudo_classes;
2174 for (i = 0; classes[i].name != NULL; i++)
2176 if (g_ascii_strcasecmp (name, classes[i].name) == 0)
2178 if ((*region_to_modify & classes[i].region_flag) ||
2179 (*state_to_modify & classes[i].state_flag))
2181 if (classes == nth_child_classes)
2182 gtk_css_provider_error (scanner->provider,
2184 GTK_CSS_PROVIDER_ERROR,
2185 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2186 "Duplicate pseudo-class 'nth-child(%s)'", name);
2188 gtk_css_provider_error (scanner->provider,
2190 GTK_CSS_PROVIDER_ERROR,
2191 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2192 "Duplicate pseudo-class '%s'", name);
2194 *region_to_modify |= classes[i].region_flag;
2195 *state_to_modify |= classes[i].state_flag;
2202 if (classes == nth_child_classes)
2203 gtk_css_provider_error (scanner->provider,
2205 GTK_CSS_PROVIDER_ERROR,
2206 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2207 "Unknown pseudo-class 'nth-child(%s)'", name);
2209 gtk_css_provider_error (scanner->provider,
2211 GTK_CSS_PROVIDER_ERROR,
2212 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2213 "Unknown pseudo-class '%s'", name);
2219 parse_simple_selector (GtkCssScanner *scanner,
2223 GtkRegionFlags *pseudo_classes,
2224 GtkStateFlags *state)
2226 gboolean parsed_something;
2228 *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
2230 parsed_something = TRUE;
2232 parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
2235 if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
2237 if (!parse_selector_name (scanner, ids))
2240 else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
2242 if (!parse_selector_class (scanner, classes))
2245 else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
2247 if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
2250 else if (!parsed_something)
2252 gtk_css_provider_error_literal (scanner->provider,
2254 GTK_CSS_PROVIDER_ERROR,
2255 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2256 "Expected a valid selector");
2262 parsed_something = TRUE;
2264 while (!_gtk_css_parser_is_eof (scanner->parser));
2266 _gtk_css_parser_skip_whitespace (scanner->parser);
2270 static GtkCssSelector *
2271 parse_selector (GtkCssScanner *scanner)
2273 GtkCssSelector *selector = NULL;
2277 GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
2278 GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
2279 GtkRegionFlags pseudo_classes = 0;
2280 GtkStateFlags state = 0;
2281 GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
2285 if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
2286 combine = GTK_CSS_COMBINE_CHILD;
2289 if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
2291 g_array_free (ids, TRUE);
2292 g_array_free (classes, TRUE);
2294 _gtk_css_selector_free (selector);
2298 selector = _gtk_css_selector_new (selector,
2301 (GQuark *) g_array_free (ids, ids->len == 0),
2302 (GQuark *) g_array_free (classes, classes->len == 0),
2307 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2308 !_gtk_css_parser_begins_with (scanner->parser, ',') &&
2309 !_gtk_css_parser_begins_with (scanner->parser, '{'));
2315 parse_selector_list (GtkCssScanner *scanner)
2317 GSList *selectors = NULL;
2319 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_SELECTOR);
2322 GtkCssSelector *select = parse_selector (scanner);
2326 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2327 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2328 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2332 selectors = g_slist_prepend (selectors, select);
2334 while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
2336 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2342 parse_declaration (GtkCssScanner *scanner,
2343 GtkCssRuleset *ruleset)
2345 GtkStyleProperty *property;
2348 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DECLARATION);
2350 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2352 goto check_for_semicolon;
2354 property = _gtk_style_property_lookup (name);
2355 if (property == NULL && name[0] != '-')
2357 gtk_css_provider_error (scanner->provider,
2359 GTK_CSS_PROVIDER_ERROR,
2360 GTK_CSS_PROVIDER_ERROR_NAME,
2361 "'%s' is not a valid property name",
2363 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2365 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2369 if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
2371 gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
2372 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2374 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2384 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2386 val = property_value_new (scanner->section);
2388 if (_gtk_style_property_parse_value (property,
2391 gtk_css_scanner_get_base_url (scanner)))
2393 if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
2394 _gtk_css_parser_begins_with (scanner->parser, '}') ||
2395 _gtk_css_parser_is_eof (scanner->parser))
2397 gtk_css_ruleset_add (ruleset, property, val);
2401 gtk_css_provider_error_literal (scanner->provider,
2403 GTK_CSS_PROVIDER_ERROR,
2404 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2405 "Junk at end of value");
2406 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2407 property_value_free (val);
2408 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2409 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2415 property_value_free (val);
2416 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2417 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2418 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2421 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2423 else if (name[0] == '-')
2427 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2429 value_str = _gtk_css_parser_read_value (scanner->parser);
2434 val = property_value_new (scanner->section);
2435 g_value_init (&val->value, G_TYPE_STRING);
2436 g_value_take_string (&val->value, value_str);
2438 gtk_css_ruleset_add_style (ruleset, name, val);
2442 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2443 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2444 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2448 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2453 check_for_semicolon:
2454 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2456 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2458 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2459 !_gtk_css_parser_is_eof (scanner->parser))
2461 gtk_css_provider_error_literal (scanner->provider,
2463 GTK_CSS_PROVIDER_ERROR,
2464 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2465 "Expected semicolon");
2466 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2472 parse_declarations (GtkCssScanner *scanner,
2473 GtkCssRuleset *ruleset)
2475 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2476 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2478 parse_declaration (scanner, ruleset);
2483 parse_ruleset (GtkCssScanner *scanner)
2486 GtkCssRuleset ruleset = { 0, };
2488 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_RULESET);
2490 selectors = parse_selector_list (scanner);
2491 if (selectors == NULL)
2493 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2497 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2499 gtk_css_provider_error_literal (scanner->provider,
2501 GTK_CSS_PROVIDER_ERROR,
2502 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2503 "expected '{' after selectors");
2504 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2505 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2506 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2510 parse_declarations (scanner, &ruleset);
2512 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2514 gtk_css_provider_error_literal (scanner->provider,
2516 GTK_CSS_PROVIDER_ERROR,
2517 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2518 "expected '}' after declarations");
2519 if (!_gtk_css_parser_is_eof (scanner->parser))
2521 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2522 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2523 gtk_css_ruleset_clear (&ruleset);
2524 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2528 css_provider_commit (scanner->provider, selectors, &ruleset);
2529 gtk_css_ruleset_clear (&ruleset);
2530 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2534 parse_statement (GtkCssScanner *scanner)
2536 if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2537 parse_at_keyword (scanner);
2539 parse_ruleset (scanner);
2543 parse_stylesheet (GtkCssScanner *scanner)
2545 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2547 _gtk_css_parser_skip_whitespace (scanner->parser);
2549 while (!_gtk_css_parser_is_eof (scanner->parser))
2551 if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2552 _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2555 parse_statement (scanner);
2558 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2562 gtk_css_provider_compare_rule (gconstpointer a_,
2565 const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2566 const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2569 compare = _gtk_css_selector_compare (a->selector, b->selector);
2573 /* compare pointers in array to ensure a stable sort */
2584 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2586 GtkCssProviderPrivate *priv = css_provider->priv;
2588 g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2592 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2593 GtkCssScanner *parent,
2598 GtkCssScanner *scanner;
2599 gulong error_handler;
2600 char *free_data = NULL;
2603 error_handler = g_signal_connect (css_provider,
2605 G_CALLBACK (gtk_css_provider_propagate_error),
2608 error_handler = 0; /* silence gcc */
2612 GError *load_error = NULL;
2614 if (g_file_load_contents (file, NULL,
2622 GtkCssSection *section;
2625 section = gtk_css_section_ref (parent->section);
2627 section = _gtk_css_section_new_for_file (GTK_CSS_SECTION_DOCUMENT, file);
2629 gtk_css_provider_error (css_provider,
2631 GTK_CSS_PROVIDER_ERROR,
2632 GTK_CSS_PROVIDER_ERROR_IMPORT,
2633 "Failed to import: %s",
2634 load_error->message);
2636 gtk_css_section_unref (section);
2642 scanner = gtk_css_scanner_new (css_provider,
2644 parent ? parent->section : NULL,
2648 parse_stylesheet (scanner);
2650 gtk_css_scanner_destroy (scanner);
2653 gtk_css_provider_postprocess (css_provider);
2660 g_signal_handler_disconnect (css_provider, error_handler);
2664 /* We clear all contents from the provider for backwards compat reasons */
2665 gtk_css_provider_reset (css_provider);
2674 * gtk_css_provider_load_from_data:
2675 * @css_provider: a #GtkCssProvider
2676 * @data: (array length=length) (element-type guint8): CSS data loaded in memory
2677 * @length: the length of @data in bytes, or -1 for NUL terminated strings. If
2678 * @length is not -1, the code will assume it is not NUL terminated and will
2679 * potentially do a copy.
2680 * @error: (out) (allow-none): return location for a #GError, or %NULL
2682 * Loads @data into @css_provider, making it clear any previously loaded
2685 * Returns: %TRUE if the data could be loaded.
2688 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2696 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2697 g_return_val_if_fail (data != NULL, FALSE);
2701 length = strlen (data);
2706 free_data = g_strndup (data, length);
2710 gtk_css_provider_reset (css_provider);
2712 ret = gtk_css_provider_load_internal (css_provider, NULL, NULL, data, error);
2720 * gtk_css_provider_load_from_file:
2721 * @css_provider: a #GtkCssProvider
2722 * @file: #GFile pointing to a file to load
2723 * @error: (out) (allow-none): return location for a #GError, or %NULL
2725 * Loads the data contained in @file into @css_provider, making it
2726 * clear any previously loaded information.
2728 * Returns: %TRUE if the data could be loaded.
2731 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2735 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2736 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2738 gtk_css_provider_reset (css_provider);
2740 return gtk_css_provider_load_internal (css_provider, NULL, file, NULL, error);
2744 * gtk_css_provider_load_from_path:
2745 * @css_provider: a #GtkCssProvider
2746 * @path: the path of a filename to load, in the GLib filename encoding
2747 * @error: (out) (allow-none): return location for a #GError, or %NULL
2749 * Loads the data contained in @path into @css_provider, making it clear
2750 * any previously loaded information.
2752 * Returns: %TRUE if the data could be loaded.
2755 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2762 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2763 g_return_val_if_fail (path != NULL, FALSE);
2765 file = g_file_new_for_path (path);
2767 result = gtk_css_provider_load_from_file (css_provider, file, error);
2769 g_object_unref (file);
2775 _gtk_css_provider_load_from_resource (GtkCssProvider *css_provider,
2776 const gchar *resource_path)
2779 char *uri, *escaped;
2782 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2783 g_return_val_if_fail (resource_path != NULL, FALSE);
2785 escaped = g_uri_escape_string (resource_path,
2786 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
2787 uri = g_strconcat ("resource://", escaped, NULL);
2790 file = g_file_new_for_uri (uri);
2793 result = gtk_css_provider_load_from_file (css_provider, file, NULL);
2795 g_object_unref (file);
2801 * gtk_css_provider_get_default:
2803 * Returns the provider containing the style settings used as a
2804 * fallback for all widgets.
2806 * Returns: (transfer none): The provider used for fallback styling.
2807 * This memory is owned by GTK+, and you must not free it.
2810 gtk_css_provider_get_default (void)
2812 static GtkCssProvider *provider;
2814 if (G_UNLIKELY (!provider))
2816 provider = gtk_css_provider_new ();
2823 _gtk_css_provider_get_theme_dir (void)
2828 var = g_getenv ("GTK_DATA_PREFIX");
2831 path = g_build_filename (var, "share", "themes", NULL);
2833 path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
2839 * gtk_css_provider_get_named:
2840 * @name: A theme name
2841 * @variant: (allow-none): variant to load, for example, "dark", or
2842 * %NULL for the default
2844 * Loads a theme from the usual theme paths
2846 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
2847 * This memory is owned by GTK+, and you must not free it.
2850 gtk_css_provider_get_named (const gchar *name,
2851 const gchar *variant)
2853 static GHashTable *themes = NULL;
2854 GtkCssProvider *provider;
2857 if (variant == NULL)
2858 key = (gchar *)name;
2860 key = g_strconcat (name, "-", variant, NULL);
2862 if (G_UNLIKELY (!themes))
2863 themes = g_hash_table_new (g_str_hash, g_str_equal);
2865 provider = g_hash_table_lookup (themes, key);
2869 gchar *resource_path = NULL;
2872 resource_path = g_strdup_printf ("/org/gtk/libgtk/%s-%s.css", name, variant);
2874 resource_path = g_strdup_printf ("/org/gtk/libgtk/%s.css", name);
2876 if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
2878 provider = gtk_css_provider_new ();
2879 if (!_gtk_css_provider_load_from_resource (provider, resource_path))
2881 g_object_unref (provider);
2885 g_free (resource_path);
2890 const gchar *home_dir;
2891 gchar *subpath, *path = NULL;
2894 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
2896 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
2898 /* First look in the users home directory
2900 home_dir = g_get_home_dir ();
2903 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
2905 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2916 theme_dir = _gtk_css_provider_get_theme_dir ();
2917 path = g_build_filename (theme_dir, name, subpath, NULL);
2920 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2931 char *dir, *resource_file;
2932 GResource *resource;
2934 provider = gtk_css_provider_new ();
2936 dir = g_path_get_dirname (path);
2937 resource_file = g_build_filename (dir, "gtk.gresource", NULL);
2938 resource = g_resource_load (resource_file, NULL);
2939 if (resource != NULL)
2940 g_resources_register (resource);
2942 if (!gtk_css_provider_load_from_path (provider, path, NULL))
2944 if (resource != NULL)
2946 g_resources_unregister (resource);
2947 g_resource_unref (resource);
2949 g_object_unref (provider);
2954 /* Only set this after load success, as load_from_path will clear it */
2955 provider->priv->resource = resource;
2956 g_hash_table_insert (themes, g_strdup (key), provider);
2971 compare_properties (gconstpointer a, gconstpointer b)
2973 return strcmp (_gtk_style_property_get_name ((GtkStyleProperty *) a),
2974 _gtk_style_property_get_name ((GtkStyleProperty *) b));
2978 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
2983 _gtk_css_selector_print (ruleset->selector, str);
2985 g_string_append (str, " {\n");
2989 keys = g_hash_table_get_keys (ruleset->style);
2990 /* so the output is identical for identical selector styles */
2991 keys = g_list_sort (keys, compare_properties);
2993 for (walk = keys; walk; walk = walk->next)
2995 GtkCssStyleProperty *prop = walk->data;
2996 const PropertyValue *value = g_hash_table_lookup (ruleset->style, prop);
2998 g_string_append (str, " ");
2999 g_string_append (str, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop)));
3000 g_string_append (str, ": ");
3001 _gtk_css_style_property_print_value (prop, &value->value, str);
3002 g_string_append (str, ";\n");
3008 if (ruleset->widget_style)
3010 keys = g_hash_table_get_keys (ruleset->widget_style);
3011 /* so the output is identical for identical selector styles */
3012 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3014 for (walk = keys; walk; walk = walk->next)
3016 const char *name = walk->data;
3017 const PropertyValue *value = g_hash_table_lookup (ruleset->widget_style, (gpointer) name);
3019 g_string_append (str, " ");
3020 g_string_append (str, name);
3021 g_string_append (str, ": ");
3022 g_string_append (str, g_value_get_string (&value->value));
3023 g_string_append (str, ";\n");
3029 g_string_append (str, "}\n");
3033 gtk_css_provider_print_colors (GHashTable *colors,
3039 keys = g_hash_table_get_keys (colors);
3040 /* so the output is identical for identical styles */
3041 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3043 for (walk = keys; walk; walk = walk->next)
3045 const char *name = walk->data;
3046 GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
3048 g_string_append (str, "@define-color ");
3049 g_string_append (str, name);
3050 g_string_append (str, " ");
3051 s = gtk_symbolic_color_to_string (symbolic);
3052 g_string_append (str, s);
3054 g_string_append (str, ";\n");
3061 * gtk_css_provider_to_string:
3062 * @provider: the provider to write to a string
3064 * Convertes the @provider into a string representation in CSS
3067 * Using gtk_css_provider_load_from_data() with the return value
3068 * from this function on a new provider created with
3069 * gtk_css_provider_new() will basicallu create a duplicate of
3072 * Returns: a new string representing the @provider.
3077 gtk_css_provider_to_string (GtkCssProvider *provider)
3079 GtkCssProviderPrivate *priv;
3083 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
3085 priv = provider->priv;
3087 str = g_string_new ("");
3089 gtk_css_provider_print_colors (priv->symbolic_colors, str);
3091 for (i = 0; i < priv->rulesets->len; i++)
3094 g_string_append (str, "\n");
3095 gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
3098 return g_string_free (str, FALSE);