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