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