changeset 4867:02a0c46f9a52

7124430: [macosx] LWCToolkit.grab() and LWCToolkit.ungrab() events are not implemented yet Reviewed-by: art, anthony
author ant
date Wed, 18 Jan 2012 18:09:04 +0300
parents 534f984e18bc
children d0a56328cb69
files src/macosx/classes/sun/lwawt/LWToolkit.java src/macosx/classes/sun/lwawt/LWWindowPeer.java src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java src/macosx/native/sun/awt/AWTWindow.m test/java/awt/Window/Grab/GrabTest.java
diffstat 6 files changed, 307 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/macosx/classes/sun/lwawt/LWToolkit.java	Tue Jan 17 17:26:50 2012 +0300
+++ b/src/macosx/classes/sun/lwawt/LWToolkit.java	Wed Jan 18 18:09:04 2012 +0300
@@ -539,4 +539,18 @@
     public boolean needUpdateWindow() {
         return true;
     }
+
+    @Override
+    public void grab(Window w) {
+        if (w.getPeer() != null) {
+            ((LWWindowPeer)w.getPeer()).grab();
+        }
+    }
+
+    @Override
+    public void ungrab(Window w) {
+        if (w.getPeer() != null) {
+            ((LWWindowPeer)w.getPeer()).ungrab();
+        }
+    }
 }
--- a/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Tue Jan 17 17:26:50 2012 +0300
+++ b/src/macosx/classes/sun/lwawt/LWWindowPeer.java	Wed Jan 18 18:09:04 2012 +0300
@@ -112,6 +112,8 @@
 
     private volatile boolean isOpaque = true;
 
+    private static LWWindowPeer grabbingWindow;
+
     /**
      * Current modal blocker or null.
      *
@@ -243,6 +245,9 @@
         if (oldData != null) {
             oldData.invalidate();
         }
+        if (isGrabbing()) {
+            ungrab();
+        }
         platformWindow.dispose();
         super.disposeImpl();
     }
@@ -628,6 +633,16 @@
         changeFocusedWindow(activation, false);
     }
 
+    // MouseDown in non-client area
+    public void notifyNCMouseDown() {
+        // Ungrab except for a click on a Dialog with the grabbing owner
+        if (grabbingWindow != null &&
+            grabbingWindow.getTarget() != getOwnerFrameDialog(this))
+        {
+            grabbingWindow.ungrab();
+        }
+    }
+
     // ---- EVENTS ---- //
 
     /*
@@ -716,6 +731,13 @@
             // based on initial targetPeer value and only then recalculate targetPeer
             // for MOUSE_DRAGGED/RELEASED events
             if (id == MouseEvent.MOUSE_PRESSED) {
+
+                // Ungrab only if this window is not an owned window of the grabbing one.
+                if (!isGrabbing() && grabbingWindow != null &&
+                    grabbingWindow.getTarget() != getOwnerFrameDialog(this))
+                {
+                    grabbingWindow.ungrab();
+                }
                 changeFocusedWindow(true, false);
 
                 if (otherButtonsPressed == 0) {
@@ -1089,7 +1111,17 @@
             getInstance(getAppContext());
 
         Window oppositeWindow = becomesFocused ? manager.getCurrentFocusedWindow() : null;
-        // TODO: ungrab
+
+        // Note, the method is not called:
+        // - when the opposite (gaining focus) window is an owned/owner window.
+        // - for a simple window in any case.
+        if (!becomesFocused &&
+            (isGrabbing() || getOwnerFrameDialog(grabbingWindow) == getTarget()))
+        {
+            // ungrab a simple window if its owner looses activation.
+            grabbingWindow.ungrab();
+        }
+
         manager.setFocusedWindow(becomesFocused ? LWWindowPeer.this : null);
 
         int eventID = becomesFocused ? WindowEvent.WINDOW_GAINED_FOCUS : WindowEvent.WINDOW_LOST_FOCUS;
@@ -1099,6 +1131,14 @@
         postEvent(windowEvent);
     }
 
+    private static Window getOwnerFrameDialog(LWWindowPeer peer) {
+        Window owner = (peer != null ? peer.getTarget().getOwner() : null);
+        while (owner != null && !(owner instanceof Frame || owner instanceof Dialog)) {
+            owner = owner.getOwner();
+        }
+        return owner;
+    }
+
     /**
      * Returns the foremost modal blocker of this window, or null.
      */
@@ -1126,4 +1166,22 @@
     public long getLayerPtr() {
         return getPlatformWindow().getLayerPtr();
     }
+
+    void grab() {
+        if (grabbingWindow != null && !isGrabbing()) {
+            grabbingWindow.ungrab();
+        }
+        grabbingWindow = this;
+    }
+
+    void ungrab() {
+        if (isGrabbing()) {
+            grabbingWindow = null;
+            postEvent(new UngrabEvent(getTarget()));
+        }
+    }
+
+    private boolean isGrabbing() {
+        return this == grabbingWindow;
+    }
 }
--- a/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Tue Jan 17 17:26:50 2012 +0300
+++ b/src/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java	Wed Jan 18 18:09:04 2012 +0300
@@ -793,6 +793,10 @@
         peer.notifyZoom(isZoomed);
     }
 
+    private void deliverNCMouseDown() {
+        peer.notifyNCMouseDown();
+    }
+
     /*
      * Our focus model is synthetic and only non-simple window
      * may become natively focusable window.
--- a/src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Tue Jan 17 17:26:50 2012 +0300
+++ b/src/macosx/classes/sun/lwawt/macosx/LWCToolkit.java	Wed Jan 18 18:09:04 2012 +0300
@@ -359,19 +359,6 @@
 
     }
 
-
-    @Override
-    public void grab(Window w) {
-        // TODO Auto-generated method stub
-
-    }
-
-    @Override
-    public void ungrab(Window w) {
-        // TODO Auto-generated method stub
-
-    }
-
     @Override
     public RobotPeer createRobot(Robot target, GraphicsDevice screen) {
         return new CRobot(target, (CGraphicsDevice)screen);
--- a/src/macosx/native/sun/awt/AWTWindow.m	Tue Jan 17 17:26:50 2012 +0300
+++ b/src/macosx/native/sun/awt/AWTWindow.m	Wed Jan 18 18:09:04 2012 +0300
@@ -495,6 +495,25 @@
 
         [self _notifyFullScreenOp:com_apple_eawt_FullScreenHandler_FULLSCREEN_DID_EXIT withEnv:env];
 }
+
+- (void)sendEvent:(NSEvent *)event {
+        if ([event type] == NSLeftMouseDown || [event type] == NSRightMouseDown || [event type] == NSOtherMouseDown) {
+			
+            NSPoint p = [NSEvent mouseLocation];
+            NSRect frame = [self frame];
+            NSRect contentRect = [self contentRectForFrameRect:frame];
+			
+            // Check if the click happened in the non-client area (title bar) 
+            if (p.y >= (frame.origin.y + contentRect.size.height)) {				
+                JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
+                jobject platformWindow = [self.javaPlatformWindow jObjectWithEnv:env];
+                // Currently, no need to deliver the whole NSEvent.
+                static JNF_MEMBER_CACHE(jm_deliverNCMouseDown, jc_CPlatformWindow, "deliverNCMouseDown", "()V");
+                JNFCallVoidMethod(env, platformWindow, jm_deliverNCMouseDown);
+            }
+        }
+        [super sendEvent:event];
+}
 @end // AWTWindow
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/awt/Window/Grab/GrabTest.java	Wed Jan 18 18:09:04 2012 +0300
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+  @test
+  @bug 7124430
+  @summary Tests that SunToolkit.grab API works
+  @author anton.tarasov@oracle.com: area=awt.toolkit
+  @library ../../regtesthelpers
+  @build Util
+  @run main GrabTest
+*/
+
+import java.awt.*;
+import java.awt.event.*;
+import test.java.awt.regtesthelpers.Util;
+
+public class GrabTest {
+    private static Frame f;
+    private static Frame f1;
+    private static Window w;
+    private static Button b;
+
+    private static Robot robot;
+    private static sun.awt.SunToolkit tk;
+
+    static volatile boolean ungrabbed;
+    static volatile boolean buttonPressed;
+    static volatile boolean windowPressed;
+    static volatile boolean framePressed;
+
+    static volatile boolean passed = true;
+
+    public static void main(String[] args) {
+
+        Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
+                public void eventDispatched(AWTEvent e) {
+                    System.out.println(e);
+                    if (e instanceof sun.awt.UngrabEvent) {
+                        ungrabbed = true;
+                    }
+                }
+            }, sun.awt.SunToolkit.GRAB_EVENT_MASK);
+
+        f = new Frame("Frame");
+        f.setSize(200, 200);
+        f.addMouseListener(new MouseAdapter() {
+                public void mousePressed(MouseEvent e) {
+                    System.out.println(e);
+                    framePressed = true;
+                }
+            });
+
+        f1 = new Frame("OtherFrame");
+        f1.setBounds(600, 100, 200, 200);
+
+        w = new Window(f);
+        w.setLayout(new FlowLayout());
+        b = new Button("Press");
+        b.addActionListener(new ActionListener() {
+                public void actionPerformed(ActionEvent e) {
+                    System.out.println(e);
+                    buttonPressed = true;
+                }
+            });
+        w.add(b);
+        w.setBounds(300, 100, 200, 200);
+        w.setBackground(Color.blue);
+        w.addMouseListener(new MouseAdapter() {
+                public void mousePressed(MouseEvent e) {
+                    System.out.println(e);
+                    windowPressed = true;
+                }
+            });
+
+        f.setVisible(true);
+        w.setVisible(true);
+
+        tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit();
+
+        try {
+            robot = new Robot();
+        } catch (AWTException ex) {
+            throw new RuntimeException(ex);
+        }
+
+        Util.waitForIdle(robot);
+
+        test();
+    }
+
+    public static void test() {
+        tk.grab(w);
+
+        // 1. Check that button press doesn't cause ungrab
+        Util.clickOnComp(b, robot);
+        Util.waitForIdle(robot);
+        checkAndThrow(buttonPressed, "Error: Button can not be pressed");
+        if (ungrabbed) {
+            passed = false;
+            tk.grab(w);
+            System.err.println("Failure: [1] Press inside of Window (on Button) caused ungrab");
+        }
+
+        // 2. Check that press on the window itself doesn't cause ungrab
+        Util.clickOnComp(w, robot);
+        Util.waitForIdle(robot);
+        checkAndThrow(windowPressed, "Error: Window can't be pressed");
+        if (ungrabbed) {
+            passed = false;
+            tk.grab(w);
+            System.err.println("Failure: [2] Press inside of Window caused ungrab");
+        }
+
+        // 3. Check that press on the frame causes ungrab, event must be dispatched
+        Util.clickOnComp(f, robot);
+        Util.waitForIdle(robot);
+        checkAndThrow(framePressed, "Error: Frame can't be pressed");
+        if (!ungrabbed) {
+            passed = false;
+            System.err.println("Failure: [3] Press inside of Frame didn't cause ungrab");
+        }
+        ungrabbed = false;
+        tk.grab(w);
+
+        // 4. Check that press on the frame's title causes ungrab
+        Util.clickOnTitle(f, robot);
+        Util.waitForIdle(robot);
+        if (!ungrabbed) {
+            passed = false;
+            System.err.println("Failure: [4] Press inside of Frame's title didn't cause ungrab");
+        }
+        ungrabbed = false;
+        tk.grab(w);
+
+
+        // 5. Check that press on the other frame's title causes ungrab
+        f1.setVisible(true);
+        Util.waitForIdle(robot);
+        Util.clickOnTitle(f1, robot);
+        if (!ungrabbed) {
+            passed = false;
+            System.err.println("Failure: [5] Press inside of other Frame's title didn't cause ungrab");
+        }
+        f.requestFocus(); // restore focus
+        Util.waitForIdle(robot);
+        if (!f.hasFocus()) {
+            System.err.println("Error: Frame can't be focused");
+        }
+        ungrabbed = false;
+        tk.grab(w);
+
+
+        // 6. Check that press on the outside area causes ungrab
+        Point loc = f.getLocationOnScreen();
+        robot.mouseMove(loc.x + 100, loc.y + f.getSize().height + 300);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+        robot.delay(50);
+        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+        Util.waitForIdle(robot);
+        if (!ungrabbed) {
+            passed = false;
+            System.err.println("Failure: [6] Press on the outside area didn't cause ungrab");
+        }
+        ungrabbed = false;
+        tk.grab(w);
+
+
+        // 7. Check that disposing the window causes ungrab
+        w.dispose();
+        Util.waitForIdle(robot);
+        if (!ungrabbed) {
+            passed = false;
+            System.err.println("Failure: [7] Window disposal didn't cause ungrab");
+        }
+
+        if (passed) {
+            System.out.println("Test passed.");
+        } else {
+            throw new RuntimeException("Test failed.");
+        }
+    }
+
+    public static void checkAndThrow(boolean condition, String msg) {
+        if (!condition) {
+            throw new RuntimeException(msg);
+        }
+    }
+}