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