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 "gtkbitmaskprivate.h"
31 #include "gtkcssstylefuncsprivate.h"
32 #include "gtkcssparserprivate.h"
33 #include "gtkcsssectionprivate.h"
34 #include "gtkcssselectorprivate.h"
35 #include "gtkcssshorthandpropertyprivate.h"
36 #include "gtksymboliccolor.h"
37 #include "gtkstyleprovider.h"
38 #include "gtkstylecontextprivate.h"
39 #include "gtkstylepropertiesprivate.h"
40 #include "gtkstylepropertyprivate.h"
41 #include "gtkstyleproviderprivate.h"
42 #include "gtkbindings.h"
43 #include "gtkmarshalers.h"
44 #include "gtkprivate.h"
48 * SECTION:gtkcssprovider
49 * @Short_description: CSS-like styling for widgets
50 * @Title: GtkCssProvider
51 * @See_also: #GtkStyleContext, #GtkStyleProvider
53 * GtkCssProvider is an object implementing the #GtkStyleProvider interface.
54 * It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
55 * input in order to style widgets.
57 * <refsect2 id="gtkcssprovider-files">
58 * <title>Default files</title>
60 * An application can cause GTK+ to parse a specific CSS style sheet by
61 * calling gtk_css_provider_load_from_file() and adding the provider with
62 * gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
63 * In addition, certain files will be read when GTK+ is initialized. First,
64 * the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
65 * is loaded if it exists. Then, GTK+ tries to load
66 * <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
68 * <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
69 * where <replaceable>theme-name</replaceable> is the name of the current theme
70 * (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
71 * is the prefix configured when GTK+ was compiled, unless overridden by the
72 * <envar>GTK_DATA_PREFIX</envar> environment variable.
75 * <refsect2 id="gtkcssprovider-stylesheets">
76 * <title>Style sheets</title>
78 * The basic structure of the style sheets understood by this provider is
79 * a series of statements, which are either rule sets or '@-rules', separated
83 * A rule set consists of a selector and a declaration block, which is
84 * a series of declarations enclosed in curly braces ({ and }). The
85 * declarations are separated by semicolons (;). Multiple selectors can
86 * share the same declaration block, by putting all the separators in
87 * front of the block, separated by commas.
89 * <example><title>A rule set with two selectors</title>
90 * <programlisting language="text">
91 * GtkButton, GtkEntry {
98 * <refsect2 id="gtkcssprovider-selectors">
99 * <title>Selectors</title>
101 * Selectors work very similar to the way they do in CSS, with widget class
102 * names taking the role of element names, and widget names taking the role
103 * of IDs. When used in a selector, widget names must be prefixed with a
104 * '#' character. The '*' character represents the so-called universal
105 * selector, which matches any widget.
108 * To express more complicated situations, selectors can be combined in
111 * <listitem><para>To require that a widget satisfies several conditions,
112 * combine several selectors into one by concatenating them. E.g.
113 * <literal>GtkButton#button1</literal> matches a GtkButton widget
114 * with the name button1.</para></listitem>
115 * <listitem><para>To only match a widget when it occurs inside some other
116 * widget, write the two selectors after each other, separated by whitespace.
117 * E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
118 * that occur inside a GtkToolBar.</para></listitem>
119 * <listitem><para>In the previous example, the GtkButton is matched even
120 * if it occurs deeply nested inside the toolbar. To restrict the match
121 * to direct children of the parent widget, insert a '>' character between
122 * the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
123 * GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
127 * <title>Widget classes and names in selectors</title>
128 * <programlisting language="text">
129 * /* Theme labels that are descendants of a window */
130 * GtkWindow GtkLabel {
131 * background-color: #898989
134 * /* Theme notebooks, and anything that's within these */
136 * background-color: #a939f0
139 * /* Theme combo boxes, and entries that
140 * are direct children of a notebook */
142 * GtkNotebook > GtkEntry {
144 * background-color: #1209a2
147 * /* Theme any widget within a GtkBin */
152 * /* Theme a label named title-label */
153 * GtkLabel#title-label {
157 * /* Theme any widget named main-entry */
159 * background-color: #f0a810
164 * Widgets may also define style classes, which can be used for matching.
165 * When used in a selector, style classes must be prefixed with a '.'
169 * Refer to the documentation of individual widgets to learn which
170 * style classes they define and see <xref linkend="gtkstylecontext-classes"/>
171 * for a list of all style classes used by GTK+ widgets.
174 * Note that there is some ambiguity in the selector syntax when it comes
175 * to differentiation widget class names from regions. GTK+ currently treats
176 * a string as a widget class name if it contains any uppercase characters
177 * (which should work for more widgets with names like GtkLabel).
180 * <title>Style classes in selectors</title>
181 * <programlisting language="text">
182 * /* Theme all widgets defining the class entry */
184 * color: #39f1f9;
187 * /* Theme spinbuttons' entry */
188 * GtkSpinButton.entry {
194 * In complicated widgets like e.g. a GtkNotebook, it may be desirable
195 * to style different parts of the widget differently. To make this
196 * possible, container widgets may define regions, whose names
197 * may be used for matching in selectors.
200 * Some containers allow to further differentiate between regions by
201 * applying so-called pseudo-classes to the region. For example, the
202 * tab region in GtkNotebook allows to single out the first or last
203 * tab by using the :first-child or :last-child pseudo-class.
204 * When used in selectors, pseudo-classes must be prefixed with a
208 * Refer to the documentation of individual widgets to learn which
209 * regions and pseudo-classes they define and see
210 * <xref linkend="gtkstylecontext-classes"/> for a list of all regions
211 * used by GTK+ widgets.
214 * <title>Regions in selectors</title>
215 * <programlisting language="text">
216 * /* Theme any label within a notebook */
217 * GtkNotebook GtkLabel {
218 * color: #f90192;
221 * /* Theme labels within notebook tabs */
222 * GtkNotebook tab GtkLabel {
223 * color: #703910;
226 * /* Theme labels in the any first notebook
227 * tab, both selectors are equivalent */
228 * GtkNotebook tab:nth-child(first) GtkLabel,
229 * GtkNotebook tab:first-child GtkLabel {
230 * color: #89d012;
235 * Another use of pseudo-classes is to match widgets depending on their
236 * state. This is conceptually similar to the :hover, :active or :focus
237 * pseudo-classes in CSS. The available pseudo-classes for widget states
238 * are :active, :prelight (or :hover), :insensitive, :selected, :focused
242 * <title>Styling specific widget states</title>
243 * <programlisting language="text">
244 * /* Theme active (pressed) buttons */
246 * background-color: #0274d9;
249 * /* Theme buttons with the mouse pointer on it,
250 * both are equivalent */
252 * GtkButton:prelight {
253 * background-color: #3085a9;
256 * /* Theme insensitive widgets, both are equivalent */
259 * background-color: #320a91;
262 * /* Theme selection colors in entries */
263 * GtkEntry:selected {
264 * background-color: #56f9a0;
267 * /* Theme focused labels */
269 * background-color: #b4940f;
272 * /* Theme inconsistent checkbuttons */
273 * GtkCheckButton:inconsistent {
274 * background-color: #20395a;
279 * Widget state pseudoclasses may only apply to the last element
283 * To determine the effective style for a widget, all the matching rule
284 * sets are merged. As in CSS, rules apply by specificity, so the rules
285 * whose selectors more closely match a widget path will take precedence
289 * <refsect2 id="gtkcssprovider-rules">
290 * <title>@ Rules</title>
292 * GTK+'s CSS supports the @import rule, in order to load another
293 * CSS style sheet in addition to the currently parsed one.
296 * <title>Using the @import rule</title>
297 * <programlisting language="text">
298 * @import url ("path/to/common.css");
301 * <para id="css-binding-set">
302 * In order to extend key bindings affecting different widgets, GTK+
303 * supports the @binding-set rule to parse a set of bind/unbind
304 * directives, see #GtkBindingSet for the supported syntax. Note that
305 * the binding sets defined in this way must be associated with rule sets
306 * by setting the gtk-key-bindings style property.
309 * Customized key bindings are typically defined in a separate
310 * <filename>gtk-keys.css</filename> CSS file and GTK+ loads this file
311 * according to the current key theme, which is defined by the
312 * #GtkSettings:gtk-key-theme-name setting.
315 * <title>Using the @binding rule</title>
316 * <programlisting language="text">
317 * @binding-set binding-set1 {
318 * bind "<alt>Left" { "move-cursor" (visual-positions, -3, 0) };
322 * @binding-set binding-set2 {
323 * bind "<alt>Right" { "move-cursor" (visual-positions, 3, 0) };
324 * bind "<alt>KP_space" { "delete-from-cursor" (whitespace, 1)
325 * "insert-at-cursor" (" ") };
329 * gtk-key-bindings: binding-set1, binding-set2;
334 * GTK+ also supports an additional @define-color rule, in order
335 * to define a color name which may be used instead of color numeric
336 * representations. Also see the #GtkSettings:gtk-color-scheme setting
337 * for a way to override the values of these named colors.
340 * <title>Defining colors</title>
341 * <programlisting language="text">
342 * @define-color bg_color #f9a039;
345 * background-color: @bg_color;
350 * <refsect2 id="gtkcssprovider-symbolic-colors">
351 * <title>Symbolic colors</title>
353 * Besides being able to define color names, the CSS parser is also able
354 * to read different color expressions, which can also be nested, providing
355 * a rich language to define colors which are derived from a set of base
359 * <title>Using symbolic colors</title>
360 * <programlisting language="text">
361 * @define-color entry-color shade (@bg_color, 0.7);
364 * background-color: @entry-color;
368 * background-color: mix (@entry-color,
369 * shade (#fff, 0.5),
375 * The various ways to express colors in GTK+ CSS are:
381 * <entry>Syntax</entry>
382 * <entry>Explanation</entry>
383 * <entry>Examples</entry>
388 * <entry>rgb(@r, @g, @b)</entry>
389 * <entry>An opaque color; @r, @g, @b can be either integers between
390 * 0 and 255 or percentages</entry>
391 * <entry><literallayout>rgb(128, 10, 54)
392 * rgb(20%, 30%, 0%)</literallayout></entry>
395 * <entry>rgba(@r, @g, @b, @a)</entry>
396 * <entry>A translucent color; @r, @g, @b are as in the previous row,
397 * @a is a floating point number between 0 and 1</entry>
398 * <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
401 * <entry>#@xxyyzz</entry>
402 * <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
403 * specifying @r, @g, @b variants with between 1 and 4
404 * hexadecimal digits per component are allowed</entry>
405 * <entry><literallayout>#ff12ab
406 * #f0c</literallayout></entry>
409 * <entry>@name</entry>
410 * <entry>Reference to a color that has been defined with
411 * @define-color
413 * <entry>@bg_color</entry>
416 * <entry>mix(@color1, @color2, @f)</entry>
417 * <entry>A linear combination of @color1 and @color2. @f is a
418 * floating point number between 0 and 1.</entry>
419 * <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
422 * <entry>shade(@color, @f)</entry>
423 * <entry>A lighter or darker variant of @color. @f is a
424 * floating point number.
426 * <entry>shade(@fg_color, 0.5)</entry>
429 * <entry>lighter(@color)</entry>
430 * <entry>A lighter variant of @color</entry>
433 * <entry>darker(@color)</entry>
434 * <entry>A darker variant of @color</entry>
440 * <refsect2 id="gtkcssprovider-gradients">
441 * <title>Gradients</title>
443 * Linear or radial Gradients can be used as background images.
446 * A linear gradient along the line from (@start_x, @start_y) to
447 * (@end_x, @end_y) is specified using the syntax
448 * <literallayout>-gtk-gradient (linear,
449 * @start_x @start_y, @end_x @end_y,
450 * color-stop (@position, @color),
451 * ...)</literallayout>
452 * where @start_x and @end_x can be either a floating point number between
453 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
454 * and @end_y can be either a floating point number between 0 and 1 or one
455 * of the special values 'top', 'bottom' or 'center', @position is a floating
456 * point number between 0 and 1 and @color is a color expression (see above).
457 * The color-stop can be repeated multiple times to add more than one color
458 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
459 * color stops with position 0 and 1, respectively.
462 * <title>A linear gradient</title>
463 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
464 * <para>This gradient was specified with
465 * <literallayout>-gtk-gradient (linear,
466 * left top, right bottom,
467 * from(@yellow), to(@blue))</literallayout></para>
470 * <title>Another linear gradient</title>
471 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
472 * <para>This gradient was specified with
473 * <literallayout>-gtk-gradient (linear,
475 * color-stop(0, @yellow),
476 * color-stop(0.2, @blue),
477 * color-stop(1, #0f0))</literallayout></para>
480 * A radial gradient along the two circles defined by (@start_x, @start_y,
481 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
483 * <literallayout>-gtk-gradient (radial,
484 * @start_x @start_y, @start_radius,
485 * @end_x @end_y, @end_radius,
486 * color-stop (@position, @color),
487 * ...)</literallayout>
488 * where @start_radius and @end_radius are floating point numbers and
489 * the other parameters are as before.
492 * <title>A radial gradient</title>
493 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
494 * <para>This gradient was specified with
495 * <literallayout>-gtk-gradient (radial,
498 * from(@yellow), to(@green))</literallayout></para>
501 * <title>Another radial gradient</title>
502 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
503 * <para>This gradient was specified with
504 * <literallayout>-gtk-gradient (radial,
507 * color-stop (0, #f00),
508 * color-stop (0.1, #a0f),
509 * color-stop (0.2, @yellow),
510 * color-stop (1, @green))</literallayout></para>
513 * <refsect2 id="gtkcssprovider-shadows">
514 * <title>Text shadow</title>
516 * A shadow list can be applied to text or symbolic icons, using the CSS3
517 * text-shadow syntax, as defined in
518 * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
521 * A text shadow is specified using the syntax
522 * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
523 * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
524 * parameters. The optional blur radius is parsed, but it is currently not rendered by
525 * the GTK+ theming engine.
528 * To set multiple shadows on an element, you can specify a comma-separated list
529 * of shadow elements in the text-shadow property. Shadows are always rendered
530 * front-back, i.e. the first shadow specified is on top of the others. Shadows
531 * can thus overlay each other, but they can never overlay the text itself,
532 * which is always rendered on top of the shadow layer.
536 * <title>Box shadow</title>
538 * Themes can apply shadows on framed elements using the CSS3 box-shadow syntax,
540 * <ulink url="http://www.w3.org/TR/css3-background/#the-box-shadow">the CSS3 specification</ulink>.
543 * A box shadow is specified using the syntax
544 * <literallayout>box-shadow: [ @inset ] @horizontal_offset @vertical_offset [ @blur_radius ] [ @spread ] @color</literallayout>
545 * A positive offset will draw a shadow that is offset to the right (down) of the box,
546 * a negative offset to the left (top). The optional spread parameter defines an additional
547 * distance to expand the shadow shape in all directions, by the specified radius.
548 * The optional blur radius parameter is parsed, but it is currently not rendered by
549 * the GTK+ theming engine.
550 * The inset parameter defines whether the drop shadow should be rendered inside or outside
551 * the box canvas. Only inset box-shadows are currently supported by the GTK+ theming engine,
552 * non-inset elements are currently ignored.
555 * To set multiple box-shadows on an element, you can specify a comma-separated list
556 * of shadow elements in the box-shadow property. Shadows are always rendered
557 * front-back, i.e. the first shadow specified is on top of the others, so they may
558 * overlap other boxes or other shadows.
561 * <refsect2 id="gtkcssprovider-border-image">
562 * <title>Border images</title>
564 * Images and gradients can also be used in slices for the purpose of creating
566 * For more information, see the CSS3 documentation for the border-image property,
567 * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
569 * <inlinegraphic fileref="slices.png" format="PNG"/>
571 * The parameters of the slicing process are controlled by
572 * four separate properties. Note that you can use the
573 * <literallayout>border-image</literallayout> shorthand property
574 * to set values for the three properties at the same time.
577 * <literallayout>border-image-source: url(@path)
578 * (or border-image-source: -gtk-gradient(...))</literallayout>:
579 * Specifies the source of the border image, and it can either
580 * be an URL or a gradient (see above).
583 * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
584 * The sizes specified by the @top, @right, @bottom and @left parameters
585 * are the offsets, in pixels, from the relevant edge where the image
586 * should be "cut off" to build the slices used for the rendering
590 * <literallayout>border-image-width: @top @right @bottom @left</literallayout>
591 * The sizes specified by the @top, @right, @bottom and @left parameters
592 * are inward distances from the border box edge, used to specify the
593 * rendered size of each slice determined by border-image-slice.
594 * If this property is not specified, the values of border-width will
595 * be used as a fallback.
598 * <literallayout>border-image-repeat: [stretch|repeat|round|space] ?
599 * [stretch|repeat|round|space]</literallayout>
600 * Specifies how the image slices should be rendered in the area
601 * outlined by border-width.
602 * The default (stretch) is to resize the slice to fill in the whole
604 * If the value of this property is 'repeat', the image slice
605 * will be tiled to fill the area.
606 * If the value of this property is 'round', the image slice will
607 * be tiled to fill the area, and scaled to fit it exactly
608 * a whole number of times.
609 * If the value of this property is 'space', the image slice will
610 * be tiled to fill the area, and if it doesn't fit it exactly a whole
611 * number of times, the extra space is distributed as padding around
613 * If two options are specified, the first one affects
614 * the horizontal behaviour and the second one the vertical behaviour.
615 * If only one option is specified, it affects both.
618 * <title>A border image</title>
619 * <inlinegraphic fileref="border1.png" format="PNG"/>
620 * <para>This border image was specified with
621 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
625 * <title>A repeating border image</title>
626 * <inlinegraphic fileref="border2.png" format="PNG"/>
627 * <para>This border image was specified with
628 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
632 * <title>A stretched border image</title>
633 * <inlinegraphic fileref="border3.png" format="PNG"/>
634 * <para>This border image was specified with
635 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
639 * <refsect2 id="gtkcssprovider-transitions">
640 * <para>Styles can specify transitions that will be used to create a gradual
641 * change in the appearance when a widget state changes. The following
642 * syntax is used to specify transitions:
643 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
644 * The @duration is the amount of time that the animation will take for
645 * a complete cycle from start to end. If the loop option is given, the
646 * animation will be repated until the state changes again.
647 * The option after the duration determines the transition function from a
648 * small set of predefined functions.
649 * <figure><title>Linear transition</title>
650 * <graphic fileref="linear.png" format="PNG"/>
652 * <figure><title>Ease transition</title>
653 * <graphic fileref="ease.png" format="PNG"/>
655 * <figure><title>Ease-in-out transition</title>
656 * <graphic fileref="ease-in-out.png" format="PNG"/>
658 * <figure><title>Ease-in transition</title>
659 * <graphic fileref="ease-in.png" format="PNG"/>
661 * <figure><title>Ease-out transition</title>
662 * <graphic fileref="ease-out.png" format="PNG"/>
666 * <refsect2 id="gtkcssprovider-properties">
667 * <title>Supported properties</title>
669 * Properties are the part that differ the most to common CSS,
670 * not all properties are supported (some are planned to be
671 * supported eventually, some others are meaningless or don't
672 * map intuitively in a widget based environment).
675 * The currently supported properties are:
681 * <entry>Property name</entry>
682 * <entry>Syntax</entry>
683 * <entry>Maps to</entry>
684 * <entry>Examples</entry>
689 * <entry>engine</entry>
690 * <entry>engine-name</entry>
691 * <entry>#GtkThemingEngine</entry>
692 * <entry>engine: clearlooks;
693 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
696 * <entry>background-color</entry>
697 * <entry morerows="2">color (see above)</entry>
698 * <entry morerows="7">#GdkRGBA</entry>
699 * <entry morerows="7"><literallayout>background-color: #fff;
700 * color: &color1;
701 * background-color: shade (&color1, 0.5);
702 * color: mix (&color1, #f0f, 0.8);</literallayout>
706 * <entry>color</entry>
709 * <entry>border-top-color</entry>
710 * <entry morerows="4">transparent|color (see above)</entry>
713 * <entry>border-right-color</entry>
716 * <entry>border-bottom-color</entry>
719 * <entry>border-left-color</entry>
722 * <entry>border-color</entry>
723 * <entry>[transparent|color]{1,4}</entry>
726 * <entry>font-family</entry>
727 * <entry>@family [, @family]*</entry>
728 * <entry>#gchararray</entry>
729 * <entry>font-family: Sans, Arial;</entry>
732 * <entry>font-style</entry>
733 * <entry>[normal|oblique|italic]</entry>
734 * <entry>#PANGO_TYPE_STYLE</entry>
735 * <entry>font-style: italic;</entry>
738 * <entry>font-variant</entry>
739 * <entry>[normal|small-caps]</entry>
740 * <entry>#PANGO_TYPE_VARIANT</entry>
741 * <entry>font-variant: normal;</entry>
744 * <entry>font-weight</entry>
745 * <entry>[normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900]</entry>
746 * <entry>#PANGO_TYPE_WEIGHT</entry>
747 * <entry>font-weight: bold;</entry>
750 * <entry>font-size</entry>
751 * <entry>Font size in point</entry>
752 * <entry>#gint</entry>
753 * <entry>font-size: 13;</entry>
756 * <entry>font</entry>
757 * <entry>@family [@style] [@size]</entry>
758 * <entry>#PangoFontDescription</entry>
759 * <entry>font: Sans 15;</entry>
762 * <entry>margin-top</entry>
763 * <entry>integer</entry>
764 * <entry>#gint</entry>
765 * <entry>margin-top: 0;</entry>
768 * <entry>margin-left</entry>
769 * <entry>integer</entry>
770 * <entry>#gint</entry>
771 * <entry>margin-left: 1;</entry>
774 * <entry>margin-bottom</entry>
775 * <entry>integer</entry>
776 * <entry>#gint</entry>
777 * <entry>margin-bottom: 2;</entry>
780 * <entry>margin-right</entry>
781 * <entry>integer</entry>
782 * <entry>#gint</entry>
783 * <entry>margin-right: 4;</entry>
786 * <entry>margin</entry>
787 * <entry morerows="1"><literallayout>@width
788 * @vertical_width @horizontal_width
789 * @top_width @horizontal_width @bottom_width
790 * @top_width @right_width @bottom_width @left_width</literallayout>
792 * <entry morerows="1">#GtkBorder</entry>
793 * <entry morerows="1"><literallayout>margin: 5;
796 * margin: 5 10 3 5;</literallayout>
800 * <entry>padding-top</entry>
801 * <entry>integer</entry>
802 * <entry>#gint</entry>
803 * <entry>padding-top: 5;</entry>
806 * <entry>padding-left</entry>
807 * <entry>integer</entry>
808 * <entry>#gint</entry>
809 * <entry>padding-left: 5;</entry>
812 * <entry>padding-bottom</entry>
813 * <entry>integer</entry>
814 * <entry>#gint</entry>
815 * <entry>padding-bottom: 5;</entry>
818 * <entry>padding-right</entry>
819 * <entry>integer</entry>
820 * <entry>#gint</entry>
821 * <entry>padding-right: 5;</entry>
824 * <entry>padding</entry>
827 * <entry>background-image</entry>
828 * <entry><literallayout>gradient (see above) or
829 * url(@path)</literallayout></entry>
830 * <entry>#cairo_pattern_t</entry>
831 * <entry><literallayout>-gtk-gradient (linear,
832 * left top, right top,
833 * from (#fff), to (#000));
834 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
836 * color-stop (0.5, #f00),
838 * -gtk-gradient (radial,
839 * center center, 0.2,
840 * center center, 0.8,
841 * color-stop (0.0, #fff),
842 * color-stop (1.0, #000));
843 * url ('background.png');</literallayout>
847 * <entry>background-repeat</entry>
848 * <entry>[repeat|no-repeat]</entry>
849 * <entry>internal</entry>
850 * <entry><literallayout>background-repeat: no-repeat;</literallayout>
851 * If not specified, the style doesn't respect the CSS3
852 * specification, since the background will be
853 * stretched to fill the area.
857 * <entry>border-top-width</entry>
858 * <entry>integer</entry>
859 * <entry>#gint</entry>
860 * <entry>border-top-width: 5;</entry>
863 * <entry>border-left-width</entry>
864 * <entry>integer</entry>
865 * <entry>#gint</entry>
866 * <entry>border-left-width: 5;</entry>
869 * <entry>border-bottom-width</entry>
870 * <entry>integer</entry>
871 * <entry>#gint</entry>
872 * <entry>border-bottom-width: 5;</entry>
875 * <entry>border-right-width</entry>
876 * <entry>integer</entry>
877 * <entry>#gint</entry>
878 * <entry>border-right-width: 5;</entry>
881 * <entry>border-width</entry>
882 * <entry morerows="1">#GtkBorder</entry>
883 * <entry morerows="1"><literallayout>border-width: 1;
885 * border-width: 1 2 3;
886 * border-width: 1 2 3 5;</literallayout>
890 * <entry>border-radius</entry>
891 * <entry>integer</entry>
892 * <entry>#gint</entry>
893 * <entry>border-radius: 5;</entry>
896 * <entry>border-style</entry>
897 * <entry>[none|solid|inset|outset]</entry>
898 * <entry>#GtkBorderStyle</entry>
899 * <entry>border-style: solid;</entry>
902 * <entry>border-image</entry>
903 * <entry><literallayout>border image (see above)</literallayout></entry>
904 * <entry>internal use only</entry>
905 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
906 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
910 * <entry>text-shadow</entry>
911 * <entry>shadow list (see above)</entry>
912 * <entry>#GtkTextShadow</entry>
913 * <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
916 * <entry>transition</entry>
917 * <entry>transition (see above)</entry>
918 * <entry>internal use only</entry>
919 * <entry><literallayout>transition: 150ms ease-in-out;
920 * transition: 1s linear loop;</literallayout>
924 * <entry>gtk-key-bindings</entry>
925 * <entry>binding set name list</entry>
926 * <entry>internal use only</entry>
927 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
934 * GtkThemingEngines can register their own, engine-specific style properties
935 * with the function gtk_theming_engine_register_property(). These properties
936 * can be set in CSS like other properties, using a name of the form
937 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
938 * the name of the theming engine, and <replaceable>name</replaceable> is the
939 * name of the property. Style properties that have been registered by widgets
940 * using gtk_widget_class_install_style_property() can also be set in this
941 * way, using the widget class name for <replaceable>namespace</replaceable>.
944 * <title>Using engine-specific style properties</title>
947 * engine: clearlooks;
949 * -GtkPaned-handle-size: 6;
950 * -clearlooks-colorize-scrollbar: false;
957 typedef struct GtkCssRuleset GtkCssRuleset;
958 typedef struct _GtkCssScanner GtkCssScanner;
959 typedef enum ParserScope ParserScope;
960 typedef enum ParserSymbol ParserSymbol;
964 GtkCssSelector *selector;
965 GHashTable *widget_style;
967 GtkBitmask *set_styles;
970 struct _GtkCssScanner
972 GtkCssProvider *provider;
973 GtkCssParser *parser;
974 GtkCssSection *section;
975 GtkCssScanner *parent;
981 struct _GtkCssProviderPrivate
985 GHashTable *symbolic_colors;
995 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
997 static void gtk_css_provider_finalize (GObject *object);
998 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
999 static void gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface);
1002 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
1003 GtkCssScanner *scanner,
1009 gtk_css_provider_error_quark (void)
1011 return g_quark_from_static_string ("gtk-css-provider-error-quark");
1014 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
1015 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
1016 gtk_css_style_provider_iface_init)
1017 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
1018 gtk_css_style_provider_private_iface_init));
1021 gtk_css_provider_parsing_error (GtkCssProvider *provider,
1022 GtkCssSection *section,
1023 const GError *error)
1025 /* Only emit a warning when we have no error handlers. This is our
1026 * default handlers. And in this case erroneous CSS files are a bug
1027 * and should be fixed.
1028 * Note that these warnings can also be triggered by a broken theme
1029 * that people installed from some weird location on the internets.
1031 if (!g_signal_has_handler_pending (provider,
1032 css_provider_signals[PARSING_ERROR],
1040 file = gtk_css_section_get_file (section);
1043 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1046 path = g_file_info_get_display_name (info);
1048 path = "<broken file>";
1056 g_warning ("Theme parsing error: %s:%u:%u: %s",
1058 gtk_css_section_get_end_line (section) + 1,
1059 gtk_css_section_get_end_position (section),
1063 g_object_unref (info);
1068 gtk_css_provider_class_init (GtkCssProviderClass *klass)
1070 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1073 * GtkCssProvider::parsing-error:
1074 * @provider: the provider that had a parsing error
1075 * @section: section the error happened in
1076 * @error: The parsing error
1078 * Signals that a parsing error occured. the @path, @line and @position
1079 * describe the actual location of the error as accurately as possible.
1081 * Parsing errors are never fatal, so the parsing will resume after
1082 * the error. Errors may however cause parts of the given
1083 * data or even all of it to not be parsed at all. So it is a useful idea
1084 * to check that the parsing succeeds by connecting to this signal.
1086 * Note that this signal may be emitted at any time as the css provider
1087 * may opt to defer parsing parts or all of the input to a later time
1088 * than when a loading function was called.
1090 css_provider_signals[PARSING_ERROR] =
1091 g_signal_new (I_("parsing-error"),
1092 G_TYPE_FROM_CLASS (object_class),
1094 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
1096 _gtk_marshal_VOID__BOXED_BOXED,
1097 G_TYPE_NONE, 2, GTK_TYPE_CSS_SECTION, G_TYPE_ERROR);
1099 object_class->finalize = gtk_css_provider_finalize;
1101 klass->parsing_error = gtk_css_provider_parsing_error;
1103 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
1107 gtk_css_ruleset_init_copy (GtkCssRuleset *new,
1108 const GtkCssRuleset *ruleset,
1109 GtkCssSelector *selector)
1111 memcpy (new, ruleset, sizeof (GtkCssRuleset));
1113 new->selector = selector;
1114 if (new->widget_style)
1115 g_hash_table_ref (new->widget_style);
1117 g_hash_table_ref (new->style);
1118 if (new->set_styles)
1119 new->set_styles = _gtk_bitmask_copy (new->set_styles);
1123 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
1126 g_hash_table_unref (ruleset->style);
1127 if (ruleset->set_styles)
1128 _gtk_bitmask_free (ruleset->set_styles);
1129 if (ruleset->widget_style)
1130 g_hash_table_unref (ruleset->widget_style);
1131 if (ruleset->selector)
1132 _gtk_css_selector_free (ruleset->selector);
1134 memset (ruleset, 0, sizeof (GtkCssRuleset));
1137 typedef struct _PropertyValue PropertyValue;
1138 struct _PropertyValue {
1139 GtkCssSection *section;
1143 static PropertyValue *
1144 property_value_new (GtkCssSection *section)
1146 PropertyValue *value;
1148 value = g_slice_new0 (PropertyValue);
1150 value->section = gtk_css_section_ref (section);
1156 property_value_free (PropertyValue *value)
1158 if (G_IS_VALUE (&value->value))
1159 g_value_unset (&value->value);
1161 gtk_css_section_unref (value->section);
1163 g_slice_free (PropertyValue, value);
1167 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
1169 PropertyValue *value)
1171 if (ruleset->widget_style == NULL)
1172 ruleset->widget_style = g_hash_table_new_full (g_str_hash,
1174 (GDestroyNotify) g_free,
1175 (GDestroyNotify) property_value_free);
1177 g_hash_table_insert (ruleset->widget_style, name, value);
1181 gtk_css_ruleset_add (GtkCssRuleset *ruleset,
1182 GtkStyleProperty *prop,
1183 PropertyValue *value)
1185 if (ruleset->style == NULL)
1187 ruleset->style = g_hash_table_new_full (g_direct_hash,
1190 (GDestroyNotify) property_value_free);
1191 ruleset->set_styles = _gtk_bitmask_new ();
1194 if (GTK_IS_CSS_SHORTHAND_PROPERTY (prop))
1196 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (prop);
1197 GValueArray *array = g_value_get_boxed (&value->value);
1200 for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
1202 GtkCssStyleProperty *child = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
1205 val = property_value_new (value->section);
1206 g_value_init (&val->value, G_VALUE_TYPE (g_value_array_get_nth (array, i)));
1207 g_value_copy (g_value_array_get_nth (array, i), &val->value);
1208 gtk_css_ruleset_add (ruleset, GTK_STYLE_PROPERTY (child), val);
1210 property_value_free (value);
1212 else if (GTK_IS_CSS_STYLE_PROPERTY (prop))
1214 _gtk_bitmask_set (ruleset->set_styles,
1215 _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)),
1217 g_hash_table_insert (ruleset->style, prop, value);
1221 g_assert_not_reached ();
1226 gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
1227 GtkWidgetPath *path,
1228 GtkStateFlags state)
1230 return _gtk_css_selector_matches (ruleset->selector, path, state);
1234 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1236 if (scanner->section)
1237 gtk_css_section_unref (scanner->section);
1238 g_object_unref (scanner->provider);
1240 g_object_unref (scanner->file);
1241 g_object_unref (scanner->base);
1242 _gtk_css_parser_free (scanner->parser);
1244 g_slice_free (GtkCssScanner, scanner);
1248 gtk_css_provider_emit_error (GtkCssProvider *provider,
1249 GtkCssScanner *scanner,
1250 const GError *error)
1252 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1253 scanner != NULL ? scanner->section : NULL, error);
1257 gtk_css_scanner_parser_error (GtkCssParser *parser,
1258 const GError *error,
1261 GtkCssScanner *scanner = user_data;
1263 gtk_css_provider_emit_error (scanner->provider,
1268 static GtkCssScanner *
1269 gtk_css_scanner_new (GtkCssProvider *provider,
1270 GtkCssScanner *parent,
1271 GtkCssSection *section,
1275 GtkCssScanner *scanner;
1277 scanner = g_slice_new0 (GtkCssScanner);
1279 g_object_ref (provider);
1280 scanner->provider = provider;
1281 scanner->parent = parent;
1283 scanner->section = gtk_css_section_ref (section);
1287 scanner->file = g_object_ref (file);
1288 scanner->base = g_file_get_parent (file);
1292 char *dir = g_get_current_dir ();
1293 scanner->base = g_file_new_for_path (dir);
1297 scanner->parser = _gtk_css_parser_new (text,
1298 gtk_css_scanner_parser_error,
1305 gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
1307 return scanner->base;
1311 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1316 if (scanner->file && g_file_equal (scanner->file, file))
1319 scanner = scanner->parent;
1326 gtk_css_scanner_push_section (GtkCssScanner *scanner,
1327 GtkCssSectionType section_type)
1329 GtkCssSection *section;
1331 section = _gtk_css_section_new (scanner->section,
1336 if (scanner->section)
1337 gtk_css_section_unref (scanner->section);
1338 scanner->section = section;
1342 gtk_css_scanner_pop_section (GtkCssScanner *scanner,
1343 GtkCssSectionType check_type)
1345 GtkCssSection *parent;
1347 g_assert (gtk_css_section_get_section_type (scanner->section) == check_type);
1349 parent = gtk_css_section_get_parent (scanner->section);
1351 gtk_css_section_ref (parent);
1353 _gtk_css_section_end (scanner->section);
1354 gtk_css_section_unref (scanner->section);
1356 scanner->section = parent;
1360 gtk_css_provider_init (GtkCssProvider *css_provider)
1362 GtkCssProviderPrivate *priv;
1364 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1365 GTK_TYPE_CSS_PROVIDER,
1366 GtkCssProviderPrivate);
1368 priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1370 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1371 (GDestroyNotify) g_free,
1372 (GDestroyNotify) gtk_symbolic_color_unref);
1376 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1377 GtkStyleProperties *props)
1379 GtkCssProviderPrivate *priv;
1380 GHashTableIter iter;
1381 gpointer key, value;
1383 priv = css_provider->priv;
1384 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1386 while (g_hash_table_iter_next (&iter, &key, &value))
1389 GtkSymbolicColor *color;
1394 gtk_style_properties_map_color (props, name, color);
1398 static GtkStyleProperties *
1399 gtk_css_provider_get_style (GtkStyleProvider *provider,
1400 GtkWidgetPath *path)
1402 GtkCssProvider *css_provider;
1403 GtkCssProviderPrivate *priv;
1404 GtkStyleProperties *props;
1407 css_provider = GTK_CSS_PROVIDER (provider);
1408 priv = css_provider->priv;
1409 props = gtk_style_properties_new ();
1411 css_provider_dump_symbolic_colors (css_provider, props);
1413 for (i = 0; i < priv->rulesets->len; i++)
1415 GtkCssRuleset *ruleset;
1416 GHashTableIter iter;
1419 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1421 if (ruleset->style == NULL)
1424 if (!gtk_css_ruleset_matches (ruleset, path, 0))
1427 g_hash_table_iter_init (&iter, ruleset->style);
1429 while (g_hash_table_iter_next (&iter, &key, &val))
1431 GtkCssStyleProperty *prop = key;
1432 PropertyValue *value = val;
1434 _gtk_style_properties_set_property_by_property (props,
1436 _gtk_css_selector_get_state_flags (ruleset->selector),
1445 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1446 GtkWidgetPath *path,
1447 GtkStateFlags state,
1451 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1452 GtkCssProviderPrivate *priv = css_provider->priv;
1454 gboolean found = FALSE;
1458 prop_name = g_strdup_printf ("-%s-%s",
1459 g_type_name (pspec->owner_type),
1462 for (i = priv->rulesets->len - 1; i >= 0; i--)
1464 GtkCssRuleset *ruleset;
1466 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1468 if (ruleset->widget_style == NULL)
1471 if (!gtk_css_ruleset_matches (ruleset, path, state))
1474 val = g_hash_table_lookup (ruleset->widget_style, prop_name);
1478 GtkCssScanner *scanner;
1480 scanner = gtk_css_scanner_new (css_provider,
1483 gtk_css_section_get_file (val->section),
1484 g_value_get_string (&val->value));
1486 found = _gtk_css_style_parse_value (value,
1490 gtk_css_scanner_destroy (scanner);
1503 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1505 iface->get_style = gtk_css_provider_get_style;
1506 iface->get_style_property = gtk_css_provider_get_style_property;
1509 static GtkSymbolicColor *
1510 gtk_css_style_provider_get_color (GtkStyleProviderPrivate *provider,
1513 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1515 return g_hash_table_lookup (css_provider->priv->symbolic_colors, name);
1519 gtk_css_style_provider_lookup (GtkStyleProviderPrivate *provider,
1520 GtkWidgetPath *path,
1521 GtkStateFlags state,
1522 GtkCssLookup *lookup)
1524 GtkCssProvider *css_provider;
1525 GtkCssProviderPrivate *priv;
1528 css_provider = GTK_CSS_PROVIDER (provider);
1529 priv = css_provider->priv;
1531 for (i = priv->rulesets->len - 1; i >= 0; i--)
1533 GtkCssRuleset *ruleset;
1534 GHashTableIter iter;
1537 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1539 if (ruleset->style == NULL)
1542 if (!_gtk_bitmask_intersects (_gtk_css_lookup_get_missing (lookup),
1543 ruleset->set_styles))
1546 if (!gtk_css_ruleset_matches (ruleset, path, state))
1549 g_hash_table_iter_init (&iter, ruleset->style);
1551 while (g_hash_table_iter_next (&iter, &key, &val))
1553 GtkCssStyleProperty *prop = key;
1554 PropertyValue *value = val;
1555 guint id = _gtk_css_style_property_get_id (prop);
1557 if (!_gtk_css_lookup_is_missing (lookup, id))
1560 _gtk_css_lookup_set (lookup, id, value->section, &value->value);
1566 gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface)
1568 iface->get_color = gtk_css_style_provider_get_color;
1569 iface->lookup = gtk_css_style_provider_lookup;
1573 gtk_css_provider_finalize (GObject *object)
1575 GtkCssProvider *css_provider;
1576 GtkCssProviderPrivate *priv;
1579 css_provider = GTK_CSS_PROVIDER (object);
1580 priv = css_provider->priv;
1582 for (i = 0; i < priv->rulesets->len; i++)
1583 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1585 g_array_free (priv->rulesets, TRUE);
1587 if (priv->symbolic_colors)
1588 g_hash_table_destroy (priv->symbolic_colors);
1590 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1594 * gtk_css_provider_new:
1596 * Returns a newly created #GtkCssProvider.
1598 * Returns: A new #GtkCssProvider
1601 gtk_css_provider_new (void)
1603 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1607 gtk_css_provider_take_error (GtkCssProvider *provider,
1608 GtkCssScanner *scanner,
1611 gtk_css_provider_emit_error (provider,
1615 g_error_free (error);
1619 gtk_css_provider_error_literal (GtkCssProvider *provider,
1620 GtkCssScanner *scanner,
1623 const char *message)
1625 gtk_css_provider_take_error (provider,
1627 g_error_new_literal (domain, code, message));
1631 gtk_css_provider_error (GtkCssProvider *provider,
1632 GtkCssScanner *scanner,
1636 ...) G_GNUC_PRINTF (5, 6);
1638 gtk_css_provider_error (GtkCssProvider *provider,
1639 GtkCssScanner *scanner,
1648 va_start (args, format);
1649 error = g_error_new_valist (domain, code, format, args);
1652 gtk_css_provider_take_error (provider, scanner, error);
1656 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1657 GtkCssScanner *scanner,
1658 const char *expected)
1660 gtk_css_provider_error (provider,
1662 GTK_CSS_PROVIDER_ERROR,
1663 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1664 "expected a valid %s", expected);
1668 css_provider_commit (GtkCssProvider *css_provider,
1670 GtkCssRuleset *ruleset)
1672 GtkCssProviderPrivate *priv;
1675 priv = css_provider->priv;
1677 if (ruleset->style == NULL && ruleset->widget_style == NULL)
1679 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1683 for (l = selectors; l; l = l->next)
1687 gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1689 g_array_append_val (priv->rulesets, new);
1692 g_slist_free (selectors);
1696 gtk_css_provider_reset (GtkCssProvider *css_provider)
1698 GtkCssProviderPrivate *priv;
1701 priv = css_provider->priv;
1703 g_hash_table_remove_all (priv->symbolic_colors);
1705 for (i = 0; i < priv->rulesets->len; i++)
1706 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1707 g_array_set_size (priv->rulesets, 0);
1711 gtk_css_provider_propagate_error (GtkCssProvider *provider,
1712 GtkCssSection *section,
1713 const GError *error,
1714 GError **propagate_to)
1721 file = gtk_css_section_get_file (section);
1724 info = g_file_query_info (file,G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1727 path = g_file_info_get_display_name (info);
1729 path = "<broken file>";
1737 /* don't fail for deprecations */
1738 if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
1740 g_warning ("Theme parsing error: %s:%u:%u: %s", path,
1741 gtk_css_section_get_end_line (section) + 1,
1742 gtk_css_section_get_end_position (section), error->message);
1746 /* we already set an error. And we'd like to keep the first one */
1750 *propagate_to = g_error_copy (error);
1751 g_prefix_error (propagate_to, "%s:%u:%u: ", path,
1752 gtk_css_section_get_end_line (section) + 1,
1753 gtk_css_section_get_end_position (section));
1756 g_object_unref (info);
1760 parse_import (GtkCssScanner *scanner)
1764 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_IMPORT);
1766 if (!_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1768 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1772 if (_gtk_css_parser_is_string (scanner->parser))
1776 uri = _gtk_css_parser_read_string (scanner->parser);
1777 file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
1782 file = _gtk_css_parser_read_url (scanner->parser,
1783 gtk_css_scanner_get_base_url (scanner));
1788 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1789 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1793 if (!_gtk_css_parser_try (scanner->parser, ";", FALSE))
1795 gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1796 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1798 else if (gtk_css_scanner_would_recurse (scanner, file))
1800 char *path = g_file_get_path (file);
1801 gtk_css_provider_error (scanner->provider,
1803 GTK_CSS_PROVIDER_ERROR,
1804 GTK_CSS_PROVIDER_ERROR_IMPORT,
1805 "Loading '%s' would recurse",
1811 gtk_css_provider_load_internal (scanner->provider,
1818 g_object_unref (file);
1820 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1821 _gtk_css_parser_skip_whitespace (scanner->parser);
1827 parse_color_definition (GtkCssScanner *scanner)
1829 GtkSymbolicColor *symbolic;
1832 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1834 if (!_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1836 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1840 name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1843 gtk_css_provider_error_literal (scanner->provider,
1845 GTK_CSS_PROVIDER_ERROR,
1846 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1847 "Not a valid color name");
1848 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1849 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1853 symbolic = _gtk_css_parser_read_symbolic_color (scanner->parser);
1854 if (symbolic == NULL)
1857 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1858 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1862 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1865 gtk_symbolic_color_unref (symbolic);
1866 gtk_css_provider_error_literal (scanner->provider,
1868 GTK_CSS_PROVIDER_ERROR,
1869 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1870 "Missing semicolon at end of color definition");
1871 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1873 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1877 g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
1879 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1884 parse_binding_set (GtkCssScanner *scanner)
1886 GtkBindingSet *binding_set;
1889 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1891 if (!_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
1893 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1897 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1900 gtk_css_provider_error_literal (scanner->provider,
1902 GTK_CSS_PROVIDER_ERROR,
1903 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1904 "Expected name for binding set");
1905 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1906 goto skip_semicolon;
1909 binding_set = gtk_binding_set_find (name);
1912 binding_set = gtk_binding_set_new (name);
1913 binding_set->parsed = TRUE;
1917 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
1919 gtk_css_provider_error_literal (scanner->provider,
1921 GTK_CSS_PROVIDER_ERROR,
1922 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1923 "Expected '{' for binding set");
1924 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1925 goto skip_semicolon;
1928 while (!_gtk_css_parser_is_eof (scanner->parser) &&
1929 !_gtk_css_parser_begins_with (scanner->parser, '}'))
1931 name = _gtk_css_parser_read_value (scanner->parser);
1934 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1938 if (gtk_binding_entry_add_signal_from_string (binding_set, name) != G_TOKEN_NONE)
1940 gtk_css_provider_error_literal (scanner->provider,
1942 GTK_CSS_PROVIDER_ERROR,
1943 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1944 "Failed to parse binding set.");
1949 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1951 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
1952 !_gtk_css_parser_is_eof (scanner->parser))
1954 gtk_css_provider_error_literal (scanner->provider,
1956 GTK_CSS_PROVIDER_ERROR,
1957 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1958 "Expected semicolon");
1959 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1964 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
1966 gtk_css_provider_error_literal (scanner->provider,
1968 GTK_CSS_PROVIDER_ERROR,
1969 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1970 "expected '}' after declarations");
1971 if (!_gtk_css_parser_is_eof (scanner->parser))
1972 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
1976 if (_gtk_css_parser_begins_with (scanner->parser, ';'))
1978 gtk_css_provider_error_literal (scanner->provider,
1980 GTK_CSS_PROVIDER_ERROR,
1981 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
1982 "Nonstandard semicolon at end of binding set");
1983 _gtk_css_parser_try (scanner->parser, ";", TRUE);
1986 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1992 parse_at_keyword (GtkCssScanner *scanner)
1994 if (parse_import (scanner))
1996 if (parse_color_definition (scanner))
1998 if (parse_binding_set (scanner))
2003 gtk_css_provider_error_literal (scanner->provider,
2005 GTK_CSS_PROVIDER_ERROR,
2006 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2008 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2013 parse_selector_class (GtkCssScanner *scanner, GArray *classes)
2018 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
2022 gtk_css_provider_error_literal (scanner->provider,
2024 GTK_CSS_PROVIDER_ERROR,
2025 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2026 "Expected a valid name for class");
2030 qname = g_quark_from_string (name);
2031 g_array_append_val (classes, qname);
2037 parse_selector_name (GtkCssScanner *scanner, GArray *names)
2042 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
2046 gtk_css_provider_error_literal (scanner->provider,
2048 GTK_CSS_PROVIDER_ERROR,
2049 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2050 "Expected a valid name for id");
2054 qname = g_quark_from_string (name);
2055 g_array_append_val (names, qname);
2061 parse_selector_pseudo_class (GtkCssScanner *scanner,
2062 GtkRegionFlags *region_to_modify,
2063 GtkStateFlags *state_to_modify)
2067 GtkRegionFlags region_flag;
2068 GtkStateFlags state_flag;
2069 } pseudo_classes[] = {
2070 { "first-child", GTK_REGION_FIRST, 0 },
2071 { "last-child", GTK_REGION_LAST, 0 },
2072 { "only-child", GTK_REGION_ONLY, 0 },
2073 { "sorted", GTK_REGION_SORTED, 0 },
2074 { "active", 0, GTK_STATE_FLAG_ACTIVE },
2075 { "prelight", 0, GTK_STATE_FLAG_PRELIGHT },
2076 { "hover", 0, GTK_STATE_FLAG_PRELIGHT },
2077 { "selected", 0, GTK_STATE_FLAG_SELECTED },
2078 { "insensitive", 0, GTK_STATE_FLAG_INSENSITIVE },
2079 { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
2080 { "focused", 0, GTK_STATE_FLAG_FOCUSED },
2081 { "focus", 0, GTK_STATE_FLAG_FOCUSED },
2082 { "backdrop", 0, GTK_STATE_FLAG_BACKDROP },
2084 }, nth_child_classes[] = {
2085 { "first", GTK_REGION_FIRST, 0 },
2086 { "last", GTK_REGION_LAST, 0 },
2087 { "even", GTK_REGION_EVEN, 0 },
2088 { "odd", GTK_REGION_ODD, 0 },
2094 name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
2097 gtk_css_provider_error_literal (scanner->provider,
2099 GTK_CSS_PROVIDER_ERROR,
2100 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2101 "Missing name of pseudo-class");
2105 if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
2107 char *function = name;
2109 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2110 if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
2112 gtk_css_provider_error_literal (scanner->provider,
2114 GTK_CSS_PROVIDER_ERROR,
2115 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2116 "Missing closing bracket for pseudo-class");
2120 if (g_ascii_strcasecmp (function, "nth-child") != 0)
2122 gtk_css_provider_error (scanner->provider,
2124 GTK_CSS_PROVIDER_ERROR,
2125 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2126 "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
2136 gtk_css_provider_error (scanner->provider,
2138 GTK_CSS_PROVIDER_ERROR,
2139 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2140 "nth-child() requires an argument");
2144 classes = nth_child_classes;
2147 classes = pseudo_classes;
2149 for (i = 0; classes[i].name != NULL; i++)
2151 if (g_ascii_strcasecmp (name, classes[i].name) == 0)
2153 if ((*region_to_modify & classes[i].region_flag) ||
2154 (*state_to_modify & classes[i].state_flag))
2156 if (classes == nth_child_classes)
2157 gtk_css_provider_error (scanner->provider,
2159 GTK_CSS_PROVIDER_ERROR,
2160 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2161 "Duplicate pseudo-class 'nth-child(%s)'", name);
2163 gtk_css_provider_error (scanner->provider,
2165 GTK_CSS_PROVIDER_ERROR,
2166 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2167 "Duplicate pseudo-class '%s'", name);
2169 *region_to_modify |= classes[i].region_flag;
2170 *state_to_modify |= classes[i].state_flag;
2177 if (classes == nth_child_classes)
2178 gtk_css_provider_error (scanner->provider,
2180 GTK_CSS_PROVIDER_ERROR,
2181 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2182 "Unknown pseudo-class 'nth-child(%s)'", name);
2184 gtk_css_provider_error (scanner->provider,
2186 GTK_CSS_PROVIDER_ERROR,
2187 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
2188 "Unknown pseudo-class '%s'", name);
2194 parse_simple_selector (GtkCssScanner *scanner,
2198 GtkRegionFlags *pseudo_classes,
2199 GtkStateFlags *state)
2201 gboolean parsed_something;
2203 *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
2205 parsed_something = TRUE;
2207 parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
2210 if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
2212 if (!parse_selector_name (scanner, ids))
2215 else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
2217 if (!parse_selector_class (scanner, classes))
2220 else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
2222 if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
2225 else if (!parsed_something)
2227 gtk_css_provider_error_literal (scanner->provider,
2229 GTK_CSS_PROVIDER_ERROR,
2230 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2231 "Expected a valid selector");
2237 parsed_something = TRUE;
2239 while (!_gtk_css_parser_is_eof (scanner->parser));
2241 _gtk_css_parser_skip_whitespace (scanner->parser);
2245 static GtkCssSelector *
2246 parse_selector (GtkCssScanner *scanner)
2248 GtkCssSelector *selector = NULL;
2252 GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
2253 GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
2254 GtkRegionFlags pseudo_classes = 0;
2255 GtkStateFlags state = 0;
2256 GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
2260 if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
2261 combine = GTK_CSS_COMBINE_CHILD;
2264 if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
2266 g_array_free (ids, TRUE);
2267 g_array_free (classes, TRUE);
2269 _gtk_css_selector_free (selector);
2273 selector = _gtk_css_selector_new (selector,
2276 (GQuark *) g_array_free (ids, ids->len == 0),
2277 (GQuark *) g_array_free (classes, classes->len == 0),
2282 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2283 !_gtk_css_parser_begins_with (scanner->parser, ',') &&
2284 !_gtk_css_parser_begins_with (scanner->parser, '{'));
2290 parse_selector_list (GtkCssScanner *scanner)
2292 GSList *selectors = NULL;
2294 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_SELECTOR);
2297 GtkCssSelector *select = parse_selector (scanner);
2301 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2302 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2303 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2307 selectors = g_slist_prepend (selectors, select);
2309 while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
2311 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2317 parse_declaration (GtkCssScanner *scanner,
2318 GtkCssRuleset *ruleset)
2320 GtkStyleProperty *property;
2323 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DECLARATION);
2325 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2327 goto check_for_semicolon;
2329 property = _gtk_style_property_lookup (name);
2330 if (property == NULL && name[0] != '-')
2332 gtk_css_provider_error (scanner->provider,
2334 GTK_CSS_PROVIDER_ERROR,
2335 GTK_CSS_PROVIDER_ERROR_NAME,
2336 "'%s' is not a valid property name",
2338 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2340 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2344 if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
2346 gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
2347 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2349 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2359 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2361 val = property_value_new (scanner->section);
2363 if (_gtk_style_property_parse_value (property,
2366 gtk_css_scanner_get_base_url (scanner)))
2368 if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
2369 _gtk_css_parser_begins_with (scanner->parser, '}') ||
2370 _gtk_css_parser_is_eof (scanner->parser))
2372 gtk_css_ruleset_add (ruleset, property, val);
2376 gtk_css_provider_error_literal (scanner->provider,
2378 GTK_CSS_PROVIDER_ERROR,
2379 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2380 "Junk at end of value");
2381 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2382 property_value_free (val);
2383 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2384 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2390 property_value_free (val);
2391 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2392 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2393 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2396 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2398 else if (name[0] == '-')
2402 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2404 value_str = _gtk_css_parser_read_value (scanner->parser);
2409 val = property_value_new (scanner->section);
2410 g_value_init (&val->value, G_TYPE_STRING);
2411 g_value_take_string (&val->value, value_str);
2413 gtk_css_ruleset_add_style (ruleset, name, val);
2417 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2418 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2419 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2423 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2428 check_for_semicolon:
2429 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2431 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2433 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2434 !_gtk_css_parser_is_eof (scanner->parser))
2436 gtk_css_provider_error_literal (scanner->provider,
2438 GTK_CSS_PROVIDER_ERROR,
2439 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2440 "Expected semicolon");
2441 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2447 parse_declarations (GtkCssScanner *scanner,
2448 GtkCssRuleset *ruleset)
2450 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2451 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2453 parse_declaration (scanner, ruleset);
2458 parse_ruleset (GtkCssScanner *scanner)
2461 GtkCssRuleset ruleset = { 0, };
2463 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_RULESET);
2465 selectors = parse_selector_list (scanner);
2466 if (selectors == NULL)
2468 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2472 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2474 gtk_css_provider_error_literal (scanner->provider,
2476 GTK_CSS_PROVIDER_ERROR,
2477 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2478 "expected '{' after selectors");
2479 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2480 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2481 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2485 parse_declarations (scanner, &ruleset);
2487 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2489 gtk_css_provider_error_literal (scanner->provider,
2491 GTK_CSS_PROVIDER_ERROR,
2492 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2493 "expected '}' after declarations");
2494 if (!_gtk_css_parser_is_eof (scanner->parser))
2496 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2497 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2498 gtk_css_ruleset_clear (&ruleset);
2499 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2503 css_provider_commit (scanner->provider, selectors, &ruleset);
2504 gtk_css_ruleset_clear (&ruleset);
2505 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2509 parse_statement (GtkCssScanner *scanner)
2511 if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2512 parse_at_keyword (scanner);
2514 parse_ruleset (scanner);
2518 parse_stylesheet (GtkCssScanner *scanner)
2520 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2522 _gtk_css_parser_skip_whitespace (scanner->parser);
2524 while (!_gtk_css_parser_is_eof (scanner->parser))
2526 if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2527 _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2530 parse_statement (scanner);
2533 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2537 gtk_css_provider_compare_rule (gconstpointer a_,
2540 const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2541 const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2544 compare = _gtk_css_selector_compare (a->selector, b->selector);
2548 /* compare pointers in array to ensure a stable sort */
2559 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2561 GtkCssProviderPrivate *priv = css_provider->priv;
2563 g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2567 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2568 GtkCssScanner *parent,
2573 GtkCssScanner *scanner;
2574 gulong error_handler;
2575 char *free_data = NULL;
2578 error_handler = g_signal_connect (css_provider,
2580 G_CALLBACK (gtk_css_provider_propagate_error),
2583 error_handler = 0; /* silence gcc */
2587 GError *load_error = NULL;
2589 if (g_file_load_contents (file, NULL,
2597 GtkCssSection *section;
2600 section = gtk_css_section_ref (parent->section);
2602 section = _gtk_css_section_new_for_file (GTK_CSS_SECTION_DOCUMENT, file);
2604 gtk_css_provider_error (css_provider,
2606 GTK_CSS_PROVIDER_ERROR,
2607 GTK_CSS_PROVIDER_ERROR_IMPORT,
2608 "Failed to import: %s",
2609 load_error->message);
2611 gtk_css_section_unref (section);
2617 scanner = gtk_css_scanner_new (css_provider,
2619 parent ? parent->section : NULL,
2623 parse_stylesheet (scanner);
2625 gtk_css_scanner_destroy (scanner);
2628 gtk_css_provider_postprocess (css_provider);
2635 g_signal_handler_disconnect (css_provider, error_handler);
2639 /* We clear all contents from the provider for backwards compat reasons */
2640 gtk_css_provider_reset (css_provider);
2649 * gtk_css_provider_load_from_data:
2650 * @css_provider: a #GtkCssProvider
2651 * @data: (array length=length) (element-type guint8): CSS data loaded in memory
2652 * @length: the length of @data in bytes, or -1 for NUL terminated strings. If
2653 * @length is not -1, the code will assume it is not NUL terminated and will
2654 * potentially do a copy.
2655 * @error: (out) (allow-none): return location for a #GError, or %NULL
2657 * Loads @data into @css_provider, making it clear any previously loaded
2660 * Returns: %TRUE if the data could be loaded.
2663 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2671 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2672 g_return_val_if_fail (data != NULL, FALSE);
2676 length = strlen (data);
2681 free_data = g_strndup (data, length);
2685 gtk_css_provider_reset (css_provider);
2687 ret = gtk_css_provider_load_internal (css_provider, NULL, NULL, data, error);
2695 * gtk_css_provider_load_from_file:
2696 * @css_provider: a #GtkCssProvider
2697 * @file: #GFile pointing to a file to load
2698 * @error: (out) (allow-none): return location for a #GError, or %NULL
2700 * Loads the data contained in @file into @css_provider, making it
2701 * clear any previously loaded information.
2703 * Returns: %TRUE if the data could be loaded.
2706 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2710 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2711 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2713 gtk_css_provider_reset (css_provider);
2715 return gtk_css_provider_load_internal (css_provider, NULL, file, NULL, error);
2719 * gtk_css_provider_load_from_path:
2720 * @css_provider: a #GtkCssProvider
2721 * @path: the path of a filename to load, in the GLib filename encoding
2722 * @error: (out) (allow-none): return location for a #GError, or %NULL
2724 * Loads the data contained in @path into @css_provider, making it clear
2725 * any previously loaded information.
2727 * Returns: %TRUE if the data could be loaded.
2730 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2737 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2738 g_return_val_if_fail (path != NULL, FALSE);
2740 file = g_file_new_for_path (path);
2742 result = gtk_css_provider_load_from_file (css_provider, file, error);
2744 g_object_unref (file);
2750 _gtk_css_provider_load_from_resource (GtkCssProvider *css_provider,
2751 const gchar *resource_path)
2754 char *uri, *escaped;
2757 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2758 g_return_val_if_fail (resource_path != NULL, FALSE);
2760 escaped = g_uri_escape_string (resource_path,
2761 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
2762 uri = g_strconcat ("resource://", escaped, NULL);
2765 file = g_file_new_for_uri (uri);
2768 result = gtk_css_provider_load_from_file (css_provider, file, NULL);
2770 g_object_unref (file);
2776 * gtk_css_provider_get_default:
2778 * Returns the provider containing the style settings used as a
2779 * fallback for all widgets.
2781 * Returns: (transfer none): The provider used for fallback styling.
2782 * This memory is owned by GTK+, and you must not free it.
2785 gtk_css_provider_get_default (void)
2787 static GtkCssProvider *provider;
2789 if (G_UNLIKELY (!provider))
2792 provider = gtk_css_provider_new ();
2793 if (!_gtk_css_provider_load_from_resource (provider, "/org/gtk/libgtk/gtk-default.css"))
2795 g_error ("Failed to load the internal default CSS.");
2803 _gtk_css_provider_get_theme_dir (void)
2808 var = g_getenv ("GTK_DATA_PREFIX");
2811 path = g_build_filename (var, "share", "themes", NULL);
2813 path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
2819 * gtk_css_provider_get_named:
2820 * @name: A theme name
2821 * @variant: (allow-none): variant to load, for example, "dark", or
2822 * %NULL for the default
2824 * Loads a theme from the usual theme paths
2826 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
2827 * This memory is owned by GTK+, and you must not free it.
2830 gtk_css_provider_get_named (const gchar *name,
2831 const gchar *variant)
2833 static GHashTable *themes = NULL;
2834 GtkCssProvider *provider;
2837 if (G_UNLIKELY (!themes))
2839 themes = g_hash_table_new (g_str_hash, g_str_equal);
2841 provider = gtk_css_provider_new ();
2842 if (!_gtk_css_provider_load_from_resource (provider, "/org/gtk/libgtk/gtk-win32.css"))
2844 g_warning ("Failed to load the internal win32 default CSS.");
2845 g_object_unref (provider);
2848 g_hash_table_insert (themes, "gtk-win32", provider);
2851 if (variant == NULL)
2852 key = (gchar *)name;
2854 key = g_strconcat (name, "-", variant, NULL);
2856 provider = g_hash_table_lookup (themes, key);
2860 const gchar *home_dir;
2861 gchar *subpath, *path = NULL;
2864 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
2866 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
2868 /* First look in the users home directory
2870 home_dir = g_get_home_dir ();
2873 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
2875 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2886 theme_dir = _gtk_css_provider_get_theme_dir ();
2887 path = g_build_filename (theme_dir, name, subpath, NULL);
2890 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2901 provider = gtk_css_provider_new ();
2903 if (!gtk_css_provider_load_from_path (provider, path, NULL))
2905 g_object_unref (provider);
2909 g_hash_table_insert (themes, g_strdup (key), provider);
2922 compare_properties (gconstpointer a, gconstpointer b)
2924 return strcmp (_gtk_style_property_get_name ((GtkStyleProperty *) a),
2925 _gtk_style_property_get_name ((GtkStyleProperty *) b));
2929 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
2934 _gtk_css_selector_print (ruleset->selector, str);
2936 g_string_append (str, " {\n");
2940 keys = g_hash_table_get_keys (ruleset->style);
2941 /* so the output is identical for identical selector styles */
2942 keys = g_list_sort (keys, compare_properties);
2944 for (walk = keys; walk; walk = walk->next)
2946 GtkCssStyleProperty *prop = walk->data;
2947 const PropertyValue *value = g_hash_table_lookup (ruleset->style, prop);
2949 g_string_append (str, " ");
2950 g_string_append (str, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop)));
2951 g_string_append (str, ": ");
2952 _gtk_css_style_property_print_value (prop, &value->value, str);
2953 g_string_append (str, ";\n");
2959 if (ruleset->widget_style)
2961 keys = g_hash_table_get_keys (ruleset->widget_style);
2962 /* so the output is identical for identical selector styles */
2963 keys = g_list_sort (keys, (GCompareFunc) strcmp);
2965 for (walk = keys; walk; walk = walk->next)
2967 const char *name = walk->data;
2968 const PropertyValue *value = g_hash_table_lookup (ruleset->widget_style, (gpointer) name);
2970 g_string_append (str, " ");
2971 g_string_append (str, name);
2972 g_string_append (str, ": ");
2973 g_string_append (str, g_value_get_string (&value->value));
2974 g_string_append (str, ";\n");
2980 g_string_append (str, "}\n");
2984 gtk_css_provider_print_colors (GHashTable *colors,
2990 keys = g_hash_table_get_keys (colors);
2991 /* so the output is identical for identical styles */
2992 keys = g_list_sort (keys, (GCompareFunc) strcmp);
2994 for (walk = keys; walk; walk = walk->next)
2996 const char *name = walk->data;
2997 GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
2999 g_string_append (str, "@define-color ");
3000 g_string_append (str, name);
3001 g_string_append (str, " ");
3002 s = gtk_symbolic_color_to_string (symbolic);
3003 g_string_append (str, s);
3005 g_string_append (str, ";\n");
3012 * gtk_css_provider_to_string:
3013 * @provider: the provider to write to a string
3015 * Convertes the @provider into a string representation in CSS
3018 * Using gtk_css_provider_load_from_data() with the return value
3019 * from this function on a new provider created with
3020 * gtk_css_provider_new() will basicallu create a duplicate of
3023 * Returns: a new string representing the @provider.
3028 gtk_css_provider_to_string (GtkCssProvider *provider)
3030 GtkCssProviderPrivate *priv;
3034 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
3036 priv = provider->priv;
3038 str = g_string_new ("");
3040 gtk_css_provider_print_colors (priv->symbolic_colors, str);
3042 for (i = 0; i < priv->rulesets->len; i++)
3045 g_string_append (str, "\n");
3046 gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
3049 return g_string_free (str, FALSE);