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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include <gtk/gtkthemingengine.h>
26 #include <gtk/gtkstylecontext.h>
27 #include <gtk/gtkintl.h>
29 #include "gtkprivate.h"
30 #include "gtkborderimageprivate.h"
32 #include "gtkshadowprivate.h"
33 #include "gtkcsstypesprivate.h"
34 #include "gtkthemingengineprivate.h"
35 #include "gtkroundedboxprivate.h"
38 * SECTION:gtkthemingengine
39 * @Short_description: Theming renderers
40 * @Title: GtkThemingEngine
41 * @See_also: #GtkStyleContext
43 * #GtkThemingEngine is the object used for rendering themed content
44 * in GTK+ widgets. Even though GTK+ has a default implementation,
45 * it can be overridden in CSS files by enforcing a #GtkThemingEngine
46 * object to be loaded as a module.
48 * In order to implement a theming engine, a #GtkThemingEngine subclass
49 * must be created, alongside the CSS file that will reference it, the
50 * theming engine would be created as an .so library, and installed in
51 * $(gtk-modules-dir)/theming-engines/.
53 * #GtkThemingEngine<!-- -->s have limited access to the object they are
54 * rendering, the #GtkThemingEngine API has read-only accessors to the
55 * style information contained in the rendered object's #GtkStyleContext.
71 struct GtkThemingEnginePrivate
73 GtkStyleContext *context;
77 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
79 static void gtk_theming_engine_finalize (GObject *object);
80 static void gtk_theming_engine_impl_set_property (GObject *object,
84 static void gtk_theming_engine_impl_get_property (GObject *object,
89 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
95 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
101 static void gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
107 static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
113 static void gtk_theming_engine_render_frame (GtkThemingEngine *engine,
119 static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
125 static void gtk_theming_engine_render_focus (GtkThemingEngine *engine,
131 static void gtk_theming_engine_render_layout (GtkThemingEngine *engine,
135 PangoLayout *layout);
136 static void gtk_theming_engine_render_line (GtkThemingEngine *engine,
142 static void gtk_theming_engine_render_slider (GtkThemingEngine *engine,
148 GtkOrientation orientation);
149 static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
155 GtkPositionType gap_side,
158 static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
164 GtkPositionType gap_side);
165 static void gtk_theming_engine_render_handle (GtkThemingEngine *engine,
171 static void gtk_theming_engine_render_activity (GtkThemingEngine *engine,
177 static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
178 const GtkIconSource *source,
180 static void gtk_theming_engine_render_icon (GtkThemingEngine *engine,
186 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
189 typedef struct GtkThemingModule GtkThemingModule;
190 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
192 struct GtkThemingModule
194 GTypeModule parent_instance;
198 void (*init) (GTypeModule *module);
200 GtkThemingEngine * (*create_engine) (void);
203 struct GtkThemingModuleClass
205 GTypeModuleClass parent_class;
208 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
209 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
210 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
212 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
215 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
217 GObjectClass *object_class = G_OBJECT_CLASS (klass);
219 object_class->finalize = gtk_theming_engine_finalize;
220 object_class->set_property = gtk_theming_engine_impl_set_property;
221 object_class->get_property = gtk_theming_engine_impl_get_property;
223 klass->render_icon = gtk_theming_engine_render_icon;
224 klass->render_check = gtk_theming_engine_render_check;
225 klass->render_option = gtk_theming_engine_render_option;
226 klass->render_arrow = gtk_theming_engine_render_arrow;
227 klass->render_background = gtk_theming_engine_render_background;
228 klass->render_frame = gtk_theming_engine_render_frame;
229 klass->render_expander = gtk_theming_engine_render_expander;
230 klass->render_focus = gtk_theming_engine_render_focus;
231 klass->render_layout = gtk_theming_engine_render_layout;
232 klass->render_line = gtk_theming_engine_render_line;
233 klass->render_slider = gtk_theming_engine_render_slider;
234 klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
235 klass->render_extension = gtk_theming_engine_render_extension;
236 klass->render_handle = gtk_theming_engine_render_handle;
237 klass->render_activity = gtk_theming_engine_render_activity;
238 klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
241 * GtkThemingEngine:name:
243 * The theming engine name, this name will be used when registering
244 * custom properties, for a theming engine named "Clearlooks" registering
245 * a "glossy" custom property, it could be referenced in the CSS file as
248 * -Clearlooks-glossy: true;
253 g_object_class_install_property (object_class,
255 g_param_spec_string ("name",
257 P_("Theming engine name"),
259 G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));
261 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
265 gtk_theming_engine_init (GtkThemingEngine *engine)
267 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
271 gtk_theming_engine_finalize (GObject *object)
273 GtkThemingEnginePrivate *priv;
275 priv = GTK_THEMING_ENGINE (object)->priv;
278 G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
282 gtk_theming_engine_impl_set_property (GObject *object,
287 GtkThemingEnginePrivate *priv;
289 priv = GTK_THEMING_ENGINE (object)->priv;
297 priv->name = g_value_dup_string (value);
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 gtk_theming_engine_impl_get_property (GObject *object,
311 GtkThemingEnginePrivate *priv;
313 priv = GTK_THEMING_ENGINE (object)->priv;
318 g_value_set_string (value, priv->name);
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
328 GtkStyleContext *context)
330 GtkThemingEnginePrivate *priv;
332 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
333 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
336 priv->context = context;
340 * gtk_theming_engine_register_property: (skip)
341 * @name_space: namespace for the property name
342 * @parse_func: parsing function to use, or %NULL
343 * @pspec: the #GParamSpec for the new property
345 * Registers a property so it can be used in the CSS file format,
346 * on the CSS file the property will look like
347 * "-${@name_space}-${property_name}". being
348 * ${property_name} the given to @pspec. @name_space will usually
349 * be the theme engine name.
351 * For any type a @parse_func may be provided, being this function
352 * used for turning any property value (between ':' and ';') in
353 * CSS to the #GValue needed. For basic types there is already
354 * builtin parsing support, so %NULL may be provided for these
358 * Engines must ensure property registration happens exactly once,
359 * usually GTK+ deals with theming engines as singletons, so this
360 * should be guaranteed to happen once, but bear this in mind
361 * when creating #GtkThemeEngine<!-- -->s yourself.
365 * In order to make use of the custom registered properties in
366 * the CSS file, make sure the engine is loaded first by specifying
367 * the engine property, either in a previous rule or within the same
371 * engine: someengine;
372 * -SomeEngine-custom-property: 2;
380 gtk_theming_engine_register_property (const gchar *name_space,
381 GtkStylePropertyParser parse_func,
386 g_return_if_fail (name_space != NULL);
387 g_return_if_fail (strchr (name_space, ' ') == NULL);
388 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
390 /* FIXME: hack hack hack, replacing pspec->name to include namespace */
391 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
392 pspec->name = (char *)g_intern_string (name);
395 gtk_style_properties_register_property (parse_func, pspec);
399 * gtk_theming_engine_get_property:
400 * @engine: a #GtkThemingEngine
401 * @property: the property name
402 * @state: state to retrieve the value for
403 * @value: (out) (transfer full): return location for the property value,
404 * you must free this memory using g_value_unset() once you are
407 * Gets a property value as retrieved from the style settings that apply
408 * to the currently rendered element.
413 gtk_theming_engine_get_property (GtkThemingEngine *engine,
414 const gchar *property,
418 GtkThemingEnginePrivate *priv;
420 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
421 g_return_if_fail (property != NULL);
422 g_return_if_fail (value != NULL);
425 gtk_style_context_get_property (priv->context, property, state, value);
429 * gtk_theming_engine_get_valist:
430 * @engine: a #GtkThemingEngine
431 * @state: state to retrieve values for
432 * @args: va_list of property name/return location pairs, followed by %NULL
434 * Retrieves several style property values that apply to the currently
440 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
444 GtkThemingEnginePrivate *priv;
446 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
449 gtk_style_context_get_valist (priv->context, state, args);
453 * gtk_theming_engine_get:
454 * @engine: a #GtkThemingEngine
455 * @state: state to retrieve values for
456 * @...: property name /return value pairs, followed by %NULL
458 * Retrieves several style property values that apply to the currently
464 gtk_theming_engine_get (GtkThemingEngine *engine,
468 GtkThemingEnginePrivate *priv;
471 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
475 va_start (args, state);
476 gtk_style_context_get_valist (priv->context, state, args);
481 * gtk_theming_engine_get_style_property:
482 * @engine: a #GtkThemingEngine
483 * @property_name: the name of the widget style property
484 * @value: Return location for the property value, free with
485 * g_value_unset() after use.
487 * Gets the value for a widget style property.
492 gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
493 const gchar *property_name,
496 GtkThemingEnginePrivate *priv;
498 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
499 g_return_if_fail (property_name != NULL);
502 gtk_style_context_get_style_property (priv->context, property_name, value);
506 * gtk_theming_engine_get_style_valist:
507 * @engine: a #GtkThemingEngine
508 * @args: va_list of property name/return location pairs, followed by %NULL
510 * Retrieves several widget style properties from @engine according to the
511 * currently rendered content's style.
516 gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
519 GtkThemingEnginePrivate *priv;
521 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
524 gtk_style_context_get_style_valist (priv->context, args);
528 * gtk_theming_engine_get_style:
529 * @engine: a #GtkThemingEngine
530 * @...: property name /return value pairs, followed by %NULL
532 * Retrieves several widget style properties from @engine according
533 * to the currently rendered content's style.
538 gtk_theming_engine_get_style (GtkThemingEngine *engine,
541 GtkThemingEnginePrivate *priv;
544 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
548 va_start (args, engine);
549 gtk_style_context_get_style_valist (priv->context, args);
554 * gtk_theming_engine_lookup_color:
555 * @engine: a #GtkThemingEngine
556 * @color_name: color name to lookup
557 * @color: (out): Return location for the looked up color
559 * Looks up and resolves a color name in the current style's color map.
561 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
564 gtk_theming_engine_lookup_color (GtkThemingEngine *engine,
565 const gchar *color_name,
568 GtkThemingEnginePrivate *priv;
570 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
571 g_return_val_if_fail (color_name != NULL, FALSE);
574 return gtk_style_context_lookup_color (priv->context, color_name, color);
578 * gtk_theming_engine_get_state:
579 * @engine: a #GtkThemingEngine
581 * returns the state used when rendering.
583 * Returns: the state flags
588 gtk_theming_engine_get_state (GtkThemingEngine *engine)
590 GtkThemingEnginePrivate *priv;
592 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
595 return gtk_style_context_get_state (priv->context);
599 * gtk_theming_engine_state_is_running:
600 * @engine: a #GtkThemingEngine
601 * @state: a widget state
602 * @progress: (out): return location for the transition progress
604 * Returns %TRUE if there is a transition animation running for the
605 * current region (see gtk_style_context_push_animatable_region()).
607 * If @progress is not %NULL, the animation progress will be returned
608 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
609 * it's closest to being %TRUE. This means transition animations will
610 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
611 * it's being set to %FALSE.
613 * Returns: %TRUE if there is a running transition animation for @state.
618 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
622 GtkThemingEnginePrivate *priv;
624 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
627 return gtk_style_context_state_is_running (priv->context, state, progress);
631 * gtk_theming_engine_get_path:
632 * @engine: a #GtkThemingEngine
634 * Returns the widget path used for style matching.
636 * Returns: (transfer none): A #GtkWidgetPath
640 const GtkWidgetPath *
641 gtk_theming_engine_get_path (GtkThemingEngine *engine)
643 GtkThemingEnginePrivate *priv;
645 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
648 return gtk_style_context_get_path (priv->context);
652 * gtk_theming_engine_has_class:
653 * @engine: a #GtkThemingEngine
654 * @style_class: class name to look up
656 * Returns %TRUE if the currently rendered contents have
657 * defined the given class name.
659 * Returns: %TRUE if @engine has @class_name defined
664 gtk_theming_engine_has_class (GtkThemingEngine *engine,
665 const gchar *style_class)
667 GtkThemingEnginePrivate *priv;
669 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
672 return gtk_style_context_has_class (priv->context, style_class);
676 * gtk_theming_engine_has_region:
677 * @engine: a #GtkThemingEngine
678 * @style_region: a region name
679 * @flags: (out) (allow-none): return location for region flags
681 * Returns %TRUE if the currently rendered contents have the
682 * region defined. If @flags_return is not %NULL, it is set
683 * to the flags affecting the region.
685 * Returns: %TRUE if region is defined
690 gtk_theming_engine_has_region (GtkThemingEngine *engine,
691 const gchar *style_region,
692 GtkRegionFlags *flags)
694 GtkThemingEnginePrivate *priv;
699 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
702 return gtk_style_context_has_region (priv->context, style_region, flags);
706 * gtk_theming_engine_get_direction:
707 * @engine: a #GtkThemingEngine
709 * Returns the widget direction used for rendering.
711 * Returns: the widget direction
716 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
718 GtkThemingEnginePrivate *priv;
720 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
723 return gtk_style_context_get_direction (priv->context);
727 * gtk_theming_engine_get_junction_sides:
728 * @engine: a #GtkThemingEngine
730 * Returns the widget direction used for rendering.
732 * Returns: the widget direction
737 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
739 GtkThemingEnginePrivate *priv;
741 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
744 return gtk_style_context_get_junction_sides (priv->context);
748 * gtk_theming_engine_get_color:
749 * @engine: a #GtkThemingEngine
750 * @state: state to retrieve the color for
751 * @color: (out): return value for the foreground color
753 * Gets the foreground color for a given state.
758 gtk_theming_engine_get_color (GtkThemingEngine *engine,
762 GtkThemingEnginePrivate *priv;
764 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
767 gtk_style_context_get_color (priv->context, state, color);
771 * gtk_theming_engine_get_background_color:
772 * @engine: a #GtkThemingEngine
773 * @state: state to retrieve the color for
774 * @color: (out): return value for the background color
776 * Gets the background color for a given state.
781 gtk_theming_engine_get_background_color (GtkThemingEngine *engine,
785 GtkThemingEnginePrivate *priv;
787 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
790 gtk_style_context_get_background_color (priv->context, state, color);
794 * gtk_theming_engine_get_border_color:
795 * @engine: a #GtkThemingEngine
796 * @state: state to retrieve the color for
797 * @color: (out): return value for the border color
799 * Gets the border color for a given state.
804 gtk_theming_engine_get_border_color (GtkThemingEngine *engine,
808 GtkThemingEnginePrivate *priv;
810 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
813 gtk_style_context_get_border_color (priv->context, state, color);
817 * gtk_theming_engine_get_border:
818 * @engine: a #GtkthemingEngine
819 * @state: state to retrieve the border for
820 * @border: (out): return value for the border settings
822 * Gets the border for a given state as a #GtkBorder.
827 gtk_theming_engine_get_border (GtkThemingEngine *engine,
831 GtkThemingEnginePrivate *priv;
833 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
836 gtk_style_context_get_border (priv->context, state, border);
840 * gtk_theming_engine_get_padding:
841 * @engine: a #GtkthemingEngine
842 * @state: state to retrieve the padding for
843 * @padding: (out): return value for the padding settings
845 * Gets the padding for a given state as a #GtkBorder.
850 gtk_theming_engine_get_padding (GtkThemingEngine *engine,
854 GtkThemingEnginePrivate *priv;
856 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
859 gtk_style_context_get_padding (priv->context, state, padding);
863 * gtk_theming_engine_get_margin:
864 * @engine: a #GtkThemingEngine
865 * @state: state to retrieve the border for
866 * @margin: (out): return value for the margin settings
868 * Gets the margin for a given state as a #GtkBorder.
873 gtk_theming_engine_get_margin (GtkThemingEngine *engine,
877 GtkThemingEnginePrivate *priv;
879 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
882 gtk_style_context_get_margin (priv->context, state, margin);
886 * gtk_theming_engine_get_font:
887 * @engine: a #GtkThemingEngine
888 * @state: state to retrieve the font for
890 * Returns the font description for a given state.
892 * Returns: (transfer none): the #PangoFontDescription for the given
893 * state. This object is owned by GTK+ and should not be
898 const PangoFontDescription *
899 gtk_theming_engine_get_font (GtkThemingEngine *engine,
902 GtkThemingEnginePrivate *priv;
904 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
907 return gtk_style_context_get_font (priv->context, state);
910 /* GtkThemingModule */
913 gtk_theming_module_load (GTypeModule *type_module)
915 GtkThemingModule *theming_module;
917 gchar *name, *module_path;
919 theming_module = GTK_THEMING_MODULE (type_module);
920 name = theming_module->name;
921 module_path = _gtk_find_module (name, "theming-engines");
926 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
927 g_free (module_path);
932 if (!g_module_symbol (module, "theme_init",
933 (gpointer *) &theming_module->init) ||
934 !g_module_symbol (module, "theme_exit",
935 (gpointer *) &theming_module->exit) ||
936 !g_module_symbol (module, "create_engine",
937 (gpointer *) &theming_module->create_engine))
939 g_module_close (module);
944 theming_module->module = module;
946 theming_module->init (G_TYPE_MODULE (theming_module));
952 gtk_theming_module_unload (GTypeModule *type_module)
954 GtkThemingModule *theming_module;
956 theming_module = GTK_THEMING_MODULE (type_module);
958 theming_module->exit ();
960 g_module_close (theming_module->module);
962 theming_module->module = NULL;
963 theming_module->init = NULL;
964 theming_module->exit = NULL;
965 theming_module->create_engine = NULL;
969 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
971 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
973 module_class->load = gtk_theming_module_load;
974 module_class->unload = gtk_theming_module_unload;
978 gtk_theming_module_init (GtkThemingModule *module)
983 * gtk_theming_engine_load:
984 * @name: Theme engine name to load
986 * Loads and initializes a theming engine module from the
987 * standard directories.
989 * Returns: (transfer none): A theming engine, or %NULL if
990 * the engine @name doesn't exist.
993 gtk_theming_engine_load (const gchar *name)
995 static GHashTable *engines = NULL;
996 static GtkThemingEngine *default_engine;
997 GtkThemingEngine *engine = NULL;
1002 engines = g_hash_table_new (g_str_hash, g_str_equal);
1004 engine = g_hash_table_lookup (engines, name);
1008 GtkThemingModule *module;
1010 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
1011 g_type_module_set_name (G_TYPE_MODULE (module), name);
1012 module->name = g_strdup (name);
1014 if (module && g_type_module_use (G_TYPE_MODULE (module)))
1016 engine = (module->create_engine) ();
1019 g_hash_table_insert (engines, module->name, engine);
1025 if (G_UNLIKELY (!default_engine))
1026 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
1028 engine = default_engine;
1035 * gtk_theming_engine_get_screen:
1036 * @engine: a #GtkThemingEngine
1038 * Returns the #GdkScreen to which @engine currently rendering to.
1040 * Returns: (transfer none): a #GdkScreen, or %NULL.
1043 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
1045 GtkThemingEnginePrivate *priv;
1047 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
1049 priv = engine->priv;
1050 return gtk_style_context_get_screen (priv->context);
1053 /* Paint method implementations */
1055 gtk_theming_engine_render_check (GtkThemingEngine *engine,
1062 GdkRGBA fg_color, bg_color;
1063 GtkStateFlags flags;
1064 gint exterior_size, interior_size, thickness, pad;
1065 GtkBorderStyle border_style;
1069 flags = gtk_theming_engine_get_state (engine);
1072 gtk_theming_engine_get_color (engine, flags, &fg_color);
1073 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1074 gtk_theming_engine_get_border (engine, flags, &border);
1076 gtk_theming_engine_get (engine, flags,
1077 "border-style", &border_style,
1080 border_width = MIN (MIN (border.top, border.bottom),
1081 MIN (border.left, border.right));
1082 exterior_size = MIN (width, height);
1084 if (exterior_size % 2 == 0) /* Ensure odd */
1087 /* FIXME: thickness */
1089 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1090 interior_size = MAX (1, exterior_size - 2 * pad);
1092 if (interior_size < 7)
1095 pad = MAX (0, (exterior_size - interior_size) / 2);
1098 x -= (1 + exterior_size - (gint) width) / 2;
1099 y -= (1 + exterior_size - (gint) height) / 2;
1101 if (border_style == GTK_BORDER_STYLE_SOLID)
1103 GdkRGBA border_color;
1105 cairo_set_line_width (cr, border_width);
1106 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1108 cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
1109 gdk_cairo_set_source_rgba (cr, &bg_color);
1110 cairo_fill_preserve (cr);
1112 gdk_cairo_set_source_rgba (cr, &border_color);
1116 gdk_cairo_set_source_rgba (cr, &fg_color);
1118 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1120 int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1122 cairo_rectangle (cr,
1124 y + pad + (1 + interior_size - line_thickness) / 2,
1134 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1136 if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
1141 cairo_translate (cr,
1144 cairo_scale (cr, interior_size / 7., interior_size / 7.);
1146 cairo_rectangle (cr, 0, 0, 7 * progress, 7);
1149 cairo_move_to (cr, 7.0, 0.0);
1150 cairo_line_to (cr, 7.5, 1.0);
1151 cairo_curve_to (cr, 5.3, 2.0,
1154 cairo_curve_to (cr, 3.0, 5.7,
1157 cairo_line_to (cr, 0.2, 3.5);
1158 cairo_curve_to (cr, 1.1, 3.5,
1161 cairo_curve_to (cr, 1.0, 3.9,
1164 cairo_curve_to (cr, 3.5, 3.1,
1176 gtk_theming_engine_render_option (GtkThemingEngine *engine,
1183 GtkStateFlags flags;
1184 GdkRGBA fg_color, bg_color;
1185 gint exterior_size, interior_size, pad, thickness, border_width;
1186 GtkBorderStyle border_style;
1189 flags = gtk_theming_engine_get_state (engine);
1193 gtk_theming_engine_get_color (engine, flags, &fg_color);
1194 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1195 gtk_theming_engine_get_border (engine, flags, &border);
1197 gtk_theming_engine_get (engine, flags,
1198 "border-style", &border_style,
1201 exterior_size = MIN (width, height);
1202 border_width = MIN (MIN (border.top, border.bottom),
1203 MIN (border.left, border.right));
1205 if (exterior_size % 2 == 0) /* Ensure odd */
1208 x -= (1 + exterior_size - width) / 2;
1209 y -= (1 + exterior_size - height) / 2;
1211 if (border_style == GTK_BORDER_STYLE_SOLID)
1213 GdkRGBA border_color;
1215 cairo_set_line_width (cr, border_width);
1216 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1218 cairo_new_sub_path (cr);
1220 x + exterior_size / 2.,
1221 y + exterior_size / 2.,
1222 (exterior_size - 1) / 2.,
1225 gdk_cairo_set_source_rgba (cr, &bg_color);
1226 cairo_fill_preserve (cr);
1228 gdk_cairo_set_source_rgba (cr, &border_color);
1232 gdk_cairo_set_source_rgba (cr, &fg_color);
1234 /* FIXME: thickness */
1237 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1239 gint line_thickness;
1241 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1242 interior_size = MAX (1, exterior_size - 2 * pad);
1244 if (interior_size < 7)
1247 pad = MAX (0, (exterior_size - interior_size) / 2);
1250 line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1252 cairo_rectangle (cr,
1254 y + pad + (interior_size - line_thickness) / 2.,
1259 if (flags & GTK_STATE_FLAG_ACTIVE)
1261 pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1262 interior_size = MAX (1, exterior_size - 2 * pad);
1264 if (interior_size < 5)
1267 pad = MAX (0, (exterior_size - interior_size) / 2);
1270 cairo_new_sub_path (cr);
1272 x + pad + interior_size / 2.,
1273 y + pad + interior_size / 2.,
1283 add_path_arrow (cairo_t *cr,
1291 cairo_translate (cr, x + (size / 2), y + (size / 2));
1292 cairo_rotate (cr, angle);
1294 cairo_move_to (cr, 0, - (size / 4));
1295 cairo_line_to (cr, - (size / 2), (size / 4));
1296 cairo_line_to (cr, (size / 2), (size / 4));
1297 cairo_close_path (cr);
1303 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1310 GtkStateFlags flags;
1315 flags = gtk_theming_engine_get_state (engine);
1316 gtk_theming_engine_get_color (engine, flags, &fg_color);
1318 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1320 add_path_arrow (cr, angle, x + 1, y + 1, size);
1321 cairo_set_source_rgb (cr, 1, 1, 1);
1325 add_path_arrow (cr, angle, x, y, size);
1326 gdk_cairo_set_source_rgba (cr, &fg_color);
1333 add_path_line (cairo_t *cr,
1339 /* Adjust endpoints */
1353 cairo_move_to (cr, x1, y1);
1354 cairo_line_to (cr, x2, y2);
1358 color_shade (const GdkRGBA *color,
1360 GdkRGBA *color_return)
1362 GtkSymbolicColor *literal, *shade;
1364 literal = gtk_symbolic_color_new_literal (color);
1365 shade = gtk_symbolic_color_new_shade (literal, factor);
1366 gtk_symbolic_color_unref (literal);
1368 gtk_symbolic_color_resolve (shade, NULL, color_return);
1369 gtk_symbolic_color_unref (shade);
1373 render_background_internal (GtkThemingEngine *engine,
1379 GtkJunctionSides junction)
1382 cairo_pattern_t *pattern;
1383 GtkStateFlags flags;
1386 GtkRoundedBox border_box;
1387 GtkShadow *box_shadow;
1390 flags = gtk_theming_engine_get_state (engine);
1392 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1394 gtk_theming_engine_get (engine, flags,
1395 "background-image", &pattern,
1396 "box-shadow", &box_shadow,
1400 cairo_translate (cr, x, y);
1402 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1404 if (gtk_theming_engine_has_class (engine, "background"))
1406 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
1407 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1413 cairo_pattern_t *other_pattern;
1414 GtkStateFlags other_flags;
1416 cairo_pattern_t *new_pattern = NULL;
1418 if (flags & GTK_STATE_FLAG_PRELIGHT)
1420 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
1421 progress = 1 - progress;
1424 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
1426 gtk_theming_engine_get_background_color (engine, other_flags, &other_bg);
1427 gtk_theming_engine_get (engine, other_flags,
1428 "background-image", &other_pattern,
1431 if (pattern && other_pattern)
1433 cairo_pattern_type_t type, other_type;
1436 cairo_pattern_get_color_stop_count (pattern, &n0);
1437 cairo_pattern_get_color_stop_count (other_pattern, &n1);
1438 type = cairo_pattern_get_type (pattern);
1439 other_type = cairo_pattern_get_type (other_pattern);
1441 if (type == other_type && n0 == n1)
1443 gdouble offset0, red0, green0, blue0, alpha0;
1444 gdouble offset1, red1, green1, blue1, alpha1;
1445 gdouble x00, x01, y00, y01, x10, x11, y10, y11;
1446 gdouble r00, r01, r10, r11;
1449 if (type == CAIRO_PATTERN_TYPE_LINEAR)
1451 cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
1452 cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
1454 new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
1455 y00 + (y10 - y00) * progress,
1456 x01 + (x11 - x01) * progress,
1457 y01 + (y11 - y01) * progress);
1461 cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
1462 cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
1464 new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
1465 y00 + (y10 - y00) * progress,
1466 r00 + (r10 - r00) * progress,
1467 x01 + (x11 - x01) * progress,
1468 y01 + (y11 - y01) * progress,
1469 r01 + (r11 - r01) * progress);
1472 cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
1475 /* Blend both gradients into one */
1476 while (i < n0 && i < n1)
1478 cairo_pattern_get_color_stop_rgba (pattern, i,
1480 &red0, &green0, &blue0,
1482 cairo_pattern_get_color_stop_rgba (other_pattern, i,
1484 &red1, &green1, &blue1,
1487 cairo_pattern_add_color_stop_rgba (new_pattern,
1488 offset0 + ((offset1 - offset0) * progress),
1489 red0 + ((red1 - red0) * progress),
1490 green0 + ((green1 - green0) * progress),
1491 blue0 + ((blue1 - blue0) * progress),
1492 alpha0 + ((alpha1 - alpha0) * progress));
1500 cairo_rectangle (cr, 0, 0, width, height);
1503 cairo_push_group (cr);
1505 cairo_scale (cr, width, height);
1506 cairo_set_source (cr, other_pattern);
1507 cairo_paint_with_alpha (cr, progress);
1508 cairo_set_source (cr, pattern);
1509 cairo_paint_with_alpha (cr, 1.0 - progress);
1511 new_pattern = cairo_pop_group (cr);
1516 else if (pattern || other_pattern)
1520 gdouble x0, y0, x1, y1, r0, r1;
1523 /* Blend a pattern with a color */
1528 progress = 1 - progress;
1536 if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
1538 cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
1539 new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
1543 cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
1544 new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
1547 cairo_pattern_get_color_stop_count (p, &n);
1549 for (i = 0; i < n; i++)
1551 gdouble red1, green1, blue1, alpha1;
1554 cairo_pattern_get_color_stop_rgba (p, i,
1556 &red1, &green1, &blue1,
1558 cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1559 c->red + ((red1 - c->red) * progress),
1560 c->green + ((green1 - c->green) * progress),
1561 c->blue + ((blue1 - c->blue) * progress),
1562 c->alpha + ((alpha1 - c->alpha) * progress));
1567 /* Merge just colors */
1568 new_pattern = cairo_pattern_create_rgba (CLAMP (bg_color.red + ((other_bg.red - bg_color.red) * progress), 0, 1),
1569 CLAMP (bg_color.green + ((other_bg.green - bg_color.green) * progress), 0, 1),
1570 CLAMP (bg_color.blue + ((other_bg.blue - bg_color.blue) * progress), 0, 1),
1571 CLAMP (bg_color.alpha + ((other_bg.alpha - bg_color.alpha) * progress), 0, 1));
1576 /* Replace pattern to use */
1577 cairo_pattern_destroy (pattern);
1578 pattern = new_pattern;
1582 cairo_pattern_destroy (other_pattern);
1585 gtk_theming_engine_get_border (engine, flags, &border);
1587 /* In the CSS box model, by default the background positioning area is
1588 * the padding-box, i.e. all the border-box minus the borders themselves,
1589 * which determines also its default size, see
1590 * http://dev.w3.org/csswg/css3-background/#background-origin
1592 * In the future we might want to support different origins or clips, but
1593 * right now we just shrink to the default.
1595 _gtk_rounded_box_init_rect (&border_box, 0, 0, width, height);
1596 _gtk_rounded_box_apply_border_radius (&border_box, engine, flags, junction);
1597 _gtk_rounded_box_shrink (&border_box,
1598 border.top, border.right,
1599 border.bottom, border.left);
1600 _gtk_rounded_box_path (&border_box, cr);
1604 cairo_scale (cr, width, height);
1605 cairo_set_source (cr, pattern);
1606 cairo_scale (cr, 1.0 / width, 1.0 / height);
1609 gdk_cairo_set_source_rgba (cr, &bg_color);
1614 cairo_pattern_destroy (pattern);
1616 if (box_shadow != NULL)
1618 _gtk_box_shadow_render (box_shadow, cr, &border_box);
1619 _gtk_shadow_unref (box_shadow);
1626 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1633 GtkJunctionSides junction;
1635 junction = gtk_theming_engine_get_junction_sides (engine);
1637 render_background_internal (engine, cr,
1638 x, y, width, height,
1643 gtk_theming_engine_hide_border_sides (GtkBorder *border,
1646 if (hidden_side & SIDE_TOP)
1648 if (hidden_side & SIDE_RIGHT)
1650 if (hidden_side & SIDE_BOTTOM)
1652 if (hidden_side & SIDE_LEFT)
1657 render_frame_internal (GtkThemingEngine *engine,
1664 GtkJunctionSides junction)
1666 GtkStateFlags state;
1667 GtkBorderStyle border_style;
1668 GtkRoundedBox border_box, padding_box;
1672 static const guint current_side[4] = { SIDE_TOP, SIDE_RIGHT, SIDE_BOTTOM, SIDE_LEFT };
1676 state = gtk_theming_engine_get_state (engine);
1678 gtk_theming_engine_get_border (engine, state, &border);
1679 gtk_theming_engine_hide_border_sides (&border, hidden_side);
1681 gtk_theming_engine_get (engine, state,
1682 "border-style", &border_style,
1683 "border-top-color", &colors[0],
1684 "border-right-color", &colors[1],
1685 "border-bottom-color", &colors[2],
1686 "border-left-color", &colors[3],
1689 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1693 GtkStateFlags other_state;
1694 GdkRGBA *other_colors[4];
1696 if (state & GTK_STATE_FLAG_PRELIGHT)
1698 other_state = state & ~(GTK_STATE_FLAG_PRELIGHT);
1699 progress = 1 - progress;
1702 other_state = state | GTK_STATE_FLAG_PRELIGHT;
1704 gtk_theming_engine_get (engine, other_state,
1705 "border-top-color", &other_colors[0],
1706 "border-right-color", &other_colors[1],
1707 "border-bottom-color", &other_colors[2],
1708 "border-left-color", &other_colors[3],
1711 for (i = 0; i < 4; i++)
1713 colors[i]->red = CLAMP (colors[i]->red + ((other_colors[i]->red - colors[i]->red) * progress), 0, 1);
1714 colors[i]->green = CLAMP (colors[i]->green + ((other_colors[i]->green - colors[i]->green) * progress), 0, 1);
1715 colors[i]->blue = CLAMP (colors[i]->blue + ((other_colors[i]->blue - colors[i]->blue) * progress), 0, 1);
1716 colors[i]->alpha = CLAMP (colors[i]->alpha + ((other_colors[i]->alpha - colors[i]->alpha) * progress), 0, 1);
1717 gdk_rgba_free (other_colors[i]);
1721 switch (border_style)
1724 g_assert_not_reached ();
1725 case GTK_BORDER_STYLE_NONE:
1726 case GTK_BORDER_STYLE_SOLID:
1728 case GTK_BORDER_STYLE_INSET:
1729 color_shade (colors[1], 1.8, colors[1]);
1730 color_shade (colors[2], 1.8, colors[2]);
1732 case GTK_BORDER_STYLE_OUTSET:
1733 color_shade (colors[0], 1.8, colors[0]);
1734 color_shade (colors[3], 1.8, colors[3]);
1740 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
1742 _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1743 _gtk_rounded_box_apply_border_radius (&border_box, engine, state, junction);
1744 padding_box = border_box;
1745 _gtk_rounded_box_shrink (&padding_box, border.top, border.right, border.bottom, border.left);
1747 switch (border_style)
1749 case GTK_BORDER_STYLE_NONE:
1751 case GTK_BORDER_STYLE_SOLID:
1752 case GTK_BORDER_STYLE_INSET:
1753 case GTK_BORDER_STYLE_OUTSET:
1755 if (gdk_rgba_equal (colors[0], colors[1]) &&
1756 gdk_rgba_equal (colors[0], colors[2]) &&
1757 gdk_rgba_equal (colors[0], colors[3]))
1759 gdk_cairo_set_source_rgba (cr, colors[0]);
1761 _gtk_rounded_box_path (&border_box, cr);
1762 _gtk_rounded_box_path (&padding_box, cr);
1767 for (i = 0; i < 4; i++)
1769 if (hidden_side & current_side[i])
1772 for (j = 0; j < 4; j++)
1774 if (hidden_side & current_side[j])
1778 gdk_rgba_equal (colors[i], colors[j]))
1780 /* We were already painted when i == j */
1785 _gtk_rounded_box_path_top (&border_box, &padding_box, cr);
1787 _gtk_rounded_box_path_right (&border_box, &padding_box, cr);
1789 _gtk_rounded_box_path_bottom (&border_box, &padding_box, cr);
1791 _gtk_rounded_box_path_left (&border_box, &padding_box, cr);
1794 /* We were already painted when i == j */
1798 gdk_cairo_set_source_rgba (cr, colors[i]);
1808 for (i = 0; i < 4; i++)
1809 gdk_rgba_free (colors[i]);
1813 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1820 GtkStateFlags flags;
1821 GtkBorderStyle border_style;
1822 GtkJunctionSides junction;
1823 GtkBorderImage *border_image;
1826 flags = gtk_theming_engine_get_state (engine);
1827 junction = gtk_theming_engine_get_junction_sides (engine);
1828 gtk_theming_engine_get_border (engine, flags, &border);
1830 gtk_theming_engine_get (engine, flags,
1831 "border-image", &border_image,
1832 "border-style", &border_style,
1835 if (border_image != NULL)
1837 _gtk_border_image_render (border_image, &border,
1838 cr, x, y, width, height);
1839 _gtk_border_image_unref (border_image);
1841 else if (border_style != GTK_BORDER_STYLE_NONE)
1842 render_frame_internal (engine, cr,
1843 x, y, width, height,
1848 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1855 GtkStateFlags flags;
1856 GdkRGBA outline_color, fg_color;
1857 double vertical_overshoot;
1860 double interp; /* interpolation factor for center position */
1861 double x_double_horz, y_double_horz;
1862 double x_double_vert, y_double_vert;
1863 double x_double, y_double;
1866 gboolean running, is_rtl;
1870 flags = gtk_theming_engine_get_state (engine);
1872 gtk_theming_engine_get_color (engine, flags, &fg_color);
1873 gtk_theming_engine_get_border_color (engine, flags, &outline_color);
1875 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1876 is_rtl = (gtk_theming_engine_get_direction (engine) == GTK_TEXT_DIR_RTL);
1880 progress = (flags & GTK_STATE_FLAG_ACTIVE) ? 1 : 0;
1882 if (!gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_HORIZONTAL))
1885 angle = (G_PI) - ((G_PI / 2) * progress);
1887 angle = (G_PI / 2) * progress;
1892 angle = (G_PI / 2) + ((G_PI / 2) * progress);
1894 angle = (G_PI / 2) - ((G_PI / 2) * progress);
1899 /* Compute distance that the stroke extends beyonds the end
1900 * of the triangle we draw.
1902 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1904 /* For odd line widths, we end the vertical line of the triangle
1905 * at a half pixel, so we round differently.
1907 if (line_width % 2 == 1)
1908 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1910 vertical_overshoot = ceil (vertical_overshoot);
1912 /* Adjust the size of the triangle we draw so that the entire stroke fits
1914 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1916 /* If the line width is odd, we want the diameter to be even,
1917 * and vice versa, so force the sum to be odd. This relationship
1918 * makes the point of the triangle look right.
1920 diameter -= (1 - (diameter + line_width) % 2);
1922 radius = diameter / 2.;
1924 /* Adjust the center so that the stroke is properly aligned with
1925 * the pixel grid. The center adjustment is different for the
1926 * horizontal and vertical orientations. For intermediate positions
1927 * we interpolate between the two.
1929 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1930 y_double_vert = (y + height / 2) - 0.5;
1932 x_double_horz = (x + width / 2) - 0.5;
1933 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1935 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1936 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1938 cairo_translate (cr, x_double, y_double);
1939 cairo_rotate (cr, angle);
1941 cairo_move_to (cr, - radius / 2., - radius);
1942 cairo_line_to (cr, radius / 2., 0);
1943 cairo_line_to (cr, - radius / 2., radius);
1944 cairo_close_path (cr);
1946 cairo_set_line_width (cr, line_width);
1948 gdk_cairo_set_source_rgba (cr, &fg_color);
1950 cairo_fill_preserve (cr);
1952 gdk_cairo_set_source_rgba (cr, &outline_color);
1959 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1966 GtkStateFlags flags;
1972 flags = gtk_theming_engine_get_state (engine);
1974 gtk_theming_engine_get_color (engine, flags, &color);
1976 gtk_theming_engine_get_style (engine,
1977 "focus-line-width", &line_width,
1978 "focus-line-pattern", (gchar *) &dash_list,
1981 cairo_set_line_width (cr, (gdouble) line_width);
1985 gint n_dashes = strlen ((const gchar *) dash_list);
1986 gdouble *dashes = g_new (gdouble, n_dashes);
1987 gdouble total_length = 0;
1988 gdouble dash_offset;
1991 for (i = 0; i < n_dashes; i++)
1993 dashes[i] = dash_list[i];
1994 total_length += dash_list[i];
1997 /* The dash offset here aligns the pattern to integer pixels
1998 * by starting the dash at the right side of the left border
1999 * Negative dash offsets in cairo don't work
2000 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
2002 dash_offset = - line_width / 2.;
2004 while (dash_offset < 0)
2005 dash_offset += total_length;
2007 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
2011 cairo_rectangle (cr,
2012 x + line_width / 2.,
2013 y + line_width / 2.,
2015 height - line_width);
2017 gdk_cairo_set_source_rgba (cr, &color);
2026 gtk_theming_engine_render_line (GtkThemingEngine *engine,
2034 GtkStateFlags flags;
2035 cairo_matrix_t matrix;
2037 flags = gtk_theming_engine_get_state (engine);
2040 gtk_theming_engine_get_color (engine, flags, &color);
2042 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
2043 cairo_set_line_width (cr, 1);
2045 cairo_move_to (cr, x0 + 0.5, y0 + 0.5);
2046 cairo_line_to (cr, x1 + 0.5, y1 + 0.5);
2048 gdk_cairo_set_source_rgba (cr, &color);
2055 prepare_context_for_layout (cairo_t *cr,
2058 PangoLayout *layout)
2060 const PangoMatrix *matrix;
2062 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2064 cairo_move_to (cr, x, y);
2068 cairo_matrix_t cairo_matrix;
2070 cairo_matrix_init (&cairo_matrix,
2071 matrix->xx, matrix->yx,
2072 matrix->xy, matrix->yy,
2073 matrix->x0, matrix->y0);
2075 cairo_transform (cr, &cairo_matrix);
2080 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2084 PangoLayout *layout)
2087 GtkShadow *text_shadow = NULL;
2088 GtkStateFlags flags;
2093 flags = gtk_theming_engine_get_state (engine);
2094 gtk_theming_engine_get_color (engine, flags, &fg_color);
2096 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
2100 GtkStateFlags other_flags;
2103 if (flags & GTK_STATE_FLAG_PRELIGHT)
2105 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
2106 progress = 1 - progress;
2109 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
2111 gtk_theming_engine_get_color (engine, other_flags, &other_fg);
2113 fg_color.red = CLAMP (fg_color.red + ((other_fg.red - fg_color.red) * progress), 0, 1);
2114 fg_color.green = CLAMP (fg_color.green + ((other_fg.green - fg_color.green) * progress), 0, 1);
2115 fg_color.blue = CLAMP (fg_color.blue + ((other_fg.blue - fg_color.blue) * progress), 0, 1);
2116 fg_color.alpha = CLAMP (fg_color.alpha + ((other_fg.alpha - fg_color.alpha) * progress), 0, 1);
2119 gtk_theming_engine_get (engine, flags,
2120 "text-shadow", &text_shadow,
2123 prepare_context_for_layout (cr, x, y, layout);
2125 if (text_shadow != NULL)
2127 _gtk_text_shadow_paint_layout (text_shadow, cr, layout);
2128 _gtk_shadow_unref (text_shadow);
2131 gdk_cairo_set_source_rgba (cr, &fg_color);
2132 pango_cairo_show_layout (cr, layout);
2138 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2144 GtkOrientation orientation)
2146 const GtkWidgetPath *path;
2149 path = gtk_theming_engine_get_path (engine);
2151 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2152 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2154 /* FIXME: thickness */
2157 if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2159 if (orientation == GTK_ORIENTATION_VERTICAL)
2160 gtk_theming_engine_render_line (engine, cr,
2162 y + (gint) height / 2,
2163 x + width - thickness - 1,
2164 y + (gint) height / 2);
2166 gtk_theming_engine_render_line (engine, cr,
2167 x + (gint) width / 2,
2169 x + (gint) width / 2,
2170 y + height - thickness - 1);
2175 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2181 GtkPositionType gap_side,
2185 GtkJunctionSides junction;
2186 GtkStateFlags state;
2188 GtkCssBorderCornerRadius *top_left_radius, *top_right_radius;
2189 GtkCssBorderCornerRadius *bottom_left_radius, *bottom_right_radius;
2190 GtkCssBorderRadius border_radius = { { 0, }, };
2191 gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2194 xc = yc = wc = hc = 0;
2195 state = gtk_theming_engine_get_state (engine);
2196 junction = gtk_theming_engine_get_junction_sides (engine);
2198 gtk_theming_engine_get_border (engine, state, &border);
2199 gtk_theming_engine_get (engine, state,
2200 /* Can't use border-radius as it's an int for
2201 * backwards compat */
2202 "border-top-left-radius", &top_left_radius,
2203 "border-top-right-radius", &top_right_radius,
2204 "border-bottom-right-radius", &bottom_right_radius,
2205 "border-bottom-left-radius", &bottom_left_radius,
2208 if (top_left_radius)
2209 border_radius.top_left = *top_left_radius;
2210 g_free (top_left_radius);
2211 if (top_right_radius)
2212 border_radius.top_right = *top_right_radius;
2213 g_free (top_right_radius);
2214 if (bottom_right_radius)
2215 border_radius.bottom_right = *bottom_right_radius;
2216 g_free (bottom_right_radius);
2217 if (bottom_left_radius)
2218 border_radius.bottom_left = *bottom_left_radius;
2219 g_free (bottom_left_radius);
2221 border_width = MIN (MIN (border.top, border.bottom),
2222 MIN (border.left, border.right));
2229 xc = x + xy0_gap + border_width;
2231 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2234 if (xy0_gap < border_radius.top_left.horizontal)
2235 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2237 if (xy1_gap > width - border_radius.top_right.horizontal)
2238 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2240 case GTK_POS_BOTTOM:
2241 xc = x + xy0_gap + border_width;
2242 yc = y + height - border_width;
2243 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2246 if (xy0_gap < border_radius.bottom_left.horizontal)
2247 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2249 if (xy1_gap > width - border_radius.bottom_right.horizontal)
2250 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2255 yc = y + xy0_gap + border_width;
2257 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2259 if (xy0_gap < border_radius.top_left.vertical)
2260 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2262 if (xy1_gap > height - border_radius.bottom_left.vertical)
2263 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2267 xc = x + width - border_width;
2268 yc = y + xy0_gap + border_width;
2270 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2272 if (xy0_gap < border_radius.top_right.vertical)
2273 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2275 if (xy1_gap > height - border_radius.bottom_right.vertical)
2276 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2281 cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2282 cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2283 cairo_rectangle (cr, x0, yc, xc - x0, hc);
2284 cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2285 cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2288 render_frame_internal (engine, cr,
2289 x, y, width, height,
2296 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2302 GtkPositionType gap_side)
2304 GtkJunctionSides junction = 0;
2305 guint hidden_side = 0;
2312 junction = GTK_JUNCTION_LEFT;
2313 hidden_side = SIDE_LEFT;
2315 cairo_translate (cr, x + width, y);
2316 cairo_rotate (cr, G_PI / 2);
2319 junction = GTK_JUNCTION_RIGHT;
2320 hidden_side = SIDE_RIGHT;
2322 cairo_translate (cr, x, y + height);
2323 cairo_rotate (cr, - G_PI / 2);
2326 junction = GTK_JUNCTION_TOP;
2327 hidden_side = SIDE_TOP;
2329 cairo_translate (cr, x + width, y + height);
2330 cairo_rotate (cr, G_PI);
2332 case GTK_POS_BOTTOM:
2333 junction = GTK_JUNCTION_BOTTOM;
2334 hidden_side = SIDE_BOTTOM;
2336 cairo_translate (cr, x, y);
2340 if (gap_side == GTK_POS_TOP ||
2341 gap_side == GTK_POS_BOTTOM)
2342 render_background_internal (engine, cr,
2343 0, 0, width, height,
2344 GTK_JUNCTION_BOTTOM);
2346 render_background_internal (engine, cr,
2347 0, 0, height, width,
2348 GTK_JUNCTION_BOTTOM);
2353 render_frame_internal (engine, cr,
2354 x, y, width, height,
2355 hidden_side, junction);
2361 render_dot (cairo_t *cr,
2362 const GdkRGBA *lighter,
2363 const GdkRGBA *darker,
2368 size = CLAMP ((gint) size, 2, 3);
2372 gdk_cairo_set_source_rgba (cr, lighter);
2373 cairo_rectangle (cr, x, y, 1, 1);
2374 cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2379 gdk_cairo_set_source_rgba (cr, lighter);
2380 cairo_rectangle (cr, x, y, 2, 1);
2381 cairo_rectangle (cr, x, y, 1, 2);
2384 gdk_cairo_set_source_rgba (cr, darker);
2385 cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2386 cairo_rectangle (cr, x + 2, y, 1, 2);
2392 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2399 GtkStateFlags flags;
2400 GdkRGBA bg_color, lighter, darker;
2401 GtkJunctionSides sides;
2405 flags = gtk_theming_engine_get_state (engine);
2407 cairo_set_line_width (cr, 1.0);
2408 sides = gtk_theming_engine_get_junction_sides (engine);
2409 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2411 color_shade (&bg_color, 0.7, &darker);
2412 color_shade (&bg_color, 1.3, &lighter);
2414 render_background_internal (engine, cr, x, y, width, height, sides);
2416 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_GRIP))
2418 /* reduce confusing values to a meaningful state */
2419 if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2420 sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2422 if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2423 sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2426 sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2428 /* align drawing area to the connected side */
2429 if (sides == GTK_JUNCTION_LEFT)
2434 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2438 else if (height < width)
2441 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2443 /* make it square, aligning to bottom left */
2446 y += (height - width);
2449 else if (height < width)
2452 else if (sides == GTK_JUNCTION_RIGHT)
2454 /* aligning to right */
2457 x += (width - height);
2461 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2465 else if (height < width)
2467 x += (width - height);
2471 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2473 /* make it square, aligning to bottom right */
2476 y += (height - width);
2479 else if (height < width)
2481 x += (width - height);
2485 else if (sides == GTK_JUNCTION_TOP)
2490 else if (sides == GTK_JUNCTION_BOTTOM)
2492 /* align to bottom */
2495 y += (height - width);
2500 g_assert_not_reached ();
2502 if (sides == GTK_JUNCTION_LEFT ||
2503 sides == GTK_JUNCTION_RIGHT)
2509 while (xi < x + width)
2511 gdk_cairo_set_source_rgba (cr, &lighter);
2512 add_path_line (cr, x, y, x, y + height);
2516 gdk_cairo_set_source_rgba (cr, &darker);
2517 add_path_line (cr, xi, y, xi, y + height);
2522 else if (sides == GTK_JUNCTION_TOP ||
2523 sides == GTK_JUNCTION_BOTTOM)
2529 while (yi < y + height)
2531 gdk_cairo_set_source_rgba (cr, &lighter);
2532 add_path_line (cr, x, yi, x + width, yi);
2536 gdk_cairo_set_source_rgba (cr, &darker);
2537 add_path_line (cr, x, yi, x + width, yi);
2542 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2551 gdk_cairo_set_source_rgba (cr, &darker);
2552 add_path_line (cr, xi, y, x, yi);
2558 add_path_line (cr, xi, y, x, yi);
2564 gdk_cairo_set_source_rgba (cr, &lighter);
2565 add_path_line (cr, xi, y, x, yi);
2572 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2579 while (xi < (x + width - 3))
2581 gdk_cairo_set_source_rgba (cr, &lighter);
2582 add_path_line (cr, xi, y, x + width, yi);
2588 gdk_cairo_set_source_rgba (cr, &darker);
2589 add_path_line (cr, xi, y, x + width, yi);
2595 add_path_line (cr, xi, y, x + width, yi);
2602 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2611 gdk_cairo_set_source_rgba (cr, &darker);
2612 add_path_line (cr, x, yi, xi, y + height);
2618 add_path_line (cr, x, yi, xi, y + height);
2624 gdk_cairo_set_source_rgba (cr, &lighter);
2625 add_path_line (cr, x, yi, xi, y + height);
2632 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2639 while (xi < (x + width - 3))
2641 gdk_cairo_set_source_rgba (cr, &lighter);
2642 add_path_line (cr, xi, y + height, x + width, yi);
2648 gdk_cairo_set_source_rgba (cr, &darker);
2649 add_path_line (cr, xi, y + height, x + width, yi);
2655 add_path_line (cr, xi, y + height, x + width, yi);
2663 else if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_PANE_SEPARATOR))
2666 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2667 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2669 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2670 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2674 for (yy = y; yy < y + height; yy += 3)
2675 for (xx = x; xx < x + width; xx += 6)
2677 render_dot (cr, &lighter, &darker, xx, yy, 2);
2678 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2686 _gtk_theming_engine_paint_spinner (cairo_t *cr,
2691 guint num_steps, step;
2698 step = (guint) (progress * num_steps);
2704 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2705 cairo_set_line_width (cr, 2.0);
2707 half = num_steps / 2;
2709 for (i = 0; i < num_steps; i++)
2711 gint inset = 0.7 * radius;
2713 /* transparency is a function of time and intial value */
2714 gdouble t = 1.0 - (gdouble) ((i + step) % num_steps) / num_steps;
2715 gdouble xscale = - sin (i * G_PI / half);
2716 gdouble yscale = - cos (i * G_PI / half);
2718 cairo_set_source_rgba (cr,
2725 (radius - inset) * xscale,
2726 (radius - inset) * yscale);
2738 render_spinner (GtkThemingEngine *engine,
2745 GtkStateFlags state;
2751 state = gtk_theming_engine_get_state (engine);
2753 if (!gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2756 radius = MIN (width / 2, height / 2);
2758 gtk_theming_engine_get_color (engine, state, &color);
2759 gtk_theming_engine_get (engine, state,
2760 "icon-shadow", &shadow,
2764 cairo_translate (cr, x + width / 2, y + height / 2);
2768 _gtk_icon_shadow_paint_spinner (shadow, cr,
2771 _gtk_shadow_unref (shadow);
2774 _gtk_theming_engine_paint_spinner (cr,
2783 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2790 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_SPINNER))
2792 render_spinner (engine, cr, x, y, width, height);
2796 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2797 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2802 scale_or_ref (GdkPixbuf *src,
2806 if (width == gdk_pixbuf_get_width (src) &&
2807 height == gdk_pixbuf_get_height (src))
2808 return g_object_ref (src);
2810 return gdk_pixbuf_scale_simple (src,
2812 GDK_INTERP_BILINEAR);
2816 lookup_icon_size (GtkThemingEngine *engine,
2822 GtkSettings *settings;
2824 screen = gtk_theming_engine_get_screen (engine);
2825 settings = gtk_settings_get_for_screen (screen);
2827 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2831 colorshift_source (cairo_t *cr,
2834 cairo_pattern_t *source;
2839 source = cairo_pattern_reference (cairo_get_source (cr));
2841 cairo_set_source_rgb (cr, shift, shift, shift);
2842 cairo_set_operator (cr, CAIRO_OPERATOR_COLOR_DODGE);
2844 cairo_mask (cr, source);
2846 cairo_pattern_destroy (source);
2851 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2852 const GtkIconSource *source,
2857 GdkPixbuf *base_pixbuf;
2858 GtkStateFlags state;
2862 cairo_surface_t *surface;
2864 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2865 state = gtk_theming_engine_get_state (engine);
2867 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2869 if (size != (GtkIconSize) -1 &&
2870 !lookup_icon_size (engine, size, &width, &height))
2872 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2876 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2879 if (size != (GtkIconSize) -1 &&
2880 gtk_icon_source_get_size_wildcarded (source))
2881 scaled = scale_or_ref (base_pixbuf, width, height);
2883 scaled = g_object_ref (base_pixbuf);
2885 /* If the state was wildcarded, then generate a state. */
2886 if (gtk_icon_source_get_state_wildcarded (source))
2888 if (state & GTK_STATE_FLAG_INSENSITIVE)
2890 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2891 gdk_pixbuf_get_width (scaled),
2892 gdk_pixbuf_get_height (scaled));
2893 cr = cairo_create (surface);
2894 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2895 cairo_paint_with_alpha (cr, 0.5);
2899 g_object_unref (scaled);
2900 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2901 cairo_image_surface_get_width (surface),
2902 cairo_image_surface_get_height (surface));
2903 cairo_surface_destroy (surface);
2905 else if (state & GTK_STATE_FLAG_PRELIGHT)
2907 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2908 gdk_pixbuf_get_width (scaled),
2909 gdk_pixbuf_get_height (scaled));
2911 cr = cairo_create (surface);
2912 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2913 colorshift_source (cr, 0.10);
2917 g_object_unref (scaled);
2918 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2919 cairo_image_surface_get_width (surface),
2920 cairo_image_surface_get_height (surface));
2921 cairo_surface_destroy (surface);
2933 gtk_theming_engine_render_icon (GtkThemingEngine *engine,
2939 GtkStateFlags state;
2940 GtkShadow *icon_shadow;
2942 state = gtk_theming_engine_get_state (engine);
2946 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2948 gtk_theming_engine_get (engine, state,
2949 "icon-shadow", &icon_shadow,
2952 if (icon_shadow != NULL)
2954 _gtk_icon_shadow_paint (icon_shadow, cr);
2955 _gtk_shadow_unref (icon_shadow);