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