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