1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <gtk/gtkthemingengine.h>
24 #include <gtk/gtkstylecontext.h>
25 #include <gtk/gtkintl.h>
27 #include "gtkprivate.h"
28 #include "gtkmodulesprivate.h"
29 #include "gtkborderimageprivate.h"
31 #include "gtkcssarrayvalueprivate.h"
32 #include "gtkcssenumvalueprivate.h"
33 #include "gtkcssrgbavalueprivate.h"
34 #include "gtkcssshadowvalueprivate.h"
35 #include "gtkcsstypesprivate.h"
36 #include "gtkthemingengineprivate.h"
37 #include "gtkroundedboxprivate.h"
38 #include "gtkthemingbackgroundprivate.h"
40 #include "fallback-c89.c"
43 * SECTION:gtkthemingengine
44 * @Short_description: Theming renderers
45 * @Title: GtkThemingEngine
46 * @See_also: #GtkStyleContext
48 * #GtkThemingEngine is the object used for rendering themed content
49 * in GTK+ widgets. Even though GTK+ has a default implementation,
50 * it can be overridden in CSS files by enforcing a #GtkThemingEngine
51 * object to be loaded as a module.
53 * In order to implement a theming engine, a #GtkThemingEngine subclass
54 * must be created, alongside the CSS file that will reference it, the
55 * theming engine would be created as an .so library, and installed in
56 * $(gtk-modules-dir)/theming-engines/.
58 * #GtkThemingEngine<!-- -->s have limited access to the object they are
59 * rendering, the #GtkThemingEngine API has read-only accessors to the
60 * style information contained in the rendered object's #GtkStyleContext.
68 struct GtkThemingEnginePrivate
70 GtkStyleContext *context;
74 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
76 static void gtk_theming_engine_finalize (GObject *object);
77 static void gtk_theming_engine_impl_set_property (GObject *object,
81 static void gtk_theming_engine_impl_get_property (GObject *object,
86 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
92 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
98 static void gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
104 static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
110 static void gtk_theming_engine_render_frame (GtkThemingEngine *engine,
116 static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
122 static void gtk_theming_engine_render_focus (GtkThemingEngine *engine,
128 static void gtk_theming_engine_render_layout (GtkThemingEngine *engine,
132 PangoLayout *layout);
133 static void gtk_theming_engine_render_line (GtkThemingEngine *engine,
139 static void gtk_theming_engine_render_slider (GtkThemingEngine *engine,
145 GtkOrientation orientation);
146 static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
152 GtkPositionType gap_side,
155 static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
161 GtkPositionType gap_side);
162 static void gtk_theming_engine_render_handle (GtkThemingEngine *engine,
168 static void gtk_theming_engine_render_activity (GtkThemingEngine *engine,
174 static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
175 const GtkIconSource *source,
177 static void gtk_theming_engine_render_icon (GtkThemingEngine *engine,
183 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
186 typedef struct GtkThemingModule GtkThemingModule;
187 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
189 struct GtkThemingModule
191 GTypeModule parent_instance;
195 void (*init) (GTypeModule *module);
197 GtkThemingEngine * (*create_engine) (void);
200 struct GtkThemingModuleClass
202 GTypeModuleClass parent_class;
205 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
206 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
207 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
209 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
212 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
214 GObjectClass *object_class = G_OBJECT_CLASS (klass);
216 object_class->finalize = gtk_theming_engine_finalize;
217 object_class->set_property = gtk_theming_engine_impl_set_property;
218 object_class->get_property = gtk_theming_engine_impl_get_property;
220 klass->render_icon = gtk_theming_engine_render_icon;
221 klass->render_check = gtk_theming_engine_render_check;
222 klass->render_option = gtk_theming_engine_render_option;
223 klass->render_arrow = gtk_theming_engine_render_arrow;
224 klass->render_background = gtk_theming_engine_render_background;
225 klass->render_frame = gtk_theming_engine_render_frame;
226 klass->render_expander = gtk_theming_engine_render_expander;
227 klass->render_focus = gtk_theming_engine_render_focus;
228 klass->render_layout = gtk_theming_engine_render_layout;
229 klass->render_line = gtk_theming_engine_render_line;
230 klass->render_slider = gtk_theming_engine_render_slider;
231 klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
232 klass->render_extension = gtk_theming_engine_render_extension;
233 klass->render_handle = gtk_theming_engine_render_handle;
234 klass->render_activity = gtk_theming_engine_render_activity;
235 klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
238 * GtkThemingEngine:name:
240 * The theming engine name, this name will be used when registering
241 * custom properties, for a theming engine named "Clearlooks" registering
242 * a "glossy" custom property, it could be referenced in the CSS file as
245 * -Clearlooks-glossy: true;
250 g_object_class_install_property (object_class,
252 g_param_spec_string ("name",
254 P_("Theming engine name"),
256 G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));
258 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
262 gtk_theming_engine_init (GtkThemingEngine *engine)
264 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
268 gtk_theming_engine_finalize (GObject *object)
270 GtkThemingEnginePrivate *priv;
272 priv = GTK_THEMING_ENGINE (object)->priv;
275 G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
279 gtk_theming_engine_impl_set_property (GObject *object,
284 GtkThemingEnginePrivate *priv;
286 priv = GTK_THEMING_ENGINE (object)->priv;
294 priv->name = g_value_dup_string (value);
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 gtk_theming_engine_impl_get_property (GObject *object,
308 GtkThemingEnginePrivate *priv;
310 priv = GTK_THEMING_ENGINE (object)->priv;
315 g_value_set_string (value, priv->name);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
325 GtkStyleContext *context)
327 GtkThemingEnginePrivate *priv;
329 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
330 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
333 priv->context = context;
337 _gtk_theming_engine_get_context (GtkThemingEngine *engine)
339 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
341 return engine->priv->context;
345 _gtk_theming_engine_peek_property (GtkThemingEngine *engine,
348 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
350 return _gtk_style_context_peek_property (engine->priv->context, property_id);
354 _gtk_theming_engine_get_number (GtkThemingEngine *engine,
356 double one_hundred_percent)
358 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0.0);
360 return _gtk_style_context_get_number (engine->priv->context, property_id, one_hundred_percent);
364 * gtk_theming_engine_get_property:
365 * @engine: a #GtkThemingEngine
366 * @property: the property name
367 * @state: state to retrieve the value for
368 * @value: (out) (transfer full): return location for the property value,
369 * you must free this memory using g_value_unset() once you are
372 * Gets a property value as retrieved from the style settings that apply
373 * to the currently rendered element.
378 gtk_theming_engine_get_property (GtkThemingEngine *engine,
379 const gchar *property,
383 GtkThemingEnginePrivate *priv;
385 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
386 g_return_if_fail (property != NULL);
387 g_return_if_fail (value != NULL);
390 gtk_style_context_get_property (priv->context, property, state, value);
394 * gtk_theming_engine_get_valist:
395 * @engine: a #GtkThemingEngine
396 * @state: state to retrieve values for
397 * @args: va_list of property name/return location pairs, followed by %NULL
399 * Retrieves several style property values that apply to the currently
405 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
409 GtkThemingEnginePrivate *priv;
411 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
414 gtk_style_context_get_valist (priv->context, state, args);
418 * gtk_theming_engine_get:
419 * @engine: a #GtkThemingEngine
420 * @state: state to retrieve values for
421 * @...: property name /return value pairs, followed by %NULL
423 * Retrieves several style property values that apply to the currently
429 gtk_theming_engine_get (GtkThemingEngine *engine,
433 GtkThemingEnginePrivate *priv;
436 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
440 va_start (args, state);
441 gtk_style_context_get_valist (priv->context, state, args);
446 * gtk_theming_engine_get_style_property:
447 * @engine: a #GtkThemingEngine
448 * @property_name: the name of the widget style property
449 * @value: Return location for the property value, free with
450 * g_value_unset() after use.
452 * Gets the value for a widget style property.
457 gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
458 const gchar *property_name,
461 GtkThemingEnginePrivate *priv;
463 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
464 g_return_if_fail (property_name != NULL);
467 gtk_style_context_get_style_property (priv->context, property_name, value);
471 * gtk_theming_engine_get_style_valist:
472 * @engine: a #GtkThemingEngine
473 * @args: va_list of property name/return location pairs, followed by %NULL
475 * Retrieves several widget style properties from @engine according to the
476 * currently rendered content's style.
481 gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
484 GtkThemingEnginePrivate *priv;
486 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
489 gtk_style_context_get_style_valist (priv->context, args);
493 * gtk_theming_engine_get_style:
494 * @engine: a #GtkThemingEngine
495 * @...: property name /return value pairs, followed by %NULL
497 * Retrieves several widget style properties from @engine according
498 * to the currently rendered content's style.
503 gtk_theming_engine_get_style (GtkThemingEngine *engine,
506 GtkThemingEnginePrivate *priv;
509 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
513 va_start (args, engine);
514 gtk_style_context_get_style_valist (priv->context, args);
519 * gtk_theming_engine_lookup_color:
520 * @engine: a #GtkThemingEngine
521 * @color_name: color name to lookup
522 * @color: (out): Return location for the looked up color
524 * Looks up and resolves a color name in the current style's color map.
526 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
529 gtk_theming_engine_lookup_color (GtkThemingEngine *engine,
530 const gchar *color_name,
533 GtkThemingEnginePrivate *priv;
535 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
536 g_return_val_if_fail (color_name != NULL, FALSE);
539 return gtk_style_context_lookup_color (priv->context, color_name, color);
543 * gtk_theming_engine_get_state:
544 * @engine: a #GtkThemingEngine
546 * returns the state used when rendering.
548 * Returns: the state flags
553 gtk_theming_engine_get_state (GtkThemingEngine *engine)
555 GtkThemingEnginePrivate *priv;
557 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
560 return gtk_style_context_get_state (priv->context);
564 * gtk_theming_engine_state_is_running:
565 * @engine: a #GtkThemingEngine
566 * @state: a widget state
567 * @progress: (out): return location for the transition progress
569 * Returns %TRUE if there is a transition animation running for the
570 * current region (see gtk_style_context_push_animatable_region()).
572 * If @progress is not %NULL, the animation progress will be returned
573 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
574 * it's closest to being %TRUE. This means transition animations will
575 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
576 * it's being set to %FALSE.
578 * Returns: %TRUE if there is a running transition animation for @state.
583 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
587 GtkThemingEnginePrivate *priv;
589 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
592 return gtk_style_context_state_is_running (priv->context, state, progress);
596 * gtk_theming_engine_get_path:
597 * @engine: a #GtkThemingEngine
599 * Returns the widget path used for style matching.
601 * Returns: (transfer none): A #GtkWidgetPath
605 const GtkWidgetPath *
606 gtk_theming_engine_get_path (GtkThemingEngine *engine)
608 GtkThemingEnginePrivate *priv;
610 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
613 return gtk_style_context_get_path (priv->context);
617 * gtk_theming_engine_has_class:
618 * @engine: a #GtkThemingEngine
619 * @style_class: class name to look up
621 * Returns %TRUE if the currently rendered contents have
622 * defined the given class name.
624 * Returns: %TRUE if @engine has @class_name defined
629 gtk_theming_engine_has_class (GtkThemingEngine *engine,
630 const gchar *style_class)
632 GtkThemingEnginePrivate *priv;
634 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
637 return gtk_style_context_has_class (priv->context, style_class);
641 * gtk_theming_engine_has_region:
642 * @engine: a #GtkThemingEngine
643 * @style_region: a region name
644 * @flags: (out) (allow-none): return location for region flags
646 * Returns %TRUE if the currently rendered contents have the
647 * region defined. If @flags_return is not %NULL, it is set
648 * to the flags affecting the region.
650 * Returns: %TRUE if region is defined
655 gtk_theming_engine_has_region (GtkThemingEngine *engine,
656 const gchar *style_region,
657 GtkRegionFlags *flags)
659 GtkThemingEnginePrivate *priv;
664 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
667 return gtk_style_context_has_region (priv->context, style_region, flags);
671 * gtk_theming_engine_get_direction:
672 * @engine: a #GtkThemingEngine
674 * Returns the widget direction used for rendering.
676 * Returns: the widget direction
681 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
683 GtkThemingEnginePrivate *priv;
685 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
688 return gtk_style_context_get_direction (priv->context);
692 * gtk_theming_engine_get_junction_sides:
693 * @engine: a #GtkThemingEngine
695 * Returns the widget direction used for rendering.
697 * Returns: the widget direction
702 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
704 GtkThemingEnginePrivate *priv;
706 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
709 return gtk_style_context_get_junction_sides (priv->context);
713 * gtk_theming_engine_get_color:
714 * @engine: a #GtkThemingEngine
715 * @state: state to retrieve the color for
716 * @color: (out): return value for the foreground color
718 * Gets the foreground color for a given state.
723 gtk_theming_engine_get_color (GtkThemingEngine *engine,
727 GtkThemingEnginePrivate *priv;
729 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
732 gtk_style_context_get_color (priv->context, state, color);
736 * gtk_theming_engine_get_background_color:
737 * @engine: a #GtkThemingEngine
738 * @state: state to retrieve the color for
739 * @color: (out): return value for the background color
741 * Gets the background color for a given state.
746 gtk_theming_engine_get_background_color (GtkThemingEngine *engine,
750 GtkThemingEnginePrivate *priv;
752 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
755 gtk_style_context_get_background_color (priv->context, state, color);
759 * gtk_theming_engine_get_border_color:
760 * @engine: a #GtkThemingEngine
761 * @state: state to retrieve the color for
762 * @color: (out): return value for the border color
764 * Gets the border color for a given state.
769 gtk_theming_engine_get_border_color (GtkThemingEngine *engine,
773 GtkThemingEnginePrivate *priv;
775 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
778 gtk_style_context_get_border_color (priv->context, state, color);
782 * gtk_theming_engine_get_border:
783 * @engine: a #GtkthemingEngine
784 * @state: state to retrieve the border for
785 * @border: (out): return value for the border settings
787 * Gets the border for a given state as a #GtkBorder.
792 gtk_theming_engine_get_border (GtkThemingEngine *engine,
796 GtkThemingEnginePrivate *priv;
798 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
801 gtk_style_context_get_border (priv->context, state, border);
805 * gtk_theming_engine_get_padding:
806 * @engine: a #GtkthemingEngine
807 * @state: state to retrieve the padding for
808 * @padding: (out): return value for the padding settings
810 * Gets the padding for a given state as a #GtkBorder.
815 gtk_theming_engine_get_padding (GtkThemingEngine *engine,
819 GtkThemingEnginePrivate *priv;
821 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
824 gtk_style_context_get_padding (priv->context, state, padding);
828 * gtk_theming_engine_get_margin:
829 * @engine: a #GtkThemingEngine
830 * @state: state to retrieve the border for
831 * @margin: (out): return value for the margin settings
833 * Gets the margin for a given state as a #GtkBorder.
838 gtk_theming_engine_get_margin (GtkThemingEngine *engine,
842 GtkThemingEnginePrivate *priv;
844 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
847 gtk_style_context_get_margin (priv->context, state, margin);
851 * gtk_theming_engine_get_font:
852 * @engine: a #GtkThemingEngine
853 * @state: state to retrieve the font for
855 * Returns the font description for a given state.
857 * Returns: (transfer none): the #PangoFontDescription for the given
858 * state. This object is owned by GTK+ and should not be
863 const PangoFontDescription *
864 gtk_theming_engine_get_font (GtkThemingEngine *engine,
867 GtkThemingEnginePrivate *priv;
869 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
872 return gtk_style_context_get_font (priv->context, state);
875 /* GtkThemingModule */
878 gtk_theming_module_load (GTypeModule *type_module)
880 GtkThemingModule *theming_module;
882 gchar *name, *module_path;
884 theming_module = GTK_THEMING_MODULE (type_module);
885 name = theming_module->name;
886 module_path = _gtk_find_module (name, "theming-engines");
891 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
892 g_free (module_path);
897 if (!g_module_symbol (module, "theme_init",
898 (gpointer *) &theming_module->init) ||
899 !g_module_symbol (module, "theme_exit",
900 (gpointer *) &theming_module->exit) ||
901 !g_module_symbol (module, "create_engine",
902 (gpointer *) &theming_module->create_engine))
904 g_module_close (module);
909 theming_module->module = module;
911 theming_module->init (G_TYPE_MODULE (theming_module));
917 gtk_theming_module_unload (GTypeModule *type_module)
919 GtkThemingModule *theming_module;
921 theming_module = GTK_THEMING_MODULE (type_module);
923 theming_module->exit ();
925 g_module_close (theming_module->module);
927 theming_module->module = NULL;
928 theming_module->init = NULL;
929 theming_module->exit = NULL;
930 theming_module->create_engine = NULL;
934 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
936 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
938 module_class->load = gtk_theming_module_load;
939 module_class->unload = gtk_theming_module_unload;
943 gtk_theming_module_init (GtkThemingModule *module)
948 * gtk_theming_engine_load:
949 * @name: Theme engine name to load
951 * Loads and initializes a theming engine module from the
952 * standard directories.
954 * Returns: (transfer none): A theming engine, or %NULL if
955 * the engine @name doesn't exist.
958 gtk_theming_engine_load (const gchar *name)
960 static GHashTable *engines = NULL;
961 static GtkThemingEngine *default_engine;
962 GtkThemingEngine *engine = NULL;
967 engines = g_hash_table_new (g_str_hash, g_str_equal);
969 engine = g_hash_table_lookup (engines, name);
973 GtkThemingModule *module;
975 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
976 g_type_module_set_name (G_TYPE_MODULE (module), name);
977 module->name = g_strdup (name);
979 if (module && g_type_module_use (G_TYPE_MODULE (module)))
981 engine = (module->create_engine) ();
984 g_hash_table_insert (engines, module->name, engine);
990 if (G_UNLIKELY (!default_engine))
991 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
993 engine = default_engine;
1000 * gtk_theming_engine_get_screen:
1001 * @engine: a #GtkThemingEngine
1003 * Returns the #GdkScreen to which @engine currently rendering to.
1005 * Returns: (transfer none): a #GdkScreen, or %NULL.
1008 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
1010 GtkThemingEnginePrivate *priv;
1012 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
1014 priv = engine->priv;
1015 return gtk_style_context_get_screen (priv->context);
1018 /* Paint method implementations */
1020 gtk_theming_engine_render_check (GtkThemingEngine *engine,
1027 GdkRGBA fg_color, bg_color;
1028 GtkStateFlags flags;
1029 gint exterior_size, interior_size, thickness, pad;
1030 GtkBorderStyle border_style;
1033 GtkThemingBackground bg;
1035 _gtk_theming_background_init (&bg, engine,
1038 gtk_theming_engine_get_junction_sides (engine));
1040 if (_gtk_theming_background_has_background_image (&bg))
1042 _gtk_theming_background_render (&bg, cr);
1046 flags = gtk_theming_engine_get_state (engine);
1049 gtk_theming_engine_get_color (engine, flags, &fg_color);
1050 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1051 gtk_theming_engine_get_border (engine, flags, &border);
1053 gtk_theming_engine_get (engine, flags,
1054 "border-style", &border_style,
1057 border_width = MIN (MIN (border.top, border.bottom),
1058 MIN (border.left, border.right));
1059 exterior_size = MIN (width, height);
1061 if (exterior_size % 2 == 0) /* Ensure odd */
1064 /* FIXME: thickness */
1066 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1067 interior_size = MAX (1, exterior_size - 2 * pad);
1069 if (interior_size < 7)
1072 pad = MAX (0, (exterior_size - interior_size) / 2);
1075 x -= (1 + exterior_size - (gint) width) / 2;
1076 y -= (1 + exterior_size - (gint) height) / 2;
1078 if (border_style == GTK_BORDER_STYLE_SOLID)
1080 GdkRGBA border_color;
1082 cairo_set_line_width (cr, border_width);
1083 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1085 cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
1086 gdk_cairo_set_source_rgba (cr, &bg_color);
1087 cairo_fill_preserve (cr);
1089 gdk_cairo_set_source_rgba (cr, &border_color);
1093 gdk_cairo_set_source_rgba (cr, &fg_color);
1095 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1097 int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1099 cairo_rectangle (cr,
1101 y + pad + (1 + interior_size - line_thickness) / 2,
1111 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1113 if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
1118 cairo_translate (cr,
1121 cairo_scale (cr, interior_size / 7., interior_size / 7.);
1123 cairo_rectangle (cr, 0, 0, 7 * progress, 7);
1126 cairo_move_to (cr, 7.0, 0.0);
1127 cairo_line_to (cr, 7.5, 1.0);
1128 cairo_curve_to (cr, 5.3, 2.0,
1131 cairo_curve_to (cr, 3.0, 5.7,
1134 cairo_line_to (cr, 0.2, 3.5);
1135 cairo_curve_to (cr, 1.1, 3.5,
1138 cairo_curve_to (cr, 1.0, 3.9,
1141 cairo_curve_to (cr, 3.5, 3.1,
1153 gtk_theming_engine_render_option (GtkThemingEngine *engine,
1160 GtkStateFlags flags;
1161 GdkRGBA fg_color, bg_color;
1162 gint exterior_size, interior_size, pad, thickness, border_width;
1163 GtkBorderStyle border_style;
1165 GtkThemingBackground bg;
1167 _gtk_theming_background_init (&bg, engine,
1170 gtk_theming_engine_get_junction_sides (engine));
1172 if (_gtk_theming_background_has_background_image (&bg))
1174 _gtk_theming_background_render (&bg, cr);
1178 flags = gtk_theming_engine_get_state (engine);
1182 gtk_theming_engine_get_color (engine, flags, &fg_color);
1183 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1184 gtk_theming_engine_get_border (engine, flags, &border);
1186 gtk_theming_engine_get (engine, flags,
1187 "border-style", &border_style,
1190 exterior_size = MIN (width, height);
1191 border_width = MIN (MIN (border.top, border.bottom),
1192 MIN (border.left, border.right));
1194 if (exterior_size % 2 == 0) /* Ensure odd */
1197 x -= (1 + exterior_size - width) / 2;
1198 y -= (1 + exterior_size - height) / 2;
1200 if (border_style == GTK_BORDER_STYLE_SOLID)
1202 GdkRGBA border_color;
1204 cairo_set_line_width (cr, border_width);
1205 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1207 cairo_new_sub_path (cr);
1209 x + exterior_size / 2.,
1210 y + exterior_size / 2.,
1211 (exterior_size - 1) / 2.,
1214 gdk_cairo_set_source_rgba (cr, &bg_color);
1215 cairo_fill_preserve (cr);
1217 gdk_cairo_set_source_rgba (cr, &border_color);
1221 gdk_cairo_set_source_rgba (cr, &fg_color);
1223 /* FIXME: thickness */
1226 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1228 gint line_thickness;
1230 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1231 interior_size = MAX (1, exterior_size - 2 * pad);
1233 if (interior_size < 7)
1236 pad = MAX (0, (exterior_size - interior_size) / 2);
1239 line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1241 cairo_rectangle (cr,
1243 y + pad + (interior_size - line_thickness) / 2.,
1248 if (flags & GTK_STATE_FLAG_ACTIVE)
1250 pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1251 interior_size = MAX (1, exterior_size - 2 * pad);
1253 if (interior_size < 5)
1256 pad = MAX (0, (exterior_size - interior_size) / 2);
1259 cairo_new_sub_path (cr);
1261 x + pad + interior_size / 2.,
1262 y + pad + interior_size / 2.,
1272 add_path_arrow (cairo_t *cr,
1280 cairo_translate (cr, x + (size / 2), y + (size / 2));
1281 cairo_rotate (cr, angle);
1283 cairo_move_to (cr, 0, - (size / 4));
1284 cairo_line_to (cr, - (size / 2), (size / 4));
1285 cairo_line_to (cr, (size / 2), (size / 4));
1286 cairo_close_path (cr);
1292 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1299 GtkStateFlags flags;
1304 flags = gtk_theming_engine_get_state (engine);
1305 gtk_theming_engine_get_color (engine, flags, &fg_color);
1307 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1309 add_path_arrow (cr, angle, x + 1, y + 1, size);
1310 cairo_set_source_rgb (cr, 1, 1, 1);
1314 add_path_arrow (cr, angle, x, y, size);
1315 gdk_cairo_set_source_rgba (cr, &fg_color);
1322 add_path_line (cairo_t *cr,
1328 /* Adjust endpoints */
1342 cairo_move_to (cr, x1, y1);
1343 cairo_line_to (cr, x2, y2);
1347 color_shade (const GdkRGBA *color,
1349 GdkRGBA *color_return)
1351 GtkSymbolicColor *literal, *shade;
1353 literal = gtk_symbolic_color_new_literal (color);
1354 shade = gtk_symbolic_color_new_shade (literal, factor);
1355 gtk_symbolic_color_unref (literal);
1357 gtk_symbolic_color_resolve (shade, NULL, color_return);
1358 gtk_symbolic_color_unref (shade);
1362 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1369 GtkThemingBackground bg;
1371 _gtk_theming_background_init (&bg, engine,
1374 gtk_theming_engine_get_junction_sides (engine));
1376 _gtk_theming_background_render (&bg, cr);
1380 gtk_theming_engine_hide_border_sides (GtkBorder *border,
1383 if (hidden_side & (1 << GTK_CSS_TOP))
1385 if (hidden_side & (1 << GTK_CSS_RIGHT))
1387 if (hidden_side & (1 << GTK_CSS_BOTTOM))
1389 if (hidden_side & (1 << GTK_CSS_LEFT))
1394 render_frame_fill (cairo_t *cr,
1395 GtkRoundedBox *border_box,
1400 GtkRoundedBox padding_box;
1403 padding_box = *border_box;
1404 _gtk_rounded_box_shrink (&padding_box, border->top, border->right, border->bottom, border->left);
1406 if (hidden_side == 0 &&
1407 gdk_rgba_equal (&colors[0], &colors[1]) &&
1408 gdk_rgba_equal (&colors[0], &colors[2]) &&
1409 gdk_rgba_equal (&colors[0], &colors[3]))
1411 gdk_cairo_set_source_rgba (cr, &colors[0]);
1413 _gtk_rounded_box_path (border_box, cr);
1414 _gtk_rounded_box_path (&padding_box, cr);
1419 for (i = 0; i < 4; i++)
1421 if (hidden_side & (1 << i))
1424 for (j = 0; j < 4; j++)
1426 if (hidden_side & (1 << j))
1430 (gdk_rgba_equal (&colors[i], &colors[j])))
1432 /* We were already painted when i == j */
1437 _gtk_rounded_box_path_top (border_box, &padding_box, cr);
1439 _gtk_rounded_box_path_right (border_box, &padding_box, cr);
1441 _gtk_rounded_box_path_bottom (border_box, &padding_box, cr);
1443 _gtk_rounded_box_path_left (border_box, &padding_box, cr);
1446 /* We were already painted when i == j */
1450 gdk_cairo_set_source_rgba (cr, &colors[i]);
1458 set_stroke_style (cairo_t *cr,
1460 GtkBorderStyle style,
1466 cairo_set_line_width (cr, line_width);
1468 if (style == GTK_BORDER_STYLE_DOTTED)
1470 n = round (0.5 * length / line_width);
1473 segments[1] = n ? length / n : 2;
1474 cairo_set_dash (cr, segments, G_N_ELEMENTS (segments), 0);
1476 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
1477 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
1481 n = length / line_width;
1482 /* Optimize the common case of an integer-sized rectangle
1483 * Again, we care about focus rectangles.
1485 if (n == nearbyint (n))
1492 n = round ((1. / 3) * n);
1494 segments[0] = n ? (1. / 3) * length / n : 1;
1495 segments[1] = 2 * segments[1];
1497 cairo_set_dash (cr, segments, G_N_ELEMENTS (segments), 0);
1499 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1500 cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
1505 get_border_side (GtkBorder *border,
1513 return border->right;
1514 case GTK_CSS_BOTTOM:
1515 return border->bottom;
1517 return border->left;
1519 g_assert_not_reached ();
1525 render_frame_stroke (cairo_t *cr,
1526 GtkRoundedBox *border_box,
1530 GtkBorderStyle stroke_style)
1532 gboolean different_colors, different_borders;
1533 GtkRoundedBox stroke_box;
1536 different_colors = !gdk_rgba_equal (&colors[0], &colors[1]) ||
1537 !gdk_rgba_equal (&colors[0], &colors[2]) ||
1538 !gdk_rgba_equal (&colors[0], &colors[3]);
1539 different_borders = border->top != border->right ||
1540 border->top != border->bottom ||
1541 border->top != border->left;
1543 stroke_box = *border_box;
1544 _gtk_rounded_box_shrink (&stroke_box,
1546 border->right / 2.0,
1547 border->bottom / 2.0,
1548 border->left / 2.0);
1550 if (!different_colors && !different_borders && hidden_side == 0)
1555 * Mostly expected to trigger for focus rectangles */
1556 for (i = 0; i < 4; i++)
1558 length += _gtk_rounded_box_guess_length (&stroke_box, i);
1559 _gtk_rounded_box_path_side (&stroke_box, cr, i);
1562 gdk_cairo_set_source_rgba (cr, &colors[0]);
1563 set_stroke_style (cr, border->top, stroke_style, length);
1568 GtkRoundedBox padding_box;
1570 padding_box = *border_box;
1571 _gtk_rounded_box_path (&padding_box, cr);
1572 _gtk_rounded_box_shrink (&padding_box,
1578 for (i = 0; i < 4; i++)
1580 if (hidden_side & (1 << i))
1586 _gtk_rounded_box_path_top (border_box, &padding_box, cr);
1588 _gtk_rounded_box_path_right (border_box, &padding_box, cr);
1590 _gtk_rounded_box_path_bottom (border_box, &padding_box, cr);
1592 _gtk_rounded_box_path_left (border_box, &padding_box, cr);
1595 _gtk_rounded_box_path_side (&stroke_box, cr, i);
1597 gdk_cairo_set_source_rgba (cr, &colors[i]);
1598 set_stroke_style (cr,
1599 get_border_side (border, i),
1601 _gtk_rounded_box_guess_length (&stroke_box, i));
1610 render_border (cairo_t *cr,
1611 GtkRoundedBox *border_box,
1615 GtkBorderStyle border_style[4])
1621 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
1623 for (i = 0; i < 4; i++)
1625 if (hidden_side & (1 << i))
1628 switch (border_style[i])
1630 case GTK_BORDER_STYLE_NONE:
1631 case GTK_BORDER_STYLE_HIDDEN:
1632 case GTK_BORDER_STYLE_SOLID:
1634 case GTK_BORDER_STYLE_INSET:
1635 if (i == 1 || i == 2)
1636 color_shade (&colors[i], 1.8, &colors[i]);
1638 case GTK_BORDER_STYLE_OUTSET:
1639 if (i == 0 || i == 3)
1640 color_shade (&colors[i], 1.8, &colors[i]);
1642 case GTK_BORDER_STYLE_DOTTED:
1643 case GTK_BORDER_STYLE_DASHED:
1645 guint dont_draw = hidden_side;
1647 for (j = 0; j < 4; j++)
1649 if (border_style[j] == border_style[i])
1650 hidden_side |= (1 << j);
1652 dont_draw |= (1 << j);
1655 render_frame_stroke (cr, border_box, border, colors, dont_draw, border_style[i]);
1658 case GTK_BORDER_STYLE_DOUBLE:
1660 GtkRoundedBox other_box;
1661 GtkBorder other_border;
1662 guint dont_draw = hidden_side;
1664 for (j = 0; j < 4; j++)
1666 if (border_style[j] == GTK_BORDER_STYLE_DOUBLE)
1667 hidden_side |= (1 << j);
1669 dont_draw |= (1 << j);
1671 other_border.top = (border->top + 2) / 3;
1672 other_border.right = (border->right + 2) / 3;
1673 other_border.bottom = (border->bottom + 2) / 3;
1674 other_border.left = (border->left + 2) / 3;
1676 render_frame_fill (cr, border_box, &other_border, colors, dont_draw);
1678 other_box = *border_box;
1679 _gtk_rounded_box_shrink (&other_box,
1680 border->top - other_border.top,
1681 border->right - other_border.right,
1682 border->bottom - other_border.bottom,
1683 border->left - other_border.left);
1684 render_frame_fill (cr, &other_box, &other_border, colors, dont_draw);
1686 case GTK_BORDER_STYLE_GROOVE:
1687 case GTK_BORDER_STYLE_RIDGE:
1689 GtkRoundedBox other_box;
1690 GdkRGBA other_colors[4];
1691 guint dont_draw = hidden_side;
1692 GtkBorder other_border;
1694 for (j = 0; j < 4; j++)
1696 other_colors[j] = colors[j];
1697 if ((j == 0 || j == 3) ^ (border_style[j] == GTK_BORDER_STYLE_RIDGE))
1698 color_shade (&other_colors[j], 1.8, &other_colors[j]);
1700 color_shade (&colors[j], 1.8, &colors[j]);
1701 if (border_style[j] == GTK_BORDER_STYLE_GROOVE ||
1702 border_style[j] == GTK_BORDER_STYLE_RIDGE)
1703 hidden_side |= (1 << j);
1705 dont_draw |= (1 << j);
1707 other_border.top = border->top / 2;
1708 other_border.right = border->right / 2;
1709 other_border.bottom = border->bottom / 2;
1710 other_border.left = border->left / 2;
1712 render_frame_fill (cr, border_box, &other_border, colors, dont_draw);
1714 other_box = *border_box;
1715 _gtk_rounded_box_shrink (&other_box,
1716 other_border.top, other_border.right,
1717 other_border.bottom, other_border.left);
1718 other_border.top = border->top - other_border.top;
1719 other_border.right = border->right - other_border.right;
1720 other_border.bottom = border->bottom - other_border.bottom;
1721 other_border.left = border->left - other_border.left;
1722 render_frame_fill (cr, &other_box, &other_border, other_colors, dont_draw);
1726 g_assert_not_reached ();
1731 render_frame_fill (cr, border_box, border, colors, hidden_side);
1737 render_frame_internal (GtkThemingEngine *engine,
1744 GtkJunctionSides junction)
1746 GtkBorderImage border_image;
1747 GtkStateFlags state;
1748 GtkBorderStyle border_style[4];
1749 GtkRoundedBox border_box;
1753 GdkRGBA *alloc_colors[4];
1757 state = gtk_theming_engine_get_state (engine);
1759 gtk_theming_engine_get_border (engine, state, &border);
1760 gtk_theming_engine_hide_border_sides (&border, hidden_side);
1762 if (_gtk_border_image_init (&border_image, engine))
1763 _gtk_border_image_render (&border_image, &border, cr, x, y, width, height);
1766 gtk_theming_engine_get (engine, state,
1767 "border-top-style", &border_style[0],
1768 "border-right-style", &border_style[1],
1769 "border-bottom-style", &border_style[2],
1770 "border-left-style", &border_style[3],
1771 "border-top-color", &alloc_colors[0],
1772 "border-right-color", &alloc_colors[1],
1773 "border-bottom-color", &alloc_colors[2],
1774 "border-left-color", &alloc_colors[3],
1777 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1781 GtkStateFlags other_state;
1782 GdkRGBA *other_colors[4];
1784 if (state & GTK_STATE_FLAG_PRELIGHT)
1786 other_state = state & ~(GTK_STATE_FLAG_PRELIGHT);
1787 progress = 1 - progress;
1790 other_state = state | GTK_STATE_FLAG_PRELIGHT;
1792 gtk_theming_engine_get (engine, other_state,
1793 "border-top-color", &other_colors[0],
1794 "border-right-color", &other_colors[1],
1795 "border-bottom-color", &other_colors[2],
1796 "border-left-color", &other_colors[3],
1799 for (i = 0; i < 4; i++)
1801 colors[i].red = CLAMP (alloc_colors[i]->red + ((other_colors[i]->red - alloc_colors[i]->red) * progress), 0, 1);
1802 colors[i].green = CLAMP (alloc_colors[i]->green + ((other_colors[i]->green - alloc_colors[i]->green) * progress), 0, 1);
1803 colors[i].blue = CLAMP (alloc_colors[i]->blue + ((other_colors[i]->blue - alloc_colors[i]->blue) * progress), 0, 1);
1804 colors[i].alpha = CLAMP (alloc_colors[i]->alpha + ((other_colors[i]->alpha - alloc_colors[i]->alpha) * progress), 0, 1);
1805 gdk_rgba_free (other_colors[i]);
1806 gdk_rgba_free (alloc_colors[i]);
1811 for (i = 0; i < 4; i++)
1813 colors[i] = *alloc_colors[i];
1814 gdk_rgba_free (alloc_colors[i]);
1818 _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1819 _gtk_rounded_box_apply_border_radius_for_engine (&border_box, engine, state, junction);
1821 render_border (cr, &border_box, &border, hidden_side, colors, border_style);
1824 border_style[0] = _gtk_css_border_style_value_get (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_OUTLINE_STYLE));
1825 if (border_style[0] != GTK_BORDER_STYLE_NONE)
1829 border_style[1] = border_style[2] = border_style[3] = border_style[0];
1830 border.top = _gtk_css_value_get_int (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_OUTLINE_WIDTH));
1831 border.left = border.right = border.bottom = border.top;
1832 colors[0] = *_gtk_css_rgba_value_get_rgba (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_OUTLINE_COLOR));
1833 colors[3] = colors[2] = colors[1] = colors[0];
1834 offset = _gtk_css_value_get_int (_gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_OUTLINE_OFFSET));
1836 /* reinit box here - outlines don't have a border radius */
1837 _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1838 _gtk_rounded_box_shrink (&border_box,
1839 - border.top - offset,
1840 - border.right - offset,
1841 - border.left - offset,
1842 - border.bottom - offset);
1844 render_border (cr, &border_box, &border, hidden_side, colors, border_style);
1849 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1856 GtkJunctionSides junction;
1858 junction = gtk_theming_engine_get_junction_sides (engine);
1860 render_frame_internal (engine, cr,
1861 x, y, width, height,
1866 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1873 GtkStateFlags flags;
1874 GdkRGBA outline_color, fg_color;
1875 double vertical_overshoot;
1878 double interp; /* interpolation factor for center position */
1879 double x_double_horz, y_double_horz;
1880 double x_double_vert, y_double_vert;
1881 double x_double, y_double;
1884 gboolean running, is_rtl;
1888 flags = gtk_theming_engine_get_state (engine);
1890 gtk_theming_engine_get_color (engine, flags, &fg_color);
1891 gtk_theming_engine_get_border_color (engine, flags, &outline_color);
1893 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1894 is_rtl = (gtk_theming_engine_get_direction (engine) == GTK_TEXT_DIR_RTL);
1898 progress = (flags & GTK_STATE_FLAG_ACTIVE) ? 1 : 0;
1900 if (!gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_HORIZONTAL))
1903 angle = (G_PI) - ((G_PI / 2) * progress);
1905 angle = (G_PI / 2) * progress;
1910 angle = (G_PI / 2) + ((G_PI / 2) * progress);
1912 angle = (G_PI / 2) - ((G_PI / 2) * progress);
1917 /* Compute distance that the stroke extends beyonds the end
1918 * of the triangle we draw.
1920 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1922 /* For odd line widths, we end the vertical line of the triangle
1923 * at a half pixel, so we round differently.
1925 if (line_width % 2 == 1)
1926 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1928 vertical_overshoot = ceil (vertical_overshoot);
1930 /* Adjust the size of the triangle we draw so that the entire stroke fits
1932 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1934 /* If the line width is odd, we want the diameter to be even,
1935 * and vice versa, so force the sum to be odd. This relationship
1936 * makes the point of the triangle look right.
1938 diameter -= (1 - (diameter + line_width) % 2);
1940 radius = diameter / 2.;
1942 /* Adjust the center so that the stroke is properly aligned with
1943 * the pixel grid. The center adjustment is different for the
1944 * horizontal and vertical orientations. For intermediate positions
1945 * we interpolate between the two.
1947 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1948 y_double_vert = (y + height / 2) - 0.5;
1950 x_double_horz = (x + width / 2) - 0.5;
1951 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1953 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1954 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1956 cairo_translate (cr, x_double, y_double);
1957 cairo_rotate (cr, angle);
1959 cairo_move_to (cr, - radius / 2., - radius);
1960 cairo_line_to (cr, radius / 2., 0);
1961 cairo_line_to (cr, - radius / 2., radius);
1962 cairo_close_path (cr);
1964 cairo_set_line_width (cr, line_width);
1966 gdk_cairo_set_source_rgba (cr, &fg_color);
1968 cairo_fill_preserve (cr);
1970 gdk_cairo_set_source_rgba (cr, &outline_color);
1977 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1984 GtkStateFlags flags;
1990 flags = gtk_theming_engine_get_state (engine);
1992 gtk_theming_engine_get_color (engine, flags, &color);
1994 gtk_theming_engine_get_style (engine,
1995 "focus-line-width", &line_width,
1996 "focus-line-pattern", (gchar *) &dash_list,
1999 cairo_set_line_width (cr, (gdouble) line_width);
2003 gint n_dashes = strlen ((const gchar *) dash_list);
2004 gdouble *dashes = g_new (gdouble, n_dashes);
2005 gdouble total_length = 0;
2006 gdouble dash_offset;
2009 for (i = 0; i < n_dashes; i++)
2011 dashes[i] = dash_list[i];
2012 total_length += dash_list[i];
2015 /* The dash offset here aligns the pattern to integer pixels
2016 * by starting the dash at the right side of the left border
2017 * Negative dash offsets in cairo don't work
2018 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
2020 dash_offset = - line_width / 2.;
2022 while (dash_offset < 0)
2023 dash_offset += total_length;
2025 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
2029 cairo_rectangle (cr,
2030 x + line_width / 2.,
2031 y + line_width / 2.,
2033 height - line_width);
2035 gdk_cairo_set_source_rgba (cr, &color);
2044 gtk_theming_engine_render_line (GtkThemingEngine *engine,
2052 GtkStateFlags flags;
2054 flags = gtk_theming_engine_get_state (engine);
2057 gtk_theming_engine_get_color (engine, flags, &color);
2059 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
2060 cairo_set_line_width (cr, 1);
2062 cairo_move_to (cr, x0 + 0.5, y0 + 0.5);
2063 cairo_line_to (cr, x1 + 0.5, y1 + 0.5);
2065 gdk_cairo_set_source_rgba (cr, &color);
2072 prepare_context_for_layout (cairo_t *cr,
2075 PangoLayout *layout)
2077 const PangoMatrix *matrix;
2079 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2081 cairo_move_to (cr, x, y);
2085 cairo_matrix_t cairo_matrix;
2087 cairo_matrix_init (&cairo_matrix,
2088 matrix->xx, matrix->yx,
2089 matrix->xy, matrix->yy,
2090 matrix->x0, matrix->y0);
2092 cairo_transform (cr, &cairo_matrix);
2097 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2101 PangoLayout *layout)
2104 GtkCssValue *shadows;
2105 GtkStateFlags flags;
2111 flags = gtk_theming_engine_get_state (engine);
2112 gtk_theming_engine_get_color (engine, flags, &fg_color);
2114 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
2118 GtkStateFlags other_flags;
2121 if (flags & GTK_STATE_FLAG_PRELIGHT)
2123 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
2124 progress = 1 - progress;
2127 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
2129 gtk_theming_engine_get_color (engine, other_flags, &other_fg);
2131 fg_color.red = CLAMP (fg_color.red + ((other_fg.red - fg_color.red) * progress), 0, 1);
2132 fg_color.green = CLAMP (fg_color.green + ((other_fg.green - fg_color.green) * progress), 0, 1);
2133 fg_color.blue = CLAMP (fg_color.blue + ((other_fg.blue - fg_color.blue) * progress), 0, 1);
2134 fg_color.alpha = CLAMP (fg_color.alpha + ((other_fg.alpha - fg_color.alpha) * progress), 0, 1);
2137 shadows = _gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_TEXT_SHADOW);
2139 prepare_context_for_layout (cr, x, y, layout);
2141 for (i = 0; i < _gtk_css_array_value_get_n_values (shadows); i++)
2143 _gtk_css_shadow_value_paint_layout (_gtk_css_array_value_get_nth (shadows, i), cr, layout);
2146 gdk_cairo_set_source_rgba (cr, &fg_color);
2147 pango_cairo_show_layout (cr, layout);
2153 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2159 GtkOrientation orientation)
2161 const GtkWidgetPath *path;
2164 path = gtk_theming_engine_get_path (engine);
2166 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2167 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2169 /* FIXME: thickness */
2172 if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2174 if (orientation == GTK_ORIENTATION_VERTICAL)
2175 gtk_theming_engine_render_line (engine, cr,
2177 y + (gint) height / 2,
2178 x + width - thickness - 1,
2179 y + (gint) height / 2);
2181 gtk_theming_engine_render_line (engine, cr,
2182 x + (gint) width / 2,
2184 x + (gint) width / 2,
2185 y + height - thickness - 1);
2190 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2196 GtkPositionType gap_side,
2200 GtkJunctionSides junction;
2201 GtkStateFlags state;
2203 GtkCssBorderCornerRadius *top_left_radius, *top_right_radius;
2204 GtkCssBorderCornerRadius *bottom_left_radius, *bottom_right_radius;
2205 gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2208 xc = yc = wc = hc = 0;
2209 state = gtk_theming_engine_get_state (engine);
2210 junction = gtk_theming_engine_get_junction_sides (engine);
2212 gtk_theming_engine_get_border (engine, state, &border);
2213 gtk_theming_engine_get (engine, state,
2214 "border-top-left-radius", &top_left_radius,
2215 "border-top-right-radius", &top_right_radius,
2216 "border-bottom-right-radius", &bottom_right_radius,
2217 "border-bottom-left-radius", &bottom_left_radius,
2220 border_width = MIN (MIN (border.top, border.bottom),
2221 MIN (border.left, border.right));
2228 xc = x + xy0_gap + border_width;
2230 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2233 if (xy0_gap < _gtk_css_number_get (&top_left_radius->horizontal, width))
2234 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2236 if (xy1_gap > width - _gtk_css_number_get (&top_right_radius->horizontal, width))
2237 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2239 case GTK_POS_BOTTOM:
2240 xc = x + xy0_gap + border_width;
2241 yc = y + height - border_width;
2242 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2245 if (xy0_gap < _gtk_css_number_get (&bottom_left_radius->horizontal, width))
2246 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2248 if (xy1_gap > width - _gtk_css_number_get (&bottom_right_radius->horizontal, width))
2249 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2254 yc = y + xy0_gap + border_width;
2256 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2258 if (xy0_gap < _gtk_css_number_get (&top_left_radius->vertical, height))
2259 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2261 if (xy1_gap > height - _gtk_css_number_get (&bottom_left_radius->vertical, height))
2262 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2266 xc = x + width - border_width;
2267 yc = y + xy0_gap + border_width;
2269 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2271 if (xy0_gap < _gtk_css_number_get (&top_right_radius->vertical, height))
2272 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2274 if (xy1_gap > height - _gtk_css_number_get (&bottom_right_radius->vertical, height))
2275 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2280 cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2281 cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2282 cairo_rectangle (cr, x0, yc, xc - x0, hc);
2283 cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2284 cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2287 render_frame_internal (engine, cr,
2288 x, y, width, height,
2293 g_free (top_left_radius);
2294 g_free (top_right_radius);
2295 g_free (bottom_right_radius);
2296 g_free (bottom_left_radius);
2300 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2306 GtkPositionType gap_side)
2308 GtkThemingBackground bg;
2309 GtkJunctionSides junction = 0;
2310 guint hidden_side = 0;
2317 junction = GTK_JUNCTION_LEFT;
2318 hidden_side = (1 << GTK_CSS_LEFT);
2320 cairo_translate (cr, x + width, y);
2321 cairo_rotate (cr, G_PI / 2);
2324 junction = GTK_JUNCTION_RIGHT;
2325 hidden_side = (1 << GTK_CSS_RIGHT);
2327 cairo_translate (cr, x, y + height);
2328 cairo_rotate (cr, - G_PI / 2);
2331 junction = GTK_JUNCTION_TOP;
2332 hidden_side = (1 << GTK_CSS_TOP);
2334 cairo_translate (cr, x + width, y + height);
2335 cairo_rotate (cr, G_PI);
2337 case GTK_POS_BOTTOM:
2338 junction = GTK_JUNCTION_BOTTOM;
2339 hidden_side = (1 << GTK_CSS_BOTTOM);
2341 cairo_translate (cr, x, y);
2345 if (gap_side == GTK_POS_TOP ||
2346 gap_side == GTK_POS_BOTTOM)
2347 _gtk_theming_background_init (&bg, engine,
2350 GTK_JUNCTION_BOTTOM);
2352 _gtk_theming_background_init (&bg, engine,
2355 GTK_JUNCTION_BOTTOM);
2357 _gtk_theming_background_render (&bg, cr);
2363 render_frame_internal (engine, cr,
2364 x, y, width, height,
2365 hidden_side, junction);
2371 render_dot (cairo_t *cr,
2372 const GdkRGBA *lighter,
2373 const GdkRGBA *darker,
2378 size = CLAMP ((gint) size, 2, 3);
2382 gdk_cairo_set_source_rgba (cr, lighter);
2383 cairo_rectangle (cr, x, y, 1, 1);
2384 cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2389 gdk_cairo_set_source_rgba (cr, lighter);
2390 cairo_rectangle (cr, x, y, 2, 1);
2391 cairo_rectangle (cr, x, y, 1, 2);
2394 gdk_cairo_set_source_rgba (cr, darker);
2395 cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2396 cairo_rectangle (cr, x + 2, y, 1, 2);
2402 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2409 GtkStateFlags flags;
2410 GdkRGBA bg_color, lighter, darker;
2411 GtkJunctionSides sides;
2412 GtkThemingBackground bg;
2416 flags = gtk_theming_engine_get_state (engine);
2418 cairo_set_line_width (cr, 1.0);
2419 sides = gtk_theming_engine_get_junction_sides (engine);
2420 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2422 color_shade (&bg_color, 0.7, &darker);
2423 color_shade (&bg_color, 1.3, &lighter);
2425 _gtk_theming_background_init (&bg, engine, x, y, width, height, sides);
2426 _gtk_theming_background_render (&bg, cr);
2428 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_GRIP))
2430 /* reduce confusing values to a meaningful state */
2431 if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2432 sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2434 if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2435 sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2438 sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2440 /* align drawing area to the connected side */
2441 if (sides == GTK_JUNCTION_LEFT)
2446 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2450 else if (height < width)
2453 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2455 /* make it square, aligning to bottom left */
2458 y += (height - width);
2461 else if (height < width)
2464 else if (sides == GTK_JUNCTION_RIGHT)
2466 /* aligning to right */
2469 x += (width - height);
2473 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2477 else if (height < width)
2479 x += (width - height);
2483 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2485 /* make it square, aligning to bottom right */
2488 y += (height - width);
2491 else if (height < width)
2493 x += (width - height);
2497 else if (sides == GTK_JUNCTION_TOP)
2502 else if (sides == GTK_JUNCTION_BOTTOM)
2504 /* align to bottom */
2507 y += (height - width);
2512 g_assert_not_reached ();
2514 if (sides == GTK_JUNCTION_LEFT ||
2515 sides == GTK_JUNCTION_RIGHT)
2521 while (xi < x + width)
2523 gdk_cairo_set_source_rgba (cr, &lighter);
2524 add_path_line (cr, x, y, x, y + height);
2528 gdk_cairo_set_source_rgba (cr, &darker);
2529 add_path_line (cr, xi, y, xi, y + height);
2534 else if (sides == GTK_JUNCTION_TOP ||
2535 sides == GTK_JUNCTION_BOTTOM)
2541 while (yi < y + height)
2543 gdk_cairo_set_source_rgba (cr, &lighter);
2544 add_path_line (cr, x, yi, x + width, yi);
2548 gdk_cairo_set_source_rgba (cr, &darker);
2549 add_path_line (cr, x, yi, x + width, yi);
2554 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2563 gdk_cairo_set_source_rgba (cr, &darker);
2564 add_path_line (cr, xi, y, x, yi);
2570 add_path_line (cr, xi, y, x, yi);
2576 gdk_cairo_set_source_rgba (cr, &lighter);
2577 add_path_line (cr, xi, y, x, yi);
2584 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2591 while (xi < (x + width - 3))
2593 gdk_cairo_set_source_rgba (cr, &lighter);
2594 add_path_line (cr, xi, y, x + width, yi);
2600 gdk_cairo_set_source_rgba (cr, &darker);
2601 add_path_line (cr, xi, y, x + width, yi);
2607 add_path_line (cr, xi, y, x + width, yi);
2614 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2623 gdk_cairo_set_source_rgba (cr, &darker);
2624 add_path_line (cr, x, yi, xi, y + height);
2630 add_path_line (cr, x, yi, xi, y + height);
2636 gdk_cairo_set_source_rgba (cr, &lighter);
2637 add_path_line (cr, x, yi, xi, y + height);
2644 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2651 while (xi < (x + width - 3))
2653 gdk_cairo_set_source_rgba (cr, &lighter);
2654 add_path_line (cr, xi, y + height, x + width, yi);
2660 gdk_cairo_set_source_rgba (cr, &darker);
2661 add_path_line (cr, xi, y + height, x + width, yi);
2667 add_path_line (cr, xi, y + height, x + width, yi);
2675 else if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_PANE_SEPARATOR))
2678 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2679 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2681 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2682 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2686 for (yy = y; yy < y + height; yy += 3)
2687 for (xx = x; xx < x + width; xx += 6)
2689 render_dot (cr, &lighter, &darker, xx, yy, 2);
2690 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2698 _gtk_theming_engine_paint_spinner (cairo_t *cr,
2701 const GdkRGBA *color)
2703 guint num_steps, step;
2710 step = (guint) (progress * num_steps);
2716 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2717 cairo_set_line_width (cr, 2.0);
2719 half = num_steps / 2;
2721 for (i = 0; i < num_steps; i++)
2723 gint inset = 0.7 * radius;
2725 /* transparency is a function of time and intial value */
2726 gdouble t = 1.0 - (gdouble) ((i + step) % num_steps) / num_steps;
2727 gdouble xscale = - sin (i * G_PI / half);
2728 gdouble yscale = - cos (i * G_PI / half);
2730 cairo_set_source_rgba (cr,
2737 (radius - inset) * xscale,
2738 (radius - inset) * yscale);
2750 render_spinner (GtkThemingEngine *engine,
2757 GtkStateFlags state;
2758 GtkCssValue *shadows;
2764 state = gtk_theming_engine_get_state (engine);
2766 if (!gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2769 radius = MIN (width / 2, height / 2);
2771 gtk_theming_engine_get_color (engine, state, &color);
2772 shadows = _gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_ICON_SHADOW);
2775 cairo_translate (cr, x + width / 2, y + height / 2);
2777 for (i = 0; i < _gtk_css_array_value_get_n_values (shadows); i++)
2779 _gtk_css_shadow_value_paint_spinner (_gtk_css_array_value_get_nth (shadows, i),
2785 _gtk_theming_engine_paint_spinner (cr,
2794 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2801 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_SPINNER))
2803 render_spinner (engine, cr, x, y, width, height);
2807 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2808 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2813 scale_or_ref (GdkPixbuf *src,
2817 if (width == gdk_pixbuf_get_width (src) &&
2818 height == gdk_pixbuf_get_height (src))
2819 return g_object_ref (src);
2821 return gdk_pixbuf_scale_simple (src,
2823 GDK_INTERP_BILINEAR);
2827 lookup_icon_size (GtkThemingEngine *engine,
2833 GtkSettings *settings;
2835 screen = gtk_theming_engine_get_screen (engine);
2836 settings = gtk_settings_get_for_screen (screen);
2838 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2842 colorshift_source (cairo_t *cr,
2845 cairo_pattern_t *source;
2850 source = cairo_pattern_reference (cairo_get_source (cr));
2852 cairo_set_source_rgb (cr, shift, shift, shift);
2853 cairo_set_operator (cr, CAIRO_OPERATOR_COLOR_DODGE);
2855 cairo_mask (cr, source);
2857 cairo_pattern_destroy (source);
2862 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2863 const GtkIconSource *source,
2868 GdkPixbuf *base_pixbuf;
2869 GtkStateFlags state;
2873 cairo_surface_t *surface;
2875 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2876 state = gtk_theming_engine_get_state (engine);
2878 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2880 if (size != (GtkIconSize) -1 &&
2881 !lookup_icon_size (engine, size, &width, &height))
2883 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2887 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2890 if (size != (GtkIconSize) -1 &&
2891 gtk_icon_source_get_size_wildcarded (source))
2892 scaled = scale_or_ref (base_pixbuf, width, height);
2894 scaled = g_object_ref (base_pixbuf);
2896 /* If the state was wildcarded, then generate a state. */
2897 if (gtk_icon_source_get_state_wildcarded (source))
2899 if (state & GTK_STATE_FLAG_INSENSITIVE)
2901 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2902 gdk_pixbuf_get_width (scaled),
2903 gdk_pixbuf_get_height (scaled));
2904 cr = cairo_create (surface);
2905 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2906 cairo_paint_with_alpha (cr, 0.5);
2910 g_object_unref (scaled);
2911 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2912 cairo_image_surface_get_width (surface),
2913 cairo_image_surface_get_height (surface));
2914 cairo_surface_destroy (surface);
2916 else if (state & GTK_STATE_FLAG_PRELIGHT)
2918 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2919 gdk_pixbuf_get_width (scaled),
2920 gdk_pixbuf_get_height (scaled));
2922 cr = cairo_create (surface);
2923 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2924 colorshift_source (cr, 0.10);
2928 g_object_unref (scaled);
2929 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2930 cairo_image_surface_get_width (surface),
2931 cairo_image_surface_get_height (surface));
2932 cairo_surface_destroy (surface);
2944 gtk_theming_engine_render_icon (GtkThemingEngine *engine,
2950 GtkCssValue *shadows;
2955 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2957 shadows = _gtk_theming_engine_peek_property (engine, GTK_CSS_PROPERTY_ICON_SHADOW);
2958 for (i = 0; i < _gtk_css_array_value_get_n_values (shadows); i++)
2960 _gtk_css_shadow_value_paint_icon (_gtk_css_array_value_get_nth (shadows, i), cr);