Project: SWT
Bug 87363 Regression: MenuDetect no longer opens new menus
Reported: 2005-03-08 08:00:02 | Status: resolved fixed

Bug introduced Nov 5, 2004. Prior to change Control.showMenu did this: boolean showMenu (int x, int y) { Event event = new Event (); event.x = x; event.y = y; sendEvent (SWT.MenuDetect, event); if (!event.doit) return true; if (menu != null && !menu.isDisposed ()) { if (x != event.x || y != event.y) { menu.setLocation (event.x, event.y); } menu.setVisible (true); return true; } return false; } If a menu was created during the MenuDetect listener notification, the menu field was updated and the menu was set visible. After Nov 5, 2004 the method was moved to Widget and now looks like this: boolean showMenu (Menu menu, int x, int y) { Event event = new Event (); event.x = x; event.y = y; sendEvent (SWT.MenuDetect, event); if (!event.doit) return true; if (menu != null && !menu.isDisposed ()) { if (x != event.x || y != event.y) { menu.setLocation (event.x, event.y); } menu.setVisible (true); return true; } return false; } Since menu is just a local variable, it is not updated when a menu is created in the MenuDetect event. The following example demonstrates the problem: public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.addListener(SWT.MenuDetect, new Listener() { int count = 0; public void handleEvent(Event event) { Menu menu = shell.getMenu(); if (menu != null) menu.dispose(); menu = new Menu(shell, SWT.POP_UP); MenuItem item = new MenuItem(menu, SWT.PUSH); item.setText("Menu " + count++); shell.setMenu(menu); } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }

Files changed in fix:
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Table.java bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Text.java bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Tree.java bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Widget.java