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