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