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