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