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