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