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, see <http://www.gnu.org/licenses/>.
23 #include <gobject/gvaluecollector.h>
25 #include "gtkstylecontextprivate.h"
26 #include "gtkstylepropertiesprivate.h"
27 #include "gtktypebuiltins.h"
28 #include "gtkthemingengineprivate.h"
30 #include "gtkwidget.h"
31 #include "gtkwindow.h"
32 #include "gtkprivate.h"
33 #include "gtksymboliccolorprivate.h"
34 #include "gtkanimationdescription.h"
35 #include "gtktimeline.h"
36 #include "gtkiconfactory.h"
37 #include "gtkwidgetpath.h"
38 #include "gtkwidgetprivate.h"
39 #include "gtkstylecascadeprivate.h"
40 #include "gtkstyleproviderprivate.h"
41 #include "gtksettings.h"
44 * SECTION:gtkstylecontext
45 * @Short_description: Rendering UI elements
46 * @Title: GtkStyleContext
48 * #GtkStyleContext is an object that stores styling information affecting
49 * a widget defined by #GtkWidgetPath.
51 * In order to construct the final style information, #GtkStyleContext
52 * queries information from all attached #GtkStyleProviders. Style providers
53 * can be either attached explicitly to the context through
54 * gtk_style_context_add_provider(), or to the screen through
55 * gtk_style_context_add_provider_for_screen(). The resulting style is a
56 * combination of all providers' information in priority order.
58 * For GTK+ widgets, any #GtkStyleContext returned by
59 * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
60 * #GdkScreen and RTL/LTR information set. The style context will be also
61 * updated automatically if any of these settings change on the widget.
63 * If you are using the theming layer standalone, you will need to set a
64 * widget path and a screen yourself to the created style context through
65 * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
66 * as updating the context yourself using gtk_style_context_invalidate()
67 * whenever any of the conditions change, such as a change in the
68 * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
71 * <refsect2 id="gtkstylecontext-animations">
72 * <title>Transition animations</title>
74 * #GtkStyleContext has built-in support for state change transitions.
75 * Note that these animations respect the #GtkSettings:gtk-enable-animations
79 * For simple widgets where state changes affect the whole widget area,
80 * calling gtk_style_context_notify_state_change() with a %NULL region
81 * is sufficient to trigger the transition animation. And GTK+ already
82 * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
86 * If a widget needs to declare several animatable regions (i.e. not
87 * affecting the whole widget area), its #GtkWidget::draw signal handler
88 * needs to wrap the render operations for the different regions with
89 * calls to gtk_style_context_push_animatable_region() and
90 * gtk_style_context_pop_animatable_region(). These functions take an
91 * identifier for the region which must be unique within the style context.
92 * For simple widgets with a fixed set of animatable regions, using an
93 * enumeration works well:
96 * <title>Using an enumeration to identify animatable regions</title>
107 * spin_button_draw (GtkWidget *widget,
110 * GtkStyleContext *context;
112 * context = gtk_widget_get_style_context (widget);
114 * gtk_style_context_push_animatable_region (context,
115 * GUINT_TO_POINTER (REGION_ENTRY));
117 * gtk_render_background (cr, 0, 0, 100, 30);
118 * gtk_render_frame (cr, 0, 0, 100, 30);
120 * gtk_style_context_pop_animatable_region (context);
127 * For complex widgets with an arbitrary number of animatable regions, it
128 * is up to the implementation to come up with a way to uniquely identify
129 * each animatable region. Using pointers to internal structs is one way
133 * <title>Using struct pointers to identify animatable regions</title>
136 * notebook_draw_tab (GtkWidget *widget,
137 * NotebookPage *page,
140 * gtk_style_context_push_animatable_region (context, page);
141 * gtk_render_extension (cr, page->x, page->y, page->width, page->height);
142 * gtk_style_context_pop_animatable_region (context);
147 * The widget also needs to notify the style context about a state change
148 * for a given animatable region so the animation is triggered.
151 * <title>Triggering a state change animation on a region</title>
154 * notebook_motion_notify (GtkWidget *widget,
155 * GdkEventMotion *event)
157 * GtkStyleContext *context;
158 * NotebookPage *page;
160 * context = gtk_widget_get_style_context (widget);
161 * page = find_page_under_pointer (widget, event);
162 * gtk_style_context_notify_state_change (context,
163 * gtk_widget_get_window (widget),
165 * GTK_STATE_PRELIGHT,
172 * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
173 * special value, in this case, the whole widget area will be updated
177 * <refsect2 id="gtkstylecontext-classes">
178 * <title>Style classes and regions</title>
180 * Widgets can add style classes to their context, which can be used
181 * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
182 * rendering. GTK+ has a number of predefined style classes:
183 * #GTK_STYLE_CLASS_CELL,
184 * #GTK_STYLE_CLASS_ENTRY,
185 * #GTK_STYLE_CLASS_BUTTON,
186 * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
187 * #GTK_STYLE_CLASS_CALENDAR,
188 * #GTK_STYLE_CLASS_SLIDER,
189 * #GTK_STYLE_CLASS_BACKGROUND,
190 * #GTK_STYLE_CLASS_RUBBERBAND,
191 * #GTK_STYLE_CLASS_TOOLTIP,
192 * #GTK_STYLE_CLASS_MENU,
193 * #GTK_STYLE_CLASS_MENUBAR,
194 * #GTK_STYLE_CLASS_MENUITEM,
195 * #GTK_STYLE_CLASS_TOOLBAR,
196 * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
197 * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
198 * #GTK_STYLE_CLASS_RADIO,
199 * #GTK_STYLE_CLASS_CHECK,
200 * #GTK_STYLE_CLASS_TROUGH,
201 * #GTK_STYLE_CLASS_SCROLLBAR,
202 * #GTK_STYLE_CLASS_SCALE,
203 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
204 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
205 * #GTK_STYLE_CLASS_HEADER,
206 * #GTK_STYLE_CLASS_ACCELERATOR,
207 * #GTK_STYLE_CLASS_GRIP,
208 * #GTK_STYLE_CLASS_DOCK,
209 * #GTK_STYLE_CLASS_PROGRESSBAR,
210 * #GTK_STYLE_CLASS_SPINNER,
211 * #GTK_STYLE_CLASS_EXPANDER,
212 * #GTK_STYLE_CLASS_SPINBUTTON,
213 * #GTK_STYLE_CLASS_NOTEBOOK,
214 * #GTK_STYLE_CLASS_VIEW,
215 * #GTK_STYLE_CLASS_SIDEBAR,
216 * #GTK_STYLE_CLASS_IMAGE,
217 * #GTK_STYLE_CLASS_HIGHLIGHT,
218 * #GTK_STYLE_CLASS_FRAME,
219 * #GTK_STYLE_CLASS_DND,
220 * #GTK_STYLE_CLASS_PANE_SEPARATOR,
221 * #GTK_STYLE_CLASS_SEPARATOR,
222 * #GTK_STYLE_CLASS_INFO,
223 * #GTK_STYLE_CLASS_WARNING,
224 * #GTK_STYLE_CLASS_QUESTION,
225 * #GTK_STYLE_CLASS_ERROR,
226 * #GTK_STYLE_CLASS_HORIZONTAL,
227 * #GTK_STYLE_CLASS_VERTICAL,
228 * #GTK_STYLE_CLASS_TOP,
229 * #GTK_STYLE_CLASS_BOTTOM,
230 * #GTK_STYLE_CLASS_LEFT,
231 * #GTK_STYLE_CLASS_RIGHT,
234 * Widgets can also add regions with flags to their context.
235 * The regions used by GTK+ widgets are:
240 * <entry>Region</entry>
241 * <entry>Flags</entry>
242 * <entry>Macro</entry>
243 * <entry>Used by</entry>
249 * <entry>even, odd</entry>
250 * <entry>GTK_STYLE_REGION_ROW</entry>
251 * <entry>#GtkTreeView</entry>
254 * <entry>column</entry>
255 * <entry>first, last, sorted</entry>
256 * <entry>GTK_STYLE_REGION_COLUMN</entry>
257 * <entry>#GtkTreeView</entry>
260 * <entry>column-header</entry>
262 * <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
267 * <entry>even, odd, first, last</entry>
268 * <entry>GTK_STYLE_REGION_TAB</entry>
269 * <entry>#GtkNotebook</entry>
276 * <refsect2 id="gtkstylecontext-custom-styling">
277 * <title>Custom styling in UI libraries and applications</title>
279 * If you are developing a library with custom #GtkWidget<!-- -->s that
280 * render differently than standard components, you may need to add a
281 * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
282 * priority, either a #GtkCssProvider or a custom object implementing the
283 * #GtkStyleProvider interface. This way theming engines may still attempt
284 * to style your UI elements in a different way if needed so.
287 * If you are using custom styling on an applications, you probably want then
288 * to make your style information prevail to the theme's, so you must use
289 * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
290 * priority, keep in mind that the user settings in
291 * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
292 * still take precedence over your changes, as it uses the
293 * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
296 * If a custom theming engine is needed, you probably want to implement a
297 * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
298 * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
299 * which loads the theming engine module from the standard paths.
304 typedef struct GtkStyleInfo GtkStyleInfo;
305 typedef struct GtkRegion GtkRegion;
306 typedef struct PropertyValue PropertyValue;
307 typedef struct AnimationInfo AnimationInfo;
308 typedef struct StyleData StyleData;
313 GtkRegionFlags flags;
326 GArray *style_classes;
328 GtkJunctionSides junction_sides;
329 GtkStateFlags state_flags;
334 GtkCssComputedValues *store;
335 GArray *property_cache;
340 GtkTimeline *timeline;
344 /* Region stack (until region_id) at the time of
345 * rendering, this is used for nested cancellation.
347 GSList *parent_regions;
351 gboolean target_value;
353 cairo_region_t *invalidation_region;
357 struct _GtkStyleContextPrivate
361 GtkStyleCascade *cascade;
363 GtkStyleContext *parent;
365 GtkWidgetPath *widget_path;
366 GHashTable *style_data;
368 StyleData *current_data;
369 GtkStateFlags current_state;
371 GSList *animation_regions;
374 GtkThemingEngine *theming_engine;
376 GtkTextDirection direction;
378 guint animations_invalidated : 1;
379 guint invalidating_context : 1;
394 static guint signals[LAST_SIGNAL] = { 0 };
396 static void gtk_style_context_finalize (GObject *object);
398 static void gtk_style_context_impl_set_property (GObject *object,
402 static void gtk_style_context_impl_get_property (GObject *object,
406 static GtkSymbolicColor *
407 gtk_style_context_color_lookup_func (gpointer contextp,
411 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
414 gtk_style_context_class_init (GtkStyleContextClass *klass)
416 GObjectClass *object_class = G_OBJECT_CLASS (klass);
418 object_class->finalize = gtk_style_context_finalize;
419 object_class->set_property = gtk_style_context_impl_set_property;
420 object_class->get_property = gtk_style_context_impl_get_property;
423 g_signal_new (I_("changed"),
424 G_TYPE_FROM_CLASS (object_class),
426 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
428 g_cclosure_marshal_VOID__VOID,
431 g_object_class_install_property (object_class,
433 g_param_spec_object ("screen",
435 P_("The associated GdkScreen"),
437 GTK_PARAM_READWRITE));
438 g_object_class_install_property (object_class,
440 g_param_spec_enum ("direction",
442 P_("Text direction"),
443 GTK_TYPE_TEXT_DIRECTION,
445 GTK_PARAM_READWRITE));
447 * GtkStyleContext:parent:
449 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
454 g_object_class_install_property (object_class,
456 g_param_spec_object ("parent",
458 P_("The parent style context"),
459 GTK_TYPE_STYLE_CONTEXT,
460 GTK_PARAM_READWRITE));
462 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
465 static GtkStyleInfo *
466 style_info_new (void)
470 info = g_slice_new0 (GtkStyleInfo);
471 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
472 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
478 style_info_free (GtkStyleInfo *info)
480 g_array_free (info->style_classes, TRUE);
481 g_array_free (info->regions, TRUE);
482 g_slice_free (GtkStyleInfo, info);
485 static GtkStyleInfo *
486 style_info_copy (const GtkStyleInfo *info)
490 copy = style_info_new ();
491 g_array_insert_vals (copy->style_classes, 0,
492 info->style_classes->data,
493 info->style_classes->len);
495 g_array_insert_vals (copy->regions, 0,
499 copy->junction_sides = info->junction_sides;
500 copy->state_flags = info->state_flags;
506 style_info_hash (gconstpointer elem)
508 const GtkStyleInfo *info;
513 for (i = 0; i < info->style_classes->len; i++)
515 hash += g_array_index (info->style_classes, GQuark, i);
519 for (i = 0; i < info->regions->len; i++)
523 region = &g_array_index (info->regions, GtkRegion, i);
524 hash += region->class_quark;
525 hash += region->flags;
529 return hash ^ info->state_flags;
533 style_info_equal (gconstpointer elem1,
536 const GtkStyleInfo *info1, *info2;
541 if (info1->state_flags != info2->state_flags)
544 if (info1->junction_sides != info2->junction_sides)
547 if (info1->style_classes->len != info2->style_classes->len)
550 if (memcmp (info1->style_classes->data,
551 info2->style_classes->data,
552 info1->style_classes->len * sizeof (GQuark)) != 0)
555 if (info1->regions->len != info2->regions->len)
558 if (memcmp (info1->regions->data,
559 info2->regions->data,
560 info1->regions->len * sizeof (GtkRegion)) != 0)
567 style_data_new (void)
571 data = g_slice_new0 (StyleData);
577 clear_property_cache (StyleData *data)
581 if (!data->property_cache)
584 for (i = 0; i < data->property_cache->len; i++)
586 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
588 g_param_spec_unref (node->pspec);
589 g_value_unset (&node->value);
592 g_array_free (data->property_cache, TRUE);
593 data->property_cache = NULL;
597 style_data_free (StyleData *data)
599 g_object_unref (data->store);
600 clear_property_cache (data);
602 g_slice_free (StyleData, data);
606 gtk_style_context_init (GtkStyleContext *style_context)
608 GtkStyleContextPrivate *priv;
611 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
612 GTK_TYPE_STYLE_CONTEXT,
613 GtkStyleContextPrivate);
615 priv->style_data = g_hash_table_new_full (style_info_hash,
617 (GDestroyNotify) style_info_free,
618 (GDestroyNotify) style_data_free);
619 priv->theming_engine = g_object_ref ((gpointer) gtk_theming_engine_load (NULL));
621 priv->direction = GTK_TEXT_DIR_LTR;
623 priv->screen = gdk_screen_get_default ();
624 priv->cascade = _gtk_style_cascade_get_for_screen (priv->screen);
625 g_object_ref (priv->cascade);
627 /* Create default info store */
628 info = style_info_new ();
629 priv->info_stack = g_slist_prepend (priv->info_stack, info);
633 animation_info_free (AnimationInfo *info)
635 g_object_unref (info->timeline);
636 g_object_unref (info->window);
638 if (info->invalidation_region)
639 cairo_region_destroy (info->invalidation_region);
641 g_array_free (info->rectangles, TRUE);
642 g_slist_free (info->parent_regions);
643 g_slice_free (AnimationInfo, info);
646 static AnimationInfo *
647 animation_info_lookup_by_timeline (GtkStyleContext *context,
648 GtkTimeline *timeline)
650 GtkStyleContextPrivate *priv;
654 priv = context->priv;
656 for (l = priv->animations; l; l = l->next)
660 if (info->timeline == timeline)
668 timeline_frame_cb (GtkTimeline *timeline,
672 GtkStyleContextPrivate *priv;
673 GtkStyleContext *context;
677 priv = context->priv;
678 info = animation_info_lookup_by_timeline (context, timeline);
680 g_assert (info != NULL);
682 /* Cancel transition if window is gone */
683 if (gdk_window_is_destroyed (info->window) ||
684 !gdk_window_is_visible (info->window))
686 priv->animations = g_slist_remove (priv->animations, info);
687 animation_info_free (info);
691 if (info->invalidation_region &&
692 !cairo_region_is_empty (info->invalidation_region))
693 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
695 gdk_window_invalidate_rect (info->window, NULL, TRUE);
699 timeline_finished_cb (GtkTimeline *timeline,
702 GtkStyleContextPrivate *priv;
703 GtkStyleContext *context;
707 priv = context->priv;
708 info = animation_info_lookup_by_timeline (context, timeline);
710 g_assert (info != NULL);
712 priv->animations = g_slist_remove (priv->animations, info);
714 /* Invalidate one last time the area, so the final content is painted */
715 if (info->invalidation_region &&
716 !cairo_region_is_empty (info->invalidation_region))
717 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
719 gdk_window_invalidate_rect (info->window, NULL, TRUE);
721 animation_info_free (info);
724 static AnimationInfo *
725 animation_info_new (GtkStyleContext *context,
728 GtkTimelineProgressType progress_type,
731 gboolean target_value,
736 info = g_slice_new0 (AnimationInfo);
738 info->rectangles = g_array_new (FALSE, FALSE, sizeof (cairo_rectangle_int_t));
739 info->timeline = _gtk_timeline_new (duration);
740 info->window = g_object_ref (window);
742 info->target_value = target_value;
743 info->region_id = region_id;
745 _gtk_timeline_set_progress_type (info->timeline, progress_type);
746 _gtk_timeline_set_loop (info->timeline, loop);
748 if (!loop && !target_value)
750 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
751 _gtk_timeline_rewind (info->timeline);
754 g_signal_connect (info->timeline, "frame",
755 G_CALLBACK (timeline_frame_cb), context);
756 g_signal_connect (info->timeline, "finished",
757 G_CALLBACK (timeline_finished_cb), context);
759 _gtk_timeline_start (info->timeline);
764 static AnimationInfo *
765 animation_info_lookup (GtkStyleContext *context,
769 GtkStyleContextPrivate *priv;
772 priv = context->priv;
774 for (l = priv->animations; l; l = l->next)
780 if (info->state == state &&
781 info->region_id == region_id)
789 gtk_style_context_finalize (GObject *object)
791 GtkStyleContextPrivate *priv;
792 GtkStyleContext *style_context;
795 style_context = GTK_STYLE_CONTEXT (object);
796 priv = style_context->priv;
798 gtk_style_context_set_parent (style_context, NULL);
800 if (priv->widget_path)
801 gtk_widget_path_free (priv->widget_path);
803 g_hash_table_destroy (priv->style_data);
805 g_object_unref (priv->cascade);
807 g_slist_free_full (priv->info_stack, (GDestroyNotify) style_info_free);
809 g_slist_free (priv->animation_regions);
811 for (l = priv->animations; l; l = l->next)
812 animation_info_free ((AnimationInfo *) l->data);
814 g_slist_free (priv->animations);
816 if (priv->theming_engine)
817 g_object_unref (priv->theming_engine);
819 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
823 gtk_style_context_impl_set_property (GObject *object,
828 GtkStyleContext *style_context;
830 style_context = GTK_STYLE_CONTEXT (object);
835 gtk_style_context_set_screen (style_context,
836 g_value_get_object (value));
839 gtk_style_context_set_direction (style_context,
840 g_value_get_enum (value));
843 gtk_style_context_set_parent (style_context,
844 g_value_get_object (value));
847 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
853 gtk_style_context_impl_get_property (GObject *object,
858 GtkStyleContext *style_context;
859 GtkStyleContextPrivate *priv;
861 style_context = GTK_STYLE_CONTEXT (object);
862 priv = style_context->priv;
867 g_value_set_object (value, priv->screen);
870 g_value_set_enum (value, priv->direction);
873 g_value_set_object (value, priv->parent);
876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
882 build_properties (GtkStyleContext *context,
883 StyleData *style_data,
887 GtkStyleContextPrivate *priv;
888 GtkCssMatcher matcher;
889 GtkCssLookup *lookup;
891 priv = context->priv;
893 _gtk_css_matcher_init (&matcher, path, state);
894 lookup = _gtk_css_lookup_new ();
896 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
900 style_data->store = _gtk_css_computed_values_new ();
901 _gtk_css_lookup_resolve (lookup, context, style_data->store);
902 _gtk_css_lookup_free (lookup);
905 static GtkWidgetPath *
906 create_query_path (GtkStyleContext *context)
908 GtkStyleContextPrivate *priv;
913 priv = context->priv;
914 path = gtk_widget_path_copy (priv->widget_path);
915 pos = gtk_widget_path_length (path) - 1;
917 info = priv->info_stack->data;
919 /* Set widget regions */
920 for (i = 0; i < info->regions->len; i++)
924 region = &g_array_index (info->regions, GtkRegion, i);
925 gtk_widget_path_iter_add_region (path, pos,
926 g_quark_to_string (region->class_quark),
930 /* Set widget classes */
931 for (i = 0; i < info->style_classes->len; i++)
935 quark = g_array_index (info->style_classes, GQuark, i);
936 gtk_widget_path_iter_add_class (path, pos,
937 g_quark_to_string (quark));
944 style_data_lookup (GtkStyleContext *context,
947 GtkStyleContextPrivate *priv;
949 gboolean state_mismatch;
952 priv = context->priv;
953 state_mismatch = ((GtkStyleInfo *) priv->info_stack->data)->state_flags != state;
955 /* Current data in use is cached, just return it */
956 if (priv->current_data && priv->current_state == state)
957 return priv->current_data;
959 g_assert (priv->widget_path != NULL);
961 if (G_UNLIKELY (state_mismatch))
963 gtk_style_context_save (context);
964 gtk_style_context_set_state (context, state);
967 priv->current_data = g_hash_table_lookup (priv->style_data, priv->info_stack->data);
968 priv->current_state = state;
970 if (!priv->current_data)
974 priv->current_data = style_data_new ();
975 g_hash_table_insert (priv->style_data,
976 style_info_copy (priv->info_stack->data),
979 path = create_query_path (context);
981 build_properties (context, priv->current_data, path, state);
983 gtk_widget_path_free (path);
986 data = priv->current_data;
988 if (priv->theming_engine)
989 g_object_unref (priv->theming_engine);
991 v = _gtk_css_computed_values_get_value_by_name (priv->current_data->store, "engine");
993 priv->theming_engine = _gtk_css_value_dup_object (v);
995 priv->theming_engine = g_object_ref (gtk_theming_engine_load (NULL));
997 if (G_UNLIKELY (state_mismatch))
998 gtk_style_context_restore (context);
1004 * gtk_style_context_new:
1006 * Creates a standalone #GtkStyleContext, this style context
1007 * won't be attached to any widget, so you may want
1008 * to call gtk_style_context_set_path() yourself.
1011 * This function is only useful when using the theming layer
1012 * separated from GTK+, if you are using #GtkStyleContext to
1013 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1014 * in order to get a style context ready to theme the widget.
1017 * Returns: A newly created #GtkStyleContext.
1020 gtk_style_context_new (void)
1022 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1026 _gtk_style_context_set_widget (GtkStyleContext *context,
1029 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1030 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1032 context->priv->widget = widget;
1036 * gtk_style_context_add_provider:
1037 * @context: a #GtkStyleContext
1038 * @provider: a #GtkStyleProvider
1039 * @priority: the priority of the style provider. The lower
1040 * it is, the earlier it will be used in the style
1041 * construction. Typically this will be in the range
1042 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1043 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1045 * Adds a style provider to @context, to be used in style construction.
1047 * <note><para>If both priorities are the same, A #GtkStyleProvider
1048 * added through this function takes precedence over another added
1049 * through gtk_style_context_add_provider_for_screen().</para></note>
1054 gtk_style_context_add_provider (GtkStyleContext *context,
1055 GtkStyleProvider *provider,
1058 GtkStyleContextPrivate *priv;
1060 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1061 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1063 priv = context->priv;
1065 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1067 GtkStyleCascade *new_cascade;
1069 new_cascade = _gtk_style_cascade_new ();
1070 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1071 g_object_unref (priv->cascade);
1072 priv->cascade = new_cascade;
1075 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1077 gtk_style_context_invalidate (context);
1081 * gtk_style_context_remove_provider:
1082 * @context: a #GtkStyleContext
1083 * @provider: a #GtkStyleProvider
1085 * Removes @provider from the style providers list in @context.
1090 gtk_style_context_remove_provider (GtkStyleContext *context,
1091 GtkStyleProvider *provider)
1093 GtkStyleContextPrivate *priv;
1095 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1096 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1098 priv = context->priv;
1100 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1103 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1107 * gtk_style_context_reset_widgets:
1108 * @screen: a #GdkScreen
1110 * This function recomputes the styles for all widgets under a particular
1111 * #GdkScreen. This is useful when some global parameter has changed that
1112 * affects the appearance of all widgets, because when a widget gets a new
1113 * style, it will both redraw and recompute any cached information about
1114 * its appearance. As an example, it is used when the color scheme changes
1115 * in the related #GtkSettings object.
1120 gtk_style_context_reset_widgets (GdkScreen *screen)
1122 GList *list, *toplevels;
1124 _gtk_icon_set_invalidate_caches ();
1126 toplevels = gtk_window_list_toplevels ();
1127 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1129 for (list = toplevels; list; list = list->next)
1131 if (gtk_widget_get_screen (list->data) == screen)
1132 gtk_widget_reset_style (list->data);
1134 g_object_unref (list->data);
1137 g_list_free (toplevels);
1141 * gtk_style_context_add_provider_for_screen:
1142 * @screen: a #GdkScreen
1143 * @provider: a #GtkStyleProvider
1144 * @priority: the priority of the style provider. The lower
1145 * it is, the earlier it will be used in the style
1146 * construction. Typically this will be in the range
1147 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1148 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1150 * Adds a global style provider to @screen, which will be used
1151 * in style construction for all #GtkStyleContext<!-- -->s under
1154 * GTK+ uses this to make styling information from #GtkSettings
1157 * <note><para>If both priorities are the same, A #GtkStyleProvider
1158 * added through gtk_style_context_add_provider() takes precedence
1159 * over another added through this function.</para></note>
1164 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1165 GtkStyleProvider *provider,
1168 GtkStyleCascade *cascade;
1170 g_return_if_fail (GDK_IS_SCREEN (screen));
1171 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1173 cascade = _gtk_style_cascade_get_for_screen (screen);
1174 _gtk_style_cascade_add_provider (cascade, provider, priority);
1178 * gtk_style_context_remove_provider_for_screen:
1179 * @screen: a #GdkScreen
1180 * @provider: a #GtkStyleProvider
1182 * Removes @provider from the global style providers list in @screen.
1187 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1188 GtkStyleProvider *provider)
1190 GtkStyleCascade *cascade;
1192 g_return_if_fail (GDK_IS_SCREEN (screen));
1193 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1195 cascade = _gtk_style_cascade_get_for_screen (screen);
1196 _gtk_style_cascade_remove_provider (cascade, provider);
1200 * gtk_style_context_get_section:
1201 * @context: a #GtkStyleContext
1202 * @property: style property name
1204 * Queries the location in the CSS where @property was defined for the
1205 * current @context. Note that the state to be queried is taken from
1206 * gtk_style_context_get_state().
1208 * If the location is not available, %NULL will be returned. The
1209 * location might not be available for various reasons, such as the
1210 * property being overridden, @property not naming a supported CSS
1211 * property or tracking of definitions being disabled for performance
1214 * Shorthand CSS properties cannot be queried for a location and will
1215 * always return %NULL.
1217 * Returns: %NULL or the section where value was defined
1220 gtk_style_context_get_section (GtkStyleContext *context,
1221 const gchar *property)
1223 GtkStyleContextPrivate *priv;
1224 GtkStyleProperty *prop;
1227 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1228 g_return_val_if_fail (property != NULL, NULL);
1230 priv = context->priv;
1231 g_return_val_if_fail (priv->widget_path != NULL, NULL);
1233 prop = _gtk_style_property_lookup (property);
1234 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1237 data = style_data_lookup (context, gtk_style_context_get_state (context));
1238 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1241 static GtkCssValue *
1242 gtk_style_context_query_func (guint id,
1245 return _gtk_css_computed_values_get_value (values, id);
1249 * gtk_style_context_get_property:
1250 * @context: a #GtkStyleContext
1251 * @property: style property name
1252 * @state: state to retrieve the property value for
1253 * @value: (out) (transfer full): return location for the style property value
1255 * Gets a style property from @context for the given state.
1257 * When @value is no longer needed, g_value_unset() must be called
1258 * to free any allocated memory.
1263 gtk_style_context_get_property (GtkStyleContext *context,
1264 const gchar *property,
1265 GtkStateFlags state,
1268 GtkStyleContextPrivate *priv;
1269 GtkStyleProperty *prop;
1273 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1274 g_return_if_fail (property != NULL);
1275 g_return_if_fail (value != NULL);
1277 priv = context->priv;
1278 g_return_if_fail (priv->widget_path != NULL);
1280 prop = _gtk_style_property_lookup (property);
1283 g_warning ("Style property \"%s\" is not registered", property);
1286 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1288 g_warning ("Style property \"%s\" is not gettable", property);
1292 data = style_data_lookup (context, state);
1293 v = _gtk_style_property_query (prop, gtk_style_context_query_func, data->store);
1294 _gtk_css_value_init_gvalue (v, value);
1295 _gtk_css_value_unref (v);
1299 * gtk_style_context_get_valist:
1300 * @context: a #GtkStyleContext
1301 * @state: state to retrieve the property values for
1302 * @args: va_list of property name/return location pairs, followed by %NULL
1304 * Retrieves several style property values from @context for a given state.
1309 gtk_style_context_get_valist (GtkStyleContext *context,
1310 GtkStateFlags state,
1313 const gchar *property_name;
1315 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1317 property_name = va_arg (args, const gchar *);
1319 while (property_name)
1321 gchar *error = NULL;
1322 GValue value = G_VALUE_INIT;
1324 gtk_style_context_get_property (context,
1329 G_VALUE_LCOPY (&value, args, 0, &error);
1330 g_value_unset (&value);
1334 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1339 property_name = va_arg (args, const gchar *);
1344 * gtk_style_context_get:
1345 * @context: a #GtkStyleContext
1346 * @state: state to retrieve the property values for
1347 * @...: property name /return value pairs, followed by %NULL
1349 * Retrieves several style property values from @context for a
1355 gtk_style_context_get (GtkStyleContext *context,
1356 GtkStateFlags state,
1361 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1363 va_start (args, state);
1364 gtk_style_context_get_valist (context, state, args);
1369 * gtk_style_context_set_state:
1370 * @context: a #GtkStyleContext
1371 * @flags: state to represent
1373 * Sets the state to be used when rendering with any
1374 * of the gtk_render_*() functions.
1379 gtk_style_context_set_state (GtkStyleContext *context,
1380 GtkStateFlags flags)
1382 GtkStyleContextPrivate *priv;
1385 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1387 priv = context->priv;
1388 info = priv->info_stack->data;
1389 info->state_flags = flags;
1393 * gtk_style_context_get_state:
1394 * @context: a #GtkStyleContext
1396 * Returns the state used when rendering.
1398 * Returns: the state flags
1403 gtk_style_context_get_state (GtkStyleContext *context)
1405 GtkStyleContextPrivate *priv;
1408 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1410 priv = context->priv;
1411 info = priv->info_stack->data;
1413 return info->state_flags;
1417 context_has_animatable_region (GtkStyleContext *context,
1420 GtkStyleContextPrivate *priv;
1422 /* NULL region_id means everything
1423 * rendered through the style context
1428 priv = context->priv;
1429 return g_slist_find (priv->animation_regions, region_id) != NULL;
1433 * gtk_style_context_state_is_running:
1434 * @context: a #GtkStyleContext
1435 * @state: a widget state
1436 * @progress: (out): return location for the transition progress
1438 * Returns %TRUE if there is a transition animation running for the
1439 * current region (see gtk_style_context_push_animatable_region()).
1441 * If @progress is not %NULL, the animation progress will be returned
1442 * there, 0.0 means the state is closest to being unset, while 1.0 means
1443 * it's closest to being set. This means transition animation will
1444 * run from 0 to 1 when @state is being set and from 1 to 0 when
1447 * Returns: %TRUE if there is a running transition animation for @state.
1452 gtk_style_context_state_is_running (GtkStyleContext *context,
1456 GtkStyleContextPrivate *priv;
1457 AnimationInfo *info;
1460 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1462 priv = context->priv;
1464 for (l = priv->animations; l; l = l->next)
1468 if (info->state == state &&
1469 context_has_animatable_region (context, info->region_id))
1472 *progress = _gtk_timeline_get_progress (info->timeline);
1482 * gtk_style_context_set_path:
1483 * @context: a #GtkStyleContext
1484 * @path: a #GtkWidgetPath
1486 * Sets the #GtkWidgetPath used for style matching. As a
1487 * consequence, the style will be regenerated to match
1488 * the new given path.
1490 * If you are using a #GtkStyleContext returned from
1491 * gtk_widget_get_style_context(), you do not need to call
1497 gtk_style_context_set_path (GtkStyleContext *context,
1498 GtkWidgetPath *path)
1500 GtkStyleContextPrivate *priv;
1502 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1503 g_return_if_fail (path != NULL);
1505 priv = context->priv;
1506 g_return_if_fail (priv->widget == NULL);
1508 if (priv->widget_path)
1510 gtk_widget_path_free (priv->widget_path);
1511 priv->widget_path = NULL;
1515 priv->widget_path = gtk_widget_path_copy (path);
1517 gtk_style_context_invalidate (context);
1521 * gtk_style_context_get_path:
1522 * @context: a #GtkStyleContext
1524 * Returns the widget path used for style matching.
1526 * Returns: (transfer none): A #GtkWidgetPath
1530 const GtkWidgetPath *
1531 gtk_style_context_get_path (GtkStyleContext *context)
1533 GtkStyleContextPrivate *priv;
1535 priv = context->priv;
1536 return priv->widget_path;
1540 * gtk_style_context_set_parent:
1541 * @context: a #GtkStyleContext
1542 * @parent: (allow-none): the new parent or %NULL
1544 * Sets the parent style context for @context. The parent style
1545 * context is used to implement
1546 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1549 * If you are using a #GtkStyleContext returned from
1550 * gtk_widget_get_style_context(), the parent will be set for you.
1555 gtk_style_context_set_parent (GtkStyleContext *context,
1556 GtkStyleContext *parent)
1558 GtkStyleContextPrivate *priv;
1560 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1561 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1563 priv = context->priv;
1565 if (priv->parent == parent)
1569 g_object_ref (parent);
1572 g_object_unref (priv->parent);
1574 priv->parent = parent;
1576 g_object_notify (G_OBJECT (context), "parent");
1577 gtk_style_context_invalidate (context);
1581 * gtk_style_context_get_parent:
1582 * @context: a #GtkStyleContext
1584 * Gets the parent context set via gtk_style_context_set_parent().
1585 * See that function for details.
1587 * Returns: (transfer none): the parent context or %NULL
1592 gtk_style_context_get_parent (GtkStyleContext *context)
1594 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1596 return context->priv->parent;
1600 * gtk_style_context_save:
1601 * @context: a #GtkStyleContext
1603 * Saves the @context state, so all modifications done through
1604 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1605 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1606 * or gtk_style_context_set_junction_sides() can be reverted in one
1607 * go through gtk_style_context_restore().
1612 gtk_style_context_save (GtkStyleContext *context)
1614 GtkStyleContextPrivate *priv;
1617 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1619 priv = context->priv;
1621 g_assert (priv->info_stack != NULL);
1623 info = style_info_copy (priv->info_stack->data);
1624 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1628 * gtk_style_context_restore:
1629 * @context: a #GtkStyleContext
1631 * Restores @context state to a previous stage.
1632 * See gtk_style_context_save().
1637 gtk_style_context_restore (GtkStyleContext *context)
1639 GtkStyleContextPrivate *priv;
1642 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1644 priv = context->priv;
1646 if (priv->info_stack)
1648 info = priv->info_stack->data;
1649 priv->info_stack = g_slist_remove (priv->info_stack, info);
1650 style_info_free (info);
1653 if (!priv->info_stack)
1655 g_warning ("Unpaired gtk_style_context_restore() call");
1657 /* Create default region */
1658 info = style_info_new ();
1659 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1662 priv->current_data = NULL;
1666 style_class_find (GArray *array,
1671 gboolean found = FALSE;
1677 if (!array || array->len == 0)
1681 max = array->len - 1;
1687 mid = (min + max) / 2;
1688 item = g_array_index (array, GQuark, mid);
1690 if (class_quark == item)
1695 else if (class_quark > item)
1696 min = pos = mid + 1;
1703 while (!found && min <= max);
1712 region_find (GArray *array,
1717 gboolean found = FALSE;
1723 if (!array || array->len == 0)
1727 max = array->len - 1;
1733 mid = (min + max) / 2;
1734 region = &g_array_index (array, GtkRegion, mid);
1736 if (region->class_quark == class_quark)
1741 else if (region->class_quark > class_quark)
1742 min = pos = mid + 1;
1749 while (!found && min <= max);
1758 * gtk_style_context_add_class:
1759 * @context: a #GtkStyleContext
1760 * @class_name: class name to use in styling
1762 * Adds a style class to @context, so posterior calls to
1763 * gtk_style_context_get() or any of the gtk_render_*()
1764 * functions will make use of this new class for styling.
1766 * In the CSS file format, a #GtkEntry defining an "entry"
1767 * class, would be matched by:
1770 * GtkEntry.entry { ... }
1773 * While any widget defining an "entry" class would be
1782 gtk_style_context_add_class (GtkStyleContext *context,
1783 const gchar *class_name)
1785 GtkStyleContextPrivate *priv;
1790 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1791 g_return_if_fail (class_name != NULL);
1793 priv = context->priv;
1794 class_quark = g_quark_from_string (class_name);
1796 g_assert (priv->info_stack != NULL);
1797 info = priv->info_stack->data;
1799 if (!style_class_find (info->style_classes, class_quark, &position))
1801 g_array_insert_val (info->style_classes, position, class_quark);
1803 /* Unset current data, as it likely changed due to the class change */
1804 priv->current_data = NULL;
1809 * gtk_style_context_remove_class:
1810 * @context: a #GtkStyleContext
1811 * @class_name: class name to remove
1813 * Removes @class_name from @context.
1818 gtk_style_context_remove_class (GtkStyleContext *context,
1819 const gchar *class_name)
1821 GtkStyleContextPrivate *priv;
1826 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1827 g_return_if_fail (class_name != NULL);
1829 class_quark = g_quark_try_string (class_name);
1834 priv = context->priv;
1836 g_assert (priv->info_stack != NULL);
1837 info = priv->info_stack->data;
1839 if (style_class_find (info->style_classes, class_quark, &position))
1841 g_array_remove_index (info->style_classes, position);
1843 /* Unset current data, as it likely changed due to the class change */
1844 priv->current_data = NULL;
1849 * gtk_style_context_has_class:
1850 * @context: a #GtkStyleContext
1851 * @class_name: a class name
1853 * Returns %TRUE if @context currently has defined the
1856 * Returns: %TRUE if @context has @class_name defined
1861 gtk_style_context_has_class (GtkStyleContext *context,
1862 const gchar *class_name)
1864 GtkStyleContextPrivate *priv;
1868 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1869 g_return_val_if_fail (class_name != NULL, FALSE);
1871 class_quark = g_quark_try_string (class_name);
1876 priv = context->priv;
1878 g_assert (priv->info_stack != NULL);
1879 info = priv->info_stack->data;
1881 if (style_class_find (info->style_classes, class_quark, NULL))
1888 * gtk_style_context_list_classes:
1889 * @context: a #GtkStyleContext
1891 * Returns the list of classes currently defined in @context.
1893 * Returns: (transfer container) (element-type utf8): a #GList of
1894 * strings with the currently defined classes. The contents
1895 * of the list are owned by GTK+, but you must free the list
1896 * itself with g_list_free() when you are done with it.
1901 gtk_style_context_list_classes (GtkStyleContext *context)
1903 GtkStyleContextPrivate *priv;
1905 GList *classes = NULL;
1908 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1910 priv = context->priv;
1912 g_assert (priv->info_stack != NULL);
1913 info = priv->info_stack->data;
1915 for (i = 0; i < info->style_classes->len; i++)
1919 quark = g_array_index (info->style_classes, GQuark, i);
1920 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1927 * gtk_style_context_list_regions:
1928 * @context: a #GtkStyleContext
1930 * Returns the list of regions currently defined in @context.
1932 * Returns: (transfer container) (element-type utf8): a #GList of
1933 * strings with the currently defined regions. The contents
1934 * of the list are owned by GTK+, but you must free the list
1935 * itself with g_list_free() when you are done with it.
1940 gtk_style_context_list_regions (GtkStyleContext *context)
1942 GtkStyleContextPrivate *priv;
1944 GList *classes = NULL;
1947 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1949 priv = context->priv;
1951 g_assert (priv->info_stack != NULL);
1952 info = priv->info_stack->data;
1954 for (i = 0; i < info->regions->len; i++)
1957 const gchar *class_name;
1959 region = &g_array_index (info->regions, GtkRegion, i);
1961 class_name = g_quark_to_string (region->class_quark);
1962 classes = g_list_prepend (classes, (gchar *) class_name);
1969 _gtk_style_context_check_region_name (const gchar *str)
1971 g_return_val_if_fail (str != NULL, FALSE);
1973 if (!g_ascii_islower (str[0]))
1979 !g_ascii_islower (*str))
1989 * gtk_style_context_add_region:
1990 * @context: a #GtkStyleContext
1991 * @region_name: region name to use in styling
1992 * @flags: flags that apply to the region
1994 * Adds a region to @context, so posterior calls to
1995 * gtk_style_context_get() or any of the gtk_render_*()
1996 * functions will make use of this new region for styling.
1998 * In the CSS file format, a #GtkTreeView defining a "row"
1999 * region, would be matched by:
2002 * GtkTreeView row { ... }
2005 * Pseudo-classes are used for matching @flags, so the two
2008 * GtkTreeView row:nth-child(even) { ... }
2009 * GtkTreeView row:nth-child(odd) { ... }
2012 * would apply to even and odd rows, respectively.
2014 * <note><para>Region names must only contain lowercase letters
2015 * and '-', starting always with a lowercase letter.</para></note>
2020 gtk_style_context_add_region (GtkStyleContext *context,
2021 const gchar *region_name,
2022 GtkRegionFlags flags)
2024 GtkStyleContextPrivate *priv;
2026 GQuark region_quark;
2029 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2030 g_return_if_fail (region_name != NULL);
2031 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2033 priv = context->priv;
2034 region_quark = g_quark_from_string (region_name);
2036 g_assert (priv->info_stack != NULL);
2037 info = priv->info_stack->data;
2039 if (!region_find (info->regions, region_quark, &position))
2043 region.class_quark = region_quark;
2044 region.flags = flags;
2046 g_array_insert_val (info->regions, position, region);
2048 /* Unset current data, as it likely changed due to the region change */
2049 priv->current_data = NULL;
2054 * gtk_style_context_remove_region:
2055 * @context: a #GtkStyleContext
2056 * @region_name: region name to unset
2058 * Removes a region from @context.
2063 gtk_style_context_remove_region (GtkStyleContext *context,
2064 const gchar *region_name)
2066 GtkStyleContextPrivate *priv;
2068 GQuark region_quark;
2071 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2072 g_return_if_fail (region_name != NULL);
2074 region_quark = g_quark_try_string (region_name);
2079 priv = context->priv;
2081 g_assert (priv->info_stack != NULL);
2082 info = priv->info_stack->data;
2084 if (region_find (info->regions, region_quark, &position))
2086 g_array_remove_index (info->regions, position);
2088 /* Unset current data, as it likely changed due to the region change */
2089 priv->current_data = NULL;
2094 * gtk_style_context_has_region:
2095 * @context: a #GtkStyleContext
2096 * @region_name: a region name
2097 * @flags_return: (out) (allow-none): return location for region flags
2099 * Returns %TRUE if @context has the region defined.
2100 * If @flags_return is not %NULL, it is set to the flags
2101 * affecting the region.
2103 * Returns: %TRUE if region is defined
2108 gtk_style_context_has_region (GtkStyleContext *context,
2109 const gchar *region_name,
2110 GtkRegionFlags *flags_return)
2112 GtkStyleContextPrivate *priv;
2114 GQuark region_quark;
2117 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2118 g_return_val_if_fail (region_name != NULL, FALSE);
2123 region_quark = g_quark_try_string (region_name);
2128 priv = context->priv;
2130 g_assert (priv->info_stack != NULL);
2131 info = priv->info_stack->data;
2133 if (region_find (info->regions, region_quark, &position))
2139 region = &g_array_index (info->regions, GtkRegion, position);
2140 *flags_return = region->flags;
2149 style_property_values_cmp (gconstpointer bsearch_node1,
2150 gconstpointer bsearch_node2)
2152 const PropertyValue *val1 = bsearch_node1;
2153 const PropertyValue *val2 = bsearch_node2;
2155 if (val1->widget_type != val2->widget_type)
2156 return val1->widget_type < val2->widget_type ? -1 : 1;
2158 if (val1->pspec != val2->pspec)
2159 return val1->pspec < val2->pspec ? -1 : 1;
2161 if (val1->state != val2->state)
2162 return val1->state < val2->state ? -1 : 1;
2168 _gtk_style_context_peek_property (GtkStyleContext *context,
2169 const char *property_name)
2171 StyleData *data = style_data_lookup (context, gtk_style_context_get_state (context));
2173 return _gtk_css_computed_values_get_value_by_name (data->store, property_name);
2177 _gtk_style_context_get_number (GtkStyleContext *context,
2178 const char *property_name,
2179 double one_hundred_percent)
2183 value = _gtk_style_context_peek_property (context, property_name);
2184 return _gtk_css_number_get (_gtk_css_value_get_number (value), one_hundred_percent);
2188 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2190 GtkStateFlags state,
2193 GtkStyleContextPrivate *priv;
2194 PropertyValue *pcache, key = { 0 };
2198 priv = context->priv;
2199 data = style_data_lookup (context, state);
2201 key.widget_type = widget_type;
2205 /* need value cache array */
2206 if (!data->property_cache)
2207 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2210 pcache = bsearch (&key,
2211 data->property_cache->data, data->property_cache->len,
2212 sizeof (PropertyValue), style_property_values_cmp);
2214 return &pcache->value;
2218 while (i < data->property_cache->len &&
2219 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2222 g_array_insert_val (data->property_cache, i, key);
2223 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2225 /* cache miss, initialize value type, then set contents */
2226 g_param_spec_ref (pcache->pspec);
2227 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2229 if (priv->widget || priv->widget_path)
2231 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2232 priv->widget_path, state,
2233 pspec, &pcache->value))
2235 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2236 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2238 GtkSymbolicColor *color;
2241 color = g_value_dup_boxed (&pcache->value);
2243 g_value_unset (&pcache->value);
2245 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2246 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2248 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2250 if (_gtk_style_context_resolve_color (context, color, &rgba))
2252 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2253 g_value_set_boxed (&pcache->value, &rgba);
2258 rgb.red = rgba.red * 65535. + 0.5;
2259 rgb.green = rgba.green * 65535. + 0.5;
2260 rgb.blue = rgba.blue * 65535. + 0.5;
2262 g_value_set_boxed (&pcache->value, &rgb);
2266 g_param_value_set_default (pspec, &pcache->value);
2268 gtk_symbolic_color_unref (color);
2271 return &pcache->value;
2275 /* not supplied by any provider, revert to default */
2276 g_param_value_set_default (pspec, &pcache->value);
2278 return &pcache->value;
2282 * gtk_style_context_get_style_property:
2283 * @context: a #GtkStyleContext
2284 * @property_name: the name of the widget style property
2285 * @value: Return location for the property value
2287 * Gets the value for a widget style property.
2289 * When @value is no longer needed, g_value_unset() must be called
2290 * to free any allocated memory.
2293 gtk_style_context_get_style_property (GtkStyleContext *context,
2294 const gchar *property_name,
2297 GtkStyleContextPrivate *priv;
2298 GtkWidgetClass *widget_class;
2299 GtkStateFlags state;
2301 const GValue *peek_value;
2304 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2305 g_return_if_fail (property_name != NULL);
2306 g_return_if_fail (value != NULL);
2308 priv = context->priv;
2310 if (!priv->widget_path)
2313 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2315 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2317 g_warning ("%s: can't get style properties for non-widget class `%s'",
2319 g_type_name (widget_type));
2323 widget_class = g_type_class_ref (widget_type);
2324 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2325 g_type_class_unref (widget_class);
2329 g_warning ("%s: widget class `%s' has no style property named `%s'",
2331 g_type_name (widget_type),
2336 state = gtk_style_context_get_state (context);
2337 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2340 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2341 g_value_copy (peek_value, value);
2342 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2343 g_value_transform (peek_value, value);
2345 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2347 G_VALUE_TYPE_NAME (peek_value),
2348 G_VALUE_TYPE_NAME (value));
2352 * gtk_style_context_get_style_valist:
2353 * @context: a #GtkStyleContext
2354 * @args: va_list of property name/return location pairs, followed by %NULL
2356 * Retrieves several widget style properties from @context according to the
2362 gtk_style_context_get_style_valist (GtkStyleContext *context,
2365 GtkStyleContextPrivate *priv;
2366 const gchar *prop_name;
2367 GtkStateFlags state;
2370 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2372 prop_name = va_arg (args, const gchar *);
2373 priv = context->priv;
2375 if (!priv->widget_path)
2378 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2380 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2382 g_warning ("%s: can't get style properties for non-widget class `%s'",
2384 g_type_name (widget_type));
2388 state = gtk_style_context_get_state (context);
2392 GtkWidgetClass *widget_class;
2394 const GValue *peek_value;
2397 widget_class = g_type_class_ref (widget_type);
2398 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2399 g_type_class_unref (widget_class);
2403 g_warning ("%s: widget class `%s' has no style property named `%s'",
2405 g_type_name (widget_type),
2410 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2413 G_VALUE_LCOPY (peek_value, args, 0, &error);
2417 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2419 G_VALUE_TYPE_NAME (peek_value),
2424 prop_name = va_arg (args, const gchar *);
2429 * gtk_style_context_get_style:
2430 * @context: a #GtkStyleContext
2431 * @...: property name /return value pairs, followed by %NULL
2433 * Retrieves several widget style properties from @context according to the
2439 gtk_style_context_get_style (GtkStyleContext *context,
2444 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2446 va_start (args, context);
2447 gtk_style_context_get_style_valist (context, args);
2453 * gtk_style_context_lookup_icon_set:
2454 * @context: a #GtkStyleContext
2455 * @stock_id: an icon name
2457 * Looks up @stock_id in the icon factories associated to @context and
2458 * the default icon factory, returning an icon set if found, otherwise
2461 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2464 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2465 const gchar *stock_id)
2467 GtkStyleContextPrivate *priv;
2469 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2470 g_return_val_if_fail (stock_id != NULL, NULL);
2472 priv = context->priv;
2473 g_return_val_if_fail (priv->widget_path != NULL, NULL);
2475 return gtk_icon_factory_lookup_default (stock_id);
2479 * gtk_style_context_set_screen:
2480 * @context: a #GtkStyleContext
2481 * @screen: a #GdkScreen
2483 * Attaches @context to the given screen.
2485 * The screen is used to add style information from 'global' style
2486 * providers, such as the screens #GtkSettings instance.
2488 * If you are using a #GtkStyleContext returned from
2489 * gtk_widget_get_style_context(), you do not need to
2490 * call this yourself.
2495 gtk_style_context_set_screen (GtkStyleContext *context,
2498 GtkStyleContextPrivate *priv;
2500 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2501 g_return_if_fail (GDK_IS_SCREEN (screen));
2503 priv = context->priv;
2504 if (priv->screen == screen)
2507 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2509 g_object_unref (priv->cascade);
2510 priv->cascade = _gtk_style_cascade_get_for_screen (screen);
2511 g_object_ref (priv->cascade);
2515 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2518 priv->screen = screen;
2520 g_object_notify (G_OBJECT (context), "screen");
2522 gtk_style_context_invalidate (context);
2526 * gtk_style_context_get_screen:
2527 * @context: a #GtkStyleContext
2529 * Returns the #GdkScreen to which @context is attached.
2531 * Returns: (transfer none): a #GdkScreen.
2534 gtk_style_context_get_screen (GtkStyleContext *context)
2536 GtkStyleContextPrivate *priv;
2538 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2540 priv = context->priv;
2541 return priv->screen;
2545 * gtk_style_context_set_direction:
2546 * @context: a #GtkStyleContext
2547 * @direction: the new direction.
2549 * Sets the reading direction for rendering purposes.
2551 * If you are using a #GtkStyleContext returned from
2552 * gtk_widget_get_style_context(), you do not need to
2553 * call this yourself.
2558 gtk_style_context_set_direction (GtkStyleContext *context,
2559 GtkTextDirection direction)
2561 GtkStyleContextPrivate *priv;
2563 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2565 priv = context->priv;
2566 priv->direction = direction;
2568 g_object_notify (G_OBJECT (context), "direction");
2572 * gtk_style_context_get_direction:
2573 * @context: a #GtkStyleContext
2575 * Returns the widget direction used for rendering.
2577 * Returns: the widget direction
2582 gtk_style_context_get_direction (GtkStyleContext *context)
2584 GtkStyleContextPrivate *priv;
2586 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2588 priv = context->priv;
2589 return priv->direction;
2593 * gtk_style_context_set_junction_sides:
2594 * @context: a #GtkStyleContext
2595 * @sides: sides where rendered elements are visually connected to
2598 * Sets the sides where rendered elements (mostly through
2599 * gtk_render_frame()) will visually connect with other visual elements.
2601 * This is merely a hint that may or may not be honored
2602 * by theming engines.
2604 * Container widgets are expected to set junction hints as appropriate
2605 * for their children, so it should not normally be necessary to call
2606 * this function manually.
2611 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2612 GtkJunctionSides sides)
2614 GtkStyleContextPrivate *priv;
2617 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2619 priv = context->priv;
2620 info = priv->info_stack->data;
2621 info->junction_sides = sides;
2625 * gtk_style_context_get_junction_sides:
2626 * @context: a #GtkStyleContext
2628 * Returns the sides where rendered elements connect visually with others.
2630 * Returns: the junction sides
2635 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2637 GtkStyleContextPrivate *priv;
2640 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2642 priv = context->priv;
2643 info = priv->info_stack->data;
2644 return info->junction_sides;
2647 static GtkSymbolicColor *
2648 gtk_style_context_color_lookup_func (gpointer contextp,
2651 GtkStyleContext *context = contextp;
2653 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2657 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2658 GtkSymbolicColor *color)
2660 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2661 g_return_val_if_fail (color != NULL, FALSE);
2663 return _gtk_symbolic_color_resolve_full (color,
2664 gtk_style_context_color_lookup_func,
2670 _gtk_style_context_resolve_color (GtkStyleContext *context,
2671 GtkSymbolicColor *color,
2676 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2677 g_return_val_if_fail (color != NULL, FALSE);
2678 g_return_val_if_fail (result != NULL, FALSE);
2680 val = _gtk_symbolic_color_resolve_full (color,
2681 gtk_style_context_color_lookup_func,
2686 *result = *_gtk_css_value_get_rgba (val);
2687 _gtk_css_value_unref (val);
2692 * gtk_style_context_lookup_color:
2693 * @context: a #GtkStyleContext
2694 * @color_name: color name to lookup
2695 * @color: (out): Return location for the looked up color
2697 * Looks up and resolves a color name in the @context color map.
2699 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2702 gtk_style_context_lookup_color (GtkStyleContext *context,
2703 const gchar *color_name,
2706 GtkSymbolicColor *sym_color;
2708 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2709 g_return_val_if_fail (color_name != NULL, FALSE);
2710 g_return_val_if_fail (color != NULL, FALSE);
2712 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2713 if (sym_color == NULL)
2716 return _gtk_style_context_resolve_color (context, sym_color, color);
2720 * gtk_style_context_notify_state_change:
2721 * @context: a #GtkStyleContext
2722 * @window: a #GdkWindow
2723 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2724 * See gtk_style_context_push_animatable_region()
2725 * @state: state to trigger transition for
2726 * @state_value: %TRUE if @state is the state we are changing to,
2727 * %FALSE if we are changing away from it
2729 * Notifies a state change on @context, so if the current style makes use
2730 * of transition animations, one will be started so all rendered elements
2731 * under @region_id are animated for state @state being set to value
2734 * The @window parameter is used in order to invalidate the rendered area
2735 * as the animation runs, so make sure it is the same window that is being
2736 * rendered on by the gtk_render_*() functions.
2738 * If @region_id is %NULL, all rendered elements using @context will be
2739 * affected by this state transition.
2741 * As a practical example, a #GtkButton notifying a state transition on
2742 * the prelight state:
2744 * gtk_style_context_notify_state_change (context,
2745 * gtk_widget_get_window (widget),
2747 * GTK_STATE_PRELIGHT,
2748 * button->in_button);
2751 * Can be handled in the CSS file like this:
2754 * background-color: #f00
2758 * background-color: #fff;
2759 * transition: 200ms linear
2763 * This combination will animate the button background from red to white
2764 * if a pointer enters the button, and back to red if the pointer leaves
2767 * Note that @state is used when finding the transition parameters, which
2768 * is why the style places the transition under the :hover pseudo-class.
2773 gtk_style_context_notify_state_change (GtkStyleContext *context,
2777 gboolean state_value)
2779 GtkStyleContextPrivate *priv;
2780 GtkAnimationDescription *desc;
2781 AnimationInfo *info;
2782 GtkStateFlags flags;
2786 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2787 g_return_if_fail (GDK_IS_WINDOW (window));
2788 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2790 priv = context->priv;
2791 g_return_if_fail (priv->widget_path != NULL);
2793 state_value = (state_value == TRUE);
2797 case GTK_STATE_ACTIVE:
2798 flags = GTK_STATE_FLAG_ACTIVE;
2800 case GTK_STATE_PRELIGHT:
2801 flags = GTK_STATE_FLAG_PRELIGHT;
2803 case GTK_STATE_SELECTED:
2804 flags = GTK_STATE_FLAG_SELECTED;
2806 case GTK_STATE_INSENSITIVE:
2807 flags = GTK_STATE_FLAG_INSENSITIVE;
2809 case GTK_STATE_INCONSISTENT:
2810 flags = GTK_STATE_FLAG_INCONSISTENT;
2812 case GTK_STATE_FOCUSED:
2813 flags = GTK_STATE_FLAG_FOCUSED;
2815 case GTK_STATE_NORMAL:
2821 /* Find out if there is any animation description for the given
2822 * state, it will fallback to the normal state as well if necessary.
2824 data = style_data_lookup (context, flags);
2825 v = _gtk_css_computed_values_get_value_by_name (data->store, "transition");
2828 desc = _gtk_css_value_get_boxed (v);
2832 if (_gtk_animation_description_get_duration (desc) == 0)
2835 info = animation_info_lookup (context, region_id, state);
2838 info->target_value != state_value)
2840 /* Target values are the opposite */
2841 if (!_gtk_timeline_get_loop (info->timeline))
2843 /* Reverse the animation */
2844 if (_gtk_timeline_get_direction (info->timeline) == GTK_TIMELINE_DIRECTION_FORWARD)
2845 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
2847 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_FORWARD);
2849 info->target_value = state_value;
2853 /* Take it out of its looping state */
2854 _gtk_timeline_set_loop (info->timeline, FALSE);
2858 (!_gtk_animation_description_get_loop (desc) ||
2861 info = animation_info_new (context, region_id,
2862 _gtk_animation_description_get_duration (desc),
2863 _gtk_animation_description_get_progress_type (desc),
2864 _gtk_animation_description_get_loop (desc),
2865 state, state_value, window);
2867 priv->animations = g_slist_prepend (priv->animations, info);
2868 priv->animations_invalidated = TRUE;
2873 * gtk_style_context_cancel_animations:
2874 * @context: a #GtkStyleContext
2875 * @region_id: (allow-none): animatable region to stop, or %NULL.
2876 * See gtk_style_context_push_animatable_region()
2878 * Stops all running animations for @region_id and all animatable
2879 * regions underneath.
2881 * A %NULL @region_id will stop all ongoing animations in @context,
2882 * when dealing with a #GtkStyleContext obtained through
2883 * gtk_widget_get_style_context(), this is normally done for you
2884 * in all circumstances you would expect all widget to be stopped,
2885 * so this should be only used in complex widgets with different
2886 * animatable regions.
2891 gtk_style_context_cancel_animations (GtkStyleContext *context,
2894 GtkStyleContextPrivate *priv;
2895 AnimationInfo *info;
2898 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2900 priv = context->priv;
2901 l = priv->animations;
2909 info->region_id == region_id ||
2910 g_slist_find (info->parent_regions, region_id))
2912 priv->animations = g_slist_remove (priv->animations, info);
2913 animation_info_free (info);
2919 is_parent_of (GdkWindow *parent,
2922 GtkWidget *child_widget, *parent_widget;
2925 gdk_window_get_user_data (child, (gpointer *) &child_widget);
2926 gdk_window_get_user_data (parent, (gpointer *) &parent_widget);
2928 if (child_widget != parent_widget &&
2929 !gtk_widget_is_ancestor (child_widget, parent_widget))
2936 if (window == parent)
2939 window = gdk_window_get_parent (window);
2946 * gtk_style_context_scroll_animations:
2947 * @context: a #GtkStyleContext
2948 * @window: a #GdkWindow used previously in
2949 * gtk_style_context_notify_state_change()
2950 * @dx: Amount to scroll in the X axis
2951 * @dy: Amount to scroll in the Y axis
2953 * This function is analogous to gdk_window_scroll(), and
2954 * should be called together with it so the invalidation
2955 * areas for any ongoing animation are scrolled together
2961 gtk_style_context_scroll_animations (GtkStyleContext *context,
2966 GtkStyleContextPrivate *priv;
2967 AnimationInfo *info;
2970 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2971 g_return_if_fail (GDK_IS_WINDOW (window));
2973 priv = context->priv;
2974 l = priv->animations;
2981 if (info->invalidation_region &&
2982 (window == info->window ||
2983 is_parent_of (window, info->window)))
2984 cairo_region_translate (info->invalidation_region, dx, dy);
2989 * gtk_style_context_push_animatable_region:
2990 * @context: a #GtkStyleContext
2991 * @region_id: unique identifier for the animatable region
2993 * Pushes an animatable region, so all further gtk_render_*() calls between
2994 * this call and the following gtk_style_context_pop_animatable_region()
2995 * will potentially show transition animations for this region if
2996 * gtk_style_context_notify_state_change() is called for a given state,
2997 * and the current theme/style defines transition animations for state
3000 * The @region_id used must be unique in @context so the theming engine
3001 * can uniquely identify rendered elements subject to a state transition.
3006 gtk_style_context_push_animatable_region (GtkStyleContext *context,
3009 GtkStyleContextPrivate *priv;
3011 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3012 g_return_if_fail (region_id != NULL);
3014 priv = context->priv;
3015 priv->animation_regions = g_slist_prepend (priv->animation_regions, region_id);
3019 * gtk_style_context_pop_animatable_region:
3020 * @context: a #GtkStyleContext
3022 * Pops an animatable region from @context.
3023 * See gtk_style_context_push_animatable_region().
3028 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
3030 GtkStyleContextPrivate *priv;
3032 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3034 priv = context->priv;
3035 priv->animation_regions = g_slist_delete_link (priv->animation_regions,
3036 priv->animation_regions);
3040 _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context)
3042 GtkStyleContextPrivate *priv;
3045 priv = context->priv;
3047 for (l = priv->animations; l; l = l->next)
3049 AnimationInfo *info;
3053 /* A NULL invalidation region means it has to be recreated on
3054 * the next expose event, this happens usually after a widget
3055 * allocation change, so the next expose after it will update
3056 * the invalidation region.
3058 if (info->invalidation_region)
3060 cairo_region_destroy (info->invalidation_region);
3061 info->invalidation_region = NULL;
3065 priv->animations_invalidated = TRUE;
3069 _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context,
3072 GtkStyleContextPrivate *priv;
3075 priv = context->priv;
3077 if (!priv->animations_invalidated)
3080 l = priv->animations;
3084 AnimationInfo *info;
3093 if (info->invalidation_region)
3096 if (info->rectangles->len == 0)
3099 info->invalidation_region = cairo_region_create ();
3100 _gtk_widget_get_translation_to_window (widget, info->window, &rel_x, &rel_y);
3102 for (i = 0; i < info->rectangles->len; i++)
3104 cairo_rectangle_int_t *rect;
3106 rect = &g_array_index (info->rectangles, cairo_rectangle_int_t, i);
3108 /* These are widget relative coordinates,
3109 * so have them inverted to be window relative
3114 cairo_region_union_rectangle (info->invalidation_region, rect);
3117 g_array_remove_range (info->rectangles, 0, info->rectangles->len);
3120 priv->animations_invalidated = FALSE;
3124 store_animation_region (GtkStyleContext *context,
3130 GtkStyleContextPrivate *priv;
3133 priv = context->priv;
3135 if (!priv->animations_invalidated)
3138 for (l = priv->animations; l; l = l->next)
3140 AnimationInfo *info;
3144 /* The animation doesn't need updating
3145 * the invalidation area, bail out.
3147 if (info->invalidation_region)
3150 if (context_has_animatable_region (context, info->region_id))
3152 cairo_rectangle_int_t rect;
3156 rect.width = (gint) width;
3157 rect.height = (gint) height;
3159 g_array_append_val (info->rectangles, rect);
3161 if (!info->parent_regions)
3163 GSList *parent_regions;
3165 parent_regions = g_slist_find (priv->animation_regions, info->region_id);
3166 info->parent_regions = g_slist_copy (parent_regions);
3173 * gtk_style_context_invalidate:
3174 * @context: a #GtkStyleContext.
3176 * Invalidates @context style information, so it will be reconstructed
3179 * If you're using a #GtkStyleContext returned from
3180 * gtk_widget_get_style_context(), you do not need to
3181 * call this yourself.
3186 gtk_style_context_invalidate (GtkStyleContext *context)
3188 GtkStyleContextPrivate *priv;
3190 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3192 priv = context->priv;
3194 /* Avoid reentrancy */
3195 if (priv->invalidating_context)
3198 priv->invalidating_context = TRUE;
3200 g_hash_table_remove_all (priv->style_data);
3201 priv->current_data = NULL;
3203 g_signal_emit (context, signals[CHANGED], 0);
3205 priv->invalidating_context = FALSE;
3209 * gtk_style_context_set_background:
3210 * @context: a #GtkStyleContext
3211 * @window: a #GdkWindow
3213 * Sets the background of @window to the background pattern or
3214 * color specified in @context for its current state.
3219 gtk_style_context_set_background (GtkStyleContext *context,
3222 GtkStateFlags state;
3223 cairo_pattern_t *pattern;
3226 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3227 g_return_if_fail (GDK_IS_WINDOW (window));
3229 state = gtk_style_context_get_state (context);
3230 gtk_style_context_get (context, state,
3231 "background-image", &pattern,
3235 gdk_window_set_background_pattern (window, pattern);
3236 cairo_pattern_destroy (pattern);
3240 gtk_style_context_get (context, state,
3241 "background-color", &color,
3245 gdk_window_set_background_rgba (window, color);
3246 gdk_rgba_free (color);
3251 * gtk_style_context_get_color:
3252 * @context: a #GtkStyleContext
3253 * @state: state to retrieve the color for
3254 * @color: (out): return value for the foreground color
3256 * Gets the foreground color for a given state.
3261 gtk_style_context_get_color (GtkStyleContext *context,
3262 GtkStateFlags state,
3267 g_return_if_fail (color != NULL);
3268 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3270 gtk_style_context_get (context,
3280 * gtk_style_context_get_background_color:
3281 * @context: a #GtkStyleContext
3282 * @state: state to retrieve the color for
3283 * @color: (out): return value for the background color
3285 * Gets the background color for a given state.
3290 gtk_style_context_get_background_color (GtkStyleContext *context,
3291 GtkStateFlags state,
3296 g_return_if_fail (color != NULL);
3297 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3299 gtk_style_context_get (context,
3301 "background-color", &c,
3309 * gtk_style_context_get_border_color:
3310 * @context: a #GtkStyleContext
3311 * @state: state to retrieve the color for
3312 * @color: (out): return value for the border color
3314 * Gets the border color for a given state.
3319 gtk_style_context_get_border_color (GtkStyleContext *context,
3320 GtkStateFlags state,
3325 g_return_if_fail (color != NULL);
3326 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3328 gtk_style_context_get (context,
3338 * gtk_style_context_get_border:
3339 * @context: a #GtkStyleContext
3340 * @state: state to retrieve the border for
3341 * @border: (out): return value for the border settings
3343 * Gets the border for a given state as a #GtkBorder.
3344 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3349 gtk_style_context_get_border (GtkStyleContext *context,
3350 GtkStateFlags state,
3353 int top, left, bottom, right;
3355 g_return_if_fail (border != NULL);
3356 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3358 gtk_style_context_get (context,
3360 "border-top-width", &top,
3361 "border-left-width", &left,
3362 "border-bottom-width", &bottom,
3363 "border-right-width", &right,
3367 border->left = left;
3368 border->bottom = bottom;
3369 border->right = right;
3373 * gtk_style_context_get_padding:
3374 * @context: a #GtkStyleContext
3375 * @state: state to retrieve the padding for
3376 * @padding: (out): return value for the padding settings
3378 * Gets the padding for a given state as a #GtkBorder.
3379 * See %GTK_STYLE_PROPERTY_PADDING.
3384 gtk_style_context_get_padding (GtkStyleContext *context,
3385 GtkStateFlags state,
3388 int top, left, bottom, right;
3390 g_return_if_fail (padding != NULL);
3391 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3393 gtk_style_context_get (context,
3395 "padding-top", &top,
3396 "padding-left", &left,
3397 "padding-bottom", &bottom,
3398 "padding-right", &right,
3402 padding->left = left;
3403 padding->bottom = bottom;
3404 padding->right = right;
3408 * gtk_style_context_get_margin:
3409 * @context: a #GtkStyleContext
3410 * @state: state to retrieve the border for
3411 * @margin: (out): return value for the margin settings
3413 * Gets the margin for a given state as a #GtkBorder.
3414 * See %GTK_STYLE_PROPERTY_MARGIN.
3419 gtk_style_context_get_margin (GtkStyleContext *context,
3420 GtkStateFlags state,
3423 int top, left, bottom, right;
3425 g_return_if_fail (margin != NULL);
3426 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3428 gtk_style_context_get (context,
3431 "margin-left", &left,
3432 "margin-bottom", &bottom,
3433 "margin-right", &right,
3437 margin->left = left;
3438 margin->bottom = bottom;
3439 margin->right = right;
3443 * gtk_style_context_get_font:
3444 * @context: a #GtkStyleContext
3445 * @state: state to retrieve the font for
3447 * Returns the font description for a given state. The returned
3448 * object is const and will remain valid until the
3449 * #GtkStyleContext::changed signal happens.
3451 * Returns: (transfer none): the #PangoFontDescription for the given
3452 * state. This object is owned by GTK+ and should not be
3457 const PangoFontDescription *
3458 gtk_style_context_get_font (GtkStyleContext *context,
3459 GtkStateFlags state)
3461 GtkStyleContextPrivate *priv;
3463 PangoFontDescription *description;
3465 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3467 priv = context->priv;
3468 g_return_val_if_fail (priv->widget_path != NULL, NULL);
3470 data = style_data_lookup (context, state);
3472 /* Yuck, fonts are created on-demand but we don't return a ref.
3473 * Do bad things to achieve this requirement */
3474 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3475 if (description == NULL)
3477 gtk_style_context_get (context, state, "font", &description, NULL);
3478 g_object_set_data_full (G_OBJECT (data->store),
3479 "font-cache-for-get_font",
3481 (GDestroyNotify) pango_font_description_free);
3487 get_cursor_color (GtkStyleContext *context,
3491 GdkColor *style_color;
3493 gtk_style_context_get_style (context,
3494 primary ? "cursor-color" : "secondary-cursor-color",
3500 color->red = style_color->red / 65535.0;
3501 color->green = style_color->green / 65535.0;
3502 color->blue = style_color->blue / 65535.0;
3505 gdk_color_free (style_color);
3509 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3515 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3517 color->red = (color->red + bg.red) * 0.5;
3518 color->green = (color->green + bg.green) * 0.5;
3519 color->blue = (color->blue + bg.blue) * 0.5;
3525 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3526 GdkRGBA *primary_color,
3527 GdkRGBA *secondary_color)
3530 get_cursor_color (context, TRUE, primary_color);
3532 if (secondary_color)
3533 get_cursor_color (context, FALSE, secondary_color);
3540 * @context: a #GtkStyleContext
3542 * @x: X origin of the rectangle
3543 * @y: Y origin of the rectangle
3544 * @width: rectangle width
3545 * @height: rectangle height
3547 * Renders a checkmark (as in a #GtkCheckButton).
3549 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3550 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3551 * should be marked as undefined.
3554 * <title>Typical checkmark rendering</title>
3555 * <inlinegraphic fileref="checks.png" format="PNG"/>
3561 gtk_render_check (GtkStyleContext *context,
3568 GtkStyleContextPrivate *priv;
3569 GtkThemingEngineClass *engine_class;
3571 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3572 g_return_if_fail (cr != NULL);
3574 if (width <= 0 || height <= 0)
3577 priv = context->priv;
3578 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3582 store_animation_region (context, x, y, width, height);
3584 _gtk_theming_engine_set_context (priv->theming_engine, context);
3585 engine_class->render_check (priv->theming_engine, cr,
3586 x, y, width, height);
3592 * gtk_render_option:
3593 * @context: a #GtkStyleContext
3595 * @x: X origin of the rectangle
3596 * @y: Y origin of the rectangle
3597 * @width: rectangle width
3598 * @height: rectangle height
3600 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3601 * state will determine whether the option is on or off, and
3602 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3605 * <title>Typical option mark rendering</title>
3606 * <inlinegraphic fileref="options.png" format="PNG"/>
3612 gtk_render_option (GtkStyleContext *context,
3619 GtkStyleContextPrivate *priv;
3620 GtkThemingEngineClass *engine_class;
3622 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3623 g_return_if_fail (cr != NULL);
3625 if (width <= 0 || height <= 0)
3628 priv = context->priv;
3629 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3633 store_animation_region (context, x, y, width, height);
3635 _gtk_theming_engine_set_context (priv->theming_engine, context);
3636 engine_class->render_option (priv->theming_engine, cr,
3637 x, y, width, height);
3644 * @context: a #GtkStyleContext
3646 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3647 * @x: X origin of the render area
3648 * @y: Y origin of the render area
3649 * @size: square side for render area
3651 * Renders an arrow pointing to @angle.
3654 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3655 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3661 gtk_render_arrow (GtkStyleContext *context,
3668 GtkStyleContextPrivate *priv;
3669 GtkThemingEngineClass *engine_class;
3671 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3672 g_return_if_fail (cr != NULL);
3677 priv = context->priv;
3678 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3682 gtk_style_context_save (context);
3683 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3685 store_animation_region (context, x, y, size, size);
3687 _gtk_theming_engine_set_context (priv->theming_engine, context);
3688 engine_class->render_arrow (priv->theming_engine, cr,
3691 gtk_style_context_restore (context);
3696 * gtk_render_background:
3697 * @context: a #GtkStyleContext
3699 * @x: X origin of the rectangle
3700 * @y: Y origin of the rectangle
3701 * @width: rectangle width
3702 * @height: rectangle height
3704 * Renders the background of an element.
3707 * <title>Typical background rendering, showing the effect of
3708 * <parameter>background-image</parameter>,
3709 * <parameter>border-width</parameter> and
3710 * <parameter>border-radius</parameter></title>
3711 * <inlinegraphic fileref="background.png" format="PNG"/>
3717 gtk_render_background (GtkStyleContext *context,
3724 GtkStyleContextPrivate *priv;
3725 GtkThemingEngineClass *engine_class;
3727 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3728 g_return_if_fail (cr != NULL);
3730 if (width <= 0 || height <= 0)
3733 priv = context->priv;
3734 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3738 store_animation_region (context, x, y, width, height);
3740 _gtk_theming_engine_set_context (priv->theming_engine, context);
3741 engine_class->render_background (priv->theming_engine, cr, x, y, width, height);
3748 * @context: a #GtkStyleContext
3750 * @x: X origin of the rectangle
3751 * @y: Y origin of the rectangle
3752 * @width: rectangle width
3753 * @height: rectangle height
3755 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3758 * <title>Examples of frame rendering, showing the effect of
3759 * <parameter>border-image</parameter>,
3760 * <parameter>border-color</parameter>,
3761 * <parameter>border-width</parameter>,
3762 * <parameter>border-radius</parameter> and
3764 * <inlinegraphic fileref="frames.png" format="PNG"/>
3770 gtk_render_frame (GtkStyleContext *context,
3777 GtkStyleContextPrivate *priv;
3778 GtkThemingEngineClass *engine_class;
3780 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3781 g_return_if_fail (cr != NULL);
3783 if (width <= 0 || height <= 0)
3786 priv = context->priv;
3787 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3791 store_animation_region (context, x, y, width, height);
3793 _gtk_theming_engine_set_context (priv->theming_engine, context);
3794 engine_class->render_frame (priv->theming_engine, cr, x, y, width, height);
3800 * gtk_render_expander:
3801 * @context: a #GtkStyleContext
3803 * @x: X origin of the rectangle
3804 * @y: Y origin of the rectangle
3805 * @width: rectangle width
3806 * @height: rectangle height
3808 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3809 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3810 * determines whether the expander is collapsed or expanded.
3813 * <title>Typical expander rendering</title>
3814 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3820 gtk_render_expander (GtkStyleContext *context,
3827 GtkStyleContextPrivate *priv;
3828 GtkThemingEngineClass *engine_class;
3830 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3831 g_return_if_fail (cr != NULL);
3833 if (width <= 0 || height <= 0)
3836 priv = context->priv;
3837 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3841 store_animation_region (context, x, y, width, height);
3843 _gtk_theming_engine_set_context (priv->theming_engine, context);
3844 engine_class->render_expander (priv->theming_engine, cr, x, y, width, height);
3851 * @context: a #GtkStyleContext
3853 * @x: X origin of the rectangle
3854 * @y: Y origin of the rectangle
3855 * @width: rectangle width
3856 * @height: rectangle height
3858 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3860 * <title>Typical focus rendering</title>
3861 * <inlinegraphic fileref="focus.png" format="PNG"/>
3867 gtk_render_focus (GtkStyleContext *context,
3874 GtkStyleContextPrivate *priv;
3875 GtkThemingEngineClass *engine_class;
3877 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3878 g_return_if_fail (cr != NULL);
3880 if (width <= 0 || height <= 0)
3883 priv = context->priv;
3884 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3888 store_animation_region (context, x, y, width, height);
3890 _gtk_theming_engine_set_context (priv->theming_engine, context);
3891 engine_class->render_focus (priv->theming_engine, cr, x, y, width, height);
3897 * gtk_render_layout:
3898 * @context: a #GtkStyleContext
3902 * @layout: the #PangoLayout to render
3904 * Renders @layout on the coordinates @x, @y
3909 gtk_render_layout (GtkStyleContext *context,
3913 PangoLayout *layout)
3915 GtkStyleContextPrivate *priv;
3916 GtkThemingEngineClass *engine_class;
3917 PangoRectangle extents;
3919 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3920 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3921 g_return_if_fail (cr != NULL);
3923 priv = context->priv;
3924 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3928 pango_layout_get_extents (layout, &extents, NULL);
3930 store_animation_region (context,
3936 _gtk_theming_engine_set_context (priv->theming_engine, context);
3937 engine_class->render_layout (priv->theming_engine, cr, x, y, layout);
3944 * @context: a #GtkStyleContext
3946 * @x0: X coordinate for the origin of the line
3947 * @y0: Y coordinate for the origin of the line
3948 * @x1: X coordinate for the end of the line
3949 * @y1: Y coordinate for the end of the line
3951 * Renders a line from (x0, y0) to (x1, y1).
3956 gtk_render_line (GtkStyleContext *context,
3963 GtkStyleContextPrivate *priv;
3964 GtkThemingEngineClass *engine_class;
3966 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3967 g_return_if_fail (cr != NULL);
3969 priv = context->priv;
3970 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3974 _gtk_theming_engine_set_context (priv->theming_engine, context);
3975 engine_class->render_line (priv->theming_engine, cr, x0, y0, x1, y1);
3981 * gtk_render_slider:
3982 * @context: a #GtkStyleContext
3984 * @x: X origin of the rectangle
3985 * @y: Y origin of the rectangle
3986 * @width: rectangle width
3987 * @height: rectangle height
3988 * @orientation: orientation of the slider
3990 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
3991 * @width, @height. @orientation defines whether the slider is vertical
3995 * <title>Typical slider rendering</title>
3996 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4002 gtk_render_slider (GtkStyleContext *context,
4008 GtkOrientation orientation)
4010 GtkStyleContextPrivate *priv;
4011 GtkThemingEngineClass *engine_class;
4013 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4014 g_return_if_fail (cr != NULL);
4016 if (width <= 0 || height <= 0)
4019 priv = context->priv;
4020 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4024 store_animation_region (context, x, y, width, height);
4026 _gtk_theming_engine_set_context (priv->theming_engine, context);
4027 engine_class->render_slider (priv->theming_engine, cr, x, y, width, height, orientation);
4033 * gtk_render_frame_gap:
4034 * @context: a #GtkStyleContext
4036 * @x: X origin of the rectangle
4037 * @y: Y origin of the rectangle
4038 * @width: rectangle width
4039 * @height: rectangle height
4040 * @gap_side: side where the gap is
4041 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4042 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4044 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4045 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4046 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4047 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4050 * <title>Typical rendering of a frame with a gap</title>
4051 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4057 gtk_render_frame_gap (GtkStyleContext *context,
4063 GtkPositionType gap_side,
4067 GtkStyleContextPrivate *priv;
4068 GtkThemingEngineClass *engine_class;
4070 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4071 g_return_if_fail (cr != NULL);
4072 g_return_if_fail (xy0_gap <= xy1_gap);
4073 g_return_if_fail (xy0_gap >= 0);
4075 if (width <= 0 || height <= 0)
4078 if (gap_side == GTK_POS_LEFT ||
4079 gap_side == GTK_POS_RIGHT)
4080 g_return_if_fail (xy1_gap <= height);
4082 g_return_if_fail (xy1_gap <= width);
4084 priv = context->priv;
4085 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4089 store_animation_region (context, x, y, width, height);
4091 _gtk_theming_engine_set_context (priv->theming_engine, context);
4092 engine_class->render_frame_gap (priv->theming_engine, cr,
4093 x, y, width, height, gap_side,
4100 * gtk_render_extension:
4101 * @context: a #GtkStyleContext
4103 * @x: X origin of the rectangle
4104 * @y: Y origin of the rectangle
4105 * @width: rectangle width
4106 * @height: rectangle height
4107 * @gap_side: side where the gap is
4109 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4110 * defined by @x, @y, @width, @height. The side where the extension
4111 * connects to is defined by @gap_side.
4114 * <title>Typical extension rendering</title>
4115 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4121 gtk_render_extension (GtkStyleContext *context,
4127 GtkPositionType gap_side)
4129 GtkStyleContextPrivate *priv;
4130 GtkThemingEngineClass *engine_class;
4132 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4133 g_return_if_fail (cr != NULL);
4135 if (width <= 0 || height <= 0)
4138 priv = context->priv;
4139 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4143 store_animation_region (context, x, y, width, height);
4145 _gtk_theming_engine_set_context (priv->theming_engine, context);
4146 engine_class->render_extension (priv->theming_engine, cr, x, y, width, height, gap_side);
4152 * gtk_render_handle:
4153 * @context: a #GtkStyleContext
4155 * @x: X origin of the rectangle
4156 * @y: Y origin of the rectangle
4157 * @width: rectangle width
4158 * @height: rectangle height
4160 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4161 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4162 * determined by @x, @y, @width, @height.
4165 * <title>Handles rendered for the paned and grip classes</title>
4166 * <inlinegraphic fileref="handles.png" format="PNG"/>
4172 gtk_render_handle (GtkStyleContext *context,
4179 GtkStyleContextPrivate *priv;
4180 GtkThemingEngineClass *engine_class;
4182 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4183 g_return_if_fail (cr != NULL);
4185 if (width <= 0 || height <= 0)
4188 priv = context->priv;
4189 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4193 store_animation_region (context, x, y, width, height);
4195 _gtk_theming_engine_set_context (priv->theming_engine, context);
4196 engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
4202 * gtk_render_activity:
4203 * @context: a #GtkStyleContext
4205 * @x: X origin of the rectangle
4206 * @y: Y origin of the rectangle
4207 * @width: rectangle width
4208 * @height: rectangle height
4210 * Renders an activity area (Such as in #GtkSpinner or the
4211 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4212 * determines whether there is activity going on.
4217 gtk_render_activity (GtkStyleContext *context,
4224 GtkStyleContextPrivate *priv;
4225 GtkThemingEngineClass *engine_class;
4227 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4228 g_return_if_fail (cr != NULL);
4230 if (width <= 0 || height <= 0)
4233 priv = context->priv;
4234 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4238 store_animation_region (context, x, y, width, height);
4240 _gtk_theming_engine_set_context (priv->theming_engine, context);
4241 engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
4247 * gtk_render_icon_pixbuf:
4248 * @context: a #GtkStyleContext
4249 * @source: the #GtkIconSource specifying the icon to render
4250 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4251 * means render at the size of the source and don't scale.
4253 * Renders the icon specified by @source at the given @size, returning the result
4256 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4261 gtk_render_icon_pixbuf (GtkStyleContext *context,
4262 const GtkIconSource *source,
4265 GtkStyleContextPrivate *priv;
4266 GtkThemingEngineClass *engine_class;
4268 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4269 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4270 g_return_val_if_fail (source != NULL, NULL);
4272 priv = context->priv;
4273 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4275 _gtk_theming_engine_set_context (priv->theming_engine, context);
4276 return engine_class->render_icon_pixbuf (priv->theming_engine, source, size);
4281 * @context: a #GtkStyleContext
4283 * @pixbuf: a #GdkPixbuf containing the icon to draw
4284 * @x: X position for the @pixbuf
4285 * @y: Y position for the @pixbuf
4287 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4292 gtk_render_icon (GtkStyleContext *context,
4298 GtkStyleContextPrivate *priv;
4299 GtkThemingEngineClass *engine_class;
4301 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4302 g_return_if_fail (cr != NULL);
4304 priv = context->priv;
4305 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4309 store_animation_region (context,
4311 gdk_pixbuf_get_width (pixbuf),
4312 gdk_pixbuf_get_height (pixbuf));
4314 _gtk_theming_engine_set_context (priv->theming_engine, context);
4315 engine_class->render_icon (priv->theming_engine, cr, pixbuf, x, y);
4321 draw_insertion_cursor (GtkStyleContext *context,
4326 gboolean is_primary,
4327 PangoDirection direction,
4328 gboolean draw_arrow)
4331 GdkRGBA primary_color;
4332 GdkRGBA secondary_color;
4333 gfloat cursor_aspect_ratio;
4339 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4340 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4342 /* When changing the shape or size of the cursor here,
4343 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4346 gtk_style_context_get_style (context,
4347 "cursor-aspect-ratio", &cursor_aspect_ratio,
4350 stem_width = height * cursor_aspect_ratio + 1;
4352 /* put (stem_width % 2) on the proper side of the cursor */
4353 if (direction == PANGO_DIRECTION_LTR)
4354 offset = stem_width / 2;
4356 offset = stem_width - stem_width / 2;
4358 cairo_rectangle (cr, x - offset, y, stem_width, height);
4366 arrow_width = stem_width + 1;
4368 if (direction == PANGO_DIRECTION_RTL)
4370 ax = x - offset - 1;
4371 ay = y + height - arrow_width * 2 - arrow_width + 1;
4373 cairo_move_to (cr, ax, ay + 1);
4374 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4375 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4378 else if (direction == PANGO_DIRECTION_LTR)
4380 ax = x + stem_width - offset;
4381 ay = y + height - arrow_width * 2 - arrow_width + 1;
4383 cairo_move_to (cr, ax, ay + 1);
4384 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4385 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4389 g_assert_not_reached();
4396 * gtk_render_insertion_cursor:
4397 * @context: a #GtkStyleContext
4401 * @layout: the #PangoLayout of the text
4402 * @index: the index in the #PangoLayout
4403 * @direction: the #PangoDirection of the text
4405 * Draws a text caret on @cr at the specified index of @layout.
4410 gtk_render_insertion_cursor (GtkStyleContext *context,
4414 PangoLayout *layout,
4416 PangoDirection direction)
4418 GtkStyleContextPrivate *priv;
4419 gboolean split_cursor;
4420 PangoRectangle strong_pos, weak_pos;
4421 PangoRectangle *cursor1, *cursor2;
4422 PangoDirection keymap_direction;
4423 PangoDirection direction2;
4425 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4426 g_return_if_fail (cr != NULL);
4427 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4428 g_return_if_fail (index >= 0);
4430 priv = context->priv;
4432 g_object_get (gtk_settings_get_for_screen (priv->screen),
4433 "gtk-split-cursor", &split_cursor,
4436 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4438 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4440 direction2 = PANGO_DIRECTION_NEUTRAL;
4444 cursor1 = &strong_pos;
4446 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4448 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4449 cursor2 = &weak_pos;
4454 if (keymap_direction == direction)
4455 cursor1 = &strong_pos;
4457 cursor1 = &weak_pos;
4460 draw_insertion_cursor (context,
4462 x + PANGO_PIXELS (cursor1->x),
4463 y + PANGO_PIXELS (cursor1->y),
4464 PANGO_PIXELS (cursor1->height),
4467 direction2 != PANGO_DIRECTION_NEUTRAL);
4469 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4471 draw_insertion_cursor (context,
4473 x + PANGO_PIXELS (cursor2->x),
4474 y + PANGO_PIXELS (cursor2->y),
4475 PANGO_PIXELS (cursor2->height),
4483 * gtk_draw_insertion_cursor:
4484 * @widget: a #GtkWidget
4485 * @cr: cairo context to draw to
4486 * @location: location where to draw the cursor (@location->width is ignored)
4487 * @is_primary: if the cursor should be the primary cursor color.
4488 * @direction: whether the cursor is left-to-right or
4489 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4490 * @draw_arrow: %TRUE to draw a directional arrow on the
4491 * cursor. Should be %FALSE unless the cursor is split.
4493 * Draws a text caret on @cr at @location. This is not a style function
4494 * but merely a convenience function for drawing the standard cursor shape.
4497 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4500 gtk_draw_insertion_cursor (GtkWidget *widget,
4502 const GdkRectangle *location,
4503 gboolean is_primary,
4504 GtkTextDirection direction,
4505 gboolean draw_arrow)
4507 GtkStyleContext *context;
4509 g_return_if_fail (GTK_IS_WIDGET (widget));
4510 g_return_if_fail (cr != NULL);
4511 g_return_if_fail (location != NULL);
4512 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4514 context = gtk_widget_get_style_context (widget);
4516 draw_insertion_cursor (context, cr,
4517 location->x, location->y, location->height,
4519 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4523 static AtkAttributeSet *
4524 add_attribute (AtkAttributeSet *attributes,
4525 AtkTextAttribute attr,
4530 at = g_new (AtkAttribute, 1);
4531 at->name = g_strdup (atk_text_attribute_get_name (attr));
4532 at->value = g_strdup (value);
4534 return g_slist_prepend (attributes, at);
4538 * _gtk_style_context_get_attributes:
4539 * @attributes: a #AtkAttributeSet to add attributes to
4540 * @context: the #GtkStyleContext to get attributes from
4541 * @flags: the state to use with @context
4543 * Adds the foreground and background color from @context to
4544 * @attributes, after translating them to ATK attributes.
4546 * This is a convenience function that can be used in
4547 * implementing the #AtkText interface in widgets.
4549 * Returns: the modified #AtkAttributeSet
4552 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4553 GtkStyleContext *context,
4554 GtkStateFlags flags)
4559 gtk_style_context_get_background_color (context, flags, &color);
4560 value = g_strdup_printf ("%u,%u,%u",
4561 (guint) ceil (color.red * 65536 - color.red),
4562 (guint) ceil (color.green * 65536 - color.green),
4563 (guint) ceil (color.blue * 65536 - color.blue));
4564 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4567 gtk_style_context_get_color (context, flags, &color);
4568 value = g_strdup_printf ("%u,%u,%u",
4569 (guint) ceil (color.red * 65536 - color.red),
4570 (guint) ceil (color.green * 65536 - color.green),
4571 (guint) ceil (color.blue * 65536 - color.blue));
4572 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);