]> Pileus Git - ~andy/gtk/blob - gtk/gtkwidget.c
Add a getter for the accessible type of a widget class
[~andy/gtk] / gtk / gtkwidget.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25  */
26
27 #include "config.h"
28
29 #include <stdarg.h>
30 #include <string.h>
31 #include <locale.h>
32
33 #include <gobject/gvaluecollector.h>
34 #include <gobject/gobjectnotifyqueue.c>
35 #include <cairo-gobject.h>
36
37 #include "gtkcontainer.h"
38 #include "gtkaccelmap.h"
39 #include "gtkclipboard.h"
40 #include "gtkiconfactory.h"
41 #include "gtkintl.h"
42 #include "gtkmainprivate.h"
43 #include "gtkmarshalers.h"
44 #include "gtkselectionprivate.h"
45 #include "gtksettingsprivate.h"
46 #include "gtksizegroup-private.h"
47 #include "gtkwidget.h"
48 #include "gtkwidgetprivate.h"
49 #include "gtkwindowprivate.h"
50 #include "gtkcontainerprivate.h"
51 #include "gtkbindings.h"
52 #include "gtkprivate.h"
53 #include "gtkaccessible.h"
54 #include "gtktooltip.h"
55 #include "gtkinvisible.h"
56 #include "gtkbuildable.h"
57 #include "gtkbuilderprivate.h"
58 #include "gtksizerequest.h"
59 #include "gtkstylecontextprivate.h"
60 #include "gtksymboliccolor.h"
61 #include "gtkcssprovider.h"
62 #include "gtkanimationdescription.h"
63 #include "gtkmodifierstyle.h"
64 #include "gtkversion.h"
65 #include "gtkdebug.h"
66 #include "gtkplug.h"
67 #include "gtktypebuiltins.h"
68 #include "a11y/gtkwidgetaccessible.h"
69
70 /**
71  * SECTION:gtkwidget
72  * @Short_description: Base class for all widgets
73  * @Title: GtkWidget
74  *
75  * GtkWidget is the base class all widgets in GTK+ derive from. It manages the
76  * widget lifecycle, states and style.
77  *
78  * <refsect2 id="geometry-management">
79  * <title>Height-for-width Geometry Management</title>
80  * <para>
81  * GTK+ uses a height-for-width (and width-for-height) geometry management
82  * system. Height-for-width means that a widget can change how much
83  * vertical space it needs, depending on the amount of horizontal space
84  * that it is given (and similar for width-for-height). The most common
85  * example is a label that reflows to fill up the available width, wraps
86  * to fewer lines, and therefore needs less height.
87  *
88  * Height-for-width geometry management is implemented in GTK+ by way
89  * of five virtual methods:
90  * <itemizedlist>
91  *   <listitem>#GtkWidgetClass.get_request_mode()</listitem>
92  *   <listitem>#GtkWidgetClass.get_preferred_width()</listitem>
93  *   <listitem>#GtkWidgetClass.get_preferred_height()</listitem>
94  *   <listitem>#GtkWidgetClass.get_preferred_height_for_width()</listitem>
95  *   <listitem>#GtkWidgetClass.get_preferred_width_for_height()</listitem>
96  * </itemizedlist>
97  *
98  * There are some important things to keep in mind when implementing
99  * height-for-width and when using it in container implementations.
100  *
101  * The geometry management system will query a widget hierarchy in
102  * only one orientation at a time. When widgets are initially queried
103  * for their minimum sizes it is generally done in two initial passes
104  * in the #GtkSizeRequestMode chosen by the toplevel.
105  *
106  * For example, when queried in the normal
107  * %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH mode:
108  * First, the default minimum and natural width for each widget
109  * in the interface will be computed using gtk_widget_get_preferred_width().
110  * Because the preferred widths for each container depend on the preferred
111  * widths of their children, this information propagates up the hierarchy,
112  * and finally a minimum and natural width is determined for the entire
113  * toplevel. Next, the toplevel will use the minimum width to query for the
114  * minimum height contextual to that width using
115  * gtk_widget_get_preferred_height_for_width(), which will also be a highly
116  * recursive operation. The minimum height for the minimum width is normally
117  * used to set the minimum size constraint on the toplevel
118  * (unless gtk_window_set_geometry_hints() is explicitly used instead).
119  *
120  * After the toplevel window has initially requested its size in both
121  * dimensions it can go on to allocate itself a reasonable size (or a size
122  * previously specified with gtk_window_set_default_size()). During the
123  * recursive allocation process it's important to note that request cycles
124  * will be recursively executed while container widgets allocate their children.
125  * Each container widget, once allocated a size, will go on to first share the
126  * space in one orientation among its children and then request each child's
127  * height for its target allocated width or its width for allocated height,
128  * depending. In this way a #GtkWidget will typically be requested its size
129  * a number of times before actually being allocated a size. The size a
130  * widget is finally allocated can of course differ from the size it has
131  * requested. For this reason, #GtkWidget caches a  small number of results
132  * to avoid re-querying for the same sizes in one allocation cycle.
133  *
134  * See <link linkend="container-geometry-management">GtkContainer's
135  * geometry management section</link>
136  * to learn more about how height-for-width allocations are performed
137  * by container widgets.
138  *
139  * If a widget does move content around to intelligently use up the
140  * allocated size then it must support the request in both
141  * #GtkSizeRequestModes even if the widget in question only
142  * trades sizes in a single orientation.
143  *
144  * For instance, a #GtkLabel that does height-for-width word wrapping
145  * will not expect to have #GtkWidgetClass.get_preferred_height() called
146  * because that call is specific to a width-for-height request. In this
147  * case the label must return the height required for its own minimum
148  * possible width. By following this rule any widget that handles
149  * height-for-width or width-for-height requests will always be allocated
150  * at least enough space to fit its own content.
151  *
152  * Here are some examples of how a %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH widget
153  * generally deals with width-for-height requests, for #GtkWidgetClass.get_preferred_height()
154  * it will do:
155  * <programlisting><![CDATA[
156  * static void
157  * foo_widget_get_preferred_height (GtkWidget *widget, gint *min_height, gint *nat_height)
158  * {
159  *    if (i_am_in_height_for_width_mode)
160  *      {
161  *        gint min_width;
162  *
163  *        GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, NULL);
164  *        GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget, min_width,
165  *                                                                      min_height, nat_height);
166  *      }
167  *    else
168  *      {
169  *         ... some widgets do both. For instance, if a GtkLabel is rotated to 90 degrees
170  *         it will return the minimum and natural height for the rotated label here.
171  *      }
172  * }
173  * ]]></programlisting>
174  *
175  * And in #GtkWidgetClass.get_preferred_width_for_height() it will simply return
176  * the minimum and natural width:
177  *
178  * <programlisting><![CDATA[
179  * static void
180  * foo_widget_get_preferred_width_for_height (GtkWidget *widget, gint for_height,
181  *                                            gint *min_width, gint *nat_width)
182  * {
183  *    if (i_am_in_height_for_width_mode)
184  *      {
185  *        GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, min_width, nat_width);
186  *      }
187  *    else
188  *      {
189  *         ... again if a widget is sometimes operating in width-for-height mode
190  *         (like a rotated GtkLabel) it can go ahead and do its real width for
191  *         height calculation here.
192  *      }
193  * }
194  * ]]></programlisting>
195  *
196  * Often a widget needs to get its own request during size request or
197  * allocation. For example, when computing height it may need to also
198  * compute width. Or when deciding how to use an allocation, the widget
199  * may need to know its natural size. In these cases, the widget should
200  * be careful to call its virtual methods directly, like this:
201  * <example>
202  *   <title>Widget calling its own size request method.</title>
203  *   <programlisting>
204  * GTK_WIDGET_GET_CLASS(widget)-&gt;get_preferred_width (widget),
205  *                                  &min, &natural);
206  *   </programlisting>
207  * </example>
208  *
209  * It will not work to use the wrapper functions, such as
210  * gtk_widget_get_preferred_width() inside your own size request
211  * implementation. These return a request adjusted by #GtkSizeGroup
212  * and by the #GtkWidgetClass.adjust_size_request() virtual method. If a
213  * widget used the wrappers inside its virtual method implementations,
214  * then the adjustments (such as widget margins) would be applied
215  * twice. GTK+ therefore does not allow this and will warn if you try
216  * to do it.
217  *
218  * Of course if you are getting the size request for
219  * <emphasis>another</emphasis> widget, such as a child of a
220  * container, you <emphasis>must</emphasis> use the wrapper APIs.
221  * Otherwise, you would not properly consider widget margins,
222  * #GtkSizeGroup, and so forth.
223  * </para>
224  * </refsect2>
225  * <refsect2 id="style-properties">
226  * <title>Style Properties</title>
227  * <para>
228  * <structname>GtkWidget</structname> introduces <firstterm>style
229  * properties</firstterm> - these are basically object properties that are stored
230  * not on the object, but in the style object associated to the widget. Style
231  * properties are set in <link linkend="gtk-Resource-Files">resource files</link>.
232  * This mechanism is used for configuring such things as the location of the
233  * scrollbar arrows through the theme, giving theme authors more control over the
234  * look of applications without the need to write a theme engine in C.
235  * </para>
236  * <para>
237  * Use gtk_widget_class_install_style_property() to install style properties for
238  * a widget class, gtk_widget_class_find_style_property() or
239  * gtk_widget_class_list_style_properties() to get information about existing
240  * style properties and gtk_widget_style_get_property(), gtk_widget_style_get() or
241  * gtk_widget_style_get_valist() to obtain the value of a style property.
242  * </para>
243  * </refsect2>
244  * <refsect2 id="GtkWidget-BUILDER-UI">
245  * <title>GtkWidget as GtkBuildable</title>
246  * <para>
247  * The GtkWidget implementation of the GtkBuildable interface supports a
248  * custom &lt;accelerator&gt; element, which has attributes named key,
249  * modifiers and signal and allows to specify accelerators.
250  * </para>
251  * <example>
252  * <title>A UI definition fragment specifying an accelerator</title>
253  * <programlisting><![CDATA[
254  * <object class="GtkButton">
255  *   <accelerator key="q" modifiers="GDK_CONTROL_MASK" signal="clicked"/>
256  * </object>
257  * ]]></programlisting>
258  * </example>
259  * <para>
260  * In addition to accelerators, <structname>GtkWidget</structname> also support a
261  * custom &lt;accessible&gt; element, which supports actions and relations.
262  * Properties on the accessible implementation of an object can be set by accessing the
263  * internal child "accessible" of a <structname>GtkWidget</structname>.
264  * </para>
265  * <example>
266  * <title>A UI definition fragment specifying an accessible</title>
267  * <programlisting><![CDATA[
268  * <object class="GtkButton" id="label1"/>
269  *   <property name="label">I am a Label for a Button</property>
270  * </object>
271  * <object class="GtkButton" id="button1">
272  *   <accessibility>
273  *     <action action_name="click" translatable="yes">Click the button.</action>
274  *     <relation target="label1" type="labelled-by"/>
275  *   </accessibility>
276  *   <child internal-child="accessible">
277  *     <object class="AtkObject" id="a11y-button1">
278  *       <property name="AtkObject::name">Clickable Button</property>
279  *     </object>
280  *   </child>
281  * </object>
282  * ]]></programlisting>
283  * </example>
284  * <para>
285  * Finally, GtkWidget allows style information such as style classes to
286  * be associated with widgets, using the custom &lt;style&gt; element:
287  * <example>
288  * <title>A UI definition fragment specifying an style class</title>
289  * <programlisting><![CDATA[
290  * <object class="GtkButton" id="button1">
291  *   <style>
292  *     <class name="my-special-button-class"/>
293  *     <class name="dark-button"/>
294  *   </style>
295  * </object>
296  * ]]></programlisting>
297  * </example>
298  * </para>
299  * </refsect2>
300  */
301
302 /* Add flags here that should not be propagated to children. By default,
303  * all flags will be set on children (think prelight or active), but we
304  * might want to not do this for some.
305  */
306 #define GTK_STATE_FLAGS_DONT_PROPAGATE (GTK_STATE_FLAG_FOCUSED)
307 #define GTK_STATE_FLAGS_DO_PROPAGATE (~GTK_STATE_FLAGS_DONT_PROPAGATE)
308
309 #define WIDGET_CLASS(w)  GTK_WIDGET_GET_CLASS (w)
310 #define INIT_PATH_SIZE  (512)
311
312 struct _GtkWidgetPrivate
313 {
314   /* The state of the widget. There are actually only
315    * 5 widget states (defined in "gtkenums.h")
316    * so 3 bits.
317    */
318   guint state_flags : 6;
319
320   guint direction             : 2;
321
322   guint in_destruction        : 1;
323   guint toplevel              : 1;
324   guint anchored              : 1;
325   guint composite_child       : 1;
326   guint no_window             : 1;
327   guint realized              : 1;
328   guint mapped                : 1;
329   guint visible               : 1;
330   guint sensitive             : 1;
331   guint can_focus             : 1;
332   guint has_focus             : 1;
333   guint can_default           : 1;
334   guint has_default           : 1;
335   guint receives_default      : 1;
336   guint has_grab              : 1;
337   guint shadowed              : 1;
338   guint rc_style              : 1;
339   guint style_update_pending  : 1;
340   guint app_paintable         : 1;
341   guint double_buffered       : 1;
342   guint redraw_on_alloc       : 1;
343   guint no_show_all           : 1;
344   guint child_visible         : 1;
345   guint multidevice           : 1;
346   guint has_shape_mask        : 1;
347   guint in_reparent           : 1;
348
349   /* Queue-resize related flags */
350   guint resize_pending        : 1;
351   guint alloc_needed          : 1;
352   guint width_request_needed  : 1;
353   guint height_request_needed : 1;
354
355   /* Expand-related flags */
356   guint need_compute_expand   : 1; /* Need to recompute computed_[hv]_expand */
357   guint computed_hexpand      : 1; /* computed results (composite of child flags) */
358   guint computed_vexpand      : 1;
359   guint hexpand               : 1; /* application-forced expand */
360   guint vexpand               : 1;
361   guint hexpand_set           : 1; /* whether to use application-forced  */
362   guint vexpand_set           : 1; /* instead of computing from children */
363
364   /* SizeGroup related flags */
365   guint sizegroup_visited     : 1;
366   guint sizegroup_bumping     : 1;
367   guint have_size_groups      : 1;
368
369   /* The widget's name. If the widget does not have a name
370    * (the name is NULL), then its name (as returned by
371    * "gtk_widget_get_name") is its class's name.
372    * Among other things, the widget name is used to determine
373    * the style to use for a widget.
374    */
375   gchar *name;
376
377   /* The style for the widget. The style contains the
378    * colors the widget should be drawn in for each state
379    * along with graphics contexts used to draw with and
380    * the font to use for text.
381    */
382   GtkStyle *style;
383   GtkStyleContext *context;
384
385   /* Widget's path for styling */
386   GtkWidgetPath *path;
387
388   /* The widget's allocated size */
389   GtkAllocation allocation;
390
391   /* The widget's requested sizes */
392   SizeRequestCache requests;
393
394   /* The widget's window or its parent window if it does
395    * not have a window. (Which will be indicated by the
396    * GTK_NO_WINDOW flag being set).
397    */
398   GdkWindow *window;
399
400   /* The widget's parent */
401   GtkWidget *parent;
402
403 #ifdef G_ENABLE_DEBUG
404   /* Number of gtk_widget_push_verify_invariants () */
405   guint verifying_invariants_count;
406 #endif /* G_ENABLE_DEBUG */
407 };
408
409 struct _GtkWidgetClassPrivate
410 {
411   GType accessible_type;
412 };
413
414 enum {
415   DESTROY,
416   SHOW,
417   HIDE,
418   MAP,
419   UNMAP,
420   REALIZE,
421   UNREALIZE,
422   SIZE_ALLOCATE,
423   STATE_FLAGS_CHANGED,
424   STATE_CHANGED,
425   PARENT_SET,
426   HIERARCHY_CHANGED,
427   STYLE_SET,
428   DIRECTION_CHANGED,
429   GRAB_NOTIFY,
430   CHILD_NOTIFY,
431   DRAW,
432   MNEMONIC_ACTIVATE,
433   GRAB_FOCUS,
434   FOCUS,
435   MOVE_FOCUS,
436   KEYNAV_FAILED,
437   EVENT,
438   EVENT_AFTER,
439   BUTTON_PRESS_EVENT,
440   BUTTON_RELEASE_EVENT,
441   SCROLL_EVENT,
442   MOTION_NOTIFY_EVENT,
443   DELETE_EVENT,
444   DESTROY_EVENT,
445   KEY_PRESS_EVENT,
446   KEY_RELEASE_EVENT,
447   ENTER_NOTIFY_EVENT,
448   LEAVE_NOTIFY_EVENT,
449   CONFIGURE_EVENT,
450   FOCUS_IN_EVENT,
451   FOCUS_OUT_EVENT,
452   MAP_EVENT,
453   UNMAP_EVENT,
454   PROPERTY_NOTIFY_EVENT,
455   SELECTION_CLEAR_EVENT,
456   SELECTION_REQUEST_EVENT,
457   SELECTION_NOTIFY_EVENT,
458   SELECTION_GET,
459   SELECTION_RECEIVED,
460   PROXIMITY_IN_EVENT,
461   PROXIMITY_OUT_EVENT,
462   VISIBILITY_NOTIFY_EVENT,
463   WINDOW_STATE_EVENT,
464   DAMAGE_EVENT,
465   GRAB_BROKEN_EVENT,
466   DRAG_BEGIN,
467   DRAG_END,
468   DRAG_DATA_DELETE,
469   DRAG_LEAVE,
470   DRAG_MOTION,
471   DRAG_DROP,
472   DRAG_DATA_GET,
473   DRAG_DATA_RECEIVED,
474   POPUP_MENU,
475   SHOW_HELP,
476   ACCEL_CLOSURES_CHANGED,
477   SCREEN_CHANGED,
478   CAN_ACTIVATE_ACCEL,
479   COMPOSITED_CHANGED,
480   QUERY_TOOLTIP,
481   DRAG_FAILED,
482   STYLE_UPDATED,
483   LAST_SIGNAL
484 };
485
486 enum {
487   PROP_0,
488   PROP_NAME,
489   PROP_PARENT,
490   PROP_WIDTH_REQUEST,
491   PROP_HEIGHT_REQUEST,
492   PROP_VISIBLE,
493   PROP_SENSITIVE,
494   PROP_APP_PAINTABLE,
495   PROP_CAN_FOCUS,
496   PROP_HAS_FOCUS,
497   PROP_IS_FOCUS,
498   PROP_CAN_DEFAULT,
499   PROP_HAS_DEFAULT,
500   PROP_RECEIVES_DEFAULT,
501   PROP_COMPOSITE_CHILD,
502   PROP_STYLE,
503   PROP_EVENTS,
504   PROP_NO_SHOW_ALL,
505   PROP_HAS_TOOLTIP,
506   PROP_TOOLTIP_MARKUP,
507   PROP_TOOLTIP_TEXT,
508   PROP_WINDOW,
509   PROP_DOUBLE_BUFFERED,
510   PROP_HALIGN,
511   PROP_VALIGN,
512   PROP_MARGIN_LEFT,
513   PROP_MARGIN_RIGHT,
514   PROP_MARGIN_TOP,
515   PROP_MARGIN_BOTTOM,
516   PROP_MARGIN,
517   PROP_HEXPAND,
518   PROP_VEXPAND,
519   PROP_HEXPAND_SET,
520   PROP_VEXPAND_SET,
521   PROP_EXPAND
522 };
523
524 typedef struct  _GtkStateData    GtkStateData;
525
526 enum {
527   STATE_CHANGE_REPLACE,
528   STATE_CHANGE_SET,
529   STATE_CHANGE_UNSET
530 };
531
532 struct _GtkStateData
533 {
534   guint         flags : 6;
535   guint         operation : 2;
536   guint         use_forall : 1;
537 };
538
539 /* --- prototypes --- */
540 static void     gtk_widget_base_class_init      (gpointer            g_class);
541 static void     gtk_widget_class_init           (GtkWidgetClass     *klass);
542 static void     gtk_widget_base_class_finalize  (GtkWidgetClass     *klass);
543 static void     gtk_widget_init                 (GtkWidget          *widget);
544 static void     gtk_widget_set_property          (GObject           *object,
545                                                   guint              prop_id,
546                                                   const GValue      *value,
547                                                   GParamSpec        *pspec);
548 static void     gtk_widget_get_property          (GObject           *object,
549                                                   guint              prop_id,
550                                                   GValue            *value,
551                                                   GParamSpec        *pspec);
552 static void     gtk_widget_dispose               (GObject           *object);
553 static void     gtk_widget_real_destroy          (GtkWidget         *object);
554 static void     gtk_widget_finalize              (GObject           *object);
555 static void     gtk_widget_real_show             (GtkWidget         *widget);
556 static void     gtk_widget_real_hide             (GtkWidget         *widget);
557 static void     gtk_widget_real_map              (GtkWidget         *widget);
558 static void     gtk_widget_real_unmap            (GtkWidget         *widget);
559 static void     gtk_widget_real_realize          (GtkWidget         *widget);
560 static void     gtk_widget_real_unrealize        (GtkWidget         *widget);
561 static void     gtk_widget_real_size_allocate    (GtkWidget         *widget,
562                                                   GtkAllocation     *allocation);
563 static void     gtk_widget_real_style_set        (GtkWidget         *widget,
564                                                   GtkStyle          *previous_style);
565 static void     gtk_widget_real_direction_changed(GtkWidget         *widget,
566                                                   GtkTextDirection   previous_direction);
567
568 static void     gtk_widget_real_grab_focus       (GtkWidget         *focus_widget);
569 static gboolean gtk_widget_real_query_tooltip    (GtkWidget         *widget,
570                                                   gint               x,
571                                                   gint               y,
572                                                   gboolean           keyboard_tip,
573                                                   GtkTooltip        *tooltip);
574 static void     gtk_widget_real_style_updated    (GtkWidget         *widget);
575 static gboolean gtk_widget_real_show_help        (GtkWidget         *widget,
576                                                   GtkWidgetHelpType  help_type);
577
578 static void     gtk_widget_dispatch_child_properties_changed    (GtkWidget        *object,
579                                                                  guint             n_pspecs,
580                                                                  GParamSpec      **pspecs);
581 static gboolean         gtk_widget_real_key_press_event         (GtkWidget        *widget,
582                                                                  GdkEventKey      *event);
583 static gboolean         gtk_widget_real_key_release_event       (GtkWidget        *widget,
584                                                                  GdkEventKey      *event);
585 static gboolean         gtk_widget_real_focus_in_event           (GtkWidget       *widget,
586                                                                   GdkEventFocus   *event);
587 static gboolean         gtk_widget_real_focus_out_event         (GtkWidget        *widget,
588                                                                  GdkEventFocus    *event);
589 static gboolean         gtk_widget_real_focus                   (GtkWidget        *widget,
590                                                                  GtkDirectionType  direction);
591 static void             gtk_widget_real_move_focus              (GtkWidget        *widget,
592                                                                  GtkDirectionType  direction);
593 static gboolean         gtk_widget_real_keynav_failed           (GtkWidget        *widget,
594                                                                  GtkDirectionType  direction);
595 #ifdef G_ENABLE_DEBUG
596 static void             gtk_widget_verify_invariants            (GtkWidget        *widget);
597 static void             gtk_widget_push_verify_invariants       (GtkWidget        *widget);
598 static void             gtk_widget_pop_verify_invariants        (GtkWidget        *widget);
599 #else
600 #define                 gtk_widget_verify_invariants(widget)
601 #define                 gtk_widget_push_verify_invariants(widget)
602 #define                 gtk_widget_pop_verify_invariants(widget)
603 #endif
604 static PangoContext*    gtk_widget_peek_pango_context           (GtkWidget        *widget);
605 static void             gtk_widget_update_pango_context         (GtkWidget        *widget);
606 static void             gtk_widget_propagate_state              (GtkWidget        *widget,
607                                                                  GtkStateData     *data);
608 ;
609 static gint             gtk_widget_event_internal               (GtkWidget        *widget,
610                                                                  GdkEvent         *event);
611 static gboolean         gtk_widget_real_mnemonic_activate       (GtkWidget        *widget,
612                                                                  gboolean          group_cycling);
613 static void             gtk_widget_real_get_width               (GtkWidget        *widget,
614                                                                  gint             *minimum_size,
615                                                                  gint             *natural_size);
616 static void             gtk_widget_real_get_height              (GtkWidget        *widget,
617                                                                  gint             *minimum_size,
618                                                                  gint             *natural_size);
619 static void             gtk_widget_real_get_height_for_width    (GtkWidget        *widget,
620                                                                  gint              width,
621                                                                  gint             *minimum_height,
622                                                                  gint             *natural_height);
623 static void             gtk_widget_real_get_width_for_height    (GtkWidget        *widget,
624                                                                  gint              height,
625                                                                  gint             *minimum_width,
626                                                                  gint             *natural_width);
627 static const GtkWidgetAuxInfo* _gtk_widget_get_aux_info_or_defaults (GtkWidget *widget);
628 static GtkWidgetAuxInfo* gtk_widget_get_aux_info                (GtkWidget        *widget,
629                                                                  gboolean          create);
630 static void             gtk_widget_aux_info_destroy             (GtkWidgetAuxInfo *aux_info);
631 static AtkObject*       gtk_widget_real_get_accessible          (GtkWidget        *widget);
632 static void             gtk_widget_accessible_interface_init    (AtkImplementorIface *iface);
633 static AtkObject*       gtk_widget_ref_accessible               (AtkImplementor *implementor);
634 static void             gtk_widget_invalidate_widget_windows    (GtkWidget        *widget,
635                                                                  cairo_region_t        *region);
636 static GdkScreen *      gtk_widget_get_screen_unchecked         (GtkWidget        *widget);
637 static void             gtk_widget_queue_shallow_draw           (GtkWidget        *widget);
638 static gboolean         gtk_widget_real_can_activate_accel      (GtkWidget *widget,
639                                                                  guint      signal_id);
640
641 static void             gtk_widget_real_set_has_tooltip         (GtkWidget *widget,
642                                                                  gboolean   has_tooltip,
643                                                                  gboolean   force);
644 static void             gtk_widget_buildable_interface_init     (GtkBuildableIface *iface);
645 static void             gtk_widget_buildable_set_name           (GtkBuildable     *buildable,
646                                                                  const gchar      *name);
647 static const gchar *    gtk_widget_buildable_get_name           (GtkBuildable     *buildable);
648 static GObject *        gtk_widget_buildable_get_internal_child (GtkBuildable *buildable,
649                                                                  GtkBuilder   *builder,
650                                                                  const gchar  *childname);
651 static void             gtk_widget_buildable_set_buildable_property (GtkBuildable     *buildable,
652                                                                      GtkBuilder       *builder,
653                                                                      const gchar      *name,
654                                                                      const GValue     *value);
655 static gboolean         gtk_widget_buildable_custom_tag_start   (GtkBuildable     *buildable,
656                                                                  GtkBuilder       *builder,
657                                                                  GObject          *child,
658                                                                  const gchar      *tagname,
659                                                                  GMarkupParser    *parser,
660                                                                  gpointer         *data);
661 static void             gtk_widget_buildable_custom_finished    (GtkBuildable     *buildable,
662                                                                  GtkBuilder       *builder,
663                                                                  GObject          *child,
664                                                                  const gchar      *tagname,
665                                                                  gpointer          data);
666 static void             gtk_widget_buildable_parser_finished    (GtkBuildable     *buildable,
667                                                                  GtkBuilder       *builder);
668
669 static GtkSizeRequestMode gtk_widget_real_get_request_mode      (GtkWidget         *widget);
670 static void             gtk_widget_real_get_width               (GtkWidget         *widget,
671                                                                  gint              *minimum_size,
672                                                                  gint              *natural_size);
673 static void             gtk_widget_real_get_height              (GtkWidget         *widget,
674                                                                  gint              *minimum_size,
675                                                                  gint              *natural_size);
676
677 static void             gtk_widget_queue_tooltip_query          (GtkWidget *widget);
678
679
680 static void             gtk_widget_real_adjust_size_request     (GtkWidget         *widget,
681                                                                  GtkOrientation     orientation,
682                                                                  gint              *minimum_size,
683                                                                  gint              *natural_size);
684 static void             gtk_widget_real_adjust_size_allocation  (GtkWidget         *widget,
685                                                                  GtkOrientation     orientation,
686                                                                  gint              *minimum_size,
687                                                                  gint              *natural_size,
688                                                                  gint              *allocated_pos,
689                                                                  gint              *allocated_size);
690
691 static void gtk_widget_set_usize_internal (GtkWidget          *widget,
692                                            gint                width,
693                                            gint                height,
694                                            GtkQueueResizeFlags flags);
695
696 static void gtk_widget_add_events_internal (GtkWidget *widget,
697                                             GdkDevice *device,
698                                             gint       events);
699 static void gtk_widget_set_device_enabled_internal (GtkWidget *widget,
700                                                     GdkDevice *device,
701                                                     gboolean   recurse,
702                                                     gboolean   enabled);
703
704 /* --- variables --- */
705 static gpointer         gtk_widget_parent_class = NULL;
706 static guint            widget_signals[LAST_SIGNAL] = { 0 };
707 static GtkStyle        *gtk_default_style = NULL;
708 static guint            composite_child_stack = 0;
709 static GtkTextDirection gtk_default_direction = GTK_TEXT_DIR_LTR;
710 static GParamSpecPool  *style_property_spec_pool = NULL;
711
712 static GQuark           quark_property_parser = 0;
713 static GQuark           quark_aux_info = 0;
714 static GQuark           quark_accel_path = 0;
715 static GQuark           quark_accel_closures = 0;
716 static GQuark           quark_event_mask = 0;
717 static GQuark           quark_device_event_mask = 0;
718 static GQuark           quark_parent_window = 0;
719 static GQuark           quark_pointer_window = 0;
720 static GQuark           quark_shape_info = 0;
721 static GQuark           quark_input_shape_info = 0;
722 static GQuark           quark_pango_context = 0;
723 static GQuark           quark_rc_style = 0;
724 static GQuark           quark_accessible_object = 0;
725 static GQuark           quark_mnemonic_labels = 0;
726 static GQuark           quark_tooltip_markup = 0;
727 static GQuark           quark_has_tooltip = 0;
728 static GQuark           quark_tooltip_window = 0;
729 static GQuark           quark_visual = 0;
730 static GQuark           quark_modifier_style = 0;
731 static GQuark           quark_enabled_devices = 0;
732 static GQuark           quark_size_groups = 0;
733 GParamSpecPool         *_gtk_widget_child_property_pool = NULL;
734 GObjectNotifyContext   *_gtk_widget_child_property_notify_context = NULL;
735
736 /* --- functions --- */
737 GType
738 gtk_widget_get_type (void)
739 {
740   static GType widget_type = 0;
741
742   if (G_UNLIKELY (widget_type == 0))
743     {
744       const GTypeInfo widget_info =
745       {
746         sizeof (GtkWidgetClass),
747         gtk_widget_base_class_init,
748         (GBaseFinalizeFunc) gtk_widget_base_class_finalize,
749         (GClassInitFunc) gtk_widget_class_init,
750         NULL,           /* class_finalize */
751         NULL,           /* class_init */
752         sizeof (GtkWidget),
753         0,              /* n_preallocs */
754         (GInstanceInitFunc) gtk_widget_init,
755         NULL,           /* value_table */
756       };
757
758       const GInterfaceInfo accessibility_info =
759       {
760         (GInterfaceInitFunc) gtk_widget_accessible_interface_init,
761         (GInterfaceFinalizeFunc) NULL,
762         NULL /* interface data */
763       };
764
765       const GInterfaceInfo buildable_info =
766       {
767         (GInterfaceInitFunc) gtk_widget_buildable_interface_init,
768         (GInterfaceFinalizeFunc) NULL,
769         NULL /* interface data */
770       };
771
772       widget_type = g_type_register_static (G_TYPE_INITIALLY_UNOWNED, "GtkWidget",
773                                             &widget_info, G_TYPE_FLAG_ABSTRACT);
774
775       g_type_add_class_private (widget_type, sizeof (GtkWidgetClassPrivate));
776
777       g_type_add_interface_static (widget_type, ATK_TYPE_IMPLEMENTOR,
778                                    &accessibility_info) ;
779       g_type_add_interface_static (widget_type, GTK_TYPE_BUILDABLE,
780                                    &buildable_info) ;
781     }
782
783   return widget_type;
784 }
785
786 static void
787 gtk_widget_base_class_init (gpointer g_class)
788 {
789   GtkWidgetClass *klass = g_class;
790
791   klass->priv = G_TYPE_CLASS_GET_PRIVATE (g_class, GTK_TYPE_WIDGET, GtkWidgetClassPrivate);
792 }
793
794 static void
795 child_property_notify_dispatcher (GObject     *object,
796                                   guint        n_pspecs,
797                                   GParamSpec **pspecs)
798 {
799   GTK_WIDGET_GET_CLASS (object)->dispatch_child_properties_changed (GTK_WIDGET (object), n_pspecs, pspecs);
800 }
801
802 /* We guard against the draw signal callbacks modifying the state of the
803  * cairo context by surounding it with save/restore.
804  * Maybe we should also cairo_new_path() just to be sure?
805  */
806 static void
807 gtk_widget_draw_marshaller (GClosure     *closure,
808                             GValue       *return_value,
809                             guint         n_param_values,
810                             const GValue *param_values,
811                             gpointer      invocation_hint,
812                             gpointer      marshal_data)
813 {
814   cairo_t *cr = g_value_get_boxed (&param_values[1]);
815
816   cairo_save (cr);
817
818   _gtk_marshal_BOOLEAN__BOXED (closure,
819                                return_value,
820                                n_param_values,
821                                param_values,
822                                invocation_hint,
823                                marshal_data);
824
825   cairo_restore (cr);
826 }
827
828 static void
829 gtk_widget_class_init (GtkWidgetClass *klass)
830 {
831   static GObjectNotifyContext cpn_context = { 0, NULL, NULL };
832   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
833   GtkBindingSet *binding_set;
834
835   gtk_widget_parent_class = g_type_class_peek_parent (klass);
836
837   quark_property_parser = g_quark_from_static_string ("gtk-rc-property-parser");
838   quark_aux_info = g_quark_from_static_string ("gtk-aux-info");
839   quark_accel_path = g_quark_from_static_string ("gtk-accel-path");
840   quark_accel_closures = g_quark_from_static_string ("gtk-accel-closures");
841   quark_event_mask = g_quark_from_static_string ("gtk-event-mask");
842   quark_device_event_mask = g_quark_from_static_string ("gtk-device-event-mask");
843   quark_parent_window = g_quark_from_static_string ("gtk-parent-window");
844   quark_pointer_window = g_quark_from_static_string ("gtk-pointer-window");
845   quark_shape_info = g_quark_from_static_string ("gtk-shape-info");
846   quark_input_shape_info = g_quark_from_static_string ("gtk-input-shape-info");
847   quark_pango_context = g_quark_from_static_string ("gtk-pango-context");
848   quark_rc_style = g_quark_from_static_string ("gtk-rc-style");
849   quark_accessible_object = g_quark_from_static_string ("gtk-accessible-object");
850   quark_mnemonic_labels = g_quark_from_static_string ("gtk-mnemonic-labels");
851   quark_tooltip_markup = g_quark_from_static_string ("gtk-tooltip-markup");
852   quark_has_tooltip = g_quark_from_static_string ("gtk-has-tooltip");
853   quark_tooltip_window = g_quark_from_static_string ("gtk-tooltip-window");
854   quark_visual = g_quark_from_static_string ("gtk-widget-visual");
855   quark_modifier_style = g_quark_from_static_string ("gtk-widget-modifier-style");
856   quark_enabled_devices = g_quark_from_static_string ("gtk-widget-enabled-devices");
857   quark_size_groups = g_quark_from_static_string ("gtk-widget-size-groups");
858
859   style_property_spec_pool = g_param_spec_pool_new (FALSE);
860   _gtk_widget_child_property_pool = g_param_spec_pool_new (TRUE);
861   cpn_context.quark_notify_queue = g_quark_from_static_string ("GtkWidget-child-property-notify-queue");
862   cpn_context.dispatcher = child_property_notify_dispatcher;
863   _gtk_widget_child_property_notify_context = &cpn_context;
864
865   gobject_class->dispose = gtk_widget_dispose;
866   gobject_class->finalize = gtk_widget_finalize;
867   gobject_class->set_property = gtk_widget_set_property;
868   gobject_class->get_property = gtk_widget_get_property;
869
870   klass->destroy = gtk_widget_real_destroy;
871
872   klass->activate_signal = 0;
873   klass->dispatch_child_properties_changed = gtk_widget_dispatch_child_properties_changed;
874   klass->show = gtk_widget_real_show;
875   klass->show_all = gtk_widget_show;
876   klass->hide = gtk_widget_real_hide;
877   klass->map = gtk_widget_real_map;
878   klass->unmap = gtk_widget_real_unmap;
879   klass->realize = gtk_widget_real_realize;
880   klass->unrealize = gtk_widget_real_unrealize;
881   klass->size_allocate = gtk_widget_real_size_allocate;
882   klass->get_request_mode = gtk_widget_real_get_request_mode;
883   klass->get_preferred_width = gtk_widget_real_get_width;
884   klass->get_preferred_height = gtk_widget_real_get_height;
885   klass->get_preferred_width_for_height = gtk_widget_real_get_width_for_height;
886   klass->get_preferred_height_for_width = gtk_widget_real_get_height_for_width;
887   klass->state_changed = NULL;
888   klass->parent_set = NULL;
889   klass->hierarchy_changed = NULL;
890   klass->style_set = gtk_widget_real_style_set;
891   klass->direction_changed = gtk_widget_real_direction_changed;
892   klass->grab_notify = NULL;
893   klass->child_notify = NULL;
894   klass->draw = NULL;
895   klass->mnemonic_activate = gtk_widget_real_mnemonic_activate;
896   klass->grab_focus = gtk_widget_real_grab_focus;
897   klass->focus = gtk_widget_real_focus;
898   klass->move_focus = gtk_widget_real_move_focus;
899   klass->keynav_failed = gtk_widget_real_keynav_failed;
900   klass->event = NULL;
901   klass->button_press_event = NULL;
902   klass->button_release_event = NULL;
903   klass->motion_notify_event = NULL;
904   klass->delete_event = NULL;
905   klass->destroy_event = NULL;
906   klass->key_press_event = gtk_widget_real_key_press_event;
907   klass->key_release_event = gtk_widget_real_key_release_event;
908   klass->enter_notify_event = NULL;
909   klass->leave_notify_event = NULL;
910   klass->configure_event = NULL;
911   klass->focus_in_event = gtk_widget_real_focus_in_event;
912   klass->focus_out_event = gtk_widget_real_focus_out_event;
913   klass->map_event = NULL;
914   klass->unmap_event = NULL;
915   klass->window_state_event = NULL;
916   klass->property_notify_event = _gtk_selection_property_notify;
917   klass->selection_clear_event = _gtk_selection_clear;
918   klass->selection_request_event = _gtk_selection_request;
919   klass->selection_notify_event = _gtk_selection_notify;
920   klass->selection_received = NULL;
921   klass->proximity_in_event = NULL;
922   klass->proximity_out_event = NULL;
923   klass->drag_begin = NULL;
924   klass->drag_end = NULL;
925   klass->drag_data_delete = NULL;
926   klass->drag_leave = NULL;
927   klass->drag_motion = NULL;
928   klass->drag_drop = NULL;
929   klass->drag_data_received = NULL;
930   klass->screen_changed = NULL;
931   klass->can_activate_accel = gtk_widget_real_can_activate_accel;
932   klass->grab_broken_event = NULL;
933   klass->query_tooltip = gtk_widget_real_query_tooltip;
934   klass->style_updated = gtk_widget_real_style_updated;
935
936   klass->show_help = gtk_widget_real_show_help;
937
938   /* Accessibility support */
939   klass->priv->accessible_type = GTK_TYPE_ACCESSIBLE;
940   klass->get_accessible = gtk_widget_real_get_accessible;
941
942   klass->adjust_size_request = gtk_widget_real_adjust_size_request;
943   klass->adjust_size_allocation = gtk_widget_real_adjust_size_allocation;
944
945   g_object_class_install_property (gobject_class,
946                                    PROP_NAME,
947                                    g_param_spec_string ("name",
948                                                         P_("Widget name"),
949                                                         P_("The name of the widget"),
950                                                         NULL,
951                                                         GTK_PARAM_READWRITE));
952   g_object_class_install_property (gobject_class,
953                                    PROP_PARENT,
954                                    g_param_spec_object ("parent",
955                                                         P_("Parent widget"),
956                                                         P_("The parent widget of this widget. Must be a Container widget"),
957                                                         GTK_TYPE_CONTAINER,
958                                                         GTK_PARAM_READWRITE));
959
960   g_object_class_install_property (gobject_class,
961                                    PROP_WIDTH_REQUEST,
962                                    g_param_spec_int ("width-request",
963                                                      P_("Width request"),
964                                                      P_("Override for width request of the widget, or -1 if natural request should be used"),
965                                                      -1,
966                                                      G_MAXINT,
967                                                      -1,
968                                                      GTK_PARAM_READWRITE));
969   g_object_class_install_property (gobject_class,
970                                    PROP_HEIGHT_REQUEST,
971                                    g_param_spec_int ("height-request",
972                                                      P_("Height request"),
973                                                      P_("Override for height request of the widget, or -1 if natural request should be used"),
974                                                      -1,
975                                                      G_MAXINT,
976                                                      -1,
977                                                      GTK_PARAM_READWRITE));
978   g_object_class_install_property (gobject_class,
979                                    PROP_VISIBLE,
980                                    g_param_spec_boolean ("visible",
981                                                          P_("Visible"),
982                                                          P_("Whether the widget is visible"),
983                                                          FALSE,
984                                                          GTK_PARAM_READWRITE));
985   g_object_class_install_property (gobject_class,
986                                    PROP_SENSITIVE,
987                                    g_param_spec_boolean ("sensitive",
988                                                          P_("Sensitive"),
989                                                          P_("Whether the widget responds to input"),
990                                                          TRUE,
991                                                          GTK_PARAM_READWRITE));
992   g_object_class_install_property (gobject_class,
993                                    PROP_APP_PAINTABLE,
994                                    g_param_spec_boolean ("app-paintable",
995                                                          P_("Application paintable"),
996                                                          P_("Whether the application will paint directly on the widget"),
997                                                          FALSE,
998                                                          GTK_PARAM_READWRITE));
999   g_object_class_install_property (gobject_class,
1000                                    PROP_CAN_FOCUS,
1001                                    g_param_spec_boolean ("can-focus",
1002                                                          P_("Can focus"),
1003                                                          P_("Whether the widget can accept the input focus"),
1004                                                          FALSE,
1005                                                          GTK_PARAM_READWRITE));
1006   g_object_class_install_property (gobject_class,
1007                                    PROP_HAS_FOCUS,
1008                                    g_param_spec_boolean ("has-focus",
1009                                                          P_("Has focus"),
1010                                                          P_("Whether the widget has the input focus"),
1011                                                          FALSE,
1012                                                          GTK_PARAM_READWRITE));
1013   g_object_class_install_property (gobject_class,
1014                                    PROP_IS_FOCUS,
1015                                    g_param_spec_boolean ("is-focus",
1016                                                          P_("Is focus"),
1017                                                          P_("Whether the widget is the focus widget within the toplevel"),
1018                                                          FALSE,
1019                                                          GTK_PARAM_READWRITE));
1020   g_object_class_install_property (gobject_class,
1021                                    PROP_CAN_DEFAULT,
1022                                    g_param_spec_boolean ("can-default",
1023                                                          P_("Can default"),
1024                                                          P_("Whether the widget can be the default widget"),
1025                                                          FALSE,
1026                                                          GTK_PARAM_READWRITE));
1027   g_object_class_install_property (gobject_class,
1028                                    PROP_HAS_DEFAULT,
1029                                    g_param_spec_boolean ("has-default",
1030                                                          P_("Has default"),
1031                                                          P_("Whether the widget is the default widget"),
1032                                                          FALSE,
1033                                                          GTK_PARAM_READWRITE));
1034   g_object_class_install_property (gobject_class,
1035                                    PROP_RECEIVES_DEFAULT,
1036                                    g_param_spec_boolean ("receives-default",
1037                                                          P_("Receives default"),
1038                                                          P_("If TRUE, the widget will receive the default action when it is focused"),
1039                                                          FALSE,
1040                                                          GTK_PARAM_READWRITE));
1041   g_object_class_install_property (gobject_class,
1042                                    PROP_COMPOSITE_CHILD,
1043                                    g_param_spec_boolean ("composite-child",
1044                                                          P_("Composite child"),
1045                                                          P_("Whether the widget is part of a composite widget"),
1046                                                          FALSE,
1047                                                          GTK_PARAM_READABLE));
1048   g_object_class_install_property (gobject_class,
1049                                    PROP_STYLE,
1050                                    g_param_spec_object ("style",
1051                                                         P_("Style"),
1052                                                         P_("The style of the widget, which contains information about how it will look (colors etc)"),
1053                                                         GTK_TYPE_STYLE,
1054                                                         GTK_PARAM_READWRITE));
1055   g_object_class_install_property (gobject_class,
1056                                    PROP_EVENTS,
1057                                    g_param_spec_flags ("events",
1058                                                        P_("Events"),
1059                                                        P_("The event mask that decides what kind of GdkEvents this widget gets"),
1060                                                        GDK_TYPE_EVENT_MASK,
1061                                                        GDK_STRUCTURE_MASK,
1062                                                        GTK_PARAM_READWRITE));
1063   g_object_class_install_property (gobject_class,
1064                                    PROP_NO_SHOW_ALL,
1065                                    g_param_spec_boolean ("no-show-all",
1066                                                          P_("No show all"),
1067                                                          P_("Whether gtk_widget_show_all() should not affect this widget"),
1068                                                          FALSE,
1069                                                          GTK_PARAM_READWRITE));
1070
1071 /**
1072  * GtkWidget:has-tooltip:
1073  *
1074  * Enables or disables the emission of #GtkWidget::query-tooltip on @widget.
1075  * A value of %TRUE indicates that @widget can have a tooltip, in this case
1076  * the widget will be queried using #GtkWidget::query-tooltip to determine
1077  * whether it will provide a tooltip or not.
1078  *
1079  * Note that setting this property to %TRUE for the first time will change
1080  * the event masks of the GdkWindows of this widget to include leave-notify
1081  * and motion-notify events.  This cannot and will not be undone when the
1082  * property is set to %FALSE again.
1083  *
1084  * Since: 2.12
1085  */
1086   g_object_class_install_property (gobject_class,
1087                                    PROP_HAS_TOOLTIP,
1088                                    g_param_spec_boolean ("has-tooltip",
1089                                                          P_("Has tooltip"),
1090                                                          P_("Whether this widget has a tooltip"),
1091                                                          FALSE,
1092                                                          GTK_PARAM_READWRITE));
1093   /**
1094    * GtkWidget:tooltip-text:
1095    *
1096    * Sets the text of tooltip to be the given string.
1097    *
1098    * Also see gtk_tooltip_set_text().
1099    *
1100    * This is a convenience property which will take care of getting the
1101    * tooltip shown if the given string is not %NULL: #GtkWidget:has-tooltip
1102    * will automatically be set to %TRUE and there will be taken care of
1103    * #GtkWidget::query-tooltip in the default signal handler.
1104    *
1105    * Since: 2.12
1106    */
1107   g_object_class_install_property (gobject_class,
1108                                    PROP_TOOLTIP_TEXT,
1109                                    g_param_spec_string ("tooltip-text",
1110                                                         P_("Tooltip Text"),
1111                                                         P_("The contents of the tooltip for this widget"),
1112                                                         NULL,
1113                                                         GTK_PARAM_READWRITE));
1114   /**
1115    * GtkWidget:tooltip-markup:
1116    *
1117    * Sets the text of tooltip to be the given string, which is marked up
1118    * with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
1119    * Also see gtk_tooltip_set_markup().
1120    *
1121    * This is a convenience property which will take care of getting the
1122    * tooltip shown if the given string is not %NULL: #GtkWidget:has-tooltip
1123    * will automatically be set to %TRUE and there will be taken care of
1124    * #GtkWidget::query-tooltip in the default signal handler.
1125    *
1126    * Since: 2.12
1127    */
1128   g_object_class_install_property (gobject_class,
1129                                    PROP_TOOLTIP_MARKUP,
1130                                    g_param_spec_string ("tooltip-markup",
1131                                                         P_("Tooltip markup"),
1132                                                         P_("The contents of the tooltip for this widget"),
1133                                                         NULL,
1134                                                         GTK_PARAM_READWRITE));
1135
1136   /**
1137    * GtkWidget:window:
1138    *
1139    * The widget's window if it is realized, %NULL otherwise.
1140    *
1141    * Since: 2.14
1142    */
1143   g_object_class_install_property (gobject_class,
1144                                    PROP_WINDOW,
1145                                    g_param_spec_object ("window",
1146                                                         P_("Window"),
1147                                                         P_("The widget's window if it is realized"),
1148                                                         GDK_TYPE_WINDOW,
1149                                                         GTK_PARAM_READABLE));
1150
1151   /**
1152    * GtkWidget:double-buffered
1153    *
1154    * Whether the widget is double buffered.
1155    *
1156    * Since: 2.18
1157    */
1158   g_object_class_install_property (gobject_class,
1159                                    PROP_DOUBLE_BUFFERED,
1160                                    g_param_spec_boolean ("double-buffered",
1161                                                          P_("Double Buffered"),
1162                                                          P_("Whether the widget is double buffered"),
1163                                                          TRUE,
1164                                                          GTK_PARAM_READWRITE));
1165
1166   /**
1167    * GtkWidget:halign:
1168    *
1169    * How to distribute horizontal space if widget gets extra space, see #GtkAlign
1170    *
1171    * Since: 3.0
1172    */
1173   g_object_class_install_property (gobject_class,
1174                                    PROP_HALIGN,
1175                                    g_param_spec_enum ("halign",
1176                                                       P_("Horizontal Alignment"),
1177                                                       P_("How to position in extra horizontal space"),
1178                                                       GTK_TYPE_ALIGN,
1179                                                       GTK_ALIGN_FILL,
1180                                                       GTK_PARAM_READWRITE));
1181
1182   /**
1183    * GtkWidget:valign:
1184    *
1185    * How to distribute vertical space if widget gets extra space, see #GtkAlign
1186    *
1187    * Since: 3.0
1188    */
1189   g_object_class_install_property (gobject_class,
1190                                    PROP_VALIGN,
1191                                    g_param_spec_enum ("valign",
1192                                                       P_("Vertical Alignment"),
1193                                                       P_("How to position in extra vertical space"),
1194                                                       GTK_TYPE_ALIGN,
1195                                                       GTK_ALIGN_FILL,
1196                                                       GTK_PARAM_READWRITE));
1197
1198   /**
1199    * GtkWidget:margin-left
1200    *
1201    * Margin on left side of widget.
1202    *
1203    * This property adds margin outside of the widget's normal size
1204    * request, the margin will be added in addition to the size from
1205    * gtk_widget_set_size_request() for example.
1206    *
1207    * Since: 3.0
1208    */
1209   g_object_class_install_property (gobject_class,
1210                                    PROP_MARGIN_LEFT,
1211                                    g_param_spec_int ("margin-left",
1212                                                      P_("Margin on Left"),
1213                                                      P_("Pixels of extra space on the left side"),
1214                                                      0,
1215                                                      G_MAXINT16,
1216                                                      0,
1217                                                      GTK_PARAM_READWRITE));
1218
1219   /**
1220    * GtkWidget:margin-right
1221    *
1222    * Margin on right side of widget.
1223    *
1224    * This property adds margin outside of the widget's normal size
1225    * request, the margin will be added in addition to the size from
1226    * gtk_widget_set_size_request() for example.
1227    *
1228    * Since: 3.0
1229    */
1230   g_object_class_install_property (gobject_class,
1231                                    PROP_MARGIN_RIGHT,
1232                                    g_param_spec_int ("margin-right",
1233                                                      P_("Margin on Right"),
1234                                                      P_("Pixels of extra space on the right side"),
1235                                                      0,
1236                                                      G_MAXINT16,
1237                                                      0,
1238                                                      GTK_PARAM_READWRITE));
1239
1240   /**
1241    * GtkWidget:margin-top
1242    *
1243    * Margin on top side of widget.
1244    *
1245    * This property adds margin outside of the widget's normal size
1246    * request, the margin will be added in addition to the size from
1247    * gtk_widget_set_size_request() for example.
1248    *
1249    * Since: 3.0
1250    */
1251   g_object_class_install_property (gobject_class,
1252                                    PROP_MARGIN_TOP,
1253                                    g_param_spec_int ("margin-top",
1254                                                      P_("Margin on Top"),
1255                                                      P_("Pixels of extra space on the top side"),
1256                                                      0,
1257                                                      G_MAXINT16,
1258                                                      0,
1259                                                      GTK_PARAM_READWRITE));
1260
1261   /**
1262    * GtkWidget:margin-bottom
1263    *
1264    * Margin on bottom side of widget.
1265    *
1266    * This property adds margin outside of the widget's normal size
1267    * request, the margin will be added in addition to the size from
1268    * gtk_widget_set_size_request() for example.
1269    *
1270    * Since: 3.0
1271    */
1272   g_object_class_install_property (gobject_class,
1273                                    PROP_MARGIN_BOTTOM,
1274                                    g_param_spec_int ("margin-bottom",
1275                                                      P_("Margin on Bottom"),
1276                                                      P_("Pixels of extra space on the bottom side"),
1277                                                      0,
1278                                                      G_MAXINT16,
1279                                                      0,
1280                                                      GTK_PARAM_READWRITE));
1281
1282   /**
1283    * GtkWidget:margin
1284    *
1285    * Sets all four sides' margin at once. If read, returns max
1286    * margin on any side.
1287    *
1288    * Since: 3.0
1289    */
1290   g_object_class_install_property (gobject_class,
1291                                    PROP_MARGIN,
1292                                    g_param_spec_int ("margin",
1293                                                      P_("All Margins"),
1294                                                      P_("Pixels of extra space on all four sides"),
1295                                                      0,
1296                                                      G_MAXINT16,
1297                                                      0,
1298                                                      GTK_PARAM_READWRITE));
1299
1300   /**
1301    * GtkWidget::destroy:
1302    * @object: the object which received the signal
1303    *
1304    * Signals that all holders of a reference to the widget should release
1305    * the reference that they hold. May result in finalization of the widget
1306    * if all references are released.
1307    */
1308   widget_signals[DESTROY] =
1309     g_signal_new (I_("destroy"),
1310                   G_TYPE_FROM_CLASS (gobject_class),
1311                   G_SIGNAL_RUN_CLEANUP | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS,
1312                   G_STRUCT_OFFSET (GtkWidgetClass, destroy),
1313                   NULL, NULL,
1314                   _gtk_marshal_VOID__VOID,
1315                   G_TYPE_NONE, 0);
1316
1317   /**
1318    * GtkWidget:hexpand
1319    *
1320    * Whether to expand horizontally. See gtk_widget_set_hexpand().
1321    *
1322    * Since: 3.0
1323    */
1324   g_object_class_install_property (gobject_class,
1325                                    PROP_HEXPAND,
1326                                    g_param_spec_boolean ("hexpand",
1327                                                          P_("Horizontal Expand"),
1328                                                          P_("Whether widget wants more horizontal space"),
1329                                                          FALSE,
1330                                                          GTK_PARAM_READWRITE));
1331
1332   /**
1333    * GtkWidget:hexpand-set
1334    *
1335    * Whether to use the #GtkWidget:hexpand property. See gtk_widget_get_hexpand_set().
1336    *
1337    * Since: 3.0
1338    */
1339   g_object_class_install_property (gobject_class,
1340                                    PROP_HEXPAND_SET,
1341                                    g_param_spec_boolean ("hexpand-set",
1342                                                          P_("Horizontal Expand Set"),
1343                                                          P_("Whether to use the hexpand property"),
1344                                                          FALSE,
1345                                                          GTK_PARAM_READWRITE));
1346
1347   /**
1348    * GtkWidget:vexpand
1349    *
1350    * Whether to expand vertically. See gtk_widget_set_vexpand().
1351    *
1352    * Since: 3.0
1353    */
1354   g_object_class_install_property (gobject_class,
1355                                    PROP_VEXPAND,
1356                                    g_param_spec_boolean ("vexpand",
1357                                                          P_("Vertical Expand"),
1358                                                          P_("Whether widget wants more vertical space"),
1359                                                          FALSE,
1360                                                          GTK_PARAM_READWRITE));
1361
1362   /**
1363    * GtkWidget:vexpand-set
1364    *
1365    * Whether to use the #GtkWidget:vexpand property. See gtk_widget_get_vexpand_set().
1366    *
1367    * Since: 3.0
1368    */
1369   g_object_class_install_property (gobject_class,
1370                                    PROP_VEXPAND_SET,
1371                                    g_param_spec_boolean ("vexpand-set",
1372                                                          P_("Vertical Expand Set"),
1373                                                          P_("Whether to use the vexpand property"),
1374                                                          FALSE,
1375                                                          GTK_PARAM_READWRITE));
1376
1377   /**
1378    * GtkWidget:expand
1379    *
1380    * Whether to expand in both directions. Setting this sets both #GtkWidget:hexpand and #GtkWidget:vexpand
1381    *
1382    * Since: 3.0
1383    */
1384   g_object_class_install_property (gobject_class,
1385                                    PROP_EXPAND,
1386                                    g_param_spec_boolean ("expand",
1387                                                          P_("Expand Both"),
1388                                                          P_("Whether widget wants to expand in both directions"),
1389                                                          FALSE,
1390                                                          GTK_PARAM_READWRITE));
1391
1392   /**
1393    * GtkWidget::show:
1394    * @widget: the object which received the signal.
1395    */
1396   widget_signals[SHOW] =
1397     g_signal_new (I_("show"),
1398                   G_TYPE_FROM_CLASS (gobject_class),
1399                   G_SIGNAL_RUN_FIRST,
1400                   G_STRUCT_OFFSET (GtkWidgetClass, show),
1401                   NULL, NULL,
1402                   _gtk_marshal_VOID__VOID,
1403                   G_TYPE_NONE, 0);
1404
1405   /**
1406    * GtkWidget::hide:
1407    * @widget: the object which received the signal.
1408    */
1409   widget_signals[HIDE] =
1410     g_signal_new (I_("hide"),
1411                   G_TYPE_FROM_CLASS (gobject_class),
1412                   G_SIGNAL_RUN_FIRST,
1413                   G_STRUCT_OFFSET (GtkWidgetClass, hide),
1414                   NULL, NULL,
1415                   _gtk_marshal_VOID__VOID,
1416                   G_TYPE_NONE, 0);
1417
1418   /**
1419    * GtkWidget::map:
1420    * @widget: the object which received the signal.
1421    */
1422   widget_signals[MAP] =
1423     g_signal_new (I_("map"),
1424                   G_TYPE_FROM_CLASS (gobject_class),
1425                   G_SIGNAL_RUN_FIRST,
1426                   G_STRUCT_OFFSET (GtkWidgetClass, map),
1427                   NULL, NULL,
1428                   _gtk_marshal_VOID__VOID,
1429                   G_TYPE_NONE, 0);
1430
1431   /**
1432    * GtkWidget::unmap:
1433    * @widget: the object which received the signal.
1434    */
1435   widget_signals[UNMAP] =
1436     g_signal_new (I_("unmap"),
1437                   G_TYPE_FROM_CLASS (gobject_class),
1438                   G_SIGNAL_RUN_FIRST,
1439                   G_STRUCT_OFFSET (GtkWidgetClass, unmap),
1440                   NULL, NULL,
1441                   _gtk_marshal_VOID__VOID,
1442                   G_TYPE_NONE, 0);
1443
1444   /**
1445    * GtkWidget::realize:
1446    * @widget: the object which received the signal.
1447    */
1448   widget_signals[REALIZE] =
1449     g_signal_new (I_("realize"),
1450                   G_TYPE_FROM_CLASS (gobject_class),
1451                   G_SIGNAL_RUN_FIRST,
1452                   G_STRUCT_OFFSET (GtkWidgetClass, realize),
1453                   NULL, NULL,
1454                   _gtk_marshal_VOID__VOID,
1455                   G_TYPE_NONE, 0);
1456
1457   /**
1458    * GtkWidget::unrealize:
1459    * @widget: the object which received the signal.
1460    */
1461   widget_signals[UNREALIZE] =
1462     g_signal_new (I_("unrealize"),
1463                   G_TYPE_FROM_CLASS (gobject_class),
1464                   G_SIGNAL_RUN_LAST,
1465                   G_STRUCT_OFFSET (GtkWidgetClass, unrealize),
1466                   NULL, NULL,
1467                   _gtk_marshal_VOID__VOID,
1468                   G_TYPE_NONE, 0);
1469
1470   /**
1471    * GtkWidget::size-allocate:
1472    * @widget: the object which received the signal.
1473    * @allocation:
1474    */
1475   widget_signals[SIZE_ALLOCATE] =
1476     g_signal_new (I_("size-allocate"),
1477                   G_TYPE_FROM_CLASS (gobject_class),
1478                   G_SIGNAL_RUN_FIRST,
1479                   G_STRUCT_OFFSET (GtkWidgetClass, size_allocate),
1480                   NULL, NULL,
1481                   _gtk_marshal_VOID__BOXED,
1482                   G_TYPE_NONE, 1,
1483                   GDK_TYPE_RECTANGLE | G_SIGNAL_TYPE_STATIC_SCOPE);
1484
1485   /**
1486    * GtkWidget::state-changed:
1487    * @widget: the object which received the signal.
1488    * @state: the previous state
1489    *
1490    * The ::state-changed signal is emitted when the widget state changes.
1491    * See gtk_widget_get_state().
1492    *
1493    * Deprecated: 3.0. Use #GtkWidget::state-flags-changed instead.
1494    */
1495   widget_signals[STATE_CHANGED] =
1496     g_signal_new (I_("state-changed"),
1497                   G_TYPE_FROM_CLASS (gobject_class),
1498                   G_SIGNAL_RUN_FIRST,
1499                   G_STRUCT_OFFSET (GtkWidgetClass, state_changed),
1500                   NULL, NULL,
1501                   _gtk_marshal_VOID__ENUM,
1502                   G_TYPE_NONE, 1,
1503                   GTK_TYPE_STATE_TYPE);
1504
1505   /**
1506    * GtkWidget::state-flags-changed:
1507    * @widget: the object which received the signal.
1508    * @flags: The previous state flags.
1509    *
1510    * The ::state-flags-changed signal is emitted when the widget state
1511    * changes, see gtk_widget_get_state_flags().
1512    *
1513    * Since: 3.0
1514    */
1515   widget_signals[STATE_FLAGS_CHANGED] =
1516     g_signal_new (I_("state-flags-changed"),
1517                   G_TYPE_FROM_CLASS (gobject_class),
1518                   G_SIGNAL_RUN_FIRST,
1519                   G_STRUCT_OFFSET (GtkWidgetClass, state_flags_changed),
1520                   NULL, NULL,
1521                   _gtk_marshal_VOID__FLAGS,
1522                   G_TYPE_NONE, 1,
1523                   GTK_TYPE_STATE_FLAGS);
1524
1525   /**
1526    * GtkWidget::parent-set:
1527    * @widget: the object on which the signal is emitted
1528    * @old_parent: (allow-none): the previous parent, or %NULL if the widget
1529    *   just got its initial parent.
1530    *
1531    * The ::parent-set signal is emitted when a new parent
1532    * has been set on a widget.
1533    */
1534   widget_signals[PARENT_SET] =
1535     g_signal_new (I_("parent-set"),
1536                   G_TYPE_FROM_CLASS (gobject_class),
1537                   G_SIGNAL_RUN_FIRST,
1538                   G_STRUCT_OFFSET (GtkWidgetClass, parent_set),
1539                   NULL, NULL,
1540                   _gtk_marshal_VOID__OBJECT,
1541                   G_TYPE_NONE, 1,
1542                   GTK_TYPE_WIDGET);
1543
1544   /**
1545    * GtkWidget::hierarchy-changed:
1546    * @widget: the object on which the signal is emitted
1547    * @previous_toplevel: (allow-none): the previous toplevel ancestor, or %NULL
1548    *   if the widget was previously unanchored
1549    *
1550    * The ::hierarchy-changed signal is emitted when the
1551    * anchored state of a widget changes. A widget is
1552    * <firstterm>anchored</firstterm> when its toplevel
1553    * ancestor is a #GtkWindow. This signal is emitted when
1554    * a widget changes from un-anchored to anchored or vice-versa.
1555    */
1556   widget_signals[HIERARCHY_CHANGED] =
1557     g_signal_new (I_("hierarchy-changed"),
1558                   G_TYPE_FROM_CLASS (gobject_class),
1559                   G_SIGNAL_RUN_LAST,
1560                   G_STRUCT_OFFSET (GtkWidgetClass, hierarchy_changed),
1561                   NULL, NULL,
1562                   _gtk_marshal_VOID__OBJECT,
1563                   G_TYPE_NONE, 1,
1564                   GTK_TYPE_WIDGET);
1565
1566   /**
1567    * GtkWidget::style-set:
1568    * @widget: the object on which the signal is emitted
1569    * @previous_style: (allow-none): the previous style, or %NULL if the widget
1570    *   just got its initial style
1571    *
1572    * The ::style-set signal is emitted when a new style has been set
1573    * on a widget. Note that style-modifying functions like
1574    * gtk_widget_modify_base() also cause this signal to be emitted.
1575    *
1576    * Note that this signal is emitted for changes to the deprecated
1577    * #GtkStyle. To track changes to the #GtkStyleContext associated
1578    * with a widget, use the #GtkWidget::style-updated signal.
1579    *
1580    * Deprecated:3.0: Use the #GtkWidget::style-updated signal
1581    */
1582   widget_signals[STYLE_SET] =
1583     g_signal_new (I_("style-set"),
1584                   G_TYPE_FROM_CLASS (gobject_class),
1585                   G_SIGNAL_RUN_FIRST,
1586                   G_STRUCT_OFFSET (GtkWidgetClass, style_set),
1587                   NULL, NULL,
1588                   _gtk_marshal_VOID__OBJECT,
1589                   G_TYPE_NONE, 1,
1590                   GTK_TYPE_STYLE);
1591
1592   /**
1593    * GtkWidget::style-updated:
1594    * @widget: the object on which the signal is emitted
1595    *
1596    * The ::style-updated signal is emitted when the #GtkStyleContext
1597    * of a widget is changed. Note that style-modifying functions like
1598    * gtk_widget_override_color() also cause this signal to be emitted.
1599    *
1600    * Since: 3.0
1601    */
1602   widget_signals[STYLE_UPDATED] =
1603     g_signal_new (I_("style-updated"),
1604                   G_TYPE_FROM_CLASS (gobject_class),
1605                   G_SIGNAL_RUN_FIRST,
1606                   G_STRUCT_OFFSET (GtkWidgetClass, style_updated),
1607                   NULL, NULL,
1608                   g_cclosure_marshal_VOID__VOID,
1609                   G_TYPE_NONE, 0);
1610
1611   /**
1612    * GtkWidget::direction-changed:
1613    * @widget: the object on which the signal is emitted
1614    * @previous_direction: the previous text direction of @widget
1615    *
1616    * The ::direction-changed signal is emitted when the text direction
1617    * of a widget changes.
1618    */
1619   widget_signals[DIRECTION_CHANGED] =
1620     g_signal_new (I_("direction-changed"),
1621                   G_TYPE_FROM_CLASS (gobject_class),
1622                   G_SIGNAL_RUN_FIRST,
1623                   G_STRUCT_OFFSET (GtkWidgetClass, direction_changed),
1624                   NULL, NULL,
1625                   _gtk_marshal_VOID__ENUM,
1626                   G_TYPE_NONE, 1,
1627                   GTK_TYPE_TEXT_DIRECTION);
1628
1629   /**
1630    * GtkWidget::grab-notify:
1631    * @widget: the object which received the signal
1632    * @was_grabbed: %FALSE if the widget becomes shadowed, %TRUE
1633    *               if it becomes unshadowed
1634    *
1635    * The ::grab-notify signal is emitted when a widget becomes
1636    * shadowed by a GTK+ grab (not a pointer or keyboard grab) on
1637    * another widget, or when it becomes unshadowed due to a grab
1638    * being removed.
1639    *
1640    * A widget is shadowed by a gtk_grab_add() when the topmost
1641    * grab widget in the grab stack of its window group is not
1642    * its ancestor.
1643    */
1644   widget_signals[GRAB_NOTIFY] =
1645     g_signal_new (I_("grab-notify"),
1646                   G_TYPE_FROM_CLASS (gobject_class),
1647                   G_SIGNAL_RUN_FIRST,
1648                   G_STRUCT_OFFSET (GtkWidgetClass, grab_notify),
1649                   NULL, NULL,
1650                   _gtk_marshal_VOID__BOOLEAN,
1651                   G_TYPE_NONE, 1,
1652                   G_TYPE_BOOLEAN);
1653
1654   /**
1655    * GtkWidget::child-notify:
1656    * @widget: the object which received the signal
1657    * @pspec: the #GParamSpec of the changed child property
1658    *
1659    * The ::child-notify signal is emitted for each
1660    * <link linkend="child-properties">child property</link>  that has
1661    * changed on an object. The signal's detail holds the property name.
1662    */
1663   widget_signals[CHILD_NOTIFY] =
1664     g_signal_new (I_("child-notify"),
1665                    G_TYPE_FROM_CLASS (gobject_class),
1666                    G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
1667                    G_STRUCT_OFFSET (GtkWidgetClass, child_notify),
1668                    NULL, NULL,
1669                    g_cclosure_marshal_VOID__PARAM,
1670                    G_TYPE_NONE, 1,
1671                    G_TYPE_PARAM);
1672
1673   /**
1674    * GtkWidget::draw:
1675    * @widget: the object which received the signal
1676    * @cr: the cairo context to draw to
1677    *
1678    * This signal is emitted when a widget is supposed to render itself.
1679    * The @widget's top left corner must be painted at the origin of
1680    * the passed in context and be sized to the values returned by
1681    * gtk_widget_get_allocated_width() and
1682    * gtk_widget_get_allocated_height().
1683    *
1684    * Signal handlers connected to this signal can modify the cairo
1685    * context passed as @cr in any way they like and don't need to
1686    * restore it. The signal emission takes care of calling cairo_save()
1687    * before and cairo_restore() after invoking the handler.
1688    *
1689    * Since: 3.0
1690    */
1691   widget_signals[DRAW] =
1692     g_signal_new (I_("draw"),
1693                    G_TYPE_FROM_CLASS (gobject_class),
1694                    G_SIGNAL_RUN_LAST,
1695                    G_STRUCT_OFFSET (GtkWidgetClass, draw),
1696                    _gtk_boolean_handled_accumulator, NULL,
1697                    gtk_widget_draw_marshaller,
1698                    G_TYPE_BOOLEAN, 1,
1699                    CAIRO_GOBJECT_TYPE_CONTEXT);
1700
1701   /**
1702    * GtkWidget::mnemonic-activate:
1703    * @widget: the object which received the signal.
1704    * @arg1:
1705    */
1706   widget_signals[MNEMONIC_ACTIVATE] =
1707     g_signal_new (I_("mnemonic-activate"),
1708                   G_TYPE_FROM_CLASS (gobject_class),
1709                   G_SIGNAL_RUN_LAST,
1710                   G_STRUCT_OFFSET (GtkWidgetClass, mnemonic_activate),
1711                   _gtk_boolean_handled_accumulator, NULL,
1712                   _gtk_marshal_BOOLEAN__BOOLEAN,
1713                   G_TYPE_BOOLEAN, 1,
1714                   G_TYPE_BOOLEAN);
1715
1716   /**
1717    * GtkWidget::grab-focus:
1718    * @widget: the object which received the signal.
1719    */
1720   widget_signals[GRAB_FOCUS] =
1721     g_signal_new (I_("grab-focus"),
1722                   G_TYPE_FROM_CLASS (gobject_class),
1723                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1724                   G_STRUCT_OFFSET (GtkWidgetClass, grab_focus),
1725                   NULL, NULL,
1726                   _gtk_marshal_VOID__VOID,
1727                   G_TYPE_NONE, 0);
1728
1729   /**
1730    * GtkWidget::focus:
1731    * @widget: the object which received the signal.
1732    * @direction:
1733    *
1734    * Returns: %TRUE to stop other handlers from being invoked for the event. %FALSE to propagate the event further.
1735    */
1736   widget_signals[FOCUS] =
1737     g_signal_new (I_("focus"),
1738                   G_TYPE_FROM_CLASS (klass),
1739                   G_SIGNAL_RUN_LAST,
1740                   G_STRUCT_OFFSET (GtkWidgetClass, focus),
1741                   _gtk_boolean_handled_accumulator, NULL,
1742                   _gtk_marshal_BOOLEAN__ENUM,
1743                   G_TYPE_BOOLEAN, 1,
1744                   GTK_TYPE_DIRECTION_TYPE);
1745
1746   /**
1747    * GtkWidget::move-focus:
1748    * @widget: the object which received the signal.
1749    * @direction:
1750    */
1751   widget_signals[MOVE_FOCUS] =
1752     g_signal_new (I_("move-focus"),
1753                   G_TYPE_FROM_CLASS (klass),
1754                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1755                   G_STRUCT_OFFSET (GtkWidgetClass, move_focus),
1756                   NULL, NULL,
1757                   _gtk_marshal_VOID__ENUM,
1758                   G_TYPE_NONE,
1759                   1,
1760                   GTK_TYPE_DIRECTION_TYPE);
1761
1762   /**
1763    * GtkWidget::keynav-failed:
1764    * @widget: the object which received the signal
1765    * @direction: the direction of movement
1766    *
1767    * Gets emitted if keyboard navigation fails.
1768    * See gtk_widget_keynav_failed() for details.
1769    *
1770    * Returns: %TRUE if stopping keyboard navigation is fine, %FALSE
1771    *          if the emitting widget should try to handle the keyboard
1772    *          navigation attempt in its parent container(s).
1773    *
1774    * Since: 2.12
1775    **/
1776   widget_signals[KEYNAV_FAILED] =
1777     g_signal_new (I_("keynav-failed"),
1778                   G_TYPE_FROM_CLASS (klass),
1779                   G_SIGNAL_RUN_LAST,
1780                   G_STRUCT_OFFSET (GtkWidgetClass, keynav_failed),
1781                   _gtk_boolean_handled_accumulator, NULL,
1782                   _gtk_marshal_BOOLEAN__ENUM,
1783                   G_TYPE_BOOLEAN, 1,
1784                   GTK_TYPE_DIRECTION_TYPE);
1785
1786   /**
1787    * GtkWidget::event:
1788    * @widget: the object which received the signal.
1789    * @event: the #GdkEvent which triggered this signal
1790    *
1791    * The GTK+ main loop will emit three signals for each GDK event delivered
1792    * to a widget: one generic ::event signal, another, more specific,
1793    * signal that matches the type of event delivered (e.g.
1794    * #GtkWidget::key-press-event) and finally a generic
1795    * #GtkWidget::event-after signal.
1796    *
1797    * Returns: %TRUE to stop other handlers from being invoked for the event
1798    * and to cancel the emission of the second specific ::event signal.
1799    *   %FALSE to propagate the event further and to allow the emission of
1800    *   the second signal. The ::event-after signal is emitted regardless of
1801    *   the return value.
1802    */
1803   widget_signals[EVENT] =
1804     g_signal_new (I_("event"),
1805                   G_TYPE_FROM_CLASS (klass),
1806                   G_SIGNAL_RUN_LAST,
1807                   G_STRUCT_OFFSET (GtkWidgetClass, event),
1808                   _gtk_boolean_handled_accumulator, NULL,
1809                   _gtk_marshal_BOOLEAN__BOXED,
1810                   G_TYPE_BOOLEAN, 1,
1811                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1812
1813   /**
1814    * GtkWidget::event-after:
1815    * @widget: the object which received the signal.
1816    * @event: the #GdkEvent which triggered this signal
1817    *
1818    * After the emission of the #GtkWidget::event signal and (optionally)
1819    * the second more specific signal, ::event-after will be emitted
1820    * regardless of the previous two signals handlers return values.
1821    *
1822    */
1823   widget_signals[EVENT_AFTER] =
1824     g_signal_new (I_("event-after"),
1825                   G_TYPE_FROM_CLASS (klass),
1826                   0,
1827                   0,
1828                   NULL, NULL,
1829                   _gtk_marshal_VOID__BOXED,
1830                   G_TYPE_NONE, 1,
1831                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1832
1833   /**
1834    * GtkWidget::button-press-event:
1835    * @widget: the object which received the signal.
1836    * @event: (type Gdk.EventButton): the #GdkEventButton which triggered
1837    *   this signal.
1838    *
1839    * The ::button-press-event signal will be emitted when a button
1840    * (typically from a mouse) is pressed.
1841    *
1842    * To receive this signal, the #GdkWindow associated to the
1843    * widget needs to enable the #GDK_BUTTON_PRESS_MASK mask.
1844    *
1845    * This signal will be sent to the grab widget if there is one.
1846    *
1847    * Returns: %TRUE to stop other handlers from being invoked for the event.
1848    *   %FALSE to propagate the event further.
1849    */
1850   widget_signals[BUTTON_PRESS_EVENT] =
1851     g_signal_new (I_("button-press-event"),
1852                   G_TYPE_FROM_CLASS (klass),
1853                   G_SIGNAL_RUN_LAST,
1854                   G_STRUCT_OFFSET (GtkWidgetClass, button_press_event),
1855                   _gtk_boolean_handled_accumulator, NULL,
1856                   _gtk_marshal_BOOLEAN__BOXED,
1857                   G_TYPE_BOOLEAN, 1,
1858                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1859
1860   /**
1861    * GtkWidget::button-release-event:
1862    * @widget: the object which received the signal.
1863    * @event: (type Gdk.EventButton): the #GdkEventButton which triggered
1864    *   this signal.
1865    *
1866    * The ::button-release-event signal will be emitted when a button
1867    * (typically from a mouse) is released.
1868    *
1869    * To receive this signal, the #GdkWindow associated to the
1870    * widget needs to enable the #GDK_BUTTON_RELEASE_MASK mask.
1871    *
1872    * This signal will be sent to the grab widget if there is one.
1873    *
1874    * Returns: %TRUE to stop other handlers from being invoked for the event.
1875    *   %FALSE to propagate the event further.
1876    */
1877   widget_signals[BUTTON_RELEASE_EVENT] =
1878     g_signal_new (I_("button-release-event"),
1879                   G_TYPE_FROM_CLASS (klass),
1880                   G_SIGNAL_RUN_LAST,
1881                   G_STRUCT_OFFSET (GtkWidgetClass, button_release_event),
1882                   _gtk_boolean_handled_accumulator, NULL,
1883                   _gtk_marshal_BOOLEAN__BOXED,
1884                   G_TYPE_BOOLEAN, 1,
1885                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1886
1887   /**
1888    * GtkWidget::scroll-event:
1889    * @widget: the object which received the signal.
1890    * @event: (type Gdk.EventScroll): the #GdkEventScroll which triggered
1891    *   this signal.
1892    *
1893    * The ::scroll-event signal is emitted when a button in the 4 to 7
1894    * range is pressed. Wheel mice are usually configured to generate
1895    * button press events for buttons 4 and 5 when the wheel is turned.
1896    *
1897    * To receive this signal, the #GdkWindow associated to the widget needs
1898    * to enable the #GDK_BUTTON_PRESS_MASK mask.
1899    *
1900    * This signal will be sent to the grab widget if there is one.
1901    *
1902    * Returns: %TRUE to stop other handlers from being invoked for the event.
1903    *   %FALSE to propagate the event further.
1904    */
1905   widget_signals[SCROLL_EVENT] =
1906     g_signal_new (I_("scroll-event"),
1907                   G_TYPE_FROM_CLASS (klass),
1908                   G_SIGNAL_RUN_LAST,
1909                   G_STRUCT_OFFSET (GtkWidgetClass, scroll_event),
1910                   _gtk_boolean_handled_accumulator, NULL,
1911                   _gtk_marshal_BOOLEAN__BOXED,
1912                   G_TYPE_BOOLEAN, 1,
1913                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1914
1915   /**
1916    * GtkWidget::motion-notify-event:
1917    * @widget: the object which received the signal.
1918    * @event: (type Gdk.EventMotion): the #GdkEventMotion which triggered
1919    *   this signal.
1920    *
1921    * The ::motion-notify-event signal is emitted when the pointer moves
1922    * over the widget's #GdkWindow.
1923    *
1924    * To receive this signal, the #GdkWindow associated to the widget
1925    * needs to enable the #GDK_POINTER_MOTION_MASK mask.
1926    *
1927    * This signal will be sent to the grab widget if there is one.
1928    *
1929    * Returns: %TRUE to stop other handlers from being invoked for the event.
1930    *   %FALSE to propagate the event further.
1931    */
1932   widget_signals[MOTION_NOTIFY_EVENT] =
1933     g_signal_new (I_("motion-notify-event"),
1934                   G_TYPE_FROM_CLASS (klass),
1935                   G_SIGNAL_RUN_LAST,
1936                   G_STRUCT_OFFSET (GtkWidgetClass, motion_notify_event),
1937                   _gtk_boolean_handled_accumulator, NULL,
1938                   _gtk_marshal_BOOLEAN__BOXED,
1939                   G_TYPE_BOOLEAN, 1,
1940                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1941
1942   /**
1943    * GtkWidget::composited-changed:
1944    * @widget: the object on which the signal is emitted
1945    *
1946    * The ::composited-changed signal is emitted when the composited
1947    * status of @widget<!-- -->s screen changes.
1948    * See gdk_screen_is_composited().
1949    */
1950   widget_signals[COMPOSITED_CHANGED] =
1951     g_signal_new (I_("composited-changed"),
1952                   G_TYPE_FROM_CLASS (klass),
1953                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1954                   G_STRUCT_OFFSET (GtkWidgetClass, composited_changed),
1955                   NULL, NULL,
1956                   _gtk_marshal_VOID__VOID,
1957                   G_TYPE_NONE, 0);
1958
1959   /**
1960    * GtkWidget::delete-event:
1961    * @widget: the object which received the signal
1962    * @event: the event which triggered this signal
1963    *
1964    * The ::delete-event signal is emitted if a user requests that
1965    * a toplevel window is closed. The default handler for this signal
1966    * destroys the window. Connecting gtk_widget_hide_on_delete() to
1967    * this signal will cause the window to be hidden instead, so that
1968    * it can later be shown again without reconstructing it.
1969    *
1970    * Returns: %TRUE to stop other handlers from being invoked for the event.
1971    *   %FALSE to propagate the event further.
1972    */
1973   widget_signals[DELETE_EVENT] =
1974     g_signal_new (I_("delete-event"),
1975                   G_TYPE_FROM_CLASS (klass),
1976                   G_SIGNAL_RUN_LAST,
1977                   G_STRUCT_OFFSET (GtkWidgetClass, delete_event),
1978                   _gtk_boolean_handled_accumulator, NULL,
1979                   _gtk_marshal_BOOLEAN__BOXED,
1980                   G_TYPE_BOOLEAN, 1,
1981                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1982
1983   /**
1984    * GtkWidget::destroy-event:
1985    * @widget: the object which received the signal.
1986    * @event: the event which triggered this signal
1987    *
1988    * The ::destroy-event signal is emitted when a #GdkWindow is destroyed.
1989    * You rarely get this signal, because most widgets disconnect themselves
1990    * from their window before they destroy it, so no widget owns the
1991    * window at destroy time.
1992    *
1993    * To receive this signal, the #GdkWindow associated to the widget needs
1994    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
1995    * automatically for all new windows.
1996    *
1997    * Returns: %TRUE to stop other handlers from being invoked for the event.
1998    *   %FALSE to propagate the event further.
1999    */
2000   widget_signals[DESTROY_EVENT] =
2001     g_signal_new (I_("destroy-event"),
2002                   G_TYPE_FROM_CLASS (klass),
2003                   G_SIGNAL_RUN_LAST,
2004                   G_STRUCT_OFFSET (GtkWidgetClass, destroy_event),
2005                   _gtk_boolean_handled_accumulator, NULL,
2006                   _gtk_marshal_BOOLEAN__BOXED,
2007                   G_TYPE_BOOLEAN, 1,
2008                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2009
2010   /**
2011    * GtkWidget::key-press-event:
2012    * @widget: the object which received the signal
2013    * @event: (type Gdk.EventKey): the #GdkEventKey which triggered this signal.
2014    *
2015    * The ::key-press-event signal is emitted when a key is pressed. The signal
2016    * emission will reoccur at the key-repeat rate when the key is kept pressed.
2017    *
2018    * To receive this signal, the #GdkWindow associated to the widget needs
2019    * to enable the #GDK_KEY_PRESS_MASK mask.
2020    *
2021    * This signal will be sent to the grab widget if there is one.
2022    *
2023    * Returns: %TRUE to stop other handlers from being invoked for the event.
2024    *   %FALSE to propagate the event further.
2025    */
2026   widget_signals[KEY_PRESS_EVENT] =
2027     g_signal_new (I_("key-press-event"),
2028                   G_TYPE_FROM_CLASS (klass),
2029                   G_SIGNAL_RUN_LAST,
2030                   G_STRUCT_OFFSET (GtkWidgetClass, key_press_event),
2031                   _gtk_boolean_handled_accumulator, NULL,
2032                   _gtk_marshal_BOOLEAN__BOXED,
2033                   G_TYPE_BOOLEAN, 1,
2034                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2035
2036   /**
2037    * GtkWidget::key-release-event:
2038    * @widget: the object which received the signal
2039    * @event: (type Gdk.EventKey): the #GdkEventKey which triggered this signal.
2040    *
2041    * The ::key-release-event signal is emitted when a key is released.
2042    *
2043    * To receive this signal, the #GdkWindow associated to the widget needs
2044    * to enable the #GDK_KEY_RELEASE_MASK mask.
2045    *
2046    * This signal will be sent to the grab widget if there is one.
2047    *
2048    * Returns: %TRUE to stop other handlers from being invoked for the event.
2049    *   %FALSE to propagate the event further.
2050    */
2051   widget_signals[KEY_RELEASE_EVENT] =
2052     g_signal_new (I_("key-release-event"),
2053                   G_TYPE_FROM_CLASS (klass),
2054                   G_SIGNAL_RUN_LAST,
2055                   G_STRUCT_OFFSET (GtkWidgetClass, key_release_event),
2056                   _gtk_boolean_handled_accumulator, NULL,
2057                   _gtk_marshal_BOOLEAN__BOXED,
2058                   G_TYPE_BOOLEAN, 1,
2059                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2060
2061   /**
2062    * GtkWidget::enter-notify-event:
2063    * @widget: the object which received the signal
2064    * @event: (type Gdk.EventCrossing): the #GdkEventCrossing which triggered
2065    *   this signal.
2066    *
2067    * The ::enter-notify-event will be emitted when the pointer enters
2068    * the @widget's window.
2069    *
2070    * To receive this signal, the #GdkWindow associated to the widget needs
2071    * to enable the #GDK_ENTER_NOTIFY_MASK mask.
2072    *
2073    * This signal will be sent to the grab widget if there is one.
2074    *
2075    * Returns: %TRUE to stop other handlers from being invoked for the event.
2076    *   %FALSE to propagate the event further.
2077    */
2078   widget_signals[ENTER_NOTIFY_EVENT] =
2079     g_signal_new (I_("enter-notify-event"),
2080                   G_TYPE_FROM_CLASS (klass),
2081                   G_SIGNAL_RUN_LAST,
2082                   G_STRUCT_OFFSET (GtkWidgetClass, enter_notify_event),
2083                   _gtk_boolean_handled_accumulator, NULL,
2084                   _gtk_marshal_BOOLEAN__BOXED,
2085                   G_TYPE_BOOLEAN, 1,
2086                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2087
2088   /**
2089    * GtkWidget::leave-notify-event:
2090    * @widget: the object which received the signal
2091    * @event: (type Gdk.EventCrossing): the #GdkEventCrossing which triggered
2092    *   this signal.
2093    *
2094    * The ::leave-notify-event will be emitted when the pointer leaves
2095    * the @widget's window.
2096    *
2097    * To receive this signal, the #GdkWindow associated to the widget needs
2098    * to enable the #GDK_LEAVE_NOTIFY_MASK mask.
2099    *
2100    * This signal will be sent to the grab widget if there is one.
2101    *
2102    * Returns: %TRUE to stop other handlers from being invoked for the event.
2103    *   %FALSE to propagate the event further.
2104    */
2105   widget_signals[LEAVE_NOTIFY_EVENT] =
2106     g_signal_new (I_("leave-notify-event"),
2107                   G_TYPE_FROM_CLASS (klass),
2108                   G_SIGNAL_RUN_LAST,
2109                   G_STRUCT_OFFSET (GtkWidgetClass, leave_notify_event),
2110                   _gtk_boolean_handled_accumulator, NULL,
2111                   _gtk_marshal_BOOLEAN__BOXED,
2112                   G_TYPE_BOOLEAN, 1,
2113                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2114
2115   /**
2116    * GtkWidget::configure-event:
2117    * @widget: the object which received the signal
2118    * @event: (type Gdk.EventConfigure): the #GdkEventConfigure which triggered
2119    *   this signal.
2120    *
2121    * The ::configure-event signal will be emitted when the size, position or
2122    * stacking of the @widget's window has changed.
2123    *
2124    * To receive this signal, the #GdkWindow associated to the widget needs
2125    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
2126    * automatically for all new windows.
2127    *
2128    * Returns: %TRUE to stop other handlers from being invoked for the event.
2129    *   %FALSE to propagate the event further.
2130    */
2131   widget_signals[CONFIGURE_EVENT] =
2132     g_signal_new (I_("configure-event"),
2133                   G_TYPE_FROM_CLASS (klass),
2134                   G_SIGNAL_RUN_LAST,
2135                   G_STRUCT_OFFSET (GtkWidgetClass, configure_event),
2136                   _gtk_boolean_handled_accumulator, NULL,
2137                   _gtk_marshal_BOOLEAN__BOXED,
2138                   G_TYPE_BOOLEAN, 1,
2139                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2140
2141   /**
2142    * GtkWidget::focus-in-event:
2143    * @widget: the object which received the signal
2144    * @event: (type Gdk.EventFocus): the #GdkEventFocus which triggered
2145    *   this signal.
2146    *
2147    * The ::focus-in-event signal will be emitted when the keyboard focus
2148    * enters the @widget's window.
2149    *
2150    * To receive this signal, the #GdkWindow associated to the widget needs
2151    * to enable the #GDK_FOCUS_CHANGE_MASK mask.
2152    *
2153    * Returns: %TRUE to stop other handlers from being invoked for the event.
2154    *   %FALSE to propagate the event further.
2155    */
2156   widget_signals[FOCUS_IN_EVENT] =
2157     g_signal_new (I_("focus-in-event"),
2158                   G_TYPE_FROM_CLASS (klass),
2159                   G_SIGNAL_RUN_LAST,
2160                   G_STRUCT_OFFSET (GtkWidgetClass, focus_in_event),
2161                   _gtk_boolean_handled_accumulator, NULL,
2162                   _gtk_marshal_BOOLEAN__BOXED,
2163                   G_TYPE_BOOLEAN, 1,
2164                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2165
2166   /**
2167    * GtkWidget::focus-out-event:
2168    * @widget: the object which received the signal
2169    * @event: (type Gdk.EventFocus): the #GdkEventFocus which triggered this
2170    *   signal.
2171    *
2172    * The ::focus-out-event signal will be emitted when the keyboard focus
2173    * leaves the @widget's window.
2174    *
2175    * To receive this signal, the #GdkWindow associated to the widget needs
2176    * to enable the #GDK_FOCUS_CHANGE_MASK mask.
2177    *
2178    * Returns: %TRUE to stop other handlers from being invoked for the event.
2179    *   %FALSE to propagate the event further.
2180    */
2181   widget_signals[FOCUS_OUT_EVENT] =
2182     g_signal_new (I_("focus-out-event"),
2183                   G_TYPE_FROM_CLASS (klass),
2184                   G_SIGNAL_RUN_LAST,
2185                   G_STRUCT_OFFSET (GtkWidgetClass, focus_out_event),
2186                   _gtk_boolean_handled_accumulator, NULL,
2187                   _gtk_marshal_BOOLEAN__BOXED,
2188                   G_TYPE_BOOLEAN, 1,
2189                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2190
2191   /**
2192    * GtkWidget::map-event:
2193    * @widget: the object which received the signal
2194    * @event: (type Gdk.EventAny): the #GdkEventAny which triggered this signal.
2195    *
2196    * The ::map-event signal will be emitted when the @widget's window is
2197    * mapped. A window is mapped when it becomes visible on the screen.
2198    *
2199    * To receive this signal, the #GdkWindow associated to the widget needs
2200    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
2201    * automatically for all new windows.
2202    *
2203    * Returns: %TRUE to stop other handlers from being invoked for the event.
2204    *   %FALSE to propagate the event further.
2205    */
2206   widget_signals[MAP_EVENT] =
2207     g_signal_new (I_("map-event"),
2208                   G_TYPE_FROM_CLASS (klass),
2209                   G_SIGNAL_RUN_LAST,
2210                   G_STRUCT_OFFSET (GtkWidgetClass, map_event),
2211                   _gtk_boolean_handled_accumulator, NULL,
2212                   _gtk_marshal_BOOLEAN__BOXED,
2213                   G_TYPE_BOOLEAN, 1,
2214                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2215
2216   /**
2217    * GtkWidget::unmap-event:
2218    * @widget: the object which received the signal
2219    * @event: (type Gdk.EventAny): the #GdkEventAny which triggered this signal
2220    *
2221    * The ::unmap-event signal will be emitted when the @widget's window is
2222    * unmapped. A window is unmapped when it becomes invisible on the screen.
2223    *
2224    * To receive this signal, the #GdkWindow associated to the widget needs
2225    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
2226    * automatically for all new windows.
2227    *
2228    * Returns: %TRUE to stop other handlers from being invoked for the event.
2229    *   %FALSE to propagate the event further.
2230    */
2231   widget_signals[UNMAP_EVENT] =
2232     g_signal_new (I_("unmap-event"),
2233                   G_TYPE_FROM_CLASS (klass),
2234                   G_SIGNAL_RUN_LAST,
2235                   G_STRUCT_OFFSET (GtkWidgetClass, unmap_event),
2236                   _gtk_boolean_handled_accumulator, NULL,
2237                   _gtk_marshal_BOOLEAN__BOXED,
2238                   G_TYPE_BOOLEAN, 1,
2239                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2240
2241   /**
2242    * GtkWidget::property-notify-event:
2243    * @widget: the object which received the signal
2244    * @event: (type Gdk.EventProperty): the #GdkEventProperty which triggered
2245    *   this signal.
2246    *
2247    * The ::property-notify-event signal will be emitted when a property on
2248    * the @widget's window has been changed or deleted.
2249    *
2250    * To receive this signal, the #GdkWindow associated to the widget needs
2251    * to enable the #GDK_PROPERTY_CHANGE_MASK mask.
2252    *
2253    * Returns: %TRUE to stop other handlers from being invoked for the event.
2254    *   %FALSE to propagate the event further.
2255    */
2256   widget_signals[PROPERTY_NOTIFY_EVENT] =
2257     g_signal_new (I_("property-notify-event"),
2258                   G_TYPE_FROM_CLASS (klass),
2259                   G_SIGNAL_RUN_LAST,
2260                   G_STRUCT_OFFSET (GtkWidgetClass, property_notify_event),
2261                   _gtk_boolean_handled_accumulator, NULL,
2262                   _gtk_marshal_BOOLEAN__BOXED,
2263                   G_TYPE_BOOLEAN, 1,
2264                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2265
2266   /**
2267    * GtkWidget::selection-clear-event:
2268    * @widget: the object which received the signal
2269    * @event: (type Gdk.EventSelection): the #GdkEventSelection which triggered
2270    *   this signal.
2271    *
2272    * The ::selection-clear-event signal will be emitted when the
2273    * the @widget's window has lost ownership of a selection.
2274    *
2275    * Returns: %TRUE to stop other handlers from being invoked for the event.
2276    *   %FALSE to propagate the event further.
2277    */
2278   widget_signals[SELECTION_CLEAR_EVENT] =
2279     g_signal_new (I_("selection-clear-event"),
2280                   G_TYPE_FROM_CLASS (klass),
2281                   G_SIGNAL_RUN_LAST,
2282                   G_STRUCT_OFFSET (GtkWidgetClass, selection_clear_event),
2283                   _gtk_boolean_handled_accumulator, NULL,
2284                   _gtk_marshal_BOOLEAN__BOXED,
2285                   G_TYPE_BOOLEAN, 1,
2286                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2287
2288   /**
2289    * GtkWidget::selection-request-event:
2290    * @widget: the object which received the signal
2291    * @event: (type Gdk.EventSelection): the #GdkEventSelection which triggered
2292    *   this signal.
2293    *
2294    * The ::selection-request-event signal will be emitted when
2295    * another client requests ownership of the selection owned by
2296    * the @widget's window.
2297    *
2298    * Returns: %TRUE to stop other handlers from being invoked for the event.
2299    *   %FALSE to propagate the event further.
2300    */
2301   widget_signals[SELECTION_REQUEST_EVENT] =
2302     g_signal_new (I_("selection-request-event"),
2303                   G_TYPE_FROM_CLASS (klass),
2304                   G_SIGNAL_RUN_LAST,
2305                   G_STRUCT_OFFSET (GtkWidgetClass, selection_request_event),
2306                   _gtk_boolean_handled_accumulator, NULL,
2307                   _gtk_marshal_BOOLEAN__BOXED,
2308                   G_TYPE_BOOLEAN, 1,
2309                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2310
2311   /**
2312    * GtkWidget::selection-notify-event:
2313    * @widget: the object which received the signal.
2314    * @event: (type Gdk.EventSelection):
2315    *
2316    * Returns: %TRUE to stop other handlers from being invoked for the event. %FALSE to propagate the event further.
2317    */
2318   widget_signals[SELECTION_NOTIFY_EVENT] =
2319     g_signal_new (I_("selection-notify-event"),
2320                   G_TYPE_FROM_CLASS (klass),
2321                   G_SIGNAL_RUN_LAST,
2322                   G_STRUCT_OFFSET (GtkWidgetClass, selection_notify_event),
2323                   _gtk_boolean_handled_accumulator, NULL,
2324                   _gtk_marshal_BOOLEAN__BOXED,
2325                   G_TYPE_BOOLEAN, 1,
2326                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2327
2328   /**
2329    * GtkWidget::selection-received:
2330    * @widget: the object which received the signal.
2331    * @data:
2332    * @time:
2333    */
2334   widget_signals[SELECTION_RECEIVED] =
2335     g_signal_new (I_("selection-received"),
2336                   G_TYPE_FROM_CLASS (klass),
2337                   G_SIGNAL_RUN_LAST,
2338                   G_STRUCT_OFFSET (GtkWidgetClass, selection_received),
2339                   NULL, NULL,
2340                   _gtk_marshal_VOID__BOXED_UINT,
2341                   G_TYPE_NONE, 2,
2342                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
2343                   G_TYPE_UINT);
2344
2345   /**
2346    * GtkWidget::selection-get:
2347    * @widget: the object which received the signal.
2348    * @data:
2349    * @info:
2350    * @time:
2351    */
2352   widget_signals[SELECTION_GET] =
2353     g_signal_new (I_("selection-get"),
2354                   G_TYPE_FROM_CLASS (klass),
2355                   G_SIGNAL_RUN_LAST,
2356                   G_STRUCT_OFFSET (GtkWidgetClass, selection_get),
2357                   NULL, NULL,
2358                   _gtk_marshal_VOID__BOXED_UINT_UINT,
2359                   G_TYPE_NONE, 3,
2360                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
2361                   G_TYPE_UINT,
2362                   G_TYPE_UINT);
2363
2364   /**
2365    * GtkWidget::proximity-in-event:
2366    * @widget: the object which received the signal
2367    * @event: (type Gdk.EventProximity): the #GdkEventProximity which triggered
2368    *   this signal.
2369    *
2370    * To receive this signal the #GdkWindow associated to the widget needs
2371    * to enable the #GDK_PROXIMITY_IN_MASK mask.
2372    *
2373    * This signal will be sent to the grab widget if there is one.
2374    *
2375    * Returns: %TRUE to stop other handlers from being invoked for the event.
2376    *   %FALSE to propagate the event further.
2377    */
2378   widget_signals[PROXIMITY_IN_EVENT] =
2379     g_signal_new (I_("proximity-in-event"),
2380                   G_TYPE_FROM_CLASS (klass),
2381                   G_SIGNAL_RUN_LAST,
2382                   G_STRUCT_OFFSET (GtkWidgetClass, proximity_in_event),
2383                   _gtk_boolean_handled_accumulator, NULL,
2384                   _gtk_marshal_BOOLEAN__BOXED,
2385                   G_TYPE_BOOLEAN, 1,
2386                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2387
2388   /**
2389    * GtkWidget::proximity-out-event:
2390    * @widget: the object which received the signal
2391    * @event: (type Gdk.EventProximity): the #GdkEventProximity which triggered
2392    *   this signal.
2393    *
2394    * To receive this signal the #GdkWindow associated to the widget needs
2395    * to enable the #GDK_PROXIMITY_OUT_MASK mask.
2396    *
2397    * This signal will be sent to the grab widget if there is one.
2398    *
2399    * Returns: %TRUE to stop other handlers from being invoked for the event.
2400    *   %FALSE to propagate the event further.
2401    */
2402   widget_signals[PROXIMITY_OUT_EVENT] =
2403     g_signal_new (I_("proximity-out-event"),
2404                   G_TYPE_FROM_CLASS (klass),
2405                   G_SIGNAL_RUN_LAST,
2406                   G_STRUCT_OFFSET (GtkWidgetClass, proximity_out_event),
2407                   _gtk_boolean_handled_accumulator, NULL,
2408                   _gtk_marshal_BOOLEAN__BOXED,
2409                   G_TYPE_BOOLEAN, 1,
2410                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2411
2412   /**
2413    * GtkWidget::drag-leave:
2414    * @widget: the object which received the signal.
2415    * @drag_context: the drag context
2416    * @time: the timestamp of the motion event
2417    *
2418    * The ::drag-leave signal is emitted on the drop site when the cursor
2419    * leaves the widget. A typical reason to connect to this signal is to
2420    * undo things done in #GtkWidget::drag-motion, e.g. undo highlighting
2421    * with gtk_drag_unhighlight()
2422    */
2423   widget_signals[DRAG_LEAVE] =
2424     g_signal_new (I_("drag-leave"),
2425                   G_TYPE_FROM_CLASS (klass),
2426                   G_SIGNAL_RUN_LAST,
2427                   G_STRUCT_OFFSET (GtkWidgetClass, drag_leave),
2428                   NULL, NULL,
2429                   _gtk_marshal_VOID__OBJECT_UINT,
2430                   G_TYPE_NONE, 2,
2431                   GDK_TYPE_DRAG_CONTEXT,
2432                   G_TYPE_UINT);
2433
2434   /**
2435    * GtkWidget::drag-begin:
2436    * @widget: the object which received the signal
2437    * @drag_context: the drag context
2438    *
2439    * The ::drag-begin signal is emitted on the drag source when a drag is
2440    * started. A typical reason to connect to this signal is to set up a
2441    * custom drag icon with gtk_drag_source_set_icon().
2442    *
2443    * Note that some widgets set up a drag icon in the default handler of
2444    * this signal, so you may have to use g_signal_connect_after() to
2445    * override what the default handler did.
2446    */
2447   widget_signals[DRAG_BEGIN] =
2448     g_signal_new (I_("drag-begin"),
2449                   G_TYPE_FROM_CLASS (klass),
2450                   G_SIGNAL_RUN_LAST,
2451                   G_STRUCT_OFFSET (GtkWidgetClass, drag_begin),
2452                   NULL, NULL,
2453                   _gtk_marshal_VOID__OBJECT,
2454                   G_TYPE_NONE, 1,
2455                   GDK_TYPE_DRAG_CONTEXT);
2456
2457   /**
2458    * GtkWidget::drag-end:
2459    * @widget: the object which received the signal
2460    * @drag_context: the drag context
2461    *
2462    * The ::drag-end signal is emitted on the drag source when a drag is
2463    * finished.  A typical reason to connect to this signal is to undo
2464    * things done in #GtkWidget::drag-begin.
2465    */
2466   widget_signals[DRAG_END] =
2467     g_signal_new (I_("drag-end"),
2468                   G_TYPE_FROM_CLASS (klass),
2469                   G_SIGNAL_RUN_LAST,
2470                   G_STRUCT_OFFSET (GtkWidgetClass, drag_end),
2471                   NULL, NULL,
2472                   _gtk_marshal_VOID__OBJECT,
2473                   G_TYPE_NONE, 1,
2474                   GDK_TYPE_DRAG_CONTEXT);
2475
2476   /**
2477    * GtkWidget::drag-data-delete:
2478    * @widget: the object which received the signal
2479    * @drag_context: the drag context
2480    *
2481    * The ::drag-data-delete signal is emitted on the drag source when a drag
2482    * with the action %GDK_ACTION_MOVE is successfully completed. The signal
2483    * handler is responsible for deleting the data that has been dropped. What
2484    * "delete" means depends on the context of the drag operation.
2485    */
2486   widget_signals[DRAG_DATA_DELETE] =
2487     g_signal_new (I_("drag-data-delete"),
2488                   G_TYPE_FROM_CLASS (klass),
2489                   G_SIGNAL_RUN_LAST,
2490                   G_STRUCT_OFFSET (GtkWidgetClass, drag_data_delete),
2491                   NULL, NULL,
2492                   _gtk_marshal_VOID__OBJECT,
2493                   G_TYPE_NONE, 1,
2494                   GDK_TYPE_DRAG_CONTEXT);
2495
2496   /**
2497    * GtkWidget::drag-failed:
2498    * @widget: the object which received the signal
2499    * @drag_context: the drag context
2500    * @result: the result of the drag operation
2501    *
2502    * The ::drag-failed signal is emitted on the drag source when a drag has
2503    * failed. The signal handler may hook custom code to handle a failed DND
2504    * operation based on the type of error, it returns %TRUE is the failure has
2505    * been already handled (not showing the default "drag operation failed"
2506    * animation), otherwise it returns %FALSE.
2507    *
2508    * Return value: %TRUE if the failed drag operation has been already handled.
2509    *
2510    * Since: 2.12
2511    */
2512   widget_signals[DRAG_FAILED] =
2513     g_signal_new (I_("drag-failed"),
2514                   G_TYPE_FROM_CLASS (klass),
2515                   G_SIGNAL_RUN_LAST,
2516                   G_STRUCT_OFFSET (GtkWidgetClass, drag_failed),
2517                   _gtk_boolean_handled_accumulator, NULL,
2518                   _gtk_marshal_BOOLEAN__OBJECT_ENUM,
2519                   G_TYPE_BOOLEAN, 2,
2520                   GDK_TYPE_DRAG_CONTEXT,
2521                   GTK_TYPE_DRAG_RESULT);
2522
2523   /**
2524    * GtkWidget::drag-motion:
2525    * @widget: the object which received the signal
2526    * @drag_context: the drag context
2527    * @x: the x coordinate of the current cursor position
2528    * @y: the y coordinate of the current cursor position
2529    * @time: the timestamp of the motion event
2530    * @returns: whether the cursor position is in a drop zone
2531    *
2532    * The drag-motion signal is emitted on the drop site when the user
2533    * moves the cursor over the widget during a drag. The signal handler
2534    * must determine whether the cursor position is in a drop zone or not.
2535    * If it is not in a drop zone, it returns %FALSE and no further processing
2536    * is necessary. Otherwise, the handler returns %TRUE. In this case, the
2537    * handler is responsible for providing the necessary information for
2538    * displaying feedback to the user, by calling gdk_drag_status().
2539    *
2540    * If the decision whether the drop will be accepted or rejected can't be
2541    * made based solely on the cursor position and the type of the data, the
2542    * handler may inspect the dragged data by calling gtk_drag_get_data() and
2543    * defer the gdk_drag_status() call to the #GtkWidget::drag-data-received
2544    * handler. Note that you cannot not pass #GTK_DEST_DEFAULT_DROP,
2545    * #GTK_DEST_DEFAULT_MOTION or #GTK_DEST_DEFAULT_ALL to gtk_drag_dest_set()
2546    * when using the drag-motion signal that way.
2547    *
2548    * Also note that there is no drag-enter signal. The drag receiver has to
2549    * keep track of whether he has received any drag-motion signals since the
2550    * last #GtkWidget::drag-leave and if not, treat the drag-motion signal as
2551    * an "enter" signal. Upon an "enter", the handler will typically highlight
2552    * the drop site with gtk_drag_highlight().
2553    * |[
2554    * static void
2555    * drag_motion (GtkWidget *widget,
2556    *              GdkDragContext *context,
2557    *              gint x,
2558    *              gint y,
2559    *              guint time)
2560    * {
2561    *   GdkAtom target;
2562    *
2563    *   PrivateData *private_data = GET_PRIVATE_DATA (widget);
2564    *
2565    *   if (!private_data->drag_highlight)
2566    *    {
2567    *      private_data->drag_highlight = 1;
2568    *      gtk_drag_highlight (widget);
2569    *    }
2570    *
2571    *   target = gtk_drag_dest_find_target (widget, context, NULL);
2572    *   if (target == GDK_NONE)
2573    *     gdk_drag_status (context, 0, time);
2574    *   else
2575    *    {
2576    *      private_data->pending_status = context->suggested_action;
2577    *      gtk_drag_get_data (widget, context, target, time);
2578    *    }
2579    *
2580    *   return TRUE;
2581    * }
2582    *
2583    * static void
2584    * drag_data_received (GtkWidget        *widget,
2585    *                     GdkDragContext   *context,
2586    *                     gint              x,
2587    *                     gint              y,
2588    *                     GtkSelectionData *selection_data,
2589    *                     guint             info,
2590    *                     guint             time)
2591    * {
2592    *   PrivateData *private_data = GET_PRIVATE_DATA (widget);
2593    *
2594    *   if (private_data->suggested_action)
2595    *    {
2596    *      private_data->suggested_action = 0;
2597    *
2598    *     /&ast; We are getting this data due to a request in drag_motion,
2599    *      * rather than due to a request in drag_drop, so we are just
2600    *      * supposed to call gdk_drag_status (), not actually paste in
2601    *      * the data.
2602    *      &ast;/
2603    *      str = gtk_selection_data_get_text (selection_data);
2604    *      if (!data_is_acceptable (str))
2605    *        gdk_drag_status (context, 0, time);
2606    *      else
2607    *        gdk_drag_status (context, private_data->suggested_action, time);
2608    *    }
2609    *   else
2610    *    {
2611    *      /&ast; accept the drop &ast;/
2612    *    }
2613    * }
2614    * ]|
2615    */
2616   widget_signals[DRAG_MOTION] =
2617     g_signal_new (I_("drag-motion"),
2618                   G_TYPE_FROM_CLASS (klass),
2619                   G_SIGNAL_RUN_LAST,
2620                   G_STRUCT_OFFSET (GtkWidgetClass, drag_motion),
2621                   _gtk_boolean_handled_accumulator, NULL,
2622                   _gtk_marshal_BOOLEAN__OBJECT_INT_INT_UINT,
2623                   G_TYPE_BOOLEAN, 4,
2624                   GDK_TYPE_DRAG_CONTEXT,
2625                   G_TYPE_INT,
2626                   G_TYPE_INT,
2627                   G_TYPE_UINT);
2628
2629   /**
2630    * GtkWidget::drag-drop:
2631    * @widget: the object which received the signal
2632    * @drag_context: the drag context
2633    * @x: the x coordinate of the current cursor position
2634    * @y: the y coordinate of the current cursor position
2635    * @time: the timestamp of the motion event
2636    * @returns: whether the cursor position is in a drop zone
2637    *
2638    * The ::drag-drop signal is emitted on the drop site when the user drops
2639    * the data onto the widget. The signal handler must determine whether
2640    * the cursor position is in a drop zone or not. If it is not in a drop
2641    * zone, it returns %FALSE and no further processing is necessary.
2642    * Otherwise, the handler returns %TRUE. In this case, the handler must
2643    * ensure that gtk_drag_finish() is called to let the source know that
2644    * the drop is done. The call to gtk_drag_finish() can be done either
2645    * directly or in a #GtkWidget::drag-data-received handler which gets
2646    * triggered by calling gtk_drag_get_data() to receive the data for one
2647    * or more of the supported targets.
2648    */
2649   widget_signals[DRAG_DROP] =
2650     g_signal_new (I_("drag-drop"),
2651                   G_TYPE_FROM_CLASS (klass),
2652                   G_SIGNAL_RUN_LAST,
2653                   G_STRUCT_OFFSET (GtkWidgetClass, drag_drop),
2654                   _gtk_boolean_handled_accumulator, NULL,
2655                   _gtk_marshal_BOOLEAN__OBJECT_INT_INT_UINT,
2656                   G_TYPE_BOOLEAN, 4,
2657                   GDK_TYPE_DRAG_CONTEXT,
2658                   G_TYPE_INT,
2659                   G_TYPE_INT,
2660                   G_TYPE_UINT);
2661
2662   /**
2663    * GtkWidget::drag-data-get:
2664    * @widget: the object which received the signal
2665    * @drag_context: the drag context
2666    * @data: the #GtkSelectionData to be filled with the dragged data
2667    * @info: the info that has been registered with the target in the
2668    *        #GtkTargetList
2669    * @time: the timestamp at which the data was requested
2670    *
2671    * The ::drag-data-get signal is emitted on the drag source when the drop
2672    * site requests the data which is dragged. It is the responsibility of
2673    * the signal handler to fill @data with the data in the format which
2674    * is indicated by @info. See gtk_selection_data_set() and
2675    * gtk_selection_data_set_text().
2676    */
2677   widget_signals[DRAG_DATA_GET] =
2678     g_signal_new (I_("drag-data-get"),
2679                   G_TYPE_FROM_CLASS (klass),
2680                   G_SIGNAL_RUN_LAST,
2681                   G_STRUCT_OFFSET (GtkWidgetClass, drag_data_get),
2682                   NULL, NULL,
2683                   _gtk_marshal_VOID__OBJECT_BOXED_UINT_UINT,
2684                   G_TYPE_NONE, 4,
2685                   GDK_TYPE_DRAG_CONTEXT,
2686                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
2687                   G_TYPE_UINT,
2688                   G_TYPE_UINT);
2689
2690   /**
2691    * GtkWidget::drag-data-received:
2692    * @widget: the object which received the signal
2693    * @drag_context: the drag context
2694    * @x: where the drop happened
2695    * @y: where the drop happened
2696    * @data: the received data
2697    * @info: the info that has been registered with the target in the
2698    *        #GtkTargetList
2699    * @time: the timestamp at which the data was received
2700    *
2701    * The ::drag-data-received signal is emitted on the drop site when the
2702    * dragged data has been received. If the data was received in order to
2703    * determine whether the drop will be accepted, the handler is expected
2704    * to call gdk_drag_status() and <emphasis>not</emphasis> finish the drag.
2705    * If the data was received in response to a #GtkWidget::drag-drop signal
2706    * (and this is the last target to be received), the handler for this
2707    * signal is expected to process the received data and then call
2708    * gtk_drag_finish(), setting the @success parameter depending on whether
2709    * the data was processed successfully.
2710    *
2711    * The handler may inspect and modify @drag_context->action before calling
2712    * gtk_drag_finish(), e.g. to implement %GDK_ACTION_ASK as shown in the
2713    * following example:
2714    * |[
2715    * void
2716    * drag_data_received (GtkWidget          *widget,
2717    *                     GdkDragContext     *drag_context,
2718    *                     gint                x,
2719    *                     gint                y,
2720    *                     GtkSelectionData   *data,
2721    *                     guint               info,
2722    *                     guint               time)
2723    * {
2724    *   if ((data->length >= 0) && (data->format == 8))
2725    *     {
2726    *       if (drag_context->action == GDK_ACTION_ASK)
2727    *         {
2728    *           GtkWidget *dialog;
2729    *           gint response;
2730    *
2731    *           dialog = gtk_message_dialog_new (NULL,
2732    *                                            GTK_DIALOG_MODAL |
2733    *                                            GTK_DIALOG_DESTROY_WITH_PARENT,
2734    *                                            GTK_MESSAGE_INFO,
2735    *                                            GTK_BUTTONS_YES_NO,
2736    *                                            "Move the data ?\n");
2737    *           response = gtk_dialog_run (GTK_DIALOG (dialog));
2738    *           gtk_widget_destroy (dialog);
2739    *
2740    *           if (response == GTK_RESPONSE_YES)
2741    *             drag_context->action = GDK_ACTION_MOVE;
2742    *           else
2743    *             drag_context->action = GDK_ACTION_COPY;
2744    *          }
2745    *
2746    *       gtk_drag_finish (drag_context, TRUE, FALSE, time);
2747    *       return;
2748    *     }
2749    *
2750    *    gtk_drag_finish (drag_context, FALSE, FALSE, time);
2751    *  }
2752    * ]|
2753    */
2754   widget_signals[DRAG_DATA_RECEIVED] =
2755     g_signal_new (I_("drag-data-received"),
2756                   G_TYPE_FROM_CLASS (klass),
2757                   G_SIGNAL_RUN_LAST,
2758                   G_STRUCT_OFFSET (GtkWidgetClass, drag_data_received),
2759                   NULL, NULL,
2760                   _gtk_marshal_VOID__OBJECT_INT_INT_BOXED_UINT_UINT,
2761                   G_TYPE_NONE, 6,
2762                   GDK_TYPE_DRAG_CONTEXT,
2763                   G_TYPE_INT,
2764                   G_TYPE_INT,
2765                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
2766                   G_TYPE_UINT,
2767                   G_TYPE_UINT);
2768
2769   /**
2770    * GtkWidget::visibility-notify-event:
2771    * @widget: the object which received the signal
2772    * @event: (type Gdk.EventVisibility): the #GdkEventVisibility which
2773    *   triggered this signal.
2774    *
2775    * The ::visibility-notify-event will be emitted when the @widget's window
2776    * is obscured or unobscured.
2777    *
2778    * To receive this signal the #GdkWindow associated to the widget needs
2779    * to enable the #GDK_VISIBILITY_NOTIFY_MASK mask.
2780    *
2781    * Returns: %TRUE to stop other handlers from being invoked for the event.
2782    *   %FALSE to propagate the event further.
2783    */
2784   widget_signals[VISIBILITY_NOTIFY_EVENT] =
2785     g_signal_new (I_("visibility-notify-event"),
2786                   G_TYPE_FROM_CLASS (klass),
2787                   G_SIGNAL_RUN_LAST,
2788                   G_STRUCT_OFFSET (GtkWidgetClass, visibility_notify_event),
2789                   _gtk_boolean_handled_accumulator, NULL,
2790                   _gtk_marshal_BOOLEAN__BOXED,
2791                   G_TYPE_BOOLEAN, 1,
2792                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2793
2794   /**
2795    * GtkWidget::window-state-event:
2796    * @widget: the object which received the signal
2797    * @event: (type Gdk.EventWindowState): the #GdkEventWindowState which
2798    *   triggered this signal.
2799    *
2800    * The ::window-state-event will be emitted when the state of the
2801    * toplevel window associated to the @widget changes.
2802    *
2803    * To receive this signal the #GdkWindow associated to the widget
2804    * needs to enable the #GDK_STRUCTURE_MASK mask. GDK will enable
2805    * this mask automatically for all new windows.
2806    *
2807    * Returns: %TRUE to stop other handlers from being invoked for the
2808    *   event. %FALSE to propagate the event further.
2809    */
2810   widget_signals[WINDOW_STATE_EVENT] =
2811     g_signal_new (I_("window-state-event"),
2812                   G_TYPE_FROM_CLASS (klass),
2813                   G_SIGNAL_RUN_LAST,
2814                   G_STRUCT_OFFSET (GtkWidgetClass, window_state_event),
2815                   _gtk_boolean_handled_accumulator, NULL,
2816                   _gtk_marshal_BOOLEAN__BOXED,
2817                   G_TYPE_BOOLEAN, 1,
2818                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2819
2820   /**
2821    * GtkWidget::damage-event:
2822    * @widget: the object which received the signal
2823    * @event: (type Gdk.EventExpose): the #GdkEventExpose event
2824    *
2825    * Emitted when a redirected window belonging to @widget gets drawn into.
2826    * The region/area members of the event shows what area of the redirected
2827    * drawable was drawn into.
2828    *
2829    * Returns: %TRUE to stop other handlers from being invoked for the event.
2830    *   %FALSE to propagate the event further.
2831    *
2832    * Since: 2.14
2833    */
2834   widget_signals[DAMAGE_EVENT] =
2835     g_signal_new (I_("damage-event"),
2836                   G_TYPE_FROM_CLASS (klass),
2837                   G_SIGNAL_RUN_LAST,
2838                   G_STRUCT_OFFSET (GtkWidgetClass, damage_event),
2839                   _gtk_boolean_handled_accumulator, NULL,
2840                   _gtk_marshal_BOOLEAN__BOXED,
2841                   G_TYPE_BOOLEAN, 1,
2842                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2843
2844 /**
2845    * GtkWidget::grab-broken-event:
2846    * @widget: the object which received the signal
2847    * @event: (type Gdk.EventGrabBroken): the #GdkEventGrabBroken event
2848    *
2849    * Emitted when a pointer or keyboard grab on a window belonging
2850    * to @widget gets broken.
2851    *
2852    * On X11, this happens when the grab window becomes unviewable
2853    * (i.e. it or one of its ancestors is unmapped), or if the same
2854    * application grabs the pointer or keyboard again.
2855    *
2856    * Returns: %TRUE to stop other handlers from being invoked for
2857    *   the event. %FALSE to propagate the event further.
2858    *
2859    * Since: 2.8
2860    */
2861   widget_signals[GRAB_BROKEN_EVENT] =
2862     g_signal_new (I_("grab-broken-event"),
2863                   G_TYPE_FROM_CLASS (klass),
2864                   G_SIGNAL_RUN_LAST,
2865                   G_STRUCT_OFFSET (GtkWidgetClass, grab_broken_event),
2866                   _gtk_boolean_handled_accumulator, NULL,
2867                   _gtk_marshal_BOOLEAN__BOXED,
2868                   G_TYPE_BOOLEAN, 1,
2869                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2870
2871   /**
2872    * GtkWidget::query-tooltip:
2873    * @widget: the object which received the signal
2874    * @x: the x coordinate of the cursor position where the request has
2875    *     been emitted, relative to @widget's left side
2876    * @y: the y coordinate of the cursor position where the request has
2877    *     been emitted, relative to @widget's top
2878    * @keyboard_mode: %TRUE if the tooltip was trigged using the keyboard
2879    * @tooltip: a #GtkTooltip
2880    *
2881    * Emitted when #GtkWidget:has-tooltip is %TRUE and the #GtkSettings:gtk-tooltip-timeout
2882    * has expired with the cursor hovering "above" @widget; or emitted when @widget got
2883    * focus in keyboard mode.
2884    *
2885    * Using the given coordinates, the signal handler should determine
2886    * whether a tooltip should be shown for @widget. If this is the case
2887    * %TRUE should be returned, %FALSE otherwise.  Note that if
2888    * @keyboard_mode is %TRUE, the values of @x and @y are undefined and
2889    * should not be used.
2890    *
2891    * The signal handler is free to manipulate @tooltip with the therefore
2892    * destined function calls.
2893    *
2894    * Returns: %TRUE if @tooltip should be shown right now, %FALSE otherwise.
2895    *
2896    * Since: 2.12
2897    */
2898   widget_signals[QUERY_TOOLTIP] =
2899     g_signal_new (I_("query-tooltip"),
2900                   G_TYPE_FROM_CLASS (klass),
2901                   G_SIGNAL_RUN_LAST,
2902                   G_STRUCT_OFFSET (GtkWidgetClass, query_tooltip),
2903                   _gtk_boolean_handled_accumulator, NULL,
2904                   _gtk_marshal_BOOLEAN__INT_INT_BOOLEAN_OBJECT,
2905                   G_TYPE_BOOLEAN, 4,
2906                   G_TYPE_INT,
2907                   G_TYPE_INT,
2908                   G_TYPE_BOOLEAN,
2909                   GTK_TYPE_TOOLTIP);
2910
2911   /**
2912    * GtkWidget::popup-menu
2913    * @widget: the object which received the signal
2914    *
2915    * This signal gets emitted whenever a widget should pop up a context
2916    * menu. This usually happens through the standard key binding mechanism;
2917    * by pressing a certain key while a widget is focused, the user can cause
2918    * the widget to pop up a menu.  For example, the #GtkEntry widget creates
2919    * a menu with clipboard commands. See <xref linkend="checklist-popup-menu"/>
2920    * for an example of how to use this signal.
2921    *
2922    * Returns: %TRUE if a menu was activated
2923    */
2924   widget_signals[POPUP_MENU] =
2925     g_signal_new (I_("popup-menu"),
2926                   G_TYPE_FROM_CLASS (klass),
2927                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
2928                   G_STRUCT_OFFSET (GtkWidgetClass, popup_menu),
2929                   _gtk_boolean_handled_accumulator, NULL,
2930                   _gtk_marshal_BOOLEAN__VOID,
2931                   G_TYPE_BOOLEAN, 0);
2932
2933   /**
2934    * GtkWidget::show-help:
2935    * @widget: the object which received the signal.
2936    * @help_type:
2937    */
2938   widget_signals[SHOW_HELP] =
2939     g_signal_new (I_("show-help"),
2940                   G_TYPE_FROM_CLASS (klass),
2941                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
2942                   G_STRUCT_OFFSET (GtkWidgetClass, show_help),
2943                   _gtk_boolean_handled_accumulator, NULL,
2944                   _gtk_marshal_BOOLEAN__ENUM,
2945                   G_TYPE_BOOLEAN, 1,
2946                   GTK_TYPE_WIDGET_HELP_TYPE);
2947
2948   /**
2949    * GtkWidget::accel-closures-changed:
2950    * @widget: the object which received the signal.
2951    */
2952   widget_signals[ACCEL_CLOSURES_CHANGED] =
2953     g_signal_new (I_("accel-closures-changed"),
2954                   G_TYPE_FROM_CLASS (klass),
2955                   0,
2956                   0,
2957                   NULL, NULL,
2958                   _gtk_marshal_VOID__VOID,
2959                   G_TYPE_NONE, 0);
2960
2961   /**
2962    * GtkWidget::screen-changed:
2963    * @widget: the object on which the signal is emitted
2964    * @previous_screen: (allow-none): the previous screen, or %NULL if the
2965    *   widget was not associated with a screen before
2966    *
2967    * The ::screen-changed signal gets emitted when the
2968    * screen of a widget has changed.
2969    */
2970   widget_signals[SCREEN_CHANGED] =
2971     g_signal_new (I_("screen-changed"),
2972                   G_TYPE_FROM_CLASS (klass),
2973                   G_SIGNAL_RUN_LAST,
2974                   G_STRUCT_OFFSET (GtkWidgetClass, screen_changed),
2975                   NULL, NULL,
2976                   _gtk_marshal_VOID__OBJECT,
2977                   G_TYPE_NONE, 1,
2978                   GDK_TYPE_SCREEN);
2979
2980   /**
2981    * GtkWidget::can-activate-accel:
2982    * @widget: the object which received the signal
2983    * @signal_id: the ID of a signal installed on @widget
2984    *
2985    * Determines whether an accelerator that activates the signal
2986    * identified by @signal_id can currently be activated.
2987    * This signal is present to allow applications and derived
2988    * widgets to override the default #GtkWidget handling
2989    * for determining whether an accelerator can be activated.
2990    *
2991    * Returns: %TRUE if the signal can be activated.
2992    */
2993   widget_signals[CAN_ACTIVATE_ACCEL] =
2994      g_signal_new (I_("can-activate-accel"),
2995                   G_TYPE_FROM_CLASS (klass),
2996                   G_SIGNAL_RUN_LAST,
2997                   G_STRUCT_OFFSET (GtkWidgetClass, can_activate_accel),
2998                   _gtk_boolean_handled_accumulator, NULL,
2999                   _gtk_marshal_BOOLEAN__UINT,
3000                   G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
3001
3002   binding_set = gtk_binding_set_by_class (klass);
3003   gtk_binding_entry_add_signal (binding_set, GDK_KEY_F10, GDK_SHIFT_MASK,
3004                                 "popup-menu", 0);
3005   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Menu, 0,
3006                                 "popup-menu", 0);
3007
3008   gtk_binding_entry_add_signal (binding_set, GDK_KEY_F1, GDK_CONTROL_MASK,
3009                                 "show-help", 1,
3010                                 GTK_TYPE_WIDGET_HELP_TYPE,
3011                                 GTK_WIDGET_HELP_TOOLTIP);
3012   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_F1, GDK_CONTROL_MASK,
3013                                 "show-help", 1,
3014                                 GTK_TYPE_WIDGET_HELP_TYPE,
3015                                 GTK_WIDGET_HELP_TOOLTIP);
3016   gtk_binding_entry_add_signal (binding_set, GDK_KEY_F1, GDK_SHIFT_MASK,
3017                                 "show-help", 1,
3018                                 GTK_TYPE_WIDGET_HELP_TYPE,
3019                                 GTK_WIDGET_HELP_WHATS_THIS);
3020   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_F1, GDK_SHIFT_MASK,
3021                                 "show-help", 1,
3022                                 GTK_TYPE_WIDGET_HELP_TYPE,
3023                                 GTK_WIDGET_HELP_WHATS_THIS);
3024
3025   gtk_widget_class_install_style_property (klass,
3026                                            g_param_spec_boolean ("interior-focus",
3027                                                                  P_("Interior Focus"),
3028                                                                  P_("Whether to draw the focus indicator inside widgets"),
3029                                                                  TRUE,
3030                                                                  GTK_PARAM_READABLE));
3031
3032   gtk_widget_class_install_style_property (klass,
3033                                            g_param_spec_int ("focus-line-width",
3034                                                              P_("Focus linewidth"),
3035                                                              P_("Width, in pixels, of the focus indicator line"),
3036                                                              0, G_MAXINT, 1,
3037                                                              GTK_PARAM_READABLE));
3038
3039   gtk_widget_class_install_style_property (klass,
3040                                            g_param_spec_string ("focus-line-pattern",
3041                                                                 P_("Focus line dash pattern"),
3042                                                                 P_("Dash pattern used to draw the focus indicator"),
3043                                                                 "\1\1",
3044                                                                 GTK_PARAM_READABLE));
3045   gtk_widget_class_install_style_property (klass,
3046                                            g_param_spec_int ("focus-padding",
3047                                                              P_("Focus padding"),
3048                                                              P_("Width, in pixels, between focus indicator and the widget 'box'"),
3049                                                              0, G_MAXINT, 1,
3050                                                              GTK_PARAM_READABLE));
3051   gtk_widget_class_install_style_property (klass,
3052                                            g_param_spec_boxed ("cursor-color",
3053                                                                P_("Cursor color"),
3054                                                                P_("Color with which to draw insertion cursor"),
3055                                                                GDK_TYPE_COLOR,
3056                                                                GTK_PARAM_READABLE));
3057   gtk_widget_class_install_style_property (klass,
3058                                            g_param_spec_boxed ("secondary-cursor-color",
3059                                                                P_("Secondary cursor color"),
3060                                                                P_("Color with which to draw the secondary insertion cursor when editing mixed right-to-left and left-to-right text"),
3061                                                                GDK_TYPE_COLOR,
3062                                                                GTK_PARAM_READABLE));
3063   gtk_widget_class_install_style_property (klass,
3064                                            g_param_spec_float ("cursor-aspect-ratio",
3065                                                                P_("Cursor line aspect ratio"),
3066                                                                P_("Aspect ratio with which to draw insertion cursor"),
3067                                                                0.0, 1.0, 0.04,
3068                                                                GTK_PARAM_READABLE));
3069
3070   gtk_widget_class_install_style_property (klass,
3071                                            g_param_spec_boolean ("window-dragging",
3072                                                                  P_("Window dragging"),
3073                                                                  P_("Whether windows can be dragged by clicking on empty areas"),
3074                                                                  FALSE,
3075                                                                  GTK_PARAM_READABLE));
3076
3077   /**
3078    * GtkWidget:link-color:
3079    *
3080    * The "link-color" style property defines the color of unvisited links.
3081    *
3082    * Since: 2.10
3083    */
3084   gtk_widget_class_install_style_property (klass,
3085                                            g_param_spec_boxed ("link-color",
3086                                                                P_("Unvisited Link Color"),
3087                                                                P_("Color of unvisited links"),
3088                                                                GDK_TYPE_COLOR,
3089                                                                GTK_PARAM_READABLE));
3090
3091   /**
3092    * GtkWidget:visited-link-color:
3093    *
3094    * The "visited-link-color" style property defines the color of visited links.
3095    *
3096    * Since: 2.10
3097    */
3098   gtk_widget_class_install_style_property (klass,
3099                                            g_param_spec_boxed ("visited-link-color",
3100                                                                P_("Visited Link Color"),
3101                                                                P_("Color of visited links"),
3102                                                                GDK_TYPE_COLOR,
3103                                                                GTK_PARAM_READABLE));
3104
3105   /**
3106    * GtkWidget:wide-separators:
3107    *
3108    * The "wide-separators" style property defines whether separators have
3109    * configurable width and should be drawn using a box instead of a line.
3110    *
3111    * Since: 2.10
3112    */
3113   gtk_widget_class_install_style_property (klass,
3114                                            g_param_spec_boolean ("wide-separators",
3115                                                                  P_("Wide Separators"),
3116                                                                  P_("Whether separators have configurable width and should be drawn using a box instead of a line"),
3117                                                                  FALSE,
3118                                                                  GTK_PARAM_READABLE));
3119
3120   /**
3121    * GtkWidget:separator-width:
3122    *
3123    * The "separator-width" style property defines the width of separators.
3124    * This property only takes effect if #GtkWidget:wide-separators is %TRUE.
3125    *
3126    * Since: 2.10
3127    */
3128   gtk_widget_class_install_style_property (klass,
3129                                            g_param_spec_int ("separator-width",
3130                                                              P_("Separator Width"),
3131                                                              P_("The width of separators if wide-separators is TRUE"),
3132                                                              0, G_MAXINT, 0,
3133                                                              GTK_PARAM_READABLE));
3134
3135   /**
3136    * GtkWidget:separator-height:
3137    *
3138    * The "separator-height" style property defines the height of separators.
3139    * This property only takes effect if #GtkWidget:wide-separators is %TRUE.
3140    *
3141    * Since: 2.10
3142    */
3143   gtk_widget_class_install_style_property (klass,
3144                                            g_param_spec_int ("separator-height",
3145                                                              P_("Separator Height"),
3146                                                              P_("The height of separators if \"wide-separators\" is TRUE"),
3147                                                              0, G_MAXINT, 0,
3148                                                              GTK_PARAM_READABLE));
3149
3150   /**
3151    * GtkWidget:scroll-arrow-hlength:
3152    *
3153    * The "scroll-arrow-hlength" style property defines the length of
3154    * horizontal scroll arrows.
3155    *
3156    * Since: 2.10
3157    */
3158   gtk_widget_class_install_style_property (klass,
3159                                            g_param_spec_int ("scroll-arrow-hlength",
3160                                                              P_("Horizontal Scroll Arrow Length"),
3161                                                              P_("The length of horizontal scroll arrows"),
3162                                                              1, G_MAXINT, 16,
3163                                                              GTK_PARAM_READABLE));
3164
3165   /**
3166    * GtkWidget:scroll-arrow-vlength:
3167    *
3168    * The "scroll-arrow-vlength" style property defines the length of
3169    * vertical scroll arrows.
3170    *
3171    * Since: 2.10
3172    */
3173   gtk_widget_class_install_style_property (klass,
3174                                            g_param_spec_int ("scroll-arrow-vlength",
3175                                                              P_("Vertical Scroll Arrow Length"),
3176                                                              P_("The length of vertical scroll arrows"),
3177                                                              1, G_MAXINT, 16,
3178                                                              GTK_PARAM_READABLE));
3179
3180   g_type_class_add_private (klass, sizeof (GtkWidgetPrivate));
3181
3182   gtk_widget_class_set_accessible_type (klass, GTK_TYPE_WIDGET_ACCESSIBLE);
3183 }
3184
3185 static void
3186 gtk_widget_base_class_finalize (GtkWidgetClass *klass)
3187 {
3188   GList *list, *node;
3189
3190   list = g_param_spec_pool_list_owned (style_property_spec_pool, G_OBJECT_CLASS_TYPE (klass));
3191   for (node = list; node; node = node->next)
3192     {
3193       GParamSpec *pspec = node->data;
3194
3195       g_param_spec_pool_remove (style_property_spec_pool, pspec);
3196       g_param_spec_unref (pspec);
3197     }
3198   g_list_free (list);
3199 }
3200
3201 static void
3202 gtk_widget_set_property (GObject         *object,
3203                          guint            prop_id,
3204                          const GValue    *value,
3205                          GParamSpec      *pspec)
3206 {
3207   GtkWidget *widget = GTK_WIDGET (object);
3208
3209   switch (prop_id)
3210     {
3211       gboolean tmp;
3212       gchar *tooltip_markup;
3213       const gchar *tooltip_text;
3214       GtkWindow *tooltip_window;
3215
3216     case PROP_NAME:
3217       gtk_widget_set_name (widget, g_value_get_string (value));
3218       break;
3219     case PROP_PARENT:
3220       gtk_container_add (GTK_CONTAINER (g_value_get_object (value)), widget);
3221       break;
3222     case PROP_WIDTH_REQUEST:
3223       gtk_widget_set_usize_internal (widget, g_value_get_int (value), -2, 0);
3224       break;
3225     case PROP_HEIGHT_REQUEST:
3226       gtk_widget_set_usize_internal (widget, -2, g_value_get_int (value), 0);
3227       break;
3228     case PROP_VISIBLE:
3229       gtk_widget_set_visible (widget, g_value_get_boolean (value));
3230       break;
3231     case PROP_SENSITIVE:
3232       gtk_widget_set_sensitive (widget, g_value_get_boolean (value));
3233       break;
3234     case PROP_APP_PAINTABLE:
3235       gtk_widget_set_app_paintable (widget, g_value_get_boolean (value));
3236       break;
3237     case PROP_CAN_FOCUS:
3238       gtk_widget_set_can_focus (widget, g_value_get_boolean (value));
3239       break;
3240     case PROP_HAS_FOCUS:
3241       if (g_value_get_boolean (value))
3242         gtk_widget_grab_focus (widget);
3243       break;
3244     case PROP_IS_FOCUS:
3245       if (g_value_get_boolean (value))
3246         gtk_widget_grab_focus (widget);
3247       break;
3248     case PROP_CAN_DEFAULT:
3249       gtk_widget_set_can_default (widget, g_value_get_boolean (value));
3250       break;
3251     case PROP_HAS_DEFAULT:
3252       if (g_value_get_boolean (value))
3253         gtk_widget_grab_default (widget);
3254       break;
3255     case PROP_RECEIVES_DEFAULT:
3256       gtk_widget_set_receives_default (widget, g_value_get_boolean (value));
3257       break;
3258     case PROP_STYLE:
3259       gtk_widget_set_style (widget, g_value_get_object (value));
3260       break;
3261     case PROP_EVENTS:
3262       if (!gtk_widget_get_realized (widget) && gtk_widget_get_has_window (widget))
3263         gtk_widget_set_events (widget, g_value_get_flags (value));
3264       break;
3265     case PROP_NO_SHOW_ALL:
3266       gtk_widget_set_no_show_all (widget, g_value_get_boolean (value));
3267       break;
3268     case PROP_HAS_TOOLTIP:
3269       gtk_widget_real_set_has_tooltip (widget,
3270                                        g_value_get_boolean (value), FALSE);
3271       break;
3272     case PROP_TOOLTIP_MARKUP:
3273       tooltip_window = g_object_get_qdata (object, quark_tooltip_window);
3274       tooltip_markup = g_value_dup_string (value);
3275
3276       /* Treat an empty string as a NULL string,
3277        * because an empty string would be useless for a tooltip:
3278        */
3279       if (tooltip_markup && (strlen (tooltip_markup) == 0))
3280         {
3281           g_free (tooltip_markup);
3282           tooltip_markup = NULL;
3283         }
3284
3285       g_object_set_qdata_full (object, quark_tooltip_markup,
3286                                tooltip_markup, g_free);
3287
3288       tmp = (tooltip_window != NULL || tooltip_markup != NULL);
3289       gtk_widget_real_set_has_tooltip (widget, tmp, FALSE);
3290       if (gtk_widget_get_visible (widget))
3291         gtk_widget_queue_tooltip_query (widget);
3292       break;
3293     case PROP_TOOLTIP_TEXT:
3294       tooltip_window = g_object_get_qdata (object, quark_tooltip_window);
3295
3296       tooltip_text = g_value_get_string (value);
3297
3298       /* Treat an empty string as a NULL string,
3299        * because an empty string would be useless for a tooltip:
3300        */
3301       if (tooltip_text && (strlen (tooltip_text) == 0))
3302         tooltip_text = NULL;
3303
3304       tooltip_markup = tooltip_text ? g_markup_escape_text (tooltip_text, -1) : NULL;
3305
3306       g_object_set_qdata_full (object, quark_tooltip_markup,
3307                                tooltip_markup, g_free);
3308
3309       tmp = (tooltip_window != NULL || tooltip_markup != NULL);
3310       gtk_widget_real_set_has_tooltip (widget, tmp, FALSE);
3311       if (gtk_widget_get_visible (widget))
3312         gtk_widget_queue_tooltip_query (widget);
3313       break;
3314     case PROP_DOUBLE_BUFFERED:
3315       gtk_widget_set_double_buffered (widget, g_value_get_boolean (value));
3316       break;
3317     case PROP_HALIGN:
3318       gtk_widget_set_halign (widget, g_value_get_enum (value));
3319       break;
3320     case PROP_VALIGN:
3321       gtk_widget_set_valign (widget, g_value_get_enum (value));
3322       break;
3323     case PROP_MARGIN_LEFT:
3324       gtk_widget_set_margin_left (widget, g_value_get_int (value));
3325       break;
3326     case PROP_MARGIN_RIGHT:
3327       gtk_widget_set_margin_right (widget, g_value_get_int (value));
3328       break;
3329     case PROP_MARGIN_TOP:
3330       gtk_widget_set_margin_top (widget, g_value_get_int (value));
3331       break;
3332     case PROP_MARGIN_BOTTOM:
3333       gtk_widget_set_margin_bottom (widget, g_value_get_int (value));
3334       break;
3335     case PROP_MARGIN:
3336       g_object_freeze_notify (G_OBJECT (widget));
3337       gtk_widget_set_margin_left (widget, g_value_get_int (value));
3338       gtk_widget_set_margin_right (widget, g_value_get_int (value));
3339       gtk_widget_set_margin_top (widget, g_value_get_int (value));
3340       gtk_widget_set_margin_bottom (widget, g_value_get_int (value));
3341       g_object_thaw_notify (G_OBJECT (widget));
3342       break;
3343     case PROP_HEXPAND:
3344       gtk_widget_set_hexpand (widget, g_value_get_boolean (value));
3345       break;
3346     case PROP_HEXPAND_SET:
3347       gtk_widget_set_hexpand_set (widget, g_value_get_boolean (value));
3348       break;
3349     case PROP_VEXPAND:
3350       gtk_widget_set_vexpand (widget, g_value_get_boolean (value));
3351       break;
3352     case PROP_VEXPAND_SET:
3353       gtk_widget_set_vexpand_set (widget, g_value_get_boolean (value));
3354       break;
3355     case PROP_EXPAND:
3356       g_object_freeze_notify (G_OBJECT (widget));
3357       gtk_widget_set_hexpand (widget, g_value_get_boolean (value));
3358       gtk_widget_set_vexpand (widget, g_value_get_boolean (value));
3359       g_object_thaw_notify (G_OBJECT (widget));
3360       break;
3361     default:
3362       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3363       break;
3364     }
3365 }
3366
3367 static void
3368 gtk_widget_get_property (GObject         *object,
3369                          guint            prop_id,
3370                          GValue          *value,
3371                          GParamSpec      *pspec)
3372 {
3373   GtkWidget *widget = GTK_WIDGET (object);
3374   GtkWidgetPrivate *priv = widget->priv;
3375
3376   switch (prop_id)
3377     {
3378       gpointer *eventp;
3379
3380     case PROP_NAME:
3381       if (priv->name)
3382         g_value_set_string (value, priv->name);
3383       else
3384         g_value_set_static_string (value, "");
3385       break;
3386     case PROP_PARENT:
3387       g_value_set_object (value, priv->parent);
3388       break;
3389     case PROP_WIDTH_REQUEST:
3390       {
3391         int w;
3392         gtk_widget_get_size_request (widget, &w, NULL);
3393         g_value_set_int (value, w);
3394       }
3395       break;
3396     case PROP_HEIGHT_REQUEST:
3397       {
3398         int h;
3399         gtk_widget_get_size_request (widget, NULL, &h);
3400         g_value_set_int (value, h);
3401       }
3402       break;
3403     case PROP_VISIBLE:
3404       g_value_set_boolean (value, (gtk_widget_get_visible (widget) != FALSE));
3405       break;
3406     case PROP_SENSITIVE:
3407       g_value_set_boolean (value, (gtk_widget_get_sensitive (widget) != FALSE));
3408       break;
3409     case PROP_APP_PAINTABLE:
3410       g_value_set_boolean (value, (gtk_widget_get_app_paintable (widget) != FALSE));
3411       break;
3412     case PROP_CAN_FOCUS:
3413       g_value_set_boolean (value, (gtk_widget_get_can_focus (widget) != FALSE));
3414       break;
3415     case PROP_HAS_FOCUS:
3416       g_value_set_boolean (value, (gtk_widget_has_focus (widget) != FALSE));
3417       break;
3418     case PROP_IS_FOCUS:
3419       g_value_set_boolean (value, (gtk_widget_is_focus (widget)));
3420       break;
3421     case PROP_CAN_DEFAULT:
3422       g_value_set_boolean (value, (gtk_widget_get_can_default (widget) != FALSE));
3423       break;
3424     case PROP_HAS_DEFAULT:
3425       g_value_set_boolean (value, (gtk_widget_has_default (widget) != FALSE));
3426       break;
3427     case PROP_RECEIVES_DEFAULT:
3428       g_value_set_boolean (value, (gtk_widget_get_receives_default (widget) != FALSE));
3429       break;
3430     case PROP_COMPOSITE_CHILD:
3431       g_value_set_boolean (value, widget->priv->composite_child);
3432       break;
3433     case PROP_STYLE:
3434       g_value_set_object (value, gtk_widget_get_style (widget));
3435       break;
3436     case PROP_EVENTS:
3437       eventp = g_object_get_qdata (G_OBJECT (widget), quark_event_mask);
3438       g_value_set_flags (value, GPOINTER_TO_INT (eventp));
3439       break;
3440     case PROP_NO_SHOW_ALL:
3441       g_value_set_boolean (value, gtk_widget_get_no_show_all (widget));
3442       break;
3443     case PROP_HAS_TOOLTIP:
3444       g_value_set_boolean (value, GPOINTER_TO_UINT (g_object_get_qdata (object, quark_has_tooltip)));
3445       break;
3446     case PROP_TOOLTIP_TEXT:
3447       {
3448         gchar *escaped = g_object_get_qdata (object, quark_tooltip_markup);
3449         gchar *text = NULL;
3450
3451         if (escaped && !pango_parse_markup (escaped, -1, 0, NULL, &text, NULL, NULL))
3452           g_assert (NULL == text); /* text should still be NULL in case of markup errors */
3453
3454         g_value_take_string (value, text);
3455       }
3456       break;
3457     case PROP_TOOLTIP_MARKUP:
3458       g_value_set_string (value, g_object_get_qdata (object, quark_tooltip_markup));
3459       break;
3460     case PROP_WINDOW:
3461       g_value_set_object (value, gtk_widget_get_window (widget));
3462       break;
3463     case PROP_DOUBLE_BUFFERED:
3464       g_value_set_boolean (value, gtk_widget_get_double_buffered (widget));
3465       break;
3466     case PROP_HALIGN:
3467       g_value_set_enum (value, gtk_widget_get_halign (widget));
3468       break;
3469     case PROP_VALIGN:
3470       g_value_set_enum (value, gtk_widget_get_valign (widget));
3471       break;
3472     case PROP_MARGIN_LEFT:
3473       g_value_set_int (value, gtk_widget_get_margin_left (widget));
3474       break;
3475     case PROP_MARGIN_RIGHT:
3476       g_value_set_int (value, gtk_widget_get_margin_right (widget));
3477       break;
3478     case PROP_MARGIN_TOP:
3479       g_value_set_int (value, gtk_widget_get_margin_top (widget));
3480       break;
3481     case PROP_MARGIN_BOTTOM:
3482       g_value_set_int (value, gtk_widget_get_margin_bottom (widget));
3483       break;
3484     case PROP_MARGIN:
3485       {
3486         GtkWidgetAuxInfo *aux_info = gtk_widget_get_aux_info (widget, FALSE);
3487         if (aux_info == NULL)
3488           {
3489             g_value_set_int (value, 0);
3490           }
3491         else
3492           {
3493             g_value_set_int (value, MAX (MAX (aux_info->margin.left,
3494                                               aux_info->margin.right),
3495                                          MAX (aux_info->margin.top,
3496                                               aux_info->margin.bottom)));
3497           }
3498       }
3499       break;
3500     case PROP_HEXPAND:
3501       g_value_set_boolean (value, gtk_widget_get_hexpand (widget));
3502       break;
3503     case PROP_HEXPAND_SET:
3504       g_value_set_boolean (value, gtk_widget_get_hexpand_set (widget));
3505       break;
3506     case PROP_VEXPAND:
3507       g_value_set_boolean (value, gtk_widget_get_vexpand (widget));
3508       break;
3509     case PROP_VEXPAND_SET:
3510       g_value_set_boolean (value, gtk_widget_get_vexpand_set (widget));
3511       break;
3512     case PROP_EXPAND:
3513       g_value_set_boolean (value,
3514                            gtk_widget_get_hexpand (widget) &&
3515                            gtk_widget_get_vexpand (widget));
3516       break;
3517     default:
3518       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
3519       break;
3520     }
3521 }
3522
3523 static void
3524 gtk_widget_init (GtkWidget *widget)
3525 {
3526   GtkWidgetPrivate *priv;
3527
3528   widget->priv = G_TYPE_INSTANCE_GET_PRIVATE (widget,
3529                                               GTK_TYPE_WIDGET,
3530                                               GtkWidgetPrivate);
3531   priv = widget->priv;
3532
3533   priv->child_visible = TRUE;
3534   priv->name = NULL;
3535   priv->allocation.x = -1;
3536   priv->allocation.y = -1;
3537   priv->allocation.width = 1;
3538   priv->allocation.height = 1;
3539   priv->window = NULL;
3540   priv->parent = NULL;
3541
3542   priv->sensitive = TRUE;
3543   priv->composite_child = composite_child_stack != 0;
3544   priv->double_buffered = TRUE;
3545   priv->redraw_on_alloc = TRUE;
3546   priv->width_request_needed = TRUE;
3547   priv->height_request_needed = TRUE;
3548   priv->alloc_needed = TRUE;
3549
3550   /* this will be set to TRUE if the widget gets a child or if the
3551    * expand flag is set on the widget, but until one of those happen
3552    * we know the expand is already properly FALSE.
3553    *
3554    * We really want to default FALSE here to avoid computing expand
3555    * all over the place while initially building a widget tree.
3556    */
3557   priv->need_compute_expand = FALSE;
3558
3559   priv->style = gtk_widget_get_default_style ();
3560   g_object_ref (priv->style);
3561 }
3562
3563
3564 static void
3565 gtk_widget_dispatch_child_properties_changed (GtkWidget   *widget,
3566                                               guint        n_pspecs,
3567                                               GParamSpec **pspecs)
3568 {
3569   GtkWidgetPrivate *priv = widget->priv;
3570   GtkWidget *container = priv->parent;
3571   guint i;
3572
3573   for (i = 0; widget->priv->parent == container && i < n_pspecs; i++)
3574     g_signal_emit (widget, widget_signals[CHILD_NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
3575 }
3576
3577 /**
3578  * gtk_widget_freeze_child_notify:
3579  * @widget: a #GtkWidget
3580  *
3581  * Stops emission of #GtkWidget::child-notify signals on @widget. The
3582  * signals are queued until gtk_widget_thaw_child_notify() is called
3583  * on @widget.
3584  *
3585  * This is the analogue of g_object_freeze_notify() for child properties.
3586  **/
3587 void
3588 gtk_widget_freeze_child_notify (GtkWidget *widget)
3589 {
3590   g_return_if_fail (GTK_IS_WIDGET (widget));
3591
3592   if (!G_OBJECT (widget)->ref_count)
3593     return;
3594
3595   g_object_ref (widget);
3596   g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
3597   g_object_unref (widget);
3598 }
3599
3600 /**
3601  * gtk_widget_child_notify:
3602  * @widget: a #GtkWidget
3603  * @child_property: the name of a child property installed on the
3604  *                  class of @widget<!-- -->'s parent
3605  *
3606  * Emits a #GtkWidget::child-notify signal for the
3607  * <link linkend="child-properties">child property</link> @child_property
3608  * on @widget.
3609  *
3610  * This is the analogue of g_object_notify() for child properties.
3611  *
3612  * Also see gtk_container_child_notify().
3613  */
3614 void
3615 gtk_widget_child_notify (GtkWidget    *widget,
3616                          const gchar  *child_property)
3617 {
3618   if (widget->priv->parent == NULL)
3619     return;
3620
3621   gtk_container_child_notify (GTK_CONTAINER (widget->priv->parent), widget, child_property);
3622 }
3623
3624 /**
3625  * gtk_widget_thaw_child_notify:
3626  * @widget: a #GtkWidget
3627  *
3628  * Reverts the effect of a previous call to gtk_widget_freeze_child_notify().
3629  * This causes all queued #GtkWidget::child-notify signals on @widget to be
3630  * emitted.
3631  */
3632 void
3633 gtk_widget_thaw_child_notify (GtkWidget *widget)
3634 {
3635   GObjectNotifyQueue *nqueue;
3636
3637   g_return_if_fail (GTK_IS_WIDGET (widget));
3638
3639   if (!G_OBJECT (widget)->ref_count)
3640     return;
3641
3642   g_object_ref (widget);
3643   nqueue = g_object_notify_queue_from_object (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
3644   if (!nqueue || !nqueue->freeze_count)
3645     g_warning (G_STRLOC ": child-property-changed notification for %s(%p) is not frozen",
3646                G_OBJECT_TYPE_NAME (widget), widget);
3647   else
3648     g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
3649   g_object_unref (widget);
3650 }
3651
3652
3653 /**
3654  * gtk_widget_new:
3655  * @type: type ID of the widget to create
3656  * @first_property_name: name of first property to set
3657  * @Varargs: value of first property, followed by more properties,
3658  *           %NULL-terminated
3659  *
3660  * This is a convenience function for creating a widget and setting
3661  * its properties in one go. For example you might write:
3662  * <literal>gtk_widget_new (GTK_TYPE_LABEL, "label", "Hello World", "xalign",
3663  * 0.0, NULL)</literal> to create a left-aligned label. Equivalent to
3664  * g_object_new(), but returns a widget so you don't have to
3665  * cast the object yourself.
3666  *
3667  * Return value: a new #GtkWidget of type @widget_type
3668  **/
3669 GtkWidget*
3670 gtk_widget_new (GType        type,
3671                 const gchar *first_property_name,
3672                 ...)
3673 {
3674   GtkWidget *widget;
3675   va_list var_args;
3676
3677   g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), NULL);
3678
3679   va_start (var_args, first_property_name);
3680   widget = (GtkWidget *)g_object_new_valist (type, first_property_name, var_args);
3681   va_end (var_args);
3682
3683   return widget;
3684 }
3685
3686 static inline void
3687 gtk_widget_queue_draw_child (GtkWidget *widget)
3688 {
3689   GtkWidgetPrivate *priv = widget->priv;
3690   GtkWidget *parent;
3691
3692   parent = priv->parent;
3693   if (parent && gtk_widget_is_drawable (parent))
3694     gtk_widget_queue_draw_area (parent,
3695                                 priv->allocation.x,
3696                                 priv->allocation.y,
3697                                 priv->allocation.width,
3698                                 priv->allocation.height);
3699 }
3700
3701 /**
3702  * gtk_widget_unparent:
3703  * @widget: a #GtkWidget
3704  *
3705  * This function is only for use in widget implementations.
3706  * Should be called by implementations of the remove method
3707  * on #GtkContainer, to dissociate a child from the container.
3708  **/
3709 void
3710 gtk_widget_unparent (GtkWidget *widget)
3711 {
3712   GtkWidgetPrivate *priv;
3713   GObjectNotifyQueue *nqueue;
3714   GtkWidget *toplevel;
3715   GtkWidget *old_parent;
3716
3717   g_return_if_fail (GTK_IS_WIDGET (widget));
3718
3719   priv = widget->priv;
3720
3721   if (priv->parent == NULL)
3722     return;
3723
3724   /* keep this function in sync with gtk_menu_detach() */
3725
3726   gtk_widget_push_verify_invariants (widget);
3727
3728   g_object_freeze_notify (G_OBJECT (widget));
3729   nqueue = g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
3730
3731   toplevel = gtk_widget_get_toplevel (widget);
3732   if (gtk_widget_is_toplevel (toplevel))
3733     _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
3734
3735   if (gtk_container_get_focus_child (GTK_CONTAINER (priv->parent)) == widget)
3736     gtk_container_set_focus_child (GTK_CONTAINER (priv->parent), NULL);
3737
3738   gtk_widget_queue_draw_child (widget);
3739
3740   /* Reset the width and height here, to force reallocation if we
3741    * get added back to a new parent. This won't work if our new
3742    * allocation is smaller than 1x1 and we actually want a size of 1x1...
3743    * (would 0x0 be OK here?)
3744    */
3745   priv->allocation.width = 1;
3746   priv->allocation.height = 1;
3747
3748   if (gtk_widget_get_realized (widget))
3749     {
3750       if (priv->in_reparent)
3751         gtk_widget_unmap (widget);
3752       else
3753         gtk_widget_unrealize (widget);
3754     }
3755
3756   /* If we are unanchoring the child, we save around the toplevel
3757    * to emit hierarchy changed
3758    */
3759   if (priv->parent->priv->anchored)
3760     g_object_ref (toplevel);
3761   else
3762     toplevel = NULL;
3763
3764   /* Removing a widget from a container restores the child visible
3765    * flag to the default state, so it doesn't affect the child
3766    * in the next parent.
3767    */
3768   priv->child_visible = TRUE;
3769
3770   old_parent = priv->parent;
3771   priv->parent = NULL;
3772
3773   /* parent may no longer expand if the removed
3774    * child was expand=TRUE and could therefore
3775    * be forcing it to.
3776    */
3777   if (gtk_widget_get_visible (widget) &&
3778       (priv->need_compute_expand ||
3779        priv->computed_hexpand ||
3780        priv->computed_vexpand))
3781     {
3782       gtk_widget_queue_compute_expand (old_parent);
3783     }
3784
3785   g_signal_emit (widget, widget_signals[PARENT_SET], 0, old_parent);
3786   if (toplevel)
3787     {
3788       _gtk_widget_propagate_hierarchy_changed (widget, toplevel);
3789       g_object_unref (toplevel);
3790     }
3791
3792   /* Now that the parent pointer is nullified and the hierarchy-changed
3793    * already passed, go ahead and unset the parent window, if we are unparenting
3794    * an embeded GtkWindow the window will become toplevel again and hierarchy-changed
3795    * will fire again for the new subhierarchy.
3796    */
3797   gtk_widget_set_parent_window (widget, NULL);
3798
3799   g_object_notify (G_OBJECT (widget), "parent");
3800   g_object_thaw_notify (G_OBJECT (widget));
3801   if (!priv->parent)
3802     g_object_notify_queue_clear (G_OBJECT (widget), nqueue);
3803   g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
3804
3805   gtk_widget_pop_verify_invariants (widget);
3806   g_object_unref (widget);
3807 }
3808
3809 /**
3810  * gtk_widget_destroy:
3811  * @widget: a #GtkWidget
3812  *
3813  * Destroys a widget.
3814  *
3815  * When a widget is
3816  * destroyed, it will break any references it holds to other objects.
3817  * If the widget is inside a container, the widget will be removed
3818  * from the container. If the widget is a toplevel (derived from
3819  * #GtkWindow), it will be removed from the list of toplevels, and the
3820  * reference GTK+ holds to it will be removed. Removing a
3821  * widget from its container or the list of toplevels results in the
3822  * widget being finalized, unless you've added additional references
3823  * to the widget with g_object_ref().
3824  *
3825  * In most cases, only toplevel widgets (windows) require explicit
3826  * destruction, because when you destroy a toplevel its children will
3827  * be destroyed as well.
3828  **/
3829 void
3830 gtk_widget_destroy (GtkWidget *widget)
3831 {
3832   g_return_if_fail (GTK_IS_WIDGET (widget));
3833
3834   if (!widget->priv->in_destruction)
3835     g_object_run_dispose (G_OBJECT (widget));
3836 }
3837
3838 /**
3839  * gtk_widget_destroyed:
3840  * @widget: a #GtkWidget
3841  * @widget_pointer: (inout) (transfer none): address of a variable that contains @widget
3842  *
3843  * This function sets *@widget_pointer to %NULL if @widget_pointer !=
3844  * %NULL.  It's intended to be used as a callback connected to the
3845  * "destroy" signal of a widget. You connect gtk_widget_destroyed()
3846  * as a signal handler, and pass the address of your widget variable
3847  * as user data. Then when the widget is destroyed, the variable will
3848  * be set to %NULL. Useful for example to avoid multiple copies
3849  * of the same dialog.
3850  **/
3851 void
3852 gtk_widget_destroyed (GtkWidget      *widget,
3853                       GtkWidget      **widget_pointer)
3854 {
3855   /* Don't make any assumptions about the
3856    *  value of widget!
3857    *  Even check widget_pointer.
3858    */
3859   if (widget_pointer)
3860     *widget_pointer = NULL;
3861 }
3862
3863 /**
3864  * gtk_widget_show:
3865  * @widget: a #GtkWidget
3866  *
3867  * Flags a widget to be displayed. Any widget that isn't shown will
3868  * not appear on the screen. If you want to show all the widgets in a
3869  * container, it's easier to call gtk_widget_show_all() on the
3870  * container, instead of individually showing the widgets.
3871  *
3872  * Remember that you have to show the containers containing a widget,
3873  * in addition to the widget itself, before it will appear onscreen.
3874  *
3875  * When a toplevel container is shown, it is immediately realized and
3876  * mapped; other shown widgets are realized and mapped when their
3877  * toplevel container is realized and mapped.
3878  **/
3879 void
3880 gtk_widget_show (GtkWidget *widget)
3881 {
3882   g_return_if_fail (GTK_IS_WIDGET (widget));
3883
3884   if (!gtk_widget_get_visible (widget))
3885     {
3886       g_object_ref (widget);
3887       gtk_widget_push_verify_invariants (widget);
3888
3889       if (!gtk_widget_is_toplevel (widget))
3890         gtk_widget_queue_resize (widget);
3891
3892       /* see comment in set_parent() for why this should and can be
3893        * conditional
3894        */
3895       if (widget->priv->need_compute_expand ||
3896           widget->priv->computed_hexpand ||
3897           widget->priv->computed_vexpand)
3898         {
3899           if (widget->priv->parent != NULL)
3900             gtk_widget_queue_compute_expand (widget->priv->parent);
3901         }
3902
3903       g_signal_emit (widget, widget_signals[SHOW], 0);
3904       g_object_notify (G_OBJECT (widget), "visible");
3905
3906       gtk_widget_pop_verify_invariants (widget);
3907       g_object_unref (widget);
3908     }
3909 }
3910
3911 static void
3912 gtk_widget_real_show (GtkWidget *widget)
3913 {
3914   GtkWidgetPrivate *priv = widget->priv;
3915
3916   if (!gtk_widget_get_visible (widget))
3917     {
3918       priv->visible = TRUE;
3919
3920       if (priv->parent &&
3921           gtk_widget_get_mapped (priv->parent) &&
3922           gtk_widget_get_child_visible (widget) &&
3923           !gtk_widget_get_mapped (widget))
3924         gtk_widget_map (widget);
3925     }
3926 }
3927
3928 static void
3929 gtk_widget_show_map_callback (GtkWidget *widget, GdkEvent *event, gint *flag)
3930 {
3931   *flag = TRUE;
3932   g_signal_handlers_disconnect_by_func (widget,
3933                                         gtk_widget_show_map_callback,
3934                                         flag);
3935 }
3936
3937 /**
3938  * gtk_widget_show_now:
3939  * @widget: a #GtkWidget
3940  *
3941  * Shows a widget. If the widget is an unmapped toplevel widget
3942  * (i.e. a #GtkWindow that has not yet been shown), enter the main
3943  * loop and wait for the window to actually be mapped. Be careful;
3944  * because the main loop is running, anything can happen during
3945  * this function.
3946  **/
3947 void
3948 gtk_widget_show_now (GtkWidget *widget)
3949 {
3950   gint flag = FALSE;
3951
3952   g_return_if_fail (GTK_IS_WIDGET (widget));
3953
3954   /* make sure we will get event */
3955   if (!gtk_widget_get_mapped (widget) &&
3956       gtk_widget_is_toplevel (widget))
3957     {
3958       gtk_widget_show (widget);
3959
3960       g_signal_connect (widget, "map-event",
3961                         G_CALLBACK (gtk_widget_show_map_callback),
3962                         &flag);
3963
3964       while (!flag)
3965         gtk_main_iteration ();
3966     }
3967   else
3968     gtk_widget_show (widget);
3969 }
3970
3971 /**
3972  * gtk_widget_hide:
3973  * @widget: a #GtkWidget
3974  *
3975  * Reverses the effects of gtk_widget_show(), causing the widget to be
3976  * hidden (invisible to the user).
3977  **/
3978 void
3979 gtk_widget_hide (GtkWidget *widget)
3980 {
3981   g_return_if_fail (GTK_IS_WIDGET (widget));
3982
3983   if (gtk_widget_get_visible (widget))
3984     {
3985       GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
3986
3987       g_object_ref (widget);
3988       gtk_widget_push_verify_invariants (widget);
3989
3990       if (toplevel != widget && gtk_widget_is_toplevel (toplevel))
3991         _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
3992
3993       /* a parent may now be expand=FALSE since we're hidden. */
3994       if (widget->priv->need_compute_expand ||
3995           widget->priv->computed_hexpand ||
3996           widget->priv->computed_vexpand)
3997         {
3998           gtk_widget_queue_compute_expand (widget);
3999         }
4000
4001       g_signal_emit (widget, widget_signals[HIDE], 0);
4002       if (!gtk_widget_is_toplevel (widget))
4003         gtk_widget_queue_resize (widget);
4004       g_object_notify (G_OBJECT (widget), "visible");
4005
4006       gtk_widget_pop_verify_invariants (widget);
4007       g_object_unref (widget);
4008     }
4009 }
4010
4011 static void
4012 gtk_widget_real_hide (GtkWidget *widget)
4013 {
4014   if (gtk_widget_get_visible (widget))
4015     {
4016       widget->priv->visible = FALSE;
4017
4018       if (gtk_widget_get_mapped (widget))
4019         gtk_widget_unmap (widget);
4020     }
4021 }
4022
4023 /**
4024  * gtk_widget_hide_on_delete:
4025  * @widget: a #GtkWidget
4026  *
4027  * Utility function; intended to be connected to the #GtkWidget::delete-event
4028  * signal on a #GtkWindow. The function calls gtk_widget_hide() on its
4029  * argument, then returns %TRUE. If connected to ::delete-event, the
4030  * result is that clicking the close button for a window (on the
4031  * window frame, top right corner usually) will hide but not destroy
4032  * the window. By default, GTK+ destroys windows when ::delete-event
4033  * is received.
4034  *
4035  * Return value: %TRUE
4036  **/
4037 gboolean
4038 gtk_widget_hide_on_delete (GtkWidget *widget)
4039 {
4040   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4041
4042   gtk_widget_hide (widget);
4043
4044   return TRUE;
4045 }
4046
4047 /**
4048  * gtk_widget_show_all:
4049  * @widget: a #GtkWidget
4050  *
4051  * Recursively shows a widget, and any child widgets (if the widget is
4052  * a container).
4053  **/
4054 void
4055 gtk_widget_show_all (GtkWidget *widget)
4056 {
4057   GtkWidgetClass *class;
4058
4059   g_return_if_fail (GTK_IS_WIDGET (widget));
4060
4061   if (gtk_widget_get_no_show_all (widget))
4062     return;
4063
4064   class = GTK_WIDGET_GET_CLASS (widget);
4065
4066   if (class->show_all)
4067     class->show_all (widget);
4068 }
4069
4070 static void
4071 _gtk_widget_notify_state_change (GtkWidget     *widget,
4072                                  GtkStateFlags  flag,
4073                                  gboolean       target)
4074 {
4075   GtkStateType state;
4076
4077   switch (flag)
4078     {
4079     case GTK_STATE_FLAG_ACTIVE:
4080       state = GTK_STATE_ACTIVE;
4081       break;
4082     case GTK_STATE_FLAG_PRELIGHT:
4083       state = GTK_STATE_PRELIGHT;
4084       break;
4085     case GTK_STATE_FLAG_SELECTED:
4086       state = GTK_STATE_SELECTED;
4087       break;
4088     case GTK_STATE_FLAG_INSENSITIVE:
4089       state = GTK_STATE_INSENSITIVE;
4090       break;
4091     case GTK_STATE_FLAG_INCONSISTENT:
4092       state = GTK_STATE_INCONSISTENT;
4093       break;
4094     case GTK_STATE_FLAG_FOCUSED:
4095       state = GTK_STATE_FOCUSED;
4096       break;
4097     default:
4098       return;
4099     }
4100
4101   gtk_style_context_notify_state_change (widget->priv->context,
4102                                          gtk_widget_get_window (widget),
4103                                          NULL, state, target);
4104 }
4105
4106 /* Initializes state transitions for those states that
4107  * were enabled before mapping and have a looping animation.
4108  */
4109 static void
4110 _gtk_widget_start_state_transitions (GtkWidget *widget)
4111 {
4112   GtkStateFlags state, flag;
4113
4114   if (!widget->priv->context)
4115     return;
4116
4117   state = gtk_widget_get_state_flags (widget);
4118   flag = GTK_STATE_FLAG_FOCUSED;
4119
4120   while (flag)
4121     {
4122       GtkAnimationDescription *animation_desc;
4123
4124       if ((state & flag) == 0)
4125         {
4126           flag >>= 1;
4127           continue;
4128         }
4129
4130       gtk_style_context_get (widget->priv->context, state,
4131                              "transition", &animation_desc,
4132                              NULL);
4133
4134       if (animation_desc)
4135         {
4136           if (_gtk_animation_description_get_loop (animation_desc))
4137             _gtk_widget_notify_state_change (widget, flag, TRUE);
4138
4139           _gtk_animation_description_unref (animation_desc);
4140         }
4141
4142       flag >>= 1;
4143     }
4144 }
4145
4146 /**
4147  * gtk_widget_map:
4148  * @widget: a #GtkWidget
4149  *
4150  * This function is only for use in widget implementations. Causes
4151  * a widget to be mapped if it isn't already.
4152  **/
4153 void
4154 gtk_widget_map (GtkWidget *widget)
4155 {
4156   GtkWidgetPrivate *priv;
4157
4158   g_return_if_fail (GTK_IS_WIDGET (widget));
4159   g_return_if_fail (gtk_widget_get_visible (widget));
4160   g_return_if_fail (gtk_widget_get_child_visible (widget));
4161
4162   priv = widget->priv;
4163
4164   if (!gtk_widget_get_mapped (widget))
4165     {
4166       gtk_widget_push_verify_invariants (widget);
4167
4168       if (!gtk_widget_get_realized (widget))
4169         gtk_widget_realize (widget);
4170
4171       g_signal_emit (widget, widget_signals[MAP], 0);
4172
4173       if (!gtk_widget_get_has_window (widget))
4174         gdk_window_invalidate_rect (priv->window, &priv->allocation, FALSE);
4175
4176       gtk_widget_pop_verify_invariants (widget);
4177
4178       _gtk_widget_start_state_transitions (widget);
4179     }
4180 }
4181
4182 /**
4183  * gtk_widget_unmap:
4184  * @widget: a #GtkWidget
4185  *
4186  * This function is only for use in widget implementations. Causes
4187  * a widget to be unmapped if it's currently mapped.
4188  **/
4189 void
4190 gtk_widget_unmap (GtkWidget *widget)
4191 {
4192   GtkWidgetPrivate *priv;
4193
4194   g_return_if_fail (GTK_IS_WIDGET (widget));
4195
4196   priv = widget->priv;
4197
4198   if (gtk_widget_get_mapped (widget))
4199     {
4200       gtk_widget_push_verify_invariants (widget);
4201
4202       if (!gtk_widget_get_has_window (widget))
4203         gdk_window_invalidate_rect (priv->window, &priv->allocation, FALSE);
4204       _gtk_tooltip_hide (widget);
4205       g_signal_emit (widget, widget_signals[UNMAP], 0);
4206
4207       gtk_widget_pop_verify_invariants (widget);
4208
4209       if (priv->context)
4210         gtk_style_context_cancel_animations (priv->context, NULL);
4211
4212       /* Unset pointer/window info */
4213       g_object_set_qdata (G_OBJECT (widget), quark_pointer_window, NULL);
4214     }
4215 }
4216
4217 static void
4218 _gtk_widget_enable_device_events (GtkWidget *widget)
4219 {
4220   GHashTable *device_events;
4221   GHashTableIter iter;
4222   gpointer key, value;
4223
4224   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
4225
4226   if (!device_events)
4227     return;
4228
4229   g_hash_table_iter_init (&iter, device_events);
4230
4231   while (g_hash_table_iter_next (&iter, &key, &value))
4232     {
4233       GdkDevice *device;
4234       GdkEventMask event_mask;
4235
4236       device = key;
4237       event_mask = GPOINTER_TO_UINT (value);
4238       gtk_widget_add_events_internal (widget, device, event_mask);
4239     }
4240 }
4241
4242 static GList *
4243 get_widget_windows (GtkWidget *widget)
4244 {
4245   GList *window_list, *last, *l, *children, *ret;
4246
4247   if (gtk_widget_get_has_window (widget))
4248     window_list = g_list_prepend (NULL, gtk_widget_get_window (widget));
4249   else
4250     window_list = gdk_window_peek_children (gtk_widget_get_window (widget));
4251
4252   last = g_list_last (window_list);
4253   ret = NULL;
4254
4255   for (l = window_list; l; l = l->next)
4256     {
4257       GtkWidget *window_widget = NULL;
4258
4259       gdk_window_get_user_data (l->data, (gpointer *) &window_widget);
4260
4261       if (widget != window_widget)
4262         continue;
4263
4264       ret = g_list_prepend (ret, l->data);
4265       children = gdk_window_peek_children (GDK_WINDOW (l->data));
4266
4267       if (children)
4268         {
4269           last = g_list_concat (last, children);
4270           last = g_list_last (last);
4271         }
4272     }
4273
4274   g_list_free (window_list);
4275
4276   return ret;
4277 }
4278
4279 static void
4280 device_enable_foreach (GtkWidget *widget,
4281                        gpointer   user_data)
4282 {
4283   GdkDevice *device = user_data;
4284   gtk_widget_set_device_enabled_internal (widget, device, TRUE, TRUE);
4285 }
4286
4287 static void
4288 device_disable_foreach (GtkWidget *widget,
4289                         gpointer   user_data)
4290 {
4291   GdkDevice *device = user_data;
4292   gtk_widget_set_device_enabled_internal (widget, device, TRUE, FALSE);
4293 }
4294
4295 static void
4296 gtk_widget_set_device_enabled_internal (GtkWidget *widget,
4297                                         GdkDevice *device,
4298                                         gboolean   recurse,
4299                                         gboolean   enabled)
4300 {
4301   GList *window_list, *l;
4302
4303   window_list = get_widget_windows (widget);
4304
4305   for (l = window_list; l; l = l->next)
4306     {
4307       GdkEventMask events = 0;
4308       GdkWindow *window;
4309
4310       window = l->data;
4311
4312       if (enabled)
4313         events = gdk_window_get_events (window);
4314
4315       gdk_window_set_device_events (window, device, events);
4316     }
4317
4318   if (recurse && GTK_IS_CONTAINER (widget))
4319     {
4320       if (enabled)
4321         gtk_container_forall (GTK_CONTAINER (widget), device_enable_foreach, device);
4322       else
4323         gtk_container_forall (GTK_CONTAINER (widget), device_disable_foreach, device);
4324     }
4325
4326   g_list_free (window_list);
4327 }
4328
4329 static void
4330 gtk_widget_update_devices_mask (GtkWidget *widget,
4331                                 gboolean   recurse)
4332 {
4333   GList *enabled_devices, *l;
4334
4335   enabled_devices = g_object_get_qdata (G_OBJECT (widget), quark_enabled_devices);
4336
4337   for (l = enabled_devices; l; l = l->next)
4338     gtk_widget_set_device_enabled_internal (widget, GDK_DEVICE (l->data), recurse, TRUE);
4339 }
4340
4341 /**
4342  * gtk_widget_realize:
4343  * @widget: a #GtkWidget
4344  *
4345  * Creates the GDK (windowing system) resources associated with a
4346  * widget.  For example, @widget->window will be created when a widget
4347  * is realized.  Normally realization happens implicitly; if you show
4348  * a widget and all its parent containers, then the widget will be
4349  * realized and mapped automatically.
4350  *
4351  * Realizing a widget requires all
4352  * the widget's parent widgets to be realized; calling
4353  * gtk_widget_realize() realizes the widget's parents in addition to
4354  * @widget itself. If a widget is not yet inside a toplevel window
4355  * when you realize it, bad things will happen.
4356  *
4357  * This function is primarily used in widget implementations, and
4358  * isn't very useful otherwise. Many times when you think you might
4359  * need it, a better approach is to connect to a signal that will be
4360  * called after the widget is realized automatically, such as
4361  * #GtkWidget::draw. Or simply g_signal_connect () to the
4362  * #GtkWidget::realize signal.
4363  **/
4364 void
4365 gtk_widget_realize (GtkWidget *widget)
4366 {
4367   GtkWidgetPrivate *priv;
4368   cairo_region_t *region;
4369
4370   g_return_if_fail (GTK_IS_WIDGET (widget));
4371   g_return_if_fail (widget->priv->anchored ||
4372                     GTK_IS_INVISIBLE (widget));
4373
4374   priv = widget->priv;
4375
4376   if (!gtk_widget_get_realized (widget))
4377     {
4378       gtk_widget_push_verify_invariants (widget);
4379
4380       /*
4381         if (GTK_IS_CONTAINER (widget) && gtk_widget_get_has_window (widget))
4382           g_message ("gtk_widget_realize(%s)", G_OBJECT_TYPE_NAME (widget));
4383       */
4384
4385       if (priv->parent == NULL &&
4386           !gtk_widget_is_toplevel (widget))
4387         g_warning ("Calling gtk_widget_realize() on a widget that isn't "
4388                    "inside a toplevel window is not going to work very well. "
4389                    "Widgets must be inside a toplevel container before realizing them.");
4390
4391       if (priv->parent && !gtk_widget_get_realized (priv->parent))
4392         gtk_widget_realize (priv->parent);
4393
4394       gtk_widget_ensure_style (widget);
4395
4396       if (priv->style_update_pending)
4397         g_signal_emit (widget, widget_signals[STYLE_UPDATED], 0);
4398
4399       g_signal_emit (widget, widget_signals[REALIZE], 0);
4400
4401       gtk_widget_real_set_has_tooltip (widget,
4402                                        GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget), quark_has_tooltip)),
4403                                        TRUE);
4404
4405       if (priv->has_shape_mask)
4406         {
4407           region = g_object_get_qdata (G_OBJECT (widget), quark_shape_info);
4408           gdk_window_shape_combine_region (priv->window, region, 0, 0);
4409         }
4410
4411       region = g_object_get_qdata (G_OBJECT (widget), quark_input_shape_info);
4412       if (region)
4413         gdk_window_input_shape_combine_region (priv->window, region, 0, 0);
4414
4415       if (priv->multidevice)
4416         gdk_window_set_support_multidevice (priv->window, TRUE);
4417
4418       _gtk_widget_enable_device_events (widget);
4419       gtk_widget_update_devices_mask (widget, TRUE);
4420
4421       gtk_widget_pop_verify_invariants (widget);
4422     }
4423 }
4424
4425 /**
4426  * gtk_widget_unrealize:
4427  * @widget: a #GtkWidget
4428  *
4429  * This function is only useful in widget implementations.
4430  * Causes a widget to be unrealized (frees all GDK resources
4431  * associated with the widget, such as @widget->window).
4432  **/
4433 void
4434 gtk_widget_unrealize (GtkWidget *widget)
4435 {
4436   g_return_if_fail (GTK_IS_WIDGET (widget));
4437
4438   gtk_widget_push_verify_invariants (widget);
4439
4440   if (widget->priv->has_shape_mask)
4441     gtk_widget_shape_combine_region (widget, NULL);
4442
4443   if (g_object_get_qdata (G_OBJECT (widget), quark_input_shape_info))
4444     gtk_widget_input_shape_combine_region (widget, NULL);
4445
4446   if (gtk_widget_get_realized (widget))
4447     {
4448       g_object_ref (widget);
4449
4450       if (widget->priv->mapped)
4451         gtk_widget_unmap (widget);
4452
4453       g_signal_emit (widget, widget_signals[UNREALIZE], 0);
4454       g_assert (!widget->priv->mapped);
4455       gtk_widget_set_realized (widget, FALSE);
4456
4457       g_object_unref (widget);
4458     }
4459
4460   gtk_widget_pop_verify_invariants (widget);
4461 }
4462
4463 /*****************************************
4464  * Draw queueing.
4465  *****************************************/
4466
4467 /**
4468  * gtk_widget_queue_draw_region:
4469  * @widget: a #GtkWidget
4470  * @region: region to draw
4471  *
4472  * Invalidates the rectangular area of @widget defined by @region by
4473  * calling gdk_window_invalidate_region() on the widget's window and
4474  * all its child windows. Once the main loop becomes idle (after the
4475  * current batch of events has been processed, roughly), the window
4476  * will receive expose events for the union of all regions that have
4477  * been invalidated.
4478  *
4479  * Normally you would only use this function in widget
4480  * implementations. You might also use it to schedule a redraw of a
4481  * #GtkDrawingArea or some portion thereof.
4482  *
4483  * Since: 3.0
4484  **/
4485 void
4486 gtk_widget_queue_draw_region (GtkWidget            *widget,
4487                               const cairo_region_t *region)
4488 {
4489   GtkWidgetPrivate *priv;
4490   GtkWidget *w;
4491
4492   g_return_if_fail (GTK_IS_WIDGET (widget));
4493
4494   priv = widget->priv;
4495
4496   if (!gtk_widget_get_realized (widget))
4497     return;
4498
4499   /* Just return if the widget or one of its ancestors isn't mapped */
4500   for (w = widget; w != NULL; w = w->priv->parent)
4501     if (!gtk_widget_get_mapped (w))
4502       return;
4503
4504   gdk_window_invalidate_region (priv->window, region, TRUE);
4505 }
4506
4507 /**
4508  * gtk_widget_queue_draw_area:
4509  * @widget: a #GtkWidget
4510  * @x: x coordinate of upper-left corner of rectangle to redraw
4511  * @y: y coordinate of upper-left corner of rectangle to redraw
4512  * @width: width of region to draw
4513  * @height: height of region to draw
4514  *
4515  * Convenience function that calls gtk_widget_queue_draw_region() on
4516  * the region created from the given coordinates.
4517  *
4518  * The region here is specified in widget coordinates.
4519  * Widget coordinates are a bit odd; for historical reasons, they are
4520  * defined as @widget->window coordinates for widgets that are not
4521  * #GTK_NO_WINDOW widgets, and are relative to @widget->allocation.x,
4522  * @widget->allocation.y for widgets that are #GTK_NO_WINDOW widgets.
4523  */
4524 void
4525 gtk_widget_queue_draw_area (GtkWidget *widget,
4526                             gint       x,
4527                             gint       y,
4528                             gint       width,
4529                             gint       height)
4530 {
4531   GdkRectangle rect;
4532   cairo_region_t *region;
4533
4534   g_return_if_fail (GTK_IS_WIDGET (widget));
4535
4536   rect.x = x;
4537   rect.y = y;
4538   rect.width = width;
4539   rect.height = height;
4540
4541   region = cairo_region_create_rectangle (&rect);
4542   gtk_widget_queue_draw_region (widget, region);
4543   cairo_region_destroy (region);
4544 }
4545
4546 /**
4547  * gtk_widget_queue_draw:
4548  * @widget: a #GtkWidget
4549  *
4550  * Equivalent to calling gtk_widget_queue_draw_area() for the
4551  * entire area of a widget.
4552  **/
4553 void
4554 gtk_widget_queue_draw (GtkWidget *widget)
4555 {
4556   GdkRectangle rect;
4557
4558   g_return_if_fail (GTK_IS_WIDGET (widget));
4559
4560   gtk_widget_get_allocation (widget, &rect);
4561
4562   if (!gtk_widget_get_has_window (widget))
4563     gtk_widget_queue_draw_area (widget,
4564                                 rect.x, rect.y, rect.width, rect.height);
4565   else
4566     gtk_widget_queue_draw_area (widget,
4567                                 0, 0, rect.width, rect.height);
4568 }
4569
4570 /**
4571  * gtk_widget_queue_resize:
4572  * @widget: a #GtkWidget
4573  *
4574  * This function is only for use in widget implementations.
4575  * Flags a widget to have its size renegotiated; should
4576  * be called when a widget for some reason has a new size request.
4577  * For example, when you change the text in a #GtkLabel, #GtkLabel
4578  * queues a resize to ensure there's enough space for the new text.
4579  *
4580  * <note><para>You cannot call gtk_widget_queue_resize() on a widget
4581  * from inside its implementation of the GtkWidgetClass::size_allocate 
4582  * virtual method. Calls to gtk_widget_queue_resize() from inside
4583  * GtkWidgetClass::size_allocate will be silently ignored.</para></note>
4584  **/
4585 void
4586 gtk_widget_queue_resize (GtkWidget *widget)
4587 {
4588   g_return_if_fail (GTK_IS_WIDGET (widget));
4589
4590   if (gtk_widget_get_realized (widget))
4591     gtk_widget_queue_shallow_draw (widget);
4592
4593   _gtk_size_group_queue_resize (widget, 0);
4594 }
4595
4596 /**
4597  * gtk_widget_queue_resize_no_redraw:
4598  * @widget: a #GtkWidget
4599  *
4600  * This function works like gtk_widget_queue_resize(),
4601  * except that the widget is not invalidated.
4602  *
4603  * Since: 2.4
4604  **/
4605 void
4606 gtk_widget_queue_resize_no_redraw (GtkWidget *widget)
4607 {
4608   g_return_if_fail (GTK_IS_WIDGET (widget));
4609
4610   _gtk_size_group_queue_resize (widget, 0);
4611 }
4612
4613 /**
4614  * gtk_widget_size_request:
4615  * @widget: a #GtkWidget
4616  * @requisition: (out): a #GtkRequisition to be filled in
4617  *
4618  * This function is typically used when implementing a #GtkContainer
4619  * subclass.  Obtains the preferred size of a widget. The container
4620  * uses this information to arrange its child widgets and decide what
4621  * size allocations to give them with gtk_widget_size_allocate().
4622  *
4623  * You can also call this function from an application, with some
4624  * caveats. Most notably, getting a size request requires the widget
4625  * to be associated with a screen, because font information may be
4626  * needed. Multihead-aware applications should keep this in mind.
4627  *
4628  * Also remember that the size request is not necessarily the size
4629  * a widget will actually be allocated.
4630  *
4631  * Deprecated: 3.0: Use gtk_widget_get_preferred_size() instead.
4632  **/
4633 void
4634 gtk_widget_size_request (GtkWidget      *widget,
4635                          GtkRequisition *requisition)
4636 {
4637   g_return_if_fail (GTK_IS_WIDGET (widget));
4638
4639   gtk_widget_get_preferred_size (widget, requisition, NULL);
4640 }
4641
4642 /**
4643  * gtk_widget_get_child_requisition:
4644  * @widget: a #GtkWidget
4645  * @requisition: (out): a #GtkRequisition to be filled in
4646  *
4647  * This function is only for use in widget implementations. Obtains
4648  * @widget->requisition, unless someone has forced a particular
4649  * geometry on the widget (e.g. with gtk_widget_set_size_request()),
4650  * in which case it returns that geometry instead of the widget's
4651  * requisition.
4652  *
4653  * This function differs from gtk_widget_size_request() in that
4654  * it retrieves the last size request value from @widget->requisition,
4655  * while gtk_widget_size_request() actually calls the "size_request" method
4656  * on @widget to compute the size request and fill in @widget->requisition,
4657  * and only then returns @widget->requisition.
4658  *
4659  * Because this function does not call the "size_request" method, it
4660  * can only be used when you know that @widget->requisition is
4661  * up-to-date, that is, gtk_widget_size_request() has been called
4662  * since the last time a resize was queued. In general, only container
4663  * implementations have this information; applications should use
4664  * gtk_widget_size_request().
4665  *
4666  *
4667  * Deprecated: 3.0: Use gtk_widget_get_preferred_size() instead.
4668  **/
4669 void
4670 gtk_widget_get_child_requisition (GtkWidget      *widget,
4671                                   GtkRequisition *requisition)
4672 {
4673   gtk_widget_get_preferred_size (widget, requisition, NULL);
4674 }
4675
4676 static gboolean
4677 invalidate_predicate (GdkWindow *window,
4678                       gpointer   data)
4679 {
4680   gpointer user_data;
4681
4682   gdk_window_get_user_data (window, &user_data);
4683
4684   return (user_data == data);
4685 }
4686
4687 /* Invalidate @region in widget->window and all children
4688  * of widget->window owned by widget. @region is in the
4689  * same coordinates as widget->allocation and will be
4690  * modified by this call.
4691  */
4692 static void
4693 gtk_widget_invalidate_widget_windows (GtkWidget *widget,
4694                                       cairo_region_t *region)
4695 {
4696   GtkWidgetPrivate *priv = widget->priv;
4697
4698   if (!gtk_widget_get_realized (widget))
4699     return;
4700
4701   if (gtk_widget_get_has_window (widget) && priv->parent)
4702     {
4703       int x, y;
4704
4705       gdk_window_get_position (priv->window, &x, &y);
4706       cairo_region_translate (region, -x, -y);
4707     }
4708
4709   gdk_window_invalidate_maybe_recurse (priv->window, region,
4710                                        invalidate_predicate, widget);
4711 }
4712
4713 /**
4714  * gtk_widget_queue_shallow_draw:
4715  * @widget: a #GtkWidget
4716  *
4717  * Like gtk_widget_queue_draw(), but only windows owned
4718  * by @widget are invalidated.
4719  **/
4720 static void
4721 gtk_widget_queue_shallow_draw (GtkWidget *widget)
4722 {
4723   GdkRectangle rect;
4724   cairo_region_t *region;
4725
4726   if (!gtk_widget_get_realized (widget))
4727     return;
4728
4729   gtk_widget_get_allocation (widget, &rect);
4730
4731   region = cairo_region_create_rectangle (&rect);
4732   gtk_widget_invalidate_widget_windows (widget, region);
4733   cairo_region_destroy (region);
4734 }
4735
4736 /**
4737  * gtk_widget_size_allocate:
4738  * @widget: a #GtkWidget
4739  * @allocation: position and size to be allocated to @widget
4740  *
4741  * This function is only used by #GtkContainer subclasses, to assign a size
4742  * and position to their child widgets.
4743  *
4744  * In this function, the allocation may be adjusted. It will be forced
4745  * to a 1x1 minimum size, and the adjust_size_allocation virtual
4746  * method on the child will be used to adjust the allocation. Standard
4747  * adjustments include removing the widget's margins, and applying the
4748  * widget's #GtkWidget:halign and #GtkWidget:valign properties.
4749  **/
4750 void
4751 gtk_widget_size_allocate (GtkWidget     *widget,
4752                           GtkAllocation *allocation)
4753 {
4754   GtkWidgetPrivate *priv;
4755   GdkRectangle real_allocation;
4756   GdkRectangle old_allocation;
4757   GdkRectangle adjusted_allocation;
4758   gboolean alloc_needed;
4759   gboolean size_changed;
4760   gboolean position_changed;
4761   gint natural_width, natural_height, dummy;
4762   gint min_width, min_height;
4763
4764   priv = widget->priv;
4765
4766   g_return_if_fail (GTK_IS_WIDGET (widget));
4767
4768   gtk_widget_push_verify_invariants (widget);
4769
4770 #ifdef G_ENABLE_DEBUG
4771   if (gtk_get_debug_flags () & GTK_DEBUG_GEOMETRY)
4772     {
4773       gint depth;
4774       GtkWidget *parent;
4775       const gchar *name;
4776
4777       depth = 0;
4778       parent = widget;
4779       while (parent)
4780         {
4781           depth++;
4782           parent = gtk_widget_get_parent (parent);
4783         }
4784
4785       name = g_type_name (G_OBJECT_TYPE (G_OBJECT (widget)));
4786       g_print ("gtk_widget_size_allocate: %*s%s %d %d\n",
4787                2 * depth, " ", name,
4788                allocation->width, allocation->height);
4789     }
4790 #endif /* G_ENABLE_DEBUG */
4791
4792   alloc_needed = priv->alloc_needed;
4793   if (!priv->width_request_needed && !priv->height_request_needed)
4794     /* Preserve request/allocate ordering */
4795     priv->alloc_needed = FALSE;
4796
4797   old_allocation = priv->allocation;
4798   real_allocation = *allocation;
4799
4800   adjusted_allocation = real_allocation;
4801   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
4802     {
4803       /* Go ahead and request the height for allocated width, note that the internals
4804        * of get_height_for_width will internally limit the for_size to natural size
4805        * when aligning implicitly.
4806        */
4807       gtk_widget_get_preferred_width (widget, &min_width, &natural_width);
4808       gtk_widget_get_preferred_height_for_width (widget, real_allocation.width, &min_height, &natural_height);
4809     }
4810   else
4811     {
4812       /* Go ahead and request the width for allocated height, note that the internals
4813        * of get_width_for_height will internally limit the for_size to natural size
4814        * when aligning implicitly.
4815        */
4816       gtk_widget_get_preferred_height (widget, &min_height, &natural_height);
4817       gtk_widget_get_preferred_width_for_height (widget, real_allocation.height, &min_width, &natural_width);
4818     }
4819
4820   if ((min_width > real_allocation.width || min_height > real_allocation.height) &&
4821       !GTK_IS_SCROLLABLE (widget))
4822     g_warning ("gtk_widget_size_allocate(): attempt to underallocate %s%s %s %p. "
4823                "Allocation is %dx%d, but minimum required size is %dx%d.",
4824                priv->parent ? G_OBJECT_TYPE_NAME (priv->parent) : "", priv->parent ? "'s child" : "toplevel",
4825                G_OBJECT_TYPE_NAME (widget), widget,
4826                real_allocation.width, real_allocation.height,
4827                min_width, min_height);
4828   /* Now that we have the right natural height and width, go ahead and remove any margins from the
4829    * allocated sizes and possibly limit them to the natural sizes */
4830   GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
4831                                                          GTK_ORIENTATION_HORIZONTAL,
4832                                                          &dummy,
4833                                                          &natural_width,
4834                                                          &adjusted_allocation.x,
4835                                                          &adjusted_allocation.width);
4836   GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
4837                                                          GTK_ORIENTATION_VERTICAL,
4838                                                          &dummy,
4839                                                          &natural_height,
4840                                                          &adjusted_allocation.y,
4841                                                          &adjusted_allocation.height);
4842
4843   if (adjusted_allocation.x < real_allocation.x ||
4844       adjusted_allocation.y < real_allocation.y ||
4845       (adjusted_allocation.x + adjusted_allocation.width) >
4846       (real_allocation.x + real_allocation.width) ||
4847       (adjusted_allocation.y + adjusted_allocation.height >
4848        real_allocation.y + real_allocation.height))
4849     {
4850       g_warning ("%s %p attempted to adjust its size allocation from %d,%d %dx%d to %d,%d %dx%d. adjust_size_allocation must keep allocation inside original bounds",
4851                  G_OBJECT_TYPE_NAME (widget), widget,
4852                  real_allocation.x, real_allocation.y, real_allocation.width, real_allocation.height,
4853                  adjusted_allocation.x, adjusted_allocation.y, adjusted_allocation.width, adjusted_allocation.height);
4854       adjusted_allocation = real_allocation; /* veto it */
4855     }
4856   else
4857     {
4858       real_allocation = adjusted_allocation;
4859     }
4860
4861   if (real_allocation.width < 0 || real_allocation.height < 0)
4862     {
4863       g_warning ("gtk_widget_size_allocate(): attempt to allocate widget with width %d and height %d",
4864                  real_allocation.width,
4865                  real_allocation.height);
4866     }
4867
4868   real_allocation.width = MAX (real_allocation.width, 1);
4869   real_allocation.height = MAX (real_allocation.height, 1);
4870
4871   size_changed = (old_allocation.width != real_allocation.width ||
4872                   old_allocation.height != real_allocation.height);
4873   position_changed = (old_allocation.x != real_allocation.x ||
4874                       old_allocation.y != real_allocation.y);
4875
4876   if (!alloc_needed && !size_changed && !position_changed)
4877     goto out;
4878
4879   g_signal_emit (widget, widget_signals[SIZE_ALLOCATE], 0, &real_allocation);
4880
4881   /* Size allocation is god... after consulting god, no further requests or allocations are needed */
4882   priv->width_request_needed  = FALSE;
4883   priv->height_request_needed = FALSE;
4884   priv->alloc_needed          = FALSE;
4885
4886   if (gtk_widget_get_mapped (widget))
4887     {
4888       if (!gtk_widget_get_has_window (widget) && priv->redraw_on_alloc && position_changed)
4889         {
4890           /* Invalidate union(old_allaction,priv->allocation) in priv->window
4891            */
4892           cairo_region_t *invalidate = cairo_region_create_rectangle (&priv->allocation);
4893           cairo_region_union_rectangle (invalidate, &old_allocation);
4894
4895           gdk_window_invalidate_region (priv->window, invalidate, FALSE);
4896           cairo_region_destroy (invalidate);
4897         }
4898
4899       if (size_changed)
4900         {
4901           if (priv->redraw_on_alloc)
4902             {
4903               /* Invalidate union(old_allaction,priv->allocation) in priv->window and descendents owned by widget
4904                */
4905               cairo_region_t *invalidate = cairo_region_create_rectangle (&priv->allocation);
4906               cairo_region_union_rectangle (invalidate, &old_allocation);
4907
4908               gtk_widget_invalidate_widget_windows (widget, invalidate);
4909               cairo_region_destroy (invalidate);
4910             }
4911         }
4912
4913       if (size_changed || position_changed)
4914         {
4915           GtkStyleContext *context;
4916
4917           context = gtk_widget_get_style_context (widget);
4918           _gtk_style_context_invalidate_animation_areas (context);
4919         }
4920     }
4921
4922   if ((size_changed || position_changed) && priv->parent &&
4923       gtk_widget_get_realized (priv->parent) && _gtk_container_get_reallocate_redraws (GTK_CONTAINER (priv->parent)))
4924     {
4925       cairo_region_t *invalidate = cairo_region_create_rectangle (&priv->parent->priv->allocation);
4926       gtk_widget_invalidate_widget_windows (priv->parent, invalidate);
4927       cairo_region_destroy (invalidate);
4928     }
4929
4930 out:
4931   gtk_widget_pop_verify_invariants (widget);
4932 }
4933
4934 /**
4935  * gtk_widget_common_ancestor:
4936  * @widget_a: a #GtkWidget
4937  * @widget_b: a #GtkWidget
4938  *
4939  * Find the common ancestor of @widget_a and @widget_b that
4940  * is closest to the two widgets.
4941  *
4942  * Return value: the closest common ancestor of @widget_a and
4943  *   @widget_b or %NULL if @widget_a and @widget_b do not
4944  *   share a common ancestor.
4945  **/
4946 static GtkWidget *
4947 gtk_widget_common_ancestor (GtkWidget *widget_a,
4948                             GtkWidget *widget_b)
4949 {
4950   GtkWidget *parent_a;
4951   GtkWidget *parent_b;
4952   gint depth_a = 0;
4953   gint depth_b = 0;
4954
4955   parent_a = widget_a;
4956   while (parent_a->priv->parent)
4957     {
4958       parent_a = parent_a->priv->parent;
4959       depth_a++;
4960     }
4961
4962   parent_b = widget_b;
4963   while (parent_b->priv->parent)
4964     {
4965       parent_b = parent_b->priv->parent;
4966       depth_b++;
4967     }
4968
4969   if (parent_a != parent_b)
4970     return NULL;
4971
4972   while (depth_a > depth_b)
4973     {
4974       widget_a = widget_a->priv->parent;
4975       depth_a--;
4976     }
4977
4978   while (depth_b > depth_a)
4979     {
4980       widget_b = widget_b->priv->parent;
4981       depth_b--;
4982     }
4983
4984   while (widget_a != widget_b)
4985     {
4986       widget_a = widget_a->priv->parent;
4987       widget_b = widget_b->priv->parent;
4988     }
4989
4990   return widget_a;
4991 }
4992
4993 /**
4994  * gtk_widget_translate_coordinates:
4995  * @src_widget:  a #GtkWidget
4996  * @dest_widget: a #GtkWidget
4997  * @src_x: X position relative to @src_widget
4998  * @src_y: Y position relative to @src_widget
4999  * @dest_x: (out): location to store X position relative to @dest_widget
5000  * @dest_y: (out): location to store Y position relative to @dest_widget
5001  *
5002  * Translate coordinates relative to @src_widget's allocation to coordinates
5003  * relative to @dest_widget's allocations. In order to perform this
5004  * operation, both widgets must be realized, and must share a common
5005  * toplevel.
5006  *
5007  * Return value: %FALSE if either widget was not realized, or there
5008  *   was no common ancestor. In this case, nothing is stored in
5009  *   *@dest_x and *@dest_y. Otherwise %TRUE.
5010  **/
5011 gboolean
5012 gtk_widget_translate_coordinates (GtkWidget  *src_widget,
5013                                   GtkWidget  *dest_widget,
5014                                   gint        src_x,
5015                                   gint        src_y,
5016                                   gint       *dest_x,
5017                                   gint       *dest_y)
5018 {
5019   GtkWidgetPrivate *src_priv = src_widget->priv;
5020   GtkWidgetPrivate *dest_priv = dest_widget->priv;
5021   GtkWidget *ancestor;
5022   GdkWindow *window;
5023   GList *dest_list = NULL;
5024
5025   g_return_val_if_fail (GTK_IS_WIDGET (src_widget), FALSE);
5026   g_return_val_if_fail (GTK_IS_WIDGET (dest_widget), FALSE);
5027
5028   ancestor = gtk_widget_common_ancestor (src_widget, dest_widget);
5029   if (!ancestor || !gtk_widget_get_realized (src_widget) || !gtk_widget_get_realized (dest_widget))
5030     return FALSE;
5031
5032   /* Translate from allocation relative to window relative */
5033   if (gtk_widget_get_has_window (src_widget) && src_priv->parent)
5034     {
5035       gint wx, wy;
5036       gdk_window_get_position (src_priv->window, &wx, &wy);
5037
5038       src_x -= wx - src_priv->allocation.x;
5039       src_y -= wy - src_priv->allocation.y;
5040     }
5041   else
5042     {
5043       src_x += src_priv->allocation.x;
5044       src_y += src_priv->allocation.y;
5045     }
5046
5047   /* Translate to the common ancestor */
5048   window = src_priv->window;
5049   while (window != ancestor->priv->window)
5050     {
5051       gdouble dx, dy;
5052
5053       gdk_window_coords_to_parent (window, src_x, src_y, &dx, &dy);
5054
5055       src_x = dx;
5056       src_y = dy;
5057
5058       window = gdk_window_get_effective_parent (window);
5059
5060       if (!window)              /* Handle GtkHandleBox */
5061         return FALSE;
5062     }
5063
5064   /* And back */
5065   window = dest_priv->window;
5066   while (window != ancestor->priv->window)
5067     {
5068       dest_list = g_list_prepend (dest_list, window);
5069
5070       window = gdk_window_get_effective_parent (window);
5071
5072       if (!window)              /* Handle GtkHandleBox */
5073         {
5074           g_list_free (dest_list);
5075           return FALSE;
5076         }
5077     }
5078
5079   while (dest_list)
5080     {
5081       gdouble dx, dy;
5082
5083       gdk_window_coords_from_parent (dest_list->data, src_x, src_y, &dx, &dy);
5084
5085       src_x = dx;
5086       src_y = dy;
5087
5088       dest_list = g_list_remove (dest_list, dest_list->data);
5089     }
5090
5091   /* Translate from window relative to allocation relative */
5092   if (gtk_widget_get_has_window (dest_widget) && dest_priv->parent)
5093     {
5094       gint wx, wy;
5095       gdk_window_get_position (dest_priv->window, &wx, &wy);
5096
5097       src_x += wx - dest_priv->allocation.x;
5098       src_y += wy - dest_priv->allocation.y;
5099     }
5100   else
5101     {
5102       src_x -= dest_priv->allocation.x;
5103       src_y -= dest_priv->allocation.y;
5104     }
5105
5106   if (dest_x)
5107     *dest_x = src_x;
5108   if (dest_y)
5109     *dest_y = src_y;
5110
5111   return TRUE;
5112 }
5113
5114 static void
5115 gtk_widget_real_size_allocate (GtkWidget     *widget,
5116                                GtkAllocation *allocation)
5117 {
5118   GtkWidgetPrivate *priv = widget->priv;
5119
5120   priv->allocation = *allocation;
5121
5122   if (gtk_widget_get_realized (widget) &&
5123       gtk_widget_get_has_window (widget))
5124      {
5125         gdk_window_move_resize (priv->window,
5126                                 allocation->x, allocation->y,
5127                                 allocation->width, allocation->height);
5128      }
5129 }
5130
5131 /* translate initial/final into start/end */
5132 static GtkAlign
5133 effective_align (GtkAlign         align,
5134                  GtkTextDirection direction)
5135 {
5136   switch (align)
5137     {
5138     case GTK_ALIGN_START:
5139       return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_END : GTK_ALIGN_START;
5140     case GTK_ALIGN_END:
5141       return direction == GTK_TEXT_DIR_RTL ? GTK_ALIGN_START : GTK_ALIGN_END;
5142     default:
5143       return align;
5144     }
5145 }
5146
5147 static void
5148 adjust_for_align (GtkAlign  align,
5149                   gint     *natural_size,
5150                   gint     *allocated_pos,
5151                   gint     *allocated_size)
5152 {
5153   switch (align)
5154     {
5155     case GTK_ALIGN_FILL:
5156       /* change nothing */
5157       break;
5158     case GTK_ALIGN_START:
5159       /* keep *allocated_pos where it is */
5160       *allocated_size = MIN (*allocated_size, *natural_size);
5161       break;
5162     case GTK_ALIGN_END:
5163       if (*allocated_size > *natural_size)
5164         {
5165           *allocated_pos += (*allocated_size - *natural_size);
5166           *allocated_size = *natural_size;
5167         }
5168       break;
5169     case GTK_ALIGN_CENTER:
5170       if (*allocated_size > *natural_size)
5171         {
5172           *allocated_pos += (*allocated_size - *natural_size) / 2;
5173           *allocated_size = MIN (*allocated_size, *natural_size);
5174         }
5175       break;
5176     }
5177 }
5178
5179 static void
5180 adjust_for_margin(gint               start_margin,
5181                   gint               end_margin,
5182                   gint              *minimum_size,
5183                   gint              *natural_size,
5184                   gint              *allocated_pos,
5185                   gint              *allocated_size)
5186 {
5187   *minimum_size -= (start_margin + end_margin);
5188   *natural_size -= (start_margin + end_margin);
5189   *allocated_pos += start_margin;
5190   *allocated_size -= (start_margin + end_margin);
5191 }
5192
5193 static void
5194 gtk_widget_real_adjust_size_allocation (GtkWidget         *widget,
5195                                         GtkOrientation     orientation,
5196                                         gint              *minimum_size,
5197                                         gint              *natural_size,
5198                                         gint              *allocated_pos,
5199                                         gint              *allocated_size)
5200 {
5201   const GtkWidgetAuxInfo *aux_info;
5202
5203   aux_info = _gtk_widget_get_aux_info_or_defaults (widget);
5204
5205   if (orientation == GTK_ORIENTATION_HORIZONTAL)
5206     {
5207       adjust_for_margin (aux_info->margin.left,
5208                          aux_info->margin.right,
5209                          minimum_size, natural_size,
5210                          allocated_pos, allocated_size);
5211       adjust_for_align (effective_align (aux_info->halign, gtk_widget_get_direction (widget)),
5212                         natural_size, allocated_pos, allocated_size);
5213     }
5214   else
5215     {
5216       adjust_for_margin (aux_info->margin.top,
5217                          aux_info->margin.bottom,
5218                          minimum_size, natural_size,
5219                          allocated_pos, allocated_size);
5220       adjust_for_align (effective_align (aux_info->valign, GTK_TEXT_DIR_NONE),
5221                         natural_size, allocated_pos, allocated_size);
5222     }
5223 }
5224
5225 static gboolean
5226 gtk_widget_real_can_activate_accel (GtkWidget *widget,
5227                                     guint      signal_id)
5228 {
5229   GtkWidgetPrivate *priv = widget->priv;
5230
5231   /* widgets must be onscreen for accels to take effect */
5232   return gtk_widget_is_sensitive (widget) &&
5233          gtk_widget_is_drawable (widget) &&
5234          gdk_window_is_viewable (priv->window);
5235 }
5236
5237 /**
5238  * gtk_widget_can_activate_accel:
5239  * @widget: a #GtkWidget
5240  * @signal_id: the ID of a signal installed on @widget
5241  *
5242  * Determines whether an accelerator that activates the signal
5243  * identified by @signal_id can currently be activated.
5244  * This is done by emitting the #GtkWidget::can-activate-accel
5245  * signal on @widget; if the signal isn't overridden by a
5246  * handler or in a derived widget, then the default check is
5247  * that the widget must be sensitive, and the widget and all
5248  * its ancestors mapped.
5249  *
5250  * Return value: %TRUE if the accelerator can be activated.
5251  *
5252  * Since: 2.4
5253  **/
5254 gboolean
5255 gtk_widget_can_activate_accel (GtkWidget *widget,
5256                                guint      signal_id)
5257 {
5258   gboolean can_activate = FALSE;
5259   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5260   g_signal_emit (widget, widget_signals[CAN_ACTIVATE_ACCEL], 0, signal_id, &can_activate);
5261   return can_activate;
5262 }
5263
5264 typedef struct {
5265   GClosure   closure;
5266   guint      signal_id;
5267 } AccelClosure;
5268
5269 static void
5270 closure_accel_activate (GClosure     *closure,
5271                         GValue       *return_value,
5272                         guint         n_param_values,
5273                         const GValue *param_values,
5274                         gpointer      invocation_hint,
5275                         gpointer      marshal_data)
5276 {
5277   AccelClosure *aclosure = (AccelClosure*) closure;
5278   gboolean can_activate = gtk_widget_can_activate_accel (closure->data, aclosure->signal_id);
5279
5280   if (can_activate)
5281     g_signal_emit (closure->data, aclosure->signal_id, 0);
5282
5283   /* whether accelerator was handled */
5284   g_value_set_boolean (return_value, can_activate);
5285 }
5286
5287 static void
5288 closures_destroy (gpointer data)
5289 {
5290   GSList *slist, *closures = data;
5291
5292   for (slist = closures; slist; slist = slist->next)
5293     {
5294       g_closure_invalidate (slist->data);
5295       g_closure_unref (slist->data);
5296     }
5297   g_slist_free (closures);
5298 }
5299
5300 static GClosure*
5301 widget_new_accel_closure (GtkWidget *widget,
5302                           guint      signal_id)
5303 {
5304   AccelClosure *aclosure;
5305   GClosure *closure = NULL;
5306   GSList *slist, *closures;
5307
5308   closures = g_object_steal_qdata (G_OBJECT (widget), quark_accel_closures);
5309   for (slist = closures; slist; slist = slist->next)
5310     if (!gtk_accel_group_from_accel_closure (slist->data))
5311       {
5312         /* reuse this closure */
5313         closure = slist->data;
5314         break;
5315       }
5316   if (!closure)
5317     {
5318       closure = g_closure_new_object (sizeof (AccelClosure), G_OBJECT (widget));
5319       closures = g_slist_prepend (closures, g_closure_ref (closure));
5320       g_closure_sink (closure);
5321       g_closure_set_marshal (closure, closure_accel_activate);
5322     }
5323   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_closures, closures, closures_destroy);
5324
5325   aclosure = (AccelClosure*) closure;
5326   g_assert (closure->data == widget);
5327   g_assert (closure->marshal == closure_accel_activate);
5328   aclosure->signal_id = signal_id;
5329
5330   return closure;
5331 }
5332
5333 /**
5334  * gtk_widget_add_accelerator
5335  * @widget:       widget to install an accelerator on
5336  * @accel_signal: widget signal to emit on accelerator activation
5337  * @accel_group:  accel group for this widget, added to its toplevel
5338  * @accel_key:    GDK keyval of the accelerator
5339  * @accel_mods:   modifier key combination of the accelerator
5340  * @accel_flags:  flag accelerators, e.g. %GTK_ACCEL_VISIBLE
5341  *
5342  * Installs an accelerator for this @widget in @accel_group that causes
5343  * @accel_signal to be emitted if the accelerator is activated.
5344  * The @accel_group needs to be added to the widget's toplevel via
5345  * gtk_window_add_accel_group(), and the signal must be of type %G_RUN_ACTION.
5346  * Accelerators added through this function are not user changeable during
5347  * runtime. If you want to support accelerators that can be changed by the
5348  * user, use gtk_accel_map_add_entry() and gtk_widget_set_accel_path() or
5349  * gtk_menu_item_set_accel_path() instead.
5350  */
5351 void
5352 gtk_widget_add_accelerator (GtkWidget      *widget,
5353                             const gchar    *accel_signal,
5354                             GtkAccelGroup  *accel_group,
5355                             guint           accel_key,
5356                             GdkModifierType accel_mods,
5357                             GtkAccelFlags   accel_flags)
5358 {
5359   GClosure *closure;
5360   GSignalQuery query;
5361
5362   g_return_if_fail (GTK_IS_WIDGET (widget));
5363   g_return_if_fail (accel_signal != NULL);
5364   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
5365
5366   g_signal_query (g_signal_lookup (accel_signal, G_OBJECT_TYPE (widget)), &query);
5367   if (!query.signal_id ||
5368       !(query.signal_flags & G_SIGNAL_ACTION) ||
5369       query.return_type != G_TYPE_NONE ||
5370       query.n_params)
5371     {
5372       /* hmm, should be elaborate enough */
5373       g_warning (G_STRLOC ": widget `%s' has no activatable signal \"%s\" without arguments",
5374                  G_OBJECT_TYPE_NAME (widget), accel_signal);
5375       return;
5376     }
5377
5378   closure = widget_new_accel_closure (widget, query.signal_id);
5379
5380   g_object_ref (widget);
5381
5382   /* install the accelerator. since we don't map this onto an accel_path,
5383    * the accelerator will automatically be locked.
5384    */
5385   gtk_accel_group_connect (accel_group,
5386                            accel_key,
5387                            accel_mods,
5388                            accel_flags | GTK_ACCEL_LOCKED,
5389                            closure);
5390
5391   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
5392
5393   g_object_unref (widget);
5394 }
5395
5396 /**
5397  * gtk_widget_remove_accelerator:
5398  * @widget:       widget to install an accelerator on
5399  * @accel_group:  accel group for this widget
5400  * @accel_key:    GDK keyval of the accelerator
5401  * @accel_mods:   modifier key combination of the accelerator
5402  * @returns:      whether an accelerator was installed and could be removed
5403  *
5404  * Removes an accelerator from @widget, previously installed with
5405  * gtk_widget_add_accelerator().
5406  */
5407 gboolean
5408 gtk_widget_remove_accelerator (GtkWidget      *widget,
5409                                GtkAccelGroup  *accel_group,
5410                                guint           accel_key,
5411                                GdkModifierType accel_mods)
5412 {
5413   GtkAccelGroupEntry *ag_entry;
5414   GList *slist, *clist;
5415   guint n;
5416
5417   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5418   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
5419
5420   ag_entry = gtk_accel_group_query (accel_group, accel_key, accel_mods, &n);
5421   clist = gtk_widget_list_accel_closures (widget);
5422   for (slist = clist; slist; slist = slist->next)
5423     {
5424       guint i;
5425
5426       for (i = 0; i < n; i++)
5427         if (slist->data == (gpointer) ag_entry[i].closure)
5428           {
5429             gboolean is_removed = gtk_accel_group_disconnect (accel_group, slist->data);
5430
5431             g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
5432
5433             g_list_free (clist);
5434
5435             return is_removed;
5436           }
5437     }
5438   g_list_free (clist);
5439
5440   g_warning (G_STRLOC ": no accelerator (%u,%u) installed in accel group (%p) for %s (%p)",
5441              accel_key, accel_mods, accel_group,
5442              G_OBJECT_TYPE_NAME (widget), widget);
5443
5444   return FALSE;
5445 }
5446
5447 /**
5448  * gtk_widget_list_accel_closures:
5449  * @widget:  widget to list accelerator closures for
5450  *
5451  * Lists the closures used by @widget for accelerator group connections
5452  * with gtk_accel_group_connect_by_path() or gtk_accel_group_connect().
5453  * The closures can be used to monitor accelerator changes on @widget,
5454  * by connecting to the @GtkAccelGroup::accel-changed signal of the
5455  * #GtkAccelGroup of a closure which can be found out with
5456  * gtk_accel_group_from_accel_closure().
5457  *
5458  * Return value: (transfer container) (element-type GClosure):
5459  *     a newly allocated #GList of closures
5460  */
5461 GList*
5462 gtk_widget_list_accel_closures (GtkWidget *widget)
5463 {
5464   GSList *slist;
5465   GList *clist = NULL;
5466
5467   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5468
5469   for (slist = g_object_get_qdata (G_OBJECT (widget), quark_accel_closures); slist; slist = slist->next)
5470     if (gtk_accel_group_from_accel_closure (slist->data))
5471       clist = g_list_prepend (clist, slist->data);
5472   return clist;
5473 }
5474
5475 typedef struct {
5476   GQuark         path_quark;
5477   GtkAccelGroup *accel_group;
5478   GClosure      *closure;
5479 } AccelPath;
5480
5481 static void
5482 destroy_accel_path (gpointer data)
5483 {
5484   AccelPath *apath = data;
5485
5486   gtk_accel_group_disconnect (apath->accel_group, apath->closure);
5487
5488   /* closures_destroy takes care of unrefing the closure */
5489   g_object_unref (apath->accel_group);
5490
5491   g_slice_free (AccelPath, apath);
5492 }
5493
5494
5495 /**
5496  * gtk_widget_set_accel_path:
5497  * @widget: a #GtkWidget
5498  * @accel_path: (allow-none): path used to look up the accelerator
5499  * @accel_group: (allow-none): a #GtkAccelGroup.
5500  *
5501  * Given an accelerator group, @accel_group, and an accelerator path,
5502  * @accel_path, sets up an accelerator in @accel_group so whenever the
5503  * key binding that is defined for @accel_path is pressed, @widget
5504  * will be activated.  This removes any accelerators (for any
5505  * accelerator group) installed by previous calls to
5506  * gtk_widget_set_accel_path(). Associating accelerators with
5507  * paths allows them to be modified by the user and the modifications
5508  * to be saved for future use. (See gtk_accel_map_save().)
5509  *
5510  * This function is a low level function that would most likely
5511  * be used by a menu creation system like #GtkUIManager. If you
5512  * use #GtkUIManager, setting up accelerator paths will be done
5513  * automatically.
5514  *
5515  * Even when you you aren't using #GtkUIManager, if you only want to
5516  * set up accelerators on menu items gtk_menu_item_set_accel_path()
5517  * provides a somewhat more convenient interface.
5518  *
5519  * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
5520  * pass a static string, you can save some memory by interning it first with
5521  * g_intern_static_string().
5522  **/
5523 void
5524 gtk_widget_set_accel_path (GtkWidget     *widget,
5525                            const gchar   *accel_path,
5526                            GtkAccelGroup *accel_group)
5527 {
5528   AccelPath *apath;
5529
5530   g_return_if_fail (GTK_IS_WIDGET (widget));
5531   g_return_if_fail (GTK_WIDGET_GET_CLASS (widget)->activate_signal != 0);
5532
5533   if (accel_path)
5534     {
5535       g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
5536       g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
5537
5538       gtk_accel_map_add_entry (accel_path, 0, 0);
5539       apath = g_slice_new (AccelPath);
5540       apath->accel_group = g_object_ref (accel_group);
5541       apath->path_quark = g_quark_from_string (accel_path);
5542       apath->closure = widget_new_accel_closure (widget, GTK_WIDGET_GET_CLASS (widget)->activate_signal);
5543     }
5544   else
5545     apath = NULL;
5546
5547   /* also removes possible old settings */
5548   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_path, apath, destroy_accel_path);
5549
5550   if (apath)
5551     gtk_accel_group_connect_by_path (apath->accel_group, g_quark_to_string (apath->path_quark), apath->closure);
5552
5553   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
5554 }
5555
5556 const gchar*
5557 _gtk_widget_get_accel_path (GtkWidget *widget,
5558                             gboolean  *locked)
5559 {
5560   AccelPath *apath;
5561
5562   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5563
5564   apath = g_object_get_qdata (G_OBJECT (widget), quark_accel_path);
5565   if (locked)
5566     *locked = apath ? gtk_accel_group_get_is_locked (apath->accel_group) : TRUE;
5567   return apath ? g_quark_to_string (apath->path_quark) : NULL;
5568 }
5569
5570 /**
5571  * gtk_widget_mnemonic_activate:
5572  * @widget: a #GtkWidget
5573  * @group_cycling:  %TRUE if there are other widgets with the same mnemonic
5574  *
5575  * Emits the #GtkWidget::mnemonic-activate signal.
5576  *
5577  * The default handler for this signal activates the @widget if
5578  * @group_cycling is %FALSE, and just grabs the focus if @group_cycling
5579  * is %TRUE.
5580  *
5581  * Returns: %TRUE if the signal has been handled
5582  */
5583 gboolean
5584 gtk_widget_mnemonic_activate (GtkWidget *widget,
5585                               gboolean   group_cycling)
5586 {
5587   gboolean handled;
5588
5589   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5590
5591   group_cycling = group_cycling != FALSE;
5592   if (!gtk_widget_is_sensitive (widget))
5593     handled = TRUE;
5594   else
5595     g_signal_emit (widget,
5596                    widget_signals[MNEMONIC_ACTIVATE],
5597                    0,
5598                    group_cycling,
5599                    &handled);
5600   return handled;
5601 }
5602
5603 static gboolean
5604 gtk_widget_real_mnemonic_activate (GtkWidget *widget,
5605                                    gboolean   group_cycling)
5606 {
5607   if (!group_cycling && GTK_WIDGET_GET_CLASS (widget)->activate_signal)
5608     gtk_widget_activate (widget);
5609   else if (gtk_widget_get_can_focus (widget))
5610     gtk_widget_grab_focus (widget);
5611   else
5612     {
5613       g_warning ("widget `%s' isn't suitable for mnemonic activation",
5614                  G_OBJECT_TYPE_NAME (widget));
5615       gtk_widget_error_bell (widget);
5616     }
5617   return TRUE;
5618 }
5619
5620 static const cairo_user_data_key_t event_key;
5621
5622 GdkEventExpose *
5623 _gtk_cairo_get_event (cairo_t *cr)
5624 {
5625   g_return_val_if_fail (cr != NULL, NULL);
5626
5627   return cairo_get_user_data (cr, &event_key);
5628 }
5629
5630 static void
5631 gtk_cairo_set_event (cairo_t        *cr,
5632                      GdkEventExpose *event)
5633 {
5634   cairo_set_user_data (cr, &event_key, event, NULL);
5635 }
5636
5637 /**
5638  * gtk_cairo_should_draw_window:
5639  * @cr: a cairo context
5640  * @window: the window to check. @window may not be an input-only
5641  *          window.
5642  *
5643  * This function is supposed to be called in #GtkWidget::draw
5644  * implementations for widgets that support multiple windows.
5645  * @cr must be untransformed from invoking of the draw function.
5646  * This function will return %TRUE if the contents of the given
5647  * @window are supposed to be drawn and %FALSE otherwise. Note
5648  * that when the drawing was not initiated by the windowing
5649  * system this function will return %TRUE for all windows, so
5650  * you need to draw the bottommost window first. Also, do not
5651  * use "else if" statements to check which window should be drawn.
5652  *
5653  * Returns: %TRUE if @window should be drawn
5654  *
5655  * Since: 3.0
5656  **/
5657 gboolean
5658 gtk_cairo_should_draw_window (cairo_t *cr,
5659                               GdkWindow *window)
5660 {
5661   GdkEventExpose *event;
5662
5663   g_return_val_if_fail (cr != NULL, FALSE);
5664   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
5665
5666   event = _gtk_cairo_get_event (cr);
5667
5668   return event == NULL ||
5669          event->window == window;
5670 }
5671
5672 static gboolean
5673 gtk_widget_get_clip_draw (GtkWidget *widget)
5674 {
5675   /* labels are not clipped, because clipping them would cause
5676    * mnemonics to not appear on characters that go beyond the
5677    * baseline.
5678    * https://bugzilla.gnome.org/show_bug.cgi?id=648570
5679    */
5680   if (GTK_IS_LABEL (widget))
5681     return FALSE;
5682
5683   return TRUE;
5684 }
5685
5686 /* code shared by gtk_container_propagate_draw() and
5687  * gtk_widget_draw()
5688  */
5689 void
5690 _gtk_widget_draw_internal (GtkWidget *widget,
5691                            cairo_t   *cr,
5692                            gboolean   clip_to_size)
5693 {
5694   GtkStyleContext *context;
5695
5696   if (!gtk_widget_is_drawable (widget))
5697     return;
5698
5699   clip_to_size &= gtk_widget_get_clip_draw (widget);
5700
5701   if (clip_to_size)
5702     {
5703       cairo_rectangle (cr,
5704                        0, 0,
5705                        widget->priv->allocation.width,
5706                        widget->priv->allocation.height);
5707       cairo_clip (cr);
5708     }
5709
5710   if (gdk_cairo_get_clip_rectangle (cr, NULL))
5711     {
5712       gboolean result;
5713
5714       g_signal_emit (widget, widget_signals[DRAW],
5715                      0, cr,
5716                      &result);
5717     }
5718
5719   context = gtk_widget_get_style_context (widget);
5720   _gtk_style_context_coalesce_animation_areas (context, widget);
5721 }
5722
5723 /**
5724  * gtk_widget_draw:
5725  * @widget: the widget to draw. It must be drawable (see
5726  *   gtk_widget_is_drawable()) and a size must have been allocated.
5727  * @cr: a cairo context to draw to
5728  *
5729  * Draws @widget to @cr. The top left corner of the widget will be
5730  * drawn to the currently set origin point of @cr.
5731  *
5732  * You should pass a cairo context as @cr argument that is in an
5733  * original state. Otherwise the resulting drawing is undefined. For
5734  * example changing the operator using cairo_set_operator() or the
5735  * line width using cairo_set_line_width() might have unwanted side
5736  * effects.
5737  * You may however change the context's transform matrix - like with
5738  * cairo_scale(), cairo_translate() or cairo_set_matrix() and clip
5739  * region with cairo_clip() prior to calling this function. Also, it
5740  * is fine to modify the context with cairo_save() and
5741  * cairo_push_group() prior to calling this function.
5742  *
5743  * <note><para>Special purpose widgets may contain special code for
5744  * rendering to the screen and might appear differently on screen
5745  * and when rendered using gtk_widget_draw().</para></note>
5746  *
5747  * Since: 3.0
5748  **/
5749 void
5750 gtk_widget_draw (GtkWidget *widget,
5751                  cairo_t   *cr)
5752 {
5753   GdkEventExpose *tmp_event;
5754
5755   g_return_if_fail (GTK_IS_WIDGET (widget));
5756   g_return_if_fail (!widget->priv->alloc_needed);
5757   g_return_if_fail (cr != NULL);
5758
5759   cairo_save (cr);
5760   /* We have to reset the event here so that draw functions can call
5761    * gtk_widget_draw() on random other widgets and get the desired
5762    * effect: Drawing all contents, not just the current window.
5763    */
5764   tmp_event = _gtk_cairo_get_event (cr);
5765   gtk_cairo_set_event (cr, NULL);
5766
5767   _gtk_widget_draw_internal (widget, cr, TRUE);
5768
5769   gtk_cairo_set_event (cr, tmp_event);
5770   cairo_restore (cr);
5771 }
5772
5773 static gboolean
5774 gtk_widget_real_key_press_event (GtkWidget         *widget,
5775                                  GdkEventKey       *event)
5776 {
5777   return gtk_bindings_activate_event (G_OBJECT (widget), event);
5778 }
5779
5780 static gboolean
5781 gtk_widget_real_key_release_event (GtkWidget         *widget,
5782                                    GdkEventKey       *event)
5783 {
5784   return gtk_bindings_activate_event (G_OBJECT (widget), event);
5785 }
5786
5787 static gboolean
5788 gtk_widget_real_focus_in_event (GtkWidget     *widget,
5789                                 GdkEventFocus *event)
5790 {
5791   gtk_widget_queue_shallow_draw (widget);
5792
5793   return FALSE;
5794 }
5795
5796 static gboolean
5797 gtk_widget_real_focus_out_event (GtkWidget     *widget,
5798                                  GdkEventFocus *event)
5799 {
5800   gtk_widget_queue_shallow_draw (widget);
5801
5802   return FALSE;
5803 }
5804
5805 #define WIDGET_REALIZED_FOR_EVENT(widget, event) \
5806      (event->type == GDK_FOCUS_CHANGE || gtk_widget_get_realized(widget))
5807
5808 /**
5809  * gtk_widget_event:
5810  * @widget: a #GtkWidget
5811  * @event: a #GdkEvent
5812  *
5813  * Rarely-used function. This function is used to emit
5814  * the event signals on a widget (those signals should never
5815  * be emitted without using this function to do so).
5816  * If you want to synthesize an event though, don't use this function;
5817  * instead, use gtk_main_do_event() so the event will behave as if
5818  * it were in the event queue. Don't synthesize expose events; instead,
5819  * use gdk_window_invalidate_rect() to invalidate a region of the
5820  * window.
5821  *
5822  * Return value: return from the event signal emission (%TRUE if
5823  *               the event was handled)
5824  **/
5825 gboolean
5826 gtk_widget_event (GtkWidget *widget,
5827                   GdkEvent  *event)
5828 {
5829   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
5830   g_return_val_if_fail (WIDGET_REALIZED_FOR_EVENT (widget, event), TRUE);
5831
5832   if (event->type == GDK_EXPOSE)
5833     {
5834       g_warning ("Events of type GDK_EXPOSE cannot be synthesized. To get "
5835                  "the same effect, call gdk_window_invalidate_rect/region(), "
5836                  "followed by gdk_window_process_updates().");
5837       return TRUE;
5838     }
5839
5840   return gtk_widget_event_internal (widget, event);
5841 }
5842
5843 /* Returns TRUE if a translation should be done */
5844 gboolean
5845 _gtk_widget_get_translation_to_window (GtkWidget      *widget,
5846                                        GdkWindow      *window,
5847                                        int            *x,
5848                                        int            *y)
5849 {
5850   GdkWindow *w, *widget_window;
5851
5852   if (!gtk_widget_get_has_window (widget))
5853     {
5854       *x = -widget->priv->allocation.x;
5855       *y = -widget->priv->allocation.y;
5856     }
5857   else
5858     {
5859       *x = 0;
5860       *y = 0;
5861     }
5862
5863   widget_window = gtk_widget_get_window (widget);
5864
5865   for (w = window; w && w != widget_window; w = gdk_window_get_parent (w))
5866     {
5867       int wx, wy;
5868       gdk_window_get_position (w, &wx, &wy);
5869       *x += wx;
5870       *y += wy;
5871     }
5872
5873   if (w == NULL)
5874     {
5875       *x = 0;
5876       *y = 0;
5877       return FALSE;
5878     }
5879
5880   return TRUE;
5881 }
5882
5883
5884 /**
5885  * gtk_cairo_transform_to_window:
5886  * @cr: the cairo context to transform
5887  * @widget: the widget the context is currently centered for
5888  * @window: the window to transform the context to
5889  *
5890  * Transforms the given cairo context @cr that from @widget-relative
5891  * coordinates to @window-relative coordinates.
5892  * If the @widget's window is not an ancestor of @window, no
5893  * modification will be applied.
5894  *
5895  * This is the inverse to the transformation GTK applies when
5896  * preparing an expose event to be emitted with the #GtkWidget::draw
5897  * signal. It is intended to help porting multiwindow widgets from
5898  * GTK+ 2 to the rendering architecture of GTK+ 3.
5899  *
5900  * Since: 3.0
5901  **/
5902 void
5903 gtk_cairo_transform_to_window (cairo_t   *cr,
5904                                GtkWidget *widget,
5905                                GdkWindow *window)
5906 {
5907   int x, y;
5908
5909   g_return_if_fail (cr != NULL);
5910   g_return_if_fail (GTK_IS_WIDGET (widget));
5911   g_return_if_fail (GDK_IS_WINDOW (window));
5912
5913   if (_gtk_widget_get_translation_to_window (widget, window, &x, &y))
5914     cairo_translate (cr, x, y);
5915 }
5916
5917 /**
5918  * gtk_widget_send_expose:
5919  * @widget: a #GtkWidget
5920  * @event: a expose #GdkEvent
5921  *
5922  * Very rarely-used function. This function is used to emit
5923  * an expose event on a widget. This function is not normally used
5924  * directly. The only time it is used is when propagating an expose
5925  * event to a child %NO_WINDOW widget, and that is normally done
5926  * using gtk_container_propagate_draw().
5927  *
5928  * If you want to force an area of a window to be redrawn,
5929  * use gdk_window_invalidate_rect() or gdk_window_invalidate_region().
5930  * To cause the redraw to be done immediately, follow that call
5931  * with a call to gdk_window_process_updates().
5932  *
5933  * Return value: return from the event signal emission (%TRUE if
5934  *               the event was handled)
5935  **/
5936 gint
5937 gtk_widget_send_expose (GtkWidget *widget,
5938                         GdkEvent  *event)
5939 {
5940   gboolean result = FALSE;
5941   cairo_t *cr;
5942   int x, y;
5943   gboolean do_clip;
5944
5945   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
5946   g_return_val_if_fail (gtk_widget_get_realized (widget), TRUE);
5947   g_return_val_if_fail (event != NULL, TRUE);
5948   g_return_val_if_fail (event->type == GDK_EXPOSE, TRUE);
5949
5950   cr = gdk_cairo_create (event->expose.window);
5951   gtk_cairo_set_event (cr, &event->expose);
5952
5953   gdk_cairo_region (cr, event->expose.region);
5954   cairo_clip (cr);
5955
5956   do_clip = _gtk_widget_get_translation_to_window (widget,
5957                                                    event->expose.window,
5958                                                    &x, &y);
5959   cairo_translate (cr, -x, -y);
5960
5961   _gtk_widget_draw_internal (widget, cr, do_clip);
5962
5963   /* unset here, so if someone keeps a reference to cr we
5964    * don't leak the window. */
5965   gtk_cairo_set_event (cr, NULL);
5966   cairo_destroy (cr);
5967
5968   return result;
5969 }
5970
5971 static gboolean
5972 event_window_is_still_viewable (GdkEvent *event)
5973 {
5974   /* Check that we think the event's window is viewable before
5975    * delivering the event, to prevent suprises. We do this here
5976    * at the last moment, since the event may have been queued
5977    * up behind other events, held over a recursive main loop, etc.
5978    */
5979   switch (event->type)
5980     {
5981     case GDK_EXPOSE:
5982     case GDK_MOTION_NOTIFY:
5983     case GDK_BUTTON_PRESS:
5984     case GDK_2BUTTON_PRESS:
5985     case GDK_3BUTTON_PRESS:
5986     case GDK_KEY_PRESS:
5987     case GDK_ENTER_NOTIFY:
5988     case GDK_PROXIMITY_IN:
5989     case GDK_SCROLL:
5990       return event->any.window && gdk_window_is_viewable (event->any.window);
5991
5992 #if 0
5993     /* The following events are the second half of paired events;
5994      * we always deliver them to deal with widgets that clean up
5995      * on the second half.
5996      */
5997     case GDK_BUTTON_RELEASE:
5998     case GDK_KEY_RELEASE:
5999     case GDK_LEAVE_NOTIFY:
6000     case GDK_PROXIMITY_OUT:
6001 #endif
6002
6003     default:
6004       /* Remaining events would make sense on an not-viewable window,
6005        * or don't have an associated window.
6006        */
6007       return TRUE;
6008     }
6009 }
6010
6011 static gint
6012 gtk_widget_event_internal (GtkWidget *widget,
6013                            GdkEvent  *event)
6014 {
6015   gboolean return_val = FALSE;
6016
6017   /* We check only once for is-still-visible; if someone
6018    * hides the window in on of the signals on the widget,
6019    * they are responsible for returning TRUE to terminate
6020    * handling.
6021    */
6022   if (!event_window_is_still_viewable (event))
6023     return TRUE;
6024
6025   g_object_ref (widget);
6026
6027   g_signal_emit (widget, widget_signals[EVENT], 0, event, &return_val);
6028   return_val |= !WIDGET_REALIZED_FOR_EVENT (widget, event);
6029   if (!return_val)
6030     {
6031       gint signal_num;
6032
6033       switch (event->type)
6034         {
6035         case GDK_EXPOSE:
6036         case GDK_NOTHING:
6037           signal_num = -1;
6038           break;
6039         case GDK_BUTTON_PRESS:
6040         case GDK_2BUTTON_PRESS:
6041         case GDK_3BUTTON_PRESS:
6042           signal_num = BUTTON_PRESS_EVENT;
6043           break;
6044         case GDK_SCROLL:
6045           signal_num = SCROLL_EVENT;
6046           break;
6047         case GDK_BUTTON_RELEASE:
6048           signal_num = BUTTON_RELEASE_EVENT;
6049           break;
6050         case GDK_MOTION_NOTIFY:
6051           signal_num = MOTION_NOTIFY_EVENT;
6052           break;
6053         case GDK_DELETE:
6054           signal_num = DELETE_EVENT;
6055           break;
6056         case GDK_DESTROY:
6057           signal_num = DESTROY_EVENT;
6058           _gtk_tooltip_hide (widget);
6059           break;
6060         case GDK_KEY_PRESS:
6061           signal_num = KEY_PRESS_EVENT;
6062           break;
6063         case GDK_KEY_RELEASE:
6064           signal_num = KEY_RELEASE_EVENT;
6065           break;
6066         case GDK_ENTER_NOTIFY:
6067           signal_num = ENTER_NOTIFY_EVENT;
6068           break;
6069         case GDK_LEAVE_NOTIFY:
6070           signal_num = LEAVE_NOTIFY_EVENT;
6071           break;
6072         case GDK_FOCUS_CHANGE:
6073           signal_num = event->focus_change.in ? FOCUS_IN_EVENT : FOCUS_OUT_EVENT;
6074           if (event->focus_change.in)
6075             _gtk_tooltip_focus_in (widget);
6076           else
6077             _gtk_tooltip_focus_out (widget);
6078           break;
6079         case GDK_CONFIGURE:
6080           signal_num = CONFIGURE_EVENT;
6081           break;
6082         case GDK_MAP:
6083           signal_num = MAP_EVENT;
6084           break;
6085         case GDK_UNMAP:
6086           signal_num = UNMAP_EVENT;
6087           break;
6088         case GDK_WINDOW_STATE:
6089           signal_num = WINDOW_STATE_EVENT;
6090           break;
6091         case GDK_PROPERTY_NOTIFY:
6092           signal_num = PROPERTY_NOTIFY_EVENT;
6093           break;
6094         case GDK_SELECTION_CLEAR:
6095           signal_num = SELECTION_CLEAR_EVENT;
6096           break;
6097         case GDK_SELECTION_REQUEST:
6098           signal_num = SELECTION_REQUEST_EVENT;
6099           break;
6100         case GDK_SELECTION_NOTIFY:
6101           signal_num = SELECTION_NOTIFY_EVENT;
6102           break;
6103         case GDK_PROXIMITY_IN:
6104           signal_num = PROXIMITY_IN_EVENT;
6105           break;
6106         case GDK_PROXIMITY_OUT:
6107           signal_num = PROXIMITY_OUT_EVENT;
6108           break;
6109         case GDK_VISIBILITY_NOTIFY:
6110           signal_num = VISIBILITY_NOTIFY_EVENT;
6111           break;
6112         case GDK_GRAB_BROKEN:
6113           signal_num = GRAB_BROKEN_EVENT;
6114           break;
6115         case GDK_DAMAGE:
6116           signal_num = DAMAGE_EVENT;
6117           break;
6118         default:
6119           g_warning ("gtk_widget_event(): unhandled event type: %d", event->type);
6120           signal_num = -1;
6121           break;
6122         }
6123       if (signal_num != -1)
6124         g_signal_emit (widget, widget_signals[signal_num], 0, event, &return_val);
6125     }
6126   if (WIDGET_REALIZED_FOR_EVENT (widget, event))
6127     g_signal_emit (widget, widget_signals[EVENT_AFTER], 0, event);
6128   else
6129     return_val = TRUE;
6130
6131   g_object_unref (widget);
6132
6133   return return_val;
6134 }
6135
6136 /**
6137  * gtk_widget_activate:
6138  * @widget: a #GtkWidget that's activatable
6139  *
6140  * For widgets that can be "activated" (buttons, menu items, etc.)
6141  * this function activates them. Activation is what happens when you
6142  * press Enter on a widget during key navigation. If @widget isn't
6143  * activatable, the function returns %FALSE.
6144  *
6145  * Return value: %TRUE if the widget was activatable
6146  **/
6147 gboolean
6148 gtk_widget_activate (GtkWidget *widget)
6149 {
6150   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6151
6152   if (WIDGET_CLASS (widget)->activate_signal)
6153     {
6154       /* FIXME: we should eventually check the signals signature here */
6155       g_signal_emit (widget, WIDGET_CLASS (widget)->activate_signal, 0);
6156
6157       return TRUE;
6158     }
6159   else
6160     return FALSE;
6161 }
6162
6163 static void
6164 gtk_widget_reparent_subwindows (GtkWidget *widget,
6165                                 GdkWindow *new_window)
6166 {
6167   GtkWidgetPrivate *priv = widget->priv;
6168
6169   if (!gtk_widget_get_has_window (widget))
6170     {
6171       GList *children = gdk_window_get_children (priv->window);
6172       GList *tmp_list;
6173
6174       for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
6175         {
6176           GdkWindow *window = tmp_list->data;
6177           gpointer child;
6178
6179           gdk_window_get_user_data (window, &child);
6180           while (child && child != widget)
6181             child = ((GtkWidget*) child)->priv->parent;
6182
6183           if (child)
6184             gdk_window_reparent (window, new_window, 0, 0);
6185         }
6186
6187       g_list_free (children);
6188     }
6189   else
6190    {
6191      GdkWindow *parent;
6192      GList *tmp_list, *children;
6193
6194      parent = gdk_window_get_parent (priv->window);
6195
6196      if (parent == NULL)
6197        gdk_window_reparent (priv->window, new_window, 0, 0);
6198      else
6199        {
6200          children = gdk_window_get_children (parent);
6201
6202          for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
6203            {
6204              GdkWindow *window = tmp_list->data;
6205              gpointer child;
6206
6207              gdk_window_get_user_data (window, &child);
6208
6209              if (child == widget)
6210                gdk_window_reparent (window, new_window, 0, 0);
6211            }
6212
6213          g_list_free (children);
6214        }
6215    }
6216 }
6217
6218 static void
6219 gtk_widget_reparent_fixup_child (GtkWidget *widget,
6220                                  gpointer   client_data)
6221 {
6222   GtkWidgetPrivate *priv = widget->priv;
6223
6224   g_assert (client_data != NULL);
6225
6226   if (!gtk_widget_get_has_window (widget))
6227     {
6228       if (priv->window)
6229         g_object_unref (priv->window);
6230       priv->window = (GdkWindow*) client_data;
6231       if (priv->window)
6232         g_object_ref (priv->window);
6233
6234       if (GTK_IS_CONTAINER (widget))
6235         gtk_container_forall (GTK_CONTAINER (widget),
6236                               gtk_widget_reparent_fixup_child,
6237                               client_data);
6238     }
6239 }
6240
6241 /**
6242  * gtk_widget_reparent:
6243  * @widget: a #GtkWidget
6244  * @new_parent: a #GtkContainer to move the widget into
6245  *
6246  * Moves a widget from one #GtkContainer to another, handling reference
6247  * count issues to avoid destroying the widget.
6248  **/
6249 void
6250 gtk_widget_reparent (GtkWidget *widget,
6251                      GtkWidget *new_parent)
6252 {
6253   GtkWidgetPrivate *priv;
6254
6255   g_return_if_fail (GTK_IS_WIDGET (widget));
6256   g_return_if_fail (GTK_IS_CONTAINER (new_parent));
6257   priv = widget->priv;
6258   g_return_if_fail (priv->parent != NULL);
6259
6260   if (priv->parent != new_parent)
6261     {
6262       /* First try to see if we can get away without unrealizing
6263        * the widget as we reparent it. if so we set a flag so
6264        * that gtk_widget_unparent doesn't unrealize widget
6265        */
6266       if (gtk_widget_get_realized (widget) && gtk_widget_get_realized (new_parent))
6267         priv->in_reparent = TRUE;
6268
6269       g_object_ref (widget);
6270       gtk_container_remove (GTK_CONTAINER (priv->parent), widget);
6271       gtk_container_add (GTK_CONTAINER (new_parent), widget);
6272       g_object_unref (widget);
6273
6274       if (priv->in_reparent)
6275         {
6276           priv->in_reparent = FALSE;
6277
6278           gtk_widget_reparent_subwindows (widget, gtk_widget_get_parent_window (widget));
6279           gtk_widget_reparent_fixup_child (widget,
6280                                            gtk_widget_get_parent_window (widget));
6281         }
6282
6283       g_object_notify (G_OBJECT (widget), "parent");
6284     }
6285 }
6286
6287 /**
6288  * gtk_widget_intersect:
6289  * @widget: a #GtkWidget
6290  * @area: a rectangle
6291  * @intersection: rectangle to store intersection of @widget and @area
6292  *
6293  * Computes the intersection of a @widget's area and @area, storing
6294  * the intersection in @intersection, and returns %TRUE if there was
6295  * an intersection.  @intersection may be %NULL if you're only
6296  * interested in whether there was an intersection.
6297  *
6298  * Return value: %TRUE if there was an intersection
6299  **/
6300 gboolean
6301 gtk_widget_intersect (GtkWidget          *widget,
6302                       const GdkRectangle *area,
6303                       GdkRectangle       *intersection)
6304 {
6305   GtkWidgetPrivate *priv;
6306   GdkRectangle *dest;
6307   GdkRectangle tmp;
6308   gint return_val;
6309
6310   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6311   g_return_val_if_fail (area != NULL, FALSE);
6312
6313   priv = widget->priv;
6314
6315   if (intersection)
6316     dest = intersection;
6317   else
6318     dest = &tmp;
6319
6320   return_val = gdk_rectangle_intersect (&priv->allocation, area, dest);
6321
6322   if (return_val && intersection && gtk_widget_get_has_window (widget))
6323     {
6324       intersection->x -= priv->allocation.x;
6325       intersection->y -= priv->allocation.y;
6326     }
6327
6328   return return_val;
6329 }
6330
6331 /**
6332  * gtk_widget_region_intersect:
6333  * @widget: a #GtkWidget
6334  * @region: a #cairo_region_t, in the same coordinate system as
6335  *          @widget->allocation. That is, relative to @widget->window
6336  *          for %NO_WINDOW widgets; relative to the parent window
6337  *          of @widget->window for widgets with their own window.
6338  * @returns: A newly allocated region holding the intersection of @widget
6339  *           and @region. The coordinates of the return value are
6340  *           relative to @widget->window for %NO_WINDOW widgets, and
6341  *           relative to the parent window of @widget->window for
6342  *           widgets with their own window.
6343  *
6344  * Computes the intersection of a @widget's area and @region, returning
6345  * the intersection. The result may be empty, use cairo_region_is_empty() to
6346  * check.
6347  **/
6348 cairo_region_t *
6349 gtk_widget_region_intersect (GtkWidget       *widget,
6350                              const cairo_region_t *region)
6351 {
6352   GdkRectangle rect;
6353   cairo_region_t *dest;
6354
6355   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6356   g_return_val_if_fail (region != NULL, NULL);
6357
6358   gtk_widget_get_allocation (widget, &rect);
6359
6360   dest = cairo_region_create_rectangle (&rect);
6361
6362   cairo_region_intersect (dest, region);
6363
6364   return dest;
6365 }
6366
6367 /**
6368  * _gtk_widget_grab_notify:
6369  * @widget: a #GtkWidget
6370  * @was_grabbed: whether a grab is now in effect
6371  *
6372  * Emits the #GtkWidget::grab-notify signal on @widget.
6373  *
6374  * Since: 2.6
6375  **/
6376 void
6377 _gtk_widget_grab_notify (GtkWidget *widget,
6378                          gboolean   was_grabbed)
6379 {
6380   g_signal_emit (widget, widget_signals[GRAB_NOTIFY], 0, was_grabbed);
6381 }
6382
6383 /**
6384  * gtk_widget_grab_focus:
6385  * @widget: a #GtkWidget
6386  *
6387  * Causes @widget to have the keyboard focus for the #GtkWindow it's
6388  * inside. @widget must be a focusable widget, such as a #GtkEntry;
6389  * something like #GtkFrame won't work.
6390  *
6391  * More precisely, it must have the %GTK_CAN_FOCUS flag set. Use
6392  * gtk_widget_set_can_focus() to modify that flag.
6393  *
6394  * The widget also needs to be realized and mapped. This is indicated by the
6395  * related signals. Grabbing the focus immediately after creating the widget
6396  * will likely fail and cause critical warnings.
6397  **/
6398 void
6399 gtk_widget_grab_focus (GtkWidget *widget)
6400 {
6401   g_return_if_fail (GTK_IS_WIDGET (widget));
6402
6403   if (!gtk_widget_is_sensitive (widget))
6404     return;
6405
6406   g_object_ref (widget);
6407   g_signal_emit (widget, widget_signals[GRAB_FOCUS], 0);
6408   g_object_notify (G_OBJECT (widget), "has-focus");
6409   g_object_unref (widget);
6410 }
6411
6412 static void
6413 reset_focus_recurse (GtkWidget *widget,
6414                      gpointer   data)
6415 {
6416   if (GTK_IS_CONTAINER (widget))
6417     {
6418       GtkContainer *container;
6419
6420       container = GTK_CONTAINER (widget);
6421       gtk_container_set_focus_child (container, NULL);
6422
6423       gtk_container_foreach (container,
6424                              reset_focus_recurse,
6425                              NULL);
6426     }
6427 }
6428
6429 static void
6430 gtk_widget_real_grab_focus (GtkWidget *focus_widget)
6431 {
6432   if (gtk_widget_get_can_focus (focus_widget))
6433     {
6434       GtkWidget *toplevel;
6435       GtkWidget *widget;
6436
6437       /* clear the current focus setting, break if the current widget
6438        * is the focus widget's parent, since containers above that will
6439        * be set by the next loop.
6440        */
6441       toplevel = gtk_widget_get_toplevel (focus_widget);
6442       if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
6443         {
6444           widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
6445
6446           if (widget == focus_widget)
6447             {
6448               /* We call _gtk_window_internal_set_focus() here so that the
6449                * toplevel window can request the focus if necessary.
6450                * This is needed when the toplevel is a GtkPlug
6451                */
6452               if (!gtk_widget_has_focus (widget))
6453                 _gtk_window_internal_set_focus (GTK_WINDOW (toplevel), focus_widget);
6454
6455               return;
6456             }
6457
6458           if (widget)
6459             {
6460               while (widget->priv->parent && widget->priv->parent != focus_widget->priv->parent)
6461                 {
6462                   widget = widget->priv->parent;
6463                   gtk_container_set_focus_child (GTK_CONTAINER (widget), NULL);
6464                 }
6465             }
6466         }
6467       else if (toplevel != focus_widget)
6468         {
6469           /* gtk_widget_grab_focus() operates on a tree without window...
6470            * actually, this is very questionable behaviour.
6471            */
6472
6473           gtk_container_foreach (GTK_CONTAINER (toplevel),
6474                                  reset_focus_recurse,
6475                                  NULL);
6476         }
6477
6478       /* now propagate the new focus up the widget tree and finally
6479        * set it on the window
6480        */
6481       widget = focus_widget;
6482       while (widget->priv->parent)
6483         {
6484           gtk_container_set_focus_child (GTK_CONTAINER (widget->priv->parent), widget);
6485           widget = widget->priv->parent;
6486         }
6487       if (GTK_IS_WINDOW (widget))
6488         _gtk_window_internal_set_focus (GTK_WINDOW (widget), focus_widget);
6489     }
6490 }
6491
6492 static gboolean
6493 gtk_widget_real_query_tooltip (GtkWidget  *widget,
6494                                gint        x,
6495                                gint        y,
6496                                gboolean    keyboard_tip,
6497                                GtkTooltip *tooltip)
6498 {
6499   gchar *tooltip_markup;
6500   gboolean has_tooltip;
6501
6502   tooltip_markup = g_object_get_qdata (G_OBJECT (widget), quark_tooltip_markup);
6503   has_tooltip = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget), quark_has_tooltip));
6504
6505   if (has_tooltip && tooltip_markup)
6506     {
6507       gtk_tooltip_set_markup (tooltip, tooltip_markup);
6508       return TRUE;
6509     }
6510
6511   return FALSE;
6512 }
6513
6514 static void
6515 gtk_widget_real_style_updated (GtkWidget *widget)
6516 {
6517   GtkWidgetPrivate *priv = widget->priv;
6518
6519   gtk_widget_update_pango_context (widget);
6520
6521   if (priv->style != NULL &&
6522       priv->style != gtk_widget_get_default_style ())
6523     {
6524       /* Trigger ::style-set for old
6525        * widgets not listening to this
6526        */
6527       g_signal_emit (widget,
6528                      widget_signals[STYLE_SET],
6529                      0,
6530                      widget->priv->style);
6531     }
6532
6533   if (widget->priv->context)
6534     {
6535       if (gtk_widget_get_realized (widget) &&
6536           gtk_widget_get_has_window (widget))
6537         gtk_style_context_set_background (widget->priv->context,
6538                                           widget->priv->window);
6539     }
6540
6541   if (widget->priv->anchored)
6542     gtk_widget_queue_resize (widget);
6543 }
6544
6545 static gboolean
6546 gtk_widget_real_show_help (GtkWidget        *widget,
6547                            GtkWidgetHelpType help_type)
6548 {
6549   if (help_type == GTK_WIDGET_HELP_TOOLTIP)
6550     {
6551       _gtk_tooltip_toggle_keyboard_mode (widget);
6552
6553       return TRUE;
6554     }
6555   else
6556     return FALSE;
6557 }
6558
6559 static gboolean
6560 gtk_widget_real_focus (GtkWidget         *widget,
6561                        GtkDirectionType   direction)
6562 {
6563   if (!gtk_widget_get_can_focus (widget))
6564     return FALSE;
6565
6566   if (!gtk_widget_is_focus (widget))
6567     {
6568       gtk_widget_grab_focus (widget);
6569       return TRUE;
6570     }
6571   else
6572     return FALSE;
6573 }
6574
6575 static void
6576 gtk_widget_real_move_focus (GtkWidget         *widget,
6577                             GtkDirectionType   direction)
6578 {
6579   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
6580
6581   if (widget != toplevel && GTK_IS_WINDOW (toplevel))
6582     {
6583       g_signal_emit (toplevel, widget_signals[MOVE_FOCUS], 0,
6584                      direction);
6585     }
6586 }
6587
6588 static gboolean
6589 gtk_widget_real_keynav_failed (GtkWidget        *widget,
6590                                GtkDirectionType  direction)
6591 {
6592   gboolean cursor_only;
6593
6594   switch (direction)
6595     {
6596     case GTK_DIR_TAB_FORWARD:
6597     case GTK_DIR_TAB_BACKWARD:
6598       return FALSE;
6599
6600     case GTK_DIR_UP:
6601     case GTK_DIR_DOWN:
6602     case GTK_DIR_LEFT:
6603     case GTK_DIR_RIGHT:
6604       g_object_get (gtk_widget_get_settings (widget),
6605                     "gtk-keynav-cursor-only", &cursor_only,
6606                     NULL);
6607       if (cursor_only)
6608         return FALSE;
6609       break;
6610     }
6611
6612   gtk_widget_error_bell (widget);
6613
6614   return TRUE;
6615 }
6616
6617 /**
6618  * gtk_widget_set_can_focus:
6619  * @widget: a #GtkWidget
6620  * @can_focus: whether or not @widget can own the input focus.
6621  *
6622  * Specifies whether @widget can own the input focus. See
6623  * gtk_widget_grab_focus() for actually setting the input focus on a
6624  * widget.
6625  *
6626  * Since: 2.18
6627  **/
6628 void
6629 gtk_widget_set_can_focus (GtkWidget *widget,
6630                           gboolean   can_focus)
6631 {
6632   g_return_if_fail (GTK_IS_WIDGET (widget));
6633
6634   if (widget->priv->can_focus != can_focus)
6635     {
6636       widget->priv->can_focus = can_focus;
6637
6638       gtk_widget_queue_resize (widget);
6639       g_object_notify (G_OBJECT (widget), "can-focus");
6640     }
6641 }
6642
6643 /**
6644  * gtk_widget_get_can_focus:
6645  * @widget: a #GtkWidget
6646  *
6647  * Determines whether @widget can own the input focus. See
6648  * gtk_widget_set_can_focus().
6649  *
6650  * Return value: %TRUE if @widget can own the input focus, %FALSE otherwise
6651  *
6652  * Since: 2.18
6653  **/
6654 gboolean
6655 gtk_widget_get_can_focus (GtkWidget *widget)
6656 {
6657   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6658
6659   return widget->priv->can_focus;
6660 }
6661
6662 /**
6663  * gtk_widget_has_focus:
6664  * @widget: a #GtkWidget
6665  *
6666  * Determines if the widget has the global input focus. See
6667  * gtk_widget_is_focus() for the difference between having the global
6668  * input focus, and only having the focus within a toplevel.
6669  *
6670  * Return value: %TRUE if the widget has the global input focus.
6671  *
6672  * Since: 2.18
6673  **/
6674 gboolean
6675 gtk_widget_has_focus (GtkWidget *widget)
6676 {
6677   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6678
6679   return widget->priv->has_focus;
6680 }
6681
6682 /**
6683  * gtk_widget_is_focus:
6684  * @widget: a #GtkWidget
6685  *
6686  * Determines if the widget is the focus widget within its
6687  * toplevel. (This does not mean that the %HAS_FOCUS flag is
6688  * necessarily set; %HAS_FOCUS will only be set if the
6689  * toplevel widget additionally has the global input focus.)
6690  *
6691  * Return value: %TRUE if the widget is the focus widget.
6692  **/
6693 gboolean
6694 gtk_widget_is_focus (GtkWidget *widget)
6695 {
6696   GtkWidget *toplevel;
6697
6698   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6699
6700   toplevel = gtk_widget_get_toplevel (widget);
6701
6702   if (GTK_IS_WINDOW (toplevel))
6703     return widget == gtk_window_get_focus (GTK_WINDOW (toplevel));
6704   else
6705     return FALSE;
6706 }
6707
6708 /**
6709  * gtk_widget_set_can_default:
6710  * @widget: a #GtkWidget
6711  * @can_default: whether or not @widget can be a default widget.
6712  *
6713  * Specifies whether @widget can be a default widget. See
6714  * gtk_widget_grab_default() for details about the meaning of
6715  * "default".
6716  *
6717  * Since: 2.18
6718  **/
6719 void
6720 gtk_widget_set_can_default (GtkWidget *widget,
6721                             gboolean   can_default)
6722 {
6723   g_return_if_fail (GTK_IS_WIDGET (widget));
6724
6725   if (widget->priv->can_default != can_default)
6726     {
6727       widget->priv->can_default = can_default;
6728
6729       gtk_widget_queue_resize (widget);
6730       g_object_notify (G_OBJECT (widget), "can-default");
6731     }
6732 }
6733
6734 /**
6735  * gtk_widget_get_can_default:
6736  * @widget: a #GtkWidget
6737  *
6738  * Determines whether @widget can be a default widget. See
6739  * gtk_widget_set_can_default().
6740  *
6741  * Return value: %TRUE if @widget can be a default widget, %FALSE otherwise
6742  *
6743  * Since: 2.18
6744  **/
6745 gboolean
6746 gtk_widget_get_can_default (GtkWidget *widget)
6747 {
6748   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6749
6750   return widget->priv->can_default;
6751 }
6752
6753 /**
6754  * gtk_widget_has_default:
6755  * @widget: a #GtkWidget
6756  *
6757  * Determines whether @widget is the current default widget within its
6758  * toplevel. See gtk_widget_set_can_default().
6759  *
6760  * Return value: %TRUE if @widget is the current default widget within
6761  *     its toplevel, %FALSE otherwise
6762  *
6763  * Since: 2.18
6764  */
6765 gboolean
6766 gtk_widget_has_default (GtkWidget *widget)
6767 {
6768   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6769
6770   return widget->priv->has_default;
6771 }
6772
6773 void
6774 _gtk_widget_set_has_default (GtkWidget *widget,
6775                              gboolean   has_default)
6776 {
6777   widget->priv->has_default = has_default;
6778 }
6779
6780 /**
6781  * gtk_widget_grab_default:
6782  * @widget: a #GtkWidget
6783  *
6784  * Causes @widget to become the default widget. @widget must have the
6785  * %GTK_CAN_DEFAULT flag set; typically you have to set this flag
6786  * yourself by calling <literal>gtk_widget_set_can_default (@widget,
6787  * %TRUE)</literal>. The default widget is activated when
6788  * the user presses Enter in a window. Default widgets must be
6789  * activatable, that is, gtk_widget_activate() should affect them. Note
6790  * that #GtkEntry widgets require the "activates-default" property
6791  * set to %TRUE before they activate the default widget when Enter
6792  * is pressed and the #GtkEntry is focused.
6793  **/
6794 void
6795 gtk_widget_grab_default (GtkWidget *widget)
6796 {
6797   GtkWidget *window;
6798
6799   g_return_if_fail (GTK_IS_WIDGET (widget));
6800   g_return_if_fail (gtk_widget_get_can_default (widget));
6801
6802   window = gtk_widget_get_toplevel (widget);
6803
6804   if (window && gtk_widget_is_toplevel (window))
6805     gtk_window_set_default (GTK_WINDOW (window), widget);
6806   else
6807     g_warning (G_STRLOC ": widget not within a GtkWindow");
6808 }
6809
6810 /**
6811  * gtk_widget_set_receives_default:
6812  * @widget: a #GtkWidget
6813  * @receives_default: whether or not @widget can be a default widget.
6814  *
6815  * Specifies whether @widget will be treated as the default widget
6816  * within its toplevel when it has the focus, even if another widget
6817  * is the default.
6818  *
6819  * See gtk_widget_grab_default() for details about the meaning of
6820  * "default".
6821  *
6822  * Since: 2.18
6823  **/
6824 void
6825 gtk_widget_set_receives_default (GtkWidget *widget,
6826                                  gboolean   receives_default)
6827 {
6828   g_return_if_fail (GTK_IS_WIDGET (widget));
6829
6830   if (widget->priv->receives_default != receives_default)
6831     {
6832       widget->priv->receives_default = receives_default;
6833
6834       g_object_notify (G_OBJECT (widget), "receives-default");
6835     }
6836 }
6837
6838 /**
6839  * gtk_widget_get_receives_default:
6840  * @widget: a #GtkWidget
6841  *
6842  * Determines whether @widget is alyways treated as default widget
6843  * withing its toplevel when it has the focus, even if another widget
6844  * is the default.
6845  *
6846  * See gtk_widget_set_receives_default().
6847  *
6848  * Return value: %TRUE if @widget acts as default widget when focussed,
6849  *               %FALSE otherwise
6850  *
6851  * Since: 2.18
6852  **/
6853 gboolean
6854 gtk_widget_get_receives_default (GtkWidget *widget)
6855 {
6856   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6857
6858   return widget->priv->receives_default;
6859 }
6860
6861 /**
6862  * gtk_widget_has_grab:
6863  * @widget: a #GtkWidget
6864  *
6865  * Determines whether the widget is currently grabbing events, so it
6866  * is the only widget receiving input events (keyboard and mouse).
6867  *
6868  * See also gtk_grab_add().
6869  *
6870  * Return value: %TRUE if the widget is in the grab_widgets stack
6871  *
6872  * Since: 2.18
6873  **/
6874 gboolean
6875 gtk_widget_has_grab (GtkWidget *widget)
6876 {
6877   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6878
6879   return widget->priv->has_grab;
6880 }
6881
6882 void
6883 _gtk_widget_set_has_grab (GtkWidget *widget,
6884                           gboolean   has_grab)
6885 {
6886   widget->priv->has_grab = has_grab;
6887 }
6888
6889 /**
6890  * gtk_widget_device_is_shadowed:
6891  * @widget: a #GtkWidget
6892  * @device: a #GdkDevice
6893  *
6894  * Returns %TRUE if @device has been shadowed by a GTK+
6895  * device grab on another widget, so it would stop sending
6896  * events to @widget. This may be used in the
6897  * #GtkWidget::grab-notify signal to check for specific
6898  * devices. See gtk_device_grab_add().
6899  *
6900  * Returns: %TRUE if there is an ongoing grab on @device
6901  *          by another #GtkWidget than @widget.
6902  *
6903  * Since: 3.0
6904  **/
6905 gboolean
6906 gtk_widget_device_is_shadowed (GtkWidget *widget,
6907                                GdkDevice *device)
6908 {
6909   GtkWindowGroup *group;
6910   GtkWidget *grab_widget, *toplevel;
6911
6912   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6913   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
6914
6915   if (!gtk_widget_get_realized (widget))
6916     return TRUE;
6917
6918   toplevel = gtk_widget_get_toplevel (widget);
6919
6920   if (GTK_IS_WINDOW (toplevel))
6921     group = gtk_window_get_group (GTK_WINDOW (toplevel));
6922   else
6923     group = gtk_window_get_group (NULL);
6924
6925   grab_widget = gtk_window_group_get_current_device_grab (group, device);
6926
6927   /* Widget not inside the hierarchy of grab_widget */
6928   if (grab_widget &&
6929       widget != grab_widget &&
6930       !gtk_widget_is_ancestor (widget, grab_widget))
6931     return TRUE;
6932
6933   grab_widget = gtk_window_group_get_current_grab (group);
6934   if (grab_widget && widget != grab_widget &&
6935       !gtk_widget_is_ancestor (widget, grab_widget))
6936     return TRUE;
6937
6938   return FALSE;
6939 }
6940
6941 /**
6942  * gtk_widget_set_name:
6943  * @widget: a #GtkWidget
6944  * @name: name for the widget
6945  *
6946  * Widgets can be named, which allows you to refer to them from a
6947  * CSS file. You can apply a style to widgets with a particular name
6948  * in the CSS file. See the documentation for the CSS syntax (on the
6949  * same page as the docs for #GtkStyleContext).
6950  *
6951  * Note that the CSS syntax has certain special characters to delimit
6952  * and represent elements in a selector (period, &num;, &gt;, &ast;...),
6953  * so using these will make your widget impossible to match by name.
6954  * Any combination of alphanumeric symbols, dashes and underscores will
6955  * suffice.
6956  **/
6957 void
6958 gtk_widget_set_name (GtkWidget   *widget,
6959                      const gchar *name)
6960 {
6961   GtkWidgetPrivate *priv;
6962   gchar *new_name;
6963
6964   g_return_if_fail (GTK_IS_WIDGET (widget));
6965
6966   priv = widget->priv;
6967
6968   new_name = g_strdup (name);
6969   g_free (priv->name);
6970   priv->name = new_name;
6971
6972   gtk_widget_reset_style (widget);
6973
6974   g_object_notify (G_OBJECT (widget), "name");
6975 }
6976
6977 /**
6978  * gtk_widget_get_name:
6979  * @widget: a #GtkWidget
6980  *
6981  * Retrieves the name of a widget. See gtk_widget_set_name() for the
6982  * significance of widget names.
6983  *
6984  * Return value: name of the widget. This string is owned by GTK+ and
6985  * should not be modified or freed
6986  **/
6987 const gchar*
6988 gtk_widget_get_name (GtkWidget *widget)
6989 {
6990   GtkWidgetPrivate *priv;
6991
6992   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6993
6994   priv = widget->priv;
6995
6996   if (priv->name)
6997     return priv->name;
6998   return G_OBJECT_TYPE_NAME (widget);
6999 }
7000
7001 static void
7002 _gtk_widget_update_state_flags (GtkWidget     *widget,
7003                                 GtkStateFlags  flags,
7004                                 guint          operation)
7005 {
7006   GtkWidgetPrivate *priv;
7007
7008   priv = widget->priv;
7009
7010   /* Handle insensitive first, since it is propagated
7011    * differently throughout the widget hierarchy.
7012    */
7013   if ((priv->state_flags & GTK_STATE_FLAG_INSENSITIVE) && (flags & GTK_STATE_FLAG_INSENSITIVE) && (operation == STATE_CHANGE_UNSET))
7014     gtk_widget_set_sensitive (widget, TRUE);
7015   else if (!(priv->state_flags & GTK_STATE_FLAG_INSENSITIVE) && (flags & GTK_STATE_FLAG_INSENSITIVE) && (operation != STATE_CHANGE_UNSET))
7016     gtk_widget_set_sensitive (widget, FALSE);
7017   else if ((priv->state_flags & GTK_STATE_FLAG_INSENSITIVE) && !(flags & GTK_STATE_FLAG_INSENSITIVE) && (operation == STATE_CHANGE_REPLACE))
7018     gtk_widget_set_sensitive (widget, TRUE);
7019
7020   if (operation != STATE_CHANGE_REPLACE)
7021     flags &= ~(GTK_STATE_FLAG_INSENSITIVE);
7022
7023   if (flags != 0 ||
7024       operation == STATE_CHANGE_REPLACE)
7025     {
7026       GtkStateData data;
7027
7028       data.flags = flags;
7029       data.operation = operation;
7030       data.use_forall = FALSE;
7031
7032       gtk_widget_propagate_state (widget, &data);
7033
7034       gtk_widget_queue_resize (widget);
7035     }
7036 }
7037
7038 /**
7039  * gtk_widget_set_state_flags:
7040  * @widget: a #GtkWidget
7041  * @flags: State flags to turn on
7042  * @clear: Whether to clear state before turning on @flags
7043  *
7044  * This function is for use in widget implementations. Turns on flag
7045  * values in the current widget state (insensitive, prelighted, etc.).
7046  *
7047  * It is worth mentioning that any other state than %GTK_STATE_FLAG_INSENSITIVE,
7048  * will be propagated down to all non-internal children if @widget is a
7049  * #GtkContainer, while %GTK_STATE_FLAG_INSENSITIVE itself will be propagated
7050  * down to all #GtkContainer children by different means than turning on the
7051  * state flag down the hierarchy, both gtk_widget_get_state_flags() and
7052  * gtk_widget_is_sensitive() will make use of these.
7053  *
7054  * Since: 3.0
7055  **/
7056 void
7057 gtk_widget_set_state_flags (GtkWidget     *widget,
7058                             GtkStateFlags  flags,
7059                             gboolean       clear)
7060 {
7061   g_return_if_fail (GTK_IS_WIDGET (widget));
7062
7063   if ((!clear && (widget->priv->state_flags & flags) == flags) ||
7064       (clear && widget->priv->state_flags == flags))
7065     return;
7066
7067   if (clear)
7068     _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_REPLACE);
7069   else
7070     _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_SET);
7071 }
7072
7073 /**
7074  * gtk_widget_unset_state_flags:
7075  * @widget: a #GtkWidget
7076  * @flags: State flags to turn off
7077  *
7078  * This function is for use in widget implementations. Turns off flag
7079  * values for the current widget state (insensitive, prelighted, etc.).
7080  * See gtk_widget_set_state_flags().
7081  *
7082  * Since: 3.0
7083  **/
7084 void
7085 gtk_widget_unset_state_flags (GtkWidget     *widget,
7086                               GtkStateFlags  flags)
7087 {
7088   g_return_if_fail (GTK_IS_WIDGET (widget));
7089
7090   if ((widget->priv->state_flags & flags) == 0)
7091     return;
7092
7093   _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_UNSET);
7094 }
7095
7096 /**
7097  * gtk_widget_get_state_flags:
7098  * @widget: a #GtkWidget
7099  *
7100  * Returns the widget state as a flag set. It is worth mentioning
7101  * that the effective %GTK_STATE_FLAG_INSENSITIVE state will be
7102  * returned, that is, also based on parent insensitivity, even if
7103  * @widget itself is sensitive.
7104  *
7105  * Returns: The state flags for widget
7106  *
7107  * Since: 3.0
7108  **/
7109 GtkStateFlags
7110 gtk_widget_get_state_flags (GtkWidget *widget)
7111 {
7112   GtkStateFlags flags;
7113
7114   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
7115
7116   flags = widget->priv->state_flags;
7117
7118   if (gtk_widget_has_focus (widget))
7119     flags |= GTK_STATE_FLAG_FOCUSED;
7120
7121   return flags;
7122 }
7123
7124 /**
7125  * gtk_widget_set_state:
7126  * @widget: a #GtkWidget
7127  * @state: new state for @widget
7128  *
7129  * This function is for use in widget implementations. Sets the state
7130  * of a widget (insensitive, prelighted, etc.) Usually you should set
7131  * the state using wrapper functions such as gtk_widget_set_sensitive().
7132  *
7133  * Deprecated: 3.0. Use gtk_widget_set_state_flags() instead.
7134  **/
7135 void
7136 gtk_widget_set_state (GtkWidget           *widget,
7137                       GtkStateType         state)
7138 {
7139   GtkStateFlags flags;
7140
7141   if (state == gtk_widget_get_state (widget))
7142     return;
7143
7144   switch (state)
7145     {
7146     case GTK_STATE_ACTIVE:
7147       flags = GTK_STATE_FLAG_ACTIVE;
7148       break;
7149     case GTK_STATE_PRELIGHT:
7150       flags = GTK_STATE_FLAG_PRELIGHT;
7151       break;
7152     case GTK_STATE_SELECTED:
7153       flags = GTK_STATE_FLAG_SELECTED;
7154       break;
7155     case GTK_STATE_INSENSITIVE:
7156       flags = GTK_STATE_FLAG_INSENSITIVE;
7157       break;
7158     case GTK_STATE_INCONSISTENT:
7159       flags = GTK_STATE_FLAG_INCONSISTENT;
7160       break;
7161     case GTK_STATE_FOCUSED:
7162       flags = GTK_STATE_FLAG_FOCUSED;
7163       break;
7164     case GTK_STATE_NORMAL:
7165     default:
7166       flags = 0;
7167       break;
7168     }
7169
7170   _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_REPLACE);
7171 }
7172
7173 /**
7174  * gtk_widget_get_state:
7175  * @widget: a #GtkWidget
7176  *
7177  * Returns the widget's state. See gtk_widget_set_state().
7178  *
7179  * Returns: the state of @widget.
7180  *
7181  * Since: 2.18
7182  *
7183  * Deprecated: 3.0. Use gtk_widget_get_state_flags() instead.
7184  */
7185 GtkStateType
7186 gtk_widget_get_state (GtkWidget *widget)
7187 {
7188   GtkStateFlags flags;
7189
7190   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_STATE_NORMAL);
7191
7192   flags = gtk_widget_get_state_flags (widget);
7193
7194   if (flags & GTK_STATE_FLAG_INSENSITIVE)
7195     return GTK_STATE_INSENSITIVE;
7196   else if (flags & GTK_STATE_FLAG_ACTIVE)
7197     return GTK_STATE_ACTIVE;
7198   else if (flags & GTK_STATE_FLAG_SELECTED)
7199     return GTK_STATE_SELECTED;
7200   else if (flags & GTK_STATE_FLAG_PRELIGHT)
7201     return GTK_STATE_PRELIGHT;
7202   else
7203     return GTK_STATE_NORMAL;
7204 }
7205
7206 /**
7207  * gtk_widget_set_visible:
7208  * @widget: a #GtkWidget
7209  * @visible: whether the widget should be shown or not
7210  *
7211  * Sets the visibility state of @widget. Note that setting this to
7212  * %TRUE doesn't mean the widget is actually viewable, see
7213  * gtk_widget_get_visible().
7214  *
7215  * This function simply calls gtk_widget_show() or gtk_widget_hide()
7216  * but is nicer to use when the visibility of the widget depends on
7217  * some condition.
7218  *
7219  * Since: 2.18
7220  **/
7221 void
7222 gtk_widget_set_visible (GtkWidget *widget,
7223                         gboolean   visible)
7224 {
7225   g_return_if_fail (GTK_IS_WIDGET (widget));
7226
7227   if (visible != gtk_widget_get_visible (widget))
7228     {
7229       if (visible)
7230         gtk_widget_show (widget);
7231       else
7232         gtk_widget_hide (widget);
7233     }
7234 }
7235
7236 void
7237 _gtk_widget_set_visible_flag (GtkWidget *widget,
7238                               gboolean   visible)
7239 {
7240   widget->priv->visible = visible;
7241 }
7242
7243 /**
7244  * gtk_widget_get_visible:
7245  * @widget: a #GtkWidget
7246  *
7247  * Determines whether the widget is visible. Note that this doesn't
7248  * take into account whether the widget's parent is also visible
7249  * or the widget is obscured in any way.
7250  *
7251  * See gtk_widget_set_visible().
7252  *
7253  * Return value: %TRUE if the widget is visible
7254  *
7255  * Since: 2.18
7256  **/
7257 gboolean
7258 gtk_widget_get_visible (GtkWidget *widget)
7259 {
7260   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7261
7262   return widget->priv->visible;
7263 }
7264
7265 /**
7266  * gtk_widget_set_has_window:
7267  * @widget: a #GtkWidget
7268  * @has_window: whether or not @widget has a window.
7269  *
7270  * Specifies whether @widget has a #GdkWindow of its own. Note that
7271  * all realized widgets have a non-%NULL "window" pointer
7272  * (gtk_widget_get_window() never returns a %NULL window when a widget
7273  * is realized), but for many of them it's actually the #GdkWindow of
7274  * one of its parent widgets. Widgets that do not create a %window for
7275  * themselves in #GtkWidget::realize must announce this by
7276  * calling this function with @has_window = %FALSE.
7277  *
7278  * This function should only be called by widget implementations,
7279  * and they should call it in their init() function.
7280  *
7281  * Since: 2.18
7282  **/
7283 void
7284 gtk_widget_set_has_window (GtkWidget *widget,
7285                            gboolean   has_window)
7286 {
7287   g_return_if_fail (GTK_IS_WIDGET (widget));
7288
7289   widget->priv->no_window = !has_window;
7290 }
7291
7292 /**
7293  * gtk_widget_get_has_window:
7294  * @widget: a #GtkWidget
7295  *
7296  * Determines whether @widget has a #GdkWindow of its own. See
7297  * gtk_widget_set_has_window().
7298  *
7299  * Return value: %TRUE if @widget has a window, %FALSE otherwise
7300  *
7301  * Since: 2.18
7302  **/
7303 gboolean
7304 gtk_widget_get_has_window (GtkWidget *widget)
7305 {
7306   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7307
7308   return ! widget->priv->no_window;
7309 }
7310
7311 /**
7312  * gtk_widget_is_toplevel:
7313  * @widget: a #GtkWidget
7314  *
7315  * Determines whether @widget is a toplevel widget.
7316  *
7317  * Currently only #GtkWindow and #GtkInvisible (and out-of-process
7318  * #GtkPlugs) are toplevel widgets. Toplevel widgets have no parent
7319  * widget.
7320  *
7321  * Return value: %TRUE if @widget is a toplevel, %FALSE otherwise
7322  *
7323  * Since: 2.18
7324  **/
7325 gboolean
7326 gtk_widget_is_toplevel (GtkWidget *widget)
7327 {
7328   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7329
7330   return widget->priv->toplevel;
7331 }
7332
7333 void
7334 _gtk_widget_set_is_toplevel (GtkWidget *widget,
7335                              gboolean   is_toplevel)
7336 {
7337   widget->priv->toplevel = is_toplevel;
7338 }
7339
7340 /**
7341  * gtk_widget_is_drawable:
7342  * @widget: a #GtkWidget
7343  *
7344  * Determines whether @widget can be drawn to. A widget can be drawn
7345  * to if it is mapped and visible.
7346  *
7347  * Return value: %TRUE if @widget is drawable, %FALSE otherwise
7348  *
7349  * Since: 2.18
7350  **/
7351 gboolean
7352 gtk_widget_is_drawable (GtkWidget *widget)
7353 {
7354   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7355
7356   return (gtk_widget_get_visible (widget) &&
7357           gtk_widget_get_mapped (widget));
7358 }
7359
7360 /**
7361  * gtk_widget_get_realized:
7362  * @widget: a #GtkWidget
7363  *
7364  * Determines whether @widget is realized.
7365  *
7366  * Return value: %TRUE if @widget is realized, %FALSE otherwise
7367  *
7368  * Since: 2.20
7369  **/
7370 gboolean
7371 gtk_widget_get_realized (GtkWidget *widget)
7372 {
7373   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7374
7375   return widget->priv->realized;
7376 }
7377
7378 /**
7379  * gtk_widget_set_realized:
7380  * @widget: a #GtkWidget
7381  * @realized: %TRUE to mark the widget as realized
7382  *
7383  * Marks the widget as being realized.
7384  *
7385  * This function should only ever be called in a derived widget's
7386  * "realize" or "unrealize" implementation.
7387  *
7388  * Since: 2.20
7389  */
7390 void
7391 gtk_widget_set_realized (GtkWidget *widget,
7392                          gboolean   realized)
7393 {
7394   g_return_if_fail (GTK_IS_WIDGET (widget));
7395
7396   widget->priv->realized = realized;
7397 }
7398
7399 /**
7400  * gtk_widget_get_mapped:
7401  * @widget: a #GtkWidget
7402  *
7403  * Whether the widget is mapped.
7404  *
7405  * Return value: %TRUE if the widget is mapped, %FALSE otherwise.
7406  *
7407  * Since: 2.20
7408  */
7409 gboolean
7410 gtk_widget_get_mapped (GtkWidget *widget)
7411 {
7412   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7413
7414   return widget->priv->mapped;
7415 }
7416
7417 /**
7418  * gtk_widget_set_mapped:
7419  * @widget: a #GtkWidget
7420  * @mapped: %TRUE to mark the widget as mapped
7421  *
7422  * Marks the widget as being realized.
7423  *
7424  * This function should only ever be called in a derived widget's
7425  * "map" or "unmap" implementation.
7426  *
7427  * Since: 2.20
7428  */
7429 void
7430 gtk_widget_set_mapped (GtkWidget *widget,
7431                        gboolean   mapped)
7432 {
7433   g_return_if_fail (GTK_IS_WIDGET (widget));
7434
7435   widget->priv->mapped = mapped;
7436 }
7437
7438 /**
7439  * gtk_widget_set_app_paintable:
7440  * @widget: a #GtkWidget
7441  * @app_paintable: %TRUE if the application will paint on the widget
7442  *
7443  * Sets whether the application intends to draw on the widget in
7444  * an #GtkWidget::draw handler.
7445  *
7446  * This is a hint to the widget and does not affect the behavior of
7447  * the GTK+ core; many widgets ignore this flag entirely. For widgets
7448  * that do pay attention to the flag, such as #GtkEventBox and #GtkWindow,
7449  * the effect is to suppress default themed drawing of the widget's
7450  * background. (Children of the widget will still be drawn.) The application
7451  * is then entirely responsible for drawing the widget background.
7452  *
7453  * Note that the background is still drawn when the widget is mapped.
7454  **/
7455 void
7456 gtk_widget_set_app_paintable (GtkWidget *widget,
7457                               gboolean   app_paintable)
7458 {
7459   g_return_if_fail (GTK_IS_WIDGET (widget));
7460
7461   app_paintable = (app_paintable != FALSE);
7462
7463   if (widget->priv->app_paintable != app_paintable)
7464     {
7465       widget->priv->app_paintable = app_paintable;
7466
7467       if (gtk_widget_is_drawable (widget))
7468         gtk_widget_queue_draw (widget);
7469
7470       g_object_notify (G_OBJECT (widget), "app-paintable");
7471     }
7472 }
7473
7474 /**
7475  * gtk_widget_get_app_paintable:
7476  * @widget: a #GtkWidget
7477  *
7478  * Determines whether the application intends to draw on the widget in
7479  * an #GtkWidget::draw handler.
7480  *
7481  * See gtk_widget_set_app_paintable()
7482  *
7483  * Return value: %TRUE if the widget is app paintable
7484  *
7485  * Since: 2.18
7486  **/
7487 gboolean
7488 gtk_widget_get_app_paintable (GtkWidget *widget)
7489 {
7490   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7491
7492   return widget->priv->app_paintable;
7493 }
7494
7495 /**
7496  * gtk_widget_set_double_buffered:
7497  * @widget: a #GtkWidget
7498  * @double_buffered: %TRUE to double-buffer a widget
7499  *
7500  * Widgets are double buffered by default; you can use this function
7501  * to turn off the buffering. "Double buffered" simply means that
7502  * gdk_window_begin_paint_region() and gdk_window_end_paint() are called
7503  * automatically around expose events sent to the
7504  * widget. gdk_window_begin_paint() diverts all drawing to a widget's
7505  * window to an offscreen buffer, and gdk_window_end_paint() draws the
7506  * buffer to the screen. The result is that users see the window
7507  * update in one smooth step, and don't see individual graphics
7508  * primitives being rendered.
7509  *
7510  * In very simple terms, double buffered widgets don't flicker,
7511  * so you would only use this function to turn off double buffering
7512  * if you had special needs and really knew what you were doing.
7513  *
7514  * Note: if you turn off double-buffering, you have to handle
7515  * expose events, since even the clearing to the background color or
7516  * pixmap will not happen automatically (as it is done in
7517  * gdk_window_begin_paint()).
7518  **/
7519 void
7520 gtk_widget_set_double_buffered (GtkWidget *widget,
7521                                 gboolean   double_buffered)
7522 {
7523   g_return_if_fail (GTK_IS_WIDGET (widget));
7524
7525   double_buffered = (double_buffered != FALSE);
7526
7527   if (widget->priv->double_buffered != double_buffered)
7528     {
7529       widget->priv->double_buffered = double_buffered;
7530
7531       g_object_notify (G_OBJECT (widget), "double-buffered");
7532     }
7533 }
7534
7535 /**
7536  * gtk_widget_get_double_buffered:
7537  * @widget: a #GtkWidget
7538  *
7539  * Determines whether the widget is double buffered.
7540  *
7541  * See gtk_widget_set_double_buffered()
7542  *
7543  * Return value: %TRUE if the widget is double buffered
7544  *
7545  * Since: 2.18
7546  **/
7547 gboolean
7548 gtk_widget_get_double_buffered (GtkWidget *widget)
7549 {
7550   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7551
7552   return widget->priv->double_buffered;
7553 }
7554
7555 /**
7556  * gtk_widget_set_redraw_on_allocate:
7557  * @widget: a #GtkWidget
7558  * @redraw_on_allocate: if %TRUE, the entire widget will be redrawn
7559  *   when it is allocated to a new size. Otherwise, only the
7560  *   new portion of the widget will be redrawn.
7561  *
7562  * Sets whether the entire widget is queued for drawing when its size
7563  * allocation changes. By default, this setting is %TRUE and
7564  * the entire widget is redrawn on every size change. If your widget
7565  * leaves the upper left unchanged when made bigger, turning this
7566  * setting off will improve performance.
7567
7568  * Note that for %NO_WINDOW widgets setting this flag to %FALSE turns
7569  * off all allocation on resizing: the widget will not even redraw if
7570  * its position changes; this is to allow containers that don't draw
7571  * anything to avoid excess invalidations. If you set this flag on a
7572  * %NO_WINDOW widget that <emphasis>does</emphasis> draw on @widget->window,
7573  * you are responsible for invalidating both the old and new allocation
7574  * of the widget when the widget is moved and responsible for invalidating
7575  * regions newly when the widget increases size.
7576  **/
7577 void
7578 gtk_widget_set_redraw_on_allocate (GtkWidget *widget,
7579                                    gboolean   redraw_on_allocate)
7580 {
7581   g_return_if_fail (GTK_IS_WIDGET (widget));
7582
7583   widget->priv->redraw_on_alloc = redraw_on_allocate;
7584 }
7585
7586 /**
7587  * gtk_widget_set_sensitive:
7588  * @widget: a #GtkWidget
7589  * @sensitive: %TRUE to make the widget sensitive
7590  *
7591  * Sets the sensitivity of a widget. A widget is sensitive if the user
7592  * can interact with it. Insensitive widgets are "grayed out" and the
7593  * user can't interact with them. Insensitive widgets are known as
7594  * "inactive", "disabled", or "ghosted" in some other toolkits.
7595  **/
7596 void
7597 gtk_widget_set_sensitive (GtkWidget *widget,
7598                           gboolean   sensitive)
7599 {
7600   GtkWidgetPrivate *priv;
7601   GtkStateData data;
7602
7603   g_return_if_fail (GTK_IS_WIDGET (widget));
7604
7605   priv = widget->priv;
7606
7607   sensitive = (sensitive != FALSE);
7608
7609   if (priv->sensitive == sensitive)
7610     return;
7611
7612   data.flags = GTK_STATE_FLAG_INSENSITIVE;
7613
7614   if (sensitive)
7615     {
7616       priv->sensitive = TRUE;
7617       data.operation = STATE_CHANGE_UNSET;
7618     }
7619   else
7620     {
7621       priv->sensitive = FALSE;
7622       data.operation = STATE_CHANGE_SET;
7623     }
7624
7625   data.use_forall = TRUE;
7626
7627   gtk_widget_propagate_state (widget, &data);
7628
7629   gtk_widget_queue_resize (widget);
7630
7631   g_object_notify (G_OBJECT (widget), "sensitive");
7632 }
7633
7634 /**
7635  * gtk_widget_get_sensitive:
7636  * @widget: a #GtkWidget
7637  *
7638  * Returns the widget's sensitivity (in the sense of returning
7639  * the value that has been set using gtk_widget_set_sensitive()).
7640  *
7641  * The effective sensitivity of a widget is however determined by both its
7642  * own and its parent widget's sensitivity. See gtk_widget_is_sensitive().
7643  *
7644  * Returns: %TRUE if the widget is sensitive
7645  *
7646  * Since: 2.18
7647  */
7648 gboolean
7649 gtk_widget_get_sensitive (GtkWidget *widget)
7650 {
7651   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7652
7653   return widget->priv->sensitive;
7654 }
7655
7656 /**
7657  * gtk_widget_is_sensitive:
7658  * @widget: a #GtkWidget
7659  *
7660  * Returns the widget's effective sensitivity, which means
7661  * it is sensitive itself and also its parent widget is sensitive
7662  *
7663  * Returns: %TRUE if the widget is effectively sensitive
7664  *
7665  * Since: 2.18
7666  */
7667 gboolean
7668 gtk_widget_is_sensitive (GtkWidget *widget)
7669 {
7670   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7671
7672   return !(widget->priv->state_flags & GTK_STATE_FLAG_INSENSITIVE);
7673 }
7674
7675 static void
7676 _gtk_widget_update_path (GtkWidget *widget)
7677 {
7678   if (widget->priv->path)
7679     {
7680       gtk_widget_path_free (widget->priv->path);
7681       widget->priv->path = NULL;
7682     }
7683
7684   gtk_widget_get_path (widget);
7685 }
7686
7687 /**
7688  * gtk_widget_set_parent:
7689  * @widget: a #GtkWidget
7690  * @parent: parent container
7691  *
7692  * This function is useful only when implementing subclasses of
7693  * #GtkContainer.
7694  * Sets the container as the parent of @widget, and takes care of
7695  * some details such as updating the state and style of the child
7696  * to reflect its new location. The opposite function is
7697  * gtk_widget_unparent().
7698  **/
7699 void
7700 gtk_widget_set_parent (GtkWidget *widget,
7701                        GtkWidget *parent)
7702 {
7703   GtkStateFlags parent_flags;
7704   GtkWidgetPrivate *priv;
7705   GtkStateData data;
7706
7707   g_return_if_fail (GTK_IS_WIDGET (widget));
7708   g_return_if_fail (GTK_IS_WIDGET (parent));
7709   g_return_if_fail (widget != parent);
7710
7711   priv = widget->priv;
7712
7713   if (priv->parent != NULL)
7714     {
7715       g_warning ("Can't set a parent on widget which has a parent\n");
7716       return;
7717     }
7718   if (gtk_widget_is_toplevel (widget))
7719     {
7720       g_warning ("Can't set a parent on a toplevel widget\n");
7721       return;
7722     }
7723
7724   /* keep this function in sync with gtk_menu_attach_to_widget()
7725    */
7726
7727   g_object_ref_sink (widget);
7728
7729   gtk_widget_push_verify_invariants (widget);
7730
7731   priv->parent = parent;
7732
7733   parent_flags = gtk_widget_get_state_flags (parent);
7734
7735   /* Merge both old state and current parent state,
7736    * making sure to only propagate the right states */
7737   data.flags = parent_flags & GTK_STATE_FLAGS_DO_PROPAGATE;
7738   data.flags |= priv->state_flags;
7739
7740   data.operation = STATE_CHANGE_REPLACE;
7741   data.use_forall = gtk_widget_is_sensitive (parent) != gtk_widget_is_sensitive (widget);
7742   gtk_widget_propagate_state (widget, &data);
7743
7744   gtk_widget_reset_style (widget);
7745
7746   g_signal_emit (widget, widget_signals[PARENT_SET], 0, NULL);
7747   if (priv->parent->priv->anchored)
7748     _gtk_widget_propagate_hierarchy_changed (widget, NULL);
7749   g_object_notify (G_OBJECT (widget), "parent");
7750
7751   /* Enforce realized/mapped invariants
7752    */
7753   if (gtk_widget_get_realized (priv->parent))
7754     gtk_widget_realize (widget);
7755
7756   if (gtk_widget_get_visible (priv->parent) &&
7757       gtk_widget_get_visible (widget))
7758     {
7759       if (gtk_widget_get_child_visible (widget) &&
7760           gtk_widget_get_mapped (priv->parent))
7761         gtk_widget_map (widget);
7762
7763       gtk_widget_queue_resize (widget);
7764     }
7765
7766   /* child may cause parent's expand to change, if the child is
7767    * expanded. If child is not expanded, then it can't modify the
7768    * parent's expand. If the child becomes expanded later then it will
7769    * queue compute_expand then. This optimization plus defaulting
7770    * newly-constructed widgets to need_compute_expand=FALSE should
7771    * mean that initially building a widget tree doesn't have to keep
7772    * walking up setting need_compute_expand on parents over and over.
7773    *
7774    * We can't change a parent to need to expand unless we're visible.
7775    */
7776   if (gtk_widget_get_visible (widget) &&
7777       (priv->need_compute_expand ||
7778        priv->computed_hexpand ||
7779        priv->computed_vexpand))
7780     {
7781       gtk_widget_queue_compute_expand (parent);
7782     }
7783
7784   gtk_widget_pop_verify_invariants (widget);
7785 }
7786
7787 /**
7788  * gtk_widget_get_parent:
7789  * @widget: a #GtkWidget
7790  *
7791  * Returns the parent container of @widget.
7792  *
7793  * Return value: (transfer none): the parent container of @widget, or %NULL
7794  **/
7795 GtkWidget *
7796 gtk_widget_get_parent (GtkWidget *widget)
7797 {
7798   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7799
7800   return widget->priv->parent;
7801 }
7802
7803 /*****************************************
7804  * Widget styles
7805  * see docs/styles.txt
7806  *****************************************/
7807
7808 /**
7809  * gtk_widget_style_attach:
7810  * @widget: a #GtkWidget
7811  *
7812  * This function attaches the widget's #GtkStyle to the widget's
7813  * #GdkWindow. It is a replacement for
7814  *
7815  * <programlisting>
7816  * widget->style = gtk_style_attach (widget->style, widget->window);
7817  * </programlisting>
7818  *
7819  * and should only ever be called in a derived widget's "realize"
7820  * implementation which does not chain up to its parent class'
7821  * "realize" implementation, because one of the parent classes
7822  * (finally #GtkWidget) would attach the style itself.
7823  *
7824  * Since: 2.20
7825  *
7826  * Deprecated: 3.0. This step is unnecessary with #GtkStyleContext.
7827  **/
7828 void
7829 gtk_widget_style_attach (GtkWidget *widget)
7830 {
7831   g_return_if_fail (GTK_IS_WIDGET (widget));
7832   g_return_if_fail (gtk_widget_get_realized (widget));
7833 }
7834
7835 /**
7836  * gtk_widget_has_rc_style:
7837  * @widget: a #GtkWidget
7838  *
7839  * Determines if the widget style has been looked up through the rc mechanism.
7840  *
7841  * Returns: %TRUE if the widget has been looked up through the rc
7842  *   mechanism, %FALSE otherwise.
7843  *
7844  * Since: 2.20
7845  *
7846  * Deprecated:3.0: Use #GtkStyleContext instead
7847  **/
7848 gboolean
7849 gtk_widget_has_rc_style (GtkWidget *widget)
7850 {
7851   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7852
7853   return widget->priv->rc_style;
7854 }
7855
7856 /**
7857  * gtk_widget_set_style:
7858  * @widget: a #GtkWidget
7859  * @style: (allow-none): a #GtkStyle, or %NULL to remove the effect
7860  *     of a previous call to gtk_widget_set_style() and go back to
7861  *     the default style
7862  *
7863  * Used to set the #GtkStyle for a widget (@widget->style). Since
7864  * GTK 3, this function does nothing, the passed in style is ignored.
7865  *
7866  * Deprecated:3.0: Use #GtkStyleContext instead
7867  */
7868 void
7869 gtk_widget_set_style (GtkWidget *widget,
7870                       GtkStyle  *style)
7871 {
7872   g_return_if_fail (GTK_IS_WIDGET (widget));
7873 }
7874
7875 /**
7876  * gtk_widget_ensure_style:
7877  * @widget: a #GtkWidget
7878  *
7879  * Ensures that @widget has a style (@widget->style).
7880  *
7881  * Not a very useful function; most of the time, if you
7882  * want the style, the widget is realized, and realized
7883  * widgets are guaranteed to have a style already.
7884  *
7885  * Deprecated:3.0: Use #GtkStyleContext instead
7886  */
7887 void
7888 gtk_widget_ensure_style (GtkWidget *widget)
7889 {
7890   GtkWidgetPrivate *priv;
7891
7892   g_return_if_fail (GTK_IS_WIDGET (widget));
7893
7894   priv = widget->priv;
7895
7896   if (priv->style == gtk_widget_get_default_style ())
7897     {
7898       g_object_unref (priv->style);
7899
7900       priv->style = NULL;
7901
7902       g_signal_emit (widget,
7903                      widget_signals[STYLE_SET],
7904                      0, NULL);
7905     }
7906 }
7907
7908 /**
7909  * gtk_widget_get_style:
7910  * @widget: a #GtkWidget
7911  *
7912  * Simply an accessor function that returns @widget->style.
7913  *
7914  * Return value: (transfer none): the widget's #GtkStyle
7915  *
7916  * Deprecated:3.0: Use #GtkStyleContext instead
7917  */
7918 GtkStyle*
7919 gtk_widget_get_style (GtkWidget *widget)
7920 {
7921   GtkWidgetPrivate *priv;
7922
7923   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7924
7925   priv = widget->priv;
7926
7927   if (priv->style == NULL)
7928     {
7929       priv->style = g_object_new (GTK_TYPE_STYLE,
7930                                   "context", gtk_widget_get_style_context (widget),
7931                                   NULL);
7932
7933     }
7934
7935   return priv->style;
7936 }
7937
7938 /**
7939  * gtk_widget_modify_style:
7940  * @widget: a #GtkWidget
7941  * @style: the #GtkRcStyle holding the style modifications
7942  *
7943  * Modifies style values on the widget.
7944  *
7945  * Modifications made using this technique take precedence over
7946  * style values set via an RC file, however, they will be overridden
7947  * if a style is explicitely set on the widget using gtk_widget_set_style().
7948  * The #GtkRcStyle structure is designed so each field can either be
7949  * set or unset, so it is possible, using this function, to modify some
7950  * style values and leave the others unchanged.
7951  *
7952  * Note that modifications made with this function are not cumulative
7953  * with previous calls to gtk_widget_modify_style() or with such
7954  * functions as gtk_widget_modify_fg(). If you wish to retain
7955  * previous values, you must first call gtk_widget_get_modifier_style(),
7956  * make your modifications to the returned style, then call
7957  * gtk_widget_modify_style() with that style. On the other hand,
7958  * if you first call gtk_widget_modify_style(), subsequent calls
7959  * to such functions gtk_widget_modify_fg() will have a cumulative
7960  * effect with the initial modifications.
7961  *
7962  * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
7963  */
7964 void
7965 gtk_widget_modify_style (GtkWidget      *widget,
7966                          GtkRcStyle     *style)
7967 {
7968   g_return_if_fail (GTK_IS_WIDGET (widget));
7969   g_return_if_fail (GTK_IS_RC_STYLE (style));
7970
7971   g_object_set_qdata_full (G_OBJECT (widget),
7972                            quark_rc_style,
7973                            gtk_rc_style_copy (style),
7974                            (GDestroyNotify) g_object_unref);
7975 }
7976
7977 /**
7978  * gtk_widget_get_modifier_style:
7979  * @widget: a #GtkWidget
7980  *
7981  * Returns the current modifier style for the widget. (As set by
7982  * gtk_widget_modify_style().) If no style has previously set, a new
7983  * #GtkRcStyle will be created with all values unset, and set as the
7984  * modifier style for the widget. If you make changes to this rc
7985  * style, you must call gtk_widget_modify_style(), passing in the
7986  * returned rc style, to make sure that your changes take effect.
7987  *
7988  * Caution: passing the style back to gtk_widget_modify_style() will
7989  * normally end up destroying it, because gtk_widget_modify_style() copies
7990  * the passed-in style and sets the copy as the new modifier style,
7991  * thus dropping any reference to the old modifier style. Add a reference
7992  * to the modifier style if you want to keep it alive.
7993  *
7994  * Return value: (transfer none): the modifier style for the widget.
7995  *     This rc style is owned by the widget. If you want to keep a
7996  *     pointer to value this around, you must add a refcount using
7997  *     g_object_ref().
7998  *
7999  * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
8000  */
8001 GtkRcStyle *
8002 gtk_widget_get_modifier_style (GtkWidget *widget)
8003 {
8004   GtkRcStyle *rc_style;
8005
8006   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8007
8008   rc_style = g_object_get_qdata (G_OBJECT (widget), quark_rc_style);
8009
8010   if (!rc_style)
8011     {
8012       rc_style = gtk_rc_style_new ();
8013       g_object_set_qdata_full (G_OBJECT (widget),
8014                                quark_rc_style,
8015                                rc_style,
8016                                (GDestroyNotify) g_object_unref);
8017     }
8018
8019   return rc_style;
8020 }
8021
8022 static void
8023 gtk_widget_modify_color_component (GtkWidget      *widget,
8024                                    GtkRcFlags      component,
8025                                    GtkStateType    state,
8026                                    const GdkColor *color)
8027 {
8028   GtkRcStyle *rc_style = gtk_widget_get_modifier_style (widget);
8029
8030   if (color)
8031     {
8032       switch (component)
8033         {
8034         case GTK_RC_FG:
8035           rc_style->fg[state] = *color;
8036           break;
8037         case GTK_RC_BG:
8038           rc_style->bg[state] = *color;
8039           break;
8040         case GTK_RC_TEXT:
8041           rc_style->text[state] = *color;
8042           break;
8043         case GTK_RC_BASE:
8044           rc_style->base[state] = *color;
8045           break;
8046         default:
8047           g_assert_not_reached();
8048         }
8049
8050       rc_style->color_flags[state] |= component;
8051     }
8052   else
8053     rc_style->color_flags[state] &= ~component;
8054
8055   gtk_widget_modify_style (widget, rc_style);
8056 }
8057
8058 static void
8059 modifier_style_changed (GtkModifierStyle *style,
8060                         GtkWidget        *widget)
8061 {
8062   GtkStyleContext *context;
8063
8064   context = gtk_widget_get_style_context (widget);
8065   gtk_style_context_invalidate (context);
8066 }
8067
8068 static GtkModifierStyle *
8069 _gtk_widget_get_modifier_properties (GtkWidget *widget)
8070 {
8071   GtkModifierStyle *style;
8072
8073   style = g_object_get_qdata (G_OBJECT (widget), quark_modifier_style);
8074
8075   if (G_UNLIKELY (!style))
8076     {
8077       GtkStyleContext *context;
8078
8079       style = _gtk_modifier_style_new ();
8080       g_object_set_qdata_full (G_OBJECT (widget),
8081                                quark_modifier_style,
8082                                style,
8083                                (GDestroyNotify) g_object_unref);
8084
8085       g_signal_connect (style, "changed",
8086                         G_CALLBACK (modifier_style_changed), widget);
8087
8088       context = gtk_widget_get_style_context (widget);
8089
8090       gtk_style_context_add_provider (context,
8091                                       GTK_STYLE_PROVIDER (style),
8092                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8093     }
8094
8095   return style;
8096 }
8097
8098 /**
8099  * gtk_widget_override_color:
8100  * @widget: a #GtkWidget
8101  * @state: the state for which to set the color
8102  * @color: (allow-none): the color to assign, or %NULL to undo the effect
8103  *     of previous calls to gtk_widget_override_color()
8104  *
8105  * Sets the color to use for a widget.
8106  *
8107  * All other style values are left untouched.
8108  *
8109  * <note><para>
8110  * This API is mostly meant as a quick way for applications to
8111  * change a widget appearance. If you are developing a widgets
8112  * library and intend this change to be themeable, it is better
8113  * done by setting meaningful CSS classes and regions in your
8114  * widget/container implementation through gtk_style_context_add_class()
8115  * and gtk_style_context_add_region().
8116  * </para><para>
8117  * This way, your widget library can install a #GtkCssProvider
8118  * with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK priority in order
8119  * to provide a default styling for those widgets that need so, and
8120  * this theming may fully overridden by the user's theme.
8121  * </para></note>
8122  * <note><para>
8123  * Note that for complex widgets this may bring in undesired
8124  * results (such as uniform background color everywhere), in
8125  * these cases it is better to fully style such widgets through a
8126  * #GtkCssProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
8127  * priority.
8128  * </para></note>
8129  *
8130  * Since: 3.0
8131  */
8132 void
8133 gtk_widget_override_color (GtkWidget     *widget,
8134                            GtkStateFlags  state,
8135                            const GdkRGBA *color)
8136 {
8137   GtkModifierStyle *style;
8138
8139   g_return_if_fail (GTK_IS_WIDGET (widget));
8140
8141   style = _gtk_widget_get_modifier_properties (widget);
8142   _gtk_modifier_style_set_color (style, state, color);
8143 }
8144
8145 /**
8146  * gtk_widget_override_background_color:
8147  * @widget: a #GtkWidget
8148  * @state: the state for which to set the background color
8149  * @color: (allow-none): the color to assign, or %NULL to undo the effect
8150  *     of previous calls to gtk_widget_override_background_color()
8151  *
8152  * Sets the background color to use for a widget.
8153  *
8154  * All other style values are left untouched.
8155  * See gtk_widget_override_color().
8156  *
8157  * Since: 3.0
8158  */
8159 void
8160 gtk_widget_override_background_color (GtkWidget     *widget,
8161                                       GtkStateFlags  state,
8162                                       const GdkRGBA *color)
8163 {
8164   GtkModifierStyle *style;
8165
8166   g_return_if_fail (GTK_IS_WIDGET (widget));
8167
8168   style = _gtk_widget_get_modifier_properties (widget);
8169   _gtk_modifier_style_set_background_color (style, state, color);
8170 }
8171
8172 /**
8173  * gtk_widget_override_font:
8174  * @widget: a #GtkWidget
8175  * @font_desc: (allow-none): the font descriptiong to use, or %NULL to undo
8176  *     the effect of previous calls to gtk_widget_override_font()
8177  *
8178  * Sets the font to use for a widget. All other style values are
8179  * left untouched. See gtk_widget_override_color().
8180  *
8181  * Since: 3.0
8182  */
8183 void
8184 gtk_widget_override_font (GtkWidget                  *widget,
8185                           const PangoFontDescription *font_desc)
8186 {
8187   GtkModifierStyle *style;
8188
8189   g_return_if_fail (GTK_IS_WIDGET (widget));
8190
8191   style = _gtk_widget_get_modifier_properties (widget);
8192   _gtk_modifier_style_set_font (style, font_desc);
8193 }
8194
8195 /**
8196  * gtk_widget_override_symbolic_color:
8197  * @widget: a #GtkWidget
8198  * @name: the name of the symbolic color to modify
8199  * @color: (allow-none): the color to assign (does not need
8200  *     to be allocated), or %NULL to undo the effect of previous
8201  *     calls to gtk_widget_override_symbolic_color()
8202  *
8203  * Sets a symbolic color for a widget.
8204  *
8205  * All other style values are left untouched.
8206  * See gtk_widget_override_color() for overriding the foreground
8207  * or background color.
8208  *
8209  * Since: 3.0
8210  */
8211 void
8212 gtk_widget_override_symbolic_color (GtkWidget     *widget,
8213                                     const gchar   *name,
8214                                     const GdkRGBA *color)
8215 {
8216   GtkModifierStyle *style;
8217
8218   g_return_if_fail (GTK_IS_WIDGET (widget));
8219
8220   style = _gtk_widget_get_modifier_properties (widget);
8221   _gtk_modifier_style_map_color (style, name, color);
8222 }
8223
8224 /**
8225  * gtk_widget_override_cursor:
8226  * @widget: a #GtkWidget
8227  * @cursor: (allow-none): the color to use for primary cursor (does not need to be
8228  *     allocated), or %NULL to undo the effect of previous calls to
8229  *     of gtk_widget_override_cursor().
8230  * @secondary_cursor: (allow-none): the color to use for secondary cursor (does not
8231  *     need to be allocated), or %NULL to undo the effect of previous
8232  *     calls to of gtk_widget_override_cursor().
8233  *
8234  * Sets the cursor color to use in a widget, overriding the
8235  * #GtkWidget:cursor-color and #GtkWidget:secondary-cursor-color
8236  * style properties. All other style values are left untouched.
8237  * See also gtk_widget_modify_style().
8238  *
8239  * Note that the underlying properties have the #GdkColor type,
8240  * so the alpha value in @primary and @secondary will be ignored.
8241  *
8242  * Since: 3.0
8243  */
8244 void
8245 gtk_widget_override_cursor (GtkWidget     *widget,
8246                             const GdkRGBA *cursor,
8247                             const GdkRGBA *secondary_cursor)
8248 {
8249   GtkModifierStyle *style;
8250
8251   g_return_if_fail (GTK_IS_WIDGET (widget));
8252
8253   style = _gtk_widget_get_modifier_properties (widget);
8254   _gtk_modifier_style_set_color_property (style,
8255                                           GTK_TYPE_WIDGET,
8256                                           "cursor-color", cursor);
8257   _gtk_modifier_style_set_color_property (style,
8258                                           GTK_TYPE_WIDGET,
8259                                           "secondary-cursor-color",
8260                                           secondary_cursor);
8261 }
8262
8263 /**
8264  * gtk_widget_modify_fg:
8265  * @widget: a #GtkWidget
8266  * @state: the state for which to set the foreground color
8267  * @color: (allow-none): the color to assign (does not need to be allocated),
8268  *     or %NULL to undo the effect of previous calls to
8269  *     of gtk_widget_modify_fg().
8270  *
8271  * Sets the foreground color for a widget in a particular state.
8272  *
8273  * All other style values are left untouched.
8274  * See also gtk_widget_modify_style().
8275  *
8276  * Deprecated:3.0: Use gtk_widget_override_color() instead
8277  */
8278 void
8279 gtk_widget_modify_fg (GtkWidget      *widget,
8280                       GtkStateType    state,
8281                       const GdkColor *color)
8282 {
8283   GtkStateFlags flags;
8284   GdkRGBA rgba;
8285
8286   g_return_if_fail (GTK_IS_WIDGET (widget));
8287   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8288
8289   switch (state)
8290     {
8291     case GTK_STATE_ACTIVE:
8292       flags = GTK_STATE_FLAG_ACTIVE;
8293       break;
8294     case GTK_STATE_PRELIGHT:
8295       flags = GTK_STATE_FLAG_PRELIGHT;
8296       break;
8297     case GTK_STATE_SELECTED:
8298       flags = GTK_STATE_FLAG_SELECTED;
8299       break;
8300     case GTK_STATE_INSENSITIVE:
8301       flags = GTK_STATE_FLAG_INSENSITIVE;
8302       break;
8303     case GTK_STATE_NORMAL:
8304     default:
8305       flags = 0;
8306     }
8307
8308   if (color)
8309     {
8310       rgba.red = color->red / 65535.;
8311       rgba.green = color->green / 65535.;
8312       rgba.blue = color->blue / 65535.;
8313       rgba.alpha = 1;
8314
8315       gtk_widget_override_color (widget, flags, &rgba);
8316     }
8317   else
8318     gtk_widget_override_color (widget, flags, NULL);
8319 }
8320
8321 /**
8322  * gtk_widget_modify_bg:
8323  * @widget: a #GtkWidget
8324  * @state: the state for which to set the background color
8325  * @color: (allow-none): the color to assign (does not need
8326  *     to be allocated), or %NULL to undo the effect of previous
8327  *     calls to of gtk_widget_modify_bg().
8328  *
8329  * Sets the background color for a widget in a particular state.
8330  *
8331  * All other style values are left untouched.
8332  * See also gtk_widget_modify_style().
8333  *
8334  * <note><para>
8335  * Note that "no window" widgets (which have the %GTK_NO_WINDOW
8336  * flag set) draw on their parent container's window and thus may
8337  * not draw any background themselves. This is the case for e.g.
8338  * #GtkLabel.
8339  * </para><para>
8340  * To modify the background of such widgets, you have to set the
8341  * background color on their parent; if you want to set the background
8342  * of a rectangular area around a label, try placing the label in
8343  * a #GtkEventBox widget and setting the background color on that.
8344  * </para></note>
8345  *
8346  * Deprecated:3.0: Use gtk_widget_override_background_color() instead
8347  */
8348 void
8349 gtk_widget_modify_bg (GtkWidget      *widget,
8350                       GtkStateType    state,
8351                       const GdkColor *color)
8352 {
8353   GtkStateFlags flags;
8354   GdkRGBA rgba;
8355
8356   g_return_if_fail (GTK_IS_WIDGET (widget));
8357   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8358
8359   switch (state)
8360     {
8361     case GTK_STATE_ACTIVE:
8362       flags = GTK_STATE_FLAG_ACTIVE;
8363       break;
8364     case GTK_STATE_PRELIGHT:
8365       flags = GTK_STATE_FLAG_PRELIGHT;
8366       break;
8367     case GTK_STATE_SELECTED:
8368       flags = GTK_STATE_FLAG_SELECTED;
8369       break;
8370     case GTK_STATE_INSENSITIVE:
8371       flags = GTK_STATE_FLAG_INSENSITIVE;
8372       break;
8373     case GTK_STATE_NORMAL:
8374     default:
8375       flags = 0;
8376     }
8377
8378   if (color)
8379     {
8380       rgba.red = color->red / 65535.;
8381       rgba.green = color->green / 65535.;
8382       rgba.blue = color->blue / 65535.;
8383       rgba.alpha = 1;
8384
8385       gtk_widget_override_background_color (widget, flags, &rgba);
8386     }
8387   else
8388     gtk_widget_override_background_color (widget, flags, NULL);
8389 }
8390
8391 /**
8392  * gtk_widget_modify_text:
8393  * @widget: a #GtkWidget
8394  * @state: the state for which to set the text color
8395  * @color: (allow-none): the color to assign (does not need to
8396  *     be allocated), or %NULL to undo the effect of previous
8397  *     calls to of gtk_widget_modify_text().
8398  *
8399  * Sets the text color for a widget in a particular state.
8400  *
8401  * All other style values are left untouched.
8402  * The text color is the foreground color used along with the
8403  * base color (see gtk_widget_modify_base()) for widgets such
8404  * as #GtkEntry and #GtkTextView.
8405  * See also gtk_widget_modify_style().
8406  *
8407  * Deprecated:3.0: Use gtk_widget_override_color() instead
8408  */
8409 void
8410 gtk_widget_modify_text (GtkWidget      *widget,
8411                         GtkStateType    state,
8412                         const GdkColor *color)
8413 {
8414   g_return_if_fail (GTK_IS_WIDGET (widget));
8415   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8416
8417   gtk_widget_modify_color_component (widget, GTK_RC_TEXT, state, color);
8418 }
8419
8420 /**
8421  * gtk_widget_modify_base:
8422  * @widget: a #GtkWidget
8423  * @state: the state for which to set the base color
8424  * @color: (allow-none): the color to assign (does not need to
8425  *     be allocated), or %NULL to undo the effect of previous
8426  *     calls to of gtk_widget_modify_base().
8427  *
8428  * Sets the base color for a widget in a particular state.
8429  * All other style values are left untouched. The base color
8430  * is the background color used along with the text color
8431  * (see gtk_widget_modify_text()) for widgets such as #GtkEntry
8432  * and #GtkTextView. See also gtk_widget_modify_style().
8433  *
8434  * <note><para>
8435  * Note that "no window" widgets (which have the %GTK_NO_WINDOW
8436  * flag set) draw on their parent container's window and thus may
8437  * not draw any background themselves. This is the case for e.g.
8438  * #GtkLabel.
8439  * </para><para>
8440  * To modify the background of such widgets, you have to set the
8441  * base color on their parent; if you want to set the background
8442  * of a rectangular area around a label, try placing the label in
8443  * a #GtkEventBox widget and setting the base color on that.
8444  * </para></note>
8445  *
8446  * Deprecated:3.0: Use gtk_widget_override_background_color() instead
8447  */
8448 void
8449 gtk_widget_modify_base (GtkWidget      *widget,
8450                         GtkStateType    state,
8451                         const GdkColor *color)
8452 {
8453   g_return_if_fail (GTK_IS_WIDGET (widget));
8454   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8455
8456   gtk_widget_modify_color_component (widget, GTK_RC_BASE, state, color);
8457 }
8458
8459 /**
8460  * gtk_widget_modify_cursor:
8461  * @widget: a #GtkWidget
8462  * @primary: the color to use for primary cursor (does not need to be
8463  *     allocated), or %NULL to undo the effect of previous calls to
8464  *     of gtk_widget_modify_cursor().
8465  * @secondary: the color to use for secondary cursor (does not need to be
8466  *     allocated), or %NULL to undo the effect of previous calls to
8467  *     of gtk_widget_modify_cursor().
8468  *
8469  * Sets the cursor color to use in a widget, overriding the
8470  * #GtkWidget:cursor-color and #GtkWidget:secondary-cursor-color
8471  * style properties.
8472  *
8473  * All other style values are left untouched.
8474  * See also gtk_widget_modify_style().
8475  *
8476  * Since: 2.12
8477  *
8478  * Deprecated: 3.0. Use gtk_widget_override_cursor() instead.
8479  */
8480 void
8481 gtk_widget_modify_cursor (GtkWidget      *widget,
8482                           const GdkColor *primary,
8483                           const GdkColor *secondary)
8484 {
8485   GdkRGBA primary_rgba, secondary_rgba;
8486
8487   g_return_if_fail (GTK_IS_WIDGET (widget));
8488
8489   primary_rgba.red = primary->red / 65535.;
8490   primary_rgba.green = primary->green / 65535.;
8491   primary_rgba.blue = primary->blue / 65535.;
8492   primary_rgba.alpha = 1;
8493
8494   secondary_rgba.red = secondary->red / 65535.;
8495   secondary_rgba.green = secondary->green / 65535.;
8496   secondary_rgba.blue = secondary->blue / 65535.;
8497   secondary_rgba.alpha = 1;
8498
8499   gtk_widget_override_cursor (widget, &primary_rgba, &secondary_rgba);
8500 }
8501
8502 /**
8503  * gtk_widget_modify_font:
8504  * @widget: a #GtkWidget
8505  * @font_desc: (allow-none): the font description to use, or %NULL
8506  *     to undo the effect of previous calls to gtk_widget_modify_font()
8507  *
8508  * Sets the font to use for a widget.
8509  *
8510  * All other style values are left untouched.
8511  * See also gtk_widget_modify_style().
8512  *
8513  * Deprecated:3.0: Use gtk_widget_override_font() instead
8514  */
8515 void
8516 gtk_widget_modify_font (GtkWidget            *widget,
8517                         PangoFontDescription *font_desc)
8518 {
8519   g_return_if_fail (GTK_IS_WIDGET (widget));
8520
8521   gtk_widget_override_font (widget, font_desc);
8522 }
8523
8524 static void
8525 gtk_widget_real_direction_changed (GtkWidget        *widget,
8526                                    GtkTextDirection  previous_direction)
8527 {
8528   gtk_widget_queue_resize (widget);
8529 }
8530
8531 static void
8532 gtk_widget_real_style_set (GtkWidget *widget,
8533                            GtkStyle  *previous_style)
8534 {
8535 }
8536
8537 typedef struct {
8538   GtkWidget *previous_toplevel;
8539   GdkScreen *previous_screen;
8540   GdkScreen *new_screen;
8541 } HierarchyChangedInfo;
8542
8543 static void
8544 do_screen_change (GtkWidget *widget,
8545                   GdkScreen *old_screen,
8546                   GdkScreen *new_screen)
8547 {
8548   if (old_screen != new_screen)
8549     {
8550       GtkWidgetPrivate *priv = widget->priv;
8551
8552       if (old_screen)
8553         {
8554           PangoContext *context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
8555           if (context)
8556             g_object_set_qdata (G_OBJECT (widget), quark_pango_context, NULL);
8557         }
8558
8559       _gtk_tooltip_hide (widget);
8560
8561       if (new_screen && priv->context)
8562         gtk_style_context_set_screen (priv->context, new_screen);
8563
8564       g_signal_emit (widget, widget_signals[SCREEN_CHANGED], 0, old_screen);
8565     }
8566 }
8567
8568 static void
8569 gtk_widget_propagate_hierarchy_changed_recurse (GtkWidget *widget,
8570                                                 gpointer   client_data)
8571 {
8572   GtkWidgetPrivate *priv = widget->priv;
8573   HierarchyChangedInfo *info = client_data;
8574   gboolean new_anchored = gtk_widget_is_toplevel (widget) ||
8575                  (priv->parent && priv->parent->priv->anchored);
8576
8577   if (priv->anchored != new_anchored)
8578     {
8579       g_object_ref (widget);
8580
8581       priv->anchored = new_anchored;
8582
8583       g_signal_emit (widget, widget_signals[HIERARCHY_CHANGED], 0, info->previous_toplevel);
8584       do_screen_change (widget, info->previous_screen, info->new_screen);
8585
8586       if (GTK_IS_CONTAINER (widget))
8587         gtk_container_forall (GTK_CONTAINER (widget),
8588                               gtk_widget_propagate_hierarchy_changed_recurse,
8589                               client_data);
8590
8591       g_object_unref (widget);
8592     }
8593 }
8594
8595 /**
8596  * _gtk_widget_propagate_hierarchy_changed:
8597  * @widget: a #GtkWidget
8598  * @previous_toplevel: Previous toplevel
8599  *
8600  * Propagates changes in the anchored state to a widget and all
8601  * children, unsetting or setting the %ANCHORED flag, and
8602  * emitting #GtkWidget::hierarchy-changed.
8603  **/
8604 void
8605 _gtk_widget_propagate_hierarchy_changed (GtkWidget *widget,
8606                                          GtkWidget *previous_toplevel)
8607 {
8608   GtkWidgetPrivate *priv = widget->priv;
8609   HierarchyChangedInfo info;
8610
8611   info.previous_toplevel = previous_toplevel;
8612   info.previous_screen = previous_toplevel ? gtk_widget_get_screen (previous_toplevel) : NULL;
8613
8614   if (gtk_widget_is_toplevel (widget) ||
8615       (priv->parent && priv->parent->priv->anchored))
8616     info.new_screen = gtk_widget_get_screen (widget);
8617   else
8618     info.new_screen = NULL;
8619
8620   if (info.previous_screen)
8621     g_object_ref (info.previous_screen);
8622   if (previous_toplevel)
8623     g_object_ref (previous_toplevel);
8624
8625   gtk_widget_propagate_hierarchy_changed_recurse (widget, &info);
8626
8627   if (previous_toplevel)
8628     g_object_unref (previous_toplevel);
8629   if (info.previous_screen)
8630     g_object_unref (info.previous_screen);
8631 }
8632
8633 static void
8634 gtk_widget_propagate_screen_changed_recurse (GtkWidget *widget,
8635                                              gpointer   client_data)
8636 {
8637   HierarchyChangedInfo *info = client_data;
8638
8639   g_object_ref (widget);
8640
8641   do_screen_change (widget, info->previous_screen, info->new_screen);
8642
8643   if (GTK_IS_CONTAINER (widget))
8644     gtk_container_forall (GTK_CONTAINER (widget),
8645                           gtk_widget_propagate_screen_changed_recurse,
8646                           client_data);
8647
8648   g_object_unref (widget);
8649 }
8650
8651 /**
8652  * gtk_widget_is_composited:
8653  * @widget: a #GtkWidget
8654  *
8655  * Whether @widget can rely on having its alpha channel
8656  * drawn correctly. On X11 this function returns whether a
8657  * compositing manager is running for @widget's screen.
8658  *
8659  * Please note that the semantics of this call will change
8660  * in the future if used on a widget that has a composited
8661  * window in its hierarchy (as set by gdk_window_set_composited()).
8662  *
8663  * Return value: %TRUE if the widget can rely on its alpha
8664  * channel being drawn correctly.
8665  *
8666  * Since: 2.10
8667  */
8668 gboolean
8669 gtk_widget_is_composited (GtkWidget *widget)
8670 {
8671   GdkScreen *screen;
8672
8673   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
8674
8675   screen = gtk_widget_get_screen (widget);
8676
8677   return gdk_screen_is_composited (screen);
8678 }
8679
8680 static void
8681 propagate_composited_changed (GtkWidget *widget,
8682                               gpointer dummy)
8683 {
8684   if (GTK_IS_CONTAINER (widget))
8685     {
8686       gtk_container_forall (GTK_CONTAINER (widget),
8687                             propagate_composited_changed,
8688                             NULL);
8689     }
8690
8691   g_signal_emit (widget, widget_signals[COMPOSITED_CHANGED], 0);
8692 }
8693
8694 void
8695 _gtk_widget_propagate_composited_changed (GtkWidget *widget)
8696 {
8697   propagate_composited_changed (widget, NULL);
8698 }
8699
8700 /**
8701  * _gtk_widget_propagate_screen_changed:
8702  * @widget: a #GtkWidget
8703  * @previous_screen: Previous screen
8704  *
8705  * Propagates changes in the screen for a widget to all
8706  * children, emitting #GtkWidget::screen-changed.
8707  **/
8708 void
8709 _gtk_widget_propagate_screen_changed (GtkWidget    *widget,
8710                                       GdkScreen    *previous_screen)
8711 {
8712   HierarchyChangedInfo info;
8713
8714   info.previous_screen = previous_screen;
8715   info.new_screen = gtk_widget_get_screen (widget);
8716
8717   if (previous_screen)
8718     g_object_ref (previous_screen);
8719
8720   gtk_widget_propagate_screen_changed_recurse (widget, &info);
8721
8722   if (previous_screen)
8723     g_object_unref (previous_screen);
8724 }
8725
8726 static void
8727 reset_style_recurse (GtkWidget *widget, gpointer data)
8728 {
8729   _gtk_widget_update_path (widget);
8730
8731   if (GTK_IS_CONTAINER (widget))
8732     gtk_container_forall (GTK_CONTAINER (widget),
8733                           reset_style_recurse,
8734                           NULL);
8735 }
8736
8737 /**
8738  * gtk_widget_reset_style:
8739  * @widget: a #GtkWidget
8740  *
8741  * Updates the style context of @widget and all descendents
8742  * by updating its widget path. #GtkContainer<!-- -->s may want
8743  * to use this on a child when reordering it in a way that a different
8744  * style might apply to it. See also gtk_container_get_path_for_child().
8745  *
8746  * Since: 3.0
8747  */
8748 void
8749 gtk_widget_reset_style (GtkWidget *widget)
8750 {
8751   g_return_if_fail (GTK_IS_WIDGET (widget));
8752
8753   reset_style_recurse (widget, NULL);
8754 }
8755
8756 /**
8757  * gtk_widget_reset_rc_styles:
8758  * @widget: a #GtkWidget.
8759  *
8760  * Reset the styles of @widget and all descendents, so when
8761  * they are looked up again, they get the correct values
8762  * for the currently loaded RC file settings.
8763  *
8764  * This function is not useful for applications.
8765  *
8766  * Deprecated:3.0: Use #GtkStyleContext instead, and gtk_widget_reset_style()
8767  */
8768 void
8769 gtk_widget_reset_rc_styles (GtkWidget *widget)
8770 {
8771   g_return_if_fail (GTK_IS_WIDGET (widget));
8772
8773   reset_style_recurse (widget, NULL);
8774 }
8775
8776 /**
8777  * gtk_widget_get_default_style:
8778  *
8779  * Returns the default style used by all widgets initially.
8780  *
8781  * Returns: (transfer none): the default style. This #GtkStyle
8782  *     object is owned by GTK+ and should not be modified or freed.
8783  *
8784  * Deprecated:3.0: Use #GtkStyleContext instead, and
8785  *     gtk_css_provider_get_default() to obtain a #GtkStyleProvider
8786  *     with the default widget style information.
8787  */
8788 GtkStyle*
8789 gtk_widget_get_default_style (void)
8790 {
8791   if (!gtk_default_style)
8792     {
8793       gtk_default_style = gtk_style_new ();
8794       g_object_ref (gtk_default_style);
8795     }
8796
8797   return gtk_default_style;
8798 }
8799
8800 #ifdef G_ENABLE_DEBUG
8801
8802 /* Verify invariants, see docs/widget_system.txt for notes on much of
8803  * this.  Invariants may be temporarily broken while we're in the
8804  * process of updating state, of course, so you can only
8805  * verify_invariants() after a given operation is complete.
8806  * Use push/pop_verify_invariants to help with that.
8807  */
8808 static void
8809 gtk_widget_verify_invariants (GtkWidget *widget)
8810 {
8811   GtkWidget *parent;
8812
8813   if (widget->priv->verifying_invariants_count > 0)
8814     return;
8815
8816   parent = widget->priv->parent;
8817
8818   if (widget->priv->mapped)
8819     {
8820       /* Mapped implies ... */
8821
8822       if (!widget->priv->realized)
8823         g_warning ("%s %p is mapped but not realized",
8824                    G_OBJECT_TYPE_NAME (widget), widget);
8825
8826       if (!widget->priv->visible)
8827         g_warning ("%s %p is mapped but not visible",
8828                    G_OBJECT_TYPE_NAME (widget), widget);
8829
8830       if (!widget->priv->toplevel)
8831         {
8832           if (!widget->priv->child_visible)
8833             g_warning ("%s %p is mapped but not child_visible",
8834                        G_OBJECT_TYPE_NAME (widget), widget);
8835         }
8836     }
8837   else
8838     {
8839       /* Not mapped implies... */
8840
8841 #if 0
8842   /* This check makes sense for normal toplevels, but for
8843    * something like a toplevel that is embedded within a clutter
8844    * state, mapping may depend on external factors.
8845    */
8846       if (widget->priv->toplevel)
8847         {
8848           if (widget->priv->visible)
8849             g_warning ("%s %p toplevel is visible but not mapped",
8850                        G_OBJECT_TYPE_NAME (widget), widget);
8851         }
8852 #endif
8853     }
8854
8855   /* Parent related checks aren't possible if parent has
8856    * verifying_invariants_count > 0 because parent needs to recurse
8857    * children first before the invariants will hold.
8858    */
8859   if (parent == NULL || parent->priv->verifying_invariants_count == 0)
8860     {
8861       if (parent &&
8862           parent->priv->realized)
8863         {
8864           /* Parent realized implies... */
8865
8866 #if 0
8867           /* This is in widget_system.txt but appears to fail
8868            * because there's no gtk_container_realize() that
8869            * realizes all children... instead we just lazily
8870            * wait for map to fix things up.
8871            */
8872           if (!widget->priv->realized)
8873             g_warning ("%s %p is realized but child %s %p is not realized",
8874                        G_OBJECT_TYPE_NAME (parent), parent,
8875                        G_OBJECT_TYPE_NAME (widget), widget);
8876 #endif
8877         }
8878       else if (!widget->priv->toplevel)
8879         {
8880           /* No parent or parent not realized on non-toplevel implies... */
8881
8882           if (widget->priv->realized && !widget->priv->in_reparent)
8883             g_warning ("%s %p is not realized but child %s %p is realized",
8884                        parent ? G_OBJECT_TYPE_NAME (parent) : "no parent", parent,
8885                        G_OBJECT_TYPE_NAME (widget), widget);
8886         }
8887
8888       if (parent &&
8889           parent->priv->mapped &&
8890           widget->priv->visible &&
8891           widget->priv->child_visible)
8892         {
8893           /* Parent mapped and we are visible implies... */
8894
8895           if (!widget->priv->mapped)
8896             g_warning ("%s %p is mapped but visible child %s %p is not mapped",
8897                        G_OBJECT_TYPE_NAME (parent), parent,
8898                        G_OBJECT_TYPE_NAME (widget), widget);
8899         }
8900       else if (!widget->priv->toplevel)
8901         {
8902           /* No parent or parent not mapped on non-toplevel implies... */
8903
8904           if (widget->priv->mapped && !widget->priv->in_reparent)
8905             g_warning ("%s %p is mapped but visible=%d child_visible=%d parent %s %p mapped=%d",
8906                        G_OBJECT_TYPE_NAME (widget), widget,
8907                        widget->priv->visible,
8908                        widget->priv->child_visible,
8909                        parent ? G_OBJECT_TYPE_NAME (parent) : "no parent", parent,
8910                        parent ? parent->priv->mapped : FALSE);
8911         }
8912     }
8913
8914   if (!widget->priv->realized)
8915     {
8916       /* Not realized implies... */
8917
8918 #if 0
8919       /* widget_system.txt says these hold, but they don't. */
8920       if (widget->priv->resize_pending)
8921         g_warning ("%s %p resize pending but not realized",
8922                    G_OBJECT_TYPE_NAME (widget), widget);
8923
8924       if (widget->priv->alloc_needed)
8925         g_warning ("%s %p alloc needed but not realized",
8926                    G_OBJECT_TYPE_NAME (widget), widget);
8927
8928       if (widget->priv->width_request_needed)
8929         g_warning ("%s %p width request needed but not realized",
8930                    G_OBJECT_TYPE_NAME (widget), widget);
8931
8932       if (widget->priv->height_request_needed)
8933         g_warning ("%s %p height request needed but not realized",
8934                    G_OBJECT_TYPE_NAME (widget), widget);
8935 #endif
8936     }
8937 }
8938
8939 /* The point of this push/pop is that invariants may not hold while
8940  * we're busy making changes. So we only check at the outermost call
8941  * on the call stack, after we finish updating everything.
8942  */
8943 static void
8944 gtk_widget_push_verify_invariants (GtkWidget *widget)
8945 {
8946   widget->priv->verifying_invariants_count += 1;
8947 }
8948
8949 static void
8950 gtk_widget_verify_child_invariants (GtkWidget *widget,
8951                                     gpointer   client_data)
8952 {
8953   /* We don't recurse further; this is a one-level check. */
8954   gtk_widget_verify_invariants (widget);
8955 }
8956
8957 static void
8958 gtk_widget_pop_verify_invariants (GtkWidget *widget)
8959 {
8960   g_assert (widget->priv->verifying_invariants_count > 0);
8961
8962   widget->priv->verifying_invariants_count -= 1;
8963
8964   if (widget->priv->verifying_invariants_count == 0)
8965     {
8966       gtk_widget_verify_invariants (widget);
8967
8968       if (GTK_IS_CONTAINER (widget))
8969         {
8970           /* Check one level of children, because our
8971            * push_verify_invariants() will have prevented some of the
8972            * checks. This does not recurse because if recursion is
8973            * needed, it will happen naturally as each child has a
8974            * push/pop on that child. For example if we're recursively
8975            * mapping children, we'll push/pop on each child as we map
8976            * it.
8977            */
8978           gtk_container_forall (GTK_CONTAINER (widget),
8979                                 gtk_widget_verify_child_invariants,
8980                                 NULL);
8981         }
8982     }
8983 }
8984 #endif /* G_ENABLE_DEBUG */
8985
8986 static PangoContext *
8987 gtk_widget_peek_pango_context (GtkWidget *widget)
8988 {
8989   return g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
8990 }
8991
8992 /**
8993  * gtk_widget_get_pango_context:
8994  * @widget: a #GtkWidget
8995  *
8996  * Gets a #PangoContext with the appropriate font map, font description,
8997  * and base direction for this widget. Unlike the context returned
8998  * by gtk_widget_create_pango_context(), this context is owned by
8999  * the widget (it can be used until the screen for the widget changes
9000  * or the widget is removed from its toplevel), and will be updated to
9001  * match any changes to the widget's attributes.
9002  *
9003  * If you create and keep a #PangoLayout using this context, you must
9004  * deal with changes to the context by calling pango_layout_context_changed()
9005  * on the layout in response to the #GtkWidget::style-updated and
9006  * #GtkWidget::direction-changed signals for the widget.
9007  *
9008  * Return value: (transfer none): the #PangoContext for the widget.
9009  **/
9010 PangoContext *
9011 gtk_widget_get_pango_context (GtkWidget *widget)
9012 {
9013   PangoContext *context;
9014
9015   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9016
9017   context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
9018   if (!context)
9019     {
9020       context = gtk_widget_create_pango_context (GTK_WIDGET (widget));
9021       g_object_set_qdata_full (G_OBJECT (widget),
9022                                quark_pango_context,
9023                                context,
9024                                g_object_unref);
9025     }
9026
9027   return context;
9028 }
9029
9030 static void
9031 update_pango_context (GtkWidget    *widget,
9032                       PangoContext *context)
9033 {
9034   const PangoFontDescription *font_desc;
9035   GtkStyleContext *style_context;
9036
9037   style_context = gtk_widget_get_style_context (widget);
9038
9039   font_desc = gtk_style_context_get_font (style_context,
9040                                           gtk_widget_get_state_flags (widget));
9041
9042   pango_context_set_font_description (context, font_desc);
9043   pango_context_set_base_dir (context,
9044                               gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
9045                               PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
9046 }
9047
9048 static void
9049 gtk_widget_update_pango_context (GtkWidget *widget)
9050 {
9051   PangoContext *context = gtk_widget_peek_pango_context (widget);
9052
9053   if (context)
9054     {
9055       GdkScreen *screen;
9056
9057       update_pango_context (widget, context);
9058
9059       screen = gtk_widget_get_screen_unchecked (widget);
9060       if (screen)
9061         {
9062           pango_cairo_context_set_resolution (context,
9063                                               gdk_screen_get_resolution (screen));
9064           pango_cairo_context_set_font_options (context,
9065                                                 gdk_screen_get_font_options (screen));
9066         }
9067     }
9068 }
9069
9070 /**
9071  * gtk_widget_create_pango_context:
9072  * @widget: a #GtkWidget
9073  *
9074  * Creates a new #PangoContext with the appropriate font map,
9075  * font description, and base direction for drawing text for
9076  * this widget. See also gtk_widget_get_pango_context().
9077  *
9078  * Return value: (transfer full): the new #PangoContext
9079  **/
9080 PangoContext *
9081 gtk_widget_create_pango_context (GtkWidget *widget)
9082 {
9083   GdkScreen *screen;
9084   PangoContext *context;
9085
9086   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9087
9088   screen = gtk_widget_get_screen_unchecked (widget);
9089   if (!screen)
9090     {
9091       GTK_NOTE (MULTIHEAD,
9092                 g_warning ("gtk_widget_create_pango_context ()) called without screen"));
9093
9094       screen = gdk_screen_get_default ();
9095     }
9096
9097   context = gdk_pango_context_get_for_screen (screen);
9098
9099   update_pango_context (widget, context);
9100   pango_context_set_language (context, gtk_get_default_language ());
9101
9102   return context;
9103 }
9104
9105 /**
9106  * gtk_widget_create_pango_layout:
9107  * @widget: a #GtkWidget
9108  * @text: text to set on the layout (can be %NULL)
9109  *
9110  * Creates a new #PangoLayout with the appropriate font map,
9111  * font description, and base direction for drawing text for
9112  * this widget.
9113  *
9114  * If you keep a #PangoLayout created in this way around, in order to
9115  * notify the layout of changes to the base direction or font of this
9116  * widget, you must call pango_layout_context_changed() in response to
9117  * the #GtkWidget::style-updated and #GtkWidget::direction-changed signals
9118  * for the widget.
9119  *
9120  * Return value: (transfer full): the new #PangoLayout
9121  **/
9122 PangoLayout *
9123 gtk_widget_create_pango_layout (GtkWidget   *widget,
9124                                 const gchar *text)
9125 {
9126   PangoLayout *layout;
9127   PangoContext *context;
9128
9129   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9130
9131   context = gtk_widget_get_pango_context (widget);
9132   layout = pango_layout_new (context);
9133
9134   if (text)
9135     pango_layout_set_text (layout, text, -1);
9136
9137   return layout;
9138 }
9139
9140 /**
9141  * gtk_widget_render_icon_pixbuf:
9142  * @widget: a #GtkWidget
9143  * @stock_id: a stock ID
9144  * @size: (type int): a stock size. A size of (GtkIconSize)-1 means
9145  *     render at the size of the source and don't scale (if there are
9146  *     multiple source sizes, GTK+ picks one of the available sizes).
9147  *
9148  * A convenience function that uses the theme engine and style
9149  * settings for @widget to look up @stock_id and render it to
9150  * a pixbuf. @stock_id should be a stock icon ID such as
9151  * #GTK_STOCK_OPEN or #GTK_STOCK_OK. @size should be a size
9152  * such as #GTK_ICON_SIZE_MENU.
9153  *
9154  * The pixels in the returned #GdkPixbuf are shared with the rest of
9155  * the application and should not be modified. The pixbuf should be freed
9156  * after use with g_object_unref().
9157  *
9158  * Return value: (transfer full): a new pixbuf, or %NULL if the
9159  *     stock ID wasn't known
9160  *
9161  * Since: 3.0
9162  **/
9163 GdkPixbuf*
9164 gtk_widget_render_icon_pixbuf (GtkWidget   *widget,
9165                                const gchar *stock_id,
9166                                GtkIconSize  size)
9167 {
9168   GtkStyleContext *context;
9169   GtkIconSet *icon_set;
9170
9171   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9172   g_return_val_if_fail (stock_id != NULL, NULL);
9173   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
9174
9175   context = gtk_widget_get_style_context (widget);
9176   icon_set = gtk_style_context_lookup_icon_set (context, stock_id);
9177
9178   if (icon_set == NULL)
9179     return NULL;
9180
9181   return gtk_icon_set_render_icon_pixbuf (icon_set, context, size);
9182 }
9183
9184 /**
9185  * gtk_widget_render_icon:
9186  * @widget: a #GtkWidget
9187  * @stock_id: a stock ID
9188  * @size: (type int): a stock size. A size of (GtkIconSize)-1 means
9189  *     render at the size of the source and don't scale (if there are
9190  *     multiple source sizes, GTK+ picks one of the available sizes).
9191  * @detail: (allow-none): render detail to pass to theme engine
9192  *
9193  * A convenience function that uses the theme settings for @widget
9194  * to look up @stock_id and render it to a pixbuf. @stock_id should
9195  * be a stock icon ID such as #GTK_STOCK_OPEN or #GTK_STOCK_OK. @size
9196  * should be a size such as #GTK_ICON_SIZE_MENU. @detail should be a
9197  * string that identifies the widget or code doing the rendering, so
9198  * that theme engines can special-case rendering for that widget or
9199  * code.
9200  *
9201  * The pixels in the returned #GdkPixbuf are shared with the rest of
9202  * the application and should not be modified. The pixbuf should be
9203  * freed after use with g_object_unref().
9204  *
9205  * Return value: (transfer full): a new pixbuf, or %NULL if the
9206  *     stock ID wasn't known
9207  *
9208  * Deprecated: 3.0: Use gtk_widget_render_icon_pixbuf() instead.
9209  **/
9210 GdkPixbuf*
9211 gtk_widget_render_icon (GtkWidget      *widget,
9212                         const gchar    *stock_id,
9213                         GtkIconSize     size,
9214                         const gchar    *detail)
9215 {
9216   gtk_widget_ensure_style (widget);
9217
9218   return gtk_widget_render_icon_pixbuf (widget, stock_id, size);
9219 }
9220
9221 /**
9222  * gtk_widget_set_parent_window:
9223  * @widget: a #GtkWidget.
9224  * @parent_window: the new parent window.
9225  *
9226  * Sets a non default parent window for @widget.
9227  *
9228  * For GtkWindow classes, setting a @parent_window effects whether
9229  * the window is a toplevel window or can be embedded into other
9230  * widgets.
9231  *
9232  * <note><para>
9233  * For GtkWindow classes, this needs to be called before the
9234  * window is realized.
9235  * </para></note>
9236  * 
9237  **/
9238 void
9239 gtk_widget_set_parent_window   (GtkWidget           *widget,
9240                                 GdkWindow           *parent_window)
9241 {
9242   GdkWindow *old_parent_window;
9243
9244   g_return_if_fail (GTK_IS_WIDGET (widget));
9245
9246   old_parent_window = g_object_get_qdata (G_OBJECT (widget),
9247                                           quark_parent_window);
9248
9249   if (parent_window != old_parent_window)
9250     {
9251       gboolean is_plug;
9252
9253       g_object_set_qdata (G_OBJECT (widget), quark_parent_window,
9254                           parent_window);
9255       if (old_parent_window)
9256         g_object_unref (old_parent_window);
9257       if (parent_window)
9258         g_object_ref (parent_window);
9259
9260       /* Unset toplevel flag when adding a parent window to a widget,
9261        * this is the primary entry point to allow toplevels to be
9262        * embeddable.
9263        */
9264 #ifdef GDK_WINDOWING_X11
9265       is_plug = GTK_IS_PLUG (widget);
9266 #else
9267       is_plug = FALSE;
9268 #endif
9269       if (GTK_IS_WINDOW (widget) && !is_plug)
9270         _gtk_window_set_is_toplevel (GTK_WINDOW (widget), parent_window == NULL);
9271     }
9272 }
9273
9274 /**
9275  * gtk_widget_get_parent_window:
9276  * @widget: a #GtkWidget.
9277  *
9278  * Gets @widget's parent window.
9279  *
9280  * Returns: (transfer none): the parent window of @widget.
9281  **/
9282 GdkWindow *
9283 gtk_widget_get_parent_window (GtkWidget *widget)
9284 {
9285   GtkWidgetPrivate *priv;
9286   GdkWindow *parent_window;
9287
9288   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9289
9290   priv = widget->priv;
9291
9292   parent_window = g_object_get_qdata (G_OBJECT (widget), quark_parent_window);
9293
9294   return (parent_window != NULL) ? parent_window :
9295          (priv->parent != NULL) ? priv->parent->priv->window : NULL;
9296 }
9297
9298
9299 /**
9300  * gtk_widget_set_child_visible:
9301  * @widget: a #GtkWidget
9302  * @is_visible: if %TRUE, @widget should be mapped along with its parent.
9303  *
9304  * Sets whether @widget should be mapped along with its when its parent
9305  * is mapped and @widget has been shown with gtk_widget_show().
9306  *
9307  * The child visibility can be set for widget before it is added to
9308  * a container with gtk_widget_set_parent(), to avoid mapping
9309  * children unnecessary before immediately unmapping them. However
9310  * it will be reset to its default state of %TRUE when the widget
9311  * is removed from a container.
9312  *
9313  * Note that changing the child visibility of a widget does not
9314  * queue a resize on the widget. Most of the time, the size of
9315  * a widget is computed from all visible children, whether or
9316  * not they are mapped. If this is not the case, the container
9317  * can queue a resize itself.
9318  *
9319  * This function is only useful for container implementations and
9320  * never should be called by an application.
9321  **/
9322 void
9323 gtk_widget_set_child_visible (GtkWidget *widget,
9324                               gboolean   is_visible)
9325 {
9326   GtkWidgetPrivate *priv;
9327
9328   g_return_if_fail (GTK_IS_WIDGET (widget));
9329   g_return_if_fail (!gtk_widget_is_toplevel (widget));
9330
9331   priv = widget->priv;
9332
9333   g_object_ref (widget);
9334   gtk_widget_verify_invariants (widget);
9335
9336   if (is_visible)
9337     priv->child_visible = TRUE;
9338   else
9339     {
9340       GtkWidget *toplevel;
9341
9342       priv->child_visible = FALSE;
9343
9344       toplevel = gtk_widget_get_toplevel (widget);
9345       if (toplevel != widget && gtk_widget_is_toplevel (toplevel))
9346         _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
9347     }
9348
9349   if (priv->parent && gtk_widget_get_realized (priv->parent))
9350     {
9351       if (gtk_widget_get_mapped (priv->parent) &&
9352           priv->child_visible &&
9353           gtk_widget_get_visible (widget))
9354         gtk_widget_map (widget);
9355       else
9356         gtk_widget_unmap (widget);
9357     }
9358
9359   gtk_widget_verify_invariants (widget);
9360   g_object_unref (widget);
9361 }
9362
9363 /**
9364  * gtk_widget_get_child_visible:
9365  * @widget: a #GtkWidget
9366  *
9367  * Gets the value set with gtk_widget_set_child_visible().
9368  * If you feel a need to use this function, your code probably
9369  * needs reorganization.
9370  *
9371  * This function is only useful for container implementations and
9372  * never should be called by an application.
9373  *
9374  * Return value: %TRUE if the widget is mapped with the parent.
9375  **/
9376 gboolean
9377 gtk_widget_get_child_visible (GtkWidget *widget)
9378 {
9379   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9380
9381   return widget->priv->child_visible;
9382 }
9383
9384 static GdkScreen *
9385 gtk_widget_get_screen_unchecked (GtkWidget *widget)
9386 {
9387   GtkWidget *toplevel;
9388
9389   toplevel = gtk_widget_get_toplevel (widget);
9390
9391   if (gtk_widget_is_toplevel (toplevel))
9392     {
9393       if (GTK_IS_WINDOW (toplevel))
9394         return gtk_window_get_screen (GTK_WINDOW (toplevel));
9395       else if (GTK_IS_INVISIBLE (toplevel))
9396         return gtk_invisible_get_screen (GTK_INVISIBLE (widget));
9397     }
9398
9399   return NULL;
9400 }
9401
9402 /**
9403  * gtk_widget_get_screen:
9404  * @widget: a #GtkWidget
9405  *
9406  * Get the #GdkScreen from the toplevel window associated with
9407  * this widget. This function can only be called after the widget
9408  * has been added to a widget hierarchy with a #GtkWindow
9409  * at the top.
9410  *
9411  * In general, you should only create screen specific
9412  * resources when a widget has been realized, and you should
9413  * free those resources when the widget is unrealized.
9414  *
9415  * Return value: (transfer none): the #GdkScreen for the toplevel for this widget.
9416  *
9417  * Since: 2.2
9418  **/
9419 GdkScreen*
9420 gtk_widget_get_screen (GtkWidget *widget)
9421 {
9422   GdkScreen *screen;
9423
9424   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9425
9426   screen = gtk_widget_get_screen_unchecked (widget);
9427
9428   if (screen)
9429     return screen;
9430   else
9431     {
9432 #if 0
9433       g_warning (G_STRLOC ": Can't get associated screen"
9434                  " for a widget unless it is inside a toplevel GtkWindow\n"
9435                  " widget type is %s associated top level type is %s",
9436                  g_type_name (G_OBJECT_TYPE(G_OBJECT (widget))),
9437                  g_type_name (G_OBJECT_TYPE(G_OBJECT (toplevel))));
9438 #endif
9439       return gdk_screen_get_default ();
9440     }
9441 }
9442
9443 /**
9444  * gtk_widget_has_screen:
9445  * @widget: a #GtkWidget
9446  *
9447  * Checks whether there is a #GdkScreen is associated with
9448  * this widget. All toplevel widgets have an associated
9449  * screen, and all widgets added into a hierarchy with a toplevel
9450  * window at the top.
9451  *
9452  * Return value: %TRUE if there is a #GdkScreen associcated
9453  *   with the widget.
9454  *
9455  * Since: 2.2
9456  **/
9457 gboolean
9458 gtk_widget_has_screen (GtkWidget *widget)
9459 {
9460   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9461
9462   return (gtk_widget_get_screen_unchecked (widget) != NULL);
9463 }
9464
9465 /**
9466  * gtk_widget_get_display:
9467  * @widget: a #GtkWidget
9468  *
9469  * Get the #GdkDisplay for the toplevel window associated with
9470  * this widget. This function can only be called after the widget
9471  * has been added to a widget hierarchy with a #GtkWindow at the top.
9472  *
9473  * In general, you should only create display specific
9474  * resources when a widget has been realized, and you should
9475  * free those resources when the widget is unrealized.
9476  *
9477  * Return value: (transfer none): the #GdkDisplay for the toplevel for this widget.
9478  *
9479  * Since: 2.2
9480  **/
9481 GdkDisplay*
9482 gtk_widget_get_display (GtkWidget *widget)
9483 {
9484   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9485
9486   return gdk_screen_get_display (gtk_widget_get_screen (widget));
9487 }
9488
9489 /**
9490  * gtk_widget_get_root_window:
9491  * @widget: a #GtkWidget
9492  *
9493  * Get the root window where this widget is located. This function can
9494  * only be called after the widget has been added to a widget
9495  * hierarchy with #GtkWindow at the top.
9496  *
9497  * The root window is useful for such purposes as creating a popup
9498  * #GdkWindow associated with the window. In general, you should only
9499  * create display specific resources when a widget has been realized,
9500  * and you should free those resources when the widget is unrealized.
9501  *
9502  * Return value: (transfer none): the #GdkWindow root window for the toplevel for this widget.
9503  *
9504  * Since: 2.2
9505  **/
9506 GdkWindow*
9507 gtk_widget_get_root_window (GtkWidget *widget)
9508 {
9509   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9510
9511   return gdk_screen_get_root_window (gtk_widget_get_screen (widget));
9512 }
9513
9514 /**
9515  * gtk_widget_child_focus:
9516  * @widget: a #GtkWidget
9517  * @direction: direction of focus movement
9518  *
9519  * This function is used by custom widget implementations; if you're
9520  * writing an app, you'd use gtk_widget_grab_focus() to move the focus
9521  * to a particular widget, and gtk_container_set_focus_chain() to
9522  * change the focus tab order. So you may want to investigate those
9523  * functions instead.
9524  *
9525  * gtk_widget_child_focus() is called by containers as the user moves
9526  * around the window using keyboard shortcuts. @direction indicates
9527  * what kind of motion is taking place (up, down, left, right, tab
9528  * forward, tab backward). gtk_widget_child_focus() emits the
9529  * #GtkWidget::focus signal; widgets override the default handler
9530  * for this signal in order to implement appropriate focus behavior.
9531  *
9532  * The default ::focus handler for a widget should return %TRUE if
9533  * moving in @direction left the focus on a focusable location inside
9534  * that widget, and %FALSE if moving in @direction moved the focus
9535  * outside the widget. If returning %TRUE, widgets normally
9536  * call gtk_widget_grab_focus() to place the focus accordingly;
9537  * if returning %FALSE, they don't modify the current focus location.
9538  *
9539  * Return value: %TRUE if focus ended up inside @widget
9540  **/
9541 gboolean
9542 gtk_widget_child_focus (GtkWidget       *widget,
9543                         GtkDirectionType direction)
9544 {
9545   gboolean return_val;
9546
9547   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9548
9549   if (!gtk_widget_get_visible (widget) ||
9550       !gtk_widget_is_sensitive (widget))
9551     return FALSE;
9552
9553   /* child widgets must set CAN_FOCUS, containers
9554    * don't have to though.
9555    */
9556   if (!GTK_IS_CONTAINER (widget) &&
9557       !gtk_widget_get_can_focus (widget))
9558     return FALSE;
9559
9560   g_signal_emit (widget,
9561                  widget_signals[FOCUS],
9562                  0,
9563                  direction, &return_val);
9564
9565   return return_val;
9566 }
9567
9568 /**
9569  * gtk_widget_keynav_failed:
9570  * @widget: a #GtkWidget
9571  * @direction: direction of focus movement
9572  *
9573  * This function should be called whenever keyboard navigation within
9574  * a single widget hits a boundary. The function emits the
9575  * #GtkWidget::keynav-failed signal on the widget and its return
9576  * value should be interpreted in a way similar to the return value of
9577  * gtk_widget_child_focus():
9578  *
9579  * When %TRUE is returned, stay in the widget, the failed keyboard
9580  * navigation is Ok and/or there is nowhere we can/should move the
9581  * focus to.
9582  *
9583  * When %FALSE is returned, the caller should continue with keyboard
9584  * navigation outside the widget, e.g. by calling
9585  * gtk_widget_child_focus() on the widget's toplevel.
9586  *
9587  * The default ::keynav-failed handler returns %TRUE for
9588  * %GTK_DIR_TAB_FORWARD and %GTK_DIR_TAB_BACKWARD. For the other
9589  * values of #GtkDirectionType, it looks at the
9590  * #GtkSettings:gtk-keynav-cursor-only setting and returns %FALSE
9591  * if the setting is %TRUE. This way the entire user interface
9592  * becomes cursor-navigatable on input devices such as mobile phones
9593  * which only have cursor keys but no tab key.
9594  *
9595  * Whenever the default handler returns %TRUE, it also calls
9596  * gtk_widget_error_bell() to notify the user of the failed keyboard
9597  * navigation.
9598  *
9599  * A use case for providing an own implementation of ::keynav-failed
9600  * (either by connecting to it or by overriding it) would be a row of
9601  * #GtkEntry widgets where the user should be able to navigate the
9602  * entire row with the cursor keys, as e.g. known from user interfaces
9603  * that require entering license keys.
9604  *
9605  * Return value: %TRUE if stopping keyboard navigation is fine, %FALSE
9606  *               if the emitting widget should try to handle the keyboard
9607  *               navigation attempt in its parent container(s).
9608  *
9609  * Since: 2.12
9610  **/
9611 gboolean
9612 gtk_widget_keynav_failed (GtkWidget        *widget,
9613                           GtkDirectionType  direction)
9614 {
9615   gboolean return_val;
9616
9617   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9618
9619   g_signal_emit (widget, widget_signals[KEYNAV_FAILED], 0,
9620                  direction, &return_val);
9621
9622   return return_val;
9623 }
9624
9625 /**
9626  * gtk_widget_error_bell:
9627  * @widget: a #GtkWidget
9628  *
9629  * Notifies the user about an input-related error on this widget.
9630  * If the #GtkSettings:gtk-error-bell setting is %TRUE, it calls
9631  * gdk_window_beep(), otherwise it does nothing.
9632  *
9633  * Note that the effect of gdk_window_beep() can be configured in many
9634  * ways, depending on the windowing backend and the desktop environment
9635  * or window manager that is used.
9636  *
9637  * Since: 2.12
9638  **/
9639 void
9640 gtk_widget_error_bell (GtkWidget *widget)
9641 {
9642   GtkWidgetPrivate *priv;
9643   GtkSettings* settings;
9644   gboolean beep;
9645
9646   g_return_if_fail (GTK_IS_WIDGET (widget));
9647
9648   priv = widget->priv;
9649
9650   settings = gtk_widget_get_settings (widget);
9651   if (!settings)
9652     return;
9653
9654   g_object_get (settings,
9655                 "gtk-error-bell", &beep,
9656                 NULL);
9657
9658   if (beep && priv->window)
9659     gdk_window_beep (priv->window);
9660 }
9661
9662 static void
9663 gtk_widget_set_usize_internal (GtkWidget          *widget,
9664                                gint                width,
9665                                gint                height,
9666                                GtkQueueResizeFlags flags)
9667 {
9668   GtkWidgetAuxInfo *aux_info;
9669   gboolean changed = FALSE;
9670
9671   g_object_freeze_notify (G_OBJECT (widget));
9672
9673   aux_info = gtk_widget_get_aux_info (widget, TRUE);
9674
9675   if (width > -2 && aux_info->width != width)
9676     {
9677       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
9678         g_object_notify (G_OBJECT (widget), "width-request");
9679       aux_info->width = width;
9680       changed = TRUE;
9681     }
9682   if (height > -2 && aux_info->height != height)
9683     {
9684       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
9685         g_object_notify (G_OBJECT (widget), "height-request");
9686       aux_info->height = height;
9687       changed = TRUE;
9688     }
9689
9690   if (gtk_widget_get_visible (widget) && changed)
9691     {
9692       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
9693         gtk_widget_queue_resize (widget);
9694       else
9695         _gtk_size_group_queue_resize (widget, GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
9696     }
9697
9698   g_object_thaw_notify (G_OBJECT (widget));
9699 }
9700
9701 /**
9702  * gtk_widget_set_size_request:
9703  * @widget: a #GtkWidget
9704  * @width: width @widget should request, or -1 to unset
9705  * @height: height @widget should request, or -1 to unset
9706  *
9707  * Sets the minimum size of a widget; that is, the widget's size
9708  * request will be @width by @height. You can use this function to
9709  * force a widget to be either larger or smaller than it normally
9710  * would be.
9711  *
9712  * In most cases, gtk_window_set_default_size() is a better choice for
9713  * toplevel windows than this function; setting the default size will
9714  * still allow users to shrink the window. Setting the size request
9715  * will force them to leave the window at least as large as the size
9716  * request. When dealing with window sizes,
9717  * gtk_window_set_geometry_hints() can be a useful function as well.
9718  *
9719  * Note the inherent danger of setting any fixed size - themes,
9720  * translations into other languages, different fonts, and user action
9721  * can all change the appropriate size for a given widget. So, it's
9722  * basically impossible to hardcode a size that will always be
9723  * correct.
9724  *
9725  * The size request of a widget is the smallest size a widget can
9726  * accept while still functioning well and drawing itself correctly.
9727  * However in some strange cases a widget may be allocated less than
9728  * its requested size, and in many cases a widget may be allocated more
9729  * space than it requested.
9730  *
9731  * If the size request in a given direction is -1 (unset), then
9732  * the "natural" size request of the widget will be used instead.
9733  *
9734  * Widgets can't actually be allocated a size less than 1 by 1, but
9735  * you can pass 0,0 to this function to mean "as small as possible."
9736  *
9737  * The size request set here does not include any margin from the
9738  * #GtkWidget properties margin-left, margin-right, margin-top, and
9739  * margin-bottom, but it does include pretty much all other padding
9740  * or border properties set by any subclass of #GtkWidget.
9741  **/
9742 void
9743 gtk_widget_set_size_request (GtkWidget *widget,
9744                              gint       width,
9745                              gint       height)
9746 {
9747   g_return_if_fail (GTK_IS_WIDGET (widget));
9748   g_return_if_fail (width >= -1);
9749   g_return_if_fail (height >= -1);
9750
9751   if (width == 0)
9752     width = 1;
9753   if (height == 0)
9754     height = 1;
9755
9756   gtk_widget_set_usize_internal (widget, width, height, 0);
9757 }
9758
9759
9760 /**
9761  * gtk_widget_get_size_request:
9762  * @widget: a #GtkWidget
9763  * @width: (out) (allow-none): return location for width, or %NULL
9764  * @height: (out) (allow-none): return location for height, or %NULL
9765  *
9766  * Gets the size request that was explicitly set for the widget using
9767  * gtk_widget_set_size_request(). A value of -1 stored in @width or
9768  * @height indicates that that dimension has not been set explicitly
9769  * and the natural requisition of the widget will be used intead. See
9770  * gtk_widget_set_size_request(). To get the size a widget will
9771  * actually request, call gtk_widget_get_preferred_size() instead of
9772  * this function.
9773  **/
9774 void
9775 gtk_widget_get_size_request (GtkWidget *widget,
9776                              gint      *width,
9777                              gint      *height)
9778 {
9779   const GtkWidgetAuxInfo *aux_info;
9780
9781   g_return_if_fail (GTK_IS_WIDGET (widget));
9782
9783   aux_info = _gtk_widget_get_aux_info_or_defaults (widget);
9784
9785   if (width)
9786     *width = aux_info->width;
9787
9788   if (height)
9789     *height = aux_info->height;
9790 }
9791
9792 /**
9793  * _gtk_widget_override_size_request:
9794  * @widget: a #GtkWidget
9795  * @width: new forced minimum width
9796  * @height: new forced minimum height
9797  * @old_width: location to store previous forced minimum width
9798  * @old_width: location to store previous forced minumum height
9799  *
9800  * Temporarily establishes a forced minimum size for a widget; this
9801  * is used by GtkWindow when calculating the size to add to the
9802  * window's geometry widget. Cached sizes for the widget and its
9803  * parents are invalidated, so that subsequent calls to the size
9804  * negotiation machinery produce the overriden result, but the
9805  * widget is not queued for relayout or redraw. The old size must
9806  * be restored with _gtk_widget_restore_size_request() or things
9807  * will go screwy.
9808  */
9809 void
9810 _gtk_widget_override_size_request (GtkWidget *widget,
9811                                    int        width,
9812                                    int        height,
9813                                    int       *old_width,
9814                                    int       *old_height)
9815 {
9816   gtk_widget_get_size_request (widget, old_width, old_height);
9817   gtk_widget_set_usize_internal (widget, width, height,
9818                                  GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
9819 }
9820
9821 /**
9822  * _gtk_widget_restore_size_request:
9823  * @widget: a #GtkWidget
9824  * @old_width: saved forced minimum size
9825  * @old_height: saved forced minimum size
9826  *
9827  * Undoes the operation of_gtk_widget_override_size_request().
9828  */
9829 void
9830 _gtk_widget_restore_size_request (GtkWidget *widget,
9831                                   int        old_width,
9832                                   int        old_height)
9833 {
9834   gtk_widget_set_usize_internal (widget, old_width, old_height,
9835                                  GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
9836 }
9837
9838 /**
9839  * gtk_widget_set_events:
9840  * @widget: a #GtkWidget
9841  * @events: event mask
9842  *
9843  * Sets the event mask (see #GdkEventMask) for a widget. The event
9844  * mask determines which events a widget will receive. Keep in mind
9845  * that different widgets have different default event masks, and by
9846  * changing the event mask you may disrupt a widget's functionality,
9847  * so be careful. This function must be called while a widget is
9848  * unrealized. Consider gtk_widget_add_events() for widgets that are
9849  * already realized, or if you want to preserve the existing event
9850  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
9851  * to get events on those widgets, place them inside a #GtkEventBox
9852  * and receive events on the event box.
9853  **/
9854 void
9855 gtk_widget_set_events (GtkWidget *widget,
9856                        gint       events)
9857 {
9858   g_return_if_fail (GTK_IS_WIDGET (widget));
9859   g_return_if_fail (!gtk_widget_get_realized (widget));
9860
9861   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
9862                       GINT_TO_POINTER (events));
9863   g_object_notify (G_OBJECT (widget), "events");
9864 }
9865
9866 /**
9867  * gtk_widget_set_device_events:
9868  * @widget: a #GtkWidget
9869  * @device: a #GdkDevice
9870  * @events: event mask
9871  *
9872  * Sets the device event mask (see #GdkEventMask) for a widget. The event
9873  * mask determines which events a widget will receive from @device. Keep
9874  * in mind that different widgets have different default event masks, and by
9875  * changing the event mask you may disrupt a widget's functionality,
9876  * so be careful. This function must be called while a widget is
9877  * unrealized. Consider gtk_widget_add_device_events() for widgets that are
9878  * already realized, or if you want to preserve the existing event
9879  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
9880  * to get events on those widgets, place them inside a #GtkEventBox
9881  * and receive events on the event box.
9882  *
9883  * Since: 3.0
9884  **/
9885 void
9886 gtk_widget_set_device_events (GtkWidget    *widget,
9887                               GdkDevice    *device,
9888                               GdkEventMask  events)
9889 {
9890   GHashTable *device_events;
9891
9892   g_return_if_fail (GTK_IS_WIDGET (widget));
9893   g_return_if_fail (GDK_IS_DEVICE (device));
9894   g_return_if_fail (!gtk_widget_get_realized (widget));
9895
9896   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
9897
9898   if (G_UNLIKELY (!device_events))
9899     {
9900       device_events = g_hash_table_new (NULL, NULL);
9901       g_object_set_qdata_full (G_OBJECT (widget), quark_device_event_mask, device_events,
9902                                (GDestroyNotify) g_hash_table_unref);
9903     }
9904
9905   g_hash_table_insert (device_events, device, GUINT_TO_POINTER (events));
9906 }
9907
9908 /**
9909  * gtk_widget_set_device_enabled:
9910  * @widget: a #GtkWidget
9911  * @device: a #GdkDevice
9912  * @enabled: whether to enable the device
9913  *
9914  * Enables or disables a #GdkDevice to interact with @widget
9915  * and all its children.
9916  *
9917  * It does so by descending through the #GdkWindow hierarchy
9918  * and enabling the same mask that is has for core events
9919  * (i.e. the one that gdk_window_get_events() returns).
9920  *
9921  * Since: 3.0
9922  */
9923 void
9924 gtk_widget_set_device_enabled (GtkWidget *widget,
9925                                GdkDevice *device,
9926                                gboolean   enabled)
9927 {
9928   GList *enabled_devices;
9929
9930   g_return_if_fail (GTK_IS_WIDGET (widget));
9931   g_return_if_fail (GDK_IS_DEVICE (device));
9932
9933   enabled_devices = g_object_get_qdata (G_OBJECT (widget), quark_enabled_devices);
9934   enabled_devices = g_list_append (enabled_devices, device);
9935
9936   g_object_set_qdata_full (G_OBJECT (widget), quark_enabled_devices,
9937                            enabled_devices, (GDestroyNotify) g_list_free);;
9938
9939   if (gtk_widget_get_realized (widget))
9940     gtk_widget_set_device_enabled_internal (widget, device, TRUE, enabled);
9941 }
9942
9943 /**
9944  * gtk_widget_get_device_enabled:
9945  * @widget: a #GtkWidget
9946  * @device: a #GdkDevice
9947  *
9948  * Returns whether @device can interact with @widget and its
9949  * children. See gtk_widget_set_device_enabled().
9950  *
9951  * Return value: %TRUE is @device is enabled for @widget
9952  *
9953  * Since: 3.0
9954  */
9955 gboolean
9956 gtk_widget_get_device_enabled (GtkWidget *widget,
9957                                GdkDevice *device)
9958 {
9959   GList *enabled_devices;
9960
9961   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9962   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
9963
9964   enabled_devices = g_object_get_qdata (G_OBJECT (widget), quark_enabled_devices);
9965
9966   return g_list_find (enabled_devices, device) != NULL;
9967 }
9968
9969 static void
9970 gtk_widget_add_events_internal_list (GtkWidget *widget,
9971                                      GdkDevice *device,
9972                                      gint       events,
9973                                      GList     *window_list)
9974 {
9975   GList *l;
9976
9977   for (l = window_list; l != NULL; l = l->next)
9978     {
9979       GdkWindow *window = l->data;
9980       gpointer user_data;
9981
9982       gdk_window_get_user_data (window, &user_data);
9983       if (user_data == widget)
9984         {
9985           GList *children;
9986
9987           if (device)
9988             gdk_window_set_device_events (window, device, gdk_window_get_events (window) | events);
9989           else
9990             gdk_window_set_events (window, gdk_window_get_events (window) | events);
9991
9992           children = gdk_window_get_children (window);
9993           gtk_widget_add_events_internal_list (widget, device, events, children);
9994           g_list_free (children);
9995         }
9996     }
9997 }
9998
9999 static void
10000 gtk_widget_add_events_internal (GtkWidget *widget,
10001                                 GdkDevice *device,
10002                                 gint       events)
10003 {
10004   GtkWidgetPrivate *priv = widget->priv;
10005   GList *window_list;
10006
10007   if (!gtk_widget_get_has_window (widget))
10008     window_list = gdk_window_get_children (priv->window);
10009   else
10010     window_list = g_list_prepend (NULL, priv->window);
10011
10012   gtk_widget_add_events_internal_list (widget, device, events, window_list);
10013
10014   g_list_free (window_list);
10015 }
10016
10017 /**
10018  * gtk_widget_add_events:
10019  * @widget: a #GtkWidget
10020  * @events: an event mask, see #GdkEventMask
10021  *
10022  * Adds the events in the bitfield @events to the event mask for
10023  * @widget. See gtk_widget_set_events() for details.
10024  **/
10025 void
10026 gtk_widget_add_events (GtkWidget *widget,
10027                        gint       events)
10028 {
10029   gint old_events;
10030
10031   g_return_if_fail (GTK_IS_WIDGET (widget));
10032
10033   old_events = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
10034   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
10035                       GINT_TO_POINTER (old_events | events));
10036
10037   if (gtk_widget_get_realized (widget))
10038     {
10039       gtk_widget_add_events_internal (widget, NULL, events);
10040       gtk_widget_update_devices_mask (widget, FALSE);
10041     }
10042
10043   g_object_notify (G_OBJECT (widget), "events");
10044 }
10045
10046 /**
10047  * gtk_widget_add_device_events:
10048  * @widget: a #GtkWidget
10049  * @device: a #GdkDevice
10050  * @events: an event mask, see #GdkEventMask
10051  *
10052  * Adds the device events in the bitfield @events to the event mask for
10053  * @widget. See gtk_widget_set_device_events() for details.
10054  *
10055  * Since: 3.0
10056  **/
10057 void
10058 gtk_widget_add_device_events (GtkWidget    *widget,
10059                               GdkDevice    *device,
10060                               GdkEventMask  events)
10061 {
10062   GdkEventMask old_events;
10063   GHashTable *device_events;
10064
10065   g_return_if_fail (GTK_IS_WIDGET (widget));
10066   g_return_if_fail (GDK_IS_DEVICE (device));
10067
10068   old_events = gtk_widget_get_device_events (widget, device);
10069
10070   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
10071
10072   if (G_UNLIKELY (!device_events))
10073     {
10074       device_events = g_hash_table_new (NULL, NULL);
10075       g_object_set_qdata_full (G_OBJECT (widget), quark_device_event_mask, device_events,
10076                                (GDestroyNotify) g_hash_table_unref);
10077     }
10078
10079   g_hash_table_insert (device_events, device,
10080                        GUINT_TO_POINTER (old_events | events));
10081
10082   if (gtk_widget_get_realized (widget))
10083     gtk_widget_add_events_internal (widget, device, events);
10084
10085   g_object_notify (G_OBJECT (widget), "events");
10086 }
10087
10088 /**
10089  * gtk_widget_get_toplevel:
10090  * @widget: a #GtkWidget
10091  *
10092  * This function returns the topmost widget in the container hierarchy
10093  * @widget is a part of. If @widget has no parent widgets, it will be
10094  * returned as the topmost widget. No reference will be added to the
10095  * returned widget; it should not be unreferenced.
10096  *
10097  * Note the difference in behavior vs. gtk_widget_get_ancestor();
10098  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)</literal>
10099  * would return
10100  * %NULL if @widget wasn't inside a toplevel window, and if the
10101  * window was inside a #GtkWindow-derived widget which was in turn
10102  * inside the toplevel #GtkWindow. While the second case may
10103  * seem unlikely, it actually happens when a #GtkPlug is embedded
10104  * inside a #GtkSocket within the same application.
10105  *
10106  * To reliably find the toplevel #GtkWindow, use
10107  * gtk_widget_get_toplevel() and check if the %TOPLEVEL flags
10108  * is set on the result.
10109  * |[
10110  *  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
10111  *  if (gtk_widget_is_toplevel (toplevel))
10112  *    {
10113  *      /&ast; Perform action on toplevel. &ast;/
10114  *    }
10115  * ]|
10116  *
10117  * Return value: (transfer none): the topmost ancestor of @widget, or @widget itself
10118  *    if there's no ancestor.
10119  **/
10120 GtkWidget*
10121 gtk_widget_get_toplevel (GtkWidget *widget)
10122 {
10123   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10124
10125   while (widget->priv->parent)
10126     widget = widget->priv->parent;
10127
10128   return widget;
10129 }
10130
10131 /**
10132  * gtk_widget_get_ancestor:
10133  * @widget: a #GtkWidget
10134  * @widget_type: ancestor type
10135  *
10136  * Gets the first ancestor of @widget with type @widget_type. For example,
10137  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_BOX)</literal> gets
10138  * the first #GtkBox that's an ancestor of @widget. No reference will be
10139  * added to the returned widget; it should not be unreferenced. See note
10140  * about checking for a toplevel #GtkWindow in the docs for
10141  * gtk_widget_get_toplevel().
10142  *
10143  * Note that unlike gtk_widget_is_ancestor(), gtk_widget_get_ancestor()
10144  * considers @widget to be an ancestor of itself.
10145  *
10146  * Return value: (transfer none): the ancestor widget, or %NULL if not found
10147  **/
10148 GtkWidget*
10149 gtk_widget_get_ancestor (GtkWidget *widget,
10150                          GType      widget_type)
10151 {
10152   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10153
10154   while (widget && !g_type_is_a (G_OBJECT_TYPE (widget), widget_type))
10155     widget = widget->priv->parent;
10156
10157   if (!(widget && g_type_is_a (G_OBJECT_TYPE (widget), widget_type)))
10158     return NULL;
10159
10160   return widget;
10161 }
10162
10163 /**
10164  * gtk_widget_set_visual:
10165  * @widget: a #GtkWidget
10166  * @visual: visual to be used or %NULL to unset a previous one
10167  *
10168  * Sets the visual that should be used for by widget and its children for
10169  * creating #GdkWindows. The visual must be on the same #GdkScreen as
10170  * returned by gdk_widget_get_screen(), so handling the
10171  * #GtkWidget::screen-changed signal is necessary.
10172  *
10173  * Setting a new @visual will not cause @widget to recreate its windows,
10174  * so you should call this function before @widget is realized.
10175  **/
10176 void
10177 gtk_widget_set_visual (GtkWidget *widget,
10178                        GdkVisual *visual)
10179 {
10180   g_return_if_fail (GTK_IS_WIDGET (widget));
10181   g_return_if_fail (visual == NULL || GDK_IS_VISUAL (visual));
10182   if (visual)
10183     {
10184       g_return_if_fail (gtk_widget_get_screen (widget) == gdk_visual_get_screen (visual));
10185     }
10186
10187   g_object_set_qdata_full (G_OBJECT (widget),
10188                            quark_visual,
10189                            g_object_ref (visual),
10190                            g_object_unref);
10191 }
10192
10193 /**
10194  * gtk_widget_get_visual:
10195  * @widget: a #GtkWidget
10196  *
10197  * Gets the visual that will be used to render @widget.
10198  *
10199  * Return value: (transfer none): the visual for @widget
10200  **/
10201 GdkVisual*
10202 gtk_widget_get_visual (GtkWidget *widget)
10203 {
10204   GtkWidget *w;
10205   GdkVisual *visual;
10206   GdkScreen *screen;
10207
10208   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10209
10210   if (gtk_widget_get_has_window (widget) &&
10211       widget->priv->window)
10212     return gdk_window_get_visual (widget->priv->window);
10213
10214   screen = gtk_widget_get_screen (widget);
10215
10216   for (w = widget; w != NULL; w = w->priv->parent)
10217     {
10218       visual = g_object_get_qdata (G_OBJECT (w), quark_visual);
10219       if (visual)
10220         {
10221           if (gdk_visual_get_screen (visual) == screen)
10222             return visual;
10223
10224           g_warning ("Ignoring visual set on widget `%s' that is not on the correct screen.",
10225                      gtk_widget_get_name (widget));
10226         }
10227     }
10228
10229   return gdk_screen_get_system_visual (screen);
10230 }
10231
10232 /**
10233  * gtk_widget_get_settings:
10234  * @widget: a #GtkWidget
10235  *
10236  * Gets the settings object holding the settings used for this widget.
10237  *
10238  * Note that this function can only be called when the #GtkWidget
10239  * is attached to a toplevel, since the settings object is specific
10240  * to a particular #GdkScreen.
10241  *
10242  * Return value: (transfer none): the relevant #GtkSettings object
10243  */
10244 GtkSettings*
10245 gtk_widget_get_settings (GtkWidget *widget)
10246 {
10247   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10248
10249   return gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
10250 }
10251
10252 /**
10253  * gtk_widget_get_events:
10254  * @widget: a #GtkWidget
10255  *
10256  * Returns the event mask for the widget (a bitfield containing flags
10257  * from the #GdkEventMask enumeration). These are the events that the widget
10258  * will receive.
10259  *
10260  * Return value: event mask for @widget
10261  **/
10262 gint
10263 gtk_widget_get_events (GtkWidget *widget)
10264 {
10265   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
10266
10267   return GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
10268 }
10269
10270 /**
10271  * gtk_widget_get_device_events:
10272  * @widget: a #GtkWidget
10273  * @device: a #GdkDevice
10274  *
10275  * Returns the events mask for the widget corresponding to an specific device. These
10276  * are the events that the widget will receive when @device operates on it.
10277  *
10278  * Returns: device event mask for @widget
10279  *
10280  * Since: 3.0
10281  **/
10282 GdkEventMask
10283 gtk_widget_get_device_events (GtkWidget *widget,
10284                               GdkDevice *device)
10285 {
10286   GHashTable *device_events;
10287
10288   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
10289   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
10290
10291   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
10292
10293   if (!device_events)
10294     return 0;
10295
10296   return GPOINTER_TO_UINT (g_hash_table_lookup (device_events, device));
10297 }
10298
10299 /**
10300  * gtk_widget_get_pointer:
10301  * @widget: a #GtkWidget
10302  * @x: (out) (allow-none): return location for the X coordinate, or %NULL
10303  * @y: (out) (allow-none): return location for the Y coordinate, or %NULL
10304  *
10305  * Obtains the location of the mouse pointer in widget coordinates.
10306  * Widget coordinates are a bit odd; for historical reasons, they are
10307  * defined as @widget->window coordinates for widgets that are not
10308  * #GTK_NO_WINDOW widgets, and are relative to @widget->allocation.x,
10309  * @widget->allocation.y for widgets that are #GTK_NO_WINDOW widgets.
10310  **/
10311 void
10312 gtk_widget_get_pointer (GtkWidget *widget,
10313                         gint      *x,
10314                         gint      *y)
10315 {
10316   GtkWidgetPrivate *priv;
10317
10318   g_return_if_fail (GTK_IS_WIDGET (widget));
10319
10320   priv = widget->priv;
10321
10322   if (x)
10323     *x = -1;
10324   if (y)
10325     *y = -1;
10326
10327   if (gtk_widget_get_realized (widget))
10328     {
10329       gdk_window_get_pointer (priv->window, x, y, NULL);
10330
10331       if (!gtk_widget_get_has_window (widget))
10332         {
10333           if (x)
10334             *x -= priv->allocation.x;
10335           if (y)
10336             *y -= priv->allocation.y;
10337         }
10338     }
10339 }
10340
10341 /**
10342  * gtk_widget_is_ancestor:
10343  * @widget: a #GtkWidget
10344  * @ancestor: another #GtkWidget
10345  *
10346  * Determines whether @widget is somewhere inside @ancestor, possibly with
10347  * intermediate containers.
10348  *
10349  * Return value: %TRUE if @ancestor contains @widget as a child,
10350  *    grandchild, great grandchild, etc.
10351  **/
10352 gboolean
10353 gtk_widget_is_ancestor (GtkWidget *widget,
10354                         GtkWidget *ancestor)
10355 {
10356   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
10357   g_return_val_if_fail (ancestor != NULL, FALSE);
10358
10359   while (widget)
10360     {
10361       if (widget->priv->parent == ancestor)
10362         return TRUE;
10363       widget = widget->priv->parent;
10364     }
10365
10366   return FALSE;
10367 }
10368
10369 static GQuark quark_composite_name = 0;
10370
10371 /**
10372  * gtk_widget_set_composite_name:
10373  * @widget: a #GtkWidget.
10374  * @name: the name to set
10375  *
10376  * Sets a widgets composite name. The widget must be
10377  * a composite child of its parent; see gtk_widget_push_composite_child().
10378  **/
10379 void
10380 gtk_widget_set_composite_name (GtkWidget   *widget,
10381                                const gchar *name)
10382 {
10383   g_return_if_fail (GTK_IS_WIDGET (widget));
10384   g_return_if_fail (widget->priv->composite_child);
10385   g_return_if_fail (name != NULL);
10386
10387   if (!quark_composite_name)
10388     quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
10389
10390   g_object_set_qdata_full (G_OBJECT (widget),
10391                            quark_composite_name,
10392                            g_strdup (name),
10393                            g_free);
10394 }
10395
10396 /**
10397  * gtk_widget_get_composite_name:
10398  * @widget: a #GtkWidget
10399  *
10400  * Obtains the composite name of a widget.
10401  *
10402  * Returns: the composite name of @widget, or %NULL if @widget is not
10403  *   a composite child. The string should be freed when it is no
10404  *   longer needed.
10405  **/
10406 gchar*
10407 gtk_widget_get_composite_name (GtkWidget *widget)
10408 {
10409   GtkWidgetPrivate *priv;
10410
10411   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10412
10413   priv = widget->priv;
10414
10415   if (widget->priv->composite_child && priv->parent)
10416     return _gtk_container_child_composite_name (GTK_CONTAINER (priv->parent),
10417                                                widget);
10418   else
10419     return NULL;
10420 }
10421
10422 /**
10423  * gtk_widget_push_composite_child:
10424  *
10425  * Makes all newly-created widgets as composite children until
10426  * the corresponding gtk_widget_pop_composite_child() call.
10427  *
10428  * A composite child is a child that's an implementation detail of the
10429  * container it's inside and should not be visible to people using the
10430  * container. Composite children aren't treated differently by GTK (but
10431  * see gtk_container_foreach() vs. gtk_container_forall()), but e.g. GUI
10432  * builders might want to treat them in a different way.
10433  *
10434  * Here is a simple example:
10435  * |[
10436  *   gtk_widget_push_composite_child ();
10437  *   scrolled_window->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
10438  *   gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
10439  *   gtk_widget_pop_composite_child ();
10440  *   gtk_widget_set_parent (scrolled_window->hscrollbar,
10441  *                          GTK_WIDGET (scrolled_window));
10442  *   g_object_ref (scrolled_window->hscrollbar);
10443  * ]|
10444  **/
10445 void
10446 gtk_widget_push_composite_child (void)
10447 {
10448   composite_child_stack++;
10449 }
10450
10451 /**
10452  * gtk_widget_pop_composite_child:
10453  *
10454  * Cancels the effect of a previous call to gtk_widget_push_composite_child().
10455  **/
10456 void
10457 gtk_widget_pop_composite_child (void)
10458 {
10459   if (composite_child_stack)
10460     composite_child_stack--;
10461 }
10462
10463 static void
10464 gtk_widget_emit_direction_changed (GtkWidget        *widget,
10465                                    GtkTextDirection  old_dir)
10466 {
10467   gtk_widget_update_pango_context (widget);
10468
10469   if (widget->priv->context)
10470     gtk_style_context_set_direction (widget->priv->context,
10471                                      gtk_widget_get_direction (widget));
10472
10473   g_signal_emit (widget, widget_signals[DIRECTION_CHANGED], 0, old_dir);
10474 }
10475
10476 /**
10477  * gtk_widget_set_direction:
10478  * @widget: a #GtkWidget
10479  * @dir:    the new direction
10480  *
10481  * Sets the reading direction on a particular widget. This direction
10482  * controls the primary direction for widgets containing text,
10483  * and also the direction in which the children of a container are
10484  * packed. The ability to set the direction is present in order
10485  * so that correct localization into languages with right-to-left
10486  * reading directions can be done. Generally, applications will
10487  * let the default reading direction present, except for containers
10488  * where the containers are arranged in an order that is explicitely
10489  * visual rather than logical (such as buttons for text justification).
10490  *
10491  * If the direction is set to %GTK_TEXT_DIR_NONE, then the value
10492  * set by gtk_widget_set_default_direction() will be used.
10493  **/
10494 void
10495 gtk_widget_set_direction (GtkWidget        *widget,
10496                           GtkTextDirection  dir)
10497 {
10498   GtkTextDirection old_dir;
10499
10500   g_return_if_fail (GTK_IS_WIDGET (widget));
10501   g_return_if_fail (dir >= GTK_TEXT_DIR_NONE && dir <= GTK_TEXT_DIR_RTL);
10502
10503   old_dir = gtk_widget_get_direction (widget);
10504
10505   widget->priv->direction = dir;
10506
10507   if (old_dir != gtk_widget_get_direction (widget))
10508     gtk_widget_emit_direction_changed (widget, old_dir);
10509 }
10510
10511 /**
10512  * gtk_widget_get_direction:
10513  * @widget: a #GtkWidget
10514  *
10515  * Gets the reading direction for a particular widget. See
10516  * gtk_widget_set_direction().
10517  *
10518  * Return value: the reading direction for the widget.
10519  **/
10520 GtkTextDirection
10521 gtk_widget_get_direction (GtkWidget *widget)
10522 {
10523   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_TEXT_DIR_LTR);
10524
10525   if (widget->priv->direction == GTK_TEXT_DIR_NONE)
10526     return gtk_default_direction;
10527   else
10528     return widget->priv->direction;
10529 }
10530
10531 static void
10532 gtk_widget_set_default_direction_recurse (GtkWidget *widget, gpointer data)
10533 {
10534   GtkTextDirection old_dir = GPOINTER_TO_UINT (data);
10535
10536   g_object_ref (widget);
10537
10538   if (widget->priv->direction == GTK_TEXT_DIR_NONE)
10539     gtk_widget_emit_direction_changed (widget, old_dir);
10540
10541   if (GTK_IS_CONTAINER (widget))
10542     gtk_container_forall (GTK_CONTAINER (widget),
10543                           gtk_widget_set_default_direction_recurse,
10544                           data);
10545
10546   g_object_unref (widget);
10547 }
10548
10549 /**
10550  * gtk_widget_set_default_direction:
10551  * @dir: the new default direction. This cannot be
10552  *        %GTK_TEXT_DIR_NONE.
10553  *
10554  * Sets the default reading direction for widgets where the
10555  * direction has not been explicitly set by gtk_widget_set_direction().
10556  **/
10557 void
10558 gtk_widget_set_default_direction (GtkTextDirection dir)
10559 {
10560   g_return_if_fail (dir == GTK_TEXT_DIR_RTL || dir == GTK_TEXT_DIR_LTR);
10561
10562   if (dir != gtk_default_direction)
10563     {
10564       GList *toplevels, *tmp_list;
10565       GtkTextDirection old_dir = gtk_default_direction;
10566
10567       gtk_default_direction = dir;
10568
10569       tmp_list = toplevels = gtk_window_list_toplevels ();
10570       g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
10571
10572       while (tmp_list)
10573         {
10574           gtk_widget_set_default_direction_recurse (tmp_list->data,
10575                                                     GUINT_TO_POINTER (old_dir));
10576           g_object_unref (tmp_list->data);
10577           tmp_list = tmp_list->next;
10578         }
10579
10580       g_list_free (toplevels);
10581     }
10582 }
10583
10584 /**
10585  * gtk_widget_get_default_direction:
10586  *
10587  * Obtains the current default reading direction. See
10588  * gtk_widget_set_default_direction().
10589  *
10590  * Return value: the current default direction.
10591  **/
10592 GtkTextDirection
10593 gtk_widget_get_default_direction (void)
10594 {
10595   return gtk_default_direction;
10596 }
10597
10598 static void
10599 gtk_widget_dispose (GObject *object)
10600 {
10601   GtkWidget *widget = GTK_WIDGET (object);
10602   GtkWidgetPrivate *priv = widget->priv;
10603
10604   if (priv->parent)
10605     gtk_container_remove (GTK_CONTAINER (priv->parent), widget);
10606   else if (gtk_widget_get_visible (widget))
10607     gtk_widget_hide (widget);
10608
10609   priv->visible = FALSE;
10610   if (gtk_widget_get_realized (widget))
10611     gtk_widget_unrealize (widget);
10612
10613   if (!priv->in_destruction)
10614     {
10615       priv->in_destruction = TRUE;
10616       g_signal_emit (object, widget_signals[DESTROY], 0);
10617       priv->in_destruction = FALSE;
10618     }
10619
10620   G_OBJECT_CLASS (gtk_widget_parent_class)->dispose (object);
10621 }
10622
10623 static void
10624 gtk_widget_real_destroy (GtkWidget *object)
10625 {
10626   /* gtk_object_destroy() will already hold a refcount on object */
10627   GtkWidget *widget = GTK_WIDGET (object);
10628   GtkWidgetPrivate *priv = widget->priv;
10629
10630   /* wipe accelerator closures (keep order) */
10631   g_object_set_qdata (G_OBJECT (widget), quark_accel_path, NULL);
10632   g_object_set_qdata (G_OBJECT (widget), quark_accel_closures, NULL);
10633
10634   /* Callers of add_mnemonic_label() should disconnect on ::destroy */
10635   g_object_set_qdata (G_OBJECT (widget), quark_mnemonic_labels, NULL);
10636
10637   gtk_grab_remove (widget);
10638
10639   if (priv->style)
10640     g_object_unref (priv->style);
10641   priv->style = gtk_widget_get_default_style ();
10642   g_object_ref (priv->style);
10643 }
10644
10645 static void
10646 gtk_widget_finalize (GObject *object)
10647 {
10648   GtkWidget *widget = GTK_WIDGET (object);
10649   GtkWidgetPrivate *priv = widget->priv;
10650   GtkWidgetAuxInfo *aux_info;
10651   GtkAccessible *accessible;
10652
10653   gtk_grab_remove (widget);
10654
10655   g_object_unref (priv->style);
10656   priv->style = NULL;
10657
10658   g_free (priv->name);
10659
10660   aux_info = gtk_widget_get_aux_info (widget, FALSE);
10661   if (aux_info)
10662     gtk_widget_aux_info_destroy (aux_info);
10663
10664   accessible = g_object_get_qdata (G_OBJECT (widget), quark_accessible_object);
10665   if (accessible)
10666     g_object_unref (accessible);
10667
10668   if (priv->path)
10669     gtk_widget_path_free (priv->path);
10670
10671   if (priv->context)
10672     g_object_unref (priv->context);
10673
10674   _gtk_widget_free_cached_sizes (widget);
10675
10676   if (g_object_is_floating (object))
10677     g_warning ("A floating object was finalized. This means that someone\n"
10678                "called g_object_unref() on an object that had only a floating\n"
10679                "reference; the initial floating reference is not owned by anyone\n"
10680                "and must be removed with g_object_ref_sink().");
10681
10682   G_OBJECT_CLASS (gtk_widget_parent_class)->finalize (object);
10683 }
10684
10685 /*****************************************
10686  * gtk_widget_real_map:
10687  *
10688  *   arguments:
10689  *
10690  *   results:
10691  *****************************************/
10692
10693 static void
10694 gtk_widget_real_map (GtkWidget *widget)
10695 {
10696   GtkWidgetPrivate *priv = widget->priv;
10697
10698   g_assert (gtk_widget_get_realized (widget));
10699
10700   if (!gtk_widget_get_mapped (widget))
10701     {
10702       gtk_widget_set_mapped (widget, TRUE);
10703
10704       if (gtk_widget_get_has_window (widget))
10705         gdk_window_show (priv->window);
10706     }
10707 }
10708
10709 /*****************************************
10710  * gtk_widget_real_unmap:
10711  *
10712  *   arguments:
10713  *
10714  *   results:
10715  *****************************************/
10716
10717 static void
10718 gtk_widget_real_unmap (GtkWidget *widget)
10719 {
10720   GtkWidgetPrivate *priv = widget->priv;
10721
10722   if (gtk_widget_get_mapped (widget))
10723     {
10724       gtk_widget_set_mapped (widget, FALSE);
10725
10726       if (gtk_widget_get_has_window (widget))
10727         gdk_window_hide (priv->window);
10728     }
10729 }
10730
10731 /*****************************************
10732  * gtk_widget_real_realize:
10733  *
10734  *   arguments:
10735  *
10736  *   results:
10737  *****************************************/
10738
10739 static void
10740 gtk_widget_real_realize (GtkWidget *widget)
10741 {
10742   GtkWidgetPrivate *priv = widget->priv;
10743
10744   g_assert (!gtk_widget_get_has_window (widget));
10745
10746   gtk_widget_set_realized (widget, TRUE);
10747   if (priv->parent)
10748     {
10749       priv->window = gtk_widget_get_parent_window (widget);
10750       g_object_ref (priv->window);
10751     }
10752 }
10753
10754 /*****************************************
10755  * gtk_widget_real_unrealize:
10756  *
10757  *   arguments:
10758  *
10759  *   results:
10760  *****************************************/
10761
10762 static void
10763 gtk_widget_real_unrealize (GtkWidget *widget)
10764 {
10765   GtkWidgetPrivate *priv = widget->priv;
10766
10767   g_assert (!widget->priv->mapped);
10768
10769   /* printf ("unrealizing %s\n", g_type_name (G_TYPE_FROM_INSTANCE (widget)));
10770    */
10771
10772    /* We must do unrealize child widget BEFORE container widget.
10773     * gdk_window_destroy() destroys specified xwindow and its sub-xwindows.
10774     * So, unrealizing container widget bofore its children causes the problem
10775     * (for example, gdk_ic_destroy () with destroyed window causes crash. )
10776     */
10777
10778   if (GTK_IS_CONTAINER (widget))
10779     gtk_container_forall (GTK_CONTAINER (widget),
10780                           (GtkCallback) gtk_widget_unrealize,
10781                           NULL);
10782
10783   if (gtk_widget_get_has_window (widget))
10784     {
10785       gdk_window_set_user_data (priv->window, NULL);
10786       gdk_window_destroy (priv->window);
10787       priv->window = NULL;
10788     }
10789   else
10790     {
10791       g_object_unref (priv->window);
10792       priv->window = NULL;
10793     }
10794
10795   gtk_selection_remove_all (widget);
10796
10797   gtk_widget_set_realized (widget, FALSE);
10798 }
10799
10800 static void
10801 gtk_widget_real_adjust_size_request (GtkWidget         *widget,
10802                                      GtkOrientation     orientation,
10803                                      gint              *minimum_size,
10804                                      gint              *natural_size)
10805 {
10806   const GtkWidgetAuxInfo *aux_info;
10807
10808   aux_info =_gtk_widget_get_aux_info_or_defaults (widget);
10809
10810   if (orientation == GTK_ORIENTATION_HORIZONTAL &&
10811       aux_info->width > 0)
10812     {
10813       *minimum_size = MAX (*minimum_size, aux_info->width);
10814     }
10815   else if (orientation == GTK_ORIENTATION_VERTICAL &&
10816            aux_info->height > 0)
10817     {
10818       *minimum_size = MAX (*minimum_size, aux_info->height);
10819     }
10820
10821   /* Fix it if set_size_request made natural size smaller than min size.
10822    * This would also silently fix broken widgets, but we warn about them
10823    * in gtksizerequest.c when calling their size request vfuncs.
10824    */
10825   *natural_size = MAX (*natural_size, *minimum_size);
10826
10827   if (orientation == GTK_ORIENTATION_HORIZONTAL)
10828     {
10829       *minimum_size += (aux_info->margin.left + aux_info->margin.right);
10830       *natural_size += (aux_info->margin.left + aux_info->margin.right);
10831     }
10832   else
10833     {
10834       *minimum_size += (aux_info->margin.top + aux_info->margin.bottom);
10835       *natural_size += (aux_info->margin.top + aux_info->margin.bottom);
10836     }
10837 }
10838
10839 /**
10840  * _gtk_widget_peek_request_cache:
10841  *
10842  * Returns the address of the widget's request cache (strictly for
10843  * internal use in gtksizerequest.c)
10844  *
10845  * Return value: the address of @widget's size request cache.
10846  **/
10847 gpointer
10848 _gtk_widget_peek_request_cache (GtkWidget *widget)
10849 {
10850   /* Don't bother slowing things down with the return_if_fail guards here */
10851   return &widget->priv->requests;
10852 }
10853
10854 /*
10855  * _gtk_widget_set_device_window:
10856  * @widget: a #GtkWidget
10857  * @device: a #GdkDevice
10858  * @window: the new device window
10859  *
10860  * Sets pointer window for @widget and @device.
10861  * Does not ref @window.
10862  */
10863 void
10864 _gtk_widget_set_device_window (GtkWidget *widget,
10865                                GdkDevice *device,
10866                                GdkWindow *window)
10867 {
10868   GHashTable *device_window;
10869
10870   g_return_if_fail (GTK_IS_WIDGET (widget));
10871   g_return_if_fail (GDK_IS_DEVICE (device));
10872   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
10873
10874   if (!gtk_widget_get_mapped (widget))
10875     return;
10876
10877   device_window = g_object_get_qdata (G_OBJECT (widget), quark_pointer_window);
10878
10879   if (!device_window && window)
10880     {
10881       device_window = g_hash_table_new (NULL, NULL);
10882       g_object_set_qdata_full (G_OBJECT (widget),
10883                                quark_pointer_window,
10884                                device_window,
10885                                (GDestroyNotify) g_hash_table_destroy);
10886     }
10887
10888   if (window)
10889     g_hash_table_insert (device_window, device, window);
10890   else if (device_window)
10891     {
10892       g_hash_table_remove (device_window, device);
10893
10894       if (g_hash_table_size (device_window) == 0)
10895         g_object_set_qdata (G_OBJECT (widget), quark_pointer_window, NULL);
10896     }
10897 }
10898
10899 /*
10900  * _gtk_widget_get_device_window:
10901  * @widget: a #GtkWidget
10902  * @device: a #GdkDevice
10903  *
10904  * Return value: the device window set on @widget, or %NULL
10905  */
10906 GdkWindow *
10907 _gtk_widget_get_device_window (GtkWidget *widget,
10908                                GdkDevice *device)
10909 {
10910   GHashTable *device_window;
10911
10912   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10913   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
10914
10915   if (!gtk_widget_get_mapped (widget))
10916     return NULL;
10917
10918   device_window = g_object_get_qdata (G_OBJECT (widget), quark_pointer_window);
10919
10920   if (!device_window)
10921     return NULL;
10922
10923   return g_hash_table_lookup (device_window, device);
10924 }
10925
10926 /*
10927  * _gtk_widget_list_devices:
10928  * @widget: a #GtkWidget
10929  *
10930  * Returns the list of #GdkDevices that is currently on top
10931  * of any window belonging to @widget.
10932  * Free the list with g_list_free(), the elements are owned
10933  * by GTK+ and must not be freed.
10934  */
10935 GList *
10936 _gtk_widget_list_devices (GtkWidget *widget)
10937 {
10938   GHashTableIter iter;
10939   GHashTable *device_window;
10940   GList *devices = NULL;
10941   gpointer key, value;
10942
10943   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10944
10945   if (!gtk_widget_get_mapped (widget))
10946     return NULL;
10947
10948   device_window = g_object_get_qdata (G_OBJECT (widget), quark_pointer_window);
10949
10950   if (G_UNLIKELY (!device_window))
10951     return NULL;
10952
10953   g_hash_table_iter_init (&iter, device_window);
10954
10955   while (g_hash_table_iter_next (&iter, &key, &value))
10956     devices = g_list_prepend (devices, key);
10957
10958   return devices;
10959 }
10960
10961 static void
10962 synth_crossing (GtkWidget       *widget,
10963                 GdkEventType     type,
10964                 GdkWindow       *window,
10965                 GdkDevice       *device,
10966                 GdkCrossingMode  mode,
10967                 GdkNotifyType    detail)
10968 {
10969   GdkEvent *event;
10970
10971   event = gdk_event_new (type);
10972
10973   event->crossing.window = g_object_ref (window);
10974   event->crossing.send_event = TRUE;
10975   event->crossing.subwindow = g_object_ref (window);
10976   event->crossing.time = GDK_CURRENT_TIME;
10977   event->crossing.x = event->crossing.y = 0;
10978   event->crossing.x_root = event->crossing.y_root = 0;
10979   event->crossing.mode = mode;
10980   event->crossing.detail = detail;
10981   event->crossing.focus = FALSE;
10982   event->crossing.state = 0;
10983   gdk_event_set_device (event, device);
10984
10985   if (!widget)
10986     widget = gtk_get_event_widget (event);
10987
10988   if (widget)
10989     gtk_widget_event_internal (widget, event);
10990
10991   gdk_event_free (event);
10992 }
10993
10994 /*
10995  * _gtk_widget_synthesize_crossing:
10996  * @from: the #GtkWidget the virtual pointer is leaving.
10997  * @to: the #GtkWidget the virtual pointer is moving to.
10998  * @mode: the #GdkCrossingMode to place on the synthesized events.
10999  *
11000  * Generate crossing event(s) on widget state (sensitivity) or GTK+ grab change.
11001  *
11002  * The real pointer window is the window that most recently received an enter notify
11003  * event.  Windows that don't select for crossing events can't become the real
11004  * poiner window.  The real pointer widget that owns the real pointer window.  The
11005  * effective pointer window is the same as the real pointer window unless the real
11006  * pointer widget is either insensitive or there is a grab on a widget that is not
11007  * an ancestor of the real pointer widget (in which case the effective pointer
11008  * window should be the root window).
11009  *
11010  * When the effective pointer window is the same as the real poiner window, we
11011  * receive crossing events from the windowing system.  When the effective pointer
11012  * window changes to become different from the real pointer window we synthesize
11013  * crossing events, attempting to follow X protocol rules:
11014  *
11015  * When the root window becomes the effective pointer window:
11016  *   - leave notify on real pointer window, detail Ancestor
11017  *   - leave notify on all of its ancestors, detail Virtual
11018  *   - enter notify on root window, detail Inferior
11019  *
11020  * When the root window ceases to be the effective pointer window:
11021  *   - leave notify on root window, detail Inferior
11022  *   - enter notify on all ancestors of real pointer window, detail Virtual
11023  *   - enter notify on real pointer window, detail Ancestor
11024  */
11025 void
11026 _gtk_widget_synthesize_crossing (GtkWidget       *from,
11027                                  GtkWidget       *to,
11028                                  GdkDevice       *device,
11029                                  GdkCrossingMode  mode)
11030 {
11031   GdkWindow *from_window = NULL, *to_window = NULL;
11032
11033   g_return_if_fail (from != NULL || to != NULL);
11034
11035   if (from != NULL)
11036     {
11037       from_window = _gtk_widget_get_device_window (from, device);
11038
11039       if (!from_window)
11040         from_window = from->priv->window;
11041     }
11042
11043   if (to != NULL)
11044     {
11045       to_window = _gtk_widget_get_device_window (to, device);
11046
11047       if (!to_window)
11048         to_window = to->priv->window;
11049     }
11050
11051   if (from_window == NULL && to_window == NULL)
11052     ;
11053   else if (from_window != NULL && to_window == NULL)
11054     {
11055       GList *from_ancestors = NULL, *list;
11056       GdkWindow *from_ancestor = from_window;
11057
11058       while (from_ancestor != NULL)
11059         {
11060           from_ancestor = gdk_window_get_effective_parent (from_ancestor);
11061           if (from_ancestor == NULL)
11062             break;
11063           from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
11064         }
11065
11066       synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11067                       device, mode, GDK_NOTIFY_ANCESTOR);
11068       for (list = g_list_last (from_ancestors); list; list = list->prev)
11069         {
11070           synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
11071                           device, mode, GDK_NOTIFY_VIRTUAL);
11072         }
11073
11074       /* XXX: enter/inferior on root window? */
11075
11076       g_list_free (from_ancestors);
11077     }
11078   else if (from_window == NULL && to_window != NULL)
11079     {
11080       GList *to_ancestors = NULL, *list;
11081       GdkWindow *to_ancestor = to_window;
11082
11083       while (to_ancestor != NULL)
11084         {
11085           to_ancestor = gdk_window_get_effective_parent (to_ancestor);
11086           if (to_ancestor == NULL)
11087             break;
11088           to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
11089         }
11090
11091       /* XXX: leave/inferior on root window? */
11092
11093       for (list = to_ancestors; list; list = list->next)
11094         {
11095           synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
11096                           device, mode, GDK_NOTIFY_VIRTUAL);
11097         }
11098       synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11099                       device, mode, GDK_NOTIFY_ANCESTOR);
11100
11101       g_list_free (to_ancestors);
11102     }
11103   else if (from_window == to_window)
11104     ;
11105   else
11106     {
11107       GList *from_ancestors = NULL, *to_ancestors = NULL, *list;
11108       GdkWindow *from_ancestor = from_window, *to_ancestor = to_window;
11109
11110       while (from_ancestor != NULL || to_ancestor != NULL)
11111         {
11112           if (from_ancestor != NULL)
11113             {
11114               from_ancestor = gdk_window_get_effective_parent (from_ancestor);
11115               if (from_ancestor == to_window)
11116                 break;
11117               if (from_ancestor)
11118                 from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
11119             }
11120           if (to_ancestor != NULL)
11121             {
11122               to_ancestor = gdk_window_get_effective_parent (to_ancestor);
11123               if (to_ancestor == from_window)
11124                 break;
11125               if (to_ancestor)
11126                 to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
11127             }
11128         }
11129       if (to_ancestor == from_window)
11130         {
11131           if (mode != GDK_CROSSING_GTK_UNGRAB)
11132             synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11133                             device, mode, GDK_NOTIFY_INFERIOR);
11134           for (list = to_ancestors; list; list = list->next)
11135             synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
11136                             device, mode, GDK_NOTIFY_VIRTUAL);
11137           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11138                           device, mode, GDK_NOTIFY_ANCESTOR);
11139         }
11140       else if (from_ancestor == to_window)
11141         {
11142           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11143                           device, mode, GDK_NOTIFY_ANCESTOR);
11144           for (list = g_list_last (from_ancestors); list; list = list->prev)
11145             {
11146               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
11147                               device, mode, GDK_NOTIFY_VIRTUAL);
11148             }
11149           if (mode != GDK_CROSSING_GTK_GRAB)
11150             synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11151                             device, mode, GDK_NOTIFY_INFERIOR);
11152         }
11153       else
11154         {
11155           while (from_ancestors != NULL && to_ancestors != NULL
11156                  && from_ancestors->data == to_ancestors->data)
11157             {
11158               from_ancestors = g_list_delete_link (from_ancestors,
11159                                                    from_ancestors);
11160               to_ancestors = g_list_delete_link (to_ancestors, to_ancestors);
11161             }
11162
11163           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11164                           device, mode, GDK_NOTIFY_NONLINEAR);
11165
11166           for (list = g_list_last (from_ancestors); list; list = list->prev)
11167             {
11168               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
11169                               device, mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
11170             }
11171           for (list = to_ancestors; list; list = list->next)
11172             {
11173               synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
11174                               device, mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
11175             }
11176           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11177                           device, mode, GDK_NOTIFY_NONLINEAR);
11178         }
11179       g_list_free (from_ancestors);
11180       g_list_free (to_ancestors);
11181     }
11182 }
11183
11184 static void
11185 gtk_widget_propagate_state (GtkWidget    *widget,
11186                             GtkStateData *data)
11187 {
11188   GtkWidgetPrivate *priv = widget->priv;
11189   GtkStateFlags new_flags, old_flags = priv->state_flags;
11190   GtkStateType old_state;
11191
11192   old_state = gtk_widget_get_state (widget);
11193
11194   switch (data->operation)
11195     {
11196     case STATE_CHANGE_REPLACE:
11197       priv->state_flags = data->flags;
11198       break;
11199     case STATE_CHANGE_SET:
11200       priv->state_flags |= data->flags;
11201       break;
11202     case STATE_CHANGE_UNSET:
11203       priv->state_flags &= ~(data->flags);
11204       break;
11205     }
11206
11207   /* make insensitivity unoverridable */
11208   if (!priv->sensitive)
11209     priv->state_flags |= GTK_STATE_FLAG_INSENSITIVE;
11210
11211   if (gtk_widget_is_focus (widget) && !gtk_widget_is_sensitive (widget))
11212     {
11213       GtkWidget *window;
11214
11215       window = gtk_widget_get_toplevel (widget);
11216
11217       if (window && gtk_widget_is_toplevel (window))
11218         gtk_window_set_focus (GTK_WINDOW (window), NULL);
11219     }
11220
11221   new_flags = priv->state_flags;
11222
11223   if (old_flags != new_flags)
11224     {
11225       g_object_ref (widget);
11226
11227       if (!gtk_widget_is_sensitive (widget) && gtk_widget_has_grab (widget))
11228         gtk_grab_remove (widget);
11229
11230       g_signal_emit (widget, widget_signals[STATE_CHANGED], 0, old_state);
11231       g_signal_emit (widget, widget_signals[STATE_FLAGS_CHANGED], 0, old_flags);
11232
11233       if (!priv->shadowed)
11234         {
11235           GList *event_windows = NULL;
11236           GList *devices, *d;
11237
11238           devices = _gtk_widget_list_devices (widget);
11239
11240           for (d = devices; d; d = d->next)
11241             {
11242               GdkWindow *window;
11243               GdkDevice *device;
11244
11245               device = d->data;
11246               window = _gtk_widget_get_device_window (widget, device);
11247
11248               /* Do not propagate more than once to the
11249                * same window if non-multidevice aware.
11250                */
11251               if (!gdk_window_get_support_multidevice (window) &&
11252                   g_list_find (event_windows, window))
11253                 continue;
11254
11255               if (!gtk_widget_is_sensitive (widget))
11256                 _gtk_widget_synthesize_crossing (widget, NULL, d->data,
11257                                                  GDK_CROSSING_STATE_CHANGED);
11258               else if (old_flags & GTK_STATE_FLAG_INSENSITIVE)
11259                 _gtk_widget_synthesize_crossing (NULL, widget, d->data,
11260                                                  GDK_CROSSING_STATE_CHANGED);
11261
11262               event_windows = g_list_prepend (event_windows, window);
11263             }
11264
11265           g_list_free (event_windows);
11266           g_list_free (devices);
11267         }
11268
11269       if (GTK_IS_CONTAINER (widget))
11270         {
11271           GtkStateData child_data = *data;
11272
11273           /* Make sure to only propate the right states further */
11274           child_data.flags &= GTK_STATE_FLAGS_DO_PROPAGATE;
11275
11276           if (child_data.use_forall)
11277             gtk_container_forall (GTK_CONTAINER (widget),
11278                                   (GtkCallback) gtk_widget_propagate_state,
11279                                   &child_data);
11280           else
11281             gtk_container_foreach (GTK_CONTAINER (widget),
11282                                    (GtkCallback) gtk_widget_propagate_state,
11283                                    &child_data);
11284         }
11285
11286       /* Trigger state change transitions for the widget */
11287       if (priv->context &&
11288           gtk_widget_get_mapped (widget))
11289         {
11290           gint diff, flag = 1;
11291
11292           diff = old_flags ^ new_flags;
11293
11294           while (diff != 0)
11295             {
11296               if ((diff & flag) != 0)
11297                 {
11298                   gboolean target;
11299
11300                   target = ((new_flags & flag) != 0);
11301                   _gtk_widget_notify_state_change (widget, flag, target);
11302
11303                   diff &= ~flag;
11304                 }
11305
11306               flag <<= 1;
11307             }
11308         }
11309
11310       g_object_unref (widget);
11311     }
11312 }
11313
11314 static const GtkWidgetAuxInfo default_aux_info = {
11315   -1, -1,
11316   GTK_ALIGN_FILL,
11317   GTK_ALIGN_FILL,
11318   { 0, 0, 0, 0 }
11319 };
11320
11321 /*
11322  * gtk_widget_get_aux_info:
11323  * @widget: a #GtkWidget
11324  * @create: if %TRUE, create the structure if it doesn't exist
11325  *
11326  * Get the #GtkWidgetAuxInfo structure for the widget.
11327  *
11328  * Return value: the #GtkAuxInfo structure for the widget, or
11329  *    %NULL if @create is %FALSE and one doesn't already exist.
11330  */
11331 static GtkWidgetAuxInfo *
11332 gtk_widget_get_aux_info (GtkWidget *widget,
11333                          gboolean   create)
11334 {
11335   GtkWidgetAuxInfo *aux_info;
11336
11337   aux_info = g_object_get_qdata (G_OBJECT (widget), quark_aux_info);
11338   if (!aux_info && create)
11339     {
11340       aux_info = g_slice_new0 (GtkWidgetAuxInfo);
11341
11342       *aux_info = default_aux_info;
11343
11344       g_object_set_qdata (G_OBJECT (widget), quark_aux_info, aux_info);
11345     }
11346
11347   return aux_info;
11348 }
11349
11350 static const GtkWidgetAuxInfo*
11351 _gtk_widget_get_aux_info_or_defaults (GtkWidget *widget)
11352 {
11353   GtkWidgetAuxInfo *aux_info;
11354
11355   aux_info = gtk_widget_get_aux_info (widget, FALSE);
11356   if (aux_info == NULL)
11357     {
11358       return &default_aux_info;
11359     }
11360   else
11361     {
11362       return aux_info;
11363     }
11364 }
11365
11366 /*****************************************
11367  * gtk_widget_aux_info_destroy:
11368  *
11369  *   arguments:
11370  *
11371  *   results:
11372  *****************************************/
11373
11374 static void
11375 gtk_widget_aux_info_destroy (GtkWidgetAuxInfo *aux_info)
11376 {
11377   g_slice_free (GtkWidgetAuxInfo, aux_info);
11378 }
11379
11380 /**
11381  * gtk_widget_shape_combine_region:
11382  * @widget: a #GtkWidget
11383  * @region: (allow-none): shape to be added, or %NULL to remove an existing shape
11384  *
11385  * Sets a shape for this widget's GDK window. This allows for
11386  * transparent windows etc., see gdk_window_shape_combine_region()
11387  * for more information.
11388  *
11389  * Since: 3.0
11390  **/
11391 void
11392 gtk_widget_shape_combine_region (GtkWidget *widget,
11393                                  cairo_region_t *region)
11394 {
11395   GtkWidgetPrivate *priv;
11396
11397   g_return_if_fail (GTK_IS_WIDGET (widget));
11398   /*  set_shape doesn't work on widgets without gdk window */
11399   g_return_if_fail (gtk_widget_get_has_window (widget));
11400
11401   priv = widget->priv;
11402
11403   if (region == NULL)
11404     {
11405       priv->has_shape_mask = FALSE;
11406
11407       if (priv->window)
11408         gdk_window_shape_combine_region (priv->window, NULL, 0, 0);
11409
11410       g_object_set_qdata (G_OBJECT (widget), quark_shape_info, NULL);
11411     }
11412   else
11413     {
11414       priv->has_shape_mask = TRUE;
11415
11416       g_object_set_qdata_full (G_OBJECT (widget), quark_shape_info,
11417                                cairo_region_copy (region),
11418                                (GDestroyNotify) cairo_region_destroy);
11419
11420       /* set shape if widget has a gdk window already.
11421        * otherwise the shape is scheduled to be set by gtk_widget_realize().
11422        */
11423       if (priv->window)
11424         gdk_window_shape_combine_region (priv->window, region, 0, 0);
11425     }
11426 }
11427
11428 /**
11429  * gtk_widget_input_shape_combine_region:
11430  * @widget: a #GtkWidget
11431  * @region: (allow-none): shape to be added, or %NULL to remove an existing shape
11432  *
11433  * Sets an input shape for this widget's GDK window. This allows for
11434  * windows which react to mouse click in a nonrectangular region, see
11435  * gdk_window_input_shape_combine_region() for more information.
11436  *
11437  * Since: 3.0
11438  **/
11439 void
11440 gtk_widget_input_shape_combine_region (GtkWidget *widget,
11441                                        cairo_region_t *region)
11442 {
11443   GtkWidgetPrivate *priv;
11444
11445   g_return_if_fail (GTK_IS_WIDGET (widget));
11446   /*  set_shape doesn't work on widgets without gdk window */
11447   g_return_if_fail (gtk_widget_get_has_window (widget));
11448
11449   priv = widget->priv;
11450
11451   if (region == NULL)
11452     {
11453       if (priv->window)
11454         gdk_window_input_shape_combine_region (priv->window, NULL, 0, 0);
11455
11456       g_object_set_qdata (G_OBJECT (widget), quark_input_shape_info, NULL);
11457     }
11458   else
11459     {
11460       g_object_set_qdata_full (G_OBJECT (widget), quark_input_shape_info,
11461                                cairo_region_copy (region),
11462                                (GDestroyNotify) cairo_region_destroy);
11463
11464       /* set shape if widget has a gdk window already.
11465        * otherwise the shape is scheduled to be set by gtk_widget_realize().
11466        */
11467       if (priv->window)
11468         gdk_window_input_shape_combine_region (priv->window, region, 0, 0);
11469     }
11470 }
11471
11472
11473 /* style properties
11474  */
11475
11476 /**
11477  * gtk_widget_class_install_style_property_parser: (skip)
11478  * @klass: a #GtkWidgetClass
11479  * @pspec: the #GParamSpec for the style property
11480  * @parser: the parser for the style property
11481  *
11482  * Installs a style property on a widget class.
11483  **/
11484 void
11485 gtk_widget_class_install_style_property_parser (GtkWidgetClass     *klass,
11486                                                 GParamSpec         *pspec,
11487                                                 GtkRcPropertyParser parser)
11488 {
11489   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
11490   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
11491   g_return_if_fail (pspec->flags & G_PARAM_READABLE);
11492   g_return_if_fail (!(pspec->flags & (G_PARAM_CONSTRUCT_ONLY | G_PARAM_CONSTRUCT)));
11493
11494   if (g_param_spec_pool_lookup (style_property_spec_pool, pspec->name, G_OBJECT_CLASS_TYPE (klass), FALSE))
11495     {
11496       g_warning (G_STRLOC ": class `%s' already contains a style property named `%s'",
11497                  G_OBJECT_CLASS_NAME (klass),
11498                  pspec->name);
11499       return;
11500     }
11501
11502   g_param_spec_ref_sink (pspec);
11503   g_param_spec_set_qdata (pspec, quark_property_parser, (gpointer) parser);
11504   g_param_spec_pool_insert (style_property_spec_pool, pspec, G_OBJECT_CLASS_TYPE (klass));
11505 }
11506
11507 /**
11508  * gtk_widget_class_install_style_property:
11509  * @klass: a #GtkWidgetClass
11510  * @pspec: the #GParamSpec for the property
11511  *
11512  * Installs a style property on a widget class. The parser for the
11513  * style property is determined by the value type of @pspec.
11514  **/
11515 void
11516 gtk_widget_class_install_style_property (GtkWidgetClass *klass,
11517                                          GParamSpec     *pspec)
11518 {
11519   GtkRcPropertyParser parser;
11520
11521   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
11522   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
11523
11524   parser = _gtk_rc_property_parser_from_type (G_PARAM_SPEC_VALUE_TYPE (pspec));
11525
11526   gtk_widget_class_install_style_property_parser (klass, pspec, parser);
11527 }
11528
11529 /**
11530  * gtk_widget_class_find_style_property:
11531  * @klass: a #GtkWidgetClass
11532  * @property_name: the name of the style property to find
11533  * @returns: (transfer none): the #GParamSpec of the style property or
11534  *   %NULL if @class has no style property with that name.
11535  *
11536  * Finds a style property of a widget class by name.
11537  *
11538  * Since: 2.2
11539  */
11540 GParamSpec*
11541 gtk_widget_class_find_style_property (GtkWidgetClass *klass,
11542                                       const gchar    *property_name)
11543 {
11544   g_return_val_if_fail (property_name != NULL, NULL);
11545
11546   return g_param_spec_pool_lookup (style_property_spec_pool,
11547                                    property_name,
11548                                    G_OBJECT_CLASS_TYPE (klass),
11549                                    TRUE);
11550 }
11551
11552 /**
11553  * gtk_widget_class_list_style_properties:
11554  * @klass: a #GtkWidgetClass
11555  * @n_properties: location to return the number of style properties found
11556  * @returns: (array length=n_properties) (transfer container): an
11557  *       newly allocated array of #GParamSpec*. The array must be
11558  *       freed with g_free().
11559  *
11560  * Returns all style properties of a widget class.
11561  *
11562  * Since: 2.2
11563  */
11564 GParamSpec**
11565 gtk_widget_class_list_style_properties (GtkWidgetClass *klass,
11566                                         guint          *n_properties)
11567 {
11568   GParamSpec **pspecs;
11569   guint n;
11570
11571   pspecs = g_param_spec_pool_list (style_property_spec_pool,
11572                                    G_OBJECT_CLASS_TYPE (klass),
11573                                    &n);
11574   if (n_properties)
11575     *n_properties = n;
11576
11577   return pspecs;
11578 }
11579
11580 /**
11581  * gtk_widget_style_get_property:
11582  * @widget: a #GtkWidget
11583  * @property_name: the name of a style property
11584  * @value: location to return the property value
11585  *
11586  * Gets the value of a style property of @widget.
11587  */
11588 void
11589 gtk_widget_style_get_property (GtkWidget   *widget,
11590                                const gchar *property_name,
11591                                GValue      *value)
11592 {
11593   GParamSpec *pspec;
11594
11595   g_return_if_fail (GTK_IS_WIDGET (widget));
11596   g_return_if_fail (property_name != NULL);
11597   g_return_if_fail (G_IS_VALUE (value));
11598
11599   g_object_ref (widget);
11600   pspec = g_param_spec_pool_lookup (style_property_spec_pool,
11601                                     property_name,
11602                                     G_OBJECT_TYPE (widget),
11603                                     TRUE);
11604   if (!pspec)
11605     g_warning ("%s: widget class `%s' has no property named `%s'",
11606                G_STRLOC,
11607                G_OBJECT_TYPE_NAME (widget),
11608                property_name);
11609   else
11610     {
11611       GtkStyleContext *context;
11612       const GValue *peek_value;
11613       GtkStateFlags state;
11614
11615       context = gtk_widget_get_style_context (widget);
11616       state = gtk_widget_get_state_flags (widget);
11617
11618       peek_value = _gtk_style_context_peek_style_property (context,
11619                                                            G_OBJECT_TYPE (widget),
11620                                                            state, pspec);
11621
11622       /* auto-conversion of the caller's value type
11623        */
11624       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
11625         g_value_copy (peek_value, value);
11626       else if (g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
11627         g_value_transform (peek_value, value);
11628       else
11629         g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
11630                    pspec->name,
11631                    g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
11632                    G_VALUE_TYPE_NAME (value));
11633     }
11634   g_object_unref (widget);
11635 }
11636
11637 /**
11638  * gtk_widget_style_get_valist:
11639  * @widget: a #GtkWidget
11640  * @first_property_name: the name of the first property to get
11641  * @var_args: a <type>va_list</type> of pairs of property names and
11642  *     locations to return the property values, starting with the location
11643  *     for @first_property_name.
11644  *
11645  * Non-vararg variant of gtk_widget_style_get(). Used primarily by language
11646  * bindings.
11647  */
11648 void
11649 gtk_widget_style_get_valist (GtkWidget   *widget,
11650                              const gchar *first_property_name,
11651                              va_list      var_args)
11652 {
11653   GtkStyleContext *context;
11654   GtkStateFlags state;
11655   const gchar *name;
11656
11657   g_return_if_fail (GTK_IS_WIDGET (widget));
11658
11659   g_object_ref (widget);
11660   context = gtk_widget_get_style_context (widget);
11661   state = gtk_widget_get_state_flags (widget);
11662
11663   name = first_property_name;
11664   while (name)
11665     {
11666       const GValue *peek_value;
11667       GParamSpec *pspec;
11668       gchar *error;
11669
11670       pspec = g_param_spec_pool_lookup (style_property_spec_pool,
11671                                         name,
11672                                         G_OBJECT_TYPE (widget),
11673                                         TRUE);
11674       if (!pspec)
11675         {
11676           g_warning ("%s: widget class `%s' has no property named `%s'",
11677                      G_STRLOC,
11678                      G_OBJECT_TYPE_NAME (widget),
11679                      name);
11680           break;
11681         }
11682       /* style pspecs are always readable so we can spare that check here */
11683
11684       peek_value = _gtk_style_context_peek_style_property (context,
11685                                                            G_OBJECT_TYPE (widget),
11686                                                            state, pspec);
11687
11688       G_VALUE_LCOPY (peek_value, var_args, 0, &error);
11689       if (error)
11690         {
11691           g_warning ("%s: %s", G_STRLOC, error);
11692           g_free (error);
11693           break;
11694         }
11695
11696       name = va_arg (var_args, gchar*);
11697     }
11698
11699   g_object_unref (widget);
11700 }
11701
11702 /**
11703  * gtk_widget_style_get:
11704  * @widget: a #GtkWidget
11705  * @first_property_name: the name of the first property to get
11706  * @Varargs: pairs of property names and locations to
11707  *   return the property values, starting with the location for
11708  *   @first_property_name, terminated by %NULL.
11709  *
11710  * Gets the values of a multiple style properties of @widget.
11711  */
11712 void
11713 gtk_widget_style_get (GtkWidget   *widget,
11714                       const gchar *first_property_name,
11715                       ...)
11716 {
11717   va_list var_args;
11718
11719   g_return_if_fail (GTK_IS_WIDGET (widget));
11720
11721   va_start (var_args, first_property_name);
11722   gtk_widget_style_get_valist (widget, first_property_name, var_args);
11723   va_end (var_args);
11724 }
11725
11726 /**
11727  * gtk_widget_path:
11728  * @widget: a #GtkWidget
11729  * @path_length: (out) (allow-none): location to store length of the path,
11730  *     or %NULL
11731  * @path: (out) (allow-none): location to store allocated path string,
11732  *     or %NULL
11733  * @path_reversed: (out) (allow-none): location to store allocated reverse
11734  *     path string, or %NULL
11735  *
11736  * Obtains the full path to @widget. The path is simply the name of a
11737  * widget and all its parents in the container hierarchy, separated by
11738  * periods. The name of a widget comes from
11739  * gtk_widget_get_name(). Paths are used to apply styles to a widget
11740  * in gtkrc configuration files. Widget names are the type of the
11741  * widget by default (e.g. "GtkButton") or can be set to an
11742  * application-specific value with gtk_widget_set_name(). By setting
11743  * the name of a widget, you allow users or theme authors to apply
11744  * styles to that specific widget in their gtkrc
11745  * file. @path_reversed_p fills in the path in reverse order,
11746  * i.e. starting with @widget's name instead of starting with the name
11747  * of @widget's outermost ancestor.
11748  *
11749  * Deprecated:3.0: Use gtk_widget_get_path() instead
11750  **/
11751 void
11752 gtk_widget_path (GtkWidget *widget,
11753                  guint     *path_length,
11754                  gchar    **path,
11755                  gchar    **path_reversed)
11756 {
11757   static gchar *rev_path = NULL;
11758   static guint tmp_path_len = 0;
11759   guint len;
11760
11761   g_return_if_fail (GTK_IS_WIDGET (widget));
11762
11763   len = 0;
11764   do
11765     {
11766       const gchar *string;
11767       const gchar *s;
11768       gchar *d;
11769       guint l;
11770
11771       string = gtk_widget_get_name (widget);
11772       l = strlen (string);
11773       while (tmp_path_len <= len + l + 1)
11774         {
11775           tmp_path_len += INIT_PATH_SIZE;
11776           rev_path = g_realloc (rev_path, tmp_path_len);
11777         }
11778       s = string + l - 1;
11779       d = rev_path + len;
11780       while (s >= string)
11781         *(d++) = *(s--);
11782       len += l;
11783
11784       widget = widget->priv->parent;
11785
11786       if (widget)
11787         rev_path[len++] = '.';
11788       else
11789         rev_path[len++] = 0;
11790     }
11791   while (widget);
11792
11793   if (path_length)
11794     *path_length = len - 1;
11795   if (path_reversed)
11796     *path_reversed = g_strdup (rev_path);
11797   if (path)
11798     {
11799       *path = g_strdup (rev_path);
11800       g_strreverse (*path);
11801     }
11802 }
11803
11804 /**
11805  * gtk_widget_class_path:
11806  * @widget: a #GtkWidget
11807  * @path_length: (out) (allow-none): location to store the length of the
11808  *     class path, or %NULL
11809  * @path: (out) (allow-none): location to store the class path as an
11810  *     allocated string, or %NULL
11811  * @path_reversed: (out) (allow-none): location to store the reverse
11812  *     class path as an allocated string, or %NULL
11813  *
11814  * Same as gtk_widget_path(), but always uses the name of a widget's type,
11815  * never uses a custom name set with gtk_widget_set_name().
11816  *
11817  * Deprecated:3.0: Use gtk_widget_get_path() instead
11818  **/
11819 void
11820 gtk_widget_class_path (GtkWidget *widget,
11821                        guint     *path_length,
11822                        gchar    **path,
11823                        gchar    **path_reversed)
11824 {
11825   static gchar *rev_path = NULL;
11826   static guint tmp_path_len = 0;
11827   guint len;
11828
11829   g_return_if_fail (GTK_IS_WIDGET (widget));
11830
11831   len = 0;
11832   do
11833     {
11834       const gchar *string;
11835       const gchar *s;
11836       gchar *d;
11837       guint l;
11838
11839       string = g_type_name (G_OBJECT_TYPE (widget));
11840       l = strlen (string);
11841       while (tmp_path_len <= len + l + 1)
11842         {
11843           tmp_path_len += INIT_PATH_SIZE;
11844           rev_path = g_realloc (rev_path, tmp_path_len);
11845         }
11846       s = string + l - 1;
11847       d = rev_path + len;
11848       while (s >= string)
11849         *(d++) = *(s--);
11850       len += l;
11851
11852       widget = widget->priv->parent;
11853
11854       if (widget)
11855         rev_path[len++] = '.';
11856       else
11857         rev_path[len++] = 0;
11858     }
11859   while (widget);
11860
11861   if (path_length)
11862     *path_length = len - 1;
11863   if (path_reversed)
11864     *path_reversed = g_strdup (rev_path);
11865   if (path)
11866     {
11867       *path = g_strdup (rev_path);
11868       g_strreverse (*path);
11869     }
11870 }
11871
11872 /**
11873  * gtk_requisition_new:
11874  *
11875  * Allocates a new #GtkRequisition structure and initializes its elements to zero.
11876  *
11877  * Returns: a new empty #GtkRequisition. The newly allocated #GtkRequisition should
11878  *   be freed with gtk_requisition_free().
11879  *
11880  * Since: 3.0
11881  */
11882 GtkRequisition *
11883 gtk_requisition_new (void)
11884 {
11885   return g_slice_new0 (GtkRequisition);
11886 }
11887
11888 /**
11889  * gtk_requisition_copy:
11890  * @requisition: a #GtkRequisition
11891  *
11892  * Copies a #GtkRequisition.
11893  *
11894  * Returns: a copy of @requisition
11895  **/
11896 GtkRequisition *
11897 gtk_requisition_copy (const GtkRequisition *requisition)
11898 {
11899   return g_slice_dup (GtkRequisition, requisition);
11900 }
11901
11902 /**
11903  * gtk_requisition_free:
11904  * @requisition: a #GtkRequisition
11905  *
11906  * Frees a #GtkRequisition.
11907  **/
11908 void
11909 gtk_requisition_free (GtkRequisition *requisition)
11910 {
11911   g_slice_free (GtkRequisition, requisition);
11912 }
11913
11914 G_DEFINE_BOXED_TYPE (GtkRequisition, gtk_requisition,
11915                      gtk_requisition_copy,
11916                      gtk_requisition_free)
11917
11918 /**
11919  * gtk_widget_class_set_accessible_type:
11920  * @widget_class: class to set the accessible type for
11921  * @type: The object type that implements the accessible for @widget_class
11922  *
11923  * Sets the type to be used for creating accessibles for widgets of
11924  * @widget_class. The given @type must be a subtype of the type used for
11925  * accessibles of the parent class.
11926  *
11927  * This function should only be called from class init functions of widgets.
11928  *
11929  * Since: 3.2
11930  **/
11931 void
11932 gtk_widget_class_set_accessible_type (GtkWidgetClass *widget_class,
11933                                       GType           type)
11934 {
11935   GtkWidgetClassPrivate *priv;
11936
11937   g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class));
11938   g_return_if_fail (g_type_is_a (type, widget_class->priv->accessible_type));
11939
11940   priv = widget_class->priv;
11941
11942   priv->accessible_type = type;
11943 }
11944
11945 /**
11946  * gtk_widget_class_get_accessible_type:
11947  * @widget_class: class to query the accessible type for
11948  *
11949  * Returns the #GType for accessibles for widgets of the given
11950  * class. See gtk_widget_class_set_accessible_type().
11951  *
11952  * Returns: the accessible type for @widget_class
11953  *
11954  * Since: 3.2
11955  */
11956 GType
11957 gtk_widget_class_get_accessible_type (GtkWidgetClass *widget_class)
11958 {
11959   GtkWidgetClassPrivate *priv;
11960
11961   g_return_if_fail (GTK_IS_WIDGET_CLASS (widget_class));
11962
11963   priv = widget_class->priv;
11964
11965   return priv->accessible_type;
11966 }
11967
11968 /**
11969  * gtk_widget_get_accessible:
11970  * @widget: a #GtkWidget
11971  *
11972  * Returns the accessible object that describes the widget to an
11973  * assistive technology.
11974  *
11975  * If no accessibility library is loaded (i.e. no ATK implementation library is
11976  * loaded via <envar>GTK_MODULES</envar> or via another application library,
11977  * such as libgnome), then this #AtkObject instance may be a no-op. Likewise,
11978  * if no class-specific #AtkObject implementation is available for the widget
11979  * instance in question, it will inherit an #AtkObject implementation from the
11980  * first ancestor class for which such an implementation is defined.
11981  *
11982  * The documentation of the
11983  * <ulink url="http://library.gnome.org/devel/atk/stable/">ATK</ulink>
11984  * library contains more information about accessible objects and their uses.
11985  *
11986  * Returns: (transfer none): the #AtkObject associated with @widget
11987  */
11988 AtkObject*
11989 gtk_widget_get_accessible (GtkWidget *widget)
11990 {
11991   GtkWidgetClass *klass;
11992
11993   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
11994
11995   klass = GTK_WIDGET_GET_CLASS (widget);
11996
11997   g_return_val_if_fail (klass->get_accessible != NULL, NULL);
11998
11999   return klass->get_accessible (widget);
12000 }
12001
12002 static AtkObject*
12003 gtk_widget_real_get_accessible (GtkWidget *widget)
12004 {
12005   AtkObject* accessible;
12006
12007   accessible = g_object_get_qdata (G_OBJECT (widget),
12008                                    quark_accessible_object);
12009   if (!accessible)
12010   {
12011     GtkWidgetClass *widget_class;
12012     AtkObjectFactory *factory;
12013     AtkRegistry *default_registry;
12014
12015     widget_class = GTK_WIDGET_GET_CLASS (widget);
12016
12017     if (widget_class->priv->accessible_type == GTK_TYPE_ACCESSIBLE)
12018       {
12019         default_registry = atk_get_default_registry ();
12020         factory = atk_registry_get_factory (default_registry,
12021                                             G_TYPE_FROM_INSTANCE (widget));
12022         accessible =
12023           atk_object_factory_create_accessible (factory,
12024                                                 G_OBJECT (widget));
12025       }
12026     else
12027       {
12028         accessible = g_object_new (widget_class->priv->accessible_type, NULL);
12029         atk_object_initialize (accessible, widget);
12030       }
12031
12032     g_object_set_qdata (G_OBJECT (widget),
12033                         quark_accessible_object,
12034                         accessible);
12035   }
12036   return accessible;
12037 }
12038
12039 /*
12040  * Initialize a AtkImplementorIface instance's virtual pointers as
12041  * appropriate to this implementor's class (GtkWidget).
12042  */
12043 static void
12044 gtk_widget_accessible_interface_init (AtkImplementorIface *iface)
12045 {
12046   iface->ref_accessible = gtk_widget_ref_accessible;
12047 }
12048
12049 static AtkObject*
12050 gtk_widget_ref_accessible (AtkImplementor *implementor)
12051 {
12052   AtkObject *accessible;
12053
12054   accessible = gtk_widget_get_accessible (GTK_WIDGET (implementor));
12055   if (accessible)
12056     g_object_ref (accessible);
12057   return accessible;
12058 }
12059
12060 /*
12061  * Expand flag management
12062  */
12063
12064 static void
12065 gtk_widget_update_computed_expand (GtkWidget *widget)
12066 {
12067   GtkWidgetPrivate *priv;
12068
12069   priv = widget->priv;
12070
12071   if (priv->need_compute_expand)
12072     {
12073       gboolean h, v;
12074
12075       if (priv->hexpand_set)
12076         h = priv->hexpand;
12077       else
12078         h = FALSE;
12079
12080       if (priv->vexpand_set)
12081         v = priv->vexpand;
12082       else
12083         v = FALSE;
12084
12085       /* we don't need to use compute_expand if both expands are
12086        * forced by the app
12087        */
12088       if (!(priv->hexpand_set && priv->vexpand_set))
12089         {
12090           if (GTK_WIDGET_GET_CLASS (widget)->compute_expand != NULL)
12091             {
12092               gboolean ignored;
12093
12094               GTK_WIDGET_GET_CLASS (widget)->compute_expand (widget,
12095                                                              priv->hexpand_set ? &ignored : &h,
12096                                                              priv->vexpand_set ? &ignored : &v);
12097             }
12098         }
12099
12100       priv->need_compute_expand = FALSE;
12101       priv->computed_hexpand = h != FALSE;
12102       priv->computed_vexpand = v != FALSE;
12103     }
12104 }
12105
12106 /**
12107  * gtk_widget_queue_compute_expand:
12108  * @widget: a #GtkWidget
12109  *
12110  * Mark @widget as needing to recompute its expand flags. Call
12111  * this function when setting legacy expand child properties
12112  * on the child of a container.
12113  *
12114  * See gtk_widget_compute_expand().
12115  */
12116 void
12117 gtk_widget_queue_compute_expand (GtkWidget *widget)
12118 {
12119   GtkWidget *parent;
12120   gboolean changed_anything;
12121
12122   if (widget->priv->need_compute_expand)
12123     return;
12124
12125   changed_anything = FALSE;
12126   parent = widget;
12127   while (parent != NULL)
12128     {
12129       if (!parent->priv->need_compute_expand)
12130         {
12131           parent->priv->need_compute_expand = TRUE;
12132           changed_anything = TRUE;
12133         }
12134
12135       /* Note: if we had an invariant that "if a child needs to
12136        * compute expand, its parents also do" then we could stop going
12137        * up when we got to a parent that already needed to
12138        * compute. However, in general we compute expand lazily (as
12139        * soon as we see something in a subtree that is expand, we know
12140        * we're expanding) and so this invariant does not hold and we
12141        * have to always walk all the way up in case some ancestor
12142        * is not currently need_compute_expand.
12143        */
12144
12145       parent = parent->priv->parent;
12146     }
12147
12148   /* recomputing expand always requires
12149    * a relayout as well
12150    */
12151   if (changed_anything)
12152     gtk_widget_queue_resize (widget);
12153 }
12154
12155 /**
12156  * gtk_widget_compute_expand:
12157  * @widget: the widget
12158  * @orientation: expand direction
12159  *
12160  * Computes whether a container should give this widget extra space
12161  * when possible. Containers should check this, rather than
12162  * looking at gtk_widget_get_hexpand() or gtk_widget_get_vexpand().
12163  *
12164  * This function already checks whether the widget is visible, so
12165  * visibility does not need to be checked separately. Non-visible
12166  * widgets are not expanded.
12167  *
12168  * The computed expand value uses either the expand setting explicitly
12169  * set on the widget itself, or, if none has been explicitly set,
12170  * the widget may expand if some of its children do.
12171  *
12172  * Return value: whether widget tree rooted here should be expanded
12173  */
12174 gboolean
12175 gtk_widget_compute_expand (GtkWidget     *widget,
12176                            GtkOrientation orientation)
12177 {
12178   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12179
12180   /* We never make a widget expand if not even showing. */
12181   if (!gtk_widget_get_visible (widget))
12182     return FALSE;
12183
12184   gtk_widget_update_computed_expand (widget);
12185
12186   if (orientation == GTK_ORIENTATION_HORIZONTAL)
12187     {
12188       return widget->priv->computed_hexpand;
12189     }
12190   else
12191     {
12192       return widget->priv->computed_vexpand;
12193     }
12194 }
12195
12196 static void
12197 gtk_widget_set_expand (GtkWidget     *widget,
12198                        GtkOrientation orientation,
12199                        gboolean       expand)
12200 {
12201   const char *expand_prop;
12202   const char *expand_set_prop;
12203   gboolean was_both;
12204   GtkWidgetPrivate *priv;
12205
12206   g_return_if_fail (GTK_IS_WIDGET (widget));
12207
12208   priv = widget->priv;
12209
12210   expand = expand != FALSE;
12211
12212   was_both = priv->hexpand && priv->vexpand;
12213
12214   if (orientation == GTK_ORIENTATION_HORIZONTAL)
12215     {
12216       if (priv->hexpand_set &&
12217           priv->hexpand == expand)
12218         return;
12219
12220       priv->hexpand_set = TRUE;
12221       priv->hexpand = expand;
12222
12223       expand_prop = "hexpand";
12224       expand_set_prop = "hexpand-set";
12225     }
12226   else
12227     {
12228       if (priv->vexpand_set &&
12229           priv->vexpand == expand)
12230         return;
12231
12232       priv->vexpand_set = TRUE;
12233       priv->vexpand = expand;
12234
12235       expand_prop = "vexpand";
12236       expand_set_prop = "vexpand-set";
12237     }
12238
12239   gtk_widget_queue_compute_expand (widget);
12240
12241   g_object_freeze_notify (G_OBJECT (widget));
12242   g_object_notify (G_OBJECT (widget), expand_prop);
12243   g_object_notify (G_OBJECT (widget), expand_set_prop);
12244   if (was_both != (priv->hexpand && priv->vexpand))
12245     g_object_notify (G_OBJECT (widget), "expand");
12246   g_object_thaw_notify (G_OBJECT (widget));
12247 }
12248
12249 static void
12250 gtk_widget_set_expand_set (GtkWidget      *widget,
12251                            GtkOrientation  orientation,
12252                            gboolean        set)
12253 {
12254   GtkWidgetPrivate *priv;
12255   const char *prop;
12256
12257   priv = widget->priv;
12258
12259   set = set != FALSE;
12260
12261   if (orientation == GTK_ORIENTATION_HORIZONTAL)
12262     {
12263       if (set == priv->hexpand_set)
12264         return;
12265
12266       priv->hexpand_set = set;
12267       prop = "hexpand-set";
12268     }
12269   else
12270     {
12271       if (set == priv->vexpand_set)
12272         return;
12273
12274       priv->vexpand_set = set;
12275       prop = "vexpand-set";
12276     }
12277
12278   gtk_widget_queue_compute_expand (widget);
12279
12280   g_object_notify (G_OBJECT (widget), prop);
12281 }
12282
12283 /**
12284  * gtk_widget_get_hexpand:
12285  * @widget: the widget
12286  *
12287  * Gets whether the widget would like any available extra horizontal
12288  * space. When a user resizes a #GtkWindow, widgets with expand=TRUE
12289  * generally receive the extra space. For example, a list or
12290  * scrollable area or document in your window would often be set to
12291  * expand.
12292  *
12293  * Containers should use gtk_widget_compute_expand() rather than
12294  * this function, to see whether a widget, or any of its children,
12295  * has the expand flag set. If any child of a widget wants to
12296  * expand, the parent may ask to expand also.
12297  *
12298  * This function only looks at the widget's own hexpand flag, rather
12299  * than computing whether the entire widget tree rooted at this widget
12300  * wants to expand.
12301  *
12302  * Return value: whether hexpand flag is set
12303  */
12304 gboolean
12305 gtk_widget_get_hexpand (GtkWidget *widget)
12306 {
12307   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12308
12309   return widget->priv->hexpand;
12310 }
12311
12312 /**
12313  * gtk_widget_set_hexpand:
12314  * @widget: the widget
12315  * @expand: whether to expand
12316  *
12317  * Sets whether the widget would like any available extra horizontal
12318  * space. When a user resizes a #GtkWindow, widgets with expand=TRUE
12319  * generally receive the extra space. For example, a list or
12320  * scrollable area or document in your window would often be set to
12321  * expand.
12322  *
12323  * Call this function to set the expand flag if you would like your
12324  * widget to become larger horizontally when the window has extra
12325  * room.
12326  *
12327  * By default, widgets automatically expand if any of their children
12328  * want to expand. (To see if a widget will automatically expand given
12329  * its current children and state, call gtk_widget_compute_expand(). A
12330  * container can decide how the expandability of children affects the
12331  * expansion of the container by overriding the compute_expand virtual
12332  * method on #GtkWidget.).
12333  *
12334  * Setting hexpand explicitly with this function will override the
12335  * automatic expand behavior.
12336  *
12337  * This function forces the widget to expand or not to expand,
12338  * regardless of children.  The override occurs because
12339  * gtk_widget_set_hexpand() sets the hexpand-set property (see
12340  * gtk_widget_set_hexpand_set()) which causes the widget's hexpand
12341  * value to be used, rather than looking at children and widget state.
12342  */
12343 void
12344 gtk_widget_set_hexpand (GtkWidget      *widget,
12345                         gboolean        expand)
12346 {
12347   g_return_if_fail (GTK_IS_WIDGET (widget));
12348
12349   gtk_widget_set_expand (widget, GTK_ORIENTATION_HORIZONTAL, expand);
12350 }
12351
12352 /**
12353  * gtk_widget_get_hexpand_set:
12354  * @widget: the widget
12355  *
12356  * Gets whether gtk_widget_set_hexpand() has been used to
12357  * explicitly set the expand flag on this widget.
12358  *
12359  * If hexpand is set, then it overrides any computed
12360  * expand value based on child widgets. If hexpand is not
12361  * set, then the expand value depends on whether any
12362  * children of the widget would like to expand.
12363  *
12364  * There are few reasons to use this function, but it's here
12365  * for completeness and consistency.
12366  *
12367  * Return value: whether hexpand has been explicitly set
12368  */
12369 gboolean
12370 gtk_widget_get_hexpand_set (GtkWidget      *widget)
12371 {
12372   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12373
12374   return widget->priv->hexpand_set;
12375 }
12376
12377 /**
12378  * gtk_widget_set_hexpand_set:
12379  * @widget: the widget
12380  * @set: value for hexpand-set property
12381  *
12382  * Sets whether the hexpand flag (see gtk_widget_get_hexpand()) will
12383  * be used.
12384  *
12385  * The hexpand-set property will be set automatically when you call
12386  * gtk_widget_set_hexpand() to set hexpand, so the most likely
12387  * reason to use this function would be to unset an explicit expand
12388  * flag.
12389  *
12390  * If hexpand is set, then it overrides any computed
12391  * expand value based on child widgets. If hexpand is not
12392  * set, then the expand value depends on whether any
12393  * children of the widget would like to expand.
12394  *
12395  * There are few reasons to use this function, but it's here
12396  * for completeness and consistency.
12397  */
12398 void
12399 gtk_widget_set_hexpand_set (GtkWidget      *widget,
12400                             gboolean        set)
12401 {
12402   g_return_if_fail (GTK_IS_WIDGET (widget));
12403
12404   gtk_widget_set_expand_set (widget, GTK_ORIENTATION_HORIZONTAL, set);
12405 }
12406
12407
12408 /**
12409  * gtk_widget_get_vexpand:
12410  * @widget: the widget
12411  *
12412  * Gets whether the widget would like any available extra vertical
12413  * space.
12414  *
12415  * See gtk_widget_get_hexpand() for more detail.
12416  *
12417  * Return value: whether vexpand flag is set
12418  */
12419 gboolean
12420 gtk_widget_get_vexpand (GtkWidget *widget)
12421 {
12422   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12423
12424   return widget->priv->vexpand;
12425 }
12426
12427 /**
12428  * gtk_widget_set_vexpand:
12429  * @widget: the widget
12430  * @expand: whether to expand
12431  *
12432  * Sets whether the widget would like any available extra vertical
12433  * space.
12434  *
12435  * See gtk_widget_set_hexpand() for more detail.
12436  */
12437 void
12438 gtk_widget_set_vexpand (GtkWidget      *widget,
12439                         gboolean        expand)
12440 {
12441   g_return_if_fail (GTK_IS_WIDGET (widget));
12442
12443   gtk_widget_set_expand (widget, GTK_ORIENTATION_VERTICAL, expand);
12444 }
12445
12446 /**
12447  * gtk_widget_get_vexpand_set:
12448  * @widget: the widget
12449  *
12450  * Gets whether gtk_widget_set_vexpand() has been used to
12451  * explicitly set the expand flag on this widget.
12452  *
12453  * See gtk_widget_get_hexpand_set() for more detail.
12454  *
12455  * Return value: whether vexpand has been explicitly set
12456  */
12457 gboolean
12458 gtk_widget_get_vexpand_set (GtkWidget      *widget)
12459 {
12460   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12461
12462   return widget->priv->vexpand_set;
12463 }
12464
12465 /**
12466  * gtk_widget_set_vexpand_set:
12467  * @widget: the widget
12468  * @set: value for vexpand-set property
12469  *
12470  * Sets whether the vexpand flag (see gtk_widget_get_vexpand()) will
12471  * be used.
12472  *
12473  * See gtk_widget_set_hexpand_set() for more detail.
12474  */
12475 void
12476 gtk_widget_set_vexpand_set (GtkWidget      *widget,
12477                             gboolean        set)
12478 {
12479   g_return_if_fail (GTK_IS_WIDGET (widget));
12480
12481   gtk_widget_set_expand_set (widget, GTK_ORIENTATION_VERTICAL, set);
12482 }
12483
12484 /*
12485  * GtkBuildable implementation
12486  */
12487 static GQuark            quark_builder_has_default = 0;
12488 static GQuark            quark_builder_has_focus = 0;
12489 static GQuark            quark_builder_atk_relations = 0;
12490 static GQuark            quark_builder_set_name = 0;
12491
12492 static void
12493 gtk_widget_buildable_interface_init (GtkBuildableIface *iface)
12494 {
12495   quark_builder_has_default = g_quark_from_static_string ("gtk-builder-has-default");
12496   quark_builder_has_focus = g_quark_from_static_string ("gtk-builder-has-focus");
12497   quark_builder_atk_relations = g_quark_from_static_string ("gtk-builder-atk-relations");
12498   quark_builder_set_name = g_quark_from_static_string ("gtk-builder-set-name");
12499
12500   iface->set_name = gtk_widget_buildable_set_name;
12501   iface->get_name = gtk_widget_buildable_get_name;
12502   iface->get_internal_child = gtk_widget_buildable_get_internal_child;
12503   iface->set_buildable_property = gtk_widget_buildable_set_buildable_property;
12504   iface->parser_finished = gtk_widget_buildable_parser_finished;
12505   iface->custom_tag_start = gtk_widget_buildable_custom_tag_start;
12506   iface->custom_finished = gtk_widget_buildable_custom_finished;
12507 }
12508
12509 static void
12510 gtk_widget_buildable_set_name (GtkBuildable *buildable,
12511                                const gchar  *name)
12512 {
12513   g_object_set_qdata_full (G_OBJECT (buildable), quark_builder_set_name,
12514                            g_strdup (name), g_free);
12515 }
12516
12517 static const gchar *
12518 gtk_widget_buildable_get_name (GtkBuildable *buildable)
12519 {
12520   return g_object_get_qdata (G_OBJECT (buildable), quark_builder_set_name);
12521 }
12522
12523 static GObject *
12524 gtk_widget_buildable_get_internal_child (GtkBuildable *buildable,
12525                                          GtkBuilder   *builder,
12526                                          const gchar  *childname)
12527 {
12528   if (strcmp (childname, "accessible") == 0)
12529     return G_OBJECT (gtk_widget_get_accessible (GTK_WIDGET (buildable)));
12530
12531   return NULL;
12532 }
12533
12534 static void
12535 gtk_widget_buildable_set_buildable_property (GtkBuildable *buildable,
12536                                              GtkBuilder   *builder,
12537                                              const gchar  *name,
12538                                              const GValue *value)
12539 {
12540   if (strcmp (name, "has-default") == 0 && g_value_get_boolean (value))
12541       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_default,
12542                           GINT_TO_POINTER (TRUE));
12543   else if (strcmp (name, "has-focus") == 0 && g_value_get_boolean (value))
12544       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_focus,
12545                           GINT_TO_POINTER (TRUE));
12546   else
12547     g_object_set_property (G_OBJECT (buildable), name, value);
12548 }
12549
12550 typedef struct
12551 {
12552   gchar *action_name;
12553   GString *description;
12554   gchar *context;
12555   gboolean translatable;
12556 } AtkActionData;
12557
12558 typedef struct
12559 {
12560   gchar *target;
12561   gchar *type;
12562 } AtkRelationData;
12563
12564 static void
12565 free_action (AtkActionData *data, gpointer user_data)
12566 {
12567   g_free (data->action_name);
12568   g_string_free (data->description, TRUE);
12569   g_free (data->context);
12570   g_slice_free (AtkActionData, data);
12571 }
12572
12573 static void
12574 free_relation (AtkRelationData *data, gpointer user_data)
12575 {
12576   g_free (data->target);
12577   g_free (data->type);
12578   g_slice_free (AtkRelationData, data);
12579 }
12580
12581 static void
12582 gtk_widget_buildable_parser_finished (GtkBuildable *buildable,
12583                                       GtkBuilder   *builder)
12584 {
12585   GSList *atk_relations;
12586
12587   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_default))
12588     gtk_widget_grab_default (GTK_WIDGET (buildable));
12589   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_focus))
12590     gtk_widget_grab_focus (GTK_WIDGET (buildable));
12591
12592   atk_relations = g_object_get_qdata (G_OBJECT (buildable),
12593                                       quark_builder_atk_relations);
12594   if (atk_relations)
12595     {
12596       AtkObject *accessible;
12597       AtkRelationSet *relation_set;
12598       GSList *l;
12599       GObject *target;
12600       AtkRelationType relation_type;
12601       AtkObject *target_accessible;
12602
12603       accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
12604       relation_set = atk_object_ref_relation_set (accessible);
12605
12606       for (l = atk_relations; l; l = l->next)
12607         {
12608           AtkRelationData *relation = (AtkRelationData*)l->data;
12609
12610           target = gtk_builder_get_object (builder, relation->target);
12611           if (!target)
12612             {
12613               g_warning ("Target object %s in <relation> does not exist",
12614                          relation->target);
12615               continue;
12616             }
12617           target_accessible = gtk_widget_get_accessible (GTK_WIDGET (target));
12618           g_assert (target_accessible != NULL);
12619
12620           relation_type = atk_relation_type_for_name (relation->type);
12621           if (relation_type == ATK_RELATION_NULL)
12622             {
12623               g_warning ("<relation> type %s not found",
12624                          relation->type);
12625               continue;
12626             }
12627           atk_relation_set_add_relation_by_type (relation_set, relation_type,
12628                                                  target_accessible);
12629         }
12630       g_object_unref (relation_set);
12631
12632       g_slist_foreach (atk_relations, (GFunc)free_relation, NULL);
12633       g_slist_free (atk_relations);
12634       g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
12635                           NULL);
12636     }
12637 }
12638
12639 typedef struct
12640 {
12641   GSList *actions;
12642   GSList *relations;
12643 } AccessibilitySubParserData;
12644
12645 static void
12646 accessibility_start_element (GMarkupParseContext  *context,
12647                              const gchar          *element_name,
12648                              const gchar         **names,
12649                              const gchar         **values,
12650                              gpointer              user_data,
12651                              GError              **error)
12652 {
12653   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
12654   guint i;
12655   gint line_number, char_number;
12656
12657   if (strcmp (element_name, "relation") == 0)
12658     {
12659       gchar *target = NULL;
12660       gchar *type = NULL;
12661       AtkRelationData *relation;
12662
12663       for (i = 0; names[i]; i++)
12664         {
12665           if (strcmp (names[i], "target") == 0)
12666             target = g_strdup (values[i]);
12667           else if (strcmp (names[i], "type") == 0)
12668             type = g_strdup (values[i]);
12669           else
12670             {
12671               g_markup_parse_context_get_position (context,
12672                                                    &line_number,
12673                                                    &char_number);
12674               g_set_error (error,
12675                            GTK_BUILDER_ERROR,
12676                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
12677                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
12678                            "<input>",
12679                            line_number, char_number, names[i], "relation");
12680               g_free (target);
12681               g_free (type);
12682               return;
12683             }
12684         }
12685
12686       if (!target || !type)
12687         {
12688           g_markup_parse_context_get_position (context,
12689                                                &line_number,
12690                                                &char_number);
12691           g_set_error (error,
12692                        GTK_BUILDER_ERROR,
12693                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
12694                        "%s:%d:%d <%s> requires attribute \"%s\"",
12695                        "<input>",
12696                        line_number, char_number, "relation",
12697                        type ? "target" : "type");
12698           g_free (target);
12699           g_free (type);
12700           return;
12701         }
12702
12703       relation = g_slice_new (AtkRelationData);
12704       relation->target = target;
12705       relation->type = type;
12706
12707       data->relations = g_slist_prepend (data->relations, relation);
12708     }
12709   else if (strcmp (element_name, "action") == 0)
12710     {
12711       const gchar *action_name = NULL;
12712       const gchar *description = NULL;
12713       const gchar *msg_context = NULL;
12714       gboolean translatable = FALSE;
12715       AtkActionData *action;
12716
12717       for (i = 0; names[i]; i++)
12718         {
12719           if (strcmp (names[i], "action_name") == 0)
12720             action_name = values[i];
12721           else if (strcmp (names[i], "description") == 0)
12722             description = values[i];
12723           else if (strcmp (names[i], "translatable") == 0)
12724             {
12725               if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
12726                 return;
12727             }
12728           else if (strcmp (names[i], "comments") == 0)
12729             {
12730               /* do nothing, comments are for translators */
12731             }
12732           else if (strcmp (names[i], "context") == 0)
12733             msg_context = values[i];
12734           else
12735             {
12736               g_markup_parse_context_get_position (context,
12737                                                    &line_number,
12738                                                    &char_number);
12739               g_set_error (error,
12740                            GTK_BUILDER_ERROR,
12741                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
12742                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
12743                            "<input>",
12744                            line_number, char_number, names[i], "action");
12745               return;
12746             }
12747         }
12748
12749       if (!action_name)
12750         {
12751           g_markup_parse_context_get_position (context,
12752                                                &line_number,
12753                                                &char_number);
12754           g_set_error (error,
12755                        GTK_BUILDER_ERROR,
12756                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
12757                        "%s:%d:%d <%s> requires attribute \"%s\"",
12758                        "<input>",
12759                        line_number, char_number, "action",
12760                        "action_name");
12761           return;
12762         }
12763
12764       action = g_slice_new (AtkActionData);
12765       action->action_name = g_strdup (action_name);
12766       action->description = g_string_new (description);
12767       action->context = g_strdup (msg_context);
12768       action->translatable = translatable;
12769
12770       data->actions = g_slist_prepend (data->actions, action);
12771     }
12772   else if (strcmp (element_name, "accessibility") == 0)
12773     ;
12774   else
12775     g_warning ("Unsupported tag for GtkWidget: %s\n", element_name);
12776 }
12777
12778 static void
12779 accessibility_text (GMarkupParseContext  *context,
12780                     const gchar          *text,
12781                     gsize                 text_len,
12782                     gpointer              user_data,
12783                     GError              **error)
12784 {
12785   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
12786
12787   if (strcmp (g_markup_parse_context_get_element (context), "action") == 0)
12788     {
12789       AtkActionData *action = data->actions->data;
12790
12791       g_string_append_len (action->description, text, text_len);
12792     }
12793 }
12794
12795 static const GMarkupParser accessibility_parser =
12796   {
12797     accessibility_start_element,
12798     NULL,
12799     accessibility_text,
12800   };
12801
12802 typedef struct
12803 {
12804   GObject *object;
12805   guint    key;
12806   guint    modifiers;
12807   gchar   *signal;
12808 } AccelGroupParserData;
12809
12810 static void
12811 accel_group_start_element (GMarkupParseContext  *context,
12812                            const gchar          *element_name,
12813                            const gchar         **names,
12814                            const gchar         **values,
12815                            gpointer              user_data,
12816                            GError              **error)
12817 {
12818   gint i;
12819   guint key = 0;
12820   guint modifiers = 0;
12821   gchar *signal = NULL;
12822   AccelGroupParserData *parser_data = (AccelGroupParserData*)user_data;
12823
12824   for (i = 0; names[i]; i++)
12825     {
12826       if (strcmp (names[i], "key") == 0)
12827         key = gdk_keyval_from_name (values[i]);
12828       else if (strcmp (names[i], "modifiers") == 0)
12829         {
12830           if (!_gtk_builder_flags_from_string (GDK_TYPE_MODIFIER_TYPE,
12831                                                values[i],
12832                                                &modifiers,
12833                                                error))
12834               return;
12835         }
12836       else if (strcmp (names[i], "signal") == 0)
12837         signal = g_strdup (values[i]);
12838     }
12839
12840   if (key == 0 || signal == NULL)
12841     {
12842       g_warning ("<accelerator> requires key and signal attributes");
12843       return;
12844     }
12845   parser_data->key = key;
12846   parser_data->modifiers = modifiers;
12847   parser_data->signal = signal;
12848 }
12849
12850 static const GMarkupParser accel_group_parser =
12851   {
12852     accel_group_start_element,
12853   };
12854
12855 typedef struct
12856 {
12857   GSList *classes;
12858 } StyleParserData;
12859
12860 static void
12861 style_start_element (GMarkupParseContext  *context,
12862                      const gchar          *element_name,
12863                      const gchar         **names,
12864                      const gchar         **values,
12865                      gpointer              user_data,
12866                      GError              **error)
12867 {
12868   StyleParserData *style_data = (StyleParserData *)user_data;
12869   gchar *class_name;
12870
12871   if (strcmp (element_name, "class") == 0)
12872     {
12873       if (g_markup_collect_attributes (element_name,
12874                                        names,
12875                                        values,
12876                                        error,
12877                                        G_MARKUP_COLLECT_STRDUP, "name", &class_name,
12878                                        G_MARKUP_COLLECT_INVALID))
12879         {
12880           style_data->classes = g_slist_append (style_data->classes, class_name);
12881         }
12882     }
12883   else if (strcmp (element_name, "style") == 0)
12884     ;
12885   else
12886     g_warning ("Unsupported tag for GtkWidget: %s\n", element_name);
12887 }
12888
12889 static const GMarkupParser style_parser =
12890   {
12891     style_start_element,
12892   };
12893
12894 static gboolean
12895 gtk_widget_buildable_custom_tag_start (GtkBuildable     *buildable,
12896                                        GtkBuilder       *builder,
12897                                        GObject          *child,
12898                                        const gchar      *tagname,
12899                                        GMarkupParser    *parser,
12900                                        gpointer         *data)
12901 {
12902   g_assert (buildable);
12903
12904   if (strcmp (tagname, "accelerator") == 0)
12905     {
12906       AccelGroupParserData *parser_data;
12907
12908       parser_data = g_slice_new0 (AccelGroupParserData);
12909       parser_data->object = g_object_ref (buildable);
12910       *parser = accel_group_parser;
12911       *data = parser_data;
12912       return TRUE;
12913     }
12914   if (strcmp (tagname, "accessibility") == 0)
12915     {
12916       AccessibilitySubParserData *parser_data;
12917
12918       parser_data = g_slice_new0 (AccessibilitySubParserData);
12919       *parser = accessibility_parser;
12920       *data = parser_data;
12921       return TRUE;
12922     }
12923   if (strcmp (tagname, "style") == 0)
12924     {
12925       StyleParserData *parser_data;
12926
12927       parser_data = g_slice_new0 (StyleParserData);
12928       *parser = style_parser;
12929       *data = parser_data;
12930       return TRUE;
12931     }
12932
12933   return FALSE;
12934 }
12935
12936 void
12937 _gtk_widget_buildable_finish_accelerator (GtkWidget *widget,
12938                                           GtkWidget *toplevel,
12939                                           gpointer   user_data)
12940 {
12941   AccelGroupParserData *accel_data;
12942   GSList *accel_groups;
12943   GtkAccelGroup *accel_group;
12944
12945   g_return_if_fail (GTK_IS_WIDGET (widget));
12946   g_return_if_fail (GTK_IS_WIDGET (toplevel));
12947   g_return_if_fail (user_data != NULL);
12948
12949   accel_data = (AccelGroupParserData*)user_data;
12950   accel_groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
12951   if (g_slist_length (accel_groups) == 0)
12952     {
12953       accel_group = gtk_accel_group_new ();
12954       gtk_window_add_accel_group (GTK_WINDOW (toplevel), accel_group);
12955     }
12956   else
12957     {
12958       g_assert (g_slist_length (accel_groups) == 1);
12959       accel_group = g_slist_nth_data (accel_groups, 0);
12960     }
12961
12962   gtk_widget_add_accelerator (GTK_WIDGET (accel_data->object),
12963                               accel_data->signal,
12964                               accel_group,
12965                               accel_data->key,
12966                               accel_data->modifiers,
12967                               GTK_ACCEL_VISIBLE);
12968
12969   g_object_unref (accel_data->object);
12970   g_free (accel_data->signal);
12971   g_slice_free (AccelGroupParserData, accel_data);
12972 }
12973
12974 static void
12975 gtk_widget_buildable_custom_finished (GtkBuildable *buildable,
12976                                       GtkBuilder   *builder,
12977                                       GObject      *child,
12978                                       const gchar  *tagname,
12979                                       gpointer      user_data)
12980 {
12981   if (strcmp (tagname, "accelerator") == 0)
12982     {
12983       AccelGroupParserData *accel_data;
12984       GtkWidget *toplevel;
12985
12986       accel_data = (AccelGroupParserData*)user_data;
12987       g_assert (accel_data->object);
12988
12989       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (accel_data->object));
12990
12991       _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
12992     }
12993   else if (strcmp (tagname, "accessibility") == 0)
12994     {
12995       AccessibilitySubParserData *a11y_data;
12996
12997       a11y_data = (AccessibilitySubParserData*)user_data;
12998
12999       if (a11y_data->actions)
13000         {
13001           AtkObject *accessible;
13002           AtkAction *action;
13003           gint i, n_actions;
13004           GSList *l;
13005
13006           accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
13007
13008           if (ATK_IS_ACTION (accessible))
13009             {
13010               action = ATK_ACTION (accessible);
13011               n_actions = atk_action_get_n_actions (action);
13012
13013               for (l = a11y_data->actions; l; l = l->next)
13014                 {
13015                   AtkActionData *action_data = (AtkActionData*)l->data;
13016
13017                   for (i = 0; i < n_actions; i++)
13018                     if (strcmp (atk_action_get_name (action, i),
13019                                 action_data->action_name) == 0)
13020                       break;
13021
13022                   if (i < n_actions)
13023                     {
13024                       gchar *description;
13025
13026                       if (action_data->translatable && action_data->description->len)
13027                         description = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
13028                                                                      action_data->context,
13029                                                                      action_data->description->str);
13030                       else
13031                         description = action_data->description->str;
13032
13033                       atk_action_set_description (action, i, description);
13034                     }
13035                 }
13036             }
13037           else
13038             g_warning ("accessibility action on a widget that does not implement AtkAction");
13039
13040           g_slist_foreach (a11y_data->actions, (GFunc)free_action, NULL);
13041           g_slist_free (a11y_data->actions);
13042         }
13043
13044       if (a11y_data->relations)
13045         g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
13046                             a11y_data->relations);
13047
13048       g_slice_free (AccessibilitySubParserData, a11y_data);
13049     }
13050   else if (strcmp (tagname, "style") == 0)
13051     {
13052       StyleParserData *style_data = (StyleParserData *)user_data;
13053       GtkStyleContext *context;
13054       GSList *l;
13055
13056       context = gtk_widget_get_style_context (GTK_WIDGET (buildable));
13057
13058       for (l = style_data->classes; l; l = l->next)
13059         gtk_style_context_add_class (context, (const gchar *)l->data);
13060
13061       gtk_widget_reset_style (GTK_WIDGET (buildable));
13062
13063       g_slist_free_full (style_data->classes, g_free);
13064       g_slice_free (StyleParserData, style_data);
13065     }
13066 }
13067
13068 static GtkSizeRequestMode 
13069 gtk_widget_real_get_request_mode (GtkWidget *widget)
13070
13071   /* By default widgets dont trade size at all. */
13072   return GTK_SIZE_REQUEST_CONSTANT_SIZE;
13073 }
13074
13075 static void
13076 gtk_widget_real_get_width (GtkWidget *widget,
13077                            gint      *minimum_size,
13078                            gint      *natural_size)
13079 {
13080   if (minimum_size)
13081     *minimum_size = 0;
13082
13083   if (natural_size)
13084     *natural_size = 0;
13085 }
13086
13087 static void
13088 gtk_widget_real_get_height (GtkWidget *widget,
13089                             gint      *minimum_size,
13090                             gint      *natural_size)
13091 {
13092   if (minimum_size)
13093     *minimum_size = 0;
13094
13095   if (natural_size)
13096     *natural_size = 0;
13097 }
13098
13099 static void
13100 gtk_widget_real_get_height_for_width (GtkWidget *widget,
13101                                       gint       width,
13102                                       gint      *minimum_height,
13103                                       gint      *natural_height)
13104 {
13105   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
13106 }
13107
13108 static void
13109 gtk_widget_real_get_width_for_height (GtkWidget *widget,
13110                                       gint       height,
13111                                       gint      *minimum_width,
13112                                       gint      *natural_width)
13113 {
13114   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
13115 }
13116
13117 /**
13118  * gtk_widget_get_halign:
13119  * @widget: a #GtkWidget
13120  *
13121  * Gets the value of the #GtkWidget:halign property.
13122  *
13123  * Returns: the horizontal alignment of @widget
13124  */
13125 GtkAlign
13126 gtk_widget_get_halign (GtkWidget *widget)
13127 {
13128   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_ALIGN_FILL);
13129   return _gtk_widget_get_aux_info_or_defaults (widget)->halign;
13130 }
13131
13132 /**
13133  * gtk_widget_set_halign:
13134  * @widget: a #GtkWidget
13135  * @align: the horizontal alignment
13136  *
13137  * Sets the horizontal alignment of @widget.
13138  * See the #GtkWidget:halign property.
13139  */
13140 void
13141 gtk_widget_set_halign (GtkWidget *widget,
13142                        GtkAlign   align)
13143 {
13144   GtkWidgetAuxInfo *aux_info;
13145
13146   g_return_if_fail (GTK_IS_WIDGET (widget));
13147
13148   aux_info = gtk_widget_get_aux_info (widget, TRUE);
13149
13150   if (aux_info->halign == align)
13151     return;
13152
13153   aux_info->halign = align;
13154   gtk_widget_queue_resize (widget);
13155   g_object_notify (G_OBJECT (widget), "halign");
13156 }
13157
13158 /**
13159  * gtk_widget_get_valign:
13160  * @widget: a #GtkWidget
13161  *
13162  * Gets the value of the #GtkWidget:valign property.
13163  *
13164  * Returns: the vertical alignment of @widget
13165  */
13166 GtkAlign
13167 gtk_widget_get_valign (GtkWidget *widget)
13168 {
13169   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_ALIGN_FILL);
13170   return _gtk_widget_get_aux_info_or_defaults (widget)->valign;
13171 }
13172
13173 /**
13174  * gtk_widget_set_valign:
13175  * @widget: a #GtkWidget
13176  * @align: the vertical alignment
13177  *
13178  * Sets the vertical alignment of @widget.
13179  * See the #GtkWidget:valign property.
13180  */
13181 void
13182 gtk_widget_set_valign (GtkWidget *widget,
13183                        GtkAlign   align)
13184 {
13185   GtkWidgetAuxInfo *aux_info;
13186
13187   g_return_if_fail (GTK_IS_WIDGET (widget));
13188
13189   aux_info = gtk_widget_get_aux_info (widget, TRUE);
13190
13191   if (aux_info->valign == align)
13192     return;
13193
13194   aux_info->valign = align;
13195   gtk_widget_queue_resize (widget);
13196   g_object_notify (G_OBJECT (widget), "valign");
13197 }
13198
13199 /**
13200  * gtk_widget_get_margin_left:
13201  * @widget: a #GtkWidget
13202  *
13203  * Gets the value of the #GtkWidget:margin-left property.
13204  *
13205  * Returns: The left margin of @widget
13206  *
13207  * Since: 3.0
13208  */
13209 gint
13210 gtk_widget_get_margin_left (GtkWidget *widget)
13211 {
13212   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13213
13214   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.left;
13215 }
13216
13217 /**
13218  * gtk_widget_set_margin_left:
13219  * @widget: a #GtkWidget
13220  * @margin: the left margin
13221  *
13222  * Sets the left margin of @widget.
13223  * See the #GtkWidget:margin-left property.
13224  *
13225  * Since: 3.0
13226  */
13227 void
13228 gtk_widget_set_margin_left (GtkWidget *widget,
13229                             gint       margin)
13230 {
13231   GtkWidgetAuxInfo *aux_info;
13232
13233   g_return_if_fail (GTK_IS_WIDGET (widget));
13234   g_return_if_fail (margin <= G_MAXINT16);
13235
13236   aux_info = gtk_widget_get_aux_info (widget, TRUE);
13237
13238   if (aux_info->margin.left == margin)
13239     return;
13240
13241   aux_info->margin.left = margin;
13242   gtk_widget_queue_resize (widget);
13243   g_object_notify (G_OBJECT (widget), "margin-left");
13244 }
13245
13246 /**
13247  * gtk_widget_get_margin_right:
13248  * @widget: a #GtkWidget
13249  *
13250  * Gets the value of the #GtkWidget:margin-right property.
13251  *
13252  * Returns: The right margin of @widget
13253  *
13254  * Since: 3.0
13255  */
13256 gint
13257 gtk_widget_get_margin_right (GtkWidget *widget)
13258 {
13259   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13260
13261   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.right;
13262 }
13263
13264 /**
13265  * gtk_widget_set_margin_right:
13266  * @widget: a #GtkWidget
13267  * @margin: the right margin
13268  *
13269  * Sets the right margin of @widget.
13270  * See the #GtkWidget:margin-right property.
13271  *
13272  * Since: 3.0
13273  */
13274 void
13275 gtk_widget_set_margin_right (GtkWidget *widget,
13276                              gint       margin)
13277 {
13278   GtkWidgetAuxInfo *aux_info;
13279
13280   g_return_if_fail (GTK_IS_WIDGET (widget));
13281   g_return_if_fail (margin <= G_MAXINT16);
13282
13283   aux_info = gtk_widget_get_aux_info (widget, TRUE);
13284
13285   if (aux_info->margin.right == margin)
13286     return;
13287
13288   aux_info->margin.right = margin;
13289   gtk_widget_queue_resize (widget);
13290   g_object_notify (G_OBJECT (widget), "margin-right");
13291 }
13292
13293 /**
13294  * gtk_widget_get_margin_top:
13295  * @widget: a #GtkWidget
13296  *
13297  * Gets the value of the #GtkWidget:margin-top property.
13298  *
13299  * Returns: The top margin of @widget
13300  *
13301  * Since: 3.0
13302  */
13303 gint
13304 gtk_widget_get_margin_top (GtkWidget *widget)
13305 {
13306   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13307
13308   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.top;
13309 }
13310
13311 /**
13312  * gtk_widget_set_margin_top:
13313  * @widget: a #GtkWidget
13314  * @margin: the top margin
13315  *
13316  * Sets the top margin of @widget.
13317  * See the #GtkWidget:margin-top property.
13318  *
13319  * Since: 3.0
13320  */
13321 void
13322 gtk_widget_set_margin_top (GtkWidget *widget,
13323                            gint       margin)
13324 {
13325   GtkWidgetAuxInfo *aux_info;
13326
13327   g_return_if_fail (GTK_IS_WIDGET (widget));
13328   g_return_if_fail (margin <= G_MAXINT16);
13329
13330   aux_info = gtk_widget_get_aux_info (widget, TRUE);
13331
13332   if (aux_info->margin.top == margin)
13333     return;
13334
13335   aux_info->margin.top = margin;
13336   gtk_widget_queue_resize (widget);
13337   g_object_notify (G_OBJECT (widget), "margin-top");
13338 }
13339
13340 /**
13341  * gtk_widget_get_margin_bottom:
13342  * @widget: a #GtkWidget
13343  *
13344  * Gets the value of the #GtkWidget:margin-bottom property.
13345  *
13346  * Returns: The bottom margin of @widget
13347  *
13348  * Since: 3.0
13349  */
13350 gint
13351 gtk_widget_get_margin_bottom (GtkWidget *widget)
13352 {
13353   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13354
13355   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.bottom;
13356 }
13357
13358 /**
13359  * gtk_widget_set_margin_bottom:
13360  * @widget: a #GtkWidget
13361  * @margin: the bottom margin
13362  *
13363  * Sets the bottom margin of @widget.
13364  * See the #GtkWidget:margin-bottom property.
13365  *
13366  * Since: 3.0
13367  */
13368 void
13369 gtk_widget_set_margin_bottom (GtkWidget *widget,
13370                               gint       margin)
13371 {
13372   GtkWidgetAuxInfo *aux_info;
13373
13374   g_return_if_fail (GTK_IS_WIDGET (widget));
13375   g_return_if_fail (margin <= G_MAXINT16);
13376
13377   aux_info = gtk_widget_get_aux_info (widget, TRUE);
13378
13379   if (aux_info->margin.bottom == margin)
13380     return;
13381
13382   aux_info->margin.bottom = margin;
13383   gtk_widget_queue_resize (widget);
13384   g_object_notify (G_OBJECT (widget), "margin-bottom");
13385 }
13386
13387 /**
13388  * gtk_widget_get_clipboard:
13389  * @widget: a #GtkWidget
13390  * @selection: a #GdkAtom which identifies the clipboard
13391  *             to use. %GDK_SELECTION_CLIPBOARD gives the
13392  *             default clipboard. Another common value
13393  *             is %GDK_SELECTION_PRIMARY, which gives
13394  *             the primary X selection.
13395  *
13396  * Returns the clipboard object for the given selection to
13397  * be used with @widget. @widget must have a #GdkDisplay
13398  * associated with it, so must be attached to a toplevel
13399  * window.
13400  *
13401  * Return value: (transfer none): the appropriate clipboard object. If no
13402  *             clipboard already exists, a new one will
13403  *             be created. Once a clipboard object has
13404  *             been created, it is persistent for all time.
13405  *
13406  * Since: 2.2
13407  **/
13408 GtkClipboard *
13409 gtk_widget_get_clipboard (GtkWidget *widget, GdkAtom selection)
13410 {
13411   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13412   g_return_val_if_fail (gtk_widget_has_screen (widget), NULL);
13413
13414   return gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
13415                                         selection);
13416 }
13417
13418 /**
13419  * gtk_widget_list_mnemonic_labels:
13420  * @widget: a #GtkWidget
13421  *
13422  * Returns a newly allocated list of the widgets, normally labels, for
13423  * which this widget is the target of a mnemonic (see for example,
13424  * gtk_label_set_mnemonic_widget()).
13425
13426  * The widgets in the list are not individually referenced. If you
13427  * want to iterate through the list and perform actions involving
13428  * callbacks that might destroy the widgets, you
13429  * <emphasis>must</emphasis> call <literal>g_list_foreach (result,
13430  * (GFunc)g_object_ref, NULL)</literal> first, and then unref all the
13431  * widgets afterwards.
13432
13433  * Return value: (element-type GtkWidget) (transfer container): the list of
13434  *  mnemonic labels; free this list
13435  *  with g_list_free() when you are done with it.
13436  *
13437  * Since: 2.4
13438  **/
13439 GList *
13440 gtk_widget_list_mnemonic_labels (GtkWidget *widget)
13441 {
13442   GList *list = NULL;
13443   GSList *l;
13444
13445   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13446
13447   for (l = g_object_get_qdata (G_OBJECT (widget), quark_mnemonic_labels); l; l = l->next)
13448     list = g_list_prepend (list, l->data);
13449
13450   return list;
13451 }
13452
13453 /**
13454  * gtk_widget_add_mnemonic_label:
13455  * @widget: a #GtkWidget
13456  * @label: a #GtkWidget that acts as a mnemonic label for @widget
13457  *
13458  * Adds a widget to the list of mnemonic labels for
13459  * this widget. (See gtk_widget_list_mnemonic_labels()). Note the
13460  * list of mnemonic labels for the widget is cleared when the
13461  * widget is destroyed, so the caller must make sure to update
13462  * its internal state at this point as well, by using a connection
13463  * to the #GtkWidget::destroy signal or a weak notifier.
13464  *
13465  * Since: 2.4
13466  **/
13467 void
13468 gtk_widget_add_mnemonic_label (GtkWidget *widget,
13469                                GtkWidget *label)
13470 {
13471   GSList *old_list, *new_list;
13472
13473   g_return_if_fail (GTK_IS_WIDGET (widget));
13474   g_return_if_fail (GTK_IS_WIDGET (label));
13475
13476   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
13477   new_list = g_slist_prepend (old_list, label);
13478
13479   g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
13480                            new_list, (GDestroyNotify) g_slist_free);
13481 }
13482
13483 /**
13484  * gtk_widget_remove_mnemonic_label:
13485  * @widget: a #GtkWidget
13486  * @label: a #GtkWidget that was previously set as a mnemnic label for
13487  *         @widget with gtk_widget_add_mnemonic_label().
13488  *
13489  * Removes a widget from the list of mnemonic labels for
13490  * this widget. (See gtk_widget_list_mnemonic_labels()). The widget
13491  * must have previously been added to the list with
13492  * gtk_widget_add_mnemonic_label().
13493  *
13494  * Since: 2.4
13495  **/
13496 void
13497 gtk_widget_remove_mnemonic_label (GtkWidget *widget,
13498                                   GtkWidget *label)
13499 {
13500   GSList *old_list, *new_list;
13501
13502   g_return_if_fail (GTK_IS_WIDGET (widget));
13503   g_return_if_fail (GTK_IS_WIDGET (label));
13504
13505   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
13506   new_list = g_slist_remove (old_list, label);
13507
13508   if (new_list)
13509     g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
13510                              new_list, (GDestroyNotify) g_slist_free);
13511 }
13512
13513 /**
13514  * gtk_widget_get_no_show_all:
13515  * @widget: a #GtkWidget
13516  *
13517  * Returns the current value of the #GtkWidget:no-show-all property,
13518  * which determines whether calls to gtk_widget_show_all()
13519  * will affect this widget.
13520  *
13521  * Return value: the current value of the "no-show-all" property.
13522  *
13523  * Since: 2.4
13524  **/
13525 gboolean
13526 gtk_widget_get_no_show_all (GtkWidget *widget)
13527 {
13528   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
13529
13530   return widget->priv->no_show_all;
13531 }
13532
13533 /**
13534  * gtk_widget_set_no_show_all:
13535  * @widget: a #GtkWidget
13536  * @no_show_all: the new value for the "no-show-all" property
13537  *
13538  * Sets the #GtkWidget:no-show-all property, which determines whether
13539  * calls to gtk_widget_show_all() will affect this widget.
13540  *
13541  * This is mostly for use in constructing widget hierarchies with externally
13542  * controlled visibility, see #GtkUIManager.
13543  *
13544  * Since: 2.4
13545  **/
13546 void
13547 gtk_widget_set_no_show_all (GtkWidget *widget,
13548                             gboolean   no_show_all)
13549 {
13550   g_return_if_fail (GTK_IS_WIDGET (widget));
13551
13552   no_show_all = (no_show_all != FALSE);
13553
13554   if (widget->priv->no_show_all != no_show_all)
13555     {
13556       widget->priv->no_show_all = no_show_all;
13557
13558       g_object_notify (G_OBJECT (widget), "no-show-all");
13559     }
13560 }
13561
13562
13563 static void
13564 gtk_widget_real_set_has_tooltip (GtkWidget *widget,
13565                                  gboolean   has_tooltip,
13566                                  gboolean   force)
13567 {
13568   GtkWidgetPrivate *priv = widget->priv;
13569   gboolean priv_has_tooltip;
13570
13571   priv_has_tooltip = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget),
13572                                        quark_has_tooltip));
13573
13574   if (priv_has_tooltip != has_tooltip || force)
13575     {
13576       priv_has_tooltip = has_tooltip;
13577
13578       if (priv_has_tooltip)
13579         {
13580           if (gtk_widget_get_realized (widget) && !gtk_widget_get_has_window (widget))
13581             gdk_window_set_events (priv->window,
13582                                    gdk_window_get_events (priv->window) |
13583                                    GDK_LEAVE_NOTIFY_MASK |
13584                                    GDK_POINTER_MOTION_MASK |
13585                                    GDK_POINTER_MOTION_HINT_MASK);
13586
13587           if (gtk_widget_get_has_window (widget))
13588               gtk_widget_add_events (widget,
13589                                      GDK_LEAVE_NOTIFY_MASK |
13590                                      GDK_POINTER_MOTION_MASK |
13591                                      GDK_POINTER_MOTION_HINT_MASK);
13592         }
13593
13594       g_object_set_qdata (G_OBJECT (widget), quark_has_tooltip,
13595                           GUINT_TO_POINTER (priv_has_tooltip));
13596     }
13597 }
13598
13599 /**
13600  * gtk_widget_set_tooltip_window:
13601  * @widget: a #GtkWidget
13602  * @custom_window: (allow-none): a #GtkWindow, or %NULL
13603  *
13604  * Replaces the default, usually yellow, window used for displaying
13605  * tooltips with @custom_window. GTK+ will take care of showing and
13606  * hiding @custom_window at the right moment, to behave likewise as
13607  * the default tooltip window. If @custom_window is %NULL, the default
13608  * tooltip window will be used.
13609  *
13610  * If the custom window should have the default theming it needs to
13611  * have the name "gtk-tooltip", see gtk_widget_set_name().
13612  *
13613  * Since: 2.12
13614  */
13615 void
13616 gtk_widget_set_tooltip_window (GtkWidget *widget,
13617                                GtkWindow *custom_window)
13618 {
13619   gboolean has_tooltip;
13620   gchar *tooltip_markup;
13621
13622   g_return_if_fail (GTK_IS_WIDGET (widget));
13623   g_return_if_fail (custom_window == NULL || GTK_IS_WINDOW (custom_window));
13624
13625   tooltip_markup = g_object_get_qdata (G_OBJECT (widget), quark_tooltip_markup);
13626
13627   if (custom_window)
13628     g_object_ref (custom_window);
13629
13630   g_object_set_qdata_full (G_OBJECT (widget), quark_tooltip_window,
13631                            custom_window, g_object_unref);
13632
13633   has_tooltip = (custom_window != NULL || tooltip_markup != NULL);
13634   gtk_widget_real_set_has_tooltip (widget, has_tooltip, FALSE);
13635
13636   if (has_tooltip && gtk_widget_get_visible (widget))
13637     gtk_widget_queue_tooltip_query (widget);
13638 }
13639
13640 /**
13641  * gtk_widget_get_tooltip_window:
13642  * @widget: a #GtkWidget
13643  *
13644  * Returns the #GtkWindow of the current tooltip. This can be the
13645  * GtkWindow created by default, or the custom tooltip window set
13646  * using gtk_widget_set_tooltip_window().
13647  *
13648  * Return value: (transfer none): The #GtkWindow of the current tooltip.
13649  *
13650  * Since: 2.12
13651  */
13652 GtkWindow *
13653 gtk_widget_get_tooltip_window (GtkWidget *widget)
13654 {
13655   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13656
13657   return g_object_get_qdata (G_OBJECT (widget), quark_tooltip_window);
13658 }
13659
13660 /**
13661  * gtk_widget_trigger_tooltip_query:
13662  * @widget: a #GtkWidget
13663  *
13664  * Triggers a tooltip query on the display where the toplevel of @widget
13665  * is located. See gtk_tooltip_trigger_tooltip_query() for more
13666  * information.
13667  *
13668  * Since: 2.12
13669  */
13670 void
13671 gtk_widget_trigger_tooltip_query (GtkWidget *widget)
13672 {
13673   gtk_tooltip_trigger_tooltip_query (gtk_widget_get_display (widget));
13674 }
13675
13676 static guint tooltip_query_id;
13677 static GSList *tooltip_query_displays;
13678
13679 static gboolean
13680 tooltip_query_idle (gpointer data)
13681 {
13682   g_slist_foreach (tooltip_query_displays, (GFunc)gtk_tooltip_trigger_tooltip_query, NULL);
13683   g_slist_foreach (tooltip_query_displays, (GFunc)g_object_unref, NULL);
13684   g_slist_free (tooltip_query_displays);
13685
13686   tooltip_query_displays = NULL;
13687   tooltip_query_id = 0;
13688
13689   return FALSE;
13690 }
13691
13692 static void
13693 gtk_widget_queue_tooltip_query (GtkWidget *widget)
13694 {
13695   GdkDisplay *display;
13696
13697   display = gtk_widget_get_display (widget);
13698
13699   if (!g_slist_find (tooltip_query_displays, display))
13700     tooltip_query_displays = g_slist_prepend (tooltip_query_displays, g_object_ref (display));
13701
13702   if (tooltip_query_id == 0)
13703     tooltip_query_id = gdk_threads_add_idle (tooltip_query_idle, NULL);
13704 }
13705
13706 /**
13707  * gtk_widget_set_tooltip_text:
13708  * @widget: a #GtkWidget
13709  * @text: the contents of the tooltip for @widget
13710  *
13711  * Sets @text as the contents of the tooltip. This function will take
13712  * care of setting #GtkWidget:has-tooltip to %TRUE and of the default
13713  * handler for the #GtkWidget::query-tooltip signal.
13714  *
13715  * See also the #GtkWidget:tooltip-text property and gtk_tooltip_set_text().
13716  *
13717  * Since: 2.12
13718  */
13719 void
13720 gtk_widget_set_tooltip_text (GtkWidget   *widget,
13721                              const gchar *text)
13722 {
13723   g_return_if_fail (GTK_IS_WIDGET (widget));
13724
13725   g_object_set (G_OBJECT (widget), "tooltip-text", text, NULL);
13726 }
13727
13728 /**
13729  * gtk_widget_get_tooltip_text:
13730  * @widget: a #GtkWidget
13731  *
13732  * Gets the contents of the tooltip for @widget.
13733  *
13734  * Return value: the tooltip text, or %NULL. You should free the
13735  *   returned string with g_free() when done.
13736  *
13737  * Since: 2.12
13738  */
13739 gchar *
13740 gtk_widget_get_tooltip_text (GtkWidget *widget)
13741 {
13742   gchar *text = NULL;
13743
13744   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13745
13746   g_object_get (G_OBJECT (widget), "tooltip-text", &text, NULL);
13747
13748   return text;
13749 }
13750
13751 /**
13752  * gtk_widget_set_tooltip_markup:
13753  * @widget: a #GtkWidget
13754  * @markup: (allow-none): the contents of the tooltip for @widget, or %NULL
13755  *
13756  * Sets @markup as the contents of the tooltip, which is marked up with
13757  *  the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
13758  *
13759  * This function will take care of setting #GtkWidget:has-tooltip to %TRUE
13760  * and of the default handler for the #GtkWidget::query-tooltip signal.
13761  *
13762  * See also the #GtkWidget:tooltip-markup property and
13763  * gtk_tooltip_set_markup().
13764  *
13765  * Since: 2.12
13766  */
13767 void
13768 gtk_widget_set_tooltip_markup (GtkWidget   *widget,
13769                                const gchar *markup)
13770 {
13771   g_return_if_fail (GTK_IS_WIDGET (widget));
13772
13773   g_object_set (G_OBJECT (widget), "tooltip-markup", markup, NULL);
13774 }
13775
13776 /**
13777  * gtk_widget_get_tooltip_markup:
13778  * @widget: a #GtkWidget
13779  *
13780  * Gets the contents of the tooltip for @widget.
13781  *
13782  * Return value: the tooltip text, or %NULL. You should free the
13783  *   returned string with g_free() when done.
13784  *
13785  * Since: 2.12
13786  */
13787 gchar *
13788 gtk_widget_get_tooltip_markup (GtkWidget *widget)
13789 {
13790   gchar *text = NULL;
13791
13792   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13793
13794   g_object_get (G_OBJECT (widget), "tooltip-markup", &text, NULL);
13795
13796   return text;
13797 }
13798
13799 /**
13800  * gtk_widget_set_has_tooltip:
13801  * @widget: a #GtkWidget
13802  * @has_tooltip: whether or not @widget has a tooltip.
13803  *
13804  * Sets the has-tooltip property on @widget to @has_tooltip.  See
13805  * #GtkWidget:has-tooltip for more information.
13806  *
13807  * Since: 2.12
13808  */
13809 void
13810 gtk_widget_set_has_tooltip (GtkWidget *widget,
13811                             gboolean   has_tooltip)
13812 {
13813   g_return_if_fail (GTK_IS_WIDGET (widget));
13814
13815   g_object_set (G_OBJECT (widget), "has-tooltip", has_tooltip, NULL);
13816 }
13817
13818 /**
13819  * gtk_widget_get_has_tooltip:
13820  * @widget: a #GtkWidget
13821  *
13822  * Returns the current value of the has-tooltip property.  See
13823  * #GtkWidget:has-tooltip for more information.
13824  *
13825  * Return value: current value of has-tooltip on @widget.
13826  *
13827  * Since: 2.12
13828  */
13829 gboolean
13830 gtk_widget_get_has_tooltip (GtkWidget *widget)
13831 {
13832   gboolean has_tooltip = FALSE;
13833
13834   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
13835
13836   g_object_get (G_OBJECT (widget), "has-tooltip", &has_tooltip, NULL);
13837
13838   return has_tooltip;
13839 }
13840
13841 /**
13842  * gtk_widget_get_allocation:
13843  * @widget: a #GtkWidget
13844  * @allocation: (out): a pointer to a #GtkAllocation to copy to
13845  *
13846  * Retrieves the widget's allocation.
13847  *
13848  * Note, when implementing a #GtkContainer: a widget's allocation will
13849  * be its "adjusted" allocation, that is, the widget's parent
13850  * container typically calls gtk_widget_size_allocate() with an
13851  * allocation, and that allocation is then adjusted (to handle margin
13852  * and alignment for example) before assignment to the widget.
13853  * gtk_widget_get_allocation() returns the adjusted allocation that
13854  * was actually assigned to the widget. The adjusted allocation is
13855  * guaranteed to be completely contained within the
13856  * gtk_widget_size_allocate() allocation, however. So a #GtkContainer
13857  * is guaranteed that its children stay inside the assigned bounds,
13858  * but not that they have exactly the bounds the container assigned.
13859  * There is no way to get the original allocation assigned by
13860  * gtk_widget_size_allocate(), since it isn't stored; if a container
13861  * implementation needs that information it will have to track it itself.
13862  *
13863  * Since: 2.18
13864  */
13865 void
13866 gtk_widget_get_allocation (GtkWidget     *widget,
13867                            GtkAllocation *allocation)
13868 {
13869   GtkWidgetPrivate *priv;
13870
13871   g_return_if_fail (GTK_IS_WIDGET (widget));
13872   g_return_if_fail (allocation != NULL);
13873
13874   priv = widget->priv;
13875
13876   *allocation = priv->allocation;
13877 }
13878
13879 /**
13880  * gtk_widget_set_allocation:
13881  * @widget: a #GtkWidget
13882  * @allocation: a pointer to a #GtkAllocation to copy from
13883  *
13884  * Sets the widget's allocation.  This should not be used
13885  * directly, but from within a widget's size_allocate method.
13886  *
13887  * The allocation set should be the "adjusted" or actual
13888  * allocation. If you're implementing a #GtkContainer, you want to use
13889  * gtk_widget_size_allocate() instead of gtk_widget_set_allocation().
13890  * The GtkWidgetClass::adjust_size_allocation virtual method adjusts the
13891  * allocation inside gtk_widget_size_allocate() to create an adjusted
13892  * allocation.
13893  *
13894  * Since: 2.18
13895  */
13896 void
13897 gtk_widget_set_allocation (GtkWidget           *widget,
13898                            const GtkAllocation *allocation)
13899 {
13900   GtkWidgetPrivate *priv;
13901
13902   g_return_if_fail (GTK_IS_WIDGET (widget));
13903   g_return_if_fail (allocation != NULL);
13904
13905   priv = widget->priv;
13906
13907   priv->allocation = *allocation;
13908 }
13909
13910 /**
13911  * gtk_widget_get_allocated_width:
13912  * @widget: the widget to query
13913  *
13914  * Returns the width that has currently been allocated to @widget.
13915  * This function is intended to be used when implementing handlers
13916  * for the #GtkWidget::draw function.
13917  *
13918  * Returns: the width of the @widget
13919  **/
13920 int
13921 gtk_widget_get_allocated_width (GtkWidget *widget)
13922 {
13923   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13924
13925   return widget->priv->allocation.width;
13926 }
13927
13928 /**
13929  * gtk_widget_get_allocated_height:
13930  * @widget: the widget to query
13931  *
13932  * Returns the height that has currently been allocated to @widget.
13933  * This function is intended to be used when implementing handlers
13934  * for the #GtkWidget::draw function.
13935  *
13936  * Returns: the height of the @widget
13937  **/
13938 int
13939 gtk_widget_get_allocated_height (GtkWidget *widget)
13940 {
13941   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13942
13943   return widget->priv->allocation.height;
13944 }
13945
13946 /**
13947  * gtk_widget_get_requisition:
13948  * @widget: a #GtkWidget
13949  * @requisition: (out): a pointer to a #GtkRequisition to copy to
13950  *
13951  * Retrieves the widget's requisition.
13952  *
13953  * This function should only be used by widget implementations in
13954  * order to figure whether the widget's requisition has actually
13955  * changed after some internal state change (so that they can call
13956  * gtk_widget_queue_resize() instead of gtk_widget_queue_draw()).
13957  *
13958  * Normally, gtk_widget_size_request() should be used.
13959  *
13960  * Since: 2.20
13961  *
13962  * Deprecated: 3.0: The #GtkRequisition cache on the widget was
13963  * removed, If you need to cache sizes across requests and allocations,
13964  * add an explicit cache to the widget in question instead.
13965  */
13966 void
13967 gtk_widget_get_requisition (GtkWidget      *widget,
13968                             GtkRequisition *requisition)
13969 {
13970   g_return_if_fail (GTK_IS_WIDGET (widget));
13971   g_return_if_fail (requisition != NULL);
13972
13973   gtk_widget_get_preferred_size (widget, requisition, NULL);
13974 }
13975
13976 /**
13977  * gtk_widget_set_window:
13978  * @widget: a #GtkWidget
13979  * @window: a #GdkWindow
13980  *
13981  * Sets a widget's window. This function should only be used in a
13982  * widget's #GtkWidget::realize implementation. The %window passed is
13983  * usually either new window created with gdk_window_new(), or the
13984  * window of its parent widget as returned by
13985  * gtk_widget_get_parent_window().
13986  *
13987  * Widgets must indicate whether they will create their own #GdkWindow
13988  * by calling gtk_widget_set_has_window(). This is usually done in the
13989  * widget's init() function.
13990  *
13991  * <note><para>This function does not add any reference to @window.</para></note>
13992  *
13993  * Since: 2.18
13994  */
13995 void
13996 gtk_widget_set_window (GtkWidget *widget,
13997                        GdkWindow *window)
13998 {
13999   GtkWidgetPrivate *priv;
14000
14001   g_return_if_fail (GTK_IS_WIDGET (widget));
14002   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
14003
14004   priv = widget->priv;
14005
14006   if (priv->window != window)
14007     {
14008       priv->window = window;
14009       g_object_notify (G_OBJECT (widget), "window");
14010     }
14011 }
14012
14013 /**
14014  * gtk_widget_get_window:
14015  * @widget: a #GtkWidget
14016  *
14017  * Returns the widget's window if it is realized, %NULL otherwise
14018  *
14019  * Return value: (transfer none): @widget's window.
14020  *
14021  * Since: 2.14
14022  */
14023 GdkWindow*
14024 gtk_widget_get_window (GtkWidget *widget)
14025 {
14026   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
14027
14028   return widget->priv->window;
14029 }
14030
14031 /**
14032  * gtk_widget_get_support_multidevice:
14033  * @widget: a #GtkWidget
14034  *
14035  * Returns %TRUE if @widget is multiple pointer aware. See
14036  * gtk_widget_set_support_multidevice() for more information.
14037  *
14038  * Returns: %TRUE if @widget is multidevice aware.
14039  **/
14040 gboolean
14041 gtk_widget_get_support_multidevice (GtkWidget *widget)
14042 {
14043   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
14044
14045   return widget->priv->multidevice;
14046 }
14047
14048 /**
14049  * gtk_widget_set_support_multidevice:
14050  * @widget: a #GtkWidget
14051  * @support_multidevice: %TRUE to support input from multiple devices.
14052  *
14053  * Enables or disables multiple pointer awareness. If this setting is %TRUE,
14054  * @widget will start receiving multiple, per device enter/leave events. Note
14055  * that if custom #GdkWindow<!-- -->s are created in #GtkWidget::realize,
14056  * gdk_window_set_support_multidevice() will have to be called manually on them.
14057  *
14058  * Since: 3.0
14059  **/
14060 void
14061 gtk_widget_set_support_multidevice (GtkWidget *widget,
14062                                     gboolean   support_multidevice)
14063 {
14064   GtkWidgetPrivate *priv;
14065
14066   g_return_if_fail (GTK_IS_WIDGET (widget));
14067
14068   priv = widget->priv;
14069   priv->multidevice = (support_multidevice == TRUE);
14070
14071   if (gtk_widget_get_realized (widget))
14072     gdk_window_set_support_multidevice (priv->window, support_multidevice);
14073 }
14074
14075 static void
14076 _gtk_widget_set_has_focus (GtkWidget *widget,
14077                            gboolean   has_focus)
14078 {
14079   widget->priv->has_focus = has_focus;
14080
14081   if (has_focus)
14082     gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_FOCUSED, FALSE);
14083   else
14084     gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_FOCUSED);
14085 }
14086
14087 /**
14088  * gtk_widget_send_focus_change:
14089  * @widget: a #GtkWidget
14090  * @event: a #GdkEvent of type GDK_FOCUS_CHANGE
14091  *
14092  * Sends the focus change @event to @widget
14093  *
14094  * This function is not meant to be used by applications. The only time it
14095  * should be used is when it is necessary for a #GtkWidget to assign focus
14096  * to a widget that is semantically owned by the first widget even though
14097  * it's not a direct child - for instance, a search entry in a floating
14098  * window similar to the quick search in #GtkTreeView.
14099  *
14100  * An example of its usage is:
14101  *
14102  * |[
14103  *   GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
14104  *
14105  *   fevent->focus_change.type = GDK_FOCUS_CHANGE;
14106  *   fevent->focus_change.in = TRUE;
14107  *   fevent->focus_change.window = gtk_widget_get_window (widget);
14108  *   if (fevent->focus_change.window != NULL)
14109  *     g_object_ref (fevent->focus_change.window);
14110  *
14111  *   gtk_widget_send_focus_change (widget, fevent);
14112  *
14113  *   gdk_event_free (event);
14114  * ]|
14115  *
14116  * Return value: the return value from the event signal emission: %TRUE
14117  *   if the event was handled, and %FALSE otherwise
14118  *
14119  * Since: 2.20
14120  */
14121 gboolean
14122 gtk_widget_send_focus_change (GtkWidget *widget,
14123                               GdkEvent  *event)
14124 {
14125   gboolean res;
14126
14127   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
14128   g_return_val_if_fail (event != NULL && event->type == GDK_FOCUS_CHANGE, FALSE);
14129
14130   g_object_ref (widget);
14131
14132   _gtk_widget_set_has_focus (widget, event->focus_change.in);
14133
14134   res = gtk_widget_event (widget, event);
14135
14136   g_object_notify (G_OBJECT (widget), "has-focus");
14137
14138   g_object_unref (widget);
14139
14140   return res;
14141 }
14142
14143 /**
14144  * gtk_widget_in_destruction:
14145  * @widget: a #GtkWidget
14146  *
14147  * Returns whether the widget is currently being destroyed.
14148  * This information can sometimes be used to avoid doing
14149  * unnecessary work.
14150  *
14151  * Returns: %TRUE if @widget is being destroyed
14152  */
14153 gboolean
14154 gtk_widget_in_destruction (GtkWidget *widget)
14155 {
14156   return widget->priv->in_destruction;
14157 }
14158
14159 gboolean
14160 _gtk_widget_get_resize_pending (GtkWidget *widget)
14161 {
14162   return widget->priv->resize_pending;
14163 }
14164
14165 void
14166 _gtk_widget_set_resize_pending (GtkWidget *widget,
14167                                 gboolean   resize_pending)
14168 {
14169   widget->priv->resize_pending = resize_pending;
14170 }
14171
14172 gboolean
14173 _gtk_widget_get_in_reparent (GtkWidget *widget)
14174 {
14175   return widget->priv->in_reparent;
14176 }
14177
14178 void
14179 _gtk_widget_set_in_reparent (GtkWidget *widget,
14180                              gboolean   in_reparent)
14181 {
14182   widget->priv->in_reparent = in_reparent;
14183 }
14184
14185 gboolean
14186 _gtk_widget_get_anchored (GtkWidget *widget)
14187 {
14188   return widget->priv->anchored;
14189 }
14190
14191 void
14192 _gtk_widget_set_anchored (GtkWidget *widget,
14193                           gboolean   anchored)
14194 {
14195   widget->priv->anchored = anchored;
14196 }
14197
14198 gboolean
14199 _gtk_widget_get_shadowed (GtkWidget *widget)
14200 {
14201   return widget->priv->shadowed;
14202 }
14203
14204 void
14205 _gtk_widget_set_shadowed (GtkWidget *widget,
14206                           gboolean   shadowed)
14207 {
14208   widget->priv->shadowed = shadowed;
14209 }
14210
14211 gboolean
14212 _gtk_widget_get_alloc_needed (GtkWidget *widget)
14213 {
14214   return widget->priv->alloc_needed;
14215 }
14216
14217 void
14218 _gtk_widget_set_alloc_needed (GtkWidget *widget,
14219                               gboolean   alloc_needed)
14220 {
14221   widget->priv->alloc_needed = alloc_needed;
14222 }
14223
14224 gboolean
14225 _gtk_widget_get_width_request_needed (GtkWidget *widget)
14226 {
14227   return widget->priv->width_request_needed;
14228 }
14229
14230 void
14231 _gtk_widget_set_width_request_needed (GtkWidget *widget,
14232                                       gboolean   width_request_needed)
14233 {
14234   widget->priv->width_request_needed = width_request_needed;
14235 }
14236
14237 gboolean
14238 _gtk_widget_get_height_request_needed (GtkWidget *widget)
14239 {
14240   return widget->priv->height_request_needed;
14241 }
14242
14243 void
14244 _gtk_widget_set_height_request_needed (GtkWidget *widget,
14245                                        gboolean   height_request_needed)
14246 {
14247   widget->priv->height_request_needed = height_request_needed;
14248 }
14249
14250 gboolean
14251 _gtk_widget_get_sizegroup_visited (GtkWidget    *widget)
14252 {
14253   return widget->priv->sizegroup_visited;
14254 }
14255
14256 void
14257 _gtk_widget_set_sizegroup_visited (GtkWidget    *widget,
14258                                    gboolean      visited)
14259 {
14260   widget->priv->sizegroup_visited = visited;
14261 }
14262
14263 gboolean
14264 _gtk_widget_get_sizegroup_bumping (GtkWidget    *widget)
14265 {
14266   return widget->priv->sizegroup_bumping;
14267 }
14268
14269 void
14270 _gtk_widget_set_sizegroup_bumping (GtkWidget    *widget,
14271                                    gboolean      bumping)
14272 {
14273   widget->priv->sizegroup_bumping = bumping;
14274 }
14275
14276 void
14277 _gtk_widget_add_sizegroup (GtkWidget    *widget,
14278                            gpointer      group)
14279 {
14280   GSList *groups;
14281
14282   groups = g_object_get_qdata (G_OBJECT (widget), quark_size_groups);
14283   groups = g_slist_prepend (groups, group);
14284   g_object_set_qdata (G_OBJECT (widget), quark_size_groups, groups);
14285
14286   widget->priv->have_size_groups = TRUE;
14287 }
14288
14289 void
14290 _gtk_widget_remove_sizegroup (GtkWidget    *widget,
14291                               gpointer      group)
14292 {
14293   GSList *groups;
14294
14295   groups = g_object_get_qdata (G_OBJECT (widget), quark_size_groups);
14296   groups = g_slist_remove (groups, group);
14297   g_object_set_qdata (G_OBJECT (widget), quark_size_groups, groups);
14298
14299   widget->priv->have_size_groups = groups != NULL;
14300 }
14301
14302 GSList *
14303 _gtk_widget_get_sizegroups (GtkWidget    *widget)
14304 {
14305   if (widget->priv->have_size_groups)
14306     return g_object_get_qdata (G_OBJECT (widget), quark_size_groups);
14307
14308   return NULL;
14309 }
14310
14311 /**
14312  * gtk_widget_path_append_for_widget:
14313  * @path: a widget path
14314  * @widget: the widget to append to the widget path
14315  *
14316  * Appends the data from @widget to the widget hierarchy represented
14317  * by @path. This function is a shortcut for adding information from
14318  * @widget to the given @path. This includes setting the name or
14319  * adding the style classes from @widget.
14320  *
14321  * Returns: the position where the data was inserted
14322  *
14323  * Since: 3.2
14324  **/
14325 gint
14326 gtk_widget_path_append_for_widget (GtkWidgetPath *path,
14327                                    GtkWidget     *widget)
14328 {
14329   gint pos;
14330
14331   g_return_val_if_fail (path != NULL, 0);
14332   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
14333
14334   pos = gtk_widget_path_append_type (path, G_OBJECT_TYPE (widget));
14335
14336   if (widget->priv->name)
14337     gtk_widget_path_iter_set_name (path, pos, widget->priv->name);
14338
14339   if (widget->priv->context)
14340     {
14341       GList *classes, *l;
14342
14343       /* Also add any persistent classes in
14344        * the style context the widget path
14345        */
14346       classes = gtk_style_context_list_classes (widget->priv->context);
14347
14348       for (l = classes; l; l = l->next)
14349         gtk_widget_path_iter_add_class (path, pos, l->data);
14350
14351       g_list_free (classes);
14352     }
14353
14354   return pos;
14355 }
14356
14357 /**
14358  * gtk_widget_get_path:
14359  * @widget: a #GtkWidget
14360  *
14361  * Returns the #GtkWidgetPath representing @widget, if the widget
14362  * is not connected to a toplevel widget, a partial path will be
14363  * created.
14364  *
14365  * Returns: (transfer none): The #GtkWidgetPath representing @widget
14366  **/
14367 GtkWidgetPath *
14368 gtk_widget_get_path (GtkWidget *widget)
14369 {
14370   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
14371
14372   /* As strange as it may seem, this may happen on object construction.
14373    * init() implementations of parent types may eventually call this function,
14374    * each with its corresponding GType, which could leave a child
14375    * implementation with a wrong widget type in the widget path
14376    */
14377   if (widget->priv->path &&
14378       G_OBJECT_TYPE (widget) != gtk_widget_path_get_object_type (widget->priv->path))
14379     {
14380       gtk_widget_path_free (widget->priv->path);
14381       widget->priv->path = NULL;
14382     }
14383
14384   if (!widget->priv->path)
14385     {
14386       GtkWidget *parent;
14387
14388       parent = widget->priv->parent;
14389
14390       if (parent)
14391         widget->priv->path = gtk_container_get_path_for_child (GTK_CONTAINER (parent), widget);
14392       else
14393         {
14394           /* Widget is either toplevel or unparented, treat both
14395            * as toplevels style wise, since there are situations
14396            * where style properties might be retrieved on that
14397            * situation.
14398            */
14399           widget->priv->path = gtk_widget_path_new ();
14400     
14401           gtk_widget_path_append_for_widget (widget->priv->path, widget);
14402         }
14403
14404       if (widget->priv->context)
14405         gtk_style_context_set_path (widget->priv->context,
14406                                     widget->priv->path);
14407     }
14408
14409   return widget->priv->path;
14410 }
14411
14412 static void
14413 style_context_changed (GtkStyleContext *context,
14414                        gpointer         user_data)
14415 {
14416   GtkWidget *widget = user_data;
14417
14418   if (gtk_widget_get_realized (widget))
14419     g_signal_emit (widget, widget_signals[STYLE_UPDATED], 0);
14420   else
14421     {
14422       /* Compress all style updates so it
14423        * is only emitted once pre-realize.
14424        */
14425       widget->priv->style_update_pending = TRUE;
14426     }
14427
14428   if (widget->priv->anchored)
14429     gtk_widget_queue_resize (widget);
14430 }
14431
14432 /**
14433  * gtk_widget_get_style_context:
14434  * @widget: a #GtkWidget
14435  *
14436  * Returns the style context associated to @widget.
14437  *
14438  * Returns: (transfer none): a #GtkStyleContext. This memory is owned by @widget and
14439  *          must not be freed.
14440  **/
14441 GtkStyleContext *
14442 gtk_widget_get_style_context (GtkWidget *widget)
14443 {
14444   GtkWidgetPrivate *priv;
14445   GtkWidgetPath *path;
14446
14447   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
14448
14449   priv = widget->priv;
14450   
14451   /* updates style context if it exists already */
14452   path = gtk_widget_get_path (widget);
14453
14454   if (G_UNLIKELY (priv->context == NULL))
14455     {
14456       GdkScreen *screen;
14457
14458       priv->context = g_object_new (GTK_TYPE_STYLE_CONTEXT,
14459                                     "direction", gtk_widget_get_direction (widget),
14460                                     NULL);
14461
14462       g_signal_connect (widget->priv->context, "changed",
14463                         G_CALLBACK (style_context_changed), widget);
14464
14465       screen = gtk_widget_get_screen (widget);
14466
14467       if (screen)
14468         gtk_style_context_set_screen (priv->context, screen);
14469
14470       gtk_style_context_set_path (priv->context, path);
14471     }
14472
14473   return widget->priv->context;
14474 }