1 /*
   2  * Copyright (c) 2011, 2014, 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 
  27 package sun.lwawt.macosx;
  28 
  29 import java.awt.AWTKeyStroke;
  30 import java.awt.Point;
  31 import java.awt.Toolkit;
  32 
  33 import sun.awt.EmbeddedFrame;
  34 import sun.lwawt.LWWindowPeer;
  35 
  36 @SuppressWarnings("serial") // JDK implementation class
  37 public class CEmbeddedFrame extends EmbeddedFrame {
  38 
  39     private CPlatformResponder responder;
  40     private static final Object classLock = new Object();
  41     private static volatile CEmbeddedFrame focusedWindow;
  42     private boolean parentWindowActive = true;
  43 
  44     public CEmbeddedFrame() {
  45         show();
  46     }
  47 
  48     public void addNotify() {
  49         if (getPeer() == null) {
  50             LWCToolkit toolkit = (LWCToolkit)Toolkit.getDefaultToolkit();
  51             LWWindowPeer peer = toolkit.createEmbeddedFrame(this);
  52             setPeer(peer);
  53             responder = new CPlatformResponder(peer, true);
  54         }
  55         super.addNotify();
  56     }
  57 
  58     public void registerAccelerator(AWTKeyStroke stroke) {}
  59 
  60     public void unregisterAccelerator(AWTKeyStroke stroke) {}
  61 
  62     protected long getLayerPtr() {
  63         LWWindowPeer peer = (LWWindowPeer)getPeer();
  64         return peer.getLayerPtr();
  65     }
  66 
  67     // -----------------------------------------------------------------------
  68     //                          SYNTHETIC EVENT DELIVERY
  69     // -----------------------------------------------------------------------
  70 
  71     public void handleMouseEvent(int eventType, int modifierFlags, double pluginX,
  72                                  double pluginY, int buttonNumber, int clickCount) {
  73         int x = (int)pluginX;
  74         int y = (int)pluginY;
  75         Point locationOnScreen = getLocationOnScreen();
  76         int screenX = locationOnScreen.x + x;
  77         int screenY = locationOnScreen.y + y;
  78 
  79         if (eventType == CocoaConstants.NPCocoaEventMouseEntered) {
  80             CCursorManager.nativeSetAllowsCursorSetInBackground(true);
  81         } else if (eventType == CocoaConstants.NPCocoaEventMouseExited) {
  82             CCursorManager.nativeSetAllowsCursorSetInBackground(false);
  83         }
  84 
  85         responder.handleMouseEvent(eventType, modifierFlags, buttonNumber,
  86                                    clickCount, x, y, screenX, screenY);
  87     }
  88 
  89     public void handleScrollEvent(double pluginX, double pluginY, int modifierFlags,
  90                                   double deltaX, double deltaY, double deltaZ) {
  91         int x = (int)pluginX;
  92         int y = (int)pluginY;
  93 
  94         responder.handleScrollEvent(x, y, modifierFlags, deltaX, deltaY);
  95     }
  96 
  97     public void handleKeyEvent(int eventType, int modifierFlags, String characters,
  98                                String charsIgnoringMods, boolean isRepeat, short keyCode,
  99                                boolean needsKeyTyped) {
 100         responder.handleKeyEvent(eventType, modifierFlags, characters, charsIgnoringMods,
 101                 keyCode, needsKeyTyped, isRepeat);
 102     }
 103 
 104     public void handleInputEvent(String text) {
 105         responder.handleInputEvent(text);
 106     }
 107 
 108     // handleFocusEvent is called when the applet becames focused/unfocused.
 109     // This method can be called from different threads.
 110     public void handleFocusEvent(boolean focused) {
 111         synchronized (classLock) {
 112             // In some cases an applet may not receive the focus lost event
 113             // from the parent window (see 8012330)
 114             focusedWindow = (focused) ? this
 115                     : ((focusedWindow == this) ? null : focusedWindow);
 116         }
 117         if (focusedWindow == this) {
 118             // see bug 8010925
 119             // we can't put this to handleWindowFocusEvent because
 120             // it won't be invoced if focuse is moved to a html element
 121             // on the same page.
 122             CClipboard clipboard = (CClipboard) Toolkit.getDefaultToolkit().getSystemClipboard();
 123             clipboard.checkPasteboard();
 124         }
 125         if (parentWindowActive) {
 126             responder.handleWindowFocusEvent(focused, null);
 127         }
 128     }
 129 
 130     /**
 131      * When the parent window is activated this method is called for all EmbeddedFrames in it.
 132      *
 133      * For the CEmbeddedFrame which had focus before the deactivation this method triggers
 134      * focus events in the following order:
 135      *  1. WINDOW_ACTIVATED for this EmbeddedFrame
 136      *  2. WINDOW_GAINED_FOCUS for this EmbeddedFrame
 137      *  3. FOCUS_GAINED for the most recent focus owner in this EmbeddedFrame
 138      *
 139      * The caller must not requestFocus on the EmbeddedFrame together with calling this method.
 140      *
 141      * @param parentWindowActive true if the window is activated, false otherwise
 142      */
 143     // handleWindowFocusEvent is called for all applets, when the browser
 144     // becomes active/inactive. This event should be filtered out for
 145     // non-focused applet. This method can be called from different threads.
 146     public void handleWindowFocusEvent(boolean parentWindowActive) {
 147         this.parentWindowActive = parentWindowActive;
 148         // If several applets are running in different browser's windows, it is necessary to
 149         // detect the switching between the parent windows and update focusedWindow accordingly.
 150         synchronized (classLock) {
 151             if (parentWindowActive && focusedWindow != this &&
 152                 (focusedWindow == null || (focusedWindow != null && !focusedWindow.isParentWindowActive()))) {
 153                 // It looks like we have switched to another browser window, let's set focus to
 154                 // the first applet in this window.
 155                 focusedWindow = this;
 156             }
 157         }
 158         // ignore focus "lost" native request as it may mistakenly
 159         // deactivate active window (see 8001161)
 160         if (focusedWindow == this && parentWindowActive) {
 161             responder.handleWindowFocusEvent(parentWindowActive, null);
 162         }
 163     }
 164 
 165     public boolean isParentWindowActive() {
 166         return parentWindowActive;
 167     }
 168 }