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