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