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