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