1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <gobject/gvaluecollector.h>
25 #include "gtkstylecontextprivate.h"
26 #include "gtkstylepropertiesprivate.h"
27 #include "gtktypebuiltins.h"
28 #include "gtkthemingengineprivate.h"
30 #include "gtkwidget.h"
31 #include "gtkwindow.h"
32 #include "gtkprivate.h"
33 #include "gtksymboliccolorprivate.h"
34 #include "gtkanimationdescription.h"
35 #include "gtktimeline.h"
36 #include "gtkiconfactory.h"
37 #include "gtkwidgetprivate.h"
38 #include "gtkstyleproviderprivate.h"
41 * SECTION:gtkstylecontext
42 * @Short_description: Rendering UI elements
43 * @Title: GtkStyleContext
45 * #GtkStyleContext is an object that stores styling information affecting
46 * a widget defined by #GtkWidgetPath.
48 * In order to construct the final style information, #GtkStyleContext
49 * queries information from all attached #GtkStyleProviders. Style providers
50 * can be either attached explicitly to the context through
51 * gtk_style_context_add_provider(), or to the screen through
52 * gtk_style_context_add_provider_for_screen(). The resulting style is a
53 * combination of all providers' information in priority order.
55 * For GTK+ widgets, any #GtkStyleContext returned by
56 * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
57 * #GdkScreen and RTL/LTR information set. The style context will be also
58 * updated automatically if any of these settings change on the widget.
60 * If you are using the theming layer standalone, you will need to set a
61 * widget path and a screen yourself to the created style context through
62 * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
63 * as updating the context yourself using gtk_style_context_invalidate()
64 * whenever any of the conditions change, such as a change in the
65 * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
68 * <refsect2 id="gtkstylecontext-animations">
69 * <title>Transition animations</title>
71 * #GtkStyleContext has built-in support for state change transitions.
72 * Note that these animations respect the #GtkSettings:gtk-enable-animations
76 * For simple widgets where state changes affect the whole widget area,
77 * calling gtk_style_context_notify_state_change() with a %NULL region
78 * is sufficient to trigger the transition animation. And GTK+ already
79 * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
83 * If a widget needs to declare several animatable regions (i.e. not
84 * affecting the whole widget area), its #GtkWidget::draw signal handler
85 * needs to wrap the render operations for the different regions with
86 * calls to gtk_style_context_push_animatable_region() and
87 * gtk_style_context_pop_animatable_region(). These functions take an
88 * identifier for the region which must be unique within the style context.
89 * For simple widgets with a fixed set of animatable regions, using an
90 * enumeration works well:
93 * <title>Using an enumeration to identify animatable regions</title>
104 * spin_button_draw (GtkWidget *widget,
107 * GtkStyleContext *context;
109 * context = gtk_widget_get_style_context (widget);
111 * gtk_style_context_push_animatable_region (context,
112 * GUINT_TO_POINTER (REGION_ENTRY));
114 * gtk_render_background (cr, 0, 0, 100, 30);
115 * gtk_render_frame (cr, 0, 0, 100, 30);
117 * gtk_style_context_pop_animatable_region (context);
124 * For complex widgets with an arbitrary number of animatable regions, it
125 * is up to the implementation to come up with a way to uniquely identify
126 * each animatable region. Using pointers to internal structs is one way
130 * <title>Using struct pointers to identify animatable regions</title>
133 * notebook_draw_tab (GtkWidget *widget,
134 * NotebookPage *page,
137 * gtk_style_context_push_animatable_region (context, page);
138 * gtk_render_extension (cr, page->x, page->y, page->width, page->height);
139 * gtk_style_context_pop_animatable_region (context);
144 * The widget also needs to notify the style context about a state change
145 * for a given animatable region so the animation is triggered.
148 * <title>Triggering a state change animation on a region</title>
151 * notebook_motion_notify (GtkWidget *widget,
152 * GdkEventMotion *event)
154 * GtkStyleContext *context;
155 * NotebookPage *page;
157 * context = gtk_widget_get_style_context (widget);
158 * page = find_page_under_pointer (widget, event);
159 * gtk_style_context_notify_state_change (context,
160 * gtk_widget_get_window (widget),
162 * GTK_STATE_PRELIGHT,
169 * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
170 * special value, in this case, the whole widget area will be updated
174 * <refsect2 id="gtkstylecontext-classes">
175 * <title>Style classes and regions</title>
177 * Widgets can add style classes to their context, which can be used
178 * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
179 * rendering. GTK+ has a number of predefined style classes:
180 * #GTK_STYLE_CLASS_CELL,
181 * #GTK_STYLE_CLASS_ENTRY,
182 * #GTK_STYLE_CLASS_BUTTON,
183 * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
184 * #GTK_STYLE_CLASS_CALENDAR,
185 * #GTK_STYLE_CLASS_SLIDER,
186 * #GTK_STYLE_CLASS_BACKGROUND,
187 * #GTK_STYLE_CLASS_RUBBERBAND,
188 * #GTK_STYLE_CLASS_TOOLTIP,
189 * #GTK_STYLE_CLASS_MENU,
190 * #GTK_STYLE_CLASS_MENUBAR,
191 * #GTK_STYLE_CLASS_MENUITEM,
192 * #GTK_STYLE_CLASS_TOOLBAR,
193 * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
194 * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
195 * #GTK_STYLE_CLASS_RADIO,
196 * #GTK_STYLE_CLASS_CHECK,
197 * #GTK_STYLE_CLASS_TROUGH,
198 * #GTK_STYLE_CLASS_SCROLLBAR,
199 * #GTK_STYLE_CLASS_SCALE,
200 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
201 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
202 * #GTK_STYLE_CLASS_HEADER,
203 * #GTK_STYLE_CLASS_ACCELERATOR,
204 * #GTK_STYLE_CLASS_GRIP,
205 * #GTK_STYLE_CLASS_DOCK,
206 * #GTK_STYLE_CLASS_PROGRESSBAR,
207 * #GTK_STYLE_CLASS_SPINNER,
208 * #GTK_STYLE_CLASS_EXPANDER,
209 * #GTK_STYLE_CLASS_SPINBUTTON,
210 * #GTK_STYLE_CLASS_NOTEBOOK,
211 * #GTK_STYLE_CLASS_VIEW,
212 * #GTK_STYLE_CLASS_SIDEBAR,
213 * #GTK_STYLE_CLASS_IMAGE,
214 * #GTK_STYLE_CLASS_HIGHLIGHT,
215 * #GTK_STYLE_CLASS_FRAME,
216 * #GTK_STYLE_CLASS_DND,
217 * #GTK_STYLE_CLASS_PANE_SEPARATOR,
218 * #GTK_STYLE_CLASS_SEPARATOR,
219 * #GTK_STYLE_CLASS_INFO,
220 * #GTK_STYLE_CLASS_WARNING,
221 * #GTK_STYLE_CLASS_QUESTION,
222 * #GTK_STYLE_CLASS_ERROR,
223 * #GTK_STYLE_CLASS_HORIZONTAL,
224 * #GTK_STYLE_CLASS_VERTICAL,
225 * #GTK_STYLE_CLASS_TOP,
226 * #GTK_STYLE_CLASS_BOTTOM,
227 * #GTK_STYLE_CLASS_LEFT,
228 * #GTK_STYLE_CLASS_RIGHT,
231 * Widgets can also add regions with flags to their context.
232 * The regions used by GTK+ widgets are:
237 * <entry>Region</entry>
238 * <entry>Flags</entry>
239 * <entry>Macro</entry>
240 * <entry>Used by</entry>
246 * <entry>even, odd</entry>
247 * <entry>GTK_STYLE_REGION_ROW</entry>
248 * <entry>#GtkTreeView</entry>
251 * <entry>column</entry>
252 * <entry>first, last, sorted</entry>
253 * <entry>GTK_STYLE_REGION_COLUMN</entry>
254 * <entry>#GtkTreeView</entry>
257 * <entry>column-header</entry>
259 * <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
264 * <entry>even, odd, first, last</entry>
265 * <entry>GTK_STYLE_REGION_TAB</entry>
266 * <entry>#GtkNotebook</entry>
273 * <refsect2 id="gtkstylecontext-custom-styling">
274 * <title>Custom styling in UI libraries and applications</title>
276 * If you are developing a library with custom #GtkWidget<!-- -->s that
277 * render differently than standard components, you may need to add a
278 * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
279 * priority, either a #GtkCssProvider or a custom object implementing the
280 * #GtkStyleProvider interface. This way theming engines may still attempt
281 * to style your UI elements in a different way if needed so.
284 * If you are using custom styling on an applications, you probably want then
285 * to make your style information prevail to the theme's, so you must use
286 * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
287 * priority, keep in mind that the user settings in
288 * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
289 * still take precedence over your changes, as it uses the
290 * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
293 * If a custom theming engine is needed, you probably want to implement a
294 * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
295 * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
296 * which loads the theming engine module from the standard paths.
301 typedef struct GtkStyleProviderData GtkStyleProviderData;
302 typedef struct GtkStyleInfo GtkStyleInfo;
303 typedef struct GtkRegion GtkRegion;
304 typedef struct PropertyValue PropertyValue;
305 typedef struct AnimationInfo AnimationInfo;
306 typedef struct StyleData StyleData;
311 GtkRegionFlags flags;
314 struct GtkStyleProviderData
316 GtkStyleProvider *provider;
330 GArray *style_classes;
332 GtkJunctionSides junction_sides;
333 GtkStateFlags state_flags;
338 GtkCssComputedValues *store;
339 GSList *icon_factories;
340 GArray *property_cache;
345 GtkTimeline *timeline;
349 /* Region stack (until region_id) at the time of
350 * rendering, this is used for nested cancellation.
352 GSList *parent_regions;
356 gboolean target_value;
358 cairo_region_t *invalidation_region;
362 struct _GtkStyleContextPrivate
367 GList *providers_last;
369 GtkStyleContext *parent;
370 GtkWidgetPath *widget_path;
371 GHashTable *style_data;
373 StyleData *current_data;
374 GtkStateFlags current_state;
376 GSList *animation_regions;
379 GtkThemingEngine *theming_engine;
381 GtkTextDirection direction;
383 guint animations_invalidated : 1;
384 guint invalidating_context : 1;
399 static guint signals[LAST_SIGNAL] = { 0 };
401 static GQuark provider_list_quark = 0;
403 static void gtk_style_context_finalize (GObject *object);
405 static void gtk_style_context_impl_set_property (GObject *object,
409 static void gtk_style_context_impl_get_property (GObject *object,
413 static GtkSymbolicColor *
414 gtk_style_context_color_lookup_func (gpointer contextp,
418 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
421 gtk_style_context_class_init (GtkStyleContextClass *klass)
423 GObjectClass *object_class = G_OBJECT_CLASS (klass);
425 object_class->finalize = gtk_style_context_finalize;
426 object_class->set_property = gtk_style_context_impl_set_property;
427 object_class->get_property = gtk_style_context_impl_get_property;
430 g_signal_new (I_("changed"),
431 G_TYPE_FROM_CLASS (object_class),
433 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
435 g_cclosure_marshal_VOID__VOID,
438 g_object_class_install_property (object_class,
440 g_param_spec_object ("screen",
442 P_("The associated GdkScreen"),
444 GTK_PARAM_READWRITE));
445 g_object_class_install_property (object_class,
447 g_param_spec_enum ("direction",
449 P_("Text direction"),
450 GTK_TYPE_TEXT_DIRECTION,
452 GTK_PARAM_READWRITE));
454 * GtkStyleContext:parent:
456 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
461 g_object_class_install_property (object_class,
463 g_param_spec_object ("parent",
465 P_("The parent style context"),
466 GTK_TYPE_STYLE_CONTEXT,
467 GTK_PARAM_READWRITE));
469 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
472 static GtkStyleInfo *
473 style_info_new (void)
477 info = g_slice_new0 (GtkStyleInfo);
478 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
479 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
485 style_info_free (GtkStyleInfo *info)
487 g_array_free (info->style_classes, TRUE);
488 g_array_free (info->regions, TRUE);
489 g_slice_free (GtkStyleInfo, info);
492 static GtkStyleInfo *
493 style_info_copy (const GtkStyleInfo *info)
497 copy = style_info_new ();
498 g_array_insert_vals (copy->style_classes, 0,
499 info->style_classes->data,
500 info->style_classes->len);
502 g_array_insert_vals (copy->regions, 0,
506 copy->junction_sides = info->junction_sides;
507 copy->state_flags = info->state_flags;
513 style_info_hash (gconstpointer elem)
515 const GtkStyleInfo *info;
520 for (i = 0; i < info->style_classes->len; i++)
522 hash += g_array_index (info->style_classes, GQuark, i);
526 for (i = 0; i < info->regions->len; i++)
530 region = &g_array_index (info->regions, GtkRegion, i);
531 hash += region->class_quark;
532 hash += region->flags;
536 return hash ^ info->state_flags;
540 style_info_equal (gconstpointer elem1,
543 const GtkStyleInfo *info1, *info2;
548 if (info1->state_flags != info2->state_flags)
551 if (info1->junction_sides != info2->junction_sides)
554 if (info1->style_classes->len != info2->style_classes->len)
557 if (memcmp (info1->style_classes->data,
558 info2->style_classes->data,
559 info1->style_classes->len * sizeof (GQuark)) != 0)
562 if (info1->regions->len != info2->regions->len)
565 if (memcmp (info1->regions->data,
566 info2->regions->data,
567 info1->regions->len * sizeof (GtkRegion)) != 0)
574 style_data_new (void)
578 data = g_slice_new0 (StyleData);
584 clear_property_cache (StyleData *data)
588 if (!data->property_cache)
591 for (i = 0; i < data->property_cache->len; i++)
593 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
595 g_param_spec_unref (node->pspec);
596 g_value_unset (&node->value);
599 g_array_free (data->property_cache, TRUE);
600 data->property_cache = NULL;
604 style_data_free (StyleData *data)
606 g_object_unref (data->store);
607 clear_property_cache (data);
609 g_slist_free_full (data->icon_factories, g_object_unref);
611 g_slice_free (StyleData, data);
615 gtk_style_context_init (GtkStyleContext *style_context)
617 GtkStyleContextPrivate *priv;
620 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
621 GTK_TYPE_STYLE_CONTEXT,
622 GtkStyleContextPrivate);
624 priv->style_data = g_hash_table_new_full (style_info_hash,
626 (GDestroyNotify) style_info_free,
627 (GDestroyNotify) style_data_free);
628 priv->theming_engine = g_object_ref ((gpointer) gtk_theming_engine_load (NULL));
630 priv->direction = GTK_TEXT_DIR_LTR;
632 priv->screen = gdk_screen_get_default ();
634 /* Create default info store */
635 info = style_info_new ();
636 priv->info_stack = g_slist_prepend (priv->info_stack, info);
639 static GtkStyleProviderData *
640 style_provider_data_new (GtkStyleProvider *provider,
643 GtkStyleProviderData *data;
645 data = g_slice_new (GtkStyleProviderData);
646 data->provider = g_object_ref (provider);
647 data->priority = priority;
653 style_provider_data_free (GtkStyleProviderData *data)
655 g_object_unref (data->provider);
656 g_slice_free (GtkStyleProviderData, data);
660 animation_info_free (AnimationInfo *info)
662 g_object_unref (info->timeline);
663 g_object_unref (info->window);
665 if (info->invalidation_region)
666 cairo_region_destroy (info->invalidation_region);
668 g_array_free (info->rectangles, TRUE);
669 g_slist_free (info->parent_regions);
670 g_slice_free (AnimationInfo, info);
673 static AnimationInfo *
674 animation_info_lookup_by_timeline (GtkStyleContext *context,
675 GtkTimeline *timeline)
677 GtkStyleContextPrivate *priv;
681 priv = context->priv;
683 for (l = priv->animations; l; l = l->next)
687 if (info->timeline == timeline)
695 timeline_frame_cb (GtkTimeline *timeline,
699 GtkStyleContextPrivate *priv;
700 GtkStyleContext *context;
704 priv = context->priv;
705 info = animation_info_lookup_by_timeline (context, timeline);
707 g_assert (info != NULL);
709 /* Cancel transition if window is gone */
710 if (gdk_window_is_destroyed (info->window) ||
711 !gdk_window_is_visible (info->window))
713 priv->animations = g_slist_remove (priv->animations, info);
714 animation_info_free (info);
718 if (info->invalidation_region &&
719 !cairo_region_is_empty (info->invalidation_region))
720 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
722 gdk_window_invalidate_rect (info->window, NULL, TRUE);
726 timeline_finished_cb (GtkTimeline *timeline,
729 GtkStyleContextPrivate *priv;
730 GtkStyleContext *context;
734 priv = context->priv;
735 info = animation_info_lookup_by_timeline (context, timeline);
737 g_assert (info != NULL);
739 priv->animations = g_slist_remove (priv->animations, info);
741 /* Invalidate one last time the area, so the final content is painted */
742 if (info->invalidation_region &&
743 !cairo_region_is_empty (info->invalidation_region))
744 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
746 gdk_window_invalidate_rect (info->window, NULL, TRUE);
748 animation_info_free (info);
751 static AnimationInfo *
752 animation_info_new (GtkStyleContext *context,
755 GtkTimelineProgressType progress_type,
758 gboolean target_value,
763 info = g_slice_new0 (AnimationInfo);
765 info->rectangles = g_array_new (FALSE, FALSE, sizeof (cairo_rectangle_int_t));
766 info->timeline = _gtk_timeline_new (duration);
767 info->window = g_object_ref (window);
769 info->target_value = target_value;
770 info->region_id = region_id;
772 _gtk_timeline_set_progress_type (info->timeline, progress_type);
773 _gtk_timeline_set_loop (info->timeline, loop);
775 if (!loop && !target_value)
777 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
778 _gtk_timeline_rewind (info->timeline);
781 g_signal_connect (info->timeline, "frame",
782 G_CALLBACK (timeline_frame_cb), context);
783 g_signal_connect (info->timeline, "finished",
784 G_CALLBACK (timeline_finished_cb), context);
786 _gtk_timeline_start (info->timeline);
791 static AnimationInfo *
792 animation_info_lookup (GtkStyleContext *context,
796 GtkStyleContextPrivate *priv;
799 priv = context->priv;
801 for (l = priv->animations; l; l = l->next)
807 if (info->state == state &&
808 info->region_id == region_id)
816 gtk_style_context_finalize (GObject *object)
818 GtkStyleContextPrivate *priv;
819 GtkStyleContext *style_context;
822 style_context = GTK_STYLE_CONTEXT (object);
823 priv = style_context->priv;
825 gtk_style_context_set_parent (style_context, NULL);
827 if (priv->widget_path)
828 gtk_widget_path_free (priv->widget_path);
830 g_hash_table_destroy (priv->style_data);
832 g_list_free_full (priv->providers, (GDestroyNotify) style_provider_data_free);
834 g_slist_free_full (priv->info_stack, (GDestroyNotify) style_info_free);
836 g_slist_free (priv->animation_regions);
838 for (l = priv->animations; l; l = l->next)
839 animation_info_free ((AnimationInfo *) l->data);
841 g_slist_free (priv->animations);
843 if (priv->theming_engine)
844 g_object_unref (priv->theming_engine);
846 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
850 gtk_style_context_impl_set_property (GObject *object,
855 GtkStyleContext *style_context;
857 style_context = GTK_STYLE_CONTEXT (object);
862 gtk_style_context_set_screen (style_context,
863 g_value_get_object (value));
866 gtk_style_context_set_direction (style_context,
867 g_value_get_enum (value));
870 gtk_style_context_set_parent (style_context,
871 g_value_get_object (value));
874 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
880 gtk_style_context_impl_get_property (GObject *object,
885 GtkStyleContext *style_context;
886 GtkStyleContextPrivate *priv;
888 style_context = GTK_STYLE_CONTEXT (object);
889 priv = style_context->priv;
894 g_value_set_object (value, priv->screen);
897 g_value_set_enum (value, priv->direction);
900 g_value_set_object (value, priv->parent);
903 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
909 find_next_candidate (GList *local,
915 GtkStyleProviderData *local_data, *global_data;
917 local_data = local->data;
918 global_data = global->data;
920 if (local_data->priority < global_data->priority)
921 return (ascending) ? local : global;
923 return (ascending) ? global : local;
934 build_properties (GtkStyleContext *context,
935 StyleData *style_data,
939 GtkStyleContextPrivate *priv;
940 GList *elem, *list, *global_list = NULL;
941 GtkCssLookup *lookup;
943 priv = context->priv;
944 list = priv->providers_last;
948 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
949 global_list = g_list_last (global_list);
952 lookup = _gtk_css_lookup_new ();
954 while ((elem = find_next_candidate (list, global_list, FALSE)) != NULL)
956 GtkStyleProviderData *data;
957 GtkStyleProperties *provider_style;
964 global_list = global_list->prev;
966 if (GTK_IS_STYLE_PROVIDER_PRIVATE (data->provider))
968 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (data->provider),
975 provider_style = gtk_style_provider_get_style (data->provider, path);
979 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (provider_style),
983 g_object_unref (provider_style);
988 style_data->store = _gtk_css_computed_values_new ();
989 _gtk_css_lookup_resolve (lookup, context, style_data->store);
990 _gtk_css_lookup_free (lookup);
994 build_icon_factories (GtkStyleContext *context,
995 StyleData *style_data,
998 GtkStyleContextPrivate *priv;
999 GList *elem, *list, *global_list = NULL;
1001 priv = context->priv;
1002 list = priv->providers_last;
1006 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
1007 global_list = g_list_last (global_list);
1010 while ((elem = find_next_candidate (list, global_list, FALSE)) != NULL)
1012 GtkIconFactory *factory;
1013 GtkStyleProviderData *data;
1020 global_list = global_list->prev;
1022 factory = gtk_style_provider_get_icon_factory (data->provider, path);
1025 style_data->icon_factories = g_slist_prepend (style_data->icon_factories, factory);
1029 static GtkWidgetPath *
1030 create_query_path (GtkStyleContext *context)
1032 GtkStyleContextPrivate *priv;
1033 GtkWidgetPath *path;
1037 priv = context->priv;
1038 path = gtk_widget_path_copy (priv->widget_path);
1039 pos = gtk_widget_path_length (path) - 1;
1041 info = priv->info_stack->data;
1043 /* Set widget regions */
1044 for (i = 0; i < info->regions->len; i++)
1048 region = &g_array_index (info->regions, GtkRegion, i);
1049 gtk_widget_path_iter_add_region (path, pos,
1050 g_quark_to_string (region->class_quark),
1054 /* Set widget classes */
1055 for (i = 0; i < info->style_classes->len; i++)
1059 quark = g_array_index (info->style_classes, GQuark, i);
1060 gtk_widget_path_iter_add_class (path, pos,
1061 g_quark_to_string (quark));
1068 style_data_lookup (GtkStyleContext *context,
1069 GtkStateFlags state)
1071 GtkStyleContextPrivate *priv;
1073 gboolean state_mismatch;
1076 priv = context->priv;
1077 state_mismatch = ((GtkStyleInfo *) priv->info_stack->data)->state_flags != state;
1079 /* Current data in use is cached, just return it */
1080 if (priv->current_data && priv->current_state == state)
1081 return priv->current_data;
1083 g_assert (priv->widget_path != NULL);
1085 if (G_UNLIKELY (state_mismatch))
1087 gtk_style_context_save (context);
1088 gtk_style_context_set_state (context, state);
1091 priv->current_data = g_hash_table_lookup (priv->style_data, priv->info_stack->data);
1092 priv->current_state = state;
1094 if (!priv->current_data)
1096 GtkWidgetPath *path;
1098 priv->current_data = style_data_new ();
1099 g_hash_table_insert (priv->style_data,
1100 style_info_copy (priv->info_stack->data),
1101 priv->current_data);
1103 path = create_query_path (context);
1105 build_properties (context, priv->current_data, path, state);
1106 build_icon_factories (context, priv->current_data, path);
1108 gtk_widget_path_free (path);
1111 data = priv->current_data;
1113 if (priv->theming_engine)
1114 g_object_unref (priv->theming_engine);
1116 v = _gtk_css_computed_values_get_value_by_name (priv->current_data->store, "engine");
1118 priv->theming_engine = g_value_dup_object (v);
1120 priv->theming_engine = g_object_ref (gtk_theming_engine_load (NULL));
1122 if (G_UNLIKELY (state_mismatch))
1123 gtk_style_context_restore (context);
1129 style_provider_add (GList **list,
1130 GtkStyleProvider *provider,
1133 GtkStyleProviderData *new_data;
1134 gboolean added = FALSE;
1137 new_data = style_provider_data_new (provider, priority);
1141 GtkStyleProviderData *data;
1145 /* Provider was already attached to the style
1146 * context, remove in order to add the new data
1148 if (data->provider == provider)
1155 /* Remove and free link */
1156 *list = g_list_remove_link (*list, link);
1157 style_provider_data_free (link->data);
1158 g_list_free_1 (link);
1164 data->priority > priority)
1166 *list = g_list_insert_before (*list, l, new_data);
1174 *list = g_list_append (*list, new_data);
1178 style_provider_remove (GList **list,
1179 GtkStyleProvider *provider)
1185 GtkStyleProviderData *data;
1189 if (data->provider == provider)
1191 *list = g_list_remove_link (*list, l);
1192 style_provider_data_free (l->data);
1205 * gtk_style_context_new:
1207 * Creates a standalone #GtkStyleContext, this style context
1208 * won't be attached to any widget, so you may want
1209 * to call gtk_style_context_set_path() yourself.
1212 * This function is only useful when using the theming layer
1213 * separated from GTK+, if you are using #GtkStyleContext to
1214 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1215 * in order to get a style context ready to theme the widget.
1218 * Returns: A newly created #GtkStyleContext.
1221 gtk_style_context_new (void)
1223 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1227 * gtk_style_context_add_provider:
1228 * @context: a #GtkStyleContext
1229 * @provider: a #GtkStyleProvider
1230 * @priority: the priority of the style provider. The lower
1231 * it is, the earlier it will be used in the style
1232 * construction. Typically this will be in the range
1233 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1234 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1236 * Adds a style provider to @context, to be used in style construction.
1238 * <note><para>If both priorities are the same, A #GtkStyleProvider
1239 * added through this function takes precedence over another added
1240 * through gtk_style_context_add_provider_for_screen().</para></note>
1245 gtk_style_context_add_provider (GtkStyleContext *context,
1246 GtkStyleProvider *provider,
1249 GtkStyleContextPrivate *priv;
1251 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1252 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1254 priv = context->priv;
1255 style_provider_add (&priv->providers, provider, priority);
1256 priv->providers_last = g_list_last (priv->providers);
1258 gtk_style_context_invalidate (context);
1262 * gtk_style_context_remove_provider:
1263 * @context: a #GtkStyleContext
1264 * @provider: a #GtkStyleProvider
1266 * Removes @provider from the style providers list in @context.
1271 gtk_style_context_remove_provider (GtkStyleContext *context,
1272 GtkStyleProvider *provider)
1274 GtkStyleContextPrivate *priv;
1276 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1277 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1279 priv = context->priv;
1281 if (style_provider_remove (&priv->providers, provider))
1283 priv->providers_last = g_list_last (priv->providers);
1285 gtk_style_context_invalidate (context);
1290 * gtk_style_context_reset_widgets:
1291 * @screen: a #GdkScreen
1293 * This function recomputes the styles for all widgets under a particular
1294 * #GdkScreen. This is useful when some global parameter has changed that
1295 * affects the appearance of all widgets, because when a widget gets a new
1296 * style, it will both redraw and recompute any cached information about
1297 * its appearance. As an example, it is used when the color scheme changes
1298 * in the related #GtkSettings object.
1303 gtk_style_context_reset_widgets (GdkScreen *screen)
1305 GList *list, *toplevels;
1307 _gtk_icon_set_invalidate_caches ();
1309 toplevels = gtk_window_list_toplevels ();
1310 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1312 for (list = toplevels; list; list = list->next)
1314 if (gtk_widget_get_screen (list->data) == screen)
1315 gtk_widget_reset_style (list->data);
1317 g_object_unref (list->data);
1320 g_list_free (toplevels);
1324 * gtk_style_context_add_provider_for_screen:
1325 * @screen: a #GdkScreen
1326 * @provider: a #GtkStyleProvider
1327 * @priority: the priority of the style provider. The lower
1328 * it is, the earlier it will be used in the style
1329 * construction. Typically this will be in the range
1330 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1331 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1333 * Adds a global style provider to @screen, which will be used
1334 * in style construction for all #GtkStyleContext<!-- -->s under
1337 * GTK+ uses this to make styling information from #GtkSettings
1340 * <note><para>If both priorities are the same, A #GtkStyleProvider
1341 * added through gtk_style_context_add_provider() takes precedence
1342 * over another added through this function.</para></note>
1347 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1348 GtkStyleProvider *provider,
1351 GList *providers, *list;
1353 g_return_if_fail (GDK_IS_SCREEN (screen));
1354 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1356 if (G_UNLIKELY (!provider_list_quark))
1357 provider_list_quark = g_quark_from_static_string ("gtk-provider-list-quark");
1359 list = providers = g_object_get_qdata (G_OBJECT (screen), provider_list_quark);
1360 style_provider_add (&list, provider, priority);
1362 if (list != providers)
1363 g_object_set_qdata (G_OBJECT (screen), provider_list_quark, list);
1365 gtk_style_context_reset_widgets (screen);
1369 * gtk_style_context_remove_provider_for_screen:
1370 * @screen: a #GdkScreen
1371 * @provider: a #GtkStyleProvider
1373 * Removes @provider from the global style providers list in @screen.
1378 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1379 GtkStyleProvider *provider)
1381 GList *providers, *list;
1383 g_return_if_fail (GDK_IS_SCREEN (screen));
1384 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1386 if (G_UNLIKELY (!provider_list_quark))
1389 list = providers = g_object_get_qdata (G_OBJECT (screen), provider_list_quark);
1391 if (style_provider_remove (&list, provider))
1393 if (list != providers)
1394 g_object_set_qdata (G_OBJECT (screen), provider_list_quark, list);
1396 gtk_style_context_reset_widgets (screen);
1401 * gtk_style_context_get_section:
1402 * @context: a #GtkStyleContext
1403 * @property: style property name
1405 * Queries the location in the CSS where @property was defined for the
1406 * current @context. Note that the state to be queried is taken from
1407 * gtk_style_context_get_state().
1409 * If the location is not available, %NULL will be returned. The
1410 * location might not be available for various reasons, such as the
1411 * property being overridden, @property not naming a supported CSS
1412 * property or tracking of definitions being disabled for performance
1415 * Shorthand CSS properties cannot be queried for a location and will
1416 * always return %NULL.
1418 * Returns: %NULL or the section where value was defined
1421 gtk_style_context_get_section (GtkStyleContext *context,
1422 const gchar *property)
1424 GtkStyleContextPrivate *priv;
1425 GtkStyleProperty *prop;
1428 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1429 g_return_val_if_fail (property != NULL, NULL);
1431 priv = context->priv;
1432 g_return_val_if_fail (priv->widget_path != NULL, NULL);
1434 prop = _gtk_style_property_lookup (property);
1435 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1438 data = style_data_lookup (context, gtk_style_context_get_state (context));
1439 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1442 static const GValue *
1443 gtk_style_context_query_func (guint id,
1446 return _gtk_css_computed_values_get_value (values, id);
1450 * gtk_style_context_get_property:
1451 * @context: a #GtkStyleContext
1452 * @property: style property name
1453 * @state: state to retrieve the property value for
1454 * @value: (out) (transfer full): return location for the style property value
1456 * Gets a style property from @context for the given state.
1458 * When @value is no longer needed, g_value_unset() must be called
1459 * to free any allocated memory.
1464 gtk_style_context_get_property (GtkStyleContext *context,
1465 const gchar *property,
1466 GtkStateFlags state,
1469 GtkStyleContextPrivate *priv;
1470 GtkStyleProperty *prop;
1473 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1474 g_return_if_fail (property != NULL);
1475 g_return_if_fail (value != NULL);
1477 priv = context->priv;
1478 g_return_if_fail (priv->widget_path != NULL);
1480 prop = _gtk_style_property_lookup (property);
1483 g_warning ("Style property \"%s\" is not registered", property);
1486 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1488 g_warning ("Style property \"%s\" is not gettable", property);
1492 data = style_data_lookup (context, state);
1493 _gtk_style_property_query (prop, value, gtk_style_context_query_func, data->store);
1497 * gtk_style_context_get_valist:
1498 * @context: a #GtkStyleContext
1499 * @state: state to retrieve the property values for
1500 * @args: va_list of property name/return location pairs, followed by %NULL
1502 * Retrieves several style property values from @context for a given state.
1507 gtk_style_context_get_valist (GtkStyleContext *context,
1508 GtkStateFlags state,
1511 const gchar *property_name;
1513 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1515 property_name = va_arg (args, const gchar *);
1517 while (property_name)
1519 gchar *error = NULL;
1520 GValue value = G_VALUE_INIT;
1522 gtk_style_context_get_property (context,
1527 G_VALUE_LCOPY (&value, args, 0, &error);
1528 g_value_unset (&value);
1532 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1537 property_name = va_arg (args, const gchar *);
1542 * gtk_style_context_get:
1543 * @context: a #GtkStyleContext
1544 * @state: state to retrieve the property values for
1545 * @...: property name /return value pairs, followed by %NULL
1547 * Retrieves several style property values from @context for a
1553 gtk_style_context_get (GtkStyleContext *context,
1554 GtkStateFlags state,
1559 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1561 va_start (args, state);
1562 gtk_style_context_get_valist (context, state, args);
1567 * gtk_style_context_set_state:
1568 * @context: a #GtkStyleContext
1569 * @flags: state to represent
1571 * Sets the state to be used when rendering with any
1572 * of the gtk_render_*() functions.
1577 gtk_style_context_set_state (GtkStyleContext *context,
1578 GtkStateFlags flags)
1580 GtkStyleContextPrivate *priv;
1583 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1585 priv = context->priv;
1586 info = priv->info_stack->data;
1587 info->state_flags = flags;
1591 * gtk_style_context_get_state:
1592 * @context: a #GtkStyleContext
1594 * Returns the state used when rendering.
1596 * Returns: the state flags
1601 gtk_style_context_get_state (GtkStyleContext *context)
1603 GtkStyleContextPrivate *priv;
1606 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1608 priv = context->priv;
1609 info = priv->info_stack->data;
1611 return info->state_flags;
1615 context_has_animatable_region (GtkStyleContext *context,
1618 GtkStyleContextPrivate *priv;
1620 /* NULL region_id means everything
1621 * rendered through the style context
1626 priv = context->priv;
1627 return g_slist_find (priv->animation_regions, region_id) != NULL;
1631 * gtk_style_context_state_is_running:
1632 * @context: a #GtkStyleContext
1633 * @state: a widget state
1634 * @progress: (out): return location for the transition progress
1636 * Returns %TRUE if there is a transition animation running for the
1637 * current region (see gtk_style_context_push_animatable_region()).
1639 * If @progress is not %NULL, the animation progress will be returned
1640 * there, 0.0 means the state is closest to being unset, while 1.0 means
1641 * it's closest to being set. This means transition animation will
1642 * run from 0 to 1 when @state is being set and from 1 to 0 when
1645 * Returns: %TRUE if there is a running transition animation for @state.
1650 gtk_style_context_state_is_running (GtkStyleContext *context,
1654 GtkStyleContextPrivate *priv;
1655 AnimationInfo *info;
1658 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1660 priv = context->priv;
1662 for (l = priv->animations; l; l = l->next)
1666 if (info->state == state &&
1667 context_has_animatable_region (context, info->region_id))
1670 *progress = _gtk_timeline_get_progress (info->timeline);
1680 * gtk_style_context_set_path:
1681 * @context: a #GtkStyleContext
1682 * @path: a #GtkWidgetPath
1684 * Sets the #GtkWidgetPath used for style matching. As a
1685 * consequence, the style will be regenerated to match
1686 * the new given path.
1688 * If you are using a #GtkStyleContext returned from
1689 * gtk_widget_get_style_context(), you do not need to call
1695 gtk_style_context_set_path (GtkStyleContext *context,
1696 GtkWidgetPath *path)
1698 GtkStyleContextPrivate *priv;
1700 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1701 g_return_if_fail (path != NULL);
1703 priv = context->priv;
1705 if (priv->widget_path)
1707 gtk_widget_path_free (priv->widget_path);
1708 priv->widget_path = NULL;
1712 priv->widget_path = gtk_widget_path_copy (path);
1714 gtk_style_context_invalidate (context);
1718 * gtk_style_context_get_path:
1719 * @context: a #GtkStyleContext
1721 * Returns the widget path used for style matching.
1723 * Returns: (transfer none): A #GtkWidgetPath
1727 const GtkWidgetPath *
1728 gtk_style_context_get_path (GtkStyleContext *context)
1730 GtkStyleContextPrivate *priv;
1732 priv = context->priv;
1733 return priv->widget_path;
1737 * gtk_style_context_set_parent:
1738 * @context: a #GtkStyleContext
1739 * @parent: (allow-none): the new parent or %NULL
1741 * Sets the parent style context for @context. The parent style
1742 * context is used to implement
1743 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1746 * If you are using a #GtkStyleContext returned from
1747 * gtk_widget_get_style_context(), the parent will be set for you.
1752 gtk_style_context_set_parent (GtkStyleContext *context,
1753 GtkStyleContext *parent)
1755 GtkStyleContextPrivate *priv;
1757 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1758 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1760 priv = context->priv;
1762 if (priv->parent == parent)
1766 g_object_ref (parent);
1769 g_object_unref (priv->parent);
1771 priv->parent = parent;
1773 g_object_notify (G_OBJECT (context), "parent");
1774 gtk_style_context_invalidate (context);
1778 * gtk_style_context_get_parent:
1779 * @context: a #GtkStyleContext
1781 * Gets the parent context set via gtk_style_context_set_parent().
1782 * See that function for details.
1784 * Returns: (transfer none): the parent context or %NULL
1789 gtk_style_context_get_parent (GtkStyleContext *context)
1791 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1793 return context->priv->parent;
1797 * gtk_style_context_save:
1798 * @context: a #GtkStyleContext
1800 * Saves the @context state, so all modifications done through
1801 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1802 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1803 * or gtk_style_context_set_junction_sides() can be reverted in one
1804 * go through gtk_style_context_restore().
1809 gtk_style_context_save (GtkStyleContext *context)
1811 GtkStyleContextPrivate *priv;
1814 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1816 priv = context->priv;
1818 g_assert (priv->info_stack != NULL);
1820 info = style_info_copy (priv->info_stack->data);
1821 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1825 * gtk_style_context_restore:
1826 * @context: a #GtkStyleContext
1828 * Restores @context state to a previous stage.
1829 * See gtk_style_context_save().
1834 gtk_style_context_restore (GtkStyleContext *context)
1836 GtkStyleContextPrivate *priv;
1839 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1841 priv = context->priv;
1843 if (priv->info_stack)
1845 info = priv->info_stack->data;
1846 priv->info_stack = g_slist_remove (priv->info_stack, info);
1847 style_info_free (info);
1850 if (!priv->info_stack)
1852 g_warning ("Unpaired gtk_style_context_restore() call");
1854 /* Create default region */
1855 info = style_info_new ();
1856 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1859 priv->current_data = NULL;
1863 style_class_find (GArray *array,
1868 gboolean found = FALSE;
1874 if (!array || array->len == 0)
1878 max = array->len - 1;
1884 mid = (min + max) / 2;
1885 item = g_array_index (array, GQuark, mid);
1887 if (class_quark == item)
1892 else if (class_quark > item)
1893 min = pos = mid + 1;
1900 while (!found && min <= max);
1909 region_find (GArray *array,
1914 gboolean found = FALSE;
1920 if (!array || array->len == 0)
1924 max = array->len - 1;
1930 mid = (min + max) / 2;
1931 region = &g_array_index (array, GtkRegion, mid);
1933 if (region->class_quark == class_quark)
1938 else if (region->class_quark > class_quark)
1939 min = pos = mid + 1;
1946 while (!found && min <= max);
1955 * gtk_style_context_add_class:
1956 * @context: a #GtkStyleContext
1957 * @class_name: class name to use in styling
1959 * Adds a style class to @context, so posterior calls to
1960 * gtk_style_context_get() or any of the gtk_render_*()
1961 * functions will make use of this new class for styling.
1963 * In the CSS file format, a #GtkEntry defining an "entry"
1964 * class, would be matched by:
1967 * GtkEntry.entry { ... }
1970 * While any widget defining an "entry" class would be
1979 gtk_style_context_add_class (GtkStyleContext *context,
1980 const gchar *class_name)
1982 GtkStyleContextPrivate *priv;
1987 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1988 g_return_if_fail (class_name != NULL);
1990 priv = context->priv;
1991 class_quark = g_quark_from_string (class_name);
1993 g_assert (priv->info_stack != NULL);
1994 info = priv->info_stack->data;
1996 if (!style_class_find (info->style_classes, class_quark, &position))
1998 g_array_insert_val (info->style_classes, position, class_quark);
2000 /* Unset current data, as it likely changed due to the class change */
2001 priv->current_data = NULL;
2006 * gtk_style_context_remove_class:
2007 * @context: a #GtkStyleContext
2008 * @class_name: class name to remove
2010 * Removes @class_name from @context.
2015 gtk_style_context_remove_class (GtkStyleContext *context,
2016 const gchar *class_name)
2018 GtkStyleContextPrivate *priv;
2023 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2024 g_return_if_fail (class_name != NULL);
2026 class_quark = g_quark_try_string (class_name);
2031 priv = context->priv;
2033 g_assert (priv->info_stack != NULL);
2034 info = priv->info_stack->data;
2036 if (style_class_find (info->style_classes, class_quark, &position))
2038 g_array_remove_index (info->style_classes, position);
2040 /* Unset current data, as it likely changed due to the class change */
2041 priv->current_data = NULL;
2046 * gtk_style_context_has_class:
2047 * @context: a #GtkStyleContext
2048 * @class_name: a class name
2050 * Returns %TRUE if @context currently has defined the
2053 * Returns: %TRUE if @context has @class_name defined
2058 gtk_style_context_has_class (GtkStyleContext *context,
2059 const gchar *class_name)
2061 GtkStyleContextPrivate *priv;
2065 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2066 g_return_val_if_fail (class_name != NULL, FALSE);
2068 class_quark = g_quark_try_string (class_name);
2073 priv = context->priv;
2075 g_assert (priv->info_stack != NULL);
2076 info = priv->info_stack->data;
2078 if (style_class_find (info->style_classes, class_quark, NULL))
2085 * gtk_style_context_list_classes:
2086 * @context: a #GtkStyleContext
2088 * Returns the list of classes currently defined in @context.
2090 * Returns: (transfer container) (element-type utf8): a #GList of
2091 * strings with the currently defined classes. The contents
2092 * of the list are owned by GTK+, but you must free the list
2093 * itself with g_list_free() when you are done with it.
2098 gtk_style_context_list_classes (GtkStyleContext *context)
2100 GtkStyleContextPrivate *priv;
2102 GList *classes = NULL;
2105 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2107 priv = context->priv;
2109 g_assert (priv->info_stack != NULL);
2110 info = priv->info_stack->data;
2112 for (i = 0; i < info->style_classes->len; i++)
2116 quark = g_array_index (info->style_classes, GQuark, i);
2117 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
2124 * gtk_style_context_list_regions:
2125 * @context: a #GtkStyleContext
2127 * Returns the list of regions currently defined in @context.
2129 * Returns: (transfer container) (element-type utf8): a #GList of
2130 * strings with the currently defined regions. The contents
2131 * of the list are owned by GTK+, but you must free the list
2132 * itself with g_list_free() when you are done with it.
2137 gtk_style_context_list_regions (GtkStyleContext *context)
2139 GtkStyleContextPrivate *priv;
2141 GList *classes = NULL;
2144 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2146 priv = context->priv;
2148 g_assert (priv->info_stack != NULL);
2149 info = priv->info_stack->data;
2151 for (i = 0; i < info->regions->len; i++)
2154 const gchar *class_name;
2156 region = &g_array_index (info->regions, GtkRegion, i);
2158 class_name = g_quark_to_string (region->class_quark);
2159 classes = g_list_prepend (classes, (gchar *) class_name);
2166 _gtk_style_context_check_region_name (const gchar *str)
2168 g_return_val_if_fail (str != NULL, FALSE);
2170 if (!g_ascii_islower (str[0]))
2176 !g_ascii_islower (*str))
2186 * gtk_style_context_add_region:
2187 * @context: a #GtkStyleContext
2188 * @region_name: region name to use in styling
2189 * @flags: flags that apply to the region
2191 * Adds a region to @context, so posterior calls to
2192 * gtk_style_context_get() or any of the gtk_render_*()
2193 * functions will make use of this new region for styling.
2195 * In the CSS file format, a #GtkTreeView defining a "row"
2196 * region, would be matched by:
2199 * GtkTreeView row { ... }
2202 * Pseudo-classes are used for matching @flags, so the two
2205 * GtkTreeView row:nth-child(even) { ... }
2206 * GtkTreeView row:nth-child(odd) { ... }
2209 * would apply to even and odd rows, respectively.
2211 * <note><para>Region names must only contain lowercase letters
2212 * and '-', starting always with a lowercase letter.</para></note>
2217 gtk_style_context_add_region (GtkStyleContext *context,
2218 const gchar *region_name,
2219 GtkRegionFlags flags)
2221 GtkStyleContextPrivate *priv;
2223 GQuark region_quark;
2226 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2227 g_return_if_fail (region_name != NULL);
2228 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2230 priv = context->priv;
2231 region_quark = g_quark_from_string (region_name);
2233 g_assert (priv->info_stack != NULL);
2234 info = priv->info_stack->data;
2236 if (!region_find (info->regions, region_quark, &position))
2240 region.class_quark = region_quark;
2241 region.flags = flags;
2243 g_array_insert_val (info->regions, position, region);
2245 /* Unset current data, as it likely changed due to the region change */
2246 priv->current_data = NULL;
2251 * gtk_style_context_remove_region:
2252 * @context: a #GtkStyleContext
2253 * @region_name: region name to unset
2255 * Removes a region from @context.
2260 gtk_style_context_remove_region (GtkStyleContext *context,
2261 const gchar *region_name)
2263 GtkStyleContextPrivate *priv;
2265 GQuark region_quark;
2268 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2269 g_return_if_fail (region_name != NULL);
2271 region_quark = g_quark_try_string (region_name);
2276 priv = context->priv;
2278 g_assert (priv->info_stack != NULL);
2279 info = priv->info_stack->data;
2281 if (region_find (info->regions, region_quark, &position))
2283 g_array_remove_index (info->regions, position);
2285 /* Unset current data, as it likely changed due to the region change */
2286 priv->current_data = NULL;
2291 * gtk_style_context_has_region:
2292 * @context: a #GtkStyleContext
2293 * @region_name: a region name
2294 * @flags_return: (out) (allow-none): return location for region flags
2296 * Returns %TRUE if @context has the region defined.
2297 * If @flags_return is not %NULL, it is set to the flags
2298 * affecting the region.
2300 * Returns: %TRUE if region is defined
2305 gtk_style_context_has_region (GtkStyleContext *context,
2306 const gchar *region_name,
2307 GtkRegionFlags *flags_return)
2309 GtkStyleContextPrivate *priv;
2311 GQuark region_quark;
2314 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2315 g_return_val_if_fail (region_name != NULL, FALSE);
2320 region_quark = g_quark_try_string (region_name);
2325 priv = context->priv;
2327 g_assert (priv->info_stack != NULL);
2328 info = priv->info_stack->data;
2330 if (region_find (info->regions, region_quark, &position))
2336 region = &g_array_index (info->regions, GtkRegion, position);
2337 *flags_return = region->flags;
2346 style_property_values_cmp (gconstpointer bsearch_node1,
2347 gconstpointer bsearch_node2)
2349 const PropertyValue *val1 = bsearch_node1;
2350 const PropertyValue *val2 = bsearch_node2;
2352 if (val1->widget_type != val2->widget_type)
2353 return val1->widget_type < val2->widget_type ? -1 : 1;
2355 if (val1->pspec != val2->pspec)
2356 return val1->pspec < val2->pspec ? -1 : 1;
2358 if (val1->state != val2->state)
2359 return val1->state < val2->state ? -1 : 1;
2365 _gtk_style_context_peek_property (GtkStyleContext *context,
2366 const char *property_name)
2368 StyleData *data = style_data_lookup (context, gtk_style_context_get_state (context));
2370 return _gtk_css_computed_values_get_value_by_name (data->store, property_name);
2374 _gtk_style_context_get_number (GtkStyleContext *context,
2375 const char *property_name,
2376 double one_hundred_percent)
2378 const GValue *value;
2380 value = _gtk_style_context_peek_property (context, property_name);
2381 return _gtk_css_number_get (g_value_get_boxed (value), one_hundred_percent);
2385 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2387 GtkStateFlags state,
2390 GtkStyleContextPrivate *priv;
2391 PropertyValue *pcache, key = { 0 };
2392 GList *global_list = NULL;
2396 priv = context->priv;
2397 data = style_data_lookup (context, state);
2399 key.widget_type = widget_type;
2403 /* need value cache array */
2404 if (!data->property_cache)
2405 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2408 pcache = bsearch (&key,
2409 data->property_cache->data, data->property_cache->len,
2410 sizeof (PropertyValue), style_property_values_cmp);
2412 return &pcache->value;
2416 while (i < data->property_cache->len &&
2417 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2420 g_array_insert_val (data->property_cache, i, key);
2421 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2423 /* cache miss, initialize value type, then set contents */
2424 g_param_spec_ref (pcache->pspec);
2425 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2429 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
2430 global_list = g_list_last (global_list);
2433 if (priv->widget_path)
2435 GList *list, *global, *elem;
2437 list = priv->providers_last;
2438 global = global_list;
2440 while ((elem = find_next_candidate (list, global, FALSE)) != NULL)
2442 GtkStyleProviderData *provider_data;
2444 provider_data = elem->data;
2449 global = global->prev;
2451 if (gtk_style_provider_get_style_property (provider_data->provider,
2452 priv->widget_path, state,
2453 pspec, &pcache->value))
2455 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2456 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2458 GtkSymbolicColor *color;
2461 color = g_value_dup_boxed (&pcache->value);
2463 g_value_unset (&pcache->value);
2465 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2466 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2468 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2470 if (_gtk_style_context_resolve_color (context, color, &rgba))
2472 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2473 g_value_set_boxed (&pcache->value, &rgba);
2478 rgb.red = rgba.red * 65535. + 0.5;
2479 rgb.green = rgba.green * 65535. + 0.5;
2480 rgb.blue = rgba.blue * 65535. + 0.5;
2482 g_value_set_boxed (&pcache->value, &rgb);
2486 g_param_value_set_default (pspec, &pcache->value);
2488 gtk_symbolic_color_unref (color);
2491 return &pcache->value;
2496 /* not supplied by any provider, revert to default */
2497 g_param_value_set_default (pspec, &pcache->value);
2499 return &pcache->value;
2503 * gtk_style_context_get_style_property:
2504 * @context: a #GtkStyleContext
2505 * @property_name: the name of the widget style property
2506 * @value: Return location for the property value
2508 * Gets the value for a widget style property.
2510 * When @value is no longer needed, g_value_unset() must be called
2511 * to free any allocated memory.
2514 gtk_style_context_get_style_property (GtkStyleContext *context,
2515 const gchar *property_name,
2518 GtkStyleContextPrivate *priv;
2519 GtkWidgetClass *widget_class;
2520 GtkStateFlags state;
2522 const GValue *peek_value;
2525 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2526 g_return_if_fail (property_name != NULL);
2527 g_return_if_fail (value != NULL);
2529 priv = context->priv;
2531 if (!priv->widget_path)
2534 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2536 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2538 g_warning ("%s: can't get style properties for non-widget class `%s'",
2540 g_type_name (widget_type));
2544 widget_class = g_type_class_ref (widget_type);
2545 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2546 g_type_class_unref (widget_class);
2550 g_warning ("%s: widget class `%s' has no style property named `%s'",
2552 g_type_name (widget_type),
2557 state = gtk_style_context_get_state (context);
2558 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2561 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2562 g_value_copy (peek_value, value);
2563 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2564 g_value_transform (peek_value, value);
2566 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2568 G_VALUE_TYPE_NAME (peek_value),
2569 G_VALUE_TYPE_NAME (value));
2573 * gtk_style_context_get_style_valist:
2574 * @context: a #GtkStyleContext
2575 * @args: va_list of property name/return location pairs, followed by %NULL
2577 * Retrieves several widget style properties from @context according to the
2583 gtk_style_context_get_style_valist (GtkStyleContext *context,
2586 GtkStyleContextPrivate *priv;
2587 const gchar *prop_name;
2588 GtkStateFlags state;
2591 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2593 prop_name = va_arg (args, const gchar *);
2594 priv = context->priv;
2596 if (!priv->widget_path)
2599 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2601 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2603 g_warning ("%s: can't get style properties for non-widget class `%s'",
2605 g_type_name (widget_type));
2609 state = gtk_style_context_get_state (context);
2613 GtkWidgetClass *widget_class;
2615 const GValue *peek_value;
2618 widget_class = g_type_class_ref (widget_type);
2619 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2620 g_type_class_unref (widget_class);
2624 g_warning ("%s: widget class `%s' has no style property named `%s'",
2626 g_type_name (widget_type),
2631 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2634 G_VALUE_LCOPY (peek_value, args, 0, &error);
2638 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2640 G_VALUE_TYPE_NAME (peek_value),
2645 prop_name = va_arg (args, const gchar *);
2650 * gtk_style_context_get_style:
2651 * @context: a #GtkStyleContext
2652 * @...: property name /return value pairs, followed by %NULL
2654 * Retrieves several widget style properties from @context according to the
2660 gtk_style_context_get_style (GtkStyleContext *context,
2665 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2667 va_start (args, context);
2668 gtk_style_context_get_style_valist (context, args);
2674 * gtk_style_context_lookup_icon_set:
2675 * @context: a #GtkStyleContext
2676 * @stock_id: an icon name
2678 * Looks up @stock_id in the icon factories associated to @context and
2679 * the default icon factory, returning an icon set if found, otherwise
2682 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2685 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2686 const gchar *stock_id)
2688 GtkStyleContextPrivate *priv;
2692 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2693 g_return_val_if_fail (stock_id != NULL, NULL);
2695 priv = context->priv;
2696 g_return_val_if_fail (priv->widget_path != NULL, NULL);
2698 data = style_data_lookup (context, 0);
2700 for (list = data->icon_factories; list; list = list->next)
2702 GtkIconFactory *factory;
2703 GtkIconSet *icon_set;
2705 factory = list->data;
2706 icon_set = gtk_icon_factory_lookup (factory, stock_id);
2712 return gtk_icon_factory_lookup_default (stock_id);
2716 * gtk_style_context_set_screen:
2717 * @context: a #GtkStyleContext
2718 * @screen: a #GdkScreen
2720 * Attaches @context to the given screen.
2722 * The screen is used to add style information from 'global' style
2723 * providers, such as the screens #GtkSettings instance.
2725 * If you are using a #GtkStyleContext returned from
2726 * gtk_widget_get_style_context(), you do not need to
2727 * call this yourself.
2732 gtk_style_context_set_screen (GtkStyleContext *context,
2735 GtkStyleContextPrivate *priv;
2737 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2738 g_return_if_fail (GDK_IS_SCREEN (screen));
2740 priv = context->priv;
2741 if (priv->screen == screen)
2744 priv->screen = screen;
2746 g_object_notify (G_OBJECT (context), "screen");
2748 gtk_style_context_invalidate (context);
2752 * gtk_style_context_get_screen:
2753 * @context: a #GtkStyleContext
2755 * Returns the #GdkScreen to which @context is attached.
2757 * Returns: (transfer none): a #GdkScreen.
2760 gtk_style_context_get_screen (GtkStyleContext *context)
2762 GtkStyleContextPrivate *priv;
2764 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2766 priv = context->priv;
2767 return priv->screen;
2771 * gtk_style_context_set_direction:
2772 * @context: a #GtkStyleContext
2773 * @direction: the new direction.
2775 * Sets the reading direction for rendering purposes.
2777 * If you are using a #GtkStyleContext returned from
2778 * gtk_widget_get_style_context(), you do not need to
2779 * call this yourself.
2784 gtk_style_context_set_direction (GtkStyleContext *context,
2785 GtkTextDirection direction)
2787 GtkStyleContextPrivate *priv;
2789 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2791 priv = context->priv;
2792 priv->direction = direction;
2794 g_object_notify (G_OBJECT (context), "direction");
2798 * gtk_style_context_get_direction:
2799 * @context: a #GtkStyleContext
2801 * Returns the widget direction used for rendering.
2803 * Returns: the widget direction
2808 gtk_style_context_get_direction (GtkStyleContext *context)
2810 GtkStyleContextPrivate *priv;
2812 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2814 priv = context->priv;
2815 return priv->direction;
2819 * gtk_style_context_set_junction_sides:
2820 * @context: a #GtkStyleContext
2821 * @sides: sides where rendered elements are visually connected to
2824 * Sets the sides where rendered elements (mostly through
2825 * gtk_render_frame()) will visually connect with other visual elements.
2827 * This is merely a hint that may or may not be honored
2828 * by theming engines.
2830 * Container widgets are expected to set junction hints as appropriate
2831 * for their children, so it should not normally be necessary to call
2832 * this function manually.
2837 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2838 GtkJunctionSides sides)
2840 GtkStyleContextPrivate *priv;
2843 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2845 priv = context->priv;
2846 info = priv->info_stack->data;
2847 info->junction_sides = sides;
2851 * gtk_style_context_get_junction_sides:
2852 * @context: a #GtkStyleContext
2854 * Returns the sides where rendered elements connect visually with others.
2856 * Returns: the junction sides
2861 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2863 GtkStyleContextPrivate *priv;
2866 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2868 priv = context->priv;
2869 info = priv->info_stack->data;
2870 return info->junction_sides;
2873 static GtkSymbolicColor *
2874 gtk_style_context_color_lookup_func (gpointer contextp,
2877 GtkSymbolicColor *sym_color;
2878 GtkStyleContext *context = contextp;
2879 GtkStyleContextPrivate *priv = context->priv;
2880 GList *elem, *list, *global_list = NULL;
2882 list = priv->providers_last;
2885 global_list = g_object_get_qdata (G_OBJECT (priv->screen), provider_list_quark);
2886 global_list = g_list_last (global_list);
2891 while (sym_color == NULL &&
2892 (elem = find_next_candidate (list, global_list, FALSE)) != NULL)
2894 GtkStyleProviderData *data;
2901 global_list = global_list->prev;
2903 if (GTK_IS_STYLE_PROVIDER_PRIVATE (data->provider))
2905 sym_color = _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (data->provider),
2910 /* If somebody hits this code path, shout at them */
2919 _gtk_style_context_resolve_color (GtkStyleContext *context,
2920 GtkSymbolicColor *color,
2923 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2924 g_return_val_if_fail (color != NULL, FALSE);
2925 g_return_val_if_fail (result != NULL, FALSE);
2927 return _gtk_symbolic_color_resolve_full (color,
2928 gtk_style_context_color_lookup_func,
2934 * gtk_style_context_lookup_color:
2935 * @context: a #GtkStyleContext
2936 * @color_name: color name to lookup
2937 * @color: (out): Return location for the looked up color
2939 * Looks up and resolves a color name in the @context color map.
2941 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2944 gtk_style_context_lookup_color (GtkStyleContext *context,
2945 const gchar *color_name,
2948 GtkSymbolicColor *sym_color;
2950 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2951 g_return_val_if_fail (color_name != NULL, FALSE);
2952 g_return_val_if_fail (color != NULL, FALSE);
2954 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2955 if (sym_color == NULL)
2958 return _gtk_style_context_resolve_color (context, sym_color, color);
2962 * gtk_style_context_notify_state_change:
2963 * @context: a #GtkStyleContext
2964 * @window: a #GdkWindow
2965 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2966 * See gtk_style_context_push_animatable_region()
2967 * @state: state to trigger transition for
2968 * @state_value: %TRUE if @state is the state we are changing to,
2969 * %FALSE if we are changing away from it
2971 * Notifies a state change on @context, so if the current style makes use
2972 * of transition animations, one will be started so all rendered elements
2973 * under @region_id are animated for state @state being set to value
2976 * The @window parameter is used in order to invalidate the rendered area
2977 * as the animation runs, so make sure it is the same window that is being
2978 * rendered on by the gtk_render_*() functions.
2980 * If @region_id is %NULL, all rendered elements using @context will be
2981 * affected by this state transition.
2983 * As a practical example, a #GtkButton notifying a state transition on
2984 * the prelight state:
2986 * gtk_style_context_notify_state_change (context,
2987 * gtk_widget_get_window (widget),
2989 * GTK_STATE_PRELIGHT,
2990 * button->in_button);
2993 * Can be handled in the CSS file like this:
2996 * background-color: #f00
3000 * background-color: #fff;
3001 * transition: 200ms linear
3005 * This combination will animate the button background from red to white
3006 * if a pointer enters the button, and back to red if the pointer leaves
3009 * Note that @state is used when finding the transition parameters, which
3010 * is why the style places the transition under the :hover pseudo-class.
3015 gtk_style_context_notify_state_change (GtkStyleContext *context,
3019 gboolean state_value)
3021 GtkStyleContextPrivate *priv;
3022 GtkAnimationDescription *desc;
3023 AnimationInfo *info;
3024 GtkStateFlags flags;
3028 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3029 g_return_if_fail (GDK_IS_WINDOW (window));
3030 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
3032 priv = context->priv;
3033 g_return_if_fail (priv->widget_path != NULL);
3035 state_value = (state_value == TRUE);
3039 case GTK_STATE_ACTIVE:
3040 flags = GTK_STATE_FLAG_ACTIVE;
3042 case GTK_STATE_PRELIGHT:
3043 flags = GTK_STATE_FLAG_PRELIGHT;
3045 case GTK_STATE_SELECTED:
3046 flags = GTK_STATE_FLAG_SELECTED;
3048 case GTK_STATE_INSENSITIVE:
3049 flags = GTK_STATE_FLAG_INSENSITIVE;
3051 case GTK_STATE_INCONSISTENT:
3052 flags = GTK_STATE_FLAG_INCONSISTENT;
3054 case GTK_STATE_FOCUSED:
3055 flags = GTK_STATE_FLAG_FOCUSED;
3057 case GTK_STATE_NORMAL:
3063 /* Find out if there is any animation description for the given
3064 * state, it will fallback to the normal state as well if necessary.
3066 data = style_data_lookup (context, flags);
3067 v = _gtk_css_computed_values_get_value_by_name (data->store, "transition");
3070 desc = g_value_get_boxed (v);
3074 if (_gtk_animation_description_get_duration (desc) == 0)
3077 info = animation_info_lookup (context, region_id, state);
3080 info->target_value != state_value)
3082 /* Target values are the opposite */
3083 if (!_gtk_timeline_get_loop (info->timeline))
3085 /* Reverse the animation */
3086 if (_gtk_timeline_get_direction (info->timeline) == GTK_TIMELINE_DIRECTION_FORWARD)
3087 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
3089 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_FORWARD);
3091 info->target_value = state_value;
3095 /* Take it out of its looping state */
3096 _gtk_timeline_set_loop (info->timeline, FALSE);
3100 (!_gtk_animation_description_get_loop (desc) ||
3103 info = animation_info_new (context, region_id,
3104 _gtk_animation_description_get_duration (desc),
3105 _gtk_animation_description_get_progress_type (desc),
3106 _gtk_animation_description_get_loop (desc),
3107 state, state_value, window);
3109 priv->animations = g_slist_prepend (priv->animations, info);
3110 priv->animations_invalidated = TRUE;
3115 * gtk_style_context_cancel_animations:
3116 * @context: a #GtkStyleContext
3117 * @region_id: (allow-none): animatable region to stop, or %NULL.
3118 * See gtk_style_context_push_animatable_region()
3120 * Stops all running animations for @region_id and all animatable
3121 * regions underneath.
3123 * A %NULL @region_id will stop all ongoing animations in @context,
3124 * when dealing with a #GtkStyleContext obtained through
3125 * gtk_widget_get_style_context(), this is normally done for you
3126 * in all circumstances you would expect all widget to be stopped,
3127 * so this should be only used in complex widgets with different
3128 * animatable regions.
3133 gtk_style_context_cancel_animations (GtkStyleContext *context,
3136 GtkStyleContextPrivate *priv;
3137 AnimationInfo *info;
3140 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3142 priv = context->priv;
3143 l = priv->animations;
3151 info->region_id == region_id ||
3152 g_slist_find (info->parent_regions, region_id))
3154 priv->animations = g_slist_remove (priv->animations, info);
3155 animation_info_free (info);
3161 is_parent_of (GdkWindow *parent,
3164 GtkWidget *child_widget, *parent_widget;
3167 gdk_window_get_user_data (child, (gpointer *) &child_widget);
3168 gdk_window_get_user_data (parent, (gpointer *) &parent_widget);
3170 if (child_widget != parent_widget &&
3171 !gtk_widget_is_ancestor (child_widget, parent_widget))
3178 if (window == parent)
3181 window = gdk_window_get_parent (window);
3188 * gtk_style_context_scroll_animations:
3189 * @context: a #GtkStyleContext
3190 * @window: a #GdkWindow used previously in
3191 * gtk_style_context_notify_state_change()
3192 * @dx: Amount to scroll in the X axis
3193 * @dy: Amount to scroll in the Y axis
3195 * This function is analogous to gdk_window_scroll(), and
3196 * should be called together with it so the invalidation
3197 * areas for any ongoing animation are scrolled together
3203 gtk_style_context_scroll_animations (GtkStyleContext *context,
3208 GtkStyleContextPrivate *priv;
3209 AnimationInfo *info;
3212 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3213 g_return_if_fail (GDK_IS_WINDOW (window));
3215 priv = context->priv;
3216 l = priv->animations;
3223 if (info->invalidation_region &&
3224 (window == info->window ||
3225 is_parent_of (window, info->window)))
3226 cairo_region_translate (info->invalidation_region, dx, dy);
3231 * gtk_style_context_push_animatable_region:
3232 * @context: a #GtkStyleContext
3233 * @region_id: unique identifier for the animatable region
3235 * Pushes an animatable region, so all further gtk_render_*() calls between
3236 * this call and the following gtk_style_context_pop_animatable_region()
3237 * will potentially show transition animations for this region if
3238 * gtk_style_context_notify_state_change() is called for a given state,
3239 * and the current theme/style defines transition animations for state
3242 * The @region_id used must be unique in @context so the theming engine
3243 * can uniquely identify rendered elements subject to a state transition.
3248 gtk_style_context_push_animatable_region (GtkStyleContext *context,
3251 GtkStyleContextPrivate *priv;
3253 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3254 g_return_if_fail (region_id != NULL);
3256 priv = context->priv;
3257 priv->animation_regions = g_slist_prepend (priv->animation_regions, region_id);
3261 * gtk_style_context_pop_animatable_region:
3262 * @context: a #GtkStyleContext
3264 * Pops an animatable region from @context.
3265 * See gtk_style_context_push_animatable_region().
3270 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
3272 GtkStyleContextPrivate *priv;
3274 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3276 priv = context->priv;
3277 priv->animation_regions = g_slist_delete_link (priv->animation_regions,
3278 priv->animation_regions);
3282 _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context)
3284 GtkStyleContextPrivate *priv;
3287 priv = context->priv;
3289 for (l = priv->animations; l; l = l->next)
3291 AnimationInfo *info;
3295 /* A NULL invalidation region means it has to be recreated on
3296 * the next expose event, this happens usually after a widget
3297 * allocation change, so the next expose after it will update
3298 * the invalidation region.
3300 if (info->invalidation_region)
3302 cairo_region_destroy (info->invalidation_region);
3303 info->invalidation_region = NULL;
3307 priv->animations_invalidated = TRUE;
3311 _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context,
3314 GtkStyleContextPrivate *priv;
3317 priv = context->priv;
3319 if (!priv->animations_invalidated)
3322 l = priv->animations;
3326 AnimationInfo *info;
3335 if (info->invalidation_region)
3338 if (info->rectangles->len == 0)
3341 info->invalidation_region = cairo_region_create ();
3342 _gtk_widget_get_translation_to_window (widget, info->window, &rel_x, &rel_y);
3344 for (i = 0; i < info->rectangles->len; i++)
3346 cairo_rectangle_int_t *rect;
3348 rect = &g_array_index (info->rectangles, cairo_rectangle_int_t, i);
3350 /* These are widget relative coordinates,
3351 * so have them inverted to be window relative
3356 cairo_region_union_rectangle (info->invalidation_region, rect);
3359 g_array_remove_range (info->rectangles, 0, info->rectangles->len);
3362 priv->animations_invalidated = FALSE;
3366 store_animation_region (GtkStyleContext *context,
3372 GtkStyleContextPrivate *priv;
3375 priv = context->priv;
3377 if (!priv->animations_invalidated)
3380 for (l = priv->animations; l; l = l->next)
3382 AnimationInfo *info;
3386 /* The animation doesn't need updating
3387 * the invalidation area, bail out.
3389 if (info->invalidation_region)
3392 if (context_has_animatable_region (context, info->region_id))
3394 cairo_rectangle_int_t rect;
3398 rect.width = (gint) width;
3399 rect.height = (gint) height;
3401 g_array_append_val (info->rectangles, rect);
3403 if (!info->parent_regions)
3405 GSList *parent_regions;
3407 parent_regions = g_slist_find (priv->animation_regions, info->region_id);
3408 info->parent_regions = g_slist_copy (parent_regions);
3415 * gtk_style_context_invalidate:
3416 * @context: a #GtkStyleContext.
3418 * Invalidates @context style information, so it will be reconstructed
3421 * If you're using a #GtkStyleContext returned from
3422 * gtk_widget_get_style_context(), you do not need to
3423 * call this yourself.
3428 gtk_style_context_invalidate (GtkStyleContext *context)
3430 GtkStyleContextPrivate *priv;
3432 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3434 priv = context->priv;
3436 /* Avoid reentrancy */
3437 if (priv->invalidating_context)
3440 priv->invalidating_context = TRUE;
3442 g_hash_table_remove_all (priv->style_data);
3443 priv->current_data = NULL;
3445 g_signal_emit (context, signals[CHANGED], 0);
3447 priv->invalidating_context = FALSE;
3451 * gtk_style_context_set_background:
3452 * @context: a #GtkStyleContext
3453 * @window: a #GdkWindow
3455 * Sets the background of @window to the background pattern or
3456 * color specified in @context for its current state.
3461 gtk_style_context_set_background (GtkStyleContext *context,
3464 GtkStateFlags state;
3465 cairo_pattern_t *pattern;
3468 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3469 g_return_if_fail (GDK_IS_WINDOW (window));
3471 state = gtk_style_context_get_state (context);
3472 gtk_style_context_get (context, state,
3473 "background-image", &pattern,
3477 gdk_window_set_background_pattern (window, pattern);
3478 cairo_pattern_destroy (pattern);
3482 gtk_style_context_get (context, state,
3483 "background-color", &color,
3487 gdk_window_set_background_rgba (window, color);
3488 gdk_rgba_free (color);
3493 * gtk_style_context_get_color:
3494 * @context: a #GtkStyleContext
3495 * @state: state to retrieve the color for
3496 * @color: (out): return value for the foreground color
3498 * Gets the foreground color for a given state.
3503 gtk_style_context_get_color (GtkStyleContext *context,
3504 GtkStateFlags state,
3509 g_return_if_fail (color != NULL);
3510 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3512 gtk_style_context_get (context,
3522 * gtk_style_context_get_background_color:
3523 * @context: a #GtkStyleContext
3524 * @state: state to retrieve the color for
3525 * @color: (out): return value for the background color
3527 * Gets the background color for a given state.
3532 gtk_style_context_get_background_color (GtkStyleContext *context,
3533 GtkStateFlags state,
3538 g_return_if_fail (color != NULL);
3539 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3541 gtk_style_context_get (context,
3543 "background-color", &c,
3551 * gtk_style_context_get_border_color:
3552 * @context: a #GtkStyleContext
3553 * @state: state to retrieve the color for
3554 * @color: (out): return value for the border color
3556 * Gets the border color for a given state.
3561 gtk_style_context_get_border_color (GtkStyleContext *context,
3562 GtkStateFlags state,
3567 g_return_if_fail (color != NULL);
3568 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3570 gtk_style_context_get (context,
3580 * gtk_style_context_get_border:
3581 * @context: a #GtkStyleContext
3582 * @state: state to retrieve the border for
3583 * @border: (out): return value for the border settings
3585 * Gets the border for a given state as a #GtkBorder.
3586 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3591 gtk_style_context_get_border (GtkStyleContext *context,
3592 GtkStateFlags state,
3595 int top, left, bottom, right;
3597 g_return_if_fail (border != NULL);
3598 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3600 gtk_style_context_get (context,
3602 "border-top-width", &top,
3603 "border-left-width", &left,
3604 "border-bottom-width", &bottom,
3605 "border-right-width", &right,
3609 border->left = left;
3610 border->bottom = bottom;
3611 border->right = right;
3615 * gtk_style_context_get_padding:
3616 * @context: a #GtkStyleContext
3617 * @state: state to retrieve the padding for
3618 * @padding: (out): return value for the padding settings
3620 * Gets the padding for a given state as a #GtkBorder.
3621 * See %GTK_STYLE_PROPERTY_PADDING.
3626 gtk_style_context_get_padding (GtkStyleContext *context,
3627 GtkStateFlags state,
3630 int top, left, bottom, right;
3632 g_return_if_fail (padding != NULL);
3633 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3635 gtk_style_context_get (context,
3637 "padding-top", &top,
3638 "padding-left", &left,
3639 "padding-bottom", &bottom,
3640 "padding-right", &right,
3644 padding->left = left;
3645 padding->bottom = bottom;
3646 padding->right = right;
3650 * gtk_style_context_get_margin:
3651 * @context: a #GtkStyleContext
3652 * @state: state to retrieve the border for
3653 * @margin: (out): return value for the margin settings
3655 * Gets the margin for a given state as a #GtkBorder.
3656 * See %GTK_STYLE_PROPERTY_MARGIN.
3661 gtk_style_context_get_margin (GtkStyleContext *context,
3662 GtkStateFlags state,
3665 int top, left, bottom, right;
3667 g_return_if_fail (margin != NULL);
3668 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3670 gtk_style_context_get (context,
3673 "margin-left", &left,
3674 "margin-bottom", &bottom,
3675 "margin-right", &right,
3679 margin->left = left;
3680 margin->bottom = bottom;
3681 margin->right = right;
3685 * gtk_style_context_get_font:
3686 * @context: a #GtkStyleContext
3687 * @state: state to retrieve the font for
3689 * Returns the font description for a given state. The returned
3690 * object is const and will remain valid until the
3691 * #GtkStyleContext::changed signal happens.
3693 * Returns: (transfer none): the #PangoFontDescription for the given
3694 * state. This object is owned by GTK+ and should not be
3699 const PangoFontDescription *
3700 gtk_style_context_get_font (GtkStyleContext *context,
3701 GtkStateFlags state)
3703 GtkStyleContextPrivate *priv;
3705 PangoFontDescription *description;
3707 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3709 priv = context->priv;
3710 g_return_val_if_fail (priv->widget_path != NULL, NULL);
3712 data = style_data_lookup (context, state);
3714 /* Yuck, fonts are created on-demand but we don't return a ref.
3715 * Do bad things to achieve this requirement */
3716 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3717 if (description == NULL)
3719 gtk_style_context_get (context, state, "font", &description, NULL);
3720 g_object_set_data_full (G_OBJECT (data->store),
3721 "font-cache-for-get_font",
3723 (GDestroyNotify) pango_font_description_free);
3729 get_cursor_color (GtkStyleContext *context,
3733 GdkColor *style_color;
3735 gtk_style_context_get_style (context,
3736 primary ? "cursor-color" : "secondary-cursor-color",
3742 color->red = style_color->red / 65535.0;
3743 color->green = style_color->green / 65535.0;
3744 color->blue = style_color->blue / 65535.0;
3747 gdk_color_free (style_color);
3751 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3757 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3759 color->red = (color->red + bg.red) * 0.5;
3760 color->green = (color->green + bg.green) * 0.5;
3761 color->blue = (color->blue + bg.blue) * 0.5;
3767 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3768 GdkRGBA *primary_color,
3769 GdkRGBA *secondary_color)
3772 get_cursor_color (context, TRUE, primary_color);
3774 if (secondary_color)
3775 get_cursor_color (context, FALSE, secondary_color);
3782 * @context: a #GtkStyleContext
3784 * @x: X origin of the rectangle
3785 * @y: Y origin of the rectangle
3786 * @width: rectangle width
3787 * @height: rectangle height
3789 * Renders a checkmark (as in a #GtkCheckButton).
3791 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3792 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3793 * should be marked as undefined.
3796 * <title>Typical checkmark rendering</title>
3797 * <inlinegraphic fileref="checks.png" format="PNG"/>
3803 gtk_render_check (GtkStyleContext *context,
3810 GtkStyleContextPrivate *priv;
3811 GtkThemingEngineClass *engine_class;
3813 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3814 g_return_if_fail (cr != NULL);
3816 if (width <= 0 || height <= 0)
3819 priv = context->priv;
3820 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3824 store_animation_region (context, x, y, width, height);
3826 _gtk_theming_engine_set_context (priv->theming_engine, context);
3827 engine_class->render_check (priv->theming_engine, cr,
3828 x, y, width, height);
3834 * gtk_render_option:
3835 * @context: a #GtkStyleContext
3837 * @x: X origin of the rectangle
3838 * @y: Y origin of the rectangle
3839 * @width: rectangle width
3840 * @height: rectangle height
3842 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3843 * state will determine whether the option is on or off, and
3844 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3847 * <title>Typical option mark rendering</title>
3848 * <inlinegraphic fileref="options.png" format="PNG"/>
3854 gtk_render_option (GtkStyleContext *context,
3861 GtkStyleContextPrivate *priv;
3862 GtkThemingEngineClass *engine_class;
3864 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3865 g_return_if_fail (cr != NULL);
3867 if (width <= 0 || height <= 0)
3870 priv = context->priv;
3871 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3875 store_animation_region (context, x, y, width, height);
3877 _gtk_theming_engine_set_context (priv->theming_engine, context);
3878 engine_class->render_option (priv->theming_engine, cr,
3879 x, y, width, height);
3886 * @context: a #GtkStyleContext
3888 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3889 * @x: X origin of the render area
3890 * @y: Y origin of the render area
3891 * @size: square side for render area
3893 * Renders an arrow pointing to @angle.
3896 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3897 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3903 gtk_render_arrow (GtkStyleContext *context,
3910 GtkStyleContextPrivate *priv;
3911 GtkThemingEngineClass *engine_class;
3913 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3914 g_return_if_fail (cr != NULL);
3919 priv = context->priv;
3920 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3924 gtk_style_context_save (context);
3925 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3927 store_animation_region (context, x, y, size, size);
3929 _gtk_theming_engine_set_context (priv->theming_engine, context);
3930 engine_class->render_arrow (priv->theming_engine, cr,
3933 gtk_style_context_restore (context);
3938 * gtk_render_background:
3939 * @context: a #GtkStyleContext
3941 * @x: X origin of the rectangle
3942 * @y: Y origin of the rectangle
3943 * @width: rectangle width
3944 * @height: rectangle height
3946 * Renders the background of an element.
3949 * <title>Typical background rendering, showing the effect of
3950 * <parameter>background-image</parameter>,
3951 * <parameter>border-width</parameter> and
3952 * <parameter>border-radius</parameter></title>
3953 * <inlinegraphic fileref="background.png" format="PNG"/>
3959 gtk_render_background (GtkStyleContext *context,
3966 GtkStyleContextPrivate *priv;
3967 GtkThemingEngineClass *engine_class;
3969 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3970 g_return_if_fail (cr != NULL);
3972 if (width <= 0 || height <= 0)
3975 priv = context->priv;
3976 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3980 store_animation_region (context, x, y, width, height);
3982 _gtk_theming_engine_set_context (priv->theming_engine, context);
3983 engine_class->render_background (priv->theming_engine, cr, x, y, width, height);
3990 * @context: a #GtkStyleContext
3992 * @x: X origin of the rectangle
3993 * @y: Y origin of the rectangle
3994 * @width: rectangle width
3995 * @height: rectangle height
3997 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
4000 * <title>Examples of frame rendering, showing the effect of
4001 * <parameter>border-image</parameter>,
4002 * <parameter>border-color</parameter>,
4003 * <parameter>border-width</parameter>,
4004 * <parameter>border-radius</parameter> and
4006 * <inlinegraphic fileref="frames.png" format="PNG"/>
4012 gtk_render_frame (GtkStyleContext *context,
4019 GtkStyleContextPrivate *priv;
4020 GtkThemingEngineClass *engine_class;
4022 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4023 g_return_if_fail (cr != NULL);
4025 if (width <= 0 || height <= 0)
4028 priv = context->priv;
4029 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4033 store_animation_region (context, x, y, width, height);
4035 _gtk_theming_engine_set_context (priv->theming_engine, context);
4036 engine_class->render_frame (priv->theming_engine, cr, x, y, width, height);
4042 * gtk_render_expander:
4043 * @context: a #GtkStyleContext
4045 * @x: X origin of the rectangle
4046 * @y: Y origin of the rectangle
4047 * @width: rectangle width
4048 * @height: rectangle height
4050 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
4051 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
4052 * determines whether the expander is collapsed or expanded.
4055 * <title>Typical expander rendering</title>
4056 * <inlinegraphic fileref="expanders.png" format="PNG"/>
4062 gtk_render_expander (GtkStyleContext *context,
4069 GtkStyleContextPrivate *priv;
4070 GtkThemingEngineClass *engine_class;
4072 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4073 g_return_if_fail (cr != NULL);
4075 if (width <= 0 || height <= 0)
4078 priv = context->priv;
4079 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4083 store_animation_region (context, x, y, width, height);
4085 _gtk_theming_engine_set_context (priv->theming_engine, context);
4086 engine_class->render_expander (priv->theming_engine, cr, x, y, width, height);
4093 * @context: a #GtkStyleContext
4095 * @x: X origin of the rectangle
4096 * @y: Y origin of the rectangle
4097 * @width: rectangle width
4098 * @height: rectangle height
4100 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
4102 * <title>Typical focus rendering</title>
4103 * <inlinegraphic fileref="focus.png" format="PNG"/>
4109 gtk_render_focus (GtkStyleContext *context,
4116 GtkStyleContextPrivate *priv;
4117 GtkThemingEngineClass *engine_class;
4119 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4120 g_return_if_fail (cr != NULL);
4122 if (width <= 0 || height <= 0)
4125 priv = context->priv;
4126 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4130 store_animation_region (context, x, y, width, height);
4132 _gtk_theming_engine_set_context (priv->theming_engine, context);
4133 engine_class->render_focus (priv->theming_engine, cr, x, y, width, height);
4139 * gtk_render_layout:
4140 * @context: a #GtkStyleContext
4144 * @layout: the #PangoLayout to render
4146 * Renders @layout on the coordinates @x, @y
4151 gtk_render_layout (GtkStyleContext *context,
4155 PangoLayout *layout)
4157 GtkStyleContextPrivate *priv;
4158 GtkThemingEngineClass *engine_class;
4159 PangoRectangle extents;
4161 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4162 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4163 g_return_if_fail (cr != NULL);
4165 priv = context->priv;
4166 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4170 pango_layout_get_extents (layout, &extents, NULL);
4172 store_animation_region (context,
4178 _gtk_theming_engine_set_context (priv->theming_engine, context);
4179 engine_class->render_layout (priv->theming_engine, cr, x, y, layout);
4186 * @context: a #GtkStyleContext
4188 * @x0: X coordinate for the origin of the line
4189 * @y0: Y coordinate for the origin of the line
4190 * @x1: X coordinate for the end of the line
4191 * @y1: Y coordinate for the end of the line
4193 * Renders a line from (x0, y0) to (x1, y1).
4198 gtk_render_line (GtkStyleContext *context,
4205 GtkStyleContextPrivate *priv;
4206 GtkThemingEngineClass *engine_class;
4208 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4209 g_return_if_fail (cr != NULL);
4211 priv = context->priv;
4212 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4216 _gtk_theming_engine_set_context (priv->theming_engine, context);
4217 engine_class->render_line (priv->theming_engine, cr, x0, y0, x1, y1);
4223 * gtk_render_slider:
4224 * @context: a #GtkStyleContext
4226 * @x: X origin of the rectangle
4227 * @y: Y origin of the rectangle
4228 * @width: rectangle width
4229 * @height: rectangle height
4230 * @orientation: orientation of the slider
4232 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4233 * @width, @height. @orientation defines whether the slider is vertical
4237 * <title>Typical slider rendering</title>
4238 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4244 gtk_render_slider (GtkStyleContext *context,
4250 GtkOrientation orientation)
4252 GtkStyleContextPrivate *priv;
4253 GtkThemingEngineClass *engine_class;
4255 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4256 g_return_if_fail (cr != NULL);
4258 if (width <= 0 || height <= 0)
4261 priv = context->priv;
4262 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4266 store_animation_region (context, x, y, width, height);
4268 _gtk_theming_engine_set_context (priv->theming_engine, context);
4269 engine_class->render_slider (priv->theming_engine, cr, x, y, width, height, orientation);
4275 * gtk_render_frame_gap:
4276 * @context: a #GtkStyleContext
4278 * @x: X origin of the rectangle
4279 * @y: Y origin of the rectangle
4280 * @width: rectangle width
4281 * @height: rectangle height
4282 * @gap_side: side where the gap is
4283 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4284 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4286 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4287 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4288 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4289 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4292 * <title>Typical rendering of a frame with a gap</title>
4293 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4299 gtk_render_frame_gap (GtkStyleContext *context,
4305 GtkPositionType gap_side,
4309 GtkStyleContextPrivate *priv;
4310 GtkThemingEngineClass *engine_class;
4312 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4313 g_return_if_fail (cr != NULL);
4314 g_return_if_fail (xy0_gap <= xy1_gap);
4315 g_return_if_fail (xy0_gap >= 0);
4317 if (width <= 0 || height <= 0)
4320 if (gap_side == GTK_POS_LEFT ||
4321 gap_side == GTK_POS_RIGHT)
4322 g_return_if_fail (xy1_gap <= height);
4324 g_return_if_fail (xy1_gap <= width);
4326 priv = context->priv;
4327 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4331 store_animation_region (context, x, y, width, height);
4333 _gtk_theming_engine_set_context (priv->theming_engine, context);
4334 engine_class->render_frame_gap (priv->theming_engine, cr,
4335 x, y, width, height, gap_side,
4342 * gtk_render_extension:
4343 * @context: a #GtkStyleContext
4345 * @x: X origin of the rectangle
4346 * @y: Y origin of the rectangle
4347 * @width: rectangle width
4348 * @height: rectangle height
4349 * @gap_side: side where the gap is
4351 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4352 * defined by @x, @y, @width, @height. The side where the extension
4353 * connects to is defined by @gap_side.
4356 * <title>Typical extension rendering</title>
4357 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4363 gtk_render_extension (GtkStyleContext *context,
4369 GtkPositionType gap_side)
4371 GtkStyleContextPrivate *priv;
4372 GtkThemingEngineClass *engine_class;
4374 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4375 g_return_if_fail (cr != NULL);
4377 if (width <= 0 || height <= 0)
4380 priv = context->priv;
4381 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4385 store_animation_region (context, x, y, width, height);
4387 _gtk_theming_engine_set_context (priv->theming_engine, context);
4388 engine_class->render_extension (priv->theming_engine, cr, x, y, width, height, gap_side);
4394 * gtk_render_handle:
4395 * @context: a #GtkStyleContext
4397 * @x: X origin of the rectangle
4398 * @y: Y origin of the rectangle
4399 * @width: rectangle width
4400 * @height: rectangle height
4402 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4403 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4404 * determined by @x, @y, @width, @height.
4407 * <title>Handles rendered for the paned and grip classes</title>
4408 * <inlinegraphic fileref="handles.png" format="PNG"/>
4414 gtk_render_handle (GtkStyleContext *context,
4421 GtkStyleContextPrivate *priv;
4422 GtkThemingEngineClass *engine_class;
4424 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4425 g_return_if_fail (cr != NULL);
4427 if (width <= 0 || height <= 0)
4430 priv = context->priv;
4431 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4435 store_animation_region (context, x, y, width, height);
4437 _gtk_theming_engine_set_context (priv->theming_engine, context);
4438 engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
4444 * gtk_render_activity:
4445 * @context: a #GtkStyleContext
4447 * @x: X origin of the rectangle
4448 * @y: Y origin of the rectangle
4449 * @width: rectangle width
4450 * @height: rectangle height
4452 * Renders an activity area (Such as in #GtkSpinner or the
4453 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4454 * determines whether there is activity going on.
4459 gtk_render_activity (GtkStyleContext *context,
4466 GtkStyleContextPrivate *priv;
4467 GtkThemingEngineClass *engine_class;
4469 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4470 g_return_if_fail (cr != NULL);
4472 if (width <= 0 || height <= 0)
4475 priv = context->priv;
4476 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4480 store_animation_region (context, x, y, width, height);
4482 _gtk_theming_engine_set_context (priv->theming_engine, context);
4483 engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
4489 * gtk_render_icon_pixbuf:
4490 * @context: a #GtkStyleContext
4491 * @source: the #GtkIconSource specifying the icon to render
4492 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4493 * means render at the size of the source and don't scale.
4495 * Renders the icon specified by @source at the given @size, returning the result
4498 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4503 gtk_render_icon_pixbuf (GtkStyleContext *context,
4504 const GtkIconSource *source,
4507 GtkStyleContextPrivate *priv;
4508 GtkThemingEngineClass *engine_class;
4510 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4511 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4512 g_return_val_if_fail (source != NULL, NULL);
4514 priv = context->priv;
4515 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4517 _gtk_theming_engine_set_context (priv->theming_engine, context);
4518 return engine_class->render_icon_pixbuf (priv->theming_engine, source, size);
4523 * @context: a #GtkStyleContext
4525 * @pixbuf: a #GdkPixbuf containing the icon to draw
4526 * @x: X position for the @pixbuf
4527 * @y: Y position for the @pixbuf
4529 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4534 gtk_render_icon (GtkStyleContext *context,
4540 GtkStyleContextPrivate *priv;
4541 GtkThemingEngineClass *engine_class;
4543 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4544 g_return_if_fail (cr != NULL);
4546 priv = context->priv;
4547 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4551 store_animation_region (context,
4553 gdk_pixbuf_get_width (pixbuf),
4554 gdk_pixbuf_get_height (pixbuf));
4556 _gtk_theming_engine_set_context (priv->theming_engine, context);
4557 engine_class->render_icon (priv->theming_engine, cr, pixbuf, x, y);
4563 draw_insertion_cursor (GtkStyleContext *context,
4568 gboolean is_primary,
4569 PangoDirection direction,
4570 gboolean draw_arrow)
4573 GdkRGBA primary_color;
4574 GdkRGBA secondary_color;
4575 gfloat cursor_aspect_ratio;
4581 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4582 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4584 /* When changing the shape or size of the cursor here,
4585 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4588 gtk_style_context_get_style (context,
4589 "cursor-aspect-ratio", &cursor_aspect_ratio,
4592 stem_width = height * cursor_aspect_ratio + 1;
4594 /* put (stem_width % 2) on the proper side of the cursor */
4595 if (direction == PANGO_DIRECTION_LTR)
4596 offset = stem_width / 2;
4598 offset = stem_width - stem_width / 2;
4600 cairo_rectangle (cr, x - offset, y, stem_width, height);
4608 arrow_width = stem_width + 1;
4610 if (direction == PANGO_DIRECTION_RTL)
4612 ax = x - offset - 1;
4613 ay = y + height - arrow_width * 2 - arrow_width + 1;
4615 cairo_move_to (cr, ax, ay + 1);
4616 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4617 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4620 else if (direction == PANGO_DIRECTION_LTR)
4622 ax = x + stem_width - offset;
4623 ay = y + height - arrow_width * 2 - arrow_width + 1;
4625 cairo_move_to (cr, ax, ay + 1);
4626 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4627 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4631 g_assert_not_reached();
4638 * gtk_render_insertion_cursor:
4639 * @context: a #GtkStyleContext
4643 * @layout: the #PangoLayout of the text
4644 * @index: the index in the #PangoLayout
4645 * @direction: the #PangoDirection of the text
4647 * Draws a text caret on @cr at the specified index of @layout.
4652 gtk_render_insertion_cursor (GtkStyleContext *context,
4656 PangoLayout *layout,
4658 PangoDirection direction)
4660 GtkStyleContextPrivate *priv;
4661 gboolean split_cursor;
4662 PangoRectangle strong_pos, weak_pos;
4663 PangoRectangle *cursor1, *cursor2;
4664 PangoDirection keymap_direction;
4665 PangoDirection direction2;
4667 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4668 g_return_if_fail (cr != NULL);
4669 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4670 g_return_if_fail (index >= 0);
4672 priv = context->priv;
4674 g_object_get (gtk_settings_get_for_screen (priv->screen),
4675 "gtk-split-cursor", &split_cursor,
4678 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4680 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4682 direction2 = PANGO_DIRECTION_NEUTRAL;
4686 cursor1 = &strong_pos;
4688 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4690 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4691 cursor2 = &weak_pos;
4696 if (keymap_direction == direction)
4697 cursor1 = &strong_pos;
4699 cursor1 = &weak_pos;
4702 draw_insertion_cursor (context,
4704 x + PANGO_PIXELS (cursor1->x),
4705 y + PANGO_PIXELS (cursor1->y),
4706 PANGO_PIXELS (cursor1->height),
4709 direction2 != PANGO_DIRECTION_NEUTRAL);
4711 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4713 draw_insertion_cursor (context,
4715 x + PANGO_PIXELS (cursor2->x),
4716 y + PANGO_PIXELS (cursor2->y),
4717 PANGO_PIXELS (cursor2->height),
4725 * gtk_draw_insertion_cursor:
4726 * @widget: a #GtkWidget
4727 * @cr: cairo context to draw to
4728 * @location: location where to draw the cursor (@location->width is ignored)
4729 * @is_primary: if the cursor should be the primary cursor color.
4730 * @direction: whether the cursor is left-to-right or
4731 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4732 * @draw_arrow: %TRUE to draw a directional arrow on the
4733 * cursor. Should be %FALSE unless the cursor is split.
4735 * Draws a text caret on @cr at @location. This is not a style function
4736 * but merely a convenience function for drawing the standard cursor shape.
4739 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4742 gtk_draw_insertion_cursor (GtkWidget *widget,
4744 const GdkRectangle *location,
4745 gboolean is_primary,
4746 GtkTextDirection direction,
4747 gboolean draw_arrow)
4749 GtkStyleContext *context;
4751 g_return_if_fail (GTK_IS_WIDGET (widget));
4752 g_return_if_fail (cr != NULL);
4753 g_return_if_fail (location != NULL);
4754 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4756 context = gtk_widget_get_style_context (widget);
4758 draw_insertion_cursor (context, cr,
4759 location->x, location->y, location->height,
4761 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4765 static AtkAttributeSet *
4766 add_attribute (AtkAttributeSet *attributes,
4767 AtkTextAttribute attr,
4772 at = g_new (AtkAttribute, 1);
4773 at->name = g_strdup (atk_text_attribute_get_name (attr));
4774 at->value = g_strdup (value);
4776 return g_slist_prepend (attributes, at);
4780 * _gtk_style_context_get_attributes:
4781 * @attributes: a #AtkAttributeSet to add attributes to
4782 * @context: the #GtkStyleContext to get attributes from
4783 * @flags: the state to use with @context
4785 * Adds the foreground and background color from @context to
4786 * @attributes, after translating them to ATK attributes.
4788 * This is a convenience function that can be used in
4789 * implementing the #AtkText interface in widgets.
4791 * Returns: the modified #AtkAttributeSet
4794 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4795 GtkStyleContext *context,
4796 GtkStateFlags flags)
4801 gtk_style_context_get_background_color (context, flags, &color);
4802 value = g_strdup_printf ("%u,%u,%u",
4803 (guint) ceil (color.red * 65536 - color.red),
4804 (guint) ceil (color.green * 65536 - color.green),
4805 (guint) ceil (color.blue * 65536 - color.blue));
4806 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4809 gtk_style_context_get_color (context, flags, &color);
4810 value = g_strdup_printf ("%u,%u,%u",
4811 (guint) ceil (color.red * 65536 - color.red),
4812 (guint) ceil (color.green * 65536 - color.green),
4813 (guint) ceil (color.blue * 65536 - color.blue));
4814 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);