]> Pileus Git - ~andy/gtk/blob - gtk/gtkstylecontext.c
css: Make color lookup handle dependencies
[~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, NULL))
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                                         GtkCssDependencies  current_deps,
2683                                         GtkCssValue        *color,
2684                                         GtkCssDependencies *dependencies)
2685 {
2686   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2687   g_return_val_if_fail (current != NULL, FALSE);
2688   g_return_val_if_fail (color != NULL, FALSE);
2689
2690   return _gtk_symbolic_color_resolve_full ((GtkSymbolicColor *) color,
2691                                            current,
2692                                            current_deps,
2693                                            gtk_style_context_color_lookup_func,
2694                                            context,
2695                                            dependencies);
2696 }
2697
2698
2699 gboolean
2700 _gtk_style_context_resolve_color (GtkStyleContext    *context,
2701                                   GtkSymbolicColor   *color,
2702                                   GdkRGBA            *result,
2703                                   GtkCssDependencies *dependencies)
2704 {
2705   GtkCssValue *val;
2706
2707   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2708   g_return_val_if_fail (color != NULL, FALSE);
2709   g_return_val_if_fail (result != NULL, FALSE);
2710
2711   val = _gtk_symbolic_color_resolve_full (color,
2712                                           _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR),
2713                                           GTK_CSS_DEPENDS_ON_COLOR,
2714                                           gtk_style_context_color_lookup_func,
2715                                           context,
2716                                           dependencies);
2717   if (val == NULL)
2718     return FALSE;
2719
2720   *result = *_gtk_css_rgba_value_get_rgba (val);
2721   _gtk_css_value_unref (val);
2722   return TRUE;
2723 }
2724
2725 /**
2726  * gtk_style_context_lookup_color:
2727  * @context: a #GtkStyleContext
2728  * @color_name: color name to lookup
2729  * @color: (out): Return location for the looked up color
2730  *
2731  * Looks up and resolves a color name in the @context color map.
2732  *
2733  * Returns: %TRUE if @color_name was found and resolved, %FALSE otherwise
2734  **/
2735 gboolean
2736 gtk_style_context_lookup_color (GtkStyleContext *context,
2737                                 const gchar     *color_name,
2738                                 GdkRGBA         *color)
2739 {
2740   GtkSymbolicColor *sym_color;
2741
2742   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), FALSE);
2743   g_return_val_if_fail (color_name != NULL, FALSE);
2744   g_return_val_if_fail (color != NULL, FALSE);
2745
2746   sym_color = gtk_style_context_color_lookup_func (context, color_name);
2747   if (sym_color == NULL)
2748     return FALSE;
2749
2750   return _gtk_style_context_resolve_color (context, sym_color, color, NULL);
2751 }
2752
2753 /**
2754  * gtk_style_context_notify_state_change:
2755  * @context: a #GtkStyleContext
2756  * @window: a #GdkWindow
2757  * @region_id: (allow-none): animatable region to notify on, or %NULL.
2758  *     See gtk_style_context_push_animatable_region()
2759  * @state: state to trigger transition for
2760  * @state_value: %TRUE if @state is the state we are changing to,
2761  *     %FALSE if we are changing away from it
2762  *
2763  * Notifies a state change on @context, so if the current style makes use
2764  * of transition animations, one will be started so all rendered elements
2765  * under @region_id are animated for state @state being set to value
2766  * @state_value.
2767  *
2768  * The @window parameter is used in order to invalidate the rendered area
2769  * as the animation runs, so make sure it is the same window that is being
2770  * rendered on by the gtk_render_*() functions.
2771  *
2772  * If @region_id is %NULL, all rendered elements using @context will be
2773  * affected by this state transition.
2774  *
2775  * As a practical example, a #GtkButton notifying a state transition on
2776  * the prelight state:
2777  * <programlisting>
2778  * gtk_style_context_notify_state_change (context,
2779  *                                        gtk_widget_get_window (widget),
2780  *                                        NULL,
2781  *                                        GTK_STATE_PRELIGHT,
2782  *                                        button->in_button);
2783  * </programlisting>
2784  *
2785  * Can be handled in the CSS file like this:
2786  * <programlisting>
2787  * GtkButton {
2788  *     background-color: &num;f00
2789  * }
2790  *
2791  * GtkButton:hover {
2792  *     background-color: &num;fff;
2793  *     transition: 200ms linear
2794  * }
2795  * </programlisting>
2796  *
2797  * This combination will animate the button background from red to white
2798  * if a pointer enters the button, and back to red if the pointer leaves
2799  * the button.
2800  *
2801  * Note that @state is used when finding the transition parameters, which
2802  * is why the style places the transition under the :hover pseudo-class.
2803  *
2804  * Since: 3.0
2805  *
2806  * Deprecated: 3.6: This function does nothing.
2807  **/
2808 void
2809 gtk_style_context_notify_state_change (GtkStyleContext *context,
2810                                        GdkWindow       *window,
2811                                        gpointer         region_id,
2812                                        GtkStateType     state,
2813                                        gboolean         state_value)
2814 {
2815   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2816   g_return_if_fail (GDK_IS_WINDOW (window));
2817   g_return_if_fail (state > GTK_STATE_NORMAL && state <= GTK_STATE_FOCUSED);
2818   g_return_if_fail (context->priv->widget != NULL || context->priv->widget_path != NULL);
2819 }
2820
2821 /**
2822  * gtk_style_context_cancel_animations:
2823  * @context: a #GtkStyleContext
2824  * @region_id: (allow-none): animatable region to stop, or %NULL.
2825  *     See gtk_style_context_push_animatable_region()
2826  *
2827  * Stops all running animations for @region_id and all animatable
2828  * regions underneath.
2829  *
2830  * A %NULL @region_id will stop all ongoing animations in @context,
2831  * when dealing with a #GtkStyleContext obtained through
2832  * gtk_widget_get_style_context(), this is normally done for you
2833  * in all circumstances you would expect all widget to be stopped,
2834  * so this should be only used in complex widgets with different
2835  * animatable regions.
2836  *
2837  * Since: 3.0
2838  *
2839  * Deprecated: 3.6: This function does nothing.
2840  **/
2841 void
2842 gtk_style_context_cancel_animations (GtkStyleContext *context,
2843                                      gpointer         region_id)
2844 {
2845   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2846 }
2847
2848 /**
2849  * gtk_style_context_scroll_animations:
2850  * @context: a #GtkStyleContext
2851  * @window: a #GdkWindow used previously in
2852  *          gtk_style_context_notify_state_change()
2853  * @dx: Amount to scroll in the X axis
2854  * @dy: Amount to scroll in the Y axis
2855  *
2856  * This function is analogous to gdk_window_scroll(), and
2857  * should be called together with it so the invalidation
2858  * areas for any ongoing animation are scrolled together
2859  * with it.
2860  *
2861  * Since: 3.0
2862  *
2863  * Deprecated: 3.6: This function does nothing.
2864  **/
2865 void
2866 gtk_style_context_scroll_animations (GtkStyleContext *context,
2867                                      GdkWindow       *window,
2868                                      gint             dx,
2869                                      gint             dy)
2870 {
2871   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2872   g_return_if_fail (GDK_IS_WINDOW (window));
2873 }
2874
2875 /**
2876  * gtk_style_context_push_animatable_region:
2877  * @context: a #GtkStyleContext
2878  * @region_id: unique identifier for the animatable region
2879  *
2880  * Pushes an animatable region, so all further gtk_render_*() calls between
2881  * this call and the following gtk_style_context_pop_animatable_region()
2882  * will potentially show transition animations for this region if
2883  * gtk_style_context_notify_state_change() is called for a given state,
2884  * and the current theme/style defines transition animations for state
2885  * changes.
2886  *
2887  * The @region_id used must be unique in @context so the theming engine
2888  * can uniquely identify rendered elements subject to a state transition.
2889  *
2890  * Since: 3.0
2891  *
2892  * Deprecated: 3.6: This function does nothing.
2893  **/
2894 void
2895 gtk_style_context_push_animatable_region (GtkStyleContext *context,
2896                                           gpointer         region_id)
2897 {
2898   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2899   g_return_if_fail (region_id != NULL);
2900 }
2901
2902 /**
2903  * gtk_style_context_pop_animatable_region:
2904  * @context: a #GtkStyleContext
2905  *
2906  * Pops an animatable region from @context.
2907  * See gtk_style_context_push_animatable_region().
2908  *
2909  * Since: 3.0
2910  *
2911  * Deprecated: 3.6: This function does nothing.
2912  **/
2913 void
2914 gtk_style_context_pop_animatable_region (GtkStyleContext *context)
2915 {
2916   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2917 }
2918
2919 static void
2920 gtk_style_context_clear_cache (GtkStyleContext *context)
2921 {
2922   GtkStyleContextPrivate *priv;
2923   GtkStyleInfo *info;
2924
2925   priv = context->priv;
2926
2927   for (info = priv->info; info; info = info->next)
2928     {
2929       style_info_set_data (info, NULL);
2930     }
2931   g_hash_table_remove_all (priv->style_data);
2932 }
2933
2934 static void
2935 gtk_style_context_update_cache (GtkStyleContext  *context,
2936                                 const GtkBitmask *parent_changes)
2937 {
2938   GtkStyleContextPrivate *priv;
2939   GHashTableIter iter;
2940   gpointer key, value;
2941
2942   priv = context->priv;
2943
2944   g_hash_table_remove_all (priv->style_data);
2945
2946   g_hash_table_iter_init (&iter, priv->style_data);
2947   while (g_hash_table_iter_next (&iter, &key, &value))
2948     {
2949       GtkStyleInfo *info = key;
2950       StyleData *data = value;
2951       GtkBitmask *changes;
2952
2953       changes = _gtk_bitmask_copy (parent_changes);
2954       changes = _gtk_bitmask_intersect (changes, data->store->depends_on_parent);
2955       if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
2956         changes = _gtk_bitmask_union (changes, data->store->depends_on_color);
2957       if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
2958         changes = _gtk_bitmask_union (changes, data->store->depends_on_font_size);
2959
2960       build_properties (context, data->store, info->state_flags, changes);
2961     }
2962 }
2963
2964 static void
2965 gtk_style_context_do_invalidate (GtkStyleContext *context)
2966 {
2967   GtkStyleContextPrivate *priv;
2968
2969   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2970
2971   priv = context->priv;
2972
2973   /* Avoid reentrancy */
2974   if (priv->invalidating_context)
2975     return;
2976
2977   priv->invalidating_context = TRUE;
2978
2979   g_signal_emit (context, signals[CHANGED], 0);
2980
2981   priv->invalidating_context = FALSE;
2982 }
2983
2984 void
2985 _gtk_style_context_stop_animations (GtkStyleContext *context)
2986 {
2987   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
2988
2989   if (!gtk_style_context_is_animating (context))
2990     return;
2991
2992   style_info_set_data (context->priv->info, NULL);
2993
2994   gtk_style_context_stop_animating (context);
2995 }
2996
2997 static GtkBitmask *
2998 gtk_style_context_update_animations (GtkStyleContext *context,
2999                                      gint64           timestamp)
3000 {
3001   GtkBitmask *differences;
3002   StyleData *style_data;
3003   
3004   style_data = style_data_lookup (context);
3005
3006   differences = _gtk_css_animated_values_advance (GTK_CSS_ANIMATED_VALUES (style_data->store),
3007                                                   timestamp);
3008
3009   if (_gtk_css_animated_values_is_finished (GTK_CSS_ANIMATED_VALUES (style_data->store)))
3010     _gtk_style_context_stop_animations (context);
3011
3012   return differences;
3013 }
3014
3015 static gboolean
3016 gtk_style_context_should_animate (GtkStyleContext *context)
3017 {
3018   GtkStyleContextPrivate *priv;
3019   gboolean animate;
3020
3021   priv = context->priv;
3022
3023   if (priv->widget == NULL)
3024     return FALSE;
3025
3026   if (!gtk_widget_get_mapped (priv->widget))
3027     return FALSE;
3028
3029   g_object_get (gtk_widget_get_settings (context->priv->widget),
3030                 "gtk-enable-animations", &animate,
3031                 NULL);
3032
3033   return animate;
3034 }
3035
3036 static void
3037 gtk_style_context_start_animations (GtkStyleContext      *context,
3038                                     GtkCssComputedValues *previous,
3039                                     gint64                timestamp)
3040 {
3041   StyleData *animated;
3042
3043   if (!gtk_style_context_should_animate (context))
3044     {
3045       gtk_style_context_stop_animating (context);
3046       return;
3047     }
3048
3049   animated = style_data_new ();
3050   animated->store = _gtk_css_animated_values_new (style_data_lookup (context)->store,
3051                                                   previous,
3052                                                   timestamp);
3053
3054   if (_gtk_css_animated_values_is_finished (GTK_CSS_ANIMATED_VALUES (animated->store)))
3055     {
3056       style_data_unref (animated);
3057       gtk_style_context_stop_animating (context);
3058       return;
3059     }
3060
3061   style_info_set_data (context->priv->info, animated);
3062   style_data_unref (animated);
3063   gtk_style_context_start_animating (context);
3064 }
3065
3066 static gboolean
3067 gtk_style_context_needs_full_revalidate (GtkStyleContext  *context,
3068                                          GtkCssChange      change)
3069 {
3070   GtkStyleContextPrivate *priv = context->priv;
3071
3072   /* Try to avoid invalidating if we can */
3073   if (change & GTK_STYLE_CONTEXT_RADICAL_CHANGE)
3074     {
3075       priv->relevant_changes = GTK_CSS_CHANGE_ANY;
3076     }
3077   else
3078     {
3079       if (priv->relevant_changes == GTK_CSS_CHANGE_ANY)
3080         {
3081           GtkWidgetPath *path;
3082           GtkCssMatcher matcher;
3083
3084           path = create_query_path (context);
3085           if (_gtk_css_matcher_init (&matcher, path, priv->info->state_flags))
3086             priv->relevant_changes = _gtk_style_provider_private_get_change (GTK_STYLE_PROVIDER_PRIVATE (priv->cascade),
3087                                                                              &matcher);
3088           else
3089             priv->relevant_changes = 0;
3090
3091           priv->relevant_changes &= ~GTK_STYLE_CONTEXT_RADICAL_CHANGE;
3092
3093           gtk_widget_path_unref (path);
3094         }
3095     }
3096
3097   if (priv->relevant_changes & change)
3098     return TRUE;
3099   else
3100     return FALSE;
3101 }
3102
3103 void
3104 _gtk_style_context_validate (GtkStyleContext  *context,
3105                              gint64            timestamp,
3106                              GtkCssChange      change,
3107                              const GtkBitmask *parent_changes)
3108 {
3109   GtkStyleContextPrivate *priv;
3110   GtkStyleInfo *info;
3111   StyleData *current;
3112   GtkBitmask *changes;
3113   GSList *list;
3114
3115   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3116
3117   priv = context->priv;
3118
3119   change |= priv->pending_changes;
3120   
3121   /* If you run your application with
3122    *   GTK_DEBUG=no-css-cache
3123    * every invalidation will purge the cache and completely query
3124    * everything anew form the cache. This is slow (in particular
3125    * when animating), but useful for figuring out bugs.
3126    *
3127    * We achieve that by pretending that everything that could have
3128    * changed has and so we of course totally need to redo everything.
3129    *
3130    * Note that this also completely revalidates child widgets all
3131    * the time.
3132    */
3133   if (G_UNLIKELY (gtk_get_debug_flags () & GTK_DEBUG_NO_CSS_CACHE))
3134     change = GTK_CSS_CHANGE_ANY;
3135
3136   if (!priv->invalid && change == 0 && _gtk_bitmask_is_empty (parent_changes))
3137     return;
3138
3139   priv->pending_changes = 0;
3140   gtk_style_context_set_invalid (context, FALSE);
3141
3142   info = priv->info;
3143   if (info->data)
3144     current = style_data_ref (info->data);
3145   else
3146     current = NULL;
3147
3148   /* Try to avoid invalidating if we can */
3149   if (current == NULL ||
3150       gtk_style_context_needs_full_revalidate (context, change))
3151     {
3152       if ((priv->relevant_changes & change) & ~GTK_STYLE_CONTEXT_CACHED_CHANGE)
3153         {
3154           gtk_style_context_clear_cache (context);
3155         }
3156       else
3157         {
3158           gtk_style_context_update_cache (context, parent_changes);
3159           style_info_set_data (info, NULL);
3160         }
3161
3162       if (current)
3163         {
3164           StyleData *data;
3165
3166           gtk_style_context_start_animations (context, current->store, timestamp);
3167           change &= ~GTK_CSS_CHANGE_ANIMATE;
3168
3169           data = style_data_lookup (context);
3170
3171           changes = _gtk_css_computed_values_get_difference (data->store, current->store);
3172
3173           style_data_unref (current);
3174         }
3175       else
3176         {
3177           changes = _gtk_bitmask_new ();
3178           changes = _gtk_bitmask_invert_range (changes, 0, _gtk_css_style_property_get_n_properties ());
3179         }
3180     }
3181   else
3182     {
3183       changes = _gtk_bitmask_copy (parent_changes);
3184       changes = _gtk_bitmask_intersect (changes, current->store->depends_on_parent);
3185       if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_COLOR))
3186         changes = _gtk_bitmask_union (changes, current->store->depends_on_color);
3187       if (_gtk_bitmask_get (changes, GTK_CSS_PROPERTY_FONT_SIZE))
3188         changes = _gtk_bitmask_union (changes, current->store->depends_on_font_size);
3189
3190       gtk_style_context_update_cache (context, parent_changes);
3191     }
3192
3193   if (change & GTK_CSS_CHANGE_ANIMATE &&
3194       gtk_style_context_is_animating (context))
3195     {
3196       GtkBitmask *animation_changes;
3197
3198       animation_changes = gtk_style_context_update_animations (context, timestamp);
3199       changes = _gtk_bitmask_union (changes, animation_changes);
3200       _gtk_bitmask_free (animation_changes);
3201     }
3202
3203   if (!_gtk_bitmask_is_empty (changes))
3204     gtk_style_context_do_invalidate (context);
3205
3206   change = _gtk_css_change_for_child (change);
3207   for (list = priv->children; list; list = list->next)
3208     {
3209       _gtk_style_context_validate (list->data, timestamp, change, changes);
3210     }
3211
3212   _gtk_bitmask_free (changes);
3213 }
3214
3215 void
3216 _gtk_style_context_queue_invalidate (GtkStyleContext *context,
3217                                      GtkCssChange     change)
3218 {
3219   GtkStyleContextPrivate *priv;
3220
3221   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3222   g_return_if_fail (change != 0);
3223
3224   priv = context->priv;
3225
3226   if (priv->widget != NULL)
3227     {
3228       priv->pending_changes |= change;
3229       gtk_style_context_set_invalid (context, TRUE);
3230     }
3231   else if (priv->widget_path == NULL)
3232     {
3233       gtk_style_context_invalidate (context);
3234     }
3235 }
3236
3237 /**
3238  * gtk_style_context_invalidate:
3239  * @context: a #GtkStyleContext.
3240  *
3241  * Invalidates @context style information, so it will be reconstructed
3242  * again.
3243  *
3244  * If you're using a #GtkStyleContext returned from
3245  * gtk_widget_get_style_context(), you do not need to
3246  * call this yourself.
3247  *
3248  * Since: 3.0
3249  **/
3250 void
3251 gtk_style_context_invalidate (GtkStyleContext *context)
3252 {
3253   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3254
3255   gtk_style_context_clear_cache (context);
3256   gtk_style_context_do_invalidate (context);
3257 }
3258
3259 /**
3260  * gtk_style_context_set_background:
3261  * @context: a #GtkStyleContext
3262  * @window: a #GdkWindow
3263  *
3264  * Sets the background of @window to the background pattern or
3265  * color specified in @context for its current state.
3266  *
3267  * Since: 3.0
3268  **/
3269 void
3270 gtk_style_context_set_background (GtkStyleContext *context,
3271                                   GdkWindow       *window)
3272 {
3273   GtkStateFlags state;
3274   cairo_pattern_t *pattern;
3275   GdkRGBA *color;
3276
3277   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3278   g_return_if_fail (GDK_IS_WINDOW (window));
3279
3280   state = gtk_style_context_get_state (context);
3281   gtk_style_context_get (context, state,
3282                          "background-image", &pattern,
3283                          NULL);
3284   if (pattern)
3285     {
3286       gdk_window_set_background_pattern (window, pattern);
3287       cairo_pattern_destroy (pattern);
3288       return;
3289     }
3290
3291   gtk_style_context_get (context, state,
3292                          "background-color", &color,
3293                          NULL);
3294   if (color)
3295     {
3296       gdk_window_set_background_rgba (window, color);
3297       gdk_rgba_free (color);
3298     }
3299 }
3300
3301 /**
3302  * gtk_style_context_get_color:
3303  * @context: a #GtkStyleContext
3304  * @state: state to retrieve the color for
3305  * @color: (out): return value for the foreground color
3306  *
3307  * Gets the foreground color for a given state.
3308  *
3309  * Since: 3.0
3310  **/
3311 void
3312 gtk_style_context_get_color (GtkStyleContext *context,
3313                              GtkStateFlags    state,
3314                              GdkRGBA         *color)
3315 {
3316   GdkRGBA *c;
3317
3318   g_return_if_fail (color != NULL);
3319   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3320
3321   gtk_style_context_get (context,
3322                          state,
3323                          "color", &c,
3324                          NULL);
3325
3326   *color = *c;
3327   gdk_rgba_free (c);
3328 }
3329
3330 /**
3331  * gtk_style_context_get_background_color:
3332  * @context: a #GtkStyleContext
3333  * @state: state to retrieve the color for
3334  * @color: (out): return value for the background color
3335  *
3336  * Gets the background color for a given state.
3337  *
3338  * Since: 3.0
3339  **/
3340 void
3341 gtk_style_context_get_background_color (GtkStyleContext *context,
3342                                         GtkStateFlags    state,
3343                                         GdkRGBA         *color)
3344 {
3345   GdkRGBA *c;
3346
3347   g_return_if_fail (color != NULL);
3348   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3349
3350   gtk_style_context_get (context,
3351                          state,
3352                          "background-color", &c,
3353                          NULL);
3354
3355   *color = *c;
3356   gdk_rgba_free (c);
3357 }
3358
3359 /**
3360  * gtk_style_context_get_border_color:
3361  * @context: a #GtkStyleContext
3362  * @state: state to retrieve the color for
3363  * @color: (out): return value for the border color
3364  *
3365  * Gets the border color for a given state.
3366  *
3367  * Since: 3.0
3368  **/
3369 void
3370 gtk_style_context_get_border_color (GtkStyleContext *context,
3371                                     GtkStateFlags    state,
3372                                     GdkRGBA         *color)
3373 {
3374   GdkRGBA *c;
3375
3376   g_return_if_fail (color != NULL);
3377   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3378
3379   gtk_style_context_get (context,
3380                          state,
3381                          "border-color", &c,
3382                          NULL);
3383
3384   *color = *c;
3385   gdk_rgba_free (c);
3386 }
3387
3388 /**
3389  * gtk_style_context_get_border:
3390  * @context: a #GtkStyleContext
3391  * @state: state to retrieve the border for
3392  * @border: (out): return value for the border settings
3393  *
3394  * Gets the border for a given state as a #GtkBorder.
3395  * See %GTK_STYLE_PROPERTY_BORDER_WIDTH.
3396  *
3397  * Since: 3.0
3398  **/
3399 void
3400 gtk_style_context_get_border (GtkStyleContext *context,
3401                               GtkStateFlags    state,
3402                               GtkBorder       *border)
3403 {
3404   int top, left, bottom, right;
3405
3406   g_return_if_fail (border != NULL);
3407   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3408
3409   gtk_style_context_get (context,
3410                          state,
3411                          "border-top-width", &top,
3412                          "border-left-width", &left,
3413                          "border-bottom-width", &bottom,
3414                          "border-right-width", &right,
3415                          NULL);
3416
3417   border->top = top;
3418   border->left = left;
3419   border->bottom = bottom;
3420   border->right = right;
3421 }
3422
3423 /**
3424  * gtk_style_context_get_padding:
3425  * @context: a #GtkStyleContext
3426  * @state: state to retrieve the padding for
3427  * @padding: (out): return value for the padding settings
3428  *
3429  * Gets the padding for a given state as a #GtkBorder.
3430  * See %GTK_STYLE_PROPERTY_PADDING.
3431  *
3432  * Since: 3.0
3433  **/
3434 void
3435 gtk_style_context_get_padding (GtkStyleContext *context,
3436                                GtkStateFlags    state,
3437                                GtkBorder       *padding)
3438 {
3439   int top, left, bottom, right;
3440
3441   g_return_if_fail (padding != NULL);
3442   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3443
3444   gtk_style_context_get (context,
3445                          state,
3446                          "padding-top", &top,
3447                          "padding-left", &left,
3448                          "padding-bottom", &bottom,
3449                          "padding-right", &right,
3450                          NULL);
3451
3452   padding->top = top;
3453   padding->left = left;
3454   padding->bottom = bottom;
3455   padding->right = right;
3456 }
3457
3458 /**
3459  * gtk_style_context_get_margin:
3460  * @context: a #GtkStyleContext
3461  * @state: state to retrieve the border for
3462  * @margin: (out): return value for the margin settings
3463  *
3464  * Gets the margin for a given state as a #GtkBorder.
3465  * See %GTK_STYLE_PROPERTY_MARGIN.
3466  *
3467  * Since: 3.0
3468  **/
3469 void
3470 gtk_style_context_get_margin (GtkStyleContext *context,
3471                               GtkStateFlags    state,
3472                               GtkBorder       *margin)
3473 {
3474   int top, left, bottom, right;
3475
3476   g_return_if_fail (margin != NULL);
3477   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3478
3479   gtk_style_context_get (context,
3480                          state,
3481                          "margin-top", &top,
3482                          "margin-left", &left,
3483                          "margin-bottom", &bottom,
3484                          "margin-right", &right,
3485                          NULL);
3486
3487   margin->top = top;
3488   margin->left = left;
3489   margin->bottom = bottom;
3490   margin->right = right;
3491 }
3492
3493 /**
3494  * gtk_style_context_get_font:
3495  * @context: a #GtkStyleContext
3496  * @state: state to retrieve the font for
3497  *
3498  * Returns the font description for a given state. The returned
3499  * object is const and will remain valid until the
3500  * #GtkStyleContext::changed signal happens.
3501  *
3502  * Returns: (transfer none): the #PangoFontDescription for the given
3503  *          state.  This object is owned by GTK+ and should not be
3504  *          freed.
3505  *
3506  * Since: 3.0
3507  **/
3508 const PangoFontDescription *
3509 gtk_style_context_get_font (GtkStyleContext *context,
3510                             GtkStateFlags    state)
3511 {
3512   GtkStyleContextPrivate *priv;
3513   StyleData *data;
3514   PangoFontDescription *description;
3515
3516   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
3517
3518   priv = context->priv;
3519   g_return_val_if_fail (priv->widget != NULL || priv->widget_path != NULL, NULL);
3520
3521   data = style_data_lookup_for_state (context, state);
3522
3523   /* Yuck, fonts are created on-demand but we don't return a ref.
3524    * Do bad things to achieve this requirement */
3525   description = g_object_get_data (G_OBJECT (data->store), "font-cache-for-get_font");
3526   if (description == NULL)
3527     {
3528       gtk_style_context_get (context, state, "font", &description, NULL);
3529       g_object_set_data_full (G_OBJECT (data->store),
3530                               "font-cache-for-get_font",
3531                               description,
3532                               (GDestroyNotify) pango_font_description_free);
3533     }
3534   return description;
3535 }
3536
3537 static void
3538 get_cursor_color (GtkStyleContext *context,
3539                   gboolean         primary,
3540                   GdkRGBA         *color)
3541 {
3542   GdkColor *style_color;
3543
3544   gtk_style_context_get_style (context,
3545                                primary ? "cursor-color" : "secondary-cursor-color",
3546                                &style_color,
3547                                NULL);
3548
3549   if (style_color)
3550     {
3551       color->red = style_color->red / 65535.0;
3552       color->green = style_color->green / 65535.0;
3553       color->blue = style_color->blue / 65535.0;
3554       color->alpha = 1;
3555
3556       gdk_color_free (style_color);
3557     }
3558   else
3559     {
3560       gtk_style_context_get_color (context, GTK_STATE_FLAG_NORMAL, color);
3561
3562       if (!primary)
3563       {
3564         GdkRGBA bg;
3565
3566         gtk_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &bg);
3567
3568         color->red = (color->red + bg.red) * 0.5;
3569         color->green = (color->green + bg.green) * 0.5;
3570         color->blue = (color->blue + bg.blue) * 0.5;
3571       }
3572     }
3573 }
3574
3575 void
3576 _gtk_style_context_get_cursor_color (GtkStyleContext *context,
3577                                      GdkRGBA         *primary_color,
3578                                      GdkRGBA         *secondary_color)
3579 {
3580   if (primary_color)
3581     get_cursor_color (context, TRUE, primary_color);
3582
3583   if (secondary_color)
3584     get_cursor_color (context, FALSE, secondary_color);
3585 }
3586
3587 /* Paint methods */
3588
3589 /**
3590  * gtk_render_check:
3591  * @context: a #GtkStyleContext
3592  * @cr: a #cairo_t
3593  * @x: X origin of the rectangle
3594  * @y: Y origin of the rectangle
3595  * @width: rectangle width
3596  * @height: rectangle height
3597  *
3598  * Renders a checkmark (as in a #GtkCheckButton).
3599  *
3600  * The %GTK_STATE_FLAG_ACTIVE state determines whether the check is
3601  * on or off, and %GTK_STATE_FLAG_INCONSISTENT determines whether it
3602  * should be marked as undefined.
3603  *
3604  * <example>
3605  * <title>Typical checkmark rendering</title>
3606  * <inlinegraphic fileref="checks.png" format="PNG"/>
3607  * </example>
3608  *
3609  * Since: 3.0
3610  **/
3611 void
3612 gtk_render_check (GtkStyleContext *context,
3613                   cairo_t         *cr,
3614                   gdouble          x,
3615                   gdouble          y,
3616                   gdouble          width,
3617                   gdouble          height)
3618 {
3619   GtkThemingEngineClass *engine_class;
3620   GtkThemingEngine *engine;
3621
3622   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3623   g_return_if_fail (cr != NULL);
3624
3625   if (width <= 0 || height <= 0)
3626     return;
3627
3628   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3629   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3630
3631   cairo_save (cr);
3632
3633
3634   _gtk_theming_engine_set_context (engine, context);
3635   engine_class->render_check (engine, cr,
3636                               x, y, width, height);
3637
3638   cairo_restore (cr);
3639 }
3640
3641 /**
3642  * gtk_render_option:
3643  * @context: a #GtkStyleContext
3644  * @cr: a #cairo_t
3645  * @x: X origin of the rectangle
3646  * @y: Y origin of the rectangle
3647  * @width: rectangle width
3648  * @height: rectangle height
3649  *
3650  * Renders an option mark (as in a #GtkRadioButton), the %GTK_STATE_FLAG_ACTIVE
3651  * state will determine whether the option is on or off, and
3652  * %GTK_STATE_FLAG_INCONSISTENT whether it should be marked as undefined.
3653  *
3654  * <example>
3655  * <title>Typical option mark rendering</title>
3656  * <inlinegraphic fileref="options.png" format="PNG"/>
3657  * </example>
3658  *
3659  * Since: 3.0
3660  **/
3661 void
3662 gtk_render_option (GtkStyleContext *context,
3663                    cairo_t         *cr,
3664                    gdouble          x,
3665                    gdouble          y,
3666                    gdouble          width,
3667                    gdouble          height)
3668 {
3669   GtkThemingEngineClass *engine_class;
3670   GtkThemingEngine *engine;
3671
3672   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3673   g_return_if_fail (cr != NULL);
3674
3675   if (width <= 0 || height <= 0)
3676     return;
3677
3678   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3679   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3680
3681   cairo_save (cr);
3682
3683   _gtk_theming_engine_set_context (engine, context);
3684   engine_class->render_option (engine, cr,
3685                                x, y, width, height);
3686
3687   cairo_restore (cr);
3688 }
3689
3690 /**
3691  * gtk_render_arrow:
3692  * @context: a #GtkStyleContext
3693  * @cr: a #cairo_t
3694  * @angle: arrow angle from 0 to 2 * %G_PI, being 0 the arrow pointing to the north
3695  * @x: X origin of the render area
3696  * @y: Y origin of the render area
3697  * @size: square side for render area
3698  *
3699  * Renders an arrow pointing to @angle.
3700  *
3701  * <example>
3702  * <title>Typical arrow rendering at 0, 1&solidus;2 &pi;, &pi; and 3&solidus;2 &pi;</title>
3703  * <inlinegraphic fileref="arrows.png" format="PNG"/>
3704  * </example>
3705  *
3706  * Since: 3.0
3707  **/
3708 void
3709 gtk_render_arrow (GtkStyleContext *context,
3710                   cairo_t         *cr,
3711                   gdouble          angle,
3712                   gdouble          x,
3713                   gdouble          y,
3714                   gdouble          size)
3715 {
3716   GtkThemingEngineClass *engine_class;
3717   GtkThemingEngine *engine;
3718
3719   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3720   g_return_if_fail (cr != NULL);
3721
3722   if (size <= 0)
3723     return;
3724
3725   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3726   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3727
3728   cairo_save (cr);
3729
3730   gtk_style_context_save (context);
3731   gtk_style_context_add_class (context, GTK_STYLE_CLASS_ARROW);
3732
3733   _gtk_theming_engine_set_context (engine, context);
3734   engine_class->render_arrow (engine, cr,
3735                               angle, x, y, size);
3736
3737   gtk_style_context_restore (context);
3738   cairo_restore (cr);
3739 }
3740
3741 /**
3742  * gtk_render_background:
3743  * @context: a #GtkStyleContext
3744  * @cr: a #cairo_t
3745  * @x: X origin of the rectangle
3746  * @y: Y origin of the rectangle
3747  * @width: rectangle width
3748  * @height: rectangle height
3749  *
3750  * Renders the background of an element.
3751  *
3752  * <example>
3753  * <title>Typical background rendering, showing the effect of
3754  * <parameter>background-image</parameter>,
3755  * <parameter>border-width</parameter> and
3756  * <parameter>border-radius</parameter></title>
3757  * <inlinegraphic fileref="background.png" format="PNG"/>
3758  * </example>
3759  *
3760  * Since: 3.0.
3761  **/
3762 void
3763 gtk_render_background (GtkStyleContext *context,
3764                        cairo_t         *cr,
3765                        gdouble          x,
3766                        gdouble          y,
3767                        gdouble          width,
3768                        gdouble          height)
3769 {
3770   GtkThemingEngineClass *engine_class;
3771   GtkThemingEngine *engine;
3772
3773   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3774   g_return_if_fail (cr != NULL);
3775
3776   if (width <= 0 || height <= 0)
3777     return;
3778
3779   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3780   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3781
3782   cairo_save (cr);
3783
3784   _gtk_theming_engine_set_context (engine, context);
3785   engine_class->render_background (engine, cr, x, y, width, height);
3786
3787   cairo_restore (cr);
3788 }
3789
3790 /**
3791  * gtk_render_frame:
3792  * @context: a #GtkStyleContext
3793  * @cr: a #cairo_t
3794  * @x: X origin of the rectangle
3795  * @y: Y origin of the rectangle
3796  * @width: rectangle width
3797  * @height: rectangle height
3798  *
3799  * Renders a frame around the rectangle defined by @x, @y, @width, @height.
3800  *
3801  * <example>
3802  * <title>Examples of frame rendering, showing the effect of
3803  * <parameter>border-image</parameter>,
3804  * <parameter>border-color</parameter>,
3805  * <parameter>border-width</parameter>,
3806  * <parameter>border-radius</parameter> and
3807  * junctions</title>
3808  * <inlinegraphic fileref="frames.png" format="PNG"/>
3809  * </example>
3810  *
3811  * Since: 3.0
3812  **/
3813 void
3814 gtk_render_frame (GtkStyleContext *context,
3815                   cairo_t         *cr,
3816                   gdouble          x,
3817                   gdouble          y,
3818                   gdouble          width,
3819                   gdouble          height)
3820 {
3821   GtkThemingEngineClass *engine_class;
3822   GtkThemingEngine *engine;
3823
3824   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3825   g_return_if_fail (cr != NULL);
3826
3827   if (width <= 0 || height <= 0)
3828     return;
3829
3830   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3831   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3832
3833   cairo_save (cr);
3834
3835   _gtk_theming_engine_set_context (engine, context);
3836   engine_class->render_frame (engine, cr, x, y, width, height);
3837
3838   cairo_restore (cr);
3839 }
3840
3841 /**
3842  * gtk_render_expander:
3843  * @context: a #GtkStyleContext
3844  * @cr: a #cairo_t
3845  * @x: X origin of the rectangle
3846  * @y: Y origin of the rectangle
3847  * @width: rectangle width
3848  * @height: rectangle height
3849  *
3850  * Renders an expander (as used in #GtkTreeView and #GtkExpander) in the area
3851  * defined by @x, @y, @width, @height. The state %GTK_STATE_FLAG_ACTIVE
3852  * determines whether the expander is collapsed or expanded.
3853  *
3854  * <example>
3855  * <title>Typical expander rendering</title>
3856  * <inlinegraphic fileref="expanders.png" format="PNG"/>
3857  * </example>
3858  *
3859  * Since: 3.0
3860  **/
3861 void
3862 gtk_render_expander (GtkStyleContext *context,
3863                      cairo_t         *cr,
3864                      gdouble          x,
3865                      gdouble          y,
3866                      gdouble          width,
3867                      gdouble          height)
3868 {
3869   GtkThemingEngineClass *engine_class;
3870   GtkThemingEngine *engine;
3871
3872   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3873   g_return_if_fail (cr != NULL);
3874
3875   if (width <= 0 || height <= 0)
3876     return;
3877
3878   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3879   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3880
3881   cairo_save (cr);
3882
3883   _gtk_theming_engine_set_context (engine, context);
3884   engine_class->render_expander (engine, cr, x, y, width, height);
3885
3886   cairo_restore (cr);
3887 }
3888
3889 /**
3890  * gtk_render_focus:
3891  * @context: a #GtkStyleContext
3892  * @cr: a #cairo_t
3893  * @x: X origin of the rectangle
3894  * @y: Y origin of the rectangle
3895  * @width: rectangle width
3896  * @height: rectangle height
3897  *
3898  * Renders a focus indicator on the rectangle determined by @x, @y, @width, @height.
3899  * <example>
3900  * <title>Typical focus rendering</title>
3901  * <inlinegraphic fileref="focus.png" format="PNG"/>
3902  * </example>
3903  *
3904  * Since: 3.0
3905  **/
3906 void
3907 gtk_render_focus (GtkStyleContext *context,
3908                   cairo_t         *cr,
3909                   gdouble          x,
3910                   gdouble          y,
3911                   gdouble          width,
3912                   gdouble          height)
3913 {
3914   GtkThemingEngineClass *engine_class;
3915   GtkThemingEngine *engine;
3916
3917   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3918   g_return_if_fail (cr != NULL);
3919
3920   if (width <= 0 || height <= 0)
3921     return;
3922
3923   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3924   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3925
3926   cairo_save (cr);
3927
3928   _gtk_theming_engine_set_context (engine, context);
3929   engine_class->render_focus (engine, cr, x, y, width, height);
3930
3931   cairo_restore (cr);
3932 }
3933
3934 /**
3935  * gtk_render_layout:
3936  * @context: a #GtkStyleContext
3937  * @cr: a #cairo_t
3938  * @x: X origin
3939  * @y: Y origin
3940  * @layout: the #PangoLayout to render
3941  *
3942  * Renders @layout on the coordinates @x, @y
3943  *
3944  * Since: 3.0
3945  **/
3946 void
3947 gtk_render_layout (GtkStyleContext *context,
3948                    cairo_t         *cr,
3949                    gdouble          x,
3950                    gdouble          y,
3951                    PangoLayout     *layout)
3952 {
3953   GtkThemingEngineClass *engine_class;
3954   GtkThemingEngine *engine;
3955   PangoRectangle extents;
3956
3957   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3958   g_return_if_fail (PANGO_IS_LAYOUT (layout));
3959   g_return_if_fail (cr != NULL);
3960
3961   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
3962   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
3963
3964   cairo_save (cr);
3965
3966   pango_layout_get_extents (layout, &extents, NULL);
3967
3968   _gtk_theming_engine_set_context (engine, context);
3969   engine_class->render_layout (engine, cr, x, y, layout);
3970
3971   cairo_restore (cr);
3972 }
3973
3974 /**
3975  * gtk_render_line:
3976  * @context: a #GtkStyleContext
3977  * @cr: a #cairo_t
3978  * @x0: X coordinate for the origin of the line
3979  * @y0: Y coordinate for the origin of the line
3980  * @x1: X coordinate for the end of the line
3981  * @y1: Y coordinate for the end of the line
3982  *
3983  * Renders a line from (x0, y0) to (x1, y1).
3984  *
3985  * Since: 3.0
3986  **/
3987 void
3988 gtk_render_line (GtkStyleContext *context,
3989                  cairo_t         *cr,
3990                  gdouble          x0,
3991                  gdouble          y0,
3992                  gdouble          x1,
3993                  gdouble          y1)
3994 {
3995   GtkThemingEngineClass *engine_class;
3996   GtkThemingEngine *engine;
3997
3998   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
3999   g_return_if_fail (cr != NULL);
4000
4001   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4002   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4003
4004   cairo_save (cr);
4005
4006   _gtk_theming_engine_set_context (engine, context);
4007   engine_class->render_line (engine, cr, x0, y0, x1, y1);
4008
4009   cairo_restore (cr);
4010 }
4011
4012 /**
4013  * gtk_render_slider:
4014  * @context: a #GtkStyleContext
4015  * @cr: a #cairo_t
4016  * @x: X origin of the rectangle
4017  * @y: Y origin of the rectangle
4018  * @width: rectangle width
4019  * @height: rectangle height
4020  * @orientation: orientation of the slider
4021  *
4022  * Renders a slider (as in #GtkScale) in the rectangle defined by @x, @y,
4023  * @width, @height. @orientation defines whether the slider is vertical
4024  * or horizontal.
4025  *
4026  * <example>
4027  * <title>Typical slider rendering</title>
4028  * <inlinegraphic fileref="sliders.png" format="PNG"/>
4029  * </example>
4030  *
4031  * Since: 3.0
4032  **/
4033 void
4034 gtk_render_slider (GtkStyleContext *context,
4035                    cairo_t         *cr,
4036                    gdouble          x,
4037                    gdouble          y,
4038                    gdouble          width,
4039                    gdouble          height,
4040                    GtkOrientation   orientation)
4041 {
4042   GtkThemingEngineClass *engine_class;
4043   GtkThemingEngine *engine;
4044
4045   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4046   g_return_if_fail (cr != NULL);
4047
4048   if (width <= 0 || height <= 0)
4049     return;
4050
4051   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4052   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4053
4054   cairo_save (cr);
4055
4056   _gtk_theming_engine_set_context (engine, context);
4057   engine_class->render_slider (engine, cr, x, y, width, height, orientation);
4058
4059   cairo_restore (cr);
4060 }
4061
4062 /**
4063  * gtk_render_frame_gap:
4064  * @context: a #GtkStyleContext
4065  * @cr: a #cairo_t
4066  * @x: X origin of the rectangle
4067  * @y: Y origin of the rectangle
4068  * @width: rectangle width
4069  * @height: rectangle height
4070  * @gap_side: side where the gap is
4071  * @xy0_gap: initial coordinate (X or Y depending on @gap_side) for the gap
4072  * @xy1_gap: end coordinate (X or Y depending on @gap_side) for the gap
4073  *
4074  * Renders a frame around the rectangle defined by (@x, @y, @width, @height),
4075  * leaving a gap on one side. @xy0_gap and @xy1_gap will mean X coordinates
4076  * for %GTK_POS_TOP and %GTK_POS_BOTTOM gap sides, and Y coordinates for
4077  * %GTK_POS_LEFT and %GTK_POS_RIGHT.
4078  *
4079  * <example>
4080  * <title>Typical rendering of a frame with a gap</title>
4081  * <inlinegraphic fileref="frame-gap.png" format="PNG"/>
4082  * </example>
4083  *
4084  * Since: 3.0
4085  **/
4086 void
4087 gtk_render_frame_gap (GtkStyleContext *context,
4088                       cairo_t         *cr,
4089                       gdouble          x,
4090                       gdouble          y,
4091                       gdouble          width,
4092                       gdouble          height,
4093                       GtkPositionType  gap_side,
4094                       gdouble          xy0_gap,
4095                       gdouble          xy1_gap)
4096 {
4097   GtkThemingEngineClass *engine_class;
4098   GtkThemingEngine *engine;
4099
4100   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4101   g_return_if_fail (cr != NULL);
4102   g_return_if_fail (xy0_gap <= xy1_gap);
4103   g_return_if_fail (xy0_gap >= 0);
4104
4105   if (width <= 0 || height <= 0)
4106     return;
4107
4108   if (gap_side == GTK_POS_LEFT ||
4109       gap_side == GTK_POS_RIGHT)
4110     g_return_if_fail (xy1_gap <= height);
4111   else
4112     g_return_if_fail (xy1_gap <= width);
4113
4114   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4115   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4116
4117   cairo_save (cr);
4118
4119   _gtk_theming_engine_set_context (engine, context);
4120   engine_class->render_frame_gap (engine, cr,
4121                                   x, y, width, height, gap_side,
4122                                   xy0_gap, xy1_gap);
4123
4124   cairo_restore (cr);
4125 }
4126
4127 /**
4128  * gtk_render_extension:
4129  * @context: a #GtkStyleContext
4130  * @cr: a #cairo_t
4131  * @x: X origin of the rectangle
4132  * @y: Y origin of the rectangle
4133  * @width: rectangle width
4134  * @height: rectangle height
4135  * @gap_side: side where the gap is
4136  *
4137  * Renders a extension (as in a #GtkNotebook tab) in the rectangle
4138  * defined by @x, @y, @width, @height. The side where the extension
4139  * connects to is defined by @gap_side.
4140  *
4141  * <example>
4142  * <title>Typical extension rendering</title>
4143  * <inlinegraphic fileref="extensions.png" format="PNG"/>
4144  * </example>
4145  *
4146  * Since: 3.0
4147  **/
4148 void
4149 gtk_render_extension (GtkStyleContext *context,
4150                       cairo_t         *cr,
4151                       gdouble          x,
4152                       gdouble          y,
4153                       gdouble          width,
4154                       gdouble          height,
4155                       GtkPositionType  gap_side)
4156 {
4157   GtkThemingEngineClass *engine_class;
4158   GtkThemingEngine *engine;
4159
4160   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4161   g_return_if_fail (cr != NULL);
4162
4163   if (width <= 0 || height <= 0)
4164     return;
4165
4166   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4167   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4168
4169   cairo_save (cr);
4170
4171   _gtk_theming_engine_set_context (engine, context);
4172   engine_class->render_extension (engine, cr, x, y, width, height, gap_side);
4173
4174   cairo_restore (cr);
4175 }
4176
4177 /**
4178  * gtk_render_handle:
4179  * @context: a #GtkStyleContext
4180  * @cr: a #cairo_t
4181  * @x: X origin of the rectangle
4182  * @y: Y origin of the rectangle
4183  * @width: rectangle width
4184  * @height: rectangle height
4185  *
4186  * Renders a handle (as in #GtkHandleBox, #GtkPaned and
4187  * #GtkWindow<!-- -->'s resize grip), in the rectangle
4188  * determined by @x, @y, @width, @height.
4189  *
4190  * <example>
4191  * <title>Handles rendered for the paned and grip classes</title>
4192  * <inlinegraphic fileref="handles.png" format="PNG"/>
4193  * </example>
4194  *
4195  * Since: 3.0
4196  **/
4197 void
4198 gtk_render_handle (GtkStyleContext *context,
4199                    cairo_t         *cr,
4200                    gdouble          x,
4201                    gdouble          y,
4202                    gdouble          width,
4203                    gdouble          height)
4204 {
4205   GtkThemingEngineClass *engine_class;
4206   GtkThemingEngine *engine;
4207
4208   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4209   g_return_if_fail (cr != NULL);
4210
4211   if (width <= 0 || height <= 0)
4212     return;
4213
4214   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4215   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4216
4217   cairo_save (cr);
4218
4219   _gtk_theming_engine_set_context (engine, context);
4220   engine_class->render_handle (engine, cr, x, y, width, height);
4221
4222   cairo_restore (cr);
4223 }
4224
4225 /**
4226  * gtk_render_activity:
4227  * @context: a #GtkStyleContext
4228  * @cr: a #cairo_t
4229  * @x: X origin of the rectangle
4230  * @y: Y origin of the rectangle
4231  * @width: rectangle width
4232  * @height: rectangle height
4233  *
4234  * Renders an activity area (Such as in #GtkSpinner or the
4235  * fill line in #GtkRange), the state %GTK_STATE_FLAG_ACTIVE
4236  * determines whether there is activity going on.
4237  *
4238  * Since: 3.0
4239  **/
4240 void
4241 gtk_render_activity (GtkStyleContext *context,
4242                      cairo_t         *cr,
4243                      gdouble          x,
4244                      gdouble          y,
4245                      gdouble          width,
4246                      gdouble          height)
4247 {
4248   GtkThemingEngineClass *engine_class;
4249   GtkThemingEngine *engine;
4250
4251   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4252   g_return_if_fail (cr != NULL);
4253
4254   if (width <= 0 || height <= 0)
4255     return;
4256
4257   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4258   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4259
4260   cairo_save (cr);
4261
4262   _gtk_theming_engine_set_context (engine, context);
4263   engine_class->render_activity (engine, cr, x, y, width, height);
4264
4265   cairo_restore (cr);
4266 }
4267
4268 /**
4269  * gtk_render_icon_pixbuf:
4270  * @context: a #GtkStyleContext
4271  * @source: the #GtkIconSource specifying the icon to render
4272  * @size: (type int): the size to render the icon at. A size of (GtkIconSize) -1
4273  *        means render at the size of the source and don't scale.
4274  *
4275  * Renders the icon specified by @source at the given @size, returning the result
4276  * in a pixbuf.
4277  *
4278  * Returns: (transfer full): a newly-created #GdkPixbuf containing the rendered icon
4279  *
4280  * Since: 3.0
4281  **/
4282 GdkPixbuf *
4283 gtk_render_icon_pixbuf (GtkStyleContext     *context,
4284                         const GtkIconSource *source,
4285                         GtkIconSize          size)
4286 {
4287   GtkThemingEngineClass *engine_class;
4288   GtkThemingEngine *engine;
4289
4290   g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
4291   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
4292   g_return_val_if_fail (source != NULL, NULL);
4293
4294   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4295   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4296
4297   _gtk_theming_engine_set_context (engine, context);
4298   return engine_class->render_icon_pixbuf (engine, source, size);
4299 }
4300
4301 /**
4302  * gtk_render_icon:
4303  * @context: a #GtkStyleContext
4304  * @cr: a #cairo_t
4305  * @pixbuf: a #GdkPixbuf containing the icon to draw
4306  * @x: X position for the @pixbuf
4307  * @y: Y position for the @pixbuf
4308  *
4309  * Renders the icon in @pixbuf at the specified @x and @y coordinates.
4310  *
4311  * Since: 3.2
4312  **/
4313 void
4314 gtk_render_icon (GtkStyleContext *context,
4315                  cairo_t         *cr,
4316                  GdkPixbuf       *pixbuf,
4317                  gdouble          x,
4318                  gdouble          y)
4319 {
4320   GtkThemingEngineClass *engine_class;
4321   GtkThemingEngine *engine;
4322
4323   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4324   g_return_if_fail (cr != NULL);
4325
4326   engine = _gtk_css_engine_value_get_engine (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_ENGINE));
4327   engine_class = GTK_THEMING_ENGINE_GET_CLASS (engine);
4328
4329   cairo_save (cr);
4330
4331   _gtk_theming_engine_set_context (engine, context);
4332   engine_class->render_icon (engine, cr, pixbuf, x, y);
4333
4334   cairo_restore (cr);
4335 }
4336
4337 static void
4338 draw_insertion_cursor (GtkStyleContext *context,
4339                        cairo_t         *cr,
4340                        gdouble          x,
4341                        gdouble          y,
4342                        gdouble          height,
4343                        gboolean         is_primary,
4344                        PangoDirection   direction,
4345                        gboolean         draw_arrow)
4346
4347 {
4348   GdkRGBA primary_color;
4349   GdkRGBA secondary_color;
4350   gfloat cursor_aspect_ratio;
4351   gint stem_width;
4352   gint offset;
4353
4354   cairo_save (cr);
4355
4356   _gtk_style_context_get_cursor_color (context, &primary_color, &secondary_color);
4357   gdk_cairo_set_source_rgba (cr, is_primary ? &primary_color : &secondary_color);
4358
4359   /* When changing the shape or size of the cursor here,
4360    * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4361    */
4362
4363   gtk_style_context_get_style (context,
4364                                "cursor-aspect-ratio", &cursor_aspect_ratio,
4365                                NULL);
4366
4367   stem_width = height * cursor_aspect_ratio + 1;
4368
4369   /* put (stem_width % 2) on the proper side of the cursor */
4370   if (direction == PANGO_DIRECTION_LTR)
4371     offset = stem_width / 2;
4372   else
4373     offset = stem_width - stem_width / 2;
4374
4375   cairo_rectangle (cr, x - offset, y, stem_width, height);
4376   cairo_fill (cr);
4377
4378   if (draw_arrow)
4379     {
4380       gint arrow_width;
4381       gint ax, ay;
4382
4383       arrow_width = stem_width + 1;
4384
4385       if (direction == PANGO_DIRECTION_RTL)
4386         {
4387           ax = x - offset - 1;
4388           ay = y + height - arrow_width * 2 - arrow_width + 1;
4389
4390           cairo_move_to (cr, ax, ay + 1);
4391           cairo_line_to (cr, ax - arrow_width, ay + arrow_width);
4392           cairo_line_to (cr, ax, ay + 2 * arrow_width);
4393           cairo_fill (cr);
4394         }
4395       else if (direction == PANGO_DIRECTION_LTR)
4396         {
4397           ax = x + stem_width - offset;
4398           ay = y + height - arrow_width * 2 - arrow_width + 1;
4399
4400           cairo_move_to (cr, ax, ay + 1);
4401           cairo_line_to (cr, ax + arrow_width, ay + arrow_width);
4402           cairo_line_to (cr, ax, ay + 2 * arrow_width);
4403           cairo_fill (cr);
4404         }
4405       else
4406         g_assert_not_reached();
4407     }
4408
4409   cairo_restore (cr);
4410 }
4411
4412 /**
4413  * gtk_render_insertion_cursor:
4414  * @context: a #GtkStyleContext
4415  * @cr: a #cairo_t
4416  * @x: X origin
4417  * @y: Y origin
4418  * @layout: the #PangoLayout of the text
4419  * @index: the index in the #PangoLayout
4420  * @direction: the #PangoDirection of the text
4421  *
4422  * Draws a text caret on @cr at the specified index of @layout.
4423  *
4424  * Since: 3.4
4425  **/
4426 void
4427 gtk_render_insertion_cursor (GtkStyleContext *context,
4428                              cairo_t         *cr,
4429                              gdouble          x,
4430                              gdouble          y,
4431                              PangoLayout     *layout,
4432                              int              index,
4433                              PangoDirection   direction)
4434 {
4435   GtkStyleContextPrivate *priv;
4436   gboolean split_cursor;
4437   PangoRectangle strong_pos, weak_pos;
4438   PangoRectangle *cursor1, *cursor2;
4439   PangoDirection keymap_direction;
4440   PangoDirection direction2;
4441
4442   g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
4443   g_return_if_fail (cr != NULL);
4444   g_return_if_fail (PANGO_IS_LAYOUT (layout));
4445   g_return_if_fail (index >= 0);
4446
4447   priv = context->priv;
4448
4449   g_object_get (gtk_settings_get_for_screen (priv->screen),
4450                 "gtk-split-cursor", &split_cursor,
4451                 NULL);
4452
4453   keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gdk_screen_get_display (priv->screen)));
4454
4455   pango_layout_get_cursor_pos (layout, index, &strong_pos, &weak_pos);
4456
4457   direction2 = PANGO_DIRECTION_NEUTRAL;
4458
4459   if (split_cursor)
4460     {
4461       cursor1 = &strong_pos;
4462
4463       if (strong_pos.x != weak_pos.x || strong_pos.y != weak_pos.y)
4464         {
4465           direction2 = (direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4466           cursor2 = &weak_pos;
4467         }
4468     }
4469   else
4470     {
4471       if (keymap_direction == direction)
4472         cursor1 = &strong_pos;
4473       else
4474         cursor1 = &weak_pos;
4475     }
4476
4477   draw_insertion_cursor (context,
4478                          cr,
4479                          x + PANGO_PIXELS (cursor1->x),
4480                          y + PANGO_PIXELS (cursor1->y),
4481                          PANGO_PIXELS (cursor1->height),
4482                          TRUE,
4483                          direction,
4484                          direction2 != PANGO_DIRECTION_NEUTRAL);
4485
4486   if (direction2 != PANGO_DIRECTION_NEUTRAL)
4487     {
4488       draw_insertion_cursor (context,
4489                              cr,
4490                              x + PANGO_PIXELS (cursor2->x),
4491                              y + PANGO_PIXELS (cursor2->y),
4492                              PANGO_PIXELS (cursor2->height),
4493                              FALSE,
4494                              direction2,
4495                              TRUE);
4496     }
4497 }
4498
4499 /**
4500  * gtk_draw_insertion_cursor:
4501  * @widget:  a #GtkWidget
4502  * @cr: cairo context to draw to
4503  * @location: location where to draw the cursor (@location->width is ignored)
4504  * @is_primary: if the cursor should be the primary cursor color.
4505  * @direction: whether the cursor is left-to-right or
4506  *             right-to-left. Should never be #GTK_TEXT_DIR_NONE
4507  * @draw_arrow: %TRUE to draw a directional arrow on the
4508  *        cursor. Should be %FALSE unless the cursor is split.
4509  *
4510  * Draws a text caret on @cr at @location. This is not a style function
4511  * but merely a convenience function for drawing the standard cursor shape.
4512  *
4513  * Since: 3.0
4514  * Deprecated: 3.4: Use gtk_render_insertion_cursor() instead.
4515  */
4516 void
4517 gtk_draw_insertion_cursor (GtkWidget          *widget,
4518                            cairo_t            *cr,
4519                            const GdkRectangle *location,
4520                            gboolean            is_primary,
4521                            GtkTextDirection    direction,
4522                            gboolean            draw_arrow)
4523 {
4524   GtkStyleContext *context;
4525
4526   g_return_if_fail (GTK_IS_WIDGET (widget));
4527   g_return_if_fail (cr != NULL);
4528   g_return_if_fail (location != NULL);
4529   g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4530
4531   context = gtk_widget_get_style_context (widget);
4532
4533   draw_insertion_cursor (context, cr,
4534                          location->x, location->y, location->height,
4535                          is_primary,
4536                          (direction == GTK_TEXT_DIR_RTL) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR,
4537                          draw_arrow);
4538 }
4539
4540 static AtkAttributeSet *
4541 add_attribute (AtkAttributeSet  *attributes,
4542                AtkTextAttribute  attr,
4543                const gchar      *value)
4544 {
4545   AtkAttribute *at;
4546
4547   at = g_new (AtkAttribute, 1);
4548   at->name = g_strdup (atk_text_attribute_get_name (attr));
4549   at->value = g_strdup (value);
4550
4551   return g_slist_prepend (attributes, at);
4552 }
4553
4554 /*
4555  * _gtk_style_context_get_attributes:
4556  * @attributes: a #AtkAttributeSet to add attributes to
4557  * @context: the #GtkStyleContext to get attributes from
4558  * @flags: the state to use with @context
4559  *
4560  * Adds the foreground and background color from @context to
4561  * @attributes, after translating them to ATK attributes.
4562  *
4563  * This is a convenience function that can be used in
4564  * implementing the #AtkText interface in widgets.
4565  *
4566  * Returns: the modified #AtkAttributeSet
4567  */
4568 AtkAttributeSet *
4569 _gtk_style_context_get_attributes (AtkAttributeSet *attributes,
4570                                    GtkStyleContext *context,
4571                                    GtkStateFlags    flags)
4572 {
4573   GdkRGBA color;
4574   gchar *value;
4575
4576   gtk_style_context_get_background_color (context, flags, &color);
4577   value = g_strdup_printf ("%u,%u,%u",
4578                            (guint) ceil (color.red * 65536 - color.red),
4579                            (guint) ceil (color.green * 65536 - color.green),
4580                            (guint) ceil (color.blue * 65536 - color.blue));
4581   attributes = add_attribute (attributes, ATK_TEXT_ATTR_BG_COLOR, value);
4582   g_free (value);
4583
4584   gtk_style_context_get_color (context, flags, &color);
4585   value = g_strdup_printf ("%u,%u,%u",
4586                            (guint) ceil (color.red * 65536 - color.red),
4587                            (guint) ceil (color.green * 65536 - color.green),
4588                            (guint) ceil (color.blue * 65536 - color.blue));
4589   attributes = add_attribute (attributes, ATK_TEXT_ATTR_FG_COLOR, value);
4590   g_free (value);
4591
4592   return attributes;
4593 }