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