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