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