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