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