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