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