1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <cairo-gobject.h>
28 #include "gtkcssproviderprivate.h"
30 #include "gtkcssparserprivate.h"
31 #include "gtkcssselectorprivate.h"
32 #include "gtkcssstringfuncsprivate.h"
33 #include "gtksymboliccolor.h"
34 #include "gtkstyleprovider.h"
35 #include "gtkstylecontextprivate.h"
36 #include "gtkstylepropertiesprivate.h"
37 #include "gtkbindings.h"
38 #include "gtkmarshalers.h"
39 #include "gtkprivate.h"
43 * SECTION:gtkcssprovider
44 * @Short_description: CSS-like styling for widgets
45 * @Title: GtkCssProvider
46 * @See_also: #GtkStyleContext, #GtkStyleProvider
48 * GtkCssProvider is an object implementing the #GtkStyleProvider interface.
49 * It is able to parse <ulink url="http://www.w3.org/TR/CSS2">CSS</ulink>-like
50 * input in order to style widgets.
52 * <refsect2 id="gtkcssprovider-files">
53 * <title>Default files</title>
55 * An application can cause GTK+ to parse a specific CSS style sheet by
56 * calling gtk_css_provider_load_from_file() and adding the provider with
57 * gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen().
58 * In addition, certain files will be read when GTK+ is initialized. First,
59 * the file <filename><envar>$XDG_CONFIG_HOME</envar>/gtk-3.0/gtk.css</filename>
60 * is loaded if it exists. Then, GTK+ tries to load
61 * <filename><envar>$HOME</envar>/.themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
63 * <filename><replaceable>datadir</replaceable>/share/themes/<replaceable>theme-name</replaceable>/gtk-3.0/gtk.css</filename>,
64 * where <replaceable>theme-name</replaceable> is the name of the current theme
65 * (see the #GtkSettings:gtk-theme-name setting) and <replaceable>datadir</replaceable>
66 * is the prefix configured when GTK+ was compiled, unless overridden by the
67 * <envar>GTK_DATA_PREFIX</envar> environment variable.
70 * <refsect2 id="gtkcssprovider-stylesheets">
71 * <title>Style sheets</title>
73 * The basic structure of the style sheets understood by this provider is
74 * a series of statements, which are either rule sets or '@-rules', separated
78 * A rule set consists of a selector and a declaration block, which is
79 * a series of declarations enclosed in curly braces ({ and }). The
80 * declarations are separated by semicolons (;). Multiple selectors can
81 * share the same declaration block, by putting all the separators in
82 * front of the block, separated by commas.
84 * <example><title>A rule set with two selectors</title>
85 * <programlisting language="text">
86 * GtkButton, GtkEntry {
93 * <refsect2 id="gtkcssprovider-selectors">
94 * <title>Selectors</title>
96 * Selectors work very similar to the way they do in CSS, with widget class
97 * names taking the role of element names, and widget names taking the role
98 * of IDs. When used in a selector, widget names must be prefixed with a
99 * '#' character. The '*' character represents the so-called universal
100 * selector, which matches any widget.
103 * To express more complicated situations, selectors can be combined in
106 * <listitem><para>To require that a widget satisfies several conditions,
107 * combine several selectors into one by concatenating them. E.g.
108 * <literal>GtkButton#button1</literal> matches a GtkButton widget
109 * with the name button1.</para></listitem>
110 * <listitem><para>To only match a widget when it occurs inside some other
111 * widget, write the two selectors after each other, separated by whitespace.
112 * E.g. <literal>GtkToolBar GtkButton</literal> matches GtkButton widgets
113 * that occur inside a GtkToolBar.</para></listitem>
114 * <listitem><para>In the previous example, the GtkButton is matched even
115 * if it occurs deeply nested inside the toolbar. To restrict the match
116 * to direct children of the parent widget, insert a '>' character between
117 * the two selectors. E.g. <literal>GtkNotebook > GtkLabel</literal> matches
118 * GtkLabel widgets that are direct children of a GtkNotebook.</para></listitem>
122 * <title>Widget classes and names in selectors</title>
123 * <programlisting language="text">
124 * /* Theme labels that are descendants of a window */
125 * GtkWindow GtkLabel {
126 * background-color: #898989
129 * /* Theme notebooks, and anything that's within these */
131 * background-color: #a939f0
134 * /* Theme combo boxes, and entries that
135 * are direct children of a notebook */
137 * GtkNotebook > GtkEntry {
139 * background-color: #1209a2
142 * /* Theme any widget within a GtkBin */
147 * /* Theme a label named title-label */
148 * GtkLabel#title-label {
152 * /* Theme any widget named main-entry */
154 * background-color: #f0a810
159 * Widgets may also define style classes, which can be used for matching.
160 * When used in a selector, style classes must be prefixed with a '.'
164 * Refer to the documentation of individual widgets to learn which
165 * style classes they define and see <xref linkend="gtkstylecontext-classes"/>
166 * for a list of all style classes used by GTK+ widgets.
169 * Note that there is some ambiguity in the selector syntax when it comes
170 * to differentiation widget class names from regions. GTK+ currently treats
171 * a string as a widget class name if it contains any uppercase characters
172 * (which should work for more widgets with names like GtkLabel).
175 * <title>Style classes in selectors</title>
176 * <programlisting language="text">
177 * /* Theme all widgets defining the class entry */
179 * color: #39f1f9;
182 * /* Theme spinbuttons' entry */
183 * GtkSpinButton.entry {
189 * In complicated widgets like e.g. a GtkNotebook, it may be desirable
190 * to style different parts of the widget differently. To make this
191 * possible, container widgets may define regions, whose names
192 * may be used for matching in selectors.
195 * Some containers allow to further differentiate between regions by
196 * applying so-called pseudo-classes to the region. For example, the
197 * tab region in GtkNotebook allows to single out the first or last
198 * tab by using the :first-child or :last-child pseudo-class.
199 * When used in selectors, pseudo-classes must be prefixed with a
203 * Refer to the documentation of individual widgets to learn which
204 * regions and pseudo-classes they define and see
205 * <xref linkend="gtkstylecontext-classes"/> for a list of all regions
206 * used by GTK+ widgets.
209 * <title>Regions in selectors</title>
210 * <programlisting language="text">
211 * /* Theme any label within a notebook */
212 * GtkNotebook GtkLabel {
213 * color: #f90192;
216 * /* Theme labels within notebook tabs */
217 * GtkNotebook tab GtkLabel {
218 * color: #703910;
221 * /* Theme labels in the any first notebook
222 * tab, both selectors are equivalent */
223 * GtkNotebook tab:nth-child(first) GtkLabel,
224 * GtkNotebook tab:first-child GtkLabel {
225 * color: #89d012;
230 * Another use of pseudo-classes is to match widgets depending on their
231 * state. This is conceptually similar to the :hover, :active or :focus
232 * pseudo-classes in CSS. The available pseudo-classes for widget states
233 * are :active, :prelight (or :hover), :insensitive, :selected, :focused
237 * <title>Styling specific widget states</title>
238 * <programlisting language="text">
239 * /* Theme active (pressed) buttons */
241 * background-color: #0274d9;
244 * /* Theme buttons with the mouse pointer on it,
245 * both are equivalent */
247 * GtkButton:prelight {
248 * background-color: #3085a9;
251 * /* Theme insensitive widgets, both are equivalent */
254 * background-color: #320a91;
257 * /* Theme selection colors in entries */
258 * GtkEntry:selected {
259 * background-color: #56f9a0;
262 * /* Theme focused labels */
264 * background-color: #b4940f;
267 * /* Theme inconsistent checkbuttons */
268 * GtkCheckButton:inconsistent {
269 * background-color: #20395a;
274 * Widget state pseudoclasses may only apply to the last element
278 * To determine the effective style for a widget, all the matching rule
279 * sets are merged. As in CSS, rules apply by specificity, so the rules
280 * whose selectors more closely match a widget path will take precedence
284 * <refsect2 id="gtkcssprovider-rules">
285 * <title>@ Rules</title>
287 * GTK+'s CSS supports the @import rule, in order to load another
288 * CSS style sheet in addition to the currently parsed one.
291 * <title>Using the @import rule</title>
292 * <programlisting language="text">
293 * @import url ("path/to/common.css");
296 * <para id="css-binding-set">
297 * In order to extend key bindings affecting different widgets, GTK+
298 * supports the @binding-set rule to parse a set of bind/unbind
299 * directives, see #GtkBindingSet for the supported syntax. Note that
300 * the binding sets defined in this way must be associated with rule sets
301 * by setting the gtk-key-bindings style property.
304 * Customized key bindings are typically defined in a separate
305 * <filename>gtk-keys.css</filename> CSS file and GTK+ loads this file
306 * according to the current key theme, which is defined by the
307 * #GtkSettings:gtk-key-theme-name setting.
310 * <title>Using the @binding rule</title>
311 * <programlisting language="text">
312 * @binding-set binding-set1 {
313 * bind "<alt>Left" { "move-cursor" (visual-positions, -3, 0) };
317 * @binding-set binding-set2 {
318 * bind "<alt>Right" { "move-cursor" (visual-positions, 3, 0) };
319 * bind "<alt>KP_space" { "delete-from-cursor" (whitespace, 1)
320 * "insert-at-cursor" (" ") };
324 * gtk-key-bindings: binding-set1, binding-set2;
329 * GTK+ also supports an additional @define-color rule, in order
330 * to define a color name which may be used instead of color numeric
331 * representations. Also see the #GtkSettings:gtk-color-scheme setting
332 * for a way to override the values of these named colors.
335 * <title>Defining colors</title>
336 * <programlisting language="text">
337 * @define-color bg_color #f9a039;
340 * background-color: @bg_color;
345 * <refsect2 id="gtkcssprovider-symbolic-colors">
346 * <title>Symbolic colors</title>
348 * Besides being able to define color names, the CSS parser is also able
349 * to read different color expressions, which can also be nested, providing
350 * a rich language to define colors which are derived from a set of base
354 * <title>Using symbolic colors</title>
355 * <programlisting language="text">
356 * @define-color entry-color shade (@bg_color, 0.7);
359 * background-color: @entry-color;
363 * background-color: mix (@entry-color,
364 * shade (#fff, 0.5),
370 * The various ways to express colors in GTK+ CSS are:
376 * <entry>Syntax</entry>
377 * <entry>Explanation</entry>
378 * <entry>Examples</entry>
383 * <entry>rgb(@r, @g, @b)</entry>
384 * <entry>An opaque color; @r, @g, @b can be either integers between
385 * 0 and 255 or percentages</entry>
386 * <entry><literallayout>rgb(128, 10, 54)
387 * rgb(20%, 30%, 0%)</literallayout></entry>
390 * <entry>rgba(@r, @g, @b, @a)</entry>
391 * <entry>A translucent color; @r, @g, @b are as in the previous row,
392 * @a is a floating point number between 0 and 1</entry>
393 * <entry><literallayout>rgba(255, 255, 0, 0.5)</literallayout></entry>
396 * <entry>#@xxyyzz</entry>
397 * <entry>An opaque color; @xx, @yy, @zz are hexadecimal numbers
398 * specifying @r, @g, @b variants with between 1 and 4
399 * hexadecimal digits per component are allowed</entry>
400 * <entry><literallayout>#ff12ab
401 * #f0c</literallayout></entry>
404 * <entry>@name</entry>
405 * <entry>Reference to a color that has been defined with
406 * @define-color
408 * <entry>@bg_color</entry>
411 * <entry>mix(@color1, @color2, @f)</entry>
412 * <entry>A linear combination of @color1 and @color2. @f is a
413 * floating point number between 0 and 1.</entry>
414 * <entry><literallayout>mix(#ff1e0a, @bg_color, 0.8)</literallayout></entry>
417 * <entry>shade(@color, @f)</entry>
418 * <entry>A lighter or darker variant of @color. @f is a
419 * floating point number.
421 * <entry>shade(@fg_color, 0.5)</entry>
424 * <entry>lighter(@color)</entry>
425 * <entry>A lighter variant of @color</entry>
428 * <entry>darker(@color)</entry>
429 * <entry>A darker variant of @color</entry>
435 * <refsect2 id="gtkcssprovider-gradients">
436 * <title>Gradients</title>
438 * Linear or radial Gradients can be used as background images.
441 * A linear gradient along the line from (@start_x, @start_y) to
442 * (@end_x, @end_y) is specified using the syntax
443 * <literallayout>-gtk-gradient (linear,
444 * @start_x @start_y, @end_x @end_y,
445 * color-stop (@position, @color),
446 * ...)</literallayout>
447 * where @start_x and @end_x can be either a floating point number between
448 * 0 and 1 or one of the special values 'left', 'right' or 'center', @start_y
449 * and @end_y can be either a floating point number between 0 and 1 or one
450 * of the special values 'top', 'bottom' or 'center', @position is a floating
451 * point number between 0 and 1 and @color is a color expression (see above).
452 * The color-stop can be repeated multiple times to add more than one color
453 * stop. 'from (@color)' and 'to (@color)' can be used as abbreviations for
454 * color stops with position 0 and 1, respectively.
457 * <title>A linear gradient</title>
458 * <inlinegraphic fileref="gradient1.png" format="PNG"/>
459 * <para>This gradient was specified with
460 * <literallayout>-gtk-gradient (linear,
461 * left top, right bottom,
462 * from(@yellow), to(@blue))</literallayout></para>
465 * <title>Another linear gradient</title>
466 * <inlinegraphic fileref="gradient2.png" format="PNG"/>
467 * <para>This gradient was specified with
468 * <literallayout>-gtk-gradient (linear,
470 * color-stop(0, @yellow),
471 * color-stop(0.2, @blue),
472 * color-stop(1, #0f0))</literallayout></para>
475 * A radial gradient along the two circles defined by (@start_x, @start_y,
476 * @start_radius) and (@end_x, @end_y, @end_radius) is specified using the
478 * <literallayout>-gtk-gradient (radial,
479 * @start_x @start_y, @start_radius,
480 * @end_x @end_y, @end_radius,
481 * color-stop (@position, @color),
482 * ...)</literallayout>
483 * where @start_radius and @end_radius are floating point numbers and
484 * the other parameters are as before.
487 * <title>A radial gradient</title>
488 * <inlinegraphic fileref="gradient3.png" format="PNG"/>
489 * <para>This gradient was specified with
490 * <literallayout>-gtk-gradient (radial,
493 * from(@yellow), to(@green))</literallayout></para>
496 * <title>Another radial gradient</title>
497 * <inlinegraphic fileref="gradient4.png" format="PNG"/>
498 * <para>This gradient was specified with
499 * <literallayout>-gtk-gradient (radial,
502 * color-stop (0, #f00),
503 * color-stop (0.1, #a0f),
504 * color-stop (0.2, @yellow),
505 * color-stop (1, @green))</literallayout></para>
508 * <refsect2 id="gtkcssprovider-slices">
509 * <title>Border images</title>
511 * Images can be used in 'slices' for the purpose of creating scalable
514 * <inlinegraphic fileref="slices.png" format="PNG"/>
516 * The syntax for specifying border images of this kind is:
517 * <literallayout>url(@path) @top @right @bottom @left [repeat|stretch]? [repeat|stretch]?</literallayout>
518 * The sizes of the 'cut off' portions are specified
519 * with the @top, @right, @bottom and @left parameters.
520 * The 'middle' sections can be repeated or stretched to create
521 * the desired effect, by adding the 'repeat' or 'stretch' options after
522 * the dimensions. If two options are specified, the first one affects
523 * the horizontal behaviour and the second one the vertical behaviour.
524 * If only one option is specified, it affects both.
527 * <title>A border image</title>
528 * <inlinegraphic fileref="border1.png" format="PNG"/>
529 * <para>This border image was specified with
530 * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
534 * <title>A repeating border image</title>
535 * <inlinegraphic fileref="border2.png" format="PNG"/>
536 * <para>This border image was specified with
537 * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
541 * <title>A stretched border image</title>
542 * <inlinegraphic fileref="border3.png" format="PNG"/>
543 * <para>This border image was specified with
544 * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
548 * <refsect2 id="gtkcssprovider-transitions">
549 * <para>Styles can specify transitions that will be used to create a gradual
550 * change in the appearance when a widget state changes. The following
551 * syntax is used to specify transitions:
552 * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
553 * The @duration is the amount of time that the animation will take for
554 * a complete cycle from start to end. If the loop option is given, the
555 * animation will be repated until the state changes again.
556 * The option after the duration determines the transition function from a
557 * small set of predefined functions.
558 * <figure><title>Linear transition</title>
559 * <graphic fileref="linear.png" format="PNG"/>
561 * <figure><title>Ease transition</title>
562 * <graphic fileref="ease.png" format="PNG"/>
564 * <figure><title>Ease-in-out transition</title>
565 * <graphic fileref="ease-in-out.png" format="PNG"/>
567 * <figure><title>Ease-in transition</title>
568 * <graphic fileref="ease-in.png" format="PNG"/>
570 * <figure><title>Ease-out transition</title>
571 * <graphic fileref="ease-out.png" format="PNG"/>
575 * <refsect2 id="gtkcssprovider-properties">
576 * <title>Supported properties</title>
578 * Properties are the part that differ the most to common CSS,
579 * not all properties are supported (some are planned to be
580 * supported eventually, some others are meaningless or don't
581 * map intuitively in a widget based environment).
584 * There is also a difference in shorthand properties, for
585 * example in common CSS it is fine to define a font through
586 * the different @font-family, @font-style, @font-size
587 * properties, meanwhile in GTK+'s CSS only the canonical
588 * @font property is supported.
591 * The currently supported properties are:
597 * <entry>Property name</entry>
598 * <entry>Syntax</entry>
599 * <entry>Maps to</entry>
600 * <entry>Examples</entry>
605 * <entry>engine</entry>
606 * <entry>engine-name</entry>
607 * <entry>#GtkThemingEngine</entry>
608 * <entry>engine: clearlooks;
609 * engine: none; /* use the default (i.e. builtin) engine) */ </entry>
612 * <entry>background-color</entry>
613 * <entry morerows="2">color (see above)</entry>
614 * <entry morerows="2">#GdkRGBA</entry>
615 * <entry morerows="2"><literallayout>background-color: #fff;
616 * color: &color1;
617 * background-color: shade (&color1, 0.5);
618 * color: mix (&color1, #f0f, 0.8);</literallayout>
622 * <entry>color</entry>
625 * <entry>border-color</entry>
628 * <entry>font</entry>
629 * <entry>@family [@style] [@size]</entry>
630 * <entry>#PangoFontDescription</entry>
631 * <entry>font: Sans 15;</entry>
634 * <entry>margin</entry>
635 * <entry morerows="1"><literallayout>@width
636 * @vertical_width @horizontal_width
637 * @top_width @horizontal_width @bottom_width
638 * @top_width @right_width @bottom_width @left_width</literallayout>
640 * <entry morerows="1">#GtkBorder</entry>
641 * <entry morerows="1"><literallayout>margin: 5;
644 * margin: 5 10 3 5;</literallayout>
648 * <entry>padding</entry>
651 * <entry>background-image</entry>
652 * <entry><literallayout>gradient (see above) or
653 * url(@path)</literallayout></entry>
654 * <entry>#cairo_pattern_t</entry>
655 * <entry><literallayout>-gtk-gradient (linear,
656 * left top, right top,
657 * from (#fff), to (#000));
658 * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
660 * color-stop (0.5, #f00),
662 * -gtk-gradient (radial,
663 * center center, 0.2,
664 * center center, 0.8,
665 * color-stop (0.0, #fff),
666 * color-stop (1.0, #000));
667 * url ('background.png');</literallayout>
671 * <entry>border-width</entry>
672 * <entry>integer</entry>
673 * <entry>#gint</entry>
674 * <entry>border-width: 5;</entry>
677 * <entry>border-radius</entry>
678 * <entry>integer</entry>
679 * <entry>#gint</entry>
680 * <entry>border-radius: 5;</entry>
683 * <entry>border-style</entry>
684 * <entry>[none|solid|inset|outset]</entry>
685 * <entry>#GtkBorderStyle</entry>
686 * <entry>border-style: solid;</entry>
689 * <entry>border-image</entry>
690 * <entry><literallayout>border image (see above)</literallayout></entry>
691 * <entry>internal use only</entry>
692 * <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
693 * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
697 * <entry>transition</entry>
698 * <entry>transition (see above)</entry>
699 * <entry>internal use only</entry>
700 * <entry><literallayout>transition: 150ms ease-in-out;
701 * transition: 1s linear loop;</literallayout>
705 * <entry>gtk-key-bindings</entry>
706 * <entry>binding set name list</entry>
707 * <entry>internal use only</entry>
708 * <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
715 * GtkThemingEngines can register their own, engine-specific style properties
716 * with the function gtk_theming_engine_register_property(). These properties
717 * can be set in CSS like other properties, using a name of the form
718 * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
719 * the name of the theming engine, and <replaceable>name</replaceable> is the
720 * name of the property. Style properties that have been registered by widgets
721 * using gtk_widget_class_install_style_property() can also be set in this
722 * way, using the widget class name for <replaceable>namespace</replaceable>.
725 * <title>Using engine-specific style properties</title>
728 * engine: clearlooks;
730 * -GtkPaned-handle-size: 6;
731 * -clearlooks-colorize-scrollbar: false;
738 typedef struct GtkCssRuleset GtkCssRuleset;
739 typedef struct _GtkCssScanner GtkCssScanner;
740 typedef enum ParserScope ParserScope;
741 typedef enum ParserSymbol ParserSymbol;
745 GtkCssSelector *selector;
746 GHashTable *widget_style;
749 guint has_inherit :1;
752 struct _GtkCssScanner
754 GtkCssProvider *provider;
755 GtkCssParser *parser;
756 GtkCssScanner *parent;
760 GSList *cur_selectors;
761 GHashTable *cur_properties;
764 struct _GtkCssProviderPrivate
768 GHashTable *symbolic_colors;
782 /* Extend GtkStateType, since these
783 * values are also used as symbols
786 /* Scope: pseudo-class */
787 SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
792 /* Scope: nth-child */
793 SYMBOL_NTH_CHILD_EVEN,
794 SYMBOL_NTH_CHILD_ODD,
795 SYMBOL_NTH_CHILD_FIRST,
796 SYMBOL_NTH_CHILD_LAST
804 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
806 static void gtk_css_provider_finalize (GObject *object);
807 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
810 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
811 GtkCssScanner *scanner,
818 gtk_css_provider_error_quark (void)
820 return g_quark_from_static_string ("gtk-css-provider-error-quark");
823 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
824 G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
825 gtk_css_style_provider_iface_init));
828 gtk_css_provider_parsing_error (GtkCssProvider *provider,
832 const GError * error)
834 /* Only emit a warning when we have no error handlers. This is our
835 * default handlers. And in this case erroneous CSS files are a bug
836 * and should be fixed.
837 * Note that these warnings can also be triggered by a broken theme
838 * that people installed from some weird location on the internets.
840 if (!g_signal_has_handler_pending (provider,
841 css_provider_signals[PARSING_ERROR],
845 g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
850 gtk_css_provider_class_init (GtkCssProviderClass *klass)
852 GObjectClass *object_class = G_OBJECT_CLASS (klass);
855 * GtkCssProvider::parsing-error:
856 * @provider: the provider that had a parsing error
857 * @path: path to the parsed file or %NULL if the file cannot be
858 * identified or the data was not loaded from a file
859 * @line: line in the file or data or 0 if unknown
860 * @position: offset into the current line or 0 if unknown or the
861 * whole line is affected
862 * @error: The parsing error
864 * Signals that a parsing error occured. the @path, @line and @position
865 * describe the actual location of the error as accurately as possible.
867 * Parsing errors are never fatal, so the parsing will resume after
868 * the error. Errors may however cause parts of the given
869 * data or even all of it to not be parsed at all. So it is a useful idea
870 * to check that the parsing succeeds by connecting to this signal.
872 * Note that this signal may be emitted at any time as the css provider
873 * may opt to defer parsing parts or all of the input to a later time
874 * than when a loading function was called.
876 css_provider_signals[PARSING_ERROR] =
877 g_signal_new (I_("parsing-error"),
878 G_TYPE_FROM_CLASS (object_class),
880 G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
882 _gtk_marshal_VOID__STRING_UINT_UINT_BOXED,
884 G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR);
886 object_class->finalize = gtk_css_provider_finalize;
888 klass->parsing_error = gtk_css_provider_parsing_error;
890 g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
894 gtk_css_provider_take_error_full (GtkCssProvider *provider,
903 filename = g_file_get_path (file);
907 g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
908 filename, line, position, error);
911 g_error_free (error);
915 gtk_css_ruleset_init_copy (GtkCssRuleset *new,
916 const GtkCssRuleset *ruleset,
917 GtkCssSelector *selector)
919 memcpy (new, ruleset, sizeof (GtkCssRuleset));
921 new->selector = selector;
922 if (new->widget_style)
923 g_hash_table_ref (new->widget_style);
925 g_hash_table_ref (new->style);
929 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
932 g_hash_table_unref (ruleset->style);
933 if (ruleset->widget_style)
934 g_hash_table_unref (ruleset->widget_style);
935 if (ruleset->selector)
936 _gtk_css_selector_free (ruleset->selector);
938 memset (ruleset, 0, sizeof (GtkCssRuleset));
942 property_value_free (GValue *value)
944 if (G_IS_VALUE (value))
945 g_value_unset (value);
947 g_slice_free (GValue, value);
951 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
955 if (ruleset->widget_style == NULL)
956 ruleset->widget_style = g_hash_table_new_full (g_str_hash,
958 (GDestroyNotify) g_free,
959 (GDestroyNotify) property_value_free);
961 g_hash_table_insert (ruleset->widget_style, name, value);
965 gtk_css_ruleset_add (GtkCssRuleset *ruleset,
969 if (ruleset->style == NULL)
970 ruleset->style = g_hash_table_new_full (g_direct_hash,
973 (GDestroyNotify) property_value_free);
975 ruleset->has_inherit |= gtk_style_param_get_inherit (pspec);
976 g_hash_table_insert (ruleset->style, pspec, value);
980 gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
984 return _gtk_css_selector_matches (ruleset->selector, path, length);
988 gtk_css_scanner_reset (GtkCssScanner *scanner)
990 g_slist_free (scanner->state);
991 scanner->state = NULL;
993 g_slist_free_full (scanner->cur_selectors, (GDestroyNotify) _gtk_css_selector_free);
994 scanner->cur_selectors = NULL;
996 if (scanner->cur_properties)
997 g_hash_table_unref (scanner->cur_properties);
999 scanner ->cur_properties = g_hash_table_new_full (g_str_hash,
1001 (GDestroyNotify) g_free,
1002 (GDestroyNotify) property_value_free);
1006 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1008 gtk_css_scanner_reset (scanner);
1010 g_object_unref (scanner->provider);
1012 g_object_unref (scanner->file);
1013 g_object_unref (scanner->base);
1014 g_hash_table_destroy (scanner->cur_properties);
1015 _gtk_css_parser_free (scanner->parser);
1017 g_slice_free (GtkCssScanner, scanner);
1021 gtk_css_scanner_parser_error (GtkCssParser *parser,
1022 const GError *error,
1025 GtkCssScanner *scanner = user_data;
1027 gtk_css_provider_take_error_full (scanner->provider,
1029 _gtk_css_parser_get_line (scanner->parser),
1030 _gtk_css_parser_get_position (scanner->parser),
1031 g_error_copy (error));
1034 static GtkCssScanner *
1035 gtk_css_scanner_new (GtkCssProvider *provider,
1036 GtkCssScanner *parent,
1041 GtkCssScanner *scanner;
1043 g_assert (data[length] == 0);
1045 scanner = g_slice_new0 (GtkCssScanner);
1047 g_object_ref (provider);
1048 scanner->provider = provider;
1049 scanner->parent = parent;
1053 scanner->file = g_object_ref (file);
1054 scanner->base = g_file_get_parent (file);
1058 char *dir = g_get_current_dir ();
1059 scanner->base = g_file_new_for_path (dir);
1063 scanner->cur_properties = g_hash_table_new_full (g_str_hash,
1065 (GDestroyNotify) g_free,
1066 (GDestroyNotify) property_value_free);
1068 scanner->parser = _gtk_css_parser_new (data,
1069 gtk_css_scanner_parser_error,
1076 gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
1078 return scanner->base;
1082 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1087 if (scanner->file && g_file_equal (scanner->file, file))
1090 scanner = scanner->parent;
1097 gtk_css_provider_init (GtkCssProvider *css_provider)
1099 GtkCssProviderPrivate *priv;
1101 priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1102 GTK_TYPE_CSS_PROVIDER,
1103 GtkCssProviderPrivate);
1105 priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1107 priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1108 (GDestroyNotify) g_free,
1109 (GDestroyNotify) gtk_symbolic_color_unref);
1113 css_provider_dump_symbolic_colors (GtkCssProvider *css_provider,
1114 GtkStyleProperties *props)
1116 GtkCssProviderPrivate *priv;
1117 GHashTableIter iter;
1118 gpointer key, value;
1120 priv = css_provider->priv;
1121 g_hash_table_iter_init (&iter, priv->symbolic_colors);
1123 while (g_hash_table_iter_next (&iter, &key, &value))
1126 GtkSymbolicColor *color;
1131 gtk_style_properties_map_color (props, name, color);
1135 static GtkStyleProperties *
1136 gtk_css_provider_get_style (GtkStyleProvider *provider,
1137 GtkWidgetPath *path)
1139 GtkCssProvider *css_provider;
1140 GtkCssProviderPrivate *priv;
1141 GtkStyleProperties *props;
1144 css_provider = GTK_CSS_PROVIDER (provider);
1145 priv = css_provider->priv;
1146 length = gtk_widget_path_length (path);
1147 props = gtk_style_properties_new ();
1149 css_provider_dump_symbolic_colors (css_provider, props);
1151 for (l = 1; l <= length; l++)
1153 for (i = 0; i < priv->rulesets->len; i++)
1155 GtkCssRuleset *ruleset;
1156 GHashTableIter iter;
1157 gpointer key, value;
1159 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1161 if (ruleset->style == NULL)
1164 if (l < length && (!ruleset->has_inherit || _gtk_css_selector_get_state_flags (ruleset->selector)))
1167 if (!gtk_css_ruleset_matches (ruleset, path, l))
1170 g_hash_table_iter_init (&iter, ruleset->style);
1172 while (g_hash_table_iter_next (&iter, &key, &value))
1174 GParamSpec *pspec = key;
1176 if (l != length && !gtk_style_param_get_inherit (pspec))
1179 _gtk_style_properties_set_property_by_pspec (props,
1181 _gtk_css_selector_get_state_flags (ruleset->selector),
1191 gtk_css_provider_parser_error (GtkCssParser *parser,
1192 const GError *error,
1195 GtkCssProvider *provider = user_data;
1197 gtk_css_provider_take_error_full (provider,
1199 _gtk_css_parser_get_line (parser),
1200 _gtk_css_parser_get_position (parser),
1201 g_error_copy (error));
1205 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1206 GtkWidgetPath *path,
1207 GtkStateFlags state,
1211 GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1212 GtkCssProviderPrivate *priv = css_provider->priv;
1214 gboolean found = FALSE;
1218 prop_name = g_strdup_printf ("-%s-%s",
1219 g_type_name (pspec->owner_type),
1222 for (i = priv->rulesets->len - 1; i >= 0; i--)
1224 GtkCssRuleset *ruleset;
1225 GtkStateFlags selector_state;
1227 ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1229 if (ruleset->widget_style == NULL)
1232 if (!gtk_css_ruleset_matches (ruleset, path, gtk_widget_path_length (path)))
1235 selector_state = _gtk_css_selector_get_state_flags (ruleset->selector);
1236 val = g_hash_table_lookup (ruleset->widget_style, prop_name);
1239 (selector_state == 0 ||
1240 selector_state == state ||
1241 ((selector_state & state) != 0 &&
1242 (selector_state & ~(state)) == 0)))
1244 GtkCssParser *parser;
1246 parser = _gtk_css_parser_new (g_value_get_string (val),
1247 gtk_css_provider_parser_error,
1250 found = _gtk_css_value_parse (value,
1254 _gtk_css_parser_free (parser);
1267 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1269 iface->get_style = gtk_css_provider_get_style;
1270 iface->get_style_property = gtk_css_provider_get_style_property;
1274 gtk_css_provider_finalize (GObject *object)
1276 GtkCssProvider *css_provider;
1277 GtkCssProviderPrivate *priv;
1279 css_provider = GTK_CSS_PROVIDER (object);
1280 priv = css_provider->priv;
1282 g_array_free (priv->rulesets, TRUE);
1284 if (priv->symbolic_colors)
1285 g_hash_table_destroy (priv->symbolic_colors);
1287 G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1291 * gtk_css_provider_new:
1293 * Returns a newly created #GtkCssProvider.
1295 * Returns: A new #GtkCssProvider
1298 gtk_css_provider_new (void)
1300 return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1304 gtk_css_provider_take_error (GtkCssProvider *provider,
1305 GtkCssScanner *scanner,
1308 gtk_css_provider_take_error_full (provider,
1310 _gtk_css_parser_get_line (scanner->parser),
1311 _gtk_css_parser_get_position (scanner->parser),
1316 gtk_css_provider_error_literal (GtkCssProvider *provider,
1317 GtkCssScanner *scanner,
1320 const char *message)
1322 gtk_css_provider_take_error (provider,
1324 g_error_new_literal (domain, code, message));
1328 gtk_css_provider_error (GtkCssProvider *provider,
1329 GtkCssScanner *scanner,
1333 ...) G_GNUC_PRINTF (5, 6);
1335 gtk_css_provider_error (GtkCssProvider *provider,
1336 GtkCssScanner *scanner,
1345 va_start (args, format);
1346 error = g_error_new_valist (domain, code, format, args);
1349 gtk_css_provider_take_error (provider, scanner, error);
1353 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1354 GtkCssScanner *scanner,
1355 const char *expected)
1357 gtk_css_provider_error (provider,
1359 GTK_CSS_PROVIDER_ERROR,
1360 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1361 "expected a valid %s", expected);
1365 css_provider_commit (GtkCssProvider *css_provider,
1367 GtkCssRuleset *ruleset)
1369 GtkCssProviderPrivate *priv;
1372 priv = css_provider->priv;
1374 if (ruleset->style == NULL && ruleset->widget_style == NULL)
1376 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1380 for (l = selectors; l; l = l->next)
1384 gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1386 g_array_append_val (priv->rulesets, new);
1389 g_slist_free (selectors);
1393 gtk_css_provider_reset (GtkCssProvider *css_provider)
1395 GtkCssProviderPrivate *priv;
1398 priv = css_provider->priv;
1400 for (i = 0; i < priv->rulesets->len; i++)
1401 gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1402 g_array_set_size (priv->rulesets, 0);
1406 gtk_css_provider_propagate_error (GtkCssProvider *provider,
1410 const GError *error,
1411 GError **propagate_to)
1413 /* we already set an error. And we'd like to keep the first one */
1417 *propagate_to = g_error_copy (error);
1418 g_prefix_error (propagate_to, "%s:%u:%u: ", path ? path : "<unknown>", line, position);
1422 parse_import (GtkCssScanner *scanner)
1427 uri = _gtk_css_parser_read_uri (scanner->parser);
1430 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1434 file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
1437 if (gtk_css_scanner_would_recurse (scanner, file))
1439 char *path = g_file_get_path (file);
1440 gtk_css_provider_error (scanner->provider,
1442 GTK_CSS_PROVIDER_ERROR,
1443 GTK_CSS_PROVIDER_ERROR_IMPORT,
1444 "Loading '%s' would recurse",
1450 gtk_css_provider_load_internal (scanner->provider,
1457 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1459 g_object_unref (file);
1460 gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1461 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1465 g_object_unref (file);
1469 parse_color_definition (GtkCssScanner *scanner)
1471 GtkSymbolicColor *symbolic;
1474 name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1477 gtk_css_provider_error_literal (scanner->provider,
1479 GTK_CSS_PROVIDER_ERROR,
1480 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1481 "Not a valid color name");
1482 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1486 symbolic = _gtk_css_parser_read_symbolic_color (scanner->parser);
1487 if (symbolic == NULL)
1490 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1494 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1497 gtk_symbolic_color_unref (symbolic);
1498 gtk_css_provider_error_literal (scanner->provider,
1500 GTK_CSS_PROVIDER_ERROR,
1501 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1502 "Missing semicolon at end of color definition");
1503 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1507 g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
1511 parse_binding_set (GtkCssScanner *scanner)
1513 GtkBindingSet *binding_set;
1516 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1519 gtk_css_provider_error_literal (scanner->provider,
1521 GTK_CSS_PROVIDER_ERROR,
1522 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1523 "Expected name for binding set");
1524 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1525 goto skip_semicolon;
1528 binding_set = gtk_binding_set_find (name);
1531 binding_set = gtk_binding_set_new (name);
1532 binding_set->parsed = TRUE;
1536 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
1538 gtk_css_provider_error_literal (scanner->provider,
1540 GTK_CSS_PROVIDER_ERROR,
1541 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1542 "Expected '{' for binding set");
1543 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1544 goto skip_semicolon;
1547 while (!_gtk_css_parser_is_eof (scanner->parser) &&
1548 !_gtk_css_parser_begins_with (scanner->parser, '}'))
1550 name = _gtk_css_parser_read_value (scanner->parser);
1553 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1557 gtk_binding_entry_add_signal_from_string (binding_set, name);
1560 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1562 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
1563 !_gtk_css_parser_is_eof (scanner->parser))
1565 gtk_css_provider_error_literal (scanner->provider,
1567 GTK_CSS_PROVIDER_ERROR,
1568 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1569 "Expected semicolon");
1570 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1575 if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
1577 gtk_css_provider_error_literal (scanner->provider,
1579 GTK_CSS_PROVIDER_ERROR,
1580 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1581 "expected '}' after declarations");
1582 if (!_gtk_css_parser_is_eof (scanner->parser))
1583 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
1587 if (_gtk_css_parser_try (scanner->parser, ";", TRUE))
1588 gtk_css_provider_error_literal (scanner->provider,
1590 GTK_CSS_PROVIDER_ERROR,
1591 GTK_CSS_PROVIDER_ERROR_DEPRECATED,
1592 "Nonstandard semicolon at end of binding set");
1596 parse_at_keyword (GtkCssScanner *scanner)
1598 if (_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1599 parse_import (scanner);
1600 else if (_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1601 parse_color_definition (scanner);
1602 else if (_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
1603 parse_binding_set (scanner);
1606 gtk_css_provider_error_literal (scanner->provider,
1608 GTK_CSS_PROVIDER_ERROR,
1609 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1611 _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1616 parse_selector_class (GtkCssScanner *scanner, GArray *classes)
1621 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1625 gtk_css_provider_error_literal (scanner->provider,
1627 GTK_CSS_PROVIDER_ERROR,
1628 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1629 "Expected a valid name for class");
1633 qname = g_quark_from_string (name);
1634 g_array_append_val (classes, qname);
1640 parse_selector_name (GtkCssScanner *scanner, GArray *names)
1645 name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1649 gtk_css_provider_error_literal (scanner->provider,
1651 GTK_CSS_PROVIDER_ERROR,
1652 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1653 "Expected a valid name for id");
1657 qname = g_quark_from_string (name);
1658 g_array_append_val (names, qname);
1664 parse_selector_pseudo_class (GtkCssScanner *scanner,
1665 GtkRegionFlags *region_to_modify,
1666 GtkStateFlags *state_to_modify)
1670 GtkRegionFlags region_flag;
1671 GtkStateFlags state_flag;
1672 } pseudo_classes[] = {
1673 { "first", GTK_REGION_FIRST, 0 },
1674 { "last", GTK_REGION_LAST, 0 },
1675 { "sorted", GTK_REGION_SORTED, 0 },
1676 { "active", 0, GTK_STATE_FLAG_ACTIVE },
1677 { "prelight", 0, GTK_STATE_FLAG_PRELIGHT },
1678 { "hover", 0, GTK_STATE_FLAG_PRELIGHT },
1679 { "selected", 0, GTK_STATE_FLAG_SELECTED },
1680 { "insensitive", 0, GTK_STATE_FLAG_INSENSITIVE },
1681 { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
1682 { "focused", 0, GTK_STATE_FLAG_FOCUSED },
1683 { "focus", 0, GTK_STATE_FLAG_FOCUSED },
1685 }, nth_child_classes[] = {
1686 { "first", GTK_REGION_FIRST, 0 },
1687 { "last", GTK_REGION_LAST, 0 },
1688 { "even", GTK_REGION_EVEN, 0 },
1689 { "odd", GTK_REGION_ODD, 0 },
1695 name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
1698 gtk_css_provider_error_literal (scanner->provider,
1700 GTK_CSS_PROVIDER_ERROR,
1701 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1702 "Missing name of pseudo-class");
1706 if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
1708 char *function = name;
1710 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1711 if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
1713 gtk_css_provider_error_literal (scanner->provider,
1715 GTK_CSS_PROVIDER_ERROR,
1716 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1717 "Missing closing bracket for pseudo-class");
1721 if (g_ascii_strcasecmp (function, "nth-child") != 0)
1723 gtk_css_provider_error (scanner->provider,
1725 GTK_CSS_PROVIDER_ERROR,
1726 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1727 "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
1737 gtk_css_provider_error (scanner->provider,
1739 GTK_CSS_PROVIDER_ERROR,
1740 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1741 "nth-child() requires an argument");
1745 classes = nth_child_classes;
1748 classes = pseudo_classes;
1750 for (i = 0; classes[i].name != NULL; i++)
1752 if (g_ascii_strcasecmp (name, classes[i].name) == 0)
1754 if ((*region_to_modify & classes[i].region_flag) ||
1755 (*state_to_modify & classes[i].state_flag))
1757 if (classes == nth_child_classes)
1758 gtk_css_provider_error (scanner->provider,
1760 GTK_CSS_PROVIDER_ERROR,
1761 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1762 "Duplicate pseudo-class 'nth-child(%s)'", name);
1764 gtk_css_provider_error (scanner->provider,
1766 GTK_CSS_PROVIDER_ERROR,
1767 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1768 "Duplicate pseudo-class '%s'", name);
1770 *region_to_modify |= classes[i].region_flag;
1771 *state_to_modify |= classes[i].state_flag;
1778 if (classes == nth_child_classes)
1779 gtk_css_provider_error (scanner->provider,
1781 GTK_CSS_PROVIDER_ERROR,
1782 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1783 "Unknown pseudo-class 'nth-child(%s)'", name);
1785 gtk_css_provider_error (scanner->provider,
1787 GTK_CSS_PROVIDER_ERROR,
1788 GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1789 "Unknown pseudo-class '%s'", name);
1795 parse_simple_selector (GtkCssScanner *scanner,
1799 GtkRegionFlags *pseudo_classes,
1800 GtkStateFlags *state)
1802 gboolean parsed_something;
1804 *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
1806 parsed_something = TRUE;
1808 parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
1811 if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
1813 if (!parse_selector_name (scanner, ids))
1816 else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
1818 if (!parse_selector_class (scanner, classes))
1821 else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
1823 if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
1826 else if (!parsed_something)
1828 gtk_css_provider_error_literal (scanner->provider,
1830 GTK_CSS_PROVIDER_ERROR,
1831 GTK_CSS_PROVIDER_ERROR_SYNTAX,
1832 "Expected a valid selector");
1838 parsed_something = TRUE;
1840 while (!_gtk_css_parser_is_eof (scanner->parser));
1842 _gtk_css_parser_skip_whitespace (scanner->parser);
1846 static GtkCssSelector *
1847 parse_selector (GtkCssScanner *scanner)
1849 GtkCssSelector *selector = NULL;
1853 GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
1854 GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
1855 GtkRegionFlags pseudo_classes = 0;
1856 GtkStateFlags state = 0;
1857 GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
1861 if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
1862 combine = GTK_CSS_COMBINE_CHILD;
1865 if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
1867 g_array_free (ids, TRUE);
1868 g_array_free (classes, TRUE);
1870 _gtk_css_selector_free (selector);
1874 selector = _gtk_css_selector_new (selector,
1877 (GQuark *) g_array_free (ids, ids->len == 0),
1878 (GQuark *) g_array_free (classes, classes->len == 0),
1883 while (!_gtk_css_parser_is_eof (scanner->parser) &&
1884 !_gtk_css_parser_begins_with (scanner->parser, ',') &&
1885 !_gtk_css_parser_begins_with (scanner->parser, '{'));
1891 parse_selector_list (GtkCssScanner *scanner)
1893 GSList *selectors = NULL;
1896 GtkCssSelector *select = parse_selector (scanner);
1900 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1901 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
1905 selectors = g_slist_prepend (selectors, select);
1907 while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
1913 parse_declaration (GtkCssScanner *scanner,
1914 GtkCssRuleset *ruleset)
1916 GtkStylePropertyParser parse_func = NULL;
1917 GParamSpec *pspec = NULL;
1920 name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1922 goto check_for_semicolon;
1924 if (!gtk_style_properties_lookup_property (name, &parse_func, &pspec) &&
1927 gtk_css_provider_error (scanner->provider,
1929 GTK_CSS_PROVIDER_ERROR,
1930 GTK_CSS_PROVIDER_ERROR_NAME,
1931 "'%s' is not a valid property name",
1933 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1938 if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
1940 gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
1941 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1952 val = g_slice_new0 (GValue);
1953 g_value_init (val, pspec->value_type);
1955 if (_gtk_css_parser_try (scanner->parser, "none", TRUE))
1957 /* Insert the default value, so it has an opportunity
1958 * to override other style providers when merged
1960 g_param_value_set_default (pspec, val);
1961 gtk_css_ruleset_add (ruleset, pspec, val);
1963 else if (parse_func)
1965 GError *error = NULL;
1968 value_str = _gtk_css_parser_read_value (scanner->parser);
1969 if (value_str == NULL)
1971 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1975 if ((*parse_func) (value_str, val, &error))
1976 gtk_css_ruleset_add (ruleset, pspec, val);
1978 gtk_css_provider_take_error (scanner->provider, scanner, error);
1984 if (_gtk_css_value_parse (val,
1986 gtk_css_scanner_get_base_url (scanner)))
1988 if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
1989 _gtk_css_parser_begins_with (scanner->parser, '}') ||
1990 _gtk_css_parser_is_eof (scanner->parser))
1992 gtk_css_ruleset_add (ruleset, pspec, val);
1996 gtk_css_provider_error_literal (scanner->provider,
1998 GTK_CSS_PROVIDER_ERROR,
1999 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2000 "Junk at end of value");
2001 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2002 g_value_unset (val);
2003 g_slice_free (GValue, val);
2009 g_value_unset (val);
2010 g_slice_free (GValue, val);
2011 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2016 else if (name[0] == '-')
2020 value_str = _gtk_css_parser_read_value (scanner->parser);
2025 val = g_slice_new0 (GValue);
2026 g_value_init (val, G_TYPE_STRING);
2027 g_value_take_string (val, value_str);
2029 gtk_css_ruleset_add_style (ruleset, name, val);
2033 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2040 check_for_semicolon:
2041 if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2043 if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2044 !_gtk_css_parser_is_eof (scanner->parser))
2046 gtk_css_provider_error_literal (scanner->provider,
2048 GTK_CSS_PROVIDER_ERROR,
2049 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2050 "Expected semicolon");
2051 _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2057 parse_declarations (GtkCssScanner *scanner,
2058 GtkCssRuleset *ruleset)
2060 while (!_gtk_css_parser_is_eof (scanner->parser) &&
2061 !_gtk_css_parser_begins_with (scanner->parser, '}'))
2063 parse_declaration (scanner, ruleset);
2068 parse_ruleset (GtkCssScanner *scanner)
2071 GtkCssRuleset ruleset = { 0, };
2073 selectors = parse_selector_list (scanner);
2074 if (selectors == NULL)
2077 if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2079 gtk_css_provider_error_literal (scanner->provider,
2081 GTK_CSS_PROVIDER_ERROR,
2082 GTK_CSS_PROVIDER_ERROR_SYNTAX,
2083 "expected '{' after selectors");
2084 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2085 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2089 parse_declarations (scanner, &ruleset);
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 '}' after declarations");
2098 if (!_gtk_css_parser_is_eof (scanner->parser))
2100 _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2101 g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2102 gtk_css_ruleset_clear (&ruleset);
2107 css_provider_commit (scanner->provider, selectors, &ruleset);
2108 gtk_css_ruleset_clear (&ruleset);
2112 parse_statement (GtkCssScanner *scanner)
2114 if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2115 parse_at_keyword (scanner);
2117 parse_ruleset (scanner);
2121 parse_stylesheet (GtkCssScanner *scanner)
2123 _gtk_css_parser_skip_whitespace (scanner->parser);
2125 while (!_gtk_css_parser_is_eof (scanner->parser))
2127 if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2128 _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2131 parse_statement (scanner);
2136 gtk_css_provider_compare_rule (gconstpointer a_,
2139 const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2140 const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2143 compare = _gtk_css_selector_compare (a->selector, b->selector);
2147 /* compare pointers in array to ensure a stable sort */
2158 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2160 GtkCssProviderPrivate *priv = css_provider->priv;
2162 g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2166 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2167 GtkCssScanner *parent,
2173 GtkCssScanner *scanner;
2174 gulong error_handler;
2178 error_handler = g_signal_connect (css_provider,
2180 G_CALLBACK (gtk_css_provider_propagate_error),
2183 error_handler = 0; /* silence gcc */
2187 GError *load_error = NULL;
2189 if (g_file_load_contents (file, NULL,
2190 &free_data, &length,
2199 gtk_css_provider_error (css_provider,
2201 GTK_CSS_PROVIDER_ERROR,
2202 GTK_CSS_PROVIDER_ERROR_IMPORT,
2203 "Failed to import: %s",
2204 load_error->message);
2205 g_error_free (load_error);
2209 gtk_css_provider_take_error_full (css_provider,
2221 scanner = gtk_css_scanner_new (css_provider, parent, file, data, length);
2223 parse_stylesheet (scanner);
2225 gtk_css_scanner_destroy (scanner);
2228 gtk_css_provider_postprocess (css_provider);
2233 g_signal_handler_disconnect (css_provider, error_handler);
2237 /* We clear all contents from the provider for backwards compat reasons */
2238 gtk_css_provider_reset (css_provider);
2247 * gtk_css_provider_load_from_data:
2248 * @css_provider: a #GtkCssProvider
2249 * @data: CSS data loaded in memory
2250 * @length: the length of @data in bytes, or -1 for NUL terminated strings
2251 * @error: (out) (allow-none): return location for a #GError, or %NULL
2253 * Loads @data into @css_provider, making it clear any previously loaded
2256 * Returns: %TRUE if the data could be loaded.
2259 gtk_css_provider_load_from_data (GtkCssProvider *css_provider,
2264 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2265 g_return_val_if_fail (data != NULL, FALSE);
2268 length = strlen (data);
2270 gtk_css_provider_reset (css_provider);
2272 return gtk_css_provider_load_internal (css_provider, NULL, NULL, data, length, error);
2276 * gtk_css_provider_load_from_file:
2277 * @css_provider: a #GtkCssProvider
2278 * @file: #GFile pointing to a file to load
2279 * @error: (out) (allow-none): return location for a #GError, or %NULL
2281 * Loads the data contained in @file into @css_provider, making it
2282 * clear any previously loaded information.
2284 * Returns: %TRUE if the data could be loaded.
2287 gtk_css_provider_load_from_file (GtkCssProvider *css_provider,
2291 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2292 g_return_val_if_fail (G_IS_FILE (file), FALSE);
2294 gtk_css_provider_reset (css_provider);
2296 return gtk_css_provider_load_internal (css_provider, NULL, file, NULL, 0, error);
2300 * gtk_css_provider_load_from_path:
2301 * @css_provider: a #GtkCssProvider
2302 * @path: the path of a filename to load, in the GLib filename encoding
2303 * @error: (out) (allow-none): return location for a #GError, or %NULL
2305 * Loads the data contained in @path into @css_provider, making it clear
2306 * any previously loaded information.
2308 * Returns: %TRUE if the data could be loaded.
2311 gtk_css_provider_load_from_path (GtkCssProvider *css_provider,
2318 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2319 g_return_val_if_fail (path != NULL, FALSE);
2321 file = g_file_new_for_path (path);
2323 result = gtk_css_provider_load_from_file (css_provider, file, error);
2325 g_object_unref (file);
2331 * gtk_css_provider_get_default:
2333 * Returns the provider containing the style settings used as a
2334 * fallback for all widgets.
2336 * Returns: (transfer none): The provider used for fallback styling.
2337 * This memory is owned by GTK+, and you must not free it.
2340 gtk_css_provider_get_default (void)
2342 static GtkCssProvider *provider;
2344 if (G_UNLIKELY (!provider))
2347 "@define-color fg_color #000; \n"
2348 "@define-color bg_color #dcdad5; \n"
2349 "@define-color text_color #000; \n"
2350 "@define-color base_color #fff; \n"
2351 "@define-color selected_bg_color #4b6983; \n"
2352 "@define-color selected_fg_color #fff; \n"
2353 "@define-color tooltip_bg_color #eee1b3; \n"
2354 "@define-color tooltip_fg_color #000; \n"
2355 "@define-color placeholder_text_color #808080; \n"
2357 "@define-color info_fg_color rgb (181, 171, 156);\n"
2358 "@define-color info_bg_color rgb (252, 252, 189);\n"
2359 "@define-color warning_fg_color rgb (173, 120, 41);\n"
2360 "@define-color warning_bg_color rgb (250, 173, 61);\n"
2361 "@define-color question_fg_color rgb (97, 122, 214);\n"
2362 "@define-color question_bg_color rgb (138, 173, 212);\n"
2363 "@define-color error_fg_color rgb (166, 38, 38);\n"
2364 "@define-color error_bg_color rgb (237, 54, 54);\n"
2367 " background-color: @bg_color;\n"
2368 " color: @fg_color;\n"
2369 " border-color: shade (@bg_color, 0.6);\n"
2371 " border-width: 0;\n"
2375 " background-color: shade (@bg_color, 1.05);\n"
2376 " color: shade (@fg_color, 1.3);\n"
2380 " background-color: @selected_bg_color;\n"
2381 " color: @selected_fg_color;\n"
2384 ".expander, GtkTreeView.view.expander {\n"
2388 ".expander:prelight,\n"
2389 "GtkTreeView.view.expander:selected:prelight {\n"
2390 " color: @text_color;\n"
2393 ".expander:active {\n"
2394 " transition: 200ms linear;\n"
2398 " border-color: shade (@bg_color, 0.7);\n"
2399 " background-color: shade (@bg_color, 0.9);\n"
2400 " color: shade (@bg_color, 0.7);\n"
2404 " border-width: 0;\n"
2405 " border-radius: 0;\n"
2406 " background-color: @base_color;\n"
2407 " color: @text_color;\n"
2409 ".view:selected {\n"
2410 " background-color: shade (@bg_color, 0.9);\n"
2411 " color: @fg_color;\n"
2414 ".view:selected:focused {\n"
2415 " background-color: @selected_bg_color;\n"
2416 " color: @selected_fg_color;\n"
2419 ".view column:sorted row,\n"
2420 ".view column:sorted row:prelight {\n"
2421 " background-color: shade (@bg_color, 0.85);\n"
2424 ".view column:sorted row:nth-child(odd),\n"
2425 ".view column:sorted row:nth-child(odd):prelight {\n"
2426 " background-color: shade (@bg_color, 0.8);\n"
2430 ".view row:prelight {\n"
2431 " background-color: @base_color;\n"
2432 " color: @text_color;\n"
2435 ".view row:nth-child(odd),\n"
2436 ".view row:nth-child(odd):prelight {\n"
2437 " background-color: shade (@base_color, 0.93); \n"
2440 ".view row:selected:focused {\n"
2441 " background-color: @selected_bg_color;\n"
2444 ".view row:selected {\n"
2445 " background-color: darker (@bg_color);\n"
2446 " color: @selected_fg_color;\n"
2449 ".view.cell.trough,\n"
2450 ".view.cell.trough:hover,\n"
2451 ".view.cell.trough:selected,\n"
2452 ".view.cell.trough:selected:focused {\n"
2453 " background-color: @bg_color;\n"
2454 " color: @fg_color;\n"
2457 ".view.cell.progressbar,\n"
2458 ".view.cell.progressbar:hover,\n"
2459 ".view.cell.progressbar:selected,\n"
2460 ".view.cell.progressbar:selected:focused {\n"
2461 " background-color: @selected_bg_color;\n"
2462 " color: @selected_fg_color;\n"
2466 " background-color: alpha (@fg_color, 0.25);\n"
2467 " border-color: @fg_color;\n"
2468 " border-style: solid;\n"
2469 " border-width: 1;\n"
2473 " background-color: @tooltip_bg_color; \n"
2474 " color: @tooltip_fg_color; \n"
2475 " border-color: @tooltip_fg_color; \n"
2476 " border-width: 1;\n"
2477 " border-style: solid;\n"
2482 " border-style: outset; \n"
2483 " border-width: 2; \n"
2486 ".button:active {\n"
2487 " background-color: shade (@bg_color, 0.7);\n"
2488 " border-style: inset; \n"
2491 ".button:prelight,\n"
2492 ".slider:prelight {\n"
2493 " background-color: @selected_bg_color;\n"
2494 " color: @selected_fg_color;\n"
2495 " border-color: shade (@selected_bg_color, 0.7);\n"
2499 " background-color: darker (@bg_color);\n"
2500 " border-style: inset;\n"
2501 " border-width: 1;\n"
2506 " border-style: inset;\n"
2507 " border-width: 2;\n"
2508 " background-color: @base_color;\n"
2509 " color: @text_color;\n"
2512 ".entry:insensitive {\n"
2513 " background-color: shade (@base_color, 0.9);\n"
2514 " color: shade (@base_color, 0.7);\n"
2517 " background-color: #c4c2bd;\n"
2522 ".entry.progressbar, \n"
2523 ".cell.progressbar {\n"
2524 " background-color: @selected_bg_color;\n"
2525 " border-color: shade (@selected_bg_color, 0.7);\n"
2526 " color: @selected_fg_color;\n"
2527 " border-style: outset;\n"
2528 " border-width: 1;\n"
2531 "GtkCheckButton:hover,\n"
2532 "GtkCheckButton:selected,\n"
2533 "GtkRadioButton:hover,\n"
2534 "GtkRadioButton:selected {\n"
2535 " background-color: shade (@bg_color, 1.05);\n"
2539 ".cell.check, .cell.radio,\n"
2540 ".cell.check:hover, .cell.radio:hover {\n"
2541 " border-style: solid;\n"
2542 " border-width: 1;\n"
2543 " background-color: @base_color;\n"
2544 " border-color: @fg_color;\n"
2547 ".check:active, .radio:active,\n"
2548 ".check:hover, .radio:hover {\n"
2549 " background-color: @base_color;\n"
2550 " border-color: @fg_color;\n"
2551 " color: @text_color;\n"
2554 ".check:selected, .radio:selected {\n"
2555 " background-color: darker (@bg_color);\n"
2556 " color: @selected_fg_color;\n"
2557 " border-color: @selected_fg_color;\n"
2560 ".check:selected:focused, .radio:selected:focused {\n"
2561 " background-color: @selected_bg_color;\n"
2564 ".menu.check, .menu.radio {\n"
2565 " color: @fg_color;\n"
2566 " border-style: none;\n"
2567 " border-width: 0;\n"
2571 " border-style: outset;\n"
2572 " border-width: 1;\n"
2576 " border-style: inset;\n"
2577 " border-width: 2;\n"
2581 " border-style: outset;\n"
2582 " border-width: 1;\n"
2586 " border-style: inset;\n"
2587 " border-width: 1;\n"
2590 "GtkScrolledWindow.frame {\n"
2597 " border-style: outset;\n"
2598 " border-width: 1;\n"
2603 ".menu.check:hover,\n"
2604 ".menu.radio:hover {\n"
2605 " background-color: @selected_bg_color;\n"
2606 " color: @selected_fg_color;\n"
2609 "GtkSpinButton.button {\n"
2610 " border-width: 1;\n"
2613 ".scale.slider:hover,\n"
2614 "GtkSpinButton.button:hover {\n"
2615 " background-color: shade (@bg_color, 1.05);\n"
2616 " border-color: shade (@bg_color, 0.8);\n"
2619 "GtkSwitch.trough:active {\n"
2620 " background-color: @selected_bg_color;\n"
2621 " color: @selected_fg_color;\n"
2624 "GtkToggleButton.button:inconsistent {\n"
2625 " border-style: outset;\n"
2626 " border-width: 1px;\n"
2627 " background-color: shade (@bg_color, 0.9);\n"
2628 " border-color: shade (@bg_color, 0.7);\n"
2631 "GtkLabel:selected {\n"
2632 " background-color: shade (@bg_color, 0.9);\n"
2635 "GtkLabel:selected:focused {\n"
2636 " background-color: @selected_bg_color;\n"
2639 ".spinner:active {\n"
2640 " transition: 750ms linear loop;\n"
2644 " background-color: @info_bg_color;\n"
2645 " color: @info_fg_color;\n"
2649 " background-color: @warning_bg_color;\n"
2650 " color: @warning_fg_color;\n"
2654 " background-color: @question_bg_color;\n"
2655 " color: @question_fg_color;\n"
2659 " background-color: @error_bg_color;\n"
2660 " color: @error_fg_color;\n"
2664 " background-color: @selected_bg_color;\n"
2665 " color: @selected_fg_color;\n"
2668 ".light-area-focus {\n"
2672 ".dark-area-focus {\n"
2675 "GtkCalendar.view {\n"
2676 " border-width: 1;\n"
2677 " border-style: inset;\n"
2681 "GtkCalendar.view:inconsistent {\n"
2682 " color: darker (@bg_color);\n"
2685 "GtkCalendar.header {\n"
2686 " background-color: @bg_color;\n"
2687 " border-style: outset;\n"
2688 " border-width: 2;\n"
2691 "GtkCalendar.highlight {\n"
2692 " border-width: 0;\n"
2695 "GtkCalendar.button {\n"
2696 " background-color: @bg_color;\n"
2699 "GtkCalendar.button:hover {\n"
2700 " background-color: lighter (@bg_color);\n"
2701 " color: @fg_color;\n"
2705 " border-width: 0;\n"
2710 provider = gtk_css_provider_new ();
2711 if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
2713 g_error ("Failed to load the internal default CSS.");
2721 _gtk_css_provider_get_theme_dir (void)
2726 var = g_getenv ("GTK_DATA_PREFIX");
2729 path = g_build_filename (var, "share", "themes", NULL);
2731 path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
2737 * gtk_css_provider_get_named:
2738 * @name: A theme name
2739 * @variant: (allow-none): variant to load, for example, "dark", or
2740 * %NULL for the default
2742 * Loads a theme from the usual theme paths
2744 * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
2745 * This memory is owned by GTK+, and you must not free it.
2748 gtk_css_provider_get_named (const gchar *name,
2749 const gchar *variant)
2751 static GHashTable *themes = NULL;
2752 GtkCssProvider *provider;
2755 if (G_UNLIKELY (!themes))
2756 themes = g_hash_table_new (g_str_hash, g_str_equal);
2758 if (variant == NULL)
2759 key = (gchar *)name;
2761 key = g_strconcat (name, "-", variant, NULL);
2763 provider = g_hash_table_lookup (themes, key);
2767 const gchar *home_dir;
2768 gchar *subpath, *path = NULL;
2771 subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
2773 subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
2775 /* First look in the users home directory
2777 home_dir = g_get_home_dir ();
2780 path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
2782 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2793 theme_dir = _gtk_css_provider_get_theme_dir ();
2794 path = g_build_filename (theme_dir, name, subpath, NULL);
2797 if (!g_file_test (path, G_FILE_TEST_EXISTS))
2810 provider = gtk_css_provider_new ();
2812 if (!gtk_css_provider_load_from_path (provider, path, &error))
2814 g_warning ("Could not load named theme \"%s\": %s", name, error->message);
2815 g_error_free (error);
2817 g_object_unref (provider);
2821 g_hash_table_insert (themes, g_strdup (key), provider);
2834 compare_pspecs (gconstpointer a, gconstpointer b)
2836 return strcmp (((const GParamSpec *) a)->name, ((const GParamSpec *) b)->name);
2840 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
2846 _gtk_css_selector_print (ruleset->selector, str);
2848 g_string_append (str, " {\n");
2852 keys = g_hash_table_get_keys (ruleset->style);
2853 /* so the output is identical for identical selector styles */
2854 keys = g_list_sort (keys, compare_pspecs);
2856 for (walk = keys; walk; walk = walk->next)
2858 GParamSpec *pspec = walk->data;
2859 const GValue *value = g_hash_table_lookup (ruleset->style, pspec);
2861 g_string_append (str, " ");
2862 g_string_append (str, pspec->name);
2863 g_string_append (str, ": ");
2864 s = _gtk_css_value_to_string (value);
2865 g_string_append (str, s);
2867 g_string_append (str, ";\n");
2873 if (ruleset->widget_style)
2875 keys = g_hash_table_get_keys (ruleset->widget_style);
2876 /* so the output is identical for identical selector styles */
2877 keys = g_list_sort (keys, (GCompareFunc) strcmp);
2879 for (walk = keys; walk; walk = walk->next)
2881 const char *name = walk->data;
2882 const GValue *value = g_hash_table_lookup (ruleset->style, (gpointer) name);
2884 g_string_append (str, " ");
2885 g_string_append (str, name);
2886 g_string_append (str, ": ");
2887 s = _gtk_css_value_to_string (value);
2888 g_string_append (str, s);
2890 g_string_append (str, ";\n");
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.
2942 gtk_css_provider_to_string (GtkCssProvider *provider)
2944 GtkCssProviderPrivate *priv;
2948 g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
2950 priv = provider->priv;
2952 str = g_string_new ("");
2954 gtk_css_provider_print_colors (priv->symbolic_colors, str);
2956 for (i = 0; i < priv->rulesets->len; i++)
2959 g_string_append (str, "\n");
2960 gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
2963 return g_string_free (str, FALSE);