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