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.
58 typedef struct GtkThemingEnginePrivate GtkThemingEnginePrivate;
73 struct GtkThemingEnginePrivate
75 GtkStyleContext *context;
79 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
81 static void gtk_theming_engine_finalize (GObject *object);
82 static void gtk_theming_engine_impl_set_property (GObject *object,
86 static void gtk_theming_engine_impl_get_property (GObject *object,
91 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
97 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
103 static void gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
109 static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
115 static void gtk_theming_engine_render_frame (GtkThemingEngine *engine,
121 static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
127 static void gtk_theming_engine_render_focus (GtkThemingEngine *engine,
133 static void gtk_theming_engine_render_layout (GtkThemingEngine *engine,
137 PangoLayout *layout);
138 static void gtk_theming_engine_render_line (GtkThemingEngine *engine,
144 static void gtk_theming_engine_render_slider (GtkThemingEngine *engine,
150 GtkOrientation orientation);
151 static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
157 GtkPositionType gap_side,
160 static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
166 GtkPositionType gap_side);
167 static void gtk_theming_engine_render_handle (GtkThemingEngine *engine,
173 static void gtk_theming_engine_render_activity (GtkThemingEngine *engine,
179 static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
180 const GtkIconSource *source,
182 static void gtk_theming_engine_render_icon (GtkThemingEngine *engine,
188 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
191 typedef struct GtkThemingModule GtkThemingModule;
192 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
194 struct GtkThemingModule
196 GTypeModule parent_instance;
200 void (*init) (GTypeModule *module);
202 GtkThemingEngine * (*create_engine) (void);
205 struct GtkThemingModuleClass
207 GTypeModuleClass parent_class;
210 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
211 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
212 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
214 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
217 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
219 GObjectClass *object_class = G_OBJECT_CLASS (klass);
221 object_class->finalize = gtk_theming_engine_finalize;
222 object_class->set_property = gtk_theming_engine_impl_set_property;
223 object_class->get_property = gtk_theming_engine_impl_get_property;
225 klass->render_icon = gtk_theming_engine_render_icon;
226 klass->render_check = gtk_theming_engine_render_check;
227 klass->render_option = gtk_theming_engine_render_option;
228 klass->render_arrow = gtk_theming_engine_render_arrow;
229 klass->render_background = gtk_theming_engine_render_background;
230 klass->render_frame = gtk_theming_engine_render_frame;
231 klass->render_expander = gtk_theming_engine_render_expander;
232 klass->render_focus = gtk_theming_engine_render_focus;
233 klass->render_layout = gtk_theming_engine_render_layout;
234 klass->render_line = gtk_theming_engine_render_line;
235 klass->render_slider = gtk_theming_engine_render_slider;
236 klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
237 klass->render_extension = gtk_theming_engine_render_extension;
238 klass->render_handle = gtk_theming_engine_render_handle;
239 klass->render_activity = gtk_theming_engine_render_activity;
240 klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
243 * GtkThemingEngine:name:
245 * The theming engine name, this name will be used when registering
246 * custom properties, for a theming engine named "Clearlooks" registering
247 * a "glossy" custom property, it could be referenced in the CSS file as
250 * -Clearlooks-glossy: true;
255 g_object_class_install_property (object_class,
257 g_param_spec_string ("name",
259 P_("Theming engine name"),
261 G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));
263 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
267 gtk_theming_engine_init (GtkThemingEngine *engine)
269 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
273 gtk_theming_engine_finalize (GObject *object)
275 GtkThemingEnginePrivate *priv;
277 priv = GTK_THEMING_ENGINE (object)->priv;
280 G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
284 gtk_theming_engine_impl_set_property (GObject *object,
289 GtkThemingEnginePrivate *priv;
291 priv = GTK_THEMING_ENGINE (object)->priv;
299 priv->name = g_value_dup_string (value);
302 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
308 gtk_theming_engine_impl_get_property (GObject *object,
313 GtkThemingEnginePrivate *priv;
315 priv = GTK_THEMING_ENGINE (object)->priv;
320 g_value_set_string (value, priv->name);
323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
329 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
330 GtkStyleContext *context)
332 GtkThemingEnginePrivate *priv;
334 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
335 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
338 priv->context = context;
342 * gtk_theming_engine_register_property: (skip)
343 * @name_space: namespace for the property name
344 * @parse_func: parsing function to use, or %NULL
345 * @pspec: the #GParamSpec for the new property
347 * Registers a property so it can be used in the CSS file format,
348 * on the CSS file the property will look like
349 * "-${@name_space}-${property_name}". being
350 * ${property_name} the given to @pspec. @name_space will usually
351 * be the theme engine name.
353 * For any type a @parse_func may be provided, being this function
354 * used for turning any property value (between ':' and ';') in
355 * CSS to the #GValue needed. For basic types there is already
356 * builtin parsing support, so %NULL may be provided for these
360 * Engines must ensure property registration happens exactly once,
361 * usually GTK+ deals with theming engines as singletons, so this
362 * should be guaranteed to happen once, but bear this in mind
363 * when creating #GtkThemeEngine<!-- -->s yourself.
367 * In order to make use of the custom registered properties in
368 * the CSS file, make sure the engine is loaded first by specifying
369 * the engine property, either in a previous rule or within the same
373 * engine: someengine;
374 * -SomeEngine-custom-property: 2;
382 gtk_theming_engine_register_property (const gchar *name_space,
383 GtkStylePropertyParser parse_func,
388 g_return_if_fail (name_space != NULL);
389 g_return_if_fail (strchr (name_space, ' ') == NULL);
390 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
392 /* FIXME: hack hack hack, replacing pspec->name to include namespace */
393 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
394 g_free (pspec->name);
397 gtk_style_properties_register_property (parse_func, pspec);
401 * gtk_theming_engine_get_property:
402 * @engine: a #GtkThemingEngine
403 * @property: the property name
404 * @state: state to retrieve the value for
405 * @value: (out) (transfer full): return location for the property value,
406 * you must free this memory using g_value_unset() once you are
409 * Gets a property value as retrieved from the style settings that apply
410 * to the currently rendered element.
415 gtk_theming_engine_get_property (GtkThemingEngine *engine,
416 const gchar *property,
420 GtkThemingEnginePrivate *priv;
422 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
423 g_return_if_fail (property != NULL);
424 g_return_if_fail (value != NULL);
427 gtk_style_context_get_property (priv->context, property, state, value);
431 * gtk_theming_engine_get_valist:
432 * @engine: a #GtkThemingEngine
433 * @state: state to retrieve values for
434 * @args: va_list of property name/return location pairs, followed by %NULL
436 * Retrieves several style property values that apply to the currently
442 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
446 GtkThemingEnginePrivate *priv;
448 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
451 gtk_style_context_get_valist (priv->context, state, args);
455 * gtk_theming_engine_get:
456 * @engine: a #GtkThemingEngine
457 * @state: state to retrieve values for
458 * @...: property name /return value pairs, followed by %NULL
460 * Retrieves several style property values that apply to the currently
466 gtk_theming_engine_get (GtkThemingEngine *engine,
470 GtkThemingEnginePrivate *priv;
473 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
477 va_start (args, state);
478 gtk_style_context_get_valist (priv->context, state, args);
483 * gtk_theming_engine_get_style_property:
484 * @engine: a #GtkThemingEngine
485 * @property_name: the name of the widget style property
486 * @value: Return location for the property value, free with
487 * g_value_unset() after use.
489 * Gets the value for a widget style property.
494 gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
495 const gchar *property_name,
498 GtkThemingEnginePrivate *priv;
500 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
501 g_return_if_fail (property_name != NULL);
504 gtk_style_context_get_style_property (priv->context, property_name, value);
508 * gtk_theming_engine_get_style_valist:
509 * @engine: a #GtkThemingEngine
510 * @args: va_list of property name/return location pairs, followed by %NULL
512 * Retrieves several widget style properties from @engine according to the
513 * currently rendered content's style.
518 gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
521 GtkThemingEnginePrivate *priv;
523 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
526 gtk_style_context_get_style_valist (priv->context, args);
530 * gtk_theming_engine_get_style:
531 * @engine: a #GtkThemingEngine
532 * @...: property name /return value pairs, followed by %NULL
534 * Retrieves several widget style properties from @engine according
535 * to the currently rendered content's style.
540 gtk_theming_engine_get_style (GtkThemingEngine *engine,
543 GtkThemingEnginePrivate *priv;
546 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
550 va_start (args, engine);
551 gtk_style_context_get_style_valist (priv->context, args);
556 * gtk_theming_engine_lookup_color:
557 * @engine: a #GtkThemingEngine
558 * @color_name: color name to lookup
559 * @color: (out): Return location for the looked up color
561 * Looks up and resolves a color name in the current style's color map.
563 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
566 gtk_theming_engine_lookup_color (GtkThemingEngine *engine,
567 const gchar *color_name,
570 GtkThemingEnginePrivate *priv;
572 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
573 g_return_val_if_fail (color_name != NULL, FALSE);
576 return gtk_style_context_lookup_color (priv->context, color_name, color);
580 * gtk_theming_engine_get_state:
581 * @engine: a #GtkThemingEngine
583 * returns the state used when rendering.
585 * Returns: the state flags
590 gtk_theming_engine_get_state (GtkThemingEngine *engine)
592 GtkThemingEnginePrivate *priv;
594 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
597 return gtk_style_context_get_state (priv->context);
601 * gtk_theming_engine_state_is_running:
602 * @engine: a #GtkThemingEngine
603 * @state: a widget state
604 * @progress: (out): return location for the transition progress
606 * Returns %TRUE if there is a transition animation running for the
607 * current region (see gtk_style_context_push_animatable_region()).
609 * If @progress is not %NULL, the animation progress will be returned
610 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
611 * it's closest to being %TRUE. This means transition animations will
612 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
613 * it's being set to %FALSE.
615 * Returns: %TRUE if there is a running transition animation for @state.
620 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
624 GtkThemingEnginePrivate *priv;
626 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
629 return gtk_style_context_state_is_running (priv->context, state, progress);
633 * gtk_theming_engine_get_path:
634 * @engine: a #GtkThemingEngine
636 * Returns the widget path used for style matching.
638 * Returns: (transfer none): A #GtkWidgetPath
642 const GtkWidgetPath *
643 gtk_theming_engine_get_path (GtkThemingEngine *engine)
645 GtkThemingEnginePrivate *priv;
647 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
650 return gtk_style_context_get_path (priv->context);
654 * gtk_theming_engine_has_class:
655 * @engine: a #GtkThemingEngine
656 * @style_class: class name to look up
658 * Returns %TRUE if the currently rendered contents have
659 * defined the given class name.
661 * Returns: %TRUE if @engine has @class_name defined
666 gtk_theming_engine_has_class (GtkThemingEngine *engine,
667 const gchar *style_class)
669 GtkThemingEnginePrivate *priv;
671 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
674 return gtk_style_context_has_class (priv->context, style_class);
678 * gtk_theming_engine_has_region:
679 * @engine: a #GtkThemingEngine
680 * @style_region: a region name
681 * @flags: (out) (allow-none): return location for region flags
683 * Returns %TRUE if the currently rendered contents have the
684 * region defined. If @flags_return is not %NULL, it is set
685 * to the flags affecting the region.
687 * Returns: %TRUE if region is defined
692 gtk_theming_engine_has_region (GtkThemingEngine *engine,
693 const gchar *style_region,
694 GtkRegionFlags *flags)
696 GtkThemingEnginePrivate *priv;
701 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
704 return gtk_style_context_has_region (priv->context, style_region, flags);
708 * gtk_theming_engine_get_direction:
709 * @engine: a #GtkThemingEngine
711 * Returns the widget direction used for rendering.
713 * Returns: the widget direction
718 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
720 GtkThemingEnginePrivate *priv;
722 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
725 return gtk_style_context_get_direction (priv->context);
729 * gtk_theming_engine_get_junction_sides:
730 * @engine: a #GtkThemingEngine
732 * Returns the widget direction used for rendering.
734 * Returns: the widget direction
739 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
741 GtkThemingEnginePrivate *priv;
743 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
746 return gtk_style_context_get_junction_sides (priv->context);
750 * gtk_theming_engine_get_color:
751 * @engine: a #GtkThemingEngine
752 * @state: state to retrieve the color for
753 * @color: (out): return value for the foreground color
755 * Gets the foreground color for a given state.
760 gtk_theming_engine_get_color (GtkThemingEngine *engine,
764 GtkThemingEnginePrivate *priv;
766 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
769 gtk_style_context_get_color (priv->context, state, color);
773 * gtk_theming_engine_get_background_color:
774 * @engine: a #GtkThemingEngine
775 * @state: state to retrieve the color for
776 * @color: (out): return value for the background color
778 * Gets the background color for a given state.
783 gtk_theming_engine_get_background_color (GtkThemingEngine *engine,
787 GtkThemingEnginePrivate *priv;
789 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
792 gtk_style_context_get_background_color (priv->context, state, color);
796 * gtk_theming_engine_get_border_color:
797 * @engine: a #GtkThemingEngine
798 * @state: state to retrieve the color for
799 * @color: (out): return value for the border color
801 * Gets the border color for a given state.
806 gtk_theming_engine_get_border_color (GtkThemingEngine *engine,
810 GtkThemingEnginePrivate *priv;
812 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
815 gtk_style_context_get_border_color (priv->context, state, color);
819 * gtk_theming_engine_get_border:
820 * @engine: a #GtkthemingEngine
821 * @state: state to retrieve the border for
822 * @border: (out): return value for the border settings
824 * Gets the border for a given state as a #GtkBorder.
829 gtk_theming_engine_get_border (GtkThemingEngine *engine,
833 GtkThemingEnginePrivate *priv;
835 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
838 gtk_style_context_get_border (priv->context, state, border);
842 * gtk_theming_engine_get_padding:
843 * @engine: a #GtkthemingEngine
844 * @state: state to retrieve the padding for
845 * @padding: (out): return value for the padding settings
847 * Gets the padding for a given state as a #GtkBorder.
852 gtk_theming_engine_get_padding (GtkThemingEngine *engine,
856 GtkThemingEnginePrivate *priv;
858 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
861 gtk_style_context_get_padding (priv->context, state, padding);
865 * gtk_theming_engine_get_margin:
866 * @engine: a #GtkThemingEngine
867 * @state: state to retrieve the border for
868 * @margin: (out): return value for the margin settings
870 * Gets the margin for a given state as a #GtkBorder.
875 gtk_theming_engine_get_margin (GtkThemingEngine *engine,
879 GtkThemingEnginePrivate *priv;
881 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
884 gtk_style_context_get_margin (priv->context, state, margin);
888 * gtk_theming_engine_get_font:
889 * @engine: a #GtkThemingEngine
890 * @state: state to retrieve the font for
892 * Returns the font description for a given state.
894 * Returns: (transfer none): the #PangoFontDescription for the given
895 * state. This object is owned by GTK+ and should not be
900 const PangoFontDescription *
901 gtk_theming_engine_get_font (GtkThemingEngine *engine,
904 GtkThemingEnginePrivate *priv;
906 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
909 return gtk_style_context_get_font (priv->context, state);
912 /* GtkThemingModule */
915 gtk_theming_module_load (GTypeModule *type_module)
917 GtkThemingModule *theming_module;
919 gchar *name, *module_path;
921 theming_module = GTK_THEMING_MODULE (type_module);
922 name = theming_module->name;
923 module_path = _gtk_find_module (name, "theming-engines");
928 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
929 g_free (module_path);
934 if (!g_module_symbol (module, "theme_init",
935 (gpointer *) &theming_module->init) ||
936 !g_module_symbol (module, "theme_exit",
937 (gpointer *) &theming_module->exit) ||
938 !g_module_symbol (module, "create_engine",
939 (gpointer *) &theming_module->create_engine))
941 g_module_close (module);
946 theming_module->module = module;
948 theming_module->init (G_TYPE_MODULE (theming_module));
954 gtk_theming_module_unload (GTypeModule *type_module)
956 GtkThemingModule *theming_module;
958 theming_module = GTK_THEMING_MODULE (type_module);
960 theming_module->exit ();
962 g_module_close (theming_module->module);
964 theming_module->module = NULL;
965 theming_module->init = NULL;
966 theming_module->exit = NULL;
967 theming_module->create_engine = NULL;
971 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
973 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
975 module_class->load = gtk_theming_module_load;
976 module_class->unload = gtk_theming_module_unload;
980 gtk_theming_module_init (GtkThemingModule *module)
985 * gtk_theming_engine_load:
986 * @name: Theme engine name to load
988 * Loads and initializes a theming engine module from the
989 * standard directories.
991 * Returns: (transfer none): A theming engine, or %NULL if
992 * the engine @name doesn't exist.
995 gtk_theming_engine_load (const gchar *name)
997 static GHashTable *engines = NULL;
998 static GtkThemingEngine *default_engine;
999 GtkThemingEngine *engine = NULL;
1004 engines = g_hash_table_new (g_str_hash, g_str_equal);
1006 engine = g_hash_table_lookup (engines, name);
1010 GtkThemingModule *module;
1012 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
1013 g_type_module_set_name (G_TYPE_MODULE (module), name);
1014 module->name = g_strdup (name);
1016 if (module && g_type_module_use (G_TYPE_MODULE (module)))
1018 engine = (module->create_engine) ();
1021 g_hash_table_insert (engines, module->name, engine);
1028 if (G_UNLIKELY (!default_engine))
1029 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
1031 engine = default_engine;
1038 * gtk_theming_engine_get_screen:
1039 * @engine: a #GtkThemingEngine
1041 * Returns the #GdkScreen to which @engine currently rendering to.
1043 * Returns: (transfer none): a #GdkScreen, or %NULL.
1046 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
1048 GtkThemingEnginePrivate *priv;
1050 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
1052 priv = engine->priv;
1053 return gtk_style_context_get_screen (priv->context);
1056 /* Paint method implementations */
1058 gtk_theming_engine_render_check (GtkThemingEngine *engine,
1065 GdkRGBA fg_color, bg_color;
1066 GtkStateFlags flags;
1067 gint exterior_size, interior_size, thickness, pad;
1068 GtkBorderStyle border_style;
1072 flags = gtk_theming_engine_get_state (engine);
1075 gtk_theming_engine_get_color (engine, flags, &fg_color);
1076 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1077 gtk_theming_engine_get_border (engine, flags, &border);
1079 gtk_theming_engine_get (engine, flags,
1080 "border-style", &border_style,
1083 border_width = MIN (MIN (border.top, border.bottom),
1084 MIN (border.left, border.right));
1085 exterior_size = MIN (width, height);
1087 if (exterior_size % 2 == 0) /* Ensure odd */
1090 /* FIXME: thickness */
1092 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1093 interior_size = MAX (1, exterior_size - 2 * pad);
1095 if (interior_size < 7)
1098 pad = MAX (0, (exterior_size - interior_size) / 2);
1101 x -= (1 + exterior_size - (gint) width) / 2;
1102 y -= (1 + exterior_size - (gint) height) / 2;
1104 if (border_style == GTK_BORDER_STYLE_SOLID)
1106 GdkRGBA border_color;
1108 cairo_set_line_width (cr, border_width);
1109 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1111 cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
1112 gdk_cairo_set_source_rgba (cr, &bg_color);
1113 cairo_fill_preserve (cr);
1115 gdk_cairo_set_source_rgba (cr, &border_color);
1119 gdk_cairo_set_source_rgba (cr, &fg_color);
1121 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1123 int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1125 cairo_rectangle (cr,
1127 y + pad + (1 + interior_size - line_thickness) / 2,
1137 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1139 if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
1144 cairo_translate (cr,
1147 cairo_scale (cr, interior_size / 7., interior_size / 7.);
1149 cairo_rectangle (cr, 0, 0, 7 * progress, 7);
1152 cairo_move_to (cr, 7.0, 0.0);
1153 cairo_line_to (cr, 7.5, 1.0);
1154 cairo_curve_to (cr, 5.3, 2.0,
1157 cairo_curve_to (cr, 3.0, 5.7,
1160 cairo_line_to (cr, 0.2, 3.5);
1161 cairo_curve_to (cr, 1.1, 3.5,
1164 cairo_curve_to (cr, 1.0, 3.9,
1167 cairo_curve_to (cr, 3.5, 3.1,
1179 gtk_theming_engine_render_option (GtkThemingEngine *engine,
1186 GtkStateFlags flags;
1187 GdkRGBA fg_color, bg_color;
1188 gint exterior_size, interior_size, pad, thickness, border_width;
1189 GtkBorderStyle border_style;
1192 flags = gtk_theming_engine_get_state (engine);
1196 gtk_theming_engine_get_color (engine, flags, &fg_color);
1197 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1198 gtk_theming_engine_get_border (engine, flags, &border);
1200 gtk_theming_engine_get (engine, flags,
1201 "border-style", &border_style,
1204 exterior_size = MIN (width, height);
1205 border_width = MIN (MIN (border.top, border.bottom),
1206 MIN (border.left, border.right));
1208 if (exterior_size % 2 == 0) /* Ensure odd */
1211 x -= (1 + exterior_size - width) / 2;
1212 y -= (1 + exterior_size - height) / 2;
1214 if (border_style == GTK_BORDER_STYLE_SOLID)
1216 GdkRGBA border_color;
1218 cairo_set_line_width (cr, border_width);
1219 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1221 cairo_new_sub_path (cr);
1223 x + exterior_size / 2.,
1224 y + exterior_size / 2.,
1225 (exterior_size - 1) / 2.,
1228 gdk_cairo_set_source_rgba (cr, &bg_color);
1229 cairo_fill_preserve (cr);
1231 gdk_cairo_set_source_rgba (cr, &border_color);
1235 gdk_cairo_set_source_rgba (cr, &fg_color);
1237 /* FIXME: thickness */
1240 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1242 gint line_thickness;
1244 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1245 interior_size = MAX (1, exterior_size - 2 * pad);
1247 if (interior_size < 7)
1250 pad = MAX (0, (exterior_size - interior_size) / 2);
1253 line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1255 cairo_rectangle (cr,
1257 y + pad + (interior_size - line_thickness) / 2.,
1262 if (flags & GTK_STATE_FLAG_ACTIVE)
1264 pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1265 interior_size = MAX (1, exterior_size - 2 * pad);
1267 if (interior_size < 5)
1270 pad = MAX (0, (exterior_size - interior_size) / 2);
1273 cairo_new_sub_path (cr);
1275 x + pad + interior_size / 2.,
1276 y + pad + interior_size / 2.,
1286 add_path_arrow (cairo_t *cr,
1294 cairo_translate (cr, x + (size / 2), y + (size / 2));
1295 cairo_rotate (cr, angle);
1297 cairo_move_to (cr, 0, - (size / 4));
1298 cairo_line_to (cr, - (size / 2), (size / 4));
1299 cairo_line_to (cr, (size / 2), (size / 4));
1300 cairo_close_path (cr);
1306 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1313 GtkStateFlags flags;
1318 flags = gtk_theming_engine_get_state (engine);
1319 gtk_theming_engine_get_color (engine, flags, &fg_color);
1321 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1323 add_path_arrow (cr, angle, x + 1, y + 1, size);
1324 cairo_set_source_rgb (cr, 1, 1, 1);
1328 add_path_arrow (cr, angle, x, y, size);
1329 gdk_cairo_set_source_rgba (cr, &fg_color);
1336 add_path_line (cairo_t *cr,
1342 /* Adjust endpoints */
1356 cairo_move_to (cr, x1, y1);
1357 cairo_line_to (cr, x2, y2);
1361 color_shade (const GdkRGBA *color,
1363 GdkRGBA *color_return)
1365 GtkSymbolicColor *literal, *shade;
1367 literal = gtk_symbolic_color_new_literal (color);
1368 shade = gtk_symbolic_color_new_shade (literal, factor);
1369 gtk_symbolic_color_unref (literal);
1371 gtk_symbolic_color_resolve (shade, NULL, color_return);
1372 gtk_symbolic_color_unref (shade);
1376 render_background_internal (GtkThemingEngine *engine,
1382 GtkJunctionSides junction)
1385 cairo_pattern_t *pattern;
1386 GtkStateFlags flags;
1389 GtkRoundedBox border_box;
1390 GtkShadow *box_shadow;
1392 flags = gtk_theming_engine_get_state (engine);
1394 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1396 gtk_theming_engine_get (engine, flags,
1397 "background-image", &pattern,
1398 "box-shadow", &box_shadow,
1402 cairo_translate (cr, x, y);
1404 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1406 if (gtk_theming_engine_has_class (engine, "background"))
1408 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
1409 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1415 cairo_pattern_t *other_pattern;
1416 GtkStateFlags other_flags;
1418 cairo_pattern_t *new_pattern = NULL;
1420 if (flags & GTK_STATE_FLAG_PRELIGHT)
1422 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
1423 progress = 1 - progress;
1426 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
1428 gtk_theming_engine_get_background_color (engine, other_flags, &other_bg);
1429 gtk_theming_engine_get (engine, other_flags,
1430 "background-image", &other_pattern,
1433 if (pattern && other_pattern)
1435 cairo_pattern_type_t type, other_type;
1438 cairo_pattern_get_color_stop_count (pattern, &n0);
1439 cairo_pattern_get_color_stop_count (other_pattern, &n1);
1440 type = cairo_pattern_get_type (pattern);
1441 other_type = cairo_pattern_get_type (other_pattern);
1443 if (type == other_type && n0 == n1)
1445 gdouble offset0, red0, green0, blue0, alpha0;
1446 gdouble offset1, red1, green1, blue1, alpha1;
1447 gdouble x00, x01, y00, y01, x10, x11, y10, y11;
1448 gdouble r00, r01, r10, r11;
1451 if (type == CAIRO_PATTERN_TYPE_LINEAR)
1453 cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
1454 cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
1456 new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
1457 y00 + (y10 - y00) * progress,
1458 x01 + (x11 - x01) * progress,
1459 y01 + (y11 - y01) * progress);
1463 cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
1464 cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
1466 new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
1467 y00 + (y10 - y00) * progress,
1468 r00 + (r10 - r00) * progress,
1469 x01 + (x11 - x01) * progress,
1470 y01 + (y11 - y01) * progress,
1471 r01 + (r11 - r01) * progress);
1474 cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
1477 /* Blend both gradients into one */
1478 while (i < n0 && i < n1)
1480 cairo_pattern_get_color_stop_rgba (pattern, i,
1482 &red0, &green0, &blue0,
1484 cairo_pattern_get_color_stop_rgba (other_pattern, i,
1486 &red1, &green1, &blue1,
1489 cairo_pattern_add_color_stop_rgba (new_pattern,
1490 offset0 + ((offset1 - offset0) * progress),
1491 red0 + ((red1 - red0) * progress),
1492 green0 + ((green1 - green0) * progress),
1493 blue0 + ((blue1 - blue0) * progress),
1494 alpha0 + ((alpha1 - alpha0) * progress));
1502 cairo_rectangle (cr, 0, 0, width, height);
1505 cairo_push_group (cr);
1507 cairo_scale (cr, width, height);
1508 cairo_set_source (cr, other_pattern);
1509 cairo_paint_with_alpha (cr, progress);
1510 cairo_set_source (cr, pattern);
1511 cairo_paint_with_alpha (cr, 1.0 - progress);
1513 new_pattern = cairo_pop_group (cr);
1518 else if (pattern || other_pattern)
1522 gdouble x0, y0, x1, y1, r0, r1;
1525 /* Blend a pattern with a color */
1530 progress = 1 - progress;
1538 if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
1540 cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
1541 new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
1545 cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
1546 new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
1549 cairo_pattern_get_color_stop_count (p, &n);
1551 for (i = 0; i < n; i++)
1553 gdouble red1, green1, blue1, alpha1;
1556 cairo_pattern_get_color_stop_rgba (p, i,
1558 &red1, &green1, &blue1,
1560 cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1561 c->red + ((red1 - c->red) * progress),
1562 c->green + ((green1 - c->green) * progress),
1563 c->blue + ((blue1 - c->blue) * progress),
1564 c->alpha + ((alpha1 - c->alpha) * progress));
1569 /* Merge just colors */
1570 new_pattern = cairo_pattern_create_rgba (CLAMP (bg_color.red + ((other_bg.red - bg_color.red) * progress), 0, 1),
1571 CLAMP (bg_color.green + ((other_bg.green - bg_color.green) * progress), 0, 1),
1572 CLAMP (bg_color.blue + ((other_bg.blue - bg_color.blue) * progress), 0, 1),
1573 CLAMP (bg_color.alpha + ((other_bg.alpha - bg_color.alpha) * progress), 0, 1));
1578 /* Replace pattern to use */
1579 cairo_pattern_destroy (pattern);
1580 pattern = new_pattern;
1584 cairo_pattern_destroy (other_pattern);
1587 _gtk_rounded_box_init_rect (&border_box, 0, 0, width, height);
1588 _gtk_rounded_box_apply_border_radius (&border_box, engine, flags, junction);
1589 _gtk_rounded_box_path (&border_box, cr);
1593 cairo_scale (cr, width, height);
1594 cairo_set_source (cr, pattern);
1595 cairo_scale (cr, 1.0 / width, 1.0 / height);
1598 gdk_cairo_set_source_rgba (cr, &bg_color);
1603 cairo_pattern_destroy (pattern);
1605 if (box_shadow != NULL)
1608 gtk_theming_engine_get_border (engine, flags, &border);
1609 _gtk_rounded_box_shrink (&border_box,
1610 border.top, border.right,
1611 border.bottom, border.left);
1612 _gtk_box_shadow_render (box_shadow, cr, &border_box);
1613 _gtk_shadow_unref (box_shadow);
1620 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1627 GtkJunctionSides junction;
1629 junction = gtk_theming_engine_get_junction_sides (engine);
1631 render_background_internal (engine, cr,
1632 x, y, width, height,
1637 gtk_theming_engine_hide_border_sides (GtkBorder *border,
1640 if (hidden_side & SIDE_TOP)
1642 if (hidden_side & SIDE_RIGHT)
1644 if (hidden_side & SIDE_BOTTOM)
1646 if (hidden_side & SIDE_LEFT)
1651 render_frame_internal (GtkThemingEngine *engine,
1658 GtkJunctionSides junction)
1660 GtkStateFlags state;
1661 GtkBorderStyle border_style;
1662 GtkRoundedBox border_box, padding_box;
1666 static const guint current_side[4] = { SIDE_TOP, SIDE_RIGHT, SIDE_BOTTOM, SIDE_LEFT };
1670 state = gtk_theming_engine_get_state (engine);
1672 gtk_theming_engine_get_border (engine, state, &border);
1673 gtk_theming_engine_hide_border_sides (&border, hidden_side);
1675 gtk_theming_engine_get (engine, state,
1676 "border-style", &border_style,
1677 "border-top-color", &colors[0],
1678 "border-right-color", &colors[1],
1679 "border-bottom-color", &colors[2],
1680 "border-left-color", &colors[3],
1683 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1687 GtkStateFlags other_state;
1688 GdkRGBA *other_colors[4];
1690 if (state & GTK_STATE_FLAG_PRELIGHT)
1692 other_state = state & ~(GTK_STATE_FLAG_PRELIGHT);
1693 progress = 1 - progress;
1696 other_state = state | GTK_STATE_FLAG_PRELIGHT;
1698 gtk_theming_engine_get (engine, other_state,
1699 "border-top-color", &other_colors[0],
1700 "border-right-color", &other_colors[1],
1701 "border-bottom-color", &other_colors[2],
1702 "border-left-color", &other_colors[3],
1705 for (i = 0; i < 4; i++)
1707 colors[i]->red = CLAMP (colors[i]->red + ((other_colors[i]->red - colors[i]->red) * progress), 0, 1);
1708 colors[i]->green = CLAMP (colors[i]->green + ((other_colors[i]->green - colors[i]->green) * progress), 0, 1);
1709 colors[i]->blue = CLAMP (colors[i]->blue + ((other_colors[i]->blue - colors[i]->blue) * progress), 0, 1);
1710 colors[i]->alpha = CLAMP (colors[i]->alpha + ((other_colors[i]->alpha - colors[i]->alpha) * progress), 0, 1);
1711 gdk_rgba_free (other_colors[i]);
1715 switch (border_style)
1718 g_assert_not_reached ();
1719 case GTK_BORDER_STYLE_NONE:
1720 case GTK_BORDER_STYLE_SOLID:
1722 case GTK_BORDER_STYLE_INSET:
1723 color_shade (colors[1], 1.8, colors[1]);
1724 color_shade (colors[2], 1.8, colors[2]);
1726 case GTK_BORDER_STYLE_OUTSET:
1727 color_shade (colors[0], 1.8, colors[0]);
1728 color_shade (colors[3], 1.8, colors[3]);
1734 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
1736 _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1737 _gtk_rounded_box_apply_border_radius (&border_box, engine, state, junction);
1738 padding_box = border_box;
1739 _gtk_rounded_box_shrink (&padding_box, border.top, border.right, border.bottom, border.left);
1741 switch (border_style)
1743 case GTK_BORDER_STYLE_NONE:
1745 case GTK_BORDER_STYLE_SOLID:
1746 case GTK_BORDER_STYLE_INSET:
1747 case GTK_BORDER_STYLE_OUTSET:
1749 if (gdk_rgba_equal (colors[0], colors[1]) &&
1750 gdk_rgba_equal (colors[0], colors[2]) &&
1751 gdk_rgba_equal (colors[0], colors[3]))
1753 gdk_cairo_set_source_rgba (cr, colors[0]);
1755 _gtk_rounded_box_path (&border_box, cr);
1756 _gtk_rounded_box_path (&padding_box, cr);
1761 for (i = 0; i < 4; i++)
1763 if (hidden_side & current_side[i])
1766 for (j = 0; j < 4; j++)
1768 if (hidden_side & current_side[j])
1772 gdk_rgba_equal (colors[i], colors[j]))
1774 /* We were already painted when i == j */
1779 _gtk_rounded_box_path_top (&border_box, &padding_box, cr);
1781 _gtk_rounded_box_path_right (&border_box, &padding_box, cr);
1783 _gtk_rounded_box_path_bottom (&border_box, &padding_box, cr);
1785 _gtk_rounded_box_path_left (&border_box, &padding_box, cr);
1788 /* We were already painted when i == j */
1792 gdk_cairo_set_source_rgba (cr, colors[i]);
1802 for (i = 0; i < 4; i++)
1803 gdk_rgba_free (colors[i]);
1807 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1814 GtkStateFlags flags;
1815 GtkBorderStyle border_style;
1816 GtkJunctionSides junction;
1817 GtkBorderImage *border_image;
1820 flags = gtk_theming_engine_get_state (engine);
1821 junction = gtk_theming_engine_get_junction_sides (engine);
1822 gtk_theming_engine_get_border (engine, flags, &border);
1824 gtk_theming_engine_get (engine, flags,
1825 "border-image", &border_image,
1826 "border-style", &border_style,
1829 if (border_image != NULL)
1831 _gtk_border_image_render (border_image, &border,
1832 cr, x, y, width, height);
1833 _gtk_border_image_unref (border_image);
1835 else if (border_style != GTK_BORDER_STYLE_NONE)
1836 render_frame_internal (engine, cr,
1837 x, y, width, height,
1842 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1849 GtkStateFlags flags;
1850 GdkRGBA outline_color, fg_color;
1851 double vertical_overshoot;
1854 double interp; /* interpolation factor for center position */
1855 double x_double_horz, y_double_horz;
1856 double x_double_vert, y_double_vert;
1857 double x_double, y_double;
1860 gboolean running, is_rtl;
1864 flags = gtk_theming_engine_get_state (engine);
1866 gtk_theming_engine_get_color (engine, flags, &fg_color);
1867 gtk_theming_engine_get_border_color (engine, flags, &outline_color);
1869 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1870 is_rtl = (gtk_theming_engine_get_direction (engine) == GTK_TEXT_DIR_RTL);
1874 progress = (flags & GTK_STATE_FLAG_ACTIVE) ? 1 : 0;
1876 if (!gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_HORIZONTAL))
1879 angle = (G_PI) - ((G_PI / 2) * progress);
1881 angle = (G_PI / 2) * progress;
1886 angle = (G_PI / 2) + ((G_PI / 2) * progress);
1888 angle = (G_PI / 2) - ((G_PI / 2) * progress);
1893 /* Compute distance that the stroke extends beyonds the end
1894 * of the triangle we draw.
1896 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1898 /* For odd line widths, we end the vertical line of the triangle
1899 * at a half pixel, so we round differently.
1901 if (line_width % 2 == 1)
1902 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1904 vertical_overshoot = ceil (vertical_overshoot);
1906 /* Adjust the size of the triangle we draw so that the entire stroke fits
1908 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1910 /* If the line width is odd, we want the diameter to be even,
1911 * and vice versa, so force the sum to be odd. This relationship
1912 * makes the point of the triangle look right.
1914 diameter -= (1 - (diameter + line_width) % 2);
1916 radius = diameter / 2.;
1918 /* Adjust the center so that the stroke is properly aligned with
1919 * the pixel grid. The center adjustment is different for the
1920 * horizontal and vertical orientations. For intermediate positions
1921 * we interpolate between the two.
1923 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1924 y_double_vert = (y + height / 2) - 0.5;
1926 x_double_horz = (x + width / 2) - 0.5;
1927 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1929 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1930 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1932 cairo_translate (cr, x_double, y_double);
1933 cairo_rotate (cr, angle);
1935 cairo_move_to (cr, - radius / 2., - radius);
1936 cairo_line_to (cr, radius / 2., 0);
1937 cairo_line_to (cr, - radius / 2., radius);
1938 cairo_close_path (cr);
1940 cairo_set_line_width (cr, line_width);
1942 gdk_cairo_set_source_rgba (cr, &fg_color);
1944 cairo_fill_preserve (cr);
1946 gdk_cairo_set_source_rgba (cr, &outline_color);
1953 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1960 GtkStateFlags flags;
1966 flags = gtk_theming_engine_get_state (engine);
1968 gtk_theming_engine_get_color (engine, flags, &color);
1970 gtk_theming_engine_get_style (engine,
1971 "focus-line-width", &line_width,
1972 "focus-line-pattern", (gchar *) &dash_list,
1975 cairo_set_line_width (cr, (gdouble) line_width);
1979 gint n_dashes = strlen ((const gchar *) dash_list);
1980 gdouble *dashes = g_new (gdouble, n_dashes);
1981 gdouble total_length = 0;
1982 gdouble dash_offset;
1985 for (i = 0; i < n_dashes; i++)
1987 dashes[i] = dash_list[i];
1988 total_length += dash_list[i];
1991 /* The dash offset here aligns the pattern to integer pixels
1992 * by starting the dash at the right side of the left border
1993 * Negative dash offsets in cairo don't work
1994 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
1996 dash_offset = - line_width / 2.;
1998 while (dash_offset < 0)
1999 dash_offset += total_length;
2001 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
2005 cairo_rectangle (cr,
2006 x + line_width / 2.,
2007 y + line_width / 2.,
2009 height - line_width);
2011 gdk_cairo_set_source_rgba (cr, &color);
2020 gtk_theming_engine_render_line (GtkThemingEngine *engine,
2027 GdkRGBA bg_color, darker, lighter;
2028 GtkStateFlags flags;
2029 gint i, thickness, thickness_dark, thickness_light, len;
2030 cairo_matrix_t matrix;
2033 /* FIXME: thickness */
2035 thickness_dark = thickness / 2;
2036 thickness_light = thickness - thickness_dark;
2038 flags = gtk_theming_engine_get_state (engine);
2041 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2042 color_shade (&bg_color, 0.7, &darker);
2043 color_shade (&bg_color, 1.3, &lighter);
2045 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
2046 cairo_set_line_width (cr, 1);
2048 angle = atan2 (x1 - x0, y1 - y0);
2049 angle = (2 * G_PI) - angle;
2052 cairo_get_matrix (cr, &matrix);
2053 cairo_matrix_translate (&matrix, x0, y0);
2054 cairo_matrix_rotate (&matrix, angle);
2055 cairo_set_matrix (cr, &matrix);
2060 len = (gint) sqrt ((x1 * x1) + (y1 * y1));
2062 y0 = -thickness_dark;
2064 for (i = 0; i < thickness_dark; i++)
2066 gdk_cairo_set_source_rgba (cr, &lighter);
2067 add_path_line (cr, len - i - 1.5, y0, len - 0.5, y0);
2070 gdk_cairo_set_source_rgba (cr, &darker);
2071 add_path_line (cr, 0.5, y0, len - i - 1.5, y0);
2077 for (i = 0; i < thickness_light; i++)
2079 gdk_cairo_set_source_rgba (cr, &darker);
2080 add_path_line (cr, 0.5, y0, thickness_light - i + 0.5, y0);
2083 gdk_cairo_set_source_rgba (cr, &lighter);
2084 add_path_line (cr, thickness_light - i + 0.5, y0, len - 0.5, y0);
2094 prepare_context_for_layout (cairo_t *cr,
2097 PangoLayout *layout)
2099 const PangoMatrix *matrix;
2101 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2103 cairo_move_to (cr, x, y);
2107 cairo_matrix_t cairo_matrix;
2109 cairo_matrix_init (&cairo_matrix,
2110 matrix->xx, matrix->yx,
2111 matrix->xy, matrix->yy,
2112 matrix->x0, matrix->y0);
2114 cairo_transform (cr, &cairo_matrix);
2119 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2123 PangoLayout *layout)
2126 GtkShadow *text_shadow = NULL;
2127 GtkStateFlags flags;
2132 flags = gtk_theming_engine_get_state (engine);
2133 gtk_theming_engine_get_color (engine, flags, &fg_color);
2135 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
2139 GtkStateFlags other_flags;
2142 if (flags & GTK_STATE_FLAG_PRELIGHT)
2144 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
2145 progress = 1 - progress;
2148 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
2150 gtk_theming_engine_get_color (engine, other_flags, &other_fg);
2152 fg_color.red = CLAMP (fg_color.red + ((other_fg.red - fg_color.red) * progress), 0, 1);
2153 fg_color.green = CLAMP (fg_color.green + ((other_fg.green - fg_color.green) * progress), 0, 1);
2154 fg_color.blue = CLAMP (fg_color.blue + ((other_fg.blue - fg_color.blue) * progress), 0, 1);
2155 fg_color.alpha = CLAMP (fg_color.alpha + ((other_fg.alpha - fg_color.alpha) * progress), 0, 1);
2158 gtk_theming_engine_get (engine, flags,
2159 "text-shadow", &text_shadow,
2162 prepare_context_for_layout (cr, x, y, layout);
2164 if (text_shadow != NULL)
2166 _gtk_text_shadow_paint_layout (text_shadow, cr, layout);
2167 _gtk_shadow_unref (text_shadow);
2170 gdk_cairo_set_source_rgba (cr, &fg_color);
2171 pango_cairo_show_layout (cr, layout);
2177 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2183 GtkOrientation orientation)
2185 const GtkWidgetPath *path;
2188 path = gtk_theming_engine_get_path (engine);
2190 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2191 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2193 /* FIXME: thickness */
2196 if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2198 if (orientation == GTK_ORIENTATION_VERTICAL)
2199 gtk_theming_engine_render_line (engine, cr,
2201 y + (gint) height / 2,
2202 x + width - thickness - 1,
2203 y + (gint) height / 2);
2205 gtk_theming_engine_render_line (engine, cr,
2206 x + (gint) width / 2,
2208 x + (gint) width / 2,
2209 y + height - thickness - 1);
2214 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2220 GtkPositionType gap_side,
2224 GtkJunctionSides junction;
2225 GtkStateFlags state;
2227 GtkCssBorderCornerRadius *top_left_radius, *top_right_radius;
2228 GtkCssBorderCornerRadius *bottom_left_radius, *bottom_right_radius;
2229 GtkCssBorderRadius border_radius = { { 0, }, };
2230 gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2233 xc = yc = wc = hc = 0;
2234 state = gtk_theming_engine_get_state (engine);
2235 junction = gtk_theming_engine_get_junction_sides (engine);
2237 gtk_theming_engine_get_border (engine, state, &border);
2238 gtk_theming_engine_get (engine, state,
2239 /* Can't use border-radius as it's an int for
2240 * backwards compat */
2241 "border-top-left-radius", &top_left_radius,
2242 "border-top-right-radius", &top_right_radius,
2243 "border-bottom-right-radius", &bottom_right_radius,
2244 "border-bottom-left-radius", &bottom_left_radius,
2247 if (top_left_radius)
2248 border_radius.top_left = *top_left_radius;
2249 g_free (top_left_radius);
2250 if (top_right_radius)
2251 border_radius.top_right = *top_right_radius;
2252 g_free (top_right_radius);
2253 if (bottom_right_radius)
2254 border_radius.bottom_right = *bottom_right_radius;
2255 g_free (bottom_right_radius);
2256 if (bottom_left_radius)
2257 border_radius.bottom_left = *bottom_left_radius;
2258 g_free (bottom_left_radius);
2260 border_width = MIN (MIN (border.top, border.bottom),
2261 MIN (border.left, border.right));
2268 xc = x + xy0_gap + border_width;
2270 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2273 if (xy0_gap < border_radius.top_left.horizontal)
2274 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2276 if (xy1_gap > width - border_radius.top_right.horizontal)
2277 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2279 case GTK_POS_BOTTOM:
2280 xc = x + xy0_gap + border_width;
2281 yc = y + height - border_width;
2282 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2285 if (xy0_gap < border_radius.bottom_left.horizontal)
2286 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2288 if (xy1_gap > width - border_radius.bottom_right.horizontal)
2289 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2294 yc = y + xy0_gap + border_width;
2296 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2298 if (xy0_gap < border_radius.top_left.vertical)
2299 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2301 if (xy1_gap > height - border_radius.bottom_left.vertical)
2302 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2306 xc = x + width - border_width;
2307 yc = y + xy0_gap + border_width;
2309 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2311 if (xy0_gap < border_radius.top_right.vertical)
2312 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2314 if (xy1_gap > height - border_radius.bottom_right.vertical)
2315 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2320 cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2321 cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2322 cairo_rectangle (cr, x0, yc, xc - x0, hc);
2323 cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2324 cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2327 render_frame_internal (engine, cr,
2328 x, y, width, height,
2335 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2341 GtkPositionType gap_side)
2343 GtkJunctionSides junction = 0;
2344 guint hidden_side = 0;
2351 junction = GTK_JUNCTION_LEFT;
2352 hidden_side = SIDE_LEFT;
2354 cairo_translate (cr, x + width, y);
2355 cairo_rotate (cr, G_PI / 2);
2358 junction = GTK_JUNCTION_RIGHT;
2359 hidden_side = SIDE_RIGHT;
2361 cairo_translate (cr, x, y + height);
2362 cairo_rotate (cr, - G_PI / 2);
2365 junction = GTK_JUNCTION_TOP;
2366 hidden_side = SIDE_TOP;
2368 cairo_translate (cr, x + width, y + height);
2369 cairo_rotate (cr, G_PI);
2371 case GTK_POS_BOTTOM:
2372 junction = GTK_JUNCTION_BOTTOM;
2373 hidden_side = SIDE_BOTTOM;
2375 cairo_translate (cr, x, y);
2379 if (gap_side == GTK_POS_TOP ||
2380 gap_side == GTK_POS_BOTTOM)
2381 render_background_internal (engine, cr,
2382 0, 0, width, height,
2383 GTK_JUNCTION_BOTTOM);
2385 render_background_internal (engine, cr,
2386 0, 0, height, width,
2387 GTK_JUNCTION_BOTTOM);
2392 render_frame_internal (engine, cr,
2393 x, y, width, height,
2394 hidden_side, junction);
2400 render_dot (cairo_t *cr,
2401 const GdkRGBA *lighter,
2402 const GdkRGBA *darker,
2407 size = CLAMP ((gint) size, 2, 3);
2411 gdk_cairo_set_source_rgba (cr, lighter);
2412 cairo_rectangle (cr, x, y, 1, 1);
2413 cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2418 gdk_cairo_set_source_rgba (cr, lighter);
2419 cairo_rectangle (cr, x, y, 2, 1);
2420 cairo_rectangle (cr, x, y, 1, 2);
2423 gdk_cairo_set_source_rgba (cr, darker);
2424 cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2425 cairo_rectangle (cr, x + 2, y, 1, 2);
2431 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2438 GtkStateFlags flags;
2439 GdkRGBA bg_color, lighter, darker;
2440 GtkJunctionSides sides;
2444 flags = gtk_theming_engine_get_state (engine);
2446 cairo_set_line_width (cr, 1.0);
2447 sides = gtk_theming_engine_get_junction_sides (engine);
2448 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2450 color_shade (&bg_color, 0.7, &darker);
2451 color_shade (&bg_color, 1.3, &lighter);
2453 render_background_internal (engine, cr, x, y, width, height, sides);
2455 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_GRIP))
2457 /* reduce confusing values to a meaningful state */
2458 if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2459 sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2461 if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2462 sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2465 sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2467 /* align drawing area to the connected side */
2468 if (sides == GTK_JUNCTION_LEFT)
2473 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2477 else if (height < width)
2480 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2482 /* make it square, aligning to bottom left */
2485 y += (height - width);
2488 else if (height < width)
2491 else if (sides == GTK_JUNCTION_RIGHT)
2493 /* aligning to right */
2496 x += (width - height);
2500 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2504 else if (height < width)
2506 x += (width - height);
2510 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2512 /* make it square, aligning to bottom right */
2515 y += (height - width);
2518 else if (height < width)
2520 x += (width - height);
2524 else if (sides == GTK_JUNCTION_TOP)
2529 else if (sides == GTK_JUNCTION_BOTTOM)
2531 /* align to bottom */
2534 y += (height - width);
2539 g_assert_not_reached ();
2541 if (sides == GTK_JUNCTION_LEFT ||
2542 sides == GTK_JUNCTION_RIGHT)
2548 while (xi < x + width)
2550 gdk_cairo_set_source_rgba (cr, &lighter);
2551 add_path_line (cr, x, y, x, y + height);
2555 gdk_cairo_set_source_rgba (cr, &darker);
2556 add_path_line (cr, xi, y, xi, y + height);
2561 else if (sides == GTK_JUNCTION_TOP ||
2562 sides == GTK_JUNCTION_BOTTOM)
2568 while (yi < y + height)
2570 gdk_cairo_set_source_rgba (cr, &lighter);
2571 add_path_line (cr, x, yi, x + width, yi);
2575 gdk_cairo_set_source_rgba (cr, &darker);
2576 add_path_line (cr, x, yi, x + width, yi);
2581 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2590 gdk_cairo_set_source_rgba (cr, &darker);
2591 add_path_line (cr, xi, y, x, yi);
2597 add_path_line (cr, xi, y, x, yi);
2603 gdk_cairo_set_source_rgba (cr, &lighter);
2604 add_path_line (cr, xi, y, x, yi);
2611 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2618 while (xi < (x + width - 3))
2620 gdk_cairo_set_source_rgba (cr, &lighter);
2621 add_path_line (cr, xi, y, x + width, yi);
2627 gdk_cairo_set_source_rgba (cr, &darker);
2628 add_path_line (cr, xi, y, x + width, yi);
2634 add_path_line (cr, xi, y, x + width, yi);
2641 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2650 gdk_cairo_set_source_rgba (cr, &darker);
2651 add_path_line (cr, x, yi, xi, y + height);
2657 add_path_line (cr, x, yi, xi, y + height);
2663 gdk_cairo_set_source_rgba (cr, &lighter);
2664 add_path_line (cr, x, yi, xi, y + height);
2671 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2678 while (xi < (x + width - 3))
2680 gdk_cairo_set_source_rgba (cr, &lighter);
2681 add_path_line (cr, xi, y + height, x + width, yi);
2687 gdk_cairo_set_source_rgba (cr, &darker);
2688 add_path_line (cr, xi, y + height, x + width, yi);
2694 add_path_line (cr, xi, y + height, x + width, yi);
2702 else if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_PANE_SEPARATOR))
2705 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2706 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2708 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2709 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2713 for (yy = y; yy < y + height; yy += 3)
2714 for (xx = x; xx < x + width; xx += 6)
2716 render_dot (cr, &lighter, &darker, xx, yy, 2);
2717 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2725 _gtk_theming_engine_paint_spinner (cairo_t *cr,
2730 guint num_steps, step;
2737 step = (guint) (progress * num_steps);
2743 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2744 cairo_set_line_width (cr, 2.0);
2746 half = num_steps / 2;
2748 for (i = 0; i < num_steps; i++)
2750 gint inset = 0.7 * radius;
2752 /* transparency is a function of time and intial value */
2753 gdouble t = 1.0 - (gdouble) ((i + step) % num_steps) / num_steps;
2754 gdouble xscale = - sin (i * G_PI / half);
2755 gdouble yscale = - cos (i * G_PI / half);
2757 cairo_set_source_rgba (cr,
2764 (radius - inset) * xscale,
2765 (radius - inset) * yscale);
2777 render_spinner (GtkThemingEngine *engine,
2784 GtkStateFlags state;
2790 state = gtk_theming_engine_get_state (engine);
2792 if (!gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2795 radius = MIN (width / 2, height / 2);
2797 gtk_theming_engine_get_color (engine, state, &color);
2798 gtk_theming_engine_get (engine, state,
2799 "icon-shadow", &shadow,
2803 cairo_translate (cr, x + width / 2, y + height / 2);
2807 _gtk_icon_shadow_paint_spinner (shadow, cr,
2810 _gtk_shadow_unref (shadow);
2813 _gtk_theming_engine_paint_spinner (cr,
2822 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2829 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_SPINNER))
2831 render_spinner (engine, cr, x, y, width, height);
2835 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2836 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2841 scale_or_ref (GdkPixbuf *src,
2845 if (width == gdk_pixbuf_get_width (src) &&
2846 height == gdk_pixbuf_get_height (src))
2847 return g_object_ref (src);
2849 return gdk_pixbuf_scale_simple (src,
2851 GDK_INTERP_BILINEAR);
2855 lookup_icon_size (GtkThemingEngine *engine,
2861 GtkSettings *settings;
2863 screen = gtk_theming_engine_get_screen (engine);
2864 settings = gtk_settings_get_for_screen (screen);
2866 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2870 colorshift_source (cairo_t *cr,
2873 cairo_pattern_t *source;
2878 source = cairo_pattern_reference (cairo_get_source (cr));
2880 cairo_set_source_rgb (cr, shift, shift, shift);
2881 cairo_set_operator (cr, CAIRO_OPERATOR_COLOR_DODGE);
2883 cairo_mask (cr, source);
2885 cairo_pattern_destroy (source);
2890 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2891 const GtkIconSource *source,
2896 GdkPixbuf *base_pixbuf;
2897 GtkStateFlags state;
2901 cairo_surface_t *surface;
2903 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2904 state = gtk_theming_engine_get_state (engine);
2906 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2908 if (size != (GtkIconSize) -1 &&
2909 !lookup_icon_size (engine, size, &width, &height))
2911 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2915 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2918 if (size != (GtkIconSize) -1 &&
2919 gtk_icon_source_get_size_wildcarded (source))
2920 scaled = scale_or_ref (base_pixbuf, width, height);
2922 scaled = g_object_ref (base_pixbuf);
2924 /* If the state was wildcarded, then generate a state. */
2925 if (gtk_icon_source_get_state_wildcarded (source))
2927 if (state & GTK_STATE_FLAG_INSENSITIVE)
2929 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2930 gdk_pixbuf_get_width (scaled),
2931 gdk_pixbuf_get_height (scaled));
2932 cr = cairo_create (surface);
2933 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2934 cairo_paint_with_alpha (cr, 0.5);
2938 g_object_unref (scaled);
2939 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2940 cairo_image_surface_get_width (surface),
2941 cairo_image_surface_get_height (surface));
2942 cairo_surface_destroy (surface);
2944 else if (state & GTK_STATE_FLAG_PRELIGHT)
2946 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2947 gdk_pixbuf_get_width (scaled),
2948 gdk_pixbuf_get_height (scaled));
2950 cr = cairo_create (surface);
2951 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2952 colorshift_source (cr, 0.10);
2956 g_object_unref (scaled);
2957 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2958 cairo_image_surface_get_width (surface),
2959 cairo_image_surface_get_height (surface));
2960 cairo_surface_destroy (surface);
2972 gtk_theming_engine_render_icon (GtkThemingEngine *engine,
2978 GtkStateFlags state;
2979 GtkShadow *icon_shadow;
2981 state = gtk_theming_engine_get_state (engine);
2985 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2987 gtk_theming_engine_get (engine, state,
2988 "icon-shadow", &icon_shadow,
2991 if (icon_shadow != NULL)
2993 _gtk_icon_shadow_paint (icon_shadow, cr);
2994 _gtk_shadow_unref (icon_shadow);