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