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