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