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