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 "gtkcssenginevalueprivate.h"
28 #include "gtkcssnumbervalueprivate.h"
29 #include "gtkcssrgbavalueprivate.h"
31 #include "gtkstylepropertiesprivate.h"
32 #include "gtktypebuiltins.h"
33 #include "gtkthemingengineprivate.h"
35 #include "gtkwidget.h"
36 #include "gtkwindow.h"
37 #include "gtkprivate.h"
38 #include "gtksymboliccolorprivate.h"
39 #include "gtkiconfactory.h"
40 #include "gtkwidgetpath.h"
41 #include "gtkwidgetprivate.h"
42 #include "gtkstylecascadeprivate.h"
43 #include "gtkstyleproviderprivate.h"
44 #include "gtksettings.h"
45 #include "gtksettingsprivate.h"
48 * SECTION:gtkstylecontext
49 * @Short_description: Rendering UI elements
50 * @Title: GtkStyleContext
52 * #GtkStyleContext is an object that stores styling information affecting
53 * a widget defined by #GtkWidgetPath.
55 * In order to construct the final style information, #GtkStyleContext
56 * queries information from all attached #GtkStyleProviders. Style providers
57 * can be either attached explicitly to the context through
58 * gtk_style_context_add_provider(), or to the screen through
59 * gtk_style_context_add_provider_for_screen(). The resulting style is a
60 * combination of all providers' information in priority order.
62 * For GTK+ widgets, any #GtkStyleContext returned by
63 * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
64 * #GdkScreen and RTL/LTR information set. The style context will be also
65 * updated automatically if any of these settings change on the widget.
67 * If you are using the theming layer standalone, you will need to set a
68 * widget path and a screen yourself to the created style context through
69 * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
70 * as updating the context yourself using gtk_style_context_invalidate()
71 * whenever any of the conditions change, such as a change in the
72 * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
75 * <refsect2 id="gtkstylecontext-animations">
76 * <title>Transition animations</title>
78 * #GtkStyleContext has built-in support for state change transitions.
79 * Note that these animations respect the #GtkSettings:gtk-enable-animations
83 * For simple widgets where state changes affect the whole widget area,
84 * calling gtk_style_context_notify_state_change() with a %NULL region
85 * is sufficient to trigger the transition animation. And GTK+ already
86 * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
90 * If a widget needs to declare several animatable regions (i.e. not
91 * affecting the whole widget area), its #GtkWidget::draw signal handler
92 * needs to wrap the render operations for the different regions with
93 * calls to gtk_style_context_push_animatable_region() and
94 * gtk_style_context_pop_animatable_region(). These functions take an
95 * identifier for the region which must be unique within the style context.
96 * For simple widgets with a fixed set of animatable regions, using an
97 * enumeration works well:
100 * <title>Using an enumeration to identify animatable regions</title>
111 * spin_button_draw (GtkWidget *widget,
114 * GtkStyleContext *context;
116 * context = gtk_widget_get_style_context (widget);
118 * gtk_style_context_push_animatable_region (context,
119 * GUINT_TO_POINTER (REGION_ENTRY));
121 * gtk_render_background (cr, 0, 0, 100, 30);
122 * gtk_render_frame (cr, 0, 0, 100, 30);
124 * gtk_style_context_pop_animatable_region (context);
131 * For complex widgets with an arbitrary number of animatable regions, it
132 * is up to the implementation to come up with a way to uniquely identify
133 * each animatable region. Using pointers to internal structs is one way
137 * <title>Using struct pointers to identify animatable regions</title>
140 * notebook_draw_tab (GtkWidget *widget,
141 * NotebookPage *page,
144 * gtk_style_context_push_animatable_region (context, page);
145 * gtk_render_extension (cr, page->x, page->y, page->width, page->height);
146 * gtk_style_context_pop_animatable_region (context);
151 * The widget also needs to notify the style context about a state change
152 * for a given animatable region so the animation is triggered.
155 * <title>Triggering a state change animation on a region</title>
158 * notebook_motion_notify (GtkWidget *widget,
159 * GdkEventMotion *event)
161 * GtkStyleContext *context;
162 * NotebookPage *page;
164 * context = gtk_widget_get_style_context (widget);
165 * page = find_page_under_pointer (widget, event);
166 * gtk_style_context_notify_state_change (context,
167 * gtk_widget_get_window (widget),
169 * GTK_STATE_PRELIGHT,
176 * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
177 * special value, in this case, the whole widget area will be updated
181 * <refsect2 id="gtkstylecontext-classes">
182 * <title>Style classes and regions</title>
184 * Widgets can add style classes to their context, which can be used
185 * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
186 * rendering. GTK+ has a number of predefined style classes:
187 * #GTK_STYLE_CLASS_CELL,
188 * #GTK_STYLE_CLASS_ENTRY,
189 * #GTK_STYLE_CLASS_BUTTON,
190 * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
191 * #GTK_STYLE_CLASS_CALENDAR,
192 * #GTK_STYLE_CLASS_SLIDER,
193 * #GTK_STYLE_CLASS_BACKGROUND,
194 * #GTK_STYLE_CLASS_RUBBERBAND,
195 * #GTK_STYLE_CLASS_TOOLTIP,
196 * #GTK_STYLE_CLASS_MENU,
197 * #GTK_STYLE_CLASS_MENUBAR,
198 * #GTK_STYLE_CLASS_MENUITEM,
199 * #GTK_STYLE_CLASS_TOOLBAR,
200 * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
201 * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
202 * #GTK_STYLE_CLASS_RADIO,
203 * #GTK_STYLE_CLASS_CHECK,
204 * #GTK_STYLE_CLASS_TROUGH,
205 * #GTK_STYLE_CLASS_SCROLLBAR,
206 * #GTK_STYLE_CLASS_SCALE,
207 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
208 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
209 * #GTK_STYLE_CLASS_HEADER,
210 * #GTK_STYLE_CLASS_ACCELERATOR,
211 * #GTK_STYLE_CLASS_GRIP,
212 * #GTK_STYLE_CLASS_DOCK,
213 * #GTK_STYLE_CLASS_PROGRESSBAR,
214 * #GTK_STYLE_CLASS_SPINNER,
215 * #GTK_STYLE_CLASS_EXPANDER,
216 * #GTK_STYLE_CLASS_SPINBUTTON,
217 * #GTK_STYLE_CLASS_NOTEBOOK,
218 * #GTK_STYLE_CLASS_VIEW,
219 * #GTK_STYLE_CLASS_SIDEBAR,
220 * #GTK_STYLE_CLASS_IMAGE,
221 * #GTK_STYLE_CLASS_HIGHLIGHT,
222 * #GTK_STYLE_CLASS_FRAME,
223 * #GTK_STYLE_CLASS_DND,
224 * #GTK_STYLE_CLASS_PANE_SEPARATOR,
225 * #GTK_STYLE_CLASS_SEPARATOR,
226 * #GTK_STYLE_CLASS_INFO,
227 * #GTK_STYLE_CLASS_WARNING,
228 * #GTK_STYLE_CLASS_QUESTION,
229 * #GTK_STYLE_CLASS_ERROR,
230 * #GTK_STYLE_CLASS_HORIZONTAL,
231 * #GTK_STYLE_CLASS_VERTICAL,
232 * #GTK_STYLE_CLASS_TOP,
233 * #GTK_STYLE_CLASS_BOTTOM,
234 * #GTK_STYLE_CLASS_LEFT,
235 * #GTK_STYLE_CLASS_RIGHT,
238 * Widgets can also add regions with flags to their context.
239 * The regions used by GTK+ widgets are:
244 * <entry>Region</entry>
245 * <entry>Flags</entry>
246 * <entry>Macro</entry>
247 * <entry>Used by</entry>
253 * <entry>even, odd</entry>
254 * <entry>GTK_STYLE_REGION_ROW</entry>
255 * <entry>#GtkTreeView</entry>
258 * <entry>column</entry>
259 * <entry>first, last, sorted</entry>
260 * <entry>GTK_STYLE_REGION_COLUMN</entry>
261 * <entry>#GtkTreeView</entry>
264 * <entry>column-header</entry>
266 * <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
271 * <entry>even, odd, first, last</entry>
272 * <entry>GTK_STYLE_REGION_TAB</entry>
273 * <entry>#GtkNotebook</entry>
280 * <refsect2 id="gtkstylecontext-custom-styling">
281 * <title>Custom styling in UI libraries and applications</title>
283 * If you are developing a library with custom #GtkWidget<!-- -->s that
284 * render differently than standard components, you may need to add a
285 * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
286 * priority, either a #GtkCssProvider or a custom object implementing the
287 * #GtkStyleProvider interface. This way theming engines may still attempt
288 * to style your UI elements in a different way if needed so.
291 * If you are using custom styling on an applications, you probably want then
292 * to make your style information prevail to the theme's, so you must use
293 * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
294 * priority, keep in mind that the user settings in
295 * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
296 * still take precedence over your changes, as it uses the
297 * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
300 * If a custom theming engine is needed, you probably want to implement a
301 * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
302 * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
303 * which loads the theming engine module from the standard paths.
308 /* When these change we do a full restyling. Otherwise we try to figure out
309 * if we need to change things. */
310 #define GTK_STYLE_CONTEXT_RADICAL_CHANGE (GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_SOURCE)
311 /* When these change we don't clear the cache. This takes more memory but makes
312 * things go faster. */
313 #define GTK_STYLE_CONTEXT_CACHED_CHANGE (GTK_CSS_CHANGE_STATE)
315 typedef struct GtkStyleInfo GtkStyleInfo;
316 typedef struct GtkRegion GtkRegion;
317 typedef struct PropertyValue PropertyValue;
318 typedef struct StyleData StyleData;
323 GtkRegionFlags flags;
337 GArray *style_classes;
339 GtkJunctionSides junction_sides;
340 GtkStateFlags state_flags;
346 GtkCssComputedValues *store;
347 GArray *property_cache;
351 struct _GtkStyleContextPrivate
355 GtkStyleCascade *cascade;
357 GtkStyleContext *animation_list_prev;
358 GtkStyleContext *animation_list_next;
360 GtkStyleContext *parent;
363 GtkWidgetPath *widget_path;
364 GHashTable *style_data;
367 GtkTextDirection direction;
369 GtkCssChange relevant_changes;
370 GtkCssChange pending_changes;
372 guint invalidating_context : 1;
388 static guint signals[LAST_SIGNAL] = { 0 };
389 static GtkStyleContext *_running_animations = NULL;
390 guint _running_animations_timer_id = 0;
392 static void gtk_style_context_finalize (GObject *object);
394 static void gtk_style_context_impl_set_property (GObject *object,
398 static void gtk_style_context_impl_get_property (GObject *object,
402 static GtkSymbolicColor *
403 gtk_style_context_color_lookup_func (gpointer contextp,
405 static StyleData *style_data_lookup (GtkStyleContext *context);
408 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
411 gtk_style_context_real_changed (GtkStyleContext *context)
413 GtkStyleContextPrivate *priv = context->priv;
416 _gtk_widget_style_context_invalidated (priv->widget);
420 gtk_style_context_class_init (GtkStyleContextClass *klass)
422 GObjectClass *object_class = G_OBJECT_CLASS (klass);
424 object_class->finalize = gtk_style_context_finalize;
425 object_class->set_property = gtk_style_context_impl_set_property;
426 object_class->get_property = gtk_style_context_impl_get_property;
428 klass->changed = gtk_style_context_real_changed;
431 g_signal_new (I_("changed"),
432 G_TYPE_FROM_CLASS (object_class),
434 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
436 g_cclosure_marshal_VOID__VOID,
439 g_object_class_install_property (object_class,
441 g_param_spec_object ("screen",
443 P_("The associated GdkScreen"),
445 GTK_PARAM_READWRITE));
446 g_object_class_install_property (object_class,
448 g_param_spec_enum ("direction",
450 P_("Text direction"),
451 GTK_TYPE_TEXT_DIRECTION,
453 GTK_PARAM_READWRITE));
455 * GtkStyleContext:parent:
457 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
462 g_object_class_install_property (object_class,
464 g_param_spec_object ("parent",
466 P_("The parent style context"),
467 GTK_TYPE_STYLE_CONTEXT,
468 GTK_PARAM_READWRITE));
470 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
474 style_data_new (void)
478 data = g_slice_new0 (StyleData);
485 clear_property_cache (StyleData *data)
489 if (!data->property_cache)
492 for (i = 0; i < data->property_cache->len; i++)
494 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
496 g_param_spec_unref (node->pspec);
497 g_value_unset (&node->value);
500 g_array_free (data->property_cache, TRUE);
501 data->property_cache = NULL;
505 style_data_ref (StyleData *style_data)
507 style_data->ref_count++;
513 style_data_unref (StyleData *data)
517 if (data->ref_count > 0)
520 g_object_unref (data->store);
521 clear_property_cache (data);
523 g_slice_free (StyleData, data);
527 style_data_is_animating (StyleData *style_data)
529 return !_gtk_css_computed_values_is_static (style_data->store);
532 static GtkStyleInfo *
533 style_info_new (void)
537 info = g_slice_new0 (GtkStyleInfo);
538 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
539 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
545 style_info_set_data (GtkStyleInfo *info,
548 if (info->data == data)
552 style_data_ref (data);
555 style_data_unref (info->data);
561 style_info_free (GtkStyleInfo *info)
563 style_info_set_data (info, NULL);
564 g_array_free (info->style_classes, TRUE);
565 g_array_free (info->regions, TRUE);
566 g_slice_free (GtkStyleInfo, info);
569 static GtkStyleInfo *
570 style_info_pop (GtkStyleInfo *info)
572 GtkStyleInfo *next = info->next;
574 style_info_free (info);
579 static GtkStyleInfo *
580 style_info_copy (GtkStyleInfo *info)
584 copy = style_info_new ();
585 g_array_insert_vals (copy->style_classes, 0,
586 info->style_classes->data,
587 info->style_classes->len);
589 g_array_insert_vals (copy->regions, 0,
594 copy->junction_sides = info->junction_sides;
595 copy->state_flags = info->state_flags;
596 style_info_set_data (copy, info->data);
602 style_info_hash (gconstpointer elem)
604 const GtkStyleInfo *info;
609 for (i = 0; i < info->style_classes->len; i++)
611 hash += g_array_index (info->style_classes, GQuark, i);
615 for (i = 0; i < info->regions->len; i++)
619 region = &g_array_index (info->regions, GtkRegion, i);
620 hash += region->class_quark;
621 hash += region->flags;
625 return hash ^ info->state_flags;
629 style_info_equal (gconstpointer elem1,
632 const GtkStyleInfo *info1, *info2;
637 if (info1->state_flags != info2->state_flags)
640 if (info1->junction_sides != info2->junction_sides)
643 if (info1->style_classes->len != info2->style_classes->len)
646 if (memcmp (info1->style_classes->data,
647 info2->style_classes->data,
648 info1->style_classes->len * sizeof (GQuark)) != 0)
651 if (info1->regions->len != info2->regions->len)
654 if (memcmp (info1->regions->data,
655 info2->regions->data,
656 info1->regions->len * sizeof (GtkRegion)) != 0)
663 gtk_style_context_cascade_changed (GtkStyleCascade *cascade,
664 GtkStyleContext *context)
666 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_SOURCE);
670 gtk_style_context_set_cascade (GtkStyleContext *context,
671 GtkStyleCascade *cascade)
673 GtkStyleContextPrivate *priv;
675 priv = context->priv;
677 if (priv->cascade == cascade)
682 g_object_ref (cascade);
683 g_signal_connect (cascade,
684 "-gtk-private-changed",
685 G_CALLBACK (gtk_style_context_cascade_changed),
691 g_signal_handlers_disconnect_by_func (priv->cascade,
692 gtk_style_context_cascade_changed,
694 g_object_unref (priv->cascade);
697 priv->cascade = cascade;
700 gtk_style_context_cascade_changed (cascade, context);
703 GtkStyleProviderPrivate *
704 _gtk_style_context_get_style_provider (GtkStyleContext *context)
706 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
708 return GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade);
712 gtk_style_context_init (GtkStyleContext *style_context)
714 GtkStyleContextPrivate *priv;
716 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
717 GTK_TYPE_STYLE_CONTEXT,
718 GtkStyleContextPrivate);
720 priv->style_data = g_hash_table_new_full (style_info_hash,
722 (GDestroyNotify) style_info_free,
723 (GDestroyNotify) style_data_unref);
725 priv->direction = GTK_TEXT_DIR_LTR;
727 priv->screen = gdk_screen_get_default ();
728 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
730 /* Create default info store */
731 priv->info = style_info_new ();
733 gtk_style_context_set_cascade (style_context,
734 _gtk_style_cascade_get_for_screen (priv->screen));
738 gtk_style_context_do_animations (gpointer unused)
740 GtkStyleContext *context;
742 for (context = _running_animations;
744 context = context->priv->animation_list_next)
746 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANIMATE);
753 gtk_style_context_is_animating (GtkStyleContext *context)
755 GtkStyleContextPrivate *priv = context->priv;
757 return priv->animation_list_prev != NULL
758 || _running_animations == context;
762 gtk_style_context_stop_animating (GtkStyleContext *context)
764 GtkStyleContextPrivate *priv = context->priv;
766 if (!gtk_style_context_is_animating (context))
769 if (priv->animation_list_prev == NULL)
771 _running_animations = priv->animation_list_next;
773 if (_running_animations == NULL)
775 /* we were the last animation */
776 g_source_remove (_running_animations_timer_id);
777 _running_animations_timer_id = 0;
781 priv->animation_list_prev->priv->animation_list_next = priv->animation_list_next;
783 if (priv->animation_list_next)
784 priv->animation_list_next->priv->animation_list_prev = priv->animation_list_prev;
786 priv->animation_list_next = NULL;
787 priv->animation_list_prev = NULL;
791 gtk_style_context_start_animating (GtkStyleContext *context)
793 GtkStyleContextPrivate *priv = context->priv;
795 if (gtk_style_context_is_animating (context))
798 if (_running_animations == NULL)
800 _running_animations_timer_id = gdk_threads_add_timeout (25,
801 gtk_style_context_do_animations,
803 _running_animations = context;
807 priv->animation_list_next = _running_animations;
808 _running_animations->priv->animation_list_prev = context;
809 _running_animations = context;
814 gtk_style_context_should_animate (GtkStyleContext *context)
816 GtkStyleContextPrivate *priv;
820 priv = context->priv;
822 if (priv->widget == NULL)
825 if (!gtk_widget_get_mapped (priv->widget))
828 data = style_data_lookup (context);
829 if (!style_data_is_animating (data))
832 g_object_get (gtk_widget_get_settings (context->priv->widget),
833 "gtk-enable-animations", &animate,
840 _gtk_style_context_update_animating (GtkStyleContext *context)
842 if (gtk_style_context_should_animate (context))
843 gtk_style_context_start_animating (context);
845 gtk_style_context_stop_animating (context);
849 gtk_style_context_finalize (GObject *object)
851 GtkStyleContextPrivate *priv;
852 GtkStyleContext *style_context;
854 style_context = GTK_STYLE_CONTEXT (object);
855 priv = style_context->priv;
857 gtk_style_context_stop_animating (style_context);
859 /* children hold a reference to us */
860 g_assert (priv->children == NULL);
862 gtk_style_context_set_parent (style_context, NULL);
864 gtk_style_context_set_cascade (style_context, NULL);
866 if (priv->widget_path)
867 gtk_widget_path_free (priv->widget_path);
869 g_hash_table_destroy (priv->style_data);
872 priv->info = style_info_pop (priv->info);
874 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
878 gtk_style_context_impl_set_property (GObject *object,
883 GtkStyleContext *style_context;
885 style_context = GTK_STYLE_CONTEXT (object);
890 gtk_style_context_set_screen (style_context,
891 g_value_get_object (value));
894 gtk_style_context_set_direction (style_context,
895 g_value_get_enum (value));
898 gtk_style_context_set_parent (style_context,
899 g_value_get_object (value));
902 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
908 gtk_style_context_impl_get_property (GObject *object,
913 GtkStyleContext *style_context;
914 GtkStyleContextPrivate *priv;
916 style_context = GTK_STYLE_CONTEXT (object);
917 priv = style_context->priv;
922 g_value_set_object (value, priv->screen);
925 g_value_set_enum (value, priv->direction);
928 g_value_set_object (value, priv->parent);
931 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
936 static GtkWidgetPath *
937 create_query_path (GtkStyleContext *context,
940 GtkStyleContextPrivate *priv;
944 priv = context->priv;
945 path = priv->widget ? _gtk_widget_create_path (priv->widget) : gtk_widget_path_copy (priv->widget_path);
946 pos = gtk_widget_path_length (path) - 1;
948 /* Set widget regions */
949 for (i = 0; i < info->regions->len; i++)
953 region = &g_array_index (info->regions, GtkRegion, i);
954 gtk_widget_path_iter_add_region (path, pos,
955 g_quark_to_string (region->class_quark),
959 /* Set widget classes */
960 for (i = 0; i < info->style_classes->len; i++)
964 quark = g_array_index (info->style_classes, GQuark, i);
965 gtk_widget_path_iter_add_class (path, pos,
966 g_quark_to_string (quark));
973 build_properties (GtkStyleContext *context,
974 GtkCssComputedValues *values,
976 const GtkBitmask *relevant_changes)
978 GtkStyleContextPrivate *priv;
979 GtkCssMatcher matcher;
981 GtkCssLookup *lookup;
983 priv = context->priv;
985 path = create_query_path (context, info);
986 lookup = _gtk_css_lookup_new (relevant_changes);
988 if (_gtk_css_matcher_init (&matcher, path, info->state_flags))
989 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
993 _gtk_css_lookup_resolve (lookup, context, values);
995 _gtk_css_lookup_free (lookup);
996 gtk_widget_path_free (path);
1000 style_data_lookup (GtkStyleContext *context)
1002 GtkStyleContextPrivate *priv;
1006 priv = context->priv;
1009 /* Current data in use is cached, just return it */
1013 g_assert (priv->widget != NULL || priv->widget_path != NULL);
1015 data = g_hash_table_lookup (priv->style_data, info);
1018 style_info_set_data (info, data);
1022 data = style_data_new ();
1023 data->store = _gtk_css_computed_values_new ();
1024 style_info_set_data (info, data);
1025 g_hash_table_insert (priv->style_data,
1026 style_info_copy (info),
1029 build_properties (context, data->store, info, NULL);
1035 style_data_lookup_for_state (GtkStyleContext *context,
1036 GtkStateFlags state)
1040 if (context->priv->info->state_flags == state)
1041 return style_data_lookup (context);
1043 gtk_style_context_save (context);
1044 gtk_style_context_set_state (context, state);
1045 data = style_data_lookup (context);
1046 gtk_style_context_restore (context);
1052 gtk_style_context_set_invalid (GtkStyleContext *context,
1055 GtkStyleContextPrivate *priv;
1057 priv = context->priv;
1059 if (priv->invalid == invalid)
1062 priv->invalid = invalid;
1067 gtk_style_context_set_invalid (priv->parent, TRUE);
1068 else if (GTK_IS_RESIZE_CONTAINER (priv->widget))
1069 _gtk_container_queue_restyle (GTK_CONTAINER (priv->widget));
1073 /* returns TRUE if someone called gtk_style_context_save() but hasn't
1074 * called gtk_style_context_restore() yet.
1075 * In those situations we don't invalidate the context when somebody
1076 * changes state/regions/classes.
1079 gtk_style_context_is_saved (GtkStyleContext *context)
1081 return context->priv->info->next != NULL;
1085 gtk_style_context_queue_invalidate_internal (GtkStyleContext *context,
1086 GtkCssChange change)
1088 GtkStyleContextPrivate *priv = context->priv;
1089 GtkStyleInfo *info = priv->info;
1091 if (gtk_style_context_is_saved (context))
1093 style_info_set_data (info, NULL);
1097 _gtk_style_context_queue_invalidate (context, change);
1098 /* XXX: We need to invalidate siblings here somehow */
1103 * gtk_style_context_new:
1105 * Creates a standalone #GtkStyleContext, this style context
1106 * won't be attached to any widget, so you may want
1107 * to call gtk_style_context_set_path() yourself.
1110 * This function is only useful when using the theming layer
1111 * separated from GTK+, if you are using #GtkStyleContext to
1112 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1113 * in order to get a style context ready to theme the widget.
1116 * Returns: A newly created #GtkStyleContext.
1119 gtk_style_context_new (void)
1121 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1125 _gtk_style_context_set_widget (GtkStyleContext *context,
1128 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1129 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1131 context->priv->widget = widget;
1133 _gtk_style_context_update_animating (context);
1135 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_SELF);
1139 * gtk_style_context_add_provider:
1140 * @context: a #GtkStyleContext
1141 * @provider: a #GtkStyleProvider
1142 * @priority: the priority of the style provider. The lower
1143 * it is, the earlier it will be used in the style
1144 * construction. Typically this will be in the range
1145 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1146 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1148 * Adds a style provider to @context, to be used in style construction.
1149 * Note that a style provider added by this function only affects
1150 * the style of the widget to which @context belongs. If you want
1151 * to affect the style of all widgets, use
1152 * gtk_style_context_add_provider_for_screen().
1154 * <note><para>If both priorities are the same, A #GtkStyleProvider
1155 * added through this function takes precedence over another added
1156 * through gtk_style_context_add_provider_for_screen().</para></note>
1161 gtk_style_context_add_provider (GtkStyleContext *context,
1162 GtkStyleProvider *provider,
1165 GtkStyleContextPrivate *priv;
1167 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1168 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1170 priv = context->priv;
1172 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1174 GtkStyleCascade *new_cascade;
1176 new_cascade = _gtk_style_cascade_new ();
1177 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1178 _gtk_style_cascade_add_provider (new_cascade, provider, priority);
1179 gtk_style_context_set_cascade (context, new_cascade);
1180 g_object_unref (new_cascade);
1184 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1189 * gtk_style_context_remove_provider:
1190 * @context: a #GtkStyleContext
1191 * @provider: a #GtkStyleProvider
1193 * Removes @provider from the style providers list in @context.
1198 gtk_style_context_remove_provider (GtkStyleContext *context,
1199 GtkStyleProvider *provider)
1201 GtkStyleContextPrivate *priv;
1203 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1204 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1206 priv = context->priv;
1208 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1211 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1215 * gtk_style_context_reset_widgets:
1216 * @screen: a #GdkScreen
1218 * This function recomputes the styles for all widgets under a particular
1219 * #GdkScreen. This is useful when some global parameter has changed that
1220 * affects the appearance of all widgets, because when a widget gets a new
1221 * style, it will both redraw and recompute any cached information about
1222 * its appearance. As an example, it is used when the color scheme changes
1223 * in the related #GtkSettings object.
1228 gtk_style_context_reset_widgets (GdkScreen *screen)
1230 GList *list, *toplevels;
1232 _gtk_icon_set_invalidate_caches ();
1234 toplevels = gtk_window_list_toplevels ();
1235 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1237 for (list = toplevels; list; list = list->next)
1239 if (gtk_widget_get_screen (list->data) == screen)
1240 gtk_widget_reset_style (list->data);
1242 g_object_unref (list->data);
1245 g_list_free (toplevels);
1249 * gtk_style_context_add_provider_for_screen:
1250 * @screen: a #GdkScreen
1251 * @provider: a #GtkStyleProvider
1252 * @priority: the priority of the style provider. The lower
1253 * it is, the earlier it will be used in the style
1254 * construction. Typically this will be in the range
1255 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1256 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1258 * Adds a global style provider to @screen, which will be used
1259 * in style construction for all #GtkStyleContexts under @screen.
1261 * GTK+ uses this to make styling information from #GtkSettings
1264 * <note><para>If both priorities are the same, A #GtkStyleProvider
1265 * added through gtk_style_context_add_provider() takes precedence
1266 * over another added through this function.</para></note>
1271 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1272 GtkStyleProvider *provider,
1275 GtkStyleCascade *cascade;
1277 g_return_if_fail (GDK_IS_SCREEN (screen));
1278 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1279 g_return_if_fail (!GTK_IS_SETTINGS (provider) || _gtk_settings_get_screen (GTK_SETTINGS (provider)) == screen);
1281 cascade = _gtk_style_cascade_get_for_screen (screen);
1282 _gtk_style_cascade_add_provider (cascade, provider, priority);
1286 * gtk_style_context_remove_provider_for_screen:
1287 * @screen: a #GdkScreen
1288 * @provider: a #GtkStyleProvider
1290 * Removes @provider from the global style providers list in @screen.
1295 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1296 GtkStyleProvider *provider)
1298 GtkStyleCascade *cascade;
1300 g_return_if_fail (GDK_IS_SCREEN (screen));
1301 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1302 g_return_if_fail (!GTK_IS_SETTINGS (provider));
1304 cascade = _gtk_style_cascade_get_for_screen (screen);
1305 _gtk_style_cascade_remove_provider (cascade, provider);
1309 * gtk_style_context_get_section:
1310 * @context: a #GtkStyleContext
1311 * @property: style property name
1313 * Queries the location in the CSS where @property was defined for the
1314 * current @context. Note that the state to be queried is taken from
1315 * gtk_style_context_get_state().
1317 * If the location is not available, %NULL will be returned. The
1318 * location might not be available for various reasons, such as the
1319 * property being overridden, @property not naming a supported CSS
1320 * property or tracking of definitions being disabled for performance
1323 * Shorthand CSS properties cannot be queried for a location and will
1324 * always return %NULL.
1326 * Returns: %NULL or the section where value was defined
1329 gtk_style_context_get_section (GtkStyleContext *context,
1330 const gchar *property)
1332 GtkStyleContextPrivate *priv;
1333 GtkStyleProperty *prop;
1336 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1337 g_return_val_if_fail (property != NULL, NULL);
1339 priv = context->priv;
1340 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1342 prop = _gtk_style_property_lookup (property);
1343 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1346 data = style_data_lookup (context);
1347 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1350 static GtkCssValue *
1351 gtk_style_context_query_func (guint id,
1354 return _gtk_css_computed_values_get_value (values, id);
1358 * gtk_style_context_get_property:
1359 * @context: a #GtkStyleContext
1360 * @property: style property name
1361 * @state: state to retrieve the property value for
1362 * @value: (out) (transfer full): return location for the style property value
1364 * Gets a style property from @context for the given state.
1366 * When @value is no longer needed, g_value_unset() must be called
1367 * to free any allocated memory.
1372 gtk_style_context_get_property (GtkStyleContext *context,
1373 const gchar *property,
1374 GtkStateFlags state,
1377 GtkStyleContextPrivate *priv;
1378 GtkStyleProperty *prop;
1381 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1382 g_return_if_fail (property != NULL);
1383 g_return_if_fail (value != NULL);
1385 priv = context->priv;
1386 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1388 prop = _gtk_style_property_lookup (property);
1391 g_warning ("Style property \"%s\" is not registered", property);
1394 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1396 g_warning ("Style property \"%s\" is not gettable", property);
1400 data = style_data_lookup_for_state (context, state);
1401 _gtk_style_property_query (prop, value, gtk_style_context_query_func, data->store);
1405 * gtk_style_context_get_valist:
1406 * @context: a #GtkStyleContext
1407 * @state: state to retrieve the property values for
1408 * @args: va_list of property name/return location pairs, followed by %NULL
1410 * Retrieves several style property values from @context for a given state.
1415 gtk_style_context_get_valist (GtkStyleContext *context,
1416 GtkStateFlags state,
1419 const gchar *property_name;
1421 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1423 property_name = va_arg (args, const gchar *);
1425 while (property_name)
1427 gchar *error = NULL;
1428 GValue value = G_VALUE_INIT;
1430 gtk_style_context_get_property (context,
1435 G_VALUE_LCOPY (&value, args, 0, &error);
1436 g_value_unset (&value);
1440 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1445 property_name = va_arg (args, const gchar *);
1450 * gtk_style_context_get:
1451 * @context: a #GtkStyleContext
1452 * @state: state to retrieve the property values for
1453 * @...: property name /return value pairs, followed by %NULL
1455 * Retrieves several style property values from @context for a
1461 gtk_style_context_get (GtkStyleContext *context,
1462 GtkStateFlags state,
1467 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1469 va_start (args, state);
1470 gtk_style_context_get_valist (context, state, args);
1475 * gtk_style_context_set_state:
1476 * @context: a #GtkStyleContext
1477 * @flags: state to represent
1479 * Sets the state to be used when rendering with any
1480 * of the gtk_render_*() functions.
1485 gtk_style_context_set_state (GtkStyleContext *context,
1486 GtkStateFlags flags)
1488 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1490 context->priv->info->state_flags = flags;
1492 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_STATE);
1496 * gtk_style_context_get_state:
1497 * @context: a #GtkStyleContext
1499 * Returns the state used when rendering.
1501 * Returns: the state flags
1506 gtk_style_context_get_state (GtkStyleContext *context)
1508 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1510 return context->priv->info->state_flags;
1514 * gtk_style_context_state_is_running:
1515 * @context: a #GtkStyleContext
1516 * @state: a widget state
1517 * @progress: (out): return location for the transition progress
1519 * Returns %TRUE if there is a transition animation running for the
1520 * current region (see gtk_style_context_push_animatable_region()).
1522 * If @progress is not %NULL, the animation progress will be returned
1523 * there, 0.0 means the state is closest to being unset, while 1.0 means
1524 * it's closest to being set. This means transition animation will
1525 * run from 0 to 1 when @state is being set and from 1 to 0 when
1528 * Returns: %TRUE if there is a running transition animation for @state.
1532 * Deprecated: 3.6: This function always returns %FALSE
1535 gtk_style_context_state_is_running (GtkStyleContext *context,
1539 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1545 * gtk_style_context_set_path:
1546 * @context: a #GtkStyleContext
1547 * @path: a #GtkWidgetPath
1549 * Sets the #GtkWidgetPath used for style matching. As a
1550 * consequence, the style will be regenerated to match
1551 * the new given path.
1553 * If you are using a #GtkStyleContext returned from
1554 * gtk_widget_get_style_context(), you do not need to call
1560 gtk_style_context_set_path (GtkStyleContext *context,
1561 GtkWidgetPath *path)
1563 GtkStyleContextPrivate *priv;
1565 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1566 g_return_if_fail (path != NULL);
1568 priv = context->priv;
1569 g_return_if_fail (priv->widget == NULL);
1571 if (priv->widget_path)
1573 gtk_widget_path_free (priv->widget_path);
1574 priv->widget_path = NULL;
1578 priv->widget_path = gtk_widget_path_copy (path);
1580 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY);
1584 * gtk_style_context_get_path:
1585 * @context: a #GtkStyleContext
1587 * Returns the widget path used for style matching.
1589 * Returns: (transfer none): A #GtkWidgetPath
1593 const GtkWidgetPath *
1594 gtk_style_context_get_path (GtkStyleContext *context)
1596 GtkStyleContextPrivate *priv;
1598 priv = context->priv;
1600 return gtk_widget_get_path (priv->widget);
1602 return priv->widget_path;
1606 * gtk_style_context_set_parent:
1607 * @context: a #GtkStyleContext
1608 * @parent: (allow-none): the new parent or %NULL
1610 * Sets the parent style context for @context. The parent style
1611 * context is used to implement
1612 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1615 * If you are using a #GtkStyleContext returned from
1616 * gtk_widget_get_style_context(), the parent will be set for you.
1621 gtk_style_context_set_parent (GtkStyleContext *context,
1622 GtkStyleContext *parent)
1624 GtkStyleContextPrivate *priv;
1626 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1627 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1629 priv = context->priv;
1631 if (priv->parent == parent)
1636 parent->priv->children = g_slist_prepend (parent->priv->children, context);
1637 g_object_ref (parent);
1639 gtk_style_context_set_invalid (parent, TRUE);
1644 priv->parent->priv->children = g_slist_remove (priv->parent->priv->children, context);
1645 g_object_unref (priv->parent);
1648 priv->parent = parent;
1650 g_object_notify (G_OBJECT (context), "parent");
1651 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
1655 * gtk_style_context_get_parent:
1656 * @context: a #GtkStyleContext
1658 * Gets the parent context set via gtk_style_context_set_parent().
1659 * See that function for details.
1661 * Returns: (transfer none): the parent context or %NULL
1666 gtk_style_context_get_parent (GtkStyleContext *context)
1668 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1670 return context->priv->parent;
1674 * gtk_style_context_save:
1675 * @context: a #GtkStyleContext
1677 * Saves the @context state, so all modifications done through
1678 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1679 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1680 * or gtk_style_context_set_junction_sides() can be reverted in one
1681 * go through gtk_style_context_restore().
1686 gtk_style_context_save (GtkStyleContext *context)
1688 GtkStyleContextPrivate *priv;
1690 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1692 priv = context->priv;
1694 priv->info = style_info_copy (priv->info);
1695 /* Need to unset animations here because we can not know what style
1696 * class potential transitions came from once we save().
1698 if (priv->info->data && style_data_is_animating (priv->info->data))
1699 style_info_set_data (priv->info, NULL);
1703 * gtk_style_context_restore:
1704 * @context: a #GtkStyleContext
1706 * Restores @context state to a previous stage.
1707 * See gtk_style_context_save().
1712 gtk_style_context_restore (GtkStyleContext *context)
1714 GtkStyleContextPrivate *priv;
1716 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1718 priv = context->priv;
1720 priv->info = style_info_pop (priv->info);
1724 g_warning ("Unpaired gtk_style_context_restore() call");
1726 /* Create default region */
1727 priv->info = style_info_new ();
1732 style_class_find (GArray *array,
1737 gboolean found = FALSE;
1743 if (!array || array->len == 0)
1747 max = array->len - 1;
1753 mid = (min + max) / 2;
1754 item = g_array_index (array, GQuark, mid);
1756 if (class_quark == item)
1761 else if (class_quark > item)
1762 min = pos = mid + 1;
1769 while (!found && min <= max);
1778 region_find (GArray *array,
1783 gboolean found = FALSE;
1789 if (!array || array->len == 0)
1793 max = array->len - 1;
1799 mid = (min + max) / 2;
1800 region = &g_array_index (array, GtkRegion, mid);
1802 if (region->class_quark == class_quark)
1807 else if (region->class_quark > class_quark)
1808 min = pos = mid + 1;
1815 while (!found && min <= max);
1824 * gtk_style_context_add_class:
1825 * @context: a #GtkStyleContext
1826 * @class_name: class name to use in styling
1828 * Adds a style class to @context, so posterior calls to
1829 * gtk_style_context_get() or any of the gtk_render_*()
1830 * functions will make use of this new class for styling.
1832 * In the CSS file format, a #GtkEntry defining an "entry"
1833 * class, would be matched by:
1836 * GtkEntry.entry { ... }
1839 * While any widget defining an "entry" class would be
1848 gtk_style_context_add_class (GtkStyleContext *context,
1849 const gchar *class_name)
1851 GtkStyleContextPrivate *priv;
1856 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1857 g_return_if_fail (class_name != NULL);
1859 priv = context->priv;
1860 class_quark = g_quark_from_string (class_name);
1864 if (!style_class_find (info->style_classes, class_quark, &position))
1866 g_array_insert_val (info->style_classes, position, class_quark);
1868 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1873 * gtk_style_context_remove_class:
1874 * @context: a #GtkStyleContext
1875 * @class_name: class name to remove
1877 * Removes @class_name from @context.
1882 gtk_style_context_remove_class (GtkStyleContext *context,
1883 const gchar *class_name)
1885 GtkStyleContextPrivate *priv;
1890 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1891 g_return_if_fail (class_name != NULL);
1893 class_quark = g_quark_try_string (class_name);
1898 priv = context->priv;
1902 if (style_class_find (info->style_classes, class_quark, &position))
1904 g_array_remove_index (info->style_classes, position);
1906 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1911 * gtk_style_context_has_class:
1912 * @context: a #GtkStyleContext
1913 * @class_name: a class name
1915 * Returns %TRUE if @context currently has defined the
1918 * Returns: %TRUE if @context has @class_name defined
1923 gtk_style_context_has_class (GtkStyleContext *context,
1924 const gchar *class_name)
1926 GtkStyleContextPrivate *priv;
1930 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1931 g_return_val_if_fail (class_name != NULL, FALSE);
1933 class_quark = g_quark_try_string (class_name);
1938 priv = context->priv;
1942 if (style_class_find (info->style_classes, class_quark, NULL))
1949 * gtk_style_context_list_classes:
1950 * @context: a #GtkStyleContext
1952 * Returns the list of classes currently defined in @context.
1954 * Returns: (transfer container) (element-type utf8): a #GList of
1955 * strings with the currently defined classes. The contents
1956 * of the list are owned by GTK+, but you must free the list
1957 * itself with g_list_free() when you are done with it.
1962 gtk_style_context_list_classes (GtkStyleContext *context)
1964 GtkStyleContextPrivate *priv;
1966 GList *classes = NULL;
1969 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1971 priv = context->priv;
1975 for (i = 0; i < info->style_classes->len; i++)
1979 quark = g_array_index (info->style_classes, GQuark, i);
1980 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1987 * gtk_style_context_list_regions:
1988 * @context: a #GtkStyleContext
1990 * Returns the list of regions currently defined in @context.
1992 * Returns: (transfer container) (element-type utf8): a #GList of
1993 * strings with the currently defined regions. The contents
1994 * of the list are owned by GTK+, but you must free the list
1995 * itself with g_list_free() when you are done with it.
2000 gtk_style_context_list_regions (GtkStyleContext *context)
2002 GtkStyleContextPrivate *priv;
2004 GList *classes = NULL;
2007 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2009 priv = context->priv;
2013 for (i = 0; i < info->regions->len; i++)
2016 const gchar *class_name;
2018 region = &g_array_index (info->regions, GtkRegion, i);
2020 class_name = g_quark_to_string (region->class_quark);
2021 classes = g_list_prepend (classes, (gchar *) class_name);
2028 _gtk_style_context_check_region_name (const gchar *str)
2030 g_return_val_if_fail (str != NULL, FALSE);
2032 if (!g_ascii_islower (str[0]))
2038 !g_ascii_islower (*str))
2048 * gtk_style_context_add_region:
2049 * @context: a #GtkStyleContext
2050 * @region_name: region name to use in styling
2051 * @flags: flags that apply to the region
2053 * Adds a region to @context, so posterior calls to
2054 * gtk_style_context_get() or any of the gtk_render_*()
2055 * functions will make use of this new region for styling.
2057 * In the CSS file format, a #GtkTreeView defining a "row"
2058 * region, would be matched by:
2061 * GtkTreeView row { ... }
2064 * Pseudo-classes are used for matching @flags, so the two
2067 * GtkTreeView row:nth-child(even) { ... }
2068 * GtkTreeView row:nth-child(odd) { ... }
2071 * would apply to even and odd rows, respectively.
2073 * <note><para>Region names must only contain lowercase letters
2074 * and '-', starting always with a lowercase letter.</para></note>
2079 gtk_style_context_add_region (GtkStyleContext *context,
2080 const gchar *region_name,
2081 GtkRegionFlags flags)
2083 GtkStyleContextPrivate *priv;
2085 GQuark region_quark;
2088 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2089 g_return_if_fail (region_name != NULL);
2090 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2092 priv = context->priv;
2093 region_quark = g_quark_from_string (region_name);
2097 if (!region_find (info->regions, region_quark, &position))
2101 region.class_quark = region_quark;
2102 region.flags = flags;
2104 g_array_insert_val (info->regions, position, region);
2106 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2111 * gtk_style_context_remove_region:
2112 * @context: a #GtkStyleContext
2113 * @region_name: region name to unset
2115 * Removes a region from @context.
2120 gtk_style_context_remove_region (GtkStyleContext *context,
2121 const gchar *region_name)
2123 GtkStyleContextPrivate *priv;
2125 GQuark region_quark;
2128 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2129 g_return_if_fail (region_name != NULL);
2131 region_quark = g_quark_try_string (region_name);
2136 priv = context->priv;
2140 if (region_find (info->regions, region_quark, &position))
2142 g_array_remove_index (info->regions, position);
2144 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2149 * gtk_style_context_has_region:
2150 * @context: a #GtkStyleContext
2151 * @region_name: a region name
2152 * @flags_return: (out) (allow-none): return location for region flags
2154 * Returns %TRUE if @context has the region defined.
2155 * If @flags_return is not %NULL, it is set to the flags
2156 * affecting the region.
2158 * Returns: %TRUE if region is defined
2163 gtk_style_context_has_region (GtkStyleContext *context,
2164 const gchar *region_name,
2165 GtkRegionFlags *flags_return)
2167 GtkStyleContextPrivate *priv;
2169 GQuark region_quark;
2172 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2173 g_return_val_if_fail (region_name != NULL, FALSE);
2178 region_quark = g_quark_try_string (region_name);
2183 priv = context->priv;
2187 if (region_find (info->regions, region_quark, &position))
2193 region = &g_array_index (info->regions, GtkRegion, position);
2194 *flags_return = region->flags;
2203 style_property_values_cmp (gconstpointer bsearch_node1,
2204 gconstpointer bsearch_node2)
2206 const PropertyValue *val1 = bsearch_node1;
2207 const PropertyValue *val2 = bsearch_node2;
2209 if (val1->widget_type != val2->widget_type)
2210 return val1->widget_type < val2->widget_type ? -1 : 1;
2212 if (val1->pspec != val2->pspec)
2213 return val1->pspec < val2->pspec ? -1 : 1;
2215 if (val1->state != val2->state)
2216 return val1->state < val2->state ? -1 : 1;
2222 _gtk_style_context_peek_property (GtkStyleContext *context,
2225 StyleData *data = style_data_lookup (context);
2227 return _gtk_css_computed_values_get_value (data->store, property_id);
2231 _gtk_style_context_get_number (GtkStyleContext *context,
2233 double one_hundred_percent)
2237 value = _gtk_style_context_peek_property (context, property_id);
2238 return _gtk_css_number_value_get (value, one_hundred_percent);
2242 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2244 GtkStateFlags state,
2247 GtkStyleContextPrivate *priv;
2248 PropertyValue *pcache, key = { 0 };
2252 priv = context->priv;
2254 data = style_data_lookup_for_state (context, state);
2256 key.widget_type = widget_type;
2260 /* need value cache array */
2261 if (!data->property_cache)
2262 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2265 pcache = bsearch (&key,
2266 data->property_cache->data, data->property_cache->len,
2267 sizeof (PropertyValue), style_property_values_cmp);
2269 return &pcache->value;
2273 while (i < data->property_cache->len &&
2274 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2277 g_array_insert_val (data->property_cache, i, key);
2278 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2280 /* cache miss, initialize value type, then set contents */
2281 g_param_spec_ref (pcache->pspec);
2282 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2284 if (priv->widget || priv->widget_path)
2286 GtkWidgetPath *widget_path = priv->widget ? _gtk_widget_create_path (priv->widget) : priv->widget_path;
2288 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2290 state, pspec, &pcache->value))
2292 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2293 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2295 GtkSymbolicColor *color;
2298 color = g_value_dup_boxed (&pcache->value);
2300 g_value_unset (&pcache->value);
2302 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2303 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2305 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2307 if (_gtk_style_context_resolve_color (context, color, &rgba, NULL))
2309 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2310 g_value_set_boxed (&pcache->value, &rgba);
2315 rgb.red = rgba.red * 65535. + 0.5;
2316 rgb.green = rgba.green * 65535. + 0.5;
2317 rgb.blue = rgba.blue * 65535. + 0.5;
2319 g_value_set_boxed (&pcache->value, &rgb);
2323 g_param_value_set_default (pspec, &pcache->value);
2325 gtk_symbolic_color_unref (color);
2329 gtk_widget_path_free (widget_path);
2331 return &pcache->value;
2335 gtk_widget_path_free (widget_path);
2338 /* not supplied by any provider, revert to default */
2339 g_param_value_set_default (pspec, &pcache->value);
2341 return &pcache->value;
2345 * gtk_style_context_get_style_property:
2346 * @context: a #GtkStyleContext
2347 * @property_name: the name of the widget style property
2348 * @value: Return location for the property value
2350 * Gets the value for a widget style property.
2352 * When @value is no longer needed, g_value_unset() must be called
2353 * to free any allocated memory.
2356 gtk_style_context_get_style_property (GtkStyleContext *context,
2357 const gchar *property_name,
2360 GtkStyleContextPrivate *priv;
2361 GtkWidgetClass *widget_class;
2362 GtkStateFlags state;
2364 const GValue *peek_value;
2367 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2368 g_return_if_fail (property_name != NULL);
2369 g_return_if_fail (value != NULL);
2371 priv = context->priv;
2375 widget_type = G_OBJECT_TYPE (priv->widget);
2379 if (!priv->widget_path)
2382 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2384 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2386 g_warning ("%s: can't get style properties for non-widget class `%s'",
2388 g_type_name (widget_type));
2393 widget_class = g_type_class_ref (widget_type);
2394 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2395 g_type_class_unref (widget_class);
2399 g_warning ("%s: widget class `%s' has no style property named `%s'",
2401 g_type_name (widget_type),
2406 state = gtk_style_context_get_state (context);
2407 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2410 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2411 g_value_copy (peek_value, value);
2412 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2413 g_value_transform (peek_value, value);
2415 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2417 G_VALUE_TYPE_NAME (peek_value),
2418 G_VALUE_TYPE_NAME (value));
2422 * gtk_style_context_get_style_valist:
2423 * @context: a #GtkStyleContext
2424 * @args: va_list of property name/return location pairs, followed by %NULL
2426 * Retrieves several widget style properties from @context according to the
2432 gtk_style_context_get_style_valist (GtkStyleContext *context,
2435 GtkStyleContextPrivate *priv;
2436 const gchar *prop_name;
2437 GtkStateFlags state;
2440 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2442 prop_name = va_arg (args, const gchar *);
2443 priv = context->priv;
2447 widget_type = G_OBJECT_TYPE (priv->widget);
2451 if (!priv->widget_path)
2454 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2456 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2458 g_warning ("%s: can't get style properties for non-widget class `%s'",
2460 g_type_name (widget_type));
2465 state = gtk_style_context_get_state (context);
2469 GtkWidgetClass *widget_class;
2471 const GValue *peek_value;
2474 widget_class = g_type_class_ref (widget_type);
2475 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2476 g_type_class_unref (widget_class);
2480 g_warning ("%s: widget class `%s' has no style property named `%s'",
2482 g_type_name (widget_type),
2487 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2490 G_VALUE_LCOPY (peek_value, args, 0, &error);
2494 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2496 G_VALUE_TYPE_NAME (peek_value),
2502 prop_name = va_arg (args, const gchar *);
2507 * gtk_style_context_get_style:
2508 * @context: a #GtkStyleContext
2509 * @...: property name /return value pairs, followed by %NULL
2511 * Retrieves several widget style properties from @context according to the
2517 gtk_style_context_get_style (GtkStyleContext *context,
2522 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2524 va_start (args, context);
2525 gtk_style_context_get_style_valist (context, args);
2531 * gtk_style_context_lookup_icon_set:
2532 * @context: a #GtkStyleContext
2533 * @stock_id: an icon name
2535 * Looks up @stock_id in the icon factories associated to @context and
2536 * the default icon factory, returning an icon set if found, otherwise
2539 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2542 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2543 const gchar *stock_id)
2545 GtkStyleContextPrivate *priv;
2547 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2548 g_return_val_if_fail (stock_id != NULL, NULL);
2550 priv = context->priv;
2551 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2553 return gtk_icon_factory_lookup_default (stock_id);
2557 * gtk_style_context_set_screen:
2558 * @context: a #GtkStyleContext
2559 * @screen: a #GdkScreen
2561 * Attaches @context to the given screen.
2563 * The screen is used to add style information from 'global' style
2564 * providers, such as the screens #GtkSettings instance.
2566 * If you are using a #GtkStyleContext returned from
2567 * gtk_widget_get_style_context(), you do not need to
2568 * call this yourself.
2573 gtk_style_context_set_screen (GtkStyleContext *context,
2576 GtkStyleContextPrivate *priv;
2578 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2579 g_return_if_fail (GDK_IS_SCREEN (screen));
2581 priv = context->priv;
2582 if (priv->screen == screen)
2585 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2587 gtk_style_context_set_cascade (context, _gtk_style_cascade_get_for_screen (screen));
2591 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2594 priv->screen = screen;
2596 g_object_notify (G_OBJECT (context), "screen");
2600 * gtk_style_context_get_screen:
2601 * @context: a #GtkStyleContext
2603 * Returns the #GdkScreen to which @context is attached.
2605 * Returns: (transfer none): a #GdkScreen.
2608 gtk_style_context_get_screen (GtkStyleContext *context)
2610 GtkStyleContextPrivate *priv;
2612 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2614 priv = context->priv;
2615 return priv->screen;
2619 * gtk_style_context_set_direction:
2620 * @context: a #GtkStyleContext
2621 * @direction: the new direction.
2623 * Sets the reading direction for rendering purposes.
2625 * If you are using a #GtkStyleContext returned from
2626 * gtk_widget_get_style_context(), you do not need to
2627 * call this yourself.
2632 gtk_style_context_set_direction (GtkStyleContext *context,
2633 GtkTextDirection direction)
2635 GtkStyleContextPrivate *priv;
2637 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2639 priv = context->priv;
2640 priv->direction = direction;
2642 g_object_notify (G_OBJECT (context), "direction");
2646 * gtk_style_context_get_direction:
2647 * @context: a #GtkStyleContext
2649 * Returns the widget direction used for rendering.
2651 * Returns: the widget direction
2656 gtk_style_context_get_direction (GtkStyleContext *context)
2658 GtkStyleContextPrivate *priv;
2660 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2662 priv = context->priv;
2663 return priv->direction;
2667 * gtk_style_context_set_junction_sides:
2668 * @context: a #GtkStyleContext
2669 * @sides: sides where rendered elements are visually connected to
2672 * Sets the sides where rendered elements (mostly through
2673 * gtk_render_frame()) will visually connect with other visual elements.
2675 * This is merely a hint that may or may not be honored
2676 * by theming engines.
2678 * Container widgets are expected to set junction hints as appropriate
2679 * for their children, so it should not normally be necessary to call
2680 * this function manually.
2685 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2686 GtkJunctionSides sides)
2688 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2690 context->priv->info->junction_sides = sides;
2694 * gtk_style_context_get_junction_sides:
2695 * @context: a #GtkStyleContext
2697 * Returns the sides where rendered elements connect visually with others.
2699 * Returns: the junction sides
2704 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2706 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2708 return context->priv->info->junction_sides;
2711 static GtkSymbolicColor *
2712 gtk_style_context_color_lookup_func (gpointer contextp,
2715 GtkStyleContext *context = contextp;
2717 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2721 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2722 GtkCssValue *current,
2723 GtkCssDependencies current_deps,
2725 GtkCssDependencies *dependencies)
2727 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2728 g_return_val_if_fail (current != NULL, FALSE);
2729 g_return_val_if_fail (color != NULL, FALSE);
2731 return _gtk_symbolic_color_resolve_full ((GtkSymbolicColor *) color,
2734 gtk_style_context_color_lookup_func,
2741 _gtk_style_context_resolve_color (GtkStyleContext *context,
2742 GtkSymbolicColor *color,
2744 GtkCssDependencies *dependencies)
2748 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2749 g_return_val_if_fail (color != NULL, FALSE);
2750 g_return_val_if_fail (result != NULL, FALSE);
2752 val = _gtk_symbolic_color_resolve_full (color,
2753 _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR),
2754 GTK_CSS_DEPENDS_ON_COLOR,
2755 gtk_style_context_color_lookup_func,
2761 *result = *_gtk_css_rgba_value_get_rgba (val);
2762 _gtk_css_value_unref (val);
2767 * gtk_style_context_lookup_color:
2768 * @context: a #GtkStyleContext
2769 * @color_name: color name to lookup
2770 * @color: (out): Return location for the looked up color
2772 * Looks up and resolves a color name in the @context color map.
2774 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2777 gtk_style_context_lookup_color (GtkStyleContext *context,
2778 const gchar *color_name,
2781 GtkSymbolicColor *sym_color;
2783 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2784 g_return_val_if_fail (color_name != NULL, FALSE);
2785 g_return_val_if_fail (color != NULL, FALSE);
2787 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2788 if (sym_color == NULL)
2791 return _gtk_style_context_resolve_color (context, sym_color, color, NULL);
2795 * gtk_style_context_notify_state_change:
2796 * @context: a #GtkStyleContext
2797 * @window: a #GdkWindow
2798 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2799 * See gtk_style_context_push_animatable_region()
2800 * @state: state to trigger transition for
2801 * @state_value: %TRUE if @state is the state we are changing to,
2802 * %FALSE if we are changing away from it
2804 * Notifies a state change on @context, so if the current style makes use
2805 * of transition animations, one will be started so all rendered elements
2806 * under @region_id are animated for state @state being set to value
2809 * The @window parameter is used in order to invalidate the rendered area
2810 * as the animation runs, so make sure it is the same window that is being
2811 * rendered on by the gtk_render_*() functions.
2813 * If @region_id is %NULL, all rendered elements using @context will be
2814 * affected by this state transition.
2816 * As a practical example, a #GtkButton notifying a state transition on
2817 * the prelight state:
2819 * gtk_style_context_notify_state_change (context,
2820 * gtk_widget_get_window (widget),
2822 * GTK_STATE_PRELIGHT,
2823 * button->in_button);
2826 * Can be handled in the CSS file like this:
2829 * background-color: #f00
2833 * background-color: #fff;
2834 * transition: 200ms linear
2838 * This combination will animate the button background from red to white
2839 * if a pointer enters the button, and back to red if the pointer leaves
2842 * Note that @state is used when finding the transition parameters, which
2843 * is why the style places the transition under the :hover pseudo-class.
2847 * Deprecated: 3.6: This function does nothing.
2850 gtk_style_context_notify_state_change (GtkStyleContext *context,
2854 gboolean state_value)
2856 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2857 g_return_if_fail (GDK_IS_WINDOW (window));
2858 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2859 g_return_if_fail (context->priv->widget != NULL || context->priv->widget_path != NULL);
2863 * gtk_style_context_cancel_animations:
2864 * @context: a #GtkStyleContext
2865 * @region_id: (allow-none): animatable region to stop, or %NULL.
2866 * See gtk_style_context_push_animatable_region()
2868 * Stops all running animations for @region_id and all animatable
2869 * regions underneath.
2871 * A %NULL @region_id will stop all ongoing animations in @context,
2872 * when dealing with a #GtkStyleContext obtained through
2873 * gtk_widget_get_style_context(), this is normally done for you
2874 * in all circumstances you would expect all widget to be stopped,
2875 * so this should be only used in complex widgets with different
2876 * animatable regions.
2880 * Deprecated: 3.6: This function does nothing.
2883 gtk_style_context_cancel_animations (GtkStyleContext *context,
2886 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2890 * gtk_style_context_scroll_animations:
2891 * @context: a #GtkStyleContext
2892 * @window: a #GdkWindow used previously in
2893 * gtk_style_context_notify_state_change()
2894 * @dx: Amount to scroll in the X axis
2895 * @dy: Amount to scroll in the Y axis
2897 * This function is analogous to gdk_window_scroll(), and
2898 * should be called together with it so the invalidation
2899 * areas for any ongoing animation are scrolled together
2904 * Deprecated: 3.6: This function does nothing.
2907 gtk_style_context_scroll_animations (GtkStyleContext *context,
2912 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2913 g_return_if_fail (GDK_IS_WINDOW (window));
2917 * gtk_style_context_push_animatable_region:
2918 * @context: a #GtkStyleContext
2919 * @region_id: unique identifier for the animatable region
2921 * Pushes an animatable region, so all further gtk_render_*() calls between
2922 * this call and the following gtk_style_context_pop_animatable_region()
2923 * will potentially show transition animations for this region if
2924 * gtk_style_context_notify_state_change() is called for a given state,
2925 * and the current theme/style defines transition animations for state
2928 * The @region_id used must be unique in @context so the theming engine
2929 * can uniquely identify rendered elements subject to a state transition.
2933 * Deprecated: 3.6: This function does nothing.
2936 gtk_style_context_push_animatable_region (GtkStyleContext *context,
2939 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2940 g_return_if_fail (region_id != NULL);
2944 * gtk_style_context_pop_animatable_region:
2945 * @context: a #GtkStyleContext
2947 * Pops an animatable region from @context.
2948 * See gtk_style_context_push_animatable_region().
2952 * Deprecated: 3.6: This function does nothing.
2955 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
2957 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2961 gtk_style_context_clear_cache (GtkStyleContext *context)
2963 GtkStyleContextPrivate *priv;
2966 priv = context->priv;
2968 for (info = priv->info; info; info = info->next)
2970 style_info_set_data (info, NULL);
2972 g_hash_table_remove_all (priv->style_data);
2976 gtk_style_context_update_cache (GtkStyleContext *context,
2977 const GtkBitmask *parent_changes)
2979 GtkStyleContextPrivate *priv;
2980 GHashTableIter iter;
2981 gpointer key, value;
2983 priv = context->priv;
2985 g_hash_table_iter_init (&iter, priv->style_data);
2986 while (g_hash_table_iter_next (&iter, &key, &value))
2988 GtkStyleInfo *info = key;
2989 StyleData *data = value;
2990 GtkBitmask *changes;
2992 changes = _gtk_bitmask_copy (parent_changes);
2993 changes = _gtk_bitmask_intersect (changes, data->store->depends_on_parent);
2994 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
2995 changes = _gtk_bitmask_union (changes, data->store->depends_on_color);
2996 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
2997 changes = _gtk_bitmask_union (changes, data->store->depends_on_font_size);
2999 build_properties (context, data->store, info, changes);
3004 gtk_style_context_do_invalidate (GtkStyleContext *context)
3006 GtkStyleContextPrivate *priv;
3008 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3010 priv = context->priv;
3012 /* Avoid reentrancy */
3013 if (priv->invalidating_context)
3016 priv->invalidating_context = TRUE;
3018 g_signal_emit (context, signals[CHANGED], 0);
3020 priv->invalidating_context = FALSE;
3024 gtk_style_context_update_animations (GtkStyleContext *context,
3027 GtkBitmask *differences;
3028 StyleData *style_data;
3030 style_data = style_data_lookup (context);
3032 differences = _gtk_css_computed_values_advance (style_data->store,
3035 if (_gtk_css_computed_values_is_static (style_data->store))
3036 _gtk_style_context_update_animating (context);
3042 gtk_style_context_needs_full_revalidate (GtkStyleContext *context,
3043 GtkCssChange change)
3045 GtkStyleContextPrivate *priv = context->priv;
3047 /* Try to avoid invalidating if we can */
3048 if (change & GTK_STYLE_CONTEXT_RADICAL_CHANGE)
3050 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
3054 if (priv->relevant_changes == GTK_CSS_CHANGE_ANY)
3056 GtkWidgetPath *path;
3057 GtkCssMatcher matcher;
3059 path = create_query_path (context, priv->info);
3060 if (_gtk_css_matcher_init (&matcher, path, priv->info->state_flags))
3061 priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3064 priv->relevant_changes = 0;
3066 priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
3068 gtk_widget_path_unref (path);
3072 if (priv->relevant_changes & change)
3079 gtk_style_context_should_create_transitions (GtkStyleContext *context)
3081 GtkStyleContextPrivate *priv;
3084 priv = context->priv;
3086 if (priv->widget == NULL)
3089 if (!gtk_widget_get_mapped (priv->widget))
3092 g_object_get (gtk_widget_get_settings (context->priv->widget),
3093 "gtk-enable-animations", &animate,
3100 _gtk_style_context_validate (GtkStyleContext *context,
3102 GtkCssChange change,
3103 const GtkBitmask *parent_changes)
3105 GtkStyleContextPrivate *priv;
3108 GtkBitmask *changes;
3111 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3113 priv = context->priv;
3115 change |= priv->pending_changes;
3117 /* If you run your application with
3118 * GTK_DEBUG=no-css-cache
3119 * every invalidation will purge the cache and completely query
3120 * everything anew form the cache. This is slow (in particular
3121 * when animating), but useful for figuring out bugs.
3123 * We achieve that by pretending that everything that could have
3124 * changed has and so we of course totally need to redo everything.
3126 * Note that this also completely revalidates child widgets all
3129 if (G_UNLIKELY (gtk_get_debug_flags () & GTK_DEBUG_NO_CSS_CACHE))
3130 change = GTK_CSS_CHANGE_ANY;
3132 if (!priv->invalid && change == 0 && _gtk_bitmask_is_empty (parent_changes))
3135 priv->pending_changes = 0;
3136 gtk_style_context_set_invalid (context, FALSE);
3140 current = style_data_ref (info->data);
3144 /* Try to avoid invalidating if we can */
3145 if (current == NULL ||
3146 gtk_style_context_needs_full_revalidate (context, change))
3150 if ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE)
3152 gtk_style_context_clear_cache (context);
3156 gtk_style_context_update_cache (context, parent_changes);
3157 style_info_set_data (info, NULL);
3160 data = style_data_lookup (context);
3162 _gtk_css_computed_values_create_animations (data->store,
3164 current && gtk_style_context_should_create_transitions (context) ? current->store : NULL,
3166 if (_gtk_css_computed_values_is_static (data->store))
3167 change &= ~GTK_CSS_CHANGE_ANIMATE;
3169 change |= GTK_CSS_CHANGE_ANIMATE;
3170 _gtk_style_context_update_animating (context);
3174 changes = _gtk_css_computed_values_get_difference (data->store, current->store);
3176 /* In the case where we keep the cache, we want unanimated values */
3177 _gtk_css_computed_values_cancel_animations (current->store);
3181 changes = _gtk_bitmask_new ();
3182 changes = _gtk_bitmask_invert_range (changes, 0, _gtk_css_style_property_get_n_properties ());
3187 changes = _gtk_bitmask_copy (parent_changes);
3188 changes = _gtk_bitmask_intersect (changes, current->store->depends_on_parent);
3189 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
3190 changes = _gtk_bitmask_union (changes, current->store->depends_on_color);
3191 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
3192 changes = _gtk_bitmask_union (changes, current->store->depends_on_font_size);
3194 gtk_style_context_update_cache (context, parent_changes);
3198 style_data_unref (current);
3200 if (change & GTK_CSS_CHANGE_ANIMATE &&
3201 gtk_style_context_is_animating (context))
3203 GtkBitmask *animation_changes;
3205 animation_changes = gtk_style_context_update_animations (context, timestamp);
3206 changes = _gtk_bitmask_union (changes, animation_changes);
3207 _gtk_bitmask_free (animation_changes);
3210 if (!_gtk_bitmask_is_empty (changes))
3211 gtk_style_context_do_invalidate (context);
3213 change = _gtk_css_change_for_child (change);
3214 for (list = priv->children; list; list = list->next)
3216 _gtk_style_context_validate (list->data, timestamp, change, changes);
3219 _gtk_bitmask_free (changes);
3223 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3224 GtkCssChange change)
3226 GtkStyleContextPrivate *priv;
3228 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3229 g_return_if_fail (change != 0);
3231 priv = context->priv;
3233 if (priv->widget != NULL)
3235 priv->pending_changes |= change;
3236 gtk_style_context_set_invalid (context, TRUE);
3238 else if (priv->widget_path == NULL)
3240 gtk_style_context_invalidate (context);
3245 * gtk_style_context_invalidate:
3246 * @context: a #GtkStyleContext.
3248 * Invalidates @context style information, so it will be reconstructed
3251 * If you're using a #GtkStyleContext returned from
3252 * gtk_widget_get_style_context(), you do not need to
3253 * call this yourself.
3258 gtk_style_context_invalidate (GtkStyleContext *context)
3260 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3262 gtk_style_context_clear_cache (context);
3263 gtk_style_context_do_invalidate (context);
3267 * gtk_style_context_set_background:
3268 * @context: a #GtkStyleContext
3269 * @window: a #GdkWindow
3271 * Sets the background of @window to the background pattern or
3272 * color specified in @context for its current state.
3277 gtk_style_context_set_background (GtkStyleContext *context,
3280 GtkStateFlags state;
3281 cairo_pattern_t *pattern;
3284 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3285 g_return_if_fail (GDK_IS_WINDOW (window));
3287 state = gtk_style_context_get_state (context);
3288 gtk_style_context_get (context, state,
3289 "background-image", &pattern,
3293 gdk_window_set_background_pattern (window, pattern);
3294 cairo_pattern_destroy (pattern);
3298 gtk_style_context_get (context, state,
3299 "background-color", &color,
3303 gdk_window_set_background_rgba (window, color);
3304 gdk_rgba_free (color);
3309 * gtk_style_context_get_color:
3310 * @context: a #GtkStyleContext
3311 * @state: state to retrieve the color for
3312 * @color: (out): return value for the foreground color
3314 * Gets the foreground color for a given state.
3319 gtk_style_context_get_color (GtkStyleContext *context,
3320 GtkStateFlags state,
3325 g_return_if_fail (color != NULL);
3326 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3328 gtk_style_context_get (context,
3338 * gtk_style_context_get_background_color:
3339 * @context: a #GtkStyleContext
3340 * @state: state to retrieve the color for
3341 * @color: (out): return value for the background color
3343 * Gets the background color for a given state.
3348 gtk_style_context_get_background_color (GtkStyleContext *context,
3349 GtkStateFlags state,
3354 g_return_if_fail (color != NULL);
3355 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3357 gtk_style_context_get (context,
3359 "background-color", &c,
3367 * gtk_style_context_get_border_color:
3368 * @context: a #GtkStyleContext
3369 * @state: state to retrieve the color for
3370 * @color: (out): return value for the border color
3372 * Gets the border color for a given state.
3377 gtk_style_context_get_border_color (GtkStyleContext *context,
3378 GtkStateFlags state,
3383 g_return_if_fail (color != NULL);
3384 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3386 gtk_style_context_get (context,
3396 * gtk_style_context_get_border:
3397 * @context: a #GtkStyleContext
3398 * @state: state to retrieve the border for
3399 * @border: (out): return value for the border settings
3401 * Gets the border for a given state as a #GtkBorder.
3402 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3407 gtk_style_context_get_border (GtkStyleContext *context,
3408 GtkStateFlags state,
3411 int top, left, bottom, right;
3413 g_return_if_fail (border != NULL);
3414 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3416 gtk_style_context_get (context,
3418 "border-top-width", &top,
3419 "border-left-width", &left,
3420 "border-bottom-width", &bottom,
3421 "border-right-width", &right,
3425 border->left = left;
3426 border->bottom = bottom;
3427 border->right = right;
3431 * gtk_style_context_get_padding:
3432 * @context: a #GtkStyleContext
3433 * @state: state to retrieve the padding for
3434 * @padding: (out): return value for the padding settings
3436 * Gets the padding for a given state as a #GtkBorder.
3437 * See %GTK_STYLE_PROPERTY_PADDING.
3442 gtk_style_context_get_padding (GtkStyleContext *context,
3443 GtkStateFlags state,
3446 int top, left, bottom, right;
3448 g_return_if_fail (padding != NULL);
3449 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3451 gtk_style_context_get (context,
3453 "padding-top", &top,
3454 "padding-left", &left,
3455 "padding-bottom", &bottom,
3456 "padding-right", &right,
3460 padding->left = left;
3461 padding->bottom = bottom;
3462 padding->right = right;
3466 * gtk_style_context_get_margin:
3467 * @context: a #GtkStyleContext
3468 * @state: state to retrieve the border for
3469 * @margin: (out): return value for the margin settings
3471 * Gets the margin for a given state as a #GtkBorder.
3472 * See %GTK_STYLE_PROPERTY_MARGIN.
3477 gtk_style_context_get_margin (GtkStyleContext *context,
3478 GtkStateFlags state,
3481 int top, left, bottom, right;
3483 g_return_if_fail (margin != NULL);
3484 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3486 gtk_style_context_get (context,
3489 "margin-left", &left,
3490 "margin-bottom", &bottom,
3491 "margin-right", &right,
3495 margin->left = left;
3496 margin->bottom = bottom;
3497 margin->right = right;
3501 * gtk_style_context_get_font:
3502 * @context: a #GtkStyleContext
3503 * @state: state to retrieve the font for
3505 * Returns the font description for a given state. The returned
3506 * object is const and will remain valid until the
3507 * #GtkStyleContext::changed signal happens.
3509 * Returns: (transfer none): the #PangoFontDescription for the given
3510 * state. This object is owned by GTK+ and should not be
3515 const PangoFontDescription *
3516 gtk_style_context_get_font (GtkStyleContext *context,
3517 GtkStateFlags state)
3519 GtkStyleContextPrivate *priv;
3521 PangoFontDescription *description;
3523 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3525 priv = context->priv;
3526 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3528 data = style_data_lookup_for_state (context, state);
3530 /* Yuck, fonts are created on-demand but we don't return a ref.
3531 * Do bad things to achieve this requirement */
3532 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3533 if (description == NULL)
3535 gtk_style_context_get (context, state, "font", &description, NULL);
3536 g_object_set_data_full (G_OBJECT (data->store),
3537 "font-cache-for-get_font",
3539 (GDestroyNotify) pango_font_description_free);
3545 get_cursor_color (GtkStyleContext *context,
3549 GdkColor *style_color;
3551 gtk_style_context_get_style (context,
3552 primary ? "cursor-color" : "secondary-cursor-color",
3558 color->red = style_color->red / 65535.0;
3559 color->green = style_color->green / 65535.0;
3560 color->blue = style_color->blue / 65535.0;
3563 gdk_color_free (style_color);
3567 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3573 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3575 color->red = (color->red + bg.red) * 0.5;
3576 color->green = (color->green + bg.green) * 0.5;
3577 color->blue = (color->blue + bg.blue) * 0.5;
3583 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3584 GdkRGBA *primary_color,
3585 GdkRGBA *secondary_color)
3588 get_cursor_color (context, TRUE, primary_color);
3590 if (secondary_color)
3591 get_cursor_color (context, FALSE, secondary_color);
3598 * @context: a #GtkStyleContext
3600 * @x: X origin of the rectangle
3601 * @y: Y origin of the rectangle
3602 * @width: rectangle width
3603 * @height: rectangle height
3605 * Renders a checkmark (as in a #GtkCheckButton).
3607 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3608 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3609 * should be marked as undefined.
3612 * <title>Typical checkmark rendering</title>
3613 * <inlinegraphic fileref="checks.png" format="PNG"/>
3619 gtk_render_check (GtkStyleContext *context,
3626 GtkThemingEngineClass *engine_class;
3627 GtkThemingEngine *engine;
3629 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3630 g_return_if_fail (cr != NULL);
3632 if (width <= 0 || height <= 0)
3635 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3636 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3641 _gtk_theming_engine_set_context (engine, context);
3642 engine_class->render_check (engine, cr,
3643 x, y, width, height);
3649 * gtk_render_option:
3650 * @context: a #GtkStyleContext
3652 * @x: X origin of the rectangle
3653 * @y: Y origin of the rectangle
3654 * @width: rectangle width
3655 * @height: rectangle height
3657 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3658 * state will determine whether the option is on or off, and
3659 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3662 * <title>Typical option mark rendering</title>
3663 * <inlinegraphic fileref="options.png" format="PNG"/>
3669 gtk_render_option (GtkStyleContext *context,
3676 GtkThemingEngineClass *engine_class;
3677 GtkThemingEngine *engine;
3679 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3680 g_return_if_fail (cr != NULL);
3682 if (width <= 0 || height <= 0)
3685 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3686 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3690 _gtk_theming_engine_set_context (engine, context);
3691 engine_class->render_option (engine, cr,
3692 x, y, width, height);
3699 * @context: a #GtkStyleContext
3701 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3702 * @x: X origin of the render area
3703 * @y: Y origin of the render area
3704 * @size: square side for render area
3706 * Renders an arrow pointing to @angle.
3709 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3710 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3716 gtk_render_arrow (GtkStyleContext *context,
3723 GtkThemingEngineClass *engine_class;
3724 GtkThemingEngine *engine;
3726 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3727 g_return_if_fail (cr != NULL);
3732 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3733 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3737 gtk_style_context_save (context);
3738 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3740 _gtk_theming_engine_set_context (engine, context);
3741 engine_class->render_arrow (engine, cr,
3744 gtk_style_context_restore (context);
3749 * gtk_render_background:
3750 * @context: a #GtkStyleContext
3752 * @x: X origin of the rectangle
3753 * @y: Y origin of the rectangle
3754 * @width: rectangle width
3755 * @height: rectangle height
3757 * Renders the background of an element.
3760 * <title>Typical background rendering, showing the effect of
3761 * <parameter>background-image</parameter>,
3762 * <parameter>border-width</parameter> and
3763 * <parameter>border-radius</parameter></title>
3764 * <inlinegraphic fileref="background.png" format="PNG"/>
3770 gtk_render_background (GtkStyleContext *context,
3777 GtkThemingEngineClass *engine_class;
3778 GtkThemingEngine *engine;
3780 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3781 g_return_if_fail (cr != NULL);
3783 if (width <= 0 || height <= 0)
3786 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3787 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3791 _gtk_theming_engine_set_context (engine, context);
3792 engine_class->render_background (engine, cr, x, y, width, height);
3799 * @context: a #GtkStyleContext
3801 * @x: X origin of the rectangle
3802 * @y: Y origin of the rectangle
3803 * @width: rectangle width
3804 * @height: rectangle height
3806 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3809 * <title>Examples of frame rendering, showing the effect of
3810 * <parameter>border-image</parameter>,
3811 * <parameter>border-color</parameter>,
3812 * <parameter>border-width</parameter>,
3813 * <parameter>border-radius</parameter> and
3815 * <inlinegraphic fileref="frames.png" format="PNG"/>
3821 gtk_render_frame (GtkStyleContext *context,
3828 GtkThemingEngineClass *engine_class;
3829 GtkThemingEngine *engine;
3831 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3832 g_return_if_fail (cr != NULL);
3834 if (width <= 0 || height <= 0)
3837 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3838 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3842 _gtk_theming_engine_set_context (engine, context);
3843 engine_class->render_frame (engine, cr, x, y, width, height);
3849 * gtk_render_expander:
3850 * @context: a #GtkStyleContext
3852 * @x: X origin of the rectangle
3853 * @y: Y origin of the rectangle
3854 * @width: rectangle width
3855 * @height: rectangle height
3857 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3858 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3859 * determines whether the expander is collapsed or expanded.
3862 * <title>Typical expander rendering</title>
3863 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3869 gtk_render_expander (GtkStyleContext *context,
3876 GtkThemingEngineClass *engine_class;
3877 GtkThemingEngine *engine;
3879 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3880 g_return_if_fail (cr != NULL);
3882 if (width <= 0 || height <= 0)
3885 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3886 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3890 _gtk_theming_engine_set_context (engine, context);
3891 engine_class->render_expander (engine, cr, x, y, width, height);
3898 * @context: a #GtkStyleContext
3900 * @x: X origin of the rectangle
3901 * @y: Y origin of the rectangle
3902 * @width: rectangle width
3903 * @height: rectangle height
3905 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3907 * <title>Typical focus rendering</title>
3908 * <inlinegraphic fileref="focus.png" format="PNG"/>
3914 gtk_render_focus (GtkStyleContext *context,
3921 GtkThemingEngineClass *engine_class;
3922 GtkThemingEngine *engine;
3924 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3925 g_return_if_fail (cr != NULL);
3927 if (width <= 0 || height <= 0)
3930 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3931 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3935 _gtk_theming_engine_set_context (engine, context);
3936 engine_class->render_focus (engine, cr, x, y, width, height);
3942 * gtk_render_layout:
3943 * @context: a #GtkStyleContext
3947 * @layout: the #PangoLayout to render
3949 * Renders @layout on the coordinates @x, @y
3954 gtk_render_layout (GtkStyleContext *context,
3958 PangoLayout *layout)
3960 GtkThemingEngineClass *engine_class;
3961 GtkThemingEngine *engine;
3962 PangoRectangle extents;
3964 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3965 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3966 g_return_if_fail (cr != NULL);
3968 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3969 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3973 pango_layout_get_extents (layout, &extents, NULL);
3975 _gtk_theming_engine_set_context (engine, context);
3976 engine_class->render_layout (engine, cr, x, y, layout);
3983 * @context: a #GtkStyleContext
3985 * @x0: X coordinate for the origin of the line
3986 * @y0: Y coordinate for the origin of the line
3987 * @x1: X coordinate for the end of the line
3988 * @y1: Y coordinate for the end of the line
3990 * Renders a line from (x0, y0) to (x1, y1).
3995 gtk_render_line (GtkStyleContext *context,
4002 GtkThemingEngineClass *engine_class;
4003 GtkThemingEngine *engine;
4005 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4006 g_return_if_fail (cr != NULL);
4008 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4009 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4013 _gtk_theming_engine_set_context (engine, context);
4014 engine_class->render_line (engine, cr, x0, y0, x1, y1);
4020 * gtk_render_slider:
4021 * @context: a #GtkStyleContext
4023 * @x: X origin of the rectangle
4024 * @y: Y origin of the rectangle
4025 * @width: rectangle width
4026 * @height: rectangle height
4027 * @orientation: orientation of the slider
4029 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4030 * @width, @height. @orientation defines whether the slider is vertical
4034 * <title>Typical slider rendering</title>
4035 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4041 gtk_render_slider (GtkStyleContext *context,
4047 GtkOrientation orientation)
4049 GtkThemingEngineClass *engine_class;
4050 GtkThemingEngine *engine;
4052 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4053 g_return_if_fail (cr != NULL);
4055 if (width <= 0 || height <= 0)
4058 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4059 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4063 _gtk_theming_engine_set_context (engine, context);
4064 engine_class->render_slider (engine, cr, x, y, width, height, orientation);
4070 * gtk_render_frame_gap:
4071 * @context: a #GtkStyleContext
4073 * @x: X origin of the rectangle
4074 * @y: Y origin of the rectangle
4075 * @width: rectangle width
4076 * @height: rectangle height
4077 * @gap_side: side where the gap is
4078 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4079 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4081 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4082 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4083 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4084 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4087 * <title>Typical rendering of a frame with a gap</title>
4088 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4094 gtk_render_frame_gap (GtkStyleContext *context,
4100 GtkPositionType gap_side,
4104 GtkThemingEngineClass *engine_class;
4105 GtkThemingEngine *engine;
4107 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4108 g_return_if_fail (cr != NULL);
4109 g_return_if_fail (xy0_gap <= xy1_gap);
4110 g_return_if_fail (xy0_gap >= 0);
4112 if (width <= 0 || height <= 0)
4115 if (gap_side == GTK_POS_LEFT ||
4116 gap_side == GTK_POS_RIGHT)
4117 g_return_if_fail (xy1_gap <= height);
4119 g_return_if_fail (xy1_gap <= width);
4121 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4122 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4126 _gtk_theming_engine_set_context (engine, context);
4127 engine_class->render_frame_gap (engine, cr,
4128 x, y, width, height, gap_side,
4135 * gtk_render_extension:
4136 * @context: a #GtkStyleContext
4138 * @x: X origin of the rectangle
4139 * @y: Y origin of the rectangle
4140 * @width: rectangle width
4141 * @height: rectangle height
4142 * @gap_side: side where the gap is
4144 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4145 * defined by @x, @y, @width, @height. The side where the extension
4146 * connects to is defined by @gap_side.
4149 * <title>Typical extension rendering</title>
4150 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4156 gtk_render_extension (GtkStyleContext *context,
4162 GtkPositionType gap_side)
4164 GtkThemingEngineClass *engine_class;
4165 GtkThemingEngine *engine;
4167 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4168 g_return_if_fail (cr != NULL);
4170 if (width <= 0 || height <= 0)
4173 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4174 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4178 _gtk_theming_engine_set_context (engine, context);
4179 engine_class->render_extension (engine, cr, x, y, width, height, gap_side);
4185 * gtk_render_handle:
4186 * @context: a #GtkStyleContext
4188 * @x: X origin of the rectangle
4189 * @y: Y origin of the rectangle
4190 * @width: rectangle width
4191 * @height: rectangle height
4193 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4194 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4195 * determined by @x, @y, @width, @height.
4198 * <title>Handles rendered for the paned and grip classes</title>
4199 * <inlinegraphic fileref="handles.png" format="PNG"/>
4205 gtk_render_handle (GtkStyleContext *context,
4212 GtkThemingEngineClass *engine_class;
4213 GtkThemingEngine *engine;
4215 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4216 g_return_if_fail (cr != NULL);
4218 if (width <= 0 || height <= 0)
4221 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4222 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4226 _gtk_theming_engine_set_context (engine, context);
4227 engine_class->render_handle (engine, cr, x, y, width, height);
4233 * gtk_render_activity:
4234 * @context: a #GtkStyleContext
4236 * @x: X origin of the rectangle
4237 * @y: Y origin of the rectangle
4238 * @width: rectangle width
4239 * @height: rectangle height
4241 * Renders an activity area (Such as in #GtkSpinner or the
4242 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4243 * determines whether there is activity going on.
4248 gtk_render_activity (GtkStyleContext *context,
4255 GtkThemingEngineClass *engine_class;
4256 GtkThemingEngine *engine;
4258 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4259 g_return_if_fail (cr != NULL);
4261 if (width <= 0 || height <= 0)
4264 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4265 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4269 _gtk_theming_engine_set_context (engine, context);
4270 engine_class->render_activity (engine, cr, x, y, width, height);
4276 * gtk_render_icon_pixbuf:
4277 * @context: a #GtkStyleContext
4278 * @source: the #GtkIconSource specifying the icon to render
4279 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4280 * means render at the size of the source and don't scale.
4282 * Renders the icon specified by @source at the given @size, returning the result
4285 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4290 gtk_render_icon_pixbuf (GtkStyleContext *context,
4291 const GtkIconSource *source,
4294 GtkThemingEngineClass *engine_class;
4295 GtkThemingEngine *engine;
4297 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4298 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4299 g_return_val_if_fail (source != NULL, NULL);
4301 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4302 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4304 _gtk_theming_engine_set_context (engine, context);
4305 return engine_class->render_icon_pixbuf (engine, source, size);
4310 * @context: a #GtkStyleContext
4312 * @pixbuf: a #GdkPixbuf containing the icon to draw
4313 * @x: X position for the @pixbuf
4314 * @y: Y position for the @pixbuf
4316 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4321 gtk_render_icon (GtkStyleContext *context,
4327 GtkThemingEngineClass *engine_class;
4328 GtkThemingEngine *engine;
4330 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4331 g_return_if_fail (cr != NULL);
4333 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4334 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4338 _gtk_theming_engine_set_context (engine, context);
4339 engine_class->render_icon (engine, cr, pixbuf, x, y);
4345 draw_insertion_cursor (GtkStyleContext *context,
4350 gboolean is_primary,
4351 PangoDirection direction,
4352 gboolean draw_arrow)
4355 GdkRGBA primary_color;
4356 GdkRGBA secondary_color;
4357 gfloat cursor_aspect_ratio;
4363 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4364 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4366 /* When changing the shape or size of the cursor here,
4367 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4370 gtk_style_context_get_style (context,
4371 "cursor-aspect-ratio", &cursor_aspect_ratio,
4374 stem_width = height * cursor_aspect_ratio + 1;
4376 /* put (stem_width % 2) on the proper side of the cursor */
4377 if (direction == PANGO_DIRECTION_LTR)
4378 offset = stem_width / 2;
4380 offset = stem_width - stem_width / 2;
4382 cairo_rectangle (cr, x - offset, y, stem_width, height);
4390 arrow_width = stem_width + 1;
4392 if (direction == PANGO_DIRECTION_RTL)
4394 ax = x - offset - 1;
4395 ay = y + height - arrow_width * 2 - arrow_width + 1;
4397 cairo_move_to (cr, ax, ay + 1);
4398 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4399 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4402 else if (direction == PANGO_DIRECTION_LTR)
4404 ax = x + stem_width - offset;
4405 ay = y + height - arrow_width * 2 - arrow_width + 1;
4407 cairo_move_to (cr, ax, ay + 1);
4408 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4409 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4413 g_assert_not_reached();
4420 * gtk_render_insertion_cursor:
4421 * @context: a #GtkStyleContext
4425 * @layout: the #PangoLayout of the text
4426 * @index: the index in the #PangoLayout
4427 * @direction: the #PangoDirection of the text
4429 * Draws a text caret on @cr at the specified index of @layout.
4434 gtk_render_insertion_cursor (GtkStyleContext *context,
4438 PangoLayout *layout,
4440 PangoDirection direction)
4442 GtkStyleContextPrivate *priv;
4443 gboolean split_cursor;
4444 PangoRectangle strong_pos, weak_pos;
4445 PangoRectangle *cursor1, *cursor2;
4446 PangoDirection keymap_direction;
4447 PangoDirection direction2;
4449 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4450 g_return_if_fail (cr != NULL);
4451 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4452 g_return_if_fail (index >= 0);
4454 priv = context->priv;
4456 g_object_get (gtk_settings_get_for_screen (priv->screen),
4457 "gtk-split-cursor", &split_cursor,
4460 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4462 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4464 direction2 = PANGO_DIRECTION_NEUTRAL;
4468 cursor1 = &strong_pos;
4470 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4472 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4473 cursor2 = &weak_pos;
4478 if (keymap_direction == direction)
4479 cursor1 = &strong_pos;
4481 cursor1 = &weak_pos;
4484 draw_insertion_cursor (context,
4486 x + PANGO_PIXELS (cursor1->x),
4487 y + PANGO_PIXELS (cursor1->y),
4488 PANGO_PIXELS (cursor1->height),
4491 direction2 != PANGO_DIRECTION_NEUTRAL);
4493 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4495 draw_insertion_cursor (context,
4497 x + PANGO_PIXELS (cursor2->x),
4498 y + PANGO_PIXELS (cursor2->y),
4499 PANGO_PIXELS (cursor2->height),
4507 * gtk_draw_insertion_cursor:
4508 * @widget: a #GtkWidget
4509 * @cr: cairo context to draw to
4510 * @location: location where to draw the cursor (@location->width is ignored)
4511 * @is_primary: if the cursor should be the primary cursor color.
4512 * @direction: whether the cursor is left-to-right or
4513 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4514 * @draw_arrow: %TRUE to draw a directional arrow on the
4515 * cursor. Should be %FALSE unless the cursor is split.
4517 * Draws a text caret on @cr at @location. This is not a style function
4518 * but merely a convenience function for drawing the standard cursor shape.
4521 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4524 gtk_draw_insertion_cursor (GtkWidget *widget,
4526 const GdkRectangle *location,
4527 gboolean is_primary,
4528 GtkTextDirection direction,
4529 gboolean draw_arrow)
4531 GtkStyleContext *context;
4533 g_return_if_fail (GTK_IS_WIDGET (widget));
4534 g_return_if_fail (cr != NULL);
4535 g_return_if_fail (location != NULL);
4536 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4538 context = gtk_widget_get_style_context (widget);
4540 draw_insertion_cursor (context, cr,
4541 location->x, location->y, location->height,
4543 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4547 static AtkAttributeSet *
4548 add_attribute (AtkAttributeSet *attributes,
4549 AtkTextAttribute attr,
4554 at = g_new (AtkAttribute, 1);
4555 at->name = g_strdup (atk_text_attribute_get_name (attr));
4556 at->value = g_strdup (value);
4558 return g_slist_prepend (attributes, at);
4562 * _gtk_style_context_get_attributes:
4563 * @attributes: a #AtkAttributeSet to add attributes to
4564 * @context: the #GtkStyleContext to get attributes from
4565 * @flags: the state to use with @context
4567 * Adds the foreground and background color from @context to
4568 * @attributes, after translating them to ATK attributes.
4570 * This is a convenience function that can be used in
4571 * implementing the #AtkText interface in widgets.
4573 * Returns: the modified #AtkAttributeSet
4576 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4577 GtkStyleContext *context,
4578 GtkStateFlags flags)
4583 gtk_style_context_get_background_color (context, flags, &color);
4584 value = g_strdup_printf ("%u,%u,%u",
4585 (guint) ceil (color.red * 65536 - color.red),
4586 (guint) ceil (color.green * 65536 - color.green),
4587 (guint) ceil (color.blue * 65536 - color.blue));
4588 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4591 gtk_style_context_get_color (context, flags, &color);
4592 value = g_strdup_printf ("%u,%u,%u",
4593 (guint) ceil (color.red * 65536 - color.red),
4594 (guint) ceil (color.green * 65536 - color.green),
4595 (guint) ceil (color.blue * 65536 - color.blue));
4596 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);