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/.
50 typedef struct GtkThemingEnginePrivate GtkThemingEnginePrivate;
65 struct GtkThemingEnginePrivate
67 GtkStyleContext *context;
71 #define GTK_THEMING_ENGINE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_THEMING_ENGINE, GtkThemingEnginePrivate))
73 static void gtk_theming_engine_finalize (GObject *object);
74 static void gtk_theming_engine_impl_set_property (GObject *object,
78 static void gtk_theming_engine_impl_get_property (GObject *object,
83 static void gtk_theming_engine_render_check (GtkThemingEngine *engine,
89 static void gtk_theming_engine_render_option (GtkThemingEngine *engine,
95 static void gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
101 static void gtk_theming_engine_render_background (GtkThemingEngine *engine,
107 static void gtk_theming_engine_render_frame (GtkThemingEngine *engine,
113 static void gtk_theming_engine_render_expander (GtkThemingEngine *engine,
119 static void gtk_theming_engine_render_focus (GtkThemingEngine *engine,
125 static void gtk_theming_engine_render_layout (GtkThemingEngine *engine,
129 PangoLayout *layout);
130 static void gtk_theming_engine_render_line (GtkThemingEngine *engine,
136 static void gtk_theming_engine_render_slider (GtkThemingEngine *engine,
142 GtkOrientation orientation);
143 static void gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
149 GtkPositionType gap_side,
152 static void gtk_theming_engine_render_extension (GtkThemingEngine *engine,
158 GtkPositionType gap_side);
159 static void gtk_theming_engine_render_handle (GtkThemingEngine *engine,
165 static void gtk_theming_engine_render_activity (GtkThemingEngine *engine,
171 static GdkPixbuf * gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
172 const GtkIconSource *source,
175 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
178 typedef struct GtkThemingModule GtkThemingModule;
179 typedef struct GtkThemingModuleClass GtkThemingModuleClass;
181 struct GtkThemingModule
183 GTypeModule parent_instance;
187 void (*init) (GTypeModule *module);
189 GtkThemingEngine * (*create_engine) (void);
192 struct GtkThemingModuleClass
194 GTypeModuleClass parent_class;
197 #define GTK_TYPE_THEMING_MODULE (gtk_theming_module_get_type ())
198 #define GTK_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_THEMING_MODULE, GtkThemingModule))
199 #define GTK_IS_THEMING_MODULE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_THEMING_MODULE))
201 G_DEFINE_TYPE (GtkThemingModule, gtk_theming_module, G_TYPE_TYPE_MODULE);
204 gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
206 GObjectClass *object_class = G_OBJECT_CLASS (klass);
208 object_class->finalize = gtk_theming_engine_finalize;
209 object_class->set_property = gtk_theming_engine_impl_set_property;
210 object_class->get_property = gtk_theming_engine_impl_get_property;
212 klass->render_check = gtk_theming_engine_render_check;
213 klass->render_option = gtk_theming_engine_render_option;
214 klass->render_arrow = gtk_theming_engine_render_arrow;
215 klass->render_background = gtk_theming_engine_render_background;
216 klass->render_frame = gtk_theming_engine_render_frame;
217 klass->render_expander = gtk_theming_engine_render_expander;
218 klass->render_focus = gtk_theming_engine_render_focus;
219 klass->render_layout = gtk_theming_engine_render_layout;
220 klass->render_line = gtk_theming_engine_render_line;
221 klass->render_slider = gtk_theming_engine_render_slider;
222 klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
223 klass->render_extension = gtk_theming_engine_render_extension;
224 klass->render_handle = gtk_theming_engine_render_handle;
225 klass->render_activity = gtk_theming_engine_render_activity;
226 klass->render_icon_pixbuf = gtk_theming_engine_render_icon_pixbuf;
229 * GtkThemingEngine:name:
231 * The theming engine name, this name will be used when registering
232 * custom properties, for a theming engine named "Clearlooks" registering
233 * a "glossy" custom property, it could be referenced in the CSS file as
236 * -Clearlooks-glossy: true;
241 g_object_class_install_property (object_class,
243 g_param_spec_string ("name",
245 P_("Theming engine name"),
247 G_PARAM_CONSTRUCT_ONLY | GTK_PARAM_READWRITE));
249 g_type_class_add_private (object_class, sizeof (GtkThemingEnginePrivate));
253 gtk_theming_engine_init (GtkThemingEngine *engine)
255 engine->priv = GTK_THEMING_ENGINE_GET_PRIVATE (engine);
259 gtk_theming_engine_finalize (GObject *object)
261 GtkThemingEnginePrivate *priv;
263 priv = GTK_THEMING_ENGINE (object)->priv;
266 G_OBJECT_GET_CLASS (gtk_theming_engine_parent_class)->finalize (object);
270 gtk_theming_engine_impl_set_property (GObject *object,
275 GtkThemingEnginePrivate *priv;
277 priv = GTK_THEMING_ENGINE (object)->priv;
285 priv->name = g_value_dup_string (value);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 gtk_theming_engine_impl_get_property (GObject *object,
299 GtkThemingEnginePrivate *priv;
301 priv = GTK_THEMING_ENGINE (object)->priv;
306 g_value_set_string (value, priv->name);
309 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
315 _gtk_theming_engine_set_context (GtkThemingEngine *engine,
316 GtkStyleContext *context)
318 GtkThemingEnginePrivate *priv;
320 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
321 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
324 priv->context = context;
328 * gtk_theming_engine_register_property:
329 * @engine: a #GtkThemingEngine
330 * @property_name: property name to register
331 * @type: #GType the property will hold
332 * @default_value: default value for this property
333 * @parse_func: parsing function to use, or %NULL
335 * Registers a property so it can be used in the CSS file format,
336 * on the CSS file the property will look like
337 * "-${engine-object-name}-${@property_name}". being
338 * ${engine-object-name} either the GtkThemingEngine:name property
339 * or G_OBJECT_TYPE_NAME(engine) if the property is unset.
341 * For any type a @parse_func may be provided, being this function
342 * used for turning any property value (between ':' and ';') in
343 * CSS to the #GValue needed. For basic types there is already
344 * builtin parsing support, so %NULL may be provided for these
348 * This function needs to be called only once during theming
349 * engine object initialization.
353 * In order to make use of the custom registered properties in
354 * the CSS file, make sure the engine is loaded first either in
355 * a previous rule or within the same one.
358 * engine: someengine;
359 * SomeEngine-custom-property: 2;
367 gtk_theming_engine_register_property (GtkThemingEngine *engine,
368 const gchar *property_name,
370 const GValue *default_value,
371 GtkStylePropertyParser parse_func)
373 GtkThemingEnginePrivate *priv;
374 const gchar *engine_name;
377 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
378 g_return_if_fail (property_name != NULL);
379 g_return_if_fail (type != G_TYPE_INVALID);
380 g_return_if_fail (default_value == NULL || G_IS_VALUE (default_value));
385 engine_name = priv->name;
387 engine_name = G_OBJECT_TYPE_NAME (engine);
389 name = g_strdup_printf ("-%s-%s", engine_name, property_name);
390 gtk_style_properties_register_property (name, type, default_value, parse_func);
395 * gtk_theming_engine_get_property:
396 * @engine: a #GtkThemingEngine
397 * @property: the property name
398 * @state: state to retrieve the value for
399 * @value: (out) (transfer full): return location for the property value,
400 * you must free this memory using g_value_unset() once you are
403 * Gets a property value as retrieved from the style settings that apply
404 * to the currently rendered element.
409 gtk_theming_engine_get_property (GtkThemingEngine *engine,
410 const gchar *property,
414 GtkThemingEnginePrivate *priv;
416 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
417 g_return_if_fail (property != NULL);
418 g_return_if_fail (value != NULL);
421 gtk_style_context_get_property (priv->context, property, state, value);
425 * gtk_theming_engine_get_valist:
426 * @engine: a #GtkThemingEngine
427 * @state: state to retrieve values for
428 * @args: va_list of property name/return location pairs, followed by %NULL
430 * Retrieves several style property values that apply to the currently
436 gtk_theming_engine_get_valist (GtkThemingEngine *engine,
440 GtkThemingEnginePrivate *priv;
442 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
445 gtk_style_context_get_valist (priv->context, state, args);
449 * gtk_theming_engine_get:
450 * @engine: a #GtkThemingEngine
451 * @state: state to retrieve values for
452 * @...: property name /return value pairs, followed by %NULL
454 * Retrieves several style property values that apply to the currently
460 gtk_theming_engine_get (GtkThemingEngine *engine,
464 GtkThemingEnginePrivate *priv;
467 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
471 va_start (args, state);
472 gtk_style_context_get_valist (priv->context, state, args);
477 * gtk_theming_engine_get_style_property:
478 * @engine: a #GtkThemingEngine
479 * @property_name: the name of the widget style property
480 * @value: (out) (transfer full): Return location for the property value, free with
481 * g_value_unset() after use.
483 * Gets the value for a widget style property.
488 gtk_theming_engine_get_style_property (GtkThemingEngine *engine,
489 const gchar *property_name,
492 GtkThemingEnginePrivate *priv;
494 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
495 g_return_if_fail (property_name != NULL);
498 gtk_style_context_get_style_property (priv->context, property_name, value);
502 * gtk_theming_engine_get_style_valist:
503 * @engine: a #GtkThemingEngine
504 * @args: va_list of property name/return location pairs, followed by %NULL
506 * Retrieves several widget style properties from @engine according to the
507 * currently rendered content's style.
512 gtk_theming_engine_get_style_valist (GtkThemingEngine *engine,
515 GtkThemingEnginePrivate *priv;
517 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
520 gtk_style_context_get_style_valist (priv->context, args);
524 * gtk_theming_engine_get_style:
525 * @engine: a #GtkThemingEngine
526 * @...: property name /return value pairs, followed by %NULL
528 * Retrieves several widget style properties from @engine according
529 * to the currently rendered content's style.
534 gtk_theming_engine_get_style (GtkThemingEngine *engine,
537 GtkThemingEnginePrivate *priv;
540 g_return_if_fail (GTK_IS_THEMING_ENGINE (engine));
544 va_start (args, engine);
545 gtk_style_context_get_style_valist (priv->context, args);
550 * gtk_theming_engine_get_state:
551 * @engine: a #GtkThemingEngine
553 * returns the state used when rendering.
555 * Returns: the state flags
560 gtk_theming_engine_get_state (GtkThemingEngine *engine)
562 GtkThemingEnginePrivate *priv;
564 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
567 return gtk_style_context_get_state (priv->context);
571 * gtk_theming_engine_state_is_running:
572 * @engine: a #GtkThemingEngine
573 * @state: a widget state
574 * @progress: (out): return location for the transition progress
576 * Returns %TRUE if there is a transition animation running for the
577 * current region (see gtk_style_context_push_animatable_region()).
579 * If @progress is not %NULL, the animation progress will be returned
580 * there, 0.0 means the state is closest to being %FALSE, while 1.0 means
581 * it's closest to being %TRUE. This means transition animations will
582 * run from 0 to 1 when @state is being set to %TRUE and from 1 to 0 when
583 * it's being set to %FALSE.
585 * Returns: %TRUE if there is a running transition animation for @state.
590 gtk_theming_engine_state_is_running (GtkThemingEngine *engine,
594 GtkThemingEnginePrivate *priv;
596 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
599 return gtk_style_context_state_is_running (priv->context, state, progress);
603 * gtk_theming_engine_get_path:
604 * @engine: a #GtkThemingEngine
606 * Returns the widget path used for style matching.
608 * Returns: (transfer none): A #GtkWidgetPath
612 G_CONST_RETURN GtkWidgetPath *
613 gtk_theming_engine_get_path (GtkThemingEngine *engine)
615 GtkThemingEnginePrivate *priv;
617 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
620 return gtk_style_context_get_path (priv->context);
624 * gtk_theming_engine_has_class:
625 * @engine: a #GtkThemingEngine
626 * @style_class: class name to look up
628 * Returns %TRUE if the currently rendered contents have
629 * defined the given class name.
631 * Returns: %TRUE if @engine has @class_name defined
636 gtk_theming_engine_has_class (GtkThemingEngine *engine,
637 const gchar *style_class)
639 GtkThemingEnginePrivate *priv;
641 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
644 return gtk_style_context_has_class (priv->context, style_class);
648 * gtk_theming_engine_has_region:
649 * @engine: a #GtkThemingEngine
650 * @style_region: a region name
651 * @flags: (out) (allow-none): return location for region flags
653 * Returns %TRUE if the currently rendered contents have the
654 * region defined. If @flags_return is not %NULL, it is set
655 * to the flags affecting the region.
657 * Returns: %TRUE if region is defined
662 gtk_theming_engine_has_region (GtkThemingEngine *engine,
663 const gchar *style_region,
664 GtkRegionFlags *flags)
666 GtkThemingEnginePrivate *priv;
671 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), FALSE);
674 return gtk_style_context_has_region (priv->context, style_region, flags);
678 * gtk_theming_engine_get_direction:
679 * @engine: a #GtkThemingEngine
681 * Returns the widget direction used for rendering.
683 * Returns: the widget direction
688 gtk_theming_engine_get_direction (GtkThemingEngine *engine)
690 GtkThemingEnginePrivate *priv;
692 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), GTK_TEXT_DIR_LTR);
695 return gtk_style_context_get_direction (priv->context);
699 * gtk_theming_engine_get_junction_sides:
700 * @engine: a #GtkThemingEngine
702 * Returns the widget direction used for rendering.
704 * Returns: the widget direction
709 gtk_theming_engine_get_junction_sides (GtkThemingEngine *engine)
711 GtkThemingEnginePrivate *priv;
713 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), 0);
716 return gtk_style_context_get_junction_sides (priv->context);
719 /* GtkThemingModule */
722 gtk_theming_module_load (GTypeModule *type_module)
724 GtkThemingModule *theming_module;
726 gchar *name, *module_path;
728 theming_module = GTK_THEMING_MODULE (type_module);
729 name = theming_module->name;
730 module_path = _gtk_find_module (name, "theming-engines");
734 g_warning (_("Unable to locate theme engine in module path: \"%s\","), name);
738 module = g_module_open (module_path, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
739 g_free (module_path);
743 g_warning ("%s", g_module_error ());
747 if (!g_module_symbol (module, "theme_init",
748 (gpointer *) &theming_module->init) ||
749 !g_module_symbol (module, "theme_exit",
750 (gpointer *) &theming_module->exit) ||
751 !g_module_symbol (module, "create_engine",
752 (gpointer *) &theming_module->create_engine))
754 g_warning ("%s", g_module_error ());
755 g_module_close (module);
760 theming_module->module = module;
762 theming_module->init (G_TYPE_MODULE (theming_module));
768 gtk_theming_module_unload (GTypeModule *type_module)
770 GtkThemingModule *theming_module;
772 theming_module = GTK_THEMING_MODULE (type_module);
774 theming_module->exit ();
776 g_module_close (theming_module->module);
778 theming_module->module = NULL;
779 theming_module->init = NULL;
780 theming_module->exit = NULL;
781 theming_module->create_engine = NULL;
785 gtk_theming_module_class_init (GtkThemingModuleClass *klass)
787 GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS (klass);
789 module_class->load = gtk_theming_module_load;
790 module_class->unload = gtk_theming_module_unload;
794 gtk_theming_module_init (GtkThemingModule *module)
799 * gtk_theming_engine_load:
800 * @name: Theme engine name to load
802 * Loads and initializes a theming engine module from the
803 * standard directories.
805 * Returns: (transfer none): A theming engine, or %NULL if
806 * the engine @name doesn't exist.
809 gtk_theming_engine_load (const gchar *name)
811 static GHashTable *engines = NULL;
812 static GtkThemingEngine *default_engine;
813 GtkThemingEngine *engine = NULL;
818 engines = g_hash_table_new (g_str_hash, g_str_equal);
820 engine = g_hash_table_lookup (engines, name);
824 GtkThemingModule *module;
826 module = g_object_new (GTK_TYPE_THEMING_MODULE, NULL);
827 g_type_module_set_name (G_TYPE_MODULE (module), name);
828 module->name = g_strdup (name);
830 if (module && g_type_module_use (G_TYPE_MODULE (module)))
832 engine = (module->create_engine) ();
835 g_hash_table_insert (engines, module->name, engine);
842 if (G_UNLIKELY (!default_engine))
843 default_engine = g_object_new (GTK_TYPE_THEMING_ENGINE, NULL);
845 engine = default_engine;
852 * gtk_theming_engine_get_screen:
853 * @engine: a #GtkThemingEngine
855 * Returns the #GdkScreen to which @engine currently rendering to.
857 * Returns: a #GdkScreen, or %NULL.
860 gtk_theming_engine_get_screen (GtkThemingEngine *engine)
862 GtkThemingEnginePrivate *priv;
864 g_return_val_if_fail (GTK_IS_THEMING_ENGINE (engine), NULL);
867 return gtk_style_context_get_screen (priv->context);
870 /* Paint method implementations */
872 gtk_theming_engine_render_check (GtkThemingEngine *engine,
879 GdkRGBA *fg_color, *bg_color;
880 const GtkWidgetPath *path;
882 gint exterior_size, interior_size, thickness, pad;
884 flags = gtk_theming_engine_get_state (engine);
885 path = gtk_theming_engine_get_path (engine);
888 gtk_theming_engine_get (engine, flags,
890 "background-color", &bg_color,
893 exterior_size = MIN (width, height);
895 if (exterior_size % 2 == 0) /* Ensure odd */
898 /* FIXME: thickness */
900 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
901 interior_size = MAX (1, exterior_size - 2 * pad);
903 if (interior_size < 7)
906 pad = MAX (0, (exterior_size - interior_size) / 2);
909 x -= (1 + exterior_size - (gint) width) / 2;
910 y -= (1 + exterior_size - (gint) height) / 2;
912 if (!gtk_theming_engine_has_class (engine, "menu"))
914 cairo_set_line_width (cr, 1.0);
916 cairo_rectangle (cr, x + 0.5, y + 0.5, exterior_size - 1, exterior_size - 1);
917 gdk_cairo_set_source_rgba (cr, bg_color);
918 cairo_fill_preserve (cr);
920 gdk_cairo_set_source_rgba (cr, fg_color);
925 gdk_cairo_set_source_rgba (cr, fg_color);
927 if (flags & GTK_STATE_FLAG_INCONSISTENT)
929 int line_thickness = MAX (1, (3 + interior_size * 2) / 7);
933 y + pad + (1 + interior_size - line_thickness) / 2,
943 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress);
945 if ((flags & GTK_STATE_FLAG_ACTIVE) || running)
953 cairo_scale (cr, interior_size / 7., interior_size / 7.);
955 cairo_rectangle (cr, 0, 0, 7 * progress, 7);
958 cairo_move_to (cr, 7.0, 0.0);
959 cairo_line_to (cr, 7.5, 1.0);
960 cairo_curve_to (cr, 5.3, 2.0,
963 cairo_curve_to (cr, 3.0, 5.7,
966 cairo_line_to (cr, 0.2, 3.5);
967 cairo_curve_to (cr, 1.1, 3.5,
970 cairo_curve_to (cr, 1.0, 3.9,
973 cairo_curve_to (cr, 3.5, 3.1,
983 gdk_rgba_free (fg_color);
987 gtk_theming_engine_render_option (GtkThemingEngine *engine,
995 GdkRGBA *fg_color, *bg_color;
996 const GtkWidgetPath *path;
997 gint exterior_size, interior_size, pad, thickness;
1000 flags = gtk_theming_engine_get_state (engine);
1001 path = gtk_theming_engine_get_path (engine);
1002 radius = MIN (width, height) / 2 - 0.5;
1006 gtk_theming_engine_get (engine, flags,
1008 "background-color", &bg_color,
1011 exterior_size = MIN (width, height);
1013 if (exterior_size % 2 == 0) /* Ensure odd */
1016 x -= (1 + exterior_size - width) / 2;
1017 y -= (1 + exterior_size - height) / 2;
1019 if (!gtk_theming_engine_has_class (engine, "menu"))
1021 gdk_cairo_set_source_rgba (cr, bg_color);
1024 x + exterior_size / 2.,
1025 y + exterior_size / 2.,
1026 (exterior_size - 1) / 2.,
1029 cairo_fill_preserve (cr);
1031 gdk_cairo_set_source_rgba (cr, fg_color);
1033 cairo_set_line_width (cr, 1.);
1037 gdk_cairo_set_source_rgba (cr, fg_color);
1039 /* FIXME: thickness */
1042 if (flags & GTK_STATE_FLAG_INCONSISTENT)
1044 gint line_thickness;
1046 pad = thickness + MAX (1, (exterior_size - 2 * thickness) / 9);
1047 interior_size = MAX (1, exterior_size - 2 * pad);
1049 if (interior_size < 7)
1052 pad = MAX (0, (exterior_size - interior_size) / 2);
1055 line_thickness = MAX (1, (3 + interior_size * 2) / 7);
1057 cairo_rectangle (cr,
1059 y + pad + (interior_size - line_thickness) / 2.,
1064 if (flags & GTK_STATE_FLAG_ACTIVE)
1066 pad = thickness + MAX (1, 2 * (exterior_size - 2 * thickness) / 9);
1067 interior_size = MAX (1, exterior_size - 2 * pad);
1069 if (interior_size < 5)
1072 pad = MAX (0, (exterior_size - interior_size) / 2);
1076 x + pad + interior_size / 2.,
1077 y + pad + interior_size / 2.,
1085 gdk_rgba_free (fg_color);
1086 gdk_rgba_free (bg_color);
1090 add_path_arrow (cairo_t *cr,
1098 cairo_translate (cr, x + (size / 2), y + (size / 2));
1099 cairo_rotate (cr, angle);
1101 cairo_move_to (cr, 0, - (size / 4));
1102 cairo_line_to (cr, - (size / 2), (size / 4));
1103 cairo_line_to (cr, (size / 2), (size / 4));
1104 cairo_close_path (cr);
1110 gtk_theming_engine_render_arrow (GtkThemingEngine *engine,
1117 GtkStateFlags flags;
1122 flags = gtk_theming_engine_get_state (engine);
1124 gtk_theming_engine_get (engine, flags,
1128 if (flags & GTK_STATE_FLAG_INSENSITIVE)
1130 add_path_arrow (cr, angle, x + 1, y + 1, size);
1131 cairo_set_source_rgb (cr, 1, 1, 1);
1135 add_path_arrow (cr, angle, x, y, size);
1136 gdk_cairo_set_source_rgba (cr, fg_color);
1141 gdk_rgba_free (fg_color);
1145 add_path_line (cairo_t *cr,
1151 /* Adjust endpoints */
1165 cairo_move_to (cr, x1, y1);
1166 cairo_line_to (cr, x2, y2);
1170 add_path_rectangle_sides (cairo_t *cr,
1177 if (sides & SIDE_TOP)
1179 cairo_move_to (cr, x, y + 0.5);
1180 cairo_line_to (cr, x + width, y + 0.5);
1183 if (sides & SIDE_RIGHT)
1185 cairo_move_to (cr, x + width - 0.5, y);
1186 cairo_line_to (cr, x + width - 0.5, y + height);
1189 if (sides & SIDE_BOTTOM)
1191 cairo_move_to (cr, x, y + height - 0.5);
1192 cairo_line_to (cr, x + width, y + height - 0.5);
1195 if (sides & SIDE_LEFT)
1197 cairo_move_to (cr, x + 0.5, y + height);
1198 cairo_line_to (cr, x + 0.5, y);
1203 add_path_gap_side (cairo_t *cr,
1204 GtkPositionType gap_side,
1215 add_path_line (cr, x, y, x + xy0_gap, y);
1216 add_path_line (cr, x + xy1_gap, y, x + width, y);
1218 case GTK_POS_BOTTOM:
1219 add_path_line (cr, x, y + height, x + xy0_gap, y + height);
1220 add_path_line (cr, x + xy1_gap, y + height, x + width, y + height);
1223 add_path_line (cr, x, y, x, y + xy0_gap);
1224 add_path_line (cr, x, y + xy1_gap, x, y + height);
1227 add_path_line (cr, x + width, y, x + width, y + xy0_gap);
1228 add_path_line (cr, x + width, y + xy1_gap, x + width, y + height);
1234 color_shade (const GdkRGBA *color,
1236 GdkRGBA *color_return)
1238 color_return->red = CLAMP (color->red * factor, 0, 1);
1239 color_return->green = CLAMP (color->green * factor, 0, 1);
1240 color_return->blue = CLAMP (color->blue * factor, 0, 1);
1241 color_return->alpha = color->alpha;
1245 _cairo_round_rectangle_sides (cairo_t *cr,
1252 GtkJunctionSides junction)
1254 radius = CLAMP (radius, 0, MIN (width / 2, height / 2));
1256 if (sides & SIDE_RIGHT)
1259 (junction & GTK_JUNCTION_TOP) ||
1260 (junction & GTK_JUNCTION_RIGHT))
1261 cairo_move_to (cr, x + width, y);
1264 cairo_new_sub_path (cr);
1265 cairo_arc (cr, x + width - radius, y + radius, radius, - G_PI / 4, 0);
1269 (junction & GTK_JUNCTION_BOTTOM) ||
1270 (junction & GTK_JUNCTION_RIGHT))
1271 cairo_line_to (cr, x + width, y + height);
1273 cairo_arc (cr, x + width - radius, y + height - radius, radius, 0, G_PI / 4);
1276 if (sides & SIDE_BOTTOM)
1279 ! (junction & GTK_JUNCTION_RIGHT) &&
1280 ! (junction & GTK_JUNCTION_BOTTOM))
1282 if ((sides & SIDE_RIGHT) == 0)
1283 cairo_new_sub_path (cr);
1285 cairo_arc (cr, x + width - radius, y + height - radius, radius, G_PI / 4, G_PI / 2);
1289 (junction & GTK_JUNCTION_BOTTOM) ||
1290 (junction & GTK_JUNCTION_LEFT))
1291 cairo_line_to (cr, x, y + height);
1293 cairo_arc (cr, x + radius, y + height - radius, radius, G_PI / 2, 3 * (G_PI / 4));
1296 cairo_move_to (cr, x, y + height);
1298 if (sides & SIDE_LEFT)
1301 ! (junction & GTK_JUNCTION_LEFT) &&
1302 ! (junction & GTK_JUNCTION_BOTTOM))
1304 if ((sides & SIDE_BOTTOM) == 0)
1305 cairo_new_sub_path (cr);
1307 cairo_arc (cr, x + radius, y + height - radius, radius, 3 * (G_PI / 4), G_PI);
1311 (junction & GTK_JUNCTION_TOP) ||
1312 (junction & GTK_JUNCTION_LEFT))
1313 cairo_line_to (cr, x, y);
1315 cairo_arc (cr, x + radius, y + radius, radius, G_PI, G_PI + G_PI / 4);
1318 if (sides & SIDE_TOP)
1321 ! (junction & GTK_JUNCTION_TOP) &&
1322 ! (junction & GTK_JUNCTION_LEFT))
1324 if ((sides & SIDE_TOP) == 0)
1325 cairo_new_sub_path (cr);
1327 cairo_arc (cr, x + radius, y + radius, radius, 5 * (G_PI / 4), 3 * (G_PI / 2));
1331 (junction & GTK_JUNCTION_TOP) ||
1332 (junction & GTK_JUNCTION_RIGHT))
1333 cairo_line_to (cr, x + width, y);
1335 cairo_arc (cr, x + width - radius, y + radius, radius, 3 * (G_PI / 2), - G_PI / 4);
1340 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1348 cairo_pattern_t *pattern;
1349 GtkStateFlags flags;
1351 gdouble progress, alpha = 1;
1352 GtkJunctionSides junction;
1355 flags = gtk_theming_engine_get_state (engine);
1356 junction = gtk_theming_engine_get_junction_sides (engine);
1359 if (gtk_theming_engine_has_class (engine, "spinbutton") &&
1360 gtk_theming_engine_has_class (engine, "button"))
1368 gtk_theming_engine_get (engine, flags,
1369 "background-image", &pattern,
1370 "background-color", &bg_color,
1371 "border-radius", &radius,
1374 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1376 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1377 x, y, width, height,
1378 SIDE_ALL, junction);
1381 if (gtk_theming_engine_has_class (engine, "background"))
1383 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
1384 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1388 cairo_translate (cr, x, y);
1389 cairo_scale (cr, width, height);
1393 cairo_pattern_t *other_pattern;
1394 GtkStateFlags other_flags;
1396 cairo_pattern_t *new_pattern = NULL;
1398 if (flags & GTK_STATE_FLAG_PRELIGHT)
1400 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
1401 progress = 1 - progress;
1404 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
1406 gtk_theming_engine_get (engine, other_flags,
1407 "background-image", &other_pattern,
1408 "background-color", &other_bg,
1411 if (pattern && other_pattern)
1413 cairo_pattern_type_t type, other_type;
1416 cairo_pattern_get_color_stop_count (pattern, &n0);
1417 cairo_pattern_get_color_stop_count (other_pattern, &n1);
1418 type = cairo_pattern_get_type (pattern);
1419 other_type = cairo_pattern_get_type (other_pattern);
1421 if (type == other_type && n0 == n1)
1423 gdouble offset0, red0, green0, blue0, alpha0;
1424 gdouble offset1, red1, green1, blue1, alpha1;
1425 gdouble x00, x01, y00, y01, x10, x11, y10, y11;
1426 gdouble r00, r01, r10, r11;
1429 if (type == CAIRO_PATTERN_TYPE_LINEAR)
1431 cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
1432 cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
1434 new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
1435 y00 + (y10 - y00) * progress,
1436 x01 + (x11 - x01) * progress,
1437 y01 + (y11 - y01) * progress);
1441 cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
1442 cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
1444 new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
1445 y00 + (y10 - y00) * progress,
1446 r00 + (r10 - r00) * progress,
1447 x01 + (x11 - x01) * progress,
1448 y01 + (y11 - y01) * progress,
1449 r01 + (r11 - r01) * progress);
1452 cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
1455 /* Blend both gradients into one */
1456 while (i < n0 && i < n1)
1458 cairo_pattern_get_color_stop_rgba (pattern, i,
1460 &red0, &green0, &blue0,
1462 cairo_pattern_get_color_stop_rgba (other_pattern, i,
1464 &red1, &green1, &blue1,
1467 cairo_pattern_add_color_stop_rgba (new_pattern,
1468 offset0 + ((offset1 - offset0) * progress),
1469 red0 + ((red1 - red0) * progress),
1470 green0 + ((green1 - green0) * progress),
1471 blue0 + ((blue1 - blue0) * progress),
1472 alpha0 + ((alpha1 - alpha0) * progress));
1478 /* Different pattern types, or different color
1479 * stop counts, alpha blend both patterns.
1481 cairo_rectangle (cr, 0, 0, 1, 1);
1482 cairo_set_source (cr, other_pattern);
1483 cairo_fill_preserve (cr);
1485 /* Set alpha for posterior drawing
1486 * of the target pattern
1488 alpha = 1 - progress;
1491 else if (pattern || other_pattern)
1495 gdouble x0, y0, x1, y1, r0, r1;
1498 /* Blend a pattern with a color */
1503 progress = 1 - progress;
1511 if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
1513 cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
1514 new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
1518 cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
1519 new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
1522 cairo_pattern_get_color_stop_count (p, &n);
1524 for (i = 0; i < n; i++)
1526 gdouble red1, green1, blue1, alpha1;
1529 cairo_pattern_get_color_stop_rgba (p, i,
1531 &red1, &green1, &blue1,
1533 cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1534 c->red + ((red1 - c->red) * progress),
1535 c->green + ((green1 - c->green) * progress),
1536 c->blue + ((blue1 - c->blue) * progress),
1537 c->alpha + ((alpha1 - c->alpha) * progress));
1542 const GdkRGBA *color, *other_color;
1544 /* Merge just colors */
1546 other_color = other_bg;
1548 new_pattern = cairo_pattern_create_rgba (CLAMP (color->red + ((other_color->red - color->red) * progress), 0, 1),
1549 CLAMP (color->green + ((other_color->green - color->green) * progress), 0, 1),
1550 CLAMP (color->blue + ((other_color->blue - color->blue) * progress), 0, 1),
1551 CLAMP (color->alpha + ((other_color->alpha - color->alpha) * progress), 0, 1));
1556 /* Replace pattern to use */
1557 cairo_pattern_destroy (pattern);
1558 pattern = new_pattern;
1562 cairo_pattern_destroy (other_pattern);
1565 gdk_rgba_free (other_bg);
1568 cairo_rectangle (cr, 0, 0, 1, 1);
1572 cairo_set_source (cr, pattern);
1573 cairo_pattern_destroy (pattern);
1576 gdk_cairo_set_source_rgba (cr, bg_color);
1578 if (gtk_theming_engine_has_class (engine, "tooltip"))
1580 cairo_fill_preserve (cr);
1582 cairo_set_source_rgb (cr, 0, 0, 0);
1591 cairo_pattern_t *mask;
1593 mask = cairo_pattern_create_rgba (1, 1, 1, alpha);
1594 cairo_mask (cr, mask);
1595 cairo_pattern_destroy (mask);
1601 gdk_rgba_free (bg_color);
1605 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1612 GtkStateFlags flags;
1614 GdkRGBA *border_color;
1616 GtkBorderStyle border_style;
1617 gint border_width, radius;
1618 GtkJunctionSides junction;
1621 flags = gtk_theming_engine_get_state (engine);
1622 junction = gtk_theming_engine_get_junction_sides (engine);
1624 gtk_theming_engine_get (engine, flags,
1625 "border-image", &slice,
1626 "border-color", &border_color,
1627 "border-style", &border_style,
1628 "border-width", &border_width,
1629 "border-radius", &radius,
1634 gtk_9slice_render (slice, cr, x, y, width, height);
1635 gtk_9slice_unref (slice);
1637 else if (border_color &&
1638 border_style != GTK_BORDER_STYLE_NONE)
1642 color_shade (border_color, 1.8, &lighter);
1644 switch (border_style)
1646 case GTK_BORDER_STYLE_NONE:
1648 case GTK_BORDER_STYLE_SOLID:
1649 cairo_set_line_width (cr, border_width);
1651 if (border_width > 1)
1653 x += (gdouble) border_width / 2;
1654 y += (gdouble) border_width / 2;
1655 width -= border_width;
1656 height -= border_width;
1659 _cairo_round_rectangle_sides (cr, (gdouble) radius, x, y, width, height,
1660 SIDE_ALL, junction);
1661 gdk_cairo_set_source_rgba (cr, border_color);
1665 case GTK_BORDER_STYLE_INSET:
1666 case GTK_BORDER_STYLE_OUTSET:
1667 cairo_set_line_width (cr, border_width);
1668 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1672 if (border_width > 1)
1674 d1 = (gdouble) border_width / 2;
1675 d2 = (gdouble) (border_width - (gint) d1) + 1;
1680 m = MIN (width, height);
1683 /* Only needed for square frames to have a 3D-like
1684 * feeling, for rounded ones, the arc will ensure
1685 * the stroke is painted to end at 45°.
1689 cairo_move_to (cr, x, y + height);
1690 cairo_line_to (cr, x + m, y + height - m);
1691 cairo_line_to (cr, x + width - m, y + m);
1692 cairo_line_to (cr, x + width, y);
1693 cairo_line_to (cr, x + width, y + height);
1694 cairo_close_path (cr);
1699 if (border_style == GTK_BORDER_STYLE_INSET)
1700 gdk_cairo_set_source_rgba (cr, &lighter);
1702 gdk_cairo_set_source_rgba (cr, border_color);
1704 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1706 width - d2, height - d2,
1707 SIDE_BOTTOM | SIDE_RIGHT, junction);
1716 cairo_move_to (cr, x, y + height);
1717 cairo_line_to (cr, x + m, y + height - m);
1718 cairo_line_to (cr, x + width - m, y + m);
1719 cairo_line_to (cr, x + width, y);
1720 cairo_line_to (cr, x, y);
1721 cairo_close_path (cr);
1726 if (border_style == GTK_BORDER_STYLE_INSET)
1727 gdk_cairo_set_source_rgba (cr, border_color);
1729 gdk_cairo_set_source_rgba (cr, &lighter);
1731 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1733 width - d2, height - d2,
1734 SIDE_TOP | SIDE_LEFT, junction);
1746 gdk_rgba_free (border_color);
1750 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1757 GtkStateFlags flags;
1758 GdkRGBA *bg_color, *fg_color;
1759 double vertical_overshoot;
1762 double interp; /* interpolation factor for center position */
1763 double x_double_horz, y_double_horz;
1764 double x_double_vert, y_double_vert;
1765 double x_double, y_double;
1770 flags = gtk_theming_engine_get_state (engine);
1772 gtk_theming_engine_get (engine, flags,
1774 "background-color", &bg_color,
1779 /* FIXME: LTR/RTL */
1780 if (flags & GTK_STATE_FLAG_ACTIVE)
1791 /* Compute distance that the stroke extends beyonds the end
1792 * of the triangle we draw.
1794 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1796 /* For odd line widths, we end the vertical line of the triangle
1797 * at a half pixel, so we round differently.
1799 if (line_width % 2 == 1)
1800 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1802 vertical_overshoot = ceil (vertical_overshoot);
1804 /* Adjust the size of the triangle we draw so that the entire stroke fits
1806 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1808 /* If the line width is odd, we want the diameter to be even,
1809 * and vice versa, so force the sum to be odd. This relationship
1810 * makes the point of the triangle look right.
1812 diameter -= (1 - (diameter + line_width) % 2);
1814 radius = diameter / 2.;
1816 /* Adjust the center so that the stroke is properly aligned with
1817 * the pixel grid. The center adjustment is different for the
1818 * horizontal and vertical orientations. For intermediate positions
1819 * we interpolate between the two.
1821 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1822 y_double_vert = (y + height / 2) - 0.5;
1824 x_double_horz = (x + width / 2) - 0.5;
1825 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1827 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1828 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1830 cairo_translate (cr, x_double, y_double);
1831 cairo_rotate (cr, angle);
1833 cairo_move_to (cr, - radius / 2., - radius);
1834 cairo_line_to (cr, radius / 2., 0);
1835 cairo_line_to (cr, - radius / 2., radius);
1836 cairo_close_path (cr);
1838 cairo_set_line_width (cr, line_width);
1840 gdk_cairo_set_source_rgba (cr, fg_color);
1842 cairo_fill_preserve (cr);
1844 gdk_cairo_set_source_rgba (cr, fg_color);
1849 gdk_rgba_free (fg_color);
1850 gdk_rgba_free (bg_color);
1854 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1861 GtkStateFlags flags;
1867 flags = gtk_theming_engine_get_state (engine);
1869 gtk_theming_engine_get (engine, flags,
1873 gtk_theming_engine_get_style (engine,
1874 "focus-line-width", &line_width,
1875 "focus-line-pattern", (gchar *) &dash_list,
1878 cairo_set_line_width (cr, (gdouble) line_width);
1882 gint n_dashes = strlen ((const gchar *) dash_list);
1883 gdouble *dashes = g_new (gdouble, n_dashes);
1884 gdouble total_length = 0;
1885 gdouble dash_offset;
1888 for (i = 0; i < n_dashes; i++)
1890 dashes[i] = dash_list[i];
1891 total_length += dash_list[i];
1894 /* The dash offset here aligns the pattern to integer pixels
1895 * by starting the dash at the right side of the left border
1896 * Negative dash offsets in cairo don't work
1897 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
1899 dash_offset = - line_width / 2.;
1901 while (dash_offset < 0)
1902 dash_offset += total_length;
1904 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
1908 cairo_rectangle (cr,
1909 x + line_width / 2.,
1910 y + line_width / 2.,
1912 height - line_width);
1914 gdk_cairo_set_source_rgba (cr, color);
1919 gdk_rgba_free (color);
1924 gtk_theming_engine_render_line (GtkThemingEngine *engine,
1931 GdkRGBA *bg_color, darker, lighter;
1932 GtkStateFlags flags;
1933 gint i, thickness, thickness_dark, thickness_light, len;
1934 cairo_matrix_t matrix;
1937 /* FIXME: thickness */
1939 thickness_dark = thickness / 2;
1940 thickness_light = thickness - thickness_dark;
1942 flags = gtk_theming_engine_get_state (engine);
1945 gtk_theming_engine_get (engine, flags,
1946 "background-color", &bg_color,
1948 color_shade (bg_color, 0.7, &darker);
1949 color_shade (bg_color, 1.3, &lighter);
1951 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1952 cairo_set_line_width (cr, 1);
1954 angle = atan2 (x1 - x0, y1 - y0);
1955 angle = (2 * G_PI) - angle;
1958 cairo_get_matrix (cr, &matrix);
1959 cairo_matrix_translate (&matrix, x0, y0);
1960 cairo_matrix_rotate (&matrix, angle);
1961 cairo_set_matrix (cr, &matrix);
1966 len = (gint) sqrt ((x1 * x1) + (y1 * y1));
1968 y0 = -thickness_dark;
1970 for (i = 0; i < thickness_dark; i++)
1972 gdk_cairo_set_source_rgba (cr, &lighter);
1973 add_path_line (cr, len - i - 1.5, y0, len - 0.5, y0);
1976 gdk_cairo_set_source_rgba (cr, &darker);
1977 add_path_line (cr, 0.5, y0, len - i - 1.5, y0);
1983 for (i = 0; i < thickness_light; i++)
1985 gdk_cairo_set_source_rgba (cr, &darker);
1986 add_path_line (cr, 0.5, y0, thickness_light - i + 0.5, y0);
1989 gdk_cairo_set_source_rgba (cr, &lighter);
1990 add_path_line (cr, thickness_light - i + 0.5, y0, len - 0.5, y0);
1998 gdk_rgba_free (bg_color);
2002 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2006 PangoLayout *layout)
2008 const PangoMatrix *matrix;
2010 GtkStateFlags flags;
2014 flags = gtk_theming_engine_get_state (engine);
2016 gtk_theming_engine_get (engine, flags,
2020 screen = gtk_theming_engine_get_screen (engine);
2021 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2025 cairo_matrix_t cairo_matrix;
2026 PangoMatrix tmp_matrix;
2027 PangoRectangle rect;
2029 cairo_matrix_init (&cairo_matrix,
2030 matrix->xx, matrix->yx,
2031 matrix->xy, matrix->yy,
2032 matrix->x0, matrix->y0);
2034 pango_layout_get_extents (layout, NULL, &rect);
2035 pango_matrix_transform_rectangle (matrix, &rect);
2036 pango_extents_to_pixels (&rect, NULL);
2038 tmp_matrix = *matrix;
2039 cairo_matrix.x0 += x - rect.x;
2040 cairo_matrix.y0 += y - rect.y;
2042 cairo_set_matrix (cr, &cairo_matrix);
2045 cairo_translate (cr, x, y);
2047 if (flags & GTK_STATE_FLAG_INSENSITIVE)
2050 cairo_set_source_rgb (cr, 1, 1, 1);
2051 cairo_move_to (cr, 1, 1);
2052 _gtk_pango_fill_layout (cr, layout);
2056 gdk_cairo_set_source_rgba (cr, fg_color);
2057 pango_cairo_show_layout (cr, layout);
2061 gdk_rgba_free (fg_color);
2065 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2071 GtkOrientation orientation)
2073 const GtkWidgetPath *path;
2076 path = gtk_theming_engine_get_path (engine);
2078 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2079 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2081 /* FIXME: thickness */
2084 if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2086 if (orientation == GTK_ORIENTATION_VERTICAL)
2087 gtk_theming_engine_render_line (engine, cr,
2089 y + (gint) height / 2,
2090 x + width - thickness - 1,
2091 y + (gint) height / 2);
2093 gtk_theming_engine_render_line (engine, cr,
2094 x + (gint) width / 2,
2096 x + (gint) width / 2,
2097 y + height - thickness - 1);
2102 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2108 GtkPositionType gap_side,
2112 GtkStateFlags flags;
2114 GdkRGBA lighter, darker;
2118 flags = gtk_theming_engine_get_state (engine);
2120 cairo_set_line_width (cr, 1);
2122 gtk_theming_engine_get (engine, flags,
2123 "background-color", &bg_color,
2125 color_shade (bg_color, 0.7, &darker);
2126 color_shade (bg_color, 1.3, &lighter);
2128 if (gtk_theming_engine_has_class (engine, "frame"))
2130 if (gap_side == GTK_POS_RIGHT)
2131 sides = SIDE_BOTTOM;
2132 else if (gap_side == GTK_POS_BOTTOM)
2135 sides = SIDE_BOTTOM | SIDE_RIGHT;
2137 gdk_cairo_set_source_rgba (cr, &lighter);
2138 add_path_rectangle_sides (cr, x , y, width , height, sides);
2141 gdk_cairo_set_source_rgba (cr, &darker);
2142 add_path_rectangle_sides (cr, x, y, width - 1, height - 1, sides);
2145 if (gap_side == GTK_POS_RIGHT ||
2146 gap_side == GTK_POS_BOTTOM)
2148 gdk_cairo_set_source_rgba (cr, &darker);
2149 add_path_gap_side (cr, gap_side,
2150 x + 1, y + 1, width - 4, height - 4,
2154 gdk_cairo_set_source_rgba (cr, &lighter);
2155 add_path_gap_side (cr, gap_side,
2156 x, y, width, height,
2161 if (gap_side == GTK_POS_LEFT)
2163 else if (gap_side == GTK_POS_TOP)
2166 sides = SIDE_TOP | SIDE_LEFT;
2168 gdk_cairo_set_source_rgba (cr, &lighter);
2169 add_path_rectangle_sides (cr, x + 1, y + 1, width - 2, height - 3, sides);
2172 gdk_cairo_set_source_rgba (cr, &darker);
2173 add_path_rectangle_sides (cr, x, y, width - 1, height - 1, sides);
2176 if (gap_side == GTK_POS_LEFT ||
2177 gap_side == GTK_POS_TOP)
2179 gdk_cairo_set_source_rgba (cr, &lighter);
2180 add_path_gap_side (cr, gap_side,
2181 x + 1, y + 1, width - 4, height - 4,
2185 gdk_cairo_set_source_rgba (cr, &darker);
2186 add_path_gap_side (cr, gap_side,
2187 x, y, width - 2, height - 2,
2194 if (gap_side == GTK_POS_RIGHT)
2195 sides = SIDE_BOTTOM;
2196 else if (gap_side == GTK_POS_BOTTOM)
2199 sides = SIDE_BOTTOM | SIDE_RIGHT;
2201 gdk_cairo_set_source_rgba (cr, &darker);
2202 add_path_rectangle_sides (cr, x + 1, y, width - 2, height, sides);
2203 add_path_rectangle_sides (cr, x, y + 1, width, height - 2, sides);
2206 cairo_set_source_rgb (cr, 0, 0, 0);
2207 add_path_rectangle_sides (cr, x, y, width, height, sides);
2210 if (gap_side == GTK_POS_RIGHT ||
2211 gap_side == GTK_POS_BOTTOM)
2213 gdk_cairo_set_source_rgba (cr, &darker);
2214 add_path_gap_side (cr, gap_side,
2215 x, y, width - 2, height - 2,
2219 cairo_set_source_rgb (cr, 0, 0, 0);
2220 add_path_gap_side (cr, gap_side,
2221 x, y, width - 1, height - 1,
2226 if (gap_side == GTK_POS_LEFT)
2228 else if (gap_side == GTK_POS_TOP)
2231 sides = SIDE_TOP | SIDE_LEFT;
2233 gdk_cairo_set_source_rgba (cr, &lighter);
2234 add_path_rectangle_sides (cr, x, y, width, height, sides);
2237 if (gap_side == GTK_POS_LEFT ||
2238 gap_side == GTK_POS_TOP)
2240 add_path_gap_side (cr, gap_side,
2241 x, y, width, height,
2249 gdk_rgba_free (bg_color);
2253 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2259 GtkPositionType gap_side)
2261 GtkStateFlags flags;
2263 GdkRGBA lighter, darker;
2266 flags = gtk_theming_engine_get_state (engine);
2268 cairo_set_line_width (cr, 1);
2270 gtk_theming_engine_get (engine, flags,
2271 "background-color", &bg_color,
2273 color_shade (bg_color, 0.7, &darker);
2274 color_shade (bg_color, 1.3, &lighter);
2279 gdk_cairo_set_source_rgba (cr, bg_color);
2280 cairo_rectangle (cr, x + 1, y, width - 2, height);
2283 gdk_cairo_set_source_rgba (cr, &lighter);
2284 add_path_line (cr, x, y, x, y + height - 2);
2287 gdk_cairo_set_source_rgba (cr, bg_color);
2288 add_path_line (cr, x + 1, y, x + 1, y + height - 2);
2291 gdk_cairo_set_source_rgba (cr, &darker);
2292 add_path_line (cr, x + 2, y + height - 2, x + width - 2, y + height - 2);
2293 add_path_line (cr, x + width - 2, y, x + width - 2, y + height - 2);
2296 cairo_set_source_rgb (cr, 0, 0, 0);
2297 add_path_line (cr, x + 1, y + height - 1, x + width - 2, y + height - 1);
2298 add_path_line (cr, x + width - 1, y, x + width - 1, y + height - 2);
2302 case GTK_POS_BOTTOM:
2303 gdk_cairo_set_source_rgba(cr, bg_color);
2304 cairo_rectangle (cr, x + 1, y, width - 2, height);
2307 gdk_cairo_set_source_rgba (cr, &lighter);
2308 add_path_line (cr, x + 1, y, x + width - 2, y);
2309 add_path_line (cr, x, y + 1, x, y + height - 1);
2312 gdk_cairo_set_source_rgba (cr, bg_color);
2313 add_path_line (cr, x + 1, y + 1, x + width - 2, y + 1);
2314 add_path_line (cr, x + 1, y + 1, x + 1, y + height - 1);
2317 gdk_cairo_set_source_rgba (cr, &darker);
2318 add_path_line (cr, x + width - 2, y + 2, x + width - 2, y + height - 1);
2321 cairo_set_source_rgb (cr, 0, 0, 0);
2322 add_path_line (cr, x + width - 1, y + 1, x + width - 1, y + height - 1);
2327 gdk_cairo_set_source_rgba (cr, bg_color);
2328 cairo_rectangle (cr, x, y + 1, width, height - 2);
2331 gdk_cairo_set_source_rgba (cr, &lighter);
2332 add_path_line (cr, x, y, x + width - 2, y);
2335 gdk_cairo_set_source_rgba (cr, bg_color);
2336 add_path_line (cr, x + 1, y + 1, x + width - 2, y + 1);
2339 gdk_cairo_set_source_rgba (cr, &darker);
2340 add_path_line (cr, x, y + height - 2, x + width - 2, y + height - 2);
2341 add_path_line (cr, x + width - 2, y + 2, x + width - 2, y + height - 2);
2344 cairo_set_source_rgb (cr, 0, 0, 0);
2345 add_path_line (cr, x, y + height - 1, x + width - 2, y + height - 1);
2346 add_path_line (cr, x + width - 1, y + 1, x + width - 1, y + height - 2);
2351 gdk_cairo_set_source_rgba (cr, bg_color);
2352 cairo_rectangle (cr, x, y + 1, width, height - 2);
2355 gdk_cairo_set_source_rgba (cr, &lighter);
2356 add_path_line (cr, x + 1, y, x + width - 1, y);
2357 add_path_line (cr, x, y + 1, x, y + height - 2);
2360 gdk_cairo_set_source_rgba (cr, bg_color);
2361 add_path_line (cr, x + 1, y + 1, x + width - 1, y + 1);
2362 add_path_line (cr, x + 1, y + 1, x + 1, y + height - 2);
2365 gdk_cairo_set_source_rgba (cr, &darker);
2366 add_path_line (cr, x + 2, y + height - 2, x + width - 1, y + height - 2);
2369 cairo_set_source_rgb (cr, 0, 0, 0);
2370 add_path_line (cr, x + 1, y + height - 1, x + width - 1, y + height - 1);
2378 gdk_rgba_free (bg_color);
2382 render_dot (cairo_t *cr,
2383 const GdkRGBA *lighter,
2384 const GdkRGBA *darker,
2389 size = CLAMP ((gint) size, 2, 3);
2393 gdk_cairo_set_source_rgba (cr, lighter);
2394 cairo_rectangle (cr, x, y, 1, 1);
2395 cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2400 gdk_cairo_set_source_rgba (cr, lighter);
2401 cairo_rectangle (cr, x, y, 2, 1);
2402 cairo_rectangle (cr, x, y, 1, 2);
2405 gdk_cairo_set_source_rgba (cr, darker);
2406 cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2407 cairo_rectangle (cr, x + 2, y, 1, 2);
2413 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2420 GtkStateFlags flags;
2422 GdkRGBA lighter, darker;
2426 flags = gtk_theming_engine_get_state (engine);
2428 cairo_set_line_width (cr, 1);
2430 gtk_theming_engine_get (engine, flags,
2431 "background-color", &bg_color,
2433 color_shade (bg_color, 0.7, &darker);
2434 color_shade (bg_color, 1.3, &lighter);
2436 gdk_cairo_set_source_rgba (cr, bg_color);
2437 cairo_rectangle (cr, x, y, width, height);
2440 if (gtk_theming_engine_has_class (engine, "grip"))
2442 GtkJunctionSides sides;
2447 cairo_set_line_width (cr, 1.0);
2448 sides = gtk_theming_engine_get_junction_sides (engine);
2450 /* reduce confusing values to a meaningful state */
2451 if (sides & (GTK_JUNCTION_LEFT | GTK_JUNCTION_RIGHT))
2452 sides &= ~(GTK_JUNCTION_LEFT);
2454 if (sides & (GTK_JUNCTION_TOP | GTK_JUNCTION_BOTTOM))
2455 sides &= ~(GTK_JUNCTION_TOP);
2458 sides = (GTK_JUNCTION_BOTTOM | GTK_JUNCTION_RIGHT);
2460 /* align drawing area to the connected side */
2461 if (sides == GTK_JUNCTION_LEFT)
2466 else if (sides & (GTK_JUNCTION_LEFT | GTK_JUNCTION_TOP))
2470 else if (height < width)
2475 else if (sides & (GTK_JUNCTION_LEFT | GTK_JUNCTION_BOTTOM))
2477 /* make it square, aligning to bottom left */
2480 y += (height - width);
2483 else if (height < width)
2488 if (sides == GTK_JUNCTION_RIGHT)
2490 /* aligning to right */
2493 x += (width - height);
2497 else if (sides & (GTK_JUNCTION_RIGHT | GTK_JUNCTION_TOP))
2501 else if (height < width)
2503 x += (width - height);
2509 else if (sides & (GTK_JUNCTION_RIGHT | GTK_JUNCTION_BOTTOM))
2511 /* make it square, aligning to bottom right */
2514 y += (height - width);
2517 else if (height < width)
2519 x += (width - height);
2525 else if (sides == GTK_JUNCTION_TOP)
2530 else if (sides == GTK_JUNCTION_BOTTOM)
2532 /* align to bottom */
2535 y += (height - width);
2540 g_assert_not_reached ();
2542 if (sides == GTK_JUNCTION_LEFT ||
2543 sides == GTK_JUNCTION_RIGHT)
2549 while (xi < x + width)
2551 gdk_cairo_set_source_rgba (cr, &lighter);
2552 add_path_line (cr, x, y, x, y + height);
2556 gdk_cairo_set_source_rgba (cr, &darker);
2557 add_path_line (cr, xi, y, xi, y + height);
2562 else if (sides == GTK_JUNCTION_TOP ||
2563 sides == GTK_JUNCTION_BOTTOM)
2569 while (yi < y + height)
2571 gdk_cairo_set_source_rgba (cr, &lighter);
2572 add_path_line (cr, x, yi, x + width, yi);
2576 gdk_cairo_set_source_rgba (cr, &darker);
2577 add_path_line (cr, x, yi, x + width, yi);
2582 else if (sides == (GTK_JUNCTION_TOP | GTK_JUNCTION_LEFT))
2591 gdk_cairo_set_source_rgba (cr, &darker);
2592 add_path_line (cr, xi, y, x, yi);
2598 add_path_line (cr, xi, y, x, yi);
2604 gdk_cairo_set_source_rgba (cr, &lighter);
2605 add_path_line (cr, xi, y, x, yi);
2612 else if (sides == (GTK_JUNCTION_TOP | GTK_JUNCTION_RIGHT))
2619 while (xi < (x + width - 3))
2621 gdk_cairo_set_source_rgba (cr, &lighter);
2622 add_path_line (cr, xi, y, x + width, yi);
2628 gdk_cairo_set_source_rgba (cr, &darker);
2629 add_path_line (cr, xi, y, x + width, yi);
2635 add_path_line (cr, xi, y, x + width, yi);
2642 else if (sides == (GTK_JUNCTION_BOTTOM | GTK_JUNCTION_LEFT))
2651 gdk_cairo_set_source_rgba (cr, &darker);
2652 add_path_line (cr, x, yi, xi, y + height);
2658 add_path_line (cr, x, yi, xi, y + height);
2664 gdk_cairo_set_source_rgba (cr, &lighter);
2665 add_path_line (cr, x, yi, xi, y + height);
2672 else if (sides == (GTK_JUNCTION_BOTTOM | GTK_JUNCTION_RIGHT))
2679 while (xi < (x + width - 3))
2681 gdk_cairo_set_source_rgba (cr, &lighter);
2682 add_path_line (cr, xi, y + height, x + width, yi);
2688 gdk_cairo_set_source_rgba (cr, &darker);
2689 add_path_line (cr, xi, y + height, x + width, yi);
2695 add_path_line (cr, xi, y + height, x + width, yi);
2705 else if (gtk_theming_engine_has_class (engine, "paned"))
2708 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2709 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2711 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2712 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2716 for (yy = y; yy < y + height; yy += 3)
2717 for (xx = x; xx < x + width; xx += 6)
2719 render_dot (cr, &lighter, &darker, xx, yy, 2);
2720 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2726 gdk_rgba_free (bg_color);
2730 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2737 if (gtk_theming_engine_has_class (engine, "spinner"))
2739 GtkStateFlags state;
2740 guint num_steps, step;
2748 gtk_theming_engine_get_style (engine,
2749 "num-steps", &num_steps,
2752 state = gtk_theming_engine_get_state (engine);
2753 gtk_theming_engine_get (engine, state,
2759 if (gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2760 step = (guint) (progress * num_steps);
2766 cairo_translate (cr, x, y);
2768 /* draw clip region */
2769 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2773 radius = MIN (width / 2, height / 2);
2774 half = num_steps / 2;
2776 for (i = 0; i < num_steps; i++)
2778 gint inset = 0.7 * radius;
2780 /* transparency is a function of time and intial value */
2781 gdouble t = (gdouble) ((i + num_steps - step)
2782 % num_steps) / num_steps;
2786 cairo_set_source_rgba (cr,
2792 cairo_set_line_width (cr, 2.0);
2794 dx + (radius - inset) * cos (i * G_PI / half),
2795 dy + (radius - inset) * sin (i * G_PI / half));
2797 dx + radius * cos (i * G_PI / half),
2798 dy + radius * sin (i * G_PI / half));
2806 gdk_rgba_free (color);
2810 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2811 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2816 scale_or_ref (GdkPixbuf *src,
2820 if (width == gdk_pixbuf_get_width (src) &&
2821 height == gdk_pixbuf_get_height (src))
2822 return g_object_ref (src);
2824 return gdk_pixbuf_scale_simple (src,
2826 GDK_INTERP_BILINEAR);
2830 lookup_icon_size (GtkThemingEngine *engine,
2836 GtkSettings *settings;
2838 screen = gtk_theming_engine_get_screen (engine);
2839 settings = gtk_settings_get_for_screen (screen);
2841 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2845 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2846 const GtkIconSource *source,
2851 GdkPixbuf *base_pixbuf;
2852 GtkStateFlags state;
2856 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2857 state = gtk_theming_engine_get_state (engine);
2859 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2861 if (size != (GtkIconSize) -1 &&
2862 !lookup_icon_size (engine, size, &width, &height))
2864 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2868 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2871 if (size != (GtkIconSize) -1 &&
2872 gtk_icon_source_get_size_wildcarded (source))
2873 scaled = scale_or_ref (base_pixbuf, width, height);
2875 scaled = g_object_ref (base_pixbuf);
2877 /* If the state was wildcarded, then generate a state. */
2878 if (gtk_icon_source_get_state_wildcarded (source))
2880 if (state & GTK_STATE_FLAG_INSENSITIVE)
2882 stated = gdk_pixbuf_copy (scaled);
2883 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2885 g_object_unref (scaled);
2887 else if (state & GTK_STATE_FLAG_PRELIGHT)
2889 stated = gdk_pixbuf_copy (scaled);
2890 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2892 g_object_unref (scaled);