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