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