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