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