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