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