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