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