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