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 "gtkcontainerprivate.h"
27 #include "gtkcsscolorvalueprivate.h"
28 #include "gtkcsscornervalueprivate.h"
29 #include "gtkcssenginevalueprivate.h"
30 #include "gtkcssnumbervalueprivate.h"
31 #include "gtkcssrgbavalueprivate.h"
33 #include "gtkstylepropertiesprivate.h"
34 #include "gtktypebuiltins.h"
35 #include "gtkthemingengineprivate.h"
37 #include "gtkwidget.h"
38 #include "gtkwindow.h"
39 #include "gtkprivate.h"
40 #include "gtkiconfactory.h"
41 #include "gtkwidgetpath.h"
42 #include "gtkwidgetprivate.h"
43 #include "gtkstylecascadeprivate.h"
44 #include "gtkstyleproviderprivate.h"
45 #include "gtksettings.h"
46 #include "gtksettingsprivate.h"
48 #include "deprecated/gtkgradientprivate.h"
49 #include "deprecated/gtksymboliccolorprivate.h"
52 * SECTION:gtkstylecontext
53 * @Short_description: Rendering UI elements
54 * @Title: GtkStyleContext
56 * #GtkStyleContext is an object that stores styling information affecting
57 * a widget defined by #GtkWidgetPath.
59 * In order to construct the final style information, #GtkStyleContext
60 * queries information from all attached #GtkStyleProviders. Style providers
61 * can be either attached explicitly to the context through
62 * gtk_style_context_add_provider(), or to the screen through
63 * gtk_style_context_add_provider_for_screen(). The resulting style is a
64 * combination of all providers' information in priority order.
66 * For GTK+ widgets, any #GtkStyleContext returned by
67 * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
68 * #GdkScreen and RTL/LTR information set. The style context will be also
69 * updated automatically if any of these settings change on the widget.
71 * If you are using the theming layer standalone, you will need to set a
72 * widget path and a screen yourself to the created style context through
73 * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
74 * as updating the context yourself using gtk_style_context_invalidate()
75 * whenever any of the conditions change, such as a change in the
76 * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
79 * <refsect2 id="gtkstylecontext-animations">
80 * <title>Transition animations</title>
82 * #GtkStyleContext has built-in support for state change transitions.
83 * Note that these animations respect the #GtkSettings:gtk-enable-animations
87 * For simple widgets where state changes affect the whole widget area,
88 * calling gtk_style_context_notify_state_change() with a %NULL region
89 * is sufficient to trigger the transition animation. And GTK+ already
90 * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
94 * If a widget needs to declare several animatable regions (i.e. not
95 * affecting the whole widget area), its #GtkWidget::draw signal handler
96 * needs to wrap the render operations for the different regions with
97 * calls to gtk_style_context_push_animatable_region() and
98 * gtk_style_context_pop_animatable_region(). These functions take an
99 * identifier for the region which must be unique within the style context.
100 * For simple widgets with a fixed set of animatable regions, using an
101 * enumeration works well:
104 * <title>Using an enumeration to identify animatable regions</title>
115 * spin_button_draw (GtkWidget *widget,
118 * GtkStyleContext *context;
120 * context = gtk_widget_get_style_context (widget);
122 * gtk_style_context_push_animatable_region (context,
123 * GUINT_TO_POINTER (REGION_ENTRY));
125 * gtk_render_background (cr, 0, 0, 100, 30);
126 * gtk_render_frame (cr, 0, 0, 100, 30);
128 * gtk_style_context_pop_animatable_region (context);
135 * For complex widgets with an arbitrary number of animatable regions, it
136 * is up to the implementation to come up with a way to uniquely identify
137 * each animatable region. Using pointers to internal structs is one way
141 * <title>Using struct pointers to identify animatable regions</title>
144 * notebook_draw_tab (GtkWidget *widget,
145 * NotebookPage *page,
148 * gtk_style_context_push_animatable_region (context, page);
149 * gtk_render_extension (cr, page->x, page->y, page->width, page->height);
150 * gtk_style_context_pop_animatable_region (context);
155 * The widget also needs to notify the style context about a state change
156 * for a given animatable region so the animation is triggered.
159 * <title>Triggering a state change animation on a region</title>
162 * notebook_motion_notify (GtkWidget *widget,
163 * GdkEventMotion *event)
165 * GtkStyleContext *context;
166 * NotebookPage *page;
168 * context = gtk_widget_get_style_context (widget);
169 * page = find_page_under_pointer (widget, event);
170 * gtk_style_context_notify_state_change (context,
171 * gtk_widget_get_window (widget),
173 * GTK_STATE_PRELIGHT,
180 * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
181 * special value, in this case, the whole widget area will be updated
185 * <refsect2 id="gtkstylecontext-classes">
186 * <title>Style classes and regions</title>
188 * Widgets can add style classes to their context, which can be used
189 * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
190 * rendering. GTK+ has a number of predefined style classes:
191 * #GTK_STYLE_CLASS_CELL,
192 * #GTK_STYLE_CLASS_ENTRY,
193 * #GTK_STYLE_CLASS_BUTTON,
194 * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
195 * #GTK_STYLE_CLASS_CALENDAR,
196 * #GTK_STYLE_CLASS_SLIDER,
197 * #GTK_STYLE_CLASS_BACKGROUND,
198 * #GTK_STYLE_CLASS_RUBBERBAND,
199 * #GTK_STYLE_CLASS_TOOLTIP,
200 * #GTK_STYLE_CLASS_MENU,
201 * #GTK_STYLE_CLASS_MENUBAR,
202 * #GTK_STYLE_CLASS_MENUITEM,
203 * #GTK_STYLE_CLASS_TOOLBAR,
204 * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
205 * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
206 * #GTK_STYLE_CLASS_RADIO,
207 * #GTK_STYLE_CLASS_CHECK,
208 * #GTK_STYLE_CLASS_TROUGH,
209 * #GTK_STYLE_CLASS_SCROLLBAR,
210 * #GTK_STYLE_CLASS_SCALE,
211 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
212 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
213 * #GTK_STYLE_CLASS_HEADER,
214 * #GTK_STYLE_CLASS_ACCELERATOR,
215 * #GTK_STYLE_CLASS_GRIP,
216 * #GTK_STYLE_CLASS_DOCK,
217 * #GTK_STYLE_CLASS_PROGRESSBAR,
218 * #GTK_STYLE_CLASS_SPINNER,
219 * #GTK_STYLE_CLASS_EXPANDER,
220 * #GTK_STYLE_CLASS_SPINBUTTON,
221 * #GTK_STYLE_CLASS_NOTEBOOK,
222 * #GTK_STYLE_CLASS_VIEW,
223 * #GTK_STYLE_CLASS_SIDEBAR,
224 * #GTK_STYLE_CLASS_IMAGE,
225 * #GTK_STYLE_CLASS_HIGHLIGHT,
226 * #GTK_STYLE_CLASS_FRAME,
227 * #GTK_STYLE_CLASS_DND,
228 * #GTK_STYLE_CLASS_PANE_SEPARATOR,
229 * #GTK_STYLE_CLASS_SEPARATOR,
230 * #GTK_STYLE_CLASS_INFO,
231 * #GTK_STYLE_CLASS_WARNING,
232 * #GTK_STYLE_CLASS_QUESTION,
233 * #GTK_STYLE_CLASS_ERROR,
234 * #GTK_STYLE_CLASS_HORIZONTAL,
235 * #GTK_STYLE_CLASS_VERTICAL,
236 * #GTK_STYLE_CLASS_TOP,
237 * #GTK_STYLE_CLASS_BOTTOM,
238 * #GTK_STYLE_CLASS_LEFT,
239 * #GTK_STYLE_CLASS_RIGHT,
242 * Widgets can also add regions with flags to their context.
243 * The regions used by GTK+ widgets are:
248 * <entry>Region</entry>
249 * <entry>Flags</entry>
250 * <entry>Macro</entry>
251 * <entry>Used by</entry>
257 * <entry>even, odd</entry>
258 * <entry>GTK_STYLE_REGION_ROW</entry>
259 * <entry>#GtkTreeView</entry>
262 * <entry>column</entry>
263 * <entry>first, last, sorted</entry>
264 * <entry>GTK_STYLE_REGION_COLUMN</entry>
265 * <entry>#GtkTreeView</entry>
268 * <entry>column-header</entry>
270 * <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
275 * <entry>even, odd, first, last</entry>
276 * <entry>GTK_STYLE_REGION_TAB</entry>
277 * <entry>#GtkNotebook</entry>
284 * <refsect2 id="gtkstylecontext-custom-styling">
285 * <title>Custom styling in UI libraries and applications</title>
287 * If you are developing a library with custom #GtkWidget<!-- -->s that
288 * render differently than standard components, you may need to add a
289 * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
290 * priority, either a #GtkCssProvider or a custom object implementing the
291 * #GtkStyleProvider interface. This way theming engines may still attempt
292 * to style your UI elements in a different way if needed so.
295 * If you are using custom styling on an applications, you probably want then
296 * to make your style information prevail to the theme's, so you must use
297 * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
298 * priority, keep in mind that the user settings in
299 * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
300 * still take precedence over your changes, as it uses the
301 * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
304 * If a custom theming engine is needed, you probably want to implement a
305 * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
306 * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
307 * which loads the theming engine module from the standard paths.
312 /* When these change we do a full restyling. Otherwise we try to figure out
313 * if we need to change things. */
314 #define GTK_STYLE_CONTEXT_RADICAL_CHANGE (GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_SOURCE)
315 /* When these change we don't clear the cache. This takes more memory but makes
316 * things go faster. */
317 #define GTK_STYLE_CONTEXT_CACHED_CHANGE (GTK_CSS_CHANGE_STATE)
319 typedef struct GtkStyleInfo GtkStyleInfo;
320 typedef struct GtkRegion GtkRegion;
321 typedef struct PropertyValue PropertyValue;
322 typedef struct StyleData StyleData;
327 GtkRegionFlags flags;
341 GArray *style_classes;
343 GtkJunctionSides junction_sides;
344 GtkStateFlags state_flags;
350 GtkCssComputedValues *store;
351 GArray *property_cache;
355 struct _GtkStyleContextPrivate
359 GtkStyleCascade *cascade;
361 GtkStyleContext *animation_list_prev;
362 GtkStyleContext *animation_list_next;
364 GtkStyleContext *parent;
367 GtkWidgetPath *widget_path;
368 GHashTable *style_data;
371 GtkCssChange relevant_changes;
372 GtkCssChange pending_changes;
374 const GtkBitmask *invalidating_context;
390 static guint signals[LAST_SIGNAL] = { 0 };
391 static GtkStyleContext *_running_animations = NULL;
392 guint _running_animations_timer_id = 0;
394 static void gtk_style_context_finalize (GObject *object);
396 static void gtk_style_context_impl_set_property (GObject *object,
400 static void gtk_style_context_impl_get_property (GObject *object,
404 static StyleData *style_data_lookup (GtkStyleContext *context);
407 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
410 gtk_style_context_real_changed (GtkStyleContext *context)
412 GtkStyleContextPrivate *priv = context->priv;
415 _gtk_widget_style_context_invalidated (priv->widget);
419 gtk_style_context_class_init (GtkStyleContextClass *klass)
421 GObjectClass *object_class = G_OBJECT_CLASS (klass);
423 object_class->finalize = gtk_style_context_finalize;
424 object_class->set_property = gtk_style_context_impl_set_property;
425 object_class->get_property = gtk_style_context_impl_get_property;
427 klass->changed = gtk_style_context_real_changed;
430 g_signal_new (I_("changed"),
431 G_TYPE_FROM_CLASS (object_class),
433 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
435 g_cclosure_marshal_VOID__VOID,
438 g_object_class_install_property (object_class,
440 g_param_spec_object ("screen",
442 P_("The associated GdkScreen"),
444 GTK_PARAM_READWRITE));
445 g_object_class_install_property (object_class,
447 g_param_spec_enum ("direction",
449 P_("Text direction"),
450 GTK_TYPE_TEXT_DIRECTION,
452 GTK_PARAM_READWRITE));
454 * GtkStyleContext:parent:
456 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
461 g_object_class_install_property (object_class,
463 g_param_spec_object ("parent",
465 P_("The parent style context"),
466 GTK_TYPE_STYLE_CONTEXT,
467 GTK_PARAM_READWRITE));
469 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
473 style_data_new (void)
477 data = g_slice_new0 (StyleData);
484 clear_property_cache (StyleData *data)
488 if (!data->property_cache)
491 for (i = 0; i < data->property_cache->len; i++)
493 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
495 g_param_spec_unref (node->pspec);
496 g_value_unset (&node->value);
499 g_array_free (data->property_cache, TRUE);
500 data->property_cache = NULL;
504 style_data_ref (StyleData *style_data)
506 style_data->ref_count++;
512 style_data_unref (StyleData *data)
516 if (data->ref_count > 0)
519 g_object_unref (data->store);
520 clear_property_cache (data);
522 g_slice_free (StyleData, data);
526 style_data_is_animating (StyleData *style_data)
528 return !_gtk_css_computed_values_is_static (style_data->store);
531 static GtkStyleInfo *
532 style_info_new (void)
536 info = g_slice_new0 (GtkStyleInfo);
537 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
538 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
544 style_info_set_data (GtkStyleInfo *info,
547 if (info->data == data)
551 style_data_ref (data);
554 style_data_unref (info->data);
560 style_info_free (GtkStyleInfo *info)
562 style_info_set_data (info, NULL);
563 g_array_free (info->style_classes, TRUE);
564 g_array_free (info->regions, TRUE);
565 g_slice_free (GtkStyleInfo, info);
568 static GtkStyleInfo *
569 style_info_pop (GtkStyleInfo *info)
571 GtkStyleInfo *next = info->next;
573 style_info_free (info);
578 static GtkStyleInfo *
579 style_info_copy (GtkStyleInfo *info)
583 copy = style_info_new ();
584 g_array_insert_vals (copy->style_classes, 0,
585 info->style_classes->data,
586 info->style_classes->len);
588 g_array_insert_vals (copy->regions, 0,
593 copy->junction_sides = info->junction_sides;
594 copy->state_flags = info->state_flags;
595 style_info_set_data (copy, info->data);
601 style_info_hash (gconstpointer elem)
603 const GtkStyleInfo *info;
608 for (i = 0; i < info->style_classes->len; i++)
610 hash += g_array_index (info->style_classes, GQuark, i);
614 for (i = 0; i < info->regions->len; i++)
618 region = &g_array_index (info->regions, GtkRegion, i);
619 hash += region->class_quark;
620 hash += region->flags;
624 return hash ^ info->state_flags;
628 style_info_equal (gconstpointer elem1,
631 const GtkStyleInfo *info1, *info2;
636 if (info1->state_flags != info2->state_flags)
639 if (info1->junction_sides != info2->junction_sides)
642 if (info1->style_classes->len != info2->style_classes->len)
645 if (memcmp (info1->style_classes->data,
646 info2->style_classes->data,
647 info1->style_classes->len * sizeof (GQuark)) != 0)
650 if (info1->regions->len != info2->regions->len)
653 if (memcmp (info1->regions->data,
654 info2->regions->data,
655 info1->regions->len * sizeof (GtkRegion)) != 0)
662 gtk_style_context_cascade_changed (GtkStyleCascade *cascade,
663 GtkStyleContext *context)
665 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_SOURCE);
669 gtk_style_context_set_cascade (GtkStyleContext *context,
670 GtkStyleCascade *cascade)
672 GtkStyleContextPrivate *priv;
674 priv = context->priv;
676 if (priv->cascade == cascade)
681 g_object_ref (cascade);
682 g_signal_connect (cascade,
683 "-gtk-private-changed",
684 G_CALLBACK (gtk_style_context_cascade_changed),
690 g_signal_handlers_disconnect_by_func (priv->cascade,
691 gtk_style_context_cascade_changed,
693 g_object_unref (priv->cascade);
696 priv->cascade = cascade;
699 gtk_style_context_cascade_changed (cascade, context);
703 gtk_style_context_init (GtkStyleContext *style_context)
705 GtkStyleContextPrivate *priv;
707 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
708 GTK_TYPE_STYLE_CONTEXT,
709 GtkStyleContextPrivate);
711 priv->style_data = g_hash_table_new_full (style_info_hash,
713 (GDestroyNotify) style_info_free,
714 (GDestroyNotify) style_data_unref);
716 priv->screen = gdk_screen_get_default ();
717 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
719 /* Create default info store */
720 priv->info = style_info_new ();
721 priv->info->state_flags = GTK_STATE_FLAG_DIR_LTR;
723 gtk_style_context_set_cascade (style_context,
724 _gtk_style_cascade_get_for_screen (priv->screen));
728 gtk_style_context_do_animations (gpointer unused)
730 GtkStyleContext *context;
732 for (context = _running_animations;
734 context = context->priv->animation_list_next)
736 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANIMATE);
743 gtk_style_context_is_animating (GtkStyleContext *context)
745 GtkStyleContextPrivate *priv = context->priv;
747 return priv->animation_list_prev != NULL
748 || _running_animations == context;
752 gtk_style_context_stop_animating (GtkStyleContext *context)
754 GtkStyleContextPrivate *priv = context->priv;
756 if (!gtk_style_context_is_animating (context))
759 if (priv->animation_list_prev == NULL)
761 _running_animations = priv->animation_list_next;
763 if (_running_animations == NULL)
765 /* we were the last animation */
766 g_source_remove (_running_animations_timer_id);
767 _running_animations_timer_id = 0;
771 priv->animation_list_prev->priv->animation_list_next = priv->animation_list_next;
773 if (priv->animation_list_next)
774 priv->animation_list_next->priv->animation_list_prev = priv->animation_list_prev;
776 priv->animation_list_next = NULL;
777 priv->animation_list_prev = NULL;
781 gtk_style_context_start_animating (GtkStyleContext *context)
783 GtkStyleContextPrivate *priv = context->priv;
785 if (gtk_style_context_is_animating (context))
788 if (_running_animations == NULL)
790 _running_animations_timer_id = gdk_threads_add_timeout (25,
791 gtk_style_context_do_animations,
793 _running_animations = context;
797 priv->animation_list_next = _running_animations;
798 _running_animations->priv->animation_list_prev = context;
799 _running_animations = context;
804 gtk_style_context_should_animate (GtkStyleContext *context)
806 GtkStyleContextPrivate *priv;
810 priv = context->priv;
812 if (priv->widget == NULL)
815 if (!gtk_widget_get_mapped (priv->widget))
818 data = style_data_lookup (context);
819 if (!style_data_is_animating (data))
822 g_object_get (gtk_widget_get_settings (context->priv->widget),
823 "gtk-enable-animations", &animate,
830 _gtk_style_context_update_animating (GtkStyleContext *context)
832 if (gtk_style_context_should_animate (context))
833 gtk_style_context_start_animating (context);
835 gtk_style_context_stop_animating (context);
839 gtk_style_context_finalize (GObject *object)
841 GtkStyleContextPrivate *priv;
842 GtkStyleContext *style_context;
844 style_context = GTK_STYLE_CONTEXT (object);
845 priv = style_context->priv;
847 gtk_style_context_stop_animating (style_context);
849 /* children hold a reference to us */
850 g_assert (priv->children == NULL);
852 gtk_style_context_set_parent (style_context, NULL);
854 gtk_style_context_set_cascade (style_context, NULL);
856 if (priv->widget_path)
857 gtk_widget_path_free (priv->widget_path);
859 g_hash_table_destroy (priv->style_data);
862 priv->info = style_info_pop (priv->info);
864 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
868 gtk_style_context_impl_set_property (GObject *object,
873 GtkStyleContext *style_context;
875 style_context = GTK_STYLE_CONTEXT (object);
880 gtk_style_context_set_screen (style_context,
881 g_value_get_object (value));
884 gtk_style_context_set_direction (style_context,
885 g_value_get_enum (value));
888 gtk_style_context_set_parent (style_context,
889 g_value_get_object (value));
892 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
898 gtk_style_context_impl_get_property (GObject *object,
903 GtkStyleContext *style_context;
904 GtkStyleContextPrivate *priv;
906 style_context = GTK_STYLE_CONTEXT (object);
907 priv = style_context->priv;
912 g_value_set_object (value, priv->screen);
915 g_value_set_enum (value, gtk_style_context_get_direction (style_context));
918 g_value_set_object (value, priv->parent);
921 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
926 static GtkWidgetPath *
927 create_query_path (GtkStyleContext *context,
930 GtkStyleContextPrivate *priv;
934 priv = context->priv;
935 path = priv->widget ? _gtk_widget_create_path (priv->widget) : gtk_widget_path_copy (priv->widget_path);
936 pos = gtk_widget_path_length (path) - 1;
938 /* Set widget regions */
939 for (i = 0; i < info->regions->len; i++)
943 region = &g_array_index (info->regions, GtkRegion, i);
944 gtk_widget_path_iter_add_region (path, pos,
945 g_quark_to_string (region->class_quark),
949 /* Set widget classes */
950 for (i = 0; i < info->style_classes->len; i++)
954 quark = g_array_index (info->style_classes, GQuark, i);
955 gtk_widget_path_iter_add_class (path, pos,
956 g_quark_to_string (quark));
963 build_properties (GtkStyleContext *context,
964 GtkCssComputedValues *values,
966 const GtkBitmask *relevant_changes)
968 GtkStyleContextPrivate *priv;
969 GtkCssMatcher matcher;
971 GtkCssLookup *lookup;
973 priv = context->priv;
975 path = create_query_path (context, info);
976 lookup = _gtk_css_lookup_new (relevant_changes);
978 if (_gtk_css_matcher_init (&matcher, path, info->state_flags))
979 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
983 _gtk_css_lookup_resolve (lookup,
984 GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
986 priv->parent ? style_data_lookup (priv->parent)->store : NULL);
988 _gtk_css_lookup_free (lookup);
989 gtk_widget_path_free (path);
993 style_data_lookup (GtkStyleContext *context)
995 GtkStyleContextPrivate *priv;
999 priv = context->priv;
1002 /* Current data in use is cached, just return it */
1006 g_assert (priv->widget != NULL || priv->widget_path != NULL);
1008 data = g_hash_table_lookup (priv->style_data, info);
1011 style_info_set_data (info, data);
1015 data = style_data_new ();
1016 data->store = _gtk_css_computed_values_new ();
1017 style_info_set_data (info, data);
1018 g_hash_table_insert (priv->style_data,
1019 style_info_copy (info),
1022 build_properties (context, data->store, info, NULL);
1028 style_data_lookup_for_state (GtkStyleContext *context,
1029 GtkStateFlags state)
1033 if (context->priv->info->state_flags == state)
1034 return style_data_lookup (context);
1036 gtk_style_context_save (context);
1037 gtk_style_context_set_state (context, state);
1038 data = style_data_lookup (context);
1039 gtk_style_context_restore (context);
1045 gtk_style_context_set_invalid (GtkStyleContext *context,
1048 GtkStyleContextPrivate *priv;
1050 priv = context->priv;
1052 if (priv->invalid == invalid)
1055 priv->invalid = invalid;
1060 gtk_style_context_set_invalid (priv->parent, TRUE);
1061 else if (GTK_IS_RESIZE_CONTAINER (priv->widget))
1062 _gtk_container_queue_restyle (GTK_CONTAINER (priv->widget));
1066 /* returns TRUE if someone called gtk_style_context_save() but hasn't
1067 * called gtk_style_context_restore() yet.
1068 * In those situations we don't invalidate the context when somebody
1069 * changes state/regions/classes.
1072 gtk_style_context_is_saved (GtkStyleContext *context)
1074 return context->priv->info->next != NULL;
1078 gtk_style_context_queue_invalidate_internal (GtkStyleContext *context,
1079 GtkCssChange change)
1081 GtkStyleContextPrivate *priv = context->priv;
1082 GtkStyleInfo *info = priv->info;
1084 if (gtk_style_context_is_saved (context))
1086 style_info_set_data (info, NULL);
1090 _gtk_style_context_queue_invalidate (context, change);
1091 /* XXX: We need to invalidate siblings here somehow */
1096 * gtk_style_context_new:
1098 * Creates a standalone #GtkStyleContext, this style context
1099 * won't be attached to any widget, so you may want
1100 * to call gtk_style_context_set_path() yourself.
1103 * This function is only useful when using the theming layer
1104 * separated from GTK+, if you are using #GtkStyleContext to
1105 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1106 * in order to get a style context ready to theme the widget.
1109 * Returns: A newly created #GtkStyleContext.
1112 gtk_style_context_new (void)
1114 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1118 _gtk_style_context_set_widget (GtkStyleContext *context,
1121 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1122 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1124 context->priv->widget = widget;
1126 _gtk_style_context_update_animating (context);
1128 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_SELF);
1132 * gtk_style_context_add_provider:
1133 * @context: a #GtkStyleContext
1134 * @provider: a #GtkStyleProvider
1135 * @priority: the priority of the style provider. The lower
1136 * it is, the earlier it will be used in the style
1137 * construction. Typically this will be in the range
1138 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1139 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1141 * Adds a style provider to @context, to be used in style construction.
1142 * Note that a style provider added by this function only affects
1143 * the style of the widget to which @context belongs. If you want
1144 * to affect the style of all widgets, use
1145 * gtk_style_context_add_provider_for_screen().
1147 * <note><para>If both priorities are the same, A #GtkStyleProvider
1148 * added through this function takes precedence over another added
1149 * through gtk_style_context_add_provider_for_screen().</para></note>
1154 gtk_style_context_add_provider (GtkStyleContext *context,
1155 GtkStyleProvider *provider,
1158 GtkStyleContextPrivate *priv;
1160 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1161 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1163 priv = context->priv;
1165 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1167 GtkStyleCascade *new_cascade;
1169 new_cascade = _gtk_style_cascade_new ();
1170 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1171 _gtk_style_cascade_add_provider (new_cascade, provider, priority);
1172 gtk_style_context_set_cascade (context, new_cascade);
1173 g_object_unref (new_cascade);
1177 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1182 * gtk_style_context_remove_provider:
1183 * @context: a #GtkStyleContext
1184 * @provider: a #GtkStyleProvider
1186 * Removes @provider from the style providers list in @context.
1191 gtk_style_context_remove_provider (GtkStyleContext *context,
1192 GtkStyleProvider *provider)
1194 GtkStyleContextPrivate *priv;
1196 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1197 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1199 priv = context->priv;
1201 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1204 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1208 * gtk_style_context_reset_widgets:
1209 * @screen: a #GdkScreen
1211 * This function recomputes the styles for all widgets under a particular
1212 * #GdkScreen. This is useful when some global parameter has changed that
1213 * affects the appearance of all widgets, because when a widget gets a new
1214 * style, it will both redraw and recompute any cached information about
1215 * its appearance. As an example, it is used when the color scheme changes
1216 * in the related #GtkSettings object.
1221 gtk_style_context_reset_widgets (GdkScreen *screen)
1223 GList *list, *toplevels;
1225 _gtk_icon_set_invalidate_caches ();
1227 toplevels = gtk_window_list_toplevels ();
1228 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1230 for (list = toplevels; list; list = list->next)
1232 if (gtk_widget_get_screen (list->data) == screen)
1233 gtk_widget_reset_style (list->data);
1235 g_object_unref (list->data);
1238 g_list_free (toplevels);
1242 * gtk_style_context_add_provider_for_screen:
1243 * @screen: a #GdkScreen
1244 * @provider: a #GtkStyleProvider
1245 * @priority: the priority of the style provider. The lower
1246 * it is, the earlier it will be used in the style
1247 * construction. Typically this will be in the range
1248 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1249 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1251 * Adds a global style provider to @screen, which will be used
1252 * in style construction for all #GtkStyleContexts under @screen.
1254 * GTK+ uses this to make styling information from #GtkSettings
1257 * <note><para>If both priorities are the same, A #GtkStyleProvider
1258 * added through gtk_style_context_add_provider() takes precedence
1259 * over another added through this function.</para></note>
1264 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1265 GtkStyleProvider *provider,
1268 GtkStyleCascade *cascade;
1270 g_return_if_fail (GDK_IS_SCREEN (screen));
1271 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1272 g_return_if_fail (!GTK_IS_SETTINGS (provider) || _gtk_settings_get_screen (GTK_SETTINGS (provider)) == screen);
1274 cascade = _gtk_style_cascade_get_for_screen (screen);
1275 _gtk_style_cascade_add_provider (cascade, provider, priority);
1279 * gtk_style_context_remove_provider_for_screen:
1280 * @screen: a #GdkScreen
1281 * @provider: a #GtkStyleProvider
1283 * Removes @provider from the global style providers list in @screen.
1288 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1289 GtkStyleProvider *provider)
1291 GtkStyleCascade *cascade;
1293 g_return_if_fail (GDK_IS_SCREEN (screen));
1294 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1295 g_return_if_fail (!GTK_IS_SETTINGS (provider));
1297 cascade = _gtk_style_cascade_get_for_screen (screen);
1298 _gtk_style_cascade_remove_provider (cascade, provider);
1302 * gtk_style_context_get_section:
1303 * @context: a #GtkStyleContext
1304 * @property: style property name
1306 * Queries the location in the CSS where @property was defined for the
1307 * current @context. Note that the state to be queried is taken from
1308 * gtk_style_context_get_state().
1310 * If the location is not available, %NULL will be returned. The
1311 * location might not be available for various reasons, such as the
1312 * property being overridden, @property not naming a supported CSS
1313 * property or tracking of definitions being disabled for performance
1316 * Shorthand CSS properties cannot be queried for a location and will
1317 * always return %NULL.
1319 * Returns: %NULL or the section where value was defined
1322 gtk_style_context_get_section (GtkStyleContext *context,
1323 const gchar *property)
1325 GtkStyleContextPrivate *priv;
1326 GtkStyleProperty *prop;
1329 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1330 g_return_val_if_fail (property != NULL, NULL);
1332 priv = context->priv;
1333 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1335 prop = _gtk_style_property_lookup (property);
1336 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1339 data = style_data_lookup (context);
1340 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1343 static GtkCssValue *
1344 gtk_style_context_query_func (guint id,
1347 return _gtk_css_computed_values_get_value (values, id);
1351 * gtk_style_context_get_property:
1352 * @context: a #GtkStyleContext
1353 * @property: style property name
1354 * @state: state to retrieve the property value for
1355 * @value: (out) (transfer full): return location for the style property value
1357 * Gets a style property from @context for the given state.
1359 * When @value is no longer needed, g_value_unset() must be called
1360 * to free any allocated memory.
1365 gtk_style_context_get_property (GtkStyleContext *context,
1366 const gchar *property,
1367 GtkStateFlags state,
1370 GtkStyleContextPrivate *priv;
1371 GtkStyleProperty *prop;
1374 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1375 g_return_if_fail (property != NULL);
1376 g_return_if_fail (value != NULL);
1378 priv = context->priv;
1379 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1381 prop = _gtk_style_property_lookup (property);
1384 g_warning ("Style property \"%s\" is not registered", property);
1387 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1389 g_warning ("Style property \"%s\" is not gettable", property);
1393 data = style_data_lookup_for_state (context, state);
1394 _gtk_style_property_query (prop, value, gtk_style_context_query_func, data->store);
1398 * gtk_style_context_get_valist:
1399 * @context: a #GtkStyleContext
1400 * @state: state to retrieve the property values for
1401 * @args: va_list of property name/return location pairs, followed by %NULL
1403 * Retrieves several style property values from @context for a given state.
1408 gtk_style_context_get_valist (GtkStyleContext *context,
1409 GtkStateFlags state,
1412 const gchar *property_name;
1414 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1416 property_name = va_arg (args, const gchar *);
1418 while (property_name)
1420 gchar *error = NULL;
1421 GValue value = G_VALUE_INIT;
1423 gtk_style_context_get_property (context,
1428 G_VALUE_LCOPY (&value, args, 0, &error);
1429 g_value_unset (&value);
1433 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1438 property_name = va_arg (args, const gchar *);
1443 * gtk_style_context_get:
1444 * @context: a #GtkStyleContext
1445 * @state: state to retrieve the property values for
1446 * @...: property name /return value pairs, followed by %NULL
1448 * Retrieves several style property values from @context for a
1454 gtk_style_context_get (GtkStyleContext *context,
1455 GtkStateFlags state,
1460 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1462 va_start (args, state);
1463 gtk_style_context_get_valist (context, state, args);
1468 * gtk_style_context_set_state:
1469 * @context: a #GtkStyleContext
1470 * @flags: state to represent
1472 * Sets the state to be used when rendering with any
1473 * of the gtk_render_*() functions.
1478 gtk_style_context_set_state (GtkStyleContext *context,
1479 GtkStateFlags flags)
1481 GtkStateFlags old_flags;
1482 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1484 old_flags = context->priv->info->state_flags;
1485 if (old_flags == flags)
1488 context->priv->info->state_flags = flags;
1490 if (((old_flags ^ flags) & (GTK_STATE_FLAG_DIR_LTR | GTK_STATE_FLAG_DIR_RTL)) &&
1491 !gtk_style_context_is_saved (context))
1492 g_object_notify (G_OBJECT (context), "direction");
1494 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_STATE);
1498 * gtk_style_context_get_state:
1499 * @context: a #GtkStyleContext
1501 * Returns the state used when rendering.
1503 * Returns: the state flags
1508 gtk_style_context_get_state (GtkStyleContext *context)
1510 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1512 return context->priv->info->state_flags;
1516 * gtk_style_context_state_is_running:
1517 * @context: a #GtkStyleContext
1518 * @state: a widget state
1519 * @progress: (out): return location for the transition progress
1521 * Returns %TRUE if there is a transition animation running for the
1522 * current region (see gtk_style_context_push_animatable_region()).
1524 * If @progress is not %NULL, the animation progress will be returned
1525 * there, 0.0 means the state is closest to being unset, while 1.0 means
1526 * it's closest to being set. This means transition animation will
1527 * run from 0 to 1 when @state is being set and from 1 to 0 when
1530 * Returns: %TRUE if there is a running transition animation for @state.
1534 * Deprecated: 3.6: This function always returns %FALSE
1537 gtk_style_context_state_is_running (GtkStyleContext *context,
1541 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1547 * gtk_style_context_set_path:
1548 * @context: a #GtkStyleContext
1549 * @path: a #GtkWidgetPath
1551 * Sets the #GtkWidgetPath used for style matching. As a
1552 * consequence, the style will be regenerated to match
1553 * the new given path.
1555 * If you are using a #GtkStyleContext returned from
1556 * gtk_widget_get_style_context(), you do not need to call
1562 gtk_style_context_set_path (GtkStyleContext *context,
1563 GtkWidgetPath *path)
1565 GtkStyleContextPrivate *priv;
1567 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1568 g_return_if_fail (path != NULL);
1570 priv = context->priv;
1571 g_return_if_fail (priv->widget == NULL);
1573 if (priv->widget_path)
1575 gtk_widget_path_free (priv->widget_path);
1576 priv->widget_path = NULL;
1580 priv->widget_path = gtk_widget_path_copy (path);
1582 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY);
1586 * gtk_style_context_get_path:
1587 * @context: a #GtkStyleContext
1589 * Returns the widget path used for style matching.
1591 * Returns: (transfer none): A #GtkWidgetPath
1595 const GtkWidgetPath *
1596 gtk_style_context_get_path (GtkStyleContext *context)
1598 GtkStyleContextPrivate *priv;
1600 priv = context->priv;
1602 return gtk_widget_get_path (priv->widget);
1604 return priv->widget_path;
1608 * gtk_style_context_set_parent:
1609 * @context: a #GtkStyleContext
1610 * @parent: (allow-none): the new parent or %NULL
1612 * Sets the parent style context for @context. The parent style
1613 * context is used to implement
1614 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1617 * If you are using a #GtkStyleContext returned from
1618 * gtk_widget_get_style_context(), the parent will be set for you.
1623 gtk_style_context_set_parent (GtkStyleContext *context,
1624 GtkStyleContext *parent)
1626 GtkStyleContextPrivate *priv;
1628 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1629 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1631 priv = context->priv;
1633 if (priv->parent == parent)
1638 parent->priv->children = g_slist_prepend (parent->priv->children, context);
1639 g_object_ref (parent);
1641 gtk_style_context_set_invalid (parent, TRUE);
1646 priv->parent->priv->children = g_slist_remove (priv->parent->priv->children, context);
1647 g_object_unref (priv->parent);
1650 priv->parent = parent;
1652 g_object_notify (G_OBJECT (context), "parent");
1653 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
1657 * gtk_style_context_get_parent:
1658 * @context: a #GtkStyleContext
1660 * Gets the parent context set via gtk_style_context_set_parent().
1661 * See that function for details.
1663 * Returns: (transfer none): the parent context or %NULL
1668 gtk_style_context_get_parent (GtkStyleContext *context)
1670 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1672 return context->priv->parent;
1676 * gtk_style_context_save:
1677 * @context: a #GtkStyleContext
1679 * Saves the @context state, so all modifications done through
1680 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1681 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1682 * or gtk_style_context_set_junction_sides() can be reverted in one
1683 * go through gtk_style_context_restore().
1688 gtk_style_context_save (GtkStyleContext *context)
1690 GtkStyleContextPrivate *priv;
1692 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1694 priv = context->priv;
1696 priv->info = style_info_copy (priv->info);
1697 /* Need to unset animations here because we can not know what style
1698 * class potential transitions came from once we save().
1700 if (priv->info->data && style_data_is_animating (priv->info->data))
1701 style_info_set_data (priv->info, NULL);
1705 * gtk_style_context_restore:
1706 * @context: a #GtkStyleContext
1708 * Restores @context state to a previous stage.
1709 * See gtk_style_context_save().
1714 gtk_style_context_restore (GtkStyleContext *context)
1716 GtkStyleContextPrivate *priv;
1718 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1720 priv = context->priv;
1722 priv->info = style_info_pop (priv->info);
1726 g_warning ("Unpaired gtk_style_context_restore() call");
1728 /* Create default region */
1729 priv->info = style_info_new ();
1734 style_class_find (GArray *array,
1739 gboolean found = FALSE;
1745 if (!array || array->len == 0)
1749 max = array->len - 1;
1755 mid = (min + max) / 2;
1756 item = g_array_index (array, GQuark, mid);
1758 if (class_quark == item)
1763 else if (class_quark > item)
1764 min = pos = mid + 1;
1771 while (!found && min <= max);
1780 region_find (GArray *array,
1785 gboolean found = FALSE;
1791 if (!array || array->len == 0)
1795 max = array->len - 1;
1801 mid = (min + max) / 2;
1802 region = &g_array_index (array, GtkRegion, mid);
1804 if (region->class_quark == class_quark)
1809 else if (region->class_quark > class_quark)
1810 min = pos = mid + 1;
1817 while (!found && min <= max);
1826 * gtk_style_context_add_class:
1827 * @context: a #GtkStyleContext
1828 * @class_name: class name to use in styling
1830 * Adds a style class to @context, so posterior calls to
1831 * gtk_style_context_get() or any of the gtk_render_*()
1832 * functions will make use of this new class for styling.
1834 * In the CSS file format, a #GtkEntry defining an "entry"
1835 * class, would be matched by:
1838 * GtkEntry.entry { ... }
1841 * While any widget defining an "entry" class would be
1850 gtk_style_context_add_class (GtkStyleContext *context,
1851 const gchar *class_name)
1853 GtkStyleContextPrivate *priv;
1858 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1859 g_return_if_fail (class_name != NULL);
1861 priv = context->priv;
1862 class_quark = g_quark_from_string (class_name);
1866 if (!style_class_find (info->style_classes, class_quark, &position))
1868 g_array_insert_val (info->style_classes, position, class_quark);
1870 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1875 * gtk_style_context_remove_class:
1876 * @context: a #GtkStyleContext
1877 * @class_name: class name to remove
1879 * Removes @class_name from @context.
1884 gtk_style_context_remove_class (GtkStyleContext *context,
1885 const gchar *class_name)
1887 GtkStyleContextPrivate *priv;
1892 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1893 g_return_if_fail (class_name != NULL);
1895 class_quark = g_quark_try_string (class_name);
1900 priv = context->priv;
1904 if (style_class_find (info->style_classes, class_quark, &position))
1906 g_array_remove_index (info->style_classes, position);
1908 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1913 * gtk_style_context_has_class:
1914 * @context: a #GtkStyleContext
1915 * @class_name: a class name
1917 * Returns %TRUE if @context currently has defined the
1920 * Returns: %TRUE if @context has @class_name defined
1925 gtk_style_context_has_class (GtkStyleContext *context,
1926 const gchar *class_name)
1928 GtkStyleContextPrivate *priv;
1932 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1933 g_return_val_if_fail (class_name != NULL, FALSE);
1935 class_quark = g_quark_try_string (class_name);
1940 priv = context->priv;
1944 if (style_class_find (info->style_classes, class_quark, NULL))
1951 * gtk_style_context_list_classes:
1952 * @context: a #GtkStyleContext
1954 * Returns the list of classes currently defined in @context.
1956 * Returns: (transfer container) (element-type utf8): a #GList of
1957 * strings with the currently defined classes. The contents
1958 * of the list are owned by GTK+, but you must free the list
1959 * itself with g_list_free() when you are done with it.
1964 gtk_style_context_list_classes (GtkStyleContext *context)
1966 GtkStyleContextPrivate *priv;
1968 GList *classes = NULL;
1971 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1973 priv = context->priv;
1977 for (i = 0; i < info->style_classes->len; i++)
1981 quark = g_array_index (info->style_classes, GQuark, i);
1982 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1989 * gtk_style_context_list_regions:
1990 * @context: a #GtkStyleContext
1992 * Returns the list of regions currently defined in @context.
1994 * Returns: (transfer container) (element-type utf8): a #GList of
1995 * strings with the currently defined regions. The contents
1996 * of the list are owned by GTK+, but you must free the list
1997 * itself with g_list_free() when you are done with it.
2002 gtk_style_context_list_regions (GtkStyleContext *context)
2004 GtkStyleContextPrivate *priv;
2006 GList *classes = NULL;
2009 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2011 priv = context->priv;
2015 for (i = 0; i < info->regions->len; i++)
2018 const gchar *class_name;
2020 region = &g_array_index (info->regions, GtkRegion, i);
2022 class_name = g_quark_to_string (region->class_quark);
2023 classes = g_list_prepend (classes, (gchar *) class_name);
2030 _gtk_style_context_check_region_name (const gchar *str)
2032 g_return_val_if_fail (str != NULL, FALSE);
2034 if (!g_ascii_islower (str[0]))
2040 !g_ascii_islower (*str))
2050 * gtk_style_context_add_region:
2051 * @context: a #GtkStyleContext
2052 * @region_name: region name to use in styling
2053 * @flags: flags that apply to the region
2055 * Adds a region to @context, so posterior calls to
2056 * gtk_style_context_get() or any of the gtk_render_*()
2057 * functions will make use of this new region for styling.
2059 * In the CSS file format, a #GtkTreeView defining a "row"
2060 * region, would be matched by:
2063 * GtkTreeView row { ... }
2066 * Pseudo-classes are used for matching @flags, so the two
2069 * GtkTreeView row:nth-child(even) { ... }
2070 * GtkTreeView row:nth-child(odd) { ... }
2073 * would apply to even and odd rows, respectively.
2075 * <note><para>Region names must only contain lowercase letters
2076 * and '-', starting always with a lowercase letter.</para></note>
2081 gtk_style_context_add_region (GtkStyleContext *context,
2082 const gchar *region_name,
2083 GtkRegionFlags flags)
2085 GtkStyleContextPrivate *priv;
2087 GQuark region_quark;
2090 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2091 g_return_if_fail (region_name != NULL);
2092 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2094 priv = context->priv;
2095 region_quark = g_quark_from_string (region_name);
2099 if (!region_find (info->regions, region_quark, &position))
2103 region.class_quark = region_quark;
2104 region.flags = flags;
2106 g_array_insert_val (info->regions, position, region);
2108 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2113 * gtk_style_context_remove_region:
2114 * @context: a #GtkStyleContext
2115 * @region_name: region name to unset
2117 * Removes a region from @context.
2122 gtk_style_context_remove_region (GtkStyleContext *context,
2123 const gchar *region_name)
2125 GtkStyleContextPrivate *priv;
2127 GQuark region_quark;
2130 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2131 g_return_if_fail (region_name != NULL);
2133 region_quark = g_quark_try_string (region_name);
2138 priv = context->priv;
2142 if (region_find (info->regions, region_quark, &position))
2144 g_array_remove_index (info->regions, position);
2146 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2151 * gtk_style_context_has_region:
2152 * @context: a #GtkStyleContext
2153 * @region_name: a region name
2154 * @flags_return: (out) (allow-none): return location for region flags
2156 * Returns %TRUE if @context has the region defined.
2157 * If @flags_return is not %NULL, it is set to the flags
2158 * affecting the region.
2160 * Returns: %TRUE if region is defined
2165 gtk_style_context_has_region (GtkStyleContext *context,
2166 const gchar *region_name,
2167 GtkRegionFlags *flags_return)
2169 GtkStyleContextPrivate *priv;
2171 GQuark region_quark;
2174 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2175 g_return_val_if_fail (region_name != NULL, FALSE);
2180 region_quark = g_quark_try_string (region_name);
2185 priv = context->priv;
2189 if (region_find (info->regions, region_quark, &position))
2195 region = &g_array_index (info->regions, GtkRegion, position);
2196 *flags_return = region->flags;
2205 style_property_values_cmp (gconstpointer bsearch_node1,
2206 gconstpointer bsearch_node2)
2208 const PropertyValue *val1 = bsearch_node1;
2209 const PropertyValue *val2 = bsearch_node2;
2211 if (val1->widget_type != val2->widget_type)
2212 return val1->widget_type < val2->widget_type ? -1 : 1;
2214 if (val1->pspec != val2->pspec)
2215 return val1->pspec < val2->pspec ? -1 : 1;
2217 if (val1->state != val2->state)
2218 return val1->state < val2->state ? -1 : 1;
2224 _gtk_style_context_peek_property (GtkStyleContext *context,
2227 StyleData *data = style_data_lookup (context);
2229 return _gtk_css_computed_values_get_value (data->store, property_id);
2233 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2235 GtkStateFlags state,
2238 GtkStyleContextPrivate *priv;
2239 PropertyValue *pcache, key = { 0 };
2243 priv = context->priv;
2245 data = style_data_lookup_for_state (context, state);
2247 key.widget_type = widget_type;
2251 /* need value cache array */
2252 if (!data->property_cache)
2253 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2256 pcache = bsearch (&key,
2257 data->property_cache->data, data->property_cache->len,
2258 sizeof (PropertyValue), style_property_values_cmp);
2260 return &pcache->value;
2264 while (i < data->property_cache->len &&
2265 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2268 g_array_insert_val (data->property_cache, i, key);
2269 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2271 /* cache miss, initialize value type, then set contents */
2272 g_param_spec_ref (pcache->pspec);
2273 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2275 if (priv->widget || priv->widget_path)
2277 GtkWidgetPath *widget_path = priv->widget ? _gtk_widget_create_path (priv->widget) : priv->widget_path;
2279 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2281 state, pspec, &pcache->value))
2283 G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
2285 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2286 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2288 GtkSymbolicColor *color;
2291 color = g_value_dup_boxed (&pcache->value);
2293 g_value_unset (&pcache->value);
2295 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2296 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2298 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2300 if (_gtk_style_context_resolve_color (context, _gtk_symbolic_color_get_css_value (color), &rgba, NULL))
2302 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2303 g_value_set_boxed (&pcache->value, &rgba);
2308 rgb.red = rgba.red * 65535. + 0.5;
2309 rgb.green = rgba.green * 65535. + 0.5;
2310 rgb.blue = rgba.blue * 65535. + 0.5;
2312 g_value_set_boxed (&pcache->value, &rgb);
2316 g_param_value_set_default (pspec, &pcache->value);
2318 gtk_symbolic_color_unref (color);
2321 G_GNUC_END_IGNORE_DEPRECATIONS;
2324 gtk_widget_path_free (widget_path);
2326 return &pcache->value;
2330 gtk_widget_path_free (widget_path);
2333 /* not supplied by any provider, revert to default */
2334 g_param_value_set_default (pspec, &pcache->value);
2336 return &pcache->value;
2340 * gtk_style_context_get_style_property:
2341 * @context: a #GtkStyleContext
2342 * @property_name: the name of the widget style property
2343 * @value: Return location for the property value
2345 * Gets the value for a widget style property.
2347 * When @value is no longer needed, g_value_unset() must be called
2348 * to free any allocated memory.
2351 gtk_style_context_get_style_property (GtkStyleContext *context,
2352 const gchar *property_name,
2355 GtkStyleContextPrivate *priv;
2356 GtkWidgetClass *widget_class;
2357 GtkStateFlags state;
2359 const GValue *peek_value;
2362 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2363 g_return_if_fail (property_name != NULL);
2364 g_return_if_fail (value != NULL);
2366 priv = context->priv;
2370 widget_type = G_OBJECT_TYPE (priv->widget);
2374 if (!priv->widget_path)
2377 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2379 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2381 g_warning ("%s: can't get style properties for non-widget class `%s'",
2383 g_type_name (widget_type));
2388 widget_class = g_type_class_ref (widget_type);
2389 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2390 g_type_class_unref (widget_class);
2394 g_warning ("%s: widget class `%s' has no style property named `%s'",
2396 g_type_name (widget_type),
2401 state = gtk_style_context_get_state (context);
2402 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2405 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2406 g_value_copy (peek_value, value);
2407 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2408 g_value_transform (peek_value, value);
2410 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2412 G_VALUE_TYPE_NAME (peek_value),
2413 G_VALUE_TYPE_NAME (value));
2417 * gtk_style_context_get_style_valist:
2418 * @context: a #GtkStyleContext
2419 * @args: va_list of property name/return location pairs, followed by %NULL
2421 * Retrieves several widget style properties from @context according to the
2427 gtk_style_context_get_style_valist (GtkStyleContext *context,
2430 GtkStyleContextPrivate *priv;
2431 const gchar *prop_name;
2432 GtkStateFlags state;
2435 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2437 prop_name = va_arg (args, const gchar *);
2438 priv = context->priv;
2442 widget_type = G_OBJECT_TYPE (priv->widget);
2446 if (!priv->widget_path)
2449 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2451 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2453 g_warning ("%s: can't get style properties for non-widget class `%s'",
2455 g_type_name (widget_type));
2460 state = gtk_style_context_get_state (context);
2464 GtkWidgetClass *widget_class;
2466 const GValue *peek_value;
2469 widget_class = g_type_class_ref (widget_type);
2470 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2471 g_type_class_unref (widget_class);
2475 g_warning ("%s: widget class `%s' has no style property named `%s'",
2477 g_type_name (widget_type),
2482 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2485 G_VALUE_LCOPY (peek_value, args, 0, &error);
2489 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2491 G_VALUE_TYPE_NAME (peek_value),
2497 prop_name = va_arg (args, const gchar *);
2502 * gtk_style_context_get_style:
2503 * @context: a #GtkStyleContext
2504 * @...: property name /return value pairs, followed by %NULL
2506 * Retrieves several widget style properties from @context according to the
2512 gtk_style_context_get_style (GtkStyleContext *context,
2517 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2519 va_start (args, context);
2520 gtk_style_context_get_style_valist (context, args);
2526 * gtk_style_context_lookup_icon_set:
2527 * @context: a #GtkStyleContext
2528 * @stock_id: an icon name
2530 * Looks up @stock_id in the icon factories associated to @context and
2531 * the default icon factory, returning an icon set if found, otherwise
2534 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2537 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2538 const gchar *stock_id)
2540 GtkStyleContextPrivate *priv;
2542 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2543 g_return_val_if_fail (stock_id != NULL, NULL);
2545 priv = context->priv;
2546 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2548 return gtk_icon_factory_lookup_default (stock_id);
2552 * gtk_style_context_set_screen:
2553 * @context: a #GtkStyleContext
2554 * @screen: a #GdkScreen
2556 * Attaches @context to the given screen.
2558 * The screen is used to add style information from 'global' style
2559 * providers, such as the screens #GtkSettings instance.
2561 * If you are using a #GtkStyleContext returned from
2562 * gtk_widget_get_style_context(), you do not need to
2563 * call this yourself.
2568 gtk_style_context_set_screen (GtkStyleContext *context,
2571 GtkStyleContextPrivate *priv;
2573 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2574 g_return_if_fail (GDK_IS_SCREEN (screen));
2576 priv = context->priv;
2577 if (priv->screen == screen)
2580 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2582 gtk_style_context_set_cascade (context, _gtk_style_cascade_get_for_screen (screen));
2586 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2589 priv->screen = screen;
2591 g_object_notify (G_OBJECT (context), "screen");
2595 * gtk_style_context_get_screen:
2596 * @context: a #GtkStyleContext
2598 * Returns the #GdkScreen to which @context is attached.
2600 * Returns: (transfer none): a #GdkScreen.
2603 gtk_style_context_get_screen (GtkStyleContext *context)
2605 GtkStyleContextPrivate *priv;
2607 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2609 priv = context->priv;
2610 return priv->screen;
2614 * gtk_style_context_set_direction:
2615 * @context: a #GtkStyleContext
2616 * @direction: the new direction.
2618 * Sets the reading direction for rendering purposes.
2620 * If you are using a #GtkStyleContext returned from
2621 * gtk_widget_get_style_context(), you do not need to
2622 * call this yourself.
2627 gtk_style_context_set_direction (GtkStyleContext *context,
2628 GtkTextDirection direction)
2630 GtkStateFlags state;
2632 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2634 state = gtk_style_context_get_state (context);
2635 state &= ~(GTK_STATE_FLAG_DIR_LTR | GTK_STATE_FLAG_DIR_RTL);
2639 case GTK_TEXT_DIR_LTR:
2640 state |= GTK_STATE_FLAG_DIR_LTR;
2643 case GTK_TEXT_DIR_RTL:
2644 state |= GTK_STATE_FLAG_DIR_RTL;
2647 case GTK_TEXT_DIR_NONE:
2652 gtk_style_context_set_state (context, state);
2656 * gtk_style_context_get_direction:
2657 * @context: a #GtkStyleContext
2659 * Returns the widget direction used for rendering.
2661 * Returns: the widget direction
2666 gtk_style_context_get_direction (GtkStyleContext *context)
2668 GtkStateFlags state;
2670 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2672 state = gtk_style_context_get_state (context);
2674 if (state & GTK_STATE_FLAG_DIR_LTR)
2675 return GTK_TEXT_DIR_LTR;
2676 else if (state & GTK_STATE_FLAG_DIR_RTL)
2677 return GTK_TEXT_DIR_RTL;
2679 return GTK_TEXT_DIR_NONE;
2683 * gtk_style_context_set_junction_sides:
2684 * @context: a #GtkStyleContext
2685 * @sides: sides where rendered elements are visually connected to
2688 * Sets the sides where rendered elements (mostly through
2689 * gtk_render_frame()) will visually connect with other visual elements.
2691 * This is merely a hint that may or may not be honored
2692 * by theming engines.
2694 * Container widgets are expected to set junction hints as appropriate
2695 * for their children, so it should not normally be necessary to call
2696 * this function manually.
2701 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2702 GtkJunctionSides sides)
2704 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2706 context->priv->info->junction_sides = sides;
2710 * gtk_style_context_get_junction_sides:
2711 * @context: a #GtkStyleContext
2713 * Returns the sides where rendered elements connect visually with others.
2715 * Returns: the junction sides
2720 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2722 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2724 return context->priv->info->junction_sides;
2728 _gtk_style_context_resolve_color (GtkStyleContext *context,
2731 GtkCssDependencies *dependencies)
2735 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2736 g_return_val_if_fail (color != NULL, FALSE);
2737 g_return_val_if_fail (result != NULL, FALSE);
2739 val = _gtk_css_color_value_resolve (color,
2740 GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade),
2741 _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR),
2742 GTK_CSS_DEPENDS_ON_COLOR,
2747 *result = *_gtk_css_rgba_value_get_rgba (val);
2748 _gtk_css_value_unref (val);
2753 * gtk_style_context_lookup_color:
2754 * @context: a #GtkStyleContext
2755 * @color_name: color name to lookup
2756 * @color: (out): Return location for the looked up color
2758 * Looks up and resolves a color name in the @context color map.
2760 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2763 gtk_style_context_lookup_color (GtkStyleContext *context,
2764 const gchar *color_name,
2769 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2770 g_return_val_if_fail (color_name != NULL, FALSE);
2771 g_return_val_if_fail (color != NULL, FALSE);
2773 value = _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), color_name);
2777 return _gtk_style_context_resolve_color (context, value, color, NULL);
2781 * gtk_style_context_notify_state_change:
2782 * @context: a #GtkStyleContext
2783 * @window: a #GdkWindow
2784 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2785 * See gtk_style_context_push_animatable_region()
2786 * @state: state to trigger transition for
2787 * @state_value: %TRUE if @state is the state we are changing to,
2788 * %FALSE if we are changing away from it
2790 * Notifies a state change on @context, so if the current style makes use
2791 * of transition animations, one will be started so all rendered elements
2792 * under @region_id are animated for state @state being set to value
2795 * The @window parameter is used in order to invalidate the rendered area
2796 * as the animation runs, so make sure it is the same window that is being
2797 * rendered on by the gtk_render_*() functions.
2799 * If @region_id is %NULL, all rendered elements using @context will be
2800 * affected by this state transition.
2802 * As a practical example, a #GtkButton notifying a state transition on
2803 * the prelight state:
2805 * gtk_style_context_notify_state_change (context,
2806 * gtk_widget_get_window (widget),
2808 * GTK_STATE_PRELIGHT,
2809 * button->in_button);
2812 * Can be handled in the CSS file like this:
2815 * background-color: #f00
2819 * background-color: #fff;
2820 * transition: 200ms linear
2824 * This combination will animate the button background from red to white
2825 * if a pointer enters the button, and back to red if the pointer leaves
2828 * Note that @state is used when finding the transition parameters, which
2829 * is why the style places the transition under the :hover pseudo-class.
2833 * Deprecated: 3.6: This function does nothing.
2836 gtk_style_context_notify_state_change (GtkStyleContext *context,
2840 gboolean state_value)
2842 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2843 g_return_if_fail (GDK_IS_WINDOW (window));
2844 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2845 g_return_if_fail (context->priv->widget != NULL || context->priv->widget_path != NULL);
2849 * gtk_style_context_cancel_animations:
2850 * @context: a #GtkStyleContext
2851 * @region_id: (allow-none): animatable region to stop, or %NULL.
2852 * See gtk_style_context_push_animatable_region()
2854 * Stops all running animations for @region_id and all animatable
2855 * regions underneath.
2857 * A %NULL @region_id will stop all ongoing animations in @context,
2858 * when dealing with a #GtkStyleContext obtained through
2859 * gtk_widget_get_style_context(), this is normally done for you
2860 * in all circumstances you would expect all widget to be stopped,
2861 * so this should be only used in complex widgets with different
2862 * animatable regions.
2866 * Deprecated: 3.6: This function does nothing.
2869 gtk_style_context_cancel_animations (GtkStyleContext *context,
2872 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2876 * gtk_style_context_scroll_animations:
2877 * @context: a #GtkStyleContext
2878 * @window: a #GdkWindow used previously in
2879 * gtk_style_context_notify_state_change()
2880 * @dx: Amount to scroll in the X axis
2881 * @dy: Amount to scroll in the Y axis
2883 * This function is analogous to gdk_window_scroll(), and
2884 * should be called together with it so the invalidation
2885 * areas for any ongoing animation are scrolled together
2890 * Deprecated: 3.6: This function does nothing.
2893 gtk_style_context_scroll_animations (GtkStyleContext *context,
2898 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2899 g_return_if_fail (GDK_IS_WINDOW (window));
2903 * gtk_style_context_push_animatable_region:
2904 * @context: a #GtkStyleContext
2905 * @region_id: unique identifier for the animatable region
2907 * Pushes an animatable region, so all further gtk_render_*() calls between
2908 * this call and the following gtk_style_context_pop_animatable_region()
2909 * will potentially show transition animations for this region if
2910 * gtk_style_context_notify_state_change() is called for a given state,
2911 * and the current theme/style defines transition animations for state
2914 * The @region_id used must be unique in @context so the theming engine
2915 * can uniquely identify rendered elements subject to a state transition.
2919 * Deprecated: 3.6: This function does nothing.
2922 gtk_style_context_push_animatable_region (GtkStyleContext *context,
2925 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2926 g_return_if_fail (region_id != NULL);
2930 * gtk_style_context_pop_animatable_region:
2931 * @context: a #GtkStyleContext
2933 * Pops an animatable region from @context.
2934 * See gtk_style_context_push_animatable_region().
2938 * Deprecated: 3.6: This function does nothing.
2941 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
2943 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2947 gtk_style_context_clear_cache (GtkStyleContext *context)
2949 GtkStyleContextPrivate *priv;
2952 priv = context->priv;
2954 for (info = priv->info; info; info = info->next)
2956 style_info_set_data (info, NULL);
2958 g_hash_table_remove_all (priv->style_data);
2962 gtk_style_context_update_cache (GtkStyleContext *context,
2963 const GtkBitmask *parent_changes)
2965 GtkStyleContextPrivate *priv;
2966 GHashTableIter iter;
2967 gpointer key, value;
2969 if (_gtk_bitmask_is_empty (parent_changes))
2972 priv = context->priv;
2974 g_hash_table_iter_init (&iter, priv->style_data);
2975 while (g_hash_table_iter_next (&iter, &key, &value))
2977 GtkStyleInfo *info = key;
2978 StyleData *data = value;
2979 GtkBitmask *changes;
2981 changes = _gtk_css_computed_values_compute_dependencies (data->store, parent_changes);
2983 if (!_gtk_bitmask_is_empty (changes))
2984 build_properties (context, data->store, info, changes);
2986 _gtk_bitmask_free (changes);
2991 gtk_style_context_do_invalidate (GtkStyleContext *context,
2992 const GtkBitmask *changes)
2994 GtkStyleContextPrivate *priv;
2996 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2998 priv = context->priv;
3000 /* Avoid reentrancy */
3001 if (priv->invalidating_context)
3004 priv->invalidating_context = changes;
3006 g_signal_emit (context, signals[CHANGED], 0);
3008 priv->invalidating_context = NULL;
3012 gtk_style_context_update_animations (GtkStyleContext *context,
3015 GtkBitmask *differences;
3016 StyleData *style_data;
3018 style_data = style_data_lookup (context);
3020 differences = _gtk_css_computed_values_advance (style_data->store,
3023 if (_gtk_css_computed_values_is_static (style_data->store))
3024 _gtk_style_context_update_animating (context);
3030 gtk_style_context_needs_full_revalidate (GtkStyleContext *context,
3031 GtkCssChange change)
3033 GtkStyleContextPrivate *priv = context->priv;
3035 /* Try to avoid invalidating if we can */
3036 if (change & GTK_STYLE_CONTEXT_RADICAL_CHANGE)
3038 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
3042 if (priv->relevant_changes == GTK_CSS_CHANGE_ANY)
3044 GtkWidgetPath *path;
3045 GtkCssMatcher matcher, superset;
3047 path = create_query_path (context, priv->info);
3048 if (_gtk_css_matcher_init (&matcher, path, priv->info->state_flags))
3050 _gtk_css_matcher_superset_init (&superset, &matcher, GTK_STYLE_CONTEXT_RADICAL_CHANGE & ~GTK_CSS_CHANGE_SOURCE);
3051 priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3055 priv->relevant_changes = 0;
3057 priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
3059 gtk_widget_path_unref (path);
3063 if (priv->relevant_changes & change)
3070 gtk_style_context_should_create_transitions (GtkStyleContext *context)
3072 GtkStyleContextPrivate *priv;
3075 priv = context->priv;
3077 if (priv->widget == NULL)
3080 if (!gtk_widget_get_mapped (priv->widget))
3083 g_object_get (gtk_widget_get_settings (context->priv->widget),
3084 "gtk-enable-animations", &animate,
3091 _gtk_style_context_validate (GtkStyleContext *context,
3093 GtkCssChange change,
3094 const GtkBitmask *parent_changes)
3096 GtkStyleContextPrivate *priv;
3099 GtkBitmask *changes;
3102 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3104 priv = context->priv;
3106 change |= priv->pending_changes;
3108 /* If you run your application with
3109 * GTK_DEBUG=no-css-cache
3110 * every invalidation will purge the cache and completely query
3111 * everything anew form the cache. This is slow (in particular
3112 * when animating), but useful for figuring out bugs.
3114 * We achieve that by pretending that everything that could have
3115 * changed has and so we of course totally need to redo everything.
3117 * Note that this also completely revalidates child widgets all
3120 if (G_UNLIKELY (gtk_get_debug_flags () & GTK_DEBUG_NO_CSS_CACHE))
3121 change = GTK_CSS_CHANGE_ANY;
3123 if (!priv->invalid && change == 0 && _gtk_bitmask_is_empty (parent_changes))
3126 priv->pending_changes = 0;
3127 gtk_style_context_set_invalid (context, FALSE);
3131 current = style_data_ref (info->data);
3135 /* Try to avoid invalidating if we can */
3136 if (current == NULL ||
3137 gtk_style_context_needs_full_revalidate (context, change))
3141 if ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE)
3143 gtk_style_context_clear_cache (context);
3147 gtk_style_context_update_cache (context, parent_changes);
3148 style_info_set_data (info, NULL);
3151 data = style_data_lookup (context);
3153 _gtk_css_computed_values_create_animations (data->store,
3154 priv->parent ? style_data_lookup (priv->parent)->store : NULL,
3156 GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3157 current && gtk_style_context_should_create_transitions (context) ? current->store : NULL);
3158 if (_gtk_css_computed_values_is_static (data->store))
3159 change &= ~GTK_CSS_CHANGE_ANIMATE;
3161 change |= GTK_CSS_CHANGE_ANIMATE;
3162 _gtk_style_context_update_animating (context);
3166 changes = _gtk_css_computed_values_get_difference (data->store, current->store);
3168 /* In the case where we keep the cache, we want unanimated values */
3169 _gtk_css_computed_values_cancel_animations (current->store);
3173 changes = _gtk_bitmask_new ();
3174 changes = _gtk_bitmask_invert_range (changes, 0, _gtk_css_style_property_get_n_properties ());
3179 changes = _gtk_css_computed_values_compute_dependencies (current->store, parent_changes);
3181 gtk_style_context_update_cache (context, parent_changes);
3185 style_data_unref (current);
3187 if (change & GTK_CSS_CHANGE_ANIMATE &&
3188 gtk_style_context_is_animating (context))
3190 GtkBitmask *animation_changes;
3192 animation_changes = gtk_style_context_update_animations (context, timestamp);
3193 changes = _gtk_bitmask_union (changes, animation_changes);
3194 _gtk_bitmask_free (animation_changes);
3197 if (!_gtk_bitmask_is_empty (changes) || (change & GTK_CSS_CHANGE_FORCE_INVALIDATE))
3198 gtk_style_context_do_invalidate (context, changes);
3200 change = _gtk_css_change_for_child (change);
3201 for (list = priv->children; list; list = list->next)
3203 _gtk_style_context_validate (list->data, timestamp, change, changes);
3206 _gtk_bitmask_free (changes);
3210 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3211 GtkCssChange change)
3213 GtkStyleContextPrivate *priv;
3215 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3216 g_return_if_fail (change != 0);
3218 priv = context->priv;
3220 if (priv->widget != NULL)
3222 priv->pending_changes |= change;
3223 gtk_style_context_set_invalid (context, TRUE);
3225 else if (priv->widget_path == NULL)
3227 gtk_style_context_invalidate (context);
3232 * gtk_style_context_invalidate:
3233 * @context: a #GtkStyleContext.
3235 * Invalidates @context style information, so it will be reconstructed
3238 * If you're using a #GtkStyleContext returned from
3239 * gtk_widget_get_style_context(), you do not need to
3240 * call this yourself.
3245 gtk_style_context_invalidate (GtkStyleContext *context)
3247 GtkBitmask *changes;
3249 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3251 gtk_style_context_clear_cache (context);
3253 changes = _gtk_bitmask_new ();
3254 changes = _gtk_bitmask_invert_range (changes,
3256 _gtk_css_style_property_get_n_properties ());
3257 gtk_style_context_do_invalidate (context, changes);
3258 _gtk_bitmask_free (changes);
3262 corner_value_is_right_angle (GtkCssValue *value)
3264 return _gtk_css_corner_value_get_x (value, 100) <= 0.0 &&
3265 _gtk_css_corner_value_get_y (value, 100) <= 0.0;
3269 * gtk_style_context_set_background:
3270 * @context: a #GtkStyleContext
3271 * @window: a #GdkWindow
3273 * Sets the background of @window to the background pattern or
3274 * color specified in @context for its current state.
3279 gtk_style_context_set_background (GtkStyleContext *context,
3282 const GdkRGBA *color;
3284 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3285 g_return_if_fail (GDK_IS_WINDOW (window));
3287 /* This is a sophisitcated optimization.
3288 * If we know the GDK window's background will be opaque, we mark
3289 * it as opaque. This is so GDK can do all the optimizations it does
3290 * for opaque windows and be fast.
3291 * This is mainly used when scrolling.
3293 * We could indeed just set black instead of the color we have.
3295 color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
3297 if (color->alpha >= 1.0 &&
3298 corner_value_is_right_angle (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_TOP_LEFT_RADIUS)) &&
3299 corner_value_is_right_angle (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_TOP_RIGHT_RADIUS)) &&
3300 corner_value_is_right_angle (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_BOTTOM_RIGHT_RADIUS)) &&
3301 corner_value_is_right_angle (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BORDER_BOTTOM_LEFT_RADIUS)))
3303 gdk_window_set_background_rgba (window, color);
3307 GdkRGBA transparent = { 0.0, 0.0, 0.0, 0.0 };
3308 gdk_window_set_background_rgba (window, &transparent);
3313 * gtk_style_context_get_color:
3314 * @context: a #GtkStyleContext
3315 * @state: state to retrieve the color for
3316 * @color: (out): return value for the foreground color
3318 * Gets the foreground color for a given state.
3323 gtk_style_context_get_color (GtkStyleContext *context,
3324 GtkStateFlags state,
3329 g_return_if_fail (color != NULL);
3330 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3332 gtk_style_context_get (context,
3342 * gtk_style_context_get_background_color:
3343 * @context: a #GtkStyleContext
3344 * @state: state to retrieve the color for
3345 * @color: (out): return value for the background color
3347 * Gets the background color for a given state.
3352 gtk_style_context_get_background_color (GtkStyleContext *context,
3353 GtkStateFlags state,
3358 g_return_if_fail (color != NULL);
3359 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3361 gtk_style_context_get (context,
3363 "background-color", &c,
3371 * gtk_style_context_get_border_color:
3372 * @context: a #GtkStyleContext
3373 * @state: state to retrieve the color for
3374 * @color: (out): return value for the border color
3376 * Gets the border color for a given state.
3381 gtk_style_context_get_border_color (GtkStyleContext *context,
3382 GtkStateFlags state,
3387 g_return_if_fail (color != NULL);
3388 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3390 gtk_style_context_get (context,
3400 * gtk_style_context_get_border:
3401 * @context: a #GtkStyleContext
3402 * @state: state to retrieve the border for
3403 * @border: (out): return value for the border settings
3405 * Gets the border for a given state as a #GtkBorder.
3406 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3411 gtk_style_context_get_border (GtkStyleContext *context,
3412 GtkStateFlags state,
3415 int top, left, bottom, right;
3417 g_return_if_fail (border != NULL);
3418 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3420 gtk_style_context_get (context,
3422 "border-top-width", &top,
3423 "border-left-width", &left,
3424 "border-bottom-width", &bottom,
3425 "border-right-width", &right,
3429 border->left = left;
3430 border->bottom = bottom;
3431 border->right = right;
3435 * gtk_style_context_get_padding:
3436 * @context: a #GtkStyleContext
3437 * @state: state to retrieve the padding for
3438 * @padding: (out): return value for the padding settings
3440 * Gets the padding for a given state as a #GtkBorder.
3441 * See %GTK_STYLE_PROPERTY_PADDING.
3446 gtk_style_context_get_padding (GtkStyleContext *context,
3447 GtkStateFlags state,
3450 int top, left, bottom, right;
3452 g_return_if_fail (padding != NULL);
3453 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3455 gtk_style_context_get (context,
3457 "padding-top", &top,
3458 "padding-left", &left,
3459 "padding-bottom", &bottom,
3460 "padding-right", &right,
3464 padding->left = left;
3465 padding->bottom = bottom;
3466 padding->right = right;
3470 * gtk_style_context_get_margin:
3471 * @context: a #GtkStyleContext
3472 * @state: state to retrieve the border for
3473 * @margin: (out): return value for the margin settings
3475 * Gets the margin for a given state as a #GtkBorder.
3476 * See %GTK_STYLE_PROPERTY_MARGIN.
3481 gtk_style_context_get_margin (GtkStyleContext *context,
3482 GtkStateFlags state,
3485 int top, left, bottom, right;
3487 g_return_if_fail (margin != NULL);
3488 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3490 gtk_style_context_get (context,
3493 "margin-left", &left,
3494 "margin-bottom", &bottom,
3495 "margin-right", &right,
3499 margin->left = left;
3500 margin->bottom = bottom;
3501 margin->right = right;
3505 * gtk_style_context_get_font:
3506 * @context: a #GtkStyleContext
3507 * @state: state to retrieve the font for
3509 * Returns the font description for a given state. The returned
3510 * object is const and will remain valid until the
3511 * #GtkStyleContext::changed signal happens.
3513 * Returns: (transfer none): the #PangoFontDescription for the given
3514 * state. This object is owned by GTK+ and should not be
3519 * Deprecated: 3.8: Use gtk_style_context_get() for "font" or
3520 * subproperties instead.
3522 const PangoFontDescription *
3523 gtk_style_context_get_font (GtkStyleContext *context,
3524 GtkStateFlags state)
3526 GtkStyleContextPrivate *priv;
3528 PangoFontDescription *description;
3530 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3532 priv = context->priv;
3533 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3535 data = style_data_lookup_for_state (context, state);
3537 /* Yuck, fonts are created on-demand but we don't return a ref.
3538 * Do bad things to achieve this requirement */
3539 gtk_style_context_get (context, state, "font", &description, NULL);
3540 g_object_set_data_full (G_OBJECT (data->store),
3541 "font-cache-for-get_font",
3543 (GDestroyNotify) pango_font_description_free);
3549 get_cursor_color (GtkStyleContext *context,
3553 GdkColor *style_color;
3555 gtk_style_context_get_style (context,
3556 primary ? "cursor-color" : "secondary-cursor-color",
3562 color->red = style_color->red / 65535.0;
3563 color->green = style_color->green / 65535.0;
3564 color->blue = style_color->blue / 65535.0;
3567 gdk_color_free (style_color);
3571 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3577 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3579 color->red = (color->red + bg.red) * 0.5;
3580 color->green = (color->green + bg.green) * 0.5;
3581 color->blue = (color->blue + bg.blue) * 0.5;
3587 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3588 GdkRGBA *primary_color,
3589 GdkRGBA *secondary_color)
3592 get_cursor_color (context, TRUE, primary_color);
3594 if (secondary_color)
3595 get_cursor_color (context, FALSE, secondary_color);
3602 * @context: a #GtkStyleContext
3604 * @x: X origin of the rectangle
3605 * @y: Y origin of the rectangle
3606 * @width: rectangle width
3607 * @height: rectangle height
3609 * Renders a checkmark (as in a #GtkCheckButton).
3611 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3612 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3613 * should be marked as undefined.
3616 * <title>Typical checkmark rendering</title>
3617 * <inlinegraphic fileref="checks.png" format="PNG"/>
3623 gtk_render_check (GtkStyleContext *context,
3630 GtkThemingEngineClass *engine_class;
3631 GtkThemingEngine *engine;
3633 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3634 g_return_if_fail (cr != NULL);
3636 if (width <= 0 || height <= 0)
3639 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3640 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3645 _gtk_theming_engine_set_context (engine, context);
3646 engine_class->render_check (engine, cr,
3647 x, y, width, height);
3653 * gtk_render_option:
3654 * @context: a #GtkStyleContext
3656 * @x: X origin of the rectangle
3657 * @y: Y origin of the rectangle
3658 * @width: rectangle width
3659 * @height: rectangle height
3661 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3662 * state will determine whether the option is on or off, and
3663 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3666 * <title>Typical option mark rendering</title>
3667 * <inlinegraphic fileref="options.png" format="PNG"/>
3673 gtk_render_option (GtkStyleContext *context,
3680 GtkThemingEngineClass *engine_class;
3681 GtkThemingEngine *engine;
3683 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3684 g_return_if_fail (cr != NULL);
3686 if (width <= 0 || height <= 0)
3689 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3690 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3694 _gtk_theming_engine_set_context (engine, context);
3695 engine_class->render_option (engine, cr,
3696 x, y, width, height);
3703 * @context: a #GtkStyleContext
3705 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3706 * @x: X origin of the render area
3707 * @y: Y origin of the render area
3708 * @size: square side for render area
3710 * Renders an arrow pointing to @angle.
3713 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3714 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3720 gtk_render_arrow (GtkStyleContext *context,
3727 GtkThemingEngineClass *engine_class;
3728 GtkThemingEngine *engine;
3730 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3731 g_return_if_fail (cr != NULL);
3736 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3737 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3741 gtk_style_context_save (context);
3742 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3744 _gtk_theming_engine_set_context (engine, context);
3745 engine_class->render_arrow (engine, cr,
3748 gtk_style_context_restore (context);
3753 * gtk_render_background:
3754 * @context: a #GtkStyleContext
3756 * @x: X origin of the rectangle
3757 * @y: Y origin of the rectangle
3758 * @width: rectangle width
3759 * @height: rectangle height
3761 * Renders the background of an element.
3764 * <title>Typical background rendering, showing the effect of
3765 * <parameter>background-image</parameter>,
3766 * <parameter>border-width</parameter> and
3767 * <parameter>border-radius</parameter></title>
3768 * <inlinegraphic fileref="background.png" format="PNG"/>
3774 gtk_render_background (GtkStyleContext *context,
3781 GtkThemingEngineClass *engine_class;
3782 GtkThemingEngine *engine;
3784 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3785 g_return_if_fail (cr != NULL);
3787 if (width <= 0 || height <= 0)
3790 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3791 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3795 _gtk_theming_engine_set_context (engine, context);
3796 engine_class->render_background (engine, cr, x, y, width, height);
3803 * @context: a #GtkStyleContext
3805 * @x: X origin of the rectangle
3806 * @y: Y origin of the rectangle
3807 * @width: rectangle width
3808 * @height: rectangle height
3810 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3813 * <title>Examples of frame rendering, showing the effect of
3814 * <parameter>border-image</parameter>,
3815 * <parameter>border-color</parameter>,
3816 * <parameter>border-width</parameter>,
3817 * <parameter>border-radius</parameter> and
3819 * <inlinegraphic fileref="frames.png" format="PNG"/>
3825 gtk_render_frame (GtkStyleContext *context,
3832 GtkThemingEngineClass *engine_class;
3833 GtkThemingEngine *engine;
3835 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3836 g_return_if_fail (cr != NULL);
3838 if (width <= 0 || height <= 0)
3841 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3842 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3846 _gtk_theming_engine_set_context (engine, context);
3847 engine_class->render_frame (engine, cr, x, y, width, height);
3853 * gtk_render_expander:
3854 * @context: a #GtkStyleContext
3856 * @x: X origin of the rectangle
3857 * @y: Y origin of the rectangle
3858 * @width: rectangle width
3859 * @height: rectangle height
3861 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3862 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3863 * determines whether the expander is collapsed or expanded.
3866 * <title>Typical expander rendering</title>
3867 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3873 gtk_render_expander (GtkStyleContext *context,
3880 GtkThemingEngineClass *engine_class;
3881 GtkThemingEngine *engine;
3883 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3884 g_return_if_fail (cr != NULL);
3886 if (width <= 0 || height <= 0)
3889 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3890 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3894 _gtk_theming_engine_set_context (engine, context);
3895 engine_class->render_expander (engine, cr, x, y, width, height);
3902 * @context: a #GtkStyleContext
3904 * @x: X origin of the rectangle
3905 * @y: Y origin of the rectangle
3906 * @width: rectangle width
3907 * @height: rectangle height
3909 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3911 * <title>Typical focus rendering</title>
3912 * <inlinegraphic fileref="focus.png" format="PNG"/>
3918 gtk_render_focus (GtkStyleContext *context,
3925 GtkThemingEngineClass *engine_class;
3926 GtkThemingEngine *engine;
3928 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3929 g_return_if_fail (cr != NULL);
3931 if (width <= 0 || height <= 0)
3934 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3935 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3939 _gtk_theming_engine_set_context (engine, context);
3940 engine_class->render_focus (engine, cr, x, y, width, height);
3946 * gtk_render_layout:
3947 * @context: a #GtkStyleContext
3951 * @layout: the #PangoLayout to render
3953 * Renders @layout on the coordinates @x, @y
3958 gtk_render_layout (GtkStyleContext *context,
3962 PangoLayout *layout)
3964 GtkThemingEngineClass *engine_class;
3965 GtkThemingEngine *engine;
3966 PangoRectangle extents;
3968 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3969 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3970 g_return_if_fail (cr != NULL);
3972 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3973 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3977 pango_layout_get_extents (layout, &extents, NULL);
3979 _gtk_theming_engine_set_context (engine, context);
3980 engine_class->render_layout (engine, cr, x, y, layout);
3987 * @context: a #GtkStyleContext
3989 * @x0: X coordinate for the origin of the line
3990 * @y0: Y coordinate for the origin of the line
3991 * @x1: X coordinate for the end of the line
3992 * @y1: Y coordinate for the end of the line
3994 * Renders a line from (x0, y0) to (x1, y1).
3999 gtk_render_line (GtkStyleContext *context,
4006 GtkThemingEngineClass *engine_class;
4007 GtkThemingEngine *engine;
4009 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4010 g_return_if_fail (cr != NULL);
4012 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4013 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4017 _gtk_theming_engine_set_context (engine, context);
4018 engine_class->render_line (engine, cr, x0, y0, x1, y1);
4024 * gtk_render_slider:
4025 * @context: a #GtkStyleContext
4027 * @x: X origin of the rectangle
4028 * @y: Y origin of the rectangle
4029 * @width: rectangle width
4030 * @height: rectangle height
4031 * @orientation: orientation of the slider
4033 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4034 * @width, @height. @orientation defines whether the slider is vertical
4038 * <title>Typical slider rendering</title>
4039 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4045 gtk_render_slider (GtkStyleContext *context,
4051 GtkOrientation orientation)
4053 GtkThemingEngineClass *engine_class;
4054 GtkThemingEngine *engine;
4056 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4057 g_return_if_fail (cr != NULL);
4059 if (width <= 0 || height <= 0)
4062 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4063 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4067 _gtk_theming_engine_set_context (engine, context);
4068 engine_class->render_slider (engine, cr, x, y, width, height, orientation);
4074 * gtk_render_frame_gap:
4075 * @context: a #GtkStyleContext
4077 * @x: X origin of the rectangle
4078 * @y: Y origin of the rectangle
4079 * @width: rectangle width
4080 * @height: rectangle height
4081 * @gap_side: side where the gap is
4082 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4083 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4085 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4086 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4087 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4088 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4091 * <title>Typical rendering of a frame with a gap</title>
4092 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4098 gtk_render_frame_gap (GtkStyleContext *context,
4104 GtkPositionType gap_side,
4108 GtkThemingEngineClass *engine_class;
4109 GtkThemingEngine *engine;
4111 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4112 g_return_if_fail (cr != NULL);
4113 g_return_if_fail (xy0_gap <= xy1_gap);
4114 g_return_if_fail (xy0_gap >= 0);
4116 if (width <= 0 || height <= 0)
4119 if (gap_side == GTK_POS_LEFT ||
4120 gap_side == GTK_POS_RIGHT)
4121 g_return_if_fail (xy1_gap <= height);
4123 g_return_if_fail (xy1_gap <= width);
4125 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4126 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4130 _gtk_theming_engine_set_context (engine, context);
4131 engine_class->render_frame_gap (engine, cr,
4132 x, y, width, height, gap_side,
4139 * gtk_render_extension:
4140 * @context: a #GtkStyleContext
4142 * @x: X origin of the rectangle
4143 * @y: Y origin of the rectangle
4144 * @width: rectangle width
4145 * @height: rectangle height
4146 * @gap_side: side where the gap is
4148 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4149 * defined by @x, @y, @width, @height. The side where the extension
4150 * connects to is defined by @gap_side.
4153 * <title>Typical extension rendering</title>
4154 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4160 gtk_render_extension (GtkStyleContext *context,
4166 GtkPositionType gap_side)
4168 GtkThemingEngineClass *engine_class;
4169 GtkThemingEngine *engine;
4171 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4172 g_return_if_fail (cr != NULL);
4174 if (width <= 0 || height <= 0)
4177 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4178 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4182 _gtk_theming_engine_set_context (engine, context);
4183 engine_class->render_extension (engine, cr, x, y, width, height, gap_side);
4189 * gtk_render_handle:
4190 * @context: a #GtkStyleContext
4192 * @x: X origin of the rectangle
4193 * @y: Y origin of the rectangle
4194 * @width: rectangle width
4195 * @height: rectangle height
4197 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4198 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4199 * determined by @x, @y, @width, @height.
4202 * <title>Handles rendered for the paned and grip classes</title>
4203 * <inlinegraphic fileref="handles.png" format="PNG"/>
4209 gtk_render_handle (GtkStyleContext *context,
4216 GtkThemingEngineClass *engine_class;
4217 GtkThemingEngine *engine;
4219 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4220 g_return_if_fail (cr != NULL);
4222 if (width <= 0 || height <= 0)
4225 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4226 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4230 _gtk_theming_engine_set_context (engine, context);
4231 engine_class->render_handle (engine, cr, x, y, width, height);
4237 * gtk_render_activity:
4238 * @context: a #GtkStyleContext
4240 * @x: X origin of the rectangle
4241 * @y: Y origin of the rectangle
4242 * @width: rectangle width
4243 * @height: rectangle height
4245 * Renders an activity area (Such as in #GtkSpinner or the
4246 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4247 * determines whether there is activity going on.
4252 gtk_render_activity (GtkStyleContext *context,
4259 GtkThemingEngineClass *engine_class;
4260 GtkThemingEngine *engine;
4262 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4263 g_return_if_fail (cr != NULL);
4265 if (width <= 0 || height <= 0)
4268 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4269 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4273 _gtk_theming_engine_set_context (engine, context);
4274 engine_class->render_activity (engine, cr, x, y, width, height);
4280 * gtk_render_icon_pixbuf:
4281 * @context: a #GtkStyleContext
4282 * @source: the #GtkIconSource specifying the icon to render
4283 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4284 * means render at the size of the source and don't scale.
4286 * Renders the icon specified by @source at the given @size, returning the result
4289 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4294 gtk_render_icon_pixbuf (GtkStyleContext *context,
4295 const GtkIconSource *source,
4298 GtkThemingEngineClass *engine_class;
4299 GtkThemingEngine *engine;
4301 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4302 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4303 g_return_val_if_fail (source != NULL, NULL);
4305 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4306 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4308 _gtk_theming_engine_set_context (engine, context);
4309 return engine_class->render_icon_pixbuf (engine, source, size);
4314 * @context: a #GtkStyleContext
4316 * @pixbuf: a #GdkPixbuf containing the icon to draw
4317 * @x: X position for the @pixbuf
4318 * @y: Y position for the @pixbuf
4320 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4325 gtk_render_icon (GtkStyleContext *context,
4331 GtkThemingEngineClass *engine_class;
4332 GtkThemingEngine *engine;
4334 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4335 g_return_if_fail (cr != NULL);
4337 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4338 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4342 _gtk_theming_engine_set_context (engine, context);
4343 engine_class->render_icon (engine, cr, pixbuf, x, y);
4349 draw_insertion_cursor (GtkStyleContext *context,
4354 gboolean is_primary,
4355 PangoDirection direction,
4356 gboolean draw_arrow)
4359 GdkRGBA primary_color;
4360 GdkRGBA secondary_color;
4361 gfloat cursor_aspect_ratio;
4367 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4368 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4370 /* When changing the shape or size of the cursor here,
4371 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4374 gtk_style_context_get_style (context,
4375 "cursor-aspect-ratio", &cursor_aspect_ratio,
4378 stem_width = height * cursor_aspect_ratio + 1;
4380 /* put (stem_width % 2) on the proper side of the cursor */
4381 if (direction == PANGO_DIRECTION_LTR)
4382 offset = stem_width / 2;
4384 offset = stem_width - stem_width / 2;
4386 cairo_rectangle (cr, x - offset, y, stem_width, height);
4394 arrow_width = stem_width + 1;
4396 if (direction == PANGO_DIRECTION_RTL)
4398 ax = x - offset - 1;
4399 ay = y + height - arrow_width * 2 - arrow_width + 1;
4401 cairo_move_to (cr, ax, ay + 1);
4402 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4403 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4406 else if (direction == PANGO_DIRECTION_LTR)
4408 ax = x + stem_width - offset;
4409 ay = y + height - arrow_width * 2 - arrow_width + 1;
4411 cairo_move_to (cr, ax, ay + 1);
4412 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4413 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4417 g_assert_not_reached();
4424 * gtk_render_insertion_cursor:
4425 * @context: a #GtkStyleContext
4429 * @layout: the #PangoLayout of the text
4430 * @index: the index in the #PangoLayout
4431 * @direction: the #PangoDirection of the text
4433 * Draws a text caret on @cr at the specified index of @layout.
4438 gtk_render_insertion_cursor (GtkStyleContext *context,
4442 PangoLayout *layout,
4444 PangoDirection direction)
4446 GtkStyleContextPrivate *priv;
4447 gboolean split_cursor;
4448 PangoRectangle strong_pos, weak_pos;
4449 PangoRectangle *cursor1, *cursor2;
4450 PangoDirection keymap_direction;
4451 PangoDirection direction2;
4453 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4454 g_return_if_fail (cr != NULL);
4455 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4456 g_return_if_fail (index >= 0);
4458 priv = context->priv;
4460 g_object_get (gtk_settings_get_for_screen (priv->screen),
4461 "gtk-split-cursor", &split_cursor,
4464 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4466 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4468 direction2 = PANGO_DIRECTION_NEUTRAL;
4472 cursor1 = &strong_pos;
4474 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4476 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4477 cursor2 = &weak_pos;
4482 if (keymap_direction == direction)
4483 cursor1 = &strong_pos;
4485 cursor1 = &weak_pos;
4488 draw_insertion_cursor (context,
4490 x + PANGO_PIXELS (cursor1->x),
4491 y + PANGO_PIXELS (cursor1->y),
4492 PANGO_PIXELS (cursor1->height),
4495 direction2 != PANGO_DIRECTION_NEUTRAL);
4497 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4499 draw_insertion_cursor (context,
4501 x + PANGO_PIXELS (cursor2->x),
4502 y + PANGO_PIXELS (cursor2->y),
4503 PANGO_PIXELS (cursor2->height),
4511 * gtk_draw_insertion_cursor:
4512 * @widget: a #GtkWidget
4513 * @cr: cairo context to draw to
4514 * @location: location where to draw the cursor (@location->width is ignored)
4515 * @is_primary: if the cursor should be the primary cursor color.
4516 * @direction: whether the cursor is left-to-right or
4517 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4518 * @draw_arrow: %TRUE to draw a directional arrow on the
4519 * cursor. Should be %FALSE unless the cursor is split.
4521 * Draws a text caret on @cr at @location. This is not a style function
4522 * but merely a convenience function for drawing the standard cursor shape.
4525 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4528 gtk_draw_insertion_cursor (GtkWidget *widget,
4530 const GdkRectangle *location,
4531 gboolean is_primary,
4532 GtkTextDirection direction,
4533 gboolean draw_arrow)
4535 GtkStyleContext *context;
4537 g_return_if_fail (GTK_IS_WIDGET (widget));
4538 g_return_if_fail (cr != NULL);
4539 g_return_if_fail (location != NULL);
4540 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4542 context = gtk_widget_get_style_context (widget);
4544 draw_insertion_cursor (context, cr,
4545 location->x, location->y, location->height,
4547 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4552 * _gtk_style_context_get_changes:
4553 * @context: the context to query
4555 * Queries the context for the changes for the currently executing
4556 * GtkStyleContext::invalidate signal. If no signal is currently
4557 * emitted, this function returns %NULL.
4559 * FIXME 4.0: Make this part of the signal.
4561 * Returns: %NULL or the currently invalidating changes
4564 _gtk_style_context_get_changes (GtkStyleContext *context)
4566 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4568 return context->priv->invalidating_context;
4571 static AtkAttributeSet *
4572 add_attribute (AtkAttributeSet *attributes,
4573 AtkTextAttribute attr,
4578 at = g_new (AtkAttribute, 1);
4579 at->name = g_strdup (atk_text_attribute_get_name (attr));
4580 at->value = g_strdup (value);
4582 return g_slist_prepend (attributes, at);
4586 * _gtk_style_context_get_attributes:
4587 * @attributes: a #AtkAttributeSet to add attributes to
4588 * @context: the #GtkStyleContext to get attributes from
4589 * @flags: the state to use with @context
4591 * Adds the foreground and background color from @context to
4592 * @attributes, after translating them to ATK attributes.
4594 * This is a convenience function that can be used in
4595 * implementing the #AtkText interface in widgets.
4597 * Returns: the modified #AtkAttributeSet
4600 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4601 GtkStyleContext *context,
4602 GtkStateFlags flags)
4607 gtk_style_context_get_background_color (context, flags, &color);
4608 value = g_strdup_printf ("%u,%u,%u",
4609 (guint) ceil (color.red * 65536 - color.red),
4610 (guint) ceil (color.green * 65536 - color.green),
4611 (guint) ceil (color.blue * 65536 - color.blue));
4612 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4615 gtk_style_context_get_color (context, flags, &color);
4616 value = g_strdup_printf ("%u,%u,%u",
4617 (guint) ceil (color.red * 65536 - color.red),
4618 (guint) ceil (color.green * 65536 - color.green),
4619 (guint) ceil (color.blue * 65536 - color.blue));
4620 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);
4627 gtk_gradient_resolve_for_context (GtkGradient *gradient,
4628 GtkStyleContext *context)
4630 GtkStyleContextPrivate *priv = context->priv;
4631 GtkCssDependencies ignored = 0;
4633 g_return_val_if_fail (gradient != NULL, NULL);
4634 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4636 return _gtk_gradient_resolve_full (gradient,
4637 GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
4638 style_data_lookup (context)->store,
4639 priv->parent ? style_data_lookup (priv->parent)->store : NULL,