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_get_state:
547 * @engine: a #GtkThemingEngine
549 * returns the state used when rendering.
551 * Returns: the state flags
556 gtk_theming_engine_get_state (GtkThemingEngine *engine)
558 GtkThemingEnginePrivate *priv;
560 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
563 return gtk_style_context_get_state (priv->context);
567 * gtk_theming_engine_state_is_running:
568 * @engine: a #GtkThemingEngine
569 * @state: a widget state
570 * @progress: (out): return location for the transition progress
572 * Returns %TRUE if there is a transition animation running for the
573 * current region (see gtk_style_context_push_animatable_region()).
575 * If @progress is not %NULL, the animation progress will be returned
576 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
577 * it's closest to being %TRUE. This means transition animations will
578 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
579 * it's being set to %FALSE.
581 * Returns: %TRUE if there is a running transition animation for @state.
586 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
590 GtkThemingEnginePrivate *priv;
592 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
595 return gtk_style_context_state_is_running (priv->context, state, progress);
599 * gtk_theming_engine_get_path:
600 * @engine: a #GtkThemingEngine
602 * Returns the widget path used for style matching.
604 * Returns: (transfer none): A #GtkWidgetPath
608 G_CONST_RETURN GtkWidgetPath *
609 gtk_theming_engine_get_path (GtkThemingEngine *engine)
611 GtkThemingEnginePrivate *priv;
613 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
616 return gtk_style_context_get_path (priv->context);
620 * gtk_theming_engine_has_class:
621 * @engine: a #GtkThemingEngine
622 * @style_class: class name to look up
624 * Returns %TRUE if the currently rendered contents have
625 * defined the given class name.
627 * Returns: %TRUE if @engine has @class_name defined
632 gtk_theming_engine_has_class (GtkThemingEngine *engine,
633 const gchar *style_class)
635 GtkThemingEnginePrivate *priv;
637 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
640 return gtk_style_context_has_class (priv->context, style_class);
644 * gtk_theming_engine_has_region:
645 * @engine: a #GtkThemingEngine
646 * @style_region: a region name
647 * @flags: (out) (allow-none): return location for region flags
649 * Returns %TRUE if the currently rendered contents have the
650 * region defined. If @flags_return is not %NULL, it is set
651 * to the flags affecting the region.
653 * Returns: %TRUE if region is defined
658 gtk_theming_engine_has_region (GtkThemingEngine *engine,
659 const gchar *style_region,
660 GtkRegionFlags *flags)
662 GtkThemingEnginePrivate *priv;
667 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
670 return gtk_style_context_has_region (priv->context, style_region, flags);
674 * gtk_theming_engine_get_direction:
675 * @engine: a #GtkThemingEngine
677 * Returns the widget direction used for rendering.
679 * Returns: the widget direction
684 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
686 GtkThemingEnginePrivate *priv;
688 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
691 return gtk_style_context_get_direction (priv->context);
695 * gtk_theming_engine_get_junction_sides:
696 * @engine: a #GtkThemingEngine
698 * Returns the widget direction used for rendering.
700 * Returns: the widget direction
705 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
707 GtkThemingEnginePrivate *priv;
709 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
712 return gtk_style_context_get_junction_sides (priv->context);
715 /* GtkThemingModule */
718 gtk_theming_module_load (GTypeModule *type_module)
720 GtkThemingModule *theming_module;
722 gchar *name, *module_path;
724 theming_module = GTK_THEMING_MODULE (type_module);
725 name = theming_module->name;
726 module_path = _gtk_find_module (name, "theming-engines");
730 g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
734 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
735 g_free (module_path);
739 g_warning ("%s", g_module_error ());
743 if (!g_module_symbol (module, "theme_init",
744 (gpointer *) &theming_module->init) ||
745 !g_module_symbol (module, "theme_exit",
746 (gpointer *) &theming_module->exit) ||
747 !g_module_symbol (module, "create_engine",
748 (gpointer *) &theming_module->create_engine))
750 g_warning ("%s", g_module_error ());
751 g_module_close (module);
756 theming_module->module = module;
758 theming_module->init (G_TYPE_MODULE (theming_module));
764 gtk_theming_module_unload (GTypeModule *type_module)
766 GtkThemingModule *theming_module;
768 theming_module = GTK_THEMING_MODULE (type_module);
770 theming_module->exit ();
772 g_module_close (theming_module->module);
774 theming_module->module = NULL;
775 theming_module->init = NULL;
776 theming_module->exit = NULL;
777 theming_module->create_engine = NULL;
781 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
783 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
785 module_class->load = gtk_theming_module_load;
786 module_class->unload = gtk_theming_module_unload;
790 gtk_theming_module_init (GtkThemingModule *module)
795 * gtk_theming_engine_load:
796 * @name: Theme engine name to load
798 * Loads and initializes a theming engine module from the
799 * standard directories.
801 * Returns: (transfer none): A theming engine, or %NULL if
802 * the engine @name doesn't exist.
805 gtk_theming_engine_load (const gchar *name)
807 static GHashTable *engines = NULL;
808 static GtkThemingEngine *default_engine;
809 GtkThemingEngine *engine = NULL;
814 engines = g_hash_table_new (g_str_hash, g_str_equal);
816 engine = g_hash_table_lookup (engines, name);
820 GtkThemingModule *module;
822 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
823 g_type_module_set_name (G_TYPE_MODULE (module), name);
824 module->name = g_strdup (name);
826 if (module && g_type_module_use (G_TYPE_MODULE (module)))
828 engine = (module->create_engine) ();
831 g_hash_table_insert (engines, module->name, engine);
838 if (G_UNLIKELY (!default_engine))
839 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
841 engine = default_engine;
848 * gtk_theming_engine_get_screen:
849 * @engine: a #GtkThemingEngine
851 * Returns the #GdkScreen to which @engine currently rendering to.
853 * Returns: a #GdkScreen, or %NULL.
856 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
858 GtkThemingEnginePrivate *priv;
860 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
863 return gtk_style_context_get_screen (priv->context);
866 /* Paint method implementations */
868 gtk_theming_engine_render_check (GtkThemingEngine *engine,
875 GdkRGBA *fg_color, *bg_color, *border_color;
877 gint exterior_size, interior_size, thickness, pad;
878 GtkBorderStyle border_style;
881 flags = gtk_theming_engine_get_state (engine);
884 gtk_theming_engine_get (engine, flags,
886 "background-color", &bg_color,
887 "border-color", &border_color,
888 "border-style", &border_style,
889 "border-width", &border_width,
892 exterior_size = MIN (width, height);
894 if (exterior_size % 2 == 0) /* Ensure odd */
897 /* FIXME: thickness */
899 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
900 interior_size = MAX (1, exterior_size - 2 * pad);
902 if (interior_size < 7)
905 pad = MAX (0, (exterior_size - interior_size) / 2);
908 x -= (1 + exterior_size - (gint) width) / 2;
909 y -= (1 + exterior_size - (gint) height) / 2;
911 if (border_style == GTK_BORDER_STYLE_SOLID)
913 cairo_set_line_width (cr, border_width);
915 cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
916 gdk_cairo_set_source_rgba (cr, bg_color);
917 cairo_fill_preserve (cr);
920 gdk_cairo_set_source_rgba (cr, border_color);
922 gdk_cairo_set_source_rgba (cr, fg_color);
927 gdk_cairo_set_source_rgba (cr, fg_color);
929 if (flags & GTK_STATE_FLAG_INCONSISTENT)
931 int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
935 y + pad + (1 + interior_size - line_thickness) / 2,
945 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
947 if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
955 cairo_scale (cr, interior_size / 7., interior_size / 7.);
957 cairo_rectangle (cr, 0, 0, 7 * progress, 7);
960 cairo_move_to (cr, 7.0, 0.0);
961 cairo_line_to (cr, 7.5, 1.0);
962 cairo_curve_to (cr, 5.3, 2.0,
965 cairo_curve_to (cr, 3.0, 5.7,
968 cairo_line_to (cr, 0.2, 3.5);
969 cairo_curve_to (cr, 1.1, 3.5,
972 cairo_curve_to (cr, 1.0, 3.9,
975 cairo_curve_to (cr, 3.5, 3.1,
985 gdk_rgba_free (fg_color);
986 gdk_rgba_free (bg_color);
987 gdk_rgba_free (border_color);
991 gtk_theming_engine_render_option (GtkThemingEngine *engine,
999 GdkRGBA *fg_color, *bg_color, *border_color;
1000 gint exterior_size, interior_size, pad, thickness, border_width;
1001 GtkBorderStyle border_style;
1004 flags = gtk_theming_engine_get_state (engine);
1005 radius = MIN (width, height) / 2 - 0.5;
1009 gtk_theming_engine_get (engine, flags,
1011 "background-color", &bg_color,
1012 "border-color", &border_color,
1013 "border-style", &border_style,
1014 "border-width", &border_width,
1017 exterior_size = MIN (width, height);
1019 if (exterior_size % 2 == 0) /* Ensure odd */
1022 x -= (1 + exterior_size - width) / 2;
1023 y -= (1 + exterior_size - height) / 2;
1025 if (border_style == GTK_BORDER_STYLE_SOLID)
1027 cairo_set_line_width (cr, border_width);
1029 x + exterior_size / 2.,
1030 y + exterior_size / 2.,
1031 (exterior_size - 1) / 2.,
1034 gdk_cairo_set_source_rgba (cr, bg_color);
1035 cairo_fill_preserve (cr);
1038 gdk_cairo_set_source_rgba (cr, border_color);
1040 gdk_cairo_set_source_rgba (cr, fg_color);
1045 gdk_cairo_set_source_rgba (cr, fg_color);
1047 /* FIXME: thickness */
1050 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1052 gint line_thickness;
1054 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1055 interior_size = MAX (1, exterior_size - 2 * pad);
1057 if (interior_size < 7)
1060 pad = MAX (0, (exterior_size - interior_size) / 2);
1063 line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1065 cairo_rectangle (cr,
1067 y + pad + (interior_size - line_thickness) / 2.,
1072 if (flags & GTK_STATE_FLAG_ACTIVE)
1074 pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1075 interior_size = MAX (1, exterior_size - 2 * pad);
1077 if (interior_size < 5)
1080 pad = MAX (0, (exterior_size - interior_size) / 2);
1084 x + pad + interior_size / 2.,
1085 y + pad + interior_size / 2.,
1093 gdk_rgba_free (fg_color);
1094 gdk_rgba_free (bg_color);
1095 gdk_rgba_free (border_color);
1099 add_path_arrow (cairo_t *cr,
1107 cairo_translate (cr, x + (size / 2), y + (size / 2));
1108 cairo_rotate (cr, angle);
1110 cairo_move_to (cr, 0, - (size / 4));
1111 cairo_line_to (cr, - (size / 2), (size / 4));
1112 cairo_line_to (cr, (size / 2), (size / 4));
1113 cairo_close_path (cr);
1119 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1126 GtkStateFlags flags;
1131 flags = gtk_theming_engine_get_state (engine);
1133 gtk_theming_engine_get (engine, flags,
1137 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1139 add_path_arrow (cr, angle, x + 1, y + 1, size);
1140 cairo_set_source_rgb (cr, 1, 1, 1);
1144 add_path_arrow (cr, angle, x, y, size);
1145 gdk_cairo_set_source_rgba (cr, fg_color);
1150 gdk_rgba_free (fg_color);
1154 add_path_line (cairo_t *cr,
1160 /* Adjust endpoints */
1174 cairo_move_to (cr, x1, y1);
1175 cairo_line_to (cr, x2, y2);
1179 color_shade (const GdkRGBA *color,
1181 GdkRGBA *color_return)
1183 GtkSymbolicColor *literal, *shade;
1185 literal = gtk_symbolic_color_new_literal (color);
1186 shade = gtk_symbolic_color_new_shade (literal, factor);
1187 gtk_symbolic_color_unref (literal);
1189 gtk_symbolic_color_resolve (shade, NULL, color_return);
1190 gtk_symbolic_color_unref (shade);
1194 _cairo_round_rectangle_sides (cairo_t *cr,
1201 GtkJunctionSides junction)
1203 radius = CLAMP (radius, 0, MIN (width / 2, height / 2));
1205 if (sides & SIDE_RIGHT)
1208 (junction & GTK_JUNCTION_CORNER_TOPRIGHT))
1209 cairo_move_to (cr, x + width, y);
1212 cairo_new_sub_path (cr);
1213 cairo_arc (cr, x + width - radius, y + radius, radius, - G_PI / 4, 0);
1217 (junction & GTK_JUNCTION_CORNER_BOTTOMRIGHT))
1218 cairo_line_to (cr, x + width, y + height);
1220 cairo_arc (cr, x + width - radius, y + height - radius, radius, 0, G_PI / 4);
1223 if (sides & SIDE_BOTTOM)
1226 ! (junction & GTK_JUNCTION_CORNER_BOTTOMRIGHT))
1228 if ((sides & SIDE_RIGHT) == 0)
1229 cairo_new_sub_path (cr);
1231 cairo_arc (cr, x + width - radius, y + height - radius, radius, G_PI / 4, G_PI / 2);
1233 else if ((sides & SIDE_RIGHT) == 0)
1234 cairo_move_to (cr, x + width, y + height);
1237 (junction & GTK_JUNCTION_CORNER_BOTTOMLEFT))
1238 cairo_line_to (cr, x, y + height);
1240 cairo_arc (cr, x + radius, y + height - radius, radius, G_PI / 2, 3 * (G_PI / 4));
1243 cairo_move_to (cr, x, y + height);
1245 if (sides & SIDE_LEFT)
1248 ! (junction & GTK_JUNCTION_CORNER_BOTTOMLEFT))
1250 if ((sides & SIDE_BOTTOM) == 0)
1251 cairo_new_sub_path (cr);
1253 cairo_arc (cr, x + radius, y + height - radius, radius, 3 * (G_PI / 4), G_PI);
1255 else if ((sides & SIDE_BOTTOM) == 0)
1256 cairo_move_to (cr, x, y + height);
1259 (junction & GTK_JUNCTION_CORNER_TOPLEFT))
1260 cairo_line_to (cr, x, y);
1262 cairo_arc (cr, x + radius, y + radius, radius, G_PI, G_PI + G_PI / 4);
1265 if (sides & SIDE_TOP)
1268 ! (junction & GTK_JUNCTION_CORNER_TOPLEFT))
1270 if ((sides & SIDE_LEFT) == 0)
1271 cairo_new_sub_path (cr);
1273 cairo_arc (cr, x + radius, y + radius, radius, 5 * (G_PI / 4), 3 * (G_PI / 2));
1275 else if ((sides & SIDE_LEFT) == 0)
1276 cairo_move_to (cr, x, y);
1279 (junction & GTK_JUNCTION_CORNER_TOPRIGHT))
1280 cairo_line_to (cr, x + width, y);
1282 cairo_arc (cr, x + width - radius, y + radius, radius, 3 * (G_PI / 2), - G_PI / 4);
1287 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1295 cairo_pattern_t *pattern;
1296 GtkStateFlags flags;
1298 gdouble progress, alpha = 1;
1299 GtkJunctionSides junction;
1300 gint radius, border_width;
1302 flags = gtk_theming_engine_get_state (engine);
1303 junction = gtk_theming_engine_get_junction_sides (engine);
1306 if (gtk_theming_engine_has_class (engine, "spinbutton") &&
1307 gtk_theming_engine_has_class (engine, "button"))
1315 gtk_theming_engine_get (engine, flags,
1316 "background-image", &pattern,
1317 "background-color", &bg_color,
1318 "border-width", &border_width,
1319 "border-radius", &radius,
1322 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1324 if (border_width > 0)
1328 width -= 2 * border_width;
1329 height -= 2 * border_width;
1330 radius -= 2 * border_width;
1336 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1337 x, y, width, height,
1338 SIDE_ALL, junction);
1341 if (gtk_theming_engine_has_class (engine, "background"))
1343 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
1344 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1348 cairo_translate (cr, x, y);
1349 cairo_scale (cr, width, height);
1353 cairo_pattern_t *other_pattern;
1354 GtkStateFlags other_flags;
1356 cairo_pattern_t *new_pattern = NULL;
1358 if (flags & GTK_STATE_FLAG_PRELIGHT)
1360 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
1361 progress = 1 - progress;
1364 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
1366 gtk_theming_engine_get (engine, other_flags,
1367 "background-image", &other_pattern,
1368 "background-color", &other_bg,
1371 if (pattern && other_pattern)
1373 cairo_pattern_type_t type, other_type;
1376 cairo_pattern_get_color_stop_count (pattern, &n0);
1377 cairo_pattern_get_color_stop_count (other_pattern, &n1);
1378 type = cairo_pattern_get_type (pattern);
1379 other_type = cairo_pattern_get_type (other_pattern);
1381 if (type == other_type && n0 == n1)
1383 gdouble offset0, red0, green0, blue0, alpha0;
1384 gdouble offset1, red1, green1, blue1, alpha1;
1385 gdouble x00, x01, y00, y01, x10, x11, y10, y11;
1386 gdouble r00, r01, r10, r11;
1389 if (type == CAIRO_PATTERN_TYPE_LINEAR)
1391 cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
1392 cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
1394 new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
1395 y00 + (y10 - y00) * progress,
1396 x01 + (x11 - x01) * progress,
1397 y01 + (y11 - y01) * progress);
1401 cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
1402 cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
1404 new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
1405 y00 + (y10 - y00) * progress,
1406 r00 + (r10 - r00) * progress,
1407 x01 + (x11 - x01) * progress,
1408 y01 + (y11 - y01) * progress,
1409 r01 + (r11 - r01) * progress);
1412 cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
1415 /* Blend both gradients into one */
1416 while (i < n0 && i < n1)
1418 cairo_pattern_get_color_stop_rgba (pattern, i,
1420 &red0, &green0, &blue0,
1422 cairo_pattern_get_color_stop_rgba (other_pattern, i,
1424 &red1, &green1, &blue1,
1427 cairo_pattern_add_color_stop_rgba (new_pattern,
1428 offset0 + ((offset1 - offset0) * progress),
1429 red0 + ((red1 - red0) * progress),
1430 green0 + ((green1 - green0) * progress),
1431 blue0 + ((blue1 - blue0) * progress),
1432 alpha0 + ((alpha1 - alpha0) * progress));
1438 /* Different pattern types, or different color
1439 * stop counts, alpha blend both patterns.
1441 cairo_rectangle (cr, 0, 0, 1, 1);
1442 cairo_set_source (cr, other_pattern);
1443 cairo_fill_preserve (cr);
1445 /* Set alpha for posterior drawing
1446 * of the target pattern
1448 alpha = 1 - progress;
1451 else if (pattern || other_pattern)
1455 gdouble x0, y0, x1, y1, r0, r1;
1458 /* Blend a pattern with a color */
1463 progress = 1 - progress;
1471 if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
1473 cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
1474 new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
1478 cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
1479 new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
1482 cairo_pattern_get_color_stop_count (p, &n);
1484 for (i = 0; i < n; i++)
1486 gdouble red1, green1, blue1, alpha1;
1489 cairo_pattern_get_color_stop_rgba (p, i,
1491 &red1, &green1, &blue1,
1493 cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1494 c->red + ((red1 - c->red) * progress),
1495 c->green + ((green1 - c->green) * progress),
1496 c->blue + ((blue1 - c->blue) * progress),
1497 c->alpha + ((alpha1 - c->alpha) * progress));
1502 const GdkRGBA *color, *other_color;
1504 /* Merge just colors */
1506 other_color = other_bg;
1508 new_pattern = cairo_pattern_create_rgba (CLAMP (color->red + ((other_color->red - color->red) * progress), 0, 1),
1509 CLAMP (color->green + ((other_color->green - color->green) * progress), 0, 1),
1510 CLAMP (color->blue + ((other_color->blue - color->blue) * progress), 0, 1),
1511 CLAMP (color->alpha + ((other_color->alpha - color->alpha) * progress), 0, 1));
1516 /* Replace pattern to use */
1517 cairo_pattern_destroy (pattern);
1518 pattern = new_pattern;
1522 cairo_pattern_destroy (other_pattern);
1525 gdk_rgba_free (other_bg);
1528 cairo_rectangle (cr, 0, 0, 1, 1);
1532 cairo_set_source (cr, pattern);
1533 cairo_pattern_destroy (pattern);
1536 gdk_cairo_set_source_rgba (cr, bg_color);
1542 cairo_pattern_t *mask;
1544 mask = cairo_pattern_create_rgba (1, 1, 1, alpha);
1545 cairo_mask (cr, mask);
1546 cairo_pattern_destroy (mask);
1551 gdk_rgba_free (bg_color);
1554 /* Renders the small triangle on corners so
1555 * frames with 0 radius have a 3D-like effect
1558 _cairo_corner_triangle (cairo_t *cr,
1565 cairo_move_to (cr, x + 0.5, y + size - 0.5);
1566 cairo_line_to (cr, x + size - 0.5, y + size - 0.5);
1567 cairo_line_to (cr, x + size - 0.5, y + 0.5);
1569 for (i = 1; i < size - 1; i++)
1571 cairo_move_to (cr, x + size - 0.5, y + i + 0.5);
1572 cairo_line_to (cr, x + (size - i) - 0.5, y + i + 0.5);
1577 render_frame_internal (GtkThemingEngine *engine,
1584 GtkJunctionSides junction)
1586 GtkStateFlags state;
1588 GdkRGBA *border_color;
1589 GtkBorderStyle border_style;
1590 gint border_width, radius;
1593 state = gtk_theming_engine_get_state (engine);
1594 gtk_theming_engine_get (engine, state,
1595 "border-color", &border_color,
1596 "border-style", &border_style,
1597 "border-width", &border_width,
1598 "border-radius", &radius,
1603 color_shade (border_color, 1.8, &lighter);
1605 switch (border_style)
1607 case GTK_BORDER_STYLE_NONE:
1609 case GTK_BORDER_STYLE_SOLID:
1610 cairo_set_line_width (cr, border_width);
1611 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1613 if (border_width > 1)
1615 x += (gdouble) border_width / 2;
1616 y += (gdouble) border_width / 2;
1617 width -= border_width;
1618 height -= border_width;
1620 else if (border_width == 1)
1628 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1629 x, y, width, height,
1630 SIDE_ALL & ~(hidden_side),
1632 gdk_cairo_set_source_rgba (cr, border_color);
1636 case GTK_BORDER_STYLE_INSET:
1637 case GTK_BORDER_STYLE_OUTSET:
1638 cairo_set_line_width (cr, border_width);
1641 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1643 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
1645 if (border_width > 1)
1647 d1 = (gdouble) border_width / 2;
1658 m = MIN (width, height);
1661 if (border_style == GTK_BORDER_STYLE_INSET)
1662 gdk_cairo_set_source_rgba (cr, &lighter);
1664 gdk_cairo_set_source_rgba (cr, border_color);
1666 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1668 width - d2, height - d2,
1669 (SIDE_BOTTOM | SIDE_RIGHT) & ~(hidden_side),
1673 if (border_style == GTK_BORDER_STYLE_INSET)
1674 gdk_cairo_set_source_rgba (cr, border_color);
1676 gdk_cairo_set_source_rgba (cr, &lighter);
1678 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1680 width - d2, height - d2,
1681 (SIDE_TOP | SIDE_LEFT) & ~(hidden_side),
1688 /* overprint top/right and bottom/left corner
1689 * triangles, to give the box a 3D-like appearance
1691 if (border_style == GTK_BORDER_STYLE_INSET)
1692 gdk_cairo_set_source_rgba (cr, &lighter);
1694 gdk_cairo_set_source_rgba (cr, border_color);
1698 cairo_set_line_width (cr, 1);
1700 _cairo_corner_triangle (cr,
1701 x + width - border_width, y,
1703 _cairo_corner_triangle (cr,
1704 x, y + height - border_width,
1717 gdk_rgba_free (border_color);
1721 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1728 GtkStateFlags flags;
1730 GtkBorderStyle border_style;
1731 GtkJunctionSides junction;
1733 flags = gtk_theming_engine_get_state (engine);
1734 junction = gtk_theming_engine_get_junction_sides (engine);
1736 gtk_theming_engine_get (engine, flags,
1737 "border-image", &slice,
1738 "border-style", &border_style,
1743 gtk_9slice_render (slice, cr, x, y, width, height);
1744 gtk_9slice_unref (slice);
1746 else if (border_style != GTK_BORDER_STYLE_NONE)
1747 render_frame_internal (engine, cr,
1748 x, y, width, height,
1753 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1760 GtkStateFlags flags;
1761 GdkRGBA *outline_color, *fg_color;
1762 double vertical_overshoot;
1765 double interp; /* interpolation factor for center position */
1766 double x_double_horz, y_double_horz;
1767 double x_double_vert, y_double_vert;
1768 double x_double, y_double;
1773 flags = gtk_theming_engine_get_state (engine);
1775 gtk_theming_engine_get (engine, flags,
1778 gtk_theming_engine_get (engine, 0,
1779 "color", &outline_color,
1784 /* FIXME: LTR/RTL */
1785 if (flags & GTK_STATE_FLAG_ACTIVE)
1796 /* Compute distance that the stroke extends beyonds the end
1797 * of the triangle we draw.
1799 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1801 /* For odd line widths, we end the vertical line of the triangle
1802 * at a half pixel, so we round differently.
1804 if (line_width % 2 == 1)
1805 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1807 vertical_overshoot = ceil (vertical_overshoot);
1809 /* Adjust the size of the triangle we draw so that the entire stroke fits
1811 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1813 /* If the line width is odd, we want the diameter to be even,
1814 * and vice versa, so force the sum to be odd. This relationship
1815 * makes the point of the triangle look right.
1817 diameter -= (1 - (diameter + line_width) % 2);
1819 radius = diameter / 2.;
1821 /* Adjust the center so that the stroke is properly aligned with
1822 * the pixel grid. The center adjustment is different for the
1823 * horizontal and vertical orientations. For intermediate positions
1824 * we interpolate between the two.
1826 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1827 y_double_vert = (y + height / 2) - 0.5;
1829 x_double_horz = (x + width / 2) - 0.5;
1830 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1832 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1833 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1835 cairo_translate (cr, x_double, y_double);
1836 cairo_rotate (cr, angle);
1838 cairo_move_to (cr, - radius / 2., - radius);
1839 cairo_line_to (cr, radius / 2., 0);
1840 cairo_line_to (cr, - radius / 2., radius);
1841 cairo_close_path (cr);
1843 cairo_set_line_width (cr, line_width);
1845 gdk_cairo_set_source_rgba (cr, fg_color);
1847 cairo_fill_preserve (cr);
1849 gdk_cairo_set_source_rgba (cr, outline_color);
1854 gdk_rgba_free (fg_color);
1855 gdk_rgba_free (outline_color);
1859 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1866 GtkStateFlags flags;
1872 flags = gtk_theming_engine_get_state (engine);
1874 gtk_theming_engine_get (engine, flags,
1878 gtk_theming_engine_get_style (engine,
1879 "focus-line-width", &line_width,
1880 "focus-line-pattern", (gchar *) &dash_list,
1883 cairo_set_line_width (cr, (gdouble) line_width);
1887 gint n_dashes = strlen ((const gchar *) dash_list);
1888 gdouble *dashes = g_new (gdouble, n_dashes);
1889 gdouble total_length = 0;
1890 gdouble dash_offset;
1893 for (i = 0; i < n_dashes; i++)
1895 dashes[i] = dash_list[i];
1896 total_length += dash_list[i];
1899 /* The dash offset here aligns the pattern to integer pixels
1900 * by starting the dash at the right side of the left border
1901 * Negative dash offsets in cairo don't work
1902 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
1904 dash_offset = - line_width / 2.;
1906 while (dash_offset < 0)
1907 dash_offset += total_length;
1909 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
1913 cairo_rectangle (cr,
1914 x + line_width / 2.,
1915 y + line_width / 2.,
1917 height - line_width);
1919 gdk_cairo_set_source_rgba (cr, color);
1924 gdk_rgba_free (color);
1929 gtk_theming_engine_render_line (GtkThemingEngine *engine,
1936 GdkRGBA *bg_color, darker, lighter;
1937 GtkStateFlags flags;
1938 gint i, thickness, thickness_dark, thickness_light, len;
1939 cairo_matrix_t matrix;
1942 /* FIXME: thickness */
1944 thickness_dark = thickness / 2;
1945 thickness_light = thickness - thickness_dark;
1947 flags = gtk_theming_engine_get_state (engine);
1950 gtk_theming_engine_get (engine, flags,
1951 "background-color", &bg_color,
1953 color_shade (bg_color, 0.7, &darker);
1954 color_shade (bg_color, 1.3, &lighter);
1956 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1957 cairo_set_line_width (cr, 1);
1959 angle = atan2 (x1 - x0, y1 - y0);
1960 angle = (2 * G_PI) - angle;
1963 cairo_get_matrix (cr, &matrix);
1964 cairo_matrix_translate (&matrix, x0, y0);
1965 cairo_matrix_rotate (&matrix, angle);
1966 cairo_set_matrix (cr, &matrix);
1971 len = (gint) sqrt ((x1 * x1) + (y1 * y1));
1973 y0 = -thickness_dark;
1975 for (i = 0; i < thickness_dark; i++)
1977 gdk_cairo_set_source_rgba (cr, &lighter);
1978 add_path_line (cr, len - i - 1.5, y0, len - 0.5, y0);
1981 gdk_cairo_set_source_rgba (cr, &darker);
1982 add_path_line (cr, 0.5, y0, len - i - 1.5, y0);
1988 for (i = 0; i < thickness_light; i++)
1990 gdk_cairo_set_source_rgba (cr, &darker);
1991 add_path_line (cr, 0.5, y0, thickness_light - i + 0.5, y0);
1994 gdk_cairo_set_source_rgba (cr, &lighter);
1995 add_path_line (cr, thickness_light - i + 0.5, y0, len - 0.5, y0);
2003 gdk_rgba_free (bg_color);
2007 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2011 PangoLayout *layout)
2013 const PangoMatrix *matrix;
2015 GtkStateFlags flags;
2019 flags = gtk_theming_engine_get_state (engine);
2021 gtk_theming_engine_get (engine, flags,
2025 screen = gtk_theming_engine_get_screen (engine);
2026 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2030 cairo_matrix_t cairo_matrix;
2031 PangoMatrix tmp_matrix;
2032 PangoRectangle rect;
2034 cairo_matrix_init (&cairo_matrix,
2035 matrix->xx, matrix->yx,
2036 matrix->xy, matrix->yy,
2037 matrix->x0, matrix->y0);
2039 pango_layout_get_extents (layout, NULL, &rect);
2040 pango_matrix_transform_rectangle (matrix, &rect);
2041 pango_extents_to_pixels (&rect, NULL);
2043 tmp_matrix = *matrix;
2044 cairo_matrix.x0 += x - rect.x;
2045 cairo_matrix.y0 += y - rect.y;
2047 cairo_set_matrix (cr, &cairo_matrix);
2050 cairo_translate (cr, x, y);
2052 if (flags & GTK_STATE_FLAG_INSENSITIVE)
2055 cairo_set_source_rgb (cr, 1, 1, 1);
2056 cairo_move_to (cr, 1, 1);
2057 _gtk_pango_fill_layout (cr, layout);
2061 gdk_cairo_set_source_rgba (cr, fg_color);
2062 pango_cairo_show_layout (cr, layout);
2066 gdk_rgba_free (fg_color);
2070 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2076 GtkOrientation orientation)
2078 const GtkWidgetPath *path;
2081 path = gtk_theming_engine_get_path (engine);
2083 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2084 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2086 /* FIXME: thickness */
2089 if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2091 if (orientation == GTK_ORIENTATION_VERTICAL)
2092 gtk_theming_engine_render_line (engine, cr,
2094 y + (gint) height / 2,
2095 x + width - thickness - 1,
2096 y + (gint) height / 2);
2098 gtk_theming_engine_render_line (engine, cr,
2099 x + (gint) width / 2,
2101 x + (gint) width / 2,
2102 y + height - thickness - 1);
2107 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2113 GtkPositionType gap_side,
2117 GtkJunctionSides junction = 0;
2118 GtkStateFlags state;
2120 gdouble x0, y0, x1, y1, xc, yc, wc, hc;
2122 state = gtk_theming_engine_get_state (engine);
2123 gtk_theming_engine_get (engine, state,
2124 "border-width", &border_width,
2132 xc = x + xy0_gap + border_width;
2134 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2136 junction = GTK_JUNCTION_TOP;
2138 case GTK_POS_BOTTOM:
2139 xc = x + xy0_gap + border_width;
2140 yc = y + height - border_width;
2141 wc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2143 junction = GTK_JUNCTION_BOTTOM;
2147 yc = y + xy0_gap + border_width;
2149 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2150 junction = GTK_JUNCTION_LEFT;
2153 xc = x + width - border_width;
2154 yc = y + xy0_gap + border_width;
2156 hc = MAX (xy1_gap - xy0_gap - 2 * border_width, 0);
2157 junction = GTK_JUNCTION_RIGHT;
2161 cairo_clip_extents (cr, &x0, &y0, &x1, &y1);
2162 cairo_rectangle (cr, x0, y0, x1 - x0, yc - y0);
2163 cairo_rectangle (cr, x0, yc, xc - x0, hc);
2164 cairo_rectangle (cr, xc + wc, yc, x1 - (xc + wc), hc);
2165 cairo_rectangle (cr, x0, yc + hc, x1 - x0, y1 - (yc + hc));
2168 render_frame_internal (engine, cr,
2169 x, y, width, height,
2176 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2182 GtkPositionType gap_side)
2184 GtkJunctionSides junction = 0;
2185 GtkStateFlags state;
2186 guint hidden_side = 0;
2195 junction = GTK_JUNCTION_LEFT;
2196 hidden_side = SIDE_LEFT;
2199 junction = GTK_JUNCTION_RIGHT;
2200 hidden_side = SIDE_RIGHT;
2203 junction = GTK_JUNCTION_TOP;
2204 hidden_side = SIDE_TOP;
2206 case GTK_POS_BOTTOM:
2207 junction = GTK_JUNCTION_BOTTOM;
2208 hidden_side = SIDE_BOTTOM;
2212 state = gtk_theming_engine_get_state (engine);
2213 gtk_theming_engine_get (engine, state,
2214 "background-color", &bg_color,
2215 "border-radius", &radius,
2218 _cairo_round_rectangle_sides (cr, radius,
2219 x, y, width, height,
2220 SIDE_ALL, junction);
2221 gdk_cairo_set_source_rgba (cr, bg_color);
2224 render_frame_internal (engine, cr,
2225 x, y, width, height,
2226 hidden_side, junction);
2231 gdk_rgba_free (bg_color);
2235 render_dot (cairo_t *cr,
2236 const GdkRGBA *lighter,
2237 const GdkRGBA *darker,
2242 size = CLAMP ((gint) size, 2, 3);
2246 gdk_cairo_set_source_rgba (cr, lighter);
2247 cairo_rectangle (cr, x, y, 1, 1);
2248 cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2253 gdk_cairo_set_source_rgba (cr, lighter);
2254 cairo_rectangle (cr, x, y, 2, 1);
2255 cairo_rectangle (cr, x, y, 1, 2);
2258 gdk_cairo_set_source_rgba (cr, darker);
2259 cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2260 cairo_rectangle (cr, x + 2, y, 1, 2);
2266 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2273 GtkStateFlags flags;
2275 GdkRGBA lighter, darker;
2279 flags = gtk_theming_engine_get_state (engine);
2281 cairo_set_line_width (cr, 1);
2283 gtk_theming_engine_get (engine, flags,
2284 "background-color", &bg_color,
2286 color_shade (bg_color, 0.7, &darker);
2287 color_shade (bg_color, 1.3, &lighter);
2289 gdk_cairo_set_source_rgba (cr, bg_color);
2290 cairo_rectangle (cr, x, y, width, height);
2293 if (gtk_theming_engine_has_class (engine, "grip"))
2295 GtkJunctionSides sides;
2300 cairo_set_line_width (cr, 1.0);
2301 sides = gtk_theming_engine_get_junction_sides (engine);
2303 /* reduce confusing values to a meaningful state */
2304 if ((sides & (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT)) == (GTK_JUNCTION_CORNER_TOPLEFT | GTK_JUNCTION_CORNER_BOTTOMRIGHT))
2305 sides &= ~GTK_JUNCTION_CORNER_TOPLEFT;
2307 if ((sides & (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT)) == (GTK_JUNCTION_CORNER_TOPRIGHT | GTK_JUNCTION_CORNER_BOTTOMLEFT))
2308 sides &= ~GTK_JUNCTION_CORNER_TOPRIGHT;
2311 sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
2313 /* align drawing area to the connected side */
2314 if (sides == GTK_JUNCTION_LEFT)
2319 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2323 else if (height < width)
2328 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2330 /* make it square, aligning to bottom left */
2333 y += (height - width);
2336 else if (height < width)
2341 else if (sides == GTK_JUNCTION_RIGHT)
2343 /* aligning to right */
2346 x += (width - height);
2350 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2354 else if (height < width)
2356 x += (width - height);
2362 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2364 /* make it square, aligning to bottom right */
2367 y += (height - width);
2370 else if (height < width)
2372 x += (width - height);
2378 else if (sides == GTK_JUNCTION_TOP)
2383 else if (sides == GTK_JUNCTION_BOTTOM)
2385 /* align to bottom */
2388 y += (height - width);
2393 g_assert_not_reached ();
2395 if (sides == GTK_JUNCTION_LEFT ||
2396 sides == GTK_JUNCTION_RIGHT)
2402 while (xi < x + width)
2404 gdk_cairo_set_source_rgba (cr, &lighter);
2405 add_path_line (cr, x, y, x, y + height);
2409 gdk_cairo_set_source_rgba (cr, &darker);
2410 add_path_line (cr, xi, y, xi, y + height);
2415 else if (sides == GTK_JUNCTION_TOP ||
2416 sides == GTK_JUNCTION_BOTTOM)
2422 while (yi < y + height)
2424 gdk_cairo_set_source_rgba (cr, &lighter);
2425 add_path_line (cr, x, yi, x + width, yi);
2429 gdk_cairo_set_source_rgba (cr, &darker);
2430 add_path_line (cr, x, yi, x + width, yi);
2435 else if (sides == GTK_JUNCTION_CORNER_TOPLEFT)
2444 gdk_cairo_set_source_rgba (cr, &darker);
2445 add_path_line (cr, xi, y, x, yi);
2451 add_path_line (cr, xi, y, x, yi);
2457 gdk_cairo_set_source_rgba (cr, &lighter);
2458 add_path_line (cr, xi, y, x, yi);
2465 else if (sides == GTK_JUNCTION_CORNER_TOPRIGHT)
2472 while (xi < (x + width - 3))
2474 gdk_cairo_set_source_rgba (cr, &lighter);
2475 add_path_line (cr, xi, y, x + width, yi);
2481 gdk_cairo_set_source_rgba (cr, &darker);
2482 add_path_line (cr, xi, y, x + width, yi);
2488 add_path_line (cr, xi, y, x + width, yi);
2495 else if (sides == GTK_JUNCTION_CORNER_BOTTOMLEFT)
2504 gdk_cairo_set_source_rgba (cr, &darker);
2505 add_path_line (cr, x, yi, xi, y + height);
2511 add_path_line (cr, x, yi, xi, y + height);
2517 gdk_cairo_set_source_rgba (cr, &lighter);
2518 add_path_line (cr, x, yi, xi, y + height);
2525 else if (sides == GTK_JUNCTION_CORNER_BOTTOMRIGHT)
2532 while (xi < (x + width - 3))
2534 gdk_cairo_set_source_rgba (cr, &lighter);
2535 add_path_line (cr, xi, y + height, x + width, yi);
2541 gdk_cairo_set_source_rgba (cr, &darker);
2542 add_path_line (cr, xi, y + height, x + width, yi);
2548 add_path_line (cr, xi, y + height, x + width, yi);
2558 else if (gtk_theming_engine_has_class (engine, "paned"))
2561 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2562 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2564 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2565 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2569 for (yy = y; yy < y + height; yy += 3)
2570 for (xx = x; xx < x + width; xx += 6)
2572 render_dot (cr, &lighter, &darker, xx, yy, 2);
2573 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2579 gdk_rgba_free (bg_color);
2583 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2590 if (gtk_theming_engine_has_class (engine, "spinner"))
2592 GtkStateFlags state;
2593 guint num_steps, step;
2603 gtk_theming_engine_get_style (engine,
2604 "num-steps", &num_steps,
2607 state = gtk_theming_engine_get_state (engine);
2608 gtk_theming_engine_get (engine, state,
2614 if (gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2615 step = (guint) (progress * num_steps);
2621 cairo_translate (cr, x, y);
2623 /* draw clip region */
2624 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2628 radius = MIN (width / 2, height / 2);
2629 half = num_steps / 2;
2631 for (i = 0; i < num_steps; i++)
2633 gint inset = 0.7 * radius;
2635 /* transparency is a function of time and intial value */
2636 gdouble t = (gdouble) ((i + num_steps - step)
2637 % num_steps) / num_steps;
2641 cairo_set_source_rgba (cr,
2647 cairo_set_line_width (cr, 2.0);
2649 dx + (radius - inset) * cos (i * G_PI / half),
2650 dy + (radius - inset) * sin (i * G_PI / half));
2652 dx + radius * cos (i * G_PI / half),
2653 dy + radius * sin (i * G_PI / half));
2661 gdk_rgba_free (color);
2665 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2666 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2671 scale_or_ref (GdkPixbuf *src,
2675 if (width == gdk_pixbuf_get_width (src) &&
2676 height == gdk_pixbuf_get_height (src))
2677 return g_object_ref (src);
2679 return gdk_pixbuf_scale_simple (src,
2681 GDK_INTERP_BILINEAR);
2685 lookup_icon_size (GtkThemingEngine *engine,
2691 GtkSettings *settings;
2693 screen = gtk_theming_engine_get_screen (engine);
2694 settings = gtk_settings_get_for_screen (screen);
2696 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2700 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2701 const GtkIconSource *source,
2706 GdkPixbuf *base_pixbuf;
2707 GtkStateFlags state;
2711 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2712 state = gtk_theming_engine_get_state (engine);
2714 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2716 if (size != (GtkIconSize) -1 &&
2717 !lookup_icon_size (engine, size, &width, &height))
2719 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2723 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2726 if (size != (GtkIconSize) -1 &&
2727 gtk_icon_source_get_size_wildcarded (source))
2728 scaled = scale_or_ref (base_pixbuf, width, height);
2730 scaled = g_object_ref (base_pixbuf);
2732 /* If the state was wildcarded, then generate a state. */
2733 if (gtk_icon_source_get_state_wildcarded (source))
2735 if (state & GTK_STATE_FLAG_INSENSITIVE)
2737 stated = gdk_pixbuf_copy (scaled);
2738 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2740 g_object_unref (scaled);
2742 else if (state & GTK_STATE_FLAG_PRELIGHT)
2744 stated = gdk_pixbuf_copy (scaled);
2745 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2747 g_object_unref (scaled);