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;
365 GtkWidgetPath *widget_path;
366 GHashTable *style_data;
368 StyleData *current_data;
369 GtkStateFlags current_state;
371 GSList *animation_regions;
374 GtkThemingEngine *theming_engine;
376 GtkTextDirection direction;
378 guint animations_invalidated : 1;
379 guint invalidating_context : 1;
394 static guint signals[LAST_SIGNAL] = { 0 };
396 static void gtk_style_context_finalize (GObject *object);
398 static void gtk_style_context_impl_set_property (GObject *object,
402 static void gtk_style_context_impl_get_property (GObject *object,
406 static GtkSymbolicColor *
407 gtk_style_context_color_lookup_func (gpointer contextp,
411 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
414 gtk_style_context_real_changed (GtkStyleContext *context)
416 GtkStyleContextPrivate *priv = context->priv;
419 _gtk_widget_style_context_invalidated (priv->widget);
423 gtk_style_context_class_init (GtkStyleContextClass *klass)
425 GObjectClass *object_class = G_OBJECT_CLASS (klass);
427 object_class->finalize = gtk_style_context_finalize;
428 object_class->set_property = gtk_style_context_impl_set_property;
429 object_class->get_property = gtk_style_context_impl_get_property;
431 klass->changed = gtk_style_context_real_changed;
434 g_signal_new (I_("changed"),
435 G_TYPE_FROM_CLASS (object_class),
437 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
439 g_cclosure_marshal_VOID__VOID,
442 g_object_class_install_property (object_class,
444 g_param_spec_object ("screen",
446 P_("The associated GdkScreen"),
448 GTK_PARAM_READWRITE));
449 g_object_class_install_property (object_class,
451 g_param_spec_enum ("direction",
453 P_("Text direction"),
454 GTK_TYPE_TEXT_DIRECTION,
456 GTK_PARAM_READWRITE));
458 * GtkStyleContext:parent:
460 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
465 g_object_class_install_property (object_class,
467 g_param_spec_object ("parent",
469 P_("The parent style context"),
470 GTK_TYPE_STYLE_CONTEXT,
471 GTK_PARAM_READWRITE));
473 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
476 static GtkStyleInfo *
477 style_info_new (void)
481 info = g_slice_new0 (GtkStyleInfo);
482 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
483 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
489 style_info_free (GtkStyleInfo *info)
491 g_array_free (info->style_classes, TRUE);
492 g_array_free (info->regions, TRUE);
493 g_slice_free (GtkStyleInfo, info);
496 static GtkStyleInfo *
497 style_info_copy (const GtkStyleInfo *info)
501 copy = style_info_new ();
502 g_array_insert_vals (copy->style_classes, 0,
503 info->style_classes->data,
504 info->style_classes->len);
506 g_array_insert_vals (copy->regions, 0,
510 copy->junction_sides = info->junction_sides;
511 copy->state_flags = info->state_flags;
517 style_info_hash (gconstpointer elem)
519 const GtkStyleInfo *info;
524 for (i = 0; i < info->style_classes->len; i++)
526 hash += g_array_index (info->style_classes, GQuark, i);
530 for (i = 0; i < info->regions->len; i++)
534 region = &g_array_index (info->regions, GtkRegion, i);
535 hash += region->class_quark;
536 hash += region->flags;
540 return hash ^ info->state_flags;
544 style_info_equal (gconstpointer elem1,
547 const GtkStyleInfo *info1, *info2;
552 if (info1->state_flags != info2->state_flags)
555 if (info1->junction_sides != info2->junction_sides)
558 if (info1->style_classes->len != info2->style_classes->len)
561 if (memcmp (info1->style_classes->data,
562 info2->style_classes->data,
563 info1->style_classes->len * sizeof (GQuark)) != 0)
566 if (info1->regions->len != info2->regions->len)
569 if (memcmp (info1->regions->data,
570 info2->regions->data,
571 info1->regions->len * sizeof (GtkRegion)) != 0)
578 style_data_new (void)
582 data = g_slice_new0 (StyleData);
588 clear_property_cache (StyleData *data)
592 if (!data->property_cache)
595 for (i = 0; i < data->property_cache->len; i++)
597 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
599 g_param_spec_unref (node->pspec);
600 g_value_unset (&node->value);
603 g_array_free (data->property_cache, TRUE);
604 data->property_cache = NULL;
608 style_data_free (StyleData *data)
610 g_object_unref (data->store);
611 clear_property_cache (data);
613 g_slice_free (StyleData, data);
617 gtk_style_context_init (GtkStyleContext *style_context)
619 GtkStyleContextPrivate *priv;
622 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
623 GTK_TYPE_STYLE_CONTEXT,
624 GtkStyleContextPrivate);
626 priv->style_data = g_hash_table_new_full (style_info_hash,
628 (GDestroyNotify) style_info_free,
629 (GDestroyNotify) style_data_free);
630 priv->theming_engine = g_object_ref ((gpointer) gtk_theming_engine_load (NULL));
632 priv->direction = GTK_TEXT_DIR_LTR;
634 priv->screen = gdk_screen_get_default ();
635 priv->cascade = _gtk_style_cascade_get_for_screen (priv->screen);
636 g_object_ref (priv->cascade);
638 /* Create default info store */
639 info = style_info_new ();
640 priv->info_stack = g_slist_prepend (priv->info_stack, info);
644 animation_info_free (AnimationInfo *info)
646 g_object_unref (info->timeline);
647 g_object_unref (info->window);
649 if (info->invalidation_region)
650 cairo_region_destroy (info->invalidation_region);
652 g_array_free (info->rectangles, TRUE);
653 g_slist_free (info->parent_regions);
654 g_slice_free (AnimationInfo, info);
657 static AnimationInfo *
658 animation_info_lookup_by_timeline (GtkStyleContext *context,
659 GtkTimeline *timeline)
661 GtkStyleContextPrivate *priv;
665 priv = context->priv;
667 for (l = priv->animations; l; l = l->next)
671 if (info->timeline == timeline)
679 timeline_frame_cb (GtkTimeline *timeline,
683 GtkStyleContextPrivate *priv;
684 GtkStyleContext *context;
688 priv = context->priv;
689 info = animation_info_lookup_by_timeline (context, timeline);
691 g_assert (info != NULL);
693 /* Cancel transition if window is gone */
694 if (gdk_window_is_destroyed (info->window) ||
695 !gdk_window_is_visible (info->window))
697 priv->animations = g_slist_remove (priv->animations, info);
698 animation_info_free (info);
702 if (info->invalidation_region &&
703 !cairo_region_is_empty (info->invalidation_region))
704 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
706 gdk_window_invalidate_rect (info->window, NULL, TRUE);
710 timeline_finished_cb (GtkTimeline *timeline,
713 GtkStyleContextPrivate *priv;
714 GtkStyleContext *context;
718 priv = context->priv;
719 info = animation_info_lookup_by_timeline (context, timeline);
721 g_assert (info != NULL);
723 priv->animations = g_slist_remove (priv->animations, info);
725 /* Invalidate one last time the area, so the final content is painted */
726 if (info->invalidation_region &&
727 !cairo_region_is_empty (info->invalidation_region))
728 gdk_window_invalidate_region (info->window, info->invalidation_region, TRUE);
730 gdk_window_invalidate_rect (info->window, NULL, TRUE);
732 animation_info_free (info);
735 static AnimationInfo *
736 animation_info_new (GtkStyleContext *context,
739 GtkTimelineProgressType progress_type,
742 gboolean target_value,
747 info = g_slice_new0 (AnimationInfo);
749 info->rectangles = g_array_new (FALSE, FALSE, sizeof (cairo_rectangle_int_t));
750 info->timeline = _gtk_timeline_new (duration);
751 info->window = g_object_ref (window);
753 info->target_value = target_value;
754 info->region_id = region_id;
756 _gtk_timeline_set_progress_type (info->timeline, progress_type);
757 _gtk_timeline_set_loop (info->timeline, loop);
759 if (!loop && !target_value)
761 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
762 _gtk_timeline_rewind (info->timeline);
765 g_signal_connect (info->timeline, "frame",
766 G_CALLBACK (timeline_frame_cb), context);
767 g_signal_connect (info->timeline, "finished",
768 G_CALLBACK (timeline_finished_cb), context);
770 _gtk_timeline_start (info->timeline);
775 static AnimationInfo *
776 animation_info_lookup (GtkStyleContext *context,
780 GtkStyleContextPrivate *priv;
783 priv = context->priv;
785 for (l = priv->animations; l; l = l->next)
791 if (info->state == state &&
792 info->region_id == region_id)
800 gtk_style_context_finalize (GObject *object)
802 GtkStyleContextPrivate *priv;
803 GtkStyleContext *style_context;
806 style_context = GTK_STYLE_CONTEXT (object);
807 priv = style_context->priv;
809 gtk_style_context_set_parent (style_context, NULL);
811 if (priv->widget_path)
812 gtk_widget_path_free (priv->widget_path);
814 g_hash_table_destroy (priv->style_data);
816 g_object_unref (priv->cascade);
818 g_slist_free_full (priv->info_stack, (GDestroyNotify) style_info_free);
820 g_slist_free (priv->animation_regions);
822 for (l = priv->animations; l; l = l->next)
823 animation_info_free ((AnimationInfo *) l->data);
825 g_slist_free (priv->animations);
827 if (priv->theming_engine)
828 g_object_unref (priv->theming_engine);
830 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
834 gtk_style_context_impl_set_property (GObject *object,
839 GtkStyleContext *style_context;
841 style_context = GTK_STYLE_CONTEXT (object);
846 gtk_style_context_set_screen (style_context,
847 g_value_get_object (value));
850 gtk_style_context_set_direction (style_context,
851 g_value_get_enum (value));
854 gtk_style_context_set_parent (style_context,
855 g_value_get_object (value));
858 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
864 gtk_style_context_impl_get_property (GObject *object,
869 GtkStyleContext *style_context;
870 GtkStyleContextPrivate *priv;
872 style_context = GTK_STYLE_CONTEXT (object);
873 priv = style_context->priv;
878 g_value_set_object (value, priv->screen);
881 g_value_set_enum (value, priv->direction);
884 g_value_set_object (value, priv->parent);
887 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
893 build_properties (GtkStyleContext *context,
894 StyleData *style_data,
898 GtkStyleContextPrivate *priv;
899 GtkCssMatcher matcher;
900 GtkCssLookup *lookup;
902 priv = context->priv;
904 _gtk_css_matcher_init (&matcher, path, state);
905 lookup = _gtk_css_lookup_new ();
907 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
911 style_data->store = _gtk_css_computed_values_new ();
912 _gtk_css_lookup_resolve (lookup, context, style_data->store);
913 _gtk_css_lookup_free (lookup);
916 static GtkWidgetPath *
917 create_query_path (GtkStyleContext *context)
919 GtkStyleContextPrivate *priv;
924 priv = context->priv;
925 path = gtk_widget_path_copy (priv->widget ? gtk_widget_get_path (priv->widget) : priv->widget_path);
926 pos = gtk_widget_path_length (path) - 1;
928 info = priv->info_stack->data;
930 /* Set widget regions */
931 for (i = 0; i < info->regions->len; i++)
935 region = &g_array_index (info->regions, GtkRegion, i);
936 gtk_widget_path_iter_add_region (path, pos,
937 g_quark_to_string (region->class_quark),
941 /* Set widget classes */
942 for (i = 0; i < info->style_classes->len; i++)
946 quark = g_array_index (info->style_classes, GQuark, i);
947 gtk_widget_path_iter_add_class (path, pos,
948 g_quark_to_string (quark));
955 style_data_lookup (GtkStyleContext *context,
958 GtkStyleContextPrivate *priv;
960 gboolean state_mismatch;
963 priv = context->priv;
964 state_mismatch = ((GtkStyleInfo *) priv->info_stack->data)->state_flags != state;
966 /* Current data in use is cached, just return it */
967 if (priv->current_data && priv->current_state == state)
968 return priv->current_data;
970 g_assert (priv->widget != NULL || priv->widget_path != NULL);
972 if (G_UNLIKELY (state_mismatch))
974 gtk_style_context_save (context);
975 gtk_style_context_set_state (context, state);
978 priv->current_data = g_hash_table_lookup (priv->style_data, priv->info_stack->data);
979 priv->current_state = state;
981 if (!priv->current_data)
985 path = create_query_path (context);
987 priv->current_data = style_data_new ();
988 g_hash_table_insert (priv->style_data,
989 style_info_copy (priv->info_stack->data),
992 build_properties (context, priv->current_data, path, state);
994 gtk_widget_path_free (path);
997 data = priv->current_data;
999 if (priv->theming_engine)
1000 g_object_unref (priv->theming_engine);
1002 v = _gtk_css_computed_values_get_value_by_name (priv->current_data->store, "engine");
1004 priv->theming_engine = _gtk_css_value_dup_object (v);
1006 priv->theming_engine = g_object_ref (gtk_theming_engine_load (NULL));
1008 if (G_UNLIKELY (state_mismatch))
1009 gtk_style_context_restore (context);
1015 * gtk_style_context_new:
1017 * Creates a standalone #GtkStyleContext, this style context
1018 * won't be attached to any widget, so you may want
1019 * to call gtk_style_context_set_path() yourself.
1022 * This function is only useful when using the theming layer
1023 * separated from GTK+, if you are using #GtkStyleContext to
1024 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1025 * in order to get a style context ready to theme the widget.
1028 * Returns: A newly created #GtkStyleContext.
1031 gtk_style_context_new (void)
1033 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1037 _gtk_style_context_set_widget (GtkStyleContext *context,
1040 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1041 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1043 context->priv->widget = widget;
1047 * gtk_style_context_add_provider:
1048 * @context: a #GtkStyleContext
1049 * @provider: a #GtkStyleProvider
1050 * @priority: the priority of the style provider. The lower
1051 * it is, the earlier it will be used in the style
1052 * construction. Typically this will be in the range
1053 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1054 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1056 * Adds a style provider to @context, to be used in style construction.
1058 * <note><para>If both priorities are the same, A #GtkStyleProvider
1059 * added through this function takes precedence over another added
1060 * through gtk_style_context_add_provider_for_screen().</para></note>
1065 gtk_style_context_add_provider (GtkStyleContext *context,
1066 GtkStyleProvider *provider,
1069 GtkStyleContextPrivate *priv;
1071 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1072 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1074 priv = context->priv;
1076 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1078 GtkStyleCascade *new_cascade;
1080 new_cascade = _gtk_style_cascade_new ();
1081 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1082 g_object_unref (priv->cascade);
1083 priv->cascade = new_cascade;
1086 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1088 gtk_style_context_invalidate (context);
1092 * gtk_style_context_remove_provider:
1093 * @context: a #GtkStyleContext
1094 * @provider: a #GtkStyleProvider
1096 * Removes @provider from the style providers list in @context.
1101 gtk_style_context_remove_provider (GtkStyleContext *context,
1102 GtkStyleProvider *provider)
1104 GtkStyleContextPrivate *priv;
1106 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1107 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1109 priv = context->priv;
1111 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1114 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1118 * gtk_style_context_reset_widgets:
1119 * @screen: a #GdkScreen
1121 * This function recomputes the styles for all widgets under a particular
1122 * #GdkScreen. This is useful when some global parameter has changed that
1123 * affects the appearance of all widgets, because when a widget gets a new
1124 * style, it will both redraw and recompute any cached information about
1125 * its appearance. As an example, it is used when the color scheme changes
1126 * in the related #GtkSettings object.
1131 gtk_style_context_reset_widgets (GdkScreen *screen)
1133 GList *list, *toplevels;
1135 _gtk_icon_set_invalidate_caches ();
1137 toplevels = gtk_window_list_toplevels ();
1138 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1140 for (list = toplevels; list; list = list->next)
1142 if (gtk_widget_get_screen (list->data) == screen)
1143 gtk_widget_reset_style (list->data);
1145 g_object_unref (list->data);
1148 g_list_free (toplevels);
1152 * gtk_style_context_add_provider_for_screen:
1153 * @screen: a #GdkScreen
1154 * @provider: a #GtkStyleProvider
1155 * @priority: the priority of the style provider. The lower
1156 * it is, the earlier it will be used in the style
1157 * construction. Typically this will be in the range
1158 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1159 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1161 * Adds a global style provider to @screen, which will be used
1162 * in style construction for all #GtkStyleContext<!-- -->s under
1165 * GTK+ uses this to make styling information from #GtkSettings
1168 * <note><para>If both priorities are the same, A #GtkStyleProvider
1169 * added through gtk_style_context_add_provider() takes precedence
1170 * over another added through this function.</para></note>
1175 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1176 GtkStyleProvider *provider,
1179 GtkStyleCascade *cascade;
1181 g_return_if_fail (GDK_IS_SCREEN (screen));
1182 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1184 cascade = _gtk_style_cascade_get_for_screen (screen);
1185 _gtk_style_cascade_add_provider (cascade, provider, priority);
1189 * gtk_style_context_remove_provider_for_screen:
1190 * @screen: a #GdkScreen
1191 * @provider: a #GtkStyleProvider
1193 * Removes @provider from the global style providers list in @screen.
1198 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1199 GtkStyleProvider *provider)
1201 GtkStyleCascade *cascade;
1203 g_return_if_fail (GDK_IS_SCREEN (screen));
1204 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1206 cascade = _gtk_style_cascade_get_for_screen (screen);
1207 _gtk_style_cascade_remove_provider (cascade, provider);
1211 * gtk_style_context_get_section:
1212 * @context: a #GtkStyleContext
1213 * @property: style property name
1215 * Queries the location in the CSS where @property was defined for the
1216 * current @context. Note that the state to be queried is taken from
1217 * gtk_style_context_get_state().
1219 * If the location is not available, %NULL will be returned. The
1220 * location might not be available for various reasons, such as the
1221 * property being overridden, @property not naming a supported CSS
1222 * property or tracking of definitions being disabled for performance
1225 * Shorthand CSS properties cannot be queried for a location and will
1226 * always return %NULL.
1228 * Returns: %NULL or the section where value was defined
1231 gtk_style_context_get_section (GtkStyleContext *context,
1232 const gchar *property)
1234 GtkStyleContextPrivate *priv;
1235 GtkStyleProperty *prop;
1238 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1239 g_return_val_if_fail (property != NULL, NULL);
1241 priv = context->priv;
1242 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1244 prop = _gtk_style_property_lookup (property);
1245 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1248 data = style_data_lookup (context, gtk_style_context_get_state (context));
1249 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1252 static GtkCssValue *
1253 gtk_style_context_query_func (guint id,
1256 return _gtk_css_computed_values_get_value (values, id);
1260 * gtk_style_context_get_property:
1261 * @context: a #GtkStyleContext
1262 * @property: style property name
1263 * @state: state to retrieve the property value for
1264 * @value: (out) (transfer full): return location for the style property value
1266 * Gets a style property from @context for the given state.
1268 * When @value is no longer needed, g_value_unset() must be called
1269 * to free any allocated memory.
1274 gtk_style_context_get_property (GtkStyleContext *context,
1275 const gchar *property,
1276 GtkStateFlags state,
1279 GtkStyleContextPrivate *priv;
1280 GtkStyleProperty *prop;
1284 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1285 g_return_if_fail (property != NULL);
1286 g_return_if_fail (value != NULL);
1288 priv = context->priv;
1289 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1291 prop = _gtk_style_property_lookup (property);
1294 g_warning ("Style property \"%s\" is not registered", property);
1297 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1299 g_warning ("Style property \"%s\" is not gettable", property);
1303 data = style_data_lookup (context, state);
1304 v = _gtk_style_property_query (prop, gtk_style_context_query_func, data->store);
1305 _gtk_css_value_init_gvalue (v, value);
1306 _gtk_css_value_unref (v);
1310 * gtk_style_context_get_valist:
1311 * @context: a #GtkStyleContext
1312 * @state: state to retrieve the property values for
1313 * @args: va_list of property name/return location pairs, followed by %NULL
1315 * Retrieves several style property values from @context for a given state.
1320 gtk_style_context_get_valist (GtkStyleContext *context,
1321 GtkStateFlags state,
1324 const gchar *property_name;
1326 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1328 property_name = va_arg (args, const gchar *);
1330 while (property_name)
1332 gchar *error = NULL;
1333 GValue value = G_VALUE_INIT;
1335 gtk_style_context_get_property (context,
1340 G_VALUE_LCOPY (&value, args, 0, &error);
1341 g_value_unset (&value);
1345 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1350 property_name = va_arg (args, const gchar *);
1355 * gtk_style_context_get:
1356 * @context: a #GtkStyleContext
1357 * @state: state to retrieve the property values for
1358 * @...: property name /return value pairs, followed by %NULL
1360 * Retrieves several style property values from @context for a
1366 gtk_style_context_get (GtkStyleContext *context,
1367 GtkStateFlags state,
1372 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1374 va_start (args, state);
1375 gtk_style_context_get_valist (context, state, args);
1380 * gtk_style_context_set_state:
1381 * @context: a #GtkStyleContext
1382 * @flags: state to represent
1384 * Sets the state to be used when rendering with any
1385 * of the gtk_render_*() functions.
1390 gtk_style_context_set_state (GtkStyleContext *context,
1391 GtkStateFlags flags)
1393 GtkStyleContextPrivate *priv;
1396 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1398 priv = context->priv;
1399 info = priv->info_stack->data;
1400 info->state_flags = flags;
1404 * gtk_style_context_get_state:
1405 * @context: a #GtkStyleContext
1407 * Returns the state used when rendering.
1409 * Returns: the state flags
1414 gtk_style_context_get_state (GtkStyleContext *context)
1416 GtkStyleContextPrivate *priv;
1419 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1421 priv = context->priv;
1422 info = priv->info_stack->data;
1424 return info->state_flags;
1428 context_has_animatable_region (GtkStyleContext *context,
1431 GtkStyleContextPrivate *priv;
1433 /* NULL region_id means everything
1434 * rendered through the style context
1439 priv = context->priv;
1440 return g_slist_find (priv->animation_regions, region_id) != NULL;
1444 * gtk_style_context_state_is_running:
1445 * @context: a #GtkStyleContext
1446 * @state: a widget state
1447 * @progress: (out): return location for the transition progress
1449 * Returns %TRUE if there is a transition animation running for the
1450 * current region (see gtk_style_context_push_animatable_region()).
1452 * If @progress is not %NULL, the animation progress will be returned
1453 * there, 0.0 means the state is closest to being unset, while 1.0 means
1454 * it's closest to being set. This means transition animation will
1455 * run from 0 to 1 when @state is being set and from 1 to 0 when
1458 * Returns: %TRUE if there is a running transition animation for @state.
1463 gtk_style_context_state_is_running (GtkStyleContext *context,
1467 GtkStyleContextPrivate *priv;
1468 AnimationInfo *info;
1471 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1473 priv = context->priv;
1475 for (l = priv->animations; l; l = l->next)
1479 if (info->state == state &&
1480 context_has_animatable_region (context, info->region_id))
1483 *progress = _gtk_timeline_get_progress (info->timeline);
1493 * gtk_style_context_set_path:
1494 * @context: a #GtkStyleContext
1495 * @path: a #GtkWidgetPath
1497 * Sets the #GtkWidgetPath used for style matching. As a
1498 * consequence, the style will be regenerated to match
1499 * the new given path.
1501 * If you are using a #GtkStyleContext returned from
1502 * gtk_widget_get_style_context(), you do not need to call
1508 gtk_style_context_set_path (GtkStyleContext *context,
1509 GtkWidgetPath *path)
1511 GtkStyleContextPrivate *priv;
1513 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1514 g_return_if_fail (path != NULL);
1516 priv = context->priv;
1517 g_return_if_fail (priv->widget == NULL);
1519 if (priv->widget_path)
1521 gtk_widget_path_free (priv->widget_path);
1522 priv->widget_path = NULL;
1526 priv->widget_path = gtk_widget_path_copy (path);
1528 gtk_style_context_invalidate (context);
1532 * gtk_style_context_get_path:
1533 * @context: a #GtkStyleContext
1535 * Returns the widget path used for style matching.
1537 * Returns: (transfer none): A #GtkWidgetPath
1541 const GtkWidgetPath *
1542 gtk_style_context_get_path (GtkStyleContext *context)
1544 GtkStyleContextPrivate *priv;
1546 priv = context->priv;
1548 return gtk_widget_get_path (priv->widget);
1550 return priv->widget_path;
1554 * gtk_style_context_set_parent:
1555 * @context: a #GtkStyleContext
1556 * @parent: (allow-none): the new parent or %NULL
1558 * Sets the parent style context for @context. The parent style
1559 * context is used to implement
1560 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1563 * If you are using a #GtkStyleContext returned from
1564 * gtk_widget_get_style_context(), the parent will be set for you.
1569 gtk_style_context_set_parent (GtkStyleContext *context,
1570 GtkStyleContext *parent)
1572 GtkStyleContextPrivate *priv;
1574 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1575 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1577 priv = context->priv;
1579 if (priv->parent == parent)
1583 g_object_ref (parent);
1586 g_object_unref (priv->parent);
1588 priv->parent = parent;
1590 g_object_notify (G_OBJECT (context), "parent");
1591 gtk_style_context_invalidate (context);
1595 * gtk_style_context_get_parent:
1596 * @context: a #GtkStyleContext
1598 * Gets the parent context set via gtk_style_context_set_parent().
1599 * See that function for details.
1601 * Returns: (transfer none): the parent context or %NULL
1606 gtk_style_context_get_parent (GtkStyleContext *context)
1608 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1610 return context->priv->parent;
1614 * gtk_style_context_save:
1615 * @context: a #GtkStyleContext
1617 * Saves the @context state, so all modifications done through
1618 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1619 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1620 * or gtk_style_context_set_junction_sides() can be reverted in one
1621 * go through gtk_style_context_restore().
1626 gtk_style_context_save (GtkStyleContext *context)
1628 GtkStyleContextPrivate *priv;
1631 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1633 priv = context->priv;
1635 g_assert (priv->info_stack != NULL);
1637 info = style_info_copy (priv->info_stack->data);
1638 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1642 * gtk_style_context_restore:
1643 * @context: a #GtkStyleContext
1645 * Restores @context state to a previous stage.
1646 * See gtk_style_context_save().
1651 gtk_style_context_restore (GtkStyleContext *context)
1653 GtkStyleContextPrivate *priv;
1656 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1658 priv = context->priv;
1660 if (priv->info_stack)
1662 info = priv->info_stack->data;
1663 priv->info_stack = g_slist_remove (priv->info_stack, info);
1664 style_info_free (info);
1667 if (!priv->info_stack)
1669 g_warning ("Unpaired gtk_style_context_restore() call");
1671 /* Create default region */
1672 info = style_info_new ();
1673 priv->info_stack = g_slist_prepend (priv->info_stack, info);
1676 priv->current_data = NULL;
1680 style_class_find (GArray *array,
1685 gboolean found = FALSE;
1691 if (!array || array->len == 0)
1695 max = array->len - 1;
1701 mid = (min + max) / 2;
1702 item = g_array_index (array, GQuark, mid);
1704 if (class_quark == item)
1709 else if (class_quark > item)
1710 min = pos = mid + 1;
1717 while (!found && min <= max);
1726 region_find (GArray *array,
1731 gboolean found = FALSE;
1737 if (!array || array->len == 0)
1741 max = array->len - 1;
1747 mid = (min + max) / 2;
1748 region = &g_array_index (array, GtkRegion, mid);
1750 if (region->class_quark == class_quark)
1755 else if (region->class_quark > class_quark)
1756 min = pos = mid + 1;
1763 while (!found && min <= max);
1772 * gtk_style_context_add_class:
1773 * @context: a #GtkStyleContext
1774 * @class_name: class name to use in styling
1776 * Adds a style class to @context, so posterior calls to
1777 * gtk_style_context_get() or any of the gtk_render_*()
1778 * functions will make use of this new class for styling.
1780 * In the CSS file format, a #GtkEntry defining an "entry"
1781 * class, would be matched by:
1784 * GtkEntry.entry { ... }
1787 * While any widget defining an "entry" class would be
1796 gtk_style_context_add_class (GtkStyleContext *context,
1797 const gchar *class_name)
1799 GtkStyleContextPrivate *priv;
1804 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1805 g_return_if_fail (class_name != NULL);
1807 priv = context->priv;
1808 class_quark = g_quark_from_string (class_name);
1810 g_assert (priv->info_stack != NULL);
1811 info = priv->info_stack->data;
1813 if (!style_class_find (info->style_classes, class_quark, &position))
1815 g_array_insert_val (info->style_classes, position, class_quark);
1817 /* Unset current data, as it likely changed due to the class change */
1818 priv->current_data = NULL;
1823 * gtk_style_context_remove_class:
1824 * @context: a #GtkStyleContext
1825 * @class_name: class name to remove
1827 * Removes @class_name from @context.
1832 gtk_style_context_remove_class (GtkStyleContext *context,
1833 const gchar *class_name)
1835 GtkStyleContextPrivate *priv;
1840 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1841 g_return_if_fail (class_name != NULL);
1843 class_quark = g_quark_try_string (class_name);
1848 priv = context->priv;
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_remove_index (info->style_classes, position);
1857 /* Unset current data, as it likely changed due to the class change */
1858 priv->current_data = NULL;
1863 * gtk_style_context_has_class:
1864 * @context: a #GtkStyleContext
1865 * @class_name: a class name
1867 * Returns %TRUE if @context currently has defined the
1870 * Returns: %TRUE if @context has @class_name defined
1875 gtk_style_context_has_class (GtkStyleContext *context,
1876 const gchar *class_name)
1878 GtkStyleContextPrivate *priv;
1882 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1883 g_return_val_if_fail (class_name != NULL, FALSE);
1885 class_quark = g_quark_try_string (class_name);
1890 priv = context->priv;
1892 g_assert (priv->info_stack != NULL);
1893 info = priv->info_stack->data;
1895 if (style_class_find (info->style_classes, class_quark, NULL))
1902 * gtk_style_context_list_classes:
1903 * @context: a #GtkStyleContext
1905 * Returns the list of classes currently defined in @context.
1907 * Returns: (transfer container) (element-type utf8): a #GList of
1908 * strings with the currently defined classes. The contents
1909 * of the list are owned by GTK+, but you must free the list
1910 * itself with g_list_free() when you are done with it.
1915 gtk_style_context_list_classes (GtkStyleContext *context)
1917 GtkStyleContextPrivate *priv;
1919 GList *classes = NULL;
1922 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1924 priv = context->priv;
1926 g_assert (priv->info_stack != NULL);
1927 info = priv->info_stack->data;
1929 for (i = 0; i < info->style_classes->len; i++)
1933 quark = g_array_index (info->style_classes, GQuark, i);
1934 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1941 * gtk_style_context_list_regions:
1942 * @context: a #GtkStyleContext
1944 * Returns the list of regions currently defined in @context.
1946 * Returns: (transfer container) (element-type utf8): a #GList of
1947 * strings with the currently defined regions. The contents
1948 * of the list are owned by GTK+, but you must free the list
1949 * itself with g_list_free() when you are done with it.
1954 gtk_style_context_list_regions (GtkStyleContext *context)
1956 GtkStyleContextPrivate *priv;
1958 GList *classes = NULL;
1961 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1963 priv = context->priv;
1965 g_assert (priv->info_stack != NULL);
1966 info = priv->info_stack->data;
1968 for (i = 0; i < info->regions->len; i++)
1971 const gchar *class_name;
1973 region = &g_array_index (info->regions, GtkRegion, i);
1975 class_name = g_quark_to_string (region->class_quark);
1976 classes = g_list_prepend (classes, (gchar *) class_name);
1983 _gtk_style_context_check_region_name (const gchar *str)
1985 g_return_val_if_fail (str != NULL, FALSE);
1987 if (!g_ascii_islower (str[0]))
1993 !g_ascii_islower (*str))
2003 * gtk_style_context_add_region:
2004 * @context: a #GtkStyleContext
2005 * @region_name: region name to use in styling
2006 * @flags: flags that apply to the region
2008 * Adds a region to @context, so posterior calls to
2009 * gtk_style_context_get() or any of the gtk_render_*()
2010 * functions will make use of this new region for styling.
2012 * In the CSS file format, a #GtkTreeView defining a "row"
2013 * region, would be matched by:
2016 * GtkTreeView row { ... }
2019 * Pseudo-classes are used for matching @flags, so the two
2022 * GtkTreeView row:nth-child(even) { ... }
2023 * GtkTreeView row:nth-child(odd) { ... }
2026 * would apply to even and odd rows, respectively.
2028 * <note><para>Region names must only contain lowercase letters
2029 * and '-', starting always with a lowercase letter.</para></note>
2034 gtk_style_context_add_region (GtkStyleContext *context,
2035 const gchar *region_name,
2036 GtkRegionFlags flags)
2038 GtkStyleContextPrivate *priv;
2040 GQuark region_quark;
2043 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2044 g_return_if_fail (region_name != NULL);
2045 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2047 priv = context->priv;
2048 region_quark = g_quark_from_string (region_name);
2050 g_assert (priv->info_stack != NULL);
2051 info = priv->info_stack->data;
2053 if (!region_find (info->regions, region_quark, &position))
2057 region.class_quark = region_quark;
2058 region.flags = flags;
2060 g_array_insert_val (info->regions, position, region);
2062 /* Unset current data, as it likely changed due to the region change */
2063 priv->current_data = NULL;
2068 * gtk_style_context_remove_region:
2069 * @context: a #GtkStyleContext
2070 * @region_name: region name to unset
2072 * Removes a region from @context.
2077 gtk_style_context_remove_region (GtkStyleContext *context,
2078 const gchar *region_name)
2080 GtkStyleContextPrivate *priv;
2082 GQuark region_quark;
2085 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2086 g_return_if_fail (region_name != NULL);
2088 region_quark = g_quark_try_string (region_name);
2093 priv = context->priv;
2095 g_assert (priv->info_stack != NULL);
2096 info = priv->info_stack->data;
2098 if (region_find (info->regions, region_quark, &position))
2100 g_array_remove_index (info->regions, position);
2102 /* Unset current data, as it likely changed due to the region change */
2103 priv->current_data = NULL;
2108 * gtk_style_context_has_region:
2109 * @context: a #GtkStyleContext
2110 * @region_name: a region name
2111 * @flags_return: (out) (allow-none): return location for region flags
2113 * Returns %TRUE if @context has the region defined.
2114 * If @flags_return is not %NULL, it is set to the flags
2115 * affecting the region.
2117 * Returns: %TRUE if region is defined
2122 gtk_style_context_has_region (GtkStyleContext *context,
2123 const gchar *region_name,
2124 GtkRegionFlags *flags_return)
2126 GtkStyleContextPrivate *priv;
2128 GQuark region_quark;
2131 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2132 g_return_val_if_fail (region_name != NULL, FALSE);
2137 region_quark = g_quark_try_string (region_name);
2142 priv = context->priv;
2144 g_assert (priv->info_stack != NULL);
2145 info = priv->info_stack->data;
2147 if (region_find (info->regions, region_quark, &position))
2153 region = &g_array_index (info->regions, GtkRegion, position);
2154 *flags_return = region->flags;
2163 style_property_values_cmp (gconstpointer bsearch_node1,
2164 gconstpointer bsearch_node2)
2166 const PropertyValue *val1 = bsearch_node1;
2167 const PropertyValue *val2 = bsearch_node2;
2169 if (val1->widget_type != val2->widget_type)
2170 return val1->widget_type < val2->widget_type ? -1 : 1;
2172 if (val1->pspec != val2->pspec)
2173 return val1->pspec < val2->pspec ? -1 : 1;
2175 if (val1->state != val2->state)
2176 return val1->state < val2->state ? -1 : 1;
2182 _gtk_style_context_peek_property (GtkStyleContext *context,
2183 const char *property_name)
2185 StyleData *data = style_data_lookup (context, gtk_style_context_get_state (context));
2187 return _gtk_css_computed_values_get_value_by_name (data->store, property_name);
2191 _gtk_style_context_get_number (GtkStyleContext *context,
2192 const char *property_name,
2193 double one_hundred_percent)
2197 value = _gtk_style_context_peek_property (context, property_name);
2198 return _gtk_css_number_get (_gtk_css_value_get_number (value), one_hundred_percent);
2202 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2204 GtkStateFlags state,
2207 GtkStyleContextPrivate *priv;
2208 PropertyValue *pcache, key = { 0 };
2212 priv = context->priv;
2213 data = style_data_lookup (context, state);
2215 key.widget_type = widget_type;
2219 /* need value cache array */
2220 if (!data->property_cache)
2221 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2224 pcache = bsearch (&key,
2225 data->property_cache->data, data->property_cache->len,
2226 sizeof (PropertyValue), style_property_values_cmp);
2228 return &pcache->value;
2232 while (i < data->property_cache->len &&
2233 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2236 g_array_insert_val (data->property_cache, i, key);
2237 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2239 /* cache miss, initialize value type, then set contents */
2240 g_param_spec_ref (pcache->pspec);
2241 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2243 if (priv->widget || priv->widget_path)
2245 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2246 priv->widget ? gtk_widget_get_path (priv->widget)
2247 : priv->widget_path,
2248 state, pspec, &pcache->value))
2250 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2251 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2253 GtkSymbolicColor *color;
2256 color = g_value_dup_boxed (&pcache->value);
2258 g_value_unset (&pcache->value);
2260 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2261 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2263 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2265 if (_gtk_style_context_resolve_color (context, color, &rgba))
2267 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2268 g_value_set_boxed (&pcache->value, &rgba);
2273 rgb.red = rgba.red * 65535. + 0.5;
2274 rgb.green = rgba.green * 65535. + 0.5;
2275 rgb.blue = rgba.blue * 65535. + 0.5;
2277 g_value_set_boxed (&pcache->value, &rgb);
2281 g_param_value_set_default (pspec, &pcache->value);
2283 gtk_symbolic_color_unref (color);
2286 return &pcache->value;
2290 /* not supplied by any provider, revert to default */
2291 g_param_value_set_default (pspec, &pcache->value);
2293 return &pcache->value;
2297 * gtk_style_context_get_style_property:
2298 * @context: a #GtkStyleContext
2299 * @property_name: the name of the widget style property
2300 * @value: Return location for the property value
2302 * Gets the value for a widget style property.
2304 * When @value is no longer needed, g_value_unset() must be called
2305 * to free any allocated memory.
2308 gtk_style_context_get_style_property (GtkStyleContext *context,
2309 const gchar *property_name,
2312 GtkStyleContextPrivate *priv;
2313 GtkWidgetClass *widget_class;
2314 GtkStateFlags state;
2316 const GValue *peek_value;
2319 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2320 g_return_if_fail (property_name != NULL);
2321 g_return_if_fail (value != NULL);
2323 priv = context->priv;
2327 widget_type = G_OBJECT_TYPE (priv->widget);
2331 if (!priv->widget_path)
2334 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2336 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2338 g_warning ("%s: can't get style properties for non-widget class `%s'",
2340 g_type_name (widget_type));
2345 widget_class = g_type_class_ref (widget_type);
2346 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2347 g_type_class_unref (widget_class);
2351 g_warning ("%s: widget class `%s' has no style property named `%s'",
2353 g_type_name (widget_type),
2358 state = gtk_style_context_get_state (context);
2359 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2362 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2363 g_value_copy (peek_value, value);
2364 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2365 g_value_transform (peek_value, value);
2367 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2369 G_VALUE_TYPE_NAME (peek_value),
2370 G_VALUE_TYPE_NAME (value));
2374 * gtk_style_context_get_style_valist:
2375 * @context: a #GtkStyleContext
2376 * @args: va_list of property name/return location pairs, followed by %NULL
2378 * Retrieves several widget style properties from @context according to the
2384 gtk_style_context_get_style_valist (GtkStyleContext *context,
2387 GtkStyleContextPrivate *priv;
2388 const gchar *prop_name;
2389 GtkStateFlags state;
2392 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2394 prop_name = va_arg (args, const gchar *);
2395 priv = context->priv;
2399 widget_type = G_OBJECT_TYPE (priv->widget);
2403 if (!priv->widget_path)
2406 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2408 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2410 g_warning ("%s: can't get style properties for non-widget class `%s'",
2412 g_type_name (widget_type));
2417 state = gtk_style_context_get_state (context);
2421 GtkWidgetClass *widget_class;
2423 const GValue *peek_value;
2426 widget_class = g_type_class_ref (widget_type);
2427 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2428 g_type_class_unref (widget_class);
2432 g_warning ("%s: widget class `%s' has no style property named `%s'",
2434 g_type_name (widget_type),
2439 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2442 G_VALUE_LCOPY (peek_value, args, 0, &error);
2446 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2448 G_VALUE_TYPE_NAME (peek_value),
2453 prop_name = va_arg (args, const gchar *);
2458 * gtk_style_context_get_style:
2459 * @context: a #GtkStyleContext
2460 * @...: property name /return value pairs, followed by %NULL
2462 * Retrieves several widget style properties from @context according to the
2468 gtk_style_context_get_style (GtkStyleContext *context,
2473 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2475 va_start (args, context);
2476 gtk_style_context_get_style_valist (context, args);
2482 * gtk_style_context_lookup_icon_set:
2483 * @context: a #GtkStyleContext
2484 * @stock_id: an icon name
2486 * Looks up @stock_id in the icon factories associated to @context and
2487 * the default icon factory, returning an icon set if found, otherwise
2490 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2493 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2494 const gchar *stock_id)
2496 GtkStyleContextPrivate *priv;
2498 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2499 g_return_val_if_fail (stock_id != NULL, NULL);
2501 priv = context->priv;
2502 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2504 return gtk_icon_factory_lookup_default (stock_id);
2508 * gtk_style_context_set_screen:
2509 * @context: a #GtkStyleContext
2510 * @screen: a #GdkScreen
2512 * Attaches @context to the given screen.
2514 * The screen is used to add style information from 'global' style
2515 * providers, such as the screens #GtkSettings instance.
2517 * If you are using a #GtkStyleContext returned from
2518 * gtk_widget_get_style_context(), you do not need to
2519 * call this yourself.
2524 gtk_style_context_set_screen (GtkStyleContext *context,
2527 GtkStyleContextPrivate *priv;
2529 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2530 g_return_if_fail (GDK_IS_SCREEN (screen));
2532 priv = context->priv;
2533 if (priv->screen == screen)
2536 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2538 g_object_unref (priv->cascade);
2539 priv->cascade = _gtk_style_cascade_get_for_screen (screen);
2540 g_object_ref (priv->cascade);
2544 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2547 priv->screen = screen;
2549 g_object_notify (G_OBJECT (context), "screen");
2551 gtk_style_context_invalidate (context);
2555 * gtk_style_context_get_screen:
2556 * @context: a #GtkStyleContext
2558 * Returns the #GdkScreen to which @context is attached.
2560 * Returns: (transfer none): a #GdkScreen.
2563 gtk_style_context_get_screen (GtkStyleContext *context)
2565 GtkStyleContextPrivate *priv;
2567 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2569 priv = context->priv;
2570 return priv->screen;
2574 * gtk_style_context_set_direction:
2575 * @context: a #GtkStyleContext
2576 * @direction: the new direction.
2578 * Sets the reading direction for rendering purposes.
2580 * If you are using a #GtkStyleContext returned from
2581 * gtk_widget_get_style_context(), you do not need to
2582 * call this yourself.
2587 gtk_style_context_set_direction (GtkStyleContext *context,
2588 GtkTextDirection direction)
2590 GtkStyleContextPrivate *priv;
2592 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2594 priv = context->priv;
2595 priv->direction = direction;
2597 g_object_notify (G_OBJECT (context), "direction");
2601 * gtk_style_context_get_direction:
2602 * @context: a #GtkStyleContext
2604 * Returns the widget direction used for rendering.
2606 * Returns: the widget direction
2611 gtk_style_context_get_direction (GtkStyleContext *context)
2613 GtkStyleContextPrivate *priv;
2615 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2617 priv = context->priv;
2618 return priv->direction;
2622 * gtk_style_context_set_junction_sides:
2623 * @context: a #GtkStyleContext
2624 * @sides: sides where rendered elements are visually connected to
2627 * Sets the sides where rendered elements (mostly through
2628 * gtk_render_frame()) will visually connect with other visual elements.
2630 * This is merely a hint that may or may not be honored
2631 * by theming engines.
2633 * Container widgets are expected to set junction hints as appropriate
2634 * for their children, so it should not normally be necessary to call
2635 * this function manually.
2640 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2641 GtkJunctionSides sides)
2643 GtkStyleContextPrivate *priv;
2646 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2648 priv = context->priv;
2649 info = priv->info_stack->data;
2650 info->junction_sides = sides;
2654 * gtk_style_context_get_junction_sides:
2655 * @context: a #GtkStyleContext
2657 * Returns the sides where rendered elements connect visually with others.
2659 * Returns: the junction sides
2664 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2666 GtkStyleContextPrivate *priv;
2669 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2671 priv = context->priv;
2672 info = priv->info_stack->data;
2673 return info->junction_sides;
2676 static GtkSymbolicColor *
2677 gtk_style_context_color_lookup_func (gpointer contextp,
2680 GtkStyleContext *context = contextp;
2682 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2686 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2687 GtkSymbolicColor *color)
2689 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2690 g_return_val_if_fail (color != NULL, FALSE);
2692 return _gtk_symbolic_color_resolve_full (color,
2693 gtk_style_context_color_lookup_func,
2699 _gtk_style_context_resolve_color (GtkStyleContext *context,
2700 GtkSymbolicColor *color,
2705 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2706 g_return_val_if_fail (color != NULL, FALSE);
2707 g_return_val_if_fail (result != NULL, FALSE);
2709 val = _gtk_symbolic_color_resolve_full (color,
2710 gtk_style_context_color_lookup_func,
2715 *result = *_gtk_css_value_get_rgba (val);
2716 _gtk_css_value_unref (val);
2721 * gtk_style_context_lookup_color:
2722 * @context: a #GtkStyleContext
2723 * @color_name: color name to lookup
2724 * @color: (out): Return location for the looked up color
2726 * Looks up and resolves a color name in the @context color map.
2728 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2731 gtk_style_context_lookup_color (GtkStyleContext *context,
2732 const gchar *color_name,
2735 GtkSymbolicColor *sym_color;
2737 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2738 g_return_val_if_fail (color_name != NULL, FALSE);
2739 g_return_val_if_fail (color != NULL, FALSE);
2741 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2742 if (sym_color == NULL)
2745 return _gtk_style_context_resolve_color (context, sym_color, color);
2749 * gtk_style_context_notify_state_change:
2750 * @context: a #GtkStyleContext
2751 * @window: a #GdkWindow
2752 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2753 * See gtk_style_context_push_animatable_region()
2754 * @state: state to trigger transition for
2755 * @state_value: %TRUE if @state is the state we are changing to,
2756 * %FALSE if we are changing away from it
2758 * Notifies a state change on @context, so if the current style makes use
2759 * of transition animations, one will be started so all rendered elements
2760 * under @region_id are animated for state @state being set to value
2763 * The @window parameter is used in order to invalidate the rendered area
2764 * as the animation runs, so make sure it is the same window that is being
2765 * rendered on by the gtk_render_*() functions.
2767 * If @region_id is %NULL, all rendered elements using @context will be
2768 * affected by this state transition.
2770 * As a practical example, a #GtkButton notifying a state transition on
2771 * the prelight state:
2773 * gtk_style_context_notify_state_change (context,
2774 * gtk_widget_get_window (widget),
2776 * GTK_STATE_PRELIGHT,
2777 * button->in_button);
2780 * Can be handled in the CSS file like this:
2783 * background-color: #f00
2787 * background-color: #fff;
2788 * transition: 200ms linear
2792 * This combination will animate the button background from red to white
2793 * if a pointer enters the button, and back to red if the pointer leaves
2796 * Note that @state is used when finding the transition parameters, which
2797 * is why the style places the transition under the :hover pseudo-class.
2802 gtk_style_context_notify_state_change (GtkStyleContext *context,
2806 gboolean state_value)
2808 GtkStyleContextPrivate *priv;
2809 GtkAnimationDescription *desc;
2810 AnimationInfo *info;
2811 GtkStateFlags flags;
2815 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2816 g_return_if_fail (GDK_IS_WINDOW (window));
2817 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2819 priv = context->priv;
2820 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
2822 state_value = (state_value == TRUE);
2826 case GTK_STATE_ACTIVE:
2827 flags = GTK_STATE_FLAG_ACTIVE;
2829 case GTK_STATE_PRELIGHT:
2830 flags = GTK_STATE_FLAG_PRELIGHT;
2832 case GTK_STATE_SELECTED:
2833 flags = GTK_STATE_FLAG_SELECTED;
2835 case GTK_STATE_INSENSITIVE:
2836 flags = GTK_STATE_FLAG_INSENSITIVE;
2838 case GTK_STATE_INCONSISTENT:
2839 flags = GTK_STATE_FLAG_INCONSISTENT;
2841 case GTK_STATE_FOCUSED:
2842 flags = GTK_STATE_FLAG_FOCUSED;
2844 case GTK_STATE_NORMAL:
2850 /* Find out if there is any animation description for the given
2851 * state, it will fallback to the normal state as well if necessary.
2853 data = style_data_lookup (context, flags);
2854 v = _gtk_css_computed_values_get_value_by_name (data->store, "transition");
2857 desc = _gtk_css_value_get_boxed (v);
2861 if (_gtk_animation_description_get_duration (desc) == 0)
2864 info = animation_info_lookup (context, region_id, state);
2867 info->target_value != state_value)
2869 /* Target values are the opposite */
2870 if (!_gtk_timeline_get_loop (info->timeline))
2872 /* Reverse the animation */
2873 if (_gtk_timeline_get_direction (info->timeline) == GTK_TIMELINE_DIRECTION_FORWARD)
2874 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_BACKWARD);
2876 _gtk_timeline_set_direction (info->timeline, GTK_TIMELINE_DIRECTION_FORWARD);
2878 info->target_value = state_value;
2882 /* Take it out of its looping state */
2883 _gtk_timeline_set_loop (info->timeline, FALSE);
2887 (!_gtk_animation_description_get_loop (desc) ||
2890 info = animation_info_new (context, region_id,
2891 _gtk_animation_description_get_duration (desc),
2892 _gtk_animation_description_get_progress_type (desc),
2893 _gtk_animation_description_get_loop (desc),
2894 state, state_value, window);
2896 priv->animations = g_slist_prepend (priv->animations, info);
2897 priv->animations_invalidated = TRUE;
2902 * gtk_style_context_cancel_animations:
2903 * @context: a #GtkStyleContext
2904 * @region_id: (allow-none): animatable region to stop, or %NULL.
2905 * See gtk_style_context_push_animatable_region()
2907 * Stops all running animations for @region_id and all animatable
2908 * regions underneath.
2910 * A %NULL @region_id will stop all ongoing animations in @context,
2911 * when dealing with a #GtkStyleContext obtained through
2912 * gtk_widget_get_style_context(), this is normally done for you
2913 * in all circumstances you would expect all widget to be stopped,
2914 * so this should be only used in complex widgets with different
2915 * animatable regions.
2920 gtk_style_context_cancel_animations (GtkStyleContext *context,
2923 GtkStyleContextPrivate *priv;
2924 AnimationInfo *info;
2927 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2929 priv = context->priv;
2930 l = priv->animations;
2938 info->region_id == region_id ||
2939 g_slist_find (info->parent_regions, region_id))
2941 priv->animations = g_slist_remove (priv->animations, info);
2942 animation_info_free (info);
2948 is_parent_of (GdkWindow *parent,
2951 GtkWidget *child_widget, *parent_widget;
2954 gdk_window_get_user_data (child, (gpointer *) &child_widget);
2955 gdk_window_get_user_data (parent, (gpointer *) &parent_widget);
2957 if (child_widget != parent_widget &&
2958 !gtk_widget_is_ancestor (child_widget, parent_widget))
2965 if (window == parent)
2968 window = gdk_window_get_parent (window);
2975 * gtk_style_context_scroll_animations:
2976 * @context: a #GtkStyleContext
2977 * @window: a #GdkWindow used previously in
2978 * gtk_style_context_notify_state_change()
2979 * @dx: Amount to scroll in the X axis
2980 * @dy: Amount to scroll in the Y axis
2982 * This function is analogous to gdk_window_scroll(), and
2983 * should be called together with it so the invalidation
2984 * areas for any ongoing animation are scrolled together
2990 gtk_style_context_scroll_animations (GtkStyleContext *context,
2995 GtkStyleContextPrivate *priv;
2996 AnimationInfo *info;
2999 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3000 g_return_if_fail (GDK_IS_WINDOW (window));
3002 priv = context->priv;
3003 l = priv->animations;
3010 if (info->invalidation_region &&
3011 (window == info->window ||
3012 is_parent_of (window, info->window)))
3013 cairo_region_translate (info->invalidation_region, dx, dy);
3018 * gtk_style_context_push_animatable_region:
3019 * @context: a #GtkStyleContext
3020 * @region_id: unique identifier for the animatable region
3022 * Pushes an animatable region, so all further gtk_render_*() calls between
3023 * this call and the following gtk_style_context_pop_animatable_region()
3024 * will potentially show transition animations for this region if
3025 * gtk_style_context_notify_state_change() is called for a given state,
3026 * and the current theme/style defines transition animations for state
3029 * The @region_id used must be unique in @context so the theming engine
3030 * can uniquely identify rendered elements subject to a state transition.
3035 gtk_style_context_push_animatable_region (GtkStyleContext *context,
3038 GtkStyleContextPrivate *priv;
3040 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3041 g_return_if_fail (region_id != NULL);
3043 priv = context->priv;
3044 priv->animation_regions = g_slist_prepend (priv->animation_regions, region_id);
3048 * gtk_style_context_pop_animatable_region:
3049 * @context: a #GtkStyleContext
3051 * Pops an animatable region from @context.
3052 * See gtk_style_context_push_animatable_region().
3057 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
3059 GtkStyleContextPrivate *priv;
3061 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3063 priv = context->priv;
3064 priv->animation_regions = g_slist_delete_link (priv->animation_regions,
3065 priv->animation_regions);
3069 _gtk_style_context_invalidate_animation_areas (GtkStyleContext *context)
3071 GtkStyleContextPrivate *priv;
3074 priv = context->priv;
3076 for (l = priv->animations; l; l = l->next)
3078 AnimationInfo *info;
3082 /* A NULL invalidation region means it has to be recreated on
3083 * the next expose event, this happens usually after a widget
3084 * allocation change, so the next expose after it will update
3085 * the invalidation region.
3087 if (info->invalidation_region)
3089 cairo_region_destroy (info->invalidation_region);
3090 info->invalidation_region = NULL;
3094 priv->animations_invalidated = TRUE;
3098 _gtk_style_context_coalesce_animation_areas (GtkStyleContext *context,
3101 GtkStyleContextPrivate *priv;
3104 priv = context->priv;
3106 if (!priv->animations_invalidated)
3109 l = priv->animations;
3113 AnimationInfo *info;
3122 if (info->invalidation_region)
3125 if (info->rectangles->len == 0)
3128 info->invalidation_region = cairo_region_create ();
3129 _gtk_widget_get_translation_to_window (widget, info->window, &rel_x, &rel_y);
3131 for (i = 0; i < info->rectangles->len; i++)
3133 cairo_rectangle_int_t *rect;
3135 rect = &g_array_index (info->rectangles, cairo_rectangle_int_t, i);
3137 /* These are widget relative coordinates,
3138 * so have them inverted to be window relative
3143 cairo_region_union_rectangle (info->invalidation_region, rect);
3146 g_array_remove_range (info->rectangles, 0, info->rectangles->len);
3149 priv->animations_invalidated = FALSE;
3153 store_animation_region (GtkStyleContext *context,
3159 GtkStyleContextPrivate *priv;
3162 priv = context->priv;
3164 if (!priv->animations_invalidated)
3167 for (l = priv->animations; l; l = l->next)
3169 AnimationInfo *info;
3173 /* The animation doesn't need updating
3174 * the invalidation area, bail out.
3176 if (info->invalidation_region)
3179 if (context_has_animatable_region (context, info->region_id))
3181 cairo_rectangle_int_t rect;
3185 rect.width = (gint) width;
3186 rect.height = (gint) height;
3188 g_array_append_val (info->rectangles, rect);
3190 if (!info->parent_regions)
3192 GSList *parent_regions;
3194 parent_regions = g_slist_find (priv->animation_regions, info->region_id);
3195 info->parent_regions = g_slist_copy (parent_regions);
3202 * gtk_style_context_invalidate:
3203 * @context: a #GtkStyleContext.
3205 * Invalidates @context style information, so it will be reconstructed
3208 * If you're using a #GtkStyleContext returned from
3209 * gtk_widget_get_style_context(), you do not need to
3210 * call this yourself.
3215 gtk_style_context_invalidate (GtkStyleContext *context)
3217 GtkStyleContextPrivate *priv;
3219 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3221 priv = context->priv;
3223 /* Avoid reentrancy */
3224 if (priv->invalidating_context)
3227 priv->invalidating_context = TRUE;
3229 g_hash_table_remove_all (priv->style_data);
3230 priv->current_data = NULL;
3232 g_signal_emit (context, signals[CHANGED], 0);
3234 priv->invalidating_context = FALSE;
3238 * gtk_style_context_set_background:
3239 * @context: a #GtkStyleContext
3240 * @window: a #GdkWindow
3242 * Sets the background of @window to the background pattern or
3243 * color specified in @context for its current state.
3248 gtk_style_context_set_background (GtkStyleContext *context,
3251 GtkStateFlags state;
3252 cairo_pattern_t *pattern;
3255 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3256 g_return_if_fail (GDK_IS_WINDOW (window));
3258 state = gtk_style_context_get_state (context);
3259 gtk_style_context_get (context, state,
3260 "background-image", &pattern,
3264 gdk_window_set_background_pattern (window, pattern);
3265 cairo_pattern_destroy (pattern);
3269 gtk_style_context_get (context, state,
3270 "background-color", &color,
3274 gdk_window_set_background_rgba (window, color);
3275 gdk_rgba_free (color);
3280 * gtk_style_context_get_color:
3281 * @context: a #GtkStyleContext
3282 * @state: state to retrieve the color for
3283 * @color: (out): return value for the foreground color
3285 * Gets the foreground color for a given state.
3290 gtk_style_context_get_color (GtkStyleContext *context,
3291 GtkStateFlags state,
3296 g_return_if_fail (color != NULL);
3297 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3299 gtk_style_context_get (context,
3309 * gtk_style_context_get_background_color:
3310 * @context: a #GtkStyleContext
3311 * @state: state to retrieve the color for
3312 * @color: (out): return value for the background color
3314 * Gets the background color for a given state.
3319 gtk_style_context_get_background_color (GtkStyleContext *context,
3320 GtkStateFlags state,
3325 g_return_if_fail (color != NULL);
3326 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3328 gtk_style_context_get (context,
3330 "background-color", &c,
3338 * gtk_style_context_get_border_color:
3339 * @context: a #GtkStyleContext
3340 * @state: state to retrieve the color for
3341 * @color: (out): return value for the border color
3343 * Gets the border color for a given state.
3348 gtk_style_context_get_border_color (GtkStyleContext *context,
3349 GtkStateFlags state,
3354 g_return_if_fail (color != NULL);
3355 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3357 gtk_style_context_get (context,
3367 * gtk_style_context_get_border:
3368 * @context: a #GtkStyleContext
3369 * @state: state to retrieve the border for
3370 * @border: (out): return value for the border settings
3372 * Gets the border for a given state as a #GtkBorder.
3373 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3378 gtk_style_context_get_border (GtkStyleContext *context,
3379 GtkStateFlags state,
3382 int top, left, bottom, right;
3384 g_return_if_fail (border != NULL);
3385 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3387 gtk_style_context_get (context,
3389 "border-top-width", &top,
3390 "border-left-width", &left,
3391 "border-bottom-width", &bottom,
3392 "border-right-width", &right,
3396 border->left = left;
3397 border->bottom = bottom;
3398 border->right = right;
3402 * gtk_style_context_get_padding:
3403 * @context: a #GtkStyleContext
3404 * @state: state to retrieve the padding for
3405 * @padding: (out): return value for the padding settings
3407 * Gets the padding for a given state as a #GtkBorder.
3408 * See %GTK_STYLE_PROPERTY_PADDING.
3413 gtk_style_context_get_padding (GtkStyleContext *context,
3414 GtkStateFlags state,
3417 int top, left, bottom, right;
3419 g_return_if_fail (padding != NULL);
3420 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3422 gtk_style_context_get (context,
3424 "padding-top", &top,
3425 "padding-left", &left,
3426 "padding-bottom", &bottom,
3427 "padding-right", &right,
3431 padding->left = left;
3432 padding->bottom = bottom;
3433 padding->right = right;
3437 * gtk_style_context_get_margin:
3438 * @context: a #GtkStyleContext
3439 * @state: state to retrieve the border for
3440 * @margin: (out): return value for the margin settings
3442 * Gets the margin for a given state as a #GtkBorder.
3443 * See %GTK_STYLE_PROPERTY_MARGIN.
3448 gtk_style_context_get_margin (GtkStyleContext *context,
3449 GtkStateFlags state,
3452 int top, left, bottom, right;
3454 g_return_if_fail (margin != NULL);
3455 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3457 gtk_style_context_get (context,
3460 "margin-left", &left,
3461 "margin-bottom", &bottom,
3462 "margin-right", &right,
3466 margin->left = left;
3467 margin->bottom = bottom;
3468 margin->right = right;
3472 * gtk_style_context_get_font:
3473 * @context: a #GtkStyleContext
3474 * @state: state to retrieve the font for
3476 * Returns the font description for a given state. The returned
3477 * object is const and will remain valid until the
3478 * #GtkStyleContext::changed signal happens.
3480 * Returns: (transfer none): the #PangoFontDescription for the given
3481 * state. This object is owned by GTK+ and should not be
3486 const PangoFontDescription *
3487 gtk_style_context_get_font (GtkStyleContext *context,
3488 GtkStateFlags state)
3490 GtkStyleContextPrivate *priv;
3492 PangoFontDescription *description;
3494 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3496 priv = context->priv;
3497 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3499 data = style_data_lookup (context, state);
3501 /* Yuck, fonts are created on-demand but we don't return a ref.
3502 * Do bad things to achieve this requirement */
3503 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3504 if (description == NULL)
3506 gtk_style_context_get (context, state, "font", &description, NULL);
3507 g_object_set_data_full (G_OBJECT (data->store),
3508 "font-cache-for-get_font",
3510 (GDestroyNotify) pango_font_description_free);
3516 get_cursor_color (GtkStyleContext *context,
3520 GdkColor *style_color;
3522 gtk_style_context_get_style (context,
3523 primary ? "cursor-color" : "secondary-cursor-color",
3529 color->red = style_color->red / 65535.0;
3530 color->green = style_color->green / 65535.0;
3531 color->blue = style_color->blue / 65535.0;
3534 gdk_color_free (style_color);
3538 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3544 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3546 color->red = (color->red + bg.red) * 0.5;
3547 color->green = (color->green + bg.green) * 0.5;
3548 color->blue = (color->blue + bg.blue) * 0.5;
3554 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3555 GdkRGBA *primary_color,
3556 GdkRGBA *secondary_color)
3559 get_cursor_color (context, TRUE, primary_color);
3561 if (secondary_color)
3562 get_cursor_color (context, FALSE, secondary_color);
3569 * @context: a #GtkStyleContext
3571 * @x: X origin of the rectangle
3572 * @y: Y origin of the rectangle
3573 * @width: rectangle width
3574 * @height: rectangle height
3576 * Renders a checkmark (as in a #GtkCheckButton).
3578 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3579 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3580 * should be marked as undefined.
3583 * <title>Typical checkmark rendering</title>
3584 * <inlinegraphic fileref="checks.png" format="PNG"/>
3590 gtk_render_check (GtkStyleContext *context,
3597 GtkStyleContextPrivate *priv;
3598 GtkThemingEngineClass *engine_class;
3600 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3601 g_return_if_fail (cr != NULL);
3603 if (width <= 0 || height <= 0)
3606 priv = context->priv;
3607 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3611 store_animation_region (context, x, y, width, height);
3613 _gtk_theming_engine_set_context (priv->theming_engine, context);
3614 engine_class->render_check (priv->theming_engine, cr,
3615 x, y, width, height);
3621 * gtk_render_option:
3622 * @context: a #GtkStyleContext
3624 * @x: X origin of the rectangle
3625 * @y: Y origin of the rectangle
3626 * @width: rectangle width
3627 * @height: rectangle height
3629 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3630 * state will determine whether the option is on or off, and
3631 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3634 * <title>Typical option mark rendering</title>
3635 * <inlinegraphic fileref="options.png" format="PNG"/>
3641 gtk_render_option (GtkStyleContext *context,
3648 GtkStyleContextPrivate *priv;
3649 GtkThemingEngineClass *engine_class;
3651 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3652 g_return_if_fail (cr != NULL);
3654 if (width <= 0 || height <= 0)
3657 priv = context->priv;
3658 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3662 store_animation_region (context, x, y, width, height);
3664 _gtk_theming_engine_set_context (priv->theming_engine, context);
3665 engine_class->render_option (priv->theming_engine, cr,
3666 x, y, width, height);
3673 * @context: a #GtkStyleContext
3675 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3676 * @x: X origin of the render area
3677 * @y: Y origin of the render area
3678 * @size: square side for render area
3680 * Renders an arrow pointing to @angle.
3683 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3684 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3690 gtk_render_arrow (GtkStyleContext *context,
3697 GtkStyleContextPrivate *priv;
3698 GtkThemingEngineClass *engine_class;
3700 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3701 g_return_if_fail (cr != NULL);
3706 priv = context->priv;
3707 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3711 gtk_style_context_save (context);
3712 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3714 store_animation_region (context, x, y, size, size);
3716 _gtk_theming_engine_set_context (priv->theming_engine, context);
3717 engine_class->render_arrow (priv->theming_engine, cr,
3720 gtk_style_context_restore (context);
3725 * gtk_render_background:
3726 * @context: a #GtkStyleContext
3728 * @x: X origin of the rectangle
3729 * @y: Y origin of the rectangle
3730 * @width: rectangle width
3731 * @height: rectangle height
3733 * Renders the background of an element.
3736 * <title>Typical background rendering, showing the effect of
3737 * <parameter>background-image</parameter>,
3738 * <parameter>border-width</parameter> and
3739 * <parameter>border-radius</parameter></title>
3740 * <inlinegraphic fileref="background.png" format="PNG"/>
3746 gtk_render_background (GtkStyleContext *context,
3753 GtkStyleContextPrivate *priv;
3754 GtkThemingEngineClass *engine_class;
3756 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3757 g_return_if_fail (cr != NULL);
3759 if (width <= 0 || height <= 0)
3762 priv = context->priv;
3763 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3767 store_animation_region (context, x, y, width, height);
3769 _gtk_theming_engine_set_context (priv->theming_engine, context);
3770 engine_class->render_background (priv->theming_engine, cr, x, y, width, height);
3777 * @context: a #GtkStyleContext
3779 * @x: X origin of the rectangle
3780 * @y: Y origin of the rectangle
3781 * @width: rectangle width
3782 * @height: rectangle height
3784 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3787 * <title>Examples of frame rendering, showing the effect of
3788 * <parameter>border-image</parameter>,
3789 * <parameter>border-color</parameter>,
3790 * <parameter>border-width</parameter>,
3791 * <parameter>border-radius</parameter> and
3793 * <inlinegraphic fileref="frames.png" format="PNG"/>
3799 gtk_render_frame (GtkStyleContext *context,
3806 GtkStyleContextPrivate *priv;
3807 GtkThemingEngineClass *engine_class;
3809 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3810 g_return_if_fail (cr != NULL);
3812 if (width <= 0 || height <= 0)
3815 priv = context->priv;
3816 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3820 store_animation_region (context, x, y, width, height);
3822 _gtk_theming_engine_set_context (priv->theming_engine, context);
3823 engine_class->render_frame (priv->theming_engine, cr, x, y, width, height);
3829 * gtk_render_expander:
3830 * @context: a #GtkStyleContext
3832 * @x: X origin of the rectangle
3833 * @y: Y origin of the rectangle
3834 * @width: rectangle width
3835 * @height: rectangle height
3837 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3838 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3839 * determines whether the expander is collapsed or expanded.
3842 * <title>Typical expander rendering</title>
3843 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3849 gtk_render_expander (GtkStyleContext *context,
3856 GtkStyleContextPrivate *priv;
3857 GtkThemingEngineClass *engine_class;
3859 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3860 g_return_if_fail (cr != NULL);
3862 if (width <= 0 || height <= 0)
3865 priv = context->priv;
3866 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3870 store_animation_region (context, x, y, width, height);
3872 _gtk_theming_engine_set_context (priv->theming_engine, context);
3873 engine_class->render_expander (priv->theming_engine, cr, x, y, width, height);
3880 * @context: a #GtkStyleContext
3882 * @x: X origin of the rectangle
3883 * @y: Y origin of the rectangle
3884 * @width: rectangle width
3885 * @height: rectangle height
3887 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3889 * <title>Typical focus rendering</title>
3890 * <inlinegraphic fileref="focus.png" format="PNG"/>
3896 gtk_render_focus (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_focus (priv->theming_engine, cr, x, y, width, height);
3926 * gtk_render_layout:
3927 * @context: a #GtkStyleContext
3931 * @layout: the #PangoLayout to render
3933 * Renders @layout on the coordinates @x, @y
3938 gtk_render_layout (GtkStyleContext *context,
3942 PangoLayout *layout)
3944 GtkStyleContextPrivate *priv;
3945 GtkThemingEngineClass *engine_class;
3946 PangoRectangle extents;
3948 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3949 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3950 g_return_if_fail (cr != NULL);
3952 priv = context->priv;
3953 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
3957 pango_layout_get_extents (layout, &extents, NULL);
3959 store_animation_region (context,
3965 _gtk_theming_engine_set_context (priv->theming_engine, context);
3966 engine_class->render_layout (priv->theming_engine, cr, x, y, layout);
3973 * @context: a #GtkStyleContext
3975 * @x0: X coordinate for the origin of the line
3976 * @y0: Y coordinate for the origin of the line
3977 * @x1: X coordinate for the end of the line
3978 * @y1: Y coordinate for the end of the line
3980 * Renders a line from (x0, y0) to (x1, y1).
3985 gtk_render_line (GtkStyleContext *context,
3992 GtkStyleContextPrivate *priv;
3993 GtkThemingEngineClass *engine_class;
3995 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3996 g_return_if_fail (cr != NULL);
3998 priv = context->priv;
3999 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4003 _gtk_theming_engine_set_context (priv->theming_engine, context);
4004 engine_class->render_line (priv->theming_engine, cr, x0, y0, x1, y1);
4010 * gtk_render_slider:
4011 * @context: a #GtkStyleContext
4013 * @x: X origin of the rectangle
4014 * @y: Y origin of the rectangle
4015 * @width: rectangle width
4016 * @height: rectangle height
4017 * @orientation: orientation of the slider
4019 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4020 * @width, @height. @orientation defines whether the slider is vertical
4024 * <title>Typical slider rendering</title>
4025 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4031 gtk_render_slider (GtkStyleContext *context,
4037 GtkOrientation orientation)
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 if (width <= 0 || height <= 0)
4048 priv = context->priv;
4049 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4053 store_animation_region (context, x, y, width, height);
4055 _gtk_theming_engine_set_context (priv->theming_engine, context);
4056 engine_class->render_slider (priv->theming_engine, cr, x, y, width, height, orientation);
4062 * gtk_render_frame_gap:
4063 * @context: a #GtkStyleContext
4065 * @x: X origin of the rectangle
4066 * @y: Y origin of the rectangle
4067 * @width: rectangle width
4068 * @height: rectangle height
4069 * @gap_side: side where the gap is
4070 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4071 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4073 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4074 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4075 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4076 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4079 * <title>Typical rendering of a frame with a gap</title>
4080 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4086 gtk_render_frame_gap (GtkStyleContext *context,
4092 GtkPositionType gap_side,
4096 GtkStyleContextPrivate *priv;
4097 GtkThemingEngineClass *engine_class;
4099 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4100 g_return_if_fail (cr != NULL);
4101 g_return_if_fail (xy0_gap <= xy1_gap);
4102 g_return_if_fail (xy0_gap >= 0);
4104 if (width <= 0 || height <= 0)
4107 if (gap_side == GTK_POS_LEFT ||
4108 gap_side == GTK_POS_RIGHT)
4109 g_return_if_fail (xy1_gap <= height);
4111 g_return_if_fail (xy1_gap <= width);
4113 priv = context->priv;
4114 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4118 store_animation_region (context, x, y, width, height);
4120 _gtk_theming_engine_set_context (priv->theming_engine, context);
4121 engine_class->render_frame_gap (priv->theming_engine, cr,
4122 x, y, width, height, gap_side,
4129 * gtk_render_extension:
4130 * @context: a #GtkStyleContext
4132 * @x: X origin of the rectangle
4133 * @y: Y origin of the rectangle
4134 * @width: rectangle width
4135 * @height: rectangle height
4136 * @gap_side: side where the gap is
4138 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4139 * defined by @x, @y, @width, @height. The side where the extension
4140 * connects to is defined by @gap_side.
4143 * <title>Typical extension rendering</title>
4144 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4150 gtk_render_extension (GtkStyleContext *context,
4156 GtkPositionType gap_side)
4158 GtkStyleContextPrivate *priv;
4159 GtkThemingEngineClass *engine_class;
4161 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4162 g_return_if_fail (cr != NULL);
4164 if (width <= 0 || height <= 0)
4167 priv = context->priv;
4168 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4172 store_animation_region (context, x, y, width, height);
4174 _gtk_theming_engine_set_context (priv->theming_engine, context);
4175 engine_class->render_extension (priv->theming_engine, cr, x, y, width, height, gap_side);
4181 * gtk_render_handle:
4182 * @context: a #GtkStyleContext
4184 * @x: X origin of the rectangle
4185 * @y: Y origin of the rectangle
4186 * @width: rectangle width
4187 * @height: rectangle height
4189 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4190 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4191 * determined by @x, @y, @width, @height.
4194 * <title>Handles rendered for the paned and grip classes</title>
4195 * <inlinegraphic fileref="handles.png" format="PNG"/>
4201 gtk_render_handle (GtkStyleContext *context,
4208 GtkStyleContextPrivate *priv;
4209 GtkThemingEngineClass *engine_class;
4211 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4212 g_return_if_fail (cr != NULL);
4214 if (width <= 0 || height <= 0)
4217 priv = context->priv;
4218 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4222 store_animation_region (context, x, y, width, height);
4224 _gtk_theming_engine_set_context (priv->theming_engine, context);
4225 engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
4231 * gtk_render_activity:
4232 * @context: a #GtkStyleContext
4234 * @x: X origin of the rectangle
4235 * @y: Y origin of the rectangle
4236 * @width: rectangle width
4237 * @height: rectangle height
4239 * Renders an activity area (Such as in #GtkSpinner or the
4240 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4241 * determines whether there is activity going on.
4246 gtk_render_activity (GtkStyleContext *context,
4253 GtkStyleContextPrivate *priv;
4254 GtkThemingEngineClass *engine_class;
4256 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4257 g_return_if_fail (cr != NULL);
4259 if (width <= 0 || height <= 0)
4262 priv = context->priv;
4263 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4267 store_animation_region (context, x, y, width, height);
4269 _gtk_theming_engine_set_context (priv->theming_engine, context);
4270 engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
4276 * gtk_render_icon_pixbuf:
4277 * @context: a #GtkStyleContext
4278 * @source: the #GtkIconSource specifying the icon to render
4279 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4280 * means render at the size of the source and don't scale.
4282 * Renders the icon specified by @source at the given @size, returning the result
4285 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4290 gtk_render_icon_pixbuf (GtkStyleContext *context,
4291 const GtkIconSource *source,
4294 GtkStyleContextPrivate *priv;
4295 GtkThemingEngineClass *engine_class;
4297 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4298 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4299 g_return_val_if_fail (source != NULL, NULL);
4301 priv = context->priv;
4302 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4304 _gtk_theming_engine_set_context (priv->theming_engine, context);
4305 return engine_class->render_icon_pixbuf (priv->theming_engine, source, size);
4310 * @context: a #GtkStyleContext
4312 * @pixbuf: a #GdkPixbuf containing the icon to draw
4313 * @x: X position for the @pixbuf
4314 * @y: Y position for the @pixbuf
4316 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4321 gtk_render_icon (GtkStyleContext *context,
4327 GtkStyleContextPrivate *priv;
4328 GtkThemingEngineClass *engine_class;
4330 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4331 g_return_if_fail (cr != NULL);
4333 priv = context->priv;
4334 engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
4338 store_animation_region (context,
4340 gdk_pixbuf_get_width (pixbuf),
4341 gdk_pixbuf_get_height (pixbuf));
4343 _gtk_theming_engine_set_context (priv->theming_engine, context);
4344 engine_class->render_icon (priv->theming_engine, cr, pixbuf, x, y);
4350 draw_insertion_cursor (GtkStyleContext *context,
4355 gboolean is_primary,
4356 PangoDirection direction,
4357 gboolean draw_arrow)
4360 GdkRGBA primary_color;
4361 GdkRGBA secondary_color;
4362 gfloat cursor_aspect_ratio;
4368 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4369 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4371 /* When changing the shape or size of the cursor here,
4372 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4375 gtk_style_context_get_style (context,
4376 "cursor-aspect-ratio", &cursor_aspect_ratio,
4379 stem_width = height * cursor_aspect_ratio + 1;
4381 /* put (stem_width % 2) on the proper side of the cursor */
4382 if (direction == PANGO_DIRECTION_LTR)
4383 offset = stem_width / 2;
4385 offset = stem_width - stem_width / 2;
4387 cairo_rectangle (cr, x - offset, y, stem_width, height);
4395 arrow_width = stem_width + 1;
4397 if (direction == PANGO_DIRECTION_RTL)
4399 ax = x - offset - 1;
4400 ay = y + height - arrow_width * 2 - arrow_width + 1;
4402 cairo_move_to (cr, ax, ay + 1);
4403 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4404 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4407 else if (direction == PANGO_DIRECTION_LTR)
4409 ax = x + stem_width - offset;
4410 ay = y + height - arrow_width * 2 - arrow_width + 1;
4412 cairo_move_to (cr, ax, ay + 1);
4413 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4414 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4418 g_assert_not_reached();
4425 * gtk_render_insertion_cursor:
4426 * @context: a #GtkStyleContext
4430 * @layout: the #PangoLayout of the text
4431 * @index: the index in the #PangoLayout
4432 * @direction: the #PangoDirection of the text
4434 * Draws a text caret on @cr at the specified index of @layout.
4439 gtk_render_insertion_cursor (GtkStyleContext *context,
4443 PangoLayout *layout,
4445 PangoDirection direction)
4447 GtkStyleContextPrivate *priv;
4448 gboolean split_cursor;
4449 PangoRectangle strong_pos, weak_pos;
4450 PangoRectangle *cursor1, *cursor2;
4451 PangoDirection keymap_direction;
4452 PangoDirection direction2;
4454 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4455 g_return_if_fail (cr != NULL);
4456 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4457 g_return_if_fail (index >= 0);
4459 priv = context->priv;
4461 g_object_get (gtk_settings_get_for_screen (priv->screen),
4462 "gtk-split-cursor", &split_cursor,
4465 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4467 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4469 direction2 = PANGO_DIRECTION_NEUTRAL;
4473 cursor1 = &strong_pos;
4475 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4477 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4478 cursor2 = &weak_pos;
4483 if (keymap_direction == direction)
4484 cursor1 = &strong_pos;
4486 cursor1 = &weak_pos;
4489 draw_insertion_cursor (context,
4491 x + PANGO_PIXELS (cursor1->x),
4492 y + PANGO_PIXELS (cursor1->y),
4493 PANGO_PIXELS (cursor1->height),
4496 direction2 != PANGO_DIRECTION_NEUTRAL);
4498 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4500 draw_insertion_cursor (context,
4502 x + PANGO_PIXELS (cursor2->x),
4503 y + PANGO_PIXELS (cursor2->y),
4504 PANGO_PIXELS (cursor2->height),
4512 * gtk_draw_insertion_cursor:
4513 * @widget: a #GtkWidget
4514 * @cr: cairo context to draw to
4515 * @location: location where to draw the cursor (@location->width is ignored)
4516 * @is_primary: if the cursor should be the primary cursor color.
4517 * @direction: whether the cursor is left-to-right or
4518 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4519 * @draw_arrow: %TRUE to draw a directional arrow on the
4520 * cursor. Should be %FALSE unless the cursor is split.
4522 * Draws a text caret on @cr at @location. This is not a style function
4523 * but merely a convenience function for drawing the standard cursor shape.
4526 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4529 gtk_draw_insertion_cursor (GtkWidget *widget,
4531 const GdkRectangle *location,
4532 gboolean is_primary,
4533 GtkTextDirection direction,
4534 gboolean draw_arrow)
4536 GtkStyleContext *context;
4538 g_return_if_fail (GTK_IS_WIDGET (widget));
4539 g_return_if_fail (cr != NULL);
4540 g_return_if_fail (location != NULL);
4541 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4543 context = gtk_widget_get_style_context (widget);
4545 draw_insertion_cursor (context, cr,
4546 location->x, location->y, location->height,
4548 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4552 static AtkAttributeSet *
4553 add_attribute (AtkAttributeSet *attributes,
4554 AtkTextAttribute attr,
4559 at = g_new (AtkAttribute, 1);
4560 at->name = g_strdup (atk_text_attribute_get_name (attr));
4561 at->value = g_strdup (value);
4563 return g_slist_prepend (attributes, at);
4567 * _gtk_style_context_get_attributes:
4568 * @attributes: a #AtkAttributeSet to add attributes to
4569 * @context: the #GtkStyleContext to get attributes from
4570 * @flags: the state to use with @context
4572 * Adds the foreground and background color from @context to
4573 * @attributes, after translating them to ATK attributes.
4575 * This is a convenience function that can be used in
4576 * implementing the #AtkText interface in widgets.
4578 * Returns: the modified #AtkAttributeSet
4581 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4582 GtkStyleContext *context,
4583 GtkStateFlags flags)
4588 gtk_style_context_get_background_color (context, flags, &color);
4589 value = g_strdup_printf ("%u,%u,%u",
4590 (guint) ceil (color.red * 65536 - color.red),
4591 (guint) ceil (color.green * 65536 - color.green),
4592 (guint) ceil (color.blue * 65536 - color.blue));
4593 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4596 gtk_style_context_get_color (context, flags, &color);
4597 value = g_strdup_printf ("%u,%u,%u",
4598 (guint) ceil (color.red * 65536 - color.red),
4599 (guint) ceil (color.green * 65536 - color.green),
4600 (guint) ceil (color.blue * 65536 - color.blue));
4601 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);