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 "gtkcssstylefuncsprivate.h"
31 #include "gtkcssparserprivate.h"
32 #include "gtkcsssectionprivate.h"
33 #include "gtkcssselectorprivate.h"
34 #include "gtkcssshorthandpropertyprivate.h"
35 #include "gtksymboliccolor.h"
36 #include "gtkstyleprovider.h"
37 #include "gtkstylecontextprivate.h"
38 #include "gtkstylepropertiesprivate.h"
39 #include "gtkstylepropertyprivate.h"
40 #include "gtkstyleproviderprivate.h"
41 #include "gtkbindings.h"
42 #include "gtkmarshalers.h"
43 #include "gtkprivate.h"
47 * SECTION:gtkcssprovider
48 * @Short_description: CSS-like styling for widgets
49 * @Title: GtkCssProvider
50 * @See_also: #GtkStyleContext, #GtkStyleProvider
52 * GtkCssProvider is an object implementing the #GtkStyleProvider interface.
53 * It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
54 * input in order to style widgets.
56 * <refsect2 id="gtkcssprovider-files">
57 * <title>Default files</title>
59 * An application can cause GTK+ to parse a specific CSS style sheet by
60 * calling gtk_css_provider_load_from_file() and adding the provider with
61 * gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
62 * In addition, certain files will be read when GTK+ is initialized. First,
63 * the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
64 * is loaded if it exists. Then, GTK+ tries to load
65 * <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
67 * <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
68 * where <replaceable>theme-name</replaceable> is the name of the current theme
69 * (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
70 * is the prefix configured when GTK+ was compiled, unless overridden by the
71 * <envar>GTK_DATA_PREFIX</envar> environment variable.
74 * <refsect2 id="gtkcssprovider-stylesheets">
75 * <title>Style sheets</title>
77 * The basic structure of the style sheets understood by this provider is
78 * a series of statements, which are either rule sets or '@-rules', separated
82 * A rule set consists of a selector and a declaration block, which is
83 * a series of declarations enclosed in curly braces ({ and }). The
84 * declarations are separated by semicolons (;). Multiple selectors can
85 * share the same declaration block, by putting all the separators in
86 * front of the block, separated by commas.
88 * <example><title>A rule set with two selectors</title>
89 * <programlisting language="text">
90 * GtkButton, GtkEntry {
97 * <refsect2 id="gtkcssprovider-selectors">
98 * <title>Selectors</title>
100 * Selectors work very similar to the way they do in CSS, with widget class
101 * names taking the role of element names, and widget names taking the role
102 * of IDs. When used in a selector, widget names must be prefixed with a
103 * '#' character. The '*' character represents the so-called universal
104 * selector, which matches any widget.
107 * To express more complicated situations, selectors can be combined in
110 * <listitem><para>To require that a widget satisfies several conditions,
111 * combine several selectors into one by concatenating them. E.g.
112 * <literal>GtkButton#button1</literal> matches a GtkButton widget
113 * with the name button1.</para></listitem>
114 * <listitem><para>To only match a widget when it occurs inside some other
115 * widget, write the two selectors after each other, separated by whitespace.
116 * E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
117 * that occur inside a GtkToolBar.</para></listitem>
118 * <listitem><para>In the previous example, the GtkButton is matched even
119 * if it occurs deeply nested inside the toolbar. To restrict the match
120 * to direct children of the parent widget, insert a '>' character between
121 * the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
122 * GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
126 * <title>Widget classes and names in selectors</title>
127 * <programlisting language="text">
128 * /* Theme labels that are descendants of a window */
129 * GtkWindow GtkLabel {
130 * background-color: #898989
133 * /* Theme notebooks, and anything that's within these */
135 * background-color: #a939f0
138 * /* Theme combo boxes, and entries that
139 * are direct children of a notebook */
141 * GtkNotebook > GtkEntry {
143 * background-color: #1209a2
146 * /* Theme any widget within a GtkBin */
151 * /* Theme a label named title-label */
152 * GtkLabel#title-label {
156 * /* Theme any widget named main-entry */
158 * background-color: #f0a810
163 * Widgets may also define style classes, which can be used for matching.
164 * When used in a selector, style classes must be prefixed with a '.'
168 * Refer to the documentation of individual widgets to learn which
169 * style classes they define and see <xref linkend="gtkstylecontext-classes"/>
170 * for a list of all style classes used by GTK+ widgets.
173 * Note that there is some ambiguity in the selector syntax when it comes
174 * to differentiation widget class names from regions. GTK+ currently treats
175 * a string as a widget class name if it contains any uppercase characters
176 * (which should work for more widgets with names like GtkLabel).
179 * <title>Style classes in selectors</title>
180 * <programlisting language="text">
181 * /* Theme all widgets defining the class entry */
183 * color: #39f1f9;
186 * /* Theme spinbuttons' entry */
187 * GtkSpinButton.entry {
193 * In complicated widgets like e.g. a GtkNotebook, it may be desirable
194 * to style different parts of the widget differently. To make this
195 * possible, container widgets may define regions, whose names
196 * may be used for matching in selectors.
199 * Some containers allow to further differentiate between regions by
200 * applying so-called pseudo-classes to the region. For example, the
201 * tab region in GtkNotebook allows to single out the first or last
202 * tab by using the :first-child or :last-child pseudo-class.
203 * When used in selectors, pseudo-classes must be prefixed with a
207 * Refer to the documentation of individual widgets to learn which
208 * regions and pseudo-classes they define and see
209 * <xref linkend="gtkstylecontext-classes"/> for a list of all regions
210 * used by GTK+ widgets.
213 * <title>Regions in selectors</title>
214 * <programlisting language="text">
215 * /* Theme any label within a notebook */
216 * GtkNotebook GtkLabel {
217 * color: #f90192;
220 * /* Theme labels within notebook tabs */
221 * GtkNotebook tab GtkLabel {
222 * color: #703910;
225 * /* Theme labels in the any first notebook
226 * tab, both selectors are equivalent */
227 * GtkNotebook tab:nth-child(first) GtkLabel,
228 * GtkNotebook tab:first-child GtkLabel {
229 * color: #89d012;
234 * Another use of pseudo-classes is to match widgets depending on their
235 * state. This is conceptually similar to the :hover, :active or :focus
236 * pseudo-classes in CSS. The available pseudo-classes for widget states
237 * are :active, :prelight (or :hover), :insensitive, :selected, :focused
241 * <title>Styling specific widget states</title>
242 * <programlisting language="text">
243 * /* Theme active (pressed) buttons */
245 * background-color: #0274d9;
248 * /* Theme buttons with the mouse pointer on it,
249 * both are equivalent */
251 * GtkButton:prelight {
252 * background-color: #3085a9;
255 * /* Theme insensitive widgets, both are equivalent */
258 * background-color: #320a91;
261 * /* Theme selection colors in entries */
262 * GtkEntry:selected {
263 * background-color: #56f9a0;
266 * /* Theme focused labels */
268 * background-color: #b4940f;
271 * /* Theme inconsistent checkbuttons */
272 * GtkCheckButton:inconsistent {
273 * background-color: #20395a;
278 * Widget state pseudoclasses may only apply to the last element
282 * To determine the effective style for a widget, all the matching rule
283 * sets are merged. As in CSS, rules apply by specificity, so the rules
284 * whose selectors more closely match a widget path will take precedence
288 * <refsect2 id="gtkcssprovider-rules">
289 * <title>@ Rules</title>
291 * GTK+'s CSS supports the @import rule, in order to load another
292 * CSS style sheet in addition to the currently parsed one.
295 * <title>Using the @import rule</title>
296 * <programlisting language="text">
297 * @import url ("path/to/common.css");
300 * <para id="css-binding-set">
301 * In order to extend key bindings affecting different widgets, GTK+
302 * supports the @binding-set rule to parse a set of bind/unbind
303 * directives, see #GtkBindingSet for the supported syntax. Note that
304 * the binding sets defined in this way must be associated with rule sets
305 * by setting the gtk-key-bindings style property.
308 * Customized key bindings are typically defined in a separate
309 * <filename>gtk-keys.css</filename> CSS file and GTK+ loads this file
310 * according to the current key theme, which is defined by the
311 * #GtkSettings:gtk-key-theme-name setting.
314 * <title>Using the @binding rule</title>
315 * <programlisting language="text">
316 * @binding-set binding-set1 {
317 * bind "<alt>Left" { "move-cursor" (visual-positions, -3, 0) };
321 * @binding-set binding-set2 {
322 * bind "<alt>Right" { "move-cursor" (visual-positions, 3, 0) };
323 * bind "<alt>KP_space" { "delete-from-cursor" (whitespace, 1)
324 * "insert-at-cursor" (" ") };
328 * gtk-key-bindings: binding-set1, binding-set2;
333 * GTK+ also supports an additional @define-color rule, in order
334 * to define a color name which may be used instead of color numeric
335 * representations. Also see the #GtkSettings:gtk-color-scheme setting
336 * for a way to override the values of these named colors.
339 * <title>Defining colors</title>
340 * <programlisting language="text">
341 * @define-color bg_color #f9a039;
344 * background-color: @bg_color;
349 * <refsect2 id="gtkcssprovider-symbolic-colors">
350 * <title>Symbolic colors</title>
352 * Besides being able to define color names, the CSS parser is also able
353 * to read different color expressions, which can also be nested, providing
354 * a rich language to define colors which are derived from a set of base
358 * <title>Using symbolic colors</title>
359 * <programlisting language="text">
360 * @define-color entry-color shade (@bg_color, 0.7);
363 * background-color: @entry-color;
367 * background-color: mix (@entry-color,
368 * shade (#fff, 0.5),
374 * The various ways to express colors in GTK+ CSS are:
380 * <entry>Syntax</entry>
381 * <entry>Explanation</entry>
382 * <entry>Examples</entry>
387 * <entry>rgb(@r, @g, @b)</entry>
388 * <entry>An opaque color; @r, @g, @b can be either integers between
389 * 0 and 255 or percentages</entry>
390 * <entry><literallayout>rgb(128, 10, 54)
391 * rgb(20%, 30%, 0%)</literallayout></entry>
394 * <entry>rgba(@r, @g, @b, @a)</entry>
395 * <entry>A translucent color; @r, @g, @b are as in the previous row,
396 * @a is a floating point number between 0 and 1</entry>
397 * <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
400 * <entry>#@xxyyzz</entry>
401 * <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
402 * specifying @r, @g, @b variants with between 1 and 4
403 * hexadecimal digits per component are allowed</entry>
404 * <entry><literallayout>#ff12ab
405 * #f0c</literallayout></entry>
408 * <entry>@name</entry>
409 * <entry>Reference to a color that has been defined with
410 * @define-color
412 * <entry>@bg_color</entry>
415 * <entry>mix(@color1, @color2, @f)</entry>
416 * <entry>A linear combination of @color1 and @color2. @f is a
417 * floating point number between 0 and 1.</entry>
418 * <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
421 * <entry>shade(@color, @f)</entry>
422 * <entry>A lighter or darker variant of @color. @f is a
423 * floating point number.
425 * <entry>shade(@fg_color, 0.5)</entry>
428 * <entry>lighter(@color)</entry>
429 * <entry>A lighter variant of @color</entry>
432 * <entry>darker(@color)</entry>
433 * <entry>A darker variant of @color</entry>
436 * <entry>alpha(@color, @f)</entry>
437 * <entry>Modifies passed color's alpha by a factor @f. @f is a
438 * floating point number. @f < 1.0 results in a more transparent
439 * color while @f > 1.0 results in a more opaque color.
441 * <entry>alhpa(blue, 0.5)</entry>
447 * <refsect2 id="gtkcssprovider-gradients">
448 * <title>Gradients</title>
450 * Linear or radial Gradients can be used as background images.
453 * A linear gradient along the line from (@start_x, @start_y) to
454 * (@end_x, @end_y) is specified using the syntax
455 * <literallayout>-gtk-gradient (linear,
456 * @start_x @start_y, @end_x @end_y,
457 * color-stop (@position, @color),
458 * ...)</literallayout>
459 * where @start_x and @end_x can be either a floating point number between
460 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
461 * and @end_y can be either a floating point number between 0 and 1 or one
462 * of the special values 'top', 'bottom' or 'center', @position is a floating
463 * point number between 0 and 1 and @color is a color expression (see above).
464 * The color-stop can be repeated multiple times to add more than one color
465 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
466 * color stops with position 0 and 1, respectively.
469 * <title>A linear gradient</title>
470 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
471 * <para>This gradient was specified with
472 * <literallayout>-gtk-gradient (linear,
473 * left top, right bottom,
474 * from(@yellow), to(@blue))</literallayout></para>
477 * <title>Another linear gradient</title>
478 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
479 * <para>This gradient was specified with
480 * <literallayout>-gtk-gradient (linear,
482 * color-stop(0, @yellow),
483 * color-stop(0.2, @blue),
484 * color-stop(1, #0f0))</literallayout></para>
487 * A radial gradient along the two circles defined by (@start_x, @start_y,
488 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
490 * <literallayout>-gtk-gradient (radial,
491 * @start_x @start_y, @start_radius,
492 * @end_x @end_y, @end_radius,
493 * color-stop (@position, @color),
494 * ...)</literallayout>
495 * where @start_radius and @end_radius are floating point numbers and
496 * the other parameters are as before.
499 * <title>A radial gradient</title>
500 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
501 * <para>This gradient was specified with
502 * <literallayout>-gtk-gradient (radial,
505 * from(@yellow), to(@green))</literallayout></para>
508 * <title>Another radial gradient</title>
509 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
510 * <para>This gradient was specified with
511 * <literallayout>-gtk-gradient (radial,
514 * color-stop (0, #f00),
515 * color-stop (0.1, #a0f),
516 * color-stop (0.2, @yellow),
517 * color-stop (1, @green))</literallayout></para>
520 * <refsect2 id="gtkcssprovider-shadows">
521 * <title>Text shadow</title>
523 * A shadow list can be applied to text or symbolic icons, using the CSS3
524 * text-shadow syntax, as defined in
525 * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
528 * A text shadow is specified using the syntax
529 * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
530 * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
531 * parameters. The optional blur radius is parsed, but it is currently not rendered by
532 * the GTK+ theming engine.
535 * To set multiple shadows on an element, you can specify a comma-separated list
536 * of shadow elements in the text-shadow property. Shadows are always rendered
537 * front-back, i.e. the first shadow specified is on top of the others. Shadows
538 * can thus overlay each other, but they can never overlay the text itself,
539 * which is always rendered on top of the shadow layer.
543 * <title>Box shadow</title>
545 * Themes can apply shadows on framed elements using the CSS3 box-shadow syntax,
547 * <ulink url="http://www.w3.org/TR/css3-background/#the-box-shadow">the CSS3 specification</ulink>.
550 * A box shadow is specified using the syntax
551 * <literallayout>box-shadow: [ @inset ] @horizontal_offset @vertical_offset [ @blur_radius ] [ @spread ] @color</literallayout>
552 * A positive offset will draw a shadow that is offset to the right (down) of the box,
553 * a negative offset to the left (top). The optional spread parameter defines an additional
554 * distance to expand the shadow shape in all directions, by the specified radius.
555 * The optional blur radius parameter is parsed, but it is currently not rendered by
556 * the GTK+ theming engine.
557 * The inset parameter defines whether the drop shadow should be rendered inside or outside
558 * the box canvas. Only inset box-shadows are currently supported by the GTK+ theming engine,
559 * non-inset elements are currently ignored.
562 * To set multiple box-shadows on an element, you can specify a comma-separated list
563 * of shadow elements in the box-shadow property. Shadows are always rendered
564 * front-back, i.e. the first shadow specified is on top of the others, so they may
565 * overlap other boxes or other shadows.
568 * <refsect2 id="gtkcssprovider-border-image">
569 * <title>Border images</title>
571 * Images and gradients can also be used in slices for the purpose of creating
573 * For more information, see the CSS3 documentation for the border-image property,
574 * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
576 * <inlinegraphic fileref="slices.png" format="PNG"/>
578 * The parameters of the slicing process are controlled by
579 * four separate properties. Note that you can use the
580 * <literallayout>border-image</literallayout> shorthand property
581 * to set values for the three properties at the same time.
584 * <literallayout>border-image-source: url(@path)
585 * (or border-image-source: -gtk-gradient(...))</literallayout>:
586 * Specifies the source of the border image, and it can either
587 * be an URL or a gradient (see above).
590 * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
591 * The sizes specified by the @top, @right, @bottom and @left parameters
592 * are the offsets, in pixels, from the relevant edge where the image
593 * should be "cut off" to build the slices used for the rendering
597 * <literallayout>border-image-width: @top @right @bottom @left</literallayout>
598 * The sizes specified by the @top, @right, @bottom and @left parameters
599 * are inward distances from the border box edge, used to specify the
600 * rendered size of each slice determined by border-image-slice.
601 * If this property is not specified, the values of border-width will
602 * be used as a fallback.
605 * <literallayout>border-image-repeat: [stretch|repeat|round|space] ?
606 * [stretch|repeat|round|space]</literallayout>
607 * Specifies how the image slices should be rendered in the area
608 * outlined by border-width.
609 * The default (stretch) is to resize the slice to fill in the whole
611 * If the value of this property is 'repeat', the image slice
612 * will be tiled to fill the area.
613 * If the value of this property is 'round', the image slice will
614 * be tiled to fill the area, and scaled to fit it exactly
615 * a whole number of times.
616 * If the value of this property is 'space', the image slice will
617 * be tiled to fill the area, and if it doesn't fit it exactly a whole
618 * number of times, the extra space is distributed as padding around
620 * If two options are specified, the first one affects
621 * the horizontal behaviour and the second one the vertical behaviour.
622 * If only one option is specified, it affects both.
625 * <title>A border image</title>
626 * <inlinegraphic fileref="border1.png" format="PNG"/>
627 * <para>This border image was specified with
628 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
632 * <title>A repeating border image</title>
633 * <inlinegraphic fileref="border2.png" format="PNG"/>
634 * <para>This border image was specified with
635 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
639 * <title>A stretched border image</title>
640 * <inlinegraphic fileref="border3.png" format="PNG"/>
641 * <para>This border image was specified with
642 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
646 * <refsect2 id="gtkcssprovider-transitions">
647 * <para>Styles can specify transitions that will be used to create a gradual
648 * change in the appearance when a widget state changes. The following
649 * syntax is used to specify transitions:
650 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
651 * The @duration is the amount of time that the animation will take for
652 * a complete cycle from start to end. If the loop option is given, the
653 * animation will be repated until the state changes again.
654 * The option after the duration determines the transition function from a
655 * small set of predefined functions.
656 * <figure><title>Linear transition</title>
657 * <graphic fileref="linear.png" format="PNG"/>
659 * <figure><title>Ease transition</title>
660 * <graphic fileref="ease.png" format="PNG"/>
662 * <figure><title>Ease-in-out transition</title>
663 * <graphic fileref="ease-in-out.png" format="PNG"/>
665 * <figure><title>Ease-in transition</title>
666 * <graphic fileref="ease-in.png" format="PNG"/>
668 * <figure><title>Ease-out transition</title>
669 * <graphic fileref="ease-out.png" format="PNG"/>
673 * <refsect2 id="gtkcssprovider-properties">
674 * <title>Supported properties</title>
676 * Properties are the part that differ the most to common CSS,
677 * not all properties are supported (some are planned to be
678 * supported eventually, some others are meaningless or don't
679 * map intuitively in a widget based environment).
682 * The currently supported properties are:
688 * <entry>Property name</entry>
689 * <entry>Syntax</entry>
690 * <entry>Maps to</entry>
691 * <entry>Examples</entry>
696 * <entry>engine</entry>
697 * <entry>engine-name</entry>
698 * <entry>#GtkThemingEngine</entry>
699 * <entry>engine: clearlooks;
700 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
703 * <entry>background-color</entry>
704 * <entry morerows="2">color (see above)</entry>
705 * <entry morerows="7">#GdkRGBA</entry>
706 * <entry morerows="7"><literallayout>background-color: #fff;
707 * color: &color1;
708 * background-color: shade (&color1, 0.5);
709 * color: mix (&color1, #f0f, 0.8);</literallayout>
713 * <entry>color</entry>
716 * <entry>border-top-color</entry>
717 * <entry morerows="4">transparent|color (see above)</entry>
720 * <entry>border-right-color</entry>
723 * <entry>border-bottom-color</entry>
726 * <entry>border-left-color</entry>
729 * <entry>border-color</entry>
730 * <entry>[transparent|color]{1,4}</entry>
733 * <entry>font-family</entry>
734 * <entry>@family [, @family]*</entry>
735 * <entry>#gchararray</entry>
736 * <entry>font-family: Sans, Arial;</entry>
739 * <entry>font-style</entry>
740 * <entry>[normal|oblique|italic]</entry>
741 * <entry>#PANGO_TYPE_STYLE</entry>
742 * <entry>font-style: italic;</entry>
745 * <entry>font-variant</entry>
746 * <entry>[normal|small-caps]</entry>
747 * <entry>#PANGO_TYPE_VARIANT</entry>
748 * <entry>font-variant: normal;</entry>
751 * <entry>font-weight</entry>
752 * <entry>[normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900]</entry>
753 * <entry>#PANGO_TYPE_WEIGHT</entry>
754 * <entry>font-weight: bold;</entry>
757 * <entry>font-size</entry>
758 * <entry>Font size in point</entry>
759 * <entry>#gint</entry>
760 * <entry>font-size: 13;</entry>
763 * <entry>font</entry>
764 * <entry>@family [@style] [@size]</entry>
765 * <entry>#PangoFontDescription</entry>
766 * <entry>font: Sans 15;</entry>
769 * <entry>margin-top</entry>
770 * <entry>integer</entry>
771 * <entry>#gint</entry>
772 * <entry>margin-top: 0;</entry>
775 * <entry>margin-left</entry>
776 * <entry>integer</entry>
777 * <entry>#gint</entry>
778 * <entry>margin-left: 1;</entry>
781 * <entry>margin-bottom</entry>
782 * <entry>integer</entry>
783 * <entry>#gint</entry>
784 * <entry>margin-bottom: 2;</entry>
787 * <entry>margin-right</entry>
788 * <entry>integer</entry>
789 * <entry>#gint</entry>
790 * <entry>margin-right: 4;</entry>
793 * <entry>margin</entry>
794 * <entry morerows="1"><literallayout>@width
795 * @vertical_width @horizontal_width
796 * @top_width @horizontal_width @bottom_width
797 * @top_width @right_width @bottom_width @left_width</literallayout>
799 * <entry morerows="1">#GtkBorder</entry>
800 * <entry morerows="1"><literallayout>margin: 5;
803 * margin: 5 10 3 5;</literallayout>
807 * <entry>padding-top</entry>
808 * <entry>integer</entry>
809 * <entry>#gint</entry>
810 * <entry>padding-top: 5;</entry>
813 * <entry>padding-left</entry>
814 * <entry>integer</entry>
815 * <entry>#gint</entry>
816 * <entry>padding-left: 5;</entry>
819 * <entry>padding-bottom</entry>
820 * <entry>integer</entry>
821 * <entry>#gint</entry>
822 * <entry>padding-bottom: 5;</entry>
825 * <entry>padding-right</entry>
826 * <entry>integer</entry>
827 * <entry>#gint</entry>
828 * <entry>padding-right: 5;</entry>
831 * <entry>padding</entry>
834 * <entry>background-image</entry>
835 * <entry><literallayout>gradient (see above) or
836 * url(@path)</literallayout></entry>
837 * <entry>#cairo_pattern_t</entry>
838 * <entry><literallayout>-gtk-gradient (linear,
839 * left top, right top,
840 * from (#fff), to (#000));
841 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
843 * color-stop (0.5, #f00),
845 * -gtk-gradient (radial,
846 * center center, 0.2,
847 * center center, 0.8,
848 * color-stop (0.0, #fff),
849 * color-stop (1.0, #000));
850 * url ('background.png');</literallayout>
854 * <entry>background-repeat</entry>
855 * <entry>[repeat|no-repeat]</entry>
856 * <entry>internal</entry>
857 * <entry><literallayout>background-repeat: no-repeat;</literallayout>
858 * If not specified, the style doesn't respect the CSS3
859 * specification, since the background will be
860 * stretched to fill the area.
864 * <entry>border-top-width</entry>
865 * <entry>integer</entry>
866 * <entry>#gint</entry>
867 * <entry>border-top-width: 5;</entry>
870 * <entry>border-left-width</entry>
871 * <entry>integer</entry>
872 * <entry>#gint</entry>
873 * <entry>border-left-width: 5;</entry>
876 * <entry>border-bottom-width</entry>
877 * <entry>integer</entry>
878 * <entry>#gint</entry>
879 * <entry>border-bottom-width: 5;</entry>
882 * <entry>border-right-width</entry>
883 * <entry>integer</entry>
884 * <entry>#gint</entry>
885 * <entry>border-right-width: 5;</entry>
888 * <entry>border-width</entry>
889 * <entry morerows="1">#GtkBorder</entry>
890 * <entry morerows="1"><literallayout>border-width: 1;
892 * border-width: 1 2 3;
893 * border-width: 1 2 3 5;</literallayout>
897 * <entry>border-radius</entry>
898 * <entry>integer</entry>
899 * <entry>#gint</entry>
900 * <entry>border-radius: 5;</entry>
903 * <entry>border-style</entry>
904 * <entry>[none|solid|inset|outset]</entry>
905 * <entry>#GtkBorderStyle</entry>
906 * <entry>border-style: solid;</entry>
909 * <entry>border-image</entry>
910 * <entry><literallayout>border image (see above)</literallayout></entry>
911 * <entry>internal use only</entry>
912 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
913 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
917 * <entry>text-shadow</entry>
918 * <entry>shadow list (see above)</entry>
919 * <entry>#GtkTextShadow</entry>
920 * <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
923 * <entry>transition</entry>
924 * <entry>transition (see above)</entry>
925 * <entry>internal use only</entry>
926 * <entry><literallayout>transition: 150ms ease-in-out;
927 * transition: 1s linear loop;</literallayout>
931 * <entry>gtk-key-bindings</entry>
932 * <entry>binding set name list</entry>
933 * <entry>internal use only</entry>
934 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
941 * GtkThemingEngines can register their own, engine-specific style properties
942 * with the function gtk_theming_engine_register_property(). These properties
943 * can be set in CSS like other properties, using a name of the form
944 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
945 * the name of the theming engine, and <replaceable>name</replaceable> is the
946 * name of the property. Style properties that have been registered by widgets
947 * using gtk_widget_class_install_style_property() can also be set in this
948 * way, using the widget class name for <replaceable>namespace</replaceable>.
951 * <title>Using engine-specific style properties</title>
954 * engine: clearlooks;
956 * -GtkPaned-handle-size: 6;
957 * -clearlooks-colorize-scrollbar: false;
964 typedef struct GtkCssRuleset GtkCssRuleset;
965 typedef struct _GtkCssScanner GtkCssScanner;
966 typedef struct _PropertyValue PropertyValue;
967 typedef struct _WidgetPropertyValue WidgetPropertyValue;
968 typedef enum ParserScope ParserScope;
969 typedef enum ParserSymbol ParserSymbol;
971 struct _PropertyValue {
972 GtkCssStyleProperty *property;
974 GtkCssSection *section;
977 struct _WidgetPropertyValue {
978 WidgetPropertyValue *next;
982 GtkCssSection *section;
987 GtkCssSelector *selector;
988 WidgetPropertyValue *widget_style;
989 PropertyValue *styles;
990 GtkBitmask *set_styles;
992 guint owns_styles : 1;
993 guint owns_widget_style : 1;
996 struct _GtkCssScanner
998 GtkCssProvider *provider;
999 GtkCssParser *parser;
1000 GtkCssSection *section;
1001 GtkCssScanner *parent;
1007 struct _GtkCssProviderPrivate
1011 GHashTable *symbolic_colors;
1014 GResource *resource;
1022 static gboolean gtk_keep_css_sections = FALSE;
1024 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
1026 static void gtk_css_provider_finalize (GObject *object);
1027 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
1028 static void gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface);
1029 static void widget_property_value_list_free (WidgetPropertyValue *head);
1032 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
1033 GtkCssScanner *scanner,
1039 gtk_css_provider_error_quark (void)
1041 return g_quark_from_static_string ("gtk-css-provider-error-quark");
1044 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
1045 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
1046 gtk_css_style_provider_iface_init)
1047 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER_PRIVATE,
1048 gtk_css_style_provider_private_iface_init));
1051 gtk_css_provider_parsing_error (GtkCssProvider *provider,
1052 GtkCssSection *section,
1053 const GError *error)
1055 /* Only emit a warning when we have no error handlers. This is our
1056 * default handlers. And in this case erroneous CSS files are a bug
1057 * and should be fixed.
1058 * Note that these warnings can also be triggered by a broken theme
1059 * that people installed from some weird location on the internets.
1061 if (!g_signal_has_handler_pending (provider,
1062 css_provider_signals[PARSING_ERROR],
1070 file = gtk_css_section_get_file (section);
1073 info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1076 path = g_file_info_get_display_name (info);
1078 path = "<broken file>";
1086 g_warning ("Theme parsing error: %s:%u:%u: %s",
1088 gtk_css_section_get_end_line (section) + 1,
1089 gtk_css_section_get_end_position (section),
1093 g_object_unref (info);
1098 gtk_css_provider_class_init (GtkCssProviderClass *klass)
1100 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1102 if (g_getenv ("GTK_CSS_DEBUG"))
1103 gtk_keep_css_sections = TRUE;
1106 * GtkCssProvider::parsing-error:
1107 * @provider: the provider that had a parsing error
1108 * @section: section the error happened in
1109 * @error: The parsing error
1111 * Signals that a parsing error occured. the @path, @line and @position
1112 * describe the actual location of the error as accurately as possible.
1114 * Parsing errors are never fatal, so the parsing will resume after
1115 * the error. Errors may however cause parts of the given
1116 * data or even all of it to not be parsed at all. So it is a useful idea
1117 * to check that the parsing succeeds by connecting to this signal.
1119 * Note that this signal may be emitted at any time as the css provider
1120 * may opt to defer parsing parts or all of the input to a later time
1121 * than when a loading function was called.
1123 css_provider_signals[PARSING_ERROR] =
1124 g_signal_new (I_("parsing-error"),
1125 G_TYPE_FROM_CLASS (object_class),
1127 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
1129 _gtk_marshal_VOID__BOXED_BOXED,
1130 G_TYPE_NONE, 2, GTK_TYPE_CSS_SECTION, G_TYPE_ERROR);
1132 object_class->finalize = gtk_css_provider_finalize;
1134 klass->parsing_error = gtk_css_provider_parsing_error;
1136 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
1140 gtk_css_ruleset_init_copy (GtkCssRuleset *new,
1141 GtkCssRuleset *ruleset,
1142 GtkCssSelector *selector)
1144 memcpy (new, ruleset, sizeof (GtkCssRuleset));
1146 new->selector = selector;
1147 /* First copy takes over ownership */
1148 if (ruleset->owns_styles)
1149 ruleset->owns_styles = FALSE;
1150 if (ruleset->owns_widget_style)
1151 ruleset->owns_widget_style = FALSE;
1152 if (new->set_styles)
1153 new->set_styles = _gtk_bitmask_copy (new->set_styles);
1157 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
1159 if (ruleset->owns_styles)
1163 for (i = 0; i < ruleset->n_styles; i++)
1165 _gtk_css_value_unref (ruleset->styles[i].value);
1166 ruleset->styles[i].value = NULL;
1167 if (ruleset->styles[i].section)
1168 gtk_css_section_unref (ruleset->styles[i].section);
1170 g_free (ruleset->styles);
1172 if (ruleset->set_styles)
1173 _gtk_bitmask_free (ruleset->set_styles);
1174 if (ruleset->owns_widget_style)
1175 widget_property_value_list_free (ruleset->widget_style);
1176 if (ruleset->selector)
1177 _gtk_css_selector_free (ruleset->selector);
1179 memset (ruleset, 0, sizeof (GtkCssRuleset));
1182 static WidgetPropertyValue *
1183 widget_property_value_new (char *name, GtkCssSection *section)
1185 WidgetPropertyValue *value;
1187 value = g_slice_new0 (WidgetPropertyValue);
1190 if (gtk_keep_css_sections)
1191 value->section = gtk_css_section_ref (section);
1197 widget_property_value_free (WidgetPropertyValue *value)
1199 g_free (value->value);
1200 g_free (value->name);
1202 gtk_css_section_unref (value->section);
1204 g_slice_free (WidgetPropertyValue, value);
1208 widget_property_value_list_free (WidgetPropertyValue *head)
1210 WidgetPropertyValue *l, *next;
1211 for (l = head; l != NULL; l = next)
1214 widget_property_value_free (l);
1218 static WidgetPropertyValue *
1219 widget_property_value_list_remove_name (WidgetPropertyValue *head, const char *name)
1221 WidgetPropertyValue *l, **last;
1225 for (l = head; l != NULL; l = l->next)
1227 if (strcmp (l->name, name) == 0)
1230 widget_property_value_free (l);
1241 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
1243 WidgetPropertyValue *value)
1245 value->next = widget_property_value_list_remove_name (ruleset->widget_style, name);
1246 ruleset->widget_style = value;
1247 ruleset->owns_widget_style = TRUE;
1251 gtk_css_ruleset_add (GtkCssRuleset *ruleset,
1252 GtkCssStyleProperty *property,
1254 GtkCssSection *section)
1258 g_return_if_fail (ruleset->owns_styles || ruleset->n_styles == 0);
1260 if (ruleset->set_styles == NULL)
1261 ruleset->set_styles = _gtk_bitmask_new ();
1263 ruleset->set_styles = _gtk_bitmask_set (ruleset->set_styles,
1264 _gtk_css_style_property_get_id (property),
1267 ruleset->owns_styles = TRUE;
1269 for (i = 0; i < ruleset->n_styles; i++)
1271 if (ruleset->styles[i].property == property)
1273 _gtk_css_value_unref (ruleset->styles[i].value);
1274 ruleset->styles[i].value = NULL;
1275 if (ruleset->styles[i].section)
1276 gtk_css_section_unref (ruleset->styles[i].section);
1280 if (i == ruleset->n_styles)
1282 ruleset->n_styles++;
1283 ruleset->styles = g_realloc (ruleset->styles, ruleset->n_styles * sizeof (PropertyValue));
1284 ruleset->styles[i].value = NULL;
1285 ruleset->styles[i].property = property;
1288 ruleset->styles[i].value = value;
1289 if (gtk_keep_css_sections)
1290 ruleset->styles[i].section = gtk_css_section_ref (section);
1292 ruleset->styles[i].section = NULL;
1296 gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
1297 const GtkCssMatcher *matcher)
1299 return _gtk_css_selector_matches (ruleset->selector, matcher);
1303 gtk_css_ruleset_get_change (GtkCssRuleset *ruleset)
1305 return _gtk_css_selector_get_change (ruleset->selector);
1309 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1311 if (scanner->section)
1312 gtk_css_section_unref (scanner->section);
1313 g_object_unref (scanner->provider);
1315 g_object_unref (scanner->file);
1316 g_object_unref (scanner->base);
1317 _gtk_css_parser_free (scanner->parser);
1319 g_slice_free (GtkCssScanner, scanner);
1323 gtk_css_provider_emit_error (GtkCssProvider *provider,
1324 GtkCssScanner *scanner,
1325 const GError *error)
1327 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1328 scanner != NULL ? scanner->section : NULL, error);
1332 gtk_css_scanner_parser_error (GtkCssParser *parser,
1333 const GError *error,
1336 GtkCssScanner *scanner = user_data;
1338 gtk_css_provider_emit_error (scanner->provider,
1343 static GtkCssScanner *
1344 gtk_css_scanner_new (GtkCssProvider *provider,
1345 GtkCssScanner *parent,
1346 GtkCssSection *section,
1350 GtkCssScanner *scanner;
1352 scanner = g_slice_new0 (GtkCssScanner);
1354 g_object_ref (provider);
1355 scanner->provider = provider;
1356 scanner->parent = parent;
1358 scanner->section = gtk_css_section_ref (section);
1362 scanner->file = g_object_ref (file);
1363 scanner->base = g_file_get_parent (file);
1367 char *dir = g_get_current_dir ();
1368 scanner->base = g_file_new_for_path (dir);
1372 scanner->parser = _gtk_css_parser_new (text,
1374 gtk_css_scanner_parser_error,
1381 gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
1383 return scanner->base;
1387 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1392 if (scanner->file && g_file_equal (scanner->file, file))
1395 scanner = scanner->parent;
1402 gtk_css_scanner_push_section (GtkCssScanner *scanner,
1403 GtkCssSectionType section_type)
1405 GtkCssSection *section;
1407 section = _gtk_css_section_new (scanner->section,
1412 if (scanner->section)
1413 gtk_css_section_unref (scanner->section);
1414 scanner->section = section;
1418 gtk_css_scanner_pop_section (GtkCssScanner *scanner,
1419 GtkCssSectionType check_type)
1421 GtkCssSection *parent;
1423 g_assert (gtk_css_section_get_section_type (scanner->section) == check_type);
1425 parent = gtk_css_section_get_parent (scanner->section);
1427 gtk_css_section_ref (parent);
1429 _gtk_css_section_end (scanner->section);
1430 gtk_css_section_unref (scanner->section);
1432 scanner->section = parent;
1436 gtk_css_provider_init (GtkCssProvider *css_provider)
1438 GtkCssProviderPrivate *priv;
1440 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1441 GTK_TYPE_CSS_PROVIDER,
1442 GtkCssProviderPrivate);
1444 priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1446 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1447 (GDestroyNotify) g_free,
1448 (GDestroyNotify) gtk_symbolic_color_unref);
1452 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1453 GtkStyleProperties *props)
1455 GtkCssProviderPrivate *priv;
1456 GHashTableIter iter;
1457 gpointer key, value;
1459 priv = css_provider->priv;
1460 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1462 while (g_hash_table_iter_next (&iter, &key, &value))
1465 GtkSymbolicColor *color;
1470 gtk_style_properties_map_color (props, name, color);
1474 static GtkStyleProperties *
1475 gtk_css_provider_get_style (GtkStyleProvider *provider,
1476 GtkWidgetPath *path)
1478 GtkCssMatcher matcher;
1479 GtkCssProvider *css_provider;
1480 GtkCssProviderPrivate *priv;
1481 GtkStyleProperties *props;
1484 css_provider = GTK_CSS_PROVIDER (provider);
1485 priv = css_provider->priv;
1486 props = gtk_style_properties_new ();
1488 css_provider_dump_symbolic_colors (css_provider, props);
1489 if (_gtk_css_matcher_init (&matcher, path, 0))
1491 for (i = 0; i < priv->rulesets->len; i++)
1493 GtkCssRuleset *ruleset;
1495 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1497 if (ruleset->styles == NULL)
1500 if (!gtk_css_ruleset_matches (ruleset, &matcher))
1503 for (j = 0; j < ruleset->n_styles; j++)
1504 _gtk_style_properties_set_property_by_property (props,
1505 GTK_CSS_STYLE_PROPERTY (ruleset->styles[i].property),
1506 _gtk_css_selector_get_state_flags (ruleset->selector),
1507 ruleset->styles[i].value);
1515 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1516 GtkWidgetPath *path,
1517 GtkStateFlags state,
1521 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1522 GtkCssProviderPrivate *priv = css_provider->priv;
1523 WidgetPropertyValue *val;
1524 GtkCssMatcher matcher;
1525 gboolean found = FALSE;
1529 if (!_gtk_css_matcher_init (&matcher, path, state))
1532 prop_name = g_strdup_printf ("-%s-%s",
1533 g_type_name (pspec->owner_type),
1536 for (i = priv->rulesets->len - 1; i >= 0; i--)
1538 GtkCssRuleset *ruleset;
1540 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1542 if (ruleset->widget_style == NULL)
1545 if (!gtk_css_ruleset_matches (ruleset, &matcher))
1548 for (val = ruleset->widget_style; val != NULL; val = val->next)
1550 if (strcmp (val->name, prop_name) == 0)
1552 GtkCssScanner *scanner;
1554 scanner = gtk_css_scanner_new (css_provider,
1557 val->section != NULL ? gtk_css_section_get_file (val->section) : NULL,
1560 found = _gtk_css_style_parse_value (value,
1563 gtk_css_scanner_destroy (scanner);
1579 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1581 iface->get_style = gtk_css_provider_get_style;
1582 iface->get_style_property = gtk_css_provider_get_style_property;
1585 static GtkSymbolicColor *
1586 gtk_css_style_provider_get_color (GtkStyleProviderPrivate *provider,
1589 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1591 return g_hash_table_lookup (css_provider->priv->symbolic_colors, name);
1595 gtk_css_style_provider_lookup (GtkStyleProviderPrivate *provider,
1596 const GtkCssMatcher *matcher,
1597 GtkCssLookup *lookup)
1599 GtkCssProvider *css_provider;
1600 GtkCssProviderPrivate *priv;
1604 css_provider = GTK_CSS_PROVIDER (provider);
1605 priv = css_provider->priv;
1607 for (i = priv->rulesets->len - 1; i >= 0; i--)
1609 GtkCssRuleset *ruleset;
1611 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1613 if (ruleset->styles == NULL)
1616 if (!_gtk_bitmask_intersects (_gtk_css_lookup_get_missing (lookup),
1617 ruleset->set_styles))
1620 if (!gtk_css_ruleset_matches (ruleset, matcher))
1623 for (j = 0; j < ruleset->n_styles; j++)
1625 GtkCssStyleProperty *prop = ruleset->styles[j].property;
1626 guint id = _gtk_css_style_property_get_id (prop);
1628 if (!_gtk_css_lookup_is_missing (lookup, id))
1631 _gtk_css_lookup_set (lookup,
1633 ruleset->styles[j].section,
1634 ruleset->styles[j].value);
1640 gtk_css_style_provider_get_change (GtkStyleProviderPrivate *provider,
1641 const GtkCssMatcher *matcher)
1643 GtkCssProvider *css_provider;
1644 GtkCssProviderPrivate *priv;
1645 GtkCssChange change = 0;
1648 css_provider = GTK_CSS_PROVIDER (provider);
1649 priv = css_provider->priv;
1651 for (i = priv->rulesets->len - 1; i >= 0; i--)
1653 GtkCssRuleset *ruleset;
1655 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1657 if (ruleset->styles == NULL)
1660 if (!gtk_css_ruleset_matches (ruleset, matcher))
1663 change |= gtk_css_ruleset_get_change (ruleset);
1670 gtk_css_style_provider_private_iface_init (GtkStyleProviderPrivateInterface *iface)
1672 iface->get_color = gtk_css_style_provider_get_color;
1673 iface->lookup = gtk_css_style_provider_lookup;
1674 iface->get_change = gtk_css_style_provider_get_change;
1678 gtk_css_provider_finalize (GObject *object)
1680 GtkCssProvider *css_provider;
1681 GtkCssProviderPrivate *priv;
1684 css_provider = GTK_CSS_PROVIDER (object);
1685 priv = css_provider->priv;
1687 for (i = 0; i < priv->rulesets->len; i++)
1688 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1690 g_array_free (priv->rulesets, TRUE);
1692 if (priv->symbolic_colors)
1693 g_hash_table_destroy (priv->symbolic_colors);
1697 g_resources_unregister (priv->resource);
1698 g_resource_unref (priv->resource);
1699 priv->resource = NULL;
1702 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1706 * gtk_css_provider_new:
1708 * Returns a newly created #GtkCssProvider.
1710 * Returns: A new #GtkCssProvider
1713 gtk_css_provider_new (void)
1715 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1719 gtk_css_provider_take_error (GtkCssProvider *provider,
1720 GtkCssScanner *scanner,
1723 gtk_css_provider_emit_error (provider,
1727 g_error_free (error);
1731 gtk_css_provider_error_literal (GtkCssProvider *provider,
1732 GtkCssScanner *scanner,
1735 const char *message)
1737 gtk_css_provider_take_error (provider,
1739 g_error_new_literal (domain, code, message));
1743 gtk_css_provider_error (GtkCssProvider *provider,
1744 GtkCssScanner *scanner,
1748 ...) G_GNUC_PRINTF (5, 6);
1750 gtk_css_provider_error (GtkCssProvider *provider,
1751 GtkCssScanner *scanner,
1760 va_start (args, format);
1761 error = g_error_new_valist (domain, code, format, args);
1764 gtk_css_provider_take_error (provider, scanner, error);
1768 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1769 GtkCssScanner *scanner,
1770 const char *expected)
1772 gtk_css_provider_error (provider,
1774 GTK_CSS_PROVIDER_ERROR,
1775 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1776 "expected a valid %s", expected);
1780 css_provider_commit (GtkCssProvider *css_provider,
1782 GtkCssRuleset *ruleset)
1784 GtkCssProviderPrivate *priv;
1787 priv = css_provider->priv;
1789 if (ruleset->styles == NULL && ruleset->widget_style == NULL)
1791 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1795 for (l = selectors; l; l = l->next)
1799 gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1801 g_array_append_val (priv->rulesets, new);
1804 g_slist_free (selectors);
1808 gtk_css_provider_reset (GtkCssProvider *css_provider)
1810 GtkCssProviderPrivate *priv;
1813 priv = css_provider->priv;
1817 g_resources_unregister (priv->resource);
1818 g_resource_unref (priv->resource);
1819 priv->resource = NULL;
1822 g_hash_table_remove_all (priv->symbolic_colors);
1824 for (i = 0; i < priv->rulesets->len; i++)
1825 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1826 g_array_set_size (priv->rulesets, 0);
1830 gtk_css_provider_propagate_error (GtkCssProvider *provider,
1831 GtkCssSection *section,
1832 const GError *error,
1833 GError **propagate_to)
1840 file = gtk_css_section_get_file (section);
1843 info = g_file_query_info (file,G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
1846 path = g_file_info_get_display_name (info);
1848 path = "<broken file>";
1856 /* don't fail for deprecations */
1857 if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
1859 g_warning ("Theme parsing error: %s:%u:%u: %s", path,
1860 gtk_css_section_get_end_line (section) + 1,
1861 gtk_css_section_get_end_position (section), error->message);
1865 /* we already set an error. And we'd like to keep the first one */
1869 *propagate_to = g_error_copy (error);
1870 g_prefix_error (propagate_to, "%s:%u:%u: ", path,
1871 gtk_css_section_get_end_line (section) + 1,
1872 gtk_css_section_get_end_position (section));
1875 g_object_unref (info);
1879 parse_import (GtkCssScanner *scanner)
1883 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_IMPORT);
1885 if (!_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1887 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1891 if (_gtk_css_parser_is_string (scanner->parser))
1895 uri = _gtk_css_parser_read_string (scanner->parser);
1896 file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
1901 file = _gtk_css_parser_read_url (scanner->parser);
1906 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1907 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1911 if (!_gtk_css_parser_try (scanner->parser, ";", FALSE))
1913 gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1914 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1916 else if (gtk_css_scanner_would_recurse (scanner, file))
1918 char *path = g_file_get_path (file);
1919 gtk_css_provider_error (scanner->provider,
1921 GTK_CSS_PROVIDER_ERROR,
1922 GTK_CSS_PROVIDER_ERROR_IMPORT,
1923 "Loading '%s' would recurse",
1929 gtk_css_provider_load_internal (scanner->provider,
1936 g_object_unref (file);
1938 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
1939 _gtk_css_parser_skip_whitespace (scanner->parser);
1945 parse_color_definition (GtkCssScanner *scanner)
1947 GtkCssValue *symbolic;
1950 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1952 if (!_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1954 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1958 name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1961 gtk_css_provider_error_literal (scanner->provider,
1963 GTK_CSS_PROVIDER_ERROR,
1964 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1965 "Not a valid color name");
1966 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1967 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1971 symbolic = _gtk_css_symbolic_value_new (scanner->parser);
1972 if (symbolic == NULL)
1975 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1976 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1980 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1983 _gtk_css_value_unref (symbolic);
1984 gtk_css_provider_error_literal (scanner->provider,
1986 GTK_CSS_PROVIDER_ERROR,
1987 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1988 "Missing semicolon at end of color definition");
1989 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1991 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
1995 g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
1997 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
2002 parse_binding_set (GtkCssScanner *scanner)
2004 GtkBindingSet *binding_set;
2007 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_BINDING_SET);
2009 if (!_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
2011 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
2015 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2018 gtk_css_provider_error_literal (scanner->provider,
2020 GTK_CSS_PROVIDER_ERROR,
2021 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2022 "Expected name for binding set");
2023 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2024 goto skip_semicolon;
2027 binding_set = gtk_binding_set_find (name);
2030 binding_set = gtk_binding_set_new (name);
2031 binding_set->parsed = TRUE;
2035 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2037 gtk_css_provider_error_literal (scanner->provider,
2039 GTK_CSS_PROVIDER_ERROR,
2040 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2041 "Expected '{' for binding set");
2042 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2043 goto skip_semicolon;
2046 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2047 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2049 name = _gtk_css_parser_read_value (scanner->parser);
2052 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2056 if (gtk_binding_entry_add_signal_from_string (binding_set, name) != G_TOKEN_NONE)
2058 gtk_css_provider_error_literal (scanner->provider,
2060 GTK_CSS_PROVIDER_ERROR,
2061 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2062 "Failed to parse binding set.");
2067 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2069 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2070 !_gtk_css_parser_is_eof (scanner->parser))
2072 gtk_css_provider_error_literal (scanner->provider,
2074 GTK_CSS_PROVIDER_ERROR,
2075 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2076 "Expected semicolon");
2077 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2082 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2084 gtk_css_provider_error_literal (scanner->provider,
2086 GTK_CSS_PROVIDER_ERROR,
2087 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2088 "expected '}' after declarations");
2089 if (!_gtk_css_parser_is_eof (scanner->parser))
2090 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2094 if (_gtk_css_parser_begins_with (scanner->parser, ';'))
2096 gtk_css_provider_error_literal (scanner->provider,
2098 GTK_CSS_PROVIDER_ERROR,
2099 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
2100 "Nonstandard semicolon at end of binding set");
2101 _gtk_css_parser_try (scanner->parser, ";", TRUE);
2104 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
2110 parse_at_keyword (GtkCssScanner *scanner)
2112 if (parse_import (scanner))
2114 if (parse_color_definition (scanner))
2116 if (parse_binding_set (scanner))
2121 gtk_css_provider_error_literal (scanner->provider,
2123 GTK_CSS_PROVIDER_ERROR,
2124 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2126 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
2131 parse_selector_list (GtkCssScanner *scanner)
2133 GSList *selectors = NULL;
2135 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_SELECTOR);
2138 GtkCssSelector *select = _gtk_css_selector_parse (scanner->parser);
2142 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2143 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2144 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2148 selectors = g_slist_prepend (selectors, select);
2150 while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
2152 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
2158 parse_declaration (GtkCssScanner *scanner,
2159 GtkCssRuleset *ruleset)
2161 GtkStyleProperty *property;
2164 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DECLARATION);
2166 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2168 goto check_for_semicolon;
2170 property = _gtk_style_property_lookup (name);
2171 if (property == NULL && name[0] != '-')
2173 gtk_css_provider_error (scanner->provider,
2175 GTK_CSS_PROVIDER_ERROR,
2176 GTK_CSS_PROVIDER_ERROR_NAME,
2177 "'%s' is not a valid property name",
2179 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2181 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2185 if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
2187 gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
2188 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2190 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2200 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2202 value = _gtk_style_property_parse_value (property,
2204 gtk_css_scanner_get_base_url (scanner));
2208 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2209 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2210 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2214 if (!_gtk_css_parser_begins_with (scanner->parser, ';') &&
2215 !_gtk_css_parser_begins_with (scanner->parser, '}') &&
2216 !_gtk_css_parser_is_eof (scanner->parser))
2218 gtk_css_provider_error_literal (scanner->provider,
2220 GTK_CSS_PROVIDER_ERROR,
2221 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2222 "Junk at end of value");
2223 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2224 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2225 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2229 if (GTK_IS_CSS_SHORTHAND_PROPERTY (property))
2231 GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
2234 for (i = 0; i < _gtk_css_shorthand_property_get_n_subproperties (shorthand); i++)
2236 GtkCssStyleProperty *child = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
2237 GtkCssValue *sub = _gtk_css_array_value_get_nth (value, i);
2239 gtk_css_ruleset_add (ruleset, child, _gtk_css_value_ref (sub), scanner->section);
2242 _gtk_css_value_unref (value);
2244 else if (GTK_IS_CSS_STYLE_PROPERTY (property))
2246 gtk_css_ruleset_add (ruleset, GTK_CSS_STYLE_PROPERTY (property), value, scanner->section);
2250 g_assert_not_reached ();
2251 _gtk_css_value_unref (value);
2255 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2257 else if (name[0] == '-')
2261 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
2263 value_str = _gtk_css_parser_read_value (scanner->parser);
2266 WidgetPropertyValue *val;
2268 val = widget_property_value_new (name, scanner->section);
2269 val->value = value_str;
2271 gtk_css_ruleset_add_style (ruleset, name, val);
2275 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2276 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2277 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2281 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
2286 check_for_semicolon:
2287 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
2289 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2291 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2292 !_gtk_css_parser_is_eof (scanner->parser))
2294 gtk_css_provider_error_literal (scanner->provider,
2296 GTK_CSS_PROVIDER_ERROR,
2297 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2298 "Expected semicolon");
2299 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2305 parse_declarations (GtkCssScanner *scanner,
2306 GtkCssRuleset *ruleset)
2308 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2309 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2311 parse_declaration (scanner, ruleset);
2316 parse_ruleset (GtkCssScanner *scanner)
2319 GtkCssRuleset ruleset = { 0, };
2321 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_RULESET);
2323 selectors = parse_selector_list (scanner);
2324 if (selectors == NULL)
2326 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2330 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2332 gtk_css_provider_error_literal (scanner->provider,
2334 GTK_CSS_PROVIDER_ERROR,
2335 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2336 "expected '{' after selectors");
2337 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2338 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2339 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2343 parse_declarations (scanner, &ruleset);
2345 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2347 gtk_css_provider_error_literal (scanner->provider,
2349 GTK_CSS_PROVIDER_ERROR,
2350 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2351 "expected '}' after declarations");
2352 if (!_gtk_css_parser_is_eof (scanner->parser))
2354 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2355 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2356 gtk_css_ruleset_clear (&ruleset);
2357 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2361 css_provider_commit (scanner->provider, selectors, &ruleset);
2362 gtk_css_ruleset_clear (&ruleset);
2363 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
2367 parse_statement (GtkCssScanner *scanner)
2369 if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2370 parse_at_keyword (scanner);
2372 parse_ruleset (scanner);
2376 parse_stylesheet (GtkCssScanner *scanner)
2378 gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2380 _gtk_css_parser_skip_whitespace (scanner->parser);
2382 while (!_gtk_css_parser_is_eof (scanner->parser))
2384 if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2385 _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2388 parse_statement (scanner);
2391 gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DOCUMENT);
2395 gtk_css_provider_compare_rule (gconstpointer a_,
2398 const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2399 const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2402 compare = _gtk_css_selector_compare (a->selector, b->selector);
2410 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2412 GtkCssProviderPrivate *priv = css_provider->priv;
2414 g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2418 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2419 GtkCssScanner *parent,
2424 GtkCssScanner *scanner;
2425 gulong error_handler;
2426 char *free_data = NULL;
2429 error_handler = g_signal_connect (css_provider,
2431 G_CALLBACK (gtk_css_provider_propagate_error),
2434 error_handler = 0; /* silence gcc */
2438 GError *load_error = NULL;
2440 if (g_file_load_contents (file, NULL,
2448 GtkCssSection *section;
2451 section = gtk_css_section_ref (parent->section);
2453 section = _gtk_css_section_new_for_file (GTK_CSS_SECTION_DOCUMENT, file);
2455 gtk_css_provider_error (css_provider,
2457 GTK_CSS_PROVIDER_ERROR,
2458 GTK_CSS_PROVIDER_ERROR_IMPORT,
2459 "Failed to import: %s",
2460 load_error->message);
2462 gtk_css_section_unref (section);
2468 scanner = gtk_css_scanner_new (css_provider,
2470 parent ? parent->section : NULL,
2474 parse_stylesheet (scanner);
2476 gtk_css_scanner_destroy (scanner);
2479 gtk_css_provider_postprocess (css_provider);
2486 g_signal_handler_disconnect (css_provider, error_handler);
2490 /* We clear all contents from the provider for backwards compat reasons */
2491 gtk_css_provider_reset (css_provider);
2500 * gtk_css_provider_load_from_data:
2501 * @css_provider: a #GtkCssProvider
2502 * @data: (array length=length) (element-type guint8): CSS data loaded in memory
2503 * @length: the length of @data in bytes, or -1 for NUL terminated strings. If
2504 * @length is not -1, the code will assume it is not NUL terminated and will
2505 * potentially do a copy.
2506 * @error: (out) (allow-none): return location for a #GError, or %NULL
2508 * Loads @data into @css_provider, making it clear any previously loaded
2511 * Returns: %TRUE if the data could be loaded.
2514 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2522 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2523 g_return_val_if_fail (data != NULL, FALSE);
2527 length = strlen (data);
2532 free_data = g_strndup (data, length);
2536 gtk_css_provider_reset (css_provider);
2538 ret = gtk_css_provider_load_internal (css_provider, NULL, NULL, data, error);
2542 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (css_provider));
2548 * gtk_css_provider_load_from_file:
2549 * @css_provider: a #GtkCssProvider
2550 * @file: #GFile pointing to a file to load
2551 * @error: (out) (allow-none): return location for a #GError, or %NULL
2553 * Loads the data contained in @file into @css_provider, making it
2554 * clear any previously loaded information.
2556 * Returns: %TRUE if the data could be loaded.
2559 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2565 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2566 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2568 gtk_css_provider_reset (css_provider);
2570 success = gtk_css_provider_load_internal (css_provider, NULL, file, NULL, error);
2572 _gtk_style_provider_private_changed (GTK_STYLE_PROVIDER_PRIVATE (css_provider));
2578 * gtk_css_provider_load_from_path:
2579 * @css_provider: a #GtkCssProvider
2580 * @path: the path of a filename to load, in the GLib filename encoding
2581 * @error: (out) (allow-none): return location for a #GError, or %NULL
2583 * Loads the data contained in @path into @css_provider, making it clear
2584 * any previously loaded information.
2586 * Returns: %TRUE if the data could be loaded.
2589 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2596 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2597 g_return_val_if_fail (path != NULL, FALSE);
2599 file = g_file_new_for_path (path);
2601 result = gtk_css_provider_load_from_file (css_provider, file, error);
2603 g_object_unref (file);
2609 _gtk_css_provider_load_from_resource (GtkCssProvider *css_provider,
2610 const gchar *resource_path)
2613 char *uri, *escaped;
2616 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2617 g_return_val_if_fail (resource_path != NULL, FALSE);
2619 escaped = g_uri_escape_string (resource_path,
2620 G_URI_RESERVED_CHARS_ALLOWED_IN_PATH, FALSE);
2621 uri = g_strconcat ("resource://", escaped, NULL);
2624 file = g_file_new_for_uri (uri);
2627 result = gtk_css_provider_load_from_file (css_provider, file, NULL);
2629 g_object_unref (file);
2635 * gtk_css_provider_get_default:
2637 * Returns the provider containing the style settings used as a
2638 * fallback for all widgets.
2640 * Returns: (transfer none): The provider used for fallback styling.
2641 * This memory is owned by GTK+, and you must not free it.
2644 gtk_css_provider_get_default (void)
2646 static GtkCssProvider *provider;
2648 if (G_UNLIKELY (!provider))
2650 provider = gtk_css_provider_new ();
2657 _gtk_css_provider_get_theme_dir (void)
2662 var = g_getenv ("GTK_DATA_PREFIX");
2665 path = g_build_filename (var, "share", "themes", NULL);
2667 path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
2673 * gtk_css_provider_get_named:
2674 * @name: A theme name
2675 * @variant: (allow-none): variant to load, for example, "dark", or
2676 * %NULL for the default
2678 * Loads a theme from the usual theme paths
2680 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
2681 * This memory is owned by GTK+, and you must not free it.
2684 gtk_css_provider_get_named (const gchar *name,
2685 const gchar *variant)
2687 static GHashTable *themes = NULL;
2688 GtkCssProvider *provider;
2691 if (variant == NULL)
2692 key = (gchar *)name;
2694 key = g_strconcat (name, "-", variant, NULL);
2696 if (G_UNLIKELY (!themes))
2697 themes = g_hash_table_new (g_str_hash, g_str_equal);
2699 provider = g_hash_table_lookup (themes, key);
2703 gchar *resource_path = NULL;
2706 resource_path = g_strdup_printf ("/org/gtk/libgtk/%s-%s.css", name, variant);
2708 resource_path = g_strdup_printf ("/org/gtk/libgtk/%s.css", name);
2710 if (g_resources_get_info (resource_path, 0, NULL, NULL, NULL))
2712 provider = gtk_css_provider_new ();
2713 if (!_gtk_css_provider_load_from_resource (provider, resource_path))
2715 g_object_unref (provider);
2719 g_free (resource_path);
2724 gchar *subpath, *path = NULL;
2727 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
2729 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
2731 /* First look in the user's config directory
2733 path = g_build_filename (g_get_user_data_dir (), "themes", name, subpath, NULL);
2734 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2740 /* Next look in the user's home directory
2744 const gchar *home_dir;
2746 home_dir = g_get_home_dir ();
2749 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
2751 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2763 theme_dir = _gtk_css_provider_get_theme_dir ();
2764 path = g_build_filename (theme_dir, name, subpath, NULL);
2767 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2778 char *dir, *resource_file;
2779 GResource *resource;
2781 provider = gtk_css_provider_new ();
2783 dir = g_path_get_dirname (path);
2784 resource_file = g_build_filename (dir, "gtk.gresource", NULL);
2785 resource = g_resource_load (resource_file, NULL);
2786 g_free (resource_file);
2788 if (resource != NULL)
2789 g_resources_register (resource);
2791 if (!gtk_css_provider_load_from_path (provider, path, NULL))
2793 if (resource != NULL)
2795 g_resources_unregister (resource);
2796 g_resource_unref (resource);
2798 g_object_unref (provider);
2803 /* Only set this after load success, as load_from_path will clear it */
2804 provider->priv->resource = resource;
2805 g_hash_table_insert (themes, g_strdup (key), provider);
2820 compare_properties (gconstpointer a, gconstpointer b, gpointer style)
2822 const guint *ua = a;
2823 const guint *ub = b;
2824 PropertyValue *styles = style;
2826 return strcmp (_gtk_style_property_get_name (GTK_STYLE_PROPERTY (styles[*ua].property)),
2827 _gtk_style_property_get_name (GTK_STYLE_PROPERTY (styles[*ub].property)));
2831 compare_names (gconstpointer a, gconstpointer b)
2833 const WidgetPropertyValue *aa = a;
2834 const WidgetPropertyValue *bb = b;
2835 return strcmp (aa->name, bb->name);
2839 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
2842 GList *values, *walk;
2843 WidgetPropertyValue *widget_value;
2846 _gtk_css_selector_print (ruleset->selector, str);
2848 g_string_append (str, " {\n");
2850 if (ruleset->styles)
2852 guint *sorted = g_new (guint, ruleset->n_styles);
2854 for (i = 0; i < ruleset->n_styles; i++)
2857 /* so the output is identical for identical selector styles */
2858 g_qsort_with_data (sorted, ruleset->n_styles, sizeof (guint), compare_properties, ruleset->styles);
2860 for (i = 0; i < ruleset->n_styles; i++)
2862 PropertyValue *prop = &ruleset->styles[sorted[i]];
2863 g_string_append (str, " ");
2864 g_string_append (str, _gtk_style_property_get_name (GTK_STYLE_PROPERTY (prop->property)));
2865 g_string_append (str, ": ");
2866 _gtk_css_style_property_print_value (prop->property, prop->value, str);
2867 g_string_append (str, ";\n");
2873 if (ruleset->widget_style)
2876 for (widget_value = ruleset->widget_style; widget_value != NULL; widget_value = widget_value->next)
2877 values = g_list_prepend (values, widget_value);
2879 /* so the output is identical for identical selector styles */
2880 values = g_list_sort (values, compare_names);
2882 for (walk = values; walk; walk = walk->next)
2884 widget_value = walk->data;
2886 g_string_append (str, " ");
2887 g_string_append (str, widget_value->name);
2888 g_string_append (str, ": ");
2889 g_string_append (str, widget_value->value);
2890 g_string_append (str, ";\n");
2893 g_list_free (values);
2896 g_string_append (str, "}\n");
2900 gtk_css_provider_print_colors (GHashTable *colors,
2906 keys = g_hash_table_get_keys (colors);
2907 /* so the output is identical for identical styles */
2908 keys = g_list_sort (keys, (GCompareFunc) strcmp);
2910 for (walk = keys; walk; walk = walk->next)
2912 const char *name = walk->data;
2913 GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
2915 g_string_append (str, "@define-color ");
2916 g_string_append (str, name);
2917 g_string_append (str, " ");
2918 s = gtk_symbolic_color_to_string (symbolic);
2919 g_string_append (str, s);
2921 g_string_append (str, ";\n");
2928 * gtk_css_provider_to_string:
2929 * @provider: the provider to write to a string
2931 * Convertes the @provider into a string representation in CSS
2934 * Using gtk_css_provider_load_from_data() with the return value
2935 * from this function on a new provider created with
2936 * gtk_css_provider_new() will basicallu create a duplicate of
2939 * Returns: a new string representing the @provider.
2944 gtk_css_provider_to_string (GtkCssProvider *provider)
2946 GtkCssProviderPrivate *priv;
2950 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
2952 priv = provider->priv;
2954 str = g_string_new ("");
2956 gtk_css_provider_print_colors (priv->symbolic_colors, str);
2958 for (i = 0; i < priv->rulesets->len; i++)
2961 g_string_append (str, "\n");
2962 gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
2965 return g_string_free (str, FALSE);