]> Pileus Git - ~andy/gtk/blob - gtk/gtkwidget.c
docs: Make clear that gtk_widget_set_style() does not do anything
[~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                               const 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   gtk_widget_reset_style (widget);
6924
6925   g_object_notify (G_OBJECT (widget), "name");
6926 }
6927
6928 /**
6929  * gtk_widget_get_name:
6930  * @widget: a #GtkWidget
6931  *
6932  * Retrieves the name of a widget. See gtk_widget_set_name() for the
6933  * significance of widget names.
6934  *
6935  * Return value: name of the widget. This string is owned by GTK+ and
6936  * should not be modified or freed
6937  **/
6938 G_CONST_RETURN gchar*
6939 gtk_widget_get_name (GtkWidget *widget)
6940 {
6941   GtkWidgetPrivate *priv;
6942
6943   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6944
6945   priv = widget->priv;
6946
6947   if (priv->name)
6948     return priv->name;
6949   return G_OBJECT_TYPE_NAME (widget);
6950 }
6951
6952 static void
6953 _gtk_widget_update_state_flags (GtkWidget     *widget,
6954                                 GtkStateFlags  flags,
6955                                 guint          operation)
6956 {
6957   GtkWidgetPrivate *priv;
6958
6959   priv = widget->priv;
6960
6961   /* Handle insensitive first, since it is propagated
6962    * differently throughout the widget hierarchy.
6963    */
6964   if ((priv->state_flags & GTK_STATE_FLAG_INSENSITIVE) && (flags & GTK_STATE_FLAG_INSENSITIVE) && (operation == STATE_CHANGE_UNSET))
6965     gtk_widget_set_sensitive (widget, TRUE);
6966   else if (!(priv->state_flags & GTK_STATE_FLAG_INSENSITIVE) && (flags & GTK_STATE_FLAG_INSENSITIVE) && (operation != STATE_CHANGE_UNSET))
6967     gtk_widget_set_sensitive (widget, FALSE);
6968   else if ((priv->state_flags & GTK_STATE_FLAG_INSENSITIVE) && !(flags & GTK_STATE_FLAG_INSENSITIVE) && (operation == STATE_CHANGE_REPLACE))
6969     gtk_widget_set_sensitive (widget, TRUE);
6970
6971   if (operation != STATE_CHANGE_REPLACE)
6972     flags &= ~(GTK_STATE_FLAG_INSENSITIVE);
6973
6974   if (flags != 0 ||
6975       operation == STATE_CHANGE_REPLACE)
6976     {
6977       GtkStateData data;
6978
6979       data.flags = flags;
6980       data.operation = operation;
6981       data.use_forall = FALSE;
6982
6983       if (priv->parent)
6984         data.parent_sensitive = (gtk_widget_is_sensitive (priv->parent) != FALSE);
6985       else
6986         data.parent_sensitive = TRUE;
6987
6988       gtk_widget_propagate_state (widget, &data);
6989
6990       gtk_widget_queue_resize (widget);
6991     }
6992 }
6993
6994 /**
6995  * gtk_widget_set_state_flags:
6996  * @widget: a #GtkWidget
6997  * @flags: State flags to turn on
6998  * @clear: Whether to clear state before turning on @flags
6999  *
7000  * This function is for use in widget implementations. Turns on flag
7001  * values in the current widget state (insensitive, prelighted, etc.).
7002  *
7003  * It is worth mentioning that any other state than %GTK_STATE_FLAG_INSENSITIVE,
7004  * will be propagated down to all non-internal children if @widget is a
7005  * #GtkContainer, while %GTK_STATE_FLAG_INSENSITIVE itself will be propagated
7006  * down to all #GtkContainer children by different means than turning on the
7007  * state flag down the hierarchy, both gtk_widget_get_state_flags() and
7008  * gtk_widget_is_sensitive() will make use of these.
7009  *
7010  * Since: 3.0
7011  **/
7012 void
7013 gtk_widget_set_state_flags (GtkWidget     *widget,
7014                             GtkStateFlags  flags,
7015                             gboolean       clear)
7016 {
7017   g_return_if_fail (GTK_IS_WIDGET (widget));
7018
7019   if ((!clear && (widget->priv->state_flags & flags) == flags) ||
7020       (clear && widget->priv->state_flags == flags))
7021     return;
7022
7023   if (clear)
7024     _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_REPLACE);
7025   else
7026     _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_SET);
7027 }
7028
7029 /**
7030  * gtk_widget_unset_state_flags:
7031  * @widget: a #GtkWidget
7032  * @flags: State flags to turn off
7033  *
7034  * This function is for use in widget implementations. Turns off flag
7035  * values for the current widget state (insensitive, prelighted, etc.).
7036  * See gtk_widget_set_state_flags().
7037  *
7038  * Since: 3.0
7039  **/
7040 void
7041 gtk_widget_unset_state_flags (GtkWidget     *widget,
7042                               GtkStateFlags  flags)
7043 {
7044   g_return_if_fail (GTK_IS_WIDGET (widget));
7045
7046   if ((widget->priv->state_flags & flags) == 0)
7047     return;
7048
7049   _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_UNSET);
7050 }
7051
7052 /**
7053  * gtk_widget_get_state_flags:
7054  * @widget: a #GtkWidget
7055  *
7056  * Returns the widget state as a flag set. It is worth mentioning
7057  * that the effective %GTK_STATE_FLAG_INSENSITIVE state will be
7058  * returned, that is, also based on parent insensitivity, even if
7059  * @widget itself is sensitive.
7060  *
7061  * Returns: The state flags for widget
7062  *
7063  * Since: 3.0
7064  **/
7065 GtkStateFlags
7066 gtk_widget_get_state_flags (GtkWidget *widget)
7067 {
7068   GtkStateFlags flags;
7069
7070   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
7071
7072   flags = widget->priv->state_flags;
7073
7074   if (!gtk_widget_is_sensitive (widget))
7075     flags |= GTK_STATE_FLAG_INSENSITIVE;
7076
7077   if (gtk_widget_has_focus (widget))
7078     flags |= GTK_STATE_FLAG_FOCUSED;
7079
7080   return flags;
7081 }
7082
7083 /**
7084  * gtk_widget_set_state:
7085  * @widget: a #GtkWidget
7086  * @state: new state for @widget
7087  *
7088  * This function is for use in widget implementations. Sets the state
7089  * of a widget (insensitive, prelighted, etc.) Usually you should set
7090  * the state using wrapper functions such as gtk_widget_set_sensitive().
7091  *
7092  * Deprecated: 3.0. Use gtk_widget_set_state_flags() instead.
7093  **/
7094 void
7095 gtk_widget_set_state (GtkWidget           *widget,
7096                       GtkStateType         state)
7097 {
7098   GtkStateFlags flags;
7099
7100   if (state == gtk_widget_get_state (widget))
7101     return;
7102
7103   switch (state)
7104     {
7105     case GTK_STATE_ACTIVE:
7106       flags = GTK_STATE_FLAG_ACTIVE;
7107       break;
7108     case GTK_STATE_PRELIGHT:
7109       flags = GTK_STATE_FLAG_PRELIGHT;
7110       break;
7111     case GTK_STATE_SELECTED:
7112       flags = GTK_STATE_FLAG_SELECTED;
7113       break;
7114     case GTK_STATE_INSENSITIVE:
7115       flags = GTK_STATE_FLAG_INSENSITIVE;
7116       break;
7117     case GTK_STATE_INCONSISTENT:
7118       flags = GTK_STATE_FLAG_INCONSISTENT;
7119       break;
7120     case GTK_STATE_FOCUSED:
7121       flags = GTK_STATE_FLAG_FOCUSED;
7122       break;
7123     case GTK_STATE_NORMAL:
7124     default:
7125       flags = 0;
7126       break;
7127     }
7128
7129   _gtk_widget_update_state_flags (widget, flags, STATE_CHANGE_REPLACE);
7130 }
7131
7132 /**
7133  * gtk_widget_get_state:
7134  * @widget: a #GtkWidget
7135  *
7136  * Returns the widget's state. See gtk_widget_set_state().
7137  *
7138  * Returns: the state of @widget.
7139  *
7140  * Since: 2.18
7141  *
7142  * Deprecated: 3.0. Use gtk_widget_get_state_flags() instead.
7143  */
7144 GtkStateType
7145 gtk_widget_get_state (GtkWidget *widget)
7146 {
7147   GtkStateFlags flags;
7148
7149   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_STATE_NORMAL);
7150
7151   flags = gtk_widget_get_state_flags (widget);
7152
7153   if (flags & GTK_STATE_FLAG_INSENSITIVE)
7154     return GTK_STATE_INSENSITIVE;
7155   else if (flags & GTK_STATE_FLAG_ACTIVE)
7156     return GTK_STATE_ACTIVE;
7157   else if (flags & GTK_STATE_FLAG_SELECTED)
7158     return GTK_STATE_SELECTED;
7159   else if (flags & GTK_STATE_FLAG_PRELIGHT)
7160     return GTK_STATE_PRELIGHT;
7161   else
7162     return GTK_STATE_NORMAL;
7163 }
7164
7165 /**
7166  * gtk_widget_set_visible:
7167  * @widget: a #GtkWidget
7168  * @visible: whether the widget should be shown or not
7169  *
7170  * Sets the visibility state of @widget. Note that setting this to
7171  * %TRUE doesn't mean the widget is actually viewable, see
7172  * gtk_widget_get_visible().
7173  *
7174  * This function simply calls gtk_widget_show() or gtk_widget_hide()
7175  * but is nicer to use when the visibility of the widget depends on
7176  * some condition.
7177  *
7178  * Since: 2.18
7179  **/
7180 void
7181 gtk_widget_set_visible (GtkWidget *widget,
7182                         gboolean   visible)
7183 {
7184   g_return_if_fail (GTK_IS_WIDGET (widget));
7185
7186   if (visible != gtk_widget_get_visible (widget))
7187     {
7188       if (visible)
7189         gtk_widget_show (widget);
7190       else
7191         gtk_widget_hide (widget);
7192     }
7193 }
7194
7195 void
7196 _gtk_widget_set_visible_flag (GtkWidget *widget,
7197                               gboolean   visible)
7198 {
7199   widget->priv->visible = visible;
7200 }
7201
7202 /**
7203  * gtk_widget_get_visible:
7204  * @widget: a #GtkWidget
7205  *
7206  * Determines whether the widget is visible. Note that this doesn't
7207  * take into account whether the widget's parent is also visible
7208  * or the widget is obscured in any way.
7209  *
7210  * See gtk_widget_set_visible().
7211  *
7212  * Return value: %TRUE if the widget is visible
7213  *
7214  * Since: 2.18
7215  **/
7216 gboolean
7217 gtk_widget_get_visible (GtkWidget *widget)
7218 {
7219   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7220
7221   return widget->priv->visible;
7222 }
7223
7224 /**
7225  * gtk_widget_set_has_window:
7226  * @widget: a #GtkWidget
7227  * @has_window: whether or not @widget has a window.
7228  *
7229  * Specifies whether @widget has a #GdkWindow of its own. Note that
7230  * all realized widgets have a non-%NULL "window" pointer
7231  * (gtk_widget_get_window() never returns a %NULL window when a widget
7232  * is realized), but for many of them it's actually the #GdkWindow of
7233  * one of its parent widgets. Widgets that do not create a %window for
7234  * themselves in #GtkWidget::realize must announce this by
7235  * calling this function with @has_window = %FALSE.
7236  *
7237  * This function should only be called by widget implementations,
7238  * and they should call it in their init() function.
7239  *
7240  * Since: 2.18
7241  **/
7242 void
7243 gtk_widget_set_has_window (GtkWidget *widget,
7244                            gboolean   has_window)
7245 {
7246   g_return_if_fail (GTK_IS_WIDGET (widget));
7247
7248   widget->priv->no_window = !has_window;
7249 }
7250
7251 /**
7252  * gtk_widget_get_has_window:
7253  * @widget: a #GtkWidget
7254  *
7255  * Determines whether @widget has a #GdkWindow of its own. See
7256  * gtk_widget_set_has_window().
7257  *
7258  * Return value: %TRUE if @widget has a window, %FALSE otherwise
7259  *
7260  * Since: 2.18
7261  **/
7262 gboolean
7263 gtk_widget_get_has_window (GtkWidget *widget)
7264 {
7265   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7266
7267   return ! widget->priv->no_window;
7268 }
7269
7270 /**
7271  * gtk_widget_is_toplevel:
7272  * @widget: a #GtkWidget
7273  *
7274  * Determines whether @widget is a toplevel widget.
7275  *
7276  * Currently only #GtkWindow and #GtkInvisible (and out-of-process
7277  * #GtkPlugs) are toplevel widgets. Toplevel widgets have no parent
7278  * widget.
7279  *
7280  * Return value: %TRUE if @widget is a toplevel, %FALSE otherwise
7281  *
7282  * Since: 2.18
7283  **/
7284 gboolean
7285 gtk_widget_is_toplevel (GtkWidget *widget)
7286 {
7287   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7288
7289   return widget->priv->toplevel;
7290 }
7291
7292 void
7293 _gtk_widget_set_is_toplevel (GtkWidget *widget,
7294                              gboolean   is_toplevel)
7295 {
7296   widget->priv->toplevel = is_toplevel;
7297 }
7298
7299 /**
7300  * gtk_widget_is_drawable:
7301  * @widget: a #GtkWidget
7302  *
7303  * Determines whether @widget can be drawn to. A widget can be drawn
7304  * to if it is mapped and visible.
7305  *
7306  * Return value: %TRUE if @widget is drawable, %FALSE otherwise
7307  *
7308  * Since: 2.18
7309  **/
7310 gboolean
7311 gtk_widget_is_drawable (GtkWidget *widget)
7312 {
7313   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7314
7315   return (gtk_widget_get_visible (widget) &&
7316           gtk_widget_get_mapped (widget));
7317 }
7318
7319 /**
7320  * gtk_widget_get_realized:
7321  * @widget: a #GtkWidget
7322  *
7323  * Determines whether @widget is realized.
7324  *
7325  * Return value: %TRUE if @widget is realized, %FALSE otherwise
7326  *
7327  * Since: 2.20
7328  **/
7329 gboolean
7330 gtk_widget_get_realized (GtkWidget *widget)
7331 {
7332   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7333
7334   return widget->priv->realized;
7335 }
7336
7337 /**
7338  * gtk_widget_set_realized:
7339  * @widget: a #GtkWidget
7340  * @realized: %TRUE to mark the widget as realized
7341  *
7342  * Marks the widget as being realized.
7343  *
7344  * This function should only ever be called in a derived widget's
7345  * "realize" or "unrealize" implementation.
7346  *
7347  * Since: 2.20
7348  */
7349 void
7350 gtk_widget_set_realized (GtkWidget *widget,
7351                          gboolean   realized)
7352 {
7353   g_return_if_fail (GTK_IS_WIDGET (widget));
7354
7355   widget->priv->realized = realized;
7356 }
7357
7358 /**
7359  * gtk_widget_get_mapped:
7360  * @widget: a #GtkWidget
7361  *
7362  * Whether the widget is mapped.
7363  *
7364  * Return value: %TRUE if the widget is mapped, %FALSE otherwise.
7365  *
7366  * Since: 2.20
7367  */
7368 gboolean
7369 gtk_widget_get_mapped (GtkWidget *widget)
7370 {
7371   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7372
7373   return widget->priv->mapped;
7374 }
7375
7376 /**
7377  * gtk_widget_set_mapped:
7378  * @widget: a #GtkWidget
7379  * @mapped: %TRUE to mark the widget as mapped
7380  *
7381  * Marks the widget as being realized.
7382  *
7383  * This function should only ever be called in a derived widget's
7384  * "map" or "unmap" implementation.
7385  *
7386  * Since: 2.20
7387  */
7388 void
7389 gtk_widget_set_mapped (GtkWidget *widget,
7390                        gboolean   mapped)
7391 {
7392   g_return_if_fail (GTK_IS_WIDGET (widget));
7393
7394   widget->priv->mapped = mapped;
7395 }
7396
7397 /**
7398  * gtk_widget_set_app_paintable:
7399  * @widget: a #GtkWidget
7400  * @app_paintable: %TRUE if the application will paint on the widget
7401  *
7402  * Sets whether the application intends to draw on the widget in
7403  * an #GtkWidget::draw handler.
7404  *
7405  * This is a hint to the widget and does not affect the behavior of
7406  * the GTK+ core; many widgets ignore this flag entirely. For widgets
7407  * that do pay attention to the flag, such as #GtkEventBox and #GtkWindow,
7408  * the effect is to suppress default themed drawing of the widget's
7409  * background. (Children of the widget will still be drawn.) The application
7410  * is then entirely responsible for drawing the widget background.
7411  *
7412  * Note that the background is still drawn when the widget is mapped.
7413  **/
7414 void
7415 gtk_widget_set_app_paintable (GtkWidget *widget,
7416                               gboolean   app_paintable)
7417 {
7418   g_return_if_fail (GTK_IS_WIDGET (widget));
7419
7420   app_paintable = (app_paintable != FALSE);
7421
7422   if (widget->priv->app_paintable != app_paintable)
7423     {
7424       widget->priv->app_paintable = app_paintable;
7425
7426       if (gtk_widget_is_drawable (widget))
7427         gtk_widget_queue_draw (widget);
7428
7429       g_object_notify (G_OBJECT (widget), "app-paintable");
7430     }
7431 }
7432
7433 /**
7434  * gtk_widget_get_app_paintable:
7435  * @widget: a #GtkWidget
7436  *
7437  * Determines whether the application intends to draw on the widget in
7438  * an #GtkWidget::draw handler.
7439  *
7440  * See gtk_widget_set_app_paintable()
7441  *
7442  * Return value: %TRUE if the widget is app paintable
7443  *
7444  * Since: 2.18
7445  **/
7446 gboolean
7447 gtk_widget_get_app_paintable (GtkWidget *widget)
7448 {
7449   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7450
7451   return widget->priv->app_paintable;
7452 }
7453
7454 /**
7455  * gtk_widget_set_double_buffered:
7456  * @widget: a #GtkWidget
7457  * @double_buffered: %TRUE to double-buffer a widget
7458  *
7459  * Widgets are double buffered by default; you can use this function
7460  * to turn off the buffering. "Double buffered" simply means that
7461  * gdk_window_begin_paint_region() and gdk_window_end_paint() are called
7462  * automatically around expose events sent to the
7463  * widget. gdk_window_begin_paint() diverts all drawing to a widget's
7464  * window to an offscreen buffer, and gdk_window_end_paint() draws the
7465  * buffer to the screen. The result is that users see the window
7466  * update in one smooth step, and don't see individual graphics
7467  * primitives being rendered.
7468  *
7469  * In very simple terms, double buffered widgets don't flicker,
7470  * so you would only use this function to turn off double buffering
7471  * if you had special needs and really knew what you were doing.
7472  *
7473  * Note: if you turn off double-buffering, you have to handle
7474  * expose events, since even the clearing to the background color or
7475  * pixmap will not happen automatically (as it is done in
7476  * gdk_window_begin_paint()).
7477  **/
7478 void
7479 gtk_widget_set_double_buffered (GtkWidget *widget,
7480                                 gboolean   double_buffered)
7481 {
7482   g_return_if_fail (GTK_IS_WIDGET (widget));
7483
7484   double_buffered = (double_buffered != FALSE);
7485
7486   if (widget->priv->double_buffered != double_buffered)
7487     {
7488       widget->priv->double_buffered = double_buffered;
7489
7490       g_object_notify (G_OBJECT (widget), "double-buffered");
7491     }
7492 }
7493
7494 /**
7495  * gtk_widget_get_double_buffered:
7496  * @widget: a #GtkWidget
7497  *
7498  * Determines whether the widget is double buffered.
7499  *
7500  * See gtk_widget_set_double_buffered()
7501  *
7502  * Return value: %TRUE if the widget is double buffered
7503  *
7504  * Since: 2.18
7505  **/
7506 gboolean
7507 gtk_widget_get_double_buffered (GtkWidget *widget)
7508 {
7509   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7510
7511   return widget->priv->double_buffered;
7512 }
7513
7514 /**
7515  * gtk_widget_set_redraw_on_allocate:
7516  * @widget: a #GtkWidget
7517  * @redraw_on_allocate: if %TRUE, the entire widget will be redrawn
7518  *   when it is allocated to a new size. Otherwise, only the
7519  *   new portion of the widget will be redrawn.
7520  *
7521  * Sets whether the entire widget is queued for drawing when its size
7522  * allocation changes. By default, this setting is %TRUE and
7523  * the entire widget is redrawn on every size change. If your widget
7524  * leaves the upper left unchanged when made bigger, turning this
7525  * setting off will improve performance.
7526
7527  * Note that for %NO_WINDOW widgets setting this flag to %FALSE turns
7528  * off all allocation on resizing: the widget will not even redraw if
7529  * its position changes; this is to allow containers that don't draw
7530  * anything to avoid excess invalidations. If you set this flag on a
7531  * %NO_WINDOW widget that <emphasis>does</emphasis> draw on @widget->window,
7532  * you are responsible for invalidating both the old and new allocation
7533  * of the widget when the widget is moved and responsible for invalidating
7534  * regions newly when the widget increases size.
7535  **/
7536 void
7537 gtk_widget_set_redraw_on_allocate (GtkWidget *widget,
7538                                    gboolean   redraw_on_allocate)
7539 {
7540   g_return_if_fail (GTK_IS_WIDGET (widget));
7541
7542   widget->priv->redraw_on_alloc = redraw_on_allocate;
7543 }
7544
7545 /**
7546  * gtk_widget_set_sensitive:
7547  * @widget: a #GtkWidget
7548  * @sensitive: %TRUE to make the widget sensitive
7549  *
7550  * Sets the sensitivity of a widget. A widget is sensitive if the user
7551  * can interact with it. Insensitive widgets are "grayed out" and the
7552  * user can't interact with them. Insensitive widgets are known as
7553  * "inactive", "disabled", or "ghosted" in some other toolkits.
7554  **/
7555 void
7556 gtk_widget_set_sensitive (GtkWidget *widget,
7557                           gboolean   sensitive)
7558 {
7559   GtkWidgetPrivate *priv;
7560   GtkStateData data;
7561
7562   g_return_if_fail (GTK_IS_WIDGET (widget));
7563
7564   priv = widget->priv;
7565
7566   sensitive = (sensitive != FALSE);
7567
7568   if (widget->priv->sensitive == sensitive)
7569     return;
7570
7571   data.flags = GTK_STATE_FLAG_INSENSITIVE;
7572
7573   if (sensitive)
7574     {
7575       widget->priv->sensitive = TRUE;
7576       data.operation = STATE_CHANGE_UNSET;
7577     }
7578   else
7579     {
7580       widget->priv->sensitive = FALSE;
7581       data.operation = STATE_CHANGE_SET;
7582     }
7583
7584   data.use_forall = TRUE;
7585
7586   if (priv->parent)
7587     data.parent_sensitive = gtk_widget_is_sensitive (priv->parent);
7588   else
7589     data.parent_sensitive = TRUE;
7590
7591   gtk_widget_propagate_state (widget, &data);
7592
7593   gtk_widget_queue_resize (widget);
7594
7595   g_object_notify (G_OBJECT (widget), "sensitive");
7596 }
7597
7598 /**
7599  * gtk_widget_get_sensitive:
7600  * @widget: a #GtkWidget
7601  *
7602  * Returns the widget's sensitivity (in the sense of returning
7603  * the value that has been set using gtk_widget_set_sensitive()).
7604  *
7605  * The effective sensitivity of a widget is however determined by both its
7606  * own and its parent widget's sensitivity. See gtk_widget_is_sensitive().
7607  *
7608  * Returns: %TRUE if the widget is sensitive
7609  *
7610  * Since: 2.18
7611  */
7612 gboolean
7613 gtk_widget_get_sensitive (GtkWidget *widget)
7614 {
7615   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7616
7617   return widget->priv->sensitive;
7618 }
7619
7620 /**
7621  * gtk_widget_is_sensitive:
7622  * @widget: a #GtkWidget
7623  *
7624  * Returns the widget's effective sensitivity, which means
7625  * it is sensitive itself and also its parent widget is sensitive
7626  *
7627  * Returns: %TRUE if the widget is effectively sensitive
7628  *
7629  * Since: 2.18
7630  */
7631 gboolean
7632 gtk_widget_is_sensitive (GtkWidget *widget)
7633 {
7634   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7635
7636   return widget->priv->sensitive && widget->priv->parent_sensitive;
7637 }
7638
7639 static void
7640 _gtk_widget_update_path (GtkWidget *widget)
7641 {
7642   if (widget->priv->path)
7643     {
7644       gtk_widget_path_free (widget->priv->path);
7645       widget->priv->path = NULL;
7646     }
7647
7648   gtk_widget_get_path (widget);
7649 }
7650
7651 /**
7652  * gtk_widget_set_parent:
7653  * @widget: a #GtkWidget
7654  * @parent: parent container
7655  *
7656  * This function is useful only when implementing subclasses of
7657  * #GtkContainer.
7658  * Sets the container as the parent of @widget, and takes care of
7659  * some details such as updating the state and style of the child
7660  * to reflect its new location. The opposite function is
7661  * gtk_widget_unparent().
7662  **/
7663 void
7664 gtk_widget_set_parent (GtkWidget *widget,
7665                        GtkWidget *parent)
7666 {
7667   GtkStateFlags parent_flags;
7668   GtkWidgetPrivate *priv;
7669   GtkStateData data;
7670
7671   g_return_if_fail (GTK_IS_WIDGET (widget));
7672   g_return_if_fail (GTK_IS_WIDGET (parent));
7673   g_return_if_fail (widget != parent);
7674
7675   priv = widget->priv;
7676
7677   if (priv->parent != NULL)
7678     {
7679       g_warning ("Can't set a parent on widget which has a parent\n");
7680       return;
7681     }
7682   if (gtk_widget_is_toplevel (widget))
7683     {
7684       g_warning ("Can't set a parent on a toplevel widget\n");
7685       return;
7686     }
7687
7688   /* keep this function in sync with gtk_menu_attach_to_widget()
7689    */
7690
7691   g_object_ref_sink (widget);
7692
7693   gtk_widget_push_verify_invariants (widget);
7694
7695   priv->parent = parent;
7696
7697   parent_flags = gtk_widget_get_state_flags (parent);
7698
7699   /* Merge both old state and current parent state,
7700    * We don't want the insensitive flag to propagate
7701    * to the new child though */
7702   data.flags = parent_flags & ~GTK_STATE_FLAG_INSENSITIVE;
7703   data.flags |= priv->state_flags;
7704
7705   data.operation = STATE_CHANGE_REPLACE;
7706   data.parent_sensitive = (gtk_widget_is_sensitive (parent) != FALSE);
7707   data.use_forall = gtk_widget_is_sensitive (parent) != gtk_widget_is_sensitive (widget);
7708   gtk_widget_propagate_state (widget, &data);
7709
7710   gtk_widget_reset_rc_styles (widget);
7711
7712   g_signal_emit (widget, widget_signals[PARENT_SET], 0, NULL);
7713   if (priv->parent->priv->anchored)
7714     _gtk_widget_propagate_hierarchy_changed (widget, NULL);
7715   g_object_notify (G_OBJECT (widget), "parent");
7716
7717   /* Enforce realized/mapped invariants
7718    */
7719   if (gtk_widget_get_realized (priv->parent))
7720     gtk_widget_realize (widget);
7721
7722   if (gtk_widget_get_visible (priv->parent) &&
7723       gtk_widget_get_visible (widget))
7724     {
7725       if (gtk_widget_get_child_visible (widget) &&
7726           gtk_widget_get_mapped (priv->parent))
7727         gtk_widget_map (widget);
7728
7729       gtk_widget_queue_resize (widget);
7730     }
7731
7732   /* child may cause parent's expand to change, if the child is
7733    * expanded. If child is not expanded, then it can't modify the
7734    * parent's expand. If the child becomes expanded later then it will
7735    * queue compute_expand then. This optimization plus defaulting
7736    * newly-constructed widgets to need_compute_expand=FALSE should
7737    * mean that initially building a widget tree doesn't have to keep
7738    * walking up setting need_compute_expand on parents over and over.
7739    *
7740    * We can't change a parent to need to expand unless we're visible.
7741    */
7742   if (gtk_widget_get_visible (widget) &&
7743       (priv->need_compute_expand ||
7744        priv->computed_hexpand ||
7745        priv->computed_vexpand))
7746     {
7747       gtk_widget_queue_compute_expand (parent);
7748     }
7749
7750   if (widget->priv->context)
7751     {
7752       GdkScreen *screen;
7753
7754       _gtk_widget_update_path (widget);
7755       gtk_style_context_set_path (widget->priv->context, widget->priv->path);
7756
7757       screen = gtk_widget_get_screen (widget);
7758
7759       if (screen)
7760         gtk_style_context_set_screen (widget->priv->context, screen);
7761     }
7762
7763   gtk_widget_pop_verify_invariants (widget);
7764 }
7765
7766 /**
7767  * gtk_widget_get_parent:
7768  * @widget: a #GtkWidget
7769  *
7770  * Returns the parent container of @widget.
7771  *
7772  * Return value: (transfer none): the parent container of @widget, or %NULL
7773  **/
7774 GtkWidget *
7775 gtk_widget_get_parent (GtkWidget *widget)
7776 {
7777   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7778
7779   return widget->priv->parent;
7780 }
7781
7782 /*****************************************
7783  * Widget styles
7784  * see docs/styles.txt
7785  *****************************************/
7786
7787 /**
7788  * gtk_widget_style_attach:
7789  * @widget: a #GtkWidget
7790  *
7791  * This function attaches the widget's #GtkStyle to the widget's
7792  * #GdkWindow. It is a replacement for
7793  *
7794  * <programlisting>
7795  * widget->style = gtk_style_attach (widget->style, widget->window);
7796  * </programlisting>
7797  *
7798  * and should only ever be called in a derived widget's "realize"
7799  * implementation which does not chain up to its parent class'
7800  * "realize" implementation, because one of the parent classes
7801  * (finally #GtkWidget) would attach the style itself.
7802  *
7803  * Since: 2.20
7804  *
7805  * Deprecated: 3.0. This step is unnecessary with #GtkStyleContext.
7806  **/
7807 void
7808 gtk_widget_style_attach (GtkWidget *widget)
7809 {
7810   GtkWidgetPrivate *priv;
7811
7812   g_return_if_fail (GTK_IS_WIDGET (widget));
7813   g_return_if_fail (gtk_widget_get_realized (widget));
7814
7815   priv = widget->priv;
7816
7817   priv->style = gtk_style_attach (priv->style, priv->window);
7818 }
7819
7820 /**
7821  * gtk_widget_has_rc_style:
7822  * @widget: a #GtkWidget
7823  *
7824  * Determines if the widget style has been looked up through the rc mechanism.
7825  *
7826  * Returns: %TRUE if the widget has been looked up through the rc
7827  *   mechanism, %FALSE otherwise.
7828  *
7829  * Since: 2.20
7830  *
7831  * Deprecated:3.0: Use #GtkStyleContext instead
7832  **/
7833 gboolean
7834 gtk_widget_has_rc_style (GtkWidget *widget)
7835 {
7836   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7837
7838   return widget->priv->rc_style;
7839 }
7840
7841 /**
7842  * gtk_widget_set_style:
7843  * @widget: a #GtkWidget
7844  * @style: (allow-none): a #GtkStyle, or %NULL to remove the effect
7845  *     of a previous call to gtk_widget_set_style() and go back to
7846  *     the default style
7847  *
7848  * Used to set the #GtkStyle for a widget (@widget->style). Since
7849  * GTK 3, this function does nothing, the passed in style is ignored.
7850  *
7851  * Deprecated:3.0: Use #GtkStyleContext instead
7852  */
7853 void
7854 gtk_widget_set_style (GtkWidget *widget,
7855                       GtkStyle  *style)
7856 {
7857   g_return_if_fail (GTK_IS_WIDGET (widget));
7858 }
7859
7860 /**
7861  * gtk_widget_ensure_style:
7862  * @widget: a #GtkWidget
7863  *
7864  * Ensures that @widget has a style (@widget->style).
7865  *
7866  * Not a very useful function; most of the time, if you
7867  * want the style, the widget is realized, and realized
7868  * widgets are guaranteed to have a style already.
7869  *
7870  * Deprecated:3.0: Use #GtkStyleContext instead
7871  */
7872 void
7873 gtk_widget_ensure_style (GtkWidget *widget)
7874 {
7875   GtkWidgetPrivate *priv;
7876
7877   g_return_if_fail (GTK_IS_WIDGET (widget));
7878
7879   priv = widget->priv;
7880
7881   if (priv->style == gtk_widget_get_default_style ())
7882     {
7883       GtkStyle *style;
7884
7885       g_object_unref (priv->style);
7886
7887       style = g_object_new (GTK_TYPE_STYLE,
7888                             "context", gtk_widget_get_style_context (widget),
7889                             NULL);
7890
7891       priv->style = g_object_ref (style);
7892
7893       g_signal_emit (widget,
7894                      widget_signals[STYLE_SET],
7895                      0, NULL);
7896
7897       g_object_unref (style);
7898     }
7899 }
7900
7901 /**
7902  * gtk_widget_get_style:
7903  * @widget: a #GtkWidget
7904  *
7905  * Simply an accessor function that returns @widget->style.
7906  *
7907  * Return value: (transfer none): the widget's #GtkStyle
7908  *
7909  * Deprecated:3.0: Use #GtkStyleContext instead
7910  */
7911 GtkStyle*
7912 gtk_widget_get_style (GtkWidget *widget)
7913 {
7914   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7915
7916   return widget->priv->style;
7917 }
7918
7919 /**
7920  * gtk_widget_modify_style:
7921  * @widget: a #GtkWidget
7922  * @style: the #GtkRcStyle holding the style modifications
7923  *
7924  * Modifies style values on the widget.
7925  *
7926  * Modifications made using this technique take precedence over
7927  * style values set via an RC file, however, they will be overridden
7928  * if a style is explicitely set on the widget using gtk_widget_set_style().
7929  * The #GtkRcStyle structure is designed so each field can either be
7930  * set or unset, so it is possible, using this function, to modify some
7931  * style values and leave the others unchanged.
7932  *
7933  * Note that modifications made with this function are not cumulative
7934  * with previous calls to gtk_widget_modify_style() or with such
7935  * functions as gtk_widget_modify_fg(). If you wish to retain
7936  * previous values, you must first call gtk_widget_get_modifier_style(),
7937  * make your modifications to the returned style, then call
7938  * gtk_widget_modify_style() with that style. On the other hand,
7939  * if you first call gtk_widget_modify_style(), subsequent calls
7940  * to such functions gtk_widget_modify_fg() will have a cumulative
7941  * effect with the initial modifications.
7942  *
7943  * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
7944  */
7945 void
7946 gtk_widget_modify_style (GtkWidget      *widget,
7947                          GtkRcStyle     *style)
7948 {
7949   g_return_if_fail (GTK_IS_WIDGET (widget));
7950   g_return_if_fail (GTK_IS_RC_STYLE (style));
7951
7952   g_object_set_qdata_full (G_OBJECT (widget),
7953                            quark_rc_style,
7954                            gtk_rc_style_copy (style),
7955                            (GDestroyNotify) g_object_unref);
7956 }
7957
7958 /**
7959  * gtk_widget_get_modifier_style:
7960  * @widget: a #GtkWidget
7961  *
7962  * Returns the current modifier style for the widget. (As set by
7963  * gtk_widget_modify_style().) If no style has previously set, a new
7964  * #GtkRcStyle will be created with all values unset, and set as the
7965  * modifier style for the widget. If you make changes to this rc
7966  * style, you must call gtk_widget_modify_style(), passing in the
7967  * returned rc style, to make sure that your changes take effect.
7968  *
7969  * Caution: passing the style back to gtk_widget_modify_style() will
7970  * normally end up destroying it, because gtk_widget_modify_style() copies
7971  * the passed-in style and sets the copy as the new modifier style,
7972  * thus dropping any reference to the old modifier style. Add a reference
7973  * to the modifier style if you want to keep it alive.
7974  *
7975  * Return value: (transfer none): the modifier style for the widget.
7976  *     This rc style is owned by the widget. If you want to keep a
7977  *     pointer to value this around, you must add a refcount using
7978  *     g_object_ref().
7979  *
7980  * Deprecated:3.0: Use #GtkStyleContext with a custom #GtkStyleProvider instead
7981  */
7982 GtkRcStyle *
7983 gtk_widget_get_modifier_style (GtkWidget *widget)
7984 {
7985   GtkRcStyle *rc_style;
7986
7987   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7988
7989   rc_style = g_object_get_qdata (G_OBJECT (widget), quark_rc_style);
7990
7991   if (!rc_style)
7992     {
7993       rc_style = gtk_rc_style_new ();
7994       g_object_set_qdata_full (G_OBJECT (widget),
7995                                quark_rc_style,
7996                                rc_style,
7997                                (GDestroyNotify) g_object_unref);
7998     }
7999
8000   return rc_style;
8001 }
8002
8003 static void
8004 gtk_widget_modify_color_component (GtkWidget      *widget,
8005                                    GtkRcFlags      component,
8006                                    GtkStateType    state,
8007                                    const GdkColor *color)
8008 {
8009   GtkRcStyle *rc_style = gtk_widget_get_modifier_style (widget);
8010
8011   if (color)
8012     {
8013       switch (component)
8014         {
8015         case GTK_RC_FG:
8016           rc_style->fg[state] = *color;
8017           break;
8018         case GTK_RC_BG:
8019           rc_style->bg[state] = *color;
8020           break;
8021         case GTK_RC_TEXT:
8022           rc_style->text[state] = *color;
8023           break;
8024         case GTK_RC_BASE:
8025           rc_style->base[state] = *color;
8026           break;
8027         default:
8028           g_assert_not_reached();
8029         }
8030
8031       rc_style->color_flags[state] |= component;
8032     }
8033   else
8034     rc_style->color_flags[state] &= ~component;
8035
8036   gtk_widget_modify_style (widget, rc_style);
8037 }
8038
8039 static void
8040 modifier_style_changed (GtkModifierStyle *style,
8041                         GtkWidget        *widget)
8042 {
8043   GtkStyleContext *context;
8044
8045   context = gtk_widget_get_style_context (widget);
8046   gtk_style_context_invalidate (context);
8047 }
8048
8049 static GtkModifierStyle *
8050 _gtk_widget_get_modifier_properties (GtkWidget *widget)
8051 {
8052   GtkModifierStyle *style;
8053
8054   style = g_object_get_qdata (G_OBJECT (widget), quark_modifier_style);
8055
8056   if (G_UNLIKELY (!style))
8057     {
8058       GtkStyleContext *context;
8059
8060       style = _gtk_modifier_style_new ();
8061       g_object_set_qdata_full (G_OBJECT (widget),
8062                                quark_modifier_style,
8063                                style,
8064                                (GDestroyNotify) g_object_unref);
8065
8066       g_signal_connect (style, "changed",
8067                         G_CALLBACK (modifier_style_changed), widget);
8068
8069       context = gtk_widget_get_style_context (widget);
8070
8071       gtk_style_context_add_provider (context,
8072                                       GTK_STYLE_PROVIDER (style),
8073                                       GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
8074     }
8075
8076   return style;
8077 }
8078
8079 /**
8080  * gtk_widget_override_color:
8081  * @widget: a #GtkWidget
8082  * @state: the state for which to set the color
8083  * @color: the color to assign, or %NULL to undo the effect
8084  *     of previous calls to gtk_widget_override_color()
8085  *
8086  * Sets the color to use for a widget.
8087  *
8088  * All other style values are left untouched.
8089  *
8090  * <note><para>
8091  * This API is mostly meant as a quick way for applications to
8092  * change a widget appearance. If you are developing a widgets
8093  * library and intend this change to be themeable, it is better
8094  * done by setting meaningful CSS classes and regions in your
8095  * widget/container implementation through gtk_style_context_add_class()
8096  * and gtk_style_context_add_region().
8097  * </para><para>
8098  * This way, your widget library can install a #GtkCssProvider
8099  * with the %GTK_STYLE_PROVIDER_PRIORITY_FALLBACK priority in order
8100  * to provide a default styling for those widgets that need so, and
8101  * this theming may fully overridden by the user's theme.
8102  * </para></note>
8103  * <note><para>
8104  * Note that for complex widgets this may bring in undesired
8105  * results (such as uniform background color everywhere), in
8106  * these cases it is better to fully style such widgets through a
8107  * #GtkCssProvider with the %GTK_STYLE_PROVIDER_PRIORITY_APPLICATION
8108  * priority.
8109  * </para></note>
8110  *
8111  * Since: 3.0
8112  */
8113 void
8114 gtk_widget_override_color (GtkWidget     *widget,
8115                            GtkStateFlags  state,
8116                            const GdkRGBA *color)
8117 {
8118   GtkModifierStyle *style;
8119
8120   g_return_if_fail (GTK_IS_WIDGET (widget));
8121
8122   style = _gtk_widget_get_modifier_properties (widget);
8123   _gtk_modifier_style_set_color (style, state, color);
8124 }
8125
8126 /**
8127  * gtk_widget_override_background_color:
8128  * @widget: a #GtkWidget
8129  * @state: the state for which to set the background color
8130  * @color: the color to assign, or %NULL to undo the effect
8131  *     of previous calls to gtk_widget_override_background_color()
8132  *
8133  * Sets the background color to use for a widget.
8134  *
8135  * All other style values are left untouched.
8136  * See gtk_widget_override_color().
8137  *
8138  * Since: 3.0
8139  */
8140 void
8141 gtk_widget_override_background_color (GtkWidget     *widget,
8142                                       GtkStateFlags  state,
8143                                       const GdkRGBA *color)
8144 {
8145   GtkModifierStyle *style;
8146
8147   g_return_if_fail (GTK_IS_WIDGET (widget));
8148
8149   style = _gtk_widget_get_modifier_properties (widget);
8150   _gtk_modifier_style_set_background_color (style, state, color);
8151 }
8152
8153 /**
8154  * gtk_widget_override_font:
8155  * @widget: a #GtkWidget
8156  * @font_desc: the font descriptiong to use, or %NULL to undo
8157  *     the effect of previous calls to gtk_widget_override_font()
8158  *
8159  * Sets the font to use for a widget. All other style values are
8160  * left untouched. See gtk_widget_override_color().
8161  *
8162  * Since: 3.0
8163  */
8164 void
8165 gtk_widget_override_font (GtkWidget                  *widget,
8166                           const PangoFontDescription *font_desc)
8167 {
8168   GtkModifierStyle *style;
8169
8170   g_return_if_fail (GTK_IS_WIDGET (widget));
8171
8172   style = _gtk_widget_get_modifier_properties (widget);
8173   _gtk_modifier_style_set_font (style, font_desc);
8174 }
8175
8176 /**
8177  * gtk_widget_override_symbolic_color:
8178  * @widget: a #GtkWidget
8179  * @name: the name of the symbolic color to modify
8180  * @color: (allow-none): the color to assign (does not need
8181  *     to be allocated), or %NULL to undo the effect of previous
8182  *     calls to gtk_widget_override_symbolic_color()
8183  *
8184  * Sets a symbolic color for a widget.
8185  *
8186  * All other style values are left untouched.
8187  * See gtk_widget_override_color() for overriding the foreground
8188  * or background color.
8189  *
8190  * Since: 3.0
8191  */
8192 void
8193 gtk_widget_override_symbolic_color (GtkWidget     *widget,
8194                                     const gchar   *name,
8195                                     const GdkRGBA *color)
8196 {
8197   GtkModifierStyle *style;
8198
8199   g_return_if_fail (GTK_IS_WIDGET (widget));
8200
8201   style = _gtk_widget_get_modifier_properties (widget);
8202   _gtk_modifier_style_map_color (style, name, color);
8203 }
8204
8205 /**
8206  * gtk_widget_override_cursor:
8207  * @widget: a #GtkWidget
8208  * @cursor: the color to use for primary cursor (does not need to be
8209  *     allocated), or %NULL to undo the effect of previous calls to
8210  *     of gtk_widget_override_cursor().
8211  * @secondary_cursor: the color to use for secondary cursor (does not
8212  *     need to be allocated), or %NULL to undo the effect of previous
8213  *     calls to of gtk_widget_override_cursor().
8214  *
8215  * Sets the cursor color to use in a widget, overriding the
8216  * #GtkWidget:cursor-color and #GtkWidget:secondary-cursor-color
8217  * style properties. All other style values are left untouched.
8218  * See also gtk_widget_modify_style().
8219  *
8220  * Note that the underlying properties have the #GdkColor type,
8221  * so the alpha value in @primary and @secondary will be ignored.
8222  *
8223  * Since: 3.0
8224  */
8225 void
8226 gtk_widget_override_cursor (GtkWidget     *widget,
8227                             const GdkRGBA *cursor,
8228                             const GdkRGBA *secondary_cursor)
8229 {
8230   GtkModifierStyle *style;
8231
8232   g_return_if_fail (GTK_IS_WIDGET (widget));
8233
8234   style = _gtk_widget_get_modifier_properties (widget);
8235   _gtk_modifier_style_set_color_property (style,
8236                                           GTK_TYPE_WIDGET,
8237                                           "cursor-color", cursor);
8238   _gtk_modifier_style_set_color_property (style,
8239                                           GTK_TYPE_WIDGET,
8240                                           "secondary-cursor-color",
8241                                           secondary_cursor);
8242 }
8243
8244 /**
8245  * gtk_widget_modify_fg:
8246  * @widget: a #GtkWidget
8247  * @state: the state for which to set the foreground color
8248  * @color: (allow-none): the color to assign (does not need to be allocated),
8249  *     or %NULL to undo the effect of previous calls to
8250  *     of gtk_widget_modify_fg().
8251  *
8252  * Sets the foreground color for a widget in a particular state.
8253  *
8254  * All other style values are left untouched.
8255  * See also gtk_widget_modify_style().
8256  *
8257  * Deprecated:3.0: Use gtk_widget_override_color() instead
8258  */
8259 void
8260 gtk_widget_modify_fg (GtkWidget      *widget,
8261                       GtkStateType    state,
8262                       const GdkColor *color)
8263 {
8264   GtkStateFlags flags;
8265   GdkRGBA rgba;
8266
8267   g_return_if_fail (GTK_IS_WIDGET (widget));
8268   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8269
8270   switch (state)
8271     {
8272     case GTK_STATE_ACTIVE:
8273       flags = GTK_STATE_FLAG_ACTIVE;
8274       break;
8275     case GTK_STATE_PRELIGHT:
8276       flags = GTK_STATE_FLAG_PRELIGHT;
8277       break;
8278     case GTK_STATE_SELECTED:
8279       flags = GTK_STATE_FLAG_SELECTED;
8280       break;
8281     case GTK_STATE_INSENSITIVE:
8282       flags = GTK_STATE_FLAG_INSENSITIVE;
8283       break;
8284     case GTK_STATE_NORMAL:
8285     default:
8286       flags = 0;
8287     }
8288
8289   if (color)
8290     {
8291       rgba.red = color->red / 65535.;
8292       rgba.green = color->green / 65535.;
8293       rgba.blue = color->blue / 65535.;
8294       rgba.alpha = 1;
8295
8296       gtk_widget_override_color (widget, flags, &rgba);
8297     }
8298   else
8299     gtk_widget_override_color (widget, flags, NULL);
8300 }
8301
8302 /**
8303  * gtk_widget_modify_bg:
8304  * @widget: a #GtkWidget
8305  * @state: the state for which to set the background color
8306  * @color: (allow-none): the color to assign (does not need
8307  *     to be allocated), or %NULL to undo the effect of previous
8308  *     calls to of gtk_widget_modify_bg().
8309  *
8310  * Sets the background color for a widget in a particular state.
8311  *
8312  * All other style values are left untouched.
8313  * See also gtk_widget_modify_style().
8314  *
8315  * <note><para>
8316  * Note that "no window" widgets (which have the %GTK_NO_WINDOW
8317  * flag set) draw on their parent container's window and thus may
8318  * not draw any background themselves. This is the case for e.g.
8319  * #GtkLabel.
8320  * </para><para>
8321  * To modify the background of such widgets, you have to set the
8322  * background color on their parent; if you want to set the background
8323  * of a rectangular area around a label, try placing the label in
8324  * a #GtkEventBox widget and setting the background color on that.
8325  * </para></note>
8326  *
8327  * Deprecated:3.0: Use gtk_widget_override_background_color() instead
8328  */
8329 void
8330 gtk_widget_modify_bg (GtkWidget      *widget,
8331                       GtkStateType    state,
8332                       const GdkColor *color)
8333 {
8334   GtkStateFlags flags;
8335   GdkRGBA rgba;
8336
8337   g_return_if_fail (GTK_IS_WIDGET (widget));
8338   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8339
8340   switch (state)
8341     {
8342     case GTK_STATE_ACTIVE:
8343       flags = GTK_STATE_FLAG_ACTIVE;
8344       break;
8345     case GTK_STATE_PRELIGHT:
8346       flags = GTK_STATE_FLAG_PRELIGHT;
8347       break;
8348     case GTK_STATE_SELECTED:
8349       flags = GTK_STATE_FLAG_SELECTED;
8350       break;
8351     case GTK_STATE_INSENSITIVE:
8352       flags = GTK_STATE_FLAG_INSENSITIVE;
8353       break;
8354     case GTK_STATE_NORMAL:
8355     default:
8356       flags = 0;
8357     }
8358
8359   if (color)
8360     {
8361       rgba.red = color->red / 65535.;
8362       rgba.green = color->green / 65535.;
8363       rgba.blue = color->blue / 65535.;
8364       rgba.alpha = 1;
8365
8366       gtk_widget_override_background_color (widget, flags, &rgba);
8367     }
8368   else
8369     gtk_widget_override_background_color (widget, flags, NULL);
8370 }
8371
8372 /**
8373  * gtk_widget_modify_text:
8374  * @widget: a #GtkWidget
8375  * @state: the state for which to set the text color
8376  * @color: (allow-none): the color to assign (does not need to
8377  *     be allocated), or %NULL to undo the effect of previous
8378  *     calls to of gtk_widget_modify_text().
8379  *
8380  * Sets the text color for a widget in a particular state.
8381  *
8382  * All other style values are left untouched.
8383  * The text color is the foreground color used along with the
8384  * base color (see gtk_widget_modify_base()) for widgets such
8385  * as #GtkEntry and #GtkTextView.
8386  * See also gtk_widget_modify_style().
8387  *
8388  * Deprecated:3.0: Use gtk_widget_override_color() instead
8389  */
8390 void
8391 gtk_widget_modify_text (GtkWidget      *widget,
8392                         GtkStateType    state,
8393                         const GdkColor *color)
8394 {
8395   g_return_if_fail (GTK_IS_WIDGET (widget));
8396   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8397
8398   gtk_widget_modify_color_component (widget, GTK_RC_TEXT, state, color);
8399 }
8400
8401 /**
8402  * gtk_widget_modify_base:
8403  * @widget: a #GtkWidget
8404  * @state: the state for which to set the base color
8405  * @color: (allow-none): the color to assign (does not need to
8406  *     be allocated), or %NULL to undo the effect of previous
8407  *     calls to of gtk_widget_modify_base().
8408  *
8409  * Sets the base color for a widget in a particular state.
8410  * All other style values are left untouched. The base color
8411  * is the background color used along with the text color
8412  * (see gtk_widget_modify_text()) for widgets such as #GtkEntry
8413  * and #GtkTextView. See also gtk_widget_modify_style().
8414  *
8415  * <note><para>
8416  * Note that "no window" widgets (which have the %GTK_NO_WINDOW
8417  * flag set) draw on their parent container's window and thus may
8418  * not draw any background themselves. This is the case for e.g.
8419  * #GtkLabel.
8420  * </para><para>
8421  * To modify the background of such widgets, you have to set the
8422  * base color on their parent; if you want to set the background
8423  * of a rectangular area around a label, try placing the label in
8424  * a #GtkEventBox widget and setting the base color on that.
8425  * </para></note>
8426  *
8427  * Deprecated:3.0: Use gtk_widget_override_background_color() instead
8428  */
8429 void
8430 gtk_widget_modify_base (GtkWidget      *widget,
8431                         GtkStateType    state,
8432                         const GdkColor *color)
8433 {
8434   g_return_if_fail (GTK_IS_WIDGET (widget));
8435   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
8436
8437   gtk_widget_modify_color_component (widget, GTK_RC_BASE, state, color);
8438 }
8439
8440 /**
8441  * gtk_widget_modify_cursor:
8442  * @widget: a #GtkWidget
8443  * @primary: the color to use for primary cursor (does not need to be
8444  *     allocated), or %NULL to undo the effect of previous calls to
8445  *     of gtk_widget_modify_cursor().
8446  * @secondary: the color to use for secondary cursor (does not need to be
8447  *     allocated), or %NULL to undo the effect of previous calls to
8448  *     of gtk_widget_modify_cursor().
8449  *
8450  * Sets the cursor color to use in a widget, overriding the
8451  * #GtkWidget:cursor-color and #GtkWidget:secondary-cursor-color
8452  * style properties.
8453  *
8454  * All other style values are left untouched.
8455  * See also gtk_widget_modify_style().
8456  *
8457  * Since: 2.12
8458  *
8459  * Deprecated: 3.0. Use gtk_widget_override_cursor() instead.
8460  */
8461 void
8462 gtk_widget_modify_cursor (GtkWidget      *widget,
8463                           const GdkColor *primary,
8464                           const GdkColor *secondary)
8465 {
8466   GdkRGBA primary_rgba, secondary_rgba;
8467
8468   g_return_if_fail (GTK_IS_WIDGET (widget));
8469
8470   primary_rgba.red = primary->red / 65535.;
8471   primary_rgba.green = primary->green / 65535.;
8472   primary_rgba.blue = primary->blue / 65535.;
8473   primary_rgba.alpha = 1;
8474
8475   secondary_rgba.red = secondary->red / 65535.;
8476   secondary_rgba.green = secondary->green / 65535.;
8477   secondary_rgba.blue = secondary->blue / 65535.;
8478   secondary_rgba.alpha = 1;
8479
8480   gtk_widget_override_cursor (widget, &primary_rgba, &secondary_rgba);
8481 }
8482
8483 /**
8484  * gtk_widget_modify_font:
8485  * @widget: a #GtkWidget
8486  * @font_desc: (allow-none): the font description to use, or %NULL
8487  *     to undo the effect of previous calls to gtk_widget_modify_font()
8488  *
8489  * Sets the font to use for a widget.
8490  *
8491  * All other style values are left untouched.
8492  * See also gtk_widget_modify_style().
8493  *
8494  * Deprecated:3.0: Use gtk_widget_override_font() instead
8495  */
8496 void
8497 gtk_widget_modify_font (GtkWidget            *widget,
8498                         PangoFontDescription *font_desc)
8499 {
8500   g_return_if_fail (GTK_IS_WIDGET (widget));
8501
8502   gtk_widget_override_font (widget, font_desc);
8503 }
8504
8505 static void
8506 gtk_widget_real_direction_changed (GtkWidget        *widget,
8507                                    GtkTextDirection  previous_direction)
8508 {
8509   gtk_widget_queue_resize (widget);
8510 }
8511
8512 static void
8513 gtk_widget_real_style_set (GtkWidget *widget,
8514                            GtkStyle  *previous_style)
8515 {
8516 }
8517
8518 typedef struct {
8519   GtkWidget *previous_toplevel;
8520   GdkScreen *previous_screen;
8521   GdkScreen *new_screen;
8522 } HierarchyChangedInfo;
8523
8524 static void
8525 do_screen_change (GtkWidget *widget,
8526                   GdkScreen *old_screen,
8527                   GdkScreen *new_screen)
8528 {
8529   if (old_screen != new_screen)
8530     {
8531       GtkWidgetPrivate *priv = widget->priv;
8532
8533       if (old_screen)
8534         {
8535           PangoContext *context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
8536           if (context)
8537             g_object_set_qdata (G_OBJECT (widget), quark_pango_context, NULL);
8538         }
8539
8540       _gtk_tooltip_hide (widget);
8541
8542       if (new_screen && priv->context)
8543         gtk_style_context_set_screen (priv->context, new_screen);
8544
8545       g_signal_emit (widget, widget_signals[SCREEN_CHANGED], 0, old_screen);
8546     }
8547 }
8548
8549 static void
8550 gtk_widget_propagate_hierarchy_changed_recurse (GtkWidget *widget,
8551                                                 gpointer   client_data)
8552 {
8553   GtkWidgetPrivate *priv = widget->priv;
8554   HierarchyChangedInfo *info = client_data;
8555   gboolean new_anchored = gtk_widget_is_toplevel (widget) ||
8556                  (priv->parent && priv->parent->priv->anchored);
8557
8558   if (priv->anchored != new_anchored)
8559     {
8560       g_object_ref (widget);
8561
8562       priv->anchored = new_anchored;
8563
8564       g_signal_emit (widget, widget_signals[HIERARCHY_CHANGED], 0, info->previous_toplevel);
8565       do_screen_change (widget, info->previous_screen, info->new_screen);
8566
8567       if (GTK_IS_CONTAINER (widget))
8568         gtk_container_forall (GTK_CONTAINER (widget),
8569                               gtk_widget_propagate_hierarchy_changed_recurse,
8570                               client_data);
8571
8572       g_object_unref (widget);
8573     }
8574 }
8575
8576 /**
8577  * _gtk_widget_propagate_hierarchy_changed:
8578  * @widget: a #GtkWidget
8579  * @previous_toplevel: Previous toplevel
8580  *
8581  * Propagates changes in the anchored state to a widget and all
8582  * children, unsetting or setting the %ANCHORED flag, and
8583  * emitting #GtkWidget::hierarchy-changed.
8584  **/
8585 void
8586 _gtk_widget_propagate_hierarchy_changed (GtkWidget *widget,
8587                                          GtkWidget *previous_toplevel)
8588 {
8589   GtkWidgetPrivate *priv = widget->priv;
8590   HierarchyChangedInfo info;
8591
8592   info.previous_toplevel = previous_toplevel;
8593   info.previous_screen = previous_toplevel ? gtk_widget_get_screen (previous_toplevel) : NULL;
8594
8595   if (gtk_widget_is_toplevel (widget) ||
8596       (priv->parent && priv->parent->priv->anchored))
8597     info.new_screen = gtk_widget_get_screen (widget);
8598   else
8599     info.new_screen = NULL;
8600
8601   if (info.previous_screen)
8602     g_object_ref (info.previous_screen);
8603   if (previous_toplevel)
8604     g_object_ref (previous_toplevel);
8605
8606   gtk_widget_propagate_hierarchy_changed_recurse (widget, &info);
8607
8608   if (previous_toplevel)
8609     g_object_unref (previous_toplevel);
8610   if (info.previous_screen)
8611     g_object_unref (info.previous_screen);
8612 }
8613
8614 static void
8615 gtk_widget_propagate_screen_changed_recurse (GtkWidget *widget,
8616                                              gpointer   client_data)
8617 {
8618   HierarchyChangedInfo *info = client_data;
8619
8620   g_object_ref (widget);
8621
8622   do_screen_change (widget, info->previous_screen, info->new_screen);
8623
8624   if (GTK_IS_CONTAINER (widget))
8625     gtk_container_forall (GTK_CONTAINER (widget),
8626                           gtk_widget_propagate_screen_changed_recurse,
8627                           client_data);
8628
8629   g_object_unref (widget);
8630 }
8631
8632 /**
8633  * gtk_widget_is_composited:
8634  * @widget: a #GtkWidget
8635  *
8636  * Whether @widget can rely on having its alpha channel
8637  * drawn correctly. On X11 this function returns whether a
8638  * compositing manager is running for @widget's screen.
8639  *
8640  * Please note that the semantics of this call will change
8641  * in the future if used on a widget that has a composited
8642  * window in its hierarchy (as set by gdk_window_set_composited()).
8643  *
8644  * Return value: %TRUE if the widget can rely on its alpha
8645  * channel being drawn correctly.
8646  *
8647  * Since: 2.10
8648  */
8649 gboolean
8650 gtk_widget_is_composited (GtkWidget *widget)
8651 {
8652   GdkScreen *screen;
8653
8654   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
8655
8656   screen = gtk_widget_get_screen (widget);
8657
8658   return gdk_screen_is_composited (screen);
8659 }
8660
8661 static void
8662 propagate_composited_changed (GtkWidget *widget,
8663                               gpointer dummy)
8664 {
8665   if (GTK_IS_CONTAINER (widget))
8666     {
8667       gtk_container_forall (GTK_CONTAINER (widget),
8668                             propagate_composited_changed,
8669                             NULL);
8670     }
8671
8672   g_signal_emit (widget, widget_signals[COMPOSITED_CHANGED], 0);
8673 }
8674
8675 void
8676 _gtk_widget_propagate_composited_changed (GtkWidget *widget)
8677 {
8678   propagate_composited_changed (widget, NULL);
8679 }
8680
8681 /**
8682  * _gtk_widget_propagate_screen_changed:
8683  * @widget: a #GtkWidget
8684  * @previous_screen: Previous screen
8685  *
8686  * Propagates changes in the screen for a widget to all
8687  * children, emitting #GtkWidget::screen-changed.
8688  **/
8689 void
8690 _gtk_widget_propagate_screen_changed (GtkWidget    *widget,
8691                                       GdkScreen    *previous_screen)
8692 {
8693   HierarchyChangedInfo info;
8694
8695   info.previous_screen = previous_screen;
8696   info.new_screen = gtk_widget_get_screen (widget);
8697
8698   if (previous_screen)
8699     g_object_ref (previous_screen);
8700
8701   gtk_widget_propagate_screen_changed_recurse (widget, &info);
8702
8703   if (previous_screen)
8704     g_object_unref (previous_screen);
8705 }
8706
8707 static void
8708 reset_style_recurse (GtkWidget *widget, gpointer data)
8709 {
8710   if (widget->priv->context)
8711     {
8712       _gtk_widget_update_path (widget);
8713       gtk_style_context_set_path (widget->priv->context,
8714                                   widget->priv->path);
8715     }
8716
8717   if (GTK_IS_CONTAINER (widget))
8718     gtk_container_forall (GTK_CONTAINER (widget),
8719                           reset_style_recurse,
8720                           NULL);
8721 }
8722
8723 /**
8724  * gtk_widget_reset_style:
8725  * @widget: a #GtkWidget
8726  *
8727  * Updates the style context of @widget and all descendents
8728  * by updating its widget path. #GtkContainer<!-- -->s may want
8729  * to use this on a child when reordering it in a way that a different
8730  * style might apply to it. See also gtk_container_get_path_for_child().
8731  *
8732  * Since: 3.0
8733  */
8734 void
8735 gtk_widget_reset_style (GtkWidget *widget)
8736 {
8737   g_return_if_fail (GTK_IS_WIDGET (widget));
8738
8739   reset_style_recurse (widget, NULL);
8740 }
8741
8742 /**
8743  * gtk_widget_reset_rc_styles:
8744  * @widget: a #GtkWidget.
8745  *
8746  * Reset the styles of @widget and all descendents, so when
8747  * they are looked up again, they get the correct values
8748  * for the currently loaded RC file settings.
8749  *
8750  * This function is not useful for applications.
8751  *
8752  * Deprecated:3.0: Use #GtkStyleContext instead, and gtk_widget_reset_style()
8753  */
8754 void
8755 gtk_widget_reset_rc_styles (GtkWidget *widget)
8756 {
8757   g_return_if_fail (GTK_IS_WIDGET (widget));
8758
8759   reset_style_recurse (widget, NULL);
8760 }
8761
8762 /**
8763  * gtk_widget_get_default_style:
8764  *
8765  * Returns the default style used by all widgets initially.
8766  *
8767  * Returns: (transfer none): the default style. This #GtkStyle
8768  *     object is owned by GTK+ and should not be modified or freed.
8769  *
8770  * Deprecated:3.0: Use #GtkStyleContext instead, and
8771  *     gtk_css_provider_get_default() to obtain a #GtkStyleProvider
8772  *     with the default widget style information.
8773  */
8774 GtkStyle*
8775 gtk_widget_get_default_style (void)
8776 {
8777   if (!gtk_default_style)
8778     {
8779       gtk_default_style = gtk_style_new ();
8780       g_object_ref (gtk_default_style);
8781     }
8782
8783   return gtk_default_style;
8784 }
8785
8786 #ifdef G_ENABLE_DEBUG
8787
8788 /* Verify invariants, see docs/widget_system.txt for notes on much of
8789  * this.  Invariants may be temporarily broken while we're in the
8790  * process of updating state, of course, so you can only
8791  * verify_invariants() after a given operation is complete.
8792  * Use push/pop_verify_invariants to help with that.
8793  */
8794 static void
8795 gtk_widget_verify_invariants (GtkWidget *widget)
8796 {
8797   GtkWidget *parent;
8798
8799   if (widget->priv->verifying_invariants_count > 0)
8800     return;
8801
8802   parent = widget->priv->parent;
8803
8804   if (widget->priv->mapped)
8805     {
8806       /* Mapped implies ... */
8807
8808       if (!widget->priv->realized)
8809         g_warning ("%s %p is mapped but not realized",
8810                    G_OBJECT_TYPE_NAME (widget), widget);
8811
8812       if (!widget->priv->visible)
8813         g_warning ("%s %p is mapped but not visible",
8814                    G_OBJECT_TYPE_NAME (widget), widget);
8815
8816       if (!widget->priv->toplevel)
8817         {
8818           if (!widget->priv->child_visible)
8819             g_warning ("%s %p is mapped but not child_visible",
8820                        G_OBJECT_TYPE_NAME (widget), widget);
8821         }
8822     }
8823   else
8824     {
8825       /* Not mapped implies... */
8826
8827 #if 0
8828   /* This check makes sense for normal toplevels, but for
8829    * something like a toplevel that is embedded within a clutter
8830    * state, mapping may depend on external factors.
8831    */
8832       if (widget->priv->toplevel)
8833         {
8834           if (widget->priv->visible)
8835             g_warning ("%s %p toplevel is visible but not mapped",
8836                        G_OBJECT_TYPE_NAME (widget), widget);
8837         }
8838 #endif
8839     }
8840
8841   /* Parent related checks aren't possible if parent has
8842    * verifying_invariants_count > 0 because parent needs to recurse
8843    * children first before the invariants will hold.
8844    */
8845   if (parent == NULL || parent->priv->verifying_invariants_count == 0)
8846     {
8847       if (parent &&
8848           parent->priv->realized)
8849         {
8850           /* Parent realized implies... */
8851
8852 #if 0
8853           /* This is in widget_system.txt but appears to fail
8854            * because there's no gtk_container_realize() that
8855            * realizes all children... instead we just lazily
8856            * wait for map to fix things up.
8857            */
8858           if (!widget->priv->realized)
8859             g_warning ("%s %p is realized but child %s %p is not realized",
8860                        G_OBJECT_TYPE_NAME (parent), parent,
8861                        G_OBJECT_TYPE_NAME (widget), widget);
8862 #endif
8863         }
8864       else if (!widget->priv->toplevel)
8865         {
8866           /* No parent or parent not realized on non-toplevel implies... */
8867
8868           if (widget->priv->realized && !widget->priv->in_reparent)
8869             g_warning ("%s %p is not realized but child %s %p is realized",
8870                        parent ? G_OBJECT_TYPE_NAME (parent) : "no parent", parent,
8871                        G_OBJECT_TYPE_NAME (widget), widget);
8872         }
8873
8874       if (parent &&
8875           parent->priv->mapped &&
8876           widget->priv->visible &&
8877           widget->priv->child_visible)
8878         {
8879           /* Parent mapped and we are visible implies... */
8880
8881           if (!widget->priv->mapped)
8882             g_warning ("%s %p is mapped but visible child %s %p is not mapped",
8883                        G_OBJECT_TYPE_NAME (parent), parent,
8884                        G_OBJECT_TYPE_NAME (widget), widget);
8885         }
8886       else if (!widget->priv->toplevel)
8887         {
8888           /* No parent or parent not mapped on non-toplevel implies... */
8889
8890           if (widget->priv->mapped && !widget->priv->in_reparent)
8891             g_warning ("%s %p is mapped but visible=%d child_visible=%d parent %s %p mapped=%d",
8892                        G_OBJECT_TYPE_NAME (widget), widget,
8893                        widget->priv->visible,
8894                        widget->priv->child_visible,
8895                        parent ? G_OBJECT_TYPE_NAME (parent) : "no parent", parent,
8896                        parent ? parent->priv->mapped : FALSE);
8897         }
8898     }
8899
8900   if (!widget->priv->realized)
8901     {
8902       /* Not realized implies... */
8903
8904 #if 0
8905       /* widget_system.txt says these hold, but they don't. */
8906       if (widget->priv->resize_pending)
8907         g_warning ("%s %p resize pending but not realized",
8908                    G_OBJECT_TYPE_NAME (widget), widget);
8909
8910       if (widget->priv->alloc_needed)
8911         g_warning ("%s %p alloc needed but not realized",
8912                    G_OBJECT_TYPE_NAME (widget), widget);
8913
8914       if (widget->priv->width_request_needed)
8915         g_warning ("%s %p width request needed but not realized",
8916                    G_OBJECT_TYPE_NAME (widget), widget);
8917
8918       if (widget->priv->height_request_needed)
8919         g_warning ("%s %p height request needed but not realized",
8920                    G_OBJECT_TYPE_NAME (widget), widget);
8921 #endif
8922     }
8923 }
8924
8925 /* The point of this push/pop is that invariants may not hold while
8926  * we're busy making changes. So we only check at the outermost call
8927  * on the call stack, after we finish updating everything.
8928  */
8929 static void
8930 gtk_widget_push_verify_invariants (GtkWidget *widget)
8931 {
8932   widget->priv->verifying_invariants_count += 1;
8933 }
8934
8935 static void
8936 gtk_widget_verify_child_invariants (GtkWidget *widget,
8937                                     gpointer   client_data)
8938 {
8939   /* We don't recurse further; this is a one-level check. */
8940   gtk_widget_verify_invariants (widget);
8941 }
8942
8943 static void
8944 gtk_widget_pop_verify_invariants (GtkWidget *widget)
8945 {
8946   g_assert (widget->priv->verifying_invariants_count > 0);
8947
8948   widget->priv->verifying_invariants_count -= 1;
8949
8950   if (widget->priv->verifying_invariants_count == 0)
8951     {
8952       gtk_widget_verify_invariants (widget);
8953
8954       if (GTK_IS_CONTAINER (widget))
8955         {
8956           /* Check one level of children, because our
8957            * push_verify_invariants() will have prevented some of the
8958            * checks. This does not recurse because if recursion is
8959            * needed, it will happen naturally as each child has a
8960            * push/pop on that child. For example if we're recursively
8961            * mapping children, we'll push/pop on each child as we map
8962            * it.
8963            */
8964           gtk_container_forall (GTK_CONTAINER (widget),
8965                                 gtk_widget_verify_child_invariants,
8966                                 NULL);
8967         }
8968     }
8969 }
8970 #endif /* G_ENABLE_DEBUG */
8971
8972 static PangoContext *
8973 gtk_widget_peek_pango_context (GtkWidget *widget)
8974 {
8975   return g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
8976 }
8977
8978 /**
8979  * gtk_widget_get_pango_context:
8980  * @widget: a #GtkWidget
8981  *
8982  * Gets a #PangoContext with the appropriate font map, font description,
8983  * and base direction for this widget. Unlike the context returned
8984  * by gtk_widget_create_pango_context(), this context is owned by
8985  * the widget (it can be used until the screen for the widget changes
8986  * or the widget is removed from its toplevel), and will be updated to
8987  * match any changes to the widget's attributes.
8988  *
8989  * If you create and keep a #PangoLayout using this context, you must
8990  * deal with changes to the context by calling pango_layout_context_changed()
8991  * on the layout in response to the #GtkWidget::style-updated and
8992  * #GtkWidget::direction-changed signals for the widget.
8993  *
8994  * Return value: (transfer none): the #PangoContext for the widget.
8995  **/
8996 PangoContext *
8997 gtk_widget_get_pango_context (GtkWidget *widget)
8998 {
8999   PangoContext *context;
9000
9001   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9002
9003   context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
9004   if (!context)
9005     {
9006       context = gtk_widget_create_pango_context (GTK_WIDGET (widget));
9007       g_object_set_qdata_full (G_OBJECT (widget),
9008                                quark_pango_context,
9009                                context,
9010                                g_object_unref);
9011     }
9012
9013   return context;
9014 }
9015
9016 static void
9017 update_pango_context (GtkWidget    *widget,
9018                       PangoContext *context)
9019 {
9020   PangoFontDescription *font_desc;
9021   GtkStyleContext *style_context;
9022
9023   style_context = gtk_widget_get_style_context (widget);
9024
9025   gtk_style_context_get (style_context,
9026                          gtk_widget_get_state_flags (widget),
9027                          "font", &font_desc,
9028                          NULL);
9029
9030   pango_context_set_font_description (context, font_desc);
9031   pango_context_set_base_dir (context,
9032                               gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
9033                               PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
9034
9035   pango_font_description_free (font_desc);
9036 }
9037
9038 static void
9039 gtk_widget_update_pango_context (GtkWidget *widget)
9040 {
9041   PangoContext *context = gtk_widget_peek_pango_context (widget);
9042
9043   if (context)
9044     {
9045       GdkScreen *screen;
9046
9047       update_pango_context (widget, context);
9048
9049       screen = gtk_widget_get_screen_unchecked (widget);
9050       if (screen)
9051         {
9052           pango_cairo_context_set_resolution (context,
9053                                               gdk_screen_get_resolution (screen));
9054           pango_cairo_context_set_font_options (context,
9055                                                 gdk_screen_get_font_options (screen));
9056         }
9057     }
9058 }
9059
9060 /**
9061  * gtk_widget_create_pango_context:
9062  * @widget: a #GtkWidget
9063  *
9064  * Creates a new #PangoContext with the appropriate font map,
9065  * font description, and base direction for drawing text for
9066  * this widget. See also gtk_widget_get_pango_context().
9067  *
9068  * Return value: (transfer full): the new #PangoContext
9069  **/
9070 PangoContext *
9071 gtk_widget_create_pango_context (GtkWidget *widget)
9072 {
9073   GdkScreen *screen;
9074   PangoContext *context;
9075
9076   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9077
9078   screen = gtk_widget_get_screen_unchecked (widget);
9079   if (!screen)
9080     {
9081       GTK_NOTE (MULTIHEAD,
9082                 g_warning ("gtk_widget_create_pango_context ()) called without screen"));
9083
9084       screen = gdk_screen_get_default ();
9085     }
9086
9087   context = gdk_pango_context_get_for_screen (screen);
9088
9089   update_pango_context (widget, context);
9090   pango_context_set_language (context, gtk_get_default_language ());
9091
9092   return context;
9093 }
9094
9095 /**
9096  * gtk_widget_create_pango_layout:
9097  * @widget: a #GtkWidget
9098  * @text: text to set on the layout (can be %NULL)
9099  *
9100  * Creates a new #PangoLayout with the appropriate font map,
9101  * font description, and base direction for drawing text for
9102  * this widget.
9103  *
9104  * If you keep a #PangoLayout created in this way around, in order to
9105  * notify the layout of changes to the base direction or font of this
9106  * widget, you must call pango_layout_context_changed() in response to
9107  * the #GtkWidget::style-updated and #GtkWidget::direction-changed signals
9108  * for the widget.
9109  *
9110  * Return value: (transfer full): the new #PangoLayout
9111  **/
9112 PangoLayout *
9113 gtk_widget_create_pango_layout (GtkWidget   *widget,
9114                                 const gchar *text)
9115 {
9116   PangoLayout *layout;
9117   PangoContext *context;
9118
9119   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9120
9121   context = gtk_widget_get_pango_context (widget);
9122   layout = pango_layout_new (context);
9123
9124   if (text)
9125     pango_layout_set_text (layout, text, -1);
9126
9127   return layout;
9128 }
9129
9130 /**
9131  * gtk_widget_render_icon_pixbuf:
9132  * @widget: a #GtkWidget
9133  * @stock_id: a stock ID
9134  * @size: (type int): a stock size. A size of (GtkIconSize)-1 means
9135  *     render at the size of the source and don't scale (if there are
9136  *     multiple source sizes, GTK+ picks one of the available sizes).
9137  *
9138  * A convenience function that uses the theme engine and style
9139  * settings for @widget to look up @stock_id and render it to
9140  * a pixbuf. @stock_id should be a stock icon ID such as
9141  * #GTK_STOCK_OPEN or #GTK_STOCK_OK. @size should be a size
9142  * such as #GTK_ICON_SIZE_MENU.
9143  *
9144  * The pixels in the returned #GdkPixbuf are shared with the rest of
9145  * the application and should not be modified. The pixbuf should be freed
9146  * after use with g_object_unref().
9147  *
9148  * Return value: (transfer full): a new pixbuf, or %NULL if the
9149  *     stock ID wasn't known
9150  *
9151  * Since: 3.0
9152  **/
9153 GdkPixbuf*
9154 gtk_widget_render_icon_pixbuf (GtkWidget   *widget,
9155                                const gchar *stock_id,
9156                                GtkIconSize  size)
9157 {
9158   GtkStyleContext *context;
9159   GtkIconSet *icon_set;
9160
9161   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9162   g_return_val_if_fail (stock_id != NULL, NULL);
9163   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
9164
9165   context = gtk_widget_get_style_context (widget);
9166   icon_set = gtk_style_context_lookup_icon_set (context, stock_id);
9167
9168   if (icon_set == NULL)
9169     return NULL;
9170
9171   return gtk_icon_set_render_icon_pixbuf (icon_set, context, size);
9172 }
9173
9174 /**
9175  * gtk_widget_render_icon:
9176  * @widget: a #GtkWidget
9177  * @stock_id: a stock ID
9178  * @size: (type int): a stock size. A size of (GtkIconSize)-1 means
9179  *     render at the size of the source and don't scale (if there are
9180  *     multiple source sizes, GTK+ picks one of the available sizes).
9181  * @detail: (allow-none): render detail to pass to theme engine
9182  *
9183  * A convenience function that uses the theme settings for @widget
9184  * to look up @stock_id and render it to a pixbuf. @stock_id should
9185  * be a stock icon ID such as #GTK_STOCK_OPEN or #GTK_STOCK_OK. @size
9186  * should be a size such as #GTK_ICON_SIZE_MENU. @detail should be a
9187  * string that identifies the widget or code doing the rendering, so
9188  * that theme engines can special-case rendering for that widget or
9189  * code.
9190  *
9191  * The pixels in the returned #GdkPixbuf are shared with the rest of
9192  * the application and should not be modified. The pixbuf should be
9193  * freed after use with g_object_unref().
9194  *
9195  * Return value: (transfer full): a new pixbuf, or %NULL if the
9196  *     stock ID wasn't known
9197  *
9198  * Deprecated: 3.0: Use gtk_widget_render_icon_pixbuf() instead.
9199  **/
9200 GdkPixbuf*
9201 gtk_widget_render_icon (GtkWidget      *widget,
9202                         const gchar    *stock_id,
9203                         GtkIconSize     size,
9204                         const gchar    *detail)
9205 {
9206   return gtk_widget_render_icon_pixbuf (widget, stock_id, size);
9207 }
9208
9209 /**
9210  * gtk_widget_set_parent_window:
9211  * @widget: a #GtkWidget.
9212  * @parent_window: the new parent window.
9213  *
9214  * Sets a non default parent window for @widget.
9215  *
9216  * For GtkWindow classes, setting a @parent_window effects whether
9217  * the window is a toplevel window or can be embedded into other
9218  * widgets.
9219  *
9220  * <note><para>
9221  * For GtkWindow classes, this needs to be called before the
9222  * window is realized.
9223  * </para></note>
9224  * 
9225  **/
9226 void
9227 gtk_widget_set_parent_window   (GtkWidget           *widget,
9228                                 GdkWindow           *parent_window)
9229 {
9230   GdkWindow *old_parent_window;
9231
9232   g_return_if_fail (GTK_IS_WIDGET (widget));
9233
9234   old_parent_window = g_object_get_qdata (G_OBJECT (widget),
9235                                           quark_parent_window);
9236
9237   if (parent_window != old_parent_window)
9238     {
9239       gboolean is_plug;
9240
9241       g_object_set_qdata (G_OBJECT (widget), quark_parent_window,
9242                           parent_window);
9243       if (old_parent_window)
9244         g_object_unref (old_parent_window);
9245       if (parent_window)
9246         g_object_ref (parent_window);
9247
9248       /* Unset toplevel flag when adding a parent window to a widget,
9249        * this is the primary entry point to allow toplevels to be
9250        * embeddable.
9251        */
9252 #ifdef GDK_WINDOWING_X11
9253       is_plug = GTK_IS_PLUG (widget);
9254 #else
9255       is_plug = FALSE;
9256 #endif
9257       if (GTK_IS_WINDOW (widget) && !is_plug)
9258         _gtk_window_set_is_toplevel (GTK_WINDOW (widget), parent_window == NULL);
9259     }
9260 }
9261
9262 /**
9263  * gtk_widget_get_parent_window:
9264  * @widget: a #GtkWidget.
9265  *
9266  * Gets @widget's parent window.
9267  *
9268  * Returns: (transfer none): the parent window of @widget.
9269  **/
9270 GdkWindow *
9271 gtk_widget_get_parent_window (GtkWidget *widget)
9272 {
9273   GtkWidgetPrivate *priv;
9274   GdkWindow *parent_window;
9275
9276   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9277
9278   priv = widget->priv;
9279
9280   parent_window = g_object_get_qdata (G_OBJECT (widget), quark_parent_window);
9281
9282   return (parent_window != NULL) ? parent_window :
9283          (priv->parent != NULL) ? priv->parent->priv->window : NULL;
9284 }
9285
9286
9287 /**
9288  * gtk_widget_set_child_visible:
9289  * @widget: a #GtkWidget
9290  * @is_visible: if %TRUE, @widget should be mapped along with its parent.
9291  *
9292  * Sets whether @widget should be mapped along with its when its parent
9293  * is mapped and @widget has been shown with gtk_widget_show().
9294  *
9295  * The child visibility can be set for widget before it is added to
9296  * a container with gtk_widget_set_parent(), to avoid mapping
9297  * children unnecessary before immediately unmapping them. However
9298  * it will be reset to its default state of %TRUE when the widget
9299  * is removed from a container.
9300  *
9301  * Note that changing the child visibility of a widget does not
9302  * queue a resize on the widget. Most of the time, the size of
9303  * a widget is computed from all visible children, whether or
9304  * not they are mapped. If this is not the case, the container
9305  * can queue a resize itself.
9306  *
9307  * This function is only useful for container implementations and
9308  * never should be called by an application.
9309  **/
9310 void
9311 gtk_widget_set_child_visible (GtkWidget *widget,
9312                               gboolean   is_visible)
9313 {
9314   GtkWidgetPrivate *priv;
9315
9316   g_return_if_fail (GTK_IS_WIDGET (widget));
9317   g_return_if_fail (!gtk_widget_is_toplevel (widget));
9318
9319   priv = widget->priv;
9320
9321   g_object_ref (widget);
9322   gtk_widget_verify_invariants (widget);
9323
9324   if (is_visible)
9325     priv->child_visible = TRUE;
9326   else
9327     {
9328       GtkWidget *toplevel;
9329
9330       priv->child_visible = FALSE;
9331
9332       toplevel = gtk_widget_get_toplevel (widget);
9333       if (toplevel != widget && gtk_widget_is_toplevel (toplevel))
9334         _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
9335     }
9336
9337   if (priv->parent && gtk_widget_get_realized (priv->parent))
9338     {
9339       if (gtk_widget_get_mapped (priv->parent) &&
9340           priv->child_visible &&
9341           gtk_widget_get_visible (widget))
9342         gtk_widget_map (widget);
9343       else
9344         gtk_widget_unmap (widget);
9345     }
9346
9347   gtk_widget_verify_invariants (widget);
9348   g_object_unref (widget);
9349 }
9350
9351 /**
9352  * gtk_widget_get_child_visible:
9353  * @widget: a #GtkWidget
9354  *
9355  * Gets the value set with gtk_widget_set_child_visible().
9356  * If you feel a need to use this function, your code probably
9357  * needs reorganization.
9358  *
9359  * This function is only useful for container implementations and
9360  * never should be called by an application.
9361  *
9362  * Return value: %TRUE if the widget is mapped with the parent.
9363  **/
9364 gboolean
9365 gtk_widget_get_child_visible (GtkWidget *widget)
9366 {
9367   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9368
9369   return widget->priv->child_visible;
9370 }
9371
9372 static GdkScreen *
9373 gtk_widget_get_screen_unchecked (GtkWidget *widget)
9374 {
9375   GtkWidget *toplevel;
9376
9377   toplevel = gtk_widget_get_toplevel (widget);
9378
9379   if (gtk_widget_is_toplevel (toplevel))
9380     {
9381       if (GTK_IS_WINDOW (toplevel))
9382         return gtk_window_get_screen (GTK_WINDOW (toplevel));
9383       else if (GTK_IS_INVISIBLE (toplevel))
9384         return gtk_invisible_get_screen (GTK_INVISIBLE (widget));
9385     }
9386
9387   return NULL;
9388 }
9389
9390 /**
9391  * gtk_widget_get_screen:
9392  * @widget: a #GtkWidget
9393  *
9394  * Get the #GdkScreen from the toplevel window associated with
9395  * this widget. This function can only be called after the widget
9396  * has been added to a widget hierarchy with a #GtkWindow
9397  * at the top.
9398  *
9399  * In general, you should only create screen specific
9400  * resources when a widget has been realized, and you should
9401  * free those resources when the widget is unrealized.
9402  *
9403  * Return value: (transfer none): the #GdkScreen for the toplevel for this widget.
9404  *
9405  * Since: 2.2
9406  **/
9407 GdkScreen*
9408 gtk_widget_get_screen (GtkWidget *widget)
9409 {
9410   GdkScreen *screen;
9411
9412   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9413
9414   screen = gtk_widget_get_screen_unchecked (widget);
9415
9416   if (screen)
9417     return screen;
9418   else
9419     {
9420 #if 0
9421       g_warning (G_STRLOC ": Can't get associated screen"
9422                  " for a widget unless it is inside a toplevel GtkWindow\n"
9423                  " widget type is %s associated top level type is %s",
9424                  g_type_name (G_OBJECT_TYPE(G_OBJECT (widget))),
9425                  g_type_name (G_OBJECT_TYPE(G_OBJECT (toplevel))));
9426 #endif
9427       return gdk_screen_get_default ();
9428     }
9429 }
9430
9431 /**
9432  * gtk_widget_has_screen:
9433  * @widget: a #GtkWidget
9434  *
9435  * Checks whether there is a #GdkScreen is associated with
9436  * this widget. All toplevel widgets have an associated
9437  * screen, and all widgets added into a hierarchy with a toplevel
9438  * window at the top.
9439  *
9440  * Return value: %TRUE if there is a #GdkScreen associcated
9441  *   with the widget.
9442  *
9443  * Since: 2.2
9444  **/
9445 gboolean
9446 gtk_widget_has_screen (GtkWidget *widget)
9447 {
9448   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9449
9450   return (gtk_widget_get_screen_unchecked (widget) != NULL);
9451 }
9452
9453 /**
9454  * gtk_widget_get_display:
9455  * @widget: a #GtkWidget
9456  *
9457  * Get the #GdkDisplay for the toplevel window associated with
9458  * this widget. This function can only be called after the widget
9459  * has been added to a widget hierarchy with a #GtkWindow at the top.
9460  *
9461  * In general, you should only create display specific
9462  * resources when a widget has been realized, and you should
9463  * free those resources when the widget is unrealized.
9464  *
9465  * Return value: (transfer none): the #GdkDisplay for the toplevel for this widget.
9466  *
9467  * Since: 2.2
9468  **/
9469 GdkDisplay*
9470 gtk_widget_get_display (GtkWidget *widget)
9471 {
9472   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9473
9474   return gdk_screen_get_display (gtk_widget_get_screen (widget));
9475 }
9476
9477 /**
9478  * gtk_widget_get_root_window:
9479  * @widget: a #GtkWidget
9480  *
9481  * Get the root window where this widget is located. This function can
9482  * only be called after the widget has been added to a widget
9483  * hierarchy with #GtkWindow at the top.
9484  *
9485  * The root window is useful for such purposes as creating a popup
9486  * #GdkWindow associated with the window. In general, you should only
9487  * create display specific resources when a widget has been realized,
9488  * and you should free those resources when the widget is unrealized.
9489  *
9490  * Return value: (transfer none): the #GdkWindow root window for the toplevel for this widget.
9491  *
9492  * Since: 2.2
9493  **/
9494 GdkWindow*
9495 gtk_widget_get_root_window (GtkWidget *widget)
9496 {
9497   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9498
9499   return gdk_screen_get_root_window (gtk_widget_get_screen (widget));
9500 }
9501
9502 /**
9503  * gtk_widget_child_focus:
9504  * @widget: a #GtkWidget
9505  * @direction: direction of focus movement
9506  *
9507  * This function is used by custom widget implementations; if you're
9508  * writing an app, you'd use gtk_widget_grab_focus() to move the focus
9509  * to a particular widget, and gtk_container_set_focus_chain() to
9510  * change the focus tab order. So you may want to investigate those
9511  * functions instead.
9512  *
9513  * gtk_widget_child_focus() is called by containers as the user moves
9514  * around the window using keyboard shortcuts. @direction indicates
9515  * what kind of motion is taking place (up, down, left, right, tab
9516  * forward, tab backward). gtk_widget_child_focus() emits the
9517  * #GtkWidget::focus signal; widgets override the default handler
9518  * for this signal in order to implement appropriate focus behavior.
9519  *
9520  * The default ::focus handler for a widget should return %TRUE if
9521  * moving in @direction left the focus on a focusable location inside
9522  * that widget, and %FALSE if moving in @direction moved the focus
9523  * outside the widget. If returning %TRUE, widgets normally
9524  * call gtk_widget_grab_focus() to place the focus accordingly;
9525  * if returning %FALSE, they don't modify the current focus location.
9526  *
9527  * Return value: %TRUE if focus ended up inside @widget
9528  **/
9529 gboolean
9530 gtk_widget_child_focus (GtkWidget       *widget,
9531                         GtkDirectionType direction)
9532 {
9533   gboolean return_val;
9534
9535   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9536
9537   if (!gtk_widget_get_visible (widget) ||
9538       !gtk_widget_is_sensitive (widget))
9539     return FALSE;
9540
9541   /* child widgets must set CAN_FOCUS, containers
9542    * don't have to though.
9543    */
9544   if (!GTK_IS_CONTAINER (widget) &&
9545       !gtk_widget_get_can_focus (widget))
9546     return FALSE;
9547
9548   g_signal_emit (widget,
9549                  widget_signals[FOCUS],
9550                  0,
9551                  direction, &return_val);
9552
9553   return return_val;
9554 }
9555
9556 /**
9557  * gtk_widget_keynav_failed:
9558  * @widget: a #GtkWidget
9559  * @direction: direction of focus movement
9560  *
9561  * This function should be called whenever keyboard navigation within
9562  * a single widget hits a boundary. The function emits the
9563  * #GtkWidget::keynav-failed signal on the widget and its return
9564  * value should be interpreted in a way similar to the return value of
9565  * gtk_widget_child_focus():
9566  *
9567  * When %TRUE is returned, stay in the widget, the failed keyboard
9568  * navigation is Ok and/or there is nowhere we can/should move the
9569  * focus to.
9570  *
9571  * When %FALSE is returned, the caller should continue with keyboard
9572  * navigation outside the widget, e.g. by calling
9573  * gtk_widget_child_focus() on the widget's toplevel.
9574  *
9575  * The default ::keynav-failed handler returns %TRUE for
9576  * %GTK_DIR_TAB_FORWARD and %GTK_DIR_TAB_BACKWARD. For the other
9577  * values of #GtkDirectionType, it looks at the
9578  * #GtkSettings:gtk-keynav-cursor-only setting and returns %FALSE
9579  * if the setting is %TRUE. This way the entire user interface
9580  * becomes cursor-navigatable on input devices such as mobile phones
9581  * which only have cursor keys but no tab key.
9582  *
9583  * Whenever the default handler returns %TRUE, it also calls
9584  * gtk_widget_error_bell() to notify the user of the failed keyboard
9585  * navigation.
9586  *
9587  * A use case for providing an own implementation of ::keynav-failed
9588  * (either by connecting to it or by overriding it) would be a row of
9589  * #GtkEntry widgets where the user should be able to navigate the
9590  * entire row with the cursor keys, as e.g. known from user interfaces
9591  * that require entering license keys.
9592  *
9593  * Return value: %TRUE if stopping keyboard navigation is fine, %FALSE
9594  *               if the emitting widget should try to handle the keyboard
9595  *               navigation attempt in its parent container(s).
9596  *
9597  * Since: 2.12
9598  **/
9599 gboolean
9600 gtk_widget_keynav_failed (GtkWidget        *widget,
9601                           GtkDirectionType  direction)
9602 {
9603   gboolean return_val;
9604
9605   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9606
9607   g_signal_emit (widget, widget_signals[KEYNAV_FAILED], 0,
9608                  direction, &return_val);
9609
9610   return return_val;
9611 }
9612
9613 /**
9614  * gtk_widget_error_bell:
9615  * @widget: a #GtkWidget
9616  *
9617  * Notifies the user about an input-related error on this widget.
9618  * If the #GtkSettings:gtk-error-bell setting is %TRUE, it calls
9619  * gdk_window_beep(), otherwise it does nothing.
9620  *
9621  * Note that the effect of gdk_window_beep() can be configured in many
9622  * ways, depending on the windowing backend and the desktop environment
9623  * or window manager that is used.
9624  *
9625  * Since: 2.12
9626  **/
9627 void
9628 gtk_widget_error_bell (GtkWidget *widget)
9629 {
9630   GtkWidgetPrivate *priv;
9631   GtkSettings* settings;
9632   gboolean beep;
9633
9634   g_return_if_fail (GTK_IS_WIDGET (widget));
9635
9636   priv = widget->priv;
9637
9638   settings = gtk_widget_get_settings (widget);
9639   if (!settings)
9640     return;
9641
9642   g_object_get (settings,
9643                 "gtk-error-bell", &beep,
9644                 NULL);
9645
9646   if (beep && priv->window)
9647     gdk_window_beep (priv->window);
9648 }
9649
9650 static void
9651 gtk_widget_set_usize_internal (GtkWidget          *widget,
9652                                gint                width,
9653                                gint                height,
9654                                GtkQueueResizeFlags flags)
9655 {
9656   GtkWidgetAuxInfo *aux_info;
9657   gboolean changed = FALSE;
9658
9659   g_object_freeze_notify (G_OBJECT (widget));
9660
9661   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
9662
9663   if (width > -2 && aux_info->width != width)
9664     {
9665       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
9666         g_object_notify (G_OBJECT (widget), "width-request");
9667       aux_info->width = width;
9668       changed = TRUE;
9669     }
9670   if (height > -2 && aux_info->height != height)
9671     {
9672       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
9673         g_object_notify (G_OBJECT (widget), "height-request");
9674       aux_info->height = height;
9675       changed = TRUE;
9676     }
9677
9678   if (gtk_widget_get_visible (widget) && changed)
9679     {
9680       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
9681         gtk_widget_queue_resize (widget);
9682       else
9683         _gtk_size_group_queue_resize (widget, GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
9684     }
9685
9686   g_object_thaw_notify (G_OBJECT (widget));
9687 }
9688
9689 /**
9690  * gtk_widget_set_size_request:
9691  * @widget: a #GtkWidget
9692  * @width: width @widget should request, or -1 to unset
9693  * @height: height @widget should request, or -1 to unset
9694  *
9695  * Sets the minimum size of a widget; that is, the widget's size
9696  * request will be @width by @height. You can use this function to
9697  * force a widget to be either larger or smaller than it normally
9698  * would be.
9699  *
9700  * In most cases, gtk_window_set_default_size() is a better choice for
9701  * toplevel windows than this function; setting the default size will
9702  * still allow users to shrink the window. Setting the size request
9703  * will force them to leave the window at least as large as the size
9704  * request. When dealing with window sizes,
9705  * gtk_window_set_geometry_hints() can be a useful function as well.
9706  *
9707  * Note the inherent danger of setting any fixed size - themes,
9708  * translations into other languages, different fonts, and user action
9709  * can all change the appropriate size for a given widget. So, it's
9710  * basically impossible to hardcode a size that will always be
9711  * correct.
9712  *
9713  * The size request of a widget is the smallest size a widget can
9714  * accept while still functioning well and drawing itself correctly.
9715  * However in some strange cases a widget may be allocated less than
9716  * its requested size, and in many cases a widget may be allocated more
9717  * space than it requested.
9718  *
9719  * If the size request in a given direction is -1 (unset), then
9720  * the "natural" size request of the widget will be used instead.
9721  *
9722  * Widgets can't actually be allocated a size less than 1 by 1, but
9723  * you can pass 0,0 to this function to mean "as small as possible."
9724  *
9725  * The size request set here does not include any margin from the
9726  * #GtkWidget properties margin-left, margin-right, margin-top, and
9727  * margin-bottom, but it does include pretty much all other padding
9728  * or border properties set by any subclass of #GtkWidget.
9729  **/
9730 void
9731 gtk_widget_set_size_request (GtkWidget *widget,
9732                              gint       width,
9733                              gint       height)
9734 {
9735   g_return_if_fail (GTK_IS_WIDGET (widget));
9736   g_return_if_fail (width >= -1);
9737   g_return_if_fail (height >= -1);
9738
9739   if (width == 0)
9740     width = 1;
9741   if (height == 0)
9742     height = 1;
9743
9744   gtk_widget_set_usize_internal (widget, width, height, 0);
9745 }
9746
9747
9748 /**
9749  * gtk_widget_get_size_request:
9750  * @widget: a #GtkWidget
9751  * @width: (out) (allow-none): return location for width, or %NULL
9752  * @height: (out) (allow-none): return location for height, or %NULL
9753  *
9754  * Gets the size request that was explicitly set for the widget using
9755  * gtk_widget_set_size_request(). A value of -1 stored in @width or
9756  * @height indicates that that dimension has not been set explicitly
9757  * and the natural requisition of the widget will be used intead. See
9758  * gtk_widget_set_size_request(). To get the size a widget will
9759  * actually request, call gtk_widget_get_preferred_size() instead of
9760  * this function.
9761  **/
9762 void
9763 gtk_widget_get_size_request (GtkWidget *widget,
9764                              gint      *width,
9765                              gint      *height)
9766 {
9767   const GtkWidgetAuxInfo *aux_info;
9768
9769   g_return_if_fail (GTK_IS_WIDGET (widget));
9770
9771   aux_info = _gtk_widget_get_aux_info_or_defaults (widget);
9772
9773   if (width)
9774     *width = aux_info->width;
9775
9776   if (height)
9777     *height = aux_info->height;
9778 }
9779
9780 /**
9781  * _gtk_widget_override_size_request:
9782  * @widget: a #GtkWidget
9783  * @width: new forced minimum width
9784  * @height: new forced minimum height
9785  * @old_width: location to store previous forced minimum width
9786  * @old_width: location to store previous forced minumum height
9787  *
9788  * Temporarily establishes a forced minimum size for a widget; this
9789  * is used by GtkWindow when calculating the size to add to the
9790  * window's geometry widget. Cached sizes for the widget and its
9791  * parents are invalidated, so that subsequent calls to the size
9792  * negotiation machinery produce the overriden result, but the
9793  * widget is not queued for relayout or redraw. The old size must
9794  * be restored with _gtk_widget_restore_size_request() or things
9795  * will go screwy.
9796  */
9797 void
9798 _gtk_widget_override_size_request (GtkWidget *widget,
9799                                    int        width,
9800                                    int        height,
9801                                    int       *old_width,
9802                                    int       *old_height)
9803 {
9804   gtk_widget_get_size_request (widget, old_width, old_height);
9805   gtk_widget_set_usize_internal (widget, width, height,
9806                                  GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
9807 }
9808
9809 /**
9810  * _gtk_widget_restore_size_request:
9811  * @widget: a #GtkWidget
9812  * @old_width: saved forced minimum size
9813  * @old_height: saved forced minimum size
9814  *
9815  * Undoes the operation of_gtk_widget_override_size_request().
9816  */
9817 void
9818 _gtk_widget_restore_size_request (GtkWidget *widget,
9819                                   int        old_width,
9820                                   int        old_height)
9821 {
9822   gtk_widget_set_usize_internal (widget, old_width, old_height,
9823                                  GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
9824 }
9825
9826 /**
9827  * gtk_widget_set_events:
9828  * @widget: a #GtkWidget
9829  * @events: event mask
9830  *
9831  * Sets the event mask (see #GdkEventMask) for a widget. The event
9832  * mask determines which events a widget will receive. Keep in mind
9833  * that different widgets have different default event masks, and by
9834  * changing the event mask you may disrupt a widget's functionality,
9835  * so be careful. This function must be called while a widget is
9836  * unrealized. Consider gtk_widget_add_events() for widgets that are
9837  * already realized, or if you want to preserve the existing event
9838  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
9839  * to get events on those widgets, place them inside a #GtkEventBox
9840  * and receive events on the event box.
9841  **/
9842 void
9843 gtk_widget_set_events (GtkWidget *widget,
9844                        gint       events)
9845 {
9846   g_return_if_fail (GTK_IS_WIDGET (widget));
9847   g_return_if_fail (!gtk_widget_get_realized (widget));
9848
9849   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
9850                       GINT_TO_POINTER (events));
9851   g_object_notify (G_OBJECT (widget), "events");
9852 }
9853
9854 /**
9855  * gtk_widget_set_device_events:
9856  * @widget: a #GtkWidget
9857  * @device: a #GdkDevice
9858  * @events: event mask
9859  *
9860  * Sets the device event mask (see #GdkEventMask) for a widget. The event
9861  * mask determines which events a widget will receive from @device. Keep
9862  * in mind that different widgets have different default event masks, and by
9863  * changing the event mask you may disrupt a widget's functionality,
9864  * so be careful. This function must be called while a widget is
9865  * unrealized. Consider gtk_widget_add_device_events() for widgets that are
9866  * already realized, or if you want to preserve the existing event
9867  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
9868  * to get events on those widgets, place them inside a #GtkEventBox
9869  * and receive events on the event box.
9870  *
9871  * Since: 3.0
9872  **/
9873 void
9874 gtk_widget_set_device_events (GtkWidget    *widget,
9875                               GdkDevice    *device,
9876                               GdkEventMask  events)
9877 {
9878   GHashTable *device_events;
9879
9880   g_return_if_fail (GTK_IS_WIDGET (widget));
9881   g_return_if_fail (GDK_IS_DEVICE (device));
9882   g_return_if_fail (!gtk_widget_get_realized (widget));
9883
9884   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
9885
9886   if (G_UNLIKELY (!device_events))
9887     {
9888       device_events = g_hash_table_new (NULL, NULL);
9889       g_object_set_qdata_full (G_OBJECT (widget), quark_device_event_mask, device_events,
9890                                (GDestroyNotify) g_hash_table_unref);
9891     }
9892
9893   g_hash_table_insert (device_events, device, GUINT_TO_POINTER (events));
9894 }
9895
9896 /**
9897  * gtk_widget_set_device_enabled:
9898  * @widget: a #GtkWidget
9899  * @device: a #GdkDevice
9900  * @enabled: whether to enable the device
9901  *
9902  * Enables or disables a #GdkDevice to interact with @widget
9903  * and all its children.
9904  *
9905  * It does so by descending through the #GdkWindow hierarchy
9906  * and enabling the same mask that is has for core events
9907  * (i.e. the one that gdk_window_get_events() returns).
9908  *
9909  * Since: 3.0
9910  */
9911 void
9912 gtk_widget_set_device_enabled (GtkWidget *widget,
9913                                GdkDevice *device,
9914                                gboolean   enabled)
9915 {
9916   GList *enabled_devices;
9917
9918   g_return_if_fail (GTK_IS_WIDGET (widget));
9919   g_return_if_fail (GDK_IS_DEVICE (device));
9920
9921   enabled_devices = g_object_get_qdata (G_OBJECT (widget), quark_enabled_devices);
9922   enabled_devices = g_list_append (enabled_devices, device);
9923
9924   g_object_set_qdata_full (G_OBJECT (widget), quark_enabled_devices,
9925                            enabled_devices, (GDestroyNotify) g_list_free);;
9926
9927   if (gtk_widget_get_realized (widget))
9928     gtk_widget_set_device_enabled_internal (widget, device, TRUE, enabled);
9929 }
9930
9931 /**
9932  * gtk_widget_get_device_enabled:
9933  * @widget: a #GtkWidget
9934  * @device: a #GdkDevice
9935  *
9936  * Returns whether @device can interact with @widget and its
9937  * children. See gtk_widget_set_device_enabled().
9938  *
9939  * Return value: %TRUE is @device is enabled for @widget
9940  *
9941  * Since: 3.0
9942  */
9943 gboolean
9944 gtk_widget_get_device_enabled (GtkWidget *widget,
9945                                GdkDevice *device)
9946 {
9947   GList *enabled_devices;
9948
9949   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9950   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
9951
9952   enabled_devices = g_object_get_qdata (G_OBJECT (widget), quark_enabled_devices);
9953
9954   return g_list_find (enabled_devices, device) != NULL;
9955 }
9956
9957 static void
9958 gtk_widget_add_events_internal_list (GtkWidget *widget,
9959                                      GdkDevice *device,
9960                                      gint       events,
9961                                      GList     *window_list)
9962 {
9963   GList *l;
9964
9965   for (l = window_list; l != NULL; l = l->next)
9966     {
9967       GdkWindow *window = l->data;
9968       gpointer user_data;
9969
9970       gdk_window_get_user_data (window, &user_data);
9971       if (user_data == widget)
9972         {
9973           GList *children;
9974
9975           if (device)
9976             gdk_window_set_device_events (window, device, gdk_window_get_events (window) | events);
9977           else
9978             gdk_window_set_events (window, gdk_window_get_events (window) | events);
9979
9980           children = gdk_window_get_children (window);
9981           gtk_widget_add_events_internal_list (widget, device, events, children);
9982           g_list_free (children);
9983         }
9984     }
9985 }
9986
9987 static void
9988 gtk_widget_add_events_internal (GtkWidget *widget,
9989                                 GdkDevice *device,
9990                                 gint       events)
9991 {
9992   GtkWidgetPrivate *priv = widget->priv;
9993   GList *window_list;
9994
9995   if (!gtk_widget_get_has_window (widget))
9996     window_list = gdk_window_get_children (priv->window);
9997   else
9998     window_list = g_list_prepend (NULL, priv->window);
9999
10000   gtk_widget_add_events_internal_list (widget, device, events, window_list);
10001
10002   g_list_free (window_list);
10003 }
10004
10005 /**
10006  * gtk_widget_add_events:
10007  * @widget: a #GtkWidget
10008  * @events: an event mask, see #GdkEventMask
10009  *
10010  * Adds the events in the bitfield @events to the event mask for
10011  * @widget. See gtk_widget_set_events() for details.
10012  **/
10013 void
10014 gtk_widget_add_events (GtkWidget *widget,
10015                        gint       events)
10016 {
10017   gint old_events;
10018
10019   g_return_if_fail (GTK_IS_WIDGET (widget));
10020
10021   old_events = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
10022   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
10023                       GINT_TO_POINTER (old_events | events));
10024
10025   if (gtk_widget_get_realized (widget))
10026     {
10027       gtk_widget_add_events_internal (widget, NULL, events);
10028       gtk_widget_update_devices_mask (widget, FALSE);
10029     }
10030
10031   g_object_notify (G_OBJECT (widget), "events");
10032 }
10033
10034 /**
10035  * gtk_widget_add_device_events:
10036  * @widget: a #GtkWidget
10037  * @device: a #GdkDevice
10038  * @events: an event mask, see #GdkEventMask
10039  *
10040  * Adds the device events in the bitfield @events to the event mask for
10041  * @widget. See gtk_widget_set_device_events() for details.
10042  *
10043  * Since: 3.0
10044  **/
10045 void
10046 gtk_widget_add_device_events (GtkWidget    *widget,
10047                               GdkDevice    *device,
10048                               GdkEventMask  events)
10049 {
10050   GdkEventMask old_events;
10051   GHashTable *device_events;
10052
10053   g_return_if_fail (GTK_IS_WIDGET (widget));
10054   g_return_if_fail (GDK_IS_DEVICE (device));
10055
10056   old_events = gtk_widget_get_device_events (widget, device);
10057
10058   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
10059
10060   if (G_UNLIKELY (!device_events))
10061     {
10062       device_events = g_hash_table_new (NULL, NULL);
10063       g_object_set_qdata_full (G_OBJECT (widget), quark_device_event_mask, device_events,
10064                                (GDestroyNotify) g_hash_table_unref);
10065     }
10066
10067   g_hash_table_insert (device_events, device,
10068                        GUINT_TO_POINTER (old_events | events));
10069
10070   if (gtk_widget_get_realized (widget))
10071     gtk_widget_add_events_internal (widget, device, events);
10072
10073   g_object_notify (G_OBJECT (widget), "events");
10074 }
10075
10076 /**
10077  * gtk_widget_get_toplevel:
10078  * @widget: a #GtkWidget
10079  *
10080  * This function returns the topmost widget in the container hierarchy
10081  * @widget is a part of. If @widget has no parent widgets, it will be
10082  * returned as the topmost widget. No reference will be added to the
10083  * returned widget; it should not be unreferenced.
10084  *
10085  * Note the difference in behavior vs. gtk_widget_get_ancestor();
10086  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)</literal>
10087  * would return
10088  * %NULL if @widget wasn't inside a toplevel window, and if the
10089  * window was inside a #GtkWindow-derived widget which was in turn
10090  * inside the toplevel #GtkWindow. While the second case may
10091  * seem unlikely, it actually happens when a #GtkPlug is embedded
10092  * inside a #GtkSocket within the same application.
10093  *
10094  * To reliably find the toplevel #GtkWindow, use
10095  * gtk_widget_get_toplevel() and check if the %TOPLEVEL flags
10096  * is set on the result.
10097  * |[
10098  *  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
10099  *  if (gtk_widget_is_toplevel (toplevel))
10100  *    {
10101  *      /&ast; Perform action on toplevel. &ast;/
10102  *    }
10103  * ]|
10104  *
10105  * Return value: (transfer none): the topmost ancestor of @widget, or @widget itself
10106  *    if there's no ancestor.
10107  **/
10108 GtkWidget*
10109 gtk_widget_get_toplevel (GtkWidget *widget)
10110 {
10111   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10112
10113   while (widget->priv->parent)
10114     widget = widget->priv->parent;
10115
10116   return widget;
10117 }
10118
10119 /**
10120  * gtk_widget_get_ancestor:
10121  * @widget: a #GtkWidget
10122  * @widget_type: ancestor type
10123  *
10124  * Gets the first ancestor of @widget with type @widget_type. For example,
10125  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_BOX)</literal> gets
10126  * the first #GtkBox that's an ancestor of @widget. No reference will be
10127  * added to the returned widget; it should not be unreferenced. See note
10128  * about checking for a toplevel #GtkWindow in the docs for
10129  * gtk_widget_get_toplevel().
10130  *
10131  * Note that unlike gtk_widget_is_ancestor(), gtk_widget_get_ancestor()
10132  * considers @widget to be an ancestor of itself.
10133  *
10134  * Return value: (transfer none): the ancestor widget, or %NULL if not found
10135  **/
10136 GtkWidget*
10137 gtk_widget_get_ancestor (GtkWidget *widget,
10138                          GType      widget_type)
10139 {
10140   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10141
10142   while (widget && !g_type_is_a (G_OBJECT_TYPE (widget), widget_type))
10143     widget = widget->priv->parent;
10144
10145   if (!(widget && g_type_is_a (G_OBJECT_TYPE (widget), widget_type)))
10146     return NULL;
10147
10148   return widget;
10149 }
10150
10151 /**
10152  * gtk_widget_set_visual:
10153  * @widget: a #GtkWidget
10154  * @visual: visual to be used or %NULL to unset a previous one
10155  *
10156  * Sets the visual that should be used for by widget and its children for
10157  * creating #GdkWindows. The visual must be on the same #GdkScreen as
10158  * returned by gdk_widget_get_screen(), so handling the
10159  * #GtkWidget::screen-changed signal is necessary.
10160  *
10161  * Setting a new @visual will not cause @widget to recreate its windows,
10162  * so you should call this function before @widget is realized.
10163  **/
10164 void
10165 gtk_widget_set_visual (GtkWidget *widget,
10166                        GdkVisual *visual)
10167 {
10168   g_return_if_fail (GTK_IS_WIDGET (widget));
10169   g_return_if_fail (visual == NULL || GDK_IS_VISUAL (visual));
10170   if (visual)
10171     {
10172       g_return_if_fail (gtk_widget_get_screen (widget) == gdk_visual_get_screen (visual));
10173     }
10174
10175   g_object_set_qdata_full (G_OBJECT (widget),
10176                            quark_visual,
10177                            g_object_ref (visual),
10178                            g_object_unref);
10179 }
10180
10181 /**
10182  * gtk_widget_get_visual:
10183  * @widget: a #GtkWidget
10184  *
10185  * Gets the visual that will be used to render @widget.
10186  *
10187  * Return value: (transfer none): the visual for @widget
10188  **/
10189 GdkVisual*
10190 gtk_widget_get_visual (GtkWidget *widget)
10191 {
10192   GtkWidget *w;
10193   GdkVisual *visual;
10194   GdkScreen *screen;
10195
10196   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10197
10198   if (gtk_widget_get_has_window (widget) &&
10199       widget->priv->window)
10200     return gdk_window_get_visual (widget->priv->window);
10201
10202   screen = gtk_widget_get_screen (widget);
10203
10204   for (w = widget; w != NULL; w = w->priv->parent)
10205     {
10206       visual = g_object_get_qdata (G_OBJECT (w), quark_visual);
10207       if (visual)
10208         {
10209           if (gdk_visual_get_screen (visual) == screen)
10210             return visual;
10211
10212           g_warning ("Ignoring visual set on widget `%s' that is not on the correct screen.",
10213                      gtk_widget_get_name (widget));
10214         }
10215     }
10216
10217   return gdk_screen_get_system_visual (screen);
10218 }
10219
10220 /**
10221  * gtk_widget_get_settings:
10222  * @widget: a #GtkWidget
10223  *
10224  * Gets the settings object holding the settings used for this widget.
10225  *
10226  * Note that this function can only be called when the #GtkWidget
10227  * is attached to a toplevel, since the settings object is specific
10228  * to a particular #GdkScreen.
10229  *
10230  * Return value: (transfer none): the relevant #GtkSettings object
10231  */
10232 GtkSettings*
10233 gtk_widget_get_settings (GtkWidget *widget)
10234 {
10235   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10236
10237   return gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
10238 }
10239
10240 /**
10241  * gtk_widget_get_events:
10242  * @widget: a #GtkWidget
10243  *
10244  * Returns the event mask for the widget (a bitfield containing flags
10245  * from the #GdkEventMask enumeration). These are the events that the widget
10246  * will receive.
10247  *
10248  * Return value: event mask for @widget
10249  **/
10250 gint
10251 gtk_widget_get_events (GtkWidget *widget)
10252 {
10253   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
10254
10255   return GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
10256 }
10257
10258 /**
10259  * gtk_widget_get_device_events:
10260  * @widget: a #GtkWidget
10261  * @device: a #GdkDevice
10262  *
10263  * Returns the events mask for the widget corresponding to an specific device. These
10264  * are the events that the widget will receive when @device operates on it.
10265  *
10266  * Returns: device event mask for @widget
10267  *
10268  * Since: 3.0
10269  **/
10270 GdkEventMask
10271 gtk_widget_get_device_events (GtkWidget *widget,
10272                               GdkDevice *device)
10273 {
10274   GHashTable *device_events;
10275
10276   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
10277   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
10278
10279   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
10280
10281   if (!device_events)
10282     return 0;
10283
10284   return GPOINTER_TO_UINT (g_hash_table_lookup (device_events, device));
10285 }
10286
10287 /**
10288  * gtk_widget_get_pointer:
10289  * @widget: a #GtkWidget
10290  * @x: (out) (allow-none): return location for the X coordinate, or %NULL
10291  * @y: (out) (allow-none): return location for the Y coordinate, or %NULL
10292  *
10293  * Obtains the location of the mouse pointer in widget coordinates.
10294  * Widget coordinates are a bit odd; for historical reasons, they are
10295  * defined as @widget->window coordinates for widgets that are not
10296  * #GTK_NO_WINDOW widgets, and are relative to @widget->allocation.x,
10297  * @widget->allocation.y for widgets that are #GTK_NO_WINDOW widgets.
10298  **/
10299 void
10300 gtk_widget_get_pointer (GtkWidget *widget,
10301                         gint      *x,
10302                         gint      *y)
10303 {
10304   GtkWidgetPrivate *priv;
10305
10306   g_return_if_fail (GTK_IS_WIDGET (widget));
10307
10308   priv = widget->priv;
10309
10310   if (x)
10311     *x = -1;
10312   if (y)
10313     *y = -1;
10314
10315   if (gtk_widget_get_realized (widget))
10316     {
10317       gdk_window_get_pointer (priv->window, x, y, NULL);
10318
10319       if (!gtk_widget_get_has_window (widget))
10320         {
10321           if (x)
10322             *x -= priv->allocation.x;
10323           if (y)
10324             *y -= priv->allocation.y;
10325         }
10326     }
10327 }
10328
10329 /**
10330  * gtk_widget_is_ancestor:
10331  * @widget: a #GtkWidget
10332  * @ancestor: another #GtkWidget
10333  *
10334  * Determines whether @widget is somewhere inside @ancestor, possibly with
10335  * intermediate containers.
10336  *
10337  * Return value: %TRUE if @ancestor contains @widget as a child,
10338  *    grandchild, great grandchild, etc.
10339  **/
10340 gboolean
10341 gtk_widget_is_ancestor (GtkWidget *widget,
10342                         GtkWidget *ancestor)
10343 {
10344   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
10345   g_return_val_if_fail (ancestor != NULL, FALSE);
10346
10347   while (widget)
10348     {
10349       if (widget->priv->parent == ancestor)
10350         return TRUE;
10351       widget = widget->priv->parent;
10352     }
10353
10354   return FALSE;
10355 }
10356
10357 static GQuark quark_composite_name = 0;
10358
10359 /**
10360  * gtk_widget_set_composite_name:
10361  * @widget: a #GtkWidget.
10362  * @name: the name to set
10363  *
10364  * Sets a widgets composite name. The widget must be
10365  * a composite child of its parent; see gtk_widget_push_composite_child().
10366  **/
10367 void
10368 gtk_widget_set_composite_name (GtkWidget   *widget,
10369                                const gchar *name)
10370 {
10371   g_return_if_fail (GTK_IS_WIDGET (widget));
10372   g_return_if_fail (widget->priv->composite_child);
10373   g_return_if_fail (name != NULL);
10374
10375   if (!quark_composite_name)
10376     quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
10377
10378   g_object_set_qdata_full (G_OBJECT (widget),
10379                            quark_composite_name,
10380                            g_strdup (name),
10381                            g_free);
10382 }
10383
10384 /**
10385  * gtk_widget_get_composite_name:
10386  * @widget: a #GtkWidget
10387  *
10388  * Obtains the composite name of a widget.
10389  *
10390  * Returns: the composite name of @widget, or %NULL if @widget is not
10391  *   a composite child. The string should be freed when it is no
10392  *   longer needed.
10393  **/
10394 gchar*
10395 gtk_widget_get_composite_name (GtkWidget *widget)
10396 {
10397   GtkWidgetPrivate *priv;
10398
10399   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10400
10401   priv = widget->priv;
10402
10403   if (widget->priv->composite_child && priv->parent)
10404     return _gtk_container_child_composite_name (GTK_CONTAINER (priv->parent),
10405                                                widget);
10406   else
10407     return NULL;
10408 }
10409
10410 /**
10411  * gtk_widget_push_composite_child:
10412  *
10413  * Makes all newly-created widgets as composite children until
10414  * the corresponding gtk_widget_pop_composite_child() call.
10415  *
10416  * A composite child is a child that's an implementation detail of the
10417  * container it's inside and should not be visible to people using the
10418  * container. Composite children aren't treated differently by GTK (but
10419  * see gtk_container_foreach() vs. gtk_container_forall()), but e.g. GUI
10420  * builders might want to treat them in a different way.
10421  *
10422  * Here is a simple example:
10423  * |[
10424  *   gtk_widget_push_composite_child ();
10425  *   scrolled_window->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
10426  *   gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
10427  *   gtk_widget_pop_composite_child ();
10428  *   gtk_widget_set_parent (scrolled_window->hscrollbar,
10429  *                          GTK_WIDGET (scrolled_window));
10430  *   g_object_ref (scrolled_window->hscrollbar);
10431  * ]|
10432  **/
10433 void
10434 gtk_widget_push_composite_child (void)
10435 {
10436   composite_child_stack++;
10437 }
10438
10439 /**
10440  * gtk_widget_pop_composite_child:
10441  *
10442  * Cancels the effect of a previous call to gtk_widget_push_composite_child().
10443  **/
10444 void
10445 gtk_widget_pop_composite_child (void)
10446 {
10447   if (composite_child_stack)
10448     composite_child_stack--;
10449 }
10450
10451 static void
10452 gtk_widget_emit_direction_changed (GtkWidget        *widget,
10453                                    GtkTextDirection  old_dir)
10454 {
10455   gtk_widget_update_pango_context (widget);
10456
10457   if (widget->priv->context)
10458     gtk_style_context_set_direction (widget->priv->context,
10459                                      gtk_widget_get_direction (widget));
10460
10461   g_signal_emit (widget, widget_signals[DIRECTION_CHANGED], 0, old_dir);
10462 }
10463
10464 /**
10465  * gtk_widget_set_direction:
10466  * @widget: a #GtkWidget
10467  * @dir:    the new direction
10468  *
10469  * Sets the reading direction on a particular widget. This direction
10470  * controls the primary direction for widgets containing text,
10471  * and also the direction in which the children of a container are
10472  * packed. The ability to set the direction is present in order
10473  * so that correct localization into languages with right-to-left
10474  * reading directions can be done. Generally, applications will
10475  * let the default reading direction present, except for containers
10476  * where the containers are arranged in an order that is explicitely
10477  * visual rather than logical (such as buttons for text justification).
10478  *
10479  * If the direction is set to %GTK_TEXT_DIR_NONE, then the value
10480  * set by gtk_widget_set_default_direction() will be used.
10481  **/
10482 void
10483 gtk_widget_set_direction (GtkWidget        *widget,
10484                           GtkTextDirection  dir)
10485 {
10486   GtkTextDirection old_dir;
10487
10488   g_return_if_fail (GTK_IS_WIDGET (widget));
10489   g_return_if_fail (dir >= GTK_TEXT_DIR_NONE && dir <= GTK_TEXT_DIR_RTL);
10490
10491   old_dir = gtk_widget_get_direction (widget);
10492
10493   widget->priv->direction = dir;
10494
10495   if (old_dir != gtk_widget_get_direction (widget))
10496     gtk_widget_emit_direction_changed (widget, old_dir);
10497 }
10498
10499 /**
10500  * gtk_widget_get_direction:
10501  * @widget: a #GtkWidget
10502  *
10503  * Gets the reading direction for a particular widget. See
10504  * gtk_widget_set_direction().
10505  *
10506  * Return value: the reading direction for the widget.
10507  **/
10508 GtkTextDirection
10509 gtk_widget_get_direction (GtkWidget *widget)
10510 {
10511   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_TEXT_DIR_LTR);
10512
10513   if (widget->priv->direction == GTK_TEXT_DIR_NONE)
10514     return gtk_default_direction;
10515   else
10516     return widget->priv->direction;
10517 }
10518
10519 static void
10520 gtk_widget_set_default_direction_recurse (GtkWidget *widget, gpointer data)
10521 {
10522   GtkTextDirection old_dir = GPOINTER_TO_UINT (data);
10523
10524   g_object_ref (widget);
10525
10526   if (widget->priv->direction == GTK_TEXT_DIR_NONE)
10527     gtk_widget_emit_direction_changed (widget, old_dir);
10528
10529   if (GTK_IS_CONTAINER (widget))
10530     gtk_container_forall (GTK_CONTAINER (widget),
10531                           gtk_widget_set_default_direction_recurse,
10532                           data);
10533
10534   g_object_unref (widget);
10535 }
10536
10537 /**
10538  * gtk_widget_set_default_direction:
10539  * @dir: the new default direction. This cannot be
10540  *        %GTK_TEXT_DIR_NONE.
10541  *
10542  * Sets the default reading direction for widgets where the
10543  * direction has not been explicitly set by gtk_widget_set_direction().
10544  **/
10545 void
10546 gtk_widget_set_default_direction (GtkTextDirection dir)
10547 {
10548   g_return_if_fail (dir == GTK_TEXT_DIR_RTL || dir == GTK_TEXT_DIR_LTR);
10549
10550   if (dir != gtk_default_direction)
10551     {
10552       GList *toplevels, *tmp_list;
10553       GtkTextDirection old_dir = gtk_default_direction;
10554
10555       gtk_default_direction = dir;
10556
10557       tmp_list = toplevels = gtk_window_list_toplevels ();
10558       g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
10559
10560       while (tmp_list)
10561         {
10562           gtk_widget_set_default_direction_recurse (tmp_list->data,
10563                                                     GUINT_TO_POINTER (old_dir));
10564           g_object_unref (tmp_list->data);
10565           tmp_list = tmp_list->next;
10566         }
10567
10568       g_list_free (toplevels);
10569     }
10570 }
10571
10572 /**
10573  * gtk_widget_get_default_direction:
10574  *
10575  * Obtains the current default reading direction. See
10576  * gtk_widget_set_default_direction().
10577  *
10578  * Return value: the current default direction.
10579  **/
10580 GtkTextDirection
10581 gtk_widget_get_default_direction (void)
10582 {
10583   return gtk_default_direction;
10584 }
10585
10586 static void
10587 gtk_widget_dispose (GObject *object)
10588 {
10589   GtkWidget *widget = GTK_WIDGET (object);
10590   GtkWidgetPrivate *priv = widget->priv;
10591
10592   if (priv->parent)
10593     gtk_container_remove (GTK_CONTAINER (priv->parent), widget);
10594   else if (gtk_widget_get_visible (widget))
10595     gtk_widget_hide (widget);
10596
10597   priv->visible = FALSE;
10598   if (gtk_widget_get_realized (widget))
10599     gtk_widget_unrealize (widget);
10600
10601   if (!priv->in_destruction)
10602     {
10603       priv->in_destruction = TRUE;
10604       g_signal_emit (object, widget_signals[DESTROY], 0);
10605       priv->in_destruction = FALSE;
10606     }
10607
10608   G_OBJECT_CLASS (gtk_widget_parent_class)->dispose (object);
10609 }
10610
10611 static void
10612 gtk_widget_real_destroy (GtkWidget *object)
10613 {
10614   /* gtk_object_destroy() will already hold a refcount on object */
10615   GtkWidget *widget = GTK_WIDGET (object);
10616   GtkWidgetPrivate *priv = widget->priv;
10617
10618   /* wipe accelerator closures (keep order) */
10619   g_object_set_qdata (G_OBJECT (widget), quark_accel_path, NULL);
10620   g_object_set_qdata (G_OBJECT (widget), quark_accel_closures, NULL);
10621
10622   /* Callers of add_mnemonic_label() should disconnect on ::destroy */
10623   g_object_set_qdata (G_OBJECT (widget), quark_mnemonic_labels, NULL);
10624
10625   gtk_grab_remove (widget);
10626
10627   g_object_unref (priv->style);
10628   priv->style = gtk_widget_get_default_style ();
10629   g_object_ref (priv->style);
10630 }
10631
10632 static void
10633 gtk_widget_finalize (GObject *object)
10634 {
10635   GtkWidget *widget = GTK_WIDGET (object);
10636   GtkWidgetPrivate *priv = widget->priv;
10637   GtkWidgetAuxInfo *aux_info;
10638   GtkAccessible *accessible;
10639
10640   gtk_grab_remove (widget);
10641
10642   g_object_unref (priv->style);
10643   priv->style = NULL;
10644
10645   g_free (priv->name);
10646
10647   aux_info =_gtk_widget_get_aux_info (widget, FALSE);
10648   if (aux_info)
10649     gtk_widget_aux_info_destroy (aux_info);
10650
10651   accessible = g_object_get_qdata (G_OBJECT (widget), quark_accessible_object);
10652   if (accessible)
10653     g_object_unref (accessible);
10654
10655   if (priv->path)
10656     gtk_widget_path_free (priv->path);
10657
10658   if (priv->context)
10659     g_object_unref (priv->context);
10660
10661   _gtk_widget_free_cached_sizes (widget);
10662
10663   if (g_object_is_floating (object))
10664     g_warning ("A floating object was finalized. This means that someone\n"
10665                "called g_object_unref() on an object that had only a floating\n"
10666                "reference; the initial floating reference is not owned by anyone\n"
10667                "and must be removed with g_object_ref_sink().");
10668
10669   G_OBJECT_CLASS (gtk_widget_parent_class)->finalize (object);
10670 }
10671
10672 /*****************************************
10673  * gtk_widget_real_map:
10674  *
10675  *   arguments:
10676  *
10677  *   results:
10678  *****************************************/
10679
10680 static void
10681 gtk_widget_real_map (GtkWidget *widget)
10682 {
10683   GtkWidgetPrivate *priv = widget->priv;
10684
10685   g_assert (gtk_widget_get_realized (widget));
10686
10687   if (!gtk_widget_get_mapped (widget))
10688     {
10689       gtk_widget_set_mapped (widget, TRUE);
10690
10691       if (gtk_widget_get_has_window (widget))
10692         gdk_window_show (priv->window);
10693     }
10694 }
10695
10696 /*****************************************
10697  * gtk_widget_real_unmap:
10698  *
10699  *   arguments:
10700  *
10701  *   results:
10702  *****************************************/
10703
10704 static void
10705 gtk_widget_real_unmap (GtkWidget *widget)
10706 {
10707   GtkWidgetPrivate *priv = widget->priv;
10708
10709   if (gtk_widget_get_mapped (widget))
10710     {
10711       gtk_widget_set_mapped (widget, FALSE);
10712
10713       if (gtk_widget_get_has_window (widget))
10714         gdk_window_hide (priv->window);
10715     }
10716 }
10717
10718 /*****************************************
10719  * gtk_widget_real_realize:
10720  *
10721  *   arguments:
10722  *
10723  *   results:
10724  *****************************************/
10725
10726 static void
10727 gtk_widget_real_realize (GtkWidget *widget)
10728 {
10729   GtkWidgetPrivate *priv = widget->priv;
10730
10731   g_assert (!gtk_widget_get_has_window (widget));
10732
10733   gtk_widget_set_realized (widget, TRUE);
10734   if (priv->parent)
10735     {
10736       priv->window = gtk_widget_get_parent_window (widget);
10737       g_object_ref (priv->window);
10738     }
10739   priv->style = gtk_style_attach (priv->style, priv->window);
10740 }
10741
10742 /*****************************************
10743  * gtk_widget_real_unrealize:
10744  *
10745  *   arguments:
10746  *
10747  *   results:
10748  *****************************************/
10749
10750 static void
10751 gtk_widget_real_unrealize (GtkWidget *widget)
10752 {
10753   GtkWidgetPrivate *priv = widget->priv;
10754
10755   g_assert (!widget->priv->mapped);
10756
10757   /* printf ("unrealizing %s\n", g_type_name (G_TYPE_FROM_INSTANCE (widget)));
10758    */
10759
10760    /* We must do unrealize child widget BEFORE container widget.
10761     * gdk_window_destroy() destroys specified xwindow and its sub-xwindows.
10762     * So, unrealizing container widget bofore its children causes the problem
10763     * (for example, gdk_ic_destroy () with destroyed window causes crash. )
10764     */
10765
10766   if (GTK_IS_CONTAINER (widget))
10767     gtk_container_forall (GTK_CONTAINER (widget),
10768                           (GtkCallback) gtk_widget_unrealize,
10769                           NULL);
10770
10771   gtk_style_detach (priv->style);
10772   if (gtk_widget_get_has_window (widget))
10773     {
10774       gdk_window_set_user_data (priv->window, NULL);
10775       gdk_window_destroy (priv->window);
10776       priv->window = NULL;
10777     }
10778   else
10779     {
10780       g_object_unref (priv->window);
10781       priv->window = NULL;
10782     }
10783
10784   gtk_selection_remove_all (widget);
10785
10786   gtk_widget_set_realized (widget, FALSE);
10787 }
10788
10789 static void
10790 gtk_widget_real_adjust_size_request (GtkWidget         *widget,
10791                                      GtkOrientation     orientation,
10792                                      gint              *minimum_size,
10793                                      gint              *natural_size)
10794 {
10795   const GtkWidgetAuxInfo *aux_info;
10796
10797   aux_info =_gtk_widget_get_aux_info_or_defaults (widget);
10798
10799   if (orientation == GTK_ORIENTATION_HORIZONTAL &&
10800       aux_info->width > 0)
10801     {
10802       *minimum_size = MAX (*minimum_size, aux_info->width);
10803     }
10804   else if (orientation == GTK_ORIENTATION_VERTICAL &&
10805            aux_info->height > 0)
10806     {
10807       *minimum_size = MAX (*minimum_size, aux_info->height);
10808     }
10809
10810   /* Fix it if set_size_request made natural size smaller than min size.
10811    * This would also silently fix broken widgets, but we warn about them
10812    * in gtksizerequest.c when calling their size request vfuncs.
10813    */
10814   *natural_size = MAX (*natural_size, *minimum_size);
10815
10816   if (orientation == GTK_ORIENTATION_HORIZONTAL)
10817     {
10818       *minimum_size += (aux_info->margin.left + aux_info->margin.right);
10819       *natural_size += (aux_info->margin.left + aux_info->margin.right);
10820     }
10821   else
10822     {
10823       *minimum_size += (aux_info->margin.top + aux_info->margin.bottom);
10824       *natural_size += (aux_info->margin.top + aux_info->margin.bottom);
10825     }
10826 }
10827
10828 /**
10829  * _gtk_widget_peek_request_cache:
10830  *
10831  * Returns the address of the widget's request cache (strictly for
10832  * internal use in gtksizerequest.c)
10833  *
10834  * Return value: the address of @widget's size request cache.
10835  **/
10836 gpointer
10837 _gtk_widget_peek_request_cache (GtkWidget *widget)
10838 {
10839   /* Don't bother slowing things down with the return_if_fail guards here */
10840   return &widget->priv->requests;
10841 }
10842
10843 /*
10844  * _gtk_widget_set_device_window:
10845  * @widget: a #GtkWidget
10846  * @device: a #GdkDevice
10847  * @window: the new device window
10848  *
10849  * Sets pointer window for @widget and @device.
10850  * Does not ref @window.
10851  */
10852 void
10853 _gtk_widget_set_device_window (GtkWidget *widget,
10854                                GdkDevice *device,
10855                                GdkWindow *window)
10856 {
10857   GHashTable *device_window;
10858
10859   g_return_if_fail (GTK_IS_WIDGET (widget));
10860   g_return_if_fail (GDK_IS_DEVICE (device));
10861   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
10862
10863   if (!gtk_widget_get_mapped (widget))
10864     return;
10865
10866   device_window = g_object_get_qdata (G_OBJECT (widget), quark_pointer_window);
10867
10868   if (!device_window && window)
10869     {
10870       device_window = g_hash_table_new (NULL, NULL);
10871       g_object_set_qdata_full (G_OBJECT (widget),
10872                                quark_pointer_window,
10873                                device_window,
10874                                (GDestroyNotify) g_hash_table_destroy);
10875     }
10876
10877   if (window)
10878     g_hash_table_insert (device_window, device, window);
10879   else if (device_window)
10880     {
10881       g_hash_table_remove (device_window, device);
10882
10883       if (g_hash_table_size (device_window) == 0)
10884         g_object_set_qdata (G_OBJECT (widget), quark_pointer_window, NULL);
10885     }
10886 }
10887
10888 /*
10889  * _gtk_widget_get_device_window:
10890  * @widget: a #GtkWidget
10891  * @device: a #GdkDevice
10892  *
10893  * Return value: the device window set on @widget, or %NULL
10894  */
10895 GdkWindow *
10896 _gtk_widget_get_device_window (GtkWidget *widget,
10897                                GdkDevice *device)
10898 {
10899   GHashTable *device_window;
10900
10901   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10902   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
10903
10904   if (!gtk_widget_get_mapped (widget))
10905     return NULL;
10906
10907   device_window = g_object_get_qdata (G_OBJECT (widget), quark_pointer_window);
10908
10909   if (!device_window)
10910     return NULL;
10911
10912   return g_hash_table_lookup (device_window, device);
10913 }
10914
10915 /*
10916  * _gtk_widget_list_devices:
10917  * @widget: a #GtkWidget
10918  *
10919  * Returns the list of #GdkDevices that is currently on top
10920  * of any window belonging to @widget.
10921  * Free the list with g_list_free(), the elements are owned
10922  * by GTK+ and must not be freed.
10923  */
10924 GList *
10925 _gtk_widget_list_devices (GtkWidget *widget)
10926 {
10927   GHashTableIter iter;
10928   GHashTable *device_window;
10929   GList *devices = NULL;
10930   gpointer key, value;
10931
10932   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10933
10934   if (!gtk_widget_get_mapped (widget))
10935     return NULL;
10936
10937   device_window = g_object_get_qdata (G_OBJECT (widget), quark_pointer_window);
10938
10939   if (G_UNLIKELY (!device_window))
10940     return NULL;
10941
10942   g_hash_table_iter_init (&iter, device_window);
10943
10944   while (g_hash_table_iter_next (&iter, &key, &value))
10945     devices = g_list_prepend (devices, key);
10946
10947   return devices;
10948 }
10949
10950 static void
10951 synth_crossing (GtkWidget       *widget,
10952                 GdkEventType     type,
10953                 GdkWindow       *window,
10954                 GdkDevice       *device,
10955                 GdkCrossingMode  mode,
10956                 GdkNotifyType    detail)
10957 {
10958   GdkEvent *event;
10959
10960   event = gdk_event_new (type);
10961
10962   event->crossing.window = g_object_ref (window);
10963   event->crossing.send_event = TRUE;
10964   event->crossing.subwindow = g_object_ref (window);
10965   event->crossing.time = GDK_CURRENT_TIME;
10966   event->crossing.x = event->crossing.y = 0;
10967   event->crossing.x_root = event->crossing.y_root = 0;
10968   event->crossing.mode = mode;
10969   event->crossing.detail = detail;
10970   event->crossing.focus = FALSE;
10971   event->crossing.state = 0;
10972   gdk_event_set_device (event, device);
10973
10974   if (!widget)
10975     widget = gtk_get_event_widget (event);
10976
10977   if (widget)
10978     gtk_widget_event_internal (widget, event);
10979
10980   gdk_event_free (event);
10981 }
10982
10983 /*
10984  * _gtk_widget_synthesize_crossing:
10985  * @from: the #GtkWidget the virtual pointer is leaving.
10986  * @to: the #GtkWidget the virtual pointer is moving to.
10987  * @mode: the #GdkCrossingMode to place on the synthesized events.
10988  *
10989  * Generate crossing event(s) on widget state (sensitivity) or GTK+ grab change.
10990  *
10991  * The real pointer window is the window that most recently received an enter notify
10992  * event.  Windows that don't select for crossing events can't become the real
10993  * poiner window.  The real pointer widget that owns the real pointer window.  The
10994  * effective pointer window is the same as the real pointer window unless the real
10995  * pointer widget is either insensitive or there is a grab on a widget that is not
10996  * an ancestor of the real pointer widget (in which case the effective pointer
10997  * window should be the root window).
10998  *
10999  * When the effective pointer window is the same as the real poiner window, we
11000  * receive crossing events from the windowing system.  When the effective pointer
11001  * window changes to become different from the real pointer window we synthesize
11002  * crossing events, attempting to follow X protocol rules:
11003  *
11004  * When the root window becomes the effective pointer window:
11005  *   - leave notify on real pointer window, detail Ancestor
11006  *   - leave notify on all of its ancestors, detail Virtual
11007  *   - enter notify on root window, detail Inferior
11008  *
11009  * When the root window ceases to be the effective pointer window:
11010  *   - leave notify on root window, detail Inferior
11011  *   - enter notify on all ancestors of real pointer window, detail Virtual
11012  *   - enter notify on real pointer window, detail Ancestor
11013  */
11014 void
11015 _gtk_widget_synthesize_crossing (GtkWidget       *from,
11016                                  GtkWidget       *to,
11017                                  GdkDevice       *device,
11018                                  GdkCrossingMode  mode)
11019 {
11020   GdkWindow *from_window = NULL, *to_window = NULL;
11021
11022   g_return_if_fail (from != NULL || to != NULL);
11023
11024   if (from != NULL)
11025     {
11026       from_window = _gtk_widget_get_device_window (from, device);
11027
11028       if (!from_window)
11029         from_window = from->priv->window;
11030     }
11031
11032   if (to != NULL)
11033     {
11034       to_window = _gtk_widget_get_device_window (to, device);
11035
11036       if (!to_window)
11037         to_window = to->priv->window;
11038     }
11039
11040   if (from_window == NULL && to_window == NULL)
11041     ;
11042   else if (from_window != NULL && to_window == NULL)
11043     {
11044       GList *from_ancestors = NULL, *list;
11045       GdkWindow *from_ancestor = from_window;
11046
11047       while (from_ancestor != NULL)
11048         {
11049           from_ancestor = gdk_window_get_effective_parent (from_ancestor);
11050           if (from_ancestor == NULL)
11051             break;
11052           from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
11053         }
11054
11055       synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11056                       device, mode, GDK_NOTIFY_ANCESTOR);
11057       for (list = g_list_last (from_ancestors); list; list = list->prev)
11058         {
11059           synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
11060                           device, mode, GDK_NOTIFY_VIRTUAL);
11061         }
11062
11063       /* XXX: enter/inferior on root window? */
11064
11065       g_list_free (from_ancestors);
11066     }
11067   else if (from_window == NULL && to_window != NULL)
11068     {
11069       GList *to_ancestors = NULL, *list;
11070       GdkWindow *to_ancestor = to_window;
11071
11072       while (to_ancestor != NULL)
11073         {
11074           to_ancestor = gdk_window_get_effective_parent (to_ancestor);
11075           if (to_ancestor == NULL)
11076             break;
11077           to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
11078         }
11079
11080       /* XXX: leave/inferior on root window? */
11081
11082       for (list = to_ancestors; list; list = list->next)
11083         {
11084           synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
11085                           device, mode, GDK_NOTIFY_VIRTUAL);
11086         }
11087       synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11088                       device, mode, GDK_NOTIFY_ANCESTOR);
11089
11090       g_list_free (to_ancestors);
11091     }
11092   else if (from_window == to_window)
11093     ;
11094   else
11095     {
11096       GList *from_ancestors = NULL, *to_ancestors = NULL, *list;
11097       GdkWindow *from_ancestor = from_window, *to_ancestor = to_window;
11098
11099       while (from_ancestor != NULL || to_ancestor != NULL)
11100         {
11101           if (from_ancestor != NULL)
11102             {
11103               from_ancestor = gdk_window_get_effective_parent (from_ancestor);
11104               if (from_ancestor == to_window)
11105                 break;
11106               if (from_ancestor)
11107                 from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
11108             }
11109           if (to_ancestor != NULL)
11110             {
11111               to_ancestor = gdk_window_get_effective_parent (to_ancestor);
11112               if (to_ancestor == from_window)
11113                 break;
11114               if (to_ancestor)
11115                 to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
11116             }
11117         }
11118       if (to_ancestor == from_window)
11119         {
11120           if (mode != GDK_CROSSING_GTK_UNGRAB)
11121             synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11122                             device, mode, GDK_NOTIFY_INFERIOR);
11123           for (list = to_ancestors; list; list = list->next)
11124             synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
11125                             device, mode, GDK_NOTIFY_VIRTUAL);
11126           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11127                           device, mode, GDK_NOTIFY_ANCESTOR);
11128         }
11129       else if (from_ancestor == to_window)
11130         {
11131           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11132                           device, mode, GDK_NOTIFY_ANCESTOR);
11133           for (list = g_list_last (from_ancestors); list; list = list->prev)
11134             {
11135               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
11136                               device, mode, GDK_NOTIFY_VIRTUAL);
11137             }
11138           if (mode != GDK_CROSSING_GTK_GRAB)
11139             synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11140                             device, mode, GDK_NOTIFY_INFERIOR);
11141         }
11142       else
11143         {
11144           while (from_ancestors != NULL && to_ancestors != NULL
11145                  && from_ancestors->data == to_ancestors->data)
11146             {
11147               from_ancestors = g_list_delete_link (from_ancestors,
11148                                                    from_ancestors);
11149               to_ancestors = g_list_delete_link (to_ancestors, to_ancestors);
11150             }
11151
11152           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
11153                           device, mode, GDK_NOTIFY_NONLINEAR);
11154
11155           for (list = g_list_last (from_ancestors); list; list = list->prev)
11156             {
11157               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
11158                               device, mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
11159             }
11160           for (list = to_ancestors; list; list = list->next)
11161             {
11162               synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
11163                               device, mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
11164             }
11165           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
11166                           device, mode, GDK_NOTIFY_NONLINEAR);
11167         }
11168       g_list_free (from_ancestors);
11169       g_list_free (to_ancestors);
11170     }
11171 }
11172
11173 static void
11174 gtk_widget_propagate_state (GtkWidget    *widget,
11175                             GtkStateData *data)
11176 {
11177   GtkWidgetPrivate *priv = widget->priv;
11178   GtkStateFlags new_flags, old_flags = priv->state_flags;
11179   GtkStateType old_state;
11180
11181   old_state = gtk_widget_get_state (widget);
11182
11183   if (!priv->parent_sensitive)
11184     old_flags |= GTK_STATE_FLAG_INSENSITIVE;
11185
11186   priv->parent_sensitive = data->parent_sensitive;
11187
11188   switch (data->operation)
11189     {
11190     case STATE_CHANGE_REPLACE:
11191       priv->state_flags = data->flags;
11192       break;
11193     case STATE_CHANGE_SET:
11194       priv->state_flags |= data->flags;
11195       break;
11196     case STATE_CHANGE_UNSET:
11197       priv->state_flags &= ~(data->flags);
11198       break;
11199     }
11200
11201   if (gtk_widget_is_focus (widget) && !gtk_widget_is_sensitive (widget))
11202     {
11203       GtkWidget *window;
11204
11205       window = gtk_widget_get_toplevel (widget);
11206
11207       if (window && gtk_widget_is_toplevel (window))
11208         gtk_window_set_focus (GTK_WINDOW (window), NULL);
11209     }
11210
11211   new_flags = gtk_widget_get_state_flags (widget);
11212
11213   if (old_flags != new_flags)
11214     {
11215       g_object_ref (widget);
11216
11217       if (!gtk_widget_is_sensitive (widget) && gtk_widget_has_grab (widget))
11218         gtk_grab_remove (widget);
11219
11220       g_signal_emit (widget, widget_signals[STATE_CHANGED], 0, old_state);
11221       g_signal_emit (widget, widget_signals[STATE_FLAGS_CHANGED], 0, old_flags);
11222
11223       if (!priv->shadowed)
11224         {
11225           GList *event_windows = NULL;
11226           GList *devices, *d;
11227
11228           devices = _gtk_widget_list_devices (widget);
11229
11230           for (d = devices; d; d = d->next)
11231             {
11232               GdkWindow *window;
11233               GdkDevice *device;
11234
11235               device = d->data;
11236               window = _gtk_widget_get_device_window (widget, device);
11237
11238               /* Do not propagate more than once to the
11239                * same window if non-multidevice aware.
11240                */
11241               if (!gdk_window_get_support_multidevice (window) &&
11242                   g_list_find (event_windows, window))
11243                 continue;
11244
11245               if (!gtk_widget_is_sensitive (widget))
11246                 _gtk_widget_synthesize_crossing (widget, NULL, d->data,
11247                                                  GDK_CROSSING_STATE_CHANGED);
11248               else if (old_flags & GTK_STATE_FLAG_INSENSITIVE)
11249                 _gtk_widget_synthesize_crossing (NULL, widget, d->data,
11250                                                  GDK_CROSSING_STATE_CHANGED);
11251
11252               event_windows = g_list_prepend (event_windows, window);
11253             }
11254
11255           g_list_free (event_windows);
11256           g_list_free (devices);
11257         }
11258
11259       if (GTK_IS_CONTAINER (widget))
11260         {
11261           GtkStateData child_data = *data;
11262
11263           child_data.parent_sensitive = gtk_widget_is_sensitive (widget);
11264
11265           /* Do not propagate focused state further */
11266           child_data.flags &= ~GTK_STATE_FLAG_FOCUSED;
11267
11268           if (child_data.use_forall)
11269             gtk_container_forall (GTK_CONTAINER (widget),
11270                                   (GtkCallback) gtk_widget_propagate_state,
11271                                   &child_data);
11272           else
11273             gtk_container_foreach (GTK_CONTAINER (widget),
11274                                    (GtkCallback) gtk_widget_propagate_state,
11275                                    &child_data);
11276         }
11277
11278       /* Trigger state change transitions for the widget */
11279       if (priv->context &&
11280           gtk_widget_get_mapped (widget))
11281         {
11282           gint diff, flag = 1;
11283
11284           diff = old_flags ^ new_flags;
11285
11286           while (diff != 0)
11287             {
11288               if ((diff & flag) != 0)
11289                 {
11290                   gboolean target;
11291
11292                   target = ((new_flags & flag) != 0);
11293                   _gtk_widget_notify_state_change (widget, flag, target);
11294
11295                   diff &= ~flag;
11296                 }
11297
11298               flag <<= 1;
11299             }
11300         }
11301
11302       g_object_unref (widget);
11303     }
11304 }
11305
11306 static const GtkWidgetAuxInfo default_aux_info = {
11307   -1, -1,
11308   GTK_ALIGN_FILL,
11309   GTK_ALIGN_FILL,
11310   { 0, 0, 0, 0 }
11311 };
11312
11313 /*
11314  * _gtk_widget_get_aux_info:
11315  * @widget: a #GtkWidget
11316  * @create: if %TRUE, create the structure if it doesn't exist
11317  *
11318  * Get the #GtkWidgetAuxInfo structure for the widget.
11319  *
11320  * Return value: the #GtkAuxInfo structure for the widget, or
11321  *    %NULL if @create is %FALSE and one doesn't already exist.
11322  */
11323 GtkWidgetAuxInfo*
11324 _gtk_widget_get_aux_info (GtkWidget *widget,
11325                           gboolean   create)
11326 {
11327   GtkWidgetAuxInfo *aux_info;
11328
11329   aux_info = g_object_get_qdata (G_OBJECT (widget), quark_aux_info);
11330   if (!aux_info && create)
11331     {
11332       aux_info = g_slice_new0 (GtkWidgetAuxInfo);
11333
11334       *aux_info = default_aux_info;
11335
11336       g_object_set_qdata (G_OBJECT (widget), quark_aux_info, aux_info);
11337     }
11338
11339   return aux_info;
11340 }
11341
11342 static const GtkWidgetAuxInfo*
11343 _gtk_widget_get_aux_info_or_defaults (GtkWidget *widget)
11344 {
11345   GtkWidgetAuxInfo *aux_info;
11346
11347   aux_info = _gtk_widget_get_aux_info (widget, FALSE);
11348   if (aux_info == NULL)
11349     {
11350       return &default_aux_info;
11351     }
11352   else
11353     {
11354       return aux_info;
11355     }
11356 }
11357
11358 /*****************************************
11359  * gtk_widget_aux_info_destroy:
11360  *
11361  *   arguments:
11362  *
11363  *   results:
11364  *****************************************/
11365
11366 static void
11367 gtk_widget_aux_info_destroy (GtkWidgetAuxInfo *aux_info)
11368 {
11369   g_slice_free (GtkWidgetAuxInfo, aux_info);
11370 }
11371
11372 /**
11373  * gtk_widget_shape_combine_region:
11374  * @widget: a #GtkWidget
11375  * @region: (allow-none): shape to be added, or %NULL to remove an existing shape
11376  *
11377  * Sets a shape for this widget's GDK window. This allows for
11378  * transparent windows etc., see gdk_window_shape_combine_region()
11379  * for more information.
11380  *
11381  * Since: 3.0
11382  **/
11383 void
11384 gtk_widget_shape_combine_region (GtkWidget *widget,
11385                                  cairo_region_t *region)
11386 {
11387   GtkWidgetPrivate *priv;
11388
11389   g_return_if_fail (GTK_IS_WIDGET (widget));
11390   /*  set_shape doesn't work on widgets without gdk window */
11391   g_return_if_fail (gtk_widget_get_has_window (widget));
11392
11393   priv = widget->priv;
11394
11395   if (region == NULL)
11396     {
11397       priv->has_shape_mask = FALSE;
11398
11399       if (priv->window)
11400         gdk_window_shape_combine_region (priv->window, NULL, 0, 0);
11401
11402       g_object_set_qdata (G_OBJECT (widget), quark_shape_info, NULL);
11403     }
11404   else
11405     {
11406       priv->has_shape_mask = TRUE;
11407
11408       g_object_set_qdata_full (G_OBJECT (widget), quark_shape_info,
11409                                cairo_region_copy (region),
11410                                (GDestroyNotify) cairo_region_destroy);
11411
11412       /* set shape if widget has a gdk window already.
11413        * otherwise the shape is scheduled to be set by gtk_widget_realize().
11414        */
11415       if (priv->window)
11416         gdk_window_shape_combine_region (priv->window, region, 0, 0);
11417     }
11418 }
11419
11420 /**
11421  * gtk_widget_input_shape_combine_region:
11422  * @widget: a #GtkWidget
11423  * @region: (allow-none): shape to be added, or %NULL to remove an existing shape
11424  *
11425  * Sets an input shape for this widget's GDK window. This allows for
11426  * windows which react to mouse click in a nonrectangular region, see
11427  * gdk_window_input_shape_combine_region() for more information.
11428  *
11429  * Since: 3.0
11430  **/
11431 void
11432 gtk_widget_input_shape_combine_region (GtkWidget *widget,
11433                                        cairo_region_t *region)
11434 {
11435   GtkWidgetPrivate *priv;
11436
11437   g_return_if_fail (GTK_IS_WIDGET (widget));
11438   /*  set_shape doesn't work on widgets without gdk window */
11439   g_return_if_fail (gtk_widget_get_has_window (widget));
11440
11441   priv = widget->priv;
11442
11443   if (region == NULL)
11444     {
11445       if (priv->window)
11446         gdk_window_input_shape_combine_region (priv->window, NULL, 0, 0);
11447
11448       g_object_set_qdata (G_OBJECT (widget), quark_input_shape_info, NULL);
11449     }
11450   else
11451     {
11452       g_object_set_qdata_full (G_OBJECT (widget), quark_input_shape_info,
11453                                cairo_region_copy (region),
11454                                (GDestroyNotify) cairo_region_destroy);
11455
11456       /* set shape if widget has a gdk window already.
11457        * otherwise the shape is scheduled to be set by gtk_widget_realize().
11458        */
11459       if (priv->window)
11460         gdk_window_input_shape_combine_region (priv->window, region, 0, 0);
11461     }
11462 }
11463
11464
11465 /* style properties
11466  */
11467
11468 /**
11469  * gtk_widget_class_install_style_property_parser: (skip)
11470  * @klass: a #GtkWidgetClass
11471  * @pspec: the #GParamSpec for the style property
11472  * @parser: the parser for the style property
11473  *
11474  * Installs a style property on a widget class.
11475  **/
11476 void
11477 gtk_widget_class_install_style_property_parser (GtkWidgetClass     *klass,
11478                                                 GParamSpec         *pspec,
11479                                                 GtkRcPropertyParser parser)
11480 {
11481   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
11482   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
11483   g_return_if_fail (pspec->flags & G_PARAM_READABLE);
11484   g_return_if_fail (!(pspec->flags & (G_PARAM_CONSTRUCT_ONLY | G_PARAM_CONSTRUCT)));
11485
11486   if (g_param_spec_pool_lookup (style_property_spec_pool, pspec->name, G_OBJECT_CLASS_TYPE (klass), FALSE))
11487     {
11488       g_warning (G_STRLOC ": class `%s' already contains a style property named `%s'",
11489                  G_OBJECT_CLASS_NAME (klass),
11490                  pspec->name);
11491       return;
11492     }
11493
11494   g_param_spec_ref_sink (pspec);
11495   g_param_spec_set_qdata (pspec, quark_property_parser, (gpointer) parser);
11496   g_param_spec_pool_insert (style_property_spec_pool, pspec, G_OBJECT_CLASS_TYPE (klass));
11497 }
11498
11499 /**
11500  * gtk_widget_class_install_style_property:
11501  * @klass: a #GtkWidgetClass
11502  * @pspec: the #GParamSpec for the property
11503  *
11504  * Installs a style property on a widget class. The parser for the
11505  * style property is determined by the value type of @pspec.
11506  **/
11507 void
11508 gtk_widget_class_install_style_property (GtkWidgetClass *klass,
11509                                          GParamSpec     *pspec)
11510 {
11511   GtkRcPropertyParser parser;
11512
11513   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
11514   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
11515
11516   parser = _gtk_rc_property_parser_from_type (G_PARAM_SPEC_VALUE_TYPE (pspec));
11517
11518   gtk_widget_class_install_style_property_parser (klass, pspec, parser);
11519 }
11520
11521 /**
11522  * gtk_widget_class_find_style_property:
11523  * @klass: a #GtkWidgetClass
11524  * @property_name: the name of the style property to find
11525  * @returns: (transfer none): the #GParamSpec of the style property or
11526  *   %NULL if @class has no style property with that name.
11527  *
11528  * Finds a style property of a widget class by name.
11529  *
11530  * Since: 2.2
11531  */
11532 GParamSpec*
11533 gtk_widget_class_find_style_property (GtkWidgetClass *klass,
11534                                       const gchar    *property_name)
11535 {
11536   g_return_val_if_fail (property_name != NULL, NULL);
11537
11538   return g_param_spec_pool_lookup (style_property_spec_pool,
11539                                    property_name,
11540                                    G_OBJECT_CLASS_TYPE (klass),
11541                                    TRUE);
11542 }
11543
11544 /**
11545  * gtk_widget_class_list_style_properties:
11546  * @klass: a #GtkWidgetClass
11547  * @n_properties: location to return the number of style properties found
11548  * @returns: (array length=n_properties) (transfer container): an
11549  *       newly allocated array of #GParamSpec*. The array must be
11550  *       freed with g_free().
11551  *
11552  * Returns all style properties of a widget class.
11553  *
11554  * Since: 2.2
11555  */
11556 GParamSpec**
11557 gtk_widget_class_list_style_properties (GtkWidgetClass *klass,
11558                                         guint          *n_properties)
11559 {
11560   GParamSpec **pspecs;
11561   guint n;
11562
11563   pspecs = g_param_spec_pool_list (style_property_spec_pool,
11564                                    G_OBJECT_CLASS_TYPE (klass),
11565                                    &n);
11566   if (n_properties)
11567     *n_properties = n;
11568
11569   return pspecs;
11570 }
11571
11572 /**
11573  * gtk_widget_style_get_property:
11574  * @widget: a #GtkWidget
11575  * @property_name: the name of a style property
11576  * @value: location to return the property value
11577  *
11578  * Gets the value of a style property of @widget.
11579  */
11580 void
11581 gtk_widget_style_get_property (GtkWidget   *widget,
11582                                const gchar *property_name,
11583                                GValue      *value)
11584 {
11585   GParamSpec *pspec;
11586
11587   g_return_if_fail (GTK_IS_WIDGET (widget));
11588   g_return_if_fail (property_name != NULL);
11589   g_return_if_fail (G_IS_VALUE (value));
11590
11591   g_object_ref (widget);
11592   pspec = g_param_spec_pool_lookup (style_property_spec_pool,
11593                                     property_name,
11594                                     G_OBJECT_TYPE (widget),
11595                                     TRUE);
11596   if (!pspec)
11597     g_warning ("%s: widget class `%s' has no property named `%s'",
11598                G_STRLOC,
11599                G_OBJECT_TYPE_NAME (widget),
11600                property_name);
11601   else
11602     {
11603       GtkStyleContext *context;
11604       const GValue *peek_value;
11605       GtkStateFlags state;
11606
11607       context = gtk_widget_get_style_context (widget);
11608       state = gtk_widget_get_state_flags (widget);
11609
11610       peek_value = _gtk_style_context_peek_style_property (context,
11611                                                            G_OBJECT_TYPE (widget),
11612                                                            state, pspec);
11613
11614       /* auto-conversion of the caller's value type
11615        */
11616       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
11617         g_value_copy (peek_value, value);
11618       else if (g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
11619         g_value_transform (peek_value, value);
11620       else
11621         g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
11622                    pspec->name,
11623                    g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
11624                    G_VALUE_TYPE_NAME (value));
11625     }
11626   g_object_unref (widget);
11627 }
11628
11629 /**
11630  * gtk_widget_style_get_valist:
11631  * @widget: a #GtkWidget
11632  * @first_property_name: the name of the first property to get
11633  * @var_args: a <type>va_list</type> of pairs of property names and
11634  *     locations to return the property values, starting with the location
11635  *     for @first_property_name.
11636  *
11637  * Non-vararg variant of gtk_widget_style_get(). Used primarily by language
11638  * bindings.
11639  */
11640 void
11641 gtk_widget_style_get_valist (GtkWidget   *widget,
11642                              const gchar *first_property_name,
11643                              va_list      var_args)
11644 {
11645   GtkStyleContext *context;
11646   GtkStateFlags state;
11647   const gchar *name;
11648
11649   g_return_if_fail (GTK_IS_WIDGET (widget));
11650
11651   g_object_ref (widget);
11652   context = gtk_widget_get_style_context (widget);
11653   state = gtk_widget_get_state_flags (widget);
11654
11655   name = first_property_name;
11656   while (name)
11657     {
11658       const GValue *peek_value;
11659       GParamSpec *pspec;
11660       gchar *error;
11661
11662       pspec = g_param_spec_pool_lookup (style_property_spec_pool,
11663                                         name,
11664                                         G_OBJECT_TYPE (widget),
11665                                         TRUE);
11666       if (!pspec)
11667         {
11668           g_warning ("%s: widget class `%s' has no property named `%s'",
11669                      G_STRLOC,
11670                      G_OBJECT_TYPE_NAME (widget),
11671                      name);
11672           break;
11673         }
11674       /* style pspecs are always readable so we can spare that check here */
11675
11676       peek_value = _gtk_style_context_peek_style_property (context,
11677                                                            G_OBJECT_TYPE (widget),
11678                                                            state, pspec);
11679
11680       G_VALUE_LCOPY (peek_value, var_args, 0, &error);
11681       if (error)
11682         {
11683           g_warning ("%s: %s", G_STRLOC, error);
11684           g_free (error);
11685           break;
11686         }
11687
11688       name = va_arg (var_args, gchar*);
11689     }
11690
11691   g_object_unref (widget);
11692 }
11693
11694 /**
11695  * gtk_widget_style_get:
11696  * @widget: a #GtkWidget
11697  * @first_property_name: the name of the first property to get
11698  * @Varargs: pairs of property names and locations to
11699  *   return the property values, starting with the location for
11700  *   @first_property_name, terminated by %NULL.
11701  *
11702  * Gets the values of a multiple style properties of @widget.
11703  */
11704 void
11705 gtk_widget_style_get (GtkWidget   *widget,
11706                       const gchar *first_property_name,
11707                       ...)
11708 {
11709   va_list var_args;
11710
11711   g_return_if_fail (GTK_IS_WIDGET (widget));
11712
11713   va_start (var_args, first_property_name);
11714   gtk_widget_style_get_valist (widget, first_property_name, var_args);
11715   va_end (var_args);
11716 }
11717
11718 /**
11719  * gtk_widget_path:
11720  * @widget: a #GtkWidget
11721  * @path_length: (out) (allow-none): location to store length of the path,
11722  *     or %NULL
11723  * @path: (out) (allow-none): location to store allocated path string,
11724  *     or %NULL
11725  * @path_reversed: (out) (allow-none): location to store allocated reverse
11726  *     path string, or %NULL
11727  *
11728  * Obtains the full path to @widget. The path is simply the name of a
11729  * widget and all its parents in the container hierarchy, separated by
11730  * periods. The name of a widget comes from
11731  * gtk_widget_get_name(). Paths are used to apply styles to a widget
11732  * in gtkrc configuration files. Widget names are the type of the
11733  * widget by default (e.g. "GtkButton") or can be set to an
11734  * application-specific value with gtk_widget_set_name(). By setting
11735  * the name of a widget, you allow users or theme authors to apply
11736  * styles to that specific widget in their gtkrc
11737  * file. @path_reversed_p fills in the path in reverse order,
11738  * i.e. starting with @widget's name instead of starting with the name
11739  * of @widget's outermost ancestor.
11740  *
11741  * Deprecated:3.0: Use gtk_widget_get_path() instead
11742  **/
11743 void
11744 gtk_widget_path (GtkWidget *widget,
11745                  guint     *path_length,
11746                  gchar    **path,
11747                  gchar    **path_reversed)
11748 {
11749   static gchar *rev_path = NULL;
11750   static guint tmp_path_len = 0;
11751   guint len;
11752
11753   g_return_if_fail (GTK_IS_WIDGET (widget));
11754
11755   len = 0;
11756   do
11757     {
11758       const gchar *string;
11759       const gchar *s;
11760       gchar *d;
11761       guint l;
11762
11763       string = gtk_widget_get_name (widget);
11764       l = strlen (string);
11765       while (tmp_path_len <= len + l + 1)
11766         {
11767           tmp_path_len += INIT_PATH_SIZE;
11768           rev_path = g_realloc (rev_path, tmp_path_len);
11769         }
11770       s = string + l - 1;
11771       d = rev_path + len;
11772       while (s >= string)
11773         *(d++) = *(s--);
11774       len += l;
11775
11776       widget = widget->priv->parent;
11777
11778       if (widget)
11779         rev_path[len++] = '.';
11780       else
11781         rev_path[len++] = 0;
11782     }
11783   while (widget);
11784
11785   if (path_length)
11786     *path_length = len - 1;
11787   if (path_reversed)
11788     *path_reversed = g_strdup (rev_path);
11789   if (path)
11790     {
11791       *path = g_strdup (rev_path);
11792       g_strreverse (*path);
11793     }
11794 }
11795
11796 /**
11797  * gtk_widget_class_path:
11798  * @widget: a #GtkWidget
11799  * @path_length: (out) (allow-none): location to store the length of the
11800  *     class path, or %NULL
11801  * @path: (out) (allow-none): location to store the class path as an
11802  *     allocated string, or %NULL
11803  * @path_reversed: (out) (allow-none): location to store the reverse
11804  *     class path as an allocated string, or %NULL
11805  *
11806  * Same as gtk_widget_path(), but always uses the name of a widget's type,
11807  * never uses a custom name set with gtk_widget_set_name().
11808  *
11809  * Deprecated:3.0: Use gtk_widget_get_path() instead
11810  **/
11811 void
11812 gtk_widget_class_path (GtkWidget *widget,
11813                        guint     *path_length,
11814                        gchar    **path,
11815                        gchar    **path_reversed)
11816 {
11817   static gchar *rev_path = NULL;
11818   static guint tmp_path_len = 0;
11819   guint len;
11820
11821   g_return_if_fail (GTK_IS_WIDGET (widget));
11822
11823   len = 0;
11824   do
11825     {
11826       const gchar *string;
11827       const gchar *s;
11828       gchar *d;
11829       guint l;
11830
11831       string = g_type_name (G_OBJECT_TYPE (widget));
11832       l = strlen (string);
11833       while (tmp_path_len <= len + l + 1)
11834         {
11835           tmp_path_len += INIT_PATH_SIZE;
11836           rev_path = g_realloc (rev_path, tmp_path_len);
11837         }
11838       s = string + l - 1;
11839       d = rev_path + len;
11840       while (s >= string)
11841         *(d++) = *(s--);
11842       len += l;
11843
11844       widget = widget->priv->parent;
11845
11846       if (widget)
11847         rev_path[len++] = '.';
11848       else
11849         rev_path[len++] = 0;
11850     }
11851   while (widget);
11852
11853   if (path_length)
11854     *path_length = len - 1;
11855   if (path_reversed)
11856     *path_reversed = g_strdup (rev_path);
11857   if (path)
11858     {
11859       *path = g_strdup (rev_path);
11860       g_strreverse (*path);
11861     }
11862 }
11863
11864 /**
11865  * gtk_requisition_new:
11866  *
11867  * Allocates a new #GtkRequisition structure and initializes its elements to zero.
11868  *
11869  * Returns: a new empty #GtkRequisition. The newly allocated #GtkRequisition should
11870  *   be freed with gtk_requisition_free().
11871  *
11872  * Since: 3.0
11873  */
11874 GtkRequisition *
11875 gtk_requisition_new (void)
11876 {
11877   return g_slice_new0 (GtkRequisition);
11878 }
11879
11880 /**
11881  * gtk_requisition_copy:
11882  * @requisition: a #GtkRequisition
11883  *
11884  * Copies a #GtkRequisition.
11885  *
11886  * Returns: a copy of @requisition
11887  **/
11888 GtkRequisition *
11889 gtk_requisition_copy (const GtkRequisition *requisition)
11890 {
11891   return g_slice_dup (GtkRequisition, requisition);
11892 }
11893
11894 /**
11895  * gtk_requisition_free:
11896  * @requisition: a #GtkRequisition
11897  *
11898  * Frees a #GtkRequisition.
11899  **/
11900 void
11901 gtk_requisition_free (GtkRequisition *requisition)
11902 {
11903   g_slice_free (GtkRequisition, requisition);
11904 }
11905
11906 G_DEFINE_BOXED_TYPE (GtkRequisition, gtk_requisition,
11907                      gtk_requisition_copy,
11908                      gtk_requisition_free)
11909
11910 /**
11911  * gtk_widget_get_accessible:
11912  * @widget: a #GtkWidget
11913  *
11914  * Returns the accessible object that describes the widget to an
11915  * assistive technology.
11916  *
11917  * If no accessibility library is loaded (i.e. no ATK implementation library is
11918  * loaded via <envar>GTK_MODULES</envar> or via another application library,
11919  * such as libgnome), then this #AtkObject instance may be a no-op. Likewise,
11920  * if no class-specific #AtkObject implementation is available for the widget
11921  * instance in question, it will inherit an #AtkObject implementation from the
11922  * first ancestor class for which such an implementation is defined.
11923  *
11924  * The documentation of the
11925  * <ulink url="http://library.gnome.org/devel/atk/stable/">ATK</ulink>
11926  * library contains more information about accessible objects and their uses.
11927  *
11928  * Returns: (transfer none): the #AtkObject associated with @widget
11929  */
11930 AtkObject*
11931 gtk_widget_get_accessible (GtkWidget *widget)
11932 {
11933   GtkWidgetClass *klass;
11934
11935   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
11936
11937   klass = GTK_WIDGET_GET_CLASS (widget);
11938
11939   g_return_val_if_fail (klass->get_accessible != NULL, NULL);
11940
11941   return klass->get_accessible (widget);
11942 }
11943
11944 static AtkObject*
11945 gtk_widget_real_get_accessible (GtkWidget *widget)
11946 {
11947   AtkObject* accessible;
11948
11949   accessible = g_object_get_qdata (G_OBJECT (widget),
11950                                    quark_accessible_object);
11951   if (!accessible)
11952   {
11953     AtkObjectFactory *factory;
11954     AtkRegistry *default_registry;
11955
11956     default_registry = atk_get_default_registry ();
11957     factory = atk_registry_get_factory (default_registry,
11958                                         G_TYPE_FROM_INSTANCE (widget));
11959     accessible =
11960       atk_object_factory_create_accessible (factory,
11961                                             G_OBJECT (widget));
11962     g_object_set_qdata (G_OBJECT (widget),
11963                         quark_accessible_object,
11964                         accessible);
11965   }
11966   return accessible;
11967 }
11968
11969 /*
11970  * Initialize a AtkImplementorIface instance's virtual pointers as
11971  * appropriate to this implementor's class (GtkWidget).
11972  */
11973 static void
11974 gtk_widget_accessible_interface_init (AtkImplementorIface *iface)
11975 {
11976   iface->ref_accessible = gtk_widget_ref_accessible;
11977 }
11978
11979 static AtkObject*
11980 gtk_widget_ref_accessible (AtkImplementor *implementor)
11981 {
11982   AtkObject *accessible;
11983
11984   accessible = gtk_widget_get_accessible (GTK_WIDGET (implementor));
11985   if (accessible)
11986     g_object_ref (accessible);
11987   return accessible;
11988 }
11989
11990 /*
11991  * Expand flag management
11992  */
11993
11994 static void
11995 gtk_widget_update_computed_expand (GtkWidget *widget)
11996 {
11997   GtkWidgetPrivate *priv;
11998
11999   priv = widget->priv;
12000
12001   if (priv->need_compute_expand)
12002     {
12003       gboolean h, v;
12004
12005       if (priv->hexpand_set)
12006         h = priv->hexpand;
12007       else
12008         h = FALSE;
12009
12010       if (priv->vexpand_set)
12011         v = priv->vexpand;
12012       else
12013         v = FALSE;
12014
12015       /* we don't need to use compute_expand if both expands are
12016        * forced by the app
12017        */
12018       if (!(priv->hexpand_set && priv->vexpand_set))
12019         {
12020           if (GTK_WIDGET_GET_CLASS (widget)->compute_expand != NULL)
12021             {
12022               gboolean ignored;
12023
12024               GTK_WIDGET_GET_CLASS (widget)->compute_expand (widget,
12025                                                              priv->hexpand_set ? &ignored : &h,
12026                                                              priv->vexpand_set ? &ignored : &v);
12027             }
12028         }
12029
12030       priv->need_compute_expand = FALSE;
12031       priv->computed_hexpand = h != FALSE;
12032       priv->computed_vexpand = v != FALSE;
12033     }
12034 }
12035
12036 /**
12037  * gtk_widget_queue_compute_expand:
12038  * @widget: a #GtkWidget
12039  *
12040  * Mark @widget as needing to recompute its expand flags. Call
12041  * this function when setting legacy expand child properties
12042  * on the child of a container.
12043  *
12044  * See gtk_widget_compute_expand().
12045  */
12046 void
12047 gtk_widget_queue_compute_expand (GtkWidget *widget)
12048 {
12049   GtkWidget *parent;
12050   gboolean changed_anything;
12051
12052   if (widget->priv->need_compute_expand)
12053     return;
12054
12055   changed_anything = FALSE;
12056   parent = widget;
12057   while (parent != NULL)
12058     {
12059       if (!parent->priv->need_compute_expand)
12060         {
12061           parent->priv->need_compute_expand = TRUE;
12062           changed_anything = TRUE;
12063         }
12064
12065       /* Note: if we had an invariant that "if a child needs to
12066        * compute expand, its parents also do" then we could stop going
12067        * up when we got to a parent that already needed to
12068        * compute. However, in general we compute expand lazily (as
12069        * soon as we see something in a subtree that is expand, we know
12070        * we're expanding) and so this invariant does not hold and we
12071        * have to always walk all the way up in case some ancestor
12072        * is not currently need_compute_expand.
12073        */
12074
12075       parent = parent->priv->parent;
12076     }
12077
12078   /* recomputing expand always requires
12079    * a relayout as well
12080    */
12081   if (changed_anything)
12082     gtk_widget_queue_resize (widget);
12083 }
12084
12085 /**
12086  * gtk_widget_compute_expand:
12087  * @widget: the widget
12088  * @orientation: expand direction
12089  *
12090  * Computes whether a container should give this widget extra space
12091  * when possible. Containers should check this, rather than
12092  * looking at gtk_widget_get_hexpand() or gtk_widget_get_vexpand().
12093  *
12094  * This function already checks whether the widget is visible, so
12095  * visibility does not need to be checked separately. Non-visible
12096  * widgets are not expanded.
12097  *
12098  * The computed expand value uses either the expand setting explicitly
12099  * set on the widget itself, or, if none has been explicitly set,
12100  * the widget may expand if some of its children do.
12101  *
12102  * Return value: whether widget tree rooted here should be expanded
12103  */
12104 gboolean
12105 gtk_widget_compute_expand (GtkWidget     *widget,
12106                            GtkOrientation orientation)
12107 {
12108   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12109
12110   /* We never make a widget expand if not even showing. */
12111   if (!gtk_widget_get_visible (widget))
12112     return FALSE;
12113
12114   gtk_widget_update_computed_expand (widget);
12115
12116   if (orientation == GTK_ORIENTATION_HORIZONTAL)
12117     {
12118       return widget->priv->computed_hexpand;
12119     }
12120   else
12121     {
12122       return widget->priv->computed_vexpand;
12123     }
12124 }
12125
12126 static void
12127 gtk_widget_set_expand (GtkWidget     *widget,
12128                        GtkOrientation orientation,
12129                        gboolean       expand)
12130 {
12131   const char *expand_prop;
12132   const char *expand_set_prop;
12133   gboolean was_both;
12134   GtkWidgetPrivate *priv;
12135
12136   g_return_if_fail (GTK_IS_WIDGET (widget));
12137
12138   priv = widget->priv;
12139
12140   expand = expand != FALSE;
12141
12142   was_both = priv->hexpand && priv->vexpand;
12143
12144   if (orientation == GTK_ORIENTATION_HORIZONTAL)
12145     {
12146       if (priv->hexpand_set &&
12147           priv->hexpand == expand)
12148         return;
12149
12150       priv->hexpand_set = TRUE;
12151       priv->hexpand = expand;
12152
12153       expand_prop = "hexpand";
12154       expand_set_prop = "hexpand-set";
12155     }
12156   else
12157     {
12158       if (priv->vexpand_set &&
12159           priv->vexpand == expand)
12160         return;
12161
12162       priv->vexpand_set = TRUE;
12163       priv->vexpand = expand;
12164
12165       expand_prop = "vexpand";
12166       expand_set_prop = "vexpand-set";
12167     }
12168
12169   gtk_widget_queue_compute_expand (widget);
12170
12171   g_object_freeze_notify (G_OBJECT (widget));
12172   g_object_notify (G_OBJECT (widget), expand_prop);
12173   g_object_notify (G_OBJECT (widget), expand_set_prop);
12174   if (was_both != (priv->hexpand && priv->vexpand))
12175     g_object_notify (G_OBJECT (widget), "expand");
12176   g_object_thaw_notify (G_OBJECT (widget));
12177 }
12178
12179 static void
12180 gtk_widget_set_expand_set (GtkWidget      *widget,
12181                            GtkOrientation  orientation,
12182                            gboolean        set)
12183 {
12184   GtkWidgetPrivate *priv;
12185   const char *prop;
12186
12187   priv = widget->priv;
12188
12189   set = set != FALSE;
12190
12191   if (orientation == GTK_ORIENTATION_HORIZONTAL)
12192     {
12193       if (set == priv->hexpand_set)
12194         return;
12195
12196       priv->hexpand_set = set;
12197       prop = "hexpand-set";
12198     }
12199   else
12200     {
12201       if (set == priv->vexpand_set)
12202         return;
12203
12204       priv->vexpand_set = set;
12205       prop = "vexpand-set";
12206     }
12207
12208   gtk_widget_queue_compute_expand (widget);
12209
12210   g_object_notify (G_OBJECT (widget), prop);
12211 }
12212
12213 /**
12214  * gtk_widget_get_hexpand:
12215  * @widget: the widget
12216  *
12217  * Gets whether the widget would like any available extra horizontal
12218  * space. When a user resizes a #GtkWindow, widgets with expand=TRUE
12219  * generally receive the extra space. For example, a list or
12220  * scrollable area or document in your window would often be set to
12221  * expand.
12222  *
12223  * Containers should use gtk_widget_compute_expand() rather than
12224  * this function, to see whether a widget, or any of its children,
12225  * has the expand flag set. If any child of a widget wants to
12226  * expand, the parent may ask to expand also.
12227  *
12228  * This function only looks at the widget's own hexpand flag, rather
12229  * than computing whether the entire widget tree rooted at this widget
12230  * wants to expand.
12231  *
12232  * Return value: whether hexpand flag is set
12233  */
12234 gboolean
12235 gtk_widget_get_hexpand (GtkWidget *widget)
12236 {
12237   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12238
12239   return widget->priv->hexpand;
12240 }
12241
12242 /**
12243  * gtk_widget_set_hexpand:
12244  * @widget: the widget
12245  * @expand: whether to expand
12246  *
12247  * Sets whether the widget would like any available extra horizontal
12248  * space. When a user resizes a #GtkWindow, widgets with expand=TRUE
12249  * generally receive the extra space. For example, a list or
12250  * scrollable area or document in your window would often be set to
12251  * expand.
12252  *
12253  * Call this function to set the expand flag if you would like your
12254  * widget to become larger horizontally when the window has extra
12255  * room.
12256  *
12257  * By default, widgets automatically expand if any of their children
12258  * want to expand. (To see if a widget will automatically expand given
12259  * its current children and state, call gtk_widget_compute_expand(). A
12260  * container can decide how the expandability of children affects the
12261  * expansion of the container by overriding the compute_expand virtual
12262  * method on #GtkWidget.).
12263  *
12264  * Setting hexpand explicitly with this function will override the
12265  * automatic expand behavior.
12266  *
12267  * This function forces the widget to expand or not to expand,
12268  * regardless of children.  The override occurs because
12269  * gtk_widget_set_hexpand() sets the hexpand-set property (see
12270  * gtk_widget_set_hexpand_set()) which causes the widget's hexpand
12271  * value to be used, rather than looking at children and widget state.
12272  */
12273 void
12274 gtk_widget_set_hexpand (GtkWidget      *widget,
12275                         gboolean        expand)
12276 {
12277   g_return_if_fail (GTK_IS_WIDGET (widget));
12278
12279   gtk_widget_set_expand (widget, GTK_ORIENTATION_HORIZONTAL, expand);
12280 }
12281
12282 /**
12283  * gtk_widget_get_hexpand_set:
12284  * @widget: the widget
12285  *
12286  * Gets whether gtk_widget_set_hexpand() has been used to
12287  * explicitly set the expand flag on this widget.
12288  *
12289  * If hexpand is set, then it overrides any computed
12290  * expand value based on child widgets. If hexpand is not
12291  * set, then the expand value depends on whether any
12292  * children of the widget would like to expand.
12293  *
12294  * There are few reasons to use this function, but it's here
12295  * for completeness and consistency.
12296  *
12297  * Return value: whether hexpand has been explicitly set
12298  */
12299 gboolean
12300 gtk_widget_get_hexpand_set (GtkWidget      *widget)
12301 {
12302   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12303
12304   return widget->priv->hexpand_set;
12305 }
12306
12307 /**
12308  * gtk_widget_set_hexpand_set:
12309  * @widget: the widget
12310  * @set: value for hexpand-set property
12311  *
12312  * Sets whether the hexpand flag (see gtk_widget_get_hexpand()) will
12313  * be used.
12314  *
12315  * The hexpand-set property will be set automatically when you call
12316  * gtk_widget_set_hexpand() to set hexpand, so the most likely
12317  * reason to use this function would be to unset an explicit expand
12318  * flag.
12319  *
12320  * If hexpand is set, then it overrides any computed
12321  * expand value based on child widgets. If hexpand is not
12322  * set, then the expand value depends on whether any
12323  * children of the widget would like to expand.
12324  *
12325  * There are few reasons to use this function, but it's here
12326  * for completeness and consistency.
12327  */
12328 void
12329 gtk_widget_set_hexpand_set (GtkWidget      *widget,
12330                             gboolean        set)
12331 {
12332   g_return_if_fail (GTK_IS_WIDGET (widget));
12333
12334   gtk_widget_set_expand_set (widget, GTK_ORIENTATION_HORIZONTAL, set);
12335 }
12336
12337
12338 /**
12339  * gtk_widget_get_vexpand:
12340  * @widget: the widget
12341  *
12342  * Gets whether the widget would like any available extra vertical
12343  * space.
12344  *
12345  * See gtk_widget_get_hexpand() for more detail.
12346  *
12347  * Return value: whether vexpand flag is set
12348  */
12349 gboolean
12350 gtk_widget_get_vexpand (GtkWidget *widget)
12351 {
12352   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12353
12354   return widget->priv->vexpand;
12355 }
12356
12357 /**
12358  * gtk_widget_set_vexpand:
12359  * @widget: the widget
12360  * @expand: whether to expand
12361  *
12362  * Sets whether the widget would like any available extra vertical
12363  * space.
12364  *
12365  * See gtk_widget_set_hexpand() for more detail.
12366  */
12367 void
12368 gtk_widget_set_vexpand (GtkWidget      *widget,
12369                         gboolean        expand)
12370 {
12371   g_return_if_fail (GTK_IS_WIDGET (widget));
12372
12373   gtk_widget_set_expand (widget, GTK_ORIENTATION_VERTICAL, expand);
12374 }
12375
12376 /**
12377  * gtk_widget_get_vexpand_set:
12378  * @widget: the widget
12379  *
12380  * Gets whether gtk_widget_set_vexpand() has been used to
12381  * explicitly set the expand flag on this widget.
12382  *
12383  * See gtk_widget_get_hexpand_set() for more detail.
12384  *
12385  * Return value: whether vexpand has been explicitly set
12386  */
12387 gboolean
12388 gtk_widget_get_vexpand_set (GtkWidget      *widget)
12389 {
12390   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12391
12392   return widget->priv->vexpand_set;
12393 }
12394
12395 /**
12396  * gtk_widget_set_vexpand_set:
12397  * @widget: the widget
12398  * @set: value for vexpand-set property
12399  *
12400  * Sets whether the vexpand flag (see gtk_widget_get_vexpand()) will
12401  * be used.
12402  *
12403  * See gtk_widget_set_hexpand_set() for more detail.
12404  */
12405 void
12406 gtk_widget_set_vexpand_set (GtkWidget      *widget,
12407                             gboolean        set)
12408 {
12409   g_return_if_fail (GTK_IS_WIDGET (widget));
12410
12411   gtk_widget_set_expand_set (widget, GTK_ORIENTATION_VERTICAL, set);
12412 }
12413
12414 /*
12415  * GtkBuildable implementation
12416  */
12417 static GQuark            quark_builder_has_default = 0;
12418 static GQuark            quark_builder_has_focus = 0;
12419 static GQuark            quark_builder_atk_relations = 0;
12420 static GQuark            quark_builder_set_name = 0;
12421
12422 static void
12423 gtk_widget_buildable_interface_init (GtkBuildableIface *iface)
12424 {
12425   quark_builder_has_default = g_quark_from_static_string ("gtk-builder-has-default");
12426   quark_builder_has_focus = g_quark_from_static_string ("gtk-builder-has-focus");
12427   quark_builder_atk_relations = g_quark_from_static_string ("gtk-builder-atk-relations");
12428   quark_builder_set_name = g_quark_from_static_string ("gtk-builder-set-name");
12429
12430   iface->set_name = gtk_widget_buildable_set_name;
12431   iface->get_name = gtk_widget_buildable_get_name;
12432   iface->get_internal_child = gtk_widget_buildable_get_internal_child;
12433   iface->set_buildable_property = gtk_widget_buildable_set_buildable_property;
12434   iface->parser_finished = gtk_widget_buildable_parser_finished;
12435   iface->custom_tag_start = gtk_widget_buildable_custom_tag_start;
12436   iface->custom_finished = gtk_widget_buildable_custom_finished;
12437 }
12438
12439 static void
12440 gtk_widget_buildable_set_name (GtkBuildable *buildable,
12441                                const gchar  *name)
12442 {
12443   g_object_set_qdata_full (G_OBJECT (buildable), quark_builder_set_name,
12444                            g_strdup (name), g_free);
12445 }
12446
12447 static const gchar *
12448 gtk_widget_buildable_get_name (GtkBuildable *buildable)
12449 {
12450   return g_object_get_qdata (G_OBJECT (buildable), quark_builder_set_name);
12451 }
12452
12453 static GObject *
12454 gtk_widget_buildable_get_internal_child (GtkBuildable *buildable,
12455                                          GtkBuilder   *builder,
12456                                          const gchar  *childname)
12457 {
12458   if (strcmp (childname, "accessible") == 0)
12459     return G_OBJECT (gtk_widget_get_accessible (GTK_WIDGET (buildable)));
12460
12461   return NULL;
12462 }
12463
12464 static void
12465 gtk_widget_buildable_set_buildable_property (GtkBuildable *buildable,
12466                                              GtkBuilder   *builder,
12467                                              const gchar  *name,
12468                                              const GValue *value)
12469 {
12470   if (strcmp (name, "has-default") == 0 && g_value_get_boolean (value))
12471       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_default,
12472                           GINT_TO_POINTER (TRUE));
12473   else if (strcmp (name, "has-focus") == 0 && g_value_get_boolean (value))
12474       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_focus,
12475                           GINT_TO_POINTER (TRUE));
12476   else
12477     g_object_set_property (G_OBJECT (buildable), name, value);
12478 }
12479
12480 typedef struct
12481 {
12482   gchar *action_name;
12483   GString *description;
12484   gchar *context;
12485   gboolean translatable;
12486 } AtkActionData;
12487
12488 typedef struct
12489 {
12490   gchar *target;
12491   gchar *type;
12492 } AtkRelationData;
12493
12494 static void
12495 free_action (AtkActionData *data, gpointer user_data)
12496 {
12497   g_free (data->action_name);
12498   g_string_free (data->description, TRUE);
12499   g_free (data->context);
12500   g_slice_free (AtkActionData, data);
12501 }
12502
12503 static void
12504 free_relation (AtkRelationData *data, gpointer user_data)
12505 {
12506   g_free (data->target);
12507   g_free (data->type);
12508   g_slice_free (AtkRelationData, data);
12509 }
12510
12511 static void
12512 gtk_widget_buildable_parser_finished (GtkBuildable *buildable,
12513                                       GtkBuilder   *builder)
12514 {
12515   GSList *atk_relations;
12516
12517   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_default))
12518     gtk_widget_grab_default (GTK_WIDGET (buildable));
12519   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_focus))
12520     gtk_widget_grab_focus (GTK_WIDGET (buildable));
12521
12522   atk_relations = g_object_get_qdata (G_OBJECT (buildable),
12523                                       quark_builder_atk_relations);
12524   if (atk_relations)
12525     {
12526       AtkObject *accessible;
12527       AtkRelationSet *relation_set;
12528       GSList *l;
12529       GObject *target;
12530       AtkRelationType relation_type;
12531       AtkObject *target_accessible;
12532
12533       accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
12534       relation_set = atk_object_ref_relation_set (accessible);
12535
12536       for (l = atk_relations; l; l = l->next)
12537         {
12538           AtkRelationData *relation = (AtkRelationData*)l->data;
12539
12540           target = gtk_builder_get_object (builder, relation->target);
12541           if (!target)
12542             {
12543               g_warning ("Target object %s in <relation> does not exist",
12544                          relation->target);
12545               continue;
12546             }
12547           target_accessible = gtk_widget_get_accessible (GTK_WIDGET (target));
12548           g_assert (target_accessible != NULL);
12549
12550           relation_type = atk_relation_type_for_name (relation->type);
12551           if (relation_type == ATK_RELATION_NULL)
12552             {
12553               g_warning ("<relation> type %s not found",
12554                          relation->type);
12555               continue;
12556             }
12557           atk_relation_set_add_relation_by_type (relation_set, relation_type,
12558                                                  target_accessible);
12559         }
12560       g_object_unref (relation_set);
12561
12562       g_slist_foreach (atk_relations, (GFunc)free_relation, NULL);
12563       g_slist_free (atk_relations);
12564       g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
12565                           NULL);
12566     }
12567 }
12568
12569 typedef struct
12570 {
12571   GSList *actions;
12572   GSList *relations;
12573 } AccessibilitySubParserData;
12574
12575 static void
12576 accessibility_start_element (GMarkupParseContext  *context,
12577                              const gchar          *element_name,
12578                              const gchar         **names,
12579                              const gchar         **values,
12580                              gpointer              user_data,
12581                              GError              **error)
12582 {
12583   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
12584   guint i;
12585   gint line_number, char_number;
12586
12587   if (strcmp (element_name, "relation") == 0)
12588     {
12589       gchar *target = NULL;
12590       gchar *type = NULL;
12591       AtkRelationData *relation;
12592
12593       for (i = 0; names[i]; i++)
12594         {
12595           if (strcmp (names[i], "target") == 0)
12596             target = g_strdup (values[i]);
12597           else if (strcmp (names[i], "type") == 0)
12598             type = g_strdup (values[i]);
12599           else
12600             {
12601               g_markup_parse_context_get_position (context,
12602                                                    &line_number,
12603                                                    &char_number);
12604               g_set_error (error,
12605                            GTK_BUILDER_ERROR,
12606                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
12607                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
12608                            "<input>",
12609                            line_number, char_number, names[i], "relation");
12610               g_free (target);
12611               g_free (type);
12612               return;
12613             }
12614         }
12615
12616       if (!target || !type)
12617         {
12618           g_markup_parse_context_get_position (context,
12619                                                &line_number,
12620                                                &char_number);
12621           g_set_error (error,
12622                        GTK_BUILDER_ERROR,
12623                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
12624                        "%s:%d:%d <%s> requires attribute \"%s\"",
12625                        "<input>",
12626                        line_number, char_number, "relation",
12627                        type ? "target" : "type");
12628           g_free (target);
12629           g_free (type);
12630           return;
12631         }
12632
12633       relation = g_slice_new (AtkRelationData);
12634       relation->target = target;
12635       relation->type = type;
12636
12637       data->relations = g_slist_prepend (data->relations, relation);
12638     }
12639   else if (strcmp (element_name, "action") == 0)
12640     {
12641       const gchar *action_name = NULL;
12642       const gchar *description = NULL;
12643       const gchar *msg_context = NULL;
12644       gboolean translatable = FALSE;
12645       AtkActionData *action;
12646
12647       for (i = 0; names[i]; i++)
12648         {
12649           if (strcmp (names[i], "action_name") == 0)
12650             action_name = values[i];
12651           else if (strcmp (names[i], "description") == 0)
12652             description = values[i];
12653           else if (strcmp (names[i], "translatable") == 0)
12654             {
12655               if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
12656                 return;
12657             }
12658           else if (strcmp (names[i], "comments") == 0)
12659             {
12660               /* do nothing, comments are for translators */
12661             }
12662           else if (strcmp (names[i], "context") == 0)
12663             msg_context = values[i];
12664           else
12665             {
12666               g_markup_parse_context_get_position (context,
12667                                                    &line_number,
12668                                                    &char_number);
12669               g_set_error (error,
12670                            GTK_BUILDER_ERROR,
12671                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
12672                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
12673                            "<input>",
12674                            line_number, char_number, names[i], "action");
12675               return;
12676             }
12677         }
12678
12679       if (!action_name)
12680         {
12681           g_markup_parse_context_get_position (context,
12682                                                &line_number,
12683                                                &char_number);
12684           g_set_error (error,
12685                        GTK_BUILDER_ERROR,
12686                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
12687                        "%s:%d:%d <%s> requires attribute \"%s\"",
12688                        "<input>",
12689                        line_number, char_number, "action",
12690                        "action_name");
12691           return;
12692         }
12693
12694       action = g_slice_new (AtkActionData);
12695       action->action_name = g_strdup (action_name);
12696       action->description = g_string_new (description);
12697       action->context = g_strdup (msg_context);
12698       action->translatable = translatable;
12699
12700       data->actions = g_slist_prepend (data->actions, action);
12701     }
12702   else if (strcmp (element_name, "accessibility") == 0)
12703     ;
12704   else
12705     g_warning ("Unsupported tag for GtkWidget: %s\n", element_name);
12706 }
12707
12708 static void
12709 accessibility_text (GMarkupParseContext  *context,
12710                     const gchar          *text,
12711                     gsize                 text_len,
12712                     gpointer              user_data,
12713                     GError              **error)
12714 {
12715   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
12716
12717   if (strcmp (g_markup_parse_context_get_element (context), "action") == 0)
12718     {
12719       AtkActionData *action = data->actions->data;
12720
12721       g_string_append_len (action->description, text, text_len);
12722     }
12723 }
12724
12725 static const GMarkupParser accessibility_parser =
12726   {
12727     accessibility_start_element,
12728     NULL,
12729     accessibility_text,
12730   };
12731
12732 typedef struct
12733 {
12734   GObject *object;
12735   guint    key;
12736   guint    modifiers;
12737   gchar   *signal;
12738 } AccelGroupParserData;
12739
12740 static void
12741 accel_group_start_element (GMarkupParseContext  *context,
12742                            const gchar          *element_name,
12743                            const gchar         **names,
12744                            const gchar         **values,
12745                            gpointer              user_data,
12746                            GError              **error)
12747 {
12748   gint i;
12749   guint key = 0;
12750   guint modifiers = 0;
12751   gchar *signal = NULL;
12752   AccelGroupParserData *parser_data = (AccelGroupParserData*)user_data;
12753
12754   for (i = 0; names[i]; i++)
12755     {
12756       if (strcmp (names[i], "key") == 0)
12757         key = gdk_keyval_from_name (values[i]);
12758       else if (strcmp (names[i], "modifiers") == 0)
12759         {
12760           if (!_gtk_builder_flags_from_string (GDK_TYPE_MODIFIER_TYPE,
12761                                                values[i],
12762                                                &modifiers,
12763                                                error))
12764               return;
12765         }
12766       else if (strcmp (names[i], "signal") == 0)
12767         signal = g_strdup (values[i]);
12768     }
12769
12770   if (key == 0 || signal == NULL)
12771     {
12772       g_warning ("<accelerator> requires key and signal attributes");
12773       return;
12774     }
12775   parser_data->key = key;
12776   parser_data->modifiers = modifiers;
12777   parser_data->signal = signal;
12778 }
12779
12780 static const GMarkupParser accel_group_parser =
12781   {
12782     accel_group_start_element,
12783   };
12784
12785 typedef struct
12786 {
12787   GSList *classes;
12788 } StyleParserData;
12789
12790 static void
12791 style_start_element (GMarkupParseContext  *context,
12792                      const gchar          *element_name,
12793                      const gchar         **names,
12794                      const gchar         **values,
12795                      gpointer              user_data,
12796                      GError              **error)
12797 {
12798   StyleParserData *style_data = (StyleParserData *)user_data;
12799   gchar *class_name;
12800
12801   if (strcmp (element_name, "class") == 0)
12802     {
12803       if (g_markup_collect_attributes (element_name,
12804                                        names,
12805                                        values,
12806                                        error,
12807                                        G_MARKUP_COLLECT_STRDUP, "name", &class_name,
12808                                        G_MARKUP_COLLECT_INVALID))
12809         {
12810           style_data->classes = g_slist_append (style_data->classes, class_name);
12811         }
12812     }
12813   else if (strcmp (element_name, "style") == 0)
12814     ;
12815   else
12816     g_warning ("Unsupported tag for GtkWidget: %s\n", element_name);
12817 }
12818
12819 static const GMarkupParser style_parser =
12820   {
12821     style_start_element,
12822   };
12823
12824 static gboolean
12825 gtk_widget_buildable_custom_tag_start (GtkBuildable     *buildable,
12826                                        GtkBuilder       *builder,
12827                                        GObject          *child,
12828                                        const gchar      *tagname,
12829                                        GMarkupParser    *parser,
12830                                        gpointer         *data)
12831 {
12832   g_assert (buildable);
12833
12834   if (strcmp (tagname, "accelerator") == 0)
12835     {
12836       AccelGroupParserData *parser_data;
12837
12838       parser_data = g_slice_new0 (AccelGroupParserData);
12839       parser_data->object = g_object_ref (buildable);
12840       *parser = accel_group_parser;
12841       *data = parser_data;
12842       return TRUE;
12843     }
12844   if (strcmp (tagname, "accessibility") == 0)
12845     {
12846       AccessibilitySubParserData *parser_data;
12847
12848       parser_data = g_slice_new0 (AccessibilitySubParserData);
12849       *parser = accessibility_parser;
12850       *data = parser_data;
12851       return TRUE;
12852     }
12853   if (strcmp (tagname, "style") == 0)
12854     {
12855       StyleParserData *parser_data;
12856
12857       parser_data = g_slice_new0 (StyleParserData);
12858       *parser = style_parser;
12859       *data = parser_data;
12860       return TRUE;
12861     }
12862
12863   return FALSE;
12864 }
12865
12866 void
12867 _gtk_widget_buildable_finish_accelerator (GtkWidget *widget,
12868                                           GtkWidget *toplevel,
12869                                           gpointer   user_data)
12870 {
12871   AccelGroupParserData *accel_data;
12872   GSList *accel_groups;
12873   GtkAccelGroup *accel_group;
12874
12875   g_return_if_fail (GTK_IS_WIDGET (widget));
12876   g_return_if_fail (GTK_IS_WIDGET (toplevel));
12877   g_return_if_fail (user_data != NULL);
12878
12879   accel_data = (AccelGroupParserData*)user_data;
12880   accel_groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
12881   if (g_slist_length (accel_groups) == 0)
12882     {
12883       accel_group = gtk_accel_group_new ();
12884       gtk_window_add_accel_group (GTK_WINDOW (toplevel), accel_group);
12885     }
12886   else
12887     {
12888       g_assert (g_slist_length (accel_groups) == 1);
12889       accel_group = g_slist_nth_data (accel_groups, 0);
12890     }
12891
12892   gtk_widget_add_accelerator (GTK_WIDGET (accel_data->object),
12893                               accel_data->signal,
12894                               accel_group,
12895                               accel_data->key,
12896                               accel_data->modifiers,
12897                               GTK_ACCEL_VISIBLE);
12898
12899   g_object_unref (accel_data->object);
12900   g_free (accel_data->signal);
12901   g_slice_free (AccelGroupParserData, accel_data);
12902 }
12903
12904 static void
12905 gtk_widget_buildable_custom_finished (GtkBuildable *buildable,
12906                                       GtkBuilder   *builder,
12907                                       GObject      *child,
12908                                       const gchar  *tagname,
12909                                       gpointer      user_data)
12910 {
12911   if (strcmp (tagname, "accelerator") == 0)
12912     {
12913       AccelGroupParserData *accel_data;
12914       GtkWidget *toplevel;
12915
12916       accel_data = (AccelGroupParserData*)user_data;
12917       g_assert (accel_data->object);
12918
12919       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (accel_data->object));
12920
12921       _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
12922     }
12923   else if (strcmp (tagname, "accessibility") == 0)
12924     {
12925       AccessibilitySubParserData *a11y_data;
12926
12927       a11y_data = (AccessibilitySubParserData*)user_data;
12928
12929       if (a11y_data->actions)
12930         {
12931           AtkObject *accessible;
12932           AtkAction *action;
12933           gint i, n_actions;
12934           GSList *l;
12935
12936           accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
12937
12938           if (ATK_IS_ACTION (accessible))
12939             {
12940               action = ATK_ACTION (accessible);
12941               n_actions = atk_action_get_n_actions (action);
12942
12943               for (l = a11y_data->actions; l; l = l->next)
12944                 {
12945                   AtkActionData *action_data = (AtkActionData*)l->data;
12946
12947                   for (i = 0; i < n_actions; i++)
12948                     if (strcmp (atk_action_get_name (action, i),
12949                                 action_data->action_name) == 0)
12950                       break;
12951
12952                   if (i < n_actions)
12953                     {
12954                       gchar *description;
12955
12956                       if (action_data->translatable && action_data->description->len)
12957                         description = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
12958                                                                      action_data->context,
12959                                                                      action_data->description->str);
12960                       else
12961                         description = action_data->description->str;
12962
12963                       atk_action_set_description (action, i, description);
12964                     }
12965                 }
12966             }
12967           else
12968             g_warning ("accessibility action on a widget that does not implement AtkAction");
12969
12970           g_slist_foreach (a11y_data->actions, (GFunc)free_action, NULL);
12971           g_slist_free (a11y_data->actions);
12972         }
12973
12974       if (a11y_data->relations)
12975         g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
12976                             a11y_data->relations);
12977
12978       g_slice_free (AccessibilitySubParserData, a11y_data);
12979     }
12980   else if (strcmp (tagname, "style") == 0)
12981     {
12982       StyleParserData *style_data = (StyleParserData *)user_data;
12983       GtkStyleContext *context;
12984       GSList *l;
12985
12986       context = gtk_widget_get_style_context (GTK_WIDGET (buildable));
12987
12988       for (l = style_data->classes; l; l = l->next)
12989         gtk_style_context_add_class (context, (const gchar *)l->data);
12990
12991       gtk_widget_reset_style (GTK_WIDGET (buildable));
12992
12993       g_slist_free_full (style_data->classes, g_free);
12994       g_slice_free (StyleParserData, style_data);
12995     }
12996 }
12997
12998 static GtkSizeRequestMode 
12999 gtk_widget_real_get_request_mode (GtkWidget *widget)
13000
13001   /* By default widgets dont trade size at all. */
13002   return GTK_SIZE_REQUEST_CONSTANT_SIZE;
13003 }
13004
13005 static void
13006 gtk_widget_real_get_width (GtkWidget *widget,
13007                            gint      *minimum_size,
13008                            gint      *natural_size)
13009 {
13010   if (minimum_size)
13011     *minimum_size = 0;
13012
13013   if (natural_size)
13014     *natural_size = 0;
13015 }
13016
13017 static void
13018 gtk_widget_real_get_height (GtkWidget *widget,
13019                             gint      *minimum_size,
13020                             gint      *natural_size)
13021 {
13022   if (minimum_size)
13023     *minimum_size = 0;
13024
13025   if (natural_size)
13026     *natural_size = 0;
13027 }
13028
13029 static void
13030 gtk_widget_real_get_height_for_width (GtkWidget *widget,
13031                                       gint       width,
13032                                       gint      *minimum_height,
13033                                       gint      *natural_height)
13034 {
13035   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
13036 }
13037
13038 static void
13039 gtk_widget_real_get_width_for_height (GtkWidget *widget,
13040                                       gint       height,
13041                                       gint      *minimum_width,
13042                                       gint      *natural_width)
13043 {
13044   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
13045 }
13046
13047 /**
13048  * gtk_widget_get_halign:
13049  * @widget: a #GtkWidget
13050  *
13051  * Gets the value of the #GtkWidget:halign property.
13052  *
13053  * Returns: the horizontal alignment of @widget
13054  */
13055 GtkAlign
13056 gtk_widget_get_halign (GtkWidget *widget)
13057 {
13058   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_ALIGN_FILL);
13059   return _gtk_widget_get_aux_info_or_defaults (widget)->halign;
13060 }
13061
13062 /**
13063  * gtk_widget_set_halign:
13064  * @widget: a #GtkWidget
13065  * @align: the horizontal alignment
13066  *
13067  * Sets the horizontal alignment of @widget.
13068  * See the #GtkWidget:halign property.
13069  */
13070 void
13071 gtk_widget_set_halign (GtkWidget *widget,
13072                        GtkAlign   align)
13073 {
13074   GtkWidgetAuxInfo *aux_info;
13075
13076   g_return_if_fail (GTK_IS_WIDGET (widget));
13077
13078   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
13079
13080   if (aux_info->halign == align)
13081     return;
13082
13083   aux_info->halign = align;
13084   gtk_widget_queue_resize (widget);
13085   g_object_notify (G_OBJECT (widget), "halign");
13086 }
13087
13088 /**
13089  * gtk_widget_get_valign:
13090  * @widget: a #GtkWidget
13091  *
13092  * Gets the value of the #GtkWidget:valign property.
13093  *
13094  * Returns: the vertical alignment of @widget
13095  */
13096 GtkAlign
13097 gtk_widget_get_valign (GtkWidget *widget)
13098 {
13099   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_ALIGN_FILL);
13100   return _gtk_widget_get_aux_info_or_defaults (widget)->valign;
13101 }
13102
13103 /**
13104  * gtk_widget_set_valign:
13105  * @widget: a #GtkWidget
13106  * @align: the vertical alignment
13107  *
13108  * Sets the vertical alignment of @widget.
13109  * See the #GtkWidget:valign property.
13110  */
13111 void
13112 gtk_widget_set_valign (GtkWidget *widget,
13113                        GtkAlign   align)
13114 {
13115   GtkWidgetAuxInfo *aux_info;
13116
13117   g_return_if_fail (GTK_IS_WIDGET (widget));
13118
13119   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
13120
13121   if (aux_info->valign == align)
13122     return;
13123
13124   aux_info->valign = align;
13125   gtk_widget_queue_resize (widget);
13126   g_object_notify (G_OBJECT (widget), "valign");
13127 }
13128
13129 /**
13130  * gtk_widget_get_margin_left:
13131  * @widget: a #GtkWidget
13132  *
13133  * Gets the value of the #GtkWidget:margin-left property.
13134  *
13135  * Returns: The left margin of @widget
13136  */
13137 gint
13138 gtk_widget_get_margin_left (GtkWidget *widget)
13139 {
13140   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13141
13142   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.left;
13143 }
13144
13145 /**
13146  * gtk_widget_set_margin_left:
13147  * @widget: a #GtkWidget
13148  * @margin: the left margin
13149  *
13150  * Sets the left margin of @widget.
13151  * See the #GtkWidget:margin-left property.
13152  */
13153 void
13154 gtk_widget_set_margin_left (GtkWidget *widget,
13155                             gint       margin)
13156 {
13157   GtkWidgetAuxInfo *aux_info;
13158
13159   g_return_if_fail (GTK_IS_WIDGET (widget));
13160   g_return_if_fail (margin <= G_MAXINT16);
13161
13162   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
13163
13164   if (aux_info->margin.left == margin)
13165     return;
13166
13167   aux_info->margin.left = margin;
13168   gtk_widget_queue_resize (widget);
13169   g_object_notify (G_OBJECT (widget), "margin-left");
13170 }
13171
13172 /**
13173  * gtk_widget_get_margin_right:
13174  * @widget: a #GtkWidget
13175  *
13176  * Gets the value of the #GtkWidget:margin-right property.
13177  *
13178  * Returns: The left margin of @widget
13179  */
13180 gint
13181 gtk_widget_get_margin_right (GtkWidget *widget)
13182 {
13183   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13184
13185   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.right;
13186 }
13187
13188 /**
13189  * gtk_widget_set_margin_right:
13190  * @widget: a #GtkWidget
13191  * @margin: the right margin
13192  *
13193  * Sets the right margin of @widget.
13194  * See the #GtkWidget:margin-right property.
13195  */
13196 void
13197 gtk_widget_set_margin_right (GtkWidget *widget,
13198                              gint       margin)
13199 {
13200   GtkWidgetAuxInfo *aux_info;
13201
13202   g_return_if_fail (GTK_IS_WIDGET (widget));
13203   g_return_if_fail (margin <= G_MAXINT16);
13204
13205   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
13206
13207   if (aux_info->margin.right == margin)
13208     return;
13209
13210   aux_info->margin.right = margin;
13211   gtk_widget_queue_resize (widget);
13212   g_object_notify (G_OBJECT (widget), "margin-right");
13213 }
13214
13215 /**
13216  * gtk_widget_get_margin_top:
13217  * @widget: a #GtkWidget
13218  *
13219  * Gets the value of the #GtkWidget:margin-top property.
13220  *
13221  * Returns: The top margin of @widget
13222  */
13223 gint
13224 gtk_widget_get_margin_top (GtkWidget *widget)
13225 {
13226   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13227
13228   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.top;
13229 }
13230
13231 /**
13232  * gtk_widget_set_margin_top:
13233  * @widget: a #GtkWidget
13234  * @margin: the top margin
13235  *
13236  * Sets the top margin of @widget.
13237  * See the #GtkWidget:margin-top property.
13238  */
13239 void
13240 gtk_widget_set_margin_top (GtkWidget *widget,
13241                            gint       margin)
13242 {
13243   GtkWidgetAuxInfo *aux_info;
13244
13245   g_return_if_fail (GTK_IS_WIDGET (widget));
13246   g_return_if_fail (margin <= G_MAXINT16);
13247
13248   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
13249
13250   if (aux_info->margin.top == margin)
13251     return;
13252
13253   aux_info->margin.top = margin;
13254   gtk_widget_queue_resize (widget);
13255   g_object_notify (G_OBJECT (widget), "margin-top");
13256 }
13257
13258 /**
13259  * gtk_widget_get_margin_bottom:
13260  * @widget: a #GtkWidget
13261  *
13262  * Gets the value of the #GtkWidget:margin-bottom property.
13263  *
13264  * Returns: The bottom margin of @widget
13265  */
13266 gint
13267 gtk_widget_get_margin_bottom (GtkWidget *widget)
13268 {
13269   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13270
13271   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.bottom;
13272 }
13273
13274 /**
13275  * gtk_widget_set_margin_bottom:
13276  * @widget: a #GtkWidget
13277  * @margin: the bottom margin
13278  *
13279  * Sets the bottom margin of @widget.
13280  * See the #GtkWidget:margin-bottom property.
13281  */
13282 void
13283 gtk_widget_set_margin_bottom (GtkWidget *widget,
13284                               gint       margin)
13285 {
13286   GtkWidgetAuxInfo *aux_info;
13287
13288   g_return_if_fail (GTK_IS_WIDGET (widget));
13289   g_return_if_fail (margin <= G_MAXINT16);
13290
13291   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
13292
13293   if (aux_info->margin.bottom == margin)
13294     return;
13295
13296   aux_info->margin.bottom = margin;
13297   gtk_widget_queue_resize (widget);
13298   g_object_notify (G_OBJECT (widget), "margin-bottom");
13299 }
13300
13301 /**
13302  * gtk_widget_get_clipboard:
13303  * @widget: a #GtkWidget
13304  * @selection: a #GdkAtom which identifies the clipboard
13305  *             to use. %GDK_SELECTION_CLIPBOARD gives the
13306  *             default clipboard. Another common value
13307  *             is %GDK_SELECTION_PRIMARY, which gives
13308  *             the primary X selection.
13309  *
13310  * Returns the clipboard object for the given selection to
13311  * be used with @widget. @widget must have a #GdkDisplay
13312  * associated with it, so must be attached to a toplevel
13313  * window.
13314  *
13315  * Return value: (transfer none): the appropriate clipboard object. If no
13316  *             clipboard already exists, a new one will
13317  *             be created. Once a clipboard object has
13318  *             been created, it is persistent for all time.
13319  *
13320  * Since: 2.2
13321  **/
13322 GtkClipboard *
13323 gtk_widget_get_clipboard (GtkWidget *widget, GdkAtom selection)
13324 {
13325   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13326   g_return_val_if_fail (gtk_widget_has_screen (widget), NULL);
13327
13328   return gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
13329                                         selection);
13330 }
13331
13332 /**
13333  * gtk_widget_list_mnemonic_labels:
13334  * @widget: a #GtkWidget
13335  *
13336  * Returns a newly allocated list of the widgets, normally labels, for
13337  * which this widget is the target of a mnemonic (see for example,
13338  * gtk_label_set_mnemonic_widget()).
13339
13340  * The widgets in the list are not individually referenced. If you
13341  * want to iterate through the list and perform actions involving
13342  * callbacks that might destroy the widgets, you
13343  * <emphasis>must</emphasis> call <literal>g_list_foreach (result,
13344  * (GFunc)g_object_ref, NULL)</literal> first, and then unref all the
13345  * widgets afterwards.
13346
13347  * Return value: (element-type GtkWidget) (transfer container): the list of
13348  *  mnemonic labels; free this list
13349  *  with g_list_free() when you are done with it.
13350  *
13351  * Since: 2.4
13352  **/
13353 GList *
13354 gtk_widget_list_mnemonic_labels (GtkWidget *widget)
13355 {
13356   GList *list = NULL;
13357   GSList *l;
13358
13359   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13360
13361   for (l = g_object_get_qdata (G_OBJECT (widget), quark_mnemonic_labels); l; l = l->next)
13362     list = g_list_prepend (list, l->data);
13363
13364   return list;
13365 }
13366
13367 /**
13368  * gtk_widget_add_mnemonic_label:
13369  * @widget: a #GtkWidget
13370  * @label: a #GtkWidget that acts as a mnemonic label for @widget
13371  *
13372  * Adds a widget to the list of mnemonic labels for
13373  * this widget. (See gtk_widget_list_mnemonic_labels()). Note the
13374  * list of mnemonic labels for the widget is cleared when the
13375  * widget is destroyed, so the caller must make sure to update
13376  * its internal state at this point as well, by using a connection
13377  * to the #GtkWidget::destroy signal or a weak notifier.
13378  *
13379  * Since: 2.4
13380  **/
13381 void
13382 gtk_widget_add_mnemonic_label (GtkWidget *widget,
13383                                GtkWidget *label)
13384 {
13385   GSList *old_list, *new_list;
13386
13387   g_return_if_fail (GTK_IS_WIDGET (widget));
13388   g_return_if_fail (GTK_IS_WIDGET (label));
13389
13390   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
13391   new_list = g_slist_prepend (old_list, label);
13392
13393   g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
13394                            new_list, (GDestroyNotify) g_slist_free);
13395 }
13396
13397 /**
13398  * gtk_widget_remove_mnemonic_label:
13399  * @widget: a #GtkWidget
13400  * @label: a #GtkWidget that was previously set as a mnemnic label for
13401  *         @widget with gtk_widget_add_mnemonic_label().
13402  *
13403  * Removes a widget from the list of mnemonic labels for
13404  * this widget. (See gtk_widget_list_mnemonic_labels()). The widget
13405  * must have previously been added to the list with
13406  * gtk_widget_add_mnemonic_label().
13407  *
13408  * Since: 2.4
13409  **/
13410 void
13411 gtk_widget_remove_mnemonic_label (GtkWidget *widget,
13412                                   GtkWidget *label)
13413 {
13414   GSList *old_list, *new_list;
13415
13416   g_return_if_fail (GTK_IS_WIDGET (widget));
13417   g_return_if_fail (GTK_IS_WIDGET (label));
13418
13419   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
13420   new_list = g_slist_remove (old_list, label);
13421
13422   if (new_list)
13423     g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
13424                              new_list, (GDestroyNotify) g_slist_free);
13425 }
13426
13427 /**
13428  * gtk_widget_get_no_show_all:
13429  * @widget: a #GtkWidget
13430  *
13431  * Returns the current value of the #GtkWidget:no-show-all property,
13432  * which determines whether calls to gtk_widget_show_all()
13433  * will affect this widget.
13434  *
13435  * Return value: the current value of the "no-show-all" property.
13436  *
13437  * Since: 2.4
13438  **/
13439 gboolean
13440 gtk_widget_get_no_show_all (GtkWidget *widget)
13441 {
13442   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
13443
13444   return widget->priv->no_show_all;
13445 }
13446
13447 /**
13448  * gtk_widget_set_no_show_all:
13449  * @widget: a #GtkWidget
13450  * @no_show_all: the new value for the "no-show-all" property
13451  *
13452  * Sets the #GtkWidget:no-show-all property, which determines whether
13453  * calls to gtk_widget_show_all() will affect this widget.
13454  *
13455  * This is mostly for use in constructing widget hierarchies with externally
13456  * controlled visibility, see #GtkUIManager.
13457  *
13458  * Since: 2.4
13459  **/
13460 void
13461 gtk_widget_set_no_show_all (GtkWidget *widget,
13462                             gboolean   no_show_all)
13463 {
13464   g_return_if_fail (GTK_IS_WIDGET (widget));
13465
13466   no_show_all = (no_show_all != FALSE);
13467
13468   if (widget->priv->no_show_all != no_show_all)
13469     {
13470       widget->priv->no_show_all = no_show_all;
13471
13472       g_object_notify (G_OBJECT (widget), "no-show-all");
13473     }
13474 }
13475
13476
13477 static void
13478 gtk_widget_real_set_has_tooltip (GtkWidget *widget,
13479                                  gboolean   has_tooltip,
13480                                  gboolean   force)
13481 {
13482   GtkWidgetPrivate *priv = widget->priv;
13483   gboolean priv_has_tooltip;
13484
13485   priv_has_tooltip = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget),
13486                                        quark_has_tooltip));
13487
13488   if (priv_has_tooltip != has_tooltip || force)
13489     {
13490       priv_has_tooltip = has_tooltip;
13491
13492       if (priv_has_tooltip)
13493         {
13494           if (gtk_widget_get_realized (widget) && !gtk_widget_get_has_window (widget))
13495             gdk_window_set_events (priv->window,
13496                                    gdk_window_get_events (priv->window) |
13497                                    GDK_LEAVE_NOTIFY_MASK |
13498                                    GDK_POINTER_MOTION_MASK |
13499                                    GDK_POINTER_MOTION_HINT_MASK);
13500
13501           if (gtk_widget_get_has_window (widget))
13502               gtk_widget_add_events (widget,
13503                                      GDK_LEAVE_NOTIFY_MASK |
13504                                      GDK_POINTER_MOTION_MASK |
13505                                      GDK_POINTER_MOTION_HINT_MASK);
13506         }
13507
13508       g_object_set_qdata (G_OBJECT (widget), quark_has_tooltip,
13509                           GUINT_TO_POINTER (priv_has_tooltip));
13510     }
13511 }
13512
13513 /**
13514  * gtk_widget_set_tooltip_window:
13515  * @widget: a #GtkWidget
13516  * @custom_window: (allow-none): a #GtkWindow, or %NULL
13517  *
13518  * Replaces the default, usually yellow, window used for displaying
13519  * tooltips with @custom_window. GTK+ will take care of showing and
13520  * hiding @custom_window at the right moment, to behave likewise as
13521  * the default tooltip window. If @custom_window is %NULL, the default
13522  * tooltip window will be used.
13523  *
13524  * If the custom window should have the default theming it needs to
13525  * have the name "gtk-tooltip", see gtk_widget_set_name().
13526  *
13527  * Since: 2.12
13528  */
13529 void
13530 gtk_widget_set_tooltip_window (GtkWidget *widget,
13531                                GtkWindow *custom_window)
13532 {
13533   gboolean has_tooltip;
13534   gchar *tooltip_markup;
13535
13536   g_return_if_fail (GTK_IS_WIDGET (widget));
13537   g_return_if_fail (custom_window == NULL || GTK_IS_WINDOW (custom_window));
13538
13539   tooltip_markup = g_object_get_qdata (G_OBJECT (widget), quark_tooltip_markup);
13540
13541   if (custom_window)
13542     g_object_ref (custom_window);
13543
13544   g_object_set_qdata_full (G_OBJECT (widget), quark_tooltip_window,
13545                            custom_window, g_object_unref);
13546
13547   has_tooltip = (custom_window != NULL || tooltip_markup != NULL);
13548   gtk_widget_real_set_has_tooltip (widget, has_tooltip, FALSE);
13549
13550   if (has_tooltip && gtk_widget_get_visible (widget))
13551     gtk_widget_queue_tooltip_query (widget);
13552 }
13553
13554 /**
13555  * gtk_widget_get_tooltip_window:
13556  * @widget: a #GtkWidget
13557  *
13558  * Returns the #GtkWindow of the current tooltip. This can be the
13559  * GtkWindow created by default, or the custom tooltip window set
13560  * using gtk_widget_set_tooltip_window().
13561  *
13562  * Return value: (transfer none): The #GtkWindow of the current tooltip.
13563  *
13564  * Since: 2.12
13565  */
13566 GtkWindow *
13567 gtk_widget_get_tooltip_window (GtkWidget *widget)
13568 {
13569   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13570
13571   return g_object_get_qdata (G_OBJECT (widget), quark_tooltip_window);
13572 }
13573
13574 /**
13575  * gtk_widget_trigger_tooltip_query:
13576  * @widget: a #GtkWidget
13577  *
13578  * Triggers a tooltip query on the display where the toplevel of @widget
13579  * is located. See gtk_tooltip_trigger_tooltip_query() for more
13580  * information.
13581  *
13582  * Since: 2.12
13583  */
13584 void
13585 gtk_widget_trigger_tooltip_query (GtkWidget *widget)
13586 {
13587   gtk_tooltip_trigger_tooltip_query (gtk_widget_get_display (widget));
13588 }
13589
13590 static guint tooltip_query_id;
13591 static GSList *tooltip_query_displays;
13592
13593 static gboolean
13594 tooltip_query_idle (gpointer data)
13595 {
13596   g_slist_foreach (tooltip_query_displays, (GFunc)gtk_tooltip_trigger_tooltip_query, NULL);
13597   g_slist_foreach (tooltip_query_displays, (GFunc)g_object_unref, NULL);
13598   g_slist_free (tooltip_query_displays);
13599
13600   tooltip_query_displays = NULL;
13601   tooltip_query_id = 0;
13602
13603   return FALSE;
13604 }
13605
13606 static void
13607 gtk_widget_queue_tooltip_query (GtkWidget *widget)
13608 {
13609   GdkDisplay *display;
13610
13611   display = gtk_widget_get_display (widget);
13612
13613   if (!g_slist_find (tooltip_query_displays, display))
13614     tooltip_query_displays = g_slist_prepend (tooltip_query_displays, g_object_ref (display));
13615
13616   if (tooltip_query_id == 0)
13617     tooltip_query_id = gdk_threads_add_idle (tooltip_query_idle, NULL);
13618 }
13619
13620 /**
13621  * gtk_widget_set_tooltip_text:
13622  * @widget: a #GtkWidget
13623  * @text: the contents of the tooltip for @widget
13624  *
13625  * Sets @text as the contents of the tooltip. This function will take
13626  * care of setting #GtkWidget:has-tooltip to %TRUE and of the default
13627  * handler for the #GtkWidget::query-tooltip signal.
13628  *
13629  * See also the #GtkWidget:tooltip-text property and gtk_tooltip_set_text().
13630  *
13631  * Since: 2.12
13632  */
13633 void
13634 gtk_widget_set_tooltip_text (GtkWidget   *widget,
13635                              const gchar *text)
13636 {
13637   g_return_if_fail (GTK_IS_WIDGET (widget));
13638
13639   g_object_set (G_OBJECT (widget), "tooltip-text", text, NULL);
13640 }
13641
13642 /**
13643  * gtk_widget_get_tooltip_text:
13644  * @widget: a #GtkWidget
13645  *
13646  * Gets the contents of the tooltip for @widget.
13647  *
13648  * Return value: the tooltip text, or %NULL. You should free the
13649  *   returned string with g_free() when done.
13650  *
13651  * Since: 2.12
13652  */
13653 gchar *
13654 gtk_widget_get_tooltip_text (GtkWidget *widget)
13655 {
13656   gchar *text = NULL;
13657
13658   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13659
13660   g_object_get (G_OBJECT (widget), "tooltip-text", &text, NULL);
13661
13662   return text;
13663 }
13664
13665 /**
13666  * gtk_widget_set_tooltip_markup:
13667  * @widget: a #GtkWidget
13668  * @markup: (allow-none): the contents of the tooltip for @widget, or %NULL
13669  *
13670  * Sets @markup as the contents of the tooltip, which is marked up with
13671  *  the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
13672  *
13673  * This function will take care of setting #GtkWidget:has-tooltip to %TRUE
13674  * and of the default handler for the #GtkWidget::query-tooltip signal.
13675  *
13676  * See also the #GtkWidget:tooltip-markup property and
13677  * gtk_tooltip_set_markup().
13678  *
13679  * Since: 2.12
13680  */
13681 void
13682 gtk_widget_set_tooltip_markup (GtkWidget   *widget,
13683                                const gchar *markup)
13684 {
13685   g_return_if_fail (GTK_IS_WIDGET (widget));
13686
13687   g_object_set (G_OBJECT (widget), "tooltip-markup", markup, NULL);
13688 }
13689
13690 /**
13691  * gtk_widget_get_tooltip_markup:
13692  * @widget: a #GtkWidget
13693  *
13694  * Gets the contents of the tooltip for @widget.
13695  *
13696  * Return value: the tooltip text, or %NULL. You should free the
13697  *   returned string with g_free() when done.
13698  *
13699  * Since: 2.12
13700  */
13701 gchar *
13702 gtk_widget_get_tooltip_markup (GtkWidget *widget)
13703 {
13704   gchar *text = NULL;
13705
13706   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13707
13708   g_object_get (G_OBJECT (widget), "tooltip-markup", &text, NULL);
13709
13710   return text;
13711 }
13712
13713 /**
13714  * gtk_widget_set_has_tooltip:
13715  * @widget: a #GtkWidget
13716  * @has_tooltip: whether or not @widget has a tooltip.
13717  *
13718  * Sets the has-tooltip property on @widget to @has_tooltip.  See
13719  * #GtkWidget:has-tooltip for more information.
13720  *
13721  * Since: 2.12
13722  */
13723 void
13724 gtk_widget_set_has_tooltip (GtkWidget *widget,
13725                             gboolean   has_tooltip)
13726 {
13727   g_return_if_fail (GTK_IS_WIDGET (widget));
13728
13729   g_object_set (G_OBJECT (widget), "has-tooltip", has_tooltip, NULL);
13730 }
13731
13732 /**
13733  * gtk_widget_get_has_tooltip:
13734  * @widget: a #GtkWidget
13735  *
13736  * Returns the current value of the has-tooltip property.  See
13737  * #GtkWidget:has-tooltip for more information.
13738  *
13739  * Return value: current value of has-tooltip on @widget.
13740  *
13741  * Since: 2.12
13742  */
13743 gboolean
13744 gtk_widget_get_has_tooltip (GtkWidget *widget)
13745 {
13746   gboolean has_tooltip = FALSE;
13747
13748   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
13749
13750   g_object_get (G_OBJECT (widget), "has-tooltip", &has_tooltip, NULL);
13751
13752   return has_tooltip;
13753 }
13754
13755 /**
13756  * gtk_widget_get_allocation:
13757  * @widget: a #GtkWidget
13758  * @allocation: (out): a pointer to a #GtkAllocation to copy to
13759  *
13760  * Retrieves the widget's allocation.
13761  *
13762  * Note, when implementing a #GtkContainer: a widget's allocation will
13763  * be its "adjusted" allocation, that is, the widget's parent
13764  * container typically calls gtk_widget_size_allocate() with an
13765  * allocation, and that allocation is then adjusted (to handle margin
13766  * and alignment for example) before assignment to the widget.
13767  * gtk_widget_get_allocation() returns the adjusted allocation that
13768  * was actually assigned to the widget. The adjusted allocation is
13769  * guaranteed to be completely contained within the
13770  * gtk_widget_size_allocate() allocation, however. So a #GtkContainer
13771  * is guaranteed that its children stay inside the assigned bounds,
13772  * but not that they have exactly the bounds the container assigned.
13773  * There is no way to get the original allocation assigned by
13774  * gtk_widget_size_allocate(), since it isn't stored; if a container
13775  * implementation needs that information it will have to track it itself.
13776  *
13777  * Since: 2.18
13778  */
13779 void
13780 gtk_widget_get_allocation (GtkWidget     *widget,
13781                            GtkAllocation *allocation)
13782 {
13783   GtkWidgetPrivate *priv;
13784
13785   g_return_if_fail (GTK_IS_WIDGET (widget));
13786   g_return_if_fail (allocation != NULL);
13787
13788   priv = widget->priv;
13789
13790   *allocation = priv->allocation;
13791 }
13792
13793 /**
13794  * gtk_widget_set_allocation:
13795  * @widget: a #GtkWidget
13796  * @allocation: a pointer to a #GtkAllocation to copy from
13797  *
13798  * Sets the widget's allocation.  This should not be used
13799  * directly, but from within a widget's size_allocate method.
13800  *
13801  * The allocation set should be the "adjusted" or actual
13802  * allocation. If you're implementing a #GtkContainer, you want to use
13803  * gtk_widget_size_allocate() instead of gtk_widget_set_allocation().
13804  * The GtkWidgetClass::adjust_size_allocation virtual method adjusts the
13805  * allocation inside gtk_widget_size_allocate() to create an adjusted
13806  * allocation.
13807  *
13808  * Since: 2.18
13809  */
13810 void
13811 gtk_widget_set_allocation (GtkWidget           *widget,
13812                            const GtkAllocation *allocation)
13813 {
13814   GtkWidgetPrivate *priv;
13815
13816   g_return_if_fail (GTK_IS_WIDGET (widget));
13817   g_return_if_fail (allocation != NULL);
13818
13819   priv = widget->priv;
13820
13821   priv->allocation = *allocation;
13822 }
13823
13824 /**
13825  * gtk_widget_get_allocated_width:
13826  * @widget: the widget to query
13827  *
13828  * Returns the width that has currently been allocated to @widget.
13829  * This function is intended to be used when implementing handlers
13830  * for the #GtkWidget::draw function.
13831  *
13832  * Returns: the width of the @widget
13833  **/
13834 int
13835 gtk_widget_get_allocated_width (GtkWidget *widget)
13836 {
13837   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13838
13839   return widget->priv->allocation.width;
13840 }
13841
13842 /**
13843  * gtk_widget_get_allocated_height:
13844  * @widget: the widget to query
13845  *
13846  * Returns the height that has currently been allocated to @widget.
13847  * This function is intended to be used when implementing handlers
13848  * for the #GtkWidget::draw function.
13849  *
13850  * Returns: the height of the @widget
13851  **/
13852 int
13853 gtk_widget_get_allocated_height (GtkWidget *widget)
13854 {
13855   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
13856
13857   return widget->priv->allocation.height;
13858 }
13859
13860 /**
13861  * gtk_widget_get_requisition:
13862  * @widget: a #GtkWidget
13863  * @requisition: (out): a pointer to a #GtkRequisition to copy to
13864  *
13865  * Retrieves the widget's requisition.
13866  *
13867  * This function should only be used by widget implementations in
13868  * order to figure whether the widget's requisition has actually
13869  * changed after some internal state change (so that they can call
13870  * gtk_widget_queue_resize() instead of gtk_widget_queue_draw()).
13871  *
13872  * Normally, gtk_widget_size_request() should be used.
13873  *
13874  * Since: 2.20
13875  *
13876  * Deprecated: 3.0: The #GtkRequisition cache on the widget was
13877  * removed, If you need to cache sizes across requests and allocations,
13878  * add an explicit cache to the widget in question instead.
13879  */
13880 void
13881 gtk_widget_get_requisition (GtkWidget      *widget,
13882                             GtkRequisition *requisition)
13883 {
13884   g_return_if_fail (GTK_IS_WIDGET (widget));
13885   g_return_if_fail (requisition != NULL);
13886
13887   gtk_widget_get_preferred_size (widget, requisition, NULL);
13888 }
13889
13890 /**
13891  * gtk_widget_set_window:
13892  * @widget: a #GtkWidget
13893  * @window: a #GdkWindow
13894  *
13895  * Sets a widget's window. This function should only be used in a
13896  * widget's #GtkWidget::realize implementation. The %window passed is
13897  * usually either new window created with gdk_window_new(), or the
13898  * window of its parent widget as returned by
13899  * gtk_widget_get_parent_window().
13900  *
13901  * Widgets must indicate whether they will create their own #GdkWindow
13902  * by calling gtk_widget_set_has_window(). This is usually done in the
13903  * widget's init() function.
13904  *
13905  * <note><para>This function does not add any reference to @window.</para></note>
13906  *
13907  * Since: 2.18
13908  */
13909 void
13910 gtk_widget_set_window (GtkWidget *widget,
13911                        GdkWindow *window)
13912 {
13913   GtkWidgetPrivate *priv;
13914
13915   g_return_if_fail (GTK_IS_WIDGET (widget));
13916   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
13917
13918   priv = widget->priv;
13919
13920   if (priv->window != window)
13921     {
13922       priv->window = window;
13923       g_object_notify (G_OBJECT (widget), "window");
13924     }
13925 }
13926
13927 /**
13928  * gtk_widget_get_window:
13929  * @widget: a #GtkWidget
13930  *
13931  * Returns the widget's window if it is realized, %NULL otherwise
13932  *
13933  * Return value: (transfer none): @widget's window.
13934  *
13935  * Since: 2.14
13936  */
13937 GdkWindow*
13938 gtk_widget_get_window (GtkWidget *widget)
13939 {
13940   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13941
13942   return widget->priv->window;
13943 }
13944
13945 /**
13946  * gtk_widget_get_support_multidevice:
13947  * @widget: a #GtkWidget
13948  *
13949  * Returns %TRUE if @widget is multiple pointer aware. See
13950  * gtk_widget_set_support_multidevice() for more information.
13951  *
13952  * Returns: %TRUE if @widget is multidevice aware.
13953  **/
13954 gboolean
13955 gtk_widget_get_support_multidevice (GtkWidget *widget)
13956 {
13957   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
13958
13959   return widget->priv->multidevice;
13960 }
13961
13962 /**
13963  * gtk_widget_set_support_multidevice:
13964  * @widget: a #GtkWidget
13965  * @support_multidevice: %TRUE to support input from multiple devices.
13966  *
13967  * Enables or disables multiple pointer awareness. If this setting is %TRUE,
13968  * @widget will start receiving multiple, per device enter/leave events. Note
13969  * that if custom #GdkWindow<!-- -->s are created in #GtkWidget::realize,
13970  * gdk_window_set_support_multidevice() will have to be called manually on them.
13971  *
13972  * Since: 3.0
13973  **/
13974 void
13975 gtk_widget_set_support_multidevice (GtkWidget *widget,
13976                                     gboolean   support_multidevice)
13977 {
13978   GtkWidgetPrivate *priv;
13979
13980   g_return_if_fail (GTK_IS_WIDGET (widget));
13981
13982   priv = widget->priv;
13983   priv->multidevice = (support_multidevice == TRUE);
13984
13985   if (gtk_widget_get_realized (widget))
13986     gdk_window_set_support_multidevice (priv->window, support_multidevice);
13987 }
13988
13989 static void
13990 _gtk_widget_set_has_focus (GtkWidget *widget,
13991                            gboolean   has_focus)
13992 {
13993   widget->priv->has_focus = has_focus;
13994
13995   if (has_focus)
13996     gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_FOCUSED, FALSE);
13997   else
13998     gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_FOCUSED);
13999 }
14000
14001 /**
14002  * gtk_widget_send_focus_change:
14003  * @widget: a #GtkWidget
14004  * @event: a #GdkEvent of type GDK_FOCUS_CHANGE
14005  *
14006  * Sends the focus change @event to @widget
14007  *
14008  * This function is not meant to be used by applications. The only time it
14009  * should be used is when it is necessary for a #GtkWidget to assign focus
14010  * to a widget that is semantically owned by the first widget even though
14011  * it's not a direct child - for instance, a search entry in a floating
14012  * window similar to the quick search in #GtkTreeView.
14013  *
14014  * An example of its usage is:
14015  *
14016  * |[
14017  *   GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
14018  *
14019  *   fevent->focus_change.type = GDK_FOCUS_CHANGE;
14020  *   fevent->focus_change.in = TRUE;
14021  *   fevent->focus_change.window = gtk_widget_get_window (widget);
14022  *   if (fevent->focus_change.window != NULL)
14023  *     g_object_ref (fevent->focus_change.window);
14024  *
14025  *   gtk_widget_send_focus_change (widget, fevent);
14026  *
14027  *   gdk_event_free (event);
14028  * ]|
14029  *
14030  * Return value: the return value from the event signal emission: %TRUE
14031  *   if the event was handled, and %FALSE otherwise
14032  *
14033  * Since: 2.20
14034  */
14035 gboolean
14036 gtk_widget_send_focus_change (GtkWidget *widget,
14037                               GdkEvent  *event)
14038 {
14039   gboolean res;
14040
14041   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
14042   g_return_val_if_fail (event != NULL && event->type == GDK_FOCUS_CHANGE, FALSE);
14043
14044   g_object_ref (widget);
14045
14046   _gtk_widget_set_has_focus (widget, event->focus_change.in);
14047
14048   res = gtk_widget_event (widget, event);
14049
14050   g_object_notify (G_OBJECT (widget), "has-focus");
14051
14052   g_object_unref (widget);
14053
14054   return res;
14055 }
14056
14057 /**
14058  * gtk_widget_in_destruction:
14059  * @widget: a #GtkWidget
14060  *
14061  * Returns whether the widget is currently being destroyed.
14062  * This information can sometimes be used to avoid doing
14063  * unnecessary work.
14064  *
14065  * Returns: %TRUE if @widget is being destroyed
14066  */
14067 gboolean
14068 gtk_widget_in_destruction (GtkWidget *widget)
14069 {
14070   return widget->priv->in_destruction;
14071 }
14072
14073 gboolean
14074 _gtk_widget_get_resize_pending (GtkWidget *widget)
14075 {
14076   return widget->priv->resize_pending;
14077 }
14078
14079 void
14080 _gtk_widget_set_resize_pending (GtkWidget *widget,
14081                                 gboolean   resize_pending)
14082 {
14083   widget->priv->resize_pending = resize_pending;
14084 }
14085
14086 gboolean
14087 _gtk_widget_get_in_reparent (GtkWidget *widget)
14088 {
14089   return widget->priv->in_reparent;
14090 }
14091
14092 void
14093 _gtk_widget_set_in_reparent (GtkWidget *widget,
14094                              gboolean   in_reparent)
14095 {
14096   widget->priv->in_reparent = in_reparent;
14097 }
14098
14099 gboolean
14100 _gtk_widget_get_anchored (GtkWidget *widget)
14101 {
14102   return widget->priv->anchored;
14103 }
14104
14105 void
14106 _gtk_widget_set_anchored (GtkWidget *widget,
14107                           gboolean   anchored)
14108 {
14109   widget->priv->anchored = anchored;
14110 }
14111
14112 gboolean
14113 _gtk_widget_get_shadowed (GtkWidget *widget)
14114 {
14115   return widget->priv->shadowed;
14116 }
14117
14118 void
14119 _gtk_widget_set_shadowed (GtkWidget *widget,
14120                           gboolean   shadowed)
14121 {
14122   widget->priv->shadowed = shadowed;
14123 }
14124
14125 gboolean
14126 _gtk_widget_get_alloc_needed (GtkWidget *widget)
14127 {
14128   return widget->priv->alloc_needed;
14129 }
14130
14131 void
14132 _gtk_widget_set_alloc_needed (GtkWidget *widget,
14133                               gboolean   alloc_needed)
14134 {
14135   widget->priv->alloc_needed = alloc_needed;
14136 }
14137
14138 gboolean
14139 _gtk_widget_get_width_request_needed (GtkWidget *widget)
14140 {
14141   return widget->priv->width_request_needed;
14142 }
14143
14144 void
14145 _gtk_widget_set_width_request_needed (GtkWidget *widget,
14146                                       gboolean   width_request_needed)
14147 {
14148   widget->priv->width_request_needed = width_request_needed;
14149 }
14150
14151 gboolean
14152 _gtk_widget_get_height_request_needed (GtkWidget *widget)
14153 {
14154   return widget->priv->height_request_needed;
14155 }
14156
14157 void
14158 _gtk_widget_set_height_request_needed (GtkWidget *widget,
14159                                        gboolean   height_request_needed)
14160 {
14161   widget->priv->height_request_needed = height_request_needed;
14162 }
14163
14164 gboolean
14165 _gtk_widget_get_sizegroup_visited (GtkWidget    *widget)
14166 {
14167   return widget->priv->sizegroup_visited;
14168 }
14169
14170 void
14171 _gtk_widget_set_sizegroup_visited (GtkWidget    *widget,
14172                                    gboolean      visited)
14173 {
14174   widget->priv->sizegroup_visited = visited;
14175 }
14176
14177 gboolean
14178 _gtk_widget_get_sizegroup_bumping (GtkWidget    *widget)
14179 {
14180   return widget->priv->sizegroup_bumping;
14181 }
14182
14183 void
14184 _gtk_widget_set_sizegroup_bumping (GtkWidget    *widget,
14185                                    gboolean      bumping)
14186 {
14187   widget->priv->sizegroup_bumping = bumping;
14188 }
14189
14190 void
14191 _gtk_widget_add_sizegroup (GtkWidget    *widget,
14192                            gpointer      group)
14193 {
14194   GSList *groups;
14195
14196   groups = g_object_get_qdata (G_OBJECT (widget), quark_size_groups);
14197   groups = g_slist_prepend (groups, group);
14198   g_object_set_qdata (G_OBJECT (widget), quark_size_groups, groups);
14199
14200   widget->priv->have_size_groups = TRUE;
14201 }
14202
14203 void
14204 _gtk_widget_remove_sizegroup (GtkWidget    *widget,
14205                               gpointer      group)
14206 {
14207   GSList *groups;
14208
14209   groups = g_object_get_qdata (G_OBJECT (widget), quark_size_groups);
14210   groups = g_slist_remove (groups, group);
14211   g_object_set_qdata (G_OBJECT (widget), quark_size_groups, groups);
14212
14213   widget->priv->have_size_groups = groups != NULL;
14214 }
14215
14216 GSList *
14217 _gtk_widget_get_sizegroups (GtkWidget    *widget)
14218 {
14219   if (widget->priv->have_size_groups)
14220     return g_object_get_qdata (G_OBJECT (widget), quark_size_groups);
14221
14222   return NULL;
14223 }
14224
14225 /**
14226  * gtk_widget_get_path:
14227  * @widget: a #GtkWidget
14228  *
14229  * Returns the #GtkWidgetPath representing @widget, if the widget
14230  * is not connected to a toplevel widget, a partial path will be
14231  * created.
14232  *
14233  * Returns: (transfer none): The #GtkWidgetPath representing @widget
14234  **/
14235 GtkWidgetPath *
14236 gtk_widget_get_path (GtkWidget *widget)
14237 {
14238   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
14239
14240   /* As strange as it may seem, this may happen on object construction.
14241    * init() implementations of parent types may eventually call this function,
14242    * each with its corresponding GType, which could leave a child
14243    * implementation with a wrong widget type in the widget path
14244    */
14245   if (widget->priv->path &&
14246       G_OBJECT_TYPE (widget) != gtk_widget_path_get_object_type (widget->priv->path))
14247     {
14248       gtk_widget_path_free (widget->priv->path);
14249       widget->priv->path = NULL;
14250     }
14251
14252   if (!widget->priv->path)
14253     {
14254       GtkWidget *parent;
14255       guint pos;
14256
14257       parent = widget->priv->parent;
14258
14259       if (parent)
14260         widget->priv->path = gtk_container_get_path_for_child (GTK_CONTAINER (parent), widget);
14261       else
14262         {
14263           /* Widget is either toplevel or unparented, treat both
14264            * as toplevels style wise, since there are situations
14265            * where style properties might be retrieved on that
14266            * situation.
14267            */
14268           widget->priv->path = gtk_widget_path_new ();
14269         }
14270
14271       pos = gtk_widget_path_append_type (widget->priv->path, G_OBJECT_TYPE (widget));
14272
14273       if (widget->priv->name)
14274         gtk_widget_path_iter_set_name (widget->priv->path, pos, widget->priv->name);
14275
14276       if (widget->priv->context)
14277         {
14278           GList *classes, *l;
14279
14280           /* Also add any persistent classes in
14281            * the style context the widget path
14282            */
14283           classes = gtk_style_context_list_classes (widget->priv->context);
14284
14285           for (l = classes; l; l = l->next)
14286             gtk_widget_path_iter_add_class (widget->priv->path, pos, l->data);
14287
14288           gtk_style_context_set_path (widget->priv->context,
14289                                       widget->priv->path);
14290
14291           g_list_free (classes);
14292         }
14293     }
14294
14295   return widget->priv->path;
14296 }
14297
14298 static void
14299 style_context_changed (GtkStyleContext *context,
14300                        gpointer         user_data)
14301 {
14302   GtkWidget *widget = user_data;
14303
14304   gtk_widget_update_pango_context (widget);
14305
14306   if (gtk_widget_get_realized (widget))
14307     g_signal_emit (widget, widget_signals[STYLE_UPDATED], 0);
14308   else
14309     {
14310       /* Compress all style updates so it
14311        * is only emitted once pre-realize.
14312        */
14313       widget->priv->style_update_pending = TRUE;
14314     }
14315
14316   if (widget->priv->anchored)
14317     gtk_widget_queue_resize (widget);
14318 }
14319
14320 /**
14321  * gtk_widget_get_style_context:
14322  * @widget: a #GtkWidget
14323  *
14324  * Returns the style context associated to @widget.
14325  *
14326  * Returns: (transfer none): a #GtkStyleContext. This memory is owned by @widget and
14327  *          must not be freed.
14328  **/
14329 GtkStyleContext *
14330 gtk_widget_get_style_context (GtkWidget *widget)
14331 {
14332   GtkWidgetPrivate *priv;
14333   GtkWidgetPath *path;
14334
14335   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
14336
14337   priv = widget->priv;
14338   
14339   /* updates style context if it exists already */
14340   path = gtk_widget_get_path (widget);
14341
14342   if (G_UNLIKELY (priv->context == NULL))
14343     {
14344       GdkScreen *screen;
14345
14346       priv->context = g_object_new (GTK_TYPE_STYLE_CONTEXT,
14347                                     "direction", gtk_widget_get_direction (widget),
14348                                     NULL);
14349
14350       g_signal_connect (widget->priv->context, "changed",
14351                         G_CALLBACK (style_context_changed), widget);
14352
14353       screen = gtk_widget_get_screen (widget);
14354
14355       if (screen)
14356         gtk_style_context_set_screen (priv->context, screen);
14357
14358       gtk_style_context_set_path (priv->context, path);
14359     }
14360
14361   return widget->priv->context;
14362 }