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