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