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