BarMenu improvement, take 2

Pavel Roskin pavel_roskin at geocities.com
Tue Jun 1 07:03:24 PDT 1999


Hello!

Current implementation of java.awt.BarMenu has following defects:

1) If the menu is active, clicking on the menubar makes the pulldown menu
disappear, but is appears again as soon as the mouse is moved.

2) If the menu is active, and the mouse is moved away from the menu over
the empty part of the menubar, there are no signs that the menu is still
active. However, it is.

3) If the menu is active, and there is an menubar item with no popup menu
(e.g. Help in WidgetsDemo), and the mouse is moved from this element
outside the menubar, clicking on other controls doesn't desactivate the
menu.

The attached patch contains following changes:

1) The only way to fix the defect 3 is to open the popup menu even if
there are no items in it. I don't see how to implement the reasonable
behaviour otherwise. WindowAdapter works only if the popup menu is
created. mouseExited doesn't keep track of clicks outside the menubar.
Anyway, this should not affect "serious" applications. And this is how JDK
(in fact Motif) does it.

2) MouseMotionAdapter is registered only for the time when the menu is
activated. This should reduce CPU usage and simplify the handler.

3) MouseMotionAdapter is not allowed to deselect menus. This is intuitive
- check other toolkits, e.g. Gtk.

4) mousePressed deselects the menu if is was selected. Check other
toolkits.

5) java.util.Vector is not imported anymore since it is not used.

6) All System.out.println (commented out) removed.

What is not done:

1) HelpMenu is still not supported

2) Menus don't follow the mouse with the left button pressed. PopupWindow
grabs the focus and gets all the mouseDragged events. I'm affraid this is
not easy to fix.

Pavel Roskin
-------------- next part --------------
Index: libraries/javalib/java/awt/BarMenu.java
===================================================================
RCS file: /home/cvspublic/kaffe/libraries/javalib/java/awt/BarMenu.java,v
retrieving revision 1.6
diff -u -r1.6 BarMenu.java
--- BarMenu.java	1999/04/23 18:22:34	1.6
+++ BarMenu.java	1999/06/01 14:09:56
@@ -9,7 +9,6 @@
 import java.awt.event.WindowAdapter;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
-import java.util.Vector;
 
 /**
  * Copyright (c) 1998
@@ -28,6 +27,7 @@
 	Menu selection;
 	MenuBar mb;
 	PopupMenu current;
+	MouseMotionAdapter mma;
 	WindowAdapter wa;
 
 public BarMenu ( MenuBar mb) {
@@ -134,7 +134,7 @@
 }
 
 boolean openSelection () {
-	if ( (selection == null) || ((selection.getItemCount() == 0)) )
+	if (selection == null)
 		return false;
 	
 	disposeCurrent();
@@ -147,7 +147,6 @@
 	ShortcutHandler.addShortcut( s2, current.wnd, this);
 	current.wnd.addWindowListener( wa);
 
-//System.out.println( "linked: " + current.wnd);
 	return true;		
 }
 
@@ -205,16 +204,20 @@
 	ShortcutHandler.addShortcut( s2, this, this);
 	ShortcutHandler.addShortcut( s3, this, this);
 	
-	MouseMotionAdapter mma = new MouseMotionAdapter() {
+	mma = new MouseMotionAdapter() {
 		public void mouseMoved( MouseEvent evt) {
-			if ( (AWTEvent.keyTgt == evt.getSource()) || (current != null) )
-				selectMenu( menuAt( evt.getX() ));
+			Menu m = menuAt( evt.getX() );
+			if (m != null)
+				selectMenu( m);
 		}
 	};
 	MouseAdapter ma = new MouseAdapter() {
 		public void mousePressed( MouseEvent evt) {
 			requestFocus();
-			selectMenu( menuAt( evt.getX() ));
+			if (selection == null)
+				selectMenu( menuAt( evt.getX() ));
+			else
+				selectMenu( null);
 		}
 		public void mouseReleased( MouseEvent evt ) {
 			processSelection();
@@ -231,7 +234,6 @@
 		}
 	};
 	
-	addMouseMotionListener( mma);
 	addMouseListener( ma);
 }
 
@@ -251,6 +253,7 @@
 	Graphics g = getGraphics();
 
 	if ( m == null ) {
+		removeMouseMotionListener( mma);
 		disposeCurrent();
 		selection = null;
 		paintMenu( g, ms);
@@ -259,6 +262,7 @@
 		selection = m;
 		paintMenu( g, m);
 		openSelection();
+		addMouseMotionListener( mma);
 	}
 	else {
 		selection = m;
@@ -304,7 +308,6 @@
 	if ( (current != null) && (current.wnd != null ) ) {
 		ShortcutHandler.removeShortcuts( current.wnd);
 		current.wnd.removeWindowListener( wa);
-//System.out.println( "unlinked: " + current.wnd);
 	}
 }
 }


More information about the kaffe mailing list