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