]> Pileus Git - ~andy/gtk/blob - gtk/gtkwidget.c
Mark abstract types as G_TYPE_FLAG_ABSTRACT. (#72383)
[~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 <stdarg.h>
28 #include <string.h>
29 #include <locale.h>
30 #include "gtkcontainer.h"
31 #include "gtkaccelmap.h"
32 #include "gtkclipboard.h"
33 #include "gtkiconfactory.h"
34 #include "gtkintl.h"
35 #include "gtkmain.h"
36 #include "gtkmarshalers.h"
37 #include "gtkrc.h"
38 #include "gtkselection.h"
39 #include "gtksettings.h"
40 #include "gtksizegroup.h"
41 #include "gtksignal.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 "gtkintl.h"
52 #include "gtkaccessible.h"
53 #include "gtktooltips.h"
54 #include "gtkinvisible.h"
55
56 #define WIDGET_CLASS(w)  GTK_WIDGET_GET_CLASS (w)
57 #define INIT_PATH_SIZE  (512)
58
59
60 enum {
61   SHOW,
62   HIDE,
63   MAP,
64   UNMAP,
65   REALIZE,
66   UNREALIZE,
67   SIZE_REQUEST,
68   SIZE_ALLOCATE,
69   STATE_CHANGED,
70   PARENT_SET,
71   HIERARCHY_CHANGED,
72   STYLE_SET,
73   DIRECTION_CHANGED,
74   GRAB_NOTIFY,
75   CHILD_NOTIFY,
76   MNEMONIC_ACTIVATE,
77   GRAB_FOCUS,
78   FOCUS,
79   EVENT,
80   EVENT_AFTER,
81   BUTTON_PRESS_EVENT,
82   BUTTON_RELEASE_EVENT,
83   SCROLL_EVENT,
84   MOTION_NOTIFY_EVENT,
85   DELETE_EVENT,
86   DESTROY_EVENT,
87   EXPOSE_EVENT,
88   KEY_PRESS_EVENT,
89   KEY_RELEASE_EVENT,
90   ENTER_NOTIFY_EVENT,
91   LEAVE_NOTIFY_EVENT,
92   CONFIGURE_EVENT,
93   FOCUS_IN_EVENT,
94   FOCUS_OUT_EVENT,
95   MAP_EVENT,
96   UNMAP_EVENT,
97   PROPERTY_NOTIFY_EVENT,
98   SELECTION_CLEAR_EVENT,
99   SELECTION_REQUEST_EVENT,
100   SELECTION_NOTIFY_EVENT,
101   SELECTION_GET,
102   SELECTION_RECEIVED,
103   PROXIMITY_IN_EVENT,
104   PROXIMITY_OUT_EVENT,
105   DRAG_BEGIN,
106   DRAG_END,
107   DRAG_DATA_DELETE,
108   DRAG_LEAVE,
109   DRAG_MOTION,
110   DRAG_DROP,
111   DRAG_DATA_GET,
112   DRAG_DATA_RECEIVED,
113   CLIENT_EVENT,
114   NO_EXPOSE_EVENT,
115   VISIBILITY_NOTIFY_EVENT,
116   WINDOW_STATE_EVENT,
117   POPUP_MENU,
118   SHOW_HELP,
119   ACCEL_CLOSURES_CHANGED,
120   LAST_SIGNAL
121 };
122
123 enum {
124   PROP_0,
125   PROP_NAME,
126   PROP_PARENT,
127   PROP_WIDTH_REQUEST,
128   PROP_HEIGHT_REQUEST,
129   PROP_VISIBLE,
130   PROP_SENSITIVE,
131   PROP_APP_PAINTABLE,
132   PROP_CAN_FOCUS,
133   PROP_HAS_FOCUS,
134   PROP_IS_FOCUS,
135   PROP_CAN_DEFAULT,
136   PROP_HAS_DEFAULT,
137   PROP_RECEIVES_DEFAULT,
138   PROP_COMPOSITE_CHILD,
139   PROP_STYLE,
140   PROP_EVENTS,
141   PROP_EXTENSION_EVENTS
142 };
143
144 typedef struct  _GtkStateData    GtkStateData;
145
146 struct _GtkStateData
147 {
148   GtkStateType  state;
149   guint         state_restoration : 1;
150   guint         parent_sensitive : 1;
151   guint         use_forall : 1;
152 };
153
154
155 /* --- prototypes --- */
156 static void     gtk_widget_class_init            (GtkWidgetClass    *klass);
157 static void     gtk_widget_init                  (GtkWidget         *widget);
158 static void     gtk_widget_set_property          (GObject           *object,
159                                                   guint              prop_id,
160                                                   const GValue      *value,
161                                                   GParamSpec        *pspec);
162 static void     gtk_widget_get_property          (GObject           *object,
163                                                   guint              prop_id,
164                                                   GValue            *value,
165                                                   GParamSpec        *pspec);
166 static void     gtk_widget_dispose               (GObject           *object);
167 static void     gtk_widget_real_destroy          (GtkObject         *object);
168 static void     gtk_widget_finalize              (GObject           *object);
169 static void     gtk_widget_real_show             (GtkWidget         *widget);
170 static void     gtk_widget_real_hide             (GtkWidget         *widget);
171 static void     gtk_widget_real_map              (GtkWidget         *widget);
172 static void     gtk_widget_real_unmap            (GtkWidget         *widget);
173 static void     gtk_widget_real_realize          (GtkWidget         *widget);
174 static void     gtk_widget_real_unrealize        (GtkWidget         *widget);
175 static void     gtk_widget_real_size_request     (GtkWidget         *widget,
176                                                   GtkRequisition    *requisition);
177 static void     gtk_widget_real_size_allocate    (GtkWidget         *widget,
178                                                   GtkAllocation     *allocation);
179 static void     gtk_widget_style_set             (GtkWidget         *widget,
180                                                   GtkStyle          *previous_style);
181 static void     gtk_widget_direction_changed     (GtkWidget         *widget,
182                                                   GtkTextDirection   previous_direction);
183
184 static void     gtk_widget_real_grab_focus       (GtkWidget         *focus_widget);
185 static gboolean gtk_widget_real_show_help        (GtkWidget         *widget,
186                                                   GtkWidgetHelpType  help_type);
187
188 static void     gtk_widget_dispatch_child_properties_changed    (GtkWidget        *object,
189                                                                  guint             n_pspecs,
190                                                                  GParamSpec      **pspecs);
191 static gboolean         gtk_widget_real_key_press_event         (GtkWidget        *widget,
192                                                                  GdkEventKey      *event);
193 static gboolean         gtk_widget_real_key_release_event       (GtkWidget        *widget,
194                                                                  GdkEventKey      *event);
195 static gboolean         gtk_widget_real_focus_in_event           (GtkWidget       *widget,
196                                                                   GdkEventFocus   *event);
197 static gboolean         gtk_widget_real_focus_out_event         (GtkWidget        *widget,
198                                                                  GdkEventFocus    *event);
199 static gboolean         gtk_widget_real_focus                   (GtkWidget        *widget,
200                                                                  GtkDirectionType  direction);
201 static PangoContext*    gtk_widget_peek_pango_context           (GtkWidget        *widget);
202 static void             gtk_widget_propagate_state              (GtkWidget        *widget,
203                                                                  GtkStateData     *data);
204 static void             gtk_widget_reset_rc_style               (GtkWidget        *widget);
205 static void             gtk_widget_set_style_internal           (GtkWidget        *widget,
206                                                                  GtkStyle         *style,
207                                                                  gboolean          initial_emission);
208 static gint             gtk_widget_event_internal               (GtkWidget        *widget,
209                                                                  GdkEvent         *event);
210 static gboolean         gtk_widget_real_mnemonic_activate       (GtkWidget        *widget,
211                                                                  gboolean          group_cycling);
212 static void             gtk_widget_aux_info_destroy             (GtkWidgetAuxInfo *aux_info);
213 static AtkObject*       gtk_widget_real_get_accessible          (GtkWidget        *widget);
214 static void             gtk_widget_accessible_interface_init    (AtkImplementorIface *iface);
215 static AtkObject*       gtk_widget_ref_accessible               (AtkImplementor *implementor);
216 static void             gtk_widget_invalidate_widget_windows    (GtkWidget        *widget,
217                                                                  GdkRegion        *region);
218
219
220 /* --- variables --- */
221 static gpointer         parent_class = NULL;
222 static guint            widget_signals[LAST_SIGNAL] = { 0 };
223 static GMemChunk       *aux_info_mem_chunk = NULL;
224 static GtkStyle        *gtk_default_style = NULL;
225 static GSList          *colormap_stack = NULL;
226 static guint            composite_child_stack = 0;
227 static GtkTextDirection gtk_default_direction = GTK_TEXT_DIR_LTR;
228 static GParamSpecPool  *style_property_spec_pool = NULL;
229
230 static GQuark           quark_property_parser = 0;
231 static GQuark           quark_aux_info = 0;
232 static GQuark           quark_accel_path = 0;
233 static GQuark           quark_accel_closures = 0;
234 static GQuark           quark_event_mask = 0;
235 static GQuark           quark_extension_event_mode = 0;
236 static GQuark           quark_parent_window = 0;
237 static GQuark           quark_shape_info = 0;
238 static GQuark           quark_colormap = 0;
239 static GQuark           quark_pango_context = 0;
240 static GQuark           quark_rc_style = 0;
241 static GQuark           quark_accessible_object = 0;
242 GParamSpecPool         *_gtk_widget_child_property_pool = NULL;
243 GObjectNotifyContext   *_gtk_widget_child_property_notify_context = NULL;
244
245 /* --- functions --- */
246 GtkType
247 gtk_widget_get_type (void)
248 {
249   static GtkType widget_type = 0;
250   
251   if (!widget_type)
252     {
253       static const GTypeInfo widget_info =
254       {
255         sizeof (GtkWidgetClass),
256         NULL,            /* base_init */
257         NULL,            /* base_finalize */
258         (GClassInitFunc) gtk_widget_class_init,
259         NULL,            /* class_finalize */
260         NULL,            /* class_data */
261         sizeof (GtkWidget),
262         0,               /* n_preallocs */
263         (GInstanceInitFunc) gtk_widget_init,
264         NULL,            /* value_table */
265       };
266       
267       static const GInterfaceInfo accessibility_info =
268       {
269         (GInterfaceInitFunc) gtk_widget_accessible_interface_init,
270         (GInterfaceFinalizeFunc) NULL,
271         NULL /* interface data */
272       };
273
274       widget_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkWidget", 
275                                             &widget_info, G_TYPE_FLAG_ABSTRACT);
276
277       g_type_add_interface_static (widget_type, ATK_TYPE_IMPLEMENTOR,
278                                    &accessibility_info) ;
279
280     }
281   
282   return widget_type;
283 }
284
285 static void
286 child_property_notify_dispatcher (GObject     *object,
287                                   guint        n_pspecs,
288                                   GParamSpec **pspecs)
289 {
290   GTK_WIDGET_GET_CLASS (object)->dispatch_child_properties_changed (GTK_WIDGET (object), n_pspecs, pspecs);
291 }
292
293 static void
294 gtk_widget_class_init (GtkWidgetClass *klass)
295 {
296   static GObjectNotifyContext cpn_context = { 0, NULL, NULL };
297   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
298   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
299   GtkBindingSet *binding_set;
300   
301   parent_class = g_type_class_peek_parent (klass);
302
303   quark_property_parser = g_quark_from_static_string ("gtk-rc-property-parser");
304   quark_aux_info = g_quark_from_static_string ("gtk-aux-info");
305   quark_accel_path = g_quark_from_static_string ("gtk-accel-path");
306   quark_accel_closures = g_quark_from_static_string ("gtk-accel-closures");
307   quark_event_mask = g_quark_from_static_string ("gtk-event-mask");
308   quark_extension_event_mode = g_quark_from_static_string ("gtk-extension-event-mode");
309   quark_parent_window = g_quark_from_static_string ("gtk-parent-window");
310   quark_shape_info = g_quark_from_static_string ("gtk-shape-info");
311   quark_colormap = g_quark_from_static_string ("gtk-colormap");
312   quark_pango_context = g_quark_from_static_string ("gtk-pango-context");
313   quark_rc_style = g_quark_from_static_string ("gtk-rc-style");
314   quark_accessible_object = g_quark_from_static_string ("gtk-accessible-object");
315
316   style_property_spec_pool = g_param_spec_pool_new (FALSE);
317   _gtk_widget_child_property_pool = g_param_spec_pool_new (TRUE);
318   cpn_context.quark_notify_queue = g_quark_from_static_string ("GtkWidget-child-property-notify-queue");
319   cpn_context.dispatcher = child_property_notify_dispatcher;
320   _gtk_widget_child_property_notify_context = &cpn_context;
321
322   gobject_class->dispose = gtk_widget_dispose;
323   gobject_class->finalize = gtk_widget_finalize;
324   gobject_class->set_property = gtk_widget_set_property;
325   gobject_class->get_property = gtk_widget_get_property;
326
327   object_class->destroy = gtk_widget_real_destroy;
328   
329   klass->activate_signal = 0;
330   klass->set_scroll_adjustments_signal = 0;
331   klass->dispatch_child_properties_changed = gtk_widget_dispatch_child_properties_changed;
332   klass->show = gtk_widget_real_show;
333   klass->show_all = gtk_widget_show;
334   klass->hide = gtk_widget_real_hide;
335   klass->hide_all = gtk_widget_hide;
336   klass->map = gtk_widget_real_map;
337   klass->unmap = gtk_widget_real_unmap;
338   klass->realize = gtk_widget_real_realize;
339   klass->unrealize = gtk_widget_real_unrealize;
340   klass->size_request = gtk_widget_real_size_request;
341   klass->size_allocate = gtk_widget_real_size_allocate;
342   klass->state_changed = NULL;
343   klass->parent_set = NULL;
344   klass->hierarchy_changed = NULL;
345   klass->style_set = gtk_widget_style_set;
346   klass->direction_changed = gtk_widget_direction_changed;
347   klass->grab_notify = NULL;
348   klass->child_notify = NULL;
349   klass->mnemonic_activate = gtk_widget_real_mnemonic_activate;
350   klass->grab_focus = gtk_widget_real_grab_focus;
351   klass->focus = gtk_widget_real_focus;
352   klass->event = NULL;
353   klass->button_press_event = NULL;
354   klass->button_release_event = NULL;
355   klass->motion_notify_event = NULL;
356   klass->delete_event = NULL;
357   klass->destroy_event = NULL;
358   klass->expose_event = NULL;
359   klass->key_press_event = gtk_widget_real_key_press_event;
360   klass->key_release_event = gtk_widget_real_key_release_event;
361   klass->enter_notify_event = NULL;
362   klass->leave_notify_event = NULL;
363   klass->configure_event = NULL;
364   klass->focus_in_event = gtk_widget_real_focus_in_event;
365   klass->focus_out_event = gtk_widget_real_focus_out_event;
366   klass->map_event = NULL;
367   klass->unmap_event = NULL;
368   klass->window_state_event = NULL;
369   klass->property_notify_event = gtk_selection_property_notify;
370   klass->selection_clear_event = gtk_selection_clear;
371   klass->selection_request_event = gtk_selection_request;
372   klass->selection_notify_event = gtk_selection_notify;
373   klass->selection_received = NULL;
374   klass->proximity_in_event = NULL;
375   klass->proximity_out_event = NULL;
376   klass->drag_begin = NULL;
377   klass->drag_end = NULL;
378   klass->drag_data_delete = NULL;
379   klass->drag_leave = NULL;
380   klass->drag_motion = NULL;
381   klass->drag_drop = NULL;
382   klass->drag_data_received = NULL;
383
384   klass->show_help = gtk_widget_real_show_help;
385   
386   /* Accessibility support */
387   klass->get_accessible = gtk_widget_real_get_accessible;
388
389   klass->no_expose_event = NULL;
390
391   g_object_class_install_property (gobject_class,
392                                    PROP_NAME,
393                                    g_param_spec_string ("name",
394                                                         _("Widget name"),
395                                                         _("The name of the widget"),
396                                                         NULL,
397                                                         G_PARAM_READWRITE));
398   g_object_class_install_property (gobject_class,
399                                    PROP_PARENT,
400                                    g_param_spec_object ("parent",
401                                                         _("Parent widget"), 
402                                                         _("The parent widget of this widget. Must be a Container widget"),
403                                                         GTK_TYPE_CONTAINER,
404                                                         G_PARAM_READWRITE));
405
406   g_object_class_install_property (gobject_class,
407                                    PROP_WIDTH_REQUEST,
408                                    g_param_spec_int ("width_request",
409                                                      _("Width request"),
410                                                      _("Override for width request of the widget, or -1 if natural request should be used"),
411                                                      -1,
412                                                      G_MAXINT,
413                                                      -1,
414                                                      G_PARAM_READWRITE));
415   g_object_class_install_property (gobject_class,
416                                    PROP_HEIGHT_REQUEST,
417                                    g_param_spec_int ("height_request",
418                                                      _("Height request"),
419                                                      _("Override for height request of the widget, or -1 if natural request should be used"),
420                                                      -1,
421                                                      G_MAXINT,
422                                                      -1,
423                                                      G_PARAM_READWRITE));
424   g_object_class_install_property (gobject_class,
425                                    PROP_VISIBLE,
426                                    g_param_spec_boolean ("visible",
427                                                          _("Visible"),
428                                                          _("Whether the widget is visible"),
429                                                          FALSE,
430                                                          G_PARAM_READWRITE));
431   g_object_class_install_property (gobject_class,
432                                    PROP_SENSITIVE,
433                                    g_param_spec_boolean ("sensitive",
434                                                          _("Sensitive"),
435                                                          _("Whether the widget responds to input"),
436                                                          TRUE,
437                                                          G_PARAM_READWRITE));
438   g_object_class_install_property (gobject_class,
439                                    PROP_APP_PAINTABLE,
440                                    g_param_spec_boolean ("app_paintable",
441                                                          _("Application paintable"),
442                                                          _("Whether the application will paint directly on the widget"),
443                                                          FALSE,
444                                                          G_PARAM_READWRITE));
445   g_object_class_install_property (gobject_class,
446                                    PROP_CAN_FOCUS,
447                                    g_param_spec_boolean ("can_focus",
448                                                          _("Can focus"),
449                                                          _("Whether the widget can accept the input focus"),
450                                                          FALSE,
451                                                          G_PARAM_READWRITE));
452   g_object_class_install_property (gobject_class,
453                                    PROP_HAS_FOCUS,
454                                    g_param_spec_boolean ("has_focus",
455                                                          _("Has focus"),
456                                                          _("Whether the widget has the input focus"),
457                                                          FALSE,
458                                                          G_PARAM_READWRITE));
459   g_object_class_install_property (gobject_class,
460                                    PROP_HAS_FOCUS,
461                                    g_param_spec_boolean ("is_focus",
462                                                          _("Is focus"),
463                                                          _("Whether the widget is the focus widget within the toplevel"),
464                                                          FALSE,
465                                                          G_PARAM_READWRITE));
466   g_object_class_install_property (gobject_class,
467                                    PROP_CAN_DEFAULT,
468                                    g_param_spec_boolean ("can_default",
469                                                          _("Can default"),
470                                                          _("Whether the widget can be the default widget"),
471                                                          FALSE,
472                                                          G_PARAM_READWRITE));
473   g_object_class_install_property (gobject_class,
474                                    PROP_HAS_DEFAULT,
475                                    g_param_spec_boolean ("has_default",
476                                                          _("Has default"),
477                                                          _("Whether the widget is the default widget"),
478                                                          FALSE,
479                                                          G_PARAM_READWRITE));
480   g_object_class_install_property (gobject_class,
481                                    PROP_RECEIVES_DEFAULT,
482                                    g_param_spec_boolean ("receives_default",
483                                                          _("Receives default"),
484                                                          _("If TRUE, the widget will receive the default action when it is focused"),
485                                                          FALSE,
486                                                          G_PARAM_READWRITE));
487   g_object_class_install_property (gobject_class,
488                                    PROP_COMPOSITE_CHILD,
489                                    g_param_spec_boolean ("composite_child",
490                                                          _("Composite child"),
491                                                          _("Whether the widget is part of a composite widget"),
492                                                          FALSE,
493                                                          G_PARAM_READABLE));
494   g_object_class_install_property (gobject_class,
495                                    PROP_STYLE,
496                                    g_param_spec_object ("style",
497                                                         _("Style"),
498                                                         _("The style of the widget, which contains information about how it will look (colors etc)"),
499                                                         GTK_TYPE_STYLE,
500                                                         G_PARAM_READWRITE));
501   g_object_class_install_property (gobject_class,
502                                    PROP_EVENTS,
503                                    g_param_spec_flags ("events",
504                                                        _("Events"),
505                                                        _("The event mask that decides what kind of GdkEvents this widget gets"),
506                                                        GDK_TYPE_EVENT_MASK,
507                                                        GDK_STRUCTURE_MASK,
508                                                        G_PARAM_READWRITE));
509   g_object_class_install_property (gobject_class,
510                                    PROP_EXTENSION_EVENTS,
511                                    g_param_spec_enum ("extension_events",
512                                                       _("Extension events"),
513                                                       _("The mask that decides what kind of extension events this widget gets"),
514                                                       GDK_TYPE_EXTENSION_MODE,
515                                                       GDK_EXTENSION_EVENTS_NONE,
516                                                       G_PARAM_READWRITE));
517   widget_signals[SHOW] =
518     gtk_signal_new ("show",
519                     GTK_RUN_FIRST,
520                     GTK_CLASS_TYPE (object_class),
521                     GTK_SIGNAL_OFFSET (GtkWidgetClass, show),
522                     _gtk_marshal_VOID__VOID,
523                     GTK_TYPE_NONE, 0);
524   widget_signals[HIDE] =
525     gtk_signal_new ("hide",
526                     GTK_RUN_FIRST,
527                     GTK_CLASS_TYPE (object_class),
528                     GTK_SIGNAL_OFFSET (GtkWidgetClass, hide),
529                     _gtk_marshal_VOID__VOID,
530                     GTK_TYPE_NONE, 0);
531   widget_signals[MAP] =
532     gtk_signal_new ("map",
533                     GTK_RUN_FIRST,
534                     GTK_CLASS_TYPE (object_class),
535                     GTK_SIGNAL_OFFSET (GtkWidgetClass, map),
536                     _gtk_marshal_VOID__VOID,
537                     GTK_TYPE_NONE, 0);
538   widget_signals[UNMAP] =
539     gtk_signal_new ("unmap",
540                     GTK_RUN_FIRST,
541                     GTK_CLASS_TYPE (object_class),
542                     GTK_SIGNAL_OFFSET (GtkWidgetClass, unmap),
543                     _gtk_marshal_VOID__VOID,
544                     GTK_TYPE_NONE, 0);
545   widget_signals[REALIZE] =
546     gtk_signal_new ("realize",
547                     GTK_RUN_FIRST,
548                     GTK_CLASS_TYPE (object_class),
549                     GTK_SIGNAL_OFFSET (GtkWidgetClass, realize),
550                     _gtk_marshal_VOID__VOID,
551                     GTK_TYPE_NONE, 0);
552   widget_signals[UNREALIZE] =
553     gtk_signal_new ("unrealize",
554                     GTK_RUN_LAST,
555                     GTK_CLASS_TYPE (object_class),
556                     GTK_SIGNAL_OFFSET (GtkWidgetClass, unrealize),
557                     _gtk_marshal_VOID__VOID,
558                     GTK_TYPE_NONE, 0);
559   widget_signals[SIZE_REQUEST] =
560     g_signal_new ("size_request",
561                    G_OBJECT_CLASS_TYPE (object_class),
562                    G_SIGNAL_RUN_FIRST,
563                    G_STRUCT_OFFSET (GtkWidgetClass, size_request),
564                    NULL, NULL,
565                    _gtk_marshal_VOID__BOXED,
566                    GTK_TYPE_NONE, 1,
567                    GTK_TYPE_REQUISITION | G_SIGNAL_TYPE_STATIC_SCOPE);
568   widget_signals[SIZE_ALLOCATE] = 
569     gtk_signal_new ("size_allocate",
570                     GTK_RUN_FIRST,
571                     GTK_CLASS_TYPE (object_class),
572                     GTK_SIGNAL_OFFSET (GtkWidgetClass, size_allocate),
573                     _gtk_marshal_VOID__BOXED,
574                     GTK_TYPE_NONE, 1,
575                     GDK_TYPE_RECTANGLE | G_SIGNAL_TYPE_STATIC_SCOPE);
576   widget_signals[STATE_CHANGED] =
577     gtk_signal_new ("state_changed",
578                     GTK_RUN_FIRST,
579                     GTK_CLASS_TYPE (object_class),
580                     GTK_SIGNAL_OFFSET (GtkWidgetClass, state_changed),
581                     _gtk_marshal_VOID__ENUM,
582                     GTK_TYPE_NONE, 1,
583                     GTK_TYPE_STATE_TYPE);
584   widget_signals[PARENT_SET] =
585     gtk_signal_new ("parent_set",
586                     GTK_RUN_FIRST,
587                     GTK_CLASS_TYPE (object_class),
588                     GTK_SIGNAL_OFFSET (GtkWidgetClass, parent_set),
589                     _gtk_marshal_VOID__OBJECT,
590                     GTK_TYPE_NONE, 1,
591                     GTK_TYPE_WIDGET);
592   widget_signals[HIERARCHY_CHANGED] =
593     gtk_signal_new ("hierarchy_changed",
594                     GTK_RUN_LAST,
595                     GTK_CLASS_TYPE (object_class),
596                     GTK_SIGNAL_OFFSET (GtkWidgetClass, hierarchy_changed),
597                     _gtk_marshal_VOID__OBJECT,
598                     GTK_TYPE_NONE, 1,
599                     GTK_TYPE_WIDGET);
600   widget_signals[STYLE_SET] =
601     gtk_signal_new ("style_set",
602                     GTK_RUN_FIRST,
603                     GTK_CLASS_TYPE (object_class),
604                     GTK_SIGNAL_OFFSET (GtkWidgetClass, style_set),
605                     _gtk_marshal_VOID__OBJECT,
606                     GTK_TYPE_NONE, 1,
607                     GTK_TYPE_STYLE);
608   widget_signals[DIRECTION_CHANGED] =
609     gtk_signal_new ("direction_changed",
610                     GTK_RUN_FIRST,
611                     GTK_CLASS_TYPE (object_class),
612                     GTK_SIGNAL_OFFSET (GtkWidgetClass, direction_changed),
613                     _gtk_marshal_VOID__ENUM,
614                     GTK_TYPE_NONE, 1,
615                     GTK_TYPE_TEXT_DIRECTION);
616   widget_signals[GRAB_NOTIFY] =
617     gtk_signal_new ("grab_notify",
618                     GTK_RUN_FIRST,
619                     GTK_CLASS_TYPE (object_class),
620                     GTK_SIGNAL_OFFSET (GtkWidgetClass, grab_notify),
621                     _gtk_marshal_VOID__BOOLEAN,
622                     GTK_TYPE_NONE, 1,
623                     GTK_TYPE_BOOL);
624   widget_signals[CHILD_NOTIFY] =
625     g_signal_new ("child_notify",
626                    G_TYPE_FROM_CLASS (klass),
627                    G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
628                    G_STRUCT_OFFSET (GtkWidgetClass, child_notify),
629                    NULL, NULL,
630                    g_cclosure_marshal_VOID__PARAM,
631                    G_TYPE_NONE,
632                    1, G_TYPE_PARAM);
633   widget_signals[MNEMONIC_ACTIVATE] =
634     g_signal_new ("mnemonic_activate",
635                   GTK_CLASS_TYPE (object_class),
636                   G_SIGNAL_RUN_LAST,
637                   GTK_SIGNAL_OFFSET (GtkWidgetClass, mnemonic_activate),
638                   _gtk_boolean_handled_accumulator, NULL,
639                   _gtk_marshal_BOOLEAN__BOOLEAN,
640                   GTK_TYPE_BOOL, 1,
641                   GTK_TYPE_BOOL);
642   widget_signals[GRAB_FOCUS] =
643     gtk_signal_new ("grab_focus",
644                     GTK_RUN_LAST | GTK_RUN_ACTION,
645                     GTK_CLASS_TYPE (object_class),
646                     GTK_SIGNAL_OFFSET (GtkWidgetClass, grab_focus),
647                     _gtk_marshal_VOID__VOID,
648                     GTK_TYPE_NONE, 0);
649   widget_signals[FOCUS] =
650     g_signal_new ("focus",
651                   G_TYPE_FROM_CLASS (object_class),
652                   G_SIGNAL_RUN_LAST,
653                   G_STRUCT_OFFSET (GtkWidgetClass, focus),
654                   _gtk_boolean_handled_accumulator, NULL,
655                   _gtk_marshal_BOOLEAN__ENUM,
656                   G_TYPE_BOOLEAN, 1,
657                   GTK_TYPE_DIRECTION_TYPE);
658   widget_signals[EVENT] =
659     g_signal_new ("event",
660                   G_TYPE_FROM_CLASS (object_class),
661                   G_SIGNAL_RUN_LAST,
662                   G_STRUCT_OFFSET(GtkWidgetClass, event),
663                   _gtk_boolean_handled_accumulator, NULL,
664                   _gtk_marshal_BOOLEAN__BOXED,
665                   G_TYPE_BOOLEAN, 1,
666                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
667   widget_signals[EVENT_AFTER] =
668     g_signal_new ("event-after",
669                   G_TYPE_FROM_CLASS (object_class),
670                   0,
671                   0,
672                   NULL, NULL,
673                   _gtk_marshal_VOID__BOXED,
674                   G_TYPE_NONE, 1, GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
675   widget_signals[BUTTON_PRESS_EVENT] =
676     g_signal_new ("button_press_event",
677                   G_TYPE_FROM_CLASS (object_class),
678                   G_SIGNAL_RUN_LAST,
679                   G_STRUCT_OFFSET(GtkWidgetClass, button_press_event),
680                   _gtk_boolean_handled_accumulator, NULL,
681                   _gtk_marshal_BOOLEAN__BOXED,
682                   G_TYPE_BOOLEAN, 1,
683                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
684   widget_signals[BUTTON_RELEASE_EVENT] =
685     g_signal_new ("button_release_event",
686                   G_TYPE_FROM_CLASS(object_class),
687                   G_SIGNAL_RUN_LAST,
688                   G_STRUCT_OFFSET(GtkWidgetClass, button_release_event),
689                   _gtk_boolean_handled_accumulator, NULL,
690                   _gtk_marshal_BOOLEAN__BOXED,
691                   G_TYPE_BOOLEAN, 1,
692                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
693   widget_signals[SCROLL_EVENT] =
694     g_signal_new ("scroll_event",
695                   G_TYPE_FROM_CLASS(object_class),
696                   G_SIGNAL_RUN_LAST,
697                   G_STRUCT_OFFSET(GtkWidgetClass, scroll_event),
698                   _gtk_boolean_handled_accumulator, NULL,
699                   _gtk_marshal_BOOLEAN__BOXED,
700                   G_TYPE_BOOLEAN, 1,
701                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
702   widget_signals[MOTION_NOTIFY_EVENT] =
703     g_signal_new ("motion_notify_event",
704                   G_TYPE_FROM_CLASS(object_class),
705                   G_SIGNAL_RUN_LAST,
706                   G_STRUCT_OFFSET(GtkWidgetClass, motion_notify_event),
707                   _gtk_boolean_handled_accumulator, NULL,
708                   _gtk_marshal_BOOLEAN__BOXED,
709                   G_TYPE_BOOLEAN, 1,
710                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
711   widget_signals[DELETE_EVENT] =
712     g_signal_new ("delete_event",
713                   G_TYPE_FROM_CLASS(object_class),
714                   G_SIGNAL_RUN_LAST,
715                   G_STRUCT_OFFSET(GtkWidgetClass, delete_event),
716                   _gtk_boolean_handled_accumulator, NULL,
717                   _gtk_marshal_BOOLEAN__BOXED,
718                   G_TYPE_BOOLEAN, 1,
719                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
720   widget_signals[DESTROY_EVENT] =
721     g_signal_new ("destroy_event",
722                   G_TYPE_FROM_CLASS(object_class),
723                   G_SIGNAL_RUN_LAST,
724                   G_STRUCT_OFFSET(GtkWidgetClass, destroy_event),
725                   _gtk_boolean_handled_accumulator, NULL,
726                   _gtk_marshal_BOOLEAN__BOXED,
727                   G_TYPE_BOOLEAN, 1,
728                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
729   widget_signals[EXPOSE_EVENT] =
730     g_signal_new ("expose_event",
731                   G_TYPE_FROM_CLASS(object_class),
732                   G_SIGNAL_RUN_LAST,
733                   G_STRUCT_OFFSET(GtkWidgetClass, expose_event),
734                   _gtk_boolean_handled_accumulator, NULL,
735                   _gtk_marshal_BOOLEAN__BOXED,
736                   G_TYPE_BOOLEAN, 1,
737                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
738   widget_signals[KEY_PRESS_EVENT] =
739     g_signal_new ("key_press_event",
740                   G_TYPE_FROM_CLASS(object_class),
741                   G_SIGNAL_RUN_LAST,
742                   G_STRUCT_OFFSET(GtkWidgetClass, key_press_event),
743                   _gtk_boolean_handled_accumulator, NULL,
744                   _gtk_marshal_BOOLEAN__BOXED,
745                   G_TYPE_BOOLEAN, 1,
746                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
747   widget_signals[KEY_RELEASE_EVENT] =
748     g_signal_new ("key_release_event",
749                   G_TYPE_FROM_CLASS(object_class),
750                   G_SIGNAL_RUN_LAST,
751                   G_STRUCT_OFFSET(GtkWidgetClass, key_release_event),
752                   _gtk_boolean_handled_accumulator, NULL,
753                   _gtk_marshal_BOOLEAN__BOXED,
754                   G_TYPE_BOOLEAN, 1,
755                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
756   widget_signals[ENTER_NOTIFY_EVENT] =
757     g_signal_new ("enter_notify_event",
758                   G_TYPE_FROM_CLASS(object_class),
759                   G_SIGNAL_RUN_LAST,
760                   G_STRUCT_OFFSET(GtkWidgetClass, enter_notify_event),
761                   _gtk_boolean_handled_accumulator, NULL,
762                   _gtk_marshal_BOOLEAN__BOXED,
763                   G_TYPE_BOOLEAN, 1,
764                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
765   widget_signals[LEAVE_NOTIFY_EVENT] =
766     g_signal_new ("leave_notify_event",
767                   G_TYPE_FROM_CLASS(object_class),
768                   G_SIGNAL_RUN_LAST,
769                   G_STRUCT_OFFSET(GtkWidgetClass, leave_notify_event),
770                   _gtk_boolean_handled_accumulator, NULL,
771                   _gtk_marshal_BOOLEAN__BOXED,
772                   G_TYPE_BOOLEAN, 1,
773                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
774   widget_signals[CONFIGURE_EVENT] =
775     g_signal_new ("configure_event",
776                   G_TYPE_FROM_CLASS(object_class),
777                   G_SIGNAL_RUN_LAST,
778                   G_STRUCT_OFFSET(GtkWidgetClass, configure_event),
779                   _gtk_boolean_handled_accumulator, NULL,
780                   _gtk_marshal_BOOLEAN__BOXED,
781                   G_TYPE_BOOLEAN, 1,
782                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
783   widget_signals[FOCUS_IN_EVENT] =
784     g_signal_new ("focus_in_event",
785                   G_TYPE_FROM_CLASS(object_class),
786                   G_SIGNAL_RUN_LAST,
787                   G_STRUCT_OFFSET(GtkWidgetClass, focus_in_event),
788                   _gtk_boolean_handled_accumulator, NULL,
789                   _gtk_marshal_BOOLEAN__BOXED,
790                   G_TYPE_BOOLEAN, 1,
791                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
792   widget_signals[FOCUS_OUT_EVENT] =
793     g_signal_new ("focus_out_event",
794                   G_TYPE_FROM_CLASS(object_class),
795                   G_SIGNAL_RUN_LAST,
796                   G_STRUCT_OFFSET(GtkWidgetClass, focus_out_event),
797                   _gtk_boolean_handled_accumulator, NULL,
798                   _gtk_marshal_BOOLEAN__BOXED,
799                   G_TYPE_BOOLEAN, 1,
800                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
801   widget_signals[MAP_EVENT] =
802     g_signal_new ("map_event",
803                   G_TYPE_FROM_CLASS(object_class),
804                   G_SIGNAL_RUN_LAST,
805                   G_STRUCT_OFFSET(GtkWidgetClass, map_event),
806                   _gtk_boolean_handled_accumulator, NULL,
807                   _gtk_marshal_BOOLEAN__BOXED,
808                   G_TYPE_BOOLEAN, 1,
809                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
810   widget_signals[UNMAP_EVENT] =
811     g_signal_new ("unmap_event",
812                   G_TYPE_FROM_CLASS(object_class),
813                   G_SIGNAL_RUN_LAST,
814                   G_STRUCT_OFFSET(GtkWidgetClass, unmap_event),
815                   _gtk_boolean_handled_accumulator, NULL,
816                   _gtk_marshal_BOOLEAN__BOXED,
817                   G_TYPE_BOOLEAN, 1,
818                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
819   widget_signals[PROPERTY_NOTIFY_EVENT] =
820     g_signal_new ("property_notify_event",
821                   G_TYPE_FROM_CLASS(object_class),
822                   G_SIGNAL_RUN_LAST,
823                   G_STRUCT_OFFSET(GtkWidgetClass, property_notify_event),
824                   _gtk_boolean_handled_accumulator, NULL,
825                   _gtk_marshal_BOOLEAN__BOXED,
826                   G_TYPE_BOOLEAN, 1,
827                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
828   widget_signals[SELECTION_CLEAR_EVENT] =
829     g_signal_new ("selection_clear_event",
830                   G_TYPE_FROM_CLASS(object_class),
831                   G_SIGNAL_RUN_LAST,
832                   G_STRUCT_OFFSET(GtkWidgetClass, selection_clear_event),
833                   _gtk_boolean_handled_accumulator, NULL,
834                   _gtk_marshal_BOOLEAN__BOXED,
835                   G_TYPE_BOOLEAN, 1,
836                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
837   widget_signals[SELECTION_REQUEST_EVENT] =
838     g_signal_new ("selection_request_event",
839                   G_TYPE_FROM_CLASS(object_class),
840                   G_SIGNAL_RUN_LAST,
841                   G_STRUCT_OFFSET(GtkWidgetClass, selection_request_event),
842                   _gtk_boolean_handled_accumulator, NULL,
843                   _gtk_marshal_BOOLEAN__BOXED,
844                   G_TYPE_BOOLEAN, 1,
845                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
846   widget_signals[SELECTION_NOTIFY_EVENT] =
847     g_signal_new ("selection_notify_event",
848                   G_TYPE_FROM_CLASS(object_class),
849                   G_SIGNAL_RUN_LAST,
850                   G_STRUCT_OFFSET(GtkWidgetClass, selection_notify_event),
851                   _gtk_boolean_handled_accumulator, NULL,
852                   _gtk_marshal_BOOLEAN__BOXED,
853                   G_TYPE_BOOLEAN, 1,
854                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
855   widget_signals[SELECTION_RECEIVED] =
856     g_signal_new ("selection_received",
857                   G_TYPE_FROM_CLASS (object_class),
858                   G_SIGNAL_RUN_LAST,
859                   G_STRUCT_OFFSET (GtkWidgetClass, selection_received),
860                   NULL, NULL,
861                   _gtk_marshal_VOID__BOXED_UINT,
862                   G_TYPE_NONE, 2,
863                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
864                   G_TYPE_UINT);
865   widget_signals[SELECTION_GET] =
866     g_signal_new ("selection_get",
867                   G_TYPE_FROM_CLASS (object_class),
868                   G_SIGNAL_RUN_LAST,
869                   G_STRUCT_OFFSET (GtkWidgetClass, selection_get),
870                   NULL, NULL,
871                   _gtk_marshal_VOID__BOXED_UINT_UINT,
872                   G_TYPE_NONE, 3,
873                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
874                   G_TYPE_UINT,
875                   G_TYPE_UINT);
876   widget_signals[PROXIMITY_IN_EVENT] =
877     g_signal_new ("proximity_in_event",
878                   G_TYPE_FROM_CLASS (object_class),
879                   G_SIGNAL_RUN_LAST,
880                   G_STRUCT_OFFSET(GtkWidgetClass, proximity_in_event),
881                   _gtk_boolean_handled_accumulator, NULL,
882                   _gtk_marshal_BOOLEAN__BOXED,
883                   G_TYPE_BOOLEAN, 1,
884                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
885   widget_signals[PROXIMITY_OUT_EVENT] =
886     g_signal_new ("proximity_out_event",
887                   G_TYPE_FROM_CLASS(object_class),
888                   G_SIGNAL_RUN_LAST,
889                   G_STRUCT_OFFSET(GtkWidgetClass, proximity_out_event),
890                   _gtk_boolean_handled_accumulator, NULL,
891                   _gtk_marshal_BOOLEAN__BOXED,
892                   G_TYPE_BOOLEAN, 1,
893                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
894   widget_signals[DRAG_LEAVE] =
895     gtk_signal_new ("drag_leave",
896                     GTK_RUN_LAST,
897                     GTK_CLASS_TYPE (object_class),
898                     GTK_SIGNAL_OFFSET (GtkWidgetClass, drag_leave),
899                     _gtk_marshal_VOID__OBJECT_UINT,
900                     GTK_TYPE_NONE, 2,
901                     GDK_TYPE_DRAG_CONTEXT,
902                     GTK_TYPE_UINT);
903   widget_signals[DRAG_BEGIN] =
904     gtk_signal_new ("drag_begin",
905                     GTK_RUN_LAST,
906                     GTK_CLASS_TYPE (object_class),
907                     GTK_SIGNAL_OFFSET (GtkWidgetClass, drag_begin),
908                     _gtk_marshal_VOID__OBJECT,
909                     GTK_TYPE_NONE, 1,
910                     GDK_TYPE_DRAG_CONTEXT);
911   widget_signals[DRAG_END] =
912     gtk_signal_new ("drag_end",
913                     GTK_RUN_LAST,
914                     GTK_CLASS_TYPE (object_class),
915                     GTK_SIGNAL_OFFSET (GtkWidgetClass, drag_end),
916                     _gtk_marshal_VOID__OBJECT,
917                     GTK_TYPE_NONE, 1,
918                     GDK_TYPE_DRAG_CONTEXT);
919   widget_signals[DRAG_DATA_DELETE] =
920     gtk_signal_new ("drag_data_delete",
921                     GTK_RUN_LAST,
922                     GTK_CLASS_TYPE (object_class),
923                     GTK_SIGNAL_OFFSET (GtkWidgetClass, drag_data_delete),
924                     _gtk_marshal_VOID__OBJECT,
925                     GTK_TYPE_NONE, 1,
926                     GDK_TYPE_DRAG_CONTEXT);
927   widget_signals[DRAG_MOTION] =
928     g_signal_new ("drag_motion",
929                   G_TYPE_FROM_CLASS(object_class),
930                   G_SIGNAL_RUN_LAST,
931                   G_STRUCT_OFFSET(GtkWidgetClass, drag_motion),
932                   _gtk_boolean_handled_accumulator, NULL,
933                   _gtk_marshal_BOOLEAN__OBJECT_INT_INT_UINT,
934                   G_TYPE_BOOLEAN, 4,
935                   GDK_TYPE_DRAG_CONTEXT,
936                   G_TYPE_INT,
937                   G_TYPE_INT,
938                   G_TYPE_UINT);
939   widget_signals[DRAG_DROP] =
940     g_signal_new ("drag_drop",
941                   G_TYPE_FROM_CLASS(object_class),
942                   G_SIGNAL_RUN_LAST,
943                   G_STRUCT_OFFSET(GtkWidgetClass, drag_drop),
944                   _gtk_boolean_handled_accumulator, NULL,
945                   _gtk_marshal_BOOLEAN__OBJECT_INT_INT_UINT,
946                   G_TYPE_BOOLEAN, 4,
947                   GDK_TYPE_DRAG_CONTEXT,
948                   G_TYPE_INT,
949                   G_TYPE_INT,
950                   G_TYPE_UINT);
951   widget_signals[DRAG_DATA_GET] =
952     g_signal_new ("drag_data_get",
953                   G_TYPE_FROM_CLASS (object_class),
954                   G_SIGNAL_RUN_LAST,
955                   G_STRUCT_OFFSET (GtkWidgetClass, drag_data_get),
956                   NULL, NULL,
957                   _gtk_marshal_VOID__OBJECT_BOXED_UINT_UINT,
958                   G_TYPE_NONE, 4,
959                   GDK_TYPE_DRAG_CONTEXT,
960                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
961                   G_TYPE_UINT,
962                   G_TYPE_UINT);
963   widget_signals[DRAG_DATA_RECEIVED] =
964     g_signal_new ("drag_data_received",
965                   G_TYPE_FROM_CLASS (object_class),
966                   G_SIGNAL_RUN_LAST,
967                   GTK_SIGNAL_OFFSET (GtkWidgetClass, drag_data_received),
968                   NULL, NULL,
969                   _gtk_marshal_VOID__OBJECT_INT_INT_BOXED_UINT_UINT,
970                   G_TYPE_NONE, 6,
971                   GDK_TYPE_DRAG_CONTEXT,
972                   G_TYPE_INT,
973                   G_TYPE_INT,
974                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
975                   G_TYPE_UINT,
976                   G_TYPE_UINT);
977   widget_signals[VISIBILITY_NOTIFY_EVENT] =
978     g_signal_new ("visibility_notify_event",
979                   G_TYPE_FROM_CLASS(object_class),
980                   G_SIGNAL_RUN_LAST,
981                   G_STRUCT_OFFSET(GtkWidgetClass, visibility_notify_event),
982                   _gtk_boolean_handled_accumulator, NULL,
983                   _gtk_marshal_BOOLEAN__BOXED,
984                   G_TYPE_BOOLEAN, 1,
985                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
986   widget_signals[CLIENT_EVENT] =
987     g_signal_new ("client_event",
988                   G_TYPE_FROM_CLASS(object_class),
989                   G_SIGNAL_RUN_LAST,
990                   G_STRUCT_OFFSET(GtkWidgetClass, client_event),
991                   _gtk_boolean_handled_accumulator, NULL,
992                   _gtk_marshal_BOOLEAN__BOXED,
993                   G_TYPE_BOOLEAN, 1,
994                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
995   widget_signals[NO_EXPOSE_EVENT] =
996     g_signal_new ("no_expose_event",
997                   G_TYPE_FROM_CLASS(object_class),
998                   G_SIGNAL_RUN_LAST,
999                   G_STRUCT_OFFSET(GtkWidgetClass, no_expose_event),
1000                   _gtk_boolean_handled_accumulator, NULL,
1001                   _gtk_marshal_BOOLEAN__BOXED,
1002                   G_TYPE_BOOLEAN, 1,
1003                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1004   widget_signals[WINDOW_STATE_EVENT] =
1005     g_signal_new ("window_state_event",
1006                   G_TYPE_FROM_CLASS(object_class),
1007                   G_SIGNAL_RUN_LAST,
1008                   G_STRUCT_OFFSET(GtkWidgetClass, window_state_event),
1009                   _gtk_boolean_handled_accumulator, NULL,
1010                   _gtk_marshal_BOOLEAN__BOXED,
1011                   G_TYPE_BOOLEAN, 1,
1012                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1013   widget_signals[POPUP_MENU] =
1014     g_signal_new ("popup_menu",
1015                   G_TYPE_FROM_CLASS (object_class),
1016                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1017                   GTK_SIGNAL_OFFSET (GtkWidgetClass, popup_menu),
1018                   _gtk_boolean_handled_accumulator, NULL,
1019                   _gtk_marshal_BOOLEAN__VOID,
1020                   G_TYPE_BOOLEAN, 0);
1021   widget_signals[SHOW_HELP] =
1022     g_signal_new ("show_help",
1023                   G_TYPE_FROM_CLASS (object_class),
1024                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1025                   GTK_SIGNAL_OFFSET (GtkWidgetClass, show_help),
1026                   _gtk_boolean_handled_accumulator, NULL,
1027                   _gtk_marshal_BOOLEAN__ENUM,
1028                   G_TYPE_BOOLEAN, 1, GTK_TYPE_WIDGET_HELP_TYPE);
1029   widget_signals[ACCEL_CLOSURES_CHANGED] =
1030     gtk_signal_new ("accel_closures_changed",
1031                     0,
1032                     GTK_CLASS_TYPE (object_class),
1033                     0,
1034                     gtk_marshal_NONE__NONE,
1035                     GTK_TYPE_NONE, 0);
1036   
1037   binding_set = gtk_binding_set_by_class (klass);
1038   gtk_binding_entry_add_signal (binding_set, GDK_F10, GDK_SHIFT_MASK,
1039                                 "popup_menu", 0);
1040   gtk_binding_entry_add_signal (binding_set, GDK_Menu, 0,
1041                                 "popup_menu", 0);  
1042
1043   gtk_binding_entry_add_signal (binding_set, GDK_F1, GDK_CONTROL_MASK,
1044                                 "show_help", 1,
1045                                 GTK_TYPE_WIDGET_HELP_TYPE,
1046                                 GTK_WIDGET_HELP_TOOLTIP);
1047   gtk_binding_entry_add_signal (binding_set, GDK_KP_F1, GDK_CONTROL_MASK,
1048                                 "show_help", 1,
1049                                 GTK_TYPE_WIDGET_HELP_TYPE,
1050                                 GTK_WIDGET_HELP_TOOLTIP);
1051   gtk_binding_entry_add_signal (binding_set, GDK_F1, GDK_SHIFT_MASK,
1052                                 "show_help", 1,
1053                                 GTK_TYPE_WIDGET_HELP_TYPE,
1054                                 GTK_WIDGET_HELP_WHATS_THIS);  
1055   gtk_binding_entry_add_signal (binding_set, GDK_KP_F1, GDK_SHIFT_MASK,
1056                                 "show_help", 1,
1057                                 GTK_TYPE_WIDGET_HELP_TYPE,
1058                                 GTK_WIDGET_HELP_WHATS_THIS);
1059
1060   gtk_widget_class_install_style_property (klass,
1061                                            g_param_spec_boolean ("interior_focus",
1062                                                                  _("Interior Focus"),
1063                                                                  _("Whether to draw the focus indicator inside widgets"),
1064                                                                  TRUE,
1065                                                                  G_PARAM_READABLE));
1066
1067   gtk_widget_class_install_style_property (klass,
1068                                            g_param_spec_int ("focus-line-width",
1069                                                              _("Focus linewidth"),
1070                                                              _("Width, in pixels, of the focus indicator line"),
1071                                                              0, G_MAXINT, 1,
1072                                                              G_PARAM_READWRITE));
1073
1074   gtk_widget_class_install_style_property (klass,
1075                                            g_param_spec_string ("focus-line-pattern",
1076                                                                 _("Focus line dash pattern"),
1077                                                                 _("Dash pattern used to draw the focus indicator"),
1078                                                                 "\1\1",
1079                                                                 G_PARAM_READWRITE));
1080   gtk_widget_class_install_style_property (klass,
1081                                            g_param_spec_int ("focus-padding",
1082                                                              _("Focus padding"),
1083                                                              _("Width, in pixels, between focus indicator and the widget 'box'"),
1084                                                              0, G_MAXINT, 1,
1085                                                              G_PARAM_READWRITE));
1086   gtk_widget_class_install_style_property (klass,
1087                                            g_param_spec_boxed ("cursor-color",
1088                                                                _("Cursor color"),
1089                                                                _("Color with which to draw insertion cursor"),
1090                                                                GDK_TYPE_COLOR,
1091                                                                G_PARAM_READABLE));
1092   gtk_widget_class_install_style_property (klass,
1093                                            g_param_spec_boxed ("secondary-cursor-color",
1094                                                                _("Secondary cursor color"),
1095                                                                _("Color with which to draw the secondary insertion cursor when editing mixed right-to-left and left-to-right text"),
1096                                                                GDK_TYPE_COLOR,
1097                                                                G_PARAM_READABLE));
1098   gtk_widget_class_install_style_property (klass,
1099                                            g_param_spec_float ("cursor-aspect-ratio",
1100                                                                _("Cursor line aspect ratio"),
1101                                                                _("Aspect ratio with which to draw insertion cursor"),
1102                                                                0.0, 1.0, 0.04,
1103                                                                G_PARAM_READABLE));
1104 }
1105
1106 static void
1107 gtk_widget_set_property (GObject         *object,
1108                          guint            prop_id,
1109                          const GValue    *value,
1110                          GParamSpec      *pspec)
1111 {
1112   GtkWidget *widget = GTK_WIDGET (object);
1113
1114   switch (prop_id)
1115     {
1116       guint32 saved_flags;
1117       
1118     case PROP_NAME:
1119       gtk_widget_set_name (widget, g_value_get_string (value));
1120       break;
1121     case PROP_PARENT:
1122       gtk_container_add (GTK_CONTAINER (g_value_get_object (value)), widget);
1123       break;
1124     case PROP_WIDTH_REQUEST:
1125       gtk_widget_set_usize (widget, g_value_get_int (value), -2);
1126       break;
1127     case PROP_HEIGHT_REQUEST:
1128       gtk_widget_set_usize (widget, -2, g_value_get_int (value));
1129       break;
1130     case PROP_VISIBLE:
1131       if (g_value_get_boolean (value))
1132         gtk_widget_show (widget);
1133       else
1134         gtk_widget_hide (widget);
1135       break;
1136     case PROP_SENSITIVE:
1137       gtk_widget_set_sensitive (widget, g_value_get_boolean (value));
1138       break;
1139     case PROP_APP_PAINTABLE:
1140       gtk_widget_set_app_paintable (widget, g_value_get_boolean (value));
1141       break;
1142     case PROP_CAN_FOCUS:
1143       saved_flags = GTK_WIDGET_FLAGS (widget);
1144       if (g_value_get_boolean (value))
1145         GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
1146       else
1147         GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
1148       if (saved_flags != GTK_WIDGET_FLAGS (widget))
1149         gtk_widget_queue_resize (widget);
1150       break;
1151     case PROP_HAS_FOCUS:
1152       if (g_value_get_boolean (value))
1153         gtk_widget_grab_focus (widget);
1154       break;
1155     case PROP_IS_FOCUS:
1156       if (g_value_get_boolean (value))
1157         gtk_widget_grab_focus (widget);
1158       break;
1159     case PROP_CAN_DEFAULT:
1160       saved_flags = GTK_WIDGET_FLAGS (widget);
1161       if (g_value_get_boolean (value))
1162         GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_DEFAULT);
1163       else
1164         GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_DEFAULT);
1165       if (saved_flags != GTK_WIDGET_FLAGS (widget))
1166         gtk_widget_queue_resize (widget);
1167       break;
1168     case PROP_HAS_DEFAULT:
1169       if (g_value_get_boolean (value))
1170         gtk_widget_grab_default (widget);
1171       break;
1172     case PROP_RECEIVES_DEFAULT:
1173       if (g_value_get_boolean (value))
1174         GTK_WIDGET_SET_FLAGS (widget, GTK_RECEIVES_DEFAULT);
1175       else
1176         GTK_WIDGET_UNSET_FLAGS (widget, GTK_RECEIVES_DEFAULT);
1177       break;
1178     case PROP_STYLE:
1179       gtk_widget_set_style (widget, g_value_get_object (value));
1180       break;
1181     case PROP_EVENTS:
1182       if (!GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_NO_WINDOW (widget))
1183         gtk_widget_set_events (widget, g_value_get_flags (value));
1184       break;
1185     case PROP_EXTENSION_EVENTS:
1186       gtk_widget_set_extension_events (widget, g_value_get_enum (value));
1187       break;
1188     default:
1189       break;
1190     }
1191 }
1192
1193 static void
1194 gtk_widget_get_property (GObject         *object,
1195                          guint            prop_id,
1196                          GValue          *value,
1197                          GParamSpec      *pspec)
1198 {
1199   GtkWidget *widget = GTK_WIDGET (object);
1200   
1201   switch (prop_id)
1202     {
1203       gint *eventp;
1204       GdkExtensionMode *modep;
1205
1206     case PROP_NAME:
1207       if (widget->name)
1208         g_value_set_string (value, widget->name);
1209       else
1210         g_value_set_string (value, "");
1211       break;
1212     case PROP_PARENT:
1213       if (widget->parent)
1214         g_value_set_object (value, widget->parent);
1215       else
1216         g_value_set_object (value, NULL);
1217       break;
1218     case PROP_WIDTH_REQUEST:
1219       {
1220         int w;
1221         gtk_widget_get_size_request (widget, &w, NULL);
1222         g_value_set_int (value, w);
1223       }
1224       break;
1225     case PROP_HEIGHT_REQUEST:
1226       {
1227         int h;
1228         gtk_widget_get_size_request (widget, NULL, &h);
1229         g_value_set_int (value, h);
1230       }
1231       break;
1232     case PROP_VISIBLE:
1233       g_value_set_boolean (value, (GTK_WIDGET_VISIBLE (widget) != FALSE));
1234       break;
1235     case PROP_SENSITIVE:
1236       g_value_set_boolean (value, (GTK_WIDGET_SENSITIVE (widget) != FALSE));
1237       break;
1238     case PROP_APP_PAINTABLE:
1239       g_value_set_boolean (value, (GTK_WIDGET_APP_PAINTABLE (widget) != FALSE));
1240       break;
1241     case PROP_CAN_FOCUS:
1242       g_value_set_boolean (value, (GTK_WIDGET_CAN_FOCUS (widget) != FALSE));
1243       break;
1244     case PROP_HAS_FOCUS:
1245       g_value_set_boolean (value, (GTK_WIDGET_HAS_FOCUS (widget) != FALSE));
1246       break;
1247     case PROP_IS_FOCUS:
1248       g_value_set_boolean (value, (gtk_widget_is_focus (widget)));
1249       break;
1250     case PROP_CAN_DEFAULT:
1251       g_value_set_boolean (value, (GTK_WIDGET_CAN_DEFAULT (widget) != FALSE));
1252       break;
1253     case PROP_HAS_DEFAULT:
1254       g_value_set_boolean (value, (GTK_WIDGET_HAS_DEFAULT (widget) != FALSE));
1255       break;
1256     case PROP_RECEIVES_DEFAULT:
1257       g_value_set_boolean (value, (GTK_WIDGET_RECEIVES_DEFAULT (widget) != FALSE));
1258       break;
1259     case PROP_COMPOSITE_CHILD:
1260       g_value_set_boolean (value, (GTK_WIDGET_COMPOSITE_CHILD (widget) != FALSE));
1261       break;
1262     case PROP_STYLE:
1263       g_value_set_object (value, gtk_widget_get_style (widget));
1264       break;
1265     case PROP_EVENTS:
1266       eventp = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_event_mask);
1267       if (!eventp)
1268         g_value_set_flags (value, 0);
1269       else
1270         g_value_set_flags (value, *eventp);
1271       break;
1272     case PROP_EXTENSION_EVENTS:
1273       modep = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_extension_event_mode);
1274       if (!modep)
1275         g_value_set_enum (value, 0);
1276       else
1277         g_value_set_enum (value, (GdkExtensionMode) *modep);
1278       break;
1279     default:
1280       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1281       break;
1282     }
1283 }
1284
1285 static void
1286 gtk_widget_init (GtkWidget *widget)
1287 {
1288   GTK_PRIVATE_FLAGS (widget) = PRIVATE_GTK_CHILD_VISIBLE;
1289   widget->state = GTK_STATE_NORMAL;
1290   widget->saved_state = GTK_STATE_NORMAL;
1291   widget->name = NULL;
1292   widget->requisition.width = 0;
1293   widget->requisition.height = 0;
1294   widget->allocation.x = -1;
1295   widget->allocation.y = -1;
1296   widget->allocation.width = 1;
1297   widget->allocation.height = 1;
1298   widget->window = NULL;
1299   widget->parent = NULL;
1300
1301   GTK_WIDGET_SET_FLAGS (widget,
1302                         GTK_SENSITIVE |
1303                         GTK_PARENT_SENSITIVE |
1304                         (composite_child_stack ? GTK_COMPOSITE_CHILD : 0) |
1305                         GTK_DOUBLE_BUFFERED);
1306
1307   GTK_PRIVATE_SET_FLAG (widget, GTK_REDRAW_ON_ALLOC);
1308   GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
1309   GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
1310
1311   widget->style = gtk_widget_get_default_style ();
1312   g_object_ref (widget->style);
1313 }
1314
1315
1316 static void
1317 gtk_widget_dispatch_child_properties_changed (GtkWidget   *widget,
1318                                               guint        n_pspecs,
1319                                               GParamSpec **pspecs)
1320 {
1321   GtkWidget *container = widget->parent;
1322   guint i;
1323
1324   for (i = 0; widget->parent == container && i < n_pspecs; i++)
1325     g_signal_emit (widget, widget_signals[CHILD_NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
1326 }
1327
1328 void
1329 gtk_widget_freeze_child_notify (GtkWidget *widget)
1330 {
1331   g_return_if_fail (GTK_IS_WIDGET (widget));
1332
1333   if (!G_OBJECT (widget)->ref_count)
1334     return;
1335
1336   g_object_ref (widget);
1337   g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
1338   g_object_unref (widget);
1339 }
1340
1341 void
1342 gtk_widget_child_notify (GtkWidget    *widget,
1343                          const gchar  *child_property)
1344 {
1345   GParamSpec *pspec;
1346
1347   g_return_if_fail (GTK_IS_WIDGET (widget));
1348   g_return_if_fail (child_property != NULL);
1349   if (!G_OBJECT (widget)->ref_count || !widget->parent)
1350     return;
1351
1352   g_object_ref (widget);
1353   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
1354                                     child_property,
1355                                     G_OBJECT_TYPE (widget->parent),
1356                                     TRUE);
1357   if (!pspec)
1358     g_warning ("%s: container class `%s' has no child property named `%s'",
1359                G_STRLOC,
1360                G_OBJECT_TYPE_NAME (widget->parent),
1361                child_property);
1362   else
1363     {
1364       GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
1365
1366       g_object_notify_queue_add (G_OBJECT (widget), nqueue, pspec);
1367       g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
1368     }
1369   g_object_unref (widget);
1370 }
1371
1372 /**
1373  * gtk_widget_thaw_child_notify:
1374  * @widget: a #GtkWidget
1375  *
1376  * Reverts the effect of a previous call to gtk_widget_freeze_child_notify().
1377  */ 
1378 void
1379 gtk_widget_thaw_child_notify (GtkWidget *widget)
1380 {
1381   GObjectNotifyQueue *nqueue;
1382
1383   g_return_if_fail (GTK_IS_WIDGET (widget));
1384
1385   if (!G_OBJECT (widget)->ref_count)
1386     return;
1387
1388   g_object_ref (widget);
1389   nqueue = g_object_notify_queue_from_object (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
1390   if (!nqueue || !nqueue->freeze_count)
1391     g_warning (G_STRLOC ": child-property-changed notification for %s(%p) is not frozen",
1392                G_OBJECT_TYPE_NAME (widget), widget);
1393   else
1394     g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
1395   g_object_unref (widget);
1396 }
1397
1398
1399 /**
1400  * gtk_widget_new:
1401  * @type: type ID of the widget to create
1402  * @first_property_name: name of first property to set
1403  * @Varargs: value of first property, followed by more properties, %NULL-terminated
1404  * 
1405  * This is a convenience function for creating a widget and setting
1406  * its properties in one go. For example you might write:
1407  * <literal>gtk_widget_new (GTK_TYPE_LABEL, "label", "Hello World", "xalign",
1408  * 0.0, NULL)</literal> to create a left-aligned label. Equivalent to
1409  * gtk_object_new(), but returns a widget so you don't have to
1410  * cast the object yourself.
1411  * 
1412  * Return value: a new #GtkWidget of type @widget_type
1413  **/
1414 GtkWidget*
1415 gtk_widget_new (GtkType      type,
1416                 const gchar *first_property_name,
1417                 ...)
1418 {
1419   GtkWidget *widget;
1420   va_list var_args;
1421   
1422   g_return_val_if_fail (gtk_type_is_a (type, GTK_TYPE_WIDGET), NULL);
1423   
1424   va_start (var_args, first_property_name);
1425   widget = (GtkWidget *)g_object_new_valist (type, first_property_name, var_args);
1426   va_end (var_args);
1427
1428   return widget;
1429 }
1430
1431 /**
1432  * gtk_widget_set:
1433  * @widget: a #GtkWidget
1434  * @first_property_name: name of first property to set
1435  * @Varargs: value of first property, followed by more properties, %NULL-terminated
1436  * 
1437  * Like gtk_object_set() - there's no reason to use this instead of
1438  * gtk_object_set().
1439  **/
1440 void
1441 gtk_widget_set (GtkWidget   *widget,
1442                 const gchar *first_property_name,
1443                 ...)
1444 {
1445   va_list var_args;
1446
1447   g_return_if_fail (GTK_IS_WIDGET (widget));
1448
1449   va_start (var_args, first_property_name);
1450   g_object_set_valist (G_OBJECT (widget), first_property_name, var_args);
1451   va_end (var_args);
1452 }
1453
1454 static inline void         
1455 gtk_widget_queue_clear_child (GtkWidget *widget)
1456 {
1457   GtkWidget *parent;
1458
1459   parent = widget->parent;
1460   if (parent && GTK_WIDGET_DRAWABLE (parent))
1461     gtk_widget_queue_clear_area (parent,
1462                                  widget->allocation.x,
1463                                  widget->allocation.y,
1464                                  widget->allocation.width,
1465                                  widget->allocation.height);
1466 }
1467
1468 /**
1469  * gtk_widget_unparent:
1470  * @widget: a #GtkWidget
1471  * 
1472  * This function is only for use in widget implementations.
1473  * Should be called by implementations of the remove method
1474  * on #GtkContainer, to dissociate a child from the container.
1475  **/
1476 void
1477 gtk_widget_unparent (GtkWidget *widget)
1478 {
1479   GObjectNotifyQueue *nqueue;
1480   GtkWidget *toplevel;
1481   GtkWidget *old_parent;
1482   
1483   g_return_if_fail (GTK_IS_WIDGET (widget));
1484   if (widget->parent == NULL)
1485     return;
1486   
1487   /* keep this function in sync with gtk_menu_detach()
1488    */
1489
1490   g_object_freeze_notify (G_OBJECT (widget));
1491   nqueue = g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
1492
1493   /* unset focused and default children properly, this code
1494    * should eventually move into some gtk_window_unparent_branch() or
1495    * similar function.
1496    */
1497   
1498   toplevel = gtk_widget_get_toplevel (widget);
1499   if (GTK_CONTAINER (widget->parent)->focus_child == widget)
1500     {
1501       gtk_container_set_focus_child (GTK_CONTAINER (widget->parent), NULL);
1502
1503       if (GTK_WIDGET_TOPLEVEL (toplevel))
1504         {
1505           GtkWidget *child;
1506       
1507           child = GTK_WINDOW (toplevel)->focus_widget;
1508           
1509           while (child && child != widget)
1510             child = child->parent;
1511           
1512           if (child == widget)
1513             gtk_window_set_focus (GTK_WINDOW (toplevel), NULL);
1514         }
1515     }
1516   if (GTK_WIDGET_TOPLEVEL (toplevel))
1517     {
1518       GtkWidget *child;
1519       
1520       child = GTK_WINDOW (toplevel)->default_widget;
1521       
1522       while (child && child != widget)
1523         child = child->parent;
1524       
1525       if (child == widget)
1526         gtk_window_set_default (GTK_WINDOW (toplevel), NULL);
1527     }
1528
1529   /* If we are unanchoring the child, we save around the toplevel
1530    * to emit hierarchy changed
1531    */
1532   if (GTK_WIDGET_ANCHORED (widget->parent))
1533     g_object_ref (toplevel);
1534   else
1535     toplevel = NULL;
1536
1537   gtk_widget_queue_clear_child (widget);
1538
1539   /* Reset the width and height here, to force reallocation if we
1540    * get added back to a new parent. This won't work if our new
1541    * allocation is smaller than 1x1 and we actually want a size of 1x1...
1542    * (would 0x0 be OK here?)
1543    */
1544   widget->allocation.width = 1;
1545   widget->allocation.height = 1;
1546   
1547   if (GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_IN_REPARENT (widget))
1548     gtk_widget_unrealize (widget);
1549
1550   /* Removing a widget from a container restores the child visible
1551    * flag to the default state, so it doesn't affect the child
1552    * in the next parent.
1553    */
1554   GTK_PRIVATE_SET_FLAG (widget, GTK_CHILD_VISIBLE);
1555     
1556   old_parent = widget->parent;
1557   widget->parent = NULL;
1558   gtk_widget_set_parent_window (widget, NULL);
1559   gtk_signal_emit (GTK_OBJECT (widget), widget_signals[PARENT_SET], old_parent);
1560   if (toplevel)
1561     {
1562       _gtk_widget_propagate_hierarchy_changed (widget, toplevel);
1563       g_object_unref (toplevel);
1564     }
1565       
1566   g_object_notify (G_OBJECT (widget), "parent");
1567   g_object_thaw_notify (G_OBJECT (widget));
1568   if (!widget->parent)
1569     g_object_notify_queue_clear (G_OBJECT (widget), nqueue);
1570   g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
1571   gtk_widget_unref (widget);
1572 }
1573
1574 /**
1575  * gtk_widget_destroy:
1576  * @widget: a #GtkWidget
1577  *
1578  * Destroys a widget. Equivalent to gtk_object_destroy(), except that
1579  * you don't have to cast the widget to #GtkObject. When a widget is
1580  * destroyed, it will break any references it holds to other objects.
1581  * If the widget is inside a container, the widget will be removed
1582  * from the container. If the widget is a toplevel (derived from
1583  * #GtkWindow), it will be removed from the list of toplevels, and the
1584  * reference GTK+ holds to it will be removed. Removing a
1585  * widget from its container or the list of toplevels results in the
1586  * widget being finalized, unless you've added additional references
1587  * to the widget with gtk_object_ref().
1588  *
1589  * In most cases, only toplevel widgets (windows) require explicit
1590  * destruction, because when you destroy a toplevel its children will
1591  * be destroyed as well.
1592  * 
1593  **/
1594 void
1595 gtk_widget_destroy (GtkWidget *widget)
1596 {
1597   g_return_if_fail (GTK_IS_WIDGET (widget));
1598
1599   gtk_object_destroy ((GtkObject*) widget);
1600 }
1601
1602 /**
1603  * gtk_widget_destroyed:
1604  * @widget: a #GtkWidget
1605  * @widget_pointer: address of a variable that contains @widget
1606  *
1607  * This function sets *@widget_pointer to %NULL if @widget_pointer !=
1608  * %NULL.  It's intended to be used as a callback connected to the
1609  * "destroy" signal of a widget. You connect gtk_widget_destroyed()
1610  * as a signal handler, and pass the address of your widget variable
1611  * as user data. Then when the widget is destroyed, the variable will
1612  * be set to %NULL. Useful for example to avoid multiple copies
1613  * of the same dialog.
1614  * 
1615  **/
1616 void
1617 gtk_widget_destroyed (GtkWidget      *widget,
1618                       GtkWidget      **widget_pointer)
1619 {
1620   /* Don't make any assumptions about the
1621    *  value of widget!
1622    *  Even check widget_pointer.
1623    */
1624   if (widget_pointer)
1625     *widget_pointer = NULL;
1626 }
1627
1628 /**
1629  * gtk_widget_show:
1630  * @widget: a #GtkWidget
1631  * 
1632  * Flags a widget to be displayed. Any widget that isn't shown will
1633  * not appear on the screen. If you want to show all the widgets in a
1634  * container, it's easier to call gtk_widget_show_all() on the
1635  * container, instead of individually showing the widgets.
1636  *
1637  * Remember that you have to show the containers containing a widget,
1638  * in addition to the widget itself, before it will appear onscreen.
1639  *
1640  * When a toplevel container is shown, it is immediately realized and
1641  * mapped; other shown widgets are realized and mapped when their
1642  * toplevel container is realized and mapped.
1643  * 
1644  **/
1645 void
1646 gtk_widget_show (GtkWidget *widget)
1647 {
1648   g_return_if_fail (GTK_IS_WIDGET (widget));
1649   
1650   if (!GTK_WIDGET_VISIBLE (widget))
1651     {
1652       g_object_ref (G_OBJECT (widget));
1653       if (!GTK_WIDGET_TOPLEVEL (widget))
1654         gtk_widget_queue_resize (widget);
1655       gtk_signal_emit (GTK_OBJECT (widget), widget_signals[SHOW]);
1656       g_object_notify (G_OBJECT (widget), "visible");
1657       g_object_unref (G_OBJECT (widget));
1658     }
1659 }
1660
1661 static void
1662 gtk_widget_real_show (GtkWidget *widget)
1663 {
1664   if (!GTK_WIDGET_VISIBLE (widget))
1665     {
1666       GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
1667
1668       if (widget->parent &&
1669           GTK_WIDGET_MAPPED (widget->parent) &&
1670           GTK_WIDGET_CHILD_VISIBLE (widget) &&
1671           !GTK_WIDGET_MAPPED (widget))
1672         gtk_widget_map (widget);
1673     }
1674 }
1675
1676 static void
1677 gtk_widget_show_map_callback (GtkWidget *widget, GdkEvent *event, gint *flag)
1678 {
1679   *flag = TRUE;
1680   gtk_signal_disconnect_by_data (GTK_OBJECT (widget), flag);
1681 }
1682
1683 /**
1684  * gtk_widget_show_now:
1685  * @widget: a #GtkWidget
1686  * 
1687  * Shows a widget. If the widget is an unmapped toplevel widget
1688  * (i.e. a #GtkWindow that has not yet been shown), enter the main
1689  * loop and wait for the window to actually be mapped. Be careful;
1690  * because the main loop is running, anything can happen during
1691  * this function.
1692  **/
1693 void
1694 gtk_widget_show_now (GtkWidget *widget)
1695 {
1696   gint flag = FALSE;
1697   
1698   g_return_if_fail (GTK_IS_WIDGET (widget));
1699
1700   /* make sure we will get event */
1701   if (!GTK_WIDGET_MAPPED (widget) &&
1702       GTK_WIDGET_TOPLEVEL (widget))
1703     {
1704       gtk_widget_show (widget);
1705
1706       gtk_signal_connect (GTK_OBJECT (widget), "map_event",
1707                           GTK_SIGNAL_FUNC (gtk_widget_show_map_callback), 
1708                           &flag);
1709
1710       while (!flag)
1711         gtk_main_iteration();
1712     }
1713   else
1714     gtk_widget_show (widget);
1715 }
1716
1717 /**
1718  * gtk_widget_hide:
1719  * @widget: a #GtkWidget
1720  * 
1721  * Reverses the effects of gtk_widget_show(), causing the widget to be
1722  * hidden (invisible to the user).
1723  **/
1724 void
1725 gtk_widget_hide (GtkWidget *widget)
1726 {
1727   g_return_if_fail (GTK_IS_WIDGET (widget));
1728   
1729   if (GTK_WIDGET_VISIBLE (widget))
1730     {
1731       gtk_widget_ref (widget);
1732       gtk_signal_emit (GTK_OBJECT (widget), widget_signals[HIDE]);
1733       if (!GTK_WIDGET_TOPLEVEL (widget))
1734         gtk_widget_queue_resize (widget);
1735       g_object_notify (G_OBJECT (widget), "visible");
1736       gtk_widget_unref (widget);
1737     }
1738 }
1739
1740 static void
1741 gtk_widget_real_hide (GtkWidget *widget)
1742 {
1743   if (GTK_WIDGET_VISIBLE (widget))
1744     {
1745       GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
1746       
1747       if (GTK_WIDGET_MAPPED (widget))
1748         gtk_widget_unmap (widget);
1749     }
1750 }
1751
1752 /**
1753  * gtk_widget_hide_on_delete:
1754  * @widget: a #GtkWidget
1755  * 
1756  * Utility function; intended to be connected to the "delete_event"
1757  * signal on a #GtkWindow. The function calls gtk_widget_hide() on its
1758  * argument, then returns %TRUE. If connected to "delete_event", the
1759  * result is that clicking the close button for a window (on the
1760  * window frame, top right corner usually) will hide but not destroy
1761  * the window. By default, GTK+ destroys windows when "delete_event"
1762  * is received.
1763  * 
1764  * Return value: %TRUE
1765  **/
1766 gboolean
1767 gtk_widget_hide_on_delete (GtkWidget      *widget)
1768 {
1769   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
1770   
1771   gtk_widget_hide (widget);
1772   
1773   return TRUE;
1774 }
1775
1776 /**
1777  * gtk_widget_show_all:
1778  * @widget: a #GtkWidget
1779  * 
1780  * Recursively shows a widget, and any child widgets (if the widget is
1781  * a container).
1782  **/
1783 void
1784 gtk_widget_show_all (GtkWidget *widget)
1785 {
1786   GtkWidgetClass *class;
1787
1788   g_return_if_fail (GTK_IS_WIDGET (widget));
1789
1790   class = GTK_WIDGET_GET_CLASS (widget);
1791
1792   if (class->show_all)
1793     class->show_all (widget);
1794 }
1795
1796 /**
1797  * gtk_widget_hide_all:
1798  * @widget: a #GtkWidget
1799  * 
1800  * Recursively hides a widget and any child widgets.
1801  **/
1802 void
1803 gtk_widget_hide_all (GtkWidget *widget)
1804 {
1805   GtkWidgetClass *class;
1806
1807   g_return_if_fail (GTK_IS_WIDGET (widget));
1808
1809   class = GTK_WIDGET_GET_CLASS (widget);
1810
1811   if (class->hide_all)
1812     class->hide_all (widget);
1813 }
1814
1815 /**
1816  * gtk_widget_map:
1817  * @widget: a #GtkWidget
1818  * 
1819  * This function is only for use in widget implementations. Causes
1820  * a widget to be mapped if it isn't already.
1821  * 
1822  **/
1823 void
1824 gtk_widget_map (GtkWidget *widget)
1825 {
1826   g_return_if_fail (GTK_IS_WIDGET (widget));
1827   g_return_if_fail (GTK_WIDGET_VISIBLE (widget));
1828   g_return_if_fail (GTK_WIDGET_CHILD_VISIBLE (widget));
1829   
1830   if (!GTK_WIDGET_MAPPED (widget))
1831     {
1832       if (!GTK_WIDGET_REALIZED (widget))
1833         gtk_widget_realize (widget);
1834
1835       gtk_signal_emit (GTK_OBJECT (widget), widget_signals[MAP]);
1836
1837       if (GTK_WIDGET_NO_WINDOW (widget))
1838         gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
1839     }
1840 }
1841
1842 /**
1843  * gtk_widget_unmap:
1844  * @widget: a #GtkWidget
1845  *
1846  * This function is only for use in widget implementations. Causes
1847  * a widget to be unmapped if it's currently mapped.
1848  * 
1849  **/
1850 void
1851 gtk_widget_unmap (GtkWidget *widget)
1852 {
1853   g_return_if_fail (GTK_IS_WIDGET (widget));
1854   
1855   if (GTK_WIDGET_MAPPED (widget))
1856     {
1857       if (GTK_WIDGET_NO_WINDOW (widget))
1858         gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
1859       gtk_signal_emit (GTK_OBJECT (widget), widget_signals[UNMAP]);
1860     }
1861 }
1862
1863 /**
1864  * gtk_widget_realize:
1865  * @widget: a #GtkWidget
1866  * 
1867  * Creates the GDK (windowing system) resources associated with a
1868  * widget.  For example, @widget->window will be created when a widget
1869  * is realized.  Normally realization happens implicitly; if you show
1870  * a widget and all its parent containers, then the widget will be
1871  * realized and mapped automatically.
1872  * 
1873  * Realizing a widget requires all
1874  * the widget's parent widgets to be realized; calling
1875  * gtk_widget_realize() realizes the widget's parents in addition to
1876  * @widget itself. If a widget is not yet inside a toplevel window
1877  * when you realize it, bad things will happen.
1878  *
1879  * This function is primarily used in widget implementations, and
1880  * isn't very useful otherwise. Many times when you think you might
1881  * need it, a better approach is to connect to a signal that will be
1882  * called after the widget is realized automatically, such as
1883  * "expose_event". Or simply gtk_signal_connect_after() to the
1884  * "realize" signal.
1885  *  
1886  **/
1887 void
1888 gtk_widget_realize (GtkWidget *widget)
1889 {
1890   gint events;
1891   GdkExtensionMode mode;
1892   GtkWidgetShapeInfo *shape_info;
1893   
1894   g_return_if_fail (GTK_IS_WIDGET (widget));
1895   
1896   if (!GTK_WIDGET_REALIZED (widget))
1897     {
1898       /*
1899         if (GTK_IS_CONTAINER (widget) && !GTK_WIDGET_NO_WINDOW (widget))
1900           g_message ("gtk_widget_realize(%s)", gtk_type_name (GTK_WIDGET_TYPE (widget)));
1901       */
1902
1903       if (widget->parent == NULL &&
1904           !GTK_WIDGET_TOPLEVEL (widget))
1905         g_warning ("Calling gtk_widget_realize() on a widget that isn't "
1906                    "inside a toplevel window is not going to work very well. "
1907                    "Widgets must be inside a toplevel container before realizing them.");
1908       
1909       if (widget->parent && !GTK_WIDGET_REALIZED (widget->parent))
1910         gtk_widget_realize (widget->parent);
1911
1912       gtk_widget_ensure_style (widget);
1913       
1914       gtk_signal_emit (GTK_OBJECT (widget), widget_signals[REALIZE]);
1915       
1916       if (GTK_WIDGET_HAS_SHAPE_MASK (widget))
1917         {
1918           shape_info = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_shape_info);
1919           gdk_window_shape_combine_mask (widget->window,
1920                                          shape_info->shape_mask,
1921                                          shape_info->offset_x,
1922                                          shape_info->offset_y);
1923         }
1924       
1925       if (!GTK_WIDGET_NO_WINDOW (widget))
1926         {
1927           mode = gtk_widget_get_extension_events (widget);
1928           if (mode != GDK_EXTENSION_EVENTS_NONE)
1929             {
1930               events = gtk_widget_get_events (widget);
1931               gdk_input_set_extension_events (widget->window, events, mode);
1932             }
1933         }
1934       
1935     }
1936 }
1937
1938 /**
1939  * gtk_widget_unrealize:
1940  * @widget: a #GtkWidget
1941  *
1942  * This function is only useful in widget implementations.
1943  * Causes a widget to be unrealized (frees all GDK resources
1944  * associated with the widget, such as @widget->window).
1945  * 
1946  **/
1947 void
1948 gtk_widget_unrealize (GtkWidget *widget)
1949 {
1950   g_return_if_fail (GTK_IS_WIDGET (widget));
1951
1952   if (GTK_WIDGET_HAS_SHAPE_MASK (widget))
1953     gtk_widget_shape_combine_mask (widget, NULL, -1, -1);
1954
1955   if (GTK_WIDGET_REALIZED (widget))
1956     {
1957       gtk_widget_ref (widget);
1958       gtk_signal_emit (GTK_OBJECT (widget), widget_signals[UNREALIZE]);
1959       GTK_WIDGET_UNSET_FLAGS (widget, GTK_REALIZED | GTK_MAPPED);
1960       gtk_widget_unref (widget);
1961     }
1962 }
1963
1964 /*****************************************
1965  * Draw queueing.
1966  *****************************************/
1967
1968 /**
1969  * gtk_widget_queue_draw_area:
1970  * @widget: a #GtkWidget
1971  * @x: x coordinate of upper-left corner of rectangle to redraw
1972  * @y: y coordinate of upper-left corner of rectangle to redraw
1973  * @width: width of region to draw
1974  * @height: height of region to draw
1975  *
1976  * Invalidates the rectangular area of @widget defined by @x, @y,
1977  * @width and @height by calling gdk_window_invalidate_rect() on the
1978  * widget's window and all its child windows.  Once the main loop
1979  * becomes idle (after the current batch of events has been processed,
1980  * roughly), the window will receive expose events for the union of
1981  * all regions that have been invalidated.
1982  *
1983  * Normally you would only use this function in widget
1984  * implementations. You might also use it, or
1985  * gdk_window_invalidate_rect() directly, to schedule a redraw of a
1986  * #GtkDrawingArea or some portion thereof.
1987  *
1988  * Frequently you can just call gdk_window_invalidate_rect() or
1989  * gdk_window_invalidate_region() instead of this function. Those
1990  * functions will invalidate only a single window, instead of the
1991  * widget and all its children.
1992  *
1993  * The advantage of adding to the invalidated region compared to
1994  * simply drawing immediately is efficiency; using an invalid region
1995  * ensures that you only have to redraw one time.
1996  * 
1997  **/
1998 void       
1999 gtk_widget_queue_draw_area (GtkWidget *widget,
2000                             gint       x,
2001                             gint       y,
2002                             gint       width,
2003                             gint       height)
2004 {
2005   g_return_if_fail (GTK_IS_WIDGET (widget));
2006
2007   gtk_widget_queue_clear_area (widget, x, y, width, height);
2008 }
2009
2010 /**
2011  * gtk_widget_queue_draw:
2012  * @widget: a #GtkWidget
2013  *
2014  * Equivalent to calling gtk_widget_queue_draw_area() for the
2015  * entire area of a widget.
2016  * 
2017  **/
2018 void       
2019 gtk_widget_queue_draw (GtkWidget *widget)
2020 {
2021   g_return_if_fail (GTK_IS_WIDGET (widget));
2022
2023   gtk_widget_queue_clear (widget);
2024 }
2025
2026 /* Invalidates the given area (allocation-relative-coordinates)
2027  * in all of the widget's windows
2028  */
2029 /**
2030  * gtk_widget_queue_clear_area:
2031  * @widget: a #GtkWidget
2032  * @x: x coordinate of upper-left corner of rectangle to redraw
2033  * @y: y coordinate of upper-left corner of rectangle to redraw
2034  * @width: width of region to draw
2035  * @height: height of region to draw
2036  * 
2037  * DEPRECATED. This function is no longer different from
2038  * gtk_widget_queue_draw_area(), though it once was. Now it just calls
2039  * gtk_widget_queue_draw_area(). Originally
2040  * gtk_widget_queue_clear_area() would force a redraw of the
2041  * background for %GTK_NO_WINDOW widgets, and
2042  * gtk_widget_queue_draw_area() would not. Now both functions ensure
2043  * the background will be redrawn.
2044  * 
2045  **/
2046 void       
2047 gtk_widget_queue_clear_area (GtkWidget *widget,
2048                              gint       x,
2049                              gint       y,
2050                              gint       width,
2051                              gint       height)
2052 {
2053   GdkRectangle invalid_rect;
2054   
2055   g_return_if_fail (GTK_IS_WIDGET (widget));
2056
2057   if (!(widget->window && gdk_window_is_viewable (widget->window)))
2058     return;
2059
2060   /* Find the correct widget */
2061
2062   if (!GTK_WIDGET_NO_WINDOW (widget))
2063     {
2064       if (widget->parent)
2065         {
2066           /* Translate widget relative to window-relative */
2067
2068           gint wx, wy, wwidth, wheight;
2069           
2070           gdk_window_get_position (widget->window, &wx, &wy);
2071           x -= wx - widget->allocation.x;
2072           y -= wy - widget->allocation.y;
2073           
2074           gdk_window_get_size (widget->window, &wwidth, &wheight);
2075
2076           if (x + width <= 0 || y + height <= 0 ||
2077               x >= wwidth || y >= wheight)
2078             return;
2079           
2080           if (x < 0)
2081             {
2082               width += x;  x = 0;
2083             }
2084           if (y < 0)
2085             {
2086               height += y; y = 0;
2087             }
2088           if (x + width > wwidth)
2089             width = wwidth - x;
2090           if (y + height > wheight)
2091             height = wheight - y;
2092         }
2093     }
2094
2095   invalid_rect.x = x;
2096   invalid_rect.y = y;
2097   invalid_rect.width = width;
2098   invalid_rect.height = height;
2099   
2100   gdk_window_invalidate_rect (widget->window, &invalid_rect, TRUE);
2101 }
2102
2103 /**
2104  * gtk_widget_queue_clear:
2105  * @widget: a #GtkWidget
2106  * 
2107  * DEPRECATED. Use gtk_widget_queue_draw() instead.
2108  **/
2109 void       
2110 gtk_widget_queue_clear (GtkWidget *widget)
2111 {
2112   g_return_if_fail (GTK_IS_WIDGET (widget));
2113
2114   if (widget->allocation.width || widget->allocation.height)
2115     {
2116       if (GTK_WIDGET_NO_WINDOW (widget))
2117         gtk_widget_queue_clear_area (widget, widget->allocation.x,
2118                                      widget->allocation.y,
2119                                      widget->allocation.width, 
2120                                      widget->allocation.height);
2121       else
2122         gtk_widget_queue_clear_area (widget, 0, 0, 
2123                                      widget->allocation.width, 
2124                                      widget->allocation.height);
2125     }
2126 }
2127
2128 /**
2129  * gtk_widget_queue_resize:
2130  * @widget: a #GtkWidget
2131  *
2132  * This function is only for use in widget implementations.
2133  * Flags a widget to have its size renegotiated; should
2134  * be called when a widget for some reason has a new size request.
2135  * For example, when you change the text in a #GtkLabel, #GtkLabel
2136  * queues a resize to ensure there's enough space for the new text.
2137  * 
2138  **/
2139 void
2140 gtk_widget_queue_resize (GtkWidget *widget)
2141 {
2142   GdkRegion *region;
2143   
2144   g_return_if_fail (GTK_IS_WIDGET (widget));
2145
2146   if (GTK_WIDGET_REALIZED (widget))
2147     {
2148       region = gdk_region_rectangle (&widget->allocation);
2149       gtk_widget_invalidate_widget_windows (widget, region);
2150       gdk_region_destroy (region);
2151     }
2152       
2153   _gtk_size_group_queue_resize (widget);
2154 }
2155
2156 /**
2157  * gtk_widget_draw:
2158  * @widget: a #GtkWidget
2159  * @area: area to draw
2160  *
2161  * DEPRECATED. In GTK+ 1.2, this function would immediately render the
2162  * region @area of a widget, by invoking the virtual draw method of a
2163  * widget. In GTK+ 2.0, the draw method is gone, and instead
2164  * gtk_widget_draw() simply invalidates the specified region of the
2165  * widget, then updates the invalid region of the widget immediately.
2166  * Usually you don't want to update the region immediately for
2167  * performance reasons, so in general gtk_widget_queue_draw_area() is
2168  * a better choice if you want to draw a region of a widget.
2169  * 
2170  **/
2171 void
2172 gtk_widget_draw (GtkWidget    *widget,
2173                  GdkRectangle *area)
2174 {
2175   g_return_if_fail (GTK_IS_WIDGET (widget));
2176
2177   if (GTK_WIDGET_DRAWABLE (widget))
2178     {
2179       if (area)
2180         gtk_widget_queue_draw_area (widget,
2181                                     area->x, area->y,
2182                                     area->width, area->height);
2183       else
2184         gtk_widget_queue_draw (widget);
2185
2186       gdk_window_process_updates (widget->window, TRUE);
2187     }
2188 }
2189
2190 /**
2191  * gtk_widget_size_request:
2192  * @widget: a #GtkWidget
2193  * @requisition: a #GtkRequisition to be filled in
2194  * 
2195  * This function is typically used when implementing a #GtkContainer
2196  * subclass.  Obtains the preferred size of a widget. The container
2197  * uses this information to arrange its child widgets and decide what
2198  * size allocations to give them with gtk_widget_size_allocate().
2199  *
2200  * You can also call this function from an application, with some
2201  * caveats. Most notably, getting a size request requires the widget
2202  * to be associated with a screen, because font information may be
2203  * needed. Multihead-aware applications should keep this in mind.
2204  *
2205  * Also remember that the size request is not necessarily the size
2206  * a widget will actually be allocated.
2207  *
2208  * See also gtk_widget_get_child_requisition().
2209  **/
2210 void
2211 gtk_widget_size_request (GtkWidget      *widget,
2212                          GtkRequisition *requisition)
2213 {
2214   g_return_if_fail (GTK_IS_WIDGET (widget));
2215
2216 #ifdef G_ENABLE_DEBUG
2217   if (requisition == &widget->requisition)
2218     g_warning ("gtk_widget_size_request() called on child widget with request equal\n to widget->requisition. gtk_widget_set_usize() may not work properly.");
2219 #endif /* G_ENABLE_DEBUG */
2220
2221   _gtk_size_group_compute_requisition (widget, requisition);
2222 }
2223
2224 /**
2225  * gtk_widget_get_child_requisition:
2226  * @widget: a #GtkWidget
2227  * @requisition: a #GtkRequisition to be filled in
2228  * 
2229  * This function is only for use in widget implementations. Obtains
2230  * @widget->requisition, unless someone has forced a particular
2231  * geometry on the widget (e.g. with gtk_widget_set_usize()), in which
2232  * case it returns that geometry instead of the widget's requisition.
2233  *
2234  * This function differs from gtk_widget_size_request() in that
2235  * it retrieves the last size request value from @widget->requisition,
2236  * while gtk_widget_size_request() actually calls the "size_request" method
2237  * on @widget to compute the size request and fill in @widget->requisition,
2238  * and only then returns @widget->requisition.
2239  *
2240  * Because this function does not call the "size_request" method, it
2241  * can only be used when you know that @widget->requisition is
2242  * up-to-date, that is, gtk_widget_size_request() has been called
2243  * since the last time a resize was queued. In general, only container
2244  * implementations have this information; applications should use
2245  * gtk_widget_size_request().
2246  **/
2247 void
2248 gtk_widget_get_child_requisition (GtkWidget      *widget,
2249                                   GtkRequisition *requisition)
2250 {
2251   _gtk_size_group_get_child_requisition (widget, requisition);
2252 }
2253
2254 static gboolean
2255 invalidate_predicate (GdkWindow *window,
2256                       gpointer   data)
2257 {
2258   gpointer user_data;
2259
2260   gdk_window_get_user_data (window, &user_data);
2261
2262   return (user_data == data);
2263 }
2264
2265 /* Invalidate @region in widget->window and all children
2266  * of widget->window owned by widget. @region is in the
2267  * same coordinates as widget->allocation and will be
2268  * modified by this call.
2269  */
2270 static void
2271 gtk_widget_invalidate_widget_windows (GtkWidget *widget,
2272                                       GdkRegion *region)
2273 {
2274   if (!GTK_WIDGET_NO_WINDOW (widget))
2275     {
2276       int x, y;
2277       
2278       gdk_window_get_position (widget->window, &x, &y);
2279       gdk_region_offset (region, -x, -y);
2280     }
2281
2282   gdk_window_invalidate_maybe_recurse (widget->window, region,
2283                                        invalidate_predicate, widget);
2284 }
2285
2286 /**
2287  * gtk_widget_size_allocate:
2288  * @widget: a #GtkWidget
2289  * @allocation: position and size to be allocated to @widget
2290  *
2291  * This function is only used by #GtkContainer subclasses, to assign a size
2292  * and position to their child widgets. 
2293  * 
2294  **/
2295 void
2296 gtk_widget_size_allocate (GtkWidget     *widget,
2297                           GtkAllocation *allocation)
2298 {
2299   GtkWidgetAuxInfo *aux_info;
2300   GdkRectangle real_allocation;
2301   GdkRectangle old_allocation;
2302   gboolean alloc_needed;
2303   gboolean size_changed;
2304   gboolean position_changed;
2305   
2306   g_return_if_fail (GTK_IS_WIDGET (widget));
2307
2308   alloc_needed = GTK_WIDGET_ALLOC_NEEDED (widget);
2309   GTK_PRIVATE_UNSET_FLAG (widget, GTK_ALLOC_NEEDED);
2310
2311   old_allocation = widget->allocation;
2312   real_allocation = *allocation;
2313   aux_info =_gtk_widget_get_aux_info (widget, FALSE);
2314   
2315   if (aux_info)
2316     {
2317       if (aux_info->x_set)
2318         real_allocation.x = aux_info->x;
2319       if (aux_info->y_set)
2320         real_allocation.y = aux_info->y;
2321     }
2322
2323   if (real_allocation.width < 0 || real_allocation.height < 0)
2324     {
2325       g_warning ("gtk_widget_size_allocate(): attempt to allocate widget with width %d and height %d",
2326                  real_allocation.width,
2327                  real_allocation.height);
2328     }
2329   
2330   real_allocation.width = MAX (real_allocation.width, 1);
2331   real_allocation.height = MAX (real_allocation.height, 1);
2332
2333   size_changed = (old_allocation.width != real_allocation.width ||
2334                   old_allocation.height != real_allocation.height);
2335   position_changed = (old_allocation.x != real_allocation.x ||
2336                       old_allocation.y != real_allocation.y);
2337
2338   if (!alloc_needed && !size_changed && !position_changed)
2339     return;
2340   
2341   gtk_signal_emit (GTK_OBJECT (widget), widget_signals[SIZE_ALLOCATE], &real_allocation);
2342
2343   if (GTK_WIDGET_MAPPED (widget))
2344     {
2345       if (GTK_WIDGET_NO_WINDOW (widget) && GTK_WIDGET_REDRAW_ON_ALLOC (widget) && position_changed)
2346         {
2347           /* Invalidate union(old_allaction,widget->allocation) in widget->window
2348            */
2349           GdkRegion *invalidate = gdk_region_rectangle (&widget->allocation);
2350           gdk_region_union_with_rect (invalidate, &old_allocation);
2351
2352           gdk_window_invalidate_region (widget->window, invalidate, FALSE);
2353           gdk_region_destroy (invalidate);
2354         }
2355       
2356       if (size_changed)
2357         {
2358           if (GTK_WIDGET_REDRAW_ON_ALLOC (widget))
2359             {
2360               /* Invalidate union(old_allaction,widget->allocation) in widget->window and descendents owned by widget
2361                */
2362               GdkRegion *invalidate = gdk_region_rectangle (&widget->allocation);
2363               gdk_region_union_with_rect (invalidate, &old_allocation);
2364
2365               gtk_widget_invalidate_widget_windows (widget, invalidate);
2366               gdk_region_destroy (invalidate);
2367             }
2368         }
2369     }
2370
2371   if ((size_changed || position_changed) && widget->parent &&
2372       GTK_WIDGET_REALIZED (widget->parent) && GTK_CONTAINER (widget->parent)->reallocate_redraws)
2373     {
2374       GdkRegion *invalidate = gdk_region_rectangle (&widget->parent->allocation);
2375       gtk_widget_invalidate_widget_windows (widget->parent, invalidate);
2376       gdk_region_destroy (invalidate);
2377     }
2378 }
2379
2380 /**
2381  * gtk_widget_common_ancestor:
2382  * @widget_a: a #GtkWidget
2383  * @widget_b: a #GtkWidget
2384  * 
2385  * Find the common ancestor of @widget_a and @widget_b that
2386  * is closest to the two widgets.
2387  * 
2388  * Return value: the closest common ancestor of @widget_a and
2389  *   @widget_b or %NULL if @widget_a and @widget_b do not
2390  *   share a common ancestor.
2391  **/
2392 static GtkWidget *
2393 gtk_widget_common_ancestor (GtkWidget *widget_a,
2394                             GtkWidget *widget_b)
2395 {
2396   GtkWidget *parent_a;
2397   GtkWidget *parent_b;
2398   gint depth_a = 0;
2399   gint depth_b = 0;
2400
2401   parent_a = widget_a;
2402   while (parent_a->parent)
2403     {
2404       parent_a = parent_a->parent;
2405       depth_a++;
2406     }
2407
2408   parent_b = widget_b;
2409   while (parent_b->parent)
2410     {
2411       parent_b = parent_b->parent;
2412       depth_b++;
2413     }
2414
2415   if (parent_a != parent_b)
2416     return NULL;
2417
2418   while (depth_a > depth_b)
2419     {
2420       widget_a = widget_a->parent;
2421       depth_a--;
2422     }
2423
2424   while (depth_b > depth_a)
2425     {
2426       widget_b = widget_b->parent;
2427       depth_b--;
2428     }
2429
2430   while (widget_a != widget_b)
2431     {
2432       widget_a = widget_a->parent;
2433       widget_b = widget_b->parent;
2434     }
2435
2436   return widget_a;
2437 }
2438
2439 /**
2440  * gtk_widget_translate_coordinates:
2441  * @src_widget:  a #GtkWidget
2442  * @dest_widget: a #GtkWidget
2443  * @src_x: X position relative to @src_widget
2444  * @src_y: Y position relative to @src_widget
2445  * @dest_x: location to store X position relative to @dest_widget
2446  * @dest_y: location to store Y position relative to @dest_widget
2447  * 
2448  * Translate coordinates relative to @src_widget's allocation to coordinates
2449  * relative to @dest_widget's allocations. In order to perform this
2450  * operation, both widgets must be realized, and must share a common
2451  * toplevel.
2452  * 
2453  * Return value: %FALSE if either widget was not realized, or there
2454  *   was no common ancestor. In this case, nothing is stored in
2455  *   *@dest_x and *@dest_y. Otherwise %TRUE.
2456  **/
2457 gboolean
2458 gtk_widget_translate_coordinates (GtkWidget  *src_widget,
2459                                   GtkWidget  *dest_widget,
2460                                   gint        src_x,
2461                                   gint        src_y,
2462                                   gint       *dest_x,
2463                                   gint       *dest_y)
2464 {
2465   GtkWidget *ancestor;
2466   GdkWindow *window;
2467
2468   g_return_val_if_fail (GTK_IS_WIDGET (src_widget), FALSE);
2469   g_return_val_if_fail (GTK_IS_WIDGET (dest_widget), FALSE);
2470
2471   ancestor = gtk_widget_common_ancestor (src_widget, dest_widget);
2472   if (!ancestor || !GTK_WIDGET_REALIZED (src_widget) || !GTK_WIDGET_REALIZED (dest_widget))
2473     return FALSE;
2474
2475   /* Translate from allocation relative to window relative */
2476   if (!GTK_WIDGET_NO_WINDOW (src_widget) && src_widget->parent)
2477     {
2478       gint wx, wy;
2479       gdk_window_get_position (src_widget->window, &wx, &wy);
2480
2481       src_x -= wx - src_widget->allocation.x;
2482       src_y -= wy - src_widget->allocation.y;
2483     }
2484   else
2485     {
2486       src_x += src_widget->allocation.x;
2487       src_y += src_widget->allocation.y;
2488     }
2489
2490   /* Translate to the common ancestor */
2491   window = src_widget->window;
2492   while (window != ancestor->window)
2493     {
2494       gint dx, dy;
2495       
2496       gdk_window_get_position (window, &dx, &dy);
2497       
2498       src_x += dx;
2499       src_y += dy;
2500       
2501       window = gdk_window_get_parent (window);
2502
2503       if (!window)              /* Handle GtkHandleBox */
2504         return FALSE;
2505     }
2506
2507   /* And back */
2508   window = dest_widget->window;
2509   while (window != ancestor->window)
2510     {
2511       gint dx, dy;
2512       
2513       gdk_window_get_position (window, &dx, &dy);
2514       
2515       src_x -= dx;
2516       src_y -= dy;
2517       
2518       window = gdk_window_get_parent (window);
2519       
2520       if (!window)              /* Handle GtkHandleBox */
2521         return FALSE;
2522     }
2523
2524   /* Translate from window relative to allocation relative */
2525   if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
2526     {
2527       gint wx, wy;
2528       gdk_window_get_position (dest_widget->window, &wx, &wy);
2529
2530       src_x += wx - dest_widget->allocation.x;
2531       src_y += wy - dest_widget->allocation.y;
2532     }
2533   else
2534     {
2535       src_x -= dest_widget->allocation.x;
2536       src_y -= dest_widget->allocation.y;
2537     }
2538
2539   if (dest_x)
2540     *dest_x = src_x;
2541   if (dest_y)
2542     *dest_y = src_y;
2543
2544   return TRUE;
2545 }
2546
2547 static void
2548 gtk_widget_real_size_allocate (GtkWidget     *widget,
2549                                GtkAllocation *allocation)
2550 {
2551   widget->allocation = *allocation;
2552   
2553   if (GTK_WIDGET_REALIZED (widget) &&
2554       !GTK_WIDGET_NO_WINDOW (widget))
2555      {
2556         gdk_window_move_resize (widget->window,
2557                                 allocation->x, allocation->y,
2558                                 allocation->width, allocation->height);
2559      }
2560 }
2561
2562 typedef struct {
2563   GClosure   closure;
2564   guint      signal_id;
2565 } AccelClosure;
2566
2567 static void
2568 closure_accel_activate (GClosure     *closure,
2569                         GValue       *return_value,
2570                         guint         n_param_values,
2571                         const GValue *param_values,
2572                         gpointer      invocation_hint,
2573                         gpointer      marshal_data)
2574 {
2575   AccelClosure *aclosure = (AccelClosure*) closure;
2576
2577   if (GTK_WIDGET_IS_SENSITIVE (closure->data))
2578     g_signal_emit (closure->data, aclosure->signal_id, 0);
2579
2580   /* we handled the accelerator */
2581   g_value_set_boolean (return_value, TRUE);
2582 }
2583
2584 static void
2585 closures_destroy (gpointer data)
2586 {
2587   GSList *slist, *closures = data;
2588
2589   for (slist = closures; slist; slist = slist->next)
2590     {
2591       g_closure_invalidate (slist->data);
2592       g_closure_unref (slist->data);
2593     }
2594   g_slist_free (closures);
2595 }
2596
2597 static GClosure*
2598 widget_new_accel_closure (GtkWidget *widget,
2599                           guint      signal_id)
2600 {
2601   AccelClosure *aclosure;
2602   GClosure *closure = NULL;
2603   GSList *slist, *closures;
2604
2605   closures = g_object_steal_qdata (G_OBJECT (widget), quark_accel_closures);
2606   for (slist = closures; slist; slist = slist->next)
2607     if (!gtk_accel_group_from_accel_closure (slist->data))
2608       {
2609         /* reuse this closure */
2610         closure = slist->data;
2611         break;
2612       }
2613   if (!closure)
2614     {
2615       closure = g_closure_new_object (sizeof (AccelClosure), G_OBJECT (widget));
2616       closures = g_slist_prepend (closures, g_closure_ref (closure));
2617       g_closure_sink (closure);
2618       g_closure_set_marshal (closure, closure_accel_activate);
2619     }
2620   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_closures, closures, closures_destroy);
2621   
2622   aclosure = (AccelClosure*) closure;
2623   g_assert (closure->data == widget);
2624   g_assert (closure->marshal == closure_accel_activate);
2625   aclosure->signal_id = signal_id;
2626
2627   return closure;
2628 }
2629
2630 /**
2631  * gtk_widget_add_accelerator
2632  * @widget:       widget to install an accelerator on
2633  * @accel_signal: widget signal to emit on accelerator activation
2634  * @accel_group:  accel group for this widget, added to its toplevel
2635  * @accel_key:    GDK keyval of the accelerator
2636  * @accel_mods:   modifier key combination of the accelerator
2637  * @accel_flags:  flag accelerators, e.g. %GTK_ACCEL_VISIBLE
2638  *
2639  * Installs an accelerator for this @widget in @accel_group that causes
2640  * @accel_signal to be emitted if the accelerator is activated.
2641  * The @accel_group needs to be added to the widget's toplevel via
2642  * gtk_window_add_accel_group(), and the signal must be of type %G_RUN_ACTION.
2643  * Accelerators added through this function are not user changeable during
2644  * runtime. If you want to support accelerators that can be changed by the
2645  * user, use gtk_accel_map_add_entry() and gtk_widget_set_accel_path() or
2646  * gtk_menu_item_set_accel_path() instead.
2647  */
2648 void
2649 gtk_widget_add_accelerator (GtkWidget      *widget,
2650                             const gchar    *accel_signal,
2651                             GtkAccelGroup  *accel_group,
2652                             guint           accel_key,
2653                             GdkModifierType accel_mods,
2654                             GtkAccelFlags   accel_flags)
2655 {
2656   GClosure *closure;
2657   GSignalQuery query;
2658
2659   g_return_if_fail (GTK_IS_WIDGET (widget));
2660   g_return_if_fail (accel_signal != NULL);
2661   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
2662
2663   g_signal_query (g_signal_lookup (accel_signal, G_OBJECT_TYPE (widget)), &query);
2664   if (!query.signal_id ||
2665       !(query.signal_flags & G_SIGNAL_ACTION) ||
2666       query.return_type != G_TYPE_NONE ||
2667       query.n_params)
2668     {
2669       /* hmm, should be elaborate enough */
2670       g_warning (G_STRLOC ": widget `%s' has no activatable signal \"%s\" without arguments",
2671                  G_OBJECT_TYPE_NAME (widget), accel_signal);
2672       return;
2673     }
2674
2675   closure = widget_new_accel_closure (widget, query.signal_id);
2676
2677   g_object_ref (widget);
2678
2679   /* install the accelerator. since we don't map this onto an accel_path,
2680    * the accelerator will automatically be locked.
2681    */
2682   gtk_accel_group_connect (accel_group,
2683                            accel_key,
2684                            accel_mods,
2685                            accel_flags | GTK_ACCEL_LOCKED,
2686                            closure);
2687
2688   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
2689
2690   g_object_unref (widget);
2691 }
2692
2693 /**
2694  * gtk_widget_remove_accelerator:
2695  * @widget:       widget to install an accelerator on
2696  * @accel_group:  accel group for this widget
2697  * @accel_key:    GDK keyval of the accelerator
2698  * @accel_mods:   modifier key combination of the accelerator
2699  * @returns:      whether an accelerator was installed and could be removed
2700  *
2701  * Removes an accelerator from @widget, previously installed with
2702  * gtk_widget_add_accelerator().
2703  */
2704 gboolean
2705 gtk_widget_remove_accelerator (GtkWidget      *widget,
2706                                GtkAccelGroup  *accel_group,
2707                                guint           accel_key,
2708                                GdkModifierType accel_mods)
2709 {
2710   GtkAccelGroupEntry *ag_entry;
2711   GList *slist, *clist;
2712   guint n;
2713   
2714   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
2715   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
2716
2717   ag_entry = gtk_accel_group_query (accel_group, accel_key, accel_mods, &n);
2718   clist = gtk_widget_list_accel_closures (widget);
2719   for (slist = clist; slist; slist = slist->next)
2720     {
2721       guint i;
2722
2723       for (i = 0; i < n; i++)
2724         if (slist->data == (gpointer) ag_entry[i].closure)
2725           {
2726             gboolean is_removed = gtk_accel_group_disconnect (accel_group, slist->data);
2727
2728             g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
2729
2730             g_list_free (clist);
2731
2732             return is_removed;
2733           }
2734     }
2735   g_list_free (clist);
2736
2737   g_warning (G_STRLOC ": no accelerator (%u,%u) installed in accel group (%p) for %s (%p)",
2738              accel_key, accel_mods, accel_group,
2739              G_OBJECT_TYPE_NAME (widget), widget);
2740
2741   return FALSE;
2742 }
2743
2744 /**
2745  * gtk_widget_list_accel_closures
2746  * @widget:  widget to list accelerator closures for
2747  * @returns: a newly allocated #GList of closures
2748  *
2749  * Lists the closures used by @widget for accelerator group connections
2750  * with gtk_accel_group_connect_by_path() or gtk_accel_group_connect().
2751  * The closures can be used to monitor accelerator changes on @widget,
2752  * by connecting to the ::accel_changed signal of the #GtkAccelGroup of a 
2753  * closure which can be found out with gtk_accel_group_from_accel_closure().
2754  */
2755 GList*
2756 gtk_widget_list_accel_closures (GtkWidget *widget)
2757 {
2758   GSList *slist;
2759   GList *clist = NULL;
2760
2761   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2762
2763   for (slist = g_object_get_qdata (G_OBJECT (widget), quark_accel_closures); slist; slist = slist->next)
2764     if (gtk_accel_group_from_accel_closure (slist->data))
2765       clist = g_list_prepend (clist, slist->data);
2766   return clist;
2767 }
2768
2769 typedef struct {
2770   GQuark         path_quark;
2771   GtkWidget     *widget;
2772   GtkAccelGroup *accel_group;
2773   GClosure      *closure;
2774 } AccelPath;
2775
2776 static void
2777 destroy_accel_path (gpointer data)
2778 {
2779   AccelPath *apath = data;
2780
2781   gtk_accel_group_disconnect (apath->accel_group, apath->closure);
2782
2783   /* closures_destroy takes care of unrefing the closure */
2784   g_object_unref (apath->accel_group);
2785   
2786   g_free (apath);
2787 }
2788
2789
2790 /**
2791  * gtk_widget_set_accel_path:
2792  * @widget: a #GtkWidget
2793  * @accel_path: path used to look up the the accelerator
2794  * @accel_group: a #GtkAccelGroup.
2795  * 
2796  * Given an accelerator group, @accel_group, and an accelerator path,
2797  * @accel_path, sets up an accelerator in @accel_group so whenever the
2798  * key binding that is defined for @accel_path is pressed, @widget
2799  * will be activated.  This removes any accelerators (for any
2800  * accelerator group) installed by previous calls to
2801  * gtk_widget_set_accel_path(). Associating accelerators with
2802  * paths allows them to be modified by the user and the modifications
2803  * to be saved for future use. (See gtk_accel_map_save().)
2804  *
2805  * This function is a low level function that would most likely
2806  * be used by a menu creation system like #GtkItemFactory. If you
2807  * use #GtkItemFactory, setting up accelerator paths will be done
2808  * automatically.
2809  *
2810  * Even when you you aren't using #GtkItemFactory, if you only want to
2811  * set up accelerators on menu items gtk_menu_item_set_accel_path()
2812  * provides a somewhat more convenient interface.
2813  **/
2814 void
2815 gtk_widget_set_accel_path (GtkWidget     *widget,
2816                            const gchar   *accel_path,
2817                            GtkAccelGroup *accel_group)
2818 {
2819   AccelPath *apath;
2820
2821   g_return_if_fail (GTK_IS_WIDGET (widget));
2822   g_return_if_fail (GTK_WIDGET_GET_CLASS (widget)->activate_signal != 0);
2823
2824   if (accel_path)
2825     {
2826       g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
2827       g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
2828
2829       gtk_accel_map_add_entry (accel_path, 0, 0);
2830       apath = g_new (AccelPath, 1);
2831       apath->widget = widget;
2832       apath->accel_group = g_object_ref (accel_group);
2833       apath->path_quark = g_quark_from_string (accel_path);
2834       apath->closure = widget_new_accel_closure (apath->widget, GTK_WIDGET_GET_CLASS (apath->widget)->activate_signal);
2835     }
2836   else
2837     apath = NULL;
2838
2839   /* also removes possible old settings */
2840   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_path, apath, destroy_accel_path);
2841
2842   if (apath)
2843     gtk_accel_group_connect_by_path (apath->accel_group, g_quark_to_string (apath->path_quark), apath->closure);
2844
2845   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
2846 }
2847
2848 const gchar*
2849 _gtk_widget_get_accel_path (GtkWidget *widget,
2850                             gboolean  *locked)
2851 {
2852   AccelPath *apath;
2853
2854   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
2855
2856   apath = g_object_get_qdata (G_OBJECT (widget), quark_accel_path);
2857   if (locked)
2858     *locked = apath ? apath->accel_group->lock_count > 0 : TRUE;
2859   return apath ? g_quark_to_string (apath->path_quark) : NULL;
2860 }
2861
2862 gboolean
2863 gtk_widget_mnemonic_activate (GtkWidget *widget,
2864                               gboolean   group_cycling)
2865 {
2866   gboolean handled;
2867   
2868   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
2869
2870   group_cycling = group_cycling != FALSE;
2871   if (!GTK_WIDGET_IS_SENSITIVE (widget))
2872     handled = TRUE;
2873   else
2874     gtk_signal_emit (GTK_OBJECT (widget),
2875                      widget_signals[MNEMONIC_ACTIVATE],
2876                      group_cycling,
2877                      &handled);
2878   return handled;
2879 }
2880
2881 static gboolean
2882 gtk_widget_real_mnemonic_activate (GtkWidget *widget,
2883                                    gboolean   group_cycling)
2884 {
2885   if (!group_cycling && GTK_WIDGET_GET_CLASS (widget)->activate_signal)
2886     gtk_widget_activate (widget);
2887   else if (GTK_WIDGET_CAN_FOCUS (widget))
2888     gtk_widget_grab_focus (widget);
2889   else
2890     {
2891       g_warning ("widget `%s' isn't suitable for mnemonic activation",
2892                  G_OBJECT_TYPE_NAME (widget));
2893       gdk_display_beep (gtk_widget_get_display (widget));
2894     }
2895   return TRUE;
2896 }
2897
2898 static gboolean
2899 gtk_widget_real_key_press_event (GtkWidget         *widget,
2900                                  GdkEventKey       *event)
2901 {
2902   return _gtk_bindings_activate_event (GTK_OBJECT (widget), event);
2903 }
2904
2905 static gboolean
2906 gtk_widget_real_key_release_event (GtkWidget         *widget,
2907                                    GdkEventKey       *event)
2908 {
2909   return _gtk_bindings_activate_event (GTK_OBJECT (widget), event);
2910 }
2911
2912 static gboolean
2913 gtk_widget_real_focus_in_event (GtkWidget     *widget,
2914                                 GdkEventFocus *event)
2915 {
2916   gtk_widget_queue_draw (widget);
2917
2918   return FALSE;
2919 }
2920
2921 static gboolean
2922 gtk_widget_real_focus_out_event (GtkWidget     *widget,
2923                                  GdkEventFocus *event)
2924 {
2925   gtk_widget_queue_draw (widget);
2926
2927   return FALSE;
2928 }
2929
2930 #define WIDGET_REALIZED_FOR_EVENT(widget, event) \
2931      (event->type == GDK_FOCUS_CHANGE || GTK_WIDGET_REALIZED(widget))
2932
2933 /**
2934  * gtk_widget_event:
2935  * @widget: a #GtkWidget
2936  * @event: a #GdkEvent
2937  * 
2938  * Rarely-used function. This function is used to emit
2939  * the event signals on a widget (those signals should never
2940  * be emitted without using this function to do so).
2941  * If you want to synthesize an event though, don't use this function;
2942  * instead, use gtk_main_do_event() so the event will behave as if
2943  * it were in the event queue. Don't synthesize expose events; instead,
2944  * use gdk_window_invalidate_rect() to invalidate a region of the
2945  * window.
2946  * 
2947  * Return value: return from the event signal emission (%TRUE if the event was handled)
2948  **/
2949 gboolean
2950 gtk_widget_event (GtkWidget *widget,
2951                   GdkEvent  *event)
2952 {
2953   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
2954   g_return_val_if_fail (WIDGET_REALIZED_FOR_EVENT (widget, event), TRUE);
2955
2956   if (event->type == GDK_EXPOSE)
2957     {
2958       g_warning ("Events of type GDK_EXPOSE cannot be synthesized. To get "
2959                  "the same effect, call gdk_window_invalidate_rect/region(), "
2960                  "followed by gdk_window_process_updates().");
2961       return TRUE;
2962     }
2963   
2964   return gtk_widget_event_internal (widget, event);
2965 }
2966
2967
2968 /**
2969  * gtk_widget_send_expose:
2970  * @widget: a #GtkWidget
2971  * @event: a expose #GdkEvent
2972  * 
2973  * Very rarely-used function. This function is used to emit
2974  * an expose event signals on a widget. This function is not
2975  * normally used directly. The only time it is used is when
2976  * propagating an expose event to a child %NO_WINDOW widget, and
2977  * that is normally done using gtk_container_propagate_expose().
2978  *
2979  * If you want to force an area of a window to be redrawn, 
2980  * use gdk_window_invalidate_rect() or gdk_window_invalidate_region().
2981  * To cause the redraw to be done immediately, follow that call
2982  * with a call to gdk_window_process_updates().
2983  * 
2984  * Return value: return from the event signal emission (%TRUE if the event was handled)
2985  **/
2986 gint
2987 gtk_widget_send_expose (GtkWidget *widget,
2988                         GdkEvent  *event)
2989 {
2990   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
2991   g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), TRUE);
2992   g_return_val_if_fail (event != NULL, TRUE);
2993   g_return_val_if_fail (event->type == GDK_EXPOSE, TRUE);
2994
2995   if (event->type != GDK_EXPOSE)
2996     return TRUE;
2997   
2998   return gtk_widget_event_internal (widget, event);
2999 }
3000
3001 static gint
3002 gtk_widget_event_internal (GtkWidget *widget,
3003                            GdkEvent  *event)
3004 {
3005   gboolean return_val = FALSE;
3006
3007   gtk_widget_ref (widget);
3008
3009   gtk_signal_emit (GTK_OBJECT (widget), widget_signals[EVENT], event, &return_val);
3010   return_val |= !WIDGET_REALIZED_FOR_EVENT (widget, event);
3011   if (!return_val)
3012     {
3013       gint signal_num;
3014
3015       switch (event->type)
3016         {
3017         case GDK_NOTHING:
3018           signal_num = -1;
3019           break;
3020         case GDK_BUTTON_PRESS:
3021         case GDK_2BUTTON_PRESS:
3022         case GDK_3BUTTON_PRESS:
3023           signal_num = BUTTON_PRESS_EVENT;
3024           break;
3025         case GDK_SCROLL:
3026           signal_num = SCROLL_EVENT;
3027           break;
3028         case GDK_BUTTON_RELEASE:
3029           signal_num = BUTTON_RELEASE_EVENT;
3030           break;
3031         case GDK_MOTION_NOTIFY:
3032           signal_num = MOTION_NOTIFY_EVENT;
3033           break;
3034         case GDK_DELETE:
3035           signal_num = DELETE_EVENT;
3036           break;
3037         case GDK_DESTROY:
3038           signal_num = DESTROY_EVENT;
3039           break;
3040         case GDK_KEY_PRESS:
3041           signal_num = KEY_PRESS_EVENT;
3042           break;
3043         case GDK_KEY_RELEASE:
3044           signal_num = KEY_RELEASE_EVENT;
3045           break;
3046         case GDK_ENTER_NOTIFY:
3047           signal_num = ENTER_NOTIFY_EVENT;
3048           break;
3049         case GDK_LEAVE_NOTIFY:
3050           signal_num = LEAVE_NOTIFY_EVENT;
3051           break;
3052         case GDK_FOCUS_CHANGE:
3053           signal_num = event->focus_change.in ? FOCUS_IN_EVENT : FOCUS_OUT_EVENT;
3054           break;
3055         case GDK_CONFIGURE:
3056           signal_num = CONFIGURE_EVENT;
3057           break;
3058         case GDK_MAP:
3059           signal_num = MAP_EVENT;
3060           break;
3061         case GDK_UNMAP:
3062           signal_num = UNMAP_EVENT;
3063           break;
3064         case GDK_WINDOW_STATE:
3065           signal_num = WINDOW_STATE_EVENT;
3066           break;
3067         case GDK_PROPERTY_NOTIFY:
3068           signal_num = PROPERTY_NOTIFY_EVENT;
3069           break;
3070         case GDK_SELECTION_CLEAR:
3071           signal_num = SELECTION_CLEAR_EVENT;
3072           break;
3073         case GDK_SELECTION_REQUEST:
3074           signal_num = SELECTION_REQUEST_EVENT;
3075           break;
3076         case GDK_SELECTION_NOTIFY:
3077           signal_num = SELECTION_NOTIFY_EVENT;
3078           break;
3079         case GDK_PROXIMITY_IN:
3080           signal_num = PROXIMITY_IN_EVENT;
3081           break;
3082         case GDK_PROXIMITY_OUT:
3083           signal_num = PROXIMITY_OUT_EVENT;
3084           break;
3085         case GDK_NO_EXPOSE:
3086           signal_num = NO_EXPOSE_EVENT;
3087           break;
3088         case GDK_CLIENT_EVENT:
3089           signal_num = CLIENT_EVENT;
3090           break;
3091         case GDK_EXPOSE:
3092           signal_num = EXPOSE_EVENT;
3093           break;
3094         case GDK_VISIBILITY_NOTIFY:
3095           signal_num = VISIBILITY_NOTIFY_EVENT;
3096           break;
3097         default:
3098           g_warning ("gtk_widget_event(): unhandled event type: %d", event->type);
3099           signal_num = -1;
3100           break;
3101         }
3102       if (signal_num != -1)
3103         gtk_signal_emit (GTK_OBJECT (widget), widget_signals[signal_num], event, &return_val);
3104     }
3105   if (WIDGET_REALIZED_FOR_EVENT (widget, event))
3106     gtk_signal_emit (GTK_OBJECT (widget), widget_signals[EVENT_AFTER], event);
3107   else
3108     return_val = TRUE;
3109
3110   gtk_widget_unref (widget);
3111
3112   return return_val;
3113 }
3114
3115 /**
3116  * gtk_widget_activate:
3117  * @widget: a #GtkWidget that's activatable
3118  * 
3119  * For widgets that can be "activated" (buttons, menu items, etc.)
3120  * this function activates them. Activation is what happens when you
3121  * press Enter on a widget during key navigation; clicking a button,
3122  * selecting a menu item, etc. If @widget isn't activatable,
3123  * the function returns %FALSE.
3124  * 
3125  * Return value: %TRUE if the widget was activatable
3126  **/
3127 gboolean
3128 gtk_widget_activate (GtkWidget *widget)
3129 {
3130   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3131   
3132   if (WIDGET_CLASS (widget)->activate_signal)
3133     {
3134       /* FIXME: we should eventually check the signals signature here */
3135       gtk_signal_emit (GTK_OBJECT (widget), WIDGET_CLASS (widget)->activate_signal);
3136
3137       return TRUE;
3138     }
3139   else
3140     return FALSE;
3141 }
3142
3143 /**
3144  * gtk_widget_set_scroll_adjustments:
3145  * @widget: a #GtkWidget
3146  * @hadjustment: an adjustment for horizontal scrolling, or %NULL
3147  * @vadjustment: an adjustment for vertical scrolling, or %NULL
3148  *
3149  * For widgets that support scrolling, sets the scroll adjustments and
3150  * returns %TRUE.  For widgets that don't support scrolling, does
3151  * nothing and returns %FALSE. Widgets that don't support scrolling
3152  * can be scrolled by placing them in a #GtkViewport, which does
3153  * support scrolling.
3154  * 
3155  * Return value: %TRUE if the widget supports scrolling
3156  **/
3157 gboolean
3158 gtk_widget_set_scroll_adjustments (GtkWidget     *widget,
3159                                    GtkAdjustment *hadjustment,
3160                                    GtkAdjustment *vadjustment)
3161 {
3162   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3163   if (hadjustment)
3164     g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), FALSE);
3165   if (vadjustment)
3166     g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), FALSE);
3167
3168   if (WIDGET_CLASS (widget)->set_scroll_adjustments_signal)
3169     {
3170       /* FIXME: we should eventually check the signals signature here */
3171       gtk_signal_emit (GTK_OBJECT (widget),
3172                        WIDGET_CLASS (widget)->set_scroll_adjustments_signal,
3173                        hadjustment, vadjustment);
3174       return TRUE;
3175     }
3176   else
3177     return FALSE;
3178 }
3179
3180 static void
3181 gtk_widget_reparent_subwindows (GtkWidget *widget,
3182                                 GdkWindow *new_window)
3183 {
3184   if (GTK_WIDGET_NO_WINDOW (widget))
3185     {
3186       GList *children = gdk_window_get_children (widget->window);
3187       GList *tmp_list;
3188
3189       for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
3190         {
3191           GtkWidget *child;
3192           GdkWindow *window = tmp_list->data;
3193
3194           gdk_window_get_user_data (window, (void **)&child);
3195           while (child && child != widget)
3196             child = child->parent;
3197
3198           if (child)
3199             gdk_window_reparent (window, new_window, 0, 0);
3200         }
3201
3202       g_list_free (children);
3203     }
3204   else
3205     gdk_window_reparent (widget->window, new_window, 0, 0);
3206 }
3207
3208 static void
3209 gtk_widget_reparent_fixup_child (GtkWidget *widget,
3210                                  gpointer   client_data)
3211 {
3212   g_return_if_fail (client_data != NULL);
3213   
3214   if (GTK_WIDGET_NO_WINDOW (widget))
3215     {
3216       if (widget->window)
3217         gdk_window_unref (widget->window);
3218       widget->window = (GdkWindow*) client_data;
3219       if (widget->window)
3220         gdk_window_ref (widget->window);
3221
3222       if (GTK_IS_CONTAINER (widget))
3223         gtk_container_forall (GTK_CONTAINER (widget),
3224                               gtk_widget_reparent_fixup_child,
3225                               client_data);
3226     }
3227 }
3228
3229 /**
3230  * gtk_widget_reparent:
3231  * @widget: a #GtkWidget
3232  * @new_parent: a #GtkContainer to move the widget into
3233  *
3234  * Moves a widget from one #GtkContainer to another, handling reference
3235  * count issues to avoid destroying the widget.
3236  * 
3237  **/
3238 void
3239 gtk_widget_reparent (GtkWidget *widget,
3240                      GtkWidget *new_parent)
3241 {
3242   g_return_if_fail (GTK_IS_WIDGET (widget));
3243   g_return_if_fail (GTK_IS_CONTAINER (new_parent));
3244   g_return_if_fail (widget->parent != NULL);
3245   
3246   if (widget->parent != new_parent)
3247     {
3248       /* First try to see if we can get away without unrealizing
3249        * the widget as we reparent it. if so we set a flag so
3250        * that gtk_widget_unparent doesn't unrealize widget
3251        */
3252       if (GTK_WIDGET_REALIZED (widget) && GTK_WIDGET_REALIZED (new_parent))
3253         GTK_PRIVATE_SET_FLAG (widget, GTK_IN_REPARENT);
3254       
3255       gtk_widget_ref (widget);
3256       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
3257       gtk_container_add (GTK_CONTAINER (new_parent), widget);
3258       gtk_widget_unref (widget);
3259       
3260       if (GTK_WIDGET_IN_REPARENT (widget))
3261         {
3262           GTK_PRIVATE_UNSET_FLAG (widget, GTK_IN_REPARENT);
3263
3264           gtk_widget_reparent_subwindows (widget, gtk_widget_get_parent_window (widget));
3265           gtk_widget_reparent_fixup_child (widget,
3266                                            gtk_widget_get_parent_window (widget));
3267         }
3268
3269       g_object_notify (G_OBJECT (widget), "parent");
3270     }
3271 }
3272
3273 /**
3274  * gtk_widget_intersect:
3275  * @widget: a #GtkWidget
3276  * @area: a rectangle
3277  * @intersection: rectangle to store intersection of @widget and @area
3278  * 
3279  * Computes the intersection of a @widget's area and @area, storing
3280  * the intersection in @intersection, and returns %TRUE if there was
3281  * an intersection.  @intersection may be %NULL if you're only
3282  * interested in whether there was an intersection.
3283  * 
3284  * Return value: %TRUE if there was an intersection
3285  **/
3286 gboolean
3287 gtk_widget_intersect (GtkWidget    *widget,
3288                       GdkRectangle *area,
3289                       GdkRectangle *intersection)
3290 {
3291   GdkRectangle *dest;
3292   GdkRectangle tmp;
3293   gint return_val;
3294   
3295   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3296   g_return_val_if_fail (area != NULL, FALSE);
3297   
3298   if (intersection)
3299     dest = intersection;
3300   else
3301     dest = &tmp;
3302   
3303   return_val = gdk_rectangle_intersect (&widget->allocation, area, dest);
3304   
3305   if (return_val && intersection && !GTK_WIDGET_NO_WINDOW (widget))
3306     {
3307       intersection->x -= widget->allocation.x;
3308       intersection->y -= widget->allocation.y;
3309     }
3310   
3311   return return_val;
3312 }
3313
3314 /**
3315  * gtk_widget_region_intersect:
3316  * @widget: a #GtkWidget
3317  * @region: a #GdkRegion, in the same coordinate system as 
3318  *          @widget->allocation. That is, relative to @widget->window
3319  *          for %NO_WINDOW widgets; relative to the parent window
3320  *          of @widget->window for widgets with their own window.
3321  * @returns: A newly allocated region holding the intersection of @widget
3322  *           and @region. The coordinates of the return value are
3323  *           relative to @widget->window for %NO_WINDOW widgets, and
3324  *           relative to the parent window of @widget->window for
3325  *           widgets with their own window.
3326  * 
3327  * Computes the intersection of a @widget's area and @region, returning
3328  * the intersection. The result may be empty, use gdk_region_empty() to
3329  * check.
3330  **/
3331 GdkRegion *
3332 gtk_widget_region_intersect (GtkWidget *widget,
3333                              GdkRegion *region)
3334 {
3335   GdkRegion *dest;
3336   
3337   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
3338   g_return_val_if_fail (region != NULL, NULL);
3339   
3340   dest = gdk_region_rectangle (&widget->allocation);
3341  
3342   gdk_region_intersect (dest, region);
3343
3344   return dest;
3345 }
3346
3347 /**
3348  * gtk_widget_grab_focus:
3349  * @widget: a #GtkWidget
3350  * 
3351  * Causes @widget to have the keyboard focus for the #GtkWindow it's
3352  * inside. @widget must be a focusable widget, such as a #GtkEntry;
3353  * something like #GtkFrame won't work. (More precisely, it must have the
3354  * %GTK_CAN_FOCUS flag set.)
3355  * 
3356  **/
3357 void
3358 gtk_widget_grab_focus (GtkWidget *widget)
3359 {
3360   g_return_if_fail (GTK_IS_WIDGET (widget));
3361
3362   if (!GTK_WIDGET_IS_SENSITIVE (widget))
3363     return;
3364   
3365   g_object_ref (G_OBJECT (widget));
3366   gtk_signal_emit (GTK_OBJECT (widget), widget_signals[GRAB_FOCUS]);
3367   g_object_notify (G_OBJECT (widget), "has_focus");
3368   g_object_unref (G_OBJECT (widget));
3369 }
3370
3371 static void
3372 reset_focus_recurse (GtkWidget *widget,
3373                      gpointer   data)
3374 {
3375   if (GTK_IS_CONTAINER (widget))
3376     {
3377       GtkContainer *container;
3378
3379       container = GTK_CONTAINER (widget);
3380       gtk_container_set_focus_child (container, NULL);
3381
3382       gtk_container_foreach (container,
3383                              reset_focus_recurse,
3384                              NULL);
3385     }
3386 }
3387
3388 static void
3389 gtk_widget_real_grab_focus (GtkWidget *focus_widget)
3390 {
3391   if (GTK_WIDGET_CAN_FOCUS (focus_widget))
3392     {
3393       GtkWidget *toplevel;
3394       GtkWidget *widget;
3395       
3396       /* clear the current focus setting, break if the current widget
3397        * is the focus widget's parent, since containers above that will
3398        * be set by the next loop.
3399        */
3400       toplevel = gtk_widget_get_toplevel (focus_widget);
3401       if (GTK_WIDGET_TOPLEVEL (toplevel))
3402         {
3403           widget = GTK_WINDOW (toplevel)->focus_widget;
3404           
3405           if (widget == focus_widget)
3406             {
3407               /* We call _gtk_window_internal_set_focus() here so that the
3408                * toplevel window can request the focus if necessary.
3409                * This is needed when the toplevel is a GtkPlug
3410                */
3411               if (!GTK_WIDGET_HAS_FOCUS (widget))
3412                 _gtk_window_internal_set_focus (GTK_WINDOW (toplevel), focus_widget);
3413
3414               return;
3415             }
3416           
3417           if (widget)
3418             {
3419               while (widget->parent && widget->parent != focus_widget->parent)
3420                 {
3421                   widget = widget->parent;
3422                   gtk_container_set_focus_child (GTK_CONTAINER (widget), NULL);
3423                 }
3424             }
3425         }
3426       else if (toplevel != focus_widget)
3427         {
3428           /* gtk_widget_grab_focus() operates on a tree without window...
3429            * actually, this is very questionable behaviour.
3430            */
3431           
3432           gtk_container_foreach (GTK_CONTAINER (toplevel),
3433                                  reset_focus_recurse,
3434                                  NULL);
3435         }
3436
3437       /* now propagate the new focus up the widget tree and finally
3438        * set it on the window
3439        */
3440       widget = focus_widget;
3441       while (widget->parent)
3442         {
3443           gtk_container_set_focus_child (GTK_CONTAINER (widget->parent), widget);
3444           widget = widget->parent;
3445         }
3446       if (GTK_IS_WINDOW (widget))
3447         _gtk_window_internal_set_focus (GTK_WINDOW (widget), focus_widget);
3448     }
3449 }
3450
3451 static gboolean
3452 gtk_widget_real_show_help (GtkWidget        *widget,
3453                            GtkWidgetHelpType help_type)
3454 {
3455   if (help_type == GTK_WIDGET_HELP_TOOLTIP)
3456     {
3457       _gtk_tooltips_toggle_keyboard_mode (widget);
3458       return TRUE;
3459     }
3460   else
3461     return FALSE;
3462 }
3463
3464 static gboolean
3465 gtk_widget_real_focus (GtkWidget         *widget,
3466                        GtkDirectionType   direction)
3467 {
3468   if (!GTK_WIDGET_CAN_FOCUS (widget))
3469     return FALSE;
3470   
3471   if (!gtk_widget_is_focus (widget))
3472     {
3473       gtk_widget_grab_focus (widget);
3474       return TRUE;
3475     }
3476   else
3477     return FALSE;
3478 }
3479
3480 /**
3481  * gtk_widget_is_focus:
3482  * @widget: a #GtkWidget
3483  * 
3484  * Determines if the widget is the focus widget within its
3485  * toplevel. (This does not mean that the %HAS_FOCUS flag is
3486  * necessarily set; %HAS_FOCUS will only be set if the
3487  * toplevel widget additionally has the global input focus.)
3488  * 
3489  * Return value: %TRUE if the widget is the focus widget.
3490  **/
3491 gboolean
3492 gtk_widget_is_focus (GtkWidget *widget)
3493 {
3494   GtkWidget *toplevel;
3495
3496   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3497
3498   toplevel = gtk_widget_get_toplevel (widget);
3499   
3500   if (GTK_IS_WINDOW (toplevel))
3501     return widget == GTK_WINDOW (toplevel)->focus_widget;
3502   else
3503     return FALSE;
3504 }
3505
3506 /**
3507  * gtk_widget_grab_default:
3508  * @widget: a #GtkWidget
3509  *
3510  * Causes @widget to become the default widget. @widget must have the
3511  * %GTK_CAN_DEFAULT flag set; typically you have to set this flag
3512  * yourself by calling <literal>GTK_WIDGET_SET_FLAGS (@widget,
3513  * GTK_CAN_DEFAULT)</literal>.  The default widget is activated when the user
3514  * presses Enter in a window.  Default widgets must be activatable,
3515  * that is, gtk_widget_activate() should affect them.
3516  * 
3517  **/
3518 void
3519 gtk_widget_grab_default (GtkWidget *widget)
3520 {
3521   GtkWidget *window;
3522   
3523   g_return_if_fail (GTK_IS_WIDGET (widget));
3524   g_return_if_fail (GTK_WIDGET_CAN_DEFAULT (widget));
3525   
3526   window = gtk_widget_get_toplevel (widget);
3527   
3528   if (window && GTK_WIDGET_TOPLEVEL (window))
3529     gtk_window_set_default (GTK_WINDOW (window), widget);
3530   else
3531     g_warning (G_STRLOC ": widget not within a GtkWindow");
3532 }
3533
3534 /**
3535  * gtk_widget_set_name:
3536  * @widget: a #GtkWidget
3537  * @name: name for the widget
3538  *
3539  * Widgets can be named, which allows you to refer to them from a
3540  * gtkrc file. You can apply a style to widgets with a particular name
3541  * in the gtkrc file. See the documentation for gtkrc files (on the
3542  * same page as the docs for #GtkRcStyle).
3543  * 
3544  **/
3545 void
3546 gtk_widget_set_name (GtkWidget   *widget,
3547                      const gchar *name)
3548 {
3549   g_return_if_fail (GTK_IS_WIDGET (widget));
3550   
3551   if (widget->name)
3552     g_free (widget->name);
3553   widget->name = g_strdup (name);
3554
3555   if (GTK_WIDGET_RC_STYLE (widget))
3556     gtk_widget_reset_rc_style (widget);
3557
3558   g_object_notify (G_OBJECT (widget), "name");
3559 }
3560
3561 /**
3562  * gtk_widget_get_name:
3563  * @widget: a #GtkWidget
3564  * 
3565  * Retrieves the name of a widget. See gtk_widget_set_name() for the
3566  * significance of widget names.
3567  * 
3568  * Return value: name of the widget. This string is owned by GTK+ and
3569  * should not be modified or freed
3570  **/
3571 G_CONST_RETURN gchar*
3572 gtk_widget_get_name (GtkWidget *widget)
3573 {
3574   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
3575   
3576   if (widget->name)
3577     return widget->name;
3578   return gtk_type_name (GTK_WIDGET_TYPE (widget));
3579 }
3580
3581 /**
3582  * gtk_widget_set_state:
3583  * @widget: a #GtkWidget
3584  * @state: new state for @widget
3585  *
3586  * This function is for use in widget implementations. Sets the state
3587  * of a widget (insensitive, prelighted, etc.) Usually you should set
3588  * the state using wrapper functions such as gtk_widget_set_sensitive().
3589  * 
3590  **/
3591 void
3592 gtk_widget_set_state (GtkWidget           *widget,
3593                       GtkStateType         state)
3594 {
3595   g_return_if_fail (GTK_IS_WIDGET (widget));
3596
3597   if (state == GTK_WIDGET_STATE (widget))
3598     return;
3599
3600   if (state == GTK_STATE_INSENSITIVE)
3601     gtk_widget_set_sensitive (widget, FALSE);
3602   else
3603     {
3604       GtkStateData data;
3605
3606       data.state = state;
3607       data.state_restoration = FALSE;
3608       data.use_forall = FALSE;
3609       if (widget->parent)
3610         data.parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (widget->parent) != FALSE);
3611       else
3612         data.parent_sensitive = TRUE;
3613
3614       gtk_widget_propagate_state (widget, &data);
3615   
3616       if (GTK_WIDGET_DRAWABLE (widget))
3617         gtk_widget_queue_clear (widget);
3618     }
3619 }
3620
3621 void
3622 gtk_widget_set_app_paintable (GtkWidget *widget,
3623                               gboolean   app_paintable)
3624 {
3625   g_return_if_fail (GTK_IS_WIDGET (widget));
3626
3627   app_paintable = (app_paintable != FALSE);
3628
3629   if (GTK_WIDGET_APP_PAINTABLE (widget) != app_paintable)
3630     {
3631       if (app_paintable)
3632         GTK_WIDGET_SET_FLAGS (widget, GTK_APP_PAINTABLE);
3633       else
3634         GTK_WIDGET_UNSET_FLAGS (widget, GTK_APP_PAINTABLE);
3635
3636       if (GTK_WIDGET_DRAWABLE (widget))
3637         gtk_widget_queue_clear (widget);
3638
3639       g_object_notify (G_OBJECT (widget), "app_paintable");
3640     }
3641 }
3642
3643 /**
3644  * gtk_widget_set_double_buffered:
3645  * @widget: a #GtkWidget
3646  * @double_buffered: %TRUE to double-buffer a widget
3647  *
3648  * Widgets are double buffered by default; you can use this function
3649  * to turn off the buffering. "Double buffered" simply means that
3650  * gdk_window_begin_paint_region() and gdk_window_end_paint() are called
3651  * automatically around expose events sent to the
3652  * widget. gdk_window_begin_paint() diverts all drawing to a widget's
3653  * window to an offscreen buffer, and gdk_window_end_paint() draws the
3654  * buffer to the screen. The result is that users see the window
3655  * update in one smooth step, and don't see individual graphics
3656  * primitives being rendered.
3657  *
3658  * In very simple terms, double buffered widgets don't flicker,
3659  * so you would only use this function to turn off double buffering
3660  * if you had special needs and really knew what you were doing.
3661  * 
3662  **/
3663 void
3664 gtk_widget_set_double_buffered (GtkWidget *widget,
3665                                 gboolean   double_buffered)
3666 {
3667   g_return_if_fail (GTK_IS_WIDGET (widget));
3668
3669   if (double_buffered)
3670     GTK_WIDGET_SET_FLAGS (widget, GTK_DOUBLE_BUFFERED);
3671   else
3672     GTK_WIDGET_UNSET_FLAGS (widget, GTK_DOUBLE_BUFFERED);
3673 }
3674
3675 /**
3676  * gtk_widget_set_redraw_on_allocate:
3677  * @widget: a #GtkWidget
3678  * @redraw_on_allocate: if %TRUE, the entire widget will be redrawn
3679  *   when it is allocated to a new size. Otherwise, only the
3680  *   new portion of the widget will be redrawn.
3681  *
3682  * Sets whether a when a widgets size allocation changes, the entire
3683  * widget is queued for drawing. By default, this setting is %TRUE and
3684  * the entire widget is redrawn on every size change. If your widget
3685  * leaves the upper left are unchanged when made bigger, turning this
3686  * setting on will improve performance.
3687
3688  * Note that for %NO_WINDOW widgets setting this flag to %FALSE turns
3689  * off all allocation on resizing: the widget will not even redraw if
3690  * its position changes; this is to allow containers that don't draw
3691  * anything to avoid excess invalidations. If you set this flag on a
3692  * %NO_WINDOW widget that <emphasis>does</emphasis> draw on @widget->window, 
3693  * you are responsible for invalidating both the old and new allocation 
3694  * of the widget when the widget is moved and responsible for invalidating
3695  * regions newly when the widget increases size.
3696  **/
3697 void
3698 gtk_widget_set_redraw_on_allocate (GtkWidget *widget,
3699                                    gboolean   redraw_on_allocate)
3700 {
3701   g_return_if_fail (GTK_IS_WIDGET (widget));
3702
3703   if (redraw_on_allocate)
3704     GTK_PRIVATE_SET_FLAG (widget, GTK_REDRAW_ON_ALLOC);
3705   else
3706     GTK_PRIVATE_UNSET_FLAG (widget, GTK_REDRAW_ON_ALLOC);
3707 }
3708
3709 /**
3710  * gtk_widget_set_sensitive:
3711  * @widget: a #GtkWidget
3712  * @sensitive: %TRUE to make the widget sensitive
3713  *
3714  * Sets the sensitivity of a widget. A widget is sensitive if the user
3715  * can interact with it. Insensitive widgets are "grayed out" and the
3716  * user can't interact with them. Insensitive widgets are known as
3717  * "inactive", "disabled", or "ghosted" in some other toolkits.
3718  * 
3719  **/
3720 void
3721 gtk_widget_set_sensitive (GtkWidget *widget,
3722                           gboolean   sensitive)
3723 {
3724   GtkStateData data;
3725
3726   g_return_if_fail (GTK_IS_WIDGET (widget));
3727
3728   sensitive = (sensitive != FALSE);
3729
3730   if (sensitive == (GTK_WIDGET_SENSITIVE (widget) != FALSE))
3731     return;
3732
3733   if (sensitive)
3734     {
3735       GTK_WIDGET_SET_FLAGS (widget, GTK_SENSITIVE);
3736       data.state = GTK_WIDGET_SAVED_STATE (widget);
3737     }
3738   else
3739     {
3740       GTK_WIDGET_UNSET_FLAGS (widget, GTK_SENSITIVE);
3741       data.state = GTK_WIDGET_STATE (widget);
3742     }
3743   data.state_restoration = TRUE;
3744   data.use_forall = TRUE;
3745
3746   if (widget->parent)
3747     data.parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (widget->parent) != FALSE);
3748   else
3749     data.parent_sensitive = TRUE;
3750
3751   gtk_widget_propagate_state (widget, &data);
3752   if (GTK_WIDGET_DRAWABLE (widget))
3753     gtk_widget_queue_clear (widget);
3754
3755   g_object_notify (G_OBJECT (widget), "sensitive");
3756 }
3757
3758 /**
3759  * gtk_widget_set_parent:
3760  * @widget: a #GtkWidget
3761  * @parent: parent container
3762  *
3763  * This function is useful only when implementing subclasses of #GtkContainer.
3764  * Sets the container as the parent of @widget, and takes care of
3765  * some details such as updating the state and style of the child
3766  * to reflect its new location. The opposite function is
3767  * gtk_widget_unparent().
3768  * 
3769  **/
3770 void
3771 gtk_widget_set_parent (GtkWidget *widget,
3772                        GtkWidget *parent)
3773 {
3774   GtkStateData data;
3775   
3776   g_return_if_fail (GTK_IS_WIDGET (widget));
3777   g_return_if_fail (widget->parent == NULL);
3778   g_return_if_fail (!GTK_WIDGET_TOPLEVEL (widget));
3779   g_return_if_fail (GTK_IS_WIDGET (parent));
3780   g_return_if_fail (widget != parent);
3781
3782   /* keep this function in sync with gtk_menu_attach_to_widget()
3783    */
3784
3785   gtk_widget_ref (widget);
3786   gtk_object_sink (GTK_OBJECT (widget));
3787   widget->parent = parent;
3788
3789   if (GTK_WIDGET_STATE (parent) != GTK_STATE_NORMAL)
3790     data.state = GTK_WIDGET_STATE (parent);
3791   else
3792     data.state = GTK_WIDGET_STATE (widget);
3793   data.state_restoration = FALSE;
3794   data.parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (parent) != FALSE);
3795   data.use_forall = GTK_WIDGET_IS_SENSITIVE (parent) != GTK_WIDGET_IS_SENSITIVE (widget);
3796
3797   gtk_widget_propagate_state (widget, &data);
3798   
3799   gtk_widget_reset_rc_styles (widget);
3800
3801   gtk_signal_emit (GTK_OBJECT (widget), widget_signals[PARENT_SET], NULL);
3802   if (GTK_WIDGET_ANCHORED (widget->parent))
3803     _gtk_widget_propagate_hierarchy_changed (widget, NULL);
3804   g_object_notify (G_OBJECT (widget), "parent");
3805
3806   /* Enforce realized/mapped invariants
3807    */
3808   if (GTK_WIDGET_REALIZED (widget->parent))
3809     gtk_widget_realize (widget);
3810
3811   if (GTK_WIDGET_VISIBLE (widget->parent) &&
3812       GTK_WIDGET_VISIBLE (widget))
3813     {
3814       if (GTK_WIDGET_CHILD_VISIBLE (widget) &&
3815           GTK_WIDGET_MAPPED (widget->parent))
3816         gtk_widget_map (widget);
3817
3818       gtk_widget_queue_resize (widget);
3819     }
3820 }
3821
3822 /**
3823  * gtk_widget_get_parent:
3824  * @widget: a #GtkWidget
3825  *
3826  * Returns the parent container of @widget.
3827  *
3828  * Return value: the parent container of @widget, or %NULL
3829  **/
3830 GtkWidget *
3831 gtk_widget_get_parent (GtkWidget *widget)
3832 {
3833   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
3834
3835   return widget->parent;
3836 }
3837
3838 /*****************************************
3839  * Widget styles
3840  * see docs/styles.txt
3841  *****************************************/
3842
3843 /**
3844  * gtk_widget_set_style:
3845  * @widget: a #GtkWidget
3846  * @style: a #GtkStyle, or %NULL to remove the effect of a previous
3847  *         gtk_widget_set_style() and go back to the default style
3848  *
3849  * Sets the #GtkStyle for a widget (@widget->style). You probably don't
3850  * want to use this function; it interacts badly with themes, because
3851  * themes work by replacing the #GtkStyle. Instead, use
3852  * gtk_widget_modify_style().
3853  * 
3854  **/
3855 void
3856 gtk_widget_set_style (GtkWidget *widget,
3857                       GtkStyle  *style)
3858 {
3859   g_return_if_fail (GTK_IS_WIDGET (widget));
3860
3861   if (style)
3862     {
3863       gboolean initial_emission;
3864
3865       initial_emission = !GTK_WIDGET_RC_STYLE (widget) && !GTK_WIDGET_USER_STYLE (widget);
3866       
3867       GTK_WIDGET_UNSET_FLAGS (widget, GTK_RC_STYLE);
3868       GTK_PRIVATE_SET_FLAG (widget, GTK_USER_STYLE);
3869       
3870       gtk_widget_set_style_internal (widget, style, initial_emission);
3871     }
3872   else
3873     {
3874       if (GTK_WIDGET_USER_STYLE (widget))
3875         gtk_widget_reset_rc_style (widget);
3876     }
3877 }
3878
3879 /**
3880  * gtk_widget_ensure_style:
3881  * @widget: a #GtkWidget
3882  *
3883  * Ensures that @widget has a style (@widget->style). Not a very useful
3884  * function; most of the time, if you want the style, the widget is
3885  * realized, and realized widgets are guaranteed to have a style
3886  * already.
3887  * 
3888  **/
3889 void
3890 gtk_widget_ensure_style (GtkWidget *widget)
3891 {
3892   g_return_if_fail (GTK_IS_WIDGET (widget));
3893
3894   if (!GTK_WIDGET_USER_STYLE (widget) &&
3895       !GTK_WIDGET_RC_STYLE (widget))
3896     gtk_widget_reset_rc_style (widget);
3897 }
3898
3899 /* Look up the RC style for this widget, unsetting any user style that
3900  * may be in effect currently
3901  **/
3902 static void
3903 gtk_widget_reset_rc_style (GtkWidget *widget)
3904 {
3905   GtkStyle *new_style = NULL;
3906   gboolean initial_emission;
3907   
3908   g_return_if_fail (GTK_IS_WIDGET (widget));
3909
3910   initial_emission = !GTK_WIDGET_RC_STYLE (widget) && !GTK_WIDGET_USER_STYLE (widget);
3911
3912   GTK_PRIVATE_UNSET_FLAG (widget, GTK_USER_STYLE);
3913   GTK_WIDGET_SET_FLAGS (widget, GTK_RC_STYLE);
3914   
3915   if (gtk_widget_has_screen (widget))
3916     new_style = gtk_rc_get_style (widget);
3917   if (!new_style)
3918     new_style = gtk_widget_get_default_style ();
3919
3920   if (initial_emission || new_style != widget->style)
3921     gtk_widget_set_style_internal (widget, new_style, initial_emission);
3922 }
3923
3924 /**
3925  * gtk_widget_get_style:
3926  * @widget: a #GtkWidget
3927  * 
3928  * Simply an accessor function that returns @widget->style.
3929  * 
3930  * Return value: the widget's #GtkStyle
3931  **/
3932 GtkStyle*
3933 gtk_widget_get_style (GtkWidget *widget)
3934 {
3935   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
3936   
3937   return widget->style;
3938 }
3939
3940 /**
3941  * gtk_widget_modify_style:
3942  * @widget: a #GtkWidget
3943  * @style: the #GtkRcStyle holding the style modifications
3944  * 
3945  * Modifies style values on the widget. Modifications made using this
3946  * technique take precedence over style values set via an RC file,
3947  * however, they will be overriden if a style is explicitely set on
3948  * the widget using gtk_widget_set_style(). The #GtkRcStyle structure
3949  * is designed so each field can either be set or unset, so it is
3950  * possible, using this function, to modify some style values and
3951  * leave the others unchanged.
3952  *
3953  * Note that modifications made with this function are not cumulative
3954  * with previous calls to gtk_widget_modify_style() or with such
3955  * functions as gtk_widget_modify_fg(). If you wish to retain
3956  * previous values, you must first call gtk_widget_get_modifier_style(),
3957  * make your modifications to the returned style, then call
3958  * gtk_widget_modify_style() with that style. On the other hand,
3959  * if you first call gtk_widget_modify_style(), subsequent calls
3960  * to such functions gtk_widget_modify_fg() will have a cumulative
3961  * effect with the initial modifications.
3962  **/
3963 void       
3964 gtk_widget_modify_style (GtkWidget      *widget,
3965                          GtkRcStyle     *style)
3966 {
3967   GtkRcStyle *old_style;
3968
3969   g_return_if_fail (GTK_IS_WIDGET (widget));
3970   g_return_if_fail (GTK_IS_RC_STYLE (style));
3971   
3972   old_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
3973                                          quark_rc_style);
3974
3975   gtk_object_set_data_by_id_full (GTK_OBJECT (widget),
3976                                   quark_rc_style,
3977                                   gtk_rc_style_copy (style),
3978                                   (GtkDestroyNotify)gtk_rc_style_unref);
3979
3980   /* note that "style" may be invalid here if it was the old
3981    * modifier style and the only reference was our own.
3982    */
3983   
3984   if (GTK_WIDGET_RC_STYLE (widget))
3985     gtk_widget_reset_rc_style (widget);
3986 }
3987
3988 /**
3989  * gtk_widget_get_modifier_style:
3990  * @widget: a #GtkWidget
3991  * 
3992  * Returns the current modifier style for the widget. (As set by
3993  * gtk_widget_modify_style().) If no style has previously set, a new
3994  * #GtkRcStyle will be created with all values unset, and set as the
3995  * modifier style for the widget. If you make changes to this rc
3996  * style, you must call gtk_widget_modify_style(), passing in the
3997  * returned rc style, to make sure that your changes take effect.
3998  *
3999  * Caution: passing the style back to gtk_widget_modify_style() will
4000  * normally end up destroying it, because gtk_widget_modify_style() copies
4001  * the passed-in style and sets the copy as the new modifier style,
4002  * thus dropping any reference to the old modifier style. Add a reference
4003  * to the modifier style if you want to keep it alive.
4004  * 
4005  * Return value: the modifier style for the widget. This rc style is
4006  *   owned by the widget. If you want to keep a pointer to value this
4007  *   around, you must add a refcount using gtk_rc_style_ref().
4008  **/
4009 GtkRcStyle *
4010 gtk_widget_get_modifier_style (GtkWidget      *widget)
4011 {
4012   GtkRcStyle *rc_style;
4013   
4014   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4015
4016   rc_style = gtk_object_get_data_by_id (GTK_OBJECT (widget),
4017                                         quark_rc_style);
4018
4019   if (!rc_style)
4020     {
4021       rc_style = gtk_rc_style_new ();
4022       gtk_object_set_data_by_id_full (GTK_OBJECT (widget),
4023                                       quark_rc_style,
4024                                       rc_style,
4025                                       (GtkDestroyNotify)gtk_rc_style_unref);
4026     }
4027
4028   return rc_style;
4029 }
4030
4031 static void
4032 gtk_widget_modify_color_component (GtkWidget     *widget,
4033                                    GtkRcFlags     component,
4034                                    GtkStateType   state,
4035                                    GdkColor      *color)
4036 {
4037   GtkRcStyle *rc_style = gtk_widget_get_modifier_style (widget);  
4038
4039   if (color)
4040     {
4041       switch (component)
4042         {
4043         case GTK_RC_FG:
4044           rc_style->fg[state] = *color;
4045           break;
4046         case GTK_RC_BG:
4047           rc_style->bg[state] = *color;
4048           break;
4049         case GTK_RC_TEXT:
4050           rc_style->text[state] = *color;
4051           break;
4052         case GTK_RC_BASE:
4053           rc_style->base[state] = *color;
4054           break;
4055         default:
4056           g_assert_not_reached();
4057         }
4058       
4059       rc_style->color_flags[state] |= component;
4060     }
4061   else
4062     rc_style->color_flags[state] &= ~component;
4063
4064   gtk_widget_modify_style (widget, rc_style);
4065 }
4066
4067 /**
4068  * gtk_widget_modify_fg:
4069  * @widget: a #GtkWidget.
4070  * @state: the state for which to set the foreground color.
4071  * @color: the color to assign (does not need to be allocated),
4072  *         or %NULL to undo the effect of previous calls to
4073  *         of gtk_widget_modify_fg().
4074  * 
4075  * Sets the foreground color for a widget in a particular state.  All
4076  * other style values are left untouched. See also
4077  * gtk_widget_modify_style().
4078  **/
4079 void
4080 gtk_widget_modify_fg (GtkWidget   *widget,
4081                       GtkStateType state,
4082                       GdkColor    *color)
4083 {
4084   g_return_if_fail (GTK_IS_WIDGET (widget));
4085   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
4086   g_return_if_fail (color != NULL);
4087
4088   gtk_widget_modify_color_component (widget, GTK_RC_FG, state, color);
4089 }
4090
4091 /**
4092  * gtk_widget_modify_bg:
4093  * @widget: a #GtkWidget.
4094  * @state: the state for which to set the background color.
4095  * @color: the color to assign (does not need to be allocated),
4096  *         or %NULL to undo the effect of previous calls to
4097  *         of gtk_widget_modify_bg().
4098  * 
4099  * Sets the background color for a widget in a particular state.  All
4100  * other style values are left untouched. See also
4101  * gtk_widget_modify_style().
4102  **/
4103 void
4104 gtk_widget_modify_bg (GtkWidget   *widget,
4105                       GtkStateType state,
4106                       GdkColor    *color)
4107 {
4108   g_return_if_fail (GTK_IS_WIDGET (widget));
4109   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
4110   g_return_if_fail (color != NULL);
4111
4112   gtk_widget_modify_color_component (widget, GTK_RC_BG, state, color);
4113 }
4114
4115 /**
4116  * gtk_widget_modify_text:
4117  * @widget: a #GtkWidget.
4118  * @state: the state for which to set the text color.
4119  * @color: the color to assign (does not need to be allocated),
4120  *         or %NULL to undo the effect of previous calls to
4121  *         of gtk_widget_modify_text().
4122  * 
4123  * Sets the text color for a widget in a particular state.  All other
4124  * style values are left untouched. The text color is the foreground
4125  * color used along with the base color (see gtk_widget_modify_base())
4126  * for widgets such as #GtkEntry and #GtkTextView. See also
4127  * gtk_widget_modify_style().
4128  **/
4129 void
4130 gtk_widget_modify_text (GtkWidget   *widget,
4131                         GtkStateType state,
4132                         GdkColor    *color)
4133 {
4134   g_return_if_fail (GTK_IS_WIDGET (widget));
4135   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
4136   g_return_if_fail (color != NULL);
4137
4138   gtk_widget_modify_color_component (widget, GTK_RC_TEXT, state, color);
4139 }
4140
4141 /**
4142  * gtk_widget_modify_base:
4143  * @widget: a #GtkWidget.
4144  * @state: the state for which to set the base color.
4145  * @color: the color to assign (does not need to be allocated),
4146  *         or %NULL to undo the effect of previous calls to
4147  *         of gtk_widget_modify_base().
4148  * 
4149  * Sets the base color for a widget in a particular state.
4150  * All other style values are left untouched. The base color
4151  * is the background color used along with the text color
4152  * (see gtk_widget_modify_text()) for widgets such as #GtkEntry
4153  * and #GtkTextView. See also gtk_widget_modify_style().
4154  **/
4155 void
4156 gtk_widget_modify_base (GtkWidget  *widget,
4157                         GtkStateType state,
4158                         GdkColor    *color)
4159 {
4160   g_return_if_fail (GTK_IS_WIDGET (widget));
4161   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
4162   g_return_if_fail (color != NULL);
4163
4164   gtk_widget_modify_color_component (widget, GTK_RC_BASE, state, color);
4165 }
4166
4167 /**
4168  * gtk_widget_modify_font:
4169  * @widget: a #GtkWidget
4170  * @font_desc: the font description to use, or %NULL to undo
4171  *   the effect of previous calls to gtk_widget_modify_font().
4172  * 
4173  * Sets the font to use for a widget.  All other style values are left
4174  * untouched. See also gtk_widget_modify_style().
4175  **/
4176 void
4177 gtk_widget_modify_font (GtkWidget            *widget,
4178                         PangoFontDescription *font_desc)
4179 {
4180   GtkRcStyle *rc_style;
4181
4182   g_return_if_fail (GTK_IS_WIDGET (widget));
4183   g_return_if_fail (font_desc != NULL);
4184
4185   rc_style = gtk_widget_get_modifier_style (widget);  
4186
4187   if (rc_style->font_desc)
4188     pango_font_description_free (rc_style->font_desc);
4189
4190   if (font_desc)
4191     rc_style->font_desc = pango_font_description_copy (font_desc);
4192   else
4193     rc_style->font_desc = NULL;
4194   
4195   gtk_widget_modify_style (widget, rc_style);
4196 }
4197
4198 static void
4199 gtk_widget_direction_changed (GtkWidget        *widget,
4200                               GtkTextDirection  previous_direction)
4201 {
4202   gtk_widget_queue_resize (widget);
4203 }
4204
4205 static void
4206 gtk_widget_style_set (GtkWidget *widget,
4207                       GtkStyle  *previous_style)
4208 {
4209   if (GTK_WIDGET_REALIZED (widget) &&
4210       !GTK_WIDGET_NO_WINDOW (widget))
4211     gtk_style_set_background (widget->style, widget->window, widget->state);
4212 }
4213
4214 static void
4215 gtk_widget_set_style_internal (GtkWidget *widget,
4216                                GtkStyle  *style,
4217                                gboolean   initial_emission)
4218 {
4219   g_object_ref (G_OBJECT (widget));
4220   g_object_freeze_notify (G_OBJECT (widget));
4221
4222   if (widget->style != style || initial_emission)
4223     {
4224       PangoContext *context = gtk_widget_peek_pango_context (widget);
4225       if (context)
4226         pango_context_set_font_description (context, style->font_desc);
4227     }
4228   
4229   if (widget->style != style)
4230     {
4231       GtkStyle *previous_style;
4232
4233       if (GTK_WIDGET_REALIZED (widget))
4234         {
4235           gtk_widget_reset_shapes (widget);
4236           gtk_style_detach (widget->style);
4237         }
4238       
4239       previous_style = widget->style;
4240       widget->style = style;
4241       g_object_ref (widget->style);
4242       
4243       if (GTK_WIDGET_REALIZED (widget))
4244         widget->style = gtk_style_attach (widget->style, widget->window);
4245
4246       gtk_signal_emit (GTK_OBJECT (widget),
4247                        widget_signals[STYLE_SET],
4248                        initial_emission ? NULL : previous_style);
4249       g_object_unref (previous_style);
4250
4251       if (widget->parent && !initial_emission)
4252         gtk_widget_queue_resize (widget);
4253     }
4254   else if (initial_emission)
4255     gtk_signal_emit (GTK_OBJECT (widget),
4256                      widget_signals[STYLE_SET],
4257                      NULL);
4258   g_object_notify (G_OBJECT (widget), "style");
4259   g_object_thaw_notify (G_OBJECT (widget));
4260   g_object_unref (G_OBJECT (widget));
4261 }
4262
4263 static void
4264 gtk_widget_propagate_hierarchy_changed_recurse (GtkWidget *widget,
4265                                                 gpointer   client_data)
4266 {
4267   gboolean new_anchored;
4268
4269   new_anchored = GTK_WIDGET_TOPLEVEL (widget) ||
4270                  (widget->parent && GTK_WIDGET_ANCHORED (widget->parent));
4271
4272   if (GTK_WIDGET_ANCHORED (widget) != new_anchored)
4273     {
4274       gtk_widget_ref (widget);
4275       
4276       if (new_anchored)
4277         GTK_PRIVATE_SET_FLAG (widget, GTK_ANCHORED);
4278       else
4279         GTK_PRIVATE_UNSET_FLAG (widget, GTK_ANCHORED);
4280       
4281       g_signal_emit (GTK_OBJECT (widget), widget_signals[HIERARCHY_CHANGED],
4282                      0, client_data);
4283   
4284       if (GTK_IS_CONTAINER (widget))
4285         gtk_container_forall (GTK_CONTAINER (widget),
4286                               gtk_widget_propagate_hierarchy_changed_recurse,
4287                               client_data);
4288       
4289       gtk_widget_unref (widget);
4290     }
4291 }
4292
4293 /**
4294  * _gtk_widget_propagate_hierarchy_changed:
4295  * @widget: a #GtkWidget
4296  * @previous_toplevel: Previous toplevel
4297  * 
4298  * Propagates changes in the anchored state to a widget and all
4299  * children, unsetting or setting the %ANCHORED flag, and
4300  * emitting ::hierarchy_changed.
4301  **/
4302 void
4303 _gtk_widget_propagate_hierarchy_changed (GtkWidget    *widget,
4304                                          GtkWidget    *previous_toplevel)
4305 {
4306   if (previous_toplevel)
4307     g_object_ref (previous_toplevel);
4308
4309   gtk_widget_propagate_hierarchy_changed_recurse (widget, previous_toplevel);
4310   
4311   if (previous_toplevel)
4312     g_object_unref (previous_toplevel);
4313 }
4314
4315 static void
4316 reset_rc_styles_recurse (GtkWidget *widget, gpointer data)
4317 {
4318   if (GTK_WIDGET_RC_STYLE (widget))
4319     gtk_widget_reset_rc_style (widget);
4320   
4321   if (GTK_IS_CONTAINER (widget))
4322     gtk_container_forall (GTK_CONTAINER (widget),
4323                           reset_rc_styles_recurse,
4324                           NULL);
4325 }
4326
4327 void
4328 gtk_widget_reset_rc_styles (GtkWidget *widget)
4329 {
4330   g_return_if_fail (GTK_IS_WIDGET (widget));
4331
4332   reset_rc_styles_recurse (widget, NULL);
4333 }
4334
4335 /**
4336  * gtk_widget_get_default_style:
4337  * 
4338  * Returns the default style used by all widgets initially.
4339  * 
4340  * Returns: the default style. This #GtkStyle object is owned by GTK+ and
4341  * should not be modified or freed.
4342  */ 
4343 GtkStyle*
4344 gtk_widget_get_default_style (void)
4345 {
4346   if (!gtk_default_style)
4347     {
4348       gtk_default_style = gtk_style_new ();
4349       g_object_ref (gtk_default_style);
4350     }
4351   
4352   return gtk_default_style;
4353 }
4354
4355 static PangoContext *
4356 gtk_widget_peek_pango_context (GtkWidget *widget)
4357 {
4358   return gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_pango_context);
4359 }
4360
4361 /**
4362  * gtk_widget_get_pango_context:
4363  * @widget: a #GtkWidget
4364  * 
4365  * Gets a #PangoContext with the appropriate colormap, font description
4366  * and base direction for this widget. Unlike the context returned
4367  * by gtk_widget_create_pango_context(), this context is owned by
4368  * the widget (it can be used as long as widget exists), and will
4369  * be updated to match any changes to the widget's attributes.
4370  *
4371  * If you create and keep a #PangoLayout using this context, you must
4372  * deal with changes to the context by calling pango_layout_context_changed()
4373  * on the layout in response to the ::style_set and ::direction_set signals
4374  * for the widget.
4375  *
4376  * Return value: the #PangoContext for the widget.
4377  **/
4378 PangoContext *
4379 gtk_widget_get_pango_context (GtkWidget *widget)
4380 {
4381   PangoContext *context;
4382   GdkScreen *screen;
4383
4384   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4385   
4386   context = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_pango_context);
4387   if (context)
4388     {
4389       screen = g_object_get_data (G_OBJECT (context), "gdk-pango-screen");
4390       if (screen && (screen != gtk_widget_get_screen (widget)))
4391           context = NULL;
4392     }
4393   
4394   if (!context)
4395     {
4396       context = gtk_widget_create_pango_context (GTK_WIDGET (widget));
4397       gtk_object_set_data_by_id_full (GTK_OBJECT (widget), quark_pango_context, context,
4398                                       (GDestroyNotify) g_object_unref);
4399     }
4400
4401   return context;
4402 }
4403
4404 /**
4405  * gtk_widget_create_pango_context:
4406  * @widget: a #GtkWidget
4407  * 
4408  * Creates a new #PangoContext with the appropriate colormap,
4409  * font description, and base direction for drawing text for
4410  * this widget. See also gtk_widget_get_pango_context().
4411  * 
4412  * Return value: the new #PangoContext
4413  **/
4414 PangoContext *
4415 gtk_widget_create_pango_context (GtkWidget *widget)
4416 {
4417   PangoContext *context;
4418
4419   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4420
4421   context = gdk_pango_context_get_for_screen (gtk_widget_get_screen (widget));
4422
4423   gdk_pango_context_set_colormap (context, gtk_widget_get_colormap (widget));
4424   pango_context_set_base_dir (context,
4425                               gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
4426                                 PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
4427   pango_context_set_font_description (context, widget->style->font_desc);
4428   pango_context_set_language (context, gtk_get_default_language ());
4429
4430   return context;
4431 }
4432
4433 /**
4434  * gtk_widget_create_pango_layout:
4435  * @widget: a #GtkWidget
4436  * @text:   text to set on the layout (can be %NULL)
4437  * 
4438  * Creates a new #PangoLayout with the appropriate colormap,
4439  * font description, and base direction for drawing text for
4440  * this widget.
4441  *
4442  * If you keep a #PangoLayout created in this way around, in order
4443  * notify the layout of changes to the base direction or font of this
4444  * widget, you must call pango_layout_context_changed() in response to
4445  * the ::style_set and ::direction_set signals for the widget.
4446  * 
4447  * Return value: the new #PangoLayout
4448  **/
4449 PangoLayout *
4450 gtk_widget_create_pango_layout (GtkWidget   *widget,
4451                                 const gchar *text)
4452 {
4453   PangoLayout *layout;
4454   PangoContext *context;
4455
4456   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4457
4458   context = gtk_widget_get_pango_context (widget);
4459   layout = pango_layout_new (context);
4460
4461   if (text)
4462     pango_layout_set_text (layout, text, -1);
4463
4464   return layout;
4465 }
4466
4467 /**
4468  * gtk_widget_render_icon:
4469  * @widget: a #GtkWidget
4470  * @stock_id: a stock ID
4471  * @size: a stock size
4472  * @detail: render detail to pass to theme engine
4473  * 
4474  * A convenience function that uses the theme engine and RC file
4475  * settings for @widget to look up @stock_id and render it to
4476  * a pixbuf. @stock_id should be a stock icon ID such as
4477  * #GTK_STOCK_OPEN or #GTK_STOCK_OK. @size should be a size
4478  * such as #GTK_ICON_SIZE_MENU. @detail should be a string that
4479  * identifies the widget or code doing the rendering, so that
4480  * theme engines can special-case rendering for that widget or code.
4481  *
4482  * The pixels in the returned #GdkPixbuf are shared with the rest of
4483  * the application and should not be modified. The pixbuf should be freed
4484  * after use with g_object_unref().
4485  *
4486  * Return value: a new pixbuf, or %NULL if the stock ID wasn't known
4487  **/
4488 GdkPixbuf*
4489 gtk_widget_render_icon (GtkWidget      *widget,
4490                         const gchar    *stock_id,
4491                         GtkIconSize     size,
4492                         const gchar    *detail)
4493 {
4494   GtkIconSet *icon_set;
4495   GdkPixbuf *retval;
4496   
4497   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4498   g_return_val_if_fail (stock_id != NULL, NULL);
4499   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID, NULL);
4500   
4501   gtk_widget_ensure_style (widget);
4502   
4503   icon_set = gtk_style_lookup_icon_set (widget->style, stock_id);
4504
4505   if (icon_set == NULL)
4506     return NULL;
4507
4508   retval = gtk_icon_set_render_icon (icon_set,
4509                                      widget->style,
4510                                      gtk_widget_get_direction (widget),
4511                                      GTK_WIDGET_STATE (widget),
4512                                      size,
4513                                      widget,
4514                                      detail);
4515
4516   return retval;
4517 }
4518
4519 /**
4520  * gtk_widget_set_parent_window:
4521  * @widget: a #GtkWidget.
4522  * @parent_window: the new parent window.
4523  *  
4524  * Sets a non default parent window for @widget.
4525  **/
4526 void
4527 gtk_widget_set_parent_window   (GtkWidget           *widget,
4528                                 GdkWindow           *parent_window)
4529 {
4530   GdkWindow *old_parent_window;
4531
4532   g_return_if_fail (GTK_IS_WIDGET (widget));
4533   
4534   old_parent_window = gtk_object_get_data_by_id (GTK_OBJECT (widget),
4535                                                  quark_parent_window);
4536
4537   if (parent_window != old_parent_window)
4538     {
4539       gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_parent_window, 
4540                                  parent_window);
4541       if (old_parent_window)
4542         gdk_window_unref (old_parent_window);
4543       if (parent_window)
4544         gdk_window_ref (parent_window);
4545     }
4546 }
4547
4548
4549 /**
4550  * gtk_widget_set_child_visible:
4551  * @widget: a #GtkWidget
4552  * @is_visible: if %TRUE, @widget should be mapped along with its parent.
4553  *
4554  * Sets whether @widget should be mapped along with its when its parent
4555  * is mapped and @widget has been shown with gtk_widget_show(). 
4556  *
4557  * The child visibility can be set for widget before it is added to
4558  * a container with gtk_widget_set_parent(), to avoid mapping
4559  * children unnecessary before immediately unmapping them. However
4560  * it will be reset to its default state of %TRUE when the widget
4561  * is removed from a container.
4562  * 
4563  * Note that changing the child visibility of a widget does not
4564  * queue a resize on the widget. Most of the time, the size of
4565  * a widget is computed from all visible children, whether or
4566  * not they are mapped. If this is not the case, the container
4567  * can queue a resize itself.
4568  *
4569  * This function is only useful for container implementations and
4570  * never should be called by an application.
4571  **/
4572 void
4573 gtk_widget_set_child_visible (GtkWidget *widget,
4574                               gboolean   is_visible)
4575 {
4576   g_return_if_fail (GTK_IS_WIDGET (widget));
4577   g_return_if_fail (!GTK_WIDGET_TOPLEVEL (widget));
4578
4579   if (is_visible)
4580     GTK_PRIVATE_SET_FLAG (widget, GTK_CHILD_VISIBLE);
4581   else
4582     GTK_PRIVATE_UNSET_FLAG (widget, GTK_CHILD_VISIBLE);
4583   if (widget->parent && GTK_WIDGET_REALIZED (widget->parent))
4584     {
4585       if (GTK_WIDGET_MAPPED (widget->parent) &&
4586           GTK_WIDGET_CHILD_VISIBLE (widget) &&
4587           GTK_WIDGET_VISIBLE (widget))
4588         gtk_widget_map (widget);
4589       else
4590         gtk_widget_unmap (widget);
4591     }
4592 }
4593
4594 /**
4595  * gtk_widget_get_child_visible:
4596  * @widget: a #GtkWidget
4597  * 
4598  * Gets the value set with gtk_widget_set_child_visible().
4599  * If you feel a need to use this function, your code probably
4600  * needs reorganization. 
4601  *
4602  * This function is only useful for container implementations and
4603  * never should be called by an application.
4604  *
4605  * Return value: %TRUE if the widget is mapped with the parent.
4606  **/
4607 gboolean
4608 gtk_widget_get_child_visible (GtkWidget *widget)
4609 {
4610   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4611   
4612   return GTK_WIDGET_CHILD_VISIBLE (widget);
4613 }
4614
4615 static GdkScreen *
4616 gtk_widget_get_screen_unchecked (GtkWidget *widget)
4617 {
4618   GtkWidget *toplevel;
4619   
4620   toplevel = gtk_widget_get_toplevel (widget);
4621
4622   if (GTK_WIDGET_TOPLEVEL (toplevel))
4623     {
4624       if (GTK_IS_WINDOW (toplevel))
4625         return GTK_WINDOW (toplevel)->screen;
4626       else if (GTK_IS_INVISIBLE (toplevel))
4627         return GTK_INVISIBLE (widget)->screen;
4628     }
4629
4630   return NULL;
4631 }
4632
4633 /**
4634  * gtk_widget_get_screen:
4635  * @widget: a #GtkWidget
4636  * 
4637  * Get the #GdkScreen from the toplevel window associated with
4638  * this widget. This function can only be called after the widget
4639  * has been added to a widget heirarchy with a #GtkWindow
4640  * at the top.
4641  *
4642  * In general, you should only create screen specific
4643  * resources when a widget has been realized, and you should
4644  * free those resources when the widget is unrealized.
4645  * 
4646  * Return value: the #GdkScreen for the toplevel for this widget.
4647  **/
4648 GdkScreen*
4649 gtk_widget_get_screen (GtkWidget *widget)
4650 {
4651   GdkScreen *screen;
4652   
4653   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4654
4655   screen = gtk_widget_get_screen_unchecked (widget);
4656
4657   if (screen)
4658     return screen;
4659   else
4660     {
4661 #if 0
4662       g_warning (G_STRLOC ": Can't get associated screen"
4663                  " for a widget unless it is inside a toplevel GtkWindow\n"
4664                  " widget type is %s associated top level type is %s",
4665                  g_type_name (G_OBJECT_TYPE(G_OBJECT (widget))),
4666                  g_type_name (G_OBJECT_TYPE(G_OBJECT (toplevel))));
4667 #endif
4668       return gdk_screen_get_default ();
4669     }
4670 }
4671
4672 /**
4673  * gtk_widget_has_screen:
4674  * @widget: a #GtkWidget
4675  * 
4676  * Checks whether there is a #GdkScreen is associated with
4677  * this widget. All toplevel widgets have an associated
4678  * screen, and all widgets added into a heirarchy with a toplevel
4679  * window at the top.
4680  * 
4681  * Return value: %TRUE if there is a #GdkScreen associcated
4682  *   with the widget.
4683  **/
4684 gboolean
4685 gtk_widget_has_screen (GtkWidget *widget)
4686 {
4687   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4688
4689   return (gtk_widget_get_screen_unchecked (widget) != NULL);
4690 }
4691
4692 /**
4693  * gtk_widget_get_display:
4694  * @widget: a #GtkWidget
4695  * 
4696  * Get the #GdkDisplay for the toplevel window associated with
4697  * this widget. This function can only be called after the widget
4698  * has been added to a widget hierarchy with a #GtkWindow at the top.
4699  *
4700  * In general, you should only create display specific
4701  * resources when a widget has been realized, and you should
4702  * free those resources when the widget is unrealized.
4703  * 
4704  * Return value: the #GdkDisplay for the toplevel for this widget.
4705  **/
4706 GdkDisplay*
4707 gtk_widget_get_display (GtkWidget *widget)
4708 {
4709   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4710   
4711   return gdk_screen_get_display (gtk_widget_get_screen (widget));
4712 }
4713
4714 /**
4715  * gtk_widget_get_root_window:
4716  * @widget: a #GtkWidget
4717  * 
4718  * Get the root window where this widget is located. This function can
4719  * only be called after the widget has been added to a widget
4720  * heirarchy with #GtkWindow at the top.
4721  *
4722  * The root window is useful for such purposes as creating a popup
4723  * #GdkWindow associated with the window. In general, you should only
4724  * create display specific resources when a widget has been realized,
4725  * and you should free those resources when the widget is unrealized.
4726  * 
4727  * Return value: the #GdkWindow root window for the toplevel for this widget.
4728  **/
4729 GdkWindow*
4730 gtk_widget_get_root_window (GtkWidget *widget)
4731 {
4732   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4733
4734   return gdk_screen_get_root_window (gtk_widget_get_screen (widget));
4735 }
4736
4737 /**
4738  * gtk_widget_get_parent_window:
4739  * @widget: a #GtkWidget.
4740  * @returns: the parent window of @widget.
4741  * 
4742  * Gets @widget's parent window.
4743  **/
4744 GdkWindow *
4745 gtk_widget_get_parent_window   (GtkWidget           *widget)
4746 {
4747   GdkWindow *parent_window;
4748
4749   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4750   g_return_val_if_fail (widget->parent != NULL, NULL);
4751   
4752   parent_window = gtk_object_get_data_by_id (GTK_OBJECT (widget),
4753                                              quark_parent_window);
4754
4755   return (parent_window != NULL) ? parent_window : widget->parent->window;
4756 }
4757
4758 /**
4759  * gtk_widget_child_focus:
4760  * @widget: a #GtkWidget
4761  * @direction: direction of focus movement
4762  *
4763  * This function is used by custom widget implementations; if you're
4764  * writing an app, you'd use gtk_widget_grab_focus() to move the focus
4765  * to a particular widget, and gtk_container_set_focus_chain() to
4766  * change the focus tab order. So you may want to investigate those
4767  * functions instead.
4768  * 
4769  * gtk_widget_child_focus() is called by containers as the user moves
4770  * around the window using keyboard shortcuts. @direction indicates
4771  * what kind of motion is taking place (up, down, left, right, tab
4772  * forward, tab backward).  gtk_widget_child_focus() invokes the
4773  * "focus" signal on #GtkWidget; widgets override the default handler
4774  * for this signal in order to implement appropriate focus behavior.
4775  *
4776  * The "focus" default handler for a widget should return %TRUE if
4777  * moving in @direction left the focus on a focusable location inside
4778  * that widget, and %FALSE if moving in @direction moved the focus
4779  * outside the widget. If returning %TRUE, widgets normally
4780  * call gtk_widget_grab_focus() to place the focus accordingly;
4781  * if returning %FALSE, they don't modify the current focus location.
4782  * 
4783  * This function replaces gtk_container_focus() from GTK+ 1.2.  It was
4784  * necessary to check that the child was visible, sensitive, and
4785  * focusable before calling
4786  * gtk_container_focus(). gtk_widget_child_focus() returns %FALSE if
4787  * the widget is not currently in a focusable state, so there's no
4788  * need for those checks.
4789  * 
4790  * Return value: %TRUE if focus ended up inside @widget
4791  **/
4792 gboolean
4793 gtk_widget_child_focus (GtkWidget       *widget,
4794                         GtkDirectionType direction)
4795 {
4796   gboolean return_val;
4797
4798   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4799
4800   if (!GTK_WIDGET_VISIBLE (widget) ||
4801       !GTK_WIDGET_IS_SENSITIVE (widget))
4802     return FALSE;
4803
4804   /* child widgets must set CAN_FOCUS, containers
4805    * don't have to though.
4806    */
4807   if (!GTK_IS_CONTAINER (widget) &&
4808       !GTK_WIDGET_CAN_FOCUS (widget))
4809     return FALSE;
4810   
4811   gtk_signal_emit (GTK_OBJECT (widget),
4812                    widget_signals[FOCUS],
4813                    direction, &return_val);
4814
4815   return return_val;
4816 }
4817
4818 /**
4819  * gtk_widget_set_uposition:
4820  * @widget: a #GtkWidget
4821  * @x: x position; -1 to unset x; -2 to leave x unchanged
4822  * @y: y position; -1 to unset y; -2 to leave y unchanged
4823  * 
4824  *
4825  * Sets the position of a widget. The funny "u" in the name comes from
4826  * the "user position" hint specified by the X Window System, and
4827  * exists for legacy reasons. This function doesn't work if a widget
4828  * is inside a container; it's only really useful on #GtkWindow.
4829  *
4830  * Don't use this function to center dialogs over the main application
4831  * window; most window managers will do the centering on your behalf
4832  * if you call gtk_window_set_transient_for(), and it's really not
4833  * possible to get the centering to work correctly in all cases from
4834  * application code. But if you insist, use gtk_window_set_position()
4835  * to set #GTK_WIN_POS_CENTER_ON_PARENT, don't do the centering
4836  * manually.
4837  *
4838  * Note that although @x and @y can be individually unset, the position
4839  * is not honoured unless both @x and @y are set.
4840  **/
4841 void
4842 gtk_widget_set_uposition (GtkWidget *widget,
4843                           gint       x,
4844                           gint       y)
4845 {
4846   /* FIXME this function is the only place that aux_info->x and
4847    * aux_info->y are even used I believe, and this function is
4848    * deprecated. Should be cleaned up.
4849    */
4850   
4851   GtkWidgetAuxInfo *aux_info;
4852   
4853   g_return_if_fail (GTK_IS_WIDGET (widget));
4854   
4855   aux_info =_gtk_widget_get_aux_info (widget, TRUE);
4856   
4857   if (x > -2)
4858     {
4859       if (x == -1)
4860         aux_info->x_set = FALSE;
4861       else
4862         {
4863           aux_info->x_set = TRUE;
4864           aux_info->x = x;
4865         }
4866     }
4867
4868   if (y > -2)
4869     {
4870       if (y == -1)
4871         aux_info->y_set = FALSE;
4872       else
4873         {
4874           aux_info->y_set = TRUE;
4875           aux_info->y = y;
4876         }
4877     }
4878
4879   if (GTK_IS_WINDOW (widget) && aux_info->x_set && aux_info->y_set)
4880     _gtk_window_reposition (GTK_WINDOW (widget), aux_info->x, aux_info->y);
4881   
4882   if (GTK_WIDGET_VISIBLE (widget) && widget->parent)
4883     gtk_widget_size_allocate (widget, &widget->allocation);
4884 }
4885
4886 /**
4887  * gtk_widget_set_usize:
4888  * @widget: a #GtkWidget
4889  * @width: minimum width, or -1 to unset
4890  * @height: minimum height, or -1 to unset
4891  *
4892  * This function is deprecated; use gtk_widget_set_size_request()
4893  * instead.
4894  * 
4895  * Sets the minimum size of a widget; that is, the widget's size
4896  * request will be @width by @height. You can use this function to
4897  * force a widget to be either larger or smaller than it is. The
4898  * strange "usize" name dates from the early days of GTK+, and derives
4899  * from X Window System terminology. In many cases,
4900  * gtk_window_set_default_size() is a better choice for toplevel
4901  * windows than this function; setting the default size will still
4902  * allow users to shrink the window. Setting the usize will force them
4903  * to leave the window at least as large as the usize. When dealing
4904  * with window sizes, gtk_window_set_geometry_hints() can be a useful
4905  * function as well.
4906  * 
4907  * Note the inherent danger of setting any fixed size - themes,
4908  * translations into other languages, different fonts, and user action
4909  * can all change the appropriate size for a given widget. So, it's
4910  * basically impossible to hardcode a size that will always be
4911  * correct.
4912  * 
4913  **/
4914 void
4915 gtk_widget_set_usize (GtkWidget *widget,
4916                       gint       width,
4917                       gint       height)
4918 {
4919   GtkWidgetAuxInfo *aux_info;
4920   
4921   g_return_if_fail (GTK_IS_WIDGET (widget));
4922   
4923   g_object_freeze_notify (G_OBJECT (widget));
4924
4925   aux_info =_gtk_widget_get_aux_info (widget, TRUE);
4926   
4927   if (width > -2)
4928     {
4929       g_object_notify (G_OBJECT (widget), "width_request");
4930       aux_info->width = width;
4931     }
4932   if (height > -2)
4933     {
4934       g_object_notify (G_OBJECT (widget), "height_request");  
4935       aux_info->height = height;
4936     }
4937   
4938   if (GTK_WIDGET_VISIBLE (widget))
4939     gtk_widget_queue_resize (widget);
4940
4941   g_object_thaw_notify (G_OBJECT (widget));
4942 }
4943
4944 /**
4945  * gtk_widget_set_size_request:
4946  * @widget: a #GtkWidget
4947  * @width: width @widget should request, or -1 to unset
4948  * @height: height @widget should request, or -1 to unset
4949  *
4950  * Sets the minimum size of a widget; that is, the widget's size
4951  * request will be @width by @height. You can use this function to
4952  * force a widget to be either larger or smaller than it normally
4953  * would be.
4954  *
4955  * In most cases, gtk_window_set_default_size() is a better choice for
4956  * toplevel windows than this function; setting the default size will
4957  * still allow users to shrink the window. Setting the size request
4958  * will force them to leave the window at least as large as the size
4959  * request. When dealing with window sizes,
4960  * gtk_window_set_geometry_hints() can be a useful function as well.
4961  * 
4962  * Note the inherent danger of setting any fixed size - themes,
4963  * translations into other languages, different fonts, and user action
4964  * can all change the appropriate size for a given widget. So, it's
4965  * basically impossible to hardcode a size that will always be
4966  * correct.
4967  *
4968  * The size request of a widget is the smallest size a widget can
4969  * accept while still functioning well and drawing itself correctly.
4970  * However in some strange cases a widget may be allocated less than
4971  * its requested size, and in many cases a widget may be allocated more
4972  * space than it requested.
4973  *
4974  * If the size request in a given direction is -1 (unset), then
4975  * the "natural" size request of the widget will be used instead.
4976  *
4977  * Widgets can't actually be allocated a size less than 1 by 1, but
4978  * you can pass 0,0 to this function to mean "as small as possible."
4979  **/
4980 void
4981 gtk_widget_set_size_request (GtkWidget *widget,
4982                              gint       width,
4983                              gint       height)
4984 {
4985   g_return_if_fail (GTK_IS_WIDGET (widget));
4986   g_return_if_fail (width >= -1);
4987   g_return_if_fail (height >= -1);
4988
4989   if (width == 0)
4990     width = 1;
4991   if (height == 0)
4992     height = 1;
4993   
4994   gtk_widget_set_usize (widget, width, height);
4995 }
4996
4997
4998 /**
4999  * gtk_widget_get_size_request:
5000  * @widget: a #GtkWidget
5001  * @width: return location for width, or %NULL
5002  * @height: return location for height, or %NULL
5003  *
5004  * Gets the size request that was explicitly set for the widget using
5005  * gtk_widget_set_size_request().  A value of -1 stored in @width or
5006  * @height indicates that that dimension has not been set explicitly
5007  * and the natural requisition of the widget will be used intead. See
5008  * gtk_widget_set_size_request(). To get the size a widget will
5009  * actually use, call gtk_widget_size_request() instead of
5010  * this function.
5011  * 
5012  **/
5013  
5014 void
5015 gtk_widget_get_size_request (GtkWidget *widget,
5016                              gint      *width,
5017                              gint      *height)
5018 {
5019   GtkWidgetAuxInfo *aux_info;
5020
5021   g_return_if_fail (GTK_IS_WIDGET (widget));
5022
5023   aux_info = _gtk_widget_get_aux_info (widget, FALSE);
5024
5025   if (width)
5026     *width = aux_info ? aux_info->width : -1;
5027
5028   if (height)
5029     *height = aux_info ? aux_info->height : -1;
5030 }
5031
5032 /**
5033  * gtk_widget_set_events:
5034  * @widget: a #GtkWidget
5035  * @events: event mask
5036  *
5037  * Sets the event mask (see #GdkEventMask) for a widget. The event
5038  * mask determines which events a widget will receive. Keep in mind
5039  * that different widgets have different default event masks, and by
5040  * changing the event mask you may disrupt a widget's functionality,
5041  * so be careful. This function must be called while a widget is
5042  * unrealized. Consider gtk_widget_add_events() for widgets that are
5043  * already realized, or if you want to preserve the existing event
5044  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
5045  * to get events on those widgets, place them inside a #GtkEventBox
5046  * and receive events on the event box.
5047  * 
5048  **/
5049 void
5050 gtk_widget_set_events (GtkWidget *widget,
5051                        gint       events)
5052 {
5053   gint *eventp;
5054   
5055   g_return_if_fail (GTK_IS_WIDGET (widget));
5056   g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
5057   
5058   eventp = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_event_mask);
5059   
5060   if (events)
5061     {
5062       if (!eventp)
5063         eventp = g_new (gint, 1);
5064       
5065       *eventp = events;
5066       gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_event_mask, eventp);
5067     }
5068   else if (eventp)
5069     {
5070       g_free (eventp);
5071       gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_event_mask);
5072     }
5073
5074   g_object_notify (G_OBJECT (widget), "events");
5075 }
5076
5077 /**
5078  * gtk_widget_add_events:
5079  * @widget: a #GtkWidget
5080  * @events: an event mask, see #GdkEventMask
5081  *
5082  * Adds the events in the bitfield @events to the event mask for
5083  * @widget. See gtk_widget_set_events() for details.
5084  * 
5085  **/
5086 void
5087 gtk_widget_add_events (GtkWidget *widget,
5088                        gint       events)
5089 {
5090   gint *eventp;
5091   
5092   g_return_if_fail (GTK_IS_WIDGET (widget));
5093
5094   eventp = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_event_mask);
5095   
5096   if (events)
5097     {
5098       if (!eventp)
5099         {
5100           eventp = g_new (gint, 1);
5101           *eventp = 0;
5102         }
5103       
5104       *eventp |= events;
5105       gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_event_mask, eventp);
5106     }
5107   else if (eventp)
5108     {
5109       g_free (eventp);
5110       gtk_object_remove_data_by_id (GTK_OBJECT (widget), quark_event_mask);
5111     }
5112
5113   if (GTK_WIDGET_REALIZED (widget))
5114     {
5115       if (GTK_WIDGET_NO_WINDOW (widget))
5116         {
5117           GList *children = gdk_window_get_children (widget->window);
5118           GList *tmp_list = children;
5119
5120           while (tmp_list)
5121             {
5122               GdkWindow *window = tmp_list->data;
5123               gpointer user_data;
5124
5125               gdk_window_get_user_data (window, &user_data);
5126               if (user_data == widget)
5127                 gdk_window_set_events (window, gdk_window_get_events (window) | events);
5128               tmp_list = tmp_list->next;
5129             }
5130
5131           g_list_free (children);
5132         }
5133       else
5134         {
5135           gdk_window_set_events (widget->window, gdk_window_get_events (widget->window) | events);
5136         }
5137     }
5138
5139   g_object_notify (G_OBJECT (widget), "events");
5140 }
5141
5142 /**
5143  * gtk_widget_set_extension_events:
5144  * @widget: a #GtkWidget
5145  * @mode: bitfield of extension events to receive
5146  *
5147  * Sets the extension events mask to @mode. See #GdkExtensionMode
5148  * and gdk_input_set_extension_events().
5149  * 
5150  **/
5151 void
5152 gtk_widget_set_extension_events (GtkWidget *widget,
5153                                  GdkExtensionMode mode)
5154 {
5155   GdkExtensionMode *modep;
5156   
5157   g_return_if_fail (GTK_IS_WIDGET (widget));
5158   
5159   modep = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_extension_event_mode);
5160   
5161   if (!modep)
5162     modep = g_new (GdkExtensionMode, 1);
5163   
5164   *modep = mode;
5165   gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_extension_event_mode, modep);
5166   g_object_notify (G_OBJECT (widget), "extension_events");
5167 }
5168
5169 /**
5170  * gtk_widget_get_toplevel:
5171  * @widget: a #GtkWidget
5172  * 
5173  * This function returns the topmost widget in the container hierarchy
5174  * @widget is a part of. If @widget has no parent widgets, it will be
5175  * returned as the topmost widget. No reference will be added to the
5176  * returned widget; it should not be unreferenced.
5177  *
5178  * Note the difference in behavior vs. gtk_widget_get_ancestor();
5179  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)</literal> 
5180  * would return
5181  * %NULL if @widget wasn't inside a toplevel window, and if the
5182  * window was inside a #GtkWindow-derived widget which was in turn
5183  * inside the toplevel #GtkWindow. While the second case may
5184  * seem unlikely, it actually happens when a #GtkPlug is embedded
5185  * inside a #GtkSocket within the same application.
5186  * 
5187  * To reliably find the toplevel #GtkWindow, use
5188  * gtk_widget_get_toplevel() and check if the %TOPLEVEL flags
5189  * is set on the result.
5190  * <informalexample><programlisting>
5191  *  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
5192  *  if (GTK_WIDGET_TOPLEVEL (toplevel))
5193  *    {
5194  *      [ Perform action on toplevel. ]
5195  *    }
5196  * </programlisting></informalexample>
5197  *
5198  * Return value: the topmost ancestor of @widget, or @widget itself if there's no ancestor.
5199  **/
5200 GtkWidget*
5201 gtk_widget_get_toplevel (GtkWidget *widget)
5202 {
5203   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5204   
5205   while (widget->parent)
5206     widget = widget->parent;
5207   
5208   return widget;
5209 }
5210
5211 /**
5212  * gtk_widget_get_ancestor:
5213  * @widget: a #GtkWidget
5214  * @widget_type: ancestor type
5215  * 
5216  * Gets the first ancestor of @widget with type @widget_type. For example,
5217  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_BOX)</literal> gets the 
5218  * first #GtkBox that's
5219  * an ancestor of @widget. No reference will be added to the returned widget;
5220  * it should not be unreferenced. See note about checking for a toplevel
5221  * #GtkWindow in the docs for gtk_widget_get_toplevel().
5222  * 
5223  * Return value: the ancestor widget, or %NULL if not found
5224  **/
5225 GtkWidget*
5226 gtk_widget_get_ancestor (GtkWidget *widget,
5227                          GtkType    widget_type)
5228 {
5229   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5230   
5231   while (widget && !gtk_type_is_a (GTK_WIDGET_TYPE (widget), widget_type))
5232     widget = widget->parent;
5233   
5234   if (!(widget && gtk_type_is_a (GTK_WIDGET_TYPE (widget), widget_type)))
5235     return NULL;
5236   
5237   return widget;
5238 }
5239
5240 /**
5241  * gtk_widget_get_colormap:
5242  * @widget: a #GtkWidget
5243  * 
5244  * Gets the colormap that will be used to render @widget. No reference will
5245  * be added to the returned colormap; it should not be unreferenced.
5246  * 
5247  * Return value: the colormap used by @widget
5248  **/
5249 GdkColormap*
5250 gtk_widget_get_colormap (GtkWidget *widget)
5251 {
5252   GdkColormap *colormap;
5253   GtkWidget *tmp_widget;
5254   
5255   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5256   
5257   if (widget->window)
5258     {
5259       colormap = gdk_window_get_colormap (widget->window);
5260       /* If window was destroyed previously, we'll get NULL here */
5261       if (colormap)
5262         return colormap;
5263     }
5264
5265   tmp_widget = widget;
5266   while (tmp_widget)
5267     {
5268       colormap = gtk_object_get_data_by_id (GTK_OBJECT (tmp_widget), quark_colormap);
5269       if (colormap)
5270         return colormap;
5271
5272       tmp_widget= tmp_widget->parent;
5273     }
5274
5275   return gdk_screen_get_default_colormap (gtk_widget_get_screen (widget));
5276 }
5277
5278 /**
5279  * gtk_widget_get_visual:
5280  * @widget: a #GtkWidget
5281  * 
5282  * Gets the visual that will be used to render @widget.
5283  * 
5284  * Return value: the visual for @widget
5285  **/
5286 GdkVisual*
5287 gtk_widget_get_visual (GtkWidget *widget)
5288 {
5289   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5290
5291   return gdk_colormap_get_visual (gtk_widget_get_colormap (widget));
5292 }
5293
5294 /**
5295  * gtk_widget_get_settings:
5296  * @widget: a #GtkWidget
5297  * 
5298  * Gets the settings object holding the settings (global property
5299  * settings, RC file information, etc) used for this widget.
5300  *
5301  * Note that this function can only be called when the #GtkWidget
5302  * is attached to a toplevel, since the settings object is specific
5303  * to a particular #GdkScreen.
5304  * 
5305  * Return value: the relevant #GtkSettings object
5306  **/
5307 GtkSettings*
5308 gtk_widget_get_settings (GtkWidget *widget)
5309 {
5310   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5311   
5312   return gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
5313 }
5314
5315 /**
5316  * gtk_widget_set_colormap:
5317  * @widget: a #GtkWidget
5318  * @colormap: a colormap
5319  *
5320  * Sets the colormap for the widget to the given value. Widget must not
5321  * have been previously realized. This probably should only be used
5322  * from an <function>init()</function> function (i.e. from the constructor 
5323  * for the widget).
5324  * 
5325  **/
5326 void
5327 gtk_widget_set_colormap (GtkWidget   *widget,
5328                          GdkColormap *colormap)
5329 {
5330   g_return_if_fail (GTK_IS_WIDGET (widget));
5331   g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
5332   g_return_if_fail (GDK_IS_COLORMAP (colormap));
5333
5334   g_object_ref (G_OBJECT (colormap));
5335   
5336   g_object_set_qdata_full (G_OBJECT (widget), 
5337                            quark_colormap,
5338                            colormap,
5339                            (GtkDestroyNotify) g_object_unref);
5340 }
5341
5342 /**
5343  * gtk_widget_get_events:
5344  * @widget: a #GtkWidget
5345  * 
5346  * Returns the event mask for the widget (a bitfield containing flags
5347  * from the #GdkEventMask enumeration). These are the events that the widget
5348  * will receive.
5349  * 
5350  * Return value: event mask for @widget
5351  **/
5352 gint
5353 gtk_widget_get_events (GtkWidget *widget)
5354 {
5355   gint *events;
5356   
5357   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
5358   
5359   events = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_event_mask);
5360   if (events)
5361     return *events;
5362   
5363   return 0;
5364 }
5365
5366 /**
5367  * gtk_widget_get_extension_events:
5368  * @widget: a #GtkWidget
5369  * 
5370  * Retrieves the extension events the widget will receive; see
5371  * gdk_input_set_extension_events().
5372  * 
5373  * Return value: extension events for @widget
5374  **/
5375 GdkExtensionMode
5376 gtk_widget_get_extension_events (GtkWidget *widget)
5377 {
5378   GdkExtensionMode *mode;
5379   
5380   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
5381   
5382   mode = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_extension_event_mode);
5383   if (mode)
5384     return *mode;
5385   
5386   return 0;
5387 }
5388
5389 /**
5390  * gtk_widget_get_pointer:
5391  * @widget: a #GtkWidget
5392  * @x: return location for the X coordinate, or %NULL
5393  * @y: return location for the Y coordinate, or %NULL
5394  *
5395  * Obtains the location of the mouse pointer in widget coordinates.
5396  * Widget coordinates are a bit odd; for historical reasons, they are
5397  * defined as @widget->window coordinates for widgets that are not
5398  * #GTK_NO_WINDOW widgets, and are relative to @widget->allocation.x,
5399  * @widget->allocation.y for widgets that are #GTK_NO_WINDOW widgets.
5400  * 
5401  **/
5402 void
5403 gtk_widget_get_pointer (GtkWidget *widget,
5404                         gint      *x,
5405                         gint      *y)
5406 {
5407   g_return_if_fail (GTK_IS_WIDGET (widget));
5408   
5409   if (x)
5410     *x = -1;
5411   if (y)
5412     *y = -1;
5413   
5414   if (GTK_WIDGET_REALIZED (widget))
5415     {
5416       gdk_window_get_pointer (widget->window, x, y, NULL);
5417       
5418       if (GTK_WIDGET_NO_WINDOW (widget))
5419         {
5420           if (x)
5421             *x -= widget->allocation.x;
5422           if (y)
5423             *y -= widget->allocation.y;
5424         }
5425     }
5426 }
5427
5428 /**
5429  * gtk_widget_is_ancestor:
5430  * @widget: a #GtkWidget
5431  * @ancestor: another #GtkWidget
5432  * 
5433  * Determines whether @widget is somewhere inside @ancestor, possibly with
5434  * intermediate containers.
5435  * 
5436  * Return value: %TRUE if @ancestor contains @widget as a child, grandchild, great grandchild, etc.
5437  **/
5438 gboolean
5439 gtk_widget_is_ancestor (GtkWidget *widget,
5440                         GtkWidget *ancestor)
5441 {
5442   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5443   g_return_val_if_fail (ancestor != NULL, FALSE);
5444   
5445   while (widget)
5446     {
5447       if (widget->parent == ancestor)
5448         return TRUE;
5449       widget = widget->parent;
5450     }
5451   
5452   return FALSE;
5453 }
5454
5455 static GQuark quark_composite_name = 0;
5456
5457 /**
5458  * gtk_widget_set_composite_name:
5459  * @widget: a #GtkWidget.
5460  * @name: the name to set.
5461  * 
5462  * Sets a widgets composite name. The widget must be
5463  * a composite child of its parent; see gtk_widget_push_composite_child().
5464  **/
5465 void
5466 gtk_widget_set_composite_name (GtkWidget   *widget,
5467                                const gchar *name)
5468 {
5469   g_return_if_fail (GTK_IS_WIDGET (widget));
5470   g_return_if_fail (GTK_WIDGET_COMPOSITE_CHILD (widget));
5471   g_return_if_fail (name != NULL);
5472
5473   if (!quark_composite_name)
5474     quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
5475
5476   gtk_object_set_data_by_id_full (GTK_OBJECT (widget),
5477                                   quark_composite_name,
5478                                   g_strdup (name),
5479                                   g_free);
5480 }
5481
5482 /**
5483  * gtk_widget_get_composite_name:
5484  * @widget: a #GtkWidget.
5485  * @returns: the composite name of @widget, or %NULL if @widget is not
5486  *   a composite child. The string should not be freed when it is no 
5487  *   longer needed.
5488  *
5489  * Obtains the composite name of a widget. 
5490  **/
5491 gchar*
5492 gtk_widget_get_composite_name (GtkWidget *widget)
5493 {
5494   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5495
5496   if (GTK_WIDGET_COMPOSITE_CHILD (widget) && widget->parent)
5497     return _gtk_container_child_composite_name (GTK_CONTAINER (widget->parent),
5498                                                widget);
5499   else
5500     return NULL;
5501 }
5502
5503 /**
5504  * gtk_widget_push_composite_child:
5505  * 
5506  * Makes all newly-created widgets as composite children until
5507  * the corresponding gtk_widget_pop_composite_child() call.
5508  * 
5509  * A composite child is a child that's an implementation detail of the
5510  * container it's inside and should not be visible to people using the
5511  * container. Composite children aren't treated differently by GTK (but
5512  * see gtk_container_foreach() vs. gtk_container_forall()), but e.g. GUI 
5513  * builders might want to treat them in a different way.
5514  * 
5515  * Here is a simple example:
5516  * <informalexample><programlisting>
5517  *   gtk_widget_push_composite_child (<!-- -->);
5518  *   scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
5519  *   gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
5520  *   gtk_widget_pop_composite_child (<!-- -->);
5521  *   gtk_widget_set_parent (scrolled_window->hscrollbar, 
5522  *                          GTK_WIDGET (scrolled_window));
5523  *   gtk_widget_ref (scrolled_window->hscrollbar);
5524  * </programlisting></informalexample>
5525  **/
5526 void
5527 gtk_widget_push_composite_child (void)
5528 {
5529   composite_child_stack++;
5530 }
5531
5532 /**
5533  * gtk_widget_pop_composite_child:
5534  *
5535  * Cancels the effect of a previous call to gtk_widget_push_composite_child().
5536  **/ 
5537 void
5538 gtk_widget_pop_composite_child (void)
5539 {
5540   if (composite_child_stack)
5541     composite_child_stack--;
5542 }
5543
5544 /**
5545  * gtk_widget_push_colormap:
5546  * @cmap: a #GdkColormap
5547  *
5548  * Pushes @cmap onto a global stack of colormaps; the topmost
5549  * colormap on the stack will be used to create all widgets.
5550  * Remove @cmap with gtk_widget_pop_colormap(). There's little
5551  * reason to use this function.
5552  * 
5553  **/
5554 void
5555 gtk_widget_push_colormap (GdkColormap *cmap)
5556 {
5557   g_return_if_fail (GDK_IS_COLORMAP (cmap));
5558
5559   colormap_stack = g_slist_prepend (colormap_stack, cmap);
5560 }
5561
5562 /**
5563  * gtk_widget_pop_colormap:
5564  *
5565  * Removes a colormap pushed with gtk_widget_push_colormap().
5566  * 
5567  **/
5568 void
5569 gtk_widget_pop_colormap (void)
5570 {
5571   if (colormap_stack)
5572     colormap_stack = g_slist_delete_link (colormap_stack, colormap_stack);
5573 }
5574
5575 /**
5576  * gtk_widget_set_default_colormap:
5577  * @colormap: a #GdkColormap
5578  * 
5579  * Sets the default colormap to use when creating widgets.
5580  * gtk_widget_push_colormap() is a better function to use if
5581  * you only want to affect a few widgets, rather than all widgets.
5582  **/
5583 void
5584 gtk_widget_set_default_colormap (GdkColormap *colormap)
5585 {
5586   g_return_if_fail (GDK_IS_COLORMAP (colormap));
5587   
5588   gdk_screen_set_default_colormap (gdk_colormap_get_screen (colormap),
5589                                    colormap);
5590 }
5591
5592 /**
5593  * gtk_widget_get_default_colormap:
5594  * 
5595  * Obtains the default colormap used to create widgets.
5596  * 
5597  * Return value: default widget colormap
5598  **/
5599 GdkColormap*
5600 gtk_widget_get_default_colormap (void)
5601 {
5602   return gdk_screen_get_default_colormap (gdk_screen_get_default ());
5603 }
5604
5605 /**
5606  * gtk_widget_get_default_visual:
5607  * 
5608  * Obtains the visual of the default colormap. Not really useful;
5609  * used to be useful before gdk_colormap_get_visual() existed.
5610  * 
5611  * Return value: visual of the default colormap
5612  **/
5613 GdkVisual*
5614 gtk_widget_get_default_visual (void)
5615 {
5616   return gdk_colormap_get_visual (gtk_widget_get_default_colormap ());
5617 }
5618
5619 static void
5620 gtk_widget_emit_direction_changed (GtkWidget        *widget,
5621                                    GtkTextDirection  old_dir)
5622 {
5623   PangoContext *context = gtk_widget_peek_pango_context (widget);
5624
5625   if (context)
5626     pango_context_set_base_dir (context,
5627                                 gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
5628                                   PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
5629   
5630   gtk_signal_emit (GTK_OBJECT (widget), widget_signals[DIRECTION_CHANGED], old_dir);
5631 }
5632
5633 /**
5634  * gtk_widget_set_direction:
5635  * @widget: a #GtkWidget
5636  * @dir:    the new direction
5637  * 
5638  * Sets the reading direction on a particular widget. This direction
5639  * controls the primary direction for widgets containing text,
5640  * and also the direction in which the children of a container are
5641  * packed. The ability to set the direction is present in order
5642  * so that correct localization into languages with right-to-left
5643  * reading directions can be done. Generally, applications will
5644  * let the default reading direction present, except for containers
5645  * where the containers are arranged in an order that is explicitely
5646  * visual rather than logical (such as buttons for text justification).
5647  *
5648  * If the direction is set to %GTK_TEXT_DIR_NONE, then the value
5649  * set by gtk_widget_set_default_direction() will be used.
5650  **/
5651 void
5652 gtk_widget_set_direction (GtkWidget        *widget,
5653                           GtkTextDirection  dir)
5654 {
5655   GtkTextDirection old_dir;
5656   
5657   g_return_if_fail (GTK_IS_WIDGET (widget));
5658   g_return_if_fail (dir >= GTK_TEXT_DIR_NONE && dir <= GTK_TEXT_DIR_RTL);
5659
5660   old_dir = gtk_widget_get_direction (widget);
5661   
5662   if (dir == GTK_TEXT_DIR_NONE)
5663     GTK_PRIVATE_UNSET_FLAG (widget, GTK_DIRECTION_SET);
5664   else
5665     {
5666       GTK_PRIVATE_SET_FLAG (widget, GTK_DIRECTION_SET);
5667       if (dir == GTK_TEXT_DIR_LTR)
5668         GTK_PRIVATE_SET_FLAG (widget, GTK_DIRECTION_LTR);
5669       else
5670         GTK_PRIVATE_UNSET_FLAG (widget, GTK_DIRECTION_LTR);
5671     }
5672
5673   if (old_dir != gtk_widget_get_direction (widget))
5674     gtk_widget_emit_direction_changed (widget, old_dir);
5675 }
5676
5677 /**
5678  * gtk_widget_get_direction:
5679  * @widget: a #GtkWidget
5680  * 
5681  * Gets the reading direction for a particular widget. See
5682  * gtk_widget_set_direction().
5683  * 
5684  * Return value: the reading direction for the widget.
5685  **/
5686 GtkTextDirection
5687 gtk_widget_get_direction (GtkWidget *widget)
5688 {
5689   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_TEXT_DIR_LTR);
5690   
5691   if (GTK_WIDGET_DIRECTION_SET (widget))
5692     return GTK_WIDGET_DIRECTION_LTR (widget) ? GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
5693   else
5694     return gtk_default_direction;
5695 }
5696
5697 static void
5698 gtk_widget_set_default_direction_recurse (GtkWidget *widget, gpointer data)
5699 {
5700   GtkTextDirection old_dir = GPOINTER_TO_UINT (data);
5701
5702   g_object_ref (G_OBJECT (widget));
5703   
5704   if (!GTK_WIDGET_DIRECTION_SET (widget))
5705     gtk_widget_emit_direction_changed (widget, old_dir);
5706   
5707   if (GTK_IS_CONTAINER (widget))
5708     gtk_container_forall (GTK_CONTAINER (widget),
5709                           gtk_widget_set_default_direction_recurse,
5710                           data);
5711
5712   g_object_unref (G_OBJECT (widget));
5713 }
5714
5715 /**
5716  * gtk_widget_set_default_direction:
5717  * @dir: the new default direction. This cannot be
5718  *        %GTK_TEXT_DIR_NONE.
5719  * 
5720  * Sets the default reading direction for widgets where the
5721  * direction has not been explicitly set by gtk_widget_set_direction().
5722  **/
5723 void
5724 gtk_widget_set_default_direction (GtkTextDirection dir)
5725 {
5726   g_return_if_fail (dir == GTK_TEXT_DIR_RTL || dir == GTK_TEXT_DIR_LTR);
5727
5728   if (dir != gtk_default_direction)
5729     {
5730       GList *toplevels, *tmp_list;
5731       GtkTextDirection old_dir = gtk_default_direction;
5732       
5733       gtk_default_direction = dir;
5734
5735       tmp_list = toplevels = gtk_window_list_toplevels ();
5736       g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
5737       
5738       while (tmp_list)
5739         {
5740           gtk_widget_set_default_direction_recurse (tmp_list->data,
5741                                                     GUINT_TO_POINTER (old_dir));
5742           g_object_unref (tmp_list->data);
5743           tmp_list = tmp_list->next;
5744         }
5745
5746       g_list_free (toplevels);
5747     }
5748 }
5749
5750 /**
5751  * gtk_widget_get_default_direction:
5752  * 
5753  * Obtains the current default reading direction. See
5754  * gtk_widget_set_default_direction().
5755  *
5756  * Return value: the current default direction. 
5757  **/
5758 GtkTextDirection
5759 gtk_widget_get_default_direction (void)
5760 {
5761   return gtk_default_direction;
5762 }
5763
5764 static void
5765 gtk_widget_dispose (GObject *object)
5766 {
5767   GtkWidget *widget = GTK_WIDGET (object);
5768
5769   if (widget->parent)
5770     gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
5771
5772   GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
5773   if (GTK_WIDGET_REALIZED (widget))
5774     gtk_widget_unrealize (widget);
5775   
5776   G_OBJECT_CLASS (parent_class)->dispose (object);
5777 }
5778
5779 static void
5780 gtk_widget_real_destroy (GtkObject *object)
5781 {
5782   /* gtk_object_destroy() will already hold a refcount on object */
5783   GtkWidget *widget = GTK_WIDGET (object);
5784
5785   /* wipe accelerator closures (keep order) */
5786   g_object_set_qdata (G_OBJECT (widget), quark_accel_path, NULL);
5787   g_object_set_qdata (G_OBJECT (widget), quark_accel_closures, NULL);
5788
5789   gtk_grab_remove (widget);
5790   
5791   g_object_unref (widget->style);
5792   widget->style = gtk_widget_get_default_style ();
5793   g_object_ref (widget->style);
5794
5795   GTK_OBJECT_CLASS (parent_class)->destroy (object);
5796 }
5797
5798 static void
5799 gtk_widget_finalize (GObject *object)
5800 {
5801   GtkWidget *widget = GTK_WIDGET (object);
5802   GtkWidgetAuxInfo *aux_info;
5803   gint *events;
5804   GdkExtensionMode *mode;
5805   GtkAccessible *accessible;
5806   
5807   gtk_grab_remove (widget);
5808
5809   g_object_unref (widget->style);
5810   widget->style = NULL;
5811
5812   if (widget->name)
5813     g_free (widget->name);
5814   
5815   aux_info =_gtk_widget_get_aux_info (widget, FALSE);
5816   if (aux_info)
5817     gtk_widget_aux_info_destroy (aux_info);
5818   
5819   events = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_event_mask);
5820   if (events)
5821     g_free (events);
5822   
5823   mode = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_extension_event_mode);
5824   if (mode)
5825     g_free (mode);
5826
5827   accessible = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_accessible_object);
5828   if (accessible)
5829     g_object_unref (G_OBJECT (accessible));
5830
5831   G_OBJECT_CLASS (parent_class)->finalize (object);
5832 }
5833
5834 /*****************************************
5835  * gtk_widget_real_map:
5836  *
5837  *   arguments:
5838  *
5839  *   results:
5840  *****************************************/
5841
5842 static void
5843 gtk_widget_real_map (GtkWidget *widget)
5844 {
5845   g_return_if_fail (GTK_WIDGET_REALIZED (widget) == TRUE);
5846   
5847   if (!GTK_WIDGET_MAPPED (widget))
5848     {
5849       GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
5850       
5851       if (!GTK_WIDGET_NO_WINDOW (widget))
5852         gdk_window_show (widget->window);
5853     }
5854 }
5855
5856 /*****************************************
5857  * gtk_widget_real_unmap:
5858  *
5859  *   arguments:
5860  *
5861  *   results:
5862  *****************************************/
5863
5864 static void
5865 gtk_widget_real_unmap (GtkWidget *widget)
5866 {
5867   if (GTK_WIDGET_MAPPED (widget))
5868     {
5869       GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
5870
5871       if (!GTK_WIDGET_NO_WINDOW (widget))
5872         gdk_window_hide (widget->window);
5873     }
5874 }
5875
5876 /*****************************************
5877  * gtk_widget_real_realize:
5878  *
5879  *   arguments:
5880  *
5881  *   results:
5882  *****************************************/
5883
5884 static void
5885 gtk_widget_real_realize (GtkWidget *widget)
5886 {
5887   g_return_if_fail (GTK_WIDGET_NO_WINDOW (widget));
5888   
5889   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
5890   if (widget->parent)
5891     {
5892       widget->window = gtk_widget_get_parent_window (widget);
5893       gdk_window_ref (widget->window);
5894     }
5895   widget->style = gtk_style_attach (widget->style, widget->window);
5896 }
5897
5898 /*****************************************
5899  * gtk_widget_real_unrealize:
5900  *
5901  *   arguments:
5902  *
5903  *   results:
5904  *****************************************/
5905
5906 static void
5907 gtk_widget_real_unrealize (GtkWidget *widget)
5908 {
5909   if (GTK_WIDGET_MAPPED (widget))
5910     gtk_widget_real_unmap (widget);
5911
5912   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
5913
5914   /* printf ("unrealizing %s\n", gtk_type_name (GTK_OBJECT (widget)->klass->type));
5915    */
5916
5917    /* We must do unrealize child widget BEFORE container widget.
5918     * gdk_window_destroy() destroys specified xwindow and its sub-xwindows.
5919     * So, unrealizing container widget bofore its children causes the problem 
5920     * (for example, gdk_ic_destroy () with destroyed window causes crash. )
5921     */
5922
5923   if (GTK_IS_CONTAINER (widget))
5924     gtk_container_forall (GTK_CONTAINER (widget),
5925                           (GtkCallback) gtk_widget_unrealize,
5926                           NULL);
5927
5928   gtk_style_detach (widget->style);
5929   if (!GTK_WIDGET_NO_WINDOW (widget))
5930     {
5931       gdk_window_set_user_data (widget->window, NULL);
5932       gdk_window_destroy (widget->window);
5933       widget->window = NULL;
5934     }
5935   else
5936     {
5937       gdk_window_unref (widget->window);
5938       widget->window = NULL;
5939     }
5940
5941   gtk_selection_remove_all (widget);
5942   
5943   GTK_WIDGET_UNSET_FLAGS (widget, GTK_REALIZED);
5944 }
5945
5946 static void
5947 gtk_widget_real_size_request (GtkWidget         *widget,
5948                               GtkRequisition    *requisition)
5949 {
5950   requisition->width = widget->requisition.width;
5951   requisition->height = widget->requisition.height;
5952 }
5953
5954 /**
5955  * _gtk_widget_peek_colormap:
5956  * 
5957  * Returns colormap currently pushed by gtk_widget_push_colormap, if any.
5958  * 
5959  * Return value: the currently pushed colormap, or %NULL if there is none.
5960  **/
5961 GdkColormap*
5962 _gtk_widget_peek_colormap (void)
5963 {
5964   if (colormap_stack)
5965     return (GdkColormap*) colormap_stack->data;
5966   return NULL;
5967 }
5968
5969 static void
5970 gtk_widget_propagate_state (GtkWidget           *widget,
5971                             GtkStateData        *data)
5972 {
5973   guint8 old_state;
5974
5975   /* don't call this function with state==GTK_STATE_INSENSITIVE,
5976    * parent_sensitive==TRUE on a sensitive widget
5977    */
5978
5979   old_state = GTK_WIDGET_STATE (widget);
5980
5981   if (data->parent_sensitive)
5982     {
5983       GTK_WIDGET_SET_FLAGS (widget, GTK_PARENT_SENSITIVE);
5984
5985       if (GTK_WIDGET_IS_SENSITIVE (widget))
5986         {
5987           if (data->state_restoration)
5988             GTK_WIDGET_STATE (widget) = GTK_WIDGET_SAVED_STATE (widget);
5989           else
5990             GTK_WIDGET_STATE (widget) = data->state;
5991         }
5992       else
5993         {
5994           GTK_WIDGET_STATE (widget) = GTK_STATE_INSENSITIVE;
5995           if (!data->state_restoration &&
5996               data->state != GTK_STATE_INSENSITIVE)
5997             GTK_WIDGET_SAVED_STATE (widget) = data->state;
5998         }
5999     }
6000   else
6001     {
6002       GTK_WIDGET_UNSET_FLAGS (widget, GTK_PARENT_SENSITIVE);
6003       if (!data->state_restoration)
6004         {
6005           if (data->state != GTK_STATE_INSENSITIVE)
6006             GTK_WIDGET_SAVED_STATE (widget) = data->state;
6007         }
6008       else if (GTK_WIDGET_STATE (widget) != GTK_STATE_INSENSITIVE)
6009         GTK_WIDGET_SAVED_STATE (widget) = GTK_WIDGET_STATE (widget);
6010       GTK_WIDGET_STATE (widget) = GTK_STATE_INSENSITIVE;
6011     }
6012
6013   if (gtk_widget_is_focus (widget) && !GTK_WIDGET_IS_SENSITIVE (widget))
6014     {
6015       GtkWidget *window;
6016
6017       window = gtk_widget_get_toplevel (widget);
6018       if (window && GTK_WIDGET_TOPLEVEL (window))
6019         gtk_window_set_focus (GTK_WINDOW (window), NULL);
6020     }
6021
6022   if (old_state != GTK_WIDGET_STATE (widget))
6023     {
6024       gtk_widget_ref (widget);
6025       
6026       if (!GTK_WIDGET_IS_SENSITIVE (widget) && GTK_WIDGET_HAS_GRAB (widget))
6027         gtk_grab_remove (widget);
6028       
6029       gtk_signal_emit (GTK_OBJECT (widget), widget_signals[STATE_CHANGED], old_state);
6030       
6031       
6032       if (GTK_IS_CONTAINER (widget))
6033         {
6034           data->parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (widget) != FALSE);
6035           data->state = GTK_WIDGET_STATE (widget);
6036           if (data->use_forall)
6037             gtk_container_forall (GTK_CONTAINER (widget),
6038                                   (GtkCallback) gtk_widget_propagate_state,
6039                                   data);
6040           else
6041             gtk_container_foreach (GTK_CONTAINER (widget),
6042                                    (GtkCallback) gtk_widget_propagate_state,
6043                                    data);
6044         }
6045       gtk_widget_unref (widget);
6046     }
6047 }
6048
6049 /**
6050  * _gtk_widget_get_aux_info:
6051  * @widget: a #GtkWidget
6052  * @create: if %TRUE, create the structure if it doesn't exist
6053  * 
6054  * Get the #GtkWidgetAuxInfo structure for the widget.
6055  * 
6056  * Return value: the #GtkAuxInfo structure for the widget, or
6057  *    %NULL if @create is %FALSE and one doesn't already exist.
6058  **/
6059 GtkWidgetAuxInfo*
6060 _gtk_widget_get_aux_info (GtkWidget *widget,
6061                           gboolean   create)
6062 {
6063   GtkWidgetAuxInfo *aux_info;
6064   
6065   aux_info = gtk_object_get_data_by_id (GTK_OBJECT (widget), quark_aux_info);
6066   if (!aux_info && create)
6067     {
6068       if (!aux_info_mem_chunk)
6069         aux_info_mem_chunk = g_mem_chunk_new ("widget aux info mem chunk",
6070                                               sizeof (GtkWidgetAuxInfo),
6071                                               1024, G_ALLOC_AND_FREE);
6072       aux_info = g_chunk_new (GtkWidgetAuxInfo, aux_info_mem_chunk);
6073
6074       aux_info->width = -1;
6075       aux_info->height = -1;
6076       aux_info->x = 0;
6077       aux_info->y = 0;
6078       aux_info->x_set = FALSE;
6079       aux_info->y_set = FALSE;
6080       gtk_object_set_data_by_id (GTK_OBJECT (widget), quark_aux_info, aux_info);
6081     }
6082   
6083   return aux_info;
6084 }
6085
6086 /*****************************************
6087  * gtk_widget_aux_info_destroy:
6088  *
6089  *   arguments:
6090  *
6091  *   results:
6092  *****************************************/
6093
6094 static void
6095 gtk_widget_aux_info_destroy (GtkWidgetAuxInfo *aux_info)
6096 {
6097   g_mem_chunk_free (aux_info_mem_chunk, aux_info);
6098 }
6099
6100 static void
6101 gtk_widget_shape_info_destroy (GtkWidgetShapeInfo *info)
6102 {
6103   gdk_drawable_unref (info->shape_mask);
6104   g_free (info);
6105 }
6106
6107 /**
6108  * gtk_widget_shape_combine_mask: 
6109  * @widget: a #GtkWidget.
6110  * @shape_mask: shape to be added, or %NULL to remove an existing shape. 
6111  * @offset_x: X position of shape mask with respect to @window.
6112  * @offset_y: Y position of shape mask with respect to @window.
6113  * 
6114  * Sets a shape for this widget's GDK window. This allows for
6115  * transparent windows etc., see gdk_window_shape_combine_mask()
6116  * for more information.
6117  **/
6118 void
6119 gtk_widget_shape_combine_mask (GtkWidget *widget,
6120                                GdkBitmap *shape_mask,
6121                                gint       offset_x,
6122                                gint       offset_y)
6123 {
6124   GtkWidgetShapeInfo* shape_info;
6125   
6126   g_return_if_fail (GTK_IS_WIDGET (widget));
6127   /*  set_shape doesn't work on widgets without gdk window */
6128   g_return_if_fail (!GTK_WIDGET_NO_WINDOW (widget));
6129
6130   if (!shape_mask)
6131     {
6132       GTK_PRIVATE_UNSET_FLAG (widget, GTK_HAS_SHAPE_MASK);
6133       
6134       if (widget->window)
6135         gdk_window_shape_combine_mask (widget->window, NULL, 0, 0);
6136       
6137       g_object_set_qdata (G_OBJECT (widget), quark_shape_info, NULL);
6138     }
6139   else
6140     {
6141       GTK_PRIVATE_SET_FLAG (widget, GTK_HAS_SHAPE_MASK);
6142       
6143       shape_info = g_new (GtkWidgetShapeInfo, 1);
6144       g_object_set_qdata_full (G_OBJECT (widget), quark_shape_info, shape_info,
6145                                (GDestroyNotify) gtk_widget_shape_info_destroy);
6146       
6147       shape_info->shape_mask = gdk_drawable_ref (shape_mask);
6148       shape_info->offset_x = offset_x;
6149       shape_info->offset_y = offset_y;
6150       
6151       /* set shape if widget has a gdk window allready.
6152        * otherwise the shape is scheduled to be set by gtk_widget_realize.
6153        */
6154       if (widget->window)
6155         gdk_window_shape_combine_mask (widget->window, shape_mask,
6156                                        offset_x, offset_y);
6157     }
6158 }
6159
6160 static void
6161 gtk_reset_shapes_recurse (GtkWidget *widget,
6162                           GdkWindow *window)
6163 {
6164   gpointer data;
6165   GList *list;
6166
6167   gdk_window_get_user_data (window, &data);
6168   if (data != widget)
6169     return;
6170
6171   gdk_window_shape_combine_mask (window, NULL, 0, 0);
6172   for (list = gdk_window_peek_children (window); list; list = list->next)
6173     gtk_reset_shapes_recurse (widget, list->data);
6174 }
6175
6176 /**
6177  * gtk_widget_reset_shapes:
6178  * @widget: a #GtkWidget.
6179  *
6180  * Recursively resets the shape on this widget and its descendants.
6181  **/
6182 void
6183 gtk_widget_reset_shapes (GtkWidget *widget)
6184 {
6185   g_return_if_fail (GTK_IS_WIDGET (widget));
6186   g_return_if_fail (GTK_WIDGET_REALIZED (widget));
6187
6188   if (!GTK_WIDGET_HAS_SHAPE_MASK (widget))
6189     gtk_reset_shapes_recurse (widget, widget->window);
6190 }
6191
6192 /**
6193  * gtk_widget_ref:
6194  * @widget: a #GtkWidget
6195  * 
6196  * Adds a reference to a widget. This function is exactly the same
6197  * as calling g_object_ref(), and exists mostly for historical
6198  * reasons. It can still be convenient to avoid casting a widget
6199  * to a #GObject, it saves a small amount of typing.
6200  * 
6201  * Return value: the widget that was referenced
6202  **/
6203 GtkWidget*
6204 gtk_widget_ref (GtkWidget *widget)
6205 {
6206   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6207
6208   return (GtkWidget*) g_object_ref ((GObject*) widget);
6209 }
6210
6211 /**
6212  * gtk_widget_unref:
6213  * @widget: a #GtkWidget
6214  *
6215  * Inverse of gtk_widget_ref(). Equivalent to g_object_unref().
6216  * 
6217  **/
6218 void
6219 gtk_widget_unref (GtkWidget *widget)
6220 {
6221   g_return_if_fail (GTK_IS_WIDGET (widget));
6222
6223   g_object_unref ((GObject*) widget);
6224 }
6225
6226
6227 /* style properties
6228  */
6229
6230 void
6231 gtk_widget_class_install_style_property_parser (GtkWidgetClass     *class,
6232                                                 GParamSpec         *pspec,
6233                                                 GtkRcPropertyParser parser)
6234 {
6235   g_return_if_fail (GTK_IS_WIDGET_CLASS (class));
6236   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
6237   g_return_if_fail (pspec->flags & G_PARAM_READABLE);
6238   g_return_if_fail (!(pspec->flags & (G_PARAM_CONSTRUCT_ONLY | G_PARAM_CONSTRUCT)));
6239   
6240   if (g_param_spec_pool_lookup (style_property_spec_pool, pspec->name, G_OBJECT_CLASS_TYPE (class), FALSE))
6241     {
6242       g_warning (G_STRLOC ": class `%s' already contains a style property named `%s'",
6243                  G_OBJECT_CLASS_NAME (class),
6244                  pspec->name);
6245       return;
6246     }
6247
6248   g_param_spec_ref (pspec);
6249   g_param_spec_sink (pspec);
6250   g_param_spec_set_qdata (pspec, quark_property_parser, (gpointer) parser);
6251   g_param_spec_pool_insert (style_property_spec_pool, pspec, G_OBJECT_CLASS_TYPE (class));
6252 }
6253
6254 void
6255 gtk_widget_class_install_style_property (GtkWidgetClass *class,
6256                                          GParamSpec     *pspec)
6257 {
6258   GtkRcPropertyParser parser;
6259
6260   g_return_if_fail (GTK_IS_WIDGET_CLASS (class));
6261   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
6262
6263   parser = _gtk_rc_property_parser_from_type (G_PARAM_SPEC_VALUE_TYPE (pspec));
6264
6265   gtk_widget_class_install_style_property_parser (class, pspec, parser);
6266 }
6267
6268 /**
6269  * gtk_widget_class_find_style_property:
6270  * @klass: a #GtkWidgetClass
6271  * @property_name: the name of the style property to find
6272  * @returns: the #GParamSpec of the style property or %NULL if @class has ho
6273  *   style property with that name.
6274  *
6275  * Finds a style property of a widget class by name.
6276  */
6277 GParamSpec*
6278 gtk_widget_class_find_style_property (GtkWidgetClass *klass,
6279                                       const gchar    *property_name)
6280 {
6281   g_return_val_if_fail (property_name != NULL, NULL);
6282
6283   return g_param_spec_pool_lookup (style_property_spec_pool,
6284                                    property_name,
6285                                    G_OBJECT_CLASS_TYPE (klass),
6286                                    TRUE);
6287 }
6288
6289 /**
6290  * gtk_widget_class_list_style_properties:
6291  * @klass: a #GtkWidgetClass
6292  * @n_properties: location to return the number of style properties found
6293  * @returns: an newly allocated array of #GParamSpec*. The array must be freed with g_free().
6294  *
6295  * Returns all style properties of a widget class.
6296  */
6297 GParamSpec**
6298 gtk_widget_class_list_style_properties (GtkWidgetClass *klass,
6299                                         guint          *n_properties)
6300 {
6301   GParamSpec **pspecs;
6302   guint n;
6303
6304   pspecs = g_param_spec_pool_list (style_property_spec_pool,
6305                                    G_OBJECT_CLASS_TYPE (klass),
6306                                    &n);
6307   if (n_properties)
6308     *n_properties = n;
6309
6310   return pspecs;
6311 }
6312
6313 /**
6314  * gtk_widget_style_get_property:
6315  * @widget: a #GtkWidget
6316  * @property_name: the name of a style property
6317  * @value: location to return the property value 
6318  *
6319  * Gets the value of a style property of @widget.
6320  */
6321 void
6322 gtk_widget_style_get_property (GtkWidget   *widget,
6323                                const gchar *property_name,
6324                                GValue      *value)
6325 {
6326   GParamSpec *pspec;
6327
6328   g_return_if_fail (GTK_IS_WIDGET (widget));
6329   g_return_if_fail (property_name != NULL);
6330   g_return_if_fail (G_IS_VALUE (value));
6331
6332   g_object_ref (widget);
6333   pspec = g_param_spec_pool_lookup (style_property_spec_pool,
6334                                     property_name,
6335                                     G_OBJECT_TYPE (widget),
6336                                     TRUE);
6337   if (!pspec)
6338     g_warning ("%s: widget class `%s' has no property named `%s'",
6339                G_STRLOC,
6340                G_OBJECT_TYPE_NAME (widget),
6341                property_name);
6342   else
6343     {
6344       const GValue *peek_value;
6345
6346       peek_value = _gtk_style_peek_property_value (widget->style,
6347                                                    G_OBJECT_TYPE (widget),
6348                                                    pspec,
6349                                                    (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser));
6350       
6351       /* auto-conversion of the caller's value type
6352        */
6353       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
6354         g_value_copy (peek_value, value);
6355       else if (g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
6356         g_value_transform (peek_value, value);
6357       else
6358         g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
6359                    pspec->name,
6360                    g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
6361                    G_VALUE_TYPE_NAME (value));
6362     }
6363   g_object_unref (widget);
6364 }
6365
6366 /**
6367  * gtk_widget_style_get_valist:
6368  * @widget: a #GtkWidget
6369  * @first_property_name: the name of the first property to get
6370  * @var_args: a <type>va_list</type> of pairs of property names and
6371  *     locations to return the property values, starting with the location
6372  *     for @first_property_name.
6373  * 
6374  * Non-vararg variant of gtk_widget_style_get(). Used primarily by language 
6375  * bindings.
6376  */ 
6377 void
6378 gtk_widget_style_get_valist (GtkWidget   *widget,
6379                              const gchar *first_property_name,
6380                              va_list      var_args)
6381 {
6382   const gchar *name;
6383
6384   g_return_if_fail (GTK_IS_WIDGET (widget));
6385
6386   g_object_ref (widget);
6387
6388   name = first_property_name;
6389   while (name)
6390     {
6391       const GValue *peek_value;
6392       GParamSpec *pspec;
6393       gchar *error;
6394
6395       pspec = g_param_spec_pool_lookup (style_property_spec_pool,
6396                                         name,
6397                                         G_OBJECT_TYPE (widget),
6398                                         TRUE);
6399       if (!pspec)
6400         {
6401           g_warning ("%s: widget class `%s' has no property named `%s'",
6402                      G_STRLOC,
6403                      G_OBJECT_TYPE_NAME (widget),
6404                      name);
6405           break;
6406         }
6407       /* style pspecs are always readable so we can spare that check here */
6408
6409       peek_value = _gtk_style_peek_property_value (widget->style,
6410                                                    G_OBJECT_TYPE (widget),
6411                                                    pspec,
6412                                                    (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser));
6413       G_VALUE_LCOPY (peek_value, var_args, 0, &error);
6414       if (error)
6415         {
6416           g_warning ("%s: %s", G_STRLOC, error);
6417           g_free (error);
6418           break;
6419         }
6420
6421       name = va_arg (var_args, gchar*);
6422     }
6423
6424   g_object_unref (widget);
6425 }
6426
6427 /**
6428  * gtk_widget_style_get:
6429  * @widget: a #GtkWidget
6430  * @first_property_name: the name of the first property to get
6431  * @Varargs: pairs of property names and locations to 
6432  *   return the property values, starting with the location for 
6433  *   @first_property_name, terminated by %NULL.
6434  *
6435  * Gets the values of a multiple style properties of @widget.
6436  */
6437 void
6438 gtk_widget_style_get (GtkWidget   *widget,
6439                       const gchar *first_property_name,
6440                       ...)
6441 {
6442   va_list var_args;
6443
6444   g_return_if_fail (GTK_IS_WIDGET (widget));
6445
6446   va_start (var_args, first_property_name);
6447   gtk_widget_style_get_valist (widget, first_property_name, var_args);
6448   va_end (var_args);
6449 }
6450
6451 /**
6452  * gtk_widget_path:
6453  * @widget: a #GtkWidget
6454  * @path_length: location to store length of the path, or %NULL
6455  * @path: location to store allocated path string, or %NULL 
6456  * @path_reversed: location to store allocated reverse path string, or %NULL
6457  *
6458  * Obtains the full path to @widget. The path is simply the name of a
6459  * widget and all its parents in the container hierarchy, separated by
6460  * periods. The name of a widget comes from
6461  * gtk_widget_get_name(). Paths are used to apply styles to a widget
6462  * in gtkrc configuration files.  Widget names are the type of the
6463  * widget by default (e.g. "GtkButton") or can be set to an
6464  * application-specific value with gtk_widget_set_name().  By setting
6465  * the name of a widget, you allow users or theme authors to apply
6466  * styles to that specific widget in their gtkrc
6467  * file. @path_reversed_p fills in the path in reverse order,
6468  * i.e. starting with @widget's name instead of starting with the name
6469  * of @widget's outermost ancestor.
6470  * 
6471  **/
6472 void
6473 gtk_widget_path (GtkWidget *widget,
6474                  guint     *path_length,
6475                  gchar    **path,
6476                  gchar    **path_reversed)
6477 {
6478   static gchar *rev_path = NULL;
6479   static guint tmp_path_len = 0;
6480   guint len;
6481   
6482   g_return_if_fail (GTK_IS_WIDGET (widget));
6483   
6484   len = 0;
6485   do
6486     {
6487       const gchar *string;
6488       const gchar *s;
6489       gchar *d;
6490       guint l;
6491       
6492       string = gtk_widget_get_name (widget);
6493       l = strlen (string);
6494       while (tmp_path_len <= len + l + 1)
6495         {
6496           tmp_path_len += INIT_PATH_SIZE;
6497           rev_path = g_realloc (rev_path, tmp_path_len);
6498         }
6499       s = string + l - 1;
6500       d = rev_path + len;
6501       while (s >= string)
6502         *(d++) = *(s--);
6503       len += l;
6504       
6505       widget = widget->parent;
6506       
6507       if (widget)
6508         rev_path[len++] = '.';
6509       else
6510         rev_path[len++] = 0;
6511     }
6512   while (widget);
6513   
6514   if (path_length)
6515     *path_length = len - 1;
6516   if (path_reversed)
6517     *path_reversed = g_strdup (rev_path);
6518   if (path)
6519     {
6520       *path = g_strdup (rev_path);
6521       g_strreverse (*path);
6522     }
6523 }
6524
6525 /**
6526  * gtk_widget_class_path:
6527  * @widget: a #GtkWidget
6528  * @path_length: location to store the length of the class path, or %NULL
6529  * @path: location to store the class path as an allocated string, or %NULL
6530  * @path_reversed: location to store the reverse class path as an allocated string, or %NULL
6531  *
6532  * Same as gtk_widget_path(), but always uses the name of a widget's type,
6533  * never uses a custom name set with gtk_widget_set_name().
6534  * 
6535  **/
6536 void
6537 gtk_widget_class_path (GtkWidget *widget,
6538                        guint     *path_length,
6539                        gchar    **path,
6540                        gchar    **path_reversed)
6541 {
6542   static gchar *rev_path = NULL;
6543   static guint tmp_path_len = 0;
6544   guint len;
6545   
6546   g_return_if_fail (GTK_IS_WIDGET (widget));
6547   
6548   len = 0;
6549   do
6550     {
6551       const gchar *string;
6552       const gchar *s;
6553       gchar *d;
6554       guint l;
6555       
6556       string = gtk_type_name (GTK_WIDGET_TYPE (widget));
6557       l = strlen (string);
6558       while (tmp_path_len <= len + l + 1)
6559         {
6560           tmp_path_len += INIT_PATH_SIZE;
6561           rev_path = g_realloc (rev_path, tmp_path_len);
6562         }
6563       s = string + l - 1;
6564       d = rev_path + len;
6565       while (s >= string)
6566         *(d++) = *(s--);
6567       len += l;
6568       
6569       widget = widget->parent;
6570       
6571       if (widget)
6572         rev_path[len++] = '.';
6573       else
6574         rev_path[len++] = 0;
6575     }
6576   while (widget);
6577   
6578   if (path_length)
6579     *path_length = len - 1;
6580   if (path_reversed)
6581     *path_reversed = g_strdup (rev_path);
6582   if (path)
6583     {
6584       *path = g_strdup (rev_path);
6585       g_strreverse (*path);
6586     }
6587 }
6588
6589 /**
6590  * gtk_requisition_copy:
6591  * @requisition: a #GtkRequisition.
6592  * @returns: a copy of @requisition.
6593  *
6594  * Copies a #GtkRequisition.
6595  **/
6596 GtkRequisition *
6597 gtk_requisition_copy (const GtkRequisition *requisition)
6598 {
6599   return (GtkRequisition *)g_memdup (requisition, sizeof (GtkRequisition));
6600 }
6601
6602 /**
6603  * gtk_requisition_free:
6604  * @requisition: a #GtkRequisition.
6605  * 
6606  * Frees a #GtkRequisition.
6607  **/
6608 void
6609 gtk_requisition_free (GtkRequisition *requisition)
6610 {
6611   g_free (requisition);
6612 }
6613
6614 GType
6615 gtk_requisition_get_type (void)
6616 {
6617   static GType our_type = 0;
6618   
6619   if (our_type == 0)
6620     our_type = g_boxed_type_register_static ("GtkRequisition",
6621                                              (GBoxedCopyFunc) gtk_requisition_copy,
6622                                              (GBoxedFreeFunc) gtk_requisition_free);
6623
6624   return our_type;
6625 }
6626
6627 AtkObject* 
6628 gtk_widget_get_accessible (GtkWidget *widget)
6629 {
6630   GtkWidgetClass *klass;
6631
6632   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6633
6634   klass = GTK_WIDGET_GET_CLASS (widget);
6635
6636   g_return_val_if_fail (klass->get_accessible != NULL, NULL);
6637
6638   return klass->get_accessible (widget);
6639 }
6640
6641 static AtkObject* 
6642 gtk_widget_real_get_accessible (GtkWidget *widget)
6643 {
6644   AtkObject* accessible;
6645
6646   accessible = g_object_get_qdata (G_OBJECT (widget), 
6647                                    quark_accessible_object);
6648   if (!accessible)
6649   {
6650     AtkObjectFactory *factory;
6651     AtkRegistry *default_registry;
6652
6653     default_registry = atk_get_default_registry ();
6654     factory = atk_registry_get_factory (default_registry, 
6655                                         GTK_OBJECT_TYPE (widget));
6656     accessible =
6657       atk_object_factory_create_accessible (factory,
6658                                             G_OBJECT (widget));
6659     g_object_set_qdata (G_OBJECT (widget), 
6660                         quark_accessible_object,
6661                         accessible);
6662   }
6663   return accessible;
6664 }
6665
6666 /*
6667  * Initialize a AtkImplementorIface instance's virtual pointers as
6668  * appropriate to this implementor's class (GtkWidget).
6669  */
6670 static void
6671 gtk_widget_accessible_interface_init (AtkImplementorIface *iface)
6672 {
6673   iface->ref_accessible = gtk_widget_ref_accessible;
6674 }
6675
6676 static AtkObject*
6677 gtk_widget_ref_accessible (AtkImplementor *implementor)
6678 {
6679   AtkObject *accessible;
6680
6681   accessible = gtk_widget_get_accessible (GTK_WIDGET (implementor));
6682   if (accessible)
6683     g_object_ref (G_OBJECT (accessible));
6684   return accessible;
6685 }
6686
6687 /**
6688  * gtk_widget_get_clipboard:
6689  * @widget: a #GtkWidget
6690  * @selection: a #GdkAtom which identifies the clipboard
6691  *             to use. %GDK_SELECTION_CLIPBOARD gives the
6692  *             default clipboard. Another common value
6693  *             is %GDK_SELECTION_PRIMARY, which gives
6694  *             the primary X selection. 
6695  * 
6696  * Returns the clipboard object for the given selection to
6697  * be used with @widget. @widget must have a #GdkDisplay
6698  * associated with it, so must be attached to a toplevel
6699  * window.
6700  * 
6701  * Return value: the appropriate clipboard object. If no
6702  *             clipboard already exists, a new one will
6703  *             be created. Once a clipboard object has
6704  *             been created, it is persistent for all time.
6705  **/
6706 GtkClipboard *
6707 gtk_widget_get_clipboard (GtkWidget *widget, GdkAtom selection)
6708 {
6709   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6710   g_return_val_if_fail (gtk_widget_has_screen (widget), NULL);
6711   
6712   return gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
6713                                         selection);
6714 }