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