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