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