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