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