diff options
Diffstat (limited to 'src/classes/com/sun')
-rw-r--r-- | src/classes/com/sun/javafx/newt/Event.java | 77 | ||||
-rw-r--r-- | src/classes/com/sun/javafx/newt/GLWindow.java | 389 | ||||
-rw-r--r-- | src/classes/com/sun/javafx/newt/InputEvent.java | 23 | ||||
-rw-r--r-- | src/classes/com/sun/javafx/newt/KeyEvent.java | 21 | ||||
-rw-r--r-- | src/classes/com/sun/javafx/newt/MouseEvent.java | 30 | ||||
-rwxr-xr-x | src/classes/com/sun/javafx/newt/Window.java | 92 | ||||
-rw-r--r-- | src/classes/com/sun/javafx/newt/WindowEvent.java | 59 | ||||
-rw-r--r-- | src/classes/com/sun/javafx/newt/WindowListener.java | 39 | ||||
-rw-r--r-- | src/classes/com/sun/javafx/newt/awt/AWTWindow.java | 122 | ||||
-rwxr-xr-x | src/classes/com/sun/javafx/newt/windows/WindowsWindow.java | 2 | ||||
-rwxr-xr-x | src/classes/com/sun/javafx/newt/x11/X11Window.java | 2 |
11 files changed, 757 insertions, 99 deletions
diff --git a/src/classes/com/sun/javafx/newt/Event.java b/src/classes/com/sun/javafx/newt/Event.java new file mode 100644 index 000000000..7d2b24ba5 --- /dev/null +++ b/src/classes/com/sun/javafx/newt/Event.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + */ + +package com.sun.javafx.newt; + +public class Event { + private boolean isSystemEvent; + private int eventType; + private Window source; + private long when; + + Event(boolean isSystemEvent, int eventType, Window source, long when) { + this.isSystemEvent = isSystemEvent; + this.eventType = eventType; + this.source = source; + this.when = when; + } + + protected Event(int eventType, Window source, long when) { + this(false, eventType, source, when); + } + + /** Indicates whether this event was produced by the system or + generated by user code. */ + public final boolean isSystemEvent() { + return isSystemEvent; + } + + /** Returns the event type of this event. */ + public final int getEventType() { + return eventType; + } + + /** Returns the source Window which produced this Event. */ + public final Window getSource() { + return source; + } + + /** Returns the timestamp, in milliseconds, of this event. */ + public final long getWhen() { + return when; + } + + public String toString() { + return "Event[sys:"+isSystemEvent()+", source:"+getSource()+", when:"+getWhen()+"]"; + } +} diff --git a/src/classes/com/sun/javafx/newt/GLWindow.java b/src/classes/com/sun/javafx/newt/GLWindow.java new file mode 100644 index 000000000..93bb34fe0 --- /dev/null +++ b/src/classes/com/sun/javafx/newt/GLWindow.java @@ -0,0 +1,389 @@ +/* + * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + */ + +package com.sun.javafx.newt; + +import javax.media.opengl.*; +import com.sun.opengl.impl.GLDrawableHelper; + +/** + * An implementation of {@link Window} which is customized for OpenGL + * use, and which implements the {@link + * javax.media.opengl.GLAutoDrawable} interface. For convenience, this + * window class guarantees that its OpenGL context is current inside + * the various EventListeners' callbacks (MouseListener, KeyListener, + * etc.). + */ +public class GLWindow extends Window implements GLAutoDrawable { + private Window window; + + /** Constructor. Do not call this directly -- use {@link + create()} instead. */ + protected GLWindow(Window window, GLCapabilities caps) { + this.window = window; + this.caps = caps; + window.addWindowListener(new WindowListener() { + public void windowResized(WindowEvent e) { + sendReshape = true; + } + + public void windowMoved(WindowEvent e) { + } + }); + } + + /** Creates a new GLWindow on the local display, screen 0, with a + dummy visual ID, and with the default GLCapabilities. */ + public static GLWindow create() { + return create(null, null); + } + + /** Creates a new GLWindow referring to the given window. */ + public static GLWindow create(Window window) { + return create(window, null); + } + + /** Creates a new GLWindow on the local display, screen 0, with a + dummy visual ID, and with the given GLCapabilities. */ + public static GLWindow create(GLCapabilities caps) { + return create(null, caps); + } + + /** Creates a new GLWindow referring to the given window, and with the given GLCapabilities. */ + public static GLWindow create(Window window, GLCapabilities caps) { + if (window == null) { + Display display = NewtFactory.createDisplay(null); // local display + Screen screen = NewtFactory.createScreen(display, 0); // screen 0 + window = NewtFactory.createWindow(screen, 0); // dummy VisualID + } + if (caps == null) { + caps = new GLCapabilities(); + } + + return new GLWindow(window, caps); + } + + public boolean isTerminalObject() { + shouldNotCallThis(); + return false; + } + + protected void createNative() { + shouldNotCallThis(); + } + + protected void closeNative() { + shouldNotCallThis(); + } + + public void close() { + if (context != null) { + if (context == GLContext.getCurrent()) { + context.release(); + } + context.destroy(); + } + if (drawable != null) { + drawable.destroy(); + } + + window.close(); + } + + public int getDisplayWidth() { + return window.getDisplayWidth(); + } + + public int getDisplayHeight() { + return window.getDisplayHeight(); + } + + public void pumpMessages(int eventMask) { + pumpMessagesWithEventMaskAction.eventMask = eventMask; + pumpMessagesImpl(pumpMessagesWithEventMaskAction); + } + + class PumpMessagesWithEventMaskAction implements Runnable { + private int eventMask; + + public void run() { + window.pumpMessages(eventMask); + } + } + private PumpMessagesWithEventMaskAction pumpMessagesWithEventMaskAction = new PumpMessagesWithEventMaskAction(); + + public void pumpMessages() { + pumpMessagesImpl(pumpMessagesAction); + } + + class PumpMessagesAction implements Runnable { + public void run() { + window.pumpMessages(); + } + } + private PumpMessagesAction pumpMessagesAction = new PumpMessagesAction(); + + private void pumpMessagesImpl(Runnable pumpMessagesAction) { + // pumpMessagesAction.run(); + + boolean autoSwapBuffer = helper.getAutoSwapBufferMode(); + helper.setAutoSwapBufferMode(false); + try { + helper.invokeGL(drawable, context, pumpMessagesAction, initAction); + } finally { + helper.setAutoSwapBufferMode(autoSwapBuffer); + } + + } + + protected void dispatchMessages(int eventMask) { + shouldNotCallThis(); + } + + public void setVisible(boolean visible) { + window.setVisible(visible); + if (visible && context == null) { + factory = GLDrawableFactory.getFactory(window); + drawable = factory.createGLDrawable(window, caps, null); + window.setVisible(true); + drawable.setRealized(true); + context = drawable.createContext(null); + System.out.println("Created context"); + } + } + + public void setSize(int width, int height) { + window.setSize(width, height); + } + + public void setPosition(int x, int y) { + window.setPosition(x, y); + } + + public boolean setFullscreen(boolean fullscreen) { + return window.setFullscreen(fullscreen); + } + + public boolean isVisible() { + return window.isVisible(); + } + + public int getX() { + return window.getX(); + } + + public int getY() { + return window.getY(); + } + + public int getWidth() { + return window.getWidth(); + } + + public int getHeight() { + return window.getHeight(); + } + + public boolean isFullscreen() { + return window.isFullscreen(); + } + + public void addMouseListener(MouseListener l) { + window.addMouseListener(l); + } + + public void removeMouseListener(MouseListener l) { + window.removeMouseListener(l); + } + + public MouseListener[] getMouseListeners() { + return window.getMouseListeners(); + } + + public void addKeyListener(KeyListener l) { + window.addKeyListener(l); + } + + public void removeKeyListener(KeyListener l) { + window.removeKeyListener(l); + } + + public KeyListener[] getKeyListeners() { + return window.getKeyListeners(); + } + + public void addWindowListener(WindowListener l) { + window.addWindowListener(l); + } + + public void removeWindowListener(WindowListener l) { + window.removeWindowListener(l); + } + + public WindowListener[] getWindowListeners() { + return window.getWindowListeners(); + } + + //---------------------------------------------------------------------- + // OpenGL-related methods and state + // + + private GLDrawableFactory factory; + private GLCapabilities caps; + private GLDrawable drawable; + private GLContext context; + private GLDrawableHelper helper = new GLDrawableHelper(); + // To make reshape events be sent immediately before a display event + private boolean sendReshape; + + public GLDrawableFactory getFactory() { + return factory; + } + + public GLContext getContext() { + return context; + } + + public GL getGL() { + GLContext ctx = getContext(); + if (ctx == null) { + return null; + } + return ctx.getGL(); + } + + public void setGL(GL gl) { + GLContext ctx = getContext(); + if (ctx != null) { + ctx.setGL(gl); + } + } + + public void addGLEventListener(GLEventListener listener) { + helper.addGLEventListener(listener); + } + + public void removeGLEventListener(GLEventListener listener) { + helper.removeGLEventListener(listener); + } + + public void display() { + pumpMessages(); + helper.invokeGL(drawable, context, displayAction, initAction); + } + + public void setAutoSwapBufferMode(boolean onOrOff) { + helper.setAutoSwapBufferMode(onOrOff); + } + + public boolean getAutoSwapBufferMode() { + return helper.getAutoSwapBufferMode(); + } + + public void swapBuffers() { + drawable.swapBuffers(); + } + + class InitAction implements Runnable { + public void run() { + helper.init(GLWindow.this); + } + } + private InitAction initAction = new InitAction(); + + class DisplayAction implements Runnable { + public void run() { + if (sendReshape) { + int width = getWidth(); + int height = getHeight(); + getGL().glViewport(0, 0, width, height); + helper.reshape(GLWindow.this, 0, 0, width, height); + sendReshape = false; + } + + helper.display(GLWindow.this); + } + } + + private DisplayAction displayAction = new DisplayAction(); + + //---------------------------------------------------------------------- + // GLDrawable methods that are not really needed + // + + public GLContext createContext(GLContext shareWith) { + return drawable.createContext(shareWith); + } + + public void setRealized(boolean realized) { + } + + public void destroy() { + close(); + } + + public GLCapabilities getChosenGLCapabilities() { + if (drawable == null) + return null; + + return drawable.getChosenGLCapabilities(); + } + + public void setChosenGLCapabilities(GLCapabilities caps) { + drawable.setChosenGLCapabilities(caps); + } + + public NativeWindow getNativeWindow() { + return drawable.getNativeWindow(); + } + + public int lockSurface() throws GLException { + return drawable.lockSurface(); + } + + public void unlockSurface() { + drawable.unlockSurface(); + } + + public boolean isSurfaceLocked() { + return drawable.isSurfaceLocked(); + } + + //---------------------------------------------------------------------- + // Internals only below this point + // + + private void shouldNotCallThis() { + throw new RuntimeException("Should not call this"); + } +} diff --git a/src/classes/com/sun/javafx/newt/InputEvent.java b/src/classes/com/sun/javafx/newt/InputEvent.java index e8644f953..dc36be34d 100644 --- a/src/classes/com/sun/javafx/newt/InputEvent.java +++ b/src/classes/com/sun/javafx/newt/InputEvent.java @@ -33,7 +33,7 @@ package com.sun.javafx.newt; -public abstract class InputEvent +public abstract class InputEvent extends Event { public static int SHIFT_MASK = 1 << 0; public static int CTRL_MASK = 1 << 1; @@ -41,18 +41,12 @@ public abstract class InputEvent public static int ALT_MASK = 1 << 3; public static int ALT_GRAPH_MASK = 1 << 5; - protected InputEvent(boolean sysEvent, Window source, long when, int modifiers) { + protected InputEvent(boolean sysEvent, int eventType, Window source, long when, int modifiers) { + super(sysEvent, eventType, source, when); this.consumed=false; - this.sysEvent=sysEvent; - this.source=source; - this.when=when; this.modifiers=modifiers; } - protected boolean isSysEvent() { - return sysEvent; - } - public void consume() { consumed=true; } @@ -63,9 +57,6 @@ public abstract class InputEvent public int getModifiers() { return modifiers; } - public long getWhen() { - return when; - } public boolean isAltDown() { return (modifiers&ALT_MASK)!=0; } @@ -83,13 +74,9 @@ public abstract class InputEvent } public String toString() { - return "InputEvent[sys:"+sysEvent+", source:"+source+", when:"+when+", modifiers:"+modifiers+"]"; + return "InputEvent[modifiers:"+modifiers+"]"; } - private boolean sysEvent, consumed; - private Window source; + private boolean consumed; private int modifiers; - private long when; - } - diff --git a/src/classes/com/sun/javafx/newt/KeyEvent.java b/src/classes/com/sun/javafx/newt/KeyEvent.java index 26e306687..92366bc93 100644 --- a/src/classes/com/sun/javafx/newt/KeyEvent.java +++ b/src/classes/com/sun/javafx/newt/KeyEvent.java @@ -35,9 +35,8 @@ package com.sun.javafx.newt; public class KeyEvent extends InputEvent { - protected KeyEvent(boolean sysEvent, int eventType, Window source, long when, int modifiers, int keyCode, char keyChar) { - super(sysEvent, source, when, modifiers); - this.eventType=eventType; + KeyEvent(boolean sysEvent, int eventType, Window source, long when, int modifiers, int keyCode, char keyChar) { + super(sysEvent, eventType, source, when, modifiers); this.keyCode=keyCode; this.keyChar=keyChar; } @@ -45,9 +44,6 @@ public class KeyEvent extends InputEvent this(false, eventType, source, when, modifiers, keyCode, keyChar); } - public int getEventType() { - return eventType; - } public char getKeyChar() { return keyChar; } @@ -56,8 +52,8 @@ public class KeyEvent extends InputEvent } public String toString() { - return "KeyEvent["+getEventTypeString(eventType)+ - ", code "+keyCode+", char "+keyChar+", isActionKey "+isActionKey()+" "+super.toString(); + return "KeyEvent["+getEventTypeString(getEventType())+ + ", code "+keyCode+", char "+keyChar+", isActionKey "+isActionKey()+", "+super.toString()+"]"; } public static String getEventTypeString(int type) { @@ -65,7 +61,7 @@ public class KeyEvent extends InputEvent case EVENT_KEY_PRESSED: return "EVENT_KEY_PRESSED"; case EVENT_KEY_RELEASED: return "EVENT_KEY_RELEASED"; case EVENT_KEY_TYPED: return "EVENT_KEY_TYPED"; - default: return "unknown"; + default: return "unknown (" + type + ")"; } } @@ -116,13 +112,12 @@ public class KeyEvent extends InputEvent return false; } - private int eventType; private int keyCode; private char keyChar; - public static final int EVENT_KEY_PRESSED = 1 << 0; - public static final int EVENT_KEY_RELEASED= 1 << 1; - public static final int EVENT_KEY_TYPED = 1 << 2; + public static final int EVENT_KEY_PRESSED = 300; + public static final int EVENT_KEY_RELEASED= 301; + public static final int EVENT_KEY_TYPED = 302; /* Virtual key codes. */ diff --git a/src/classes/com/sun/javafx/newt/MouseEvent.java b/src/classes/com/sun/javafx/newt/MouseEvent.java index 19b5f2b93..47d2803c0 100644 --- a/src/classes/com/sun/javafx/newt/MouseEvent.java +++ b/src/classes/com/sun/javafx/newt/MouseEvent.java @@ -41,8 +41,7 @@ public class MouseEvent extends InputEvent protected MouseEvent(boolean sysEvent, int eventType, Window source, long when, int modifiers, int x, int y, int clickCount, int button) { - super(sysEvent, source, when, modifiers); - this.eventType=eventType; + super(sysEvent, eventType, source, when, modifiers); this.x=x; this.y=y; this.clickCount=clickCount; @@ -52,9 +51,6 @@ public class MouseEvent extends InputEvent this(false, eventType, source, when, modifiers, x, y, clickCount, button); } - public int getEventType() { - return eventType; - } public int getButton() { return button; } @@ -69,8 +65,8 @@ public class MouseEvent extends InputEvent } public String toString() { - return "MouseEvent["+getEventTypeString(eventType)+ - ", "+x+"/"+y+", button "+button+", count "+clickCount+", "+super.toString(); + return "MouseEvent["+getEventTypeString(getEventType())+ + ", "+x+"/"+y+", button "+button+", count "+clickCount+", "+super.toString()+"]"; } public static String getEventTypeString(int type) { @@ -82,19 +78,17 @@ public class MouseEvent extends InputEvent case EVENT_MOUSE_RELEASED: return "EVENT_MOUSE_RELEASED"; case EVENT_MOUSE_MOVED: return "EVENT_MOUSE_MOVED"; case EVENT_MOUSE_DRAGGED: return "EVENT_MOUSE_DRAGGED"; - default: return "unknown"; + default: return "unknown (" + type + ")"; } } - private int eventType, x, y, clickCount, button; - - public static final int EVENT_MOUSE_CLICKED = 1 << 0; - public static final int EVENT_MOUSE_ENTERED = 1 << 1; - public static final int EVENT_MOUSE_EXITED = 1 << 2; - public static final int EVENT_MOUSE_PRESSED = 1 << 3; - public static final int EVENT_MOUSE_RELEASED = 1 << 4; - public static final int EVENT_MOUSE_MOVED = 1 << 5; - public static final int EVENT_MOUSE_DRAGGED = 1 << 6; + private int x, y, clickCount, button; + public static final int EVENT_MOUSE_CLICKED = 200; + public static final int EVENT_MOUSE_ENTERED = 201; + public static final int EVENT_MOUSE_EXITED = 202; + public static final int EVENT_MOUSE_PRESSED = 203; + public static final int EVENT_MOUSE_RELEASED = 204; + public static final int EVENT_MOUSE_MOVED = 205; + public static final int EVENT_MOUSE_DRAGGED = 206; } - diff --git a/src/classes/com/sun/javafx/newt/Window.java b/src/classes/com/sun/javafx/newt/Window.java index 1486091e8..a5bdf0806 100755 --- a/src/classes/com/sun/javafx/newt/Window.java +++ b/src/classes/com/sun/javafx/newt/Window.java @@ -43,6 +43,7 @@ public abstract class Window implements NativeWindow { public static final boolean DEBUG_MOUSE_EVENT = false; public static final boolean DEBUG_KEY_EVENT = false; + public static final boolean DEBUG_WINDOW_EVENT = false; public static final boolean DEBUG_IMPLEMENTATION = false; private static Class getWindowClass(String type) @@ -131,9 +132,9 @@ public abstract class Window implements NativeWindow public void pumpMessages() { int em = 0; - //if(windowistener.size()>0) em |= EventListener.WINDOW; - if(mouseListener.size()>0) em |= EventListener.MOUSE; - if(keyListener.size()>0) em |= EventListener.KEY; + if(windowListeners.size()>0) em |= EventListener.WINDOW; + if(mouseListeners.size()>0) em |= EventListener.MOUSE; + if(keyListeners.size()>0) em |= EventListener.KEY; pumpMessages(em); } @@ -271,25 +272,25 @@ public abstract class Window implements NativeWindow if(l == null) { return; } - ArrayList newMouseListeners = (ArrayList) mouseListener.clone(); + ArrayList newMouseListeners = (ArrayList) mouseListeners.clone(); newMouseListeners.add(l); - mouseListener = newMouseListeners; + mouseListeners = newMouseListeners; } public synchronized void removeMouseListener(MouseListener l) { if (l == null) { return; } - ArrayList newMouseListeners = (ArrayList) mouseListener.clone(); + ArrayList newMouseListeners = (ArrayList) mouseListeners.clone(); newMouseListeners.remove(l); - mouseListener = newMouseListeners; + mouseListeners = newMouseListeners; } public synchronized MouseListener[] getMouseListeners() { - return (MouseListener[]) mouseListener.toArray(); + return (MouseListener[]) mouseListeners.toArray(); } - private ArrayList mouseListener = new ArrayList(); + private ArrayList mouseListeners = new ArrayList(); private long lastMousePressed = 0; private int lastMouseClickCount = 0; public static final int ClickTimeout = 200; @@ -346,7 +347,7 @@ public abstract class Window implements NativeWindow ArrayList listeners = null; synchronized(this) { - listeners = mouseListener; + listeners = mouseListeners; } for(Iterator i = listeners.iterator(); i.hasNext(); ) { MouseListener l = (MouseListener) i.next(); @@ -389,21 +390,25 @@ public abstract class Window implements NativeWindow if(l == null) { return; } - keyListener.add(l); + ArrayList newKeyListeners = (ArrayList) keyListeners.clone(); + newKeyListeners.add(l); + keyListeners = newKeyListeners; } public synchronized void removeKeyListener(KeyListener l) { if (l == null) { return; } - keyListener.remove(l); + ArrayList newKeyListeners = (ArrayList) keyListeners.clone(); + newKeyListeners.remove(l); + keyListeners = newKeyListeners; } public synchronized KeyListener[] getKeyListeners() { - return (KeyListener[]) keyListener.toArray(); + return (KeyListener[]) keyListeners.toArray(); } - private ArrayList keyListener = new ArrayList(); + private ArrayList keyListeners = new ArrayList(); protected void sendKeyEvent(int eventType, int modifiers, int keyCode, char keyChar) { KeyEvent e = new KeyEvent(true, eventType, this, System.currentTimeMillis(), @@ -411,7 +416,11 @@ public abstract class Window implements NativeWindow if(DEBUG_KEY_EVENT) { System.out.println("sendKeyEvent: "+e); } - for(Iterator i = keyListener.iterator(); i.hasNext(); ) { + ArrayList listeners = null; + synchronized(this) { + listeners = keyListeners; + } + for(Iterator i = listeners.iterator(); i.hasNext(); ) { KeyListener l = (KeyListener) i.next(); switch(eventType) { case KeyEvent.EVENT_KEY_PRESSED: @@ -428,5 +437,56 @@ public abstract class Window implements NativeWindow } } } -} + // + // WindowListener Support + // + + private ArrayList windowListeners = new ArrayList(); + + public synchronized void addWindowListener(WindowListener l) { + if(l == null) { + return; + } + ArrayList newWindowListeners = (ArrayList) windowListeners.clone(); + newWindowListeners.add(l); + windowListeners = newWindowListeners; + } + + public synchronized void removeWindowListener(WindowListener l) { + if (l == null) { + return; + } + ArrayList newWindowListeners = (ArrayList) windowListeners.clone(); + newWindowListeners.remove(l); + windowListeners = newWindowListeners; + } + + public synchronized WindowListener[] getWindowListeners() { + return (WindowListener[]) windowListeners.toArray(); + } + + protected void sendWindowEvent(int eventType) { + WindowEvent e = new WindowEvent(true, eventType, this, System.currentTimeMillis()); + if(DEBUG_WINDOW_EVENT) { + System.out.println("sendWindowEvent: "+e); + } + ArrayList listeners = null; + synchronized(this) { + listeners = windowListeners; + } + for(Iterator i = listeners.iterator(); i.hasNext(); ) { + WindowListener l = (WindowListener) i.next(); + switch(eventType) { + case WindowEvent.EVENT_WINDOW_RESIZED: + l.windowResized(e); + break; + case WindowEvent.EVENT_WINDOW_MOVED: + l.windowMoved(e); + break; + default: + throw new RuntimeException("Unexpected window event type " + e.getEventType()); + } + } + } +} diff --git a/src/classes/com/sun/javafx/newt/WindowEvent.java b/src/classes/com/sun/javafx/newt/WindowEvent.java new file mode 100644 index 000000000..49cdb5497 --- /dev/null +++ b/src/classes/com/sun/javafx/newt/WindowEvent.java @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + */ + +package com.sun.javafx.newt; + +public class WindowEvent extends Event { + public static final int EVENT_WINDOW_RESIZED = 100; + public static final int EVENT_WINDOW_MOVED = 101; + + public WindowEvent(int eventType, Window source, long when) { + this(false, eventType, source, when); + } + + WindowEvent(boolean isSystemEvent, int eventType, Window source, long when) { + super(isSystemEvent, eventType, source, when); + } + + public static String getEventTypeString(int type) { + switch(type) { + case EVENT_WINDOW_RESIZED: return "WINDOW_RESIZED"; + case EVENT_WINDOW_MOVED: return "WINDOW_MOVED"; + default: return "unknown (" + type + ")"; + } + } + public String toString() { + return "WindowEvent["+getEventTypeString(getEventType()) + + ", " + super.toString() + "]"; + } +} diff --git a/src/classes/com/sun/javafx/newt/WindowListener.java b/src/classes/com/sun/javafx/newt/WindowListener.java new file mode 100644 index 000000000..b76202f01 --- /dev/null +++ b/src/classes/com/sun/javafx/newt/WindowListener.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name of Sun Microsystems, Inc. or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * + */ + +package com.sun.javafx.newt; + +public interface WindowListener extends EventListener { + public void windowResized(WindowEvent e); + public void windowMoved(WindowEvent e); +} diff --git a/src/classes/com/sun/javafx/newt/awt/AWTWindow.java b/src/classes/com/sun/javafx/newt/awt/AWTWindow.java index 1e0f8d6ee..780d7f488 100644 --- a/src/classes/com/sun/javafx/newt/awt/AWTWindow.java +++ b/src/classes/com/sun/javafx/newt/awt/AWTWindow.java @@ -71,9 +71,11 @@ public class AWTWindow extends Window { canvas.addMouseListener(listener); canvas.addMouseMotionListener(listener); canvas.addKeyListener(listener); + canvas.addComponentListener(listener); frame.add(canvas, BorderLayout.CENTER); frame.setSize(width, height); frame.setLocation(x, y); + frame.addComponentListener(new MoveListener()); } }); } @@ -126,6 +128,8 @@ public class AWTWindow extends Window { } public void setPosition(final int x, final int y) { + this.x = x; + this.y = y; runOnEDT(new Runnable() { public void run() { frame.setLocation(x, y); @@ -153,18 +157,40 @@ public class AWTWindow extends Window { } } if (w != null) { - if (w.isMouseEvent()) { - if ((eventMask & com.sun.javafx.newt.EventListener.MOUSE) != 0) { - MouseEvent e = (MouseEvent) w.getEvent(); - sendMouseEvent(w.getType(), convertModifiers(e), - e.getX(), e.getY(), convertButton(e)); - } - } else { - if ((eventMask & com.sun.javafx.newt.EventListener.KEY) != 0) { - KeyEvent e = (KeyEvent) w.getEvent(); - sendKeyEvent(w.getType(), convertModifiers(e), - e.getKeyCode(), e.getKeyChar()); - } + switch (w.getType()) { + case com.sun.javafx.newt.WindowEvent.EVENT_WINDOW_RESIZED: + case com.sun.javafx.newt.WindowEvent.EVENT_WINDOW_MOVED: + if ((eventMask & com.sun.javafx.newt.EventListener.WINDOW) != 0) { + sendWindowEvent(w.getType()); + } + break; + + case com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_CLICKED: + case com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_ENTERED: + case com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_EXITED: + case com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_PRESSED: + case com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_RELEASED: + case com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_MOVED: + case com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_DRAGGED: + if ((eventMask & com.sun.javafx.newt.EventListener.MOUSE) != 0) { + MouseEvent e = (MouseEvent) w.getEvent(); + sendMouseEvent(w.getType(), convertModifiers(e), + e.getX(), e.getY(), convertButton(e)); + } + break; + + case com.sun.javafx.newt.KeyEvent.EVENT_KEY_PRESSED: + case com.sun.javafx.newt.KeyEvent.EVENT_KEY_RELEASED: + case com.sun.javafx.newt.KeyEvent.EVENT_KEY_TYPED: + if ((eventMask & com.sun.javafx.newt.EventListener.KEY) != 0) { + KeyEvent e = (KeyEvent) w.getEvent(); + sendKeyEvent(w.getType(), convertModifiers(e), + e.getKeyCode(), e.getKeyChar()); + } + break; + + default: + throw new RuntimeException("Unknown event type " + w.getType()); } if(DEBUG_MOUSE_EVENT) { System.out.println("dispatchMessages: in event:"+w.getEvent()); @@ -205,31 +231,26 @@ public class AWTWindow extends Window { } } - private void enqueueEvent(boolean isMouseEvent, int type, InputEvent e) { - if(DEBUG_MOUSE_EVENT) { - System.out.println("enqueueEvent: mouse"+isMouseEvent+", event: "+e); - } - AWTEventWrapper wrapper = new AWTEventWrapper(isMouseEvent,type, e); + private void enqueueEvent(int type, InputEvent e) { + AWTEventWrapper wrapper = new AWTEventWrapper(type, e); synchronized(this) { events.add(wrapper); } } + private static final int WINDOW_EVENT = 1; + private static final int KEY_EVENT = 2; + private static final int MOUSE_EVENT = 3; + static class AWTEventWrapper { - boolean isMouseEvent; int type; InputEvent e; - AWTEventWrapper(boolean isMouseEvent, int type, InputEvent e) { - this.isMouseEvent = isMouseEvent; + AWTEventWrapper(int type, InputEvent e) { this.type = type; this.e = e; } - public boolean isMouseEvent() { - return isMouseEvent; - } - public int getType() { return type; } @@ -239,46 +260,79 @@ public class AWTWindow extends Window { } } - class Listener implements MouseListener, MouseMotionListener, KeyListener { + class MoveListener implements ComponentListener { + public void componentResized(ComponentEvent e) { + } + + public void componentMoved(ComponentEvent e) { + x = frame.getX(); + y = frame.getY(); + enqueueEvent(com.sun.javafx.newt.WindowEvent.EVENT_WINDOW_MOVED, null); + } + + public void componentShown(ComponentEvent e) { + } + + public void componentHidden(ComponentEvent e) { + } + + } + + class Listener implements ComponentListener, MouseListener, MouseMotionListener, KeyListener { + public void componentResized(ComponentEvent e) { + width = canvas.getWidth(); + height = canvas.getHeight(); + enqueueEvent(com.sun.javafx.newt.WindowEvent.EVENT_WINDOW_RESIZED, null); + } + + public void componentMoved(ComponentEvent e) { + } + + public void componentShown(ComponentEvent e) { + } + + public void componentHidden(ComponentEvent e) { + } + public void mouseClicked(MouseEvent e) { // We ignore these as we synthesize them ourselves out of // mouse pressed and released events } public void mouseEntered(MouseEvent e) { - enqueueEvent(true, com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_ENTERED, e); + enqueueEvent(com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_ENTERED, e); } public void mouseExited(MouseEvent e) { - enqueueEvent(true, com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_EXITED, e); + enqueueEvent(com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_EXITED, e); } public void mousePressed(MouseEvent e) { - enqueueEvent(true, com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_PRESSED, e); + enqueueEvent(com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_PRESSED, e); } public void mouseReleased(MouseEvent e) { - enqueueEvent(true, com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_RELEASED, e); + enqueueEvent(com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_RELEASED, e); } public void mouseMoved(MouseEvent e) { - enqueueEvent(true, com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_MOVED, e); + enqueueEvent(com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_MOVED, e); } public void mouseDragged(MouseEvent e) { - enqueueEvent(true, com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_DRAGGED, e); + enqueueEvent(com.sun.javafx.newt.MouseEvent.EVENT_MOUSE_DRAGGED, e); } public void keyPressed(KeyEvent e) { - enqueueEvent(false, com.sun.javafx.newt.KeyEvent.EVENT_KEY_PRESSED, e); + enqueueEvent(com.sun.javafx.newt.KeyEvent.EVENT_KEY_PRESSED, e); } public void keyReleased(KeyEvent e) { - enqueueEvent(false, com.sun.javafx.newt.KeyEvent.EVENT_KEY_RELEASED, e); + enqueueEvent(com.sun.javafx.newt.KeyEvent.EVENT_KEY_RELEASED, e); } public void keyTyped(KeyEvent e) { - enqueueEvent(false, com.sun.javafx.newt.KeyEvent.EVENT_KEY_TYPED, e); + enqueueEvent(com.sun.javafx.newt.KeyEvent.EVENT_KEY_TYPED, e); } } } diff --git a/src/classes/com/sun/javafx/newt/windows/WindowsWindow.java b/src/classes/com/sun/javafx/newt/windows/WindowsWindow.java index 00ecd5973..9e0df6c71 100755 --- a/src/classes/com/sun/javafx/newt/windows/WindowsWindow.java +++ b/src/classes/com/sun/javafx/newt/windows/WindowsWindow.java @@ -154,11 +154,13 @@ public class WindowsWindow extends Window { private void sizeChanged(int newWidth, int newHeight) { width = newWidth; height = newHeight; + sendWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); } private void positionChanged(int newX, int newY) { x = newX; y = newY; + sendWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); } private void windowClosed() { diff --git a/src/classes/com/sun/javafx/newt/x11/X11Window.java b/src/classes/com/sun/javafx/newt/x11/X11Window.java index 290b1e506..05d9abcb9 100755 --- a/src/classes/com/sun/javafx/newt/x11/X11Window.java +++ b/src/classes/com/sun/javafx/newt/x11/X11Window.java @@ -138,6 +138,7 @@ public class X11Window extends Window { nfs_width=width; nfs_height=height; } + sendWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED); } private void positionChanged(int newX, int newY) { @@ -147,6 +148,7 @@ public class X11Window extends Window { nfs_x=x; nfs_y=y; } + sendWindowEvent(WindowEvent.EVENT_WINDOW_MOVED); } private void windowCreated(long visualID, long windowHandle) { |