]> Pileus Git - ~andy/gtk/blob - gtk/gtkwidget.c
8922eb9f7acebfe911cb4f8c3279488cece42c4c
[~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 }
4203
4204 /**
4205  * gtk_widget_unrealize:
4206  * @widget: a #GtkWidget
4207  *
4208  * This function is only useful in widget implementations.
4209  * Causes a widget to be unrealized (frees all GDK resources
4210  * associated with the widget, such as @widget->window).
4211  **/
4212 void
4213 gtk_widget_unrealize (GtkWidget *widget)
4214 {
4215   g_return_if_fail (GTK_IS_WIDGET (widget));
4216
4217   if (widget->priv->has_shape_mask)
4218     gtk_widget_shape_combine_region (widget, NULL);
4219
4220   if (g_object_get_qdata (G_OBJECT (widget), quark_input_shape_info))
4221     gtk_widget_input_shape_combine_region (widget, NULL);
4222
4223   if (gtk_widget_get_realized (widget))
4224     {
4225       g_object_ref (widget);
4226       _gtk_tooltip_hide (widget);
4227       g_signal_emit (widget, widget_signals[UNREALIZE], 0);
4228       gtk_widget_set_realized (widget, FALSE);
4229       gtk_widget_set_mapped (widget, FALSE);
4230       g_object_unref (widget);
4231     }
4232 }
4233
4234 /*****************************************
4235  * Draw queueing.
4236  *****************************************/
4237
4238 /**
4239  * gtk_widget_queue_draw_region:
4240  * @widget: a #GtkWidget
4241  * @region: region to draw
4242  *
4243  * Invalidates the rectangular area of @widget defined by @region by
4244  * calling gdk_window_invalidate_region() on the widget's window and
4245  * all its child windows. Once the main loop becomes idle (after the
4246  * current batch of events has been processed, roughly), the window
4247  * will receive expose events for the union of all regions that have
4248  * been invalidated.
4249  *
4250  * Normally you would only use this function in widget
4251  * implementations. You might also use it to schedule a redraw of a
4252  * #GtkDrawingArea or some portion thereof.
4253  **/
4254 void
4255 gtk_widget_queue_draw_region (GtkWidget      *widget,
4256                               cairo_region_t *region)
4257 {
4258   GtkWidgetPrivate *priv;
4259   GtkWidget *w;
4260
4261   g_return_if_fail (GTK_IS_WIDGET (widget));
4262
4263   priv = widget->priv;
4264
4265   if (!gtk_widget_get_realized (widget))
4266     return;
4267
4268   /* Just return if the widget or one of its ancestors isn't mapped */
4269   for (w = widget; w != NULL; w = w->priv->parent)
4270     if (!gtk_widget_get_mapped (w))
4271       return;
4272
4273   gdk_window_invalidate_region (priv->window, region, TRUE);
4274 }
4275
4276 /**
4277  * gtk_widget_queue_draw_area:
4278  * @widget: a #GtkWidget
4279  * @x: x coordinate of upper-left corner of rectangle to redraw
4280  * @y: y coordinate of upper-left corner of rectangle to redraw
4281  * @width: width of region to draw
4282  * @height: height of region to draw
4283  *
4284  * Convenience function that calls gtk_widget_queue_draw_region() on
4285  * the region created from the given coordinates.
4286  **/
4287 void
4288 gtk_widget_queue_draw_area (GtkWidget *widget,
4289                             gint       x,
4290                             gint       y,
4291                             gint       width,
4292                             gint       height)
4293 {
4294   GdkRectangle rect;
4295   cairo_region_t *region;
4296
4297   g_return_if_fail (GTK_IS_WIDGET (widget));
4298
4299   rect.x = x;
4300   rect.y = y;
4301   rect.width = width;
4302   rect.height = height;
4303
4304   region = cairo_region_create_rectangle (&rect);
4305   gtk_widget_queue_draw_region (widget, region);
4306   cairo_region_destroy (region);
4307 }
4308
4309 /**
4310  * gtk_widget_queue_draw:
4311  * @widget: a #GtkWidget
4312  *
4313  * Equivalent to calling gtk_widget_queue_draw_area() for the
4314  * entire area of a widget.
4315  **/
4316 void
4317 gtk_widget_queue_draw (GtkWidget *widget)
4318 {
4319   GdkRectangle rect;
4320
4321   g_return_if_fail (GTK_IS_WIDGET (widget));
4322
4323   gtk_widget_get_allocation (widget, &rect);
4324
4325   if (!gtk_widget_get_has_window (widget))
4326     gtk_widget_queue_draw_area (widget,
4327                                 rect.x, rect.y, rect.width, rect.height);
4328   else
4329     gtk_widget_queue_draw_area (widget,
4330                                 0, 0, rect.width, rect.height);
4331 }
4332
4333 /**
4334  * gtk_widget_queue_resize:
4335  * @widget: a #GtkWidget
4336  *
4337  * This function is only for use in widget implementations.
4338  * Flags a widget to have its size renegotiated; should
4339  * be called when a widget for some reason has a new size request.
4340  * For example, when you change the text in a #GtkLabel, #GtkLabel
4341  * queues a resize to ensure there's enough space for the new text.
4342  *
4343  * <note><para>You cannot call gtk_widget_queue_resize() on a widget
4344  * from inside it's implementation of the GtkWidgetClass::size_allocate 
4345  * virtual method. Calls to gtk_widget_queue_resize() from inside
4346  * GtkWidgetClass::size_allocate will be silently ignored.</para></note>
4347  **/
4348 void
4349 gtk_widget_queue_resize (GtkWidget *widget)
4350 {
4351   g_return_if_fail (GTK_IS_WIDGET (widget));
4352
4353   if (gtk_widget_get_realized (widget))
4354     gtk_widget_queue_shallow_draw (widget);
4355
4356   _gtk_size_group_queue_resize (widget, 0);
4357 }
4358
4359 /**
4360  * gtk_widget_queue_resize_no_redraw:
4361  * @widget: a #GtkWidget
4362  *
4363  * This function works like gtk_widget_queue_resize(),
4364  * except that the widget is not invalidated.
4365  *
4366  * Since: 2.4
4367  **/
4368 void
4369 gtk_widget_queue_resize_no_redraw (GtkWidget *widget)
4370 {
4371   g_return_if_fail (GTK_IS_WIDGET (widget));
4372
4373   _gtk_size_group_queue_resize (widget, 0);
4374 }
4375
4376 /**
4377  * gtk_widget_size_request:
4378  * @widget: a #GtkWidget
4379  * @requisition: (out): a #GtkRequisition to be filled in
4380  *
4381  * This function is typically used when implementing a #GtkContainer
4382  * subclass.  Obtains the preferred size of a widget. The container
4383  * uses this information to arrange its child widgets and decide what
4384  * size allocations to give them with gtk_widget_size_allocate().
4385  *
4386  * You can also call this function from an application, with some
4387  * caveats. Most notably, getting a size request requires the widget
4388  * to be associated with a screen, because font information may be
4389  * needed. Multihead-aware applications should keep this in mind.
4390  *
4391  * Also remember that the size request is not necessarily the size
4392  * a widget will actually be allocated.
4393  *
4394  * Deprecated: 3.0: Use gtk_widget_get_preferred_size() instead.
4395  **/
4396 void
4397 gtk_widget_size_request (GtkWidget      *widget,
4398                          GtkRequisition *requisition)
4399 {
4400   g_return_if_fail (GTK_IS_WIDGET (widget));
4401
4402   gtk_widget_get_preferred_size (widget, requisition, NULL);
4403 }
4404
4405 /**
4406  * gtk_widget_get_child_requisition:
4407  * @widget: a #GtkWidget
4408  * @requisition: (out): a #GtkRequisition to be filled in
4409  *
4410  * This function is only for use in widget implementations. Obtains
4411  * @widget->requisition, unless someone has forced a particular
4412  * geometry on the widget (e.g. with gtk_widget_set_size_request()),
4413  * in which case it returns that geometry instead of the widget's
4414  * requisition.
4415  *
4416  * This function differs from gtk_widget_size_request() in that
4417  * it retrieves the last size request value from @widget->requisition,
4418  * while gtk_widget_size_request() actually calls the "size_request" method
4419  * on @widget to compute the size request and fill in @widget->requisition,
4420  * and only then returns @widget->requisition.
4421  *
4422  * Because this function does not call the "size_request" method, it
4423  * can only be used when you know that @widget->requisition is
4424  * up-to-date, that is, gtk_widget_size_request() has been called
4425  * since the last time a resize was queued. In general, only container
4426  * implementations have this information; applications should use
4427  * gtk_widget_size_request().
4428  *
4429  *
4430  * Deprecated: 3.0: Use gtk_widget_get_preferred_size() instead.
4431  **/
4432 void
4433 gtk_widget_get_child_requisition (GtkWidget      *widget,
4434                                   GtkRequisition *requisition)
4435 {
4436   gtk_widget_get_preferred_size (widget, requisition, NULL);
4437 }
4438
4439 static gboolean
4440 invalidate_predicate (GdkWindow *window,
4441                       gpointer   data)
4442 {
4443   gpointer user_data;
4444
4445   gdk_window_get_user_data (window, &user_data);
4446
4447   return (user_data == data);
4448 }
4449
4450 /* Invalidate @region in widget->window and all children
4451  * of widget->window owned by widget. @region is in the
4452  * same coordinates as widget->allocation and will be
4453  * modified by this call.
4454  */
4455 static void
4456 gtk_widget_invalidate_widget_windows (GtkWidget *widget,
4457                                       cairo_region_t *region)
4458 {
4459   GtkWidgetPrivate *priv = widget->priv;
4460
4461   if (!gtk_widget_get_realized (widget))
4462     return;
4463
4464   if (gtk_widget_get_has_window (widget) && priv->parent)
4465     {
4466       int x, y;
4467
4468       gdk_window_get_position (priv->window, &x, &y);
4469       cairo_region_translate (region, -x, -y);
4470     }
4471
4472   gdk_window_invalidate_maybe_recurse (priv->window, region,
4473                                        invalidate_predicate, widget);
4474 }
4475
4476 /**
4477  * gtk_widget_queue_shallow_draw:
4478  * @widget: a #GtkWidget
4479  *
4480  * Like gtk_widget_queue_draw(), but only windows owned
4481  * by @widget are invalidated.
4482  **/
4483 static void
4484 gtk_widget_queue_shallow_draw (GtkWidget *widget)
4485 {
4486   GdkRectangle rect;
4487   cairo_region_t *region;
4488
4489   if (!gtk_widget_get_realized (widget))
4490     return;
4491
4492   gtk_widget_get_allocation (widget, &rect);
4493
4494   region = cairo_region_create_rectangle (&rect);
4495   gtk_widget_invalidate_widget_windows (widget, region);
4496   cairo_region_destroy (region);
4497 }
4498
4499 /**
4500  * gtk_widget_size_allocate:
4501  * @widget: a #GtkWidget
4502  * @allocation: (inout): position and size to be allocated to @widget
4503  *
4504  * This function is only used by #GtkContainer subclasses, to assign a size
4505  * and position to their child widgets.
4506  *
4507  * In this function, the allocation may be adjusted. It will be forced
4508  * to a 1x1 minimum size, and the adjust_size_allocation virtual
4509  * method on the child will be used to adjust the allocation. Standard
4510  * adjustments include removing the widget's margins, and applying the
4511  * widget's #GtkWidget:halign and #GtkWidget:valign properties.
4512  **/
4513 void
4514 gtk_widget_size_allocate (GtkWidget     *widget,
4515                           GtkAllocation *allocation)
4516 {
4517   GtkWidgetPrivate *priv;
4518   GdkRectangle real_allocation;
4519   GdkRectangle old_allocation;
4520   GdkRectangle adjusted_allocation;
4521   gboolean alloc_needed;
4522   gboolean size_changed;
4523   gboolean position_changed;
4524   gint natural_width, natural_height;
4525   gint min_width, min_height;
4526
4527   priv = widget->priv;
4528
4529   g_return_if_fail (GTK_IS_WIDGET (widget));
4530
4531 #ifdef G_ENABLE_DEBUG
4532   if (gtk_get_debug_flags () & GTK_DEBUG_GEOMETRY)
4533     {
4534       gint depth;
4535       GtkWidget *parent;
4536       const gchar *name;
4537
4538       depth = 0;
4539       parent = widget;
4540       while (parent)
4541         {
4542           depth++;
4543           parent = gtk_widget_get_parent (parent);
4544         }
4545
4546       name = g_type_name (G_OBJECT_TYPE (G_OBJECT (widget)));
4547       g_print ("gtk_widget_size_allocate: %*s%s %d %d\n",
4548                2 * depth, " ", name,
4549                allocation->width, allocation->height);
4550     }
4551 #endif /* G_ENABLE_DEBUG */
4552
4553   alloc_needed = priv->alloc_needed;
4554   if (!priv->width_request_needed && !priv->height_request_needed)
4555     /* Preserve request/allocate ordering */
4556     priv->alloc_needed = FALSE;
4557
4558   old_allocation = priv->allocation;
4559   real_allocation = *allocation;
4560
4561   adjusted_allocation = real_allocation;
4562   if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
4563     {
4564       /* Go ahead and request the height for allocated width, note that the internals
4565        * of get_height_for_width will internally limit the for_size to natural size
4566        * when aligning implicitly.
4567        */
4568       gtk_widget_get_preferred_width (widget, &min_width, &natural_width);
4569       gtk_widget_get_preferred_height_for_width (widget, real_allocation.width, NULL, &natural_height);
4570     }
4571   else
4572     {
4573       /* Go ahead and request the width for allocated height, note that the internals
4574        * of get_width_for_height will internally limit the for_size to natural size
4575        * when aligning implicitly.
4576        */
4577       gtk_widget_get_preferred_height (widget, &min_height, &natural_height);
4578       gtk_widget_get_preferred_width_for_height (widget, real_allocation.height, NULL, &natural_width);
4579     }
4580
4581   /* Now that we have the right natural height and width, go ahead and remove any margins from the
4582    * allocated sizes and possibly limit them to the natural sizes */
4583   GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
4584                                                          GTK_ORIENTATION_HORIZONTAL,
4585                                                          &natural_width,
4586                                                          &adjusted_allocation.x,
4587                                                          &adjusted_allocation.width);
4588   GTK_WIDGET_GET_CLASS (widget)->adjust_size_allocation (widget,
4589                                                          GTK_ORIENTATION_VERTICAL,
4590                                                          &natural_height,
4591                                                          &adjusted_allocation.y,
4592                                                          &adjusted_allocation.height);
4593
4594   if (adjusted_allocation.x < real_allocation.x ||
4595       adjusted_allocation.y < real_allocation.y ||
4596       (adjusted_allocation.x + adjusted_allocation.width) >
4597       (real_allocation.x + real_allocation.width) ||
4598       (adjusted_allocation.y + adjusted_allocation.height >
4599        real_allocation.y + real_allocation.height))
4600     {
4601       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",
4602                  G_OBJECT_TYPE_NAME (widget), widget,
4603                  real_allocation.x, real_allocation.y, real_allocation.width, real_allocation.height,
4604                  adjusted_allocation.x, adjusted_allocation.y, adjusted_allocation.width, adjusted_allocation.height);
4605       adjusted_allocation = real_allocation; /* veto it */
4606     }
4607   else
4608     {
4609       real_allocation = adjusted_allocation;
4610     }
4611
4612   if (real_allocation.width < 0 || real_allocation.height < 0)
4613     {
4614       g_warning ("gtk_widget_size_allocate(): attempt to allocate widget with width %d and height %d",
4615                  real_allocation.width,
4616                  real_allocation.height);
4617     }
4618
4619   real_allocation.width = MAX (real_allocation.width, 1);
4620   real_allocation.height = MAX (real_allocation.height, 1);
4621
4622   size_changed = (old_allocation.width != real_allocation.width ||
4623                   old_allocation.height != real_allocation.height);
4624   position_changed = (old_allocation.x != real_allocation.x ||
4625                       old_allocation.y != real_allocation.y);
4626
4627   if (!alloc_needed && !size_changed && !position_changed)
4628     return;
4629
4630   g_signal_emit (widget, widget_signals[SIZE_ALLOCATE], 0, &real_allocation);
4631
4632   /* Size allocation is god... after consulting god, no further requests or allocations are needed */
4633   priv->width_request_needed  = FALSE;
4634   priv->height_request_needed = FALSE;
4635   priv->alloc_needed          = FALSE;
4636
4637   if (gtk_widget_get_mapped (widget))
4638     {
4639       if (!gtk_widget_get_has_window (widget) && priv->redraw_on_alloc && position_changed)
4640         {
4641           /* Invalidate union(old_allaction,priv->allocation) in priv->window
4642            */
4643           cairo_region_t *invalidate = cairo_region_create_rectangle (&priv->allocation);
4644           cairo_region_union_rectangle (invalidate, &old_allocation);
4645
4646           gdk_window_invalidate_region (priv->window, invalidate, FALSE);
4647           cairo_region_destroy (invalidate);
4648         }
4649
4650       if (size_changed)
4651         {
4652           if (priv->redraw_on_alloc)
4653             {
4654               /* Invalidate union(old_allaction,priv->allocation) in priv->window and descendents owned by widget
4655                */
4656               cairo_region_t *invalidate = cairo_region_create_rectangle (&priv->allocation);
4657               cairo_region_union_rectangle (invalidate, &old_allocation);
4658
4659               gtk_widget_invalidate_widget_windows (widget, invalidate);
4660               cairo_region_destroy (invalidate);
4661             }
4662         }
4663
4664       if (size_changed || position_changed)
4665         {
4666           GtkStyleContext *context;
4667
4668           context = gtk_widget_get_style_context (widget);
4669           _gtk_style_context_invalidate_animation_areas (context);
4670         }
4671     }
4672
4673   if ((size_changed || position_changed) && priv->parent &&
4674       gtk_widget_get_realized (priv->parent) && _gtk_container_get_reallocate_redraws (GTK_CONTAINER (priv->parent)))
4675     {
4676       cairo_region_t *invalidate = cairo_region_create_rectangle (&priv->parent->priv->allocation);
4677       gtk_widget_invalidate_widget_windows (priv->parent, invalidate);
4678       cairo_region_destroy (invalidate);
4679     }
4680 }
4681
4682 /**
4683  * gtk_widget_common_ancestor:
4684  * @widget_a: a #GtkWidget
4685  * @widget_b: a #GtkWidget
4686  *
4687  * Find the common ancestor of @widget_a and @widget_b that
4688  * is closest to the two widgets.
4689  *
4690  * Return value: the closest common ancestor of @widget_a and
4691  *   @widget_b or %NULL if @widget_a and @widget_b do not
4692  *   share a common ancestor.
4693  **/
4694 static GtkWidget *
4695 gtk_widget_common_ancestor (GtkWidget *widget_a,
4696                             GtkWidget *widget_b)
4697 {
4698   GtkWidget *parent_a;
4699   GtkWidget *parent_b;
4700   gint depth_a = 0;
4701   gint depth_b = 0;
4702
4703   parent_a = widget_a;
4704   while (parent_a->priv->parent)
4705     {
4706       parent_a = parent_a->priv->parent;
4707       depth_a++;
4708     }
4709
4710   parent_b = widget_b;
4711   while (parent_b->priv->parent)
4712     {
4713       parent_b = parent_b->priv->parent;
4714       depth_b++;
4715     }
4716
4717   if (parent_a != parent_b)
4718     return NULL;
4719
4720   while (depth_a > depth_b)
4721     {
4722       widget_a = widget_a->priv->parent;
4723       depth_a--;
4724     }
4725
4726   while (depth_b > depth_a)
4727     {
4728       widget_b = widget_b->priv->parent;
4729       depth_b--;
4730     }
4731
4732   while (widget_a != widget_b)
4733     {
4734       widget_a = widget_a->priv->parent;
4735       widget_b = widget_b->priv->parent;
4736     }
4737
4738   return widget_a;
4739 }
4740
4741 /**
4742  * gtk_widget_translate_coordinates:
4743  * @src_widget:  a #GtkWidget
4744  * @dest_widget: a #GtkWidget
4745  * @src_x: X position relative to @src_widget
4746  * @src_y: Y position relative to @src_widget
4747  * @dest_x: (out): location to store X position relative to @dest_widget
4748  * @dest_y: (out): location to store Y position relative to @dest_widget
4749  *
4750  * Translate coordinates relative to @src_widget's allocation to coordinates
4751  * relative to @dest_widget's allocations. In order to perform this
4752  * operation, both widgets must be realized, and must share a common
4753  * toplevel.
4754  *
4755  * Return value: %FALSE if either widget was not realized, or there
4756  *   was no common ancestor. In this case, nothing is stored in
4757  *   *@dest_x and *@dest_y. Otherwise %TRUE.
4758  **/
4759 gboolean
4760 gtk_widget_translate_coordinates (GtkWidget  *src_widget,
4761                                   GtkWidget  *dest_widget,
4762                                   gint        src_x,
4763                                   gint        src_y,
4764                                   gint       *dest_x,
4765                                   gint       *dest_y)
4766 {
4767   GtkWidgetPrivate *src_priv = src_widget->priv;
4768   GtkWidgetPrivate *dest_priv = dest_widget->priv;
4769   GtkWidget *ancestor;
4770   GdkWindow *window;
4771   GList *dest_list = NULL;
4772
4773   g_return_val_if_fail (GTK_IS_WIDGET (src_widget), FALSE);
4774   g_return_val_if_fail (GTK_IS_WIDGET (dest_widget), FALSE);
4775
4776   ancestor = gtk_widget_common_ancestor (src_widget, dest_widget);
4777   if (!ancestor || !gtk_widget_get_realized (src_widget) || !gtk_widget_get_realized (dest_widget))
4778     return FALSE;
4779
4780   /* Translate from allocation relative to window relative */
4781   if (gtk_widget_get_has_window (src_widget) && src_priv->parent)
4782     {
4783       gint wx, wy;
4784       gdk_window_get_position (src_priv->window, &wx, &wy);
4785
4786       src_x -= wx - src_priv->allocation.x;
4787       src_y -= wy - src_priv->allocation.y;
4788     }
4789   else
4790     {
4791       src_x += src_priv->allocation.x;
4792       src_y += src_priv->allocation.y;
4793     }
4794
4795   /* Translate to the common ancestor */
4796   window = src_priv->window;
4797   while (window != ancestor->priv->window)
4798     {
4799       gdouble dx, dy;
4800
4801       gdk_window_coords_to_parent (window, src_x, src_y, &dx, &dy);
4802
4803       src_x = dx;
4804       src_y = dy;
4805
4806       window = gdk_window_get_effective_parent (window);
4807
4808       if (!window)              /* Handle GtkHandleBox */
4809         return FALSE;
4810     }
4811
4812   /* And back */
4813   window = dest_priv->window;
4814   while (window != ancestor->priv->window)
4815     {
4816       dest_list = g_list_prepend (dest_list, window);
4817
4818       window = gdk_window_get_effective_parent (window);
4819
4820       if (!window)              /* Handle GtkHandleBox */
4821         {
4822           g_list_free (dest_list);
4823           return FALSE;
4824         }
4825     }
4826
4827   while (dest_list)
4828     {
4829       gdouble dx, dy;
4830
4831       gdk_window_coords_from_parent (dest_list->data, src_x, src_y, &dx, &dy);
4832
4833       src_x = dx;
4834       src_y = dy;
4835
4836       dest_list = g_list_remove (dest_list, dest_list->data);
4837     }
4838
4839   /* Translate from window relative to allocation relative */
4840   if (gtk_widget_get_has_window (dest_widget) && dest_priv->parent)
4841     {
4842       gint wx, wy;
4843       gdk_window_get_position (dest_priv->window, &wx, &wy);
4844
4845       src_x += wx - dest_priv->allocation.x;
4846       src_y += wy - dest_priv->allocation.y;
4847     }
4848   else
4849     {
4850       src_x -= dest_priv->allocation.x;
4851       src_y -= dest_priv->allocation.y;
4852     }
4853
4854   if (dest_x)
4855     *dest_x = src_x;
4856   if (dest_y)
4857     *dest_y = src_y;
4858
4859   return TRUE;
4860 }
4861
4862 static void
4863 gtk_widget_real_size_allocate (GtkWidget     *widget,
4864                                GtkAllocation *allocation)
4865 {
4866   GtkWidgetPrivate *priv = widget->priv;
4867
4868   priv->allocation = *allocation;
4869
4870   if (gtk_widget_get_realized (widget) &&
4871       gtk_widget_get_has_window (widget))
4872      {
4873         gdk_window_move_resize (priv->window,
4874                                 allocation->x, allocation->y,
4875                                 allocation->width, allocation->height);
4876      }
4877 }
4878
4879 static void
4880 adjust_for_align(GtkAlign           align,
4881                  gint              *natural_size,
4882                  gint              *allocated_pos,
4883                  gint              *allocated_size)
4884 {
4885   switch (align)
4886     {
4887     case GTK_ALIGN_FILL:
4888       /* change nothing */
4889       break;
4890     case GTK_ALIGN_START:
4891       /* keep *allocated_pos where it is */
4892       *allocated_size = MIN (*allocated_size, *natural_size);
4893       break;
4894     case GTK_ALIGN_END:
4895       if (*allocated_size > *natural_size)
4896         {
4897           *allocated_pos += (*allocated_size - *natural_size);
4898           *allocated_size = *natural_size;
4899         }
4900       break;
4901     case GTK_ALIGN_CENTER:
4902       if (*allocated_size > *natural_size)
4903         {
4904           *allocated_pos += (*allocated_size - *natural_size) / 2;
4905           *allocated_size = MIN (*allocated_size, *natural_size);
4906         }
4907       break;
4908     }
4909 }
4910
4911 static void
4912 adjust_for_margin(gint               start_margin,
4913                   gint               end_margin,
4914                   gint              *natural_size,
4915                   gint              *allocated_pos,
4916                   gint              *allocated_size)
4917 {
4918   *natural_size -= (start_margin + end_margin);
4919   *allocated_pos += start_margin;
4920   *allocated_size -= (start_margin + end_margin);
4921 }
4922
4923 static void
4924 gtk_widget_real_adjust_size_allocation (GtkWidget         *widget,
4925                                         GtkOrientation     orientation,
4926                                         gint              *natural_size,
4927                                         gint              *allocated_pos,
4928                                         gint              *allocated_size)
4929 {
4930   const GtkWidgetAuxInfo *aux_info;
4931
4932   aux_info = _gtk_widget_get_aux_info_or_defaults (widget);
4933
4934   if (orientation == GTK_ORIENTATION_HORIZONTAL)
4935     {
4936       adjust_for_margin (aux_info->margin.left,
4937                          aux_info->margin.right,
4938                          natural_size, allocated_pos, allocated_size);
4939       adjust_for_align (aux_info->halign,
4940                         natural_size, allocated_pos, allocated_size);
4941     }
4942   else
4943     {
4944       adjust_for_margin (aux_info->margin.top,
4945                          aux_info->margin.bottom,
4946                          natural_size, allocated_pos, allocated_size);
4947       adjust_for_align (aux_info->valign,
4948                         natural_size, allocated_pos, allocated_size);
4949     }
4950 }
4951
4952 static gboolean
4953 gtk_widget_real_can_activate_accel (GtkWidget *widget,
4954                                     guint      signal_id)
4955 {
4956   GtkWidgetPrivate *priv = widget->priv;
4957
4958   /* widgets must be onscreen for accels to take effect */
4959   return gtk_widget_is_sensitive (widget) &&
4960          gtk_widget_is_drawable (widget) &&
4961          gdk_window_is_viewable (priv->window);
4962 }
4963
4964 /**
4965  * gtk_widget_can_activate_accel:
4966  * @widget: a #GtkWidget
4967  * @signal_id: the ID of a signal installed on @widget
4968  *
4969  * Determines whether an accelerator that activates the signal
4970  * identified by @signal_id can currently be activated.
4971  * This is done by emitting the #GtkWidget::can-activate-accel
4972  * signal on @widget; if the signal isn't overridden by a
4973  * handler or in a derived widget, then the default check is
4974  * that the widget must be sensitive, and the widget and all
4975  * its ancestors mapped.
4976  *
4977  * Return value: %TRUE if the accelerator can be activated.
4978  *
4979  * Since: 2.4
4980  **/
4981 gboolean
4982 gtk_widget_can_activate_accel (GtkWidget *widget,
4983                                guint      signal_id)
4984 {
4985   gboolean can_activate = FALSE;
4986   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4987   g_signal_emit (widget, widget_signals[CAN_ACTIVATE_ACCEL], 0, signal_id, &can_activate);
4988   return can_activate;
4989 }
4990
4991 typedef struct {
4992   GClosure   closure;
4993   guint      signal_id;
4994 } AccelClosure;
4995
4996 static void
4997 closure_accel_activate (GClosure     *closure,
4998                         GValue       *return_value,
4999                         guint         n_param_values,
5000                         const GValue *param_values,
5001                         gpointer      invocation_hint,
5002                         gpointer      marshal_data)
5003 {
5004   AccelClosure *aclosure = (AccelClosure*) closure;
5005   gboolean can_activate = gtk_widget_can_activate_accel (closure->data, aclosure->signal_id);
5006
5007   if (can_activate)
5008     g_signal_emit (closure->data, aclosure->signal_id, 0);
5009
5010   /* whether accelerator was handled */
5011   g_value_set_boolean (return_value, can_activate);
5012 }
5013
5014 static void
5015 closures_destroy (gpointer data)
5016 {
5017   GSList *slist, *closures = data;
5018
5019   for (slist = closures; slist; slist = slist->next)
5020     {
5021       g_closure_invalidate (slist->data);
5022       g_closure_unref (slist->data);
5023     }
5024   g_slist_free (closures);
5025 }
5026
5027 static GClosure*
5028 widget_new_accel_closure (GtkWidget *widget,
5029                           guint      signal_id)
5030 {
5031   AccelClosure *aclosure;
5032   GClosure *closure = NULL;
5033   GSList *slist, *closures;
5034
5035   closures = g_object_steal_qdata (G_OBJECT (widget), quark_accel_closures);
5036   for (slist = closures; slist; slist = slist->next)
5037     if (!gtk_accel_group_from_accel_closure (slist->data))
5038       {
5039         /* reuse this closure */
5040         closure = slist->data;
5041         break;
5042       }
5043   if (!closure)
5044     {
5045       closure = g_closure_new_object (sizeof (AccelClosure), G_OBJECT (widget));
5046       closures = g_slist_prepend (closures, g_closure_ref (closure));
5047       g_closure_sink (closure);
5048       g_closure_set_marshal (closure, closure_accel_activate);
5049     }
5050   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_closures, closures, closures_destroy);
5051
5052   aclosure = (AccelClosure*) closure;
5053   g_assert (closure->data == widget);
5054   g_assert (closure->marshal == closure_accel_activate);
5055   aclosure->signal_id = signal_id;
5056
5057   return closure;
5058 }
5059
5060 /**
5061  * gtk_widget_add_accelerator
5062  * @widget:       widget to install an accelerator on
5063  * @accel_signal: widget signal to emit on accelerator activation
5064  * @accel_group:  accel group for this widget, added to its toplevel
5065  * @accel_key:    GDK keyval of the accelerator
5066  * @accel_mods:   modifier key combination of the accelerator
5067  * @accel_flags:  flag accelerators, e.g. %GTK_ACCEL_VISIBLE
5068  *
5069  * Installs an accelerator for this @widget in @accel_group that causes
5070  * @accel_signal to be emitted if the accelerator is activated.
5071  * The @accel_group needs to be added to the widget's toplevel via
5072  * gtk_window_add_accel_group(), and the signal must be of type %G_RUN_ACTION.
5073  * Accelerators added through this function are not user changeable during
5074  * runtime. If you want to support accelerators that can be changed by the
5075  * user, use gtk_accel_map_add_entry() and gtk_widget_set_accel_path() or
5076  * gtk_menu_item_set_accel_path() instead.
5077  */
5078 void
5079 gtk_widget_add_accelerator (GtkWidget      *widget,
5080                             const gchar    *accel_signal,
5081                             GtkAccelGroup  *accel_group,
5082                             guint           accel_key,
5083                             GdkModifierType accel_mods,
5084                             GtkAccelFlags   accel_flags)
5085 {
5086   GClosure *closure;
5087   GSignalQuery query;
5088
5089   g_return_if_fail (GTK_IS_WIDGET (widget));
5090   g_return_if_fail (accel_signal != NULL);
5091   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
5092
5093   g_signal_query (g_signal_lookup (accel_signal, G_OBJECT_TYPE (widget)), &query);
5094   if (!query.signal_id ||
5095       !(query.signal_flags & G_SIGNAL_ACTION) ||
5096       query.return_type != G_TYPE_NONE ||
5097       query.n_params)
5098     {
5099       /* hmm, should be elaborate enough */
5100       g_warning (G_STRLOC ": widget `%s' has no activatable signal \"%s\" without arguments",
5101                  G_OBJECT_TYPE_NAME (widget), accel_signal);
5102       return;
5103     }
5104
5105   closure = widget_new_accel_closure (widget, query.signal_id);
5106
5107   g_object_ref (widget);
5108
5109   /* install the accelerator. since we don't map this onto an accel_path,
5110    * the accelerator will automatically be locked.
5111    */
5112   gtk_accel_group_connect (accel_group,
5113                            accel_key,
5114                            accel_mods,
5115                            accel_flags | GTK_ACCEL_LOCKED,
5116                            closure);
5117
5118   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
5119
5120   g_object_unref (widget);
5121 }
5122
5123 /**
5124  * gtk_widget_remove_accelerator:
5125  * @widget:       widget to install an accelerator on
5126  * @accel_group:  accel group for this widget
5127  * @accel_key:    GDK keyval of the accelerator
5128  * @accel_mods:   modifier key combination of the accelerator
5129  * @returns:      whether an accelerator was installed and could be removed
5130  *
5131  * Removes an accelerator from @widget, previously installed with
5132  * gtk_widget_add_accelerator().
5133  */
5134 gboolean
5135 gtk_widget_remove_accelerator (GtkWidget      *widget,
5136                                GtkAccelGroup  *accel_group,
5137                                guint           accel_key,
5138                                GdkModifierType accel_mods)
5139 {
5140   GtkAccelGroupEntry *ag_entry;
5141   GList *slist, *clist;
5142   guint n;
5143
5144   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5145   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
5146
5147   ag_entry = gtk_accel_group_query (accel_group, accel_key, accel_mods, &n);
5148   clist = gtk_widget_list_accel_closures (widget);
5149   for (slist = clist; slist; slist = slist->next)
5150     {
5151       guint i;
5152
5153       for (i = 0; i < n; i++)
5154         if (slist->data == (gpointer) ag_entry[i].closure)
5155           {
5156             gboolean is_removed = gtk_accel_group_disconnect (accel_group, slist->data);
5157
5158             g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
5159
5160             g_list_free (clist);
5161
5162             return is_removed;
5163           }
5164     }
5165   g_list_free (clist);
5166
5167   g_warning (G_STRLOC ": no accelerator (%u,%u) installed in accel group (%p) for %s (%p)",
5168              accel_key, accel_mods, accel_group,
5169              G_OBJECT_TYPE_NAME (widget), widget);
5170
5171   return FALSE;
5172 }
5173
5174 /**
5175  * gtk_widget_list_accel_closures:
5176  * @widget:  widget to list accelerator closures for
5177  *
5178  * Lists the closures used by @widget for accelerator group connections
5179  * with gtk_accel_group_connect_by_path() or gtk_accel_group_connect().
5180  * The closures can be used to monitor accelerator changes on @widget,
5181  * by connecting to the @GtkAccelGroup::accel-changed signal of the
5182  * #GtkAccelGroup of a closure which can be found out with
5183  * gtk_accel_group_from_accel_closure().
5184  *
5185  * Return value: (transfer container) (element-type GClosure):
5186  *     a newly allocated #GList of closures
5187  */
5188 GList*
5189 gtk_widget_list_accel_closures (GtkWidget *widget)
5190 {
5191   GSList *slist;
5192   GList *clist = NULL;
5193
5194   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5195
5196   for (slist = g_object_get_qdata (G_OBJECT (widget), quark_accel_closures); slist; slist = slist->next)
5197     if (gtk_accel_group_from_accel_closure (slist->data))
5198       clist = g_list_prepend (clist, slist->data);
5199   return clist;
5200 }
5201
5202 typedef struct {
5203   GQuark         path_quark;
5204   GtkAccelGroup *accel_group;
5205   GClosure      *closure;
5206 } AccelPath;
5207
5208 static void
5209 destroy_accel_path (gpointer data)
5210 {
5211   AccelPath *apath = data;
5212
5213   gtk_accel_group_disconnect (apath->accel_group, apath->closure);
5214
5215   /* closures_destroy takes care of unrefing the closure */
5216   g_object_unref (apath->accel_group);
5217
5218   g_slice_free (AccelPath, apath);
5219 }
5220
5221
5222 /**
5223  * gtk_widget_set_accel_path:
5224  * @widget: a #GtkWidget
5225  * @accel_path: (allow-none): path used to look up the accelerator
5226  * @accel_group: (allow-none): a #GtkAccelGroup.
5227  *
5228  * Given an accelerator group, @accel_group, and an accelerator path,
5229  * @accel_path, sets up an accelerator in @accel_group so whenever the
5230  * key binding that is defined for @accel_path is pressed, @widget
5231  * will be activated.  This removes any accelerators (for any
5232  * accelerator group) installed by previous calls to
5233  * gtk_widget_set_accel_path(). Associating accelerators with
5234  * paths allows them to be modified by the user and the modifications
5235  * to be saved for future use. (See gtk_accel_map_save().)
5236  *
5237  * This function is a low level function that would most likely
5238  * be used by a menu creation system like #GtkUIManager. If you
5239  * use #GtkUIManager, setting up accelerator paths will be done
5240  * automatically.
5241  *
5242  * Even when you you aren't using #GtkUIManager, if you only want to
5243  * set up accelerators on menu items gtk_menu_item_set_accel_path()
5244  * provides a somewhat more convenient interface.
5245  *
5246  * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
5247  * pass a static string, you can save some memory by interning it first with
5248  * g_intern_static_string().
5249  **/
5250 void
5251 gtk_widget_set_accel_path (GtkWidget     *widget,
5252                            const gchar   *accel_path,
5253                            GtkAccelGroup *accel_group)
5254 {
5255   AccelPath *apath;
5256
5257   g_return_if_fail (GTK_IS_WIDGET (widget));
5258   g_return_if_fail (GTK_WIDGET_GET_CLASS (widget)->activate_signal != 0);
5259
5260   if (accel_path)
5261     {
5262       g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
5263       g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
5264
5265       gtk_accel_map_add_entry (accel_path, 0, 0);
5266       apath = g_slice_new (AccelPath);
5267       apath->accel_group = g_object_ref (accel_group);
5268       apath->path_quark = g_quark_from_string (accel_path);
5269       apath->closure = widget_new_accel_closure (widget, GTK_WIDGET_GET_CLASS (widget)->activate_signal);
5270     }
5271   else
5272     apath = NULL;
5273
5274   /* also removes possible old settings */
5275   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_path, apath, destroy_accel_path);
5276
5277   if (apath)
5278     gtk_accel_group_connect_by_path (apath->accel_group, g_quark_to_string (apath->path_quark), apath->closure);
5279
5280   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
5281 }
5282
5283 const gchar*
5284 _gtk_widget_get_accel_path (GtkWidget *widget,
5285                             gboolean  *locked)
5286 {
5287   AccelPath *apath;
5288
5289   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5290
5291   apath = g_object_get_qdata (G_OBJECT (widget), quark_accel_path);
5292   if (locked)
5293     *locked = apath ? gtk_accel_group_get_is_locked (apath->accel_group) : TRUE;
5294   return apath ? g_quark_to_string (apath->path_quark) : NULL;
5295 }
5296
5297 /**
5298  * gtk_widget_mnemonic_activate:
5299  * @widget: a #GtkWidget
5300  * @group_cycling:  %TRUE if there are other widgets with the same mnemonic
5301  *
5302  * Emits the #GtkWidget::mnemonic-activate signal.
5303  *
5304  * The default handler for this signal activates the @widget if
5305  * @group_cycling is %FALSE, and just grabs the focus if @group_cycling
5306  * is %TRUE.
5307  *
5308  * Returns: %TRUE if the signal has been handled
5309  */
5310 gboolean
5311 gtk_widget_mnemonic_activate (GtkWidget *widget,
5312                               gboolean   group_cycling)
5313 {
5314   gboolean handled;
5315
5316   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5317
5318   group_cycling = group_cycling != FALSE;
5319   if (!gtk_widget_is_sensitive (widget))
5320     handled = TRUE;
5321   else
5322     g_signal_emit (widget,
5323                    widget_signals[MNEMONIC_ACTIVATE],
5324                    0,
5325                    group_cycling,
5326                    &handled);
5327   return handled;
5328 }
5329
5330 static gboolean
5331 gtk_widget_real_mnemonic_activate (GtkWidget *widget,
5332                                    gboolean   group_cycling)
5333 {
5334   if (!group_cycling && GTK_WIDGET_GET_CLASS (widget)->activate_signal)
5335     gtk_widget_activate (widget);
5336   else if (gtk_widget_get_can_focus (widget))
5337     gtk_widget_grab_focus (widget);
5338   else
5339     {
5340       g_warning ("widget `%s' isn't suitable for mnemonic activation",
5341                  G_OBJECT_TYPE_NAME (widget));
5342       gtk_widget_error_bell (widget);
5343     }
5344   return TRUE;
5345 }
5346
5347 static const cairo_user_data_key_t event_key;
5348
5349 GdkEventExpose *
5350 _gtk_cairo_get_event (cairo_t *cr)
5351 {
5352   g_return_val_if_fail (cr != NULL, NULL);
5353
5354   return cairo_get_user_data (cr, &event_key);
5355 }
5356
5357 static void
5358 gtk_cairo_set_event (cairo_t        *cr,
5359                      GdkEventExpose *event)
5360 {
5361   cairo_set_user_data (cr, &event_key, event, NULL);
5362 }
5363
5364 /**
5365  * gtk_cairo_should_draw_window:
5366  * @cr: a cairo context
5367  * @window: the window to check
5368  *
5369  * This function is supposed to be called in #GtkWidget::draw
5370  * implementations for widgets that support multiple windows.
5371  * @cr must be untransformed from invoking of the draw function.
5372  * This function will return %TRUE if the contents of the given
5373  * @window are supposed to be drawn and %FALSE otherwise. Note
5374  * that when the drawing was not initiated by the windowing
5375  * system this function will return %TRUE for all windows, so
5376  * you need to draw the bottommost window first. Also, do not
5377  * use "else if" statements to check which window should be drawn.
5378  *
5379  * Returns: %TRUE if @window should be drawn
5380  **/
5381 gboolean
5382 gtk_cairo_should_draw_window (cairo_t *cr,
5383                               GdkWindow *window)
5384 {
5385   GdkEventExpose *event;
5386
5387   g_return_val_if_fail (cr != NULL, FALSE);
5388   g_return_val_if_fail (GDK_IS_WINDOW (window), FALSE);
5389
5390   event = _gtk_cairo_get_event (cr);
5391
5392   return event == NULL ||
5393          event->window == window;
5394 }
5395
5396 /* code shared by gtk_container_propagate_draw() and
5397  * gtk_widget_draw()
5398  */
5399 void
5400 _gtk_widget_draw_internal (GtkWidget *widget,
5401                            cairo_t   *cr,
5402                            gboolean   clip_to_size)
5403 {
5404   if (!gtk_widget_is_drawable (widget))
5405     return;
5406
5407   if (clip_to_size)
5408     {
5409       cairo_rectangle (cr,
5410                        0, 0,
5411                        widget->priv->allocation.width,
5412                        widget->priv->allocation.height);
5413       cairo_clip (cr);
5414     }
5415
5416   if (gdk_cairo_get_clip_rectangle (cr, NULL))
5417     {
5418       gboolean result;
5419
5420       g_signal_emit (widget, widget_signals[DRAW],
5421                      0, cr,
5422                      &result);
5423     }
5424 }
5425
5426 /**
5427  * gtk_widget_draw:
5428  * @widget: the widget to draw. It must be drawable (see
5429  *   gtk_widget_is_drawable()) and a size must have been allocated.
5430  * @cr: a cairo context to draw to
5431  *
5432  * Draws @widget to @cr. The top left corner of the widget will be
5433  * drawn to the currently set origin point of @cr.
5434  *
5435  * You should pass a cairo context as @cr argument that is in an
5436  * original state. Otherwise the resulting drawing is undefined. For
5437  * example changing the operator using cairo_set_operator() or the
5438  * line width using cairo_set_line_width() might have unwanted side
5439  * effects.
5440  * You may however change the context's transform matrix - like with
5441  * cairo_scale(), cairo_translate() or cairo_set_matrix() and clip
5442  * region with cairo_clip() prior to calling this function. Also, it
5443  * is fine to modify the context with cairo_save() and
5444  * cairo_push_group() prior to calling this function.
5445  *
5446  * <note><para>Special purpose widgets may contain special code for
5447  * rendering to the screen and might appear differently on screen
5448  * and when rendered using gtk_widget_draw().</para></note>
5449  **/
5450 void
5451 gtk_widget_draw (GtkWidget *widget,
5452                  cairo_t   *cr)
5453 {
5454   GdkEventExpose *tmp_event;
5455
5456   g_return_if_fail (GTK_IS_WIDGET (widget));
5457   g_return_if_fail (!widget->priv->alloc_needed);
5458   g_return_if_fail (cr != NULL);
5459
5460   cairo_save (cr);
5461   /* We have to reset the event here so that draw functions can call
5462    * gtk_widget_draw() on random other widgets and get the desired
5463    * effect: Drawing all contents, not just the current window.
5464    */
5465   tmp_event = _gtk_cairo_get_event (cr);
5466   gtk_cairo_set_event (cr, NULL);
5467
5468   _gtk_widget_draw_internal (widget, cr, TRUE);
5469
5470   gtk_cairo_set_event (cr, tmp_event);
5471   cairo_restore (cr);
5472 }
5473
5474 static gboolean
5475 gtk_widget_real_key_press_event (GtkWidget         *widget,
5476                                  GdkEventKey       *event)
5477 {
5478   return gtk_bindings_activate_event (G_OBJECT (widget), event);
5479 }
5480
5481 static gboolean
5482 gtk_widget_real_key_release_event (GtkWidget         *widget,
5483                                    GdkEventKey       *event)
5484 {
5485   return gtk_bindings_activate_event (G_OBJECT (widget), event);
5486 }
5487
5488 static gboolean
5489 gtk_widget_real_focus_in_event (GtkWidget     *widget,
5490                                 GdkEventFocus *event)
5491 {
5492   gtk_widget_queue_shallow_draw (widget);
5493
5494   return FALSE;
5495 }
5496
5497 static gboolean
5498 gtk_widget_real_focus_out_event (GtkWidget     *widget,
5499                                  GdkEventFocus *event)
5500 {
5501   gtk_widget_queue_shallow_draw (widget);
5502
5503   return FALSE;
5504 }
5505
5506 #define WIDGET_REALIZED_FOR_EVENT(widget, event) \
5507      (event->type == GDK_FOCUS_CHANGE || gtk_widget_get_realized(widget))
5508
5509 /**
5510  * gtk_widget_event:
5511  * @widget: a #GtkWidget
5512  * @event: a #GdkEvent
5513  *
5514  * Rarely-used function. This function is used to emit
5515  * the event signals on a widget (those signals should never
5516  * be emitted without using this function to do so).
5517  * If you want to synthesize an event though, don't use this function;
5518  * instead, use gtk_main_do_event() so the event will behave as if
5519  * it were in the event queue. Don't synthesize expose events; instead,
5520  * use gdk_window_invalidate_rect() to invalidate a region of the
5521  * window.
5522  *
5523  * Return value: return from the event signal emission (%TRUE if
5524  *               the event was handled)
5525  **/
5526 gboolean
5527 gtk_widget_event (GtkWidget *widget,
5528                   GdkEvent  *event)
5529 {
5530   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
5531   g_return_val_if_fail (WIDGET_REALIZED_FOR_EVENT (widget, event), TRUE);
5532
5533   if (event->type == GDK_EXPOSE)
5534     {
5535       g_warning ("Events of type GDK_EXPOSE cannot be synthesized. To get "
5536                  "the same effect, call gdk_window_invalidate_rect/region(), "
5537                  "followed by gdk_window_process_updates().");
5538       return TRUE;
5539     }
5540
5541   return gtk_widget_event_internal (widget, event);
5542 }
5543
5544 /* Returns TRUE if a translation should be done */
5545 static gboolean
5546 gtk_widget_get_translation_to_window (GtkWidget      *widget,
5547                                       GdkWindow      *window,
5548                                       int            *x,
5549                                       int            *y)
5550 {
5551   GdkWindow *w, *widget_window;
5552
5553   if (!gtk_widget_get_has_window (widget))
5554     {
5555       *x = -widget->priv->allocation.x;
5556       *y = -widget->priv->allocation.y;
5557     }
5558   else
5559     {
5560       *x = 0;
5561       *y = 0;
5562     }
5563
5564   widget_window = gtk_widget_get_window (widget);
5565
5566   for (w = window; w && w != widget_window; w = gdk_window_get_parent (w))
5567     {
5568       int wx, wy;
5569       gdk_window_get_position (w, &wx, &wy);
5570       *x += wx;
5571       *y += wy;
5572     }
5573
5574   if (w == NULL)
5575     {
5576       *x = 0;
5577       *y = 0;
5578       return FALSE;
5579     }
5580
5581   return TRUE;
5582 }
5583
5584
5585 /**
5586  * gtk_cairo_transform_to_window:
5587  * @cr: the cairo context to transform
5588  * @widget: the widget the context is currently centered for
5589  * @window: the window to transform the context to
5590  *
5591  * Transforms the given cairo context @cr that from @widget-relative
5592  * coordinates to @window-relative coordinates.
5593  * If the @widget's window is not an ancestor of @window, no
5594  * modification will be applied.
5595  *
5596  * This is the inverse to the transformation GTK applies when
5597  * preparing an expose event to be emitted with the #GtkWidget::draw
5598  * signal. It is intended to help porting multiwindow widgets from
5599  * GTK+ 2 to the rendering architecture of GTK+ 3.
5600  **/
5601 void
5602 gtk_cairo_transform_to_window (cairo_t   *cr,
5603                                GtkWidget *widget,
5604                                GdkWindow *window)
5605 {
5606   int x, y;
5607
5608   g_return_if_fail (cr != NULL);
5609   g_return_if_fail (GTK_IS_WIDGET (widget));
5610   g_return_if_fail (GDK_IS_WINDOW (window));
5611
5612   if (gtk_widget_get_translation_to_window (widget, window, &x, &y))
5613     cairo_translate (cr, x, y);
5614 }
5615
5616 /**
5617  * gtk_widget_send_expose:
5618  * @widget: a #GtkWidget
5619  * @event: a expose #GdkEvent
5620  *
5621  * Very rarely-used function. This function is used to emit
5622  * an expose event on a widget. This function is not normally used
5623  * directly. The only time it is used is when propagating an expose
5624  * event to a child %NO_WINDOW widget, and that is normally done
5625  * using gtk_container_propagate_draw().
5626  *
5627  * If you want to force an area of a window to be redrawn,
5628  * use gdk_window_invalidate_rect() or gdk_window_invalidate_region().
5629  * To cause the redraw to be done immediately, follow that call
5630  * with a call to gdk_window_process_updates().
5631  *
5632  * Return value: return from the event signal emission (%TRUE if
5633  *               the event was handled)
5634  **/
5635 gint
5636 gtk_widget_send_expose (GtkWidget *widget,
5637                         GdkEvent  *event)
5638 {
5639   gboolean result = FALSE;
5640   cairo_t *cr;
5641   int x, y;
5642   gboolean do_clip;
5643   GtkStyleContext *context;
5644
5645   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
5646   g_return_val_if_fail (gtk_widget_get_realized (widget), TRUE);
5647   g_return_val_if_fail (event != NULL, TRUE);
5648   g_return_val_if_fail (event->type == GDK_EXPOSE, TRUE);
5649
5650   cr = gdk_cairo_create (event->expose.window);
5651   gtk_cairo_set_event (cr, &event->expose);
5652
5653   gdk_cairo_region (cr, event->expose.region);
5654   cairo_clip (cr);
5655
5656   do_clip = gtk_widget_get_translation_to_window (widget,
5657                                                   event->expose.window,
5658                                                   &x, &y);
5659   cairo_translate (cr, -x, -y);
5660
5661   _gtk_widget_draw_internal (widget, cr, do_clip);
5662
5663   /* unset here, so if someone keeps a reference to cr we
5664    * don't leak the window. */
5665   gtk_cairo_set_event (cr, NULL);
5666   cairo_destroy (cr);
5667
5668   context = gtk_widget_get_style_context (widget);
5669   _gtk_style_context_coalesce_animation_areas (context);
5670
5671   return result;
5672 }
5673
5674 static gboolean
5675 event_window_is_still_viewable (GdkEvent *event)
5676 {
5677   /* Check that we think the event's window is viewable before
5678    * delivering the event, to prevent suprises. We do this here
5679    * at the last moment, since the event may have been queued
5680    * up behind other events, held over a recursive main loop, etc.
5681    */
5682   switch (event->type)
5683     {
5684     case GDK_EXPOSE:
5685     case GDK_MOTION_NOTIFY:
5686     case GDK_BUTTON_PRESS:
5687     case GDK_2BUTTON_PRESS:
5688     case GDK_3BUTTON_PRESS:
5689     case GDK_KEY_PRESS:
5690     case GDK_ENTER_NOTIFY:
5691     case GDK_PROXIMITY_IN:
5692     case GDK_SCROLL:
5693       return event->any.window && gdk_window_is_viewable (event->any.window);
5694
5695 #if 0
5696     /* The following events are the second half of paired events;
5697      * we always deliver them to deal with widgets that clean up
5698      * on the second half.
5699      */
5700     case GDK_BUTTON_RELEASE:
5701     case GDK_KEY_RELEASE:
5702     case GDK_LEAVE_NOTIFY:
5703     case GDK_PROXIMITY_OUT:
5704 #endif
5705
5706     default:
5707       /* Remaining events would make sense on an not-viewable window,
5708        * or don't have an associated window.
5709        */
5710       return TRUE;
5711     }
5712 }
5713
5714 static gint
5715 gtk_widget_event_internal (GtkWidget *widget,
5716                            GdkEvent  *event)
5717 {
5718   gboolean return_val = FALSE;
5719
5720   /* We check only once for is-still-visible; if someone
5721    * hides the window in on of the signals on the widget,
5722    * they are responsible for returning TRUE to terminate
5723    * handling.
5724    */
5725   if (!event_window_is_still_viewable (event))
5726     return TRUE;
5727
5728   g_object_ref (widget);
5729
5730   g_signal_emit (widget, widget_signals[EVENT], 0, event, &return_val);
5731   return_val |= !WIDGET_REALIZED_FOR_EVENT (widget, event);
5732   if (!return_val)
5733     {
5734       gint signal_num;
5735
5736       switch (event->type)
5737         {
5738         case GDK_NOTHING:
5739           signal_num = -1;
5740           break;
5741         case GDK_BUTTON_PRESS:
5742         case GDK_2BUTTON_PRESS:
5743         case GDK_3BUTTON_PRESS:
5744           signal_num = BUTTON_PRESS_EVENT;
5745           break;
5746         case GDK_SCROLL:
5747           signal_num = SCROLL_EVENT;
5748           break;
5749         case GDK_BUTTON_RELEASE:
5750           signal_num = BUTTON_RELEASE_EVENT;
5751           break;
5752         case GDK_MOTION_NOTIFY:
5753           signal_num = MOTION_NOTIFY_EVENT;
5754           break;
5755         case GDK_DELETE:
5756           signal_num = DELETE_EVENT;
5757           break;
5758         case GDK_DESTROY:
5759           signal_num = DESTROY_EVENT;
5760           _gtk_tooltip_hide (widget);
5761           break;
5762         case GDK_KEY_PRESS:
5763           signal_num = KEY_PRESS_EVENT;
5764           break;
5765         case GDK_KEY_RELEASE:
5766           signal_num = KEY_RELEASE_EVENT;
5767           break;
5768         case GDK_ENTER_NOTIFY:
5769           signal_num = ENTER_NOTIFY_EVENT;
5770           break;
5771         case GDK_LEAVE_NOTIFY:
5772           signal_num = LEAVE_NOTIFY_EVENT;
5773           break;
5774         case GDK_FOCUS_CHANGE:
5775           signal_num = event->focus_change.in ? FOCUS_IN_EVENT : FOCUS_OUT_EVENT;
5776           if (event->focus_change.in)
5777             _gtk_tooltip_focus_in (widget);
5778           else
5779             _gtk_tooltip_focus_out (widget);
5780           break;
5781         case GDK_CONFIGURE:
5782           signal_num = CONFIGURE_EVENT;
5783           break;
5784         case GDK_MAP:
5785           signal_num = MAP_EVENT;
5786           break;
5787         case GDK_UNMAP:
5788           signal_num = UNMAP_EVENT;
5789           break;
5790         case GDK_WINDOW_STATE:
5791           signal_num = WINDOW_STATE_EVENT;
5792           break;
5793         case GDK_PROPERTY_NOTIFY:
5794           signal_num = PROPERTY_NOTIFY_EVENT;
5795           break;
5796         case GDK_SELECTION_CLEAR:
5797           signal_num = SELECTION_CLEAR_EVENT;
5798           break;
5799         case GDK_SELECTION_REQUEST:
5800           signal_num = SELECTION_REQUEST_EVENT;
5801           break;
5802         case GDK_SELECTION_NOTIFY:
5803           signal_num = SELECTION_NOTIFY_EVENT;
5804           break;
5805         case GDK_PROXIMITY_IN:
5806           signal_num = PROXIMITY_IN_EVENT;
5807           break;
5808         case GDK_PROXIMITY_OUT:
5809           signal_num = PROXIMITY_OUT_EVENT;
5810           break;
5811         case GDK_CLIENT_EVENT:
5812           signal_num = CLIENT_EVENT;
5813           break;
5814         case GDK_EXPOSE:
5815           signal_num = EXPOSE_EVENT;
5816           break;
5817         case GDK_VISIBILITY_NOTIFY:
5818           signal_num = VISIBILITY_NOTIFY_EVENT;
5819           break;
5820         case GDK_GRAB_BROKEN:
5821           signal_num = GRAB_BROKEN_EVENT;
5822           break;
5823         case GDK_DAMAGE:
5824           signal_num = DAMAGE_EVENT;
5825           break;
5826         default:
5827           g_warning ("gtk_widget_event(): unhandled event type: %d", event->type);
5828           signal_num = -1;
5829           break;
5830         }
5831       if (signal_num != -1)
5832         g_signal_emit (widget, widget_signals[signal_num], 0, event, &return_val);
5833     }
5834   if (WIDGET_REALIZED_FOR_EVENT (widget, event))
5835     g_signal_emit (widget, widget_signals[EVENT_AFTER], 0, event);
5836   else
5837     return_val = TRUE;
5838
5839   g_object_unref (widget);
5840
5841   return return_val;
5842 }
5843
5844 /**
5845  * gtk_widget_activate:
5846  * @widget: a #GtkWidget that's activatable
5847  *
5848  * For widgets that can be "activated" (buttons, menu items, etc.)
5849  * this function activates them. Activation is what happens when you
5850  * press Enter on a widget during key navigation. If @widget isn't
5851  * activatable, the function returns %FALSE.
5852  *
5853  * Return value: %TRUE if the widget was activatable
5854  **/
5855 gboolean
5856 gtk_widget_activate (GtkWidget *widget)
5857 {
5858   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5859
5860   if (WIDGET_CLASS (widget)->activate_signal)
5861     {
5862       /* FIXME: we should eventually check the signals signature here */
5863       g_signal_emit (widget, WIDGET_CLASS (widget)->activate_signal, 0);
5864
5865       return TRUE;
5866     }
5867   else
5868     return FALSE;
5869 }
5870
5871 static void
5872 gtk_widget_reparent_subwindows (GtkWidget *widget,
5873                                 GdkWindow *new_window)
5874 {
5875   GtkWidgetPrivate *priv = widget->priv;
5876
5877   if (!gtk_widget_get_has_window (widget))
5878     {
5879       GList *children = gdk_window_get_children (priv->window);
5880       GList *tmp_list;
5881
5882       for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
5883         {
5884           GdkWindow *window = tmp_list->data;
5885           gpointer child;
5886
5887           gdk_window_get_user_data (window, &child);
5888           while (child && child != widget)
5889             child = ((GtkWidget*) child)->priv->parent;
5890
5891           if (child)
5892             gdk_window_reparent (window, new_window, 0, 0);
5893         }
5894
5895       g_list_free (children);
5896     }
5897   else
5898    {
5899      GdkWindow *parent;
5900      GList *tmp_list, *children;
5901
5902      parent = gdk_window_get_parent (priv->window);
5903
5904      if (parent == NULL)
5905        gdk_window_reparent (priv->window, new_window, 0, 0);
5906      else
5907        {
5908          children = gdk_window_get_children (parent);
5909
5910          for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
5911            {
5912              GdkWindow *window = tmp_list->data;
5913              gpointer child;
5914
5915              gdk_window_get_user_data (window, &child);
5916
5917              if (child == widget)
5918                gdk_window_reparent (window, new_window, 0, 0);
5919            }
5920
5921          g_list_free (children);
5922        }
5923    }
5924 }
5925
5926 static void
5927 gtk_widget_reparent_fixup_child (GtkWidget *widget,
5928                                  gpointer   client_data)
5929 {
5930   GtkWidgetPrivate *priv = widget->priv;
5931
5932   g_assert (client_data != NULL);
5933
5934   if (!gtk_widget_get_has_window (widget))
5935     {
5936       if (priv->window)
5937         g_object_unref (priv->window);
5938       priv->window = (GdkWindow*) client_data;
5939       if (priv->window)
5940         g_object_ref (priv->window);
5941
5942       if (GTK_IS_CONTAINER (widget))
5943         gtk_container_forall (GTK_CONTAINER (widget),
5944                               gtk_widget_reparent_fixup_child,
5945                               client_data);
5946     }
5947 }
5948
5949 /**
5950  * gtk_widget_reparent:
5951  * @widget: a #GtkWidget
5952  * @new_parent: a #GtkContainer to move the widget into
5953  *
5954  * Moves a widget from one #GtkContainer to another, handling reference
5955  * count issues to avoid destroying the widget.
5956  **/
5957 void
5958 gtk_widget_reparent (GtkWidget *widget,
5959                      GtkWidget *new_parent)
5960 {
5961   GtkWidgetPrivate *priv;
5962
5963   g_return_if_fail (GTK_IS_WIDGET (widget));
5964   g_return_if_fail (GTK_IS_CONTAINER (new_parent));
5965   priv = widget->priv;
5966   g_return_if_fail (priv->parent != NULL);
5967
5968   if (priv->parent != new_parent)
5969     {
5970       /* First try to see if we can get away without unrealizing
5971        * the widget as we reparent it. if so we set a flag so
5972        * that gtk_widget_unparent doesn't unrealize widget
5973        */
5974       if (gtk_widget_get_realized (widget) && gtk_widget_get_realized (new_parent))
5975         priv->in_reparent = TRUE;
5976
5977       g_object_ref (widget);
5978       gtk_container_remove (GTK_CONTAINER (priv->parent), widget);
5979       gtk_container_add (GTK_CONTAINER (new_parent), widget);
5980       g_object_unref (widget);
5981
5982       if (priv->in_reparent)
5983         {
5984           priv->in_reparent = FALSE;
5985
5986           gtk_widget_reparent_subwindows (widget, gtk_widget_get_parent_window (widget));
5987           gtk_widget_reparent_fixup_child (widget,
5988                                            gtk_widget_get_parent_window (widget));
5989         }
5990
5991       g_object_notify (G_OBJECT (widget), "parent");
5992     }
5993 }
5994
5995 /**
5996  * gtk_widget_intersect:
5997  * @widget: a #GtkWidget
5998  * @area: a rectangle
5999  * @intersection: rectangle to store intersection of @widget and @area
6000  *
6001  * Computes the intersection of a @widget's area and @area, storing
6002  * the intersection in @intersection, and returns %TRUE if there was
6003  * an intersection.  @intersection may be %NULL if you're only
6004  * interested in whether there was an intersection.
6005  *
6006  * Return value: %TRUE if there was an intersection
6007  **/
6008 gboolean
6009 gtk_widget_intersect (GtkWidget          *widget,
6010                       const GdkRectangle *area,
6011                       GdkRectangle       *intersection)
6012 {
6013   GtkWidgetPrivate *priv;
6014   GdkRectangle *dest;
6015   GdkRectangle tmp;
6016   gint return_val;
6017
6018   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6019   g_return_val_if_fail (area != NULL, FALSE);
6020
6021   priv = widget->priv;
6022
6023   if (intersection)
6024     dest = intersection;
6025   else
6026     dest = &tmp;
6027
6028   return_val = gdk_rectangle_intersect (&priv->allocation, area, dest);
6029
6030   if (return_val && intersection && gtk_widget_get_has_window (widget))
6031     {
6032       intersection->x -= priv->allocation.x;
6033       intersection->y -= priv->allocation.y;
6034     }
6035
6036   return return_val;
6037 }
6038
6039 /**
6040  * gtk_widget_region_intersect:
6041  * @widget: a #GtkWidget
6042  * @region: a #cairo_region_t, in the same coordinate system as
6043  *          @widget->allocation. That is, relative to @widget->window
6044  *          for %NO_WINDOW widgets; relative to the parent window
6045  *          of @widget->window for widgets with their own window.
6046  * @returns: A newly allocated region holding the intersection of @widget
6047  *           and @region. The coordinates of the return value are
6048  *           relative to @widget->window for %NO_WINDOW widgets, and
6049  *           relative to the parent window of @widget->window for
6050  *           widgets with their own window.
6051  *
6052  * Computes the intersection of a @widget's area and @region, returning
6053  * the intersection. The result may be empty, use cairo_region_is_empty() to
6054  * check.
6055  **/
6056 cairo_region_t *
6057 gtk_widget_region_intersect (GtkWidget       *widget,
6058                              const cairo_region_t *region)
6059 {
6060   GdkRectangle rect;
6061   cairo_region_t *dest;
6062
6063   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6064   g_return_val_if_fail (region != NULL, NULL);
6065
6066   gtk_widget_get_allocation (widget, &rect);
6067
6068   dest = cairo_region_create_rectangle (&rect);
6069
6070   cairo_region_intersect (dest, region);
6071
6072   return dest;
6073 }
6074
6075 /**
6076  * _gtk_widget_grab_notify:
6077  * @widget: a #GtkWidget
6078  * @was_grabbed: whether a grab is now in effect
6079  *
6080  * Emits the #GtkWidget::grab-notify signal on @widget.
6081  *
6082  * Since: 2.6
6083  **/
6084 void
6085 _gtk_widget_grab_notify (GtkWidget *widget,
6086                          gboolean   was_grabbed)
6087 {
6088   g_signal_emit (widget, widget_signals[GRAB_NOTIFY], 0, was_grabbed);
6089 }
6090
6091 /**
6092  * gtk_widget_grab_focus:
6093  * @widget: a #GtkWidget
6094  *
6095  * Causes @widget to have the keyboard focus for the #GtkWindow it's
6096  * inside. @widget must be a focusable widget, such as a #GtkEntry;
6097  * something like #GtkFrame won't work.
6098  *
6099  * More precisely, it must have the %GTK_CAN_FOCUS flag set. Use
6100  * gtk_widget_set_can_focus() to modify that flag.
6101  *
6102  * The widget also needs to be realized and mapped. This is indicated by the
6103  * related signals. Grabbing the focus immediately after creating the widget
6104  * will likely fail and cause critical warnings.
6105  **/
6106 void
6107 gtk_widget_grab_focus (GtkWidget *widget)
6108 {
6109   g_return_if_fail (GTK_IS_WIDGET (widget));
6110
6111   if (!gtk_widget_is_sensitive (widget))
6112     return;
6113
6114   g_object_ref (widget);
6115   g_signal_emit (widget, widget_signals[GRAB_FOCUS], 0);
6116   g_object_notify (G_OBJECT (widget), "has-focus");
6117   g_object_unref (widget);
6118 }
6119
6120 static void
6121 reset_focus_recurse (GtkWidget *widget,
6122                      gpointer   data)
6123 {
6124   if (GTK_IS_CONTAINER (widget))
6125     {
6126       GtkContainer *container;
6127
6128       container = GTK_CONTAINER (widget);
6129       gtk_container_set_focus_child (container, NULL);
6130
6131       gtk_container_foreach (container,
6132                              reset_focus_recurse,
6133                              NULL);
6134     }
6135 }
6136
6137 static void
6138 gtk_widget_real_grab_focus (GtkWidget *focus_widget)
6139 {
6140   if (gtk_widget_get_can_focus (focus_widget))
6141     {
6142       GtkWidget *toplevel;
6143       GtkWidget *widget;
6144
6145       /* clear the current focus setting, break if the current widget
6146        * is the focus widget's parent, since containers above that will
6147        * be set by the next loop.
6148        */
6149       toplevel = gtk_widget_get_toplevel (focus_widget);
6150       if (gtk_widget_is_toplevel (toplevel) && GTK_IS_WINDOW (toplevel))
6151         {
6152           widget = gtk_window_get_focus (GTK_WINDOW (toplevel));
6153
6154           if (widget == focus_widget)
6155             {
6156               /* We call _gtk_window_internal_set_focus() here so that the
6157                * toplevel window can request the focus if necessary.
6158                * This is needed when the toplevel is a GtkPlug
6159                */
6160               if (!gtk_widget_has_focus (widget))
6161                 _gtk_window_internal_set_focus (GTK_WINDOW (toplevel), focus_widget);
6162
6163               return;
6164             }
6165
6166           if (widget)
6167             {
6168               while (widget->priv->parent && widget->priv->parent != focus_widget->priv->parent)
6169                 {
6170                   widget = widget->priv->parent;
6171                   gtk_container_set_focus_child (GTK_CONTAINER (widget), NULL);
6172                 }
6173             }
6174         }
6175       else if (toplevel != focus_widget)
6176         {
6177           /* gtk_widget_grab_focus() operates on a tree without window...
6178            * actually, this is very questionable behaviour.
6179            */
6180
6181           gtk_container_foreach (GTK_CONTAINER (toplevel),
6182                                  reset_focus_recurse,
6183                                  NULL);
6184         }
6185
6186       /* now propagate the new focus up the widget tree and finally
6187        * set it on the window
6188        */
6189       widget = focus_widget;
6190       while (widget->priv->parent)
6191         {
6192           gtk_container_set_focus_child (GTK_CONTAINER (widget->priv->parent), widget);
6193           widget = widget->priv->parent;
6194         }
6195       if (GTK_IS_WINDOW (widget))
6196         _gtk_window_internal_set_focus (GTK_WINDOW (widget), focus_widget);
6197     }
6198 }
6199
6200 static gboolean
6201 gtk_widget_real_query_tooltip (GtkWidget  *widget,
6202                                gint        x,
6203                                gint        y,
6204                                gboolean    keyboard_tip,
6205                                GtkTooltip *tooltip)
6206 {
6207   gchar *tooltip_markup;
6208   gboolean has_tooltip;
6209
6210   tooltip_markup = g_object_get_qdata (G_OBJECT (widget), quark_tooltip_markup);
6211   has_tooltip = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget), quark_has_tooltip));
6212
6213   if (has_tooltip && tooltip_markup)
6214     {
6215       gtk_tooltip_set_markup (tooltip, tooltip_markup);
6216       return TRUE;
6217     }
6218
6219   return FALSE;
6220 }
6221
6222 static gboolean
6223 gtk_widget_real_show_help (GtkWidget        *widget,
6224                            GtkWidgetHelpType help_type)
6225 {
6226   if (help_type == GTK_WIDGET_HELP_TOOLTIP)
6227     {
6228       _gtk_tooltip_toggle_keyboard_mode (widget);
6229
6230       return TRUE;
6231     }
6232   else
6233     return FALSE;
6234 }
6235
6236 static gboolean
6237 gtk_widget_real_focus (GtkWidget         *widget,
6238                        GtkDirectionType   direction)
6239 {
6240   if (!gtk_widget_get_can_focus (widget))
6241     return FALSE;
6242
6243   if (!gtk_widget_is_focus (widget))
6244     {
6245       gtk_widget_grab_focus (widget);
6246       return TRUE;
6247     }
6248   else
6249     return FALSE;
6250 }
6251
6252 static void
6253 gtk_widget_real_move_focus (GtkWidget         *widget,
6254                             GtkDirectionType   direction)
6255 {
6256   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
6257
6258   if (widget != toplevel && GTK_IS_WINDOW (toplevel))
6259     {
6260       g_signal_emit (toplevel, widget_signals[MOVE_FOCUS], 0,
6261                      direction);
6262     }
6263 }
6264
6265 static gboolean
6266 gtk_widget_real_keynav_failed (GtkWidget        *widget,
6267                                GtkDirectionType  direction)
6268 {
6269   gboolean cursor_only;
6270
6271   switch (direction)
6272     {
6273     case GTK_DIR_TAB_FORWARD:
6274     case GTK_DIR_TAB_BACKWARD:
6275       return FALSE;
6276
6277     case GTK_DIR_UP:
6278     case GTK_DIR_DOWN:
6279     case GTK_DIR_LEFT:
6280     case GTK_DIR_RIGHT:
6281       g_object_get (gtk_widget_get_settings (widget),
6282                     "gtk-keynav-cursor-only", &cursor_only,
6283                     NULL);
6284       if (cursor_only)
6285         return FALSE;
6286       break;
6287     }
6288
6289   gtk_widget_error_bell (widget);
6290
6291   return TRUE;
6292 }
6293
6294 /**
6295  * gtk_widget_set_can_focus:
6296  * @widget: a #GtkWidget
6297  * @can_focus: whether or not @widget can own the input focus.
6298  *
6299  * Specifies whether @widget can own the input focus. See
6300  * gtk_widget_grab_focus() for actually setting the input focus on a
6301  * widget.
6302  *
6303  * Since: 2.18
6304  **/
6305 void
6306 gtk_widget_set_can_focus (GtkWidget *widget,
6307                           gboolean   can_focus)
6308 {
6309   g_return_if_fail (GTK_IS_WIDGET (widget));
6310
6311   if (widget->priv->can_focus != can_focus)
6312     {
6313       widget->priv->can_focus = can_focus;
6314
6315       gtk_widget_queue_resize (widget);
6316       g_object_notify (G_OBJECT (widget), "can-focus");
6317     }
6318 }
6319
6320 /**
6321  * gtk_widget_get_can_focus:
6322  * @widget: a #GtkWidget
6323  *
6324  * Determines whether @widget can own the input focus. See
6325  * gtk_widget_set_can_focus().
6326  *
6327  * Return value: %TRUE if @widget can own the input focus, %FALSE otherwise
6328  *
6329  * Since: 2.18
6330  **/
6331 gboolean
6332 gtk_widget_get_can_focus (GtkWidget *widget)
6333 {
6334   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6335
6336   return widget->priv->can_focus;
6337 }
6338
6339 /**
6340  * gtk_widget_has_focus:
6341  * @widget: a #GtkWidget
6342  *
6343  * Determines if the widget has the global input focus. See
6344  * gtk_widget_is_focus() for the difference between having the global
6345  * input focus, and only having the focus within a toplevel.
6346  *
6347  * Return value: %TRUE if the widget has the global input focus.
6348  *
6349  * Since: 2.18
6350  **/
6351 gboolean
6352 gtk_widget_has_focus (GtkWidget *widget)
6353 {
6354   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6355
6356   return widget->priv->has_focus;
6357 }
6358
6359 /**
6360  * gtk_widget_is_focus:
6361  * @widget: a #GtkWidget
6362  *
6363  * Determines if the widget is the focus widget within its
6364  * toplevel. (This does not mean that the %HAS_FOCUS flag is
6365  * necessarily set; %HAS_FOCUS will only be set if the
6366  * toplevel widget additionally has the global input focus.)
6367  *
6368  * Return value: %TRUE if the widget is the focus widget.
6369  **/
6370 gboolean
6371 gtk_widget_is_focus (GtkWidget *widget)
6372 {
6373   GtkWidget *toplevel;
6374
6375   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6376
6377   toplevel = gtk_widget_get_toplevel (widget);
6378
6379   if (GTK_IS_WINDOW (toplevel))
6380     return widget == gtk_window_get_focus (GTK_WINDOW (toplevel));
6381   else
6382     return FALSE;
6383 }
6384
6385 /**
6386  * gtk_widget_set_can_default:
6387  * @widget: a #GtkWidget
6388  * @can_default: whether or not @widget can be a default widget.
6389  *
6390  * Specifies whether @widget can be a default widget. See
6391  * gtk_widget_grab_default() for details about the meaning of
6392  * "default".
6393  *
6394  * Since: 2.18
6395  **/
6396 void
6397 gtk_widget_set_can_default (GtkWidget *widget,
6398                             gboolean   can_default)
6399 {
6400   g_return_if_fail (GTK_IS_WIDGET (widget));
6401
6402   if (widget->priv->can_default != can_default)
6403     {
6404       widget->priv->can_default = can_default;
6405
6406       gtk_widget_queue_resize (widget);
6407       g_object_notify (G_OBJECT (widget), "can-default");
6408     }
6409 }
6410
6411 /**
6412  * gtk_widget_get_can_default:
6413  * @widget: a #GtkWidget
6414  *
6415  * Determines whether @widget can be a default widget. See
6416  * gtk_widget_set_can_default().
6417  *
6418  * Return value: %TRUE if @widget can be a default widget, %FALSE otherwise
6419  *
6420  * Since: 2.18
6421  **/
6422 gboolean
6423 gtk_widget_get_can_default (GtkWidget *widget)
6424 {
6425   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6426
6427   return widget->priv->can_default;
6428 }
6429
6430 /**
6431  * gtk_widget_has_default:
6432  * @widget: a #GtkWidget
6433  *
6434  * Determines whether @widget is the current default widget within its
6435  * toplevel. See gtk_widget_set_can_default().
6436  *
6437  * Return value: %TRUE if @widget is the current default widget within
6438  *     its toplevel, %FALSE otherwise
6439  *
6440  * Since: 2.18
6441  */
6442 gboolean
6443 gtk_widget_has_default (GtkWidget *widget)
6444 {
6445   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6446
6447   return widget->priv->has_default;
6448 }
6449
6450 void
6451 _gtk_widget_set_has_default (GtkWidget *widget,
6452                              gboolean   has_default)
6453 {
6454   widget->priv->has_default = has_default;
6455 }
6456
6457 /**
6458  * gtk_widget_grab_default:
6459  * @widget: a #GtkWidget
6460  *
6461  * Causes @widget to become the default widget. @widget must have the
6462  * %GTK_CAN_DEFAULT flag set; typically you have to set this flag
6463  * yourself by calling <literal>gtk_widget_set_can_default (@widget,
6464  * %TRUE)</literal>. The default widget is activated when
6465  * the user presses Enter in a window. Default widgets must be
6466  * activatable, that is, gtk_widget_activate() should affect them. Note
6467  * that #GtkEntry widgets require the "activates-default" property
6468  * set to %TRUE before they activate the default widget when Enter
6469  * is pressed and the #GtkEntry is focused.
6470  **/
6471 void
6472 gtk_widget_grab_default (GtkWidget *widget)
6473 {
6474   GtkWidget *window;
6475
6476   g_return_if_fail (GTK_IS_WIDGET (widget));
6477   g_return_if_fail (gtk_widget_get_can_default (widget));
6478
6479   window = gtk_widget_get_toplevel (widget);
6480
6481   if (window && gtk_widget_is_toplevel (window))
6482     gtk_window_set_default (GTK_WINDOW (window), widget);
6483   else
6484     g_warning (G_STRLOC ": widget not within a GtkWindow");
6485 }
6486
6487 /**
6488  * gtk_widget_set_receives_default:
6489  * @widget: a #GtkWidget
6490  * @receives_default: whether or not @widget can be a default widget.
6491  *
6492  * Specifies whether @widget will be treated as the default widget
6493  * within its toplevel when it has the focus, even if another widget
6494  * is the default.
6495  *
6496  * See gtk_widget_grab_default() for details about the meaning of
6497  * "default".
6498  *
6499  * Since: 2.18
6500  **/
6501 void
6502 gtk_widget_set_receives_default (GtkWidget *widget,
6503                                  gboolean   receives_default)
6504 {
6505   g_return_if_fail (GTK_IS_WIDGET (widget));
6506
6507   if (widget->priv->receives_default != receives_default)
6508     {
6509       widget->priv->receives_default = receives_default;
6510
6511       g_object_notify (G_OBJECT (widget), "receives-default");
6512     }
6513 }
6514
6515 /**
6516  * gtk_widget_get_receives_default:
6517  * @widget: a #GtkWidget
6518  *
6519  * Determines whether @widget is alyways treated as default widget
6520  * withing its toplevel when it has the focus, even if another widget
6521  * is the default.
6522  *
6523  * See gtk_widget_set_receives_default().
6524  *
6525  * Return value: %TRUE if @widget acts as default widget when focussed,
6526  *               %FALSE otherwise
6527  *
6528  * Since: 2.18
6529  **/
6530 gboolean
6531 gtk_widget_get_receives_default (GtkWidget *widget)
6532 {
6533   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6534
6535   return widget->priv->receives_default;
6536 }
6537
6538 /**
6539  * gtk_widget_has_grab:
6540  * @widget: a #GtkWidget
6541  *
6542  * Determines whether the widget is currently grabbing events, so it
6543  * is the only widget receiving input events (keyboard and mouse).
6544  *
6545  * See also gtk_grab_add().
6546  *
6547  * Return value: %TRUE if the widget is in the grab_widgets stack
6548  *
6549  * Since: 2.18
6550  **/
6551 gboolean
6552 gtk_widget_has_grab (GtkWidget *widget)
6553 {
6554   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6555
6556   return widget->priv->has_grab;
6557 }
6558
6559 void
6560 _gtk_widget_set_has_grab (GtkWidget *widget,
6561                           gboolean   has_grab)
6562 {
6563   widget->priv->has_grab = has_grab;
6564 }
6565
6566 /**
6567  * gtk_widget_device_is_shadowed:
6568  * @widget: a #GtkWidget
6569  * @device: a #GdkDevice
6570  *
6571  * Returns %TRUE if @device has been shadowed by a GTK+
6572  * device grab on another widget, so it would stop sending
6573  * events to @widget. This may be used in the
6574  * #GtkWidget::grab-notify signal to check for specific
6575  * devices. See gtk_device_grab_add().
6576  *
6577  * Returns: %TRUE if there is an ongoing grab on @device
6578  *          by another #GtkWidget than @widget.
6579  *
6580  * Since: 3.0
6581  **/
6582 gboolean
6583 gtk_widget_device_is_shadowed (GtkWidget *widget,
6584                                GdkDevice *device)
6585 {
6586   GtkWindowGroup *group;
6587   GtkWidget *grab_widget, *toplevel;
6588
6589   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6590   g_return_val_if_fail (GDK_IS_DEVICE (device), FALSE);
6591
6592   if (!gtk_widget_get_realized (widget))
6593     return TRUE;
6594
6595   toplevel = gtk_widget_get_toplevel (widget);
6596
6597   if (GTK_IS_WINDOW (toplevel))
6598     group = gtk_window_get_group (GTK_WINDOW (toplevel));
6599   else
6600     group = gtk_window_get_group (NULL);
6601
6602   grab_widget = gtk_window_group_get_current_device_grab (group, device);
6603
6604   /* Widget not inside the hierarchy of grab_widget */
6605   if (grab_widget &&
6606       widget != grab_widget &&
6607       !gtk_widget_is_ancestor (widget, grab_widget))
6608     return TRUE;
6609
6610   grab_widget = gtk_window_group_get_current_grab (group);
6611   if (grab_widget && widget != grab_widget &&
6612       !gtk_widget_is_ancestor (widget, grab_widget))
6613     return TRUE;
6614
6615   return FALSE;
6616 }
6617
6618 /**
6619  * gtk_widget_set_name:
6620  * @widget: a #GtkWidget
6621  * @name: name for the widget
6622  *
6623  * Widgets can be named, which allows you to refer to them from a
6624  * gtkrc file. You can apply a style to widgets with a particular name
6625  * in the gtkrc file. See the documentation for gtkrc files (on the
6626  * same page as the docs for #GtkRcStyle).
6627  *
6628  * Note that widget names are separated by periods in paths (see
6629  * gtk_widget_path()), so names with embedded periods may cause confusion.
6630  **/
6631 void
6632 gtk_widget_set_name (GtkWidget   *widget,
6633                      const gchar *name)
6634 {
6635   GtkWidgetPrivate *priv;
6636   gchar *new_name;
6637
6638   g_return_if_fail (GTK_IS_WIDGET (widget));
6639
6640   priv = widget->priv;
6641
6642   new_name = g_strdup (name);
6643   g_free (priv->name);
6644   priv->name = new_name;
6645
6646   if (priv->rc_style)
6647     gtk_widget_reset_rc_style (widget);
6648
6649   g_object_notify (G_OBJECT (widget), "name");
6650 }
6651
6652 /**
6653  * gtk_widget_get_name:
6654  * @widget: a #GtkWidget
6655  *
6656  * Retrieves the name of a widget. See gtk_widget_set_name() for the
6657  * significance of widget names.
6658  *
6659  * Return value: name of the widget. This string is owned by GTK+ and
6660  * should not be modified or freed
6661  **/
6662 G_CONST_RETURN gchar*
6663 gtk_widget_get_name (GtkWidget *widget)
6664 {
6665   GtkWidgetPrivate *priv;
6666
6667   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6668
6669   priv = widget->priv;
6670
6671   if (priv->name)
6672     return priv->name;
6673   return G_OBJECT_TYPE_NAME (widget);
6674 }
6675
6676 /**
6677  * gtk_widget_set_state:
6678  * @widget: a #GtkWidget
6679  * @state: new state for @widget
6680  *
6681  * This function is for use in widget implementations. Sets the state
6682  * of a widget (insensitive, prelighted, etc.) Usually you should set
6683  * the state using wrapper functions such as gtk_widget_set_sensitive().
6684  **/
6685 void
6686 gtk_widget_set_state (GtkWidget           *widget,
6687                       GtkStateType         state)
6688 {
6689   GtkWidgetPrivate *priv;
6690
6691   g_return_if_fail (GTK_IS_WIDGET (widget));
6692
6693   priv = widget->priv;
6694
6695   if (state == gtk_widget_get_state (widget))
6696     return;
6697
6698   if (state == GTK_STATE_INSENSITIVE)
6699     gtk_widget_set_sensitive (widget, FALSE);
6700   else
6701     {
6702       GtkStateData data;
6703
6704       data.state = state;
6705       data.state_restoration = FALSE;
6706       data.use_forall = FALSE;
6707       if (priv->parent)
6708         data.parent_sensitive = (gtk_widget_is_sensitive (priv->parent) != FALSE);
6709       else
6710         data.parent_sensitive = TRUE;
6711
6712       gtk_widget_propagate_state (widget, &data);
6713
6714       if (gtk_widget_is_drawable (widget))
6715         gtk_widget_queue_draw (widget);
6716     }
6717 }
6718
6719 /**
6720  * gtk_widget_get_state:
6721  * @widget: a #GtkWidget
6722  *
6723  * Returns the widget's state. See gtk_widget_set_state().
6724  *
6725  * Returns: the state of @widget.
6726  *
6727  * Since: 2.18
6728  */
6729 GtkStateType
6730 gtk_widget_get_state (GtkWidget *widget)
6731 {
6732   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_STATE_NORMAL);
6733
6734   return widget->priv->state;
6735 }
6736
6737 /**
6738  * gtk_widget_set_visible:
6739  * @widget: a #GtkWidget
6740  * @visible: whether the widget should be shown or not
6741  *
6742  * Sets the visibility state of @widget. Note that setting this to
6743  * %TRUE doesn't mean the widget is actually viewable, see
6744  * gtk_widget_get_visible().
6745  *
6746  * This function simply calls gtk_widget_show() or gtk_widget_hide()
6747  * but is nicer to use when the visibility of the widget depends on
6748  * some condition.
6749  *
6750  * Since: 2.18
6751  **/
6752 void
6753 gtk_widget_set_visible (GtkWidget *widget,
6754                         gboolean   visible)
6755 {
6756   g_return_if_fail (GTK_IS_WIDGET (widget));
6757
6758   if (visible != gtk_widget_get_visible (widget))
6759     {
6760       if (visible)
6761         gtk_widget_show (widget);
6762       else
6763         gtk_widget_hide (widget);
6764     }
6765 }
6766
6767 void
6768 _gtk_widget_set_visible_flag (GtkWidget *widget,
6769                               gboolean   visible)
6770 {
6771   widget->priv->visible = visible;
6772 }
6773
6774 /**
6775  * gtk_widget_get_visible:
6776  * @widget: a #GtkWidget
6777  *
6778  * Determines whether the widget is visible. Note that this doesn't
6779  * take into account whether the widget's parent is also visible
6780  * or the widget is obscured in any way.
6781  *
6782  * See gtk_widget_set_visible().
6783  *
6784  * Return value: %TRUE if the widget is visible
6785  *
6786  * Since: 2.18
6787  **/
6788 gboolean
6789 gtk_widget_get_visible (GtkWidget *widget)
6790 {
6791   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6792
6793   return widget->priv->visible;
6794 }
6795
6796 /**
6797  * gtk_widget_set_has_window:
6798  * @widget: a #GtkWidget
6799  * @has_window: whether or not @widget has a window.
6800  *
6801  * Specifies whether @widget has a #GdkWindow of its own. Note that
6802  * all realized widgets have a non-%NULL "window" pointer
6803  * (gtk_widget_get_window() never returns a %NULL window when a widget
6804  * is realized), but for many of them it's actually the #GdkWindow of
6805  * one of its parent widgets. Widgets that do not create a %window for
6806  * themselves in #GtkWidget::realize must announce this by
6807  * calling this function with @has_window = %FALSE.
6808  *
6809  * This function should only be called by widget implementations,
6810  * and they should call it in their init() function.
6811  *
6812  * Since: 2.18
6813  **/
6814 void
6815 gtk_widget_set_has_window (GtkWidget *widget,
6816                            gboolean   has_window)
6817 {
6818   g_return_if_fail (GTK_IS_WIDGET (widget));
6819
6820   widget->priv->no_window = !has_window;
6821 }
6822
6823 /**
6824  * gtk_widget_get_has_window:
6825  * @widget: a #GtkWidget
6826  *
6827  * Determines whether @widget has a #GdkWindow of its own. See
6828  * gtk_widget_set_has_window().
6829  *
6830  * Return value: %TRUE if @widget has a window, %FALSE otherwise
6831  *
6832  * Since: 2.18
6833  **/
6834 gboolean
6835 gtk_widget_get_has_window (GtkWidget *widget)
6836 {
6837   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6838
6839   return ! widget->priv->no_window;
6840 }
6841
6842 /**
6843  * gtk_widget_is_toplevel:
6844  * @widget: a #GtkWidget
6845  *
6846  * Determines whether @widget is a toplevel widget. Currently only
6847  * #GtkWindow and #GtkInvisible are toplevel widgets. Toplevel
6848  * widgets have no parent widget.
6849  *
6850  * Return value: %TRUE if @widget is a toplevel, %FALSE otherwise
6851  *
6852  * Since: 2.18
6853  **/
6854 gboolean
6855 gtk_widget_is_toplevel (GtkWidget *widget)
6856 {
6857   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6858
6859   return widget->priv->toplevel;
6860 }
6861
6862 void
6863 _gtk_widget_set_is_toplevel (GtkWidget *widget,
6864                              gboolean   is_toplevel)
6865 {
6866   widget->priv->toplevel = is_toplevel;
6867 }
6868
6869 /**
6870  * gtk_widget_is_drawable:
6871  * @widget: a #GtkWidget
6872  *
6873  * Determines whether @widget can be drawn to. A widget can be drawn
6874  * to if it is mapped and visible.
6875  *
6876  * Return value: %TRUE if @widget is drawable, %FALSE otherwise
6877  *
6878  * Since: 2.18
6879  **/
6880 gboolean
6881 gtk_widget_is_drawable (GtkWidget *widget)
6882 {
6883   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6884
6885   return (gtk_widget_get_visible (widget) &&
6886           gtk_widget_get_mapped (widget));
6887 }
6888
6889 /**
6890  * gtk_widget_get_realized:
6891  * @widget: a #GtkWidget
6892  *
6893  * Determines whether @widget is realized.
6894  *
6895  * Return value: %TRUE if @widget is realized, %FALSE otherwise
6896  *
6897  * Since: 2.20
6898  **/
6899 gboolean
6900 gtk_widget_get_realized (GtkWidget *widget)
6901 {
6902   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6903
6904   return widget->priv->realized;
6905 }
6906
6907 /**
6908  * gtk_widget_set_realized:
6909  * @widget: a #GtkWidget
6910  * @realized: %TRUE to mark the widget as realized
6911  *
6912  * Marks the widget as being realized.
6913  *
6914  * This function should only ever be called in a derived widget's
6915  * "realize" or "unrealize" implementation.
6916  *
6917  * Since: 2.20
6918  */
6919 void
6920 gtk_widget_set_realized (GtkWidget *widget,
6921                          gboolean   realized)
6922 {
6923   g_return_if_fail (GTK_IS_WIDGET (widget));
6924
6925   widget->priv->realized = realized;
6926 }
6927
6928 /**
6929  * gtk_widget_get_mapped:
6930  * @widget: a #GtkWidget
6931  *
6932  * Whether the widget is mapped.
6933  *
6934  * Return value: %TRUE if the widget is mapped, %FALSE otherwise.
6935  *
6936  * Since: 2.20
6937  */
6938 gboolean
6939 gtk_widget_get_mapped (GtkWidget *widget)
6940 {
6941   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6942
6943   return widget->priv->mapped;
6944 }
6945
6946 /**
6947  * gtk_widget_set_mapped:
6948  * @widget: a #GtkWidget
6949  * @mapped: %TRUE to mark the widget as mapped
6950  *
6951  * Marks the widget as being realized.
6952  *
6953  * This function should only ever be called in a derived widget's
6954  * "map" or "unmap" implementation.
6955  *
6956  * Since: 2.20
6957  */
6958 void
6959 gtk_widget_set_mapped (GtkWidget *widget,
6960                        gboolean   mapped)
6961 {
6962   g_return_if_fail (GTK_IS_WIDGET (widget));
6963
6964   widget->priv->mapped = mapped;
6965 }
6966
6967 /**
6968  * gtk_widget_set_app_paintable:
6969  * @widget: a #GtkWidget
6970  * @app_paintable: %TRUE if the application will paint on the widget
6971  *
6972  * Sets whether the application intends to draw on the widget in
6973  * an #GtkWidget::draw handler.
6974  *
6975  * This is a hint to the widget and does not affect the behavior of
6976  * the GTK+ core; many widgets ignore this flag entirely. For widgets
6977  * that do pay attention to the flag, such as #GtkEventBox and #GtkWindow,
6978  * the effect is to suppress default themed drawing of the widget's
6979  * background. (Children of the widget will still be drawn.) The application
6980  * is then entirely responsible for drawing the widget background.
6981  *
6982  * Note that the background is still drawn when the widget is mapped.
6983  **/
6984 void
6985 gtk_widget_set_app_paintable (GtkWidget *widget,
6986                               gboolean   app_paintable)
6987 {
6988   g_return_if_fail (GTK_IS_WIDGET (widget));
6989
6990   app_paintable = (app_paintable != FALSE);
6991
6992   if (widget->priv->app_paintable != app_paintable)
6993     {
6994       widget->priv->app_paintable = app_paintable;
6995
6996       if (gtk_widget_is_drawable (widget))
6997         gtk_widget_queue_draw (widget);
6998
6999       g_object_notify (G_OBJECT (widget), "app-paintable");
7000     }
7001 }
7002
7003 /**
7004  * gtk_widget_get_app_paintable:
7005  * @widget: a #GtkWidget
7006  *
7007  * Determines whether the application intends to draw on the widget in
7008  * an #GtkWidget::draw handler.
7009  *
7010  * See gtk_widget_set_app_paintable()
7011  *
7012  * Return value: %TRUE if the widget is app paintable
7013  *
7014  * Since: 2.18
7015  **/
7016 gboolean
7017 gtk_widget_get_app_paintable (GtkWidget *widget)
7018 {
7019   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7020
7021   return widget->priv->app_paintable;
7022 }
7023
7024 /**
7025  * gtk_widget_set_double_buffered:
7026  * @widget: a #GtkWidget
7027  * @double_buffered: %TRUE to double-buffer a widget
7028  *
7029  * Widgets are double buffered by default; you can use this function
7030  * to turn off the buffering. "Double buffered" simply means that
7031  * gdk_window_begin_paint_region() and gdk_window_end_paint() are called
7032  * automatically around expose events sent to the
7033  * widget. gdk_window_begin_paint() diverts all drawing to a widget's
7034  * window to an offscreen buffer, and gdk_window_end_paint() draws the
7035  * buffer to the screen. The result is that users see the window
7036  * update in one smooth step, and don't see individual graphics
7037  * primitives being rendered.
7038  *
7039  * In very simple terms, double buffered widgets don't flicker,
7040  * so you would only use this function to turn off double buffering
7041  * if you had special needs and really knew what you were doing.
7042  *
7043  * Note: if you turn off double-buffering, you have to handle
7044  * expose events, since even the clearing to the background color or
7045  * pixmap will not happen automatically (as it is done in
7046  * gdk_window_begin_paint()).
7047  **/
7048 void
7049 gtk_widget_set_double_buffered (GtkWidget *widget,
7050                                 gboolean   double_buffered)
7051 {
7052   g_return_if_fail (GTK_IS_WIDGET (widget));
7053
7054   double_buffered = (double_buffered != FALSE);
7055
7056   if (widget->priv->double_buffered != double_buffered)
7057     {
7058       widget->priv->double_buffered = double_buffered;
7059
7060       g_object_notify (G_OBJECT (widget), "double-buffered");
7061     }
7062 }
7063
7064 /**
7065  * gtk_widget_get_double_buffered:
7066  * @widget: a #GtkWidget
7067  *
7068  * Determines whether the widget is double buffered.
7069  *
7070  * See gtk_widget_set_double_buffered()
7071  *
7072  * Return value: %TRUE if the widget is double buffered
7073  *
7074  * Since: 2.18
7075  **/
7076 gboolean
7077 gtk_widget_get_double_buffered (GtkWidget *widget)
7078 {
7079   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7080
7081   return widget->priv->double_buffered;
7082 }
7083
7084 /**
7085  * gtk_widget_set_redraw_on_allocate:
7086  * @widget: a #GtkWidget
7087  * @redraw_on_allocate: if %TRUE, the entire widget will be redrawn
7088  *   when it is allocated to a new size. Otherwise, only the
7089  *   new portion of the widget will be redrawn.
7090  *
7091  * Sets whether the entire widget is queued for drawing when its size
7092  * allocation changes. By default, this setting is %TRUE and
7093  * the entire widget is redrawn on every size change. If your widget
7094  * leaves the upper left unchanged when made bigger, turning this
7095  * setting off will improve performance.
7096
7097  * Note that for %NO_WINDOW widgets setting this flag to %FALSE turns
7098  * off all allocation on resizing: the widget will not even redraw if
7099  * its position changes; this is to allow containers that don't draw
7100  * anything to avoid excess invalidations. If you set this flag on a
7101  * %NO_WINDOW widget that <emphasis>does</emphasis> draw on @widget->window,
7102  * you are responsible for invalidating both the old and new allocation
7103  * of the widget when the widget is moved and responsible for invalidating
7104  * regions newly when the widget increases size.
7105  **/
7106 void
7107 gtk_widget_set_redraw_on_allocate (GtkWidget *widget,
7108                                    gboolean   redraw_on_allocate)
7109 {
7110   g_return_if_fail (GTK_IS_WIDGET (widget));
7111
7112   widget->priv->redraw_on_alloc = redraw_on_allocate;
7113 }
7114
7115 /**
7116  * gtk_widget_set_sensitive:
7117  * @widget: a #GtkWidget
7118  * @sensitive: %TRUE to make the widget sensitive
7119  *
7120  * Sets the sensitivity of a widget. A widget is sensitive if the user
7121  * can interact with it. Insensitive widgets are "grayed out" and the
7122  * user can't interact with them. Insensitive widgets are known as
7123  * "inactive", "disabled", or "ghosted" in some other toolkits.
7124  **/
7125 void
7126 gtk_widget_set_sensitive (GtkWidget *widget,
7127                           gboolean   sensitive)
7128 {
7129   GtkWidgetPrivate *priv;
7130   GtkStateData data;
7131
7132   g_return_if_fail (GTK_IS_WIDGET (widget));
7133
7134   priv = widget->priv;
7135
7136   sensitive = (sensitive != FALSE);
7137
7138   if (widget->priv->sensitive == sensitive)
7139     return;
7140
7141   if (sensitive)
7142     {
7143       widget->priv->sensitive = TRUE;
7144       data.state = priv->saved_state;
7145     }
7146   else
7147     {
7148       widget->priv->sensitive = FALSE;
7149       data.state = gtk_widget_get_state (widget);
7150     }
7151   data.state_restoration = TRUE;
7152   data.use_forall = TRUE;
7153
7154   if (priv->parent)
7155     data.parent_sensitive = gtk_widget_is_sensitive (priv->parent);
7156   else
7157     data.parent_sensitive = TRUE;
7158
7159   gtk_widget_propagate_state (widget, &data);
7160   if (gtk_widget_is_drawable (widget))
7161     gtk_widget_queue_draw (widget);
7162
7163   g_object_notify (G_OBJECT (widget), "sensitive");
7164 }
7165
7166 /**
7167  * gtk_widget_get_sensitive:
7168  * @widget: a #GtkWidget
7169  *
7170  * Returns the widget's sensitivity (in the sense of returning
7171  * the value that has been set using gtk_widget_set_sensitive()).
7172  *
7173  * The effective sensitivity of a widget is however determined by both its
7174  * own and its parent widget's sensitivity. See gtk_widget_is_sensitive().
7175  *
7176  * Returns: %TRUE if the widget is sensitive
7177  *
7178  * Since: 2.18
7179  */
7180 gboolean
7181 gtk_widget_get_sensitive (GtkWidget *widget)
7182 {
7183   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7184
7185   return widget->priv->sensitive;
7186 }
7187
7188 /**
7189  * gtk_widget_is_sensitive:
7190  * @widget: a #GtkWidget
7191  *
7192  * Returns the widget's effective sensitivity, which means
7193  * it is sensitive itself and also its parent widget is sensntive
7194  *
7195  * Returns: %TRUE if the widget is effectively sensitive
7196  *
7197  * Since: 2.18
7198  */
7199 gboolean
7200 gtk_widget_is_sensitive (GtkWidget *widget)
7201 {
7202   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7203
7204   return widget->priv->sensitive && widget->priv->parent_sensitive;
7205 }
7206
7207 /**
7208  * gtk_widget_set_parent:
7209  * @widget: a #GtkWidget
7210  * @parent: parent container
7211  *
7212  * This function is useful only when implementing subclasses of
7213  * #GtkContainer.
7214  * Sets the container as the parent of @widget, and takes care of
7215  * some details such as updating the state and style of the child
7216  * to reflect its new location. The opposite function is
7217  * gtk_widget_unparent().
7218  **/
7219 void
7220 gtk_widget_set_parent (GtkWidget *widget,
7221                        GtkWidget *parent)
7222 {
7223   GtkWidgetPrivate *priv;
7224   GtkStateData data;
7225   GtkStyleContext *context;
7226
7227   g_return_if_fail (GTK_IS_WIDGET (widget));
7228   g_return_if_fail (GTK_IS_WIDGET (parent));
7229   g_return_if_fail (widget != parent);
7230
7231   priv = widget->priv;
7232
7233   if (priv->parent != NULL)
7234     {
7235       g_warning ("Can't set a parent on widget which has a parent\n");
7236       return;
7237     }
7238   if (gtk_widget_is_toplevel (widget))
7239     {
7240       g_warning ("Can't set a parent on a toplevel widget\n");
7241       return;
7242     }
7243
7244   /* keep this function in sync with gtk_menu_attach_to_widget()
7245    */
7246
7247   g_object_ref_sink (widget);
7248   priv->parent = parent;
7249
7250   if (gtk_widget_get_state (parent) != GTK_STATE_NORMAL)
7251     data.state = gtk_widget_get_state (parent);
7252   else
7253     data.state = gtk_widget_get_state (widget);
7254   data.state_restoration = FALSE;
7255   data.parent_sensitive = (gtk_widget_is_sensitive (parent) != FALSE);
7256   data.use_forall = gtk_widget_is_sensitive (parent) != gtk_widget_is_sensitive (widget);
7257
7258   gtk_widget_propagate_state (widget, &data);
7259
7260   gtk_widget_reset_rc_styles (widget);
7261
7262   g_signal_emit (widget, widget_signals[PARENT_SET], 0, NULL);
7263   if (priv->parent->priv->anchored)
7264     _gtk_widget_propagate_hierarchy_changed (widget, NULL);
7265   g_object_notify (G_OBJECT (widget), "parent");
7266
7267   /* Enforce realized/mapped invariants
7268    */
7269   if (gtk_widget_get_realized (priv->parent))
7270     gtk_widget_realize (widget);
7271
7272   if (gtk_widget_get_visible (priv->parent) &&
7273       gtk_widget_get_visible (widget))
7274     {
7275       if (gtk_widget_get_child_visible (widget) &&
7276           gtk_widget_get_mapped (priv->parent))
7277         gtk_widget_map (widget);
7278
7279       gtk_widget_queue_resize (widget);
7280     }
7281
7282   /* child may cause parent's expand to change, if the child is
7283    * expanded. If child is not expanded, then it can't modify the
7284    * parent's expand. If the child becomes expanded later then it will
7285    * queue compute_expand then. This optimization plus defaulting
7286    * newly-constructed widgets to need_compute_expand=FALSE should
7287    * mean that initially building a widget tree doesn't have to keep
7288    * walking up setting need_compute_expand on parents over and over.
7289    *
7290    * We can't change a parent to need to expand unless we're visible.
7291    */
7292   if (gtk_widget_get_visible (widget) &&
7293       (priv->need_compute_expand ||
7294        priv->computed_hexpand ||
7295        priv->computed_vexpand))
7296     {
7297       gtk_widget_queue_compute_expand (parent);
7298     }
7299
7300   context = g_object_get_qdata (G_OBJECT (widget),
7301                                 quark_style_context);
7302   if (context)
7303     {
7304       GtkWidgetPath *path;
7305
7306       path = gtk_widget_get_path (widget);
7307       gtk_style_context_set_path (context, path);
7308       gtk_widget_path_free (path);
7309
7310       gtk_style_context_set_screen (context,
7311                                     gtk_widget_get_screen (widget));
7312     }
7313 }
7314
7315 /**
7316  * gtk_widget_get_parent:
7317  * @widget: a #GtkWidget
7318  *
7319  * Returns the parent container of @widget.
7320  *
7321  * Return value: (transfer none): the parent container of @widget, or %NULL
7322  **/
7323 GtkWidget *
7324 gtk_widget_get_parent (GtkWidget *widget)
7325 {
7326   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7327
7328   return widget->priv->parent;
7329 }
7330
7331 /*****************************************
7332  * Widget styles
7333  * see docs/styles.txt
7334  *****************************************/
7335
7336 /**
7337  * gtk_widget_style_attach:
7338  * @widget: a #GtkWidget
7339  *
7340  * This function attaches the widget's #GtkStyle to the widget's
7341  * #GdkWindow. It is a replacement for
7342  *
7343  * <programlisting>
7344  * widget->style = gtk_style_attach (widget->style, widget->window);
7345  * </programlisting>
7346  *
7347  * and should only ever be called in a derived widget's "realize"
7348  * implementation which does not chain up to its parent class'
7349  * "realize" implementation, because one of the parent classes
7350  * (finally #GtkWidget) would attach the style itself.
7351  *
7352  * Since: 2.20
7353  **/
7354 void
7355 gtk_widget_style_attach (GtkWidget *widget)
7356 {
7357   GtkWidgetPrivate *priv;
7358
7359   g_return_if_fail (GTK_IS_WIDGET (widget));
7360   g_return_if_fail (gtk_widget_get_realized (widget));
7361
7362   priv = widget->priv;
7363
7364   priv->style = gtk_style_attach (priv->style, priv->window);
7365 }
7366
7367 /**
7368  * gtk_widget_has_rc_style:
7369  * @widget: a #GtkWidget
7370  *
7371  * Determines if the widget style has been looked up through the rc mechanism.
7372  *
7373  * Returns: %TRUE if the widget has been looked up through the rc
7374  *   mechanism, %FALSE otherwise.
7375  *
7376  * Since: 2.20
7377  **/
7378 gboolean
7379 gtk_widget_has_rc_style (GtkWidget *widget)
7380 {
7381   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7382
7383   return widget->priv->rc_style;
7384 }
7385
7386 /**
7387  * gtk_widget_set_style:
7388  * @widget: a #GtkWidget
7389  * @style: (allow-none): a #GtkStyle, or %NULL to remove the effect of a previous
7390  *         gtk_widget_set_style() and go back to the default style
7391  *
7392  * Sets the #GtkStyle for a widget (@widget->style). You probably don't
7393  * want to use this function; it interacts badly with themes, because
7394  * themes work by replacing the #GtkStyle. Instead, use
7395  * gtk_widget_modify_style().
7396  **/
7397 void
7398 gtk_widget_set_style (GtkWidget *widget,
7399                       GtkStyle  *style)
7400 {
7401   g_return_if_fail (GTK_IS_WIDGET (widget));
7402
7403   if (style)
7404     {
7405       gboolean initial_emission;
7406
7407       initial_emission = !widget->priv->rc_style && !widget->priv->user_style;
7408
7409       widget->priv->rc_style = FALSE;
7410       widget->priv->user_style = TRUE;
7411
7412       gtk_widget_set_style_internal (widget, style, initial_emission);
7413     }
7414   else
7415     {
7416       if (widget->priv->user_style)
7417         gtk_widget_reset_rc_style (widget);
7418     }
7419 }
7420
7421 /**
7422  * gtk_widget_ensure_style:
7423  * @widget: a #GtkWidget
7424  *
7425  * Ensures that @widget has a style (@widget->style). Not a very useful
7426  * function; most of the time, if you want the style, the widget is
7427  * realized, and realized widgets are guaranteed to have a style
7428  * already.
7429  **/
7430 void
7431 gtk_widget_ensure_style (GtkWidget *widget)
7432 {
7433   g_return_if_fail (GTK_IS_WIDGET (widget));
7434
7435   if (!widget->priv->style ||
7436       !gtk_style_has_context (widget->priv->style))
7437     {
7438       GtkStyle *style;
7439
7440       style = g_object_new (GTK_TYPE_STYLE,
7441                             "context", gtk_widget_get_style_context (widget),
7442                             NULL);
7443
7444       gtk_widget_set_style_internal (widget, style, TRUE);
7445       g_object_unref (style);
7446     }
7447
7448 #if 0
7449   if (!widget->priv->rc_style && !widget->priv->user_style)
7450     gtk_widget_reset_rc_style (widget);
7451 #endif
7452 }
7453
7454 /* Look up the RC style for this widget, unsetting any user style that
7455  * may be in effect currently
7456  **/
7457 static void
7458 gtk_widget_reset_rc_style (GtkWidget *widget)
7459 {
7460   GtkWidgetPrivate *priv = widget->priv;
7461   GtkStyle *new_style = NULL;
7462   gboolean initial_emission;
7463
7464   initial_emission = !priv->rc_style && !priv->user_style;
7465
7466   priv->user_style = FALSE;
7467   priv->rc_style = TRUE;
7468
7469   if (gtk_widget_has_screen (widget))
7470     new_style = gtk_rc_get_style (widget);
7471   if (!new_style)
7472     new_style = gtk_widget_get_default_style ();
7473
7474   if (initial_emission || new_style != priv->style)
7475     gtk_widget_set_style_internal (widget, new_style, initial_emission);
7476 }
7477
7478 /**
7479  * gtk_widget_get_style:
7480  * @widget: a #GtkWidget
7481  *
7482  * Simply an accessor function that returns @widget->style.
7483  *
7484  * Return value: (transfer none): the widget's #GtkStyle
7485  **/
7486 GtkStyle*
7487 gtk_widget_get_style (GtkWidget *widget)
7488 {
7489   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7490
7491   return widget->priv->style;
7492 }
7493
7494 /**
7495  * gtk_widget_modify_style:
7496  * @widget: a #GtkWidget
7497  * @style: the #GtkRcStyle holding the style modifications
7498  *
7499  * Modifies style values on the widget. Modifications made using this
7500  * technique take precedence over style values set via an RC file,
7501  * however, they will be overriden if a style is explicitely set on
7502  * the widget using gtk_widget_set_style(). The #GtkRcStyle structure
7503  * is designed so each field can either be set or unset, so it is
7504  * possible, using this function, to modify some style values and
7505  * leave the others unchanged.
7506  *
7507  * Note that modifications made with this function are not cumulative
7508  * with previous calls to gtk_widget_modify_style() or with such
7509  * functions as gtk_widget_modify_fg(). If you wish to retain
7510  * previous values, you must first call gtk_widget_get_modifier_style(),
7511  * make your modifications to the returned style, then call
7512  * gtk_widget_modify_style() with that style. On the other hand,
7513  * if you first call gtk_widget_modify_style(), subsequent calls
7514  * to such functions gtk_widget_modify_fg() will have a cumulative
7515  * effect with the initial modifications.
7516  **/
7517 void
7518 gtk_widget_modify_style (GtkWidget      *widget,
7519                          GtkRcStyle     *style)
7520 {
7521   g_return_if_fail (GTK_IS_WIDGET (widget));
7522   g_return_if_fail (GTK_IS_RC_STYLE (style));
7523
7524   g_object_set_qdata_full (G_OBJECT (widget),
7525                            quark_rc_style,
7526                            gtk_rc_style_copy (style),
7527                            (GDestroyNotify) g_object_unref);
7528
7529   /* note that "style" may be invalid here if it was the old
7530    * modifier style and the only reference was our own.
7531    */
7532
7533   if (widget->priv->rc_style)
7534     gtk_widget_reset_rc_style (widget);
7535 }
7536
7537 /**
7538  * gtk_widget_get_modifier_style:
7539  * @widget: a #GtkWidget
7540  *
7541  * Returns the current modifier style for the widget. (As set by
7542  * gtk_widget_modify_style().) If no style has previously set, a new
7543  * #GtkRcStyle will be created with all values unset, and set as the
7544  * modifier style for the widget. If you make changes to this rc
7545  * style, you must call gtk_widget_modify_style(), passing in the
7546  * returned rc style, to make sure that your changes take effect.
7547  *
7548  * Caution: passing the style back to gtk_widget_modify_style() will
7549  * normally end up destroying it, because gtk_widget_modify_style() copies
7550  * the passed-in style and sets the copy as the new modifier style,
7551  * thus dropping any reference to the old modifier style. Add a reference
7552  * to the modifier style if you want to keep it alive.
7553  *
7554  * Return value: (transfer none): the modifier style for the widget. This rc style is
7555  *   owned by the widget. If you want to keep a pointer to value this
7556  *   around, you must add a refcount using g_object_ref().
7557  **/
7558 GtkRcStyle *
7559 gtk_widget_get_modifier_style (GtkWidget      *widget)
7560 {
7561   GtkRcStyle *rc_style;
7562
7563   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7564
7565   rc_style = g_object_get_qdata (G_OBJECT (widget), quark_rc_style);
7566
7567   if (!rc_style)
7568     {
7569       rc_style = gtk_rc_style_new ();
7570       g_object_set_qdata_full (G_OBJECT (widget),
7571                                quark_rc_style,
7572                                rc_style,
7573                                (GDestroyNotify) g_object_unref);
7574     }
7575
7576   return rc_style;
7577 }
7578
7579 static void
7580 gtk_widget_modify_color_component (GtkWidget      *widget,
7581                                    GtkRcFlags      component,
7582                                    GtkStateType    state,
7583                                    const GdkColor *color)
7584 {
7585   GtkRcStyle *rc_style = gtk_widget_get_modifier_style (widget);
7586
7587   if (color)
7588     {
7589       switch (component)
7590         {
7591         case GTK_RC_FG:
7592           rc_style->fg[state] = *color;
7593           break;
7594         case GTK_RC_BG:
7595           rc_style->bg[state] = *color;
7596           break;
7597         case GTK_RC_TEXT:
7598           rc_style->text[state] = *color;
7599           break;
7600         case GTK_RC_BASE:
7601           rc_style->base[state] = *color;
7602           break;
7603         default:
7604           g_assert_not_reached();
7605         }
7606
7607       rc_style->color_flags[state] |= component;
7608     }
7609   else
7610     rc_style->color_flags[state] &= ~component;
7611
7612   gtk_widget_modify_style (widget, rc_style);
7613 }
7614
7615 /**
7616  * gtk_widget_modify_symbolic_color:
7617  * @widget: a #GtkWidget
7618  * @name: the name of the symbolic color to modify
7619  * @color: (allow-none): the color to assign (does not need to be allocated),
7620  *         or %NULL to undo the effect of previous calls to
7621  *         of gtk_widget_modify_symbolic_color().
7622  *
7623  * Sets a symbolic color for a widget.
7624  * All other style values are left untouched. See also
7625  * gtk_widget_modify_style().
7626  *
7627  * Since: 3.0
7628  **/
7629 void
7630 gtk_widget_modify_symbolic_color (GtkWidget      *widget,
7631                                   const gchar    *name,
7632                                   const GdkColor *color)
7633 {
7634   GtkRcStyle *rc_style = gtk_widget_get_modifier_style (widget);
7635
7636   _gtk_rc_style_set_symbolic_color (rc_style, name, color);
7637
7638   gtk_widget_modify_style (widget, rc_style);
7639 }
7640
7641 /**
7642  * gtk_widget_modify_fg:
7643  * @widget: a #GtkWidget
7644  * @state: the state for which to set the foreground color
7645  * @color: (allow-none): the color to assign (does not need to be allocated),
7646  *         or %NULL to undo the effect of previous calls to
7647  *         of gtk_widget_modify_fg().
7648  *
7649  * Sets the foreground color for a widget in a particular state.
7650  * All other style values are left untouched. See also
7651  * gtk_widget_modify_style().
7652  **/
7653 void
7654 gtk_widget_modify_fg (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_FG, state, color);
7662 }
7663
7664 /**
7665  * gtk_widget_modify_bg:
7666  * @widget: a #GtkWidget
7667  * @state: the state for which to set the background 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_bg().
7671  *
7672  * Sets the background color for a widget in a particular state.
7673  * All other style values are left untouched. See also
7674  * gtk_widget_modify_style().
7675  *
7676  * Note that "no window" widgets (which have the %GTK_NO_WINDOW flag set)
7677  * draw on their parent container's window and thus may not draw any
7678  * background themselves. This is the case for e.g. #GtkLabel. To modify
7679  * the background of such widgets, you have to set the background color
7680  * on their parent; if you want to set the background of a rectangular
7681  * area around a label, try placing the label in a #GtkEventBox widget
7682  * and setting the background color on that.
7683  **/
7684 void
7685 gtk_widget_modify_bg (GtkWidget      *widget,
7686                       GtkStateType    state,
7687                       const GdkColor *color)
7688 {
7689   g_return_if_fail (GTK_IS_WIDGET (widget));
7690   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
7691
7692   gtk_widget_modify_color_component (widget, GTK_RC_BG, state, color);
7693 }
7694
7695 /**
7696  * gtk_widget_modify_text:
7697  * @widget: a #GtkWidget
7698  * @state: the state for which to set the text color
7699  * @color: (allow-none): the color to assign (does not need to be allocated),
7700  *         or %NULL to undo the effect of previous calls to
7701  *         of gtk_widget_modify_text().
7702  *
7703  * Sets the text color for a widget in a particular state.  All other
7704  * style values are left untouched. The text color is the foreground
7705  * color used along with the base color (see gtk_widget_modify_base())
7706  * for widgets such as #GtkEntry and #GtkTextView. See also
7707  * gtk_widget_modify_style().
7708  **/
7709 void
7710 gtk_widget_modify_text (GtkWidget      *widget,
7711                         GtkStateType    state,
7712                         const GdkColor *color)
7713 {
7714   g_return_if_fail (GTK_IS_WIDGET (widget));
7715   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
7716
7717   gtk_widget_modify_color_component (widget, GTK_RC_TEXT, state, color);
7718 }
7719
7720 /**
7721  * gtk_widget_modify_base:
7722  * @widget: a #GtkWidget
7723  * @state: the state for which to set the base color
7724  * @color: (allow-none): the color to assign (does not need to be allocated),
7725  *         or %NULL to undo the effect of previous calls to
7726  *         of gtk_widget_modify_base().
7727  *
7728  * Sets the base color for a widget in a particular state.
7729  * All other style values are left untouched. The base color
7730  * is the background color used along with the text color
7731  * (see gtk_widget_modify_text()) for widgets such as #GtkEntry
7732  * and #GtkTextView. See also gtk_widget_modify_style().
7733  *
7734  * Note that "no window" widgets (which have the %GTK_NO_WINDOW flag set)
7735  * draw on their parent container's window and thus may not draw any
7736  * background themselves. This is the case for e.g. #GtkLabel. To modify
7737  * the background of such widgets, you have to set the base color on their
7738  * parent; if you want to set the background of a rectangular area around
7739  * a label, try placing the label in a #GtkEventBox widget and setting
7740  * the base color on that.
7741  **/
7742 void
7743 gtk_widget_modify_base (GtkWidget      *widget,
7744                         GtkStateType    state,
7745                         const GdkColor *color)
7746 {
7747   g_return_if_fail (GTK_IS_WIDGET (widget));
7748   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
7749
7750   gtk_widget_modify_color_component (widget, GTK_RC_BASE, state, color);
7751 }
7752
7753 static void
7754 modify_color_property (GtkWidget      *widget,
7755                        GtkRcStyle     *rc_style,
7756                        const char     *name,
7757                        const GdkColor *color)
7758 {
7759   GQuark type_name = g_type_qname (G_OBJECT_TYPE (widget));
7760   GQuark property_name = g_quark_from_string (name);
7761
7762   if (color)
7763     {
7764       GtkRcProperty rc_property = {0};
7765       char *color_name;
7766
7767       rc_property.type_name = type_name;
7768       rc_property.property_name = property_name;
7769       rc_property.origin = NULL;
7770
7771       color_name = gdk_color_to_string (color);
7772       g_value_init (&rc_property.value, G_TYPE_STRING);
7773       g_value_take_string (&rc_property.value, color_name);
7774
7775       _gtk_rc_style_set_rc_property (rc_style, &rc_property);
7776
7777       g_value_unset (&rc_property.value);
7778     }
7779   else
7780     _gtk_rc_style_unset_rc_property (rc_style, type_name, property_name);
7781 }
7782
7783 /**
7784  * gtk_widget_modify_cursor:
7785  * @widget: a #GtkWidget
7786  * @primary: the color to use for primary cursor (does not need to be
7787  *           allocated), or %NULL to undo the effect of previous calls to
7788  *           of gtk_widget_modify_cursor().
7789  * @secondary: the color to use for secondary cursor (does not need to be
7790  *             allocated), or %NULL to undo the effect of previous calls to
7791  *             of gtk_widget_modify_cursor().
7792  *
7793  * Sets the cursor color to use in a widget, overriding the
7794  * #GtkWidget:cursor-color and #GtkWidget:secondary-cursor-color
7795  * style properties. All other style values are left untouched.
7796  * See also gtk_widget_modify_style().
7797  *
7798  * Since: 2.12
7799  **/
7800 void
7801 gtk_widget_modify_cursor (GtkWidget      *widget,
7802                           const GdkColor *primary,
7803                           const GdkColor *secondary)
7804 {
7805   GtkRcStyle *rc_style;
7806
7807   g_return_if_fail (GTK_IS_WIDGET (widget));
7808
7809   rc_style = gtk_widget_get_modifier_style (widget);
7810
7811   modify_color_property (widget, rc_style, "cursor-color", primary);
7812   modify_color_property (widget, rc_style, "secondary-cursor-color", secondary);
7813
7814   gtk_widget_modify_style (widget, rc_style);
7815 }
7816
7817 /**
7818  * gtk_widget_modify_font:
7819  * @widget: a #GtkWidget
7820  * @font_desc: (allow-none): the font description to use, or %NULL to undo
7821  *   the effect of previous calls to gtk_widget_modify_font().
7822  *
7823  * Sets the font to use for a widget.  All other style values are left
7824  * untouched. See also gtk_widget_modify_style().
7825  **/
7826 void
7827 gtk_widget_modify_font (GtkWidget            *widget,
7828                         PangoFontDescription *font_desc)
7829 {
7830   GtkRcStyle *rc_style;
7831
7832   g_return_if_fail (GTK_IS_WIDGET (widget));
7833
7834   rc_style = gtk_widget_get_modifier_style (widget);
7835
7836   if (rc_style->font_desc)
7837     pango_font_description_free (rc_style->font_desc);
7838
7839   if (font_desc)
7840     rc_style->font_desc = pango_font_description_copy (font_desc);
7841   else
7842     rc_style->font_desc = NULL;
7843
7844   gtk_widget_modify_style (widget, rc_style);
7845 }
7846
7847 static void
7848 gtk_widget_real_direction_changed (GtkWidget        *widget,
7849                                    GtkTextDirection  previous_direction)
7850 {
7851   gtk_widget_queue_resize (widget);
7852 }
7853
7854 static void
7855 gtk_widget_real_style_set (GtkWidget *widget,
7856                            GtkStyle  *previous_style)
7857 {
7858   GtkWidgetPrivate *priv = widget->priv;
7859
7860   if (gtk_widget_get_realized (widget) &&
7861       gtk_widget_get_has_window (widget))
7862     gtk_style_set_background (priv->style, priv->window, priv->state);
7863 }
7864
7865 static void
7866 gtk_widget_set_style_internal (GtkWidget *widget,
7867                                GtkStyle  *style,
7868                                gboolean   initial_emission)
7869 {
7870   GtkWidgetPrivate *priv = widget->priv;
7871
7872   g_object_ref (widget);
7873   g_object_freeze_notify (G_OBJECT (widget));
7874
7875   if (priv->style != style)
7876     {
7877       GtkStyle *previous_style;
7878
7879       if (gtk_widget_get_realized (widget))
7880         {
7881           gtk_widget_reset_shapes (widget);
7882           gtk_style_detach (priv->style);
7883         }
7884
7885       previous_style = priv->style;
7886       priv->style = style;
7887       g_object_ref (priv->style);
7888
7889       if (gtk_widget_get_realized (widget))
7890         priv->style = gtk_style_attach (priv->style, priv->window);
7891
7892       gtk_widget_update_pango_context (widget);
7893       g_signal_emit (widget,
7894                      widget_signals[STYLE_SET],
7895                      0,
7896                      initial_emission ? NULL : previous_style);
7897       g_object_unref (previous_style);
7898
7899       if (priv->anchored && !initial_emission)
7900         gtk_widget_queue_resize (widget);
7901     }
7902   else if (initial_emission)
7903     {
7904       gtk_widget_update_pango_context (widget);
7905       g_signal_emit (widget,
7906                      widget_signals[STYLE_SET],
7907                      0,
7908                      NULL);
7909     }
7910   g_object_notify (G_OBJECT (widget), "style");
7911   g_object_thaw_notify (G_OBJECT (widget));
7912   g_object_unref (widget);
7913 }
7914
7915 typedef struct {
7916   GtkWidget *previous_toplevel;
7917   GdkScreen *previous_screen;
7918   GdkScreen *new_screen;
7919 } HierarchyChangedInfo;
7920
7921 static void
7922 do_screen_change (GtkWidget *widget,
7923                   GdkScreen *old_screen,
7924                   GdkScreen *new_screen)
7925 {
7926   if (old_screen != new_screen)
7927     {
7928       GtkStyleContext *context;
7929
7930       if (old_screen)
7931         {
7932           PangoContext *context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
7933           if (context)
7934             g_object_set_qdata (G_OBJECT (widget), quark_pango_context, NULL);
7935         }
7936
7937       _gtk_tooltip_hide (widget);
7938
7939       context = gtk_widget_get_style_context (widget);
7940       gtk_style_context_set_screen (context, new_screen);
7941
7942       g_signal_emit (widget, widget_signals[SCREEN_CHANGED], 0, old_screen);
7943     }
7944 }
7945
7946 static void
7947 gtk_widget_propagate_hierarchy_changed_recurse (GtkWidget *widget,
7948                                                 gpointer   client_data)
7949 {
7950   GtkWidgetPrivate *priv = widget->priv;
7951   HierarchyChangedInfo *info = client_data;
7952   gboolean new_anchored = gtk_widget_is_toplevel (widget) ||
7953                  (priv->parent && priv->parent->priv->anchored);
7954
7955   if (priv->anchored != new_anchored)
7956     {
7957       g_object_ref (widget);
7958
7959       priv->anchored = new_anchored;
7960
7961       g_signal_emit (widget, widget_signals[HIERARCHY_CHANGED], 0, info->previous_toplevel);
7962       do_screen_change (widget, info->previous_screen, info->new_screen);
7963
7964       if (GTK_IS_CONTAINER (widget))
7965         gtk_container_forall (GTK_CONTAINER (widget),
7966                               gtk_widget_propagate_hierarchy_changed_recurse,
7967                               client_data);
7968
7969       g_object_unref (widget);
7970     }
7971 }
7972
7973 /**
7974  * _gtk_widget_propagate_hierarchy_changed:
7975  * @widget: a #GtkWidget
7976  * @previous_toplevel: Previous toplevel
7977  *
7978  * Propagates changes in the anchored state to a widget and all
7979  * children, unsetting or setting the %ANCHORED flag, and
7980  * emitting #GtkWidget::hierarchy-changed.
7981  **/
7982 void
7983 _gtk_widget_propagate_hierarchy_changed (GtkWidget *widget,
7984                                          GtkWidget *previous_toplevel)
7985 {
7986   GtkWidgetPrivate *priv = widget->priv;
7987   HierarchyChangedInfo info;
7988
7989   info.previous_toplevel = previous_toplevel;
7990   info.previous_screen = previous_toplevel ? gtk_widget_get_screen (previous_toplevel) : NULL;
7991
7992   if (gtk_widget_is_toplevel (widget) ||
7993       (priv->parent && priv->parent->priv->anchored))
7994     info.new_screen = gtk_widget_get_screen (widget);
7995   else
7996     info.new_screen = NULL;
7997
7998   if (info.previous_screen)
7999     g_object_ref (info.previous_screen);
8000   if (previous_toplevel)
8001     g_object_ref (previous_toplevel);
8002
8003   gtk_widget_propagate_hierarchy_changed_recurse (widget, &info);
8004
8005   if (previous_toplevel)
8006     g_object_unref (previous_toplevel);
8007   if (info.previous_screen)
8008     g_object_unref (info.previous_screen);
8009 }
8010
8011 static void
8012 gtk_widget_propagate_screen_changed_recurse (GtkWidget *widget,
8013                                              gpointer   client_data)
8014 {
8015   HierarchyChangedInfo *info = client_data;
8016
8017   g_object_ref (widget);
8018
8019   do_screen_change (widget, info->previous_screen, info->new_screen);
8020
8021   if (GTK_IS_CONTAINER (widget))
8022     gtk_container_forall (GTK_CONTAINER (widget),
8023                           gtk_widget_propagate_screen_changed_recurse,
8024                           client_data);
8025
8026   g_object_unref (widget);
8027 }
8028
8029 /**
8030  * gtk_widget_is_composited:
8031  * @widget: a #GtkWidget
8032  *
8033  * Whether @widget can rely on having its alpha channel
8034  * drawn correctly. On X11 this function returns whether a
8035  * compositing manager is running for @widget's screen.
8036  *
8037  * Please note that the semantics of this call will change
8038  * in the future if used on a widget that has a composited
8039  * window in its hierarchy (as set by gdk_window_set_composited()).
8040  *
8041  * Return value: %TRUE if the widget can rely on its alpha
8042  * channel being drawn correctly.
8043  *
8044  * Since: 2.10
8045  */
8046 gboolean
8047 gtk_widget_is_composited (GtkWidget *widget)
8048 {
8049   GdkScreen *screen;
8050
8051   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
8052
8053   screen = gtk_widget_get_screen (widget);
8054
8055   return gdk_screen_is_composited (screen);
8056 }
8057
8058 static void
8059 propagate_composited_changed (GtkWidget *widget,
8060                               gpointer dummy)
8061 {
8062   if (GTK_IS_CONTAINER (widget))
8063     {
8064       gtk_container_forall (GTK_CONTAINER (widget),
8065                             propagate_composited_changed,
8066                             NULL);
8067     }
8068
8069   g_signal_emit (widget, widget_signals[COMPOSITED_CHANGED], 0);
8070 }
8071
8072 void
8073 _gtk_widget_propagate_composited_changed (GtkWidget *widget)
8074 {
8075   propagate_composited_changed (widget, NULL);
8076 }
8077
8078 /**
8079  * _gtk_widget_propagate_screen_changed:
8080  * @widget: a #GtkWidget
8081  * @previous_screen: Previous screen
8082  *
8083  * Propagates changes in the screen for a widget to all
8084  * children, emitting #GtkWidget::screen-changed.
8085  **/
8086 void
8087 _gtk_widget_propagate_screen_changed (GtkWidget    *widget,
8088                                       GdkScreen    *previous_screen)
8089 {
8090   HierarchyChangedInfo info;
8091
8092   info.previous_screen = previous_screen;
8093   info.new_screen = gtk_widget_get_screen (widget);
8094
8095   if (previous_screen)
8096     g_object_ref (previous_screen);
8097
8098   gtk_widget_propagate_screen_changed_recurse (widget, &info);
8099
8100   if (previous_screen)
8101     g_object_unref (previous_screen);
8102 }
8103
8104 static void
8105 reset_rc_styles_recurse (GtkWidget *widget, gpointer data)
8106 {
8107   if (widget->priv->rc_style)
8108     gtk_widget_reset_rc_style (widget);
8109
8110   if (GTK_IS_CONTAINER (widget))
8111     gtk_container_forall (GTK_CONTAINER (widget),
8112                           reset_rc_styles_recurse,
8113                           NULL);
8114 }
8115
8116
8117 /**
8118  * gtk_widget_reset_rc_styles:
8119  * @widget: a #GtkWidget.
8120  *
8121  * Reset the styles of @widget and all descendents, so when
8122  * they are looked up again, they get the correct values
8123  * for the currently loaded RC file settings.
8124  *
8125  * This function is not useful for applications.
8126  */
8127 void
8128 gtk_widget_reset_rc_styles (GtkWidget *widget)
8129 {
8130   g_return_if_fail (GTK_IS_WIDGET (widget));
8131
8132   reset_rc_styles_recurse (widget, NULL);
8133 }
8134
8135 /**
8136  * gtk_widget_get_default_style:
8137  *
8138  * Returns the default style used by all widgets initially.
8139  *
8140  * Returns: (transfer none): the default style. This #GtkStyle object is owned
8141  *          by GTK+ and should not be modified or freed.
8142  */
8143 GtkStyle*
8144 gtk_widget_get_default_style (void)
8145 {
8146   if (!gtk_default_style)
8147     {
8148       gtk_default_style = gtk_style_new ();
8149       g_object_ref (gtk_default_style);
8150     }
8151
8152   return gtk_default_style;
8153 }
8154
8155 static PangoContext *
8156 gtk_widget_peek_pango_context (GtkWidget *widget)
8157 {
8158   return g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
8159 }
8160
8161 /**
8162  * gtk_widget_get_pango_context:
8163  * @widget: a #GtkWidget
8164  *
8165  * Gets a #PangoContext with the appropriate font map, font description,
8166  * and base direction for this widget. Unlike the context returned
8167  * by gtk_widget_create_pango_context(), this context is owned by
8168  * the widget (it can be used until the screen for the widget changes
8169  * or the widget is removed from its toplevel), and will be updated to
8170  * match any changes to the widget's attributes.
8171  *
8172  * If you create and keep a #PangoLayout using this context, you must
8173  * deal with changes to the context by calling pango_layout_context_changed()
8174  * on the layout in response to the #GtkWidget::style-set and
8175  * #GtkWidget::direction-changed signals for the widget.
8176  *
8177  * Return value: (transfer none): the #PangoContext for the widget.
8178  **/
8179 PangoContext *
8180 gtk_widget_get_pango_context (GtkWidget *widget)
8181 {
8182   PangoContext *context;
8183
8184   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8185
8186   context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
8187   if (!context)
8188     {
8189       context = gtk_widget_create_pango_context (GTK_WIDGET (widget));
8190       g_object_set_qdata_full (G_OBJECT (widget),
8191                                quark_pango_context,
8192                                context,
8193                                g_object_unref);
8194     }
8195
8196   return context;
8197 }
8198
8199 static void
8200 update_pango_context (GtkWidget    *widget,
8201                       PangoContext *context)
8202 {
8203   GtkWidgetPrivate *priv = widget->priv;
8204
8205   pango_context_set_font_description (context, priv->style->font_desc);
8206   pango_context_set_base_dir (context,
8207                               gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
8208                               PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
8209 }
8210
8211 static void
8212 gtk_widget_update_pango_context (GtkWidget *widget)
8213 {
8214   PangoContext *context = gtk_widget_peek_pango_context (widget);
8215
8216   if (context)
8217     {
8218       GdkScreen *screen;
8219
8220       update_pango_context (widget, context);
8221
8222       screen = gtk_widget_get_screen_unchecked (widget);
8223       if (screen)
8224         {
8225           pango_cairo_context_set_resolution (context,
8226                                               gdk_screen_get_resolution (screen));
8227           pango_cairo_context_set_font_options (context,
8228                                                 gdk_screen_get_font_options (screen));
8229         }
8230     }
8231 }
8232
8233 /**
8234  * gtk_widget_create_pango_context:
8235  * @widget: a #GtkWidget
8236  *
8237  * Creates a new #PangoContext with the appropriate font map,
8238  * font description, and base direction for drawing text for
8239  * this widget. See also gtk_widget_get_pango_context().
8240  *
8241  * Return value: (transfer full): the new #PangoContext
8242  **/
8243 PangoContext *
8244 gtk_widget_create_pango_context (GtkWidget *widget)
8245 {
8246   GdkScreen *screen;
8247   PangoContext *context;
8248
8249   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8250
8251   screen = gtk_widget_get_screen_unchecked (widget);
8252   if (!screen)
8253     {
8254       GTK_NOTE (MULTIHEAD,
8255                 g_warning ("gtk_widget_create_pango_context ()) called without screen"));
8256
8257       screen = gdk_screen_get_default ();
8258     }
8259
8260   context = gdk_pango_context_get_for_screen (screen);
8261
8262   update_pango_context (widget, context);
8263   pango_context_set_language (context, gtk_get_default_language ());
8264
8265   return context;
8266 }
8267
8268 /**
8269  * gtk_widget_create_pango_layout:
8270  * @widget: a #GtkWidget
8271  * @text: text to set on the layout (can be %NULL)
8272  *
8273  * Creates a new #PangoLayout with the appropriate font map,
8274  * font description, and base direction for drawing text for
8275  * this widget.
8276  *
8277  * If you keep a #PangoLayout created in this way around, in order to
8278  * notify the layout of changes to the base direction or font of this
8279  * widget, you must call pango_layout_context_changed() in response to
8280  * the #GtkWidget::style-set and #GtkWidget::direction-changed signals
8281  * for the widget.
8282  *
8283  * Return value: (transfer full): the new #PangoLayout
8284  **/
8285 PangoLayout *
8286 gtk_widget_create_pango_layout (GtkWidget   *widget,
8287                                 const gchar *text)
8288 {
8289   PangoLayout *layout;
8290   PangoContext *context;
8291
8292   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8293
8294   context = gtk_widget_get_pango_context (widget);
8295   layout = pango_layout_new (context);
8296
8297   if (text)
8298     pango_layout_set_text (layout, text, -1);
8299
8300   return layout;
8301 }
8302
8303 /**
8304  * gtk_widget_render_icon:
8305  * @widget: a #GtkWidget
8306  * @stock_id: a stock ID
8307  * @size: (type int): a stock size. A size of (GtkIconSize)-1 means
8308  *     render at the size of the source and don't scale (if there are
8309  *     multiple source sizes, GTK+ picks one of the available sizes).
8310  * @detail: (allow-none): render detail to pass to theme engine
8311  *
8312  * A convenience function that uses the theme engine and RC file
8313  * settings for @widget to look up @stock_id and render it to
8314  * a pixbuf. @stock_id should be a stock icon ID such as
8315  * #GTK_STOCK_OPEN or #GTK_STOCK_OK. @size should be a size
8316  * such as #GTK_ICON_SIZE_MENU. @detail should be a string that
8317  * identifies the widget or code doing the rendering, so that
8318  * theme engines can special-case rendering for that widget or code.
8319  *
8320  * The pixels in the returned #GdkPixbuf are shared with the rest of
8321  * the application and should not be modified. The pixbuf should be freed
8322  * after use with g_object_unref().
8323  *
8324  * Return value: (transfer full): a new pixbuf, or %NULL if the
8325  *     stock ID wasn't known
8326  **/
8327 GdkPixbuf*
8328 gtk_widget_render_icon (GtkWidget      *widget,
8329                         const gchar    *stock_id,
8330                         GtkIconSize     size,
8331                         const gchar    *detail)
8332 {
8333   GtkWidgetPrivate *priv;
8334   GtkIconSet *icon_set;
8335   GdkPixbuf *retval;
8336
8337   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8338   g_return_val_if_fail (stock_id != NULL, NULL);
8339   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
8340
8341   priv = widget->priv;
8342
8343   gtk_widget_ensure_style (widget);
8344
8345   icon_set = gtk_style_lookup_icon_set (priv->style, stock_id);
8346
8347   if (icon_set == NULL)
8348     return NULL;
8349
8350   retval = gtk_icon_set_render_icon (icon_set,
8351                                      priv->style,
8352                                      gtk_widget_get_direction (widget),
8353                                      gtk_widget_get_state (widget),
8354                                      size,
8355                                      widget,
8356                                      detail);
8357
8358   return retval;
8359 }
8360
8361 /**
8362  * gtk_widget_set_parent_window:
8363  * @widget: a #GtkWidget.
8364  * @parent_window: the new parent window.
8365  *
8366  * Sets a non default parent window for @widget.
8367  **/
8368 void
8369 gtk_widget_set_parent_window   (GtkWidget           *widget,
8370                                 GdkWindow           *parent_window)
8371 {
8372   GdkWindow *old_parent_window;
8373
8374   g_return_if_fail (GTK_IS_WIDGET (widget));
8375
8376   old_parent_window = g_object_get_qdata (G_OBJECT (widget),
8377                                           quark_parent_window);
8378
8379   if (parent_window != old_parent_window)
8380     {
8381       g_object_set_qdata (G_OBJECT (widget), quark_parent_window,
8382                           parent_window);
8383       if (old_parent_window)
8384         g_object_unref (old_parent_window);
8385       if (parent_window)
8386         g_object_ref (parent_window);
8387     }
8388 }
8389
8390 /**
8391  * gtk_widget_get_parent_window:
8392  * @widget: a #GtkWidget.
8393  *
8394  * Gets @widget's parent window.
8395  *
8396  * Returns: (transfer none): the parent window of @widget.
8397  **/
8398 GdkWindow *
8399 gtk_widget_get_parent_window (GtkWidget *widget)
8400 {
8401   GtkWidgetPrivate *priv;
8402   GdkWindow *parent_window;
8403
8404   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8405
8406   priv = widget->priv;
8407
8408   parent_window = g_object_get_qdata (G_OBJECT (widget), quark_parent_window);
8409
8410   return (parent_window != NULL) ? parent_window :
8411          (priv->parent != NULL) ? priv->parent->priv->window : NULL;
8412 }
8413
8414
8415 /**
8416  * gtk_widget_set_child_visible:
8417  * @widget: a #GtkWidget
8418  * @is_visible: if %TRUE, @widget should be mapped along with its parent.
8419  *
8420  * Sets whether @widget should be mapped along with its when its parent
8421  * is mapped and @widget has been shown with gtk_widget_show().
8422  *
8423  * The child visibility can be set for widget before it is added to
8424  * a container with gtk_widget_set_parent(), to avoid mapping
8425  * children unnecessary before immediately unmapping them. However
8426  * it will be reset to its default state of %TRUE when the widget
8427  * is removed from a container.
8428  *
8429  * Note that changing the child visibility of a widget does not
8430  * queue a resize on the widget. Most of the time, the size of
8431  * a widget is computed from all visible children, whether or
8432  * not they are mapped. If this is not the case, the container
8433  * can queue a resize itself.
8434  *
8435  * This function is only useful for container implementations and
8436  * never should be called by an application.
8437  **/
8438 void
8439 gtk_widget_set_child_visible (GtkWidget *widget,
8440                               gboolean   is_visible)
8441 {
8442   GtkWidgetPrivate *priv;
8443
8444   g_return_if_fail (GTK_IS_WIDGET (widget));
8445   g_return_if_fail (!gtk_widget_is_toplevel (widget));
8446
8447   priv = widget->priv;
8448
8449   g_object_ref (widget);
8450
8451   if (is_visible)
8452     priv->child_visible = TRUE;
8453   else
8454     {
8455       GtkWidget *toplevel;
8456
8457       priv->child_visible = FALSE;
8458
8459       toplevel = gtk_widget_get_toplevel (widget);
8460       if (toplevel != widget && gtk_widget_is_toplevel (toplevel))
8461         _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
8462     }
8463
8464   if (priv->parent && gtk_widget_get_realized (priv->parent))
8465     {
8466       if (gtk_widget_get_mapped (priv->parent) &&
8467           priv->child_visible &&
8468           gtk_widget_get_visible (widget))
8469         gtk_widget_map (widget);
8470       else
8471         gtk_widget_unmap (widget);
8472     }
8473
8474   g_object_unref (widget);
8475 }
8476
8477 /**
8478  * gtk_widget_get_child_visible:
8479  * @widget: a #GtkWidget
8480  *
8481  * Gets the value set with gtk_widget_set_child_visible().
8482  * If you feel a need to use this function, your code probably
8483  * needs reorganization.
8484  *
8485  * This function is only useful for container implementations and
8486  * never should be called by an application.
8487  *
8488  * Return value: %TRUE if the widget is mapped with the parent.
8489  **/
8490 gboolean
8491 gtk_widget_get_child_visible (GtkWidget *widget)
8492 {
8493   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
8494
8495   return widget->priv->child_visible;
8496 }
8497
8498 static GdkScreen *
8499 gtk_widget_get_screen_unchecked (GtkWidget *widget)
8500 {
8501   GtkWidget *toplevel;
8502
8503   toplevel = gtk_widget_get_toplevel (widget);
8504
8505   if (gtk_widget_is_toplevel (toplevel))
8506     {
8507       if (GTK_IS_WINDOW (toplevel))
8508         return gtk_window_get_screen (GTK_WINDOW (toplevel));
8509       else if (GTK_IS_INVISIBLE (toplevel))
8510         return gtk_invisible_get_screen (GTK_INVISIBLE (widget));
8511     }
8512
8513   return NULL;
8514 }
8515
8516 /**
8517  * gtk_widget_get_screen:
8518  * @widget: a #GtkWidget
8519  *
8520  * Get the #GdkScreen from the toplevel window associated with
8521  * this widget. This function can only be called after the widget
8522  * has been added to a widget hierarchy with a #GtkWindow
8523  * at the top.
8524  *
8525  * In general, you should only create screen specific
8526  * resources when a widget has been realized, and you should
8527  * free those resources when the widget is unrealized.
8528  *
8529  * Return value: (transfer none): the #GdkScreen for the toplevel for this widget.
8530  *
8531  * Since: 2.2
8532  **/
8533 GdkScreen*
8534 gtk_widget_get_screen (GtkWidget *widget)
8535 {
8536   GdkScreen *screen;
8537
8538   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8539
8540   screen = gtk_widget_get_screen_unchecked (widget);
8541
8542   if (screen)
8543     return screen;
8544   else
8545     {
8546 #if 0
8547       g_warning (G_STRLOC ": Can't get associated screen"
8548                  " for a widget unless it is inside a toplevel GtkWindow\n"
8549                  " widget type is %s associated top level type is %s",
8550                  g_type_name (G_OBJECT_TYPE(G_OBJECT (widget))),
8551                  g_type_name (G_OBJECT_TYPE(G_OBJECT (toplevel))));
8552 #endif
8553       return gdk_screen_get_default ();
8554     }
8555 }
8556
8557 /**
8558  * gtk_widget_has_screen:
8559  * @widget: a #GtkWidget
8560  *
8561  * Checks whether there is a #GdkScreen is associated with
8562  * this widget. All toplevel widgets have an associated
8563  * screen, and all widgets added into a hierarchy with a toplevel
8564  * window at the top.
8565  *
8566  * Return value: %TRUE if there is a #GdkScreen associcated
8567  *   with the widget.
8568  *
8569  * Since: 2.2
8570  **/
8571 gboolean
8572 gtk_widget_has_screen (GtkWidget *widget)
8573 {
8574   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
8575
8576   return (gtk_widget_get_screen_unchecked (widget) != NULL);
8577 }
8578
8579 /**
8580  * gtk_widget_get_display:
8581  * @widget: a #GtkWidget
8582  *
8583  * Get the #GdkDisplay for the toplevel window associated with
8584  * this widget. This function can only be called after the widget
8585  * has been added to a widget hierarchy with a #GtkWindow at the top.
8586  *
8587  * In general, you should only create display specific
8588  * resources when a widget has been realized, and you should
8589  * free those resources when the widget is unrealized.
8590  *
8591  * Return value: (transfer none): the #GdkDisplay for the toplevel for this widget.
8592  *
8593  * Since: 2.2
8594  **/
8595 GdkDisplay*
8596 gtk_widget_get_display (GtkWidget *widget)
8597 {
8598   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8599
8600   return gdk_screen_get_display (gtk_widget_get_screen (widget));
8601 }
8602
8603 /**
8604  * gtk_widget_get_root_window:
8605  * @widget: a #GtkWidget
8606  *
8607  * Get the root window where this widget is located. This function can
8608  * only be called after the widget has been added to a widget
8609  * hierarchy with #GtkWindow at the top.
8610  *
8611  * The root window is useful for such purposes as creating a popup
8612  * #GdkWindow associated with the window. In general, you should only
8613  * create display specific resources when a widget has been realized,
8614  * and you should free those resources when the widget is unrealized.
8615  *
8616  * Return value: (transfer none): the #GdkWindow root window for the toplevel for this widget.
8617  *
8618  * Since: 2.2
8619  **/
8620 GdkWindow*
8621 gtk_widget_get_root_window (GtkWidget *widget)
8622 {
8623   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8624
8625   return gdk_screen_get_root_window (gtk_widget_get_screen (widget));
8626 }
8627
8628 /**
8629  * gtk_widget_child_focus:
8630  * @widget: a #GtkWidget
8631  * @direction: direction of focus movement
8632  *
8633  * This function is used by custom widget implementations; if you're
8634  * writing an app, you'd use gtk_widget_grab_focus() to move the focus
8635  * to a particular widget, and gtk_container_set_focus_chain() to
8636  * change the focus tab order. So you may want to investigate those
8637  * functions instead.
8638  *
8639  * gtk_widget_child_focus() is called by containers as the user moves
8640  * around the window using keyboard shortcuts. @direction indicates
8641  * what kind of motion is taking place (up, down, left, right, tab
8642  * forward, tab backward). gtk_widget_child_focus() emits the
8643  * #GtkWidget::focus signal; widgets override the default handler
8644  * for this signal in order to implement appropriate focus behavior.
8645  *
8646  * The default ::focus handler for a widget should return %TRUE if
8647  * moving in @direction left the focus on a focusable location inside
8648  * that widget, and %FALSE if moving in @direction moved the focus
8649  * outside the widget. If returning %TRUE, widgets normally
8650  * call gtk_widget_grab_focus() to place the focus accordingly;
8651  * if returning %FALSE, they don't modify the current focus location.
8652  *
8653  * Return value: %TRUE if focus ended up inside @widget
8654  **/
8655 gboolean
8656 gtk_widget_child_focus (GtkWidget       *widget,
8657                         GtkDirectionType direction)
8658 {
8659   gboolean return_val;
8660
8661   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
8662
8663   if (!gtk_widget_get_visible (widget) ||
8664       !gtk_widget_is_sensitive (widget))
8665     return FALSE;
8666
8667   /* child widgets must set CAN_FOCUS, containers
8668    * don't have to though.
8669    */
8670   if (!GTK_IS_CONTAINER (widget) &&
8671       !gtk_widget_get_can_focus (widget))
8672     return FALSE;
8673
8674   g_signal_emit (widget,
8675                  widget_signals[FOCUS],
8676                  0,
8677                  direction, &return_val);
8678
8679   return return_val;
8680 }
8681
8682 /**
8683  * gtk_widget_keynav_failed:
8684  * @widget: a #GtkWidget
8685  * @direction: direction of focus movement
8686  *
8687  * This function should be called whenever keyboard navigation within
8688  * a single widget hits a boundary. The function emits the
8689  * #GtkWidget::keynav-failed signal on the widget and its return
8690  * value should be interpreted in a way similar to the return value of
8691  * gtk_widget_child_focus():
8692  *
8693  * When %TRUE is returned, stay in the widget, the failed keyboard
8694  * navigation is Ok and/or there is nowhere we can/should move the
8695  * focus to.
8696  *
8697  * When %FALSE is returned, the caller should continue with keyboard
8698  * navigation outside the widget, e.g. by calling
8699  * gtk_widget_child_focus() on the widget's toplevel.
8700  *
8701  * The default ::keynav-failed handler returns %TRUE for
8702  * %GTK_DIR_TAB_FORWARD and %GTK_DIR_TAB_BACKWARD. For the other
8703  * values of #GtkDirectionType, it looks at the
8704  * #GtkSettings:gtk-keynav-cursor-only setting and returns %FALSE
8705  * if the setting is %TRUE. This way the entire user interface
8706  * becomes cursor-navigatable on input devices such as mobile phones
8707  * which only have cursor keys but no tab key.
8708  *
8709  * Whenever the default handler returns %TRUE, it also calls
8710  * gtk_widget_error_bell() to notify the user of the failed keyboard
8711  * navigation.
8712  *
8713  * A use case for providing an own implementation of ::keynav-failed
8714  * (either by connecting to it or by overriding it) would be a row of
8715  * #GtkEntry widgets where the user should be able to navigate the
8716  * entire row with the cursor keys, as e.g. known from user interfaces
8717  * that require entering license keys.
8718  *
8719  * Return value: %TRUE if stopping keyboard navigation is fine, %FALSE
8720  *               if the emitting widget should try to handle the keyboard
8721  *               navigation attempt in its parent container(s).
8722  *
8723  * Since: 2.12
8724  **/
8725 gboolean
8726 gtk_widget_keynav_failed (GtkWidget        *widget,
8727                           GtkDirectionType  direction)
8728 {
8729   gboolean return_val;
8730
8731   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
8732
8733   g_signal_emit (widget, widget_signals[KEYNAV_FAILED], 0,
8734                  direction, &return_val);
8735
8736   return return_val;
8737 }
8738
8739 /**
8740  * gtk_widget_error_bell:
8741  * @widget: a #GtkWidget
8742  *
8743  * Notifies the user about an input-related error on this widget.
8744  * If the #GtkSettings:gtk-error-bell setting is %TRUE, it calls
8745  * gdk_window_beep(), otherwise it does nothing.
8746  *
8747  * Note that the effect of gdk_window_beep() can be configured in many
8748  * ways, depending on the windowing backend and the desktop environment
8749  * or window manager that is used.
8750  *
8751  * Since: 2.12
8752  **/
8753 void
8754 gtk_widget_error_bell (GtkWidget *widget)
8755 {
8756   GtkWidgetPrivate *priv;
8757   GtkSettings* settings;
8758   gboolean beep;
8759
8760   g_return_if_fail (GTK_IS_WIDGET (widget));
8761
8762   priv = widget->priv;
8763
8764   settings = gtk_widget_get_settings (widget);
8765   if (!settings)
8766     return;
8767
8768   g_object_get (settings,
8769                 "gtk-error-bell", &beep,
8770                 NULL);
8771
8772   if (beep && priv->window)
8773     gdk_window_beep (priv->window);
8774 }
8775
8776 static void
8777 gtk_widget_set_usize_internal (GtkWidget          *widget,
8778                                gint                width,
8779                                gint                height,
8780                                GtkQueueResizeFlags flags)
8781 {
8782   GtkWidgetAuxInfo *aux_info;
8783   gboolean changed = FALSE;
8784
8785   g_object_freeze_notify (G_OBJECT (widget));
8786
8787   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
8788
8789   if (width > -2 && aux_info->width != width)
8790     {
8791       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
8792         g_object_notify (G_OBJECT (widget), "width-request");
8793       aux_info->width = width;
8794       changed = TRUE;
8795     }
8796   if (height > -2 && aux_info->height != height)
8797     {
8798       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
8799         g_object_notify (G_OBJECT (widget), "height-request");
8800       aux_info->height = height;
8801       changed = TRUE;
8802     }
8803
8804   if (gtk_widget_get_visible (widget) && changed)
8805     {
8806       if ((flags & GTK_QUEUE_RESIZE_INVALIDATE_ONLY) == 0)
8807         gtk_widget_queue_resize (widget);
8808       else
8809         _gtk_size_group_queue_resize (widget, GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
8810     }
8811
8812   g_object_thaw_notify (G_OBJECT (widget));
8813 }
8814
8815 /**
8816  * gtk_widget_set_size_request:
8817  * @widget: a #GtkWidget
8818  * @width: width @widget should request, or -1 to unset
8819  * @height: height @widget should request, or -1 to unset
8820  *
8821  * Sets the minimum size of a widget; that is, the widget's size
8822  * request will be @width by @height. You can use this function to
8823  * force a widget to be either larger or smaller than it normally
8824  * would be.
8825  *
8826  * In most cases, gtk_window_set_default_size() is a better choice for
8827  * toplevel windows than this function; setting the default size will
8828  * still allow users to shrink the window. Setting the size request
8829  * will force them to leave the window at least as large as the size
8830  * request. When dealing with window sizes,
8831  * gtk_window_set_geometry_hints() can be a useful function as well.
8832  *
8833  * Note the inherent danger of setting any fixed size - themes,
8834  * translations into other languages, different fonts, and user action
8835  * can all change the appropriate size for a given widget. So, it's
8836  * basically impossible to hardcode a size that will always be
8837  * correct.
8838  *
8839  * The size request of a widget is the smallest size a widget can
8840  * accept while still functioning well and drawing itself correctly.
8841  * However in some strange cases a widget may be allocated less than
8842  * its requested size, and in many cases a widget may be allocated more
8843  * space than it requested.
8844  *
8845  * If the size request in a given direction is -1 (unset), then
8846  * the "natural" size request of the widget will be used instead.
8847  *
8848  * Widgets can't actually be allocated a size less than 1 by 1, but
8849  * you can pass 0,0 to this function to mean "as small as possible."
8850  *
8851  * The size request set here does not include any margin from the
8852  * #GtkWidget properties margin-left, margin-right, margin-top, and
8853  * margin-bottom, but it does include pretty much all other padding
8854  * or border properties set by any subclass of #GtkWidget.
8855  **/
8856 void
8857 gtk_widget_set_size_request (GtkWidget *widget,
8858                              gint       width,
8859                              gint       height)
8860 {
8861   g_return_if_fail (GTK_IS_WIDGET (widget));
8862   g_return_if_fail (width >= -1);
8863   g_return_if_fail (height >= -1);
8864
8865   if (width == 0)
8866     width = 1;
8867   if (height == 0)
8868     height = 1;
8869
8870   gtk_widget_set_usize_internal (widget, width, height, 0);
8871 }
8872
8873
8874 /**
8875  * gtk_widget_get_size_request:
8876  * @widget: a #GtkWidget
8877  * @width: (out) (allow-none): return location for width, or %NULL
8878  * @height: (out) (allow-none): return location for height, or %NULL
8879  *
8880  * Gets the size request that was explicitly set for the widget using
8881  * gtk_widget_set_size_request(). A value of -1 stored in @width or
8882  * @height indicates that that dimension has not been set explicitly
8883  * and the natural requisition of the widget will be used intead. See
8884  * gtk_widget_set_size_request(). To get the size a widget will
8885  * actually request, call gtk_widget_get_preferred_size() instead of
8886  * this function.
8887  **/
8888 void
8889 gtk_widget_get_size_request (GtkWidget *widget,
8890                              gint      *width,
8891                              gint      *height)
8892 {
8893   const GtkWidgetAuxInfo *aux_info;
8894
8895   g_return_if_fail (GTK_IS_WIDGET (widget));
8896
8897   aux_info = _gtk_widget_get_aux_info_or_defaults (widget);
8898
8899   if (width)
8900     *width = aux_info->width;
8901
8902   if (height)
8903     *height = aux_info->height;
8904 }
8905
8906 /**
8907  * _gtk_widget_override_size_request:
8908  * @widget: a #GtkWidget
8909  * @width: new forced minimum width
8910  * @height: new forced minimum height
8911  * @old_width: location to store previous forced minimum width
8912  * @old_width: location to store previous forced minumum height
8913  *
8914  * Temporarily establishes a forced minimum size for a widget; this
8915  * is used by GtkWindow when calculating the size to add to the
8916  * window's geometry widget. Cached sizes for the widget and its
8917  * parents are invalidated, so that subsequent calls to the size
8918  * negotiation machinery produce the overriden result, but the
8919  * widget is not queued for relayout or redraw. The old size must
8920  * be restored with _gtk_widget_restore_size_request() or things
8921  * will go screwy.
8922  */
8923 void
8924 _gtk_widget_override_size_request (GtkWidget *widget,
8925                                    int        width,
8926                                    int        height,
8927                                    int       *old_width,
8928                                    int       *old_height)
8929 {
8930   gtk_widget_get_size_request (widget, old_width, old_height);
8931   gtk_widget_set_usize_internal (widget, width, height,
8932                                  GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
8933 }
8934
8935 /**
8936  * _gtk_widget_restore_size_request:
8937  * @widget: a #GtkWidget
8938  * @old_width: saved forced minimum size
8939  * @old_height: saved forced minimum size
8940  *
8941  * Undoes the operation of_gtk_widget_override_size_request().
8942  */
8943 void
8944 _gtk_widget_restore_size_request (GtkWidget *widget,
8945                                   int        old_width,
8946                                   int        old_height)
8947 {
8948   gtk_widget_set_usize_internal (widget, old_width, old_height,
8949                                  GTK_QUEUE_RESIZE_INVALIDATE_ONLY);
8950 }
8951
8952 /**
8953  * gtk_widget_set_events:
8954  * @widget: a #GtkWidget
8955  * @events: event mask
8956  *
8957  * Sets the event mask (see #GdkEventMask) for a widget. The event
8958  * mask determines which events a widget will receive. Keep in mind
8959  * that different widgets have different default event masks, and by
8960  * changing the event mask you may disrupt a widget's functionality,
8961  * so be careful. This function must be called while a widget is
8962  * unrealized. Consider gtk_widget_add_events() for widgets that are
8963  * already realized, or if you want to preserve the existing event
8964  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
8965  * to get events on those widgets, place them inside a #GtkEventBox
8966  * and receive events on the event box.
8967  **/
8968 void
8969 gtk_widget_set_events (GtkWidget *widget,
8970                        gint       events)
8971 {
8972   g_return_if_fail (GTK_IS_WIDGET (widget));
8973   g_return_if_fail (!gtk_widget_get_realized (widget));
8974
8975   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
8976                       GINT_TO_POINTER (events));
8977   g_object_notify (G_OBJECT (widget), "events");
8978 }
8979
8980 /**
8981  * gtk_widget_set_device_events:
8982  * @widget: a #GtkWidget
8983  * @device: a #GdkDevice
8984  * @events: event mask
8985  *
8986  * Sets the device event mask (see #GdkEventMask) for a widget. The event
8987  * mask determines which events a widget will receive from @device. Keep
8988  * in mind that different widgets have different default event masks, and by
8989  * changing the event mask you may disrupt a widget's functionality,
8990  * so be careful. This function must be called while a widget is
8991  * unrealized. Consider gtk_widget_add_device_events() for widgets that are
8992  * already realized, or if you want to preserve the existing event
8993  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
8994  * to get events on those widgets, place them inside a #GtkEventBox
8995  * and receive events on the event box.
8996  *
8997  * Since: 3.0
8998  **/
8999 void
9000 gtk_widget_set_device_events (GtkWidget    *widget,
9001                               GdkDevice    *device,
9002                               GdkEventMask  events)
9003 {
9004   GHashTable *device_events;
9005
9006   g_return_if_fail (GTK_IS_WIDGET (widget));
9007   g_return_if_fail (GDK_IS_DEVICE (device));
9008   g_return_if_fail (!gtk_widget_get_realized (widget));
9009
9010   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
9011
9012   if (G_UNLIKELY (!device_events))
9013     {
9014       device_events = g_hash_table_new (NULL, NULL);
9015       g_object_set_qdata_full (G_OBJECT (widget), quark_device_event_mask, device_events,
9016                                (GDestroyNotify) g_hash_table_unref);
9017     }
9018
9019   g_hash_table_insert (device_events, device, GUINT_TO_POINTER (events));
9020 }
9021
9022 static void
9023 gtk_widget_add_events_internal_list (GtkWidget *widget,
9024                                      GdkDevice *device,
9025                                      gint       events,
9026                                      GList     *window_list)
9027 {
9028   GList *l;
9029
9030   for (l = window_list; l != NULL; l = l->next)
9031     {
9032       GdkWindow *window = l->data;
9033       gpointer user_data;
9034
9035       gdk_window_get_user_data (window, &user_data);
9036       if (user_data == widget)
9037         {
9038           GList *children;
9039
9040           if (device)
9041             gdk_window_set_device_events (window, device, gdk_window_get_events (window) | events);
9042           else
9043             gdk_window_set_events (window, gdk_window_get_events (window) | events);
9044
9045           children = gdk_window_get_children (window);
9046           gtk_widget_add_events_internal_list (widget, device, events, children);
9047           g_list_free (children);
9048         }
9049     }
9050 }
9051
9052 static void
9053 gtk_widget_add_events_internal (GtkWidget *widget,
9054                                 GdkDevice *device,
9055                                 gint       events)
9056 {
9057   GtkWidgetPrivate *priv = widget->priv;
9058   GList *window_list;
9059
9060   if (!gtk_widget_get_has_window (widget))
9061     window_list = gdk_window_get_children (priv->window);
9062   else
9063     window_list = g_list_prepend (NULL, priv->window);
9064
9065   gtk_widget_add_events_internal_list (widget, device, events, window_list);
9066
9067   g_list_free (window_list);
9068 }
9069
9070 /**
9071  * gtk_widget_add_events:
9072  * @widget: a #GtkWidget
9073  * @events: an event mask, see #GdkEventMask
9074  *
9075  * Adds the events in the bitfield @events to the event mask for
9076  * @widget. See gtk_widget_set_events() for details.
9077  **/
9078 void
9079 gtk_widget_add_events (GtkWidget *widget,
9080                        gint       events)
9081 {
9082   gint old_events;
9083
9084   g_return_if_fail (GTK_IS_WIDGET (widget));
9085
9086   old_events = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
9087   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
9088                       GINT_TO_POINTER (old_events | events));
9089
9090   if (gtk_widget_get_realized (widget))
9091     gtk_widget_add_events_internal (widget, NULL, events);
9092
9093   g_object_notify (G_OBJECT (widget), "events");
9094 }
9095
9096 /**
9097  * gtk_widget_add_device_events:
9098  * @widget: a #GtkWidget
9099  * @device: a #GdkDevice
9100  * @events: an event mask, see #GdkEventMask
9101  *
9102  * Adds the device events in the bitfield @events to the event mask for
9103  * @widget. See gtk_widget_set_device_events() for details.
9104  *
9105  * Since: 3.0
9106  **/
9107 void
9108 gtk_widget_add_device_events (GtkWidget    *widget,
9109                               GdkDevice    *device,
9110                               GdkEventMask  events)
9111 {
9112   GdkEventMask old_events;
9113   GHashTable *device_events;
9114
9115   g_return_if_fail (GTK_IS_WIDGET (widget));
9116   g_return_if_fail (GDK_IS_DEVICE (device));
9117
9118   old_events = gtk_widget_get_device_events (widget, device);
9119
9120   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
9121
9122   if (G_UNLIKELY (!device_events))
9123     {
9124       device_events = g_hash_table_new (NULL, NULL);
9125       g_object_set_qdata_full (G_OBJECT (widget), quark_device_event_mask, device_events,
9126                                (GDestroyNotify) g_hash_table_unref);
9127     }
9128
9129   g_hash_table_insert (device_events, device,
9130                        GUINT_TO_POINTER (old_events | events));
9131
9132   if (gtk_widget_get_realized (widget))
9133     gtk_widget_add_events_internal (widget, device, events);
9134
9135   g_object_notify (G_OBJECT (widget), "events");
9136 }
9137
9138 /**
9139  * gtk_widget_set_extension_events:
9140  * @widget: a #GtkWidget
9141  * @mode: bitfield of extension events to receive
9142  *
9143  * Sets the extension events mask to @mode. See #GdkExtensionMode
9144  * and gdk_input_set_extension_events().
9145  **/
9146 void
9147 gtk_widget_set_extension_events (GtkWidget *widget,
9148                                  GdkExtensionMode mode)
9149 {
9150   g_return_if_fail (GTK_IS_WIDGET (widget));
9151
9152   if (gtk_widget_get_realized (widget))
9153     gtk_widget_set_extension_events_internal (widget, mode, NULL);
9154
9155   g_object_set_qdata (G_OBJECT (widget), quark_extension_event_mode,
9156                       GINT_TO_POINTER (mode));
9157   g_object_notify (G_OBJECT (widget), "extension-events");
9158 }
9159
9160 /**
9161  * gtk_widget_get_toplevel:
9162  * @widget: a #GtkWidget
9163  *
9164  * This function returns the topmost widget in the container hierarchy
9165  * @widget is a part of. If @widget has no parent widgets, it will be
9166  * returned as the topmost widget. No reference will be added to the
9167  * returned widget; it should not be unreferenced.
9168  *
9169  * Note the difference in behavior vs. gtk_widget_get_ancestor();
9170  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)</literal>
9171  * would return
9172  * %NULL if @widget wasn't inside a toplevel window, and if the
9173  * window was inside a #GtkWindow-derived widget which was in turn
9174  * inside the toplevel #GtkWindow. While the second case may
9175  * seem unlikely, it actually happens when a #GtkPlug is embedded
9176  * inside a #GtkSocket within the same application.
9177  *
9178  * To reliably find the toplevel #GtkWindow, use
9179  * gtk_widget_get_toplevel() and check if the %TOPLEVEL flags
9180  * is set on the result.
9181  * |[
9182  *  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
9183  *  if (gtk_widget_is_toplevel (toplevel))
9184  *    {
9185  *      /&ast; Perform action on toplevel. &ast;/
9186  *    }
9187  * ]|
9188  *
9189  * Return value: (transfer none): the topmost ancestor of @widget, or @widget itself
9190  *    if there's no ancestor.
9191  **/
9192 GtkWidget*
9193 gtk_widget_get_toplevel (GtkWidget *widget)
9194 {
9195   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9196
9197   while (widget->priv->parent)
9198     widget = widget->priv->parent;
9199
9200   return widget;
9201 }
9202
9203 /**
9204  * gtk_widget_get_ancestor:
9205  * @widget: a #GtkWidget
9206  * @widget_type: ancestor type
9207  *
9208  * Gets the first ancestor of @widget with type @widget_type. For example,
9209  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_BOX)</literal> gets
9210  * the first #GtkBox that's an ancestor of @widget. No reference will be
9211  * added to the returned widget; it should not be unreferenced. See note
9212  * about checking for a toplevel #GtkWindow in the docs for
9213  * gtk_widget_get_toplevel().
9214  *
9215  * Note that unlike gtk_widget_is_ancestor(), gtk_widget_get_ancestor()
9216  * considers @widget to be an ancestor of itself.
9217  *
9218  * Return value: (transfer none): the ancestor widget, or %NULL if not found
9219  **/
9220 GtkWidget*
9221 gtk_widget_get_ancestor (GtkWidget *widget,
9222                          GType      widget_type)
9223 {
9224   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9225
9226   while (widget && !g_type_is_a (G_OBJECT_TYPE (widget), widget_type))
9227     widget = widget->priv->parent;
9228
9229   if (!(widget && g_type_is_a (G_OBJECT_TYPE (widget), widget_type)))
9230     return NULL;
9231
9232   return widget;
9233 }
9234
9235 /**
9236  * gtk_widget_set_visual:
9237  * @widget: a #GtkWidget
9238  * @visual: visual to be used or %NULL to unset a previous one
9239  *
9240  * Sets the visual that should be used for by widget and its children for
9241  * creating #GdkWindows. The visual must be on the same #GdkScreen as
9242  * returned by gdk_widget_get_screen(), so handling the
9243  * #GtkWidget::screen-changed signal is necessary.
9244  *
9245  * Setting a new @visual will not cause @widget to recreate its windows,
9246  * so you should call this function before @widget is realized.
9247  **/
9248 void
9249 gtk_widget_set_visual (GtkWidget *widget,
9250                        GdkVisual *visual)
9251 {
9252   g_return_if_fail (GTK_IS_WIDGET (widget));
9253   g_return_if_fail (visual == NULL || GDK_IS_VISUAL (visual));
9254   if (visual)
9255     {
9256       g_return_if_fail (gtk_widget_get_screen (widget) == gdk_visual_get_screen (visual));
9257     }
9258
9259   g_object_set_qdata_full (G_OBJECT (widget),
9260                            quark_visual,
9261                            g_object_ref (visual),
9262                            g_object_unref);
9263 }
9264
9265 /**
9266  * gtk_widget_get_visual:
9267  * @widget: a #GtkWidget
9268  *
9269  * Gets the visual that will be used to render @widget.
9270  *
9271  * Return value: (transfer none): the visual for @widget
9272  **/
9273 GdkVisual*
9274 gtk_widget_get_visual (GtkWidget *widget)
9275 {
9276   GtkWidget *w;
9277   GdkVisual *visual;
9278   GdkScreen *screen;
9279
9280   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9281
9282   if (gtk_widget_get_has_window (widget) &&
9283       widget->priv->window)
9284     return gdk_window_get_visual (widget->priv->window);
9285
9286   screen = gtk_widget_get_screen (widget);
9287
9288   for (w = widget; w != NULL; w = w->priv->parent)
9289     {
9290       visual = g_object_get_qdata (G_OBJECT (w), quark_visual);
9291       if (visual)
9292         {
9293           if (gdk_visual_get_screen (visual) == screen)
9294             return visual;
9295
9296           g_warning ("Ignoring visual set on widget `%s' that is not on the correct screen.",
9297                      gtk_widget_get_name (widget));
9298         }
9299     }
9300
9301   return gdk_screen_get_system_visual (screen);
9302 }
9303
9304 /**
9305  * gtk_widget_get_settings:
9306  * @widget: a #GtkWidget
9307  *
9308  * Gets the settings object holding the settings (global property
9309  * settings, RC file information, etc) used for this widget.
9310  *
9311  * Note that this function can only be called when the #GtkWidget
9312  * is attached to a toplevel, since the settings object is specific
9313  * to a particular #GdkScreen.
9314  *
9315  * Return value: (transfer none): the relevant #GtkSettings object
9316  **/
9317 GtkSettings*
9318 gtk_widget_get_settings (GtkWidget *widget)
9319 {
9320   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9321
9322   return gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
9323 }
9324
9325 /**
9326  * gtk_widget_get_events:
9327  * @widget: a #GtkWidget
9328  *
9329  * Returns the event mask for the widget (a bitfield containing flags
9330  * from the #GdkEventMask enumeration). These are the events that the widget
9331  * will receive.
9332  *
9333  * Return value: event mask for @widget
9334  **/
9335 gint
9336 gtk_widget_get_events (GtkWidget *widget)
9337 {
9338   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
9339
9340   return GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
9341 }
9342
9343 /**
9344  * gtk_widget_get_device_events:
9345  * @widget: a #GtkWidget
9346  * @device: a #GdkDevice
9347  *
9348  * Returns the events mask for the widget corresponding to an specific device. These
9349  * are the events that the widget will receive when @device operates on it.
9350  *
9351  * Returns: device event mask for @widget
9352  *
9353  * Since: 3.0
9354  **/
9355 GdkEventMask
9356 gtk_widget_get_device_events (GtkWidget *widget,
9357                               GdkDevice *device)
9358 {
9359   GHashTable *device_events;
9360
9361   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
9362   g_return_val_if_fail (GDK_IS_DEVICE (device), 0);
9363
9364   device_events = g_object_get_qdata (G_OBJECT (widget), quark_device_event_mask);
9365
9366   if (!device_events)
9367     return 0;
9368
9369   return GPOINTER_TO_UINT (g_hash_table_lookup (device_events, device));
9370 }
9371
9372 /**
9373  * gtk_widget_get_extension_events:
9374  * @widget: a #GtkWidget
9375  *
9376  * Retrieves the extension events the widget will receive; see
9377  * gdk_input_set_extension_events().
9378  *
9379  * Return value: extension events for @widget
9380  **/
9381 GdkExtensionMode
9382 gtk_widget_get_extension_events (GtkWidget *widget)
9383 {
9384   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
9385
9386   return GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_extension_event_mode));
9387 }
9388
9389 /**
9390  * gtk_widget_get_pointer:
9391  * @widget: a #GtkWidget
9392  * @x: (out) (allow-none): return location for the X coordinate, or %NULL
9393  * @y: (out) (allow-none): return location for the Y coordinate, or %NULL
9394  *
9395  * Obtains the location of the mouse pointer in widget coordinates.
9396  * Widget coordinates are a bit odd; for historical reasons, they are
9397  * defined as @widget->window coordinates for widgets that are not
9398  * #GTK_NO_WINDOW widgets, and are relative to @widget->allocation.x,
9399  * @widget->allocation.y for widgets that are #GTK_NO_WINDOW widgets.
9400  **/
9401 void
9402 gtk_widget_get_pointer (GtkWidget *widget,
9403                         gint      *x,
9404                         gint      *y)
9405 {
9406   GtkWidgetPrivate *priv;
9407
9408   g_return_if_fail (GTK_IS_WIDGET (widget));
9409
9410   priv = widget->priv;
9411
9412   if (x)
9413     *x = -1;
9414   if (y)
9415     *y = -1;
9416
9417   if (gtk_widget_get_realized (widget))
9418     {
9419       gdk_window_get_pointer (priv->window, x, y, NULL);
9420
9421       if (!gtk_widget_get_has_window (widget))
9422         {
9423           if (x)
9424             *x -= priv->allocation.x;
9425           if (y)
9426             *y -= priv->allocation.y;
9427         }
9428     }
9429 }
9430
9431 /**
9432  * gtk_widget_is_ancestor:
9433  * @widget: a #GtkWidget
9434  * @ancestor: another #GtkWidget
9435  *
9436  * Determines whether @widget is somewhere inside @ancestor, possibly with
9437  * intermediate containers.
9438  *
9439  * Return value: %TRUE if @ancestor contains @widget as a child,
9440  *    grandchild, great grandchild, etc.
9441  **/
9442 gboolean
9443 gtk_widget_is_ancestor (GtkWidget *widget,
9444                         GtkWidget *ancestor)
9445 {
9446   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
9447   g_return_val_if_fail (ancestor != NULL, FALSE);
9448
9449   while (widget)
9450     {
9451       if (widget->priv->parent == ancestor)
9452         return TRUE;
9453       widget = widget->priv->parent;
9454     }
9455
9456   return FALSE;
9457 }
9458
9459 static GQuark quark_composite_name = 0;
9460
9461 /**
9462  * gtk_widget_set_composite_name:
9463  * @widget: a #GtkWidget.
9464  * @name: the name to set
9465  *
9466  * Sets a widgets composite name. The widget must be
9467  * a composite child of its parent; see gtk_widget_push_composite_child().
9468  **/
9469 void
9470 gtk_widget_set_composite_name (GtkWidget   *widget,
9471                                const gchar *name)
9472 {
9473   g_return_if_fail (GTK_IS_WIDGET (widget));
9474   g_return_if_fail (widget->priv->composite_child);
9475   g_return_if_fail (name != NULL);
9476
9477   if (!quark_composite_name)
9478     quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
9479
9480   g_object_set_qdata_full (G_OBJECT (widget),
9481                            quark_composite_name,
9482                            g_strdup (name),
9483                            g_free);
9484 }
9485
9486 /**
9487  * gtk_widget_get_composite_name:
9488  * @widget: a #GtkWidget
9489  *
9490  * Obtains the composite name of a widget.
9491  *
9492  * Returns: the composite name of @widget, or %NULL if @widget is not
9493  *   a composite child. The string should be freed when it is no
9494  *   longer needed.
9495  **/
9496 gchar*
9497 gtk_widget_get_composite_name (GtkWidget *widget)
9498 {
9499   GtkWidgetPrivate *priv;
9500
9501   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9502
9503   priv = widget->priv;
9504
9505   if (widget->priv->composite_child && priv->parent)
9506     return _gtk_container_child_composite_name (GTK_CONTAINER (priv->parent),
9507                                                widget);
9508   else
9509     return NULL;
9510 }
9511
9512 /**
9513  * gtk_widget_push_composite_child:
9514  *
9515  * Makes all newly-created widgets as composite children until
9516  * the corresponding gtk_widget_pop_composite_child() call.
9517  *
9518  * A composite child is a child that's an implementation detail of the
9519  * container it's inside and should not be visible to people using the
9520  * container. Composite children aren't treated differently by GTK (but
9521  * see gtk_container_foreach() vs. gtk_container_forall()), but e.g. GUI
9522  * builders might want to treat them in a different way.
9523  *
9524  * Here is a simple example:
9525  * |[
9526  *   gtk_widget_push_composite_child ();
9527  *   scrolled_window->hscrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, hadjustment);
9528  *   gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
9529  *   gtk_widget_pop_composite_child ();
9530  *   gtk_widget_set_parent (scrolled_window->hscrollbar,
9531  *                          GTK_WIDGET (scrolled_window));
9532  *   g_object_ref (scrolled_window->hscrollbar);
9533  * ]|
9534  **/
9535 void
9536 gtk_widget_push_composite_child (void)
9537 {
9538   composite_child_stack++;
9539 }
9540
9541 /**
9542  * gtk_widget_pop_composite_child:
9543  *
9544  * Cancels the effect of a previous call to gtk_widget_push_composite_child().
9545  **/
9546 void
9547 gtk_widget_pop_composite_child (void)
9548 {
9549   if (composite_child_stack)
9550     composite_child_stack--;
9551 }
9552
9553 static void
9554 gtk_widget_emit_direction_changed (GtkWidget        *widget,
9555                                    GtkTextDirection  old_dir)
9556 {
9557   gtk_widget_update_pango_context (widget);
9558
9559   g_signal_emit (widget, widget_signals[DIRECTION_CHANGED], 0, old_dir);
9560 }
9561
9562 /**
9563  * gtk_widget_set_direction:
9564  * @widget: a #GtkWidget
9565  * @dir:    the new direction
9566  *
9567  * Sets the reading direction on a particular widget. This direction
9568  * controls the primary direction for widgets containing text,
9569  * and also the direction in which the children of a container are
9570  * packed. The ability to set the direction is present in order
9571  * so that correct localization into languages with right-to-left
9572  * reading directions can be done. Generally, applications will
9573  * let the default reading direction present, except for containers
9574  * where the containers are arranged in an order that is explicitely
9575  * visual rather than logical (such as buttons for text justification).
9576  *
9577  * If the direction is set to %GTK_TEXT_DIR_NONE, then the value
9578  * set by gtk_widget_set_default_direction() will be used.
9579  **/
9580 void
9581 gtk_widget_set_direction (GtkWidget        *widget,
9582                           GtkTextDirection  dir)
9583 {
9584   GtkTextDirection old_dir;
9585
9586   g_return_if_fail (GTK_IS_WIDGET (widget));
9587   g_return_if_fail (dir >= GTK_TEXT_DIR_NONE && dir <= GTK_TEXT_DIR_RTL);
9588
9589   old_dir = gtk_widget_get_direction (widget);
9590
9591   widget->priv->direction = dir;
9592
9593   if (old_dir != gtk_widget_get_direction (widget))
9594     {
9595       GtkStyleContext *context;
9596
9597       context = g_object_get_qdata (G_OBJECT (widget), quark_style_context);
9598
9599       if (context)
9600         gtk_style_context_set_direction (context,
9601                                          gtk_widget_get_direction (widget));
9602
9603       gtk_widget_emit_direction_changed (widget, old_dir);
9604     }
9605 }
9606
9607 /**
9608  * gtk_widget_get_direction:
9609  * @widget: a #GtkWidget
9610  *
9611  * Gets the reading direction for a particular widget. See
9612  * gtk_widget_set_direction().
9613  *
9614  * Return value: the reading direction for the widget.
9615  **/
9616 GtkTextDirection
9617 gtk_widget_get_direction (GtkWidget *widget)
9618 {
9619   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_TEXT_DIR_LTR);
9620
9621   if (widget->priv->direction == GTK_TEXT_DIR_NONE)
9622     return gtk_default_direction;
9623   else
9624     return widget->priv->direction;
9625 }
9626
9627 static void
9628 gtk_widget_set_default_direction_recurse (GtkWidget *widget, gpointer data)
9629 {
9630   GtkTextDirection old_dir = GPOINTER_TO_UINT (data);
9631
9632   g_object_ref (widget);
9633
9634   if (widget->priv->direction == GTK_TEXT_DIR_NONE)
9635     gtk_widget_emit_direction_changed (widget, old_dir);
9636
9637   if (GTK_IS_CONTAINER (widget))
9638     gtk_container_forall (GTK_CONTAINER (widget),
9639                           gtk_widget_set_default_direction_recurse,
9640                           data);
9641
9642   g_object_unref (widget);
9643 }
9644
9645 /**
9646  * gtk_widget_set_default_direction:
9647  * @dir: the new default direction. This cannot be
9648  *        %GTK_TEXT_DIR_NONE.
9649  *
9650  * Sets the default reading direction for widgets where the
9651  * direction has not been explicitly set by gtk_widget_set_direction().
9652  **/
9653 void
9654 gtk_widget_set_default_direction (GtkTextDirection dir)
9655 {
9656   g_return_if_fail (dir == GTK_TEXT_DIR_RTL || dir == GTK_TEXT_DIR_LTR);
9657
9658   if (dir != gtk_default_direction)
9659     {
9660       GList *toplevels, *tmp_list;
9661       GtkTextDirection old_dir = gtk_default_direction;
9662
9663       gtk_default_direction = dir;
9664
9665       tmp_list = toplevels = gtk_window_list_toplevels ();
9666       g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
9667
9668       while (tmp_list)
9669         {
9670           gtk_widget_set_default_direction_recurse (tmp_list->data,
9671                                                     GUINT_TO_POINTER (old_dir));
9672           g_object_unref (tmp_list->data);
9673           tmp_list = tmp_list->next;
9674         }
9675
9676       g_list_free (toplevels);
9677     }
9678 }
9679
9680 /**
9681  * gtk_widget_get_default_direction:
9682  *
9683  * Obtains the current default reading direction. See
9684  * gtk_widget_set_default_direction().
9685  *
9686  * Return value: the current default direction.
9687  **/
9688 GtkTextDirection
9689 gtk_widget_get_default_direction (void)
9690 {
9691   return gtk_default_direction;
9692 }
9693
9694 static void
9695 gtk_widget_dispose (GObject *object)
9696 {
9697   GtkWidget *widget = GTK_WIDGET (object);
9698   GtkWidgetPrivate *priv = widget->priv;
9699
9700   if (priv->parent)
9701     gtk_container_remove (GTK_CONTAINER (priv->parent), widget);
9702   else if (gtk_widget_get_visible (widget))
9703     gtk_widget_hide (widget);
9704
9705   priv->visible = FALSE;
9706   if (gtk_widget_get_realized (widget))
9707     gtk_widget_unrealize (widget);
9708
9709   if (!priv->in_destruction)
9710     {
9711       priv->in_destruction = TRUE;
9712       g_signal_emit (object, widget_signals[DESTROY], 0);
9713       priv->in_destruction = FALSE;
9714     }
9715
9716   G_OBJECT_CLASS (gtk_widget_parent_class)->dispose (object);
9717 }
9718
9719 static void
9720 gtk_widget_real_destroy (GtkWidget *object)
9721 {
9722   /* gtk_object_destroy() will already hold a refcount on object */
9723   GtkWidget *widget = GTK_WIDGET (object);
9724   GtkWidgetPrivate *priv = widget->priv;
9725
9726   /* wipe accelerator closures (keep order) */
9727   g_object_set_qdata (G_OBJECT (widget), quark_accel_path, NULL);
9728   g_object_set_qdata (G_OBJECT (widget), quark_accel_closures, NULL);
9729
9730   /* Callers of add_mnemonic_label() should disconnect on ::destroy */
9731   g_object_set_qdata (G_OBJECT (widget), quark_mnemonic_labels, NULL);
9732
9733   gtk_grab_remove (widget);
9734
9735   g_object_unref (priv->style);
9736   priv->style = gtk_widget_get_default_style ();
9737   g_object_ref (priv->style);
9738 }
9739
9740 static void
9741 gtk_widget_finalize (GObject *object)
9742 {
9743   GtkWidget *widget = GTK_WIDGET (object);
9744   GtkWidgetPrivate *priv = widget->priv;
9745   GtkWidgetAuxInfo *aux_info;
9746   GtkAccessible *accessible;
9747
9748   gtk_grab_remove (widget);
9749
9750   g_object_unref (priv->style);
9751   priv->style = NULL;
9752
9753   g_free (priv->name);
9754
9755   aux_info =_gtk_widget_get_aux_info (widget, FALSE);
9756   if (aux_info)
9757     gtk_widget_aux_info_destroy (aux_info);
9758
9759   accessible = g_object_get_qdata (G_OBJECT (widget), quark_accessible_object);
9760   if (accessible)
9761     g_object_unref (accessible);
9762
9763   if (g_object_is_floating (object))
9764     g_warning ("A floating object was finalized. This means that someone\n"
9765                "called g_object_unref() on an object that had only a floating\n"
9766                "reference; the initial floating reference is not owned by anyone\n"
9767                "and must be removed with g_object_ref_sink().");
9768
9769   G_OBJECT_CLASS (gtk_widget_parent_class)->finalize (object);
9770 }
9771
9772 /*****************************************
9773  * gtk_widget_real_map:
9774  *
9775  *   arguments:
9776  *
9777  *   results:
9778  *****************************************/
9779
9780 static void
9781 gtk_widget_real_map (GtkWidget *widget)
9782 {
9783   GtkWidgetPrivate *priv = widget->priv;
9784
9785   g_assert (gtk_widget_get_realized (widget));
9786
9787   if (!gtk_widget_get_mapped (widget))
9788     {
9789       gtk_widget_set_mapped (widget, TRUE);
9790
9791       if (gtk_widget_get_has_window (widget))
9792         gdk_window_show (priv->window);
9793     }
9794 }
9795
9796 /*****************************************
9797  * gtk_widget_real_unmap:
9798  *
9799  *   arguments:
9800  *
9801  *   results:
9802  *****************************************/
9803
9804 static void
9805 gtk_widget_real_unmap (GtkWidget *widget)
9806 {
9807   GtkWidgetPrivate *priv = widget->priv;
9808
9809   if (gtk_widget_get_mapped (widget))
9810     {
9811       gtk_widget_set_mapped (widget, FALSE);
9812
9813       if (gtk_widget_get_has_window (widget))
9814         gdk_window_hide (priv->window);
9815     }
9816 }
9817
9818 /*****************************************
9819  * gtk_widget_real_realize:
9820  *
9821  *   arguments:
9822  *
9823  *   results:
9824  *****************************************/
9825
9826 static void
9827 gtk_widget_real_realize (GtkWidget *widget)
9828 {
9829   GtkWidgetPrivate *priv = widget->priv;
9830
9831   g_assert (!gtk_widget_get_has_window (widget));
9832
9833   gtk_widget_set_realized (widget, TRUE);
9834   if (priv->parent)
9835     {
9836       priv->window = gtk_widget_get_parent_window (widget);
9837       g_object_ref (priv->window);
9838     }
9839   priv->style = gtk_style_attach (priv->style, priv->window);
9840 }
9841
9842 /*****************************************
9843  * gtk_widget_real_unrealize:
9844  *
9845  *   arguments:
9846  *
9847  *   results:
9848  *****************************************/
9849
9850 static void
9851 gtk_widget_real_unrealize (GtkWidget *widget)
9852 {
9853   GtkWidgetPrivate *priv = widget->priv;
9854
9855   if (gtk_widget_get_mapped (widget))
9856     gtk_widget_real_unmap (widget);
9857
9858   gtk_widget_set_mapped (widget, FALSE);
9859
9860   /* printf ("unrealizing %s\n", g_type_name (G_TYPE_FROM_INSTANCE (widget)));
9861    */
9862
9863    /* We must do unrealize child widget BEFORE container widget.
9864     * gdk_window_destroy() destroys specified xwindow and its sub-xwindows.
9865     * So, unrealizing container widget bofore its children causes the problem
9866     * (for example, gdk_ic_destroy () with destroyed window causes crash. )
9867     */
9868
9869   if (GTK_IS_CONTAINER (widget))
9870     gtk_container_forall (GTK_CONTAINER (widget),
9871                           (GtkCallback) gtk_widget_unrealize,
9872                           NULL);
9873
9874   gtk_style_detach (priv->style);
9875   if (gtk_widget_get_has_window (widget))
9876     {
9877       gdk_window_set_user_data (priv->window, NULL);
9878       gdk_window_destroy (priv->window);
9879       priv->window = NULL;
9880     }
9881   else
9882     {
9883       g_object_unref (priv->window);
9884       priv->window = NULL;
9885     }
9886
9887   gtk_selection_remove_all (widget);
9888
9889   gtk_widget_set_realized (widget, FALSE);
9890 }
9891
9892 static void
9893 gtk_widget_real_adjust_size_request (GtkWidget         *widget,
9894                                      GtkOrientation     orientation,
9895                                      gint              *minimum_size,
9896                                      gint              *natural_size)
9897 {
9898   const GtkWidgetAuxInfo *aux_info;
9899
9900   aux_info =_gtk_widget_get_aux_info_or_defaults (widget);
9901
9902   if (orientation == GTK_ORIENTATION_HORIZONTAL &&
9903       aux_info->width > 0)
9904     {
9905       *minimum_size = MAX (*minimum_size, aux_info->width);
9906     }
9907   else if (orientation == GTK_ORIENTATION_VERTICAL &&
9908            aux_info->height > 0)
9909     {
9910       *minimum_size = MAX (*minimum_size, aux_info->height);
9911     }
9912
9913   /* Fix it if set_size_request made natural size smaller than min size.
9914    * This would also silently fix broken widgets, but we warn about them
9915    * in gtksizerequest.c when calling their size request vfuncs.
9916    */
9917   *natural_size = MAX (*natural_size, *minimum_size);
9918
9919   if (orientation == GTK_ORIENTATION_HORIZONTAL)
9920     {
9921       *minimum_size += (aux_info->margin.left + aux_info->margin.right);
9922       *natural_size += (aux_info->margin.left + aux_info->margin.right);
9923     }
9924   else
9925     {
9926       *minimum_size += (aux_info->margin.top + aux_info->margin.bottom);
9927       *natural_size += (aux_info->margin.top + aux_info->margin.bottom);
9928     }
9929 }
9930
9931 /**
9932  * _gtk_widget_peek_request_cache:
9933  *
9934  * Returns the address of the widget's request cache (strictly for
9935  * internal use in gtksizerequest.c)
9936  *
9937  * Return value: the address of @widget's size request cache.
9938  **/
9939 gpointer
9940 _gtk_widget_peek_request_cache (GtkWidget *widget)
9941 {
9942   /* Don't bother slowing things down with the return_if_fail guards here */
9943   return &widget->priv->requests;
9944 }
9945
9946 /*
9947  * _gtk_widget_set_device_window:
9948  * @widget: a #GtkWidget.
9949  * @device: a #GdkDevice.
9950  * @window: the new device window.
9951  *
9952  * Sets pointer window for @widget and @device.  Does not ref @window.
9953  * Actually stores it on the #GdkScreen, but you don't need to know that.
9954  */
9955 void
9956 _gtk_widget_set_device_window (GtkWidget *widget,
9957                                GdkDevice *device,
9958                                GdkWindow *window)
9959 {
9960   GtkWidgetPrivate *priv;
9961   GdkScreen *screen;
9962   GHashTable *device_window;
9963
9964   g_return_if_fail (GTK_IS_WIDGET (widget));
9965   g_return_if_fail (GDK_IS_DEVICE (device));
9966   g_return_if_fail (!window || GDK_IS_WINDOW (window));
9967
9968   priv = widget->priv;
9969
9970   if (!gtk_widget_get_realized (widget))
9971     return;
9972
9973   screen = gdk_window_get_screen (priv->window);
9974   device_window = g_object_get_qdata (G_OBJECT (screen), quark_pointer_window);
9975
9976   if (G_UNLIKELY (!device_window))
9977     {
9978       device_window = g_hash_table_new (NULL, NULL);
9979       g_object_set_qdata_full (G_OBJECT (screen),
9980                                quark_pointer_window,
9981                                device_window,
9982                                (GDestroyNotify) g_hash_table_destroy);
9983     }
9984
9985   if (window)
9986     g_hash_table_insert (device_window, device, window);
9987   else
9988     g_hash_table_remove (device_window, device);
9989 }
9990
9991 /*
9992  * _gtk_widget_get_device_window:
9993  * @widget: a #GtkWidget.
9994  * @device: a #GdkDevice.
9995  *
9996  * Return value: the device window set on the #GdkScreen @widget is attached
9997  * to, or %NULL.
9998  */
9999 GdkWindow *
10000 _gtk_widget_get_device_window (GtkWidget *widget,
10001                                GdkDevice *device)
10002 {
10003   GtkWidgetPrivate *priv;
10004   GdkScreen *screen;
10005   GHashTable *device_window;
10006   GdkWindow *window;
10007   GtkWidget *w;
10008
10009   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10010   g_return_val_if_fail (GDK_IS_DEVICE (device), NULL);
10011
10012   priv = widget->priv;
10013
10014   if (!gtk_widget_get_realized (widget))
10015     return NULL;
10016
10017   screen = gdk_window_get_screen (priv->window);
10018   device_window = g_object_get_qdata (G_OBJECT (screen), quark_pointer_window);
10019
10020   if (G_UNLIKELY (!device_window))
10021     return NULL;
10022
10023   window = g_hash_table_lookup (device_window, device);
10024
10025   if (!window)
10026     return NULL;
10027
10028   gdk_window_get_user_data (window, (gpointer *) &w);
10029
10030   if (widget != w)
10031     return NULL;
10032
10033   return window;
10034 }
10035
10036 /*
10037  * _gtk_widget_list_devices:
10038  * @widget: a #GtkWidget.
10039  *
10040  * Returns the list of #GdkDevices that is currently on top of any widget #GdkWindow.
10041  * Free the list with g_list_free(), the elements are owned by GTK+ and must not
10042  * be freed.
10043  */
10044 GList *
10045 _gtk_widget_list_devices (GtkWidget *widget)
10046 {
10047   GtkWidgetPrivate *priv;
10048   GdkScreen *screen;
10049   GHashTableIter iter;
10050   GHashTable *device_window;
10051   GList *devices = NULL;
10052   gpointer key, value;
10053
10054   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10055
10056   priv = widget->priv;
10057
10058   if (!gtk_widget_get_realized (widget))
10059     return NULL;
10060
10061   screen = gdk_window_get_screen (priv->window);
10062   device_window = g_object_get_qdata (G_OBJECT (screen), quark_pointer_window);
10063
10064   if (G_UNLIKELY (!device_window))
10065     return NULL;
10066
10067   g_hash_table_iter_init (&iter, device_window);
10068
10069   while (g_hash_table_iter_next (&iter, &key, &value))
10070     {
10071       GdkDevice *device = key;
10072       GdkWindow *window = value;
10073       GtkWidget *w;
10074
10075       if (window)
10076         {
10077           gdk_window_get_user_data (window, (gpointer *) &w);
10078
10079           if (widget == w)
10080             devices = g_list_prepend (devices, device);
10081         }
10082     }
10083
10084   return devices;
10085 }
10086
10087 static void
10088 synth_crossing (GtkWidget       *widget,
10089                 GdkEventType     type,
10090                 GdkWindow       *window,
10091                 GdkDevice       *device,
10092                 GdkCrossingMode  mode,
10093                 GdkNotifyType    detail)
10094 {
10095   GdkEvent *event;
10096
10097   event = gdk_event_new (type);
10098
10099   event->crossing.window = g_object_ref (window);
10100   event->crossing.send_event = TRUE;
10101   event->crossing.subwindow = g_object_ref (window);
10102   event->crossing.time = GDK_CURRENT_TIME;
10103   event->crossing.x = event->crossing.y = 0;
10104   event->crossing.x_root = event->crossing.y_root = 0;
10105   event->crossing.mode = mode;
10106   event->crossing.detail = detail;
10107   event->crossing.focus = FALSE;
10108   event->crossing.state = 0;
10109   gdk_event_set_device (event, device);
10110
10111   if (!widget)
10112     widget = gtk_get_event_widget (event);
10113
10114   if (widget)
10115     gtk_widget_event_internal (widget, event);
10116
10117   gdk_event_free (event);
10118 }
10119
10120 /*
10121  * _gtk_widget_synthesize_crossing:
10122  * @from: the #GtkWidget the virtual pointer is leaving.
10123  * @to: the #GtkWidget the virtual pointer is moving to.
10124  * @mode: the #GdkCrossingMode to place on the synthesized events.
10125  *
10126  * Generate crossing event(s) on widget state (sensitivity) or GTK+ grab change.
10127  *
10128  * The real pointer window is the window that most recently received an enter notify
10129  * event.  Windows that don't select for crossing events can't become the real
10130  * poiner window.  The real pointer widget that owns the real pointer window.  The
10131  * effective pointer window is the same as the real pointer window unless the real
10132  * pointer widget is either insensitive or there is a grab on a widget that is not
10133  * an ancestor of the real pointer widget (in which case the effective pointer
10134  * window should be the root window).
10135  *
10136  * When the effective pointer window is the same as the real poiner window, we
10137  * receive crossing events from the windowing system.  When the effective pointer
10138  * window changes to become different from the real pointer window we synthesize
10139  * crossing events, attempting to follow X protocol rules:
10140  *
10141  * When the root window becomes the effective pointer window:
10142  *   - leave notify on real pointer window, detail Ancestor
10143  *   - leave notify on all of its ancestors, detail Virtual
10144  *   - enter notify on root window, detail Inferior
10145  *
10146  * When the root window ceases to be the effective pointer window:
10147  *   - leave notify on root window, detail Inferior
10148  *   - enter notify on all ancestors of real pointer window, detail Virtual
10149  *   - enter notify on real pointer window, detail Ancestor
10150  */
10151 void
10152 _gtk_widget_synthesize_crossing (GtkWidget       *from,
10153                                  GtkWidget       *to,
10154                                  GdkDevice       *device,
10155                                  GdkCrossingMode  mode)
10156 {
10157   GdkWindow *from_window = NULL, *to_window = NULL;
10158
10159   g_return_if_fail (from != NULL || to != NULL);
10160
10161   if (from != NULL)
10162     {
10163       from_window = _gtk_widget_get_device_window (from, device);
10164
10165       if (!from_window)
10166         from_window = from->priv->window;
10167     }
10168
10169   if (to != NULL)
10170     {
10171       to_window = _gtk_widget_get_device_window (to, device);
10172
10173       if (!to_window)
10174         to_window = to->priv->window;
10175     }
10176
10177   if (from_window == NULL && to_window == NULL)
10178     ;
10179   else if (from_window != NULL && to_window == NULL)
10180     {
10181       GList *from_ancestors = NULL, *list;
10182       GdkWindow *from_ancestor = from_window;
10183
10184       while (from_ancestor != NULL)
10185         {
10186           from_ancestor = gdk_window_get_effective_parent (from_ancestor);
10187           if (from_ancestor == NULL)
10188             break;
10189           from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
10190         }
10191
10192       synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
10193                       device, mode, GDK_NOTIFY_ANCESTOR);
10194       for (list = g_list_last (from_ancestors); list; list = list->prev)
10195         {
10196           synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
10197                           device, mode, GDK_NOTIFY_VIRTUAL);
10198         }
10199
10200       /* XXX: enter/inferior on root window? */
10201
10202       g_list_free (from_ancestors);
10203     }
10204   else if (from_window == NULL && to_window != NULL)
10205     {
10206       GList *to_ancestors = NULL, *list;
10207       GdkWindow *to_ancestor = to_window;
10208
10209       while (to_ancestor != NULL)
10210         {
10211           to_ancestor = gdk_window_get_effective_parent (to_ancestor);
10212           if (to_ancestor == NULL)
10213             break;
10214           to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
10215         }
10216
10217       /* XXX: leave/inferior on root window? */
10218
10219       for (list = to_ancestors; list; list = list->next)
10220         {
10221           synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
10222                           device, mode, GDK_NOTIFY_VIRTUAL);
10223         }
10224       synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
10225                       device, mode, GDK_NOTIFY_ANCESTOR);
10226
10227       g_list_free (to_ancestors);
10228     }
10229   else if (from_window == to_window)
10230     ;
10231   else
10232     {
10233       GList *from_ancestors = NULL, *to_ancestors = NULL, *list;
10234       GdkWindow *from_ancestor = from_window, *to_ancestor = to_window;
10235
10236       while (from_ancestor != NULL || to_ancestor != NULL)
10237         {
10238           if (from_ancestor != NULL)
10239             {
10240               from_ancestor = gdk_window_get_effective_parent (from_ancestor);
10241               if (from_ancestor == to_window)
10242                 break;
10243               if (from_ancestor)
10244                 from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
10245             }
10246           if (to_ancestor != NULL)
10247             {
10248               to_ancestor = gdk_window_get_effective_parent (to_ancestor);
10249               if (to_ancestor == from_window)
10250                 break;
10251               if (to_ancestor)
10252                 to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
10253             }
10254         }
10255       if (to_ancestor == from_window)
10256         {
10257           if (mode != GDK_CROSSING_GTK_UNGRAB)
10258             synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
10259                             device, mode, GDK_NOTIFY_INFERIOR);
10260           for (list = to_ancestors; list; list = list->next)
10261             synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
10262                             device, mode, GDK_NOTIFY_VIRTUAL);
10263           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
10264                           device, mode, GDK_NOTIFY_ANCESTOR);
10265         }
10266       else if (from_ancestor == to_window)
10267         {
10268           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
10269                           device, mode, GDK_NOTIFY_ANCESTOR);
10270           for (list = g_list_last (from_ancestors); list; list = list->prev)
10271             {
10272               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
10273                               device, mode, GDK_NOTIFY_VIRTUAL);
10274             }
10275           if (mode != GDK_CROSSING_GTK_GRAB)
10276             synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
10277                             device, mode, GDK_NOTIFY_INFERIOR);
10278         }
10279       else
10280         {
10281           while (from_ancestors != NULL && to_ancestors != NULL
10282                  && from_ancestors->data == to_ancestors->data)
10283             {
10284               from_ancestors = g_list_delete_link (from_ancestors,
10285                                                    from_ancestors);
10286               to_ancestors = g_list_delete_link (to_ancestors, to_ancestors);
10287             }
10288
10289           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
10290                           device, mode, GDK_NOTIFY_NONLINEAR);
10291
10292           for (list = g_list_last (from_ancestors); list; list = list->prev)
10293             {
10294               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
10295                               device, mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
10296             }
10297           for (list = to_ancestors; list; list = list->next)
10298             {
10299               synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
10300                               device, mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
10301             }
10302           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
10303                           device, mode, GDK_NOTIFY_NONLINEAR);
10304         }
10305       g_list_free (from_ancestors);
10306       g_list_free (to_ancestors);
10307     }
10308 }
10309
10310 static void
10311 gtk_widget_propagate_state (GtkWidget           *widget,
10312                             GtkStateData        *data)
10313 {
10314   GtkWidgetPrivate *priv = widget->priv;
10315   guint8 old_state = gtk_widget_get_state (widget);
10316   guint8 old_saved_state = priv->saved_state;
10317
10318   /* don't call this function with state==GTK_STATE_INSENSITIVE,
10319    * parent_sensitive==TRUE on a sensitive widget
10320    */
10321
10322
10323   priv->parent_sensitive = data->parent_sensitive;
10324
10325   if (gtk_widget_is_sensitive (widget))
10326     {
10327       if (data->state_restoration)
10328         priv->state = priv->saved_state;
10329       else
10330         priv->state = data->state;
10331     }
10332   else
10333     {
10334       if (!data->state_restoration)
10335         {
10336           if (data->state != GTK_STATE_INSENSITIVE)
10337             priv->saved_state = data->state;
10338         }
10339       else if (gtk_widget_get_state (widget) != GTK_STATE_INSENSITIVE)
10340         priv->saved_state = gtk_widget_get_state (widget);
10341       priv->state = GTK_STATE_INSENSITIVE;
10342     }
10343
10344   if (gtk_widget_is_focus (widget) && !gtk_widget_is_sensitive (widget))
10345     {
10346       GtkWidget *window;
10347
10348       window = gtk_widget_get_toplevel (widget);
10349       if (window && gtk_widget_is_toplevel (window))
10350         gtk_window_set_focus (GTK_WINDOW (window), NULL);
10351     }
10352
10353   if (old_state != gtk_widget_get_state (widget) ||
10354       old_saved_state != priv->saved_state)
10355     {
10356       g_object_ref (widget);
10357
10358       if (!gtk_widget_is_sensitive (widget) && gtk_widget_has_grab (widget))
10359         gtk_grab_remove (widget);
10360
10361       g_signal_emit (widget, widget_signals[STATE_CHANGED], 0, old_state);
10362
10363       if (!priv->shadowed)
10364         {
10365           GList *event_windows = NULL;
10366           GList *devices, *d;
10367
10368           devices = _gtk_widget_list_devices (widget);
10369
10370           for (d = devices; d; d = d->next)
10371             {
10372               GdkWindow *window;
10373               GdkDevice *device;
10374
10375               device = d->data;
10376               window = _gtk_widget_get_device_window (widget, device);
10377
10378               /* Do not propagate more than once to the
10379                * same window if non-multidevice aware.
10380                */
10381               if (!gdk_window_get_support_multidevice (window) &&
10382                   g_list_find (event_windows, window))
10383                 continue;
10384
10385               if (!gtk_widget_is_sensitive (widget))
10386                 _gtk_widget_synthesize_crossing (widget, NULL, d->data,
10387                                                  GDK_CROSSING_STATE_CHANGED);
10388               else if (old_state == GTK_STATE_INSENSITIVE)
10389                 _gtk_widget_synthesize_crossing (NULL, widget, d->data,
10390                                                  GDK_CROSSING_STATE_CHANGED);
10391
10392               event_windows = g_list_prepend (event_windows, window);
10393             }
10394
10395           g_list_free (event_windows);
10396           g_list_free (devices);
10397         }
10398
10399       if (GTK_IS_CONTAINER (widget))
10400         {
10401           data->parent_sensitive = (gtk_widget_is_sensitive (widget) != FALSE);
10402           if (data->use_forall)
10403             gtk_container_forall (GTK_CONTAINER (widget),
10404                                   (GtkCallback) gtk_widget_propagate_state,
10405                                   data);
10406           else
10407             gtk_container_foreach (GTK_CONTAINER (widget),
10408                                    (GtkCallback) gtk_widget_propagate_state,
10409                                    data);
10410         }
10411       g_object_unref (widget);
10412     }
10413 }
10414
10415 static const GtkWidgetAuxInfo default_aux_info = {
10416   -1, -1,
10417   GTK_ALIGN_FILL,
10418   GTK_ALIGN_FILL,
10419   { 0, 0, 0, 0 }
10420 };
10421
10422 /*
10423  * _gtk_widget_get_aux_info:
10424  * @widget: a #GtkWidget
10425  * @create: if %TRUE, create the structure if it doesn't exist
10426  *
10427  * Get the #GtkWidgetAuxInfo structure for the widget.
10428  *
10429  * Return value: the #GtkAuxInfo structure for the widget, or
10430  *    %NULL if @create is %FALSE and one doesn't already exist.
10431  */
10432 GtkWidgetAuxInfo*
10433 _gtk_widget_get_aux_info (GtkWidget *widget,
10434                           gboolean   create)
10435 {
10436   GtkWidgetAuxInfo *aux_info;
10437
10438   aux_info = g_object_get_qdata (G_OBJECT (widget), quark_aux_info);
10439   if (!aux_info && create)
10440     {
10441       aux_info = g_slice_new0 (GtkWidgetAuxInfo);
10442
10443       *aux_info = default_aux_info;
10444
10445       g_object_set_qdata (G_OBJECT (widget), quark_aux_info, aux_info);
10446     }
10447
10448   return aux_info;
10449 }
10450
10451 static const GtkWidgetAuxInfo*
10452 _gtk_widget_get_aux_info_or_defaults (GtkWidget *widget)
10453 {
10454   GtkWidgetAuxInfo *aux_info;
10455
10456   aux_info = _gtk_widget_get_aux_info (widget, FALSE);
10457   if (aux_info == NULL)
10458     {
10459       return &default_aux_info;
10460     }
10461   else
10462     {
10463       return aux_info;
10464     }
10465 }
10466
10467 /*****************************************
10468  * gtk_widget_aux_info_destroy:
10469  *
10470  *   arguments:
10471  *
10472  *   results:
10473  *****************************************/
10474
10475 static void
10476 gtk_widget_aux_info_destroy (GtkWidgetAuxInfo *aux_info)
10477 {
10478   g_slice_free (GtkWidgetAuxInfo, aux_info);
10479 }
10480
10481 /**
10482  * gtk_widget_shape_combine_region:
10483  * @widget: a #GtkWidget
10484  * @region: (allow-none): shape to be added, or %NULL to remove an existing shape
10485  *
10486  * Sets a shape for this widget's GDK window. This allows for
10487  * transparent windows etc., see gdk_window_shape_combine_region()
10488  * for more information.
10489  *
10490  * Since: 3.0
10491  **/
10492 void
10493 gtk_widget_shape_combine_region (GtkWidget *widget,
10494                                  cairo_region_t *region)
10495 {
10496   GtkWidgetPrivate *priv;
10497
10498   g_return_if_fail (GTK_IS_WIDGET (widget));
10499   /*  set_shape doesn't work on widgets without gdk window */
10500   g_return_if_fail (gtk_widget_get_has_window (widget));
10501
10502   priv = widget->priv;
10503
10504   if (region == NULL)
10505     {
10506       priv->has_shape_mask = FALSE;
10507
10508       if (priv->window)
10509         gdk_window_shape_combine_region (priv->window, NULL, 0, 0);
10510
10511       g_object_set_qdata (G_OBJECT (widget), quark_shape_info, NULL);
10512     }
10513   else
10514     {
10515       priv->has_shape_mask = TRUE;
10516
10517       g_object_set_qdata_full (G_OBJECT (widget), quark_shape_info,
10518                                cairo_region_copy (region),
10519                                (GDestroyNotify) cairo_region_destroy);
10520
10521       /* set shape if widget has a gdk window already.
10522        * otherwise the shape is scheduled to be set by gtk_widget_realize().
10523        */
10524       if (priv->window)
10525         gdk_window_shape_combine_region (priv->window, region, 0, 0);
10526     }
10527 }
10528
10529 /**
10530  * gtk_widget_input_shape_combine_region:
10531  * @widget: a #GtkWidget
10532  * @region: (allow-none): shape to be added, or %NULL to remove an existing shape
10533  *
10534  * Sets an input shape for this widget's GDK window. This allows for
10535  * windows which react to mouse click in a nonrectangular region, see
10536  * gdk_window_input_shape_combine_region() for more information.
10537  *
10538  * Since: 3.0
10539  **/
10540 void
10541 gtk_widget_input_shape_combine_region (GtkWidget *widget,
10542                                        cairo_region_t *region)
10543 {
10544   GtkWidgetPrivate *priv;
10545
10546   g_return_if_fail (GTK_IS_WIDGET (widget));
10547   /*  set_shape doesn't work on widgets without gdk window */
10548   g_return_if_fail (gtk_widget_get_has_window (widget));
10549
10550   priv = widget->priv;
10551
10552   if (region == NULL)
10553     {
10554       if (priv->window)
10555         gdk_window_input_shape_combine_region (priv->window, NULL, 0, 0);
10556
10557       g_object_set_qdata (G_OBJECT (widget), quark_input_shape_info, NULL);
10558     }
10559   else
10560     {
10561       g_object_set_qdata_full (G_OBJECT (widget), quark_input_shape_info,
10562                                cairo_region_copy (region),
10563                                (GDestroyNotify) cairo_region_destroy);
10564
10565       /* set shape if widget has a gdk window already.
10566        * otherwise the shape is scheduled to be set by gtk_widget_realize().
10567        */
10568       if (priv->window)
10569         gdk_window_input_shape_combine_region (priv->window, region, 0, 0);
10570     }
10571 }
10572
10573
10574 static void
10575 gtk_reset_shapes_recurse (GtkWidget *widget,
10576                           GdkWindow *window)
10577 {
10578   gpointer data;
10579   GList *list;
10580
10581   gdk_window_get_user_data (window, &data);
10582   if (data != widget)
10583     return;
10584
10585   gdk_window_shape_combine_region (window, NULL, 0, 0);
10586   for (list = gdk_window_peek_children (window); list; list = list->next)
10587     gtk_reset_shapes_recurse (widget, list->data);
10588 }
10589
10590 /**
10591  * gtk_widget_reset_shapes:
10592  * @widget: a #GtkWidget
10593  *
10594  * Recursively resets the shape on this widget and its descendants.
10595  **/
10596 void
10597 gtk_widget_reset_shapes (GtkWidget *widget)
10598 {
10599   GtkWidgetPrivate *priv;
10600
10601   g_return_if_fail (GTK_IS_WIDGET (widget));
10602   g_return_if_fail (gtk_widget_get_realized (widget));
10603
10604   priv = widget->priv;
10605
10606   if (!priv->has_shape_mask)
10607     gtk_reset_shapes_recurse (widget, priv->window);
10608 }
10609
10610 /* style properties
10611  */
10612
10613 /**
10614  * gtk_widget_class_install_style_property_parser:
10615  * @klass: a #GtkWidgetClass
10616  * @pspec: the #GParamSpec for the style property
10617  * @parser: the parser for the style property
10618  *
10619  * Installs a style property on a widget class.
10620  **/
10621 void
10622 gtk_widget_class_install_style_property_parser (GtkWidgetClass     *klass,
10623                                                 GParamSpec         *pspec,
10624                                                 GtkRcPropertyParser parser)
10625 {
10626   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
10627   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
10628   g_return_if_fail (pspec->flags & G_PARAM_READABLE);
10629   g_return_if_fail (!(pspec->flags & (G_PARAM_CONSTRUCT_ONLY | G_PARAM_CONSTRUCT)));
10630
10631   if (g_param_spec_pool_lookup (style_property_spec_pool, pspec->name, G_OBJECT_CLASS_TYPE (klass), FALSE))
10632     {
10633       g_warning (G_STRLOC ": class `%s' already contains a style property named `%s'",
10634                  G_OBJECT_CLASS_NAME (klass),
10635                  pspec->name);
10636       return;
10637     }
10638
10639   g_param_spec_ref_sink (pspec);
10640   g_param_spec_set_qdata (pspec, quark_property_parser, (gpointer) parser);
10641   g_param_spec_pool_insert (style_property_spec_pool, pspec, G_OBJECT_CLASS_TYPE (klass));
10642 }
10643
10644 /**
10645  * gtk_widget_class_install_style_property:
10646  * @klass: a #GtkWidgetClass
10647  * @pspec: the #GParamSpec for the property
10648  *
10649  * Installs a style property on a widget class. The parser for the
10650  * style property is determined by the value type of @pspec.
10651  **/
10652 void
10653 gtk_widget_class_install_style_property (GtkWidgetClass *klass,
10654                                          GParamSpec     *pspec)
10655 {
10656   GtkRcPropertyParser parser;
10657
10658   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
10659   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
10660
10661   parser = _gtk_rc_property_parser_from_type (G_PARAM_SPEC_VALUE_TYPE (pspec));
10662
10663   gtk_widget_class_install_style_property_parser (klass, pspec, parser);
10664 }
10665
10666 /**
10667  * gtk_widget_class_find_style_property:
10668  * @klass: a #GtkWidgetClass
10669  * @property_name: the name of the style property to find
10670  * @returns: (allow-none): the #GParamSpec of the style property or %NULL if @class has no
10671  *   style property with that name.
10672  *
10673  * Finds a style property of a widget class by name.
10674  *
10675  * Since: 2.2
10676  */
10677 GParamSpec*
10678 gtk_widget_class_find_style_property (GtkWidgetClass *klass,
10679                                       const gchar    *property_name)
10680 {
10681   g_return_val_if_fail (property_name != NULL, NULL);
10682
10683   return g_param_spec_pool_lookup (style_property_spec_pool,
10684                                    property_name,
10685                                    G_OBJECT_CLASS_TYPE (klass),
10686                                    TRUE);
10687 }
10688
10689 /**
10690  * gtk_widget_class_list_style_properties:
10691  * @klass: a #GtkWidgetClass
10692  * @n_properties: location to return the number of style properties found
10693  * @returns: an newly allocated array of #GParamSpec*. The array must
10694  *       be freed with g_free().
10695  *
10696  * Returns all style properties of a widget class.
10697  *
10698  * Since: 2.2
10699  */
10700 GParamSpec**
10701 gtk_widget_class_list_style_properties (GtkWidgetClass *klass,
10702                                         guint          *n_properties)
10703 {
10704   GParamSpec **pspecs;
10705   guint n;
10706
10707   pspecs = g_param_spec_pool_list (style_property_spec_pool,
10708                                    G_OBJECT_CLASS_TYPE (klass),
10709                                    &n);
10710   if (n_properties)
10711     *n_properties = n;
10712
10713   return pspecs;
10714 }
10715
10716 /**
10717  * gtk_widget_style_get_property:
10718  * @widget: a #GtkWidget
10719  * @property_name: the name of a style property
10720  * @value: location to return the property value
10721  *
10722  * Gets the value of a style property of @widget.
10723  */
10724 void
10725 gtk_widget_style_get_property (GtkWidget   *widget,
10726                                const gchar *property_name,
10727                                GValue      *value)
10728 {
10729   GParamSpec *pspec;
10730
10731   g_return_if_fail (GTK_IS_WIDGET (widget));
10732   g_return_if_fail (property_name != NULL);
10733   g_return_if_fail (G_IS_VALUE (value));
10734
10735   g_object_ref (widget);
10736   pspec = g_param_spec_pool_lookup (style_property_spec_pool,
10737                                     property_name,
10738                                     G_OBJECT_TYPE (widget),
10739                                     TRUE);
10740   if (!pspec)
10741     g_warning ("%s: widget class `%s' has no property named `%s'",
10742                G_STRLOC,
10743                G_OBJECT_TYPE_NAME (widget),
10744                property_name);
10745   else
10746     {
10747       GtkStyleContext *context;
10748       const GValue *peek_value;
10749
10750       context = gtk_widget_get_style_context (widget);
10751       peek_value = _gtk_style_context_peek_style_property (context,
10752                                                            G_OBJECT_TYPE (widget),
10753                                                            pspec);
10754
10755       /* auto-conversion of the caller's value type
10756        */
10757       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
10758         g_value_copy (peek_value, value);
10759       else if (g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
10760         g_value_transform (peek_value, value);
10761       else
10762         g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
10763                    pspec->name,
10764                    g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
10765                    G_VALUE_TYPE_NAME (value));
10766     }
10767   g_object_unref (widget);
10768 }
10769
10770 /**
10771  * gtk_widget_style_get_valist:
10772  * @widget: a #GtkWidget
10773  * @first_property_name: the name of the first property to get
10774  * @var_args: a <type>va_list</type> of pairs of property names and
10775  *     locations to return the property values, starting with the location
10776  *     for @first_property_name.
10777  *
10778  * Non-vararg variant of gtk_widget_style_get(). Used primarily by language
10779  * bindings.
10780  */
10781 void
10782 gtk_widget_style_get_valist (GtkWidget   *widget,
10783                              const gchar *first_property_name,
10784                              va_list      var_args)
10785 {
10786   GtkStyleContext *context;
10787   const gchar *name;
10788
10789   g_return_if_fail (GTK_IS_WIDGET (widget));
10790
10791   g_object_ref (widget);
10792   context = gtk_widget_get_style_context (widget);
10793
10794   name = first_property_name;
10795   while (name)
10796     {
10797       const GValue *peek_value;
10798       GParamSpec *pspec;
10799       gchar *error;
10800
10801       pspec = g_param_spec_pool_lookup (style_property_spec_pool,
10802                                         name,
10803                                         G_OBJECT_TYPE (widget),
10804                                         TRUE);
10805       if (!pspec)
10806         {
10807           g_warning ("%s: widget class `%s' has no property named `%s'",
10808                      G_STRLOC,
10809                      G_OBJECT_TYPE_NAME (widget),
10810                      name);
10811           break;
10812         }
10813       /* style pspecs are always readable so we can spare that check here */
10814
10815       peek_value = _gtk_style_context_peek_style_property (context,
10816                                                            G_OBJECT_TYPE (widget),
10817                                                            pspec);
10818
10819       G_VALUE_LCOPY (peek_value, var_args, 0, &error);
10820       if (error)
10821         {
10822           g_warning ("%s: %s", G_STRLOC, error);
10823           g_free (error);
10824           break;
10825         }
10826
10827       name = va_arg (var_args, gchar*);
10828     }
10829
10830   g_object_unref (widget);
10831 }
10832
10833 /**
10834  * gtk_widget_style_get:
10835  * @widget: a #GtkWidget
10836  * @first_property_name: the name of the first property to get
10837  * @Varargs: pairs of property names and locations to
10838  *   return the property values, starting with the location for
10839  *   @first_property_name, terminated by %NULL.
10840  *
10841  * Gets the values of a multiple style properties of @widget.
10842  */
10843 void
10844 gtk_widget_style_get (GtkWidget   *widget,
10845                       const gchar *first_property_name,
10846                       ...)
10847 {
10848   va_list var_args;
10849
10850   g_return_if_fail (GTK_IS_WIDGET (widget));
10851
10852   va_start (var_args, first_property_name);
10853   gtk_widget_style_get_valist (widget, first_property_name, var_args);
10854   va_end (var_args);
10855 }
10856
10857 /**
10858  * gtk_widget_path:
10859  * @widget: a #GtkWidget
10860  * @path_length: (out) (allow-none): location to store length of the path, or %NULL
10861  * @path: (out) (allow-none):  location to store allocated path string, or %NULL
10862  * @path_reversed: (out) (allow-none):  location to store allocated reverse path string, or %NULL
10863  *
10864  * Obtains the full path to @widget. The path is simply the name of a
10865  * widget and all its parents in the container hierarchy, separated by
10866  * periods. The name of a widget comes from
10867  * gtk_widget_get_name(). Paths are used to apply styles to a widget
10868  * in gtkrc configuration files. Widget names are the type of the
10869  * widget by default (e.g. "GtkButton") or can be set to an
10870  * application-specific value with gtk_widget_set_name(). By setting
10871  * the name of a widget, you allow users or theme authors to apply
10872  * styles to that specific widget in their gtkrc
10873  * file. @path_reversed_p fills in the path in reverse order,
10874  * i.e. starting with @widget's name instead of starting with the name
10875  * of @widget's outermost ancestor.
10876  **/
10877 void
10878 gtk_widget_path (GtkWidget *widget,
10879                  guint     *path_length,
10880                  gchar    **path,
10881                  gchar    **path_reversed)
10882 {
10883   static gchar *rev_path = NULL;
10884   static guint tmp_path_len = 0;
10885   guint len;
10886
10887   g_return_if_fail (GTK_IS_WIDGET (widget));
10888
10889   len = 0;
10890   do
10891     {
10892       const gchar *string;
10893       const gchar *s;
10894       gchar *d;
10895       guint l;
10896
10897       string = gtk_widget_get_name (widget);
10898       l = strlen (string);
10899       while (tmp_path_len <= len + l + 1)
10900         {
10901           tmp_path_len += INIT_PATH_SIZE;
10902           rev_path = g_realloc (rev_path, tmp_path_len);
10903         }
10904       s = string + l - 1;
10905       d = rev_path + len;
10906       while (s >= string)
10907         *(d++) = *(s--);
10908       len += l;
10909
10910       widget = widget->priv->parent;
10911
10912       if (widget)
10913         rev_path[len++] = '.';
10914       else
10915         rev_path[len++] = 0;
10916     }
10917   while (widget);
10918
10919   if (path_length)
10920     *path_length = len - 1;
10921   if (path_reversed)
10922     *path_reversed = g_strdup (rev_path);
10923   if (path)
10924     {
10925       *path = g_strdup (rev_path);
10926       g_strreverse (*path);
10927     }
10928 }
10929
10930 /**
10931  * gtk_widget_class_path:
10932  * @widget: a #GtkWidget
10933  * @path_length: (out) (allow-none): location to store the length of the class path, or %NULL
10934  * @path: (out) (allow-none): location to store the class path as an allocated string, or %NULL
10935  * @path_reversed: (out) (allow-none): location to store the reverse class path as an allocated
10936  *    string, or %NULL
10937  *
10938  * Same as gtk_widget_path(), but always uses the name of a widget's type,
10939  * never uses a custom name set with gtk_widget_set_name().
10940  *
10941  **/
10942 void
10943 gtk_widget_class_path (GtkWidget *widget,
10944                        guint     *path_length,
10945                        gchar    **path,
10946                        gchar    **path_reversed)
10947 {
10948   static gchar *rev_path = NULL;
10949   static guint tmp_path_len = 0;
10950   guint len;
10951
10952   g_return_if_fail (GTK_IS_WIDGET (widget));
10953
10954   len = 0;
10955   do
10956     {
10957       const gchar *string;
10958       const gchar *s;
10959       gchar *d;
10960       guint l;
10961
10962       string = g_type_name (G_OBJECT_TYPE (widget));
10963       l = strlen (string);
10964       while (tmp_path_len <= len + l + 1)
10965         {
10966           tmp_path_len += INIT_PATH_SIZE;
10967           rev_path = g_realloc (rev_path, tmp_path_len);
10968         }
10969       s = string + l - 1;
10970       d = rev_path + len;
10971       while (s >= string)
10972         *(d++) = *(s--);
10973       len += l;
10974
10975       widget = widget->priv->parent;
10976
10977       if (widget)
10978         rev_path[len++] = '.';
10979       else
10980         rev_path[len++] = 0;
10981     }
10982   while (widget);
10983
10984   if (path_length)
10985     *path_length = len - 1;
10986   if (path_reversed)
10987     *path_reversed = g_strdup (rev_path);
10988   if (path)
10989     {
10990       *path = g_strdup (rev_path);
10991       g_strreverse (*path);
10992     }
10993 }
10994
10995 /**
10996  * gtk_requisition_new:
10997  *
10998  * Allocates a new #GtkRequisition structure and initializes its elements to zero.
10999  *
11000  * Returns: a new empty #GtkRequisition. The newly allocated #GtkRequisition should
11001  *   be freed with gtk_requisition_free().
11002  *
11003  * Since: 3.0
11004  */
11005 GtkRequisition *
11006 gtk_requisition_new (void)
11007 {
11008   return g_slice_new0 (GtkRequisition);
11009 }
11010
11011 /**
11012  * gtk_requisition_copy:
11013  * @requisition: a #GtkRequisition
11014  *
11015  * Copies a #GtkRequisition.
11016  *
11017  * Returns: a copy of @requisition
11018  **/
11019 GtkRequisition *
11020 gtk_requisition_copy (const GtkRequisition *requisition)
11021 {
11022   return g_slice_dup (GtkRequisition, requisition);
11023 }
11024
11025 /**
11026  * gtk_requisition_free:
11027  * @requisition: a #GtkRequisition
11028  *
11029  * Frees a #GtkRequisition.
11030  **/
11031 void
11032 gtk_requisition_free (GtkRequisition *requisition)
11033 {
11034   g_slice_free (GtkRequisition, requisition);
11035 }
11036
11037 G_DEFINE_BOXED_TYPE (GtkRequisition, gtk_requisition,
11038                      gtk_requisition_copy,
11039                      gtk_requisition_free)
11040
11041 /**
11042  * gtk_widget_get_accessible:
11043  * @widget: a #GtkWidget
11044  *
11045  * Returns the accessible object that describes the widget to an
11046  * assistive technology.
11047  *
11048  * If no accessibility library is loaded (i.e. no ATK implementation library is
11049  * loaded via <envar>GTK_MODULES</envar> or via another application library,
11050  * such as libgnome), then this #AtkObject instance may be a no-op. Likewise,
11051  * if no class-specific #AtkObject implementation is available for the widget
11052  * instance in question, it will inherit an #AtkObject implementation from the
11053  * first ancestor class for which such an implementation is defined.
11054  *
11055  * The documentation of the
11056  * <ulink url="http://library.gnome.org/devel/atk/stable/">ATK</ulink>
11057  * library contains more information about accessible objects and their uses.
11058  *
11059  * Returns: (transfer none): the #AtkObject associated with @widget
11060  */
11061 AtkObject*
11062 gtk_widget_get_accessible (GtkWidget *widget)
11063 {
11064   GtkWidgetClass *klass;
11065
11066   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
11067
11068   klass = GTK_WIDGET_GET_CLASS (widget);
11069
11070   g_return_val_if_fail (klass->get_accessible != NULL, NULL);
11071
11072   return klass->get_accessible (widget);
11073 }
11074
11075 static AtkObject*
11076 gtk_widget_real_get_accessible (GtkWidget *widget)
11077 {
11078   AtkObject* accessible;
11079
11080   accessible = g_object_get_qdata (G_OBJECT (widget),
11081                                    quark_accessible_object);
11082   if (!accessible)
11083   {
11084     AtkObjectFactory *factory;
11085     AtkRegistry *default_registry;
11086
11087     default_registry = atk_get_default_registry ();
11088     factory = atk_registry_get_factory (default_registry,
11089                                         G_TYPE_FROM_INSTANCE (widget));
11090     accessible =
11091       atk_object_factory_create_accessible (factory,
11092                                             G_OBJECT (widget));
11093     g_object_set_qdata (G_OBJECT (widget),
11094                         quark_accessible_object,
11095                         accessible);
11096   }
11097   return accessible;
11098 }
11099
11100 /*
11101  * Initialize a AtkImplementorIface instance's virtual pointers as
11102  * appropriate to this implementor's class (GtkWidget).
11103  */
11104 static void
11105 gtk_widget_accessible_interface_init (AtkImplementorIface *iface)
11106 {
11107   iface->ref_accessible = gtk_widget_ref_accessible;
11108 }
11109
11110 static AtkObject*
11111 gtk_widget_ref_accessible (AtkImplementor *implementor)
11112 {
11113   AtkObject *accessible;
11114
11115   accessible = gtk_widget_get_accessible (GTK_WIDGET (implementor));
11116   if (accessible)
11117     g_object_ref (accessible);
11118   return accessible;
11119 }
11120
11121 /*
11122  * Expand flag management
11123  */
11124
11125 static void
11126 gtk_widget_update_computed_expand (GtkWidget *widget)
11127 {
11128   GtkWidgetPrivate *priv;
11129
11130   priv = widget->priv;
11131
11132   if (priv->need_compute_expand)
11133     {
11134       gboolean h, v;
11135
11136       if (priv->hexpand_set)
11137         h = priv->hexpand;
11138       else
11139         h = FALSE;
11140
11141       if (priv->vexpand_set)
11142         v = priv->vexpand;
11143       else
11144         v = FALSE;
11145
11146       /* we don't need to use compute_expand if both expands are
11147        * forced by the app
11148        */
11149       if (!(priv->hexpand_set && priv->vexpand_set))
11150         {
11151           if (GTK_WIDGET_GET_CLASS (widget)->compute_expand != NULL)
11152             {
11153               gboolean ignored;
11154
11155               GTK_WIDGET_GET_CLASS (widget)->compute_expand (widget,
11156                                                              priv->hexpand_set ? &ignored : &h,
11157                                                              priv->vexpand_set ? &ignored : &v);
11158             }
11159         }
11160
11161       priv->need_compute_expand = FALSE;
11162       priv->computed_hexpand = h != FALSE;
11163       priv->computed_vexpand = v != FALSE;
11164     }
11165 }
11166
11167 /**
11168  * gtk_widget_queue_compute_expand:
11169  * @widget: a #GtkWidget
11170  *
11171  * Mark @widget as needing to recompute its expand flags. Call
11172  * this function when setting legacy expand child properties
11173  * on the child of a container.
11174  *
11175  * See gtk_widget_compute_expand().
11176  */
11177 void
11178 gtk_widget_queue_compute_expand (GtkWidget *widget)
11179 {
11180   GtkWidget *parent;
11181   gboolean changed_anything;
11182
11183   if (widget->priv->need_compute_expand)
11184     return;
11185
11186   changed_anything = FALSE;
11187   parent = widget;
11188   while (parent != NULL)
11189     {
11190       if (!parent->priv->need_compute_expand)
11191         {
11192           parent->priv->need_compute_expand = TRUE;
11193           changed_anything = TRUE;
11194         }
11195
11196       /* Note: if we had an invariant that "if a child needs to
11197        * compute expand, its parents also do" then we could stop going
11198        * up when we got to a parent that already needed to
11199        * compute. However, in general we compute expand lazily (as
11200        * soon as we see something in a subtree that is expand, we know
11201        * we're expanding) and so this invariant does not hold and we
11202        * have to always walk all the way up in case some ancestor
11203        * is not currently need_compute_expand.
11204        */
11205
11206       parent = parent->priv->parent;
11207     }
11208
11209   /* recomputing expand always requires
11210    * a relayout as well
11211    */
11212   if (changed_anything)
11213     gtk_widget_queue_resize (widget);
11214 }
11215
11216 /**
11217  * gtk_widget_compute_expand:
11218  * @widget: the widget
11219  * @orientation: expand direction
11220  *
11221  * Computes whether a container should give this widget extra space
11222  * when possible. Containers should check this, rather than
11223  * looking at gtk_widget_get_hexpand() or gtk_widget_get_vexpand().
11224  *
11225  * This function already checks whether the widget is visible, so
11226  * visibility does not need to be checked separately. Non-visible
11227  * widgets are not expanded.
11228  *
11229  * The computed expand value uses either the expand setting explicitly
11230  * set on the widget itself, or, if none has been explicitly set,
11231  * the widget may expand if some of its children do.
11232  *
11233  * Return value: whether widget tree rooted here should be expanded
11234  */
11235 gboolean
11236 gtk_widget_compute_expand (GtkWidget     *widget,
11237                            GtkOrientation orientation)
11238 {
11239   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11240
11241   /* We never make a widget expand if not even showing. */
11242   if (!gtk_widget_get_visible (widget))
11243     return FALSE;
11244
11245   gtk_widget_update_computed_expand (widget);
11246
11247   if (orientation == GTK_ORIENTATION_HORIZONTAL)
11248     {
11249       return widget->priv->computed_hexpand;
11250     }
11251   else
11252     {
11253       return widget->priv->computed_vexpand;
11254     }
11255 }
11256
11257 static void
11258 gtk_widget_set_expand (GtkWidget     *widget,
11259                        GtkOrientation orientation,
11260                        gboolean       expand)
11261 {
11262   const char *expand_prop;
11263   const char *expand_set_prop;
11264   gboolean was_both;
11265   GtkWidgetPrivate *priv;
11266
11267   g_return_if_fail (GTK_IS_WIDGET (widget));
11268
11269   priv = widget->priv;
11270
11271   expand = expand != FALSE;
11272
11273   was_both = priv->hexpand && priv->vexpand;
11274
11275   if (orientation == GTK_ORIENTATION_HORIZONTAL)
11276     {
11277       if (priv->hexpand_set &&
11278           priv->hexpand == expand)
11279         return;
11280
11281       priv->hexpand_set = TRUE;
11282       priv->hexpand = expand;
11283
11284       expand_prop = "hexpand";
11285       expand_set_prop = "hexpand-set";
11286     }
11287   else
11288     {
11289       if (priv->vexpand_set &&
11290           priv->vexpand == expand)
11291         return;
11292
11293       priv->vexpand_set = TRUE;
11294       priv->vexpand = expand;
11295
11296       expand_prop = "vexpand";
11297       expand_set_prop = "vexpand-set";
11298     }
11299
11300   gtk_widget_queue_compute_expand (widget);
11301
11302   g_object_freeze_notify (G_OBJECT (widget));
11303   g_object_notify (G_OBJECT (widget), expand_prop);
11304   g_object_notify (G_OBJECT (widget), expand_set_prop);
11305   if (was_both != (priv->hexpand && priv->vexpand))
11306     g_object_notify (G_OBJECT (widget), "expand");
11307   g_object_thaw_notify (G_OBJECT (widget));
11308 }
11309
11310 static void
11311 gtk_widget_set_expand_set (GtkWidget      *widget,
11312                            GtkOrientation  orientation,
11313                            gboolean        set)
11314 {
11315   GtkWidgetPrivate *priv;
11316   const char *prop;
11317
11318   priv = widget->priv;
11319
11320   set = set != FALSE;
11321
11322   if (orientation == GTK_ORIENTATION_HORIZONTAL)
11323     {
11324       if (set == priv->hexpand_set)
11325         return;
11326
11327       priv->hexpand_set = set;
11328       prop = "hexpand-set";
11329     }
11330   else
11331     {
11332       if (set == priv->vexpand_set)
11333         return;
11334
11335       priv->vexpand_set = set;
11336       prop = "vexpand-set";
11337     }
11338
11339   gtk_widget_queue_compute_expand (widget);
11340
11341   g_object_notify (G_OBJECT (widget), prop);
11342 }
11343
11344 /**
11345  * gtk_widget_get_hexpand:
11346  * @widget: the widget
11347  *
11348  * Gets whether the widget would like any available extra horizontal
11349  * space. When a user resizes a #GtkWindow, widgets with expand=TRUE
11350  * generally receive the extra space. For example, a list or
11351  * scrollable area or document in your window would often be set to
11352  * expand.
11353  *
11354  * Containers should use gtk_widget_compute_expand() rather than
11355  * this function, to see whether a widget, or any of its children,
11356  * has the expand flag set. If any child of a widget wants to
11357  * expand, the parent may ask to expand also.
11358  *
11359  * This function only looks at the widget's own hexpand flag, rather
11360  * than computing whether the entire widget tree rooted at this widget
11361  * wants to expand.
11362  *
11363  * Return value: whether hexpand flag is set
11364  */
11365 gboolean
11366 gtk_widget_get_hexpand (GtkWidget *widget)
11367 {
11368   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11369
11370   return widget->priv->hexpand;
11371 }
11372
11373 /**
11374  * gtk_widget_set_hexpand:
11375  * @widget: the widget
11376  * @expand: whether to expand
11377  *
11378  * Sets whether the widget would like any available extra horizontal
11379  * space. When a user resizes a #GtkWindow, widgets with expand=TRUE
11380  * generally receive the extra space. For example, a list or
11381  * scrollable area or document in your window would often be set to
11382  * expand.
11383  *
11384  * Call this function to set the expand flag if you would like your
11385  * widget to become larger horizontally when the window has extra
11386  * room.
11387  *
11388  * By default, widgets automatically expand if any of their children
11389  * want to expand. (To see if a widget will automatically expand given
11390  * its current children and state, call gtk_widget_compute_expand(). A
11391  * container can decide how the expandability of children affects the
11392  * expansion of the container by overriding the compute_expand virtual
11393  * method on #GtkWidget.).
11394  *
11395  * Setting hexpand explicitly with this function will override the
11396  * automatic expand behavior.
11397  *
11398  * This function forces the widget to expand or not to expand,
11399  * regardless of children.  The override occurs because
11400  * gtk_widget_set_hexpand() sets the hexpand-set property (see
11401  * gtk_widget_set_hexpand_set()) which causes the widget's hexpand
11402  * value to be used, rather than looking at children and widget state.
11403  */
11404 void
11405 gtk_widget_set_hexpand (GtkWidget      *widget,
11406                         gboolean        expand)
11407 {
11408   g_return_if_fail (GTK_IS_WIDGET (widget));
11409
11410   gtk_widget_set_expand (widget, GTK_ORIENTATION_HORIZONTAL, expand);
11411 }
11412
11413 /**
11414  * gtk_widget_get_hexpand_set:
11415  * @widget: the widget
11416  *
11417  * Gets whether gtk_widget_set_hexpand() has been used to
11418  * explicitly set the expand flag on this widget.
11419  *
11420  * If hexpand is set, then it overrides any computed
11421  * expand value based on child widgets. If hexpand is not
11422  * set, then the expand value depends on whether any
11423  * children of the widget would like to expand.
11424  *
11425  * There are few reasons to use this function, but it's here
11426  * for completeness and consistency.
11427  *
11428  * Return value: whether hexpand has been explicitly set
11429  */
11430 gboolean
11431 gtk_widget_get_hexpand_set (GtkWidget      *widget)
11432 {
11433   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11434
11435   return widget->priv->hexpand_set;
11436 }
11437
11438 /**
11439  * gtk_widget_set_hexpand_set:
11440  * @widget: the widget
11441  * @set: value for hexpand-set property
11442  *
11443  * Sets whether the hexpand flag (see gtk_widget_get_hexpand()) will
11444  * be used.
11445  *
11446  * The hexpand-set property will be set automatically when you call
11447  * gtk_widget_set_hexpand() to set hexpand, so the most likely
11448  * reason to use this function would be to unset an explicit expand
11449  * flag.
11450  *
11451  * If hexpand is set, then it overrides any computed
11452  * expand value based on child widgets. If hexpand is not
11453  * set, then the expand value depends on whether any
11454  * children of the widget would like to expand.
11455  *
11456  * There are few reasons to use this function, but it's here
11457  * for completeness and consistency.
11458  *
11459  * Return value: whether hexpand has been explicitly set
11460  */
11461 void
11462 gtk_widget_set_hexpand_set (GtkWidget      *widget,
11463                             gboolean        set)
11464 {
11465   g_return_if_fail (GTK_IS_WIDGET (widget));
11466
11467   gtk_widget_set_expand_set (widget, GTK_ORIENTATION_HORIZONTAL, set);
11468 }
11469
11470
11471 /**
11472  * gtk_widget_get_vexpand:
11473  * @widget: the widget
11474  *
11475  * Gets whether the widget would like any available extra vertical
11476  * space.
11477  *
11478  * See gtk_widget_get_hexpand() for more detail.
11479  *
11480  * Return value: whether vexpand flag is set
11481  */
11482 gboolean
11483 gtk_widget_get_vexpand (GtkWidget *widget)
11484 {
11485   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11486
11487   return widget->priv->vexpand;
11488 }
11489
11490 /**
11491  * gtk_widget_set_vexpand:
11492  * @widget: the widget
11493  * @expand: whether to expand
11494  *
11495  * Sets whether the widget would like any available extra vertical
11496  * space.
11497  *
11498  * See gtk_widget_set_hexpand() for more detail.
11499  */
11500 void
11501 gtk_widget_set_vexpand (GtkWidget      *widget,
11502                         gboolean        expand)
11503 {
11504   g_return_if_fail (GTK_IS_WIDGET (widget));
11505
11506   gtk_widget_set_expand (widget, GTK_ORIENTATION_VERTICAL, expand);
11507 }
11508
11509 /**
11510  * gtk_widget_get_vexpand_set:
11511  * @widget: the widget
11512  *
11513  * Gets whether gtk_widget_set_vexpand() has been used to
11514  * explicitly set the expand flag on this widget.
11515  *
11516  * See gtk_widget_get_hexpand_set() for more detail.
11517  *
11518  * Return value: whether vexpand has been explicitly set
11519  */
11520 gboolean
11521 gtk_widget_get_vexpand_set (GtkWidget      *widget)
11522 {
11523   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
11524
11525   return widget->priv->vexpand_set;
11526 }
11527
11528 /**
11529  * gtk_widget_set_vexpand_set:
11530  * @widget: the widget
11531  * @set: value for vexpand-set property
11532  *
11533  * Sets whether the vexpand flag (see gtk_widget_get_vexpand()) will
11534  * be used.
11535  *
11536  * See gtk_widget_set_hexpand_set() for more detail.
11537  *
11538  * Return value: whether vexpand has been explicitly set
11539  */
11540 void
11541 gtk_widget_set_vexpand_set (GtkWidget      *widget,
11542                             gboolean        set)
11543 {
11544   g_return_if_fail (GTK_IS_WIDGET (widget));
11545
11546   gtk_widget_set_expand_set (widget, GTK_ORIENTATION_VERTICAL, set);
11547 }
11548
11549 /*
11550  * GtkBuildable implementation
11551  */
11552 static GQuark            quark_builder_has_default = 0;
11553 static GQuark            quark_builder_has_focus = 0;
11554 static GQuark            quark_builder_atk_relations = 0;
11555 static GQuark            quark_builder_set_name = 0;
11556
11557 static void
11558 gtk_widget_buildable_interface_init (GtkBuildableIface *iface)
11559 {
11560   quark_builder_has_default = g_quark_from_static_string ("gtk-builder-has-default");
11561   quark_builder_has_focus = g_quark_from_static_string ("gtk-builder-has-focus");
11562   quark_builder_atk_relations = g_quark_from_static_string ("gtk-builder-atk-relations");
11563   quark_builder_set_name = g_quark_from_static_string ("gtk-builder-set-name");
11564
11565   iface->set_name = gtk_widget_buildable_set_name;
11566   iface->get_name = gtk_widget_buildable_get_name;
11567   iface->get_internal_child = gtk_widget_buildable_get_internal_child;
11568   iface->set_buildable_property = gtk_widget_buildable_set_buildable_property;
11569   iface->parser_finished = gtk_widget_buildable_parser_finished;
11570   iface->custom_tag_start = gtk_widget_buildable_custom_tag_start;
11571   iface->custom_finished = gtk_widget_buildable_custom_finished;
11572 }
11573
11574 static void
11575 gtk_widget_buildable_set_name (GtkBuildable *buildable,
11576                                const gchar  *name)
11577 {
11578   g_object_set_qdata_full (G_OBJECT (buildable), quark_builder_set_name,
11579                            g_strdup (name), g_free);
11580 }
11581
11582 static const gchar *
11583 gtk_widget_buildable_get_name (GtkBuildable *buildable)
11584 {
11585   return g_object_get_qdata (G_OBJECT (buildable), quark_builder_set_name);
11586 }
11587
11588 static GObject *
11589 gtk_widget_buildable_get_internal_child (GtkBuildable *buildable,
11590                                          GtkBuilder   *builder,
11591                                          const gchar  *childname)
11592 {
11593   if (strcmp (childname, "accessible") == 0)
11594     return G_OBJECT (gtk_widget_get_accessible (GTK_WIDGET (buildable)));
11595
11596   return NULL;
11597 }
11598
11599 static void
11600 gtk_widget_buildable_set_buildable_property (GtkBuildable *buildable,
11601                                              GtkBuilder   *builder,
11602                                              const gchar  *name,
11603                                              const GValue *value)
11604 {
11605   if (strcmp (name, "has-default") == 0 && g_value_get_boolean (value))
11606       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_default,
11607                           GINT_TO_POINTER (TRUE));
11608   else if (strcmp (name, "has-focus") == 0 && g_value_get_boolean (value))
11609       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_focus,
11610                           GINT_TO_POINTER (TRUE));
11611   else
11612     g_object_set_property (G_OBJECT (buildable), name, value);
11613 }
11614
11615 typedef struct
11616 {
11617   gchar *action_name;
11618   GString *description;
11619   gchar *context;
11620   gboolean translatable;
11621 } AtkActionData;
11622
11623 typedef struct
11624 {
11625   gchar *target;
11626   gchar *type;
11627 } AtkRelationData;
11628
11629 static void
11630 free_action (AtkActionData *data, gpointer user_data)
11631 {
11632   g_free (data->action_name);
11633   g_string_free (data->description, TRUE);
11634   g_free (data->context);
11635   g_slice_free (AtkActionData, data);
11636 }
11637
11638 static void
11639 free_relation (AtkRelationData *data, gpointer user_data)
11640 {
11641   g_free (data->target);
11642   g_free (data->type);
11643   g_slice_free (AtkRelationData, data);
11644 }
11645
11646 static void
11647 gtk_widget_buildable_parser_finished (GtkBuildable *buildable,
11648                                       GtkBuilder   *builder)
11649 {
11650   GSList *atk_relations;
11651
11652   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_default))
11653     gtk_widget_grab_default (GTK_WIDGET (buildable));
11654   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_focus))
11655     gtk_widget_grab_focus (GTK_WIDGET (buildable));
11656
11657   atk_relations = g_object_get_qdata (G_OBJECT (buildable),
11658                                       quark_builder_atk_relations);
11659   if (atk_relations)
11660     {
11661       AtkObject *accessible;
11662       AtkRelationSet *relation_set;
11663       GSList *l;
11664       GObject *target;
11665       AtkRelationType relation_type;
11666       AtkObject *target_accessible;
11667
11668       accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
11669       relation_set = atk_object_ref_relation_set (accessible);
11670
11671       for (l = atk_relations; l; l = l->next)
11672         {
11673           AtkRelationData *relation = (AtkRelationData*)l->data;
11674
11675           target = gtk_builder_get_object (builder, relation->target);
11676           if (!target)
11677             {
11678               g_warning ("Target object %s in <relation> does not exist",
11679                          relation->target);
11680               continue;
11681             }
11682           target_accessible = gtk_widget_get_accessible (GTK_WIDGET (target));
11683           g_assert (target_accessible != NULL);
11684
11685           relation_type = atk_relation_type_for_name (relation->type);
11686           if (relation_type == ATK_RELATION_NULL)
11687             {
11688               g_warning ("<relation> type %s not found",
11689                          relation->type);
11690               continue;
11691             }
11692           atk_relation_set_add_relation_by_type (relation_set, relation_type,
11693                                                  target_accessible);
11694         }
11695       g_object_unref (relation_set);
11696
11697       g_slist_foreach (atk_relations, (GFunc)free_relation, NULL);
11698       g_slist_free (atk_relations);
11699       g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
11700                           NULL);
11701     }
11702 }
11703
11704 typedef struct
11705 {
11706   GSList *actions;
11707   GSList *relations;
11708 } AccessibilitySubParserData;
11709
11710 static void
11711 accessibility_start_element (GMarkupParseContext  *context,
11712                              const gchar          *element_name,
11713                              const gchar         **names,
11714                              const gchar         **values,
11715                              gpointer              user_data,
11716                              GError              **error)
11717 {
11718   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
11719   guint i;
11720   gint line_number, char_number;
11721
11722   if (strcmp (element_name, "relation") == 0)
11723     {
11724       gchar *target = NULL;
11725       gchar *type = NULL;
11726       AtkRelationData *relation;
11727
11728       for (i = 0; names[i]; i++)
11729         {
11730           if (strcmp (names[i], "target") == 0)
11731             target = g_strdup (values[i]);
11732           else if (strcmp (names[i], "type") == 0)
11733             type = g_strdup (values[i]);
11734           else
11735             {
11736               g_markup_parse_context_get_position (context,
11737                                                    &line_number,
11738                                                    &char_number);
11739               g_set_error (error,
11740                            GTK_BUILDER_ERROR,
11741                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
11742                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
11743                            "<input>",
11744                            line_number, char_number, names[i], "relation");
11745               g_free (target);
11746               g_free (type);
11747               return;
11748             }
11749         }
11750
11751       if (!target || !type)
11752         {
11753           g_markup_parse_context_get_position (context,
11754                                                &line_number,
11755                                                &char_number);
11756           g_set_error (error,
11757                        GTK_BUILDER_ERROR,
11758                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
11759                        "%s:%d:%d <%s> requires attribute \"%s\"",
11760                        "<input>",
11761                        line_number, char_number, "relation",
11762                        type ? "target" : "type");
11763           g_free (target);
11764           g_free (type);
11765           return;
11766         }
11767
11768       relation = g_slice_new (AtkRelationData);
11769       relation->target = target;
11770       relation->type = type;
11771
11772       data->relations = g_slist_prepend (data->relations, relation);
11773     }
11774   else if (strcmp (element_name, "action") == 0)
11775     {
11776       const gchar *action_name = NULL;
11777       const gchar *description = NULL;
11778       const gchar *msg_context = NULL;
11779       gboolean translatable = FALSE;
11780       AtkActionData *action;
11781
11782       for (i = 0; names[i]; i++)
11783         {
11784           if (strcmp (names[i], "action_name") == 0)
11785             action_name = values[i];
11786           else if (strcmp (names[i], "description") == 0)
11787             description = values[i];
11788           else if (strcmp (names[i], "translatable") == 0)
11789             {
11790               if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
11791                 return;
11792             }
11793           else if (strcmp (names[i], "comments") == 0)
11794             {
11795               /* do nothing, comments are for translators */
11796             }
11797           else if (strcmp (names[i], "context") == 0)
11798             msg_context = values[i];
11799           else
11800             {
11801               g_markup_parse_context_get_position (context,
11802                                                    &line_number,
11803                                                    &char_number);
11804               g_set_error (error,
11805                            GTK_BUILDER_ERROR,
11806                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
11807                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
11808                            "<input>",
11809                            line_number, char_number, names[i], "action");
11810               return;
11811             }
11812         }
11813
11814       if (!action_name)
11815         {
11816           g_markup_parse_context_get_position (context,
11817                                                &line_number,
11818                                                &char_number);
11819           g_set_error (error,
11820                        GTK_BUILDER_ERROR,
11821                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
11822                        "%s:%d:%d <%s> requires attribute \"%s\"",
11823                        "<input>",
11824                        line_number, char_number, "action",
11825                        "action_name");
11826           return;
11827         }
11828
11829       action = g_slice_new (AtkActionData);
11830       action->action_name = g_strdup (action_name);
11831       action->description = g_string_new (description);
11832       action->context = g_strdup (msg_context);
11833       action->translatable = translatable;
11834
11835       data->actions = g_slist_prepend (data->actions, action);
11836     }
11837   else if (strcmp (element_name, "accessibility") == 0)
11838     ;
11839   else
11840     g_warning ("Unsupported tag for GtkWidget: %s\n", element_name);
11841 }
11842
11843 static void
11844 accessibility_text (GMarkupParseContext  *context,
11845                     const gchar          *text,
11846                     gsize                 text_len,
11847                     gpointer              user_data,
11848                     GError              **error)
11849 {
11850   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
11851
11852   if (strcmp (g_markup_parse_context_get_element (context), "action") == 0)
11853     {
11854       AtkActionData *action = data->actions->data;
11855
11856       g_string_append_len (action->description, text, text_len);
11857     }
11858 }
11859
11860 static const GMarkupParser accessibility_parser =
11861   {
11862     accessibility_start_element,
11863     NULL,
11864     accessibility_text,
11865   };
11866
11867 typedef struct
11868 {
11869   GObject *object;
11870   guint    key;
11871   guint    modifiers;
11872   gchar   *signal;
11873 } AccelGroupParserData;
11874
11875 static void
11876 accel_group_start_element (GMarkupParseContext  *context,
11877                            const gchar          *element_name,
11878                            const gchar         **names,
11879                            const gchar         **values,
11880                            gpointer              user_data,
11881                            GError              **error)
11882 {
11883   gint i;
11884   guint key = 0;
11885   guint modifiers = 0;
11886   gchar *signal = NULL;
11887   AccelGroupParserData *parser_data = (AccelGroupParserData*)user_data;
11888
11889   for (i = 0; names[i]; i++)
11890     {
11891       if (strcmp (names[i], "key") == 0)
11892         key = gdk_keyval_from_name (values[i]);
11893       else if (strcmp (names[i], "modifiers") == 0)
11894         {
11895           if (!_gtk_builder_flags_from_string (GDK_TYPE_MODIFIER_TYPE,
11896                                                values[i],
11897                                                &modifiers,
11898                                                error))
11899               return;
11900         }
11901       else if (strcmp (names[i], "signal") == 0)
11902         signal = g_strdup (values[i]);
11903     }
11904
11905   if (key == 0 || signal == NULL)
11906     {
11907       g_warning ("<accelerator> requires key and signal attributes");
11908       return;
11909     }
11910   parser_data->key = key;
11911   parser_data->modifiers = modifiers;
11912   parser_data->signal = signal;
11913 }
11914
11915 static const GMarkupParser accel_group_parser =
11916   {
11917     accel_group_start_element,
11918   };
11919
11920 static gboolean
11921 gtk_widget_buildable_custom_tag_start (GtkBuildable     *buildable,
11922                                        GtkBuilder       *builder,
11923                                        GObject          *child,
11924                                        const gchar      *tagname,
11925                                        GMarkupParser    *parser,
11926                                        gpointer         *data)
11927 {
11928   g_assert (buildable);
11929
11930   if (strcmp (tagname, "accelerator") == 0)
11931     {
11932       AccelGroupParserData *parser_data;
11933
11934       parser_data = g_slice_new0 (AccelGroupParserData);
11935       parser_data->object = g_object_ref (buildable);
11936       *parser = accel_group_parser;
11937       *data = parser_data;
11938       return TRUE;
11939     }
11940   if (strcmp (tagname, "accessibility") == 0)
11941     {
11942       AccessibilitySubParserData *parser_data;
11943
11944       parser_data = g_slice_new0 (AccessibilitySubParserData);
11945       *parser = accessibility_parser;
11946       *data = parser_data;
11947       return TRUE;
11948     }
11949   return FALSE;
11950 }
11951
11952 void
11953 _gtk_widget_buildable_finish_accelerator (GtkWidget *widget,
11954                                           GtkWidget *toplevel,
11955                                           gpointer   user_data)
11956 {
11957   AccelGroupParserData *accel_data;
11958   GSList *accel_groups;
11959   GtkAccelGroup *accel_group;
11960
11961   g_return_if_fail (GTK_IS_WIDGET (widget));
11962   g_return_if_fail (GTK_IS_WIDGET (toplevel));
11963   g_return_if_fail (user_data != NULL);
11964
11965   accel_data = (AccelGroupParserData*)user_data;
11966   accel_groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
11967   if (g_slist_length (accel_groups) == 0)
11968     {
11969       accel_group = gtk_accel_group_new ();
11970       gtk_window_add_accel_group (GTK_WINDOW (toplevel), accel_group);
11971     }
11972   else
11973     {
11974       g_assert (g_slist_length (accel_groups) == 1);
11975       accel_group = g_slist_nth_data (accel_groups, 0);
11976     }
11977
11978   gtk_widget_add_accelerator (GTK_WIDGET (accel_data->object),
11979                               accel_data->signal,
11980                               accel_group,
11981                               accel_data->key,
11982                               accel_data->modifiers,
11983                               GTK_ACCEL_VISIBLE);
11984
11985   g_object_unref (accel_data->object);
11986   g_free (accel_data->signal);
11987   g_slice_free (AccelGroupParserData, accel_data);
11988 }
11989
11990 static void
11991 gtk_widget_buildable_custom_finished (GtkBuildable *buildable,
11992                                       GtkBuilder   *builder,
11993                                       GObject      *child,
11994                                       const gchar  *tagname,
11995                                       gpointer      user_data)
11996 {
11997   AccelGroupParserData *accel_data;
11998   AccessibilitySubParserData *a11y_data;
11999   GtkWidget *toplevel;
12000
12001   if (strcmp (tagname, "accelerator") == 0)
12002     {
12003       accel_data = (AccelGroupParserData*)user_data;
12004       g_assert (accel_data->object);
12005
12006       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (accel_data->object));
12007
12008       _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
12009     }
12010   else if (strcmp (tagname, "accessibility") == 0)
12011     {
12012       a11y_data = (AccessibilitySubParserData*)user_data;
12013
12014       if (a11y_data->actions)
12015         {
12016           AtkObject *accessible;
12017           AtkAction *action;
12018           gint i, n_actions;
12019           GSList *l;
12020
12021           accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
12022
12023           action = ATK_ACTION (accessible);
12024           n_actions = atk_action_get_n_actions (action);
12025
12026           for (l = a11y_data->actions; l; l = l->next)
12027             {
12028               AtkActionData *action_data = (AtkActionData*)l->data;
12029
12030               for (i = 0; i < n_actions; i++)
12031                 if (strcmp (atk_action_get_name (action, i),
12032                             action_data->action_name) == 0)
12033                   break;
12034
12035               if (i < n_actions)
12036                 {
12037                   gchar *description;
12038
12039                   if (action_data->translatable && action_data->description->len)
12040                     description = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
12041                                                                  action_data->context,
12042                                                                  action_data->description->str);
12043                   else
12044                     description = action_data->description->str;
12045
12046                   atk_action_set_description (action, i, description);
12047                 }
12048             }
12049
12050           g_slist_foreach (a11y_data->actions, (GFunc)free_action, NULL);
12051           g_slist_free (a11y_data->actions);
12052         }
12053
12054       if (a11y_data->relations)
12055         g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
12056                             a11y_data->relations);
12057
12058       g_slice_free (AccessibilitySubParserData, a11y_data);
12059     }
12060 }
12061
12062 static void
12063 gtk_widget_real_get_width (GtkWidget *widget,
12064                            gint      *minimum_size,
12065                            gint      *natural_size)
12066 {
12067   if (minimum_size)
12068     *minimum_size = 0;
12069
12070   if (natural_size)
12071     *natural_size = 0;
12072 }
12073
12074 static void
12075 gtk_widget_real_get_height (GtkWidget *widget,
12076                             gint      *minimum_size,
12077                             gint      *natural_size)
12078 {
12079   if (minimum_size)
12080     *minimum_size = 0;
12081
12082   if (natural_size)
12083     *natural_size = 0;
12084 }
12085
12086 static void
12087 gtk_widget_real_get_height_for_width (GtkWidget *widget,
12088                                       gint       width,
12089                                       gint      *minimum_height,
12090                                       gint      *natural_height)
12091 {
12092   GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
12093 }
12094
12095 static void
12096 gtk_widget_real_get_width_for_height (GtkWidget *widget,
12097                                       gint       height,
12098                                       gint      *minimum_width,
12099                                       gint      *natural_width)
12100 {
12101   GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
12102 }
12103
12104 /**
12105  * gtk_widget_get_halign:
12106  * @widget: a #GtkWidget
12107  *
12108  * Gets the value of the #GtkWidget:halign property.
12109  *
12110  * Returns: the horizontal alignment of @widget
12111  */
12112 GtkAlign
12113 gtk_widget_get_halign (GtkWidget *widget)
12114 {
12115   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_ALIGN_FILL);
12116   return _gtk_widget_get_aux_info_or_defaults (widget)->halign;
12117 }
12118
12119 /**
12120  * gtk_widget_set_halign:
12121  * @widget: a #GtkWidget
12122  * @align: the horizontal alignment
12123  *
12124  * Sets the horizontal alignment of @widget.
12125  * See the #GtkWidget:halign property.
12126  */
12127 void
12128 gtk_widget_set_halign (GtkWidget *widget,
12129                        GtkAlign   align)
12130 {
12131   GtkWidgetAuxInfo *aux_info;
12132
12133   g_return_if_fail (GTK_IS_WIDGET (widget));
12134
12135   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
12136
12137   if (aux_info->halign == align)
12138     return;
12139
12140   aux_info->halign = align;
12141   gtk_widget_queue_resize (widget);
12142   g_object_notify (G_OBJECT (widget), "halign");
12143 }
12144
12145 /**
12146  * gtk_widget_get_valign:
12147  * @widget: a #GtkWidget
12148  *
12149  * Gets the value of the #GtkWidget:valign property.
12150  *
12151  * Returns: the vertical alignment of @widget
12152  */
12153 GtkAlign
12154 gtk_widget_get_valign (GtkWidget *widget)
12155 {
12156   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_ALIGN_FILL);
12157   return _gtk_widget_get_aux_info_or_defaults (widget)->valign;
12158 }
12159
12160 /**
12161  * gtk_widget_set_valign:
12162  * @widget: a #GtkWidget
12163  * @align: the vertical alignment
12164  *
12165  * Sets the vertical alignment of @widget.
12166  * See the #GtkWidget:valign property.
12167  */
12168 void
12169 gtk_widget_set_valign (GtkWidget *widget,
12170                        GtkAlign   align)
12171 {
12172   GtkWidgetAuxInfo *aux_info;
12173
12174   g_return_if_fail (GTK_IS_WIDGET (widget));
12175
12176   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
12177
12178   if (aux_info->valign == align)
12179     return;
12180
12181   aux_info->valign = align;
12182   gtk_widget_queue_resize (widget);
12183   g_object_notify (G_OBJECT (widget), "valign");
12184 }
12185
12186 /**
12187  * gtk_widget_get_margin_left:
12188  * @widget: a #GtkWidget
12189  *
12190  * Gets the value of the #GtkWidget:margin-left property.
12191  *
12192  * Returns: The left margin of @widget
12193  */
12194 gint
12195 gtk_widget_get_margin_left (GtkWidget *widget)
12196 {
12197   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
12198
12199   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.left;
12200 }
12201
12202 /**
12203  * gtk_widget_set_margin_left:
12204  * @widget: a #GtkWidget
12205  * @margin: the left margin
12206  *
12207  * Sets the left margin of @widget.
12208  * See the #GtkWidget:margin-left property.
12209  */
12210 void
12211 gtk_widget_set_margin_left (GtkWidget *widget,
12212                             gint       margin)
12213 {
12214   GtkWidgetAuxInfo *aux_info;
12215
12216   g_return_if_fail (GTK_IS_WIDGET (widget));
12217   g_return_if_fail (margin <= G_MAXINT16);
12218
12219   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
12220
12221   if (aux_info->margin.left == margin)
12222     return;
12223
12224   aux_info->margin.left = margin;
12225   gtk_widget_queue_resize (widget);
12226   g_object_notify (G_OBJECT (widget), "margin-left");
12227 }
12228
12229 /**
12230  * gtk_widget_get_margin_right:
12231  * @widget: a #GtkWidget
12232  *
12233  * Gets the value of the #GtkWidget:margin-right property.
12234  *
12235  * Returns: The left margin of @widget
12236  */
12237 gint
12238 gtk_widget_get_margin_right (GtkWidget *widget)
12239 {
12240   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
12241
12242   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.right;
12243 }
12244
12245 /**
12246  * gtk_widget_set_margin_right:
12247  * @widget: a #GtkWidget
12248  * @margin: the right margin
12249  *
12250  * Sets the right margin of @widget.
12251  * See the #GtkWidget:margin-right property.
12252  */
12253 void
12254 gtk_widget_set_margin_right (GtkWidget *widget,
12255                              gint       margin)
12256 {
12257   GtkWidgetAuxInfo *aux_info;
12258
12259   g_return_if_fail (GTK_IS_WIDGET (widget));
12260   g_return_if_fail (margin <= G_MAXINT16);
12261
12262   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
12263
12264   if (aux_info->margin.right == margin)
12265     return;
12266
12267   aux_info->margin.right = margin;
12268   gtk_widget_queue_resize (widget);
12269   g_object_notify (G_OBJECT (widget), "margin-right");
12270 }
12271
12272 /**
12273  * gtk_widget_get_margin_top:
12274  * @widget: a #GtkWidget
12275  *
12276  * Gets the value of the #GtkWidget:margin-top property.
12277  *
12278  * Returns: The top margin of @widget
12279  */
12280 gint
12281 gtk_widget_get_margin_top (GtkWidget *widget)
12282 {
12283   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
12284
12285   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.top;
12286 }
12287
12288 /**
12289  * gtk_widget_set_margin_top:
12290  * @widget: a #GtkWidget
12291  * @margin: the top margin
12292  *
12293  * Sets the top margin of @widget.
12294  * See the #GtkWidget:margin-top property.
12295  */
12296 void
12297 gtk_widget_set_margin_top (GtkWidget *widget,
12298                            gint       margin)
12299 {
12300   GtkWidgetAuxInfo *aux_info;
12301
12302   g_return_if_fail (GTK_IS_WIDGET (widget));
12303   g_return_if_fail (margin <= G_MAXINT16);
12304
12305   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
12306
12307   if (aux_info->margin.top == margin)
12308     return;
12309
12310   aux_info->margin.top = margin;
12311   gtk_widget_queue_resize (widget);
12312   g_object_notify (G_OBJECT (widget), "margin-top");
12313 }
12314
12315 /**
12316  * gtk_widget_get_margin_bottom:
12317  * @widget: a #GtkWidget
12318  *
12319  * Gets the value of the #GtkWidget:margin-bottom property.
12320  *
12321  * Returns: The bottom margin of @widget
12322  */
12323 gint
12324 gtk_widget_get_margin_bottom (GtkWidget *widget)
12325 {
12326   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
12327
12328   return _gtk_widget_get_aux_info_or_defaults (widget)->margin.bottom;
12329 }
12330
12331 /**
12332  * gtk_widget_set_margin_bottom:
12333  * @widget: a #GtkWidget
12334  * @margin: the bottom margin
12335  *
12336  * Sets the bottom margin of @widget.
12337  * See the #GtkWidget:margin-bottom property.
12338  */
12339 void
12340 gtk_widget_set_margin_bottom (GtkWidget *widget,
12341                               gint       margin)
12342 {
12343   GtkWidgetAuxInfo *aux_info;
12344
12345   g_return_if_fail (GTK_IS_WIDGET (widget));
12346   g_return_if_fail (margin <= G_MAXINT16);
12347
12348   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
12349
12350   if (aux_info->margin.bottom == margin)
12351     return;
12352
12353   aux_info->margin.bottom = margin;
12354   gtk_widget_queue_resize (widget);
12355   g_object_notify (G_OBJECT (widget), "margin-bottom");
12356 }
12357
12358 /**
12359  * gtk_widget_get_clipboard:
12360  * @widget: a #GtkWidget
12361  * @selection: a #GdkAtom which identifies the clipboard
12362  *             to use. %GDK_SELECTION_CLIPBOARD gives the
12363  *             default clipboard. Another common value
12364  *             is %GDK_SELECTION_PRIMARY, which gives
12365  *             the primary X selection.
12366  *
12367  * Returns the clipboard object for the given selection to
12368  * be used with @widget. @widget must have a #GdkDisplay
12369  * associated with it, so must be attached to a toplevel
12370  * window.
12371  *
12372  * Return value: (transfer none): the appropriate clipboard object. If no
12373  *             clipboard already exists, a new one will
12374  *             be created. Once a clipboard object has
12375  *             been created, it is persistent for all time.
12376  *
12377  * Since: 2.2
12378  **/
12379 GtkClipboard *
12380 gtk_widget_get_clipboard (GtkWidget *widget, GdkAtom selection)
12381 {
12382   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
12383   g_return_val_if_fail (gtk_widget_has_screen (widget), NULL);
12384
12385   return gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
12386                                         selection);
12387 }
12388
12389 /**
12390  * gtk_widget_list_mnemonic_labels:
12391  * @widget: a #GtkWidget
12392  *
12393  * Returns a newly allocated list of the widgets, normally labels, for
12394  * which this widget is the target of a mnemonic (see for example,
12395  * gtk_label_set_mnemonic_widget()).
12396
12397  * The widgets in the list are not individually referenced. If you
12398  * want to iterate through the list and perform actions involving
12399  * callbacks that might destroy the widgets, you
12400  * <emphasis>must</emphasis> call <literal>g_list_foreach (result,
12401  * (GFunc)g_object_ref, NULL)</literal> first, and then unref all the
12402  * widgets afterwards.
12403
12404  * Return value: (element-type GtkWidget) (transfer container): the list of
12405  *  mnemonic labels; free this list
12406  *  with g_list_free() when you are done with it.
12407  *
12408  * Since: 2.4
12409  **/
12410 GList *
12411 gtk_widget_list_mnemonic_labels (GtkWidget *widget)
12412 {
12413   GList *list = NULL;
12414   GSList *l;
12415
12416   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
12417
12418   for (l = g_object_get_qdata (G_OBJECT (widget), quark_mnemonic_labels); l; l = l->next)
12419     list = g_list_prepend (list, l->data);
12420
12421   return list;
12422 }
12423
12424 /**
12425  * gtk_widget_add_mnemonic_label:
12426  * @widget: a #GtkWidget
12427  * @label: a #GtkWidget that acts as a mnemonic label for @widget
12428  *
12429  * Adds a widget to the list of mnemonic labels for
12430  * this widget. (See gtk_widget_list_mnemonic_labels()). Note the
12431  * list of mnemonic labels for the widget is cleared when the
12432  * widget is destroyed, so the caller must make sure to update
12433  * its internal state at this point as well, by using a connection
12434  * to the #GtkWidget::destroy signal or a weak notifier.
12435  *
12436  * Since: 2.4
12437  **/
12438 void
12439 gtk_widget_add_mnemonic_label (GtkWidget *widget,
12440                                GtkWidget *label)
12441 {
12442   GSList *old_list, *new_list;
12443
12444   g_return_if_fail (GTK_IS_WIDGET (widget));
12445   g_return_if_fail (GTK_IS_WIDGET (label));
12446
12447   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
12448   new_list = g_slist_prepend (old_list, label);
12449
12450   g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
12451                            new_list, (GDestroyNotify) g_slist_free);
12452 }
12453
12454 /**
12455  * gtk_widget_remove_mnemonic_label:
12456  * @widget: a #GtkWidget
12457  * @label: a #GtkWidget that was previously set as a mnemnic label for
12458  *         @widget with gtk_widget_add_mnemonic_label().
12459  *
12460  * Removes a widget from the list of mnemonic labels for
12461  * this widget. (See gtk_widget_list_mnemonic_labels()). The widget
12462  * must have previously been added to the list with
12463  * gtk_widget_add_mnemonic_label().
12464  *
12465  * Since: 2.4
12466  **/
12467 void
12468 gtk_widget_remove_mnemonic_label (GtkWidget *widget,
12469                                   GtkWidget *label)
12470 {
12471   GSList *old_list, *new_list;
12472
12473   g_return_if_fail (GTK_IS_WIDGET (widget));
12474   g_return_if_fail (GTK_IS_WIDGET (label));
12475
12476   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
12477   new_list = g_slist_remove (old_list, label);
12478
12479   if (new_list)
12480     g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
12481                              new_list, (GDestroyNotify) g_slist_free);
12482 }
12483
12484 /**
12485  * gtk_widget_get_no_show_all:
12486  * @widget: a #GtkWidget
12487  *
12488  * Returns the current value of the #GtkWidget:no-show-all property,
12489  * which determines whether calls to gtk_widget_show_all()
12490  * will affect this widget.
12491  *
12492  * Return value: the current value of the "no-show-all" property.
12493  *
12494  * Since: 2.4
12495  **/
12496 gboolean
12497 gtk_widget_get_no_show_all (GtkWidget *widget)
12498 {
12499   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12500
12501   return widget->priv->no_show_all;
12502 }
12503
12504 /**
12505  * gtk_widget_set_no_show_all:
12506  * @widget: a #GtkWidget
12507  * @no_show_all: the new value for the "no-show-all" property
12508  *
12509  * Sets the #GtkWidget:no-show-all property, which determines whether
12510  * calls to gtk_widget_show_all() will affect this widget.
12511  *
12512  * This is mostly for use in constructing widget hierarchies with externally
12513  * controlled visibility, see #GtkUIManager.
12514  *
12515  * Since: 2.4
12516  **/
12517 void
12518 gtk_widget_set_no_show_all (GtkWidget *widget,
12519                             gboolean   no_show_all)
12520 {
12521   g_return_if_fail (GTK_IS_WIDGET (widget));
12522
12523   no_show_all = (no_show_all != FALSE);
12524
12525   if (widget->priv->no_show_all != no_show_all)
12526     {
12527       widget->priv->no_show_all = no_show_all;
12528
12529       g_object_notify (G_OBJECT (widget), "no-show-all");
12530     }
12531 }
12532
12533
12534 static void
12535 gtk_widget_real_set_has_tooltip (GtkWidget *widget,
12536                                  gboolean   has_tooltip,
12537                                  gboolean   force)
12538 {
12539   GtkWidgetPrivate *priv = widget->priv;
12540   gboolean priv_has_tooltip;
12541
12542   priv_has_tooltip = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget),
12543                                        quark_has_tooltip));
12544
12545   if (priv_has_tooltip != has_tooltip || force)
12546     {
12547       priv_has_tooltip = has_tooltip;
12548
12549       if (priv_has_tooltip)
12550         {
12551           if (gtk_widget_get_realized (widget) && !gtk_widget_get_has_window (widget))
12552             gdk_window_set_events (priv->window,
12553                                    gdk_window_get_events (priv->window) |
12554                                    GDK_LEAVE_NOTIFY_MASK |
12555                                    GDK_POINTER_MOTION_MASK |
12556                                    GDK_POINTER_MOTION_HINT_MASK);
12557
12558           if (gtk_widget_get_has_window (widget))
12559               gtk_widget_add_events (widget,
12560                                      GDK_LEAVE_NOTIFY_MASK |
12561                                      GDK_POINTER_MOTION_MASK |
12562                                      GDK_POINTER_MOTION_HINT_MASK);
12563         }
12564
12565       g_object_set_qdata (G_OBJECT (widget), quark_has_tooltip,
12566                           GUINT_TO_POINTER (priv_has_tooltip));
12567     }
12568 }
12569
12570 /**
12571  * gtk_widget_set_tooltip_window:
12572  * @widget: a #GtkWidget
12573  * @custom_window: (allow-none): a #GtkWindow, or %NULL
12574  *
12575  * Replaces the default, usually yellow, window used for displaying
12576  * tooltips with @custom_window. GTK+ will take care of showing and
12577  * hiding @custom_window at the right moment, to behave likewise as
12578  * the default tooltip window. If @custom_window is %NULL, the default
12579  * tooltip window will be used.
12580  *
12581  * If the custom window should have the default theming it needs to
12582  * have the name "gtk-tooltip", see gtk_widget_set_name().
12583  *
12584  * Since: 2.12
12585  */
12586 void
12587 gtk_widget_set_tooltip_window (GtkWidget *widget,
12588                                GtkWindow *custom_window)
12589 {
12590   gboolean has_tooltip;
12591   gchar *tooltip_markup;
12592
12593   g_return_if_fail (GTK_IS_WIDGET (widget));
12594   g_return_if_fail (custom_window == NULL || GTK_IS_WINDOW (custom_window));
12595
12596   tooltip_markup = g_object_get_qdata (G_OBJECT (widget), quark_tooltip_markup);
12597
12598   if (custom_window)
12599     g_object_ref (custom_window);
12600
12601   g_object_set_qdata_full (G_OBJECT (widget), quark_tooltip_window,
12602                            custom_window, g_object_unref);
12603
12604   has_tooltip = (custom_window != NULL || tooltip_markup != NULL);
12605   gtk_widget_real_set_has_tooltip (widget, has_tooltip, FALSE);
12606
12607   if (has_tooltip && gtk_widget_get_visible (widget))
12608     gtk_widget_queue_tooltip_query (widget);
12609 }
12610
12611 /**
12612  * gtk_widget_get_tooltip_window:
12613  * @widget: a #GtkWidget
12614  *
12615  * Returns the #GtkWindow of the current tooltip. This can be the
12616  * GtkWindow created by default, or the custom tooltip window set
12617  * using gtk_widget_set_tooltip_window().
12618  *
12619  * Return value: (transfer none): The #GtkWindow of the current tooltip.
12620  *
12621  * Since: 2.12
12622  */
12623 GtkWindow *
12624 gtk_widget_get_tooltip_window (GtkWidget *widget)
12625 {
12626   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
12627
12628   return g_object_get_qdata (G_OBJECT (widget), quark_tooltip_window);
12629 }
12630
12631 /**
12632  * gtk_widget_trigger_tooltip_query:
12633  * @widget: a #GtkWidget
12634  *
12635  * Triggers a tooltip query on the display where the toplevel of @widget
12636  * is located. See gtk_tooltip_trigger_tooltip_query() for more
12637  * information.
12638  *
12639  * Since: 2.12
12640  */
12641 void
12642 gtk_widget_trigger_tooltip_query (GtkWidget *widget)
12643 {
12644   gtk_tooltip_trigger_tooltip_query (gtk_widget_get_display (widget));
12645 }
12646
12647 static guint tooltip_query_id;
12648 static GSList *tooltip_query_displays;
12649
12650 static gboolean
12651 tooltip_query_idle (gpointer data)
12652 {
12653   g_slist_foreach (tooltip_query_displays, (GFunc)gtk_tooltip_trigger_tooltip_query, NULL);
12654   g_slist_foreach (tooltip_query_displays, (GFunc)g_object_unref, NULL);
12655   g_slist_free (tooltip_query_displays);
12656
12657   tooltip_query_displays = NULL;
12658   tooltip_query_id = 0;
12659
12660   return FALSE;
12661 }
12662
12663 static void
12664 gtk_widget_queue_tooltip_query (GtkWidget *widget)
12665 {
12666   GdkDisplay *display;
12667
12668   display = gtk_widget_get_display (widget);
12669
12670   if (!g_slist_find (tooltip_query_displays, display))
12671     tooltip_query_displays = g_slist_prepend (tooltip_query_displays, g_object_ref (display));
12672
12673   if (tooltip_query_id == 0)
12674     tooltip_query_id = gdk_threads_add_idle (tooltip_query_idle, NULL);
12675 }
12676
12677 /**
12678  * gtk_widget_set_tooltip_text:
12679  * @widget: a #GtkWidget
12680  * @text: the contents of the tooltip for @widget
12681  *
12682  * Sets @text as the contents of the tooltip. This function will take
12683  * care of setting #GtkWidget:has-tooltip to %TRUE and of the default
12684  * handler for the #GtkWidget::query-tooltip signal.
12685  *
12686  * See also the #GtkWidget:tooltip-text property and gtk_tooltip_set_text().
12687  *
12688  * Since: 2.12
12689  */
12690 void
12691 gtk_widget_set_tooltip_text (GtkWidget   *widget,
12692                              const gchar *text)
12693 {
12694   g_return_if_fail (GTK_IS_WIDGET (widget));
12695
12696   g_object_set (G_OBJECT (widget), "tooltip-text", text, NULL);
12697 }
12698
12699 /**
12700  * gtk_widget_get_tooltip_text:
12701  * @widget: a #GtkWidget
12702  *
12703  * Gets the contents of the tooltip for @widget.
12704  *
12705  * Return value: the tooltip text, or %NULL. You should free the
12706  *   returned string with g_free() when done.
12707  *
12708  * Since: 2.12
12709  */
12710 gchar *
12711 gtk_widget_get_tooltip_text (GtkWidget *widget)
12712 {
12713   gchar *text = NULL;
12714
12715   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
12716
12717   g_object_get (G_OBJECT (widget), "tooltip-text", &text, NULL);
12718
12719   return text;
12720 }
12721
12722 /**
12723  * gtk_widget_set_tooltip_markup:
12724  * @widget: a #GtkWidget
12725  * @markup: (allow-none): the contents of the tooltip for @widget, or %NULL
12726  *
12727  * Sets @markup as the contents of the tooltip, which is marked up with
12728  *  the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
12729  *
12730  * This function will take care of setting #GtkWidget:has-tooltip to %TRUE
12731  * and of the default handler for the #GtkWidget::query-tooltip signal.
12732  *
12733  * See also the #GtkWidget:tooltip-markup property and
12734  * gtk_tooltip_set_markup().
12735  *
12736  * Since: 2.12
12737  */
12738 void
12739 gtk_widget_set_tooltip_markup (GtkWidget   *widget,
12740                                const gchar *markup)
12741 {
12742   g_return_if_fail (GTK_IS_WIDGET (widget));
12743
12744   g_object_set (G_OBJECT (widget), "tooltip-markup", markup, NULL);
12745 }
12746
12747 /**
12748  * gtk_widget_get_tooltip_markup:
12749  * @widget: a #GtkWidget
12750  *
12751  * Gets the contents of the tooltip for @widget.
12752  *
12753  * Return value: the tooltip text, or %NULL. You should free the
12754  *   returned string with g_free() when done.
12755  *
12756  * Since: 2.12
12757  */
12758 gchar *
12759 gtk_widget_get_tooltip_markup (GtkWidget *widget)
12760 {
12761   gchar *text = NULL;
12762
12763   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
12764
12765   g_object_get (G_OBJECT (widget), "tooltip-markup", &text, NULL);
12766
12767   return text;
12768 }
12769
12770 /**
12771  * gtk_widget_set_has_tooltip:
12772  * @widget: a #GtkWidget
12773  * @has_tooltip: whether or not @widget has a tooltip.
12774  *
12775  * Sets the has-tooltip property on @widget to @has_tooltip.  See
12776  * #GtkWidget:has-tooltip for more information.
12777  *
12778  * Since: 2.12
12779  */
12780 void
12781 gtk_widget_set_has_tooltip (GtkWidget *widget,
12782                             gboolean   has_tooltip)
12783 {
12784   g_return_if_fail (GTK_IS_WIDGET (widget));
12785
12786   g_object_set (G_OBJECT (widget), "has-tooltip", has_tooltip, NULL);
12787 }
12788
12789 /**
12790  * gtk_widget_get_has_tooltip:
12791  * @widget: a #GtkWidget
12792  *
12793  * Returns the current value of the has-tooltip property.  See
12794  * #GtkWidget:has-tooltip for more information.
12795  *
12796  * Return value: current value of has-tooltip on @widget.
12797  *
12798  * Since: 2.12
12799  */
12800 gboolean
12801 gtk_widget_get_has_tooltip (GtkWidget *widget)
12802 {
12803   gboolean has_tooltip = FALSE;
12804
12805   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
12806
12807   g_object_get (G_OBJECT (widget), "has-tooltip", &has_tooltip, NULL);
12808
12809   return has_tooltip;
12810 }
12811
12812 /**
12813  * gtk_widget_get_allocation:
12814  * @widget: a #GtkWidget
12815  * @allocation: (out): a pointer to a #GtkAllocation to copy to
12816  *
12817  * Retrieves the widget's allocation.
12818  *
12819  * Note, when implementing a #GtkContainer: a widget's allocation will
12820  * be its "adjusted" allocation, that is, the widget's parent
12821  * container typically calls gtk_widget_size_allocate() with an
12822  * allocation, and that allocation is then adjusted (to handle margin
12823  * and alignment for example) before assignment to the widget.
12824  * gtk_widget_get_allocation() returns the adjusted allocation that
12825  * was actually assigned to the widget. The adjusted allocation is
12826  * guaranteed to be completely contained within the
12827  * gtk_widget_size_allocate() allocation, however. So a #GtkContainer
12828  * is guaranteed that its children stay inside the assigned bounds,
12829  * but not that they have exactly the bounds the container assigned.
12830  * There is no way to get the original allocation assigned by
12831  * gtk_widget_size_allocate(), since it isn't stored; if a container
12832  * implementation needs that information it will have to track it itself.
12833  *
12834  * Since: 2.18
12835  */
12836 void
12837 gtk_widget_get_allocation (GtkWidget     *widget,
12838                            GtkAllocation *allocation)
12839 {
12840   GtkWidgetPrivate *priv;
12841
12842   g_return_if_fail (GTK_IS_WIDGET (widget));
12843   g_return_if_fail (allocation != NULL);
12844
12845   priv = widget->priv;
12846
12847   *allocation = priv->allocation;
12848 }
12849
12850 /**
12851  * gtk_widget_set_allocation:
12852  * @widget: a #GtkWidget
12853  * @allocation: a pointer to a #GtkAllocation to copy from
12854  *
12855  * Sets the widget's allocation.  This should not be used
12856  * directly, but from within a widget's size_allocate method.
12857  *
12858  * The allocation set should be the "adjusted" or actual
12859  * allocation. If you're implementing a #GtkContainer, you want to use
12860  * gtk_widget_size_allocate() instead of gtk_widget_set_allocation().
12861  * The GtkWidgetClass::adjust_size_allocation virtual method adjusts the
12862  * allocation inside gtk_widget_size_allocate() to create an adjusted
12863  * allocation.
12864  *
12865  * Since: 2.18
12866  */
12867 void
12868 gtk_widget_set_allocation (GtkWidget           *widget,
12869                            const GtkAllocation *allocation)
12870 {
12871   GtkWidgetPrivate *priv;
12872
12873   g_return_if_fail (GTK_IS_WIDGET (widget));
12874   g_return_if_fail (allocation != NULL);
12875
12876   priv = widget->priv;
12877
12878   priv->allocation = *allocation;
12879 }
12880
12881 /**
12882  * gtk_widget_get_allocated_width:
12883  * @widget: the widget to query
12884  *
12885  * Returns the width that has currently been allocated to @widget.
12886  * This function is intended to be used when implementing handlers
12887  * for the #GtkWidget::draw function.
12888  *
12889  * Returns: the width of the @widget
12890  **/
12891 int
12892 gtk_widget_get_allocated_width (GtkWidget *widget)
12893 {
12894   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
12895
12896   return widget->priv->allocation.width;
12897 }
12898
12899 /**
12900  * gtk_widget_get_allocated_height:
12901  * @widget: the widget to query
12902  *
12903  * Returns the height that has currently been allocated to @widget.
12904  * This function is intended to be used when implementing handlers
12905  * for the #GtkWidget::draw function.
12906  *
12907  * Returns: the height of the @widget
12908  **/
12909 int
12910 gtk_widget_get_allocated_height (GtkWidget *widget)
12911 {
12912   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
12913
12914   return widget->priv->allocation.height;
12915 }
12916
12917 /**
12918  * gtk_widget_get_requisition:
12919  * @widget: a #GtkWidget
12920  * @requisition: (out): a pointer to a #GtkRequisition to copy to
12921  *
12922  * Retrieves the widget's requisition.
12923  *
12924  * This function should only be used by widget implementations in
12925  * order to figure whether the widget's requisition has actually
12926  * changed after some internal state change (so that they can call
12927  * gtk_widget_queue_resize() instead of gtk_widget_queue_draw()).
12928  *
12929  * Normally, gtk_widget_size_request() should be used.
12930  *
12931  * Since: 2.20
12932  *
12933  * Deprecated: 3.0: The #GtkRequisition cache on the widget was
12934  * removed, If you need to cache sizes across requests and allocations,
12935  * add an explicit cache to the widget in question instead.
12936  */
12937 void
12938 gtk_widget_get_requisition (GtkWidget      *widget,
12939                             GtkRequisition *requisition)
12940 {
12941   g_return_if_fail (GTK_IS_WIDGET (widget));
12942   g_return_if_fail (requisition != NULL);
12943
12944   gtk_widget_get_preferred_size (widget, requisition, NULL);
12945 }
12946
12947 /**
12948  * gtk_widget_set_window:
12949  * @widget: a #GtkWidget
12950  * @window: a #GdkWindow
12951  *
12952  * Sets a widget's window. This function should only be used in a
12953  * widget's #GtkWidget::realize implementation. The %window passed is
12954  * usually either new window created with gdk_window_new(), or the
12955  * window of its parent widget as returned by
12956  * gtk_widget_get_parent_window().
12957  *
12958  * Widgets must indicate whether they will create their own #GdkWindow
12959  * by calling gtk_widget_set_has_window(). This is usually done in the
12960  * widget's init() function.
12961  *
12962  * <note><para>This function does not add any reference to @window.</para></note>
12963  *
12964  * Since: 2.18
12965  */
12966 void
12967 gtk_widget_set_window (GtkWidget *widget,
12968                        GdkWindow *window)
12969 {
12970   GtkWidgetPrivate *priv;
12971
12972   g_return_if_fail (GTK_IS_WIDGET (widget));
12973   g_return_if_fail (window == NULL || GDK_IS_WINDOW (window));
12974
12975   priv = widget->priv;
12976
12977   if (priv->window != window)
12978     {
12979       priv->window = window;
12980       g_object_notify (G_OBJECT (widget), "window");
12981     }
12982 }
12983
12984 /**
12985  * gtk_widget_get_window:
12986  * @widget: a #GtkWidget
12987  *
12988  * Returns the widget's window if it is realized, %NULL otherwise
12989  *
12990  * Return value: (transfer none): @widget's window.
12991  *
12992  * Since: 2.14
12993  */
12994 GdkWindow*
12995 gtk_widget_get_window (GtkWidget *widget)
12996 {
12997   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
12998
12999   return widget->priv->window;
13000 }
13001
13002 /**
13003  * gtk_widget_get_support_multidevice:
13004  * @widget: a #GtkWidget
13005  *
13006  * Returns %TRUE if @widget is multiple pointer aware. See
13007  * gtk_widget_set_support_multidevice() for more information.
13008  *
13009  * Returns: %TRUE is @widget is multidevice aware.
13010  **/
13011 gboolean
13012 gtk_widget_get_support_multidevice (GtkWidget *widget)
13013 {
13014   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
13015
13016   return widget->priv->multidevice;
13017 }
13018
13019 /**
13020  * gtk_widget_set_support_multidevice:
13021  * @widget: a #GtkWidget
13022  * @support_multidevice: %TRUE to support input from multiple devices.
13023  *
13024  * Enables or disables multiple pointer awareness. If this setting is %TRUE,
13025  * @widget will start receiving multiple, per device enter/leave events. Note
13026  * that if custom #GdkWindow<!-- -->s are created in #GtkWidget::realize,
13027  * gdk_window_set_support_multidevice() will have to be called manually on them.
13028  *
13029  * Since: 3.0
13030  **/
13031 void
13032 gtk_widget_set_support_multidevice (GtkWidget *widget,
13033                                     gboolean   support_multidevice)
13034 {
13035   GtkWidgetPrivate *priv;
13036
13037   g_return_if_fail (GTK_IS_WIDGET (widget));
13038
13039   priv = widget->priv;
13040
13041   if (support_multidevice)
13042     {
13043       priv->multidevice = TRUE;
13044       gtk_widget_set_extension_events (widget, GDK_EXTENSION_EVENTS_ALL);
13045     }
13046   else
13047     {
13048       priv->multidevice = FALSE;
13049       gtk_widget_set_extension_events (widget, GDK_EXTENSION_EVENTS_NONE);
13050     }
13051
13052   if (gtk_widget_get_realized (widget))
13053     gdk_window_set_support_multidevice (priv->window, support_multidevice);
13054 }
13055
13056 static void
13057 _gtk_widget_set_has_focus (GtkWidget *widget,
13058                            gboolean   has_focus)
13059 {
13060   widget->priv->has_focus = has_focus;
13061 }
13062
13063 /**
13064  * gtk_widget_send_focus_change:
13065  * @widget: a #GtkWidget
13066  * @event: a #GdkEvent of type GDK_FOCUS_CHANGE
13067  *
13068  * Sends the focus change @event to @widget
13069  *
13070  * This function is not meant to be used by applications. The only time it
13071  * should be used is when it is necessary for a #GtkWidget to assign focus
13072  * to a widget that is semantically owned by the first widget even though
13073  * it's not a direct child - for instance, a search entry in a floating
13074  * window similar to the quick search in #GtkTreeView.
13075  *
13076  * An example of its usage is:
13077  *
13078  * |[
13079  *   GdkEvent *fevent = gdk_event_new (GDK_FOCUS_CHANGE);
13080  *
13081  *   fevent->focus_change.type = GDK_FOCUS_CHANGE;
13082  *   fevent->focus_change.in = TRUE;
13083  *   fevent->focus_change.window = gtk_widget_get_window (widget);
13084  *   if (fevent->focus_change.window != NULL)
13085  *     g_object_ref (fevent->focus_change.window);
13086  *
13087  *   gtk_widget_send_focus_change (widget, fevent);
13088  *
13089  *   gdk_event_free (event);
13090  * ]|
13091  *
13092  * Return value: the return value from the event signal emission: %TRUE
13093  *   if the event was handled, and %FALSE otherwise
13094  *
13095  * Since: 2.20
13096  */
13097 gboolean
13098 gtk_widget_send_focus_change (GtkWidget *widget,
13099                               GdkEvent  *event)
13100 {
13101   gboolean res;
13102
13103   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
13104   g_return_val_if_fail (event != NULL && event->type == GDK_FOCUS_CHANGE, FALSE);
13105
13106   g_object_ref (widget);
13107
13108   _gtk_widget_set_has_focus (widget, event->focus_change.in);
13109
13110   res = gtk_widget_event (widget, event);
13111
13112   g_object_notify (G_OBJECT (widget), "has-focus");
13113
13114   g_object_unref (widget);
13115
13116   return res;
13117 }
13118
13119 /**
13120  * gtk_widget_in_destruction:
13121  * @widget: a #GtkWidget
13122  *
13123  * Returns whether the widget is currently being destroyed.
13124  * This information can sometimes be used to avoid doing
13125  * unnecessary work.
13126  *
13127  * Returns: %TRUE if @widget is being destroyed
13128  */
13129 gboolean
13130 gtk_widget_in_destruction (GtkWidget *widget)
13131 {
13132   return widget->priv->in_destruction;
13133 }
13134
13135 gboolean
13136 _gtk_widget_get_resize_pending (GtkWidget *widget)
13137 {
13138   return widget->priv->resize_pending;
13139 }
13140
13141 void
13142 _gtk_widget_set_resize_pending (GtkWidget *widget,
13143                                 gboolean   resize_pending)
13144 {
13145   widget->priv->resize_pending = resize_pending;
13146 }
13147
13148 gboolean
13149 _gtk_widget_get_in_reparent (GtkWidget *widget)
13150 {
13151   return widget->priv->in_reparent;
13152 }
13153
13154 void
13155 _gtk_widget_set_in_reparent (GtkWidget *widget,
13156                              gboolean   in_reparent)
13157 {
13158   widget->priv->in_reparent = in_reparent;
13159 }
13160
13161 gboolean
13162 _gtk_widget_get_anchored (GtkWidget *widget)
13163 {
13164   return widget->priv->anchored;
13165 }
13166
13167 void
13168 _gtk_widget_set_anchored (GtkWidget *widget,
13169                           gboolean   anchored)
13170 {
13171   widget->priv->anchored = anchored;
13172 }
13173
13174 gboolean
13175 _gtk_widget_get_shadowed (GtkWidget *widget)
13176 {
13177   return widget->priv->shadowed;
13178 }
13179
13180 void
13181 _gtk_widget_set_shadowed (GtkWidget *widget,
13182                           gboolean   shadowed)
13183 {
13184   widget->priv->shadowed = shadowed;
13185 }
13186
13187 gboolean
13188 _gtk_widget_get_alloc_needed (GtkWidget *widget)
13189 {
13190   return widget->priv->alloc_needed;
13191 }
13192
13193 void
13194 _gtk_widget_set_alloc_needed (GtkWidget *widget,
13195                               gboolean   alloc_needed)
13196 {
13197   widget->priv->alloc_needed = alloc_needed;
13198 }
13199
13200 gboolean
13201 _gtk_widget_get_width_request_needed (GtkWidget *widget)
13202 {
13203   return widget->priv->width_request_needed;
13204 }
13205
13206 void
13207 _gtk_widget_set_width_request_needed (GtkWidget *widget,
13208                                       gboolean   width_request_needed)
13209 {
13210   widget->priv->width_request_needed = width_request_needed;
13211 }
13212
13213 gboolean
13214 _gtk_widget_get_height_request_needed (GtkWidget *widget)
13215 {
13216   return widget->priv->height_request_needed;
13217 }
13218
13219 void
13220 _gtk_widget_set_height_request_needed (GtkWidget *widget,
13221                                        gboolean   height_request_needed)
13222 {
13223   widget->priv->height_request_needed = height_request_needed;
13224 }
13225
13226 GtkWidgetPath *
13227 gtk_widget_get_path (GtkWidget *widget)
13228 {
13229   GtkStyleContext *context;
13230   GtkWidgetPath *path;
13231   GtkWidget *parent;
13232
13233   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13234
13235   parent = widget->priv->parent;
13236
13237   path = gtk_widget_path_new ();
13238   gtk_widget_path_prepend_type (path, G_OBJECT_TYPE (widget));
13239
13240   if (widget->priv->name)
13241     gtk_widget_path_iter_set_name (path, 0, widget->priv->name);
13242
13243   context = g_object_get_qdata (G_OBJECT (widget),
13244                                 quark_style_context);
13245
13246   if (context)
13247     {
13248       GList *list, *l;
13249
13250       list = l = gtk_style_context_list_regions (context);
13251
13252       while (l)
13253         {
13254           GtkRegionFlags flags;
13255           const gchar *region_name;
13256
13257           region_name = l->data;
13258           l = l->next;
13259
13260           gtk_style_context_has_region (context, region_name, &flags);
13261           gtk_widget_path_iter_add_region (path, 0, region_name, flags);
13262         }
13263
13264       g_list_free (list);
13265
13266       list = l = gtk_style_context_list_classes (context);
13267
13268       while (l)
13269         {
13270           const gchar *class_name;
13271
13272           class_name = l->data;
13273           l = l->next;
13274
13275           gtk_widget_path_iter_add_class (path, 0, class_name);
13276         }
13277
13278       g_list_free (list);
13279     }
13280
13281   while (parent)
13282     {
13283       guint position;
13284
13285       position = gtk_widget_path_prepend_type (path, G_OBJECT_TYPE (parent));
13286
13287       if (parent->priv->name)
13288         gtk_widget_path_iter_set_name (path, position, parent->priv->name);
13289
13290       parent = parent->priv->parent;
13291     }
13292
13293   return path;
13294 }
13295
13296 GtkStyleContext *
13297 gtk_widget_get_style_context (GtkWidget *widget)
13298 {
13299   GtkStyleContext *context;
13300
13301   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
13302
13303   context = g_object_get_qdata (G_OBJECT (widget),
13304                                 quark_style_context);
13305
13306   if (G_UNLIKELY (!context))
13307     {
13308       static GtkCssProvider *css_provider = NULL;
13309       GtkCssProvider *default_provider;
13310       GtkWidgetPath *path;
13311       GtkSettings *settings;
13312
13313       context = g_object_new (GTK_TYPE_STYLE_CONTEXT,
13314                               "direction", gtk_widget_get_direction (widget),
13315                               NULL);
13316
13317
13318       g_object_set_qdata_full (G_OBJECT (widget),
13319                                quark_style_context, context,
13320                                (GDestroyNotify) g_object_unref);
13321
13322       /* Add provider for user file */
13323       if (G_UNLIKELY (!css_provider))
13324         {
13325           GFile *home_dir, *css_file;
13326           gchar *filename;
13327
13328           css_provider = gtk_css_provider_new ();
13329           home_dir = g_file_new_for_path (g_get_home_dir ());
13330
13331           filename = g_strdup_printf (".gtk-%d.0.css", GTK_MAJOR_VERSION);
13332           css_file = g_file_get_child (home_dir, filename);
13333           g_free (filename);
13334
13335           if (g_file_query_exists (css_file, NULL))
13336             gtk_css_provider_load_from_file (css_provider, css_file, NULL);
13337
13338           g_object_unref (home_dir);
13339           g_object_unref (css_file);
13340         }
13341
13342       gtk_style_context_add_provider (context,
13343                                       GTK_STYLE_PROVIDER (css_provider),
13344                                       GTK_STYLE_PROVIDER_PRIORITY_USER);
13345
13346       default_provider = gtk_css_provider_get_default ();
13347       gtk_style_context_add_provider (context,
13348                                       GTK_STYLE_PROVIDER (default_provider),
13349                                       GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
13350
13351       settings = gtk_widget_get_settings (widget);
13352       gtk_style_context_add_provider (context,
13353                                       GTK_STYLE_PROVIDER (settings),
13354                                       GTK_STYLE_PROVIDER_PRIORITY_SETTINGS);
13355
13356       path = gtk_widget_get_path (widget);
13357       gtk_style_context_set_path (context, path);
13358       gtk_widget_path_free (path);
13359
13360       gtk_style_context_set_screen (context,
13361                                     gtk_widget_get_screen (widget));
13362     }
13363
13364   return context;
13365 }