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