1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.lwawt.macosx;
  27 
  28 import sun.lwawt.LWToolkit;
  29 import sun.lwawt.LWWindowPeer;
  30 import sun.lwawt.macosx.CocoaConstants;
  31 import sun.lwawt.macosx.event.NSEvent;
  32 
  33 import sun.awt.EmbeddedFrame;
  34 
  35 import java.awt.*;
  36 import java.awt.event.*;
  37 
  38 public class CEmbeddedFrame extends EmbeddedFrame {
  39 
  40     private CPlatformResponder responder;
  41     private static final Object classLock = new Object();
  42     private static volatile CEmbeddedFrame focusedWindow;
  43     private boolean parentWindowActive = true;
  44 
  45     public CEmbeddedFrame() {
  46         show();
  47     }
  48 
  49     public void addNotify() {
  50         if (getPeer() == null) {
  51             LWToolkit toolkit = (LWToolkit)Toolkit.getDefaultToolkit();
  52             LWWindowPeer peer = toolkit.createEmbeddedFrame(this);
  53             setPeer(peer);
  54             responder = new CPlatformResponder(peer, true);
  55         }
  56         super.addNotify();
  57     }
  58 
  59     public void registerAccelerator(AWTKeyStroke stroke) {}
  60 
  61     public void unregisterAccelerator(AWTKeyStroke stroke) {}
  62 
  63     protected long getLayerPtr() {
  64         LWWindowPeer peer = (LWWindowPeer)getPeer();
  65         return peer.getLayerPtr();
  66     }
  67 
  68     // -----------------------------------------------------------------------
  69     //                          SYNTHETIC EVENT DELIVERY
  70     // -----------------------------------------------------------------------
  71 
  72     public void handleMouseEvent(int eventType, int modifierFlags, double pluginX,
  73                                  double pluginY, int buttonNumber, int clickCount) {
  74         int x = (int)pluginX;
  75         int y = (int)pluginY;
  76         Point locationOnScreen = getLocationOnScreen();
  77         int screenX = locationOnScreen.x + x;
  78         int screenY = locationOnScreen.y + y;
  79 
  80         if (eventType == CocoaConstants.NPCocoaEventMouseEntered) {
  81             CCursorManager.nativeSetAllowsCursorSetInBackground(true);
  82         } else if (eventType == CocoaConstants.NPCocoaEventMouseExited) {
  83             CCursorManager.nativeSetAllowsCursorSetInBackground(false);
  84         }
  85 
  86         responder.handleMouseEvent(eventType, modifierFlags, buttonNumber,
  87                                    clickCount, x, y, screenX, screenY);
  88     }
  89 
  90     public void handleScrollEvent(double pluginX, double pluginY, int modifierFlags,
  91                                   double deltaX, double deltaY, double deltaZ) {
  92         int x = (int)pluginX;
  93         int y = (int)pluginY;
  94 
  95         responder.handleScrollEvent(x, y, modifierFlags, deltaX, deltaY);
  96     }
  97 
  98     public void handleKeyEvent(int eventType, int modifierFlags, String characters,
  99                                String charsIgnoringMods, boolean isRepeat, short keyCode,
 100                                boolean needsKeyTyped) {
 101         responder.handleKeyEvent(eventType, modifierFlags, charsIgnoringMods, keyCode, needsKeyTyped, isRepeat);
 102     }
 103 
 104     // REMIND: delete this method once 'deploy' changes for 7156194 is pushed
 105     public void handleKeyEvent(int eventType, int modifierFlags, String characters,
 106                                String charsIgnoringMods, boolean isRepeat, short keyCode) {
 107         handleKeyEvent(eventType, modifierFlags, characters, charsIgnoringMods, isRepeat, keyCode, true);
 108     }
 109 
 110     public void handleInputEvent(String text) {
 111         responder.handleInputEvent(text);
 112     }
 113 
 114     // handleFocusEvent is called when the applet becames focused/unfocused.
 115     // This method can be called from different threads.
 116     public void handleFocusEvent(boolean focused) {
 117         synchronized (classLock) {
 118             // In some cases an applet may not receive the focus lost event
 119             // from the parent window (see 8012330)
 120             focusedWindow = (focused) ? this
 121                     : ((focusedWindow == this) ? null : focusedWindow);
 122         }
 123         if (focusedWindow == this) {
 124             // see bug 8010925
 125             // we can't put this to handleWindowFocusEvent because
 126             // it won't be invoced if focuse is moved to a html element
 127             // on the same page.
 128             CClipboard clipboard = (CClipboard) Toolkit.getDefaultToolkit().getSystemClipboard();
 129             clipboard.checkPasteboard();
 130         }
 131         if (parentWindowActive) {
 132             responder.handleWindowFocusEvent(focused, null);
 133         }
 134     }
 135 
 136     /**
 137      * handleWindowFocusEvent is called for all applets, when the browser
 138      * becames active/inactive. This event should be filtered out for
 139      * non-focused applet. This method can be called from different threads.
 140      *
 141      * When the window is activated and had focus before the deactivation
 142      * calling this method triggers focus events in the following order:
 143      *  1. WINDOW_ACTIVATED for this EmbeddedFrame
 144      *  2. WINDOW_GAINED_FOCUS for this EmbeddedFrame
 145      *  3. FOCUS_GAINED for the most recent focus owner in this EmbeddedFrame
 146      *
 147      * The caller must not requestFocus on the EmbeddedFrame together with calling this method.
 148      *
 149      * @param parentWindowActive true if the window is activated, false otherwise
 150      */
 151     public void handleWindowFocusEvent(boolean parentWindowActive) {
 152         this.parentWindowActive = parentWindowActive;
 153         // ignore focus "lost" native request as it may mistakenly
 154         // deactivate active window (see 8001161)
 155         if (focusedWindow == this && parentWindowActive) {
 156             responder.handleWindowFocusEvent(parentWindowActive, null);
 157         }
 158     }
 159 
 160     public boolean isParentWindowActive() {
 161         return parentWindowActive;
 162     }
 163 }