29package com.jogamp.newt.swt;
31import com.jogamp.common.util.locks.RecursiveLock;
32import com.jogamp.nativewindow.AbstractGraphicsConfiguration;
33import com.jogamp.nativewindow.AbstractGraphicsDevice;
34import com.jogamp.nativewindow.AbstractGraphicsScreen;
35import com.jogamp.nativewindow.Capabilities;
36import com.jogamp.nativewindow.CapabilitiesImmutable;
37import com.jogamp.nativewindow.GraphicsConfigurationFactory;
38import com.jogamp.nativewindow.NativeSurface;
39import com.jogamp.nativewindow.NativeWindow;
40import com.jogamp.nativewindow.NativeWindowException;
41import com.jogamp.nativewindow.NativeWindowFactory;
42import com.jogamp.nativewindow.NativeWindowHolder;
43import com.jogamp.nativewindow.SurfaceUpdatedListener;
44import com.jogamp.nativewindow.WindowClosingProtocol;
45import com.jogamp.nativewindow.util.Insets;
46import com.jogamp.nativewindow.util.InsetsImmutable;
47import com.jogamp.nativewindow.util.Point;
48import com.jogamp.opengl.GLCapabilities;
50import jogamp.nativewindow.macosx.OSXUtil;
51import jogamp.newt.Debug;
52import jogamp.newt.swt.SWTEDTUtil;
54import org.eclipse.swt.SWT;
55import org.eclipse.swt.SWTException;
56import org.eclipse.swt.graphics.Color;
57import org.eclipse.swt.widgets.Canvas;
58import org.eclipse.swt.widgets.Composite;
59import org.eclipse.swt.widgets.Event;
60import org.eclipse.swt.widgets.Listener;
62import com.jogamp.nativewindow.swt.SWTAccessor;
63import com.jogamp.newt.Display;
64import com.jogamp.newt.Window;
65import com.jogamp.newt.event.WindowEvent;
66import com.jogamp.newt.util.EDTUtil;
75 private static final boolean DEBUG = Debug.debug(
"Window");
77 private final int iHashCode;
82 private volatile org.eclipse.swt.graphics.Rectangle clientAreaPixels, clientAreaWindow;
84 private volatile float[] pixelScale =
new float[] { 1f, 1f };
86 private volatile SWTNativeWindow nativeWindow;
87 private volatile Window newtChild =
null;
88 private volatile boolean newtChildReady =
false;
89 private volatile boolean postSetSize =
false;
90 private volatile boolean postSetPos =
false;
108 parent.getDisplay().syncExec(
new Runnable() {
117 private final String shortName() {
return "NewtCanvasSWT("+toHexString(iHashCode)+
")"; }
132 super(parent, style | SWT.NO_BACKGROUND);
133 iHashCode = this.hashCode();
138 clientAreaWindow = getClientArea();
139 if( 0 < clientAreaWindow.width && 0 < clientAreaWindow.height ) {
140 pixelScale[0] = clientAreaPixels.width / clientAreaWindow.width;
141 pixelScale[1] = clientAreaPixels.height / clientAreaWindow.height;
153 setBackground(
new Color(parent.getDisplay(), 255, 255, 255));
160 System.err.println(
"NewtCanvasSWT: "+
161 ", ("+Thread.currentThread().getName()+
"): newtChildReady "+newtChildReady+
162 ", pixel "+clientAreaPixels.x+
"/"+clientAreaPixels.y+
" "+clientAreaPixels.width+
"x"+clientAreaPixels.height+
163 ", window "+clientAreaWindow.x+
"/"+clientAreaWindow.y+
" "+clientAreaWindow.width+
"x"+clientAreaWindow.height+
164 ", scale "+pixelScale[0]+
"/"+pixelScale[1]+
165 " - surfaceHandle 0x"+Long.toHexString(nsh));
168 addListener (SWT.Paint, swtListener);
169 addListener (SWT.Move, swtListener);
170 addListener (SWT.Show, swtListener);
171 addListener (SWT.Hide, swtListener);
172 addListener (SWT.Resize, swtListener);
173 addListener (SWT.Dispose, swtListener);
174 addListener (SWT.Activate, swtListener);
175 addListener (SWT.Deactivate, swtListener);
176 addListener (SWT.FocusIn, swtListener);
177 addListener (SWT.FocusOut, swtListener);
199 private final void setNewtChildSize(
final org.eclipse.swt.graphics.Rectangle r) {
204 newtChild.
setSize(r.width, r.height);
220 private final int newtScaleUp(
final int scaleUp,
final int passthrough) {
221 if( !SWTAccessor.isOSX ) {
222 return SWTAccessor.deviceZoomScaleUp(scaleUp);
227 private final Listener swtListener =
new Listener () {
229 public void handleEvent (
final Event event) {
230 switch (event.type) {
233 System.err.println(shortName()+
".Event.PAINT, "+event);
239 setNewtChildSize(clientAreaWindow);
243 newtChild.
setPosition(clientAreaWindow.x, clientAreaWindow.y);
246 newtChild.
windowRepaint(0, 0, clientAreaPixels.width, clientAreaPixels.height);
251 System.err.println(shortName()+
".Event.MOVE, "+event);
257 System.err.println(shortName()+
".Event.SHOW, "+event);
260 if( newtChildReady ) {
266 System.err.println(shortName()+
".Event.HIDE, "+event);
269 if( newtChildReady ) {
275 System.err.println(shortName()+
".Event.RESIZE, "+event);
287 System.err.println(shortName()+
".Event.DISPOSE, "+event);
295 System.err.println(shortName()+
".Event.ACTIVATE, "+event);
301 System.err.println(shortName()+
".Event.DEACTIVATE, "+event);
307 System.err.println(shortName()+
".Event.FOCUS_IN, "+event);
310 if( newtChildReady ) {
316 System.err.println(shortName()+
".Event.FOCUS_OUT, "+event);
323 System.err.println(shortName()+
".Event.misc: "+event.type+
", "+event);
331 public void setBounds(
final int x,
final int y,
final int width,
final int height) {
332 super.setBounds(x, y, width, height);
334 System.err.println(shortName()+
".setBounds: "+x+
"/"+y+
" "+width+
"x"+height);
342 if(
null != nativeWindow ) {
346 final org.eclipse.swt.graphics.Rectangle nClientAreaWindow = clientAreaWindow;
347 if(0 >= nClientAreaWindow.width || 0 >= nClientAreaWindow.height) {
357 System.err.println(shortName()+
".validateNative() windowHandle 0x"+Long.toHexString(nativeWindowHandle)+
", visualID 0x"+Integer.toHexString(visualID)+
", valid "+visualIDValid);
359 if( visualIDValid ) {
366 System.err.println(shortName()+
".validateNative() factory: "+factory+
", windowHandle 0x"+Long.
toHexString(nativeWindowHandle)+
", visualID 0x"+Integer.toHexString(visualID)+
", chosen config: "+config);
369 if (
null == config) {
373 nativeWindow =
new SWTNativeWindow(config, nativeWindowHandle);
374 reparentWindow(
true );
376 return null != nativeWindow;
380 final org.eclipse.swt.graphics.Rectangle oClientAreaWindow = clientAreaWindow;
382 final org.eclipse.swt.graphics.Rectangle nClientAreaWindow = getClientArea();
383 final boolean sizeChanged, posChanged;
385 sizeChanged = nClientAreaWindow.width != oClientAreaWindow.width || nClientAreaWindow.height != oClientAreaWindow.height;
386 posChanged = nClientAreaWindow.x != oClientAreaWindow.x || nClientAreaWindow.y != oClientAreaWindow.y;
387 if( sizeChanged || posChanged ) {
388 clientAreaPixels = nClientAreaPixels;
389 clientAreaWindow = nClientAreaWindow;
390 if( 0 < nClientAreaWindow.width && 0 < nClientAreaWindow.height ) {
391 pixelScale[0] = nClientAreaPixels.width / nClientAreaWindow.width;
392 pixelScale[1] = nClientAreaPixels.height / nClientAreaWindow.height;
401 System.err.println(shortName()+
".updatePosSizeCheck: sizeChanged "+sizeChanged+
", posChanged "+posChanged+
402 ", ("+Thread.currentThread().getName()+
"): newtChildReady "+newtChildReady+
403 ", pixel "+nClientAreaPixels.x+
"/"+nClientAreaPixels.y+
" "+nClientAreaPixels.width+
"x"+nClientAreaPixels.height+
404 ", window "+nClientAreaWindow.x+
"/"+nClientAreaWindow.y+
" "+nClientAreaWindow.width+
"x"+nClientAreaWindow.height+
405 ", scale "+pixelScale[0]+
"/"+pixelScale[1]+
406 " - surfaceHandle 0x"+Long.toHexString(nsh));
409 if( newtChildReady ) {
410 setNewtChildSize(nClientAreaWindow);
417 if( newtChildReady ) {
418 newtChild.
setPosition(nClientAreaWindow.x, nClientAreaWindow.y);
424 System.err.println(shortName()+
".updatePosSizeCheck.X END");
450 throw new SWTException(
"Invalid thread access");
452 removeListener (SWT.Paint, swtListener);
453 removeListener (SWT.Move, swtListener);
454 removeListener (SWT.Show, swtListener);
455 removeListener (SWT.Hide, swtListener);
456 removeListener (SWT.Resize, swtListener);
457 removeListener (SWT.Dispose, swtListener);
458 removeListener (SWT.Activate, swtListener);
459 removeListener (SWT.Deactivate, swtListener);
460 removeListener (SWT.FocusIn, swtListener);
461 removeListener (SWT.FocusOut, swtListener);
463 if(
null != newtChild ) {
468 configureNewtChild(
false);
505 return null!=newtChild ;
508 boolean isFullscreen() {
535 throw new SWTException(
"Invalid thread access");
539 final Window prevChild = newtChild;
541 System.err.println(shortName()+
".setNEWTChild.0: win "+newtWinHandleToHexString(prevChild)+
" -> "+newtWinHandleToHexString(newChild));
544 if(
null != newtChild) {
545 reparentWindow(
false );
549 newtChild = newChild;
550 if(
null != nativeWindow &&
null != newChild) {
551 reparentWindow(
true );
563 return super.setParent(parent);
566 void configureNewtChild(
final boolean attach) {
567 newtChildReady = attach;
568 if(
null != newtChild ) {
579 void reparentWindow(
final boolean add) {
580 if(
null == newtChild ) {
584 System.err.println(shortName()+
".reparentWindow.0: add="+add+
", win "+newtWinHandleToHexString(newtChild)+
", EDTUtil: cur "+newtChild.
getScreen().
getDisplay().
getEDTUtil());
594 final EDTUtil edtUtil =
new SWTEDTUtil(newtDisplay, getDisplay());
596 newtDisplay.setEDTUtil( edtUtil );
599 setNewtChildSize(clientAreaWindow);
600 newtChild.
reparentWindow(nativeWindow, -1, -1, Window.REPARENT_HINT_BECOMES_VISIBLE);
601 newtChild.
setPosition(clientAreaWindow.x, clientAreaWindow.y);
603 configureNewtChild(
true);
611 configureNewtChild(
false);
616 System.err.println(shortName()+
".reparentWindow.X: add="+add+
", win "+newtWinHandleToHexString(newtChild)+
", EDTUtil: cur "+newtChild.
getScreen().
getDisplay().
getEDTUtil());
620 private final void requestFocusNEWTChild() {
621 if( newtChildReady ) {
630 requestFocusNEWTChild();
636 private final long nativeWindowHandle;
640 this.config = config;
641 this.nativeWindowHandle = nativeWindowHandle;
643 this.insets = OSXUtil.GetInsets(nativeWindowHandle);
645 this.insets =
new Insets(0, 0, 0, 0);
650 public RecursiveLock getLock() {
return null; }
653 public int lockSurface() throws NativeWindowException, RuntimeException {
654 return NativeSurface.LOCK_SUCCESS;
658 public void unlockSurface() { }
661 public boolean isSurfaceLockedByOtherThread() {
666 public Thread getSurfaceLockOwner() {
671 public boolean surfaceSwap() {
676 public void addSurfaceUpdatedListener(
final SurfaceUpdatedListener l) { }
679 public void addSurfaceUpdatedListener(
final int index,
final SurfaceUpdatedListener l)
throws IndexOutOfBoundsException {
683 public void removeSurfaceUpdatedListener(
final SurfaceUpdatedListener l) { }
686 public long getSurfaceHandle() {
701 public int getWidth() {
702 return newtScaleUp(clientAreaWindow.width, clientAreaWindow.width);
706 public int getHeight() {
707 return newtScaleUp(clientAreaWindow.height, clientAreaWindow.height);
712 return new com.jogamp.nativewindow.util.
Rectangle(getX(), getY(), getWidth(), getHeight());
717 return new com.jogamp.nativewindow.util.
Rectangle(getX(), getY(),
718 getSurfaceWidth(), getSurfaceHeight());
722 public final int[] convertToWindowUnits(
final int[] pixelUnitsAndResult) {
723 pixelUnitsAndResult[0] /= pixelScale[0];
724 pixelUnitsAndResult[1] /= pixelScale[1];
725 return pixelUnitsAndResult;
729 public final int[] convertToPixelUnits(
final int[] windowUnitsAndResult) {
730 windowUnitsAndResult[0] *= pixelScale[0];
731 windowUnitsAndResult[1] *= pixelScale[1];
732 return windowUnitsAndResult;
736 public int getSurfaceWidth() {
737 return newtScaleUp(clientAreaWindow.width, clientAreaPixels.width);
741 public int getSurfaceHeight() {
742 return newtScaleUp(clientAreaWindow.height, clientAreaPixels.height);
749 public AbstractGraphicsConfiguration getGraphicsConfiguration() {
754 public long getDisplayHandle() {
759 public int getScreenIndex() {
764 public void surfaceUpdated(
final Object updater,
final NativeSurface ns,
final long when) { }
767 public void destroy() { }
770 public NativeWindow getParent() {
775 public long getWindowHandle() {
776 return nativeWindowHandle;
780 public InsetsImmutable getInsets() {
785 public Point getLocationOnScreen(
final Point point) {
786 final Point los = NativeWindowFactory.getLocationOnScreen(
this);
788 return point.translate(los);
795 public boolean hasFocus() {
796 return isFocusControl();
800 static String newtWinHandleToHexString(
final Window w) {
801 return null != w ? toHexString(w.getWindowHandle()) :
"nil";
803 static String toHexString(
final long l) {
804 return "0x"+Long.toHexString(l);
Specifies a set of capabilities that a window's rendering context must support, such as color depth p...
Provides the mechanism by which the graphics configuration for a window can be chosen before the wind...
final AbstractGraphicsConfiguration chooseGraphicsConfiguration(final CapabilitiesImmutable capsChosen, final CapabilitiesImmutable capsRequested, final CapabilitiesChooser chooser, final AbstractGraphicsScreen screen, final int nativeVisualID)
static String toHexString(final int val)
static GraphicsConfigurationFactory getFactory(final AbstractGraphicsDevice device, final CapabilitiesImmutable caps)
Returns the graphics configuration factory for use with the given device and capability.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Provides a pluggable mechanism for arbitrary window toolkits to adapt their components to the NativeW...
static boolean isNativeVisualIDValidForProcessing(final int visualID)
Returns true if the given visualID is valid for further processing, i.e.
static boolean isOnSWTThread(final org.eclipse.swt.widgets.Display display)
Return true if the current thread is the SWT UI thread, otherwise false.
static final boolean isOSX
static long getWindowHandle(final Control swtControl)
static Rectangle getClientAreaInPixels(final Scrollable s)
Returns the unscaled Scrollable#getClientArea() in pixels.
static int deviceZoomScaleUp(final int v)
Returns SWT derived scaled-up value v, based on DPIUtil#getDeviceZoom() only, not considering higher-...
static AbstractGraphicsDevice getDevice(final Control swtControl)
static AbstractGraphicsScreen getScreen(final AbstractGraphicsDevice device, final int screen)
static int getNativeVisualID(final AbstractGraphicsDevice device, final long windowHandle)
static void setRealized(final Control swtControl, final boolean realize)
abstract EDTUtil getEDTUtil()
abstract Display getDisplay()
SWT Canvas containing a NEWT Window using native parenting.
NativeWindow getNativeWindow()
Returns the associated NativeWindow of this NativeWindowHolder, which is identical to getNativeSurfac...
static NewtCanvasSWT create(final Composite parent, final int style, final Window child)
Creates an instance using NewtCanvasSWT(Composite, int, Window) on the SWT thread.
void dispose()
Destroys this resource:
boolean setParent(final Composite parent)
void setBounds(final int x, final int y, final int width, final int height)
final boolean validateNative()
final void updatePosSizeCheck()
NewtCanvasSWT(final Composite parent, final int style, final Window child)
Instantiates a NewtCanvas with a NEWT child.
WindowClosingMode getDefaultCloseOperation()
NativeSurface getNativeSurface()
Returns the associated NativeSurface of this NativeSurfaceHolder.
Window setNEWTChild(final Window newChild)
Sets a new NEWT child, provoking reparenting.
final boolean isNativeValid()
WindowClosingMode setDefaultCloseOperation(final WindowClosingMode op)
Window closing mode if triggered by toolkit close operation.
DISPOSE_ON_CLOSE
Dispose resources on native window close operation.
A marker interface describing a graphics configuration, visual, or pixel format in a toolkit-independ...
AbstractGraphicsScreen getScreen()
Return the screen this graphics configuration is valid for.
A interface describing a graphics device in a toolkit-independent manner.
boolean close()
Optionally closing the device if handle is not null.
long getHandle()
Returns the native handle of the underlying native device, if such thing exist.
boolean open()
Optionally [re]opening the device if handle is null.
A interface describing a graphics screen in a toolkit-independent manner.
int getIndex()
Returns the screen index this graphics screen is valid for.
AbstractGraphicsDevice getDevice()
Return the device this graphics configuration is valid for.
Specifies an immutable set of capabilities that a window's rendering context must support,...
Provides low-level information required for hardware-accelerated rendering using a surface in a platf...
long getSurfaceHandle()
Returns the handle to the surface for this NativeSurface.
Accessor interface for implementing classes with ownership of a NativeWindow via an is-a or has-a rel...
Extend the NativeSurface interface with windowing information such as window-handle,...
boolean setSurfaceScale(final float[] pixelScale)
Request a pixel scale in x- and y-direction for the associated NativeSurface, where size_in_pixel_uni...
Protocol for handling window closing events.
WindowClosingMode setDefaultCloseOperation(WindowClosingMode op)
Immutable insets representing rectangular window decoration insets on all four edges in window units.
Specifying NEWT's Window functionality:
void sendWindowEvent(int eventType)
Send a WindowEvent to all WindowListener.
void setKeyboardFocusHandler(KeyListener l)
Sets a KeyListener allowing focus traversal with a covered window toolkit like AWT.
ReparentOperation reparentWindow(NativeWindow newParent, int x, int y, int hints)
Change this window's parent window.
boolean windowRepaint(int x, int y, int width, int height)
Trigger window repaint while passing the dirty region in pixel units.
void requestFocus()
Request focus for this native window.
void setFocusAction(FocusRunnable focusAction)
Sets a FocusRunnable, which FocusRunnable#run() method is executed before the native focus is request...
void setSize(int width, int height)
Sets the size of the window's client area in window units, excluding decorations.
void setVisible(boolean visible)
Calls setVisible(true, visible), i.e.
void setPosition(int x, int y)
Sets the location of the window's client area excluding insets (window decorations) in window units.
void destroy()
Destroys this window incl.releasing all related resources.