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