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.event.*; 30 import java.awt.image.VolatileImage; 31 32 import sun.awt.CGraphicsConfig; 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 43 private LWWindowPeer peer; 44 private SurfaceData surfaceData; 45 private CGLLayer windowLayer; 46 private CPlatformResponder responder; 47 48 public CPlatformView() { 49 super(0, true); 50 } 51 52 public void initialize(LWWindowPeer peer) { 53 this.peer = peer; 54 this.responder = new CPlatformResponder(peer, false); 55 56 if (!LWCToolkit.getSunAwtDisableCALayers()) { 57 this.windowLayer = new CGLLayer(peer); 58 } 59 setPtr(nativeCreateView(0, 0, 0, 0, getWindowLayerPtr())); 60 } 61 62 public long getAWTView() { 63 return ptr; 64 } 65 66 public boolean isOpaque() { 67 return peer.isOpaque(); 68 } 69 70 /* 71 * All coordinates passed to the method should be based on the origin being in the bottom-left corner (standard 72 * Cocoa coordinates). 73 */ 74 public void setBounds(int x, int y, int width, int height) { 75 CWrapper.NSView.setFrame(ptr, x, y, width, height); 76 } 77 78 // REMIND: CGLSurfaceData expects top-level's size 79 public Rectangle getBounds() { 80 return peer.getBounds(); 81 } 82 83 public Object getDestination() { 84 return peer; 85 } 86 87 // ---------------------------------------------------------------------- 88 // PAINTING METHODS 89 // ---------------------------------------------------------------------- 90 91 public void drawImageOnPeer(VolatileImage xBackBuffer, int x1, int y1, int x2, int y2) { 92 Graphics g = peer.getGraphics(); 93 try { 94 g.drawImage(xBackBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null); 95 } finally { 96 g.dispose(); 97 } 98 } 99 100 public Image createBackBuffer() { 101 Rectangle r = peer.getBounds(); 102 Image im = null; 103 if (!r.isEmpty()) { 104 int transparency = (isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT); 105 im = peer.getGraphicsConfiguration().createCompatibleImage(r.width, r.height, transparency); 106 } 107 return im; 108 } 109 110 public SurfaceData replaceSurfaceData() { 111 if (!LWCToolkit.getSunAwtDisableCALayers()) { 112 surfaceData = windowLayer.replaceSurfaceData(); 113 } else { 114 if (surfaceData == null) { 115 CGraphicsConfig graphicsConfig = (CGraphicsConfig)peer.getGraphicsConfiguration(); 116 surfaceData = graphicsConfig.createSurfaceData(this); 117 } else { 118 validateSurface(); 119 } 120 } 121 return surfaceData; 122 } 123 124 private void validateSurface() { 125 if (surfaceData != null) { 126 ((CGLSurfaceData)surfaceData).validate(); 127 } 128 } 129 130 public GraphicsConfiguration getGraphicsConfiguration() { 131 return peer.getGraphicsConfiguration(); 132 } 133 134 public SurfaceData getSurfaceData() { 135 return surfaceData; 136 } 137 138 @Override 139 public void dispose() { 140 if (!LWCToolkit.getSunAwtDisableCALayers()) { 141 windowLayer.dispose(); 142 } 143 super.dispose(); 144 } 145 146 public long getWindowLayerPtr() { 147 if (!LWCToolkit.getSunAwtDisableCALayers()) { 148 return windowLayer.getPointer(); 149 } else { 150 return 0; 151 } 152 } 153 154 // ---------------------------------------------------------------------- 155 // NATIVE CALLBACKS 156 // ---------------------------------------------------------------------- 157 158 private void deliverMouseEvent(NSEvent event) { 159 int x = event.getX(); 160 int y = getBounds().height - event.getY(); 161 162 if (event.getType() == CocoaConstants.NSScrollWheel) { 163 responder.handleScrollEvent(x, y, event.getModifierFlags(), 164 event.getScrollDeltaX(), event.getScrollDeltaY()); 165 } else { 166 responder.handleMouseEvent(event.getType(), event.getModifierFlags(), event.getButtonNumber(), 167 event.getClickCount(), x, y, event.getAbsX(), event.getAbsY()); 168 } 169 } 170 171 private void deliverKeyEvent(NSEvent event) { 172 responder.handleKeyEvent(event.getType(), event.getModifierFlags(), 173 event.getCharactersIgnoringModifiers(), event.getKeyCode()); 174 } 175 176 private void deliverWindowDidExposeEvent() { 177 Rectangle r = peer.getBounds(); 178 peer.notifyExpose(0, 0, r.width, r.height); 179 } 180 181 private void deliverWindowDidExposeEvent(float x, float y, float w, float h) { 182 peer.notifyExpose((int)x, (int)y, (int)w, (int)h); 183 } 184 }