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