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