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