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