]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingengine.c
themingengine: Return NULL for nonexistant theming engines
[~andy/gtk] / gtk / gtkthemingengine.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 <math.h>
23 #include <gtk/gtk.h>
24
25 #include <gtk/gtkthemingengine.h>
26 #include <gtk/gtkstylecontext.h>
27 #include <gtk/gtkintl.h>
28
29 #include "gtkprivate.h"
30 #include "gtkborderimageprivate.h"
31 #include "gtkpango.h"
32 #include "gtkshadowprivate.h"
33 #include "gtkcsstypesprivate.h"
34 #include "gtkthemingengineprivate.h"
35 #include "gtkroundedboxprivate.h"
36
37 /**
38  * SECTION:gtkthemingengine
39  * @Short_description: Theming renderers
40  * @Title: GtkThemingEngine
41  * @See_also: #GtkStyleContext
42  *
43  * #GtkThemingEngine is the object used for rendering themed content
44  * in GTK+ widgets. Even though GTK+ has a default implementation,
45  * it can be overridden in CSS files by enforcing a #GtkThemingEngine
46  * object to be loaded as a module.
47  *
48  * In order to implement a theming engine, a #GtkThemingEngine subclass
49  * must be created, alongside the CSS file that will reference it, the
50  * theming engine would be created as an .so library, and installed in
51  * $(gtk-modules-dir)/theming-engines/.
52  *
53  * #GtkThemingEngine<!-- -->s have limited access to the object they are
54  * rendering, the #GtkThemingEngine API has read-only accessors to the
55  * style information contained in the rendered object's #GtkStyleContext.
56  */
57
58 enum {
59   SIDE_LEFT   = 1,
60   SIDE_BOTTOM = 1 << 1,
61   SIDE_RIGHT  = 1 << 2,
62   SIDE_TOP    = 1 << 3,
63   SIDE_ALL    = 0xF
64 };
65
66 enum {
67   PROP_0,
68   PROP_NAME
69 };
70
71 struct GtkThemingEnginePrivate
72 {
73   GtkStyleContext *context;
74   gchar *name;
75 };
76
77 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
78
79 static void gtk_theming_engine_finalize          (GObject      *object);
80 static void gtk_theming_engine_impl_set_property (GObject      *object,
81                                                   guint         prop_id,
82                                                   const GValue *value,
83                                                   GParamSpec   *pspec);
84 static void gtk_theming_engine_impl_get_property (GObject      *object,
85                                                   guint         prop_id,
86                                                   GValue       *value,
87                                                   GParamSpec   *pspec);
88
89 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
90                                              cairo_t          *cr,
91                                              gdouble           x,
92                                              gdouble           y,
93                                              gdouble           width,
94                                              gdouble           height);
95 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
96                                               cairo_t          *cr,
97                                               gdouble           x,
98                                               gdouble           y,
99                                               gdouble           width,
100                                               gdouble           height);
101 static void gtk_theming_engine_render_arrow  (GtkThemingEngine *engine,
102                                               cairo_t          *cr,
103                                               gdouble           angle,
104                                               gdouble           x,
105                                               gdouble           y,
106                                               gdouble           size);
107 static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
108                                                   cairo_t          *cr,
109                                                   gdouble           x,
110                                                   gdouble           y,
111                                                   gdouble           width,
112                                                   gdouble           height);
113 static void gtk_theming_engine_render_frame  (GtkThemingEngine *engine,
114                                               cairo_t          *cr,
115                                               gdouble           x,
116                                               gdouble           y,
117                                               gdouble           width,
118                                               gdouble           height);
119 static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
120                                                 cairo_t          *cr,
121                                                 gdouble           x,
122                                                 gdouble           y,
123                                                 gdouble           width,
124                                                 gdouble           height);
125 static void gtk_theming_engine_render_focus    (GtkThemingEngine *engine,
126                                                 cairo_t          *cr,
127                                                 gdouble           x,
128                                                 gdouble           y,
129                                                 gdouble           width,
130                                                 gdouble           height);
131 static void gtk_theming_engine_render_layout   (GtkThemingEngine *engine,
132                                                 cairo_t          *cr,
133                                                 gdouble           x,
134                                                 gdouble           y,
135                                                 PangoLayout      *layout);
136 static void gtk_theming_engine_render_line     (GtkThemingEngine *engine,
137                                                 cairo_t          *cr,
138                                                 gdouble           x0,
139                                                 gdouble           y0,
140                                                 gdouble           x1,
141                                                 gdouble           y1);
142 static void gtk_theming_engine_render_slider   (GtkThemingEngine *engine,
143                                                 cairo_t          *cr,
144                                                 gdouble           x,
145                                                 gdouble           y,
146                                                 gdouble           width,
147                                                 gdouble           height,
148                                                 GtkOrientation    orientation);
149 static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
150                                                  cairo_t          *cr,
151                                                  gdouble           x,
152                                                  gdouble           y,
153                                                  gdouble           width,
154                                                  gdouble           height,
155                                                  GtkPositionType   gap_side,
156                                                  gdouble           xy0_gap,
157                                                  gdouble           xy1_gap);
158 static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
159                                                  cairo_t          *cr,
160                                                  gdouble           x,
161                                                  gdouble           y,
162                                                  gdouble           width,
163                                                  gdouble           height,
164                                                  GtkPositionType   gap_side);
165 static void gtk_theming_engine_render_handle    (GtkThemingEngine *engine,
166                                                  cairo_t          *cr,
167                                                  gdouble           x,
168                                                  gdouble           y,
169                                                  gdouble           width,
170                                                  gdouble           height);
171 static void gtk_theming_engine_render_activity  (GtkThemingEngine *engine,
172                                                  cairo_t          *cr,
173                                                  gdouble           x,
174                                                  gdouble           y,
175                                                  gdouble           width,
176                                                  gdouble           height);
177 static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine    *engine,
178                                                           const GtkIconSource *source,
179                                                           GtkIconSize          size);
180 static void gtk_theming_engine_render_icon (GtkThemingEngine *engine,
181                                             cairo_t *cr,
182                                             GdkPixbuf *pixbuf,
183                                             gdouble x,
184                                             gdouble y);
185
186 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
187
188
189 typedef struct GtkThemingModule GtkThemingModule;
190 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
191
192 struct GtkThemingModule
193 {
194   GTypeModule parent_instance;
195   GModule *module;
196   gchar *name;
197
198   void (*init) (GTypeModule *module);
199   void (*exit) (void);
200   GtkThemingEngine * (*create_engine) (void);
201 };
202
203 struct GtkThemingModuleClass
204 {
205   GTypeModuleClass parent_class;
206 };
207
208 #define GTK_TYPE_THEMING_MODULE  (gtk_theming_module_get_type ())
209 #define GTK_THEMING_MODULE(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
210 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
211
212 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
213
214 static void
215 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
216 {
217   GObjectClass *object_class = G_OBJECT_CLASS (klass);
218
219   object_class->finalize = gtk_theming_engine_finalize;
220   object_class->set_property = gtk_theming_engine_impl_set_property;
221   object_class->get_property = gtk_theming_engine_impl_get_property;
222
223   klass->render_icon = gtk_theming_engine_render_icon;
224   klass->render_check = gtk_theming_engine_render_check;
225   klass->render_option = gtk_theming_engine_render_option;
226   klass->render_arrow = gtk_theming_engine_render_arrow;
227   klass->render_background = gtk_theming_engine_render_background;
228   klass->render_frame = gtk_theming_engine_render_frame;
229   klass->render_expander = gtk_theming_engine_render_expander;
230   klass->render_focus = gtk_theming_engine_render_focus;
231   klass->render_layout = gtk_theming_engine_render_layout;
232   klass->render_line = gtk_theming_engine_render_line;
233   klass->render_slider = gtk_theming_engine_render_slider;
234   klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
235   klass->render_extension = gtk_theming_engine_render_extension;
236   klass->render_handle = gtk_theming_engine_render_handle;
237   klass->render_activity = gtk_theming_engine_render_activity;
238   klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
239
240   /**
241    * GtkThemingEngine:name:
242    *
243    * The theming engine name, this name will be used when registering
244    * custom properties, for a theming engine named "Clearlooks" registering
245    * a "glossy" custom property, it could be referenced in the CSS file as
246    *
247    * <programlisting>
248    * -Clearlooks-glossy: true;
249    * </programlisting>
250    *
251    * Since: 3.0
252    */
253   g_object_class_install_property (object_class,
254                                    PROP_NAME,
255                                    g_param_spec_string ("name",
256                                                         P_("Name"),
257                                                         P_("Theming engine name"),
258                                                         NULL,
259                                                         G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));
260
261   g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
262 }
263
264 static void
265 gtk_theming_engine_init (GtkThemingEngine *engine)
266 {
267   engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
268 }
269
270 static void
271 gtk_theming_engine_finalize (GObject *object)
272 {
273   GtkThemingEnginePrivate *priv;
274
275   priv = GTK_THEMING_ENGINE (object)->priv;
276   g_free (priv->name);
277
278   G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
279 }
280
281 static void
282 gtk_theming_engine_impl_set_property (GObject      *object,
283                                       guint         prop_id,
284                                       const GValue *value,
285                                       GParamSpec   *pspec)
286 {
287   GtkThemingEnginePrivate *priv;
288
289   priv = GTK_THEMING_ENGINE (object)->priv;
290
291   switch (prop_id)
292     {
293     case PROP_NAME:
294       if (priv->name)
295         g_free (priv->name);
296
297       priv->name = g_value_dup_string (value);
298       break;
299     default:
300       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301       break;
302     }
303 }
304
305 static void
306 gtk_theming_engine_impl_get_property (GObject    *object,
307                                       guint       prop_id,
308                                       GValue     *value,
309                                       GParamSpec *pspec)
310 {
311   GtkThemingEnginePrivate *priv;
312
313   priv = GTK_THEMING_ENGINE (object)->priv;
314
315   switch (prop_id)
316     {
317     case PROP_NAME:
318       g_value_set_string (value, priv->name);
319       break;
320     default:
321       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
322       break;
323     }
324 }
325
326 void
327 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
328                                  GtkStyleContext  *context)
329 {
330   GtkThemingEnginePrivate *priv;
331
332   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
333   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
334
335   priv = engine->priv;
336   priv->context = context;
337 }
338
339 /**
340  * gtk_theming_engine_register_property: (skip)
341  * @name_space: namespace for the property name
342  * @parse_func: parsing function to use, or %NULL
343  * @pspec: the #GParamSpec for the new property
344  *
345  * Registers a property so it can be used in the CSS file format,
346  * on the CSS file the property will look like
347  * "-${@name_space}-${property_name}". being
348  * ${property_name} the given to @pspec. @name_space will usually
349  * be the theme engine name.
350  *
351  * For any type a @parse_func may be provided, being this function
352  * used for turning any property value (between ':' and ';') in
353  * CSS to the #GValue needed. For basic types there is already
354  * builtin parsing support, so %NULL may be provided for these
355  * cases.
356  *
357  * <note>
358  * Engines must ensure property registration happens exactly once,
359  * usually GTK+ deals with theming engines as singletons, so this
360  * should be guaranteed to happen once, but bear this in mind
361  * when creating #GtkThemeEngine<!-- -->s yourself.
362  * </note>
363  *
364  * <note>
365  * In order to make use of the custom registered properties in
366  * the CSS file, make sure the engine is loaded first by specifying
367  * the engine property, either in a previous rule or within the same
368  * one.
369  * <programlisting>
370  * &ast; {
371  *     engine: someengine;
372  *     -SomeEngine-custom-property: 2;
373  * }
374  * </programlisting>
375  * </note>
376  *
377  * Since: 3.0
378  **/
379 void
380 gtk_theming_engine_register_property (const gchar            *name_space,
381                                       GtkStylePropertyParser  parse_func,
382                                       GParamSpec             *pspec)
383 {
384   gchar *name;
385
386   g_return_if_fail (name_space != NULL);
387   g_return_if_fail (strchr (name_space, ' ') == NULL);
388   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
389
390   /* FIXME: hack hack hack, replacing pspec->name to include namespace */
391   name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
392   pspec->name = (char *)g_intern_string (name);
393   g_free (name);
394
395   gtk_style_properties_register_property (parse_func, pspec);
396 }
397
398 /**
399  * gtk_theming_engine_get_property:
400  * @engine: a #GtkThemingEngine
401  * @property: the property name
402  * @state: state to retrieve the value for
403  * @value: (out) (transfer full): return location for the property value,
404  *         you must free this memory using g_value_unset() once you are
405  *         done with it.
406  *
407  * Gets a property value as retrieved from the style settings that apply
408  * to the currently rendered element.
409  *
410  * Since: 3.0
411  **/
412 void
413 gtk_theming_engine_get_property (GtkThemingEngine *engine,
414                                  const gchar      *property,
415                                  GtkStateFlags     state,
416                                  GValue           *value)
417 {
418   GtkThemingEnginePrivate *priv;
419
420   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
421   g_return_if_fail (property != NULL);
422   g_return_if_fail (value != NULL);
423
424   priv = engine->priv;
425   gtk_style_context_get_property (priv->context, property, state, value);
426 }
427
428 /**
429  * gtk_theming_engine_get_valist:
430  * @engine: a #GtkThemingEngine
431  * @state: state to retrieve values for
432  * @args: va_list of property name/return location pairs, followed by %NULL
433  *
434  * Retrieves several style property values that apply to the currently
435  * rendered element.
436  *
437  * Since: 3.0
438  **/
439 void
440 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
441                                GtkStateFlags     state,
442                                va_list           args)
443 {
444   GtkThemingEnginePrivate *priv;
445
446   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
447
448   priv = engine->priv;
449   gtk_style_context_get_valist (priv->context, state, args);
450 }
451
452 /**
453  * gtk_theming_engine_get:
454  * @engine: a #GtkThemingEngine
455  * @state: state to retrieve values for
456  * @...: property name /return value pairs, followed by %NULL
457  *
458  * Retrieves several style property values that apply to the currently
459  * rendered element.
460  *
461  * Since: 3.0
462  **/
463 void
464 gtk_theming_engine_get (GtkThemingEngine *engine,
465                         GtkStateFlags     state,
466                         ...)
467 {
468   GtkThemingEnginePrivate *priv;
469   va_list args;
470
471   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
472
473   priv = engine->priv;
474
475   va_start (args, state);
476   gtk_style_context_get_valist (priv->context, state, args);
477   va_end (args);
478 }
479
480 /**
481  * gtk_theming_engine_get_style_property:
482  * @engine: a #GtkThemingEngine
483  * @property_name: the name of the widget style property
484  * @value: Return location for the property value, free with
485  *         g_value_unset() after use.
486  *
487  * Gets the value for a widget style property.
488  *
489  * Since: 3.0
490  **/
491 void
492 gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
493                                        const gchar      *property_name,
494                                        GValue           *value)
495 {
496   GtkThemingEnginePrivate *priv;
497
498   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
499   g_return_if_fail (property_name != NULL);
500
501   priv = engine->priv;
502   gtk_style_context_get_style_property (priv->context, property_name, value);
503 }
504
505 /**
506  * gtk_theming_engine_get_style_valist:
507  * @engine: a #GtkThemingEngine
508  * @args: va_list of property name/return location pairs, followed by %NULL
509  *
510  * Retrieves several widget style properties from @engine according to the
511  * currently rendered content's style.
512  *
513  * Since: 3.0
514  **/
515 void
516 gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
517                                      va_list           args)
518 {
519   GtkThemingEnginePrivate *priv;
520
521   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
522
523   priv = engine->priv;
524   gtk_style_context_get_style_valist (priv->context, args);
525 }
526
527 /**
528  * gtk_theming_engine_get_style:
529  * @engine: a #GtkThemingEngine
530  * @...: property name /return value pairs, followed by %NULL
531  *
532  * Retrieves several widget style properties from @engine according
533  * to the currently rendered content's style.
534  *
535  * Since: 3.0
536  **/
537 void
538 gtk_theming_engine_get_style (GtkThemingEngine *engine,
539                               ...)
540 {
541   GtkThemingEnginePrivate *priv;
542   va_list args;
543
544   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
545
546   priv = engine->priv;
547
548   va_start (args, engine);
549   gtk_style_context_get_style_valist (priv->context, args);
550   va_end (args);
551 }
552
553 /**
554  * gtk_theming_engine_lookup_color:
555  * @engine: a #GtkThemingEngine
556  * @color_name: color name to lookup
557  * @color: (out): Return location for the looked up color
558  *
559  * Looks up and resolves a color name in the current style's color map.
560  *
561  * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
562  **/
563 gboolean
564 gtk_theming_engine_lookup_color (GtkThemingEngine *engine,
565                                  const gchar      *color_name,
566                                  GdkRGBA          *color)
567 {
568   GtkThemingEnginePrivate *priv;
569
570   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
571   g_return_val_if_fail (color_name != NULL, FALSE);
572
573   priv = engine->priv;
574   return gtk_style_context_lookup_color (priv->context, color_name, color);
575 }
576
577 /**
578  * gtk_theming_engine_get_state:
579  * @engine: a #GtkThemingEngine
580  *
581  * returns the state used when rendering.
582  *
583  * Returns: the state flags
584  *
585  * Since: 3.0
586  **/
587 GtkStateFlags
588 gtk_theming_engine_get_state (GtkThemingEngine *engine)
589 {
590   GtkThemingEnginePrivate *priv;
591
592   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
593
594   priv = engine->priv;
595   return gtk_style_context_get_state (priv->context);
596 }
597
598 /**
599  * gtk_theming_engine_state_is_running:
600  * @engine: a #GtkThemingEngine
601  * @state: a widget state
602  * @progress: (out): return location for the transition progress
603  *
604  * Returns %TRUE if there is a transition animation running for the
605  * current region (see gtk_style_context_push_animatable_region()).
606  *
607  * If @progress is not %NULL, the animation progress will be returned
608  * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
609  * it's closest to being %TRUE. This means transition animations will
610  * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
611  * it's being set to %FALSE.
612  *
613  * Returns: %TRUE if there is a running transition animation for @state.
614  *
615  * Since: 3.0
616  **/
617 gboolean
618 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
619                                      GtkStateType      state,
620                                      gdouble          *progress)
621 {
622   GtkThemingEnginePrivate *priv;
623
624   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
625
626   priv = engine->priv;
627   return gtk_style_context_state_is_running (priv->context, state, progress);
628 }
629
630 /**
631  * gtk_theming_engine_get_path:
632  * @engine: a #GtkThemingEngine
633  *
634  * Returns the widget path used for style matching.
635  *
636  * Returns: (transfer none): A #GtkWidgetPath
637  *
638  * Since: 3.0
639  **/
640 const GtkWidgetPath *
641 gtk_theming_engine_get_path (GtkThemingEngine *engine)
642 {
643   GtkThemingEnginePrivate *priv;
644
645   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
646
647   priv = engine->priv;
648   return gtk_style_context_get_path (priv->context);
649 }
650
651 /**
652  * gtk_theming_engine_has_class:
653  * @engine: a #GtkThemingEngine
654  * @style_class: class name to look up
655  *
656  * Returns %TRUE if the currently rendered contents have
657  * defined the given class name.
658  *
659  * Returns: %TRUE if @engine has @class_name defined
660  *
661  * Since: 3.0
662  **/
663 gboolean
664 gtk_theming_engine_has_class (GtkThemingEngine *engine,
665                               const gchar      *style_class)
666 {
667   GtkThemingEnginePrivate *priv;
668
669   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
670
671   priv = engine->priv;
672   return gtk_style_context_has_class (priv->context, style_class);
673 }
674
675 /**
676  * gtk_theming_engine_has_region:
677  * @engine: a #GtkThemingEngine
678  * @style_region: a region name
679  * @flags: (out) (allow-none): return location for region flags
680  *
681  * Returns %TRUE if the currently rendered contents have the
682  * region defined. If @flags_return is not %NULL, it is set
683  * to the flags affecting the region.
684  *
685  * Returns: %TRUE if region is defined
686  *
687  * Since: 3.0
688  **/
689 gboolean
690 gtk_theming_engine_has_region (GtkThemingEngine *engine,
691                                const gchar      *style_region,
692                                GtkRegionFlags   *flags)
693 {
694   GtkThemingEnginePrivate *priv;
695
696   if (flags)
697     *flags = 0;
698
699   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
700
701   priv = engine->priv;
702   return gtk_style_context_has_region (priv->context, style_region, flags);
703 }
704
705 /**
706  * gtk_theming_engine_get_direction:
707  * @engine: a #GtkThemingEngine
708  *
709  * Returns the widget direction used for rendering.
710  *
711  * Returns: the widget direction
712  *
713  * Since: 3.0
714  **/
715 GtkTextDirection
716 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
717 {
718   GtkThemingEnginePrivate *priv;
719
720   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
721
722   priv = engine->priv;
723   return gtk_style_context_get_direction (priv->context);
724 }
725
726 /**
727  * gtk_theming_engine_get_junction_sides:
728  * @engine: a #GtkThemingEngine
729  *
730  * Returns the widget direction used for rendering.
731  *
732  * Returns: the widget direction
733  *
734  * Since: 3.0
735  **/
736 GtkJunctionSides
737 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
738 {
739   GtkThemingEnginePrivate *priv;
740
741   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
742
743   priv = engine->priv;
744   return gtk_style_context_get_junction_sides (priv->context);
745 }
746
747 /**
748  * gtk_theming_engine_get_color:
749  * @engine: a #GtkThemingEngine
750  * @state: state to retrieve the color for
751  * @color: (out): return value for the foreground color
752  *
753  * Gets the foreground color for a given state.
754  *
755  * Since: 3.0
756  **/
757 void
758 gtk_theming_engine_get_color (GtkThemingEngine *engine,
759                               GtkStateFlags     state,
760                               GdkRGBA          *color)
761 {
762   GtkThemingEnginePrivate *priv;
763
764   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
765
766   priv = engine->priv;
767   gtk_style_context_get_color (priv->context, state, color);
768 }
769
770 /**
771  * gtk_theming_engine_get_background_color:
772  * @engine: a #GtkThemingEngine
773  * @state: state to retrieve the color for
774  * @color: (out): return value for the background color
775  *
776  * Gets the background color for a given state.
777  *
778  * Since: 3.0
779  **/
780 void
781 gtk_theming_engine_get_background_color (GtkThemingEngine *engine,
782                                          GtkStateFlags     state,
783                                          GdkRGBA          *color)
784 {
785   GtkThemingEnginePrivate *priv;
786
787   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
788
789   priv = engine->priv;
790   gtk_style_context_get_background_color (priv->context, state, color);
791 }
792
793 /**
794  * gtk_theming_engine_get_border_color:
795  * @engine: a #GtkThemingEngine
796  * @state: state to retrieve the color for
797  * @color: (out): return value for the border color
798  *
799  * Gets the border color for a given state.
800  *
801  * Since: 3.0
802  **/
803 void
804 gtk_theming_engine_get_border_color (GtkThemingEngine *engine,
805                                      GtkStateFlags     state,
806                                      GdkRGBA          *color)
807 {
808   GtkThemingEnginePrivate *priv;
809
810   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
811
812   priv = engine->priv;
813   gtk_style_context_get_border_color (priv->context, state, color);
814 }
815
816 /**
817  * gtk_theming_engine_get_border:
818  * @engine: a #GtkthemingEngine
819  * @state: state to retrieve the border for
820  * @border: (out): return value for the border settings
821  *
822  * Gets the border for a given state as a #GtkBorder.
823  *
824  * Since: 3.0
825  **/
826 void
827 gtk_theming_engine_get_border (GtkThemingEngine *engine,
828                                GtkStateFlags     state,
829                                GtkBorder        *border)
830 {
831   GtkThemingEnginePrivate *priv;
832
833   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
834
835   priv = engine->priv;
836   gtk_style_context_get_border (priv->context, state, border);
837 }
838
839 /**
840  * gtk_theming_engine_get_padding:
841  * @engine: a #GtkthemingEngine
842  * @state: state to retrieve the padding for
843  * @padding: (out): return value for the padding settings
844  *
845  * Gets the padding for a given state as a #GtkBorder.
846  *
847  * Since: 3.0
848  **/
849 void
850 gtk_theming_engine_get_padding (GtkThemingEngine *engine,
851                                 GtkStateFlags     state,
852                                 GtkBorder        *padding)
853 {
854   GtkThemingEnginePrivate *priv;
855
856   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
857
858   priv = engine->priv;
859   gtk_style_context_get_padding (priv->context, state, padding);
860 }
861
862 /**
863  * gtk_theming_engine_get_margin:
864  * @engine: a #GtkThemingEngine
865  * @state: state to retrieve the border for
866  * @margin: (out): return value for the margin settings
867  *
868  * Gets the margin for a given state as a #GtkBorder.
869  *
870  * Since: 3.0
871  **/
872 void
873 gtk_theming_engine_get_margin (GtkThemingEngine *engine,
874                                GtkStateFlags     state,
875                                GtkBorder        *margin)
876 {
877   GtkThemingEnginePrivate *priv;
878
879   g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
880
881   priv = engine->priv;
882   gtk_style_context_get_margin (priv->context, state, margin);
883 }
884
885 /**
886  * gtk_theming_engine_get_font:
887  * @engine: a #GtkThemingEngine
888  * @state: state to retrieve the font for
889  *
890  * Returns the font description for a given state.
891  *
892  * Returns: (transfer none): the #PangoFontDescription for the given
893  *          state. This object is owned by GTK+ and should not be
894  *          freed.
895  *
896  * Since: 3.0
897  **/
898 const PangoFontDescription *
899 gtk_theming_engine_get_font (GtkThemingEngine *engine,
900                              GtkStateFlags     state)
901 {
902   GtkThemingEnginePrivate *priv;
903
904   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
905
906   priv = engine->priv;
907   return gtk_style_context_get_font (priv->context, state);
908 }
909
910 /* GtkThemingModule */
911
912 static gboolean
913 gtk_theming_module_load (GTypeModule *type_module)
914 {
915   GtkThemingModule *theming_module;
916   GModule *module;
917   gchar *name, *module_path;
918
919   theming_module = GTK_THEMING_MODULE (type_module);
920   name = theming_module->name;
921   module_path = _gtk_find_module (name, "theming-engines");
922
923   if (!module_path)
924     return FALSE;
925
926   module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
927   g_free (module_path);
928
929   if (!module)
930     return FALSE;
931
932   if (!g_module_symbol (module, "theme_init",
933                         (gpointer *) &theming_module->init) ||
934       !g_module_symbol (module, "theme_exit",
935                         (gpointer *) &theming_module->exit) ||
936       !g_module_symbol (module, "create_engine",
937                         (gpointer *) &theming_module->create_engine))
938     {
939       g_module_close (module);
940
941       return FALSE;
942     }
943
944   theming_module->module = module;
945
946   theming_module->init (G_TYPE_MODULE (theming_module));
947
948   return TRUE;
949 }
950
951 static void
952 gtk_theming_module_unload (GTypeModule *type_module)
953 {
954   GtkThemingModule *theming_module;
955
956   theming_module = GTK_THEMING_MODULE (type_module);
957
958   theming_module->exit ();
959
960   g_module_close (theming_module->module);
961
962   theming_module->module = NULL;
963   theming_module->init = NULL;
964   theming_module->exit = NULL;
965   theming_module->create_engine = NULL;
966 }
967
968 static void
969 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
970 {
971   GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
972
973   module_class->load = gtk_theming_module_load;
974   module_class->unload = gtk_theming_module_unload;
975 }
976
977 static void
978 gtk_theming_module_init (GtkThemingModule *module)
979 {
980 }
981
982 /**
983  * gtk_theming_engine_load:
984  * @name: Theme engine name to load
985  *
986  * Loads and initializes a theming engine module from the
987  * standard directories.
988  *
989  * Returns: (transfer none): A theming engine, or %NULL if
990  * the engine @name doesn't exist.
991  **/
992 GtkThemingEngine *
993 gtk_theming_engine_load (const gchar *name)
994 {
995   static GHashTable *engines = NULL;
996   static GtkThemingEngine *default_engine;
997   GtkThemingEngine *engine = NULL;
998
999   if (name)
1000     {
1001       if (!engines)
1002         engines = g_hash_table_new (g_str_hash, g_str_equal);
1003
1004       engine = g_hash_table_lookup (engines, name);
1005
1006       if (!engine)
1007         {
1008           GtkThemingModule *module;
1009
1010           module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
1011           g_type_module_set_name (G_TYPE_MODULE (module), name);
1012           module->name = g_strdup (name);
1013
1014           if (module && g_type_module_use (G_TYPE_MODULE (module)))
1015             {
1016               engine = (module->create_engine) ();
1017
1018               if (engine)
1019                 g_hash_table_insert (engines, module->name, engine);
1020             }
1021         }
1022     }
1023   else
1024     {
1025       if (G_UNLIKELY (!default_engine))
1026         default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
1027
1028       engine = default_engine;
1029     }
1030
1031   return engine;
1032 }
1033
1034 /**
1035  * gtk_theming_engine_get_screen:
1036  * @engine: a #GtkThemingEngine
1037  *
1038  * Returns the #GdkScreen to which @engine currently rendering to.
1039  *
1040  * Returns: (transfer none): a #GdkScreen, or %NULL.
1041  **/
1042 GdkScreen *
1043 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
1044 {
1045   GtkThemingEnginePrivate *priv;
1046
1047   g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
1048
1049   priv = engine->priv;
1050   return gtk_style_context_get_screen (priv->context);
1051 }
1052
1053 /* Paint method implementations */
1054 static void
1055 gtk_theming_engine_render_check (GtkThemingEngine *engine,
1056                                  cairo_t          *cr,
1057                                  gdouble           x,
1058                                  gdouble           y,
1059                                  gdouble           width,
1060                                  gdouble           height)
1061 {
1062   GdkRGBA fg_color, bg_color;
1063   GtkStateFlags flags;
1064   gint exterior_size, interior_size, thickness, pad;
1065   GtkBorderStyle border_style;
1066   GtkBorder border;
1067   gint border_width;
1068
1069   flags = gtk_theming_engine_get_state (engine);
1070   cairo_save (cr);
1071
1072   gtk_theming_engine_get_color (engine, flags, &fg_color);
1073   gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1074   gtk_theming_engine_get_border (engine, flags, &border);
1075
1076   gtk_theming_engine_get (engine, flags,
1077                           "border-style", &border_style,
1078                           NULL);
1079
1080   border_width = MIN (MIN (border.top, border.bottom),
1081                       MIN (border.left, border.right));
1082   exterior_size = MIN (width, height);
1083
1084   if (exterior_size % 2 == 0) /* Ensure odd */
1085     exterior_size -= 1;
1086
1087   /* FIXME: thickness */
1088   thickness = 1;
1089   pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1090   interior_size = MAX (1, exterior_size - 2 * pad);
1091
1092   if (interior_size < 7)
1093     {
1094       interior_size = 7;
1095       pad = MAX (0, (exterior_size - interior_size) / 2);
1096     }
1097
1098   x -= (1 + exterior_size - (gint) width) / 2;
1099   y -= (1 + exterior_size - (gint) height) / 2;
1100
1101   if (border_style == GTK_BORDER_STYLE_SOLID)
1102     {
1103       GdkRGBA border_color;
1104
1105       cairo_set_line_width (cr, border_width);
1106       gtk_theming_engine_get_border_color (engine, flags, &border_color);
1107
1108       cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
1109       gdk_cairo_set_source_rgba (cr, &bg_color);
1110       cairo_fill_preserve (cr);
1111
1112       gdk_cairo_set_source_rgba (cr, &border_color);
1113       cairo_stroke (cr);
1114     }
1115
1116   gdk_cairo_set_source_rgba (cr, &fg_color);
1117
1118   if (flags & GTK_STATE_FLAG_INCONSISTENT)
1119     {
1120       int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1121
1122       cairo_rectangle (cr,
1123                        x + pad,
1124                        y + pad + (1 + interior_size - line_thickness) / 2,
1125                        interior_size,
1126                        line_thickness);
1127       cairo_fill (cr);
1128     }
1129   else
1130     {
1131       gdouble progress;
1132       gboolean running;
1133
1134       running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1135
1136       if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
1137         {
1138           if (!running)
1139             progress = 1;
1140
1141           cairo_translate (cr,
1142                            x + pad, y + pad);
1143
1144           cairo_scale (cr, interior_size / 7., interior_size / 7.);
1145
1146           cairo_rectangle (cr, 0, 0, 7 * progress, 7);
1147           cairo_clip (cr);
1148
1149           cairo_move_to  (cr, 7.0, 0.0);
1150           cairo_line_to  (cr, 7.5, 1.0);
1151           cairo_curve_to (cr, 5.3, 2.0,
1152                           4.3, 4.0,
1153                           3.5, 7.0);
1154           cairo_curve_to (cr, 3.0, 5.7,
1155                           1.3, 4.7,
1156                           0.0, 4.7);
1157           cairo_line_to  (cr, 0.2, 3.5);
1158           cairo_curve_to (cr, 1.1, 3.5,
1159                           2.3, 4.3,
1160                           3.0, 5.0);
1161           cairo_curve_to (cr, 1.0, 3.9,
1162                           2.4, 4.1,
1163                           3.2, 4.9);
1164           cairo_curve_to (cr, 3.5, 3.1,
1165                           5.2, 2.0,
1166                           7.0, 0.0);
1167
1168           cairo_fill (cr);
1169         }
1170     }
1171
1172   cairo_restore (cr);
1173 }
1174
1175 static void
1176 gtk_theming_engine_render_option (GtkThemingEngine *engine,
1177                                   cairo_t          *cr,
1178                                   gdouble           x,
1179                                   gdouble           y,
1180                                   gdouble           width,
1181                                   gdouble           height)
1182 {
1183   GtkStateFlags flags;
1184   GdkRGBA fg_color, bg_color;
1185   gint exterior_size, interior_size, pad, thickness, border_width;
1186   GtkBorderStyle border_style;
1187   GtkBorder border;
1188
1189   flags = gtk_theming_engine_get_state (engine);
1190
1191   cairo_save (cr);
1192
1193   gtk_theming_engine_get_color (engine, flags, &fg_color);
1194   gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1195   gtk_theming_engine_get_border (engine, flags, &border);
1196
1197   gtk_theming_engine_get (engine, flags,
1198                           "border-style", &border_style,
1199                           NULL);
1200
1201   exterior_size = MIN (width, height);
1202   border_width = MIN (MIN (border.top, border.bottom),
1203                       MIN (border.left, border.right));
1204
1205   if (exterior_size % 2 == 0) /* Ensure odd */
1206     exterior_size -= 1;
1207
1208   x -= (1 + exterior_size - width) / 2;
1209   y -= (1 + exterior_size - height) / 2;
1210
1211   if (border_style == GTK_BORDER_STYLE_SOLID)
1212     {
1213       GdkRGBA border_color;
1214
1215       cairo_set_line_width (cr, border_width);
1216       gtk_theming_engine_get_border_color (engine, flags, &border_color);
1217
1218       cairo_new_sub_path (cr);
1219       cairo_arc (cr,
1220                  x + exterior_size / 2.,
1221                  y + exterior_size / 2.,
1222                  (exterior_size - 1) / 2.,
1223                  0, 2 * G_PI);
1224
1225       gdk_cairo_set_source_rgba (cr, &bg_color);
1226       cairo_fill_preserve (cr);
1227
1228       gdk_cairo_set_source_rgba (cr, &border_color);
1229       cairo_stroke (cr);
1230     }
1231
1232   gdk_cairo_set_source_rgba (cr, &fg_color);
1233
1234   /* FIXME: thickness */
1235   thickness = 1;
1236
1237   if (flags & GTK_STATE_FLAG_INCONSISTENT)
1238     {
1239       gint line_thickness;
1240
1241       pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1242       interior_size = MAX (1, exterior_size - 2 * pad);
1243
1244       if (interior_size < 7)
1245         {
1246           interior_size = 7;
1247           pad = MAX (0, (exterior_size - interior_size) / 2);
1248         }
1249
1250       line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1251
1252       cairo_rectangle (cr,
1253                        x + pad,
1254                        y + pad + (interior_size - line_thickness) / 2.,
1255                        interior_size,
1256                        line_thickness);
1257       cairo_fill (cr);
1258     }
1259   if (flags & GTK_STATE_FLAG_ACTIVE)
1260     {
1261       pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1262       interior_size = MAX (1, exterior_size - 2 * pad);
1263
1264       if (interior_size < 5)
1265         {
1266           interior_size = 7;
1267           pad = MAX (0, (exterior_size - interior_size) / 2);
1268         }
1269
1270       cairo_new_sub_path (cr);
1271       cairo_arc (cr,
1272                  x + pad + interior_size / 2.,
1273                  y + pad + interior_size / 2.,
1274                  interior_size / 2.,
1275                  0, 2 * G_PI);
1276       cairo_fill (cr);
1277     }
1278
1279   cairo_restore (cr);
1280 }
1281
1282 static void
1283 add_path_arrow (cairo_t *cr,
1284                 gdouble  angle,
1285                 gdouble  x,
1286                 gdouble  y,
1287                 gdouble  size)
1288 {
1289   cairo_save (cr);
1290
1291   cairo_translate (cr, x + (size / 2), y + (size / 2));
1292   cairo_rotate (cr, angle);
1293
1294   cairo_move_to (cr, 0, - (size / 4));
1295   cairo_line_to (cr, - (size / 2), (size / 4));
1296   cairo_line_to (cr, (size / 2), (size / 4));
1297   cairo_close_path (cr);
1298
1299   cairo_restore (cr);
1300 }
1301
1302 static void
1303 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1304                                  cairo_t          *cr,
1305                                  gdouble           angle,
1306                                  gdouble           x,
1307                                  gdouble           y,
1308                                  gdouble           size)
1309 {
1310   GtkStateFlags flags;
1311   GdkRGBA fg_color;
1312
1313   cairo_save (cr);
1314
1315   flags = gtk_theming_engine_get_state (engine);
1316   gtk_theming_engine_get_color (engine, flags, &fg_color);
1317
1318   if (flags & GTK_STATE_FLAG_INSENSITIVE)
1319     {
1320       add_path_arrow (cr, angle, x + 1, y + 1, size);
1321       cairo_set_source_rgb (cr, 1, 1, 1);
1322       cairo_fill (cr);
1323     }
1324
1325   add_path_arrow (cr, angle, x, y, size);
1326   gdk_cairo_set_source_rgba (cr, &fg_color);
1327   cairo_fill (cr);
1328
1329   cairo_restore (cr);
1330 }
1331
1332 static void
1333 add_path_line (cairo_t        *cr,
1334                gdouble         x1,
1335                gdouble         y1,
1336                gdouble         x2,
1337                gdouble         y2)
1338 {
1339   /* Adjust endpoints */
1340   if (y1 == y2)
1341     {
1342       y1 += 0.5;
1343       y2 += 0.5;
1344       x2 += 1;
1345     }
1346   else if (x1 == x2)
1347     {
1348       x1 += 0.5;
1349       x2 += 0.5;
1350       y2 += 1;
1351     }
1352
1353   cairo_move_to (cr, x1, y1);
1354   cairo_line_to (cr, x2, y2);
1355 }
1356
1357 static void
1358 color_shade (const GdkRGBA *color,
1359              gdouble        factor,
1360              GdkRGBA       *color_return)
1361 {
1362   GtkSymbolicColor *literal, *shade;
1363
1364   literal = gtk_symbolic_color_new_literal (color);
1365   shade = gtk_symbolic_color_new_shade (literal, factor);
1366   gtk_symbolic_color_unref (literal);
1367
1368   gtk_symbolic_color_resolve (shade, NULL, color_return);
1369   gtk_symbolic_color_unref (shade);
1370 }
1371
1372 static void
1373 render_background_internal (GtkThemingEngine *engine,
1374                             cairo_t          *cr,
1375                             gdouble           x,
1376                             gdouble           y,
1377                             gdouble           width,
1378                             gdouble           height,
1379                             GtkJunctionSides  junction)
1380 {
1381   GdkRGBA bg_color;
1382   cairo_pattern_t *pattern;
1383   GtkStateFlags flags;
1384   gboolean running;
1385   gdouble progress;
1386   GtkRoundedBox border_box;
1387   GtkShadow *box_shadow;
1388   GtkBorder border;
1389
1390   flags = gtk_theming_engine_get_state (engine);
1391
1392   gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1393
1394   gtk_theming_engine_get (engine, flags,
1395                           "background-image", &pattern,
1396                           "box-shadow", &box_shadow,
1397                           NULL);
1398
1399   cairo_save (cr);
1400   cairo_translate (cr, x, y);
1401
1402   running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1403
1404   if (gtk_theming_engine_has_class (engine, "background"))
1405     {
1406       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
1407       cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1408       cairo_paint (cr);
1409     }
1410
1411   if (running)
1412     {
1413       cairo_pattern_t *other_pattern;
1414       GtkStateFlags other_flags;
1415       GdkRGBA other_bg;
1416       cairo_pattern_t *new_pattern = NULL;
1417
1418       if (flags & GTK_STATE_FLAG_PRELIGHT)
1419         {
1420           other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
1421           progress = 1 - progress;
1422         }
1423       else
1424         other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
1425
1426       gtk_theming_engine_get_background_color (engine, other_flags, &other_bg);
1427       gtk_theming_engine_get (engine, other_flags,
1428                               "background-image", &other_pattern,
1429                               NULL);
1430
1431       if (pattern && other_pattern)
1432         {
1433           cairo_pattern_type_t type, other_type;
1434           gint n0, n1;
1435
1436           cairo_pattern_get_color_stop_count (pattern, &n0);
1437           cairo_pattern_get_color_stop_count (other_pattern, &n1);
1438           type = cairo_pattern_get_type (pattern);
1439           other_type = cairo_pattern_get_type (other_pattern);
1440
1441           if (type == other_type && n0 == n1)
1442             {
1443               gdouble offset0, red0, green0, blue0, alpha0;
1444               gdouble offset1, red1, green1, blue1, alpha1;
1445               gdouble x00, x01, y00, y01, x10, x11, y10, y11;
1446               gdouble r00, r01, r10, r11;
1447               guint i;
1448
1449               if (type == CAIRO_PATTERN_TYPE_LINEAR)
1450                 {
1451                   cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
1452                   cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
1453
1454                   new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
1455                                                              y00 + (y10 - y00) * progress,
1456                                                              x01 + (x11 - x01) * progress,
1457                                                              y01 + (y11 - y01) * progress);
1458                 }
1459               else
1460                 {
1461                   cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
1462                   cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
1463
1464                   new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
1465                                                              y00 + (y10 - y00) * progress,
1466                                                              r00 + (r10 - r00) * progress,
1467                                                              x01 + (x11 - x01) * progress,
1468                                                              y01 + (y11 - y01) * progress,
1469                                                              r01 + (r11 - r01) * progress);
1470                 }
1471
1472               cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
1473               i = 0;
1474
1475               /* Blend both gradients into one */
1476               while (i < n0 && i < n1)
1477                 {
1478                   cairo_pattern_get_color_stop_rgba (pattern, i,
1479                                                      &offset0,
1480                                                      &red0, &green0, &blue0,
1481                                                      &alpha0);
1482                   cairo_pattern_get_color_stop_rgba (other_pattern, i,
1483                                                      &offset1,
1484                                                      &red1, &green1, &blue1,
1485                                                      &alpha1);
1486
1487                   cairo_pattern_add_color_stop_rgba (new_pattern,
1488                                                      offset0 + ((offset1 - offset0) * progress),
1489                                                      red0 + ((red1 - red0) * progress),
1490                                                      green0 + ((green1 - green0) * progress),
1491                                                      blue0 + ((blue1 - blue0) * progress),
1492                                                      alpha0 + ((alpha1 - alpha0) * progress));
1493                   i++;
1494                 }
1495             }
1496           else
1497             {
1498               cairo_save (cr);
1499
1500               cairo_rectangle (cr, 0, 0, width, height);
1501               cairo_clip (cr);
1502
1503               cairo_push_group (cr);
1504
1505               cairo_scale (cr, width, height);
1506               cairo_set_source (cr, other_pattern);
1507               cairo_paint_with_alpha (cr, progress);
1508               cairo_set_source (cr, pattern);
1509               cairo_paint_with_alpha (cr, 1.0 - progress);
1510
1511               new_pattern = cairo_pop_group (cr);
1512
1513               cairo_restore (cr);
1514             }
1515         }
1516       else if (pattern || other_pattern)
1517         {
1518           cairo_pattern_t *p;
1519           const GdkRGBA *c;
1520           gdouble x0, y0, x1, y1, r0, r1;
1521           gint n, i;
1522
1523           /* Blend a pattern with a color */
1524           if (pattern)
1525             {
1526               p = pattern;
1527               c = &other_bg;
1528               progress = 1 - progress;
1529             }
1530           else
1531             {
1532               p = other_pattern;
1533               c = &bg_color;
1534             }
1535
1536           if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
1537             {
1538               cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
1539               new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
1540             }
1541           else
1542             {
1543               cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
1544               new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
1545             }
1546
1547           cairo_pattern_get_color_stop_count (p, &n);
1548
1549           for (i = 0; i < n; i++)
1550             {
1551               gdouble red1, green1, blue1, alpha1;
1552               gdouble offset;
1553
1554               cairo_pattern_get_color_stop_rgba (p, i,
1555                                                  &offset,
1556                                                  &red1, &green1, &blue1,
1557                                                  &alpha1);
1558               cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1559                                                  c->red + ((red1 - c->red) * progress),
1560                                                  c->green + ((green1 - c->green) * progress),
1561                                                  c->blue + ((blue1 - c->blue) * progress),
1562                                                  c->alpha + ((alpha1 - c->alpha) * progress));
1563             }
1564         }
1565       else
1566         {
1567           /* Merge just colors */
1568           new_pattern = cairo_pattern_create_rgba (CLAMP (bg_color.red + ((other_bg.red - bg_color.red) * progress), 0, 1),
1569                                                    CLAMP (bg_color.green + ((other_bg.green - bg_color.green) * progress), 0, 1),
1570                                                    CLAMP (bg_color.blue + ((other_bg.blue - bg_color.blue) * progress), 0, 1),
1571                                                    CLAMP (bg_color.alpha + ((other_bg.alpha - bg_color.alpha) * progress), 0, 1));
1572         }
1573
1574       if (new_pattern)
1575         {
1576           /* Replace pattern to use */
1577           cairo_pattern_destroy (pattern);
1578           pattern = new_pattern;
1579         }
1580
1581       if (other_pattern)
1582         cairo_pattern_destroy (other_pattern);
1583     }
1584
1585   gtk_theming_engine_get_border (engine, flags, &border);
1586
1587   /* In the CSS box model, by default the background positioning area is
1588    * the padding-box, i.e. all the border-box minus the borders themselves,
1589    * which determines also its default size, see
1590    * http://dev.w3.org/csswg/css3-background/#background-origin
1591    *
1592    * In the future we might want to support different origins or clips, but
1593    * right now we just shrink to the default.
1594    */
1595   _gtk_rounded_box_init_rect (&border_box, 0, 0, width, height);
1596   _gtk_rounded_box_apply_border_radius (&border_box, engine, flags, junction);
1597   _gtk_rounded_box_shrink (&border_box,
1598                            border.top, border.right,
1599                            border.bottom, border.left);
1600   _gtk_rounded_box_path (&border_box, cr);
1601
1602   if (pattern)
1603     {
1604       cairo_scale (cr, width, height);
1605       cairo_set_source (cr, pattern);
1606       cairo_scale (cr, 1.0 / width, 1.0 / height);
1607     }
1608   else
1609     gdk_cairo_set_source_rgba (cr, &bg_color);
1610
1611   cairo_fill (cr);
1612
1613   if (pattern)
1614     cairo_pattern_destroy (pattern);
1615
1616   if (box_shadow != NULL)
1617     {
1618       _gtk_box_shadow_render (box_shadow, cr, &border_box);
1619       _gtk_shadow_unref (box_shadow);
1620     }
1621
1622   cairo_restore (cr);
1623 }
1624
1625 static void
1626 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1627                                       cairo_t          *cr,
1628                                       gdouble           x,
1629                                       gdouble           y,
1630                                       gdouble           width,
1631                                       gdouble           height)
1632 {
1633   GtkJunctionSides junction;
1634
1635   junction = gtk_theming_engine_get_junction_sides (engine);
1636
1637   render_background_internal (engine, cr,
1638                               x, y, width, height,
1639                               junction);
1640 }
1641
1642 static void
1643 gtk_theming_engine_hide_border_sides (GtkBorder *border,
1644                                       guint      hidden_side)
1645 {
1646   if (hidden_side & SIDE_TOP)
1647     border->top = 0;
1648   if (hidden_side & SIDE_RIGHT)
1649     border->right = 0;
1650   if (hidden_side & SIDE_BOTTOM)
1651     border->bottom = 0;
1652   if (hidden_side & SIDE_LEFT)
1653     border->left = 0;
1654 }
1655
1656 static void
1657 render_frame_internal (GtkThemingEngine *engine,
1658                        cairo_t          *cr,
1659                        gdouble           x,
1660                        gdouble           y,
1661                        gdouble           width,
1662                        gdouble           height,
1663                        guint             hidden_side,
1664                        GtkJunctionSides  junction)
1665 {
1666   GtkStateFlags state;
1667   GtkBorderStyle border_style;
1668   GtkRoundedBox border_box, padding_box;
1669   gdouble progress;
1670   gboolean running;
1671   GtkBorder border;
1672   static const guint current_side[4] = { SIDE_TOP, SIDE_RIGHT, SIDE_BOTTOM, SIDE_LEFT };
1673   GdkRGBA *colors[4];
1674   guint i, j;
1675
1676   state = gtk_theming_engine_get_state (engine);
1677
1678   gtk_theming_engine_get_border (engine, state, &border);
1679   gtk_theming_engine_hide_border_sides (&border, hidden_side);
1680
1681   gtk_theming_engine_get (engine, state,
1682                           "border-style", &border_style,
1683                           "border-top-color", &colors[0],
1684                           "border-right-color", &colors[1],
1685                           "border-bottom-color", &colors[2],
1686                           "border-left-color", &colors[3],
1687                           NULL);
1688
1689   running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1690
1691   if (running)
1692     {
1693       GtkStateFlags other_state;
1694       GdkRGBA *other_colors[4];
1695
1696       if (state & GTK_STATE_FLAG_PRELIGHT)
1697         {
1698           other_state = state & ~(GTK_STATE_FLAG_PRELIGHT);
1699           progress = 1 - progress;
1700         }
1701       else
1702         other_state = state | GTK_STATE_FLAG_PRELIGHT;
1703
1704       gtk_theming_engine_get (engine, other_state,
1705                               "border-top-color", &other_colors[0],
1706                               "border-right-color", &other_colors[1],
1707                               "border-bottom-color", &other_colors[2],
1708                               "border-left-color", &other_colors[3],
1709                               NULL);
1710
1711       for (i = 0; i < 4; i++)
1712         {
1713           colors[i]->red = CLAMP (colors[i]->red + ((other_colors[i]->red - colors[i]->red) * progress), 0, 1);
1714           colors[i]->green = CLAMP (colors[i]->green + ((other_colors[i]->green - colors[i]->green) * progress), 0, 1);
1715           colors[i]->blue = CLAMP (colors[i]->blue + ((other_colors[i]->blue - colors[i]->blue) * progress), 0, 1);
1716           colors[i]->alpha = CLAMP (colors[i]->alpha + ((other_colors[i]->alpha - colors[i]->alpha) * progress), 0, 1);
1717           gdk_rgba_free (other_colors[i]);
1718         }
1719     }
1720
1721   switch (border_style)
1722     {
1723     default:
1724       g_assert_not_reached ();
1725     case GTK_BORDER_STYLE_NONE:
1726     case GTK_BORDER_STYLE_SOLID:
1727       break;
1728     case GTK_BORDER_STYLE_INSET:
1729       color_shade (colors[1], 1.8, colors[1]);
1730       color_shade (colors[2], 1.8, colors[2]);
1731       break;
1732     case GTK_BORDER_STYLE_OUTSET:
1733       color_shade (colors[0], 1.8, colors[0]);
1734       color_shade (colors[3], 1.8, colors[3]);
1735       break;
1736     }
1737
1738   cairo_save (cr);
1739
1740   cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
1741
1742   _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1743   _gtk_rounded_box_apply_border_radius (&border_box, engine, state, junction);
1744   padding_box = border_box;
1745   _gtk_rounded_box_shrink (&padding_box, border.top, border.right, border.bottom, border.left);
1746
1747   switch (border_style)
1748     {
1749     case GTK_BORDER_STYLE_NONE:
1750       break;
1751     case GTK_BORDER_STYLE_SOLID:
1752     case GTK_BORDER_STYLE_INSET:
1753     case GTK_BORDER_STYLE_OUTSET:
1754
1755       if (gdk_rgba_equal (colors[0], colors[1]) &&
1756           gdk_rgba_equal (colors[0], colors[2]) &&
1757           gdk_rgba_equal (colors[0], colors[3]))
1758         {
1759           gdk_cairo_set_source_rgba (cr, colors[0]);
1760
1761           _gtk_rounded_box_path (&border_box, cr);
1762           _gtk_rounded_box_path (&padding_box, cr);
1763           cairo_fill (cr);
1764         }
1765       else
1766         {
1767           for (i = 0; i < 4; i++) 
1768             {
1769               if (hidden_side & current_side[i])
1770                 continue;
1771
1772               for (j = 0; j < 4; j++)
1773                 { 
1774                   if (hidden_side & current_side[j])
1775                     continue;
1776
1777                   if (i == j || 
1778                       gdk_rgba_equal (colors[i], colors[j]))
1779                     {
1780                       /* We were already painted when i == j */
1781                       if (i > j)
1782                         break;
1783
1784                       if (j == 0)
1785                         _gtk_rounded_box_path_top (&border_box, &padding_box, cr);
1786                       else if (j == 1)
1787                         _gtk_rounded_box_path_right (&border_box, &padding_box, cr);
1788                       else if (j == 2)
1789                         _gtk_rounded_box_path_bottom (&border_box, &padding_box, cr);
1790                       else if (j == 3)
1791                         _gtk_rounded_box_path_left (&border_box, &padding_box, cr);
1792                     }
1793                 }
1794               /* We were already painted when i == j */
1795               if (i > j)
1796                 continue;
1797
1798               gdk_cairo_set_source_rgba (cr, colors[i]);
1799
1800               cairo_fill (cr);
1801             }
1802         }
1803       break;
1804     }
1805
1806   cairo_restore (cr);
1807
1808   for (i = 0; i < 4; i++)
1809     gdk_rgba_free (colors[i]);
1810 }
1811
1812 static void
1813 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1814                                  cairo_t          *cr,
1815                                  gdouble           x,
1816                                  gdouble           y,
1817                                  gdouble           width,
1818                                  gdouble           height)
1819 {
1820   GtkStateFlags flags;
1821   GtkBorderStyle border_style;
1822   GtkJunctionSides junction;
1823   GtkBorderImage *border_image;
1824   GtkBorder border;
1825
1826   flags = gtk_theming_engine_get_state (engine);
1827   junction = gtk_theming_engine_get_junction_sides (engine);
1828   gtk_theming_engine_get_border (engine, flags, &border);
1829
1830   gtk_theming_engine_get (engine, flags,
1831                           "border-image", &border_image,
1832                           "border-style", &border_style,
1833                           NULL);
1834
1835   if (border_image != NULL)
1836     {
1837       _gtk_border_image_render (border_image, &border,
1838                                 cr, x, y, width, height);
1839       _gtk_border_image_unref (border_image);
1840     }
1841   else if (border_style != GTK_BORDER_STYLE_NONE)
1842     render_frame_internal (engine, cr,
1843                            x, y, width, height,
1844                            0, junction);
1845 }
1846
1847 static void
1848 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1849                                     cairo_t          *cr,
1850                                     gdouble           x,
1851                                     gdouble           y,
1852                                     gdouble           width,
1853                                     gdouble           height)
1854 {
1855   GtkStateFlags flags;
1856   GdkRGBA outline_color, fg_color;
1857   double vertical_overshoot;
1858   int diameter;
1859   double radius;
1860   double interp;                /* interpolation factor for center position */
1861   double x_double_horz, y_double_horz;
1862   double x_double_vert, y_double_vert;
1863   double x_double, y_double;
1864   gdouble angle;
1865   gint line_width;
1866   gboolean running, is_rtl;
1867   gdouble progress;
1868
1869   cairo_save (cr);
1870   flags = gtk_theming_engine_get_state (engine);
1871
1872   gtk_theming_engine_get_color (engine, flags, &fg_color);
1873   gtk_theming_engine_get_border_color (engine, flags, &outline_color);
1874
1875   running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1876   is_rtl = (gtk_theming_engine_get_direction (engine) == GTK_TEXT_DIR_RTL);
1877   line_width = 1;
1878
1879   if (!running)
1880     progress = (flags & GTK_STATE_FLAG_ACTIVE) ? 1 : 0;
1881
1882   if (!gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_HORIZONTAL))
1883     {
1884       if (is_rtl)
1885         angle = (G_PI) - ((G_PI / 2) * progress);
1886       else
1887         angle = (G_PI / 2) * progress;
1888     }
1889   else
1890     {
1891       if (is_rtl)
1892         angle = (G_PI / 2) + ((G_PI / 2) * progress);
1893       else
1894         angle = (G_PI / 2) - ((G_PI / 2) * progress);
1895     }
1896
1897   interp = progress;
1898
1899   /* Compute distance that the stroke extends beyonds the end
1900    * of the triangle we draw.
1901    */
1902   vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1903
1904   /* For odd line widths, we end the vertical line of the triangle
1905    * at a half pixel, so we round differently.
1906    */
1907   if (line_width % 2 == 1)
1908     vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1909   else
1910     vertical_overshoot = ceil (vertical_overshoot);
1911
1912   /* Adjust the size of the triangle we draw so that the entire stroke fits
1913    */
1914   diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1915
1916   /* If the line width is odd, we want the diameter to be even,
1917    * and vice versa, so force the sum to be odd. This relationship
1918    * makes the point of the triangle look right.
1919    */
1920   diameter -= (1 - (diameter + line_width) % 2);
1921
1922   radius = diameter / 2.;
1923
1924   /* Adjust the center so that the stroke is properly aligned with
1925    * the pixel grid. The center adjustment is different for the
1926    * horizontal and vertical orientations. For intermediate positions
1927    * we interpolate between the two.
1928    */
1929   x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1930   y_double_vert = (y + height / 2) - 0.5;
1931
1932   x_double_horz = (x + width / 2) - 0.5;
1933   y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1934
1935   x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1936   y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1937
1938   cairo_translate (cr, x_double, y_double);
1939   cairo_rotate (cr, angle);
1940
1941   cairo_move_to (cr, - radius / 2., - radius);
1942   cairo_line_to (cr,   radius / 2.,   0);
1943   cairo_line_to (cr, - radius / 2.,   radius);
1944   cairo_close_path (cr);
1945
1946   cairo_set_line_width (cr, line_width);
1947
1948   gdk_cairo_set_source_rgba (cr, &fg_color);
1949
1950   cairo_fill_preserve (cr);
1951
1952   gdk_cairo_set_source_rgba (cr, &outline_color);
1953   cairo_stroke (cr);
1954
1955   cairo_restore (cr);
1956 }
1957
1958 static void
1959 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1960                                  cairo_t          *cr,
1961                                  gdouble           x,
1962                                  gdouble           y,
1963                                  gdouble           width,
1964                                  gdouble           height)
1965 {
1966   GtkStateFlags flags;
1967   GdkRGBA color;
1968   gint line_width;
1969   gint8 *dash_list;
1970
1971   cairo_save (cr);
1972   flags = gtk_theming_engine_get_state (engine);
1973
1974   gtk_theming_engine_get_color (engine, flags, &color);
1975
1976   gtk_theming_engine_get_style (engine,
1977                                 "focus-line-width", &line_width,
1978                                 "focus-line-pattern", (gchar *) &dash_list,
1979                                 NULL);
1980
1981   cairo_set_line_width (cr, (gdouble) line_width);
1982
1983   if (dash_list[0])
1984     {
1985       gint n_dashes = strlen ((const gchar *) dash_list);
1986       gdouble *dashes = g_new (gdouble, n_dashes);
1987       gdouble total_length = 0;
1988       gdouble dash_offset;
1989       gint i;
1990
1991       for (i = 0; i < n_dashes; i++)
1992         {
1993           dashes[i] = dash_list[i];
1994           total_length += dash_list[i];
1995         }
1996
1997       /* The dash offset here aligns the pattern to integer pixels
1998        * by starting the dash at the right side of the left border
1999        * Negative dash offsets in cairo don't work
2000        * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
2001        */
2002       dash_offset = - line_width / 2.;
2003
2004       while (dash_offset < 0)
2005         dash_offset += total_length;
2006
2007       cairo_set_dash (cr, dashes, n_dashes, dash_offset);
2008       g_free (dashes);
2009     }
2010
2011   cairo_rectangle (cr,
2012                    x + line_width / 2.,
2013                    y + line_width / 2.,
2014                    width - line_width,
2015                    height - line_width);
2016
2017   gdk_cairo_set_source_rgba (cr, &color);
2018   cairo_stroke (cr);
2019
2020   cairo_restore (cr);
2021
2022   g_free (dash_list);
2023 }
2024
2025 static void
2026 gtk_theming_engine_render_line (GtkThemingEngine *engine,
2027                                 cairo_t          *cr,
2028                                 gdouble           x0,
2029                                 gdouble           y0,
2030                                 gdouble           x1,
2031                                 gdouble           y1)
2032 {
2033   GdkRGBA bg_color, darker, lighter;
2034   GtkStateFlags flags;
2035   gint i, thickness, thickness_dark, thickness_light, len;
2036   cairo_matrix_t matrix;
2037   gdouble angle;
2038
2039   /* FIXME: thickness */
2040   thickness = 2;
2041   thickness_dark = thickness / 2;
2042   thickness_light = thickness - thickness_dark;
2043
2044   flags = gtk_theming_engine_get_state (engine);
2045   cairo_save (cr);
2046
2047   gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2048   color_shade (&bg_color, 0.7, &darker);
2049   color_shade (&bg_color, 1.3, &lighter);
2050
2051   cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
2052   cairo_set_line_width (cr, 1);
2053
2054   angle = atan2 (x1 - x0, y1 - y0);
2055   angle = (2 * G_PI) - angle;
2056   angle += G_PI / 2;
2057
2058   cairo_get_matrix (cr, &matrix);
2059   cairo_matrix_translate (&matrix, x0, y0);
2060   cairo_matrix_rotate (&matrix, angle);
2061   cairo_set_matrix (cr, &matrix);
2062
2063   x1 -= x0;
2064   y1 -= y0;
2065
2066   len = (gint) sqrt ((x1 * x1) + (y1 * y1));
2067
2068   y0 = -thickness_dark;
2069
2070   for (i = 0; i < thickness_dark; i++)
2071     {
2072       gdk_cairo_set_source_rgba (cr, &lighter);
2073       add_path_line (cr, len - i - 1.5, y0, len - 0.5, y0);
2074       cairo_stroke (cr);
2075
2076       gdk_cairo_set_source_rgba (cr, &darker);
2077       add_path_line (cr, 0.5, y0, len - i - 1.5, y0);
2078       cairo_stroke (cr);
2079
2080       y0++;
2081     }
2082
2083   for (i = 0; i < thickness_light; i++)
2084     {
2085       gdk_cairo_set_source_rgba (cr, &darker);
2086       add_path_line (cr, 0.5, y0, thickness_light - i + 0.5, y0);
2087       cairo_stroke (cr);
2088
2089       gdk_cairo_set_source_rgba (cr, &lighter);
2090       add_path_line (cr, thickness_light - i + 0.5, y0, len - 0.5, y0);
2091       cairo_stroke (cr);
2092
2093       y0++;
2094     }
2095
2096   cairo_restore (cr);
2097 }
2098
2099 static void
2100 prepare_context_for_layout (cairo_t *cr,
2101                             gdouble x,
2102                             gdouble y,
2103                             PangoLayout *layout)
2104 {
2105   const PangoMatrix *matrix;
2106
2107   matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2108
2109   cairo_move_to (cr, x, y);
2110
2111   if (matrix)
2112     {
2113       cairo_matrix_t cairo_matrix;
2114
2115       cairo_matrix_init (&cairo_matrix,
2116                          matrix->xx, matrix->yx,
2117                          matrix->xy, matrix->yy,
2118                          matrix->x0, matrix->y0);
2119
2120       cairo_transform (cr, &cairo_matrix);
2121     }
2122 }
2123
2124 static void
2125 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2126                                   cairo_t          *cr,
2127                                   gdouble           x,
2128                                   gdouble           y,
2129                                   PangoLayout      *layout)
2130 {
2131   GdkRGBA fg_color;
2132   GtkShadow *text_shadow = NULL;
2133   GtkStateFlags flags;
2134   gdouble progress;
2135   gboolean running;
2136
2137   cairo_save (cr);
2138   flags = gtk_theming_engine_get_state (engine);
2139   gtk_theming_engine_get_color (engine, flags, &fg_color);
2140
2141   running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
2142
2143   if (running)
2144     {
2145       GtkStateFlags other_flags;
2146       GdkRGBA other_fg;
2147
2148       if (flags & GTK_STATE_FLAG_PRELIGHT)
2149         {
2150           other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
2151           progress = 1 - progress;
2152         }
2153       else
2154         other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
2155
2156       gtk_theming_engine_get_color (engine, other_flags, &other_fg);
2157
2158       fg_color.red = CLAMP (fg_color.red + ((other_fg.red - fg_color.red) * progress), 0, 1);
2159       fg_color.green = CLAMP (fg_color.green + ((other_fg.green - fg_color.green) * progress), 0, 1);
2160       fg_color.blue = CLAMP (fg_color.blue + ((other_fg.blue - fg_color.blue) * progress), 0, 1);
2161       fg_color.alpha = CLAMP (fg_color.alpha + ((other_fg.alpha - fg_color.alpha) * progress), 0, 1);
2162     }
2163
2164   gtk_theming_engine_get (engine, flags,
2165                           "text-shadow", &text_shadow,
2166                           NULL);
2167
2168   prepare_context_for_layout (cr, x, y, layout);
2169
2170   if (text_shadow != NULL)
2171     {
2172       _gtk_text_shadow_paint_layout (text_shadow, cr, layout);
2173       _gtk_shadow_unref (text_shadow);
2174     }
2175
2176   gdk_cairo_set_source_rgba (cr, &fg_color);
2177   pango_cairo_show_layout (cr, layout);
2178
2179   cairo_restore (cr);
2180 }
2181
2182 static void
2183 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2184                                   cairo_t          *cr,
2185                                   gdouble           x,
2186                                   gdouble           y,
2187                                   gdouble           width,
2188                                   gdouble           height,
2189                                   GtkOrientation    orientation)
2190 {
2191   const GtkWidgetPath *path;
2192   gint thickness;
2193
2194   path = gtk_theming_engine_get_path (engine);
2195
2196   gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2197   gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2198
2199   /* FIXME: thickness */
2200   thickness = 2;
2201
2202   if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2203     {
2204       if (orientation == GTK_ORIENTATION_VERTICAL)
2205         gtk_theming_engine_render_line (engine, cr,
2206                                         x + thickness,
2207                                         y + (gint) height / 2,
2208                                         x + width - thickness - 1,
2209                                         y + (gint) height / 2);
2210       else
2211         gtk_theming_engine_render_line (engine, cr,
2212                                         x + (gint) width / 2,
2213                                         y + thickness,
2214                                         x + (gint) width / 2,
2215                                         y + height - thickness - 1);
2216     }
2217 }
2218
2219 static void
2220 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2221                                      cairo_t          *cr,
2222                                      gdouble           x,
2223                                      gdouble           y,
2224                                      gdouble           width,
2225                                      gdouble           height,
2226                                      GtkPositionType   gap_side,
2227                                      gdouble           xy0_gap,
2228                                      gdouble           xy1_gap)
2229 {
2230   GtkJunctionSides junction;
2231   GtkStateFlags state;
2232   gint border_width;
2233   GtkCssBorderCornerRadius *top_left_radius, *top_right_radius;
2234   GtkCssBorderCornerRadius *bottom_left_radius, *bottom_right_radius;
2235   GtkCssBorderRadius border_radius = { { 0, },  };
2236   gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2237   GtkBorder border;
2238
2239   xc = yc = wc = hc = 0;
2240   state = gtk_theming_engine_get_state (engine);
2241   junction = gtk_theming_engine_get_junction_sides (engine);
2242
2243   gtk_theming_engine_get_border (engine, state, &border);
2244   gtk_theming_engine_get (engine, state,
2245                           /* Can't use border-radius as it's an int for
2246                            * backwards compat */
2247                           "border-top-left-radius", &top_left_radius,
2248                           "border-top-right-radius", &top_right_radius,
2249                           "border-bottom-right-radius", &bottom_right_radius,
2250                           "border-bottom-left-radius", &bottom_left_radius,
2251                           NULL);
2252
2253   if (top_left_radius)
2254     border_radius.top_left = *top_left_radius;
2255   g_free (top_left_radius);
2256   if (top_right_radius)
2257     border_radius.top_right = *top_right_radius;
2258   g_free (top_right_radius);
2259   if (bottom_right_radius)
2260     border_radius.bottom_right = *bottom_right_radius;
2261   g_free (bottom_right_radius);
2262   if (bottom_left_radius)
2263     border_radius.bottom_left = *bottom_left_radius;
2264   g_free (bottom_left_radius);
2265
2266   border_width = MIN (MIN (border.top, border.bottom),
2267                       MIN (border.left, border.right));
2268
2269   cairo_save (cr);
2270
2271   switch (gap_side)
2272     {
2273     case GTK_POS_TOP:
2274       xc = x + xy0_gap + border_width;
2275       yc = y;
2276       wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2277       hc = border_width;
2278
2279       if (xy0_gap < border_radius.top_left.horizontal)
2280         junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2281
2282       if (xy1_gap > width - border_radius.top_right.horizontal)
2283         junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2284       break;
2285     case GTK_POS_BOTTOM:
2286       xc = x + xy0_gap + border_width;
2287       yc = y + height - border_width;
2288       wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2289       hc = border_width;
2290
2291       if (xy0_gap < border_radius.bottom_left.horizontal)
2292         junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2293
2294       if (xy1_gap > width - border_radius.bottom_right.horizontal)
2295         junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2296
2297       break;
2298     case GTK_POS_LEFT:
2299       xc = x;
2300       yc = y + xy0_gap + border_width;
2301       wc = border_width;
2302       hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2303
2304       if (xy0_gap < border_radius.top_left.vertical)
2305         junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2306
2307       if (xy1_gap > height - border_radius.bottom_left.vertical)
2308         junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2309
2310       break;
2311     case GTK_POS_RIGHT:
2312       xc = x + width - border_width;
2313       yc = y + xy0_gap + border_width;
2314       wc = border_width;
2315       hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2316
2317       if (xy0_gap < border_radius.top_right.vertical)
2318         junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2319
2320       if (xy1_gap > height - border_radius.bottom_right.vertical)
2321         junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2322
2323       break;
2324     }
2325
2326   cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2327   cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2328   cairo_rectangle (cr, x0, yc, xc - x0, hc);
2329   cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2330   cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2331   cairo_clip (cr);
2332
2333   render_frame_internal (engine, cr,
2334                          x, y, width, height,
2335                          0, junction);
2336
2337   cairo_restore (cr);
2338 }
2339
2340 static void
2341 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2342                                      cairo_t          *cr,
2343                                      gdouble           x,
2344                                      gdouble           y,
2345                                      gdouble           width,
2346                                      gdouble           height,
2347                                      GtkPositionType   gap_side)
2348 {
2349   GtkJunctionSides junction = 0;
2350   guint hidden_side = 0;
2351
2352   cairo_save (cr);
2353
2354   switch (gap_side)
2355     {
2356     case GTK_POS_LEFT:
2357       junction = GTK_JUNCTION_LEFT;
2358       hidden_side = SIDE_LEFT;
2359
2360       cairo_translate (cr, x + width, y);
2361       cairo_rotate (cr, G_PI / 2);
2362       break;
2363     case GTK_POS_RIGHT:
2364       junction = GTK_JUNCTION_RIGHT;
2365       hidden_side = SIDE_RIGHT;
2366
2367       cairo_translate (cr, x, y + height);
2368       cairo_rotate (cr, - G_PI / 2);
2369       break;
2370     case GTK_POS_TOP:
2371       junction = GTK_JUNCTION_TOP;
2372       hidden_side = SIDE_TOP;
2373
2374       cairo_translate (cr, x + width, y + height);
2375       cairo_rotate (cr, G_PI);
2376       break;
2377     case GTK_POS_BOTTOM:
2378       junction = GTK_JUNCTION_BOTTOM;
2379       hidden_side = SIDE_BOTTOM;
2380
2381       cairo_translate (cr, x, y);
2382       break;
2383     }
2384
2385   if (gap_side == GTK_POS_TOP ||
2386       gap_side == GTK_POS_BOTTOM)
2387     render_background_internal (engine, cr,
2388                                 0, 0, width, height,
2389                                 GTK_JUNCTION_BOTTOM);
2390   else
2391     render_background_internal (engine, cr,
2392                                 0, 0, height, width,
2393                                 GTK_JUNCTION_BOTTOM);
2394   cairo_restore (cr);
2395
2396   cairo_save (cr);
2397
2398   render_frame_internal (engine, cr,
2399                          x, y, width, height,
2400                          hidden_side, junction);
2401
2402   cairo_restore (cr);
2403 }
2404
2405 static void
2406 render_dot (cairo_t       *cr,
2407             const GdkRGBA *lighter,
2408             const GdkRGBA *darker,
2409             gdouble        x,
2410             gdouble        y,
2411             gdouble        size)
2412 {
2413   size = CLAMP ((gint) size, 2, 3);
2414
2415   if (size == 2)
2416     {
2417       gdk_cairo_set_source_rgba (cr, lighter);
2418       cairo_rectangle (cr, x, y, 1, 1);
2419       cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2420       cairo_fill (cr);
2421     }
2422   else if (size == 3)
2423     {
2424       gdk_cairo_set_source_rgba (cr, lighter);
2425       cairo_rectangle (cr, x, y, 2, 1);
2426       cairo_rectangle (cr, x, y, 1, 2);
2427       cairo_fill (cr);
2428
2429       gdk_cairo_set_source_rgba (cr, darker);
2430       cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2431       cairo_rectangle (cr, x + 2, y, 1, 2);
2432       cairo_fill (cr);
2433     }
2434 }
2435
2436 static void
2437 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2438                                   cairo_t          *cr,
2439                                   gdouble           x,
2440                                   gdouble           y,
2441                                   gdouble           width,
2442                                   gdouble           height)
2443 {
2444   GtkStateFlags flags;
2445   GdkRGBA bg_color, lighter, darker;
2446   GtkJunctionSides sides;
2447   gint xx, yy;
2448
2449   cairo_save (cr);
2450   flags = gtk_theming_engine_get_state (engine);
2451
2452   cairo_set_line_width (cr, 1.0);
2453   sides = gtk_theming_engine_get_junction_sides (engine);
2454   gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2455
2456   color_shade (&bg_color, 0.7, &darker);
2457   color_shade (&bg_color, 1.3, &lighter);
2458
2459   render_background_internal (engine, cr, x, y, width, height, sides);
2460
2461   if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_GRIP))
2462     {
2463       /* reduce confusing values to a meaningful state */
2464       if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2465         sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2466
2467       if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2468         sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2469
2470       if (sides == 0)
2471         sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2472
2473       /* align drawing area to the connected side */
2474       if (sides == GTK_JUNCTION_LEFT)
2475         {
2476           if (height < width)
2477             width = height;
2478         }
2479       else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2480         {
2481           if (width < height)
2482             height = width;
2483           else if (height < width)
2484             width = height;
2485         }
2486       else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2487         {
2488           /* make it square, aligning to bottom left */
2489           if (width < height)
2490             {
2491               y += (height - width);
2492               height = width;
2493             }
2494           else if (height < width)
2495             width = height;
2496         }
2497       else if (sides == GTK_JUNCTION_RIGHT)
2498         {
2499           /* aligning to right */
2500           if (height < width)
2501             {
2502               x += (width - height);
2503               width = height;
2504             }
2505         }
2506       else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2507         {
2508           if (width < height)
2509             height = width;
2510           else if (height < width)
2511             {
2512               x += (width - height);
2513               width = height;
2514             }
2515         }
2516       else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2517         {
2518           /* make it square, aligning to bottom right */
2519           if (width < height)
2520             {
2521               y += (height - width);
2522               height = width;
2523             }
2524           else if (height < width)
2525             {
2526               x += (width - height);
2527               width = height;
2528             }
2529         }
2530       else if (sides == GTK_JUNCTION_TOP)
2531         {
2532           if (width < height)
2533             height = width;
2534         }
2535       else if (sides == GTK_JUNCTION_BOTTOM)
2536         {
2537           /* align to bottom */
2538           if (width < height)
2539             {
2540               y += (height - width);
2541               height = width;
2542             }
2543         }
2544       else
2545         g_assert_not_reached ();
2546
2547       if (sides == GTK_JUNCTION_LEFT ||
2548           sides == GTK_JUNCTION_RIGHT)
2549         {
2550           gint xi;
2551
2552           xi = x;
2553
2554           while (xi < x + width)
2555             {
2556               gdk_cairo_set_source_rgba (cr, &lighter);
2557               add_path_line (cr, x, y, x, y + height);
2558               cairo_stroke (cr);
2559               xi++;
2560
2561               gdk_cairo_set_source_rgba (cr, &darker);
2562               add_path_line (cr, xi, y, xi, y + height);
2563               cairo_stroke (cr);
2564               xi += 2;
2565             }
2566         }
2567       else if (sides == GTK_JUNCTION_TOP ||
2568                sides == GTK_JUNCTION_BOTTOM)
2569         {
2570           gint yi;
2571
2572           yi = y;
2573
2574           while (yi < y + height)
2575             {
2576               gdk_cairo_set_source_rgba (cr, &lighter);
2577               add_path_line (cr, x, yi, x + width, yi);
2578               cairo_stroke (cr);
2579               yi++;
2580
2581               gdk_cairo_set_source_rgba (cr, &darker);
2582               add_path_line (cr, x, yi, x + width, yi);
2583               cairo_stroke (cr);
2584               yi += 2;
2585             }
2586         }
2587       else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2588         {
2589           gint xi, yi;
2590
2591           xi = x + width;
2592           yi = y + height;
2593
2594           while (xi > x + 3)
2595             {
2596               gdk_cairo_set_source_rgba (cr, &darker);
2597               add_path_line (cr, xi, y, x, yi);
2598               cairo_stroke (cr);
2599
2600               --xi;
2601               --yi;
2602
2603               add_path_line (cr, xi, y, x, yi);
2604               cairo_stroke (cr);
2605
2606               --xi;
2607               --yi;
2608
2609               gdk_cairo_set_source_rgba (cr, &lighter);
2610               add_path_line (cr, xi, y, x, yi);
2611               cairo_stroke (cr);
2612
2613               xi -= 3;
2614               yi -= 3;
2615             }
2616         }
2617       else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2618         {
2619           gint xi, yi;
2620
2621           xi = x;
2622           yi = y + height;
2623
2624           while (xi < (x + width - 3))
2625             {
2626               gdk_cairo_set_source_rgba (cr, &lighter);
2627               add_path_line (cr, xi, y, x + width, yi);
2628               cairo_stroke (cr);
2629
2630               ++xi;
2631               --yi;
2632
2633               gdk_cairo_set_source_rgba (cr, &darker);
2634               add_path_line (cr, xi, y, x + width, yi);
2635               cairo_stroke (cr);
2636
2637               ++xi;
2638               --yi;
2639
2640               add_path_line (cr, xi, y, x + width, yi);
2641               cairo_stroke (cr);
2642
2643               xi += 3;
2644               yi -= 3;
2645             }
2646         }
2647       else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2648         {
2649           gint xi, yi;
2650
2651           xi = x + width;
2652           yi = y;
2653
2654           while (xi > x + 3)
2655             {
2656               gdk_cairo_set_source_rgba (cr, &darker);
2657               add_path_line (cr, x, yi, xi, y + height);
2658               cairo_stroke (cr);
2659
2660               --xi;
2661               ++yi;
2662
2663               add_path_line (cr, x, yi, xi, y + height);
2664               cairo_stroke (cr);
2665
2666               --xi;
2667               ++yi;
2668
2669               gdk_cairo_set_source_rgba (cr, &lighter);
2670               add_path_line (cr, x, yi, xi, y + height);
2671               cairo_stroke (cr);
2672
2673               xi -= 3;
2674               yi += 3;
2675             }
2676         }
2677       else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2678         {
2679           gint xi, yi;
2680
2681           xi = x;
2682           yi = y;
2683
2684           while (xi < (x + width - 3))
2685             {
2686               gdk_cairo_set_source_rgba (cr, &lighter);
2687               add_path_line (cr, xi, y + height, x + width, yi);
2688               cairo_stroke (cr);
2689
2690               ++xi;
2691               ++yi;
2692
2693               gdk_cairo_set_source_rgba (cr, &darker);
2694               add_path_line (cr, xi, y + height, x + width, yi);
2695               cairo_stroke (cr);
2696
2697               ++xi;
2698               ++yi;
2699
2700               add_path_line (cr, xi, y + height, x + width, yi);
2701               cairo_stroke (cr);
2702
2703               xi += 3;
2704               yi += 3;
2705             }
2706         }
2707     }
2708   else if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_PANE_SEPARATOR))
2709     {
2710       if (width > height)
2711         for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2712           render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2713       else
2714         for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2715           render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2716     }
2717   else
2718     {
2719       for (yy = y; yy < y + height; yy += 3)
2720         for (xx = x; xx < x + width; xx += 6)
2721           {
2722             render_dot (cr, &lighter, &darker, xx, yy, 2);
2723             render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2724           }
2725     }
2726
2727   cairo_restore (cr);
2728 }
2729
2730 void
2731 _gtk_theming_engine_paint_spinner (cairo_t *cr,
2732                                    gdouble  radius,
2733                                    gdouble  progress,
2734                                    GdkRGBA *color)
2735 {
2736   guint num_steps, step;
2737   gdouble half;
2738   gint i;
2739
2740   num_steps = 12;
2741
2742   if (progress >= 0)
2743     step = (guint) (progress * num_steps);
2744   else
2745     step = 0;
2746
2747   cairo_save (cr);
2748
2749   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2750   cairo_set_line_width (cr, 2.0);
2751
2752   half = num_steps / 2;
2753
2754   for (i = 0; i < num_steps; i++)
2755     {
2756       gint inset = 0.7 * radius;
2757
2758       /* transparency is a function of time and intial value */
2759       gdouble t = 1.0 - (gdouble) ((i + step) % num_steps) / num_steps;
2760       gdouble xscale = - sin (i * G_PI / half);
2761       gdouble yscale = - cos (i * G_PI / half);
2762
2763       cairo_set_source_rgba (cr,
2764                              color->red,
2765                              color->green,
2766                              color->blue,
2767                              color->alpha * t);
2768
2769       cairo_move_to (cr,
2770                      (radius - inset) * xscale,
2771                      (radius - inset) * yscale);
2772       cairo_line_to (cr,
2773                      radius * xscale,
2774                      radius * yscale);
2775
2776       cairo_stroke (cr);
2777     }
2778
2779   cairo_restore (cr);
2780 }
2781
2782 static void
2783 render_spinner (GtkThemingEngine *engine,
2784                 cairo_t          *cr,
2785                 gdouble           x,
2786                 gdouble           y,
2787                 gdouble           width,
2788                 gdouble           height)
2789 {
2790   GtkStateFlags state;
2791   GtkShadow *shadow;
2792   GdkRGBA color;
2793   gdouble progress;
2794   gdouble radius;
2795
2796   state = gtk_theming_engine_get_state (engine);
2797
2798   if (!gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2799     progress = -1;
2800
2801   radius = MIN (width / 2, height / 2);
2802
2803   gtk_theming_engine_get_color (engine, state, &color);
2804   gtk_theming_engine_get (engine, state,
2805                           "icon-shadow", &shadow,
2806                           NULL);
2807
2808   cairo_save (cr);
2809   cairo_translate (cr, x + width / 2, y + height / 2);
2810
2811   if (shadow != NULL)
2812     {
2813       _gtk_icon_shadow_paint_spinner (shadow, cr,
2814                                       radius,
2815                                       progress);
2816       _gtk_shadow_unref (shadow);
2817     }
2818
2819   _gtk_theming_engine_paint_spinner (cr,
2820                                      radius,
2821                                      progress,
2822                                      &color);
2823
2824   cairo_restore (cr);
2825 }
2826
2827 static void
2828 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2829                                     cairo_t          *cr,
2830                                     gdouble           x,
2831                                     gdouble           y,
2832                                     gdouble           width,
2833                                     gdouble           height)
2834 {
2835   if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_SPINNER))
2836     {
2837       render_spinner (engine, cr, x, y, width, height);
2838     }
2839   else
2840     {
2841       gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2842       gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2843     }
2844 }
2845
2846 static GdkPixbuf *
2847 scale_or_ref (GdkPixbuf *src,
2848               gint       width,
2849               gint       height)
2850 {
2851   if (width == gdk_pixbuf_get_width (src) &&
2852       height == gdk_pixbuf_get_height (src))
2853     return g_object_ref (src);
2854   else
2855     return gdk_pixbuf_scale_simple (src,
2856                                     width, height,
2857                                     GDK_INTERP_BILINEAR);
2858 }
2859
2860 static gboolean
2861 lookup_icon_size (GtkThemingEngine *engine,
2862                   GtkIconSize       size,
2863                   gint             *width,
2864                   gint             *height)
2865 {
2866   GdkScreen *screen;
2867   GtkSettings *settings;
2868
2869   screen = gtk_theming_engine_get_screen (engine);
2870   settings = gtk_settings_get_for_screen (screen);
2871
2872   return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2873 }
2874
2875 static void
2876 colorshift_source (cairo_t *cr,
2877                    gdouble shift)
2878 {
2879   cairo_pattern_t *source;
2880
2881   cairo_save (cr);
2882   cairo_paint (cr);
2883
2884   source = cairo_pattern_reference (cairo_get_source (cr));
2885
2886   cairo_set_source_rgb (cr, shift, shift, shift);
2887   cairo_set_operator (cr, CAIRO_OPERATOR_COLOR_DODGE);
2888
2889   cairo_mask (cr, source);
2890
2891   cairo_pattern_destroy (source);
2892   cairo_restore (cr);
2893 }
2894
2895 static GdkPixbuf *
2896 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine    *engine,
2897                                        const GtkIconSource *source,
2898                                        GtkIconSize          size)
2899 {
2900   GdkPixbuf *scaled;
2901   GdkPixbuf *stated;
2902   GdkPixbuf *base_pixbuf;
2903   GtkStateFlags state;
2904   gint width = 1;
2905   gint height = 1;
2906   cairo_t *cr;
2907   cairo_surface_t *surface;
2908
2909   base_pixbuf = gtk_icon_source_get_pixbuf (source);
2910   state = gtk_theming_engine_get_state (engine);
2911
2912   g_return_val_if_fail (base_pixbuf != NULL, NULL);
2913
2914   if (size != (GtkIconSize) -1 &&
2915       !lookup_icon_size (engine, size, &width, &height))
2916     {
2917       g_warning (G_STRLOC ": invalid icon size '%d'", size);
2918       return NULL;
2919     }
2920
2921   /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2922    * leave it alone.
2923    */
2924   if (size != (GtkIconSize) -1 &&
2925       gtk_icon_source_get_size_wildcarded (source))
2926     scaled = scale_or_ref (base_pixbuf, width, height);
2927   else
2928     scaled = g_object_ref (base_pixbuf);
2929
2930   /* If the state was wildcarded, then generate a state. */
2931   if (gtk_icon_source_get_state_wildcarded (source))
2932     {
2933       if (state & GTK_STATE_FLAG_INSENSITIVE)
2934         {
2935           surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2936                                                 gdk_pixbuf_get_width (scaled),
2937                                                 gdk_pixbuf_get_height (scaled));
2938           cr = cairo_create (surface);
2939           gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2940           cairo_paint_with_alpha (cr, 0.5);
2941
2942           cairo_destroy (cr);
2943
2944           g_object_unref (scaled);
2945           stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2946                                                 cairo_image_surface_get_width (surface),
2947                                                 cairo_image_surface_get_height (surface));
2948           cairo_surface_destroy (surface);
2949         }
2950       else if (state & GTK_STATE_FLAG_PRELIGHT)
2951         {
2952           surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2953                                                 gdk_pixbuf_get_width (scaled),
2954                                                 gdk_pixbuf_get_height (scaled));
2955
2956           cr = cairo_create (surface);
2957           gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2958           colorshift_source (cr, 0.10);
2959
2960           cairo_destroy (cr);
2961
2962           g_object_unref (scaled);
2963           stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2964                                                 cairo_image_surface_get_width (surface),
2965                                                 cairo_image_surface_get_height (surface));
2966           cairo_surface_destroy (surface);
2967         }
2968       else
2969         stated = scaled;
2970     }
2971   else
2972     stated = scaled;
2973
2974   return stated;
2975 }
2976
2977 static void
2978 gtk_theming_engine_render_icon (GtkThemingEngine *engine,
2979                                 cairo_t *cr,
2980                                 GdkPixbuf *pixbuf,
2981                                 gdouble x,
2982                                 gdouble y)
2983 {
2984   GtkStateFlags state;
2985   GtkShadow *icon_shadow;
2986
2987   state = gtk_theming_engine_get_state (engine);
2988
2989   cairo_save (cr);
2990
2991   gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2992
2993   gtk_theming_engine_get (engine, state,
2994                           "icon-shadow", &icon_shadow,
2995                           NULL);
2996
2997   if (icon_shadow != NULL)
2998     {
2999       _gtk_icon_shadow_paint (icon_shadow, cr);
3000       _gtk_shadow_unref (icon_shadow);
3001     }
3002
3003   cairo_paint (cr);
3004
3005   cairo_restore (cr);
3006 }
3007