lwjgl2-arm64/src/java/org/lwjgl/opengl/MacOSXFrame.java

282 lines
7.1 KiB
Java
Raw Normal View History

2004-11-25 23:31:38 +01:00
/*
2004-11-12 14:23:20 +01:00
* Copyright (c) 2002-2004 LWJGL Project
* All rights reserved.
2004-11-25 23:31:38 +01:00
*
2004-11-12 14:23:20 +01:00
* Redistribution and use in source and binary forms, with or without
2004-11-25 23:31:38 +01:00
* modification, are permitted provided that the following conditions are
2004-11-12 14:23:20 +01:00
* met:
2004-11-25 23:31:38 +01:00
*
* * Redistributions of source code must retain the above copyright
2004-11-12 14:23:20 +01:00
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
2004-11-25 23:31:38 +01:00
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
2004-11-12 14:23:20 +01:00
* from this software without specific prior written permission.
2004-11-25 23:31:38 +01:00
*
2004-11-12 14:23:20 +01:00
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2004-11-25 23:31:38 +01:00
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2004-11-12 14:23:20 +01:00
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2004-11-25 23:31:38 +01:00
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2004-11-12 14:23:20 +01:00
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.opengl;
/**
* This is the Mac OS X AWT Frame. It contains thread safe
* methods to manipulateit from non-AWT threads
* @author elias_naur
*/
import java.awt.BorderLayout;
import java.awt.Frame;
2004-11-20 17:46:44 +01:00
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
2004-11-12 14:23:20 +01:00
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
2004-11-20 17:46:44 +01:00
import java.awt.event.ComponentListener;
2004-11-12 14:23:20 +01:00
import java.awt.event.WindowEvent;
2004-11-20 17:46:44 +01:00
import java.awt.event.WindowListener;
2004-11-12 14:23:20 +01:00
import java.lang.reflect.InvocationTargetException;
import org.lwjgl.LWJGLException;
final class MacOSXFrame extends Frame implements WindowListener, ComponentListener {
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
private final MacOSXGLCanvas canvas;
private boolean close_requested;
/* States */
private Rectangle bounds;
private boolean should_update;
private boolean active;
private boolean visible;
private boolean minimized;
2004-11-25 23:31:38 +01:00
MacOSXFrame(DisplayMode mode, java.awt.DisplayMode requested_mode, boolean fullscreen, int x, int y) throws LWJGLException {
2004-11-12 14:23:20 +01:00
setResizable(false);
addWindowListener(this);
addComponentListener(this);
canvas = new MacOSXGLCanvas();
add(canvas, BorderLayout.CENTER);
boolean undecorated = Boolean.getBoolean("org.lwjgl.opengl.Window.undecorated");
setUndecorated(fullscreen || undecorated);
2004-11-25 23:31:38 +01:00
if ( fullscreen ) {
2004-11-12 14:23:20 +01:00
getDevice().setFullScreenWindow(this);
getDevice().setDisplayMode(requested_mode);
java.awt.DisplayMode real_mode = getDevice().getDisplayMode();
2004-11-12 14:23:20 +01:00
/** For some strange reason, the display mode is sometimes silently capped even though the mode is reported as supported */
2004-11-25 23:31:38 +01:00
if ( requested_mode.getWidth() != real_mode.getWidth() || requested_mode.getHeight() != real_mode.getHeight() ) {
getDevice().setFullScreenWindow(null);
2004-11-12 14:23:20 +01:00
syncDispose();
throw new LWJGLException("AWT capped mode: requested mode = " + requested_mode.getWidth() + "x" + requested_mode.getHeight() +
2004-11-25 23:31:38 +01:00
" but got " + real_mode.getWidth() + " " + real_mode.getHeight());
2004-11-12 14:23:20 +01:00
}
}
pack();
syncReshape(x, y, mode.getWidth(), mode.getHeight());
invokeAWT(new Runnable() {
public void run() {
setVisible(true);
requestFocus();
canvas.requestFocus();
}
});
canvas.waitForCanvasCreated();
}
public Rectangle syncGetBounds() {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
return bounds;
}
}
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
public void componentShown(ComponentEvent e) {
}
public void componentHidden(ComponentEvent e) {
}
private void updateBounds() {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
bounds = getBounds();
}
}
public void componentResized(ComponentEvent e) {
updateBounds();
}
public void componentMoved(ComponentEvent e) {
updateBounds();
}
2004-11-25 23:31:38 +01:00
public static GraphicsDevice getDevice() {
2004-11-12 14:23:20 +01:00
GraphicsEnvironment g_env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice device = g_env.getDefaultScreenDevice();
return device;
}
public void windowIconified(WindowEvent e) {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
minimized = true;
}
}
public void windowDeiconified(WindowEvent e) {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
minimized = false;
}
}
public void windowOpened(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
close_requested = true;
}
}
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
public void windowDeactivated(WindowEvent e) {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
active = false;
}
}
public void windowActivated(WindowEvent e) {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
should_update = true;
active = true;
}
}
public void syncDispose() {
invokeAWT(new Runnable() {
public void run() {
2004-11-25 23:31:38 +01:00
if ( isDisplayable() )
dispose();
2004-11-12 14:23:20 +01:00
}
});
}
private class TitleSetter implements Runnable {
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
private final String title;
2004-11-25 23:31:38 +01:00
TitleSetter(String title) {
2004-11-12 14:23:20 +01:00
this.title = title;
}
public void run() {
setTitle(title);
}
}
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
public void syncSetTitle(String title) {
invokeAWT(new TitleSetter(title));
}
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
public boolean syncIsCloseRequested() {
boolean result;
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
result = close_requested;
close_requested = false;
}
return result;
}
public boolean syncIsVisible() {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
return !minimized;
}
}
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
public boolean syncIsActive() {
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
return active;
}
}
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
public MacOSXGLCanvas getCanvas() {
return canvas;
}
public boolean syncShouldUpdateContext() {
boolean result;
2004-11-25 23:31:38 +01:00
synchronized ( this ) {
2004-11-12 14:23:20 +01:00
result = canvas.syncShouldUpdateContext() || should_update;
should_update = false;
}
return result;
}
private class Reshaper implements Runnable {
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
private final int x;
private final int y;
private final int width;
private final int height;
2004-11-25 23:31:38 +01:00
Reshaper(int x, int y, int width, int height) {
2004-11-12 14:23:20 +01:00
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void run() {
Insets insets = getInsets();
setBounds(x, y, width + insets.left + insets.right, height + insets.top + insets.bottom);
}
}
private void invokeAWT(Runnable r) {
try {
if (java.awt.EventQueue.isDispatchThread()) {
r.run();
} else {
java.awt.EventQueue.invokeAndWait(r);
}
2004-11-12 14:23:20 +01:00
} catch (InterruptedException e) {
// ignore
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public void syncReshape(int x, int y, int width, int height) {
invokeAWT(new Reshaper(x, y, width, height));
}
private class CursorSetter implements Runnable {
2004-11-25 23:31:38 +01:00
2004-11-12 14:23:20 +01:00
private final java.awt.Cursor awt_cursor;
2004-11-25 23:31:38 +01:00
CursorSetter(java.awt.Cursor awt_cursor) {
2004-11-12 14:23:20 +01:00
this.awt_cursor = awt_cursor;
}
public void run() {
canvas.setCursor(awt_cursor);
}
}
public void syncSetCursor(java.awt.Cursor awt_cursor) {
invokeAWT(new CursorSetter(awt_cursor));
}
}