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