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