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