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 "gtkmodulesprivate.h"
31 #include "gtkborderimageprivate.h"
33 #include "gtkshadowprivate.h"
34 #include "gtkcsstypesprivate.h"
35 #include "gtkthemingengineprivate.h"
36 #include "gtkroundedboxprivate.h"
37 #include "gtkthemingbackgroundprivate.h"
40 * SECTION:gtkthemingengine
41 * @Short_description: Theming renderers
42 * @Title: GtkThemingEngine
43 * @See_also: #GtkStyleContext
45 * #GtkThemingEngine is the object used for rendering themed content
46 * in GTK+ widgets. Even though GTK+ has a default implementation,
47 * it can be overridden in CSS files by enforcing a #GtkThemingEngine
48 * object to be loaded as a module.
50 * In order to implement a theming engine, a #GtkThemingEngine subclass
51 * must be created, alongside the CSS file that will reference it, the
52 * theming engine would be created as an .so library, and installed in
53 * $(gtk-modules-dir)/theming-engines/.
55 * #GtkThemingEngine<!-- -->s have limited access to the object they are
56 * rendering, the #GtkThemingEngine API has read-only accessors to the
57 * style information contained in the rendered object's #GtkStyleContext.
65 struct GtkThemingEnginePrivate
67 GtkStyleContext *context;
71 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
73 static void gtk_theming_engine_finalize (GObject *object);
74 static void gtk_theming_engine_impl_set_property (GObject *object,
78 static void gtk_theming_engine_impl_get_property (GObject *object,
83 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
89 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
95 static void gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
101 static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
107 static void gtk_theming_engine_render_frame (GtkThemingEngine *engine,
113 static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
119 static void gtk_theming_engine_render_focus (GtkThemingEngine *engine,
125 static void gtk_theming_engine_render_layout (GtkThemingEngine *engine,
129 PangoLayout *layout);
130 static void gtk_theming_engine_render_line (GtkThemingEngine *engine,
136 static void gtk_theming_engine_render_slider (GtkThemingEngine *engine,
142 GtkOrientation orientation);
143 static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
149 GtkPositionType gap_side,
152 static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
158 GtkPositionType gap_side);
159 static void gtk_theming_engine_render_handle (GtkThemingEngine *engine,
165 static void gtk_theming_engine_render_activity (GtkThemingEngine *engine,
171 static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
172 const GtkIconSource *source,
174 static void gtk_theming_engine_render_icon (GtkThemingEngine *engine,
180 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
183 typedef struct GtkThemingModule GtkThemingModule;
184 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
186 struct GtkThemingModule
188 GTypeModule parent_instance;
192 void (*init) (GTypeModule *module);
194 GtkThemingEngine * (*create_engine) (void);
197 struct GtkThemingModuleClass
199 GTypeModuleClass parent_class;
202 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
203 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
204 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
206 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
209 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
211 GObjectClass *object_class = G_OBJECT_CLASS (klass);
213 object_class->finalize = gtk_theming_engine_finalize;
214 object_class->set_property = gtk_theming_engine_impl_set_property;
215 object_class->get_property = gtk_theming_engine_impl_get_property;
217 klass->render_icon = gtk_theming_engine_render_icon;
218 klass->render_check = gtk_theming_engine_render_check;
219 klass->render_option = gtk_theming_engine_render_option;
220 klass->render_arrow = gtk_theming_engine_render_arrow;
221 klass->render_background = gtk_theming_engine_render_background;
222 klass->render_frame = gtk_theming_engine_render_frame;
223 klass->render_expander = gtk_theming_engine_render_expander;
224 klass->render_focus = gtk_theming_engine_render_focus;
225 klass->render_layout = gtk_theming_engine_render_layout;
226 klass->render_line = gtk_theming_engine_render_line;
227 klass->render_slider = gtk_theming_engine_render_slider;
228 klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
229 klass->render_extension = gtk_theming_engine_render_extension;
230 klass->render_handle = gtk_theming_engine_render_handle;
231 klass->render_activity = gtk_theming_engine_render_activity;
232 klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
235 * GtkThemingEngine:name:
237 * The theming engine name, this name will be used when registering
238 * custom properties, for a theming engine named "Clearlooks" registering
239 * a "glossy" custom property, it could be referenced in the CSS file as
242 * -Clearlooks-glossy: true;
247 g_object_class_install_property (object_class,
249 g_param_spec_string ("name",
251 P_("Theming engine name"),
253 G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));
255 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
259 gtk_theming_engine_init (GtkThemingEngine *engine)
261 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
265 gtk_theming_engine_finalize (GObject *object)
267 GtkThemingEnginePrivate *priv;
269 priv = GTK_THEMING_ENGINE (object)->priv;
272 G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
276 gtk_theming_engine_impl_set_property (GObject *object,
281 GtkThemingEnginePrivate *priv;
283 priv = GTK_THEMING_ENGINE (object)->priv;
291 priv->name = g_value_dup_string (value);
294 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
300 gtk_theming_engine_impl_get_property (GObject *object,
305 GtkThemingEnginePrivate *priv;
307 priv = GTK_THEMING_ENGINE (object)->priv;
312 g_value_set_string (value, priv->name);
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
322 GtkStyleContext *context)
324 GtkThemingEnginePrivate *priv;
326 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
327 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
330 priv->context = context;
334 _gtk_theming_engine_peek_property (GtkThemingEngine *engine,
335 const char *property_name)
337 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
338 g_return_val_if_fail (property_name != NULL, NULL);
340 return _gtk_style_context_peek_property (engine->priv->context, property_name);
344 * gtk_theming_engine_get_property:
345 * @engine: a #GtkThemingEngine
346 * @property: the property name
347 * @state: state to retrieve the value for
348 * @value: (out) (transfer full): return location for the property value,
349 * you must free this memory using g_value_unset() once you are
352 * Gets a property value as retrieved from the style settings that apply
353 * to the currently rendered element.
358 gtk_theming_engine_get_property (GtkThemingEngine *engine,
359 const gchar *property,
363 GtkThemingEnginePrivate *priv;
365 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
366 g_return_if_fail (property != NULL);
367 g_return_if_fail (value != NULL);
370 gtk_style_context_get_property (priv->context, property, state, value);
374 * gtk_theming_engine_get_valist:
375 * @engine: a #GtkThemingEngine
376 * @state: state to retrieve values for
377 * @args: va_list of property name/return location pairs, followed by %NULL
379 * Retrieves several style property values that apply to the currently
385 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
389 GtkThemingEnginePrivate *priv;
391 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
394 gtk_style_context_get_valist (priv->context, state, args);
398 * gtk_theming_engine_get:
399 * @engine: a #GtkThemingEngine
400 * @state: state to retrieve values for
401 * @...: property name /return value pairs, followed by %NULL
403 * Retrieves several style property values that apply to the currently
409 gtk_theming_engine_get (GtkThemingEngine *engine,
413 GtkThemingEnginePrivate *priv;
416 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
420 va_start (args, state);
421 gtk_style_context_get_valist (priv->context, state, args);
426 * gtk_theming_engine_get_style_property:
427 * @engine: a #GtkThemingEngine
428 * @property_name: the name of the widget style property
429 * @value: Return location for the property value, free with
430 * g_value_unset() after use.
432 * Gets the value for a widget style property.
437 gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
438 const gchar *property_name,
441 GtkThemingEnginePrivate *priv;
443 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
444 g_return_if_fail (property_name != NULL);
447 gtk_style_context_get_style_property (priv->context, property_name, value);
451 * gtk_theming_engine_get_style_valist:
452 * @engine: a #GtkThemingEngine
453 * @args: va_list of property name/return location pairs, followed by %NULL
455 * Retrieves several widget style properties from @engine according to the
456 * currently rendered content's style.
461 gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
464 GtkThemingEnginePrivate *priv;
466 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
469 gtk_style_context_get_style_valist (priv->context, args);
473 * gtk_theming_engine_get_style:
474 * @engine: a #GtkThemingEngine
475 * @...: property name /return value pairs, followed by %NULL
477 * Retrieves several widget style properties from @engine according
478 * to the currently rendered content's style.
483 gtk_theming_engine_get_style (GtkThemingEngine *engine,
486 GtkThemingEnginePrivate *priv;
489 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
493 va_start (args, engine);
494 gtk_style_context_get_style_valist (priv->context, args);
499 * gtk_theming_engine_lookup_color:
500 * @engine: a #GtkThemingEngine
501 * @color_name: color name to lookup
502 * @color: (out): Return location for the looked up color
504 * Looks up and resolves a color name in the current style's color map.
506 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
509 gtk_theming_engine_lookup_color (GtkThemingEngine *engine,
510 const gchar *color_name,
513 GtkThemingEnginePrivate *priv;
515 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
516 g_return_val_if_fail (color_name != NULL, FALSE);
519 return gtk_style_context_lookup_color (priv->context, color_name, color);
523 * gtk_theming_engine_get_state:
524 * @engine: a #GtkThemingEngine
526 * returns the state used when rendering.
528 * Returns: the state flags
533 gtk_theming_engine_get_state (GtkThemingEngine *engine)
535 GtkThemingEnginePrivate *priv;
537 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
540 return gtk_style_context_get_state (priv->context);
544 * gtk_theming_engine_state_is_running:
545 * @engine: a #GtkThemingEngine
546 * @state: a widget state
547 * @progress: (out): return location for the transition progress
549 * Returns %TRUE if there is a transition animation running for the
550 * current region (see gtk_style_context_push_animatable_region()).
552 * If @progress is not %NULL, the animation progress will be returned
553 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
554 * it's closest to being %TRUE. This means transition animations will
555 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
556 * it's being set to %FALSE.
558 * Returns: %TRUE if there is a running transition animation for @state.
563 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
567 GtkThemingEnginePrivate *priv;
569 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
572 return gtk_style_context_state_is_running (priv->context, state, progress);
576 * gtk_theming_engine_get_path:
577 * @engine: a #GtkThemingEngine
579 * Returns the widget path used for style matching.
581 * Returns: (transfer none): A #GtkWidgetPath
585 const GtkWidgetPath *
586 gtk_theming_engine_get_path (GtkThemingEngine *engine)
588 GtkThemingEnginePrivate *priv;
590 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
593 return gtk_style_context_get_path (priv->context);
597 * gtk_theming_engine_has_class:
598 * @engine: a #GtkThemingEngine
599 * @style_class: class name to look up
601 * Returns %TRUE if the currently rendered contents have
602 * defined the given class name.
604 * Returns: %TRUE if @engine has @class_name defined
609 gtk_theming_engine_has_class (GtkThemingEngine *engine,
610 const gchar *style_class)
612 GtkThemingEnginePrivate *priv;
614 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
617 return gtk_style_context_has_class (priv->context, style_class);
621 * gtk_theming_engine_has_region:
622 * @engine: a #GtkThemingEngine
623 * @style_region: a region name
624 * @flags: (out) (allow-none): return location for region flags
626 * Returns %TRUE if the currently rendered contents have the
627 * region defined. If @flags_return is not %NULL, it is set
628 * to the flags affecting the region.
630 * Returns: %TRUE if region is defined
635 gtk_theming_engine_has_region (GtkThemingEngine *engine,
636 const gchar *style_region,
637 GtkRegionFlags *flags)
639 GtkThemingEnginePrivate *priv;
644 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
647 return gtk_style_context_has_region (priv->context, style_region, flags);
651 * gtk_theming_engine_get_direction:
652 * @engine: a #GtkThemingEngine
654 * Returns the widget direction used for rendering.
656 * Returns: the widget direction
661 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
663 GtkThemingEnginePrivate *priv;
665 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
668 return gtk_style_context_get_direction (priv->context);
672 * gtk_theming_engine_get_junction_sides:
673 * @engine: a #GtkThemingEngine
675 * Returns the widget direction used for rendering.
677 * Returns: the widget direction
682 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
684 GtkThemingEnginePrivate *priv;
686 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
689 return gtk_style_context_get_junction_sides (priv->context);
693 * gtk_theming_engine_get_color:
694 * @engine: a #GtkThemingEngine
695 * @state: state to retrieve the color for
696 * @color: (out): return value for the foreground color
698 * Gets the foreground color for a given state.
703 gtk_theming_engine_get_color (GtkThemingEngine *engine,
707 GtkThemingEnginePrivate *priv;
709 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
712 gtk_style_context_get_color (priv->context, state, color);
716 * gtk_theming_engine_get_background_color:
717 * @engine: a #GtkThemingEngine
718 * @state: state to retrieve the color for
719 * @color: (out): return value for the background color
721 * Gets the background color for a given state.
726 gtk_theming_engine_get_background_color (GtkThemingEngine *engine,
730 GtkThemingEnginePrivate *priv;
732 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
735 gtk_style_context_get_background_color (priv->context, state, color);
739 * gtk_theming_engine_get_border_color:
740 * @engine: a #GtkThemingEngine
741 * @state: state to retrieve the color for
742 * @color: (out): return value for the border color
744 * Gets the border color for a given state.
749 gtk_theming_engine_get_border_color (GtkThemingEngine *engine,
753 GtkThemingEnginePrivate *priv;
755 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
758 gtk_style_context_get_border_color (priv->context, state, color);
762 * gtk_theming_engine_get_border:
763 * @engine: a #GtkthemingEngine
764 * @state: state to retrieve the border for
765 * @border: (out): return value for the border settings
767 * Gets the border for a given state as a #GtkBorder.
772 gtk_theming_engine_get_border (GtkThemingEngine *engine,
776 GtkThemingEnginePrivate *priv;
778 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
781 gtk_style_context_get_border (priv->context, state, border);
785 * gtk_theming_engine_get_padding:
786 * @engine: a #GtkthemingEngine
787 * @state: state to retrieve the padding for
788 * @padding: (out): return value for the padding settings
790 * Gets the padding for a given state as a #GtkBorder.
795 gtk_theming_engine_get_padding (GtkThemingEngine *engine,
799 GtkThemingEnginePrivate *priv;
801 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
804 gtk_style_context_get_padding (priv->context, state, padding);
808 * gtk_theming_engine_get_margin:
809 * @engine: a #GtkThemingEngine
810 * @state: state to retrieve the border for
811 * @margin: (out): return value for the margin settings
813 * Gets the margin for a given state as a #GtkBorder.
818 gtk_theming_engine_get_margin (GtkThemingEngine *engine,
822 GtkThemingEnginePrivate *priv;
824 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
827 gtk_style_context_get_margin (priv->context, state, margin);
831 * gtk_theming_engine_get_font:
832 * @engine: a #GtkThemingEngine
833 * @state: state to retrieve the font for
835 * Returns the font description for a given state.
837 * Returns: (transfer none): the #PangoFontDescription for the given
838 * state. This object is owned by GTK+ and should not be
843 const PangoFontDescription *
844 gtk_theming_engine_get_font (GtkThemingEngine *engine,
847 GtkThemingEnginePrivate *priv;
849 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
852 return gtk_style_context_get_font (priv->context, state);
855 /* GtkThemingModule */
858 gtk_theming_module_load (GTypeModule *type_module)
860 GtkThemingModule *theming_module;
862 gchar *name, *module_path;
864 theming_module = GTK_THEMING_MODULE (type_module);
865 name = theming_module->name;
866 module_path = _gtk_find_module (name, "theming-engines");
871 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
872 g_free (module_path);
877 if (!g_module_symbol (module, "theme_init",
878 (gpointer *) &theming_module->init) ||
879 !g_module_symbol (module, "theme_exit",
880 (gpointer *) &theming_module->exit) ||
881 !g_module_symbol (module, "create_engine",
882 (gpointer *) &theming_module->create_engine))
884 g_module_close (module);
889 theming_module->module = module;
891 theming_module->init (G_TYPE_MODULE (theming_module));
897 gtk_theming_module_unload (GTypeModule *type_module)
899 GtkThemingModule *theming_module;
901 theming_module = GTK_THEMING_MODULE (type_module);
903 theming_module->exit ();
905 g_module_close (theming_module->module);
907 theming_module->module = NULL;
908 theming_module->init = NULL;
909 theming_module->exit = NULL;
910 theming_module->create_engine = NULL;
914 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
916 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
918 module_class->load = gtk_theming_module_load;
919 module_class->unload = gtk_theming_module_unload;
923 gtk_theming_module_init (GtkThemingModule *module)
928 * gtk_theming_engine_load:
929 * @name: Theme engine name to load
931 * Loads and initializes a theming engine module from the
932 * standard directories.
934 * Returns: (transfer none): A theming engine, or %NULL if
935 * the engine @name doesn't exist.
938 gtk_theming_engine_load (const gchar *name)
940 static GHashTable *engines = NULL;
941 static GtkThemingEngine *default_engine;
942 GtkThemingEngine *engine = NULL;
947 engines = g_hash_table_new (g_str_hash, g_str_equal);
949 engine = g_hash_table_lookup (engines, name);
953 GtkThemingModule *module;
955 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
956 g_type_module_set_name (G_TYPE_MODULE (module), name);
957 module->name = g_strdup (name);
959 if (module && g_type_module_use (G_TYPE_MODULE (module)))
961 engine = (module->create_engine) ();
964 g_hash_table_insert (engines, module->name, engine);
970 if (G_UNLIKELY (!default_engine))
971 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
973 engine = default_engine;
980 * gtk_theming_engine_get_screen:
981 * @engine: a #GtkThemingEngine
983 * Returns the #GdkScreen to which @engine currently rendering to.
985 * Returns: (transfer none): a #GdkScreen, or %NULL.
988 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
990 GtkThemingEnginePrivate *priv;
992 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
995 return gtk_style_context_get_screen (priv->context);
998 /* Paint method implementations */
1000 gtk_theming_engine_render_check (GtkThemingEngine *engine,
1007 GdkRGBA fg_color, bg_color;
1008 GtkStateFlags flags;
1009 gint exterior_size, interior_size, thickness, pad;
1010 GtkBorderStyle border_style;
1013 GtkThemingBackground bg;
1015 _gtk_theming_background_init (&bg, engine,
1018 gtk_theming_engine_get_junction_sides (engine));
1020 if (_gtk_theming_background_has_background_image (&bg))
1022 _gtk_theming_background_render (&bg, cr);
1026 flags = gtk_theming_engine_get_state (engine);
1029 gtk_theming_engine_get_color (engine, flags, &fg_color);
1030 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1031 gtk_theming_engine_get_border (engine, flags, &border);
1033 gtk_theming_engine_get (engine, flags,
1034 "border-style", &border_style,
1037 border_width = MIN (MIN (border.top, border.bottom),
1038 MIN (border.left, border.right));
1039 exterior_size = MIN (width, height);
1041 if (exterior_size % 2 == 0) /* Ensure odd */
1044 /* FIXME: thickness */
1046 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1047 interior_size = MAX (1, exterior_size - 2 * pad);
1049 if (interior_size < 7)
1052 pad = MAX (0, (exterior_size - interior_size) / 2);
1055 x -= (1 + exterior_size - (gint) width) / 2;
1056 y -= (1 + exterior_size - (gint) height) / 2;
1058 if (border_style == GTK_BORDER_STYLE_SOLID)
1060 GdkRGBA border_color;
1062 cairo_set_line_width (cr, border_width);
1063 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1065 cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
1066 gdk_cairo_set_source_rgba (cr, &bg_color);
1067 cairo_fill_preserve (cr);
1069 gdk_cairo_set_source_rgba (cr, &border_color);
1073 gdk_cairo_set_source_rgba (cr, &fg_color);
1075 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1077 int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1079 cairo_rectangle (cr,
1081 y + pad + (1 + interior_size - line_thickness) / 2,
1091 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1093 if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
1098 cairo_translate (cr,
1101 cairo_scale (cr, interior_size / 7., interior_size / 7.);
1103 cairo_rectangle (cr, 0, 0, 7 * progress, 7);
1106 cairo_move_to (cr, 7.0, 0.0);
1107 cairo_line_to (cr, 7.5, 1.0);
1108 cairo_curve_to (cr, 5.3, 2.0,
1111 cairo_curve_to (cr, 3.0, 5.7,
1114 cairo_line_to (cr, 0.2, 3.5);
1115 cairo_curve_to (cr, 1.1, 3.5,
1118 cairo_curve_to (cr, 1.0, 3.9,
1121 cairo_curve_to (cr, 3.5, 3.1,
1133 gtk_theming_engine_render_option (GtkThemingEngine *engine,
1140 GtkStateFlags flags;
1141 GdkRGBA fg_color, bg_color;
1142 gint exterior_size, interior_size, pad, thickness, border_width;
1143 GtkBorderStyle border_style;
1145 GtkThemingBackground bg;
1147 _gtk_theming_background_init (&bg, engine,
1150 gtk_theming_engine_get_junction_sides (engine));
1152 if (_gtk_theming_background_has_background_image (&bg))
1154 _gtk_theming_background_render (&bg, cr);
1158 flags = gtk_theming_engine_get_state (engine);
1162 gtk_theming_engine_get_color (engine, flags, &fg_color);
1163 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
1164 gtk_theming_engine_get_border (engine, flags, &border);
1166 gtk_theming_engine_get (engine, flags,
1167 "border-style", &border_style,
1170 exterior_size = MIN (width, height);
1171 border_width = MIN (MIN (border.top, border.bottom),
1172 MIN (border.left, border.right));
1174 if (exterior_size % 2 == 0) /* Ensure odd */
1177 x -= (1 + exterior_size - width) / 2;
1178 y -= (1 + exterior_size - height) / 2;
1180 if (border_style == GTK_BORDER_STYLE_SOLID)
1182 GdkRGBA border_color;
1184 cairo_set_line_width (cr, border_width);
1185 gtk_theming_engine_get_border_color (engine, flags, &border_color);
1187 cairo_new_sub_path (cr);
1189 x + exterior_size / 2.,
1190 y + exterior_size / 2.,
1191 (exterior_size - 1) / 2.,
1194 gdk_cairo_set_source_rgba (cr, &bg_color);
1195 cairo_fill_preserve (cr);
1197 gdk_cairo_set_source_rgba (cr, &border_color);
1201 gdk_cairo_set_source_rgba (cr, &fg_color);
1203 /* FIXME: thickness */
1206 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1208 gint line_thickness;
1210 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1211 interior_size = MAX (1, exterior_size - 2 * pad);
1213 if (interior_size < 7)
1216 pad = MAX (0, (exterior_size - interior_size) / 2);
1219 line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1221 cairo_rectangle (cr,
1223 y + pad + (interior_size - line_thickness) / 2.,
1228 if (flags & GTK_STATE_FLAG_ACTIVE)
1230 pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1231 interior_size = MAX (1, exterior_size - 2 * pad);
1233 if (interior_size < 5)
1236 pad = MAX (0, (exterior_size - interior_size) / 2);
1239 cairo_new_sub_path (cr);
1241 x + pad + interior_size / 2.,
1242 y + pad + interior_size / 2.,
1252 add_path_arrow (cairo_t *cr,
1260 cairo_translate (cr, x + (size / 2), y + (size / 2));
1261 cairo_rotate (cr, angle);
1263 cairo_move_to (cr, 0, - (size / 4));
1264 cairo_line_to (cr, - (size / 2), (size / 4));
1265 cairo_line_to (cr, (size / 2), (size / 4));
1266 cairo_close_path (cr);
1272 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1279 GtkStateFlags flags;
1284 flags = gtk_theming_engine_get_state (engine);
1285 gtk_theming_engine_get_color (engine, flags, &fg_color);
1287 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1289 add_path_arrow (cr, angle, x + 1, y + 1, size);
1290 cairo_set_source_rgb (cr, 1, 1, 1);
1294 add_path_arrow (cr, angle, x, y, size);
1295 gdk_cairo_set_source_rgba (cr, &fg_color);
1302 add_path_line (cairo_t *cr,
1308 /* Adjust endpoints */
1322 cairo_move_to (cr, x1, y1);
1323 cairo_line_to (cr, x2, y2);
1327 color_shade (const GdkRGBA *color,
1329 GdkRGBA *color_return)
1331 GtkSymbolicColor *literal, *shade;
1333 literal = gtk_symbolic_color_new_literal (color);
1334 shade = gtk_symbolic_color_new_shade (literal, factor);
1335 gtk_symbolic_color_unref (literal);
1337 gtk_symbolic_color_resolve (shade, NULL, color_return);
1338 gtk_symbolic_color_unref (shade);
1342 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1349 GtkThemingBackground bg;
1351 _gtk_theming_background_init (&bg, engine,
1354 gtk_theming_engine_get_junction_sides (engine));
1356 _gtk_theming_background_render (&bg, cr);
1360 gtk_theming_engine_hide_border_sides (GtkBorder *border,
1363 if (hidden_side & (1 << GTK_CSS_TOP))
1365 if (hidden_side & (1 << GTK_CSS_RIGHT))
1367 if (hidden_side & (1 << GTK_CSS_BOTTOM))
1369 if (hidden_side & (1 << GTK_CSS_LEFT))
1374 render_frame_fill (cairo_t *cr,
1375 GtkRoundedBox *border_box,
1380 GtkRoundedBox padding_box;
1383 padding_box = *border_box;
1384 _gtk_rounded_box_shrink (&padding_box, border->top, border->right, border->bottom, border->left);
1386 if (hidden_side == 0 &&
1387 gdk_rgba_equal (&colors[0], &colors[1]) &&
1388 gdk_rgba_equal (&colors[0], &colors[2]) &&
1389 gdk_rgba_equal (&colors[0], &colors[3]))
1391 gdk_cairo_set_source_rgba (cr, &colors[0]);
1393 _gtk_rounded_box_path (border_box, cr);
1394 _gtk_rounded_box_path (&padding_box, cr);
1399 for (i = 0; i < 4; i++)
1401 if (hidden_side & (1 << i))
1404 for (j = 0; j < 4; j++)
1406 if (hidden_side & (1 << j))
1410 (gdk_rgba_equal (&colors[i], &colors[j])))
1412 /* We were already painted when i == j */
1417 _gtk_rounded_box_path_top (border_box, &padding_box, cr);
1419 _gtk_rounded_box_path_right (border_box, &padding_box, cr);
1421 _gtk_rounded_box_path_bottom (border_box, &padding_box, cr);
1423 _gtk_rounded_box_path_left (border_box, &padding_box, cr);
1426 /* We were already painted when i == j */
1430 gdk_cairo_set_source_rgba (cr, &colors[i]);
1438 set_stroke_style (cairo_t *cr,
1440 GtkBorderStyle style,
1446 cairo_set_line_width (cr, line_width);
1448 if (style == GTK_BORDER_STYLE_DOTTED)
1450 n = round (0.5 * length / line_width);
1453 segments[1] = n ? length / n : 2;
1454 cairo_set_dash (cr, segments, G_N_ELEMENTS (segments), 0);
1456 cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
1457 cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
1461 n = length / line_width;
1462 /* Optimize the common case of an integer-sized rectangle
1463 * Again, we care about focus rectangles.
1465 if (n == nearbyint (n))
1472 n = round ((1. / 3) * n);
1474 segments[0] = n ? (1. / 3) * length / n : 1;
1475 segments[1] = 2 * segments[1];
1477 cairo_set_dash (cr, segments, G_N_ELEMENTS (segments), 0);
1479 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1480 cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
1485 get_border_side (GtkBorder *border,
1493 return border->right;
1494 case GTK_CSS_BOTTOM:
1495 return border->bottom;
1497 return border->left;
1499 g_assert_not_reached ();
1505 render_frame_stroke (cairo_t *cr,
1506 GtkRoundedBox *border_box,
1510 GtkBorderStyle stroke_style)
1512 gboolean different_colors, different_borders;
1513 GtkRoundedBox stroke_box;
1516 different_colors = !gdk_rgba_equal (&colors[0], &colors[1]) ||
1517 !gdk_rgba_equal (&colors[0], &colors[2]) ||
1518 !gdk_rgba_equal (&colors[0], &colors[3]);
1519 different_borders = border->top != border->right ||
1520 border->top != border->bottom ||
1521 border->top != border->left;
1523 stroke_box = *border_box;
1524 _gtk_rounded_box_shrink (&stroke_box,
1526 border->right / 2.0,
1527 border->bottom / 2.0,
1528 border->left / 2.0);
1530 if (!different_colors && !different_borders && hidden_side == 0)
1535 * Mostly expected to trigger for focus rectangles */
1536 for (i = 0; i < 4; i++)
1538 length += _gtk_rounded_box_guess_length (&stroke_box, i);
1539 _gtk_rounded_box_path_side (&stroke_box, cr, i);
1542 gdk_cairo_set_source_rgba (cr, &colors[0]);
1543 set_stroke_style (cr, border->top, stroke_style, length);
1548 GtkRoundedBox padding_box;
1550 padding_box = *border_box;
1551 _gtk_rounded_box_path (&padding_box, cr);
1552 _gtk_rounded_box_shrink (&padding_box,
1558 for (i = 0; i < 4; i++)
1560 if (hidden_side & (1 << i))
1566 _gtk_rounded_box_path_top (border_box, &padding_box, cr);
1568 _gtk_rounded_box_path_right (border_box, &padding_box, cr);
1570 _gtk_rounded_box_path_bottom (border_box, &padding_box, cr);
1572 _gtk_rounded_box_path_left (border_box, &padding_box, cr);
1575 _gtk_rounded_box_path_side (&stroke_box, cr, i);
1577 gdk_cairo_set_source_rgba (cr, &colors[i]);
1578 set_stroke_style (cr,
1579 get_border_side (border, i),
1581 _gtk_rounded_box_guess_length (&stroke_box, i));
1590 render_border (cairo_t *cr,
1591 GtkRoundedBox *border_box,
1595 GtkBorderStyle border_style[4])
1601 cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
1603 for (i = 0; i < 4; i++)
1605 if (hidden_side & (1 << i))
1608 switch (border_style[i])
1610 case GTK_BORDER_STYLE_NONE:
1611 case GTK_BORDER_STYLE_HIDDEN:
1612 case GTK_BORDER_STYLE_SOLID:
1614 case GTK_BORDER_STYLE_INSET:
1615 if (i == 1 || i == 2)
1616 color_shade (&colors[i], 1.8, &colors[i]);
1618 case GTK_BORDER_STYLE_OUTSET:
1619 if (i == 0 || i == 3)
1620 color_shade (&colors[i], 1.8, &colors[i]);
1622 case GTK_BORDER_STYLE_DOTTED:
1623 case GTK_BORDER_STYLE_DASHED:
1625 guint dont_draw = hidden_side;
1627 for (j = 0; j < 4; j++)
1629 if (border_style[j] == border_style[i])
1630 hidden_side |= (1 << j);
1632 dont_draw |= (1 << j);
1635 render_frame_stroke (cr, border_box, border, colors, dont_draw, border_style[i]);
1638 case GTK_BORDER_STYLE_DOUBLE:
1640 GtkRoundedBox other_box;
1641 GtkBorder other_border;
1642 guint dont_draw = hidden_side;
1644 for (j = 0; j < 4; j++)
1646 if (border_style[j] == GTK_BORDER_STYLE_DOUBLE)
1647 hidden_side |= (1 << j);
1649 dont_draw |= (1 << j);
1651 other_border.top = (border->top + 2) / 3;
1652 other_border.right = (border->right + 2) / 3;
1653 other_border.bottom = (border->bottom + 2) / 3;
1654 other_border.left = (border->left + 2) / 3;
1656 render_frame_fill (cr, border_box, &other_border, colors, dont_draw);
1658 other_box = *border_box;
1659 _gtk_rounded_box_shrink (&other_box,
1660 border->top - other_border.top,
1661 border->right - other_border.right,
1662 border->bottom - other_border.bottom,
1663 border->left - other_border.left);
1664 render_frame_fill (cr, &other_box, &other_border, colors, dont_draw);
1666 case GTK_BORDER_STYLE_GROOVE:
1667 case GTK_BORDER_STYLE_RIDGE:
1669 GtkRoundedBox other_box;
1670 GdkRGBA other_colors[4];
1671 guint dont_draw = hidden_side;
1672 GtkBorder other_border;
1674 for (j = 0; j < 4; j++)
1676 other_colors[j] = colors[j];
1677 if ((j == 0 || j == 3) ^ (border_style[j] == GTK_BORDER_STYLE_RIDGE))
1678 color_shade (&other_colors[j], 1.8, &other_colors[j]);
1680 color_shade (&colors[j], 1.8, &colors[j]);
1681 if (border_style[j] == GTK_BORDER_STYLE_GROOVE ||
1682 border_style[j] == GTK_BORDER_STYLE_RIDGE)
1683 hidden_side |= (1 << j);
1685 dont_draw |= (1 << j);
1687 other_border.top = border->top / 2;
1688 other_border.right = border->right / 2;
1689 other_border.bottom = border->bottom / 2;
1690 other_border.left = border->left / 2;
1692 render_frame_fill (cr, border_box, &other_border, colors, dont_draw);
1694 other_box = *border_box;
1695 _gtk_rounded_box_shrink (&other_box,
1696 other_border.top, other_border.right,
1697 other_border.bottom, other_border.left);
1698 other_border.top = border->top - other_border.top;
1699 other_border.right = border->right - other_border.right;
1700 other_border.bottom = border->bottom - other_border.bottom;
1701 other_border.left = border->left - other_border.left;
1702 render_frame_fill (cr, &other_box, &other_border, other_colors, dont_draw);
1706 g_assert_not_reached ();
1711 render_frame_fill (cr, border_box, border, colors, hidden_side);
1717 render_frame_internal (GtkThemingEngine *engine,
1724 GtkJunctionSides junction)
1726 GtkStateFlags state;
1727 GtkBorderStyle border_style[4];
1728 GtkRoundedBox border_box;
1732 GdkRGBA *alloc_colors[4];
1736 state = gtk_theming_engine_get_state (engine);
1738 gtk_theming_engine_get_border (engine, state, &border);
1739 gtk_theming_engine_hide_border_sides (&border, hidden_side);
1741 gtk_theming_engine_get (engine, state,
1742 "border-top-style", &border_style[0],
1743 "border-right-style", &border_style[1],
1744 "border-bottom-style", &border_style[2],
1745 "border-left-style", &border_style[3],
1746 "border-top-color", &alloc_colors[0],
1747 "border-right-color", &alloc_colors[1],
1748 "border-bottom-color", &alloc_colors[2],
1749 "border-left-color", &alloc_colors[3],
1752 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1756 GtkStateFlags other_state;
1757 GdkRGBA *other_colors[4];
1759 if (state & GTK_STATE_FLAG_PRELIGHT)
1761 other_state = state & ~(GTK_STATE_FLAG_PRELIGHT);
1762 progress = 1 - progress;
1765 other_state = state | GTK_STATE_FLAG_PRELIGHT;
1767 gtk_theming_engine_get (engine, other_state,
1768 "border-top-color", &other_colors[0],
1769 "border-right-color", &other_colors[1],
1770 "border-bottom-color", &other_colors[2],
1771 "border-left-color", &other_colors[3],
1774 for (i = 0; i < 4; i++)
1776 colors[i].red = CLAMP (alloc_colors[i]->red + ((other_colors[i]->red - alloc_colors[i]->red) * progress), 0, 1);
1777 colors[i].green = CLAMP (alloc_colors[i]->green + ((other_colors[i]->green - alloc_colors[i]->green) * progress), 0, 1);
1778 colors[i].blue = CLAMP (alloc_colors[i]->blue + ((other_colors[i]->blue - alloc_colors[i]->blue) * progress), 0, 1);
1779 colors[i].alpha = CLAMP (alloc_colors[i]->alpha + ((other_colors[i]->alpha - alloc_colors[i]->alpha) * progress), 0, 1);
1780 gdk_rgba_free (other_colors[i]);
1781 gdk_rgba_free (alloc_colors[i]);
1786 for (i = 0; i < 4; i++)
1788 colors[i] = *alloc_colors[i];
1789 gdk_rgba_free (alloc_colors[i]);
1793 _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1794 _gtk_rounded_box_apply_border_radius (&border_box, engine, state, junction);
1796 render_border (cr, &border_box, &border, hidden_side, colors, border_style);
1798 border_style[0] = g_value_get_enum (_gtk_theming_engine_peek_property (engine, "outline-style"));
1799 if (border_style[0] != GTK_BORDER_STYLE_NONE)
1803 border_style[1] = border_style[2] = border_style[3] = border_style[0];
1804 border.top = g_value_get_int (_gtk_theming_engine_peek_property (engine, "outline-width"));
1805 border.left = border.right = border.bottom = border.top;
1806 colors[0] = *(GdkRGBA *) g_value_get_boxed (_gtk_theming_engine_peek_property (engine, "outline-color"));
1807 colors[3] = colors[2] = colors[1] = colors[0];
1808 offset = g_value_get_int (_gtk_theming_engine_peek_property (engine, "outline-offset"));
1810 /* reinit box here - outlines don't have a border radius */
1811 _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
1812 _gtk_rounded_box_shrink (&border_box,
1813 - border.top - offset,
1814 - border.right - offset,
1815 - border.left - offset,
1816 - border.bottom - offset);
1818 render_border (cr, &border_box, &border, hidden_side, colors, border_style);
1823 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1830 GtkStateFlags flags;
1831 GtkJunctionSides junction;
1832 GtkBorderImage border_image;
1835 flags = gtk_theming_engine_get_state (engine);
1836 junction = gtk_theming_engine_get_junction_sides (engine);
1837 gtk_theming_engine_get_border (engine, flags, &border);
1839 if (_gtk_border_image_init (&border_image, engine))
1840 _gtk_border_image_render (&border_image, &border,
1841 cr, x, y, width, height);
1843 render_frame_internal (engine, cr,
1844 x, y, width, height,
1849 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1856 GtkStateFlags flags;
1857 GdkRGBA outline_color, fg_color;
1858 double vertical_overshoot;
1861 double interp; /* interpolation factor for center position */
1862 double x_double_horz, y_double_horz;
1863 double x_double_vert, y_double_vert;
1864 double x_double, y_double;
1867 gboolean running, is_rtl;
1871 flags = gtk_theming_engine_get_state (engine);
1873 gtk_theming_engine_get_color (engine, flags, &fg_color);
1874 gtk_theming_engine_get_border_color (engine, flags, &outline_color);
1876 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
1877 is_rtl = (gtk_theming_engine_get_direction (engine) == GTK_TEXT_DIR_RTL);
1881 progress = (flags & GTK_STATE_FLAG_ACTIVE) ? 1 : 0;
1883 if (!gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_HORIZONTAL))
1886 angle = (G_PI) - ((G_PI / 2) * progress);
1888 angle = (G_PI / 2) * progress;
1893 angle = (G_PI / 2) + ((G_PI / 2) * progress);
1895 angle = (G_PI / 2) - ((G_PI / 2) * progress);
1900 /* Compute distance that the stroke extends beyonds the end
1901 * of the triangle we draw.
1903 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1905 /* For odd line widths, we end the vertical line of the triangle
1906 * at a half pixel, so we round differently.
1908 if (line_width % 2 == 1)
1909 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1911 vertical_overshoot = ceil (vertical_overshoot);
1913 /* Adjust the size of the triangle we draw so that the entire stroke fits
1915 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1917 /* If the line width is odd, we want the diameter to be even,
1918 * and vice versa, so force the sum to be odd. This relationship
1919 * makes the point of the triangle look right.
1921 diameter -= (1 - (diameter + line_width) % 2);
1923 radius = diameter / 2.;
1925 /* Adjust the center so that the stroke is properly aligned with
1926 * the pixel grid. The center adjustment is different for the
1927 * horizontal and vertical orientations. For intermediate positions
1928 * we interpolate between the two.
1930 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1931 y_double_vert = (y + height / 2) - 0.5;
1933 x_double_horz = (x + width / 2) - 0.5;
1934 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1936 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1937 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1939 cairo_translate (cr, x_double, y_double);
1940 cairo_rotate (cr, angle);
1942 cairo_move_to (cr, - radius / 2., - radius);
1943 cairo_line_to (cr, radius / 2., 0);
1944 cairo_line_to (cr, - radius / 2., radius);
1945 cairo_close_path (cr);
1947 cairo_set_line_width (cr, line_width);
1949 gdk_cairo_set_source_rgba (cr, &fg_color);
1951 cairo_fill_preserve (cr);
1953 gdk_cairo_set_source_rgba (cr, &outline_color);
1960 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1967 GtkStateFlags flags;
1973 flags = gtk_theming_engine_get_state (engine);
1975 gtk_theming_engine_get_color (engine, flags, &color);
1977 gtk_theming_engine_get_style (engine,
1978 "focus-line-width", &line_width,
1979 "focus-line-pattern", (gchar *) &dash_list,
1982 cairo_set_line_width (cr, (gdouble) line_width);
1986 gint n_dashes = strlen ((const gchar *) dash_list);
1987 gdouble *dashes = g_new (gdouble, n_dashes);
1988 gdouble total_length = 0;
1989 gdouble dash_offset;
1992 for (i = 0; i < n_dashes; i++)
1994 dashes[i] = dash_list[i];
1995 total_length += dash_list[i];
1998 /* The dash offset here aligns the pattern to integer pixels
1999 * by starting the dash at the right side of the left border
2000 * Negative dash offsets in cairo don't work
2001 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
2003 dash_offset = - line_width / 2.;
2005 while (dash_offset < 0)
2006 dash_offset += total_length;
2008 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
2012 cairo_rectangle (cr,
2013 x + line_width / 2.,
2014 y + line_width / 2.,
2016 height - line_width);
2018 gdk_cairo_set_source_rgba (cr, &color);
2027 gtk_theming_engine_render_line (GtkThemingEngine *engine,
2035 GtkStateFlags flags;
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 gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2191 GtkBorderImage border_image;
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 "border-top-left-radius", &top_left_radius,
2201 "border-top-right-radius", &top_right_radius,
2202 "border-bottom-right-radius", &bottom_right_radius,
2203 "border-bottom-left-radius", &bottom_left_radius,
2206 border_width = MIN (MIN (border.top, border.bottom),
2207 MIN (border.left, border.right));
2214 xc = x + xy0_gap + border_width;
2216 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2219 if (xy0_gap < top_left_radius->horizontal)
2220 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2222 if (xy1_gap > width - top_right_radius->horizontal)
2223 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2225 case GTK_POS_BOTTOM:
2226 xc = x + xy0_gap + border_width;
2227 yc = y + height - border_width;
2228 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2231 if (xy0_gap < bottom_left_radius->horizontal)
2232 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2234 if (xy1_gap > width - bottom_right_radius->horizontal)
2235 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2240 yc = y + xy0_gap + border_width;
2242 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2244 if (xy0_gap < top_left_radius->vertical)
2245 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2247 if (xy1_gap > height - bottom_left_radius->vertical)
2248 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2252 xc = x + width - border_width;
2253 yc = y + xy0_gap + border_width;
2255 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2257 if (xy0_gap < top_right_radius->vertical)
2258 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2260 if (xy1_gap > height - bottom_right_radius->vertical)
2261 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2266 cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2267 cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2268 cairo_rectangle (cr, x0, yc, xc - x0, hc);
2269 cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2270 cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2273 if (_gtk_border_image_init (&border_image, engine))
2274 _gtk_border_image_render (&border_image, &border,
2275 cr, x, y, width, height);
2277 render_frame_internal (engine, cr,
2278 x, y, width, height,
2283 g_free (top_left_radius);
2284 g_free (top_right_radius);
2285 g_free (bottom_right_radius);
2286 g_free (bottom_left_radius);
2290 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2296 GtkPositionType gap_side)
2298 GtkThemingBackground bg;
2299 GtkJunctionSides junction = 0;
2300 guint hidden_side = 0;
2307 junction = GTK_JUNCTION_LEFT;
2308 hidden_side = (1 << GTK_CSS_LEFT);
2310 cairo_translate (cr, x + width, y);
2311 cairo_rotate (cr, G_PI / 2);
2314 junction = GTK_JUNCTION_RIGHT;
2315 hidden_side = (1 << GTK_CSS_RIGHT);
2317 cairo_translate (cr, x, y + height);
2318 cairo_rotate (cr, - G_PI / 2);
2321 junction = GTK_JUNCTION_TOP;
2322 hidden_side = (1 << GTK_CSS_TOP);
2324 cairo_translate (cr, x + width, y + height);
2325 cairo_rotate (cr, G_PI);
2327 case GTK_POS_BOTTOM:
2328 junction = GTK_JUNCTION_BOTTOM;
2329 hidden_side = (1 << GTK_CSS_BOTTOM);
2331 cairo_translate (cr, x, y);
2335 if (gap_side == GTK_POS_TOP ||
2336 gap_side == GTK_POS_BOTTOM)
2337 _gtk_theming_background_init (&bg, engine,
2340 GTK_JUNCTION_BOTTOM);
2342 _gtk_theming_background_init (&bg, engine,
2345 GTK_JUNCTION_BOTTOM);
2347 _gtk_theming_background_render (&bg, cr);
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;
2402 GtkThemingBackground bg;
2406 flags = gtk_theming_engine_get_state (engine);
2408 cairo_set_line_width (cr, 1.0);
2409 sides = gtk_theming_engine_get_junction_sides (engine);
2410 gtk_theming_engine_get_background_color (engine, flags, &bg_color);
2412 color_shade (&bg_color, 0.7, &darker);
2413 color_shade (&bg_color, 1.3, &lighter);
2415 _gtk_theming_background_init (&bg, engine, x, y, width, height, sides);
2416 _gtk_theming_background_render (&bg, cr);
2418 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_GRIP))
2420 /* reduce confusing values to a meaningful state */
2421 if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2422 sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2424 if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2425 sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2428 sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2430 /* align drawing area to the connected side */
2431 if (sides == GTK_JUNCTION_LEFT)
2436 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2440 else if (height < width)
2443 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2445 /* make it square, aligning to bottom left */
2448 y += (height - width);
2451 else if (height < width)
2454 else if (sides == GTK_JUNCTION_RIGHT)
2456 /* aligning to right */
2459 x += (width - height);
2463 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2467 else if (height < width)
2469 x += (width - height);
2473 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2475 /* make it square, aligning to bottom right */
2478 y += (height - width);
2481 else if (height < width)
2483 x += (width - height);
2487 else if (sides == GTK_JUNCTION_TOP)
2492 else if (sides == GTK_JUNCTION_BOTTOM)
2494 /* align to bottom */
2497 y += (height - width);
2502 g_assert_not_reached ();
2504 if (sides == GTK_JUNCTION_LEFT ||
2505 sides == GTK_JUNCTION_RIGHT)
2511 while (xi < x + width)
2513 gdk_cairo_set_source_rgba (cr, &lighter);
2514 add_path_line (cr, x, y, x, y + height);
2518 gdk_cairo_set_source_rgba (cr, &darker);
2519 add_path_line (cr, xi, y, xi, y + height);
2524 else if (sides == GTK_JUNCTION_TOP ||
2525 sides == GTK_JUNCTION_BOTTOM)
2531 while (yi < y + height)
2533 gdk_cairo_set_source_rgba (cr, &lighter);
2534 add_path_line (cr, x, yi, x + width, yi);
2538 gdk_cairo_set_source_rgba (cr, &darker);
2539 add_path_line (cr, x, yi, x + width, yi);
2544 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2553 gdk_cairo_set_source_rgba (cr, &darker);
2554 add_path_line (cr, xi, y, x, yi);
2560 add_path_line (cr, xi, y, x, yi);
2566 gdk_cairo_set_source_rgba (cr, &lighter);
2567 add_path_line (cr, xi, y, x, yi);
2574 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2581 while (xi < (x + width - 3))
2583 gdk_cairo_set_source_rgba (cr, &lighter);
2584 add_path_line (cr, xi, y, x + width, yi);
2590 gdk_cairo_set_source_rgba (cr, &darker);
2591 add_path_line (cr, xi, y, x + width, yi);
2597 add_path_line (cr, xi, y, x + width, yi);
2604 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2613 gdk_cairo_set_source_rgba (cr, &darker);
2614 add_path_line (cr, x, yi, xi, y + height);
2620 add_path_line (cr, x, yi, xi, y + height);
2626 gdk_cairo_set_source_rgba (cr, &lighter);
2627 add_path_line (cr, x, yi, xi, y + height);
2634 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2641 while (xi < (x + width - 3))
2643 gdk_cairo_set_source_rgba (cr, &lighter);
2644 add_path_line (cr, xi, y + height, x + width, yi);
2650 gdk_cairo_set_source_rgba (cr, &darker);
2651 add_path_line (cr, xi, y + height, x + width, yi);
2657 add_path_line (cr, xi, y + height, x + width, yi);
2665 else if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_PANE_SEPARATOR))
2668 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2669 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2671 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2672 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2676 for (yy = y; yy < y + height; yy += 3)
2677 for (xx = x; xx < x + width; xx += 6)
2679 render_dot (cr, &lighter, &darker, xx, yy, 2);
2680 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2688 _gtk_theming_engine_paint_spinner (cairo_t *cr,
2693 guint num_steps, step;
2700 step = (guint) (progress * num_steps);
2706 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2707 cairo_set_line_width (cr, 2.0);
2709 half = num_steps / 2;
2711 for (i = 0; i < num_steps; i++)
2713 gint inset = 0.7 * radius;
2715 /* transparency is a function of time and intial value */
2716 gdouble t = 1.0 - (gdouble) ((i + step) % num_steps) / num_steps;
2717 gdouble xscale = - sin (i * G_PI / half);
2718 gdouble yscale = - cos (i * G_PI / half);
2720 cairo_set_source_rgba (cr,
2727 (radius - inset) * xscale,
2728 (radius - inset) * yscale);
2740 render_spinner (GtkThemingEngine *engine,
2747 GtkStateFlags state;
2753 state = gtk_theming_engine_get_state (engine);
2755 if (!gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2758 radius = MIN (width / 2, height / 2);
2760 gtk_theming_engine_get_color (engine, state, &color);
2761 gtk_theming_engine_get (engine, state,
2762 "icon-shadow", &shadow,
2766 cairo_translate (cr, x + width / 2, y + height / 2);
2770 _gtk_icon_shadow_paint_spinner (shadow, cr,
2773 _gtk_shadow_unref (shadow);
2776 _gtk_theming_engine_paint_spinner (cr,
2785 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2792 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_SPINNER))
2794 render_spinner (engine, cr, x, y, width, height);
2798 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2799 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2804 scale_or_ref (GdkPixbuf *src,
2808 if (width == gdk_pixbuf_get_width (src) &&
2809 height == gdk_pixbuf_get_height (src))
2810 return g_object_ref (src);
2812 return gdk_pixbuf_scale_simple (src,
2814 GDK_INTERP_BILINEAR);
2818 lookup_icon_size (GtkThemingEngine *engine,
2824 GtkSettings *settings;
2826 screen = gtk_theming_engine_get_screen (engine);
2827 settings = gtk_settings_get_for_screen (screen);
2829 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2833 colorshift_source (cairo_t *cr,
2836 cairo_pattern_t *source;
2841 source = cairo_pattern_reference (cairo_get_source (cr));
2843 cairo_set_source_rgb (cr, shift, shift, shift);
2844 cairo_set_operator (cr, CAIRO_OPERATOR_COLOR_DODGE);
2846 cairo_mask (cr, source);
2848 cairo_pattern_destroy (source);
2853 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2854 const GtkIconSource *source,
2859 GdkPixbuf *base_pixbuf;
2860 GtkStateFlags state;
2864 cairo_surface_t *surface;
2866 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2867 state = gtk_theming_engine_get_state (engine);
2869 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2871 if (size != (GtkIconSize) -1 &&
2872 !lookup_icon_size (engine, size, &width, &height))
2874 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2878 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2881 if (size != (GtkIconSize) -1 &&
2882 gtk_icon_source_get_size_wildcarded (source))
2883 scaled = scale_or_ref (base_pixbuf, width, height);
2885 scaled = g_object_ref (base_pixbuf);
2887 /* If the state was wildcarded, then generate a state. */
2888 if (gtk_icon_source_get_state_wildcarded (source))
2890 if (state & GTK_STATE_FLAG_INSENSITIVE)
2892 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2893 gdk_pixbuf_get_width (scaled),
2894 gdk_pixbuf_get_height (scaled));
2895 cr = cairo_create (surface);
2896 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2897 cairo_paint_with_alpha (cr, 0.5);
2901 g_object_unref (scaled);
2902 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2903 cairo_image_surface_get_width (surface),
2904 cairo_image_surface_get_height (surface));
2905 cairo_surface_destroy (surface);
2907 else if (state & GTK_STATE_FLAG_PRELIGHT)
2909 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
2910 gdk_pixbuf_get_width (scaled),
2911 gdk_pixbuf_get_height (scaled));
2913 cr = cairo_create (surface);
2914 gdk_cairo_set_source_pixbuf (cr, scaled, 0, 0);
2915 colorshift_source (cr, 0.10);
2919 g_object_unref (scaled);
2920 stated = gdk_pixbuf_get_from_surface (surface, 0, 0,
2921 cairo_image_surface_get_width (surface),
2922 cairo_image_surface_get_height (surface));
2923 cairo_surface_destroy (surface);
2935 gtk_theming_engine_render_icon (GtkThemingEngine *engine,
2941 GtkStateFlags state;
2942 GtkShadow *icon_shadow;
2944 state = gtk_theming_engine_get_state (engine);
2948 gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y);
2950 gtk_theming_engine_get (engine, state,
2951 "icon-shadow", &icon_shadow,
2954 if (icon_shadow != NULL)
2956 _gtk_icon_shadow_paint (icon_shadow, cr);
2957 _gtk_shadow_unref (icon_shadow);