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