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