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