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