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