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