diff options
author | Sven Gothel <[email protected]> | 2010-05-05 15:30:33 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2010-05-05 15:30:33 +0200 |
commit | 599bf2dea34e82e32a7b443900a0c24c92b96b80 (patch) | |
tree | 083be99b7dbc800c7369b3b2559601d492252e6a /src/newt/classes | |
parent | ccc30b050b98d3ac19779e1dc2d0fcee68331863 (diff) |
Newt: Add generic NativeWindow parenting, inclusive using an AWT Component as a parent
Diffstat (limited to 'src/newt/classes')
3 files changed, 247 insertions, 3 deletions
diff --git a/src/newt/classes/com/jogamp/newt/NewtFactory.java b/src/newt/classes/com/jogamp/newt/NewtFactory.java index 2d5c10c52..531d686b2 100755 --- a/src/newt/classes/com/jogamp/newt/NewtFactory.java +++ b/src/newt/classes/com/jogamp/newt/NewtFactory.java @@ -33,10 +33,13 @@ package com.jogamp.newt; +import com.jogamp.common.util.ReflectionUtil; import javax.media.nativewindow.*; import java.util.ArrayList; import java.util.Iterator; import com.jogamp.common.jvm.JVMUtil; +import com.jogamp.newt.event.WindowAdapter; +import com.jogamp.newt.event.WindowEvent; public abstract class NewtFactory { // Work-around for initialization order problems on Mac OS X @@ -100,18 +103,87 @@ public abstract class NewtFactory { } /** - * Create a Window entity, incl native creation + * Create a top level Window entity, incl native creation */ public static Window createWindow(Screen screen, Capabilities caps) { return Window.create(NativeWindowFactory.getNativeWindowType(true), 0, screen, caps, false); } + /** + * Create a top level Window entity, incl native creation + */ public static Window createWindow(Screen screen, Capabilities caps, boolean undecorated) { return Window.create(NativeWindowFactory.getNativeWindowType(true), 0, screen, caps, undecorated); } - public static Window createWindow(long parentWindowHandle, Screen screen, Capabilities caps, boolean undecorated) { - return Window.create(NativeWindowFactory.getNativeWindowType(true), parentWindowHandle, screen, caps, undecorated); + /** + * Create a child Window entity attached to the given parent, incl native creation<br> + * <p> + * In case <code>parentWindowObject</code> is a {@link javax.media.nativewindow.NativeWindow},<br> + * we create a child {@link com.jogamp.newt.Window}, + * utilizing {@link com.jogamp.newt.NewtFactory#createWindow(long, com.jogamp.newt.Screen, com.jogamp.newt.Capabilities)}, + * passing the parent's native window handle retrieved via {@link javax.media.nativewindow.NativeWindow#getWindowHandle()}.<br></p> + * <p> + * In case <code>parentWindowObject</code> is even a {@link com.jogamp.newt.Window}, the following applies:<br> + * {@link com.jogamp.newt.event.WindowEvent#EVENT_WINDOW_RESIZED} is not propagated to the child window for e.g. layout<br>, + * you have to add an appropriate {@link com.jogamp.newt.event.WindowListener} for this use case.<br> + * However, {@link com.jogamp.newt.event.WindowEvent#EVENT_WINDOW_DESTROY_NOTIFY} is propagated to the child window, so it will be closed properly.<br> + * In case <code>parentWindowObject</code> is a different {@javax.media.nativewindow.NativeWindow} implementation,<br> + * you have to handle all events appropriatly.<br></p> + * <p> + * In case <code>parentWindowObject</code> is a {@link java.awt.Component},<br> + * we utilize the {@link com.jogamp.newt.impl.awt.AWTNewtFactory#createNativeChildWindow(Object, com.jogamp.newt.Screen, com.jogamp.newt.Capabilities)} + * factory method.<br> + * The factory adds a {@link com.jogamp.newt.event.WindowListener} to propagate {@link com.jogamp.newt.event.WindowEvent}'s so + * your NEWT Window integrates into the AWT layout.<br></p> + * + * @param parentWindowObject either a NativeWindow or java.awt.Component + * + * @see com.jogamp.newt.NewtFactory#createWindow(long, com.jogamp.newt.Screen, com.jogamp.newt.Capabilities) + * @see com.jogamp.newt.impl.awt.AWTNewtFactory#createNativeChildWindow(Object, com.jogamp.newt.Screen, com.jogamp.newt.Capabilities) + */ + public static Window createWindow(Object parentWindowObject, Screen screen, Capabilities caps) { + if(null==parentWindowObject) { + throw new RuntimeException("Null parentWindowObject"); + } + if(parentWindowObject instanceof NativeWindow) { + NativeWindow nativeParentWindow = (NativeWindow) parentWindowObject; + nativeParentWindow.lockSurface(); + long parentWindowHandle = nativeParentWindow.getWindowHandle(); + nativeParentWindow.unlockSurface(); + final Window win = createWindow(parentWindowHandle, screen, caps); + if ( nativeParentWindow instanceof Window) { + final Window f_nativeParentWindow = (Window) nativeParentWindow ; + f_nativeParentWindow.addWindowListener(new WindowAdapter() { + public void windowDestroyNotify(WindowEvent e) { + win.sendEvent(e); + } + }); + } + return win; + } else { + if(ReflectionUtil.instanceOf(parentWindowObject, "java.awt.Component")) { + if(ReflectionUtil.isClassAvailable("com.jogamp.newt.impl.awt.AWTNewtFactory")) { + return (Window) ReflectionUtil.callStaticMethod("com.jogamp.newt.impl.awt.AWTNewtFactory", + "createNativeChildWindow", + new Class[] { Object.class, Screen.class, Capabilities.class }, + new Object[] { parentWindowObject, screen, caps } ); + } + } + } + throw new RuntimeException("No NEWT child Window factory method for parent object: "+parentWindowObject); + } + + /** + * Create a child Window entity attached to the given parent, incl native creation<br> + * + * @param parentWindowObject the native parent window handle + */ + public static Window createWindow(long parentWindowHandle, Screen screen, Capabilities caps) { + if(0==parentWindowHandle) { + throw new RuntimeException("Null parentWindowHandle"); + } + return Window.create(NativeWindowFactory.getNativeWindowType(true), parentWindowHandle, screen, caps, true); } /** diff --git a/src/newt/classes/com/jogamp/newt/event/awt/AWTParentWindowAdapter.java b/src/newt/classes/com/jogamp/newt/event/awt/AWTParentWindowAdapter.java new file mode 100644 index 000000000..49d9bf0f2 --- /dev/null +++ b/src/newt/classes/com/jogamp/newt/event/awt/AWTParentWindowAdapter.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ +package com.jogamp.newt.event.awt; + +/** + * Specialized parent/client adapter, + * where the NEWT child window really gets resized, + * and the parent move window event gets discarded. */ +public class AWTParentWindowAdapter extends AWTWindowAdapter +{ + public AWTParentWindowAdapter(com.jogamp.newt.Window downstream) { + super(downstream); + } + + public void componentResized(java.awt.event.ComponentEvent e) { + // need to really resize the NEWT child window + java.awt.Component comp = e.getComponent(); + newtWindow.setSize(comp.getWidth(), comp.getHeight()); + } + + public void componentMoved(java.awt.event.ComponentEvent e) { + // no propagation to NEWT child window + } + + public void windowActivated(java.awt.event.WindowEvent e) { + // no propagation to NEWT child window ?? FIXME: Maybe yes in case of a 100% Opaque one ? + } + + public void windowDeactivated(java.awt.event.WindowEvent e) { + // no propagation to NEWT child window ?? FIXME: Maybe yes in case of a 100% Opaque one ? + } +} + diff --git a/src/newt/classes/com/jogamp/newt/impl/awt/AWTNewtFactory.java b/src/newt/classes/com/jogamp/newt/impl/awt/AWTNewtFactory.java new file mode 100644 index 000000000..f3296d9d5 --- /dev/null +++ b/src/newt/classes/com/jogamp/newt/impl/awt/AWTNewtFactory.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2010 Sven Gothel. 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 Sven Gothel 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 + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.newt.impl.awt; + +import java.awt.GraphicsConfiguration; +import java.awt.GraphicsDevice; +import java.awt.Component; +import java.awt.Canvas; + +import javax.media.opengl.*; +import javax.media.nativewindow.*; +import javax.media.nativewindow.awt.*; + +import com.jogamp.newt.event.awt.AWTParentWindowAdapter; +import com.jogamp.newt.Screen; +import com.jogamp.newt.Window; +import com.jogamp.newt.NewtFactory; + +public class AWTNewtFactory { + + /** + * Wraps an AWT component into a {@link javax.media.nativewindow.NativeWindow} utilizing the {@link javax.media.nativewindow.NativeWindowFactory},<br> + * using a configuration agnostic dummy {@link javax.media.nativewindow.DefaultGraphicsConfiguration}.<br> + * <p> + * The actual wrapping implementation is {@link com.jogamp.nativewindow.impl.jawt.JAWTWindow}.<br></p> + * <p> + * Purpose of this wrapping is to access the AWT window handle,<br> + * not to actually render into it.<br> + * Hence the dummy configuration only.</p> + * + * @param awtCompObject must be of type java.awt.Component + */ + public static NativeWindow getNativeWindow(Object awtCompObject) { + if(null==awtCompObject) { + throw new NativeWindowException("Null AWT Component"); + } + if( ! (awtCompObject instanceof java.awt.Component) ) { + throw new NativeWindowException("AWT Component not a java.awt.Component"); + } + java.awt.Component awtComp = (java.awt.Component) awtCompObject; + DefaultGraphicsDevice dummyDevice = new DefaultGraphicsDevice("AWTNewtBridge"); + DefaultGraphicsScreen dummyScreen = new DefaultGraphicsScreen(dummyDevice, 0); + Capabilities dummyCaps = new Capabilities(); + DefaultGraphicsConfiguration dummyConfig = new DefaultGraphicsConfiguration(dummyScreen, dummyCaps, dummyCaps); + NativeWindow awtNative = NativeWindowFactory.getNativeWindow(awtComp, dummyConfig); + return awtNative; + } + + /** + * Creates a native NEWT child window to a AWT parent window.<br> + * <p> + * First we create a {@link javax.media.nativewindow.NativeWindow} presentation of the given {@link java.awt.Component}, + * utilizing {@link #getNativeWindow(java.awt.Component)}.<br> + * The actual wrapping implementation is {@link com.jogamp.nativewindow.impl.jawt.JAWTWindow}.<br></p> + * <p> + * Second we create a child {@link com.jogamp.newt.Window}, utilizing {@link com.jogamp.newt.NewtFactory#createWindow(long, com.jogamp.newt.Screen, com.jogamp.newt.Capabilities)}, passing the AWT parent's native window handle retrieved via {@link com.jogamp.nativewindow.impl.jawt.JAWTWindow#getWindowHandle()}.<br></p> + * <p> + * Third we attach a {@link com.jogamp.newt.event.awt.AWTParentWindowAdapter} to the given AWT component.<br> + * The adapter passes window related events to our new child window, look at the implementation<br></p> + * + * @param awtParentObject must be of type java.awt.Component + */ + public static Window createNativeChildWindow(Object awtParentObject, Screen newtScreen, Capabilities newtCaps) { + NativeWindow parent = getNativeWindow(awtParentObject); // also checks java.awt.Component type + java.awt.Component awtParent = (java.awt.Component) awtParentObject; + if(null==parent) { + throw new NativeWindowException("Null NativeWindow from parent: "+awtParent); + } + parent.lockSurface(); + long windowHandle = parent.getWindowHandle(); + parent.unlockSurface(); + if(0==windowHandle) { + throw new NativeWindowException("Null window handle: "+parent); + } + Window window = NewtFactory.createWindow(windowHandle, newtScreen, newtCaps); + new AWTParentWindowAdapter(window).addTo(awtParent); + return window; + } +} + |