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