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