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