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