]> Pileus Git - ~andy/gtk/blob - gtk/gtkmenu.c
Replace homegrown "nbsp", "hash" and "percent" entities by standard ISO
[~andy/gtk] / gtk / gtkmenu.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 <string.h> /* memset */
28 #include "gdk/gdkkeysyms.h"
29 #include "gtkaccelmap.h"
30 #include "gtkbindings.h"
31 #include "gtklabel.h"
32 #include "gtkmain.h"
33 #include "gtkmenu.h"
34 #include "gtkmenuitem.h"
35 #include "gtksignal.h"
36 #include "gtkwindow.h"
37 #include "gtkhbox.h"
38 #include "gtkvscrollbar.h"
39 #include "gtksettings.h"
40 #include "gtkintl.h"
41
42
43 #define MENU_ITEM_CLASS(w)   GTK_MENU_ITEM_GET_CLASS (w)
44 #define MENU_NEEDS_RESIZE(m) GTK_MENU_SHELL (m)->menu_flag
45
46 #define SUBMENU_NAV_REGION_PADDING 2
47 #define SUBMENU_NAV_HYSTERESIS_TIMEOUT 333
48
49 #define MENU_SCROLL_STEP 10
50 #define MENU_SCROLL_ARROW_HEIGHT 16
51 #define MENU_SCROLL_FAST_ZONE 4
52 #define MENU_SCROLL_TIMEOUT1 150
53 #define MENU_SCROLL_TIMEOUT2 50
54
55 typedef struct _GtkMenuAttachData       GtkMenuAttachData;
56
57 struct _GtkMenuAttachData
58 {
59   GtkWidget *attach_widget;
60   GtkMenuDetachFunc detacher;
61 };
62
63 enum {
64   PROP_0,
65   PROP_TEAROFF_TITLE
66 };
67
68 static void     gtk_menu_class_init        (GtkMenuClass     *klass);
69 static void     gtk_menu_init              (GtkMenu          *menu);
70 static void     gtk_menu_set_property      (GObject      *object,
71                                             guint         prop_id,
72                                             const GValue *value,
73                                             GParamSpec   *pspec);
74 static void     gtk_menu_get_property      (GObject     *object,
75                                             guint        prop_id,
76                                             GValue      *value,
77                                             GParamSpec  *pspec);
78 static void     gtk_menu_destroy           (GtkObject        *object);
79 static void     gtk_menu_finalize          (GObject          *object);
80 static void     gtk_menu_realize           (GtkWidget        *widget);
81 static void     gtk_menu_unrealize         (GtkWidget        *widget);
82 static void     gtk_menu_size_request      (GtkWidget        *widget,
83                                             GtkRequisition   *requisition);
84 static void     gtk_menu_size_allocate     (GtkWidget        *widget,
85                                             GtkAllocation    *allocation);
86 static void     gtk_menu_paint             (GtkWidget        *widget,
87                                             GdkEventExpose   *expose);
88 static void     gtk_menu_show              (GtkWidget        *widget);
89 static gboolean gtk_menu_expose            (GtkWidget        *widget,
90                                             GdkEventExpose   *event);
91 static gboolean gtk_menu_key_press         (GtkWidget        *widget,
92                                             GdkEventKey      *event);
93 static gboolean gtk_menu_motion_notify     (GtkWidget        *widget,
94                                             GdkEventMotion   *event);
95 static gboolean gtk_menu_enter_notify      (GtkWidget        *widget,
96                                             GdkEventCrossing *event);
97 static gboolean gtk_menu_leave_notify      (GtkWidget        *widget,
98                                             GdkEventCrossing *event);
99 static void     gtk_menu_scroll_to         (GtkMenu          *menu,
100                                             gint              offset);
101 static void     gtk_menu_stop_scrolling    (GtkMenu          *menu);
102 static gboolean gtk_menu_scroll_timeout    (gpointer          data);
103 static void     gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
104                                               GtkWidget       *menu_item);
105 static void     gtk_menu_select_item       (GtkMenuShell     *menu_shell,
106                                             GtkWidget        *menu_item);
107 static void     gtk_menu_real_insert       (GtkMenuShell     *menu_shell,
108                                             GtkWidget        *child,
109                                             gint              position);
110 static void     gtk_menu_scrollbar_changed (GtkAdjustment    *adjustment,
111                                             GtkMenu          *menu);
112 static void     gtk_menu_handle_scrolling  (GtkMenu          *menu,
113                                             gboolean         enter);
114 static void     gtk_menu_set_tearoff_hints (GtkMenu          *menu,
115                                             gint             width);
116 static void     gtk_menu_style_set         (GtkWidget        *widget,
117                                             GtkStyle         *previous_style);
118
119
120 static void     gtk_menu_stop_navigating_submenu       (GtkMenu          *menu);
121 static gboolean gtk_menu_stop_navigating_submenu_cb    (gpointer          user_data);
122 static gboolean gtk_menu_navigating_submenu            (GtkMenu          *menu,
123                                                         gint              event_x,
124                                                         gint              event_y);
125 static void     gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
126                                                         GtkMenuItem      *menu_item,
127                                                         GdkEventCrossing *event);
128  
129 static void gtk_menu_deactivate     (GtkMenuShell      *menu_shell);
130 static void gtk_menu_show_all       (GtkWidget         *widget);
131 static void gtk_menu_hide_all       (GtkWidget         *widget);
132 static void gtk_menu_position       (GtkMenu           *menu);
133 static void gtk_menu_reparent       (GtkMenu           *menu, 
134                                      GtkWidget         *new_parent, 
135                                      gboolean           unrealize);
136 static void gtk_menu_remove         (GtkContainer      *menu,
137                                      GtkWidget         *widget);
138
139 static void gtk_menu_update_title   (GtkMenu           *menu);
140
141 static void       menu_grab_transfer_window_destroy (GtkMenu *menu);
142 static GdkWindow *menu_grab_transfer_window_get     (GtkMenu *menu);
143
144 static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
145                                            gboolean group_changed);
146
147 static GtkMenuShellClass *parent_class = NULL;
148 static const gchar       *attach_data_key = "gtk-menu-attach-data";
149
150 GtkType
151 gtk_menu_get_type (void)
152 {
153   static GtkType menu_type = 0;
154   
155   if (!menu_type)
156     {
157       static const GtkTypeInfo menu_info =
158       {
159         "GtkMenu",
160         sizeof (GtkMenu),
161         sizeof (GtkMenuClass),
162         (GtkClassInitFunc) gtk_menu_class_init,
163         (GtkObjectInitFunc) gtk_menu_init,
164         /* reserved_1 */ NULL,
165         /* reserved_2 */ NULL,
166         (GtkClassInitFunc) NULL,
167       };
168       
169       menu_type = gtk_type_unique (gtk_menu_shell_get_type (), &menu_info);
170     }
171   
172   return menu_type;
173 }
174
175 static void
176 gtk_menu_class_init (GtkMenuClass *class)
177 {
178   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
179   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
180   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
181   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
182   GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
183   GtkBindingSet *binding_set;
184   
185   parent_class = g_type_class_peek_parent (class);
186   
187   gobject_class->finalize = gtk_menu_finalize;
188   gobject_class->set_property = gtk_menu_set_property;
189   gobject_class->get_property = gtk_menu_get_property;
190
191   g_object_class_install_property (gobject_class,
192                                    PROP_TEAROFF_TITLE,
193                                    g_param_spec_string ("tearoff-title",
194                                                         _("Tearoff Title"),
195                                                         _("A title that may be displayed by the window manager when this menu is torn-off."),
196                                                         "",
197                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
198   object_class->destroy = gtk_menu_destroy;
199   
200   widget_class->realize = gtk_menu_realize;
201   widget_class->unrealize = gtk_menu_unrealize;
202   widget_class->size_request = gtk_menu_size_request;
203   widget_class->size_allocate = gtk_menu_size_allocate;
204   widget_class->show = gtk_menu_show;
205   widget_class->expose_event = gtk_menu_expose;
206   widget_class->key_press_event = gtk_menu_key_press;
207   widget_class->motion_notify_event = gtk_menu_motion_notify;
208   widget_class->show_all = gtk_menu_show_all;
209   widget_class->hide_all = gtk_menu_hide_all;
210   widget_class->enter_notify_event = gtk_menu_enter_notify;
211   widget_class->leave_notify_event = gtk_menu_leave_notify;
212   widget_class->style_set = gtk_menu_style_set;
213
214   container_class->remove = gtk_menu_remove;
215   
216   menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
217   menu_shell_class->deactivate = gtk_menu_deactivate;
218   menu_shell_class->select_item = gtk_menu_select_item;
219   menu_shell_class->insert = gtk_menu_real_insert;
220
221   binding_set = gtk_binding_set_by_class (class);
222   gtk_binding_entry_add_signal (binding_set,
223                                 GDK_Up, 0,
224                                 "move_current", 1,
225                                 GTK_TYPE_MENU_DIRECTION_TYPE,
226                                 GTK_MENU_DIR_PREV);
227   gtk_binding_entry_add_signal (binding_set,
228                                 GDK_KP_Up, 0,
229                                 "move_current", 1,
230                                 GTK_TYPE_MENU_DIRECTION_TYPE,
231                                 GTK_MENU_DIR_PREV);
232   gtk_binding_entry_add_signal (binding_set,
233                                 GDK_Down, 0,
234                                 "move_current", 1,
235                                 GTK_TYPE_MENU_DIRECTION_TYPE,
236                                 GTK_MENU_DIR_NEXT);
237   gtk_binding_entry_add_signal (binding_set,
238                                 GDK_KP_Down, 0,
239                                 "move_current", 1,
240                                 GTK_TYPE_MENU_DIRECTION_TYPE,
241                                 GTK_MENU_DIR_NEXT);
242   gtk_binding_entry_add_signal (binding_set,
243                                 GDK_Left, 0,
244                                 "move_current", 1,
245                                 GTK_TYPE_MENU_DIRECTION_TYPE,
246                                 GTK_MENU_DIR_PARENT);
247   gtk_binding_entry_add_signal (binding_set,
248                                 GDK_KP_Left, 0,
249                                 "move_current", 1,
250                                 GTK_TYPE_MENU_DIRECTION_TYPE,
251                                 GTK_MENU_DIR_PARENT);
252   gtk_binding_entry_add_signal (binding_set,
253                                 GDK_Right, 0,
254                                 "move_current", 1,
255                                 GTK_TYPE_MENU_DIRECTION_TYPE,
256                                 GTK_MENU_DIR_CHILD);
257   gtk_binding_entry_add_signal (binding_set,
258                                 GDK_KP_Right, 0,
259                                 "move_current", 1,
260                                 GTK_TYPE_MENU_DIRECTION_TYPE,
261                                 GTK_MENU_DIR_CHILD);
262
263   gtk_settings_install_property (g_param_spec_boolean ("gtk-can-change-accels",
264                                                        _("Can change accelerators"),
265                                                        _("Whether menu accelerators can be changed by pressing a key over the menu item."),
266                                                        FALSE,
267                                                        G_PARAM_READWRITE));
268 }
269
270
271 static void 
272 gtk_menu_set_property (GObject      *object,
273                        guint         prop_id,
274                        const GValue *value,
275                        GParamSpec   *pspec)
276 {
277   GtkMenu *menu;
278   
279   menu = GTK_MENU (object);
280   
281   switch (prop_id)
282     {
283     case PROP_TEAROFF_TITLE:
284       gtk_menu_set_title (menu, g_value_get_string (value));
285       break;      
286     default:
287       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
288       break;
289     }
290 }
291
292 static void 
293 gtk_menu_get_property (GObject     *object,
294                        guint        prop_id,
295                        GValue      *value,
296                        GParamSpec  *pspec)
297 {
298   GtkMenu *menu;
299   
300   menu = GTK_MENU (object);
301   
302   switch (prop_id)
303     {
304     case PROP_TEAROFF_TITLE:
305       g_value_set_string (value, gtk_menu_get_title (menu));
306       break;
307     default:
308       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
309       break;
310     }
311 }
312
313 static gboolean
314 gtk_menu_window_event (GtkWidget *window,
315                        GdkEvent  *event,
316                        GtkWidget *menu)
317 {
318   gboolean handled = FALSE;
319
320   gtk_widget_ref (window);
321   gtk_widget_ref (menu);
322
323   switch (event->type)
324     {
325     case GDK_KEY_PRESS:
326     case GDK_KEY_RELEASE:
327       handled = gtk_widget_event (menu, event);
328       break;
329     default:
330       break;
331     }
332
333   gtk_widget_unref (window);
334   gtk_widget_unref (menu);
335
336   return handled;
337 }
338
339 static void
340 gtk_menu_init (GtkMenu *menu)
341 {
342   menu->parent_menu_item = NULL;
343   menu->old_active_menu_item = NULL;
344   menu->accel_group = NULL;
345   menu->position_func = NULL;
346   menu->position_func_data = NULL;
347   menu->toggle_size = 0;
348
349   menu->toplevel = g_object_connect (gtk_widget_new (GTK_TYPE_WINDOW,
350                                                      "type", GTK_WINDOW_POPUP,
351                                                      "child", menu,
352                                                      NULL),
353                                      "signal::event", gtk_menu_window_event, menu,
354                                      "signal::destroy", gtk_widget_destroyed, &menu->toplevel,
355                                      NULL);
356   gtk_window_set_policy (GTK_WINDOW (menu->toplevel),
357                          FALSE, FALSE, TRUE);
358   gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->toplevel), 0);
359
360   /* Refloat the menu, so that reference counting for the menu isn't
361    * affected by it being a child of the toplevel
362    */
363   GTK_WIDGET_SET_FLAGS (menu, GTK_FLOATING);
364   menu->needs_destruction_ref_count = TRUE;
365
366   menu->view_window = NULL;
367   menu->bin_window = NULL;
368
369   menu->scroll_offset = 0;
370   menu->scroll_step  = 0;
371   menu->timeout_id = 0;
372   menu->scroll_fast = FALSE;
373   
374   menu->tearoff_window = NULL;
375   menu->tearoff_hbox = NULL;
376   menu->torn_off = FALSE;
377   menu->tearoff_active = FALSE;
378   menu->tearoff_adjustment = NULL;
379   menu->tearoff_scrollbar = NULL;
380
381   menu->upper_arrow_visible = FALSE;
382   menu->lower_arrow_visible = FALSE;
383   menu->upper_arrow_prelight = FALSE;
384   menu->lower_arrow_prelight = FALSE;
385   
386   MENU_NEEDS_RESIZE (menu) = TRUE;
387 }
388
389 static void
390 gtk_menu_destroy (GtkObject *object)
391 {
392   GtkMenu *menu;
393   GtkMenuAttachData *data;
394
395   g_return_if_fail (GTK_IS_MENU (object));
396
397   menu = GTK_MENU (object);
398
399   gtk_menu_stop_scrolling (menu);
400   
401   data = gtk_object_get_data (object, attach_data_key);
402   if (data)
403     gtk_menu_detach (menu);
404   
405   gtk_menu_stop_navigating_submenu (menu);
406
407   if (menu->old_active_menu_item)
408     {
409       gtk_widget_unref (menu->old_active_menu_item);
410       menu->old_active_menu_item = NULL;
411     }
412
413   /* Add back the reference count for being a child */
414   if (menu->needs_destruction_ref_count)
415     {
416       menu->needs_destruction_ref_count = FALSE;
417       gtk_object_ref (object);
418     }
419   
420   if (menu->accel_group)
421     {
422       g_object_unref (menu->accel_group);
423       menu->accel_group = NULL;
424     }
425
426   if (menu->toplevel)
427     gtk_widget_destroy (menu->toplevel);
428   if (menu->tearoff_window)
429     gtk_widget_destroy (menu->tearoff_window);
430
431   GTK_OBJECT_CLASS (parent_class)->destroy (object);
432 }
433
434 static void
435 gtk_menu_finalize (GObject *object)
436 {
437   GtkMenu *menu = GTK_MENU (object);
438
439   g_free (menu->accel_path);
440   
441   G_OBJECT_CLASS (parent_class)->finalize (object);
442 }
443
444 void
445 gtk_menu_attach_to_widget (GtkMenu             *menu,
446                            GtkWidget           *attach_widget,
447                            GtkMenuDetachFunc    detacher)
448 {
449   GtkMenuAttachData *data;
450   
451   g_return_if_fail (GTK_IS_MENU (menu));
452   g_return_if_fail (GTK_IS_WIDGET (attach_widget));
453   g_return_if_fail (detacher != NULL);
454   
455   /* keep this function in sync with gtk_widget_set_parent()
456    */
457   
458   data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
459   if (data)
460     {
461       g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
462                  gtk_type_name (GTK_OBJECT_TYPE (data->attach_widget)));
463       return;
464     }
465   
466   gtk_object_ref (GTK_OBJECT (menu));
467   gtk_object_sink (GTK_OBJECT (menu));
468   
469   data = g_new (GtkMenuAttachData, 1);
470   data->attach_widget = attach_widget;
471   data->detacher = detacher;
472   gtk_object_set_data (GTK_OBJECT (menu), attach_data_key, data);
473   
474   if (GTK_WIDGET_STATE (menu) != GTK_STATE_NORMAL)
475     gtk_widget_set_state (GTK_WIDGET (menu), GTK_STATE_NORMAL);
476   
477   /* we don't need to set the style here, since
478    * we are a toplevel widget.
479    */
480
481   /* Fallback title for menu comes from attach widget */
482   gtk_menu_update_title (menu);
483 }
484
485 GtkWidget*
486 gtk_menu_get_attach_widget (GtkMenu *menu)
487 {
488   GtkMenuAttachData *data;
489   
490   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
491   
492   data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
493   if (data)
494     return data->attach_widget;
495   return NULL;
496 }
497
498 void
499 gtk_menu_detach (GtkMenu *menu)
500 {
501   GtkMenuAttachData *data;
502   
503   g_return_if_fail (GTK_IS_MENU (menu));
504   
505   /* keep this function in sync with gtk_widget_unparent()
506    */
507   data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
508   if (!data)
509     {
510       g_warning ("gtk_menu_detach(): menu is not attached");
511       return;
512     }
513   gtk_object_remove_data (GTK_OBJECT (menu), attach_data_key);
514   
515   data->detacher (data->attach_widget, menu);
516   
517   if (GTK_WIDGET_REALIZED (menu))
518     gtk_widget_unrealize (GTK_WIDGET (menu));
519   
520   g_free (data);
521   
522   /* Fallback title for menu comes from attach widget */
523   gtk_menu_update_title (menu);
524
525   gtk_widget_unref (GTK_WIDGET (menu));
526 }
527
528 static void 
529 gtk_menu_remove (GtkContainer *container,
530                  GtkWidget    *widget)
531 {
532   GtkMenu *menu;
533   g_return_if_fail (GTK_IS_MENU (container));
534   g_return_if_fail (GTK_IS_MENU_ITEM (widget));
535
536   menu = GTK_MENU (container);
537
538   /* Clear out old_active_menu_item if it matches the item we are removing
539    */
540   if (menu->old_active_menu_item == widget)
541     {
542       gtk_widget_unref (menu->old_active_menu_item);
543       menu->old_active_menu_item = NULL;
544     }
545
546   GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
547 }
548
549
550 GtkWidget*
551 gtk_menu_new (void)
552 {
553   return GTK_WIDGET (gtk_type_new (gtk_menu_get_type ()));
554 }
555
556 static void
557 gtk_menu_real_insert (GtkMenuShell     *menu_shell,
558                       GtkWidget        *child,
559                       gint              position)
560 {
561   if (GTK_WIDGET_REALIZED (menu_shell))
562     gtk_widget_set_parent_window (child, GTK_MENU (menu_shell)->bin_window);
563   
564   GTK_MENU_SHELL_CLASS (parent_class)->insert (menu_shell, child, position);
565 }
566
567 static void
568 gtk_menu_tearoff_bg_copy (GtkMenu *menu)
569 {
570   GtkWidget *widget;
571   gint width, height;
572
573   widget = GTK_WIDGET (menu);
574
575   if (menu->torn_off)
576     {
577       GdkPixmap *pixmap;
578       GdkGC *gc;
579       GdkGCValues gc_values;
580
581       menu->tearoff_active = FALSE;
582       menu->saved_scroll_offset = menu->scroll_offset;
583       
584       gc_values.subwindow_mode = GDK_INCLUDE_INFERIORS;
585       gc = gdk_gc_new_with_values (widget->window,
586                                    &gc_values, GDK_GC_SUBWINDOW);
587       
588       gdk_window_get_size (menu->tearoff_window->window, &width, &height);
589       
590       pixmap = gdk_pixmap_new (menu->tearoff_window->window,
591                                width,
592                                height,
593                                -1);
594
595       gdk_draw_pixmap (pixmap, gc,
596                        menu->tearoff_window->window,
597                        0, 0, 0, 0, -1, -1);
598       gdk_gc_unref (gc);
599
600       gtk_widget_set_usize (menu->tearoff_window,
601                             width,
602                             height);
603
604       gdk_window_set_back_pixmap (menu->tearoff_window->window, pixmap, FALSE);
605       gdk_pixmap_unref (pixmap);
606     }
607 }
608
609 static gboolean
610 popup_grab_on_window (GdkWindow *window,
611                       guint32    activate_time)
612 {
613   if ((gdk_pointer_grab (window, TRUE,
614                          GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
615                          GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK |
616                          GDK_POINTER_MOTION_MASK,
617                          NULL, NULL, activate_time) == 0))
618     {
619       if (gdk_keyboard_grab (window, TRUE,
620                              activate_time) == 0)
621         return TRUE;
622       else
623         {
624           gdk_display_pointer_ungrab (gdk_drawable_get_display (window),
625                                       activate_time);
626           return FALSE;
627         }
628     }
629
630   return FALSE;
631 }
632
633 void
634 gtk_menu_popup (GtkMenu             *menu,
635                 GtkWidget           *parent_menu_shell,
636                 GtkWidget           *parent_menu_item,
637                 GtkMenuPositionFunc  func,
638                 gpointer             data,
639                 guint                button,
640                 guint32              activate_time)
641 {
642   GtkWidget *widget;
643   GtkWidget *xgrab_shell;
644   GtkWidget *parent;
645   GdkEvent *current_event;
646   GtkMenuShell *menu_shell;
647   GtkMenuAttachData *attach_data;
648
649   g_return_if_fail (GTK_IS_MENU (menu));
650   
651   widget = GTK_WIDGET (menu);
652   menu_shell = GTK_MENU_SHELL (menu);
653   
654   menu_shell->parent_menu_shell = parent_menu_shell;
655   
656   if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-screen"))
657     {
658       /* The screen was not set explicitly, if the menu is
659        * attached to a widget, try to get screen from its 
660        * toplevel window else go with the default
661        */
662       attach_data = gtk_object_get_data (GTK_OBJECT (menu), attach_data_key);
663       if (attach_data)
664         {
665           if (!GTK_WIDGET_REALIZED (menu))
666           gtk_window_set_screen (GTK_WINDOW (menu->toplevel),
667                                  gtk_widget_get_screen (attach_data->attach_widget));
668         }
669     }
670
671   /* Find the last viewable ancestor, and make an X grab on it
672    */
673   parent = GTK_WIDGET (menu);
674   xgrab_shell = NULL;
675   while (parent)
676     {
677       gboolean viewable = TRUE;
678       GtkWidget *tmp = parent;
679       
680       while (tmp)
681         {
682           if (!GTK_WIDGET_MAPPED (tmp))
683             {
684               viewable = FALSE;
685               break;
686             }
687           tmp = tmp->parent;
688         }
689       
690       if (viewable)
691         xgrab_shell = parent;
692       
693       parent = GTK_MENU_SHELL (parent)->parent_menu_shell;
694     }
695
696   /* We want to receive events generated when we map the menu; unfortunately,
697    * since there is probably already an implicit grab in place from the
698    * button that the user used to pop up the menu, we won't receive then --
699    * in particular, the EnterNotify when the menu pops up under the pointer.
700    *
701    * If we are grabbing on a parent menu shell, no problem; just grab on
702    * that menu shell first before popping up the window with owner_events = TRUE.
703    *
704    * When grabbing on the menu itself, things get more convuluted - we
705    * we do an explicit grab on a specially created window with
706    * owner_events = TRUE, which we override further down with a grab
707    * on the menu. (We can't grab on the menu until it is mapped; we
708    * probably could just leave the grab on the other window, with a
709    * little reorganization of the code in gtkmenu*).
710    */
711   if (xgrab_shell && xgrab_shell != widget)
712     {
713       if (popup_grab_on_window (xgrab_shell->window, activate_time))
714         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
715     }
716   else
717     {
718       GdkWindow *transfer_window;
719
720       xgrab_shell = widget;
721       transfer_window = menu_grab_transfer_window_get (menu);
722       if (popup_grab_on_window (transfer_window, activate_time))
723         GTK_MENU_SHELL (xgrab_shell)->have_xgrab = TRUE;
724     }
725
726   if (!GTK_MENU_SHELL (xgrab_shell)->have_xgrab)
727     {
728       /* We failed to make our pointer/keyboard grab. Rather than leaving the user
729        * with a stuck up window, we just abort here. Presumably the user will
730        * try again.
731        */
732       menu_shell->parent_menu_shell = NULL;
733       menu_grab_transfer_window_destroy (menu);
734       return;
735     }
736
737   menu_shell->active = TRUE;
738   menu_shell->button = button;
739
740   /* If we are popping up the menu from something other than, a button
741    * press then, as a heuristic, we ignore enter events for the menu
742    * until we get a MOTION_NOTIFY.  
743    */
744
745   current_event = gtk_get_current_event ();
746   if (current_event)
747     {
748       if ((current_event->type != GDK_BUTTON_PRESS) &&
749           (current_event->type != GDK_ENTER_NOTIFY))
750         menu_shell->ignore_enter = TRUE;
751
752       gdk_event_free (current_event);
753     }
754
755   if (menu->torn_off)
756     {
757       gtk_menu_tearoff_bg_copy (menu);
758
759       gtk_menu_reparent (menu, menu->toplevel, FALSE);
760     }
761   
762   menu->parent_menu_item = parent_menu_item;
763   menu->position_func = func;
764   menu->position_func_data = data;
765   menu_shell->activate_time = activate_time;
766
767   /* We need to show the menu here rather in the init function because
768    * code expects to be able to tell if the menu is onscreen by
769    * looking at the GTK_WIDGET_VISIBLE (menu)
770    */
771   gtk_widget_show (GTK_WIDGET (menu));
772
773   /* Position the menu, possibly changing the size request
774    */
775   gtk_menu_position (menu);
776
777   /* Compute the size of the toplevel and realize it so we
778    * can scroll correctly.
779    */
780   {
781     GtkRequisition tmp_request;
782     GtkAllocation tmp_allocation = { 0, };
783
784     gtk_widget_size_request (menu->toplevel, &tmp_request);
785     
786     tmp_allocation.width = tmp_request.width;
787     tmp_allocation.height = tmp_request.height;
788
789     gtk_widget_size_allocate (menu->toplevel, &tmp_allocation);
790     
791     gtk_widget_realize (GTK_WIDGET (menu));
792   }
793
794   gtk_menu_scroll_to (menu, menu->scroll_offset);
795
796   /* Once everything is set up correctly, map the toplevel window on
797      the screen.
798    */
799   gtk_widget_show (menu->toplevel);
800
801   if (xgrab_shell == widget)
802     popup_grab_on_window (widget->window, activate_time); /* Should always succeed */
803
804   gtk_grab_add (GTK_WIDGET (menu));
805 }
806
807 void
808 gtk_menu_popdown (GtkMenu *menu)
809 {
810   GtkMenuShell *menu_shell;
811
812   g_return_if_fail (GTK_IS_MENU (menu));
813   
814   menu_shell = GTK_MENU_SHELL (menu);
815   
816   menu_shell->parent_menu_shell = NULL;
817   menu_shell->active = FALSE;
818   menu_shell->ignore_enter = FALSE;
819
820   gtk_menu_stop_scrolling (menu);
821   
822   gtk_menu_stop_navigating_submenu (menu);
823   
824   if (menu_shell->active_menu_item)
825     {
826       if (menu->old_active_menu_item)
827         gtk_widget_unref (menu->old_active_menu_item);
828       menu->old_active_menu_item = menu_shell->active_menu_item;
829       gtk_widget_ref (menu->old_active_menu_item);
830     }
831
832   gtk_menu_shell_deselect (menu_shell);
833   
834   /* The X Grab, if present, will automatically be removed when we hide
835    * the window */
836   gtk_widget_hide (menu->toplevel);
837
838   if (menu->torn_off)
839     {
840       gint width, height;
841       gdk_window_get_size (menu->tearoff_window->window, &width, &height);
842       gtk_widget_set_usize (menu->tearoff_window,
843                             -1,
844                             height);
845       
846       if (GTK_BIN (menu->toplevel)->child) 
847         {
848           gtk_menu_reparent (menu, menu->tearoff_hbox, TRUE);
849         } 
850       else
851         {
852           /* We popped up the menu from the tearoff, so we need to 
853            * release the grab - we aren't actually hiding the menu.
854            */
855           if (menu_shell->have_xgrab)
856             {
857               GdkDisplay *display = gtk_widget_get_display (GTK_WIDGET (menu));
858               
859               gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
860               gdk_display_keyboard_ungrab (display, GDK_CURRENT_TIME);
861             }
862         }
863
864       /* gtk_menu_popdown is called each time a menu item is selected from
865        * a torn off menu. Only scroll back to the saved position if the
866        * non-tearoff menu was popped down.
867        */
868       if (!menu->tearoff_active)
869         gtk_menu_scroll_to (menu, menu->saved_scroll_offset);
870       menu->tearoff_active = TRUE;
871     }
872   else
873     gtk_widget_hide (GTK_WIDGET (menu));
874
875   menu_shell->have_xgrab = FALSE;
876   gtk_grab_remove (GTK_WIDGET (menu));
877
878   menu_grab_transfer_window_destroy (menu);
879 }
880
881 GtkWidget*
882 gtk_menu_get_active (GtkMenu *menu)
883 {
884   GtkWidget *child;
885   GList *children;
886   
887   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
888   
889   if (!menu->old_active_menu_item)
890     {
891       child = NULL;
892       children = GTK_MENU_SHELL (menu)->children;
893       
894       while (children)
895         {
896           child = children->data;
897           children = children->next;
898           
899           if (GTK_BIN (child)->child)
900             break;
901           child = NULL;
902         }
903       
904       menu->old_active_menu_item = child;
905       if (menu->old_active_menu_item)
906         gtk_widget_ref (menu->old_active_menu_item);
907     }
908   
909   return menu->old_active_menu_item;
910 }
911
912 void
913 gtk_menu_set_active (GtkMenu *menu,
914                      guint    index)
915 {
916   GtkWidget *child;
917   GList *tmp_list;
918   
919   g_return_if_fail (GTK_IS_MENU (menu));
920   
921   tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->children, index);
922   if (tmp_list)
923     {
924       child = tmp_list->data;
925       if (GTK_BIN (child)->child)
926         {
927           if (menu->old_active_menu_item)
928             gtk_widget_unref (menu->old_active_menu_item);
929           menu->old_active_menu_item = child;
930           gtk_widget_ref (menu->old_active_menu_item);
931         }
932     }
933 }
934
935 void
936 gtk_menu_set_accel_group (GtkMenu       *menu,
937                           GtkAccelGroup *accel_group)
938 {
939   g_return_if_fail (GTK_IS_MENU (menu));
940   
941   if (menu->accel_group != accel_group)
942     {
943       if (menu->accel_group)
944         gtk_accel_group_unref (menu->accel_group);
945       menu->accel_group = accel_group;
946       if (menu->accel_group)
947         gtk_accel_group_ref (menu->accel_group);
948       _gtk_menu_refresh_accel_paths (menu, TRUE);
949     }
950 }
951
952 GtkAccelGroup*
953 gtk_menu_get_accel_group (GtkMenu *menu)
954 {
955   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
956
957   return menu->accel_group;
958 }
959
960 /**
961  * gtk_menu_set_accel_path
962  * @menu:       a valid #GtkMenu
963  * @accel_path: a valid accelerator path
964  *
965  * Sets an accelerator path for this menu from which accelerator paths
966  * for its immediate children, its menu items, can be constructed.
967  * The main purpose of this function is to spare the programmer the
968  * inconvenience of having to call gtk_menu_item_set_accel_path() on
969  * each menu item that should support runtime user changable accelerators.
970  * Instead, by just calling gtk_menu_set_accel_path() on their parent,
971  * each menu item of this menu, that contains a label describing its purpose,
972  * automatically gets an accel path assigned. For example, a menu containing
973  * menu items "New" and "Exit", will, after 
974  * <literal>gtk_menu_set_accel_path (menu, "&lt;Gnumeric-Sheet&gt;/File");</literal>
975  * has been called, assign its items the accel paths:
976  * <literal>"&lt;Gnumeric-Sheet&gt;/File/New"</literal> and <literal>"&lt;Gnumeric-Sheet&gt;/File/Exit"</literal>.
977  * Assigning accel paths to menu items then enables the user to change
978  * their accelerators at runtime. More details about accelerator paths
979  * and their default setups can be found at gtk_accel_map_add_entry().
980  */
981 void
982 gtk_menu_set_accel_path (GtkMenu     *menu,
983                          const gchar *accel_path)
984 {
985   g_return_if_fail (GTK_IS_MENU (menu));
986   if (accel_path)
987     g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
988
989   g_free (menu->accel_path);
990   menu->accel_path = g_strdup (accel_path);
991   if (menu->accel_path)
992     _gtk_menu_refresh_accel_paths (menu, FALSE);
993 }
994
995 typedef struct {
996   GtkMenu *menu;
997   gboolean group_changed;
998 } AccelPropagation;
999
1000 static void
1001 refresh_accel_paths_foreach (GtkWidget *widget,
1002                              gpointer   data)
1003 {
1004   AccelPropagation *prop = data;
1005
1006   if (GTK_IS_MENU_ITEM (widget))        /* should always be true */
1007     _gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
1008                                        prop->menu->accel_path,
1009                                        prop->menu->accel_group,
1010                                        prop->group_changed);
1011 }
1012
1013 static void
1014 _gtk_menu_refresh_accel_paths (GtkMenu *menu,
1015                                gboolean group_changed)
1016 {
1017   g_return_if_fail (GTK_IS_MENU (menu));
1018       
1019   if (menu->accel_path && menu->accel_group)
1020     {
1021       AccelPropagation prop;
1022
1023       prop.menu = menu;
1024       prop.group_changed = group_changed;
1025       gtk_container_foreach (GTK_CONTAINER (menu),
1026                              refresh_accel_paths_foreach,
1027                              &prop);
1028     }
1029 }
1030
1031 void
1032 gtk_menu_reposition (GtkMenu *menu)
1033 {
1034   g_return_if_fail (GTK_IS_MENU (menu));
1035
1036   if (GTK_WIDGET_DRAWABLE (menu) && !menu->torn_off)
1037     gtk_menu_position (menu);
1038 }
1039
1040 static void
1041 gtk_menu_scrollbar_changed (GtkAdjustment *adjustment,
1042                             GtkMenu       *menu)
1043 {
1044   g_return_if_fail (GTK_IS_MENU (menu));
1045
1046   if (adjustment->value != menu->scroll_offset)
1047     gtk_menu_scroll_to (menu, adjustment->value);
1048 }
1049
1050 static void
1051 gtk_menu_set_tearoff_hints (GtkMenu *menu,
1052                             gint     width)
1053 {
1054   GdkGeometry geometry_hints;
1055   
1056   if (!menu->tearoff_window)
1057     return;
1058
1059   if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1060     {
1061       gtk_widget_size_request (menu->tearoff_scrollbar, NULL);
1062       width += menu->tearoff_scrollbar->requisition.width;
1063     }
1064
1065   geometry_hints.min_width = width;
1066   geometry_hints.max_width = width;
1067     
1068   geometry_hints.min_height = 0;
1069   geometry_hints.max_height = GTK_WIDGET (menu)->requisition.height;
1070   gtk_window_set_geometry_hints (GTK_WINDOW (menu->tearoff_window),
1071                                  NULL,
1072                                  &geometry_hints,
1073                                  GDK_HINT_MAX_SIZE|GDK_HINT_MIN_SIZE);
1074 }
1075
1076 static void
1077 gtk_menu_update_title (GtkMenu *menu)
1078 {
1079   if (menu->tearoff_window)
1080     {
1081       const gchar *title;
1082       GtkWidget *attach_widget;
1083
1084       title = gtk_menu_get_title (menu);
1085       if (!title)
1086         {
1087           attach_widget = gtk_menu_get_attach_widget (menu);
1088           if (GTK_IS_MENU_ITEM (attach_widget))
1089             {
1090               GtkWidget *child = GTK_BIN (attach_widget)->child;
1091               if (GTK_IS_LABEL (child))
1092                 title = gtk_label_get_text (GTK_LABEL (child));
1093             }
1094         }
1095       
1096       if (title)
1097         gtk_window_set_title (GTK_WINDOW (menu->tearoff_window), title);
1098     }
1099 }
1100
1101 void       
1102 gtk_menu_set_tearoff_state (GtkMenu  *menu,
1103                             gboolean  torn_off)
1104 {
1105   gint width, height;
1106   
1107   g_return_if_fail (GTK_IS_MENU (menu));
1108
1109   if (menu->torn_off != torn_off)
1110     {
1111       menu->torn_off = torn_off;
1112       menu->tearoff_active = torn_off;
1113       
1114       if (menu->torn_off)
1115         {
1116           if (GTK_WIDGET_VISIBLE (menu))
1117             gtk_menu_popdown (menu);
1118
1119           if (!menu->tearoff_window)
1120             {
1121               menu->tearoff_window = gtk_widget_new (GTK_TYPE_WINDOW,
1122                                                      "type", GTK_WINDOW_TOPLEVEL,
1123                                                      "screen", gtk_widget_get_screen (menu->toplevel),
1124                                                      "app_paintable", TRUE,
1125                                                      NULL);
1126
1127               gtk_window_set_type_hint (GTK_WINDOW (menu->tearoff_window),
1128                                         GDK_WINDOW_TYPE_HINT_MENU);
1129               gtk_window_set_mnemonic_modifier (GTK_WINDOW (menu->tearoff_window), 0);
1130               g_signal_connect (menu->tearoff_window, "destroy",
1131                                 G_CALLBACK (gtk_widget_destroyed), &menu->tearoff_window);
1132               g_signal_connect (menu->tearoff_window, "event",
1133                                 G_CALLBACK (gtk_menu_window_event), menu);
1134
1135               gtk_menu_update_title (menu);
1136
1137               gtk_widget_realize (menu->tearoff_window);
1138               
1139               menu->tearoff_hbox = gtk_hbox_new (FALSE, FALSE);
1140               gtk_container_add (GTK_CONTAINER (menu->tearoff_window), menu->tearoff_hbox);
1141
1142               gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
1143               menu->tearoff_adjustment =
1144                 GTK_ADJUSTMENT (gtk_adjustment_new (0,
1145                                                     0,
1146                                                     GTK_WIDGET (menu)->requisition.height,
1147                                                     MENU_SCROLL_STEP,
1148                                                     height/2,
1149                                                     height));
1150               g_object_connect (GTK_OBJECT (menu->tearoff_adjustment),
1151                                 "signal::value_changed", gtk_menu_scrollbar_changed, menu,
1152                                 NULL);
1153               menu->tearoff_scrollbar = gtk_vscrollbar_new (menu->tearoff_adjustment);
1154
1155               gtk_box_pack_end (GTK_BOX (menu->tearoff_hbox),
1156                                 menu->tearoff_scrollbar,
1157                                 FALSE, FALSE, 0);
1158               
1159               if (menu->tearoff_adjustment->upper > height)
1160                 gtk_widget_show (menu->tearoff_scrollbar);
1161               
1162               gtk_widget_show (menu->tearoff_hbox);
1163             }
1164           
1165           gtk_menu_reparent (menu, menu->tearoff_hbox, FALSE);
1166
1167           gdk_window_get_size (GTK_WIDGET (menu)->window, &width, NULL);
1168
1169           /* Update menu->requisition
1170            */
1171           gtk_widget_size_request (GTK_WIDGET (menu), NULL);
1172   
1173           gtk_menu_set_tearoff_hints (menu, width);
1174             
1175           gtk_widget_realize (menu->tearoff_window);
1176           gtk_menu_position (menu);
1177           
1178           gtk_widget_show (GTK_WIDGET (menu));
1179           gtk_widget_show (menu->tearoff_window);
1180
1181           gtk_menu_scroll_to (menu, 0);
1182
1183         }
1184       else
1185         {
1186           gtk_widget_hide (menu->tearoff_window);
1187           gtk_menu_reparent (menu, menu->toplevel, FALSE);
1188         }
1189     }
1190 }
1191
1192 /**
1193  * gtk_menu_get_tearoff_state:
1194  * @menu: a #GtkMenu
1195  *
1196  * Returns whether the menu is torn off. See
1197  * gtk_menu_set_tearoff_state ().
1198  *
1199  * Return value: %TRUE if the menu is currently torn off.
1200  **/
1201 gboolean
1202 gtk_menu_get_tearoff_state (GtkMenu *menu)
1203 {
1204   g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
1205
1206   return menu->torn_off;
1207 }
1208
1209 /**
1210  * gtk_menu_set_title:
1211  * @menu: a #GtkMenu
1212  * @title: a string containing the title for the menu.
1213  * 
1214  * Sets the title string for the menu.  The title is displayed when the menu
1215  * is shown as a tearoff menu.
1216  **/
1217 void       
1218 gtk_menu_set_title (GtkMenu     *menu,
1219                     const gchar *title)
1220 {
1221   g_return_if_fail (GTK_IS_MENU (menu));
1222
1223   if (title)
1224     g_object_set_data_full (G_OBJECT (menu), "gtk-menu-title",
1225                             g_strdup (title), (GtkDestroyNotify) g_free);
1226   else
1227     g_object_set_data (G_OBJECT (menu), "gtk-menu-title", NULL);
1228     
1229   gtk_menu_update_title (menu);
1230   g_object_notify (G_OBJECT (menu), "tearoff_title");
1231 }
1232
1233 /**
1234  * gtk_menu_get_title:
1235  * @menu: a #GtkMenu
1236  *
1237  * Returns the title of the menu. See gtk_menu_set_title().
1238  *
1239  * Return value: the title of the menu, or %NULL if the menu has no
1240  *               title set on it.
1241  **/
1242 G_CONST_RETURN gchar *
1243 gtk_menu_get_title (GtkMenu *menu)
1244 {
1245   g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
1246
1247   return gtk_object_get_data (GTK_OBJECT (menu), "gtk-menu-title");
1248 }
1249
1250 void
1251 gtk_menu_reorder_child (GtkMenu   *menu,
1252                         GtkWidget *child,
1253                         gint       position)
1254 {
1255   GtkMenuShell *menu_shell;
1256   g_return_if_fail (GTK_IS_MENU (menu));
1257   g_return_if_fail (GTK_IS_MENU_ITEM (child));
1258   menu_shell = GTK_MENU_SHELL (menu);
1259   if (g_list_find (menu_shell->children, child))
1260     {   
1261       menu_shell->children = g_list_remove (menu_shell->children, child);
1262       menu_shell->children = g_list_insert (menu_shell->children, child, position);   
1263       if (GTK_WIDGET_VISIBLE (menu_shell))
1264         gtk_widget_queue_resize (GTK_WIDGET (menu_shell));
1265     }   
1266 }
1267
1268 static void
1269 gtk_menu_style_set (GtkWidget *widget,
1270                     GtkStyle  *previous_style)
1271 {
1272   if (GTK_WIDGET_REALIZED (widget))
1273     {
1274       GtkMenu *menu = GTK_MENU (widget);
1275       
1276       gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1277       gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1278       gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1279     }
1280 }
1281
1282 static void
1283 gtk_menu_realize (GtkWidget *widget)
1284 {
1285   GdkWindowAttr attributes;
1286   gint attributes_mask;
1287   gint border_width;
1288   GtkMenu *menu;
1289   GtkWidget *child;
1290   GList *children;
1291
1292   g_return_if_fail (GTK_IS_MENU (widget));
1293
1294   menu = GTK_MENU (widget);
1295   
1296   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1297   
1298   attributes.window_type = GDK_WINDOW_CHILD;
1299   attributes.x = widget->allocation.x;
1300   attributes.y = widget->allocation.y;
1301   attributes.width = widget->allocation.width;
1302   attributes.height = widget->allocation.height;
1303   attributes.wclass = GDK_INPUT_OUTPUT;
1304   attributes.visual = gtk_widget_get_visual (widget);
1305   attributes.colormap = gtk_widget_get_colormap (widget);
1306   
1307   attributes.event_mask = gtk_widget_get_events (widget);
1308   attributes.event_mask |= (GDK_EXPOSURE_MASK | GDK_KEY_PRESS_MASK |
1309                             GDK_ENTER_NOTIFY_MASK | GDK_LEAVE_NOTIFY_MASK );
1310   
1311   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1312   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
1313   gdk_window_set_user_data (widget->window, widget);
1314   
1315   border_width = GTK_CONTAINER (widget)->border_width;
1316   
1317   attributes.x = border_width + widget->style->xthickness;
1318   attributes.y = border_width + widget->style->ythickness;
1319   attributes.width = MAX (1, widget->allocation.width - attributes.x * 2);
1320   attributes.height = MAX (1, widget->allocation.height - attributes.y * 2);
1321
1322   if (menu->upper_arrow_visible)
1323     {
1324       attributes.y += MENU_SCROLL_ARROW_HEIGHT;
1325       attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1326     }
1327   if (menu->lower_arrow_visible)
1328     attributes.height -= MENU_SCROLL_ARROW_HEIGHT;
1329
1330   menu->view_window = gdk_window_new (widget->window, &attributes, attributes_mask);
1331   gdk_window_set_user_data (menu->view_window, menu);
1332
1333   attributes.x = 0;
1334   attributes.y = 0;
1335   attributes.height = MAX (1, widget->requisition.height - (border_width + widget->style->ythickness) * 2);
1336   
1337   menu->bin_window = gdk_window_new (menu->view_window, &attributes, attributes_mask);
1338   gdk_window_set_user_data (menu->bin_window, menu);
1339
1340   children = GTK_MENU_SHELL (menu)->children;
1341   while (children)
1342     {
1343       child = children->data;
1344       children = children->next;
1345           
1346       gtk_widget_set_parent_window (child, menu->bin_window);
1347     }
1348   
1349   widget->style = gtk_style_attach (widget->style, widget->window);
1350   gtk_style_set_background (widget->style, menu->bin_window, GTK_STATE_NORMAL);
1351   gtk_style_set_background (widget->style, menu->view_window, GTK_STATE_NORMAL);
1352   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1353
1354   if (GTK_MENU_SHELL (widget)->active_menu_item)
1355     gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
1356                                   GTK_MENU_SHELL (widget)->active_menu_item);
1357
1358   gdk_window_show (menu->bin_window);
1359   gdk_window_show (menu->view_window);
1360 }
1361
1362 /* See notes in gtk_menu_popup() for information about the "grab transfer window"
1363  */
1364 static GdkWindow *
1365 menu_grab_transfer_window_get (GtkMenu *menu)
1366 {
1367   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1368   if (!window)
1369     {
1370       GdkWindowAttr attributes;
1371       gint attributes_mask;
1372       
1373       attributes.x = -100;
1374       attributes.y = -100;
1375       attributes.width = 10;
1376       attributes.height = 10;
1377       attributes.window_type = GDK_WINDOW_TEMP;
1378       attributes.wclass = GDK_INPUT_ONLY;
1379       attributes.override_redirect = TRUE;
1380       attributes.event_mask = 0;
1381
1382       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
1383       
1384       window = gdk_window_new (gtk_widget_get_root_window (GTK_WIDGET (menu)),
1385                                &attributes, attributes_mask);
1386       gdk_window_set_user_data (window, menu);
1387
1388       gdk_window_show (window);
1389
1390       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", window);
1391     }
1392
1393   return window;
1394 }
1395
1396 static void
1397 menu_grab_transfer_window_destroy (GtkMenu *menu)
1398 {
1399   GdkWindow *window = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-window");
1400   if (window)
1401     {
1402       gdk_window_set_user_data (window, NULL);
1403       gdk_window_destroy (window);
1404       g_object_set_data (G_OBJECT (menu), "gtk-menu-transfer-window", NULL);
1405     }
1406 }
1407
1408 static void
1409 gtk_menu_unrealize (GtkWidget *widget)
1410 {
1411   GtkMenu *menu;
1412
1413   g_return_if_fail (GTK_IS_MENU (widget));
1414
1415   menu = GTK_MENU (widget);
1416
1417   menu_grab_transfer_window_destroy (menu);
1418
1419   gdk_window_set_user_data (menu->view_window, NULL);
1420   gdk_window_destroy (menu->view_window);
1421   menu->view_window = NULL;
1422
1423   gdk_window_set_user_data (menu->bin_window, NULL);
1424   gdk_window_destroy (menu->bin_window);
1425   menu->bin_window = NULL;
1426
1427   (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
1428 }
1429
1430 static void
1431 gtk_menu_size_request (GtkWidget      *widget,
1432                        GtkRequisition *requisition)
1433 {
1434   GtkMenu *menu;
1435   GtkMenuShell *menu_shell;
1436   GtkWidget *child;
1437   GList *children;
1438   guint max_toggle_size;
1439   guint max_accel_width;
1440   GtkRequisition child_requisition;
1441   
1442   g_return_if_fail (GTK_IS_MENU (widget));
1443   g_return_if_fail (requisition != NULL);
1444   
1445   menu = GTK_MENU (widget);
1446   menu_shell = GTK_MENU_SHELL (widget);
1447   
1448   requisition->width = 0;
1449   requisition->height = 0;
1450   
1451   max_toggle_size = 0;
1452   max_accel_width = 0;
1453   
1454   children = menu_shell->children;
1455   while (children)
1456     {
1457       child = children->data;
1458       children = children->next;
1459       
1460       if (GTK_WIDGET_VISIBLE (child))
1461         {
1462           gint toggle_size;
1463
1464           /* It's important to size_request the child
1465            * before doing the toggle size request, in
1466            * case the toggle size request depends on the size
1467            * request of a child of the child (e.g. for ImageMenuItem)
1468            */
1469           
1470           GTK_MENU_ITEM (child)->show_submenu_indicator = TRUE;
1471           gtk_widget_size_request (child, &child_requisition);
1472           
1473           requisition->width = MAX (requisition->width, child_requisition.width);
1474           requisition->height += child_requisition.height;
1475
1476           gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
1477           max_toggle_size = MAX (max_toggle_size, toggle_size);
1478           max_accel_width = MAX (max_accel_width, GTK_MENU_ITEM (child)->accelerator_width);
1479         }
1480     }
1481   
1482   requisition->width += max_toggle_size + max_accel_width;
1483   requisition->width += (GTK_CONTAINER (menu)->border_width +
1484                          widget->style->xthickness) * 2;
1485   requisition->height += (GTK_CONTAINER (menu)->border_width +
1486                           widget->style->ythickness) * 2;
1487   
1488   menu->toggle_size = max_toggle_size;
1489
1490   /* If the requested width was different than the allocated width, we need to change
1491    * the geometry hints for the tear off window so that the window can actually be resized.
1492    * Don't resize the tearoff if it is not active, because it won't redraw (it is only a background pixmap).
1493    */
1494   if ((requisition->width != GTK_WIDGET (menu)->allocation.width) && menu->tearoff_active)
1495     gtk_menu_set_tearoff_hints (menu, requisition->width);
1496 }
1497
1498 static void
1499 gtk_menu_size_allocate (GtkWidget     *widget,
1500                         GtkAllocation *allocation)
1501 {
1502   GtkMenu *menu;
1503   GtkMenuShell *menu_shell;
1504   GtkWidget *child;
1505   GtkAllocation child_allocation;
1506   GList *children;
1507   gint x, y;
1508   gint width, height;
1509
1510   g_return_if_fail (GTK_IS_MENU (widget));
1511   g_return_if_fail (allocation != NULL);
1512   
1513   menu = GTK_MENU (widget);
1514   menu_shell = GTK_MENU_SHELL (widget);
1515
1516   widget->allocation = *allocation;
1517
1518   x = GTK_CONTAINER (menu)->border_width + widget->style->xthickness;
1519   y = GTK_CONTAINER (menu)->border_width + widget->style->ythickness;
1520   
1521   width = MAX (1, allocation->width - x * 2);
1522   height = MAX (1, allocation->height - y * 2);
1523   
1524   if (menu->upper_arrow_visible && !menu->tearoff_active)
1525     {
1526       y += MENU_SCROLL_ARROW_HEIGHT;
1527       height -= MENU_SCROLL_ARROW_HEIGHT;
1528     }
1529   
1530   if (menu->lower_arrow_visible && !menu->tearoff_active)
1531     height -= MENU_SCROLL_ARROW_HEIGHT;
1532   
1533   if (GTK_WIDGET_REALIZED (widget))
1534     {
1535       gdk_window_move_resize (widget->window,
1536                               allocation->x, allocation->y,
1537                               allocation->width, allocation->height);
1538
1539       gdk_window_move_resize (menu->view_window,
1540                               x,
1541                               y,
1542                               width,
1543                               height);
1544     }
1545
1546   if (menu_shell->children)
1547     {
1548       child_allocation.x = 0;
1549       child_allocation.y = 0;
1550       child_allocation.width = width;
1551       
1552       children = menu_shell->children;
1553       while (children)
1554         {
1555           child = children->data;
1556           children = children->next;
1557           
1558           if (GTK_WIDGET_VISIBLE (child))
1559             {
1560               GtkRequisition child_requisition;
1561               gtk_widget_get_child_requisition (child, &child_requisition);
1562               
1563               child_allocation.height = child_requisition.height;
1564
1565               gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
1566                                                   menu->toggle_size);
1567               gtk_widget_size_allocate (child, &child_allocation);
1568               gtk_widget_queue_draw (child);
1569               
1570               child_allocation.y += child_allocation.height;
1571             }
1572         }
1573       
1574       /* Resize the item window */
1575       if (GTK_WIDGET_REALIZED (widget))
1576         {
1577           gdk_window_resize (menu->bin_window,
1578                              child_allocation.width,
1579                              child_allocation.y);
1580         }
1581
1582
1583       if (menu->tearoff_active)
1584         {
1585           if (allocation->height >= widget->requisition.height)
1586             {
1587               if (GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1588                 {
1589                   gtk_widget_hide (menu->tearoff_scrollbar);
1590                   gtk_menu_set_tearoff_hints (menu, allocation->width);
1591                   gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1592
1593                   gtk_menu_scroll_to (menu, 0);
1594                 }
1595             }
1596           else
1597             {
1598               menu->tearoff_adjustment->upper = widget->requisition.height;
1599               menu->tearoff_adjustment->page_size = allocation->height;
1600               
1601               if (menu->tearoff_adjustment->value + menu->tearoff_adjustment->page_size >
1602                   menu->tearoff_adjustment->upper)
1603                 {
1604                   gint value;
1605                   value = menu->tearoff_adjustment->upper - menu->tearoff_adjustment->page_size;
1606                   if (value < 0)
1607                     value = 0;
1608                   gtk_menu_scroll_to (menu, value);
1609                 }
1610               
1611               gtk_adjustment_changed (menu->tearoff_adjustment);
1612               
1613               if (!GTK_WIDGET_VISIBLE (menu->tearoff_scrollbar))
1614                 {
1615                   gtk_widget_show (menu->tearoff_scrollbar);
1616                   gtk_menu_set_tearoff_hints (menu, allocation->width);
1617                   gtk_widget_set_usize (menu->tearoff_window, -1, allocation->height);
1618                 }
1619             }
1620         }
1621     }
1622 }
1623
1624 static void
1625 gtk_menu_paint (GtkWidget      *widget,
1626                 GdkEventExpose *event)
1627 {
1628   GtkMenu *menu;
1629   gint width, height;
1630   gint border_x, border_y;
1631   
1632   g_return_if_fail (GTK_IS_MENU (widget));
1633
1634   menu = GTK_MENU (widget);
1635   
1636   border_x = GTK_CONTAINER (widget)->border_width + widget->style->xthickness;
1637   border_y = GTK_CONTAINER (widget)->border_width + widget->style->ythickness;
1638   gdk_window_get_size (widget->window, &width, &height);
1639
1640   if (event->window == widget->window)
1641     {
1642       gtk_paint_box (widget->style,
1643                      widget->window,
1644                      GTK_STATE_NORMAL,
1645                      GTK_SHADOW_OUT,
1646                      NULL, widget, "menu",
1647                      0, 0, -1, -1);
1648       if (menu->upper_arrow_visible && !menu->tearoff_active)
1649         {
1650           gtk_paint_box (widget->style,
1651                          widget->window,
1652                          menu->upper_arrow_prelight ?
1653                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1654                          GTK_SHADOW_OUT,
1655                          NULL, widget, "menu",
1656                          border_x,
1657                          border_y,
1658                          width - 2*border_x,
1659                          MENU_SCROLL_ARROW_HEIGHT);
1660           
1661           gtk_paint_arrow (widget->style,
1662                            widget->window,
1663                            menu->upper_arrow_prelight ?
1664                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1665                            GTK_SHADOW_OUT,
1666                            NULL, widget, "menu",
1667                            GTK_ARROW_UP,
1668                            TRUE,
1669                            width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1670                            2 * border_y + 1,
1671                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1672                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1673         }
1674   
1675       if (menu->lower_arrow_visible && !menu->tearoff_active)
1676         {
1677           gtk_paint_box (widget->style,
1678                          widget->window,
1679                          menu->lower_arrow_prelight ?
1680                          GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1681                          GTK_SHADOW_OUT,
1682                          NULL, widget, "menu",
1683                          border_x,
1684                          height - border_y - MENU_SCROLL_ARROW_HEIGHT + 1,
1685                          width - 2*border_x,
1686                          MENU_SCROLL_ARROW_HEIGHT);
1687           
1688           gtk_paint_arrow (widget->style,
1689                            widget->window,
1690                            menu->lower_arrow_prelight ?
1691                            GTK_STATE_PRELIGHT : GTK_STATE_NORMAL,
1692                            GTK_SHADOW_OUT,
1693                            NULL, widget, "menu",
1694                            GTK_ARROW_DOWN,
1695                            TRUE,
1696                            width / 2 - MENU_SCROLL_ARROW_HEIGHT / 2 + 1,
1697                            height - MENU_SCROLL_ARROW_HEIGHT + 1,
1698                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2,
1699                            MENU_SCROLL_ARROW_HEIGHT - 2 * border_y - 2);
1700         }
1701     }
1702 }
1703
1704 static gboolean
1705 gtk_menu_expose (GtkWidget      *widget,
1706                  GdkEventExpose *event)
1707 {
1708   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1709   g_return_val_if_fail (event != NULL, FALSE);
1710
1711   if (GTK_WIDGET_DRAWABLE (widget))
1712     {
1713       gtk_menu_paint (widget, event);
1714       
1715       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
1716     }
1717   
1718   return FALSE;
1719 }
1720
1721 static void
1722 gtk_menu_show (GtkWidget *widget)
1723 {
1724   GtkMenu *menu = GTK_MENU (widget);
1725
1726   _gtk_menu_refresh_accel_paths (menu, FALSE);
1727
1728   GTK_WIDGET_CLASS (parent_class)->show (widget);
1729 }
1730
1731 static gboolean
1732 gtk_menu_key_press (GtkWidget   *widget,
1733                     GdkEventKey *event)
1734 {
1735   GtkMenuShell *menu_shell;
1736   GtkMenu *menu;
1737   gboolean delete = FALSE;
1738   gboolean can_change_accels;
1739   gchar *accel = NULL;
1740   guint accel_key, accel_mods;
1741   GdkModifierType consumed_modifiers;
1742   GdkDisplay *display;
1743   
1744   g_return_val_if_fail (GTK_IS_MENU (widget), FALSE);
1745   g_return_val_if_fail (event != NULL, FALSE);
1746       
1747   menu_shell = GTK_MENU_SHELL (widget);
1748   menu = GTK_MENU (widget);
1749   
1750   gtk_menu_stop_navigating_submenu (menu);
1751
1752   if (GTK_WIDGET_CLASS (parent_class)->key_press_event (widget, event))
1753     return TRUE;
1754
1755   display = gtk_widget_get_display (widget);
1756     
1757   g_object_get (G_OBJECT (gtk_widget_get_settings (widget)),
1758                 "gtk-menu-bar-accel", &accel,
1759                 "gtk-can-change-accels", &can_change_accels,
1760                 NULL);
1761
1762   if (accel)
1763     {
1764       guint keyval = 0;
1765       GdkModifierType mods = 0;
1766       gboolean handled = FALSE;
1767       
1768       gtk_accelerator_parse (accel, &keyval, &mods);
1769
1770       if (keyval == 0)
1771         g_warning ("Failed to parse menu bar accelerator '%s'\n", accel);
1772
1773       /* FIXME this is wrong, needs to be in the global accel resolution
1774        * thing, to properly consider i18n etc., but that probably requires
1775        * AccelGroup changes etc.
1776        */
1777       if (event->keyval == keyval &&
1778           (mods & event->state) == mods)
1779         {
1780           gtk_signal_emit_by_name (GTK_OBJECT (menu), "cancel");
1781         }
1782
1783       g_free (accel);
1784
1785       if (handled)
1786         return TRUE;
1787     }
1788   
1789   switch (event->keyval)
1790     {
1791     case GDK_Delete:
1792     case GDK_KP_Delete:
1793     case GDK_BackSpace:
1794       delete = TRUE;
1795       break;
1796     default:
1797       break;
1798     }
1799
1800   /* Figure out what modifiers went into determining the key symbol */
1801   gdk_keymap_translate_keyboard_state (gdk_keymap_get_for_display (display),
1802                                        event->hardware_keycode, event->state, event->group,
1803                                        NULL, NULL, NULL, &consumed_modifiers);
1804
1805   accel_key = gdk_keyval_to_lower (event->keyval);
1806   accel_mods = event->state & gtk_accelerator_get_default_mod_mask () & ~consumed_modifiers;
1807
1808   /* If lowercasing affects the keysym, then we need to include SHIFT in the modifiers,
1809    * We re-upper case when we match against the keyval, but display and save in caseless form.
1810    */
1811   if (accel_key != event->keyval)
1812     accel_mods |= GDK_SHIFT_MASK;
1813   
1814   /* Modify the accelerators */
1815   if (can_change_accels &&
1816       menu_shell->active_menu_item &&
1817       GTK_BIN (menu_shell->active_menu_item)->child &&                  /* no seperators */
1818       GTK_MENU_ITEM (menu_shell->active_menu_item)->submenu == NULL &&  /* no submenus */
1819       (delete || gtk_accelerator_valid (accel_key, accel_mods)))
1820     {
1821       GtkWidget *menu_item = menu_shell->active_menu_item;
1822       gboolean locked, replace_accels = TRUE;
1823       const gchar *path;
1824
1825       path = _gtk_widget_get_accel_path (menu_item, &locked);
1826       if (!path || locked)
1827         {
1828           /* can't change accelerators on menu_items without paths
1829            * (basically, those items are accelerator-locked).
1830            */
1831           /* g_print("item has no path or is locked, menu prefix: %s\n", menu->accel_path); */
1832           gdk_display_beep (display);
1833         }
1834       else
1835         {
1836           gboolean changed;
1837
1838           /* For the keys that act to delete the current setting, we delete
1839            * the current setting if there is one, otherwise, we set the
1840            * key as the accelerator.
1841            */
1842           if (delete)
1843             {
1844               GtkAccelKey key;
1845               
1846               if (gtk_accel_map_lookup_entry (path, &key) &&
1847                   (key.accel_key || key.accel_mods))
1848                 {
1849                   accel_key = 0;
1850                   accel_mods = 0;
1851                 }
1852             }
1853           changed = gtk_accel_map_change_entry (path, accel_key, accel_mods, replace_accels);
1854
1855           if (!changed)
1856             {
1857               /* we failed, probably because this key is in use and
1858                * locked already
1859                */
1860               /* g_print("failed to change\n"); */
1861               gdk_display_beep (display);
1862             }
1863         }
1864     }
1865   
1866   return TRUE;
1867 }
1868
1869 static gboolean
1870 gtk_menu_motion_notify  (GtkWidget         *widget,
1871                          GdkEventMotion    *event)
1872 {
1873   GtkWidget *menu_item;
1874   GtkMenu *menu;
1875   GtkMenuShell *menu_shell;
1876
1877   gboolean need_enter;
1878
1879   if (GTK_IS_MENU (widget))
1880     gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
1881   
1882   /* We received the event for one of two reasons:
1883    *
1884    * a) We are the active menu, and did gtk_grab_add()
1885    * b) The widget is a child of ours, and the event was propagated
1886    *
1887    * Since for computation of navigation regions, we want the menu which
1888    * is the parent of the menu item, for a), we need to find that menu,
1889    * which may be different from 'widget'.
1890    */
1891   menu_item = gtk_get_event_widget ((GdkEvent*) event);
1892   if (!menu_item || !GTK_IS_MENU_ITEM (menu_item) ||
1893       !_gtk_menu_item_is_selectable (menu_item) ||
1894       !GTK_IS_MENU (menu_item->parent))
1895     return FALSE;
1896
1897   menu_shell = GTK_MENU_SHELL (menu_item->parent);
1898   menu = GTK_MENU (menu_shell);
1899   
1900   need_enter = (menu->navigation_region != NULL || menu_shell->ignore_enter);
1901
1902   /* Check to see if we are within an active submenu's navigation region
1903    */
1904   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
1905     return TRUE; 
1906
1907   if (need_enter)
1908     {
1909       /* The menu is now sensitive to enter events on its items, but
1910        * was previously sensitive.  So we fake an enter event.
1911        */
1912       gint width, height;
1913       
1914       menu_shell->ignore_enter = FALSE; 
1915       
1916       gdk_window_get_size (event->window, &width, &height);
1917       if (event->x >= 0 && event->x < width &&
1918           event->y >= 0 && event->y < height)
1919         {
1920           GdkEvent send_event;
1921
1922           memset (&send_event, 0, sizeof (send_event));
1923           send_event.crossing.type = GDK_ENTER_NOTIFY;
1924           send_event.crossing.window = event->window;
1925           send_event.crossing.time = event->time;
1926           send_event.crossing.send_event = TRUE;
1927           send_event.crossing.x_root = event->x_root;
1928           send_event.crossing.y_root = event->y_root;
1929           send_event.crossing.x = event->x;
1930           send_event.crossing.y = event->y;
1931
1932           /* We send the event to 'widget', the currently active menu,
1933            * instead of 'menu', the menu that the pointer is in. This
1934            * will ensure that the event will be ignored unless the
1935            * menuitem is a child of the active menu or some parent
1936            * menu of the active menu.
1937            */
1938           return gtk_widget_event (widget, &send_event);
1939         }
1940     }
1941
1942   return FALSE;
1943 }
1944
1945 static gboolean
1946 gtk_menu_scroll_timeout (gpointer  data)
1947 {
1948   GtkMenu *menu;
1949   GtkWidget *widget;
1950   gint offset;
1951   gint view_width, view_height;
1952
1953   GDK_THREADS_ENTER ();
1954
1955   menu = GTK_MENU (data);
1956   widget = GTK_WIDGET (menu);
1957
1958   offset = menu->scroll_offset + menu->scroll_step;
1959
1960   /* If we scroll upward and the non-visible top part
1961    * is smaller than the scroll arrow it would be
1962    * pretty stupid to show the arrow and taking more
1963    * screen space than just scrolling to the top.
1964    */
1965   if ((menu->scroll_step < 0) && (offset < MENU_SCROLL_ARROW_HEIGHT))
1966     offset = 0;
1967
1968   /* Don't scroll over the top if we weren't before: */
1969   if ((menu->scroll_offset >= 0) && (offset < 0))
1970     offset = 0;
1971
1972   gdk_window_get_size (widget->window, &view_width, &view_height);
1973
1974   /* Don't scroll past the bottom if we weren't before: */
1975   if (menu->scroll_offset > 0)
1976     view_height -= MENU_SCROLL_ARROW_HEIGHT;
1977   
1978   if ((menu->scroll_offset + view_height <= widget->requisition.height) &&
1979       (offset + view_height > widget->requisition.height))
1980     offset = widget->requisition.height - view_height;
1981
1982   gtk_menu_scroll_to (menu, offset);
1983
1984   GDK_THREADS_LEAVE ();
1985
1986   return TRUE;
1987 }
1988
1989 static void
1990 gtk_menu_handle_scrolling (GtkMenu *menu, gboolean enter)
1991 {
1992   GtkMenuShell *menu_shell;
1993   gint width, height;
1994   gint x, y;
1995   gint border;
1996   GdkRectangle rect;
1997   gboolean in_arrow;
1998   gboolean scroll_fast = FALSE;
1999
2000   menu_shell = GTK_MENU_SHELL (menu);
2001
2002   gdk_window_get_pointer (GTK_WIDGET (menu)->window, &x, &y, NULL);
2003   gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
2004
2005   border = GTK_CONTAINER (menu)->border_width + GTK_WIDGET (menu)->style->ythickness;
2006
2007   if (menu->upper_arrow_visible && !menu->tearoff_active)
2008     {
2009       rect.x = 0;
2010       rect.y = 0;
2011       rect.width = width;
2012       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2013       
2014       in_arrow = FALSE;
2015       if ((x >= rect.x) && (x < rect.x + rect.width) &&
2016           (y >= rect.y) && (y < rect.y + rect.height))
2017         {
2018           in_arrow = TRUE;
2019           scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
2020         }
2021         
2022       if (enter && in_arrow &&
2023           (!menu->upper_arrow_prelight || menu->scroll_fast != scroll_fast))
2024         {
2025           menu->upper_arrow_prelight = TRUE;
2026           menu->scroll_fast = scroll_fast;
2027           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2028           
2029           /* Deselect the active item so that any submenus are poped down */
2030           gtk_menu_shell_deselect (menu_shell);
2031
2032           gtk_menu_stop_scrolling (menu);
2033           menu->scroll_step = -MENU_SCROLL_STEP;
2034           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2035                                             gtk_menu_scroll_timeout,
2036                                             menu);
2037         }
2038       else if (!enter && !in_arrow && menu->upper_arrow_prelight)
2039         {
2040           menu->upper_arrow_prelight = FALSE;
2041           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2042           
2043           gtk_menu_stop_scrolling (menu);
2044         }
2045     }
2046   
2047   if (menu->lower_arrow_visible && !menu->tearoff_active)
2048     {
2049       rect.x = 0;
2050       rect.y = height - border - MENU_SCROLL_ARROW_HEIGHT;
2051       rect.width = width;
2052       rect.height = MENU_SCROLL_ARROW_HEIGHT + border;
2053
2054       in_arrow = FALSE;
2055       if ((x >= rect.x) && (x < rect.x + rect.width) &&
2056           (y >= rect.y) && (y < rect.y + rect.height))
2057         {
2058           in_arrow = TRUE;
2059           scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
2060         }
2061
2062       if (enter && in_arrow &&
2063           (!menu->lower_arrow_prelight || menu->scroll_fast != scroll_fast))
2064         {
2065           menu->lower_arrow_prelight = TRUE;
2066           menu->scroll_fast = scroll_fast;
2067           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2068
2069           /* Deselect the active item so that any submenus are poped down */
2070           gtk_menu_shell_deselect (menu_shell);
2071
2072           gtk_menu_stop_scrolling (menu);
2073           menu->scroll_step = MENU_SCROLL_STEP;
2074           menu->timeout_id = g_timeout_add ((scroll_fast) ? MENU_SCROLL_TIMEOUT2 : MENU_SCROLL_TIMEOUT1,
2075                                             gtk_menu_scroll_timeout,
2076                                             menu);
2077         }
2078       else if (!enter && !in_arrow && menu->lower_arrow_prelight)
2079         {
2080           menu->lower_arrow_prelight = FALSE;
2081           gdk_window_invalidate_rect (GTK_WIDGET (menu)->window, &rect, FALSE);
2082           
2083           gtk_menu_stop_scrolling (menu);
2084         }
2085     }
2086 }
2087
2088 static gboolean
2089 gtk_menu_enter_notify (GtkWidget        *widget,
2090                        GdkEventCrossing *event)
2091 {
2092   GtkWidget *menu_item;
2093
2094   if (widget && GTK_IS_MENU (widget))
2095     gtk_menu_handle_scrolling (GTK_MENU (widget), TRUE);
2096       
2097   /* If this is a faked enter (see gtk_menu_motion_notify), 'widget'
2098    * will not correspond to the event widget's parent.  Check to see
2099    * if we are in the parent's navigation region.
2100    */
2101   menu_item = gtk_get_event_widget ((GdkEvent*) event);
2102   if (menu_item && GTK_IS_MENU_ITEM (menu_item) && GTK_IS_MENU (menu_item->parent) &&
2103       gtk_menu_navigating_submenu (GTK_MENU (menu_item->parent), event->x_root, event->y_root))
2104     return TRUE;
2105
2106   return GTK_WIDGET_CLASS (parent_class)->enter_notify_event (widget, event); 
2107 }
2108
2109 static gboolean
2110 gtk_menu_leave_notify (GtkWidget        *widget,
2111                        GdkEventCrossing *event)
2112 {
2113   GtkMenuShell *menu_shell;
2114   GtkMenu *menu;
2115   GtkMenuItem *menu_item;
2116   GtkWidget *event_widget;
2117
2118   menu = GTK_MENU (widget);
2119   menu_shell = GTK_MENU_SHELL (widget); 
2120   
2121   if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
2122     return TRUE; 
2123
2124   gtk_menu_handle_scrolling (menu, FALSE);
2125   
2126   event_widget = gtk_get_event_widget ((GdkEvent*) event);
2127   
2128   if (!event_widget || !GTK_IS_MENU_ITEM (event_widget))
2129     return TRUE;
2130   
2131   menu_item = GTK_MENU_ITEM (event_widget); 
2132   
2133   /* Here we check to see if we're leaving an active menu item with a submenu, 
2134    * in which case we enter submenu navigation mode. 
2135    */
2136   if (menu_shell->active_menu_item != NULL
2137       && menu_item->submenu != NULL
2138       && menu_item->submenu_placement == GTK_LEFT_RIGHT)
2139     {
2140       if (GTK_MENU_SHELL (menu_item->submenu)->active)
2141         {
2142           gtk_menu_set_submenu_navigation_region (menu, menu_item, event);
2143           return TRUE;
2144         }
2145     }
2146   
2147   return GTK_WIDGET_CLASS (parent_class)->leave_notify_event (widget, event); 
2148 }
2149
2150 static void 
2151 gtk_menu_stop_navigating_submenu (GtkMenu *menu)
2152 {
2153   if (menu->navigation_region) 
2154     {
2155       gdk_region_destroy (menu->navigation_region);
2156       menu->navigation_region = NULL;
2157     }
2158   
2159   if (menu->navigation_timeout)
2160     {
2161       gtk_timeout_remove (menu->navigation_timeout);
2162       menu->navigation_timeout = 0;
2163     }
2164 }
2165
2166 /* When the timeout is elapsed, the navigation region is destroyed
2167  * and the menuitem under the pointer (if any) is selected.
2168  */
2169 static gboolean
2170 gtk_menu_stop_navigating_submenu_cb (gpointer user_data)
2171 {
2172   GtkMenu *menu = user_data;
2173   GdkWindow *child_window;
2174
2175   GDK_THREADS_ENTER ();
2176
2177   gtk_menu_stop_navigating_submenu (menu);
2178   
2179   if (GTK_WIDGET_REALIZED (menu))
2180     {
2181       child_window = gdk_window_get_pointer (menu->bin_window, NULL, NULL, NULL);
2182
2183       if (child_window)
2184         {
2185           GdkEventCrossing send_event;
2186
2187           memset (&send_event, 0, sizeof (send_event));
2188           send_event.window = child_window;
2189           send_event.type = GDK_ENTER_NOTIFY;
2190           send_event.time = GDK_CURRENT_TIME; /* Bogus */
2191           send_event.send_event = TRUE;
2192
2193           GTK_WIDGET_CLASS (parent_class)->enter_notify_event (GTK_WIDGET (menu), &send_event); 
2194         }
2195     }
2196
2197   GDK_THREADS_LEAVE ();
2198
2199   return FALSE; 
2200 }
2201
2202 static gboolean
2203 gtk_menu_navigating_submenu (GtkMenu *menu,
2204                              gint     event_x,
2205                              gint     event_y)
2206 {
2207   if (menu->navigation_region)
2208     {
2209       if (gdk_region_point_in (menu->navigation_region, event_x, event_y))
2210         return TRUE;
2211       else
2212         {
2213           gtk_menu_stop_navigating_submenu (menu);
2214           return FALSE;
2215         }
2216     }
2217   return FALSE;
2218 }
2219
2220 static void
2221 gtk_menu_set_submenu_navigation_region (GtkMenu          *menu,
2222                                         GtkMenuItem      *menu_item,
2223                                         GdkEventCrossing *event)
2224 {
2225   gint submenu_left = 0;
2226   gint submenu_right = 0;
2227   gint submenu_top = 0;
2228   gint submenu_bottom = 0;
2229   gint width = 0;
2230   gint height = 0;
2231   GdkPoint point[3];
2232   GtkWidget *event_widget;
2233
2234   g_return_if_fail (menu_item->submenu != NULL);
2235   g_return_if_fail (event != NULL);
2236   
2237   event_widget = gtk_get_event_widget ((GdkEvent*) event);
2238   
2239   gdk_window_get_origin (menu_item->submenu->window, &submenu_left, &submenu_top);
2240   gdk_window_get_size (menu_item->submenu->window, &width, &height);
2241   submenu_right = submenu_left + width;
2242   submenu_bottom = submenu_top + height;
2243   
2244   gdk_window_get_size (event_widget->window, &width, &height);
2245   
2246   if (event->x >= 0 && event->x < width)
2247     {
2248       /* Set navigation region */
2249       /* We fudge/give a little padding in case the user
2250        * ``misses the vertex'' of the triangle/is off by a pixel or two.
2251        */ 
2252       if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2253         point[0].x = event->x_root - SUBMENU_NAV_REGION_PADDING; 
2254       else                             
2255         point[0].x = event->x_root + SUBMENU_NAV_REGION_PADDING;  
2256       
2257       /* Exiting the top or bottom? */ 
2258       if (event->y < 0)
2259         { /* top */
2260           point[0].y = event->y_root + 1;
2261           point[1].y = submenu_top;
2262
2263           if (point[0].y <= point[1].y)
2264             return;
2265         }
2266       else
2267         { /* bottom */
2268           point[0].y = event->y_root;
2269           point[1].y = submenu_bottom;
2270
2271           if (point[0].y >= point[1].y)
2272             return;
2273         }
2274       
2275       /* Submenu is to the left or right? */ 
2276       if (menu_item->submenu_direction == GTK_DIRECTION_RIGHT)
2277         point[1].x = submenu_left;  /* right */
2278       else
2279         point[1].x = submenu_right; /* left */
2280       
2281       point[2].x = point[1].x;
2282       point[2].y = point[0].y;
2283
2284       gtk_menu_stop_navigating_submenu (menu);
2285       
2286       menu->navigation_region = gdk_region_polygon (point, 3, GDK_WINDING_RULE);
2287
2288       menu->navigation_timeout = gtk_timeout_add (SUBMENU_NAV_HYSTERESIS_TIMEOUT, 
2289                                                   gtk_menu_stop_navigating_submenu_cb, menu);
2290     }
2291 }
2292
2293 static void
2294 gtk_menu_deactivate (GtkMenuShell *menu_shell)
2295 {
2296   GtkWidget *parent;
2297   
2298   g_return_if_fail (GTK_IS_MENU (menu_shell));
2299   
2300   parent = menu_shell->parent_menu_shell;
2301   
2302   menu_shell->activate_time = 0;
2303   gtk_menu_popdown (GTK_MENU (menu_shell));
2304   
2305   if (parent)
2306     gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
2307 }
2308
2309 static void
2310 gtk_menu_position (GtkMenu *menu)
2311 {
2312   GtkWidget *widget;
2313   GtkRequisition requisition;
2314   gint x, y;
2315   gint scroll_offset;
2316   gint menu_height;
2317   gboolean push_in;
2318   GdkScreen *screen;
2319   GdkRectangle monitor;
2320   gint monitor_num;
2321
2322   g_return_if_fail (GTK_IS_MENU (menu));
2323
2324   widget = GTK_WIDGET (menu);
2325
2326   gdk_window_get_pointer (gtk_widget_get_root_window (widget),
2327                           &x, &y, NULL);
2328
2329   screen = gtk_widget_get_screen (widget);
2330   monitor_num = gdk_screen_get_monitor_at_point (screen, x, y);
2331   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
2332
2333   /* We need the requisition to figure out the right place to
2334    * popup the menu. In fact, we always need to ask here, since
2335    * if a size_request was queued while we weren't popped up,
2336    * the requisition won't have been recomputed yet.
2337    */
2338   gtk_widget_size_request (widget, &requisition);
2339
2340   push_in = FALSE;
2341   
2342   if (menu->position_func)
2343     (* menu->position_func) (menu, &x, &y, &push_in, menu->position_func_data);
2344   else
2345     {
2346       x = CLAMP (x - 2, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2347       y = CLAMP (y - 2, monitor.y, MAX (monitor.y, monitor.y + monitor.height - requisition.height));      
2348     }
2349
2350   scroll_offset = 0;
2351
2352   if (push_in)
2353     {
2354       menu_height = GTK_WIDGET (menu)->requisition.height;
2355
2356       if (y + menu_height > monitor.y + monitor.height)
2357         {
2358           scroll_offset -= y + menu_height - (monitor.y + monitor.height);
2359           y = (monitor.y + monitor.height) - menu_height;
2360         }
2361   
2362       if (y < monitor.y)
2363         {
2364           scroll_offset -= y;
2365           y = monitor.y;
2366         }
2367     }
2368
2369   /* FIXME: should this be done in the various position_funcs ? */
2370   x = CLAMP (x, monitor.x, MAX (monitor.x, monitor.x + monitor.width - requisition.width));
2371  
2372   if (y + requisition.height > monitor.y + monitor.height)
2373     requisition.height = (monitor.y + monitor.height) - y;
2374   
2375   if (y < monitor.y)
2376     {
2377       scroll_offset -= y;
2378       requisition.height -= -y;
2379       y = monitor.y;
2380     }
2381
2382   if (scroll_offset > 0)
2383     scroll_offset += MENU_SCROLL_ARROW_HEIGHT;
2384   
2385   gtk_window_move (GTK_WINDOW (GTK_MENU_SHELL (menu)->active ? menu->toplevel : menu->tearoff_window), 
2386                    x, y);
2387   gtk_widget_set_usize (GTK_MENU_SHELL (menu)->active ?
2388                         menu->toplevel : menu->tearoff_hbox,
2389                         -1, requisition.height);
2390   menu->scroll_offset = scroll_offset;
2391 }
2392
2393 static void
2394 gtk_menu_stop_scrolling (GtkMenu *menu)
2395 {
2396   if (menu->timeout_id)
2397     {
2398       g_source_remove (menu->timeout_id);
2399       menu->timeout_id = 0;
2400     }
2401 }
2402
2403
2404 static void
2405 gtk_menu_scroll_to (GtkMenu *menu,
2406                     gint    offset)
2407 {
2408   GtkWidget *widget;
2409   gint x, y;
2410   gint view_width, view_height;
2411   gint border_width;
2412   gboolean last_visible;
2413   gint menu_height;
2414
2415   widget = GTK_WIDGET (menu);
2416
2417   if (menu->tearoff_active &&
2418       menu->tearoff_adjustment &&
2419       (menu->tearoff_adjustment->value != offset))
2420     {
2421       menu->tearoff_adjustment->value = offset;
2422       gtk_adjustment_value_changed (menu->tearoff_adjustment);
2423     }
2424   
2425   /* Scroll the menu: */
2426   gdk_window_move (menu->bin_window, 0, -offset);
2427
2428   /* Move/resize the viewport according to arrows: */
2429   gdk_window_get_size (widget->window, &view_width, &view_height);
2430
2431   border_width = GTK_CONTAINER (menu)->border_width;
2432   view_width -= (border_width + widget->style->xthickness) * 2;
2433   view_height -= (border_width + widget->style->ythickness) * 2;
2434   menu_height = widget->requisition.height - (border_width + widget->style->ythickness) * 2;
2435
2436   x = border_width + widget->style->xthickness;
2437   y = border_width + widget->style->ythickness;
2438   
2439   if (!menu->tearoff_active)
2440     {
2441       last_visible = menu->upper_arrow_visible;
2442       menu->upper_arrow_visible = (offset > 0);
2443
2444       if (menu->upper_arrow_visible)
2445         view_height -= MENU_SCROLL_ARROW_HEIGHT;
2446       
2447       if ( (last_visible != menu->upper_arrow_visible) &&
2448            !menu->upper_arrow_visible)
2449         {
2450           menu->upper_arrow_prelight = FALSE;
2451           
2452           /* If we hid the upper arrow, possibly remove timeout */
2453           if (menu->scroll_step < 0)
2454             gtk_menu_stop_scrolling (menu);
2455         }
2456       
2457       last_visible = menu->lower_arrow_visible;
2458       menu->lower_arrow_visible = (view_height + offset < menu_height);
2459       
2460       if (menu->lower_arrow_visible)
2461         view_height -= MENU_SCROLL_ARROW_HEIGHT;
2462       
2463       if ( (last_visible != menu->lower_arrow_visible) &&
2464            !menu->lower_arrow_visible)
2465         {
2466           menu->lower_arrow_prelight = FALSE;
2467           
2468           /* If we hid the lower arrow, possibly remove timeout */
2469           if (menu->scroll_step > 0)
2470             gtk_menu_stop_scrolling (menu);
2471         }
2472       
2473       if (menu->upper_arrow_visible)
2474         y += MENU_SCROLL_ARROW_HEIGHT;
2475     }
2476   
2477   gdk_window_move_resize (menu->view_window,
2478                           x,
2479                           y,
2480                           view_width,
2481                           view_height);
2482
2483   menu->scroll_offset = offset;
2484 }
2485
2486 static void
2487 gtk_menu_scroll_item_visible (GtkMenuShell    *menu_shell,
2488                               GtkWidget       *menu_item)
2489 {
2490   GtkMenu *menu;
2491   GtkWidget *child;
2492   GList *children;
2493   GtkRequisition child_requisition;
2494   gint child_offset, child_height;
2495   gint width, height;
2496   gint y;
2497   gint arrow_height;
2498   gboolean last_child = 0;
2499   
2500   menu = GTK_MENU (menu_shell);
2501
2502   /* We need to check if the selected item fully visible.
2503    * If not we need to scroll the menu so that it becomes fully
2504    * visible.
2505    */
2506
2507   child = NULL;
2508   child_offset = 0;
2509   child_height = 0;
2510   children = menu_shell->children;
2511   while (children)
2512     {
2513       child = children->data;
2514       children = children->next;
2515       
2516       if (GTK_WIDGET_VISIBLE (child))
2517         {
2518           gtk_widget_size_request (child, &child_requisition);
2519           child_offset += child_height;
2520           child_height = child_requisition.height;
2521         }
2522       
2523       if (child == menu_item)
2524         {
2525           last_child = (children == NULL);
2526           break;
2527         }
2528     }
2529
2530   if (child == menu_item)
2531     {
2532       y = menu->scroll_offset;
2533       gdk_window_get_size (GTK_WIDGET (menu)->window, &width, &height);
2534
2535       height -= 2*GTK_CONTAINER (menu)->border_width + 2*GTK_WIDGET (menu)->style->ythickness;
2536       
2537       if (child_offset + child_height <= y)
2538         {
2539           /* Ignore the enter event we might get if the pointer is on the menu
2540            */
2541           menu_shell->ignore_enter = TRUE;
2542           gtk_menu_scroll_to (menu, child_offset);
2543         }
2544       else
2545         {
2546           arrow_height = 0;
2547           if (menu->upper_arrow_visible && !menu->tearoff_active)
2548             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2549           if (menu->lower_arrow_visible && !menu->tearoff_active)
2550             arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2551           
2552           if (child_offset >= y + height - arrow_height)
2553             {
2554               arrow_height = 0;
2555               if (!last_child && !menu->tearoff_active)
2556                 arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2557               
2558               y = child_offset + child_height - height + arrow_height;
2559               if ((y > 0) && !menu->tearoff_active)
2560                 {
2561                   /* Need upper arrow */
2562                   arrow_height += MENU_SCROLL_ARROW_HEIGHT;
2563                   y = child_offset + child_height - height + arrow_height;
2564                 }
2565               /* Ignore the enter event we might get if the pointer is on the menu
2566                */
2567               menu_shell->ignore_enter = TRUE;
2568               gtk_menu_scroll_to (menu, y);
2569             }
2570         }    
2571       
2572     }
2573 }
2574
2575 static void
2576 gtk_menu_select_item (GtkMenuShell  *menu_shell,
2577                       GtkWidget     *menu_item)
2578 {
2579   GtkMenu *menu = GTK_MENU (menu_shell);
2580
2581   if (GTK_WIDGET_REALIZED (GTK_WIDGET (menu)))
2582     gtk_menu_scroll_item_visible (menu_shell, menu_item);
2583
2584   GTK_MENU_SHELL_CLASS (parent_class)->select_item (menu_shell, menu_item);
2585 }
2586
2587
2588 /* Reparent the menu, taking care of the refcounting
2589  *
2590  * If unrealize is true we force a unrealize while reparenting the parent.
2591  * This can help eliminate flicker in some cases.
2592  *
2593  * What happens is that when the menu is unrealized and then re-realized,
2594  * the allocations are as follows:
2595  *
2596  *  parent - 1x1 at (0,0) 
2597  *  child1 - 100x20 at (0,0)
2598  *  child2 - 100x20 at (0,20)
2599  *  child3 - 100x20 at (0,40)
2600  *
2601  * That is, the parent is small but the children are full sized. Then,
2602  * when the queued_resize gets processed, the parent gets resized to
2603  * full size. 
2604  *
2605  * But in order to eliminate flicker when scrolling, gdkgeometry-x11.c
2606  * contains the following logic:
2607  * 
2608  * - if a move or resize operation on a window would change the clip 
2609  *   region on the children, then before the window is resized
2610  *   the background for children is temporarily set to None, the
2611  *   move/resize done, and the background for the children restored.
2612  *
2613  * So, at the point where the parent is resized to final size, the
2614  * background for the children is temporarily None, and thus they
2615  * are not cleared to the background color and the previous background
2616  * (the image of the menu) is left in place.
2617  */
2618 static void 
2619 gtk_menu_reparent (GtkMenu      *menu, 
2620                    GtkWidget    *new_parent, 
2621                    gboolean      unrealize)
2622 {
2623   GtkObject *object = GTK_OBJECT (menu);
2624   GtkWidget *widget = GTK_WIDGET (menu);
2625   gboolean was_floating = GTK_OBJECT_FLOATING (object);
2626
2627   gtk_object_ref (object);
2628   gtk_object_sink (object);
2629
2630   if (unrealize)
2631     {
2632       gtk_object_ref (object);
2633       gtk_container_remove (GTK_CONTAINER (widget->parent), widget);
2634       gtk_container_add (GTK_CONTAINER (new_parent), widget);
2635       gtk_object_unref (object);
2636     }
2637   else
2638     gtk_widget_reparent (GTK_WIDGET (menu), new_parent);
2639   
2640   if (was_floating)
2641     GTK_OBJECT_SET_FLAGS (object, GTK_FLOATING);
2642   else
2643     gtk_object_unref (object);
2644 }
2645
2646 static void
2647 gtk_menu_show_all (GtkWidget *widget)
2648 {
2649   g_return_if_fail (GTK_IS_MENU (widget));
2650
2651   /* Show children, but not self. */
2652   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_show_all, NULL);
2653 }
2654
2655
2656 static void
2657 gtk_menu_hide_all (GtkWidget *widget)
2658 {
2659   g_return_if_fail (GTK_IS_MENU (widget));
2660
2661   /* Hide children, but not self. */
2662   gtk_container_foreach (GTK_CONTAINER (widget), (GtkCallback) gtk_widget_hide_all, NULL);
2663 }
2664
2665 /**
2666  * gtk_menu_set_screen:
2667  * @menu: a #GtkMenu.
2668  * @screen: a #GtkScreen.
2669  *
2670  * Sets the #GtkScreen on which the GtkMenu will be displayed.
2671  * This function can only be called before @menu is realized.
2672  **/
2673 void
2674 gtk_menu_set_screen (GtkMenu *menu, 
2675                      GdkScreen *screen)
2676 {
2677   g_return_if_fail (GTK_IS_MENU (menu));
2678   gtk_window_set_screen (GTK_WINDOW (menu->toplevel), 
2679                          screen);
2680   g_object_set_data (G_OBJECT (menu), "gtk-menu-explicit-screen", screen);
2681 }
2682