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 /* When these change we do a full restyling. Otherwise we try to figure out
305 * if we need to change things. */
306 #define GTK_STYLE_CONTEXT_RADICAL_CHANGE (GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_CLASS)
307 /* When these change we don't clear the cache. This takes more memory but makes
308 * things go faster. */
309 #define GTK_STYLE_CONTEXT_CACHED_CHANGE (GTK_CSS_CHANGE_STATE)
311 typedef struct GtkStyleInfo GtkStyleInfo;
312 typedef struct GtkRegion GtkRegion;
313 typedef struct PropertyValue PropertyValue;
314 typedef struct AnimationInfo AnimationInfo;
315 typedef struct StyleData StyleData;
320 GtkRegionFlags flags;
333 GArray *style_classes;
335 GtkJunctionSides junction_sides;
336 GtkStateFlags state_flags;
341 GtkCssComputedValues *store;
342 GArray *property_cache;
347 GtkTimeline *timeline;
351 /* Region stack (until region_id) at the time of
352 * rendering, this is used for nested cancellation.
354 GSList *parent_regions;
358 gboolean target_value;
360 cairo_region_t *invalidation_region;
364 struct _GtkStyleContextPrivate
368 GtkStyleCascade *cascade;
370 GtkStyleContext *parent;
373 GtkWidgetPath *widget_path;
374 GHashTable *style_data;
376 StyleData *current_data;
377 GtkStateFlags current_state;
379 GSList *animation_regions;
382 GtkThemingEngine *theming_engine;
384 GtkTextDirection direction;
386 GtkCssChange relevant_changes;
387 GtkCssChange pending_changes;
389 guint animations_invalidated : 1;
390 guint invalidating_context : 1;
406 static guint signals[LAST_SIGNAL] = { 0 };
408 static void gtk_style_context_finalize (GObject *object);
410 static void gtk_style_context_impl_set_property (GObject *object,
414 static void gtk_style_context_impl_get_property (GObject *object,
418 static GtkSymbolicColor *
419 gtk_style_context_color_lookup_func (gpointer contextp,
423 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
426 gtk_style_context_real_changed (GtkStyleContext *context)
428 GtkStyleContextPrivate *priv = context->priv;
431 _gtk_widget_style_context_invalidated (priv->widget);
435 gtk_style_context_class_init (GtkStyleContextClass *klass)
437 GObjectClass *object_class = G_OBJECT_CLASS (klass);
439 object_class->finalize = gtk_style_context_finalize;
440 object_class->set_property = gtk_style_context_impl_set_property;
441 object_class->get_property = gtk_style_context_impl_get_property;
443 klass->changed = gtk_style_context_real_changed;
446 g_signal_new (I_("changed"),
447 G_TYPE_FROM_CLASS (object_class),
449 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
451 g_cclosure_marshal_VOID__VOID,
454 g_object_class_install_property (object_class,
456 g_param_spec_object ("screen",
458 P_("The associated GdkScreen"),
460 GTK_PARAM_READWRITE));
461 g_object_class_install_property (object_class,
463 g_param_spec_enum ("direction",
465 P_("Text direction"),
466 GTK_TYPE_TEXT_DIRECTION,
468 GTK_PARAM_READWRITE));
470 * GtkStyleContext:parent:
472 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
477 g_object_class_install_property (object_class,
479 g_param_spec_object ("parent",
481 P_("The parent style context"),
482 GTK_TYPE_STYLE_CONTEXT,
483 GTK_PARAM_READWRITE));
485 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
488 static GtkStyleInfo *
489 style_info_new (void)
493 info = g_slice_new0 (GtkStyleInfo);
494 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
495 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
501 style_info_free (GtkStyleInfo *info)
503 g_array_free (info->style_classes, TRUE);
504 g_array_free (info->regions, TRUE);
505 g_slice_free (GtkStyleInfo, info);
508 static GtkStyleInfo *
509 style_info_copy (const GtkStyleInfo *info)
513 copy = style_info_new ();
514 g_array_insert_vals (copy->style_classes, 0,
515 info->style_classes->data,
516 info->style_classes->len);
518 g_array_insert_vals (copy->regions, 0,
522 copy->junction_sides = info->junction_sides;
523 copy->state_flags = info->state_flags;
529 style_info_hash (gconstpointer elem)
531 const GtkStyleInfo *info;
536 for (i = 0; i < info->style_classes->len; i++)
538 hash += g_array_index (info->style_classes, GQuark, i);
542 for (i = 0; i < info->regions->len; i++)
546 region = &g_array_index (info->regions, GtkRegion, i);
547 hash += region->class_quark;
548 hash += region->flags;
552 return hash ^ info->state_flags;
556 style_info_equal (gconstpointer elem1,
559 const GtkStyleInfo *info1, *info2;
564 if (info1->state_flags != info2->state_flags)
567 if (info1->junction_sides != info2->junction_sides)
570 if (info1->style_classes->len != info2->style_classes->len)
573 if (memcmp (info1->style_classes->data,
574 info2->style_classes->data,
575 info1->style_classes->len * sizeof (GQuark)) != 0)
578 if (info1->regions->len != info2->regions->len)
581 if (memcmp (info1->regions->data,
582 info2->regions->data,
583 info1->regions->len * sizeof (GtkRegion)) != 0)
590 style_data_new (void)
594 data = g_slice_new0 (StyleData);
600 clear_property_cache (StyleData *data)
604 if (!data->property_cache)
607 for (i = 0; i < data->property_cache->len; i++)
609 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
611 g_param_spec_unref (node->pspec);
612 g_value_unset (&node->value);
615 g_array_free (data->property_cache, TRUE);
616 data->property_cache = NULL;
620 style_data_free (StyleData *data)
622 g_object_unref (data->store);
623 clear_property_cache (data);
625 g_slice_free (StyleData, data);
629 gtk_style_context_init (GtkStyleContext *style_context)
631 GtkStyleContextPrivate *priv;
634 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
635 GTK_TYPE_STYLE_CONTEXT,
636 GtkStyleContextPrivate);
638 priv->style_data = g_hash_table_new_full (style_info_hash,
640 (GDestroyNotify) style_info_free,
641 (GDestroyNotify) style_data_free);
642 priv->theming_engine = g_object_ref ((gpointer) gtk_theming_engine_load (NULL));
644 priv->direction = GTK_TEXT_DIR_LTR;
646 priv->screen = gdk_screen_get_default ();
647 priv->cascade = _gtk_style_cascade_get_for_screen (priv->screen);
648 g_object_ref (priv->cascade);
649 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
651 /* Create default info store */
652 info = style_info_new ();
653 priv->info_stack = g_slist_prepend (priv->info_stack, info);
657 animation_info_free (AnimationInfo *info)
659 g_object_unref (info->timeline);
660 g_object_unref (info->window);
662 if (info->invalidation_region)
663 cairo_region_destroy (info->invalidation_region);
665 g_array_free (info->rectangles, TRUE);
666 g_slist_free (info->parent_regions);
667 g_slice_free (AnimationInfo, info);
670 static AnimationInfo *
671 animation_info_lookup_by_timeline (GtkStyleContext *context,
672 GtkTimeline *timeline)
674 GtkStyleContextPrivate *priv;
678 priv = context->priv;
680 for (l = priv->animations; l; l = l->next)
684 if (info->timeline == timeline)
692 timeline_frame_cb (GtkTimeline *timeline,
696 GtkStyleContextPrivate *priv;
697 GtkStyleContext *context;
701 priv = context->priv;
702 info = animation_info_lookup_by_timeline (context, timeline);
704 g_assert (info != NULL);
706 /* Cancel transition if window is gone */
707 if (gdk_window_is_destroyed (info->window) ||
708 !gdk_window_is_visible (info->window))
710 priv->animations = g_slist_remove (priv->animations, info);
711 animation_info_free (info);
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);
723 timeline_finished_cb (GtkTimeline *timeline,
726 GtkStyleContextPrivate *priv;
727 GtkStyleContext *context;
731 priv = context->priv;
732 info = animation_info_lookup_by_timeline (context, timeline);
734 g_assert (info != NULL);
736 priv->animations = g_slist_remove (priv->animations, info);
738 /* Invalidate one last time the area, so the final content is painted */
739 if (info->invalidation_region &&
740 !cairo_region_is_empty (info->invalidation_region))
741 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
743 gdk_window_invalidate_rect (info->window, NULL, TRUE);
745 animation_info_free (info);
748 static AnimationInfo *
749 animation_info_new (GtkStyleContext *context,
752 GtkTimelineProgressType progress_type,
755 gboolean target_value,
760 info = g_slice_new0 (AnimationInfo);
762 info->rectangles = g_array_new (FALSE, FALSE, sizeof (cairo_rectangle_int_t));
763 info->timeline = _gtk_timeline_new (duration);
764 info->window = g_object_ref (window);
766 info->target_value = target_value;
767 info->region_id = region_id;
769 _gtk_timeline_set_progress_type (info->timeline, progress_type);
770 _gtk_timeline_set_loop (info->timeline, loop);
772 if (!loop && !target_value)
774 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
775 _gtk_timeline_rewind (info->timeline);
778 g_signal_connect (info->timeline, "frame",
779 G_CALLBACK (timeline_frame_cb), context);
780 g_signal_connect (info->timeline, "finished",
781 G_CALLBACK (timeline_finished_cb), context);
783 _gtk_timeline_start (info->timeline);
788 static AnimationInfo *
789 animation_info_lookup (GtkStyleContext *context,
793 GtkStyleContextPrivate *priv;
796 priv = context->priv;
798 for (l = priv->animations; l; l = l->next)
804 if (info->state == state &&
805 info->region_id == region_id)
813 gtk_style_context_finalize (GObject *object)
815 GtkStyleContextPrivate *priv;
816 GtkStyleContext *style_context;
819 style_context = GTK_STYLE_CONTEXT (object);
820 priv = style_context->priv;
822 /* children hold a reference to us */
823 g_assert (priv->children == NULL);
825 gtk_style_context_set_parent (style_context, NULL);
827 if (priv->widget_path)
828 gtk_widget_path_free (priv->widget_path);
830 g_hash_table_destroy (priv->style_data);
832 g_object_unref (priv->cascade);
834 g_slist_free_full (priv->info_stack, (GDestroyNotify) style_info_free);
836 g_slist_free (priv->animation_regions);
838 for (l = priv->animations; l; l = l->next)
839 animation_info_free ((AnimationInfo *) l->data);
841 g_slist_free (priv->animations);
843 if (priv->theming_engine)
844 g_object_unref (priv->theming_engine);
846 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
850 gtk_style_context_impl_set_property (GObject *object,
855 GtkStyleContext *style_context;
857 style_context = GTK_STYLE_CONTEXT (object);
862 gtk_style_context_set_screen (style_context,
863 g_value_get_object (value));
866 gtk_style_context_set_direction (style_context,
867 g_value_get_enum (value));
870 gtk_style_context_set_parent (style_context,
871 g_value_get_object (value));
874 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
880 gtk_style_context_impl_get_property (GObject *object,
885 GtkStyleContext *style_context;
886 GtkStyleContextPrivate *priv;
888 style_context = GTK_STYLE_CONTEXT (object);
889 priv = style_context->priv;
894 g_value_set_object (value, priv->screen);
897 g_value_set_enum (value, priv->direction);
900 g_value_set_object (value, priv->parent);
903 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
909 build_properties (GtkStyleContext *context,
910 StyleData *style_data,
914 GtkStyleContextPrivate *priv;
915 GtkCssMatcher matcher;
916 GtkCssLookup *lookup;
918 priv = context->priv;
920 _gtk_css_matcher_init (&matcher, path, state);
921 lookup = _gtk_css_lookup_new ();
923 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
927 style_data->store = _gtk_css_computed_values_new ();
928 _gtk_css_lookup_resolve (lookup, context, style_data->store);
929 _gtk_css_lookup_free (lookup);
932 static GtkWidgetPath *
933 create_query_path (GtkStyleContext *context)
935 GtkStyleContextPrivate *priv;
940 priv = context->priv;
941 path = gtk_widget_path_copy (priv->widget ? gtk_widget_get_path (priv->widget) : priv->widget_path);
942 pos = gtk_widget_path_length (path) - 1;
944 info = priv->info_stack->data;
946 /* Set widget regions */
947 for (i = 0; i < info->regions->len; i++)
951 region = &g_array_index (info->regions, GtkRegion, i);
952 gtk_widget_path_iter_add_region (path, pos,
953 g_quark_to_string (region->class_quark),
957 /* Set widget classes */
958 for (i = 0; i < info->style_classes->len; i++)
962 quark = g_array_index (info->style_classes, GQuark, i);
963 gtk_widget_path_iter_add_class (path, pos,
964 g_quark_to_string (quark));
971 style_data_lookup (GtkStyleContext *context,
974 GtkStyleContextPrivate *priv;
976 gboolean state_mismatch;
979 priv = context->priv;
980 state_mismatch = ((GtkStyleInfo *) priv->info_stack->data)->state_flags != state;
982 /* Current data in use is cached, just return it */
983 if (priv->current_data && priv->current_state == state)
984 return priv->current_data;
986 g_assert (priv->widget != NULL || priv->widget_path != NULL);
988 if (G_UNLIKELY (state_mismatch))
990 gtk_style_context_save (context);
991 gtk_style_context_set_state (context, state);
994 priv->current_data = g_hash_table_lookup (priv->style_data, priv->info_stack->data);
995 priv->current_state = state;
997 if (!priv->current_data)
1001 path = create_query_path (context);
1003 priv->current_data = style_data_new ();
1004 g_hash_table_insert (priv->style_data,
1005 style_info_copy (priv->info_stack->data),
1006 priv->current_data);
1008 build_properties (context, priv->current_data, path, state);
1010 gtk_widget_path_free (path);
1013 data = priv->current_data;
1015 if (priv->theming_engine)
1016 g_object_unref (priv->theming_engine);
1018 v = _gtk_css_computed_values_get_value_by_name (priv->current_data->store, "engine");
1020 priv->theming_engine = _gtk_css_value_dup_object (v);
1022 priv->theming_engine = g_object_ref (gtk_theming_engine_load (NULL));
1024 if (G_UNLIKELY (state_mismatch))
1025 gtk_style_context_restore (context);
1031 gtk_style_context_set_invalid (GtkStyleContext *context,
1034 GtkStyleContextPrivate *priv;
1036 priv = context->priv;
1038 if (priv->invalid == invalid)
1041 priv->invalid = invalid;
1046 gtk_widget_queue_resize (priv->widget);
1048 gtk_style_context_set_invalid (priv->parent, TRUE);
1052 /* returns TRUE if someone called gtk_style_context_save() but hasn't
1053 * called gtk_style_context_restore() yet.
1054 * In those situations we don't invalidate the context when somebody
1055 * changes state/regions/classes.
1058 gtk_style_context_is_saved (GtkStyleContext *context)
1060 return context->priv->info_stack->next != NULL;
1064 gtk_style_context_queue_invalidate_internal (GtkStyleContext *context,
1065 GtkCssChange change)
1067 GtkStyleContextPrivate *priv = context->priv;
1069 priv->current_data = NULL;
1071 if (!gtk_style_context_is_saved (context))
1073 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_STATE);
1074 /* XXX: We need to invalidate siblings here somehow */
1079 * gtk_style_context_new:
1081 * Creates a standalone #GtkStyleContext, this style context
1082 * won't be attached to any widget, so you may want
1083 * to call gtk_style_context_set_path() yourself.
1086 * This function is only useful when using the theming layer
1087 * separated from GTK+, if you are using #GtkStyleContext to
1088 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1089 * in order to get a style context ready to theme the widget.
1092 * Returns: A newly created #GtkStyleContext.
1095 gtk_style_context_new (void)
1097 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1101 _gtk_style_context_set_widget (GtkStyleContext *context,
1104 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1105 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1107 context->priv->widget = widget;
1109 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_SELF);
1113 * gtk_style_context_add_provider:
1114 * @context: a #GtkStyleContext
1115 * @provider: a #GtkStyleProvider
1116 * @priority: the priority of the style provider. The lower
1117 * it is, the earlier it will be used in the style
1118 * construction. Typically this will be in the range
1119 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1120 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1122 * Adds a style provider to @context, to be used in style construction.
1124 * <note><para>If both priorities are the same, A #GtkStyleProvider
1125 * added through this function takes precedence over another added
1126 * through gtk_style_context_add_provider_for_screen().</para></note>
1131 gtk_style_context_add_provider (GtkStyleContext *context,
1132 GtkStyleProvider *provider,
1135 GtkStyleContextPrivate *priv;
1137 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1138 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1140 priv = context->priv;
1142 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1144 GtkStyleCascade *new_cascade;
1146 new_cascade = _gtk_style_cascade_new ();
1147 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1148 g_object_unref (priv->cascade);
1149 priv->cascade = new_cascade;
1152 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1154 gtk_style_context_invalidate (context);
1158 * gtk_style_context_remove_provider:
1159 * @context: a #GtkStyleContext
1160 * @provider: a #GtkStyleProvider
1162 * Removes @provider from the style providers list in @context.
1167 gtk_style_context_remove_provider (GtkStyleContext *context,
1168 GtkStyleProvider *provider)
1170 GtkStyleContextPrivate *priv;
1172 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1173 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1175 priv = context->priv;
1177 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1180 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1184 * gtk_style_context_reset_widgets:
1185 * @screen: a #GdkScreen
1187 * This function recomputes the styles for all widgets under a particular
1188 * #GdkScreen. This is useful when some global parameter has changed that
1189 * affects the appearance of all widgets, because when a widget gets a new
1190 * style, it will both redraw and recompute any cached information about
1191 * its appearance. As an example, it is used when the color scheme changes
1192 * in the related #GtkSettings object.
1197 gtk_style_context_reset_widgets (GdkScreen *screen)
1199 GList *list, *toplevels;
1201 _gtk_icon_set_invalidate_caches ();
1203 toplevels = gtk_window_list_toplevels ();
1204 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1206 for (list = toplevels; list; list = list->next)
1208 if (gtk_widget_get_screen (list->data) == screen)
1209 gtk_widget_reset_style (list->data);
1211 g_object_unref (list->data);
1214 g_list_free (toplevels);
1218 * gtk_style_context_add_provider_for_screen:
1219 * @screen: a #GdkScreen
1220 * @provider: a #GtkStyleProvider
1221 * @priority: the priority of the style provider. The lower
1222 * it is, the earlier it will be used in the style
1223 * construction. Typically this will be in the range
1224 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1225 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1227 * Adds a global style provider to @screen, which will be used
1228 * in style construction for all #GtkStyleContext<!-- -->s under
1231 * GTK+ uses this to make styling information from #GtkSettings
1234 * <note><para>If both priorities are the same, A #GtkStyleProvider
1235 * added through gtk_style_context_add_provider() takes precedence
1236 * over another added through this function.</para></note>
1241 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1242 GtkStyleProvider *provider,
1245 GtkStyleCascade *cascade;
1247 g_return_if_fail (GDK_IS_SCREEN (screen));
1248 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1250 cascade = _gtk_style_cascade_get_for_screen (screen);
1251 _gtk_style_cascade_add_provider (cascade, provider, priority);
1255 * gtk_style_context_remove_provider_for_screen:
1256 * @screen: a #GdkScreen
1257 * @provider: a #GtkStyleProvider
1259 * Removes @provider from the global style providers list in @screen.
1264 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1265 GtkStyleProvider *provider)
1267 GtkStyleCascade *cascade;
1269 g_return_if_fail (GDK_IS_SCREEN (screen));
1270 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1272 cascade = _gtk_style_cascade_get_for_screen (screen);
1273 _gtk_style_cascade_remove_provider (cascade, provider);
1277 * gtk_style_context_get_section:
1278 * @context: a #GtkStyleContext
1279 * @property: style property name
1281 * Queries the location in the CSS where @property was defined for the
1282 * current @context. Note that the state to be queried is taken from
1283 * gtk_style_context_get_state().
1285 * If the location is not available, %NULL will be returned. The
1286 * location might not be available for various reasons, such as the
1287 * property being overridden, @property not naming a supported CSS
1288 * property or tracking of definitions being disabled for performance
1291 * Shorthand CSS properties cannot be queried for a location and will
1292 * always return %NULL.
1294 * Returns: %NULL or the section where value was defined
1297 gtk_style_context_get_section (GtkStyleContext *context,
1298 const gchar *property)
1300 GtkStyleContextPrivate *priv;
1301 GtkStyleProperty *prop;
1304 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1305 g_return_val_if_fail (property != NULL, NULL);
1307 priv = context->priv;
1308 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1310 prop = _gtk_style_property_lookup (property);
1311 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1314 data = style_data_lookup (context, gtk_style_context_get_state (context));
1315 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1318 static GtkCssValue *
1319 gtk_style_context_query_func (guint id,
1322 return _gtk_css_computed_values_get_value (values, id);
1326 * gtk_style_context_get_property:
1327 * @context: a #GtkStyleContext
1328 * @property: style property name
1329 * @state: state to retrieve the property value for
1330 * @value: (out) (transfer full): return location for the style property value
1332 * Gets a style property from @context for the given state.
1334 * When @value is no longer needed, g_value_unset() must be called
1335 * to free any allocated memory.
1340 gtk_style_context_get_property (GtkStyleContext *context,
1341 const gchar *property,
1342 GtkStateFlags state,
1345 GtkStyleContextPrivate *priv;
1346 GtkStyleProperty *prop;
1349 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1350 g_return_if_fail (property != NULL);
1351 g_return_if_fail (value != NULL);
1353 priv = context->priv;
1354 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1356 prop = _gtk_style_property_lookup (property);
1359 g_warning ("Style property \"%s\" is not registered", property);
1362 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1364 g_warning ("Style property \"%s\" is not gettable", property);
1368 data = style_data_lookup (context, state);
1369 _gtk_style_property_query (prop, value, gtk_style_context_query_func, data->store);
1373 * gtk_style_context_get_valist:
1374 * @context: a #GtkStyleContext
1375 * @state: state to retrieve the property values for
1376 * @args: va_list of property name/return location pairs, followed by %NULL
1378 * Retrieves several style property values from @context for a given state.
1383 gtk_style_context_get_valist (GtkStyleContext *context,
1384 GtkStateFlags state,
1387 const gchar *property_name;
1389 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1391 property_name = va_arg (args, const gchar *);
1393 while (property_name)
1395 gchar *error = NULL;
1396 GValue value = G_VALUE_INIT;
1398 gtk_style_context_get_property (context,
1403 G_VALUE_LCOPY (&value, args, 0, &error);
1404 g_value_unset (&value);
1408 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1413 property_name = va_arg (args, const gchar *);
1418 * gtk_style_context_get:
1419 * @context: a #GtkStyleContext
1420 * @state: state to retrieve the property values for
1421 * @...: property name /return value pairs, followed by %NULL
1423 * Retrieves several style property values from @context for a
1429 gtk_style_context_get (GtkStyleContext *context,
1430 GtkStateFlags state,
1435 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1437 va_start (args, state);
1438 gtk_style_context_get_valist (context, state, args);
1443 * gtk_style_context_set_state:
1444 * @context: a #GtkStyleContext
1445 * @flags: state to represent
1447 * Sets the state to be used when rendering with any
1448 * of the gtk_render_*() functions.
1453 gtk_style_context_set_state (GtkStyleContext *context,
1454 GtkStateFlags flags)
1456 GtkStyleContextPrivate *priv;
1459 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1461 priv = context->priv;
1462 info = priv->info_stack->data;
1463 info->state_flags = flags;
1465 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_STATE);
1469 * gtk_style_context_get_state:
1470 * @context: a #GtkStyleContext
1472 * Returns the state used when rendering.
1474 * Returns: the state flags
1479 gtk_style_context_get_state (GtkStyleContext *context)
1481 GtkStyleContextPrivate *priv;
1484 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1486 priv = context->priv;
1487 info = priv->info_stack->data;
1489 return info->state_flags;
1493 context_has_animatable_region (GtkStyleContext *context,
1496 GtkStyleContextPrivate *priv;
1498 /* NULL region_id means everything
1499 * rendered through the style context
1504 priv = context->priv;
1505 return g_slist_find (priv->animation_regions, region_id) != NULL;
1509 * gtk_style_context_state_is_running:
1510 * @context: a #GtkStyleContext
1511 * @state: a widget state
1512 * @progress: (out): return location for the transition progress
1514 * Returns %TRUE if there is a transition animation running for the
1515 * current region (see gtk_style_context_push_animatable_region()).
1517 * If @progress is not %NULL, the animation progress will be returned
1518 * there, 0.0 means the state is closest to being unset, while 1.0 means
1519 * it's closest to being set. This means transition animation will
1520 * run from 0 to 1 when @state is being set and from 1 to 0 when
1523 * Returns: %TRUE if there is a running transition animation for @state.
1528 gtk_style_context_state_is_running (GtkStyleContext *context,
1532 GtkStyleContextPrivate *priv;
1533 AnimationInfo *info;
1536 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1538 priv = context->priv;
1540 for (l = priv->animations; l; l = l->next)
1544 if (info->state == state &&
1545 context_has_animatable_region (context, info->region_id))
1548 *progress = _gtk_timeline_get_progress (info->timeline);
1558 * gtk_style_context_set_path:
1559 * @context: a #GtkStyleContext
1560 * @path: a #GtkWidgetPath
1562 * Sets the #GtkWidgetPath used for style matching. As a
1563 * consequence, the style will be regenerated to match
1564 * the new given path.
1566 * If you are using a #GtkStyleContext returned from
1567 * gtk_widget_get_style_context(), you do not need to call
1573 gtk_style_context_set_path (GtkStyleContext *context,
1574 GtkWidgetPath *path)
1576 GtkStyleContextPrivate *priv;
1578 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1579 g_return_if_fail (path != NULL);
1581 priv = context->priv;
1582 g_return_if_fail (priv->widget == NULL);
1584 if (priv->widget_path)
1586 gtk_widget_path_free (priv->widget_path);
1587 priv->widget_path = NULL;
1591 priv->widget_path = gtk_widget_path_copy (path);
1593 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY);
1597 * gtk_style_context_get_path:
1598 * @context: a #GtkStyleContext
1600 * Returns the widget path used for style matching.
1602 * Returns: (transfer none): A #GtkWidgetPath
1606 const GtkWidgetPath *
1607 gtk_style_context_get_path (GtkStyleContext *context)
1609 GtkStyleContextPrivate *priv;
1611 priv = context->priv;
1613 return gtk_widget_get_path (priv->widget);
1615 return priv->widget_path;
1619 * gtk_style_context_set_parent:
1620 * @context: a #GtkStyleContext
1621 * @parent: (allow-none): the new parent or %NULL
1623 * Sets the parent style context for @context. The parent style
1624 * context is used to implement
1625 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1628 * If you are using a #GtkStyleContext returned from
1629 * gtk_widget_get_style_context(), the parent will be set for you.
1634 gtk_style_context_set_parent (GtkStyleContext *context,
1635 GtkStyleContext *parent)
1637 GtkStyleContextPrivate *priv;
1639 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1640 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1642 priv = context->priv;
1644 if (priv->parent == parent)
1649 parent->priv->children = g_slist_prepend (parent->priv->children, context);
1650 g_object_ref (parent);
1652 gtk_style_context_set_invalid (parent, TRUE);
1657 priv->parent->priv->children = g_slist_remove (priv->parent->priv->children, context);
1658 g_object_unref (priv->parent);
1661 priv->parent = parent;
1663 g_object_notify (G_OBJECT (context), "parent");
1664 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
1668 * gtk_style_context_get_parent:
1669 * @context: a #GtkStyleContext
1671 * Gets the parent context set via gtk_style_context_set_parent().
1672 * See that function for details.
1674 * Returns: (transfer none): the parent context or %NULL
1679 gtk_style_context_get_parent (GtkStyleContext *context)
1681 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1683 return context->priv->parent;
1687 * gtk_style_context_save:
1688 * @context: a #GtkStyleContext
1690 * Saves the @context state, so all modifications done through
1691 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1692 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1693 * or gtk_style_context_set_junction_sides() can be reverted in one
1694 * go through gtk_style_context_restore().
1699 gtk_style_context_save (GtkStyleContext *context)
1701 GtkStyleContextPrivate *priv;
1704 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1706 priv = context->priv;
1708 g_assert (priv->info_stack != NULL);
1710 info = style_info_copy (priv->info_stack->data);
1711 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1715 * gtk_style_context_restore:
1716 * @context: a #GtkStyleContext
1718 * Restores @context state to a previous stage.
1719 * See gtk_style_context_save().
1724 gtk_style_context_restore (GtkStyleContext *context)
1726 GtkStyleContextPrivate *priv;
1729 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1731 priv = context->priv;
1733 if (priv->info_stack)
1735 info = priv->info_stack->data;
1736 priv->info_stack = g_slist_remove (priv->info_stack, info);
1737 style_info_free (info);
1740 if (!priv->info_stack)
1742 g_warning ("Unpaired gtk_style_context_restore() call");
1744 /* Create default region */
1745 info = style_info_new ();
1746 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1749 priv->current_data = NULL;
1753 style_class_find (GArray *array,
1758 gboolean found = FALSE;
1764 if (!array || array->len == 0)
1768 max = array->len - 1;
1774 mid = (min + max) / 2;
1775 item = g_array_index (array, GQuark, mid);
1777 if (class_quark == item)
1782 else if (class_quark > item)
1783 min = pos = mid + 1;
1790 while (!found && min <= max);
1799 region_find (GArray *array,
1804 gboolean found = FALSE;
1810 if (!array || array->len == 0)
1814 max = array->len - 1;
1820 mid = (min + max) / 2;
1821 region = &g_array_index (array, GtkRegion, mid);
1823 if (region->class_quark == class_quark)
1828 else if (region->class_quark > class_quark)
1829 min = pos = mid + 1;
1836 while (!found && min <= max);
1845 * gtk_style_context_add_class:
1846 * @context: a #GtkStyleContext
1847 * @class_name: class name to use in styling
1849 * Adds a style class to @context, so posterior calls to
1850 * gtk_style_context_get() or any of the gtk_render_*()
1851 * functions will make use of this new class for styling.
1853 * In the CSS file format, a #GtkEntry defining an "entry"
1854 * class, would be matched by:
1857 * GtkEntry.entry { ... }
1860 * While any widget defining an "entry" class would be
1869 gtk_style_context_add_class (GtkStyleContext *context,
1870 const gchar *class_name)
1872 GtkStyleContextPrivate *priv;
1877 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1878 g_return_if_fail (class_name != NULL);
1880 priv = context->priv;
1881 class_quark = g_quark_from_string (class_name);
1883 g_assert (priv->info_stack != NULL);
1884 info = priv->info_stack->data;
1886 if (!style_class_find (info->style_classes, class_quark, &position))
1888 g_array_insert_val (info->style_classes, position, class_quark);
1890 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1895 * gtk_style_context_remove_class:
1896 * @context: a #GtkStyleContext
1897 * @class_name: class name to remove
1899 * Removes @class_name from @context.
1904 gtk_style_context_remove_class (GtkStyleContext *context,
1905 const gchar *class_name)
1907 GtkStyleContextPrivate *priv;
1912 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1913 g_return_if_fail (class_name != NULL);
1915 class_quark = g_quark_try_string (class_name);
1920 priv = context->priv;
1922 g_assert (priv->info_stack != NULL);
1923 info = priv->info_stack->data;
1925 if (style_class_find (info->style_classes, class_quark, &position))
1927 g_array_remove_index (info->style_classes, position);
1929 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1934 * gtk_style_context_has_class:
1935 * @context: a #GtkStyleContext
1936 * @class_name: a class name
1938 * Returns %TRUE if @context currently has defined the
1941 * Returns: %TRUE if @context has @class_name defined
1946 gtk_style_context_has_class (GtkStyleContext *context,
1947 const gchar *class_name)
1949 GtkStyleContextPrivate *priv;
1953 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1954 g_return_val_if_fail (class_name != NULL, FALSE);
1956 class_quark = g_quark_try_string (class_name);
1961 priv = context->priv;
1963 g_assert (priv->info_stack != NULL);
1964 info = priv->info_stack->data;
1966 if (style_class_find (info->style_classes, class_quark, NULL))
1973 * gtk_style_context_list_classes:
1974 * @context: a #GtkStyleContext
1976 * Returns the list of classes currently defined in @context.
1978 * Returns: (transfer container) (element-type utf8): a #GList of
1979 * strings with the currently defined classes. The contents
1980 * of the list are owned by GTK+, but you must free the list
1981 * itself with g_list_free() when you are done with it.
1986 gtk_style_context_list_classes (GtkStyleContext *context)
1988 GtkStyleContextPrivate *priv;
1990 GList *classes = NULL;
1993 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1995 priv = context->priv;
1997 g_assert (priv->info_stack != NULL);
1998 info = priv->info_stack->data;
2000 for (i = 0; i < info->style_classes->len; i++)
2004 quark = g_array_index (info->style_classes, GQuark, i);
2005 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
2012 * gtk_style_context_list_regions:
2013 * @context: a #GtkStyleContext
2015 * Returns the list of regions currently defined in @context.
2017 * Returns: (transfer container) (element-type utf8): a #GList of
2018 * strings with the currently defined regions. The contents
2019 * of the list are owned by GTK+, but you must free the list
2020 * itself with g_list_free() when you are done with it.
2025 gtk_style_context_list_regions (GtkStyleContext *context)
2027 GtkStyleContextPrivate *priv;
2029 GList *classes = NULL;
2032 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2034 priv = context->priv;
2036 g_assert (priv->info_stack != NULL);
2037 info = priv->info_stack->data;
2039 for (i = 0; i < info->regions->len; i++)
2042 const gchar *class_name;
2044 region = &g_array_index (info->regions, GtkRegion, i);
2046 class_name = g_quark_to_string (region->class_quark);
2047 classes = g_list_prepend (classes, (gchar *) class_name);
2054 _gtk_style_context_check_region_name (const gchar *str)
2056 g_return_val_if_fail (str != NULL, FALSE);
2058 if (!g_ascii_islower (str[0]))
2064 !g_ascii_islower (*str))
2074 * gtk_style_context_add_region:
2075 * @context: a #GtkStyleContext
2076 * @region_name: region name to use in styling
2077 * @flags: flags that apply to the region
2079 * Adds a region to @context, so posterior calls to
2080 * gtk_style_context_get() or any of the gtk_render_*()
2081 * functions will make use of this new region for styling.
2083 * In the CSS file format, a #GtkTreeView defining a "row"
2084 * region, would be matched by:
2087 * GtkTreeView row { ... }
2090 * Pseudo-classes are used for matching @flags, so the two
2093 * GtkTreeView row:nth-child(even) { ... }
2094 * GtkTreeView row:nth-child(odd) { ... }
2097 * would apply to even and odd rows, respectively.
2099 * <note><para>Region names must only contain lowercase letters
2100 * and '-', starting always with a lowercase letter.</para></note>
2105 gtk_style_context_add_region (GtkStyleContext *context,
2106 const gchar *region_name,
2107 GtkRegionFlags flags)
2109 GtkStyleContextPrivate *priv;
2111 GQuark region_quark;
2114 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2115 g_return_if_fail (region_name != NULL);
2116 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2118 priv = context->priv;
2119 region_quark = g_quark_from_string (region_name);
2121 g_assert (priv->info_stack != NULL);
2122 info = priv->info_stack->data;
2124 if (!region_find (info->regions, region_quark, &position))
2128 region.class_quark = region_quark;
2129 region.flags = flags;
2131 g_array_insert_val (info->regions, position, region);
2133 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2138 * gtk_style_context_remove_region:
2139 * @context: a #GtkStyleContext
2140 * @region_name: region name to unset
2142 * Removes a region from @context.
2147 gtk_style_context_remove_region (GtkStyleContext *context,
2148 const gchar *region_name)
2150 GtkStyleContextPrivate *priv;
2152 GQuark region_quark;
2155 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2156 g_return_if_fail (region_name != NULL);
2158 region_quark = g_quark_try_string (region_name);
2163 priv = context->priv;
2165 g_assert (priv->info_stack != NULL);
2166 info = priv->info_stack->data;
2168 if (region_find (info->regions, region_quark, &position))
2170 g_array_remove_index (info->regions, position);
2172 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2177 * gtk_style_context_has_region:
2178 * @context: a #GtkStyleContext
2179 * @region_name: a region name
2180 * @flags_return: (out) (allow-none): return location for region flags
2182 * Returns %TRUE if @context has the region defined.
2183 * If @flags_return is not %NULL, it is set to the flags
2184 * affecting the region.
2186 * Returns: %TRUE if region is defined
2191 gtk_style_context_has_region (GtkStyleContext *context,
2192 const gchar *region_name,
2193 GtkRegionFlags *flags_return)
2195 GtkStyleContextPrivate *priv;
2197 GQuark region_quark;
2200 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2201 g_return_val_if_fail (region_name != NULL, FALSE);
2206 region_quark = g_quark_try_string (region_name);
2211 priv = context->priv;
2213 g_assert (priv->info_stack != NULL);
2214 info = priv->info_stack->data;
2216 if (region_find (info->regions, region_quark, &position))
2222 region = &g_array_index (info->regions, GtkRegion, position);
2223 *flags_return = region->flags;
2232 style_property_values_cmp (gconstpointer bsearch_node1,
2233 gconstpointer bsearch_node2)
2235 const PropertyValue *val1 = bsearch_node1;
2236 const PropertyValue *val2 = bsearch_node2;
2238 if (val1->widget_type != val2->widget_type)
2239 return val1->widget_type < val2->widget_type ? -1 : 1;
2241 if (val1->pspec != val2->pspec)
2242 return val1->pspec < val2->pspec ? -1 : 1;
2244 if (val1->state != val2->state)
2245 return val1->state < val2->state ? -1 : 1;
2251 _gtk_style_context_peek_property (GtkStyleContext *context,
2252 const char *property_name)
2254 StyleData *data = style_data_lookup (context, gtk_style_context_get_state (context));
2256 return _gtk_css_computed_values_get_value_by_name (data->store, property_name);
2260 _gtk_style_context_get_number (GtkStyleContext *context,
2261 const char *property_name,
2262 double one_hundred_percent)
2266 value = _gtk_style_context_peek_property (context, property_name);
2267 return _gtk_css_number_get (_gtk_css_value_get_number (value), one_hundred_percent);
2271 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2273 GtkStateFlags state,
2276 GtkStyleContextPrivate *priv;
2277 PropertyValue *pcache, key = { 0 };
2281 priv = context->priv;
2282 data = style_data_lookup (context, state);
2284 key.widget_type = widget_type;
2288 /* need value cache array */
2289 if (!data->property_cache)
2290 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2293 pcache = bsearch (&key,
2294 data->property_cache->data, data->property_cache->len,
2295 sizeof (PropertyValue), style_property_values_cmp);
2297 return &pcache->value;
2301 while (i < data->property_cache->len &&
2302 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2305 g_array_insert_val (data->property_cache, i, key);
2306 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2308 /* cache miss, initialize value type, then set contents */
2309 g_param_spec_ref (pcache->pspec);
2310 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2312 if (priv->widget || priv->widget_path)
2314 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2315 priv->widget ? gtk_widget_get_path (priv->widget)
2316 : priv->widget_path,
2317 state, pspec, &pcache->value))
2319 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2320 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2322 GtkSymbolicColor *color;
2325 color = g_value_dup_boxed (&pcache->value);
2327 g_value_unset (&pcache->value);
2329 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2330 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2332 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2334 if (_gtk_style_context_resolve_color (context, color, &rgba))
2336 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2337 g_value_set_boxed (&pcache->value, &rgba);
2342 rgb.red = rgba.red * 65535. + 0.5;
2343 rgb.green = rgba.green * 65535. + 0.5;
2344 rgb.blue = rgba.blue * 65535. + 0.5;
2346 g_value_set_boxed (&pcache->value, &rgb);
2350 g_param_value_set_default (pspec, &pcache->value);
2352 gtk_symbolic_color_unref (color);
2355 return &pcache->value;
2359 /* not supplied by any provider, revert to default */
2360 g_param_value_set_default (pspec, &pcache->value);
2362 return &pcache->value;
2366 * gtk_style_context_get_style_property:
2367 * @context: a #GtkStyleContext
2368 * @property_name: the name of the widget style property
2369 * @value: Return location for the property value
2371 * Gets the value for a widget style property.
2373 * When @value is no longer needed, g_value_unset() must be called
2374 * to free any allocated memory.
2377 gtk_style_context_get_style_property (GtkStyleContext *context,
2378 const gchar *property_name,
2381 GtkStyleContextPrivate *priv;
2382 GtkWidgetClass *widget_class;
2383 GtkStateFlags state;
2385 const GValue *peek_value;
2388 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2389 g_return_if_fail (property_name != NULL);
2390 g_return_if_fail (value != NULL);
2392 priv = context->priv;
2396 widget_type = G_OBJECT_TYPE (priv->widget);
2400 if (!priv->widget_path)
2403 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2405 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2407 g_warning ("%s: can't get style properties for non-widget class `%s'",
2409 g_type_name (widget_type));
2414 widget_class = g_type_class_ref (widget_type);
2415 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2416 g_type_class_unref (widget_class);
2420 g_warning ("%s: widget class `%s' has no style property named `%s'",
2422 g_type_name (widget_type),
2427 state = gtk_style_context_get_state (context);
2428 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2431 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2432 g_value_copy (peek_value, value);
2433 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2434 g_value_transform (peek_value, value);
2436 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2438 G_VALUE_TYPE_NAME (peek_value),
2439 G_VALUE_TYPE_NAME (value));
2443 * gtk_style_context_get_style_valist:
2444 * @context: a #GtkStyleContext
2445 * @args: va_list of property name/return location pairs, followed by %NULL
2447 * Retrieves several widget style properties from @context according to the
2453 gtk_style_context_get_style_valist (GtkStyleContext *context,
2456 GtkStyleContextPrivate *priv;
2457 const gchar *prop_name;
2458 GtkStateFlags state;
2461 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2463 prop_name = va_arg (args, const gchar *);
2464 priv = context->priv;
2468 widget_type = G_OBJECT_TYPE (priv->widget);
2472 if (!priv->widget_path)
2475 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2477 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2479 g_warning ("%s: can't get style properties for non-widget class `%s'",
2481 g_type_name (widget_type));
2486 state = gtk_style_context_get_state (context);
2490 GtkWidgetClass *widget_class;
2492 const GValue *peek_value;
2495 widget_class = g_type_class_ref (widget_type);
2496 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2497 g_type_class_unref (widget_class);
2501 g_warning ("%s: widget class `%s' has no style property named `%s'",
2503 g_type_name (widget_type),
2508 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2511 G_VALUE_LCOPY (peek_value, args, 0, &error);
2515 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2517 G_VALUE_TYPE_NAME (peek_value),
2523 prop_name = va_arg (args, const gchar *);
2528 * gtk_style_context_get_style:
2529 * @context: a #GtkStyleContext
2530 * @...: property name /return value pairs, followed by %NULL
2532 * Retrieves several widget style properties from @context according to the
2538 gtk_style_context_get_style (GtkStyleContext *context,
2543 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2545 va_start (args, context);
2546 gtk_style_context_get_style_valist (context, args);
2552 * gtk_style_context_lookup_icon_set:
2553 * @context: a #GtkStyleContext
2554 * @stock_id: an icon name
2556 * Looks up @stock_id in the icon factories associated to @context and
2557 * the default icon factory, returning an icon set if found, otherwise
2560 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2563 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2564 const gchar *stock_id)
2566 GtkStyleContextPrivate *priv;
2568 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2569 g_return_val_if_fail (stock_id != NULL, NULL);
2571 priv = context->priv;
2572 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2574 return gtk_icon_factory_lookup_default (stock_id);
2578 * gtk_style_context_set_screen:
2579 * @context: a #GtkStyleContext
2580 * @screen: a #GdkScreen
2582 * Attaches @context to the given screen.
2584 * The screen is used to add style information from 'global' style
2585 * providers, such as the screens #GtkSettings instance.
2587 * If you are using a #GtkStyleContext returned from
2588 * gtk_widget_get_style_context(), you do not need to
2589 * call this yourself.
2594 gtk_style_context_set_screen (GtkStyleContext *context,
2597 GtkStyleContextPrivate *priv;
2599 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2600 g_return_if_fail (GDK_IS_SCREEN (screen));
2602 priv = context->priv;
2603 if (priv->screen == screen)
2606 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2608 g_object_unref (priv->cascade);
2609 priv->cascade = _gtk_style_cascade_get_for_screen (screen);
2610 g_object_ref (priv->cascade);
2614 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2617 priv->screen = screen;
2619 g_object_notify (G_OBJECT (context), "screen");
2621 gtk_style_context_invalidate (context);
2625 * gtk_style_context_get_screen:
2626 * @context: a #GtkStyleContext
2628 * Returns the #GdkScreen to which @context is attached.
2630 * Returns: (transfer none): a #GdkScreen.
2633 gtk_style_context_get_screen (GtkStyleContext *context)
2635 GtkStyleContextPrivate *priv;
2637 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2639 priv = context->priv;
2640 return priv->screen;
2644 * gtk_style_context_set_direction:
2645 * @context: a #GtkStyleContext
2646 * @direction: the new direction.
2648 * Sets the reading direction for rendering purposes.
2650 * If you are using a #GtkStyleContext returned from
2651 * gtk_widget_get_style_context(), you do not need to
2652 * call this yourself.
2657 gtk_style_context_set_direction (GtkStyleContext *context,
2658 GtkTextDirection direction)
2660 GtkStyleContextPrivate *priv;
2662 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2664 priv = context->priv;
2665 priv->direction = direction;
2667 g_object_notify (G_OBJECT (context), "direction");
2671 * gtk_style_context_get_direction:
2672 * @context: a #GtkStyleContext
2674 * Returns the widget direction used for rendering.
2676 * Returns: the widget direction
2681 gtk_style_context_get_direction (GtkStyleContext *context)
2683 GtkStyleContextPrivate *priv;
2685 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2687 priv = context->priv;
2688 return priv->direction;
2692 * gtk_style_context_set_junction_sides:
2693 * @context: a #GtkStyleContext
2694 * @sides: sides where rendered elements are visually connected to
2697 * Sets the sides where rendered elements (mostly through
2698 * gtk_render_frame()) will visually connect with other visual elements.
2700 * This is merely a hint that may or may not be honored
2701 * by theming engines.
2703 * Container widgets are expected to set junction hints as appropriate
2704 * for their children, so it should not normally be necessary to call
2705 * this function manually.
2710 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2711 GtkJunctionSides sides)
2713 GtkStyleContextPrivate *priv;
2716 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2718 priv = context->priv;
2719 info = priv->info_stack->data;
2720 info->junction_sides = sides;
2724 * gtk_style_context_get_junction_sides:
2725 * @context: a #GtkStyleContext
2727 * Returns the sides where rendered elements connect visually with others.
2729 * Returns: the junction sides
2734 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2736 GtkStyleContextPrivate *priv;
2739 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2741 priv = context->priv;
2742 info = priv->info_stack->data;
2743 return info->junction_sides;
2746 static GtkSymbolicColor *
2747 gtk_style_context_color_lookup_func (gpointer contextp,
2750 GtkStyleContext *context = contextp;
2752 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2756 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2757 GtkSymbolicColor *color)
2759 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2760 g_return_val_if_fail (color != NULL, FALSE);
2762 return _gtk_symbolic_color_resolve_full (color,
2763 gtk_style_context_color_lookup_func,
2769 _gtk_style_context_resolve_color (GtkStyleContext *context,
2770 GtkSymbolicColor *color,
2775 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2776 g_return_val_if_fail (color != NULL, FALSE);
2777 g_return_val_if_fail (result != NULL, FALSE);
2779 val = _gtk_symbolic_color_resolve_full (color,
2780 gtk_style_context_color_lookup_func,
2785 *result = *_gtk_css_value_get_rgba (val);
2786 _gtk_css_value_unref (val);
2791 * gtk_style_context_lookup_color:
2792 * @context: a #GtkStyleContext
2793 * @color_name: color name to lookup
2794 * @color: (out): Return location for the looked up color
2796 * Looks up and resolves a color name in the @context color map.
2798 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2801 gtk_style_context_lookup_color (GtkStyleContext *context,
2802 const gchar *color_name,
2805 GtkSymbolicColor *sym_color;
2807 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2808 g_return_val_if_fail (color_name != NULL, FALSE);
2809 g_return_val_if_fail (color != NULL, FALSE);
2811 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2812 if (sym_color == NULL)
2815 return _gtk_style_context_resolve_color (context, sym_color, color);
2819 * gtk_style_context_notify_state_change:
2820 * @context: a #GtkStyleContext
2821 * @window: a #GdkWindow
2822 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2823 * See gtk_style_context_push_animatable_region()
2824 * @state: state to trigger transition for
2825 * @state_value: %TRUE if @state is the state we are changing to,
2826 * %FALSE if we are changing away from it
2828 * Notifies a state change on @context, so if the current style makes use
2829 * of transition animations, one will be started so all rendered elements
2830 * under @region_id are animated for state @state being set to value
2833 * The @window parameter is used in order to invalidate the rendered area
2834 * as the animation runs, so make sure it is the same window that is being
2835 * rendered on by the gtk_render_*() functions.
2837 * If @region_id is %NULL, all rendered elements using @context will be
2838 * affected by this state transition.
2840 * As a practical example, a #GtkButton notifying a state transition on
2841 * the prelight state:
2843 * gtk_style_context_notify_state_change (context,
2844 * gtk_widget_get_window (widget),
2846 * GTK_STATE_PRELIGHT,
2847 * button->in_button);
2850 * Can be handled in the CSS file like this:
2853 * background-color: #f00
2857 * background-color: #fff;
2858 * transition: 200ms linear
2862 * This combination will animate the button background from red to white
2863 * if a pointer enters the button, and back to red if the pointer leaves
2866 * Note that @state is used when finding the transition parameters, which
2867 * is why the style places the transition under the :hover pseudo-class.
2872 gtk_style_context_notify_state_change (GtkStyleContext *context,
2876 gboolean state_value)
2878 GtkStyleContextPrivate *priv;
2879 GtkAnimationDescription *desc;
2880 AnimationInfo *info;
2881 GtkStateFlags flags;
2885 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2886 g_return_if_fail (GDK_IS_WINDOW (window));
2887 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2889 priv = context->priv;
2890 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
2892 state_value = (state_value == TRUE);
2896 case GTK_STATE_ACTIVE:
2897 flags = GTK_STATE_FLAG_ACTIVE;
2899 case GTK_STATE_PRELIGHT:
2900 flags = GTK_STATE_FLAG_PRELIGHT;
2902 case GTK_STATE_SELECTED:
2903 flags = GTK_STATE_FLAG_SELECTED;
2905 case GTK_STATE_INSENSITIVE:
2906 flags = GTK_STATE_FLAG_INSENSITIVE;
2908 case GTK_STATE_INCONSISTENT:
2909 flags = GTK_STATE_FLAG_INCONSISTENT;
2911 case GTK_STATE_FOCUSED:
2912 flags = GTK_STATE_FLAG_FOCUSED;
2914 case GTK_STATE_NORMAL:
2920 /* Find out if there is any animation description for the given
2921 * state, it will fallback to the normal state as well if necessary.
2923 data = style_data_lookup (context, flags);
2924 v = _gtk_css_computed_values_get_value_by_name (data->store, "transition");
2927 desc = _gtk_css_value_get_boxed (v);
2931 if (_gtk_animation_description_get_duration (desc) == 0)
2934 info = animation_info_lookup (context, region_id, state);
2937 info->target_value != state_value)
2939 /* Target values are the opposite */
2940 if (!_gtk_timeline_get_loop (info->timeline))
2942 /* Reverse the animation */
2943 if (_gtk_timeline_get_direction (info->timeline) == GTK_TIMELINE_DIRECTION_FORWARD)
2944 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
2946 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_FORWARD);
2948 info->target_value = state_value;
2952 /* Take it out of its looping state */
2953 _gtk_timeline_set_loop (info->timeline, FALSE);
2957 (!_gtk_animation_description_get_loop (desc) ||
2960 info = animation_info_new (context, region_id,
2961 _gtk_animation_description_get_duration (desc),
2962 _gtk_animation_description_get_progress_type (desc),
2963 _gtk_animation_description_get_loop (desc),
2964 state, state_value, window);
2966 priv->animations = g_slist_prepend (priv->animations, info);
2967 priv->animations_invalidated = TRUE;
2972 * gtk_style_context_cancel_animations:
2973 * @context: a #GtkStyleContext
2974 * @region_id: (allow-none): animatable region to stop, or %NULL.
2975 * See gtk_style_context_push_animatable_region()
2977 * Stops all running animations for @region_id and all animatable
2978 * regions underneath.
2980 * A %NULL @region_id will stop all ongoing animations in @context,
2981 * when dealing with a #GtkStyleContext obtained through
2982 * gtk_widget_get_style_context(), this is normally done for you
2983 * in all circumstances you would expect all widget to be stopped,
2984 * so this should be only used in complex widgets with different
2985 * animatable regions.
2990 gtk_style_context_cancel_animations (GtkStyleContext *context,
2993 GtkStyleContextPrivate *priv;
2994 AnimationInfo *info;
2997 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2999 priv = context->priv;
3000 l = priv->animations;
3008 info->region_id == region_id ||
3009 g_slist_find (info->parent_regions, region_id))
3011 priv->animations = g_slist_remove (priv->animations, info);
3012 animation_info_free (info);
3018 is_parent_of (GdkWindow *parent,
3021 GtkWidget *child_widget, *parent_widget;
3024 gdk_window_get_user_data (child, (gpointer *) &child_widget);
3025 gdk_window_get_user_data (parent, (gpointer *) &parent_widget);
3027 if (child_widget != parent_widget &&
3028 !gtk_widget_is_ancestor (child_widget, parent_widget))
3035 if (window == parent)
3038 window = gdk_window_get_parent (window);
3045 * gtk_style_context_scroll_animations:
3046 * @context: a #GtkStyleContext
3047 * @window: a #GdkWindow used previously in
3048 * gtk_style_context_notify_state_change()
3049 * @dx: Amount to scroll in the X axis
3050 * @dy: Amount to scroll in the Y axis
3052 * This function is analogous to gdk_window_scroll(), and
3053 * should be called together with it so the invalidation
3054 * areas for any ongoing animation are scrolled together
3060 gtk_style_context_scroll_animations (GtkStyleContext *context,
3065 GtkStyleContextPrivate *priv;
3066 AnimationInfo *info;
3069 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3070 g_return_if_fail (GDK_IS_WINDOW (window));
3072 priv = context->priv;
3073 l = priv->animations;
3080 if (info->invalidation_region &&
3081 (window == info->window ||
3082 is_parent_of (window, info->window)))
3083 cairo_region_translate (info->invalidation_region, dx, dy);
3088 * gtk_style_context_push_animatable_region:
3089 * @context: a #GtkStyleContext
3090 * @region_id: unique identifier for the animatable region
3092 * Pushes an animatable region, so all further gtk_render_*() calls between
3093 * this call and the following gtk_style_context_pop_animatable_region()
3094 * will potentially show transition animations for this region if
3095 * gtk_style_context_notify_state_change() is called for a given state,
3096 * and the current theme/style defines transition animations for state
3099 * The @region_id used must be unique in @context so the theming engine
3100 * can uniquely identify rendered elements subject to a state transition.
3105 gtk_style_context_push_animatable_region (GtkStyleContext *context,
3108 GtkStyleContextPrivate *priv;
3110 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3111 g_return_if_fail (region_id != NULL);
3113 priv = context->priv;
3114 priv->animation_regions = g_slist_prepend (priv->animation_regions, region_id);
3118 * gtk_style_context_pop_animatable_region:
3119 * @context: a #GtkStyleContext
3121 * Pops an animatable region from @context.
3122 * See gtk_style_context_push_animatable_region().
3127 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
3129 GtkStyleContextPrivate *priv;
3131 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3133 priv = context->priv;
3134 priv->animation_regions = g_slist_delete_link (priv->animation_regions,
3135 priv->animation_regions);
3139 _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context)
3141 GtkStyleContextPrivate *priv;
3144 priv = context->priv;
3146 for (l = priv->animations; l; l = l->next)
3148 AnimationInfo *info;
3152 /* A NULL invalidation region means it has to be recreated on
3153 * the next expose event, this happens usually after a widget
3154 * allocation change, so the next expose after it will update
3155 * the invalidation region.
3157 if (info->invalidation_region)
3159 cairo_region_destroy (info->invalidation_region);
3160 info->invalidation_region = NULL;
3164 priv->animations_invalidated = TRUE;
3168 _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context,
3171 GtkStyleContextPrivate *priv;
3174 priv = context->priv;
3176 if (!priv->animations_invalidated)
3179 l = priv->animations;
3183 AnimationInfo *info;
3192 if (info->invalidation_region)
3195 if (info->rectangles->len == 0)
3198 info->invalidation_region = cairo_region_create ();
3199 _gtk_widget_get_translation_to_window (widget, info->window, &rel_x, &rel_y);
3201 for (i = 0; i < info->rectangles->len; i++)
3203 cairo_rectangle_int_t *rect;
3205 rect = &g_array_index (info->rectangles, cairo_rectangle_int_t, i);
3207 /* These are widget relative coordinates,
3208 * so have them inverted to be window relative
3213 cairo_region_union_rectangle (info->invalidation_region, rect);
3216 g_array_remove_range (info->rectangles, 0, info->rectangles->len);
3219 priv->animations_invalidated = FALSE;
3223 store_animation_region (GtkStyleContext *context,
3229 GtkStyleContextPrivate *priv;
3232 priv = context->priv;
3234 if (!priv->animations_invalidated)
3237 for (l = priv->animations; l; l = l->next)
3239 AnimationInfo *info;
3243 /* The animation doesn't need updating
3244 * the invalidation area, bail out.
3246 if (info->invalidation_region)
3249 if (context_has_animatable_region (context, info->region_id))
3251 cairo_rectangle_int_t rect;
3255 rect.width = (gint) width;
3256 rect.height = (gint) height;
3258 g_array_append_val (info->rectangles, rect);
3260 if (!info->parent_regions)
3262 GSList *parent_regions;
3264 parent_regions = g_slist_find (priv->animation_regions, info->region_id);
3265 info->parent_regions = g_slist_copy (parent_regions);
3272 gtk_style_context_do_invalidate (GtkStyleContext *context,
3273 gboolean clear_caches)
3275 GtkStyleContextPrivate *priv;
3277 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3279 priv = context->priv;
3281 /* Avoid reentrancy */
3282 if (priv->invalidating_context)
3285 priv->invalidating_context = TRUE;
3288 g_hash_table_remove_all (priv->style_data);
3289 priv->current_data = NULL;
3291 g_signal_emit (context, signals[CHANGED], 0);
3293 priv->invalidating_context = FALSE;
3297 _gtk_style_context_validate (GtkStyleContext *context,
3298 GtkCssChange change)
3300 GtkStyleContextPrivate *priv;
3303 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3305 priv = context->priv;
3307 change |= priv->pending_changes;
3309 if (!priv->invalid && change == 0)
3312 priv->pending_changes = 0;
3313 gtk_style_context_set_invalid (context, FALSE);
3315 /* Try to avoid invalidating if we can */
3316 if (change & GTK_STYLE_CONTEXT_RADICAL_CHANGE)
3318 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
3322 if (priv->relevant_changes == GTK_CSS_CHANGE_ANY)
3324 GtkWidgetPath *path;
3325 GtkCssMatcher matcher;
3327 path = create_query_path (context);
3328 _gtk_css_matcher_init (&matcher, path, priv->current_state);
3330 priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3332 priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
3334 gtk_widget_path_unref (path);
3338 if (priv->relevant_changes & change)
3340 gboolean clear_cache = ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE) != 0;
3342 gtk_style_context_do_invalidate (context, clear_cache);
3345 change = _gtk_css_change_for_child (change);
3346 for (list = priv->children; list; list = list->next)
3348 _gtk_style_context_validate (list->data, change);
3353 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3354 GtkCssChange change)
3356 GtkStyleContextPrivate *priv;
3358 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3359 g_return_if_fail (change != 0);
3361 priv = context->priv;
3363 if (priv->widget == NULL && priv->widget_path == NULL)
3366 priv->pending_changes |= change;
3367 gtk_style_context_set_invalid (context, TRUE);
3371 * gtk_style_context_invalidate:
3372 * @context: a #GtkStyleContext.
3374 * Invalidates @context style information, so it will be reconstructed
3377 * If you're using a #GtkStyleContext returned from
3378 * gtk_widget_get_style_context(), you do not need to
3379 * call this yourself.
3384 gtk_style_context_invalidate (GtkStyleContext *context)
3386 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3388 gtk_style_context_do_invalidate (context, TRUE);
3392 * gtk_style_context_set_background:
3393 * @context: a #GtkStyleContext
3394 * @window: a #GdkWindow
3396 * Sets the background of @window to the background pattern or
3397 * color specified in @context for its current state.
3402 gtk_style_context_set_background (GtkStyleContext *context,
3405 GtkStateFlags state;
3406 cairo_pattern_t *pattern;
3409 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3410 g_return_if_fail (GDK_IS_WINDOW (window));
3412 state = gtk_style_context_get_state (context);
3413 gtk_style_context_get (context, state,
3414 "background-image", &pattern,
3418 gdk_window_set_background_pattern (window, pattern);
3419 cairo_pattern_destroy (pattern);
3423 gtk_style_context_get (context, state,
3424 "background-color", &color,
3428 gdk_window_set_background_rgba (window, color);
3429 gdk_rgba_free (color);
3434 * gtk_style_context_get_color:
3435 * @context: a #GtkStyleContext
3436 * @state: state to retrieve the color for
3437 * @color: (out): return value for the foreground color
3439 * Gets the foreground color for a given state.
3444 gtk_style_context_get_color (GtkStyleContext *context,
3445 GtkStateFlags state,
3450 g_return_if_fail (color != NULL);
3451 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3453 gtk_style_context_get (context,
3463 * gtk_style_context_get_background_color:
3464 * @context: a #GtkStyleContext
3465 * @state: state to retrieve the color for
3466 * @color: (out): return value for the background color
3468 * Gets the background color for a given state.
3473 gtk_style_context_get_background_color (GtkStyleContext *context,
3474 GtkStateFlags state,
3479 g_return_if_fail (color != NULL);
3480 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3482 gtk_style_context_get (context,
3484 "background-color", &c,
3492 * gtk_style_context_get_border_color:
3493 * @context: a #GtkStyleContext
3494 * @state: state to retrieve the color for
3495 * @color: (out): return value for the border color
3497 * Gets the border color for a given state.
3502 gtk_style_context_get_border_color (GtkStyleContext *context,
3503 GtkStateFlags state,
3508 g_return_if_fail (color != NULL);
3509 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3511 gtk_style_context_get (context,
3521 * gtk_style_context_get_border:
3522 * @context: a #GtkStyleContext
3523 * @state: state to retrieve the border for
3524 * @border: (out): return value for the border settings
3526 * Gets the border for a given state as a #GtkBorder.
3527 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3532 gtk_style_context_get_border (GtkStyleContext *context,
3533 GtkStateFlags state,
3536 int top, left, bottom, right;
3538 g_return_if_fail (border != NULL);
3539 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3541 gtk_style_context_get (context,
3543 "border-top-width", &top,
3544 "border-left-width", &left,
3545 "border-bottom-width", &bottom,
3546 "border-right-width", &right,
3550 border->left = left;
3551 border->bottom = bottom;
3552 border->right = right;
3556 * gtk_style_context_get_padding:
3557 * @context: a #GtkStyleContext
3558 * @state: state to retrieve the padding for
3559 * @padding: (out): return value for the padding settings
3561 * Gets the padding for a given state as a #GtkBorder.
3562 * See %GTK_STYLE_PROPERTY_PADDING.
3567 gtk_style_context_get_padding (GtkStyleContext *context,
3568 GtkStateFlags state,
3571 int top, left, bottom, right;
3573 g_return_if_fail (padding != NULL);
3574 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3576 gtk_style_context_get (context,
3578 "padding-top", &top,
3579 "padding-left", &left,
3580 "padding-bottom", &bottom,
3581 "padding-right", &right,
3585 padding->left = left;
3586 padding->bottom = bottom;
3587 padding->right = right;
3591 * gtk_style_context_get_margin:
3592 * @context: a #GtkStyleContext
3593 * @state: state to retrieve the border for
3594 * @margin: (out): return value for the margin settings
3596 * Gets the margin for a given state as a #GtkBorder.
3597 * See %GTK_STYLE_PROPERTY_MARGIN.
3602 gtk_style_context_get_margin (GtkStyleContext *context,
3603 GtkStateFlags state,
3606 int top, left, bottom, right;
3608 g_return_if_fail (margin != NULL);
3609 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3611 gtk_style_context_get (context,
3614 "margin-left", &left,
3615 "margin-bottom", &bottom,
3616 "margin-right", &right,
3620 margin->left = left;
3621 margin->bottom = bottom;
3622 margin->right = right;
3626 * gtk_style_context_get_font:
3627 * @context: a #GtkStyleContext
3628 * @state: state to retrieve the font for
3630 * Returns the font description for a given state. The returned
3631 * object is const and will remain valid until the
3632 * #GtkStyleContext::changed signal happens.
3634 * Returns: (transfer none): the #PangoFontDescription for the given
3635 * state. This object is owned by GTK+ and should not be
3640 const PangoFontDescription *
3641 gtk_style_context_get_font (GtkStyleContext *context,
3642 GtkStateFlags state)
3644 GtkStyleContextPrivate *priv;
3646 PangoFontDescription *description;
3648 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3650 priv = context->priv;
3651 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3653 data = style_data_lookup (context, state);
3655 /* Yuck, fonts are created on-demand but we don't return a ref.
3656 * Do bad things to achieve this requirement */
3657 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3658 if (description == NULL)
3660 gtk_style_context_get (context, state, "font", &description, NULL);
3661 g_object_set_data_full (G_OBJECT (data->store),
3662 "font-cache-for-get_font",
3664 (GDestroyNotify) pango_font_description_free);
3670 get_cursor_color (GtkStyleContext *context,
3674 GdkColor *style_color;
3676 gtk_style_context_get_style (context,
3677 primary ? "cursor-color" : "secondary-cursor-color",
3683 color->red = style_color->red / 65535.0;
3684 color->green = style_color->green / 65535.0;
3685 color->blue = style_color->blue / 65535.0;
3688 gdk_color_free (style_color);
3692 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3698 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3700 color->red = (color->red + bg.red) * 0.5;
3701 color->green = (color->green + bg.green) * 0.5;
3702 color->blue = (color->blue + bg.blue) * 0.5;
3708 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3709 GdkRGBA *primary_color,
3710 GdkRGBA *secondary_color)
3713 get_cursor_color (context, TRUE, primary_color);
3715 if (secondary_color)
3716 get_cursor_color (context, FALSE, secondary_color);
3723 * @context: a #GtkStyleContext
3725 * @x: X origin of the rectangle
3726 * @y: Y origin of the rectangle
3727 * @width: rectangle width
3728 * @height: rectangle height
3730 * Renders a checkmark (as in a #GtkCheckButton).
3732 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3733 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3734 * should be marked as undefined.
3737 * <title>Typical checkmark rendering</title>
3738 * <inlinegraphic fileref="checks.png" format="PNG"/>
3744 gtk_render_check (GtkStyleContext *context,
3751 GtkStyleContextPrivate *priv;
3752 GtkThemingEngineClass *engine_class;
3754 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3755 g_return_if_fail (cr != NULL);
3757 if (width <= 0 || height <= 0)
3760 priv = context->priv;
3761 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3765 store_animation_region (context, x, y, width, height);
3767 _gtk_theming_engine_set_context (priv->theming_engine, context);
3768 engine_class->render_check (priv->theming_engine, cr,
3769 x, y, width, height);
3775 * gtk_render_option:
3776 * @context: a #GtkStyleContext
3778 * @x: X origin of the rectangle
3779 * @y: Y origin of the rectangle
3780 * @width: rectangle width
3781 * @height: rectangle height
3783 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3784 * state will determine whether the option is on or off, and
3785 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3788 * <title>Typical option mark rendering</title>
3789 * <inlinegraphic fileref="options.png" format="PNG"/>
3795 gtk_render_option (GtkStyleContext *context,
3802 GtkStyleContextPrivate *priv;
3803 GtkThemingEngineClass *engine_class;
3805 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3806 g_return_if_fail (cr != NULL);
3808 if (width <= 0 || height <= 0)
3811 priv = context->priv;
3812 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3816 store_animation_region (context, x, y, width, height);
3818 _gtk_theming_engine_set_context (priv->theming_engine, context);
3819 engine_class->render_option (priv->theming_engine, cr,
3820 x, y, width, height);
3827 * @context: a #GtkStyleContext
3829 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3830 * @x: X origin of the render area
3831 * @y: Y origin of the render area
3832 * @size: square side for render area
3834 * Renders an arrow pointing to @angle.
3837 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3838 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3844 gtk_render_arrow (GtkStyleContext *context,
3851 GtkStyleContextPrivate *priv;
3852 GtkThemingEngineClass *engine_class;
3854 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3855 g_return_if_fail (cr != NULL);
3860 priv = context->priv;
3861 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3865 gtk_style_context_save (context);
3866 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3868 store_animation_region (context, x, y, size, size);
3870 _gtk_theming_engine_set_context (priv->theming_engine, context);
3871 engine_class->render_arrow (priv->theming_engine, cr,
3874 gtk_style_context_restore (context);
3879 * gtk_render_background:
3880 * @context: a #GtkStyleContext
3882 * @x: X origin of the rectangle
3883 * @y: Y origin of the rectangle
3884 * @width: rectangle width
3885 * @height: rectangle height
3887 * Renders the background of an element.
3890 * <title>Typical background rendering, showing the effect of
3891 * <parameter>background-image</parameter>,
3892 * <parameter>border-width</parameter> and
3893 * <parameter>border-radius</parameter></title>
3894 * <inlinegraphic fileref="background.png" format="PNG"/>
3900 gtk_render_background (GtkStyleContext *context,
3907 GtkStyleContextPrivate *priv;
3908 GtkThemingEngineClass *engine_class;
3910 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3911 g_return_if_fail (cr != NULL);
3913 if (width <= 0 || height <= 0)
3916 priv = context->priv;
3917 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3921 store_animation_region (context, x, y, width, height);
3923 _gtk_theming_engine_set_context (priv->theming_engine, context);
3924 engine_class->render_background (priv->theming_engine, cr, x, y, width, height);
3931 * @context: a #GtkStyleContext
3933 * @x: X origin of the rectangle
3934 * @y: Y origin of the rectangle
3935 * @width: rectangle width
3936 * @height: rectangle height
3938 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3941 * <title>Examples of frame rendering, showing the effect of
3942 * <parameter>border-image</parameter>,
3943 * <parameter>border-color</parameter>,
3944 * <parameter>border-width</parameter>,
3945 * <parameter>border-radius</parameter> and
3947 * <inlinegraphic fileref="frames.png" format="PNG"/>
3953 gtk_render_frame (GtkStyleContext *context,
3960 GtkStyleContextPrivate *priv;
3961 GtkThemingEngineClass *engine_class;
3963 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3964 g_return_if_fail (cr != NULL);
3966 if (width <= 0 || height <= 0)
3969 priv = context->priv;
3970 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3974 store_animation_region (context, x, y, width, height);
3976 _gtk_theming_engine_set_context (priv->theming_engine, context);
3977 engine_class->render_frame (priv->theming_engine, cr, x, y, width, height);
3983 * gtk_render_expander:
3984 * @context: a #GtkStyleContext
3986 * @x: X origin of the rectangle
3987 * @y: Y origin of the rectangle
3988 * @width: rectangle width
3989 * @height: rectangle height
3991 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3992 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3993 * determines whether the expander is collapsed or expanded.
3996 * <title>Typical expander rendering</title>
3997 * <inlinegraphic fileref="expanders.png" format="PNG"/>
4003 gtk_render_expander (GtkStyleContext *context,
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_expander (priv->theming_engine, cr, x, y, width, height);
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
4041 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
4043 * <title>Typical focus rendering</title>
4044 * <inlinegraphic fileref="focus.png" format="PNG"/>
4050 gtk_render_focus (GtkStyleContext *context,
4057 GtkStyleContextPrivate *priv;
4058 GtkThemingEngineClass *engine_class;
4060 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4061 g_return_if_fail (cr != NULL);
4063 if (width <= 0 || height <= 0)
4066 priv = context->priv;
4067 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4071 store_animation_region (context, x, y, width, height);
4073 _gtk_theming_engine_set_context (priv->theming_engine, context);
4074 engine_class->render_focus (priv->theming_engine, cr, x, y, width, height);
4080 * gtk_render_layout:
4081 * @context: a #GtkStyleContext
4085 * @layout: the #PangoLayout to render
4087 * Renders @layout on the coordinates @x, @y
4092 gtk_render_layout (GtkStyleContext *context,
4096 PangoLayout *layout)
4098 GtkStyleContextPrivate *priv;
4099 GtkThemingEngineClass *engine_class;
4100 PangoRectangle extents;
4102 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4103 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4104 g_return_if_fail (cr != NULL);
4106 priv = context->priv;
4107 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4111 pango_layout_get_extents (layout, &extents, NULL);
4113 store_animation_region (context,
4119 _gtk_theming_engine_set_context (priv->theming_engine, context);
4120 engine_class->render_layout (priv->theming_engine, cr, x, y, layout);
4127 * @context: a #GtkStyleContext
4129 * @x0: X coordinate for the origin of the line
4130 * @y0: Y coordinate for the origin of the line
4131 * @x1: X coordinate for the end of the line
4132 * @y1: Y coordinate for the end of the line
4134 * Renders a line from (x0, y0) to (x1, y1).
4139 gtk_render_line (GtkStyleContext *context,
4146 GtkStyleContextPrivate *priv;
4147 GtkThemingEngineClass *engine_class;
4149 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4150 g_return_if_fail (cr != NULL);
4152 priv = context->priv;
4153 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4157 _gtk_theming_engine_set_context (priv->theming_engine, context);
4158 engine_class->render_line (priv->theming_engine, cr, x0, y0, x1, y1);
4164 * gtk_render_slider:
4165 * @context: a #GtkStyleContext
4167 * @x: X origin of the rectangle
4168 * @y: Y origin of the rectangle
4169 * @width: rectangle width
4170 * @height: rectangle height
4171 * @orientation: orientation of the slider
4173 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4174 * @width, @height. @orientation defines whether the slider is vertical
4178 * <title>Typical slider rendering</title>
4179 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4185 gtk_render_slider (GtkStyleContext *context,
4191 GtkOrientation orientation)
4193 GtkStyleContextPrivate *priv;
4194 GtkThemingEngineClass *engine_class;
4196 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4197 g_return_if_fail (cr != NULL);
4199 if (width <= 0 || height <= 0)
4202 priv = context->priv;
4203 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4207 store_animation_region (context, x, y, width, height);
4209 _gtk_theming_engine_set_context (priv->theming_engine, context);
4210 engine_class->render_slider (priv->theming_engine, cr, x, y, width, height, orientation);
4216 * gtk_render_frame_gap:
4217 * @context: a #GtkStyleContext
4219 * @x: X origin of the rectangle
4220 * @y: Y origin of the rectangle
4221 * @width: rectangle width
4222 * @height: rectangle height
4223 * @gap_side: side where the gap is
4224 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4225 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4227 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4228 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4229 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4230 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4233 * <title>Typical rendering of a frame with a gap</title>
4234 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4240 gtk_render_frame_gap (GtkStyleContext *context,
4246 GtkPositionType gap_side,
4250 GtkStyleContextPrivate *priv;
4251 GtkThemingEngineClass *engine_class;
4253 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4254 g_return_if_fail (cr != NULL);
4255 g_return_if_fail (xy0_gap <= xy1_gap);
4256 g_return_if_fail (xy0_gap >= 0);
4258 if (width <= 0 || height <= 0)
4261 if (gap_side == GTK_POS_LEFT ||
4262 gap_side == GTK_POS_RIGHT)
4263 g_return_if_fail (xy1_gap <= height);
4265 g_return_if_fail (xy1_gap <= width);
4267 priv = context->priv;
4268 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4272 store_animation_region (context, x, y, width, height);
4274 _gtk_theming_engine_set_context (priv->theming_engine, context);
4275 engine_class->render_frame_gap (priv->theming_engine, cr,
4276 x, y, width, height, gap_side,
4283 * gtk_render_extension:
4284 * @context: a #GtkStyleContext
4286 * @x: X origin of the rectangle
4287 * @y: Y origin of the rectangle
4288 * @width: rectangle width
4289 * @height: rectangle height
4290 * @gap_side: side where the gap is
4292 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4293 * defined by @x, @y, @width, @height. The side where the extension
4294 * connects to is defined by @gap_side.
4297 * <title>Typical extension rendering</title>
4298 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4304 gtk_render_extension (GtkStyleContext *context,
4310 GtkPositionType gap_side)
4312 GtkStyleContextPrivate *priv;
4313 GtkThemingEngineClass *engine_class;
4315 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4316 g_return_if_fail (cr != NULL);
4318 if (width <= 0 || height <= 0)
4321 priv = context->priv;
4322 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4326 store_animation_region (context, x, y, width, height);
4328 _gtk_theming_engine_set_context (priv->theming_engine, context);
4329 engine_class->render_extension (priv->theming_engine, cr, x, y, width, height, gap_side);
4335 * gtk_render_handle:
4336 * @context: a #GtkStyleContext
4338 * @x: X origin of the rectangle
4339 * @y: Y origin of the rectangle
4340 * @width: rectangle width
4341 * @height: rectangle height
4343 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4344 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4345 * determined by @x, @y, @width, @height.
4348 * <title>Handles rendered for the paned and grip classes</title>
4349 * <inlinegraphic fileref="handles.png" format="PNG"/>
4355 gtk_render_handle (GtkStyleContext *context,
4362 GtkStyleContextPrivate *priv;
4363 GtkThemingEngineClass *engine_class;
4365 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4366 g_return_if_fail (cr != NULL);
4368 if (width <= 0 || height <= 0)
4371 priv = context->priv;
4372 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4376 store_animation_region (context, x, y, width, height);
4378 _gtk_theming_engine_set_context (priv->theming_engine, context);
4379 engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
4385 * gtk_render_activity:
4386 * @context: a #GtkStyleContext
4388 * @x: X origin of the rectangle
4389 * @y: Y origin of the rectangle
4390 * @width: rectangle width
4391 * @height: rectangle height
4393 * Renders an activity area (Such as in #GtkSpinner or the
4394 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4395 * determines whether there is activity going on.
4400 gtk_render_activity (GtkStyleContext *context,
4407 GtkStyleContextPrivate *priv;
4408 GtkThemingEngineClass *engine_class;
4410 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4411 g_return_if_fail (cr != NULL);
4413 if (width <= 0 || height <= 0)
4416 priv = context->priv;
4417 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4421 store_animation_region (context, x, y, width, height);
4423 _gtk_theming_engine_set_context (priv->theming_engine, context);
4424 engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
4430 * gtk_render_icon_pixbuf:
4431 * @context: a #GtkStyleContext
4432 * @source: the #GtkIconSource specifying the icon to render
4433 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4434 * means render at the size of the source and don't scale.
4436 * Renders the icon specified by @source at the given @size, returning the result
4439 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4444 gtk_render_icon_pixbuf (GtkStyleContext *context,
4445 const GtkIconSource *source,
4448 GtkStyleContextPrivate *priv;
4449 GtkThemingEngineClass *engine_class;
4451 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4452 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4453 g_return_val_if_fail (source != NULL, NULL);
4455 priv = context->priv;
4456 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4458 _gtk_theming_engine_set_context (priv->theming_engine, context);
4459 return engine_class->render_icon_pixbuf (priv->theming_engine, source, size);
4464 * @context: a #GtkStyleContext
4466 * @pixbuf: a #GdkPixbuf containing the icon to draw
4467 * @x: X position for the @pixbuf
4468 * @y: Y position for the @pixbuf
4470 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4475 gtk_render_icon (GtkStyleContext *context,
4481 GtkStyleContextPrivate *priv;
4482 GtkThemingEngineClass *engine_class;
4484 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4485 g_return_if_fail (cr != NULL);
4487 priv = context->priv;
4488 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4492 store_animation_region (context,
4494 gdk_pixbuf_get_width (pixbuf),
4495 gdk_pixbuf_get_height (pixbuf));
4497 _gtk_theming_engine_set_context (priv->theming_engine, context);
4498 engine_class->render_icon (priv->theming_engine, cr, pixbuf, x, y);
4504 draw_insertion_cursor (GtkStyleContext *context,
4509 gboolean is_primary,
4510 PangoDirection direction,
4511 gboolean draw_arrow)
4514 GdkRGBA primary_color;
4515 GdkRGBA secondary_color;
4516 gfloat cursor_aspect_ratio;
4522 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4523 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4525 /* When changing the shape or size of the cursor here,
4526 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4529 gtk_style_context_get_style (context,
4530 "cursor-aspect-ratio", &cursor_aspect_ratio,
4533 stem_width = height * cursor_aspect_ratio + 1;
4535 /* put (stem_width % 2) on the proper side of the cursor */
4536 if (direction == PANGO_DIRECTION_LTR)
4537 offset = stem_width / 2;
4539 offset = stem_width - stem_width / 2;
4541 cairo_rectangle (cr, x - offset, y, stem_width, height);
4549 arrow_width = stem_width + 1;
4551 if (direction == PANGO_DIRECTION_RTL)
4553 ax = x - offset - 1;
4554 ay = y + height - arrow_width * 2 - arrow_width + 1;
4556 cairo_move_to (cr, ax, ay + 1);
4557 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4558 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4561 else if (direction == PANGO_DIRECTION_LTR)
4563 ax = x + stem_width - offset;
4564 ay = y + height - arrow_width * 2 - arrow_width + 1;
4566 cairo_move_to (cr, ax, ay + 1);
4567 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4568 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4572 g_assert_not_reached();
4579 * gtk_render_insertion_cursor:
4580 * @context: a #GtkStyleContext
4584 * @layout: the #PangoLayout of the text
4585 * @index: the index in the #PangoLayout
4586 * @direction: the #PangoDirection of the text
4588 * Draws a text caret on @cr at the specified index of @layout.
4593 gtk_render_insertion_cursor (GtkStyleContext *context,
4597 PangoLayout *layout,
4599 PangoDirection direction)
4601 GtkStyleContextPrivate *priv;
4602 gboolean split_cursor;
4603 PangoRectangle strong_pos, weak_pos;
4604 PangoRectangle *cursor1, *cursor2;
4605 PangoDirection keymap_direction;
4606 PangoDirection direction2;
4608 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4609 g_return_if_fail (cr != NULL);
4610 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4611 g_return_if_fail (index >= 0);
4613 priv = context->priv;
4615 g_object_get (gtk_settings_get_for_screen (priv->screen),
4616 "gtk-split-cursor", &split_cursor,
4619 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4621 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4623 direction2 = PANGO_DIRECTION_NEUTRAL;
4627 cursor1 = &strong_pos;
4629 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4631 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4632 cursor2 = &weak_pos;
4637 if (keymap_direction == direction)
4638 cursor1 = &strong_pos;
4640 cursor1 = &weak_pos;
4643 draw_insertion_cursor (context,
4645 x + PANGO_PIXELS (cursor1->x),
4646 y + PANGO_PIXELS (cursor1->y),
4647 PANGO_PIXELS (cursor1->height),
4650 direction2 != PANGO_DIRECTION_NEUTRAL);
4652 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4654 draw_insertion_cursor (context,
4656 x + PANGO_PIXELS (cursor2->x),
4657 y + PANGO_PIXELS (cursor2->y),
4658 PANGO_PIXELS (cursor2->height),
4666 * gtk_draw_insertion_cursor:
4667 * @widget: a #GtkWidget
4668 * @cr: cairo context to draw to
4669 * @location: location where to draw the cursor (@location->width is ignored)
4670 * @is_primary: if the cursor should be the primary cursor color.
4671 * @direction: whether the cursor is left-to-right or
4672 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4673 * @draw_arrow: %TRUE to draw a directional arrow on the
4674 * cursor. Should be %FALSE unless the cursor is split.
4676 * Draws a text caret on @cr at @location. This is not a style function
4677 * but merely a convenience function for drawing the standard cursor shape.
4680 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4683 gtk_draw_insertion_cursor (GtkWidget *widget,
4685 const GdkRectangle *location,
4686 gboolean is_primary,
4687 GtkTextDirection direction,
4688 gboolean draw_arrow)
4690 GtkStyleContext *context;
4692 g_return_if_fail (GTK_IS_WIDGET (widget));
4693 g_return_if_fail (cr != NULL);
4694 g_return_if_fail (location != NULL);
4695 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4697 context = gtk_widget_get_style_context (widget);
4699 draw_insertion_cursor (context, cr,
4700 location->x, location->y, location->height,
4702 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4706 static AtkAttributeSet *
4707 add_attribute (AtkAttributeSet *attributes,
4708 AtkTextAttribute attr,
4713 at = g_new (AtkAttribute, 1);
4714 at->name = g_strdup (atk_text_attribute_get_name (attr));
4715 at->value = g_strdup (value);
4717 return g_slist_prepend (attributes, at);
4721 * _gtk_style_context_get_attributes:
4722 * @attributes: a #AtkAttributeSet to add attributes to
4723 * @context: the #GtkStyleContext to get attributes from
4724 * @flags: the state to use with @context
4726 * Adds the foreground and background color from @context to
4727 * @attributes, after translating them to ATK attributes.
4729 * This is a convenience function that can be used in
4730 * implementing the #AtkText interface in widgets.
4732 * Returns: the modified #AtkAttributeSet
4735 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4736 GtkStyleContext *context,
4737 GtkStateFlags flags)
4742 gtk_style_context_get_background_color (context, flags, &color);
4743 value = g_strdup_printf ("%u,%u,%u",
4744 (guint) ceil (color.red * 65536 - color.red),
4745 (guint) ceil (color.green * 65536 - color.green),
4746 (guint) ceil (color.blue * 65536 - color.blue));
4747 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4750 gtk_style_context_get_color (context, flags, &color);
4751 value = g_strdup_printf ("%u,%u,%u",
4752 (guint) ceil (color.red * 65536 - color.red),
4753 (guint) ceil (color.green * 65536 - color.green),
4754 (guint) ceil (color.blue * 65536 - color.blue));
4755 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);