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,
407 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
410 gtk_style_context_real_changed (GtkStyleContext *context)
412 GtkStyleContextPrivate *priv = context->priv;
415 _gtk_widget_style_context_invalidated (priv->widget);
419 gtk_style_context_class_init (GtkStyleContextClass *klass)
421 GObjectClass *object_class = G_OBJECT_CLASS (klass);
423 object_class->finalize = gtk_style_context_finalize;
424 object_class->set_property = gtk_style_context_impl_set_property;
425 object_class->get_property = gtk_style_context_impl_get_property;
427 klass->changed = gtk_style_context_real_changed;
430 g_signal_new (I_("changed"),
431 G_TYPE_FROM_CLASS (object_class),
433 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
435 g_cclosure_marshal_VOID__VOID,
438 g_object_class_install_property (object_class,
440 g_param_spec_object ("screen",
442 P_("The associated GdkScreen"),
444 GTK_PARAM_READWRITE));
445 g_object_class_install_property (object_class,
447 g_param_spec_enum ("direction",
449 P_("Text direction"),
450 GTK_TYPE_TEXT_DIRECTION,
452 GTK_PARAM_READWRITE));
454 * GtkStyleContext:parent:
456 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
461 g_object_class_install_property (object_class,
463 g_param_spec_object ("parent",
465 P_("The parent style context"),
466 GTK_TYPE_STYLE_CONTEXT,
467 GTK_PARAM_READWRITE));
469 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
473 style_data_new (void)
477 data = g_slice_new0 (StyleData);
484 clear_property_cache (StyleData *data)
488 if (!data->property_cache)
491 for (i = 0; i < data->property_cache->len; i++)
493 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
495 g_param_spec_unref (node->pspec);
496 g_value_unset (&node->value);
499 g_array_free (data->property_cache, TRUE);
500 data->property_cache = NULL;
504 style_data_ref (StyleData *style_data)
506 style_data->ref_count++;
512 style_data_unref (StyleData *data)
516 if (data->ref_count > 0)
519 g_object_unref (data->store);
520 clear_property_cache (data);
522 g_slice_free (StyleData, data);
526 style_data_is_animating (StyleData *style_data)
528 return !_gtk_css_computed_values_is_static (style_data->store);
531 static GtkStyleInfo *
532 style_info_new (void)
536 info = g_slice_new0 (GtkStyleInfo);
537 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
538 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
544 style_info_set_data (GtkStyleInfo *info,
547 if (info->data == data)
551 style_data_ref (data);
554 style_data_unref (info->data);
560 style_info_free (GtkStyleInfo *info)
562 style_info_set_data (info, NULL);
563 g_array_free (info->style_classes, TRUE);
564 g_array_free (info->regions, TRUE);
565 g_slice_free (GtkStyleInfo, info);
568 static GtkStyleInfo *
569 style_info_pop (GtkStyleInfo *info)
571 GtkStyleInfo *next = info->next;
573 style_info_free (info);
578 static GtkStyleInfo *
579 style_info_copy (GtkStyleInfo *info)
583 copy = style_info_new ();
584 g_array_insert_vals (copy->style_classes, 0,
585 info->style_classes->data,
586 info->style_classes->len);
588 g_array_insert_vals (copy->regions, 0,
593 copy->junction_sides = info->junction_sides;
594 copy->state_flags = info->state_flags;
595 style_info_set_data (copy, info->data);
601 style_info_hash (gconstpointer elem)
603 const GtkStyleInfo *info;
608 for (i = 0; i < info->style_classes->len; i++)
610 hash += g_array_index (info->style_classes, GQuark, i);
614 for (i = 0; i < info->regions->len; i++)
618 region = &g_array_index (info->regions, GtkRegion, i);
619 hash += region->class_quark;
620 hash += region->flags;
624 return hash ^ info->state_flags;
628 style_info_equal (gconstpointer elem1,
631 const GtkStyleInfo *info1, *info2;
636 if (info1->state_flags != info2->state_flags)
639 if (info1->junction_sides != info2->junction_sides)
642 if (info1->style_classes->len != info2->style_classes->len)
645 if (memcmp (info1->style_classes->data,
646 info2->style_classes->data,
647 info1->style_classes->len * sizeof (GQuark)) != 0)
650 if (info1->regions->len != info2->regions->len)
653 if (memcmp (info1->regions->data,
654 info2->regions->data,
655 info1->regions->len * sizeof (GtkRegion)) != 0)
662 gtk_style_context_cascade_changed (GtkStyleCascade *cascade,
663 GtkStyleContext *context)
665 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_SOURCE);
669 gtk_style_context_set_cascade (GtkStyleContext *context,
670 GtkStyleCascade *cascade)
672 GtkStyleContextPrivate *priv;
674 priv = context->priv;
676 if (priv->cascade == cascade)
681 g_object_ref (cascade);
682 g_signal_connect (cascade,
683 "-gtk-private-changed",
684 G_CALLBACK (gtk_style_context_cascade_changed),
690 g_signal_handlers_disconnect_by_func (priv->cascade,
691 gtk_style_context_cascade_changed,
693 g_object_unref (priv->cascade);
696 priv->cascade = cascade;
699 gtk_style_context_cascade_changed (cascade, context);
702 GtkStyleProviderPrivate *
703 _gtk_style_context_get_style_provider (GtkStyleContext *context)
705 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
707 return GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade);
711 gtk_style_context_init (GtkStyleContext *style_context)
713 GtkStyleContextPrivate *priv;
715 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
716 GTK_TYPE_STYLE_CONTEXT,
717 GtkStyleContextPrivate);
719 priv->style_data = g_hash_table_new_full (style_info_hash,
721 (GDestroyNotify) style_info_free,
722 (GDestroyNotify) style_data_unref);
724 priv->direction = GTK_TEXT_DIR_LTR;
726 priv->screen = gdk_screen_get_default ();
727 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
729 /* Create default info store */
730 priv->info = style_info_new ();
732 gtk_style_context_set_cascade (style_context,
733 _gtk_style_cascade_get_for_screen (priv->screen));
737 gtk_style_context_do_animations (gpointer unused)
739 GtkStyleContext *context;
741 for (context = _running_animations;
743 context = context->priv->animation_list_next)
745 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANIMATE);
752 gtk_style_context_is_animating (GtkStyleContext *context)
754 GtkStyleContextPrivate *priv = context->priv;
756 return priv->animation_list_prev != NULL
757 || _running_animations == context;
761 gtk_style_context_stop_animating (GtkStyleContext *context)
763 GtkStyleContextPrivate *priv = context->priv;
765 if (!gtk_style_context_is_animating (context))
768 if (priv->animation_list_prev == NULL)
770 _running_animations = priv->animation_list_next;
772 if (_running_animations == NULL)
774 /* we were the last animation */
775 g_source_remove (_running_animations_timer_id);
776 _running_animations_timer_id = 0;
780 priv->animation_list_prev->priv->animation_list_next = priv->animation_list_next;
782 if (priv->animation_list_next)
783 priv->animation_list_next->priv->animation_list_prev = priv->animation_list_prev;
785 priv->animation_list_next = NULL;
786 priv->animation_list_prev = NULL;
790 gtk_style_context_start_animating (GtkStyleContext *context)
792 GtkStyleContextPrivate *priv = context->priv;
794 if (gtk_style_context_is_animating (context))
797 if (_running_animations == NULL)
799 _running_animations_timer_id = gdk_threads_add_timeout (25,
800 gtk_style_context_do_animations,
802 _running_animations = context;
806 priv->animation_list_next = _running_animations;
807 _running_animations->priv->animation_list_prev = context;
808 _running_animations = context;
813 gtk_style_context_finalize (GObject *object)
815 GtkStyleContextPrivate *priv;
816 GtkStyleContext *style_context;
818 style_context = GTK_STYLE_CONTEXT (object);
819 priv = style_context->priv;
821 _gtk_style_context_stop_animations (style_context);
823 /* children hold a reference to us */
824 g_assert (priv->children == NULL);
826 gtk_style_context_set_parent (style_context, NULL);
828 gtk_style_context_set_cascade (style_context, NULL);
830 if (priv->widget_path)
831 gtk_widget_path_free (priv->widget_path);
833 g_hash_table_destroy (priv->style_data);
836 priv->info = style_info_pop (priv->info);
838 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
842 gtk_style_context_impl_set_property (GObject *object,
847 GtkStyleContext *style_context;
849 style_context = GTK_STYLE_CONTEXT (object);
854 gtk_style_context_set_screen (style_context,
855 g_value_get_object (value));
858 gtk_style_context_set_direction (style_context,
859 g_value_get_enum (value));
862 gtk_style_context_set_parent (style_context,
863 g_value_get_object (value));
866 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
872 gtk_style_context_impl_get_property (GObject *object,
877 GtkStyleContext *style_context;
878 GtkStyleContextPrivate *priv;
880 style_context = GTK_STYLE_CONTEXT (object);
881 priv = style_context->priv;
886 g_value_set_object (value, priv->screen);
889 g_value_set_enum (value, priv->direction);
892 g_value_set_object (value, priv->parent);
895 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
900 static GtkWidgetPath *
901 create_query_path (GtkStyleContext *context)
903 GtkStyleContextPrivate *priv;
908 priv = context->priv;
909 path = priv->widget ? _gtk_widget_create_path (priv->widget) : gtk_widget_path_copy (priv->widget_path);
910 pos = gtk_widget_path_length (path) - 1;
914 /* Set widget regions */
915 for (i = 0; i < info->regions->len; i++)
919 region = &g_array_index (info->regions, GtkRegion, i);
920 gtk_widget_path_iter_add_region (path, pos,
921 g_quark_to_string (region->class_quark),
925 /* Set widget classes */
926 for (i = 0; i < info->style_classes->len; i++)
930 quark = g_array_index (info->style_classes, GQuark, i);
931 gtk_widget_path_iter_add_class (path, pos,
932 g_quark_to_string (quark));
939 build_properties (GtkStyleContext *context,
940 GtkCssComputedValues *values,
942 const GtkBitmask *relevant_changes)
944 GtkStyleContextPrivate *priv;
945 GtkCssMatcher matcher;
947 GtkCssLookup *lookup;
949 priv = context->priv;
951 path = create_query_path (context);
952 lookup = _gtk_css_lookup_new (relevant_changes);
954 if (_gtk_css_matcher_init (&matcher, path, state))
955 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
959 _gtk_css_lookup_resolve (lookup, context, values);
961 _gtk_css_lookup_free (lookup);
962 gtk_widget_path_free (path);
966 style_data_lookup (GtkStyleContext *context)
968 GtkStyleContextPrivate *priv;
972 priv = context->priv;
975 /* Current data in use is cached, just return it */
979 g_assert (priv->widget != NULL || priv->widget_path != NULL);
981 data = g_hash_table_lookup (priv->style_data, info);
984 style_info_set_data (info, data);
988 data = style_data_new ();
989 data->store = _gtk_css_computed_values_new ();
990 style_info_set_data (info, data);
991 g_hash_table_insert (priv->style_data,
992 style_info_copy (info),
995 build_properties (context, data->store, info->state_flags, NULL);
1001 style_data_lookup_for_state (GtkStyleContext *context,
1002 GtkStateFlags state)
1006 if (context->priv->info->state_flags == state)
1007 return style_data_lookup (context);
1009 gtk_style_context_save (context);
1010 gtk_style_context_set_state (context, state);
1011 data = style_data_lookup (context);
1012 gtk_style_context_restore (context);
1018 gtk_style_context_set_invalid (GtkStyleContext *context,
1021 GtkStyleContextPrivate *priv;
1023 priv = context->priv;
1025 if (priv->invalid == invalid)
1028 priv->invalid = invalid;
1033 gtk_style_context_set_invalid (priv->parent, TRUE);
1034 else if (GTK_IS_RESIZE_CONTAINER (priv->widget))
1035 _gtk_container_queue_restyle (GTK_CONTAINER (priv->widget));
1039 /* returns TRUE if someone called gtk_style_context_save() but hasn't
1040 * called gtk_style_context_restore() yet.
1041 * In those situations we don't invalidate the context when somebody
1042 * changes state/regions/classes.
1045 gtk_style_context_is_saved (GtkStyleContext *context)
1047 return context->priv->info->next != NULL;
1051 gtk_style_context_queue_invalidate_internal (GtkStyleContext *context,
1052 GtkCssChange change)
1054 GtkStyleContextPrivate *priv = context->priv;
1055 GtkStyleInfo *info = priv->info;
1057 if (gtk_style_context_is_saved (context))
1059 style_info_set_data (info, NULL);
1063 _gtk_style_context_queue_invalidate (context, change);
1064 /* XXX: We need to invalidate siblings here somehow */
1069 * gtk_style_context_new:
1071 * Creates a standalone #GtkStyleContext, this style context
1072 * won't be attached to any widget, so you may want
1073 * to call gtk_style_context_set_path() yourself.
1076 * This function is only useful when using the theming layer
1077 * separated from GTK+, if you are using #GtkStyleContext to
1078 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1079 * in order to get a style context ready to theme the widget.
1082 * Returns: A newly created #GtkStyleContext.
1085 gtk_style_context_new (void)
1087 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1091 _gtk_style_context_set_widget (GtkStyleContext *context,
1094 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1095 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1097 context->priv->widget = widget;
1099 _gtk_style_context_stop_animations (context);
1101 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_SELF);
1105 * gtk_style_context_add_provider:
1106 * @context: a #GtkStyleContext
1107 * @provider: a #GtkStyleProvider
1108 * @priority: the priority of the style provider. The lower
1109 * it is, the earlier it will be used in the style
1110 * construction. Typically this will be in the range
1111 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1112 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1114 * Adds a style provider to @context, to be used in style construction.
1115 * Note that a style provider added by this function only affects
1116 * the style of the widget to which @context belongs. If you want
1117 * to affect the style of all widgets, use
1118 * gtk_style_context_add_provider_for_screen().
1120 * <note><para>If both priorities are the same, A #GtkStyleProvider
1121 * added through this function takes precedence over another added
1122 * through gtk_style_context_add_provider_for_screen().</para></note>
1127 gtk_style_context_add_provider (GtkStyleContext *context,
1128 GtkStyleProvider *provider,
1131 GtkStyleContextPrivate *priv;
1133 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1134 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1136 priv = context->priv;
1138 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1140 GtkStyleCascade *new_cascade;
1142 new_cascade = _gtk_style_cascade_new ();
1143 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1144 _gtk_style_cascade_add_provider (new_cascade, provider, priority);
1145 gtk_style_context_set_cascade (context, new_cascade);
1146 g_object_unref (new_cascade);
1150 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1155 * gtk_style_context_remove_provider:
1156 * @context: a #GtkStyleContext
1157 * @provider: a #GtkStyleProvider
1159 * Removes @provider from the style providers list in @context.
1164 gtk_style_context_remove_provider (GtkStyleContext *context,
1165 GtkStyleProvider *provider)
1167 GtkStyleContextPrivate *priv;
1169 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1170 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1172 priv = context->priv;
1174 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1177 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1181 * gtk_style_context_reset_widgets:
1182 * @screen: a #GdkScreen
1184 * This function recomputes the styles for all widgets under a particular
1185 * #GdkScreen. This is useful when some global parameter has changed that
1186 * affects the appearance of all widgets, because when a widget gets a new
1187 * style, it will both redraw and recompute any cached information about
1188 * its appearance. As an example, it is used when the color scheme changes
1189 * in the related #GtkSettings object.
1194 gtk_style_context_reset_widgets (GdkScreen *screen)
1196 GList *list, *toplevels;
1198 _gtk_icon_set_invalidate_caches ();
1200 toplevels = gtk_window_list_toplevels ();
1201 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1203 for (list = toplevels; list; list = list->next)
1205 if (gtk_widget_get_screen (list->data) == screen)
1206 gtk_widget_reset_style (list->data);
1208 g_object_unref (list->data);
1211 g_list_free (toplevels);
1215 * gtk_style_context_add_provider_for_screen:
1216 * @screen: a #GdkScreen
1217 * @provider: a #GtkStyleProvider
1218 * @priority: the priority of the style provider. The lower
1219 * it is, the earlier it will be used in the style
1220 * construction. Typically this will be in the range
1221 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1222 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1224 * Adds a global style provider to @screen, which will be used
1225 * in style construction for all #GtkStyleContexts under @screen.
1227 * GTK+ uses this to make styling information from #GtkSettings
1230 * <note><para>If both priorities are the same, A #GtkStyleProvider
1231 * added through gtk_style_context_add_provider() takes precedence
1232 * over another added through this function.</para></note>
1237 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1238 GtkStyleProvider *provider,
1241 GtkStyleCascade *cascade;
1243 g_return_if_fail (GDK_IS_SCREEN (screen));
1244 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1245 g_return_if_fail (!GTK_IS_SETTINGS (provider) || _gtk_settings_get_screen (GTK_SETTINGS (provider)) == screen);
1247 cascade = _gtk_style_cascade_get_for_screen (screen);
1248 _gtk_style_cascade_add_provider (cascade, provider, priority);
1252 * gtk_style_context_remove_provider_for_screen:
1253 * @screen: a #GdkScreen
1254 * @provider: a #GtkStyleProvider
1256 * Removes @provider from the global style providers list in @screen.
1261 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1262 GtkStyleProvider *provider)
1264 GtkStyleCascade *cascade;
1266 g_return_if_fail (GDK_IS_SCREEN (screen));
1267 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1268 g_return_if_fail (!GTK_IS_SETTINGS (provider));
1270 cascade = _gtk_style_cascade_get_for_screen (screen);
1271 _gtk_style_cascade_remove_provider (cascade, provider);
1275 * gtk_style_context_get_section:
1276 * @context: a #GtkStyleContext
1277 * @property: style property name
1279 * Queries the location in the CSS where @property was defined for the
1280 * current @context. Note that the state to be queried is taken from
1281 * gtk_style_context_get_state().
1283 * If the location is not available, %NULL will be returned. The
1284 * location might not be available for various reasons, such as the
1285 * property being overridden, @property not naming a supported CSS
1286 * property or tracking of definitions being disabled for performance
1289 * Shorthand CSS properties cannot be queried for a location and will
1290 * always return %NULL.
1292 * Returns: %NULL or the section where value was defined
1295 gtk_style_context_get_section (GtkStyleContext *context,
1296 const gchar *property)
1298 GtkStyleContextPrivate *priv;
1299 GtkStyleProperty *prop;
1302 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1303 g_return_val_if_fail (property != NULL, NULL);
1305 priv = context->priv;
1306 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1308 prop = _gtk_style_property_lookup (property);
1309 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1312 data = style_data_lookup (context);
1313 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1316 static GtkCssValue *
1317 gtk_style_context_query_func (guint id,
1320 return _gtk_css_computed_values_get_value (values, id);
1324 * gtk_style_context_get_property:
1325 * @context: a #GtkStyleContext
1326 * @property: style property name
1327 * @state: state to retrieve the property value for
1328 * @value: (out) (transfer full): return location for the style property value
1330 * Gets a style property from @context for the given state.
1332 * When @value is no longer needed, g_value_unset() must be called
1333 * to free any allocated memory.
1338 gtk_style_context_get_property (GtkStyleContext *context,
1339 const gchar *property,
1340 GtkStateFlags state,
1343 GtkStyleContextPrivate *priv;
1344 GtkStyleProperty *prop;
1347 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1348 g_return_if_fail (property != NULL);
1349 g_return_if_fail (value != NULL);
1351 priv = context->priv;
1352 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1354 prop = _gtk_style_property_lookup (property);
1357 g_warning ("Style property \"%s\" is not registered", property);
1360 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1362 g_warning ("Style property \"%s\" is not gettable", property);
1366 data = style_data_lookup_for_state (context, state);
1367 _gtk_style_property_query (prop, value, gtk_style_context_query_func, data->store);
1371 * gtk_style_context_get_valist:
1372 * @context: a #GtkStyleContext
1373 * @state: state to retrieve the property values for
1374 * @args: va_list of property name/return location pairs, followed by %NULL
1376 * Retrieves several style property values from @context for a given state.
1381 gtk_style_context_get_valist (GtkStyleContext *context,
1382 GtkStateFlags state,
1385 const gchar *property_name;
1387 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1389 property_name = va_arg (args, const gchar *);
1391 while (property_name)
1393 gchar *error = NULL;
1394 GValue value = G_VALUE_INIT;
1396 gtk_style_context_get_property (context,
1401 G_VALUE_LCOPY (&value, args, 0, &error);
1402 g_value_unset (&value);
1406 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1411 property_name = va_arg (args, const gchar *);
1416 * gtk_style_context_get:
1417 * @context: a #GtkStyleContext
1418 * @state: state to retrieve the property values for
1419 * @...: property name /return value pairs, followed by %NULL
1421 * Retrieves several style property values from @context for a
1427 gtk_style_context_get (GtkStyleContext *context,
1428 GtkStateFlags state,
1433 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1435 va_start (args, state);
1436 gtk_style_context_get_valist (context, state, args);
1441 * gtk_style_context_set_state:
1442 * @context: a #GtkStyleContext
1443 * @flags: state to represent
1445 * Sets the state to be used when rendering with any
1446 * of the gtk_render_*() functions.
1451 gtk_style_context_set_state (GtkStyleContext *context,
1452 GtkStateFlags flags)
1454 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1456 context->priv->info->state_flags = flags;
1458 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_STATE);
1462 * gtk_style_context_get_state:
1463 * @context: a #GtkStyleContext
1465 * Returns the state used when rendering.
1467 * Returns: the state flags
1472 gtk_style_context_get_state (GtkStyleContext *context)
1474 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1476 return context->priv->info->state_flags;
1480 * gtk_style_context_state_is_running:
1481 * @context: a #GtkStyleContext
1482 * @state: a widget state
1483 * @progress: (out): return location for the transition progress
1485 * Returns %TRUE if there is a transition animation running for the
1486 * current region (see gtk_style_context_push_animatable_region()).
1488 * If @progress is not %NULL, the animation progress will be returned
1489 * there, 0.0 means the state is closest to being unset, while 1.0 means
1490 * it's closest to being set. This means transition animation will
1491 * run from 0 to 1 when @state is being set and from 1 to 0 when
1494 * Returns: %TRUE if there is a running transition animation for @state.
1498 * Deprecated: 3.6: This function always returns %FALSE
1501 gtk_style_context_state_is_running (GtkStyleContext *context,
1505 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1511 * gtk_style_context_set_path:
1512 * @context: a #GtkStyleContext
1513 * @path: a #GtkWidgetPath
1515 * Sets the #GtkWidgetPath used for style matching. As a
1516 * consequence, the style will be regenerated to match
1517 * the new given path.
1519 * If you are using a #GtkStyleContext returned from
1520 * gtk_widget_get_style_context(), you do not need to call
1526 gtk_style_context_set_path (GtkStyleContext *context,
1527 GtkWidgetPath *path)
1529 GtkStyleContextPrivate *priv;
1531 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1532 g_return_if_fail (path != NULL);
1534 priv = context->priv;
1535 g_return_if_fail (priv->widget == NULL);
1537 if (priv->widget_path)
1539 gtk_widget_path_free (priv->widget_path);
1540 priv->widget_path = NULL;
1544 priv->widget_path = gtk_widget_path_copy (path);
1546 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY);
1550 * gtk_style_context_get_path:
1551 * @context: a #GtkStyleContext
1553 * Returns the widget path used for style matching.
1555 * Returns: (transfer none): A #GtkWidgetPath
1559 const GtkWidgetPath *
1560 gtk_style_context_get_path (GtkStyleContext *context)
1562 GtkStyleContextPrivate *priv;
1564 priv = context->priv;
1566 return gtk_widget_get_path (priv->widget);
1568 return priv->widget_path;
1572 * gtk_style_context_set_parent:
1573 * @context: a #GtkStyleContext
1574 * @parent: (allow-none): the new parent or %NULL
1576 * Sets the parent style context for @context. The parent style
1577 * context is used to implement
1578 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1581 * If you are using a #GtkStyleContext returned from
1582 * gtk_widget_get_style_context(), the parent will be set for you.
1587 gtk_style_context_set_parent (GtkStyleContext *context,
1588 GtkStyleContext *parent)
1590 GtkStyleContextPrivate *priv;
1592 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1593 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1595 priv = context->priv;
1597 if (priv->parent == parent)
1602 parent->priv->children = g_slist_prepend (parent->priv->children, context);
1603 g_object_ref (parent);
1605 gtk_style_context_set_invalid (parent, TRUE);
1610 priv->parent->priv->children = g_slist_remove (priv->parent->priv->children, context);
1611 g_object_unref (priv->parent);
1614 priv->parent = parent;
1616 g_object_notify (G_OBJECT (context), "parent");
1617 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
1621 * gtk_style_context_get_parent:
1622 * @context: a #GtkStyleContext
1624 * Gets the parent context set via gtk_style_context_set_parent().
1625 * See that function for details.
1627 * Returns: (transfer none): the parent context or %NULL
1632 gtk_style_context_get_parent (GtkStyleContext *context)
1634 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1636 return context->priv->parent;
1640 * gtk_style_context_save:
1641 * @context: a #GtkStyleContext
1643 * Saves the @context state, so all modifications done through
1644 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1645 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1646 * or gtk_style_context_set_junction_sides() can be reverted in one
1647 * go through gtk_style_context_restore().
1652 gtk_style_context_save (GtkStyleContext *context)
1654 GtkStyleContextPrivate *priv;
1656 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1658 priv = context->priv;
1660 priv->info = style_info_copy (priv->info);
1661 /* Need to unset animations here because we can not know what style
1662 * class potential transitions came from once we save().
1664 if (priv->info->data && style_data_is_animating (priv->info->data))
1665 style_info_set_data (priv->info, NULL);
1669 * gtk_style_context_restore:
1670 * @context: a #GtkStyleContext
1672 * Restores @context state to a previous stage.
1673 * See gtk_style_context_save().
1678 gtk_style_context_restore (GtkStyleContext *context)
1680 GtkStyleContextPrivate *priv;
1682 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1684 priv = context->priv;
1686 priv->info = style_info_pop (priv->info);
1690 g_warning ("Unpaired gtk_style_context_restore() call");
1692 /* Create default region */
1693 priv->info = style_info_new ();
1698 style_class_find (GArray *array,
1703 gboolean found = FALSE;
1709 if (!array || array->len == 0)
1713 max = array->len - 1;
1719 mid = (min + max) / 2;
1720 item = g_array_index (array, GQuark, mid);
1722 if (class_quark == item)
1727 else if (class_quark > item)
1728 min = pos = mid + 1;
1735 while (!found && min <= max);
1744 region_find (GArray *array,
1749 gboolean found = FALSE;
1755 if (!array || array->len == 0)
1759 max = array->len - 1;
1765 mid = (min + max) / 2;
1766 region = &g_array_index (array, GtkRegion, mid);
1768 if (region->class_quark == class_quark)
1773 else if (region->class_quark > class_quark)
1774 min = pos = mid + 1;
1781 while (!found && min <= max);
1790 * gtk_style_context_add_class:
1791 * @context: a #GtkStyleContext
1792 * @class_name: class name to use in styling
1794 * Adds a style class to @context, so posterior calls to
1795 * gtk_style_context_get() or any of the gtk_render_*()
1796 * functions will make use of this new class for styling.
1798 * In the CSS file format, a #GtkEntry defining an "entry"
1799 * class, would be matched by:
1802 * GtkEntry.entry { ... }
1805 * While any widget defining an "entry" class would be
1814 gtk_style_context_add_class (GtkStyleContext *context,
1815 const gchar *class_name)
1817 GtkStyleContextPrivate *priv;
1822 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1823 g_return_if_fail (class_name != NULL);
1825 priv = context->priv;
1826 class_quark = g_quark_from_string (class_name);
1830 if (!style_class_find (info->style_classes, class_quark, &position))
1832 g_array_insert_val (info->style_classes, position, class_quark);
1834 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1839 * gtk_style_context_remove_class:
1840 * @context: a #GtkStyleContext
1841 * @class_name: class name to remove
1843 * Removes @class_name from @context.
1848 gtk_style_context_remove_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 class_quark = g_quark_try_string (class_name);
1864 priv = context->priv;
1868 if (style_class_find (info->style_classes, class_quark, &position))
1870 g_array_remove_index (info->style_classes, position);
1872 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1877 * gtk_style_context_has_class:
1878 * @context: a #GtkStyleContext
1879 * @class_name: a class name
1881 * Returns %TRUE if @context currently has defined the
1884 * Returns: %TRUE if @context has @class_name defined
1889 gtk_style_context_has_class (GtkStyleContext *context,
1890 const gchar *class_name)
1892 GtkStyleContextPrivate *priv;
1896 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1897 g_return_val_if_fail (class_name != NULL, FALSE);
1899 class_quark = g_quark_try_string (class_name);
1904 priv = context->priv;
1908 if (style_class_find (info->style_classes, class_quark, NULL))
1915 * gtk_style_context_list_classes:
1916 * @context: a #GtkStyleContext
1918 * Returns the list of classes currently defined in @context.
1920 * Returns: (transfer container) (element-type utf8): a #GList of
1921 * strings with the currently defined classes. The contents
1922 * of the list are owned by GTK+, but you must free the list
1923 * itself with g_list_free() when you are done with it.
1928 gtk_style_context_list_classes (GtkStyleContext *context)
1930 GtkStyleContextPrivate *priv;
1932 GList *classes = NULL;
1935 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1937 priv = context->priv;
1941 for (i = 0; i < info->style_classes->len; i++)
1945 quark = g_array_index (info->style_classes, GQuark, i);
1946 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1953 * gtk_style_context_list_regions:
1954 * @context: a #GtkStyleContext
1956 * Returns the list of regions currently defined in @context.
1958 * Returns: (transfer container) (element-type utf8): a #GList of
1959 * strings with the currently defined regions. The contents
1960 * of the list are owned by GTK+, but you must free the list
1961 * itself with g_list_free() when you are done with it.
1966 gtk_style_context_list_regions (GtkStyleContext *context)
1968 GtkStyleContextPrivate *priv;
1970 GList *classes = NULL;
1973 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1975 priv = context->priv;
1979 for (i = 0; i < info->regions->len; i++)
1982 const gchar *class_name;
1984 region = &g_array_index (info->regions, GtkRegion, i);
1986 class_name = g_quark_to_string (region->class_quark);
1987 classes = g_list_prepend (classes, (gchar *) class_name);
1994 _gtk_style_context_check_region_name (const gchar *str)
1996 g_return_val_if_fail (str != NULL, FALSE);
1998 if (!g_ascii_islower (str[0]))
2004 !g_ascii_islower (*str))
2014 * gtk_style_context_add_region:
2015 * @context: a #GtkStyleContext
2016 * @region_name: region name to use in styling
2017 * @flags: flags that apply to the region
2019 * Adds a region to @context, so posterior calls to
2020 * gtk_style_context_get() or any of the gtk_render_*()
2021 * functions will make use of this new region for styling.
2023 * In the CSS file format, a #GtkTreeView defining a "row"
2024 * region, would be matched by:
2027 * GtkTreeView row { ... }
2030 * Pseudo-classes are used for matching @flags, so the two
2033 * GtkTreeView row:nth-child(even) { ... }
2034 * GtkTreeView row:nth-child(odd) { ... }
2037 * would apply to even and odd rows, respectively.
2039 * <note><para>Region names must only contain lowercase letters
2040 * and '-', starting always with a lowercase letter.</para></note>
2045 gtk_style_context_add_region (GtkStyleContext *context,
2046 const gchar *region_name,
2047 GtkRegionFlags flags)
2049 GtkStyleContextPrivate *priv;
2051 GQuark region_quark;
2054 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2055 g_return_if_fail (region_name != NULL);
2056 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2058 priv = context->priv;
2059 region_quark = g_quark_from_string (region_name);
2063 if (!region_find (info->regions, region_quark, &position))
2067 region.class_quark = region_quark;
2068 region.flags = flags;
2070 g_array_insert_val (info->regions, position, region);
2072 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2077 * gtk_style_context_remove_region:
2078 * @context: a #GtkStyleContext
2079 * @region_name: region name to unset
2081 * Removes a region from @context.
2086 gtk_style_context_remove_region (GtkStyleContext *context,
2087 const gchar *region_name)
2089 GtkStyleContextPrivate *priv;
2091 GQuark region_quark;
2094 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2095 g_return_if_fail (region_name != NULL);
2097 region_quark = g_quark_try_string (region_name);
2102 priv = context->priv;
2106 if (region_find (info->regions, region_quark, &position))
2108 g_array_remove_index (info->regions, position);
2110 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2115 * gtk_style_context_has_region:
2116 * @context: a #GtkStyleContext
2117 * @region_name: a region name
2118 * @flags_return: (out) (allow-none): return location for region flags
2120 * Returns %TRUE if @context has the region defined.
2121 * If @flags_return is not %NULL, it is set to the flags
2122 * affecting the region.
2124 * Returns: %TRUE if region is defined
2129 gtk_style_context_has_region (GtkStyleContext *context,
2130 const gchar *region_name,
2131 GtkRegionFlags *flags_return)
2133 GtkStyleContextPrivate *priv;
2135 GQuark region_quark;
2138 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2139 g_return_val_if_fail (region_name != NULL, FALSE);
2144 region_quark = g_quark_try_string (region_name);
2149 priv = context->priv;
2153 if (region_find (info->regions, region_quark, &position))
2159 region = &g_array_index (info->regions, GtkRegion, position);
2160 *flags_return = region->flags;
2169 style_property_values_cmp (gconstpointer bsearch_node1,
2170 gconstpointer bsearch_node2)
2172 const PropertyValue *val1 = bsearch_node1;
2173 const PropertyValue *val2 = bsearch_node2;
2175 if (val1->widget_type != val2->widget_type)
2176 return val1->widget_type < val2->widget_type ? -1 : 1;
2178 if (val1->pspec != val2->pspec)
2179 return val1->pspec < val2->pspec ? -1 : 1;
2181 if (val1->state != val2->state)
2182 return val1->state < val2->state ? -1 : 1;
2188 _gtk_style_context_peek_property (GtkStyleContext *context,
2191 StyleData *data = style_data_lookup (context);
2193 return _gtk_css_computed_values_get_value (data->store, property_id);
2197 _gtk_style_context_get_number (GtkStyleContext *context,
2199 double one_hundred_percent)
2203 value = _gtk_style_context_peek_property (context, property_id);
2204 return _gtk_css_number_value_get (value, one_hundred_percent);
2208 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2210 GtkStateFlags state,
2213 GtkStyleContextPrivate *priv;
2214 PropertyValue *pcache, key = { 0 };
2218 priv = context->priv;
2220 data = style_data_lookup_for_state (context, state);
2222 key.widget_type = widget_type;
2226 /* need value cache array */
2227 if (!data->property_cache)
2228 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2231 pcache = bsearch (&key,
2232 data->property_cache->data, data->property_cache->len,
2233 sizeof (PropertyValue), style_property_values_cmp);
2235 return &pcache->value;
2239 while (i < data->property_cache->len &&
2240 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2243 g_array_insert_val (data->property_cache, i, key);
2244 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2246 /* cache miss, initialize value type, then set contents */
2247 g_param_spec_ref (pcache->pspec);
2248 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2250 if (priv->widget || priv->widget_path)
2252 GtkWidgetPath *widget_path = priv->widget ? _gtk_widget_create_path (priv->widget) : priv->widget_path;
2254 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2256 state, pspec, &pcache->value))
2258 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2259 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2261 GtkSymbolicColor *color;
2264 color = g_value_dup_boxed (&pcache->value);
2266 g_value_unset (&pcache->value);
2268 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2269 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2271 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2273 if (_gtk_style_context_resolve_color (context, color, &rgba, NULL))
2275 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2276 g_value_set_boxed (&pcache->value, &rgba);
2281 rgb.red = rgba.red * 65535. + 0.5;
2282 rgb.green = rgba.green * 65535. + 0.5;
2283 rgb.blue = rgba.blue * 65535. + 0.5;
2285 g_value_set_boxed (&pcache->value, &rgb);
2289 g_param_value_set_default (pspec, &pcache->value);
2291 gtk_symbolic_color_unref (color);
2295 gtk_widget_path_free (widget_path);
2297 return &pcache->value;
2301 gtk_widget_path_free (widget_path);
2304 /* not supplied by any provider, revert to default */
2305 g_param_value_set_default (pspec, &pcache->value);
2307 return &pcache->value;
2311 * gtk_style_context_get_style_property:
2312 * @context: a #GtkStyleContext
2313 * @property_name: the name of the widget style property
2314 * @value: Return location for the property value
2316 * Gets the value for a widget style property.
2318 * When @value is no longer needed, g_value_unset() must be called
2319 * to free any allocated memory.
2322 gtk_style_context_get_style_property (GtkStyleContext *context,
2323 const gchar *property_name,
2326 GtkStyleContextPrivate *priv;
2327 GtkWidgetClass *widget_class;
2328 GtkStateFlags state;
2330 const GValue *peek_value;
2333 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2334 g_return_if_fail (property_name != NULL);
2335 g_return_if_fail (value != NULL);
2337 priv = context->priv;
2341 widget_type = G_OBJECT_TYPE (priv->widget);
2345 if (!priv->widget_path)
2348 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2350 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2352 g_warning ("%s: can't get style properties for non-widget class `%s'",
2354 g_type_name (widget_type));
2359 widget_class = g_type_class_ref (widget_type);
2360 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2361 g_type_class_unref (widget_class);
2365 g_warning ("%s: widget class `%s' has no style property named `%s'",
2367 g_type_name (widget_type),
2372 state = gtk_style_context_get_state (context);
2373 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2376 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2377 g_value_copy (peek_value, value);
2378 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2379 g_value_transform (peek_value, value);
2381 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2383 G_VALUE_TYPE_NAME (peek_value),
2384 G_VALUE_TYPE_NAME (value));
2388 * gtk_style_context_get_style_valist:
2389 * @context: a #GtkStyleContext
2390 * @args: va_list of property name/return location pairs, followed by %NULL
2392 * Retrieves several widget style properties from @context according to the
2398 gtk_style_context_get_style_valist (GtkStyleContext *context,
2401 GtkStyleContextPrivate *priv;
2402 const gchar *prop_name;
2403 GtkStateFlags state;
2406 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2408 prop_name = va_arg (args, const gchar *);
2409 priv = context->priv;
2413 widget_type = G_OBJECT_TYPE (priv->widget);
2417 if (!priv->widget_path)
2420 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2422 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2424 g_warning ("%s: can't get style properties for non-widget class `%s'",
2426 g_type_name (widget_type));
2431 state = gtk_style_context_get_state (context);
2435 GtkWidgetClass *widget_class;
2437 const GValue *peek_value;
2440 widget_class = g_type_class_ref (widget_type);
2441 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2442 g_type_class_unref (widget_class);
2446 g_warning ("%s: widget class `%s' has no style property named `%s'",
2448 g_type_name (widget_type),
2453 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2456 G_VALUE_LCOPY (peek_value, args, 0, &error);
2460 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2462 G_VALUE_TYPE_NAME (peek_value),
2468 prop_name = va_arg (args, const gchar *);
2473 * gtk_style_context_get_style:
2474 * @context: a #GtkStyleContext
2475 * @...: property name /return value pairs, followed by %NULL
2477 * Retrieves several widget style properties from @context according to the
2483 gtk_style_context_get_style (GtkStyleContext *context,
2488 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2490 va_start (args, context);
2491 gtk_style_context_get_style_valist (context, args);
2497 * gtk_style_context_lookup_icon_set:
2498 * @context: a #GtkStyleContext
2499 * @stock_id: an icon name
2501 * Looks up @stock_id in the icon factories associated to @context and
2502 * the default icon factory, returning an icon set if found, otherwise
2505 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2508 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2509 const gchar *stock_id)
2511 GtkStyleContextPrivate *priv;
2513 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2514 g_return_val_if_fail (stock_id != NULL, NULL);
2516 priv = context->priv;
2517 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2519 return gtk_icon_factory_lookup_default (stock_id);
2523 * gtk_style_context_set_screen:
2524 * @context: a #GtkStyleContext
2525 * @screen: a #GdkScreen
2527 * Attaches @context to the given screen.
2529 * The screen is used to add style information from 'global' style
2530 * providers, such as the screens #GtkSettings instance.
2532 * If you are using a #GtkStyleContext returned from
2533 * gtk_widget_get_style_context(), you do not need to
2534 * call this yourself.
2539 gtk_style_context_set_screen (GtkStyleContext *context,
2542 GtkStyleContextPrivate *priv;
2544 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2545 g_return_if_fail (GDK_IS_SCREEN (screen));
2547 priv = context->priv;
2548 if (priv->screen == screen)
2551 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2553 gtk_style_context_set_cascade (context, _gtk_style_cascade_get_for_screen (screen));
2557 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2560 priv->screen = screen;
2562 g_object_notify (G_OBJECT (context), "screen");
2566 * gtk_style_context_get_screen:
2567 * @context: a #GtkStyleContext
2569 * Returns the #GdkScreen to which @context is attached.
2571 * Returns: (transfer none): a #GdkScreen.
2574 gtk_style_context_get_screen (GtkStyleContext *context)
2576 GtkStyleContextPrivate *priv;
2578 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2580 priv = context->priv;
2581 return priv->screen;
2585 * gtk_style_context_set_direction:
2586 * @context: a #GtkStyleContext
2587 * @direction: the new direction.
2589 * Sets the reading direction for rendering purposes.
2591 * If you are using a #GtkStyleContext returned from
2592 * gtk_widget_get_style_context(), you do not need to
2593 * call this yourself.
2598 gtk_style_context_set_direction (GtkStyleContext *context,
2599 GtkTextDirection direction)
2601 GtkStyleContextPrivate *priv;
2603 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2605 priv = context->priv;
2606 priv->direction = direction;
2608 g_object_notify (G_OBJECT (context), "direction");
2612 * gtk_style_context_get_direction:
2613 * @context: a #GtkStyleContext
2615 * Returns the widget direction used for rendering.
2617 * Returns: the widget direction
2622 gtk_style_context_get_direction (GtkStyleContext *context)
2624 GtkStyleContextPrivate *priv;
2626 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2628 priv = context->priv;
2629 return priv->direction;
2633 * gtk_style_context_set_junction_sides:
2634 * @context: a #GtkStyleContext
2635 * @sides: sides where rendered elements are visually connected to
2638 * Sets the sides where rendered elements (mostly through
2639 * gtk_render_frame()) will visually connect with other visual elements.
2641 * This is merely a hint that may or may not be honored
2642 * by theming engines.
2644 * Container widgets are expected to set junction hints as appropriate
2645 * for their children, so it should not normally be necessary to call
2646 * this function manually.
2651 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2652 GtkJunctionSides sides)
2654 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2656 context->priv->info->junction_sides = sides;
2660 * gtk_style_context_get_junction_sides:
2661 * @context: a #GtkStyleContext
2663 * Returns the sides where rendered elements connect visually with others.
2665 * Returns: the junction sides
2670 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2672 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2674 return context->priv->info->junction_sides;
2677 static GtkSymbolicColor *
2678 gtk_style_context_color_lookup_func (gpointer contextp,
2681 GtkStyleContext *context = contextp;
2683 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2687 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2688 GtkCssValue *current,
2689 GtkCssDependencies current_deps,
2691 GtkCssDependencies *dependencies)
2693 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2694 g_return_val_if_fail (current != NULL, FALSE);
2695 g_return_val_if_fail (color != NULL, FALSE);
2697 return _gtk_symbolic_color_resolve_full ((GtkSymbolicColor *) color,
2700 gtk_style_context_color_lookup_func,
2707 _gtk_style_context_resolve_color (GtkStyleContext *context,
2708 GtkSymbolicColor *color,
2710 GtkCssDependencies *dependencies)
2714 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2715 g_return_val_if_fail (color != NULL, FALSE);
2716 g_return_val_if_fail (result != NULL, FALSE);
2718 val = _gtk_symbolic_color_resolve_full (color,
2719 _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR),
2720 GTK_CSS_DEPENDS_ON_COLOR,
2721 gtk_style_context_color_lookup_func,
2727 *result = *_gtk_css_rgba_value_get_rgba (val);
2728 _gtk_css_value_unref (val);
2733 * gtk_style_context_lookup_color:
2734 * @context: a #GtkStyleContext
2735 * @color_name: color name to lookup
2736 * @color: (out): Return location for the looked up color
2738 * Looks up and resolves a color name in the @context color map.
2740 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2743 gtk_style_context_lookup_color (GtkStyleContext *context,
2744 const gchar *color_name,
2747 GtkSymbolicColor *sym_color;
2749 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2750 g_return_val_if_fail (color_name != NULL, FALSE);
2751 g_return_val_if_fail (color != NULL, FALSE);
2753 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2754 if (sym_color == NULL)
2757 return _gtk_style_context_resolve_color (context, sym_color, color, NULL);
2761 * gtk_style_context_notify_state_change:
2762 * @context: a #GtkStyleContext
2763 * @window: a #GdkWindow
2764 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2765 * See gtk_style_context_push_animatable_region()
2766 * @state: state to trigger transition for
2767 * @state_value: %TRUE if @state is the state we are changing to,
2768 * %FALSE if we are changing away from it
2770 * Notifies a state change on @context, so if the current style makes use
2771 * of transition animations, one will be started so all rendered elements
2772 * under @region_id are animated for state @state being set to value
2775 * The @window parameter is used in order to invalidate the rendered area
2776 * as the animation runs, so make sure it is the same window that is being
2777 * rendered on by the gtk_render_*() functions.
2779 * If @region_id is %NULL, all rendered elements using @context will be
2780 * affected by this state transition.
2782 * As a practical example, a #GtkButton notifying a state transition on
2783 * the prelight state:
2785 * gtk_style_context_notify_state_change (context,
2786 * gtk_widget_get_window (widget),
2788 * GTK_STATE_PRELIGHT,
2789 * button->in_button);
2792 * Can be handled in the CSS file like this:
2795 * background-color: #f00
2799 * background-color: #fff;
2800 * transition: 200ms linear
2804 * This combination will animate the button background from red to white
2805 * if a pointer enters the button, and back to red if the pointer leaves
2808 * Note that @state is used when finding the transition parameters, which
2809 * is why the style places the transition under the :hover pseudo-class.
2813 * Deprecated: 3.6: This function does nothing.
2816 gtk_style_context_notify_state_change (GtkStyleContext *context,
2820 gboolean state_value)
2822 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2823 g_return_if_fail (GDK_IS_WINDOW (window));
2824 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2825 g_return_if_fail (context->priv->widget != NULL || context->priv->widget_path != NULL);
2829 * gtk_style_context_cancel_animations:
2830 * @context: a #GtkStyleContext
2831 * @region_id: (allow-none): animatable region to stop, or %NULL.
2832 * See gtk_style_context_push_animatable_region()
2834 * Stops all running animations for @region_id and all animatable
2835 * regions underneath.
2837 * A %NULL @region_id will stop all ongoing animations in @context,
2838 * when dealing with a #GtkStyleContext obtained through
2839 * gtk_widget_get_style_context(), this is normally done for you
2840 * in all circumstances you would expect all widget to be stopped,
2841 * so this should be only used in complex widgets with different
2842 * animatable regions.
2846 * Deprecated: 3.6: This function does nothing.
2849 gtk_style_context_cancel_animations (GtkStyleContext *context,
2852 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2856 * gtk_style_context_scroll_animations:
2857 * @context: a #GtkStyleContext
2858 * @window: a #GdkWindow used previously in
2859 * gtk_style_context_notify_state_change()
2860 * @dx: Amount to scroll in the X axis
2861 * @dy: Amount to scroll in the Y axis
2863 * This function is analogous to gdk_window_scroll(), and
2864 * should be called together with it so the invalidation
2865 * areas for any ongoing animation are scrolled together
2870 * Deprecated: 3.6: This function does nothing.
2873 gtk_style_context_scroll_animations (GtkStyleContext *context,
2878 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2879 g_return_if_fail (GDK_IS_WINDOW (window));
2883 * gtk_style_context_push_animatable_region:
2884 * @context: a #GtkStyleContext
2885 * @region_id: unique identifier for the animatable region
2887 * Pushes an animatable region, so all further gtk_render_*() calls between
2888 * this call and the following gtk_style_context_pop_animatable_region()
2889 * will potentially show transition animations for this region if
2890 * gtk_style_context_notify_state_change() is called for a given state,
2891 * and the current theme/style defines transition animations for state
2894 * The @region_id used must be unique in @context so the theming engine
2895 * can uniquely identify rendered elements subject to a state transition.
2899 * Deprecated: 3.6: This function does nothing.
2902 gtk_style_context_push_animatable_region (GtkStyleContext *context,
2905 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2906 g_return_if_fail (region_id != NULL);
2910 * gtk_style_context_pop_animatable_region:
2911 * @context: a #GtkStyleContext
2913 * Pops an animatable region from @context.
2914 * See gtk_style_context_push_animatable_region().
2918 * Deprecated: 3.6: This function does nothing.
2921 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
2923 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2927 gtk_style_context_clear_cache (GtkStyleContext *context)
2929 GtkStyleContextPrivate *priv;
2932 priv = context->priv;
2934 for (info = priv->info; info; info = info->next)
2936 style_info_set_data (info, NULL);
2938 g_hash_table_remove_all (priv->style_data);
2942 gtk_style_context_update_cache (GtkStyleContext *context,
2943 const GtkBitmask *parent_changes)
2945 GtkStyleContextPrivate *priv;
2946 GHashTableIter iter;
2947 gpointer key, value;
2949 priv = context->priv;
2951 g_hash_table_iter_init (&iter, priv->style_data);
2952 while (g_hash_table_iter_next (&iter, &key, &value))
2954 GtkStyleInfo *info = key;
2955 StyleData *data = value;
2956 GtkBitmask *changes;
2958 changes = _gtk_bitmask_copy (parent_changes);
2959 changes = _gtk_bitmask_intersect (changes, data->store->depends_on_parent);
2960 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
2961 changes = _gtk_bitmask_union (changes, data->store->depends_on_color);
2962 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
2963 changes = _gtk_bitmask_union (changes, data->store->depends_on_font_size);
2965 build_properties (context, data->store, info->state_flags, changes);
2970 gtk_style_context_do_invalidate (GtkStyleContext *context)
2972 GtkStyleContextPrivate *priv;
2974 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2976 priv = context->priv;
2978 /* Avoid reentrancy */
2979 if (priv->invalidating_context)
2982 priv->invalidating_context = TRUE;
2984 g_signal_emit (context, signals[CHANGED], 0);
2986 priv->invalidating_context = FALSE;
2990 _gtk_style_context_stop_animations (GtkStyleContext *context)
2992 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2994 if (!gtk_style_context_is_animating (context))
2997 style_info_set_data (context->priv->info, NULL);
2999 gtk_style_context_stop_animating (context);
3003 gtk_style_context_update_animations (GtkStyleContext *context,
3006 GtkBitmask *differences;
3007 StyleData *style_data;
3009 style_data = style_data_lookup (context);
3011 differences = _gtk_css_computed_values_advance (style_data->store,
3014 if (_gtk_css_computed_values_is_static (style_data->store))
3015 _gtk_style_context_stop_animations (context);
3021 gtk_style_context_should_animate (GtkStyleContext *context)
3023 GtkStyleContextPrivate *priv;
3026 priv = context->priv;
3028 if (priv->widget == NULL)
3031 if (!gtk_widget_get_mapped (priv->widget))
3034 g_object_get (gtk_widget_get_settings (context->priv->widget),
3035 "gtk-enable-animations", &animate,
3042 gtk_style_context_create_animations (GtkStyleContext *context,
3043 GtkCssComputedValues *values,
3044 GtkCssComputedValues *previous,
3047 if (!gtk_style_context_should_animate (context))
3049 gtk_style_context_stop_animating (context);
3053 _gtk_css_computed_values_create_animations (values,
3058 if (_gtk_css_computed_values_is_static (values))
3060 gtk_style_context_stop_animating (context);
3064 gtk_style_context_start_animating (context);
3068 gtk_style_context_needs_full_revalidate (GtkStyleContext *context,
3069 GtkCssChange change)
3071 GtkStyleContextPrivate *priv = context->priv;
3073 /* Try to avoid invalidating if we can */
3074 if (change & GTK_STYLE_CONTEXT_RADICAL_CHANGE)
3076 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
3080 if (priv->relevant_changes == GTK_CSS_CHANGE_ANY)
3082 GtkWidgetPath *path;
3083 GtkCssMatcher matcher;
3085 path = create_query_path (context);
3086 if (_gtk_css_matcher_init (&matcher, path, priv->info->state_flags))
3087 priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3090 priv->relevant_changes = 0;
3092 priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
3094 gtk_widget_path_unref (path);
3098 if (priv->relevant_changes & change)
3105 _gtk_style_context_validate (GtkStyleContext *context,
3107 GtkCssChange change,
3108 const GtkBitmask *parent_changes)
3110 GtkStyleContextPrivate *priv;
3113 GtkBitmask *changes;
3116 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3118 priv = context->priv;
3120 change |= priv->pending_changes;
3122 /* If you run your application with
3123 * GTK_DEBUG=no-css-cache
3124 * every invalidation will purge the cache and completely query
3125 * everything anew form the cache. This is slow (in particular
3126 * when animating), but useful for figuring out bugs.
3128 * We achieve that by pretending that everything that could have
3129 * changed has and so we of course totally need to redo everything.
3131 * Note that this also completely revalidates child widgets all
3134 if (G_UNLIKELY (gtk_get_debug_flags () & GTK_DEBUG_NO_CSS_CACHE))
3135 change = GTK_CSS_CHANGE_ANY;
3137 if (!priv->invalid && change == 0 && _gtk_bitmask_is_empty (parent_changes))
3140 priv->pending_changes = 0;
3141 gtk_style_context_set_invalid (context, FALSE);
3145 current = style_data_ref (info->data);
3149 /* Try to avoid invalidating if we can */
3150 if (current == NULL ||
3151 gtk_style_context_needs_full_revalidate (context, change))
3153 if ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE)
3155 gtk_style_context_clear_cache (context);
3159 gtk_style_context_update_cache (context, parent_changes);
3160 style_info_set_data (info, NULL);
3167 data = style_data_lookup (context);
3169 gtk_style_context_create_animations (context, data->store, current->store, timestamp);
3170 if (_gtk_css_computed_values_is_static (data->store))
3171 change &= ~GTK_CSS_CHANGE_ANIMATE;
3173 change |= GTK_CSS_CHANGE_ANIMATE;
3175 changes = _gtk_css_computed_values_get_difference (data->store, current->store);
3179 changes = _gtk_bitmask_new ();
3180 changes = _gtk_bitmask_invert_range (changes, 0, _gtk_css_style_property_get_n_properties ());
3185 changes = _gtk_bitmask_copy (parent_changes);
3186 changes = _gtk_bitmask_intersect (changes, current->store->depends_on_parent);
3187 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
3188 changes = _gtk_bitmask_union (changes, current->store->depends_on_color);
3189 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
3190 changes = _gtk_bitmask_union (changes, current->store->depends_on_font_size);
3192 gtk_style_context_update_cache (context, parent_changes);
3196 style_data_unref (current);
3198 if (change & GTK_CSS_CHANGE_ANIMATE &&
3199 gtk_style_context_is_animating (context))
3201 GtkBitmask *animation_changes;
3203 animation_changes = gtk_style_context_update_animations (context, timestamp);
3204 changes = _gtk_bitmask_union (changes, animation_changes);
3205 _gtk_bitmask_free (animation_changes);
3208 if (!_gtk_bitmask_is_empty (changes))
3209 gtk_style_context_do_invalidate (context);
3211 change = _gtk_css_change_for_child (change);
3212 for (list = priv->children; list; list = list->next)
3214 _gtk_style_context_validate (list->data, timestamp, change, changes);
3217 _gtk_bitmask_free (changes);
3221 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3222 GtkCssChange change)
3224 GtkStyleContextPrivate *priv;
3226 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3227 g_return_if_fail (change != 0);
3229 priv = context->priv;
3231 if (priv->widget != NULL)
3233 priv->pending_changes |= change;
3234 gtk_style_context_set_invalid (context, TRUE);
3236 else if (priv->widget_path == NULL)
3238 gtk_style_context_invalidate (context);
3243 * gtk_style_context_invalidate:
3244 * @context: a #GtkStyleContext.
3246 * Invalidates @context style information, so it will be reconstructed
3249 * If you're using a #GtkStyleContext returned from
3250 * gtk_widget_get_style_context(), you do not need to
3251 * call this yourself.
3256 gtk_style_context_invalidate (GtkStyleContext *context)
3258 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3260 gtk_style_context_clear_cache (context);
3261 gtk_style_context_do_invalidate (context);
3265 * gtk_style_context_set_background:
3266 * @context: a #GtkStyleContext
3267 * @window: a #GdkWindow
3269 * Sets the background of @window to the background pattern or
3270 * color specified in @context for its current state.
3275 gtk_style_context_set_background (GtkStyleContext *context,
3278 GtkStateFlags state;
3279 cairo_pattern_t *pattern;
3282 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3283 g_return_if_fail (GDK_IS_WINDOW (window));
3285 state = gtk_style_context_get_state (context);
3286 gtk_style_context_get (context, state,
3287 "background-image", &pattern,
3291 gdk_window_set_background_pattern (window, pattern);
3292 cairo_pattern_destroy (pattern);
3296 gtk_style_context_get (context, state,
3297 "background-color", &color,
3301 gdk_window_set_background_rgba (window, color);
3302 gdk_rgba_free (color);
3307 * gtk_style_context_get_color:
3308 * @context: a #GtkStyleContext
3309 * @state: state to retrieve the color for
3310 * @color: (out): return value for the foreground color
3312 * Gets the foreground color for a given state.
3317 gtk_style_context_get_color (GtkStyleContext *context,
3318 GtkStateFlags state,
3323 g_return_if_fail (color != NULL);
3324 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3326 gtk_style_context_get (context,
3336 * gtk_style_context_get_background_color:
3337 * @context: a #GtkStyleContext
3338 * @state: state to retrieve the color for
3339 * @color: (out): return value for the background color
3341 * Gets the background color for a given state.
3346 gtk_style_context_get_background_color (GtkStyleContext *context,
3347 GtkStateFlags state,
3352 g_return_if_fail (color != NULL);
3353 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3355 gtk_style_context_get (context,
3357 "background-color", &c,
3365 * gtk_style_context_get_border_color:
3366 * @context: a #GtkStyleContext
3367 * @state: state to retrieve the color for
3368 * @color: (out): return value for the border color
3370 * Gets the border color for a given state.
3375 gtk_style_context_get_border_color (GtkStyleContext *context,
3376 GtkStateFlags state,
3381 g_return_if_fail (color != NULL);
3382 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3384 gtk_style_context_get (context,
3394 * gtk_style_context_get_border:
3395 * @context: a #GtkStyleContext
3396 * @state: state to retrieve the border for
3397 * @border: (out): return value for the border settings
3399 * Gets the border for a given state as a #GtkBorder.
3400 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3405 gtk_style_context_get_border (GtkStyleContext *context,
3406 GtkStateFlags state,
3409 int top, left, bottom, right;
3411 g_return_if_fail (border != NULL);
3412 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3414 gtk_style_context_get (context,
3416 "border-top-width", &top,
3417 "border-left-width", &left,
3418 "border-bottom-width", &bottom,
3419 "border-right-width", &right,
3423 border->left = left;
3424 border->bottom = bottom;
3425 border->right = right;
3429 * gtk_style_context_get_padding:
3430 * @context: a #GtkStyleContext
3431 * @state: state to retrieve the padding for
3432 * @padding: (out): return value for the padding settings
3434 * Gets the padding for a given state as a #GtkBorder.
3435 * See %GTK_STYLE_PROPERTY_PADDING.
3440 gtk_style_context_get_padding (GtkStyleContext *context,
3441 GtkStateFlags state,
3444 int top, left, bottom, right;
3446 g_return_if_fail (padding != NULL);
3447 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3449 gtk_style_context_get (context,
3451 "padding-top", &top,
3452 "padding-left", &left,
3453 "padding-bottom", &bottom,
3454 "padding-right", &right,
3458 padding->left = left;
3459 padding->bottom = bottom;
3460 padding->right = right;
3464 * gtk_style_context_get_margin:
3465 * @context: a #GtkStyleContext
3466 * @state: state to retrieve the border for
3467 * @margin: (out): return value for the margin settings
3469 * Gets the margin for a given state as a #GtkBorder.
3470 * See %GTK_STYLE_PROPERTY_MARGIN.
3475 gtk_style_context_get_margin (GtkStyleContext *context,
3476 GtkStateFlags state,
3479 int top, left, bottom, right;
3481 g_return_if_fail (margin != NULL);
3482 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3484 gtk_style_context_get (context,
3487 "margin-left", &left,
3488 "margin-bottom", &bottom,
3489 "margin-right", &right,
3493 margin->left = left;
3494 margin->bottom = bottom;
3495 margin->right = right;
3499 * gtk_style_context_get_font:
3500 * @context: a #GtkStyleContext
3501 * @state: state to retrieve the font for
3503 * Returns the font description for a given state. The returned
3504 * object is const and will remain valid until the
3505 * #GtkStyleContext::changed signal happens.
3507 * Returns: (transfer none): the #PangoFontDescription for the given
3508 * state. This object is owned by GTK+ and should not be
3513 const PangoFontDescription *
3514 gtk_style_context_get_font (GtkStyleContext *context,
3515 GtkStateFlags state)
3517 GtkStyleContextPrivate *priv;
3519 PangoFontDescription *description;
3521 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3523 priv = context->priv;
3524 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3526 data = style_data_lookup_for_state (context, state);
3528 /* Yuck, fonts are created on-demand but we don't return a ref.
3529 * Do bad things to achieve this requirement */
3530 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3531 if (description == NULL)
3533 gtk_style_context_get (context, state, "font", &description, NULL);
3534 g_object_set_data_full (G_OBJECT (data->store),
3535 "font-cache-for-get_font",
3537 (GDestroyNotify) pango_font_description_free);
3543 get_cursor_color (GtkStyleContext *context,
3547 GdkColor *style_color;
3549 gtk_style_context_get_style (context,
3550 primary ? "cursor-color" : "secondary-cursor-color",
3556 color->red = style_color->red / 65535.0;
3557 color->green = style_color->green / 65535.0;
3558 color->blue = style_color->blue / 65535.0;
3561 gdk_color_free (style_color);
3565 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3571 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3573 color->red = (color->red + bg.red) * 0.5;
3574 color->green = (color->green + bg.green) * 0.5;
3575 color->blue = (color->blue + bg.blue) * 0.5;
3581 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3582 GdkRGBA *primary_color,
3583 GdkRGBA *secondary_color)
3586 get_cursor_color (context, TRUE, primary_color);
3588 if (secondary_color)
3589 get_cursor_color (context, FALSE, secondary_color);
3596 * @context: a #GtkStyleContext
3598 * @x: X origin of the rectangle
3599 * @y: Y origin of the rectangle
3600 * @width: rectangle width
3601 * @height: rectangle height
3603 * Renders a checkmark (as in a #GtkCheckButton).
3605 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3606 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3607 * should be marked as undefined.
3610 * <title>Typical checkmark rendering</title>
3611 * <inlinegraphic fileref="checks.png" format="PNG"/>
3617 gtk_render_check (GtkStyleContext *context,
3624 GtkThemingEngineClass *engine_class;
3625 GtkThemingEngine *engine;
3627 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3628 g_return_if_fail (cr != NULL);
3630 if (width <= 0 || height <= 0)
3633 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3634 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3639 _gtk_theming_engine_set_context (engine, context);
3640 engine_class->render_check (engine, cr,
3641 x, y, width, height);
3647 * gtk_render_option:
3648 * @context: a #GtkStyleContext
3650 * @x: X origin of the rectangle
3651 * @y: Y origin of the rectangle
3652 * @width: rectangle width
3653 * @height: rectangle height
3655 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3656 * state will determine whether the option is on or off, and
3657 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3660 * <title>Typical option mark rendering</title>
3661 * <inlinegraphic fileref="options.png" format="PNG"/>
3667 gtk_render_option (GtkStyleContext *context,
3674 GtkThemingEngineClass *engine_class;
3675 GtkThemingEngine *engine;
3677 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3678 g_return_if_fail (cr != NULL);
3680 if (width <= 0 || height <= 0)
3683 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3684 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3688 _gtk_theming_engine_set_context (engine, context);
3689 engine_class->render_option (engine, cr,
3690 x, y, width, height);
3697 * @context: a #GtkStyleContext
3699 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3700 * @x: X origin of the render area
3701 * @y: Y origin of the render area
3702 * @size: square side for render area
3704 * Renders an arrow pointing to @angle.
3707 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3708 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3714 gtk_render_arrow (GtkStyleContext *context,
3721 GtkThemingEngineClass *engine_class;
3722 GtkThemingEngine *engine;
3724 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3725 g_return_if_fail (cr != NULL);
3730 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3731 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3735 gtk_style_context_save (context);
3736 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3738 _gtk_theming_engine_set_context (engine, context);
3739 engine_class->render_arrow (engine, cr,
3742 gtk_style_context_restore (context);
3747 * gtk_render_background:
3748 * @context: a #GtkStyleContext
3750 * @x: X origin of the rectangle
3751 * @y: Y origin of the rectangle
3752 * @width: rectangle width
3753 * @height: rectangle height
3755 * Renders the background of an element.
3758 * <title>Typical background rendering, showing the effect of
3759 * <parameter>background-image</parameter>,
3760 * <parameter>border-width</parameter> and
3761 * <parameter>border-radius</parameter></title>
3762 * <inlinegraphic fileref="background.png" format="PNG"/>
3768 gtk_render_background (GtkStyleContext *context,
3775 GtkThemingEngineClass *engine_class;
3776 GtkThemingEngine *engine;
3778 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3779 g_return_if_fail (cr != NULL);
3781 if (width <= 0 || height <= 0)
3784 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3785 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3789 _gtk_theming_engine_set_context (engine, context);
3790 engine_class->render_background (engine, cr, x, y, width, height);
3797 * @context: a #GtkStyleContext
3799 * @x: X origin of the rectangle
3800 * @y: Y origin of the rectangle
3801 * @width: rectangle width
3802 * @height: rectangle height
3804 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3807 * <title>Examples of frame rendering, showing the effect of
3808 * <parameter>border-image</parameter>,
3809 * <parameter>border-color</parameter>,
3810 * <parameter>border-width</parameter>,
3811 * <parameter>border-radius</parameter> and
3813 * <inlinegraphic fileref="frames.png" format="PNG"/>
3819 gtk_render_frame (GtkStyleContext *context,
3826 GtkThemingEngineClass *engine_class;
3827 GtkThemingEngine *engine;
3829 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3830 g_return_if_fail (cr != NULL);
3832 if (width <= 0 || height <= 0)
3835 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3836 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3840 _gtk_theming_engine_set_context (engine, context);
3841 engine_class->render_frame (engine, cr, x, y, width, height);
3847 * gtk_render_expander:
3848 * @context: a #GtkStyleContext
3850 * @x: X origin of the rectangle
3851 * @y: Y origin of the rectangle
3852 * @width: rectangle width
3853 * @height: rectangle height
3855 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3856 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3857 * determines whether the expander is collapsed or expanded.
3860 * <title>Typical expander rendering</title>
3861 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3867 gtk_render_expander (GtkStyleContext *context,
3874 GtkThemingEngineClass *engine_class;
3875 GtkThemingEngine *engine;
3877 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3878 g_return_if_fail (cr != NULL);
3880 if (width <= 0 || height <= 0)
3883 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3884 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3888 _gtk_theming_engine_set_context (engine, context);
3889 engine_class->render_expander (engine, cr, x, y, width, height);
3896 * @context: a #GtkStyleContext
3898 * @x: X origin of the rectangle
3899 * @y: Y origin of the rectangle
3900 * @width: rectangle width
3901 * @height: rectangle height
3903 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3905 * <title>Typical focus rendering</title>
3906 * <inlinegraphic fileref="focus.png" format="PNG"/>
3912 gtk_render_focus (GtkStyleContext *context,
3919 GtkThemingEngineClass *engine_class;
3920 GtkThemingEngine *engine;
3922 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3923 g_return_if_fail (cr != NULL);
3925 if (width <= 0 || height <= 0)
3928 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3929 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3933 _gtk_theming_engine_set_context (engine, context);
3934 engine_class->render_focus (engine, cr, x, y, width, height);
3940 * gtk_render_layout:
3941 * @context: a #GtkStyleContext
3945 * @layout: the #PangoLayout to render
3947 * Renders @layout on the coordinates @x, @y
3952 gtk_render_layout (GtkStyleContext *context,
3956 PangoLayout *layout)
3958 GtkThemingEngineClass *engine_class;
3959 GtkThemingEngine *engine;
3960 PangoRectangle extents;
3962 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3963 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3964 g_return_if_fail (cr != NULL);
3966 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3967 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3971 pango_layout_get_extents (layout, &extents, NULL);
3973 _gtk_theming_engine_set_context (engine, context);
3974 engine_class->render_layout (engine, cr, x, y, layout);
3981 * @context: a #GtkStyleContext
3983 * @x0: X coordinate for the origin of the line
3984 * @y0: Y coordinate for the origin of the line
3985 * @x1: X coordinate for the end of the line
3986 * @y1: Y coordinate for the end of the line
3988 * Renders a line from (x0, y0) to (x1, y1).
3993 gtk_render_line (GtkStyleContext *context,
4000 GtkThemingEngineClass *engine_class;
4001 GtkThemingEngine *engine;
4003 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4004 g_return_if_fail (cr != NULL);
4006 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4007 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4011 _gtk_theming_engine_set_context (engine, context);
4012 engine_class->render_line (engine, cr, x0, y0, x1, y1);
4018 * gtk_render_slider:
4019 * @context: a #GtkStyleContext
4021 * @x: X origin of the rectangle
4022 * @y: Y origin of the rectangle
4023 * @width: rectangle width
4024 * @height: rectangle height
4025 * @orientation: orientation of the slider
4027 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4028 * @width, @height. @orientation defines whether the slider is vertical
4032 * <title>Typical slider rendering</title>
4033 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4039 gtk_render_slider (GtkStyleContext *context,
4045 GtkOrientation orientation)
4047 GtkThemingEngineClass *engine_class;
4048 GtkThemingEngine *engine;
4050 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4051 g_return_if_fail (cr != NULL);
4053 if (width <= 0 || height <= 0)
4056 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4057 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4061 _gtk_theming_engine_set_context (engine, context);
4062 engine_class->render_slider (engine, cr, x, y, width, height, orientation);
4068 * gtk_render_frame_gap:
4069 * @context: a #GtkStyleContext
4071 * @x: X origin of the rectangle
4072 * @y: Y origin of the rectangle
4073 * @width: rectangle width
4074 * @height: rectangle height
4075 * @gap_side: side where the gap is
4076 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4077 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4079 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4080 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4081 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4082 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4085 * <title>Typical rendering of a frame with a gap</title>
4086 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4092 gtk_render_frame_gap (GtkStyleContext *context,
4098 GtkPositionType gap_side,
4102 GtkThemingEngineClass *engine_class;
4103 GtkThemingEngine *engine;
4105 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4106 g_return_if_fail (cr != NULL);
4107 g_return_if_fail (xy0_gap <= xy1_gap);
4108 g_return_if_fail (xy0_gap >= 0);
4110 if (width <= 0 || height <= 0)
4113 if (gap_side == GTK_POS_LEFT ||
4114 gap_side == GTK_POS_RIGHT)
4115 g_return_if_fail (xy1_gap <= height);
4117 g_return_if_fail (xy1_gap <= width);
4119 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4120 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4124 _gtk_theming_engine_set_context (engine, context);
4125 engine_class->render_frame_gap (engine, cr,
4126 x, y, width, height, gap_side,
4133 * gtk_render_extension:
4134 * @context: a #GtkStyleContext
4136 * @x: X origin of the rectangle
4137 * @y: Y origin of the rectangle
4138 * @width: rectangle width
4139 * @height: rectangle height
4140 * @gap_side: side where the gap is
4142 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4143 * defined by @x, @y, @width, @height. The side where the extension
4144 * connects to is defined by @gap_side.
4147 * <title>Typical extension rendering</title>
4148 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4154 gtk_render_extension (GtkStyleContext *context,
4160 GtkPositionType gap_side)
4162 GtkThemingEngineClass *engine_class;
4163 GtkThemingEngine *engine;
4165 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4166 g_return_if_fail (cr != NULL);
4168 if (width <= 0 || height <= 0)
4171 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4172 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4176 _gtk_theming_engine_set_context (engine, context);
4177 engine_class->render_extension (engine, cr, x, y, width, height, gap_side);
4183 * gtk_render_handle:
4184 * @context: a #GtkStyleContext
4186 * @x: X origin of the rectangle
4187 * @y: Y origin of the rectangle
4188 * @width: rectangle width
4189 * @height: rectangle height
4191 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4192 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4193 * determined by @x, @y, @width, @height.
4196 * <title>Handles rendered for the paned and grip classes</title>
4197 * <inlinegraphic fileref="handles.png" format="PNG"/>
4203 gtk_render_handle (GtkStyleContext *context,
4210 GtkThemingEngineClass *engine_class;
4211 GtkThemingEngine *engine;
4213 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4214 g_return_if_fail (cr != NULL);
4216 if (width <= 0 || height <= 0)
4219 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4220 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4224 _gtk_theming_engine_set_context (engine, context);
4225 engine_class->render_handle (engine, cr, x, y, width, height);
4231 * gtk_render_activity:
4232 * @context: a #GtkStyleContext
4234 * @x: X origin of the rectangle
4235 * @y: Y origin of the rectangle
4236 * @width: rectangle width
4237 * @height: rectangle height
4239 * Renders an activity area (Such as in #GtkSpinner or the
4240 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4241 * determines whether there is activity going on.
4246 gtk_render_activity (GtkStyleContext *context,
4253 GtkThemingEngineClass *engine_class;
4254 GtkThemingEngine *engine;
4256 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4257 g_return_if_fail (cr != NULL);
4259 if (width <= 0 || height <= 0)
4262 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4263 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4267 _gtk_theming_engine_set_context (engine, context);
4268 engine_class->render_activity (engine, cr, x, y, width, height);
4274 * gtk_render_icon_pixbuf:
4275 * @context: a #GtkStyleContext
4276 * @source: the #GtkIconSource specifying the icon to render
4277 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4278 * means render at the size of the source and don't scale.
4280 * Renders the icon specified by @source at the given @size, returning the result
4283 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4288 gtk_render_icon_pixbuf (GtkStyleContext *context,
4289 const GtkIconSource *source,
4292 GtkThemingEngineClass *engine_class;
4293 GtkThemingEngine *engine;
4295 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4296 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4297 g_return_val_if_fail (source != NULL, NULL);
4299 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4300 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4302 _gtk_theming_engine_set_context (engine, context);
4303 return engine_class->render_icon_pixbuf (engine, source, size);
4308 * @context: a #GtkStyleContext
4310 * @pixbuf: a #GdkPixbuf containing the icon to draw
4311 * @x: X position for the @pixbuf
4312 * @y: Y position for the @pixbuf
4314 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4319 gtk_render_icon (GtkStyleContext *context,
4325 GtkThemingEngineClass *engine_class;
4326 GtkThemingEngine *engine;
4328 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4329 g_return_if_fail (cr != NULL);
4331 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4332 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4336 _gtk_theming_engine_set_context (engine, context);
4337 engine_class->render_icon (engine, cr, pixbuf, x, y);
4343 draw_insertion_cursor (GtkStyleContext *context,
4348 gboolean is_primary,
4349 PangoDirection direction,
4350 gboolean draw_arrow)
4353 GdkRGBA primary_color;
4354 GdkRGBA secondary_color;
4355 gfloat cursor_aspect_ratio;
4361 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4362 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4364 /* When changing the shape or size of the cursor here,
4365 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4368 gtk_style_context_get_style (context,
4369 "cursor-aspect-ratio", &cursor_aspect_ratio,
4372 stem_width = height * cursor_aspect_ratio + 1;
4374 /* put (stem_width % 2) on the proper side of the cursor */
4375 if (direction == PANGO_DIRECTION_LTR)
4376 offset = stem_width / 2;
4378 offset = stem_width - stem_width / 2;
4380 cairo_rectangle (cr, x - offset, y, stem_width, height);
4388 arrow_width = stem_width + 1;
4390 if (direction == PANGO_DIRECTION_RTL)
4392 ax = x - offset - 1;
4393 ay = y + height - arrow_width * 2 - arrow_width + 1;
4395 cairo_move_to (cr, ax, ay + 1);
4396 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4397 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4400 else if (direction == PANGO_DIRECTION_LTR)
4402 ax = x + stem_width - offset;
4403 ay = y + height - arrow_width * 2 - arrow_width + 1;
4405 cairo_move_to (cr, ax, ay + 1);
4406 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4407 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4411 g_assert_not_reached();
4418 * gtk_render_insertion_cursor:
4419 * @context: a #GtkStyleContext
4423 * @layout: the #PangoLayout of the text
4424 * @index: the index in the #PangoLayout
4425 * @direction: the #PangoDirection of the text
4427 * Draws a text caret on @cr at the specified index of @layout.
4432 gtk_render_insertion_cursor (GtkStyleContext *context,
4436 PangoLayout *layout,
4438 PangoDirection direction)
4440 GtkStyleContextPrivate *priv;
4441 gboolean split_cursor;
4442 PangoRectangle strong_pos, weak_pos;
4443 PangoRectangle *cursor1, *cursor2;
4444 PangoDirection keymap_direction;
4445 PangoDirection direction2;
4447 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4448 g_return_if_fail (cr != NULL);
4449 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4450 g_return_if_fail (index >= 0);
4452 priv = context->priv;
4454 g_object_get (gtk_settings_get_for_screen (priv->screen),
4455 "gtk-split-cursor", &split_cursor,
4458 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4460 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4462 direction2 = PANGO_DIRECTION_NEUTRAL;
4466 cursor1 = &strong_pos;
4468 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4470 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4471 cursor2 = &weak_pos;
4476 if (keymap_direction == direction)
4477 cursor1 = &strong_pos;
4479 cursor1 = &weak_pos;
4482 draw_insertion_cursor (context,
4484 x + PANGO_PIXELS (cursor1->x),
4485 y + PANGO_PIXELS (cursor1->y),
4486 PANGO_PIXELS (cursor1->height),
4489 direction2 != PANGO_DIRECTION_NEUTRAL);
4491 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4493 draw_insertion_cursor (context,
4495 x + PANGO_PIXELS (cursor2->x),
4496 y + PANGO_PIXELS (cursor2->y),
4497 PANGO_PIXELS (cursor2->height),
4505 * gtk_draw_insertion_cursor:
4506 * @widget: a #GtkWidget
4507 * @cr: cairo context to draw to
4508 * @location: location where to draw the cursor (@location->width is ignored)
4509 * @is_primary: if the cursor should be the primary cursor color.
4510 * @direction: whether the cursor is left-to-right or
4511 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4512 * @draw_arrow: %TRUE to draw a directional arrow on the
4513 * cursor. Should be %FALSE unless the cursor is split.
4515 * Draws a text caret on @cr at @location. This is not a style function
4516 * but merely a convenience function for drawing the standard cursor shape.
4519 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4522 gtk_draw_insertion_cursor (GtkWidget *widget,
4524 const GdkRectangle *location,
4525 gboolean is_primary,
4526 GtkTextDirection direction,
4527 gboolean draw_arrow)
4529 GtkStyleContext *context;
4531 g_return_if_fail (GTK_IS_WIDGET (widget));
4532 g_return_if_fail (cr != NULL);
4533 g_return_if_fail (location != NULL);
4534 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4536 context = gtk_widget_get_style_context (widget);
4538 draw_insertion_cursor (context, cr,
4539 location->x, location->y, location->height,
4541 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4545 static AtkAttributeSet *
4546 add_attribute (AtkAttributeSet *attributes,
4547 AtkTextAttribute attr,
4552 at = g_new (AtkAttribute, 1);
4553 at->name = g_strdup (atk_text_attribute_get_name (attr));
4554 at->value = g_strdup (value);
4556 return g_slist_prepend (attributes, at);
4560 * _gtk_style_context_get_attributes:
4561 * @attributes: a #AtkAttributeSet to add attributes to
4562 * @context: the #GtkStyleContext to get attributes from
4563 * @flags: the state to use with @context
4565 * Adds the foreground and background color from @context to
4566 * @attributes, after translating them to ATK attributes.
4568 * This is a convenience function that can be used in
4569 * implementing the #AtkText interface in widgets.
4571 * Returns: the modified #AtkAttributeSet
4574 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4575 GtkStyleContext *context,
4576 GtkStateFlags flags)
4581 gtk_style_context_get_background_color (context, flags, &color);
4582 value = g_strdup_printf ("%u,%u,%u",
4583 (guint) ceil (color.red * 65536 - color.red),
4584 (guint) ceil (color.green * 65536 - color.green),
4585 (guint) ceil (color.blue * 65536 - color.blue));
4586 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4589 gtk_style_context_get_color (context, flags, &color);
4590 value = g_strdup_printf ("%u,%u,%u",
4591 (guint) ceil (color.red * 65536 - color.red),
4592 (guint) ceil (color.green * 65536 - color.green),
4593 (guint) ceil (color.blue * 65536 - color.blue));
4594 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);