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);
216 newtChild.
setPosition(clientAreaWindow.x, clientAreaWindow.y);
219 newtChild.
windowRepaint(0, 0, clientAreaPixels.width, clientAreaPixels.height);
224 System.err.println(shortName()+
".Event.MOVE, "+event);
230 System.err.println(shortName()+
".Event.SHOW, "+event);
233 if( newtChildReady ) {
239 System.err.println(shortName()+
".Event.HIDE, "+event);
242 if( newtChildReady ) {
248 System.err.println(shortName()+
".Event.RESIZE, "+event);
260 System.err.println(shortName()+
".Event.DISPOSE, "+event);
268 System.err.println(shortName()+
".Event.ACTIVATE, "+event);
274 System.err.println(shortName()+
".Event.DEACTIVATE, "+event);
280 System.err.println(shortName()+
".Event.FOCUS_IN, "+event);
283 if( newtChildReady ) {
289 System.err.println(shortName()+
".Event.FOCUS_OUT, "+event);
296 System.err.println(shortName()+
".Event.misc: "+event.type+
", "+event);
304 public void setBounds(
final int x,
final int y,
final int width,
final int height) {
305 super.setBounds(x, y, width, height);
307 System.err.println(shortName()+
".setBounds: "+x+
"/"+y+
" "+width+
"x"+height);
315 if(
null != nativeWindow ) {
319 final org.eclipse.swt.graphics.Rectangle nClientAreaWindow = clientAreaWindow;
320 if(0 >= nClientAreaWindow.width || 0 >= nClientAreaWindow.height) {
330 System.err.println(shortName()+
".validateNative() windowHandle 0x"+Long.toHexString(nativeWindowHandle)+
", visualID 0x"+Integer.toHexString(visualID)+
", valid "+visualIDValid);
332 if( visualIDValid ) {
339 System.err.println(shortName()+
".validateNative() factory: "+factory+
", windowHandle 0x"+Long.
toHexString(nativeWindowHandle)+
", visualID 0x"+Integer.toHexString(visualID)+
", chosen config: "+config);
342 if (
null == config) {
346 nativeWindow =
new SWTNativeWindow(config, nativeWindowHandle);
347 reparentWindow(
true );
349 return null != nativeWindow;
353 final org.eclipse.swt.graphics.Rectangle oClientAreaWindow = clientAreaWindow;
355 final org.eclipse.swt.graphics.Rectangle nClientAreaWindow = getClientArea();
356 final boolean sizeChanged, posChanged;
358 sizeChanged = nClientAreaWindow.width != oClientAreaWindow.width || nClientAreaWindow.height != oClientAreaWindow.height;
359 posChanged = nClientAreaWindow.x != oClientAreaWindow.x || nClientAreaWindow.y != oClientAreaWindow.y;
360 if( sizeChanged || posChanged ) {
361 clientAreaPixels = nClientAreaPixels;
362 clientAreaWindow = nClientAreaWindow;
363 if( 0 < nClientAreaWindow.width && 0 < nClientAreaWindow.height ) {
364 pixelScale[0] = nClientAreaPixels.width / nClientAreaWindow.width;
365 pixelScale[1] = nClientAreaPixels.height / nClientAreaWindow.height;
374 System.err.println(shortName()+
".updatePosSizeCheck: sizeChanged "+sizeChanged+
", posChanged "+posChanged+
375 ", ("+Thread.currentThread().getName()+
"): newtChildReady "+newtChildReady+
376 ", pixel "+nClientAreaPixels.x+
"/"+nClientAreaPixels.y+
" "+nClientAreaPixels.width+
"x"+nClientAreaPixels.height+
377 ", window "+nClientAreaWindow.x+
"/"+nClientAreaWindow.y+
" "+nClientAreaWindow.width+
"x"+nClientAreaWindow.height+
378 ", scale "+pixelScale[0]+
"/"+pixelScale[1]+
379 " - surfaceHandle 0x"+Long.toHexString(nsh));
383 if( newtChildReady ) {
384 newtChild.
setSize(nClientAreaWindow.width, nClientAreaWindow.height);
390 if( newtChildReady ) {
391 newtChild.
setPosition(nClientAreaWindow.x, nClientAreaWindow.y);
397 System.err.println(shortName()+
".updatePosSizeCheck.X END");
423 throw new SWTException(
"Invalid thread access");
425 removeListener (SWT.Paint, swtListener);
426 removeListener (SWT.Move, swtListener);
427 removeListener (SWT.Show, swtListener);
428 removeListener (SWT.Hide, swtListener);
429 removeListener (SWT.Resize, swtListener);
430 removeListener (SWT.Dispose, swtListener);
431 removeListener (SWT.Activate, swtListener);
432 removeListener (SWT.Deactivate, swtListener);
433 removeListener (SWT.FocusIn, swtListener);
434 removeListener (SWT.FocusOut, swtListener);
436 if(
null != newtChild ) {
441 configureNewtChild(
false);
478 return null!=newtChild ;
481 boolean isFullscreen() {
508 throw new SWTException(
"Invalid thread access");
512 final Window prevChild = newtChild;
514 System.err.println(shortName()+
".setNEWTChild.0: win "+newtWinHandleToHexString(prevChild)+
" -> "+newtWinHandleToHexString(newChild));
517 if(
null != newtChild) {
518 reparentWindow(
false );
522 newtChild = newChild;
523 if(
null != nativeWindow &&
null != newChild) {
524 reparentWindow(
true );
536 return super.setParent(parent);
539 void configureNewtChild(
final boolean attach) {
540 newtChildReady = attach;
541 if(
null != newtChild ) {
552 void reparentWindow(
final boolean add) {
553 if(
null == newtChild ) {
557 System.err.println(shortName()+
".reparentWindow.0: add="+add+
", win "+newtWinHandleToHexString(newtChild)+
", EDTUtil: cur "+newtChild.
getScreen().
getDisplay().
getEDTUtil());
567 final EDTUtil edtUtil =
new SWTEDTUtil(newtDisplay, getDisplay());
569 newtDisplay.setEDTUtil( edtUtil );
572 newtChild.
setSize(clientAreaWindow.width, clientAreaWindow.height);
573 newtChild.
reparentWindow(nativeWindow, -1, -1, Window.REPARENT_HINT_BECOMES_VISIBLE);
574 newtChild.
setPosition(clientAreaWindow.x, clientAreaWindow.y);
576 configureNewtChild(
true);
584 configureNewtChild(
false);
589 System.err.println(shortName()+
".reparentWindow.X: add="+add+
", win "+newtWinHandleToHexString(newtChild)+
", EDTUtil: cur "+newtChild.
getScreen().
getDisplay().
getEDTUtil());
593 private final void requestFocusNEWTChild() {
594 if( newtChildReady ) {
603 requestFocusNEWTChild();
609 private final long nativeWindowHandle;
613 this.config = config;
614 this.nativeWindowHandle = nativeWindowHandle;
616 this.insets = OSXUtil.GetInsets(nativeWindowHandle);
618 this.insets =
new Insets(0, 0, 0, 0);
623 public RecursiveLock getLock() {
return null; }
626 public int lockSurface() throws NativeWindowException, RuntimeException {
627 return NativeSurface.LOCK_SUCCESS;
631 public void unlockSurface() { }
634 public boolean isSurfaceLockedByOtherThread() {
639 public Thread getSurfaceLockOwner() {
644 public boolean surfaceSwap() {
649 public void addSurfaceUpdatedListener(
final SurfaceUpdatedListener l) { }
652 public void addSurfaceUpdatedListener(
final int index,
final SurfaceUpdatedListener l)
throws IndexOutOfBoundsException {
656 public void removeSurfaceUpdatedListener(
final SurfaceUpdatedListener l) { }
659 public long getSurfaceHandle() {
674 public int getWidth() {
675 return newtScaleUp(clientAreaWindow.width, clientAreaWindow.width);
679 public int getHeight() {
680 return newtScaleUp(clientAreaWindow.height, clientAreaWindow.height);
685 return new com.jogamp.nativewindow.util.
Rectangle(getX(), getY(), getWidth(), getHeight());
690 return new com.jogamp.nativewindow.util.
Rectangle(getX(), getY(),
691 getSurfaceWidth(), getSurfaceHeight());
695 public final int[] convertToWindowUnits(
final int[] pixelUnitsAndResult) {
696 pixelUnitsAndResult[0] /= pixelScale[0];
697 pixelUnitsAndResult[1] /= pixelScale[1];
698 return pixelUnitsAndResult;
702 public final int[] convertToPixelUnits(
final int[] windowUnitsAndResult) {
703 windowUnitsAndResult[0] *= pixelScale[0];
704 windowUnitsAndResult[1] *= pixelScale[1];
705 return windowUnitsAndResult;
709 public int getSurfaceWidth() {
710 return newtScaleUp(clientAreaWindow.width, clientAreaPixels.width);
714 public int getSurfaceHeight() {
715 return newtScaleUp(clientAreaWindow.height, clientAreaPixels.height);
722 public AbstractGraphicsConfiguration getGraphicsConfiguration() {
727 public long getDisplayHandle() {
732 public int getScreenIndex() {
737 public void surfaceUpdated(
final Object updater,
final NativeSurface ns,
final long when) { }
740 public void destroy() { }
743 public NativeWindow getParent() {
748 public long getWindowHandle() {
749 return nativeWindowHandle;
753 public InsetsImmutable getInsets() {
758 public Point getLocationOnScreen(
final Point point) {
759 final Point los = NativeWindowFactory.getLocationOnScreen(
this);
761 return point.translate(los);
768 public boolean hasFocus() {
769 return isFocusControl();
773 static String newtWinHandleToHexString(
final Window w) {
774 return null != w ? toHexString(w.getWindowHandle()) :
"nil";
776 static String toHexString(
final long l) {
777 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.