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