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