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 GtkSymbolicColor *literal, *shade;
1240 literal = gtk_symbolic_color_new_literal (color);
1241 shade = gtk_symbolic_color_new_shade (literal, factor);
1242 gtk_symbolic_color_unref (literal);
1244 gtk_symbolic_color_resolve (shade, NULL, color_return);
1245 gtk_symbolic_color_unref (shade);
1249 _cairo_round_rectangle_sides (cairo_t *cr,
1256 GtkJunctionSides junction)
1258 radius = CLAMP (radius, 0, MIN (width / 2, height / 2));
1260 if (sides & SIDE_RIGHT)
1263 (junction & GTK_JUNCTION_TOP) ||
1264 (junction & GTK_JUNCTION_RIGHT))
1265 cairo_move_to (cr, x + width, y);
1268 cairo_new_sub_path (cr);
1269 cairo_arc (cr, x + width - radius, y + radius, radius, - G_PI / 4, 0);
1273 (junction & GTK_JUNCTION_BOTTOM) ||
1274 (junction & GTK_JUNCTION_RIGHT))
1275 cairo_line_to (cr, x + width, y + height);
1277 cairo_arc (cr, x + width - radius, y + height - radius, radius, 0, G_PI / 4);
1280 if (sides & SIDE_BOTTOM)
1283 ! (junction & GTK_JUNCTION_RIGHT) &&
1284 ! (junction & GTK_JUNCTION_BOTTOM))
1286 if ((sides & SIDE_RIGHT) == 0)
1287 cairo_new_sub_path (cr);
1289 cairo_arc (cr, x + width - radius, y + height - radius, radius, G_PI / 4, G_PI / 2);
1293 (junction & GTK_JUNCTION_BOTTOM) ||
1294 (junction & GTK_JUNCTION_LEFT))
1295 cairo_line_to (cr, x, y + height);
1297 cairo_arc (cr, x + radius, y + height - radius, radius, G_PI / 2, 3 * (G_PI / 4));
1300 cairo_move_to (cr, x, y + height);
1302 if (sides & SIDE_LEFT)
1305 ! (junction & GTK_JUNCTION_LEFT) &&
1306 ! (junction & GTK_JUNCTION_BOTTOM))
1308 if ((sides & SIDE_BOTTOM) == 0)
1309 cairo_new_sub_path (cr);
1311 cairo_arc (cr, x + radius, y + height - radius, radius, 3 * (G_PI / 4), G_PI);
1315 (junction & GTK_JUNCTION_TOP) ||
1316 (junction & GTK_JUNCTION_LEFT))
1317 cairo_line_to (cr, x, y);
1319 cairo_arc (cr, x + radius, y + radius, radius, G_PI, G_PI + G_PI / 4);
1322 if (sides & SIDE_TOP)
1325 ! (junction & GTK_JUNCTION_TOP) &&
1326 ! (junction & GTK_JUNCTION_LEFT))
1328 if ((sides & SIDE_TOP) == 0)
1329 cairo_new_sub_path (cr);
1331 cairo_arc (cr, x + radius, y + radius, radius, 5 * (G_PI / 4), 3 * (G_PI / 2));
1335 (junction & GTK_JUNCTION_TOP) ||
1336 (junction & GTK_JUNCTION_RIGHT))
1337 cairo_line_to (cr, x + width, y);
1339 cairo_arc (cr, x + width - radius, y + radius, radius, 3 * (G_PI / 2), - G_PI / 4);
1344 gtk_theming_engine_render_background (GtkThemingEngine *engine,
1352 cairo_pattern_t *pattern;
1353 GtkStateFlags flags;
1355 gdouble progress, alpha = 1;
1356 GtkJunctionSides junction;
1357 gint radius, border_width;
1359 flags = gtk_theming_engine_get_state (engine);
1360 junction = gtk_theming_engine_get_junction_sides (engine);
1363 if (gtk_theming_engine_has_class (engine, "spinbutton") &&
1364 gtk_theming_engine_has_class (engine, "button"))
1372 gtk_theming_engine_get (engine, flags,
1373 "background-image", &pattern,
1374 "background-color", &bg_color,
1375 "border-width", &border_width,
1376 "border-radius", &radius,
1379 running = gtk_theming_engine_state_is_running (engine, GTK_STATE_PRELIGHT, &progress);
1381 if (border_width > 0)
1385 width -= 2 * border_width;
1386 height -= 2 * border_width;
1387 radius -= 2 * border_width;
1393 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1394 x, y, width, height,
1395 SIDE_ALL, junction);
1398 if (gtk_theming_engine_has_class (engine, "background"))
1400 cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.0); /* transparent */
1401 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
1405 cairo_translate (cr, x, y);
1406 cairo_scale (cr, width, height);
1410 cairo_pattern_t *other_pattern;
1411 GtkStateFlags other_flags;
1413 cairo_pattern_t *new_pattern = NULL;
1415 if (flags & GTK_STATE_FLAG_PRELIGHT)
1417 other_flags = flags & ~(GTK_STATE_FLAG_PRELIGHT);
1418 progress = 1 - progress;
1421 other_flags = flags | GTK_STATE_FLAG_PRELIGHT;
1423 gtk_theming_engine_get (engine, other_flags,
1424 "background-image", &other_pattern,
1425 "background-color", &other_bg,
1428 if (pattern && other_pattern)
1430 cairo_pattern_type_t type, other_type;
1433 cairo_pattern_get_color_stop_count (pattern, &n0);
1434 cairo_pattern_get_color_stop_count (other_pattern, &n1);
1435 type = cairo_pattern_get_type (pattern);
1436 other_type = cairo_pattern_get_type (other_pattern);
1438 if (type == other_type && n0 == n1)
1440 gdouble offset0, red0, green0, blue0, alpha0;
1441 gdouble offset1, red1, green1, blue1, alpha1;
1442 gdouble x00, x01, y00, y01, x10, x11, y10, y11;
1443 gdouble r00, r01, r10, r11;
1446 if (type == CAIRO_PATTERN_TYPE_LINEAR)
1448 cairo_pattern_get_linear_points (pattern, &x00, &y00, &x01, &y01);
1449 cairo_pattern_get_linear_points (other_pattern, &x10, &y10, &x11, &y11);
1451 new_pattern = cairo_pattern_create_linear (x00 + (x10 - x00) * progress,
1452 y00 + (y10 - y00) * progress,
1453 x01 + (x11 - x01) * progress,
1454 y01 + (y11 - y01) * progress);
1458 cairo_pattern_get_radial_circles (pattern, &x00, &y00, &r00, &x01, &y01, &r01);
1459 cairo_pattern_get_radial_circles (other_pattern, &x10, &y10, &r10, &x11, &y11, &r11);
1461 new_pattern = cairo_pattern_create_radial (x00 + (x10 - x00) * progress,
1462 y00 + (y10 - y00) * progress,
1463 r00 + (r10 - r00) * progress,
1464 x01 + (x11 - x01) * progress,
1465 y01 + (y11 - y01) * progress,
1466 r01 + (r11 - r01) * progress);
1469 cairo_pattern_set_filter (new_pattern, CAIRO_FILTER_FAST);
1472 /* Blend both gradients into one */
1473 while (i < n0 && i < n1)
1475 cairo_pattern_get_color_stop_rgba (pattern, i,
1477 &red0, &green0, &blue0,
1479 cairo_pattern_get_color_stop_rgba (other_pattern, i,
1481 &red1, &green1, &blue1,
1484 cairo_pattern_add_color_stop_rgba (new_pattern,
1485 offset0 + ((offset1 - offset0) * progress),
1486 red0 + ((red1 - red0) * progress),
1487 green0 + ((green1 - green0) * progress),
1488 blue0 + ((blue1 - blue0) * progress),
1489 alpha0 + ((alpha1 - alpha0) * progress));
1495 /* Different pattern types, or different color
1496 * stop counts, alpha blend both patterns.
1498 cairo_rectangle (cr, 0, 0, 1, 1);
1499 cairo_set_source (cr, other_pattern);
1500 cairo_fill_preserve (cr);
1502 /* Set alpha for posterior drawing
1503 * of the target pattern
1505 alpha = 1 - progress;
1508 else if (pattern || other_pattern)
1512 gdouble x0, y0, x1, y1, r0, r1;
1515 /* Blend a pattern with a color */
1520 progress = 1 - progress;
1528 if (cairo_pattern_get_type (p) == CAIRO_PATTERN_TYPE_LINEAR)
1530 cairo_pattern_get_linear_points (p, &x0, &y0, &x1, &y1);
1531 new_pattern = cairo_pattern_create_linear (x0, y0, x1, y1);
1535 cairo_pattern_get_radial_circles (p, &x0, &y0, &r0, &x1, &y1, &r1);
1536 new_pattern = cairo_pattern_create_radial (x0, y0, r0, x1, y1, r1);
1539 cairo_pattern_get_color_stop_count (p, &n);
1541 for (i = 0; i < n; i++)
1543 gdouble red1, green1, blue1, alpha1;
1546 cairo_pattern_get_color_stop_rgba (p, i,
1548 &red1, &green1, &blue1,
1550 cairo_pattern_add_color_stop_rgba (new_pattern, offset,
1551 c->red + ((red1 - c->red) * progress),
1552 c->green + ((green1 - c->green) * progress),
1553 c->blue + ((blue1 - c->blue) * progress),
1554 c->alpha + ((alpha1 - c->alpha) * progress));
1559 const GdkRGBA *color, *other_color;
1561 /* Merge just colors */
1563 other_color = other_bg;
1565 new_pattern = cairo_pattern_create_rgba (CLAMP (color->red + ((other_color->red - color->red) * progress), 0, 1),
1566 CLAMP (color->green + ((other_color->green - color->green) * progress), 0, 1),
1567 CLAMP (color->blue + ((other_color->blue - color->blue) * progress), 0, 1),
1568 CLAMP (color->alpha + ((other_color->alpha - color->alpha) * progress), 0, 1));
1573 /* Replace pattern to use */
1574 cairo_pattern_destroy (pattern);
1575 pattern = new_pattern;
1579 cairo_pattern_destroy (other_pattern);
1582 gdk_rgba_free (other_bg);
1585 cairo_rectangle (cr, 0, 0, 1, 1);
1589 cairo_set_source (cr, pattern);
1590 cairo_pattern_destroy (pattern);
1593 gdk_cairo_set_source_rgba (cr, bg_color);
1599 cairo_pattern_t *mask;
1601 mask = cairo_pattern_create_rgba (1, 1, 1, alpha);
1602 cairo_mask (cr, mask);
1603 cairo_pattern_destroy (mask);
1608 gdk_rgba_free (bg_color);
1612 gtk_theming_engine_render_frame (GtkThemingEngine *engine,
1619 GtkStateFlags flags;
1621 GdkRGBA *border_color;
1623 GtkBorderStyle border_style;
1624 gint border_width, radius;
1625 GtkJunctionSides junction;
1628 flags = gtk_theming_engine_get_state (engine);
1629 junction = gtk_theming_engine_get_junction_sides (engine);
1631 gtk_theming_engine_get (engine, flags,
1632 "border-image", &slice,
1633 "border-color", &border_color,
1634 "border-style", &border_style,
1635 "border-width", &border_width,
1636 "border-radius", &radius,
1641 gtk_9slice_render (slice, cr, x, y, width, height);
1642 gtk_9slice_unref (slice);
1644 else if (border_color &&
1645 border_style != GTK_BORDER_STYLE_NONE)
1649 color_shade (border_color, 1.8, &lighter);
1651 switch (border_style)
1653 case GTK_BORDER_STYLE_NONE:
1655 case GTK_BORDER_STYLE_SOLID:
1656 cairo_set_line_width (cr, border_width);
1657 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1659 if (border_width > 1)
1661 x += (gdouble) border_width / 2;
1662 y += (gdouble) border_width / 2;
1663 width -= border_width;
1664 height -= border_width;
1667 _cairo_round_rectangle_sides (cr, (gdouble) radius, x, y, width, height,
1668 SIDE_ALL, junction);
1669 gdk_cairo_set_source_rgba (cr, border_color);
1673 case GTK_BORDER_STYLE_INSET:
1674 case GTK_BORDER_STYLE_OUTSET:
1675 cairo_set_line_width (cr, border_width);
1676 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1680 if (border_width > 1)
1682 d1 = (gdouble) border_width / 2;
1683 d2 = (gdouble) (border_width - (gint) d1) + 1;
1688 m = MIN (width, height);
1691 /* Only needed for square frames to have a 3D-like
1692 * feeling, for rounded ones, the arc will ensure
1693 * the stroke is painted to end at 45°.
1697 cairo_move_to (cr, x, y + height);
1698 cairo_line_to (cr, x + m, y + height - m);
1699 cairo_line_to (cr, x + width - m, y + m);
1700 cairo_line_to (cr, x + width, y);
1701 cairo_line_to (cr, x + width, y + height);
1702 cairo_close_path (cr);
1707 if (border_style == GTK_BORDER_STYLE_INSET)
1708 gdk_cairo_set_source_rgba (cr, &lighter);
1710 gdk_cairo_set_source_rgba (cr, border_color);
1712 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1714 width - d2, height - d2,
1715 SIDE_BOTTOM | SIDE_RIGHT, junction);
1724 cairo_move_to (cr, x, y + height);
1725 cairo_line_to (cr, x + m, y + height - m);
1726 cairo_line_to (cr, x + width - m, y + m);
1727 cairo_line_to (cr, x + width, y);
1728 cairo_line_to (cr, x, y);
1729 cairo_close_path (cr);
1734 if (border_style == GTK_BORDER_STYLE_INSET)
1735 gdk_cairo_set_source_rgba (cr, border_color);
1737 gdk_cairo_set_source_rgba (cr, &lighter);
1739 _cairo_round_rectangle_sides (cr, (gdouble) radius,
1741 width - d2, height - d2,
1742 SIDE_TOP | SIDE_LEFT, junction);
1754 gdk_rgba_free (border_color);
1758 gtk_theming_engine_render_expander (GtkThemingEngine *engine,
1765 GtkStateFlags flags;
1766 GdkRGBA *bg_color, *fg_color;
1767 double vertical_overshoot;
1770 double interp; /* interpolation factor for center position */
1771 double x_double_horz, y_double_horz;
1772 double x_double_vert, y_double_vert;
1773 double x_double, y_double;
1778 flags = gtk_theming_engine_get_state (engine);
1780 gtk_theming_engine_get (engine, flags,
1782 "background-color", &bg_color,
1787 /* FIXME: LTR/RTL */
1788 if (flags & GTK_STATE_FLAG_ACTIVE)
1799 /* Compute distance that the stroke extends beyonds the end
1800 * of the triangle we draw.
1802 vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
1804 /* For odd line widths, we end the vertical line of the triangle
1805 * at a half pixel, so we round differently.
1807 if (line_width % 2 == 1)
1808 vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
1810 vertical_overshoot = ceil (vertical_overshoot);
1812 /* Adjust the size of the triangle we draw so that the entire stroke fits
1814 diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
1816 /* If the line width is odd, we want the diameter to be even,
1817 * and vice versa, so force the sum to be odd. This relationship
1818 * makes the point of the triangle look right.
1820 diameter -= (1 - (diameter + line_width) % 2);
1822 radius = diameter / 2.;
1824 /* Adjust the center so that the stroke is properly aligned with
1825 * the pixel grid. The center adjustment is different for the
1826 * horizontal and vertical orientations. For intermediate positions
1827 * we interpolate between the two.
1829 x_double_vert = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1830 y_double_vert = (y + height / 2) - 0.5;
1832 x_double_horz = (x + width / 2) - 0.5;
1833 y_double_horz = floor ((y + height / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
1835 x_double = x_double_vert * (1 - interp) + x_double_horz * interp;
1836 y_double = y_double_vert * (1 - interp) + y_double_horz * interp;
1838 cairo_translate (cr, x_double, y_double);
1839 cairo_rotate (cr, angle);
1841 cairo_move_to (cr, - radius / 2., - radius);
1842 cairo_line_to (cr, radius / 2., 0);
1843 cairo_line_to (cr, - radius / 2., radius);
1844 cairo_close_path (cr);
1846 cairo_set_line_width (cr, line_width);
1848 gdk_cairo_set_source_rgba (cr, fg_color);
1850 cairo_fill_preserve (cr);
1852 gdk_cairo_set_source_rgba (cr, fg_color);
1857 gdk_rgba_free (fg_color);
1858 gdk_rgba_free (bg_color);
1862 gtk_theming_engine_render_focus (GtkThemingEngine *engine,
1869 GtkStateFlags flags;
1875 flags = gtk_theming_engine_get_state (engine);
1877 gtk_theming_engine_get (engine, flags,
1881 gtk_theming_engine_get_style (engine,
1882 "focus-line-width", &line_width,
1883 "focus-line-pattern", (gchar *) &dash_list,
1886 cairo_set_line_width (cr, (gdouble) line_width);
1890 gint n_dashes = strlen ((const gchar *) dash_list);
1891 gdouble *dashes = g_new (gdouble, n_dashes);
1892 gdouble total_length = 0;
1893 gdouble dash_offset;
1896 for (i = 0; i < n_dashes; i++)
1898 dashes[i] = dash_list[i];
1899 total_length += dash_list[i];
1902 /* The dash offset here aligns the pattern to integer pixels
1903 * by starting the dash at the right side of the left border
1904 * Negative dash offsets in cairo don't work
1905 * (https://bugs.freedesktop.org/show_bug.cgi?id=2729)
1907 dash_offset = - line_width / 2.;
1909 while (dash_offset < 0)
1910 dash_offset += total_length;
1912 cairo_set_dash (cr, dashes, n_dashes, dash_offset);
1916 cairo_rectangle (cr,
1917 x + line_width / 2.,
1918 y + line_width / 2.,
1920 height - line_width);
1922 gdk_cairo_set_source_rgba (cr, color);
1927 gdk_rgba_free (color);
1932 gtk_theming_engine_render_line (GtkThemingEngine *engine,
1939 GdkRGBA *bg_color, darker, lighter;
1940 GtkStateFlags flags;
1941 gint i, thickness, thickness_dark, thickness_light, len;
1942 cairo_matrix_t matrix;
1945 /* FIXME: thickness */
1947 thickness_dark = thickness / 2;
1948 thickness_light = thickness - thickness_dark;
1950 flags = gtk_theming_engine_get_state (engine);
1953 gtk_theming_engine_get (engine, flags,
1954 "background-color", &bg_color,
1956 color_shade (bg_color, 0.7, &darker);
1957 color_shade (bg_color, 1.3, &lighter);
1959 cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1960 cairo_set_line_width (cr, 1);
1962 angle = atan2 (x1 - x0, y1 - y0);
1963 angle = (2 * G_PI) - angle;
1966 cairo_get_matrix (cr, &matrix);
1967 cairo_matrix_translate (&matrix, x0, y0);
1968 cairo_matrix_rotate (&matrix, angle);
1969 cairo_set_matrix (cr, &matrix);
1974 len = (gint) sqrt ((x1 * x1) + (y1 * y1));
1976 y0 = -thickness_dark;
1978 for (i = 0; i < thickness_dark; i++)
1980 gdk_cairo_set_source_rgba (cr, &lighter);
1981 add_path_line (cr, len - i - 1.5, y0, len - 0.5, y0);
1984 gdk_cairo_set_source_rgba (cr, &darker);
1985 add_path_line (cr, 0.5, y0, len - i - 1.5, y0);
1991 for (i = 0; i < thickness_light; i++)
1993 gdk_cairo_set_source_rgba (cr, &darker);
1994 add_path_line (cr, 0.5, y0, thickness_light - i + 0.5, y0);
1997 gdk_cairo_set_source_rgba (cr, &lighter);
1998 add_path_line (cr, thickness_light - i + 0.5, y0, len - 0.5, y0);
2006 gdk_rgba_free (bg_color);
2010 gtk_theming_engine_render_layout (GtkThemingEngine *engine,
2014 PangoLayout *layout)
2016 const PangoMatrix *matrix;
2018 GtkStateFlags flags;
2022 flags = gtk_theming_engine_get_state (engine);
2024 gtk_theming_engine_get (engine, flags,
2028 screen = gtk_theming_engine_get_screen (engine);
2029 matrix = pango_context_get_matrix (pango_layout_get_context (layout));
2033 cairo_matrix_t cairo_matrix;
2034 PangoMatrix tmp_matrix;
2035 PangoRectangle rect;
2037 cairo_matrix_init (&cairo_matrix,
2038 matrix->xx, matrix->yx,
2039 matrix->xy, matrix->yy,
2040 matrix->x0, matrix->y0);
2042 pango_layout_get_extents (layout, NULL, &rect);
2043 pango_matrix_transform_rectangle (matrix, &rect);
2044 pango_extents_to_pixels (&rect, NULL);
2046 tmp_matrix = *matrix;
2047 cairo_matrix.x0 += x - rect.x;
2048 cairo_matrix.y0 += y - rect.y;
2050 cairo_set_matrix (cr, &cairo_matrix);
2053 cairo_translate (cr, x, y);
2055 if (flags & GTK_STATE_FLAG_INSENSITIVE)
2058 cairo_set_source_rgb (cr, 1, 1, 1);
2059 cairo_move_to (cr, 1, 1);
2060 _gtk_pango_fill_layout (cr, layout);
2064 gdk_cairo_set_source_rgba (cr, fg_color);
2065 pango_cairo_show_layout (cr, layout);
2069 gdk_rgba_free (fg_color);
2073 gtk_theming_engine_render_slider (GtkThemingEngine *engine,
2079 GtkOrientation orientation)
2081 const GtkWidgetPath *path;
2084 path = gtk_theming_engine_get_path (engine);
2086 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2087 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2089 /* FIXME: thickness */
2092 if (gtk_widget_path_is_type (path, GTK_TYPE_SCALE))
2094 if (orientation == GTK_ORIENTATION_VERTICAL)
2095 gtk_theming_engine_render_line (engine, cr,
2097 y + (gint) height / 2,
2098 x + width - thickness - 1,
2099 y + (gint) height / 2);
2101 gtk_theming_engine_render_line (engine, cr,
2102 x + (gint) width / 2,
2104 x + (gint) width / 2,
2105 y + height - thickness - 1);
2110 gtk_theming_engine_render_frame_gap (GtkThemingEngine *engine,
2116 GtkPositionType gap_side,
2120 GtkStateFlags flags;
2122 GdkRGBA lighter, darker;
2126 flags = gtk_theming_engine_get_state (engine);
2128 cairo_set_line_width (cr, 1);
2130 gtk_theming_engine_get (engine, flags,
2131 "background-color", &bg_color,
2133 color_shade (bg_color, 0.7, &darker);
2134 color_shade (bg_color, 1.3, &lighter);
2136 if (gtk_theming_engine_has_class (engine, "frame"))
2138 if (gap_side == GTK_POS_RIGHT)
2139 sides = SIDE_BOTTOM;
2140 else if (gap_side == GTK_POS_BOTTOM)
2143 sides = SIDE_BOTTOM | SIDE_RIGHT;
2145 gdk_cairo_set_source_rgba (cr, &lighter);
2146 add_path_rectangle_sides (cr, x , y, width , height, sides);
2149 gdk_cairo_set_source_rgba (cr, &darker);
2150 add_path_rectangle_sides (cr, x, y, width - 1, height - 1, sides);
2153 if (gap_side == GTK_POS_RIGHT ||
2154 gap_side == GTK_POS_BOTTOM)
2156 gdk_cairo_set_source_rgba (cr, &darker);
2157 add_path_gap_side (cr, gap_side,
2158 x + 1, y + 1, width - 4, height - 4,
2162 gdk_cairo_set_source_rgba (cr, &lighter);
2163 add_path_gap_side (cr, gap_side,
2164 x, y, width, height,
2169 if (gap_side == GTK_POS_LEFT)
2171 else if (gap_side == GTK_POS_TOP)
2174 sides = SIDE_TOP | SIDE_LEFT;
2176 gdk_cairo_set_source_rgba (cr, &lighter);
2177 add_path_rectangle_sides (cr, x + 1, y + 1, width - 2, height - 3, sides);
2180 gdk_cairo_set_source_rgba (cr, &darker);
2181 add_path_rectangle_sides (cr, x, y, width - 1, height - 1, sides);
2184 if (gap_side == GTK_POS_LEFT ||
2185 gap_side == GTK_POS_TOP)
2187 gdk_cairo_set_source_rgba (cr, &lighter);
2188 add_path_gap_side (cr, gap_side,
2189 x + 1, y + 1, width - 4, height - 4,
2193 gdk_cairo_set_source_rgba (cr, &darker);
2194 add_path_gap_side (cr, gap_side,
2195 x, y, width - 2, height - 2,
2202 if (gap_side == GTK_POS_RIGHT)
2203 sides = SIDE_BOTTOM;
2204 else if (gap_side == GTK_POS_BOTTOM)
2207 sides = SIDE_BOTTOM | SIDE_RIGHT;
2209 gdk_cairo_set_source_rgba (cr, &darker);
2210 add_path_rectangle_sides (cr, x + 1, y, width - 2, height, sides);
2211 add_path_rectangle_sides (cr, x, y + 1, width, height - 2, sides);
2214 cairo_set_source_rgb (cr, 0, 0, 0);
2215 add_path_rectangle_sides (cr, x, y, width, height, sides);
2218 if (gap_side == GTK_POS_RIGHT ||
2219 gap_side == GTK_POS_BOTTOM)
2221 gdk_cairo_set_source_rgba (cr, &darker);
2222 add_path_gap_side (cr, gap_side,
2223 x, y, width - 2, height - 2,
2227 cairo_set_source_rgb (cr, 0, 0, 0);
2228 add_path_gap_side (cr, gap_side,
2229 x, y, width - 1, height - 1,
2234 if (gap_side == GTK_POS_LEFT)
2236 else if (gap_side == GTK_POS_TOP)
2239 sides = SIDE_TOP | SIDE_LEFT;
2241 gdk_cairo_set_source_rgba (cr, &lighter);
2242 add_path_rectangle_sides (cr, x, y, width, height, sides);
2245 if (gap_side == GTK_POS_LEFT ||
2246 gap_side == GTK_POS_TOP)
2248 add_path_gap_side (cr, gap_side,
2249 x, y, width, height,
2257 gdk_rgba_free (bg_color);
2261 gtk_theming_engine_render_extension (GtkThemingEngine *engine,
2267 GtkPositionType gap_side)
2269 GtkStateFlags flags;
2271 GdkRGBA lighter, darker;
2274 flags = gtk_theming_engine_get_state (engine);
2276 cairo_set_line_width (cr, 1);
2278 gtk_theming_engine_get (engine, flags,
2279 "background-color", &bg_color,
2281 color_shade (bg_color, 0.7, &darker);
2282 color_shade (bg_color, 1.3, &lighter);
2287 gdk_cairo_set_source_rgba (cr, bg_color);
2288 cairo_rectangle (cr, x + 1, y, width - 2, height);
2291 gdk_cairo_set_source_rgba (cr, &lighter);
2292 add_path_line (cr, x, y, x, y + height - 2);
2295 gdk_cairo_set_source_rgba (cr, bg_color);
2296 add_path_line (cr, x + 1, y, x + 1, y + height - 2);
2299 gdk_cairo_set_source_rgba (cr, &darker);
2300 add_path_line (cr, x + 2, y + height - 2, x + width - 2, y + height - 2);
2301 add_path_line (cr, x + width - 2, y, x + width - 2, y + height - 2);
2304 cairo_set_source_rgb (cr, 0, 0, 0);
2305 add_path_line (cr, x + 1, y + height - 1, x + width - 2, y + height - 1);
2306 add_path_line (cr, x + width - 1, y, x + width - 1, y + height - 2);
2310 case GTK_POS_BOTTOM:
2311 gdk_cairo_set_source_rgba(cr, bg_color);
2312 cairo_rectangle (cr, x + 1, y, width - 2, height);
2315 gdk_cairo_set_source_rgba (cr, &lighter);
2316 add_path_line (cr, x + 1, y, x + width - 2, y);
2317 add_path_line (cr, x, y + 1, x, y + height - 1);
2320 gdk_cairo_set_source_rgba (cr, bg_color);
2321 add_path_line (cr, x + 1, y + 1, x + width - 2, y + 1);
2322 add_path_line (cr, x + 1, y + 1, x + 1, y + height - 1);
2325 gdk_cairo_set_source_rgba (cr, &darker);
2326 add_path_line (cr, x + width - 2, y + 2, x + width - 2, y + height - 1);
2329 cairo_set_source_rgb (cr, 0, 0, 0);
2330 add_path_line (cr, x + width - 1, y + 1, x + width - 1, y + height - 1);
2335 gdk_cairo_set_source_rgba (cr, bg_color);
2336 cairo_rectangle (cr, x, y + 1, width, height - 2);
2339 gdk_cairo_set_source_rgba (cr, &lighter);
2340 add_path_line (cr, x, y, x + width - 2, y);
2343 gdk_cairo_set_source_rgba (cr, bg_color);
2344 add_path_line (cr, x + 1, y + 1, x + width - 2, y + 1);
2347 gdk_cairo_set_source_rgba (cr, &darker);
2348 add_path_line (cr, x, y + height - 2, x + width - 2, y + height - 2);
2349 add_path_line (cr, x + width - 2, y + 2, x + width - 2, y + height - 2);
2352 cairo_set_source_rgb (cr, 0, 0, 0);
2353 add_path_line (cr, x, y + height - 1, x + width - 2, y + height - 1);
2354 add_path_line (cr, x + width - 1, y + 1, x + width - 1, y + height - 2);
2359 gdk_cairo_set_source_rgba (cr, bg_color);
2360 cairo_rectangle (cr, x, y + 1, width, height - 2);
2363 gdk_cairo_set_source_rgba (cr, &lighter);
2364 add_path_line (cr, x + 1, y, x + width - 1, y);
2365 add_path_line (cr, x, y + 1, x, y + height - 2);
2368 gdk_cairo_set_source_rgba (cr, bg_color);
2369 add_path_line (cr, x + 1, y + 1, x + width - 1, y + 1);
2370 add_path_line (cr, x + 1, y + 1, x + 1, y + height - 2);
2373 gdk_cairo_set_source_rgba (cr, &darker);
2374 add_path_line (cr, x + 2, y + height - 2, x + width - 1, y + height - 2);
2377 cairo_set_source_rgb (cr, 0, 0, 0);
2378 add_path_line (cr, x + 1, y + height - 1, x + width - 1, y + height - 1);
2386 gdk_rgba_free (bg_color);
2390 render_dot (cairo_t *cr,
2391 const GdkRGBA *lighter,
2392 const GdkRGBA *darker,
2397 size = CLAMP ((gint) size, 2, 3);
2401 gdk_cairo_set_source_rgba (cr, lighter);
2402 cairo_rectangle (cr, x, y, 1, 1);
2403 cairo_rectangle (cr, x + 1, y + 1, 1, 1);
2408 gdk_cairo_set_source_rgba (cr, lighter);
2409 cairo_rectangle (cr, x, y, 2, 1);
2410 cairo_rectangle (cr, x, y, 1, 2);
2413 gdk_cairo_set_source_rgba (cr, darker);
2414 cairo_rectangle (cr, x + 1, y + 1, 2, 1);
2415 cairo_rectangle (cr, x + 2, y, 1, 2);
2421 gtk_theming_engine_render_handle (GtkThemingEngine *engine,
2428 GtkStateFlags flags;
2430 GdkRGBA lighter, darker;
2434 flags = gtk_theming_engine_get_state (engine);
2436 cairo_set_line_width (cr, 1);
2438 gtk_theming_engine_get (engine, flags,
2439 "background-color", &bg_color,
2441 color_shade (bg_color, 0.7, &darker);
2442 color_shade (bg_color, 1.3, &lighter);
2444 gdk_cairo_set_source_rgba (cr, bg_color);
2445 cairo_rectangle (cr, x, y, width, height);
2448 if (gtk_theming_engine_has_class (engine, "grip"))
2450 GtkJunctionSides sides;
2455 cairo_set_line_width (cr, 1.0);
2456 sides = gtk_theming_engine_get_junction_sides (engine);
2458 /* reduce confusing values to a meaningful state */
2459 if (sides & (GTK_JUNCTION_LEFT | GTK_JUNCTION_RIGHT))
2460 sides &= ~(GTK_JUNCTION_LEFT);
2462 if (sides & (GTK_JUNCTION_TOP | GTK_JUNCTION_BOTTOM))
2463 sides &= ~(GTK_JUNCTION_TOP);
2466 sides = (GTK_JUNCTION_BOTTOM | GTK_JUNCTION_RIGHT);
2468 /* align drawing area to the connected side */
2469 if (sides == GTK_JUNCTION_LEFT)
2474 else if (sides & (GTK_JUNCTION_LEFT | GTK_JUNCTION_TOP))
2478 else if (height < width)
2483 else if (sides & (GTK_JUNCTION_LEFT | GTK_JUNCTION_BOTTOM))
2485 /* make it square, aligning to bottom left */
2488 y += (height - width);
2491 else if (height < width)
2496 if (sides == GTK_JUNCTION_RIGHT)
2498 /* aligning to right */
2501 x += (width - height);
2505 else if (sides & (GTK_JUNCTION_RIGHT | GTK_JUNCTION_TOP))
2509 else if (height < width)
2511 x += (width - height);
2517 else if (sides & (GTK_JUNCTION_RIGHT | GTK_JUNCTION_BOTTOM))
2519 /* make it square, aligning to bottom right */
2522 y += (height - width);
2525 else if (height < width)
2527 x += (width - height);
2533 else if (sides == GTK_JUNCTION_TOP)
2538 else if (sides == GTK_JUNCTION_BOTTOM)
2540 /* align to bottom */
2543 y += (height - width);
2548 g_assert_not_reached ();
2550 if (sides == GTK_JUNCTION_LEFT ||
2551 sides == GTK_JUNCTION_RIGHT)
2557 while (xi < x + width)
2559 gdk_cairo_set_source_rgba (cr, &lighter);
2560 add_path_line (cr, x, y, x, y + height);
2564 gdk_cairo_set_source_rgba (cr, &darker);
2565 add_path_line (cr, xi, y, xi, y + height);
2570 else if (sides == GTK_JUNCTION_TOP ||
2571 sides == GTK_JUNCTION_BOTTOM)
2577 while (yi < y + height)
2579 gdk_cairo_set_source_rgba (cr, &lighter);
2580 add_path_line (cr, x, yi, x + width, yi);
2584 gdk_cairo_set_source_rgba (cr, &darker);
2585 add_path_line (cr, x, yi, x + width, yi);
2590 else if (sides == (GTK_JUNCTION_TOP | GTK_JUNCTION_LEFT))
2599 gdk_cairo_set_source_rgba (cr, &darker);
2600 add_path_line (cr, xi, y, x, yi);
2606 add_path_line (cr, xi, y, x, yi);
2612 gdk_cairo_set_source_rgba (cr, &lighter);
2613 add_path_line (cr, xi, y, x, yi);
2620 else if (sides == (GTK_JUNCTION_TOP | GTK_JUNCTION_RIGHT))
2627 while (xi < (x + width - 3))
2629 gdk_cairo_set_source_rgba (cr, &lighter);
2630 add_path_line (cr, xi, y, x + width, yi);
2636 gdk_cairo_set_source_rgba (cr, &darker);
2637 add_path_line (cr, xi, y, x + width, yi);
2643 add_path_line (cr, xi, y, x + width, yi);
2650 else if (sides == (GTK_JUNCTION_BOTTOM | GTK_JUNCTION_LEFT))
2659 gdk_cairo_set_source_rgba (cr, &darker);
2660 add_path_line (cr, x, yi, xi, y + height);
2666 add_path_line (cr, x, yi, xi, y + height);
2672 gdk_cairo_set_source_rgba (cr, &lighter);
2673 add_path_line (cr, x, yi, xi, y + height);
2680 else if (sides == (GTK_JUNCTION_BOTTOM | GTK_JUNCTION_RIGHT))
2687 while (xi < (x + width - 3))
2689 gdk_cairo_set_source_rgba (cr, &lighter);
2690 add_path_line (cr, xi, y + height, x + width, yi);
2696 gdk_cairo_set_source_rgba (cr, &darker);
2697 add_path_line (cr, xi, y + height, x + width, yi);
2703 add_path_line (cr, xi, y + height, x + width, yi);
2713 else if (gtk_theming_engine_has_class (engine, "paned"))
2716 for (xx = x + width / 2 - 15; xx <= x + width / 2 + 15; xx += 5)
2717 render_dot (cr, &lighter, &darker, xx, y + height / 2 - 1, 3);
2719 for (yy = y + height / 2 - 15; yy <= y + height / 2 + 15; yy += 5)
2720 render_dot (cr, &lighter, &darker, x + width / 2 - 1, yy, 3);
2724 for (yy = y; yy < y + height; yy += 3)
2725 for (xx = x; xx < x + width; xx += 6)
2727 render_dot (cr, &lighter, &darker, xx, yy, 2);
2728 render_dot (cr, &lighter, &darker, xx + 3, yy + 1, 2);
2734 gdk_rgba_free (bg_color);
2738 gtk_theming_engine_render_activity (GtkThemingEngine *engine,
2745 if (gtk_theming_engine_has_class (engine, "spinner"))
2747 GtkStateFlags state;
2748 guint num_steps, step;
2756 gtk_theming_engine_get_style (engine,
2757 "num-steps", &num_steps,
2760 state = gtk_theming_engine_get_state (engine);
2761 gtk_theming_engine_get (engine, state,
2767 if (gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
2768 step = (guint) (progress * num_steps);
2774 cairo_translate (cr, x, y);
2776 /* draw clip region */
2777 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
2781 radius = MIN (width / 2, height / 2);
2782 half = num_steps / 2;
2784 for (i = 0; i < num_steps; i++)
2786 gint inset = 0.7 * radius;
2788 /* transparency is a function of time and intial value */
2789 gdouble t = (gdouble) ((i + num_steps - step)
2790 % num_steps) / num_steps;
2794 cairo_set_source_rgba (cr,
2800 cairo_set_line_width (cr, 2.0);
2802 dx + (radius - inset) * cos (i * G_PI / half),
2803 dy + (radius - inset) * sin (i * G_PI / half));
2805 dx + radius * cos (i * G_PI / half),
2806 dy + radius * sin (i * G_PI / half));
2814 gdk_rgba_free (color);
2818 gtk_theming_engine_render_background (engine, cr, x, y, width, height);
2819 gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
2824 scale_or_ref (GdkPixbuf *src,
2828 if (width == gdk_pixbuf_get_width (src) &&
2829 height == gdk_pixbuf_get_height (src))
2830 return g_object_ref (src);
2832 return gdk_pixbuf_scale_simple (src,
2834 GDK_INTERP_BILINEAR);
2838 lookup_icon_size (GtkThemingEngine *engine,
2844 GtkSettings *settings;
2846 screen = gtk_theming_engine_get_screen (engine);
2847 settings = gtk_settings_get_for_screen (screen);
2849 return gtk_icon_size_lookup_for_settings (settings, size, width, height);
2853 gtk_theming_engine_render_icon_pixbuf (GtkThemingEngine *engine,
2854 const GtkIconSource *source,
2859 GdkPixbuf *base_pixbuf;
2860 GtkStateFlags state;
2864 base_pixbuf = gtk_icon_source_get_pixbuf (source);
2865 state = gtk_theming_engine_get_state (engine);
2867 g_return_val_if_fail (base_pixbuf != NULL, NULL);
2869 if (size != (GtkIconSize) -1 &&
2870 !lookup_icon_size (engine, size, &width, &height))
2872 g_warning (G_STRLOC ": invalid icon size '%d'", size);
2876 /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2879 if (size != (GtkIconSize) -1 &&
2880 gtk_icon_source_get_size_wildcarded (source))
2881 scaled = scale_or_ref (base_pixbuf, width, height);
2883 scaled = g_object_ref (base_pixbuf);
2885 /* If the state was wildcarded, then generate a state. */
2886 if (gtk_icon_source_get_state_wildcarded (source))
2888 if (state & GTK_STATE_FLAG_INSENSITIVE)
2890 stated = gdk_pixbuf_copy (scaled);
2891 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2893 g_object_unref (scaled);
2895 else if (state & GTK_STATE_FLAG_PRELIGHT)
2897 stated = gdk_pixbuf_copy (scaled);
2898 gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2900 g_object_unref (scaled);