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),
2490 prop_name = va_arg (args, const gchar *);
2495 * gtk_style_context_get_style:
2496 * @context: a #GtkStyleContext
2497 * @...: property name /return value pairs, followed by %NULL
2499 * Retrieves several widget style properties from @context according to the
2505 gtk_style_context_get_style (GtkStyleContext *context,
2510 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2512 va_start (args, context);
2513 gtk_style_context_get_style_valist (context, args);
2519 * gtk_style_context_lookup_icon_set:
2520 * @context: a #GtkStyleContext
2521 * @stock_id: an icon name
2523 * Looks up @stock_id in the icon factories associated to @context and
2524 * the default icon factory, returning an icon set if found, otherwise
2527 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2530 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2531 const gchar *stock_id)
2533 GtkStyleContextPrivate *priv;
2535 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2536 g_return_val_if_fail (stock_id != NULL, NULL);
2538 priv = context->priv;
2539 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2541 return gtk_icon_factory_lookup_default (stock_id);
2545 * gtk_style_context_set_screen:
2546 * @context: a #GtkStyleContext
2547 * @screen: a #GdkScreen
2549 * Attaches @context to the given screen.
2551 * The screen is used to add style information from 'global' style
2552 * providers, such as the screens #GtkSettings instance.
2554 * If you are using a #GtkStyleContext returned from
2555 * gtk_widget_get_style_context(), you do not need to
2556 * call this yourself.
2561 gtk_style_context_set_screen (GtkStyleContext *context,
2564 GtkStyleContextPrivate *priv;
2566 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2567 g_return_if_fail (GDK_IS_SCREEN (screen));
2569 priv = context->priv;
2570 if (priv->screen == screen)
2573 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2575 g_object_unref (priv->cascade);
2576 priv->cascade = _gtk_style_cascade_get_for_screen (screen);
2577 g_object_ref (priv->cascade);
2581 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2584 priv->screen = screen;
2586 g_object_notify (G_OBJECT (context), "screen");
2588 gtk_style_context_invalidate (context);
2592 * gtk_style_context_get_screen:
2593 * @context: a #GtkStyleContext
2595 * Returns the #GdkScreen to which @context is attached.
2597 * Returns: (transfer none): a #GdkScreen.
2600 gtk_style_context_get_screen (GtkStyleContext *context)
2602 GtkStyleContextPrivate *priv;
2604 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2606 priv = context->priv;
2607 return priv->screen;
2611 * gtk_style_context_set_direction:
2612 * @context: a #GtkStyleContext
2613 * @direction: the new direction.
2615 * Sets the reading direction for rendering purposes.
2617 * If you are using a #GtkStyleContext returned from
2618 * gtk_widget_get_style_context(), you do not need to
2619 * call this yourself.
2624 gtk_style_context_set_direction (GtkStyleContext *context,
2625 GtkTextDirection direction)
2627 GtkStyleContextPrivate *priv;
2629 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2631 priv = context->priv;
2632 priv->direction = direction;
2634 g_object_notify (G_OBJECT (context), "direction");
2638 * gtk_style_context_get_direction:
2639 * @context: a #GtkStyleContext
2641 * Returns the widget direction used for rendering.
2643 * Returns: the widget direction
2648 gtk_style_context_get_direction (GtkStyleContext *context)
2650 GtkStyleContextPrivate *priv;
2652 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2654 priv = context->priv;
2655 return priv->direction;
2659 * gtk_style_context_set_junction_sides:
2660 * @context: a #GtkStyleContext
2661 * @sides: sides where rendered elements are visually connected to
2664 * Sets the sides where rendered elements (mostly through
2665 * gtk_render_frame()) will visually connect with other visual elements.
2667 * This is merely a hint that may or may not be honored
2668 * by theming engines.
2670 * Container widgets are expected to set junction hints as appropriate
2671 * for their children, so it should not normally be necessary to call
2672 * this function manually.
2677 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2678 GtkJunctionSides sides)
2680 GtkStyleContextPrivate *priv;
2683 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2685 priv = context->priv;
2686 info = priv->info_stack->data;
2687 info->junction_sides = sides;
2691 * gtk_style_context_get_junction_sides:
2692 * @context: a #GtkStyleContext
2694 * Returns the sides where rendered elements connect visually with others.
2696 * Returns: the junction sides
2701 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2703 GtkStyleContextPrivate *priv;
2706 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2708 priv = context->priv;
2709 info = priv->info_stack->data;
2710 return info->junction_sides;
2713 static GtkSymbolicColor *
2714 gtk_style_context_color_lookup_func (gpointer contextp,
2717 GtkStyleContext *context = contextp;
2719 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2723 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2724 GtkSymbolicColor *color)
2726 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2727 g_return_val_if_fail (color != NULL, FALSE);
2729 return _gtk_symbolic_color_resolve_full (color,
2730 gtk_style_context_color_lookup_func,
2736 _gtk_style_context_resolve_color (GtkStyleContext *context,
2737 GtkSymbolicColor *color,
2742 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2743 g_return_val_if_fail (color != NULL, FALSE);
2744 g_return_val_if_fail (result != NULL, FALSE);
2746 val = _gtk_symbolic_color_resolve_full (color,
2747 gtk_style_context_color_lookup_func,
2752 *result = *_gtk_css_value_get_rgba (val);
2753 _gtk_css_value_unref (val);
2758 * gtk_style_context_lookup_color:
2759 * @context: a #GtkStyleContext
2760 * @color_name: color name to lookup
2761 * @color: (out): Return location for the looked up color
2763 * Looks up and resolves a color name in the @context color map.
2765 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2768 gtk_style_context_lookup_color (GtkStyleContext *context,
2769 const gchar *color_name,
2772 GtkSymbolicColor *sym_color;
2774 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2775 g_return_val_if_fail (color_name != NULL, FALSE);
2776 g_return_val_if_fail (color != NULL, FALSE);
2778 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2779 if (sym_color == NULL)
2782 return _gtk_style_context_resolve_color (context, sym_color, color);
2786 * gtk_style_context_notify_state_change:
2787 * @context: a #GtkStyleContext
2788 * @window: a #GdkWindow
2789 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2790 * See gtk_style_context_push_animatable_region()
2791 * @state: state to trigger transition for
2792 * @state_value: %TRUE if @state is the state we are changing to,
2793 * %FALSE if we are changing away from it
2795 * Notifies a state change on @context, so if the current style makes use
2796 * of transition animations, one will be started so all rendered elements
2797 * under @region_id are animated for state @state being set to value
2800 * The @window parameter is used in order to invalidate the rendered area
2801 * as the animation runs, so make sure it is the same window that is being
2802 * rendered on by the gtk_render_*() functions.
2804 * If @region_id is %NULL, all rendered elements using @context will be
2805 * affected by this state transition.
2807 * As a practical example, a #GtkButton notifying a state transition on
2808 * the prelight state:
2810 * gtk_style_context_notify_state_change (context,
2811 * gtk_widget_get_window (widget),
2813 * GTK_STATE_PRELIGHT,
2814 * button->in_button);
2817 * Can be handled in the CSS file like this:
2820 * background-color: #f00
2824 * background-color: #fff;
2825 * transition: 200ms linear
2829 * This combination will animate the button background from red to white
2830 * if a pointer enters the button, and back to red if the pointer leaves
2833 * Note that @state is used when finding the transition parameters, which
2834 * is why the style places the transition under the :hover pseudo-class.
2839 gtk_style_context_notify_state_change (GtkStyleContext *context,
2843 gboolean state_value)
2845 GtkStyleContextPrivate *priv;
2846 GtkAnimationDescription *desc;
2847 AnimationInfo *info;
2848 GtkStateFlags flags;
2852 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2853 g_return_if_fail (GDK_IS_WINDOW (window));
2854 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2856 priv = context->priv;
2857 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
2859 state_value = (state_value == TRUE);
2863 case GTK_STATE_ACTIVE:
2864 flags = GTK_STATE_FLAG_ACTIVE;
2866 case GTK_STATE_PRELIGHT:
2867 flags = GTK_STATE_FLAG_PRELIGHT;
2869 case GTK_STATE_SELECTED:
2870 flags = GTK_STATE_FLAG_SELECTED;
2872 case GTK_STATE_INSENSITIVE:
2873 flags = GTK_STATE_FLAG_INSENSITIVE;
2875 case GTK_STATE_INCONSISTENT:
2876 flags = GTK_STATE_FLAG_INCONSISTENT;
2878 case GTK_STATE_FOCUSED:
2879 flags = GTK_STATE_FLAG_FOCUSED;
2881 case GTK_STATE_NORMAL:
2887 /* Find out if there is any animation description for the given
2888 * state, it will fallback to the normal state as well if necessary.
2890 data = style_data_lookup (context, flags);
2891 v = _gtk_css_computed_values_get_value_by_name (data->store, "transition");
2894 desc = _gtk_css_value_get_boxed (v);
2898 if (_gtk_animation_description_get_duration (desc) == 0)
2901 info = animation_info_lookup (context, region_id, state);
2904 info->target_value != state_value)
2906 /* Target values are the opposite */
2907 if (!_gtk_timeline_get_loop (info->timeline))
2909 /* Reverse the animation */
2910 if (_gtk_timeline_get_direction (info->timeline) == GTK_TIMELINE_DIRECTION_FORWARD)
2911 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
2913 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_FORWARD);
2915 info->target_value = state_value;
2919 /* Take it out of its looping state */
2920 _gtk_timeline_set_loop (info->timeline, FALSE);
2924 (!_gtk_animation_description_get_loop (desc) ||
2927 info = animation_info_new (context, region_id,
2928 _gtk_animation_description_get_duration (desc),
2929 _gtk_animation_description_get_progress_type (desc),
2930 _gtk_animation_description_get_loop (desc),
2931 state, state_value, window);
2933 priv->animations = g_slist_prepend (priv->animations, info);
2934 priv->animations_invalidated = TRUE;
2939 * gtk_style_context_cancel_animations:
2940 * @context: a #GtkStyleContext
2941 * @region_id: (allow-none): animatable region to stop, or %NULL.
2942 * See gtk_style_context_push_animatable_region()
2944 * Stops all running animations for @region_id and all animatable
2945 * regions underneath.
2947 * A %NULL @region_id will stop all ongoing animations in @context,
2948 * when dealing with a #GtkStyleContext obtained through
2949 * gtk_widget_get_style_context(), this is normally done for you
2950 * in all circumstances you would expect all widget to be stopped,
2951 * so this should be only used in complex widgets with different
2952 * animatable regions.
2957 gtk_style_context_cancel_animations (GtkStyleContext *context,
2960 GtkStyleContextPrivate *priv;
2961 AnimationInfo *info;
2964 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2966 priv = context->priv;
2967 l = priv->animations;
2975 info->region_id == region_id ||
2976 g_slist_find (info->parent_regions, region_id))
2978 priv->animations = g_slist_remove (priv->animations, info);
2979 animation_info_free (info);
2985 is_parent_of (GdkWindow *parent,
2988 GtkWidget *child_widget, *parent_widget;
2991 gdk_window_get_user_data (child, (gpointer *) &child_widget);
2992 gdk_window_get_user_data (parent, (gpointer *) &parent_widget);
2994 if (child_widget != parent_widget &&
2995 !gtk_widget_is_ancestor (child_widget, parent_widget))
3002 if (window == parent)
3005 window = gdk_window_get_parent (window);
3012 * gtk_style_context_scroll_animations:
3013 * @context: a #GtkStyleContext
3014 * @window: a #GdkWindow used previously in
3015 * gtk_style_context_notify_state_change()
3016 * @dx: Amount to scroll in the X axis
3017 * @dy: Amount to scroll in the Y axis
3019 * This function is analogous to gdk_window_scroll(), and
3020 * should be called together with it so the invalidation
3021 * areas for any ongoing animation are scrolled together
3027 gtk_style_context_scroll_animations (GtkStyleContext *context,
3032 GtkStyleContextPrivate *priv;
3033 AnimationInfo *info;
3036 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3037 g_return_if_fail (GDK_IS_WINDOW (window));
3039 priv = context->priv;
3040 l = priv->animations;
3047 if (info->invalidation_region &&
3048 (window == info->window ||
3049 is_parent_of (window, info->window)))
3050 cairo_region_translate (info->invalidation_region, dx, dy);
3055 * gtk_style_context_push_animatable_region:
3056 * @context: a #GtkStyleContext
3057 * @region_id: unique identifier for the animatable region
3059 * Pushes an animatable region, so all further gtk_render_*() calls between
3060 * this call and the following gtk_style_context_pop_animatable_region()
3061 * will potentially show transition animations for this region if
3062 * gtk_style_context_notify_state_change() is called for a given state,
3063 * and the current theme/style defines transition animations for state
3066 * The @region_id used must be unique in @context so the theming engine
3067 * can uniquely identify rendered elements subject to a state transition.
3072 gtk_style_context_push_animatable_region (GtkStyleContext *context,
3075 GtkStyleContextPrivate *priv;
3077 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3078 g_return_if_fail (region_id != NULL);
3080 priv = context->priv;
3081 priv->animation_regions = g_slist_prepend (priv->animation_regions, region_id);
3085 * gtk_style_context_pop_animatable_region:
3086 * @context: a #GtkStyleContext
3088 * Pops an animatable region from @context.
3089 * See gtk_style_context_push_animatable_region().
3094 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
3096 GtkStyleContextPrivate *priv;
3098 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3100 priv = context->priv;
3101 priv->animation_regions = g_slist_delete_link (priv->animation_regions,
3102 priv->animation_regions);
3106 _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context)
3108 GtkStyleContextPrivate *priv;
3111 priv = context->priv;
3113 for (l = priv->animations; l; l = l->next)
3115 AnimationInfo *info;
3119 /* A NULL invalidation region means it has to be recreated on
3120 * the next expose event, this happens usually after a widget
3121 * allocation change, so the next expose after it will update
3122 * the invalidation region.
3124 if (info->invalidation_region)
3126 cairo_region_destroy (info->invalidation_region);
3127 info->invalidation_region = NULL;
3131 priv->animations_invalidated = TRUE;
3135 _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context,
3138 GtkStyleContextPrivate *priv;
3141 priv = context->priv;
3143 if (!priv->animations_invalidated)
3146 l = priv->animations;
3150 AnimationInfo *info;
3159 if (info->invalidation_region)
3162 if (info->rectangles->len == 0)
3165 info->invalidation_region = cairo_region_create ();
3166 _gtk_widget_get_translation_to_window (widget, info->window, &rel_x, &rel_y);
3168 for (i = 0; i < info->rectangles->len; i++)
3170 cairo_rectangle_int_t *rect;
3172 rect = &g_array_index (info->rectangles, cairo_rectangle_int_t, i);
3174 /* These are widget relative coordinates,
3175 * so have them inverted to be window relative
3180 cairo_region_union_rectangle (info->invalidation_region, rect);
3183 g_array_remove_range (info->rectangles, 0, info->rectangles->len);
3186 priv->animations_invalidated = FALSE;
3190 store_animation_region (GtkStyleContext *context,
3196 GtkStyleContextPrivate *priv;
3199 priv = context->priv;
3201 if (!priv->animations_invalidated)
3204 for (l = priv->animations; l; l = l->next)
3206 AnimationInfo *info;
3210 /* The animation doesn't need updating
3211 * the invalidation area, bail out.
3213 if (info->invalidation_region)
3216 if (context_has_animatable_region (context, info->region_id))
3218 cairo_rectangle_int_t rect;
3222 rect.width = (gint) width;
3223 rect.height = (gint) height;
3225 g_array_append_val (info->rectangles, rect);
3227 if (!info->parent_regions)
3229 GSList *parent_regions;
3231 parent_regions = g_slist_find (priv->animation_regions, info->region_id);
3232 info->parent_regions = g_slist_copy (parent_regions);
3239 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3240 GtkCssChange change)
3242 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3243 g_return_if_fail (change != 0);
3245 gtk_style_context_invalidate (context);
3249 * gtk_style_context_invalidate:
3250 * @context: a #GtkStyleContext.
3252 * Invalidates @context style information, so it will be reconstructed
3255 * If you're using a #GtkStyleContext returned from
3256 * gtk_widget_get_style_context(), you do not need to
3257 * call this yourself.
3262 gtk_style_context_invalidate (GtkStyleContext *context)
3264 GtkStyleContextPrivate *priv;
3266 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3268 priv = context->priv;
3270 /* Avoid reentrancy */
3271 if (priv->invalidating_context)
3274 priv->invalidating_context = TRUE;
3276 g_hash_table_remove_all (priv->style_data);
3277 priv->current_data = NULL;
3279 g_signal_emit (context, signals[CHANGED], 0);
3281 priv->invalidating_context = FALSE;
3285 * gtk_style_context_set_background:
3286 * @context: a #GtkStyleContext
3287 * @window: a #GdkWindow
3289 * Sets the background of @window to the background pattern or
3290 * color specified in @context for its current state.
3295 gtk_style_context_set_background (GtkStyleContext *context,
3298 GtkStateFlags state;
3299 cairo_pattern_t *pattern;
3302 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3303 g_return_if_fail (GDK_IS_WINDOW (window));
3305 state = gtk_style_context_get_state (context);
3306 gtk_style_context_get (context, state,
3307 "background-image", &pattern,
3311 gdk_window_set_background_pattern (window, pattern);
3312 cairo_pattern_destroy (pattern);
3316 gtk_style_context_get (context, state,
3317 "background-color", &color,
3321 gdk_window_set_background_rgba (window, color);
3322 gdk_rgba_free (color);
3327 * gtk_style_context_get_color:
3328 * @context: a #GtkStyleContext
3329 * @state: state to retrieve the color for
3330 * @color: (out): return value for the foreground color
3332 * Gets the foreground color for a given state.
3337 gtk_style_context_get_color (GtkStyleContext *context,
3338 GtkStateFlags state,
3343 g_return_if_fail (color != NULL);
3344 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3346 gtk_style_context_get (context,
3356 * gtk_style_context_get_background_color:
3357 * @context: a #GtkStyleContext
3358 * @state: state to retrieve the color for
3359 * @color: (out): return value for the background color
3361 * Gets the background color for a given state.
3366 gtk_style_context_get_background_color (GtkStyleContext *context,
3367 GtkStateFlags state,
3372 g_return_if_fail (color != NULL);
3373 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3375 gtk_style_context_get (context,
3377 "background-color", &c,
3385 * gtk_style_context_get_border_color:
3386 * @context: a #GtkStyleContext
3387 * @state: state to retrieve the color for
3388 * @color: (out): return value for the border color
3390 * Gets the border color for a given state.
3395 gtk_style_context_get_border_color (GtkStyleContext *context,
3396 GtkStateFlags state,
3401 g_return_if_fail (color != NULL);
3402 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3404 gtk_style_context_get (context,
3414 * gtk_style_context_get_border:
3415 * @context: a #GtkStyleContext
3416 * @state: state to retrieve the border for
3417 * @border: (out): return value for the border settings
3419 * Gets the border for a given state as a #GtkBorder.
3420 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3425 gtk_style_context_get_border (GtkStyleContext *context,
3426 GtkStateFlags state,
3429 int top, left, bottom, right;
3431 g_return_if_fail (border != NULL);
3432 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3434 gtk_style_context_get (context,
3436 "border-top-width", &top,
3437 "border-left-width", &left,
3438 "border-bottom-width", &bottom,
3439 "border-right-width", &right,
3443 border->left = left;
3444 border->bottom = bottom;
3445 border->right = right;
3449 * gtk_style_context_get_padding:
3450 * @context: a #GtkStyleContext
3451 * @state: state to retrieve the padding for
3452 * @padding: (out): return value for the padding settings
3454 * Gets the padding for a given state as a #GtkBorder.
3455 * See %GTK_STYLE_PROPERTY_PADDING.
3460 gtk_style_context_get_padding (GtkStyleContext *context,
3461 GtkStateFlags state,
3464 int top, left, bottom, right;
3466 g_return_if_fail (padding != NULL);
3467 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3469 gtk_style_context_get (context,
3471 "padding-top", &top,
3472 "padding-left", &left,
3473 "padding-bottom", &bottom,
3474 "padding-right", &right,
3478 padding->left = left;
3479 padding->bottom = bottom;
3480 padding->right = right;
3484 * gtk_style_context_get_margin:
3485 * @context: a #GtkStyleContext
3486 * @state: state to retrieve the border for
3487 * @margin: (out): return value for the margin settings
3489 * Gets the margin for a given state as a #GtkBorder.
3490 * See %GTK_STYLE_PROPERTY_MARGIN.
3495 gtk_style_context_get_margin (GtkStyleContext *context,
3496 GtkStateFlags state,
3499 int top, left, bottom, right;
3501 g_return_if_fail (margin != NULL);
3502 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3504 gtk_style_context_get (context,
3507 "margin-left", &left,
3508 "margin-bottom", &bottom,
3509 "margin-right", &right,
3513 margin->left = left;
3514 margin->bottom = bottom;
3515 margin->right = right;
3519 * gtk_style_context_get_font:
3520 * @context: a #GtkStyleContext
3521 * @state: state to retrieve the font for
3523 * Returns the font description for a given state. The returned
3524 * object is const and will remain valid until the
3525 * #GtkStyleContext::changed signal happens.
3527 * Returns: (transfer none): the #PangoFontDescription for the given
3528 * state. This object is owned by GTK+ and should not be
3533 const PangoFontDescription *
3534 gtk_style_context_get_font (GtkStyleContext *context,
3535 GtkStateFlags state)
3537 GtkStyleContextPrivate *priv;
3539 PangoFontDescription *description;
3541 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3543 priv = context->priv;
3544 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3546 data = style_data_lookup (context, state);
3548 /* Yuck, fonts are created on-demand but we don't return a ref.
3549 * Do bad things to achieve this requirement */
3550 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3551 if (description == NULL)
3553 gtk_style_context_get (context, state, "font", &description, NULL);
3554 g_object_set_data_full (G_OBJECT (data->store),
3555 "font-cache-for-get_font",
3557 (GDestroyNotify) pango_font_description_free);
3563 get_cursor_color (GtkStyleContext *context,
3567 GdkColor *style_color;
3569 gtk_style_context_get_style (context,
3570 primary ? "cursor-color" : "secondary-cursor-color",
3576 color->red = style_color->red / 65535.0;
3577 color->green = style_color->green / 65535.0;
3578 color->blue = style_color->blue / 65535.0;
3581 gdk_color_free (style_color);
3585 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3591 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3593 color->red = (color->red + bg.red) * 0.5;
3594 color->green = (color->green + bg.green) * 0.5;
3595 color->blue = (color->blue + bg.blue) * 0.5;
3601 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3602 GdkRGBA *primary_color,
3603 GdkRGBA *secondary_color)
3606 get_cursor_color (context, TRUE, primary_color);
3608 if (secondary_color)
3609 get_cursor_color (context, FALSE, secondary_color);
3616 * @context: a #GtkStyleContext
3618 * @x: X origin of the rectangle
3619 * @y: Y origin of the rectangle
3620 * @width: rectangle width
3621 * @height: rectangle height
3623 * Renders a checkmark (as in a #GtkCheckButton).
3625 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3626 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3627 * should be marked as undefined.
3630 * <title>Typical checkmark rendering</title>
3631 * <inlinegraphic fileref="checks.png" format="PNG"/>
3637 gtk_render_check (GtkStyleContext *context,
3644 GtkStyleContextPrivate *priv;
3645 GtkThemingEngineClass *engine_class;
3647 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3648 g_return_if_fail (cr != NULL);
3650 if (width <= 0 || height <= 0)
3653 priv = context->priv;
3654 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3658 store_animation_region (context, x, y, width, height);
3660 _gtk_theming_engine_set_context (priv->theming_engine, context);
3661 engine_class->render_check (priv->theming_engine, cr,
3662 x, y, width, height);
3668 * gtk_render_option:
3669 * @context: a #GtkStyleContext
3671 * @x: X origin of the rectangle
3672 * @y: Y origin of the rectangle
3673 * @width: rectangle width
3674 * @height: rectangle height
3676 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3677 * state will determine whether the option is on or off, and
3678 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3681 * <title>Typical option mark rendering</title>
3682 * <inlinegraphic fileref="options.png" format="PNG"/>
3688 gtk_render_option (GtkStyleContext *context,
3695 GtkStyleContextPrivate *priv;
3696 GtkThemingEngineClass *engine_class;
3698 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3699 g_return_if_fail (cr != NULL);
3701 if (width <= 0 || height <= 0)
3704 priv = context->priv;
3705 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3709 store_animation_region (context, x, y, width, height);
3711 _gtk_theming_engine_set_context (priv->theming_engine, context);
3712 engine_class->render_option (priv->theming_engine, cr,
3713 x, y, width, height);
3720 * @context: a #GtkStyleContext
3722 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3723 * @x: X origin of the render area
3724 * @y: Y origin of the render area
3725 * @size: square side for render area
3727 * Renders an arrow pointing to @angle.
3730 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3731 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3737 gtk_render_arrow (GtkStyleContext *context,
3744 GtkStyleContextPrivate *priv;
3745 GtkThemingEngineClass *engine_class;
3747 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3748 g_return_if_fail (cr != NULL);
3753 priv = context->priv;
3754 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3758 gtk_style_context_save (context);
3759 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3761 store_animation_region (context, x, y, size, size);
3763 _gtk_theming_engine_set_context (priv->theming_engine, context);
3764 engine_class->render_arrow (priv->theming_engine, cr,
3767 gtk_style_context_restore (context);
3772 * gtk_render_background:
3773 * @context: a #GtkStyleContext
3775 * @x: X origin of the rectangle
3776 * @y: Y origin of the rectangle
3777 * @width: rectangle width
3778 * @height: rectangle height
3780 * Renders the background of an element.
3783 * <title>Typical background rendering, showing the effect of
3784 * <parameter>background-image</parameter>,
3785 * <parameter>border-width</parameter> and
3786 * <parameter>border-radius</parameter></title>
3787 * <inlinegraphic fileref="background.png" format="PNG"/>
3793 gtk_render_background (GtkStyleContext *context,
3800 GtkStyleContextPrivate *priv;
3801 GtkThemingEngineClass *engine_class;
3803 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3804 g_return_if_fail (cr != NULL);
3806 if (width <= 0 || height <= 0)
3809 priv = context->priv;
3810 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3814 store_animation_region (context, x, y, width, height);
3816 _gtk_theming_engine_set_context (priv->theming_engine, context);
3817 engine_class->render_background (priv->theming_engine, cr, x, y, width, height);
3824 * @context: a #GtkStyleContext
3826 * @x: X origin of the rectangle
3827 * @y: Y origin of the rectangle
3828 * @width: rectangle width
3829 * @height: rectangle height
3831 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3834 * <title>Examples of frame rendering, showing the effect of
3835 * <parameter>border-image</parameter>,
3836 * <parameter>border-color</parameter>,
3837 * <parameter>border-width</parameter>,
3838 * <parameter>border-radius</parameter> and
3840 * <inlinegraphic fileref="frames.png" format="PNG"/>
3846 gtk_render_frame (GtkStyleContext *context,
3853 GtkStyleContextPrivate *priv;
3854 GtkThemingEngineClass *engine_class;
3856 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3857 g_return_if_fail (cr != NULL);
3859 if (width <= 0 || height <= 0)
3862 priv = context->priv;
3863 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3867 store_animation_region (context, x, y, width, height);
3869 _gtk_theming_engine_set_context (priv->theming_engine, context);
3870 engine_class->render_frame (priv->theming_engine, cr, x, y, width, height);
3876 * gtk_render_expander:
3877 * @context: a #GtkStyleContext
3879 * @x: X origin of the rectangle
3880 * @y: Y origin of the rectangle
3881 * @width: rectangle width
3882 * @height: rectangle height
3884 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3885 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3886 * determines whether the expander is collapsed or expanded.
3889 * <title>Typical expander rendering</title>
3890 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3896 gtk_render_expander (GtkStyleContext *context,
3903 GtkStyleContextPrivate *priv;
3904 GtkThemingEngineClass *engine_class;
3906 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3907 g_return_if_fail (cr != NULL);
3909 if (width <= 0 || height <= 0)
3912 priv = context->priv;
3913 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3917 store_animation_region (context, x, y, width, height);
3919 _gtk_theming_engine_set_context (priv->theming_engine, context);
3920 engine_class->render_expander (priv->theming_engine, cr, x, y, width, height);
3927 * @context: a #GtkStyleContext
3929 * @x: X origin of the rectangle
3930 * @y: Y origin of the rectangle
3931 * @width: rectangle width
3932 * @height: rectangle height
3934 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3936 * <title>Typical focus rendering</title>
3937 * <inlinegraphic fileref="focus.png" format="PNG"/>
3943 gtk_render_focus (GtkStyleContext *context,
3950 GtkStyleContextPrivate *priv;
3951 GtkThemingEngineClass *engine_class;
3953 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3954 g_return_if_fail (cr != NULL);
3956 if (width <= 0 || height <= 0)
3959 priv = context->priv;
3960 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3964 store_animation_region (context, x, y, width, height);
3966 _gtk_theming_engine_set_context (priv->theming_engine, context);
3967 engine_class->render_focus (priv->theming_engine, cr, x, y, width, height);
3973 * gtk_render_layout:
3974 * @context: a #GtkStyleContext
3978 * @layout: the #PangoLayout to render
3980 * Renders @layout on the coordinates @x, @y
3985 gtk_render_layout (GtkStyleContext *context,
3989 PangoLayout *layout)
3991 GtkStyleContextPrivate *priv;
3992 GtkThemingEngineClass *engine_class;
3993 PangoRectangle extents;
3995 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3996 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3997 g_return_if_fail (cr != NULL);
3999 priv = context->priv;
4000 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4004 pango_layout_get_extents (layout, &extents, NULL);
4006 store_animation_region (context,
4012 _gtk_theming_engine_set_context (priv->theming_engine, context);
4013 engine_class->render_layout (priv->theming_engine, cr, x, y, layout);
4020 * @context: a #GtkStyleContext
4022 * @x0: X coordinate for the origin of the line
4023 * @y0: Y coordinate for the origin of the line
4024 * @x1: X coordinate for the end of the line
4025 * @y1: Y coordinate for the end of the line
4027 * Renders a line from (x0, y0) to (x1, y1).
4032 gtk_render_line (GtkStyleContext *context,
4039 GtkStyleContextPrivate *priv;
4040 GtkThemingEngineClass *engine_class;
4042 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4043 g_return_if_fail (cr != NULL);
4045 priv = context->priv;
4046 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4050 _gtk_theming_engine_set_context (priv->theming_engine, context);
4051 engine_class->render_line (priv->theming_engine, cr, x0, y0, x1, y1);
4057 * gtk_render_slider:
4058 * @context: a #GtkStyleContext
4060 * @x: X origin of the rectangle
4061 * @y: Y origin of the rectangle
4062 * @width: rectangle width
4063 * @height: rectangle height
4064 * @orientation: orientation of the slider
4066 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4067 * @width, @height. @orientation defines whether the slider is vertical
4071 * <title>Typical slider rendering</title>
4072 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4078 gtk_render_slider (GtkStyleContext *context,
4084 GtkOrientation orientation)
4086 GtkStyleContextPrivate *priv;
4087 GtkThemingEngineClass *engine_class;
4089 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4090 g_return_if_fail (cr != NULL);
4092 if (width <= 0 || height <= 0)
4095 priv = context->priv;
4096 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4100 store_animation_region (context, x, y, width, height);
4102 _gtk_theming_engine_set_context (priv->theming_engine, context);
4103 engine_class->render_slider (priv->theming_engine, cr, x, y, width, height, orientation);
4109 * gtk_render_frame_gap:
4110 * @context: a #GtkStyleContext
4112 * @x: X origin of the rectangle
4113 * @y: Y origin of the rectangle
4114 * @width: rectangle width
4115 * @height: rectangle height
4116 * @gap_side: side where the gap is
4117 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4118 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4120 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4121 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4122 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4123 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4126 * <title>Typical rendering of a frame with a gap</title>
4127 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4133 gtk_render_frame_gap (GtkStyleContext *context,
4139 GtkPositionType gap_side,
4143 GtkStyleContextPrivate *priv;
4144 GtkThemingEngineClass *engine_class;
4146 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4147 g_return_if_fail (cr != NULL);
4148 g_return_if_fail (xy0_gap <= xy1_gap);
4149 g_return_if_fail (xy0_gap >= 0);
4151 if (width <= 0 || height <= 0)
4154 if (gap_side == GTK_POS_LEFT ||
4155 gap_side == GTK_POS_RIGHT)
4156 g_return_if_fail (xy1_gap <= height);
4158 g_return_if_fail (xy1_gap <= width);
4160 priv = context->priv;
4161 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4165 store_animation_region (context, x, y, width, height);
4167 _gtk_theming_engine_set_context (priv->theming_engine, context);
4168 engine_class->render_frame_gap (priv->theming_engine, cr,
4169 x, y, width, height, gap_side,
4176 * gtk_render_extension:
4177 * @context: a #GtkStyleContext
4179 * @x: X origin of the rectangle
4180 * @y: Y origin of the rectangle
4181 * @width: rectangle width
4182 * @height: rectangle height
4183 * @gap_side: side where the gap is
4185 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4186 * defined by @x, @y, @width, @height. The side where the extension
4187 * connects to is defined by @gap_side.
4190 * <title>Typical extension rendering</title>
4191 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4197 gtk_render_extension (GtkStyleContext *context,
4203 GtkPositionType gap_side)
4205 GtkStyleContextPrivate *priv;
4206 GtkThemingEngineClass *engine_class;
4208 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4209 g_return_if_fail (cr != NULL);
4211 if (width <= 0 || height <= 0)
4214 priv = context->priv;
4215 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4219 store_animation_region (context, x, y, width, height);
4221 _gtk_theming_engine_set_context (priv->theming_engine, context);
4222 engine_class->render_extension (priv->theming_engine, cr, x, y, width, height, gap_side);
4228 * gtk_render_handle:
4229 * @context: a #GtkStyleContext
4231 * @x: X origin of the rectangle
4232 * @y: Y origin of the rectangle
4233 * @width: rectangle width
4234 * @height: rectangle height
4236 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4237 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4238 * determined by @x, @y, @width, @height.
4241 * <title>Handles rendered for the paned and grip classes</title>
4242 * <inlinegraphic fileref="handles.png" format="PNG"/>
4248 gtk_render_handle (GtkStyleContext *context,
4255 GtkStyleContextPrivate *priv;
4256 GtkThemingEngineClass *engine_class;
4258 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4259 g_return_if_fail (cr != NULL);
4261 if (width <= 0 || height <= 0)
4264 priv = context->priv;
4265 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4269 store_animation_region (context, x, y, width, height);
4271 _gtk_theming_engine_set_context (priv->theming_engine, context);
4272 engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
4278 * gtk_render_activity:
4279 * @context: a #GtkStyleContext
4281 * @x: X origin of the rectangle
4282 * @y: Y origin of the rectangle
4283 * @width: rectangle width
4284 * @height: rectangle height
4286 * Renders an activity area (Such as in #GtkSpinner or the
4287 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4288 * determines whether there is activity going on.
4293 gtk_render_activity (GtkStyleContext *context,
4300 GtkStyleContextPrivate *priv;
4301 GtkThemingEngineClass *engine_class;
4303 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4304 g_return_if_fail (cr != NULL);
4306 if (width <= 0 || height <= 0)
4309 priv = context->priv;
4310 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4314 store_animation_region (context, x, y, width, height);
4316 _gtk_theming_engine_set_context (priv->theming_engine, context);
4317 engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
4323 * gtk_render_icon_pixbuf:
4324 * @context: a #GtkStyleContext
4325 * @source: the #GtkIconSource specifying the icon to render
4326 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4327 * means render at the size of the source and don't scale.
4329 * Renders the icon specified by @source at the given @size, returning the result
4332 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4337 gtk_render_icon_pixbuf (GtkStyleContext *context,
4338 const GtkIconSource *source,
4341 GtkStyleContextPrivate *priv;
4342 GtkThemingEngineClass *engine_class;
4344 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4345 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4346 g_return_val_if_fail (source != NULL, NULL);
4348 priv = context->priv;
4349 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4351 _gtk_theming_engine_set_context (priv->theming_engine, context);
4352 return engine_class->render_icon_pixbuf (priv->theming_engine, source, size);
4357 * @context: a #GtkStyleContext
4359 * @pixbuf: a #GdkPixbuf containing the icon to draw
4360 * @x: X position for the @pixbuf
4361 * @y: Y position for the @pixbuf
4363 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4368 gtk_render_icon (GtkStyleContext *context,
4374 GtkStyleContextPrivate *priv;
4375 GtkThemingEngineClass *engine_class;
4377 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4378 g_return_if_fail (cr != NULL);
4380 priv = context->priv;
4381 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4385 store_animation_region (context,
4387 gdk_pixbuf_get_width (pixbuf),
4388 gdk_pixbuf_get_height (pixbuf));
4390 _gtk_theming_engine_set_context (priv->theming_engine, context);
4391 engine_class->render_icon (priv->theming_engine, cr, pixbuf, x, y);
4397 draw_insertion_cursor (GtkStyleContext *context,
4402 gboolean is_primary,
4403 PangoDirection direction,
4404 gboolean draw_arrow)
4407 GdkRGBA primary_color;
4408 GdkRGBA secondary_color;
4409 gfloat cursor_aspect_ratio;
4415 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4416 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4418 /* When changing the shape or size of the cursor here,
4419 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4422 gtk_style_context_get_style (context,
4423 "cursor-aspect-ratio", &cursor_aspect_ratio,
4426 stem_width = height * cursor_aspect_ratio + 1;
4428 /* put (stem_width % 2) on the proper side of the cursor */
4429 if (direction == PANGO_DIRECTION_LTR)
4430 offset = stem_width / 2;
4432 offset = stem_width - stem_width / 2;
4434 cairo_rectangle (cr, x - offset, y, stem_width, height);
4442 arrow_width = stem_width + 1;
4444 if (direction == PANGO_DIRECTION_RTL)
4446 ax = x - offset - 1;
4447 ay = y + height - arrow_width * 2 - arrow_width + 1;
4449 cairo_move_to (cr, ax, ay + 1);
4450 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4451 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4454 else if (direction == PANGO_DIRECTION_LTR)
4456 ax = x + stem_width - offset;
4457 ay = y + height - arrow_width * 2 - arrow_width + 1;
4459 cairo_move_to (cr, ax, ay + 1);
4460 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4461 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4465 g_assert_not_reached();
4472 * gtk_render_insertion_cursor:
4473 * @context: a #GtkStyleContext
4477 * @layout: the #PangoLayout of the text
4478 * @index: the index in the #PangoLayout
4479 * @direction: the #PangoDirection of the text
4481 * Draws a text caret on @cr at the specified index of @layout.
4486 gtk_render_insertion_cursor (GtkStyleContext *context,
4490 PangoLayout *layout,
4492 PangoDirection direction)
4494 GtkStyleContextPrivate *priv;
4495 gboolean split_cursor;
4496 PangoRectangle strong_pos, weak_pos;
4497 PangoRectangle *cursor1, *cursor2;
4498 PangoDirection keymap_direction;
4499 PangoDirection direction2;
4501 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4502 g_return_if_fail (cr != NULL);
4503 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4504 g_return_if_fail (index >= 0);
4506 priv = context->priv;
4508 g_object_get (gtk_settings_get_for_screen (priv->screen),
4509 "gtk-split-cursor", &split_cursor,
4512 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4514 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4516 direction2 = PANGO_DIRECTION_NEUTRAL;
4520 cursor1 = &strong_pos;
4522 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4524 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4525 cursor2 = &weak_pos;
4530 if (keymap_direction == direction)
4531 cursor1 = &strong_pos;
4533 cursor1 = &weak_pos;
4536 draw_insertion_cursor (context,
4538 x + PANGO_PIXELS (cursor1->x),
4539 y + PANGO_PIXELS (cursor1->y),
4540 PANGO_PIXELS (cursor1->height),
4543 direction2 != PANGO_DIRECTION_NEUTRAL);
4545 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4547 draw_insertion_cursor (context,
4549 x + PANGO_PIXELS (cursor2->x),
4550 y + PANGO_PIXELS (cursor2->y),
4551 PANGO_PIXELS (cursor2->height),
4559 * gtk_draw_insertion_cursor:
4560 * @widget: a #GtkWidget
4561 * @cr: cairo context to draw to
4562 * @location: location where to draw the cursor (@location->width is ignored)
4563 * @is_primary: if the cursor should be the primary cursor color.
4564 * @direction: whether the cursor is left-to-right or
4565 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4566 * @draw_arrow: %TRUE to draw a directional arrow on the
4567 * cursor. Should be %FALSE unless the cursor is split.
4569 * Draws a text caret on @cr at @location. This is not a style function
4570 * but merely a convenience function for drawing the standard cursor shape.
4573 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4576 gtk_draw_insertion_cursor (GtkWidget *widget,
4578 const GdkRectangle *location,
4579 gboolean is_primary,
4580 GtkTextDirection direction,
4581 gboolean draw_arrow)
4583 GtkStyleContext *context;
4585 g_return_if_fail (GTK_IS_WIDGET (widget));
4586 g_return_if_fail (cr != NULL);
4587 g_return_if_fail (location != NULL);
4588 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4590 context = gtk_widget_get_style_context (widget);
4592 draw_insertion_cursor (context, cr,
4593 location->x, location->y, location->height,
4595 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4599 static AtkAttributeSet *
4600 add_attribute (AtkAttributeSet *attributes,
4601 AtkTextAttribute attr,
4606 at = g_new (AtkAttribute, 1);
4607 at->name = g_strdup (atk_text_attribute_get_name (attr));
4608 at->value = g_strdup (value);
4610 return g_slist_prepend (attributes, at);
4614 * _gtk_style_context_get_attributes:
4615 * @attributes: a #AtkAttributeSet to add attributes to
4616 * @context: the #GtkStyleContext to get attributes from
4617 * @flags: the state to use with @context
4619 * Adds the foreground and background color from @context to
4620 * @attributes, after translating them to ATK attributes.
4622 * This is a convenience function that can be used in
4623 * implementing the #AtkText interface in widgets.
4625 * Returns: the modified #AtkAttributeSet
4628 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4629 GtkStyleContext *context,
4630 GtkStateFlags flags)
4635 gtk_style_context_get_background_color (context, flags, &color);
4636 value = g_strdup_printf ("%u,%u,%u",
4637 (guint) ceil (color.red * 65536 - color.red),
4638 (guint) ceil (color.green * 65536 - color.green),
4639 (guint) ceil (color.blue * 65536 - color.blue));
4640 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4643 gtk_style_context_get_color (context, flags, &color);
4644 value = g_strdup_printf ("%u,%u,%u",
4645 (guint) ceil (color.red * 65536 - color.red),
4646 (guint) ceil (color.green * 65536 - color.green),
4647 (guint) ceil (color.blue * 65536 - color.blue));
4648 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);