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