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