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