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