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