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 CEmbeddedFrame previousFocusedWindow;
  43     private boolean parentWindowActive = true;
  44 
  45     public CEmbeddedFrame() {
  46         show();
  47     }
  48 
  49     public void addNotify() {
  50         if (getPeer() == null) {
  51             LWCToolkit toolkit = (LWCToolkit)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, characters, charsIgnoringMods,
 102                 keyCode, needsKeyTyped, isRepeat);
 103     }
 104 
 105     public void handleInputEvent(String text) {
 106         responder.handleInputEvent(text);
 107     }
 108 
 109     // handleFocusEvent is called when the applet becames focused/unfocused.
 110     // This method can be called from different threads.
 111     public void handleFocusEvent(boolean focused) {
 112         synchronized (classLock) {
 113             // In some cases an applet may not receive the focus lost event
 114             // from the parent window (see 8012330)
 115             focusedWindow = (focused) ? this
 116                     : ((focusedWindow == this) ? null : focusedWindow);
 117         }
 118         if (focusedWindow == this) {
 119             // see bug 8010925
 120             // we can't put this to handleWindowFocusEvent because
 121             // it won't be invoced if focuse is moved to a html element
 122             // on the same page.
 123             CClipboard clipboard = (CClipboard) Toolkit.getDefaultToolkit().getSystemClipboard();
 124             clipboard.checkPasteboard();
 125         }
 126         if (parentWindowActive) {
 127             responder.handleWindowFocusEvent(focused, null);
 128         }
 129     }
 130 
 131     /**
 132      * When the parent window is activated this method is called for all EmbeddedFrames in it.
 133      *
 134      * For the CEmbeddedFrame which had focus before the deactivation this method triggers
 135      * focus events in the following order:
 136      *  1. WINDOW_ACTIVATED for this EmbeddedFrame
 137      *  2. WINDOW_GAINED_FOCUS for this EmbeddedFrame
 138      *  3. FOCUS_GAINED for the most recent focus owner in this EmbeddedFrame
 139      *
 140      * The caller must not requestFocus on the EmbeddedFrame together with calling this method.
 141      *
 142      * @param parentWindowActive true if the window is activated, false otherwise
 143      */
 144     // handleWindowFocusEvent is called for all applets, when the browser
 145     // becomes active/inactive. This event should be filtered out for
 146     // non-focused applet. This method can be called from different threads.
 147     public void handleWindowFocusEvent(boolean parentWindowActive) {
 148         this.parentWindowActive = parentWindowActive;
 149         // If several applets are running in different browser's windows, it is necessary to
 150         // detect the switching between the parent windows and update focusedWindow accordingly.
 151         synchronized (classLock) {
 152             if (!parentWindowActive) {
 153                 this.previousFocusedWindow = focusedWindow;
 154             }
 155             if (parentWindowActive && focusedWindow != this &&
 156                 (focusedWindow == null || (focusedWindow != null && !focusedWindow.isParentWindowActive()))) {
 157                 // It looks like we have switched to another browser window, let's restore focus to
 158                 // the previously focused applet in this window.
 159                 if (this.previousFocusedWindow != null) {
 160                     focusedWindow = this.previousFocusedWindow;
 161                 } else {
 162                     // No applets were focused in this window, so set focus to the first
 163                     // applet in the window
 164                     focusedWindow = this;
 165                 }
 166             }
 167         }
 168         // ignore focus "lost" native request as it may mistakenly
 169         // deactivate active window (see 8001161)
 170         if (focusedWindow == this && parentWindowActive) {
 171             responder.handleWindowFocusEvent(parentWindowActive, null);
 172         }
 173     }
 174 
 175     public boolean isParentWindowActive() {
 176         return parentWindowActive;
 177     }
 178 }