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 "gtkcssarrayvalueprivate.h"
30 #include "gtkcsscolorvalueprivate.h"
31 #include "gtkcsskeyframesprivate.h"
32 #include "gtkcssparserprivate.h"
33 #include "gtkcsssectionprivate.h"
34 #include "gtkcssselectorprivate.h"
35 #include "gtkcssshorthandpropertyprivate.h"
36 #include "gtkcssstylefuncsprivate.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>
437 * <entry>alpha(@color, @f)</entry>
438 * <entry>Modifies passed color's alpha by a factor @f. @f is a
439 * floating point number. @f < 1.0 results in a more transparent
440 * color while @f > 1.0 results in a more opaque color.
442 * <entry>alhpa(blue, 0.5)</entry>
448 * <refsect2 id="gtkcssprovider-gradients">
449 * <title>Gradients</title>
451 * Linear or radial Gradients can be used as background images.
454 * A linear gradient along the line from (@start_x, @start_y) to
455 * (@end_x, @end_y) is specified using the syntax
456 * <literallayout>-gtk-gradient (linear,
457 * @start_x @start_y, @end_x @end_y,
458 * color-stop (@position, @color),
459 * ...)</literallayout>
460 * where @start_x and @end_x can be either a floating point number between
461 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
462 * and @end_y can be either a floating point number between 0 and 1 or one
463 * of the special values 'top', 'bottom' or 'center', @position is a floating
464 * point number between 0 and 1 and @color is a color expression (see above).
465 * The color-stop can be repeated multiple times to add more than one color
466 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
467 * color stops with position 0 and 1, respectively.
470 * <title>A linear gradient</title>
471 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
472 * <para>This gradient was specified with
473 * <literallayout>-gtk-gradient (linear,
474 * left top, right bottom,
475 * from(@yellow), to(@blue))</literallayout></para>
478 * <title>Another linear gradient</title>
479 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
480 * <para>This gradient was specified with
481 * <literallayout>-gtk-gradient (linear,
483 * color-stop(0, @yellow),
484 * color-stop(0.2, @blue),
485 * color-stop(1, #0f0))</literallayout></para>
488 * A radial gradient along the two circles defined by (@start_x, @start_y,
489 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
491 * <literallayout>-gtk-gradient (radial,
492 * @start_x @start_y, @start_radius,
493 * @end_x @end_y, @end_radius,
494 * color-stop (@position, @color),
495 * ...)</literallayout>
496 * where @start_radius and @end_radius are floating point numbers and
497 * the other parameters are as before.
500 * <title>A radial gradient</title>
501 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
502 * <para>This gradient was specified with
503 * <literallayout>-gtk-gradient (radial,
506 * from(@yellow), to(@green))</literallayout></para>
509 * <title>Another radial gradient</title>
510 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
511 * <para>This gradient was specified with
512 * <literallayout>-gtk-gradient (radial,
515 * color-stop (0, #f00),
516 * color-stop (0.1, #a0f),
517 * color-stop (0.2, @yellow),
518 * color-stop (1, @green))</literallayout></para>
521 * <refsect2 id="gtkcssprovider-shadows">
522 * <title>Text shadow</title>
524 * A shadow list can be applied to text or symbolic icons, using the CSS3
525 * text-shadow syntax, as defined in
526 * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
529 * A text shadow is specified using the syntax
530 * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
531 * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
532 * parameters. The optional blur radius is parsed, but it is currently not rendered by
533 * the GTK+ theming engine.
536 * To set multiple shadows on an element, you can specify a comma-separated list
537 * of shadow elements in the text-shadow property. Shadows are always rendered
538 * front-back, i.e. the first shadow specified is on top of the others. Shadows
539 * can thus overlay each other, but they can never overlay the text itself,
540 * which is always rendered on top of the shadow layer.
544 * <title>Box shadow</title>
546 * Themes can apply shadows on framed elements using the CSS3 box-shadow syntax,
548 * <ulink url="http://www.w3.org/TR/css3-background/#the-box-shadow">the CSS3 specification</ulink>.
551 * A box shadow is specified using the syntax
552 * <literallayout>box-shadow: [ @inset ] @horizontal_offset @vertical_offset [ @blur_radius ] [ @spread ] @color</literallayout>
553 * A positive offset will draw a shadow that is offset to the right (down) of the box,
554 * a negative offset to the left (top). The optional spread parameter defines an additional
555 * distance to expand the shadow shape in all directions, by the specified radius.
556 * The optional blur radius parameter is parsed, but it is currently not rendered by
557 * the GTK+ theming engine.
558 * The inset parameter defines whether the drop shadow should be rendered inside or outside
559 * the box canvas. Only inset box-shadows are currently supported by the GTK+ theming engine,
560 * non-inset elements are currently ignored.
563 * To set multiple box-shadows on an element, you can specify a comma-separated list
564 * of shadow elements in the box-shadow property. Shadows are always rendered
565 * front-back, i.e. the first shadow specified is on top of the others, so they may
566 * overlap other boxes or other shadows.
569 * <refsect2 id="gtkcssprovider-border-image">
570 * <title>Border images</title>
572 * Images and gradients can also be used in slices for the purpose of creating
574 * For more information, see the CSS3 documentation for the border-image property,
575 * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
577 * <inlinegraphic fileref="slices.png" format="PNG"/>
579 * The parameters of the slicing process are controlled by
580 * four separate properties. Note that you can use the
581 * <literallayout>border-image</literallayout> shorthand property
582 * to set values for the three properties at the same time.
585 * <literallayout>border-image-source: url(@path)
586 * (or border-image-source: -gtk-gradient(...))</literallayout>:
587 * Specifies the source of the border image, and it can either
588 * be an URL or a gradient (see above).
591 * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
592 * The sizes specified by the @top, @right, @bottom and @left parameters
593 * are the offsets, in pixels, from the relevant edge where the image
594 * should be "cut off" to build the slices used for the rendering
598 * <literallayout>border-image-width: @top @right @bottom @left</literallayout>
599 * The sizes specified by the @top, @right, @bottom and @left parameters
600 * are inward distances from the border box edge, used to specify the
601 * rendered size of each slice determined by border-image-slice.
602 * If this property is not specified, the values of border-width will
603 * be used as a fallback.
606 * <literallayout>border-image-repeat: [stretch|repeat|round|space] ?
607 * [stretch|repeat|round|space]</literallayout>
608 * Specifies how the image slices should be rendered in the area
609 * outlined by border-width.
610 * The default (stretch) is to resize the slice to fill in the whole
612 * If the value of this property is 'repeat', the image slice
613 * will be tiled to fill the area.
614 * If the value of this property is 'round', the image slice will
615 * be tiled to fill the area, and scaled to fit it exactly
616 * a whole number of times.
617 * If the value of this property is 'space', the image slice will
618 * be tiled to fill the area, and if it doesn't fit it exactly a whole
619 * number of times, the extra space is distributed as padding around
621 * If two options are specified, the first one affects
622 * the horizontal behaviour and the second one the vertical behaviour.
623 * If only one option is specified, it affects both.
626 * <title>A border image</title>
627 * <inlinegraphic fileref="border1.png" format="PNG"/>
628 * <para>This border image was specified with
629 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
633 * <title>A repeating border image</title>
634 * <inlinegraphic fileref="border2.png" format="PNG"/>
635 * <para>This border image was specified with
636 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
640 * <title>A stretched border image</title>
641 * <inlinegraphic fileref="border3.png" format="PNG"/>
642 * <para>This border image was specified with
643 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
647 * <refsect2 id="gtkcssprovider-transitions">
648 * <para>Styles can specify transitions that will be used to create a gradual
649 * change in the appearance when a widget state changes. The following
650 * syntax is used to specify transitions:
651 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
652 * The @duration is the amount of time that the animation will take for
653 * a complete cycle from start to end. If the loop option is given, the
654 * animation will be repated until the state changes again.
655 * The option after the duration determines the transition function from a
656 * small set of predefined functions.
657 * <figure><title>Linear transition</title>
658 * <graphic fileref="linear.png" format="PNG"/>
660 * <figure><title>Ease transition</title>
661 * <graphic fileref="ease.png" format="PNG"/>
663 * <figure><title>Ease-in-out transition</title>
664 * <graphic fileref="ease-in-out.png" format="PNG"/>
666 * <figure><title>Ease-in transition</title>
667 * <graphic fileref="ease-in.png" format="PNG"/>
669 * <figure><title>Ease-out transition</title>
670 * <graphic fileref="ease-out.png" format="PNG"/>
674 * <refsect2 id="gtkcssprovider-properties">
675 * <title>Supported properties</title>
677 * Properties are the part that differ the most to common CSS,
678 * not all properties are supported (some are planned to be
679 * supported eventually, some others are meaningless or don't
680 * map intuitively in a widget based environment).
683 * The currently supported properties are:
689 * <entry>Property name</entry>
690 * <entry>Syntax</entry>
691 * <entry>Maps to</entry>
692 * <entry>Examples</entry>
697 * <entry>engine</entry>
698 * <entry>engine-name</entry>
699 * <entry>#GtkThemingEngine</entry>
700 * <entry>engine: clearlooks;
701 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
704 * <entry>background-color</entry>
705 * <entry morerows="2">color (see above)</entry>
706 * <entry morerows="7">#GdkRGBA</entry>
707 * <entry morerows="7"><literallayout>background-color: #fff;
708 * color: &color1;
709 * background-color: shade (&color1, 0.5);
710 * color: mix (&color1, #f0f, 0.8);</literallayout>
714 * <entry>color</entry>
717 * <entry>border-top-color</entry>
718 * <entry morerows="4">transparent|color (see above)</entry>
721 * <entry>border-right-color</entry>
724 * <entry>border-bottom-color</entry>
727 * <entry>border-left-color</entry>
730 * <entry>border-color</entry>
731 * <entry>[transparent|color]{1,4}</entry>
734 * <entry>font-family</entry>
735 * <entry>@family [, @family]*</entry>
736 * <entry>#gchararray</entry>
737 * <entry>font-family: Sans, Arial;</entry>
740 * <entry>font-style</entry>
741 * <entry>[normal|oblique|italic]</entry>
742 * <entry>#PANGO_TYPE_STYLE</entry>
743 * <entry>font-style: italic;</entry>
746 * <entry>font-variant</entry>
747 * <entry>[normal|small-caps]</entry>
748 * <entry>#PANGO_TYPE_VARIANT</entry>
749 * <entry>font-variant: normal;</entry>
752 * <entry>font-weight</entry>
753 * <entry>[normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900]</entry>
754 * <entry>#PANGO_TYPE_WEIGHT</entry>
755 * <entry>font-weight: bold;</entry>
758 * <entry>font-size</entry>
759 * <entry>Font size in point</entry>
760 * <entry>#gint</entry>
761 * <entry>font-size: 13;</entry>
764 * <entry>font</entry>
765 * <entry>@family [@style] [@size]</entry>
766 * <entry>#PangoFontDescription</entry>
767 * <entry>font: Sans 15;</entry>
770 * <entry>margin-top</entry>
771 * <entry>integer</entry>
772 * <entry>#gint</entry>
773 * <entry>margin-top: 0;</entry>
776 * <entry>margin-left</entry>
777 * <entry>integer</entry>
778 * <entry>#gint</entry>
779 * <entry>margin-left: 1;</entry>
782 * <entry>margin-bottom</entry>
783 * <entry>integer</entry>
784 * <entry>#gint</entry>
785 * <entry>margin-bottom: 2;</entry>
788 * <entry>margin-right</entry>
789 * <entry>integer</entry>
790 * <entry>#gint</entry>
791 * <entry>margin-right: 4;</entry>
794 * <entry>margin</entry>
795 * <entry morerows="1"><literallayout>@width
796 * @vertical_width @horizontal_width
797 * @top_width @horizontal_width @bottom_width
798 * @top_width @right_width @bottom_width @left_width</literallayout>
800 * <entry morerows="1">#GtkBorder</entry>
801 * <entry morerows="1"><literallayout>margin: 5;
804 * margin: 5 10 3 5;</literallayout>
808 * <entry>padding-top</entry>
809 * <entry>integer</entry>
810 * <entry>#gint</entry>
811 * <entry>padding-top: 5;</entry>
814 * <entry>padding-left</entry>
815 * <entry>integer</entry>
816 * <entry>#gint</entry>
817 * <entry>padding-left: 5;</entry>
820 * <entry>padding-bottom</entry>
821 * <entry>integer</entry>
822 * <entry>#gint</entry>
823 * <entry>padding-bottom: 5;</entry>
826 * <entry>padding-right</entry>
827 * <entry>integer</entry>
828 * <entry>#gint</entry>
829 * <entry>padding-right: 5;</entry>
832 * <entry>padding</entry>
835 * <entry>background-image</entry>
836 * <entry><literallayout>gradient (see above) or
837 * url(@path)</literallayout></entry>
838 * <entry>#cairo_pattern_t</entry>
839 * <entry><literallayout>-gtk-gradient (linear,
840 * left top, right top,
841 * from (#fff), to (#000));
842 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
844 * color-stop (0.5, #f00),
846 * -gtk-gradient (radial,
847 * center center, 0.2,
848 * center center, 0.8,
849 * color-stop (0.0, #fff),
850 * color-stop (1.0, #000));
851 * url ('background.png');</literallayout>
855 * <entry>background-repeat</entry>
856 * <entry>[repeat|no-repeat]</entry>
857 * <entry>internal</entry>
858 * <entry><literallayout>background-repeat: no-repeat;</literallayout>
859 * If not specified, the style doesn't respect the CSS3
860 * specification, since the background will be
861 * stretched to fill the area.
865 * <entry>border-top-width</entry>
866 * <entry>integer</entry>
867 * <entry>#gint</entry>
868 * <entry>border-top-width: 5;</entry>
871 * <entry>border-left-width</entry>
872 * <entry>integer</entry>
873 * <entry>#gint</entry>
874 * <entry>border-left-width: 5;</entry>
877 * <entry>border-bottom-width</entry>
878 * <entry>integer</entry>
879 * <entry>#gint</entry>
880 * <entry>border-bottom-width: 5;</entry>
883 * <entry>border-right-width</entry>
884 * <entry>integer</entry>
885 * <entry>#gint</entry>
886 * <entry>border-right-width: 5;</entry>
889 * <entry>border-width</entry>
890 * <entry morerows="1">#GtkBorder</entry>
891 * <entry morerows="1"><literallayout>border-width: 1;
893 * border-width: 1 2 3;
894 * border-width: 1 2 3 5;</literallayout>
898 * <entry>border-radius</entry>
899 * <entry>integer</entry>
900 * <entry>#gint</entry>
901 * <entry>border-radius: 5;</entry>
904 * <entry>border-style</entry>
905 * <entry>[none|solid|inset|outset]</entry>
906 * <entry>#GtkBorderStyle</entry>
907 * <entry>border-style: solid;</entry>
910 * <entry>border-image</entry>
911 * <entry><literallayout>border image (see above)</literallayout></entry>
912 * <entry>internal use only</entry>
913 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
914 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
918 * <entry>text-shadow</entry>
919 * <entry>shadow list (see above)</entry>
920 * <entry>internal use only</entry>
921 * <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
924 * <entry>transition</entry>
925 * <entry>transition (see above)</entry>
926 * <entry>internal use only</entry>
927 * <entry><literallayout>transition: 150ms ease-in-out;
928 * transition: 1s linear loop;</literallayout>
932 * <entry>gtk-key-bindings</entry>
933 * <entry>binding set name list</entry>
934 * <entry>internal use only</entry>
935 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
942 * GtkThemingEngines can register their own, engine-specific style properties
943 * with the function gtk_theming_engine_register_property(). These properties
944 * can be set in CSS like other properties, using a name of the form
945 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
946 * the name of the theming engine, and <replaceable>name</replaceable> is the
947 * name of the property. Style properties that have been registered by widgets
948 * using gtk_widget_class_install_style_property() can also be set in this
949 * way, using the widget class name for <replaceable>namespace</replaceable>.
952 * <title>Using engine-specific style properties</title>
955 * engine: clearlooks;
957 * -GtkPaned-handle-size: 6;
958 * -clearlooks-colorize-scrollbar: false;
965 typedef struct GtkCssRuleset GtkCssRuleset;
966 typedef struct _GtkCssScanner GtkCssScanner;
967 typedef struct _PropertyValue PropertyValue;
968 typedef struct _WidgetPropertyValue WidgetPropertyValue;
969 typedef enum ParserScope ParserScope;
970 typedef enum ParserSymbol ParserSymbol;
972 struct _PropertyValue {
973 GtkCssStyleProperty *property;
975 GtkCssSection *section;
978 struct _WidgetPropertyValue {
979 WidgetPropertyValue *next;
983 GtkCssSection *section;
988 GtkCssSelector *selector;
989 GtkCssSelectorTree *selector_match;
990 WidgetPropertyValue *widget_style;
991 PropertyValue *styles;
992 GtkBitmask *set_styles;
994 guint owns_styles : 1;
995 guint owns_widget_style : 1;
998 struct _GtkCssScanner
1000 GtkCssProvider *provider;
1001 GtkCssParser *parser;
1002 GtkCssSection *section;
1003 GtkCssScanner *parent;
1007 struct _GtkCssProviderPrivate
1011 GHashTable *symbolic_colors;
1012 GHashTable *keyframes;
1015 GtkCssSelectorTree *tree;
1016 GResource *resource;
1024 static gboolean gtk_keep_css_sections = FALSE;
1026 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
1028 static void gtk_css_provider_finalize (GObject *object);
1029 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
1030 static void gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface);
1031 static void widget_property_value_list_free (WidgetPropertyValue *head);
1034 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
1035 GtkCssScanner *scanner,
1041 gtk_css_provider_error_quark (void)
1043 return g_quark_from_static_string ("gtk-css-provider-error-quark");
1046 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
1047 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
1048 gtk_css_style_provider_iface_init)
1049 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
1050 gtk_css_style_provider_private_iface_init));
1053 gtk_css_provider_parsing_error (GtkCssProvider *provider,
1054 GtkCssSection *section,
1055 const GError *error)
1057 /* Only emit a warning when we have no error handlers. This is our
1058 * default handlers. And in this case erroneous CSS files are a bug
1059 * and should be fixed.
1060 * Note that these warnings can also be triggered by a broken theme
1061 * that people installed from some weird location on the internets.
1063 if (!g_signal_has_handler_pending (provider,
1064 css_provider_signals[PARSING_ERROR],
1068 char *s = _gtk_css_section_to_string (section);
1070 g_warning ("Theme parsing error: %s: %s",
1079 gtk_css_provider_class_init (GtkCssProviderClass *klass)
1081 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1083 if (g_getenv ("GTK_CSS_DEBUG"))
1084 gtk_keep_css_sections = TRUE;
1087 * GtkCssProvider::parsing-error:
1088 * @provider: the provider that had a parsing error
1089 * @section: section the error happened in
1090 * @error: The parsing error
1092 * Signals that a parsing error occured. the @path, @line and @position
1093 * describe the actual location of the error as accurately as possible.
1095 * Parsing errors are never fatal, so the parsing will resume after
1096 * the error. Errors may however cause parts of the given
1097 * data or even all of it to not be parsed at all. So it is a useful idea
1098 * to check that the parsing succeeds by connecting to this signal.
1100 * Note that this signal may be emitted at any time as the css provider
1101 * may opt to defer parsing parts or all of the input to a later time
1102 * than when a loading function was called.
1104 css_provider_signals[PARSING_ERROR] =
1105 g_signal_new (I_("parsing-error"),
1106 G_TYPE_FROM_CLASS (object_class),
1108 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
1110 _gtk_marshal_VOID__BOXED_BOXED,
1111 G_TYPE_NONE, 2, GTK_TYPE_CSS_SECTION, G_TYPE_ERROR);
1113 object_class->finalize = gtk_css_provider_finalize;
1115 klass->parsing_error = gtk_css_provider_parsing_error;
1117 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
1121 gtk_css_ruleset_init_copy (GtkCssRuleset *new,
1122 GtkCssRuleset *ruleset,
1123 GtkCssSelector *selector)
1125 memcpy (new, ruleset, sizeof (GtkCssRuleset));
1127 new->selector = selector;
1128 /* First copy takes over ownership */
1129 if (ruleset->owns_styles)
1130 ruleset->owns_styles = FALSE;
1131 if (ruleset->owns_widget_style)
1132 ruleset->owns_widget_style = FALSE;
1133 if (new->set_styles)
1134 new->set_styles = _gtk_bitmask_copy (new->set_styles);
1138 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
1140 if (ruleset->owns_styles)
1144 for (i = 0; i < ruleset->n_styles; i++)
1146 _gtk_css_value_unref (ruleset->styles[i].value);
1147 ruleset->styles[i].value = NULL;
1148 if (ruleset->styles[i].section)
1149 gtk_css_section_unref (ruleset->styles[i].section);
1151 g_free (ruleset->styles);
1153 if (ruleset->set_styles)
1154 _gtk_bitmask_free (ruleset->set_styles);
1155 if (ruleset->owns_widget_style)
1156 widget_property_value_list_free (ruleset->widget_style);
1157 if (ruleset->selector)
1158 _gtk_css_selector_free (ruleset->selector);
1160 memset (ruleset, 0, sizeof (GtkCssRuleset));
1163 static WidgetPropertyValue *
1164 widget_property_value_new (char *name, GtkCssSection *section)
1166 WidgetPropertyValue *value;
1168 value = g_slice_new0 (WidgetPropertyValue);
1171 if (gtk_keep_css_sections)
1172 value->section = gtk_css_section_ref (section);
1178 widget_property_value_free (WidgetPropertyValue *value)
1180 g_free (value->value);
1181 g_free (value->name);
1183 gtk_css_section_unref (value->section);
1185 g_slice_free (WidgetPropertyValue, value);
1189 widget_property_value_list_free (WidgetPropertyValue *head)
1191 WidgetPropertyValue *l, *next;
1192 for (l = head; l != NULL; l = next)
1195 widget_property_value_free (l);
1199 static WidgetPropertyValue *
1200 widget_property_value_list_remove_name (WidgetPropertyValue *head, const char *name)
1202 WidgetPropertyValue *l, **last;
1206 for (l = head; l != NULL; l = l->next)
1208 if (strcmp (l->name, name) == 0)
1211 widget_property_value_free (l);
1222 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
1224 WidgetPropertyValue *value)
1226 value->next = widget_property_value_list_remove_name (ruleset->widget_style, name);
1227 ruleset->widget_style = value;
1228 ruleset->owns_widget_style = TRUE;
1232 gtk_css_ruleset_add (GtkCssRuleset *ruleset,
1233 GtkCssStyleProperty *property,
1235 GtkCssSection *section)
1239 g_return_if_fail (ruleset->owns_styles || ruleset->n_styles == 0);
1241 if (ruleset->set_styles == NULL)
1242 ruleset->set_styles = _gtk_bitmask_new ();
1244 ruleset->set_styles = _gtk_bitmask_set (ruleset->set_styles,
1245 _gtk_css_style_property_get_id (property),
1248 ruleset->owns_styles = TRUE;
1250 for (i = 0; i < ruleset->n_styles; i++)
1252 if (ruleset->styles[i].property == property)
1254 _gtk_css_value_unref (ruleset->styles[i].value);
1255 ruleset->styles[i].value = NULL;
1256 if (ruleset->styles[i].section)
1257 gtk_css_section_unref (ruleset->styles[i].section);
1261 if (i == ruleset->n_styles)
1263 ruleset->n_styles++;
1264 ruleset->styles = g_realloc (ruleset->styles, ruleset->n_styles * sizeof (PropertyValue));
1265 ruleset->styles[i].value = NULL;
1266 ruleset->styles[i].property = property;
1269 ruleset->styles[i].value = value;
1270 if (gtk_keep_css_sections)
1271 ruleset->styles[i].section = gtk_css_section_ref (section);
1273 ruleset->styles[i].section = NULL;
1277 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1279 if (scanner->section)
1280 gtk_css_section_unref (scanner->section);
1281 g_object_unref (scanner->provider);
1282 _gtk_css_parser_free (scanner->parser);
1284 g_slice_free (GtkCssScanner, scanner);
1288 gtk_css_provider_emit_error (GtkCssProvider *provider,
1289 GtkCssScanner *scanner,
1290 const GError *error)
1292 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1293 scanner != NULL ? scanner->section : NULL, error);
1297 gtk_css_scanner_parser_error (GtkCssParser *parser,
1298 const GError *error,
1301 GtkCssScanner *scanner = user_data;
1303 gtk_css_provider_emit_error (scanner->provider,
1308 static GtkCssScanner *
1309 gtk_css_scanner_new (GtkCssProvider *provider,
1310 GtkCssScanner *parent,
1311 GtkCssSection *section,
1315 GtkCssScanner *scanner;
1317 scanner = g_slice_new0 (GtkCssScanner);
1319 g_object_ref (provider);
1320 scanner->provider = provider;
1321 scanner->parent = parent;
1323 scanner->section = gtk_css_section_ref (section);
1325 scanner->parser = _gtk_css_parser_new (text,
1327 gtk_css_scanner_parser_error,
1334 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1339 GFile *parser_file = _gtk_css_parser_get_file (scanner->parser);
1340 if (parser_file && g_file_equal (parser_file, file))
1343 scanner = scanner->parent;
1350 gtk_css_scanner_push_section (GtkCssScanner *scanner,
1351 GtkCssSectionType section_type)
1353 GtkCssSection *section;
1355 section = _gtk_css_section_new (scanner->section,
1359 if (scanner->section)
1360 gtk_css_section_unref (scanner->section);
1361 scanner->section = section;
1365 gtk_css_scanner_pop_section (GtkCssScanner *scanner,
1366 GtkCssSectionType check_type)
1368 GtkCssSection *parent;
1370 g_assert (gtk_css_section_get_section_type (scanner->section) == check_type);
1372 parent = gtk_css_section_get_parent (scanner->section);
1374 gtk_css_section_ref (parent);
1376 _gtk_css_section_end (scanner->section);
1377 gtk_css_section_unref (scanner->section);
1379 scanner->section = parent;
1383 gtk_css_provider_init (GtkCssProvider *css_provider)
1385 GtkCssProviderPrivate *priv;
1387 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1388 GTK_TYPE_CSS_PROVIDER,
1389 GtkCssProviderPrivate);
1391 priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1393 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1394 (GDestroyNotify) g_free,
1395 (GDestroyNotify) _gtk_css_value_unref);
1396 priv->keyframes = g_hash_table_new_full (g_str_hash, g_str_equal,
1397 (GDestroyNotify) g_free,
1398 (GDestroyNotify) _gtk_css_value_unref);
1402 verify_tree_match_results (GtkCssProvider *provider,
1403 const GtkCssMatcher *matcher,
1404 GPtrArray *tree_rules)
1407 GtkCssProviderPrivate *priv = provider->priv;
1408 GtkCssRuleset *ruleset;
1409 gboolean should_match;
1412 for (i = 0; i < priv->rulesets->len; i++)
1414 gboolean found = FALSE;
1416 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1418 for (j = 0; j < tree_rules->len; j++)
1420 if (ruleset == tree_rules->pdata[j])
1426 should_match = _gtk_css_selector_matches (ruleset->selector, matcher);
1427 if (found != !!should_match)
1429 g_error ("expected rule '%s' to %s, but it %s\n",
1430 _gtk_css_selector_to_string (ruleset->selector),
1431 should_match ? "match" : "not match",
1432 found ? "matched" : "didn't match");
1439 verify_tree_get_change_results (GtkCssProvider *provider,
1440 const GtkCssMatcher *matcher,
1441 GtkCssChange change)
1445 GtkCssChange verify_change = 0;
1446 GPtrArray *tree_rules;
1449 tree_rules = _gtk_css_selector_tree_match_all (provider->priv->tree, matcher);
1450 verify_tree_match_results (provider, matcher, tree_rules);
1452 for (i = tree_rules->len - 1; i >= 0; i--)
1454 GtkCssRuleset *ruleset;
1456 ruleset = tree_rules->pdata[i];
1458 verify_change |= _gtk_css_selector_tree_match_get_change (ruleset->selector_match);
1461 if (change != verify_change)
1465 s = g_string_new ("");
1466 g_string_append_printf (s, "expected change 0x%x, but it was 0x%x", verify_change, change);
1467 if ((change & ~verify_change) != 0)
1468 g_string_append_printf (s, ", unexpectedly set: 0x%x", change & ~verify_change);
1469 if ((~change & verify_change) != 0)
1470 g_string_append_printf (s, ", unexpectedly no set: 0x%x", ~change & verify_change);
1472 g_string_free (s, TRUE);
1475 g_ptr_array_free (tree_rules, TRUE);
1482 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1483 GtkWidgetPath *path,
1484 GtkStateFlags state,
1488 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1489 GtkCssProviderPrivate *priv = css_provider->priv;
1490 WidgetPropertyValue *val;
1491 GPtrArray *tree_rules;
1492 GtkCssMatcher matcher;
1493 gboolean found = FALSE;
1497 if (!_gtk_css_matcher_init (&matcher, path, state))
1500 tree_rules = _gtk_css_selector_tree_match_all (priv->tree, &matcher);
1501 verify_tree_match_results (css_provider, &matcher, tree_rules);
1503 prop_name = g_strdup_printf ("-%s-%s",
1504 g_type_name (pspec->owner_type),
1507 for (i = tree_rules->len - 1; i >= 0; i--)
1509 GtkCssRuleset *ruleset = tree_rules->pdata[i];
1511 if (ruleset->widget_style == NULL)
1514 for (val = ruleset->widget_style; val != NULL; val = val->next)
1516 if (strcmp (val->name, prop_name) == 0)
1518 GtkCssScanner *scanner;
1520 scanner = gtk_css_scanner_new (css_provider,
1523 val->section != NULL ? gtk_css_section_get_file (val->section) : NULL,
1526 found = _gtk_css_style_parse_value (value,
1529 gtk_css_scanner_destroy (scanner);
1540 g_ptr_array_free (tree_rules, TRUE);
1546 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1548 iface->get_style_property = gtk_css_provider_get_style_property;
1551 static GtkCssValue *
1552 gtk_css_style_provider_get_color (GtkStyleProviderPrivate *provider,
1555 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1557 return g_hash_table_lookup (css_provider->priv->symbolic_colors, name);
1560 static GtkCssKeyframes *
1561 gtk_css_style_provider_get_keyframes (GtkStyleProviderPrivate *provider,
1564 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1566 return g_hash_table_lookup (css_provider->priv->keyframes, name);
1570 gtk_css_style_provider_lookup (GtkStyleProviderPrivate *provider,
1571 const GtkCssMatcher *matcher,
1572 GtkCssLookup *lookup)
1574 GtkCssProvider *css_provider;
1575 GtkCssProviderPrivate *priv;
1576 GtkCssRuleset *ruleset;
1579 GPtrArray *tree_rules;
1581 css_provider = GTK_CSS_PROVIDER (provider);
1582 priv = css_provider->priv;
1584 tree_rules = _gtk_css_selector_tree_match_all (priv->tree, matcher);
1585 verify_tree_match_results (css_provider, matcher, tree_rules);
1587 for (i = tree_rules->len - 1; i >= 0; i--)
1589 ruleset = tree_rules->pdata[i];
1591 if (ruleset->styles == NULL)
1594 if (!_gtk_bitmask_intersects (_gtk_css_lookup_get_missing (lookup),
1595 ruleset->set_styles))
1598 for (j = 0; j < ruleset->n_styles; j++)
1600 GtkCssStyleProperty *prop = ruleset->styles[j].property;
1601 guint id = _gtk_css_style_property_get_id (prop);
1603 if (!_gtk_css_lookup_is_missing (lookup, id))
1606 _gtk_css_lookup_set (lookup,
1608 ruleset->styles[j].section,
1609 ruleset->styles[j].value);
1612 if (_gtk_bitmask_is_empty (_gtk_css_lookup_get_missing (lookup)))
1616 g_ptr_array_free (tree_rules, TRUE);
1620 gtk_css_style_provider_get_change (GtkStyleProviderPrivate *provider,
1621 const GtkCssMatcher *matcher)
1623 GtkCssProvider *css_provider;
1624 GtkCssProviderPrivate *priv;
1625 GtkCssChange change;
1627 css_provider = GTK_CSS_PROVIDER (provider);
1628 priv = css_provider->priv;
1630 change = _gtk_css_selector_tree_get_change_all (priv->tree, matcher);
1632 verify_tree_get_change_results (css_provider, matcher, change);
1638 gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface)
1640 iface->get_color = gtk_css_style_provider_get_color;
1641 iface->get_keyframes = gtk_css_style_provider_get_keyframes;
1642 iface->lookup = gtk_css_style_provider_lookup;
1643 iface->get_change = gtk_css_style_provider_get_change;
1647 gtk_css_provider_finalize (GObject *object)
1649 GtkCssProvider *css_provider;
1650 GtkCssProviderPrivate *priv;
1653 css_provider = GTK_CSS_PROVIDER (object);
1654 priv = css_provider->priv;
1656 for (i = 0; i < priv->rulesets->len; i++)
1657 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1659 g_array_free (priv->rulesets, TRUE);
1660 _gtk_css_selector_tree_free (priv->tree);
1662 g_hash_table_destroy (priv->symbolic_colors);
1663 g_hash_table_destroy (priv->keyframes);
1667 g_resources_unregister (priv->resource);
1668 g_resource_unref (priv->resource);
1669 priv->resource = NULL;
1672 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1676 * gtk_css_provider_new:
1678 * Returns a newly created #GtkCssProvider.
1680 * Returns: A new #GtkCssProvider
1683 gtk_css_provider_new (void)
1685 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1689 gtk_css_provider_take_error (GtkCssProvider *provider,
1690 GtkCssScanner *scanner,
1693 gtk_css_provider_emit_error (provider,
1697 g_error_free (error);
1701 gtk_css_provider_error_literal (GtkCssProvider *provider,
1702 GtkCssScanner *scanner,
1705 const char *message)
1707 gtk_css_provider_take_error (provider,
1709 g_error_new_literal (domain, code, message));
1713 gtk_css_provider_error (GtkCssProvider *provider,
1714 GtkCssScanner *scanner,
1718 ...) G_GNUC_PRINTF (5, 6);
1720 gtk_css_provider_error (GtkCssProvider *provider,
1721 GtkCssScanner *scanner,
1730 va_start (args, format);
1731 error = g_error_new_valist (domain, code, format, args);
1734 gtk_css_provider_take_error (provider, scanner, error);
1738 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1739 GtkCssScanner *scanner,
1740 const char *expected)
1742 gtk_css_provider_error (provider,
1744 GTK_CSS_PROVIDER_ERROR,
1745 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1746 "expected a valid %s", expected);
1750 css_provider_commit (GtkCssProvider *css_provider,
1752 GtkCssRuleset *ruleset)
1754 GtkCssProviderPrivate *priv;
1757 priv = css_provider->priv;
1759 if (ruleset->styles == NULL && ruleset->widget_style == NULL)
1761 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1765 for (l = selectors; l; l = l->next)
1769 gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1771 g_array_append_val (priv->rulesets, new);
1774 g_slist_free (selectors);
1778 gtk_css_provider_reset (GtkCssProvider *css_provider)
1780 GtkCssProviderPrivate *priv;
1783 priv = css_provider->priv;
1787 g_resources_unregister (priv->resource);
1788 g_resource_unref (priv->resource);
1789 priv->resource = NULL;
1792 g_hash_table_remove_all (priv->symbolic_colors);
1793 g_hash_table_remove_all (priv->keyframes);
1795 for (i = 0; i < priv->rulesets->len; i++)
1796 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1797 g_array_set_size (priv->rulesets, 0);
1798 _gtk_css_selector_tree_free (priv->tree);
1804 gtk_css_provider_propagate_error (GtkCssProvider *provider,
1805 GtkCssSection *section,
1806 const GError *error,
1807 GError **propagate_to)
1812 /* don't fail for deprecations */
1813 if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
1815 s = _gtk_css_section_to_string (section);
1816 g_warning ("Theme parsing error: %s: %s", s, error->message);
1821 /* we already set an error. And we'd like to keep the first one */
1825 *propagate_to = g_error_copy (error);
1828 s = _gtk_css_section_to_string (section);
1829 g_prefix_error (propagate_to, "%s", s);
1835 parse_import (GtkCssScanner *scanner)
1839 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_IMPORT);
1841 if (!_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1843 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1847 if (_gtk_css_parser_is_string (scanner->parser))
1851 uri = _gtk_css_parser_read_string (scanner->parser);
1852 file = _gtk_css_parser_get_file_for_path (scanner->parser, uri);
1857 file = _gtk_css_parser_read_url (scanner->parser);
1862 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1863 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1867 if (!_gtk_css_parser_try (scanner->parser, ";", FALSE))
1869 gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1870 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1872 else if (gtk_css_scanner_would_recurse (scanner, file))
1874 char *path = g_file_get_path (file);
1875 gtk_css_provider_error (scanner->provider,
1877 GTK_CSS_PROVIDER_ERROR,
1878 GTK_CSS_PROVIDER_ERROR_IMPORT,
1879 "Loading '%s' would recurse",
1885 gtk_css_provider_load_internal (scanner->provider,
1892 g_object_unref (file);
1894 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1895 _gtk_css_parser_skip_whitespace (scanner->parser);
1901 parse_color_definition (GtkCssScanner *scanner)
1906 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1908 if (!_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1910 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1914 name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1917 gtk_css_provider_error_literal (scanner->provider,
1919 GTK_CSS_PROVIDER_ERROR,
1920 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1921 "Not a valid color name");
1922 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1923 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1927 color = _gtk_css_color_value_parse (scanner->parser);
1931 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1932 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1936 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1939 _gtk_css_value_unref (color);
1940 gtk_css_provider_error_literal (scanner->provider,
1942 GTK_CSS_PROVIDER_ERROR,
1943 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1944 "Missing semicolon at end of color definition");
1945 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1947 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1951 g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, color);
1953 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1958 parse_binding_set (GtkCssScanner *scanner)
1960 GtkBindingSet *binding_set;
1963 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1965 if (!_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
1967 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
1971 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1974 gtk_css_provider_error_literal (scanner->provider,
1976 GTK_CSS_PROVIDER_ERROR,
1977 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1978 "Expected name for binding set");
1979 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1980 goto skip_semicolon;
1983 binding_set = gtk_binding_set_find (name);
1986 binding_set = gtk_binding_set_new (name);
1987 binding_set->parsed = TRUE;
1991 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
1993 gtk_css_provider_error_literal (scanner->provider,
1995 GTK_CSS_PROVIDER_ERROR,
1996 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1997 "Expected '{' for binding set");
1998 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1999 goto skip_semicolon;
2002 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2003 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2005 name = _gtk_css_parser_read_value (scanner->parser);
2008 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2012 if (gtk_binding_entry_add_signal_from_string (binding_set, name) != G_TOKEN_NONE)
2014 gtk_css_provider_error_literal (scanner->provider,
2016 GTK_CSS_PROVIDER_ERROR,
2017 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2018 "Failed to parse binding set.");
2023 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2025 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2026 !_gtk_css_parser_is_eof (scanner->parser))
2028 gtk_css_provider_error_literal (scanner->provider,
2030 GTK_CSS_PROVIDER_ERROR,
2031 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2032 "Expected semicolon");
2033 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2038 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2040 gtk_css_provider_error_literal (scanner->provider,
2042 GTK_CSS_PROVIDER_ERROR,
2043 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2044 "expected '}' after declarations");
2045 if (!_gtk_css_parser_is_eof (scanner->parser))
2046 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2050 if (_gtk_css_parser_begins_with (scanner->parser, ';'))
2052 gtk_css_provider_error_literal (scanner->provider,
2054 GTK_CSS_PROVIDER_ERROR,
2055 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
2056 "Nonstandard semicolon at end of binding set");
2057 _gtk_css_parser_try (scanner->parser, ";", TRUE);
2060 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
2066 parse_keyframes (GtkCssScanner *scanner)
2068 GtkCssKeyframes *keyframes;
2071 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_KEYFRAMES);
2073 if (!_gtk_css_parser_try (scanner->parser, "@keyframes", TRUE))
2075 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_KEYFRAMES);
2079 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2082 gtk_css_provider_error_literal (scanner->provider,
2084 GTK_CSS_PROVIDER_ERROR,
2085 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2086 "Expected name for keyframes");
2087 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2091 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2093 gtk_css_provider_error_literal (scanner->provider,
2095 GTK_CSS_PROVIDER_ERROR,
2096 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2097 "Expected '{' for keyframes");
2098 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2103 keyframes = _gtk_css_keyframes_parse (scanner->parser);
2104 if (keyframes == NULL)
2106 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2111 g_hash_table_insert (scanner->provider->priv->keyframes, name, keyframes);
2113 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2115 gtk_css_provider_error_literal (scanner->provider,
2117 GTK_CSS_PROVIDER_ERROR,
2118 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2119 "expected '}' after declarations");
2120 if (!_gtk_css_parser_is_eof (scanner->parser))
2121 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2125 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_KEYFRAMES);
2131 parse_at_keyword (GtkCssScanner *scanner)
2133 if (parse_import (scanner))
2135 if (parse_color_definition (scanner))
2137 if (parse_binding_set (scanner))
2139 if (parse_keyframes (scanner))
2144 gtk_css_provider_error_literal (scanner->provider,
2146 GTK_CSS_PROVIDER_ERROR,
2147 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2149 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2154 parse_selector_list (GtkCssScanner *scanner)
2156 GSList *selectors = NULL;
2158 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_SELECTOR);
2161 GtkCssSelector *select = _gtk_css_selector_parse (scanner->parser);
2165 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2166 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2167 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2171 selectors = g_slist_prepend (selectors, select);
2173 while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
2175 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2181 parse_declaration (GtkCssScanner *scanner,
2182 GtkCssRuleset *ruleset)
2184 GtkStyleProperty *property;
2187 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DECLARATION);
2189 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2191 goto check_for_semicolon;
2193 property = _gtk_style_property_lookup (name);
2194 if (property == NULL && name[0] != '-')
2196 gtk_css_provider_error (scanner->provider,
2198 GTK_CSS_PROVIDER_ERROR,
2199 GTK_CSS_PROVIDER_ERROR_NAME,
2200 "'%s' is not a valid property name",
2202 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2204 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2208 if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
2210 gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
2211 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2213 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2223 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2225 value = _gtk_style_property_parse_value (property,
2230 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2231 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2232 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2236 if (!_gtk_css_parser_begins_with (scanner->parser, ';') &&
2237 !_gtk_css_parser_begins_with (scanner->parser, '}') &&
2238 !_gtk_css_parser_is_eof (scanner->parser))
2240 gtk_css_provider_error_literal (scanner->provider,
2242 GTK_CSS_PROVIDER_ERROR,
2243 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2244 "Junk at end of value");
2245 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2246 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2247 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2251 if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
2253 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
2256 for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
2258 GtkCssStyleProperty *child = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
2259 GtkCssValue *sub = _gtk_css_array_value_get_nth (value, i);
2261 gtk_css_ruleset_add (ruleset, child, _gtk_css_value_ref (sub), scanner->section);
2264 _gtk_css_value_unref (value);
2266 else if (GTK_IS_CSS_STYLE_PROPERTY (property))
2268 gtk_css_ruleset_add (ruleset, GTK_CSS_STYLE_PROPERTY (property), value, scanner->section);
2272 g_assert_not_reached ();
2273 _gtk_css_value_unref (value);
2277 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2279 else if (name[0] == '-')
2283 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2285 value_str = _gtk_css_parser_read_value (scanner->parser);
2288 WidgetPropertyValue *val;
2290 val = widget_property_value_new (name, scanner->section);
2291 val->value = value_str;
2293 gtk_css_ruleset_add_style (ruleset, name, val);
2297 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2298 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2299 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2303 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2308 check_for_semicolon:
2309 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2311 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2313 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2314 !_gtk_css_parser_is_eof (scanner->parser))
2316 gtk_css_provider_error_literal (scanner->provider,
2318 GTK_CSS_PROVIDER_ERROR,
2319 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2320 "Expected semicolon");
2321 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2327 parse_declarations (GtkCssScanner *scanner,
2328 GtkCssRuleset *ruleset)
2330 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2331 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2333 parse_declaration (scanner, ruleset);
2338 parse_ruleset (GtkCssScanner *scanner)
2341 GtkCssRuleset ruleset = { 0, };
2343 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_RULESET);
2345 selectors = parse_selector_list (scanner);
2346 if (selectors == NULL)
2348 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2352 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2354 gtk_css_provider_error_literal (scanner->provider,
2356 GTK_CSS_PROVIDER_ERROR,
2357 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2358 "expected '{' after selectors");
2359 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2360 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2361 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2365 parse_declarations (scanner, &ruleset);
2367 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2369 gtk_css_provider_error_literal (scanner->provider,
2371 GTK_CSS_PROVIDER_ERROR,
2372 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2373 "expected '}' after declarations");
2374 if (!_gtk_css_parser_is_eof (scanner->parser))
2376 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2377 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2378 gtk_css_ruleset_clear (&ruleset);
2379 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2383 css_provider_commit (scanner->provider, selectors, &ruleset);
2384 gtk_css_ruleset_clear (&ruleset);
2385 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2389 parse_statement (GtkCssScanner *scanner)
2391 if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2392 parse_at_keyword (scanner);
2394 parse_ruleset (scanner);
2398 parse_stylesheet (GtkCssScanner *scanner)
2400 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2402 _gtk_css_parser_skip_whitespace (scanner->parser);
2404 while (!_gtk_css_parser_is_eof (scanner->parser))
2406 if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2407 _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2410 parse_statement (scanner);
2413 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2417 gtk_css_provider_compare_rule (gconstpointer a_,
2420 const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2421 const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2424 compare = _gtk_css_selector_compare (a->selector, b->selector);
2432 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2434 GtkCssProviderPrivate *priv = css_provider->priv;
2435 GtkCssSelectorTreeBuilder *builder;
2438 g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2440 builder = _gtk_css_selector_tree_builder_new ();
2441 for (i = 0; i < priv->rulesets->len; i++)
2443 GtkCssRuleset *ruleset;
2445 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
2447 _gtk_css_selector_tree_builder_add (builder,
2449 &ruleset->selector_match,
2453 priv->tree = _gtk_css_selector_tree_builder_build (builder);
2454 _gtk_css_selector_tree_builder_free (builder);
2457 for (i = 0; i < priv->rulesets->len; i++)
2459 GtkCssRuleset *ruleset;
2461 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
2463 _gtk_css_selector_free (ruleset->selector);
2464 ruleset->selector = NULL;
2470 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2471 GtkCssScanner *parent,
2476 GtkCssScanner *scanner;
2477 gulong error_handler;
2478 char *free_data = NULL;
2481 error_handler = g_signal_connect (css_provider,
2483 G_CALLBACK (gtk_css_provider_propagate_error),
2486 error_handler = 0; /* silence gcc */
2490 GError *load_error = NULL;
2492 if (g_file_load_contents (file, NULL,
2500 GtkCssSection *section;
2503 section = gtk_css_section_ref (parent->section);
2505 section = _gtk_css_section_new_for_file (GTK_CSS_SECTION_DOCUMENT, file);
2507 gtk_css_provider_error (css_provider,
2509 GTK_CSS_PROVIDER_ERROR,
2510 GTK_CSS_PROVIDER_ERROR_IMPORT,
2511 "Failed to import: %s",
2512 load_error->message);
2514 gtk_css_section_unref (section);
2520 scanner = gtk_css_scanner_new (css_provider,
2522 parent ? parent->section : NULL,
2526 parse_stylesheet (scanner);
2528 gtk_css_scanner_destroy (scanner);
2531 gtk_css_provider_postprocess (css_provider);
2538 g_signal_handler_disconnect (css_provider, error_handler);
2542 /* We clear all contents from the provider for backwards compat reasons */
2543 gtk_css_provider_reset (css_provider);
2552 * gtk_css_provider_load_from_data:
2553 * @css_provider: a #GtkCssProvider
2554 * @data: (array length=length) (element-type guint8): CSS data loaded in memory
2555 * @length: the length of @data in bytes, or -1 for NUL terminated strings. If
2556 * @length is not -1, the code will assume it is not NUL terminated and will
2557 * potentially do a copy.
2558 * @error: (out) (allow-none): return location for a #GError, or %NULL
2560 * Loads @data into @css_provider, making it clear any previously loaded
2563 * Returns: %TRUE. The return value is deprecated and %FALSE will only be
2564 * returned for backwards compatibility reasons if an @error is not
2565 * %NULL and a loading error occured. To track errors while loading
2566 * CSS, connect to the GtkCssProvider::parsing-error signal.
2569 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2577 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2578 g_return_val_if_fail (data != NULL, FALSE);
2582 length = strlen (data);
2587 free_data = g_strndup (data, length);
2591 gtk_css_provider_reset (css_provider);
2593 ret = gtk_css_provider_load_internal (css_provider, NULL, NULL, data, error);
2597 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (css_provider));
2603 * gtk_css_provider_load_from_file:
2604 * @css_provider: a #GtkCssProvider
2605 * @file: #GFile pointing to a file to load
2606 * @error: (out) (allow-none): return location for a #GError, or %NULL
2608 * Loads the data contained in @file into @css_provider, making it
2609 * clear any previously loaded information.
2611 * Returns: %TRUE. The return value is deprecated and %FALSE will only be
2612 * returned for backwards compatibility reasons if an @error is not
2613 * %NULL and a loading error occured. To track errors while loading
2614 * CSS, connect to the GtkCssProvider::parsing-error signal.
2617 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2623 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2624 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2626 gtk_css_provider_reset (css_provider);
2628 success = gtk_css_provider_load_internal (css_provider, NULL, file, NULL, error);
2630 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (css_provider));
2636 * gtk_css_provider_load_from_path:
2637 * @css_provider: a #GtkCssProvider
2638 * @path: the path of a filename to load, in the GLib filename encoding
2639 * @error: (out) (allow-none): return location for a #GError, or %NULL
2641 * Loads the data contained in @path into @css_provider, making it clear
2642 * any previously loaded information.
2644 * Returns: %TRUE. The return value is deprecated and %FALSE will only be
2645 * returned for backwards compatibility reasons if an @error is not
2646 * %NULL and a loading error occured. To track errors while loading
2647 * CSS, connect to the GtkCssProvider::parsing-error signal.
2650 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2657 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2658 g_return_val_if_fail (path != NULL, FALSE);
2660 file = g_file_new_for_path (path);
2662 result = gtk_css_provider_load_from_file (css_provider, file, error);
2664 g_object_unref (file);
2670 gtk_css_provider_load_from_resource (GtkCssProvider *css_provider,
2671 const gchar *resource_path)
2674 char *uri, *escaped;
2676 g_return_if_fail (GTK_IS_CSS_PROVIDER (css_provider));
2677 g_return_if_fail (resource_path != NULL);
2679 escaped = g_uri_escape_string (resource_path,
2680 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
2681 uri = g_strconcat ("resource://", escaped, NULL);
2684 file = g_file_new_for_uri (uri);
2687 gtk_css_provider_load_from_file (css_provider, file, NULL);
2689 g_object_unref (file);
2693 * gtk_css_provider_get_default:
2695 * Returns the provider containing the style settings used as a
2696 * fallback for all widgets.
2698 * Returns: (transfer none): The provider used for fallback styling.
2699 * This memory is owned by GTK+, and you must not free it.
2702 gtk_css_provider_get_default (void)
2704 static GtkCssProvider *provider;
2706 if (G_UNLIKELY (!provider))
2708 provider = gtk_css_provider_new ();
2715 _gtk_css_provider_get_theme_dir (void)
2720 var = g_getenv ("GTK_DATA_PREFIX");
2723 path = g_build_filename (var, "share", "themes", NULL);
2725 path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
2731 * _gtk_css_provider_load_named:
2732 * @provider: a #GtkCssProvider
2733 * @name: A theme name
2734 * @variant: (allow-none): variant to load, for example, "dark", or
2735 * %NULL for the default
2737 * Loads a theme from the usual theme paths. The actual process of
2738 * finding the theme might change between releases, but it is
2739 * guaranteed that this function uses the same mechanism to load the
2740 * theme than GTK uses for loading its own theme.
2743 _gtk_css_provider_load_named (GtkCssProvider *provider,
2745 const gchar *variant)
2747 gchar *subpath, *path;
2748 gchar *resource_path;
2750 g_return_if_fail (GTK_IS_CSS_PROVIDER (provider));
2751 g_return_if_fail (name != NULL);
2753 gtk_css_provider_reset (provider);
2755 /* try loading the resource for the theme. This is mostly meant for built-in
2759 resource_path = g_strdup_printf ("/org/gtk/libgtk/%s-%s.css", name, variant);
2761 resource_path = g_strdup_printf ("/org/gtk/libgtk/%s.css", name);
2763 if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
2765 gtk_css_provider_load_from_resource (provider, resource_path);
2766 g_free (resource_path);
2769 g_free (resource_path);
2772 /* Next try looking for files in the various theme directories.
2775 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
2777 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
2779 /* First look in the user's config directory
2781 path = g_build_filename (g_get_user_data_dir (), "themes", name, subpath, NULL);
2782 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2785 /* Next look in the user's home directory
2787 path = g_build_filename (g_get_home_dir (), ".themes", name, subpath, NULL);
2788 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2794 /* Finally, try in the default theme directory */
2795 theme_dir = _gtk_css_provider_get_theme_dir ();
2796 path = g_build_filename (theme_dir, name, subpath, NULL);
2799 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2811 char *dir, *resource_file;
2812 GResource *resource;
2814 dir = g_path_get_dirname (path);
2815 resource_file = g_build_filename (dir, "gtk.gresource", NULL);
2816 resource = g_resource_load (resource_file, NULL);
2817 g_free (resource_file);
2819 if (resource != NULL)
2820 g_resources_register (resource);
2822 gtk_css_provider_load_from_path (provider, path, NULL);
2824 /* Only set this after load, as load_from_path will clear it */
2825 provider->priv->resource = resource;
2832 /* Things failed! Fall back! Fall back! */
2836 /* If there was a variant, try without */
2837 _gtk_css_provider_load_named (provider, name, NULL);
2841 /* Worst case, fall back to Raleigh */
2842 g_return_if_fail (!g_str_equal (name, "Raleigh")); /* infloop protection */
2843 _gtk_css_provider_load_named (provider, "Raleigh", NULL);
2849 * gtk_css_provider_get_named:
2850 * @name: A theme name
2851 * @variant: (allow-none): variant to load, for example, "dark", or
2852 * %NULL for the default
2854 * Loads a theme from the usual theme paths
2856 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
2857 * This memory is owned by GTK+, and you must not free it.
2860 gtk_css_provider_get_named (const gchar *name,
2861 const gchar *variant)
2863 static GHashTable *themes = NULL;
2864 GtkCssProvider *provider;
2867 if (variant == NULL)
2868 key = g_strdup (name);
2870 key = g_strconcat (name, "-", variant, NULL);
2871 if (G_UNLIKELY (!themes))
2872 themes = g_hash_table_new (g_str_hash, g_str_equal);
2874 provider = g_hash_table_lookup (themes, key);
2878 provider = gtk_css_provider_new ();
2879 _gtk_css_provider_load_named (provider, name, variant);
2880 g_hash_table_insert (themes, g_strdup (key), provider);
2889 compare_properties (gconstpointer a, gconstpointer b, gpointer style)
2891 const guint *ua = a;
2892 const guint *ub = b;
2893 PropertyValue *styles = style;
2895 return strcmp (_gtk_style_property_get_name (GTK_STYLE_PROPERTY (styles[*ua].property)),
2896 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (styles[*ub].property)));
2900 compare_names (gconstpointer a, gconstpointer b)
2902 const WidgetPropertyValue *aa = a;
2903 const WidgetPropertyValue *bb = b;
2904 return strcmp (aa->name, bb->name);
2908 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
2911 GList *values, *walk;
2912 WidgetPropertyValue *widget_value;
2915 _gtk_css_selector_tree_match_print (ruleset->selector_match, str);
2917 g_string_append (str, " {\n");
2919 if (ruleset->styles)
2921 guint *sorted = g_new (guint, ruleset->n_styles);
2923 for (i = 0; i < ruleset->n_styles; i++)
2926 /* so the output is identical for identical selector styles */
2927 g_qsort_with_data (sorted, ruleset->n_styles, sizeof (guint), compare_properties, ruleset->styles);
2929 for (i = 0; i < ruleset->n_styles; i++)
2931 PropertyValue *prop = &ruleset->styles[sorted[i]];
2932 g_string_append (str, " ");
2933 g_string_append (str, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop->property)));
2934 g_string_append (str, ": ");
2935 _gtk_css_value_print (prop->value, str);
2936 g_string_append (str, ";\n");
2942 if (ruleset->widget_style)
2945 for (widget_value = ruleset->widget_style; widget_value != NULL; widget_value = widget_value->next)
2946 values = g_list_prepend (values, widget_value);
2948 /* so the output is identical for identical selector styles */
2949 values = g_list_sort (values, compare_names);
2951 for (walk = values; walk; walk = walk->next)
2953 widget_value = walk->data;
2955 g_string_append (str, " ");
2956 g_string_append (str, widget_value->name);
2957 g_string_append (str, ": ");
2958 g_string_append (str, widget_value->value);
2959 g_string_append (str, ";\n");
2962 g_list_free (values);
2965 g_string_append (str, "}\n");
2969 gtk_css_provider_print_colors (GHashTable *colors,
2974 keys = g_hash_table_get_keys (colors);
2975 /* so the output is identical for identical styles */
2976 keys = g_list_sort (keys, (GCompareFunc) strcmp);
2978 for (walk = keys; walk; walk = walk->next)
2980 const char *name = walk->data;
2981 GtkCssValue *color = g_hash_table_lookup (colors, (gpointer) name);
2983 g_string_append (str, "@define-color ");
2984 g_string_append (str, name);
2985 g_string_append (str, " ");
2986 _gtk_css_value_print (color, str);
2987 g_string_append (str, ";\n");
2994 gtk_css_provider_print_keyframes (GHashTable *keyframes,
2999 keys = g_hash_table_get_keys (keyframes);
3000 /* so the output is identical for identical styles */
3001 keys = g_list_sort (keys, (GCompareFunc) strcmp);
3003 for (walk = keys; walk; walk = walk->next)
3005 const char *name = walk->data;
3006 GtkCssKeyframes *keyframe = g_hash_table_lookup (keyframes, (gpointer) name);
3009 g_string_append (str, "\n");
3010 g_string_append (str, "@keyframes ");
3011 g_string_append (str, name);
3012 g_string_append (str, " {\n");
3013 _gtk_css_keyframes_print (keyframe, str);
3014 g_string_append (str, "}\n");
3021 * gtk_css_provider_to_string:
3022 * @provider: the provider to write to a string
3024 * Convertes the @provider into a string representation in CSS
3027 * Using gtk_css_provider_load_from_data() with the return value
3028 * from this function on a new provider created with
3029 * gtk_css_provider_new() will basicallu create a duplicate of
3032 * Returns: a new string representing the @provider.
3037 gtk_css_provider_to_string (GtkCssProvider *provider)
3039 GtkCssProviderPrivate *priv;
3043 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
3045 priv = provider->priv;
3047 str = g_string_new ("");
3049 gtk_css_provider_print_colors (priv->symbolic_colors, str);
3050 gtk_css_provider_print_keyframes (priv->keyframes, str);
3052 for (i = 0; i < priv->rulesets->len; i++)
3055 g_string_append (str, "\n");
3056 gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
3059 return g_string_free (str, FALSE);