]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssprovider.c
Add optional colord support to the CUPS print module
[~andy/gtk] / gtk / gtkcssprovider.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <cairo-gobject.h>
27
28 #include "gtkcssproviderprivate.h"
29
30 #include "gtkcssparserprivate.h"
31 #include "gtkcssselectorprivate.h"
32 #include "gtksymboliccolor.h"
33 #include "gtkstyleprovider.h"
34 #include "gtkstylecontextprivate.h"
35 #include "gtkstylepropertiesprivate.h"
36 #include "gtkstylepropertyprivate.h"
37 #include "gtkbindings.h"
38 #include "gtkmarshalers.h"
39 #include "gtkprivate.h"
40 #include "gtkintl.h"
41
42 /**
43  * SECTION:gtkcssprovider
44  * @Short_description: CSS-like styling for widgets
45  * @Title: GtkCssProvider
46  * @See_also: #GtkStyleContext, #GtkStyleProvider
47  *
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.
51  *
52  * <refsect2 id="gtkcssprovider-files">
53  * <title>Default files</title>
54  * <para>
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>,
62  * falling back to
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.
68  * </para>
69  * </refsect2>
70  * <refsect2 id="gtkcssprovider-stylesheets">
71  * <title>Style sheets</title>
72  * <para>
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
75  * by whitespace.
76  * </para>
77  * <para>
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.
83  * </para>
84  * <example><title>A rule set with two selectors</title>
85  * <programlisting language="text">
86  * GtkButton, GtkEntry {
87  *     color: &num;ff00ea;
88  *     font: Comic Sans 12
89  * }
90  * </programlisting>
91  * </example>
92  * </refsect2>
93  * <refsect2 id="gtkcssprovider-selectors">
94  * <title>Selectors</title>
95  * <para>
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  * '&num;' character. The '*' character represents the so-called universal
100  * selector, which matches any widget.
101  * </para>
102  * <para>
103  * To express more complicated situations, selectors can be combined in
104  * various ways:
105  * <itemizedlist>
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&num;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>
119  * </itemizedlist>
120  * </para>
121  * <example>
122  * <title>Widget classes and names in selectors</title>
123  * <programlisting language="text">
124  * /&ast; Theme labels that are descendants of a window &ast;/
125  * GtkWindow GtkLabel {
126  *     background-color: &num;898989
127  * }
128  *
129  * /&ast; Theme notebooks, and anything that's within these &ast;/
130  * GtkNotebook {
131  *     background-color: &num;a939f0
132  * }
133  *
134  * /&ast; Theme combo boxes, and entries that
135  *  are direct children of a notebook &ast;/
136  * GtkComboBox,
137  * GtkNotebook > GtkEntry {
138  *     color: @fg_color;
139  *     background-color: &num;1209a2
140  * }
141  *
142  * /&ast; Theme any widget within a GtkBin &ast;/
143  * GtkBin * {
144  *     font: Sans 20
145  * }
146  *
147  * /&ast; Theme a label named title-label &ast;/
148  * GtkLabel&num;title-label {
149  *     font: Sans 15
150  * }
151  *
152  * /&ast; Theme any widget named main-entry &ast;/
153  * &num;main-entry {
154  *     background-color: &num;f0a810
155  * }
156  * </programlisting>
157  * </example>
158  * <para>
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 '.'
161  * character.
162  * </para>
163  * <para>
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.
167  * </para>
168  * <para>
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).
173  * </para>
174  * <example>
175  * <title>Style classes in selectors</title>
176  * <programlisting language="text">
177  * /&ast; Theme all widgets defining the class entry &ast;/
178  * .entry {
179  *     color: &num;39f1f9;
180  * }
181  *
182  * /&ast; Theme spinbuttons' entry &ast;/
183  * GtkSpinButton.entry {
184  *     color: &num;900185
185  * }
186  * </programlisting>
187  * </example>
188  * <para>
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.
193  * </para>
194  * <para>
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
200  * ':' character.
201  * </para>
202  * <para>
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.
207  * </para>
208  * <example>
209  * <title>Regions in selectors</title>
210  * <programlisting language="text">
211  * /&ast; Theme any label within a notebook &ast;/
212  * GtkNotebook GtkLabel {
213  *     color: &num;f90192;
214  * }
215  *
216  * /&ast; Theme labels within notebook tabs &ast;/
217  * GtkNotebook tab GtkLabel {
218  *     color: &num;703910;
219  * }
220  *
221  * /&ast; Theme labels in the any first notebook
222  *  tab, both selectors are equivalent &ast;/
223  * GtkNotebook tab:nth-child(first) GtkLabel,
224  * GtkNotebook tab:first-child GtkLabel {
225  *     color: &num;89d012;
226  * }
227  * </programlisting>
228  * </example>
229  * <para>
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
234  * and :inconsistent.
235  * </para>
236  * <example>
237  * <title>Styling specific widget states</title>
238  * <programlisting language="text">
239  * /&ast; Theme active (pressed) buttons &ast;/
240  * GtkButton:active {
241  *     background-color: &num;0274d9;
242  * }
243  *
244  * /&ast; Theme buttons with the mouse pointer on it,
245  *    both are equivalent &ast;/
246  * GtkButton:hover,
247  * GtkButton:prelight {
248  *     background-color: &num;3085a9;
249  * }
250  *
251  * /&ast; Theme insensitive widgets, both are equivalent &ast;/
252  * :insensitive,
253  * *:insensitive {
254  *     background-color: &num;320a91;
255  * }
256  *
257  * /&ast; Theme selection colors in entries &ast;/
258  * GtkEntry:selected {
259  *     background-color: &num;56f9a0;
260  * }
261  *
262  * /&ast; Theme focused labels &ast;/
263  * GtkLabel:focused {
264  *     background-color: &num;b4940f;
265  * }
266  *
267  * /&ast; Theme inconsistent checkbuttons &ast;/
268  * GtkCheckButton:inconsistent {
269  *     background-color: &num;20395a;
270  * }
271  * </programlisting>
272  * </example>
273  * <para>
274  * Widget state pseudoclasses may only apply to the last element
275  * in a selector.
276  * </para>
277  * <para>
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
281  * over the others.
282  * </para>
283  * </refsect2>
284  * <refsect2 id="gtkcssprovider-rules">
285  * <title>&commat; Rules</title>
286  * <para>
287  * GTK+'s CSS supports the &commat;import rule, in order to load another
288  * CSS style sheet in addition to the currently parsed one.
289  * </para>
290  * <example>
291  * <title>Using the &commat;import rule</title>
292  * <programlisting language="text">
293  * &commat;import url ("path/to/common.css");
294  * </programlisting>
295  * </example>
296  * <para id="css-binding-set">
297  * In order to extend key bindings affecting different widgets, GTK+
298  * supports the &commat;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.
302  * </para>
303  * <para>
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.
308  * </para>
309  * <example>
310  * <title>Using the &commat;binding rule</title>
311  * <programlisting language="text">
312  * &commat;binding-set binding-set1 {
313  *   bind "&lt;alt&gt;Left" { "move-cursor" (visual-positions, -3, 0) };
314  *   unbind "End";
315  * };
316  *
317  * &commat;binding-set binding-set2 {
318  *   bind "&lt;alt&gt;Right" { "move-cursor" (visual-positions, 3, 0) };
319  *   bind "&lt;alt&gt;KP_space" { "delete-from-cursor" (whitespace, 1)
320  *                          "insert-at-cursor" (" ") };
321  * };
322  *
323  * GtkEntry {
324  *   gtk-key-bindings: binding-set1, binding-set2;
325  * }
326  * </programlisting>
327  * </example>
328  * <para>
329  * GTK+ also supports an additional &commat;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.
333  * </para>
334  * <example>
335  * <title>Defining colors</title>
336  * <programlisting language="text">
337  * &commat;define-color bg_color &num;f9a039;
338  *
339  * &ast; {
340  *     background-color: &commat;bg_color;
341  * }
342  * </programlisting>
343  * </example>
344  * </refsect2>
345  * <refsect2 id="gtkcssprovider-symbolic-colors">
346  * <title>Symbolic colors</title>
347  * <para>
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
351  * colors.
352  * </para>
353  * <example>
354  * <title>Using symbolic colors</title>
355  * <programlisting language="text">
356  * &commat;define-color entry-color shade (&commat;bg_color, 0.7);
357  *
358  * GtkEntry {
359  *     background-color: @entry-color;
360  * }
361  *
362  * GtkEntry:focused {
363  *     background-color: mix (&commat;entry-color,
364  *                            shade (&num;fff, 0.5),
365  *                            0.8);
366  * }
367  * </programlisting>
368  * </example>
369  * <para>
370  *   The various ways to express colors in GTK+ CSS are:
371  * </para>
372  * <informaltable>
373  *   <tgroup cols="3">
374  *     <thead>
375  *       <row>
376  *         <entry>Syntax</entry>
377  *         <entry>Explanation</entry>
378  *         <entry>Examples</entry>
379  *       </row>
380  *     </thead>
381  *     <tbody>
382  *       <row>
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>
388  *       </row>
389  *       <row>
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>
394  *       </row>
395  *       <row>
396  *         <entry>&num;@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>&num;ff12ab
401  * &num;f0c</literallayout></entry>
402  *       </row>
403  *       <row>
404  *         <entry>&commat;name</entry>
405  *         <entry>Reference to a color that has been defined with
406  *                &commat;define-color
407  *         </entry>
408  *         <entry>&commat;bg_color</entry>
409  *       </row>
410  *       <row>
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(&num;ff1e0a, &commat;bg_color, 0.8)</literallayout></entry>
415  *       </row>
416  *       <row>
417  *         <entry>shade(@color, @f)</entry>
418  *         <entry>A lighter or darker variant of @color. @f is a
419  *                floating point number.
420  *         </entry>
421  *         <entry>shade(&commat;fg_color, 0.5)</entry>
422  *       </row>
423  *       <row>
424  *         <entry>lighter(@color)</entry>
425  *         <entry>A lighter variant of @color</entry>
426  *       </row>
427  *       <row>
428  *         <entry>darker(@color)</entry>
429  *         <entry>A darker variant of @color</entry>
430  *       </row>
431  *     </tbody>
432  *   </tgroup>
433  * </informaltable>
434  * </refsect2>
435  * <refsect2 id="gtkcssprovider-gradients">
436  * <title>Gradients</title>
437  * <para>
438  * Linear or radial Gradients can be used as background images.
439  * </para>
440  * <para>
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.
455  * </para>
456  * <example>
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(&commat;yellow), to(&commat;blue))</literallayout></para>
463  * </example>
464  * <example>
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,
469  *                0 0, 0 1,
470  *                color-stop(0, &commat;yellow),
471  *                color-stop(0.2, &commat;blue),
472  *                color-stop(1, &num;0f0))</literallayout></para>
473  * </example>
474  * <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
477  * syntax
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.
485  * </para>
486  * <example>
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,
491  *                center center, 0,
492  *                center center, 1,
493  *                from(&commat;yellow), to(&commat;green))</literallayout></para>
494  * </example>
495  * <example>
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,
500  *                0.4 0.4, 0.1,
501  *                0.6 0.6, 0.7,
502  *                color-stop (0, &num;f00),
503  *                color-stop (0.1, &num;a0f),
504  *                color-stop (0.2, &commat;yellow),
505  *                color-stop (1, &commat;green))</literallayout></para>
506  * </example>
507  * </refsect2>
508  * <refsect2 id="gtkcssprovider-shadows">
509  * <title>Text shadow</title>
510  * <para>
511  * A shadow list can be applied to text or symbolic icons, using the CSS3
512  * text-shadow syntax, as defined in
513  * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
514  * </para>
515  * <para>
516  * A text shadow is specified using the syntax
517  * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
518  * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
519  * parameters. The optional blur radius is parsed, but it is currently not rendered by
520  * the GTK+ theming engine.
521  * </para>
522  * <para>
523  * To set multiple shadows on an element, you can specify a comma-separated list
524  * of shadow elements in the text-shadow property. Shadows are always rendered
525  * front-back, i.e. the first shadow specified is on top of the others. Shadows
526  * can thus overlay each other, but they can never overlay the text itself,
527  * which is always rendered on top of the shadow layer.
528  * </para>
529  * </refsect2>
530  * <refsect2 id="gtkcssprovider-border-image">
531  * <title>Border images</title>
532  * <para>
533  * Images and gradients can also be used in slices for the purpose of creating
534  * scalable borders.
535  * For more information, see the CSS3 documentation for the border-image property,
536  * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
537  * </para>
538  * <inlinegraphic fileref="slices.png" format="PNG"/>
539  * <para>
540  * The parameters of the slicing process are controlled by
541  * three separate properties. Note that you can use the
542  * <literallayout>border-image</literallayout> shorthand property
543  * to set values for the three properties at the same time.
544  * </para>
545  * <para>
546  * <literallayout>border-image-source: url(@path)
547  * (or border-image-source: -gtk-gradient(...))</literallayout>:
548  * Specifies the source of the border image, and it can either
549  * be an URL or a gradient (see above).
550  * </para>
551  * <para>
552  * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
553  * The sizes specified by the @top, @right, @bottom and @left parameters
554  * are the offsets, in pixels, from the relevant edge where the image
555  * should be "cut off" to build the slices used for the rendering
556  * of the border.
557  * </para>
558  * <para>
559  * <literallayout>border-image-repeat: [stretch|repeat|round|space] ? 
560  * [stretch|repeat|round|space]</literallayout>
561  * Specifies how the image slices should be rendered in the area
562  * outlined by border-width.
563  * The default (stretch) is to resize the slice to fill in the whole 
564  * allocated area.
565  * If the value of this property is 'repeat', the image slice
566  * will be tiled to fill the area.
567  * If the value of this property is 'round', the image slice will
568  * be tiled to fill the area, and scaled to fit it exactly
569  * a whole number of times.
570  * If the value of this property is 'space', the image slice will
571  * be tiled to fill the area, and if it doesn't fit it exactly a whole
572  * number of times, the extra space is distributed as padding around 
573  * the slices.
574  * If two options are specified, the first one affects
575  * the horizontal behaviour and the second one the vertical behaviour.
576  * If only one option is specified, it affects both.
577  * </para>
578  * <example>
579  * <title>A border image</title>
580  * <inlinegraphic fileref="border1.png" format="PNG"/>
581  * <para>This border image was specified with
582  * <literallayout>url("gradient1.png") 10 10 10 10</literallayout>
583  * </para>
584  * </example>
585  * <example>
586  * <title>A repeating border image</title>
587  * <inlinegraphic fileref="border2.png" format="PNG"/>
588  * <para>This border image was specified with
589  * <literallayout>url("gradient1.png") 10 10 10 10 repeat</literallayout>
590  * </para>
591  * </example>
592  * <example>
593  * <title>A stretched border image</title>
594  * <inlinegraphic fileref="border3.png" format="PNG"/>
595  * <para>This border image was specified with
596  * <literallayout>url("gradient1.png") 10 10 10 10 stretch</literallayout>
597  * </para>
598  * </example>
599  * </refsect2>
600  * <refsect2 id="gtkcssprovider-transitions">
601  * <para>Styles can specify transitions that will be used to create a gradual
602  * change in the appearance when a widget state changes. The following
603  * syntax is used to specify transitions:
604  * <literallayout>@duration [s|ms] [linear|ease|ease-in|ease-out|ease-in-out] [loop]?</literallayout>
605  * The @duration is the amount of time that the animation will take for
606  * a complete cycle from start to end. If the loop option is given, the
607  * animation will be repated until the state changes again.
608  * The option after the duration determines the transition function from a
609  * small set of predefined functions.
610  * <figure><title>Linear transition</title>
611  * <graphic fileref="linear.png" format="PNG"/>
612  * </figure>
613  * <figure><title>Ease transition</title>
614  * <graphic fileref="ease.png" format="PNG"/>
615  * </figure>
616  * <figure><title>Ease-in-out transition</title>
617  * <graphic fileref="ease-in-out.png" format="PNG"/>
618  * </figure>
619  * <figure><title>Ease-in transition</title>
620  * <graphic fileref="ease-in.png" format="PNG"/>
621  * </figure>
622  * <figure><title>Ease-out transition</title>
623  * <graphic fileref="ease-out.png" format="PNG"/>
624  * </figure>
625  * </para>
626  * </refsect2>
627  * <refsect2 id="gtkcssprovider-properties">
628  * <title>Supported properties</title>
629  * <para>
630  * Properties are the part that differ the most to common CSS,
631  * not all properties are supported (some are planned to be
632  * supported eventually, some others are meaningless or don't
633  * map intuitively in a widget based environment).
634  * </para>
635  * <para>
636  * There is also a difference in shorthand properties, for
637  * example in common CSS it is fine to define a font through
638  * the different @font-family, @font-style, @font-size
639  * properties, meanwhile in GTK+'s CSS only the canonical
640  * @font property is supported.
641  * </para>
642  * <para>
643  * The currently supported properties are:
644  * </para>
645  * <informaltable>
646  *   <tgroup cols="4">
647  *     <thead>
648  *       <row>
649  *         <entry>Property name</entry>
650  *         <entry>Syntax</entry>
651  *         <entry>Maps to</entry>
652  *         <entry>Examples</entry>
653  *       </row>
654  *     </thead>
655  *     <tbody>
656  *       <row>
657  *         <entry>engine</entry>
658  *         <entry>engine-name</entry>
659  *         <entry>#GtkThemingEngine</entry>
660  *         <entry>engine: clearlooks;
661  *  engine: none; /&ast; use the default (i.e. builtin) engine) &ast;/ </entry>
662  *       </row>
663  *       <row>
664  *         <entry>background-color</entry>
665  *         <entry morerows="2">color (see above)</entry>
666  *         <entry morerows="2">#GdkRGBA</entry>
667  *         <entry morerows="2"><literallayout>background-color: &num;fff;
668  * color: &amp;color1;
669  * background-color: shade (&amp;color1, 0.5);
670  * color: mix (&amp;color1, &num;f0f, 0.8);</literallayout>
671  *         </entry>
672  *       </row>
673  *       <row>
674  *         <entry>color</entry>
675  *       </row>
676  *       <row>
677  *         <entry>border-color</entry>
678  *       </row>
679  *       <row>
680  *         <entry>font</entry>
681  *         <entry>@family [@style] [@size]</entry>
682  *         <entry>#PangoFontDescription</entry>
683  *         <entry>font: Sans 15;</entry>
684  *       </row>
685  *       <row>
686  *         <entry>margin-top</entry>
687  *         <entry>integer</entry>
688  *         <entry>#gint</entry>
689  *         <entry>margin-top: 0;</entry>
690  *       </row>
691  *       <row>
692  *         <entry>margin-left</entry>
693  *         <entry>integer</entry>
694  *         <entry>#gint</entry>
695  *         <entry>margin-left: 1;</entry>
696  *       </row>
697  *       <row>
698  *         <entry>margin-bottom</entry>
699  *         <entry>integer</entry>
700  *         <entry>#gint</entry>
701  *         <entry>margin-bottom: 2;</entry>
702  *       </row>
703  *       <row>
704  *         <entry>margin-right</entry>
705  *         <entry>integer</entry>
706  *         <entry>#gint</entry>
707  *         <entry>margin-right: 4;</entry>
708  *       </row>
709  *       <row>
710  *         <entry>margin</entry>
711  *         <entry morerows="1"><literallayout>@width
712  * @vertical_width @horizontal_width
713  * @top_width @horizontal_width @bottom_width
714  * @top_width @right_width @bottom_width @left_width</literallayout>
715  *         </entry>
716  *         <entry morerows="1">#GtkBorder</entry>
717  *         <entry morerows="1"><literallayout>margin: 5;
718  * margin: 5 10;
719  * margin: 5 10 3;
720  * margin: 5 10 3 5;</literallayout>
721  *         </entry>
722  *       </row>
723  *       <row>
724  *         <entry>padding-top</entry>
725  *         <entry>integer</entry>
726  *         <entry>#gint</entry>
727  *         <entry>padding-top: 5;</entry>
728  *       </row>
729  *       <row>
730  *         <entry>padding-left</entry>
731  *         <entry>integer</entry>
732  *         <entry>#gint</entry>
733  *         <entry>padding-left: 5;</entry>
734  *       </row>
735  *       <row>
736  *         <entry>padding-bottom</entry>
737  *         <entry>integer</entry>
738  *         <entry>#gint</entry>
739  *         <entry>padding-bottom: 5;</entry>
740  *       </row>
741  *       <row>
742  *         <entry>padding-right</entry>
743  *         <entry>integer</entry>
744  *         <entry>#gint</entry>
745  *         <entry>padding-right: 5;</entry>
746  *       </row>
747  *       <row>
748  *         <entry>padding</entry>
749  *       </row>
750  *       <row>
751  *         <entry>background-image</entry>
752  *         <entry><literallayout>gradient (see above) or
753  * url(@path)</literallayout></entry>
754  *         <entry>#cairo_pattern_t</entry>
755  *         <entry><literallayout>-gtk-gradient (linear,
756  *                left top, right top,
757  *                from (&num;fff), to (&num;000));
758  * -gtk-gradient (linear, 0.0 0.5, 0.5 1.0,
759  *                from (&num;fff),
760  *                color-stop (0.5, &num;f00),
761  *                to (&num;000));
762  * -gtk-gradient (radial,
763  *                center center, 0.2,
764  *                center center, 0.8,
765  *                color-stop (0.0, &num;fff),
766  *                color-stop (1.0, &num;000));
767  * url ('background.png');</literallayout>
768  *         </entry>
769  *       </row>
770  *       <row>
771  *         <entry>border-top-width</entry>
772  *         <entry>integer</entry>
773  *         <entry>#gint</entry>
774  *         <entry>border-top-width: 5;</entry>
775  *       </row>
776  *       <row>
777  *         <entry>border-left-width</entry>
778  *         <entry>integer</entry>
779  *         <entry>#gint</entry>
780  *         <entry>border-left-width: 5;</entry>
781  *       </row>
782  *       <row>
783  *         <entry>border-bottom-width</entry>
784  *         <entry>integer</entry>
785  *         <entry>#gint</entry>
786  *         <entry>border-bottom-width: 5;</entry>
787  *       </row>
788  *       <row>
789  *         <entry>border-right-width</entry>
790  *         <entry>integer</entry>
791  *         <entry>#gint</entry>
792  *         <entry>border-right-width: 5;</entry>
793  *       </row>
794  *       <row>
795  *         <entry>border-width</entry>
796  *         <entry morerows="1">#GtkBorder</entry>
797  *         <entry morerows="1"><literallayout>border-width: 1;
798  * border-width: 1 2;
799  * border-width: 1 2 3;
800  * border-width: 1 2 3 5;</literallayout>
801  *         </entry>
802  *       </row>
803  *       <row>
804  *         <entry>border-radius</entry>
805  *         <entry>integer</entry>
806  *         <entry>#gint</entry>
807  *         <entry>border-radius: 5;</entry>
808  *       </row>
809  *       <row>
810  *         <entry>border-style</entry>
811  *         <entry>[none|solid|inset|outset]</entry>
812  *         <entry>#GtkBorderStyle</entry>
813  *         <entry>border-style: solid;</entry>
814  *       </row>
815  *       <row>
816  *         <entry>border-image</entry>
817  *         <entry><literallayout>border image (see above)</literallayout></entry>
818  *         <entry>internal use only</entry>
819  *         <entry><literallayout>border-image: url("/path/to/image.png") 3 4 3 4 stretch;
820  * border-image: url("/path/to/image.png") 3 4 4 3 repeat stretch;</literallayout>
821  *         </entry>
822  *       </row>
823  *       <row>
824  *         <entry>text-shadow</entry>
825  *         <entry>shadow list (see above)</entry>
826  *         <entry>#GtkTextShadow</entry>
827  *         <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
828  *       </row>
829  *       <row>
830  *         <entry>transition</entry>
831  *         <entry>transition (see above)</entry>
832  *         <entry>internal use only</entry>
833  *         <entry><literallayout>transition: 150ms ease-in-out;
834  * transition: 1s linear loop;</literallayout>
835  *         </entry>
836  *       </row>
837  *       <row>
838  *         <entry>gtk-key-bindings</entry>
839  *         <entry>binding set name list</entry>
840  *         <entry>internal use only</entry>
841  *         <entry><literallayout>gtk-bindings: binding1, binding2, ...;</literallayout>
842  *         </entry>
843  *       </row>
844  *     </tbody>
845  *   </tgroup>
846  * </informaltable>
847  * <para>
848  * GtkThemingEngines can register their own, engine-specific style properties
849  * with the function gtk_theming_engine_register_property(). These properties
850  * can be set in CSS like other properties, using a name of the form
851  * <literallayout>-<replaceable>namespace</replaceable>-<replaceable>name</replaceable></literallayout>, where <replaceable>namespace</replaceable> is typically
852  * the name of the theming engine, and <replaceable>name</replaceable> is the
853  * name of the property. Style properties that have been registered by widgets
854  * using gtk_widget_class_install_style_property() can also be set in this
855  * way, using the widget class name for <replaceable>namespace</replaceable>.
856  * </para>
857  * <example>
858  * <title>Using engine-specific style properties</title>
859  * <programlisting>
860  * * {
861  *     engine: clearlooks;
862  *     border-radius: 4;
863  *     -GtkPaned-handle-size: 6;
864  *     -clearlooks-colorize-scrollbar: false;
865  * }
866  * </programlisting>
867  * </example>
868  * </refsect2>
869  */
870
871 typedef struct GtkCssRuleset GtkCssRuleset;
872 typedef struct _GtkCssScanner GtkCssScanner;
873 typedef enum ParserScope ParserScope;
874 typedef enum ParserSymbol ParserSymbol;
875
876 struct GtkCssRuleset
877 {
878   GtkCssSelector *selector;
879   GHashTable *widget_style;
880   GHashTable *style;
881
882   guint has_inherit :1;
883 };
884
885 struct _GtkCssScanner
886 {
887   GtkCssProvider *provider;
888   GtkCssParser *parser;
889   GtkCssScanner *parent;
890   GFile *file;
891   GFile *base;
892   GSList *state;
893   GSList *cur_selectors;
894   GHashTable *cur_properties;
895 };
896
897 struct _GtkCssProviderPrivate
898 {
899   GScanner *scanner;
900
901   GHashTable *symbolic_colors;
902
903   GArray *rulesets;
904 };
905
906 enum ParserScope {
907   SCOPE_SELECTOR,
908   SCOPE_PSEUDO_CLASS,
909   SCOPE_NTH_CHILD,
910   SCOPE_DECLARATION,
911   SCOPE_VALUE,
912   SCOPE_BINDING_SET
913 };
914
915 /* Extend GtkStateType, since these
916  * values are also used as symbols
917  */
918 enum ParserSymbol {
919   /* Scope: pseudo-class */
920   SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
921   SYMBOL_FIRST_CHILD,
922   SYMBOL_LAST_CHILD,
923   SYMBOL_SORTED_CHILD,
924
925   /* Scope: nth-child */
926   SYMBOL_NTH_CHILD_EVEN,
927   SYMBOL_NTH_CHILD_ODD,
928   SYMBOL_NTH_CHILD_FIRST,
929   SYMBOL_NTH_CHILD_LAST
930 };
931
932 enum {
933   PARSING_ERROR,
934   LAST_SIGNAL
935 };
936
937 static guint css_provider_signals[LAST_SIGNAL] = { 0 };
938
939 static void gtk_css_provider_finalize (GObject *object);
940 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
941
942 static gboolean
943 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
944                                 GtkCssScanner  *scanner,
945                                 GFile          *file,
946                                 const char     *data,
947                                 gsize           length,
948                                 GError        **error);
949
950 GQuark
951 gtk_css_provider_error_quark (void)
952 {
953   return g_quark_from_static_string ("gtk-css-provider-error-quark");
954 }
955
956 G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
957                         G_IMPLEMENT_INTERFACE (GTK_TYPE_STYLE_PROVIDER,
958                                                gtk_css_style_provider_iface_init));
959
960 static void
961 gtk_css_provider_parsing_error (GtkCssProvider  *provider,
962                                 const gchar     *path,
963                                 guint            line,
964                                 guint            position,
965                                 const GError *   error)
966 {
967   /* Only emit a warning when we have no error handlers. This is our
968    * default handlers. And in this case erroneous CSS files are a bug
969    * and should be fixed.
970    * Note that these warnings can also be triggered by a broken theme
971    * that people installed from some weird location on the internets.
972    */
973   if (!g_signal_has_handler_pending (provider,
974                                      css_provider_signals[PARSING_ERROR],
975                                      0,
976                                      TRUE))
977     {
978       g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
979     }
980 }
981
982 static void
983 gtk_css_provider_class_init (GtkCssProviderClass *klass)
984 {
985   GObjectClass *object_class = G_OBJECT_CLASS (klass);
986
987   /**
988    * GtkCssProvider::parsing-error:
989    * @provider: the provider that had a parsing error
990    * @path: path to the parsed file or %NULL if the file cannot be
991    *   identified or the data was not loaded from a file
992    * @line: line in the file or data or 0 if unknown
993    * @position: offset into the current line or 0 if unknown or the
994    *   whole line is affected
995    * @error: The parsing error
996    *
997    * Signals that a parsing error occured. the @path, @line and @position
998    * describe the actual location of the error as accurately as possible.
999    *
1000    * Parsing errors are never fatal, so the parsing will resume after
1001    * the error. Errors may however cause parts of the given
1002    * data or even all of it to not be parsed at all. So it is a useful idea
1003    * to check that the parsing succeeds by connecting to this signal.
1004    *
1005    * Note that this signal may be emitted at any time as the css provider
1006    * may opt to defer parsing parts or all of the input to a later time
1007    * than when a loading function was called.
1008    */
1009   css_provider_signals[PARSING_ERROR] =
1010     g_signal_new (I_("parsing-error"),
1011                   G_TYPE_FROM_CLASS (object_class),
1012                   G_SIGNAL_RUN_LAST,
1013                   G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
1014                   NULL, NULL,
1015                   _gtk_marshal_VOID__STRING_UINT_UINT_BOXED,
1016                   G_TYPE_NONE, 4,
1017                   G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR);
1018
1019   object_class->finalize = gtk_css_provider_finalize;
1020
1021   klass->parsing_error = gtk_css_provider_parsing_error;
1022
1023   g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
1024 }
1025
1026 static void
1027 gtk_css_provider_take_error_full (GtkCssProvider *provider,
1028                                   GFile          *file,
1029                                   guint           line,
1030                                   guint           position,
1031                                   GError         *error)
1032 {
1033   char *filename;
1034
1035   if (file)
1036     filename = g_file_get_path (file);
1037   else
1038     filename = NULL;
1039
1040   g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
1041                  filename, line, position, error);
1042
1043   g_free (filename);
1044   g_error_free (error);
1045 }
1046
1047 static void
1048 gtk_css_ruleset_init_copy (GtkCssRuleset       *new,
1049                            const GtkCssRuleset *ruleset,
1050                            GtkCssSelector      *selector)
1051 {
1052   memcpy (new, ruleset, sizeof (GtkCssRuleset));
1053
1054   new->selector = selector;
1055   if (new->widget_style)
1056     g_hash_table_ref (new->widget_style);
1057   if (new->style)
1058     g_hash_table_ref (new->style);
1059 }
1060
1061 static void
1062 gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
1063 {
1064   if (ruleset->style)
1065     g_hash_table_unref (ruleset->style);
1066   if (ruleset->widget_style)
1067     g_hash_table_unref (ruleset->widget_style);
1068   if (ruleset->selector)
1069     _gtk_css_selector_free (ruleset->selector);
1070
1071   memset (ruleset, 0, sizeof (GtkCssRuleset));
1072 }
1073
1074 static void
1075 property_value_free (GValue *value)
1076 {
1077   if (G_IS_VALUE (value))
1078     g_value_unset (value);
1079
1080   g_slice_free (GValue, value);
1081 }
1082
1083 static void
1084 gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
1085                            char          *name,
1086                            GValue        *value)
1087 {
1088   if (ruleset->widget_style == NULL)
1089     ruleset->widget_style = g_hash_table_new_full (g_str_hash,
1090                                                    g_str_equal,
1091                                                    (GDestroyNotify) g_free,
1092                                                    (GDestroyNotify) property_value_free);
1093
1094   g_hash_table_insert (ruleset->widget_style, name, value);
1095 }
1096
1097 static void
1098 gtk_css_ruleset_add (GtkCssRuleset          *ruleset,
1099                      const GtkStyleProperty *prop,
1100                      GValue                 *value)
1101 {
1102   if (ruleset->style == NULL)
1103     ruleset->style = g_hash_table_new_full (g_direct_hash,
1104                                             g_direct_equal,
1105                                             NULL,
1106                                             (GDestroyNotify) property_value_free);
1107
1108   if (_gtk_style_property_is_shorthand (prop))
1109     {
1110       GParameter *parameters;
1111       guint i, n_parameters;
1112
1113       parameters = _gtk_style_property_unpack (prop, value, &n_parameters);
1114
1115       for (i = 0; i < n_parameters; i++)
1116         {
1117           const GtkStyleProperty *child;
1118           GValue *value;
1119
1120           child = _gtk_style_property_lookup (parameters[i].name);
1121           value = g_slice_dup (GValue, &parameters[i].value);
1122           gtk_css_ruleset_add (ruleset, child, value);
1123         }
1124       g_free (parameters);
1125       property_value_free (value);
1126       return;
1127     }
1128
1129   ruleset->has_inherit |= _gtk_style_property_is_inherit (prop);
1130   g_hash_table_insert (ruleset->style, (gpointer) prop, value);
1131 }
1132
1133 static gboolean
1134 gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
1135                          GtkWidgetPath *path,
1136                          guint          length)
1137 {
1138   return _gtk_css_selector_matches (ruleset->selector, path, length);
1139 }
1140
1141 static void
1142 gtk_css_scanner_reset (GtkCssScanner *scanner)
1143 {
1144   g_slist_free (scanner->state);
1145   scanner->state = NULL;
1146
1147   g_slist_free_full (scanner->cur_selectors, (GDestroyNotify) _gtk_css_selector_free);
1148   scanner->cur_selectors = NULL;
1149
1150   if (scanner->cur_properties)
1151     g_hash_table_unref (scanner->cur_properties);
1152
1153   scanner ->cur_properties = g_hash_table_new_full (g_str_hash,
1154                                                     g_str_equal,
1155                                                     (GDestroyNotify) g_free,
1156                                                     (GDestroyNotify) property_value_free);
1157 }
1158
1159 static void
1160 gtk_css_scanner_destroy (GtkCssScanner *scanner)
1161 {
1162   gtk_css_scanner_reset (scanner);
1163
1164   g_object_unref (scanner->provider);
1165   if (scanner->file)
1166     g_object_unref (scanner->file);
1167   g_object_unref (scanner->base);
1168   g_hash_table_destroy (scanner->cur_properties);
1169   _gtk_css_parser_free (scanner->parser);
1170
1171   g_slice_free (GtkCssScanner, scanner);
1172 }
1173
1174 static void
1175 gtk_css_scanner_parser_error (GtkCssParser *parser,
1176                               const GError *error,
1177                               gpointer      user_data)
1178 {
1179   GtkCssScanner *scanner = user_data;
1180
1181   gtk_css_provider_take_error_full (scanner->provider,
1182                                     scanner->file,
1183                                     _gtk_css_parser_get_line (scanner->parser),
1184                                     _gtk_css_parser_get_position (scanner->parser),
1185                                     g_error_copy (error));
1186 }
1187
1188 static GtkCssScanner *
1189 gtk_css_scanner_new (GtkCssProvider *provider,
1190                      GtkCssScanner  *parent,
1191                      GFile          *file,
1192                      const gchar    *data,
1193                      gsize           length)
1194 {
1195   GtkCssScanner *scanner;
1196
1197   g_assert (data[length] == 0);
1198
1199   scanner = g_slice_new0 (GtkCssScanner);
1200
1201   g_object_ref (provider);
1202   scanner->provider = provider;
1203   scanner->parent = parent;
1204
1205   if (file)
1206     {
1207       scanner->file = g_object_ref (file);
1208       scanner->base = g_file_get_parent (file);
1209     }
1210   else
1211     {
1212       char *dir = g_get_current_dir ();
1213       scanner->base = g_file_new_for_path (dir);
1214       g_free (dir);
1215     }
1216
1217   scanner->cur_properties = g_hash_table_new_full (g_str_hash,
1218                                                    g_str_equal,
1219                                                    (GDestroyNotify) g_free,
1220                                                    (GDestroyNotify) property_value_free);
1221
1222   scanner->parser = _gtk_css_parser_new (data,
1223                                          gtk_css_scanner_parser_error,
1224                                          scanner);
1225
1226   return scanner;
1227 }
1228
1229 static GFile *
1230 gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
1231 {
1232   return scanner->base;
1233 }
1234
1235 static gboolean
1236 gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
1237                                GFile         *file)
1238 {
1239   while (scanner)
1240     {
1241       if (scanner->file && g_file_equal (scanner->file, file))
1242         return TRUE;
1243
1244       scanner = scanner->parent;
1245     }
1246
1247   return FALSE;
1248 }
1249
1250 static void
1251 gtk_css_provider_init (GtkCssProvider *css_provider)
1252 {
1253   GtkCssProviderPrivate *priv;
1254
1255   priv = css_provider->priv = G_TYPE_INSTANCE_GET_PRIVATE (css_provider,
1256                                                            GTK_TYPE_CSS_PROVIDER,
1257                                                            GtkCssProviderPrivate);
1258
1259   priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
1260
1261   priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
1262                                                  (GDestroyNotify) g_free,
1263                                                  (GDestroyNotify) gtk_symbolic_color_unref);
1264 }
1265
1266 static void
1267 css_provider_dump_symbolic_colors (GtkCssProvider     *css_provider,
1268                                    GtkStyleProperties *props)
1269 {
1270   GtkCssProviderPrivate *priv;
1271   GHashTableIter iter;
1272   gpointer key, value;
1273
1274   priv = css_provider->priv;
1275   g_hash_table_iter_init (&iter, priv->symbolic_colors);
1276
1277   while (g_hash_table_iter_next (&iter, &key, &value))
1278     {
1279       const gchar *name;
1280       GtkSymbolicColor *color;
1281
1282       name = key;
1283       color = value;
1284
1285       gtk_style_properties_map_color (props, name, color);
1286     }
1287 }
1288
1289 static GtkStyleProperties *
1290 gtk_css_provider_get_style (GtkStyleProvider *provider,
1291                             GtkWidgetPath    *path)
1292 {
1293   GtkCssProvider *css_provider;
1294   GtkCssProviderPrivate *priv;
1295   GtkStyleProperties *props;
1296   guint i, l, length;
1297
1298   css_provider = GTK_CSS_PROVIDER (provider);
1299   priv = css_provider->priv;
1300   length = gtk_widget_path_length (path);
1301   props = gtk_style_properties_new ();
1302
1303   css_provider_dump_symbolic_colors (css_provider, props);
1304
1305   for (l = 1; l <= length; l++)
1306     {
1307       for (i = 0; i < priv->rulesets->len; i++)
1308         {
1309           GtkCssRuleset *ruleset;
1310           GHashTableIter iter;
1311           gpointer key, value;
1312
1313           ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1314
1315           if (ruleset->style == NULL)
1316             continue;
1317
1318           if (l < length && (!ruleset->has_inherit || _gtk_css_selector_get_state_flags (ruleset->selector)))
1319             continue;
1320
1321           if (!gtk_css_ruleset_matches (ruleset, path, l))
1322             continue;
1323
1324           g_hash_table_iter_init (&iter, ruleset->style);
1325
1326           while (g_hash_table_iter_next (&iter, &key, &value))
1327             {
1328               GtkStyleProperty *prop = key;
1329
1330               if (l != length && !_gtk_style_property_is_inherit (prop))
1331                 continue;
1332
1333               _gtk_style_properties_set_property_by_property (props,
1334                                                               prop,
1335                                                               _gtk_css_selector_get_state_flags (ruleset->selector),
1336                                                               value);
1337             }
1338         }
1339     }
1340
1341   return props;
1342 }
1343
1344 static void
1345 gtk_css_provider_parser_error (GtkCssParser *parser,
1346                                const GError *error,
1347                                gpointer      user_data)
1348 {
1349   GtkCssProvider *provider = user_data;
1350
1351   gtk_css_provider_take_error_full (provider,
1352                                     NULL,
1353                                     _gtk_css_parser_get_line (parser),
1354                                     _gtk_css_parser_get_position (parser),
1355                                     g_error_copy (error));
1356 }
1357
1358 static gboolean
1359 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
1360                                      GtkWidgetPath    *path,
1361                                      GtkStateFlags     state,
1362                                      GParamSpec       *pspec,
1363                                      GValue           *value)
1364 {
1365   GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
1366   GtkCssProviderPrivate *priv = css_provider->priv;
1367   const GValue *val;
1368   gboolean found = FALSE;
1369   gchar *prop_name;
1370   gint i;
1371
1372   prop_name = g_strdup_printf ("-%s-%s",
1373                                g_type_name (pspec->owner_type),
1374                                pspec->name);
1375
1376   for (i = priv->rulesets->len - 1; i >= 0; i--)
1377     {
1378       GtkCssRuleset *ruleset;
1379       GtkStateFlags selector_state;
1380
1381       ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
1382
1383       if (ruleset->widget_style == NULL)
1384         continue;
1385
1386       if (!gtk_css_ruleset_matches (ruleset, path, gtk_widget_path_length (path)))
1387         continue;
1388
1389       selector_state = _gtk_css_selector_get_state_flags (ruleset->selector);
1390       val = g_hash_table_lookup (ruleset->widget_style, prop_name);
1391
1392       if (val &&
1393           (selector_state == 0 ||
1394            selector_state == state ||
1395            ((selector_state & state) != 0 &&
1396             (selector_state & ~(state)) == 0)))
1397         {
1398           GtkCssParser *parser;
1399
1400           parser = _gtk_css_parser_new (g_value_get_string (val),
1401                                         gtk_css_provider_parser_error,
1402                                         provider);
1403
1404           found = _gtk_style_property_parse_value (NULL,
1405                                                    value,
1406                                                    parser,
1407                                                    NULL);
1408
1409           _gtk_css_parser_free (parser);
1410
1411           if (found)
1412             break;
1413         }
1414     }
1415
1416   g_free (prop_name);
1417
1418   return found;
1419 }
1420
1421 static void
1422 gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface)
1423 {
1424   iface->get_style = gtk_css_provider_get_style;
1425   iface->get_style_property = gtk_css_provider_get_style_property;
1426 }
1427
1428 static void
1429 gtk_css_provider_finalize (GObject *object)
1430 {
1431   GtkCssProvider *css_provider;
1432   GtkCssProviderPrivate *priv;
1433   guint i;
1434
1435   css_provider = GTK_CSS_PROVIDER (object);
1436   priv = css_provider->priv;
1437
1438   for (i = 0; i < priv->rulesets->len; i++)
1439     gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1440
1441   g_array_free (priv->rulesets, TRUE);
1442
1443   if (priv->symbolic_colors)
1444     g_hash_table_destroy (priv->symbolic_colors);
1445
1446   G_OBJECT_CLASS (gtk_css_provider_parent_class)->finalize (object);
1447 }
1448
1449 /**
1450  * gtk_css_provider_new:
1451  *
1452  * Returns a newly created #GtkCssProvider.
1453  *
1454  * Returns: A new #GtkCssProvider
1455  **/
1456 GtkCssProvider *
1457 gtk_css_provider_new (void)
1458 {
1459   return g_object_new (GTK_TYPE_CSS_PROVIDER, NULL);
1460 }
1461
1462 static void
1463 gtk_css_provider_take_error (GtkCssProvider *provider,
1464                              GtkCssScanner  *scanner,
1465                              GError         *error)
1466 {
1467   gtk_css_provider_take_error_full (provider,
1468                                     scanner->file,
1469                                     _gtk_css_parser_get_line (scanner->parser),
1470                                     _gtk_css_parser_get_position (scanner->parser),
1471                                     error);
1472 }
1473
1474 static void
1475 gtk_css_provider_error_literal (GtkCssProvider *provider,
1476                                 GtkCssScanner  *scanner,
1477                                 GQuark          domain,
1478                                 gint            code,
1479                                 const char     *message)
1480 {
1481   gtk_css_provider_take_error (provider,
1482                                scanner,
1483                                g_error_new_literal (domain, code, message));
1484 }
1485
1486 static void
1487 gtk_css_provider_error (GtkCssProvider *provider,
1488                         GtkCssScanner  *scanner,
1489                         GQuark          domain,
1490                         gint            code,
1491                         const char     *format,
1492                         ...)  G_GNUC_PRINTF (5, 6);
1493 static void
1494 gtk_css_provider_error (GtkCssProvider *provider,
1495                         GtkCssScanner  *scanner,
1496                         GQuark          domain,
1497                         gint            code,
1498                         const char     *format,
1499                         ...)
1500 {
1501   GError *error;
1502   va_list args;
1503
1504   va_start (args, format);
1505   error = g_error_new_valist (domain, code, format, args);
1506   va_end (args);
1507
1508   gtk_css_provider_take_error (provider, scanner, error);
1509 }
1510
1511 static void
1512 gtk_css_provider_invalid_token (GtkCssProvider *provider,
1513                                 GtkCssScanner  *scanner,
1514                                 const char     *expected)
1515 {
1516   gtk_css_provider_error (provider,
1517                           scanner,
1518                           GTK_CSS_PROVIDER_ERROR,
1519                           GTK_CSS_PROVIDER_ERROR_SYNTAX,
1520                           "expected a valid %s", expected);
1521 }
1522
1523 static void 
1524 css_provider_commit (GtkCssProvider *css_provider,
1525                      GSList         *selectors,
1526                      GtkCssRuleset  *ruleset)
1527 {
1528   GtkCssProviderPrivate *priv;
1529   GSList *l;
1530
1531   priv = css_provider->priv;
1532
1533   if (ruleset->style == NULL && ruleset->widget_style == NULL)
1534     {
1535       g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
1536       return;
1537     }
1538
1539   for (l = selectors; l; l = l->next)
1540     {
1541       GtkCssRuleset new;
1542
1543       gtk_css_ruleset_init_copy (&new, ruleset, l->data);
1544
1545       g_array_append_val (priv->rulesets, new);
1546     }
1547
1548   g_slist_free (selectors);
1549 }
1550
1551 static void
1552 gtk_css_provider_reset (GtkCssProvider *css_provider)
1553 {
1554   GtkCssProviderPrivate *priv;
1555   guint i;
1556
1557   priv = css_provider->priv;
1558
1559   g_hash_table_remove_all (priv->symbolic_colors);
1560
1561   for (i = 0; i < priv->rulesets->len; i++)
1562     gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
1563   g_array_set_size (priv->rulesets, 0);
1564 }
1565
1566 static void
1567 gtk_css_provider_propagate_error (GtkCssProvider  *provider,
1568                                   const gchar     *path,
1569                                   guint            line,
1570                                   guint            position,
1571                                   const GError    *error,
1572                                   GError         **propagate_to)
1573 {
1574   /* don't fail for deprecations */
1575   if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
1576     {
1577       g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
1578       return;
1579     }
1580
1581   /* we already set an error. And we'd like to keep the first one */
1582   if (*propagate_to)
1583     return;
1584
1585   *propagate_to = g_error_copy (error);
1586   g_prefix_error (propagate_to, "%s:%u:%u: ", path ? path : "<unknown>", line, position);
1587 }
1588
1589 static void
1590 parse_import (GtkCssScanner *scanner)
1591 {
1592   GFile *file;
1593   char *uri;
1594
1595   uri = _gtk_css_parser_read_uri (scanner->parser);
1596   if (uri == NULL)
1597     {
1598       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1599       return;
1600     }
1601
1602   file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
1603   g_free (uri);
1604
1605   if (gtk_css_scanner_would_recurse (scanner, file))
1606     {
1607        char *path = g_file_get_path (file);
1608        gtk_css_provider_error (scanner->provider,
1609                                scanner,
1610                                GTK_CSS_PROVIDER_ERROR,
1611                                GTK_CSS_PROVIDER_ERROR_IMPORT,
1612                                "Loading '%s' would recurse",
1613                                path);
1614        g_free (path);
1615     }
1616   else
1617     {
1618       gtk_css_provider_load_internal (scanner->provider,
1619                                       scanner,
1620                                       file,
1621                                       NULL, 0,
1622                                       NULL);
1623     }
1624
1625   if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1626     {
1627       g_object_unref (file);
1628       gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
1629       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1630       return;
1631     }
1632
1633   g_object_unref (file);
1634 }
1635
1636 static void
1637 parse_color_definition (GtkCssScanner *scanner)
1638 {
1639   GtkSymbolicColor *symbolic;
1640   char *name;
1641
1642   name = _gtk_css_parser_try_name (scanner->parser, TRUE);
1643   if (name == NULL)
1644     {
1645       gtk_css_provider_error_literal (scanner->provider,
1646                                       scanner,
1647                                       GTK_CSS_PROVIDER_ERROR,
1648                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1649                                       "Not a valid color name");
1650       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1651       return;
1652     }
1653
1654   symbolic = _gtk_css_parser_read_symbolic_color (scanner->parser);
1655   if (symbolic == NULL)
1656     {
1657       g_free (name);
1658       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1659       return;
1660     }
1661
1662   if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1663     {
1664       g_free (name);
1665       gtk_symbolic_color_unref (symbolic);
1666       gtk_css_provider_error_literal (scanner->provider,
1667                                       scanner,
1668                                       GTK_CSS_PROVIDER_ERROR,
1669                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1670                                       "Missing semicolon at end of color definition");
1671       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1672       return;
1673     }
1674
1675   g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
1676 }
1677
1678 static void
1679 parse_binding_set (GtkCssScanner *scanner)
1680 {
1681   GtkBindingSet *binding_set;
1682   char *name;
1683
1684   name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1685   if (name == NULL)
1686     {
1687       gtk_css_provider_error_literal (scanner->provider,
1688                                       scanner,
1689                                       GTK_CSS_PROVIDER_ERROR,
1690                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1691                                       "Expected name for binding set");
1692       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1693       goto skip_semicolon;
1694     }
1695
1696   binding_set = gtk_binding_set_find (name);
1697   if (!binding_set)
1698     {
1699       binding_set = gtk_binding_set_new (name);
1700       binding_set->parsed = TRUE;
1701     }
1702   g_free (name);
1703
1704   if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
1705     {
1706       gtk_css_provider_error_literal (scanner->provider,
1707                                       scanner,
1708                                       GTK_CSS_PROVIDER_ERROR,
1709                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1710                                       "Expected '{' for binding set");
1711       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1712       goto skip_semicolon;
1713     }
1714
1715   while (!_gtk_css_parser_is_eof (scanner->parser) &&
1716          !_gtk_css_parser_begins_with (scanner->parser, '}'))
1717     {
1718       name = _gtk_css_parser_read_value (scanner->parser);
1719       if (name == NULL)
1720         {
1721           _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1722           continue;
1723         }
1724
1725       gtk_binding_entry_add_signal_from_string (binding_set, name);
1726       g_free (name);
1727
1728       if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
1729         {
1730           if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
1731               !_gtk_css_parser_is_eof (scanner->parser))
1732             {
1733               gtk_css_provider_error_literal (scanner->provider,
1734                                               scanner,
1735                                               GTK_CSS_PROVIDER_ERROR,
1736                                               GTK_CSS_PROVIDER_ERROR_SYNTAX,
1737                                               "Expected semicolon");
1738               _gtk_css_parser_resync (scanner->parser, TRUE, '}');
1739             }
1740         }
1741     }
1742
1743   if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
1744     {
1745       gtk_css_provider_error_literal (scanner->provider,
1746                                       scanner,
1747                                       GTK_CSS_PROVIDER_ERROR,
1748                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1749                                       "expected '}' after declarations");
1750       if (!_gtk_css_parser_is_eof (scanner->parser))
1751         _gtk_css_parser_resync (scanner->parser, FALSE, 0);
1752     }
1753
1754 skip_semicolon:
1755   if (_gtk_css_parser_try (scanner->parser, ";", TRUE))
1756     gtk_css_provider_error_literal (scanner->provider,
1757                                     scanner,
1758                                     GTK_CSS_PROVIDER_ERROR,
1759                                     GTK_CSS_PROVIDER_ERROR_DEPRECATED,
1760                                     "Nonstandard semicolon at end of binding set");
1761 }
1762
1763 static void
1764 parse_at_keyword (GtkCssScanner *scanner)
1765 {
1766   if (_gtk_css_parser_try (scanner->parser, "@import", TRUE))
1767     parse_import (scanner);
1768   else if (_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
1769     parse_color_definition (scanner);
1770   else if (_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
1771     parse_binding_set (scanner);
1772   else
1773     {
1774       gtk_css_provider_error_literal (scanner->provider,
1775                                       scanner,
1776                                       GTK_CSS_PROVIDER_ERROR,
1777                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1778                                       "unknown @ rule");
1779       _gtk_css_parser_resync (scanner->parser, TRUE, 0);
1780     }
1781 }
1782
1783 static gboolean
1784 parse_selector_class (GtkCssScanner *scanner, GArray *classes)
1785 {
1786   GQuark qname;
1787   char *name;
1788     
1789   name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1790
1791   if (name == NULL)
1792     {
1793       gtk_css_provider_error_literal (scanner->provider,
1794                                       scanner,
1795                                       GTK_CSS_PROVIDER_ERROR,
1796                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1797                                       "Expected a valid name for class");
1798       return FALSE;
1799     }
1800
1801   qname = g_quark_from_string (name);
1802   g_array_append_val (classes, qname);
1803   g_free (name);
1804   return TRUE;
1805 }
1806
1807 static gboolean
1808 parse_selector_name (GtkCssScanner *scanner, GArray *names)
1809 {
1810   GQuark qname;
1811   char *name;
1812     
1813   name = _gtk_css_parser_try_name (scanner->parser, FALSE);
1814
1815   if (name == NULL)
1816     {
1817       gtk_css_provider_error_literal (scanner->provider,
1818                                       scanner,
1819                                       GTK_CSS_PROVIDER_ERROR,
1820                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1821                                       "Expected a valid name for id");
1822       return FALSE;
1823     }
1824
1825   qname = g_quark_from_string (name);
1826   g_array_append_val (names, qname);
1827   g_free (name);
1828   return TRUE;
1829 }
1830
1831 static gboolean
1832 parse_selector_pseudo_class (GtkCssScanner  *scanner,
1833                              GtkRegionFlags *region_to_modify,
1834                              GtkStateFlags  *state_to_modify)
1835 {
1836   struct {
1837     const char *name;
1838     GtkRegionFlags region_flag;
1839     GtkStateFlags state_flag;
1840   } pseudo_classes[] = {
1841     { "first",        GTK_REGION_FIRST, 0 },
1842     { "last",         GTK_REGION_LAST, 0 },
1843     { "sorted",       GTK_REGION_SORTED, 0 },
1844     { "active",       0, GTK_STATE_FLAG_ACTIVE },
1845     { "prelight",     0, GTK_STATE_FLAG_PRELIGHT },
1846     { "hover",        0, GTK_STATE_FLAG_PRELIGHT },
1847     { "selected",     0, GTK_STATE_FLAG_SELECTED },
1848     { "insensitive",  0, GTK_STATE_FLAG_INSENSITIVE },
1849     { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
1850     { "focused",      0, GTK_STATE_FLAG_FOCUSED },
1851     { "focus",        0, GTK_STATE_FLAG_FOCUSED },
1852     { NULL, }
1853   }, nth_child_classes[] = {
1854     { "first",        GTK_REGION_FIRST, 0 },
1855     { "last",         GTK_REGION_LAST, 0 },
1856     { "even",         GTK_REGION_EVEN, 0 },
1857     { "odd",          GTK_REGION_ODD, 0 },
1858     { NULL, }
1859   }, *classes;
1860   guint i;
1861   char *name;
1862
1863   name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
1864   if (name == NULL)
1865     {
1866       gtk_css_provider_error_literal (scanner->provider,
1867                                       scanner,
1868                                       GTK_CSS_PROVIDER_ERROR,
1869                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
1870                                       "Missing name of pseudo-class");
1871       return FALSE;
1872     }
1873
1874   if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
1875     {
1876       char *function = name;
1877
1878       name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
1879       if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
1880         {
1881           gtk_css_provider_error_literal (scanner->provider,
1882                                           scanner,
1883                                           GTK_CSS_PROVIDER_ERROR,
1884                                           GTK_CSS_PROVIDER_ERROR_SYNTAX,
1885                                           "Missing closing bracket for pseudo-class");
1886           return FALSE;
1887         }
1888
1889       if (g_ascii_strcasecmp (function, "nth-child") != 0)
1890         {
1891           gtk_css_provider_error (scanner->provider,
1892                                   scanner,
1893                                   GTK_CSS_PROVIDER_ERROR,
1894                                   GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1895                                   "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
1896           g_free (function);
1897           g_free (name);
1898           return FALSE;
1899         }
1900       
1901       g_free (function);
1902     
1903       if (name == NULL)
1904         {
1905           gtk_css_provider_error (scanner->provider,
1906                                   scanner,
1907                                   GTK_CSS_PROVIDER_ERROR,
1908                                   GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1909                                   "nth-child() requires an argument");
1910           return FALSE;
1911         }
1912
1913       classes = nth_child_classes;
1914     }
1915   else
1916     classes = pseudo_classes;
1917
1918   for (i = 0; classes[i].name != NULL; i++)
1919     {
1920       if (g_ascii_strcasecmp (name, classes[i].name) == 0)
1921         {
1922           if ((*region_to_modify & classes[i].region_flag) ||
1923               (*state_to_modify & classes[i].state_flag))
1924             {
1925               if (classes == nth_child_classes)
1926                 gtk_css_provider_error (scanner->provider,
1927                                         scanner,
1928                                         GTK_CSS_PROVIDER_ERROR,
1929                                         GTK_CSS_PROVIDER_ERROR_SYNTAX,
1930                                         "Duplicate pseudo-class 'nth-child(%s)'", name);
1931               else
1932                 gtk_css_provider_error (scanner->provider,
1933                                         scanner,
1934                                         GTK_CSS_PROVIDER_ERROR,
1935                                         GTK_CSS_PROVIDER_ERROR_SYNTAX,
1936                                         "Duplicate pseudo-class '%s'", name);
1937             }
1938           *region_to_modify |= classes[i].region_flag;
1939           *state_to_modify |= classes[i].state_flag;
1940
1941           g_free (name);
1942           return TRUE;
1943         }
1944     }
1945
1946   if (classes == nth_child_classes)
1947     gtk_css_provider_error (scanner->provider,
1948                             scanner,
1949                             GTK_CSS_PROVIDER_ERROR,
1950                             GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1951                             "Unknown pseudo-class 'nth-child(%s)'", name);
1952   else
1953     gtk_css_provider_error (scanner->provider,
1954                             scanner,
1955                             GTK_CSS_PROVIDER_ERROR,
1956                             GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
1957                             "Unknown pseudo-class '%s'", name);
1958   g_free (name);
1959   return FALSE;
1960 }
1961
1962 static gboolean
1963 parse_simple_selector (GtkCssScanner *scanner,
1964                        char **name,
1965                        GArray *ids,
1966                        GArray *classes,
1967                        GtkRegionFlags *pseudo_classes,
1968                        GtkStateFlags *state)
1969 {
1970   gboolean parsed_something;
1971   
1972   *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
1973   if (*name)
1974     parsed_something = TRUE;
1975   else
1976     parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
1977
1978   do {
1979       if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
1980         {
1981           if (!parse_selector_name (scanner, ids))
1982             return FALSE;
1983         }
1984       else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
1985         {
1986           if (!parse_selector_class (scanner, classes))
1987             return FALSE;
1988         }
1989       else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
1990         {
1991           if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
1992             return FALSE;
1993         }
1994       else if (!parsed_something)
1995         {
1996           gtk_css_provider_error_literal (scanner->provider,
1997                                           scanner,
1998                                           GTK_CSS_PROVIDER_ERROR,
1999                                           GTK_CSS_PROVIDER_ERROR_SYNTAX,
2000                                           "Expected a valid selector");
2001           return FALSE;
2002         }
2003       else
2004         break;
2005
2006       parsed_something = TRUE;
2007     }
2008   while (!_gtk_css_parser_is_eof (scanner->parser));
2009
2010   _gtk_css_parser_skip_whitespace (scanner->parser);
2011   return TRUE;
2012 }
2013
2014 static GtkCssSelector *
2015 parse_selector (GtkCssScanner *scanner)
2016 {
2017   GtkCssSelector *selector = NULL;
2018
2019   do {
2020       char *name = NULL;
2021       GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
2022       GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
2023       GtkRegionFlags pseudo_classes = 0;
2024       GtkStateFlags state = 0;
2025       GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
2026
2027       if (selector)
2028         {
2029           if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
2030             combine = GTK_CSS_COMBINE_CHILD;
2031         }
2032
2033       if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
2034         {
2035           g_array_free (ids, TRUE);
2036           g_array_free (classes, TRUE);
2037           if (selector)
2038             _gtk_css_selector_free (selector);
2039           return NULL;
2040         }
2041
2042       selector = _gtk_css_selector_new (selector,
2043                                         combine,
2044                                         name,
2045                                         (GQuark *) g_array_free (ids, ids->len == 0),
2046                                         (GQuark *) g_array_free (classes, classes->len == 0),
2047                                         pseudo_classes,
2048                                         state);
2049       g_free (name);
2050     }
2051   while (!_gtk_css_parser_is_eof (scanner->parser) &&
2052          !_gtk_css_parser_begins_with (scanner->parser, ',') &&
2053          !_gtk_css_parser_begins_with (scanner->parser, '{'));
2054
2055   return selector;
2056 }
2057
2058 static GSList *
2059 parse_selector_list (GtkCssScanner *scanner)
2060 {
2061   GSList *selectors = NULL;
2062
2063   do {
2064       GtkCssSelector *select = parse_selector (scanner);
2065
2066       if (select == NULL)
2067         {
2068           g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2069           _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2070           return NULL;
2071         }
2072
2073       selectors = g_slist_prepend (selectors, select);
2074     }
2075   while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
2076
2077   return selectors;
2078 }
2079
2080 static void
2081 parse_declaration (GtkCssScanner *scanner,
2082                    GtkCssRuleset *ruleset)
2083 {
2084   const GtkStyleProperty *property;
2085   char *name;
2086
2087   name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
2088   if (name == NULL)
2089     goto check_for_semicolon;
2090
2091   property = _gtk_style_property_lookup (name);
2092   if (property == NULL && name[0] != '-')
2093     {
2094       gtk_css_provider_error (scanner->provider,
2095                               scanner,
2096                               GTK_CSS_PROVIDER_ERROR,
2097                               GTK_CSS_PROVIDER_ERROR_NAME,
2098                               "'%s' is not a valid property name",
2099                               name);
2100       _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2101       g_free (name);
2102       return;
2103     }
2104
2105   if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
2106     {
2107       gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
2108       _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2109       g_free (name);
2110       return;
2111     }
2112
2113   if (property)
2114     {
2115       GValue *val;
2116
2117       g_free (name);
2118
2119       val = g_slice_new0 (GValue);
2120       g_value_init (val, property->pspec->value_type);
2121
2122       if (_gtk_style_property_parse_value (property,
2123                                            val,
2124                                            scanner->parser,
2125                                            gtk_css_scanner_get_base_url (scanner)))
2126         {
2127           if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
2128               _gtk_css_parser_begins_with (scanner->parser, '}') ||
2129               _gtk_css_parser_is_eof (scanner->parser))
2130             {
2131               gtk_css_ruleset_add (ruleset, property, val);
2132             }
2133           else
2134             {
2135               gtk_css_provider_error_literal (scanner->provider,
2136                                               scanner,
2137                                               GTK_CSS_PROVIDER_ERROR,
2138                                               GTK_CSS_PROVIDER_ERROR_SYNTAX,
2139                                               "Junk at end of value");
2140               _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2141               g_value_unset (val);
2142               g_slice_free (GValue, val);
2143               return;
2144             }
2145         }
2146       else
2147         {
2148           g_value_unset (val);
2149           g_slice_free (GValue, val);
2150           _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2151           return;
2152         }
2153     }
2154   else if (name[0] == '-')
2155     {
2156       char *value_str;
2157
2158       value_str = _gtk_css_parser_read_value (scanner->parser);
2159       if (value_str)
2160         {
2161           GValue *val;
2162
2163           val = g_slice_new0 (GValue);
2164           g_value_init (val, G_TYPE_STRING);
2165           g_value_take_string (val, value_str);
2166
2167           gtk_css_ruleset_add_style (ruleset, name, val);
2168         }
2169       else
2170         {
2171           _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2172           return;
2173         }
2174     }
2175   else
2176     g_free (name);
2177
2178 check_for_semicolon:
2179   if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
2180     {
2181       if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
2182           !_gtk_css_parser_is_eof (scanner->parser))
2183         {
2184           gtk_css_provider_error_literal (scanner->provider,
2185                                           scanner,
2186                                           GTK_CSS_PROVIDER_ERROR,
2187                                           GTK_CSS_PROVIDER_ERROR_SYNTAX,
2188                                           "Expected semicolon");
2189           _gtk_css_parser_resync (scanner->parser, TRUE, '}');
2190         }
2191     }
2192 }
2193
2194 static void
2195 parse_declarations (GtkCssScanner *scanner,
2196                     GtkCssRuleset *ruleset)
2197 {
2198   while (!_gtk_css_parser_is_eof (scanner->parser) &&
2199          !_gtk_css_parser_begins_with (scanner->parser, '}'))
2200     {
2201       parse_declaration (scanner, ruleset);
2202     }
2203 }
2204
2205 static void
2206 parse_ruleset (GtkCssScanner *scanner)
2207 {
2208   GSList *selectors;
2209   GtkCssRuleset ruleset = { 0, };
2210
2211   selectors = parse_selector_list (scanner);
2212   if (selectors == NULL)
2213     return;
2214
2215   if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
2216     {
2217       gtk_css_provider_error_literal (scanner->provider,
2218                                       scanner,
2219                                       GTK_CSS_PROVIDER_ERROR,
2220                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
2221                                       "expected '{' after selectors");
2222       _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2223       g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2224       return;
2225     }
2226
2227   parse_declarations (scanner, &ruleset);
2228
2229   if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
2230     {
2231       gtk_css_provider_error_literal (scanner->provider,
2232                                       scanner,
2233                                       GTK_CSS_PROVIDER_ERROR,
2234                                       GTK_CSS_PROVIDER_ERROR_SYNTAX,
2235                                       "expected '}' after declarations");
2236       if (!_gtk_css_parser_is_eof (scanner->parser))
2237         {
2238           _gtk_css_parser_resync (scanner->parser, FALSE, 0);
2239           g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
2240           gtk_css_ruleset_clear (&ruleset);
2241           return;
2242         }
2243     }
2244
2245   css_provider_commit (scanner->provider, selectors, &ruleset);
2246   gtk_css_ruleset_clear (&ruleset);
2247 }
2248
2249 static void
2250 parse_statement (GtkCssScanner *scanner)
2251 {
2252   if (_gtk_css_parser_begins_with (scanner->parser, '@'))
2253     parse_at_keyword (scanner);
2254   else
2255     parse_ruleset (scanner);
2256 }
2257
2258 static void
2259 parse_stylesheet (GtkCssScanner *scanner)
2260 {
2261   _gtk_css_parser_skip_whitespace (scanner->parser);
2262
2263   while (!_gtk_css_parser_is_eof (scanner->parser))
2264     {
2265       if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
2266           _gtk_css_parser_try (scanner->parser, "-->", TRUE))
2267         continue;
2268
2269       parse_statement (scanner);
2270     }
2271 }
2272
2273 static int
2274 gtk_css_provider_compare_rule (gconstpointer a_,
2275                                gconstpointer b_)
2276 {
2277   const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
2278   const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
2279   int compare;
2280
2281   compare = _gtk_css_selector_compare (a->selector, b->selector);
2282   if (compare != 0)
2283     return compare;
2284
2285   /* compare pointers in array to ensure a stable sort */
2286   if (a_ < b_)
2287     return -1;
2288
2289   if (a_ > b_)
2290     return 1;
2291
2292   return 0;
2293 }
2294
2295 static void
2296 gtk_css_provider_postprocess (GtkCssProvider *css_provider)
2297 {
2298   GtkCssProviderPrivate *priv = css_provider->priv;
2299
2300   g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
2301 }
2302
2303 static gboolean
2304 gtk_css_provider_load_internal (GtkCssProvider *css_provider,
2305                                 GtkCssScanner  *parent,
2306                                 GFile          *file,
2307                                 const char     *data,
2308                                 gsize           length,
2309                                 GError        **error)
2310 {
2311   GtkCssScanner *scanner;
2312   gulong error_handler;
2313   char *free_data = NULL;
2314
2315   if (error)
2316     error_handler = g_signal_connect (css_provider,
2317                                       "parsing-error",
2318                                       G_CALLBACK (gtk_css_provider_propagate_error),
2319                                       error);
2320   else
2321     error_handler = 0; /* silence gcc */
2322
2323   if (data == NULL)
2324     {
2325       GError *load_error = NULL;
2326
2327       if (g_file_load_contents (file, NULL,
2328                                 &free_data, &length,
2329                                 NULL, &load_error))
2330         {
2331           data = free_data;
2332         }
2333       else
2334         {
2335           if (parent)
2336             {
2337               gtk_css_provider_error (css_provider,
2338                                       parent,
2339                                       GTK_CSS_PROVIDER_ERROR,
2340                                       GTK_CSS_PROVIDER_ERROR_IMPORT,
2341                                       "Failed to import: %s",
2342                                       load_error->message);
2343               g_error_free (load_error);
2344             }
2345           else
2346             {
2347               gtk_css_provider_take_error_full (css_provider,
2348                                                 file,
2349                                                 0, 0,
2350                                                 load_error);
2351             }
2352         }
2353     }
2354
2355   if (data)
2356     {
2357       scanner = gtk_css_scanner_new (css_provider, parent, file, data, length);
2358
2359       parse_stylesheet (scanner);
2360
2361       gtk_css_scanner_destroy (scanner);
2362
2363       if (parent == NULL)
2364         gtk_css_provider_postprocess (css_provider);
2365     }
2366
2367   g_free (free_data);
2368
2369   if (error)
2370     {
2371       g_signal_handler_disconnect (css_provider, error_handler);
2372
2373       if (*error)
2374         {
2375           /* We clear all contents from the provider for backwards compat reasons */
2376           gtk_css_provider_reset (css_provider);
2377           return FALSE;
2378         }
2379     }
2380
2381   return TRUE;
2382 }
2383
2384 /**
2385  * gtk_css_provider_load_from_data:
2386  * @css_provider: a #GtkCssProvider
2387  * @data: (array length=length) (element-type guint8): CSS data loaded in memory
2388  * @length: the length of @data in bytes, or -1 for NUL terminated strings
2389  * @error: (out) (allow-none): return location for a #GError, or %NULL
2390  *
2391  * Loads @data into @css_provider, making it clear any previously loaded
2392  * information.
2393  *
2394  * Returns: %TRUE if the data could be loaded.
2395  **/
2396 gboolean
2397 gtk_css_provider_load_from_data (GtkCssProvider  *css_provider,
2398                                  const gchar     *data,
2399                                  gssize           length,
2400                                  GError         **error)
2401 {
2402   g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2403   g_return_val_if_fail (data != NULL, FALSE);
2404
2405   if (length < 0)
2406     length = strlen (data);
2407
2408   gtk_css_provider_reset (css_provider);
2409
2410   return gtk_css_provider_load_internal (css_provider, NULL, NULL, data, length, error);
2411 }
2412
2413 /**
2414  * gtk_css_provider_load_from_file:
2415  * @css_provider: a #GtkCssProvider
2416  * @file: #GFile pointing to a file to load
2417  * @error: (out) (allow-none): return location for a #GError, or %NULL
2418  *
2419  * Loads the data contained in @file into @css_provider, making it
2420  * clear any previously loaded information.
2421  *
2422  * Returns: %TRUE if the data could be loaded.
2423  **/
2424 gboolean
2425 gtk_css_provider_load_from_file (GtkCssProvider  *css_provider,
2426                                  GFile           *file,
2427                                  GError         **error)
2428 {
2429   g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2430   g_return_val_if_fail (G_IS_FILE (file), FALSE);
2431
2432   gtk_css_provider_reset (css_provider);
2433
2434   return gtk_css_provider_load_internal (css_provider, NULL, file, NULL, 0, error);
2435 }
2436
2437 /**
2438  * gtk_css_provider_load_from_path:
2439  * @css_provider: a #GtkCssProvider
2440  * @path: the path of a filename to load, in the GLib filename encoding
2441  * @error: (out) (allow-none): return location for a #GError, or %NULL
2442  *
2443  * Loads the data contained in @path into @css_provider, making it clear
2444  * any previously loaded information.
2445  *
2446  * Returns: %TRUE if the data could be loaded.
2447  **/
2448 gboolean
2449 gtk_css_provider_load_from_path (GtkCssProvider  *css_provider,
2450                                  const gchar     *path,
2451                                  GError         **error)
2452 {
2453   GFile *file;
2454   gboolean result;
2455
2456   g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
2457   g_return_val_if_fail (path != NULL, FALSE);
2458
2459   file = g_file_new_for_path (path);
2460   
2461   result = gtk_css_provider_load_from_file (css_provider, file, error);
2462
2463   g_object_unref (file);
2464
2465   return result;
2466 }
2467
2468 /**
2469  * gtk_css_provider_get_default:
2470  *
2471  * Returns the provider containing the style settings used as a
2472  * fallback for all widgets.
2473  *
2474  * Returns: (transfer none): The provider used for fallback styling.
2475  *          This memory is owned by GTK+, and you must not free it.
2476  **/
2477 GtkCssProvider *
2478 gtk_css_provider_get_default (void)
2479 {
2480   static GtkCssProvider *provider;
2481
2482   if (G_UNLIKELY (!provider))
2483     {
2484       const gchar *str =
2485         "@define-color fg_color #000; \n"
2486         "@define-color bg_color #dcdad5; \n"
2487         "@define-color text_color #000; \n"
2488         "@define-color base_color #fff; \n"
2489         "@define-color selected_bg_color #4b6983; \n"
2490         "@define-color selected_fg_color #fff; \n"
2491         "@define-color tooltip_bg_color #eee1b3; \n"
2492         "@define-color tooltip_fg_color #000; \n"
2493         "@define-color placeholder_text_color #808080; \n"
2494         "\n"
2495         "@define-color info_fg_color rgb (181, 171, 156);\n"
2496         "@define-color info_bg_color rgb (252, 252, 189);\n"
2497         "@define-color warning_fg_color rgb (173, 120, 41);\n"
2498         "@define-color warning_bg_color rgb (250, 173, 61);\n"
2499         "@define-color question_fg_color rgb (97, 122, 214);\n"
2500         "@define-color question_bg_color rgb (138, 173, 212);\n"
2501         "@define-color error_fg_color rgb (166, 38, 38);\n"
2502         "@define-color error_bg_color rgb (237, 54, 54);\n"
2503         "\n"
2504         "* {\n"
2505         "  background-color: @bg_color;\n"
2506         "  color: @fg_color;\n"
2507         "  border-color: shade (@bg_color, 0.6);\n"
2508         "  padding: 2;\n"
2509         "  border-width: 0;\n"
2510         "}\n"
2511         "\n"
2512         "*:prelight {\n"
2513         "  background-color: shade (@bg_color, 1.05);\n"
2514         "  color: shade (@fg_color, 1.3);\n"
2515         "}\n"
2516         "\n"
2517         "*:selected {\n"
2518         "  background-color: @selected_bg_color;\n"
2519         "  color: @selected_fg_color;\n"
2520         "}\n"
2521         "\n"
2522         ".expander, GtkTreeView.view.expander {\n"
2523         "  color: #fff;\n"
2524         "}\n"
2525         "\n"
2526         ".expander:prelight,\n"
2527         "GtkTreeView.view.expander:selected:prelight {\n"
2528         "  color: @text_color;\n"
2529         "}\n"
2530         "\n"
2531         ".expander:active {\n"
2532         "  transition: 200ms linear;\n"
2533         "}\n"
2534         "\n"
2535         "*:insensitive {\n"
2536         "  border-color: shade (@bg_color, 0.7);\n"
2537         "  background-color: shade (@bg_color, 0.9);\n"
2538         "  color: shade (@bg_color, 0.7);\n"
2539         "}\n"
2540         "\n"
2541         ".view {\n"
2542         "  border-width: 0;\n"
2543         "  border-radius: 0;\n"
2544         "  background-color: @base_color;\n"
2545         "  color: @text_color;\n"
2546         "}\n"
2547         ".view:selected {\n"
2548         "  background-color: shade (@bg_color, 0.9);\n"
2549         "  color: @fg_color;\n"
2550         "}\n"
2551         "\n"
2552         ".view:selected:focused {\n"
2553         "  background-color: @selected_bg_color;\n"
2554         "  color: @selected_fg_color;\n"
2555         "}\n"
2556         "\n"
2557         ".view column:sorted row,\n"
2558         ".view column:sorted row:prelight {\n"
2559         "  background-color: shade (@bg_color, 0.85);\n"
2560         "}\n"
2561         "\n"
2562         ".view column:sorted row:nth-child(odd),\n"
2563         ".view column:sorted row:nth-child(odd):prelight {\n"
2564         "  background-color: shade (@bg_color, 0.8);\n"
2565         "}\n"
2566         "\n"
2567         ".view row,\n"
2568         ".view row:prelight {\n"
2569         "  background-color: @base_color;\n"
2570         "  color: @text_color;\n"
2571         "}\n"
2572         "\n"
2573         ".view row:nth-child(odd),\n"
2574         ".view row:nth-child(odd):prelight {\n"
2575         "  background-color: shade (@base_color, 0.93); \n"
2576         "}\n"
2577         "\n"
2578         ".view row:selected:focused {\n"
2579         "  background-color: @selected_bg_color;\n"
2580         "}\n"
2581         "\n"
2582         ".view row:selected {\n"
2583         "  background-color: darker (@bg_color);\n"
2584         "  color: @selected_fg_color;\n"
2585         "}\n"
2586         "\n"
2587         ".view.cell.trough,\n"
2588         ".view.cell.trough:hover,\n"
2589         ".view.cell.trough:selected,\n"
2590         ".view.cell.trough:selected:focused {\n"
2591         "  background-color: @bg_color;\n"
2592         "  color: @fg_color;\n"
2593         "}\n"
2594         "\n"
2595         ".view.cell.progressbar,\n"
2596         ".view.cell.progressbar:hover,\n"
2597         ".view.cell.progressbar:selected,\n"
2598         ".view.cell.progressbar:selected:focused {\n"
2599         "  background-color: @selected_bg_color;\n"
2600         "  color: @selected_fg_color;\n"
2601         "}\n"
2602         "\n"
2603         ".rubberband {\n"
2604         "  background-color: alpha (@fg_color, 0.25);\n"
2605         "  border-color: @fg_color;\n"
2606         "  border-style: solid;\n"
2607         "  border-width: 1;\n"
2608         "}\n"
2609         "\n"
2610         ".tooltip {\n"
2611         "  background-color: @tooltip_bg_color; \n"
2612         "  color: @tooltip_fg_color; \n"
2613         "  border-color: @tooltip_fg_color; \n"
2614         "  border-width: 1;\n"
2615         "  border-style: solid;\n"
2616         "}\n"
2617         "\n"
2618         ".button,\n"
2619         ".slider {\n"
2620         "  border-style: outset; \n"
2621         "  border-width: 2; \n"
2622         "}\n"
2623         "\n"
2624         ".button:active {\n"
2625         "  background-color: shade (@bg_color, 0.7);\n"
2626         "  border-style: inset; \n"
2627         "}\n"
2628         "\n"
2629         ".button:prelight,\n"
2630         ".slider:prelight {\n"
2631         "  background-color: @selected_bg_color;\n"
2632         "  color: @selected_fg_color;\n"
2633         "  border-color: shade (@selected_bg_color, 0.7);\n"
2634         "}\n"
2635         "\n"
2636         ".trough {\n"
2637         "  background-color: darker (@bg_color);\n"
2638         "  border-style: inset;\n"
2639         "  border-width: 1;\n"
2640         "  padding: 0;\n"
2641         "}\n"
2642         "\n"
2643         ".entry {\n"
2644         "  border-style: inset;\n"
2645         "  border-width: 2;\n"
2646         "  background-color: @base_color;\n"
2647         "  color: @text_color;\n"
2648         "}\n"
2649         "\n"
2650         ".entry:insensitive {\n"
2651         "  background-color: shade (@base_color, 0.9);\n"
2652         "  color: shade (@base_color, 0.7);\n"
2653         "}\n"
2654         ".entry:active {\n"
2655         "  background-color: #c4c2bd;\n"
2656         "  color: #000;\n"
2657         "}\n"
2658         "\n"
2659         ".progressbar,\n"
2660         ".entry.progressbar, \n"
2661         ".cell.progressbar {\n"
2662         "  background-color: @selected_bg_color;\n"
2663         "  border-color: shade (@selected_bg_color, 0.7);\n"
2664         "  color: @selected_fg_color;\n"
2665         "  border-style: outset;\n"
2666         "  border-width: 1;\n"
2667         "}\n"
2668         "\n"
2669         "GtkCheckButton:hover,\n"
2670         "GtkCheckButton:selected,\n"
2671         "GtkRadioButton:hover,\n"
2672         "GtkRadioButton:selected {\n"
2673         "  background-color: shade (@bg_color, 1.05);\n"
2674         "}\n"
2675         "\n"
2676         ".check, .radio,"
2677         ".cell.check, .cell.radio,\n"
2678         ".cell.check:hover, .cell.radio:hover {\n"
2679         "  border-style: solid;\n"
2680         "  border-width: 1;\n"
2681         "  background-color: @base_color;\n"
2682         "  border-color: @fg_color;\n"
2683         "}\n"
2684         "\n"
2685         ".check:active, .radio:active,\n"
2686         ".check:hover, .radio:hover {\n"
2687         "  background-color: @base_color;\n"
2688         "  border-color: @fg_color;\n"
2689         "  color: @text_color;\n"
2690         "}\n"
2691         "\n"
2692         ".check:selected, .radio:selected {\n"
2693         "  background-color: darker (@bg_color);\n"
2694         "  color: @selected_fg_color;\n"
2695         "  border-color: @selected_fg_color;\n"
2696         "}\n"
2697         "\n"
2698         ".check:selected:focused, .radio:selected:focused {\n"
2699         "  background-color: @selected_bg_color;\n"
2700         "}\n"
2701         "\n"
2702         ".menu.check, .menu.radio {\n"
2703         "  color: @fg_color;\n"
2704         "  border-style: none;\n"
2705         "  border-width: 0;\n"
2706         "}\n"
2707         "\n"
2708         ".popup {\n"
2709         "  border-style: outset;\n"
2710         "  border-width: 1;\n"
2711         "}\n"
2712         "\n"
2713         ".viewport {\n"
2714         "  border-style: inset;\n"
2715         "  border-width: 2;\n"
2716         "}\n"
2717         "\n"
2718         ".notebook {\n"
2719         "  border-style: outset;\n"
2720         "  border-width: 1;\n"
2721         "}\n"
2722         "\n"
2723         ".frame {\n"
2724         "  border-style: inset;\n"
2725         "  border-width: 1;\n"
2726         "}\n"
2727         "\n"
2728         "GtkScrolledWindow.frame {\n"
2729         "  padding: 0;\n"
2730         "}\n"
2731         "\n"
2732         ".menu,\n"
2733         ".menubar,\n"
2734         ".toolbar {\n"
2735         "  border-style: outset;\n"
2736         "  border-width: 1;\n"
2737         "}\n"
2738         "\n"
2739         ".menu:hover,\n"
2740         ".menubar:hover,\n"
2741         ".menu.check:hover,\n"
2742         ".menu.radio:hover {\n"
2743         "  background-color: @selected_bg_color;\n"
2744         "  color: @selected_fg_color;\n"
2745         "}\n"
2746         "\n"
2747         "GtkSpinButton.button {\n"
2748         "  border-width: 1;\n"
2749         "}\n"
2750         "\n"
2751         ".scale.slider:hover,\n"
2752         "GtkSpinButton.button:hover {\n"
2753         "  background-color: shade (@bg_color, 1.05);\n"
2754         "  border-color: shade (@bg_color, 0.8);\n"
2755         "}\n"
2756         "\n"
2757         "GtkSwitch.trough:active {\n"
2758         "  background-color: @selected_bg_color;\n"
2759         "  color: @selected_fg_color;\n"
2760         "}\n"
2761         "\n"
2762         "GtkToggleButton.button:inconsistent {\n"
2763         "  border-style: outset;\n"
2764         "  border-width: 1px;\n"
2765         "  background-color: shade (@bg_color, 0.9);\n"
2766         "  border-color: shade (@bg_color, 0.7);\n"
2767         "}\n"
2768         "\n"
2769         "GtkLabel:selected {\n"
2770         "  background-color: shade (@bg_color, 0.9);\n"
2771         "}\n"
2772         "\n"
2773         "GtkLabel:selected:focused {\n"
2774         "  background-color: @selected_bg_color;\n"
2775         "}\n"
2776         "\n"
2777         ".spinner:active {\n"
2778         "  transition: 750ms linear loop;\n"
2779         "}\n"
2780         "\n"
2781         ".info {\n"
2782         "  background-color: @info_bg_color;\n"
2783         "  color: @info_fg_color;\n"
2784         "}\n"
2785         "\n"
2786         ".warning {\n"
2787         "  background-color: @warning_bg_color;\n"
2788         "  color: @warning_fg_color;\n"
2789         "}\n"
2790         "\n"
2791         ".question {\n"
2792         "  background-color: @question_bg_color;\n"
2793         "  color: @question_fg_color;\n"
2794         "}\n"
2795         "\n"
2796         ".error {\n"
2797         "  background-color: @error_bg_color;\n"
2798         "  color: @error_fg_color;\n"
2799         "}\n"
2800         "\n"
2801         ".highlight {\n"
2802         "  background-color: @selected_bg_color;\n"
2803         "  color: @selected_fg_color;\n"
2804         "}\n"
2805         "\n"
2806         ".light-area-focus {\n"
2807         "  color: #000;\n"
2808         "}\n"
2809         "\n"
2810         ".dark-area-focus {\n"
2811         "  color: #fff;\n"
2812         "}\n"
2813         "GtkCalendar.view {\n"
2814         "  border-width: 1;\n"
2815         "  border-style: inset;\n"
2816         "  padding: 1;\n"
2817         "}\n"
2818         "\n"
2819         "GtkCalendar.view:inconsistent {\n"
2820         "  color: darker (@bg_color);\n"
2821         "}\n"
2822         "\n"
2823         "GtkCalendar.header {\n"
2824         "  background-color: @bg_color;\n"
2825         "  border-style: outset;\n"
2826         "  border-width: 2;\n"
2827         "}\n"
2828         "\n"
2829         "GtkCalendar.highlight {\n"
2830         "  border-width: 0;\n"
2831         "}\n"
2832         "\n"
2833         "GtkCalendar.button {\n"
2834         "  background-color: @bg_color;\n"
2835         "}\n"
2836         "\n"
2837         "GtkCalendar.button:hover {\n"
2838         "  background-color: lighter (@bg_color);\n"
2839         "  color: @fg_color;\n"
2840         "}\n"
2841         "\n"
2842         ".menu * {\n"
2843         "  border-width: 0;\n"
2844         "  padding: 2;\n"
2845         "}\n"
2846         "\n";
2847
2848       provider = gtk_css_provider_new ();
2849       if (!gtk_css_provider_load_from_data (provider, str, -1, NULL))
2850         {
2851           g_error ("Failed to load the internal default CSS.");
2852         }
2853     }
2854
2855   return provider;
2856 }
2857
2858 gchar *
2859 _gtk_css_provider_get_theme_dir (void)
2860 {
2861   const gchar *var;
2862   gchar *path;
2863
2864   var = g_getenv ("GTK_DATA_PREFIX");
2865
2866   if (var)
2867     path = g_build_filename (var, "share", "themes", NULL);
2868   else
2869     path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
2870
2871   return path;
2872 }
2873
2874 /**
2875  * gtk_css_provider_get_named:
2876  * @name: A theme name
2877  * @variant: (allow-none): variant to load, for example, "dark", or
2878  *     %NULL for the default
2879  *
2880  * Loads a theme from the usual theme paths
2881  *
2882  * Returns: (transfer none): a #GtkCssProvider with the theme loaded.
2883  *     This memory is owned by GTK+, and you must not free it.
2884  */
2885 GtkCssProvider *
2886 gtk_css_provider_get_named (const gchar *name,
2887                             const gchar *variant)
2888 {
2889   static GHashTable *themes = NULL;
2890   GtkCssProvider *provider;
2891   gchar *key;
2892
2893   if (G_UNLIKELY (!themes))
2894     themes = g_hash_table_new (g_str_hash, g_str_equal);
2895
2896   if (variant == NULL)
2897     key = (gchar *)name;
2898   else
2899     key = g_strconcat (name, "-", variant, NULL);
2900
2901   provider = g_hash_table_lookup (themes, key);
2902
2903   if (!provider)
2904     {
2905       const gchar *home_dir;
2906       gchar *subpath, *path = NULL;
2907
2908       if (variant)
2909         subpath = g_strdup_printf ("gtk-3.0" G_DIR_SEPARATOR_S "gtk-%s.css", variant);
2910       else
2911         subpath = g_strdup ("gtk-3.0" G_DIR_SEPARATOR_S "gtk.css");
2912
2913       /* First look in the users home directory
2914        */
2915       home_dir = g_get_home_dir ();
2916       if (home_dir)
2917         {
2918           path = g_build_filename (home_dir, ".themes", name, subpath, NULL);
2919
2920           if (!g_file_test (path, G_FILE_TEST_EXISTS))
2921             {
2922               g_free (path);
2923               path = NULL;
2924             }
2925         }
2926
2927       if (!path)
2928         {
2929           gchar *theme_dir;
2930
2931           theme_dir = _gtk_css_provider_get_theme_dir ();
2932           path = g_build_filename (theme_dir, name, subpath, NULL);
2933           g_free (theme_dir);
2934
2935           if (!g_file_test (path, G_FILE_TEST_EXISTS))
2936             {
2937               g_free (path);
2938               path = NULL;
2939             }
2940         }
2941
2942       g_free (subpath);
2943
2944       if (path)
2945         {
2946           provider = gtk_css_provider_new ();
2947
2948           if (!gtk_css_provider_load_from_path (provider, path, NULL))
2949             {
2950               g_object_unref (provider);
2951               provider = NULL;
2952             }
2953           else
2954             g_hash_table_insert (themes, g_strdup (key), provider);
2955
2956           g_free (path);
2957         }
2958     }
2959
2960   if (key != name)
2961     g_free (key);
2962
2963   return provider;
2964 }
2965
2966 static int
2967 compare_properties (gconstpointer a, gconstpointer b)
2968 {
2969   return strcmp (((const GtkStyleProperty *) a)->pspec->name,
2970                  ((const GtkStyleProperty *) b)->pspec->name);
2971 }
2972
2973 static void
2974 gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
2975                        GString             *str)
2976 {
2977   GList *keys, *walk;
2978
2979   _gtk_css_selector_print (ruleset->selector, str);
2980
2981   g_string_append (str, " {\n");
2982
2983   if (ruleset->style)
2984     {
2985       keys = g_hash_table_get_keys (ruleset->style);
2986       /* so the output is identical for identical selector styles */
2987       keys = g_list_sort (keys, compare_properties);
2988
2989       for (walk = keys; walk; walk = walk->next)
2990         {
2991           GtkStyleProperty *prop = walk->data;
2992           const GValue *value = g_hash_table_lookup (ruleset->style, prop);
2993
2994           g_string_append (str, "  ");
2995           g_string_append (str, prop->pspec->name);
2996           g_string_append (str, ": ");
2997           _gtk_style_property_print_value (prop, value, str);
2998           g_string_append (str, ";\n");
2999         }
3000
3001       g_list_free (keys);
3002     }
3003
3004   if (ruleset->widget_style)
3005     {
3006       keys = g_hash_table_get_keys (ruleset->widget_style);
3007       /* so the output is identical for identical selector styles */
3008       keys = g_list_sort (keys, (GCompareFunc) strcmp);
3009
3010       for (walk = keys; walk; walk = walk->next)
3011         {
3012           const char *name = walk->data;
3013           const GValue *value = g_hash_table_lookup (ruleset->widget_style, (gpointer) name);
3014
3015           g_string_append (str, "  ");
3016           g_string_append (str, name);
3017           g_string_append (str, ": ");
3018           g_string_append (str, g_value_get_string (value));
3019           g_string_append (str, ";\n");
3020         }
3021
3022       g_list_free (keys);
3023     }
3024
3025   g_string_append (str, "}\n");
3026 }
3027
3028 static void
3029 gtk_css_provider_print_colors (GHashTable *colors,
3030                                GString    *str)
3031 {
3032   GList *keys, *walk;
3033   char *s;
3034
3035   keys = g_hash_table_get_keys (colors);
3036   /* so the output is identical for identical styles */
3037   keys = g_list_sort (keys, (GCompareFunc) strcmp);
3038
3039   for (walk = keys; walk; walk = walk->next)
3040     {
3041       const char *name = walk->data;
3042       GtkSymbolicColor *symbolic = g_hash_table_lookup (colors, (gpointer) name);
3043
3044       g_string_append (str, "@define-color ");
3045       g_string_append (str, name);
3046       g_string_append (str, " ");
3047       s = gtk_symbolic_color_to_string (symbolic);
3048       g_string_append (str, s);
3049       g_free (s);
3050       g_string_append (str, ";\n");
3051     }
3052
3053   g_list_free (keys);
3054 }
3055
3056 /**
3057  * gtk_css_provider_to_string:
3058  * @provider: the provider to write to a string
3059  *
3060  * Convertes the @provider into a string representation in CSS
3061  * format.
3062  * 
3063  * Using gtk_css_provider_load_from_data() with the return value
3064  * from this function on a new provider created with
3065  * gtk_css_provider_new() will basicallu create a duplicate of
3066  * this @provider.
3067  *
3068  * Returns: a new string representing the @provider.
3069  **/
3070 char *
3071 gtk_css_provider_to_string (GtkCssProvider *provider)
3072 {
3073   GtkCssProviderPrivate *priv;
3074   GString *str;
3075   guint i;
3076
3077   g_return_val_if_fail (GTK_IS_CSS_PROVIDER (provider), NULL);
3078
3079   priv = provider->priv;
3080
3081   str = g_string_new ("");
3082
3083   gtk_css_provider_print_colors (priv->symbolic_colors, str);
3084
3085   for (i = 0; i < priv->rulesets->len; i++)
3086     {
3087       if (i > 0)
3088         g_string_append (str, "\n");
3089       gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
3090     }
3091
3092   return g_string_free (str, FALSE);
3093 }
3094