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