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