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