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