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