]> Pileus Git - ~andy/gtk/blob - gtk/gtkstylecontext.c
stylecontext: Make StyleData refcounted
[~andy/gtk] / gtk / gtkstylecontext.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include "config.h"
19
20 #include <gdk/gdk.h>
21 #include <math.h>
22 #include <stdlib.h>
23 #include <gobject/gvaluecollector.h>
24
25 #include "gtkstylecontextprivate.h"
26 #include "gtkcontainerprivate.h"
27 #include "gtkcssenginevalueprivate.h"
28 #include "gtkcssrgbavalueprivate.h"
29 #include "gtkstylepropertiesprivate.h"
30 #include "gtktypebuiltins.h"
31 #include "gtkthemingengineprivate.h"
32 #include "gtkintl.h"
33 #include "gtkwidget.h"
34 #include "gtkwindow.h"
35 #include "gtkprivate.h"
36 #include "gtksymboliccolorprivate.h"
37 #include "gtkcssnumbervalueprivate.h"
38 #include "gtkiconfactory.h"
39 #include "gtkwidgetpath.h"
40 #include "gtkwidgetprivate.h"
41 #include "gtkstylecascadeprivate.h"
42 #include "gtkstyleproviderprivate.h"
43 #include "gtksettings.h"
44
45 /**
46  * SECTION:gtkstylecontext
47  * @Short_description: Rendering UI elements
48  * @Title: GtkStyleContext
49  *
50  * #GtkStyleContext is an object that stores styling information affecting
51  * a widget defined by #GtkWidgetPath.
52  *
53  * In order to construct the final style information, #GtkStyleContext
54  * queries information from all attached #GtkStyleProviders. Style providers
55  * can be either attached explicitly to the context through
56  * gtk_style_context_add_provider(), or to the screen through
57  * gtk_style_context_add_provider_for_screen(). The resulting style is a
58  * combination of all providers' information in priority order.
59  *
60  * For GTK+ widgets, any #GtkStyleContext returned by
61  * gtk_widget_get_style_context() will already have a #GtkWidgetPath, a
62  * #GdkScreen and RTL/LTR information set. The style context will be also
63  * updated automatically if any of these settings change on the widget.
64  *
65  * If you are using the theming layer standalone, you will need to set a
66  * widget path and a screen yourself to the created style context through
67  * gtk_style_context_set_path() and gtk_style_context_set_screen(), as well
68  * as updating the context yourself using gtk_style_context_invalidate()
69  * whenever any of the conditions change, such as a change in the
70  * #GtkSettings:gtk-theme-name setting or a hierarchy change in the rendered
71  * widget.
72  *
73  * <refsect2 id="gtkstylecontext-animations">
74  * <title>Transition animations</title>
75  * <para>
76  * #GtkStyleContext has built-in support for state change transitions.
77  * Note that these animations respect the #GtkSettings:gtk-enable-animations
78  * setting.
79  * </para>
80  * <para>
81  * For simple widgets where state changes affect the whole widget area,
82  * calling gtk_style_context_notify_state_change() with a %NULL region
83  * is sufficient to trigger the transition animation. And GTK+ already
84  * does that when gtk_widget_set_state() or gtk_widget_set_state_flags()
85  * are called.
86  * </para>
87  * <para>
88  * If a widget needs to declare several animatable regions (i.e. not
89  * affecting the whole widget area), its #GtkWidget::draw signal handler
90  * needs to wrap the render operations for the different regions with
91  * calls to gtk_style_context_push_animatable_region() and
92  * gtk_style_context_pop_animatable_region(). These functions take an
93  * identifier for the region which must be unique within the style context.
94  * For simple widgets with a fixed set of animatable regions, using an
95  * enumeration works well:
96  * </para>
97  * <example>
98  * <title>Using an enumeration to identify  animatable regions</title>
99  * <programlisting>
100  * enum {
101  *   REGION_ENTRY,
102  *   REGION_BUTTON_UP,
103  *   REGION_BUTTON_DOWN
104  * };
105  *
106  * ...
107  *
108  * gboolean
109  * spin_button_draw (GtkWidget *widget,
110  *                   cairo_t   *cr)
111  * {
112  *   GtkStyleContext *context;
113  *
114  *   context = gtk_widget_get_style_context (widget);
115  *
116  *   gtk_style_context_push_animatable_region (context,
117  *                                             GUINT_TO_POINTER (REGION_ENTRY));
118  *
119  *   gtk_render_background (cr, 0, 0, 100, 30);
120  *   gtk_render_frame (cr, 0, 0, 100, 30);
121  *
122  *   gtk_style_context_pop_animatable_region (context);
123  *
124  *   ...
125  * }
126  * </programlisting>
127  * </example>
128  * <para>
129  * For complex widgets with an arbitrary number of animatable regions, it
130  * is up to the implementation to come up with a way to uniquely identify
131  * each animatable region. Using pointers to internal structs is one way
132  * to achieve this:
133  * </para>
134  * <example>
135  * <title>Using struct pointers to identify animatable regions</title>
136  * <programlisting>
137  * void
138  * notebook_draw_tab (GtkWidget    *widget,
139  *                    NotebookPage *page,
140  *                    cairo_t      *cr)
141  * {
142  *   gtk_style_context_push_animatable_region (context, page);
143  *   gtk_render_extension (cr, page->x, page->y, page->width, page->height);
144  *   gtk_style_context_pop_animatable_region (context);
145  * }
146  * </programlisting>
147  * </example>
148  * <para>
149  * The widget also needs to notify the style context about a state change
150  * for a given animatable region so the animation is triggered.
151  * </para>
152  * <example>
153  * <title>Triggering a state change animation on a region</title>
154  * <programlisting>
155  * gboolean
156  * notebook_motion_notify (GtkWidget      *widget,
157  *                         GdkEventMotion *event)
158  * {
159  *   GtkStyleContext *context;
160  *   NotebookPage *page;
161  *
162  *   context = gtk_widget_get_style_context (widget);
163  *   page = find_page_under_pointer (widget, event);
164  *   gtk_style_context_notify_state_change (context,
165  *                                          gtk_widget_get_window (widget),
166  *                                          page,
167  *                                          GTK_STATE_PRELIGHT,
168  *                                          TRUE);
169  *   ...
170  * }
171  * </programlisting>
172  * </example>
173  * <para>
174  * gtk_style_context_notify_state_change() accepts %NULL region IDs as a
175  * special value, in this case, the whole widget area will be updated
176  * by the animation.
177  * </para>
178  * </refsect2>
179  * <refsect2 id="gtkstylecontext-classes">
180  * <title>Style classes and regions</title>
181  * <para>
182  * Widgets can add style classes to their context, which can be used
183  * to associate different styles by class (see <xref linkend="gtkcssprovider-selectors"/>). Theme engines can also use style classes to vary their
184  * rendering. GTK+ has a number of predefined style classes:
185  * #GTK_STYLE_CLASS_CELL,
186  * #GTK_STYLE_CLASS_ENTRY,
187  * #GTK_STYLE_CLASS_BUTTON,
188  * #GTK_STYLE_CLASS_COMBOBOX_ENTRY,
189  * #GTK_STYLE_CLASS_CALENDAR,
190  * #GTK_STYLE_CLASS_SLIDER,
191  * #GTK_STYLE_CLASS_BACKGROUND,
192  * #GTK_STYLE_CLASS_RUBBERBAND,
193  * #GTK_STYLE_CLASS_TOOLTIP,
194  * #GTK_STYLE_CLASS_MENU,
195  * #GTK_STYLE_CLASS_MENUBAR,
196  * #GTK_STYLE_CLASS_MENUITEM,
197  * #GTK_STYLE_CLASS_TOOLBAR,
198  * #GTK_STYLE_CLASS_PRIMARY_TOOLBAR,
199  * #GTK_STYLE_CLASS_INLINE_TOOLBAR,
200  * #GTK_STYLE_CLASS_RADIO,
201  * #GTK_STYLE_CLASS_CHECK,
202  * #GTK_STYLE_CLASS_TROUGH,
203  * #GTK_STYLE_CLASS_SCROLLBAR,
204  * #GTK_STYLE_CLASS_SCALE,
205  * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_ABOVE,
206  * #GTK_STYLE_CLASS_SCALE_HAS_MARKS_BELOW,
207  * #GTK_STYLE_CLASS_HEADER,
208  * #GTK_STYLE_CLASS_ACCELERATOR,
209  * #GTK_STYLE_CLASS_GRIP,
210  * #GTK_STYLE_CLASS_DOCK,
211  * #GTK_STYLE_CLASS_PROGRESSBAR,
212  * #GTK_STYLE_CLASS_SPINNER,
213  * #GTK_STYLE_CLASS_EXPANDER,
214  * #GTK_STYLE_CLASS_SPINBUTTON,
215  * #GTK_STYLE_CLASS_NOTEBOOK,
216  * #GTK_STYLE_CLASS_VIEW,
217  * #GTK_STYLE_CLASS_SIDEBAR,
218  * #GTK_STYLE_CLASS_IMAGE,
219  * #GTK_STYLE_CLASS_HIGHLIGHT,
220  * #GTK_STYLE_CLASS_FRAME,
221  * #GTK_STYLE_CLASS_DND,
222  * #GTK_STYLE_CLASS_PANE_SEPARATOR,
223  * #GTK_STYLE_CLASS_SEPARATOR,
224  * #GTK_STYLE_CLASS_INFO,
225  * #GTK_STYLE_CLASS_WARNING,
226  * #GTK_STYLE_CLASS_QUESTION,
227  * #GTK_STYLE_CLASS_ERROR,
228  * #GTK_STYLE_CLASS_HORIZONTAL,
229  * #GTK_STYLE_CLASS_VERTICAL,
230  * #GTK_STYLE_CLASS_TOP,
231  * #GTK_STYLE_CLASS_BOTTOM,
232  * #GTK_STYLE_CLASS_LEFT,
233  * #GTK_STYLE_CLASS_RIGHT,
234  * </para>
235  * <para>
236  * Widgets can also add regions with flags to their context.
237  * The regions used by GTK+ widgets are:
238  * <informaltable>
239  *   <tgroup cols="4">
240  *     <thead>
241  *       <row>
242  *         <entry>Region</entry>
243  *         <entry>Flags</entry>
244  *         <entry>Macro</entry>
245  *         <entry>Used by</entry>
246  *       </row>
247  *     </thead>
248  *     <tbody>
249  *       <row>
250  *         <entry>row</entry>
251  *         <entry>even, odd</entry>
252  *         <entry>GTK_STYLE_REGION_ROW</entry>
253  *         <entry>#GtkTreeView</entry>
254  *       </row>
255  *       <row>
256  *         <entry>column</entry>
257  *         <entry>first, last, sorted</entry>
258  *         <entry>GTK_STYLE_REGION_COLUMN</entry>
259  *         <entry>#GtkTreeView</entry>
260  *       </row>
261  *       <row>
262  *         <entry>column-header</entry>
263  *         <entry></entry>
264  *         <entry>GTK_STYLE_REGION_COLUMN_HEADER</entry>
265  *         <entry></entry>
266  *       </row>
267  *       <row>
268  *         <entry>tab</entry>
269  *         <entry>even, odd, first, last</entry>
270  *         <entry>GTK_STYLE_REGION_TAB</entry>
271  *         <entry>#GtkNotebook</entry>
272  *       </row>
273  *     </tbody>
274  *   </tgroup>
275  * </informaltable>
276  * </para>
277  * </refsect2>
278  * <refsect2 id="gtkstylecontext-custom-styling">
279  * <title>Custom styling in UI libraries and applications</title>
280  * <para>
281  * If you are developing a library with custom #GtkWidget<!-- -->s that
282  * render differently than standard components, you may need to add a
283  * #GtkStyleProvider yourself with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK
284  * priority, either a #GtkCssProvider or a custom object implementing the
285  * #GtkStyleProvider interface. This way theming engines may still attempt
286  * to style your UI elements in a different way if needed so.
287  * </para>
288  * <para>
289  * If you are using custom styling on an applications, you probably want then
290  * to make your style information prevail to the theme's, so you must use
291  * a #GtkStyleProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
292  * priority, keep in mind that the user settings in
293  * <filename><replaceable>XDG_CONFIG_HOME</replaceable>/gtk-3.0/gtk.css</filename> will
294  * still take precedence over your changes, as it uses the
295  * %GTK_STYLE_PROVIDER_PRIORITY_USER priority.
296  * </para>
297  * <para>
298  * If a custom theming engine is needed, you probably want to implement a
299  * #GtkStyleProvider yourself so it points to your #GtkThemingEngine
300  * implementation, as #GtkCssProvider uses gtk_theming_engine_load()
301  * which loads the theming engine module from the standard paths.
302  * </para>
303  * </refsect2>
304  */
305
306 /* When these change we do a full restyling. Otherwise we try to figure out
307  * if we need to change things. */
308 #define GTK_STYLE_CONTEXT_RADICAL_CHANGE (GTK_CSS_CHANGE_NAME | GTK_CSS_CHANGE_CLASS | GTK_CSS_CHANGE_SOURCE)
309 /* When these change we don't clear the cache. This takes more memory but makes
310  * things go faster. */
311 #define GTK_STYLE_CONTEXT_CACHED_CHANGE (GTK_CSS_CHANGE_STATE)
312
313 typedef struct GtkStyleInfo GtkStyleInfo;
314 typedef struct GtkRegion GtkRegion;
315 typedef struct PropertyValue PropertyValue;
316 typedef struct StyleData StyleData;
317
318 struct GtkRegion
319 {
320   GQuark class_quark;
321   GtkRegionFlags flags;
322 };
323
324 struct PropertyValue
325 {
326   GType       widget_type;
327   GParamSpec *pspec;
328   GtkStateFlags state;
329   GValue      value;
330 };
331
332 struct GtkStyleInfo
333 {
334   GArray *style_classes;
335   GArray *regions;
336   GtkJunctionSides junction_sides;
337   GtkStateFlags state_flags;
338   StyleData *data;
339 };
340
341 struct StyleData
342 {
343   GtkCssComputedValues *store;
344   GArray *property_cache;
345   guint ref_count;
346 };
347
348 struct _GtkStyleContextPrivate
349 {
350   GdkScreen *screen;
351
352   GtkStyleCascade *cascade;
353
354   GtkStyleContext *animation_list_prev;
355   GtkStyleContext *animation_list_next;
356
357   GtkStyleContext *parent;
358   GSList *children;
359   GtkWidget *widget;            
360   GtkWidgetPath *widget_path;
361   GHashTable *style_data;
362   GSList *info_stack;
363
364   GtkTextDirection direction;
365
366   GtkCssChange relevant_changes;
367   GtkCssChange pending_changes;
368
369   guint invalidating_context : 1;
370   guint invalid : 1;
371 };
372
373 enum {
374   PROP_0,
375   PROP_SCREEN,
376   PROP_DIRECTION,
377   PROP_PARENT
378 };
379
380 enum {
381   CHANGED,
382   LAST_SIGNAL
383 };
384
385 static guint signals[LAST_SIGNAL] = { 0 };
386 static GtkStyleContext *_running_animations = NULL;
387 guint _running_animations_timer_id = 0;
388
389 static void gtk_style_context_finalize (GObject *object);
390
391 static void gtk_style_context_impl_set_property (GObject      *object,
392                                                  guint         prop_id,
393                                                  const GValue *value,
394                                                  GParamSpec   *pspec);
395 static void gtk_style_context_impl_get_property (GObject      *object,
396                                                  guint         prop_id,
397                                                  GValue       *value,
398                                                  GParamSpec   *pspec);
399 static GtkSymbolicColor *
400             gtk_style_context_color_lookup_func (gpointer      contextp,
401                                                  const char   *name);
402
403
404 G_DEFINE_TYPE (GtkStyleContext, gtk_style_context, G_TYPE_OBJECT)
405
406 static void
407 gtk_style_context_real_changed (GtkStyleContext *context)
408 {
409   GtkStyleContextPrivate *priv = context->priv;
410
411   if (priv->widget)
412     _gtk_widget_style_context_invalidated (priv->widget);
413 }
414
415 static void
416 gtk_style_context_class_init (GtkStyleContextClass *klass)
417 {
418   GObjectClass *object_class = G_OBJECT_CLASS (klass);
419
420   object_class->finalize = gtk_style_context_finalize;
421   object_class->set_property = gtk_style_context_impl_set_property;
422   object_class->get_property = gtk_style_context_impl_get_property;
423
424   klass->changed = gtk_style_context_real_changed;
425
426   signals[CHANGED] =
427     g_signal_new (I_("changed"),
428                   G_TYPE_FROM_CLASS (object_class),
429                   G_SIGNAL_RUN_FIRST,
430                   G_STRUCT_OFFSET (GtkStyleContextClass, changed),
431                   NULL, NULL,
432                   g_cclosure_marshal_VOID__VOID,
433                   G_TYPE_NONE, 0);
434
435   g_object_class_install_property (object_class,
436                                    PROP_SCREEN,
437                                    g_param_spec_object ("screen",
438                                                         P_("Screen"),
439                                                         P_("The associated GdkScreen"),
440                                                         GDK_TYPE_SCREEN,
441                                                         GTK_PARAM_READWRITE));
442   g_object_class_install_property (object_class,
443                                    PROP_DIRECTION,
444                                    g_param_spec_enum ("direction",
445                                                       P_("Direction"),
446                                                       P_("Text direction"),
447                                                       GTK_TYPE_TEXT_DIRECTION,
448                                                       GTK_TEXT_DIR_LTR,
449                                                       GTK_PARAM_READWRITE));
450   /**
451    * GtkStyleContext:parent:
452    *
453    * Sets or gets the style context's parent. See gtk_style_context_set_parent()
454    * for details.
455    *
456    * Since: 3.4
457    */
458   g_object_class_install_property (object_class,
459                                    PROP_PARENT,
460                                    g_param_spec_object ("parent",
461                                                         P_("Parent"),
462                                                         P_("The parent style context"),
463                                                         GTK_TYPE_STYLE_CONTEXT,
464                                                         GTK_PARAM_READWRITE));
465
466   g_type_class_add_private (object_class, sizeof (GtkStyleContextPrivate));
467 }
468
469 static StyleData *
470 style_data_new (void)
471 {
472   StyleData *data;
473
474   data = g_slice_new0 (StyleData);
475   data->ref_count = 1;
476
477   return data;
478 }
479
480 static void
481 clear_property_cache (StyleData *data)
482 {
483   guint i;
484
485   if (!data->property_cache)
486     return;
487
488   for (i = 0; i < data->property_cache->len; i++)
489     {
490       PropertyValue *node = &g_array_index (data->property_cache, PropertyValue, i);
491
492       g_param_spec_unref (node->pspec);
493       g_value_unset (&node->value);
494     }
495
496   g_array_free (data->property_cache, TRUE);
497   data->property_cache = NULL;
498 }
499
500 static StyleData *
501 style_data_ref (StyleData *style_data)
502 {
503   style_data->ref_count++;
504
505   return style_data;
506 }
507
508 static void
509 style_data_unref (StyleData *data)
510 {
511   data->ref_count--;
512
513   if (data->ref_count > 0)
514     return;
515
516   g_object_unref (data->store);
517   clear_property_cache (data);
518
519   g_slice_free (StyleData, data);
520 }
521
522 static GtkStyleInfo *
523 style_info_new (void)
524 {
525   GtkStyleInfo *info;
526
527   info = g_slice_new0 (GtkStyleInfo);
528   info->style_classes = g_array_new (FALSE, FALSE, sizeof (GQuark));
529   info->regions = g_array_new (FALSE, FALSE, sizeof (GtkRegion));
530
531   return info;
532 }
533
534 static void
535 style_info_free (GtkStyleInfo *info)
536 {
537   if (info->data)
538     style_data_unref (info->data);
539   g_array_free (info->style_classes, TRUE);
540   g_array_free (info->regions, TRUE);
541   g_slice_free (GtkStyleInfo, info);
542 }
543
544 static GtkStyleInfo *
545 style_info_copy (const GtkStyleInfo *info)
546 {
547   GtkStyleInfo *copy;
548
549   copy = style_info_new ();
550   g_array_insert_vals (copy->style_classes, 0,
551                        info->style_classes->data,
552                        info->style_classes->len);
553
554   g_array_insert_vals (copy->regions, 0,
555                        info->regions->data,
556                        info->regions->len);
557
558   copy->junction_sides = info->junction_sides;
559   copy->state_flags = info->state_flags;
560   if (info->data)
561     copy->data = style_data_ref (info->data);
562
563   return copy;
564 }
565
566 static guint
567 style_info_hash (gconstpointer elem)
568 {
569   const GtkStyleInfo *info;
570   guint i, hash = 0;
571
572   info = elem;
573
574   for (i = 0; i < info->style_classes->len; i++)
575     {
576       hash += g_array_index (info->style_classes, GQuark, i);
577       hash <<= 5;
578     }
579
580   for (i = 0; i < info->regions->len; i++)
581     {
582       GtkRegion *region;
583
584       region = &g_array_index (info->regions, GtkRegion, i);
585       hash += region->class_quark;
586       hash += region->flags;
587       hash <<= 5;
588     }
589
590   return hash ^ info->state_flags;
591 }
592
593 static gboolean
594 style_info_equal (gconstpointer elem1,
595                   gconstpointer elem2)
596 {
597   const GtkStyleInfo *info1, *info2;
598
599   info1 = elem1;
600   info2 = elem2;
601
602   if (info1->state_flags != info2->state_flags)
603     return FALSE;
604
605   if (info1->junction_sides != info2->junction_sides)
606     return FALSE;
607
608   if (info1->style_classes->len != info2->style_classes->len)
609     return FALSE;
610
611   if (memcmp (info1->style_classes->data,
612               info2->style_classes->data,
613               info1->style_classes->len * sizeof (GQuark)) != 0)
614     return FALSE;
615
616   if (info1->regions->len != info2->regions->len)
617     return FALSE;
618
619   if (memcmp (info1->regions->data,
620               info2->regions->data,
621               info1->regions->len * sizeof (GtkRegion)) != 0)
622     return FALSE;
623
624   return TRUE;
625 }
626
627 static void
628 gtk_style_context_cascade_changed (GtkStyleCascade *cascade,
629                                    GtkStyleContext *context)
630 {
631   GtkStyleContextPrivate *priv = context->priv;
632
633   if (priv->widget)
634     _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_SOURCE);
635   else
636     gtk_style_context_invalidate (context);
637 }
638
639 static void
640 gtk_style_context_set_cascade (GtkStyleContext *context,
641                                GtkStyleCascade *cascade)
642 {
643   GtkStyleContextPrivate *priv;
644
645   priv = context->priv;
646
647   if (priv->cascade == cascade)
648     return;
649
650   if (cascade)
651     {
652       g_object_ref (cascade);
653       g_signal_connect (cascade,
654                         "-gtk-private-changed",
655                         G_CALLBACK (gtk_style_context_cascade_changed),
656                         context);
657     }
658
659   if (priv->cascade)
660     {
661       g_signal_handlers_disconnect_by_func (priv->cascade, 
662                                             gtk_style_context_cascade_changed,
663                                             context);
664       g_object_unref (priv->cascade);
665     }
666
667   priv->cascade = cascade;
668
669   if (cascade)
670     gtk_style_context_cascade_changed (cascade, context);
671 }
672
673 static void
674 gtk_style_context_init (GtkStyleContext *style_context)
675 {
676   GtkStyleContextPrivate *priv;
677   GtkStyleInfo *info;
678
679   priv = style_context->priv = G_TYPE_INSTANCE_GET_PRIVATE (style_context,
680                                                             GTK_TYPE_STYLE_CONTEXT,
681                                                             GtkStyleContextPrivate);
682
683   priv->style_data = g_hash_table_new_full (style_info_hash,
684                                             style_info_equal,
685                                             (GDestroyNotify) style_info_free,
686                                             (GDestroyNotify) style_data_unref);
687
688   priv->direction = GTK_TEXT_DIR_LTR;
689
690   priv->screen = gdk_screen_get_default ();
691   priv->relevant_changes = GTK_CSS_CHANGE_ANY;
692
693   /* Create default info store */
694   info = style_info_new ();
695   priv->info_stack = g_slist_prepend (priv->info_stack, info);
696
697   gtk_style_context_set_cascade (style_context,
698                                  _gtk_style_cascade_get_for_screen (priv->screen));
699 }
700
701 static gboolean
702 gtk_style_context_do_animations (gpointer unused)
703 {
704   GtkStyleContext *context;
705
706   for (context = _running_animations;
707        context != NULL;
708        context = context->priv->animation_list_next)
709     {
710       _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANIMATE);
711     }
712
713   return TRUE;
714 }
715
716 static gboolean
717 gtk_style_context_is_animating (GtkStyleContext *context)
718 {
719   GtkStyleContextPrivate *priv = context->priv;
720
721   return priv->animation_list_prev != NULL
722       || _running_animations == context;
723 }
724
725 static void
726 gtk_style_context_stop_animating (GtkStyleContext *context)
727 {
728   GtkStyleContextPrivate *priv = context->priv;
729
730   if (!gtk_style_context_is_animating (context))
731     return;
732
733   if (priv->animation_list_prev == NULL)
734     {
735       _running_animations = priv->animation_list_next;
736
737       if (_running_animations == NULL)
738         {
739           /* we were the last animation */
740           g_source_remove (_running_animations_timer_id);
741           _running_animations_timer_id = 0;
742         }
743     }
744   else
745     priv->animation_list_prev->priv->animation_list_next = priv->animation_list_next;
746
747   if (priv->animation_list_next)
748     priv->animation_list_next->priv->animation_list_prev = priv->animation_list_prev;
749
750   priv->animation_list_next = NULL;
751   priv->animation_list_prev = NULL;
752 }
753
754 static void G_GNUC_UNUSED
755 gtk_style_context_start_animating (GtkStyleContext *context)
756 {
757   GtkStyleContextPrivate *priv = context->priv;
758
759   if (gtk_style_context_is_animating (context))
760     return;
761
762   if (_running_animations == NULL)
763     {
764       _running_animations_timer_id = gdk_threads_add_timeout (25,
765                                                               gtk_style_context_do_animations,
766                                                               NULL);
767       _running_animations = context;
768     }
769   else
770     {
771       priv->animation_list_next = _running_animations;
772       _running_animations->priv->animation_list_prev = context;
773       _running_animations = context;
774     }
775 }
776
777 static void
778 gtk_style_context_finalize (GObject *object)
779 {
780   GtkStyleContextPrivate *priv;
781   GtkStyleContext *style_context;
782
783   style_context = GTK_STYLE_CONTEXT (object);
784   priv = style_context->priv;
785
786   _gtk_style_context_stop_animations (style_context);
787
788   /* children hold a reference to us */
789   g_assert (priv->children == NULL);
790
791   gtk_style_context_set_parent (style_context, NULL);
792
793   gtk_style_context_set_cascade (style_context, NULL);
794
795   if (priv->widget_path)
796     gtk_widget_path_free (priv->widget_path);
797
798   g_hash_table_destroy (priv->style_data);
799
800   g_slist_free_full (priv->info_stack, (GDestroyNotify) style_info_free);
801
802   G_OBJECT_CLASS (gtk_style_context_parent_class)->finalize (object);
803 }
804
805 static void
806 gtk_style_context_impl_set_property (GObject      *object,
807                                      guint         prop_id,
808                                      const GValue *value,
809                                      GParamSpec   *pspec)
810 {
811   GtkStyleContext *style_context;
812
813   style_context = GTK_STYLE_CONTEXT (object);
814
815   switch (prop_id)
816     {
817     case PROP_SCREEN:
818       gtk_style_context_set_screen (style_context,
819                                     g_value_get_object (value));
820       break;
821     case PROP_DIRECTION:
822       gtk_style_context_set_direction (style_context,
823                                        g_value_get_enum (value));
824       break;
825     case PROP_PARENT:
826       gtk_style_context_set_parent (style_context,
827                                     g_value_get_object (value));
828       break;
829     default:
830       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
831       break;
832     }
833 }
834
835 static void
836 gtk_style_context_impl_get_property (GObject    *object,
837                                      guint       prop_id,
838                                      GValue     *value,
839                                      GParamSpec *pspec)
840 {
841   GtkStyleContext *style_context;
842   GtkStyleContextPrivate *priv;
843
844   style_context = GTK_STYLE_CONTEXT (object);
845   priv = style_context->priv;
846
847   switch (prop_id)
848     {
849     case PROP_SCREEN:
850       g_value_set_object (value, priv->screen);
851       break;
852     case PROP_DIRECTION:
853       g_value_set_enum (value, priv->direction);
854       break;
855     case PROP_PARENT:
856       g_value_set_object (value, priv->parent);
857       break;
858     default:
859       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
860       break;
861     }
862 }
863
864 static void
865 build_properties (GtkStyleContext *context,
866                   StyleData       *style_data,
867                   GtkWidgetPath   *path,
868                   GtkStateFlags    state)
869 {
870   GtkStyleContextPrivate *priv;
871   GtkCssMatcher matcher;
872   GtkCssLookup *lookup;
873
874   priv = context->priv;
875
876   _gtk_css_matcher_init (&matcher, path, state);
877   lookup = _gtk_css_lookup_new ();
878
879   _gtk_style_provider_private_lookup (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
880                                       &matcher,
881                                       lookup);
882
883   style_data->store = _gtk_css_computed_values_new ();
884   _gtk_css_lookup_resolve (lookup, context, style_data->store);
885   _gtk_css_lookup_free (lookup);
886 }
887
888 static GtkWidgetPath *
889 create_query_path (GtkStyleContext *context)
890 {
891   GtkStyleContextPrivate *priv;
892   GtkWidgetPath *path;
893   GtkStyleInfo *info;
894   guint i, pos;
895
896   priv = context->priv;
897   path = priv->widget ? _gtk_widget_create_path (priv->widget) : gtk_widget_path_copy (priv->widget_path);
898   pos = gtk_widget_path_length (path) - 1;
899
900   info = priv->info_stack->data;
901
902   /* Set widget regions */
903   for (i = 0; i < info->regions->len; i++)
904     {
905       GtkRegion *region;
906
907       region = &g_array_index (info->regions, GtkRegion, i);
908       gtk_widget_path_iter_add_region (path, pos,
909                                        g_quark_to_string (region->class_quark),
910                                        region->flags);
911     }
912
913   /* Set widget classes */
914   for (i = 0; i < info->style_classes->len; i++)
915     {
916       GQuark quark;
917
918       quark = g_array_index (info->style_classes, GQuark, i);
919       gtk_widget_path_iter_add_class (path, pos,
920                                       g_quark_to_string (quark));
921     }
922
923   return path;
924 }
925
926 static StyleData *
927 style_data_lookup (GtkStyleContext *context)
928 {
929   GtkStyleContextPrivate *priv;
930   GtkStyleInfo *info;
931
932   priv = context->priv;
933   info = priv->info_stack->data;
934
935   /* Current data in use is cached, just return it */
936   if (info->data)
937     return info->data;
938
939   g_assert (priv->widget != NULL || priv->widget_path != NULL);
940
941   info->data = g_hash_table_lookup (priv->style_data, info);
942
943   if (info->data)
944     {
945       style_data_ref (info->data);
946     }
947   else
948     {
949       GtkWidgetPath *path;
950
951       path = create_query_path (context);
952
953       info->data = style_data_new ();
954       g_hash_table_insert (priv->style_data,
955                            style_info_copy (info),
956                            style_data_ref (info->data));
957
958       build_properties (context, info->data, path, info->state_flags);
959
960       gtk_widget_path_free (path);
961     }
962
963   return info->data;
964 }
965
966 static void
967 gtk_style_context_set_invalid (GtkStyleContext *context,
968                                gboolean         invalid)
969 {
970   GtkStyleContextPrivate *priv;
971   
972   priv = context->priv;
973
974   if (priv->invalid == invalid)
975     return;
976
977   priv->invalid = invalid;
978
979   if (invalid)
980     {
981       if (priv->parent)
982         gtk_style_context_set_invalid (priv->parent, TRUE);
983       else if (GTK_IS_RESIZE_CONTAINER (priv->widget))
984         _gtk_container_queue_resize_handler (GTK_CONTAINER (priv->widget));
985     }
986 }
987
988 /* returns TRUE if someone called gtk_style_context_save() but hasn't
989  * called gtk_style_context_restore() yet.
990  * In those situations we don't invalidate the context when somebody
991  * changes state/regions/classes.
992  */
993 static gboolean
994 gtk_style_context_is_saved (GtkStyleContext *context)
995 {
996   return context->priv->info_stack->next != NULL;
997 }
998
999 static void
1000 gtk_style_context_queue_invalidate_internal (GtkStyleContext *context,
1001                                              GtkCssChange     change)
1002 {
1003   GtkStyleContextPrivate *priv = context->priv;
1004   GtkStyleInfo *info = priv->info_stack->data;
1005
1006   if (gtk_style_context_is_saved (context))
1007     {
1008       if (info->data)
1009         {
1010           style_data_unref (info->data);
1011           info->data = NULL;
1012         }
1013     }
1014   else
1015     {
1016       _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_STATE);
1017       /* XXX: We need to invalidate siblings here somehow */
1018     }
1019 }
1020
1021 /**
1022  * gtk_style_context_new:
1023  *
1024  * Creates a standalone #GtkStyleContext, this style context
1025  * won't be attached to any widget, so you may want
1026  * to call gtk_style_context_set_path() yourself.
1027  *
1028  * <note>
1029  * This function is only useful when using the theming layer
1030  * separated from GTK+, if you are using #GtkStyleContext to
1031  * theme #GtkWidget<!-- -->s, use gtk_widget_get_style_context()
1032  * in order to get a style context ready to theme the widget.
1033  * </note>
1034  *
1035  * Returns: A newly created #GtkStyleContext.
1036  **/
1037 GtkStyleContext *
1038 gtk_style_context_new (void)
1039 {
1040   return g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
1041 }
1042
1043 void
1044 _gtk_style_context_set_widget (GtkStyleContext *context,
1045                                GtkWidget       *widget)
1046 {
1047   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1048   g_return_if_fail (widget == NULL || GTK_IS_WIDGET (widget));
1049
1050   context->priv->widget = widget;
1051
1052   _gtk_style_context_stop_animations (context);
1053
1054   _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_SELF);
1055 }
1056
1057 /**
1058  * gtk_style_context_add_provider:
1059  * @context: a #GtkStyleContext
1060  * @provider: a #GtkStyleProvider
1061  * @priority: the priority of the style provider. The lower
1062  *            it is, the earlier it will be used in the style
1063  *            construction. Typically this will be in the range
1064  *            between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1065  *            %GTK_STYLE_PROVIDER_PRIORITY_USER
1066  *
1067  * Adds a style provider to @context, to be used in style construction.
1068  *
1069  * <note><para>If both priorities are the same, A #GtkStyleProvider
1070  * added through this function takes precedence over another added
1071  * through gtk_style_context_add_provider_for_screen().</para></note>
1072  *
1073  * Since: 3.0
1074  **/
1075 void
1076 gtk_style_context_add_provider (GtkStyleContext  *context,
1077                                 GtkStyleProvider *provider,
1078                                 guint             priority)
1079 {
1080   GtkStyleContextPrivate *priv;
1081
1082   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1083   g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1084
1085   priv = context->priv;
1086
1087   if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1088     {
1089       GtkStyleCascade *new_cascade;
1090       
1091       new_cascade = _gtk_style_cascade_new ();
1092       _gtk_style_cascade_set_parent (new_cascade, priv->cascade);
1093       _gtk_style_cascade_add_provider (new_cascade, provider, priority);
1094       gtk_style_context_set_cascade (context, new_cascade);
1095       g_object_unref (new_cascade);
1096     }
1097   else
1098     {
1099       _gtk_style_cascade_add_provider (priv->cascade, provider, priority);
1100     }
1101 }
1102
1103 /**
1104  * gtk_style_context_remove_provider:
1105  * @context: a #GtkStyleContext
1106  * @provider: a #GtkStyleProvider
1107  *
1108  * Removes @provider from the style providers list in @context.
1109  *
1110  * Since: 3.0
1111  **/
1112 void
1113 gtk_style_context_remove_provider (GtkStyleContext  *context,
1114                                    GtkStyleProvider *provider)
1115 {
1116   GtkStyleContextPrivate *priv;
1117
1118   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1119   g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1120
1121   priv = context->priv;
1122
1123   if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
1124     return;
1125
1126   _gtk_style_cascade_remove_provider (priv->cascade, provider);
1127 }
1128
1129 /**
1130  * gtk_style_context_reset_widgets:
1131  * @screen: a #GdkScreen
1132  *
1133  * This function recomputes the styles for all widgets under a particular
1134  * #GdkScreen. This is useful when some global parameter has changed that
1135  * affects the appearance of all widgets, because when a widget gets a new
1136  * style, it will both redraw and recompute any cached information about
1137  * its appearance. As an example, it is used when the color scheme changes
1138  * in the related #GtkSettings object.
1139  *
1140  * Since: 3.0
1141  **/
1142 void
1143 gtk_style_context_reset_widgets (GdkScreen *screen)
1144 {
1145   GList *list, *toplevels;
1146
1147   _gtk_icon_set_invalidate_caches ();
1148
1149   toplevels = gtk_window_list_toplevels ();
1150   g_list_foreach (toplevels, (GFunc) g_object_ref, NULL);
1151
1152   for (list = toplevels; list; list = list->next)
1153     {
1154       if (gtk_widget_get_screen (list->data) == screen)
1155         gtk_widget_reset_style (list->data);
1156
1157       g_object_unref (list->data);
1158     }
1159
1160   g_list_free (toplevels);
1161 }
1162
1163 /**
1164  * gtk_style_context_add_provider_for_screen:
1165  * @screen: a #GdkScreen
1166  * @provider: a #GtkStyleProvider
1167  * @priority: the priority of the style provider. The lower
1168  *            it is, the earlier it will be used in the style
1169  *            construction. Typically this will be in the range
1170  *            between %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK and
1171  *            %GTK_STYLE_PROVIDER_PRIORITY_USER
1172  *
1173  * Adds a global style provider to @screen, which will be used
1174  * in style construction for all #GtkStyleContext<!-- -->s under
1175  * @screen.
1176  *
1177  * GTK+ uses this to make styling information from #GtkSettings
1178  * available.
1179  *
1180  * <note><para>If both priorities are the same, A #GtkStyleProvider
1181  * added through gtk_style_context_add_provider() takes precedence
1182  * over another added through this function.</para></note>
1183  *
1184  * Since: 3.0
1185  **/
1186 void
1187 gtk_style_context_add_provider_for_screen (GdkScreen        *screen,
1188                                            GtkStyleProvider *provider,
1189                                            guint             priority)
1190 {
1191   GtkStyleCascade *cascade;
1192
1193   g_return_if_fail (GDK_IS_SCREEN (screen));
1194   g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1195
1196   cascade = _gtk_style_cascade_get_for_screen (screen);
1197   _gtk_style_cascade_add_provider (cascade, provider, priority);
1198 }
1199
1200 /**
1201  * gtk_style_context_remove_provider_for_screen:
1202  * @screen: a #GdkScreen
1203  * @provider: a #GtkStyleProvider
1204  *
1205  * Removes @provider from the global style providers list in @screen.
1206  *
1207  * Since: 3.0
1208  **/
1209 void
1210 gtk_style_context_remove_provider_for_screen (GdkScreen        *screen,
1211                                               GtkStyleProvider *provider)
1212 {
1213   GtkStyleCascade *cascade;
1214
1215   g_return_if_fail (GDK_IS_SCREEN (screen));
1216   g_return_if_fail (GTK_IS_STYLE_PROVIDER (provider));
1217
1218   cascade = _gtk_style_cascade_get_for_screen (screen);
1219   _gtk_style_cascade_remove_provider (cascade, provider);
1220 }
1221
1222 /**
1223  * gtk_style_context_get_section:
1224  * @context: a #GtkStyleContext
1225  * @property: style property name
1226  *
1227  * Queries the location in the CSS where @property was defined for the
1228  * current @context. Note that the state to be queried is taken from
1229  * gtk_style_context_get_state().
1230  *
1231  * If the location is not available, %NULL will be returned. The
1232  * location might not be available for various reasons, such as the
1233  * property being overridden, @property not naming a supported CSS
1234  * property or tracking of definitions being disabled for performance
1235  * reasons.
1236  *
1237  * Shorthand CSS properties cannot be queried for a location and will
1238  * always return %NULL.
1239  *
1240  * Returns: %NULL or the section where value was defined
1241  **/
1242 GtkCssSection *
1243 gtk_style_context_get_section (GtkStyleContext *context,
1244                                const gchar     *property)
1245 {
1246   GtkStyleContextPrivate *priv;
1247   GtkStyleProperty *prop;
1248   StyleData *data;
1249
1250   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1251   g_return_val_if_fail (property != NULL, NULL);
1252
1253   priv = context->priv;
1254   g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
1255
1256   prop = _gtk_style_property_lookup (property);
1257   if (!GTK_IS_CSS_STYLE_PROPERTY (prop))
1258     return NULL;
1259
1260   data = style_data_lookup (context);
1261   return _gtk_css_computed_values_get_section (data->store, _gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (prop)));
1262 }
1263
1264 static GtkCssValue *
1265 gtk_style_context_query_func (guint    id,
1266                               gpointer values)
1267 {
1268   return _gtk_css_computed_values_get_value (values, id);
1269 }
1270
1271 /**
1272  * gtk_style_context_get_property:
1273  * @context: a #GtkStyleContext
1274  * @property: style property name
1275  * @state: state to retrieve the property value for
1276  * @value: (out) (transfer full):  return location for the style property value
1277  *
1278  * Gets a style property from @context for the given state.
1279  *
1280  * When @value is no longer needed, g_value_unset() must be called
1281  * to free any allocated memory.
1282  *
1283  * Since: 3.0
1284  **/
1285 void
1286 gtk_style_context_get_property (GtkStyleContext *context,
1287                                 const gchar     *property,
1288                                 GtkStateFlags    state,
1289                                 GValue          *value)
1290 {
1291   GtkStyleContextPrivate *priv;
1292   GtkStyleProperty *prop;
1293   StyleData *data;
1294
1295   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1296   g_return_if_fail (property != NULL);
1297   g_return_if_fail (value != NULL);
1298
1299   priv = context->priv;
1300   g_return_if_fail (priv->widget != NULL || priv->widget_path != NULL);
1301
1302   prop = _gtk_style_property_lookup (property);
1303   if (prop == NULL)
1304     {
1305       g_warning ("Style property \"%s\" is not registered", property);
1306       return;
1307     }
1308   if (_gtk_style_property_get_value_type (prop) == G_TYPE_NONE)
1309     {
1310       g_warning ("Style property \"%s\" is not gettable", property);
1311       return;
1312     }
1313
1314   gtk_style_context_save (context);
1315   gtk_style_context_set_state (context, state);
1316   data = style_data_lookup (context);
1317   _gtk_style_property_query (prop, value, gtk_style_context_query_func, data->store);
1318   gtk_style_context_restore (context);
1319 }
1320
1321 /**
1322  * gtk_style_context_get_valist:
1323  * @context: a #GtkStyleContext
1324  * @state: state to retrieve the property values for
1325  * @args: va_list of property name/return location pairs, followed by %NULL
1326  *
1327  * Retrieves several style property values from @context for a given state.
1328  *
1329  * Since: 3.0
1330  **/
1331 void
1332 gtk_style_context_get_valist (GtkStyleContext *context,
1333                               GtkStateFlags    state,
1334                               va_list          args)
1335 {
1336   const gchar *property_name;
1337
1338   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1339
1340   property_name = va_arg (args, const gchar *);
1341
1342   while (property_name)
1343     {
1344       gchar *error = NULL;
1345       GValue value = G_VALUE_INIT;
1346
1347       gtk_style_context_get_property (context,
1348                                       property_name,
1349                                       state,
1350                                       &value);
1351
1352       G_VALUE_LCOPY (&value, args, 0, &error);
1353       g_value_unset (&value);
1354
1355       if (error)
1356         {
1357           g_warning ("Could not get style property \"%s\": %s", property_name, error);
1358           g_free (error);
1359           break;
1360         }
1361
1362       property_name = va_arg (args, const gchar *);
1363     }
1364 }
1365
1366 /**
1367  * gtk_style_context_get:
1368  * @context: a #GtkStyleContext
1369  * @state: state to retrieve the property values for
1370  * @...: property name /return value pairs, followed by %NULL
1371  *
1372  * Retrieves several style property values from @context for a
1373  * given state.
1374  *
1375  * Since: 3.0
1376  **/
1377 void
1378 gtk_style_context_get (GtkStyleContext *context,
1379                        GtkStateFlags    state,
1380                        ...)
1381 {
1382   va_list args;
1383
1384   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1385
1386   va_start (args, state);
1387   gtk_style_context_get_valist (context, state, args);
1388   va_end (args);
1389 }
1390
1391 /**
1392  * gtk_style_context_set_state:
1393  * @context: a #GtkStyleContext
1394  * @flags: state to represent
1395  *
1396  * Sets the state to be used when rendering with any
1397  * of the gtk_render_*() functions.
1398  *
1399  * Since: 3.0
1400  **/
1401 void
1402 gtk_style_context_set_state (GtkStyleContext *context,
1403                              GtkStateFlags    flags)
1404 {
1405   GtkStyleContextPrivate *priv;
1406   GtkStyleInfo *info;
1407
1408   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1409
1410   priv = context->priv;
1411   info = priv->info_stack->data;
1412   info->state_flags = flags;
1413   
1414   gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_STATE);
1415 }
1416
1417 /**
1418  * gtk_style_context_get_state:
1419  * @context: a #GtkStyleContext
1420  *
1421  * Returns the state used when rendering.
1422  *
1423  * Returns: the state flags
1424  *
1425  * Since: 3.0
1426  **/
1427 GtkStateFlags
1428 gtk_style_context_get_state (GtkStyleContext *context)
1429 {
1430   GtkStyleContextPrivate *priv;
1431   GtkStyleInfo *info;
1432
1433   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
1434
1435   priv = context->priv;
1436   info = priv->info_stack->data;
1437
1438   return info->state_flags;
1439 }
1440
1441 /**
1442  * gtk_style_context_state_is_running:
1443  * @context: a #GtkStyleContext
1444  * @state: a widget state
1445  * @progress: (out): return location for the transition progress
1446  *
1447  * Returns %TRUE if there is a transition animation running for the
1448  * current region (see gtk_style_context_push_animatable_region()).
1449  *
1450  * If @progress is not %NULL, the animation progress will be returned
1451  * there, 0.0 means the state is closest to being unset, while 1.0 means
1452  * it's closest to being set. This means transition animation will
1453  * run from 0 to 1 when @state is being set and from 1 to 0 when
1454  * it's being unset.
1455  *
1456  * Returns: %TRUE if there is a running transition animation for @state.
1457  *
1458  * Since: 3.0
1459  *
1460  * Deprecated: 3.6: This function always returns %FALSE
1461  **/
1462 gboolean
1463 gtk_style_context_state_is_running (GtkStyleContext *context,
1464                                     GtkStateType     state,
1465                                     gdouble         *progress)
1466 {
1467   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1468
1469   return FALSE;
1470 }
1471
1472 /**
1473  * gtk_style_context_set_path:
1474  * @context: a #GtkStyleContext
1475  * @path: a #GtkWidgetPath
1476  *
1477  * Sets the #GtkWidgetPath used for style matching. As a
1478  * consequence, the style will be regenerated to match
1479  * the new given path.
1480  *
1481  * If you are using a #GtkStyleContext returned from
1482  * gtk_widget_get_style_context(), you do not need to call
1483  * this yourself.
1484  *
1485  * Since: 3.0
1486  **/
1487 void
1488 gtk_style_context_set_path (GtkStyleContext *context,
1489                             GtkWidgetPath   *path)
1490 {
1491   GtkStyleContextPrivate *priv;
1492
1493   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1494   g_return_if_fail (path != NULL);
1495
1496   priv = context->priv;
1497   g_return_if_fail (priv->widget == NULL);
1498
1499   if (priv->widget_path)
1500     {
1501       gtk_widget_path_free (priv->widget_path);
1502       priv->widget_path = NULL;
1503     }
1504
1505   if (path)
1506     priv->widget_path = gtk_widget_path_copy (path);
1507
1508   _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY);
1509 }
1510
1511 /**
1512  * gtk_style_context_get_path:
1513  * @context: a #GtkStyleContext
1514  *
1515  * Returns the widget path used for style matching.
1516  *
1517  * Returns: (transfer none): A #GtkWidgetPath
1518  *
1519  * Since: 3.0
1520  **/
1521 const GtkWidgetPath *
1522 gtk_style_context_get_path (GtkStyleContext *context)
1523 {
1524   GtkStyleContextPrivate *priv;
1525
1526   priv = context->priv;
1527   if (priv->widget)
1528     return gtk_widget_get_path (priv->widget);
1529   else
1530     return priv->widget_path;
1531 }
1532
1533 /**
1534  * gtk_style_context_set_parent:
1535  * @context: a #GtkStyleContext
1536  * @parent: (allow-none): the new parent or %NULL
1537  *
1538  * Sets the parent style context for @context. The parent style
1539  * context is used to implement
1540  * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance">inheritance</ulink>
1541  * of properties.
1542  *
1543  * If you are using a #GtkStyleContext returned from
1544  * gtk_widget_get_style_context(), the parent will be set for you.
1545  *
1546  * Since: 3.4
1547  **/
1548 void
1549 gtk_style_context_set_parent (GtkStyleContext *context,
1550                               GtkStyleContext *parent)
1551 {
1552   GtkStyleContextPrivate *priv;
1553
1554   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1555   g_return_if_fail (parent == NULL || GTK_IS_STYLE_CONTEXT (parent));
1556
1557   priv = context->priv;
1558
1559   if (priv->parent == parent)
1560     return;
1561
1562   if (parent)
1563     {
1564       parent->priv->children = g_slist_prepend (parent->priv->children, context);
1565       g_object_ref (parent);
1566       if (priv->invalid)
1567         gtk_style_context_set_invalid (parent, TRUE);
1568     }
1569
1570   if (priv->parent)
1571     {
1572       priv->parent->priv->children = g_slist_remove (priv->parent->priv->children, context);
1573       g_object_unref (priv->parent);
1574     }
1575
1576   priv->parent = parent;
1577
1578   g_object_notify (G_OBJECT (context), "parent");
1579   _gtk_style_context_queue_invalidate (context, GTK_CSS_CHANGE_ANY_PARENT | GTK_CSS_CHANGE_ANY_SIBLING);
1580 }
1581
1582 /**
1583  * gtk_style_context_get_parent:
1584  * @context: a #GtkStyleContext
1585  *
1586  * Gets the parent context set via gtk_style_context_set_parent().
1587  * See that function for details.
1588  *
1589  * Returns: (transfer none): the parent context or %NULL
1590  *
1591  * Since: 3.4
1592  **/
1593 GtkStyleContext *
1594 gtk_style_context_get_parent (GtkStyleContext *context)
1595 {
1596   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1597
1598   return context->priv->parent;
1599 }
1600
1601 /**
1602  * gtk_style_context_save:
1603  * @context: a #GtkStyleContext
1604  *
1605  * Saves the @context state, so all modifications done through
1606  * gtk_style_context_add_class(), gtk_style_context_remove_class(),
1607  * gtk_style_context_add_region(), gtk_style_context_remove_region()
1608  * or gtk_style_context_set_junction_sides() can be reverted in one
1609  * go through gtk_style_context_restore().
1610  *
1611  * Since: 3.0
1612  **/
1613 void
1614 gtk_style_context_save (GtkStyleContext *context)
1615 {
1616   GtkStyleContextPrivate *priv;
1617   GtkStyleInfo *info;
1618
1619   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1620
1621   priv = context->priv;
1622
1623   g_assert (priv->info_stack != NULL);
1624
1625   info = style_info_copy (priv->info_stack->data);
1626   priv->info_stack = g_slist_prepend (priv->info_stack, info);
1627 }
1628
1629 /**
1630  * gtk_style_context_restore:
1631  * @context: a #GtkStyleContext
1632  *
1633  * Restores @context state to a previous stage.
1634  * See gtk_style_context_save().
1635  *
1636  * Since: 3.0
1637  **/
1638 void
1639 gtk_style_context_restore (GtkStyleContext *context)
1640 {
1641   GtkStyleContextPrivate *priv;
1642   GtkStyleInfo *info;
1643
1644   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1645
1646   priv = context->priv;
1647
1648   if (priv->info_stack)
1649     {
1650       info = priv->info_stack->data;
1651       priv->info_stack = g_slist_remove (priv->info_stack, info);
1652       style_info_free (info);
1653     }
1654
1655   if (!priv->info_stack)
1656     {
1657       g_warning ("Unpaired gtk_style_context_restore() call");
1658
1659       /* Create default region */
1660       info = style_info_new ();
1661       priv->info_stack = g_slist_prepend (priv->info_stack, info);
1662     }
1663 }
1664
1665 static gboolean
1666 style_class_find (GArray *array,
1667                   GQuark  class_quark,
1668                   guint  *position)
1669 {
1670   gint min, max, mid;
1671   gboolean found = FALSE;
1672   guint pos;
1673
1674   if (position)
1675     *position = 0;
1676
1677   if (!array || array->len == 0)
1678     return FALSE;
1679
1680   min = 0;
1681   max = array->len - 1;
1682
1683   do
1684     {
1685       GQuark item;
1686
1687       mid = (min + max) / 2;
1688       item = g_array_index (array, GQuark, mid);
1689
1690       if (class_quark == item)
1691         {
1692           found = TRUE;
1693           pos = mid;
1694         }
1695       else if (class_quark > item)
1696         min = pos = mid + 1;
1697       else
1698         {
1699           max = mid - 1;
1700           pos = mid;
1701         }
1702     }
1703   while (!found && min <= max);
1704
1705   if (position)
1706     *position = pos;
1707
1708   return found;
1709 }
1710
1711 static gboolean
1712 region_find (GArray *array,
1713              GQuark  class_quark,
1714              guint  *position)
1715 {
1716   gint min, max, mid;
1717   gboolean found = FALSE;
1718   guint pos;
1719
1720   if (position)
1721     *position = 0;
1722
1723   if (!array || array->len == 0)
1724     return FALSE;
1725
1726   min = 0;
1727   max = array->len - 1;
1728
1729   do
1730     {
1731       GtkRegion *region;
1732
1733       mid = (min + max) / 2;
1734       region = &g_array_index (array, GtkRegion, mid);
1735
1736       if (region->class_quark == class_quark)
1737         {
1738           found = TRUE;
1739           pos = mid;
1740         }
1741       else if (region->class_quark > class_quark)
1742         min = pos = mid + 1;
1743       else
1744         {
1745           max = mid - 1;
1746           pos = mid;
1747         }
1748     }
1749   while (!found && min <= max);
1750
1751   if (position)
1752     *position = pos;
1753
1754   return found;
1755 }
1756
1757 /**
1758  * gtk_style_context_add_class:
1759  * @context: a #GtkStyleContext
1760  * @class_name: class name to use in styling
1761  *
1762  * Adds a style class to @context, so posterior calls to
1763  * gtk_style_context_get() or any of the gtk_render_*()
1764  * functions will make use of this new class for styling.
1765  *
1766  * In the CSS file format, a #GtkEntry defining an "entry"
1767  * class, would be matched by:
1768  *
1769  * <programlisting>
1770  * GtkEntry.entry { ... }
1771  * </programlisting>
1772  *
1773  * While any widget defining an "entry" class would be
1774  * matched by:
1775  * <programlisting>
1776  * .entry { ... }
1777  * </programlisting>
1778  *
1779  * Since: 3.0
1780  **/
1781 void
1782 gtk_style_context_add_class (GtkStyleContext *context,
1783                              const gchar     *class_name)
1784 {
1785   GtkStyleContextPrivate *priv;
1786   GtkStyleInfo *info;
1787   GQuark class_quark;
1788   guint position;
1789
1790   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1791   g_return_if_fail (class_name != NULL);
1792
1793   priv = context->priv;
1794   class_quark = g_quark_from_string (class_name);
1795
1796   g_assert (priv->info_stack != NULL);
1797   info = priv->info_stack->data;
1798
1799   if (!style_class_find (info->style_classes, class_quark, &position))
1800     {
1801       g_array_insert_val (info->style_classes, position, class_quark);
1802
1803       gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1804     }
1805 }
1806
1807 /**
1808  * gtk_style_context_remove_class:
1809  * @context: a #GtkStyleContext
1810  * @class_name: class name to remove
1811  *
1812  * Removes @class_name from @context.
1813  *
1814  * Since: 3.0
1815  **/
1816 void
1817 gtk_style_context_remove_class (GtkStyleContext *context,
1818                                 const gchar     *class_name)
1819 {
1820   GtkStyleContextPrivate *priv;
1821   GtkStyleInfo *info;
1822   GQuark class_quark;
1823   guint position;
1824
1825   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
1826   g_return_if_fail (class_name != NULL);
1827
1828   class_quark = g_quark_try_string (class_name);
1829
1830   if (!class_quark)
1831     return;
1832
1833   priv = context->priv;
1834
1835   g_assert (priv->info_stack != NULL);
1836   info = priv->info_stack->data;
1837
1838   if (style_class_find (info->style_classes, class_quark, &position))
1839     {
1840       g_array_remove_index (info->style_classes, position);
1841
1842       gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_CLASS);
1843     }
1844 }
1845
1846 /**
1847  * gtk_style_context_has_class:
1848  * @context: a #GtkStyleContext
1849  * @class_name: a class name
1850  *
1851  * Returns %TRUE if @context currently has defined the
1852  * given class name
1853  *
1854  * Returns: %TRUE if @context has @class_name defined
1855  *
1856  * Since: 3.0
1857  **/
1858 gboolean
1859 gtk_style_context_has_class (GtkStyleContext *context,
1860                              const gchar     *class_name)
1861 {
1862   GtkStyleContextPrivate *priv;
1863   GtkStyleInfo *info;
1864   GQuark class_quark;
1865
1866   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
1867   g_return_val_if_fail (class_name != NULL, FALSE);
1868
1869   class_quark = g_quark_try_string (class_name);
1870
1871   if (!class_quark)
1872     return FALSE;
1873
1874   priv = context->priv;
1875
1876   g_assert (priv->info_stack != NULL);
1877   info = priv->info_stack->data;
1878
1879   if (style_class_find (info->style_classes, class_quark, NULL))
1880     return TRUE;
1881
1882   return FALSE;
1883 }
1884
1885 /**
1886  * gtk_style_context_list_classes:
1887  * @context: a #GtkStyleContext
1888  *
1889  * Returns the list of classes currently defined in @context.
1890  *
1891  * Returns: (transfer container) (element-type utf8): a #GList of
1892  *          strings with the currently defined classes. The contents
1893  *          of the list are owned by GTK+, but you must free the list
1894  *          itself with g_list_free() when you are done with it.
1895  *
1896  * Since: 3.0
1897  **/
1898 GList *
1899 gtk_style_context_list_classes (GtkStyleContext *context)
1900 {
1901   GtkStyleContextPrivate *priv;
1902   GtkStyleInfo *info;
1903   GList *classes = NULL;
1904   guint i;
1905
1906   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1907
1908   priv = context->priv;
1909
1910   g_assert (priv->info_stack != NULL);
1911   info = priv->info_stack->data;
1912
1913   for (i = 0; i < info->style_classes->len; i++)
1914     {
1915       GQuark quark;
1916
1917       quark = g_array_index (info->style_classes, GQuark, i);
1918       classes = g_list_prepend (classes, (gchar *) g_quark_to_string (quark));
1919     }
1920
1921   return classes;
1922 }
1923
1924 /**
1925  * gtk_style_context_list_regions:
1926  * @context: a #GtkStyleContext
1927  *
1928  * Returns the list of regions currently defined in @context.
1929  *
1930  * Returns: (transfer container) (element-type utf8): a #GList of
1931  *          strings with the currently defined regions. The contents
1932  *          of the list are owned by GTK+, but you must free the list
1933  *          itself with g_list_free() when you are done with it.
1934  *
1935  * Since: 3.0
1936  **/
1937 GList *
1938 gtk_style_context_list_regions (GtkStyleContext *context)
1939 {
1940   GtkStyleContextPrivate *priv;
1941   GtkStyleInfo *info;
1942   GList *classes = NULL;
1943   guint i;
1944
1945   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
1946
1947   priv = context->priv;
1948
1949   g_assert (priv->info_stack != NULL);
1950   info = priv->info_stack->data;
1951
1952   for (i = 0; i < info->regions->len; i++)
1953     {
1954       GtkRegion *region;
1955       const gchar *class_name;
1956
1957       region = &g_array_index (info->regions, GtkRegion, i);
1958
1959       class_name = g_quark_to_string (region->class_quark);
1960       classes = g_list_prepend (classes, (gchar *) class_name);
1961     }
1962
1963   return classes;
1964 }
1965
1966 gboolean
1967 _gtk_style_context_check_region_name (const gchar *str)
1968 {
1969   g_return_val_if_fail (str != NULL, FALSE);
1970
1971   if (!g_ascii_islower (str[0]))
1972     return FALSE;
1973
1974   while (*str)
1975     {
1976       if (*str != '-' &&
1977           !g_ascii_islower (*str))
1978         return FALSE;
1979
1980       str++;
1981     }
1982
1983   return TRUE;
1984 }
1985
1986 /**
1987  * gtk_style_context_add_region:
1988  * @context: a #GtkStyleContext
1989  * @region_name: region name to use in styling
1990  * @flags: flags that apply to the region
1991  *
1992  * Adds a region to @context, so posterior calls to
1993  * gtk_style_context_get() or any of the gtk_render_*()
1994  * functions will make use of this new region for styling.
1995  *
1996  * In the CSS file format, a #GtkTreeView defining a "row"
1997  * region, would be matched by:
1998  *
1999  * <programlisting>
2000  * GtkTreeView row { ... }
2001  * </programlisting>
2002  *
2003  * Pseudo-classes are used for matching @flags, so the two
2004  * following rules:
2005  * <programlisting>
2006  * GtkTreeView row:nth-child(even) { ... }
2007  * GtkTreeView row:nth-child(odd) { ... }
2008  * </programlisting>
2009  *
2010  * would apply to even and odd rows, respectively.
2011  *
2012  * <note><para>Region names must only contain lowercase letters
2013  * and '-', starting always with a lowercase letter.</para></note>
2014  *
2015  * Since: 3.0
2016  **/
2017 void
2018 gtk_style_context_add_region (GtkStyleContext *context,
2019                               const gchar     *region_name,
2020                               GtkRegionFlags   flags)
2021 {
2022   GtkStyleContextPrivate *priv;
2023   GtkStyleInfo *info;
2024   GQuark region_quark;
2025   guint position;
2026
2027   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2028   g_return_if_fail (region_name != NULL);
2029   g_return_if_fail (_gtk_style_context_check_region_name (region_name));
2030
2031   priv = context->priv;
2032   region_quark = g_quark_from_string (region_name);
2033
2034   g_assert (priv->info_stack != NULL);
2035   info = priv->info_stack->data;
2036
2037   if (!region_find (info->regions, region_quark, &position))
2038     {
2039       GtkRegion region;
2040
2041       region.class_quark = region_quark;
2042       region.flags = flags;
2043
2044       g_array_insert_val (info->regions, position, region);
2045
2046       gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2047     }
2048 }
2049
2050 /**
2051  * gtk_style_context_remove_region:
2052  * @context: a #GtkStyleContext
2053  * @region_name: region name to unset
2054  *
2055  * Removes a region from @context.
2056  *
2057  * Since: 3.0
2058  **/
2059 void
2060 gtk_style_context_remove_region (GtkStyleContext *context,
2061                                  const gchar     *region_name)
2062 {
2063   GtkStyleContextPrivate *priv;
2064   GtkStyleInfo *info;
2065   GQuark region_quark;
2066   guint position;
2067
2068   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2069   g_return_if_fail (region_name != NULL);
2070
2071   region_quark = g_quark_try_string (region_name);
2072
2073   if (!region_quark)
2074     return;
2075
2076   priv = context->priv;
2077
2078   g_assert (priv->info_stack != NULL);
2079   info = priv->info_stack->data;
2080
2081   if (region_find (info->regions, region_quark, &position))
2082     {
2083       g_array_remove_index (info->regions, position);
2084
2085       gtk_style_context_queue_invalidate_internal (context, GTK_CSS_CHANGE_REGION);
2086     }
2087 }
2088
2089 /**
2090  * gtk_style_context_has_region:
2091  * @context: a #GtkStyleContext
2092  * @region_name: a region name
2093  * @flags_return: (out) (allow-none): return location for region flags
2094  *
2095  * Returns %TRUE if @context has the region defined.
2096  * If @flags_return is not %NULL, it is set to the flags
2097  * affecting the region.
2098  *
2099  * Returns: %TRUE if region is defined
2100  *
2101  * Since: 3.0
2102  **/
2103 gboolean
2104 gtk_style_context_has_region (GtkStyleContext *context,
2105                               const gchar     *region_name,
2106                               GtkRegionFlags  *flags_return)
2107 {
2108   GtkStyleContextPrivate *priv;
2109   GtkStyleInfo *info;
2110   GQuark region_quark;
2111   guint position;
2112
2113   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2114   g_return_val_if_fail (region_name != NULL, FALSE);
2115
2116   if (flags_return)
2117     *flags_return = 0;
2118
2119   region_quark = g_quark_try_string (region_name);
2120
2121   if (!region_quark)
2122     return FALSE;
2123
2124   priv = context->priv;
2125
2126   g_assert (priv->info_stack != NULL);
2127   info = priv->info_stack->data;
2128
2129   if (region_find (info->regions, region_quark, &position))
2130     {
2131       if (flags_return)
2132         {
2133           GtkRegion *region;
2134
2135           region = &g_array_index (info->regions, GtkRegion, position);
2136           *flags_return = region->flags;
2137         }
2138       return TRUE;
2139     }
2140
2141   return FALSE;
2142 }
2143
2144 static gint
2145 style_property_values_cmp (gconstpointer bsearch_node1,
2146                            gconstpointer bsearch_node2)
2147 {
2148   const PropertyValue *val1 = bsearch_node1;
2149   const PropertyValue *val2 = bsearch_node2;
2150
2151   if (val1->widget_type != val2->widget_type)
2152     return val1->widget_type < val2->widget_type ? -1 : 1;
2153
2154   if (val1->pspec != val2->pspec)
2155     return val1->pspec < val2->pspec ? -1 : 1;
2156
2157   if (val1->state != val2->state)
2158     return val1->state < val2->state ? -1 : 1;
2159
2160   return 0;
2161 }
2162
2163 GtkCssValue *
2164 _gtk_style_context_peek_property (GtkStyleContext *context,
2165                                   guint            property_id)
2166 {
2167   StyleData *data = style_data_lookup (context);
2168
2169   return _gtk_css_computed_values_get_value (data->store, property_id);
2170 }
2171
2172 double
2173 _gtk_style_context_get_number (GtkStyleContext *context,
2174                                guint            property_id,
2175                                double           one_hundred_percent)
2176 {
2177   GtkCssValue *value;
2178   
2179   value = _gtk_style_context_peek_property (context, property_id);
2180   return _gtk_css_number_value_get (value, one_hundred_percent);
2181 }
2182
2183 const GValue *
2184 _gtk_style_context_peek_style_property (GtkStyleContext *context,
2185                                         GType            widget_type,
2186                                         GtkStateFlags    state,
2187                                         GParamSpec      *pspec)
2188 {
2189   GtkStyleContextPrivate *priv;
2190   PropertyValue *pcache, key = { 0 };
2191   StyleData *data;
2192   guint i;
2193
2194   priv = context->priv;
2195
2196   gtk_style_context_save (context);
2197   gtk_style_context_set_state (context, state);
2198   data = style_data_lookup (context);
2199   gtk_style_context_restore (context);
2200
2201   key.widget_type = widget_type;
2202   key.state = state;
2203   key.pspec = pspec;
2204
2205   /* need value cache array */
2206   if (!data->property_cache)
2207     data->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
2208   else
2209     {
2210       pcache = bsearch (&key,
2211                         data->property_cache->data, data->property_cache->len,
2212                         sizeof (PropertyValue), style_property_values_cmp);
2213       if (pcache)
2214         return &pcache->value;
2215     }
2216
2217   i = 0;
2218   while (i < data->property_cache->len &&
2219          style_property_values_cmp (&key, &g_array_index (data->property_cache, PropertyValue, i)) >= 0)
2220     i++;
2221
2222   g_array_insert_val (data->property_cache, i, key);
2223   pcache = &g_array_index (data->property_cache, PropertyValue, i);
2224
2225   /* cache miss, initialize value type, then set contents */
2226   g_param_spec_ref (pcache->pspec);
2227   g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
2228
2229   if (priv->widget || priv->widget_path)
2230     {
2231       GtkWidgetPath *widget_path = priv->widget ? _gtk_widget_create_path (priv->widget) : priv->widget_path;
2232
2233       if (gtk_style_provider_get_style_property (GTK_STYLE_PROVIDER (priv->cascade),
2234                                                  widget_path,
2235                                                  state, pspec, &pcache->value))
2236         {
2237           /* Resolve symbolic colors to GdkColor/GdkRGBA */
2238           if (G_VALUE_TYPE (&pcache->value) == GTK_TYPE_SYMBOLIC_COLOR)
2239             {
2240               GtkSymbolicColor *color;
2241               GdkRGBA rgba;
2242
2243               color = g_value_dup_boxed (&pcache->value);
2244
2245               g_value_unset (&pcache->value);
2246
2247               if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2248                 g_value_init (&pcache->value, GDK_TYPE_RGBA);
2249               else
2250                 g_value_init (&pcache->value, GDK_TYPE_COLOR);
2251
2252               if (_gtk_style_context_resolve_color (context, color, &rgba))
2253                 {
2254                   if (G_PARAM_SPEC_VALUE_TYPE (pspec) == GDK_TYPE_RGBA)
2255                     g_value_set_boxed (&pcache->value, &rgba);
2256                   else
2257                     {
2258                       GdkColor rgb;
2259
2260                       rgb.red = rgba.red * 65535. + 0.5;
2261                       rgb.green = rgba.green * 65535. + 0.5;
2262                       rgb.blue = rgba.blue * 65535. + 0.5;
2263
2264                       g_value_set_boxed (&pcache->value, &rgb);
2265                     }
2266                 }
2267               else
2268                 g_param_value_set_default (pspec, &pcache->value);
2269
2270               gtk_symbolic_color_unref (color);
2271             }
2272
2273           if (priv->widget)
2274             gtk_widget_path_free (widget_path);
2275
2276           return &pcache->value;
2277         }
2278
2279       if (priv->widget)
2280         gtk_widget_path_free (widget_path);
2281     }
2282
2283   /* not supplied by any provider, revert to default */
2284   g_param_value_set_default (pspec, &pcache->value);
2285
2286   return &pcache->value;
2287 }
2288
2289 /**
2290  * gtk_style_context_get_style_property:
2291  * @context: a #GtkStyleContext
2292  * @property_name: the name of the widget style property
2293  * @value: Return location for the property value
2294  *
2295  * Gets the value for a widget style property.
2296  *
2297  * When @value is no longer needed, g_value_unset() must be called
2298  * to free any allocated memory.
2299  **/
2300 void
2301 gtk_style_context_get_style_property (GtkStyleContext *context,
2302                                       const gchar     *property_name,
2303                                       GValue          *value)
2304 {
2305   GtkStyleContextPrivate *priv;
2306   GtkWidgetClass *widget_class;
2307   GtkStateFlags state;
2308   GParamSpec *pspec;
2309   const GValue *peek_value;
2310   GType widget_type;
2311
2312   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2313   g_return_if_fail (property_name != NULL);
2314   g_return_if_fail (value != NULL);
2315
2316   priv = context->priv;
2317
2318   if (priv->widget)
2319     {
2320       widget_type = G_OBJECT_TYPE (priv->widget);
2321     }
2322   else
2323     {
2324       if (!priv->widget_path)
2325         return;
2326
2327       widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2328
2329       if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2330         {
2331           g_warning ("%s: can't get style properties for non-widget class `%s'",
2332                      G_STRLOC,
2333                      g_type_name (widget_type));
2334           return;
2335         }
2336     }
2337
2338   widget_class = g_type_class_ref (widget_type);
2339   pspec = gtk_widget_class_find_style_property (widget_class, property_name);
2340   g_type_class_unref (widget_class);
2341
2342   if (!pspec)
2343     {
2344       g_warning ("%s: widget class `%s' has no style property named `%s'",
2345                  G_STRLOC,
2346                  g_type_name (widget_type),
2347                  property_name);
2348       return;
2349     }
2350
2351   state = gtk_style_context_get_state (context);
2352   peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2353                                                        state, pspec);
2354
2355   if (G_VALUE_TYPE (value) == G_VALUE_TYPE (peek_value))
2356     g_value_copy (peek_value, value);
2357   else if (g_value_type_transformable (G_VALUE_TYPE (peek_value), G_VALUE_TYPE (value)))
2358     g_value_transform (peek_value, value);
2359   else
2360     g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
2361                pspec->name,
2362                G_VALUE_TYPE_NAME (peek_value),
2363                G_VALUE_TYPE_NAME (value));
2364 }
2365
2366 /**
2367  * gtk_style_context_get_style_valist:
2368  * @context: a #GtkStyleContext
2369  * @args: va_list of property name/return location pairs, followed by %NULL
2370  *
2371  * Retrieves several widget style properties from @context according to the
2372  * current style.
2373  *
2374  * Since: 3.0
2375  **/
2376 void
2377 gtk_style_context_get_style_valist (GtkStyleContext *context,
2378                                     va_list          args)
2379 {
2380   GtkStyleContextPrivate *priv;
2381   const gchar *prop_name;
2382   GtkStateFlags state;
2383   GType widget_type;
2384
2385   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2386
2387   prop_name = va_arg (args, const gchar *);
2388   priv = context->priv;
2389
2390   if (priv->widget)
2391     {
2392       widget_type = G_OBJECT_TYPE (priv->widget);
2393     }
2394   else
2395     {
2396       if (!priv->widget_path)
2397         return;
2398
2399       widget_type = gtk_widget_path_get_object_type (priv->widget_path);
2400
2401       if (!g_type_is_a (widget_type, GTK_TYPE_WIDGET))
2402         {
2403           g_warning ("%s: can't get style properties for non-widget class `%s'",
2404                      G_STRLOC,
2405                      g_type_name (widget_type));
2406           return;
2407         }
2408     }
2409
2410   state = gtk_style_context_get_state (context);
2411
2412   while (prop_name)
2413     {
2414       GtkWidgetClass *widget_class;
2415       GParamSpec *pspec;
2416       const GValue *peek_value;
2417       gchar *error;
2418
2419       widget_class = g_type_class_ref (widget_type);
2420       pspec = gtk_widget_class_find_style_property (widget_class, prop_name);
2421       g_type_class_unref (widget_class);
2422
2423       if (!pspec)
2424         {
2425           g_warning ("%s: widget class `%s' has no style property named `%s'",
2426                      G_STRLOC,
2427                      g_type_name (widget_type),
2428                      prop_name);
2429           break;
2430         }
2431
2432       peek_value = _gtk_style_context_peek_style_property (context, widget_type,
2433                                                            state, pspec);
2434
2435       G_VALUE_LCOPY (peek_value, args, 0, &error);
2436
2437       if (error)
2438         {
2439           g_warning ("can't retrieve style property `%s' of type `%s': %s",
2440                      pspec->name,
2441                      G_VALUE_TYPE_NAME (peek_value),
2442                      error);
2443           g_free (error);
2444           break;
2445         }
2446
2447       prop_name = va_arg (args, const gchar *);
2448     }
2449 }
2450
2451 /**
2452  * gtk_style_context_get_style:
2453  * @context: a #GtkStyleContext
2454  * @...: property name /return value pairs, followed by %NULL
2455  *
2456  * Retrieves several widget style properties from @context according to the
2457  * current style.
2458  *
2459  * Since: 3.0
2460  **/
2461 void
2462 gtk_style_context_get_style (GtkStyleContext *context,
2463                              ...)
2464 {
2465   va_list args;
2466
2467   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2468
2469   va_start (args, context);
2470   gtk_style_context_get_style_valist (context, args);
2471   va_end (args);
2472 }
2473
2474
2475 /**
2476  * gtk_style_context_lookup_icon_set:
2477  * @context: a #GtkStyleContext
2478  * @stock_id: an icon name
2479  *
2480  * Looks up @stock_id in the icon factories associated to @context and
2481  * the default icon factory, returning an icon set if found, otherwise
2482  * %NULL.
2483  *
2484  * Returns: (transfer none): The looked  up %GtkIconSet, or %NULL
2485  **/
2486 GtkIconSet *
2487 gtk_style_context_lookup_icon_set (GtkStyleContext *context,
2488                                    const gchar     *stock_id)
2489 {
2490   GtkStyleContextPrivate *priv;
2491
2492   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2493   g_return_val_if_fail (stock_id != NULL, NULL);
2494
2495   priv = context->priv;
2496   g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
2497
2498   return gtk_icon_factory_lookup_default (stock_id);
2499 }
2500
2501 /**
2502  * gtk_style_context_set_screen:
2503  * @context: a #GtkStyleContext
2504  * @screen: a #GdkScreen
2505  *
2506  * Attaches @context to the given screen.
2507  *
2508  * The screen is used to add style information from 'global' style
2509  * providers, such as the screens #GtkSettings instance.
2510  *
2511  * If you are using a #GtkStyleContext returned from
2512  * gtk_widget_get_style_context(), you do not need to
2513  * call this yourself.
2514  *
2515  * Since: 3.0
2516  **/
2517 void
2518 gtk_style_context_set_screen (GtkStyleContext *context,
2519                               GdkScreen       *screen)
2520 {
2521   GtkStyleContextPrivate *priv;
2522
2523   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2524   g_return_if_fail (GDK_IS_SCREEN (screen));
2525
2526   priv = context->priv;
2527   if (priv->screen == screen)
2528     return;
2529
2530   if (priv->cascade == _gtk_style_cascade_get_for_screen (priv->screen))
2531     {
2532       gtk_style_context_set_cascade (context, _gtk_style_cascade_get_for_screen (screen));
2533     }
2534   else
2535     {
2536       _gtk_style_cascade_set_parent (priv->cascade, _gtk_style_cascade_get_for_screen (screen));
2537     }
2538
2539   priv->screen = screen;
2540
2541   g_object_notify (G_OBJECT (context), "screen");
2542
2543   gtk_style_context_invalidate (context);
2544 }
2545
2546 /**
2547  * gtk_style_context_get_screen:
2548  * @context: a #GtkStyleContext
2549  *
2550  * Returns the #GdkScreen to which @context is attached.
2551  *
2552  * Returns: (transfer none): a #GdkScreen.
2553  **/
2554 GdkScreen *
2555 gtk_style_context_get_screen (GtkStyleContext *context)
2556 {
2557   GtkStyleContextPrivate *priv;
2558
2559   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
2560
2561   priv = context->priv;
2562   return priv->screen;
2563 }
2564
2565 /**
2566  * gtk_style_context_set_direction:
2567  * @context: a #GtkStyleContext
2568  * @direction: the new direction.
2569  *
2570  * Sets the reading direction for rendering purposes.
2571  *
2572  * If you are using a #GtkStyleContext returned from
2573  * gtk_widget_get_style_context(), you do not need to
2574  * call this yourself.
2575  *
2576  * Since: 3.0
2577  **/
2578 void
2579 gtk_style_context_set_direction (GtkStyleContext  *context,
2580                                  GtkTextDirection  direction)
2581 {
2582   GtkStyleContextPrivate *priv;
2583
2584   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2585
2586   priv = context->priv;
2587   priv->direction = direction;
2588
2589   g_object_notify (G_OBJECT (context), "direction");
2590 }
2591
2592 /**
2593  * gtk_style_context_get_direction:
2594  * @context: a #GtkStyleContext
2595  *
2596  * Returns the widget direction used for rendering.
2597  *
2598  * Returns: the widget direction
2599  *
2600  * Since: 3.0
2601  **/
2602 GtkTextDirection
2603 gtk_style_context_get_direction (GtkStyleContext *context)
2604 {
2605   GtkStyleContextPrivate *priv;
2606
2607   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), GTK_TEXT_DIR_LTR);
2608
2609   priv = context->priv;
2610   return priv->direction;
2611 }
2612
2613 /**
2614  * gtk_style_context_set_junction_sides:
2615  * @context: a #GtkStyleContext
2616  * @sides: sides where rendered elements are visually connected to
2617  *     other elements
2618  *
2619  * Sets the sides where rendered elements (mostly through
2620  * gtk_render_frame()) will visually connect with other visual elements.
2621  *
2622  * This is merely a hint that may or may not be honored
2623  * by theming engines.
2624  *
2625  * Container widgets are expected to set junction hints as appropriate
2626  * for their children, so it should not normally be necessary to call
2627  * this function manually.
2628  *
2629  * Since: 3.0
2630  **/
2631 void
2632 gtk_style_context_set_junction_sides (GtkStyleContext  *context,
2633                                       GtkJunctionSides  sides)
2634 {
2635   GtkStyleContextPrivate *priv;
2636   GtkStyleInfo *info;
2637
2638   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2639
2640   priv = context->priv;
2641   info = priv->info_stack->data;
2642   info->junction_sides = sides;
2643 }
2644
2645 /**
2646  * gtk_style_context_get_junction_sides:
2647  * @context: a #GtkStyleContext
2648  *
2649  * Returns the sides where rendered elements connect visually with others.
2650  *
2651  * Returns: the junction sides
2652  *
2653  * Since: 3.0
2654  **/
2655 GtkJunctionSides
2656 gtk_style_context_get_junction_sides (GtkStyleContext *context)
2657 {
2658   GtkStyleContextPrivate *priv;
2659   GtkStyleInfo *info;
2660
2661   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), 0);
2662
2663   priv = context->priv;
2664   info = priv->info_stack->data;
2665   return info->junction_sides;
2666 }
2667
2668 static GtkSymbolicColor *
2669 gtk_style_context_color_lookup_func (gpointer    contextp,
2670                                      const char *name)
2671 {
2672   GtkStyleContext *context = contextp;
2673
2674   return _gtk_style_provider_private_get_color (GTK_STYLE_PROVIDER_PRIVATE (context->priv->cascade), name);
2675 }
2676
2677 GtkCssValue *
2678 _gtk_style_context_resolve_color_value (GtkStyleContext  *context,
2679                                         GtkCssValue      *current,
2680                                         GtkCssValue      *color)
2681 {
2682   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2683   g_return_val_if_fail (current != NULL, FALSE);
2684   g_return_val_if_fail (color != NULL, FALSE);
2685
2686   return _gtk_symbolic_color_resolve_full ((GtkSymbolicColor *) color,
2687                                            current,
2688                                            gtk_style_context_color_lookup_func,
2689                                            context);
2690 }
2691
2692
2693 gboolean
2694 _gtk_style_context_resolve_color (GtkStyleContext  *context,
2695                                   GtkSymbolicColor *color,
2696                                   GdkRGBA          *result)
2697 {
2698   GtkCssValue *val;
2699
2700   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2701   g_return_val_if_fail (color != NULL, FALSE);
2702   g_return_val_if_fail (result != NULL, FALSE);
2703
2704   val = _gtk_symbolic_color_resolve_full (color,
2705                                           _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR),
2706                                           gtk_style_context_color_lookup_func,
2707                                           context);
2708   if (val == NULL)
2709     return FALSE;
2710
2711   *result = *_gtk_css_rgba_value_get_rgba (val);
2712   _gtk_css_value_unref (val);
2713   return TRUE;
2714 }
2715
2716 /**
2717  * gtk_style_context_lookup_color:
2718  * @context: a #GtkStyleContext
2719  * @color_name: color name to lookup
2720  * @color: (out): Return location for the looked up color
2721  *
2722  * Looks up and resolves a color name in the @context color map.
2723  *
2724  * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2725  **/
2726 gboolean
2727 gtk_style_context_lookup_color (GtkStyleContext *context,
2728                                 const gchar     *color_name,
2729                                 GdkRGBA         *color)
2730 {
2731   GtkSymbolicColor *sym_color;
2732
2733   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2734   g_return_val_if_fail (color_name != NULL, FALSE);
2735   g_return_val_if_fail (color != NULL, FALSE);
2736
2737   sym_color = gtk_style_context_color_lookup_func (context, color_name);
2738   if (sym_color == NULL)
2739     return FALSE;
2740
2741   return _gtk_style_context_resolve_color (context, sym_color, color);
2742 }
2743
2744 /**
2745  * gtk_style_context_notify_state_change:
2746  * @context: a #GtkStyleContext
2747  * @window: a #GdkWindow
2748  * @region_id: (allow-none): animatable region to notify on, or %NULL.
2749  *     See gtk_style_context_push_animatable_region()
2750  * @state: state to trigger transition for
2751  * @state_value: %TRUE if @state is the state we are changing to,
2752  *     %FALSE if we are changing away from it
2753  *
2754  * Notifies a state change on @context, so if the current style makes use
2755  * of transition animations, one will be started so all rendered elements
2756  * under @region_id are animated for state @state being set to value
2757  * @state_value.
2758  *
2759  * The @window parameter is used in order to invalidate the rendered area
2760  * as the animation runs, so make sure it is the same window that is being
2761  * rendered on by the gtk_render_*() functions.
2762  *
2763  * If @region_id is %NULL, all rendered elements using @context will be
2764  * affected by this state transition.
2765  *
2766  * As a practical example, a #GtkButton notifying a state transition on
2767  * the prelight state:
2768  * <programlisting>
2769  * gtk_style_context_notify_state_change (context,
2770  *                                        gtk_widget_get_window (widget),
2771  *                                        NULL,
2772  *                                        GTK_STATE_PRELIGHT,
2773  *                                        button->in_button);
2774  * </programlisting>
2775  *
2776  * Can be handled in the CSS file like this:
2777  * <programlisting>
2778  * GtkButton {
2779  *     background-color: &num;f00
2780  * }
2781  *
2782  * GtkButton:hover {
2783  *     background-color: &num;fff;
2784  *     transition: 200ms linear
2785  * }
2786  * </programlisting>
2787  *
2788  * This combination will animate the button background from red to white
2789  * if a pointer enters the button, and back to red if the pointer leaves
2790  * the button.
2791  *
2792  * Note that @state is used when finding the transition parameters, which
2793  * is why the style places the transition under the :hover pseudo-class.
2794  *
2795  * Since: 3.0
2796  *
2797  * Deprecated: 3.6: This function does nothing.
2798  **/
2799 void
2800 gtk_style_context_notify_state_change (GtkStyleContext *context,
2801                                        GdkWindow       *window,
2802                                        gpointer         region_id,
2803                                        GtkStateType     state,
2804                                        gboolean         state_value)
2805 {
2806   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2807   g_return_if_fail (GDK_IS_WINDOW (window));
2808   g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2809   g_return_if_fail (context->priv->widget != NULL || context->priv->widget_path != NULL);
2810 }
2811
2812 /**
2813  * gtk_style_context_cancel_animations:
2814  * @context: a #GtkStyleContext
2815  * @region_id: (allow-none): animatable region to stop, or %NULL.
2816  *     See gtk_style_context_push_animatable_region()
2817  *
2818  * Stops all running animations for @region_id and all animatable
2819  * regions underneath.
2820  *
2821  * A %NULL @region_id will stop all ongoing animations in @context,
2822  * when dealing with a #GtkStyleContext obtained through
2823  * gtk_widget_get_style_context(), this is normally done for you
2824  * in all circumstances you would expect all widget to be stopped,
2825  * so this should be only used in complex widgets with different
2826  * animatable regions.
2827  *
2828  * Since: 3.0
2829  *
2830  * Deprecated: 3.6: This function does nothing.
2831  **/
2832 void
2833 gtk_style_context_cancel_animations (GtkStyleContext *context,
2834                                      gpointer         region_id)
2835 {
2836   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2837 }
2838
2839 /**
2840  * gtk_style_context_scroll_animations:
2841  * @context: a #GtkStyleContext
2842  * @window: a #GdkWindow used previously in
2843  *          gtk_style_context_notify_state_change()
2844  * @dx: Amount to scroll in the X axis
2845  * @dy: Amount to scroll in the Y axis
2846  *
2847  * This function is analogous to gdk_window_scroll(), and
2848  * should be called together with it so the invalidation
2849  * areas for any ongoing animation are scrolled together
2850  * with it.
2851  *
2852  * Since: 3.0
2853  *
2854  * Deprecated: 3.6: This function does nothing.
2855  **/
2856 void
2857 gtk_style_context_scroll_animations (GtkStyleContext *context,
2858                                      GdkWindow       *window,
2859                                      gint             dx,
2860                                      gint             dy)
2861 {
2862   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2863   g_return_if_fail (GDK_IS_WINDOW (window));
2864 }
2865
2866 /**
2867  * gtk_style_context_push_animatable_region:
2868  * @context: a #GtkStyleContext
2869  * @region_id: unique identifier for the animatable region
2870  *
2871  * Pushes an animatable region, so all further gtk_render_*() calls between
2872  * this call and the following gtk_style_context_pop_animatable_region()
2873  * will potentially show transition animations for this region if
2874  * gtk_style_context_notify_state_change() is called for a given state,
2875  * and the current theme/style defines transition animations for state
2876  * changes.
2877  *
2878  * The @region_id used must be unique in @context so the theming engine
2879  * can uniquely identify rendered elements subject to a state transition.
2880  *
2881  * Since: 3.0
2882  *
2883  * Deprecated: 3.6: This function does nothing.
2884  **/
2885 void
2886 gtk_style_context_push_animatable_region (GtkStyleContext *context,
2887                                           gpointer         region_id)
2888 {
2889   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2890   g_return_if_fail (region_id != NULL);
2891 }
2892
2893 /**
2894  * gtk_style_context_pop_animatable_region:
2895  * @context: a #GtkStyleContext
2896  *
2897  * Pops an animatable region from @context.
2898  * See gtk_style_context_push_animatable_region().
2899  *
2900  * Since: 3.0
2901  *
2902  * Deprecated: 3.6: This function does nothing.
2903  **/
2904 void
2905 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
2906 {
2907   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2908 }
2909
2910 static void
2911 gtk_style_context_clear_cache (GtkStyleContext *context)
2912 {
2913   GtkStyleContextPrivate *priv;
2914   GSList *list;
2915
2916   priv = context->priv;
2917
2918   for (list = priv->info_stack; list; list = list->next)
2919     {
2920       GtkStyleInfo *info = list->data;
2921       if (info->data)
2922         {
2923           style_data_unref (info->data);
2924           info->data = NULL;
2925         }
2926     }
2927   g_hash_table_remove_all (priv->style_data);
2928 }
2929
2930 static void
2931 gtk_style_context_do_invalidate (GtkStyleContext *context)
2932 {
2933   GtkStyleContextPrivate *priv;
2934
2935   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2936
2937   priv = context->priv;
2938
2939   /* Avoid reentrancy */
2940   if (priv->invalidating_context)
2941     return;
2942
2943   priv->invalidating_context = TRUE;
2944
2945   g_signal_emit (context, signals[CHANGED], 0);
2946
2947   priv->invalidating_context = FALSE;
2948 }
2949
2950 void
2951 _gtk_style_context_stop_animations (GtkStyleContext *context)
2952 {
2953   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2954
2955   if (!gtk_style_context_is_animating (context))
2956     return;
2957
2958   gtk_style_context_stop_animating (context);
2959 }
2960
2961 void
2962 _gtk_style_context_validate (GtkStyleContext *context,
2963                              gint64           timestamp,
2964                              GtkCssChange     change)
2965 {
2966   GtkStyleContextPrivate *priv;
2967   GSList *list;
2968
2969   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2970
2971   priv = context->priv;
2972
2973   change |= priv->pending_changes;
2974
2975   if (!priv->invalid && change == 0)
2976     return;
2977
2978   priv->pending_changes = 0;
2979   gtk_style_context_set_invalid (context, FALSE);
2980
2981   /* Try to avoid invalidating if we can */
2982   if (change & GTK_STYLE_CONTEXT_RADICAL_CHANGE)
2983     {
2984       priv->relevant_changes = GTK_CSS_CHANGE_ANY;
2985     }
2986   else
2987     {
2988       if (priv->relevant_changes == GTK_CSS_CHANGE_ANY)
2989         {
2990           GtkWidgetPath *path;
2991           GtkCssMatcher matcher;
2992
2993           path = create_query_path (context);
2994           _gtk_css_matcher_init (&matcher, path, ((GtkStyleInfo *) priv->info_stack->data)->state_flags);
2995
2996           priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
2997                                                                            &matcher);
2998           priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
2999
3000           gtk_widget_path_unref (path);
3001         }
3002     }
3003
3004   if (priv->relevant_changes & change)
3005     {
3006       GtkStyleInfo *info = priv->info_stack->data;
3007       GtkCssComputedValues *old, *new;
3008
3009       old = info->data ? g_object_ref (info->data->store) : NULL;
3010
3011       if ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE)
3012         gtk_style_context_clear_cache (context);
3013       else
3014         info->data = NULL;
3015
3016       if (old)
3017         {
3018           GtkBitmask *bitmask;
3019
3020           new = style_data_lookup (context)->store;
3021
3022           bitmask = _gtk_css_computed_values_get_difference (new, old);
3023           if (!_gtk_bitmask_is_empty (bitmask))
3024             gtk_style_context_do_invalidate (context);
3025
3026           _gtk_bitmask_free (bitmask);
3027           g_object_unref (old);
3028         }
3029       else
3030         gtk_style_context_do_invalidate (context);
3031
3032     }
3033
3034   change = _gtk_css_change_for_child (change);
3035   for (list = priv->children; list; list = list->next)
3036     {
3037       _gtk_style_context_validate (list->data, timestamp, change);
3038     }
3039 }
3040
3041 void
3042 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3043                                      GtkCssChange     change)
3044 {
3045   GtkStyleContextPrivate *priv;
3046
3047   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3048   g_return_if_fail (change != 0);
3049
3050   priv = context->priv;
3051
3052   if (priv->widget == NULL && priv->widget_path == NULL)
3053     return;
3054
3055   priv->pending_changes |= change;
3056   gtk_style_context_set_invalid (context, TRUE);
3057 }
3058
3059 /**
3060  * gtk_style_context_invalidate:
3061  * @context: a #GtkStyleContext.
3062  *
3063  * Invalidates @context style information, so it will be reconstructed
3064  * again.
3065  *
3066  * If you're using a #GtkStyleContext returned from
3067  * gtk_widget_get_style_context(), you do not need to
3068  * call this yourself.
3069  *
3070  * Since: 3.0
3071  **/
3072 void
3073 gtk_style_context_invalidate (GtkStyleContext *context)
3074 {
3075   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3076
3077   gtk_style_context_clear_cache (context);
3078   gtk_style_context_do_invalidate (context);
3079 }
3080
3081 /**
3082  * gtk_style_context_set_background:
3083  * @context: a #GtkStyleContext
3084  * @window: a #GdkWindow
3085  *
3086  * Sets the background of @window to the background pattern or
3087  * color specified in @context for its current state.
3088  *
3089  * Since: 3.0
3090  **/
3091 void
3092 gtk_style_context_set_background (GtkStyleContext *context,
3093                                   GdkWindow       *window)
3094 {
3095   GtkStateFlags state;
3096   cairo_pattern_t *pattern;
3097   GdkRGBA *color;
3098
3099   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3100   g_return_if_fail (GDK_IS_WINDOW (window));
3101
3102   state = gtk_style_context_get_state (context);
3103   gtk_style_context_get (context, state,
3104                          "background-image", &pattern,
3105                          NULL);
3106   if (pattern)
3107     {
3108       gdk_window_set_background_pattern (window, pattern);
3109       cairo_pattern_destroy (pattern);
3110       return;
3111     }
3112
3113   gtk_style_context_get (context, state,
3114                          "background-color", &color,
3115                          NULL);
3116   if (color)
3117     {
3118       gdk_window_set_background_rgba (window, color);
3119       gdk_rgba_free (color);
3120     }
3121 }
3122
3123 /**
3124  * gtk_style_context_get_color:
3125  * @context: a #GtkStyleContext
3126  * @state: state to retrieve the color for
3127  * @color: (out): return value for the foreground color
3128  *
3129  * Gets the foreground color for a given state.
3130  *
3131  * Since: 3.0
3132  **/
3133 void
3134 gtk_style_context_get_color (GtkStyleContext *context,
3135                              GtkStateFlags    state,
3136                              GdkRGBA         *color)
3137 {
3138   GdkRGBA *c;
3139
3140   g_return_if_fail (color != NULL);
3141   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3142
3143   gtk_style_context_get (context,
3144                          state,
3145                          "color", &c,
3146                          NULL);
3147
3148   *color = *c;
3149   gdk_rgba_free (c);
3150 }
3151
3152 /**
3153  * gtk_style_context_get_background_color:
3154  * @context: a #GtkStyleContext
3155  * @state: state to retrieve the color for
3156  * @color: (out): return value for the background color
3157  *
3158  * Gets the background color for a given state.
3159  *
3160  * Since: 3.0
3161  **/
3162 void
3163 gtk_style_context_get_background_color (GtkStyleContext *context,
3164                                         GtkStateFlags    state,
3165                                         GdkRGBA         *color)
3166 {
3167   GdkRGBA *c;
3168
3169   g_return_if_fail (color != NULL);
3170   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3171
3172   gtk_style_context_get (context,
3173                          state,
3174                          "background-color", &c,
3175                          NULL);
3176
3177   *color = *c;
3178   gdk_rgba_free (c);
3179 }
3180
3181 /**
3182  * gtk_style_context_get_border_color:
3183  * @context: a #GtkStyleContext
3184  * @state: state to retrieve the color for
3185  * @color: (out): return value for the border color
3186  *
3187  * Gets the border color for a given state.
3188  *
3189  * Since: 3.0
3190  **/
3191 void
3192 gtk_style_context_get_border_color (GtkStyleContext *context,
3193                                     GtkStateFlags    state,
3194                                     GdkRGBA         *color)
3195 {
3196   GdkRGBA *c;
3197
3198   g_return_if_fail (color != NULL);
3199   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3200
3201   gtk_style_context_get (context,
3202                          state,
3203                          "border-color", &c,
3204                          NULL);
3205
3206   *color = *c;
3207   gdk_rgba_free (c);
3208 }
3209
3210 /**
3211  * gtk_style_context_get_border:
3212  * @context: a #GtkStyleContext
3213  * @state: state to retrieve the border for
3214  * @border: (out): return value for the border settings
3215  *
3216  * Gets the border for a given state as a #GtkBorder.
3217  * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3218  *
3219  * Since: 3.0
3220  **/
3221 void
3222 gtk_style_context_get_border (GtkStyleContext *context,
3223                               GtkStateFlags    state,
3224                               GtkBorder       *border)
3225 {
3226   int top, left, bottom, right;
3227
3228   g_return_if_fail (border != NULL);
3229   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3230
3231   gtk_style_context_get (context,
3232                          state,
3233                          "border-top-width", &top,
3234                          "border-left-width", &left,
3235                          "border-bottom-width", &bottom,
3236                          "border-right-width", &right,
3237                          NULL);
3238
3239   border->top = top;
3240   border->left = left;
3241   border->bottom = bottom;
3242   border->right = right;
3243 }
3244
3245 /**
3246  * gtk_style_context_get_padding:
3247  * @context: a #GtkStyleContext
3248  * @state: state to retrieve the padding for
3249  * @padding: (out): return value for the padding settings
3250  *
3251  * Gets the padding for a given state as a #GtkBorder.
3252  * See %GTK_STYLE_PROPERTY_PADDING.
3253  *
3254  * Since: 3.0
3255  **/
3256 void
3257 gtk_style_context_get_padding (GtkStyleContext *context,
3258                                GtkStateFlags    state,
3259                                GtkBorder       *padding)
3260 {
3261   int top, left, bottom, right;
3262
3263   g_return_if_fail (padding != NULL);
3264   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3265
3266   gtk_style_context_get (context,
3267                          state,
3268                          "padding-top", &top,
3269                          "padding-left", &left,
3270                          "padding-bottom", &bottom,
3271                          "padding-right", &right,
3272                          NULL);
3273
3274   padding->top = top;
3275   padding->left = left;
3276   padding->bottom = bottom;
3277   padding->right = right;
3278 }
3279
3280 /**
3281  * gtk_style_context_get_margin:
3282  * @context: a #GtkStyleContext
3283  * @state: state to retrieve the border for
3284  * @margin: (out): return value for the margin settings
3285  *
3286  * Gets the margin for a given state as a #GtkBorder.
3287  * See %GTK_STYLE_PROPERTY_MARGIN.
3288  *
3289  * Since: 3.0
3290  **/
3291 void
3292 gtk_style_context_get_margin (GtkStyleContext *context,
3293                               GtkStateFlags    state,
3294                               GtkBorder       *margin)
3295 {
3296   int top, left, bottom, right;
3297
3298   g_return_if_fail (margin != NULL);
3299   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3300
3301   gtk_style_context_get (context,
3302                          state,
3303                          "margin-top", &top,
3304                          "margin-left", &left,
3305                          "margin-bottom", &bottom,
3306                          "margin-right", &right,
3307                          NULL);
3308
3309   margin->top = top;
3310   margin->left = left;
3311   margin->bottom = bottom;
3312   margin->right = right;
3313 }
3314
3315 /**
3316  * gtk_style_context_get_font:
3317  * @context: a #GtkStyleContext
3318  * @state: state to retrieve the font for
3319  *
3320  * Returns the font description for a given state. The returned
3321  * object is const and will remain valid until the
3322  * #GtkStyleContext::changed signal happens.
3323  *
3324  * Returns: (transfer none): the #PangoFontDescription for the given
3325  *          state.  This object is owned by GTK+ and should not be
3326  *          freed.
3327  *
3328  * Since: 3.0
3329  **/
3330 const PangoFontDescription *
3331 gtk_style_context_get_font (GtkStyleContext *context,
3332                             GtkStateFlags    state)
3333 {
3334   GtkStyleContextPrivate *priv;
3335   StyleData *data;
3336   PangoFontDescription *description;
3337
3338   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3339
3340   priv = context->priv;
3341   g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3342
3343   gtk_style_context_save (context);
3344   gtk_style_context_set_state (context, state);
3345   data = style_data_lookup (context);
3346   gtk_style_context_restore (context);
3347
3348   /* Yuck, fonts are created on-demand but we don't return a ref.
3349    * Do bad things to achieve this requirement */
3350   description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3351   if (description == NULL)
3352     {
3353       gtk_style_context_get (context, state, "font", &description, NULL);
3354       g_object_set_data_full (G_OBJECT (data->store),
3355                               "font-cache-for-get_font",
3356                               description,
3357                               (GDestroyNotify) pango_font_description_free);
3358     }
3359   return description;
3360 }
3361
3362 static void
3363 get_cursor_color (GtkStyleContext *context,
3364                   gboolean         primary,
3365                   GdkRGBA         *color)
3366 {
3367   GdkColor *style_color;
3368
3369   gtk_style_context_get_style (context,
3370                                primary ? "cursor-color" : "secondary-cursor-color",
3371                                &style_color,
3372                                NULL);
3373
3374   if (style_color)
3375     {
3376       color->red = style_color->red / 65535.0;
3377       color->green = style_color->green / 65535.0;
3378       color->blue = style_color->blue / 65535.0;
3379       color->alpha = 1;
3380
3381       gdk_color_free (style_color);
3382     }
3383   else
3384     {
3385       gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3386
3387       if (!primary)
3388       {
3389         GdkRGBA bg;
3390
3391         gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3392
3393         color->red = (color->red + bg.red) * 0.5;
3394         color->green = (color->green + bg.green) * 0.5;
3395         color->blue = (color->blue + bg.blue) * 0.5;
3396       }
3397     }
3398 }
3399
3400 void
3401 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3402                                      GdkRGBA         *primary_color,
3403                                      GdkRGBA         *secondary_color)
3404 {
3405   if (primary_color)
3406     get_cursor_color (context, TRUE, primary_color);
3407
3408   if (secondary_color)
3409     get_cursor_color (context, FALSE, secondary_color);
3410 }
3411
3412 /* Paint methods */
3413
3414 /**
3415  * gtk_render_check:
3416  * @context: a #GtkStyleContext
3417  * @cr: a #cairo_t
3418  * @x: X origin of the rectangle
3419  * @y: Y origin of the rectangle
3420  * @width: rectangle width
3421  * @height: rectangle height
3422  *
3423  * Renders a checkmark (as in a #GtkCheckButton).
3424  *
3425  * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3426  * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3427  * should be marked as undefined.
3428  *
3429  * <example>
3430  * <title>Typical checkmark rendering</title>
3431  * <inlinegraphic fileref="checks.png" format="PNG"/>
3432  * </example>
3433  *
3434  * Since: 3.0
3435  **/
3436 void
3437 gtk_render_check (GtkStyleContext *context,
3438                   cairo_t         *cr,
3439                   gdouble          x,
3440                   gdouble          y,
3441                   gdouble          width,
3442                   gdouble          height)
3443 {
3444   GtkThemingEngineClass *engine_class;
3445   GtkThemingEngine *engine;
3446
3447   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3448   g_return_if_fail (cr != NULL);
3449
3450   if (width <= 0 || height <= 0)
3451     return;
3452
3453   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3454   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3455
3456   cairo_save (cr);
3457
3458
3459   _gtk_theming_engine_set_context (engine, context);
3460   engine_class->render_check (engine, cr,
3461                               x, y, width, height);
3462
3463   cairo_restore (cr);
3464 }
3465
3466 /**
3467  * gtk_render_option:
3468  * @context: a #GtkStyleContext
3469  * @cr: a #cairo_t
3470  * @x: X origin of the rectangle
3471  * @y: Y origin of the rectangle
3472  * @width: rectangle width
3473  * @height: rectangle height
3474  *
3475  * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3476  * state will determine whether the option is on or off, and
3477  * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3478  *
3479  * <example>
3480  * <title>Typical option mark rendering</title>
3481  * <inlinegraphic fileref="options.png" format="PNG"/>
3482  * </example>
3483  *
3484  * Since: 3.0
3485  **/
3486 void
3487 gtk_render_option (GtkStyleContext *context,
3488                    cairo_t         *cr,
3489                    gdouble          x,
3490                    gdouble          y,
3491                    gdouble          width,
3492                    gdouble          height)
3493 {
3494   GtkThemingEngineClass *engine_class;
3495   GtkThemingEngine *engine;
3496
3497   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3498   g_return_if_fail (cr != NULL);
3499
3500   if (width <= 0 || height <= 0)
3501     return;
3502
3503   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3504   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3505
3506   cairo_save (cr);
3507
3508   _gtk_theming_engine_set_context (engine, context);
3509   engine_class->render_option (engine, cr,
3510                                x, y, width, height);
3511
3512   cairo_restore (cr);
3513 }
3514
3515 /**
3516  * gtk_render_arrow:
3517  * @context: a #GtkStyleContext
3518  * @cr: a #cairo_t
3519  * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3520  * @x: X origin of the render area
3521  * @y: Y origin of the render area
3522  * @size: square side for render area
3523  *
3524  * Renders an arrow pointing to @angle.
3525  *
3526  * <example>
3527  * <title>Typical arrow rendering at 0, 1&solidus;2 &pi;, &pi; and 3&solidus;2 &pi;</title>
3528  * <inlinegraphic fileref="arrows.png" format="PNG"/>
3529  * </example>
3530  *
3531  * Since: 3.0
3532  **/
3533 void
3534 gtk_render_arrow (GtkStyleContext *context,
3535                   cairo_t         *cr,
3536                   gdouble          angle,
3537                   gdouble          x,
3538                   gdouble          y,
3539                   gdouble          size)
3540 {
3541   GtkThemingEngineClass *engine_class;
3542   GtkThemingEngine *engine;
3543
3544   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3545   g_return_if_fail (cr != NULL);
3546
3547   if (size <= 0)
3548     return;
3549
3550   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3551   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3552
3553   cairo_save (cr);
3554
3555   gtk_style_context_save (context);
3556   gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3557
3558   _gtk_theming_engine_set_context (engine, context);
3559   engine_class->render_arrow (engine, cr,
3560                               angle, x, y, size);
3561
3562   gtk_style_context_restore (context);
3563   cairo_restore (cr);
3564 }
3565
3566 /**
3567  * gtk_render_background:
3568  * @context: a #GtkStyleContext
3569  * @cr: a #cairo_t
3570  * @x: X origin of the rectangle
3571  * @y: Y origin of the rectangle
3572  * @width: rectangle width
3573  * @height: rectangle height
3574  *
3575  * Renders the background of an element.
3576  *
3577  * <example>
3578  * <title>Typical background rendering, showing the effect of
3579  * <parameter>background-image</parameter>,
3580  * <parameter>border-width</parameter> and
3581  * <parameter>border-radius</parameter></title>
3582  * <inlinegraphic fileref="background.png" format="PNG"/>
3583  * </example>
3584  *
3585  * Since: 3.0.
3586  **/
3587 void
3588 gtk_render_background (GtkStyleContext *context,
3589                        cairo_t         *cr,
3590                        gdouble          x,
3591                        gdouble          y,
3592                        gdouble          width,
3593                        gdouble          height)
3594 {
3595   GtkThemingEngineClass *engine_class;
3596   GtkThemingEngine *engine;
3597
3598   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3599   g_return_if_fail (cr != NULL);
3600
3601   if (width <= 0 || height <= 0)
3602     return;
3603
3604   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3605   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3606
3607   cairo_save (cr);
3608
3609   _gtk_theming_engine_set_context (engine, context);
3610   engine_class->render_background (engine, cr, x, y, width, height);
3611
3612   cairo_restore (cr);
3613 }
3614
3615 /**
3616  * gtk_render_frame:
3617  * @context: a #GtkStyleContext
3618  * @cr: a #cairo_t
3619  * @x: X origin of the rectangle
3620  * @y: Y origin of the rectangle
3621  * @width: rectangle width
3622  * @height: rectangle height
3623  *
3624  * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3625  *
3626  * <example>
3627  * <title>Examples of frame rendering, showing the effect of
3628  * <parameter>border-image</parameter>,
3629  * <parameter>border-color</parameter>,
3630  * <parameter>border-width</parameter>,
3631  * <parameter>border-radius</parameter> and
3632  * junctions</title>
3633  * <inlinegraphic fileref="frames.png" format="PNG"/>
3634  * </example>
3635  *
3636  * Since: 3.0
3637  **/
3638 void
3639 gtk_render_frame (GtkStyleContext *context,
3640                   cairo_t         *cr,
3641                   gdouble          x,
3642                   gdouble          y,
3643                   gdouble          width,
3644                   gdouble          height)
3645 {
3646   GtkThemingEngineClass *engine_class;
3647   GtkThemingEngine *engine;
3648
3649   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3650   g_return_if_fail (cr != NULL);
3651
3652   if (width <= 0 || height <= 0)
3653     return;
3654
3655   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3656   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3657
3658   cairo_save (cr);
3659
3660   _gtk_theming_engine_set_context (engine, context);
3661   engine_class->render_frame (engine, cr, x, y, width, height);
3662
3663   cairo_restore (cr);
3664 }
3665
3666 /**
3667  * gtk_render_expander:
3668  * @context: a #GtkStyleContext
3669  * @cr: a #cairo_t
3670  * @x: X origin of the rectangle
3671  * @y: Y origin of the rectangle
3672  * @width: rectangle width
3673  * @height: rectangle height
3674  *
3675  * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3676  * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3677  * determines whether the expander is collapsed or expanded.
3678  *
3679  * <example>
3680  * <title>Typical expander rendering</title>
3681  * <inlinegraphic fileref="expanders.png" format="PNG"/>
3682  * </example>
3683  *
3684  * Since: 3.0
3685  **/
3686 void
3687 gtk_render_expander (GtkStyleContext *context,
3688                      cairo_t         *cr,
3689                      gdouble          x,
3690                      gdouble          y,
3691                      gdouble          width,
3692                      gdouble          height)
3693 {
3694   GtkThemingEngineClass *engine_class;
3695   GtkThemingEngine *engine;
3696
3697   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3698   g_return_if_fail (cr != NULL);
3699
3700   if (width <= 0 || height <= 0)
3701     return;
3702
3703   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3704   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3705
3706   cairo_save (cr);
3707
3708   _gtk_theming_engine_set_context (engine, context);
3709   engine_class->render_expander (engine, cr, x, y, width, height);
3710
3711   cairo_restore (cr);
3712 }
3713
3714 /**
3715  * gtk_render_focus:
3716  * @context: a #GtkStyleContext
3717  * @cr: a #cairo_t
3718  * @x: X origin of the rectangle
3719  * @y: Y origin of the rectangle
3720  * @width: rectangle width
3721  * @height: rectangle height
3722  *
3723  * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3724  * <example>
3725  * <title>Typical focus rendering</title>
3726  * <inlinegraphic fileref="focus.png" format="PNG"/>
3727  * </example>
3728  *
3729  * Since: 3.0
3730  **/
3731 void
3732 gtk_render_focus (GtkStyleContext *context,
3733                   cairo_t         *cr,
3734                   gdouble          x,
3735                   gdouble          y,
3736                   gdouble          width,
3737                   gdouble          height)
3738 {
3739   GtkThemingEngineClass *engine_class;
3740   GtkThemingEngine *engine;
3741
3742   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3743   g_return_if_fail (cr != NULL);
3744
3745   if (width <= 0 || height <= 0)
3746     return;
3747
3748   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3749   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3750
3751   cairo_save (cr);
3752
3753   _gtk_theming_engine_set_context (engine, context);
3754   engine_class->render_focus (engine, cr, x, y, width, height);
3755
3756   cairo_restore (cr);
3757 }
3758
3759 /**
3760  * gtk_render_layout:
3761  * @context: a #GtkStyleContext
3762  * @cr: a #cairo_t
3763  * @x: X origin
3764  * @y: Y origin
3765  * @layout: the #PangoLayout to render
3766  *
3767  * Renders @layout on the coordinates @x, @y
3768  *
3769  * Since: 3.0
3770  **/
3771 void
3772 gtk_render_layout (GtkStyleContext *context,
3773                    cairo_t         *cr,
3774                    gdouble          x,
3775                    gdouble          y,
3776                    PangoLayout     *layout)
3777 {
3778   GtkThemingEngineClass *engine_class;
3779   GtkThemingEngine *engine;
3780   PangoRectangle extents;
3781
3782   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3783   g_return_if_fail (PANGO_IS_LAYOUT (layout));
3784   g_return_if_fail (cr != NULL);
3785
3786   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3787   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3788
3789   cairo_save (cr);
3790
3791   pango_layout_get_extents (layout, &extents, NULL);
3792
3793   _gtk_theming_engine_set_context (engine, context);
3794   engine_class->render_layout (engine, cr, x, y, layout);
3795
3796   cairo_restore (cr);
3797 }
3798
3799 /**
3800  * gtk_render_line:
3801  * @context: a #GtkStyleContext
3802  * @cr: a #cairo_t
3803  * @x0: X coordinate for the origin of the line
3804  * @y0: Y coordinate for the origin of the line
3805  * @x1: X coordinate for the end of the line
3806  * @y1: Y coordinate for the end of the line
3807  *
3808  * Renders a line from (x0, y0) to (x1, y1).
3809  *
3810  * Since: 3.0
3811  **/
3812 void
3813 gtk_render_line (GtkStyleContext *context,
3814                  cairo_t         *cr,
3815                  gdouble          x0,
3816                  gdouble          y0,
3817                  gdouble          x1,
3818                  gdouble          y1)
3819 {
3820   GtkThemingEngineClass *engine_class;
3821   GtkThemingEngine *engine;
3822
3823   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3824   g_return_if_fail (cr != NULL);
3825
3826   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3827   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3828
3829   cairo_save (cr);
3830
3831   _gtk_theming_engine_set_context (engine, context);
3832   engine_class->render_line (engine, cr, x0, y0, x1, y1);
3833
3834   cairo_restore (cr);
3835 }
3836
3837 /**
3838  * gtk_render_slider:
3839  * @context: a #GtkStyleContext
3840  * @cr: a #cairo_t
3841  * @x: X origin of the rectangle
3842  * @y: Y origin of the rectangle
3843  * @width: rectangle width
3844  * @height: rectangle height
3845  * @orientation: orientation of the slider
3846  *
3847  * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
3848  * @width, @height. @orientation defines whether the slider is vertical
3849  * or horizontal.
3850  *
3851  * <example>
3852  * <title>Typical slider rendering</title>
3853  * <inlinegraphic fileref="sliders.png" format="PNG"/>
3854  * </example>
3855  *
3856  * Since: 3.0
3857  **/
3858 void
3859 gtk_render_slider (GtkStyleContext *context,
3860                    cairo_t         *cr,
3861                    gdouble          x,
3862                    gdouble          y,
3863                    gdouble          width,
3864                    gdouble          height,
3865                    GtkOrientation   orientation)
3866 {
3867   GtkThemingEngineClass *engine_class;
3868   GtkThemingEngine *engine;
3869
3870   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3871   g_return_if_fail (cr != NULL);
3872
3873   if (width <= 0 || height <= 0)
3874     return;
3875
3876   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3877   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3878
3879   cairo_save (cr);
3880
3881   _gtk_theming_engine_set_context (engine, context);
3882   engine_class->render_slider (engine, cr, x, y, width, height, orientation);
3883
3884   cairo_restore (cr);
3885 }
3886
3887 /**
3888  * gtk_render_frame_gap:
3889  * @context: a #GtkStyleContext
3890  * @cr: a #cairo_t
3891  * @x: X origin of the rectangle
3892  * @y: Y origin of the rectangle
3893  * @width: rectangle width
3894  * @height: rectangle height
3895  * @gap_side: side where the gap is
3896  * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
3897  * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
3898  *
3899  * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
3900  * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
3901  * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
3902  * %GTK_POS_LEFT and %GTK_POS_RIGHT.
3903  *
3904  * <example>
3905  * <title>Typical rendering of a frame with a gap</title>
3906  * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
3907  * </example>
3908  *
3909  * Since: 3.0
3910  **/
3911 void
3912 gtk_render_frame_gap (GtkStyleContext *context,
3913                       cairo_t         *cr,
3914                       gdouble          x,
3915                       gdouble          y,
3916                       gdouble          width,
3917                       gdouble          height,
3918                       GtkPositionType  gap_side,
3919                       gdouble          xy0_gap,
3920                       gdouble          xy1_gap)
3921 {
3922   GtkThemingEngineClass *engine_class;
3923   GtkThemingEngine *engine;
3924
3925   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3926   g_return_if_fail (cr != NULL);
3927   g_return_if_fail (xy0_gap <= xy1_gap);
3928   g_return_if_fail (xy0_gap >= 0);
3929
3930   if (width <= 0 || height <= 0)
3931     return;
3932
3933   if (gap_side == GTK_POS_LEFT ||
3934       gap_side == GTK_POS_RIGHT)
3935     g_return_if_fail (xy1_gap <= height);
3936   else
3937     g_return_if_fail (xy1_gap <= width);
3938
3939   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3940   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3941
3942   cairo_save (cr);
3943
3944   _gtk_theming_engine_set_context (engine, context);
3945   engine_class->render_frame_gap (engine, cr,
3946                                   x, y, width, height, gap_side,
3947                                   xy0_gap, xy1_gap);
3948
3949   cairo_restore (cr);
3950 }
3951
3952 /**
3953  * gtk_render_extension:
3954  * @context: a #GtkStyleContext
3955  * @cr: a #cairo_t
3956  * @x: X origin of the rectangle
3957  * @y: Y origin of the rectangle
3958  * @width: rectangle width
3959  * @height: rectangle height
3960  * @gap_side: side where the gap is
3961  *
3962  * Renders a extension (as in a #GtkNotebook tab) in the rectangle
3963  * defined by @x, @y, @width, @height. The side where the extension
3964  * connects to is defined by @gap_side.
3965  *
3966  * <example>
3967  * <title>Typical extension rendering</title>
3968  * <inlinegraphic fileref="extensions.png" format="PNG"/>
3969  * </example>
3970  *
3971  * Since: 3.0
3972  **/
3973 void
3974 gtk_render_extension (GtkStyleContext *context,
3975                       cairo_t         *cr,
3976                       gdouble          x,
3977                       gdouble          y,
3978                       gdouble          width,
3979                       gdouble          height,
3980                       GtkPositionType  gap_side)
3981 {
3982   GtkThemingEngineClass *engine_class;
3983   GtkThemingEngine *engine;
3984
3985   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3986   g_return_if_fail (cr != NULL);
3987
3988   if (width <= 0 || height <= 0)
3989     return;
3990
3991   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3992   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3993
3994   cairo_save (cr);
3995
3996   _gtk_theming_engine_set_context (engine, context);
3997   engine_class->render_extension (engine, cr, x, y, width, height, gap_side);
3998
3999   cairo_restore (cr);
4000 }
4001
4002 /**
4003  * gtk_render_handle:
4004  * @context: a #GtkStyleContext
4005  * @cr: a #cairo_t
4006  * @x: X origin of the rectangle
4007  * @y: Y origin of the rectangle
4008  * @width: rectangle width
4009  * @height: rectangle height
4010  *
4011  * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4012  * #GtkWindow<!-- -->'s resize grip), in the rectangle
4013  * determined by @x, @y, @width, @height.
4014  *
4015  * <example>
4016  * <title>Handles rendered for the paned and grip classes</title>
4017  * <inlinegraphic fileref="handles.png" format="PNG"/>
4018  * </example>
4019  *
4020  * Since: 3.0
4021  **/
4022 void
4023 gtk_render_handle (GtkStyleContext *context,
4024                    cairo_t         *cr,
4025                    gdouble          x,
4026                    gdouble          y,
4027                    gdouble          width,
4028                    gdouble          height)
4029 {
4030   GtkThemingEngineClass *engine_class;
4031   GtkThemingEngine *engine;
4032
4033   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4034   g_return_if_fail (cr != NULL);
4035
4036   if (width <= 0 || height <= 0)
4037     return;
4038
4039   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4040   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4041
4042   cairo_save (cr);
4043
4044   _gtk_theming_engine_set_context (engine, context);
4045   engine_class->render_handle (engine, cr, x, y, width, height);
4046
4047   cairo_restore (cr);
4048 }
4049
4050 /**
4051  * gtk_render_activity:
4052  * @context: a #GtkStyleContext
4053  * @cr: a #cairo_t
4054  * @x: X origin of the rectangle
4055  * @y: Y origin of the rectangle
4056  * @width: rectangle width
4057  * @height: rectangle height
4058  *
4059  * Renders an activity area (Such as in #GtkSpinner or the
4060  * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4061  * determines whether there is activity going on.
4062  *
4063  * Since: 3.0
4064  **/
4065 void
4066 gtk_render_activity (GtkStyleContext *context,
4067                      cairo_t         *cr,
4068                      gdouble          x,
4069                      gdouble          y,
4070                      gdouble          width,
4071                      gdouble          height)
4072 {
4073   GtkThemingEngineClass *engine_class;
4074   GtkThemingEngine *engine;
4075
4076   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4077   g_return_if_fail (cr != NULL);
4078
4079   if (width <= 0 || height <= 0)
4080     return;
4081
4082   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4083   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4084
4085   cairo_save (cr);
4086
4087   _gtk_theming_engine_set_context (engine, context);
4088   engine_class->render_activity (engine, cr, x, y, width, height);
4089
4090   cairo_restore (cr);
4091 }
4092
4093 /**
4094  * gtk_render_icon_pixbuf:
4095  * @context: a #GtkStyleContext
4096  * @source: the #GtkIconSource specifying the icon to render
4097  * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4098  *        means render at the size of the source and don't scale.
4099  *
4100  * Renders the icon specified by @source at the given @size, returning the result
4101  * in a pixbuf.
4102  *
4103  * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4104  *
4105  * Since: 3.0
4106  **/
4107 GdkPixbuf *
4108 gtk_render_icon_pixbuf (GtkStyleContext     *context,
4109                         const GtkIconSource *source,
4110                         GtkIconSize          size)
4111 {
4112   GtkThemingEngineClass *engine_class;
4113   GtkThemingEngine *engine;
4114
4115   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4116   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4117   g_return_val_if_fail (source != NULL, NULL);
4118
4119   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4120   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4121
4122   _gtk_theming_engine_set_context (engine, context);
4123   return engine_class->render_icon_pixbuf (engine, source, size);
4124 }
4125
4126 /**
4127  * gtk_render_icon:
4128  * @context: a #GtkStyleContext
4129  * @cr: a #cairo_t
4130  * @pixbuf: a #GdkPixbuf containing the icon to draw
4131  * @x: X position for the @pixbuf
4132  * @y: Y position for the @pixbuf
4133  *
4134  * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4135  *
4136  * Since: 3.2
4137  **/
4138 void
4139 gtk_render_icon (GtkStyleContext *context,
4140                  cairo_t         *cr,
4141                  GdkPixbuf       *pixbuf,
4142                  gdouble          x,
4143                  gdouble          y)
4144 {
4145   GtkThemingEngineClass *engine_class;
4146   GtkThemingEngine *engine;
4147
4148   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4149   g_return_if_fail (cr != NULL);
4150
4151   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4152   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4153
4154   cairo_save (cr);
4155
4156   _gtk_theming_engine_set_context (engine, context);
4157   engine_class->render_icon (engine, cr, pixbuf, x, y);
4158
4159   cairo_restore (cr);
4160 }
4161
4162 static void
4163 draw_insertion_cursor (GtkStyleContext *context,
4164                        cairo_t         *cr,
4165                        gdouble          x,
4166                        gdouble          y,
4167                        gdouble          height,
4168                        gboolean         is_primary,
4169                        PangoDirection   direction,
4170                        gboolean         draw_arrow)
4171
4172 {
4173   GdkRGBA primary_color;
4174   GdkRGBA secondary_color;
4175   gfloat cursor_aspect_ratio;
4176   gint stem_width;
4177   gint offset;
4178
4179   cairo_save (cr);
4180
4181   _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4182   gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4183
4184   /* When changing the shape or size of the cursor here,
4185    * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4186    */
4187
4188   gtk_style_context_get_style (context,
4189                                "cursor-aspect-ratio", &cursor_aspect_ratio,
4190                                NULL);
4191
4192   stem_width = height * cursor_aspect_ratio + 1;
4193
4194   /* put (stem_width % 2) on the proper side of the cursor */
4195   if (direction == PANGO_DIRECTION_LTR)
4196     offset = stem_width / 2;
4197   else
4198     offset = stem_width - stem_width / 2;
4199
4200   cairo_rectangle (cr, x - offset, y, stem_width, height);
4201   cairo_fill (cr);
4202
4203   if (draw_arrow)
4204     {
4205       gint arrow_width;
4206       gint ax, ay;
4207
4208       arrow_width = stem_width + 1;
4209
4210       if (direction == PANGO_DIRECTION_RTL)
4211         {
4212           ax = x - offset - 1;
4213           ay = y + height - arrow_width * 2 - arrow_width + 1;
4214
4215           cairo_move_to (cr, ax, ay + 1);
4216           cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4217           cairo_line_to (cr, ax, ay + 2 * arrow_width);
4218           cairo_fill (cr);
4219         }
4220       else if (direction == PANGO_DIRECTION_LTR)
4221         {
4222           ax = x + stem_width - offset;
4223           ay = y + height - arrow_width * 2 - arrow_width + 1;
4224
4225           cairo_move_to (cr, ax, ay + 1);
4226           cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4227           cairo_line_to (cr, ax, ay + 2 * arrow_width);
4228           cairo_fill (cr);
4229         }
4230       else
4231         g_assert_not_reached();
4232     }
4233
4234   cairo_restore (cr);
4235 }
4236
4237 /**
4238  * gtk_render_insertion_cursor:
4239  * @context: a #GtkStyleContext
4240  * @cr: a #cairo_t
4241  * @x: X origin
4242  * @y: Y origin
4243  * @layout: the #PangoLayout of the text
4244  * @index: the index in the #PangoLayout
4245  * @direction: the #PangoDirection of the text
4246  *
4247  * Draws a text caret on @cr at the specified index of @layout.
4248  *
4249  * Since: 3.4
4250  **/
4251 void
4252 gtk_render_insertion_cursor (GtkStyleContext *context,
4253                              cairo_t         *cr,
4254                              gdouble          x,
4255                              gdouble          y,
4256                              PangoLayout     *layout,
4257                              int              index,
4258                              PangoDirection   direction)
4259 {
4260   GtkStyleContextPrivate *priv;
4261   gboolean split_cursor;
4262   PangoRectangle strong_pos, weak_pos;
4263   PangoRectangle *cursor1, *cursor2;
4264   PangoDirection keymap_direction;
4265   PangoDirection direction2;
4266
4267   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4268   g_return_if_fail (cr != NULL);
4269   g_return_if_fail (PANGO_IS_LAYOUT (layout));
4270   g_return_if_fail (index >= 0);
4271
4272   priv = context->priv;
4273
4274   g_object_get (gtk_settings_get_for_screen (priv->screen),
4275                 "gtk-split-cursor", &split_cursor,
4276                 NULL);
4277
4278   keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4279
4280   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4281
4282   direction2 = PANGO_DIRECTION_NEUTRAL;
4283
4284   if (split_cursor)
4285     {
4286       cursor1 = &strong_pos;
4287
4288       if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4289         {
4290           direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4291           cursor2 = &weak_pos;
4292         }
4293     }
4294   else
4295     {
4296       if (keymap_direction == direction)
4297         cursor1 = &strong_pos;
4298       else
4299         cursor1 = &weak_pos;
4300     }
4301
4302   draw_insertion_cursor (context,
4303                          cr,
4304                          x + PANGO_PIXELS (cursor1->x),
4305                          y + PANGO_PIXELS (cursor1->y),
4306                          PANGO_PIXELS (cursor1->height),
4307                          TRUE,
4308                          direction,
4309                          direction2 != PANGO_DIRECTION_NEUTRAL);
4310
4311   if (direction2 != PANGO_DIRECTION_NEUTRAL)
4312     {
4313       draw_insertion_cursor (context,
4314                              cr,
4315                              x + PANGO_PIXELS (cursor2->x),
4316                              y + PANGO_PIXELS (cursor2->y),
4317                              PANGO_PIXELS (cursor2->height),
4318                              FALSE,
4319                              direction2,
4320                              TRUE);
4321     }
4322 }
4323
4324 /**
4325  * gtk_draw_insertion_cursor:
4326  * @widget:  a #GtkWidget
4327  * @cr: cairo context to draw to
4328  * @location: location where to draw the cursor (@location->width is ignored)
4329  * @is_primary: if the cursor should be the primary cursor color.
4330  * @direction: whether the cursor is left-to-right or
4331  *             right-to-left. Should never be #GTK_TEXT_DIR_NONE
4332  * @draw_arrow: %TRUE to draw a directional arrow on the
4333  *        cursor. Should be %FALSE unless the cursor is split.
4334  *
4335  * Draws a text caret on @cr at @location. This is not a style function
4336  * but merely a convenience function for drawing the standard cursor shape.
4337  *
4338  * Since: 3.0
4339  * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4340  */
4341 void
4342 gtk_draw_insertion_cursor (GtkWidget          *widget,
4343                            cairo_t            *cr,
4344                            const GdkRectangle *location,
4345                            gboolean            is_primary,
4346                            GtkTextDirection    direction,
4347                            gboolean            draw_arrow)
4348 {
4349   GtkStyleContext *context;
4350
4351   g_return_if_fail (GTK_IS_WIDGET (widget));
4352   g_return_if_fail (cr != NULL);
4353   g_return_if_fail (location != NULL);
4354   g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4355
4356   context = gtk_widget_get_style_context (widget);
4357
4358   draw_insertion_cursor (context, cr,
4359                          location->x, location->y, location->height,
4360                          is_primary,
4361                          (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4362                          draw_arrow);
4363 }
4364
4365 static AtkAttributeSet *
4366 add_attribute (AtkAttributeSet  *attributes,
4367                AtkTextAttribute  attr,
4368                const gchar      *value)
4369 {
4370   AtkAttribute *at;
4371
4372   at = g_new (AtkAttribute, 1);
4373   at->name = g_strdup (atk_text_attribute_get_name (attr));
4374   at->value = g_strdup (value);
4375
4376   return g_slist_prepend (attributes, at);
4377 }
4378
4379 /*
4380  * _gtk_style_context_get_attributes:
4381  * @attributes: a #AtkAttributeSet to add attributes to
4382  * @context: the #GtkStyleContext to get attributes from
4383  * @flags: the state to use with @context
4384  *
4385  * Adds the foreground and background color from @context to
4386  * @attributes, after translating them to ATK attributes.
4387  *
4388  * This is a convenience function that can be used in
4389  * implementing the #AtkText interface in widgets.
4390  *
4391  * Returns: the modified #AtkAttributeSet
4392  */
4393 AtkAttributeSet *
4394 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4395                                    GtkStyleContext *context,
4396                                    GtkStateFlags    flags)
4397 {
4398   GdkRGBA color;
4399   gchar *value;
4400
4401   gtk_style_context_get_background_color (context, flags, &color);
4402   value = g_strdup_printf ("%u,%u,%u",
4403                            (guint) ceil (color.red * 65536 - color.red),
4404                            (guint) ceil (color.green * 65536 - color.green),
4405                            (guint) ceil (color.blue * 65536 - color.blue));
4406   attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4407   g_free (value);
4408
4409   gtk_style_context_get_color (context, flags, &color);
4410   value = g_strdup_printf ("%u,%u,%u",
4411                            (guint) ceil (color.red * 65536 - color.red),
4412                            (guint) ceil (color.green * 65536 - color.green),
4413                            (guint) ceil (color.blue * 65536 - color.blue));
4414   attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);
4415   g_free (value);
4416
4417   return attributes;
4418 }