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);
193 private final int newtScaleUp(
final int scaleUp,
final int passthrough) {
200 private final Listener swtListener =
new Listener () {
202 public void handleEvent (
final Event event) {
203 switch (event.type) {
206 System.err.println(shortName()+
".Event.PAINT, "+event);
212 newtChild.
setSize(clientAreaWindow.width, clientAreaWindow.height);
217 newtChild.
setPosition(clientAreaWindow.x, clientAreaWindow.y);
220 newtChild.
windowRepaint(0, 0, clientAreaPixels.width, clientAreaPixels.height);
225 System.err.println(shortName()+
".Event.MOVE, "+event);
231 System.err.println(shortName()+
".Event.SHOW, "+event);
234 if( newtChildReady ) {
240 System.err.println(shortName()+
".Event.HIDE, "+event);
243 if( newtChildReady ) {
249 System.err.println(shortName()+
".Event.RESIZE, "+event);
261 System.err.println(shortName()+
".Event.DISPOSE, "+event);
269 System.err.println(shortName()+
".Event.ACTIVATE, "+event);
275 System.err.println(shortName()+
".Event.DEACTIVATE, "+event);
281 System.err.println(shortName()+
".Event.FOCUS_IN, "+event);
284 if( newtChildReady ) {
290 System.err.println(shortName()+
".Event.FOCUS_OUT, "+event);
297 System.err.println(shortName()+
".Event.misc: "+event.type+
", "+event);
305 public void setBounds(
final int x,
final int y,
final int width,
final int height) {
306 super.setBounds(x, y, width, height);
308 System.err.println(shortName()+
".setBounds: "+x+
"/"+y+
" "+width+
"x"+height);
316 if(
null != nativeWindow ) {
320 final org.eclipse.swt.graphics.Rectangle nClientAreaWindow = clientAreaWindow;
321 if(0 >= nClientAreaWindow.width || 0 >= nClientAreaWindow.height) {
331 System.err.println(shortName()+
".validateNative() windowHandle 0x"+Long.toHexString(nativeWindowHandle)+
", visualID 0x"+Integer.toHexString(visualID)+
", valid "+visualIDValid);
333 if( visualIDValid ) {
340 System.err.println(shortName()+
".validateNative() factory: "+factory+
", windowHandle 0x"+Long.
toHexString(nativeWindowHandle)+
", visualID 0x"+Integer.toHexString(visualID)+
", chosen config: "+config);
343 if (
null == config) {
347 nativeWindow =
new SWTNativeWindow(config, nativeWindowHandle);
348 reparentWindow(
true );
350 return null != nativeWindow;
354 final org.eclipse.swt.graphics.Rectangle oClientAreaWindow = clientAreaWindow;
356 final org.eclipse.swt.graphics.Rectangle nClientAreaWindow = getClientArea();
357 final boolean sizeChanged, posChanged;
359 sizeChanged = nClientAreaWindow.width != oClientAreaWindow.width || nClientAreaWindow.height != oClientAreaWindow.height;
360 posChanged = nClientAreaWindow.x != oClientAreaWindow.x || nClientAreaWindow.y != oClientAreaWindow.y;
361 if( sizeChanged || posChanged ) {
362 clientAreaPixels = nClientAreaPixels;
363 clientAreaWindow = nClientAreaWindow;
364 if( 0 < nClientAreaWindow.width && 0 < nClientAreaWindow.height ) {
365 pixelScale[0] = nClientAreaPixels.width / nClientAreaWindow.width;
366 pixelScale[1] = nClientAreaPixels.height / nClientAreaWindow.height;
375 System.err.println(shortName()+
".updatePosSizeCheck: sizeChanged "+sizeChanged+
", posChanged "+posChanged+
376 ", ("+Thread.currentThread().getName()+
"): newtChildReady "+newtChildReady+
377 ", pixel "+nClientAreaPixels.x+
"/"+nClientAreaPixels.y+
" "+nClientAreaPixels.width+
"x"+nClientAreaPixels.height+
378 ", window "+nClientAreaWindow.x+
"/"+nClientAreaWindow.y+
" "+nClientAreaWindow.width+
"x"+nClientAreaWindow.height+
379 ", scale "+pixelScale[0]+
"/"+pixelScale[1]+
380 " - surfaceHandle 0x"+Long.toHexString(nsh));
383 if( newtChildReady ) {
384 newtChild.
setSize(nClientAreaWindow.width, nClientAreaWindow.height);
391 if( newtChildReady ) {
392 newtChild.
setPosition(nClientAreaWindow.x, nClientAreaWindow.y);
398 System.err.println(shortName()+
".updatePosSizeCheck.X END");
424 throw new SWTException(
"Invalid thread access");
426 removeListener (SWT.Paint, swtListener);
427 removeListener (SWT.Move, swtListener);
428 removeListener (SWT.Show, swtListener);
429 removeListener (SWT.Hide, swtListener);
430 removeListener (SWT.Resize, swtListener);
431 removeListener (SWT.Dispose, swtListener);
432 removeListener (SWT.Activate, swtListener);
433 removeListener (SWT.Deactivate, swtListener);
434 removeListener (SWT.FocusIn, swtListener);
435 removeListener (SWT.FocusOut, swtListener);
437 if(
null != newtChild ) {
442 configureNewtChild(
false);
479 return null!=newtChild ;
482 boolean isFullscreen() {
509 throw new SWTException(
"Invalid thread access");
513 final Window prevChild = newtChild;
515 System.err.println(shortName()+
".setNEWTChild.0: win "+newtWinHandleToHexString(prevChild)+
" -> "+newtWinHandleToHexString(newChild));
518 if(
null != newtChild) {
519 reparentWindow(
false );
523 newtChild = newChild;
524 if(
null != nativeWindow &&
null != newChild) {
525 reparentWindow(
true );
537 return super.setParent(parent);
540 void configureNewtChild(
final boolean attach) {
541 newtChildReady = attach;
542 if(
null != newtChild ) {
553 void reparentWindow(
final boolean add) {
554 if(
null == newtChild ) {
558 System.err.println(shortName()+
".reparentWindow.0: add="+add+
", win "+newtWinHandleToHexString(newtChild)+
", EDTUtil: cur "+newtChild.
getScreen().
getDisplay().
getEDTUtil());
568 final EDTUtil edtUtil =
new SWTEDTUtil(newtDisplay, getDisplay());
570 newtDisplay.setEDTUtil( edtUtil );
573 newtChild.
setSize(clientAreaWindow.width, clientAreaWindow.height);
574 newtChild.
reparentWindow(nativeWindow, -1, -1, Window.REPARENT_HINT_BECOMES_VISIBLE);
575 newtChild.
setPosition(clientAreaWindow.x, clientAreaWindow.y);
577 configureNewtChild(
true);
585 configureNewtChild(
false);
590 System.err.println(shortName()+
".reparentWindow.X: add="+add+
", win "+newtWinHandleToHexString(newtChild)+
", EDTUtil: cur "+newtChild.
getScreen().
getDisplay().
getEDTUtil());
594 private final void requestFocusNEWTChild() {
595 if( newtChildReady ) {
604 requestFocusNEWTChild();
610 private final long nativeWindowHandle;
614 this.config = config;
615 this.nativeWindowHandle = nativeWindowHandle;
617 this.insets = OSXUtil.GetInsets(nativeWindowHandle);
619 this.insets =
new Insets(0, 0, 0, 0);
624 public RecursiveLock getLock() {
return null; }
627 public int lockSurface() throws NativeWindowException, RuntimeException {
628 return NativeSurface.LOCK_SUCCESS;
632 public void unlockSurface() { }
635 public boolean isSurfaceLockedByOtherThread() {
640 public Thread getSurfaceLockOwner() {
645 public boolean surfaceSwap() {
650 public void addSurfaceUpdatedListener(
final SurfaceUpdatedListener l) { }
653 public void addSurfaceUpdatedListener(
final int index,
final SurfaceUpdatedListener l)
throws IndexOutOfBoundsException {
657 public void removeSurfaceUpdatedListener(
final SurfaceUpdatedListener l) { }
660 public long getSurfaceHandle() {
675 public int getWidth() {
676 return newtScaleUp(clientAreaWindow.width, clientAreaWindow.width);
680 public int getHeight() {
681 return newtScaleUp(clientAreaWindow.height, clientAreaWindow.height);
686 return new com.jogamp.nativewindow.util.
Rectangle(getX(), getY(), getWidth(), getHeight());
691 return new com.jogamp.nativewindow.util.
Rectangle(getX(), getY(),
692 getSurfaceWidth(), getSurfaceHeight());
696 public final int[] convertToWindowUnits(
final int[] pixelUnitsAndResult) {
697 pixelUnitsAndResult[0] /= pixelScale[0];
698 pixelUnitsAndResult[1] /= pixelScale[1];
699 return pixelUnitsAndResult;
703 public final int[] convertToPixelUnits(
final int[] windowUnitsAndResult) {
704 windowUnitsAndResult[0] *= pixelScale[0];
705 windowUnitsAndResult[1] *= pixelScale[1];
706 return windowUnitsAndResult;
710 public int getSurfaceWidth() {
711 return newtScaleUp(clientAreaWindow.width, clientAreaPixels.width);
715 public int getSurfaceHeight() {
716 return newtScaleUp(clientAreaWindow.height, clientAreaPixels.height);
723 public AbstractGraphicsConfiguration getGraphicsConfiguration() {
728 public long getDisplayHandle() {
733 public int getScreenIndex() {
738 public void surfaceUpdated(
final Object updater,
final NativeSurface ns,
final long when) { }
741 public void destroy() { }
744 public NativeWindow getParent() {
749 public long getWindowHandle() {
750 return nativeWindowHandle;
754 public InsetsImmutable getInsets() {
759 public Point getLocationOnScreen(
final Point point) {
760 final Point los = NativeWindowFactory.getLocationOnScreen(
this);
762 return point.translate(los);
769 public boolean hasFocus() {
770 return isFocusControl();
774 static String newtWinHandleToHexString(
final Window w) {
775 return null != w ? toHexString(w.getWindowHandle()) :
"nil";
777 static String toHexString(
final long l) {
778 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.