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