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