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