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