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