]> Pileus Git - ~andy/gtk/blob - gtk/gtkwidget.c
Accelerators failed for submenus (GNOME bug 582025)
[~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 "gtkaccessible.h"
52 #include "gtktooltip.h"
53 #include "gtkinvisible.h"
54 #include "gtkbuildable.h"
55 #include "gtkbuilderprivate.h"
56 #include "gtkalias.h"
57
58 #define WIDGET_CLASS(w)  GTK_WIDGET_GET_CLASS (w)
59 #define INIT_PATH_SIZE  (512)
60
61
62 enum {
63   SHOW,
64   HIDE,
65   MAP,
66   UNMAP,
67   REALIZE,
68   UNREALIZE,
69   SIZE_REQUEST,
70   SIZE_ALLOCATE,
71   STATE_CHANGED,
72   PARENT_SET,
73   HIERARCHY_CHANGED,
74   STYLE_SET,
75   DIRECTION_CHANGED,
76   GRAB_NOTIFY,
77   CHILD_NOTIFY,
78   MNEMONIC_ACTIVATE,
79   GRAB_FOCUS,
80   FOCUS,
81   MOVE_FOCUS,
82   EVENT,
83   EVENT_AFTER,
84   BUTTON_PRESS_EVENT,
85   BUTTON_RELEASE_EVENT,
86   SCROLL_EVENT,
87   MOTION_NOTIFY_EVENT,
88   DELETE_EVENT,
89   DESTROY_EVENT,
90   EXPOSE_EVENT,
91   KEY_PRESS_EVENT,
92   KEY_RELEASE_EVENT,
93   ENTER_NOTIFY_EVENT,
94   LEAVE_NOTIFY_EVENT,
95   CONFIGURE_EVENT,
96   FOCUS_IN_EVENT,
97   FOCUS_OUT_EVENT,
98   MAP_EVENT,
99   UNMAP_EVENT,
100   PROPERTY_NOTIFY_EVENT,
101   SELECTION_CLEAR_EVENT,
102   SELECTION_REQUEST_EVENT,
103   SELECTION_NOTIFY_EVENT,
104   SELECTION_GET,
105   SELECTION_RECEIVED,
106   PROXIMITY_IN_EVENT,
107   PROXIMITY_OUT_EVENT,
108   DRAG_BEGIN,
109   DRAG_END,
110   DRAG_DATA_DELETE,
111   DRAG_LEAVE,
112   DRAG_MOTION,
113   DRAG_DROP,
114   DRAG_DATA_GET,
115   DRAG_DATA_RECEIVED,
116   CLIENT_EVENT,
117   NO_EXPOSE_EVENT,
118   VISIBILITY_NOTIFY_EVENT,
119   WINDOW_STATE_EVENT,
120   POPUP_MENU,
121   SHOW_HELP,
122   ACCEL_CLOSURES_CHANGED,
123   SCREEN_CHANGED,
124   CAN_ACTIVATE_ACCEL,
125   GRAB_BROKEN,
126   COMPOSITED_CHANGED,
127   QUERY_TOOLTIP,
128   KEYNAV_FAILED,
129   DRAG_FAILED,
130   DAMAGE_EVENT,
131   LAST_SIGNAL
132 };
133
134 enum {
135   PROP_0,
136   PROP_NAME,
137   PROP_PARENT,
138   PROP_WIDTH_REQUEST,
139   PROP_HEIGHT_REQUEST,
140   PROP_VISIBLE,
141   PROP_SENSITIVE,
142   PROP_APP_PAINTABLE,
143   PROP_CAN_FOCUS,
144   PROP_HAS_FOCUS,
145   PROP_IS_FOCUS,
146   PROP_CAN_DEFAULT,
147   PROP_HAS_DEFAULT,
148   PROP_RECEIVES_DEFAULT,
149   PROP_COMPOSITE_CHILD,
150   PROP_STYLE,
151   PROP_EVENTS,
152   PROP_EXTENSION_EVENTS,
153   PROP_NO_SHOW_ALL,
154   PROP_HAS_TOOLTIP,
155   PROP_TOOLTIP_MARKUP,
156   PROP_TOOLTIP_TEXT,
157   PROP_WINDOW
158 };
159
160 typedef struct  _GtkStateData    GtkStateData;
161
162 struct _GtkStateData
163 {
164   GtkStateType  state;
165   guint         state_restoration : 1;
166   guint         parent_sensitive : 1;
167   guint         use_forall : 1;
168 };
169
170 /* --- prototypes --- */
171 static void     gtk_widget_class_init           (GtkWidgetClass     *klass);
172 static void     gtk_widget_base_class_finalize  (GtkWidgetClass     *klass);
173 static void     gtk_widget_init                 (GtkWidget          *widget);
174 static void     gtk_widget_set_property          (GObject           *object,
175                                                   guint              prop_id,
176                                                   const GValue      *value,
177                                                   GParamSpec        *pspec);
178 static void     gtk_widget_get_property          (GObject           *object,
179                                                   guint              prop_id,
180                                                   GValue            *value,
181                                                   GParamSpec        *pspec);
182 static void     gtk_widget_dispose               (GObject           *object);
183 static void     gtk_widget_real_destroy          (GtkObject         *object);
184 static void     gtk_widget_finalize              (GObject           *object);
185 static void     gtk_widget_real_show             (GtkWidget         *widget);
186 static void     gtk_widget_real_hide             (GtkWidget         *widget);
187 static void     gtk_widget_real_map              (GtkWidget         *widget);
188 static void     gtk_widget_real_unmap            (GtkWidget         *widget);
189 static void     gtk_widget_real_realize          (GtkWidget         *widget);
190 static void     gtk_widget_real_unrealize        (GtkWidget         *widget);
191 static void     gtk_widget_real_size_request     (GtkWidget         *widget,
192                                                   GtkRequisition    *requisition);
193 static void     gtk_widget_real_size_allocate    (GtkWidget         *widget,
194                                                   GtkAllocation     *allocation);
195 static void     gtk_widget_real_style_set        (GtkWidget         *widget,
196                                                   GtkStyle          *previous_style);
197 static void     gtk_widget_real_direction_changed(GtkWidget         *widget,
198                                                   GtkTextDirection   previous_direction);
199
200 static void     gtk_widget_real_grab_focus       (GtkWidget         *focus_widget);
201 static gboolean gtk_widget_real_query_tooltip    (GtkWidget         *widget,
202                                                   gint               x,
203                                                   gint               y,
204                                                   gboolean           keyboard_tip,
205                                                   GtkTooltip        *tooltip);
206 static gboolean gtk_widget_real_show_help        (GtkWidget         *widget,
207                                                   GtkWidgetHelpType  help_type);
208
209 static void     gtk_widget_dispatch_child_properties_changed    (GtkWidget        *object,
210                                                                  guint             n_pspecs,
211                                                                  GParamSpec      **pspecs);
212 static gboolean         gtk_widget_real_key_press_event         (GtkWidget        *widget,
213                                                                  GdkEventKey      *event);
214 static gboolean         gtk_widget_real_key_release_event       (GtkWidget        *widget,
215                                                                  GdkEventKey      *event);
216 static gboolean         gtk_widget_real_focus_in_event           (GtkWidget       *widget,
217                                                                   GdkEventFocus   *event);
218 static gboolean         gtk_widget_real_focus_out_event         (GtkWidget        *widget,
219                                                                  GdkEventFocus    *event);
220 static gboolean         gtk_widget_real_focus                   (GtkWidget        *widget,
221                                                                  GtkDirectionType  direction);
222 static void             gtk_widget_real_move_focus              (GtkWidget        *widget,
223                                                                  GtkDirectionType  direction);
224 static gboolean         gtk_widget_real_keynav_failed           (GtkWidget        *widget,
225                                                                  GtkDirectionType  direction);
226 static PangoContext*    gtk_widget_peek_pango_context           (GtkWidget        *widget);
227 static void             gtk_widget_update_pango_context         (GtkWidget        *widget);
228 static void             gtk_widget_propagate_state              (GtkWidget        *widget,
229                                                                  GtkStateData     *data);
230 static void             gtk_widget_reset_rc_style               (GtkWidget        *widget);
231 static void             gtk_widget_set_style_internal           (GtkWidget        *widget,
232                                                                  GtkStyle         *style,
233                                                                  gboolean          initial_emission);
234 static gint             gtk_widget_event_internal               (GtkWidget        *widget,
235                                                                  GdkEvent         *event);
236 static gboolean         gtk_widget_real_mnemonic_activate       (GtkWidget        *widget,
237                                                                  gboolean          group_cycling);
238 static void             gtk_widget_aux_info_destroy             (GtkWidgetAuxInfo *aux_info);
239 static AtkObject*       gtk_widget_real_get_accessible          (GtkWidget        *widget);
240 static void             gtk_widget_accessible_interface_init    (AtkImplementorIface *iface);
241 static AtkObject*       gtk_widget_ref_accessible               (AtkImplementor *implementor);
242 static void             gtk_widget_invalidate_widget_windows    (GtkWidget        *widget,
243                                                                  GdkRegion        *region);
244 static GdkScreen *      gtk_widget_get_screen_unchecked         (GtkWidget        *widget);
245 static void             gtk_widget_queue_shallow_draw           (GtkWidget        *widget);
246 static gboolean         gtk_widget_real_can_activate_accel      (GtkWidget *widget,
247                                                                  guint      signal_id);
248
249 static void             gtk_widget_real_set_has_tooltip         (GtkWidget *widget,
250                                                                  gboolean   has_tooltip,
251                                                                  gboolean   force);
252 static void             gtk_widget_buildable_interface_init     (GtkBuildableIface *iface);
253 static void             gtk_widget_buildable_set_name           (GtkBuildable     *buildable,
254                                                                  const gchar      *name);
255 static const gchar *    gtk_widget_buildable_get_name           (GtkBuildable     *buildable);
256 static GObject *        gtk_widget_buildable_get_internal_child (GtkBuildable *buildable,
257                                                                  GtkBuilder   *builder,
258                                                                  const gchar  *childname);
259 static void             gtk_widget_buildable_set_buildable_property (GtkBuildable     *buildable,
260                                                                      GtkBuilder       *builder,
261                                                                      const gchar      *name,
262                                                                      const GValue     *value);
263 static gboolean         gtk_widget_buildable_custom_tag_start   (GtkBuildable     *buildable,
264                                                                  GtkBuilder       *builder,
265                                                                  GObject          *child,
266                                                                  const gchar      *tagname,
267                                                                  GMarkupParser    *parser,
268                                                                  gpointer         *data);
269 static void             gtk_widget_buildable_custom_finished    (GtkBuildable     *buildable,
270                                                                  GtkBuilder       *builder,
271                                                                  GObject          *child,
272                                                                  const gchar      *tagname,
273                                                                  gpointer          data);
274 static void             gtk_widget_buildable_parser_finished    (GtkBuildable     *buildable,
275                                                                  GtkBuilder       *builder);
276
277 static void             gtk_widget_queue_tooltip_query          (GtkWidget *widget);
278      
279 static void gtk_widget_set_usize_internal (GtkWidget *widget,
280                                            gint       width,
281                                            gint       height);
282 static void gtk_widget_get_draw_rectangle (GtkWidget    *widget,
283                                            GdkRectangle *rect);
284
285
286 /* --- variables --- */
287 static gpointer         gtk_widget_parent_class = NULL;
288 static guint            widget_signals[LAST_SIGNAL] = { 0 };
289 static GtkStyle        *gtk_default_style = NULL;
290 static GSList          *colormap_stack = NULL;
291 static guint            composite_child_stack = 0;
292 static GtkTextDirection gtk_default_direction = GTK_TEXT_DIR_LTR;
293 static GParamSpecPool  *style_property_spec_pool = NULL;
294
295 static GQuark           quark_property_parser = 0;
296 static GQuark           quark_aux_info = 0;
297 static GQuark           quark_accel_path = 0;
298 static GQuark           quark_accel_closures = 0;
299 static GQuark           quark_event_mask = 0;
300 static GQuark           quark_extension_event_mode = 0;
301 static GQuark           quark_parent_window = 0;
302 static GQuark           quark_pointer_window = 0;
303 static GQuark           quark_shape_info = 0;
304 static GQuark           quark_input_shape_info = 0;
305 static GQuark           quark_colormap = 0;
306 static GQuark           quark_pango_context = 0;
307 static GQuark           quark_rc_style = 0;
308 static GQuark           quark_accessible_object = 0;
309 static GQuark           quark_mnemonic_labels = 0;
310 static GQuark           quark_tooltip_markup = 0;
311 static GQuark           quark_has_tooltip = 0;
312 static GQuark           quark_tooltip_window = 0;
313 GParamSpecPool         *_gtk_widget_child_property_pool = NULL;
314 GObjectNotifyContext   *_gtk_widget_child_property_notify_context = NULL;
315
316 /* --- functions --- */
317 GType
318 gtk_widget_get_type (void)
319 {
320   static GType widget_type = 0;
321
322   if (G_UNLIKELY (widget_type == 0))
323     {
324       const GTypeInfo widget_info =
325       {
326         sizeof (GtkWidgetClass),
327         NULL,           /* base_init */
328         (GBaseFinalizeFunc) gtk_widget_base_class_finalize,
329         (GClassInitFunc) gtk_widget_class_init,
330         NULL,           /* class_finalize */
331         NULL,           /* class_init */
332         sizeof (GtkWidget),
333         0,              /* n_preallocs */
334         (GInstanceInitFunc) gtk_widget_init,
335         NULL,           /* value_table */
336       };
337
338       const GInterfaceInfo accessibility_info =
339       {
340         (GInterfaceInitFunc) gtk_widget_accessible_interface_init,
341         (GInterfaceFinalizeFunc) NULL,
342         NULL /* interface data */
343       };
344
345       const GInterfaceInfo buildable_info =
346       {
347         (GInterfaceInitFunc) gtk_widget_buildable_interface_init,
348         (GInterfaceFinalizeFunc) NULL,
349         NULL /* interface data */
350       };
351
352       widget_type = g_type_register_static (GTK_TYPE_OBJECT, "GtkWidget",
353                                            &widget_info, G_TYPE_FLAG_ABSTRACT);
354
355       g_type_add_interface_static (widget_type, ATK_TYPE_IMPLEMENTOR,
356                                    &accessibility_info) ;
357       g_type_add_interface_static (widget_type, GTK_TYPE_BUILDABLE,
358                                    &buildable_info) ;
359
360     }
361
362   return widget_type;
363 }
364
365 static void
366 child_property_notify_dispatcher (GObject     *object,
367                                   guint        n_pspecs,
368                                   GParamSpec **pspecs)
369 {
370   GTK_WIDGET_GET_CLASS (object)->dispatch_child_properties_changed (GTK_WIDGET (object), n_pspecs, pspecs);
371 }
372
373 static void
374 gtk_widget_class_init (GtkWidgetClass *klass)
375 {
376   static GObjectNotifyContext cpn_context = { 0, NULL, NULL };
377   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
378   GtkObjectClass *object_class = GTK_OBJECT_CLASS (klass);
379   GtkBindingSet *binding_set;
380
381   gtk_widget_parent_class = g_type_class_peek_parent (klass);
382
383   quark_property_parser = g_quark_from_static_string ("gtk-rc-property-parser");
384   quark_aux_info = g_quark_from_static_string ("gtk-aux-info");
385   quark_accel_path = g_quark_from_static_string ("gtk-accel-path");
386   quark_accel_closures = g_quark_from_static_string ("gtk-accel-closures");
387   quark_event_mask = g_quark_from_static_string ("gtk-event-mask");
388   quark_extension_event_mode = g_quark_from_static_string ("gtk-extension-event-mode");
389   quark_parent_window = g_quark_from_static_string ("gtk-parent-window");
390   quark_pointer_window = g_quark_from_static_string ("gtk-pointer-window");
391   quark_shape_info = g_quark_from_static_string ("gtk-shape-info");
392   quark_input_shape_info = g_quark_from_static_string ("gtk-input-shape-info");
393   quark_colormap = g_quark_from_static_string ("gtk-colormap");
394   quark_pango_context = g_quark_from_static_string ("gtk-pango-context");
395   quark_rc_style = g_quark_from_static_string ("gtk-rc-style");
396   quark_accessible_object = g_quark_from_static_string ("gtk-accessible-object");
397   quark_mnemonic_labels = g_quark_from_static_string ("gtk-mnemonic-labels");
398   quark_tooltip_markup = g_quark_from_static_string ("gtk-tooltip-markup");
399   quark_has_tooltip = g_quark_from_static_string ("gtk-has-tooltip");
400   quark_tooltip_window = g_quark_from_static_string ("gtk-tooltip-window");
401
402   style_property_spec_pool = g_param_spec_pool_new (FALSE);
403   _gtk_widget_child_property_pool = g_param_spec_pool_new (TRUE);
404   cpn_context.quark_notify_queue = g_quark_from_static_string ("GtkWidget-child-property-notify-queue");
405   cpn_context.dispatcher = child_property_notify_dispatcher;
406   _gtk_widget_child_property_notify_context = &cpn_context;
407
408   gobject_class->dispose = gtk_widget_dispose;
409   gobject_class->finalize = gtk_widget_finalize;
410   gobject_class->set_property = gtk_widget_set_property;
411   gobject_class->get_property = gtk_widget_get_property;
412
413   object_class->destroy = gtk_widget_real_destroy;
414   
415   klass->activate_signal = 0;
416   klass->set_scroll_adjustments_signal = 0;
417   klass->dispatch_child_properties_changed = gtk_widget_dispatch_child_properties_changed;
418   klass->show = gtk_widget_real_show;
419   klass->show_all = gtk_widget_show;
420   klass->hide = gtk_widget_real_hide;
421   klass->hide_all = gtk_widget_hide;
422   klass->map = gtk_widget_real_map;
423   klass->unmap = gtk_widget_real_unmap;
424   klass->realize = gtk_widget_real_realize;
425   klass->unrealize = gtk_widget_real_unrealize;
426   klass->size_request = gtk_widget_real_size_request;
427   klass->size_allocate = gtk_widget_real_size_allocate;
428   klass->state_changed = NULL;
429   klass->parent_set = NULL;
430   klass->hierarchy_changed = NULL;
431   klass->style_set = gtk_widget_real_style_set;
432   klass->direction_changed = gtk_widget_real_direction_changed;
433   klass->grab_notify = NULL;
434   klass->child_notify = NULL;
435   klass->mnemonic_activate = gtk_widget_real_mnemonic_activate;
436   klass->grab_focus = gtk_widget_real_grab_focus;
437   klass->focus = gtk_widget_real_focus;
438   klass->event = NULL;
439   klass->button_press_event = NULL;
440   klass->button_release_event = NULL;
441   klass->motion_notify_event = NULL;
442   klass->delete_event = NULL;
443   klass->destroy_event = NULL;
444   klass->expose_event = NULL;
445   klass->key_press_event = gtk_widget_real_key_press_event;
446   klass->key_release_event = gtk_widget_real_key_release_event;
447   klass->enter_notify_event = NULL;
448   klass->leave_notify_event = NULL;
449   klass->configure_event = NULL;
450   klass->focus_in_event = gtk_widget_real_focus_in_event;
451   klass->focus_out_event = gtk_widget_real_focus_out_event;
452   klass->map_event = NULL;
453   klass->unmap_event = NULL;
454   klass->window_state_event = NULL;
455   klass->property_notify_event = _gtk_selection_property_notify;
456   klass->selection_clear_event = gtk_selection_clear;
457   klass->selection_request_event = _gtk_selection_request;
458   klass->selection_notify_event = _gtk_selection_notify;
459   klass->selection_received = NULL;
460   klass->proximity_in_event = NULL;
461   klass->proximity_out_event = NULL;
462   klass->drag_begin = NULL;
463   klass->drag_end = NULL;
464   klass->drag_data_delete = NULL;
465   klass->drag_leave = NULL;
466   klass->drag_motion = NULL;
467   klass->drag_drop = NULL;
468   klass->drag_data_received = NULL;
469   klass->screen_changed = NULL;
470   klass->can_activate_accel = gtk_widget_real_can_activate_accel;
471   klass->grab_broken_event = NULL;
472   klass->query_tooltip = gtk_widget_real_query_tooltip;
473
474   klass->show_help = gtk_widget_real_show_help;
475   
476   /* Accessibility support */
477   klass->get_accessible = gtk_widget_real_get_accessible;
478
479   klass->no_expose_event = NULL;
480
481   g_object_class_install_property (gobject_class,
482                                    PROP_NAME,
483                                    g_param_spec_string ("name",
484                                                         P_("Widget name"),
485                                                         P_("The name of the widget"),
486                                                         NULL,
487                                                         GTK_PARAM_READWRITE));
488   g_object_class_install_property (gobject_class,
489                                    PROP_PARENT,
490                                    g_param_spec_object ("parent",
491                                                         P_("Parent widget"), 
492                                                         P_("The parent widget of this widget. Must be a Container widget"),
493                                                         GTK_TYPE_CONTAINER,
494                                                         GTK_PARAM_READWRITE));
495
496   g_object_class_install_property (gobject_class,
497                                    PROP_WIDTH_REQUEST,
498                                    g_param_spec_int ("width-request",
499                                                      P_("Width request"),
500                                                      P_("Override for width request of the widget, or -1 if natural request should be used"),
501                                                      -1,
502                                                      G_MAXINT,
503                                                      -1,
504                                                      GTK_PARAM_READWRITE));
505   g_object_class_install_property (gobject_class,
506                                    PROP_HEIGHT_REQUEST,
507                                    g_param_spec_int ("height-request",
508                                                      P_("Height request"),
509                                                      P_("Override for height request of the widget, or -1 if natural request should be used"),
510                                                      -1,
511                                                      G_MAXINT,
512                                                      -1,
513                                                      GTK_PARAM_READWRITE));
514   g_object_class_install_property (gobject_class,
515                                    PROP_VISIBLE,
516                                    g_param_spec_boolean ("visible",
517                                                          P_("Visible"),
518                                                          P_("Whether the widget is visible"),
519                                                          FALSE,
520                                                          GTK_PARAM_READWRITE));
521   g_object_class_install_property (gobject_class,
522                                    PROP_SENSITIVE,
523                                    g_param_spec_boolean ("sensitive",
524                                                          P_("Sensitive"),
525                                                          P_("Whether the widget responds to input"),
526                                                          TRUE,
527                                                          GTK_PARAM_READWRITE));
528   g_object_class_install_property (gobject_class,
529                                    PROP_APP_PAINTABLE,
530                                    g_param_spec_boolean ("app-paintable",
531                                                          P_("Application paintable"),
532                                                          P_("Whether the application will paint directly on the widget"),
533                                                          FALSE,
534                                                          GTK_PARAM_READWRITE));
535   g_object_class_install_property (gobject_class,
536                                    PROP_CAN_FOCUS,
537                                    g_param_spec_boolean ("can-focus",
538                                                          P_("Can focus"),
539                                                          P_("Whether the widget can accept the input focus"),
540                                                          FALSE,
541                                                          GTK_PARAM_READWRITE));
542   g_object_class_install_property (gobject_class,
543                                    PROP_HAS_FOCUS,
544                                    g_param_spec_boolean ("has-focus",
545                                                          P_("Has focus"),
546                                                          P_("Whether the widget has the input focus"),
547                                                          FALSE,
548                                                          GTK_PARAM_READWRITE));
549   g_object_class_install_property (gobject_class,
550                                    PROP_IS_FOCUS,
551                                    g_param_spec_boolean ("is-focus",
552                                                          P_("Is focus"),
553                                                          P_("Whether the widget is the focus widget within the toplevel"),
554                                                          FALSE,
555                                                          GTK_PARAM_READWRITE));
556   g_object_class_install_property (gobject_class,
557                                    PROP_CAN_DEFAULT,
558                                    g_param_spec_boolean ("can-default",
559                                                          P_("Can default"),
560                                                          P_("Whether the widget can be the default widget"),
561                                                          FALSE,
562                                                          GTK_PARAM_READWRITE));
563   g_object_class_install_property (gobject_class,
564                                    PROP_HAS_DEFAULT,
565                                    g_param_spec_boolean ("has-default",
566                                                          P_("Has default"),
567                                                          P_("Whether the widget is the default widget"),
568                                                          FALSE,
569                                                          GTK_PARAM_READWRITE));
570   g_object_class_install_property (gobject_class,
571                                    PROP_RECEIVES_DEFAULT,
572                                    g_param_spec_boolean ("receives-default",
573                                                          P_("Receives default"),
574                                                          P_("If TRUE, the widget will receive the default action when it is focused"),
575                                                          FALSE,
576                                                          GTK_PARAM_READWRITE));
577   g_object_class_install_property (gobject_class,
578                                    PROP_COMPOSITE_CHILD,
579                                    g_param_spec_boolean ("composite-child",
580                                                          P_("Composite child"),
581                                                          P_("Whether the widget is part of a composite widget"),
582                                                          FALSE,
583                                                          GTK_PARAM_READABLE));
584   g_object_class_install_property (gobject_class,
585                                    PROP_STYLE,
586                                    g_param_spec_object ("style",
587                                                         P_("Style"),
588                                                         P_("The style of the widget, which contains information about how it will look (colors etc)"),
589                                                         GTK_TYPE_STYLE,
590                                                         GTK_PARAM_READWRITE));
591   g_object_class_install_property (gobject_class,
592                                    PROP_EVENTS,
593                                    g_param_spec_flags ("events",
594                                                        P_("Events"),
595                                                        P_("The event mask that decides what kind of GdkEvents this widget gets"),
596                                                        GDK_TYPE_EVENT_MASK,
597                                                        GDK_STRUCTURE_MASK,
598                                                        GTK_PARAM_READWRITE));
599   g_object_class_install_property (gobject_class,
600                                    PROP_EXTENSION_EVENTS,
601                                    g_param_spec_enum ("extension-events",
602                                                       P_("Extension events"),
603                                                       P_("The mask that decides what kind of extension events this widget gets"),
604                                                       GDK_TYPE_EXTENSION_MODE,
605                                                       GDK_EXTENSION_EVENTS_NONE,
606                                                       GTK_PARAM_READWRITE));
607   g_object_class_install_property (gobject_class,
608                                    PROP_NO_SHOW_ALL,
609                                    g_param_spec_boolean ("no-show-all",
610                                                          P_("No show all"),
611                                                          P_("Whether gtk_widget_show_all() should not affect this widget"),
612                                                          FALSE,
613                                                          GTK_PARAM_READWRITE));
614
615 /**
616  * GtkWidget:has-tooltip:
617  *
618  * Enables or disables the emission of #GtkWidget::query-tooltip on @widget.  
619  * A value of %TRUE indicates that @widget can have a tooltip, in this case
620  * the widget will be queried using #GtkWidget::query-tooltip to determine
621  * whether it will provide a tooltip or not.
622  *
623  * Note that setting this property to %TRUE for the first time will change
624  * the event masks of the GdkWindows of this widget to include leave-notify
625  * and motion-notify events.  This cannot and will not be undone when the
626  * property is set to %FALSE again.
627  *
628  * Since: 2.12
629  */
630   g_object_class_install_property (gobject_class,
631                                    PROP_HAS_TOOLTIP,
632                                    g_param_spec_boolean ("has-tooltip",
633                                                          P_("Has tooltip"),
634                                                          P_("Whether this widget has a tooltip"),
635                                                          FALSE,
636                                                          GTK_PARAM_READWRITE));
637   /**
638    * GtkWidget:tooltip-text:
639    *
640    * Sets the text of tooltip to be the given string.
641    *
642    * Also see gtk_tooltip_set_text().
643    *
644    * This is a convenience property which will take care of getting the
645    * tooltip shown if the given string is not %NULL: #GtkWidget:has-tooltip
646    * will automatically be set to %TRUE and there will be taken care of
647    * #GtkWidget::query-tooltip in the default signal handler.
648    *
649    * Since: 2.12
650    */
651   g_object_class_install_property (gobject_class,
652                                    PROP_TOOLTIP_TEXT,
653                                    g_param_spec_string ("tooltip-text",
654                                                         P_("Tooltip Text"),
655                                                         P_("The contents of the tooltip for this widget"),
656                                                         NULL,
657                                                         GTK_PARAM_READWRITE));
658   /**
659    * GtkWidget:tooltip-markup:
660    *
661    * Sets the text of tooltip to be the given string, which is marked up
662    * with the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
663    * Also see gtk_tooltip_set_markup().
664    *
665    * This is a convenience property which will take care of getting the
666    * tooltip shown if the given string is not %NULL: #GtkWidget:has-tooltip
667    * will automatically be set to %TRUE and there will be taken care of
668    * #GtkWidget::query-tooltip in the default signal handler.
669    *
670    * Since: 2.12
671    */
672   g_object_class_install_property (gobject_class,
673                                    PROP_TOOLTIP_MARKUP,
674                                    g_param_spec_string ("tooltip-markup",
675                                                         P_("Tooltip markup"),
676                                                         P_("The contents of the tooltip for this widget"),
677                                                         NULL,
678                                                         GTK_PARAM_READWRITE));
679
680   /**
681    * GtkWidget:window:
682    *
683    * The widget's window if it is realized, %NULL otherwise.
684    *
685    * Since: 2.14
686    */
687   g_object_class_install_property (gobject_class,
688                                    PROP_WINDOW,
689                                    g_param_spec_object ("window",
690                                                         P_("Window"),
691                                                         P_("The widget's window if it is realized"),
692                                                         GDK_TYPE_WINDOW,
693                                                         GTK_PARAM_READABLE));
694
695   widget_signals[SHOW] =
696     g_signal_new (I_("show"),
697                   G_TYPE_FROM_CLASS (gobject_class),
698                   G_SIGNAL_RUN_FIRST,
699                   G_STRUCT_OFFSET (GtkWidgetClass, show),
700                   NULL, NULL,
701                   _gtk_marshal_VOID__VOID,
702                   G_TYPE_NONE, 0);
703   widget_signals[HIDE] =
704     g_signal_new (I_("hide"),
705                   G_TYPE_FROM_CLASS (gobject_class),
706                   G_SIGNAL_RUN_FIRST,
707                   G_STRUCT_OFFSET (GtkWidgetClass, hide),
708                   NULL, NULL,
709                   _gtk_marshal_VOID__VOID,
710                   G_TYPE_NONE, 0);
711   widget_signals[MAP] =
712     g_signal_new (I_("map"),
713                   G_TYPE_FROM_CLASS (gobject_class),
714                   G_SIGNAL_RUN_FIRST,
715                   G_STRUCT_OFFSET (GtkWidgetClass, map),
716                   NULL, NULL,
717                   _gtk_marshal_VOID__VOID,
718                   G_TYPE_NONE, 0);
719   widget_signals[UNMAP] =
720     g_signal_new (I_("unmap"),
721                   G_TYPE_FROM_CLASS (gobject_class),
722                   G_SIGNAL_RUN_FIRST,
723                   G_STRUCT_OFFSET (GtkWidgetClass, unmap),
724                   NULL, NULL,
725                   _gtk_marshal_VOID__VOID,
726                   G_TYPE_NONE, 0);
727   widget_signals[REALIZE] =
728     g_signal_new (I_("realize"),
729                   G_TYPE_FROM_CLASS (gobject_class),
730                   G_SIGNAL_RUN_FIRST,
731                   G_STRUCT_OFFSET (GtkWidgetClass, realize),
732                   NULL, NULL,
733                   _gtk_marshal_VOID__VOID,
734                   G_TYPE_NONE, 0);
735   widget_signals[UNREALIZE] =
736     g_signal_new (I_("unrealize"),
737                   G_TYPE_FROM_CLASS (gobject_class),
738                   G_SIGNAL_RUN_LAST,
739                   G_STRUCT_OFFSET (GtkWidgetClass, unrealize),
740                   NULL, NULL,
741                   _gtk_marshal_VOID__VOID,
742                   G_TYPE_NONE, 0);
743   widget_signals[SIZE_REQUEST] =
744     g_signal_new (I_("size-request"),
745                   G_TYPE_FROM_CLASS (gobject_class),
746                   G_SIGNAL_RUN_FIRST,
747                   G_STRUCT_OFFSET (GtkWidgetClass, size_request),
748                   NULL, NULL,
749                   _gtk_marshal_VOID__BOXED,
750                   G_TYPE_NONE, 1,
751                   GTK_TYPE_REQUISITION | G_SIGNAL_TYPE_STATIC_SCOPE);
752   widget_signals[SIZE_ALLOCATE] = 
753     g_signal_new (I_("size-allocate"),
754                   G_TYPE_FROM_CLASS (gobject_class),
755                   G_SIGNAL_RUN_FIRST,
756                   G_STRUCT_OFFSET (GtkWidgetClass, size_allocate),
757                   NULL, NULL,
758                   _gtk_marshal_VOID__BOXED,
759                   G_TYPE_NONE, 1,
760                   GDK_TYPE_RECTANGLE | G_SIGNAL_TYPE_STATIC_SCOPE);
761
762   widget_signals[STATE_CHANGED] =
763     g_signal_new (I_("state-changed"),
764                   G_TYPE_FROM_CLASS (gobject_class),
765                   G_SIGNAL_RUN_FIRST,
766                   G_STRUCT_OFFSET (GtkWidgetClass, state_changed),
767                   NULL, NULL,
768                   _gtk_marshal_VOID__ENUM,
769                   G_TYPE_NONE, 1,
770                   GTK_TYPE_STATE_TYPE);
771
772   /**
773    * GtkWidget::parent-set:
774    * @widget: the object on which the signal is emitted
775    * @old_parent: the previous parent, or %NULL if the widget 
776    *   just got its initial parent.
777    *
778    * The ::parent-set signal is emitted when a new parent 
779    * has been set on a widget. 
780    */
781   widget_signals[PARENT_SET] =
782     g_signal_new (I_("parent-set"),
783                   G_TYPE_FROM_CLASS (gobject_class),
784                   G_SIGNAL_RUN_FIRST,
785                   G_STRUCT_OFFSET (GtkWidgetClass, parent_set),
786                   NULL, NULL,
787                   _gtk_marshal_VOID__OBJECT,
788                   G_TYPE_NONE, 1,
789                   GTK_TYPE_WIDGET);
790
791   /**
792    * GtkWidget::hierarchy-changed:
793    * @widget: the object on which the signal is emitted
794    * @previous_toplevel: the previous toplevel ancestor, or %NULL
795    *   if the widget was previously unanchored
796    *
797    * The ::hierarchy-changed signal is emitted when the
798    * anchored state of a widget changes. A widget is
799    * <firstterm>anchored</firstterm> when its toplevel
800    * ancestor is a #GtkWindow. This signal is emitted when
801    * a widget changes from un-anchored to anchored or vice-versa.
802    */
803   widget_signals[HIERARCHY_CHANGED] =
804     g_signal_new (I_("hierarchy-changed"),
805                   G_TYPE_FROM_CLASS (gobject_class),
806                   G_SIGNAL_RUN_LAST,
807                   G_STRUCT_OFFSET (GtkWidgetClass, hierarchy_changed),
808                   NULL, NULL,
809                   _gtk_marshal_VOID__OBJECT,
810                   G_TYPE_NONE, 1,
811                   GTK_TYPE_WIDGET);
812
813   /**
814    * GtkWidget::style-set:
815    * @widget: the object on which the signal is emitted
816    * @previous_style: the previous style, or %NULL if the widget 
817    *   just got its initial style 
818    *
819    * The ::style-set signal is emitted when a new style has been set 
820    * on a widget. Note that style-modifying functions like 
821    * gtk_widget_modify_base() also cause this signal to be emitted.
822    */
823   widget_signals[STYLE_SET] =
824     g_signal_new (I_("style-set"),
825                   G_TYPE_FROM_CLASS (gobject_class),
826                   G_SIGNAL_RUN_FIRST,
827                   G_STRUCT_OFFSET (GtkWidgetClass, style_set),
828                   NULL, NULL,
829                   _gtk_marshal_VOID__OBJECT,
830                   G_TYPE_NONE, 1,
831                   GTK_TYPE_STYLE);
832 /**
833  * GtkWidget::direction-changed:
834  * @widget: the object on which the signal is emitted
835  * @previous_direction: the previous text direction of @widget
836  *
837  * The ::direction-changed signal is emitted when the text direction
838  * of a widget changes.
839  */
840   widget_signals[DIRECTION_CHANGED] =
841     g_signal_new (I_("direction-changed"),
842                   G_TYPE_FROM_CLASS (gobject_class),
843                   G_SIGNAL_RUN_FIRST,
844                   G_STRUCT_OFFSET (GtkWidgetClass, direction_changed),
845                   NULL, NULL,
846                   _gtk_marshal_VOID__ENUM,
847                   G_TYPE_NONE, 1,
848                   GTK_TYPE_TEXT_DIRECTION);
849
850   /**
851    * GtkWidget::grab-notify:
852    * @widget: the object which received the signal
853    * @was_grabbed: %FALSE if the widget becomes shadowed, %TRUE
854    *               if it becomes unshadowed
855    *
856    * The ::grab-notify signal is emitted when a widget becomes
857    * shadowed by a GTK+ grab (not a pointer or keyboard grab) on 
858    * another widget, or when it becomes unshadowed due to a grab 
859    * being removed.
860    * 
861    * A widget is shadowed by a gtk_grab_add() when the topmost 
862    * grab widget in the grab stack of its window group is not 
863    * its ancestor.
864    */
865   widget_signals[GRAB_NOTIFY] =
866     g_signal_new (I_("grab-notify"),
867                   G_TYPE_FROM_CLASS (gobject_class),
868                   G_SIGNAL_RUN_FIRST,
869                   G_STRUCT_OFFSET (GtkWidgetClass, grab_notify),
870                   NULL, NULL,
871                   _gtk_marshal_VOID__BOOLEAN,
872                   G_TYPE_NONE, 1,
873                   G_TYPE_BOOLEAN);
874
875 /**
876  * GtkWidget::child-notify:
877  * @widget: the object which received the signal
878  * @pspec: the #GParamSpec of the changed child property
879  *
880  * The ::child-notify signal is emitted for each 
881  * <link linkend="child-properties">child property</link>  that has
882  * changed on an object. The signal's detail holds the property name. 
883  */
884   widget_signals[CHILD_NOTIFY] =
885     g_signal_new (I_("child-notify"),
886                    G_TYPE_FROM_CLASS (gobject_class),
887                    G_SIGNAL_RUN_FIRST | G_SIGNAL_NO_RECURSE | G_SIGNAL_DETAILED | G_SIGNAL_NO_HOOKS,
888                    G_STRUCT_OFFSET (GtkWidgetClass, child_notify),
889                    NULL, NULL,
890                    g_cclosure_marshal_VOID__PARAM,
891                    G_TYPE_NONE, 1,
892                    G_TYPE_PARAM);
893   widget_signals[MNEMONIC_ACTIVATE] =
894     g_signal_new (I_("mnemonic-activate"),
895                   G_TYPE_FROM_CLASS (gobject_class),
896                   G_SIGNAL_RUN_LAST,
897                   G_STRUCT_OFFSET (GtkWidgetClass, mnemonic_activate),
898                   _gtk_boolean_handled_accumulator, NULL,
899                   _gtk_marshal_BOOLEAN__BOOLEAN,
900                   G_TYPE_BOOLEAN, 1,
901                   G_TYPE_BOOLEAN);
902   widget_signals[GRAB_FOCUS] =
903     g_signal_new (I_("grab-focus"),
904                   G_TYPE_FROM_CLASS (gobject_class),
905                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
906                   G_STRUCT_OFFSET (GtkWidgetClass, grab_focus),
907                   NULL, NULL,
908                   _gtk_marshal_VOID__VOID,
909                   G_TYPE_NONE, 0);
910   widget_signals[FOCUS] =
911     g_signal_new (I_("focus"),
912                   G_TYPE_FROM_CLASS (object_class),
913                   G_SIGNAL_RUN_LAST,
914                   G_STRUCT_OFFSET (GtkWidgetClass, focus),
915                   _gtk_boolean_handled_accumulator, NULL,
916                   _gtk_marshal_BOOLEAN__ENUM,
917                   G_TYPE_BOOLEAN, 1,
918                   GTK_TYPE_DIRECTION_TYPE);
919   widget_signals[MOVE_FOCUS] =
920     g_signal_new_class_handler (I_("move-focus"),
921                                 G_TYPE_FROM_CLASS (object_class),
922                                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
923                                 G_CALLBACK (gtk_widget_real_move_focus),
924                                 NULL, NULL,
925                                 _gtk_marshal_VOID__ENUM,
926                                 G_TYPE_NONE,
927                                 1,
928                                 GTK_TYPE_DIRECTION_TYPE);
929   /**
930    * GtkWidget::event:
931    * @widget: the object which received the signal.
932    * @event: the #GdkEvent which triggered this signal
933    *
934    * The GTK+ main loop will emit three signals for each GDK event delivered
935    * to a widget: one generic ::event signal, another, more specific,
936    * signal that matches the type of event delivered (e.g. 
937    * #GtkWidget::key-press-event) and finally a generic 
938    * #GtkWidget::event-after signal.
939    *
940    * Returns: %TRUE to stop other handlers from being invoked for the event 
941    * and to cancel the emission of the second specific ::event signal.
942    *   %FALSE to propagate the event further and to allow the emission of 
943    *   the second signal. The ::event-after signal is emitted regardless of
944    *   the return value.
945    */
946   widget_signals[EVENT] =
947     g_signal_new (I_("event"),
948                   G_TYPE_FROM_CLASS (gobject_class),
949                   G_SIGNAL_RUN_LAST,
950                   G_STRUCT_OFFSET (GtkWidgetClass, event),
951                   _gtk_boolean_handled_accumulator, NULL,
952                   _gtk_marshal_BOOLEAN__BOXED,
953                   G_TYPE_BOOLEAN, 1,
954                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
955
956   /**
957    * GtkWidget::event-after:
958    * @widget: the object which received the signal.
959    * @event: the #GdkEvent which triggered this signal
960    *
961    * After the emission of the #GtkWidget::event signal and (optionally) 
962    * the second more specific signal, ::event-after will be emitted 
963    * regardless of the previous two signals handlers return values.
964    *
965    */
966   widget_signals[EVENT_AFTER] =
967     g_signal_new (I_("event-after"),
968                   G_TYPE_FROM_CLASS (gobject_class),
969                   0,
970                   0,
971                   NULL, NULL,
972                   _gtk_marshal_VOID__BOXED,
973                   G_TYPE_NONE, 1,
974                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
975
976   /**
977    * GtkWidget::button-press-event:
978    * @widget: the object which received the signal.
979    * @event: the #GdkEventButton which triggered this signal
980    *
981    * The ::button-press-event signal will be emitted when a button
982    * (typically from a mouse) is pressed.
983    *
984    * To receive this signal, the #GdkWindow associated to the 
985    * widget needs to enable the #GDK_BUTTON_PRESS_MASK mask.
986    *
987    * This signal will be sent to the grab widget if there is one.
988    *
989    * Returns: %TRUE to stop other handlers from being invoked for the event. 
990    *   %FALSE to propagate the event further.
991    */
992   widget_signals[BUTTON_PRESS_EVENT] =
993     g_signal_new (I_("button-press-event"),
994                   G_TYPE_FROM_CLASS (gobject_class),
995                   G_SIGNAL_RUN_LAST,
996                   G_STRUCT_OFFSET (GtkWidgetClass, button_press_event),
997                   _gtk_boolean_handled_accumulator, NULL,
998                   _gtk_marshal_BOOLEAN__BOXED,
999                   G_TYPE_BOOLEAN, 1,
1000                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1001
1002   /**
1003    * GtkWidget::button-release-event:
1004    * @widget: the object which received the signal.
1005    * @event: the #GdkEventButton which triggered this signal
1006    *
1007    * The ::button-release-event signal will be emitted when a button
1008    * (typically from a mouse) is released.
1009    *
1010    * To receive this signal, the #GdkWindow associated to the 
1011    * widget needs to enable the #GDK_BUTTON_RELEASE_MASK mask.
1012    *
1013    * This signal will be sent to the grab widget if there is one.
1014    *
1015    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1016    *   %FALSE to propagate the event further.
1017    */
1018   widget_signals[BUTTON_RELEASE_EVENT] =
1019     g_signal_new (I_("button-release-event"),
1020                   G_TYPE_FROM_CLASS (gobject_class),
1021                   G_SIGNAL_RUN_LAST,
1022                   G_STRUCT_OFFSET (GtkWidgetClass, button_release_event),
1023                   _gtk_boolean_handled_accumulator, NULL,
1024                   _gtk_marshal_BOOLEAN__BOXED,
1025                   G_TYPE_BOOLEAN, 1,
1026                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1027
1028   /**
1029    * GtkWidget::scroll-event:
1030    * @widget: the object which received the signal.
1031    * @event: the #GdkEventScroll which triggered this signal
1032    *
1033    * The ::scroll-event signal is emitted when a button in the 4 to 7
1034    * range is pressed. Wheel mice are usually configured to generate 
1035    * button press events for buttons 4 and 5 when the wheel is turned.
1036    *
1037    * To receive this signal, the #GdkWindow associated to the widget needs
1038    * to enable the #GDK_BUTTON_PRESS_MASK mask.
1039    *
1040    * This signal will be sent to the grab widget if there is one.
1041    *
1042    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1043    *   %FALSE to propagate the event further.
1044    */
1045   widget_signals[SCROLL_EVENT] =
1046     g_signal_new (I_("scroll-event"),
1047                   G_TYPE_FROM_CLASS (gobject_class),
1048                   G_SIGNAL_RUN_LAST,
1049                   G_STRUCT_OFFSET (GtkWidgetClass, scroll_event),
1050                   _gtk_boolean_handled_accumulator, NULL,
1051                   _gtk_marshal_BOOLEAN__BOXED,
1052                   G_TYPE_BOOLEAN, 1,
1053                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1054   /**
1055    * GtkWidget::motion-notify-event:
1056    * @widget: the object which received the signal.
1057    * @event: the #GdkEventMotion which triggered this signal
1058    *
1059    * The ::motion-notify-event signal is emitted when the pointer moves 
1060    * over the widget's #GdkWindow.
1061    *
1062    * To receive this signal, the #GdkWindow associated to the widget 
1063    * needs to enable the #GDK_POINTER_MOTION_MASK mask.
1064    *
1065    * This signal will be sent to the grab widget if there is one.
1066    *
1067    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1068    *   %FALSE to propagate the event further.
1069    */
1070   widget_signals[MOTION_NOTIFY_EVENT] =
1071     g_signal_new (I_("motion-notify-event"),
1072                   G_TYPE_FROM_CLASS (gobject_class),
1073                   G_SIGNAL_RUN_LAST,
1074                   G_STRUCT_OFFSET (GtkWidgetClass, motion_notify_event),
1075                   _gtk_boolean_handled_accumulator, NULL,
1076                   _gtk_marshal_BOOLEAN__BOXED,
1077                   G_TYPE_BOOLEAN, 1,
1078                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1079
1080   /**
1081    * GtkWidget::composited-changed:
1082    * @widget: the object on which the signal is emitted
1083    *
1084    * The ::composited-changed signal is emitted when the composited
1085    * status of @widget<!-- -->s screen changes. 
1086    * See gdk_screen_is_composited().
1087    */
1088   widget_signals[COMPOSITED_CHANGED] =
1089     g_signal_new (I_("composited-changed"),
1090                   G_TYPE_FROM_CLASS (gobject_class),
1091                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1092                   G_STRUCT_OFFSET (GtkWidgetClass, composited_changed),
1093                   NULL, NULL,
1094                   _gtk_marshal_VOID__VOID,
1095                   G_TYPE_NONE, 0);
1096
1097   /**
1098    * GtkWidget::keynav-failed:
1099    * @widget: the object which received the signal
1100    * @direction: the direction of movement
1101    *
1102    * Gets emitted if keyboard navigation fails. 
1103    * See gtk_widget_keynav_failed() for details.
1104    *
1105    * Returns: %TRUE if stopping keyboard navigation is fine, %FALSE
1106    *          if the emitting widget should try to handle the keyboard
1107    *          navigation attempt in its parent container(s).
1108    *
1109    * Since: 2.12
1110    **/
1111   widget_signals[KEYNAV_FAILED] =
1112     g_signal_new_class_handler (I_("keynav-failed"),
1113                                 G_TYPE_FROM_CLASS (gobject_class),
1114                                 G_SIGNAL_RUN_LAST,
1115                                 G_CALLBACK (gtk_widget_real_keynav_failed),
1116                                 _gtk_boolean_handled_accumulator, NULL,
1117                                 _gtk_marshal_BOOLEAN__ENUM,
1118                                 G_TYPE_BOOLEAN, 1,
1119                                 GTK_TYPE_DIRECTION_TYPE);
1120
1121   /**
1122    * GtkWidget::delete-event:
1123    * @widget: the object which received the signal
1124    * @event: the event which triggered this signal
1125    *
1126    * The ::delete-event signal is emitted if a user requests that
1127    * a toplevel window is closed. The default handler for this signal
1128    * destroys the window. Connecting gtk_widget_hide_on_delete() to
1129    * this signal will cause the window to be hidden instead, so that
1130    * it can later be shown again without reconstructing it.
1131    *
1132    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1133    *   %FALSE to propagate the event further.
1134    */
1135   widget_signals[DELETE_EVENT] =
1136     g_signal_new (I_("delete-event"),
1137                   G_TYPE_FROM_CLASS (gobject_class),
1138                   G_SIGNAL_RUN_LAST,
1139                   G_STRUCT_OFFSET (GtkWidgetClass, delete_event),
1140                   _gtk_boolean_handled_accumulator, NULL,
1141                   _gtk_marshal_BOOLEAN__BOXED,
1142                   G_TYPE_BOOLEAN, 1,
1143                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1144
1145   /**
1146    * GtkWidget::destroy-event:
1147    * @widget: the object which received the signal.
1148    * @event: the event which triggered this signal
1149    *
1150    * The ::destroy-event signal is emitted when a #GdkWindow is destroyed.
1151    * You rarely get this signal, because most widgets disconnect themselves 
1152    * from their window before they destroy it, so no widget owns the 
1153    * window at destroy time.
1154    * 
1155    * To receive this signal, the #GdkWindow associated to the widget needs
1156    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
1157    * automatically for all new windows.
1158    *
1159    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1160    *   %FALSE to propagate the event further.
1161    */
1162   widget_signals[DESTROY_EVENT] =
1163     g_signal_new (I_("destroy-event"),
1164                   G_TYPE_FROM_CLASS (gobject_class),
1165                   G_SIGNAL_RUN_LAST,
1166                   G_STRUCT_OFFSET (GtkWidgetClass, destroy_event),
1167                   _gtk_boolean_handled_accumulator, NULL,
1168                   _gtk_marshal_BOOLEAN__BOXED,
1169                   G_TYPE_BOOLEAN, 1,
1170                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1171
1172   /**
1173    * GtkWidget::expose-event:
1174    * @widget: the object which received the signal.
1175    * @event: the #GdkEventExpose which triggered this signal
1176    *
1177    * The ::expose-event signal is emitted when an area of a previously
1178    * obscured #GdkWindow is made visible and needs to be redrawn.
1179    * #GTK_NO_WINDOW widgets will get a synthesized event from their parent 
1180    * widget.
1181    *
1182    * To receive this signal, the #GdkWindow associated to the widget needs
1183    * to enable the #GDK_EXPOSURE_MASK mask.
1184    * 
1185    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1186    *   %FALSE to propagate the event further.
1187    */
1188   widget_signals[EXPOSE_EVENT] =
1189     g_signal_new (I_("expose-event"),
1190                   G_TYPE_FROM_CLASS (gobject_class),
1191                   G_SIGNAL_RUN_LAST,
1192                   G_STRUCT_OFFSET (GtkWidgetClass, expose_event),
1193                   _gtk_boolean_handled_accumulator, NULL,
1194                   _gtk_marshal_BOOLEAN__BOXED,
1195                   G_TYPE_BOOLEAN, 1,
1196                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1197
1198   /**
1199    * GtkWidget::key-press-event:
1200    * @widget: the object which received the signal
1201    * @event: the #GdkEventKey which triggered this signal
1202    *
1203    * The ::key-press-event signal is emitted when a key is pressed.
1204    *
1205    * To receive this signal, the #GdkWindow associated to the widget needs
1206    * to enable the #GDK_KEY_PRESS_MASK mask.
1207    *
1208    * This signal will be sent to the grab widget if there is one.
1209    *
1210    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1211    *   %FALSE to propagate the event further.
1212    */
1213   widget_signals[KEY_PRESS_EVENT] =
1214     g_signal_new (I_("key-press-event"),
1215                   G_TYPE_FROM_CLASS (gobject_class),
1216                   G_SIGNAL_RUN_LAST,
1217                   G_STRUCT_OFFSET (GtkWidgetClass, key_press_event),
1218                   _gtk_boolean_handled_accumulator, NULL,
1219                   _gtk_marshal_BOOLEAN__BOXED,
1220                   G_TYPE_BOOLEAN, 1,
1221                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1222
1223   /**
1224    * GtkWidget::key-release-event:
1225    * @widget: the object which received the signal
1226    * @event: the #GdkEventKey which triggered this signal
1227    *
1228    * The ::key-release-event signal is emitted when a key is pressed.
1229    *
1230    * To receive this signal, the #GdkWindow associated to the widget needs
1231    * to enable the #GDK_KEY_RELEASE_MASK mask.
1232    *
1233    * This signal will be sent to the grab widget if there is one.
1234    *
1235    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1236    *   %FALSE to propagate the event further.
1237    */
1238   widget_signals[KEY_RELEASE_EVENT] =
1239     g_signal_new (I_("key-release-event"),
1240                   G_TYPE_FROM_CLASS (gobject_class),
1241                   G_SIGNAL_RUN_LAST,
1242                   G_STRUCT_OFFSET (GtkWidgetClass, key_release_event),
1243                   _gtk_boolean_handled_accumulator, NULL,
1244                   _gtk_marshal_BOOLEAN__BOXED,
1245                   G_TYPE_BOOLEAN, 1,
1246                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1247
1248   /**
1249    * GtkWidget::enter-notify-event:
1250    * @widget: the object which received the signal
1251    * @event: the #GdkEventCrossing which triggered this signal
1252    *
1253    * The ::enter-notify-event will be emitted when the pointer enters
1254    * the @widget's window.
1255    *
1256    * To receive this signal, the #GdkWindow associated to the widget needs
1257    * to enable the #GDK_ENTER_NOTIFY_MASK mask.
1258    *
1259    * This signal will be sent to the grab widget if there is one.
1260    *
1261    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1262    *   %FALSE to propagate the event further.
1263    */
1264   widget_signals[ENTER_NOTIFY_EVENT] =
1265     g_signal_new (I_("enter-notify-event"),
1266                   G_TYPE_FROM_CLASS (gobject_class),
1267                   G_SIGNAL_RUN_LAST,
1268                   G_STRUCT_OFFSET (GtkWidgetClass, enter_notify_event),
1269                   _gtk_boolean_handled_accumulator, NULL,
1270                   _gtk_marshal_BOOLEAN__BOXED,
1271                   G_TYPE_BOOLEAN, 1,
1272                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1273
1274   /**
1275    * GtkWidget::leave-notify-event:
1276    * @widget: the object which received the signal
1277    * @event: the #GdkEventCrossing which triggered this signal
1278    *
1279    * The ::leave-notify-event will be emitted when the pointer leaves
1280    * the @widget's window.
1281    *
1282    * To receive this signal, the #GdkWindow associated to the widget needs
1283    * to enable the #GDK_LEAVE_NOTIFY_MASK mask.
1284    *
1285    * This signal will be sent to the grab widget if there is one.
1286    *
1287    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1288    *   %FALSE to propagate the event further.
1289    */
1290   widget_signals[LEAVE_NOTIFY_EVENT] =
1291     g_signal_new (I_("leave-notify-event"),
1292                   G_TYPE_FROM_CLASS (gobject_class),
1293                   G_SIGNAL_RUN_LAST,
1294                   G_STRUCT_OFFSET (GtkWidgetClass, leave_notify_event),
1295                   _gtk_boolean_handled_accumulator, NULL,
1296                   _gtk_marshal_BOOLEAN__BOXED,
1297                   G_TYPE_BOOLEAN, 1,
1298                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1299
1300   /**
1301    * GtkWidget::configure-event
1302    * @widget: the object which received the signal
1303    * @event: the #GdkEventConfigure which triggered this signal
1304    *
1305    * The ::configure-event signal will be emitted when the size, position or
1306    * stacking of the @widget's window has changed.
1307    *
1308    * To receive this signal, the #GdkWindow associated to the widget needs
1309    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
1310    * automatically for all new windows.
1311    *
1312    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1313    *   %FALSE to propagate the event further.
1314    */
1315   widget_signals[CONFIGURE_EVENT] =
1316     g_signal_new (I_("configure-event"),
1317                   G_TYPE_FROM_CLASS (gobject_class),
1318                   G_SIGNAL_RUN_LAST,
1319                   G_STRUCT_OFFSET (GtkWidgetClass, configure_event),
1320                   _gtk_boolean_handled_accumulator, NULL,
1321                   _gtk_marshal_BOOLEAN__BOXED,
1322                   G_TYPE_BOOLEAN, 1,
1323                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1324
1325   /**
1326    * GtkWidget::focus-in-event
1327    * @widget: the object which received the signal
1328    * @event: the #GdkEventFocus which triggered this signal
1329    *
1330    * The ::focus-in-event signal will be emitted when the keyboard focus
1331    * enters the @widget's window.
1332    *
1333    * To receive this signal, the #GdkWindow associated to the widget needs
1334    * to enable the #GDK_FOCUS_CHANGE_MASK mask.
1335    *
1336    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1337    *   %FALSE to propagate the event further.
1338    */
1339   widget_signals[FOCUS_IN_EVENT] =
1340     g_signal_new (I_("focus-in-event"),
1341                   G_TYPE_FROM_CLASS (gobject_class),
1342                   G_SIGNAL_RUN_LAST,
1343                   G_STRUCT_OFFSET (GtkWidgetClass, focus_in_event),
1344                   _gtk_boolean_handled_accumulator, NULL,
1345                   _gtk_marshal_BOOLEAN__BOXED,
1346                   G_TYPE_BOOLEAN, 1,
1347                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1348
1349   /**
1350    * GtkWidget::focus-out-event
1351    * @widget: the object which received the signal
1352    * @event: the #GdkEventFocus which triggered this signal
1353    *
1354    * The ::focus-out-event signal will be emitted when the keyboard focus
1355    * leaves the @widget's window.
1356    *
1357    * To receive this signal, the #GdkWindow associated to the widget needs
1358    * to enable the #GDK_FOCUS_CHANGE_MASK mask.
1359    *
1360    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1361    *   %FALSE to propagate the event further.
1362    */
1363   widget_signals[FOCUS_OUT_EVENT] =
1364     g_signal_new (I_("focus-out-event"),
1365                   G_TYPE_FROM_CLASS (gobject_class),
1366                   G_SIGNAL_RUN_LAST,
1367                   G_STRUCT_OFFSET (GtkWidgetClass, focus_out_event),
1368                   _gtk_boolean_handled_accumulator, NULL,
1369                   _gtk_marshal_BOOLEAN__BOXED,
1370                   G_TYPE_BOOLEAN, 1,
1371                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1372
1373   /**
1374    * GtkWidget::map-event
1375    * @widget: the object which received the signal
1376    * @event: the #GdkEventAny which triggered this signal
1377    *
1378    * The ::map-event signal will be emitted when the @widget's window is
1379    * mapped. A window is mapped when it becomes visible on the screen.
1380    *
1381    * To receive this signal, the #GdkWindow associated to the widget needs
1382    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
1383    * automatically for all new windows.
1384    *
1385    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1386    *   %FALSE to propagate the event further.
1387    */
1388   widget_signals[MAP_EVENT] =
1389     g_signal_new (I_("map-event"),
1390                   G_TYPE_FROM_CLASS (gobject_class),
1391                   G_SIGNAL_RUN_LAST,
1392                   G_STRUCT_OFFSET (GtkWidgetClass, map_event),
1393                   _gtk_boolean_handled_accumulator, NULL,
1394                   _gtk_marshal_BOOLEAN__BOXED,
1395                   G_TYPE_BOOLEAN, 1,
1396                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1397
1398   /**
1399    * GtkWidget::unmap-event
1400    * @widget: the object which received the signal
1401    * @event: the #GdkEventAny which triggered this signal
1402    *
1403    * The ::unmap-event signal will be emitted when the @widget's window is
1404    * unmapped. A window is unmapped when it becomes invisible on the screen.
1405    *
1406    * To receive this signal, the #GdkWindow associated to the widget needs
1407    * to enable the #GDK_STRUCTURE_MASK mask. GDK will enable this mask
1408    * automatically for all new windows.
1409    *
1410    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1411    *   %FALSE to propagate the event further.
1412    */
1413   widget_signals[UNMAP_EVENT] =
1414     g_signal_new (I_("unmap-event"),
1415                   G_TYPE_FROM_CLASS (gobject_class),
1416                   G_SIGNAL_RUN_LAST,
1417                   G_STRUCT_OFFSET (GtkWidgetClass, unmap_event),
1418                   _gtk_boolean_handled_accumulator, NULL,
1419                   _gtk_marshal_BOOLEAN__BOXED,
1420                   G_TYPE_BOOLEAN, 1,
1421                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1422
1423   /**
1424    * GtkWidget::property-notify-event
1425    * @widget: the object which received the signal
1426    * @event: the #GdkEventProperty which triggered this signal
1427    *
1428    * The ::property-notify-event signal will be emitted when a property on
1429    * the @widget's window has been changed or deleted.
1430    *
1431    * To receive this signal, the #GdkWindow associated to the widget needs
1432    * to enable the #GDK_PROPERTY_CHANGE_MASK mask.
1433    *
1434    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1435    *   %FALSE to propagate the event further.
1436    */
1437   widget_signals[PROPERTY_NOTIFY_EVENT] =
1438     g_signal_new (I_("property-notify-event"),
1439                   G_TYPE_FROM_CLASS (gobject_class),
1440                   G_SIGNAL_RUN_LAST,
1441                   G_STRUCT_OFFSET (GtkWidgetClass, property_notify_event),
1442                   _gtk_boolean_handled_accumulator, NULL,
1443                   _gtk_marshal_BOOLEAN__BOXED,
1444                   G_TYPE_BOOLEAN, 1,
1445                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1446
1447   /**
1448    * GtkWidget::selection-clear-event
1449    * @widget: the object which received the signal
1450    * @event: the #GdkEventSelection which triggered this signal
1451    *
1452    * The ::selection-clear-event signal will be emitted when the
1453    * the @widget's window has lost ownership of a selection.
1454    *
1455    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1456    *   %FALSE to propagate the event further.
1457    */
1458   widget_signals[SELECTION_CLEAR_EVENT] =
1459     g_signal_new (I_("selection-clear-event"),
1460                   G_TYPE_FROM_CLASS (gobject_class),
1461                   G_SIGNAL_RUN_LAST,
1462                   G_STRUCT_OFFSET (GtkWidgetClass, selection_clear_event),
1463                   _gtk_boolean_handled_accumulator, NULL,
1464                   _gtk_marshal_BOOLEAN__BOXED,
1465                   G_TYPE_BOOLEAN, 1,
1466                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1467
1468   /**
1469    * GtkWidget::selection-request-event
1470    * @widget: the object which received the signal
1471    * @event: the #GdkEventSelection which triggered this signal
1472    *
1473    * The ::selection-request-event signal will be emitted when
1474    * another client requests ownership of the selection owned by
1475    * the @widget's window.
1476    *
1477    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1478    *   %FALSE to propagate the event further.
1479    */
1480   widget_signals[SELECTION_REQUEST_EVENT] =
1481     g_signal_new (I_("selection-request-event"),
1482                   G_TYPE_FROM_CLASS (gobject_class),
1483                   G_SIGNAL_RUN_LAST,
1484                   G_STRUCT_OFFSET (GtkWidgetClass, selection_request_event),
1485                   _gtk_boolean_handled_accumulator, NULL,
1486                   _gtk_marshal_BOOLEAN__BOXED,
1487                   G_TYPE_BOOLEAN, 1,
1488                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1489
1490   widget_signals[SELECTION_NOTIFY_EVENT] =
1491     g_signal_new (I_("selection-notify-event"),
1492                   G_TYPE_FROM_CLASS (gobject_class),
1493                   G_SIGNAL_RUN_LAST,
1494                   G_STRUCT_OFFSET (GtkWidgetClass, selection_notify_event),
1495                   _gtk_boolean_handled_accumulator, NULL,
1496                   _gtk_marshal_BOOLEAN__BOXED,
1497                   G_TYPE_BOOLEAN, 1,
1498                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1499
1500   widget_signals[SELECTION_RECEIVED] =
1501     g_signal_new (I_("selection-received"),
1502                   G_TYPE_FROM_CLASS (gobject_class),
1503                   G_SIGNAL_RUN_LAST,
1504                   G_STRUCT_OFFSET (GtkWidgetClass, selection_received),
1505                   NULL, NULL,
1506                   _gtk_marshal_VOID__BOXED_UINT,
1507                   G_TYPE_NONE, 2,
1508                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
1509                   G_TYPE_UINT);
1510
1511   widget_signals[SELECTION_GET] =
1512     g_signal_new (I_("selection-get"),
1513                   G_TYPE_FROM_CLASS (gobject_class),
1514                   G_SIGNAL_RUN_LAST,
1515                   G_STRUCT_OFFSET (GtkWidgetClass, selection_get),
1516                   NULL, NULL,
1517                   _gtk_marshal_VOID__BOXED_UINT_UINT,
1518                   G_TYPE_NONE, 3,
1519                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
1520                   G_TYPE_UINT,
1521                   G_TYPE_UINT);
1522
1523   /**
1524    * GtkWidget::proximity-in-event
1525    * @widget: the object which received the signal
1526    * @event: the #GdkEventProximity which triggered this signal
1527    *
1528    * To receive this signal the #GdkWindow associated to the widget needs
1529    * to enable the #GDK_PROXIMITY_IN_MASK mask.
1530    *
1531    * This signal will be sent to the grab widget if there is one.
1532    *
1533    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1534    *   %FALSE to propagate the event further.
1535    */
1536   widget_signals[PROXIMITY_IN_EVENT] =
1537     g_signal_new (I_("proximity-in-event"),
1538                   G_TYPE_FROM_CLASS (gobject_class),
1539                   G_SIGNAL_RUN_LAST,
1540                   G_STRUCT_OFFSET (GtkWidgetClass, proximity_in_event),
1541                   _gtk_boolean_handled_accumulator, NULL,
1542                   _gtk_marshal_BOOLEAN__BOXED,
1543                   G_TYPE_BOOLEAN, 1,
1544                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1545
1546   /**
1547    * GtkWidget::proximity-out-event
1548    * @widget: the object which received the signal
1549    * @event: the #GdkEventProximity which triggered this signal
1550    *
1551    * To receive this signal the #GdkWindow associated to the widget needs
1552    * to enable the #GDK_PROXIMITY_OUT_MASK mask.
1553    *
1554    * This signal will be sent to the grab widget if there is one.
1555    *
1556    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1557    *   %FALSE to propagate the event further.
1558    */
1559   widget_signals[PROXIMITY_OUT_EVENT] =
1560     g_signal_new (I_("proximity-out-event"),
1561                   G_TYPE_FROM_CLASS (gobject_class),
1562                   G_SIGNAL_RUN_LAST,
1563                   G_STRUCT_OFFSET (GtkWidgetClass, proximity_out_event),
1564                   _gtk_boolean_handled_accumulator, NULL,
1565                   _gtk_marshal_BOOLEAN__BOXED,
1566                   G_TYPE_BOOLEAN, 1,
1567                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1568
1569   /**
1570    * GtkWidget::drag-leave:
1571    * @widget: the object which received the signal.
1572    * @drag_context: the drag context
1573    * @time: the timestamp of the motion event
1574    *
1575    * The ::drag-leave signal is emitted on the drop site when the cursor 
1576    * leaves the widget. A typical reason to connect to this signal is to 
1577    * undo things done in #GtkWidget::drag-motion, e.g. undo highlighting 
1578    * with gtk_drag_unhighlight()
1579    */
1580   widget_signals[DRAG_LEAVE] =
1581     g_signal_new (I_("drag-leave"),
1582                   G_TYPE_FROM_CLASS (gobject_class),
1583                   G_SIGNAL_RUN_LAST,
1584                   G_STRUCT_OFFSET (GtkWidgetClass, drag_leave),
1585                   NULL, NULL,
1586                   _gtk_marshal_VOID__OBJECT_UINT,
1587                   G_TYPE_NONE, 2,
1588                   GDK_TYPE_DRAG_CONTEXT,
1589                   G_TYPE_UINT);
1590
1591   /**
1592    * GtkWidget::drag-begin:
1593    * @widget: the object which received the signal
1594    * @drag_context: the drag context
1595    *
1596    * The ::drag-begin signal is emitted on the drag source when a drag is 
1597    * started. A typical reason to connect to this signal is to set up a 
1598    * custom drag icon with gtk_drag_source_set_icon().
1599    *
1600    * Note that some widgets set up a drag icon in the default handler of
1601    * this signal, so you may have to use g_signal_connect_after() to
1602    * override what the default handler did.
1603    */
1604   widget_signals[DRAG_BEGIN] =
1605     g_signal_new (I_("drag-begin"),
1606                   G_TYPE_FROM_CLASS (gobject_class),
1607                   G_SIGNAL_RUN_LAST,
1608                   G_STRUCT_OFFSET (GtkWidgetClass, drag_begin),
1609                   NULL, NULL,
1610                   _gtk_marshal_VOID__OBJECT,
1611                   G_TYPE_NONE, 1,
1612                   GDK_TYPE_DRAG_CONTEXT);
1613
1614   /**
1615    * GtkWidget::drag-end:
1616    * @widget: the object which received the signal
1617    * @drag_context: the drag context
1618    *
1619    * The ::drag-end signal is emitted on the drag source when a drag is 
1620    * finished.  A typical reason to connect to this signal is to undo 
1621    * things done in #GtkWidget::drag-begin.
1622    */
1623   widget_signals[DRAG_END] =
1624     g_signal_new (I_("drag-end"),
1625                   G_TYPE_FROM_CLASS (gobject_class),
1626                   G_SIGNAL_RUN_LAST,
1627                   G_STRUCT_OFFSET (GtkWidgetClass, drag_end),
1628                   NULL, NULL,
1629                   _gtk_marshal_VOID__OBJECT,
1630                   G_TYPE_NONE, 1,
1631                   GDK_TYPE_DRAG_CONTEXT);
1632
1633   /**
1634    * GtkWidget::drag-data-delete:
1635    * @widget: the object which received the signal
1636    * @drag_context: the drag context
1637    *
1638    * The ::drag-data-delete signal is emitted on the drag source when a drag 
1639    * with the action %GDK_ACTION_MOVE is successfully completed. The signal 
1640    * handler is responsible for deleting the data that has been dropped. What 
1641    * "delete" means depends on the context of the drag operation. 
1642    */
1643   widget_signals[DRAG_DATA_DELETE] =
1644     g_signal_new (I_("drag-data-delete"),
1645                   G_TYPE_FROM_CLASS (gobject_class),
1646                   G_SIGNAL_RUN_LAST,
1647                   G_STRUCT_OFFSET (GtkWidgetClass, drag_data_delete),
1648                   NULL, NULL,
1649                   _gtk_marshal_VOID__OBJECT,
1650                   G_TYPE_NONE, 1,
1651                   GDK_TYPE_DRAG_CONTEXT);
1652
1653   /**
1654    * GtkWidget::drag-failed:
1655    * @widget: the object which received the signal
1656    * @drag_context: the drag context
1657    * @result: the result of the drag operation
1658    *
1659    * The ::drag-failed signal is emitted on the drag source when a drag has
1660    * failed. The signal handler may hook custom code to handle a failed DND
1661    * operation based on the type of error, it returns %TRUE is the failure has
1662    * been already handled (not showing the default "drag operation failed"
1663    * animation), otherwise it returns %FALSE.
1664    *
1665    * Return value: %TRUE if the failed drag operation has been already handled.
1666    *
1667    * Since: 2.12
1668    */
1669   widget_signals[DRAG_FAILED] =
1670     g_signal_new (I_("drag-failed"),
1671                   G_TYPE_FROM_CLASS (gobject_class),
1672                   G_SIGNAL_RUN_LAST,
1673                   0, _gtk_boolean_handled_accumulator, NULL,
1674                   _gtk_marshal_BOOLEAN__OBJECT_ENUM,
1675                   G_TYPE_BOOLEAN, 2,
1676                   GDK_TYPE_DRAG_CONTEXT,
1677                   GTK_TYPE_DRAG_RESULT);
1678
1679   /**
1680    * GtkWidget::drag-motion:
1681    * @widget: the object which received the signal
1682    * @drag_context: the drag context
1683    * @x: the x coordinate of the current cursor position
1684    * @y: the y coordinate of the current cursor position
1685    * @time: the timestamp of the motion event
1686    * @returns: whether the cursor position is in a drop zone
1687    *
1688    * The drag-motion signal is emitted on the drop site when the user
1689    * moves the cursor over the widget during a drag. The signal handler
1690    * must determine whether the cursor position is in a drop zone or not.
1691    * If it is not in a drop zone, it returns %FALSE and no further processing
1692    * is necessary. Otherwise, the handler returns %TRUE. In this case, the
1693    * handler is responsible for providing the necessary information for
1694    * displaying feedback to the user, by calling gdk_drag_status().
1695    *
1696    * If the decision whether the drop will be accepted or rejected can't be
1697    * made based solely on the cursor position and the type of the data, the
1698    * handler may inspect the dragged data by calling gtk_drag_get_data() and
1699    * defer the gdk_drag_status() call to the #GtkWidget::drag-data-received
1700    * handler. Note that you cannot not pass #GTK_DEST_DEFAULT_DROP,
1701    * #GTK_DEST_DEFAULT_MOTION or #GTK_DEST_DEFAULT_ALL to gtk_drag_dest_set()
1702    * when using the drag-motion signal that way.
1703    *
1704    * Also note that there is no drag-enter signal. The drag receiver has to
1705    * keep track of whether he has received any drag-motion signals since the
1706    * last #GtkWidget::drag-leave and if not, treat the drag-motion signal as
1707    * an "enter" signal. Upon an "enter", the handler will typically highlight
1708    * the drop site with gtk_drag_highlight().
1709    * |[
1710    * static void
1711    * drag_motion (GtkWidget *widget,
1712    *              GdkDragContext *context,
1713    *              gint x,
1714    *              gint y,
1715    *              guint time)
1716    * {
1717    *   GdkAtom target;
1718    *  
1719    *   PrivateData *private_data = GET_PRIVATE_DATA (widget);
1720    *  
1721    *   if (!private_data->drag_highlight) 
1722    *    {
1723    *      private_data->drag_highlight = 1;
1724    *      gtk_drag_highlight (widget);
1725    *    }
1726    *  
1727    *   target = gtk_drag_dest_find_target (widget, context, NULL);
1728    *   if (target == GDK_NONE)
1729    *     gdk_drag_status (context, 0, time);
1730    *   else 
1731    *    {
1732    *      private_data->pending_status = context->suggested_action;
1733    *      gtk_drag_get_data (widget, context, target, time);
1734    *    }
1735    *  
1736    *   return TRUE;
1737    * }
1738    *   
1739    * static void
1740    * drag_data_received (GtkWidget        *widget,
1741    *                     GdkDragContext   *context,
1742    *                     gint              x,
1743    *                     gint              y,
1744    *                     GtkSelectionData *selection_data,
1745    *                     guint             info,
1746    *                     guint             time)
1747    * {
1748    *   PrivateData *private_data = GET_PRIVATE_DATA (widget);
1749    *   
1750    *   if (private_data->suggested_action) 
1751    *    {
1752    *      private_data->suggested_action = 0;
1753    *      
1754    *     /&ast; We are getting this data due to a request in drag_motion,
1755    *      * rather than due to a request in drag_drop, so we are just
1756    *      * supposed to call gdk_drag_status (), not actually paste in 
1757    *      * the data.
1758    *      &ast;/
1759    *      str = gtk_selection_data_get_text (selection_data);
1760    *      if (!data_is_acceptable (str)) 
1761    *        gdk_drag_status (context, 0, time);
1762    *      else
1763    *        gdk_drag_status (context, private_data->suggested_action, time);
1764    *    }
1765    *   else
1766    *    {
1767    *      /&ast; accept the drop &ast;/
1768    *    }
1769    * }
1770    * ]|
1771    */
1772   widget_signals[DRAG_MOTION] =
1773     g_signal_new (I_("drag-motion"),
1774                   G_TYPE_FROM_CLASS (gobject_class),
1775                   G_SIGNAL_RUN_LAST,
1776                   G_STRUCT_OFFSET (GtkWidgetClass, drag_motion),
1777                   _gtk_boolean_handled_accumulator, NULL,
1778                   _gtk_marshal_BOOLEAN__OBJECT_INT_INT_UINT,
1779                   G_TYPE_BOOLEAN, 4,
1780                   GDK_TYPE_DRAG_CONTEXT,
1781                   G_TYPE_INT,
1782                   G_TYPE_INT,
1783                   G_TYPE_UINT);
1784
1785   /**
1786    * GtkWidget::drag-drop:
1787    * @widget: the object which received the signal
1788    * @drag_context: the drag context
1789    * @x: the x coordinate of the current cursor position
1790    * @y: the y coordinate of the current cursor position
1791    * @time: the timestamp of the motion event
1792    * @returns: whether the cursor position is in a drop zone
1793    *
1794    * The ::drag-drop signal is emitted on the drop site when the user drops 
1795    * the data onto the widget. The signal handler must determine whether 
1796    * the cursor position is in a drop zone or not. If it is not in a drop 
1797    * zone, it returns %FALSE and no further processing is necessary. 
1798    * Otherwise, the handler returns %TRUE. In this case, the handler must 
1799    * ensure that gtk_drag_finish() is called to let the source know that 
1800    * the drop is done. The call to gtk_drag_finish() can be done either 
1801    * directly or in a #GtkWidget::drag-data-received handler which gets 
1802    * triggered by calling gtk_drag_get_data() to receive the data for one 
1803    * or more of the supported targets.
1804    */
1805   widget_signals[DRAG_DROP] =
1806     g_signal_new (I_("drag-drop"),
1807                   G_TYPE_FROM_CLASS (gobject_class),
1808                   G_SIGNAL_RUN_LAST,
1809                   G_STRUCT_OFFSET (GtkWidgetClass, drag_drop),
1810                   _gtk_boolean_handled_accumulator, NULL,
1811                   _gtk_marshal_BOOLEAN__OBJECT_INT_INT_UINT,
1812                   G_TYPE_BOOLEAN, 4,
1813                   GDK_TYPE_DRAG_CONTEXT,
1814                   G_TYPE_INT,
1815                   G_TYPE_INT,
1816                   G_TYPE_UINT);
1817
1818   /** 
1819    * GtkWidget::drag-data-get:
1820    * @widget: the object which received the signal
1821    * @drag_context: the drag context
1822    * @data: the #GtkSelectionData to be filled with the dragged data
1823    * @info: the info that has been registered with the target in the 
1824    *        #GtkTargetList
1825    * @time: the timestamp at which the data was requested
1826    *
1827    * The ::drag-data-get signal is emitted on the drag source when the drop 
1828    * site requests the data which is dragged. It is the responsibility of 
1829    * the signal handler to fill @data with the data in the format which 
1830    * is indicated by @info. See gtk_selection_data_set() and 
1831    * gtk_selection_data_set_text().
1832    */
1833   widget_signals[DRAG_DATA_GET] =
1834     g_signal_new (I_("drag-data-get"),
1835                   G_TYPE_FROM_CLASS (gobject_class),
1836                   G_SIGNAL_RUN_LAST,
1837                   G_STRUCT_OFFSET (GtkWidgetClass, drag_data_get),
1838                   NULL, NULL,
1839                   _gtk_marshal_VOID__OBJECT_BOXED_UINT_UINT,
1840                   G_TYPE_NONE, 4,
1841                   GDK_TYPE_DRAG_CONTEXT,
1842                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
1843                   G_TYPE_UINT,
1844                   G_TYPE_UINT);
1845
1846   /**
1847    * GtkWidget::drag-data-received:
1848    * @widget: the object which received the signal
1849    * @drag_context: the drag context
1850    * @x: where the drop happened
1851    * @y: where the drop happened
1852    * @data: the received data
1853    * @info: the info that has been registered with the target in the 
1854    *        #GtkTargetList
1855    * @time: the timestamp at which the data was received
1856    *
1857    * The ::drag-data-received signal is emitted on the drop site when the 
1858    * dragged data has been received. If the data was received in order to 
1859    * determine whether the drop will be accepted, the handler is expected 
1860    * to call gdk_drag_status() and <emphasis>not</emphasis> finish the drag. 
1861    * If the data was received in response to a #GtkWidget::drag-drop signal 
1862    * (and this is the last target to be received), the handler for this 
1863    * signal is expected to process the received data and then call 
1864    * gtk_drag_finish(), setting the @success parameter depending on whether 
1865    * the data was processed successfully. 
1866    * 
1867    * The handler may inspect and modify @drag_context->action before calling 
1868    * gtk_drag_finish(), e.g. to implement %GDK_ACTION_ASK as shown in the 
1869    * following example:
1870    * |[
1871    * void  
1872    * drag_data_received (GtkWidget          *widget,
1873    *                     GdkDragContext     *drag_context,
1874    *                     gint                x,
1875    *                     gint                y,
1876    *                     GtkSelectionData   *data,
1877    *                     guint               info,
1878    *                     guint               time)
1879    * {
1880    *   if ((data->length >= 0) && (data->format == 8))
1881    *     {
1882    *       if (drag_context->action == GDK_ACTION_ASK) 
1883    *         {
1884    *           GtkWidget *dialog;
1885    *           gint response;
1886    *           
1887    *           dialog = gtk_message_dialog_new (NULL,
1888    *                                            GTK_DIALOG_MODAL | 
1889    *                                            GTK_DIALOG_DESTROY_WITH_PARENT,
1890    *                                            GTK_MESSAGE_INFO,
1891    *                                            GTK_BUTTONS_YES_NO,
1892    *                                            "Move the data ?\n");
1893    *           response = gtk_dialog_run (GTK_DIALOG (dialog));
1894    *           gtk_widget_destroy (dialog);
1895    *             
1896    *           if (response == GTK_RESPONSE_YES)
1897    *             drag_context->action = GDK_ACTION_MOVE;
1898    *           else
1899    *             drag_context->action = GDK_ACTION_COPY;
1900    *          }
1901    *          
1902    *       gtk_drag_finish (drag_context, TRUE, FALSE, time);
1903    *       return;
1904    *     }
1905    *       
1906    *    gtk_drag_finish (drag_context, FALSE, FALSE, time);
1907    *  }
1908    * ]|
1909    */
1910   widget_signals[DRAG_DATA_RECEIVED] =
1911     g_signal_new (I_("drag-data-received"),
1912                   G_TYPE_FROM_CLASS (gobject_class),
1913                   G_SIGNAL_RUN_LAST,
1914                   G_STRUCT_OFFSET (GtkWidgetClass, drag_data_received),
1915                   NULL, NULL,
1916                   _gtk_marshal_VOID__OBJECT_INT_INT_BOXED_UINT_UINT,
1917                   G_TYPE_NONE, 6,
1918                   GDK_TYPE_DRAG_CONTEXT,
1919                   G_TYPE_INT,
1920                   G_TYPE_INT,
1921                   GTK_TYPE_SELECTION_DATA | G_SIGNAL_TYPE_STATIC_SCOPE,
1922                   G_TYPE_UINT,
1923                   G_TYPE_UINT);
1924   
1925   /**
1926    * GtkWidget::visibility-notify-event:
1927    * @widget: the object which received the signal
1928    * @event: the #GdkEventVisibility which triggered this signal
1929    *
1930    * The ::visibility-notify-event will be emitted when the @widget's window
1931    * is obscured or unobscured.
1932    *
1933    * To receive this signal the #GdkWindow associated to the widget needs
1934    * to enable the #GDK_VISIBILITY_NOTIFY_MASK mask.
1935    *
1936    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1937    *   %FALSE to propagate the event further.
1938    */
1939   widget_signals[VISIBILITY_NOTIFY_EVENT] =
1940     g_signal_new (I_("visibility-notify-event"),
1941                   G_TYPE_FROM_CLASS (gobject_class),
1942                   G_SIGNAL_RUN_LAST,
1943                   G_STRUCT_OFFSET (GtkWidgetClass, visibility_notify_event),
1944                   _gtk_boolean_handled_accumulator, NULL,
1945                   _gtk_marshal_BOOLEAN__BOXED,
1946                   G_TYPE_BOOLEAN, 1,
1947                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1948
1949   /**
1950    * GtkWidget::client-event:
1951    * @widget: the object which received the signal
1952    * @event: the #GdkEventClient which triggered this signal
1953    *
1954    * The ::client-event will be emitted when the @widget's window
1955    * receives a message (via a ClientMessage event) from another
1956    * application.
1957    *
1958    * Returns: %TRUE to stop other handlers from being invoked for 
1959    *   the event. %FALSE to propagate the event further.
1960    */
1961   widget_signals[CLIENT_EVENT] =
1962     g_signal_new (I_("client-event"),
1963                   G_TYPE_FROM_CLASS (gobject_class),
1964                   G_SIGNAL_RUN_LAST,
1965                   G_STRUCT_OFFSET (GtkWidgetClass, client_event),
1966                   _gtk_boolean_handled_accumulator, NULL,
1967                   _gtk_marshal_BOOLEAN__BOXED,
1968                   G_TYPE_BOOLEAN, 1,
1969                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1970
1971   /**
1972    * GtkWidget::no-expose-event:
1973    * @widget: the object which received the signal
1974    * @event: the #GdkEventNoExpose which triggered this signal
1975    *
1976    * The ::no-expose-event will be emitted when the @widget's window is 
1977    * drawn as a copy of another #GdkDrawable (with gdk_draw_drawable() or
1978    * gdk_window_copy_area()) which was completely unobscured. If the source
1979    * window was partially obscured #GdkEventExpose events will be generated
1980    * for those areas.
1981    *
1982    * Returns: %TRUE to stop other handlers from being invoked for the event. 
1983    *   %FALSE to propagate the event further.
1984    */
1985   widget_signals[NO_EXPOSE_EVENT] =
1986     g_signal_new (I_("no-expose-event"),
1987                   G_TYPE_FROM_CLASS (gobject_class),
1988                   G_SIGNAL_RUN_LAST,
1989                   G_STRUCT_OFFSET (GtkWidgetClass, no_expose_event),
1990                   _gtk_boolean_handled_accumulator, NULL,
1991                   _gtk_marshal_BOOLEAN__BOXED,
1992                   G_TYPE_BOOLEAN, 1,
1993                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
1994
1995   /**
1996    * GtkWidget::window-state-event:
1997    * @widget: the object which received the signal
1998    * @event: the #GdkEventWindowState which triggered this signal
1999    *
2000    * The ::window-state-event will be emitted when the state of the 
2001    * toplevel window associated to the @widget changes.
2002    *
2003    * To receive this signal the #GdkWindow associated to the widget 
2004    * needs to enable the #GDK_STRUCTURE_MASK mask. GDK will enable 
2005    * this mask automatically for all new windows.
2006    *
2007    * Returns: %TRUE to stop other handlers from being invoked for the 
2008    *   event. %FALSE to propagate the event further.
2009    */
2010   widget_signals[WINDOW_STATE_EVENT] =
2011     g_signal_new (I_("window-state-event"),
2012                   G_TYPE_FROM_CLASS (gobject_class),
2013                   G_SIGNAL_RUN_LAST,
2014                   G_STRUCT_OFFSET (GtkWidgetClass, window_state_event),
2015                   _gtk_boolean_handled_accumulator, NULL,
2016                   _gtk_marshal_BOOLEAN__BOXED,
2017                   G_TYPE_BOOLEAN, 1,
2018                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2019
2020   /**
2021    * GtkWidget::damage-event:
2022    * @widget: the object which received the signal
2023    * @event: the #GdkEventExpose event
2024    *
2025    * Emitted when a redirected window belonging to @widget gets drawn into.
2026    * The region/area members of the event shows what area of the redirected
2027    * drawable was drawn into.
2028    *
2029    * Returns: %TRUE to stop other handlers from being invoked for the event.
2030    *   %FALSE to propagate the event further.
2031    *
2032    * Since: 2.14
2033    */
2034   widget_signals[DAMAGE_EVENT] =
2035     g_signal_new (I_("damage-event"),
2036                   G_TYPE_FROM_CLASS (gobject_class),
2037                   G_SIGNAL_RUN_LAST,
2038                   0,
2039                   _gtk_boolean_handled_accumulator, NULL,
2040                   _gtk_marshal_BOOLEAN__BOXED,
2041                   G_TYPE_BOOLEAN, 1,
2042                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2043 /**
2044    * GtkWidget::grab-broken-event:
2045    * @widget: the object which received the signal
2046    * @event: the #GdkEventGrabBroken event
2047    *
2048    * Emitted when a pointer or keyboard grab on a window belonging 
2049    * to @widget gets broken. 
2050    * 
2051    * On X11, this happens when the grab window becomes unviewable 
2052    * (i.e. it or one of its ancestors is unmapped), or if the same 
2053    * application grabs the pointer or keyboard again.
2054    *
2055    * Returns: %TRUE to stop other handlers from being invoked for 
2056    *   the event. %FALSE to propagate the event further.
2057    *
2058    * Since: 2.8
2059    */
2060   widget_signals[GRAB_BROKEN] =
2061     g_signal_new (I_("grab-broken-event"),
2062                   G_TYPE_FROM_CLASS (gobject_class),
2063                   G_SIGNAL_RUN_LAST,
2064                   G_STRUCT_OFFSET (GtkWidgetClass, grab_broken_event),
2065                   _gtk_boolean_handled_accumulator, NULL,
2066                   _gtk_marshal_BOOLEAN__BOXED,
2067                   G_TYPE_BOOLEAN, 1,
2068                   GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
2069   /**
2070    * GtkWidget::query-tooltip:
2071    * @widget: the object which received the signal
2072    * @x: the x coordinate of the cursor position where the request has 
2073    *     been emitted, relative to @widget->window
2074    * @y: the y coordinate of the cursor position where the request has 
2075    *     been emitted, relative to @widget->window
2076    * @keyboard_mode: %TRUE if the tooltip was trigged using the keyboard
2077    * @tooltip: a #GtkTooltip
2078    *
2079    * Emitted when #GtkWidget:has-tooltip is %TRUE and the #GtkSettings:gtk-tooltip-timeout 
2080    * has expired with the cursor hovering "above" @widget; or emitted when @widget got 
2081    * focus in keyboard mode.
2082    *
2083    * Using the given coordinates, the signal handler should determine
2084    * whether a tooltip should be shown for @widget. If this is the case
2085    * %TRUE should be returned, %FALSE otherwise.  Note that if
2086    * @keyboard_mode is %TRUE, the values of @x and @y are undefined and
2087    * should not be used.
2088    *
2089    * The signal handler is free to manipulate @tooltip with the therefore
2090    * destined function calls.
2091    *
2092    * Returns: %TRUE if @tooltip should be shown right now, %FALSE otherwise.
2093    *
2094    * Since: 2.12
2095    */
2096   widget_signals[QUERY_TOOLTIP] =
2097     g_signal_new (I_("query-tooltip"),
2098                   G_TYPE_FROM_CLASS (gobject_class),
2099                   G_SIGNAL_RUN_LAST,
2100                   G_STRUCT_OFFSET (GtkWidgetClass, query_tooltip),
2101                   _gtk_boolean_handled_accumulator, NULL,
2102                   _gtk_marshal_BOOLEAN__INT_INT_BOOLEAN_OBJECT,
2103                   G_TYPE_BOOLEAN, 4,
2104                   G_TYPE_INT,
2105                   G_TYPE_INT,
2106                   G_TYPE_BOOLEAN,
2107                   GTK_TYPE_TOOLTIP);
2108
2109   /**
2110    * GtkWidget::popup-menu
2111    * @widget: the object which received the signal
2112    *
2113    * This signal gets emitted whenever a widget should pop up a context 
2114    * menu. This usually happens through the standard key binding mechanism; 
2115    * by pressing a certain key while a widget is focused, the user can cause 
2116    * the widget to pop up a menu.  For example, the #GtkEntry widget creates 
2117    * a menu with clipboard commands. See <xref linkend="checklist-popup-menu"/> 
2118    * for an example of how to use this signal.
2119    *
2120    * Returns: %TRUE if a menu was activated
2121    */
2122   widget_signals[POPUP_MENU] =
2123     g_signal_new (I_("popup-menu"),
2124                   G_TYPE_FROM_CLASS (gobject_class),
2125                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
2126                   G_STRUCT_OFFSET (GtkWidgetClass, popup_menu),
2127                   _gtk_boolean_handled_accumulator, NULL,
2128                   _gtk_marshal_BOOLEAN__VOID,
2129                   G_TYPE_BOOLEAN, 0);
2130   widget_signals[SHOW_HELP] =
2131     g_signal_new (I_("show-help"),
2132                   G_TYPE_FROM_CLASS (gobject_class),
2133                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
2134                   G_STRUCT_OFFSET (GtkWidgetClass, show_help),
2135                   _gtk_boolean_handled_accumulator, NULL,
2136                   _gtk_marshal_BOOLEAN__ENUM,
2137                   G_TYPE_BOOLEAN, 1,
2138                   GTK_TYPE_WIDGET_HELP_TYPE);
2139   widget_signals[ACCEL_CLOSURES_CHANGED] =
2140     g_signal_new (I_("accel-closures-changed"),
2141                   G_TYPE_FROM_CLASS (gobject_class),
2142                   0,
2143                   0,
2144                   NULL, NULL,
2145                   _gtk_marshal_VOID__VOID,
2146                   G_TYPE_NONE, 0);
2147
2148   /**
2149    * GtkWidget::screen-changed:
2150    * @widget: the object on which the signal is emitted
2151    * @previous_screen: the previous screen, or %NULL if the
2152    *   widget was not associated with a screen before
2153    *
2154    * The ::screen-changed signal gets emitted when the
2155    * screen of a widget has changed.
2156    */
2157   widget_signals[SCREEN_CHANGED] =
2158     g_signal_new (I_("screen-changed"),
2159                   G_TYPE_FROM_CLASS (gobject_class),
2160                   G_SIGNAL_RUN_LAST,
2161                   G_STRUCT_OFFSET (GtkWidgetClass, screen_changed),
2162                   NULL, NULL,
2163                   _gtk_marshal_VOID__OBJECT,
2164                   G_TYPE_NONE, 1,
2165                   GDK_TYPE_SCREEN);
2166   /**
2167    * GtkWidget::can-activate-accel:
2168    * @widget: the object which received the signal
2169    * @signal_id: the ID of a signal installed on @widget
2170    *
2171    * Determines whether an accelerator that activates the signal
2172    * identified by @signal_id can currently be activated.
2173    * This signal is present to allow applications and derived
2174    * widgets to override the default #GtkWidget handling
2175    * for determining whether an accelerator can be activated.
2176    *
2177    * Returns: %TRUE if the signal can be activated.
2178    */
2179   widget_signals[CAN_ACTIVATE_ACCEL] =
2180      g_signal_new (I_("can-activate-accel"),
2181                   G_TYPE_FROM_CLASS (gobject_class),
2182                   G_SIGNAL_RUN_LAST,
2183                   G_STRUCT_OFFSET (GtkWidgetClass, can_activate_accel),
2184                   _gtk_boolean_handled_accumulator, NULL,
2185                   _gtk_marshal_BOOLEAN__UINT,
2186                   G_TYPE_BOOLEAN, 1, G_TYPE_UINT);
2187
2188   binding_set = gtk_binding_set_by_class (klass);
2189   gtk_binding_entry_add_signal (binding_set, GDK_F10, GDK_SHIFT_MASK,
2190                                 "popup-menu", 0);
2191   gtk_binding_entry_add_signal (binding_set, GDK_Menu, 0,
2192                                 "popup-menu", 0);  
2193
2194   gtk_binding_entry_add_signal (binding_set, GDK_F1, GDK_CONTROL_MASK,
2195                                 "show-help", 1,
2196                                 GTK_TYPE_WIDGET_HELP_TYPE,
2197                                 GTK_WIDGET_HELP_TOOLTIP);
2198   gtk_binding_entry_add_signal (binding_set, GDK_KP_F1, GDK_CONTROL_MASK,
2199                                 "show-help", 1,
2200                                 GTK_TYPE_WIDGET_HELP_TYPE,
2201                                 GTK_WIDGET_HELP_TOOLTIP);
2202   gtk_binding_entry_add_signal (binding_set, GDK_F1, GDK_SHIFT_MASK,
2203                                 "show-help", 1,
2204                                 GTK_TYPE_WIDGET_HELP_TYPE,
2205                                 GTK_WIDGET_HELP_WHATS_THIS);  
2206   gtk_binding_entry_add_signal (binding_set, GDK_KP_F1, GDK_SHIFT_MASK,
2207                                 "show-help", 1,
2208                                 GTK_TYPE_WIDGET_HELP_TYPE,
2209                                 GTK_WIDGET_HELP_WHATS_THIS);
2210
2211   gtk_widget_class_install_style_property (klass,
2212                                            g_param_spec_boolean ("interior-focus",
2213                                                                  P_("Interior Focus"),
2214                                                                  P_("Whether to draw the focus indicator inside widgets"),
2215                                                                  TRUE,
2216                                                                  GTK_PARAM_READABLE));
2217
2218   gtk_widget_class_install_style_property (klass,
2219                                            g_param_spec_int ("focus-line-width",
2220                                                              P_("Focus linewidth"),
2221                                                              P_("Width, in pixels, of the focus indicator line"),
2222                                                              0, G_MAXINT, 1,
2223                                                              GTK_PARAM_READABLE));
2224
2225   gtk_widget_class_install_style_property (klass,
2226                                            g_param_spec_string ("focus-line-pattern",
2227                                                                 P_("Focus line dash pattern"),
2228                                                                 P_("Dash pattern used to draw the focus indicator"),
2229                                                                 "\1\1",
2230                                                                 GTK_PARAM_READABLE));
2231   gtk_widget_class_install_style_property (klass,
2232                                            g_param_spec_int ("focus-padding",
2233                                                              P_("Focus padding"),
2234                                                              P_("Width, in pixels, between focus indicator and the widget 'box'"),
2235                                                              0, G_MAXINT, 1,
2236                                                              GTK_PARAM_READABLE));
2237   gtk_widget_class_install_style_property (klass,
2238                                            g_param_spec_boxed ("cursor-color",
2239                                                                P_("Cursor color"),
2240                                                                P_("Color with which to draw insertion cursor"),
2241                                                                GDK_TYPE_COLOR,
2242                                                                GTK_PARAM_READABLE));
2243   gtk_widget_class_install_style_property (klass,
2244                                            g_param_spec_boxed ("secondary-cursor-color",
2245                                                                P_("Secondary cursor color"),
2246                                                                P_("Color with which to draw the secondary insertion cursor when editing mixed right-to-left and left-to-right text"),
2247                                                                GDK_TYPE_COLOR,
2248                                                                GTK_PARAM_READABLE));
2249   gtk_widget_class_install_style_property (klass,
2250                                            g_param_spec_float ("cursor-aspect-ratio",
2251                                                                P_("Cursor line aspect ratio"),
2252                                                                P_("Aspect ratio with which to draw insertion cursor"),
2253                                                                0.0, 1.0, 0.04,
2254                                                                GTK_PARAM_READABLE));
2255
2256   /**
2257    * GtkWidget:draw-border:
2258    *
2259    * The "draw-border" style property defines the size of areas outside 
2260    * the widget's allocation to draw.
2261    *
2262    * Since: 2.8
2263    */
2264   gtk_widget_class_install_style_property (klass,
2265                                            g_param_spec_boxed ("draw-border",
2266                                                                P_("Draw Border"),
2267                                                                P_("Size of areas outside the widget's allocation to draw"),
2268                                                                GTK_TYPE_BORDER,
2269                                                                GTK_PARAM_READABLE));
2270
2271   /**
2272    * GtkWidget:link-color:
2273    *
2274    * The "link-color" style property defines the color of unvisited links.
2275    *
2276    * Since: 2.10
2277    */
2278   gtk_widget_class_install_style_property (klass,
2279                                            g_param_spec_boxed ("link-color",
2280                                                                P_("Unvisited Link Color"),
2281                                                                P_("Color of unvisited links"),
2282                                                                GDK_TYPE_COLOR,
2283                                                                GTK_PARAM_READABLE));
2284
2285   /**
2286    * GtkWidget:visited-link-color:
2287    *
2288    * The "visited-link-color" style property defines the color of visited links.
2289    *
2290    * Since: 2.10
2291    */
2292   gtk_widget_class_install_style_property (klass,
2293                                            g_param_spec_boxed ("visited-link-color",
2294                                                                P_("Visited Link Color"),
2295                                                                P_("Color of visited links"),
2296                                                                GDK_TYPE_COLOR,
2297                                                                GTK_PARAM_READABLE));
2298
2299   /**
2300    * GtkWidget:wide-separators:
2301    *
2302    * The "wide-separators" style property defines whether separators have 
2303    * configurable width and should be drawn using a box instead of a line.
2304    *
2305    * Since: 2.10
2306    */
2307   gtk_widget_class_install_style_property (klass,
2308                                            g_param_spec_boolean ("wide-separators",
2309                                                                  P_("Wide Separators"),
2310                                                                  P_("Whether separators have configurable width and should be drawn using a box instead of a line"),
2311                                                                  FALSE,
2312                                                                  GTK_PARAM_READABLE));
2313
2314   /**
2315    * GtkWidget:separator-width:
2316    *
2317    * The "separator-width" style property defines the width of separators.
2318    * This property only takes effect if #GtkWidget:wide-separators is %TRUE.
2319    *
2320    * Since: 2.10
2321    */
2322   gtk_widget_class_install_style_property (klass,
2323                                            g_param_spec_int ("separator-width",
2324                                                              P_("Separator Width"),
2325                                                              P_("The width of separators if wide-separators is TRUE"),
2326                                                              0, G_MAXINT, 0,
2327                                                              GTK_PARAM_READABLE));
2328
2329   /**
2330    * GtkWidget:separator-height:
2331    *
2332    * The "separator-height" style property defines the height of separators.
2333    * This property only takes effect if #GtkWidget:wide-separators is %TRUE.
2334    *
2335    * Since: 2.10
2336    */
2337   gtk_widget_class_install_style_property (klass,
2338                                            g_param_spec_int ("separator-height",
2339                                                              P_("Separator Height"),
2340                                                              P_("The height of separators if \"wide-separators\" is TRUE"),
2341                                                              0, G_MAXINT, 0,
2342                                                              GTK_PARAM_READABLE));
2343
2344   /**
2345    * GtkWidget:scroll-arrow-hlength:
2346    *
2347    * The "scroll-arrow-hlength" style property defines the length of 
2348    * horizontal scroll arrows.
2349    *
2350    * Since: 2.10
2351    */
2352   gtk_widget_class_install_style_property (klass,
2353                                            g_param_spec_int ("scroll-arrow-hlength",
2354                                                              P_("Horizontal Scroll Arrow Length"),
2355                                                              P_("The length of horizontal scroll arrows"),
2356                                                              1, G_MAXINT, 16,
2357                                                              GTK_PARAM_READABLE));
2358
2359   /**
2360    * GtkWidget:scroll-arrow-vlength:
2361    *
2362    * The "scroll-arrow-vlength" style property defines the length of 
2363    * vertical scroll arrows.
2364    *
2365    * Since: 2.10
2366    */
2367   gtk_widget_class_install_style_property (klass,
2368                                            g_param_spec_int ("scroll-arrow-vlength",
2369                                                              P_("Vertical Scroll Arrow Length"),
2370                                                              P_("The length of vertical scroll arrows"),
2371                                                              1, G_MAXINT, 16,
2372                                                              GTK_PARAM_READABLE));
2373 }
2374
2375 static void
2376 gtk_widget_base_class_finalize (GtkWidgetClass *klass)
2377 {
2378   GList *list, *node;
2379
2380   list = g_param_spec_pool_list_owned (style_property_spec_pool, G_OBJECT_CLASS_TYPE (klass));
2381   for (node = list; node; node = node->next)
2382     {
2383       GParamSpec *pspec = node->data;
2384
2385       g_param_spec_pool_remove (style_property_spec_pool, pspec);
2386       g_param_spec_unref (pspec);
2387     }
2388   g_list_free (list);
2389 }
2390
2391 static void
2392 gtk_widget_set_property (GObject         *object,
2393                          guint            prop_id,
2394                          const GValue    *value,
2395                          GParamSpec      *pspec)
2396 {
2397   GtkWidget *widget = GTK_WIDGET (object);
2398
2399   switch (prop_id)
2400     {
2401       gboolean tmp;
2402       guint32 saved_flags;
2403       gchar *tooltip_markup;
2404       const gchar *tooltip_text;
2405       GtkWindow *tooltip_window;
2406       
2407     case PROP_NAME:
2408       gtk_widget_set_name (widget, g_value_get_string (value));
2409       break;
2410     case PROP_PARENT:
2411       gtk_container_add (GTK_CONTAINER (g_value_get_object (value)), widget);
2412       break;
2413     case PROP_WIDTH_REQUEST:
2414       gtk_widget_set_usize_internal (widget, g_value_get_int (value), -2);
2415       break;
2416     case PROP_HEIGHT_REQUEST:
2417       gtk_widget_set_usize_internal (widget, -2, g_value_get_int (value));
2418       break;
2419     case PROP_VISIBLE:
2420       if (g_value_get_boolean (value))
2421         gtk_widget_show (widget);
2422       else
2423         gtk_widget_hide (widget);
2424       break;
2425     case PROP_SENSITIVE:
2426       gtk_widget_set_sensitive (widget, g_value_get_boolean (value));
2427       break;
2428     case PROP_APP_PAINTABLE:
2429       gtk_widget_set_app_paintable (widget, g_value_get_boolean (value));
2430       break;
2431     case PROP_CAN_FOCUS:
2432       saved_flags = GTK_WIDGET_FLAGS (widget);
2433       if (g_value_get_boolean (value))
2434         GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
2435       else
2436         GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
2437       if (saved_flags != GTK_WIDGET_FLAGS (widget))
2438         gtk_widget_queue_resize (widget);
2439       break;
2440     case PROP_HAS_FOCUS:
2441       if (g_value_get_boolean (value))
2442         gtk_widget_grab_focus (widget);
2443       break;
2444     case PROP_IS_FOCUS:
2445       if (g_value_get_boolean (value))
2446         gtk_widget_grab_focus (widget);
2447       break;
2448     case PROP_CAN_DEFAULT:
2449       saved_flags = GTK_WIDGET_FLAGS (widget);
2450       if (g_value_get_boolean (value))
2451         GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_DEFAULT);
2452       else
2453         GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_DEFAULT);
2454       if (saved_flags != GTK_WIDGET_FLAGS (widget))
2455         gtk_widget_queue_resize (widget);
2456       break;
2457     case PROP_HAS_DEFAULT:
2458       if (g_value_get_boolean (value))
2459         gtk_widget_grab_default (widget);
2460       break;
2461     case PROP_RECEIVES_DEFAULT:
2462       if (g_value_get_boolean (value))
2463         GTK_WIDGET_SET_FLAGS (widget, GTK_RECEIVES_DEFAULT);
2464       else
2465         GTK_WIDGET_UNSET_FLAGS (widget, GTK_RECEIVES_DEFAULT);
2466       break;
2467     case PROP_STYLE:
2468       gtk_widget_set_style (widget, g_value_get_object (value));
2469       break;
2470     case PROP_EVENTS:
2471       if (!GTK_WIDGET_REALIZED (widget) && !GTK_WIDGET_NO_WINDOW (widget))
2472         gtk_widget_set_events (widget, g_value_get_flags (value));
2473       break;
2474     case PROP_EXTENSION_EVENTS:
2475       gtk_widget_set_extension_events (widget, g_value_get_enum (value));
2476       break;
2477     case PROP_NO_SHOW_ALL:
2478       gtk_widget_set_no_show_all (widget, g_value_get_boolean (value));
2479       break;
2480     case PROP_HAS_TOOLTIP:
2481       gtk_widget_real_set_has_tooltip (widget,
2482                                        g_value_get_boolean (value), FALSE);
2483       break;
2484     case PROP_TOOLTIP_MARKUP:
2485       tooltip_window = g_object_get_qdata (object, quark_tooltip_window);
2486       tooltip_markup = g_value_dup_string (value);
2487
2488       /* Treat an empty string as a NULL string, 
2489        * because an empty string would be useless for a tooltip:
2490        */
2491       if (tooltip_markup && (strlen (tooltip_markup) == 0))
2492         {
2493           g_free (tooltip_markup);
2494           tooltip_markup = NULL;
2495         }
2496
2497       g_object_set_qdata_full (object, quark_tooltip_markup,
2498                                tooltip_markup, g_free);
2499
2500       tmp = (tooltip_window != NULL || tooltip_markup != NULL);
2501       gtk_widget_real_set_has_tooltip (widget, tmp, FALSE);
2502       if (GTK_WIDGET_VISIBLE (widget))
2503         gtk_widget_queue_tooltip_query (widget);
2504       break;
2505     case PROP_TOOLTIP_TEXT:
2506       tooltip_window = g_object_get_qdata (object, quark_tooltip_window);
2507
2508       tooltip_text = g_value_get_string (value);
2509
2510       /* Treat an empty string as a NULL string, 
2511        * because an empty string would be useless for a tooltip:
2512        */
2513       if (tooltip_text && (strlen (tooltip_text) == 0))
2514         tooltip_text = NULL;
2515
2516       tooltip_markup = tooltip_text ? g_markup_escape_text (tooltip_text, -1) : NULL;
2517
2518       g_object_set_qdata_full (object, quark_tooltip_markup,
2519                                tooltip_markup, g_free);
2520
2521       tmp = (tooltip_window != NULL || tooltip_markup != NULL);
2522       gtk_widget_real_set_has_tooltip (widget, tmp, FALSE);
2523       if (GTK_WIDGET_VISIBLE (widget))
2524         gtk_widget_queue_tooltip_query (widget);
2525       break;
2526     default:
2527       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2528       break;
2529     }
2530 }
2531
2532 static void
2533 gtk_widget_get_property (GObject         *object,
2534                          guint            prop_id,
2535                          GValue          *value,
2536                          GParamSpec      *pspec)
2537 {
2538   GtkWidget *widget = GTK_WIDGET (object);
2539   
2540   switch (prop_id)
2541     {
2542       gpointer *eventp;
2543       gpointer *modep;
2544
2545     case PROP_NAME:
2546       if (widget->name)
2547         g_value_set_string (value, widget->name);
2548       else
2549         g_value_set_static_string (value, "");
2550       break;
2551     case PROP_PARENT:
2552       g_value_set_object (value, widget->parent);
2553       break;
2554     case PROP_WIDTH_REQUEST:
2555       {
2556         int w;
2557         gtk_widget_get_size_request (widget, &w, NULL);
2558         g_value_set_int (value, w);
2559       }
2560       break;
2561     case PROP_HEIGHT_REQUEST:
2562       {
2563         int h;
2564         gtk_widget_get_size_request (widget, NULL, &h);
2565         g_value_set_int (value, h);
2566       }
2567       break;
2568     case PROP_VISIBLE:
2569       g_value_set_boolean (value, (GTK_WIDGET_VISIBLE (widget) != FALSE));
2570       break;
2571     case PROP_SENSITIVE:
2572       g_value_set_boolean (value, (GTK_WIDGET_SENSITIVE (widget) != FALSE));
2573       break;
2574     case PROP_APP_PAINTABLE:
2575       g_value_set_boolean (value, (GTK_WIDGET_APP_PAINTABLE (widget) != FALSE));
2576       break;
2577     case PROP_CAN_FOCUS:
2578       g_value_set_boolean (value, (GTK_WIDGET_CAN_FOCUS (widget) != FALSE));
2579       break;
2580     case PROP_HAS_FOCUS:
2581       g_value_set_boolean (value, (GTK_WIDGET_HAS_FOCUS (widget) != FALSE));
2582       break;
2583     case PROP_IS_FOCUS:
2584       g_value_set_boolean (value, (gtk_widget_is_focus (widget)));
2585       break;
2586     case PROP_CAN_DEFAULT:
2587       g_value_set_boolean (value, (GTK_WIDGET_CAN_DEFAULT (widget) != FALSE));
2588       break;
2589     case PROP_HAS_DEFAULT:
2590       g_value_set_boolean (value, (GTK_WIDGET_HAS_DEFAULT (widget) != FALSE));
2591       break;
2592     case PROP_RECEIVES_DEFAULT:
2593       g_value_set_boolean (value, (GTK_WIDGET_RECEIVES_DEFAULT (widget) != FALSE));
2594       break;
2595     case PROP_COMPOSITE_CHILD:
2596       g_value_set_boolean (value, (GTK_WIDGET_COMPOSITE_CHILD (widget) != FALSE));
2597       break;
2598     case PROP_STYLE:
2599       g_value_set_object (value, gtk_widget_get_style (widget));
2600       break;
2601     case PROP_EVENTS:
2602       eventp = g_object_get_qdata (G_OBJECT (widget), quark_event_mask);
2603       g_value_set_flags (value, GPOINTER_TO_INT (eventp));
2604       break;
2605     case PROP_EXTENSION_EVENTS:
2606       modep = g_object_get_qdata (G_OBJECT (widget), quark_extension_event_mode);
2607       g_value_set_enum (value, GPOINTER_TO_INT (modep));
2608       break;
2609     case PROP_NO_SHOW_ALL:
2610       g_value_set_boolean (value, gtk_widget_get_no_show_all (widget));
2611       break;
2612     case PROP_HAS_TOOLTIP:
2613       g_value_set_boolean (value, GPOINTER_TO_UINT (g_object_get_qdata (object, quark_has_tooltip)));
2614       break;
2615     case PROP_TOOLTIP_TEXT:
2616       {
2617         gchar *escaped = g_object_get_qdata (object, quark_tooltip_markup);
2618         gchar *text = NULL;
2619
2620         if (escaped && !pango_parse_markup (escaped, -1, 0, NULL, &text, NULL, NULL))
2621           g_assert (NULL == text); /* text should still be NULL in case of markup errors */
2622
2623         g_value_set_string (value, text);
2624         g_free (text);
2625       }
2626       break;
2627     case PROP_TOOLTIP_MARKUP:
2628       g_value_set_string (value, g_object_get_qdata (object, quark_tooltip_markup));
2629       break;
2630     case PROP_WINDOW:
2631       g_value_set_object (value, gtk_widget_get_window (widget));
2632       break;
2633     default:
2634       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
2635       break;
2636     }
2637 }
2638
2639 static void
2640 gtk_widget_init (GtkWidget *widget)
2641 {
2642   GTK_PRIVATE_FLAGS (widget) = PRIVATE_GTK_CHILD_VISIBLE;
2643   widget->state = GTK_STATE_NORMAL;
2644   widget->saved_state = GTK_STATE_NORMAL;
2645   widget->name = NULL;
2646   widget->requisition.width = 0;
2647   widget->requisition.height = 0;
2648   widget->allocation.x = -1;
2649   widget->allocation.y = -1;
2650   widget->allocation.width = 1;
2651   widget->allocation.height = 1;
2652   widget->window = NULL;
2653   widget->parent = NULL;
2654
2655   GTK_WIDGET_SET_FLAGS (widget,
2656                         GTK_SENSITIVE |
2657                         GTK_PARENT_SENSITIVE |
2658                         (composite_child_stack ? GTK_COMPOSITE_CHILD : 0) |
2659                         GTK_DOUBLE_BUFFERED);
2660
2661   GTK_PRIVATE_SET_FLAG (widget, GTK_REDRAW_ON_ALLOC);
2662   GTK_PRIVATE_SET_FLAG (widget, GTK_REQUEST_NEEDED);
2663   GTK_PRIVATE_SET_FLAG (widget, GTK_ALLOC_NEEDED);
2664
2665   widget->style = gtk_widget_get_default_style ();
2666   g_object_ref (widget->style);
2667 }
2668
2669
2670 static void
2671 gtk_widget_dispatch_child_properties_changed (GtkWidget   *widget,
2672                                               guint        n_pspecs,
2673                                               GParamSpec **pspecs)
2674 {
2675   GtkWidget *container = widget->parent;
2676   guint i;
2677
2678   for (i = 0; widget->parent == container && i < n_pspecs; i++)
2679     g_signal_emit (widget, widget_signals[CHILD_NOTIFY], g_quark_from_string (pspecs[i]->name), pspecs[i]);
2680 }
2681
2682 /**
2683  * gtk_widget_freeze_child_notify:
2684  * @widget: a #GtkWidget
2685  * 
2686  * Stops emission of #GtkWidget::child-notify signals on @widget. The 
2687  * signals are queued until gtk_widget_thaw_child_notify() is called 
2688  * on @widget. 
2689  *
2690  * This is the analogue of g_object_freeze_notify() for child properties.
2691  **/
2692 void
2693 gtk_widget_freeze_child_notify (GtkWidget *widget)
2694 {
2695   g_return_if_fail (GTK_IS_WIDGET (widget));
2696
2697   if (!G_OBJECT (widget)->ref_count)
2698     return;
2699
2700   g_object_ref (widget);
2701   g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
2702   g_object_unref (widget);
2703 }
2704
2705 /**
2706  * gtk_widget_child_notify:
2707  * @widget: a #GtkWidget
2708  * @child_property: the name of a child property installed on the 
2709  *                  class of @widget<!-- -->'s parent
2710  * 
2711  * Emits a #GtkWidget::child-notify signal for the 
2712  * <link linkend="child-properties">child property</link> @child_property 
2713  * on @widget.
2714  *
2715  * This is the analogue of g_object_notify() for child properties.
2716  **/
2717 void
2718 gtk_widget_child_notify (GtkWidget    *widget,
2719                          const gchar  *child_property)
2720 {
2721   GParamSpec *pspec;
2722
2723   g_return_if_fail (GTK_IS_WIDGET (widget));
2724   g_return_if_fail (child_property != NULL);
2725   if (!G_OBJECT (widget)->ref_count || !widget->parent)
2726     return;
2727
2728   g_object_ref (widget);
2729   pspec = g_param_spec_pool_lookup (_gtk_widget_child_property_pool,
2730                                     child_property,
2731                                     G_OBJECT_TYPE (widget->parent),
2732                                     TRUE);
2733   if (!pspec)
2734     g_warning ("%s: container class `%s' has no child property named `%s'",
2735                G_STRLOC,
2736                G_OBJECT_TYPE_NAME (widget->parent),
2737                child_property);
2738   else
2739     {
2740       GObjectNotifyQueue *nqueue = g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
2741
2742       g_object_notify_queue_add (G_OBJECT (widget), nqueue, pspec);
2743       g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
2744     }
2745   g_object_unref (widget);
2746 }
2747
2748 /**
2749  * gtk_widget_thaw_child_notify:
2750  * @widget: a #GtkWidget
2751  *
2752  * Reverts the effect of a previous call to gtk_widget_freeze_child_notify().
2753  * This causes all queued #GtkWidget::child-notify signals on @widget to be 
2754  * emitted.
2755  */ 
2756 void
2757 gtk_widget_thaw_child_notify (GtkWidget *widget)
2758 {
2759   GObjectNotifyQueue *nqueue;
2760
2761   g_return_if_fail (GTK_IS_WIDGET (widget));
2762
2763   if (!G_OBJECT (widget)->ref_count)
2764     return;
2765
2766   g_object_ref (widget);
2767   nqueue = g_object_notify_queue_from_object (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
2768   if (!nqueue || !nqueue->freeze_count)
2769     g_warning (G_STRLOC ": child-property-changed notification for %s(%p) is not frozen",
2770                G_OBJECT_TYPE_NAME (widget), widget);
2771   else
2772     g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
2773   g_object_unref (widget);
2774 }
2775
2776
2777 /**
2778  * gtk_widget_new:
2779  * @type: type ID of the widget to create
2780  * @first_property_name: name of first property to set
2781  * @Varargs: value of first property, followed by more properties, 
2782  *           %NULL-terminated
2783  * 
2784  * This is a convenience function for creating a widget and setting
2785  * its properties in one go. For example you might write:
2786  * <literal>gtk_widget_new (GTK_TYPE_LABEL, "label", "Hello World", "xalign",
2787  * 0.0, NULL)</literal> to create a left-aligned label. Equivalent to
2788  * g_object_new(), but returns a widget so you don't have to
2789  * cast the object yourself.
2790  * 
2791  * Return value: a new #GtkWidget of type @widget_type
2792  **/
2793 GtkWidget*
2794 gtk_widget_new (GType        type,
2795                 const gchar *first_property_name,
2796                 ...)
2797 {
2798   GtkWidget *widget;
2799   va_list var_args;
2800   
2801   g_return_val_if_fail (g_type_is_a (type, GTK_TYPE_WIDGET), NULL);
2802   
2803   va_start (var_args, first_property_name);
2804   widget = (GtkWidget *)g_object_new_valist (type, first_property_name, var_args);
2805   va_end (var_args);
2806
2807   return widget;
2808 }
2809
2810 /**
2811  * gtk_widget_set:
2812  * @widget: a #GtkWidget
2813  * @first_property_name: name of first property to set
2814  * @Varargs: value of first property, followed by more properties, 
2815  *           %NULL-terminated
2816  * 
2817  * Precursor of g_object_set().
2818  *
2819  * Deprecated: 2.0: Use g_object_set() instead.
2820  **/
2821 void
2822 gtk_widget_set (GtkWidget   *widget,
2823                 const gchar *first_property_name,
2824                 ...)
2825 {
2826   va_list var_args;
2827
2828   g_return_if_fail (GTK_IS_WIDGET (widget));
2829
2830   va_start (var_args, first_property_name);
2831   g_object_set_valist (G_OBJECT (widget), first_property_name, var_args);
2832   va_end (var_args);
2833 }
2834
2835 static inline void         
2836 gtk_widget_queue_draw_child (GtkWidget *widget)
2837 {
2838   GtkWidget *parent;
2839
2840   parent = widget->parent;
2841   if (parent && GTK_WIDGET_DRAWABLE (parent))
2842     gtk_widget_queue_draw_area (parent,
2843                                 widget->allocation.x,
2844                                 widget->allocation.y,
2845                                 widget->allocation.width,
2846                                 widget->allocation.height);
2847 }
2848
2849 /**
2850  * gtk_widget_unparent:
2851  * @widget: a #GtkWidget
2852  * 
2853  * This function is only for use in widget implementations.
2854  * Should be called by implementations of the remove method
2855  * on #GtkContainer, to dissociate a child from the container.
2856  **/
2857 void
2858 gtk_widget_unparent (GtkWidget *widget)
2859 {
2860   GObjectNotifyQueue *nqueue;
2861   GtkWidget *toplevel;
2862   GtkWidget *old_parent;
2863   
2864   g_return_if_fail (GTK_IS_WIDGET (widget));
2865   if (widget->parent == NULL)
2866     return;
2867   
2868   /* keep this function in sync with gtk_menu_detach()
2869    */
2870
2871   g_object_freeze_notify (G_OBJECT (widget));
2872   nqueue = g_object_notify_queue_freeze (G_OBJECT (widget), _gtk_widget_child_property_notify_context);
2873
2874   toplevel = gtk_widget_get_toplevel (widget);
2875   if (GTK_WIDGET_TOPLEVEL (toplevel))
2876     _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
2877
2878   if (GTK_CONTAINER (widget->parent)->focus_child == widget)
2879     gtk_container_set_focus_child (GTK_CONTAINER (widget->parent), NULL);
2880
2881   /* If we are unanchoring the child, we save around the toplevel
2882    * to emit hierarchy changed
2883    */
2884   if (GTK_WIDGET_ANCHORED (widget->parent))
2885     g_object_ref (toplevel);
2886   else
2887     toplevel = NULL;
2888
2889   gtk_widget_queue_draw_child (widget);
2890
2891   /* Reset the width and height here, to force reallocation if we
2892    * get added back to a new parent. This won't work if our new
2893    * allocation is smaller than 1x1 and we actually want a size of 1x1...
2894    * (would 0x0 be OK here?)
2895    */
2896   widget->allocation.width = 1;
2897   widget->allocation.height = 1;
2898   
2899   if (GTK_WIDGET_REALIZED (widget)) 
2900     {
2901       if (GTK_WIDGET_IN_REPARENT (widget))
2902         gtk_widget_unmap (widget);
2903       else
2904         gtk_widget_unrealize (widget);
2905     }
2906
2907   /* Removing a widget from a container restores the child visible
2908    * flag to the default state, so it doesn't affect the child
2909    * in the next parent.
2910    */
2911   GTK_PRIVATE_SET_FLAG (widget, GTK_CHILD_VISIBLE);
2912     
2913   old_parent = widget->parent;
2914   widget->parent = NULL;
2915   gtk_widget_set_parent_window (widget, NULL);
2916   g_signal_emit (widget, widget_signals[PARENT_SET], 0, old_parent);
2917   if (toplevel)
2918     {
2919       _gtk_widget_propagate_hierarchy_changed (widget, toplevel);
2920       g_object_unref (toplevel);
2921     }
2922       
2923   g_object_notify (G_OBJECT (widget), "parent");
2924   g_object_thaw_notify (G_OBJECT (widget));
2925   if (!widget->parent)
2926     g_object_notify_queue_clear (G_OBJECT (widget), nqueue);
2927   g_object_notify_queue_thaw (G_OBJECT (widget), nqueue);
2928   g_object_unref (widget);
2929 }
2930
2931 /**
2932  * gtk_widget_destroy:
2933  * @widget: a #GtkWidget
2934  *
2935  * Destroys a widget. Equivalent to gtk_object_destroy(), except that
2936  * you don't have to cast the widget to #GtkObject. When a widget is
2937  * destroyed, it will break any references it holds to other objects.
2938  * If the widget is inside a container, the widget will be removed
2939  * from the container. If the widget is a toplevel (derived from
2940  * #GtkWindow), it will be removed from the list of toplevels, and the
2941  * reference GTK+ holds to it will be removed. Removing a
2942  * widget from its container or the list of toplevels results in the
2943  * widget being finalized, unless you've added additional references
2944  * to the widget with g_object_ref().
2945  *
2946  * In most cases, only toplevel widgets (windows) require explicit
2947  * destruction, because when you destroy a toplevel its children will
2948  * be destroyed as well.
2949  **/
2950 void
2951 gtk_widget_destroy (GtkWidget *widget)
2952 {
2953   g_return_if_fail (GTK_IS_WIDGET (widget));
2954
2955   gtk_object_destroy ((GtkObject*) widget);
2956 }
2957
2958 /**
2959  * gtk_widget_destroyed:
2960  * @widget: a #GtkWidget
2961  * @widget_pointer: address of a variable that contains @widget
2962  *
2963  * This function sets *@widget_pointer to %NULL if @widget_pointer !=
2964  * %NULL.  It's intended to be used as a callback connected to the
2965  * "destroy" signal of a widget. You connect gtk_widget_destroyed()
2966  * as a signal handler, and pass the address of your widget variable
2967  * as user data. Then when the widget is destroyed, the variable will
2968  * be set to %NULL. Useful for example to avoid multiple copies
2969  * of the same dialog.
2970  **/
2971 void
2972 gtk_widget_destroyed (GtkWidget      *widget,
2973                       GtkWidget      **widget_pointer)
2974 {
2975   /* Don't make any assumptions about the
2976    *  value of widget!
2977    *  Even check widget_pointer.
2978    */
2979   if (widget_pointer)
2980     *widget_pointer = NULL;
2981 }
2982
2983 /**
2984  * gtk_widget_show:
2985  * @widget: a #GtkWidget
2986  * 
2987  * Flags a widget to be displayed. Any widget that isn't shown will
2988  * not appear on the screen. If you want to show all the widgets in a
2989  * container, it's easier to call gtk_widget_show_all() on the
2990  * container, instead of individually showing the widgets.
2991  *
2992  * Remember that you have to show the containers containing a widget,
2993  * in addition to the widget itself, before it will appear onscreen.
2994  *
2995  * When a toplevel container is shown, it is immediately realized and
2996  * mapped; other shown widgets are realized and mapped when their
2997  * toplevel container is realized and mapped.
2998  **/
2999 void
3000 gtk_widget_show (GtkWidget *widget)
3001 {
3002   g_return_if_fail (GTK_IS_WIDGET (widget));
3003
3004   if (!GTK_WIDGET_VISIBLE (widget))
3005     {
3006       g_object_ref (widget);
3007       if (!GTK_WIDGET_TOPLEVEL (widget))
3008         gtk_widget_queue_resize (widget);
3009       g_signal_emit (widget, widget_signals[SHOW], 0);
3010       g_object_notify (G_OBJECT (widget), "visible");
3011       g_object_unref (widget);
3012     }
3013 }
3014
3015 static void
3016 gtk_widget_real_show (GtkWidget *widget)
3017 {
3018   if (!GTK_WIDGET_VISIBLE (widget))
3019     {
3020       GTK_WIDGET_SET_FLAGS (widget, GTK_VISIBLE);
3021
3022       if (widget->parent &&
3023           GTK_WIDGET_MAPPED (widget->parent) &&
3024           GTK_WIDGET_CHILD_VISIBLE (widget) &&
3025           !GTK_WIDGET_MAPPED (widget))
3026         gtk_widget_map (widget);
3027     }
3028 }
3029
3030 static void
3031 gtk_widget_show_map_callback (GtkWidget *widget, GdkEvent *event, gint *flag)
3032 {
3033   *flag = TRUE;
3034   g_signal_handlers_disconnect_by_func (widget,
3035                                         gtk_widget_show_map_callback, 
3036                                         flag);
3037 }
3038
3039 /**
3040  * gtk_widget_show_now:
3041  * @widget: a #GtkWidget
3042  * 
3043  * Shows a widget. If the widget is an unmapped toplevel widget
3044  * (i.e. a #GtkWindow that has not yet been shown), enter the main
3045  * loop and wait for the window to actually be mapped. Be careful;
3046  * because the main loop is running, anything can happen during
3047  * this function.
3048  **/
3049 void
3050 gtk_widget_show_now (GtkWidget *widget)
3051 {
3052   gint flag = FALSE;
3053   
3054   g_return_if_fail (GTK_IS_WIDGET (widget));
3055
3056   /* make sure we will get event */
3057   if (!GTK_WIDGET_MAPPED (widget) &&
3058       GTK_WIDGET_TOPLEVEL (widget))
3059     {
3060       gtk_widget_show (widget);
3061
3062       g_signal_connect (widget, "map-event",
3063                         G_CALLBACK (gtk_widget_show_map_callback), 
3064                         &flag);
3065
3066       while (!flag)
3067         gtk_main_iteration ();
3068     }
3069   else
3070     gtk_widget_show (widget);
3071 }
3072
3073 /**
3074  * gtk_widget_hide:
3075  * @widget: a #GtkWidget
3076  * 
3077  * Reverses the effects of gtk_widget_show(), causing the widget to be
3078  * hidden (invisible to the user).
3079  **/
3080 void
3081 gtk_widget_hide (GtkWidget *widget)
3082 {
3083   g_return_if_fail (GTK_IS_WIDGET (widget));
3084   
3085   if (GTK_WIDGET_VISIBLE (widget))
3086     {
3087       GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
3088       
3089       g_object_ref (widget);
3090       if (toplevel != widget && GTK_WIDGET_TOPLEVEL (toplevel))
3091         _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
3092
3093       g_signal_emit (widget, widget_signals[HIDE], 0);
3094       if (!GTK_WIDGET_TOPLEVEL (widget))
3095         gtk_widget_queue_resize (widget);
3096       g_object_notify (G_OBJECT (widget), "visible");
3097       g_object_unref (widget);
3098     }
3099 }
3100
3101 static void
3102 gtk_widget_real_hide (GtkWidget *widget)
3103 {
3104   if (GTK_WIDGET_VISIBLE (widget))
3105     {
3106       GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
3107       
3108       if (GTK_WIDGET_MAPPED (widget))
3109         gtk_widget_unmap (widget);
3110     }
3111 }
3112
3113 /**
3114  * gtk_widget_hide_on_delete:
3115  * @widget: a #GtkWidget
3116  * 
3117  * Utility function; intended to be connected to the #GtkWidget::delete-event
3118  * signal on a #GtkWindow. The function calls gtk_widget_hide() on its
3119  * argument, then returns %TRUE. If connected to ::delete-event, the
3120  * result is that clicking the close button for a window (on the
3121  * window frame, top right corner usually) will hide but not destroy
3122  * the window. By default, GTK+ destroys windows when ::delete-event
3123  * is received.
3124  * 
3125  * Return value: %TRUE
3126  **/
3127 gboolean
3128 gtk_widget_hide_on_delete (GtkWidget *widget)
3129 {
3130   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
3131   
3132   gtk_widget_hide (widget);
3133   
3134   return TRUE;
3135 }
3136
3137 /**
3138  * gtk_widget_show_all:
3139  * @widget: a #GtkWidget
3140  * 
3141  * Recursively shows a widget, and any child widgets (if the widget is
3142  * a container).
3143  **/
3144 void
3145 gtk_widget_show_all (GtkWidget *widget)
3146 {
3147   GtkWidgetClass *class;
3148
3149   g_return_if_fail (GTK_IS_WIDGET (widget));
3150
3151   if ((GTK_WIDGET_FLAGS (widget) & GTK_NO_SHOW_ALL) != 0)
3152     return;
3153
3154   class = GTK_WIDGET_GET_CLASS (widget);
3155
3156   if (class->show_all)
3157     class->show_all (widget);
3158 }
3159
3160 /**
3161  * gtk_widget_hide_all:
3162  * @widget: a #GtkWidget
3163  * 
3164  * Recursively hides a widget and any child widgets.
3165  **/
3166 void
3167 gtk_widget_hide_all (GtkWidget *widget)
3168 {
3169   GtkWidgetClass *class;
3170
3171   g_return_if_fail (GTK_IS_WIDGET (widget));
3172
3173   if ((GTK_WIDGET_FLAGS (widget) & GTK_NO_SHOW_ALL) != 0)
3174     return;
3175
3176   class = GTK_WIDGET_GET_CLASS (widget);
3177
3178   if (class->hide_all)
3179     class->hide_all (widget);
3180 }
3181
3182 /**
3183  * gtk_widget_map:
3184  * @widget: a #GtkWidget
3185  * 
3186  * This function is only for use in widget implementations. Causes
3187  * a widget to be mapped if it isn't already.
3188  **/
3189 void
3190 gtk_widget_map (GtkWidget *widget)
3191 {
3192   g_return_if_fail (GTK_IS_WIDGET (widget));
3193   g_return_if_fail (GTK_WIDGET_VISIBLE (widget));
3194   g_return_if_fail (GTK_WIDGET_CHILD_VISIBLE (widget));
3195   
3196   if (!GTK_WIDGET_MAPPED (widget))
3197     {
3198       if (!GTK_WIDGET_REALIZED (widget))
3199         gtk_widget_realize (widget);
3200
3201       g_signal_emit (widget, widget_signals[MAP], 0);
3202
3203       if (GTK_WIDGET_NO_WINDOW (widget))
3204         gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
3205     }
3206 }
3207
3208 /**
3209  * gtk_widget_unmap:
3210  * @widget: a #GtkWidget
3211  *
3212  * This function is only for use in widget implementations. Causes
3213  * a widget to be unmapped if it's currently mapped.
3214  **/
3215 void
3216 gtk_widget_unmap (GtkWidget *widget)
3217 {
3218   g_return_if_fail (GTK_IS_WIDGET (widget));
3219   
3220   if (GTK_WIDGET_MAPPED (widget))
3221     {
3222       if (GTK_WIDGET_NO_WINDOW (widget))
3223         gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
3224       _gtk_tooltip_hide (widget);
3225       g_signal_emit (widget, widget_signals[UNMAP], 0);
3226     }
3227 }
3228
3229 static void
3230 gtk_widget_set_extension_events_internal (GtkWidget        *widget,
3231                                           GdkExtensionMode  mode,
3232                                           GList            *window_list)
3233 {
3234   GList *free_list = NULL;
3235   GList *l;
3236
3237   if (window_list == NULL)
3238     {
3239       if (!GTK_WIDGET_NO_WINDOW (widget))
3240         window_list = g_list_prepend (NULL, widget->window);
3241       else
3242         window_list = gdk_window_get_children (widget->window);
3243
3244       free_list = window_list;
3245     }
3246
3247   for (l = window_list; l != NULL; l = l->next)
3248     {
3249       GdkWindow *window = l->data;
3250       gpointer user_data;
3251
3252       gdk_window_get_user_data (window, &user_data);
3253       if (user_data == widget)
3254         {
3255           GList *children;
3256
3257           gdk_input_set_extension_events (window,
3258                                           gdk_window_get_events (window),
3259                                           mode);
3260
3261           children = gdk_window_get_children (window);
3262           if (children)
3263             {
3264               gtk_widget_set_extension_events_internal (widget, mode, children);
3265               g_list_free (children);
3266             }
3267         }
3268     }
3269
3270   if (free_list)
3271     g_list_free (free_list);
3272 }
3273
3274 /**
3275  * gtk_widget_realize:
3276  * @widget: a #GtkWidget
3277  * 
3278  * Creates the GDK (windowing system) resources associated with a
3279  * widget.  For example, @widget->window will be created when a widget
3280  * is realized.  Normally realization happens implicitly; if you show
3281  * a widget and all its parent containers, then the widget will be
3282  * realized and mapped automatically.
3283  * 
3284  * Realizing a widget requires all
3285  * the widget's parent widgets to be realized; calling
3286  * gtk_widget_realize() realizes the widget's parents in addition to
3287  * @widget itself. If a widget is not yet inside a toplevel window
3288  * when you realize it, bad things will happen.
3289  *
3290  * This function is primarily used in widget implementations, and
3291  * isn't very useful otherwise. Many times when you think you might
3292  * need it, a better approach is to connect to a signal that will be
3293  * called after the widget is realized automatically, such as
3294  * GtkWidget::expose-event. Or simply g_signal_connect () to the
3295  * GtkWidget::realize signal.
3296  **/
3297 void
3298 gtk_widget_realize (GtkWidget *widget)
3299 {
3300   GdkExtensionMode mode;
3301   GtkWidgetShapeInfo *shape_info;
3302   
3303   g_return_if_fail (GTK_IS_WIDGET (widget));
3304   g_return_if_fail (GTK_WIDGET_ANCHORED (widget) ||
3305                     GTK_IS_INVISIBLE (widget));
3306   
3307   if (!GTK_WIDGET_REALIZED (widget))
3308     {
3309       /*
3310         if (GTK_IS_CONTAINER (widget) && !GTK_WIDGET_NO_WINDOW (widget))
3311           g_message ("gtk_widget_realize(%s)", g_type_name (GTK_WIDGET_TYPE (widget)));
3312       */
3313
3314       if (widget->parent == NULL &&
3315           !GTK_WIDGET_TOPLEVEL (widget))
3316         g_warning ("Calling gtk_widget_realize() on a widget that isn't "
3317                    "inside a toplevel window is not going to work very well. "
3318                    "Widgets must be inside a toplevel container before realizing them.");
3319       
3320       if (widget->parent && !GTK_WIDGET_REALIZED (widget->parent))
3321         gtk_widget_realize (widget->parent);
3322
3323       gtk_widget_ensure_style (widget);
3324       
3325       g_signal_emit (widget, widget_signals[REALIZE], 0);
3326
3327       gtk_widget_real_set_has_tooltip (widget,
3328                                        GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget), quark_has_tooltip)),
3329                                        TRUE);
3330
3331       if (GTK_WIDGET_HAS_SHAPE_MASK (widget))
3332         {
3333           shape_info = g_object_get_qdata (G_OBJECT (widget), quark_shape_info);
3334           gdk_window_shape_combine_mask (widget->window,
3335                                          shape_info->shape_mask,
3336                                          shape_info->offset_x,
3337                                          shape_info->offset_y);
3338         }
3339       
3340       shape_info = g_object_get_qdata (G_OBJECT (widget), quark_input_shape_info);
3341       if (shape_info)
3342         gdk_window_input_shape_combine_mask (widget->window,
3343                                              shape_info->shape_mask,
3344                                              shape_info->offset_x,
3345                                              shape_info->offset_y);
3346
3347       mode = gtk_widget_get_extension_events (widget);
3348       if (mode != GDK_EXTENSION_EVENTS_NONE)
3349         gtk_widget_set_extension_events_internal (widget, mode, NULL);
3350     }
3351 }
3352
3353 /**
3354  * gtk_widget_unrealize:
3355  * @widget: a #GtkWidget
3356  *
3357  * This function is only useful in widget implementations.
3358  * Causes a widget to be unrealized (frees all GDK resources
3359  * associated with the widget, such as @widget->window).
3360  **/
3361 void
3362 gtk_widget_unrealize (GtkWidget *widget)
3363 {
3364   g_return_if_fail (GTK_IS_WIDGET (widget));
3365
3366   if (GTK_WIDGET_HAS_SHAPE_MASK (widget))
3367     gtk_widget_shape_combine_mask (widget, NULL, 0, 0);
3368
3369   if (g_object_get_qdata (G_OBJECT (widget), quark_input_shape_info))
3370     gtk_widget_input_shape_combine_mask (widget, NULL, 0, 0);
3371
3372   if (GTK_WIDGET_REALIZED (widget))
3373     {
3374       g_object_ref (widget);
3375       _gtk_tooltip_hide (widget);
3376       g_signal_emit (widget, widget_signals[UNREALIZE], 0);
3377       GTK_WIDGET_UNSET_FLAGS (widget, GTK_REALIZED | GTK_MAPPED);
3378       g_object_unref (widget);
3379     }
3380 }
3381
3382 /*****************************************
3383  * Draw queueing.
3384  *****************************************/
3385
3386 /**
3387  * gtk_widget_queue_draw_area:
3388  * @widget: a #GtkWidget
3389  * @x: x coordinate of upper-left corner of rectangle to redraw
3390  * @y: y coordinate of upper-left corner of rectangle to redraw
3391  * @width: width of region to draw
3392  * @height: height of region to draw
3393  *
3394  * Invalidates the rectangular area of @widget defined by @x, @y,
3395  * @width and @height by calling gdk_window_invalidate_rect() on the
3396  * widget's window and all its child windows. Once the main loop
3397  * becomes idle (after the current batch of events has been processed,
3398  * roughly), the window will receive expose events for the union of
3399  * all regions that have been invalidated.
3400  *
3401  * Normally you would only use this function in widget
3402  * implementations. You might also use it, or
3403  * gdk_window_invalidate_rect() directly, to schedule a redraw of a
3404  * #GtkDrawingArea or some portion thereof.
3405  *
3406  * Frequently you can just call gdk_window_invalidate_rect() or
3407  * gdk_window_invalidate_region() instead of this function. Those
3408  * functions will invalidate only a single window, instead of the
3409  * widget and all its children.
3410  *
3411  * The advantage of adding to the invalidated region compared to
3412  * simply drawing immediately is efficiency; using an invalid region
3413  * ensures that you only have to redraw one time.
3414  **/
3415 void       
3416 gtk_widget_queue_draw_area (GtkWidget *widget,
3417                             gint       x,
3418                             gint       y,
3419                             gint       width,
3420                             gint       height)
3421 {
3422   GdkRectangle invalid_rect;
3423   GtkWidget *w;
3424   
3425   g_return_if_fail (GTK_IS_WIDGET (widget));
3426
3427   if (!GTK_WIDGET_REALIZED (widget))
3428     return;
3429   
3430   /* Just return if the widget or one of its ancestors isn't mapped */
3431   for (w = widget; w != NULL; w = w->parent)
3432     if (!GTK_WIDGET_MAPPED (w))
3433       return;
3434
3435   /* Find the correct widget */
3436
3437   if (!GTK_WIDGET_NO_WINDOW (widget))
3438     {
3439       if (widget->parent)
3440         {
3441           /* Translate widget relative to window-relative */
3442
3443           gint wx, wy, wwidth, wheight;
3444           
3445           gdk_window_get_position (widget->window, &wx, &wy);
3446           x -= wx - widget->allocation.x;
3447           y -= wy - widget->allocation.y;
3448           
3449           gdk_drawable_get_size (widget->window, &wwidth, &wheight);
3450
3451           if (x + width <= 0 || y + height <= 0 ||
3452               x >= wwidth || y >= wheight)
3453             return;
3454           
3455           if (x < 0)
3456             {
3457               width += x;  x = 0;
3458             }
3459           if (y < 0)
3460             {
3461               height += y; y = 0;
3462             }
3463           if (x + width > wwidth)
3464             width = wwidth - x;
3465           if (y + height > wheight)
3466             height = wheight - y;
3467         }
3468     }
3469
3470   invalid_rect.x = x;
3471   invalid_rect.y = y;
3472   invalid_rect.width = width;
3473   invalid_rect.height = height;
3474   
3475   gdk_window_invalidate_rect (widget->window, &invalid_rect, TRUE);
3476 }
3477
3478 static void
3479 widget_add_child_draw_rectangle (GtkWidget    *widget,
3480                                  GdkRectangle *rect)
3481 {
3482   GdkRectangle child_rect;
3483   
3484   if (!GTK_WIDGET_MAPPED (widget) ||
3485       widget->window != widget->parent->window)
3486     return;
3487
3488   gtk_widget_get_draw_rectangle (widget, &child_rect);
3489   gdk_rectangle_union (rect, &child_rect, rect);
3490 }
3491
3492 static void
3493 gtk_widget_get_draw_rectangle (GtkWidget    *widget,
3494                                GdkRectangle *rect)
3495 {
3496   if (GTK_WIDGET_NO_WINDOW (widget))
3497     {
3498       GtkBorder *draw_border = NULL;
3499
3500       *rect = widget->allocation;
3501
3502       gtk_widget_style_get (widget,
3503                             "draw-border", &draw_border,
3504                             NULL);
3505       if (draw_border)
3506         {
3507           rect->x -= draw_border->left;
3508           rect->y -= draw_border->top;
3509           rect->width += draw_border->left + draw_border->right;
3510           rect->height += draw_border->top + draw_border->bottom;
3511
3512           gtk_border_free (draw_border);
3513         }
3514
3515       if (GTK_IS_CONTAINER (widget))
3516         gtk_container_forall (GTK_CONTAINER (widget),
3517                               (GtkCallback)widget_add_child_draw_rectangle,
3518                               rect);
3519     }
3520   else
3521     {
3522       rect->x = 0;
3523       rect->y = 0;
3524       rect->width = widget->allocation.width;
3525       rect->height = widget->allocation.height;
3526     }
3527 }
3528
3529 /**
3530  * gtk_widget_queue_draw:
3531  * @widget: a #GtkWidget
3532  *
3533  * Equivalent to calling gtk_widget_queue_draw_area() for the
3534  * entire area of a widget.
3535  **/
3536 void       
3537 gtk_widget_queue_draw (GtkWidget *widget)
3538 {
3539   GdkRectangle rect;
3540   
3541   g_return_if_fail (GTK_IS_WIDGET (widget));
3542
3543   gtk_widget_get_draw_rectangle (widget, &rect);
3544
3545   gtk_widget_queue_draw_area (widget,
3546                               rect.x, rect.y,
3547                               rect.width, rect.height);
3548 }
3549
3550 /* Invalidates the given area (allocation-relative-coordinates)
3551  * in all of the widget's windows
3552  */
3553 /**
3554  * gtk_widget_queue_clear_area:
3555  * @widget: a #GtkWidget
3556  * @x: x coordinate of upper-left corner of rectangle to redraw
3557  * @y: y coordinate of upper-left corner of rectangle to redraw
3558  * @width: width of region to draw
3559  * @height: height of region to draw
3560  * 
3561  * This function is no longer different from
3562  * gtk_widget_queue_draw_area(), though it once was. Now it just calls
3563  * gtk_widget_queue_draw_area(). Originally
3564  * gtk_widget_queue_clear_area() would force a redraw of the
3565  * background for %GTK_NO_WINDOW widgets, and
3566  * gtk_widget_queue_draw_area() would not. Now both functions ensure
3567  * the background will be redrawn.
3568  * 
3569  * Deprecated: 2.2: Use gtk_widget_queue_draw_area() instead.
3570  **/
3571 void       
3572 gtk_widget_queue_clear_area (GtkWidget *widget,
3573                              gint       x,
3574                              gint       y,
3575                              gint       width,
3576                              gint       height)
3577 {
3578   g_return_if_fail (GTK_IS_WIDGET (widget));
3579
3580   gtk_widget_queue_draw_area (widget, x, y, width, height);
3581 }
3582
3583 /**
3584  * gtk_widget_queue_clear:
3585  * @widget: a #GtkWidget
3586  * 
3587  * This function does the same as gtk_widget_queue_draw().
3588  *
3589  * Deprecated: 2.2: Use gtk_widget_queue_draw() instead.
3590  **/
3591 void       
3592 gtk_widget_queue_clear (GtkWidget *widget)
3593 {
3594   g_return_if_fail (GTK_IS_WIDGET (widget));
3595
3596   gtk_widget_queue_draw (widget);
3597 }
3598
3599 /**
3600  * gtk_widget_queue_resize:
3601  * @widget: a #GtkWidget
3602  *
3603  * This function is only for use in widget implementations.
3604  * Flags a widget to have its size renegotiated; should
3605  * be called when a widget for some reason has a new size request.
3606  * For example, when you change the text in a #GtkLabel, #GtkLabel
3607  * queues a resize to ensure there's enough space for the new text.
3608  **/
3609 void
3610 gtk_widget_queue_resize (GtkWidget *widget)
3611 {
3612   g_return_if_fail (GTK_IS_WIDGET (widget));
3613
3614   if (GTK_WIDGET_REALIZED (widget))
3615     gtk_widget_queue_shallow_draw (widget);
3616       
3617   _gtk_size_group_queue_resize (widget);
3618 }
3619
3620 /**
3621  * gtk_widget_queue_resize_no_redraw:
3622  * @widget: a #GtkWidget
3623  *
3624  * This function works like gtk_widget_queue_resize(), 
3625  * except that the widget is not invalidated.
3626  *
3627  * Since: 2.4
3628  **/
3629 void
3630 gtk_widget_queue_resize_no_redraw (GtkWidget *widget)
3631 {
3632   g_return_if_fail (GTK_IS_WIDGET (widget));
3633
3634   _gtk_size_group_queue_resize (widget);
3635 }
3636
3637 /**
3638  * gtk_widget_draw:
3639  * @widget: a #GtkWidget
3640  * @area: area to draw
3641  *
3642  * In GTK+ 1.2, this function would immediately render the
3643  * region @area of a widget, by invoking the virtual draw method of a
3644  * widget. In GTK+ 2.0, the draw method is gone, and instead
3645  * gtk_widget_draw() simply invalidates the specified region of the
3646  * widget, then updates the invalid region of the widget immediately.
3647  * Usually you don't want to update the region immediately for
3648  * performance reasons, so in general gtk_widget_queue_draw_area() is
3649  * a better choice if you want to draw a region of a widget.
3650  **/
3651 void
3652 gtk_widget_draw (GtkWidget          *widget,
3653                  const GdkRectangle *area)
3654 {
3655   g_return_if_fail (GTK_IS_WIDGET (widget));
3656
3657   if (GTK_WIDGET_DRAWABLE (widget))
3658     {
3659       if (area)
3660         gtk_widget_queue_draw_area (widget,
3661                                     area->x, area->y,
3662                                     area->width, area->height);
3663       else
3664         gtk_widget_queue_draw (widget);
3665
3666       gdk_window_process_updates (widget->window, TRUE);
3667     }
3668 }
3669
3670 /**
3671  * gtk_widget_size_request:
3672  * @widget: a #GtkWidget
3673  * @requisition: a #GtkRequisition to be filled in
3674  * 
3675  * This function is typically used when implementing a #GtkContainer
3676  * subclass.  Obtains the preferred size of a widget. The container
3677  * uses this information to arrange its child widgets and decide what
3678  * size allocations to give them with gtk_widget_size_allocate().
3679  *
3680  * You can also call this function from an application, with some
3681  * caveats. Most notably, getting a size request requires the widget
3682  * to be associated with a screen, because font information may be
3683  * needed. Multihead-aware applications should keep this in mind.
3684  *
3685  * Also remember that the size request is not necessarily the size
3686  * a widget will actually be allocated.
3687  *
3688  * See also gtk_widget_get_child_requisition().
3689  **/
3690 void
3691 gtk_widget_size_request (GtkWidget      *widget,
3692                          GtkRequisition *requisition)
3693 {
3694   g_return_if_fail (GTK_IS_WIDGET (widget));
3695
3696 #ifdef G_ENABLE_DEBUG
3697   if (requisition == &widget->requisition)
3698     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.");
3699 #endif /* G_ENABLE_DEBUG */
3700
3701   _gtk_size_group_compute_requisition (widget, requisition);
3702 }
3703
3704 /**
3705  * gtk_widget_get_child_requisition:
3706  * @widget: a #GtkWidget
3707  * @requisition: a #GtkRequisition to be filled in
3708  * 
3709  * This function is only for use in widget implementations. Obtains
3710  * @widget->requisition, unless someone has forced a particular
3711  * geometry on the widget (e.g. with gtk_widget_set_size_request()),
3712  * in which case it returns that geometry instead of the widget's
3713  * requisition.
3714  *
3715  * This function differs from gtk_widget_size_request() in that
3716  * it retrieves the last size request value from @widget->requisition,
3717  * while gtk_widget_size_request() actually calls the "size_request" method
3718  * on @widget to compute the size request and fill in @widget->requisition,
3719  * and only then returns @widget->requisition.
3720  *
3721  * Because this function does not call the "size_request" method, it
3722  * can only be used when you know that @widget->requisition is
3723  * up-to-date, that is, gtk_widget_size_request() has been called
3724  * since the last time a resize was queued. In general, only container
3725  * implementations have this information; applications should use
3726  * gtk_widget_size_request().
3727  **/
3728 void
3729 gtk_widget_get_child_requisition (GtkWidget      *widget,
3730                                   GtkRequisition *requisition)
3731 {
3732   _gtk_size_group_get_child_requisition (widget, requisition);
3733 }
3734
3735 static gboolean
3736 invalidate_predicate (GdkWindow *window,
3737                       gpointer   data)
3738 {
3739   gpointer user_data;
3740
3741   gdk_window_get_user_data (window, &user_data);
3742
3743   return (user_data == data);
3744 }
3745
3746 /* Invalidate @region in widget->window and all children
3747  * of widget->window owned by widget. @region is in the
3748  * same coordinates as widget->allocation and will be
3749  * modified by this call.
3750  */
3751 static void
3752 gtk_widget_invalidate_widget_windows (GtkWidget *widget,
3753                                       GdkRegion *region)
3754 {
3755   if (!GTK_WIDGET_REALIZED (widget))
3756     return;
3757   
3758   if (!GTK_WIDGET_NO_WINDOW (widget) && widget->parent)
3759     {
3760       int x, y;
3761       
3762       gdk_window_get_position (widget->window, &x, &y);
3763       gdk_region_offset (region, -x, -y);
3764     }
3765
3766   gdk_window_invalidate_maybe_recurse (widget->window, region,
3767                                        invalidate_predicate, widget);
3768 }
3769
3770 /**
3771  * gtk_widget_queue_shallow_draw:
3772  * @widget: a #GtkWidget
3773  *
3774  * Like gtk_widget_queue_draw(), but only windows owned
3775  * by @widget are invalidated.
3776  **/
3777 static void
3778 gtk_widget_queue_shallow_draw (GtkWidget *widget)
3779 {
3780   GdkRectangle rect;
3781   GdkRegion *region;
3782   
3783   if (!GTK_WIDGET_REALIZED (widget))
3784     return;
3785
3786   gtk_widget_get_draw_rectangle (widget, &rect);
3787
3788   /* get_draw_rectangle() gives us window coordinates, we
3789    * need to convert to the coordinates that widget->allocation
3790    * is in.
3791    */
3792   if (!GTK_WIDGET_NO_WINDOW (widget) && widget->parent)
3793     {
3794       int wx, wy;
3795       
3796       gdk_window_get_position (widget->window, &wx, &wy);
3797       
3798       rect.x += wx;
3799       rect.y += wy;
3800     }
3801   
3802   region = gdk_region_rectangle (&rect);
3803   gtk_widget_invalidate_widget_windows (widget, region);
3804   gdk_region_destroy (region);
3805 }
3806
3807 /**
3808  * gtk_widget_size_allocate:
3809  * @widget: a #GtkWidget
3810  * @allocation: position and size to be allocated to @widget
3811  *
3812  * This function is only used by #GtkContainer subclasses, to assign a size
3813  * and position to their child widgets. 
3814  **/
3815 void
3816 gtk_widget_size_allocate (GtkWidget     *widget,
3817                           GtkAllocation *allocation)
3818 {
3819   GtkWidgetAuxInfo *aux_info;
3820   GdkRectangle real_allocation;
3821   GdkRectangle old_allocation;
3822   gboolean alloc_needed;
3823   gboolean size_changed;
3824   gboolean position_changed;
3825   
3826   g_return_if_fail (GTK_IS_WIDGET (widget));
3827  
3828 #ifdef G_ENABLE_DEBUG
3829   if (gtk_debug_flags & GTK_DEBUG_GEOMETRY)
3830     {
3831       gint depth;
3832       GtkWidget *parent;
3833       const gchar *name;
3834
3835       depth = 0;
3836       parent = widget;
3837       while (parent)
3838         {
3839           depth++;
3840           parent = gtk_widget_get_parent (parent);
3841         }
3842       
3843       name = g_type_name (G_OBJECT_TYPE (G_OBJECT (widget)));
3844       g_print ("gtk_widget_size_allocate: %*s%s %d %d\n", 
3845                2 * depth, " ", name, 
3846                allocation->width, allocation->height);
3847     }
3848 #endif /* G_ENABLE_DEBUG */
3849  
3850   alloc_needed = GTK_WIDGET_ALLOC_NEEDED (widget);
3851   if (!GTK_WIDGET_REQUEST_NEEDED (widget))      /* Preserve request/allocate ordering */
3852     GTK_PRIVATE_UNSET_FLAG (widget, GTK_ALLOC_NEEDED);
3853
3854   old_allocation = widget->allocation;
3855   real_allocation = *allocation;
3856   aux_info =_gtk_widget_get_aux_info (widget, FALSE);
3857   
3858   if (aux_info)
3859     {
3860       if (aux_info->x_set)
3861         real_allocation.x = aux_info->x;
3862       if (aux_info->y_set)
3863         real_allocation.y = aux_info->y;
3864     }
3865
3866   if (real_allocation.width < 0 || real_allocation.height < 0)
3867     {
3868       g_warning ("gtk_widget_size_allocate(): attempt to allocate widget with width %d and height %d",
3869                  real_allocation.width,
3870                  real_allocation.height);
3871     }
3872   
3873   real_allocation.width = MAX (real_allocation.width, 1);
3874   real_allocation.height = MAX (real_allocation.height, 1);
3875
3876   size_changed = (old_allocation.width != real_allocation.width ||
3877                   old_allocation.height != real_allocation.height);
3878   position_changed = (old_allocation.x != real_allocation.x ||
3879                       old_allocation.y != real_allocation.y);
3880
3881   if (!alloc_needed && !size_changed && !position_changed)
3882     return;
3883   
3884   g_signal_emit (widget, widget_signals[SIZE_ALLOCATE], 0, &real_allocation);
3885
3886   if (GTK_WIDGET_MAPPED (widget))
3887     {
3888       if (GTK_WIDGET_NO_WINDOW (widget) && GTK_WIDGET_REDRAW_ON_ALLOC (widget) && position_changed)
3889         {
3890           /* Invalidate union(old_allaction,widget->allocation) in widget->window
3891            */
3892           GdkRegion *invalidate = gdk_region_rectangle (&widget->allocation);
3893           gdk_region_union_with_rect (invalidate, &old_allocation);
3894
3895           gdk_window_invalidate_region (widget->window, invalidate, FALSE);
3896           gdk_region_destroy (invalidate);
3897         }
3898       
3899       if (size_changed)
3900         {
3901           if (GTK_WIDGET_REDRAW_ON_ALLOC (widget))
3902             {
3903               /* Invalidate union(old_allaction,widget->allocation) in widget->window and descendents owned by widget
3904                */
3905               GdkRegion *invalidate = gdk_region_rectangle (&widget->allocation);
3906               gdk_region_union_with_rect (invalidate, &old_allocation);
3907
3908               gtk_widget_invalidate_widget_windows (widget, invalidate);
3909               gdk_region_destroy (invalidate);
3910             }
3911         }
3912     }
3913
3914   if ((size_changed || position_changed) && widget->parent &&
3915       GTK_WIDGET_REALIZED (widget->parent) && GTK_CONTAINER (widget->parent)->reallocate_redraws)
3916     {
3917       GdkRegion *invalidate = gdk_region_rectangle (&widget->parent->allocation);
3918       gtk_widget_invalidate_widget_windows (widget->parent, invalidate);
3919       gdk_region_destroy (invalidate);
3920     }
3921 }
3922
3923 /**
3924  * gtk_widget_common_ancestor:
3925  * @widget_a: a #GtkWidget
3926  * @widget_b: a #GtkWidget
3927  * 
3928  * Find the common ancestor of @widget_a and @widget_b that
3929  * is closest to the two widgets.
3930  * 
3931  * Return value: the closest common ancestor of @widget_a and
3932  *   @widget_b or %NULL if @widget_a and @widget_b do not
3933  *   share a common ancestor.
3934  **/
3935 static GtkWidget *
3936 gtk_widget_common_ancestor (GtkWidget *widget_a,
3937                             GtkWidget *widget_b)
3938 {
3939   GtkWidget *parent_a;
3940   GtkWidget *parent_b;
3941   gint depth_a = 0;
3942   gint depth_b = 0;
3943
3944   parent_a = widget_a;
3945   while (parent_a->parent)
3946     {
3947       parent_a = parent_a->parent;
3948       depth_a++;
3949     }
3950
3951   parent_b = widget_b;
3952   while (parent_b->parent)
3953     {
3954       parent_b = parent_b->parent;
3955       depth_b++;
3956     }
3957
3958   if (parent_a != parent_b)
3959     return NULL;
3960
3961   while (depth_a > depth_b)
3962     {
3963       widget_a = widget_a->parent;
3964       depth_a--;
3965     }
3966
3967   while (depth_b > depth_a)
3968     {
3969       widget_b = widget_b->parent;
3970       depth_b--;
3971     }
3972
3973   while (widget_a != widget_b)
3974     {
3975       widget_a = widget_a->parent;
3976       widget_b = widget_b->parent;
3977     }
3978
3979   return widget_a;
3980 }
3981
3982 /**
3983  * gtk_widget_translate_coordinates:
3984  * @src_widget:  a #GtkWidget
3985  * @dest_widget: a #GtkWidget
3986  * @src_x: X position relative to @src_widget
3987  * @src_y: Y position relative to @src_widget
3988  * @dest_x: location to store X position relative to @dest_widget
3989  * @dest_y: location to store Y position relative to @dest_widget
3990  * 
3991  * Translate coordinates relative to @src_widget's allocation to coordinates
3992  * relative to @dest_widget's allocations. In order to perform this
3993  * operation, both widgets must be realized, and must share a common
3994  * toplevel.
3995  * 
3996  * Return value: %FALSE if either widget was not realized, or there
3997  *   was no common ancestor. In this case, nothing is stored in
3998  *   *@dest_x and *@dest_y. Otherwise %TRUE.
3999  **/
4000 gboolean
4001 gtk_widget_translate_coordinates (GtkWidget  *src_widget,
4002                                   GtkWidget  *dest_widget,
4003                                   gint        src_x,
4004                                   gint        src_y,
4005                                   gint       *dest_x,
4006                                   gint       *dest_y)
4007 {
4008   GtkWidget *ancestor;
4009   GdkWindow *window;
4010
4011   g_return_val_if_fail (GTK_IS_WIDGET (src_widget), FALSE);
4012   g_return_val_if_fail (GTK_IS_WIDGET (dest_widget), FALSE);
4013
4014   ancestor = gtk_widget_common_ancestor (src_widget, dest_widget);
4015   if (!ancestor || !GTK_WIDGET_REALIZED (src_widget) || !GTK_WIDGET_REALIZED (dest_widget))
4016     return FALSE;
4017
4018   /* Translate from allocation relative to window relative */
4019   if (!GTK_WIDGET_NO_WINDOW (src_widget) && src_widget->parent)
4020     {
4021       gint wx, wy;
4022       gdk_window_get_position (src_widget->window, &wx, &wy);
4023
4024       src_x -= wx - src_widget->allocation.x;
4025       src_y -= wy - src_widget->allocation.y;
4026     }
4027   else
4028     {
4029       src_x += src_widget->allocation.x;
4030       src_y += src_widget->allocation.y;
4031     }
4032
4033   /* Translate to the common ancestor */
4034   window = src_widget->window;
4035   while (window != ancestor->window)
4036     {
4037       gint dx, dy;
4038       
4039       gdk_window_get_position (window, &dx, &dy);
4040       
4041       src_x += dx;
4042       src_y += dy;
4043       
4044       window = gdk_window_get_parent (window);
4045
4046       if (!window)              /* Handle GtkHandleBox */
4047         return FALSE;
4048     }
4049
4050   /* And back */
4051   window = dest_widget->window;
4052   while (window != ancestor->window)
4053     {
4054       gint dx, dy;
4055       
4056       gdk_window_get_position (window, &dx, &dy);
4057       
4058       src_x -= dx;
4059       src_y -= dy;
4060       
4061       window = gdk_window_get_parent (window);
4062       
4063       if (!window)              /* Handle GtkHandleBox */
4064         return FALSE;
4065     }
4066
4067   /* Translate from window relative to allocation relative */
4068   if (!GTK_WIDGET_NO_WINDOW (dest_widget) && dest_widget->parent)
4069     {
4070       gint wx, wy;
4071       gdk_window_get_position (dest_widget->window, &wx, &wy);
4072
4073       src_x += wx - dest_widget->allocation.x;
4074       src_y += wy - dest_widget->allocation.y;
4075     }
4076   else
4077     {
4078       src_x -= dest_widget->allocation.x;
4079       src_y -= dest_widget->allocation.y;
4080     }
4081
4082   if (dest_x)
4083     *dest_x = src_x;
4084   if (dest_y)
4085     *dest_y = src_y;
4086
4087   return TRUE;
4088 }
4089
4090 static void
4091 gtk_widget_real_size_allocate (GtkWidget     *widget,
4092                                GtkAllocation *allocation)
4093 {
4094   widget->allocation = *allocation;
4095   
4096   if (GTK_WIDGET_REALIZED (widget) &&
4097       !GTK_WIDGET_NO_WINDOW (widget))
4098      {
4099         gdk_window_move_resize (widget->window,
4100                                 allocation->x, allocation->y,
4101                                 allocation->width, allocation->height);
4102      }
4103 }
4104
4105 static gboolean
4106 gtk_widget_real_can_activate_accel (GtkWidget *widget,
4107                                     guint      signal_id)
4108 {
4109   /* widgets must be onscreen for accels to take effect */
4110   return GTK_WIDGET_IS_SENSITIVE (widget) && GTK_WIDGET_DRAWABLE (widget) && gdk_window_is_viewable (widget->window);
4111 }
4112
4113 /**
4114  * gtk_widget_can_activate_accel:
4115  * @widget: a #GtkWidget
4116  * @signal_id: the ID of a signal installed on @widget
4117  * 
4118  * Determines whether an accelerator that activates the signal
4119  * identified by @signal_id can currently be activated.
4120  * This is done by emitting the #GtkWidget::can-activate-accel
4121  * signal on @widget; if the signal isn't overridden by a
4122  * handler or in a derived widget, then the default check is
4123  * that the widget must be sensitive, and the widget and all
4124  * its ancestors mapped.
4125  *
4126  * Return value: %TRUE if the accelerator can be activated.
4127  *
4128  * Since: 2.4
4129  **/
4130 gboolean
4131 gtk_widget_can_activate_accel (GtkWidget *widget,
4132                                guint      signal_id)
4133 {
4134   gboolean can_activate = FALSE;
4135   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4136   g_signal_emit (widget, widget_signals[CAN_ACTIVATE_ACCEL], 0, signal_id, &can_activate);
4137   return can_activate;
4138 }
4139
4140 typedef struct {
4141   GClosure   closure;
4142   guint      signal_id;
4143 } AccelClosure;
4144
4145 static void
4146 closure_accel_activate (GClosure     *closure,
4147                         GValue       *return_value,
4148                         guint         n_param_values,
4149                         const GValue *param_values,
4150                         gpointer      invocation_hint,
4151                         gpointer      marshal_data)
4152 {
4153   AccelClosure *aclosure = (AccelClosure*) closure;
4154   gboolean can_activate = gtk_widget_can_activate_accel (closure->data, aclosure->signal_id);
4155
4156   if (can_activate)
4157     g_signal_emit (closure->data, aclosure->signal_id, 0);
4158
4159   /* whether accelerator was handled */
4160   g_value_set_boolean (return_value, can_activate);
4161 }
4162
4163 static void
4164 closures_destroy (gpointer data)
4165 {
4166   GSList *slist, *closures = data;
4167
4168   for (slist = closures; slist; slist = slist->next)
4169     {
4170       g_closure_invalidate (slist->data);
4171       g_closure_unref (slist->data);
4172     }
4173   g_slist_free (closures);
4174 }
4175
4176 static GClosure*
4177 widget_new_accel_closure (GtkWidget *widget,
4178                           guint      signal_id)
4179 {
4180   AccelClosure *aclosure;
4181   GClosure *closure = NULL;
4182   GSList *slist, *closures;
4183
4184   closures = g_object_steal_qdata (G_OBJECT (widget), quark_accel_closures);
4185   for (slist = closures; slist; slist = slist->next)
4186     if (!gtk_accel_group_from_accel_closure (slist->data))
4187       {
4188         /* reuse this closure */
4189         closure = slist->data;
4190         break;
4191       }
4192   if (!closure)
4193     {
4194       closure = g_closure_new_object (sizeof (AccelClosure), G_OBJECT (widget));
4195       closures = g_slist_prepend (closures, g_closure_ref (closure));
4196       g_closure_sink (closure);
4197       g_closure_set_marshal (closure, closure_accel_activate);
4198     }
4199   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_closures, closures, closures_destroy);
4200   
4201   aclosure = (AccelClosure*) closure;
4202   g_assert (closure->data == widget);
4203   g_assert (closure->marshal == closure_accel_activate);
4204   aclosure->signal_id = signal_id;
4205
4206   return closure;
4207 }
4208
4209 /**
4210  * gtk_widget_add_accelerator
4211  * @widget:       widget to install an accelerator on
4212  * @accel_signal: widget signal to emit on accelerator activation
4213  * @accel_group:  accel group for this widget, added to its toplevel
4214  * @accel_key:    GDK keyval of the accelerator
4215  * @accel_mods:   modifier key combination of the accelerator
4216  * @accel_flags:  flag accelerators, e.g. %GTK_ACCEL_VISIBLE
4217  *
4218  * Installs an accelerator for this @widget in @accel_group that causes
4219  * @accel_signal to be emitted if the accelerator is activated.
4220  * The @accel_group needs to be added to the widget's toplevel via
4221  * gtk_window_add_accel_group(), and the signal must be of type %G_RUN_ACTION.
4222  * Accelerators added through this function are not user changeable during
4223  * runtime. If you want to support accelerators that can be changed by the
4224  * user, use gtk_accel_map_add_entry() and gtk_widget_set_accel_path() or
4225  * gtk_menu_item_set_accel_path() instead.
4226  */
4227 void
4228 gtk_widget_add_accelerator (GtkWidget      *widget,
4229                             const gchar    *accel_signal,
4230                             GtkAccelGroup  *accel_group,
4231                             guint           accel_key,
4232                             GdkModifierType accel_mods,
4233                             GtkAccelFlags   accel_flags)
4234 {
4235   GClosure *closure;
4236   GSignalQuery query;
4237
4238   g_return_if_fail (GTK_IS_WIDGET (widget));
4239   g_return_if_fail (accel_signal != NULL);
4240   g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
4241
4242   g_signal_query (g_signal_lookup (accel_signal, G_OBJECT_TYPE (widget)), &query);
4243   if (!query.signal_id ||
4244       !(query.signal_flags & G_SIGNAL_ACTION) ||
4245       query.return_type != G_TYPE_NONE ||
4246       query.n_params)
4247     {
4248       /* hmm, should be elaborate enough */
4249       g_warning (G_STRLOC ": widget `%s' has no activatable signal \"%s\" without arguments",
4250                  G_OBJECT_TYPE_NAME (widget), accel_signal);
4251       return;
4252     }
4253
4254   closure = widget_new_accel_closure (widget, query.signal_id);
4255
4256   g_object_ref (widget);
4257
4258   /* install the accelerator. since we don't map this onto an accel_path,
4259    * the accelerator will automatically be locked.
4260    */
4261   gtk_accel_group_connect (accel_group,
4262                            accel_key,
4263                            accel_mods,
4264                            accel_flags | GTK_ACCEL_LOCKED,
4265                            closure);
4266
4267   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
4268
4269   g_object_unref (widget);
4270 }
4271
4272 /**
4273  * gtk_widget_remove_accelerator:
4274  * @widget:       widget to install an accelerator on
4275  * @accel_group:  accel group for this widget
4276  * @accel_key:    GDK keyval of the accelerator
4277  * @accel_mods:   modifier key combination of the accelerator
4278  * @returns:      whether an accelerator was installed and could be removed
4279  *
4280  * Removes an accelerator from @widget, previously installed with
4281  * gtk_widget_add_accelerator().
4282  */
4283 gboolean
4284 gtk_widget_remove_accelerator (GtkWidget      *widget,
4285                                GtkAccelGroup  *accel_group,
4286                                guint           accel_key,
4287                                GdkModifierType accel_mods)
4288 {
4289   GtkAccelGroupEntry *ag_entry;
4290   GList *slist, *clist;
4291   guint n;
4292   
4293   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4294   g_return_val_if_fail (GTK_IS_ACCEL_GROUP (accel_group), FALSE);
4295
4296   ag_entry = gtk_accel_group_query (accel_group, accel_key, accel_mods, &n);
4297   clist = gtk_widget_list_accel_closures (widget);
4298   for (slist = clist; slist; slist = slist->next)
4299     {
4300       guint i;
4301
4302       for (i = 0; i < n; i++)
4303         if (slist->data == (gpointer) ag_entry[i].closure)
4304           {
4305             gboolean is_removed = gtk_accel_group_disconnect (accel_group, slist->data);
4306
4307             g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
4308
4309             g_list_free (clist);
4310
4311             return is_removed;
4312           }
4313     }
4314   g_list_free (clist);
4315
4316   g_warning (G_STRLOC ": no accelerator (%u,%u) installed in accel group (%p) for %s (%p)",
4317              accel_key, accel_mods, accel_group,
4318              G_OBJECT_TYPE_NAME (widget), widget);
4319
4320   return FALSE;
4321 }
4322
4323 /**
4324  * gtk_widget_list_accel_closures
4325  * @widget:  widget to list accelerator closures for
4326  * @returns: a newly allocated #GList of closures
4327  *
4328  * Lists the closures used by @widget for accelerator group connections
4329  * with gtk_accel_group_connect_by_path() or gtk_accel_group_connect().
4330  * The closures can be used to monitor accelerator changes on @widget,
4331  * by connecting to the @GtkAccelGroup::accel-changed signal of the 
4332  * #GtkAccelGroup of a closure which can be found out with 
4333  * gtk_accel_group_from_accel_closure().
4334  */
4335 GList*
4336 gtk_widget_list_accel_closures (GtkWidget *widget)
4337 {
4338   GSList *slist;
4339   GList *clist = NULL;
4340
4341   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4342
4343   for (slist = g_object_get_qdata (G_OBJECT (widget), quark_accel_closures); slist; slist = slist->next)
4344     if (gtk_accel_group_from_accel_closure (slist->data))
4345       clist = g_list_prepend (clist, slist->data);
4346   return clist;
4347 }
4348
4349 typedef struct {
4350   GQuark         path_quark;
4351   GtkAccelGroup *accel_group;
4352   GClosure      *closure;
4353 } AccelPath;
4354
4355 static void
4356 destroy_accel_path (gpointer data)
4357 {
4358   AccelPath *apath = data;
4359
4360   gtk_accel_group_disconnect (apath->accel_group, apath->closure);
4361
4362   /* closures_destroy takes care of unrefing the closure */
4363   g_object_unref (apath->accel_group);
4364   
4365   g_slice_free (AccelPath, apath);
4366 }
4367
4368
4369 /**
4370  * gtk_widget_set_accel_path:
4371  * @widget: a #GtkWidget
4372  * @accel_path: path used to look up the accelerator
4373  * @accel_group: a #GtkAccelGroup.
4374  * 
4375  * Given an accelerator group, @accel_group, and an accelerator path,
4376  * @accel_path, sets up an accelerator in @accel_group so whenever the
4377  * key binding that is defined for @accel_path is pressed, @widget
4378  * will be activated.  This removes any accelerators (for any
4379  * accelerator group) installed by previous calls to
4380  * gtk_widget_set_accel_path(). Associating accelerators with
4381  * paths allows them to be modified by the user and the modifications
4382  * to be saved for future use. (See gtk_accel_map_save().)
4383  *
4384  * This function is a low level function that would most likely
4385  * be used by a menu creation system like #GtkUIManager. If you
4386  * use #GtkUIManager, setting up accelerator paths will be done
4387  * automatically.
4388  *
4389  * Even when you you aren't using #GtkUIManager, if you only want to
4390  * set up accelerators on menu items gtk_menu_item_set_accel_path()
4391  * provides a somewhat more convenient interface.
4392  * 
4393  * Note that @accel_path string will be stored in a #GQuark. Therefore, if you
4394  * pass a static string, you can save some memory by interning it first with 
4395  * g_intern_static_string().
4396  **/
4397 void
4398 gtk_widget_set_accel_path (GtkWidget     *widget,
4399                            const gchar   *accel_path,
4400                            GtkAccelGroup *accel_group)
4401 {
4402   AccelPath *apath;
4403
4404   g_return_if_fail (GTK_IS_WIDGET (widget));
4405   g_return_if_fail (GTK_WIDGET_GET_CLASS (widget)->activate_signal != 0);
4406
4407   if (accel_path)
4408     {
4409       g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
4410       g_return_if_fail (_gtk_accel_path_is_valid (accel_path));
4411
4412       gtk_accel_map_add_entry (accel_path, 0, 0);
4413       apath = g_slice_new (AccelPath);
4414       apath->accel_group = g_object_ref (accel_group);
4415       apath->path_quark = g_quark_from_string (accel_path);
4416       apath->closure = widget_new_accel_closure (widget, GTK_WIDGET_GET_CLASS (widget)->activate_signal);
4417     }
4418   else
4419     apath = NULL;
4420
4421   /* also removes possible old settings */
4422   g_object_set_qdata_full (G_OBJECT (widget), quark_accel_path, apath, destroy_accel_path);
4423
4424   if (apath)
4425     gtk_accel_group_connect_by_path (apath->accel_group, g_quark_to_string (apath->path_quark), apath->closure);
4426
4427   g_signal_emit (widget, widget_signals[ACCEL_CLOSURES_CHANGED], 0);
4428 }
4429
4430 const gchar*
4431 _gtk_widget_get_accel_path (GtkWidget *widget,
4432                             gboolean  *locked)
4433 {
4434   AccelPath *apath;
4435
4436   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
4437
4438   apath = g_object_get_qdata (G_OBJECT (widget), quark_accel_path);
4439   if (locked)
4440     *locked = apath ? apath->accel_group->lock_count > 0 : TRUE;
4441   return apath ? g_quark_to_string (apath->path_quark) : NULL;
4442 }
4443
4444 /**
4445  * gtk_widget_mnemonic_activate:
4446  * @widget: a #GtkWidget
4447  * @group_cycling:  %TRUE if there are other widgets with the same mnemonic
4448  *
4449  * Emits the #GtkWidget::mnemonic-activate signal.
4450  * 
4451  * The default handler for this signal activates the @widget if
4452  * @group_cycling is %FALSE, and just grabs the focus if @group_cycling
4453  * is %TRUE.
4454  *
4455  * Returns: %TRUE if the signal has been handled
4456  */
4457 gboolean
4458 gtk_widget_mnemonic_activate (GtkWidget *widget,
4459                               gboolean   group_cycling)
4460 {
4461   gboolean handled;
4462   
4463   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4464
4465   group_cycling = group_cycling != FALSE;
4466   if (!GTK_WIDGET_IS_SENSITIVE (widget))
4467     handled = TRUE;
4468   else
4469     g_signal_emit (widget,
4470                    widget_signals[MNEMONIC_ACTIVATE],
4471                    0,
4472                    group_cycling,
4473                    &handled);
4474   return handled;
4475 }
4476
4477 static gboolean
4478 gtk_widget_real_mnemonic_activate (GtkWidget *widget,
4479                                    gboolean   group_cycling)
4480 {
4481   if (!group_cycling && GTK_WIDGET_GET_CLASS (widget)->activate_signal)
4482     gtk_widget_activate (widget);
4483   else if (GTK_WIDGET_CAN_FOCUS (widget))
4484     gtk_widget_grab_focus (widget);
4485   else
4486     {
4487       g_warning ("widget `%s' isn't suitable for mnemonic activation",
4488                  G_OBJECT_TYPE_NAME (widget));
4489       gtk_widget_error_bell (widget);
4490     }
4491   return TRUE;
4492 }
4493
4494 static gboolean
4495 gtk_widget_real_key_press_event (GtkWidget         *widget,
4496                                  GdkEventKey       *event)
4497 {
4498   return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
4499 }
4500
4501 static gboolean
4502 gtk_widget_real_key_release_event (GtkWidget         *widget,
4503                                    GdkEventKey       *event)
4504 {
4505   return gtk_bindings_activate_event (GTK_OBJECT (widget), event);
4506 }
4507
4508 static gboolean
4509 gtk_widget_real_focus_in_event (GtkWidget     *widget,
4510                                 GdkEventFocus *event)
4511 {
4512   gtk_widget_queue_shallow_draw (widget);
4513
4514   return FALSE;
4515 }
4516
4517 static gboolean
4518 gtk_widget_real_focus_out_event (GtkWidget     *widget,
4519                                  GdkEventFocus *event)
4520 {
4521   gtk_widget_queue_shallow_draw (widget);
4522
4523   return FALSE;
4524 }
4525
4526 #define WIDGET_REALIZED_FOR_EVENT(widget, event) \
4527      (event->type == GDK_FOCUS_CHANGE || GTK_WIDGET_REALIZED(widget))
4528
4529 /**
4530  * gtk_widget_event:
4531  * @widget: a #GtkWidget
4532  * @event: a #GdkEvent
4533  * 
4534  * Rarely-used function. This function is used to emit
4535  * the event signals on a widget (those signals should never
4536  * be emitted without using this function to do so).
4537  * If you want to synthesize an event though, don't use this function;
4538  * instead, use gtk_main_do_event() so the event will behave as if
4539  * it were in the event queue. Don't synthesize expose events; instead,
4540  * use gdk_window_invalidate_rect() to invalidate a region of the
4541  * window.
4542  * 
4543  * Return value: return from the event signal emission (%TRUE if 
4544  *               the event was handled)
4545  **/
4546 gboolean
4547 gtk_widget_event (GtkWidget *widget,
4548                   GdkEvent  *event)
4549 {
4550   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
4551   g_return_val_if_fail (WIDGET_REALIZED_FOR_EVENT (widget, event), TRUE);
4552
4553   if (event->type == GDK_EXPOSE)
4554     {
4555       g_warning ("Events of type GDK_EXPOSE cannot be synthesized. To get "
4556                  "the same effect, call gdk_window_invalidate_rect/region(), "
4557                  "followed by gdk_window_process_updates().");
4558       return TRUE;
4559     }
4560   
4561   return gtk_widget_event_internal (widget, event);
4562 }
4563
4564
4565 /**
4566  * gtk_widget_send_expose:
4567  * @widget: a #GtkWidget
4568  * @event: a expose #GdkEvent
4569  * 
4570  * Very rarely-used function. This function is used to emit
4571  * an expose event signals on a widget. This function is not
4572  * normally used directly. The only time it is used is when
4573  * propagating an expose event to a child %NO_WINDOW widget, and
4574  * that is normally done using gtk_container_propagate_expose().
4575  *
4576  * If you want to force an area of a window to be redrawn, 
4577  * use gdk_window_invalidate_rect() or gdk_window_invalidate_region().
4578  * To cause the redraw to be done immediately, follow that call
4579  * with a call to gdk_window_process_updates().
4580  * 
4581  * Return value: return from the event signal emission (%TRUE if 
4582  *               the event was handled)
4583  **/
4584 gint
4585 gtk_widget_send_expose (GtkWidget *widget,
4586                         GdkEvent  *event)
4587 {
4588   g_return_val_if_fail (GTK_IS_WIDGET (widget), TRUE);
4589   g_return_val_if_fail (GTK_WIDGET_REALIZED (widget), TRUE);
4590   g_return_val_if_fail (event != NULL, TRUE);
4591   g_return_val_if_fail (event->type == GDK_EXPOSE, TRUE);
4592
4593   return gtk_widget_event_internal (widget, event);
4594 }
4595
4596 static gboolean
4597 event_window_is_still_viewable (GdkEvent *event)
4598 {
4599   /* Some programs, such as gnome-theme-manager, fake widgets
4600    * into exposing onto a pixmap by sending expose events with
4601    * event->window pointing to a pixmap
4602    */
4603   if (GDK_IS_PIXMAP (event->any.window))
4604     return event->type == GDK_EXPOSE;
4605   
4606   /* Check that we think the event's window is viewable before
4607    * delivering the event, to prevent suprises. We do this here
4608    * at the last moment, since the event may have been queued
4609    * up behind other events, held over a recursive main loop, etc.
4610    */
4611   switch (event->type)
4612     {
4613     case GDK_EXPOSE:
4614     case GDK_MOTION_NOTIFY:
4615     case GDK_BUTTON_PRESS:
4616     case GDK_2BUTTON_PRESS:
4617     case GDK_3BUTTON_PRESS:
4618     case GDK_KEY_PRESS:
4619     case GDK_ENTER_NOTIFY:
4620     case GDK_PROXIMITY_IN:
4621     case GDK_SCROLL:
4622       return event->any.window && gdk_window_is_viewable (event->any.window);
4623
4624 #if 0
4625     /* The following events are the second half of paired events;
4626      * we always deliver them to deal with widgets that clean up
4627      * on the second half.
4628      */
4629     case GDK_BUTTON_RELEASE:
4630     case GDK_KEY_RELEASE:
4631     case GDK_LEAVE_NOTIFY:
4632     case GDK_PROXIMITY_OUT:
4633 #endif      
4634       
4635     default:
4636       /* Remaining events would make sense on an not-viewable window,
4637        * or don't have an associated window.
4638        */
4639       return TRUE;
4640     }
4641 }
4642
4643 static gint
4644 gtk_widget_event_internal (GtkWidget *widget,
4645                            GdkEvent  *event)
4646 {
4647   gboolean return_val = FALSE;
4648
4649   /* We check only once for is-still-visible; if someone
4650    * hides the window in on of the signals on the widget,
4651    * they are responsible for returning TRUE to terminate
4652    * handling.
4653    */
4654   if (!event_window_is_still_viewable (event))
4655     return TRUE;
4656
4657   g_object_ref (widget);
4658
4659   g_signal_emit (widget, widget_signals[EVENT], 0, event, &return_val);
4660   return_val |= !WIDGET_REALIZED_FOR_EVENT (widget, event);
4661   if (!return_val)
4662     {
4663       gint signal_num;
4664
4665       switch (event->type)
4666         {
4667         case GDK_NOTHING:
4668           signal_num = -1;
4669           break;
4670         case GDK_BUTTON_PRESS:
4671         case GDK_2BUTTON_PRESS:
4672         case GDK_3BUTTON_PRESS:
4673           signal_num = BUTTON_PRESS_EVENT;
4674           break;
4675         case GDK_SCROLL:
4676           signal_num = SCROLL_EVENT;
4677           break;
4678         case GDK_BUTTON_RELEASE:
4679           signal_num = BUTTON_RELEASE_EVENT;
4680           break;
4681         case GDK_MOTION_NOTIFY:
4682           signal_num = MOTION_NOTIFY_EVENT;
4683           break;
4684         case GDK_DELETE:
4685           signal_num = DELETE_EVENT;
4686           break;
4687         case GDK_DESTROY:
4688           signal_num = DESTROY_EVENT;
4689           _gtk_tooltip_hide (widget);
4690           break;
4691         case GDK_KEY_PRESS:
4692           signal_num = KEY_PRESS_EVENT;
4693           break;
4694         case GDK_KEY_RELEASE:
4695           signal_num = KEY_RELEASE_EVENT;
4696           break;
4697         case GDK_ENTER_NOTIFY:
4698           signal_num = ENTER_NOTIFY_EVENT;
4699           break;
4700         case GDK_LEAVE_NOTIFY:
4701           signal_num = LEAVE_NOTIFY_EVENT;
4702           break;
4703         case GDK_FOCUS_CHANGE:
4704           signal_num = event->focus_change.in ? FOCUS_IN_EVENT : FOCUS_OUT_EVENT;
4705           if (event->focus_change.in)
4706             _gtk_tooltip_focus_in (widget);
4707           else
4708             _gtk_tooltip_focus_out (widget);
4709           break;
4710         case GDK_CONFIGURE:
4711           signal_num = CONFIGURE_EVENT;
4712           break;
4713         case GDK_MAP:
4714           signal_num = MAP_EVENT;
4715           break;
4716         case GDK_UNMAP:
4717           signal_num = UNMAP_EVENT;
4718           break;
4719         case GDK_WINDOW_STATE:
4720           signal_num = WINDOW_STATE_EVENT;
4721           break;
4722         case GDK_PROPERTY_NOTIFY:
4723           signal_num = PROPERTY_NOTIFY_EVENT;
4724           break;
4725         case GDK_SELECTION_CLEAR:
4726           signal_num = SELECTION_CLEAR_EVENT;
4727           break;
4728         case GDK_SELECTION_REQUEST:
4729           signal_num = SELECTION_REQUEST_EVENT;
4730           break;
4731         case GDK_SELECTION_NOTIFY:
4732           signal_num = SELECTION_NOTIFY_EVENT;
4733           break;
4734         case GDK_PROXIMITY_IN:
4735           signal_num = PROXIMITY_IN_EVENT;
4736           break;
4737         case GDK_PROXIMITY_OUT:
4738           signal_num = PROXIMITY_OUT_EVENT;
4739           break;
4740         case GDK_NO_EXPOSE:
4741           signal_num = NO_EXPOSE_EVENT;
4742           break;
4743         case GDK_CLIENT_EVENT:
4744           signal_num = CLIENT_EVENT;
4745           break;
4746         case GDK_EXPOSE:
4747           signal_num = EXPOSE_EVENT;
4748           break;
4749         case GDK_VISIBILITY_NOTIFY:
4750           signal_num = VISIBILITY_NOTIFY_EVENT;
4751           break;
4752         case GDK_GRAB_BROKEN:
4753           signal_num = GRAB_BROKEN;
4754           break;
4755         case GDK_DAMAGE:
4756           signal_num = DAMAGE_EVENT;
4757           break;
4758         default:
4759           g_warning ("gtk_widget_event(): unhandled event type: %d", event->type);
4760           signal_num = -1;
4761           break;
4762         }
4763       if (signal_num != -1)
4764         g_signal_emit (widget, widget_signals[signal_num], 0, event, &return_val);
4765     }
4766   if (WIDGET_REALIZED_FOR_EVENT (widget, event))
4767     g_signal_emit (widget, widget_signals[EVENT_AFTER], 0, event);
4768   else
4769     return_val = TRUE;
4770
4771   g_object_unref (widget);
4772
4773   return return_val;
4774 }
4775
4776 /**
4777  * gtk_widget_activate:
4778  * @widget: a #GtkWidget that's activatable
4779  * 
4780  * For widgets that can be "activated" (buttons, menu items, etc.)
4781  * this function activates them. Activation is what happens when you
4782  * press Enter on a widget during key navigation. If @widget isn't 
4783  * activatable, the function returns %FALSE.
4784  * 
4785  * Return value: %TRUE if the widget was activatable
4786  **/
4787 gboolean
4788 gtk_widget_activate (GtkWidget *widget)
4789 {
4790   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4791   
4792   if (WIDGET_CLASS (widget)->activate_signal)
4793     {
4794       /* FIXME: we should eventually check the signals signature here */
4795       g_signal_emit (widget, WIDGET_CLASS (widget)->activate_signal, 0);
4796
4797       return TRUE;
4798     }
4799   else
4800     return FALSE;
4801 }
4802
4803 /**
4804  * gtk_widget_set_scroll_adjustments:
4805  * @widget: a #GtkWidget
4806  * @hadjustment: an adjustment for horizontal scrolling, or %NULL
4807  * @vadjustment: an adjustment for vertical scrolling, or %NULL
4808  *
4809  * For widgets that support scrolling, sets the scroll adjustments and
4810  * returns %TRUE.  For widgets that don't support scrolling, does
4811  * nothing and returns %FALSE. Widgets that don't support scrolling
4812  * can be scrolled by placing them in a #GtkViewport, which does
4813  * support scrolling.
4814  * 
4815  * Return value: %TRUE if the widget supports scrolling
4816  **/
4817 gboolean
4818 gtk_widget_set_scroll_adjustments (GtkWidget     *widget,
4819                                    GtkAdjustment *hadjustment,
4820                                    GtkAdjustment *vadjustment)
4821 {
4822   guint signal_id;
4823   GSignalQuery query;
4824
4825   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4826
4827   if (hadjustment)
4828     g_return_val_if_fail (GTK_IS_ADJUSTMENT (hadjustment), FALSE);
4829   if (vadjustment)
4830     g_return_val_if_fail (GTK_IS_ADJUSTMENT (vadjustment), FALSE);
4831
4832   signal_id = WIDGET_CLASS (widget)->set_scroll_adjustments_signal;
4833   if (!signal_id)
4834     return FALSE;
4835
4836   g_signal_query (signal_id, &query);
4837   if (!query.signal_id ||
4838       !g_type_is_a (query.itype, GTK_TYPE_WIDGET) ||
4839       query.return_type != G_TYPE_NONE ||
4840       query.n_params != 2 ||
4841       query.param_types[0] != GTK_TYPE_ADJUSTMENT ||
4842       query.param_types[1] != GTK_TYPE_ADJUSTMENT)
4843     {
4844       g_warning (G_STRLOC ": signal \"%s::%s\" has wrong signature",
4845                  G_OBJECT_TYPE_NAME (widget), query.signal_name);
4846       return FALSE;
4847     }
4848       
4849   g_signal_emit (widget, signal_id, 0, hadjustment, vadjustment);
4850   return TRUE;
4851 }
4852
4853 static void
4854 gtk_widget_reparent_subwindows (GtkWidget *widget,
4855                                 GdkWindow *new_window)
4856 {
4857   if (GTK_WIDGET_NO_WINDOW (widget))
4858     {
4859       GList *children = gdk_window_get_children (widget->window);
4860       GList *tmp_list;
4861
4862       for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
4863         {
4864           GdkWindow *window = tmp_list->data;
4865           gpointer child;
4866
4867           gdk_window_get_user_data (window, &child);
4868           while (child && child != widget)
4869             child = ((GtkWidget*) child)->parent;
4870
4871           if (child)
4872             gdk_window_reparent (window, new_window, 0, 0);
4873         }
4874
4875       g_list_free (children);
4876     }
4877   else
4878    {
4879      GdkWindow *parent;
4880      GList *tmp_list, *children;
4881
4882      parent = gdk_window_get_parent (widget->window);
4883
4884      if (parent == NULL)
4885        gdk_window_reparent (widget->window, new_window, 0, 0);
4886      else
4887        {
4888          children = gdk_window_get_children (parent);
4889          
4890          for (tmp_list = children; tmp_list; tmp_list = tmp_list->next)
4891            {
4892              GdkWindow *window = tmp_list->data;
4893              gpointer child;
4894
4895              gdk_window_get_user_data (window, &child);
4896
4897              if (child == widget)
4898                gdk_window_reparent (window, new_window, 0, 0);
4899            }
4900          
4901          g_list_free (children);
4902        }
4903    }
4904 }
4905
4906 static void
4907 gtk_widget_reparent_fixup_child (GtkWidget *widget,
4908                                  gpointer   client_data)
4909 {
4910   g_assert (client_data != NULL);
4911   
4912   if (GTK_WIDGET_NO_WINDOW (widget))
4913     {
4914       if (widget->window)
4915         g_object_unref (widget->window);
4916       widget->window = (GdkWindow*) client_data;
4917       if (widget->window)
4918         g_object_ref (widget->window);
4919
4920       if (GTK_IS_CONTAINER (widget))
4921         gtk_container_forall (GTK_CONTAINER (widget),
4922                               gtk_widget_reparent_fixup_child,
4923                               client_data);
4924     }
4925 }
4926
4927 /**
4928  * gtk_widget_reparent:
4929  * @widget: a #GtkWidget
4930  * @new_parent: a #GtkContainer to move the widget into
4931  *
4932  * Moves a widget from one #GtkContainer to another, handling reference
4933  * count issues to avoid destroying the widget.
4934  **/
4935 void
4936 gtk_widget_reparent (GtkWidget *widget,
4937                      GtkWidget *new_parent)
4938 {
4939   g_return_if_fail (GTK_IS_WIDGET (widget));
4940   g_return_if_fail (GTK_IS_CONTAINER (new_parent));
4941   g_return_if_fail (widget->parent != NULL);
4942   
4943   if (widget->parent != new_parent)
4944     {
4945       /* First try to see if we can get away without unrealizing
4946        * the widget as we reparent it. if so we set a flag so
4947        * that gtk_widget_unparent doesn't unrealize widget
4948        */
4949       if (GTK_WIDGET_REALIZED (widget) && GTK_WIDGET_REALIZED (new_parent))
4950         GTK_PRIVATE_SET_FLAG (widget, GTK_IN_REPARENT);
4951       
4952       g_object_ref (widget);
4953       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
4954       gtk_container_add (GTK_CONTAINER (new_parent), widget);
4955       g_object_unref (widget);
4956       
4957       if (GTK_WIDGET_IN_REPARENT (widget))
4958         {
4959           GTK_PRIVATE_UNSET_FLAG (widget, GTK_IN_REPARENT);
4960
4961           gtk_widget_reparent_subwindows (widget, gtk_widget_get_parent_window (widget));
4962           gtk_widget_reparent_fixup_child (widget,
4963                                            gtk_widget_get_parent_window (widget));
4964         }
4965
4966       g_object_notify (G_OBJECT (widget), "parent");
4967     }
4968 }
4969
4970 /**
4971  * gtk_widget_intersect:
4972  * @widget: a #GtkWidget
4973  * @area: a rectangle
4974  * @intersection: rectangle to store intersection of @widget and @area
4975  * 
4976  * Computes the intersection of a @widget's area and @area, storing
4977  * the intersection in @intersection, and returns %TRUE if there was
4978  * an intersection.  @intersection may be %NULL if you're only
4979  * interested in whether there was an intersection.
4980  * 
4981  * Return value: %TRUE if there was an intersection
4982  **/
4983 gboolean
4984 gtk_widget_intersect (GtkWidget          *widget,
4985                       const GdkRectangle *area,
4986                       GdkRectangle       *intersection)
4987 {
4988   GdkRectangle *dest;
4989   GdkRectangle tmp;
4990   gint return_val;
4991   
4992   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
4993   g_return_val_if_fail (area != NULL, FALSE);
4994   
4995   if (intersection)
4996     dest = intersection;
4997   else
4998     dest = &tmp;
4999   
5000   return_val = gdk_rectangle_intersect (&widget->allocation, area, dest);
5001   
5002   if (return_val && intersection && !GTK_WIDGET_NO_WINDOW (widget))
5003     {
5004       intersection->x -= widget->allocation.x;
5005       intersection->y -= widget->allocation.y;
5006     }
5007   
5008   return return_val;
5009 }
5010
5011 /**
5012  * gtk_widget_region_intersect:
5013  * @widget: a #GtkWidget
5014  * @region: a #GdkRegion, in the same coordinate system as 
5015  *          @widget->allocation. That is, relative to @widget->window
5016  *          for %NO_WINDOW widgets; relative to the parent window
5017  *          of @widget->window for widgets with their own window.
5018  * @returns: A newly allocated region holding the intersection of @widget
5019  *           and @region. The coordinates of the return value are
5020  *           relative to @widget->window for %NO_WINDOW widgets, and
5021  *           relative to the parent window of @widget->window for
5022  *           widgets with their own window.
5023  * 
5024  * Computes the intersection of a @widget's area and @region, returning
5025  * the intersection. The result may be empty, use gdk_region_empty() to
5026  * check.
5027  **/
5028 GdkRegion *
5029 gtk_widget_region_intersect (GtkWidget       *widget,
5030                              const GdkRegion *region)
5031 {
5032   GdkRectangle rect;
5033   GdkRegion *dest;
5034   
5035   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5036   g_return_val_if_fail (region != NULL, NULL);
5037
5038   gtk_widget_get_draw_rectangle (widget, &rect);
5039   
5040   dest = gdk_region_rectangle (&rect);
5041  
5042   gdk_region_intersect (dest, region);
5043
5044   return dest;
5045 }
5046
5047 /**
5048  * _gtk_widget_grab_notify:
5049  * @widget: a #GtkWidget
5050  * @was_grabbed: whether a grab is now in effect
5051  * 
5052  * Emits the #GtkWidget::grab-notify signal on @widget.
5053  * 
5054  * Since: 2.6
5055  **/
5056 void
5057 _gtk_widget_grab_notify (GtkWidget *widget,
5058                          gboolean   was_grabbed)
5059 {
5060   g_signal_emit (widget, widget_signals[GRAB_NOTIFY], 0, was_grabbed);
5061 }
5062
5063 /**
5064  * gtk_widget_grab_focus:
5065  * @widget: a #GtkWidget
5066  * 
5067  * Causes @widget to have the keyboard focus for the #GtkWindow it's
5068  * inside. @widget must be a focusable widget, such as a #GtkEntry;
5069  * something like #GtkFrame won't work. (More precisely, it must have the
5070  * %GTK_CAN_FOCUS flag set.)
5071  **/
5072 void
5073 gtk_widget_grab_focus (GtkWidget *widget)
5074 {
5075   g_return_if_fail (GTK_IS_WIDGET (widget));
5076
5077   if (!GTK_WIDGET_IS_SENSITIVE (widget))
5078     return;
5079   
5080   g_object_ref (widget);
5081   g_signal_emit (widget, widget_signals[GRAB_FOCUS], 0);
5082   g_object_notify (G_OBJECT (widget), "has-focus");
5083   g_object_unref (widget);
5084 }
5085
5086 static void
5087 reset_focus_recurse (GtkWidget *widget,
5088                      gpointer   data)
5089 {
5090   if (GTK_IS_CONTAINER (widget))
5091     {
5092       GtkContainer *container;
5093
5094       container = GTK_CONTAINER (widget);
5095       gtk_container_set_focus_child (container, NULL);
5096
5097       gtk_container_foreach (container,
5098                              reset_focus_recurse,
5099                              NULL);
5100     }
5101 }
5102
5103 static void
5104 gtk_widget_real_grab_focus (GtkWidget *focus_widget)
5105 {
5106   if (GTK_WIDGET_CAN_FOCUS (focus_widget))
5107     {
5108       GtkWidget *toplevel;
5109       GtkWidget *widget;
5110       
5111       /* clear the current focus setting, break if the current widget
5112        * is the focus widget's parent, since containers above that will
5113        * be set by the next loop.
5114        */
5115       toplevel = gtk_widget_get_toplevel (focus_widget);
5116       if (GTK_WIDGET_TOPLEVEL (toplevel) && GTK_IS_WINDOW (toplevel))
5117         {
5118           widget = GTK_WINDOW (toplevel)->focus_widget;
5119           
5120           if (widget == focus_widget)
5121             {
5122               /* We call _gtk_window_internal_set_focus() here so that the
5123                * toplevel window can request the focus if necessary.
5124                * This is needed when the toplevel is a GtkPlug
5125                */
5126               if (!GTK_WIDGET_HAS_FOCUS (widget))
5127                 _gtk_window_internal_set_focus (GTK_WINDOW (toplevel), focus_widget);
5128
5129               return;
5130             }
5131           
5132           if (widget)
5133             {
5134               while (widget->parent && widget->parent != focus_widget->parent)
5135                 {
5136                   widget = widget->parent;
5137                   gtk_container_set_focus_child (GTK_CONTAINER (widget), NULL);
5138                 }
5139             }
5140         }
5141       else if (toplevel != focus_widget)
5142         {
5143           /* gtk_widget_grab_focus() operates on a tree without window...
5144            * actually, this is very questionable behaviour.
5145            */
5146           
5147           gtk_container_foreach (GTK_CONTAINER (toplevel),
5148                                  reset_focus_recurse,
5149                                  NULL);
5150         }
5151
5152       /* now propagate the new focus up the widget tree and finally
5153        * set it on the window
5154        */
5155       widget = focus_widget;
5156       while (widget->parent)
5157         {
5158           gtk_container_set_focus_child (GTK_CONTAINER (widget->parent), widget);
5159           widget = widget->parent;
5160         }
5161       if (GTK_IS_WINDOW (widget))
5162         _gtk_window_internal_set_focus (GTK_WINDOW (widget), focus_widget);
5163     }
5164 }
5165
5166 static gboolean
5167 gtk_widget_real_query_tooltip (GtkWidget  *widget,
5168                                gint        x,
5169                                gint        y,
5170                                gboolean    keyboard_tip,
5171                                GtkTooltip *tooltip)
5172 {
5173   gchar *tooltip_markup;
5174   gboolean has_tooltip;
5175
5176   tooltip_markup = g_object_get_qdata (G_OBJECT (widget), quark_tooltip_markup);
5177   has_tooltip = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget), quark_has_tooltip));
5178
5179   if (has_tooltip && tooltip_markup)
5180     {
5181       gtk_tooltip_set_markup (tooltip, tooltip_markup);
5182       return TRUE;
5183     }
5184
5185   return FALSE;
5186 }
5187
5188 static gboolean
5189 gtk_widget_real_show_help (GtkWidget        *widget,
5190                            GtkWidgetHelpType help_type)
5191 {
5192   if (help_type == GTK_WIDGET_HELP_TOOLTIP)
5193     {
5194       _gtk_tooltip_toggle_keyboard_mode (widget);
5195
5196       return TRUE;
5197     }
5198   else
5199     return FALSE;
5200 }
5201
5202 static gboolean
5203 gtk_widget_real_focus (GtkWidget         *widget,
5204                        GtkDirectionType   direction)
5205 {
5206   if (!GTK_WIDGET_CAN_FOCUS (widget))
5207     return FALSE;
5208   
5209   if (!gtk_widget_is_focus (widget))
5210     {
5211       gtk_widget_grab_focus (widget);
5212       return TRUE;
5213     }
5214   else
5215     return FALSE;
5216 }
5217
5218 static void
5219 gtk_widget_real_move_focus (GtkWidget         *widget,
5220                             GtkDirectionType   direction)
5221 {
5222   GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
5223
5224   if (GTK_IS_WINDOW (toplevel) &&
5225       GTK_WINDOW_GET_CLASS (toplevel)->move_focus)
5226     {
5227       GTK_WINDOW_GET_CLASS (toplevel)->move_focus (GTK_WINDOW (toplevel),
5228                                                    direction);
5229     }
5230 }
5231
5232 static gboolean
5233 gtk_widget_real_keynav_failed (GtkWidget        *widget,
5234                                GtkDirectionType  direction)
5235 {
5236   gboolean cursor_only;
5237
5238   switch (direction)
5239     {
5240     case GTK_DIR_TAB_FORWARD:
5241     case GTK_DIR_TAB_BACKWARD:
5242       return FALSE;
5243
5244     case GTK_DIR_UP:
5245     case GTK_DIR_DOWN:
5246     case GTK_DIR_LEFT:
5247     case GTK_DIR_RIGHT:
5248       g_object_get (gtk_widget_get_settings (widget),
5249                     "gtk-keynav-cursor-only", &cursor_only,
5250                     NULL);
5251       if (cursor_only)
5252         return FALSE;
5253       break;
5254     }
5255
5256   gtk_widget_error_bell (widget);
5257
5258   return TRUE;
5259 }
5260
5261 /**
5262  * gtk_widget_is_focus:
5263  * @widget: a #GtkWidget
5264  * 
5265  * Determines if the widget is the focus widget within its
5266  * toplevel. (This does not mean that the %HAS_FOCUS flag is
5267  * necessarily set; %HAS_FOCUS will only be set if the
5268  * toplevel widget additionally has the global input focus.)
5269  * 
5270  * Return value: %TRUE if the widget is the focus widget.
5271  **/
5272 gboolean
5273 gtk_widget_is_focus (GtkWidget *widget)
5274 {
5275   GtkWidget *toplevel;
5276
5277   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
5278
5279   toplevel = gtk_widget_get_toplevel (widget);
5280   
5281   if (GTK_IS_WINDOW (toplevel))
5282     return widget == GTK_WINDOW (toplevel)->focus_widget;
5283   else
5284     return FALSE;
5285 }
5286
5287 /**
5288  * gtk_widget_grab_default:
5289  * @widget: a #GtkWidget
5290  *
5291  * Causes @widget to become the default widget. @widget must have the
5292  * %GTK_CAN_DEFAULT flag set; typically you have to set this flag
5293  * yourself by calling <literal>GTK_WIDGET_SET_FLAGS (@widget,
5294  * GTK_CAN_DEFAULT)</literal>. The default widget is activated when 
5295  * the user presses Enter in a window. Default widgets must be 
5296  * activatable, that is, gtk_widget_activate() should affect them.
5297  **/
5298 void
5299 gtk_widget_grab_default (GtkWidget *widget)
5300 {
5301   GtkWidget *window;
5302   
5303   g_return_if_fail (GTK_IS_WIDGET (widget));
5304   g_return_if_fail (GTK_WIDGET_CAN_DEFAULT (widget));
5305   
5306   window = gtk_widget_get_toplevel (widget);
5307   
5308   if (window && GTK_WIDGET_TOPLEVEL (window))
5309     gtk_window_set_default (GTK_WINDOW (window), widget);
5310   else
5311     g_warning (G_STRLOC ": widget not within a GtkWindow");
5312 }
5313
5314 /**
5315  * gtk_widget_set_name:
5316  * @widget: a #GtkWidget
5317  * @name: name for the widget
5318  *
5319  * Widgets can be named, which allows you to refer to them from a
5320  * gtkrc file. You can apply a style to widgets with a particular name
5321  * in the gtkrc file. See the documentation for gtkrc files (on the
5322  * same page as the docs for #GtkRcStyle).
5323  * 
5324  * Note that widget names are separated by periods in paths (see 
5325  * gtk_widget_path()), so names with embedded periods may cause confusion.
5326  **/
5327 void
5328 gtk_widget_set_name (GtkWidget   *widget,
5329                      const gchar *name)
5330 {
5331   gchar *new_name;
5332   
5333   g_return_if_fail (GTK_IS_WIDGET (widget));
5334
5335   new_name = g_strdup (name);
5336   g_free (widget->name);
5337   widget->name = new_name;
5338
5339   if (GTK_WIDGET_RC_STYLE (widget))
5340     gtk_widget_reset_rc_style (widget);
5341
5342   g_object_notify (G_OBJECT (widget), "name");
5343 }
5344
5345 /**
5346  * gtk_widget_get_name:
5347  * @widget: a #GtkWidget
5348  * 
5349  * Retrieves the name of a widget. See gtk_widget_set_name() for the
5350  * significance of widget names.
5351  * 
5352  * Return value: name of the widget. This string is owned by GTK+ and
5353  * should not be modified or freed
5354  **/
5355 G_CONST_RETURN gchar*
5356 gtk_widget_get_name (GtkWidget *widget)
5357 {
5358   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5359   
5360   if (widget->name)
5361     return widget->name;
5362   return g_type_name (GTK_WIDGET_TYPE (widget));
5363 }
5364
5365 /**
5366  * gtk_widget_set_state:
5367  * @widget: a #GtkWidget
5368  * @state: new state for @widget
5369  *
5370  * This function is for use in widget implementations. Sets the state
5371  * of a widget (insensitive, prelighted, etc.) Usually you should set
5372  * the state using wrapper functions such as gtk_widget_set_sensitive().
5373  **/
5374 void
5375 gtk_widget_set_state (GtkWidget           *widget,
5376                       GtkStateType         state)
5377 {
5378   g_return_if_fail (GTK_IS_WIDGET (widget));
5379
5380   if (state == GTK_WIDGET_STATE (widget))
5381     return;
5382
5383   if (state == GTK_STATE_INSENSITIVE)
5384     gtk_widget_set_sensitive (widget, FALSE);
5385   else
5386     {
5387       GtkStateData data;
5388
5389       data.state = state;
5390       data.state_restoration = FALSE;
5391       data.use_forall = FALSE;
5392       if (widget->parent)
5393         data.parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (widget->parent) != FALSE);
5394       else
5395         data.parent_sensitive = TRUE;
5396
5397       gtk_widget_propagate_state (widget, &data);
5398   
5399       if (GTK_WIDGET_DRAWABLE (widget))
5400         gtk_widget_queue_draw (widget);
5401     }
5402 }
5403
5404
5405 /**
5406  * gtk_widget_set_app_paintable:
5407  * @widget: a #GtkWidget
5408  * @app_paintable: %TRUE if the application will paint on the widget
5409  *
5410  * Sets whether the application intends to draw on the widget in
5411  * an #GtkWidget::expose-event handler. 
5412  *
5413  * This is a hint to the widget and does not affect the behavior of 
5414  * the GTK+ core; many widgets ignore this flag entirely. For widgets 
5415  * that do pay attention to the flag, such as #GtkEventBox and #GtkWindow, 
5416  * the effect is to suppress default themed drawing of the widget's 
5417  * background. (Children of the widget will still be drawn.) The application 
5418  * is then entirely responsible for drawing the widget background.
5419  *
5420  * Note that the background is still drawn when the widget is mapped.
5421  * If this is not suitable (e.g. because you want to make a transparent
5422  * window using an RGBA visual), you can work around this by doing:
5423  * |[
5424  *  gtk_widget_realize (window);
5425  *  gdk_window_set_back_pixmap (window->window, NULL, FALSE);
5426  *  gtk_widget_show (window);
5427  * ]|
5428  **/
5429 void
5430 gtk_widget_set_app_paintable (GtkWidget *widget,
5431                               gboolean   app_paintable)
5432 {
5433   g_return_if_fail (GTK_IS_WIDGET (widget));
5434
5435   app_paintable = (app_paintable != FALSE);
5436
5437   if (GTK_WIDGET_APP_PAINTABLE (widget) != app_paintable)
5438     {
5439       if (app_paintable)
5440         GTK_WIDGET_SET_FLAGS (widget, GTK_APP_PAINTABLE);
5441       else
5442         GTK_WIDGET_UNSET_FLAGS (widget, GTK_APP_PAINTABLE);
5443
5444       if (GTK_WIDGET_DRAWABLE (widget))
5445         gtk_widget_queue_draw (widget);
5446
5447       g_object_notify (G_OBJECT (widget), "app-paintable");
5448     }
5449 }
5450
5451 /**
5452  * gtk_widget_set_double_buffered:
5453  * @widget: a #GtkWidget
5454  * @double_buffered: %TRUE to double-buffer a widget
5455  *
5456  * Widgets are double buffered by default; you can use this function
5457  * to turn off the buffering. "Double buffered" simply means that
5458  * gdk_window_begin_paint_region() and gdk_window_end_paint() are called
5459  * automatically around expose events sent to the
5460  * widget. gdk_window_begin_paint() diverts all drawing to a widget's
5461  * window to an offscreen buffer, and gdk_window_end_paint() draws the
5462  * buffer to the screen. The result is that users see the window
5463  * update in one smooth step, and don't see individual graphics
5464  * primitives being rendered.
5465  *
5466  * In very simple terms, double buffered widgets don't flicker,
5467  * so you would only use this function to turn off double buffering
5468  * if you had special needs and really knew what you were doing.
5469  * 
5470  * Note: if you turn off double-buffering, you have to handle
5471  * expose events, since even the clearing to the background color or 
5472  * pixmap will not happen automatically (as it is done in 
5473  * gdk_window_begin_paint()).
5474  **/
5475 void
5476 gtk_widget_set_double_buffered (GtkWidget *widget,
5477                                 gboolean   double_buffered)
5478 {
5479   g_return_if_fail (GTK_IS_WIDGET (widget));
5480
5481   if (double_buffered)
5482     GTK_WIDGET_SET_FLAGS (widget, GTK_DOUBLE_BUFFERED);
5483   else
5484     GTK_WIDGET_UNSET_FLAGS (widget, GTK_DOUBLE_BUFFERED);
5485 }
5486
5487 /**
5488  * gtk_widget_set_redraw_on_allocate:
5489  * @widget: a #GtkWidget
5490  * @redraw_on_allocate: if %TRUE, the entire widget will be redrawn
5491  *   when it is allocated to a new size. Otherwise, only the
5492  *   new portion of the widget will be redrawn.
5493  *
5494  * Sets whether the entire widget is queued for drawing when its size 
5495  * allocation changes. By default, this setting is %TRUE and
5496  * the entire widget is redrawn on every size change. If your widget
5497  * leaves the upper left unchanged when made bigger, turning this
5498  * setting off will improve performance.
5499
5500  * Note that for %NO_WINDOW widgets setting this flag to %FALSE turns
5501  * off all allocation on resizing: the widget will not even redraw if
5502  * its position changes; this is to allow containers that don't draw
5503  * anything to avoid excess invalidations. If you set this flag on a
5504  * %NO_WINDOW widget that <emphasis>does</emphasis> draw on @widget->window, 
5505  * you are responsible for invalidating both the old and new allocation 
5506  * of the widget when the widget is moved and responsible for invalidating
5507  * regions newly when the widget increases size.
5508  **/
5509 void
5510 gtk_widget_set_redraw_on_allocate (GtkWidget *widget,
5511                                    gboolean   redraw_on_allocate)
5512 {
5513   g_return_if_fail (GTK_IS_WIDGET (widget));
5514
5515   if (redraw_on_allocate)
5516     GTK_PRIVATE_SET_FLAG (widget, GTK_REDRAW_ON_ALLOC);
5517   else
5518     GTK_PRIVATE_UNSET_FLAG (widget, GTK_REDRAW_ON_ALLOC);
5519 }
5520
5521 /**
5522  * gtk_widget_set_sensitive:
5523  * @widget: a #GtkWidget
5524  * @sensitive: %TRUE to make the widget sensitive
5525  *
5526  * Sets the sensitivity of a widget. A widget is sensitive if the user
5527  * can interact with it. Insensitive widgets are "grayed out" and the
5528  * user can't interact with them. Insensitive widgets are known as
5529  * "inactive", "disabled", or "ghosted" in some other toolkits.
5530  **/
5531 void
5532 gtk_widget_set_sensitive (GtkWidget *widget,
5533                           gboolean   sensitive)
5534 {
5535   GtkStateData data;
5536
5537   g_return_if_fail (GTK_IS_WIDGET (widget));
5538
5539   sensitive = (sensitive != FALSE);
5540
5541   if (sensitive == (GTK_WIDGET_SENSITIVE (widget) != FALSE))
5542     return;
5543
5544   if (sensitive)
5545     {
5546       GTK_WIDGET_SET_FLAGS (widget, GTK_SENSITIVE);
5547       data.state = GTK_WIDGET_SAVED_STATE (widget);
5548     }
5549   else
5550     {
5551       GTK_WIDGET_UNSET_FLAGS (widget, GTK_SENSITIVE);
5552       data.state = GTK_WIDGET_STATE (widget);
5553     }
5554   data.state_restoration = TRUE;
5555   data.use_forall = TRUE;
5556
5557   if (widget->parent)
5558     data.parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (widget->parent) != FALSE);
5559   else
5560     data.parent_sensitive = TRUE;
5561
5562   gtk_widget_propagate_state (widget, &data);
5563   if (GTK_WIDGET_DRAWABLE (widget))
5564     gtk_widget_queue_draw (widget);
5565
5566   g_object_notify (G_OBJECT (widget), "sensitive");
5567 }
5568
5569 /**
5570  * gtk_widget_set_parent:
5571  * @widget: a #GtkWidget
5572  * @parent: parent container
5573  *
5574  * This function is useful only when implementing subclasses of 
5575  * #GtkContainer.
5576  * Sets the container as the parent of @widget, and takes care of
5577  * some details such as updating the state and style of the child
5578  * to reflect its new location. The opposite function is
5579  * gtk_widget_unparent().
5580  **/
5581 void
5582 gtk_widget_set_parent (GtkWidget *widget,
5583                        GtkWidget *parent)
5584 {
5585   GtkStateData data;
5586   
5587   g_return_if_fail (GTK_IS_WIDGET (widget));
5588   g_return_if_fail (GTK_IS_WIDGET (parent));
5589   g_return_if_fail (widget != parent);
5590   if (widget->parent != NULL)
5591     {
5592       g_warning ("Can't set a parent on widget which has a parent\n");
5593       return;
5594     }
5595   if (GTK_WIDGET_TOPLEVEL (widget))
5596     {
5597       g_warning ("Can't set a parent on a toplevel widget\n");
5598       return;
5599     }
5600
5601   /* keep this function in sync with gtk_menu_attach_to_widget()
5602    */
5603
5604   g_object_ref_sink (widget);
5605   widget->parent = parent;
5606
5607   if (GTK_WIDGET_STATE (parent) != GTK_STATE_NORMAL)
5608     data.state = GTK_WIDGET_STATE (parent);
5609   else
5610     data.state = GTK_WIDGET_STATE (widget);
5611   data.state_restoration = FALSE;
5612   data.parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (parent) != FALSE);
5613   data.use_forall = GTK_WIDGET_IS_SENSITIVE (parent) != GTK_WIDGET_IS_SENSITIVE (widget);
5614
5615   gtk_widget_propagate_state (widget, &data);
5616   
5617   gtk_widget_reset_rc_styles (widget);
5618
5619   g_signal_emit (widget, widget_signals[PARENT_SET], 0, NULL);
5620   if (GTK_WIDGET_ANCHORED (widget->parent))
5621     _gtk_widget_propagate_hierarchy_changed (widget, NULL);
5622   g_object_notify (G_OBJECT (widget), "parent");
5623
5624   /* Enforce realized/mapped invariants
5625    */
5626   if (GTK_WIDGET_REALIZED (widget->parent))
5627     gtk_widget_realize (widget);
5628
5629   if (GTK_WIDGET_VISIBLE (widget->parent) &&
5630       GTK_WIDGET_VISIBLE (widget))
5631     {
5632       if (GTK_WIDGET_CHILD_VISIBLE (widget) &&
5633           GTK_WIDGET_MAPPED (widget->parent))
5634         gtk_widget_map (widget);
5635
5636       gtk_widget_queue_resize (widget);
5637     }
5638 }
5639
5640 /**
5641  * gtk_widget_get_parent:
5642  * @widget: a #GtkWidget
5643  *
5644  * Returns the parent container of @widget.
5645  *
5646  * Return value: the parent container of @widget, or %NULL
5647  **/
5648 GtkWidget *
5649 gtk_widget_get_parent (GtkWidget *widget)
5650 {
5651   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5652
5653   return widget->parent;
5654 }
5655
5656 /*****************************************
5657  * Widget styles
5658  * see docs/styles.txt
5659  *****************************************/
5660
5661 /**
5662  * gtk_widget_set_style:
5663  * @widget: a #GtkWidget
5664  * @style: a #GtkStyle, or %NULL to remove the effect of a previous
5665  *         gtk_widget_set_style() and go back to the default style
5666  *
5667  * Sets the #GtkStyle for a widget (@widget->style). You probably don't
5668  * want to use this function; it interacts badly with themes, because
5669  * themes work by replacing the #GtkStyle. Instead, use
5670  * gtk_widget_modify_style().
5671  **/
5672 void
5673 gtk_widget_set_style (GtkWidget *widget,
5674                       GtkStyle  *style)
5675 {
5676   g_return_if_fail (GTK_IS_WIDGET (widget));
5677
5678   if (style)
5679     {
5680       gboolean initial_emission;
5681
5682       initial_emission = !GTK_WIDGET_RC_STYLE (widget) && !GTK_WIDGET_USER_STYLE (widget);
5683       
5684       GTK_WIDGET_UNSET_FLAGS (widget, GTK_RC_STYLE);
5685       GTK_PRIVATE_SET_FLAG (widget, GTK_USER_STYLE);
5686       
5687       gtk_widget_set_style_internal (widget, style, initial_emission);
5688     }
5689   else
5690     {
5691       if (GTK_WIDGET_USER_STYLE (widget))
5692         gtk_widget_reset_rc_style (widget);
5693     }
5694 }
5695
5696 /**
5697  * gtk_widget_ensure_style:
5698  * @widget: a #GtkWidget
5699  *
5700  * Ensures that @widget has a style (@widget->style). Not a very useful
5701  * function; most of the time, if you want the style, the widget is
5702  * realized, and realized widgets are guaranteed to have a style
5703  * already.
5704  **/
5705 void
5706 gtk_widget_ensure_style (GtkWidget *widget)
5707 {
5708   g_return_if_fail (GTK_IS_WIDGET (widget));
5709
5710   if (!GTK_WIDGET_USER_STYLE (widget) &&
5711       !GTK_WIDGET_RC_STYLE (widget))
5712     gtk_widget_reset_rc_style (widget);
5713 }
5714
5715 /* Look up the RC style for this widget, unsetting any user style that
5716  * may be in effect currently
5717  **/
5718 static void
5719 gtk_widget_reset_rc_style (GtkWidget *widget)
5720 {
5721   GtkStyle *new_style = NULL;
5722   gboolean initial_emission;
5723   
5724   initial_emission = !GTK_WIDGET_RC_STYLE (widget) && !GTK_WIDGET_USER_STYLE (widget);
5725
5726   GTK_PRIVATE_UNSET_FLAG (widget, GTK_USER_STYLE);
5727   GTK_WIDGET_SET_FLAGS (widget, GTK_RC_STYLE);
5728   
5729   if (gtk_widget_has_screen (widget))
5730     new_style = gtk_rc_get_style (widget);
5731   if (!new_style)
5732     new_style = gtk_widget_get_default_style ();
5733
5734   if (initial_emission || new_style != widget->style)
5735     gtk_widget_set_style_internal (widget, new_style, initial_emission);
5736 }
5737
5738 /**
5739  * gtk_widget_get_style:
5740  * @widget: a #GtkWidget
5741  * 
5742  * Simply an accessor function that returns @widget->style.
5743  * 
5744  * Return value: the widget's #GtkStyle
5745  **/
5746 GtkStyle*
5747 gtk_widget_get_style (GtkWidget *widget)
5748 {
5749   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5750   
5751   return widget->style;
5752 }
5753
5754 /**
5755  * gtk_widget_modify_style:
5756  * @widget: a #GtkWidget
5757  * @style: the #GtkRcStyle holding the style modifications
5758  * 
5759  * Modifies style values on the widget. Modifications made using this
5760  * technique take precedence over style values set via an RC file,
5761  * however, they will be overriden if a style is explicitely set on
5762  * the widget using gtk_widget_set_style(). The #GtkRcStyle structure
5763  * is designed so each field can either be set or unset, so it is
5764  * possible, using this function, to modify some style values and
5765  * leave the others unchanged.
5766  *
5767  * Note that modifications made with this function are not cumulative
5768  * with previous calls to gtk_widget_modify_style() or with such
5769  * functions as gtk_widget_modify_fg(). If you wish to retain
5770  * previous values, you must first call gtk_widget_get_modifier_style(),
5771  * make your modifications to the returned style, then call
5772  * gtk_widget_modify_style() with that style. On the other hand,
5773  * if you first call gtk_widget_modify_style(), subsequent calls
5774  * to such functions gtk_widget_modify_fg() will have a cumulative
5775  * effect with the initial modifications.
5776  **/
5777 void       
5778 gtk_widget_modify_style (GtkWidget      *widget,
5779                          GtkRcStyle     *style)
5780 {
5781   g_return_if_fail (GTK_IS_WIDGET (widget));
5782   g_return_if_fail (GTK_IS_RC_STYLE (style));
5783   
5784   g_object_set_qdata_full (G_OBJECT (widget),
5785                            quark_rc_style,
5786                            gtk_rc_style_copy (style),
5787                            (GDestroyNotify) g_object_unref);
5788
5789   /* note that "style" may be invalid here if it was the old
5790    * modifier style and the only reference was our own.
5791    */
5792   
5793   if (GTK_WIDGET_RC_STYLE (widget))
5794     gtk_widget_reset_rc_style (widget);
5795 }
5796
5797 /**
5798  * gtk_widget_get_modifier_style:
5799  * @widget: a #GtkWidget
5800  * 
5801  * Returns the current modifier style for the widget. (As set by
5802  * gtk_widget_modify_style().) If no style has previously set, a new
5803  * #GtkRcStyle will be created with all values unset, and set as the
5804  * modifier style for the widget. If you make changes to this rc
5805  * style, you must call gtk_widget_modify_style(), passing in the
5806  * returned rc style, to make sure that your changes take effect.
5807  *
5808  * Caution: passing the style back to gtk_widget_modify_style() will
5809  * normally end up destroying it, because gtk_widget_modify_style() copies
5810  * the passed-in style and sets the copy as the new modifier style,
5811  * thus dropping any reference to the old modifier style. Add a reference
5812  * to the modifier style if you want to keep it alive.
5813  * 
5814  * Return value: the modifier style for the widget. This rc style is
5815  *   owned by the widget. If you want to keep a pointer to value this
5816  *   around, you must add a refcount using g_object_ref().
5817  **/
5818 GtkRcStyle *
5819 gtk_widget_get_modifier_style (GtkWidget      *widget)
5820 {
5821   GtkRcStyle *rc_style;
5822   
5823   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
5824
5825   rc_style = g_object_get_qdata (G_OBJECT (widget), quark_rc_style);
5826
5827   if (!rc_style)
5828     {
5829       rc_style = gtk_rc_style_new ();
5830       g_object_set_qdata_full (G_OBJECT (widget),
5831                                quark_rc_style,
5832                                rc_style,
5833                                (GDestroyNotify) g_object_unref);
5834     }
5835
5836   return rc_style;
5837 }
5838
5839 static void
5840 gtk_widget_modify_color_component (GtkWidget      *widget,
5841                                    GtkRcFlags      component,
5842                                    GtkStateType    state,
5843                                    const GdkColor *color)
5844 {
5845   GtkRcStyle *rc_style = gtk_widget_get_modifier_style (widget);  
5846
5847   if (color)
5848     {
5849       switch (component)
5850         {
5851         case GTK_RC_FG:
5852           rc_style->fg[state] = *color;
5853           break;
5854         case GTK_RC_BG:
5855           rc_style->bg[state] = *color;
5856           break;
5857         case GTK_RC_TEXT:
5858           rc_style->text[state] = *color;
5859           break;
5860         case GTK_RC_BASE:
5861           rc_style->base[state] = *color;
5862           break;
5863         default:
5864           g_assert_not_reached();
5865         }
5866       
5867       rc_style->color_flags[state] |= component;
5868     }
5869   else
5870     rc_style->color_flags[state] &= ~component;
5871
5872   gtk_widget_modify_style (widget, rc_style);
5873 }
5874
5875 /**
5876  * gtk_widget_modify_fg:
5877  * @widget: a #GtkWidget
5878  * @state: the state for which to set the foreground color
5879  * @color: the color to assign (does not need to be allocated),
5880  *         or %NULL to undo the effect of previous calls to
5881  *         of gtk_widget_modify_fg().
5882  * 
5883  * Sets the foreground color for a widget in a particular state.  
5884  * All other style values are left untouched. See also
5885  * gtk_widget_modify_style().
5886  **/
5887 void
5888 gtk_widget_modify_fg (GtkWidget      *widget,
5889                       GtkStateType    state,
5890                       const GdkColor *color)
5891 {
5892   g_return_if_fail (GTK_IS_WIDGET (widget));
5893   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
5894
5895   gtk_widget_modify_color_component (widget, GTK_RC_FG, state, color);
5896 }
5897
5898 /**
5899  * gtk_widget_modify_bg:
5900  * @widget: a #GtkWidget
5901  * @state: the state for which to set the background color
5902  * @color: the color to assign (does not need to be allocated),
5903  *         or %NULL to undo the effect of previous calls to
5904  *         of gtk_widget_modify_bg().
5905  * 
5906  * Sets the background color for a widget in a particular state.  
5907  * All other style values are left untouched. See also
5908  * gtk_widget_modify_style(). 
5909  *
5910  * Note that "no window" widgets (which have the %GTK_NO_WINDOW flag set)
5911  * draw on their parent container's window and thus may not draw any 
5912  * background themselves. This is the case for e.g. #GtkLabel. To modify 
5913  * the background of such widgets, you have to set the background color 
5914  * on their parent; if you want to set the background of a rectangular 
5915  * area around a label, try placing the label in a #GtkEventBox widget 
5916  * and setting the background color on that.
5917  **/
5918 void
5919 gtk_widget_modify_bg (GtkWidget      *widget,
5920                       GtkStateType    state,
5921                       const GdkColor *color)
5922 {
5923   g_return_if_fail (GTK_IS_WIDGET (widget));
5924   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
5925
5926   gtk_widget_modify_color_component (widget, GTK_RC_BG, state, color);
5927 }
5928
5929 /**
5930  * gtk_widget_modify_text:
5931  * @widget: a #GtkWidget
5932  * @state: the state for which to set the text color
5933  * @color: the color to assign (does not need to be allocated),
5934  *         or %NULL to undo the effect of previous calls to
5935  *         of gtk_widget_modify_text().
5936  * 
5937  * Sets the text color for a widget in a particular state.  All other
5938  * style values are left untouched. The text color is the foreground
5939  * color used along with the base color (see gtk_widget_modify_base())
5940  * for widgets such as #GtkEntry and #GtkTextView. See also
5941  * gtk_widget_modify_style().
5942  **/
5943 void
5944 gtk_widget_modify_text (GtkWidget      *widget,
5945                         GtkStateType    state,
5946                         const GdkColor *color)
5947 {
5948   g_return_if_fail (GTK_IS_WIDGET (widget));
5949   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
5950
5951   gtk_widget_modify_color_component (widget, GTK_RC_TEXT, state, color);
5952 }
5953
5954 /**
5955  * gtk_widget_modify_base:
5956  * @widget: a #GtkWidget
5957  * @state: the state for which to set the base color
5958  * @color: the color to assign (does not need to be allocated),
5959  *         or %NULL to undo the effect of previous calls to
5960  *         of gtk_widget_modify_base().
5961  * 
5962  * Sets the base color for a widget in a particular state.
5963  * All other style values are left untouched. The base color
5964  * is the background color used along with the text color
5965  * (see gtk_widget_modify_text()) for widgets such as #GtkEntry
5966  * and #GtkTextView. See also gtk_widget_modify_style().
5967  *
5968  * Note that "no window" widgets (which have the %GTK_NO_WINDOW flag set)
5969  * draw on their parent container's window and thus may not draw any 
5970  * background themselves. This is the case for e.g. #GtkLabel. To modify 
5971  * the background of such widgets, you have to set the base color on their 
5972  * parent; if you want to set the background of a rectangular area around 
5973  * a label, try placing the label in a #GtkEventBox widget and setting 
5974  * the base color on that.
5975  **/
5976 void
5977 gtk_widget_modify_base (GtkWidget      *widget,
5978                         GtkStateType    state,
5979                         const GdkColor *color)
5980 {
5981   g_return_if_fail (GTK_IS_WIDGET (widget));
5982   g_return_if_fail (state >= GTK_STATE_NORMAL && state <= GTK_STATE_INSENSITIVE);
5983
5984   gtk_widget_modify_color_component (widget, GTK_RC_BASE, state, color);
5985 }
5986
5987 static void
5988 modify_color_property (GtkWidget      *widget,
5989                        GtkRcStyle     *rc_style,
5990                        const char     *name,
5991                        const GdkColor *color)
5992 {
5993   GQuark type_name = g_type_qname (G_OBJECT_TYPE (widget));
5994   GQuark property_name = g_quark_from_string (name);
5995
5996   if (color)
5997     {
5998       GtkRcProperty rc_property = {0};
5999       char *color_name;
6000
6001       rc_property.type_name = type_name;
6002       rc_property.property_name = property_name;
6003       rc_property.origin = NULL;
6004
6005       color_name = gdk_color_to_string (color);
6006       g_value_init (&rc_property.value, G_TYPE_STRING);
6007       g_value_take_string (&rc_property.value, color_name);
6008
6009       _gtk_rc_style_set_rc_property (rc_style, &rc_property);
6010
6011       g_value_unset (&rc_property.value);
6012     }
6013   else
6014     _gtk_rc_style_unset_rc_property (rc_style, type_name, property_name);
6015 }
6016
6017 /**
6018  * gtk_widget_modify_cursor:
6019  * @widget: a #GtkWidget
6020  * @primary: the color to use for primary cursor (does not need to be
6021  *           allocated), or %NULL to undo the effect of previous calls to
6022  *           of gtk_widget_modify_cursor().
6023  * @secondary: the color to use for secondary cursor (does not need to be
6024  *             allocated), or %NULL to undo the effect of previous calls to
6025  *             of gtk_widget_modify_cursor().
6026  *
6027  * Sets the cursor color to use in a widget, overriding the
6028  * #GtkWidget:cursor-color and #GtkWidget:secondary-cursor-color
6029  * style properties. All other style values are left untouched. 
6030  * See also gtk_widget_modify_style().
6031  *
6032  * Since: 2.12
6033  **/
6034 void
6035 gtk_widget_modify_cursor (GtkWidget      *widget,
6036                           const GdkColor *primary,
6037                           const GdkColor *secondary)
6038 {
6039   GtkRcStyle *rc_style;
6040
6041   g_return_if_fail (GTK_IS_WIDGET (widget));
6042
6043   rc_style = gtk_widget_get_modifier_style (widget);
6044
6045   modify_color_property (widget, rc_style, "cursor-color", primary);
6046   modify_color_property (widget, rc_style, "secondary-cursor-color", secondary);
6047
6048   gtk_widget_modify_style (widget, rc_style);
6049 }
6050
6051 /**
6052  * gtk_widget_modify_font:
6053  * @widget: a #GtkWidget
6054  * @font_desc: the font description to use, or %NULL to undo
6055  *   the effect of previous calls to gtk_widget_modify_font().
6056  * 
6057  * Sets the font to use for a widget.  All other style values are left
6058  * untouched. See also gtk_widget_modify_style().
6059  **/
6060 void
6061 gtk_widget_modify_font (GtkWidget            *widget,
6062                         PangoFontDescription *font_desc)
6063 {
6064   GtkRcStyle *rc_style;
6065
6066   g_return_if_fail (GTK_IS_WIDGET (widget));
6067
6068   rc_style = gtk_widget_get_modifier_style (widget);  
6069
6070   if (rc_style->font_desc)
6071     pango_font_description_free (rc_style->font_desc);
6072
6073   if (font_desc)
6074     rc_style->font_desc = pango_font_description_copy (font_desc);
6075   else
6076     rc_style->font_desc = NULL;
6077   
6078   gtk_widget_modify_style (widget, rc_style);
6079 }
6080
6081 static void
6082 gtk_widget_real_direction_changed (GtkWidget        *widget,
6083                                    GtkTextDirection  previous_direction)
6084 {
6085   gtk_widget_queue_resize (widget);
6086 }
6087
6088 static void
6089 gtk_widget_real_style_set (GtkWidget *widget,
6090                            GtkStyle  *previous_style)
6091 {
6092   if (GTK_WIDGET_REALIZED (widget) &&
6093       !GTK_WIDGET_NO_WINDOW (widget))
6094     gtk_style_set_background (widget->style, widget->window, widget->state);
6095 }
6096
6097 static void
6098 gtk_widget_set_style_internal (GtkWidget *widget,
6099                                GtkStyle  *style,
6100                                gboolean   initial_emission)
6101 {
6102   g_object_ref (widget);
6103   g_object_freeze_notify (G_OBJECT (widget));
6104
6105   if (widget->style != style)
6106     {
6107       GtkStyle *previous_style;
6108
6109       if (GTK_WIDGET_REALIZED (widget))
6110         {
6111           gtk_widget_reset_shapes (widget);
6112           gtk_style_detach (widget->style);
6113         }
6114       
6115       previous_style = widget->style;
6116       widget->style = style;
6117       g_object_ref (widget->style);
6118       
6119       if (GTK_WIDGET_REALIZED (widget))
6120         widget->style = gtk_style_attach (widget->style, widget->window);
6121
6122       gtk_widget_update_pango_context (widget);
6123       g_signal_emit (widget,
6124                      widget_signals[STYLE_SET],
6125                      0,
6126                      initial_emission ? NULL : previous_style);
6127       g_object_unref (previous_style);
6128
6129       if (GTK_WIDGET_ANCHORED (widget) && !initial_emission)
6130         gtk_widget_queue_resize (widget);
6131     }
6132   else if (initial_emission)
6133     {
6134       gtk_widget_update_pango_context (widget);
6135       g_signal_emit (widget,
6136                      widget_signals[STYLE_SET],
6137                      0,
6138                      NULL);
6139     }
6140   g_object_notify (G_OBJECT (widget), "style");
6141   g_object_thaw_notify (G_OBJECT (widget));
6142   g_object_unref (widget);
6143 }
6144
6145 typedef struct {
6146   GtkWidget *previous_toplevel;
6147   GdkScreen *previous_screen;
6148   GdkScreen *new_screen;
6149 } HierarchyChangedInfo;
6150
6151 static void
6152 do_screen_change (GtkWidget *widget,
6153                   GdkScreen *old_screen,
6154                   GdkScreen *new_screen)
6155 {
6156   if (old_screen != new_screen)
6157     {
6158       if (old_screen)
6159         {
6160           PangoContext *context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
6161           if (context)
6162             g_object_set_qdata (G_OBJECT (widget), quark_pango_context, NULL);
6163         }
6164       
6165       _gtk_tooltip_hide (widget);
6166       g_signal_emit (widget, widget_signals[SCREEN_CHANGED], 0, old_screen);
6167     }
6168 }
6169
6170 static void
6171 gtk_widget_propagate_hierarchy_changed_recurse (GtkWidget *widget,
6172                                                 gpointer   client_data)
6173 {
6174   HierarchyChangedInfo *info = client_data;
6175   gboolean new_anchored = GTK_WIDGET_TOPLEVEL (widget) ||
6176                  (widget->parent && GTK_WIDGET_ANCHORED (widget->parent));
6177
6178   if (GTK_WIDGET_ANCHORED (widget) != new_anchored)
6179     {
6180       g_object_ref (widget);
6181       
6182       if (new_anchored)
6183         GTK_PRIVATE_SET_FLAG (widget, GTK_ANCHORED);
6184       else
6185         GTK_PRIVATE_UNSET_FLAG (widget, GTK_ANCHORED);
6186       
6187       g_signal_emit (widget, widget_signals[HIERARCHY_CHANGED], 0, info->previous_toplevel);
6188       do_screen_change (widget, info->previous_screen, info->new_screen);
6189       
6190       if (GTK_IS_CONTAINER (widget))
6191         gtk_container_forall (GTK_CONTAINER (widget),
6192                               gtk_widget_propagate_hierarchy_changed_recurse,
6193                               client_data);
6194       
6195       g_object_unref (widget);
6196     }
6197 }
6198
6199 /**
6200  * _gtk_widget_propagate_hierarchy_changed:
6201  * @widget: a #GtkWidget
6202  * @previous_toplevel: Previous toplevel
6203  * 
6204  * Propagates changes in the anchored state to a widget and all
6205  * children, unsetting or setting the %ANCHORED flag, and
6206  * emitting #GtkWidget::hierarchy-changed.
6207  **/
6208 void
6209 _gtk_widget_propagate_hierarchy_changed (GtkWidget    *widget,
6210                                          GtkWidget    *previous_toplevel)
6211 {
6212   HierarchyChangedInfo info;
6213
6214   info.previous_toplevel = previous_toplevel;
6215   info.previous_screen = previous_toplevel ? gtk_widget_get_screen (previous_toplevel) : NULL;
6216
6217   if (GTK_WIDGET_TOPLEVEL (widget) ||
6218       (widget->parent && GTK_WIDGET_ANCHORED (widget->parent)))
6219     info.new_screen = gtk_widget_get_screen (widget);
6220   else
6221     info.new_screen = NULL;
6222
6223   if (info.previous_screen)
6224     g_object_ref (info.previous_screen);
6225   if (previous_toplevel)
6226     g_object_ref (previous_toplevel);
6227
6228   gtk_widget_propagate_hierarchy_changed_recurse (widget, &info);
6229
6230   if (previous_toplevel)
6231     g_object_unref (previous_toplevel);
6232   if (info.previous_screen)
6233     g_object_unref (info.previous_screen);
6234 }
6235
6236 static void
6237 gtk_widget_propagate_screen_changed_recurse (GtkWidget *widget,
6238                                              gpointer   client_data)
6239 {
6240   HierarchyChangedInfo *info = client_data;
6241
6242   g_object_ref (widget);
6243   
6244   do_screen_change (widget, info->previous_screen, info->new_screen);
6245   
6246   if (GTK_IS_CONTAINER (widget))
6247     gtk_container_forall (GTK_CONTAINER (widget),
6248                           gtk_widget_propagate_screen_changed_recurse,
6249                           client_data);
6250   
6251   g_object_unref (widget);
6252 }
6253
6254 /**
6255  * gtk_widget_is_composited:
6256  * @widget: a #GtkWidget
6257  * 
6258  * Whether @widget can rely on having its alpha channel
6259  * drawn correctly. On X11 this function returns whether a
6260  * compositing manager is running for @widget's screen.
6261  *
6262  * Please note that the semantics of this call will change
6263  * in the future if used on a widget that has a composited
6264  * window in its hierarchy (as set by gdk_window_set_composited()).
6265  * 
6266  * Return value: %TRUE if the widget can rely on its alpha
6267  * channel being drawn correctly.
6268  * 
6269  * Since: 2.10
6270  */
6271 gboolean
6272 gtk_widget_is_composited (GtkWidget *widget)
6273 {
6274   GdkScreen *screen;
6275
6276   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6277   
6278   screen = gtk_widget_get_screen (widget);
6279   
6280   return gdk_screen_is_composited (screen);
6281 }
6282
6283 static void
6284 propagate_composited_changed (GtkWidget *widget,
6285                               gpointer dummy)
6286 {
6287   if (GTK_IS_CONTAINER (widget))
6288     {
6289       gtk_container_forall (GTK_CONTAINER (widget),
6290                             propagate_composited_changed,
6291                             NULL);
6292     }
6293   
6294   g_signal_emit (widget, widget_signals[COMPOSITED_CHANGED], 0);
6295 }
6296
6297 void
6298 _gtk_widget_propagate_composited_changed (GtkWidget *widget)
6299 {
6300   propagate_composited_changed (widget, NULL);
6301 }
6302
6303 /**
6304  * _gtk_widget_propagate_screen_changed:
6305  * @widget: a #GtkWidget
6306  * @previous_screen: Previous screen
6307  * 
6308  * Propagates changes in the screen for a widget to all
6309  * children, emitting #GtkWidget::screen-changed.
6310  **/
6311 void
6312 _gtk_widget_propagate_screen_changed (GtkWidget    *widget,
6313                                       GdkScreen    *previous_screen)
6314 {
6315   HierarchyChangedInfo info;
6316
6317   info.previous_screen = previous_screen;
6318   info.new_screen = gtk_widget_get_screen (widget);
6319
6320   if (previous_screen)
6321     g_object_ref (previous_screen);
6322
6323   gtk_widget_propagate_screen_changed_recurse (widget, &info);
6324
6325   if (previous_screen)
6326     g_object_unref (previous_screen);
6327 }
6328
6329 static void
6330 reset_rc_styles_recurse (GtkWidget *widget, gpointer data)
6331 {
6332   if (GTK_WIDGET_RC_STYLE (widget))
6333     gtk_widget_reset_rc_style (widget);
6334   
6335   if (GTK_IS_CONTAINER (widget))
6336     gtk_container_forall (GTK_CONTAINER (widget),
6337                           reset_rc_styles_recurse,
6338                           NULL);
6339 }
6340
6341 void
6342 gtk_widget_reset_rc_styles (GtkWidget *widget)
6343 {
6344   g_return_if_fail (GTK_IS_WIDGET (widget));
6345
6346   reset_rc_styles_recurse (widget, NULL);
6347 }
6348
6349 /**
6350  * gtk_widget_get_default_style:
6351  * 
6352  * Returns the default style used by all widgets initially.
6353  * 
6354  * Returns: the default style. This #GtkStyle object is owned 
6355  *          by GTK+ and should not be modified or freed.
6356  */ 
6357 GtkStyle*
6358 gtk_widget_get_default_style (void)
6359 {
6360   if (!gtk_default_style)
6361     {
6362       gtk_default_style = gtk_style_new ();
6363       g_object_ref (gtk_default_style);
6364     }
6365   
6366   return gtk_default_style;
6367 }
6368
6369 static PangoContext *
6370 gtk_widget_peek_pango_context (GtkWidget *widget)
6371 {
6372   return g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
6373 }
6374
6375 /**
6376  * gtk_widget_get_pango_context:
6377  * @widget: a #GtkWidget
6378  * 
6379  * Gets a #PangoContext with the appropriate font map, font description,
6380  * and base direction for this widget. Unlike the context returned
6381  * by gtk_widget_create_pango_context(), this context is owned by
6382  * the widget (it can be used until the screen for the widget changes
6383  * or the widget is removed from its toplevel), and will be updated to
6384  * match any changes to the widget's attributes.
6385  *
6386  * If you create and keep a #PangoLayout using this context, you must
6387  * deal with changes to the context by calling pango_layout_context_changed()
6388  * on the layout in response to the #GtkWidget::style-set and 
6389  * #GtkWidget::direction-changed signals for the widget.
6390  *
6391  * Return value: the #PangoContext for the widget.
6392  **/
6393 PangoContext *
6394 gtk_widget_get_pango_context (GtkWidget *widget)
6395 {
6396   PangoContext *context;
6397
6398   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6399   
6400   context = g_object_get_qdata (G_OBJECT (widget), quark_pango_context);
6401   if (!context)
6402     {
6403       context = gtk_widget_create_pango_context (GTK_WIDGET (widget));
6404       g_object_set_qdata_full (G_OBJECT (widget),
6405                                quark_pango_context,
6406                                context,
6407                                g_object_unref);
6408     }
6409
6410   return context;
6411 }
6412
6413 static void
6414 update_pango_context (GtkWidget    *widget,
6415                       PangoContext *context)
6416 {
6417   pango_context_set_font_description (context, widget->style->font_desc);
6418   pango_context_set_base_dir (context,
6419                               gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR ?
6420                               PANGO_DIRECTION_LTR : PANGO_DIRECTION_RTL);
6421 }
6422
6423 static void
6424 gtk_widget_update_pango_context (GtkWidget *widget)
6425 {
6426   PangoContext *context = gtk_widget_peek_pango_context (widget);
6427   
6428   if (context)
6429     {
6430       GdkScreen *screen;
6431
6432       update_pango_context (widget, context);
6433
6434       screen = gtk_widget_get_screen_unchecked (widget);
6435       if (screen)
6436         {
6437           pango_cairo_context_set_resolution (context,
6438                                               gdk_screen_get_resolution (screen));
6439           pango_cairo_context_set_font_options (context,
6440                                                 gdk_screen_get_font_options (screen));
6441         }
6442     }
6443 }
6444
6445 /**
6446  * gtk_widget_create_pango_context:
6447  * @widget: a #GtkWidget
6448  * 
6449  * Creates a new #PangoContext with the appropriate font map,
6450  * font description, and base direction for drawing text for
6451  * this widget. See also gtk_widget_get_pango_context().
6452  * 
6453  * Return value: the new #PangoContext
6454  **/
6455 PangoContext *
6456 gtk_widget_create_pango_context (GtkWidget *widget)
6457 {
6458   GdkScreen *screen;
6459   PangoContext *context;
6460
6461   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6462
6463   screen = gtk_widget_get_screen_unchecked (widget);
6464   if (!screen)
6465     {
6466       GTK_NOTE (MULTIHEAD,
6467                 g_warning ("gtk_widget_create_pango_context ()) called without screen"));
6468
6469       screen = gdk_screen_get_default ();
6470     }
6471
6472   context = gdk_pango_context_get_for_screen (screen);
6473
6474   update_pango_context (widget, context);
6475   pango_context_set_language (context, gtk_get_default_language ());
6476
6477   return context;
6478 }
6479
6480 /**
6481  * gtk_widget_create_pango_layout:
6482  * @widget: a #GtkWidget
6483  * @text: text to set on the layout (can be %NULL)
6484  * 
6485  * Creates a new #PangoLayout with the appropriate font map,
6486  * font description, and base direction for drawing text for
6487  * this widget.
6488  *
6489  * If you keep a #PangoLayout created in this way around, in order to
6490  * notify the layout of changes to the base direction or font of this
6491  * widget, you must call pango_layout_context_changed() in response to
6492  * the #GtkWidget::style-set and #GtkWidget::direction-changed signals 
6493  * for the widget.
6494  * 
6495  * Return value: the new #PangoLayout
6496  **/
6497 PangoLayout *
6498 gtk_widget_create_pango_layout (GtkWidget   *widget,
6499                                 const gchar *text)
6500 {
6501   PangoLayout *layout;
6502   PangoContext *context;
6503
6504   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6505
6506   context = gtk_widget_get_pango_context (widget);
6507   layout = pango_layout_new (context);
6508
6509   if (text)
6510     pango_layout_set_text (layout, text, -1);
6511
6512   return layout;
6513 }
6514
6515 /**
6516  * gtk_widget_render_icon:
6517  * @widget: a #GtkWidget
6518  * @stock_id: a stock ID
6519  * @size: a stock size. A size of (GtkIconSize)-1 means render at 
6520  *     the size of the source and don't scale (if there are multiple 
6521  *     source sizes, GTK+ picks one of the available sizes).
6522  * @detail: render detail to pass to theme engine
6523  * 
6524  * A convenience function that uses the theme engine and RC file
6525  * settings for @widget to look up @stock_id and render it to
6526  * a pixbuf. @stock_id should be a stock icon ID such as
6527  * #GTK_STOCK_OPEN or #GTK_STOCK_OK. @size should be a size
6528  * such as #GTK_ICON_SIZE_MENU. @detail should be a string that
6529  * identifies the widget or code doing the rendering, so that
6530  * theme engines can special-case rendering for that widget or code.
6531  *
6532  * The pixels in the returned #GdkPixbuf are shared with the rest of
6533  * the application and should not be modified. The pixbuf should be freed
6534  * after use with g_object_unref().
6535  *
6536  * Return value: a new pixbuf, or %NULL if the stock ID wasn't known
6537  **/
6538 GdkPixbuf*
6539 gtk_widget_render_icon (GtkWidget      *widget,
6540                         const gchar    *stock_id,
6541                         GtkIconSize     size,
6542                         const gchar    *detail)
6543 {
6544   GtkIconSet *icon_set;
6545   GdkPixbuf *retval;
6546   
6547   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6548   g_return_val_if_fail (stock_id != NULL, NULL);
6549   g_return_val_if_fail (size > GTK_ICON_SIZE_INVALID || size == -1, NULL);
6550   
6551   gtk_widget_ensure_style (widget);
6552   
6553   icon_set = gtk_style_lookup_icon_set (widget->style, stock_id);
6554
6555   if (icon_set == NULL)
6556     return NULL;
6557
6558   retval = gtk_icon_set_render_icon (icon_set,
6559                                      widget->style,
6560                                      gtk_widget_get_direction (widget),
6561                                      GTK_WIDGET_STATE (widget),
6562                                      size,
6563                                      widget,
6564                                      detail);
6565
6566   return retval;
6567 }
6568
6569 /**
6570  * gtk_widget_set_parent_window:
6571  * @widget: a #GtkWidget.
6572  * @parent_window: the new parent window.
6573  *  
6574  * Sets a non default parent window for @widget.
6575  **/
6576 void
6577 gtk_widget_set_parent_window   (GtkWidget           *widget,
6578                                 GdkWindow           *parent_window)
6579 {
6580   GdkWindow *old_parent_window;
6581
6582   g_return_if_fail (GTK_IS_WIDGET (widget));
6583   
6584   old_parent_window = g_object_get_qdata (G_OBJECT (widget),
6585                                           quark_parent_window);
6586
6587   if (parent_window != old_parent_window)
6588     {
6589       g_object_set_qdata (G_OBJECT (widget), quark_parent_window, 
6590                           parent_window);
6591       if (old_parent_window)
6592         g_object_unref (old_parent_window);
6593       if (parent_window)
6594         g_object_ref (parent_window);
6595     }
6596 }
6597
6598 /**
6599  * gtk_widget_get_parent_window:
6600  * @widget: a #GtkWidget.
6601  * @returns: the parent window of @widget.
6602  *
6603  * Gets @widget's parent window.
6604  **/
6605 GdkWindow *
6606 gtk_widget_get_parent_window (GtkWidget *widget)
6607 {
6608   GdkWindow *parent_window;
6609
6610   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6611
6612   parent_window = g_object_get_qdata (G_OBJECT (widget), quark_parent_window);
6613
6614   return (parent_window != NULL) ? parent_window :
6615          (widget->parent != NULL) ? widget->parent->window : NULL;
6616 }
6617
6618
6619 /**
6620  * gtk_widget_set_child_visible:
6621  * @widget: a #GtkWidget
6622  * @is_visible: if %TRUE, @widget should be mapped along with its parent.
6623  *
6624  * Sets whether @widget should be mapped along with its when its parent
6625  * is mapped and @widget has been shown with gtk_widget_show(). 
6626  *
6627  * The child visibility can be set for widget before it is added to
6628  * a container with gtk_widget_set_parent(), to avoid mapping
6629  * children unnecessary before immediately unmapping them. However
6630  * it will be reset to its default state of %TRUE when the widget
6631  * is removed from a container.
6632  * 
6633  * Note that changing the child visibility of a widget does not
6634  * queue a resize on the widget. Most of the time, the size of
6635  * a widget is computed from all visible children, whether or
6636  * not they are mapped. If this is not the case, the container
6637  * can queue a resize itself.
6638  *
6639  * This function is only useful for container implementations and
6640  * never should be called by an application.
6641  **/
6642 void
6643 gtk_widget_set_child_visible (GtkWidget *widget,
6644                               gboolean   is_visible)
6645 {
6646   g_return_if_fail (GTK_IS_WIDGET (widget));
6647   g_return_if_fail (!GTK_WIDGET_TOPLEVEL (widget));
6648
6649   g_object_ref (widget);
6650
6651   if (is_visible)
6652     GTK_PRIVATE_SET_FLAG (widget, GTK_CHILD_VISIBLE);
6653   else
6654     {
6655       GtkWidget *toplevel;
6656       
6657       GTK_PRIVATE_UNSET_FLAG (widget, GTK_CHILD_VISIBLE);
6658
6659       toplevel = gtk_widget_get_toplevel (widget);
6660       if (toplevel != widget && GTK_WIDGET_TOPLEVEL (toplevel))
6661         _gtk_window_unset_focus_and_default (GTK_WINDOW (toplevel), widget);
6662     }
6663
6664   if (widget->parent && GTK_WIDGET_REALIZED (widget->parent))
6665     {
6666       if (GTK_WIDGET_MAPPED (widget->parent) &&
6667           GTK_WIDGET_CHILD_VISIBLE (widget) &&
6668           GTK_WIDGET_VISIBLE (widget))
6669         gtk_widget_map (widget);
6670       else
6671         gtk_widget_unmap (widget);
6672     }
6673
6674   g_object_unref (widget);
6675 }
6676
6677 /**
6678  * gtk_widget_get_child_visible:
6679  * @widget: a #GtkWidget
6680  * 
6681  * Gets the value set with gtk_widget_set_child_visible().
6682  * If you feel a need to use this function, your code probably
6683  * needs reorganization. 
6684  *
6685  * This function is only useful for container implementations and
6686  * never should be called by an application.
6687  *
6688  * Return value: %TRUE if the widget is mapped with the parent.
6689  **/
6690 gboolean
6691 gtk_widget_get_child_visible (GtkWidget *widget)
6692 {
6693   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6694   
6695   return GTK_WIDGET_CHILD_VISIBLE (widget);
6696 }
6697
6698 static GdkScreen *
6699 gtk_widget_get_screen_unchecked (GtkWidget *widget)
6700 {
6701   GtkWidget *toplevel;
6702   
6703   toplevel = gtk_widget_get_toplevel (widget);
6704
6705   if (GTK_WIDGET_TOPLEVEL (toplevel))
6706     {
6707       if (GTK_IS_WINDOW (toplevel))
6708         return GTK_WINDOW (toplevel)->screen;
6709       else if (GTK_IS_INVISIBLE (toplevel))
6710         return GTK_INVISIBLE (widget)->screen;
6711     }
6712
6713   return NULL;
6714 }
6715
6716 /**
6717  * gtk_widget_get_screen:
6718  * @widget: a #GtkWidget
6719  * 
6720  * Get the #GdkScreen from the toplevel window associated with
6721  * this widget. This function can only be called after the widget
6722  * has been added to a widget hierarchy with a #GtkWindow
6723  * at the top.
6724  *
6725  * In general, you should only create screen specific
6726  * resources when a widget has been realized, and you should
6727  * free those resources when the widget is unrealized.
6728  * 
6729  * Return value: the #GdkScreen for the toplevel for this widget.
6730  *
6731  * Since: 2.2
6732  **/
6733 GdkScreen*
6734 gtk_widget_get_screen (GtkWidget *widget)
6735 {
6736   GdkScreen *screen;
6737   
6738   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6739
6740   screen = gtk_widget_get_screen_unchecked (widget);
6741
6742   if (screen)
6743     return screen;
6744   else
6745     {
6746 #if 0
6747       g_warning (G_STRLOC ": Can't get associated screen"
6748                  " for a widget unless it is inside a toplevel GtkWindow\n"
6749                  " widget type is %s associated top level type is %s",
6750                  g_type_name (G_OBJECT_TYPE(G_OBJECT (widget))),
6751                  g_type_name (G_OBJECT_TYPE(G_OBJECT (toplevel))));
6752 #endif
6753       return gdk_screen_get_default ();
6754     }
6755 }
6756
6757 /**
6758  * gtk_widget_has_screen:
6759  * @widget: a #GtkWidget
6760  * 
6761  * Checks whether there is a #GdkScreen is associated with
6762  * this widget. All toplevel widgets have an associated
6763  * screen, and all widgets added into a hierarchy with a toplevel
6764  * window at the top.
6765  * 
6766  * Return value: %TRUE if there is a #GdkScreen associcated
6767  *   with the widget.
6768  *
6769  * Since: 2.2
6770  **/
6771 gboolean
6772 gtk_widget_has_screen (GtkWidget *widget)
6773 {
6774   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6775
6776   return (gtk_widget_get_screen_unchecked (widget) != NULL);
6777 }
6778
6779 /**
6780  * gtk_widget_get_display:
6781  * @widget: a #GtkWidget
6782  * 
6783  * Get the #GdkDisplay for the toplevel window associated with
6784  * this widget. This function can only be called after the widget
6785  * has been added to a widget hierarchy with a #GtkWindow at the top.
6786  *
6787  * In general, you should only create display specific
6788  * resources when a widget has been realized, and you should
6789  * free those resources when the widget is unrealized.
6790  * 
6791  * Return value: the #GdkDisplay for the toplevel for this widget.
6792  *
6793  * Since: 2.2
6794  **/
6795 GdkDisplay*
6796 gtk_widget_get_display (GtkWidget *widget)
6797 {
6798   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6799   
6800   return gdk_screen_get_display (gtk_widget_get_screen (widget));
6801 }
6802
6803 /**
6804  * gtk_widget_get_root_window:
6805  * @widget: a #GtkWidget
6806  * 
6807  * Get the root window where this widget is located. This function can
6808  * only be called after the widget has been added to a widget
6809  * hierarchy with #GtkWindow at the top.
6810  *
6811  * The root window is useful for such purposes as creating a popup
6812  * #GdkWindow associated with the window. In general, you should only
6813  * create display specific resources when a widget has been realized,
6814  * and you should free those resources when the widget is unrealized.
6815  * 
6816  * Return value: the #GdkWindow root window for the toplevel for this widget.
6817  *
6818  * Since: 2.2
6819  **/
6820 GdkWindow*
6821 gtk_widget_get_root_window (GtkWidget *widget)
6822 {
6823   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
6824
6825   return gdk_screen_get_root_window (gtk_widget_get_screen (widget));
6826 }
6827
6828 /**
6829  * gtk_widget_child_focus:
6830  * @widget: a #GtkWidget
6831  * @direction: direction of focus movement
6832  *
6833  * This function is used by custom widget implementations; if you're
6834  * writing an app, you'd use gtk_widget_grab_focus() to move the focus
6835  * to a particular widget, and gtk_container_set_focus_chain() to
6836  * change the focus tab order. So you may want to investigate those
6837  * functions instead.
6838  * 
6839  * gtk_widget_child_focus() is called by containers as the user moves
6840  * around the window using keyboard shortcuts. @direction indicates
6841  * what kind of motion is taking place (up, down, left, right, tab
6842  * forward, tab backward). gtk_widget_child_focus() emits the
6843  * #GtkWidget::focus" signal; widgets override the default handler
6844  * for this signal in order to implement appropriate focus behavior.
6845  *
6846  * The default ::focus handler for a widget should return %TRUE if
6847  * moving in @direction left the focus on a focusable location inside
6848  * that widget, and %FALSE if moving in @direction moved the focus
6849  * outside the widget. If returning %TRUE, widgets normally
6850  * call gtk_widget_grab_focus() to place the focus accordingly;
6851  * if returning %FALSE, they don't modify the current focus location.
6852  * 
6853  * This function replaces gtk_container_focus() from GTK+ 1.2.  
6854  * It was necessary to check that the child was visible, sensitive, 
6855  * and focusable before calling gtk_container_focus(). 
6856  * gtk_widget_child_focus() returns %FALSE if the widget is not 
6857  * currently in a focusable state, so there's no need for those checks.
6858  * 
6859  * Return value: %TRUE if focus ended up inside @widget
6860  **/
6861 gboolean
6862 gtk_widget_child_focus (GtkWidget       *widget,
6863                         GtkDirectionType direction)
6864 {
6865   gboolean return_val;
6866
6867   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6868
6869   if (!GTK_WIDGET_VISIBLE (widget) ||
6870       !GTK_WIDGET_IS_SENSITIVE (widget))
6871     return FALSE;
6872
6873   /* child widgets must set CAN_FOCUS, containers
6874    * don't have to though.
6875    */
6876   if (!GTK_IS_CONTAINER (widget) &&
6877       !GTK_WIDGET_CAN_FOCUS (widget))
6878     return FALSE;
6879   
6880   g_signal_emit (widget,
6881                  widget_signals[FOCUS],
6882                  0,
6883                  direction, &return_val);
6884
6885   return return_val;
6886 }
6887
6888 /**
6889  * gtk_widget_keynav_failed:
6890  * @widget: a #GtkWidget
6891  * @direction: direction of focus movement
6892  *
6893  * This function should be called whenever keyboard navigation within
6894  * a single widget hits a boundary. The function emits the
6895  * #GtkWidget::keynav-failed signal on the widget and its return
6896  * value should be interpreted in a way similar to the return value of
6897  * gtk_widget_child_focus():
6898  *
6899  * When %TRUE is returned, stay in the widget, the failed keyboard
6900  * navigation is Ok and/or there is nowhere we can/should move the
6901  * focus to.
6902  *
6903  * When %FALSE is returned, the caller should continue with keyboard
6904  * navigation outside the widget, e.g. by calling
6905  * gtk_widget_child_focus() on the widget's toplevel.
6906  *
6907  * The default ::keynav-failed handler returns %TRUE for 
6908  * %GTK_DIR_TAB_FORWARD and %GTK_DIR_TAB_BACKWARD. For the other 
6909  * values of #GtkDirectionType, it looks at the 
6910  * #GtkSettings:gtk-keynav-cursor-only setting and returns %FALSE 
6911  * if the setting is %TRUE. This way the entire user interface
6912  * becomes cursor-navigatable on input devices such as mobile phones
6913  * which only have cursor keys but no tab key.
6914  *
6915  * Whenever the default handler returns %TRUE, it also calls
6916  * gtk_widget_error_bell() to notify the user of the failed keyboard
6917  * navigation.
6918  *
6919  * A use case for providing an own implementation of ::keynav-failed 
6920  * (either by connecting to it or by overriding it) would be a row of
6921  * #GtkEntry widgets where the user should be able to navigate the
6922  * entire row with the cursor keys, as e.g. known from user interfaces 
6923  * that require entering license keys.
6924  *
6925  * Return value: %TRUE if stopping keyboard navigation is fine, %FALSE
6926  *               if the emitting widget should try to handle the keyboard
6927  *               navigation attempt in its parent container(s).
6928  *
6929  * Since: 2.12
6930  **/
6931 gboolean
6932 gtk_widget_keynav_failed (GtkWidget        *widget,
6933                           GtkDirectionType  direction)
6934 {
6935   gboolean return_val;
6936
6937   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
6938
6939   g_signal_emit (widget, widget_signals[KEYNAV_FAILED], 0,
6940                  direction, &return_val);
6941
6942   return return_val;
6943 }
6944
6945 /**
6946  * gtk_widget_error_bell:
6947  * @widget: a #GtkWidget
6948  *
6949  * Notifies the user about an input-related error on this widget. 
6950  * If the #GtkSettings:gtk-error-bell setting is %TRUE, it calls
6951  * gdk_window_beep(), otherwise it does nothing.
6952  *
6953  * Note that the effect of gdk_window_beep() can be configured in many
6954  * ways, depending on the windowing backend and the desktop environment
6955  * or window manager that is used.
6956  *
6957  * Since: 2.12
6958  **/
6959 void
6960 gtk_widget_error_bell (GtkWidget *widget)
6961 {
6962   GtkSettings* settings;
6963   gboolean beep;
6964
6965   g_return_if_fail (GTK_IS_WIDGET (widget));
6966
6967   settings = gtk_widget_get_settings (widget);
6968   if (!settings)
6969     return;
6970
6971   g_object_get (settings,
6972                 "gtk-error-bell", &beep,
6973                 NULL);
6974
6975   if (beep && widget->window)
6976     gdk_window_beep (widget->window);
6977 }
6978
6979 /**
6980  * gtk_widget_set_uposition:
6981  * @widget: a #GtkWidget
6982  * @x: x position; -1 to unset x; -2 to leave x unchanged
6983  * @y: y position; -1 to unset y; -2 to leave y unchanged
6984  * 
6985  *
6986  * Sets the position of a widget. The funny "u" in the name comes from
6987  * the "user position" hint specified by the X Window System, and
6988  * exists for legacy reasons. This function doesn't work if a widget
6989  * is inside a container; it's only really useful on #GtkWindow.
6990  *
6991  * Don't use this function to center dialogs over the main application
6992  * window; most window managers will do the centering on your behalf
6993  * if you call gtk_window_set_transient_for(), and it's really not
6994  * possible to get the centering to work correctly in all cases from
6995  * application code. But if you insist, use gtk_window_set_position()
6996  * to set #GTK_WIN_POS_CENTER_ON_PARENT, don't do the centering
6997  * manually.
6998  *
6999  * Note that although @x and @y can be individually unset, the position
7000  * is not honoured unless both @x and @y are set.
7001  **/
7002 void
7003 gtk_widget_set_uposition (GtkWidget *widget,
7004                           gint       x,
7005                           gint       y)
7006 {
7007   /* FIXME this function is the only place that aux_info->x and
7008    * aux_info->y are even used I believe, and this function is
7009    * deprecated. Should be cleaned up.
7010    *
7011    * (Actually, size_allocate uses them) -Yosh
7012    */
7013   
7014   GtkWidgetAuxInfo *aux_info;
7015   
7016   g_return_if_fail (GTK_IS_WIDGET (widget));
7017   
7018   aux_info =_gtk_widget_get_aux_info (widget, TRUE);
7019   
7020   if (x > -2)
7021     {
7022       if (x == -1)
7023         aux_info->x_set = FALSE;
7024       else
7025         {
7026           aux_info->x_set = TRUE;
7027           aux_info->x = x;
7028         }
7029     }
7030
7031   if (y > -2)
7032     {
7033       if (y == -1)
7034         aux_info->y_set = FALSE;
7035       else
7036         {
7037           aux_info->y_set = TRUE;
7038           aux_info->y = y;
7039         }
7040     }
7041
7042   if (GTK_IS_WINDOW (widget) && aux_info->x_set && aux_info->y_set)
7043     _gtk_window_reposition (GTK_WINDOW (widget), aux_info->x, aux_info->y);
7044   
7045   if (GTK_WIDGET_VISIBLE (widget) && widget->parent)
7046     gtk_widget_size_allocate (widget, &widget->allocation);
7047 }
7048
7049 static void
7050 gtk_widget_set_usize_internal (GtkWidget *widget,
7051                                gint       width,
7052                                gint       height)
7053 {
7054   GtkWidgetAuxInfo *aux_info;
7055   gboolean changed = FALSE;
7056   
7057   g_object_freeze_notify (G_OBJECT (widget));
7058
7059   aux_info = _gtk_widget_get_aux_info (widget, TRUE);
7060   
7061   if (width > -2 && aux_info->width != width)
7062     {
7063       g_object_notify (G_OBJECT (widget), "width-request");
7064       aux_info->width = width;
7065       changed = TRUE;
7066     }
7067   if (height > -2 && aux_info->height != height)
7068     {
7069       g_object_notify (G_OBJECT (widget), "height-request");  
7070       aux_info->height = height;
7071       changed = TRUE;
7072     }
7073   
7074   if (GTK_WIDGET_VISIBLE (widget) && changed)
7075     gtk_widget_queue_resize (widget);
7076
7077   g_object_thaw_notify (G_OBJECT (widget));
7078 }
7079
7080 /**
7081  * gtk_widget_set_usize:
7082  * @widget: a #GtkWidget
7083  * @width: minimum width, or -1 to unset
7084  * @height: minimum height, or -1 to unset
7085  *
7086  * Sets the minimum size of a widget; that is, the widget's size
7087  * request will be @width by @height. You can use this function to
7088  * force a widget to be either larger or smaller than it is. The
7089  * strange "usize" name dates from the early days of GTK+, and derives
7090  * from X Window System terminology. In many cases,
7091  * gtk_window_set_default_size() is a better choice for toplevel
7092  * windows than this function; setting the default size will still
7093  * allow users to shrink the window. Setting the usize will force them
7094  * to leave the window at least as large as the usize. When dealing
7095  * with window sizes, gtk_window_set_geometry_hints() can be a useful
7096  * function as well.
7097  * 
7098  * Note the inherent danger of setting any fixed size - themes,
7099  * translations into other languages, different fonts, and user action
7100  * can all change the appropriate size for a given widget. So, it's
7101  * basically impossible to hardcode a size that will always be
7102  * correct.
7103  * 
7104  * Deprecated: 2.2: Use gtk_widget_set_size_request() instead.
7105  **/
7106 void
7107 gtk_widget_set_usize (GtkWidget *widget,
7108                       gint       width,
7109                       gint       height)
7110 {
7111   g_return_if_fail (GTK_IS_WIDGET (widget));
7112   
7113   gtk_widget_set_usize_internal (widget, width, height);
7114 }
7115
7116 /**
7117  * gtk_widget_set_size_request:
7118  * @widget: a #GtkWidget
7119  * @width: width @widget should request, or -1 to unset
7120  * @height: height @widget should request, or -1 to unset
7121  *
7122  * Sets the minimum size of a widget; that is, the widget's size
7123  * request will be @width by @height. You can use this function to
7124  * force a widget to be either larger or smaller than it normally
7125  * would be.
7126  *
7127  * In most cases, gtk_window_set_default_size() is a better choice for
7128  * toplevel windows than this function; setting the default size will
7129  * still allow users to shrink the window. Setting the size request
7130  * will force them to leave the window at least as large as the size
7131  * request. When dealing with window sizes,
7132  * gtk_window_set_geometry_hints() can be a useful function as well.
7133  * 
7134  * Note the inherent danger of setting any fixed size - themes,
7135  * translations into other languages, different fonts, and user action
7136  * can all change the appropriate size for a given widget. So, it's
7137  * basically impossible to hardcode a size that will always be
7138  * correct.
7139  *
7140  * The size request of a widget is the smallest size a widget can
7141  * accept while still functioning well and drawing itself correctly.
7142  * However in some strange cases a widget may be allocated less than
7143  * its requested size, and in many cases a widget may be allocated more
7144  * space than it requested.
7145  *
7146  * If the size request in a given direction is -1 (unset), then
7147  * the "natural" size request of the widget will be used instead.
7148  *
7149  * Widgets can't actually be allocated a size less than 1 by 1, but
7150  * you can pass 0,0 to this function to mean "as small as possible."
7151  **/
7152 void
7153 gtk_widget_set_size_request (GtkWidget *widget,
7154                              gint       width,
7155                              gint       height)
7156 {
7157   g_return_if_fail (GTK_IS_WIDGET (widget));
7158   g_return_if_fail (width >= -1);
7159   g_return_if_fail (height >= -1);
7160
7161   if (width == 0)
7162     width = 1;
7163   if (height == 0)
7164     height = 1;
7165   
7166   gtk_widget_set_usize_internal (widget, width, height);
7167 }
7168
7169
7170 /**
7171  * gtk_widget_get_size_request:
7172  * @widget: a #GtkWidget
7173  * @width: return location for width, or %NULL
7174  * @height: return location for height, or %NULL
7175  *
7176  * Gets the size request that was explicitly set for the widget using
7177  * gtk_widget_set_size_request(). A value of -1 stored in @width or
7178  * @height indicates that that dimension has not been set explicitly
7179  * and the natural requisition of the widget will be used intead. See
7180  * gtk_widget_set_size_request(). To get the size a widget will
7181  * actually use, call gtk_widget_size_request() instead of
7182  * this function.
7183  **/
7184 void
7185 gtk_widget_get_size_request (GtkWidget *widget,
7186                              gint      *width,
7187                              gint      *height)
7188 {
7189   GtkWidgetAuxInfo *aux_info;
7190
7191   g_return_if_fail (GTK_IS_WIDGET (widget));
7192
7193   aux_info = _gtk_widget_get_aux_info (widget, FALSE);
7194
7195   if (width)
7196     *width = aux_info ? aux_info->width : -1;
7197
7198   if (height)
7199     *height = aux_info ? aux_info->height : -1;
7200 }
7201
7202 /**
7203  * gtk_widget_set_events:
7204  * @widget: a #GtkWidget
7205  * @events: event mask
7206  *
7207  * Sets the event mask (see #GdkEventMask) for a widget. The event
7208  * mask determines which events a widget will receive. Keep in mind
7209  * that different widgets have different default event masks, and by
7210  * changing the event mask you may disrupt a widget's functionality,
7211  * so be careful. This function must be called while a widget is
7212  * unrealized. Consider gtk_widget_add_events() for widgets that are
7213  * already realized, or if you want to preserve the existing event
7214  * mask. This function can't be used with #GTK_NO_WINDOW widgets;
7215  * to get events on those widgets, place them inside a #GtkEventBox
7216  * and receive events on the event box.
7217  **/
7218 void
7219 gtk_widget_set_events (GtkWidget *widget,
7220                        gint       events)
7221 {
7222   g_return_if_fail (GTK_IS_WIDGET (widget));
7223   g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
7224   
7225   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
7226                       GINT_TO_POINTER (events));
7227   g_object_notify (G_OBJECT (widget), "events");
7228 }
7229
7230 static void
7231 gtk_widget_add_events_internal (GtkWidget *widget,
7232                                 gint       events,
7233                                 GList     *window_list)
7234 {
7235   GList *l;
7236
7237   for (l = window_list; l != NULL; l = l->next)
7238     {
7239       GdkWindow *window = l->data;
7240       gpointer user_data;
7241
7242       gdk_window_get_user_data (window, &user_data);
7243       if (user_data == widget)
7244         {
7245           GList *children;
7246
7247           gdk_window_set_events (window, gdk_window_get_events (window) | events);
7248
7249           children = gdk_window_get_children (window);
7250           gtk_widget_add_events_internal (widget, events, children);
7251           g_list_free (children);
7252         }
7253     }
7254 }
7255
7256 /**
7257  * gtk_widget_add_events:
7258  * @widget: a #GtkWidget
7259  * @events: an event mask, see #GdkEventMask
7260  *
7261  * Adds the events in the bitfield @events to the event mask for
7262  * @widget. See gtk_widget_set_events() for details.
7263  **/
7264 void
7265 gtk_widget_add_events (GtkWidget *widget,
7266                        gint       events)
7267 {
7268   gint old_events;
7269
7270   g_return_if_fail (GTK_IS_WIDGET (widget));
7271
7272   old_events = GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
7273   g_object_set_qdata (G_OBJECT (widget), quark_event_mask,
7274                       GINT_TO_POINTER (old_events | events));
7275
7276   if (GTK_WIDGET_REALIZED (widget))
7277     {
7278       GList *window_list;
7279
7280       if (GTK_WIDGET_NO_WINDOW (widget))
7281         window_list = gdk_window_get_children (widget->window);
7282       else
7283         window_list = g_list_prepend (NULL, widget->window);
7284
7285       gtk_widget_add_events_internal (widget, events, window_list);
7286
7287       g_list_free (window_list);
7288     }
7289
7290   g_object_notify (G_OBJECT (widget), "events");
7291 }
7292
7293 /**
7294  * gtk_widget_set_extension_events:
7295  * @widget: a #GtkWidget
7296  * @mode: bitfield of extension events to receive
7297  *
7298  * Sets the extension events mask to @mode. See #GdkExtensionMode
7299  * and gdk_input_set_extension_events().
7300  **/
7301 void
7302 gtk_widget_set_extension_events (GtkWidget *widget,
7303                                  GdkExtensionMode mode)
7304 {
7305   g_return_if_fail (GTK_IS_WIDGET (widget));
7306
7307   if (GTK_WIDGET_REALIZED (widget))
7308     gtk_widget_set_extension_events_internal (widget, mode, NULL);
7309
7310   g_object_set_qdata (G_OBJECT (widget), quark_extension_event_mode,
7311                       GINT_TO_POINTER (mode));
7312   g_object_notify (G_OBJECT (widget), "extension-events");
7313 }
7314
7315 /**
7316  * gtk_widget_get_toplevel:
7317  * @widget: a #GtkWidget
7318  * 
7319  * This function returns the topmost widget in the container hierarchy
7320  * @widget is a part of. If @widget has no parent widgets, it will be
7321  * returned as the topmost widget. No reference will be added to the
7322  * returned widget; it should not be unreferenced.
7323  *
7324  * Note the difference in behavior vs. gtk_widget_get_ancestor();
7325  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW)</literal> 
7326  * would return
7327  * %NULL if @widget wasn't inside a toplevel window, and if the
7328  * window was inside a #GtkWindow-derived widget which was in turn
7329  * inside the toplevel #GtkWindow. While the second case may
7330  * seem unlikely, it actually happens when a #GtkPlug is embedded
7331  * inside a #GtkSocket within the same application.
7332  * 
7333  * To reliably find the toplevel #GtkWindow, use
7334  * gtk_widget_get_toplevel() and check if the %TOPLEVEL flags
7335  * is set on the result.
7336  * |[
7337  *  GtkWidget *toplevel = gtk_widget_get_toplevel (widget);
7338  *  if (GTK_WIDGET_TOPLEVEL (toplevel))
7339  *    {
7340  *      /&ast; Perform action on toplevel. &ast;/
7341  *    }
7342  * ]|
7343  *
7344  * Return value: the topmost ancestor of @widget, or @widget itself 
7345  *    if there's no ancestor.
7346  **/
7347 GtkWidget*
7348 gtk_widget_get_toplevel (GtkWidget *widget)
7349 {
7350   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7351   
7352   while (widget->parent)
7353     widget = widget->parent;
7354   
7355   return widget;
7356 }
7357
7358 /**
7359  * gtk_widget_get_ancestor:
7360  * @widget: a #GtkWidget
7361  * @widget_type: ancestor type
7362  * 
7363  * Gets the first ancestor of @widget with type @widget_type. For example,
7364  * <literal>gtk_widget_get_ancestor (widget, GTK_TYPE_BOX)</literal> gets 
7365  * the first #GtkBox that's an ancestor of @widget. No reference will be 
7366  * added to the returned widget; it should not be unreferenced. See note 
7367  * about checking for a toplevel #GtkWindow in the docs for 
7368  * gtk_widget_get_toplevel().
7369  * 
7370  * Note that unlike gtk_widget_is_ancestor(), gtk_widget_get_ancestor() 
7371  * considers @widget to be an ancestor of itself.
7372  *
7373  * Return value: the ancestor widget, or %NULL if not found
7374  **/
7375 GtkWidget*
7376 gtk_widget_get_ancestor (GtkWidget *widget,
7377                          GType      widget_type)
7378 {
7379   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7380   
7381   while (widget && !g_type_is_a (GTK_WIDGET_TYPE (widget), widget_type))
7382     widget = widget->parent;
7383   
7384   if (!(widget && g_type_is_a (GTK_WIDGET_TYPE (widget), widget_type)))
7385     return NULL;
7386   
7387   return widget;
7388 }
7389
7390 /**
7391  * gtk_widget_get_colormap:
7392  * @widget: a #GtkWidget
7393  * 
7394  * Gets the colormap that will be used to render @widget. No reference will
7395  * be added to the returned colormap; it should not be unreferenced.
7396  * 
7397  * Return value: the colormap used by @widget
7398  **/
7399 GdkColormap*
7400 gtk_widget_get_colormap (GtkWidget *widget)
7401 {
7402   GdkColormap *colormap;
7403   GtkWidget *tmp_widget;
7404   
7405   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7406   
7407   if (widget->window)
7408     {
7409       colormap = gdk_drawable_get_colormap (widget->window);
7410       /* If window was destroyed previously, we'll get NULL here */
7411       if (colormap)
7412         return colormap;
7413     }
7414
7415   tmp_widget = widget;
7416   while (tmp_widget)
7417     {
7418       colormap = g_object_get_qdata (G_OBJECT (tmp_widget), quark_colormap);
7419       if (colormap)
7420         return colormap;
7421
7422       tmp_widget= tmp_widget->parent;
7423     }
7424
7425   return gdk_screen_get_default_colormap (gtk_widget_get_screen (widget));
7426 }
7427
7428 /**
7429  * gtk_widget_get_visual:
7430  * @widget: a #GtkWidget
7431  * 
7432  * Gets the visual that will be used to render @widget.
7433  * 
7434  * Return value: the visual for @widget
7435  **/
7436 GdkVisual*
7437 gtk_widget_get_visual (GtkWidget *widget)
7438 {
7439   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7440
7441   return gdk_colormap_get_visual (gtk_widget_get_colormap (widget));
7442 }
7443
7444 /**
7445  * gtk_widget_get_settings:
7446  * @widget: a #GtkWidget
7447  * 
7448  * Gets the settings object holding the settings (global property
7449  * settings, RC file information, etc) used for this widget.
7450  *
7451  * Note that this function can only be called when the #GtkWidget
7452  * is attached to a toplevel, since the settings object is specific
7453  * to a particular #GdkScreen.
7454  * 
7455  * Return value: the relevant #GtkSettings object
7456  **/
7457 GtkSettings*
7458 gtk_widget_get_settings (GtkWidget *widget)
7459 {
7460   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7461   
7462   return gtk_settings_get_for_screen (gtk_widget_get_screen (widget));
7463 }
7464
7465 /**
7466  * gtk_widget_set_colormap:
7467  * @widget: a #GtkWidget
7468  * @colormap: a colormap
7469  *
7470  * Sets the colormap for the widget to the given value. Widget must not
7471  * have been previously realized. This probably should only be used
7472  * from an <function>init()</function> function (i.e. from the constructor 
7473  * for the widget).
7474  **/
7475 void
7476 gtk_widget_set_colormap (GtkWidget   *widget,
7477                          GdkColormap *colormap)
7478 {
7479   g_return_if_fail (GTK_IS_WIDGET (widget));
7480   g_return_if_fail (!GTK_WIDGET_REALIZED (widget));
7481   g_return_if_fail (GDK_IS_COLORMAP (colormap));
7482
7483   g_object_ref (colormap);
7484   
7485   g_object_set_qdata_full (G_OBJECT (widget), 
7486                            quark_colormap,
7487                            colormap,
7488                            g_object_unref);
7489 }
7490
7491 /**
7492  * gtk_widget_get_events:
7493  * @widget: a #GtkWidget
7494  * 
7495  * Returns the event mask for the widget (a bitfield containing flags
7496  * from the #GdkEventMask enumeration). These are the events that the widget
7497  * will receive.
7498  * 
7499  * Return value: event mask for @widget
7500  **/
7501 gint
7502 gtk_widget_get_events (GtkWidget *widget)
7503 {
7504   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
7505
7506   return GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_event_mask));
7507 }
7508
7509 /**
7510  * gtk_widget_get_extension_events:
7511  * @widget: a #GtkWidget
7512  * 
7513  * Retrieves the extension events the widget will receive; see
7514  * gdk_input_set_extension_events().
7515  * 
7516  * Return value: extension events for @widget
7517  **/
7518 GdkExtensionMode
7519 gtk_widget_get_extension_events (GtkWidget *widget)
7520 {
7521   g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);
7522
7523   return GPOINTER_TO_INT (g_object_get_qdata (G_OBJECT (widget), quark_extension_event_mode));
7524 }
7525
7526 /**
7527  * gtk_widget_get_pointer:
7528  * @widget: a #GtkWidget
7529  * @x: return location for the X coordinate, or %NULL
7530  * @y: return location for the Y coordinate, or %NULL
7531  *
7532  * Obtains the location of the mouse pointer in widget coordinates.
7533  * Widget coordinates are a bit odd; for historical reasons, they are
7534  * defined as @widget->window coordinates for widgets that are not
7535  * #GTK_NO_WINDOW widgets, and are relative to @widget->allocation.x,
7536  * @widget->allocation.y for widgets that are #GTK_NO_WINDOW widgets.
7537  **/
7538 void
7539 gtk_widget_get_pointer (GtkWidget *widget,
7540                         gint      *x,
7541                         gint      *y)
7542 {
7543   g_return_if_fail (GTK_IS_WIDGET (widget));
7544   
7545   if (x)
7546     *x = -1;
7547   if (y)
7548     *y = -1;
7549   
7550   if (GTK_WIDGET_REALIZED (widget))
7551     {
7552       gdk_window_get_pointer (widget->window, x, y, NULL);
7553       
7554       if (GTK_WIDGET_NO_WINDOW (widget))
7555         {
7556           if (x)
7557             *x -= widget->allocation.x;
7558           if (y)
7559             *y -= widget->allocation.y;
7560         }
7561     }
7562 }
7563
7564 /**
7565  * gtk_widget_is_ancestor:
7566  * @widget: a #GtkWidget
7567  * @ancestor: another #GtkWidget
7568  * 
7569  * Determines whether @widget is somewhere inside @ancestor, possibly with
7570  * intermediate containers.
7571  * 
7572  * Return value: %TRUE if @ancestor contains @widget as a child, 
7573  *    grandchild, great grandchild, etc.
7574  **/
7575 gboolean
7576 gtk_widget_is_ancestor (GtkWidget *widget,
7577                         GtkWidget *ancestor)
7578 {
7579   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
7580   g_return_val_if_fail (ancestor != NULL, FALSE);
7581   
7582   while (widget)
7583     {
7584       if (widget->parent == ancestor)
7585         return TRUE;
7586       widget = widget->parent;
7587     }
7588   
7589   return FALSE;
7590 }
7591
7592 static GQuark quark_composite_name = 0;
7593
7594 /**
7595  * gtk_widget_set_composite_name:
7596  * @widget: a #GtkWidget.
7597  * @name: the name to set
7598  * 
7599  * Sets a widgets composite name. The widget must be
7600  * a composite child of its parent; see gtk_widget_push_composite_child().
7601  **/
7602 void
7603 gtk_widget_set_composite_name (GtkWidget   *widget,
7604                                const gchar *name)
7605 {
7606   g_return_if_fail (GTK_IS_WIDGET (widget));
7607   g_return_if_fail (GTK_WIDGET_COMPOSITE_CHILD (widget));
7608   g_return_if_fail (name != NULL);
7609
7610   if (!quark_composite_name)
7611     quark_composite_name = g_quark_from_static_string ("gtk-composite-name");
7612
7613   g_object_set_qdata_full (G_OBJECT (widget),
7614                            quark_composite_name,
7615                            g_strdup (name),
7616                            g_free);
7617 }
7618
7619 /**
7620  * gtk_widget_get_composite_name:
7621  * @widget: a #GtkWidget
7622  *
7623  * Obtains the composite name of a widget. 
7624  *
7625  * Returns: the composite name of @widget, or %NULL if @widget is not
7626  *   a composite child. The string should be freed when it is no 
7627  *   longer needed.
7628  **/
7629 gchar*
7630 gtk_widget_get_composite_name (GtkWidget *widget)
7631 {
7632   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
7633
7634   if (GTK_WIDGET_COMPOSITE_CHILD (widget) && widget->parent)
7635     return _gtk_container_child_composite_name (GTK_CONTAINER (widget->parent),
7636                                                widget);
7637   else
7638     return NULL;
7639 }
7640
7641 /**
7642  * gtk_widget_push_composite_child:
7643  * 
7644  * Makes all newly-created widgets as composite children until
7645  * the corresponding gtk_widget_pop_composite_child() call.
7646  * 
7647  * A composite child is a child that's an implementation detail of the
7648  * container it's inside and should not be visible to people using the
7649  * container. Composite children aren't treated differently by GTK (but
7650  * see gtk_container_foreach() vs. gtk_container_forall()), but e.g. GUI 
7651  * builders might want to treat them in a different way.
7652  * 
7653  * Here is a simple example:
7654  * |[
7655  *   gtk_widget_push_composite_child ();
7656  *   scrolled_window->hscrollbar = gtk_hscrollbar_new (hadjustment);
7657  *   gtk_widget_set_composite_name (scrolled_window->hscrollbar, "hscrollbar");
7658  *   gtk_widget_pop_composite_child ();
7659  *   gtk_widget_set_parent (scrolled_window->hscrollbar, 
7660  *                          GTK_WIDGET (scrolled_window));
7661  *   g_object_ref (scrolled_window->hscrollbar);
7662  * ]|
7663  **/
7664 void
7665 gtk_widget_push_composite_child (void)
7666 {
7667   composite_child_stack++;
7668 }
7669
7670 /**
7671  * gtk_widget_pop_composite_child:
7672  *
7673  * Cancels the effect of a previous call to gtk_widget_push_composite_child().
7674  **/ 
7675 void
7676 gtk_widget_pop_composite_child (void)
7677 {
7678   if (composite_child_stack)
7679     composite_child_stack--;
7680 }
7681
7682 /**
7683  * gtk_widget_push_colormap:
7684  * @cmap: a #GdkColormap
7685  *
7686  * Pushes @cmap onto a global stack of colormaps; the topmost
7687  * colormap on the stack will be used to create all widgets.
7688  * Remove @cmap with gtk_widget_pop_colormap(). There's little
7689  * reason to use this function.
7690  **/
7691 void
7692 gtk_widget_push_colormap (GdkColormap *cmap)
7693 {
7694   g_return_if_fail (!cmap || GDK_IS_COLORMAP (cmap));
7695
7696   colormap_stack = g_slist_prepend (colormap_stack, cmap);
7697 }
7698
7699 /**
7700  * gtk_widget_pop_colormap:
7701  *
7702  * Removes a colormap pushed with gtk_widget_push_colormap().
7703  **/
7704 void
7705 gtk_widget_pop_colormap (void)
7706 {
7707   if (colormap_stack)
7708     colormap_stack = g_slist_delete_link (colormap_stack, colormap_stack);
7709 }
7710
7711 /**
7712  * gtk_widget_set_default_colormap:
7713  * @colormap: a #GdkColormap
7714  * 
7715  * Sets the default colormap to use when creating widgets.
7716  * gtk_widget_push_colormap() is a better function to use if
7717  * you only want to affect a few widgets, rather than all widgets.
7718  **/
7719 void
7720 gtk_widget_set_default_colormap (GdkColormap *colormap)
7721 {
7722   g_return_if_fail (GDK_IS_COLORMAP (colormap));
7723   
7724   gdk_screen_set_default_colormap (gdk_colormap_get_screen (colormap),
7725                                    colormap);
7726 }
7727
7728 /**
7729  * gtk_widget_get_default_colormap:
7730  * 
7731  * Obtains the default colormap used to create widgets.
7732  * 
7733  * Return value: default widget colormap
7734  **/
7735 GdkColormap*
7736 gtk_widget_get_default_colormap (void)
7737 {
7738   return gdk_screen_get_default_colormap (gdk_screen_get_default ());
7739 }
7740
7741 /**
7742  * gtk_widget_get_default_visual:
7743  * 
7744  * Obtains the visual of the default colormap. Not really useful;
7745  * used to be useful before gdk_colormap_get_visual() existed.
7746  * 
7747  * Return value: visual of the default colormap
7748  **/
7749 GdkVisual*
7750 gtk_widget_get_default_visual (void)
7751 {
7752   return gdk_colormap_get_visual (gtk_widget_get_default_colormap ());
7753 }
7754
7755 static void
7756 gtk_widget_emit_direction_changed (GtkWidget        *widget,
7757                                    GtkTextDirection  old_dir)
7758 {
7759   gtk_widget_update_pango_context (widget);
7760   
7761   g_signal_emit (widget, widget_signals[DIRECTION_CHANGED], 0, old_dir);
7762 }
7763
7764 /**
7765  * gtk_widget_set_direction:
7766  * @widget: a #GtkWidget
7767  * @dir:    the new direction
7768  * 
7769  * Sets the reading direction on a particular widget. This direction
7770  * controls the primary direction for widgets containing text,
7771  * and also the direction in which the children of a container are
7772  * packed. The ability to set the direction is present in order
7773  * so that correct localization into languages with right-to-left
7774  * reading directions can be done. Generally, applications will
7775  * let the default reading direction present, except for containers
7776  * where the containers are arranged in an order that is explicitely
7777  * visual rather than logical (such as buttons for text justification).
7778  *
7779  * If the direction is set to %GTK_TEXT_DIR_NONE, then the value
7780  * set by gtk_widget_set_default_direction() will be used.
7781  **/
7782 void
7783 gtk_widget_set_direction (GtkWidget        *widget,
7784                           GtkTextDirection  dir)
7785 {
7786   GtkTextDirection old_dir;
7787   
7788   g_return_if_fail (GTK_IS_WIDGET (widget));
7789   g_return_if_fail (dir >= GTK_TEXT_DIR_NONE && dir <= GTK_TEXT_DIR_RTL);
7790
7791   old_dir = gtk_widget_get_direction (widget);
7792   
7793   if (dir == GTK_TEXT_DIR_NONE)
7794     GTK_PRIVATE_UNSET_FLAG (widget, GTK_DIRECTION_SET);
7795   else
7796     {
7797       GTK_PRIVATE_SET_FLAG (widget, GTK_DIRECTION_SET);
7798       if (dir == GTK_TEXT_DIR_LTR)
7799         GTK_PRIVATE_SET_FLAG (widget, GTK_DIRECTION_LTR);
7800       else
7801         GTK_PRIVATE_UNSET_FLAG (widget, GTK_DIRECTION_LTR);
7802     }
7803
7804   if (old_dir != gtk_widget_get_direction (widget))
7805     gtk_widget_emit_direction_changed (widget, old_dir);
7806 }
7807
7808 /**
7809  * gtk_widget_get_direction:
7810  * @widget: a #GtkWidget
7811  * 
7812  * Gets the reading direction for a particular widget. See
7813  * gtk_widget_set_direction().
7814  * 
7815  * Return value: the reading direction for the widget.
7816  **/
7817 GtkTextDirection
7818 gtk_widget_get_direction (GtkWidget *widget)
7819 {
7820   g_return_val_if_fail (GTK_IS_WIDGET (widget), GTK_TEXT_DIR_LTR);
7821   
7822   if (GTK_WIDGET_DIRECTION_SET (widget))
7823     return GTK_WIDGET_DIRECTION_LTR (widget) ? GTK_TEXT_DIR_LTR : GTK_TEXT_DIR_RTL;
7824   else
7825     return gtk_default_direction;
7826 }
7827
7828 static void
7829 gtk_widget_set_default_direction_recurse (GtkWidget *widget, gpointer data)
7830 {
7831   GtkTextDirection old_dir = GPOINTER_TO_UINT (data);
7832
7833   g_object_ref (widget);
7834   
7835   if (!GTK_WIDGET_DIRECTION_SET (widget))
7836     gtk_widget_emit_direction_changed (widget, old_dir);
7837   
7838   if (GTK_IS_CONTAINER (widget))
7839     gtk_container_forall (GTK_CONTAINER (widget),
7840                           gtk_widget_set_default_direction_recurse,
7841                           data);
7842
7843   g_object_unref (widget);
7844 }
7845
7846 /**
7847  * gtk_widget_set_default_direction:
7848  * @dir: the new default direction. This cannot be
7849  *        %GTK_TEXT_DIR_NONE.
7850  * 
7851  * Sets the default reading direction for widgets where the
7852  * direction has not been explicitly set by gtk_widget_set_direction().
7853  **/
7854 void
7855 gtk_widget_set_default_direction (GtkTextDirection dir)
7856 {
7857   g_return_if_fail (dir == GTK_TEXT_DIR_RTL || dir == GTK_TEXT_DIR_LTR);
7858
7859   if (dir != gtk_default_direction)
7860     {
7861       GList *toplevels, *tmp_list;
7862       GtkTextDirection old_dir = gtk_default_direction;
7863       
7864       gtk_default_direction = dir;
7865
7866       tmp_list = toplevels = gtk_window_list_toplevels ();
7867       g_list_foreach (toplevels, (GFunc)g_object_ref, NULL);
7868       
7869       while (tmp_list)
7870         {
7871           gtk_widget_set_default_direction_recurse (tmp_list->data,
7872                                                     GUINT_TO_POINTER (old_dir));
7873           g_object_unref (tmp_list->data);
7874           tmp_list = tmp_list->next;
7875         }
7876
7877       g_list_free (toplevels);
7878     }
7879 }
7880
7881 /**
7882  * gtk_widget_get_default_direction:
7883  * 
7884  * Obtains the current default reading direction. See
7885  * gtk_widget_set_default_direction().
7886  *
7887  * Return value: the current default direction. 
7888  **/
7889 GtkTextDirection
7890 gtk_widget_get_default_direction (void)
7891 {
7892   return gtk_default_direction;
7893 }
7894
7895 static void
7896 gtk_widget_dispose (GObject *object)
7897 {
7898   GtkWidget *widget = GTK_WIDGET (object);
7899
7900   if (widget->parent)
7901     gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
7902   else if (GTK_WIDGET_VISIBLE (widget))
7903     gtk_widget_hide (widget);
7904
7905   GTK_WIDGET_UNSET_FLAGS (widget, GTK_VISIBLE);
7906   if (GTK_WIDGET_REALIZED (widget))
7907     gtk_widget_unrealize (widget);
7908   
7909   G_OBJECT_CLASS (gtk_widget_parent_class)->dispose (object);
7910 }
7911
7912 static void
7913 gtk_widget_real_destroy (GtkObject *object)
7914 {
7915   /* gtk_object_destroy() will already hold a refcount on object */
7916   GtkWidget *widget = GTK_WIDGET (object);
7917
7918   /* wipe accelerator closures (keep order) */
7919   g_object_set_qdata (G_OBJECT (widget), quark_accel_path, NULL);
7920   g_object_set_qdata (G_OBJECT (widget), quark_accel_closures, NULL);
7921
7922   /* Callers of add_mnemonic_label() should disconnect on ::destroy */
7923   g_object_set_qdata (G_OBJECT (widget), quark_mnemonic_labels, NULL);
7924   
7925   gtk_grab_remove (widget);
7926   
7927   g_object_unref (widget->style);
7928   widget->style = gtk_widget_get_default_style ();
7929   g_object_ref (widget->style);
7930
7931   GTK_OBJECT_CLASS (gtk_widget_parent_class)->destroy (object);
7932 }
7933
7934 static void
7935 gtk_widget_finalize (GObject *object)
7936 {
7937   GtkWidget *widget = GTK_WIDGET (object);
7938   GtkWidgetAuxInfo *aux_info;
7939   GtkAccessible *accessible;
7940   
7941   gtk_grab_remove (widget);
7942
7943   g_object_unref (widget->style);
7944   widget->style = NULL;
7945
7946   g_free (widget->name);
7947   
7948   aux_info =_gtk_widget_get_aux_info (widget, FALSE);
7949   if (aux_info)
7950     gtk_widget_aux_info_destroy (aux_info);
7951
7952   accessible = g_object_get_qdata (G_OBJECT (widget), quark_accessible_object);
7953   if (accessible)
7954     g_object_unref (accessible);
7955
7956   G_OBJECT_CLASS (gtk_widget_parent_class)->finalize (object);
7957 }
7958
7959 /*****************************************
7960  * gtk_widget_real_map:
7961  *
7962  *   arguments:
7963  *
7964  *   results:
7965  *****************************************/
7966
7967 static void
7968 gtk_widget_real_map (GtkWidget *widget)
7969 {
7970   g_assert (GTK_WIDGET_REALIZED (widget));
7971   
7972   if (!GTK_WIDGET_MAPPED (widget))
7973     {
7974       GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
7975       
7976       if (!GTK_WIDGET_NO_WINDOW (widget))
7977         gdk_window_show (widget->window);
7978     }
7979 }
7980
7981 /*****************************************
7982  * gtk_widget_real_unmap:
7983  *
7984  *   arguments:
7985  *
7986  *   results:
7987  *****************************************/
7988
7989 static void
7990 gtk_widget_real_unmap (GtkWidget *widget)
7991 {
7992   if (GTK_WIDGET_MAPPED (widget))
7993     {
7994       GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
7995
7996       if (!GTK_WIDGET_NO_WINDOW (widget))
7997         gdk_window_hide (widget->window);
7998     }
7999 }
8000
8001 /*****************************************
8002  * gtk_widget_real_realize:
8003  *
8004  *   arguments:
8005  *
8006  *   results:
8007  *****************************************/
8008
8009 static void
8010 gtk_widget_real_realize (GtkWidget *widget)
8011 {
8012   g_assert (GTK_WIDGET_NO_WINDOW (widget));
8013   
8014   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
8015   if (widget->parent)
8016     {
8017       widget->window = gtk_widget_get_parent_window (widget);
8018       g_object_ref (widget->window);
8019     }
8020   widget->style = gtk_style_attach (widget->style, widget->window);
8021 }
8022
8023 /*****************************************
8024  * gtk_widget_real_unrealize:
8025  *
8026  *   arguments:
8027  *
8028  *   results:
8029  *****************************************/
8030
8031 static void
8032 gtk_widget_real_unrealize (GtkWidget *widget)
8033 {
8034   if (GTK_WIDGET_MAPPED (widget))
8035     gtk_widget_real_unmap (widget);
8036
8037   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
8038
8039   /* printf ("unrealizing %s\n", g_type_name (G_TYPE_FROM_INSTANCE (widget)));
8040    */
8041
8042    /* We must do unrealize child widget BEFORE container widget.
8043     * gdk_window_destroy() destroys specified xwindow and its sub-xwindows.
8044     * So, unrealizing container widget bofore its children causes the problem 
8045     * (for example, gdk_ic_destroy () with destroyed window causes crash. )
8046     */
8047
8048   if (GTK_IS_CONTAINER (widget))
8049     gtk_container_forall (GTK_CONTAINER (widget),
8050                           (GtkCallback) gtk_widget_unrealize,
8051                           NULL);
8052
8053   gtk_style_detach (widget->style);
8054   if (!GTK_WIDGET_NO_WINDOW (widget))
8055     {
8056       gdk_window_set_user_data (widget->window, NULL);
8057       gdk_window_destroy (widget->window);
8058       widget->window = NULL;
8059     }
8060   else
8061     {
8062       g_object_unref (widget->window);
8063       widget->window = NULL;
8064     }
8065
8066   gtk_selection_remove_all (widget);
8067   
8068   GTK_WIDGET_UNSET_FLAGS (widget, GTK_REALIZED);
8069 }
8070
8071 static void
8072 gtk_widget_real_size_request (GtkWidget         *widget,
8073                               GtkRequisition    *requisition)
8074 {
8075   requisition->width = widget->requisition.width;
8076   requisition->height = widget->requisition.height;
8077 }
8078
8079 /**
8080  * _gtk_widget_peek_colormap:
8081  * 
8082  * Returns colormap currently pushed by gtk_widget_push_colormap, if any.
8083  * 
8084  * Return value: the currently pushed colormap, or %NULL if there is none.
8085  **/
8086 GdkColormap*
8087 _gtk_widget_peek_colormap (void)
8088 {
8089   if (colormap_stack)
8090     return (GdkColormap*) colormap_stack->data;
8091   return NULL;
8092 }
8093
8094 /*
8095  * _gtk_widget_set_pointer_window:
8096  * @widget: a #GtkWidget.
8097  * @pointer_window: the new pointer window.
8098  *
8099  * Sets pointer window for @widget.  Does not ref @pointer_window.
8100  * Actually stores it on the #GdkScreen, but you don't need to know that.
8101  */
8102 void
8103 _gtk_widget_set_pointer_window (GtkWidget *widget,
8104                                 GdkWindow *pointer_window)
8105 {
8106   g_return_if_fail (GTK_IS_WIDGET (widget));
8107
8108   if (GTK_WIDGET_REALIZED (widget))
8109     {
8110       GdkScreen *screen = gdk_drawable_get_screen (widget->window);
8111
8112       g_object_set_qdata (G_OBJECT (screen), quark_pointer_window,
8113                           pointer_window);
8114     }
8115 }
8116
8117 /*
8118  * _gtk_widget_get_pointer_window:
8119  * @widget: a #GtkWidget.
8120  *
8121  * Return value: the pointer window set on the #GdkScreen @widget is attached
8122  * to, or %NULL.
8123  */
8124 GdkWindow *
8125 _gtk_widget_get_pointer_window (GtkWidget *widget)
8126 {
8127   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8128
8129   if (GTK_WIDGET_REALIZED (widget))
8130     {
8131       GdkScreen *screen = gdk_drawable_get_screen (widget->window);
8132
8133       return g_object_get_qdata (G_OBJECT (screen), quark_pointer_window);
8134     }
8135
8136   return NULL;
8137 }
8138
8139 static void
8140 synth_crossing (GtkWidget      *widget,
8141                 GdkEventType    type,
8142                 GdkWindow      *window,
8143                 GdkCrossingMode mode,
8144                 GdkNotifyType   detail)
8145 {
8146   GdkEvent *event;
8147   
8148   event = gdk_event_new (type);
8149
8150   event->crossing.window = g_object_ref (window);
8151   event->crossing.send_event = TRUE;
8152   event->crossing.subwindow = g_object_ref (window);
8153   event->crossing.time = GDK_CURRENT_TIME;
8154   event->crossing.x = event->crossing.y = 0;
8155   event->crossing.x_root = event->crossing.y_root = 0;
8156   event->crossing.mode = mode;
8157   event->crossing.detail = detail;
8158   event->crossing.focus = FALSE;
8159   event->crossing.state = 0;
8160
8161   if (!widget)
8162     widget = gtk_get_event_widget (event);
8163
8164   if (widget)
8165     gtk_widget_event_internal (widget, event);
8166
8167   gdk_event_free (event);
8168 }
8169
8170 /*
8171  * _gtk_widget_is_pointer_widget:
8172  * @widget: a #GtkWidget
8173  *
8174  * Returns %TRUE if the pointer window belongs to @widget.
8175  */
8176 gboolean
8177 _gtk_widget_is_pointer_widget (GtkWidget *widget)
8178 {
8179   if (GTK_WIDGET_HAS_POINTER (widget))
8180     {
8181       GdkWindow *win;
8182       GtkWidget *wid;
8183
8184       win = _gtk_widget_get_pointer_window (widget);
8185       if (win)
8186         {
8187           gdk_window_get_user_data (win, (gpointer *)&wid);
8188           if (wid == widget)
8189             return TRUE;
8190         }
8191     }
8192
8193   return FALSE;
8194 }
8195
8196 /*
8197  * _gtk_widget_synthesize_crossing:
8198  * @from: the #GtkWidget the virtual pointer is leaving.
8199  * @to: the #GtkWidget the virtual pointer is moving to.
8200  * @mode: the #GdkCrossingMode to place on the synthesized events.
8201  *
8202  * Generate crossing event(s) on widget state (sensitivity) or GTK+ grab change.
8203  *
8204  * The real pointer window is the window that most recently received an enter notify
8205  * event.  Windows that don't select for crossing events can't become the real
8206  * poiner window.  The real pointer widget that owns the real pointer window.  The
8207  * effective pointer window is the same as the real pointer window unless the real
8208  * pointer widget is either insensitive or there is a grab on a widget that is not
8209  * an ancestor of the real pointer widget (in which case the effective pointer
8210  * window should be the root window).
8211  *
8212  * When the effective pointer window is the same as the real poiner window, we
8213  * receive crossing events from the windowing system.  When the effective pointer
8214  * window changes to become different from the real pointer window we synthesize
8215  * crossing events, attempting to follow X protocol rules:
8216  *
8217  * When the root window becomes the effective pointer window:
8218  *   - leave notify on real pointer window, detail Ancestor
8219  *   - leave notify on all of its ancestors, detail Virtual
8220  *   - enter notify on root window, detail Inferior
8221  *
8222  * When the root window ceases to be the effective pointer window:
8223  *   - leave notify on root window, detail Inferior
8224  *   - enter notify on all ancestors of real pointer window, detail Virtual
8225  *   - enter notify on real pointer window, detail Ancestor
8226  */
8227 void
8228 _gtk_widget_synthesize_crossing (GtkWidget      *from,
8229                                  GtkWidget      *to,
8230                                  GdkCrossingMode mode)
8231 {
8232   GdkWindow *from_window = NULL, *to_window = NULL;
8233
8234   g_return_if_fail (from != NULL || to != NULL);
8235
8236   if (from != NULL)
8237     from_window = GTK_WIDGET_HAS_POINTER (from)
8238       ? _gtk_widget_get_pointer_window (from) : from->window;
8239   if (to != NULL)
8240     to_window = GTK_WIDGET_HAS_POINTER (to)
8241       ? _gtk_widget_get_pointer_window (to) : to->window;
8242
8243   if (from_window == NULL && to_window == NULL)
8244     ;
8245   else if (from_window != NULL && to_window == NULL)
8246     {
8247       GList *from_ancestors = NULL, *list;
8248       GdkWindow *from_ancestor = from_window;
8249
8250       while (from_ancestor != NULL)
8251         {
8252           from_ancestor = gdk_window_get_parent (from_ancestor);
8253           if (from_ancestor == NULL)
8254             break;
8255           from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
8256         }
8257
8258       synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
8259                       mode, GDK_NOTIFY_ANCESTOR);
8260       for (list = g_list_last (from_ancestors); list; list = list->prev)
8261         {
8262           synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
8263                           mode, GDK_NOTIFY_VIRTUAL);
8264         }
8265
8266       /* XXX: enter/inferior on root window? */
8267
8268       g_list_free (from_ancestors);
8269     }
8270   else if (from_window == NULL && to_window != NULL)
8271     {
8272       GList *to_ancestors = NULL, *list;
8273       GdkWindow *to_ancestor = to_window;
8274
8275       while (to_ancestor != NULL)
8276         {
8277           to_ancestor = gdk_window_get_parent (to_ancestor);
8278           if (to_ancestor == NULL)
8279             break;
8280           to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
8281         }
8282
8283       /* XXX: leave/inferior on root window? */
8284
8285       for (list = to_ancestors; list; list = list->next)
8286         {
8287           synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
8288                           mode, GDK_NOTIFY_VIRTUAL);
8289         }
8290       synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
8291                       mode, GDK_NOTIFY_ANCESTOR);
8292
8293       g_list_free (to_ancestors);
8294     }
8295   else if (from_window == to_window)
8296     ;
8297   else
8298     {
8299       GList *from_ancestors = NULL, *to_ancestors = NULL, *list;
8300       GdkWindow *from_ancestor = from_window, *to_ancestor = to_window;
8301
8302       while (from_ancestor != NULL || to_ancestor != NULL)
8303         {
8304           if (from_ancestor != NULL)
8305             {
8306               from_ancestor = gdk_window_get_parent (from_ancestor);
8307               if (from_ancestor == to_window)
8308                 break;
8309               if (from_ancestor)
8310                 from_ancestors = g_list_prepend (from_ancestors, from_ancestor);
8311             }
8312           if (to_ancestor != NULL)
8313             {
8314               to_ancestor = gdk_window_get_parent (to_ancestor);
8315               if (to_ancestor == from_window)
8316                 break;
8317               if (to_ancestor)
8318                 to_ancestors = g_list_prepend (to_ancestors, to_ancestor);
8319             }
8320         }
8321       if (to_ancestor == from_window)
8322         {
8323           if (mode != GDK_CROSSING_GTK_UNGRAB)
8324             synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
8325                             mode, GDK_NOTIFY_INFERIOR);
8326           for (list = to_ancestors; list; list = list->next)
8327             synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data, 
8328                             mode, GDK_NOTIFY_VIRTUAL);
8329           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
8330                           mode, GDK_NOTIFY_ANCESTOR);
8331         }
8332       else if (from_ancestor == to_window)
8333         {
8334           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
8335                           mode, GDK_NOTIFY_ANCESTOR);
8336           for (list = g_list_last (from_ancestors); list; list = list->prev)
8337             {
8338               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
8339                               mode, GDK_NOTIFY_VIRTUAL);
8340             }
8341           if (mode != GDK_CROSSING_GTK_GRAB)
8342             synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
8343                             mode, GDK_NOTIFY_INFERIOR);
8344         }
8345       else
8346         {
8347           while (from_ancestors != NULL && to_ancestors != NULL 
8348                  && from_ancestors->data == to_ancestors->data)
8349             {
8350               from_ancestors = g_list_delete_link (from_ancestors, 
8351                                                    from_ancestors);
8352               to_ancestors = g_list_delete_link (to_ancestors, to_ancestors);
8353             }
8354
8355           synth_crossing (from, GDK_LEAVE_NOTIFY, from_window,
8356                           mode, GDK_NOTIFY_NONLINEAR);
8357
8358           for (list = g_list_last (from_ancestors); list; list = list->prev)
8359             {
8360               synth_crossing (NULL, GDK_LEAVE_NOTIFY, (GdkWindow *) list->data,
8361                               mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
8362             }
8363           for (list = to_ancestors; list; list = list->next)
8364             {
8365               synth_crossing (NULL, GDK_ENTER_NOTIFY, (GdkWindow *) list->data,
8366                               mode, GDK_NOTIFY_NONLINEAR_VIRTUAL);
8367             }
8368           synth_crossing (to, GDK_ENTER_NOTIFY, to_window,
8369                           mode, GDK_NOTIFY_NONLINEAR);
8370         }
8371       g_list_free (from_ancestors);
8372       g_list_free (to_ancestors);
8373     }
8374 }
8375
8376 static void
8377 gtk_widget_propagate_state (GtkWidget           *widget,
8378                             GtkStateData        *data)
8379 {
8380   guint8 old_state = GTK_WIDGET_STATE (widget);
8381   guint8 old_saved_state = GTK_WIDGET_SAVED_STATE (widget);
8382
8383   /* don't call this function with state==GTK_STATE_INSENSITIVE,
8384    * parent_sensitive==TRUE on a sensitive widget
8385    */
8386
8387
8388   if (data->parent_sensitive)
8389     GTK_WIDGET_SET_FLAGS (widget, GTK_PARENT_SENSITIVE);
8390   else
8391     GTK_WIDGET_UNSET_FLAGS (widget, GTK_PARENT_SENSITIVE);
8392
8393   if (GTK_WIDGET_IS_SENSITIVE (widget))
8394     {
8395       if (data->state_restoration)
8396         GTK_WIDGET_STATE (widget) = GTK_WIDGET_SAVED_STATE (widget);
8397       else
8398         GTK_WIDGET_STATE (widget) = data->state;
8399     }
8400   else
8401     {
8402       if (!data->state_restoration)
8403         {
8404           if (data->state != GTK_STATE_INSENSITIVE)
8405             GTK_WIDGET_SAVED_STATE (widget) = data->state;
8406         }
8407       else if (GTK_WIDGET_STATE (widget) != GTK_STATE_INSENSITIVE)
8408         GTK_WIDGET_SAVED_STATE (widget) = GTK_WIDGET_STATE (widget);
8409       GTK_WIDGET_STATE (widget) = GTK_STATE_INSENSITIVE;
8410     }
8411
8412   if (gtk_widget_is_focus (widget) && !GTK_WIDGET_IS_SENSITIVE (widget))
8413     {
8414       GtkWidget *window;
8415
8416       window = gtk_widget_get_toplevel (widget);
8417       if (window && GTK_WIDGET_TOPLEVEL (window))
8418         gtk_window_set_focus (GTK_WINDOW (window), NULL);
8419     }
8420
8421   if (old_state != GTK_WIDGET_STATE (widget) ||
8422       old_saved_state != GTK_WIDGET_SAVED_STATE (widget))
8423     {
8424       g_object_ref (widget);
8425
8426       if (!GTK_WIDGET_IS_SENSITIVE (widget) && GTK_WIDGET_HAS_GRAB (widget))
8427         gtk_grab_remove (widget);
8428
8429       g_signal_emit (widget, widget_signals[STATE_CHANGED], 0, old_state);
8430
8431       if (GTK_WIDGET_HAS_POINTER (widget) && !GTK_WIDGET_SHADOWED (widget))
8432         {
8433           if (!GTK_WIDGET_IS_SENSITIVE (widget))
8434             _gtk_widget_synthesize_crossing (widget, NULL, 
8435                                              GDK_CROSSING_STATE_CHANGED);
8436           else if (old_state == GTK_STATE_INSENSITIVE)
8437             _gtk_widget_synthesize_crossing (NULL, widget, 
8438                                              GDK_CROSSING_STATE_CHANGED);
8439         }
8440
8441       if (GTK_IS_CONTAINER (widget))
8442         {
8443           data->parent_sensitive = (GTK_WIDGET_IS_SENSITIVE (widget) != FALSE);
8444           if (data->use_forall)
8445             gtk_container_forall (GTK_CONTAINER (widget),
8446                                   (GtkCallback) gtk_widget_propagate_state,
8447                                   data);
8448           else
8449             gtk_container_foreach (GTK_CONTAINER (widget),
8450                                    (GtkCallback) gtk_widget_propagate_state,
8451                                    data);
8452         }
8453       g_object_unref (widget);
8454     }
8455 }
8456
8457 /*
8458  * _gtk_widget_get_aux_info:
8459  * @widget: a #GtkWidget
8460  * @create: if %TRUE, create the structure if it doesn't exist
8461  * 
8462  * Get the #GtkWidgetAuxInfo structure for the widget.
8463  * 
8464  * Return value: the #GtkAuxInfo structure for the widget, or
8465  *    %NULL if @create is %FALSE and one doesn't already exist.
8466  */
8467 GtkWidgetAuxInfo*
8468 _gtk_widget_get_aux_info (GtkWidget *widget,
8469                           gboolean   create)
8470 {
8471   GtkWidgetAuxInfo *aux_info;
8472   
8473   aux_info = g_object_get_qdata (G_OBJECT (widget), quark_aux_info);
8474   if (!aux_info && create)
8475     {
8476       aux_info = g_slice_new (GtkWidgetAuxInfo);
8477
8478       aux_info->width = -1;
8479       aux_info->height = -1;
8480       aux_info->x = 0;
8481       aux_info->y = 0;
8482       aux_info->x_set = FALSE;
8483       aux_info->y_set = FALSE;
8484       g_object_set_qdata (G_OBJECT (widget), quark_aux_info, aux_info);
8485     }
8486   
8487   return aux_info;
8488 }
8489
8490 /*****************************************
8491  * gtk_widget_aux_info_destroy:
8492  *
8493  *   arguments:
8494  *
8495  *   results:
8496  *****************************************/
8497
8498 static void
8499 gtk_widget_aux_info_destroy (GtkWidgetAuxInfo *aux_info)
8500 {
8501   g_slice_free (GtkWidgetAuxInfo, aux_info);
8502 }
8503
8504 static void
8505 gtk_widget_shape_info_destroy (GtkWidgetShapeInfo *info)
8506 {
8507   g_object_unref (info->shape_mask);
8508   g_slice_free (GtkWidgetShapeInfo, info);
8509 }
8510
8511 /**
8512  * gtk_widget_shape_combine_mask: 
8513  * @widget: a #GtkWidget
8514  * @shape_mask: shape to be added, or %NULL to remove an existing shape
8515  * @offset_x: X position of shape mask with respect to @window
8516  * @offset_y: Y position of shape mask with respect to @window
8517  * 
8518  * Sets a shape for this widget's GDK window. This allows for
8519  * transparent windows etc., see gdk_window_shape_combine_mask()
8520  * for more information.
8521  **/
8522 void
8523 gtk_widget_shape_combine_mask (GtkWidget *widget,
8524                                GdkBitmap *shape_mask,
8525                                gint       offset_x,
8526                                gint       offset_y)
8527 {
8528   GtkWidgetShapeInfo* shape_info;
8529   
8530   g_return_if_fail (GTK_IS_WIDGET (widget));
8531   /*  set_shape doesn't work on widgets without gdk window */
8532   g_return_if_fail (!GTK_WIDGET_NO_WINDOW (widget));
8533
8534   if (!shape_mask)
8535     {
8536       GTK_PRIVATE_UNSET_FLAG (widget, GTK_HAS_SHAPE_MASK);
8537       
8538       if (widget->window)
8539         gdk_window_shape_combine_mask (widget->window, NULL, 0, 0);
8540       
8541       g_object_set_qdata (G_OBJECT (widget), quark_shape_info, NULL);
8542     }
8543   else
8544     {
8545       GTK_PRIVATE_SET_FLAG (widget, GTK_HAS_SHAPE_MASK);
8546       
8547       shape_info = g_slice_new (GtkWidgetShapeInfo);
8548       g_object_set_qdata_full (G_OBJECT (widget), quark_shape_info, shape_info,
8549                                (GDestroyNotify) gtk_widget_shape_info_destroy);
8550       
8551       shape_info->shape_mask = g_object_ref (shape_mask);
8552       shape_info->offset_x = offset_x;
8553       shape_info->offset_y = offset_y;
8554       
8555       /* set shape if widget has a gdk window already.
8556        * otherwise the shape is scheduled to be set by gtk_widget_realize().
8557        */
8558       if (widget->window)
8559         gdk_window_shape_combine_mask (widget->window, shape_mask,
8560                                        offset_x, offset_y);
8561     }
8562 }
8563
8564 /**
8565  * gtk_widget_input_shape_combine_mask: 
8566  * @widget: a #GtkWidget
8567  * @shape_mask: shape to be added, or %NULL to remove an existing shape
8568  * @offset_x: X position of shape mask with respect to @window
8569  * @offset_y: Y position of shape mask with respect to @window
8570  * 
8571  * Sets an input shape for this widget's GDK window. This allows for
8572  * windows which react to mouse click in a nonrectangular region, see 
8573  * gdk_window_input_shape_combine_mask() for more information.
8574  *
8575  * Since: 2.10
8576  **/
8577 void
8578 gtk_widget_input_shape_combine_mask (GtkWidget *widget,
8579                                      GdkBitmap *shape_mask,
8580                                      gint       offset_x,
8581                                      gint       offset_y)
8582 {
8583   GtkWidgetShapeInfo* shape_info;
8584   
8585   g_return_if_fail (GTK_IS_WIDGET (widget));
8586   /*  set_shape doesn't work on widgets without gdk window */
8587   g_return_if_fail (!GTK_WIDGET_NO_WINDOW (widget));
8588
8589   if (!shape_mask)
8590     {
8591       if (widget->window)
8592         gdk_window_input_shape_combine_mask (widget->window, NULL, 0, 0);
8593       
8594       g_object_set_qdata (G_OBJECT (widget), quark_input_shape_info, NULL);
8595     }
8596   else
8597     {
8598       shape_info = g_slice_new (GtkWidgetShapeInfo);
8599       g_object_set_qdata_full (G_OBJECT (widget), quark_input_shape_info, 
8600                                shape_info,
8601                                (GDestroyNotify) gtk_widget_shape_info_destroy);
8602       
8603       shape_info->shape_mask = g_object_ref (shape_mask);
8604       shape_info->offset_x = offset_x;
8605       shape_info->offset_y = offset_y;
8606       
8607       /* set shape if widget has a gdk window already.
8608        * otherwise the shape is scheduled to be set by gtk_widget_realize().
8609        */
8610       if (widget->window)
8611         gdk_window_input_shape_combine_mask (widget->window, shape_mask,
8612                                              offset_x, offset_y);
8613     }
8614 }
8615
8616
8617 static void
8618 gtk_reset_shapes_recurse (GtkWidget *widget,
8619                           GdkWindow *window)
8620 {
8621   gpointer data;
8622   GList *list;
8623
8624   gdk_window_get_user_data (window, &data);
8625   if (data != widget)
8626     return;
8627
8628   gdk_window_shape_combine_mask (window, NULL, 0, 0);
8629   for (list = gdk_window_peek_children (window); list; list = list->next)
8630     gtk_reset_shapes_recurse (widget, list->data);
8631 }
8632
8633 /**
8634  * gtk_widget_reset_shapes:
8635  * @widget: a #GtkWidget
8636  *
8637  * Recursively resets the shape on this widget and its descendants.
8638  **/
8639 void
8640 gtk_widget_reset_shapes (GtkWidget *widget)
8641 {
8642   g_return_if_fail (GTK_IS_WIDGET (widget));
8643   g_return_if_fail (GTK_WIDGET_REALIZED (widget));
8644
8645   if (!GTK_WIDGET_HAS_SHAPE_MASK (widget))
8646     gtk_reset_shapes_recurse (widget, widget->window);
8647 }
8648
8649 /**
8650  * gtk_widget_ref:
8651  * @widget: a #GtkWidget
8652  * 
8653  * Adds a reference to a widget. This function is exactly the same
8654  * as calling g_object_ref(), and exists mostly for historical
8655  * reasons. It can still be convenient to avoid casting a widget
8656  * to a #GObject, it saves a small amount of typing.
8657  * 
8658  * Return value: the widget that was referenced
8659  *
8660  * Deprecated: 2.12: Use g_object_ref() instead.
8661  **/
8662 GtkWidget*
8663 gtk_widget_ref (GtkWidget *widget)
8664 {
8665   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8666
8667   return (GtkWidget*) g_object_ref ((GObject*) widget);
8668 }
8669
8670 /**
8671  * gtk_widget_unref:
8672  * @widget: a #GtkWidget
8673  *
8674  * Inverse of gtk_widget_ref(). Equivalent to g_object_unref().
8675  * 
8676  * Deprecated: 2.12: Use g_object_unref() instead.
8677  **/
8678 void
8679 gtk_widget_unref (GtkWidget *widget)
8680 {
8681   g_return_if_fail (GTK_IS_WIDGET (widget));
8682
8683   g_object_unref ((GObject*) widget);
8684 }
8685
8686 static void
8687 expose_window (GdkWindow *window)
8688 {
8689   GdkEvent event;
8690   GList *l, *children;
8691   gpointer user_data;
8692   gboolean is_double_buffered;
8693
8694   gdk_window_get_user_data (window, &user_data);
8695
8696   if (user_data)
8697     is_double_buffered = GTK_WIDGET_DOUBLE_BUFFERED (GTK_WIDGET (user_data));
8698   else
8699     is_double_buffered = FALSE;
8700   
8701   event.expose.type = GDK_EXPOSE;
8702   event.expose.window = g_object_ref (window);
8703   event.expose.send_event = FALSE;
8704   event.expose.count = 0;
8705   event.expose.area.x = 0;
8706   event.expose.area.y = 0;
8707   gdk_drawable_get_size (GDK_DRAWABLE (window),
8708                          &event.expose.area.width,
8709                          &event.expose.area.height);
8710   event.expose.region = gdk_region_rectangle (&event.expose.area);
8711
8712   /* If this is not double buffered, force a double buffer so that
8713      redirection works. */
8714   if (!is_double_buffered)
8715     gdk_window_begin_paint_region (window, event.expose.region);
8716   
8717   gtk_main_do_event (&event);
8718
8719   if (!is_double_buffered)
8720     gdk_window_end_paint (window);
8721   
8722   children = gdk_window_peek_children (window);
8723   for (l = children; l != NULL; l = l->next)
8724     {
8725       GdkWindow *child = l->data;
8726
8727       /* Don't expose input-only windows */
8728       if (gdk_drawable_get_depth (GDK_DRAWABLE (child)) != 0)
8729         expose_window (l->data);
8730     }
8731   
8732   g_object_unref (window);
8733 }
8734
8735 /**
8736  * gtk_widget_get_snapshot:
8737  * @widget:    a #GtkWidget
8738  * @clip_rect: a #GdkRectangle or %NULL
8739  *
8740  * Create a #GdkPixmap of the contents of the widget and its children.
8741  *
8742  * Works even if the widget is obscured. The depth and visual of the
8743  * resulting pixmap is dependent on the widget being snapshot and likely
8744  * differs from those of a target widget displaying the pixmap.
8745  * The function gdk_pixbuf_get_from_drawable() can be used to convert
8746  * the pixmap to a visual independant representation.
8747  *
8748  * The snapshot area used by this function is the @widget's allocation plus
8749  * any extra space occupied by additional windows belonging to this widget
8750  * (such as the arrows of a spin button).
8751  * Thus, the resulting snapshot pixmap is possibly larger than the allocation.
8752  * 
8753  * If @clip_rect is non-%NULL, the resulting pixmap is shrunken to
8754  * match the specified clip_rect. The (x,y) coordinates of @clip_rect are
8755  * interpreted widget relative. If width or height of @clip_rect are 0 or
8756  * negative, the width or height of the resulting pixmap will be shrunken
8757  * by the respective amount.
8758  * For instance a @clip_rect <literal>{ +5, +5, -10, -10 }</literal> will
8759  * chop off 5 pixels at each side of the snapshot pixmap.
8760  * If non-%NULL, @clip_rect will contain the exact widget-relative snapshot
8761  * coordinates upon return. A @clip_rect of <literal>{ -1, -1, 0, 0 }</literal>
8762  * can be used to preserve the auto-grown snapshot area and use @clip_rect
8763  * as a pure output parameter.
8764  *
8765  * The returned pixmap can be %NULL, if the resulting @clip_area was empty.
8766  *
8767  * Return value: #GdkPixmap snapshot of the widget
8768  * 
8769  * Since: 2.14
8770  **/
8771 GdkPixmap*
8772 gtk_widget_get_snapshot (GtkWidget    *widget,
8773                          GdkRectangle *clip_rect)
8774 {
8775   int x, y, width, height;
8776   GdkWindow *parent_window = NULL;
8777   GdkPixmap *pixmap;
8778   GList *windows = NULL, *list;
8779
8780   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
8781   if (!GTK_WIDGET_VISIBLE (widget))
8782     return NULL;
8783
8784   /* the widget (and parent_window) must be realized to be drawable */
8785   if (widget->parent && !GTK_WIDGET_REALIZED (widget->parent))
8786     gtk_widget_realize (widget->parent);
8787   if (!GTK_WIDGET_REALIZED (widget))
8788     gtk_widget_realize (widget);
8789
8790   /* determine snapshot rectangle */
8791   x = widget->allocation.x;
8792   y = widget->allocation.y;
8793   width = widget->allocation.width;
8794   height = widget->allocation.height;
8795
8796   if (widget->parent && !GTK_WIDGET_NO_WINDOW (widget))
8797     {
8798       /* grow snapshot rectangle to cover all widget windows */
8799       parent_window = gtk_widget_get_parent_window (widget);
8800       for (list = gdk_window_peek_children (parent_window); list; list = list->next)
8801         {
8802           GdkWindow *subwin = list->data;
8803           gpointer windata;
8804           int wx, wy, ww, wh;
8805           gdk_window_get_user_data (subwin, &windata);
8806           if (windata != widget)
8807             continue;
8808           windows = g_list_prepend (windows, subwin);
8809           gdk_window_get_position (subwin, &wx, &wy);
8810           gdk_drawable_get_size (subwin, &ww, &wh);
8811           /* grow snapshot rectangle by extra widget sub window */
8812           if (wx < x)
8813             {
8814               width += x - wx;
8815               x = wx;
8816             }
8817           if (wy < y)
8818             {
8819               height += y - wy;
8820               y = wy;
8821             }
8822           if (x + width < wx + ww)
8823             width += wx + ww - (x + width);
8824           if (y + height < wy + wh)
8825             height += wy + wh - (y + height);
8826         }
8827     }
8828   else if (!widget->parent)
8829     x = y = 0; /* toplevel */
8830
8831   /* at this point, (x,y,width,height) is the parent_window relative
8832    * snapshot area covering all of widget's windows.
8833    */
8834
8835   /* shrink snapshot size by clip_rectangle */
8836   if (clip_rect)
8837     {
8838       GdkRectangle snap = { x, y, width, height }, clip = *clip_rect;
8839       clip.x = clip.x < 0 ? x : clip.x;
8840       clip.y = clip.y < 0 ? y : clip.y;
8841       clip.width = clip.width <= 0 ? MAX (0, width + clip.width) : clip.width;
8842       clip.height = clip.height <= 0 ? MAX (0, height + clip.height) : clip.height;
8843       if (widget->parent)
8844         {
8845           /* offset clip_rect, so it's parent_window relative */
8846           if (clip_rect->x >= 0)
8847             clip.x += widget->allocation.x;
8848           if (clip_rect->y >= 0)
8849             clip.y += widget->allocation.y;
8850         }
8851       if (!gdk_rectangle_intersect (&snap, &clip, &snap))
8852         {
8853           g_list_free (windows);
8854           clip_rect->width = clip_rect->height = 0;
8855           return NULL; /* empty snapshot area */
8856         }
8857       x = snap.x;
8858       y = snap.y;
8859       width = snap.width;
8860       height = snap.height;
8861     }
8862
8863   /* render snapshot */
8864   pixmap = gdk_pixmap_new (widget->window, width, height, gdk_drawable_get_depth (widget->window));
8865   for (list = windows; list; list = list->next) /* !NO_WINDOW widgets */
8866     {
8867       GdkWindow *subwin = list->data;
8868       int wx, wy;
8869       if (gdk_drawable_get_depth (GDK_DRAWABLE (subwin)) == 0)
8870         continue; /* Input only window */
8871       gdk_window_get_position (subwin, &wx, &wy);
8872       gdk_window_redirect_to_drawable (subwin, pixmap, MAX (0, x - wx), MAX (0, y - wy),
8873                                        MAX (0, wx - x), MAX (0, wy - y), width, height);
8874
8875       expose_window (subwin);
8876     }
8877   if (!windows) /* NO_WINDOW || toplevel => parent_window == NULL || parent_window == widget->window */
8878     {
8879       gdk_window_redirect_to_drawable (widget->window, pixmap, x, y, 0, 0, width, height);
8880       expose_window (widget->window);
8881     }
8882   for (list = windows; list; list = list->next)
8883     gdk_window_remove_redirection (list->data);
8884   if (!windows) /* NO_WINDOW || toplevel */
8885     gdk_window_remove_redirection (widget->window);
8886   g_list_free (windows);
8887
8888   /* return pixmap and snapshot rectangle coordinates */
8889   if (clip_rect)
8890     {
8891       clip_rect->x = x;
8892       clip_rect->y = y;
8893       clip_rect->width = width;
8894       clip_rect->height = height;
8895       if (widget->parent)
8896         {
8897           /* offset clip_rect from parent_window so it's widget relative */
8898           clip_rect->x -= widget->allocation.x;
8899           clip_rect->y -= widget->allocation.y;
8900         }
8901       if (0)
8902         g_printerr ("gtk_widget_get_snapshot: %s (%d,%d, %dx%d)\n",
8903                     G_OBJECT_TYPE_NAME (widget),
8904                     clip_rect->x, clip_rect->y, clip_rect->width, clip_rect->height);
8905     }
8906   return pixmap;
8907 }
8908
8909 /* style properties
8910  */
8911
8912 /**
8913  * gtk_widget_class_install_style_property_parser:
8914  * @klass: a #GtkWidgetClass
8915  * @pspec: the #GParamSpec for the style property
8916  * @parser: the parser for the style property
8917  * 
8918  * Installs a style property on a widget class. 
8919  **/
8920 void
8921 gtk_widget_class_install_style_property_parser (GtkWidgetClass     *klass,
8922                                                 GParamSpec         *pspec,
8923                                                 GtkRcPropertyParser parser)
8924 {
8925   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
8926   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
8927   g_return_if_fail (pspec->flags & G_PARAM_READABLE);
8928   g_return_if_fail (!(pspec->flags & (G_PARAM_CONSTRUCT_ONLY | G_PARAM_CONSTRUCT)));
8929   
8930   if (g_param_spec_pool_lookup (style_property_spec_pool, pspec->name, G_OBJECT_CLASS_TYPE (klass), FALSE))
8931     {
8932       g_warning (G_STRLOC ": class `%s' already contains a style property named `%s'",
8933                  G_OBJECT_CLASS_NAME (klass),
8934                  pspec->name);
8935       return;
8936     }
8937
8938   g_param_spec_ref_sink (pspec);
8939   g_param_spec_set_qdata (pspec, quark_property_parser, (gpointer) parser);
8940   g_param_spec_pool_insert (style_property_spec_pool, pspec, G_OBJECT_CLASS_TYPE (klass));
8941 }
8942
8943 /**
8944  * gtk_widget_class_install_style_property:
8945  * @klass: a #GtkWidgetClass
8946  * @pspec: the #GParamSpec for the property
8947  * 
8948  * Installs a style property on a widget class. The parser for the
8949  * style property is determined by the value type of @pspec.
8950  **/
8951 void
8952 gtk_widget_class_install_style_property (GtkWidgetClass *klass,
8953                                          GParamSpec     *pspec)
8954 {
8955   GtkRcPropertyParser parser;
8956
8957   g_return_if_fail (GTK_IS_WIDGET_CLASS (klass));
8958   g_return_if_fail (G_IS_PARAM_SPEC (pspec));
8959
8960   parser = _gtk_rc_property_parser_from_type (G_PARAM_SPEC_VALUE_TYPE (pspec));
8961
8962   gtk_widget_class_install_style_property_parser (klass, pspec, parser);
8963 }
8964
8965 /**
8966  * gtk_widget_class_find_style_property:
8967  * @klass: a #GtkWidgetClass
8968  * @property_name: the name of the style property to find
8969  * @returns: the #GParamSpec of the style property or %NULL if @class has no
8970  *   style property with that name.
8971  *
8972  * Finds a style property of a widget class by name.
8973  *
8974  * Since: 2.2
8975  */
8976 GParamSpec*
8977 gtk_widget_class_find_style_property (GtkWidgetClass *klass,
8978                                       const gchar    *property_name)
8979 {
8980   g_return_val_if_fail (property_name != NULL, NULL);
8981
8982   return g_param_spec_pool_lookup (style_property_spec_pool,
8983                                    property_name,
8984                                    G_OBJECT_CLASS_TYPE (klass),
8985                                    TRUE);
8986 }
8987
8988 /**
8989  * gtk_widget_class_list_style_properties:
8990  * @klass: a #GtkWidgetClass
8991  * @n_properties: location to return the number of style properties found
8992  * @returns: an newly allocated array of #GParamSpec*. The array must 
8993  *       be freed with g_free().
8994  *
8995  * Returns all style properties of a widget class.
8996  *
8997  * Since: 2.2
8998  */
8999 GParamSpec**
9000 gtk_widget_class_list_style_properties (GtkWidgetClass *klass,
9001                                         guint          *n_properties)
9002 {
9003   GParamSpec **pspecs;
9004   guint n;
9005
9006   pspecs = g_param_spec_pool_list (style_property_spec_pool,
9007                                    G_OBJECT_CLASS_TYPE (klass),
9008                                    &n);
9009   if (n_properties)
9010     *n_properties = n;
9011
9012   return pspecs;
9013 }
9014
9015 /**
9016  * gtk_widget_style_get_property:
9017  * @widget: a #GtkWidget
9018  * @property_name: the name of a style property
9019  * @value: location to return the property value 
9020  *
9021  * Gets the value of a style property of @widget.
9022  */
9023 void
9024 gtk_widget_style_get_property (GtkWidget   *widget,
9025                                const gchar *property_name,
9026                                GValue      *value)
9027 {
9028   GParamSpec *pspec;
9029
9030   g_return_if_fail (GTK_IS_WIDGET (widget));
9031   g_return_if_fail (property_name != NULL);
9032   g_return_if_fail (G_IS_VALUE (value));
9033
9034   g_object_ref (widget);
9035   pspec = g_param_spec_pool_lookup (style_property_spec_pool,
9036                                     property_name,
9037                                     G_OBJECT_TYPE (widget),
9038                                     TRUE);
9039   if (!pspec)
9040     g_warning ("%s: widget class `%s' has no property named `%s'",
9041                G_STRLOC,
9042                G_OBJECT_TYPE_NAME (widget),
9043                property_name);
9044   else
9045     {
9046       const GValue *peek_value;
9047
9048       peek_value = _gtk_style_peek_property_value (widget->style,
9049                                                    G_OBJECT_TYPE (widget),
9050                                                    pspec,
9051                                                    (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser));
9052       
9053       /* auto-conversion of the caller's value type
9054        */
9055       if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
9056         g_value_copy (peek_value, value);
9057       else if (g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
9058         g_value_transform (peek_value, value);
9059       else
9060         g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
9061                    pspec->name,
9062                    g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
9063                    G_VALUE_TYPE_NAME (value));
9064     }
9065   g_object_unref (widget);
9066 }
9067
9068 /**
9069  * gtk_widget_style_get_valist:
9070  * @widget: a #GtkWidget
9071  * @first_property_name: the name of the first property to get
9072  * @var_args: a <type>va_list</type> of pairs of property names and
9073  *     locations to return the property values, starting with the location
9074  *     for @first_property_name.
9075  * 
9076  * Non-vararg variant of gtk_widget_style_get(). Used primarily by language 
9077  * bindings.
9078  */ 
9079 void
9080 gtk_widget_style_get_valist (GtkWidget   *widget,
9081                              const gchar *first_property_name,
9082                              va_list      var_args)
9083 {
9084   const gchar *name;
9085
9086   g_return_if_fail (GTK_IS_WIDGET (widget));
9087
9088   g_object_ref (widget);
9089
9090   name = first_property_name;
9091   while (name)
9092     {
9093       const GValue *peek_value;
9094       GParamSpec *pspec;
9095       gchar *error;
9096
9097       pspec = g_param_spec_pool_lookup (style_property_spec_pool,
9098                                         name,
9099                                         G_OBJECT_TYPE (widget),
9100                                         TRUE);
9101       if (!pspec)
9102         {
9103           g_warning ("%s: widget class `%s' has no property named `%s'",
9104                      G_STRLOC,
9105                      G_OBJECT_TYPE_NAME (widget),
9106                      name);
9107           break;
9108         }
9109       /* style pspecs are always readable so we can spare that check here */
9110
9111       peek_value = _gtk_style_peek_property_value (widget->style,
9112                                                    G_OBJECT_TYPE (widget),
9113                                                    pspec,
9114                                                    (GtkRcPropertyParser) g_param_spec_get_qdata (pspec, quark_property_parser));
9115       G_VALUE_LCOPY (peek_value, var_args, 0, &error);
9116       if (error)
9117         {
9118           g_warning ("%s: %s", G_STRLOC, error);
9119           g_free (error);
9120           break;
9121         }
9122
9123       name = va_arg (var_args, gchar*);
9124     }
9125
9126   g_object_unref (widget);
9127 }
9128
9129 /**
9130  * gtk_widget_style_get:
9131  * @widget: a #GtkWidget
9132  * @first_property_name: the name of the first property to get
9133  * @Varargs: pairs of property names and locations to 
9134  *   return the property values, starting with the location for 
9135  *   @first_property_name, terminated by %NULL.
9136  *
9137  * Gets the values of a multiple style properties of @widget.
9138  */
9139 void
9140 gtk_widget_style_get (GtkWidget   *widget,
9141                       const gchar *first_property_name,
9142                       ...)
9143 {
9144   va_list var_args;
9145
9146   g_return_if_fail (GTK_IS_WIDGET (widget));
9147
9148   va_start (var_args, first_property_name);
9149   gtk_widget_style_get_valist (widget, first_property_name, var_args);
9150   va_end (var_args);
9151 }
9152
9153 /**
9154  * gtk_widget_path:
9155  * @widget: a #GtkWidget
9156  * @path_length: location to store length of the path, or %NULL
9157  * @path: location to store allocated path string, or %NULL 
9158  * @path_reversed: location to store allocated reverse path string, or %NULL
9159  *
9160  * Obtains the full path to @widget. The path is simply the name of a
9161  * widget and all its parents in the container hierarchy, separated by
9162  * periods. The name of a widget comes from
9163  * gtk_widget_get_name(). Paths are used to apply styles to a widget
9164  * in gtkrc configuration files. Widget names are the type of the
9165  * widget by default (e.g. "GtkButton") or can be set to an
9166  * application-specific value with gtk_widget_set_name(). By setting
9167  * the name of a widget, you allow users or theme authors to apply
9168  * styles to that specific widget in their gtkrc
9169  * file. @path_reversed_p fills in the path in reverse order,
9170  * i.e. starting with @widget's name instead of starting with the name
9171  * of @widget's outermost ancestor.
9172  **/
9173 void
9174 gtk_widget_path (GtkWidget *widget,
9175                  guint     *path_length,
9176                  gchar    **path,
9177                  gchar    **path_reversed)
9178 {
9179   static gchar *rev_path = NULL;
9180   static guint tmp_path_len = 0;
9181   guint len;
9182   
9183   g_return_if_fail (GTK_IS_WIDGET (widget));
9184   
9185   len = 0;
9186   do
9187     {
9188       const gchar *string;
9189       const gchar *s;
9190       gchar *d;
9191       guint l;
9192       
9193       string = gtk_widget_get_name (widget);
9194       l = strlen (string);
9195       while (tmp_path_len <= len + l + 1)
9196         {
9197           tmp_path_len += INIT_PATH_SIZE;
9198           rev_path = g_realloc (rev_path, tmp_path_len);
9199         }
9200       s = string + l - 1;
9201       d = rev_path + len;
9202       while (s >= string)
9203         *(d++) = *(s--);
9204       len += l;
9205       
9206       widget = widget->parent;
9207       
9208       if (widget)
9209         rev_path[len++] = '.';
9210       else
9211         rev_path[len++] = 0;
9212     }
9213   while (widget);
9214   
9215   if (path_length)
9216     *path_length = len - 1;
9217   if (path_reversed)
9218     *path_reversed = g_strdup (rev_path);
9219   if (path)
9220     {
9221       *path = g_strdup (rev_path);
9222       g_strreverse (*path);
9223     }
9224 }
9225
9226 /**
9227  * gtk_widget_class_path:
9228  * @widget: a #GtkWidget
9229  * @path_length: location to store the length of the class path, or %NULL
9230  * @path: location to store the class path as an allocated string, or %NULL
9231  * @path_reversed: location to store the reverse class path as an allocated 
9232  *    string, or %NULL
9233  *
9234  * Same as gtk_widget_path(), but always uses the name of a widget's type,
9235  * never uses a custom name set with gtk_widget_set_name().
9236  * 
9237  **/
9238 void
9239 gtk_widget_class_path (GtkWidget *widget,
9240                        guint     *path_length,
9241                        gchar    **path,
9242                        gchar    **path_reversed)
9243 {
9244   static gchar *rev_path = NULL;
9245   static guint tmp_path_len = 0;
9246   guint len;
9247   
9248   g_return_if_fail (GTK_IS_WIDGET (widget));
9249   
9250   len = 0;
9251   do
9252     {
9253       const gchar *string;
9254       const gchar *s;
9255       gchar *d;
9256       guint l;
9257       
9258       string = g_type_name (GTK_WIDGET_TYPE (widget));
9259       l = strlen (string);
9260       while (tmp_path_len <= len + l + 1)
9261         {
9262           tmp_path_len += INIT_PATH_SIZE;
9263           rev_path = g_realloc (rev_path, tmp_path_len);
9264         }
9265       s = string + l - 1;
9266       d = rev_path + len;
9267       while (s >= string)
9268         *(d++) = *(s--);
9269       len += l;
9270       
9271       widget = widget->parent;
9272       
9273       if (widget)
9274         rev_path[len++] = '.';
9275       else
9276         rev_path[len++] = 0;
9277     }
9278   while (widget);
9279   
9280   if (path_length)
9281     *path_length = len - 1;
9282   if (path_reversed)
9283     *path_reversed = g_strdup (rev_path);
9284   if (path)
9285     {
9286       *path = g_strdup (rev_path);
9287       g_strreverse (*path);
9288     }
9289 }
9290
9291 /**
9292  * gtk_requisition_copy:
9293  * @requisition: a #GtkRequisition
9294  *
9295  * Copies a #GtkRequisition.
9296  *
9297  * Returns: a copy of @requisition
9298  **/
9299 GtkRequisition *
9300 gtk_requisition_copy (const GtkRequisition *requisition)
9301 {
9302   return (GtkRequisition *)g_memdup (requisition, sizeof (GtkRequisition));
9303 }
9304
9305 /**
9306  * gtk_requisition_free:
9307  * @requisition: a #GtkRequisition
9308  * 
9309  * Frees a #GtkRequisition.
9310  **/
9311 void
9312 gtk_requisition_free (GtkRequisition *requisition)
9313 {
9314   g_free (requisition);
9315 }
9316
9317 GType
9318 gtk_requisition_get_type (void)
9319 {
9320   static GType our_type = 0;
9321   
9322   if (our_type == 0)
9323     our_type = g_boxed_type_register_static (I_("GtkRequisition"),
9324                                              (GBoxedCopyFunc) gtk_requisition_copy,
9325                                              (GBoxedFreeFunc) gtk_requisition_free);
9326
9327   return our_type;
9328 }
9329
9330 /**
9331  * gtk_widget_get_accessible:
9332  * @widget: a #GtkWidget
9333  *
9334  * Returns the accessible object that describes the widget to an
9335  * assistive technology. 
9336  * 
9337  * If no accessibility library is loaded (i.e. no ATK implementation library is 
9338  * loaded via <envar>GTK_MODULES</envar> or via another application library, 
9339  * such as libgnome), then this #AtkObject instance may be a no-op. Likewise, 
9340  * if no class-specific #AtkObject implementation is available for the widget 
9341  * instance in question, it will inherit an #AtkObject implementation from the 
9342  * first ancestor class for which such an implementation is defined.
9343  *
9344  * The documentation of the <ulink url="http://developer.gnome.org/doc/API/2.0/atk/index.html">ATK</ulink>
9345  * library contains more information about accessible objects and their uses.
9346  * 
9347  * Returns: the #AtkObject associated with @widget
9348  */
9349 AtkObject* 
9350 gtk_widget_get_accessible (GtkWidget *widget)
9351 {
9352   GtkWidgetClass *klass;
9353
9354   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9355
9356   klass = GTK_WIDGET_GET_CLASS (widget);
9357
9358   g_return_val_if_fail (klass->get_accessible != NULL, NULL);
9359
9360   return klass->get_accessible (widget);
9361 }
9362
9363 static AtkObject* 
9364 gtk_widget_real_get_accessible (GtkWidget *widget)
9365 {
9366   AtkObject* accessible;
9367
9368   accessible = g_object_get_qdata (G_OBJECT (widget), 
9369                                    quark_accessible_object);
9370   if (!accessible)
9371   {
9372     AtkObjectFactory *factory;
9373     AtkRegistry *default_registry;
9374
9375     default_registry = atk_get_default_registry ();
9376     factory = atk_registry_get_factory (default_registry, 
9377                                         G_TYPE_FROM_INSTANCE (widget));
9378     accessible =
9379       atk_object_factory_create_accessible (factory,
9380                                             G_OBJECT (widget));
9381     g_object_set_qdata (G_OBJECT (widget), 
9382                         quark_accessible_object,
9383                         accessible);
9384   }
9385   return accessible;
9386 }
9387
9388 /*
9389  * Initialize a AtkImplementorIface instance's virtual pointers as
9390  * appropriate to this implementor's class (GtkWidget).
9391  */
9392 static void
9393 gtk_widget_accessible_interface_init (AtkImplementorIface *iface)
9394 {
9395   iface->ref_accessible = gtk_widget_ref_accessible;
9396 }
9397
9398 static AtkObject*
9399 gtk_widget_ref_accessible (AtkImplementor *implementor)
9400 {
9401   AtkObject *accessible;
9402
9403   accessible = gtk_widget_get_accessible (GTK_WIDGET (implementor));
9404   if (accessible)
9405     g_object_ref (accessible);
9406   return accessible;
9407 }
9408
9409 /*
9410  * GtkBuildable implementation
9411  */
9412 static GQuark            quark_builder_has_default = 0;
9413 static GQuark            quark_builder_has_focus = 0;
9414 static GQuark            quark_builder_atk_relations = 0;
9415
9416 static void
9417 gtk_widget_buildable_interface_init (GtkBuildableIface *iface)
9418 {
9419   quark_builder_has_default = g_quark_from_static_string ("gtk-builder-has-default");
9420   quark_builder_has_focus = g_quark_from_static_string ("gtk-builder-has-focus");
9421   quark_builder_atk_relations = g_quark_from_static_string ("gtk-builder-atk-relations");
9422
9423   iface->set_name = gtk_widget_buildable_set_name;
9424   iface->get_name = gtk_widget_buildable_get_name;
9425   iface->get_internal_child = gtk_widget_buildable_get_internal_child;
9426   iface->set_buildable_property = gtk_widget_buildable_set_buildable_property;
9427   iface->parser_finished = gtk_widget_buildable_parser_finished;
9428   iface->custom_tag_start = gtk_widget_buildable_custom_tag_start;
9429   iface->custom_finished = gtk_widget_buildable_custom_finished;
9430 }
9431
9432 static void
9433 gtk_widget_buildable_set_name (GtkBuildable *buildable,
9434                                const gchar  *name)
9435 {
9436   gtk_widget_set_name (GTK_WIDGET (buildable), name);
9437 }
9438
9439 static const gchar *
9440 gtk_widget_buildable_get_name (GtkBuildable *buildable)
9441 {
9442   return gtk_widget_get_name (GTK_WIDGET (buildable));
9443 }
9444
9445 static GObject *
9446 gtk_widget_buildable_get_internal_child (GtkBuildable *buildable,
9447                                          GtkBuilder   *builder,
9448                                          const gchar  *childname)
9449 {
9450   if (strcmp (childname, "accessible") == 0)
9451     return G_OBJECT (gtk_widget_get_accessible (GTK_WIDGET (buildable)));
9452
9453   return NULL;
9454 }
9455
9456 static void
9457 gtk_widget_buildable_set_buildable_property (GtkBuildable *buildable,
9458                                              GtkBuilder   *builder,
9459                                              const gchar  *name,
9460                                              const GValue *value)
9461 {
9462   if (strcmp (name, "has-default") == 0 && g_value_get_boolean (value))
9463       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_default,
9464                           GINT_TO_POINTER (TRUE));
9465   else if (strcmp (name, "has-focus") == 0 && g_value_get_boolean (value))
9466       g_object_set_qdata (G_OBJECT (buildable), quark_builder_has_focus,
9467                           GINT_TO_POINTER (TRUE));
9468   else
9469     g_object_set_property (G_OBJECT (buildable), name, value);
9470 }
9471
9472 typedef struct
9473 {
9474   gchar *action_name;
9475   GString *description;
9476   gchar *context;
9477   gboolean translatable;
9478 } AtkActionData;
9479
9480 typedef struct
9481 {
9482   gchar *target;
9483   gchar *type;
9484 } AtkRelationData;
9485
9486 static void
9487 free_action (AtkActionData *data, gpointer user_data)
9488 {
9489   g_free (data->action_name);
9490   g_string_free (data->description, TRUE);
9491   g_free (data->context);
9492   g_slice_free (AtkActionData, data);
9493 }
9494
9495 static void
9496 free_relation (AtkRelationData *data, gpointer user_data)
9497 {
9498   g_free (data->target);
9499   g_free (data->type);
9500   g_slice_free (AtkRelationData, data);
9501 }
9502
9503 static void
9504 gtk_widget_buildable_parser_finished (GtkBuildable *buildable,
9505                                       GtkBuilder   *builder)
9506 {
9507   GSList *atk_relations;
9508
9509   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_default))
9510     gtk_widget_grab_default (GTK_WIDGET (buildable));
9511   if (g_object_get_qdata (G_OBJECT (buildable), quark_builder_has_focus))
9512     gtk_widget_grab_focus (GTK_WIDGET (buildable));
9513
9514   atk_relations = g_object_get_qdata (G_OBJECT (buildable),
9515                                       quark_builder_atk_relations);
9516   if (atk_relations)
9517     {
9518       AtkObject *accessible;
9519       AtkRelationSet *relation_set;
9520       GSList *l;
9521       GObject *target;
9522       AtkRelationType relation_type;
9523       AtkObject *target_accessible;
9524
9525       accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
9526       relation_set = atk_object_ref_relation_set (accessible);
9527
9528       for (l = atk_relations; l; l = l->next)
9529         {
9530           AtkRelationData *relation = (AtkRelationData*)l->data;
9531
9532           target = gtk_builder_get_object (builder, relation->target);
9533           if (!target)
9534             {
9535               g_warning ("Target object %s in <relation> does not exist",
9536                          relation->target);
9537               continue;
9538             }
9539           target_accessible = gtk_widget_get_accessible (GTK_WIDGET (target));
9540           g_assert (target_accessible != NULL);
9541
9542           relation_type = atk_relation_type_for_name (relation->type);
9543           if (relation_type == ATK_RELATION_NULL)
9544             {
9545               g_warning ("<relation> type %s not found",
9546                          relation->type);
9547               continue;
9548             }
9549           atk_relation_set_add_relation_by_type (relation_set, relation_type,
9550                                                  target_accessible);
9551         }
9552       g_object_unref (relation_set);
9553
9554       g_slist_foreach (atk_relations, (GFunc)free_relation, NULL);
9555       g_slist_free (atk_relations);
9556       g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
9557                           NULL);
9558     }
9559 }
9560
9561 typedef struct
9562 {
9563   GSList *actions;
9564   GSList *relations;
9565 } AccessibilitySubParserData;
9566
9567 static void
9568 accessibility_start_element (GMarkupParseContext  *context,
9569                              const gchar          *element_name,
9570                              const gchar         **names,
9571                              const gchar         **values,
9572                              gpointer              user_data,
9573                              GError              **error)
9574 {
9575   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
9576   guint i;
9577   gint line_number, char_number;
9578
9579   if (strcmp (element_name, "relation") == 0)
9580     {
9581       gchar *target = NULL;
9582       gchar *type = NULL;
9583       AtkRelationData *relation;
9584
9585       for (i = 0; names[i]; i++)
9586         {
9587           if (strcmp (names[i], "target") == 0)
9588             target = g_strdup (values[i]);
9589           else if (strcmp (names[i], "type") == 0)
9590             type = g_strdup (values[i]);
9591           else
9592             {
9593               g_markup_parse_context_get_position (context,
9594                                                    &line_number,
9595                                                    &char_number);
9596               g_set_error (error,
9597                            GTK_BUILDER_ERROR,
9598                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
9599                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
9600                            "<input>",
9601                            line_number, char_number, names[i], "relation");
9602               g_free (target);
9603               g_free (type);
9604               return;
9605             }
9606         }
9607
9608       if (!target || !type)
9609         {
9610           g_markup_parse_context_get_position (context,
9611                                                &line_number,
9612                                                &char_number);
9613           g_set_error (error,
9614                        GTK_BUILDER_ERROR,
9615                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
9616                        "%s:%d:%d <%s> requires attribute \"%s\"",
9617                        "<input>",
9618                        line_number, char_number, "relation",
9619                        type ? "target" : "type");
9620           g_free (target);
9621           g_free (type);
9622           return;
9623         }
9624
9625       relation = g_slice_new (AtkRelationData);
9626       relation->target = target;
9627       relation->type = type;
9628
9629       data->relations = g_slist_prepend (data->relations, relation);
9630     }
9631   else if (strcmp (element_name, "action") == 0)
9632     {
9633       const gchar *action_name = NULL;
9634       const gchar *description = NULL;
9635       const gchar *msg_context = NULL;
9636       gboolean translatable = FALSE;
9637       AtkActionData *action;
9638
9639       for (i = 0; names[i]; i++)
9640         {
9641           if (strcmp (names[i], "action_name") == 0)
9642             action_name = values[i];
9643           else if (strcmp (names[i], "description") == 0)
9644             description = values[i];
9645           else if (strcmp (names[i], "translatable") == 0)
9646             {
9647               if (!_gtk_builder_boolean_from_string (values[i], &translatable, error))
9648                 return;
9649             }
9650           else if (strcmp (names[i], "comments") == 0)
9651             {
9652               /* do nothing, comments are for translators */
9653             }
9654           else if (strcmp (names[i], "context") == 0)
9655             msg_context = values[i];
9656           else
9657             {
9658               g_markup_parse_context_get_position (context,
9659                                                    &line_number,
9660                                                    &char_number);
9661               g_set_error (error,
9662                            GTK_BUILDER_ERROR,
9663                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
9664                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
9665                            "<input>",
9666                            line_number, char_number, names[i], "action");
9667               return;
9668             }
9669         }
9670
9671       if (!action_name)
9672         {
9673           g_markup_parse_context_get_position (context,
9674                                                &line_number,
9675                                                &char_number);
9676           g_set_error (error,
9677                        GTK_BUILDER_ERROR,
9678                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
9679                        "%s:%d:%d <%s> requires attribute \"%s\"",
9680                        "<input>",
9681                        line_number, char_number, "action",
9682                        "action_name");
9683           return;
9684         }
9685
9686       action = g_slice_new (AtkActionData);
9687       action->action_name = g_strdup (action_name);
9688       action->description = g_string_new (description);
9689       action->context = g_strdup (msg_context);
9690       action->translatable = translatable;
9691
9692       data->actions = g_slist_prepend (data->actions, action);
9693     }
9694   else if (strcmp (element_name, "accessibility") == 0)
9695     ;
9696   else
9697     g_warning ("Unsupported tag for GtkWidget: %s\n", element_name);
9698 }
9699
9700 static void
9701 accessibility_text (GMarkupParseContext  *context,
9702                     const gchar          *text,
9703                     gsize                 text_len,
9704                     gpointer              user_data,
9705                     GError              **error)
9706 {
9707   AccessibilitySubParserData *data = (AccessibilitySubParserData*)user_data;
9708
9709   if (strcmp (g_markup_parse_context_get_element (context), "action") == 0)
9710     {
9711       AtkActionData *action = data->actions->data;
9712
9713       g_string_append_len (action->description, text, text_len);
9714     }
9715 }
9716
9717 static const GMarkupParser accessibility_parser =
9718   {
9719     accessibility_start_element,
9720     NULL,
9721     accessibility_text,
9722   };
9723
9724 typedef struct
9725 {
9726   GObject *object;
9727   guint    key;
9728   guint    modifiers;
9729   gchar   *signal;
9730 } AccelGroupParserData;
9731
9732 static void
9733 accel_group_start_element (GMarkupParseContext  *context,
9734                            const gchar          *element_name,
9735                            const gchar         **names,
9736                            const gchar         **values,
9737                            gpointer              user_data,
9738                            GError              **error)
9739 {
9740   gint i;
9741   guint key = 0;
9742   guint modifiers = 0;
9743   gchar *signal = NULL;
9744   AccelGroupParserData *parser_data = (AccelGroupParserData*)user_data;
9745
9746   for (i = 0; names[i]; i++)
9747     {
9748       if (strcmp (names[i], "key") == 0)
9749         key = gdk_keyval_from_name (values[i]);
9750       else if (strcmp (names[i], "modifiers") == 0)
9751         {
9752           if (!_gtk_builder_flags_from_string (GDK_TYPE_MODIFIER_TYPE,
9753                                                values[i],
9754                                                &modifiers,
9755                                                error))
9756               return;
9757         }
9758       else if (strcmp (names[i], "signal") == 0)
9759         signal = g_strdup (values[i]);
9760     }
9761
9762   if (key == 0 || signal == NULL)
9763     {
9764       g_warning ("<accelerator> requires key and signal attributes");
9765       return;
9766     }
9767   parser_data->key = key;
9768   parser_data->modifiers = modifiers;
9769   parser_data->signal = signal;
9770 }
9771
9772 static const GMarkupParser accel_group_parser =
9773   {
9774     accel_group_start_element,
9775   };
9776
9777 static gboolean
9778 gtk_widget_buildable_custom_tag_start (GtkBuildable     *buildable,
9779                                        GtkBuilder       *builder,
9780                                        GObject          *child,
9781                                        const gchar      *tagname,
9782                                        GMarkupParser    *parser,
9783                                        gpointer         *data)
9784 {
9785   g_assert (buildable);
9786
9787   if (strcmp (tagname, "accelerator") == 0)
9788     {
9789       AccelGroupParserData *parser_data;
9790
9791       parser_data = g_slice_new0 (AccelGroupParserData);
9792       parser_data->object = g_object_ref (buildable);
9793       *parser = accel_group_parser;
9794       *data = parser_data;
9795       return TRUE;
9796     }
9797   if (strcmp (tagname, "accessibility") == 0)
9798     {
9799       AccessibilitySubParserData *parser_data;
9800
9801       parser_data = g_slice_new0 (AccessibilitySubParserData);
9802       *parser = accessibility_parser;
9803       *data = parser_data;
9804       return TRUE;
9805     }
9806   return FALSE;
9807 }
9808
9809 void
9810 _gtk_widget_buildable_finish_accelerator (GtkWidget *widget,
9811                                           GtkWidget *toplevel,
9812                                           gpointer   user_data)
9813 {
9814   AccelGroupParserData *accel_data;
9815   GSList *accel_groups;
9816   GtkAccelGroup *accel_group;
9817
9818   g_return_if_fail (GTK_IS_WIDGET (widget));
9819   g_return_if_fail (GTK_IS_WIDGET (toplevel));
9820   g_return_if_fail (user_data != NULL);
9821
9822   accel_data = (AccelGroupParserData*)user_data;
9823   accel_groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
9824   if (g_slist_length (accel_groups) == 0)
9825     {
9826       accel_group = gtk_accel_group_new ();
9827       gtk_window_add_accel_group (GTK_WINDOW (toplevel), accel_group);
9828     }
9829   else
9830     {
9831       g_assert (g_slist_length (accel_groups) == 1);
9832       accel_group = g_slist_nth_data (accel_groups, 0);
9833     }
9834
9835   gtk_widget_add_accelerator (GTK_WIDGET (accel_data->object),
9836                               accel_data->signal,
9837                               accel_group,
9838                               accel_data->key,
9839                               accel_data->modifiers,
9840                               GTK_ACCEL_VISIBLE);
9841
9842   g_object_unref (accel_data->object);
9843   g_free (accel_data->signal);
9844   g_slice_free (AccelGroupParserData, accel_data);
9845 }
9846
9847 static void
9848 gtk_widget_buildable_custom_finished (GtkBuildable *buildable,
9849                                       GtkBuilder   *builder,
9850                                       GObject      *child,
9851                                       const gchar  *tagname,
9852                                       gpointer      user_data)
9853 {
9854   AccelGroupParserData *accel_data;
9855   AccessibilitySubParserData *a11y_data;
9856   GtkWidget *toplevel;
9857
9858   if (strcmp (tagname, "accelerator") == 0)
9859     {
9860       accel_data = (AccelGroupParserData*)user_data;
9861       g_assert (accel_data->object);
9862
9863       toplevel = gtk_widget_get_toplevel (GTK_WIDGET (accel_data->object));
9864
9865       _gtk_widget_buildable_finish_accelerator (GTK_WIDGET (buildable), toplevel, user_data);
9866     }
9867   else if (strcmp (tagname, "accessibility") == 0)
9868     {
9869       a11y_data = (AccessibilitySubParserData*)user_data;
9870
9871       if (a11y_data->actions)
9872         {
9873           AtkObject *accessible;
9874           AtkAction *action;
9875           gint i, n_actions;
9876           GSList *l;
9877
9878           accessible = gtk_widget_get_accessible (GTK_WIDGET (buildable));
9879
9880           action = ATK_ACTION (accessible);
9881           n_actions = atk_action_get_n_actions (action);
9882
9883           for (l = a11y_data->actions; l; l = l->next)
9884             {
9885               AtkActionData *action_data = (AtkActionData*)l->data;
9886
9887               for (i = 0; i < n_actions; i++)
9888                 if (strcmp (atk_action_get_name (action, i),
9889                             action_data->action_name) == 0)
9890                   break;
9891
9892               if (i < n_actions)
9893                 {
9894                   gchar *description;
9895
9896                   if (action_data->translatable && action_data->description->len)
9897                     description = _gtk_builder_parser_translate (gtk_builder_get_translation_domain (builder),
9898                                                                  action_data->context,
9899                                                                  action_data->description->str);
9900                   else
9901                     description = action_data->description->str;
9902
9903                   atk_action_set_description (action, i, description);
9904                 }
9905             }
9906
9907           g_slist_foreach (a11y_data->actions, (GFunc)free_action, NULL);
9908           g_slist_free (a11y_data->actions);
9909         }
9910
9911       if (a11y_data->relations)
9912         g_object_set_qdata (G_OBJECT (buildable), quark_builder_atk_relations,
9913                             a11y_data->relations);
9914
9915       g_slice_free (AccessibilitySubParserData, a11y_data);
9916     }
9917 }
9918
9919
9920 /**
9921  * gtk_widget_get_clipboard:
9922  * @widget: a #GtkWidget
9923  * @selection: a #GdkAtom which identifies the clipboard
9924  *             to use. %GDK_SELECTION_CLIPBOARD gives the
9925  *             default clipboard. Another common value
9926  *             is %GDK_SELECTION_PRIMARY, which gives
9927  *             the primary X selection. 
9928  * 
9929  * Returns the clipboard object for the given selection to
9930  * be used with @widget. @widget must have a #GdkDisplay
9931  * associated with it, so must be attached to a toplevel
9932  * window.
9933  * 
9934  * Return value: the appropriate clipboard object. If no
9935  *             clipboard already exists, a new one will
9936  *             be created. Once a clipboard object has
9937  *             been created, it is persistent for all time.
9938  *
9939  * Since: 2.2
9940  **/
9941 GtkClipboard *
9942 gtk_widget_get_clipboard (GtkWidget *widget, GdkAtom selection)
9943 {
9944   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9945   g_return_val_if_fail (gtk_widget_has_screen (widget), NULL);
9946   
9947   return gtk_clipboard_get_for_display (gtk_widget_get_display (widget),
9948                                         selection);
9949 }
9950
9951 /**
9952  * gtk_widget_list_mnemonic_labels:
9953  * @widget: a #GtkWidget
9954  * 
9955  * Returns a newly allocated list of the widgets, normally labels, for 
9956  * which this widget is a the target of a mnemonic (see for example, 
9957  * gtk_label_set_mnemonic_widget()).
9958
9959  * The widgets in the list are not individually referenced. If you
9960  * want to iterate through the list and perform actions involving
9961  * callbacks that might destroy the widgets, you
9962  * <emphasis>must</emphasis> call <literal>g_list_foreach (result,
9963  * (GFunc)g_object_ref, NULL)</literal> first, and then unref all the
9964  * widgets afterwards.
9965
9966  * Return value: the list of mnemonic labels; free this list
9967  *  with g_list_free() when you are done with it.
9968  *
9969  * Since: 2.4
9970  **/
9971 GList *
9972 gtk_widget_list_mnemonic_labels (GtkWidget *widget)
9973 {
9974   GList *list = NULL;
9975   GSList *l;
9976   
9977   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
9978
9979   for (l = g_object_get_qdata (G_OBJECT (widget), quark_mnemonic_labels); l; l = l->next)
9980     list = g_list_prepend (list, l->data);
9981
9982   return list;
9983 }
9984
9985 /**
9986  * gtk_widget_add_mnemonic_label:
9987  * @widget: a #GtkWidget
9988  * @label: a #GtkWidget that acts as a mnemonic label for @widget
9989  * 
9990  * Adds a widget to the list of mnemonic labels for
9991  * this widget. (See gtk_widget_list_mnemonic_labels()). Note the
9992  * list of mnemonic labels for the widget is cleared when the
9993  * widget is destroyed, so the caller must make sure to update
9994  * its internal state at this point as well, by using a connection
9995  * to the #GtkWidget::destroy signal or a weak notifier.
9996  *
9997  * Since: 2.4
9998  **/
9999 void
10000 gtk_widget_add_mnemonic_label (GtkWidget *widget,
10001                                GtkWidget *label)
10002 {
10003   GSList *old_list, *new_list;
10004
10005   g_return_if_fail (GTK_IS_WIDGET (widget));
10006   g_return_if_fail (GTK_IS_WIDGET (label));
10007
10008   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
10009   new_list = g_slist_prepend (old_list, label);
10010   
10011   g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
10012                            new_list, (GDestroyNotify) g_slist_free);
10013 }
10014
10015 /**
10016  * gtk_widget_remove_mnemonic_label:
10017  * @widget: a #GtkWidget
10018  * @label: a #GtkWidget that was previously set as a mnemnic label for
10019  *         @widget with gtk_widget_add_mnemonic_label().
10020  * 
10021  * Removes a widget from the list of mnemonic labels for
10022  * this widget. (See gtk_widget_list_mnemonic_labels()). The widget
10023  * must have previously been added to the list with
10024  * gtk_widget_add_mnemonic_label().
10025  *
10026  * Since: 2.4
10027  **/
10028 void
10029 gtk_widget_remove_mnemonic_label (GtkWidget *widget,
10030                                   GtkWidget *label)
10031 {
10032   GSList *old_list, *new_list;
10033
10034   g_return_if_fail (GTK_IS_WIDGET (widget));
10035   g_return_if_fail (GTK_IS_WIDGET (label));
10036
10037   old_list = g_object_steal_qdata (G_OBJECT (widget), quark_mnemonic_labels);
10038   new_list = g_slist_remove (old_list, label);
10039
10040   if (new_list)
10041     g_object_set_qdata_full (G_OBJECT (widget), quark_mnemonic_labels,
10042                              new_list, (GDestroyNotify) g_slist_free);
10043 }
10044
10045 /**
10046  * gtk_widget_get_no_show_all:
10047  * @widget: a #GtkWidget
10048  * 
10049  * Returns the current value of the GtkWidget:no-show-all property, 
10050  * which determines whether calls to gtk_widget_show_all() and 
10051  * gtk_widget_hide_all() will affect this widget. 
10052  * 
10053  * Return value: the current value of the "no-show-all" property.
10054  *
10055  * Since: 2.4
10056  **/
10057 gboolean
10058 gtk_widget_get_no_show_all (GtkWidget *widget)
10059 {
10060   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
10061   
10062   return (GTK_WIDGET_FLAGS (widget) & GTK_NO_SHOW_ALL) != 0;
10063 }
10064
10065 /**
10066  * gtk_widget_set_no_show_all:
10067  * @widget: a #GtkWidget
10068  * @no_show_all: the new value for the "no-show-all" property
10069  * 
10070  * Sets the #GtkWidget:no-show-all property, which determines whether 
10071  * calls to gtk_widget_show_all() and gtk_widget_hide_all() will affect 
10072  * this widget. 
10073  *
10074  * This is mostly for use in constructing widget hierarchies with externally
10075  * controlled visibility, see #GtkUIManager.
10076  * 
10077  * Since: 2.4
10078  **/
10079 void
10080 gtk_widget_set_no_show_all (GtkWidget *widget,
10081                             gboolean   no_show_all)
10082 {
10083   g_return_if_fail (GTK_IS_WIDGET (widget));
10084
10085   no_show_all = (no_show_all != FALSE);
10086
10087   if (no_show_all == ((GTK_WIDGET_FLAGS (widget) & GTK_NO_SHOW_ALL) != 0))
10088     return;
10089
10090   if (no_show_all)
10091     GTK_WIDGET_SET_FLAGS (widget, GTK_NO_SHOW_ALL);
10092   else
10093     GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_SHOW_ALL);
10094   
10095   g_object_notify (G_OBJECT (widget), "no-show-all");
10096 }
10097
10098
10099 static void
10100 gtk_widget_real_set_has_tooltip (GtkWidget *widget,
10101                                  gboolean   has_tooltip,
10102                                  gboolean   force)
10103 {
10104   gboolean priv_has_tooltip;
10105
10106   priv_has_tooltip = GPOINTER_TO_UINT (g_object_get_qdata (G_OBJECT (widget),
10107                                        quark_has_tooltip));
10108
10109   if (priv_has_tooltip != has_tooltip || force)
10110     {
10111       priv_has_tooltip = has_tooltip;
10112
10113       if (priv_has_tooltip)
10114         {
10115           if (GTK_WIDGET_REALIZED (widget) && GTK_WIDGET_NO_WINDOW (widget))
10116             gdk_window_set_events (widget->window,
10117                                    gdk_window_get_events (widget->window) |
10118                                    GDK_LEAVE_NOTIFY_MASK |
10119                                    GDK_POINTER_MOTION_MASK |
10120                                    GDK_POINTER_MOTION_HINT_MASK);
10121
10122           if (!GTK_WIDGET_NO_WINDOW (widget))
10123               gtk_widget_add_events (widget,
10124                                      GDK_LEAVE_NOTIFY_MASK |
10125                                      GDK_POINTER_MOTION_MASK |
10126                                      GDK_POINTER_MOTION_HINT_MASK);
10127         }
10128
10129       g_object_set_qdata (G_OBJECT (widget), quark_has_tooltip,
10130                           GUINT_TO_POINTER (priv_has_tooltip));
10131     }
10132 }
10133
10134 /**
10135  * gtk_widget_set_tooltip_window:
10136  * @widget: a #GtkWidget
10137  * @custom_window: a #GtkWindow, or %NULL
10138  *
10139  * Replaces the default, usually yellow, window used for displaying
10140  * tooltips with @custom_window. GTK+ will take care of showing and
10141  * hiding @custom_window at the right moment, to behave likewise as
10142  * the default tooltip window. If @custom_window is %NULL, the default
10143  * tooltip window will be used.
10144  *
10145  * Since: 2.12
10146  */
10147 void
10148 gtk_widget_set_tooltip_window (GtkWidget *widget,
10149                                GtkWindow *custom_window)
10150 {
10151   gboolean has_tooltip;
10152   gchar *tooltip_markup;
10153
10154   g_return_if_fail (GTK_IS_WIDGET (widget));
10155   g_return_if_fail (custom_window == NULL || GTK_IS_WINDOW (custom_window));
10156
10157   tooltip_markup = g_object_get_qdata (G_OBJECT (widget), quark_tooltip_markup);
10158
10159   if (custom_window)
10160     g_object_ref (custom_window);
10161
10162   g_object_set_qdata_full (G_OBJECT (widget), quark_tooltip_window,
10163                            custom_window, g_object_unref);
10164
10165   has_tooltip = (custom_window != NULL || tooltip_markup != NULL);
10166   gtk_widget_real_set_has_tooltip (widget, has_tooltip, FALSE);
10167
10168   if (has_tooltip && GTK_WIDGET_VISIBLE (widget))
10169     gtk_widget_queue_tooltip_query (widget);
10170 }
10171
10172 /**
10173  * gtk_widget_get_tooltip_window:
10174  * @widget: a #GtkWidget
10175  *
10176  * Returns the #GtkWindow of the current tooltip. This can be the
10177  * GtkWindow created by default, or the custom tooltip window set
10178  * using gtk_widget_set_tooltip_window().
10179  *
10180  * Return value: The #GtkWindow of the current tooltip.
10181  *
10182  * Since: 2.12
10183  */
10184 GtkWindow *
10185 gtk_widget_get_tooltip_window (GtkWidget *widget)
10186 {
10187   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10188
10189   return g_object_get_qdata (G_OBJECT (widget), quark_tooltip_window);
10190 }
10191
10192 /**
10193  * gtk_widget_trigger_tooltip_query:
10194  * @widget: a #GtkWidget
10195  *
10196  * Triggers a tooltip query on the display where the toplevel of @widget
10197  * is located. See gtk_tooltip_trigger_tooltip_query() for more
10198  * information.
10199  *
10200  * Since: 2.12
10201  */
10202 void
10203 gtk_widget_trigger_tooltip_query (GtkWidget *widget)
10204 {
10205   gtk_tooltip_trigger_tooltip_query (gtk_widget_get_display (widget));
10206 }
10207
10208 static guint tooltip_query_id;
10209 static GSList *tooltip_query_displays;
10210
10211 static gboolean
10212 tooltip_query_idle (gpointer data)
10213 {
10214   g_slist_foreach (tooltip_query_displays, (GFunc)gtk_tooltip_trigger_tooltip_query, NULL);
10215   g_slist_foreach (tooltip_query_displays, (GFunc)g_object_unref, NULL);
10216   g_slist_free (tooltip_query_displays);
10217
10218   tooltip_query_displays = NULL;
10219   tooltip_query_id = 0;
10220
10221   return FALSE;
10222 }
10223
10224 static void
10225 gtk_widget_queue_tooltip_query (GtkWidget *widget)
10226 {
10227   GdkDisplay *display;
10228
10229   display = gtk_widget_get_display (widget);
10230
10231   if (!g_slist_find (tooltip_query_displays, display))
10232     tooltip_query_displays = g_slist_prepend (tooltip_query_displays, g_object_ref (display));
10233
10234   if (tooltip_query_id == 0)
10235     tooltip_query_id = gdk_threads_add_idle (tooltip_query_idle, NULL);
10236 }
10237
10238 /**
10239  * gtk_widget_set_tooltip_text:
10240  * @widget: a #GtkWidget
10241  * @text: the contents of the tooltip for @widget
10242  *
10243  * Sets @text as the contents of the tooltip. This function will take
10244  * care of setting GtkWidget:has-tooltip to %TRUE and of the default
10245  * handler for the GtkWidget::query-tooltip signal.
10246  *
10247  * See also the GtkWidget:tooltip-text property and gtk_tooltip_set_text().
10248  *
10249  * Since: 2.12
10250  */
10251 void
10252 gtk_widget_set_tooltip_text (GtkWidget   *widget,
10253                              const gchar *text)
10254 {
10255   g_return_if_fail (GTK_IS_WIDGET (widget));
10256
10257   g_object_set (G_OBJECT (widget), "tooltip-text", text, NULL);
10258 }
10259
10260 /**
10261  * gtk_widget_get_tooltip_text:
10262  * @widget: a #GtkWidget
10263  *
10264  * Gets the contents of the tooltip for @widget.
10265  *
10266  * Return value: the tooltip text, or %NULL. You should free the
10267  *   returned string with g_free() when done.
10268  *
10269  * Since: 2.12
10270  */
10271 gchar *
10272 gtk_widget_get_tooltip_text (GtkWidget *widget)
10273 {
10274   gchar *text = NULL;
10275
10276   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10277
10278   g_object_get (G_OBJECT (widget), "tooltip-text", &text, NULL);
10279
10280   return text;
10281 }
10282
10283 /**
10284  * gtk_widget_set_tooltip_markup:
10285  * @widget: a #GtkWidget
10286  * @markup: the contents of the tooltip for @widget, or %NULL
10287  *
10288  * Sets @markup as the contents of the tooltip, which is marked up with
10289  *  the <link linkend="PangoMarkupFormat">Pango text markup language</link>.
10290  *
10291  * This function will take care of setting GtkWidget:has-tooltip to %TRUE
10292  * and of the default handler for the GtkWidget::query-tooltip signal.
10293  *
10294  * See also the GtkWidget:tooltip-markup property and
10295  * gtk_tooltip_set_markup().
10296  *
10297  * Since: 2.12
10298  */
10299 void
10300 gtk_widget_set_tooltip_markup (GtkWidget   *widget,
10301                                const gchar *markup)
10302 {
10303   g_return_if_fail (GTK_IS_WIDGET (widget));
10304
10305   g_object_set (G_OBJECT (widget), "tooltip-markup", markup, NULL);
10306 }
10307
10308 /**
10309  * gtk_widget_get_tooltip_markup:
10310  * @widget: a #GtkWidget
10311  *
10312  * Gets the contents of the tooltip for @widget.
10313  *
10314  * Return value: the tooltip text, or %NULL. You should free the
10315  *   returned string with g_free() when done.
10316  *
10317  * Since: 2.12
10318  */
10319 gchar *
10320 gtk_widget_get_tooltip_markup (GtkWidget *widget)
10321 {
10322   gchar *text = NULL;
10323
10324   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10325
10326   g_object_get (G_OBJECT (widget), "tooltip-markup", &text, NULL);
10327
10328   return text;
10329 }
10330
10331 /**
10332  * gtk_widget_set_has_tooltip:
10333  * @widget: a #GtkWidget
10334  * @has_tooltip: whether or not @widget has a tooltip.
10335  *
10336  * Sets the has-tooltip property on @widget to @has_tooltip.  See
10337  * GtkWidget:has-tooltip for more information.
10338  *
10339  * Since: 2.12
10340  */
10341 void
10342 gtk_widget_set_has_tooltip (GtkWidget *widget,
10343                             gboolean   has_tooltip)
10344 {
10345   g_return_if_fail (GTK_IS_WIDGET (widget));
10346
10347   g_object_set (G_OBJECT (widget), "has-tooltip", has_tooltip, NULL);
10348 }
10349
10350 /**
10351  * gtk_widget_get_has_tooltip:
10352  * @widget: a #GtkWidget
10353  *
10354  * Returns the current value of the has-tooltip property.  See
10355  * GtkWidget:has-tooltip for more information.
10356  *
10357  * Return value: current value of has-tooltip on @widget.
10358  *
10359  * Since: 2.12
10360  */
10361 gboolean
10362 gtk_widget_get_has_tooltip (GtkWidget *widget)
10363 {
10364   gboolean has_tooltip = FALSE;
10365
10366   g_return_val_if_fail (GTK_IS_WIDGET (widget), FALSE);
10367
10368   g_object_get (G_OBJECT (widget), "has-tooltip", &has_tooltip, NULL);
10369
10370   return has_tooltip;
10371 }
10372
10373 /**
10374  * gtk_widget_get_window:
10375  * @widget: a #GtkWidget
10376  *
10377  * Returns the widget's window if it is realized, %NULL otherwise
10378  *
10379  * Return value: @widget's window.
10380  *
10381  * Since: 2.14
10382  */
10383 GdkWindow*
10384 gtk_widget_get_window (GtkWidget *widget)
10385 {
10386   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
10387
10388   return widget->window;
10389 }
10390
10391 #define __GTK_WIDGET_C__
10392 #include "gtkaliasdef.c"