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