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