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