1 /* 2 * Copyright (c) 2011, 2015, 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.AWTAccessor; 34 import sun.awt.EmbeddedFrame; 35 import sun.lwawt.LWWindowPeer; 36 37 @SuppressWarnings("serial") // JDK implementation class 38 public class CEmbeddedFrame extends EmbeddedFrame { 39 40 private CPlatformResponder responder; 41 private static final Object classLock = new Object(); 42 private static volatile CEmbeddedFrame globalFocusedWindow; 43 private CEmbeddedFrame browserWindowFocusedApplet; 44 private boolean parentWindowActive = true; 45 CEmbeddedFrame()46 public CEmbeddedFrame() { 47 show(); 48 } 49 addNotify()50 public void addNotify() { 51 if (!isDisplayable()) { 52 LWCToolkit toolkit = (LWCToolkit)Toolkit.getDefaultToolkit(); 53 LWWindowPeer peer = toolkit.createEmbeddedFrame(this); 54 setPeer(peer); 55 responder = new CPlatformResponder(peer, true); 56 } 57 super.addNotify(); 58 } 59 registerAccelerator(AWTKeyStroke stroke)60 public void registerAccelerator(AWTKeyStroke stroke) {} 61 unregisterAccelerator(AWTKeyStroke stroke)62 public void unregisterAccelerator(AWTKeyStroke stroke) {} 63 getLayerPtr()64 protected long getLayerPtr() { 65 return AWTAccessor.getComponentAccessor().<LWWindowPeer>getPeer(this) 66 .getLayerPtr(); 67 } 68 69 // ----------------------------------------------------------------------- 70 // SYNTHETIC EVENT DELIVERY 71 // ----------------------------------------------------------------------- 72 handleMouseEvent(int eventType, int modifierFlags, double pluginX, double pluginY, int buttonNumber, int clickCount)73 public void handleMouseEvent(int eventType, int modifierFlags, double pluginX, 74 double pluginY, int buttonNumber, int clickCount) { 75 int x = (int)pluginX; 76 int y = (int)pluginY; 77 Point locationOnScreen = getLocationOnScreen(); 78 int absX = locationOnScreen.x + x; 79 int absY = locationOnScreen.y + y; 80 81 if (eventType == CocoaConstants.NPCocoaEventMouseEntered) { 82 CCursorManager.nativeSetAllowsCursorSetInBackground(true); 83 } else if (eventType == CocoaConstants.NPCocoaEventMouseExited) { 84 CCursorManager.nativeSetAllowsCursorSetInBackground(false); 85 } 86 87 responder.handleMouseEvent(eventType, modifierFlags, buttonNumber, 88 clickCount, x, y, absX, absY); 89 } 90 handleScrollEvent(double pluginX, double pluginY, int modifierFlags, double deltaX, double deltaY, double deltaZ)91 public void handleScrollEvent(double pluginX, double pluginY, int modifierFlags, 92 double deltaX, double deltaY, double deltaZ) { 93 int x = (int)pluginX; 94 int y = (int)pluginY; 95 Point locationOnScreen = getLocationOnScreen(); 96 int absX = locationOnScreen.x + x; 97 int absY = locationOnScreen.y + y; 98 99 responder.handleScrollEvent(x, y, absX, absY, modifierFlags, deltaX, 100 deltaY, NSEvent.SCROLL_PHASE_UNSUPPORTED); 101 } 102 handleKeyEvent(int eventType, int modifierFlags, String characters, String charsIgnoringMods, boolean isRepeat, short keyCode, boolean needsKeyTyped)103 public void handleKeyEvent(int eventType, int modifierFlags, String characters, 104 String charsIgnoringMods, boolean isRepeat, short keyCode, 105 boolean needsKeyTyped) { 106 responder.handleKeyEvent(eventType, modifierFlags, characters, charsIgnoringMods, 107 keyCode, needsKeyTyped, isRepeat); 108 } 109 handleInputEvent(String text)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. handleFocusEvent(boolean focused)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 globalFocusedWindow = (focused) ? this 121 : ((globalFocusedWindow == this) ? null : globalFocusedWindow); 122 } 123 if (globalFocusedWindow == 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.checkPasteboardAndNotify(); 130 } 131 if (parentWindowActive) { 132 responder.handleWindowFocusEvent(focused, null); 133 } 134 } 135 136 /** 137 * When the parent window is activated this method is called for all EmbeddedFrames in it. 138 * 139 * For the CEmbeddedFrame which had focus before the deactivation this method triggers 140 * focus events in the following order: 141 * 1. WINDOW_ACTIVATED for this EmbeddedFrame 142 * 2. WINDOW_GAINED_FOCUS for this EmbeddedFrame 143 * 3. FOCUS_GAINED for the most recent focus owner in this EmbeddedFrame 144 * 145 * The caller must not requestFocus on the EmbeddedFrame together with calling this method. 146 * 147 * @param parentWindowActive true if the window is activated, false otherwise 148 */ 149 // handleWindowFocusEvent is called for all applets, when the browser 150 // becomes active/inactive. This event should be filtered out for 151 // non-focused applet. This method can be called from different threads. handleWindowFocusEvent(boolean parentWindowActive)152 public void handleWindowFocusEvent(boolean parentWindowActive) { 153 this.parentWindowActive = parentWindowActive; 154 // If several applets are running in different browser's windows, it is necessary to 155 // detect the switching between the parent windows and update globalFocusedWindow accordingly. 156 synchronized (classLock) { 157 if (!parentWindowActive) { 158 this.browserWindowFocusedApplet = globalFocusedWindow; 159 } 160 if (parentWindowActive && globalFocusedWindow != this && isParentWindowChanged()) { 161 // It looks like we have switched to another browser window, let's restore focus to 162 // the previously focused applet in this window. If no applets were focused in the 163 // window, we will set focus to the first applet in the window. 164 globalFocusedWindow = (this.browserWindowFocusedApplet != null) ? this.browserWindowFocusedApplet 165 : this; 166 } 167 } 168 if (globalFocusedWindow == this) { 169 responder.handleWindowFocusEvent(parentWindowActive, null); 170 } 171 } 172 isParentWindowActive()173 public boolean isParentWindowActive() { 174 return parentWindowActive; 175 } 176 isParentWindowChanged()177 private boolean isParentWindowChanged() { 178 // If globalFocusedWindow is located at inactive parent window or null, we have swithed to 179 // another window. 180 return globalFocusedWindow != null ? !globalFocusedWindow.isParentWindowActive() : true; 181 } 182 183 @Override synthesizeWindowActivation(boolean doActivate)184 public void synthesizeWindowActivation(boolean doActivate) { 185 if (isParentWindowActive() != doActivate) { 186 handleWindowFocusEvent(doActivate); 187 } 188 } 189 updateGlobalFocusedWindow(CEmbeddedFrame newGlobalFocusedWindow)190 public static void updateGlobalFocusedWindow(CEmbeddedFrame newGlobalFocusedWindow) { 191 synchronized (classLock) { 192 if (newGlobalFocusedWindow.isParentWindowActive()) { 193 globalFocusedWindow = newGlobalFocusedWindow; 194 } 195 } 196 } 197 } 198