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 java.awt.*;
  29 import java.awt.geom.Rectangle2D;
  30 
  31 import sun.awt.CGraphicsConfig;
  32 import sun.awt.CGraphicsEnvironment;
  33 import sun.lwawt.LWWindowPeer;
  34 import sun.lwawt.macosx.event.NSEvent;
  35 
  36 import sun.java2d.SurfaceData;
  37 import sun.java2d.opengl.CGLLayer;
  38 import sun.java2d.opengl.CGLSurfaceData;
  39 
  40 public class CPlatformView extends CFRetainedResource {
  41     private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr);
  42     private static native void nativeSetAutoResizable(long awtView, boolean toResize);
  43     private static native int nativeGetNSViewDisplayID(long awtView);
  44     private static native Rectangle2D nativeGetLocationOnScreen(long awtView);
  45     private static native boolean nativeIsViewUnderMouse(long ptr);
  46 
  47     private LWWindowPeer peer;
  48     private SurfaceData surfaceData;
  49     private CGLLayer windowLayer;
  50     private CPlatformResponder responder;
  51 
  52     public CPlatformView() {
  53         super(0, true);
  54     }
  55 
  56     public void initialize(LWWindowPeer peer, CPlatformResponder responder) {
  57         this.peer = peer;
  58         this.responder = responder;
  59 
  60         if (!LWCToolkit.getSunAwtDisableCALayers()) {
  61             this.windowLayer = new CGLLayer(peer);
  62         }
  63         setPtr(nativeCreateView(0, 0, 0, 0, getWindowLayerPtr()));
  64     }
  65 
  66     public long getAWTView() {
  67         return ptr;
  68         }
  69 
  70     public boolean isOpaque() {
  71         return !peer.isTranslucent();
  72     }
  73 
  74     /*
  75      * All coordinates passed to the method should be based on the origin being in the bottom-left corner (standard
  76      * Cocoa coordinates).
  77      */
  78     public void setBounds(int x, int y, int width, int height) {
  79         CWrapper.NSView.setFrame(ptr, x, y, width, height);
  80     }
  81 
  82     // REMIND: CGLSurfaceData expects top-level's size
  83     public Rectangle getBounds() {
  84         return peer.getBounds();
  85     }
  86 
  87     public Object getDestination() {
  88         return peer;
  89     }
  90 
  91     public void enterFullScreenMode() {
  92         CWrapper.NSView.enterFullScreenMode(ptr);
  93     }
  94 
  95     public void exitFullScreenMode() {
  96         CWrapper.NSView.exitFullScreenMode(ptr);
  97     }
  98 
  99     // ----------------------------------------------------------------------
 100     // PAINTING METHODS
 101     // ----------------------------------------------------------------------
 102     public SurfaceData replaceSurfaceData() {
 103         if (!LWCToolkit.getSunAwtDisableCALayers()) {
 104             surfaceData = windowLayer.replaceSurfaceData();
 105         } else {
 106             if (surfaceData == null) {
 107                 CGraphicsConfig graphicsConfig = (CGraphicsConfig)peer.getGraphicsConfiguration();
 108                 surfaceData = graphicsConfig.createSurfaceData(this);
 109             } else {
 110                 validateSurface();
 111             }
 112         }
 113         return surfaceData;
 114     }
 115 
 116     private void validateSurface() {
 117         if (surfaceData != null) {
 118             ((CGLSurfaceData)surfaceData).validate();
 119         }
 120     }
 121 
 122     public GraphicsConfiguration getGraphicsConfiguration() {
 123         return peer.getGraphicsConfiguration();
 124     }
 125 
 126     public SurfaceData getSurfaceData() {
 127         return surfaceData;
 128     }
 129 
 130     @Override
 131     public void dispose() {
 132         if (!LWCToolkit.getSunAwtDisableCALayers()) {
 133             windowLayer.dispose();
 134         }
 135         super.dispose();
 136     }
 137 
 138     public long getWindowLayerPtr() {
 139         if (!LWCToolkit.getSunAwtDisableCALayers()) {
 140             return windowLayer.getPointer();
 141         } else {
 142             return 0;
 143         }
 144     }
 145 
 146     public void setAutoResizable(boolean toResize) {
 147         nativeSetAutoResizable(this.getAWTView(), toResize);
 148     }
 149 
 150     public boolean isUnderMouse() {
 151         return nativeIsViewUnderMouse(getAWTView());
 152     }
 153 
 154     public GraphicsDevice getGraphicsDevice() {
 155         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 156         CGraphicsEnvironment cge = (CGraphicsEnvironment)ge;
 157         int displayID = nativeGetNSViewDisplayID(getAWTView());
 158         GraphicsDevice gd = cge.getScreenDevice(displayID);
 159         if (gd == null) {
 160             // this could possibly happen during device removal
 161             // use the default screen device in this case
 162             gd = ge.getDefaultScreenDevice();
 163         }
 164         return gd;
 165     }
 166 
 167     public Point getLocationOnScreen() {
 168         Rectangle r = nativeGetLocationOnScreen(this.getAWTView()).getBounds();
 169         return new Point(r.x, r.y);
 170     }
 171 
 172     // ----------------------------------------------------------------------
 173     // NATIVE CALLBACKS
 174     // ----------------------------------------------------------------------
 175 
 176     /*
 177      * The callback is called only in the embedded case when the view is
 178      * automatically resized by the superview.
 179      * In normal mode this method is never called.
 180      */
 181     private void deliverResize(int x, int y, int w, int h) {
 182         peer.notifyReshape(x, y, w, h);
 183     }
 184 
 185 
 186     private void deliverMouseEvent(NSEvent event) {
 187         int x = event.getX();
 188         int y = getBounds().height - event.getY();
 189 
 190         if (event.getType() == CocoaConstants.NSScrollWheel) {
 191             responder.handleScrollEvent(x, y, event.getModifierFlags(),
 192                                         event.getScrollDeltaX(), event.getScrollDeltaY());
 193         } else {
 194             responder.handleMouseEvent(event.getType(), event.getModifierFlags(), event.getButtonNumber(),
 195                                        event.getClickCount(), x, y, event.getAbsX(), event.getAbsY());
 196         }
 197     }
 198 
 199     private void deliverKeyEvent(NSEvent event) {
 200         responder.handleKeyEvent(event.getType(), event.getModifierFlags(),
 201                                  event.getCharactersIgnoringModifiers(), event.getKeyCode(), true);
 202     }
 203 
 204     /**
 205      * Called by the native delegate in layer backed view mode or in the simple
 206      * NSView mode. See NSView.drawRect().
 207      */
 208     private void deliverWindowDidExposeEvent() {
 209         peer.notifyExpose(peer.getSize());
 210     }
 211 }