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