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