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