]> Pileus Git - ~andy/gtk/blob - gtk/gtkstylecontext.c
container: Redo style validation handling
[~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   _gtk_css_matcher_init (&matcher, path, state);
909   lookup = _gtk_css_lookup_new ();
910
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           _gtk_css_matcher_init (&matcher, path, priv->info->state_flags);
3065
3066           priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3067                                                                            &matcher);
3068           priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
3069
3070           gtk_widget_path_unref (path);
3071         }
3072     }
3073
3074   if (priv->relevant_changes & change)
3075     {
3076       GtkStyleInfo *info = priv->info;
3077       StyleData *current;
3078
3079       if (info->data)
3080         current = style_data_ref (info->data);
3081       else
3082         current = NULL;
3083
3084       if ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE)
3085         gtk_style_context_clear_cache (context);
3086       else
3087         style_info_set_data (info, NULL);
3088
3089       if (current)
3090         {
3091           StyleData *data;
3092
3093           gtk_style_context_start_animations (context, current->store, timestamp);
3094           change &= ~GTK_CSS_CHANGE_ANIMATE;
3095
3096           data = style_data_lookup (context);
3097
3098           changes = _gtk_css_computed_values_get_difference (data->store, current->store);
3099
3100           style_data_unref (current);
3101         }
3102       else
3103         {
3104           changes = _gtk_bitmask_new ();
3105           changes = _gtk_bitmask_invert_range (changes, 0, _gtk_css_style_property_get_n_properties ());
3106         }
3107     }
3108   else
3109     changes = _gtk_bitmask_new ();
3110
3111   if (change & GTK_CSS_CHANGE_ANIMATE &&
3112       gtk_style_context_is_animating (context))
3113     {
3114       GtkBitmask *animation_changes;
3115
3116       animation_changes = gtk_style_context_update_animations (context, timestamp);
3117       changes = _gtk_bitmask_union (changes, animation_changes);
3118       _gtk_bitmask_free (animation_changes);
3119     }
3120
3121   if (!_gtk_bitmask_is_empty (changes))
3122     gtk_style_context_do_invalidate (context);
3123
3124   change = _gtk_css_change_for_child (change);
3125   for (list = priv->children; list; list = list->next)
3126     {
3127       _gtk_style_context_validate (list->data, timestamp, change);
3128     }
3129
3130   _gtk_bitmask_free (changes);
3131 }
3132
3133 void
3134 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3135                                      GtkCssChange     change)
3136 {
3137   GtkStyleContextPrivate *priv;
3138
3139   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3140   g_return_if_fail (change != 0);
3141
3142   priv = context->priv;
3143
3144   if (priv->widget == NULL && priv->widget_path == NULL)
3145     return;
3146
3147   priv->pending_changes |= change;
3148   gtk_style_context_set_invalid (context, TRUE);
3149 }
3150
3151 /**
3152  * gtk_style_context_invalidate:
3153  * @context: a #GtkStyleContext.
3154  *
3155  * Invalidates @context style information, so it will be reconstructed
3156  * again.
3157  *
3158  * If you're using a #GtkStyleContext returned from
3159  * gtk_widget_get_style_context(), you do not need to
3160  * call this yourself.
3161  *
3162  * Since: 3.0
3163  **/
3164 void
3165 gtk_style_context_invalidate (GtkStyleContext *context)
3166 {
3167   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3168
3169   gtk_style_context_clear_cache (context);
3170   gtk_style_context_do_invalidate (context);
3171 }
3172
3173 /**
3174  * gtk_style_context_set_background:
3175  * @context: a #GtkStyleContext
3176  * @window: a #GdkWindow
3177  *
3178  * Sets the background of @window to the background pattern or
3179  * color specified in @context for its current state.
3180  *
3181  * Since: 3.0
3182  **/
3183 void
3184 gtk_style_context_set_background (GtkStyleContext *context,
3185                                   GdkWindow       *window)
3186 {
3187   GtkStateFlags state;
3188   cairo_pattern_t *pattern;
3189   GdkRGBA *color;
3190
3191   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3192   g_return_if_fail (GDK_IS_WINDOW (window));
3193
3194   state = gtk_style_context_get_state (context);
3195   gtk_style_context_get (context, state,
3196                          "background-image", &pattern,
3197                          NULL);
3198   if (pattern)
3199     {
3200       gdk_window_set_background_pattern (window, pattern);
3201       cairo_pattern_destroy (pattern);
3202       return;
3203     }
3204
3205   gtk_style_context_get (context, state,
3206                          "background-color", &color,
3207                          NULL);
3208   if (color)
3209     {
3210       gdk_window_set_background_rgba (window, color);
3211       gdk_rgba_free (color);
3212     }
3213 }
3214
3215 /**
3216  * gtk_style_context_get_color:
3217  * @context: a #GtkStyleContext
3218  * @state: state to retrieve the color for
3219  * @color: (out): return value for the foreground color
3220  *
3221  * Gets the foreground color for a given state.
3222  *
3223  * Since: 3.0
3224  **/
3225 void
3226 gtk_style_context_get_color (GtkStyleContext *context,
3227                              GtkStateFlags    state,
3228                              GdkRGBA         *color)
3229 {
3230   GdkRGBA *c;
3231
3232   g_return_if_fail (color != NULL);
3233   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3234
3235   gtk_style_context_get (context,
3236                          state,
3237                          "color", &c,
3238                          NULL);
3239
3240   *color = *c;
3241   gdk_rgba_free (c);
3242 }
3243
3244 /**
3245  * gtk_style_context_get_background_color:
3246  * @context: a #GtkStyleContext
3247  * @state: state to retrieve the color for
3248  * @color: (out): return value for the background color
3249  *
3250  * Gets the background color for a given state.
3251  *
3252  * Since: 3.0
3253  **/
3254 void
3255 gtk_style_context_get_background_color (GtkStyleContext *context,
3256                                         GtkStateFlags    state,
3257                                         GdkRGBA         *color)
3258 {
3259   GdkRGBA *c;
3260
3261   g_return_if_fail (color != NULL);
3262   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3263
3264   gtk_style_context_get (context,
3265                          state,
3266                          "background-color", &c,
3267                          NULL);
3268
3269   *color = *c;
3270   gdk_rgba_free (c);
3271 }
3272
3273 /**
3274  * gtk_style_context_get_border_color:
3275  * @context: a #GtkStyleContext
3276  * @state: state to retrieve the color for
3277  * @color: (out): return value for the border color
3278  *
3279  * Gets the border color for a given state.
3280  *
3281  * Since: 3.0
3282  **/
3283 void
3284 gtk_style_context_get_border_color (GtkStyleContext *context,
3285                                     GtkStateFlags    state,
3286                                     GdkRGBA         *color)
3287 {
3288   GdkRGBA *c;
3289
3290   g_return_if_fail (color != NULL);
3291   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3292
3293   gtk_style_context_get (context,
3294                          state,
3295                          "border-color", &c,
3296                          NULL);
3297
3298   *color = *c;
3299   gdk_rgba_free (c);
3300 }
3301
3302 /**
3303  * gtk_style_context_get_border:
3304  * @context: a #GtkStyleContext
3305  * @state: state to retrieve the border for
3306  * @border: (out): return value for the border settings
3307  *
3308  * Gets the border for a given state as a #GtkBorder.
3309  * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3310  *
3311  * Since: 3.0
3312  **/
3313 void
3314 gtk_style_context_get_border (GtkStyleContext *context,
3315                               GtkStateFlags    state,
3316                               GtkBorder       *border)
3317 {
3318   int top, left, bottom, right;
3319
3320   g_return_if_fail (border != NULL);
3321   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3322
3323   gtk_style_context_get (context,
3324                          state,
3325                          "border-top-width", &top,
3326                          "border-left-width", &left,
3327                          "border-bottom-width", &bottom,
3328                          "border-right-width", &right,
3329                          NULL);
3330
3331   border->top = top;
3332   border->left = left;
3333   border->bottom = bottom;
3334   border->right = right;
3335 }
3336
3337 /**
3338  * gtk_style_context_get_padding:
3339  * @context: a #GtkStyleContext
3340  * @state: state to retrieve the padding for
3341  * @padding: (out): return value for the padding settings
3342  *
3343  * Gets the padding for a given state as a #GtkBorder.
3344  * See %GTK_STYLE_PROPERTY_PADDING.
3345  *
3346  * Since: 3.0
3347  **/
3348 void
3349 gtk_style_context_get_padding (GtkStyleContext *context,
3350                                GtkStateFlags    state,
3351                                GtkBorder       *padding)
3352 {
3353   int top, left, bottom, right;
3354
3355   g_return_if_fail (padding != NULL);
3356   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3357
3358   gtk_style_context_get (context,
3359                          state,
3360                          "padding-top", &top,
3361                          "padding-left", &left,
3362                          "padding-bottom", &bottom,
3363                          "padding-right", &right,
3364                          NULL);
3365
3366   padding->top = top;
3367   padding->left = left;
3368   padding->bottom = bottom;
3369   padding->right = right;
3370 }
3371
3372 /**
3373  * gtk_style_context_get_margin:
3374  * @context: a #GtkStyleContext
3375  * @state: state to retrieve the border for
3376  * @margin: (out): return value for the margin settings
3377  *
3378  * Gets the margin for a given state as a #GtkBorder.
3379  * See %GTK_STYLE_PROPERTY_MARGIN.
3380  *
3381  * Since: 3.0
3382  **/
3383 void
3384 gtk_style_context_get_margin (GtkStyleContext *context,
3385                               GtkStateFlags    state,
3386                               GtkBorder       *margin)
3387 {
3388   int top, left, bottom, right;
3389
3390   g_return_if_fail (margin != NULL);
3391   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3392
3393   gtk_style_context_get (context,
3394                          state,
3395                          "margin-top", &top,
3396                          "margin-left", &left,
3397                          "margin-bottom", &bottom,
3398                          "margin-right", &right,
3399                          NULL);
3400
3401   margin->top = top;
3402   margin->left = left;
3403   margin->bottom = bottom;
3404   margin->right = right;
3405 }
3406
3407 /**
3408  * gtk_style_context_get_font:
3409  * @context: a #GtkStyleContext
3410  * @state: state to retrieve the font for
3411  *
3412  * Returns the font description for a given state. The returned
3413  * object is const and will remain valid until the
3414  * #GtkStyleContext::changed signal happens.
3415  *
3416  * Returns: (transfer none): the #PangoFontDescription for the given
3417  *          state.  This object is owned by GTK+ and should not be
3418  *          freed.
3419  *
3420  * Since: 3.0
3421  **/
3422 const PangoFontDescription *
3423 gtk_style_context_get_font (GtkStyleContext *context,
3424                             GtkStateFlags    state)
3425 {
3426   GtkStyleContextPrivate *priv;
3427   StyleData *data;
3428   PangoFontDescription *description;
3429
3430   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3431
3432   priv = context->priv;
3433   g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3434
3435   data = style_data_lookup_for_state (context, state);
3436
3437   /* Yuck, fonts are created on-demand but we don't return a ref.
3438    * Do bad things to achieve this requirement */
3439   description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3440   if (description == NULL)
3441     {
3442       gtk_style_context_get (context, state, "font", &description, NULL);
3443       g_object_set_data_full (G_OBJECT (data->store),
3444                               "font-cache-for-get_font",
3445                               description,
3446                               (GDestroyNotify) pango_font_description_free);
3447     }
3448   return description;
3449 }
3450
3451 static void
3452 get_cursor_color (GtkStyleContext *context,
3453                   gboolean         primary,
3454                   GdkRGBA         *color)
3455 {
3456   GdkColor *style_color;
3457
3458   gtk_style_context_get_style (context,
3459                                primary ? "cursor-color" : "secondary-cursor-color",
3460                                &style_color,
3461                                NULL);
3462
3463   if (style_color)
3464     {
3465       color->red = style_color->red / 65535.0;
3466       color->green = style_color->green / 65535.0;
3467       color->blue = style_color->blue / 65535.0;
3468       color->alpha = 1;
3469
3470       gdk_color_free (style_color);
3471     }
3472   else
3473     {
3474       gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3475
3476       if (!primary)
3477       {
3478         GdkRGBA bg;
3479
3480         gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3481
3482         color->red = (color->red + bg.red) * 0.5;
3483         color->green = (color->green + bg.green) * 0.5;
3484         color->blue = (color->blue + bg.blue) * 0.5;
3485       }
3486     }
3487 }
3488
3489 void
3490 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3491                                      GdkRGBA         *primary_color,
3492                                      GdkRGBA         *secondary_color)
3493 {
3494   if (primary_color)
3495     get_cursor_color (context, TRUE, primary_color);
3496
3497   if (secondary_color)
3498     get_cursor_color (context, FALSE, secondary_color);
3499 }
3500
3501 /* Paint methods */
3502
3503 /**
3504  * gtk_render_check:
3505  * @context: a #GtkStyleContext
3506  * @cr: a #cairo_t
3507  * @x: X origin of the rectangle
3508  * @y: Y origin of the rectangle
3509  * @width: rectangle width
3510  * @height: rectangle height
3511  *
3512  * Renders a checkmark (as in a #GtkCheckButton).
3513  *
3514  * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3515  * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3516  * should be marked as undefined.
3517  *
3518  * <example>
3519  * <title>Typical checkmark rendering</title>
3520  * <inlinegraphic fileref="checks.png" format="PNG"/>
3521  * </example>
3522  *
3523  * Since: 3.0
3524  **/
3525 void
3526 gtk_render_check (GtkStyleContext *context,
3527                   cairo_t         *cr,
3528                   gdouble          x,
3529                   gdouble          y,
3530                   gdouble          width,
3531                   gdouble          height)
3532 {
3533   GtkThemingEngineClass *engine_class;
3534   GtkThemingEngine *engine;
3535
3536   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3537   g_return_if_fail (cr != NULL);
3538
3539   if (width <= 0 || height <= 0)
3540     return;
3541
3542   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3543   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3544
3545   cairo_save (cr);
3546
3547
3548   _gtk_theming_engine_set_context (engine, context);
3549   engine_class->render_check (engine, cr,
3550                               x, y, width, height);
3551
3552   cairo_restore (cr);
3553 }
3554
3555 /**
3556  * gtk_render_option:
3557  * @context: a #GtkStyleContext
3558  * @cr: a #cairo_t
3559  * @x: X origin of the rectangle
3560  * @y: Y origin of the rectangle
3561  * @width: rectangle width
3562  * @height: rectangle height
3563  *
3564  * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3565  * state will determine whether the option is on or off, and
3566  * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3567  *
3568  * <example>
3569  * <title>Typical option mark rendering</title>
3570  * <inlinegraphic fileref="options.png" format="PNG"/>
3571  * </example>
3572  *
3573  * Since: 3.0
3574  **/
3575 void
3576 gtk_render_option (GtkStyleContext *context,
3577                    cairo_t         *cr,
3578                    gdouble          x,
3579                    gdouble          y,
3580                    gdouble          width,
3581                    gdouble          height)
3582 {
3583   GtkThemingEngineClass *engine_class;
3584   GtkThemingEngine *engine;
3585
3586   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3587   g_return_if_fail (cr != NULL);
3588
3589   if (width <= 0 || height <= 0)
3590     return;
3591
3592   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3593   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3594
3595   cairo_save (cr);
3596
3597   _gtk_theming_engine_set_context (engine, context);
3598   engine_class->render_option (engine, cr,
3599                                x, y, width, height);
3600
3601   cairo_restore (cr);
3602 }
3603
3604 /**
3605  * gtk_render_arrow:
3606  * @context: a #GtkStyleContext
3607  * @cr: a #cairo_t
3608  * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3609  * @x: X origin of the render area
3610  * @y: Y origin of the render area
3611  * @size: square side for render area
3612  *
3613  * Renders an arrow pointing to @angle.
3614  *
3615  * <example>
3616  * <title>Typical arrow rendering at 0, 1&solidus;2 &pi;, &pi; and 3&solidus;2 &pi;</title>
3617  * <inlinegraphic fileref="arrows.png" format="PNG"/>
3618  * </example>
3619  *
3620  * Since: 3.0
3621  **/
3622 void
3623 gtk_render_arrow (GtkStyleContext *context,
3624                   cairo_t         *cr,
3625                   gdouble          angle,
3626                   gdouble          x,
3627                   gdouble          y,
3628                   gdouble          size)
3629 {
3630   GtkThemingEngineClass *engine_class;
3631   GtkThemingEngine *engine;
3632
3633   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3634   g_return_if_fail (cr != NULL);
3635
3636   if (size <= 0)
3637     return;
3638
3639   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3640   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3641
3642   cairo_save (cr);
3643
3644   gtk_style_context_save (context);
3645   gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3646
3647   _gtk_theming_engine_set_context (engine, context);
3648   engine_class->render_arrow (engine, cr,
3649                               angle, x, y, size);
3650
3651   gtk_style_context_restore (context);
3652   cairo_restore (cr);
3653 }
3654
3655 /**
3656  * gtk_render_background:
3657  * @context: a #GtkStyleContext
3658  * @cr: a #cairo_t
3659  * @x: X origin of the rectangle
3660  * @y: Y origin of the rectangle
3661  * @width: rectangle width
3662  * @height: rectangle height
3663  *
3664  * Renders the background of an element.
3665  *
3666  * <example>
3667  * <title>Typical background rendering, showing the effect of
3668  * <parameter>background-image</parameter>,
3669  * <parameter>border-width</parameter> and
3670  * <parameter>border-radius</parameter></title>
3671  * <inlinegraphic fileref="background.png" format="PNG"/>
3672  * </example>
3673  *
3674  * Since: 3.0.
3675  **/
3676 void
3677 gtk_render_background (GtkStyleContext *context,
3678                        cairo_t         *cr,
3679                        gdouble          x,
3680                        gdouble          y,
3681                        gdouble          width,
3682                        gdouble          height)
3683 {
3684   GtkThemingEngineClass *engine_class;
3685   GtkThemingEngine *engine;
3686
3687   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3688   g_return_if_fail (cr != NULL);
3689
3690   if (width <= 0 || height <= 0)
3691     return;
3692
3693   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3694   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3695
3696   cairo_save (cr);
3697
3698   _gtk_theming_engine_set_context (engine, context);
3699   engine_class->render_background (engine, cr, x, y, width, height);
3700
3701   cairo_restore (cr);
3702 }
3703
3704 /**
3705  * gtk_render_frame:
3706  * @context: a #GtkStyleContext
3707  * @cr: a #cairo_t
3708  * @x: X origin of the rectangle
3709  * @y: Y origin of the rectangle
3710  * @width: rectangle width
3711  * @height: rectangle height
3712  *
3713  * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3714  *
3715  * <example>
3716  * <title>Examples of frame rendering, showing the effect of
3717  * <parameter>border-image</parameter>,
3718  * <parameter>border-color</parameter>,
3719  * <parameter>border-width</parameter>,
3720  * <parameter>border-radius</parameter> and
3721  * junctions</title>
3722  * <inlinegraphic fileref="frames.png" format="PNG"/>
3723  * </example>
3724  *
3725  * Since: 3.0
3726  **/
3727 void
3728 gtk_render_frame (GtkStyleContext *context,
3729                   cairo_t         *cr,
3730                   gdouble          x,
3731                   gdouble          y,
3732                   gdouble          width,
3733                   gdouble          height)
3734 {
3735   GtkThemingEngineClass *engine_class;
3736   GtkThemingEngine *engine;
3737
3738   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3739   g_return_if_fail (cr != NULL);
3740
3741   if (width <= 0 || height <= 0)
3742     return;
3743
3744   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3745   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3746
3747   cairo_save (cr);
3748
3749   _gtk_theming_engine_set_context (engine, context);
3750   engine_class->render_frame (engine, cr, x, y, width, height);
3751
3752   cairo_restore (cr);
3753 }
3754
3755 /**
3756  * gtk_render_expander:
3757  * @context: a #GtkStyleContext
3758  * @cr: a #cairo_t
3759  * @x: X origin of the rectangle
3760  * @y: Y origin of the rectangle
3761  * @width: rectangle width
3762  * @height: rectangle height
3763  *
3764  * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3765  * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3766  * determines whether the expander is collapsed or expanded.
3767  *
3768  * <example>
3769  * <title>Typical expander rendering</title>
3770  * <inlinegraphic fileref="expanders.png" format="PNG"/>
3771  * </example>
3772  *
3773  * Since: 3.0
3774  **/
3775 void
3776 gtk_render_expander (GtkStyleContext *context,
3777                      cairo_t         *cr,
3778                      gdouble          x,
3779                      gdouble          y,
3780                      gdouble          width,
3781                      gdouble          height)
3782 {
3783   GtkThemingEngineClass *engine_class;
3784   GtkThemingEngine *engine;
3785
3786   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3787   g_return_if_fail (cr != NULL);
3788
3789   if (width <= 0 || height <= 0)
3790     return;
3791
3792   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3793   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3794
3795   cairo_save (cr);
3796
3797   _gtk_theming_engine_set_context (engine, context);
3798   engine_class->render_expander (engine, cr, x, y, width, height);
3799
3800   cairo_restore (cr);
3801 }
3802
3803 /**
3804  * gtk_render_focus:
3805  * @context: a #GtkStyleContext
3806  * @cr: a #cairo_t
3807  * @x: X origin of the rectangle
3808  * @y: Y origin of the rectangle
3809  * @width: rectangle width
3810  * @height: rectangle height
3811  *
3812  * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3813  * <example>
3814  * <title>Typical focus rendering</title>
3815  * <inlinegraphic fileref="focus.png" format="PNG"/>
3816  * </example>
3817  *
3818  * Since: 3.0
3819  **/
3820 void
3821 gtk_render_focus (GtkStyleContext *context,
3822                   cairo_t         *cr,
3823                   gdouble          x,
3824                   gdouble          y,
3825                   gdouble          width,
3826                   gdouble          height)
3827 {
3828   GtkThemingEngineClass *engine_class;
3829   GtkThemingEngine *engine;
3830
3831   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3832   g_return_if_fail (cr != NULL);
3833
3834   if (width <= 0 || height <= 0)
3835     return;
3836
3837   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3838   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3839
3840   cairo_save (cr);
3841
3842   _gtk_theming_engine_set_context (engine, context);
3843   engine_class->render_focus (engine, cr, x, y, width, height);
3844
3845   cairo_restore (cr);
3846 }
3847
3848 /**
3849  * gtk_render_layout:
3850  * @context: a #GtkStyleContext
3851  * @cr: a #cairo_t
3852  * @x: X origin
3853  * @y: Y origin
3854  * @layout: the #PangoLayout to render
3855  *
3856  * Renders @layout on the coordinates @x, @y
3857  *
3858  * Since: 3.0
3859  **/
3860 void
3861 gtk_render_layout (GtkStyleContext *context,
3862                    cairo_t         *cr,
3863                    gdouble          x,
3864                    gdouble          y,
3865                    PangoLayout     *layout)
3866 {
3867   GtkThemingEngineClass *engine_class;
3868   GtkThemingEngine *engine;
3869   PangoRectangle extents;
3870
3871   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3872   g_return_if_fail (PANGO_IS_LAYOUT (layout));
3873   g_return_if_fail (cr != NULL);
3874
3875   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3876   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3877
3878   cairo_save (cr);
3879
3880   pango_layout_get_extents (layout, &extents, NULL);
3881
3882   _gtk_theming_engine_set_context (engine, context);
3883   engine_class->render_layout (engine, cr, x, y, layout);
3884
3885   cairo_restore (cr);
3886 }
3887
3888 /**
3889  * gtk_render_line:
3890  * @context: a #GtkStyleContext
3891  * @cr: a #cairo_t
3892  * @x0: X coordinate for the origin of the line
3893  * @y0: Y coordinate for the origin of the line
3894  * @x1: X coordinate for the end of the line
3895  * @y1: Y coordinate for the end of the line
3896  *
3897  * Renders a line from (x0, y0) to (x1, y1).
3898  *
3899  * Since: 3.0
3900  **/
3901 void
3902 gtk_render_line (GtkStyleContext *context,
3903                  cairo_t         *cr,
3904                  gdouble          x0,
3905                  gdouble          y0,
3906                  gdouble          x1,
3907                  gdouble          y1)
3908 {
3909   GtkThemingEngineClass *engine_class;
3910   GtkThemingEngine *engine;
3911
3912   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3913   g_return_if_fail (cr != NULL);
3914
3915   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3916   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3917
3918   cairo_save (cr);
3919
3920   _gtk_theming_engine_set_context (engine, context);
3921   engine_class->render_line (engine, cr, x0, y0, x1, y1);
3922
3923   cairo_restore (cr);
3924 }
3925
3926 /**
3927  * gtk_render_slider:
3928  * @context: a #GtkStyleContext
3929  * @cr: a #cairo_t
3930  * @x: X origin of the rectangle
3931  * @y: Y origin of the rectangle
3932  * @width: rectangle width
3933  * @height: rectangle height
3934  * @orientation: orientation of the slider
3935  *
3936  * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
3937  * @width, @height. @orientation defines whether the slider is vertical
3938  * or horizontal.
3939  *
3940  * <example>
3941  * <title>Typical slider rendering</title>
3942  * <inlinegraphic fileref="sliders.png" format="PNG"/>
3943  * </example>
3944  *
3945  * Since: 3.0
3946  **/
3947 void
3948 gtk_render_slider (GtkStyleContext *context,
3949                    cairo_t         *cr,
3950                    gdouble          x,
3951                    gdouble          y,
3952                    gdouble          width,
3953                    gdouble          height,
3954                    GtkOrientation   orientation)
3955 {
3956   GtkThemingEngineClass *engine_class;
3957   GtkThemingEngine *engine;
3958
3959   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3960   g_return_if_fail (cr != NULL);
3961
3962   if (width <= 0 || height <= 0)
3963     return;
3964
3965   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3966   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3967
3968   cairo_save (cr);
3969
3970   _gtk_theming_engine_set_context (engine, context);
3971   engine_class->render_slider (engine, cr, x, y, width, height, orientation);
3972
3973   cairo_restore (cr);
3974 }
3975
3976 /**
3977  * gtk_render_frame_gap:
3978  * @context: a #GtkStyleContext
3979  * @cr: a #cairo_t
3980  * @x: X origin of the rectangle
3981  * @y: Y origin of the rectangle
3982  * @width: rectangle width
3983  * @height: rectangle height
3984  * @gap_side: side where the gap is
3985  * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
3986  * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
3987  *
3988  * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
3989  * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
3990  * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
3991  * %GTK_POS_LEFT and %GTK_POS_RIGHT.
3992  *
3993  * <example>
3994  * <title>Typical rendering of a frame with a gap</title>
3995  * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
3996  * </example>
3997  *
3998  * Since: 3.0
3999  **/
4000 void
4001 gtk_render_frame_gap (GtkStyleContext *context,
4002                       cairo_t         *cr,
4003                       gdouble          x,
4004                       gdouble          y,
4005                       gdouble          width,
4006                       gdouble          height,
4007                       GtkPositionType  gap_side,
4008                       gdouble          xy0_gap,
4009                       gdouble          xy1_gap)
4010 {
4011   GtkThemingEngineClass *engine_class;
4012   GtkThemingEngine *engine;
4013
4014   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4015   g_return_if_fail (cr != NULL);
4016   g_return_if_fail (xy0_gap <= xy1_gap);
4017   g_return_if_fail (xy0_gap >= 0);
4018
4019   if (width <= 0 || height <= 0)
4020     return;
4021
4022   if (gap_side == GTK_POS_LEFT ||
4023       gap_side == GTK_POS_RIGHT)
4024     g_return_if_fail (xy1_gap <= height);
4025   else
4026     g_return_if_fail (xy1_gap <= width);
4027
4028   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4029   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4030
4031   cairo_save (cr);
4032
4033   _gtk_theming_engine_set_context (engine, context);
4034   engine_class->render_frame_gap (engine, cr,
4035                                   x, y, width, height, gap_side,
4036                                   xy0_gap, xy1_gap);
4037
4038   cairo_restore (cr);
4039 }
4040
4041 /**
4042  * gtk_render_extension:
4043  * @context: a #GtkStyleContext
4044  * @cr: a #cairo_t
4045  * @x: X origin of the rectangle
4046  * @y: Y origin of the rectangle
4047  * @width: rectangle width
4048  * @height: rectangle height
4049  * @gap_side: side where the gap is
4050  *
4051  * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4052  * defined by @x, @y, @width, @height. The side where the extension
4053  * connects to is defined by @gap_side.
4054  *
4055  * <example>
4056  * <title>Typical extension rendering</title>
4057  * <inlinegraphic fileref="extensions.png" format="PNG"/>
4058  * </example>
4059  *
4060  * Since: 3.0
4061  **/
4062 void
4063 gtk_render_extension (GtkStyleContext *context,
4064                       cairo_t         *cr,
4065                       gdouble          x,
4066                       gdouble          y,
4067                       gdouble          width,
4068                       gdouble          height,
4069                       GtkPositionType  gap_side)
4070 {
4071   GtkThemingEngineClass *engine_class;
4072   GtkThemingEngine *engine;
4073
4074   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4075   g_return_if_fail (cr != NULL);
4076
4077   if (width <= 0 || height <= 0)
4078     return;
4079
4080   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4081   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4082
4083   cairo_save (cr);
4084
4085   _gtk_theming_engine_set_context (engine, context);
4086   engine_class->render_extension (engine, cr, x, y, width, height, gap_side);
4087
4088   cairo_restore (cr);
4089 }
4090
4091 /**
4092  * gtk_render_handle:
4093  * @context: a #GtkStyleContext
4094  * @cr: a #cairo_t
4095  * @x: X origin of the rectangle
4096  * @y: Y origin of the rectangle
4097  * @width: rectangle width
4098  * @height: rectangle height
4099  *
4100  * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4101  * #GtkWindow<!-- -->'s resize grip), in the rectangle
4102  * determined by @x, @y, @width, @height.
4103  *
4104  * <example>
4105  * <title>Handles rendered for the paned and grip classes</title>
4106  * <inlinegraphic fileref="handles.png" format="PNG"/>
4107  * </example>
4108  *
4109  * Since: 3.0
4110  **/
4111 void
4112 gtk_render_handle (GtkStyleContext *context,
4113                    cairo_t         *cr,
4114                    gdouble          x,
4115                    gdouble          y,
4116                    gdouble          width,
4117                    gdouble          height)
4118 {
4119   GtkThemingEngineClass *engine_class;
4120   GtkThemingEngine *engine;
4121
4122   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4123   g_return_if_fail (cr != NULL);
4124
4125   if (width <= 0 || height <= 0)
4126     return;
4127
4128   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4129   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4130
4131   cairo_save (cr);
4132
4133   _gtk_theming_engine_set_context (engine, context);
4134   engine_class->render_handle (engine, cr, x, y, width, height);
4135
4136   cairo_restore (cr);
4137 }
4138
4139 /**
4140  * gtk_render_activity:
4141  * @context: a #GtkStyleContext
4142  * @cr: a #cairo_t
4143  * @x: X origin of the rectangle
4144  * @y: Y origin of the rectangle
4145  * @width: rectangle width
4146  * @height: rectangle height
4147  *
4148  * Renders an activity area (Such as in #GtkSpinner or the
4149  * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4150  * determines whether there is activity going on.
4151  *
4152  * Since: 3.0
4153  **/
4154 void
4155 gtk_render_activity (GtkStyleContext *context,
4156                      cairo_t         *cr,
4157                      gdouble          x,
4158                      gdouble          y,
4159                      gdouble          width,
4160                      gdouble          height)
4161 {
4162   GtkThemingEngineClass *engine_class;
4163   GtkThemingEngine *engine;
4164
4165   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4166   g_return_if_fail (cr != NULL);
4167
4168   if (width <= 0 || height <= 0)
4169     return;
4170
4171   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4172   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4173
4174   cairo_save (cr);
4175
4176   _gtk_theming_engine_set_context (engine, context);
4177   engine_class->render_activity (engine, cr, x, y, width, height);
4178
4179   cairo_restore (cr);
4180 }
4181
4182 /**
4183  * gtk_render_icon_pixbuf:
4184  * @context: a #GtkStyleContext
4185  * @source: the #GtkIconSource specifying the icon to render
4186  * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4187  *        means render at the size of the source and don't scale.
4188  *
4189  * Renders the icon specified by @source at the given @size, returning the result
4190  * in a pixbuf.
4191  *
4192  * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4193  *
4194  * Since: 3.0
4195  **/
4196 GdkPixbuf *
4197 gtk_render_icon_pixbuf (GtkStyleContext     *context,
4198                         const GtkIconSource *source,
4199                         GtkIconSize          size)
4200 {
4201   GtkThemingEngineClass *engine_class;
4202   GtkThemingEngine *engine;
4203
4204   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4205   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4206   g_return_val_if_fail (source != NULL, NULL);
4207
4208   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4209   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4210
4211   _gtk_theming_engine_set_context (engine, context);
4212   return engine_class->render_icon_pixbuf (engine, source, size);
4213 }
4214
4215 /**
4216  * gtk_render_icon:
4217  * @context: a #GtkStyleContext
4218  * @cr: a #cairo_t
4219  * @pixbuf: a #GdkPixbuf containing the icon to draw
4220  * @x: X position for the @pixbuf
4221  * @y: Y position for the @pixbuf
4222  *
4223  * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4224  *
4225  * Since: 3.2
4226  **/
4227 void
4228 gtk_render_icon (GtkStyleContext *context,
4229                  cairo_t         *cr,
4230                  GdkPixbuf       *pixbuf,
4231                  gdouble          x,
4232                  gdouble          y)
4233 {
4234   GtkThemingEngineClass *engine_class;
4235   GtkThemingEngine *engine;
4236
4237   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4238   g_return_if_fail (cr != NULL);
4239
4240   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4241   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4242
4243   cairo_save (cr);
4244
4245   _gtk_theming_engine_set_context (engine, context);
4246   engine_class->render_icon (engine, cr, pixbuf, x, y);
4247
4248   cairo_restore (cr);
4249 }
4250
4251 static void
4252 draw_insertion_cursor (GtkStyleContext *context,
4253                        cairo_t         *cr,
4254                        gdouble          x,
4255                        gdouble          y,
4256                        gdouble          height,
4257                        gboolean         is_primary,
4258                        PangoDirection   direction,
4259                        gboolean         draw_arrow)
4260
4261 {
4262   GdkRGBA primary_color;
4263   GdkRGBA secondary_color;
4264   gfloat cursor_aspect_ratio;
4265   gint stem_width;
4266   gint offset;
4267
4268   cairo_save (cr);
4269
4270   _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4271   gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4272
4273   /* When changing the shape or size of the cursor here,
4274    * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4275    */
4276
4277   gtk_style_context_get_style (context,
4278                                "cursor-aspect-ratio", &cursor_aspect_ratio,
4279                                NULL);
4280
4281   stem_width = height * cursor_aspect_ratio + 1;
4282
4283   /* put (stem_width % 2) on the proper side of the cursor */
4284   if (direction == PANGO_DIRECTION_LTR)
4285     offset = stem_width / 2;
4286   else
4287     offset = stem_width - stem_width / 2;
4288
4289   cairo_rectangle (cr, x - offset, y, stem_width, height);
4290   cairo_fill (cr);
4291
4292   if (draw_arrow)
4293     {
4294       gint arrow_width;
4295       gint ax, ay;
4296
4297       arrow_width = stem_width + 1;
4298
4299       if (direction == PANGO_DIRECTION_RTL)
4300         {
4301           ax = x - offset - 1;
4302           ay = y + height - arrow_width * 2 - arrow_width + 1;
4303
4304           cairo_move_to (cr, ax, ay + 1);
4305           cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4306           cairo_line_to (cr, ax, ay + 2 * arrow_width);
4307           cairo_fill (cr);
4308         }
4309       else if (direction == PANGO_DIRECTION_LTR)
4310         {
4311           ax = x + stem_width - offset;
4312           ay = y + height - arrow_width * 2 - arrow_width + 1;
4313
4314           cairo_move_to (cr, ax, ay + 1);
4315           cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4316           cairo_line_to (cr, ax, ay + 2 * arrow_width);
4317           cairo_fill (cr);
4318         }
4319       else
4320         g_assert_not_reached();
4321     }
4322
4323   cairo_restore (cr);
4324 }
4325
4326 /**
4327  * gtk_render_insertion_cursor:
4328  * @context: a #GtkStyleContext
4329  * @cr: a #cairo_t
4330  * @x: X origin
4331  * @y: Y origin
4332  * @layout: the #PangoLayout of the text
4333  * @index: the index in the #PangoLayout
4334  * @direction: the #PangoDirection of the text
4335  *
4336  * Draws a text caret on @cr at the specified index of @layout.
4337  *
4338  * Since: 3.4
4339  **/
4340 void
4341 gtk_render_insertion_cursor (GtkStyleContext *context,
4342                              cairo_t         *cr,
4343                              gdouble          x,
4344                              gdouble          y,
4345                              PangoLayout     *layout,
4346                              int              index,
4347                              PangoDirection   direction)
4348 {
4349   GtkStyleContextPrivate *priv;
4350   gboolean split_cursor;
4351   PangoRectangle strong_pos, weak_pos;
4352   PangoRectangle *cursor1, *cursor2;
4353   PangoDirection keymap_direction;
4354   PangoDirection direction2;
4355
4356   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4357   g_return_if_fail (cr != NULL);
4358   g_return_if_fail (PANGO_IS_LAYOUT (layout));
4359   g_return_if_fail (index >= 0);
4360
4361   priv = context->priv;
4362
4363   g_object_get (gtk_settings_get_for_screen (priv->screen),
4364                 "gtk-split-cursor", &split_cursor,
4365                 NULL);
4366
4367   keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4368
4369   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4370
4371   direction2 = PANGO_DIRECTION_NEUTRAL;
4372
4373   if (split_cursor)
4374     {
4375       cursor1 = &strong_pos;
4376
4377       if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4378         {
4379           direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4380           cursor2 = &weak_pos;
4381         }
4382     }
4383   else
4384     {
4385       if (keymap_direction == direction)
4386         cursor1 = &strong_pos;
4387       else
4388         cursor1 = &weak_pos;
4389     }
4390
4391   draw_insertion_cursor (context,
4392                          cr,
4393                          x + PANGO_PIXELS (cursor1->x),
4394                          y + PANGO_PIXELS (cursor1->y),
4395                          PANGO_PIXELS (cursor1->height),
4396                          TRUE,
4397                          direction,
4398                          direction2 != PANGO_DIRECTION_NEUTRAL);
4399
4400   if (direction2 != PANGO_DIRECTION_NEUTRAL)
4401     {
4402       draw_insertion_cursor (context,
4403                              cr,
4404                              x + PANGO_PIXELS (cursor2->x),
4405                              y + PANGO_PIXELS (cursor2->y),
4406                              PANGO_PIXELS (cursor2->height),
4407                              FALSE,
4408                              direction2,
4409                              TRUE);
4410     }
4411 }
4412
4413 /**
4414  * gtk_draw_insertion_cursor:
4415  * @widget:  a #GtkWidget
4416  * @cr: cairo context to draw to
4417  * @location: location where to draw the cursor (@location->width is ignored)
4418  * @is_primary: if the cursor should be the primary cursor color.
4419  * @direction: whether the cursor is left-to-right or
4420  *             right-to-left. Should never be #GTK_TEXT_DIR_NONE
4421  * @draw_arrow: %TRUE to draw a directional arrow on the
4422  *        cursor. Should be %FALSE unless the cursor is split.
4423  *
4424  * Draws a text caret on @cr at @location. This is not a style function
4425  * but merely a convenience function for drawing the standard cursor shape.
4426  *
4427  * Since: 3.0
4428  * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4429  */
4430 void
4431 gtk_draw_insertion_cursor (GtkWidget          *widget,
4432                            cairo_t            *cr,
4433                            const GdkRectangle *location,
4434                            gboolean            is_primary,
4435                            GtkTextDirection    direction,
4436                            gboolean            draw_arrow)
4437 {
4438   GtkStyleContext *context;
4439
4440   g_return_if_fail (GTK_IS_WIDGET (widget));
4441   g_return_if_fail (cr != NULL);
4442   g_return_if_fail (location != NULL);
4443   g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4444
4445   context = gtk_widget_get_style_context (widget);
4446
4447   draw_insertion_cursor (context, cr,
4448                          location->x, location->y, location->height,
4449                          is_primary,
4450                          (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4451                          draw_arrow);
4452 }
4453
4454 static AtkAttributeSet *
4455 add_attribute (AtkAttributeSet  *attributes,
4456                AtkTextAttribute  attr,
4457                const gchar      *value)
4458 {
4459   AtkAttribute *at;
4460
4461   at = g_new (AtkAttribute, 1);
4462   at->name = g_strdup (atk_text_attribute_get_name (attr));
4463   at->value = g_strdup (value);
4464
4465   return g_slist_prepend (attributes, at);
4466 }
4467
4468 /*
4469  * _gtk_style_context_get_attributes:
4470  * @attributes: a #AtkAttributeSet to add attributes to
4471  * @context: the #GtkStyleContext to get attributes from
4472  * @flags: the state to use with @context
4473  *
4474  * Adds the foreground and background color from @context to
4475  * @attributes, after translating them to ATK attributes.
4476  *
4477  * This is a convenience function that can be used in
4478  * implementing the #AtkText interface in widgets.
4479  *
4480  * Returns: the modified #AtkAttributeSet
4481  */
4482 AtkAttributeSet *
4483 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4484                                    GtkStyleContext *context,
4485                                    GtkStateFlags    flags)
4486 {
4487   GdkRGBA color;
4488   gchar *value;
4489
4490   gtk_style_context_get_background_color (context, flags, &color);
4491   value = g_strdup_printf ("%u,%u,%u",
4492                            (guint) ceil (color.red * 65536 - color.red),
4493                            (guint) ceil (color.green * 65536 - color.green),
4494                            (guint) ceil (color.blue * 65536 - color.blue));
4495   attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4496   g_free (value);
4497
4498   gtk_style_context_get_color (context, flags, &color);
4499   value = g_strdup_printf ("%u,%u,%u",
4500                            (guint) ceil (color.red * 65536 - color.red),
4501                            (guint) ceil (color.green * 65536 - color.green),
4502                            (guint) ceil (color.blue * 65536 - color.blue));
4503   attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);
4504   g_free (value);
4505
4506   return attributes;
4507 }