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 "gtkcssanimatedvaluesprivate.h"
28 #include "gtkcssenginevalueprivate.h"
29 #include "gtkcssnumbervalueprivate.h"
30 #include "gtkcssrgbavalueprivate.h"
32 #include "gtkstylepropertiesprivate.h"
33 #include "gtktypebuiltins.h"
34 #include "gtkthemingengineprivate.h"
36 #include "gtkwidget.h"
37 #include "gtkwindow.h"
38 #include "gtkprivate.h"
39 #include "gtksymboliccolorprivate.h"
40 #include "gtkiconfactory.h"
41 #include "gtkwidgetpath.h"
42 #include "gtkwidgetprivate.h"
43 #include "gtkstylecascadeprivate.h"
44 #include "gtkstyleproviderprivate.h"
45 #include "gtksettings.h"
46 #include "gtksettingsprivate.h"
49 * SECTION:gtkstylecontext
50 * @Short_description: Rendering UI elements
51 * @Title: GtkStyleContext
53 * #GtkStyleContext is an object that stores styling information affecting
54 * a widget defined by #GtkWidgetPath.
56 * In order to construct the final style information, #GtkStyleContext
57 * queries information from all attached #GtkStyleProviders. Style providers
58 * can be either attached explicitly to the context through
59 * gtk_style_context_add_provider(), or to the screen through
60 * gtk_style_context_add_provider_for_screen(). The resulting style is a
61 * combination of all providers' information in priority order.
63 * For GTK+ widgets, any #GtkStyleContext returned by
64 * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
65 * #GdkScreen and RTL/LTR information set. The style context will be also
66 * updated automatically if any of these settings change on the widget.
68 * If you are using the theming layer standalone, you will need to set a
69 * widget path and a screen yourself to the created style context through
70 * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
71 * as updating the context yourself using gtk_style_context_invalidate()
72 * whenever any of the conditions change, such as a change in the
73 * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
76 * <refsect2 id="gtkstylecontext-animations">
77 * <title>Transition animations</title>
79 * #GtkStyleContext has built-in support for state change transitions.
80 * Note that these animations respect the #GtkSettings:gtk-enable-animations
84 * For simple widgets where state changes affect the whole widget area,
85 * calling gtk_style_context_notify_state_change() with a %NULL region
86 * is sufficient to trigger the transition animation. And GTK+ already
87 * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
91 * If a widget needs to declare several animatable regions (i.e. not
92 * affecting the whole widget area), its #GtkWidget::draw signal handler
93 * needs to wrap the render operations for the different regions with
94 * calls to gtk_style_context_push_animatable_region() and
95 * gtk_style_context_pop_animatable_region(). These functions take an
96 * identifier for the region which must be unique within the style context.
97 * For simple widgets with a fixed set of animatable regions, using an
98 * enumeration works well:
101 * <title>Using an enumeration to identify animatable regions</title>
112 * spin_button_draw (GtkWidget *widget,
115 * GtkStyleContext *context;
117 * context = gtk_widget_get_style_context (widget);
119 * gtk_style_context_push_animatable_region (context,
120 * GUINT_TO_POINTER (REGION_ENTRY));
122 * gtk_render_background (cr, 0, 0, 100, 30);
123 * gtk_render_frame (cr, 0, 0, 100, 30);
125 * gtk_style_context_pop_animatable_region (context);
132 * For complex widgets with an arbitrary number of animatable regions, it
133 * is up to the implementation to come up with a way to uniquely identify
134 * each animatable region. Using pointers to internal structs is one way
138 * <title>Using struct pointers to identify animatable regions</title>
141 * notebook_draw_tab (GtkWidget *widget,
142 * NotebookPage *page,
145 * gtk_style_context_push_animatable_region (context, page);
146 * gtk_render_extension (cr, page->x, page->y, page->width, page->height);
147 * gtk_style_context_pop_animatable_region (context);
152 * The widget also needs to notify the style context about a state change
153 * for a given animatable region so the animation is triggered.
156 * <title>Triggering a state change animation on a region</title>
159 * notebook_motion_notify (GtkWidget *widget,
160 * GdkEventMotion *event)
162 * GtkStyleContext *context;
163 * NotebookPage *page;
165 * context = gtk_widget_get_style_context (widget);
166 * page = find_page_under_pointer (widget, event);
167 * gtk_style_context_notify_state_change (context,
168 * gtk_widget_get_window (widget),
170 * GTK_STATE_PRELIGHT,
177 * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
178 * special value, in this case, the whole widget area will be updated
182 * <refsect2 id="gtkstylecontext-classes">
183 * <title>Style classes and regions</title>
185 * Widgets can add style classes to their context, which can be used
186 * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
187 * rendering. GTK+ has a number of predefined style classes:
188 * #GTK_STYLE_CLASS_CELL,
189 * #GTK_STYLE_CLASS_ENTRY,
190 * #GTK_STYLE_CLASS_BUTTON,
191 * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
192 * #GTK_STYLE_CLASS_CALENDAR,
193 * #GTK_STYLE_CLASS_SLIDER,
194 * #GTK_STYLE_CLASS_BACKGROUND,
195 * #GTK_STYLE_CLASS_RUBBERBAND,
196 * #GTK_STYLE_CLASS_TOOLTIP,
197 * #GTK_STYLE_CLASS_MENU,
198 * #GTK_STYLE_CLASS_MENUBAR,
199 * #GTK_STYLE_CLASS_MENUITEM,
200 * #GTK_STYLE_CLASS_TOOLBAR,
201 * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
202 * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
203 * #GTK_STYLE_CLASS_RADIO,
204 * #GTK_STYLE_CLASS_CHECK,
205 * #GTK_STYLE_CLASS_TROUGH,
206 * #GTK_STYLE_CLASS_SCROLLBAR,
207 * #GTK_STYLE_CLASS_SCALE,
208 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
209 * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
210 * #GTK_STYLE_CLASS_HEADER,
211 * #GTK_STYLE_CLASS_ACCELERATOR,
212 * #GTK_STYLE_CLASS_GRIP,
213 * #GTK_STYLE_CLASS_DOCK,
214 * #GTK_STYLE_CLASS_PROGRESSBAR,
215 * #GTK_STYLE_CLASS_SPINNER,
216 * #GTK_STYLE_CLASS_EXPANDER,
217 * #GTK_STYLE_CLASS_SPINBUTTON,
218 * #GTK_STYLE_CLASS_NOTEBOOK,
219 * #GTK_STYLE_CLASS_VIEW,
220 * #GTK_STYLE_CLASS_SIDEBAR,
221 * #GTK_STYLE_CLASS_IMAGE,
222 * #GTK_STYLE_CLASS_HIGHLIGHT,
223 * #GTK_STYLE_CLASS_FRAME,
224 * #GTK_STYLE_CLASS_DND,
225 * #GTK_STYLE_CLASS_PANE_SEPARATOR,
226 * #GTK_STYLE_CLASS_SEPARATOR,
227 * #GTK_STYLE_CLASS_INFO,
228 * #GTK_STYLE_CLASS_WARNING,
229 * #GTK_STYLE_CLASS_QUESTION,
230 * #GTK_STYLE_CLASS_ERROR,
231 * #GTK_STYLE_CLASS_HORIZONTAL,
232 * #GTK_STYLE_CLASS_VERTICAL,
233 * #GTK_STYLE_CLASS_TOP,
234 * #GTK_STYLE_CLASS_BOTTOM,
235 * #GTK_STYLE_CLASS_LEFT,
236 * #GTK_STYLE_CLASS_RIGHT,
239 * Widgets can also add regions with flags to their context.
240 * The regions used by GTK+ widgets are:
245 * <entry>Region</entry>
246 * <entry>Flags</entry>
247 * <entry>Macro</entry>
248 * <entry>Used by</entry>
254 * <entry>even, odd</entry>
255 * <entry>GTK_STYLE_REGION_ROW</entry>
256 * <entry>#GtkTreeView</entry>
259 * <entry>column</entry>
260 * <entry>first, last, sorted</entry>
261 * <entry>GTK_STYLE_REGION_COLUMN</entry>
262 * <entry>#GtkTreeView</entry>
265 * <entry>column-header</entry>
267 * <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
272 * <entry>even, odd, first, last</entry>
273 * <entry>GTK_STYLE_REGION_TAB</entry>
274 * <entry>#GtkNotebook</entry>
281 * <refsect2 id="gtkstylecontext-custom-styling">
282 * <title>Custom styling in UI libraries and applications</title>
284 * If you are developing a library with custom #GtkWidget<!-- -->s that
285 * render differently than standard components, you may need to add a
286 * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
287 * priority, either a #GtkCssProvider or a custom object implementing the
288 * #GtkStyleProvider interface. This way theming engines may still attempt
289 * to style your UI elements in a different way if needed so.
292 * If you are using custom styling on an applications, you probably want then
293 * to make your style information prevail to the theme's, so you must use
294 * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
295 * priority, keep in mind that the user settings in
296 * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
297 * still take precedence over your changes, as it uses the
298 * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
301 * If a custom theming engine is needed, you probably want to implement a
302 * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
303 * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
304 * which loads the theming engine module from the standard paths.
309 /* When these change we do a full restyling. Otherwise we try to figure out
310 * if we need to change things. */
311 #define GTK_STYLE_CONTEXT_RADICAL_CHANGE (GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_SOURCE)
312 /* When these change we don't clear the cache. This takes more memory but makes
313 * things go faster. */
314 #define GTK_STYLE_CONTEXT_CACHED_CHANGE (GTK_CSS_CHANGE_STATE)
316 typedef struct GtkStyleInfo GtkStyleInfo;
317 typedef struct GtkRegion GtkRegion;
318 typedef struct PropertyValue PropertyValue;
319 typedef struct StyleData StyleData;
324 GtkRegionFlags flags;
338 GArray *style_classes;
340 GtkJunctionSides junction_sides;
341 GtkStateFlags state_flags;
347 GtkCssComputedValues *store;
348 GArray *property_cache;
352 struct _GtkStyleContextPrivate
356 GtkStyleCascade *cascade;
358 GtkStyleContext *animation_list_prev;
359 GtkStyleContext *animation_list_next;
361 GtkStyleContext *parent;
364 GtkWidgetPath *widget_path;
365 GHashTable *style_data;
368 GtkTextDirection direction;
370 GtkCssChange relevant_changes;
371 GtkCssChange pending_changes;
373 guint invalidating_context : 1;
389 static guint signals[LAST_SIGNAL] = { 0 };
390 static GtkStyleContext *_running_animations = NULL;
391 guint _running_animations_timer_id = 0;
393 static void gtk_style_context_finalize (GObject *object);
395 static void gtk_style_context_impl_set_property (GObject *object,
399 static void gtk_style_context_impl_get_property (GObject *object,
403 static GtkSymbolicColor *
404 gtk_style_context_color_lookup_func (gpointer contextp,
408 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
411 gtk_style_context_real_changed (GtkStyleContext *context)
413 GtkStyleContextPrivate *priv = context->priv;
416 _gtk_widget_style_context_invalidated (priv->widget);
420 gtk_style_context_class_init (GtkStyleContextClass *klass)
422 GObjectClass *object_class = G_OBJECT_CLASS (klass);
424 object_class->finalize = gtk_style_context_finalize;
425 object_class->set_property = gtk_style_context_impl_set_property;
426 object_class->get_property = gtk_style_context_impl_get_property;
428 klass->changed = gtk_style_context_real_changed;
431 g_signal_new (I_("changed"),
432 G_TYPE_FROM_CLASS (object_class),
434 G_STRUCT_OFFSET (GtkStyleContextClass, changed),
436 g_cclosure_marshal_VOID__VOID,
439 g_object_class_install_property (object_class,
441 g_param_spec_object ("screen",
443 P_("The associated GdkScreen"),
445 GTK_PARAM_READWRITE));
446 g_object_class_install_property (object_class,
448 g_param_spec_enum ("direction",
450 P_("Text direction"),
451 GTK_TYPE_TEXT_DIRECTION,
453 GTK_PARAM_READWRITE));
455 * GtkStyleContext:parent:
457 * Sets or gets the style context's parent. See gtk_style_context_set_parent()
462 g_object_class_install_property (object_class,
464 g_param_spec_object ("parent",
466 P_("The parent style context"),
467 GTK_TYPE_STYLE_CONTEXT,
468 GTK_PARAM_READWRITE));
470 g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
474 style_data_new (void)
478 data = g_slice_new0 (StyleData);
485 clear_property_cache (StyleData *data)
489 if (!data->property_cache)
492 for (i = 0; i < data->property_cache->len; i++)
494 PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
496 g_param_spec_unref (node->pspec);
497 g_value_unset (&node->value);
500 g_array_free (data->property_cache, TRUE);
501 data->property_cache = NULL;
505 style_data_ref (StyleData *style_data)
507 style_data->ref_count++;
513 style_data_unref (StyleData *data)
517 if (data->ref_count > 0)
520 g_object_unref (data->store);
521 clear_property_cache (data);
523 g_slice_free (StyleData, data);
527 style_data_is_animating (StyleData *style_data)
529 return GTK_IS_CSS_ANIMATED_VALUES (style_data->store);
532 static GtkStyleInfo *
533 style_info_new (void)
537 info = g_slice_new0 (GtkStyleInfo);
538 info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
539 info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
545 style_info_set_data (GtkStyleInfo *info,
548 if (info->data == data)
552 style_data_ref (data);
555 style_data_unref (info->data);
561 style_info_free (GtkStyleInfo *info)
563 style_info_set_data (info, NULL);
564 g_array_free (info->style_classes, TRUE);
565 g_array_free (info->regions, TRUE);
566 g_slice_free (GtkStyleInfo, info);
569 static GtkStyleInfo *
570 style_info_pop (GtkStyleInfo *info)
572 GtkStyleInfo *next = info->next;
574 style_info_free (info);
579 static GtkStyleInfo *
580 style_info_copy (GtkStyleInfo *info)
584 copy = style_info_new ();
585 g_array_insert_vals (copy->style_classes, 0,
586 info->style_classes->data,
587 info->style_classes->len);
589 g_array_insert_vals (copy->regions, 0,
594 copy->junction_sides = info->junction_sides;
595 copy->state_flags = info->state_flags;
596 style_info_set_data (copy, info->data);
602 style_info_hash (gconstpointer elem)
604 const GtkStyleInfo *info;
609 for (i = 0; i < info->style_classes->len; i++)
611 hash += g_array_index (info->style_classes, GQuark, i);
615 for (i = 0; i < info->regions->len; i++)
619 region = &g_array_index (info->regions, GtkRegion, i);
620 hash += region->class_quark;
621 hash += region->flags;
625 return hash ^ info->state_flags;
629 style_info_equal (gconstpointer elem1,
632 const GtkStyleInfo *info1, *info2;
637 if (info1->state_flags != info2->state_flags)
640 if (info1->junction_sides != info2->junction_sides)
643 if (info1->style_classes->len != info2->style_classes->len)
646 if (memcmp (info1->style_classes->data,
647 info2->style_classes->data,
648 info1->style_classes->len * sizeof (GQuark)) != 0)
651 if (info1->regions->len != info2->regions->len)
654 if (memcmp (info1->regions->data,
655 info2->regions->data,
656 info1->regions->len * sizeof (GtkRegion)) != 0)
663 gtk_style_context_cascade_changed (GtkStyleCascade *cascade,
664 GtkStyleContext *context)
666 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_SOURCE);
670 gtk_style_context_set_cascade (GtkStyleContext *context,
671 GtkStyleCascade *cascade)
673 GtkStyleContextPrivate *priv;
675 priv = context->priv;
677 if (priv->cascade == cascade)
682 g_object_ref (cascade);
683 g_signal_connect (cascade,
684 "-gtk-private-changed",
685 G_CALLBACK (gtk_style_context_cascade_changed),
691 g_signal_handlers_disconnect_by_func (priv->cascade,
692 gtk_style_context_cascade_changed,
694 g_object_unref (priv->cascade);
697 priv->cascade = cascade;
700 gtk_style_context_cascade_changed (cascade, context);
704 gtk_style_context_init (GtkStyleContext *style_context)
706 GtkStyleContextPrivate *priv;
708 priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
709 GTK_TYPE_STYLE_CONTEXT,
710 GtkStyleContextPrivate);
712 priv->style_data = g_hash_table_new_full (style_info_hash,
714 (GDestroyNotify) style_info_free,
715 (GDestroyNotify) style_data_unref);
717 priv->direction = GTK_TEXT_DIR_LTR;
719 priv->screen = gdk_screen_get_default ();
720 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
722 /* Create default info store */
723 priv->info = style_info_new ();
725 gtk_style_context_set_cascade (style_context,
726 _gtk_style_cascade_get_for_screen (priv->screen));
730 gtk_style_context_do_animations (gpointer unused)
732 GtkStyleContext *context;
734 for (context = _running_animations;
736 context = context->priv->animation_list_next)
738 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANIMATE);
745 gtk_style_context_is_animating (GtkStyleContext *context)
747 GtkStyleContextPrivate *priv = context->priv;
749 return priv->animation_list_prev != NULL
750 || _running_animations == context;
754 gtk_style_context_stop_animating (GtkStyleContext *context)
756 GtkStyleContextPrivate *priv = context->priv;
758 if (!gtk_style_context_is_animating (context))
761 if (priv->animation_list_prev == NULL)
763 _running_animations = priv->animation_list_next;
765 if (_running_animations == NULL)
767 /* we were the last animation */
768 g_source_remove (_running_animations_timer_id);
769 _running_animations_timer_id = 0;
773 priv->animation_list_prev->priv->animation_list_next = priv->animation_list_next;
775 if (priv->animation_list_next)
776 priv->animation_list_next->priv->animation_list_prev = priv->animation_list_prev;
778 priv->animation_list_next = NULL;
779 priv->animation_list_prev = NULL;
783 gtk_style_context_start_animating (GtkStyleContext *context)
785 GtkStyleContextPrivate *priv = context->priv;
787 if (gtk_style_context_is_animating (context))
790 if (_running_animations == NULL)
792 _running_animations_timer_id = gdk_threads_add_timeout (25,
793 gtk_style_context_do_animations,
795 _running_animations = context;
799 priv->animation_list_next = _running_animations;
800 _running_animations->priv->animation_list_prev = context;
801 _running_animations = context;
806 gtk_style_context_finalize (GObject *object)
808 GtkStyleContextPrivate *priv;
809 GtkStyleContext *style_context;
811 style_context = GTK_STYLE_CONTEXT (object);
812 priv = style_context->priv;
814 _gtk_style_context_stop_animations (style_context);
816 /* children hold a reference to us */
817 g_assert (priv->children == NULL);
819 gtk_style_context_set_parent (style_context, NULL);
821 gtk_style_context_set_cascade (style_context, NULL);
823 if (priv->widget_path)
824 gtk_widget_path_free (priv->widget_path);
826 g_hash_table_destroy (priv->style_data);
829 priv->info = style_info_pop (priv->info);
831 G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
835 gtk_style_context_impl_set_property (GObject *object,
840 GtkStyleContext *style_context;
842 style_context = GTK_STYLE_CONTEXT (object);
847 gtk_style_context_set_screen (style_context,
848 g_value_get_object (value));
851 gtk_style_context_set_direction (style_context,
852 g_value_get_enum (value));
855 gtk_style_context_set_parent (style_context,
856 g_value_get_object (value));
859 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
865 gtk_style_context_impl_get_property (GObject *object,
870 GtkStyleContext *style_context;
871 GtkStyleContextPrivate *priv;
873 style_context = GTK_STYLE_CONTEXT (object);
874 priv = style_context->priv;
879 g_value_set_object (value, priv->screen);
882 g_value_set_enum (value, priv->direction);
885 g_value_set_object (value, priv->parent);
888 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
893 static GtkWidgetPath *
894 create_query_path (GtkStyleContext *context)
896 GtkStyleContextPrivate *priv;
901 priv = context->priv;
902 path = priv->widget ? _gtk_widget_create_path (priv->widget) : gtk_widget_path_copy (priv->widget_path);
903 pos = gtk_widget_path_length (path) - 1;
907 /* Set widget regions */
908 for (i = 0; i < info->regions->len; i++)
912 region = &g_array_index (info->regions, GtkRegion, i);
913 gtk_widget_path_iter_add_region (path, pos,
914 g_quark_to_string (region->class_quark),
918 /* Set widget classes */
919 for (i = 0; i < info->style_classes->len; i++)
923 quark = g_array_index (info->style_classes, GQuark, i);
924 gtk_widget_path_iter_add_class (path, pos,
925 g_quark_to_string (quark));
932 build_properties (GtkStyleContext *context,
933 GtkCssComputedValues *values,
935 const GtkBitmask *relevant_changes)
937 GtkStyleContextPrivate *priv;
938 GtkCssMatcher matcher;
940 GtkCssLookup *lookup;
942 priv = context->priv;
944 path = create_query_path (context);
945 lookup = _gtk_css_lookup_new (relevant_changes);
947 if (_gtk_css_matcher_init (&matcher, path, state))
948 _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
952 _gtk_css_lookup_resolve (lookup, context, values);
954 _gtk_css_lookup_free (lookup);
955 gtk_widget_path_free (path);
959 style_data_lookup (GtkStyleContext *context)
961 GtkStyleContextPrivate *priv;
965 priv = context->priv;
968 /* Current data in use is cached, just return it */
972 g_assert (priv->widget != NULL || priv->widget_path != NULL);
974 data = g_hash_table_lookup (priv->style_data, info);
977 style_info_set_data (info, data);
981 data = style_data_new ();
982 data->store = _gtk_css_computed_values_new ();
983 style_info_set_data (info, data);
984 g_hash_table_insert (priv->style_data,
985 style_info_copy (info),
988 build_properties (context, data->store, info->state_flags, NULL);
994 style_data_lookup_for_state (GtkStyleContext *context,
999 if (context->priv->info->state_flags == state)
1000 return style_data_lookup (context);
1002 gtk_style_context_save (context);
1003 gtk_style_context_set_state (context, state);
1004 data = style_data_lookup (context);
1005 gtk_style_context_restore (context);
1011 gtk_style_context_set_invalid (GtkStyleContext *context,
1014 GtkStyleContextPrivate *priv;
1016 priv = context->priv;
1018 if (priv->invalid == invalid)
1021 priv->invalid = invalid;
1026 gtk_style_context_set_invalid (priv->parent, TRUE);
1027 else if (GTK_IS_RESIZE_CONTAINER (priv->widget))
1028 _gtk_container_queue_restyle (GTK_CONTAINER (priv->widget));
1032 /* returns TRUE if someone called gtk_style_context_save() but hasn't
1033 * called gtk_style_context_restore() yet.
1034 * In those situations we don't invalidate the context when somebody
1035 * changes state/regions/classes.
1038 gtk_style_context_is_saved (GtkStyleContext *context)
1040 return context->priv->info->next != NULL;
1044 gtk_style_context_queue_invalidate_internal (GtkStyleContext *context,
1045 GtkCssChange change)
1047 GtkStyleContextPrivate *priv = context->priv;
1048 GtkStyleInfo *info = priv->info;
1050 if (gtk_style_context_is_saved (context))
1052 style_info_set_data (info, NULL);
1056 _gtk_style_context_queue_invalidate (context, change);
1057 /* XXX: We need to invalidate siblings here somehow */
1062 * gtk_style_context_new:
1064 * Creates a standalone #GtkStyleContext, this style context
1065 * won't be attached to any widget, so you may want
1066 * to call gtk_style_context_set_path() yourself.
1069 * This function is only useful when using the theming layer
1070 * separated from GTK+, if you are using #GtkStyleContext to
1071 * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1072 * in order to get a style context ready to theme the widget.
1075 * Returns: A newly created #GtkStyleContext.
1078 gtk_style_context_new (void)
1080 return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1084 _gtk_style_context_set_widget (GtkStyleContext *context,
1087 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1088 g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1090 context->priv->widget = widget;
1092 _gtk_style_context_stop_animations (context);
1094 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_SELF);
1098 * gtk_style_context_add_provider:
1099 * @context: a #GtkStyleContext
1100 * @provider: a #GtkStyleProvider
1101 * @priority: the priority of the style provider. The lower
1102 * it is, the earlier it will be used in the style
1103 * construction. Typically this will be in the range
1104 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1105 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1107 * Adds a style provider to @context, to be used in style construction.
1108 * Note that a style provider added by this function only affects
1109 * the style of the widget to which @context belongs. If you want
1110 * to affect the style of all widgets, use
1111 * gtk_style_context_add_provider_for_screen().
1113 * <note><para>If both priorities are the same, A #GtkStyleProvider
1114 * added through this function takes precedence over another added
1115 * through gtk_style_context_add_provider_for_screen().</para></note>
1120 gtk_style_context_add_provider (GtkStyleContext *context,
1121 GtkStyleProvider *provider,
1124 GtkStyleContextPrivate *priv;
1126 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1127 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1129 priv = context->priv;
1131 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1133 GtkStyleCascade *new_cascade;
1135 new_cascade = _gtk_style_cascade_new ();
1136 _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1137 _gtk_style_cascade_add_provider (new_cascade, provider, priority);
1138 gtk_style_context_set_cascade (context, new_cascade);
1139 g_object_unref (new_cascade);
1143 _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1148 * gtk_style_context_remove_provider:
1149 * @context: a #GtkStyleContext
1150 * @provider: a #GtkStyleProvider
1152 * Removes @provider from the style providers list in @context.
1157 gtk_style_context_remove_provider (GtkStyleContext *context,
1158 GtkStyleProvider *provider)
1160 GtkStyleContextPrivate *priv;
1162 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1163 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1165 priv = context->priv;
1167 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1170 _gtk_style_cascade_remove_provider (priv->cascade, provider);
1174 * gtk_style_context_reset_widgets:
1175 * @screen: a #GdkScreen
1177 * This function recomputes the styles for all widgets under a particular
1178 * #GdkScreen. This is useful when some global parameter has changed that
1179 * affects the appearance of all widgets, because when a widget gets a new
1180 * style, it will both redraw and recompute any cached information about
1181 * its appearance. As an example, it is used when the color scheme changes
1182 * in the related #GtkSettings object.
1187 gtk_style_context_reset_widgets (GdkScreen *screen)
1189 GList *list, *toplevels;
1191 _gtk_icon_set_invalidate_caches ();
1193 toplevels = gtk_window_list_toplevels ();
1194 g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1196 for (list = toplevels; list; list = list->next)
1198 if (gtk_widget_get_screen (list->data) == screen)
1199 gtk_widget_reset_style (list->data);
1201 g_object_unref (list->data);
1204 g_list_free (toplevels);
1208 * gtk_style_context_add_provider_for_screen:
1209 * @screen: a #GdkScreen
1210 * @provider: a #GtkStyleProvider
1211 * @priority: the priority of the style provider. The lower
1212 * it is, the earlier it will be used in the style
1213 * construction. Typically this will be in the range
1214 * between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1215 * %GTK_STYLE_PROVIDER_PRIORITY_USER
1217 * Adds a global style provider to @screen, which will be used
1218 * in style construction for all #GtkStyleContexts under @screen.
1220 * GTK+ uses this to make styling information from #GtkSettings
1223 * <note><para>If both priorities are the same, A #GtkStyleProvider
1224 * added through gtk_style_context_add_provider() takes precedence
1225 * over another added through this function.</para></note>
1230 gtk_style_context_add_provider_for_screen (GdkScreen *screen,
1231 GtkStyleProvider *provider,
1234 GtkStyleCascade *cascade;
1236 g_return_if_fail (GDK_IS_SCREEN (screen));
1237 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1238 g_return_if_fail (!GTK_IS_SETTINGS (provider) || _gtk_settings_get_screen (GTK_SETTINGS (provider)) == screen);
1240 cascade = _gtk_style_cascade_get_for_screen (screen);
1241 _gtk_style_cascade_add_provider (cascade, provider, priority);
1245 * gtk_style_context_remove_provider_for_screen:
1246 * @screen: a #GdkScreen
1247 * @provider: a #GtkStyleProvider
1249 * Removes @provider from the global style providers list in @screen.
1254 gtk_style_context_remove_provider_for_screen (GdkScreen *screen,
1255 GtkStyleProvider *provider)
1257 GtkStyleCascade *cascade;
1259 g_return_if_fail (GDK_IS_SCREEN (screen));
1260 g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1261 g_return_if_fail (!GTK_IS_SETTINGS (provider));
1263 cascade = _gtk_style_cascade_get_for_screen (screen);
1264 _gtk_style_cascade_remove_provider (cascade, provider);
1268 * gtk_style_context_get_section:
1269 * @context: a #GtkStyleContext
1270 * @property: style property name
1272 * Queries the location in the CSS where @property was defined for the
1273 * current @context. Note that the state to be queried is taken from
1274 * gtk_style_context_get_state().
1276 * If the location is not available, %NULL will be returned. The
1277 * location might not be available for various reasons, such as the
1278 * property being overridden, @property not naming a supported CSS
1279 * property or tracking of definitions being disabled for performance
1282 * Shorthand CSS properties cannot be queried for a location and will
1283 * always return %NULL.
1285 * Returns: %NULL or the section where value was defined
1288 gtk_style_context_get_section (GtkStyleContext *context,
1289 const gchar *property)
1291 GtkStyleContextPrivate *priv;
1292 GtkStyleProperty *prop;
1295 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1296 g_return_val_if_fail (property != NULL, NULL);
1298 priv = context->priv;
1299 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1301 prop = _gtk_style_property_lookup (property);
1302 if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1305 data = style_data_lookup (context);
1306 return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1309 static GtkCssValue *
1310 gtk_style_context_query_func (guint id,
1313 return _gtk_css_computed_values_get_value (values, id);
1317 * gtk_style_context_get_property:
1318 * @context: a #GtkStyleContext
1319 * @property: style property name
1320 * @state: state to retrieve the property value for
1321 * @value: (out) (transfer full): return location for the style property value
1323 * Gets a style property from @context for the given state.
1325 * When @value is no longer needed, g_value_unset() must be called
1326 * to free any allocated memory.
1331 gtk_style_context_get_property (GtkStyleContext *context,
1332 const gchar *property,
1333 GtkStateFlags state,
1336 GtkStyleContextPrivate *priv;
1337 GtkStyleProperty *prop;
1340 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1341 g_return_if_fail (property != NULL);
1342 g_return_if_fail (value != NULL);
1344 priv = context->priv;
1345 g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1347 prop = _gtk_style_property_lookup (property);
1350 g_warning ("Style property \"%s\" is not registered", property);
1353 if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1355 g_warning ("Style property \"%s\" is not gettable", property);
1359 data = style_data_lookup_for_state (context, state);
1360 _gtk_style_property_query (prop, value, gtk_style_context_query_func, data->store);
1364 * gtk_style_context_get_valist:
1365 * @context: a #GtkStyleContext
1366 * @state: state to retrieve the property values for
1367 * @args: va_list of property name/return location pairs, followed by %NULL
1369 * Retrieves several style property values from @context for a given state.
1374 gtk_style_context_get_valist (GtkStyleContext *context,
1375 GtkStateFlags state,
1378 const gchar *property_name;
1380 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1382 property_name = va_arg (args, const gchar *);
1384 while (property_name)
1386 gchar *error = NULL;
1387 GValue value = G_VALUE_INIT;
1389 gtk_style_context_get_property (context,
1394 G_VALUE_LCOPY (&value, args, 0, &error);
1395 g_value_unset (&value);
1399 g_warning ("Could not get style property \"%s\": %s", property_name, error);
1404 property_name = va_arg (args, const gchar *);
1409 * gtk_style_context_get:
1410 * @context: a #GtkStyleContext
1411 * @state: state to retrieve the property values for
1412 * @...: property name /return value pairs, followed by %NULL
1414 * Retrieves several style property values from @context for a
1420 gtk_style_context_get (GtkStyleContext *context,
1421 GtkStateFlags state,
1426 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1428 va_start (args, state);
1429 gtk_style_context_get_valist (context, state, args);
1434 * gtk_style_context_set_state:
1435 * @context: a #GtkStyleContext
1436 * @flags: state to represent
1438 * Sets the state to be used when rendering with any
1439 * of the gtk_render_*() functions.
1444 gtk_style_context_set_state (GtkStyleContext *context,
1445 GtkStateFlags flags)
1447 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1449 context->priv->info->state_flags = flags;
1451 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_STATE);
1455 * gtk_style_context_get_state:
1456 * @context: a #GtkStyleContext
1458 * Returns the state used when rendering.
1460 * Returns: the state flags
1465 gtk_style_context_get_state (GtkStyleContext *context)
1467 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1469 return context->priv->info->state_flags;
1473 * gtk_style_context_state_is_running:
1474 * @context: a #GtkStyleContext
1475 * @state: a widget state
1476 * @progress: (out): return location for the transition progress
1478 * Returns %TRUE if there is a transition animation running for the
1479 * current region (see gtk_style_context_push_animatable_region()).
1481 * If @progress is not %NULL, the animation progress will be returned
1482 * there, 0.0 means the state is closest to being unset, while 1.0 means
1483 * it's closest to being set. This means transition animation will
1484 * run from 0 to 1 when @state is being set and from 1 to 0 when
1487 * Returns: %TRUE if there is a running transition animation for @state.
1491 * Deprecated: 3.6: This function always returns %FALSE
1494 gtk_style_context_state_is_running (GtkStyleContext *context,
1498 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1504 * gtk_style_context_set_path:
1505 * @context: a #GtkStyleContext
1506 * @path: a #GtkWidgetPath
1508 * Sets the #GtkWidgetPath used for style matching. As a
1509 * consequence, the style will be regenerated to match
1510 * the new given path.
1512 * If you are using a #GtkStyleContext returned from
1513 * gtk_widget_get_style_context(), you do not need to call
1519 gtk_style_context_set_path (GtkStyleContext *context,
1520 GtkWidgetPath *path)
1522 GtkStyleContextPrivate *priv;
1524 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1525 g_return_if_fail (path != NULL);
1527 priv = context->priv;
1528 g_return_if_fail (priv->widget == NULL);
1530 if (priv->widget_path)
1532 gtk_widget_path_free (priv->widget_path);
1533 priv->widget_path = NULL;
1537 priv->widget_path = gtk_widget_path_copy (path);
1539 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY);
1543 * gtk_style_context_get_path:
1544 * @context: a #GtkStyleContext
1546 * Returns the widget path used for style matching.
1548 * Returns: (transfer none): A #GtkWidgetPath
1552 const GtkWidgetPath *
1553 gtk_style_context_get_path (GtkStyleContext *context)
1555 GtkStyleContextPrivate *priv;
1557 priv = context->priv;
1559 return gtk_widget_get_path (priv->widget);
1561 return priv->widget_path;
1565 * gtk_style_context_set_parent:
1566 * @context: a #GtkStyleContext
1567 * @parent: (allow-none): the new parent or %NULL
1569 * Sets the parent style context for @context. The parent style
1570 * context is used to implement
1571 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1574 * If you are using a #GtkStyleContext returned from
1575 * gtk_widget_get_style_context(), the parent will be set for you.
1580 gtk_style_context_set_parent (GtkStyleContext *context,
1581 GtkStyleContext *parent)
1583 GtkStyleContextPrivate *priv;
1585 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1586 g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1588 priv = context->priv;
1590 if (priv->parent == parent)
1595 parent->priv->children = g_slist_prepend (parent->priv->children, context);
1596 g_object_ref (parent);
1598 gtk_style_context_set_invalid (parent, TRUE);
1603 priv->parent->priv->children = g_slist_remove (priv->parent->priv->children, context);
1604 g_object_unref (priv->parent);
1607 priv->parent = parent;
1609 g_object_notify (G_OBJECT (context), "parent");
1610 _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
1614 * gtk_style_context_get_parent:
1615 * @context: a #GtkStyleContext
1617 * Gets the parent context set via gtk_style_context_set_parent().
1618 * See that function for details.
1620 * Returns: (transfer none): the parent context or %NULL
1625 gtk_style_context_get_parent (GtkStyleContext *context)
1627 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1629 return context->priv->parent;
1633 * gtk_style_context_save:
1634 * @context: a #GtkStyleContext
1636 * Saves the @context state, so all modifications done through
1637 * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1638 * gtk_style_context_add_region(), gtk_style_context_remove_region()
1639 * or gtk_style_context_set_junction_sides() can be reverted in one
1640 * go through gtk_style_context_restore().
1645 gtk_style_context_save (GtkStyleContext *context)
1647 GtkStyleContextPrivate *priv;
1649 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1651 priv = context->priv;
1653 priv->info = style_info_copy (priv->info);
1654 /* Need to unset animations here because we can not know what style
1655 * class potential transitions came from once we save().
1657 if (priv->info->data && style_data_is_animating (priv->info->data))
1658 style_info_set_data (priv->info, NULL);
1662 * gtk_style_context_restore:
1663 * @context: a #GtkStyleContext
1665 * Restores @context state to a previous stage.
1666 * See gtk_style_context_save().
1671 gtk_style_context_restore (GtkStyleContext *context)
1673 GtkStyleContextPrivate *priv;
1675 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1677 priv = context->priv;
1679 priv->info = style_info_pop (priv->info);
1683 g_warning ("Unpaired gtk_style_context_restore() call");
1685 /* Create default region */
1686 priv->info = style_info_new ();
1691 style_class_find (GArray *array,
1696 gboolean found = FALSE;
1702 if (!array || array->len == 0)
1706 max = array->len - 1;
1712 mid = (min + max) / 2;
1713 item = g_array_index (array, GQuark, mid);
1715 if (class_quark == item)
1720 else if (class_quark > item)
1721 min = pos = mid + 1;
1728 while (!found && min <= max);
1737 region_find (GArray *array,
1742 gboolean found = FALSE;
1748 if (!array || array->len == 0)
1752 max = array->len - 1;
1758 mid = (min + max) / 2;
1759 region = &g_array_index (array, GtkRegion, mid);
1761 if (region->class_quark == class_quark)
1766 else if (region->class_quark > class_quark)
1767 min = pos = mid + 1;
1774 while (!found && min <= max);
1783 * gtk_style_context_add_class:
1784 * @context: a #GtkStyleContext
1785 * @class_name: class name to use in styling
1787 * Adds a style class to @context, so posterior calls to
1788 * gtk_style_context_get() or any of the gtk_render_*()
1789 * functions will make use of this new class for styling.
1791 * In the CSS file format, a #GtkEntry defining an "entry"
1792 * class, would be matched by:
1795 * GtkEntry.entry { ... }
1798 * While any widget defining an "entry" class would be
1807 gtk_style_context_add_class (GtkStyleContext *context,
1808 const gchar *class_name)
1810 GtkStyleContextPrivate *priv;
1815 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1816 g_return_if_fail (class_name != NULL);
1818 priv = context->priv;
1819 class_quark = g_quark_from_string (class_name);
1823 if (!style_class_find (info->style_classes, class_quark, &position))
1825 g_array_insert_val (info->style_classes, position, class_quark);
1827 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1832 * gtk_style_context_remove_class:
1833 * @context: a #GtkStyleContext
1834 * @class_name: class name to remove
1836 * Removes @class_name from @context.
1841 gtk_style_context_remove_class (GtkStyleContext *context,
1842 const gchar *class_name)
1844 GtkStyleContextPrivate *priv;
1849 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1850 g_return_if_fail (class_name != NULL);
1852 class_quark = g_quark_try_string (class_name);
1857 priv = context->priv;
1861 if (style_class_find (info->style_classes, class_quark, &position))
1863 g_array_remove_index (info->style_classes, position);
1865 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1870 * gtk_style_context_has_class:
1871 * @context: a #GtkStyleContext
1872 * @class_name: a class name
1874 * Returns %TRUE if @context currently has defined the
1877 * Returns: %TRUE if @context has @class_name defined
1882 gtk_style_context_has_class (GtkStyleContext *context,
1883 const gchar *class_name)
1885 GtkStyleContextPrivate *priv;
1889 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1890 g_return_val_if_fail (class_name != NULL, FALSE);
1892 class_quark = g_quark_try_string (class_name);
1897 priv = context->priv;
1901 if (style_class_find (info->style_classes, class_quark, NULL))
1908 * gtk_style_context_list_classes:
1909 * @context: a #GtkStyleContext
1911 * Returns the list of classes currently defined in @context.
1913 * Returns: (transfer container) (element-type utf8): a #GList of
1914 * strings with the currently defined classes. The contents
1915 * of the list are owned by GTK+, but you must free the list
1916 * itself with g_list_free() when you are done with it.
1921 gtk_style_context_list_classes (GtkStyleContext *context)
1923 GtkStyleContextPrivate *priv;
1925 GList *classes = NULL;
1928 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1930 priv = context->priv;
1934 for (i = 0; i < info->style_classes->len; i++)
1938 quark = g_array_index (info->style_classes, GQuark, i);
1939 classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1946 * gtk_style_context_list_regions:
1947 * @context: a #GtkStyleContext
1949 * Returns the list of regions currently defined in @context.
1951 * Returns: (transfer container) (element-type utf8): a #GList of
1952 * strings with the currently defined regions. The contents
1953 * of the list are owned by GTK+, but you must free the list
1954 * itself with g_list_free() when you are done with it.
1959 gtk_style_context_list_regions (GtkStyleContext *context)
1961 GtkStyleContextPrivate *priv;
1963 GList *classes = NULL;
1966 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1968 priv = context->priv;
1972 for (i = 0; i < info->regions->len; i++)
1975 const gchar *class_name;
1977 region = &g_array_index (info->regions, GtkRegion, i);
1979 class_name = g_quark_to_string (region->class_quark);
1980 classes = g_list_prepend (classes, (gchar *) class_name);
1987 _gtk_style_context_check_region_name (const gchar *str)
1989 g_return_val_if_fail (str != NULL, FALSE);
1991 if (!g_ascii_islower (str[0]))
1997 !g_ascii_islower (*str))
2007 * gtk_style_context_add_region:
2008 * @context: a #GtkStyleContext
2009 * @region_name: region name to use in styling
2010 * @flags: flags that apply to the region
2012 * Adds a region to @context, so posterior calls to
2013 * gtk_style_context_get() or any of the gtk_render_*()
2014 * functions will make use of this new region for styling.
2016 * In the CSS file format, a #GtkTreeView defining a "row"
2017 * region, would be matched by:
2020 * GtkTreeView row { ... }
2023 * Pseudo-classes are used for matching @flags, so the two
2026 * GtkTreeView row:nth-child(even) { ... }
2027 * GtkTreeView row:nth-child(odd) { ... }
2030 * would apply to even and odd rows, respectively.
2032 * <note><para>Region names must only contain lowercase letters
2033 * and '-', starting always with a lowercase letter.</para></note>
2038 gtk_style_context_add_region (GtkStyleContext *context,
2039 const gchar *region_name,
2040 GtkRegionFlags flags)
2042 GtkStyleContextPrivate *priv;
2044 GQuark region_quark;
2047 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2048 g_return_if_fail (region_name != NULL);
2049 g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2051 priv = context->priv;
2052 region_quark = g_quark_from_string (region_name);
2056 if (!region_find (info->regions, region_quark, &position))
2060 region.class_quark = region_quark;
2061 region.flags = flags;
2063 g_array_insert_val (info->regions, position, region);
2065 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2070 * gtk_style_context_remove_region:
2071 * @context: a #GtkStyleContext
2072 * @region_name: region name to unset
2074 * Removes a region from @context.
2079 gtk_style_context_remove_region (GtkStyleContext *context,
2080 const gchar *region_name)
2082 GtkStyleContextPrivate *priv;
2084 GQuark region_quark;
2087 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2088 g_return_if_fail (region_name != NULL);
2090 region_quark = g_quark_try_string (region_name);
2095 priv = context->priv;
2099 if (region_find (info->regions, region_quark, &position))
2101 g_array_remove_index (info->regions, position);
2103 gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2108 * gtk_style_context_has_region:
2109 * @context: a #GtkStyleContext
2110 * @region_name: a region name
2111 * @flags_return: (out) (allow-none): return location for region flags
2113 * Returns %TRUE if @context has the region defined.
2114 * If @flags_return is not %NULL, it is set to the flags
2115 * affecting the region.
2117 * Returns: %TRUE if region is defined
2122 gtk_style_context_has_region (GtkStyleContext *context,
2123 const gchar *region_name,
2124 GtkRegionFlags *flags_return)
2126 GtkStyleContextPrivate *priv;
2128 GQuark region_quark;
2131 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2132 g_return_val_if_fail (region_name != NULL, FALSE);
2137 region_quark = g_quark_try_string (region_name);
2142 priv = context->priv;
2146 if (region_find (info->regions, region_quark, &position))
2152 region = &g_array_index (info->regions, GtkRegion, position);
2153 *flags_return = region->flags;
2162 style_property_values_cmp (gconstpointer bsearch_node1,
2163 gconstpointer bsearch_node2)
2165 const PropertyValue *val1 = bsearch_node1;
2166 const PropertyValue *val2 = bsearch_node2;
2168 if (val1->widget_type != val2->widget_type)
2169 return val1->widget_type < val2->widget_type ? -1 : 1;
2171 if (val1->pspec != val2->pspec)
2172 return val1->pspec < val2->pspec ? -1 : 1;
2174 if (val1->state != val2->state)
2175 return val1->state < val2->state ? -1 : 1;
2181 _gtk_style_context_peek_property (GtkStyleContext *context,
2184 StyleData *data = style_data_lookup (context);
2186 return _gtk_css_computed_values_get_value (data->store, property_id);
2190 _gtk_style_context_get_number (GtkStyleContext *context,
2192 double one_hundred_percent)
2196 value = _gtk_style_context_peek_property (context, property_id);
2197 return _gtk_css_number_value_get (value, one_hundred_percent);
2201 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2203 GtkStateFlags state,
2206 GtkStyleContextPrivate *priv;
2207 PropertyValue *pcache, key = { 0 };
2211 priv = context->priv;
2213 data = style_data_lookup_for_state (context, state);
2215 key.widget_type = widget_type;
2219 /* need value cache array */
2220 if (!data->property_cache)
2221 data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2224 pcache = bsearch (&key,
2225 data->property_cache->data, data->property_cache->len,
2226 sizeof (PropertyValue), style_property_values_cmp);
2228 return &pcache->value;
2232 while (i < data->property_cache->len &&
2233 style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2236 g_array_insert_val (data->property_cache, i, key);
2237 pcache = &g_array_index (data->property_cache, PropertyValue, i);
2239 /* cache miss, initialize value type, then set contents */
2240 g_param_spec_ref (pcache->pspec);
2241 g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2243 if (priv->widget || priv->widget_path)
2245 GtkWidgetPath *widget_path = priv->widget ? _gtk_widget_create_path (priv->widget) : priv->widget_path;
2247 if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2249 state, pspec, &pcache->value))
2251 /* Resolve symbolic colors to GdkColor/GdkRGBA */
2252 if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2254 GtkSymbolicColor *color;
2257 color = g_value_dup_boxed (&pcache->value);
2259 g_value_unset (&pcache->value);
2261 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2262 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2264 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2266 if (_gtk_style_context_resolve_color (context, color, &rgba, NULL))
2268 if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2269 g_value_set_boxed (&pcache->value, &rgba);
2274 rgb.red = rgba.red * 65535. + 0.5;
2275 rgb.green = rgba.green * 65535. + 0.5;
2276 rgb.blue = rgba.blue * 65535. + 0.5;
2278 g_value_set_boxed (&pcache->value, &rgb);
2282 g_param_value_set_default (pspec, &pcache->value);
2284 gtk_symbolic_color_unref (color);
2288 gtk_widget_path_free (widget_path);
2290 return &pcache->value;
2294 gtk_widget_path_free (widget_path);
2297 /* not supplied by any provider, revert to default */
2298 g_param_value_set_default (pspec, &pcache->value);
2300 return &pcache->value;
2304 * gtk_style_context_get_style_property:
2305 * @context: a #GtkStyleContext
2306 * @property_name: the name of the widget style property
2307 * @value: Return location for the property value
2309 * Gets the value for a widget style property.
2311 * When @value is no longer needed, g_value_unset() must be called
2312 * to free any allocated memory.
2315 gtk_style_context_get_style_property (GtkStyleContext *context,
2316 const gchar *property_name,
2319 GtkStyleContextPrivate *priv;
2320 GtkWidgetClass *widget_class;
2321 GtkStateFlags state;
2323 const GValue *peek_value;
2326 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2327 g_return_if_fail (property_name != NULL);
2328 g_return_if_fail (value != NULL);
2330 priv = context->priv;
2334 widget_type = G_OBJECT_TYPE (priv->widget);
2338 if (!priv->widget_path)
2341 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2343 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2345 g_warning ("%s: can't get style properties for non-widget class `%s'",
2347 g_type_name (widget_type));
2352 widget_class = g_type_class_ref (widget_type);
2353 pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2354 g_type_class_unref (widget_class);
2358 g_warning ("%s: widget class `%s' has no style property named `%s'",
2360 g_type_name (widget_type),
2365 state = gtk_style_context_get_state (context);
2366 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2369 if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2370 g_value_copy (peek_value, value);
2371 else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2372 g_value_transform (peek_value, value);
2374 g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2376 G_VALUE_TYPE_NAME (peek_value),
2377 G_VALUE_TYPE_NAME (value));
2381 * gtk_style_context_get_style_valist:
2382 * @context: a #GtkStyleContext
2383 * @args: va_list of property name/return location pairs, followed by %NULL
2385 * Retrieves several widget style properties from @context according to the
2391 gtk_style_context_get_style_valist (GtkStyleContext *context,
2394 GtkStyleContextPrivate *priv;
2395 const gchar *prop_name;
2396 GtkStateFlags state;
2399 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2401 prop_name = va_arg (args, const gchar *);
2402 priv = context->priv;
2406 widget_type = G_OBJECT_TYPE (priv->widget);
2410 if (!priv->widget_path)
2413 widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2415 if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2417 g_warning ("%s: can't get style properties for non-widget class `%s'",
2419 g_type_name (widget_type));
2424 state = gtk_style_context_get_state (context);
2428 GtkWidgetClass *widget_class;
2430 const GValue *peek_value;
2433 widget_class = g_type_class_ref (widget_type);
2434 pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2435 g_type_class_unref (widget_class);
2439 g_warning ("%s: widget class `%s' has no style property named `%s'",
2441 g_type_name (widget_type),
2446 peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2449 G_VALUE_LCOPY (peek_value, args, 0, &error);
2453 g_warning ("can't retrieve style property `%s' of type `%s': %s",
2455 G_VALUE_TYPE_NAME (peek_value),
2461 prop_name = va_arg (args, const gchar *);
2466 * gtk_style_context_get_style:
2467 * @context: a #GtkStyleContext
2468 * @...: property name /return value pairs, followed by %NULL
2470 * Retrieves several widget style properties from @context according to the
2476 gtk_style_context_get_style (GtkStyleContext *context,
2481 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2483 va_start (args, context);
2484 gtk_style_context_get_style_valist (context, args);
2490 * gtk_style_context_lookup_icon_set:
2491 * @context: a #GtkStyleContext
2492 * @stock_id: an icon name
2494 * Looks up @stock_id in the icon factories associated to @context and
2495 * the default icon factory, returning an icon set if found, otherwise
2498 * Returns: (transfer none): The looked up %GtkIconSet, or %NULL
2501 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2502 const gchar *stock_id)
2504 GtkStyleContextPrivate *priv;
2506 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2507 g_return_val_if_fail (stock_id != NULL, NULL);
2509 priv = context->priv;
2510 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2512 return gtk_icon_factory_lookup_default (stock_id);
2516 * gtk_style_context_set_screen:
2517 * @context: a #GtkStyleContext
2518 * @screen: a #GdkScreen
2520 * Attaches @context to the given screen.
2522 * The screen is used to add style information from 'global' style
2523 * providers, such as the screens #GtkSettings instance.
2525 * If you are using a #GtkStyleContext returned from
2526 * gtk_widget_get_style_context(), you do not need to
2527 * call this yourself.
2532 gtk_style_context_set_screen (GtkStyleContext *context,
2535 GtkStyleContextPrivate *priv;
2537 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2538 g_return_if_fail (GDK_IS_SCREEN (screen));
2540 priv = context->priv;
2541 if (priv->screen == screen)
2544 if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2546 gtk_style_context_set_cascade (context, _gtk_style_cascade_get_for_screen (screen));
2550 _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2553 priv->screen = screen;
2555 g_object_notify (G_OBJECT (context), "screen");
2559 * gtk_style_context_get_screen:
2560 * @context: a #GtkStyleContext
2562 * Returns the #GdkScreen to which @context is attached.
2564 * Returns: (transfer none): a #GdkScreen.
2567 gtk_style_context_get_screen (GtkStyleContext *context)
2569 GtkStyleContextPrivate *priv;
2571 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2573 priv = context->priv;
2574 return priv->screen;
2578 * gtk_style_context_set_direction:
2579 * @context: a #GtkStyleContext
2580 * @direction: the new direction.
2582 * Sets the reading direction for rendering purposes.
2584 * If you are using a #GtkStyleContext returned from
2585 * gtk_widget_get_style_context(), you do not need to
2586 * call this yourself.
2591 gtk_style_context_set_direction (GtkStyleContext *context,
2592 GtkTextDirection direction)
2594 GtkStyleContextPrivate *priv;
2596 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2598 priv = context->priv;
2599 priv->direction = direction;
2601 g_object_notify (G_OBJECT (context), "direction");
2605 * gtk_style_context_get_direction:
2606 * @context: a #GtkStyleContext
2608 * Returns the widget direction used for rendering.
2610 * Returns: the widget direction
2615 gtk_style_context_get_direction (GtkStyleContext *context)
2617 GtkStyleContextPrivate *priv;
2619 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2621 priv = context->priv;
2622 return priv->direction;
2626 * gtk_style_context_set_junction_sides:
2627 * @context: a #GtkStyleContext
2628 * @sides: sides where rendered elements are visually connected to
2631 * Sets the sides where rendered elements (mostly through
2632 * gtk_render_frame()) will visually connect with other visual elements.
2634 * This is merely a hint that may or may not be honored
2635 * by theming engines.
2637 * Container widgets are expected to set junction hints as appropriate
2638 * for their children, so it should not normally be necessary to call
2639 * this function manually.
2644 gtk_style_context_set_junction_sides (GtkStyleContext *context,
2645 GtkJunctionSides sides)
2647 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2649 context->priv->info->junction_sides = sides;
2653 * gtk_style_context_get_junction_sides:
2654 * @context: a #GtkStyleContext
2656 * Returns the sides where rendered elements connect visually with others.
2658 * Returns: the junction sides
2663 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2665 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2667 return context->priv->info->junction_sides;
2670 static GtkSymbolicColor *
2671 gtk_style_context_color_lookup_func (gpointer contextp,
2674 GtkStyleContext *context = contextp;
2676 return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2680 _gtk_style_context_resolve_color_value (GtkStyleContext *context,
2681 GtkCssValue *current,
2682 GtkCssDependencies current_deps,
2684 GtkCssDependencies *dependencies)
2686 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2687 g_return_val_if_fail (current != NULL, FALSE);
2688 g_return_val_if_fail (color != NULL, FALSE);
2690 return _gtk_symbolic_color_resolve_full ((GtkSymbolicColor *) color,
2693 gtk_style_context_color_lookup_func,
2700 _gtk_style_context_resolve_color (GtkStyleContext *context,
2701 GtkSymbolicColor *color,
2703 GtkCssDependencies *dependencies)
2707 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2708 g_return_val_if_fail (color != NULL, FALSE);
2709 g_return_val_if_fail (result != NULL, FALSE);
2711 val = _gtk_symbolic_color_resolve_full (color,
2712 _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR),
2713 GTK_CSS_DEPENDS_ON_COLOR,
2714 gtk_style_context_color_lookup_func,
2720 *result = *_gtk_css_rgba_value_get_rgba (val);
2721 _gtk_css_value_unref (val);
2726 * gtk_style_context_lookup_color:
2727 * @context: a #GtkStyleContext
2728 * @color_name: color name to lookup
2729 * @color: (out): Return location for the looked up color
2731 * Looks up and resolves a color name in the @context color map.
2733 * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2736 gtk_style_context_lookup_color (GtkStyleContext *context,
2737 const gchar *color_name,
2740 GtkSymbolicColor *sym_color;
2742 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2743 g_return_val_if_fail (color_name != NULL, FALSE);
2744 g_return_val_if_fail (color != NULL, FALSE);
2746 sym_color = gtk_style_context_color_lookup_func (context, color_name);
2747 if (sym_color == NULL)
2750 return _gtk_style_context_resolve_color (context, sym_color, color, NULL);
2754 * gtk_style_context_notify_state_change:
2755 * @context: a #GtkStyleContext
2756 * @window: a #GdkWindow
2757 * @region_id: (allow-none): animatable region to notify on, or %NULL.
2758 * See gtk_style_context_push_animatable_region()
2759 * @state: state to trigger transition for
2760 * @state_value: %TRUE if @state is the state we are changing to,
2761 * %FALSE if we are changing away from it
2763 * Notifies a state change on @context, so if the current style makes use
2764 * of transition animations, one will be started so all rendered elements
2765 * under @region_id are animated for state @state being set to value
2768 * The @window parameter is used in order to invalidate the rendered area
2769 * as the animation runs, so make sure it is the same window that is being
2770 * rendered on by the gtk_render_*() functions.
2772 * If @region_id is %NULL, all rendered elements using @context will be
2773 * affected by this state transition.
2775 * As a practical example, a #GtkButton notifying a state transition on
2776 * the prelight state:
2778 * gtk_style_context_notify_state_change (context,
2779 * gtk_widget_get_window (widget),
2781 * GTK_STATE_PRELIGHT,
2782 * button->in_button);
2785 * Can be handled in the CSS file like this:
2788 * background-color: #f00
2792 * background-color: #fff;
2793 * transition: 200ms linear
2797 * This combination will animate the button background from red to white
2798 * if a pointer enters the button, and back to red if the pointer leaves
2801 * Note that @state is used when finding the transition parameters, which
2802 * is why the style places the transition under the :hover pseudo-class.
2806 * Deprecated: 3.6: This function does nothing.
2809 gtk_style_context_notify_state_change (GtkStyleContext *context,
2813 gboolean state_value)
2815 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2816 g_return_if_fail (GDK_IS_WINDOW (window));
2817 g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2818 g_return_if_fail (context->priv->widget != NULL || context->priv->widget_path != NULL);
2822 * gtk_style_context_cancel_animations:
2823 * @context: a #GtkStyleContext
2824 * @region_id: (allow-none): animatable region to stop, or %NULL.
2825 * See gtk_style_context_push_animatable_region()
2827 * Stops all running animations for @region_id and all animatable
2828 * regions underneath.
2830 * A %NULL @region_id will stop all ongoing animations in @context,
2831 * when dealing with a #GtkStyleContext obtained through
2832 * gtk_widget_get_style_context(), this is normally done for you
2833 * in all circumstances you would expect all widget to be stopped,
2834 * so this should be only used in complex widgets with different
2835 * animatable regions.
2839 * Deprecated: 3.6: This function does nothing.
2842 gtk_style_context_cancel_animations (GtkStyleContext *context,
2845 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2849 * gtk_style_context_scroll_animations:
2850 * @context: a #GtkStyleContext
2851 * @window: a #GdkWindow used previously in
2852 * gtk_style_context_notify_state_change()
2853 * @dx: Amount to scroll in the X axis
2854 * @dy: Amount to scroll in the Y axis
2856 * This function is analogous to gdk_window_scroll(), and
2857 * should be called together with it so the invalidation
2858 * areas for any ongoing animation are scrolled together
2863 * Deprecated: 3.6: This function does nothing.
2866 gtk_style_context_scroll_animations (GtkStyleContext *context,
2871 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2872 g_return_if_fail (GDK_IS_WINDOW (window));
2876 * gtk_style_context_push_animatable_region:
2877 * @context: a #GtkStyleContext
2878 * @region_id: unique identifier for the animatable region
2880 * Pushes an animatable region, so all further gtk_render_*() calls between
2881 * this call and the following gtk_style_context_pop_animatable_region()
2882 * will potentially show transition animations for this region if
2883 * gtk_style_context_notify_state_change() is called for a given state,
2884 * and the current theme/style defines transition animations for state
2887 * The @region_id used must be unique in @context so the theming engine
2888 * can uniquely identify rendered elements subject to a state transition.
2892 * Deprecated: 3.6: This function does nothing.
2895 gtk_style_context_push_animatable_region (GtkStyleContext *context,
2898 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2899 g_return_if_fail (region_id != NULL);
2903 * gtk_style_context_pop_animatable_region:
2904 * @context: a #GtkStyleContext
2906 * Pops an animatable region from @context.
2907 * See gtk_style_context_push_animatable_region().
2911 * Deprecated: 3.6: This function does nothing.
2914 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
2916 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2920 gtk_style_context_clear_cache (GtkStyleContext *context)
2922 GtkStyleContextPrivate *priv;
2925 priv = context->priv;
2927 for (info = priv->info; info; info = info->next)
2929 style_info_set_data (info, NULL);
2931 g_hash_table_remove_all (priv->style_data);
2935 gtk_style_context_update_cache (GtkStyleContext *context,
2936 const GtkBitmask *parent_changes)
2938 GtkStyleContextPrivate *priv;
2939 GHashTableIter iter;
2940 gpointer key, value;
2942 priv = context->priv;
2944 g_hash_table_remove_all (priv->style_data);
2946 g_hash_table_iter_init (&iter, priv->style_data);
2947 while (g_hash_table_iter_next (&iter, &key, &value))
2949 GtkStyleInfo *info = key;
2950 StyleData *data = value;
2951 GtkBitmask *changes;
2953 changes = _gtk_bitmask_copy (parent_changes);
2954 changes = _gtk_bitmask_intersect (changes, data->store->depends_on_parent);
2955 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
2956 changes = _gtk_bitmask_union (changes, data->store->depends_on_color);
2957 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
2958 changes = _gtk_bitmask_union (changes, data->store->depends_on_font_size);
2960 build_properties (context, data->store, info->state_flags, changes);
2965 gtk_style_context_do_invalidate (GtkStyleContext *context)
2967 GtkStyleContextPrivate *priv;
2969 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2971 priv = context->priv;
2973 /* Avoid reentrancy */
2974 if (priv->invalidating_context)
2977 priv->invalidating_context = TRUE;
2979 g_signal_emit (context, signals[CHANGED], 0);
2981 priv->invalidating_context = FALSE;
2985 _gtk_style_context_stop_animations (GtkStyleContext *context)
2987 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2989 if (!gtk_style_context_is_animating (context))
2992 style_info_set_data (context->priv->info, NULL);
2994 gtk_style_context_stop_animating (context);
2998 gtk_style_context_update_animations (GtkStyleContext *context,
3001 GtkBitmask *differences;
3002 StyleData *style_data;
3004 style_data = style_data_lookup (context);
3006 differences = _gtk_css_animated_values_advance (GTK_CSS_ANIMATED_VALUES (style_data->store),
3009 if (_gtk_css_animated_values_is_finished (GTK_CSS_ANIMATED_VALUES (style_data->store)))
3010 _gtk_style_context_stop_animations (context);
3016 gtk_style_context_should_animate (GtkStyleContext *context)
3018 GtkStyleContextPrivate *priv;
3021 priv = context->priv;
3023 if (priv->widget == NULL)
3026 if (!gtk_widget_get_mapped (priv->widget))
3029 g_object_get (gtk_widget_get_settings (context->priv->widget),
3030 "gtk-enable-animations", &animate,
3037 gtk_style_context_start_animations (GtkStyleContext *context,
3038 GtkCssComputedValues *previous,
3041 StyleData *animated;
3043 if (!gtk_style_context_should_animate (context))
3045 gtk_style_context_stop_animating (context);
3049 animated = style_data_new ();
3050 animated->store = _gtk_css_animated_values_new (style_data_lookup (context)->store,
3054 if (_gtk_css_animated_values_is_finished (GTK_CSS_ANIMATED_VALUES (animated->store)))
3056 style_data_unref (animated);
3057 gtk_style_context_stop_animating (context);
3061 style_info_set_data (context->priv->info, animated);
3062 style_data_unref (animated);
3063 gtk_style_context_start_animating (context);
3067 gtk_style_context_needs_full_revalidate (GtkStyleContext *context,
3068 GtkCssChange change)
3070 GtkStyleContextPrivate *priv = context->priv;
3072 /* Try to avoid invalidating if we can */
3073 if (change & GTK_STYLE_CONTEXT_RADICAL_CHANGE)
3075 priv->relevant_changes = GTK_CSS_CHANGE_ANY;
3079 if (priv->relevant_changes == GTK_CSS_CHANGE_ANY)
3081 GtkWidgetPath *path;
3082 GtkCssMatcher matcher;
3084 path = create_query_path (context);
3085 if (_gtk_css_matcher_init (&matcher, path, priv->info->state_flags))
3086 priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3089 priv->relevant_changes = 0;
3091 priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
3093 gtk_widget_path_unref (path);
3097 if (priv->relevant_changes & change)
3104 _gtk_style_context_validate (GtkStyleContext *context,
3106 GtkCssChange change,
3107 const GtkBitmask *parent_changes)
3109 GtkStyleContextPrivate *priv;
3112 GtkBitmask *changes;
3115 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3117 priv = context->priv;
3119 change |= priv->pending_changes;
3121 /* If you run your application with
3122 * GTK_DEBUG=no-css-cache
3123 * every invalidation will purge the cache and completely query
3124 * everything anew form the cache. This is slow (in particular
3125 * when animating), but useful for figuring out bugs.
3127 * We achieve that by pretending that everything that could have
3128 * changed has and so we of course totally need to redo everything.
3130 * Note that this also completely revalidates child widgets all
3133 if (G_UNLIKELY (gtk_get_debug_flags () & GTK_DEBUG_NO_CSS_CACHE))
3134 change = GTK_CSS_CHANGE_ANY;
3136 if (!priv->invalid && change == 0 && _gtk_bitmask_is_empty (parent_changes))
3139 priv->pending_changes = 0;
3140 gtk_style_context_set_invalid (context, FALSE);
3144 current = style_data_ref (info->data);
3148 /* Try to avoid invalidating if we can */
3149 if (current == NULL ||
3150 gtk_style_context_needs_full_revalidate (context, change))
3152 if ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE)
3154 gtk_style_context_clear_cache (context);
3158 gtk_style_context_update_cache (context, parent_changes);
3159 style_info_set_data (info, NULL);
3166 gtk_style_context_start_animations (context, current->store, timestamp);
3167 change &= ~GTK_CSS_CHANGE_ANIMATE;
3169 data = style_data_lookup (context);
3171 changes = _gtk_css_computed_values_get_difference (data->store, current->store);
3173 style_data_unref (current);
3177 changes = _gtk_bitmask_new ();
3178 changes = _gtk_bitmask_invert_range (changes, 0, _gtk_css_style_property_get_n_properties ());
3183 changes = _gtk_bitmask_copy (parent_changes);
3184 changes = _gtk_bitmask_intersect (changes, current->store->depends_on_parent);
3185 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
3186 changes = _gtk_bitmask_union (changes, current->store->depends_on_color);
3187 if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
3188 changes = _gtk_bitmask_union (changes, current->store->depends_on_font_size);
3190 gtk_style_context_update_cache (context, parent_changes);
3193 if (change & GTK_CSS_CHANGE_ANIMATE &&
3194 gtk_style_context_is_animating (context))
3196 GtkBitmask *animation_changes;
3198 animation_changes = gtk_style_context_update_animations (context, timestamp);
3199 changes = _gtk_bitmask_union (changes, animation_changes);
3200 _gtk_bitmask_free (animation_changes);
3203 if (!_gtk_bitmask_is_empty (changes))
3204 gtk_style_context_do_invalidate (context);
3206 change = _gtk_css_change_for_child (change);
3207 for (list = priv->children; list; list = list->next)
3209 _gtk_style_context_validate (list->data, timestamp, change, changes);
3212 _gtk_bitmask_free (changes);
3216 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3217 GtkCssChange change)
3219 GtkStyleContextPrivate *priv;
3221 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3222 g_return_if_fail (change != 0);
3224 priv = context->priv;
3226 if (priv->widget != NULL)
3228 priv->pending_changes |= change;
3229 gtk_style_context_set_invalid (context, TRUE);
3231 else if (priv->widget_path == NULL)
3233 gtk_style_context_invalidate (context);
3238 * gtk_style_context_invalidate:
3239 * @context: a #GtkStyleContext.
3241 * Invalidates @context style information, so it will be reconstructed
3244 * If you're using a #GtkStyleContext returned from
3245 * gtk_widget_get_style_context(), you do not need to
3246 * call this yourself.
3251 gtk_style_context_invalidate (GtkStyleContext *context)
3253 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3255 gtk_style_context_clear_cache (context);
3256 gtk_style_context_do_invalidate (context);
3260 * gtk_style_context_set_background:
3261 * @context: a #GtkStyleContext
3262 * @window: a #GdkWindow
3264 * Sets the background of @window to the background pattern or
3265 * color specified in @context for its current state.
3270 gtk_style_context_set_background (GtkStyleContext *context,
3273 GtkStateFlags state;
3274 cairo_pattern_t *pattern;
3277 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3278 g_return_if_fail (GDK_IS_WINDOW (window));
3280 state = gtk_style_context_get_state (context);
3281 gtk_style_context_get (context, state,
3282 "background-image", &pattern,
3286 gdk_window_set_background_pattern (window, pattern);
3287 cairo_pattern_destroy (pattern);
3291 gtk_style_context_get (context, state,
3292 "background-color", &color,
3296 gdk_window_set_background_rgba (window, color);
3297 gdk_rgba_free (color);
3302 * gtk_style_context_get_color:
3303 * @context: a #GtkStyleContext
3304 * @state: state to retrieve the color for
3305 * @color: (out): return value for the foreground color
3307 * Gets the foreground color for a given state.
3312 gtk_style_context_get_color (GtkStyleContext *context,
3313 GtkStateFlags state,
3318 g_return_if_fail (color != NULL);
3319 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3321 gtk_style_context_get (context,
3331 * gtk_style_context_get_background_color:
3332 * @context: a #GtkStyleContext
3333 * @state: state to retrieve the color for
3334 * @color: (out): return value for the background color
3336 * Gets the background color for a given state.
3341 gtk_style_context_get_background_color (GtkStyleContext *context,
3342 GtkStateFlags state,
3347 g_return_if_fail (color != NULL);
3348 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3350 gtk_style_context_get (context,
3352 "background-color", &c,
3360 * gtk_style_context_get_border_color:
3361 * @context: a #GtkStyleContext
3362 * @state: state to retrieve the color for
3363 * @color: (out): return value for the border color
3365 * Gets the border color for a given state.
3370 gtk_style_context_get_border_color (GtkStyleContext *context,
3371 GtkStateFlags state,
3376 g_return_if_fail (color != NULL);
3377 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3379 gtk_style_context_get (context,
3389 * gtk_style_context_get_border:
3390 * @context: a #GtkStyleContext
3391 * @state: state to retrieve the border for
3392 * @border: (out): return value for the border settings
3394 * Gets the border for a given state as a #GtkBorder.
3395 * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3400 gtk_style_context_get_border (GtkStyleContext *context,
3401 GtkStateFlags state,
3404 int top, left, bottom, right;
3406 g_return_if_fail (border != NULL);
3407 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3409 gtk_style_context_get (context,
3411 "border-top-width", &top,
3412 "border-left-width", &left,
3413 "border-bottom-width", &bottom,
3414 "border-right-width", &right,
3418 border->left = left;
3419 border->bottom = bottom;
3420 border->right = right;
3424 * gtk_style_context_get_padding:
3425 * @context: a #GtkStyleContext
3426 * @state: state to retrieve the padding for
3427 * @padding: (out): return value for the padding settings
3429 * Gets the padding for a given state as a #GtkBorder.
3430 * See %GTK_STYLE_PROPERTY_PADDING.
3435 gtk_style_context_get_padding (GtkStyleContext *context,
3436 GtkStateFlags state,
3439 int top, left, bottom, right;
3441 g_return_if_fail (padding != NULL);
3442 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3444 gtk_style_context_get (context,
3446 "padding-top", &top,
3447 "padding-left", &left,
3448 "padding-bottom", &bottom,
3449 "padding-right", &right,
3453 padding->left = left;
3454 padding->bottom = bottom;
3455 padding->right = right;
3459 * gtk_style_context_get_margin:
3460 * @context: a #GtkStyleContext
3461 * @state: state to retrieve the border for
3462 * @margin: (out): return value for the margin settings
3464 * Gets the margin for a given state as a #GtkBorder.
3465 * See %GTK_STYLE_PROPERTY_MARGIN.
3470 gtk_style_context_get_margin (GtkStyleContext *context,
3471 GtkStateFlags state,
3474 int top, left, bottom, right;
3476 g_return_if_fail (margin != NULL);
3477 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3479 gtk_style_context_get (context,
3482 "margin-left", &left,
3483 "margin-bottom", &bottom,
3484 "margin-right", &right,
3488 margin->left = left;
3489 margin->bottom = bottom;
3490 margin->right = right;
3494 * gtk_style_context_get_font:
3495 * @context: a #GtkStyleContext
3496 * @state: state to retrieve the font for
3498 * Returns the font description for a given state. The returned
3499 * object is const and will remain valid until the
3500 * #GtkStyleContext::changed signal happens.
3502 * Returns: (transfer none): the #PangoFontDescription for the given
3503 * state. This object is owned by GTK+ and should not be
3508 const PangoFontDescription *
3509 gtk_style_context_get_font (GtkStyleContext *context,
3510 GtkStateFlags state)
3512 GtkStyleContextPrivate *priv;
3514 PangoFontDescription *description;
3516 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3518 priv = context->priv;
3519 g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3521 data = style_data_lookup_for_state (context, state);
3523 /* Yuck, fonts are created on-demand but we don't return a ref.
3524 * Do bad things to achieve this requirement */
3525 description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3526 if (description == NULL)
3528 gtk_style_context_get (context, state, "font", &description, NULL);
3529 g_object_set_data_full (G_OBJECT (data->store),
3530 "font-cache-for-get_font",
3532 (GDestroyNotify) pango_font_description_free);
3538 get_cursor_color (GtkStyleContext *context,
3542 GdkColor *style_color;
3544 gtk_style_context_get_style (context,
3545 primary ? "cursor-color" : "secondary-cursor-color",
3551 color->red = style_color->red / 65535.0;
3552 color->green = style_color->green / 65535.0;
3553 color->blue = style_color->blue / 65535.0;
3556 gdk_color_free (style_color);
3560 gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3566 gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3568 color->red = (color->red + bg.red) * 0.5;
3569 color->green = (color->green + bg.green) * 0.5;
3570 color->blue = (color->blue + bg.blue) * 0.5;
3576 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3577 GdkRGBA *primary_color,
3578 GdkRGBA *secondary_color)
3581 get_cursor_color (context, TRUE, primary_color);
3583 if (secondary_color)
3584 get_cursor_color (context, FALSE, secondary_color);
3591 * @context: a #GtkStyleContext
3593 * @x: X origin of the rectangle
3594 * @y: Y origin of the rectangle
3595 * @width: rectangle width
3596 * @height: rectangle height
3598 * Renders a checkmark (as in a #GtkCheckButton).
3600 * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3601 * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3602 * should be marked as undefined.
3605 * <title>Typical checkmark rendering</title>
3606 * <inlinegraphic fileref="checks.png" format="PNG"/>
3612 gtk_render_check (GtkStyleContext *context,
3619 GtkThemingEngineClass *engine_class;
3620 GtkThemingEngine *engine;
3622 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3623 g_return_if_fail (cr != NULL);
3625 if (width <= 0 || height <= 0)
3628 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3629 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3634 _gtk_theming_engine_set_context (engine, context);
3635 engine_class->render_check (engine, cr,
3636 x, y, width, height);
3642 * gtk_render_option:
3643 * @context: a #GtkStyleContext
3645 * @x: X origin of the rectangle
3646 * @y: Y origin of the rectangle
3647 * @width: rectangle width
3648 * @height: rectangle height
3650 * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3651 * state will determine whether the option is on or off, and
3652 * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3655 * <title>Typical option mark rendering</title>
3656 * <inlinegraphic fileref="options.png" format="PNG"/>
3662 gtk_render_option (GtkStyleContext *context,
3669 GtkThemingEngineClass *engine_class;
3670 GtkThemingEngine *engine;
3672 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3673 g_return_if_fail (cr != NULL);
3675 if (width <= 0 || height <= 0)
3678 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3679 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3683 _gtk_theming_engine_set_context (engine, context);
3684 engine_class->render_option (engine, cr,
3685 x, y, width, height);
3692 * @context: a #GtkStyleContext
3694 * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3695 * @x: X origin of the render area
3696 * @y: Y origin of the render area
3697 * @size: square side for render area
3699 * Renders an arrow pointing to @angle.
3702 * <title>Typical arrow rendering at 0, 1&solidus;2 π, π and 3&solidus;2 π</title>
3703 * <inlinegraphic fileref="arrows.png" format="PNG"/>
3709 gtk_render_arrow (GtkStyleContext *context,
3716 GtkThemingEngineClass *engine_class;
3717 GtkThemingEngine *engine;
3719 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3720 g_return_if_fail (cr != NULL);
3725 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3726 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3730 gtk_style_context_save (context);
3731 gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3733 _gtk_theming_engine_set_context (engine, context);
3734 engine_class->render_arrow (engine, cr,
3737 gtk_style_context_restore (context);
3742 * gtk_render_background:
3743 * @context: a #GtkStyleContext
3745 * @x: X origin of the rectangle
3746 * @y: Y origin of the rectangle
3747 * @width: rectangle width
3748 * @height: rectangle height
3750 * Renders the background of an element.
3753 * <title>Typical background rendering, showing the effect of
3754 * <parameter>background-image</parameter>,
3755 * <parameter>border-width</parameter> and
3756 * <parameter>border-radius</parameter></title>
3757 * <inlinegraphic fileref="background.png" format="PNG"/>
3763 gtk_render_background (GtkStyleContext *context,
3770 GtkThemingEngineClass *engine_class;
3771 GtkThemingEngine *engine;
3773 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3774 g_return_if_fail (cr != NULL);
3776 if (width <= 0 || height <= 0)
3779 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3780 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3784 _gtk_theming_engine_set_context (engine, context);
3785 engine_class->render_background (engine, cr, x, y, width, height);
3792 * @context: a #GtkStyleContext
3794 * @x: X origin of the rectangle
3795 * @y: Y origin of the rectangle
3796 * @width: rectangle width
3797 * @height: rectangle height
3799 * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3802 * <title>Examples of frame rendering, showing the effect of
3803 * <parameter>border-image</parameter>,
3804 * <parameter>border-color</parameter>,
3805 * <parameter>border-width</parameter>,
3806 * <parameter>border-radius</parameter> and
3808 * <inlinegraphic fileref="frames.png" format="PNG"/>
3814 gtk_render_frame (GtkStyleContext *context,
3821 GtkThemingEngineClass *engine_class;
3822 GtkThemingEngine *engine;
3824 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3825 g_return_if_fail (cr != NULL);
3827 if (width <= 0 || height <= 0)
3830 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3831 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3835 _gtk_theming_engine_set_context (engine, context);
3836 engine_class->render_frame (engine, cr, x, y, width, height);
3842 * gtk_render_expander:
3843 * @context: a #GtkStyleContext
3845 * @x: X origin of the rectangle
3846 * @y: Y origin of the rectangle
3847 * @width: rectangle width
3848 * @height: rectangle height
3850 * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3851 * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3852 * determines whether the expander is collapsed or expanded.
3855 * <title>Typical expander rendering</title>
3856 * <inlinegraphic fileref="expanders.png" format="PNG"/>
3862 gtk_render_expander (GtkStyleContext *context,
3869 GtkThemingEngineClass *engine_class;
3870 GtkThemingEngine *engine;
3872 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3873 g_return_if_fail (cr != NULL);
3875 if (width <= 0 || height <= 0)
3878 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3879 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3883 _gtk_theming_engine_set_context (engine, context);
3884 engine_class->render_expander (engine, cr, x, y, width, height);
3891 * @context: a #GtkStyleContext
3893 * @x: X origin of the rectangle
3894 * @y: Y origin of the rectangle
3895 * @width: rectangle width
3896 * @height: rectangle height
3898 * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3900 * <title>Typical focus rendering</title>
3901 * <inlinegraphic fileref="focus.png" format="PNG"/>
3907 gtk_render_focus (GtkStyleContext *context,
3914 GtkThemingEngineClass *engine_class;
3915 GtkThemingEngine *engine;
3917 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3918 g_return_if_fail (cr != NULL);
3920 if (width <= 0 || height <= 0)
3923 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3924 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3928 _gtk_theming_engine_set_context (engine, context);
3929 engine_class->render_focus (engine, cr, x, y, width, height);
3935 * gtk_render_layout:
3936 * @context: a #GtkStyleContext
3940 * @layout: the #PangoLayout to render
3942 * Renders @layout on the coordinates @x, @y
3947 gtk_render_layout (GtkStyleContext *context,
3951 PangoLayout *layout)
3953 GtkThemingEngineClass *engine_class;
3954 GtkThemingEngine *engine;
3955 PangoRectangle extents;
3957 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3958 g_return_if_fail (PANGO_IS_LAYOUT (layout));
3959 g_return_if_fail (cr != NULL);
3961 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3962 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3966 pango_layout_get_extents (layout, &extents, NULL);
3968 _gtk_theming_engine_set_context (engine, context);
3969 engine_class->render_layout (engine, cr, x, y, layout);
3976 * @context: a #GtkStyleContext
3978 * @x0: X coordinate for the origin of the line
3979 * @y0: Y coordinate for the origin of the line
3980 * @x1: X coordinate for the end of the line
3981 * @y1: Y coordinate for the end of the line
3983 * Renders a line from (x0, y0) to (x1, y1).
3988 gtk_render_line (GtkStyleContext *context,
3995 GtkThemingEngineClass *engine_class;
3996 GtkThemingEngine *engine;
3998 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3999 g_return_if_fail (cr != NULL);
4001 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4002 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4006 _gtk_theming_engine_set_context (engine, context);
4007 engine_class->render_line (engine, cr, x0, y0, x1, y1);
4013 * gtk_render_slider:
4014 * @context: a #GtkStyleContext
4016 * @x: X origin of the rectangle
4017 * @y: Y origin of the rectangle
4018 * @width: rectangle width
4019 * @height: rectangle height
4020 * @orientation: orientation of the slider
4022 * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4023 * @width, @height. @orientation defines whether the slider is vertical
4027 * <title>Typical slider rendering</title>
4028 * <inlinegraphic fileref="sliders.png" format="PNG"/>
4034 gtk_render_slider (GtkStyleContext *context,
4040 GtkOrientation orientation)
4042 GtkThemingEngineClass *engine_class;
4043 GtkThemingEngine *engine;
4045 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4046 g_return_if_fail (cr != NULL);
4048 if (width <= 0 || height <= 0)
4051 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4052 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4056 _gtk_theming_engine_set_context (engine, context);
4057 engine_class->render_slider (engine, cr, x, y, width, height, orientation);
4063 * gtk_render_frame_gap:
4064 * @context: a #GtkStyleContext
4066 * @x: X origin of the rectangle
4067 * @y: Y origin of the rectangle
4068 * @width: rectangle width
4069 * @height: rectangle height
4070 * @gap_side: side where the gap is
4071 * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4072 * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4074 * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4075 * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4076 * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4077 * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4080 * <title>Typical rendering of a frame with a gap</title>
4081 * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4087 gtk_render_frame_gap (GtkStyleContext *context,
4093 GtkPositionType gap_side,
4097 GtkThemingEngineClass *engine_class;
4098 GtkThemingEngine *engine;
4100 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4101 g_return_if_fail (cr != NULL);
4102 g_return_if_fail (xy0_gap <= xy1_gap);
4103 g_return_if_fail (xy0_gap >= 0);
4105 if (width <= 0 || height <= 0)
4108 if (gap_side == GTK_POS_LEFT ||
4109 gap_side == GTK_POS_RIGHT)
4110 g_return_if_fail (xy1_gap <= height);
4112 g_return_if_fail (xy1_gap <= width);
4114 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4115 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4119 _gtk_theming_engine_set_context (engine, context);
4120 engine_class->render_frame_gap (engine, cr,
4121 x, y, width, height, gap_side,
4128 * gtk_render_extension:
4129 * @context: a #GtkStyleContext
4131 * @x: X origin of the rectangle
4132 * @y: Y origin of the rectangle
4133 * @width: rectangle width
4134 * @height: rectangle height
4135 * @gap_side: side where the gap is
4137 * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4138 * defined by @x, @y, @width, @height. The side where the extension
4139 * connects to is defined by @gap_side.
4142 * <title>Typical extension rendering</title>
4143 * <inlinegraphic fileref="extensions.png" format="PNG"/>
4149 gtk_render_extension (GtkStyleContext *context,
4155 GtkPositionType gap_side)
4157 GtkThemingEngineClass *engine_class;
4158 GtkThemingEngine *engine;
4160 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4161 g_return_if_fail (cr != NULL);
4163 if (width <= 0 || height <= 0)
4166 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4167 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4171 _gtk_theming_engine_set_context (engine, context);
4172 engine_class->render_extension (engine, cr, x, y, width, height, gap_side);
4178 * gtk_render_handle:
4179 * @context: a #GtkStyleContext
4181 * @x: X origin of the rectangle
4182 * @y: Y origin of the rectangle
4183 * @width: rectangle width
4184 * @height: rectangle height
4186 * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4187 * #GtkWindow<!-- -->'s resize grip), in the rectangle
4188 * determined by @x, @y, @width, @height.
4191 * <title>Handles rendered for the paned and grip classes</title>
4192 * <inlinegraphic fileref="handles.png" format="PNG"/>
4198 gtk_render_handle (GtkStyleContext *context,
4205 GtkThemingEngineClass *engine_class;
4206 GtkThemingEngine *engine;
4208 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4209 g_return_if_fail (cr != NULL);
4211 if (width <= 0 || height <= 0)
4214 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4215 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4219 _gtk_theming_engine_set_context (engine, context);
4220 engine_class->render_handle (engine, cr, x, y, width, height);
4226 * gtk_render_activity:
4227 * @context: a #GtkStyleContext
4229 * @x: X origin of the rectangle
4230 * @y: Y origin of the rectangle
4231 * @width: rectangle width
4232 * @height: rectangle height
4234 * Renders an activity area (Such as in #GtkSpinner or the
4235 * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4236 * determines whether there is activity going on.
4241 gtk_render_activity (GtkStyleContext *context,
4248 GtkThemingEngineClass *engine_class;
4249 GtkThemingEngine *engine;
4251 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4252 g_return_if_fail (cr != NULL);
4254 if (width <= 0 || height <= 0)
4257 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4258 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4262 _gtk_theming_engine_set_context (engine, context);
4263 engine_class->render_activity (engine, cr, x, y, width, height);
4269 * gtk_render_icon_pixbuf:
4270 * @context: a #GtkStyleContext
4271 * @source: the #GtkIconSource specifying the icon to render
4272 * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4273 * means render at the size of the source and don't scale.
4275 * Renders the icon specified by @source at the given @size, returning the result
4278 * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4283 gtk_render_icon_pixbuf (GtkStyleContext *context,
4284 const GtkIconSource *source,
4287 GtkThemingEngineClass *engine_class;
4288 GtkThemingEngine *engine;
4290 g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4291 g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4292 g_return_val_if_fail (source != NULL, NULL);
4294 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4295 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4297 _gtk_theming_engine_set_context (engine, context);
4298 return engine_class->render_icon_pixbuf (engine, source, size);
4303 * @context: a #GtkStyleContext
4305 * @pixbuf: a #GdkPixbuf containing the icon to draw
4306 * @x: X position for the @pixbuf
4307 * @y: Y position for the @pixbuf
4309 * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4314 gtk_render_icon (GtkStyleContext *context,
4320 GtkThemingEngineClass *engine_class;
4321 GtkThemingEngine *engine;
4323 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4324 g_return_if_fail (cr != NULL);
4326 engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4327 engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4331 _gtk_theming_engine_set_context (engine, context);
4332 engine_class->render_icon (engine, cr, pixbuf, x, y);
4338 draw_insertion_cursor (GtkStyleContext *context,
4343 gboolean is_primary,
4344 PangoDirection direction,
4345 gboolean draw_arrow)
4348 GdkRGBA primary_color;
4349 GdkRGBA secondary_color;
4350 gfloat cursor_aspect_ratio;
4356 _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4357 gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4359 /* When changing the shape or size of the cursor here,
4360 * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4363 gtk_style_context_get_style (context,
4364 "cursor-aspect-ratio", &cursor_aspect_ratio,
4367 stem_width = height * cursor_aspect_ratio + 1;
4369 /* put (stem_width % 2) on the proper side of the cursor */
4370 if (direction == PANGO_DIRECTION_LTR)
4371 offset = stem_width / 2;
4373 offset = stem_width - stem_width / 2;
4375 cairo_rectangle (cr, x - offset, y, stem_width, height);
4383 arrow_width = stem_width + 1;
4385 if (direction == PANGO_DIRECTION_RTL)
4387 ax = x - offset - 1;
4388 ay = y + height - arrow_width * 2 - arrow_width + 1;
4390 cairo_move_to (cr, ax, ay + 1);
4391 cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4392 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4395 else if (direction == PANGO_DIRECTION_LTR)
4397 ax = x + stem_width - offset;
4398 ay = y + height - arrow_width * 2 - arrow_width + 1;
4400 cairo_move_to (cr, ax, ay + 1);
4401 cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4402 cairo_line_to (cr, ax, ay + 2 * arrow_width);
4406 g_assert_not_reached();
4413 * gtk_render_insertion_cursor:
4414 * @context: a #GtkStyleContext
4418 * @layout: the #PangoLayout of the text
4419 * @index: the index in the #PangoLayout
4420 * @direction: the #PangoDirection of the text
4422 * Draws a text caret on @cr at the specified index of @layout.
4427 gtk_render_insertion_cursor (GtkStyleContext *context,
4431 PangoLayout *layout,
4433 PangoDirection direction)
4435 GtkStyleContextPrivate *priv;
4436 gboolean split_cursor;
4437 PangoRectangle strong_pos, weak_pos;
4438 PangoRectangle *cursor1, *cursor2;
4439 PangoDirection keymap_direction;
4440 PangoDirection direction2;
4442 g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4443 g_return_if_fail (cr != NULL);
4444 g_return_if_fail (PANGO_IS_LAYOUT (layout));
4445 g_return_if_fail (index >= 0);
4447 priv = context->priv;
4449 g_object_get (gtk_settings_get_for_screen (priv->screen),
4450 "gtk-split-cursor", &split_cursor,
4453 keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4455 pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4457 direction2 = PANGO_DIRECTION_NEUTRAL;
4461 cursor1 = &strong_pos;
4463 if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4465 direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4466 cursor2 = &weak_pos;
4471 if (keymap_direction == direction)
4472 cursor1 = &strong_pos;
4474 cursor1 = &weak_pos;
4477 draw_insertion_cursor (context,
4479 x + PANGO_PIXELS (cursor1->x),
4480 y + PANGO_PIXELS (cursor1->y),
4481 PANGO_PIXELS (cursor1->height),
4484 direction2 != PANGO_DIRECTION_NEUTRAL);
4486 if (direction2 != PANGO_DIRECTION_NEUTRAL)
4488 draw_insertion_cursor (context,
4490 x + PANGO_PIXELS (cursor2->x),
4491 y + PANGO_PIXELS (cursor2->y),
4492 PANGO_PIXELS (cursor2->height),
4500 * gtk_draw_insertion_cursor:
4501 * @widget: a #GtkWidget
4502 * @cr: cairo context to draw to
4503 * @location: location where to draw the cursor (@location->width is ignored)
4504 * @is_primary: if the cursor should be the primary cursor color.
4505 * @direction: whether the cursor is left-to-right or
4506 * right-to-left. Should never be #GTK_TEXT_DIR_NONE
4507 * @draw_arrow: %TRUE to draw a directional arrow on the
4508 * cursor. Should be %FALSE unless the cursor is split.
4510 * Draws a text caret on @cr at @location. This is not a style function
4511 * but merely a convenience function for drawing the standard cursor shape.
4514 * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4517 gtk_draw_insertion_cursor (GtkWidget *widget,
4519 const GdkRectangle *location,
4520 gboolean is_primary,
4521 GtkTextDirection direction,
4522 gboolean draw_arrow)
4524 GtkStyleContext *context;
4526 g_return_if_fail (GTK_IS_WIDGET (widget));
4527 g_return_if_fail (cr != NULL);
4528 g_return_if_fail (location != NULL);
4529 g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4531 context = gtk_widget_get_style_context (widget);
4533 draw_insertion_cursor (context, cr,
4534 location->x, location->y, location->height,
4536 (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4540 static AtkAttributeSet *
4541 add_attribute (AtkAttributeSet *attributes,
4542 AtkTextAttribute attr,
4547 at = g_new (AtkAttribute, 1);
4548 at->name = g_strdup (atk_text_attribute_get_name (attr));
4549 at->value = g_strdup (value);
4551 return g_slist_prepend (attributes, at);
4555 * _gtk_style_context_get_attributes:
4556 * @attributes: a #AtkAttributeSet to add attributes to
4557 * @context: the #GtkStyleContext to get attributes from
4558 * @flags: the state to use with @context
4560 * Adds the foreground and background color from @context to
4561 * @attributes, after translating them to ATK attributes.
4563 * This is a convenience function that can be used in
4564 * implementing the #AtkText interface in widgets.
4566 * Returns: the modified #AtkAttributeSet
4569 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4570 GtkStyleContext *context,
4571 GtkStateFlags flags)
4576 gtk_style_context_get_background_color (context, flags, &color);
4577 value = g_strdup_printf ("%u,%u,%u",
4578 (guint) ceil (color.red * 65536 - color.red),
4579 (guint) ceil (color.green * 65536 - color.green),
4580 (guint) ceil (color.blue * 65536 - color.blue));
4581 attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4584 gtk_style_context_get_color (context, flags, &color);
4585 value = g_strdup_printf ("%u,%u,%u",
4586 (guint) ceil (color.red * 65536 - color.red),
4587 (guint) ceil (color.green * 65536 - color.green),
4588 (guint) ceil (color.blue * 65536 - color.blue));
4589 attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);