package sun.lwawt.macosx;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import sun.awt.CGraphicsEnvironment;
import sun.java2d.SurfaceData;
import sun.java2d.opengl.CGLLayer;
import sun.lwawt.LWWindowPeer;
public class CPlatformView extends CFRetainedResource {
private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr);
private static native void nativeSetAutoResizable(long awtView, boolean toResize);
private static native int nativeGetNSViewDisplayID(long awtView);
private static native Rectangle2D nativeGetLocationOnScreen(long awtView);
private static native boolean nativeIsViewUnderMouse(long ptr);
private LWWindowPeer peer;
private SurfaceData surfaceData;
private CGLLayer windowLayer;
private CPlatformResponder responder;
public CPlatformView() {
super(0, true);
}
public void initialize(LWWindowPeer peer, CPlatformResponder responder) {
initializeBase(peer, responder);
this.windowLayer = createCGLayer();
setPtr(nativeCreateView(0, 0, 0, 0, getWindowLayerPtr()));
}
public CGLLayer createCGLayer() {
return new CGLLayer(peer);
}
protected void initializeBase(LWWindowPeer peer, CPlatformResponder responder) {
this.peer = peer;
this.responder = responder;
}
public long getAWTView() {
return ptr;
}
public void setBounds(int x, int y, int width, int height) {
execute(ptr->CWrapper.NSView.setFrame(ptr, x, y, width, height));
}
public Rectangle getBounds() {
return peer.getBounds();
}
public void setToolTip(String msg) {
execute(ptr -> CWrapper.NSView.setToolTip(ptr, msg));
}
public SurfaceData replaceSurfaceData() {
surfaceData = windowLayer.replaceSurfaceData();
return surfaceData;
}
public SurfaceData getSurfaceData() {
return surfaceData;
}
@Override
public void dispose() {
windowLayer.dispose();
super.dispose();
}
public long getWindowLayerPtr() {
return windowLayer.getPointer();
}
public void setAutoResizable(boolean toResize) {
execute(ptr -> nativeSetAutoResizable(ptr, toResize));
}
public boolean isUnderMouse() {
AtomicBoolean ref = new AtomicBoolean();
execute(ptr -> {
ref.set(nativeIsViewUnderMouse(ptr));
});
return ref.get();
}
public GraphicsDevice getGraphicsDevice() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
CGraphicsEnvironment cge = (CGraphicsEnvironment)ge;
AtomicInteger ref = new AtomicInteger();
execute(ptr -> {
ref.set(nativeGetNSViewDisplayID(ptr));
});
GraphicsDevice gd = cge.getScreenDevice(ref.get());
if (gd == null) {
gd = ge.getDefaultScreenDevice();
}
return gd;
}
public Point getLocationOnScreen() {
AtomicReference<Rectangle> ref = new AtomicReference<>();
execute(ptr -> {
ref.set(nativeGetLocationOnScreen(ptr).getBounds());
});
Rectangle r = ref.get();
if (r != null) {
return new Point(r.x, r.y);
}
return new Point(0, 0);
}
private void deliverResize(int x, int y, int w, int h) {
peer.notifyReshape(x, y, w, h);
}
private void deliverMouseEvent(final NSEvent event) {
int x = event.getX();
int y = getBounds().height - event.getY();
int absX = event.getAbsX();
int absY = event.getAbsY();
if (event.getType() == CocoaConstants.NSScrollWheel) {
responder.handleScrollEvent(x, y, absX, absY, event.getModifierFlags(),
event.getScrollDeltaX(), event.getScrollDeltaY(),
event.getScrollPhase());
} else {
responder.handleMouseEvent(event.getType(), event.getModifierFlags(), event.getButtonNumber(),
event.getClickCount(), x, y,
absX, absY);
}
}
private void deliverKeyEvent(NSEvent event) {
responder.handleKeyEvent(event.getType(), event.getModifierFlags(), event.getCharacters(),
event.getCharactersIgnoringModifiers(), event.getKeyCode(), true, false);
}
private void deliverWindowDidExposeEvent() {
peer.notifyExpose(peer.getSize());
}
}