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