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 "gtk9slice.h"
34 * SECTION:gtkthemingengine
35 * @Short_description: Theming renderers
36 * @Title: GtkThemingEngine
37 * @See_also: #GtkStyleContext
39 * #GtkThemingEngine is the object used for rendering themed content
40 * in GTK+ widgets. Even though GTK+ has a default implementation,
41 * it can be overridden in CSS files by enforcing a #GtkThemingEngine
42 * object to be loaded as a module.
44 * In order to implement a theming engine, a #GtkThemingEngine subclass
45 * must be created, alongside the CSS file that will reference it, the
46 * theming engine would be created as an .so library, and installed in
47 * $(gtk-modules-dir)/theming-engines/.
49 * #GtkThemingEngine<!-- -->s have limited access to the object they are
50 * rendering, the #GtkThemingEngine API has read-only accessors to the
51 * style information contained in the rendered object's #GtkStyleContext.
54 typedef struct GtkThemingEnginePrivate GtkThemingEnginePrivate;
69 struct GtkThemingEnginePrivate
71 GtkStyleContext *context;
75 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
77 static void gtk_theming_engine_finalize (GObject *object);
78 static void gtk_theming_engine_impl_set_property (GObject *object,
82 static void gtk_theming_engine_impl_get_property (GObject *object,
87 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
93 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
99 static void gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
105 static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
111 static void gtk_theming_engine_render_frame (GtkThemingEngine *engine,
117 static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
123 static void gtk_theming_engine_render_focus (GtkThemingEngine *engine,
129 static void gtk_theming_engine_render_layout (GtkThemingEngine *engine,
133 PangoLayout *layout);
134 static void gtk_theming_engine_render_line (GtkThemingEngine *engine,
140 static void gtk_theming_engine_render_slider (GtkThemingEngine *engine,
146 GtkOrientation orientation);
147 static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
153 GtkPositionType gap_side,
156 static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
162 GtkPositionType gap_side);
163 static void gtk_theming_engine_render_handle (GtkThemingEngine *engine,
169 static void gtk_theming_engine_render_activity (GtkThemingEngine *engine,
175 static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
176 const GtkIconSource *source,
179 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
182 typedef struct GtkThemingModule GtkThemingModule;
183 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
185 struct GtkThemingModule
187 GTypeModule parent_instance;
191 void (*init) (GTypeModule *module);
193 GtkThemingEngine * (*create_engine) (void);
196 struct GtkThemingModuleClass
198 GTypeModuleClass parent_class;
201 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
202 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
203 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
205 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
208 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
210 GObjectClass *object_class = G_OBJECT_CLASS (klass);
212 object_class->finalize = gtk_theming_engine_finalize;
213 object_class->set_property = gtk_theming_engine_impl_set_property;
214 object_class->get_property = gtk_theming_engine_impl_get_property;
216 klass->render_check = gtk_theming_engine_render_check;
217 klass->render_option = gtk_theming_engine_render_option;
218 klass->render_arrow = gtk_theming_engine_render_arrow;
219 klass->render_background = gtk_theming_engine_render_background;
220 klass->render_frame = gtk_theming_engine_render_frame;
221 klass->render_expander = gtk_theming_engine_render_expander;
222 klass->render_focus = gtk_theming_engine_render_focus;
223 klass->render_layout = gtk_theming_engine_render_layout;
224 klass->render_line = gtk_theming_engine_render_line;
225 klass->render_slider = gtk_theming_engine_render_slider;
226 klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
227 klass->render_extension = gtk_theming_engine_render_extension;
228 klass->render_handle = gtk_theming_engine_render_handle;
229 klass->render_activity = gtk_theming_engine_render_activity;
230 klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
233 * GtkThemingEngine:name:
235 * The theming engine name, this name will be used when registering
236 * custom properties, for a theming engine named "Clearlooks" registering
237 * a "glossy" custom property, it could be referenced in the CSS file as
240 * -Clearlooks-glossy: true;
245 g_object_class_install_property (object_class,
247 g_param_spec_string ("name",
249 P_("Theming engine name"),
251 G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));
253 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
257 gtk_theming_engine_init (GtkThemingEngine *engine)
259 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
263 gtk_theming_engine_finalize (GObject *object)
265 GtkThemingEnginePrivate *priv;
267 priv = GTK_THEMING_ENGINE (object)->priv;
270 G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
274 gtk_theming_engine_impl_set_property (GObject *object,
279 GtkThemingEnginePrivate *priv;
281 priv = GTK_THEMING_ENGINE (object)->priv;
289 priv->name = g_value_dup_string (value);
292 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
298 gtk_theming_engine_impl_get_property (GObject *object,
303 GtkThemingEnginePrivate *priv;
305 priv = GTK_THEMING_ENGINE (object)->priv;
310 g_value_set_string (value, priv->name);
313 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
319 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
320 GtkStyleContext *context)
322 GtkThemingEnginePrivate *priv;
324 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
325 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
328 priv->context = context;
332 * gtk_theming_engine_register_property:
333 * @name_space: namespace for the property name
334 * @parse_func: parsing function to use, or %NULL
335 * @pspec: the #GParamSpec for the new property
337 * Registers a property so it can be used in the CSS file format,
338 * on the CSS file the property will look like
339 * "-${@name_space}-${property_name}". being
340 * ${property_name} the given to @pspec. @name_space will usually
341 * be the theme engine name.
343 * For any type a @parse_func may be provided, being this function
344 * used for turning any property value (between ':' and ';') in
345 * CSS to the #GValue needed. For basic types there is already
346 * builtin parsing support, so %NULL may be provided for these
350 * Engines must ensure property registration happens exactly once,
351 * usually GTK+ deals with theming engines as singletons, so this
352 * should be guaranteed to happen once, but bear this in mind
353 * when creating #GtkThemeEngine<!-- -->s yourself.
357 * In order to make use of the custom registered properties in
358 * the CSS file, make sure the engine is loaded first by specifying
359 * the engine property, either in a previous rule or within the same
363 * engine: someengine;
364 * -SomeEngine-custom-property: 2;
372 gtk_theming_engine_register_property (const gchar *name_space,
373 GtkStylePropertyParser parse_func,
378 g_return_if_fail (name_space != NULL);
379 g_return_if_fail (strchr (name_space, ' ') == NULL);
380 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
382 /* FIXME: hack hack hack, replacing pspec->name to include namespace */
383 name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
384 g_free (pspec->name);
387 gtk_style_properties_register_property (parse_func, pspec);
391 * gtk_theming_engine_get_property:
392 * @engine: a #GtkThemingEngine
393 * @property: the property name
394 * @state: state to retrieve the value for
395 * @value: (out) (transfer full): return location for the property value,
396 * you must free this memory using g_value_unset() once you are
399 * Gets a property value as retrieved from the style settings that apply
400 * to the currently rendered element.
405 gtk_theming_engine_get_property (GtkThemingEngine *engine,
406 const gchar *property,
410 GtkThemingEnginePrivate *priv;
412 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
413 g_return_if_fail (property != NULL);
414 g_return_if_fail (value != NULL);
417 gtk_style_context_get_property (priv->context, property, state, value);
421 * gtk_theming_engine_get_valist:
422 * @engine: a #GtkThemingEngine
423 * @state: state to retrieve values for
424 * @args: va_list of property name/return location pairs, followed by %NULL
426 * Retrieves several style property values that apply to the currently
432 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
436 GtkThemingEnginePrivate *priv;
438 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
441 gtk_style_context_get_valist (priv->context, state, args);
445 * gtk_theming_engine_get:
446 * @engine: a #GtkThemingEngine
447 * @state: state to retrieve values for
448 * @...: property name /return value pairs, followed by %NULL
450 * Retrieves several style property values that apply to the currently
456 gtk_theming_engine_get (GtkThemingEngine *engine,
460 GtkThemingEnginePrivate *priv;
463 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
467 va_start (args, state);
468 gtk_style_context_get_valist (priv->context, state, args);
473 * gtk_theming_engine_get_style_property:
474 * @engine: a #GtkThemingEngine
475 * @property_name: the name of the widget style property
476 * @value: (out) (transfer full): Return location for the property value, free with
477 * g_value_unset() after use.
479 * Gets the value for a widget style property.
484 gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
485 const gchar *property_name,
488 GtkThemingEnginePrivate *priv;
490 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
491 g_return_if_fail (property_name != NULL);
494 gtk_style_context_get_style_property (priv->context, property_name, value);
498 * gtk_theming_engine_get_style_valist:
499 * @engine: a #GtkThemingEngine
500 * @args: va_list of property name/return location pairs, followed by %NULL
502 * Retrieves several widget style properties from @engine according to the
503 * currently rendered content's style.
508 gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
511 GtkThemingEnginePrivate *priv;
513 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
516 gtk_style_context_get_style_valist (priv->context, args);
520 * gtk_theming_engine_get_style:
521 * @engine: a #GtkThemingEngine
522 * @...: property name /return value pairs, followed by %NULL
524 * Retrieves several widget style properties from @engine according
525 * to the currently rendered content's style.
530 gtk_theming_engine_get_style (GtkThemingEngine *engine,
533 GtkThemingEnginePrivate *priv;
536 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
540 va_start (args, engine);
541 gtk_style_context_get_style_valist (priv->context, args);
546 * gtk_theming_engine_lookup_color:
547 * @engine: a #GtkThemingEngine
548 * @color_name: color name to lookup
549 * @color: (out) (transfer full): Return location for the looked up color
551 * Looks up and resolves a color name in the current style's color map.
552 * The returned color must be freed with gdk_rgba_free().
554 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
557 gtk_theming_engine_lookup_color (GtkThemingEngine *engine,
558 const gchar *color_name,
561 GtkThemingEnginePrivate *priv;
563 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
564 g_return_val_if_fail (color_name != NULL, FALSE);
567 return gtk_style_context_lookup_color (priv->context, color_name, color);
571 * gtk_theming_engine_get_state:
572 * @engine: a #GtkThemingEngine
574 * returns the state used when rendering.
576 * Returns: the state flags
581 gtk_theming_engine_get_state (GtkThemingEngine *engine)
583 GtkThemingEnginePrivate *priv;
585 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
588 return gtk_style_context_get_state (priv->context);
592 * gtk_theming_engine_state_is_running:
593 * @engine: a #GtkThemingEngine
594 * @state: a widget state
595 * @progress: (out): return location for the transition progress
597 * Returns %TRUE if there is a transition animation running for the
598 * current region (see gtk_style_context_push_animatable_region()).
600 * If @progress is not %NULL, the animation progress will be returned
601 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
602 * it's closest to being %TRUE. This means transition animations will
603 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
604 * it's being set to %FALSE.
606 * Returns: %TRUE if there is a running transition animation for @state.
611 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
615 GtkThemingEnginePrivate *priv;
617 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
620 return gtk_style_context_state_is_running (priv->context, state, progress);
624 * gtk_theming_engine_get_path:
625 * @engine: a #GtkThemingEngine
627 * Returns the widget path used for style matching.
629 * Returns: (transfer none): A #GtkWidgetPath
633 G_CONST_RETURN GtkWidgetPath *
634 gtk_theming_engine_get_path (GtkThemingEngine *engine)
636 GtkThemingEnginePrivate *priv;
638 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
641 return gtk_style_context_get_path (priv->context);
645 * gtk_theming_engine_has_class:
646 * @engine: a #GtkThemingEngine
647 * @style_class: class name to look up
649 * Returns %TRUE if the currently rendered contents have
650 * defined the given class name.
652 * Returns: %TRUE if @engine has @class_name defined
657 gtk_theming_engine_has_class (GtkThemingEngine *engine,
658 const gchar *style_class)
660 GtkThemingEnginePrivate *priv;
662 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
665 return gtk_style_context_has_class (priv->context, style_class);
669 * gtk_theming_engine_has_region:
670 * @engine: a #GtkThemingEngine
671 * @style_region: a region name
672 * @flags: (out) (allow-none): return location for region flags
674 * Returns %TRUE if the currently rendered contents have the
675 * region defined. If @flags_return is not %NULL, it is set
676 * to the flags affecting the region.
678 * Returns: %TRUE if region is defined
683 gtk_theming_engine_has_region (GtkThemingEngine *engine,
684 const gchar *style_region,
685 GtkRegionFlags *flags)
687 GtkThemingEnginePrivate *priv;
692 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
695 return gtk_style_context_has_region (priv->context, style_region, flags);
699 * gtk_theming_engine_get_direction:
700 * @engine: a #GtkThemingEngine
702 * Returns the widget direction used for rendering.
704 * Returns: the widget direction
709 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
711 GtkThemingEnginePrivate *priv;
713 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
716 return gtk_style_context_get_direction (priv->context);
720 * gtk_theming_engine_get_junction_sides:
721 * @engine: a #GtkThemingEngine
723 * Returns the widget direction used for rendering.
725 * Returns: the widget direction
730 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
732 GtkThemingEnginePrivate *priv;
734 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
737 return gtk_style_context_get_junction_sides (priv->context);
740 /* GtkThemingModule */
743 gtk_theming_module_load (GTypeModule *type_module)
745 GtkThemingModule *theming_module;
747 gchar *name, *module_path;
749 theming_module = GTK_THEMING_MODULE (type_module);
750 name = theming_module->name;
751 module_path = _gtk_find_module (name, "theming-engines");
755 g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
759 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
760 g_free (module_path);
764 g_warning ("%s", g_module_error ());
768 if (!g_module_symbol (module, "theme_init",
769 (gpointer *) &theming_module->init) ||
770 !g_module_symbol (module, "theme_exit",
771 (gpointer *) &theming_module->exit) ||
772 !g_module_symbol (module, "create_engine",
773 (gpointer *) &theming_module->create_engine))
775 g_warning ("%s", g_module_error ());
776 g_module_close (module);
781 theming_module->module = module;
783 theming_module->init (G_TYPE_MODULE (theming_module));
789 gtk_theming_module_unload (GTypeModule *type_module)
791 GtkThemingModule *theming_module;
793 theming_module = GTK_THEMING_MODULE (type_module);
795 theming_module->exit ();
797 g_module_close (theming_module->module);
799 theming_module->module = NULL;
800 theming_module->init = NULL;
801 theming_module->exit = NULL;
802 theming_module->create_engine = NULL;
806 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
808 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
810 module_class->load = gtk_theming_module_load;
811 module_class->unload = gtk_theming_module_unload;
815 gtk_theming_module_init (GtkThemingModule *module)
820 * gtk_theming_engine_load:
821 * @name: Theme engine name to load
823 * Loads and initializes a theming engine module from the
824 * standard directories.
826 * Returns: (transfer none): A theming engine, or %NULL if
827 * the engine @name doesn't exist.
830 gtk_theming_engine_load (const gchar *name)
832 static GHashTable *engines = NULL;
833 static GtkThemingEngine *default_engine;
834 GtkThemingEngine *engine = NULL;
839 engines = g_hash_table_new (g_str_hash, g_str_equal);
841 engine = g_hash_table_lookup (engines, name);
845 GtkThemingModule *module;
847 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
848 g_type_module_set_name (G_TYPE_MODULE (module), name);
849 module->name = g_strdup (name);
851 if (module && g_type_module_use (G_TYPE_MODULE (module)))
853 engine = (module->create_engine) ();
856 g_hash_table_insert (engines, module->name, engine);
863 if (G_UNLIKELY (!default_engine))
864 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
866 engine = default_engine;
873 * gtk_theming_engine_get_screen:
874 * @engine: a #GtkThemingEngine
876 * Returns the #GdkScreen to which @engine currently rendering to.
878 * Returns: a #GdkScreen, or %NULL.
881 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
883 GtkThemingEnginePrivate *priv;
885 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
888 return gtk_style_context_get_screen (priv->context);
891 /* Paint method implementations */
893 gtk_theming_engine_render_check (GtkThemingEngine *engine,
900 GdkRGBA *fg_color, *bg_color, *border_color;
902 gint exterior_size, interior_size, thickness, pad;
903 GtkBorderStyle border_style;
907 flags = gtk_theming_engine_get_state (engine);
910 gtk_theming_engine_get (engine, flags,
912 "background-color", &bg_color,
913 "border-color", &border_color,
914 "border-style", &border_style,
915 "border-width", &border,
918 border_width = MIN (MIN (border->top, border->bottom),
919 MIN (border->left, border->right));
920 exterior_size = MIN (width, height);
922 if (exterior_size % 2 == 0) /* Ensure odd */
925 /* FIXME: thickness */
927 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
928 interior_size = MAX (1, exterior_size - 2 * pad);
930 if (interior_size < 7)
933 pad = MAX (0, (exterior_size - interior_size) / 2);
936 x -= (1 + exterior_size - (gint) width) / 2;
937 y -= (1 + exterior_size - (gint) height) / 2;
939 if (border_style == GTK_BORDER_STYLE_SOLID)
941 cairo_set_line_width (cr, border_width);
943 cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
944 gdk_cairo_set_source_rgba (cr, bg_color);
945 cairo_fill_preserve (cr);
948 gdk_cairo_set_source_rgba (cr, border_color);
950 gdk_cairo_set_source_rgba (cr, fg_color);
955 gdk_cairo_set_source_rgba (cr, fg_color);
957 if (flags & GTK_STATE_FLAG_INCONSISTENT)
959 int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
963 y + pad + (1 + interior_size - line_thickness) / 2,
973 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
975 if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
983 cairo_scale (cr, interior_size / 7., interior_size / 7.);
985 cairo_rectangle (cr, 0, 0, 7 * progress, 7);
988 cairo_move_to (cr, 7.0, 0.0);
989 cairo_line_to (cr, 7.5, 1.0);
990 cairo_curve_to (cr, 5.3, 2.0,
993 cairo_curve_to (cr, 3.0, 5.7,
996 cairo_line_to (cr, 0.2, 3.5);
997 cairo_curve_to (cr, 1.1, 3.5,
1000 cairo_curve_to (cr, 1.0, 3.9,
1003 cairo_curve_to (cr, 3.5, 3.1,
1013 gdk_rgba_free (fg_color);
1014 gdk_rgba_free (bg_color);
1015 gdk_rgba_free (border_color);
1016 gtk_border_free (border);
1020 gtk_theming_engine_render_option (GtkThemingEngine *engine,
1027 GtkStateFlags flags;
1028 GdkRGBA *fg_color, *bg_color, *border_color;
1029 gint exterior_size, interior_size, pad, thickness, border_width;
1030 GtkBorderStyle border_style;
1034 flags = gtk_theming_engine_get_state (engine);
1035 radius = MIN (width, height) / 2 - 0.5;
1039 gtk_theming_engine_get (engine, flags,
1041 "background-color", &bg_color,
1042 "border-color", &border_color,
1043 "border-style", &border_style,
1044 "border-width", &border,
1047 exterior_size = MIN (width, height);
1048 border_width = MIN (MIN (border->top, border->bottom),
1049 MIN (border->left, border->right));
1051 if (exterior_size % 2 == 0) /* Ensure odd */
1054 x -= (1 + exterior_size - width) / 2;
1055 y -= (1 + exterior_size - height) / 2;
1057 if (border_style == GTK_BORDER_STYLE_SOLID)
1059 cairo_set_line_width (cr, border_width);
1061 x + exterior_size / 2.,
1062 y + exterior_size / 2.,
1063 (exterior_size - 1) / 2.,
1066 gdk_cairo_set_source_rgba (cr, bg_color);
1067 cairo_fill_preserve (cr);
1070 gdk_cairo_set_source_rgba (cr, border_color);
1072 gdk_cairo_set_source_rgba (cr, fg_color);
1077 gdk_cairo_set_source_rgba (cr, fg_color);
1079 /* FIXME: thickness */
1082 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1084 gint line_thickness;
1086 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1087 interior_size = MAX (1, exterior_size - 2 * pad);
1089 if (interior_size < 7)
1092 pad = MAX (0, (exterior_size - interior_size) / 2);
1095 line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1097 cairo_rectangle (cr,
1099 y + pad + (interior_size - line_thickness) / 2.,
1104 if (flags & GTK_STATE_FLAG_ACTIVE)
1106 pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1107 interior_size = MAX (1, exterior_size - 2 * pad);
1109 if (interior_size < 5)
1112 pad = MAX (0, (exterior_size - interior_size) / 2);
1116 x + pad + interior_size / 2.,
1117 y + pad + interior_size / 2.,
1125 gdk_rgba_free (fg_color);
1126 gdk_rgba_free (bg_color);
1127 gdk_rgba_free (border_color);
1128 gtk_border_free (border);
1132 add_path_arrow (cairo_t *cr,
1140 cairo_translate (cr, x + (size / 2), y + (size / 2));
1141 cairo_rotate (cr, angle);
1143 cairo_move_to (cr, 0, - (size / 4));
1144 cairo_line_to (cr, - (size / 2), (size / 4));
1145 cairo_line_to (cr, (size / 2), (size / 4));
1146 cairo_close_path (cr);
1152 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1159 GtkStateFlags flags;
1164 flags = gtk_theming_engine_get_state (engine);
1166 gtk_theming_engine_get (engine, flags,
1170 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1172 add_path_arrow (cr, angle, x + 1, y + 1, size);
1173 cairo_set_source_rgb (cr, 1, 1, 1);
1177 add_path_arrow (cr, angle, x, y, size);
1178 gdk_cairo_set_source_rgba (cr, fg_color);
1183 gdk_rgba_free (fg_color);
1187 add_path_line (cairo_t *cr,
1193 /* Adjust endpoints */
1207 cairo_move_to (cr, x1, y1);
1208 cairo_line_to (cr, x2, y2);
1212 color_shade (const GdkRGBA *color,
1214 GdkRGBA *color_return)
1216 GtkSymbolicColor *literal, *shade;
1218 literal = gtk_symbolic_color_new_literal (color);
1219 shade = gtk_symbolic_color_new_shade (literal, factor);
1220 gtk_symbolic_color_unref (literal);
1222 gtk_symbolic_color_resolve (shade, NULL, color_return);
1223 gtk_symbolic_color_unref (shade);
1227 _cairo_round_rectangle_sides (cairo_t *cr,
1234 GtkJunctionSides junction)
1236 radius = CLAMP (radius, 0, MIN (width / 2, height / 2));
1238 if (sides & SIDE_RIGHT)
1241 (junction & GTK_JUNCTION_CORNER_TOPRIGHT))
1242 cairo_move_to (cr, x + width, y);
1245 cairo_new_sub_path (cr);
1246 cairo_arc (cr, x + width - radius, y + radius, radius, - G_PI / 4, 0);
1250 (junction & GTK_JUNCTION_CORNER_BOTTOMRIGHT))
1251 cairo_line_to (cr, x + width, y + height);
1253 cairo_arc (cr, x + width - radius, y + height - radius, radius, 0, G_PI / 4);
1256 if (sides & SIDE_BOTTOM)
1259 ! (junction & GTK_JUNCTION_CORNER_BOTTOMRIGHT))
1261 if ((sides & SIDE_RIGHT) == 0)
1262 cairo_new_sub_path (cr);
1264 cairo_arc (cr, x + width - radius, y + height - radius, radius, G_PI / 4, G_PI / 2);
1266 else if ((sides & SIDE_RIGHT) == 0)
1267 cairo_move_to (cr, x + width, y + height);
1270 (junction & GTK_JUNCTION_CORNER_BOTTOMLEFT))
1271 cairo_line_to (cr, x, y + height);
1273 cairo_arc (cr, x + radius, y + height - radius, radius, G_PI / 2, 3 * (G_PI / 4));
1276 cairo_move_to (cr, x, y + height);
1278 if (sides & SIDE_LEFT)
1281 ! (junction & GTK_JUNCTION_CORNER_BOTTOMLEFT))
1283 if ((sides & SIDE_BOTTOM) == 0)
1284 cairo_new_sub_path (cr);
1286 cairo_arc (cr, x + radius, y + height - radius, radius, 3 * (G_PI / 4), G_PI);
1288 else if ((sides & SIDE_BOTTOM) == 0)
1289 cairo_move_to (cr, x, y + height);
1292 (junction & GTK_JUNCTION_CORNER_TOPLEFT))
1293 cairo_line_to (cr, x, y);
1295 cairo_arc (cr, x + radius, y + radius, radius, G_PI, G_PI + G_PI / 4);
1298 if (sides & SIDE_TOP)
1301 ! (junction & GTK_JUNCTION_CORNER_TOPLEFT))
1303 if ((sides & SIDE_LEFT) == 0)
1304 cairo_new_sub_path (cr);
1306 cairo_arc (cr, x + radius, y + radius, radius, 5 * (G_PI / 4), 3 * (G_PI / 2));
1308 else if ((sides & SIDE_LEFT) == 0)
1309 cairo_move_to (cr, x, y);
1312 (junction & GTK_JUNCTION_CORNER_TOPRIGHT))
1313 cairo_line_to (cr, x + width, y);
1315 cairo_arc (cr, x + width - radius, y + radius, radius, 3 * (G_PI / 2), - G_PI / 4);
1320 render_background_internal (GtkThemingEngine *engine,
1326 GtkJunctionSides junction)
1329 cairo_pattern_t *pattern;
1330 GtkStateFlags flags;
1332 gdouble progress, alpha = 1;
1333 gint radius, border_width;
1336 flags = gtk_theming_engine_get_state (engine);
1339 gtk_theming_engine_get (engine, flags,
1340 "background-image", &pattern,
1341 "background-color", &bg_color,
1342 "border-width", &border,
1343 "border-radius", &radius,
1346 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1347 border_width = MIN (MIN (border->top, border->bottom),
1348 MIN (border->left, border->right));
1350 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1351 x, y, width, height,
1352 SIDE_ALL, junction);
1355 if (gtk_theming_engine_has_class (engine, "background"))
1357 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
1358 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1362 cairo_translate (cr, x, y);
1363 cairo_scale (cr, width, height);
1367 cairo_pattern_t *other_pattern;
1368 GtkStateFlags other_flags;
1370 cairo_pattern_t *new_pattern = NULL;
1372 if (flags & GTK_STATE_FLAG_PRELIGHT)
1374 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
1375 progress = 1 - progress;
1378 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
1380 gtk_theming_engine_get (engine, other_flags,
1381 "background-image", &other_pattern,
1382 "background-color", &other_bg,
1385 if (pattern && other_pattern)
1387 cairo_pattern_type_t type, other_type;
1390 cairo_pattern_get_color_stop_count (pattern, &n0);
1391 cairo_pattern_get_color_stop_count (other_pattern, &n1);
1392 type = cairo_pattern_get_type (pattern);
1393 other_type = cairo_pattern_get_type (other_pattern);
1395 if (type == other_type && n0 == n1)
1397 gdouble offset0, red0, green0, blue0, alpha0;
1398 gdouble offset1, red1, green1, blue1, alpha1;
1399 gdouble x00, x01, y00, y01, x10, x11, y10, y11;
1400 gdouble r00, r01, r10, r11;
1403 if (type == CAIRO_PATTERN_TYPE_LINEAR)
1405 cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
1406 cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
1408 new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
1409 y00 + (y10 - y00) * progress,
1410 x01 + (x11 - x01) * progress,
1411 y01 + (y11 - y01) * progress);
1415 cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
1416 cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
1418 new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
1419 y00 + (y10 - y00) * progress,
1420 r00 + (r10 - r00) * progress,
1421 x01 + (x11 - x01) * progress,
1422 y01 + (y11 - y01) * progress,
1423 r01 + (r11 - r01) * progress);
1426 cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
1429 /* Blend both gradients into one */
1430 while (i < n0 && i < n1)
1432 cairo_pattern_get_color_stop_rgba (pattern, i,
1434 &red0, &green0, &blue0,
1436 cairo_pattern_get_color_stop_rgba (other_pattern, i,
1438 &red1, &green1, &blue1,
1441 cairo_pattern_add_color_stop_rgba (new_pattern,
1442 offset0 + ((offset1 - offset0) * progress),
1443 red0 + ((red1 - red0) * progress),
1444 green0 + ((green1 - green0) * progress),
1445 blue0 + ((blue1 - blue0) * progress),
1446 alpha0 + ((alpha1 - alpha0) * progress));
1452 /* Different pattern types, or different color
1453 * stop counts, alpha blend both patterns.
1455 cairo_rectangle (cr, 0, 0, 1, 1);
1456 cairo_set_source (cr, other_pattern);
1457 cairo_fill_preserve (cr);
1459 /* Set alpha for posterior drawing
1460 * of the target pattern
1462 alpha = 1 - progress;
1465 else if (pattern || other_pattern)
1469 gdouble x0, y0, x1, y1, r0, r1;
1472 /* Blend a pattern with a color */
1477 progress = 1 - progress;
1485 if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
1487 cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
1488 new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
1492 cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
1493 new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
1496 cairo_pattern_get_color_stop_count (p, &n);
1498 for (i = 0; i < n; i++)
1500 gdouble red1, green1, blue1, alpha1;
1503 cairo_pattern_get_color_stop_rgba (p, i,
1505 &red1, &green1, &blue1,
1507 cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1508 c->red + ((red1 - c->red) * progress),
1509 c->green + ((green1 - c->green) * progress),
1510 c->blue + ((blue1 - c->blue) * progress),
1511 c->alpha + ((alpha1 - c->alpha) * progress));
1516 const GdkRGBA *color, *other_color;
1518 /* Merge just colors */
1520 other_color = other_bg;
1522 new_pattern = cairo_pattern_create_rgba (CLAMP (color->red + ((other_color->red - color->red) * progress), 0, 1),
1523 CLAMP (color->green + ((other_color->green - color->green) * progress), 0, 1),
1524 CLAMP (color->blue + ((other_color->blue - color->blue) * progress), 0, 1),
1525 CLAMP (color->alpha + ((other_color->alpha - color->alpha) * progress), 0, 1));
1530 /* Replace pattern to use */
1531 cairo_pattern_destroy (pattern);
1532 pattern = new_pattern;
1536 cairo_pattern_destroy (other_pattern);
1539 gdk_rgba_free (other_bg);
1542 cairo_rectangle (cr, 0, 0, 1, 1);
1546 cairo_set_source (cr, pattern);
1547 cairo_pattern_destroy (pattern);
1550 gdk_cairo_set_source_rgba (cr, bg_color);
1556 cairo_pattern_t *mask;
1558 mask = cairo_pattern_create_rgba (1, 1, 1, alpha);
1559 cairo_mask (cr, mask);
1560 cairo_pattern_destroy (mask);
1565 gdk_rgba_free (bg_color);
1566 gtk_border_free (border);
1570 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1577 GtkJunctionSides junction;
1578 GtkStateFlags flags;
1581 junction = gtk_theming_engine_get_junction_sides (engine);
1583 if (gtk_theming_engine_has_class (engine, "spinbutton") &&
1584 gtk_theming_engine_has_class (engine, "button"))
1592 flags = gtk_theming_engine_get_state (engine);
1593 gtk_theming_engine_get (engine, flags,
1594 "border-width", &border,
1599 width -= border->left + border->right;
1600 height -= border->top + border->bottom;
1602 render_background_internal (engine, cr,
1603 x, y, width, height,
1606 gtk_border_free (border);
1609 /* Renders the small triangle on corners so
1610 * frames with 0 radius have a 3D-like effect
1613 _cairo_corner_triangle (cairo_t *cr,
1620 cairo_move_to (cr, x + 0.5, y + size - 0.5);
1621 cairo_line_to (cr, x + size - 0.5, y + size - 0.5);
1622 cairo_line_to (cr, x + size - 0.5, y + 0.5);
1624 for (i = 1; i < size - 1; i++)
1626 cairo_move_to (cr, x + size - 0.5, y + i + 0.5);
1627 cairo_line_to (cr, x + (size - i) - 0.5, y + i + 0.5);
1632 render_frame_internal (GtkThemingEngine *engine,
1639 GtkJunctionSides junction)
1641 GtkStateFlags state;
1643 GdkRGBA *border_color;
1644 GtkBorderStyle border_style;
1645 gint border_width, radius;
1646 gdouble progress, d1, d2, m;
1650 state = gtk_theming_engine_get_state (engine);
1651 gtk_theming_engine_get (engine, state,
1652 "border-color", &border_color,
1653 "border-style", &border_style,
1654 "border-width", &border,
1655 "border-radius", &radius,
1658 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1659 border_width = MIN (MIN (border->top, border->bottom),
1660 MIN (border->left, border->right));
1664 GtkStateFlags other_state;
1665 GdkRGBA *other_color;
1667 if (state & GTK_STATE_FLAG_PRELIGHT)
1669 other_state = state & ~(GTK_STATE_FLAG_PRELIGHT);
1670 progress = 1 - progress;
1673 other_state = state | GTK_STATE_FLAG_PRELIGHT;
1675 gtk_theming_engine_get (engine, other_state,
1676 "border-color", &other_color,
1679 border_color->red = CLAMP (border_color->red + ((other_color->red - border_color->red) * progress), 0, 1);
1680 border_color->green = CLAMP (border_color->green + ((other_color->green - border_color->green) * progress), 0, 1);
1681 border_color->blue = CLAMP (border_color->blue + ((other_color->blue - border_color->blue) * progress), 0, 1);
1682 border_color->alpha = CLAMP (border_color->alpha + ((other_color->alpha - border_color->alpha) * progress), 0, 1);
1684 gdk_rgba_free (other_color);
1689 color_shade (border_color, 1.8, &lighter);
1691 switch (border_style)
1693 case GTK_BORDER_STYLE_NONE:
1695 case GTK_BORDER_STYLE_SOLID:
1696 cairo_set_line_width (cr, border_width);
1697 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1699 if (border_width > 1)
1701 x += (gdouble) border_width / 2;
1702 y += (gdouble) border_width / 2;
1703 width -= border_width;
1704 height -= border_width;
1706 else if (border_width == 1)
1714 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1715 x, y, width, height,
1716 SIDE_ALL & ~(hidden_side),
1718 gdk_cairo_set_source_rgba (cr, border_color);
1722 case GTK_BORDER_STYLE_INSET:
1723 case GTK_BORDER_STYLE_OUTSET:
1724 cairo_set_line_width (cr, border_width);
1727 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1729 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
1731 if (border_width > 1)
1733 d1 = (gdouble) border_width / 2;
1744 m = MIN (width, height);
1747 if (border_style == GTK_BORDER_STYLE_INSET)
1748 gdk_cairo_set_source_rgba (cr, &lighter);
1750 gdk_cairo_set_source_rgba (cr, border_color);
1752 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1754 width - d2, height - d2,
1755 (SIDE_BOTTOM | SIDE_RIGHT) & ~(hidden_side),
1759 if (border_style == GTK_BORDER_STYLE_INSET)
1760 gdk_cairo_set_source_rgba (cr, border_color);
1762 gdk_cairo_set_source_rgba (cr, &lighter);
1764 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1766 width - d2, height - d2,
1767 (SIDE_TOP | SIDE_LEFT) & ~(hidden_side),
1771 if (border_width > 1)
1773 /* overprint top/right and bottom/left corner
1774 * triangles if there are square corners there,
1775 * to give the box a 3D-like appearance.
1779 if (border_style == GTK_BORDER_STYLE_INSET)
1780 gdk_cairo_set_source_rgba (cr, &lighter);
1782 gdk_cairo_set_source_rgba (cr, border_color);
1784 cairo_set_line_width (cr, 1);
1787 (junction & GTK_JUNCTION_CORNER_TOPRIGHT) != 0)
1788 _cairo_corner_triangle (cr,
1789 x + width - border_width, y,
1793 (junction & GTK_JUNCTION_CORNER_BOTTOMLEFT) != 0)
1794 _cairo_corner_triangle (cr,
1795 x, y + height - border_width,
1808 gdk_rgba_free (border_color);
1810 gtk_border_free (border);
1814 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1821 GtkStateFlags flags;
1823 GtkBorderStyle border_style;
1824 GtkJunctionSides junction;
1826 flags = gtk_theming_engine_get_state (engine);
1827 junction = gtk_theming_engine_get_junction_sides (engine);
1829 gtk_theming_engine_get (engine, flags,
1830 "border-image", &slice,
1831 "border-style", &border_style,
1836 gtk_9slice_render (slice, cr, x, y, width, height);
1837 gtk_9slice_unref (slice);
1839 else if (border_style != GTK_BORDER_STYLE_NONE)
1840 render_frame_internal (engine, cr,
1841 x, y, width, height,
1846 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1853 GtkStateFlags flags;
1854 GdkRGBA *outline_color, *fg_color;
1855 double vertical_overshoot;
1858 double interp; /* interpolation factor for center position */
1859 double x_double_horz, y_double_horz;
1860 double x_double_vert, y_double_vert;
1861 double x_double, y_double;
1866 flags = gtk_theming_engine_get_state (engine);
1868 gtk_theming_engine_get (engine, flags,
1871 gtk_theming_engine_get (engine, 0,
1872 "color", &outline_color,
1877 /* FIXME: LTR/RTL */
1878 if (flags & GTK_STATE_FLAG_ACTIVE)
1889 /* Compute distance that the stroke extends beyonds the end
1890 * of the triangle we draw.
1892 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1894 /* For odd line widths, we end the vertical line of the triangle
1895 * at a half pixel, so we round differently.
1897 if (line_width % 2 == 1)
1898 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1900 vertical_overshoot = ceil (vertical_overshoot);
1902 /* Adjust the size of the triangle we draw so that the entire stroke fits
1904 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1906 /* If the line width is odd, we want the diameter to be even,
1907 * and vice versa, so force the sum to be odd. This relationship
1908 * makes the point of the triangle look right.
1910 diameter -= (1 - (diameter + line_width) % 2);
1912 radius = diameter / 2.;
1914 /* Adjust the center so that the stroke is properly aligned with
1915 * the pixel grid. The center adjustment is different for the
1916 * horizontal and vertical orientations. For intermediate positions
1917 * we interpolate between the two.
1919 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1920 y_double_vert = (y + height / 2) - 0.5;
1922 x_double_horz = (x + width / 2) - 0.5;
1923 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1925 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1926 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1928 cairo_translate (cr, x_double, y_double);
1929 cairo_rotate (cr, angle);
1931 cairo_move_to (cr, - radius / 2., - radius);
1932 cairo_line_to (cr, radius / 2., 0);
1933 cairo_line_to (cr, - radius / 2., radius);
1934 cairo_close_path (cr);
1936 cairo_set_line_width (cr, line_width);
1938 gdk_cairo_set_source_rgba (cr, fg_color);
1940 cairo_fill_preserve (cr);
1942 gdk_cairo_set_source_rgba (cr, outline_color);
1947 gdk_rgba_free (fg_color);
1948 gdk_rgba_free (outline_color);
1952 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1959 GtkStateFlags flags;
1965 flags = gtk_theming_engine_get_state (engine);
1967 gtk_theming_engine_get (engine, flags,
1971 gtk_theming_engine_get_style (engine,
1972 "focus-line-width", &line_width,
1973 "focus-line-pattern", (gchar *) &dash_list,
1976 cairo_set_line_width (cr, (gdouble) line_width);
1980 gint n_dashes = strlen ((const gchar *) dash_list);
1981 gdouble *dashes = g_new (gdouble, n_dashes);
1982 gdouble total_length = 0;
1983 gdouble dash_offset;
1986 for (i = 0; i < n_dashes; i++)
1988 dashes[i] = dash_list[i];
1989 total_length += dash_list[i];
1992 /* The dash offset here aligns the pattern to integer pixels
1993 * by starting the dash at the right side of the left border
1994 * Negative dash offsets in cairo don't work
1995 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
1997 dash_offset = - line_width / 2.;
1999 while (dash_offset < 0)
2000 dash_offset += total_length;
2002 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
2006 cairo_rectangle (cr,
2007 x + line_width / 2.,
2008 y + line_width / 2.,
2010 height - line_width);
2012 gdk_cairo_set_source_rgba (cr, color);
2017 gdk_rgba_free (color);
2022 gtk_theming_engine_render_line (GtkThemingEngine *engine,
2029 GdkRGBA *bg_color, darker, lighter;
2030 GtkStateFlags flags;
2031 gint i, thickness, thickness_dark, thickness_light, len;
2032 cairo_matrix_t matrix;
2035 /* FIXME: thickness */
2037 thickness_dark = thickness / 2;
2038 thickness_light = thickness - thickness_dark;
2040 flags = gtk_theming_engine_get_state (engine);
2043 gtk_theming_engine_get (engine, flags,
2044 "background-color", &bg_color,
2046 color_shade (bg_color, 0.7, &darker);
2047 color_shade (bg_color, 1.3, &lighter);
2049 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
2050 cairo_set_line_width (cr, 1);
2052 angle = atan2 (x1 - x0, y1 - y0);
2053 angle = (2 * G_PI) - angle;
2056 cairo_get_matrix (cr, &matrix);
2057 cairo_matrix_translate (&matrix, x0, y0);
2058 cairo_matrix_rotate (&matrix, angle);
2059 cairo_set_matrix (cr, &matrix);
2064 len = (gint) sqrt ((x1 * x1) + (y1 * y1));
2066 y0 = -thickness_dark;
2068 for (i = 0; i < thickness_dark; i++)
2070 gdk_cairo_set_source_rgba (cr, &lighter);
2071 add_path_line (cr, len - i - 1.5, y0, len - 0.5, y0);
2074 gdk_cairo_set_source_rgba (cr, &darker);
2075 add_path_line (cr, 0.5, y0, len - i - 1.5, y0);
2081 for (i = 0; i < thickness_light; i++)
2083 gdk_cairo_set_source_rgba (cr, &darker);
2084 add_path_line (cr, 0.5, y0, thickness_light - i + 0.5, y0);
2087 gdk_cairo_set_source_rgba (cr, &lighter);
2088 add_path_line (cr, thickness_light - i + 0.5, y0, len - 0.5, y0);
2096 gdk_rgba_free (bg_color);
2100 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2104 PangoLayout *layout)
2106 const PangoMatrix *matrix;
2108 GtkStateFlags flags;
2114 flags = gtk_theming_engine_get_state (engine);
2116 gtk_theming_engine_get (engine, flags,
2120 screen = gtk_theming_engine_get_screen (engine);
2121 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2123 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
2127 GtkStateFlags other_flags;
2130 if (flags & GTK_STATE_FLAG_PRELIGHT)
2132 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
2133 progress = 1 - progress;
2136 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
2138 gtk_theming_engine_get (engine, other_flags,
2142 fg_color->red = CLAMP (fg_color->red + ((other_fg->red - fg_color->red) * progress), 0, 1);
2143 fg_color->green = CLAMP (fg_color->green + ((other_fg->green - fg_color->green) * progress), 0, 1);
2144 fg_color->blue = CLAMP (fg_color->blue + ((other_fg->blue - fg_color->blue) * progress), 0, 1);
2145 fg_color->alpha = CLAMP (fg_color->alpha + ((other_fg->alpha - fg_color->alpha) * progress), 0, 1);
2147 gdk_rgba_free (other_fg);
2152 cairo_matrix_t cairo_matrix;
2153 PangoMatrix tmp_matrix;
2154 PangoRectangle rect;
2156 cairo_matrix_init (&cairo_matrix,
2157 matrix->xx, matrix->yx,
2158 matrix->xy, matrix->yy,
2159 matrix->x0, matrix->y0);
2161 pango_layout_get_extents (layout, NULL, &rect);
2162 pango_matrix_transform_rectangle (matrix, &rect);
2163 pango_extents_to_pixels (&rect, NULL);
2165 tmp_matrix = *matrix;
2166 cairo_matrix.x0 += x - rect.x;
2167 cairo_matrix.y0 += y - rect.y;
2169 cairo_set_matrix (cr, &cairo_matrix);
2172 cairo_translate (cr, x, y);
2174 if (flags & GTK_STATE_FLAG_INSENSITIVE)
2177 cairo_set_source_rgb (cr, 1, 1, 1);
2178 cairo_move_to (cr, 1, 1);
2179 _gtk_pango_fill_layout (cr, layout);
2183 gdk_cairo_set_source_rgba (cr, fg_color);
2184 pango_cairo_show_layout (cr, layout);
2188 gdk_rgba_free (fg_color);
2192 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2198 GtkOrientation orientation)
2200 const GtkWidgetPath *path;
2203 path = gtk_theming_engine_get_path (engine);
2205 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2206 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2208 /* FIXME: thickness */
2211 if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2213 if (orientation == GTK_ORIENTATION_VERTICAL)
2214 gtk_theming_engine_render_line (engine, cr,
2216 y + (gint) height / 2,
2217 x + width - thickness - 1,
2218 y + (gint) height / 2);
2220 gtk_theming_engine_render_line (engine, cr,
2221 x + (gint) width / 2,
2223 x + (gint) width / 2,
2224 y + height - thickness - 1);
2229 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2235 GtkPositionType gap_side,
2239 GtkJunctionSides junction;
2240 GtkStateFlags state;
2241 gint border_width, radius;
2242 gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2245 xc = yc = wc = hc = 0;
2246 state = gtk_theming_engine_get_state (engine);
2247 junction = gtk_theming_engine_get_junction_sides (engine);
2248 gtk_theming_engine_get (engine, state,
2249 "border-width", &border,
2250 "border-radius", &radius,
2253 border_width = MIN (MIN (border->top, border->bottom),
2254 MIN (border->left, border->right));
2261 xc = x + xy0_gap + border_width;
2263 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2266 if (xy0_gap < radius)
2267 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2269 if (xy1_gap > width - radius)
2270 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2272 case GTK_POS_BOTTOM:
2273 xc = x + xy0_gap + border_width;
2274 yc = y + height - border_width;
2275 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2278 if (xy0_gap < radius)
2279 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2281 if (xy1_gap > width - radius)
2282 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2287 yc = y + xy0_gap + border_width;
2289 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2291 if (xy0_gap < radius)
2292 junction |= GTK_JUNCTION_CORNER_TOPLEFT;
2294 if (xy1_gap > height - radius)
2295 junction |= GTK_JUNCTION_CORNER_BOTTOMLEFT;
2299 xc = x + width - border_width;
2300 yc = y + xy0_gap + border_width;
2302 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2304 if (xy0_gap < radius)
2305 junction |= GTK_JUNCTION_CORNER_TOPRIGHT;
2307 if (xy1_gap > height - radius)
2308 junction |= GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2313 cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2314 cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2315 cairo_rectangle (cr, x0, yc, xc - x0, hc);
2316 cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2317 cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2320 render_frame_internal (engine, cr,
2321 x, y, width, height,
2326 gtk_border_free (border);
2330 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2336 GtkPositionType gap_side)
2338 GtkJunctionSides junction = 0;
2339 guint hidden_side = 0;
2346 junction = GTK_JUNCTION_LEFT;
2347 hidden_side = SIDE_LEFT;
2349 cairo_translate (cr, x + width, y);
2350 cairo_rotate (cr, G_PI / 2);
2353 junction = GTK_JUNCTION_RIGHT;
2354 hidden_side = SIDE_RIGHT;
2356 cairo_translate (cr, x, y + height);
2357 cairo_rotate (cr, - G_PI / 2);
2360 junction = GTK_JUNCTION_TOP;
2361 hidden_side = SIDE_TOP;
2363 cairo_translate (cr, x + width, y + height);
2364 cairo_rotate (cr, G_PI);
2366 case GTK_POS_BOTTOM:
2367 junction = GTK_JUNCTION_BOTTOM;
2368 hidden_side = SIDE_BOTTOM;
2370 cairo_translate (cr, x, y);
2374 if (gap_side == GTK_POS_TOP ||
2375 gap_side == GTK_POS_BOTTOM)
2376 render_background_internal (engine, cr,
2377 0, 0, width, height,
2380 render_background_internal (engine, cr,
2381 0, 0, height, width,
2388 render_frame_internal (engine, cr,
2389 x, y, width, height,
2390 hidden_side, junction);
2396 render_dot (cairo_t *cr,
2397 const GdkRGBA *lighter,
2398 const GdkRGBA *darker,
2403 size = CLAMP ((gint) size, 2, 3);
2407 gdk_cairo_set_source_rgba (cr, lighter);
2408 cairo_rectangle (cr, x, y, 1, 1);
2409 cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2414 gdk_cairo_set_source_rgba (cr, lighter);
2415 cairo_rectangle (cr, x, y, 2, 1);
2416 cairo_rectangle (cr, x, y, 1, 2);
2419 gdk_cairo_set_source_rgba (cr, darker);
2420 cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2421 cairo_rectangle (cr, x + 2, y, 1, 2);
2427 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2434 GtkStateFlags flags;
2436 GdkRGBA lighter, darker;
2440 flags = gtk_theming_engine_get_state (engine);
2442 cairo_set_line_width (cr, 1);
2444 gtk_theming_engine_get (engine, flags,
2445 "background-color", &bg_color,
2447 color_shade (bg_color, 0.7, &darker);
2448 color_shade (bg_color, 1.3, &lighter);
2450 gdk_cairo_set_source_rgba (cr, bg_color);
2451 cairo_rectangle (cr, x, y, width, height);
2454 if (gtk_theming_engine_has_class (engine, "grip"))
2456 GtkJunctionSides sides;
2461 cairo_set_line_width (cr, 1.0);
2462 sides = gtk_theming_engine_get_junction_sides (engine);
2464 /* reduce confusing values to a meaningful state */
2465 if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2466 sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2468 if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2469 sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2472 sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2474 /* align drawing area to the connected side */
2475 if (sides == GTK_JUNCTION_LEFT)
2480 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2484 else if (height < width)
2489 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2491 /* make it square, aligning to bottom left */
2494 y += (height - width);
2497 else if (height < width)
2502 else if (sides == GTK_JUNCTION_RIGHT)
2504 /* aligning to right */
2507 x += (width - height);
2511 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2515 else if (height < width)
2517 x += (width - height);
2523 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2525 /* make it square, aligning to bottom right */
2528 y += (height - width);
2531 else if (height < width)
2533 x += (width - height);
2539 else if (sides == GTK_JUNCTION_TOP)
2544 else if (sides == GTK_JUNCTION_BOTTOM)
2546 /* align to bottom */
2549 y += (height - width);
2554 g_assert_not_reached ();
2556 if (sides == GTK_JUNCTION_LEFT ||
2557 sides == GTK_JUNCTION_RIGHT)
2563 while (xi < x + width)
2565 gdk_cairo_set_source_rgba (cr, &lighter);
2566 add_path_line (cr, x, y, x, y + height);
2570 gdk_cairo_set_source_rgba (cr, &darker);
2571 add_path_line (cr, xi, y, xi, y + height);
2576 else if (sides == GTK_JUNCTION_TOP ||
2577 sides == GTK_JUNCTION_BOTTOM)
2583 while (yi < y + height)
2585 gdk_cairo_set_source_rgba (cr, &lighter);
2586 add_path_line (cr, x, yi, x + width, yi);
2590 gdk_cairo_set_source_rgba (cr, &darker);
2591 add_path_line (cr, x, yi, x + width, yi);
2596 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2605 gdk_cairo_set_source_rgba (cr, &darker);
2606 add_path_line (cr, xi, y, x, yi);
2612 add_path_line (cr, xi, y, x, yi);
2618 gdk_cairo_set_source_rgba (cr, &lighter);
2619 add_path_line (cr, xi, y, x, yi);
2626 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2633 while (xi < (x + width - 3))
2635 gdk_cairo_set_source_rgba (cr, &lighter);
2636 add_path_line (cr, xi, y, x + width, yi);
2642 gdk_cairo_set_source_rgba (cr, &darker);
2643 add_path_line (cr, xi, y, x + width, yi);
2649 add_path_line (cr, xi, y, x + width, yi);
2656 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2665 gdk_cairo_set_source_rgba (cr, &darker);
2666 add_path_line (cr, x, yi, xi, y + height);
2672 add_path_line (cr, x, yi, xi, y + height);
2678 gdk_cairo_set_source_rgba (cr, &lighter);
2679 add_path_line (cr, x, yi, xi, y + height);
2686 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2693 while (xi < (x + width - 3))
2695 gdk_cairo_set_source_rgba (cr, &lighter);
2696 add_path_line (cr, xi, y + height, x + width, yi);
2702 gdk_cairo_set_source_rgba (cr, &darker);
2703 add_path_line (cr, xi, y + height, x + width, yi);
2709 add_path_line (cr, xi, y + height, x + width, yi);
2719 else if (gtk_theming_engine_has_class (engine, "paned"))
2722 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2723 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2725 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2726 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2730 for (yy = y; yy < y + height; yy += 3)
2731 for (xx = x; xx < x + width; xx += 6)
2733 render_dot (cr, &lighter, &darker, xx, yy, 2);
2734 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2740 gdk_rgba_free (bg_color);
2744 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2751 if (gtk_theming_engine_has_class (engine, GTK_STYLE_CLASS_SPINNER))
2753 GtkStateFlags state;
2754 guint num_steps, step;
2764 gtk_theming_engine_get_style (engine,
2765 "num-steps", &num_steps,
2768 state = gtk_theming_engine_get_state (engine);
2769 gtk_theming_engine_get (engine, state,
2775 if (gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2776 step = (guint) (progress * num_steps);
2782 cairo_translate (cr, x, y);
2784 /* draw clip region */
2785 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2789 radius = MIN (width / 2, height / 2);
2790 half = num_steps / 2;
2792 for (i = 0; i < num_steps; i++)
2794 gint inset = 0.7 * radius;
2796 /* transparency is a function of time and intial value */
2797 gdouble t = (gdouble) ((i + num_steps - step)
2798 % num_steps) / num_steps;
2802 cairo_set_source_rgba (cr,
2808 cairo_set_line_width (cr, 2.0);
2810 dx + (radius - inset) * cos (i * G_PI / half),
2811 dy + (radius - inset) * sin (i * G_PI / half));
2813 dx + radius * cos (i * G_PI / half),
2814 dy + radius * sin (i * G_PI / half));
2822 gdk_rgba_free (color);
2826 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2827 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2832 scale_or_ref (GdkPixbuf *src,
2836 if (width == gdk_pixbuf_get_width (src) &&
2837 height == gdk_pixbuf_get_height (src))
2838 return g_object_ref (src);
2840 return gdk_pixbuf_scale_simple (src,
2842 GDK_INTERP_BILINEAR);
2846 lookup_icon_size (GtkThemingEngine *engine,
2852 GtkSettings *settings;
2854 screen = gtk_theming_engine_get_screen (engine);
2855 settings = gtk_settings_get_for_screen (screen);
2857 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2861 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2862 const GtkIconSource *source,
2867 GdkPixbuf *base_pixbuf;
2868 GtkStateFlags state;
2872 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2873 state = gtk_theming_engine_get_state (engine);
2875 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2877 if (size != (GtkIconSize) -1 &&
2878 !lookup_icon_size (engine, size, &width, &height))
2880 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2884 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2887 if (size != (GtkIconSize) -1 &&
2888 gtk_icon_source_get_size_wildcarded (source))
2889 scaled = scale_or_ref (base_pixbuf, width, height);
2891 scaled = g_object_ref (base_pixbuf);
2893 /* If the state was wildcarded, then generate a state. */
2894 if (gtk_icon_source_get_state_wildcarded (source))
2896 if (state & GTK_STATE_FLAG_INSENSITIVE)
2898 stated = gdk_pixbuf_copy (scaled);
2899 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2901 g_object_unref (scaled);
2903 else if (state & GTK_STATE_FLAG_PRELIGHT)
2905 stated = gdk_pixbuf_copy (scaled);
2906 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2908 g_object_unref (scaled);