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