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