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