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