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