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