1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 #include <gobject/gvaluecollector.h>
25 #include "gtkstylecontextprivate.h"
26 #include "gtkstylepropertiesprivate.h"
27 #include "gtktypebuiltins.h"
28 #include "gtkthemingengineprivate.h"
30 #include "gtkwidget.h"
31 #include "gtkwindow.h"
32 #include "gtkprivate.h"
33 #include "gtksymboliccolorprivate.h"
34 #include "gtkanimationdescription.h"
35 #include "gtktimeline.h"
36 #include "gtkiconfactory.h"
37 #include "gtkwidgetpath.h"
38 #include "gtkwidgetprivate.h"
39 #include "gtkstylecascadeprivate.h"
40 #include "gtkstyleproviderprivate.h"
41 #include "gtksettings.h"
44 * SECTION:gtkstylecontext
45 * @Short_description: Rendering UI elements
46 * @Title: GtkStyleContext
48 * #GtkStyleContext is an object that stores styling information affecting
49 * a widget defined by #GtkWidgetPath.
51 * In order to construct the final style information, #GtkStyleContext
52 * queries information from all attached #GtkStyleProviders. Style providers
53 * can be either attached explicitly to the context through
54 * gtk_style_context_add_provider(), or to the screen through
55 * gtk_style_context_add_provider_for_screen(). The resulting style is a
56 * combination of all providers' information in priority order.
58 * For GTK+ widgets, any #GtkStyleContext returned by
59 * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
60 * #GdkScreen and RTL/LTR information set. The style context will be also
61 * updated automatically if any of these settings change on the widget.
63 * If you are using the theming layer standalone, you will need to set a
64 * widget path and a screen yourself to the created style context through
65 * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
66 * as updating the context yourself using gtk_style_context_invalidate()
67 * whenever any of the conditions change, such as a change in the
68 * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
71 * <refsect2 id="gtkstylecontext-animations">
72 * <title>Transition animations</title>
74 * #GtkStyleContext has built-in support for state change transitions.
75 * Note that these animations respect the #GtkSettings:gtk-enable-animations
79 * For simple widgets where state changes affect the whole widget area,
80 * calling gtk_style_context_notify_state_change() with a %NULL region
81 * is sufficient to trigger the transition animation. And GTK+ already
82 * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
86 * If a widget needs to declare several animatable regions (i.e. not
87 * affecting the whole widget area), its #GtkWidget::draw signal handler
88 * needs to wrap the render operations for the different regions with
89 * calls to gtk_style_context_push_animatable_region() and
90 * gtk_style_context_pop_animatable_region(). These functions take an
91 * identifier for the region which must be unique within the style context.
92 * For simple widgets with a fixed set of animatable regions, using an
93 * enumeration works well:
96 * <title>Using an enumeration to identify animatable regions</title>
107 * spin_button_draw (GtkWidget *widget,
110 * GtkStyleContext *context;
112 * context = gtk_widget_get_style_context (widget);
114 * gtk_style_context_push_animatable_region (context,
115 * GUINT_TO_POINTER (REGION_ENTRY));
117 * gtk_render_background (cr, 0, 0, 100, 30);
118 * gtk_render_frame (cr, 0, 0, 100, 30);
120 * gtk_style_context_pop_animatable_region (context);
127 * For complex widgets with an arbitrary number of animatable regions, it
128 * is up to the implementation to come up with a way to uniquely identify
129 * each animatable region. Using pointers to internal structs is one way
133 * <title>Using struct pointers to identify animatable regions</title>
136 * notebook_draw_tab (GtkWidget *widget,
137 * NotebookPage *page,
140 * gtk_style_context_push_animatable_region (context, page);
141 * gtk_render_extension (cr, page->x, page->y, page->width, page->height);
142 * gtk_style_context_pop_animatable_region (context);
147 * The widget also needs to notify the style context about a state change
148 * for a given animatable region so the animation is triggered.
151 * <title>Triggering a state change animation on a region</title>
154 * notebook_motion_notify (GtkWidget *widget,
155 * GdkEventMotion *event)
157 * GtkStyleContext *context;
158 * NotebookPage *page;
160 * context = gtk_widget_get_style_context (widget);
161 * page = find_page_under_pointer (widget, event);
162 * gtk_style_context_notify_state_change (context,
163 * gtk_widget_get_window (widget),
165 * GTK_STATE_PRELIGHT,
172 * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
173 * special value, in this case, the whole widget area will be updated
177 * <refsect2 id="gtkstylecontext-classes">
178 * <title>Style classes and regions</title>
180 * Widgets can add style classes to their context, which can be used
181 * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
182 * rendering. GTK+ has a number of predefined style classes:
183 * #GTK_STYLE_CLASS_CELL,
184 * #GTK_STYLE_CLASS_ENTRY,
185 * #GTK_STYLE_CLASS_BUTTON,
186 * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
187 * #GTK_STYLE_CLASS_CALENDAR,
188 * #GTK_STYLE_CLASS_SLIDER,
189 * #GTK_STYLE_CLASS_BACKGROUND,
190 * #GTK_STYLE_CLASS_RUBBERBAND,
191 * #GTK_STYLE_CLASS_TOOLTIP,
192 * #GTK_STYLE_CLASS_MENU,
193 * #GTK_STYLE_CLASS_MENUBAR,
194 * #GTK_STYLE_CLASS_MENUITEM,
195 * #GTK_STYLE_CLASS_TOOLBAR,
196 * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
197 * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
198 * #GTK_STYLE_CLASS_RADIO,
199 * #GTK_STYLE_CLASS_CHECK,
200 * #GTK_STYLE_CLASS_TROUGH,
201 * #GTK_STYLE_CLASS_SCROLLBAR,
202 * #GTK_STYLE_CLASS_SCALE,
203 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
204 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
205 * #GTK_STYLE_CLASS_HEADER,
206 * #GTK_STYLE_CLASS_ACCELERATOR,
207 * #GTK_STYLE_CLASS_GRIP,
208 * #GTK_STYLE_CLASS_DOCK,
209 * #GTK_STYLE_CLASS_PROGRESSBAR,
210 * #GTK_STYLE_CLASS_SPINNER,
211 * #GTK_STYLE_CLASS_EXPANDER,
212 * #GTK_STYLE_CLASS_SPINBUTTON,
213 * #GTK_STYLE_CLASS_NOTEBOOK,
214 * #GTK_STYLE_CLASS_VIEW,
215 * #GTK_STYLE_CLASS_SIDEBAR,
216 * #GTK_STYLE_CLASS_IMAGE,
217 * #GTK_STYLE_CLASS_HIGHLIGHT,
218 * #GTK_STYLE_CLASS_FRAME,
219 * #GTK_STYLE_CLASS_DND,
220 * #GTK_STYLE_CLASS_PANE_SEPARATOR,
221 * #GTK_STYLE_CLASS_SEPARATOR,
222 * #GTK_STYLE_CLASS_INFO,
223 * #GTK_STYLE_CLASS_WARNING,
224 * #GTK_STYLE_CLASS_QUESTION,
225 * #GTK_STYLE_CLASS_ERROR,
226 * #GTK_STYLE_CLASS_HORIZONTAL,
227 * #GTK_STYLE_CLASS_VERTICAL,
228 * #GTK_STYLE_CLASS_TOP,
229 * #GTK_STYLE_CLASS_BOTTOM,
230 * #GTK_STYLE_CLASS_LEFT,
231 * #GTK_STYLE_CLASS_RIGHT,
234 * Widgets can also add regions with flags to their context.
235 * The regions used by GTK+ widgets are:
240 * <entry>Region</entry>
241 * <entry>Flags</entry>
242 * <entry>Macro</entry>
243 * <entry>Used by</entry>
249 * <entry>even, odd</entry>
250 * <entry>GTK_STYLE_REGION_ROW</entry>
251 * <entry>#GtkTreeView</entry>
254 * <entry>column</entry>
255 * <entry>first, last, sorted</entry>
256 * <entry>GTK_STYLE_REGION_COLUMN</entry>
257 * <entry>#GtkTreeView</entry>
260 * <entry>column-header</entry>
262 * <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
267 * <entry>even, odd, first, last</entry>
268 * <entry>GTK_STYLE_REGION_TAB</entry>
269 * <entry>#GtkNotebook</entry>
276 * <refsect2 id="gtkstylecontext-custom-styling">
277 * <title>Custom styling in UI libraries and applications</title>
279 * If you are developing a library with custom #GtkWidget<!-- -->s that
280 * render differently than standard components, you may need to add a
281 * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
282 * priority, either a #GtkCssProvider or a custom object implementing the
283 * #GtkStyleProvider interface. This way theming engines may still attempt
284 * to style your UI elements in a different way if needed so.
287 * If you are using custom styling on an applications, you probably want then
288 * to make your style information prevail to the theme's, so you must use
289 * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
290 * priority, keep in mind that the user settings in
291 * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
292 * still take precedence over your changes, as it uses the
293 * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
296 * If a custom theming engine is needed, you probably want to implement a
297 * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
298 * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
299 * which loads the theming engine module from the standard paths.
304 typedef struct GtkStyleInfo GtkStyleInfo;
305 typedef struct GtkRegion GtkRegion;
306 typedef struct PropertyValue PropertyValue;
307 typedef struct AnimationInfo AnimationInfo;
308 typedef struct StyleData StyleData;
313 GtkRegionFlags flags;
326 GArray *style_classes;
328 GtkJunctionSides junction_sides;
329 GtkStateFlags state_flags;
334 GtkCssComputedValues *store;
335 GArray *property_cache;
340 GtkTimeline *timeline;
344 /* Region stack (until region_id) at the time of
345 * rendering, this is used for nested cancellation.
347 GSList *parent_regions;
351 gboolean target_value;
353 cairo_region_t *invalidation_region;
357 struct _GtkStyleContextPrivate
361 GtkStyleCascade *cascade;
363 GtkStyleContext *parent;
366 GtkWidgetPath *widget_path;
367 GHashTable *style_data;
369 StyleData *current_data;
370 GtkStateFlags current_state;
372 GSList *animation_regions;
375 GtkThemingEngine *theming_engine;
377 GtkTextDirection direction;
379 guint animations_invalidated : 1;
380 guint invalidating_context : 1;
395 static guint signals[LAST_SIGNAL] = { 0 };
397 static void gtk_style_context_finalize (GObject *object);
399 static void gtk_style_context_impl_set_property (GObject *object,
403 static void gtk_style_context_impl_get_property (GObject *object,
407 static GtkSymbolicColor *
408 gtk_style_context_color_lookup_func (gpointer contextp,
412 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
415 gtk_style_context_real_changed (GtkStyleContext *context)
417 GtkStyleContextPrivate *priv = context->priv;
420 _gtk_widget_style_context_invalidated (priv->widget);
424 gtk_style_context_class_init (GtkStyleContextClass *klass)
426 GObjectClass *object_class = G_OBJECT_CLASS (klass);
428 object_class->finalize = gtk_style_context_finalize;
429 object_class->set_property = gtk_style_context_impl_set_property;
430 object_class->get_property = gtk_style_context_impl_get_property;
432 klass->changed = gtk_style_context_real_changed;
435 g_signal_new (I_("changed"),
436 G_TYPE_FROM_CLASS (object_class),
438 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
440 g_cclosure_marshal_VOID__VOID,
443 g_object_class_install_property (object_class,
445 g_param_spec_object ("screen",
447 P_("The associated GdkScreen"),
449 GTK_PARAM_READWRITE));
450 g_object_class_install_property (object_class,
452 g_param_spec_enum ("direction",
454 P_("Text direction"),
455 GTK_TYPE_TEXT_DIRECTION,
457 GTK_PARAM_READWRITE));
459 * GtkStyleContext:parent:
461 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
466 g_object_class_install_property (object_class,
468 g_param_spec_object ("parent",
470 P_("The parent style context"),
471 GTK_TYPE_STYLE_CONTEXT,
472 GTK_PARAM_READWRITE));
474 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
477 static GtkStyleInfo *
478 style_info_new (void)
482 info = g_slice_new0 (GtkStyleInfo);
483 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
484 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
490 style_info_free (GtkStyleInfo *info)
492 g_array_free (info->style_classes, TRUE);
493 g_array_free (info->regions, TRUE);
494 g_slice_free (GtkStyleInfo, info);
497 static GtkStyleInfo *
498 style_info_copy (const GtkStyleInfo *info)
502 copy = style_info_new ();
503 g_array_insert_vals (copy->style_classes, 0,
504 info->style_classes->data,
505 info->style_classes->len);
507 g_array_insert_vals (copy->regions, 0,
511 copy->junction_sides = info->junction_sides;
512 copy->state_flags = info->state_flags;
518 style_info_hash (gconstpointer elem)
520 const GtkStyleInfo *info;
525 for (i = 0; i < info->style_classes->len; i++)
527 hash += g_array_index (info->style_classes, GQuark, i);
531 for (i = 0; i < info->regions->len; i++)
535 region = &g_array_index (info->regions, GtkRegion, i);
536 hash += region->class_quark;
537 hash += region->flags;
541 return hash ^ info->state_flags;
545 style_info_equal (gconstpointer elem1,
548 const GtkStyleInfo *info1, *info2;
553 if (info1->state_flags != info2->state_flags)
556 if (info1->junction_sides != info2->junction_sides)
559 if (info1->style_classes->len != info2->style_classes->len)
562 if (memcmp (info1->style_classes->data,
563 info2->style_classes->data,
564 info1->style_classes->len * sizeof (GQuark)) != 0)
567 if (info1->regions->len != info2->regions->len)
570 if (memcmp (info1->regions->data,
571 info2->regions->data,
572 info1->regions->len * sizeof (GtkRegion)) != 0)
579 style_data_new (void)
583 data = g_slice_new0 (StyleData);
589 clear_property_cache (StyleData *data)
593 if (!data->property_cache)
596 for (i = 0; i < data->property_cache->len; i++)
598 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
600 g_param_spec_unref (node->pspec);
601 g_value_unset (&node->value);
604 g_array_free (data->property_cache, TRUE);
605 data->property_cache = NULL;
609 style_data_free (StyleData *data)
611 g_object_unref (data->store);
612 clear_property_cache (data);
614 g_slice_free (StyleData, data);
618 gtk_style_context_init (GtkStyleContext *style_context)
620 GtkStyleContextPrivate *priv;
623 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
624 GTK_TYPE_STYLE_CONTEXT,
625 GtkStyleContextPrivate);
627 priv->style_data = g_hash_table_new_full (style_info_hash,
629 (GDestroyNotify) style_info_free,
630 (GDestroyNotify) style_data_free);
631 priv->theming_engine = g_object_ref ((gpointer) gtk_theming_engine_load (NULL));
633 priv->direction = GTK_TEXT_DIR_LTR;
635 priv->screen = gdk_screen_get_default ();
636 priv->cascade = _gtk_style_cascade_get_for_screen (priv->screen);
637 g_object_ref (priv->cascade);
639 /* Create default info store */
640 info = style_info_new ();
641 priv->info_stack = g_slist_prepend (priv->info_stack, info);
645 animation_info_free (AnimationInfo *info)
647 g_object_unref (info->timeline);
648 g_object_unref (info->window);
650 if (info->invalidation_region)
651 cairo_region_destroy (info->invalidation_region);
653 g_array_free (info->rectangles, TRUE);
654 g_slist_free (info->parent_regions);
655 g_slice_free (AnimationInfo, info);
658 static AnimationInfo *
659 animation_info_lookup_by_timeline (GtkStyleContext *context,
660 GtkTimeline *timeline)
662 GtkStyleContextPrivate *priv;
666 priv = context->priv;
668 for (l = priv->animations; l; l = l->next)
672 if (info->timeline == timeline)
680 timeline_frame_cb (GtkTimeline *timeline,
684 GtkStyleContextPrivate *priv;
685 GtkStyleContext *context;
689 priv = context->priv;
690 info = animation_info_lookup_by_timeline (context, timeline);
692 g_assert (info != NULL);
694 /* Cancel transition if window is gone */
695 if (gdk_window_is_destroyed (info->window) ||
696 !gdk_window_is_visible (info->window))
698 priv->animations = g_slist_remove (priv->animations, info);
699 animation_info_free (info);
703 if (info->invalidation_region &&
704 !cairo_region_is_empty (info->invalidation_region))
705 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
707 gdk_window_invalidate_rect (info->window, NULL, TRUE);
711 timeline_finished_cb (GtkTimeline *timeline,
714 GtkStyleContextPrivate *priv;
715 GtkStyleContext *context;
719 priv = context->priv;
720 info = animation_info_lookup_by_timeline (context, timeline);
722 g_assert (info != NULL);
724 priv->animations = g_slist_remove (priv->animations, info);
726 /* Invalidate one last time the area, so the final content is painted */
727 if (info->invalidation_region &&
728 !cairo_region_is_empty (info->invalidation_region))
729 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
731 gdk_window_invalidate_rect (info->window, NULL, TRUE);
733 animation_info_free (info);
736 static AnimationInfo *
737 animation_info_new (GtkStyleContext *context,
740 GtkTimelineProgressType progress_type,
743 gboolean target_value,
748 info = g_slice_new0 (AnimationInfo);
750 info->rectangles = g_array_new (FALSE, FALSE, sizeof (cairo_rectangle_int_t));
751 info->timeline = _gtk_timeline_new (duration);
752 info->window = g_object_ref (window);
754 info->target_value = target_value;
755 info->region_id = region_id;
757 _gtk_timeline_set_progress_type (info->timeline, progress_type);
758 _gtk_timeline_set_loop (info->timeline, loop);
760 if (!loop && !target_value)
762 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
763 _gtk_timeline_rewind (info->timeline);
766 g_signal_connect (info->timeline, "frame",
767 G_CALLBACK (timeline_frame_cb), context);
768 g_signal_connect (info->timeline, "finished",
769 G_CALLBACK (timeline_finished_cb), context);
771 _gtk_timeline_start (info->timeline);
776 static AnimationInfo *
777 animation_info_lookup (GtkStyleContext *context,
781 GtkStyleContextPrivate *priv;
784 priv = context->priv;
786 for (l = priv->animations; l; l = l->next)
792 if (info->state == state &&
793 info->region_id == region_id)
801 gtk_style_context_finalize (GObject *object)
803 GtkStyleContextPrivate *priv;
804 GtkStyleContext *style_context;
807 style_context = GTK_STYLE_CONTEXT (object);
808 priv = style_context->priv;
810 /* children hold a reference to us */
811 g_assert (priv->children == NULL);
813 gtk_style_context_set_parent (style_context, NULL);
815 if (priv->widget_path)
816 gtk_widget_path_free (priv->widget_path);
818 g_hash_table_destroy (priv->style_data);
820 g_object_unref (priv->cascade);
822 g_slist_free_full (priv->info_stack, (GDestroyNotify) style_info_free);
824 g_slist_free (priv->animation_regions);
826 for (l = priv->animations; l; l = l->next)
827 animation_info_free ((AnimationInfo *) l->data);
829 g_slist_free (priv->animations);
831 if (priv->theming_engine)
832 g_object_unref (priv->theming_engine);
834 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
838 gtk_style_context_impl_set_property (GObject *object,
843 GtkStyleContext *style_context;
845 style_context = GTK_STYLE_CONTEXT (object);
850 gtk_style_context_set_screen (style_context,
851 g_value_get_object (value));
854 gtk_style_context_set_direction (style_context,
855 g_value_get_enum (value));
858 gtk_style_context_set_parent (style_context,
859 g_value_get_object (value));
862 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
868 gtk_style_context_impl_get_property (GObject *object,
873 GtkStyleContext *style_context;
874 GtkStyleContextPrivate *priv;
876 style_context = GTK_STYLE_CONTEXT (object);
877 priv = style_context->priv;
882 g_value_set_object (value, priv->screen);
885 g_value_set_enum (value, priv->direction);
888 g_value_set_object (value, priv->parent);
891 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
897 build_properties (GtkStyleContext *context,
898 StyleData *style_data,
902 GtkStyleContextPrivate *priv;
903 GtkCssMatcher matcher;
904 GtkCssLookup *lookup;
906 priv = context->priv;
908 _gtk_css_matcher_init (&matcher, path, state);
909 lookup = _gtk_css_lookup_new ();
911 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
915 style_data->store = _gtk_css_computed_values_new ();
916 _gtk_css_lookup_resolve (lookup, context, style_data->store);
917 _gtk_css_lookup_free (lookup);
920 static GtkWidgetPath *
921 create_query_path (GtkStyleContext *context)
923 GtkStyleContextPrivate *priv;
928 priv = context->priv;
929 path = gtk_widget_path_copy (priv->widget ? gtk_widget_get_path (priv->widget) : priv->widget_path);
930 pos = gtk_widget_path_length (path) - 1;
932 info = priv->info_stack->data;
934 /* Set widget regions */
935 for (i = 0; i < info->regions->len; i++)
939 region = &g_array_index (info->regions, GtkRegion, i);
940 gtk_widget_path_iter_add_region (path, pos,
941 g_quark_to_string (region->class_quark),
945 /* Set widget classes */
946 for (i = 0; i < info->style_classes->len; i++)
950 quark = g_array_index (info->style_classes, GQuark, i);
951 gtk_widget_path_iter_add_class (path, pos,
952 g_quark_to_string (quark));
959 style_data_lookup (GtkStyleContext *context,
962 GtkStyleContextPrivate *priv;
964 gboolean state_mismatch;
967 priv = context->priv;
968 state_mismatch = ((GtkStyleInfo *) priv->info_stack->data)->state_flags != state;
970 /* Current data in use is cached, just return it */
971 if (priv->current_data && priv->current_state == state)
972 return priv->current_data;
974 g_assert (priv->widget != NULL || priv->widget_path != NULL);
976 if (G_UNLIKELY (state_mismatch))
978 gtk_style_context_save (context);
979 gtk_style_context_set_state (context, state);
982 priv->current_data = g_hash_table_lookup (priv->style_data, priv->info_stack->data);
983 priv->current_state = state;
985 if (!priv->current_data)
989 path = create_query_path (context);
991 priv->current_data = style_data_new ();
992 g_hash_table_insert (priv->style_data,
993 style_info_copy (priv->info_stack->data),
996 build_properties (context, priv->current_data, path, state);
998 gtk_widget_path_free (path);
1001 data = priv->current_data;
1003 if (priv->theming_engine)
1004 g_object_unref (priv->theming_engine);
1006 v = _gtk_css_computed_values_get_value_by_name (priv->current_data->store, "engine");
1008 priv->theming_engine = _gtk_css_value_dup_object (v);
1010 priv->theming_engine = g_object_ref (gtk_theming_engine_load (NULL));
1012 if (G_UNLIKELY (state_mismatch))
1013 gtk_style_context_restore (context);
1018 /* returns TRUE if someone called gtk_style_context_save() but hasn't
1019 * called gtk_style_context_restore() yet.
1020 * In those situations we don't invalidate the context when somebody
1021 * changes state/regions/classes.
1024 gtk_style_context_is_saved (GtkStyleContext *context)
1026 return context->priv->info_stack->next != NULL;
1030 gtk_style_context_queue_invalidate_internal (GtkStyleContext *context,
1031 GtkCssChange change)
1033 GtkStyleContextPrivate *priv = context->priv;
1035 priv->current_data = NULL;
1037 if (!gtk_style_context_is_saved (context))
1039 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_STATE);
1040 /* XXX: We need to invalidate siblings here somehow */
1045 * gtk_style_context_new:
1047 * Creates a standalone #GtkStyleContext, this style context
1048 * won't be attached to any widget, so you may want
1049 * to call gtk_style_context_set_path() yourself.
1052 * This function is only useful when using the theming layer
1053 * separated from GTK+, if you are using #GtkStyleContext to
1054 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1055 * in order to get a style context ready to theme the widget.
1058 * Returns: A newly created #GtkStyleContext.
1061 gtk_style_context_new (void)
1063 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1067 _gtk_style_context_set_widget (GtkStyleContext *context,
1070 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1071 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1073 context->priv->widget = widget;
1075 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_SELF);
1079 * gtk_style_context_add_provider:
1080 * @context: a #GtkStyleContext
1081 * @provider: a #GtkStyleProvider
1082 * @priority: the priority of the style provider. The lower
1083 * it is, the earlier it will be used in the style
1084 * construction. Typically this will be in the range
1085 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1086 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1088 * Adds a style provider to @context, to be used in style construction.
1090 * <note><para>If both priorities are the same, A #GtkStyleProvider
1091 * added through this function takes precedence over another added
1092 * through gtk_style_context_add_provider_for_screen().</para></note>
1097 gtk_style_context_add_provider (GtkStyleContext *context,
1098 GtkStyleProvider *provider,
1101 GtkStyleContextPrivate *priv;
1103 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1104 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1106 priv = context->priv;
1108 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1110 GtkStyleCascade *new_cascade;
1112 new_cascade = _gtk_style_cascade_new ();
1113 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1114 g_object_unref (priv->cascade);
1115 priv->cascade = new_cascade;
1118 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1120 gtk_style_context_invalidate (context);
1124 * gtk_style_context_remove_provider:
1125 * @context: a #GtkStyleContext
1126 * @provider: a #GtkStyleProvider
1128 * Removes @provider from the style providers list in @context.
1133 gtk_style_context_remove_provider (GtkStyleContext *context,
1134 GtkStyleProvider *provider)
1136 GtkStyleContextPrivate *priv;
1138 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1139 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1141 priv = context->priv;
1143 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1146 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1150 * gtk_style_context_reset_widgets:
1151 * @screen: a #GdkScreen
1153 * This function recomputes the styles for all widgets under a particular
1154 * #GdkScreen. This is useful when some global parameter has changed that
1155 * affects the appearance of all widgets, because when a widget gets a new
1156 * style, it will both redraw and recompute any cached information about
1157 * its appearance. As an example, it is used when the color scheme changes
1158 * in the related #GtkSettings object.
1163 gtk_style_context_reset_widgets (GdkScreen *screen)
1165 GList *list, *toplevels;
1167 _gtk_icon_set_invalidate_caches ();
1169 toplevels = gtk_window_list_toplevels ();
1170 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1172 for (list = toplevels; list; list = list->next)
1174 if (gtk_widget_get_screen (list->data) == screen)
1175 gtk_widget_reset_style (list->data);
1177 g_object_unref (list->data);
1180 g_list_free (toplevels);
1184 * gtk_style_context_add_provider_for_screen:
1185 * @screen: a #GdkScreen
1186 * @provider: a #GtkStyleProvider
1187 * @priority: the priority of the style provider. The lower
1188 * it is, the earlier it will be used in the style
1189 * construction. Typically this will be in the range
1190 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1191 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1193 * Adds a global style provider to @screen, which will be used
1194 * in style construction for all #GtkStyleContext<!-- -->s under
1197 * GTK+ uses this to make styling information from #GtkSettings
1200 * <note><para>If both priorities are the same, A #GtkStyleProvider
1201 * added through gtk_style_context_add_provider() takes precedence
1202 * over another added through this function.</para></note>
1207 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1208 GtkStyleProvider *provider,
1211 GtkStyleCascade *cascade;
1213 g_return_if_fail (GDK_IS_SCREEN (screen));
1214 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1216 cascade = _gtk_style_cascade_get_for_screen (screen);
1217 _gtk_style_cascade_add_provider (cascade, provider, priority);
1221 * gtk_style_context_remove_provider_for_screen:
1222 * @screen: a #GdkScreen
1223 * @provider: a #GtkStyleProvider
1225 * Removes @provider from the global style providers list in @screen.
1230 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1231 GtkStyleProvider *provider)
1233 GtkStyleCascade *cascade;
1235 g_return_if_fail (GDK_IS_SCREEN (screen));
1236 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1238 cascade = _gtk_style_cascade_get_for_screen (screen);
1239 _gtk_style_cascade_remove_provider (cascade, provider);
1243 * gtk_style_context_get_section:
1244 * @context: a #GtkStyleContext
1245 * @property: style property name
1247 * Queries the location in the CSS where @property was defined for the
1248 * current @context. Note that the state to be queried is taken from
1249 * gtk_style_context_get_state().
1251 * If the location is not available, %NULL will be returned. The
1252 * location might not be available for various reasons, such as the
1253 * property being overridden, @property not naming a supported CSS
1254 * property or tracking of definitions being disabled for performance
1257 * Shorthand CSS properties cannot be queried for a location and will
1258 * always return %NULL.
1260 * Returns: %NULL or the section where value was defined
1263 gtk_style_context_get_section (GtkStyleContext *context,
1264 const gchar *property)
1266 GtkStyleContextPrivate *priv;
1267 GtkStyleProperty *prop;
1270 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1271 g_return_val_if_fail (property != NULL, NULL);
1273 priv = context->priv;
1274 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1276 prop = _gtk_style_property_lookup (property);
1277 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1280 data = style_data_lookup (context, gtk_style_context_get_state (context));
1281 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1284 static GtkCssValue *
1285 gtk_style_context_query_func (guint id,
1288 return _gtk_css_computed_values_get_value (values, id);
1292 * gtk_style_context_get_property:
1293 * @context: a #GtkStyleContext
1294 * @property: style property name
1295 * @state: state to retrieve the property value for
1296 * @value: (out) (transfer full): return location for the style property value
1298 * Gets a style property from @context for the given state.
1300 * When @value is no longer needed, g_value_unset() must be called
1301 * to free any allocated memory.
1306 gtk_style_context_get_property (GtkStyleContext *context,
1307 const gchar *property,
1308 GtkStateFlags state,
1311 GtkStyleContextPrivate *priv;
1312 GtkStyleProperty *prop;
1316 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1317 g_return_if_fail (property != NULL);
1318 g_return_if_fail (value != NULL);
1320 priv = context->priv;
1321 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1323 prop = _gtk_style_property_lookup (property);
1326 g_warning ("Style property \"%s\" is not registered", property);
1329 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1331 g_warning ("Style property \"%s\" is not gettable", property);
1335 data = style_data_lookup (context, state);
1336 v = _gtk_style_property_query (prop, gtk_style_context_query_func, data->store);
1337 _gtk_css_value_init_gvalue (v, value);
1338 _gtk_css_value_unref (v);
1342 * gtk_style_context_get_valist:
1343 * @context: a #GtkStyleContext
1344 * @state: state to retrieve the property values for
1345 * @args: va_list of property name/return location pairs, followed by %NULL
1347 * Retrieves several style property values from @context for a given state.
1352 gtk_style_context_get_valist (GtkStyleContext *context,
1353 GtkStateFlags state,
1356 const gchar *property_name;
1358 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1360 property_name = va_arg (args, const gchar *);
1362 while (property_name)
1364 gchar *error = NULL;
1365 GValue value = G_VALUE_INIT;
1367 gtk_style_context_get_property (context,
1372 G_VALUE_LCOPY (&value, args, 0, &error);
1373 g_value_unset (&value);
1377 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1382 property_name = va_arg (args, const gchar *);
1387 * gtk_style_context_get:
1388 * @context: a #GtkStyleContext
1389 * @state: state to retrieve the property values for
1390 * @...: property name /return value pairs, followed by %NULL
1392 * Retrieves several style property values from @context for a
1398 gtk_style_context_get (GtkStyleContext *context,
1399 GtkStateFlags state,
1404 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1406 va_start (args, state);
1407 gtk_style_context_get_valist (context, state, args);
1412 * gtk_style_context_set_state:
1413 * @context: a #GtkStyleContext
1414 * @flags: state to represent
1416 * Sets the state to be used when rendering with any
1417 * of the gtk_render_*() functions.
1422 gtk_style_context_set_state (GtkStyleContext *context,
1423 GtkStateFlags flags)
1425 GtkStyleContextPrivate *priv;
1428 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1430 priv = context->priv;
1431 info = priv->info_stack->data;
1432 info->state_flags = flags;
1434 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_STATE);
1438 * gtk_style_context_get_state:
1439 * @context: a #GtkStyleContext
1441 * Returns the state used when rendering.
1443 * Returns: the state flags
1448 gtk_style_context_get_state (GtkStyleContext *context)
1450 GtkStyleContextPrivate *priv;
1453 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1455 priv = context->priv;
1456 info = priv->info_stack->data;
1458 return info->state_flags;
1462 context_has_animatable_region (GtkStyleContext *context,
1465 GtkStyleContextPrivate *priv;
1467 /* NULL region_id means everything
1468 * rendered through the style context
1473 priv = context->priv;
1474 return g_slist_find (priv->animation_regions, region_id) != NULL;
1478 * gtk_style_context_state_is_running:
1479 * @context: a #GtkStyleContext
1480 * @state: a widget state
1481 * @progress: (out): return location for the transition progress
1483 * Returns %TRUE if there is a transition animation running for the
1484 * current region (see gtk_style_context_push_animatable_region()).
1486 * If @progress is not %NULL, the animation progress will be returned
1487 * there, 0.0 means the state is closest to being unset, while 1.0 means
1488 * it's closest to being set. This means transition animation will
1489 * run from 0 to 1 when @state is being set and from 1 to 0 when
1492 * Returns: %TRUE if there is a running transition animation for @state.
1497 gtk_style_context_state_is_running (GtkStyleContext *context,
1501 GtkStyleContextPrivate *priv;
1502 AnimationInfo *info;
1505 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1507 priv = context->priv;
1509 for (l = priv->animations; l; l = l->next)
1513 if (info->state == state &&
1514 context_has_animatable_region (context, info->region_id))
1517 *progress = _gtk_timeline_get_progress (info->timeline);
1527 * gtk_style_context_set_path:
1528 * @context: a #GtkStyleContext
1529 * @path: a #GtkWidgetPath
1531 * Sets the #GtkWidgetPath used for style matching. As a
1532 * consequence, the style will be regenerated to match
1533 * the new given path.
1535 * If you are using a #GtkStyleContext returned from
1536 * gtk_widget_get_style_context(), you do not need to call
1542 gtk_style_context_set_path (GtkStyleContext *context,
1543 GtkWidgetPath *path)
1545 GtkStyleContextPrivate *priv;
1547 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1548 g_return_if_fail (path != NULL);
1550 priv = context->priv;
1551 g_return_if_fail (priv->widget == NULL);
1553 if (priv->widget_path)
1555 gtk_widget_path_free (priv->widget_path);
1556 priv->widget_path = NULL;
1560 priv->widget_path = gtk_widget_path_copy (path);
1562 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY);
1566 * gtk_style_context_get_path:
1567 * @context: a #GtkStyleContext
1569 * Returns the widget path used for style matching.
1571 * Returns: (transfer none): A #GtkWidgetPath
1575 const GtkWidgetPath *
1576 gtk_style_context_get_path (GtkStyleContext *context)
1578 GtkStyleContextPrivate *priv;
1580 priv = context->priv;
1582 return gtk_widget_get_path (priv->widget);
1584 return priv->widget_path;
1588 * gtk_style_context_set_parent:
1589 * @context: a #GtkStyleContext
1590 * @parent: (allow-none): the new parent or %NULL
1592 * Sets the parent style context for @context. The parent style
1593 * context is used to implement
1594 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1597 * If you are using a #GtkStyleContext returned from
1598 * gtk_widget_get_style_context(), the parent will be set for you.
1603 gtk_style_context_set_parent (GtkStyleContext *context,
1604 GtkStyleContext *parent)
1606 GtkStyleContextPrivate *priv;
1608 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1609 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1611 priv = context->priv;
1613 if (priv->parent == parent)
1618 parent->priv->children = g_slist_prepend (parent->priv->children, context);
1619 g_object_ref (parent);
1624 priv->parent->priv->children = g_slist_remove (priv->parent->priv->children, context);
1625 g_object_unref (priv->parent);
1628 priv->parent = parent;
1630 g_object_notify (G_OBJECT (context), "parent");
1631 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
1635 * gtk_style_context_get_parent:
1636 * @context: a #GtkStyleContext
1638 * Gets the parent context set via gtk_style_context_set_parent().
1639 * See that function for details.
1641 * Returns: (transfer none): the parent context or %NULL
1646 gtk_style_context_get_parent (GtkStyleContext *context)
1648 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1650 return context->priv->parent;
1654 * gtk_style_context_save:
1655 * @context: a #GtkStyleContext
1657 * Saves the @context state, so all modifications done through
1658 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1659 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1660 * or gtk_style_context_set_junction_sides() can be reverted in one
1661 * go through gtk_style_context_restore().
1666 gtk_style_context_save (GtkStyleContext *context)
1668 GtkStyleContextPrivate *priv;
1671 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1673 priv = context->priv;
1675 g_assert (priv->info_stack != NULL);
1677 info = style_info_copy (priv->info_stack->data);
1678 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1682 * gtk_style_context_restore:
1683 * @context: a #GtkStyleContext
1685 * Restores @context state to a previous stage.
1686 * See gtk_style_context_save().
1691 gtk_style_context_restore (GtkStyleContext *context)
1693 GtkStyleContextPrivate *priv;
1696 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1698 priv = context->priv;
1700 if (priv->info_stack)
1702 info = priv->info_stack->data;
1703 priv->info_stack = g_slist_remove (priv->info_stack, info);
1704 style_info_free (info);
1707 if (!priv->info_stack)
1709 g_warning ("Unpaired gtk_style_context_restore() call");
1711 /* Create default region */
1712 info = style_info_new ();
1713 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1716 priv->current_data = NULL;
1720 style_class_find (GArray *array,
1725 gboolean found = FALSE;
1731 if (!array || array->len == 0)
1735 max = array->len - 1;
1741 mid = (min + max) / 2;
1742 item = g_array_index (array, GQuark, mid);
1744 if (class_quark == item)
1749 else if (class_quark > item)
1750 min = pos = mid + 1;
1757 while (!found && min <= max);
1766 region_find (GArray *array,
1771 gboolean found = FALSE;
1777 if (!array || array->len == 0)
1781 max = array->len - 1;
1787 mid = (min + max) / 2;
1788 region = &g_array_index (array, GtkRegion, mid);
1790 if (region->class_quark == class_quark)
1795 else if (region->class_quark > class_quark)
1796 min = pos = mid + 1;
1803 while (!found && min <= max);
1812 * gtk_style_context_add_class:
1813 * @context: a #GtkStyleContext
1814 * @class_name: class name to use in styling
1816 * Adds a style class to @context, so posterior calls to
1817 * gtk_style_context_get() or any of the gtk_render_*()
1818 * functions will make use of this new class for styling.
1820 * In the CSS file format, a #GtkEntry defining an "entry"
1821 * class, would be matched by:
1824 * GtkEntry.entry { ... }
1827 * While any widget defining an "entry" class would be
1836 gtk_style_context_add_class (GtkStyleContext *context,
1837 const gchar *class_name)
1839 GtkStyleContextPrivate *priv;
1844 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1845 g_return_if_fail (class_name != NULL);
1847 priv = context->priv;
1848 class_quark = g_quark_from_string (class_name);
1850 g_assert (priv->info_stack != NULL);
1851 info = priv->info_stack->data;
1853 if (!style_class_find (info->style_classes, class_quark, &position))
1855 g_array_insert_val (info->style_classes, position, class_quark);
1857 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1862 * gtk_style_context_remove_class:
1863 * @context: a #GtkStyleContext
1864 * @class_name: class name to remove
1866 * Removes @class_name from @context.
1871 gtk_style_context_remove_class (GtkStyleContext *context,
1872 const gchar *class_name)
1874 GtkStyleContextPrivate *priv;
1879 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1880 g_return_if_fail (class_name != NULL);
1882 class_quark = g_quark_try_string (class_name);
1887 priv = context->priv;
1889 g_assert (priv->info_stack != NULL);
1890 info = priv->info_stack->data;
1892 if (style_class_find (info->style_classes, class_quark, &position))
1894 g_array_remove_index (info->style_classes, position);
1896 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1901 * gtk_style_context_has_class:
1902 * @context: a #GtkStyleContext
1903 * @class_name: a class name
1905 * Returns %TRUE if @context currently has defined the
1908 * Returns: %TRUE if @context has @class_name defined
1913 gtk_style_context_has_class (GtkStyleContext *context,
1914 const gchar *class_name)
1916 GtkStyleContextPrivate *priv;
1920 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1921 g_return_val_if_fail (class_name != NULL, FALSE);
1923 class_quark = g_quark_try_string (class_name);
1928 priv = context->priv;
1930 g_assert (priv->info_stack != NULL);
1931 info = priv->info_stack->data;
1933 if (style_class_find (info->style_classes, class_quark, NULL))
1940 * gtk_style_context_list_classes:
1941 * @context: a #GtkStyleContext
1943 * Returns the list of classes currently defined in @context.
1945 * Returns: (transfer container) (element-type utf8): a #GList of
1946 * strings with the currently defined classes. The contents
1947 * of the list are owned by GTK+, but you must free the list
1948 * itself with g_list_free() when you are done with it.
1953 gtk_style_context_list_classes (GtkStyleContext *context)
1955 GtkStyleContextPrivate *priv;
1957 GList *classes = NULL;
1960 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1962 priv = context->priv;
1964 g_assert (priv->info_stack != NULL);
1965 info = priv->info_stack->data;
1967 for (i = 0; i < info->style_classes->len; i++)
1971 quark = g_array_index (info->style_classes, GQuark, i);
1972 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1979 * gtk_style_context_list_regions:
1980 * @context: a #GtkStyleContext
1982 * Returns the list of regions currently defined in @context.
1984 * Returns: (transfer container) (element-type utf8): a #GList of
1985 * strings with the currently defined regions. The contents
1986 * of the list are owned by GTK+, but you must free the list
1987 * itself with g_list_free() when you are done with it.
1992 gtk_style_context_list_regions (GtkStyleContext *context)
1994 GtkStyleContextPrivate *priv;
1996 GList *classes = NULL;
1999 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2001 priv = context->priv;
2003 g_assert (priv->info_stack != NULL);
2004 info = priv->info_stack->data;
2006 for (i = 0; i < info->regions->len; i++)
2009 const gchar *class_name;
2011 region = &g_array_index (info->regions, GtkRegion, i);
2013 class_name = g_quark_to_string (region->class_quark);
2014 classes = g_list_prepend (classes, (gchar *) class_name);
2021 _gtk_style_context_check_region_name (const gchar *str)
2023 g_return_val_if_fail (str != NULL, FALSE);
2025 if (!g_ascii_islower (str[0]))
2031 !g_ascii_islower (*str))
2041 * gtk_style_context_add_region:
2042 * @context: a #GtkStyleContext
2043 * @region_name: region name to use in styling
2044 * @flags: flags that apply to the region
2046 * Adds a region to @context, so posterior calls to
2047 * gtk_style_context_get() or any of the gtk_render_*()
2048 * functions will make use of this new region for styling.
2050 * In the CSS file format, a #GtkTreeView defining a "row"
2051 * region, would be matched by:
2054 * GtkTreeView row { ... }
2057 * Pseudo-classes are used for matching @flags, so the two
2060 * GtkTreeView row:nth-child(even) { ... }
2061 * GtkTreeView row:nth-child(odd) { ... }
2064 * would apply to even and odd rows, respectively.
2066 * <note><para>Region names must only contain lowercase letters
2067 * and '-', starting always with a lowercase letter.</para></note>
2072 gtk_style_context_add_region (GtkStyleContext *context,
2073 const gchar *region_name,
2074 GtkRegionFlags flags)
2076 GtkStyleContextPrivate *priv;
2078 GQuark region_quark;
2081 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2082 g_return_if_fail (region_name != NULL);
2083 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2085 priv = context->priv;
2086 region_quark = g_quark_from_string (region_name);
2088 g_assert (priv->info_stack != NULL);
2089 info = priv->info_stack->data;
2091 if (!region_find (info->regions, region_quark, &position))
2095 region.class_quark = region_quark;
2096 region.flags = flags;
2098 g_array_insert_val (info->regions, position, region);
2100 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2105 * gtk_style_context_remove_region:
2106 * @context: a #GtkStyleContext
2107 * @region_name: region name to unset
2109 * Removes a region from @context.
2114 gtk_style_context_remove_region (GtkStyleContext *context,
2115 const gchar *region_name)
2117 GtkStyleContextPrivate *priv;
2119 GQuark region_quark;
2122 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2123 g_return_if_fail (region_name != NULL);
2125 region_quark = g_quark_try_string (region_name);
2130 priv = context->priv;
2132 g_assert (priv->info_stack != NULL);
2133 info = priv->info_stack->data;
2135 if (region_find (info->regions, region_quark, &position))
2137 g_array_remove_index (info->regions, position);
2139 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2144 * gtk_style_context_has_region:
2145 * @context: a #GtkStyleContext
2146 * @region_name: a region name
2147 * @flags_return: (out) (allow-none): return location for region flags
2149 * Returns %TRUE if @context has the region defined.
2150 * If @flags_return is not %NULL, it is set to the flags
2151 * affecting the region.
2153 * Returns: %TRUE if region is defined
2158 gtk_style_context_has_region (GtkStyleContext *context,
2159 const gchar *region_name,
2160 GtkRegionFlags *flags_return)
2162 GtkStyleContextPrivate *priv;
2164 GQuark region_quark;
2167 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2168 g_return_val_if_fail (region_name != NULL, FALSE);
2173 region_quark = g_quark_try_string (region_name);
2178 priv = context->priv;
2180 g_assert (priv->info_stack != NULL);
2181 info = priv->info_stack->data;
2183 if (region_find (info->regions, region_quark, &position))
2189 region = &g_array_index (info->regions, GtkRegion, position);
2190 *flags_return = region->flags;
2199 style_property_values_cmp (gconstpointer bsearch_node1,
2200 gconstpointer bsearch_node2)
2202 const PropertyValue *val1 = bsearch_node1;
2203 const PropertyValue *val2 = bsearch_node2;
2205 if (val1->widget_type != val2->widget_type)
2206 return val1->widget_type < val2->widget_type ? -1 : 1;
2208 if (val1->pspec != val2->pspec)
2209 return val1->pspec < val2->pspec ? -1 : 1;
2211 if (val1->state != val2->state)
2212 return val1->state < val2->state ? -1 : 1;
2218 _gtk_style_context_peek_property (GtkStyleContext *context,
2219 const char *property_name)
2221 StyleData *data = style_data_lookup (context, gtk_style_context_get_state (context));
2223 return _gtk_css_computed_values_get_value_by_name (data->store, property_name);
2227 _gtk_style_context_get_number (GtkStyleContext *context,
2228 const char *property_name,
2229 double one_hundred_percent)
2233 value = _gtk_style_context_peek_property (context, property_name);
2234 return _gtk_css_number_get (_gtk_css_value_get_number (value), one_hundred_percent);
2238 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2240 GtkStateFlags state,
2243 GtkStyleContextPrivate *priv;
2244 PropertyValue *pcache, key = { 0 };
2248 priv = context->priv;
2249 data = style_data_lookup (context, state);
2251 key.widget_type = widget_type;
2255 /* need value cache array */
2256 if (!data->property_cache)
2257 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2260 pcache = bsearch (&key,
2261 data->property_cache->data, data->property_cache->len,
2262 sizeof (PropertyValue), style_property_values_cmp);
2264 return &pcache->value;
2268 while (i < data->property_cache->len &&
2269 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2272 g_array_insert_val (data->property_cache, i, key);
2273 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2275 /* cache miss, initialize value type, then set contents */
2276 g_param_spec_ref (pcache->pspec);
2277 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2279 if (priv->widget || priv->widget_path)
2281 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2282 priv->widget ? gtk_widget_get_path (priv->widget)
2283 : priv->widget_path,
2284 state, pspec, &pcache->value))
2286 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2287 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2289 GtkSymbolicColor *color;
2292 color = g_value_dup_boxed (&pcache->value);
2294 g_value_unset (&pcache->value);
2296 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2297 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2299 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2301 if (_gtk_style_context_resolve_color (context, color, &rgba))
2303 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2304 g_value_set_boxed (&pcache->value, &rgba);
2309 rgb.red = rgba.red * 65535. + 0.5;
2310 rgb.green = rgba.green * 65535. + 0.5;
2311 rgb.blue = rgba.blue * 65535. + 0.5;
2313 g_value_set_boxed (&pcache->value, &rgb);
2317 g_param_value_set_default (pspec, &pcache->value);
2319 gtk_symbolic_color_unref (color);
2322 return &pcache->value;
2326 /* not supplied by any provider, revert to default */
2327 g_param_value_set_default (pspec, &pcache->value);
2329 return &pcache->value;
2333 * gtk_style_context_get_style_property:
2334 * @context: a #GtkStyleContext
2335 * @property_name: the name of the widget style property
2336 * @value: Return location for the property value
2338 * Gets the value for a widget style property.
2340 * When @value is no longer needed, g_value_unset() must be called
2341 * to free any allocated memory.
2344 gtk_style_context_get_style_property (GtkStyleContext *context,
2345 const gchar *property_name,
2348 GtkStyleContextPrivate *priv;
2349 GtkWidgetClass *widget_class;
2350 GtkStateFlags state;
2352 const GValue *peek_value;
2355 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2356 g_return_if_fail (property_name != NULL);
2357 g_return_if_fail (value != NULL);
2359 priv = context->priv;
2363 widget_type = G_OBJECT_TYPE (priv->widget);
2367 if (!priv->widget_path)
2370 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2372 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2374 g_warning ("%s: can't get style properties for non-widget class `%s'",
2376 g_type_name (widget_type));
2381 widget_class = g_type_class_ref (widget_type);
2382 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2383 g_type_class_unref (widget_class);
2387 g_warning ("%s: widget class `%s' has no style property named `%s'",
2389 g_type_name (widget_type),
2394 state = gtk_style_context_get_state (context);
2395 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2398 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2399 g_value_copy (peek_value, value);
2400 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2401 g_value_transform (peek_value, value);
2403 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2405 G_VALUE_TYPE_NAME (peek_value),
2406 G_VALUE_TYPE_NAME (value));
2410 * gtk_style_context_get_style_valist:
2411 * @context: a #GtkStyleContext
2412 * @args: va_list of property name/return location pairs, followed by %NULL
2414 * Retrieves several widget style properties from @context according to the
2420 gtk_style_context_get_style_valist (GtkStyleContext *context,
2423 GtkStyleContextPrivate *priv;
2424 const gchar *prop_name;
2425 GtkStateFlags state;
2428 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2430 prop_name = va_arg (args, const gchar *);
2431 priv = context->priv;
2435 widget_type = G_OBJECT_TYPE (priv->widget);
2439 if (!priv->widget_path)
2442 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2444 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2446 g_warning ("%s: can't get style properties for non-widget class `%s'",
2448 g_type_name (widget_type));
2453 state = gtk_style_context_get_state (context);
2457 GtkWidgetClass *widget_class;
2459 const GValue *peek_value;
2462 widget_class = g_type_class_ref (widget_type);
2463 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2464 g_type_class_unref (widget_class);
2468 g_warning ("%s: widget class `%s' has no style property named `%s'",
2470 g_type_name (widget_type),
2475 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2478 G_VALUE_LCOPY (peek_value, args, 0, &error);
2482 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2484 G_VALUE_TYPE_NAME (peek_value),
2489 prop_name = va_arg (args, const gchar *);
2494 * gtk_style_context_get_style:
2495 * @context: a #GtkStyleContext
2496 * @...: property name /return value pairs, followed by %NULL
2498 * Retrieves several widget style properties from @context according to the
2504 gtk_style_context_get_style (GtkStyleContext *context,
2509 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2511 va_start (args, context);
2512 gtk_style_context_get_style_valist (context, args);
2518 * gtk_style_context_lookup_icon_set:
2519 * @context: a #GtkStyleContext
2520 * @stock_id: an icon name
2522 * Looks up @stock_id in the icon factories associated to @context and
2523 * the default icon factory, returning an icon set if found, otherwise
2526 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2529 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2530 const gchar *stock_id)
2532 GtkStyleContextPrivate *priv;
2534 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2535 g_return_val_if_fail (stock_id != NULL, NULL);
2537 priv = context->priv;
2538 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2540 return gtk_icon_factory_lookup_default (stock_id);
2544 * gtk_style_context_set_screen:
2545 * @context: a #GtkStyleContext
2546 * @screen: a #GdkScreen
2548 * Attaches @context to the given screen.
2550 * The screen is used to add style information from 'global' style
2551 * providers, such as the screens #GtkSettings instance.
2553 * If you are using a #GtkStyleContext returned from
2554 * gtk_widget_get_style_context(), you do not need to
2555 * call this yourself.
2560 gtk_style_context_set_screen (GtkStyleContext *context,
2563 GtkStyleContextPrivate *priv;
2565 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2566 g_return_if_fail (GDK_IS_SCREEN (screen));
2568 priv = context->priv;
2569 if (priv->screen == screen)
2572 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2574 g_object_unref (priv->cascade);
2575 priv->cascade = _gtk_style_cascade_get_for_screen (screen);
2576 g_object_ref (priv->cascade);
2580 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2583 priv->screen = screen;
2585 g_object_notify (G_OBJECT (context), "screen");
2587 gtk_style_context_invalidate (context);
2591 * gtk_style_context_get_screen:
2592 * @context: a #GtkStyleContext
2594 * Returns the #GdkScreen to which @context is attached.
2596 * Returns: (transfer none): a #GdkScreen.
2599 gtk_style_context_get_screen (GtkStyleContext *context)
2601 GtkStyleContextPrivate *priv;
2603 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2605 priv = context->priv;
2606 return priv->screen;
2610 * gtk_style_context_set_direction:
2611 * @context: a #GtkStyleContext
2612 * @direction: the new direction.
2614 * Sets the reading direction for rendering purposes.
2616 * If you are using a #GtkStyleContext returned from
2617 * gtk_widget_get_style_context(), you do not need to
2618 * call this yourself.
2623 gtk_style_context_set_direction (GtkStyleContext *context,
2624 GtkTextDirection direction)
2626 GtkStyleContextPrivate *priv;
2628 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2630 priv = context->priv;
2631 priv->direction = direction;
2633 g_object_notify (G_OBJECT (context), "direction");
2637 * gtk_style_context_get_direction:
2638 * @context: a #GtkStyleContext
2640 * Returns the widget direction used for rendering.
2642 * Returns: the widget direction
2647 gtk_style_context_get_direction (GtkStyleContext *context)
2649 GtkStyleContextPrivate *priv;
2651 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2653 priv = context->priv;
2654 return priv->direction;
2658 * gtk_style_context_set_junction_sides:
2659 * @context: a #GtkStyleContext
2660 * @sides: sides where rendered elements are visually connected to
2663 * Sets the sides where rendered elements (mostly through
2664 * gtk_render_frame()) will visually connect with other visual elements.
2666 * This is merely a hint that may or may not be honored
2667 * by theming engines.
2669 * Container widgets are expected to set junction hints as appropriate
2670 * for their children, so it should not normally be necessary to call
2671 * this function manually.
2676 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2677 GtkJunctionSides sides)
2679 GtkStyleContextPrivate *priv;
2682 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2684 priv = context->priv;
2685 info = priv->info_stack->data;
2686 info->junction_sides = sides;
2690 * gtk_style_context_get_junction_sides:
2691 * @context: a #GtkStyleContext
2693 * Returns the sides where rendered elements connect visually with others.
2695 * Returns: the junction sides
2700 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2702 GtkStyleContextPrivate *priv;
2705 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2707 priv = context->priv;
2708 info = priv->info_stack->data;
2709 return info->junction_sides;
2712 static GtkSymbolicColor *
2713 gtk_style_context_color_lookup_func (gpointer contextp,
2716 GtkStyleContext *context = contextp;
2718 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2722 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2723 GtkSymbolicColor *color)
2725 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2726 g_return_val_if_fail (color != NULL, FALSE);
2728 return _gtk_symbolic_color_resolve_full (color,
2729 gtk_style_context_color_lookup_func,
2735 _gtk_style_context_resolve_color (GtkStyleContext *context,
2736 GtkSymbolicColor *color,
2741 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2742 g_return_val_if_fail (color != NULL, FALSE);
2743 g_return_val_if_fail (result != NULL, FALSE);
2745 val = _gtk_symbolic_color_resolve_full (color,
2746 gtk_style_context_color_lookup_func,
2751 *result = *_gtk_css_value_get_rgba (val);
2752 _gtk_css_value_unref (val);
2757 * gtk_style_context_lookup_color:
2758 * @context: a #GtkStyleContext
2759 * @color_name: color name to lookup
2760 * @color: (out): Return location for the looked up color
2762 * Looks up and resolves a color name in the @context color map.
2764 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2767 gtk_style_context_lookup_color (GtkStyleContext *context,
2768 const gchar *color_name,
2771 GtkSymbolicColor *sym_color;
2773 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2774 g_return_val_if_fail (color_name != NULL, FALSE);
2775 g_return_val_if_fail (color != NULL, FALSE);
2777 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2778 if (sym_color == NULL)
2781 return _gtk_style_context_resolve_color (context, sym_color, color);
2785 * gtk_style_context_notify_state_change:
2786 * @context: a #GtkStyleContext
2787 * @window: a #GdkWindow
2788 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2789 * See gtk_style_context_push_animatable_region()
2790 * @state: state to trigger transition for
2791 * @state_value: %TRUE if @state is the state we are changing to,
2792 * %FALSE if we are changing away from it
2794 * Notifies a state change on @context, so if the current style makes use
2795 * of transition animations, one will be started so all rendered elements
2796 * under @region_id are animated for state @state being set to value
2799 * The @window parameter is used in order to invalidate the rendered area
2800 * as the animation runs, so make sure it is the same window that is being
2801 * rendered on by the gtk_render_*() functions.
2803 * If @region_id is %NULL, all rendered elements using @context will be
2804 * affected by this state transition.
2806 * As a practical example, a #GtkButton notifying a state transition on
2807 * the prelight state:
2809 * gtk_style_context_notify_state_change (context,
2810 * gtk_widget_get_window (widget),
2812 * GTK_STATE_PRELIGHT,
2813 * button->in_button);
2816 * Can be handled in the CSS file like this:
2819 * background-color: #f00
2823 * background-color: #fff;
2824 * transition: 200ms linear
2828 * This combination will animate the button background from red to white
2829 * if a pointer enters the button, and back to red if the pointer leaves
2832 * Note that @state is used when finding the transition parameters, which
2833 * is why the style places the transition under the :hover pseudo-class.
2838 gtk_style_context_notify_state_change (GtkStyleContext *context,
2842 gboolean state_value)
2844 GtkStyleContextPrivate *priv;
2845 GtkAnimationDescription *desc;
2846 AnimationInfo *info;
2847 GtkStateFlags flags;
2851 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2852 g_return_if_fail (GDK_IS_WINDOW (window));
2853 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2855 priv = context->priv;
2856 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
2858 state_value = (state_value == TRUE);
2862 case GTK_STATE_ACTIVE:
2863 flags = GTK_STATE_FLAG_ACTIVE;
2865 case GTK_STATE_PRELIGHT:
2866 flags = GTK_STATE_FLAG_PRELIGHT;
2868 case GTK_STATE_SELECTED:
2869 flags = GTK_STATE_FLAG_SELECTED;
2871 case GTK_STATE_INSENSITIVE:
2872 flags = GTK_STATE_FLAG_INSENSITIVE;
2874 case GTK_STATE_INCONSISTENT:
2875 flags = GTK_STATE_FLAG_INCONSISTENT;
2877 case GTK_STATE_FOCUSED:
2878 flags = GTK_STATE_FLAG_FOCUSED;
2880 case GTK_STATE_NORMAL:
2886 /* Find out if there is any animation description for the given
2887 * state, it will fallback to the normal state as well if necessary.
2889 data = style_data_lookup (context, flags);
2890 v = _gtk_css_computed_values_get_value_by_name (data->store, "transition");
2893 desc = _gtk_css_value_get_boxed (v);
2897 if (_gtk_animation_description_get_duration (desc) == 0)
2900 info = animation_info_lookup (context, region_id, state);
2903 info->target_value != state_value)
2905 /* Target values are the opposite */
2906 if (!_gtk_timeline_get_loop (info->timeline))
2908 /* Reverse the animation */
2909 if (_gtk_timeline_get_direction (info->timeline) == GTK_TIMELINE_DIRECTION_FORWARD)
2910 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
2912 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_FORWARD);
2914 info->target_value = state_value;
2918 /* Take it out of its looping state */
2919 _gtk_timeline_set_loop (info->timeline, FALSE);
2923 (!_gtk_animation_description_get_loop (desc) ||
2926 info = animation_info_new (context, region_id,
2927 _gtk_animation_description_get_duration (desc),
2928 _gtk_animation_description_get_progress_type (desc),
2929 _gtk_animation_description_get_loop (desc),
2930 state, state_value, window);
2932 priv->animations = g_slist_prepend (priv->animations, info);
2933 priv->animations_invalidated = TRUE;
2938 * gtk_style_context_cancel_animations:
2939 * @context: a #GtkStyleContext
2940 * @region_id: (allow-none): animatable region to stop, or %NULL.
2941 * See gtk_style_context_push_animatable_region()
2943 * Stops all running animations for @region_id and all animatable
2944 * regions underneath.
2946 * A %NULL @region_id will stop all ongoing animations in @context,
2947 * when dealing with a #GtkStyleContext obtained through
2948 * gtk_widget_get_style_context(), this is normally done for you
2949 * in all circumstances you would expect all widget to be stopped,
2950 * so this should be only used in complex widgets with different
2951 * animatable regions.
2956 gtk_style_context_cancel_animations (GtkStyleContext *context,
2959 GtkStyleContextPrivate *priv;
2960 AnimationInfo *info;
2963 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2965 priv = context->priv;
2966 l = priv->animations;
2974 info->region_id == region_id ||
2975 g_slist_find (info->parent_regions, region_id))
2977 priv->animations = g_slist_remove (priv->animations, info);
2978 animation_info_free (info);
2984 is_parent_of (GdkWindow *parent,
2987 GtkWidget *child_widget, *parent_widget;
2990 gdk_window_get_user_data (child, (gpointer *) &child_widget);
2991 gdk_window_get_user_data (parent, (gpointer *) &parent_widget);
2993 if (child_widget != parent_widget &&
2994 !gtk_widget_is_ancestor (child_widget, parent_widget))
3001 if (window == parent)
3004 window = gdk_window_get_parent (window);
3011 * gtk_style_context_scroll_animations:
3012 * @context: a #GtkStyleContext
3013 * @window: a #GdkWindow used previously in
3014 * gtk_style_context_notify_state_change()
3015 * @dx: Amount to scroll in the X axis
3016 * @dy: Amount to scroll in the Y axis
3018 * This function is analogous to gdk_window_scroll(), and
3019 * should be called together with it so the invalidation
3020 * areas for any ongoing animation are scrolled together
3026 gtk_style_context_scroll_animations (GtkStyleContext *context,
3031 GtkStyleContextPrivate *priv;
3032 AnimationInfo *info;
3035 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3036 g_return_if_fail (GDK_IS_WINDOW (window));
3038 priv = context->priv;
3039 l = priv->animations;
3046 if (info->invalidation_region &&
3047 (window == info->window ||
3048 is_parent_of (window, info->window)))
3049 cairo_region_translate (info->invalidation_region, dx, dy);
3054 * gtk_style_context_push_animatable_region:
3055 * @context: a #GtkStyleContext
3056 * @region_id: unique identifier for the animatable region
3058 * Pushes an animatable region, so all further gtk_render_*() calls between
3059 * this call and the following gtk_style_context_pop_animatable_region()
3060 * will potentially show transition animations for this region if
3061 * gtk_style_context_notify_state_change() is called for a given state,
3062 * and the current theme/style defines transition animations for state
3065 * The @region_id used must be unique in @context so the theming engine
3066 * can uniquely identify rendered elements subject to a state transition.
3071 gtk_style_context_push_animatable_region (GtkStyleContext *context,
3074 GtkStyleContextPrivate *priv;
3076 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3077 g_return_if_fail (region_id != NULL);
3079 priv = context->priv;
3080 priv->animation_regions = g_slist_prepend (priv->animation_regions, region_id);
3084 * gtk_style_context_pop_animatable_region:
3085 * @context: a #GtkStyleContext
3087 * Pops an animatable region from @context.
3088 * See gtk_style_context_push_animatable_region().
3093 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
3095 GtkStyleContextPrivate *priv;
3097 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3099 priv = context->priv;
3100 priv->animation_regions = g_slist_delete_link (priv->animation_regions,
3101 priv->animation_regions);
3105 _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context)
3107 GtkStyleContextPrivate *priv;
3110 priv = context->priv;
3112 for (l = priv->animations; l; l = l->next)
3114 AnimationInfo *info;
3118 /* A NULL invalidation region means it has to be recreated on
3119 * the next expose event, this happens usually after a widget
3120 * allocation change, so the next expose after it will update
3121 * the invalidation region.
3123 if (info->invalidation_region)
3125 cairo_region_destroy (info->invalidation_region);
3126 info->invalidation_region = NULL;
3130 priv->animations_invalidated = TRUE;
3134 _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context,
3137 GtkStyleContextPrivate *priv;
3140 priv = context->priv;
3142 if (!priv->animations_invalidated)
3145 l = priv->animations;
3149 AnimationInfo *info;
3158 if (info->invalidation_region)
3161 if (info->rectangles->len == 0)
3164 info->invalidation_region = cairo_region_create ();
3165 _gtk_widget_get_translation_to_window (widget, info->window, &rel_x, &rel_y);
3167 for (i = 0; i < info->rectangles->len; i++)
3169 cairo_rectangle_int_t *rect;
3171 rect = &g_array_index (info->rectangles, cairo_rectangle_int_t, i);
3173 /* These are widget relative coordinates,
3174 * so have them inverted to be window relative
3179 cairo_region_union_rectangle (info->invalidation_region, rect);
3182 g_array_remove_range (info->rectangles, 0, info->rectangles->len);
3185 priv->animations_invalidated = FALSE;
3189 store_animation_region (GtkStyleContext *context,
3195 GtkStyleContextPrivate *priv;
3198 priv = context->priv;
3200 if (!priv->animations_invalidated)
3203 for (l = priv->animations; l; l = l->next)
3205 AnimationInfo *info;
3209 /* The animation doesn't need updating
3210 * the invalidation area, bail out.
3212 if (info->invalidation_region)
3215 if (context_has_animatable_region (context, info->region_id))
3217 cairo_rectangle_int_t rect;
3221 rect.width = (gint) width;
3222 rect.height = (gint) height;
3224 g_array_append_val (info->rectangles, rect);
3226 if (!info->parent_regions)
3228 GSList *parent_regions;
3230 parent_regions = g_slist_find (priv->animation_regions, info->region_id);
3231 info->parent_regions = g_slist_copy (parent_regions);
3238 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3239 GtkCssChange change)
3241 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3242 g_return_if_fail (change != 0);
3244 gtk_style_context_invalidate (context);
3248 * gtk_style_context_invalidate:
3249 * @context: a #GtkStyleContext.
3251 * Invalidates @context style information, so it will be reconstructed
3254 * If you're using a #GtkStyleContext returned from
3255 * gtk_widget_get_style_context(), you do not need to
3256 * call this yourself.
3261 gtk_style_context_invalidate (GtkStyleContext *context)
3263 GtkStyleContextPrivate *priv;
3265 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3267 priv = context->priv;
3269 /* Avoid reentrancy */
3270 if (priv->invalidating_context)
3273 priv->invalidating_context = TRUE;
3275 g_hash_table_remove_all (priv->style_data);
3276 priv->current_data = NULL;
3278 g_signal_emit (context, signals[CHANGED], 0);
3280 priv->invalidating_context = FALSE;
3284 * gtk_style_context_set_background:
3285 * @context: a #GtkStyleContext
3286 * @window: a #GdkWindow
3288 * Sets the background of @window to the background pattern or
3289 * color specified in @context for its current state.
3294 gtk_style_context_set_background (GtkStyleContext *context,
3297 GtkStateFlags state;
3298 cairo_pattern_t *pattern;
3301 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3302 g_return_if_fail (GDK_IS_WINDOW (window));
3304 state = gtk_style_context_get_state (context);
3305 gtk_style_context_get (context, state,
3306 "background-image", &pattern,
3310 gdk_window_set_background_pattern (window, pattern);
3311 cairo_pattern_destroy (pattern);
3315 gtk_style_context_get (context, state,
3316 "background-color", &color,
3320 gdk_window_set_background_rgba (window, color);
3321 gdk_rgba_free (color);
3326 * gtk_style_context_get_color:
3327 * @context: a #GtkStyleContext
3328 * @state: state to retrieve the color for
3329 * @color: (out): return value for the foreground color
3331 * Gets the foreground color for a given state.
3336 gtk_style_context_get_color (GtkStyleContext *context,
3337 GtkStateFlags state,
3342 g_return_if_fail (color != NULL);
3343 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3345 gtk_style_context_get (context,
3355 * gtk_style_context_get_background_color:
3356 * @context: a #GtkStyleContext
3357 * @state: state to retrieve the color for
3358 * @color: (out): return value for the background color
3360 * Gets the background color for a given state.
3365 gtk_style_context_get_background_color (GtkStyleContext *context,
3366 GtkStateFlags state,
3371 g_return_if_fail (color != NULL);
3372 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3374 gtk_style_context_get (context,
3376 "background-color", &c,
3384 * gtk_style_context_get_border_color:
3385 * @context: a #GtkStyleContext
3386 * @state: state to retrieve the color for
3387 * @color: (out): return value for the border color
3389 * Gets the border color for a given state.
3394 gtk_style_context_get_border_color (GtkStyleContext *context,
3395 GtkStateFlags state,
3400 g_return_if_fail (color != NULL);
3401 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3403 gtk_style_context_get (context,
3413 * gtk_style_context_get_border:
3414 * @context: a #GtkStyleContext
3415 * @state: state to retrieve the border for
3416 * @border: (out): return value for the border settings
3418 * Gets the border for a given state as a #GtkBorder.
3419 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3424 gtk_style_context_get_border (GtkStyleContext *context,
3425 GtkStateFlags state,
3428 int top, left, bottom, right;
3430 g_return_if_fail (border != NULL);
3431 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3433 gtk_style_context_get (context,
3435 "border-top-width", &top,
3436 "border-left-width", &left,
3437 "border-bottom-width", &bottom,
3438 "border-right-width", &right,
3442 border->left = left;
3443 border->bottom = bottom;
3444 border->right = right;
3448 * gtk_style_context_get_padding:
3449 * @context: a #GtkStyleContext
3450 * @state: state to retrieve the padding for
3451 * @padding: (out): return value for the padding settings
3453 * Gets the padding for a given state as a #GtkBorder.
3454 * See %GTK_STYLE_PROPERTY_PADDING.
3459 gtk_style_context_get_padding (GtkStyleContext *context,
3460 GtkStateFlags state,
3463 int top, left, bottom, right;
3465 g_return_if_fail (padding != NULL);
3466 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3468 gtk_style_context_get (context,
3470 "padding-top", &top,
3471 "padding-left", &left,
3472 "padding-bottom", &bottom,
3473 "padding-right", &right,
3477 padding->left = left;
3478 padding->bottom = bottom;
3479 padding->right = right;
3483 * gtk_style_context_get_margin:
3484 * @context: a #GtkStyleContext
3485 * @state: state to retrieve the border for
3486 * @margin: (out): return value for the margin settings
3488 * Gets the margin for a given state as a #GtkBorder.
3489 * See %GTK_STYLE_PROPERTY_MARGIN.
3494 gtk_style_context_get_margin (GtkStyleContext *context,
3495 GtkStateFlags state,
3498 int top, left, bottom, right;
3500 g_return_if_fail (margin != NULL);
3501 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3503 gtk_style_context_get (context,
3506 "margin-left", &left,
3507 "margin-bottom", &bottom,
3508 "margin-right", &right,
3512 margin->left = left;
3513 margin->bottom = bottom;
3514 margin->right = right;
3518 * gtk_style_context_get_font:
3519 * @context: a #GtkStyleContext
3520 * @state: state to retrieve the font for
3522 * Returns the font description for a given state. The returned
3523 * object is const and will remain valid until the
3524 * #GtkStyleContext::changed signal happens.
3526 * Returns: (transfer none): the #PangoFontDescription for the given
3527 * state. This object is owned by GTK+ and should not be
3532 const PangoFontDescription *
3533 gtk_style_context_get_font (GtkStyleContext *context,
3534 GtkStateFlags state)
3536 GtkStyleContextPrivate *priv;
3538 PangoFontDescription *description;
3540 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3542 priv = context->priv;
3543 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3545 data = style_data_lookup (context, state);
3547 /* Yuck, fonts are created on-demand but we don't return a ref.
3548 * Do bad things to achieve this requirement */
3549 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3550 if (description == NULL)
3552 gtk_style_context_get (context, state, "font", &description, NULL);
3553 g_object_set_data_full (G_OBJECT (data->store),
3554 "font-cache-for-get_font",
3556 (GDestroyNotify) pango_font_description_free);
3562 get_cursor_color (GtkStyleContext *context,
3566 GdkColor *style_color;
3568 gtk_style_context_get_style (context,
3569 primary ? "cursor-color" : "secondary-cursor-color",
3575 color->red = style_color->red / 65535.0;
3576 color->green = style_color->green / 65535.0;
3577 color->blue = style_color->blue / 65535.0;
3580 gdk_color_free (style_color);
3584 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3590 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3592 color->red = (color->red + bg.red) * 0.5;
3593 color->green = (color->green + bg.green) * 0.5;
3594 color->blue = (color->blue + bg.blue) * 0.5;
3600 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3601 GdkRGBA *primary_color,
3602 GdkRGBA *secondary_color)
3605 get_cursor_color (context, TRUE, primary_color);
3607 if (secondary_color)
3608 get_cursor_color (context, FALSE, secondary_color);
3615 * @context: a #GtkStyleContext
3617 * @x: X origin of the rectangle
3618 * @y: Y origin of the rectangle
3619 * @width: rectangle width
3620 * @height: rectangle height
3622 * Renders a checkmark (as in a #GtkCheckButton).
3624 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3625 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3626 * should be marked as undefined.
3629 * <title>Typical checkmark rendering</title>
3630 * <inlinegraphic fileref="checks.png" format="PNG"/>
3636 gtk_render_check (GtkStyleContext *context,
3643 GtkStyleContextPrivate *priv;
3644 GtkThemingEngineClass *engine_class;
3646 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3647 g_return_if_fail (cr != NULL);
3649 if (width <= 0 || height <= 0)
3652 priv = context->priv;
3653 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3657 store_animation_region (context, x, y, width, height);
3659 _gtk_theming_engine_set_context (priv->theming_engine, context);
3660 engine_class->render_check (priv->theming_engine, cr,
3661 x, y, width, height);
3667 * gtk_render_option:
3668 * @context: a #GtkStyleContext
3670 * @x: X origin of the rectangle
3671 * @y: Y origin of the rectangle
3672 * @width: rectangle width
3673 * @height: rectangle height
3675 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3676 * state will determine whether the option is on or off, and
3677 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3680 * <title>Typical option mark rendering</title>
3681 * <inlinegraphic fileref="options.png" format="PNG"/>
3687 gtk_render_option (GtkStyleContext *context,
3694 GtkStyleContextPrivate *priv;
3695 GtkThemingEngineClass *engine_class;
3697 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3698 g_return_if_fail (cr != NULL);
3700 if (width <= 0 || height <= 0)
3703 priv = context->priv;
3704 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3708 store_animation_region (context, x, y, width, height);
3710 _gtk_theming_engine_set_context (priv->theming_engine, context);
3711 engine_class->render_option (priv->theming_engine, cr,
3712 x, y, width, height);
3719 * @context: a #GtkStyleContext
3721 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3722 * @x: X origin of the render area
3723 * @y: Y origin of the render area
3724 * @size: square side for render area
3726 * Renders an arrow pointing to @angle.
3729 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3730 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3736 gtk_render_arrow (GtkStyleContext *context,
3743 GtkStyleContextPrivate *priv;
3744 GtkThemingEngineClass *engine_class;
3746 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3747 g_return_if_fail (cr != NULL);
3752 priv = context->priv;
3753 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3757 gtk_style_context_save (context);
3758 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3760 store_animation_region (context, x, y, size, size);
3762 _gtk_theming_engine_set_context (priv->theming_engine, context);
3763 engine_class->render_arrow (priv->theming_engine, cr,
3766 gtk_style_context_restore (context);
3771 * gtk_render_background:
3772 * @context: a #GtkStyleContext
3774 * @x: X origin of the rectangle
3775 * @y: Y origin of the rectangle
3776 * @width: rectangle width
3777 * @height: rectangle height
3779 * Renders the background of an element.
3782 * <title>Typical background rendering, showing the effect of
3783 * <parameter>background-image</parameter>,
3784 * <parameter>border-width</parameter> and
3785 * <parameter>border-radius</parameter></title>
3786 * <inlinegraphic fileref="background.png" format="PNG"/>
3792 gtk_render_background (GtkStyleContext *context,
3799 GtkStyleContextPrivate *priv;
3800 GtkThemingEngineClass *engine_class;
3802 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3803 g_return_if_fail (cr != NULL);
3805 if (width <= 0 || height <= 0)
3808 priv = context->priv;
3809 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3813 store_animation_region (context, x, y, width, height);
3815 _gtk_theming_engine_set_context (priv->theming_engine, context);
3816 engine_class->render_background (priv->theming_engine, cr, x, y, width, height);
3823 * @context: a #GtkStyleContext
3825 * @x: X origin of the rectangle
3826 * @y: Y origin of the rectangle
3827 * @width: rectangle width
3828 * @height: rectangle height
3830 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3833 * <title>Examples of frame rendering, showing the effect of
3834 * <parameter>border-image</parameter>,
3835 * <parameter>border-color</parameter>,
3836 * <parameter>border-width</parameter>,
3837 * <parameter>border-radius</parameter> and
3839 * <inlinegraphic fileref="frames.png" format="PNG"/>
3845 gtk_render_frame (GtkStyleContext *context,
3852 GtkStyleContextPrivate *priv;
3853 GtkThemingEngineClass *engine_class;
3855 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3856 g_return_if_fail (cr != NULL);
3858 if (width <= 0 || height <= 0)
3861 priv = context->priv;
3862 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3866 store_animation_region (context, x, y, width, height);
3868 _gtk_theming_engine_set_context (priv->theming_engine, context);
3869 engine_class->render_frame (priv->theming_engine, cr, x, y, width, height);
3875 * gtk_render_expander:
3876 * @context: a #GtkStyleContext
3878 * @x: X origin of the rectangle
3879 * @y: Y origin of the rectangle
3880 * @width: rectangle width
3881 * @height: rectangle height
3883 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3884 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3885 * determines whether the expander is collapsed or expanded.
3888 * <title>Typical expander rendering</title>
3889 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3895 gtk_render_expander (GtkStyleContext *context,
3902 GtkStyleContextPrivate *priv;
3903 GtkThemingEngineClass *engine_class;
3905 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3906 g_return_if_fail (cr != NULL);
3908 if (width <= 0 || height <= 0)
3911 priv = context->priv;
3912 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3916 store_animation_region (context, x, y, width, height);
3918 _gtk_theming_engine_set_context (priv->theming_engine, context);
3919 engine_class->render_expander (priv->theming_engine, cr, x, y, width, height);
3926 * @context: a #GtkStyleContext
3928 * @x: X origin of the rectangle
3929 * @y: Y origin of the rectangle
3930 * @width: rectangle width
3931 * @height: rectangle height
3933 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3935 * <title>Typical focus rendering</title>
3936 * <inlinegraphic fileref="focus.png" format="PNG"/>
3942 gtk_render_focus (GtkStyleContext *context,
3949 GtkStyleContextPrivate *priv;
3950 GtkThemingEngineClass *engine_class;
3952 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3953 g_return_if_fail (cr != NULL);
3955 if (width <= 0 || height <= 0)
3958 priv = context->priv;
3959 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3963 store_animation_region (context, x, y, width, height);
3965 _gtk_theming_engine_set_context (priv->theming_engine, context);
3966 engine_class->render_focus (priv->theming_engine, cr, x, y, width, height);
3972 * gtk_render_layout:
3973 * @context: a #GtkStyleContext
3977 * @layout: the #PangoLayout to render
3979 * Renders @layout on the coordinates @x, @y
3984 gtk_render_layout (GtkStyleContext *context,
3988 PangoLayout *layout)
3990 GtkStyleContextPrivate *priv;
3991 GtkThemingEngineClass *engine_class;
3992 PangoRectangle extents;
3994 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3995 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3996 g_return_if_fail (cr != NULL);
3998 priv = context->priv;
3999 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4003 pango_layout_get_extents (layout, &extents, NULL);
4005 store_animation_region (context,
4011 _gtk_theming_engine_set_context (priv->theming_engine, context);
4012 engine_class->render_layout (priv->theming_engine, cr, x, y, layout);
4019 * @context: a #GtkStyleContext
4021 * @x0: X coordinate for the origin of the line
4022 * @y0: Y coordinate for the origin of the line
4023 * @x1: X coordinate for the end of the line
4024 * @y1: Y coordinate for the end of the line
4026 * Renders a line from (x0, y0) to (x1, y1).
4031 gtk_render_line (GtkStyleContext *context,
4038 GtkStyleContextPrivate *priv;
4039 GtkThemingEngineClass *engine_class;
4041 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4042 g_return_if_fail (cr != NULL);
4044 priv = context->priv;
4045 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4049 _gtk_theming_engine_set_context (priv->theming_engine, context);
4050 engine_class->render_line (priv->theming_engine, cr, x0, y0, x1, y1);
4056 * gtk_render_slider:
4057 * @context: a #GtkStyleContext
4059 * @x: X origin of the rectangle
4060 * @y: Y origin of the rectangle
4061 * @width: rectangle width
4062 * @height: rectangle height
4063 * @orientation: orientation of the slider
4065 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4066 * @width, @height. @orientation defines whether the slider is vertical
4070 * <title>Typical slider rendering</title>
4071 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4077 gtk_render_slider (GtkStyleContext *context,
4083 GtkOrientation orientation)
4085 GtkStyleContextPrivate *priv;
4086 GtkThemingEngineClass *engine_class;
4088 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4089 g_return_if_fail (cr != NULL);
4091 if (width <= 0 || height <= 0)
4094 priv = context->priv;
4095 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4099 store_animation_region (context, x, y, width, height);
4101 _gtk_theming_engine_set_context (priv->theming_engine, context);
4102 engine_class->render_slider (priv->theming_engine, cr, x, y, width, height, orientation);
4108 * gtk_render_frame_gap:
4109 * @context: a #GtkStyleContext
4111 * @x: X origin of the rectangle
4112 * @y: Y origin of the rectangle
4113 * @width: rectangle width
4114 * @height: rectangle height
4115 * @gap_side: side where the gap is
4116 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4117 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4119 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4120 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4121 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4122 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4125 * <title>Typical rendering of a frame with a gap</title>
4126 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4132 gtk_render_frame_gap (GtkStyleContext *context,
4138 GtkPositionType gap_side,
4142 GtkStyleContextPrivate *priv;
4143 GtkThemingEngineClass *engine_class;
4145 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4146 g_return_if_fail (cr != NULL);
4147 g_return_if_fail (xy0_gap <= xy1_gap);
4148 g_return_if_fail (xy0_gap >= 0);
4150 if (width <= 0 || height <= 0)
4153 if (gap_side == GTK_POS_LEFT ||
4154 gap_side == GTK_POS_RIGHT)
4155 g_return_if_fail (xy1_gap <= height);
4157 g_return_if_fail (xy1_gap <= width);
4159 priv = context->priv;
4160 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4164 store_animation_region (context, x, y, width, height);
4166 _gtk_theming_engine_set_context (priv->theming_engine, context);
4167 engine_class->render_frame_gap (priv->theming_engine, cr,
4168 x, y, width, height, gap_side,
4175 * gtk_render_extension:
4176 * @context: a #GtkStyleContext
4178 * @x: X origin of the rectangle
4179 * @y: Y origin of the rectangle
4180 * @width: rectangle width
4181 * @height: rectangle height
4182 * @gap_side: side where the gap is
4184 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4185 * defined by @x, @y, @width, @height. The side where the extension
4186 * connects to is defined by @gap_side.
4189 * <title>Typical extension rendering</title>
4190 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4196 gtk_render_extension (GtkStyleContext *context,
4202 GtkPositionType gap_side)
4204 GtkStyleContextPrivate *priv;
4205 GtkThemingEngineClass *engine_class;
4207 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4208 g_return_if_fail (cr != NULL);
4210 if (width <= 0 || height <= 0)
4213 priv = context->priv;
4214 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4218 store_animation_region (context, x, y, width, height);
4220 _gtk_theming_engine_set_context (priv->theming_engine, context);
4221 engine_class->render_extension (priv->theming_engine, cr, x, y, width, height, gap_side);
4227 * gtk_render_handle:
4228 * @context: a #GtkStyleContext
4230 * @x: X origin of the rectangle
4231 * @y: Y origin of the rectangle
4232 * @width: rectangle width
4233 * @height: rectangle height
4235 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4236 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4237 * determined by @x, @y, @width, @height.
4240 * <title>Handles rendered for the paned and grip classes</title>
4241 * <inlinegraphic fileref="handles.png" format="PNG"/>
4247 gtk_render_handle (GtkStyleContext *context,
4254 GtkStyleContextPrivate *priv;
4255 GtkThemingEngineClass *engine_class;
4257 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4258 g_return_if_fail (cr != NULL);
4260 if (width <= 0 || height <= 0)
4263 priv = context->priv;
4264 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4268 store_animation_region (context, x, y, width, height);
4270 _gtk_theming_engine_set_context (priv->theming_engine, context);
4271 engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
4277 * gtk_render_activity:
4278 * @context: a #GtkStyleContext
4280 * @x: X origin of the rectangle
4281 * @y: Y origin of the rectangle
4282 * @width: rectangle width
4283 * @height: rectangle height
4285 * Renders an activity area (Such as in #GtkSpinner or the
4286 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4287 * determines whether there is activity going on.
4292 gtk_render_activity (GtkStyleContext *context,
4299 GtkStyleContextPrivate *priv;
4300 GtkThemingEngineClass *engine_class;
4302 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4303 g_return_if_fail (cr != NULL);
4305 if (width <= 0 || height <= 0)
4308 priv = context->priv;
4309 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4313 store_animation_region (context, x, y, width, height);
4315 _gtk_theming_engine_set_context (priv->theming_engine, context);
4316 engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
4322 * gtk_render_icon_pixbuf:
4323 * @context: a #GtkStyleContext
4324 * @source: the #GtkIconSource specifying the icon to render
4325 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4326 * means render at the size of the source and don't scale.
4328 * Renders the icon specified by @source at the given @size, returning the result
4331 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4336 gtk_render_icon_pixbuf (GtkStyleContext *context,
4337 const GtkIconSource *source,
4340 GtkStyleContextPrivate *priv;
4341 GtkThemingEngineClass *engine_class;
4343 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4344 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4345 g_return_val_if_fail (source != NULL, NULL);
4347 priv = context->priv;
4348 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4350 _gtk_theming_engine_set_context (priv->theming_engine, context);
4351 return engine_class->render_icon_pixbuf (priv->theming_engine, source, size);
4356 * @context: a #GtkStyleContext
4358 * @pixbuf: a #GdkPixbuf containing the icon to draw
4359 * @x: X position for the @pixbuf
4360 * @y: Y position for the @pixbuf
4362 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4367 gtk_render_icon (GtkStyleContext *context,
4373 GtkStyleContextPrivate *priv;
4374 GtkThemingEngineClass *engine_class;
4376 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4377 g_return_if_fail (cr != NULL);
4379 priv = context->priv;
4380 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4384 store_animation_region (context,
4386 gdk_pixbuf_get_width (pixbuf),
4387 gdk_pixbuf_get_height (pixbuf));
4389 _gtk_theming_engine_set_context (priv->theming_engine, context);
4390 engine_class->render_icon (priv->theming_engine, cr, pixbuf, x, y);
4396 draw_insertion_cursor (GtkStyleContext *context,
4401 gboolean is_primary,
4402 PangoDirection direction,
4403 gboolean draw_arrow)
4406 GdkRGBA primary_color;
4407 GdkRGBA secondary_color;
4408 gfloat cursor_aspect_ratio;
4414 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4415 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4417 /* When changing the shape or size of the cursor here,
4418 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4421 gtk_style_context_get_style (context,
4422 "cursor-aspect-ratio", &cursor_aspect_ratio,
4425 stem_width = height * cursor_aspect_ratio + 1;
4427 /* put (stem_width % 2) on the proper side of the cursor */
4428 if (direction == PANGO_DIRECTION_LTR)
4429 offset = stem_width / 2;
4431 offset = stem_width - stem_width / 2;
4433 cairo_rectangle (cr, x - offset, y, stem_width, height);
4441 arrow_width = stem_width + 1;
4443 if (direction == PANGO_DIRECTION_RTL)
4445 ax = x - offset - 1;
4446 ay = y + height - arrow_width * 2 - arrow_width + 1;
4448 cairo_move_to (cr, ax, ay + 1);
4449 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4450 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4453 else if (direction == PANGO_DIRECTION_LTR)
4455 ax = x + stem_width - offset;
4456 ay = y + height - arrow_width * 2 - arrow_width + 1;
4458 cairo_move_to (cr, ax, ay + 1);
4459 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4460 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4464 g_assert_not_reached();
4471 * gtk_render_insertion_cursor:
4472 * @context: a #GtkStyleContext
4476 * @layout: the #PangoLayout of the text
4477 * @index: the index in the #PangoLayout
4478 * @direction: the #PangoDirection of the text
4480 * Draws a text caret on @cr at the specified index of @layout.
4485 gtk_render_insertion_cursor (GtkStyleContext *context,
4489 PangoLayout *layout,
4491 PangoDirection direction)
4493 GtkStyleContextPrivate *priv;
4494 gboolean split_cursor;
4495 PangoRectangle strong_pos, weak_pos;
4496 PangoRectangle *cursor1, *cursor2;
4497 PangoDirection keymap_direction;
4498 PangoDirection direction2;
4500 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4501 g_return_if_fail (cr != NULL);
4502 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4503 g_return_if_fail (index >= 0);
4505 priv = context->priv;
4507 g_object_get (gtk_settings_get_for_screen (priv->screen),
4508 "gtk-split-cursor", &split_cursor,
4511 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4513 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4515 direction2 = PANGO_DIRECTION_NEUTRAL;
4519 cursor1 = &strong_pos;
4521 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4523 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4524 cursor2 = &weak_pos;
4529 if (keymap_direction == direction)
4530 cursor1 = &strong_pos;
4532 cursor1 = &weak_pos;
4535 draw_insertion_cursor (context,
4537 x + PANGO_PIXELS (cursor1->x),
4538 y + PANGO_PIXELS (cursor1->y),
4539 PANGO_PIXELS (cursor1->height),
4542 direction2 != PANGO_DIRECTION_NEUTRAL);
4544 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4546 draw_insertion_cursor (context,
4548 x + PANGO_PIXELS (cursor2->x),
4549 y + PANGO_PIXELS (cursor2->y),
4550 PANGO_PIXELS (cursor2->height),
4558 * gtk_draw_insertion_cursor:
4559 * @widget: a #GtkWidget
4560 * @cr: cairo context to draw to
4561 * @location: location where to draw the cursor (@location->width is ignored)
4562 * @is_primary: if the cursor should be the primary cursor color.
4563 * @direction: whether the cursor is left-to-right or
4564 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4565 * @draw_arrow: %TRUE to draw a directional arrow on the
4566 * cursor. Should be %FALSE unless the cursor is split.
4568 * Draws a text caret on @cr at @location. This is not a style function
4569 * but merely a convenience function for drawing the standard cursor shape.
4572 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4575 gtk_draw_insertion_cursor (GtkWidget *widget,
4577 const GdkRectangle *location,
4578 gboolean is_primary,
4579 GtkTextDirection direction,
4580 gboolean draw_arrow)
4582 GtkStyleContext *context;
4584 g_return_if_fail (GTK_IS_WIDGET (widget));
4585 g_return_if_fail (cr != NULL);
4586 g_return_if_fail (location != NULL);
4587 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4589 context = gtk_widget_get_style_context (widget);
4591 draw_insertion_cursor (context, cr,
4592 location->x, location->y, location->height,
4594 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4598 static AtkAttributeSet *
4599 add_attribute (AtkAttributeSet *attributes,
4600 AtkTextAttribute attr,
4605 at = g_new (AtkAttribute, 1);
4606 at->name = g_strdup (atk_text_attribute_get_name (attr));
4607 at->value = g_strdup (value);
4609 return g_slist_prepend (attributes, at);
4613 * _gtk_style_context_get_attributes:
4614 * @attributes: a #AtkAttributeSet to add attributes to
4615 * @context: the #GtkStyleContext to get attributes from
4616 * @flags: the state to use with @context
4618 * Adds the foreground and background color from @context to
4619 * @attributes, after translating them to ATK attributes.
4621 * This is a convenience function that can be used in
4622 * implementing the #AtkText interface in widgets.
4624 * Returns: the modified #AtkAttributeSet
4627 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4628 GtkStyleContext *context,
4629 GtkStateFlags flags)
4634 gtk_style_context_get_background_color (context, flags, &color);
4635 value = g_strdup_printf ("%u,%u,%u",
4636 (guint) ceil (color.red * 65536 - color.red),
4637 (guint) ceil (color.green * 65536 - color.green),
4638 (guint) ceil (color.blue * 65536 - color.blue));
4639 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4642 gtk_style_context_get_color (context, flags, &color);
4643 value = g_strdup_printf ("%u,%u,%u",
4644 (guint) ceil (color.red * 65536 - color.red),
4645 (guint) ceil (color.green * 65536 - color.green),
4646 (guint) ceil (color.blue * 65536 - color.blue));
4647 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);