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