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