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