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