1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #include <gobject/gvaluecollector.h>
27 #include "gtkstylecontextprivate.h"
28 #include "gtkstylepropertiesprivate.h"
29 #include "gtktypebuiltins.h"
30 #include "gtkthemingengine.h"
32 #include "gtkwidget.h"
33 #include "gtkwindow.h"
34 #include "gtkprivate.h"
35 #include "gtksymboliccolorprivate.h"
36 #include "gtkanimationdescription.h"
37 #include "gtktimeline.h"
38 #include "gtkiconfactory.h"
39 #include "gtkwidgetprivate.h"
40 #include "gtkstyleproviderprivate.h"
43 * SECTION:gtkstylecontext
44 * @Short_description: Rendering UI elements
45 * @Title: GtkStyleContext
47 * #GtkStyleContext is an object that stores styling information affecting
48 * a widget defined by #GtkWidgetPath.
50 * In order to construct the final style information, #GtkStyleContext
51 * queries information from all attached #GtkStyleProviders. Style providers
52 * can be either attached explicitly to the context through
53 * gtk_style_context_add_provider(), or to the screen through
54 * gtk_style_context_add_provider_for_screen(). The resulting style is a
55 * combination of all providers' information in priority order.
57 * For GTK+ widgets, any #GtkStyleContext returned by
58 * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
59 * #GdkScreen and RTL/LTR information set. The style context will be also
60 * updated automatically if any of these settings change on the widget.
62 * If you are using the theming layer standalone, you will need to set a
63 * widget path and a screen yourself to the created style context through
64 * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
65 * as updating the context yourself using gtk_style_context_invalidate()
66 * whenever any of the conditions change, such as a change in the
67 * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
70 * <refsect2 id="gtkstylecontext-animations">
71 * <title>Transition animations</title>
73 * #GtkStyleContext has built-in support for state change transitions.
74 * Note that these animations respect the #GtkSettings:gtk-enable-animations
78 * For simple widgets where state changes affect the whole widget area,
79 * calling gtk_style_context_notify_state_change() with a %NULL region
80 * is sufficient to trigger the transition animation. And GTK+ already
81 * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
85 * If a widget needs to declare several animatable regions (i.e. not
86 * affecting the whole widget area), its #GtkWidget::draw signal handler
87 * needs to wrap the render operations for the different regions with
88 * calls to gtk_style_context_push_animatable_region() and
89 * gtk_style_context_pop_animatable_region(). These functions take an
90 * identifier for the region which must be unique within the style context.
91 * For simple widgets with a fixed set of animatable regions, using an
92 * enumeration works well:
95 * <title>Using an enumeration to identify animatable regions</title>
106 * spin_button_draw (GtkWidget *widget,
109 * GtkStyleContext *context;
111 * context = gtk_widget_get_style_context (widget);
113 * gtk_style_context_push_animatable_region (context,
114 * GUINT_TO_POINTER (REGION_ENTRY));
116 * gtk_render_background (cr, 0, 0, 100, 30);
117 * gtk_render_frame (cr, 0, 0, 100, 30);
119 * gtk_style_context_pop_animatable_region (context);
126 * For complex widgets with an arbitrary number of animatable regions, it
127 * is up to the implementation to come up with a way to uniquely identify
128 * each animatable region. Using pointers to internal structs is one way
132 * <title>Using struct pointers to identify animatable regions</title>
135 * notebook_draw_tab (GtkWidget *widget,
136 * NotebookPage *page,
139 * gtk_style_context_push_animatable_region (context, page);
140 * gtk_render_extension (cr, page->x, page->y, page->width, page->height);
141 * gtk_style_context_pop_animatable_region (context);
146 * The widget also needs to notify the style context about a state change
147 * for a given animatable region so the animation is triggered.
150 * <title>Triggering a state change animation on a region</title>
153 * notebook_motion_notify (GtkWidget *widget,
154 * GdkEventMotion *event)
156 * GtkStyleContext *context;
157 * NotebookPage *page;
159 * context = gtk_widget_get_style_context (widget);
160 * page = find_page_under_pointer (widget, event);
161 * gtk_style_context_notify_state_change (context,
162 * gtk_widget_get_window (widget),
164 * GTK_STATE_PRELIGHT,
171 * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
172 * special value, in this case, the whole widget area will be updated
176 * <refsect2 id="gtkstylecontext-classes">
177 * <title>Style classes and regions</title>
179 * Widgets can add style classes to their context, which can be used
180 * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
181 * rendering. GTK+ has a number of predefined style classes:
182 * #GTK_STYLE_CLASS_CELL,
183 * #GTK_STYLE_CLASS_ENTRY,
184 * #GTK_STYLE_CLASS_BUTTON,
185 * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
186 * #GTK_STYLE_CLASS_CALENDAR,
187 * #GTK_STYLE_CLASS_SLIDER,
188 * #GTK_STYLE_CLASS_BACKGROUND,
189 * #GTK_STYLE_CLASS_RUBBERBAND,
190 * #GTK_STYLE_CLASS_TOOLTIP,
191 * #GTK_STYLE_CLASS_MENU,
192 * #GTK_STYLE_CLASS_MENUBAR,
193 * #GTK_STYLE_CLASS_MENUITEM,
194 * #GTK_STYLE_CLASS_TOOLBAR,
195 * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
196 * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
197 * #GTK_STYLE_CLASS_RADIO,
198 * #GTK_STYLE_CLASS_CHECK,
199 * #GTK_STYLE_CLASS_TROUGH,
200 * #GTK_STYLE_CLASS_SCROLLBAR,
201 * #GTK_STYLE_CLASS_SCALE,
202 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
203 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
204 * #GTK_STYLE_CLASS_HEADER,
205 * #GTK_STYLE_CLASS_ACCELERATOR,
206 * #GTK_STYLE_CLASS_GRIP,
207 * #GTK_STYLE_CLASS_DOCK,
208 * #GTK_STYLE_CLASS_PROGRESSBAR,
209 * #GTK_STYLE_CLASS_SPINNER,
210 * #GTK_STYLE_CLASS_EXPANDER,
211 * #GTK_STYLE_CLASS_SPINBUTTON,
212 * #GTK_STYLE_CLASS_NOTEBOOK,
213 * #GTK_STYLE_CLASS_VIEW,
214 * #GTK_STYLE_CLASS_SIDEBAR,
215 * #GTK_STYLE_CLASS_IMAGE,
216 * #GTK_STYLE_CLASS_HIGHLIGHT,
217 * #GTK_STYLE_CLASS_FRAME,
218 * #GTK_STYLE_CLASS_DND,
219 * #GTK_STYLE_CLASS_PANE_SEPARATOR,
220 * #GTK_STYLE_CLASS_SEPARATOR,
221 * #GTK_STYLE_CLASS_INFO,
222 * #GTK_STYLE_CLASS_WARNING,
223 * #GTK_STYLE_CLASS_QUESTION,
224 * #GTK_STYLE_CLASS_ERROR,
225 * #GTK_STYLE_CLASS_HORIZONTAL,
226 * #GTK_STYLE_CLASS_VERTICAL,
227 * #GTK_STYLE_CLASS_TOP,
228 * #GTK_STYLE_CLASS_BOTTOM,
229 * #GTK_STYLE_CLASS_LEFT,
230 * #GTK_STYLE_CLASS_RIGHT,
233 * Widgets can also add regions with flags to their context.
234 * The regions used by GTK+ widgets are:
239 * <entry>Region</entry>
240 * <entry>Flags</entry>
241 * <entry>Macro</entry>
242 * <entry>Used by</entry>
248 * <entry>even, odd</entry>
249 * <entry>GTK_STYLE_REGION_ROW</entry>
250 * <entry>#GtkTreeView</entry>
253 * <entry>column</entry>
254 * <entry>first, last, sorted</entry>
255 * <entry>GTK_STYLE_REGION_COLUMN</entry>
256 * <entry>#GtkTreeView</entry>
259 * <entry>column-header</entry>
261 * <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
266 * <entry>even, odd, first, last</entry>
267 * <entry>GTK_STYLE_REGION_TAB</entry>
268 * <entry>#GtkNotebook</entry>
275 * <refsect2 id="gtkstylecontext-custom-styling">
276 * <title>Custom styling in UI libraries and applications</title>
278 * If you are developing a library with custom #GtkWidget<!-- -->s that
279 * render differently than standard components, you may need to add a
280 * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
281 * priority, either a #GtkCssProvider or a custom object implementing the
282 * #GtkStyleProvider interface. This way theming engines may still attempt
283 * to style your UI elements in a different way if needed so.
286 * If you are using custom styling on an applications, you probably want then
287 * to make your style information prevail to the theme's, so you must use
288 * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
289 * priority, keep in mind that the user settings in
290 * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
291 * still take precedence over your changes, as it uses the
292 * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
295 * If a custom theming engine is needed, you probably want to implement a
296 * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
297 * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
298 * which loads the theming engine module from the standard paths.
303 typedef struct GtkStyleProviderData GtkStyleProviderData;
304 typedef struct GtkStyleInfo GtkStyleInfo;
305 typedef struct GtkRegion GtkRegion;
306 typedef struct PropertyValue PropertyValue;
307 typedef struct AnimationInfo AnimationInfo;
308 typedef struct StyleData StyleData;
313 GtkRegionFlags flags;
316 struct GtkStyleProviderData
318 GtkStyleProvider *provider;
332 GArray *style_classes;
334 GtkJunctionSides junction_sides;
335 GtkStateFlags state_flags;
340 GtkStyleProperties *store;
341 GSList *icon_factories;
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
369 GList *providers_last;
371 GtkStyleContext *parent;
372 GtkWidgetPath *widget_path;
373 GHashTable *style_data;
375 StyleData *current_data;
376 GtkStateFlags current_state;
378 GSList *animation_regions;
381 GtkThemingEngine *theming_engine;
383 GtkTextDirection direction;
385 guint animations_invalidated : 1;
386 guint invalidating_context : 1;
401 static guint signals[LAST_SIGNAL] = { 0 };
403 static GQuark provider_list_quark = 0;
405 static void gtk_style_context_finalize (GObject *object);
407 static void gtk_style_context_impl_set_property (GObject *object,
411 static void gtk_style_context_impl_get_property (GObject *object,
415 static GtkSymbolicColor *
416 gtk_style_context_color_lookup_func (gpointer contextp,
420 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
423 gtk_style_context_class_init (GtkStyleContextClass *klass)
425 GObjectClass *object_class = G_OBJECT_CLASS (klass);
427 object_class->finalize = gtk_style_context_finalize;
428 object_class->set_property = gtk_style_context_impl_set_property;
429 object_class->get_property = gtk_style_context_impl_get_property;
432 g_signal_new (I_("changed"),
433 G_TYPE_FROM_CLASS (object_class),
435 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
437 g_cclosure_marshal_VOID__VOID,
440 g_object_class_install_property (object_class,
442 g_param_spec_object ("screen",
444 P_("The associated GdkScreen"),
446 GTK_PARAM_READWRITE));
447 g_object_class_install_property (object_class,
449 g_param_spec_enum ("direction",
451 P_("Text direction"),
452 GTK_TYPE_TEXT_DIRECTION,
454 GTK_PARAM_READWRITE));
456 * GtkStyleContext:parent:
458 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
463 g_object_class_install_property (object_class,
465 g_param_spec_object ("parent",
467 P_("The parent style context"),
468 GTK_TYPE_STYLE_CONTEXT,
469 GTK_PARAM_READWRITE));
471 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
474 static GtkStyleInfo *
475 style_info_new (void)
479 info = g_slice_new0 (GtkStyleInfo);
480 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
481 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
487 style_info_free (GtkStyleInfo *info)
489 g_array_free (info->style_classes, TRUE);
490 g_array_free (info->regions, TRUE);
491 g_slice_free (GtkStyleInfo, info);
494 static GtkStyleInfo *
495 style_info_copy (const GtkStyleInfo *info)
499 copy = style_info_new ();
500 g_array_insert_vals (copy->style_classes, 0,
501 info->style_classes->data,
502 info->style_classes->len);
504 g_array_insert_vals (copy->regions, 0,
508 copy->junction_sides = info->junction_sides;
509 copy->state_flags = info->state_flags;
515 style_info_hash (gconstpointer elem)
517 const GtkStyleInfo *info;
522 for (i = 0; i < info->style_classes->len; i++)
524 hash += g_array_index (info->style_classes, GQuark, i);
528 for (i = 0; i < info->regions->len; i++)
532 region = &g_array_index (info->regions, GtkRegion, i);
533 hash += region->class_quark;
534 hash += region->flags;
538 return hash ^ info->state_flags;
542 style_info_equal (gconstpointer elem1,
545 const GtkStyleInfo *info1, *info2;
550 if (info1->state_flags != info2->state_flags)
553 if (info1->junction_sides != info2->junction_sides)
556 if (info1->style_classes->len != info2->style_classes->len)
559 if (memcmp (info1->style_classes->data,
560 info2->style_classes->data,
561 info1->style_classes->len * sizeof (GQuark)) != 0)
564 if (info1->regions->len != info2->regions->len)
567 if (memcmp (info1->regions->data,
568 info2->regions->data,
569 info1->regions->len * sizeof (GtkRegion)) != 0)
576 style_data_new (void)
580 data = g_slice_new0 (StyleData);
586 clear_property_cache (StyleData *data)
590 if (!data->property_cache)
593 for (i = 0; i < data->property_cache->len; i++)
595 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
597 g_param_spec_unref (node->pspec);
598 g_value_unset (&node->value);
601 g_array_free (data->property_cache, TRUE);
602 data->property_cache = NULL;
606 style_data_free (StyleData *data)
608 g_object_unref (data->store);
609 clear_property_cache (data);
611 g_slist_free_full (data->icon_factories, g_object_unref);
613 g_slice_free (StyleData, data);
617 gtk_style_context_init (GtkStyleContext *style_context)
619 GtkStyleContextPrivate *priv;
622 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
623 GTK_TYPE_STYLE_CONTEXT,
624 GtkStyleContextPrivate);
626 priv->style_data = g_hash_table_new_full (style_info_hash,
628 (GDestroyNotify) style_info_free,
629 (GDestroyNotify) style_data_free);
630 priv->theming_engine = g_object_ref ((gpointer) gtk_theming_engine_load (NULL));
632 priv->direction = GTK_TEXT_DIR_LTR;
634 priv->screen = gdk_screen_get_default ();
636 /* Create default info store */
637 info = style_info_new ();
638 priv->info_stack = g_slist_prepend (priv->info_stack, info);
641 static GtkStyleProviderData *
642 style_provider_data_new (GtkStyleProvider *provider,
645 GtkStyleProviderData *data;
647 data = g_slice_new (GtkStyleProviderData);
648 data->provider = g_object_ref (provider);
649 data->priority = priority;
655 style_provider_data_free (GtkStyleProviderData *data)
657 g_object_unref (data->provider);
658 g_slice_free (GtkStyleProviderData, data);
662 animation_info_free (AnimationInfo *info)
664 g_object_unref (info->timeline);
665 g_object_unref (info->window);
667 if (info->invalidation_region)
668 cairo_region_destroy (info->invalidation_region);
670 g_array_free (info->rectangles, TRUE);
671 g_slist_free (info->parent_regions);
672 g_slice_free (AnimationInfo, info);
675 static AnimationInfo *
676 animation_info_lookup_by_timeline (GtkStyleContext *context,
677 GtkTimeline *timeline)
679 GtkStyleContextPrivate *priv;
683 priv = context->priv;
685 for (l = priv->animations; l; l = l->next)
689 if (info->timeline == timeline)
697 timeline_frame_cb (GtkTimeline *timeline,
701 GtkStyleContextPrivate *priv;
702 GtkStyleContext *context;
706 priv = context->priv;
707 info = animation_info_lookup_by_timeline (context, timeline);
709 g_assert (info != NULL);
711 /* Cancel transition if window is gone */
712 if (gdk_window_is_destroyed (info->window) ||
713 !gdk_window_is_visible (info->window))
715 priv->animations = g_slist_remove (priv->animations, info);
716 animation_info_free (info);
720 if (info->invalidation_region &&
721 !cairo_region_is_empty (info->invalidation_region))
722 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
724 gdk_window_invalidate_rect (info->window, NULL, TRUE);
728 timeline_finished_cb (GtkTimeline *timeline,
731 GtkStyleContextPrivate *priv;
732 GtkStyleContext *context;
736 priv = context->priv;
737 info = animation_info_lookup_by_timeline (context, timeline);
739 g_assert (info != NULL);
741 priv->animations = g_slist_remove (priv->animations, info);
743 /* Invalidate one last time the area, so the final content is painted */
744 if (info->invalidation_region &&
745 !cairo_region_is_empty (info->invalidation_region))
746 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
748 gdk_window_invalidate_rect (info->window, NULL, TRUE);
750 animation_info_free (info);
753 static AnimationInfo *
754 animation_info_new (GtkStyleContext *context,
757 GtkTimelineProgressType progress_type,
760 gboolean target_value,
765 info = g_slice_new0 (AnimationInfo);
767 info->rectangles = g_array_new (FALSE, FALSE, sizeof (cairo_rectangle_int_t));
768 info->timeline = _gtk_timeline_new (duration);
769 info->window = g_object_ref (window);
771 info->target_value = target_value;
772 info->region_id = region_id;
774 _gtk_timeline_set_progress_type (info->timeline, progress_type);
775 _gtk_timeline_set_loop (info->timeline, loop);
777 if (!loop && !target_value)
779 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
780 _gtk_timeline_rewind (info->timeline);
783 g_signal_connect (info->timeline, "frame",
784 G_CALLBACK (timeline_frame_cb), context);
785 g_signal_connect (info->timeline, "finished",
786 G_CALLBACK (timeline_finished_cb), context);
788 _gtk_timeline_start (info->timeline);
793 static AnimationInfo *
794 animation_info_lookup (GtkStyleContext *context,
798 GtkStyleContextPrivate *priv;
801 priv = context->priv;
803 for (l = priv->animations; l; l = l->next)
809 if (info->state == state &&
810 info->region_id == region_id)
818 gtk_style_context_finalize (GObject *object)
820 GtkStyleContextPrivate *priv;
821 GtkStyleContext *style_context;
824 style_context = GTK_STYLE_CONTEXT (object);
825 priv = style_context->priv;
827 gtk_style_context_set_parent (style_context, NULL);
829 if (priv->widget_path)
830 gtk_widget_path_free (priv->widget_path);
832 g_hash_table_destroy (priv->style_data);
834 g_list_free_full (priv->providers, (GDestroyNotify) style_provider_data_free);
836 g_slist_free_full (priv->info_stack, (GDestroyNotify) style_info_free);
838 g_slist_free (priv->animation_regions);
840 for (l = priv->animations; l; l = l->next)
841 animation_info_free ((AnimationInfo *) l->data);
843 g_slist_free (priv->animations);
845 if (priv->theming_engine)
846 g_object_unref (priv->theming_engine);
848 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
852 gtk_style_context_impl_set_property (GObject *object,
857 GtkStyleContext *style_context;
859 style_context = GTK_STYLE_CONTEXT (object);
864 gtk_style_context_set_screen (style_context,
865 g_value_get_object (value));
868 gtk_style_context_set_direction (style_context,
869 g_value_get_enum (value));
872 gtk_style_context_set_parent (style_context,
873 g_value_get_object (value));
876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
882 gtk_style_context_impl_get_property (GObject *object,
887 GtkStyleContext *style_context;
888 GtkStyleContextPrivate *priv;
890 style_context = GTK_STYLE_CONTEXT (object);
891 priv = style_context->priv;
896 g_value_set_object (value, priv->screen);
899 g_value_set_enum (value, priv->direction);
902 g_value_set_object (value, priv->parent);
905 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
911 find_next_candidate (GList *local,
917 GtkStyleProviderData *local_data, *global_data;
919 local_data = local->data;
920 global_data = global->data;
922 if (local_data->priority < global_data->priority)
923 return (ascending) ? local : global;
925 return (ascending) ? global : local;
936 build_properties (GtkStyleContext *context,
937 StyleData *style_data,
941 GtkStyleContextPrivate *priv;
942 GList *elem, *list, *global_list = NULL;
943 GtkCssLookup *lookup;
945 priv = context->priv;
946 list = priv->providers_last;
950 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
951 global_list = g_list_last (global_list);
954 lookup = _gtk_css_lookup_new ();
956 while ((elem = find_next_candidate (list, global_list, FALSE)) != NULL)
958 GtkStyleProviderData *data;
959 GtkStyleProperties *provider_style;
966 global_list = global_list->prev;
968 if (GTK_IS_STYLE_PROVIDER_PRIVATE (data->provider))
970 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (data->provider),
977 provider_style = gtk_style_provider_get_style (data->provider, path);
981 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (provider_style),
985 g_object_unref (provider_style);
990 style_data->store = gtk_style_properties_new ();
991 _gtk_css_lookup_resolve (lookup, context, style_data->store);
992 _gtk_css_lookup_free (lookup);
996 build_icon_factories (GtkStyleContext *context,
997 StyleData *style_data,
1000 GtkStyleContextPrivate *priv;
1001 GList *elem, *list, *global_list = NULL;
1003 priv = context->priv;
1004 list = priv->providers_last;
1008 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
1009 global_list = g_list_last (global_list);
1012 while ((elem = find_next_candidate (list, global_list, FALSE)) != NULL)
1014 GtkIconFactory *factory;
1015 GtkStyleProviderData *data;
1022 global_list = global_list->prev;
1024 factory = gtk_style_provider_get_icon_factory (data->provider, path);
1027 style_data->icon_factories = g_slist_prepend (style_data->icon_factories, factory);
1031 static GtkWidgetPath *
1032 create_query_path (GtkStyleContext *context)
1034 GtkStyleContextPrivate *priv;
1035 GtkWidgetPath *path;
1039 priv = context->priv;
1040 path = gtk_widget_path_copy (priv->widget_path);
1041 pos = gtk_widget_path_length (path) - 1;
1043 info = priv->info_stack->data;
1045 /* Set widget regions */
1046 for (i = 0; i < info->regions->len; i++)
1050 region = &g_array_index (info->regions, GtkRegion, i);
1051 gtk_widget_path_iter_add_region (path, pos,
1052 g_quark_to_string (region->class_quark),
1056 /* Set widget classes */
1057 for (i = 0; i < info->style_classes->len; i++)
1061 quark = g_array_index (info->style_classes, GQuark, i);
1062 gtk_widget_path_iter_add_class (path, pos,
1063 g_quark_to_string (quark));
1070 style_data_lookup (GtkStyleContext *context,
1071 GtkStateFlags state)
1073 GtkStyleContextPrivate *priv;
1075 gboolean state_mismatch;
1077 priv = context->priv;
1078 state_mismatch = ((GtkStyleInfo *) priv->info_stack->data)->state_flags != state;
1080 /* Current data in use is cached, just return it */
1081 if (priv->current_data && priv->current_state == state)
1082 return priv->current_data;
1084 g_assert (priv->widget_path != NULL);
1086 if (G_UNLIKELY (state_mismatch))
1088 gtk_style_context_save (context);
1089 gtk_style_context_set_state (context, state);
1092 priv->current_data = g_hash_table_lookup (priv->style_data, priv->info_stack->data);
1093 priv->current_state = state;
1095 if (!priv->current_data)
1097 GtkWidgetPath *path;
1099 priv->current_data = style_data_new ();
1100 g_hash_table_insert (priv->style_data,
1101 style_info_copy (priv->info_stack->data),
1102 priv->current_data);
1104 path = create_query_path (context);
1106 build_properties (context, priv->current_data, path, state);
1107 build_icon_factories (context, priv->current_data, path);
1109 gtk_widget_path_free (path);
1112 data = priv->current_data;
1114 if (priv->theming_engine)
1115 g_object_unref (priv->theming_engine);
1117 gtk_style_properties_get (priv->current_data->store, 0,
1118 "engine", &priv->theming_engine,
1121 if (!priv->theming_engine)
1122 priv->theming_engine = g_object_ref (gtk_theming_engine_load (NULL));
1124 if (G_UNLIKELY (state_mismatch))
1125 gtk_style_context_restore (context);
1131 style_provider_add (GList **list,
1132 GtkStyleProvider *provider,
1135 GtkStyleProviderData *new_data;
1136 gboolean added = FALSE;
1139 new_data = style_provider_data_new (provider, priority);
1143 GtkStyleProviderData *data;
1147 /* Provider was already attached to the style
1148 * context, remove in order to add the new data
1150 if (data->provider == provider)
1157 /* Remove and free link */
1158 *list = g_list_remove_link (*list, link);
1159 style_provider_data_free (link->data);
1160 g_list_free_1 (link);
1166 data->priority > priority)
1168 *list = g_list_insert_before (*list, l, new_data);
1176 *list = g_list_append (*list, new_data);
1180 style_provider_remove (GList **list,
1181 GtkStyleProvider *provider)
1187 GtkStyleProviderData *data;
1191 if (data->provider == provider)
1193 *list = g_list_remove_link (*list, l);
1194 style_provider_data_free (l->data);
1207 * gtk_style_context_new:
1209 * Creates a standalone #GtkStyleContext, this style context
1210 * won't be attached to any widget, so you may want
1211 * to call gtk_style_context_set_path() yourself.
1214 * This function is only useful when using the theming layer
1215 * separated from GTK+, if you are using #GtkStyleContext to
1216 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1217 * in order to get a style context ready to theme the widget.
1220 * Returns: A newly created #GtkStyleContext.
1223 gtk_style_context_new (void)
1225 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1229 * gtk_style_context_add_provider:
1230 * @context: a #GtkStyleContext
1231 * @provider: a #GtkStyleProvider
1232 * @priority: the priority of the style provider. The lower
1233 * it is, the earlier it will be used in the style
1234 * construction. Typically this will be in the range
1235 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1236 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1238 * Adds a style provider to @context, to be used in style construction.
1240 * <note><para>If both priorities are the same, A #GtkStyleProvider
1241 * added through this function takes precedence over another added
1242 * through gtk_style_context_add_provider_for_screen().</para></note>
1247 gtk_style_context_add_provider (GtkStyleContext *context,
1248 GtkStyleProvider *provider,
1251 GtkStyleContextPrivate *priv;
1253 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1254 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1256 priv = context->priv;
1257 style_provider_add (&priv->providers, provider, priority);
1258 priv->providers_last = g_list_last (priv->providers);
1260 gtk_style_context_invalidate (context);
1264 * gtk_style_context_remove_provider:
1265 * @context: a #GtkStyleContext
1266 * @provider: a #GtkStyleProvider
1268 * Removes @provider from the style providers list in @context.
1273 gtk_style_context_remove_provider (GtkStyleContext *context,
1274 GtkStyleProvider *provider)
1276 GtkStyleContextPrivate *priv;
1278 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1279 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1281 priv = context->priv;
1283 if (style_provider_remove (&priv->providers, provider))
1285 priv->providers_last = g_list_last (priv->providers);
1287 gtk_style_context_invalidate (context);
1292 * gtk_style_context_reset_widgets:
1293 * @screen: a #GdkScreen
1295 * This function recomputes the styles for all widgets under a particular
1296 * #GdkScreen. This is useful when some global parameter has changed that
1297 * affects the appearance of all widgets, because when a widget gets a new
1298 * style, it will both redraw and recompute any cached information about
1299 * its appearance. As an example, it is used when the color scheme changes
1300 * in the related #GtkSettings object.
1305 gtk_style_context_reset_widgets (GdkScreen *screen)
1307 GList *list, *toplevels;
1309 _gtk_icon_set_invalidate_caches ();
1311 toplevels = gtk_window_list_toplevels ();
1312 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1314 for (list = toplevels; list; list = list->next)
1316 if (gtk_widget_get_screen (list->data) == screen)
1317 gtk_widget_reset_style (list->data);
1319 g_object_unref (list->data);
1322 g_list_free (toplevels);
1326 * gtk_style_context_add_provider_for_screen:
1327 * @screen: a #GdkScreen
1328 * @provider: a #GtkStyleProvider
1329 * @priority: the priority of the style provider. The lower
1330 * it is, the earlier it will be used in the style
1331 * construction. Typically this will be in the range
1332 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1333 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1335 * Adds a global style provider to @screen, which will be used
1336 * in style construction for all #GtkStyleContext<!-- -->s under
1339 * GTK+ uses this to make styling information from #GtkSettings
1342 * <note><para>If both priorities are the same, A #GtkStyleProvider
1343 * added through gtk_style_context_add_provider() takes precedence
1344 * over another added through this function.</para></note>
1349 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1350 GtkStyleProvider *provider,
1353 GList *providers, *list;
1355 g_return_if_fail (GDK_IS_SCREEN (screen));
1356 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1358 if (G_UNLIKELY (!provider_list_quark))
1359 provider_list_quark = g_quark_from_static_string ("gtk-provider-list-quark");
1361 list = providers = g_object_get_qdata (G_OBJECT (screen), provider_list_quark);
1362 style_provider_add (&list, provider, priority);
1364 if (list != providers)
1365 g_object_set_qdata (G_OBJECT (screen), provider_list_quark, list);
1367 gtk_style_context_reset_widgets (screen);
1371 * gtk_style_context_remove_provider_for_screen:
1372 * @screen: a #GdkScreen
1373 * @provider: a #GtkStyleProvider
1375 * Removes @provider from the global style providers list in @screen.
1380 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1381 GtkStyleProvider *provider)
1383 GList *providers, *list;
1385 g_return_if_fail (GDK_IS_SCREEN (screen));
1386 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1388 if (G_UNLIKELY (!provider_list_quark))
1391 list = providers = g_object_get_qdata (G_OBJECT (screen), provider_list_quark);
1393 if (style_provider_remove (&list, provider))
1395 if (list != providers)
1396 g_object_set_qdata (G_OBJECT (screen), provider_list_quark, list);
1398 gtk_style_context_reset_widgets (screen);
1403 * gtk_style_context_get_property:
1404 * @context: a #GtkStyleContext
1405 * @property: style property name
1406 * @state: state to retrieve the property value for
1407 * @value: (out) (transfer full): return location for the style property value
1409 * Gets a style property from @context for the given state.
1411 * When @value is no longer needed, g_value_unset() must be called
1412 * to free any allocated memory.
1417 gtk_style_context_get_property (GtkStyleContext *context,
1418 const gchar *property,
1419 GtkStateFlags state,
1422 GtkStyleContextPrivate *priv;
1425 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1426 g_return_if_fail (property != NULL);
1427 g_return_if_fail (value != NULL);
1429 priv = context->priv;
1431 g_return_if_fail (priv->widget_path != NULL);
1433 data = style_data_lookup (context, state);
1434 gtk_style_properties_get_property (data->store, property, 0, value);
1438 * gtk_style_context_get_valist:
1439 * @context: a #GtkStyleContext
1440 * @state: state to retrieve the property values for
1441 * @args: va_list of property name/return location pairs, followed by %NULL
1443 * Retrieves several style property values from @context for a given state.
1448 gtk_style_context_get_valist (GtkStyleContext *context,
1449 GtkStateFlags state,
1452 GtkStyleContextPrivate *priv;
1455 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1457 priv = context->priv;
1458 g_return_if_fail (priv->widget_path != NULL);
1460 data = style_data_lookup (context, state);
1461 gtk_style_properties_get_valist (data->store, 0, args);
1465 * gtk_style_context_get:
1466 * @context: a #GtkStyleContext
1467 * @state: state to retrieve the property values for
1468 * @...: property name /return value pairs, followed by %NULL
1470 * Retrieves several style property values from @context for a
1476 gtk_style_context_get (GtkStyleContext *context,
1477 GtkStateFlags state,
1482 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1484 va_start (args, state);
1485 gtk_style_context_get_valist (context, state, args);
1490 * gtk_style_context_set_state:
1491 * @context: a #GtkStyleContext
1492 * @flags: state to represent
1494 * Sets the state to be used when rendering with any
1495 * of the gtk_render_*() functions.
1500 gtk_style_context_set_state (GtkStyleContext *context,
1501 GtkStateFlags flags)
1503 GtkStyleContextPrivate *priv;
1506 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1508 priv = context->priv;
1509 info = priv->info_stack->data;
1510 info->state_flags = flags;
1514 * gtk_style_context_get_state:
1515 * @context: a #GtkStyleContext
1517 * Returns the state used when rendering.
1519 * Returns: the state flags
1524 gtk_style_context_get_state (GtkStyleContext *context)
1526 GtkStyleContextPrivate *priv;
1529 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1531 priv = context->priv;
1532 info = priv->info_stack->data;
1534 return info->state_flags;
1538 context_has_animatable_region (GtkStyleContext *context,
1541 GtkStyleContextPrivate *priv;
1543 /* NULL region_id means everything
1544 * rendered through the style context
1549 priv = context->priv;
1550 return g_slist_find (priv->animation_regions, region_id) != NULL;
1554 * gtk_style_context_state_is_running:
1555 * @context: a #GtkStyleContext
1556 * @state: a widget state
1557 * @progress: (out): return location for the transition progress
1559 * Returns %TRUE if there is a transition animation running for the
1560 * current region (see gtk_style_context_push_animatable_region()).
1562 * If @progress is not %NULL, the animation progress will be returned
1563 * there, 0.0 means the state is closest to being unset, while 1.0 means
1564 * it's closest to being set. This means transition animation will
1565 * run from 0 to 1 when @state is being set and from 1 to 0 when
1568 * Returns: %TRUE if there is a running transition animation for @state.
1573 gtk_style_context_state_is_running (GtkStyleContext *context,
1577 GtkStyleContextPrivate *priv;
1578 AnimationInfo *info;
1581 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1583 priv = context->priv;
1585 for (l = priv->animations; l; l = l->next)
1589 if (info->state == state &&
1590 context_has_animatable_region (context, info->region_id))
1593 *progress = _gtk_timeline_get_progress (info->timeline);
1603 * gtk_style_context_set_path:
1604 * @context: a #GtkStyleContext
1605 * @path: a #GtkWidgetPath
1607 * Sets the #GtkWidgetPath used for style matching. As a
1608 * consequence, the style will be regenerated to match
1609 * the new given path.
1611 * If you are using a #GtkStyleContext returned from
1612 * gtk_widget_get_style_context(), you do not need to call
1618 gtk_style_context_set_path (GtkStyleContext *context,
1619 GtkWidgetPath *path)
1621 GtkStyleContextPrivate *priv;
1623 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1624 g_return_if_fail (path != NULL);
1626 priv = context->priv;
1628 if (priv->widget_path)
1630 gtk_widget_path_free (priv->widget_path);
1631 priv->widget_path = NULL;
1635 priv->widget_path = gtk_widget_path_copy (path);
1637 gtk_style_context_invalidate (context);
1641 * gtk_style_context_get_path:
1642 * @context: a #GtkStyleContext
1644 * Returns the widget path used for style matching.
1646 * Returns: (transfer none): A #GtkWidgetPath
1650 const GtkWidgetPath *
1651 gtk_style_context_get_path (GtkStyleContext *context)
1653 GtkStyleContextPrivate *priv;
1655 priv = context->priv;
1656 return priv->widget_path;
1660 * gtk_style_context_set_parent:
1661 * @context: a #GtkStyleContext
1662 * @parent: (allow-none): the new parent or %NULL
1664 * Sets the parent style context for @context. The parent style
1665 * context is used to implement
1666 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1669 * If you are using a #GtkStyleContext returned from
1670 * gtk_widget_get_style_context(), the parent will be set for you.
1675 gtk_style_context_set_parent (GtkStyleContext *context,
1676 GtkStyleContext *parent)
1678 GtkStyleContextPrivate *priv;
1680 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1681 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1683 priv = context->priv;
1685 if (priv->parent == parent)
1689 g_object_ref (parent);
1692 g_object_unref (priv->parent);
1694 priv->parent = parent;
1696 g_object_notify (G_OBJECT (context), "parent");
1697 gtk_style_context_invalidate (context);
1701 * gtk_style_context_get_parent:
1702 * @context: a #GtkStyleContext
1704 * Gets the parent context set via gtk_style_context_set_parent().
1705 * See that function for details.
1707 * Returns: (transfer none): the parent context or %NULL
1712 gtk_style_context_get_parent (GtkStyleContext *context)
1714 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1716 return context->priv->parent;
1720 * gtk_style_context_save:
1721 * @context: a #GtkStyleContext
1723 * Saves the @context state, so all modifications done through
1724 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1725 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1726 * or gtk_style_context_set_junction_sides() can be reverted in one
1727 * go through gtk_style_context_restore().
1732 gtk_style_context_save (GtkStyleContext *context)
1734 GtkStyleContextPrivate *priv;
1737 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1739 priv = context->priv;
1741 g_assert (priv->info_stack != NULL);
1743 info = style_info_copy (priv->info_stack->data);
1744 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1748 * gtk_style_context_restore:
1749 * @context: a #GtkStyleContext
1751 * Restores @context state to a previous stage.
1752 * See gtk_style_context_save().
1757 gtk_style_context_restore (GtkStyleContext *context)
1759 GtkStyleContextPrivate *priv;
1762 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1764 priv = context->priv;
1766 if (priv->info_stack)
1768 info = priv->info_stack->data;
1769 priv->info_stack = g_slist_remove (priv->info_stack, info);
1770 style_info_free (info);
1773 if (!priv->info_stack)
1775 g_warning ("Unpaired gtk_style_context_restore() call");
1777 /* Create default region */
1778 info = style_info_new ();
1779 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1782 priv->current_data = NULL;
1786 style_class_find (GArray *array,
1791 gboolean found = FALSE;
1797 if (!array || array->len == 0)
1801 max = array->len - 1;
1807 mid = (min + max) / 2;
1808 item = g_array_index (array, GQuark, mid);
1810 if (class_quark == item)
1815 else if (class_quark > item)
1816 min = pos = mid + 1;
1823 while (!found && min <= max);
1832 region_find (GArray *array,
1837 gboolean found = FALSE;
1843 if (!array || array->len == 0)
1847 max = array->len - 1;
1853 mid = (min + max) / 2;
1854 region = &g_array_index (array, GtkRegion, mid);
1856 if (region->class_quark == class_quark)
1861 else if (region->class_quark > class_quark)
1862 min = pos = mid + 1;
1869 while (!found && min <= max);
1878 * gtk_style_context_add_class:
1879 * @context: a #GtkStyleContext
1880 * @class_name: class name to use in styling
1882 * Adds a style class to @context, so posterior calls to
1883 * gtk_style_context_get() or any of the gtk_render_*()
1884 * functions will make use of this new class for styling.
1886 * In the CSS file format, a #GtkEntry defining an "entry"
1887 * class, would be matched by:
1890 * GtkEntry.entry { ... }
1893 * While any widget defining an "entry" class would be
1902 gtk_style_context_add_class (GtkStyleContext *context,
1903 const gchar *class_name)
1905 GtkStyleContextPrivate *priv;
1910 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1911 g_return_if_fail (class_name != NULL);
1913 priv = context->priv;
1914 class_quark = g_quark_from_string (class_name);
1916 g_assert (priv->info_stack != NULL);
1917 info = priv->info_stack->data;
1919 if (!style_class_find (info->style_classes, class_quark, &position))
1921 g_array_insert_val (info->style_classes, position, class_quark);
1923 /* Unset current data, as it likely changed due to the class change */
1924 priv->current_data = NULL;
1929 * gtk_style_context_remove_class:
1930 * @context: a #GtkStyleContext
1931 * @class_name: class name to remove
1933 * Removes @class_name from @context.
1938 gtk_style_context_remove_class (GtkStyleContext *context,
1939 const gchar *class_name)
1941 GtkStyleContextPrivate *priv;
1946 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1947 g_return_if_fail (class_name != NULL);
1949 class_quark = g_quark_try_string (class_name);
1954 priv = context->priv;
1956 g_assert (priv->info_stack != NULL);
1957 info = priv->info_stack->data;
1959 if (style_class_find (info->style_classes, class_quark, &position))
1961 g_array_remove_index (info->style_classes, position);
1963 /* Unset current data, as it likely changed due to the class change */
1964 priv->current_data = NULL;
1969 * gtk_style_context_has_class:
1970 * @context: a #GtkStyleContext
1971 * @class_name: a class name
1973 * Returns %TRUE if @context currently has defined the
1976 * Returns: %TRUE if @context has @class_name defined
1981 gtk_style_context_has_class (GtkStyleContext *context,
1982 const gchar *class_name)
1984 GtkStyleContextPrivate *priv;
1988 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1989 g_return_val_if_fail (class_name != NULL, FALSE);
1991 class_quark = g_quark_try_string (class_name);
1996 priv = context->priv;
1998 g_assert (priv->info_stack != NULL);
1999 info = priv->info_stack->data;
2001 if (style_class_find (info->style_classes, class_quark, NULL))
2008 * gtk_style_context_list_classes:
2009 * @context: a #GtkStyleContext
2011 * Returns the list of classes currently defined in @context.
2013 * Returns: (transfer container) (element-type utf8): a #GList of
2014 * strings with the currently defined classes. The contents
2015 * of the list are owned by GTK+, but you must free the list
2016 * itself with g_list_free() when you are done with it.
2021 gtk_style_context_list_classes (GtkStyleContext *context)
2023 GtkStyleContextPrivate *priv;
2025 GList *classes = NULL;
2028 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2030 priv = context->priv;
2032 g_assert (priv->info_stack != NULL);
2033 info = priv->info_stack->data;
2035 for (i = 0; i < info->style_classes->len; i++)
2039 quark = g_array_index (info->style_classes, GQuark, i);
2040 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
2047 * gtk_style_context_list_regions:
2048 * @context: a #GtkStyleContext
2050 * Returns the list of regions currently defined in @context.
2052 * Returns: (transfer container) (element-type utf8): a #GList of
2053 * strings with the currently defined regions. The contents
2054 * of the list are owned by GTK+, but you must free the list
2055 * itself with g_list_free() when you are done with it.
2060 gtk_style_context_list_regions (GtkStyleContext *context)
2062 GtkStyleContextPrivate *priv;
2064 GList *classes = NULL;
2067 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2069 priv = context->priv;
2071 g_assert (priv->info_stack != NULL);
2072 info = priv->info_stack->data;
2074 for (i = 0; i < info->regions->len; i++)
2077 const gchar *class_name;
2079 region = &g_array_index (info->regions, GtkRegion, i);
2081 class_name = g_quark_to_string (region->class_quark);
2082 classes = g_list_prepend (classes, (gchar *) class_name);
2089 _gtk_style_context_check_region_name (const gchar *str)
2091 g_return_val_if_fail (str != NULL, FALSE);
2093 if (!g_ascii_islower (str[0]))
2099 !g_ascii_islower (*str))
2109 * gtk_style_context_add_region:
2110 * @context: a #GtkStyleContext
2111 * @region_name: region name to use in styling
2112 * @flags: flags that apply to the region
2114 * Adds a region to @context, so posterior calls to
2115 * gtk_style_context_get() or any of the gtk_render_*()
2116 * functions will make use of this new region for styling.
2118 * In the CSS file format, a #GtkTreeView defining a "row"
2119 * region, would be matched by:
2122 * GtkTreeView row { ... }
2125 * Pseudo-classes are used for matching @flags, so the two
2128 * GtkTreeView row:nth-child(even) { ... }
2129 * GtkTreeView row:nth-child(odd) { ... }
2132 * would apply to even and odd rows, respectively.
2134 * <note><para>Region names must only contain lowercase letters
2135 * and '-', starting always with a lowercase letter.</para></note>
2140 gtk_style_context_add_region (GtkStyleContext *context,
2141 const gchar *region_name,
2142 GtkRegionFlags flags)
2144 GtkStyleContextPrivate *priv;
2146 GQuark region_quark;
2149 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2150 g_return_if_fail (region_name != NULL);
2151 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2153 priv = context->priv;
2154 region_quark = g_quark_from_string (region_name);
2156 g_assert (priv->info_stack != NULL);
2157 info = priv->info_stack->data;
2159 if (!region_find (info->regions, region_quark, &position))
2163 region.class_quark = region_quark;
2164 region.flags = flags;
2166 g_array_insert_val (info->regions, position, region);
2168 /* Unset current data, as it likely changed due to the region change */
2169 priv->current_data = NULL;
2174 * gtk_style_context_remove_region:
2175 * @context: a #GtkStyleContext
2176 * @region_name: region name to unset
2178 * Removes a region from @context.
2183 gtk_style_context_remove_region (GtkStyleContext *context,
2184 const gchar *region_name)
2186 GtkStyleContextPrivate *priv;
2188 GQuark region_quark;
2191 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2192 g_return_if_fail (region_name != NULL);
2194 region_quark = g_quark_try_string (region_name);
2199 priv = context->priv;
2201 g_assert (priv->info_stack != NULL);
2202 info = priv->info_stack->data;
2204 if (region_find (info->regions, region_quark, &position))
2206 g_array_remove_index (info->regions, position);
2208 /* Unset current data, as it likely changed due to the region change */
2209 priv->current_data = NULL;
2214 * gtk_style_context_has_region:
2215 * @context: a #GtkStyleContext
2216 * @region_name: a region name
2217 * @flags_return: (out) (allow-none): return location for region flags
2219 * Returns %TRUE if @context has the region defined.
2220 * If @flags_return is not %NULL, it is set to the flags
2221 * affecting the region.
2223 * Returns: %TRUE if region is defined
2228 gtk_style_context_has_region (GtkStyleContext *context,
2229 const gchar *region_name,
2230 GtkRegionFlags *flags_return)
2232 GtkStyleContextPrivate *priv;
2234 GQuark region_quark;
2237 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2238 g_return_val_if_fail (region_name != NULL, FALSE);
2243 region_quark = g_quark_try_string (region_name);
2248 priv = context->priv;
2250 g_assert (priv->info_stack != NULL);
2251 info = priv->info_stack->data;
2253 if (region_find (info->regions, region_quark, &position))
2259 region = &g_array_index (info->regions, GtkRegion, position);
2260 *flags_return = region->flags;
2269 style_property_values_cmp (gconstpointer bsearch_node1,
2270 gconstpointer bsearch_node2)
2272 const PropertyValue *val1 = bsearch_node1;
2273 const PropertyValue *val2 = bsearch_node2;
2275 if (val1->widget_type != val2->widget_type)
2276 return val1->widget_type < val2->widget_type ? -1 : 1;
2278 if (val1->pspec != val2->pspec)
2279 return val1->pspec < val2->pspec ? -1 : 1;
2281 if (val1->state != val2->state)
2282 return val1->state < val2->state ? -1 : 1;
2288 _gtk_style_context_peek_property (GtkStyleContext *context,
2289 const char *property_name)
2291 GtkStyleProperty *property;
2294 property = _gtk_style_property_lookup (property_name);
2295 if (!GTK_IS_CSS_STYLE_PROPERTY (property))
2297 g_warning ("Style property \"%s\" does not exist", property_name);
2301 data = style_data_lookup (context, gtk_style_context_get_state (context));
2303 return _gtk_style_properties_peek_property (data->store,
2304 GTK_CSS_STYLE_PROPERTY (property),
2309 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2311 GtkStateFlags state,
2314 GtkStyleContextPrivate *priv;
2315 PropertyValue *pcache, key = { 0 };
2316 GList *global_list = NULL;
2320 priv = context->priv;
2321 data = style_data_lookup (context, state);
2323 key.widget_type = widget_type;
2327 /* need value cache array */
2328 if (!data->property_cache)
2329 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2332 pcache = bsearch (&key,
2333 data->property_cache->data, data->property_cache->len,
2334 sizeof (PropertyValue), style_property_values_cmp);
2336 return &pcache->value;
2340 while (i < data->property_cache->len &&
2341 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2344 g_array_insert_val (data->property_cache, i, key);
2345 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2347 /* cache miss, initialize value type, then set contents */
2348 g_param_spec_ref (pcache->pspec);
2349 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2353 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
2354 global_list = g_list_last (global_list);
2357 if (priv->widget_path)
2359 GList *list, *global, *elem;
2361 list = priv->providers_last;
2362 global = global_list;
2364 while ((elem = find_next_candidate (list, global, FALSE)) != NULL)
2366 GtkStyleProviderData *provider_data;
2368 provider_data = elem->data;
2373 global = global->prev;
2375 if (gtk_style_provider_get_style_property (provider_data->provider,
2376 priv->widget_path, state,
2377 pspec, &pcache->value))
2379 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2380 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2382 GtkSymbolicColor *color;
2385 color = g_value_dup_boxed (&pcache->value);
2387 g_value_unset (&pcache->value);
2389 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2390 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2392 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2394 if (gtk_symbolic_color_resolve (color, data->store, &rgba))
2396 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2397 g_value_set_boxed (&pcache->value, &rgba);
2402 rgb.red = rgba.red * 65535. + 0.5;
2403 rgb.green = rgba.green * 65535. + 0.5;
2404 rgb.blue = rgba.blue * 65535. + 0.5;
2406 g_value_set_boxed (&pcache->value, &rgb);
2410 g_param_value_set_default (pspec, &pcache->value);
2412 gtk_symbolic_color_unref (color);
2415 return &pcache->value;
2420 /* not supplied by any provider, revert to default */
2421 g_param_value_set_default (pspec, &pcache->value);
2423 return &pcache->value;
2427 * gtk_style_context_get_style_property:
2428 * @context: a #GtkStyleContext
2429 * @property_name: the name of the widget style property
2430 * @value: Return location for the property value
2432 * Gets the value for a widget style property.
2434 * When @value is no longer needed, g_value_unset() must be called
2435 * to free any allocated memory.
2438 gtk_style_context_get_style_property (GtkStyleContext *context,
2439 const gchar *property_name,
2442 GtkStyleContextPrivate *priv;
2443 GtkWidgetClass *widget_class;
2444 GtkStateFlags state;
2446 const GValue *peek_value;
2449 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2450 g_return_if_fail (property_name != NULL);
2451 g_return_if_fail (value != NULL);
2453 priv = context->priv;
2455 if (!priv->widget_path)
2458 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2460 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2462 g_warning ("%s: can't get style properties for non-widget class `%s'",
2464 g_type_name (widget_type));
2468 widget_class = g_type_class_ref (widget_type);
2469 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2470 g_type_class_unref (widget_class);
2474 g_warning ("%s: widget class `%s' has no style property named `%s'",
2476 g_type_name (widget_type),
2481 state = gtk_style_context_get_state (context);
2482 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2485 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2486 g_value_copy (peek_value, value);
2487 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2488 g_value_transform (peek_value, value);
2490 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2492 G_VALUE_TYPE_NAME (peek_value),
2493 G_VALUE_TYPE_NAME (value));
2497 * gtk_style_context_get_style_valist:
2498 * @context: a #GtkStyleContext
2499 * @args: va_list of property name/return location pairs, followed by %NULL
2501 * Retrieves several widget style properties from @context according to the
2507 gtk_style_context_get_style_valist (GtkStyleContext *context,
2510 GtkStyleContextPrivate *priv;
2511 const gchar *prop_name;
2512 GtkStateFlags state;
2515 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2517 prop_name = va_arg (args, const gchar *);
2518 priv = context->priv;
2520 if (!priv->widget_path)
2523 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2525 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2527 g_warning ("%s: can't get style properties for non-widget class `%s'",
2529 g_type_name (widget_type));
2533 state = gtk_style_context_get_state (context);
2537 GtkWidgetClass *widget_class;
2539 const GValue *peek_value;
2542 widget_class = g_type_class_ref (widget_type);
2543 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2544 g_type_class_unref (widget_class);
2548 g_warning ("%s: widget class `%s' has no style property named `%s'",
2550 g_type_name (widget_type),
2555 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2558 G_VALUE_LCOPY (peek_value, args, 0, &error);
2562 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2564 G_VALUE_TYPE_NAME (peek_value),
2569 prop_name = va_arg (args, const gchar *);
2574 * gtk_style_context_get_style:
2575 * @context: a #GtkStyleContext
2576 * @...: property name /return value pairs, followed by %NULL
2578 * Retrieves several widget style properties from @context according to the
2584 gtk_style_context_get_style (GtkStyleContext *context,
2589 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2591 va_start (args, context);
2592 gtk_style_context_get_style_valist (context, args);
2598 * gtk_style_context_lookup_icon_set:
2599 * @context: a #GtkStyleContext
2600 * @stock_id: an icon name
2602 * Looks up @stock_id in the icon factories associated to @context and
2603 * the default icon factory, returning an icon set if found, otherwise
2606 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2609 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2610 const gchar *stock_id)
2612 GtkStyleContextPrivate *priv;
2616 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2617 g_return_val_if_fail (stock_id != NULL, NULL);
2619 priv = context->priv;
2620 g_return_val_if_fail (priv->widget_path != NULL, NULL);
2622 data = style_data_lookup (context, 0);
2624 for (list = data->icon_factories; list; list = list->next)
2626 GtkIconFactory *factory;
2627 GtkIconSet *icon_set;
2629 factory = list->data;
2630 icon_set = gtk_icon_factory_lookup (factory, stock_id);
2636 return gtk_icon_factory_lookup_default (stock_id);
2640 * gtk_style_context_set_screen:
2641 * @context: a #GtkStyleContext
2642 * @screen: a #GdkScreen
2644 * Attaches @context to the given screen.
2646 * The screen is used to add style information from 'global' style
2647 * providers, such as the screens #GtkSettings instance.
2649 * If you are using a #GtkStyleContext returned from
2650 * gtk_widget_get_style_context(), you do not need to
2651 * call this yourself.
2656 gtk_style_context_set_screen (GtkStyleContext *context,
2659 GtkStyleContextPrivate *priv;
2661 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2662 g_return_if_fail (GDK_IS_SCREEN (screen));
2664 priv = context->priv;
2665 if (priv->screen == screen)
2668 priv->screen = screen;
2670 g_object_notify (G_OBJECT (context), "screen");
2672 gtk_style_context_invalidate (context);
2676 * gtk_style_context_get_screen:
2677 * @context: a #GtkStyleContext
2679 * Returns the #GdkScreen to which @context is attached.
2681 * Returns: (transfer none): a #GdkScreen.
2684 gtk_style_context_get_screen (GtkStyleContext *context)
2686 GtkStyleContextPrivate *priv;
2688 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2690 priv = context->priv;
2691 return priv->screen;
2695 * gtk_style_context_set_direction:
2696 * @context: a #GtkStyleContext
2697 * @direction: the new direction.
2699 * Sets the reading direction for rendering purposes.
2701 * If you are using a #GtkStyleContext returned from
2702 * gtk_widget_get_style_context(), you do not need to
2703 * call this yourself.
2708 gtk_style_context_set_direction (GtkStyleContext *context,
2709 GtkTextDirection direction)
2711 GtkStyleContextPrivate *priv;
2713 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2715 priv = context->priv;
2716 priv->direction = direction;
2718 g_object_notify (G_OBJECT (context), "direction");
2722 * gtk_style_context_get_direction:
2723 * @context: a #GtkStyleContext
2725 * Returns the widget direction used for rendering.
2727 * Returns: the widget direction
2732 gtk_style_context_get_direction (GtkStyleContext *context)
2734 GtkStyleContextPrivate *priv;
2736 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2738 priv = context->priv;
2739 return priv->direction;
2743 * gtk_style_context_set_junction_sides:
2744 * @context: a #GtkStyleContext
2745 * @sides: sides where rendered elements are visually connected to
2748 * Sets the sides where rendered elements (mostly through
2749 * gtk_render_frame()) will visually connect with other visual elements.
2751 * This is merely a hint that may or may not be honored
2752 * by theming engines.
2754 * Container widgets are expected to set junction hints as appropriate
2755 * for their children, so it should not normally be necessary to call
2756 * this function manually.
2761 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2762 GtkJunctionSides sides)
2764 GtkStyleContextPrivate *priv;
2767 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2769 priv = context->priv;
2770 info = priv->info_stack->data;
2771 info->junction_sides = sides;
2775 * gtk_style_context_get_junction_sides:
2776 * @context: a #GtkStyleContext
2778 * Returns the sides where rendered elements connect visually with others.
2780 * Returns: the junction sides
2785 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2787 GtkStyleContextPrivate *priv;
2790 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2792 priv = context->priv;
2793 info = priv->info_stack->data;
2794 return info->junction_sides;
2797 static GtkSymbolicColor *
2798 gtk_style_context_color_lookup_func (gpointer contextp,
2801 GtkSymbolicColor *sym_color;
2802 GtkStyleContext *context = contextp;
2803 GtkStyleContextPrivate *priv = context->priv;
2804 GList *elem, *list, *global_list = NULL;
2806 list = priv->providers_last;
2809 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
2810 global_list = g_list_last (global_list);
2815 while (sym_color == NULL &&
2816 (elem = find_next_candidate (list, global_list, FALSE)) != NULL)
2818 GtkStyleProviderData *data;
2825 global_list = global_list->prev;
2827 if (GTK_IS_STYLE_PROVIDER_PRIVATE (data->provider))
2829 sym_color = _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (data->provider),
2834 /* If somebody hits this code path, shout at them */
2843 _gtk_style_context_resolve_color (GtkStyleContext *context,
2844 GtkSymbolicColor *color,
2847 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2848 g_return_val_if_fail (color != NULL, FALSE);
2849 g_return_val_if_fail (result != NULL, FALSE);
2851 return _gtk_symbolic_color_resolve_full (color,
2852 gtk_style_context_color_lookup_func,
2858 * gtk_style_context_lookup_color:
2859 * @context: a #GtkStyleContext
2860 * @color_name: color name to lookup
2861 * @color: (out): Return location for the looked up color
2863 * Looks up and resolves a color name in the @context color map.
2865 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2868 gtk_style_context_lookup_color (GtkStyleContext *context,
2869 const gchar *color_name,
2872 GtkSymbolicColor *sym_color;
2874 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2875 g_return_val_if_fail (color_name != NULL, FALSE);
2876 g_return_val_if_fail (color != NULL, FALSE);
2878 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2879 if (sym_color == NULL)
2882 return _gtk_style_context_resolve_color (context, sym_color, color);
2886 * gtk_style_context_notify_state_change:
2887 * @context: a #GtkStyleContext
2888 * @window: a #GdkWindow
2889 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2890 * See gtk_style_context_push_animatable_region()
2891 * @state: state to trigger transition for
2892 * @state_value: %TRUE if @state is the state we are changing to,
2893 * %FALSE if we are changing away from it
2895 * Notifies a state change on @context, so if the current style makes use
2896 * of transition animations, one will be started so all rendered elements
2897 * under @region_id are animated for state @state being set to value
2900 * The @window parameter is used in order to invalidate the rendered area
2901 * as the animation runs, so make sure it is the same window that is being
2902 * rendered on by the gtk_render_*() functions.
2904 * If @region_id is %NULL, all rendered elements using @context will be
2905 * affected by this state transition.
2907 * As a practical example, a #GtkButton notifying a state transition on
2908 * the prelight state:
2910 * gtk_style_context_notify_state_change (context,
2911 * gtk_widget_get_window (widget),
2913 * GTK_STATE_PRELIGHT,
2914 * button->in_button);
2917 * Can be handled in the CSS file like this:
2920 * background-color: #f00
2924 * background-color: #fff;
2925 * transition: 200ms linear
2929 * This combination will animate the button background from red to white
2930 * if a pointer enters the button, and back to red if the pointer leaves
2933 * Note that @state is used when finding the transition parameters, which
2934 * is why the style places the transition under the :hover pseudo-class.
2939 gtk_style_context_notify_state_change (GtkStyleContext *context,
2943 gboolean state_value)
2945 GtkStyleContextPrivate *priv;
2946 GtkAnimationDescription *desc;
2947 AnimationInfo *info;
2948 GtkStateFlags flags;
2951 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2952 g_return_if_fail (GDK_IS_WINDOW (window));
2953 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2955 priv = context->priv;
2956 g_return_if_fail (priv->widget_path != NULL);
2958 state_value = (state_value == TRUE);
2962 case GTK_STATE_ACTIVE:
2963 flags = GTK_STATE_FLAG_ACTIVE;
2965 case GTK_STATE_PRELIGHT:
2966 flags = GTK_STATE_FLAG_PRELIGHT;
2968 case GTK_STATE_SELECTED:
2969 flags = GTK_STATE_FLAG_SELECTED;
2971 case GTK_STATE_INSENSITIVE:
2972 flags = GTK_STATE_FLAG_INSENSITIVE;
2974 case GTK_STATE_INCONSISTENT:
2975 flags = GTK_STATE_FLAG_INCONSISTENT;
2977 case GTK_STATE_FOCUSED:
2978 flags = GTK_STATE_FLAG_FOCUSED;
2980 case GTK_STATE_NORMAL:
2986 /* Find out if there is any animation description for the given
2987 * state, it will fallback to the normal state as well if necessary.
2989 data = style_data_lookup (context, flags);
2990 gtk_style_properties_get (data->store, 0,
2991 "transition", &desc,
2997 if (_gtk_animation_description_get_duration (desc) == 0)
2999 _gtk_animation_description_unref (desc);
3003 info = animation_info_lookup (context, region_id, state);
3006 info->target_value != state_value)
3008 /* Target values are the opposite */
3009 if (!_gtk_timeline_get_loop (info->timeline))
3011 /* Reverse the animation */
3012 if (_gtk_timeline_get_direction (info->timeline) == GTK_TIMELINE_DIRECTION_FORWARD)
3013 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
3015 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_FORWARD);
3017 info->target_value = state_value;
3021 /* Take it out of its looping state */
3022 _gtk_timeline_set_loop (info->timeline, FALSE);
3026 (!_gtk_animation_description_get_loop (desc) ||
3029 info = animation_info_new (context, region_id,
3030 _gtk_animation_description_get_duration (desc),
3031 _gtk_animation_description_get_progress_type (desc),
3032 _gtk_animation_description_get_loop (desc),
3033 state, state_value, window);
3035 priv->animations = g_slist_prepend (priv->animations, info);
3036 priv->animations_invalidated = TRUE;
3039 _gtk_animation_description_unref (desc);
3043 * gtk_style_context_cancel_animations:
3044 * @context: a #GtkStyleContext
3045 * @region_id: (allow-none): animatable region to stop, or %NULL.
3046 * See gtk_style_context_push_animatable_region()
3048 * Stops all running animations for @region_id and all animatable
3049 * regions underneath.
3051 * A %NULL @region_id will stop all ongoing animations in @context,
3052 * when dealing with a #GtkStyleContext obtained through
3053 * gtk_widget_get_style_context(), this is normally done for you
3054 * in all circumstances you would expect all widget to be stopped,
3055 * so this should be only used in complex widgets with different
3056 * animatable regions.
3061 gtk_style_context_cancel_animations (GtkStyleContext *context,
3064 GtkStyleContextPrivate *priv;
3065 AnimationInfo *info;
3068 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3070 priv = context->priv;
3071 l = priv->animations;
3079 info->region_id == region_id ||
3080 g_slist_find (info->parent_regions, region_id))
3082 priv->animations = g_slist_remove (priv->animations, info);
3083 animation_info_free (info);
3089 is_parent_of (GdkWindow *parent,
3092 GtkWidget *child_widget, *parent_widget;
3095 gdk_window_get_user_data (child, (gpointer *) &child_widget);
3096 gdk_window_get_user_data (parent, (gpointer *) &parent_widget);
3098 if (child_widget != parent_widget &&
3099 !gtk_widget_is_ancestor (child_widget, parent_widget))
3106 if (window == parent)
3109 window = gdk_window_get_parent (window);
3116 * gtk_style_context_scroll_animations:
3117 * @context: a #GtkStyleContext
3118 * @window: a #GdkWindow used previously in
3119 * gtk_style_context_notify_state_change()
3120 * @dx: Amount to scroll in the X axis
3121 * @dy: Amount to scroll in the Y axis
3123 * This function is analogous to gdk_window_scroll(), and
3124 * should be called together with it so the invalidation
3125 * areas for any ongoing animation are scrolled together
3131 gtk_style_context_scroll_animations (GtkStyleContext *context,
3136 GtkStyleContextPrivate *priv;
3137 AnimationInfo *info;
3140 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3141 g_return_if_fail (GDK_IS_WINDOW (window));
3143 priv = context->priv;
3144 l = priv->animations;
3151 if (info->invalidation_region &&
3152 (window == info->window ||
3153 is_parent_of (window, info->window)))
3154 cairo_region_translate (info->invalidation_region, dx, dy);
3159 * gtk_style_context_push_animatable_region:
3160 * @context: a #GtkStyleContext
3161 * @region_id: unique identifier for the animatable region
3163 * Pushes an animatable region, so all further gtk_render_*() calls between
3164 * this call and the following gtk_style_context_pop_animatable_region()
3165 * will potentially show transition animations for this region if
3166 * gtk_style_context_notify_state_change() is called for a given state,
3167 * and the current theme/style defines transition animations for state
3170 * The @region_id used must be unique in @context so the theming engine
3171 * can uniquely identify rendered elements subject to a state transition.
3176 gtk_style_context_push_animatable_region (GtkStyleContext *context,
3179 GtkStyleContextPrivate *priv;
3181 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3182 g_return_if_fail (region_id != NULL);
3184 priv = context->priv;
3185 priv->animation_regions = g_slist_prepend (priv->animation_regions, region_id);
3189 * gtk_style_context_pop_animatable_region:
3190 * @context: a #GtkStyleContext
3192 * Pops an animatable region from @context.
3193 * See gtk_style_context_push_animatable_region().
3198 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
3200 GtkStyleContextPrivate *priv;
3202 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3204 priv = context->priv;
3205 priv->animation_regions = g_slist_delete_link (priv->animation_regions,
3206 priv->animation_regions);
3210 _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context)
3212 GtkStyleContextPrivate *priv;
3215 priv = context->priv;
3217 for (l = priv->animations; l; l = l->next)
3219 AnimationInfo *info;
3223 /* A NULL invalidation region means it has to be recreated on
3224 * the next expose event, this happens usually after a widget
3225 * allocation change, so the next expose after it will update
3226 * the invalidation region.
3228 if (info->invalidation_region)
3230 cairo_region_destroy (info->invalidation_region);
3231 info->invalidation_region = NULL;
3235 priv->animations_invalidated = TRUE;
3239 _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context,
3242 GtkStyleContextPrivate *priv;
3245 priv = context->priv;
3247 if (!priv->animations_invalidated)
3250 l = priv->animations;
3254 AnimationInfo *info;
3263 if (info->invalidation_region)
3266 if (info->rectangles->len == 0)
3269 info->invalidation_region = cairo_region_create ();
3270 _gtk_widget_get_translation_to_window (widget, info->window, &rel_x, &rel_y);
3272 for (i = 0; i < info->rectangles->len; i++)
3274 cairo_rectangle_int_t *rect;
3276 rect = &g_array_index (info->rectangles, cairo_rectangle_int_t, i);
3278 /* These are widget relative coordinates,
3279 * so have them inverted to be window relative
3284 cairo_region_union_rectangle (info->invalidation_region, rect);
3287 g_array_remove_range (info->rectangles, 0, info->rectangles->len);
3290 priv->animations_invalidated = FALSE;
3294 store_animation_region (GtkStyleContext *context,
3300 GtkStyleContextPrivate *priv;
3303 priv = context->priv;
3305 if (!priv->animations_invalidated)
3308 for (l = priv->animations; l; l = l->next)
3310 AnimationInfo *info;
3314 /* The animation doesn't need updating
3315 * the invalidation area, bail out.
3317 if (info->invalidation_region)
3320 if (context_has_animatable_region (context, info->region_id))
3322 cairo_rectangle_int_t rect;
3326 rect.width = (gint) width;
3327 rect.height = (gint) height;
3329 g_array_append_val (info->rectangles, rect);
3331 if (!info->parent_regions)
3333 GSList *parent_regions;
3335 parent_regions = g_slist_find (priv->animation_regions, info->region_id);
3336 info->parent_regions = g_slist_copy (parent_regions);
3343 * gtk_style_context_invalidate:
3344 * @context: a #GtkStyleContext.
3346 * Invalidates @context style information, so it will be reconstructed
3349 * If you're using a #GtkStyleContext returned from
3350 * gtk_widget_get_style_context(), you do not need to
3351 * call this yourself.
3356 gtk_style_context_invalidate (GtkStyleContext *context)
3358 GtkStyleContextPrivate *priv;
3360 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3362 priv = context->priv;
3364 /* Avoid reentrancy */
3365 if (priv->invalidating_context)
3368 priv->invalidating_context = TRUE;
3370 g_hash_table_remove_all (priv->style_data);
3371 priv->current_data = NULL;
3373 g_signal_emit (context, signals[CHANGED], 0);
3375 priv->invalidating_context = FALSE;
3379 * gtk_style_context_set_background:
3380 * @context: a #GtkStyleContext
3381 * @window: a #GdkWindow
3383 * Sets the background of @window to the background pattern or
3384 * color specified in @context for its current state.
3389 gtk_style_context_set_background (GtkStyleContext *context,
3392 GtkStateFlags state;
3393 cairo_pattern_t *pattern;
3396 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3397 g_return_if_fail (GDK_IS_WINDOW (window));
3399 state = gtk_style_context_get_state (context);
3400 gtk_style_context_get (context, state,
3401 "background-image", &pattern,
3405 gdk_window_set_background_pattern (window, pattern);
3406 cairo_pattern_destroy (pattern);
3410 gtk_style_context_get (context, state,
3411 "background-color", &color,
3415 gdk_window_set_background_rgba (window, color);
3416 gdk_rgba_free (color);
3421 * gtk_style_context_get_color:
3422 * @context: a #GtkStyleContext
3423 * @state: state to retrieve the color for
3424 * @color: (out): return value for the foreground color
3426 * Gets the foreground color for a given state.
3431 gtk_style_context_get_color (GtkStyleContext *context,
3432 GtkStateFlags state,
3437 g_return_if_fail (color != NULL);
3438 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3440 gtk_style_context_get (context,
3450 * gtk_style_context_get_background_color:
3451 * @context: a #GtkStyleContext
3452 * @state: state to retrieve the color for
3453 * @color: (out): return value for the background color
3455 * Gets the background color for a given state.
3460 gtk_style_context_get_background_color (GtkStyleContext *context,
3461 GtkStateFlags state,
3466 g_return_if_fail (color != NULL);
3467 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3469 gtk_style_context_get (context,
3471 "background-color", &c,
3479 * gtk_style_context_get_border_color:
3480 * @context: a #GtkStyleContext
3481 * @state: state to retrieve the color for
3482 * @color: (out): return value for the border color
3484 * Gets the border color for a given state.
3489 gtk_style_context_get_border_color (GtkStyleContext *context,
3490 GtkStateFlags state,
3495 g_return_if_fail (color != NULL);
3496 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3498 gtk_style_context_get (context,
3508 * gtk_style_context_get_border:
3509 * @context: a #GtkStyleContext
3510 * @state: state to retrieve the border for
3511 * @border: (out): return value for the border settings
3513 * Gets the border for a given state as a #GtkBorder.
3514 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3519 gtk_style_context_get_border (GtkStyleContext *context,
3520 GtkStateFlags state,
3523 GtkStyleContextPrivate *priv;
3525 int top, left, bottom, right;
3527 g_return_if_fail (border != NULL);
3528 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3530 priv = context->priv;
3531 g_return_if_fail (priv->widget_path != NULL);
3533 data = style_data_lookup (context, state);
3534 gtk_style_properties_get (data->store,
3536 "border-top-width", &top,
3537 "border-left-width", &left,
3538 "border-bottom-width", &bottom,
3539 "border-right-width", &right,
3543 border->left = left;
3544 border->bottom = bottom;
3545 border->right = right;
3549 * gtk_style_context_get_padding:
3550 * @context: a #GtkStyleContext
3551 * @state: state to retrieve the padding for
3552 * @padding: (out): return value for the padding settings
3554 * Gets the padding for a given state as a #GtkBorder.
3555 * See %GTK_STYLE_PROPERTY_PADDING.
3560 gtk_style_context_get_padding (GtkStyleContext *context,
3561 GtkStateFlags state,
3564 GtkStyleContextPrivate *priv;
3566 int top, left, bottom, right;
3568 g_return_if_fail (padding != NULL);
3569 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3571 priv = context->priv;
3572 g_return_if_fail (priv->widget_path != NULL);
3574 data = style_data_lookup (context, state);
3575 gtk_style_properties_get (data->store,
3577 "padding-top", &top,
3578 "padding-left", &left,
3579 "padding-bottom", &bottom,
3580 "padding-right", &right,
3584 padding->left = left;
3585 padding->bottom = bottom;
3586 padding->right = right;
3590 * gtk_style_context_get_margin:
3591 * @context: a #GtkStyleContext
3592 * @state: state to retrieve the border for
3593 * @margin: (out): return value for the margin settings
3595 * Gets the margin for a given state as a #GtkBorder.
3596 * See %GTK_STYLE_PROPERTY_MARGIN.
3601 gtk_style_context_get_margin (GtkStyleContext *context,
3602 GtkStateFlags state,
3605 GtkStyleContextPrivate *priv;
3607 int top, left, bottom, right;
3609 g_return_if_fail (margin != NULL);
3610 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3612 priv = context->priv;
3613 g_return_if_fail (priv->widget_path != NULL);
3615 data = style_data_lookup (context, state);
3616 gtk_style_properties_get (data->store,
3619 "margin-left", &left,
3620 "margin-bottom", &bottom,
3621 "margin-right", &right,
3625 margin->left = left;
3626 margin->bottom = bottom;
3627 margin->right = right;
3631 * gtk_style_context_get_font:
3632 * @context: a #GtkStyleContext
3633 * @state: state to retrieve the font for
3635 * Returns the font description for a given state. The returned
3636 * object is const and will remain valid until the
3637 * #GtkStyleContext::changed signal happens.
3639 * Returns: (transfer none): the #PangoFontDescription for the given
3640 * state. This object is owned by GTK+ and should not be
3645 const PangoFontDescription *
3646 gtk_style_context_get_font (GtkStyleContext *context,
3647 GtkStateFlags state)
3649 GtkStyleContextPrivate *priv;
3651 GHashTable *font_cache;
3652 PangoFontDescription *description;
3654 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3656 priv = context->priv;
3657 g_return_val_if_fail (priv->widget_path != NULL, NULL);
3659 data = style_data_lookup (context, state);
3661 /* Yuck, fonts are created on-demand but we don't return a ref.
3662 * Do bad things to achieve this requirement */
3663 font_cache = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3666 description = g_hash_table_lookup (font_cache, GUINT_TO_POINTER (state));
3670 font_cache = g_hash_table_new_full (NULL, NULL, NULL, (GDestroyNotify) pango_font_description_free);
3671 g_object_set_data_full (G_OBJECT (data->store),
3672 "font-cache-for-get_font",
3674 (GDestroyNotify) g_hash_table_unref);
3678 if (description == NULL)
3680 gtk_style_properties_get (data->store, 0, "font", &description, NULL);
3681 g_hash_table_insert (font_cache, GUINT_TO_POINTER (state), description);
3688 get_cursor_color (GtkStyleContext *context,
3692 GdkColor *style_color;
3694 gtk_style_context_get_style (context,
3695 primary ? "cursor-color" : "secondary-cursor-color",
3701 color->red = style_color->red / 65535.0;
3702 color->green = style_color->green / 65535.0;
3703 color->blue = style_color->blue / 65535.0;
3706 gdk_color_free (style_color);
3710 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3716 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3718 color->red = (color->red + bg.red) * 0.5;
3719 color->green = (color->green + bg.green) * 0.5;
3720 color->blue = (color->blue + bg.blue) * 0.5;
3726 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3727 GdkRGBA *primary_color,
3728 GdkRGBA *secondary_color)
3731 get_cursor_color (context, TRUE, primary_color);
3733 if (secondary_color)
3734 get_cursor_color (context, FALSE, secondary_color);
3741 * @context: a #GtkStyleContext
3743 * @x: X origin of the rectangle
3744 * @y: Y origin of the rectangle
3745 * @width: rectangle width
3746 * @height: rectangle height
3748 * Renders a checkmark (as in a #GtkCheckButton).
3750 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3751 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3752 * should be marked as undefined.
3755 * <title>Typical checkmark rendering</title>
3756 * <inlinegraphic fileref="checks.png" format="PNG"/>
3762 gtk_render_check (GtkStyleContext *context,
3769 GtkStyleContextPrivate *priv;
3770 GtkThemingEngineClass *engine_class;
3772 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3773 g_return_if_fail (cr != NULL);
3775 if (width <= 0 || height <= 0)
3778 priv = context->priv;
3779 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3783 store_animation_region (context, x, y, width, height);
3785 _gtk_theming_engine_set_context (priv->theming_engine, context);
3786 engine_class->render_check (priv->theming_engine, cr,
3787 x, y, width, height);
3793 * gtk_render_option:
3794 * @context: a #GtkStyleContext
3796 * @x: X origin of the rectangle
3797 * @y: Y origin of the rectangle
3798 * @width: rectangle width
3799 * @height: rectangle height
3801 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3802 * state will determine whether the option is on or off, and
3803 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3806 * <title>Typical option mark rendering</title>
3807 * <inlinegraphic fileref="options.png" format="PNG"/>
3813 gtk_render_option (GtkStyleContext *context,
3820 GtkStyleContextPrivate *priv;
3821 GtkThemingEngineClass *engine_class;
3823 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3824 g_return_if_fail (cr != NULL);
3826 if (width <= 0 || height <= 0)
3829 priv = context->priv;
3830 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3834 store_animation_region (context, x, y, width, height);
3836 _gtk_theming_engine_set_context (priv->theming_engine, context);
3837 engine_class->render_option (priv->theming_engine, cr,
3838 x, y, width, height);
3845 * @context: a #GtkStyleContext
3847 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3848 * @x: X origin of the render area
3849 * @y: Y origin of the render area
3850 * @size: square side for render area
3852 * Renders an arrow pointing to @angle.
3855 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3856 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3862 gtk_render_arrow (GtkStyleContext *context,
3869 GtkStyleContextPrivate *priv;
3870 GtkThemingEngineClass *engine_class;
3872 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3873 g_return_if_fail (cr != NULL);
3878 priv = context->priv;
3879 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3883 gtk_style_context_save (context);
3884 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3886 store_animation_region (context, x, y, size, size);
3888 _gtk_theming_engine_set_context (priv->theming_engine, context);
3889 engine_class->render_arrow (priv->theming_engine, cr,
3892 gtk_style_context_restore (context);
3897 * gtk_render_background:
3898 * @context: a #GtkStyleContext
3900 * @x: X origin of the rectangle
3901 * @y: Y origin of the rectangle
3902 * @width: rectangle width
3903 * @height: rectangle height
3905 * Renders the background of an element.
3908 * <title>Typical background rendering, showing the effect of
3909 * <parameter>background-image</parameter>,
3910 * <parameter>border-width</parameter> and
3911 * <parameter>border-radius</parameter></title>
3912 * <inlinegraphic fileref="background.png" format="PNG"/>
3918 gtk_render_background (GtkStyleContext *context,
3925 GtkStyleContextPrivate *priv;
3926 GtkThemingEngineClass *engine_class;
3928 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3929 g_return_if_fail (cr != NULL);
3931 if (width <= 0 || height <= 0)
3934 priv = context->priv;
3935 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3939 store_animation_region (context, x, y, width, height);
3941 _gtk_theming_engine_set_context (priv->theming_engine, context);
3942 engine_class->render_background (priv->theming_engine, cr, x, y, width, height);
3949 * @context: a #GtkStyleContext
3951 * @x: X origin of the rectangle
3952 * @y: Y origin of the rectangle
3953 * @width: rectangle width
3954 * @height: rectangle height
3956 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3959 * <title>Examples of frame rendering, showing the effect of
3960 * <parameter>border-image</parameter>,
3961 * <parameter>border-color</parameter>,
3962 * <parameter>border-width</parameter>,
3963 * <parameter>border-radius</parameter> and
3965 * <inlinegraphic fileref="frames.png" format="PNG"/>
3971 gtk_render_frame (GtkStyleContext *context,
3978 GtkStyleContextPrivate *priv;
3979 GtkThemingEngineClass *engine_class;
3981 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3982 g_return_if_fail (cr != NULL);
3984 if (width <= 0 || height <= 0)
3987 priv = context->priv;
3988 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3992 store_animation_region (context, x, y, width, height);
3994 _gtk_theming_engine_set_context (priv->theming_engine, context);
3995 engine_class->render_frame (priv->theming_engine, cr, x, y, width, height);
4001 * gtk_render_expander:
4002 * @context: a #GtkStyleContext
4004 * @x: X origin of the rectangle
4005 * @y: Y origin of the rectangle
4006 * @width: rectangle width
4007 * @height: rectangle height
4009 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
4010 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
4011 * determines whether the expander is collapsed or expanded.
4014 * <title>Typical expander rendering</title>
4015 * <inlinegraphic fileref="expanders.png" format="PNG"/>
4021 gtk_render_expander (GtkStyleContext *context,
4028 GtkStyleContextPrivate *priv;
4029 GtkThemingEngineClass *engine_class;
4031 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4032 g_return_if_fail (cr != NULL);
4034 if (width <= 0 || height <= 0)
4037 priv = context->priv;
4038 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4042 store_animation_region (context, x, y, width, height);
4044 _gtk_theming_engine_set_context (priv->theming_engine, context);
4045 engine_class->render_expander (priv->theming_engine, cr, x, y, width, height);
4052 * @context: a #GtkStyleContext
4054 * @x: X origin of the rectangle
4055 * @y: Y origin of the rectangle
4056 * @width: rectangle width
4057 * @height: rectangle height
4059 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
4061 * <title>Typical focus rendering</title>
4062 * <inlinegraphic fileref="focus.png" format="PNG"/>
4068 gtk_render_focus (GtkStyleContext *context,
4075 GtkStyleContextPrivate *priv;
4076 GtkThemingEngineClass *engine_class;
4078 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4079 g_return_if_fail (cr != NULL);
4081 if (width <= 0 || height <= 0)
4084 priv = context->priv;
4085 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4089 store_animation_region (context, x, y, width, height);
4091 _gtk_theming_engine_set_context (priv->theming_engine, context);
4092 engine_class->render_focus (priv->theming_engine, cr, x, y, width, height);
4098 * gtk_render_layout:
4099 * @context: a #GtkStyleContext
4103 * @layout: the #PangoLayout to render
4105 * Renders @layout on the coordinates @x, @y
4110 gtk_render_layout (GtkStyleContext *context,
4114 PangoLayout *layout)
4116 GtkStyleContextPrivate *priv;
4117 GtkThemingEngineClass *engine_class;
4118 PangoRectangle extents;
4120 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4121 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4122 g_return_if_fail (cr != NULL);
4124 priv = context->priv;
4125 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4129 pango_layout_get_extents (layout, &extents, NULL);
4131 store_animation_region (context,
4137 _gtk_theming_engine_set_context (priv->theming_engine, context);
4138 engine_class->render_layout (priv->theming_engine, cr, x, y, layout);
4145 * @context: a #GtkStyleContext
4147 * @x0: X coordinate for the origin of the line
4148 * @y0: Y coordinate for the origin of the line
4149 * @x1: X coordinate for the end of the line
4150 * @y1: Y coordinate for the end of the line
4152 * Renders a line from (x0, y0) to (x1, y1).
4157 gtk_render_line (GtkStyleContext *context,
4164 GtkStyleContextPrivate *priv;
4165 GtkThemingEngineClass *engine_class;
4167 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4168 g_return_if_fail (cr != NULL);
4170 priv = context->priv;
4171 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4175 _gtk_theming_engine_set_context (priv->theming_engine, context);
4176 engine_class->render_line (priv->theming_engine, cr, x0, y0, x1, y1);
4182 * gtk_render_slider:
4183 * @context: a #GtkStyleContext
4185 * @x: X origin of the rectangle
4186 * @y: Y origin of the rectangle
4187 * @width: rectangle width
4188 * @height: rectangle height
4189 * @orientation: orientation of the slider
4191 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4192 * @width, @height. @orientation defines whether the slider is vertical
4196 * <title>Typical slider rendering</title>
4197 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4203 gtk_render_slider (GtkStyleContext *context,
4209 GtkOrientation orientation)
4211 GtkStyleContextPrivate *priv;
4212 GtkThemingEngineClass *engine_class;
4214 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4215 g_return_if_fail (cr != NULL);
4217 if (width <= 0 || height <= 0)
4220 priv = context->priv;
4221 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4225 store_animation_region (context, x, y, width, height);
4227 _gtk_theming_engine_set_context (priv->theming_engine, context);
4228 engine_class->render_slider (priv->theming_engine, cr, x, y, width, height, orientation);
4234 * gtk_render_frame_gap:
4235 * @context: a #GtkStyleContext
4237 * @x: X origin of the rectangle
4238 * @y: Y origin of the rectangle
4239 * @width: rectangle width
4240 * @height: rectangle height
4241 * @gap_side: side where the gap is
4242 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4243 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4245 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4246 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4247 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4248 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4251 * <title>Typical rendering of a frame with a gap</title>
4252 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4258 gtk_render_frame_gap (GtkStyleContext *context,
4264 GtkPositionType gap_side,
4268 GtkStyleContextPrivate *priv;
4269 GtkThemingEngineClass *engine_class;
4271 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4272 g_return_if_fail (cr != NULL);
4273 g_return_if_fail (xy0_gap <= xy1_gap);
4274 g_return_if_fail (xy0_gap >= 0);
4276 if (width <= 0 || height <= 0)
4279 if (gap_side == GTK_POS_LEFT ||
4280 gap_side == GTK_POS_RIGHT)
4281 g_return_if_fail (xy1_gap <= height);
4283 g_return_if_fail (xy1_gap <= width);
4285 priv = context->priv;
4286 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4290 store_animation_region (context, x, y, width, height);
4292 _gtk_theming_engine_set_context (priv->theming_engine, context);
4293 engine_class->render_frame_gap (priv->theming_engine, cr,
4294 x, y, width, height, gap_side,
4301 * gtk_render_extension:
4302 * @context: a #GtkStyleContext
4304 * @x: X origin of the rectangle
4305 * @y: Y origin of the rectangle
4306 * @width: rectangle width
4307 * @height: rectangle height
4308 * @gap_side: side where the gap is
4310 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4311 * defined by @x, @y, @width, @height. The side where the extension
4312 * connects to is defined by @gap_side.
4315 * <title>Typical extension rendering</title>
4316 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4322 gtk_render_extension (GtkStyleContext *context,
4328 GtkPositionType gap_side)
4330 GtkStyleContextPrivate *priv;
4331 GtkThemingEngineClass *engine_class;
4333 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4334 g_return_if_fail (cr != NULL);
4336 if (width <= 0 || height <= 0)
4339 priv = context->priv;
4340 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4344 store_animation_region (context, x, y, width, height);
4346 _gtk_theming_engine_set_context (priv->theming_engine, context);
4347 engine_class->render_extension (priv->theming_engine, cr, x, y, width, height, gap_side);
4353 * gtk_render_handle:
4354 * @context: a #GtkStyleContext
4356 * @x: X origin of the rectangle
4357 * @y: Y origin of the rectangle
4358 * @width: rectangle width
4359 * @height: rectangle height
4361 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4362 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4363 * determined by @x, @y, @width, @height.
4366 * <title>Handles rendered for the paned and grip classes</title>
4367 * <inlinegraphic fileref="handles.png" format="PNG"/>
4373 gtk_render_handle (GtkStyleContext *context,
4380 GtkStyleContextPrivate *priv;
4381 GtkThemingEngineClass *engine_class;
4383 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4384 g_return_if_fail (cr != NULL);
4386 if (width <= 0 || height <= 0)
4389 priv = context->priv;
4390 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4394 store_animation_region (context, x, y, width, height);
4396 _gtk_theming_engine_set_context (priv->theming_engine, context);
4397 engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
4403 * gtk_render_activity:
4404 * @context: a #GtkStyleContext
4406 * @x: X origin of the rectangle
4407 * @y: Y origin of the rectangle
4408 * @width: rectangle width
4409 * @height: rectangle height
4411 * Renders an activity area (Such as in #GtkSpinner or the
4412 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4413 * determines whether there is activity going on.
4418 gtk_render_activity (GtkStyleContext *context,
4425 GtkStyleContextPrivate *priv;
4426 GtkThemingEngineClass *engine_class;
4428 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4429 g_return_if_fail (cr != NULL);
4431 if (width <= 0 || height <= 0)
4434 priv = context->priv;
4435 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4439 store_animation_region (context, x, y, width, height);
4441 _gtk_theming_engine_set_context (priv->theming_engine, context);
4442 engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
4448 * gtk_render_icon_pixbuf:
4449 * @context: a #GtkStyleContext
4450 * @source: the #GtkIconSource specifying the icon to render
4451 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4452 * means render at the size of the source and don't scale.
4454 * Renders the icon specified by @source at the given @size, returning the result
4457 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4462 gtk_render_icon_pixbuf (GtkStyleContext *context,
4463 const GtkIconSource *source,
4466 GtkStyleContextPrivate *priv;
4467 GtkThemingEngineClass *engine_class;
4469 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4470 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4471 g_return_val_if_fail (source != NULL, NULL);
4473 priv = context->priv;
4474 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4476 _gtk_theming_engine_set_context (priv->theming_engine, context);
4477 return engine_class->render_icon_pixbuf (priv->theming_engine, source, size);
4482 * @context: a #GtkStyleContext
4484 * @pixbuf: a #GdkPixbuf containing the icon to draw
4485 * @x: X position for the @pixbuf
4486 * @y: Y position for the @pixbuf
4488 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4493 gtk_render_icon (GtkStyleContext *context,
4499 GtkStyleContextPrivate *priv;
4500 GtkThemingEngineClass *engine_class;
4502 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4503 g_return_if_fail (cr != NULL);
4505 priv = context->priv;
4506 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4510 store_animation_region (context,
4512 gdk_pixbuf_get_width (pixbuf),
4513 gdk_pixbuf_get_height (pixbuf));
4515 _gtk_theming_engine_set_context (priv->theming_engine, context);
4516 engine_class->render_icon (priv->theming_engine, cr, pixbuf, x, y);
4522 draw_insertion_cursor (GtkStyleContext *context,
4527 gboolean is_primary,
4528 PangoDirection direction,
4529 gboolean draw_arrow)
4532 GdkRGBA primary_color;
4533 GdkRGBA secondary_color;
4534 gfloat cursor_aspect_ratio;
4540 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4541 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4543 /* When changing the shape or size of the cursor here,
4544 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4547 gtk_style_context_get_style (context,
4548 "cursor-aspect-ratio", &cursor_aspect_ratio,
4551 stem_width = height * cursor_aspect_ratio + 1;
4553 /* put (stem_width % 2) on the proper side of the cursor */
4554 if (direction == PANGO_DIRECTION_LTR)
4555 offset = stem_width / 2;
4557 offset = stem_width - stem_width / 2;
4559 cairo_rectangle (cr, x - offset, y, stem_width, height);
4567 arrow_width = stem_width + 1;
4569 if (direction == PANGO_DIRECTION_RTL)
4571 ax = x - offset - 1;
4572 ay = y + height - arrow_width * 2 - arrow_width + 1;
4574 cairo_move_to (cr, ax, ay + 1);
4575 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4576 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4579 else if (direction == PANGO_DIRECTION_LTR)
4581 ax = x + stem_width - offset;
4582 ay = y + height - arrow_width * 2 - arrow_width + 1;
4584 cairo_move_to (cr, ax, ay + 1);
4585 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4586 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4590 g_assert_not_reached();
4597 * gtk_render_insertion_cursor:
4598 * @context: a #GtkStyleContext
4602 * @layout: the #PangoLayout of the text
4603 * @index: the index in the #PangoLayout
4604 * @direction: the #PangoDirection of the text
4606 * Draws a text caret on @cr at the specified index of @layout.
4611 gtk_render_insertion_cursor (GtkStyleContext *context,
4615 PangoLayout *layout,
4617 PangoDirection direction)
4619 GtkStyleContextPrivate *priv;
4620 gboolean split_cursor;
4621 PangoRectangle strong_pos, weak_pos;
4622 PangoRectangle *cursor1, *cursor2;
4623 PangoDirection keymap_direction;
4624 PangoDirection direction2;
4626 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4627 g_return_if_fail (cr != NULL);
4628 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4629 g_return_if_fail (index >= 0);
4631 priv = context->priv;
4633 g_object_get (gtk_settings_get_for_screen (priv->screen),
4634 "gtk-split-cursor", &split_cursor,
4637 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4639 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4641 direction2 = PANGO_DIRECTION_NEUTRAL;
4645 cursor1 = &strong_pos;
4647 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4649 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4650 cursor2 = &weak_pos;
4655 if (keymap_direction == direction)
4656 cursor1 = &strong_pos;
4658 cursor1 = &weak_pos;
4661 draw_insertion_cursor (context,
4663 x + PANGO_PIXELS (cursor1->x),
4664 y + PANGO_PIXELS (cursor1->y),
4665 PANGO_PIXELS (cursor1->height),
4668 direction2 != PANGO_DIRECTION_NEUTRAL);
4670 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4672 draw_insertion_cursor (context,
4674 x + PANGO_PIXELS (cursor2->x),
4675 y + PANGO_PIXELS (cursor2->y),
4676 PANGO_PIXELS (cursor2->height),
4684 * gtk_draw_insertion_cursor:
4685 * @widget: a #GtkWidget
4686 * @cr: cairo context to draw to
4687 * @location: location where to draw the cursor (@location->width is ignored)
4688 * @is_primary: if the cursor should be the primary cursor color.
4689 * @direction: whether the cursor is left-to-right or
4690 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4691 * @draw_arrow: %TRUE to draw a directional arrow on the
4692 * cursor. Should be %FALSE unless the cursor is split.
4694 * Draws a text caret on @cr at @location. This is not a style function
4695 * but merely a convenience function for drawing the standard cursor shape.
4700 gtk_draw_insertion_cursor (GtkWidget *widget,
4702 const GdkRectangle *location,
4703 gboolean is_primary,
4704 GtkTextDirection direction,
4705 gboolean draw_arrow)
4707 GtkStyleContext *context;
4709 g_return_if_fail (GTK_IS_WIDGET (widget));
4710 g_return_if_fail (cr != NULL);
4711 g_return_if_fail (location != NULL);
4712 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4714 context = gtk_widget_get_style_context (widget);
4716 draw_insertion_cursor (context, cr,
4717 location->x, location->y, location->height,
4719 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4723 static AtkAttributeSet *
4724 add_attribute (AtkAttributeSet *attributes,
4725 AtkTextAttribute attr,
4730 at = g_new (AtkAttribute, 1);
4731 at->name = g_strdup (atk_text_attribute_get_name (attr));
4732 at->value = g_strdup (value);
4734 return g_slist_prepend (attributes, at);
4738 * _gtk_style_context_get_attributes:
4739 * @attributes: a #AtkAttributeSet to add attributes to
4740 * @context: the #GtkStyleContext to get attributes from
4741 * @flags: the state to use with @context
4743 * Adds the foreground and background color from @context to
4744 * @attributes, after translating them to ATK attributes.
4746 * This is a convenience function that can be used in
4747 * implementing the #AtkText interface in widgets.
4749 * Returns: the modified #AtkAttributeSet
4752 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4753 GtkStyleContext *context,
4754 GtkStateFlags flags)
4759 gtk_style_context_get_background_color (context, flags, &color);
4760 value = g_strdup_printf ("%u,%u,%u",
4761 (guint) ceil (color.red * 65536 - color.red),
4762 (guint) ceil (color.green * 65536 - color.green),
4763 (guint) ceil (color.blue * 65536 - color.blue));
4764 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4767 gtk_style_context_get_color (context, flags, &color);
4768 value = g_strdup_printf ("%u,%u,%u",
4769 (guint) ceil (color.red * 65536 - color.red),
4770 (guint) ceil (color.green * 65536 - color.green),
4771 (guint) ceil (color.blue * 65536 - color.blue));
4772 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);