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