]> Pileus Git - ~andy/gtk/blob - gtk/gtkthemingengine.c
themingengine: set the cairo extend to PAD when we're stretching bg
[~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           cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);
1626           scale_width = width;
1627           scale_height = height;
1628         }
1629
1630       cairo_scale (cr, scale_width, scale_height);
1631       cairo_set_source (cr, pattern);
1632       cairo_scale (cr, 1.0 / scale_width, 1.0 / scale_height);
1633     }
1634   else
1635     gdk_cairo_set_source_rgba (cr, &bg_color);
1636
1637   cairo_fill (cr);
1638
1639   if (pattern)
1640     cairo_pattern_destroy (pattern);
1641
1642   if (repeat)
1643     g_free (repeat);
1644
1645   if (box_shadow != NULL)
1646     {
1647       _gtk_box_shadow_render (box_shadow, cr, &border_box);
1648       _gtk_shadow_unref (box_shadow);
1649     }
1650
1651   cairo_restore (cr);
1652 }
1653
1654 static void
1655 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1656                                       cairo_t          *cr,
1657                                       gdouble           x,
1658                                       gdouble           y,
1659                                       gdouble           width,
1660                                       gdouble           height)
1661 {
1662   GtkJunctionSides junction;
1663
1664   junction = gtk_theming_engine_get_junction_sides (engine);
1665
1666   render_background_internal (engine, cr,
1667                               x, y, width, height,
1668                               junction);
1669 }
1670
1671 static void
1672 gtk_theming_engine_hide_border_sides (GtkBorder *border,
1673                                       guint      hidden_side)
1674 {
1675   if (hidden_side & SIDE_TOP)
1676     border->top = 0;
1677   if (hidden_side & SIDE_RIGHT)
1678     border->right = 0;
1679   if (hidden_side & SIDE_BOTTOM)
1680     border->bottom = 0;
1681   if (hidden_side & SIDE_LEFT)
1682     border->left = 0;
1683 }
1684
1685 static void
1686 render_frame_internal (GtkThemingEngine *engine,
1687                        cairo_t          *cr,
1688                        gdouble           x,
1689                        gdouble           y,
1690                        gdouble           width,
1691                        gdouble           height,
1692                        guint             hidden_side,
1693                        GtkJunctionSides  junction)
1694 {
1695   GtkStateFlags state;
1696   GtkBorderStyle border_style;
1697   GtkRoundedBox border_box, padding_box;
1698   gdouble progress;
1699   gboolean running;
1700   GtkBorder border;
1701   static const guint current_side[4] = { SIDE_TOP, SIDE_RIGHT, SIDE_BOTTOM, SIDE_LEFT };
1702   GdkRGBA *colors[4];
1703   guint i, j;
1704
1705   state = gtk_theming_engine_get_state (engine);
1706
1707   gtk_theming_engine_get_border (engine, state, &border);
1708   gtk_theming_engine_hide_border_sides (&border, hidden_side);
1709
1710   gtk_theming_engine_get (engine, state,
1711                           "border-style", &border_style,
1712                           "border-top-color", &colors[0],
1713                           "border-right-color", &colors[1],
1714                           "border-bottom-color", &colors[2],
1715                           "border-left-color", &colors[3],
1716                           NULL);
1717
1718   running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1719
1720   if (running)
1721     {
1722       GtkStateFlags other_state;
1723       GdkRGBA *other_colors[4];
1724
1725       if (state & GTK_STATE_FLAG_PRELIGHT)
1726         {
1727           other_state = state & ~(GTK_STATE_FLAG_PRELIGHT);
1728           progress = 1 - progress;
1729         }
1730       else
1731         other_state = state | GTK_STATE_FLAG_PRELIGHT;
1732
1733       gtk_theming_engine_get (engine, other_state,
1734                               "border-top-color", &other_colors[0],
1735                               "border-right-color", &other_colors[1],
1736                               "border-bottom-color", &other_colors[2],
1737                               "border-left-color", &other_colors[3],
1738                               NULL);
1739
1740       for (i = 0; i < 4; i++)
1741         {
1742           colors[i]->red = CLAMP (colors[i]->red + ((other_colors[i]->red - colors[i]->red) * progress), 0, 1);
1743           colors[i]->green = CLAMP (colors[i]->green + ((other_colors[i]->green - colors[i]->green) * progress), 0, 1);
1744           colors[i]->blue = CLAMP (colors[i]->blue + ((other_colors[i]->blue - colors[i]->blue) * progress), 0, 1);
1745           colors[i]->alpha = CLAMP (colors[i]->alpha + ((other_colors[i]->alpha - colors[i]->alpha) * progress), 0, 1);
1746           gdk_rgba_free (other_colors[i]);
1747         }
1748     }
1749
1750   switch (border_style)
1751     {
1752     default:
1753       g_assert_not_reached ();
1754     case GTK_BORDER_STYLE_NONE:
1755     case GTK_BORDER_STYLE_SOLID:
1756       break;
1757     case GTK_BORDER_STYLE_INSET:
1758       color_shade (colors[1], 1.8, colors[1]);
1759       color_shade (colors[2], 1.8, colors[2]);
1760       break;
1761     case GTK_BORDER_STYLE_OUTSET:
1762       color_shade (colors[0], 1.8, colors[0]);
1763       color_shade (colors[3], 1.8, colors[3]);
1764       break;
1765     }
1766
1767   cairo_save (cr);
1768
1769   cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
1770
1771   _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1772   _gtk_rounded_box_apply_border_radius (&border_box, engine, state, junction);
1773   padding_box = border_box;
1774   _gtk_rounded_box_shrink (&padding_box, border.top, border.right, border.bottom, border.left);
1775
1776   switch (border_style)
1777     {
1778     case GTK_BORDER_STYLE_NONE:
1779       break;
1780     case GTK_BORDER_STYLE_SOLID:
1781     case GTK_BORDER_STYLE_INSET:
1782     case GTK_BORDER_STYLE_OUTSET:
1783
1784       if (gdk_rgba_equal (colors[0], colors[1]) &&
1785           gdk_rgba_equal (colors[0], colors[2]) &&
1786           gdk_rgba_equal (colors[0], colors[3]))
1787         {
1788           gdk_cairo_set_source_rgba (cr, colors[0]);
1789
1790           _gtk_rounded_box_path (&border_box, cr);
1791           _gtk_rounded_box_path (&padding_box, cr);
1792           cairo_fill (cr);
1793         }
1794       else
1795         {
1796           for (i = 0; i < 4; i++) 
1797             {
1798               if (hidden_side & current_side[i])
1799                 continue;
1800
1801               for (j = 0; j < 4; j++)
1802                 { 
1803                   if (hidden_side & current_side[j])
1804                     continue;
1805
1806                   if (i == j || 
1807                       gdk_rgba_equal (colors[i], colors[j]))
1808                     {
1809                       /* We were already painted when i == j */
1810                       if (i > j)
1811                         break;
1812
1813                       if (j == 0)
1814                         _gtk_rounded_box_path_top (&border_box, &padding_box, cr);
1815                       else if (j == 1)
1816                         _gtk_rounded_box_path_right (&border_box, &padding_box, cr);
1817                       else if (j == 2)
1818                         _gtk_rounded_box_path_bottom (&border_box, &padding_box, cr);
1819                       else if (j == 3)
1820                         _gtk_rounded_box_path_left (&border_box, &padding_box, cr);
1821                     }
1822                 }
1823               /* We were already painted when i == j */
1824               if (i > j)
1825                 continue;
1826
1827               gdk_cairo_set_source_rgba (cr, colors[i]);
1828
1829               cairo_fill (cr);
1830             }
1831         }
1832       break;
1833     }
1834
1835   cairo_restore (cr);
1836
1837   for (i = 0; i < 4; i++)
1838     gdk_rgba_free (colors[i]);
1839 }
1840
1841 static void
1842 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1843                                  cairo_t          *cr,
1844                                  gdouble           x,
1845                                  gdouble           y,
1846                                  gdouble           width,
1847                                  gdouble           height)
1848 {
1849   GtkStateFlags flags;
1850   GtkBorderStyle border_style;
1851   GtkJunctionSides junction;
1852   GtkBorderImage *border_image;
1853   GtkBorder border;
1854
1855   flags = gtk_theming_engine_get_state (engine);
1856   junction = gtk_theming_engine_get_junction_sides (engine);
1857   gtk_theming_engine_get_border (engine, flags, &border);
1858
1859   gtk_theming_engine_get (engine, flags,
1860                           "border-image", &border_image,
1861                           "border-style", &border_style,
1862                           NULL);
1863
1864   if (border_image != NULL)
1865     {
1866       _gtk_border_image_render (border_image, &border,
1867                                 cr, x, y, width, height);
1868       _gtk_border_image_unref (border_image);
1869     }
1870   else if (border_style != GTK_BORDER_STYLE_NONE)
1871     render_frame_internal (engine, cr,
1872                            x, y, width, height,
1873                            0, junction);
1874 }
1875
1876 static void
1877 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1878                                     cairo_t          *cr,
1879                                     gdouble           x,
1880                                     gdouble           y,
1881                                     gdouble           width,
1882                                     gdouble           height)
1883 {
1884   GtkStateFlags flags;
1885   GdkRGBA outline_color, fg_color;
1886   double vertical_overshoot;
1887   int diameter;
1888   double radius;
1889   double interp;                /* interpolation factor for center position */
1890   double x_double_horz, y_double_horz;
1891   double x_double_vert, y_double_vert;
1892   double x_double, y_double;
1893   gdouble angle;
1894   gint line_width;
1895   gboolean running, is_rtl;
1896   gdouble progress;
1897
1898   cairo_save (cr);
1899   flags = gtk_theming_engine_get_state (engine);
1900
1901   gtk_theming_engine_get_color (engine, flags, &fg_color);
1902   gtk_theming_engine_get_border_color (engine, flags, &outline_color);
1903
1904   running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1905   is_rtl = (gtk_theming_engine_get_direction (engine) == GTK_TEXT_DIR_RTL);
1906   line_width = 1;
1907
1908   if (!running)
1909     progress = (flags & GTK_STATE_FLAG_ACTIVE) ? 1 : 0;
1910
1911   if (!gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_HORIZONTAL))
1912     {
1913       if (is_rtl)
1914         angle = (G_PI) - ((G_PI / 2) * progress);
1915       else
1916         angle = (G_PI / 2) * progress;
1917     }
1918   else
1919     {
1920       if (is_rtl)
1921         angle = (G_PI / 2) + ((G_PI / 2) * progress);
1922       else
1923         angle = (G_PI / 2) - ((G_PI / 2) * progress);
1924     }
1925
1926   interp = progress;
1927
1928   /* Compute distance that the stroke extends beyonds the end
1929    * of the triangle we draw.
1930    */
1931   vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1932
1933   /* For odd line widths, we end the vertical line of the triangle
1934    * at a half pixel, so we round differently.
1935    */
1936   if (line_width % 2 == 1)
1937     vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1938   else
1939     vertical_overshoot = ceil (vertical_overshoot);
1940
1941   /* Adjust the size of the triangle we draw so that the entire stroke fits
1942    */
1943   diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1944
1945   /* If the line width is odd, we want the diameter to be even,
1946    * and vice versa, so force the sum to be odd. This relationship
1947    * makes the point of the triangle look right.
1948    */
1949   diameter -= (1 - (diameter + line_width) % 2);
1950
1951   radius = diameter / 2.;
1952
1953   /* Adjust the center so that the stroke is properly aligned with
1954    * the pixel grid. The center adjustment is different for the
1955    * horizontal and vertical orientations. For intermediate positions
1956    * we interpolate between the two.
1957    */
1958   x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1959   y_double_vert = (y + height / 2) - 0.5;
1960
1961   x_double_horz = (x + width / 2) - 0.5;
1962   y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1963
1964   x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1965   y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1966
1967   cairo_translate (cr, x_double, y_double);
1968   cairo_rotate (cr, angle);
1969
1970   cairo_move_to (cr, - radius / 2., - radius);
1971   cairo_line_to (cr,   radius / 2.,   0);
1972   cairo_line_to (cr, - radius / 2.,   radius);
1973   cairo_close_path (cr);
1974
1975   cairo_set_line_width (cr, line_width);
1976
1977   gdk_cairo_set_source_rgba (cr, &fg_color);
1978
1979   cairo_fill_preserve (cr);
1980
1981   gdk_cairo_set_source_rgba (cr, &outline_color);
1982   cairo_stroke (cr);
1983
1984   cairo_restore (cr);
1985 }
1986
1987 static void
1988 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1989                                  cairo_t          *cr,
1990                                  gdouble           x,
1991                                  gdouble           y,
1992                                  gdouble           width,
1993                                  gdouble           height)
1994 {
1995   GtkStateFlags flags;
1996   GdkRGBA color;
1997   gint line_width;
1998   gint8 *dash_list;
1999
2000   cairo_save (cr);
2001   flags = gtk_theming_engine_get_state (engine);
2002
2003   gtk_theming_engine_get_color (engine, flags, &color);
2004
2005   gtk_theming_engine_get_style (engine,
2006                                 "focus-line-width", &line_width,
2007                                 "focus-line-pattern", (gchar *) &dash_list,
2008                                 NULL);
2009
2010   cairo_set_line_width (cr, (gdouble) line_width);
2011
2012   if (dash_list[0])
2013     {
2014       gint n_dashes = strlen ((const gchar *) dash_list);
2015       gdouble *dashes = g_new (gdouble, n_dashes);
2016       gdouble total_length = 0;
2017       gdouble dash_offset;
2018       gint i;
2019
2020       for (i = 0; i < n_dashes; i++)
2021         {
2022           dashes[i] = dash_list[i];
2023           total_length += dash_list[i];
2024         }
2025
2026       /* The dash offset here aligns the pattern to integer pixels
2027        * by starting the dash at the right side of the left border
2028        * Negative dash offsets in cairo don't work
2029        * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
2030        */
2031       dash_offset = - line_width / 2.;
2032
2033       while (dash_offset < 0)
2034         dash_offset += total_length;
2035
2036       cairo_set_dash (cr, dashes, n_dashes, dash_offset);
2037       g_free (dashes);
2038     }
2039
2040   cairo_rectangle (cr,
2041                    x + line_width / 2.,
2042                    y + line_width / 2.,
2043                    width - line_width,
2044                    height - line_width);
2045
2046   gdk_cairo_set_source_rgba (cr, &color);
2047   cairo_stroke (cr);
2048
2049   cairo_restore (cr);
2050
2051   g_free (dash_list);
2052 }
2053
2054 static void
2055 gtk_theming_engine_render_line (GtkThemingEngine *engine,
2056                                 cairo_t          *cr,
2057                                 gdouble           x0,
2058                                 gdouble           y0,
2059                                 gdouble           x1,
2060                                 gdouble           y1)
2061 {
2062   GdkRGBA color;
2063   GtkStateFlags flags;
2064
2065   flags = gtk_theming_engine_get_state (engine);
2066   cairo_save (cr);
2067
2068   gtk_theming_engine_get_color (engine, flags, &color);
2069
2070   cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
2071   cairo_set_line_width (cr, 1);
2072
2073   cairo_move_to (cr, x0 + 0.5, y0 + 0.5);
2074   cairo_line_to (cr, x1 + 0.5, y1 + 0.5);
2075
2076   gdk_cairo_set_source_rgba (cr, &color);
2077   cairo_stroke (cr);
2078
2079   cairo_restore (cr);
2080 }
2081
2082 static void
2083 prepare_context_for_layout (cairo_t *cr,
2084                             gdouble x,
2085                             gdouble y,
2086                             PangoLayout *layout)
2087 {
2088   const PangoMatrix *matrix;
2089
2090   matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2091
2092   cairo_move_to (cr, x, y);
2093
2094   if (matrix)
2095     {
2096       cairo_matrix_t cairo_matrix;
2097
2098       cairo_matrix_init (&cairo_matrix,
2099                          matrix->xx, matrix->yx,
2100                          matrix->xy, matrix->yy,
2101                          matrix->x0, matrix->y0);
2102
2103       cairo_transform (cr, &cairo_matrix);
2104     }
2105 }
2106
2107 static void
2108 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2109                                   cairo_t          *cr,
2110                                   gdouble           x,
2111                                   gdouble           y,
2112                                   PangoLayout      *layout)
2113 {
2114   GdkRGBA fg_color;
2115   GtkShadow *text_shadow = NULL;
2116   GtkStateFlags flags;
2117   gdouble progress;
2118   gboolean running;
2119
2120   cairo_save (cr);
2121   flags = gtk_theming_engine_get_state (engine);
2122   gtk_theming_engine_get_color (engine, flags, &fg_color);
2123
2124   running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
2125
2126   if (running)
2127     {
2128       GtkStateFlags other_flags;
2129       GdkRGBA other_fg;
2130
2131       if (flags & GTK_STATE_FLAG_PRELIGHT)
2132         {
2133           other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
2134           progress = 1 - progress;
2135         }
2136       else
2137         other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
2138
2139       gtk_theming_engine_get_color (engine, other_flags, &other_fg);
2140
2141       fg_color.red = CLAMP (fg_color.red + ((other_fg.red - fg_color.red) * progress), 0, 1);
2142       fg_color.green = CLAMP (fg_color.green + ((other_fg.green - fg_color.green) * progress), 0, 1);
2143       fg_color.blue = CLAMP (fg_color.blue + ((other_fg.blue - fg_color.blue) * progress), 0, 1);
2144       fg_color.alpha = CLAMP (fg_color.alpha + ((other_fg.alpha - fg_color.alpha) * progress), 0, 1);
2145     }
2146
2147   gtk_theming_engine_get (engine, flags,
2148                           "text-shadow", &text_shadow,
2149                           NULL);
2150
2151   prepare_context_for_layout (cr, x, y, layout);
2152
2153   if (text_shadow != NULL)
2154     {
2155       _gtk_text_shadow_paint_layout (text_shadow, cr, layout);
2156       _gtk_shadow_unref (text_shadow);
2157     }
2158
2159   gdk_cairo_set_source_rgba (cr, &fg_color);
2160   pango_cairo_show_layout (cr, layout);
2161
2162   cairo_restore (cr);
2163 }
2164
2165 static void
2166 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2167                                   cairo_t          *cr,
2168                                   gdouble           x,
2169                                   gdouble           y,
2170                                   gdouble           width,
2171                                   gdouble           height,
2172                                   GtkOrientation    orientation)
2173 {
2174   const GtkWidgetPath *path;
2175   gint thickness;
2176
2177   path = gtk_theming_engine_get_path (engine);
2178
2179   gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2180   gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2181
2182   /* FIXME: thickness */
2183   thickness = 2;
2184
2185   if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2186     {
2187       if (orientation == GTK_ORIENTATION_VERTICAL)
2188         gtk_theming_engine_render_line (engine, cr,
2189                                         x + thickness,
2190                                         y + (gint) height / 2,
2191                                         x + width - thickness - 1,
2192                                         y + (gint) height / 2);
2193       else
2194         gtk_theming_engine_render_line (engine, cr,
2195                                         x + (gint) width / 2,
2196                                         y + thickness,
2197                                         x + (gint) width / 2,
2198                                         y + height - thickness - 1);
2199     }
2200 }
2201
2202 static void
2203 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2204                                      cairo_t          *cr,
2205                                      gdouble           x,
2206                                      gdouble           y,
2207                                      gdouble           width,
2208                                      gdouble           height,
2209                                      GtkPositionType   gap_side,
2210                                      gdouble           xy0_gap,
2211                                      gdouble           xy1_gap)
2212 {
2213   GtkJunctionSides junction;
2214   GtkStateFlags state;
2215   gint border_width;
2216   GtkCssBorderCornerRadius *top_left_radius, *top_right_radius;
2217   GtkCssBorderCornerRadius *bottom_left_radius, *bottom_right_radius;
2218   GtkCssBorderRadius border_radius = { { 0, },  };
2219   gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2220   GtkBorder border;
2221
2222   xc = yc = wc = hc = 0;
2223   state = gtk_theming_engine_get_state (engine);
2224   junction = gtk_theming_engine_get_junction_sides (engine);
2225
2226   gtk_theming_engine_get_border (engine, state, &border);
2227   gtk_theming_engine_get (engine, state,
2228                           /* Can't use border-radius as it's an int for
2229                            * backwards compat */
2230                           "border-top-left-radius", &top_left_radius,
2231                           "border-top-right-radius", &top_right_radius,
2232                           "border-bottom-right-radius", &bottom_right_radius,
2233                           "border-bottom-left-radius", &bottom_left_radius,
2234                           NULL);
2235
2236   if (top_left_radius)
2237     border_radius.top_left = *top_left_radius;
2238   g_free (top_left_radius);
2239   if (top_right_radius)
2240     border_radius.top_right = *top_right_radius;
2241   g_free (top_right_radius);
2242   if (bottom_right_radius)
2243     border_radius.bottom_right = *bottom_right_radius;
2244   g_free (bottom_right_radius);
2245   if (bottom_left_radius)
2246     border_radius.bottom_left = *bottom_left_radius;
2247   g_free (bottom_left_radius);
2248
2249   border_width = MIN (MIN (border.top, border.bottom),
2250                       MIN (border.left, border.right));
2251
2252   cairo_save (cr);
2253
2254   switch (gap_side)
2255     {
2256     case GTK_POS_TOP:
2257       xc = x + xy0_gap + border_width;
2258       yc = y;
2259       wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2260       hc = border_width;
2261
2262       if (xy0_gap < border_radius.top_left.horizontal)
2263         junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2264
2265       if (xy1_gap > width - border_radius.top_right.horizontal)
2266         junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2267       break;
2268     case GTK_POS_BOTTOM:
2269       xc = x + xy0_gap + border_width;
2270       yc = y + height - border_width;
2271       wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2272       hc = border_width;
2273
2274       if (xy0_gap < border_radius.bottom_left.horizontal)
2275         junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2276
2277       if (xy1_gap > width - border_radius.bottom_right.horizontal)
2278         junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2279
2280       break;
2281     case GTK_POS_LEFT:
2282       xc = x;
2283       yc = y + xy0_gap + border_width;
2284       wc = border_width;
2285       hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2286
2287       if (xy0_gap < border_radius.top_left.vertical)
2288         junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2289
2290       if (xy1_gap > height - border_radius.bottom_left.vertical)
2291         junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2292
2293       break;
2294     case GTK_POS_RIGHT:
2295       xc = x + width - border_width;
2296       yc = y + xy0_gap + border_width;
2297       wc = border_width;
2298       hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2299
2300       if (xy0_gap < border_radius.top_right.vertical)
2301         junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2302
2303       if (xy1_gap > height - border_radius.bottom_right.vertical)
2304         junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2305
2306       break;
2307     }
2308
2309   cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2310   cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2311   cairo_rectangle (cr, x0, yc, xc - x0, hc);
2312   cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2313   cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2314   cairo_clip (cr);
2315
2316   render_frame_internal (engine, cr,
2317                          x, y, width, height,
2318                          0, junction);
2319
2320   cairo_restore (cr);
2321 }
2322
2323 static void
2324 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2325                                      cairo_t          *cr,
2326                                      gdouble           x,
2327                                      gdouble           y,
2328                                      gdouble           width,
2329                                      gdouble           height,
2330                                      GtkPositionType   gap_side)
2331 {
2332   GtkJunctionSides junction = 0;
2333   guint hidden_side = 0;
2334
2335   cairo_save (cr);
2336
2337   switch (gap_side)
2338     {
2339     case GTK_POS_LEFT:
2340       junction = GTK_JUNCTION_LEFT;
2341       hidden_side = SIDE_LEFT;
2342
2343       cairo_translate (cr, x + width, y);
2344       cairo_rotate (cr, G_PI / 2);
2345       break;
2346     case GTK_POS_RIGHT:
2347       junction = GTK_JUNCTION_RIGHT;
2348       hidden_side = SIDE_RIGHT;
2349
2350       cairo_translate (cr, x, y + height);
2351       cairo_rotate (cr, - G_PI / 2);
2352       break;
2353     case GTK_POS_TOP:
2354       junction = GTK_JUNCTION_TOP;
2355       hidden_side = SIDE_TOP;
2356
2357       cairo_translate (cr, x + width, y + height);
2358       cairo_rotate (cr, G_PI);
2359       break;
2360     case GTK_POS_BOTTOM:
2361       junction = GTK_JUNCTION_BOTTOM;
2362       hidden_side = SIDE_BOTTOM;
2363
2364       cairo_translate (cr, x, y);
2365       break;
2366     }
2367
2368   if (gap_side == GTK_POS_TOP ||
2369       gap_side == GTK_POS_BOTTOM)
2370     render_background_internal (engine, cr,
2371                                 0, 0, width, height,
2372                                 GTK_JUNCTION_BOTTOM);
2373   else
2374     render_background_internal (engine, cr,
2375                                 0, 0, height, width,
2376                                 GTK_JUNCTION_BOTTOM);
2377   cairo_restore (cr);
2378
2379   cairo_save (cr);
2380
2381   render_frame_internal (engine, cr,
2382                          x, y, width, height,
2383                          hidden_side, junction);
2384
2385   cairo_restore (cr);
2386 }
2387
2388 static void
2389 render_dot (cairo_t       *cr,
2390             const GdkRGBA *lighter,
2391             const GdkRGBA *darker,
2392             gdouble        x,
2393             gdouble        y,
2394             gdouble        size)
2395 {
2396   size = CLAMP ((gint) size, 2, 3);
2397
2398   if (size == 2)
2399     {
2400       gdk_cairo_set_source_rgba (cr, lighter);
2401       cairo_rectangle (cr, x, y, 1, 1);
2402       cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2403       cairo_fill (cr);
2404     }
2405   else if (size == 3)
2406     {
2407       gdk_cairo_set_source_rgba (cr, lighter);
2408       cairo_rectangle (cr, x, y, 2, 1);
2409       cairo_rectangle (cr, x, y, 1, 2);
2410       cairo_fill (cr);
2411
2412       gdk_cairo_set_source_rgba (cr, darker);
2413       cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2414       cairo_rectangle (cr, x + 2, y, 1, 2);
2415       cairo_fill (cr);
2416     }
2417 }
2418
2419 static void
2420 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2421                                   cairo_t          *cr,
2422                                   gdouble           x,
2423                                   gdouble           y,
2424                                   gdouble           width,
2425                                   gdouble           height)
2426 {
2427   GtkStateFlags flags;
2428   GdkRGBA bg_color, lighter, darker;
2429   GtkJunctionSides sides;
2430   gint xx, yy;
2431
2432   cairo_save (cr);
2433   flags = gtk_theming_engine_get_state (engine);
2434
2435   cairo_set_line_width (cr, 1.0);
2436   sides = gtk_theming_engine_get_junction_sides (engine);
2437   gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2438
2439   color_shade (&bg_color, 0.7, &darker);
2440   color_shade (&bg_color, 1.3, &lighter);
2441
2442   render_background_internal (engine, cr, x, y, width, height, sides);
2443
2444   if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_GRIP))
2445     {
2446       /* reduce confusing values to a meaningful state */
2447       if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2448         sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2449
2450       if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2451         sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2452
2453       if (sides == 0)
2454         sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2455
2456       /* align drawing area to the connected side */
2457       if (sides == GTK_JUNCTION_LEFT)
2458         {
2459           if (height < width)
2460             width = height;
2461         }
2462       else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2463         {
2464           if (width < height)
2465             height = width;
2466           else if (height < width)
2467             width = height;
2468         }
2469       else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2470         {
2471           /* make it square, aligning to bottom left */
2472           if (width < height)
2473             {
2474               y += (height - width);
2475               height = width;
2476             }
2477           else if (height < width)
2478             width = height;
2479         }
2480       else if (sides == GTK_JUNCTION_RIGHT)
2481         {
2482           /* aligning to right */
2483           if (height < width)
2484             {
2485               x += (width - height);
2486               width = height;
2487             }
2488         }
2489       else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2490         {
2491           if (width < height)
2492             height = width;
2493           else if (height < width)
2494             {
2495               x += (width - height);
2496               width = height;
2497             }
2498         }
2499       else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2500         {
2501           /* make it square, aligning to bottom right */
2502           if (width < height)
2503             {
2504               y += (height - width);
2505               height = width;
2506             }
2507           else if (height < width)
2508             {
2509               x += (width - height);
2510               width = height;
2511             }
2512         }
2513       else if (sides == GTK_JUNCTION_TOP)
2514         {
2515           if (width < height)
2516             height = width;
2517         }
2518       else if (sides == GTK_JUNCTION_BOTTOM)
2519         {
2520           /* align to bottom */
2521           if (width < height)
2522             {
2523               y += (height - width);
2524               height = width;
2525             }
2526         }
2527       else
2528         g_assert_not_reached ();
2529
2530       if (sides == GTK_JUNCTION_LEFT ||
2531           sides == GTK_JUNCTION_RIGHT)
2532         {
2533           gint xi;
2534
2535           xi = x;
2536
2537           while (xi < x + width)
2538             {
2539               gdk_cairo_set_source_rgba (cr, &lighter);
2540               add_path_line (cr, x, y, x, y + height);
2541               cairo_stroke (cr);
2542               xi++;
2543
2544               gdk_cairo_set_source_rgba (cr, &darker);
2545               add_path_line (cr, xi, y, xi, y + height);
2546               cairo_stroke (cr);
2547               xi += 2;
2548             }
2549         }
2550       else if (sides == GTK_JUNCTION_TOP ||
2551                sides == GTK_JUNCTION_BOTTOM)
2552         {
2553           gint yi;
2554
2555           yi = y;
2556
2557           while (yi < y + height)
2558             {
2559               gdk_cairo_set_source_rgba (cr, &lighter);
2560               add_path_line (cr, x, yi, x + width, yi);
2561               cairo_stroke (cr);
2562               yi++;
2563
2564               gdk_cairo_set_source_rgba (cr, &darker);
2565               add_path_line (cr, x, yi, x + width, yi);
2566               cairo_stroke (cr);
2567               yi += 2;
2568             }
2569         }
2570       else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2571         {
2572           gint xi, yi;
2573
2574           xi = x + width;
2575           yi = y + height;
2576
2577           while (xi > x + 3)
2578             {
2579               gdk_cairo_set_source_rgba (cr, &darker);
2580               add_path_line (cr, xi, y, x, yi);
2581               cairo_stroke (cr);
2582
2583               --xi;
2584               --yi;
2585
2586               add_path_line (cr, xi, y, x, yi);
2587               cairo_stroke (cr);
2588
2589               --xi;
2590               --yi;
2591
2592               gdk_cairo_set_source_rgba (cr, &lighter);
2593               add_path_line (cr, xi, y, x, yi);
2594               cairo_stroke (cr);
2595
2596               xi -= 3;
2597               yi -= 3;
2598             }
2599         }
2600       else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2601         {
2602           gint xi, yi;
2603
2604           xi = x;
2605           yi = y + height;
2606
2607           while (xi < (x + width - 3))
2608             {
2609               gdk_cairo_set_source_rgba (cr, &lighter);
2610               add_path_line (cr, xi, y, x + width, yi);
2611               cairo_stroke (cr);
2612
2613               ++xi;
2614               --yi;
2615
2616               gdk_cairo_set_source_rgba (cr, &darker);
2617               add_path_line (cr, xi, y, x + width, yi);
2618               cairo_stroke (cr);
2619
2620               ++xi;
2621               --yi;
2622
2623               add_path_line (cr, xi, y, x + width, yi);
2624               cairo_stroke (cr);
2625
2626               xi += 3;
2627               yi -= 3;
2628             }
2629         }
2630       else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2631         {
2632           gint xi, yi;
2633
2634           xi = x + width;
2635           yi = y;
2636
2637           while (xi > x + 3)
2638             {
2639               gdk_cairo_set_source_rgba (cr, &darker);
2640               add_path_line (cr, x, yi, xi, y + height);
2641               cairo_stroke (cr);
2642
2643               --xi;
2644               ++yi;
2645
2646               add_path_line (cr, x, yi, xi, y + height);
2647               cairo_stroke (cr);
2648
2649               --xi;
2650               ++yi;
2651
2652               gdk_cairo_set_source_rgba (cr, &lighter);
2653               add_path_line (cr, x, yi, xi, y + height);
2654               cairo_stroke (cr);
2655
2656               xi -= 3;
2657               yi += 3;
2658             }
2659         }
2660       else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2661         {
2662           gint xi, yi;
2663
2664           xi = x;
2665           yi = y;
2666
2667           while (xi < (x + width - 3))
2668             {
2669               gdk_cairo_set_source_rgba (cr, &lighter);
2670               add_path_line (cr, xi, y + height, x + width, yi);
2671               cairo_stroke (cr);
2672
2673               ++xi;
2674               ++yi;
2675
2676               gdk_cairo_set_source_rgba (cr, &darker);
2677               add_path_line (cr, xi, y + height, x + width, yi);
2678               cairo_stroke (cr);
2679
2680               ++xi;
2681               ++yi;
2682
2683               add_path_line (cr, xi, y + height, x + width, yi);
2684               cairo_stroke (cr);
2685
2686               xi += 3;
2687               yi += 3;
2688             }
2689         }
2690     }
2691   else if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_PANE_SEPARATOR))
2692     {
2693       if (width > height)
2694         for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2695           render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2696       else
2697         for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2698           render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2699     }
2700   else
2701     {
2702       for (yy = y; yy < y + height; yy += 3)
2703         for (xx = x; xx < x + width; xx += 6)
2704           {
2705             render_dot (cr, &lighter, &darker, xx, yy, 2);
2706             render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2707           }
2708     }
2709
2710   cairo_restore (cr);
2711 }
2712
2713 void
2714 _gtk_theming_engine_paint_spinner (cairo_t *cr,
2715                                    gdouble  radius,
2716                                    gdouble  progress,
2717                                    GdkRGBA *color)
2718 {
2719   guint num_steps, step;
2720   gdouble half;
2721   gint i;
2722
2723   num_steps = 12;
2724
2725   if (progress >= 0)
2726     step = (guint) (progress * num_steps);
2727   else
2728     step = 0;
2729
2730   cairo_save (cr);
2731
2732   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2733   cairo_set_line_width (cr, 2.0);
2734
2735   half = num_steps / 2;
2736
2737   for (i = 0; i < num_steps; i++)
2738     {
2739       gint inset = 0.7 * radius;
2740
2741       /* transparency is a function of time and intial value */
2742       gdouble t = 1.0 - (gdouble) ((i + step) % num_steps) / num_steps;
2743       gdouble xscale = - sin (i * G_PI / half);
2744       gdouble yscale = - cos (i * G_PI / half);
2745
2746       cairo_set_source_rgba (cr,
2747                              color->red,
2748                              color->green,
2749                              color->blue,
2750                              color->alpha * t);
2751
2752       cairo_move_to (cr,
2753                      (radius - inset) * xscale,
2754                      (radius - inset) * yscale);
2755       cairo_line_to (cr,
2756                      radius * xscale,
2757                      radius * yscale);
2758
2759       cairo_stroke (cr);
2760     }
2761
2762   cairo_restore (cr);
2763 }
2764
2765 static void
2766 render_spinner (GtkThemingEngine *engine,
2767                 cairo_t          *cr,
2768                 gdouble           x,
2769                 gdouble           y,
2770                 gdouble           width,
2771                 gdouble           height)
2772 {
2773   GtkStateFlags state;
2774   GtkShadow *shadow;
2775   GdkRGBA color;
2776   gdouble progress;
2777   gdouble radius;
2778
2779   state = gtk_theming_engine_get_state (engine);
2780
2781   if (!gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2782     progress = -1;
2783
2784   radius = MIN (width / 2, height / 2);
2785
2786   gtk_theming_engine_get_color (engine, state, &color);
2787   gtk_theming_engine_get (engine, state,
2788                           "icon-shadow", &shadow,
2789                           NULL);
2790
2791   cairo_save (cr);
2792   cairo_translate (cr, x + width / 2, y + height / 2);
2793
2794   if (shadow != NULL)
2795     {
2796       _gtk_icon_shadow_paint_spinner (shadow, cr,
2797                                       radius,
2798                                       progress);
2799       _gtk_shadow_unref (shadow);
2800     }
2801
2802   _gtk_theming_engine_paint_spinner (cr,
2803                                      radius,
2804                                      progress,
2805                                      &color);
2806
2807   cairo_restore (cr);
2808 }
2809
2810 static void
2811 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2812                                     cairo_t          *cr,
2813                                     gdouble           x,
2814                                     gdouble           y,
2815                                     gdouble           width,
2816                                     gdouble           height)
2817 {
2818   if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_SPINNER))
2819     {
2820       render_spinner (engine, cr, x, y, width, height);
2821     }
2822   else
2823     {
2824       gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2825       gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2826     }
2827 }
2828
2829 static GdkPixbuf *
2830 scale_or_ref (GdkPixbuf *src,
2831               gint       width,
2832               gint       height)
2833 {
2834   if (width == gdk_pixbuf_get_width (src) &&
2835       height == gdk_pixbuf_get_height (src))
2836     return g_object_ref (src);
2837   else
2838     return gdk_pixbuf_scale_simple (src,
2839                                     width, height,
2840                                     GDK_INTERP_BILINEAR);
2841 }
2842
2843 static gboolean
2844 lookup_icon_size (GtkThemingEngine *engine,
2845                   GtkIconSize       size,
2846                   gint             *width,
2847                   gint             *height)
2848 {
2849   GdkScreen *screen;
2850   GtkSettings *settings;
2851
2852   screen = gtk_theming_engine_get_screen (engine);
2853   settings = gtk_settings_get_for_screen (screen);
2854
2855   return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2856 }
2857
2858 static void
2859 colorshift_source (cairo_t *cr,
2860                    gdouble shift)
2861 {
2862   cairo_pattern_t *source;
2863
2864   cairo_save (cr);
2865   cairo_paint (cr);
2866
2867   source = cairo_pattern_reference (cairo_get_source (cr));
2868
2869   cairo_set_source_rgb (cr, shift, shift, shift);
2870   cairo_set_operator (cr, CAIRO_OPERATOR_COLOR_DODGE);
2871
2872   cairo_mask (cr, source);
2873
2874   cairo_pattern_destroy (source);
2875   cairo_restore (cr);
2876 }
2877
2878 static GdkPixbuf *
2879 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine    *engine,
2880                                        const GtkIconSource *source,
2881                                        GtkIconSize          size)
2882 {
2883   GdkPixbuf *scaled;
2884   GdkPixbuf *stated;
2885   GdkPixbuf *base_pixbuf;
2886   GtkStateFlags state;
2887   gint width = 1;
2888   gint height = 1;
2889   cairo_t *cr;
2890   cairo_surface_t *surface;
2891
2892   base_pixbuf = gtk_icon_source_get_pixbuf (source);
2893   state = gtk_theming_engine_get_state (engine);
2894
2895   g_return_val_if_fail (base_pixbuf != NULL, NULL);
2896
2897   if (size != (GtkIconSize) -1 &&
2898       !lookup_icon_size (engine, size, &width, &height))
2899     {
2900       g_warning (G_STRLOC ": invalid icon size '%d'", size);
2901       return NULL;
2902     }
2903
2904   /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2905    * leave it alone.
2906    */
2907   if (size != (GtkIconSize) -1 &&
2908       gtk_icon_source_get_size_wildcarded (source))
2909     scaled = scale_or_ref (base_pixbuf, width, height);
2910   else
2911     scaled = g_object_ref (base_pixbuf);
2912
2913   /* If the state was wildcarded, then generate a state. */
2914   if (gtk_icon_source_get_state_wildcarded (source))
2915     {
2916       if (state & GTK_STATE_FLAG_INSENSITIVE)
2917         {
2918           surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2919                                                 gdk_pixbuf_get_width (scaled),
2920                                                 gdk_pixbuf_get_height (scaled));
2921           cr = cairo_create (surface);
2922           gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2923           cairo_paint_with_alpha (cr, 0.5);
2924
2925           cairo_destroy (cr);
2926
2927           g_object_unref (scaled);
2928           stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2929                                                 cairo_image_surface_get_width (surface),
2930                                                 cairo_image_surface_get_height (surface));
2931           cairo_surface_destroy (surface);
2932         }
2933       else if (state & GTK_STATE_FLAG_PRELIGHT)
2934         {
2935           surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2936                                                 gdk_pixbuf_get_width (scaled),
2937                                                 gdk_pixbuf_get_height (scaled));
2938
2939           cr = cairo_create (surface);
2940           gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2941           colorshift_source (cr, 0.10);
2942
2943           cairo_destroy (cr);
2944
2945           g_object_unref (scaled);
2946           stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2947                                                 cairo_image_surface_get_width (surface),
2948                                                 cairo_image_surface_get_height (surface));
2949           cairo_surface_destroy (surface);
2950         }
2951       else
2952         stated = scaled;
2953     }
2954   else
2955     stated = scaled;
2956
2957   return stated;
2958 }
2959
2960 static void
2961 gtk_theming_engine_render_icon (GtkThemingEngine *engine,
2962                                 cairo_t *cr,
2963                                 GdkPixbuf *pixbuf,
2964                                 gdouble x,
2965                                 gdouble y)
2966 {
2967   GtkStateFlags state;
2968   GtkShadow *icon_shadow;
2969
2970   state = gtk_theming_engine_get_state (engine);
2971
2972   cairo_save (cr);
2973
2974   gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2975
2976   gtk_theming_engine_get (engine, state,
2977                           "icon-shadow", &icon_shadow,
2978                           NULL);
2979
2980   if (icon_shadow != NULL)
2981     {
2982       _gtk_icon_shadow_paint (icon_shadow, cr);
2983       _gtk_shadow_unref (icon_shadow);
2984     }
2985
2986   cairo_paint (cr);
2987
2988   cairo_restore (cr);
2989 }
2990