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