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