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