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