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