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