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