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