Jogamp
7c8864190507240bc9de879d3e5c4442f6cb6669
[jogl.git] / src / newt / classes / com / jogamp / javafx / newt / windows / WindowsWindow.java
1 /*
2  * Copyright (c) 2008 Sun Microsystems, Inc. All Rights Reserved.
3  * 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * 
8  * - Redistribution of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * 
11  * - Redistribution in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * 
15  * Neither the name of Sun Microsystems, Inc. or the names of
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  * 
19  * This software is provided "AS IS," without a warranty of any kind. ALL
20  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
21  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
22  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
23  * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
24  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
25  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
26  * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
27  * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
28  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
29  * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
30  * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  * 
32  */
33
34 package com.jogamp.javafx.newt.windows;
35
36 import javax.media.nativewindow.*;
37 import com.jogamp.javafx.newt.*;
38
39 public class WindowsWindow extends Window {
40
41     private long hmon;
42     private long hdc;
43     private long windowHandleClose;
44     private long parentWindowHandle;
45     // non fullscreen dimensions ..
46     private int nfs_width, nfs_height, nfs_x, nfs_y;
47     private final Insets insets = new Insets(0, 0, 0, 0);
48
49     static {
50         WindowsDisplay.initSingleton();
51     }
52
53     public WindowsWindow() {
54     }
55
56     Thread hdcOwner = null;
57
58     public synchronized int lockSurface() throws NativeWindowException {
59         int res = super.lockSurface(); 
60         if(LOCK_SUCCESS==res && 0!=windowHandle) {
61             if(hdc!=0) {
62                 throw new NativeWindowException("NEWT Surface handle set HDC "+toHexString(hdc)+" - "+Thread.currentThread().getName()+" ; "+this);
63             }
64             hdc = GetDC(windowHandle);
65             hmon = MonitorFromWindow(windowHandle);
66             hdcOwner = Thread.currentThread();
67         }
68         return res;
69     }
70
71     public synchronized void unlockSurface() {
72         // prevalidate, before we change data ..
73         Thread cur = Thread.currentThread();
74         if ( getSurfaceLockOwner() != cur ) {
75             getLockedStack().printStackTrace();
76             throw new NativeWindowException(cur+": Not owner, owner is "+getSurfaceLockOwner());
77         }
78         if (0!=hdc && 0!=windowHandle) {
79             if(hdcOwner != cur) {
80                 throw new NativeWindowException("NEWT Surface handle set HDC "+toHexString(hdc)+" by other thread "+hdcOwner+", this "+cur+" ; "+this);
81             }
82             ReleaseDC(windowHandle, hdc);
83             hdc=0;
84             hdcOwner=null;
85         }
86         super.unlockSurface();
87     }
88
89     public long getSurfaceHandle() {
90         return hdc;
91     }
92
93     public boolean hasDeviceChanged() {
94         if(0!=windowHandle) {
95             long _hmon = MonitorFromWindow(windowHandle);
96             if (hmon != _hmon) {
97                 if(DEBUG_IMPLEMENTATION || DEBUG_WINDOW_EVENT) {
98                     Exception e = new Exception("!!! Window Device Changed "+Thread.currentThread().getName()+
99                                                 ", HMON "+toHexString(hmon)+" -> "+toHexString(_hmon));
100                     e.printStackTrace();
101                 }
102                 hmon = _hmon;
103                 return true;
104             }
105         }
106         return false;
107     }
108
109     protected void createNative(long parentWindowHandle, Capabilities caps) {
110         WindowsScreen  screen = (WindowsScreen) getScreen();
111         WindowsDisplay display = (WindowsDisplay) screen.getDisplay();
112         config = GraphicsConfigurationFactory.getFactory(display.getGraphicsDevice()).chooseGraphicsConfiguration(caps, null, screen.getGraphicsScreen());
113         if (config == null) {
114             throw new NativeWindowException("Error choosing GraphicsConfiguration creating window: "+this);
115         }
116         windowHandle = CreateWindow(parentWindowHandle, 
117                                     display.getWindowClassAtom(), display.WINDOW_CLASS_NAME, display.getHInstance(), 
118                                     0, undecorated, x, y, width, height);
119         if (windowHandle == 0) {
120             throw new NativeWindowException("Error creating window");
121         }
122         this.parentWindowHandle = parentWindowHandle;
123         windowHandleClose = windowHandle;
124         if(DEBUG_IMPLEMENTATION || DEBUG_WINDOW_EVENT) {
125             Exception e = new Exception("!!! Window new window handle "+Thread.currentThread().getName()+
126                                         " (Parent HWND "+toHexString(parentWindowHandle)+
127                                         ") : HWND "+toHexString(windowHandle)+", "+Thread.currentThread());
128             e.printStackTrace();
129         }
130     }
131
132     protected void closeNative() {
133         if (hdc != 0) {
134             if(windowHandleClose != 0) {
135                 ReleaseDC(windowHandleClose, hdc);
136             }
137             hdc = 0;
138         }
139         if(windowHandleClose != 0) {
140             DestroyWindow(windowHandleClose);
141             windowHandleClose = 0;
142         }
143     }
144
145     protected void windowDestroyed() {
146         windowHandleClose = 0;
147         super.windowDestroyed();
148     }
149
150     public void setVisible(boolean visible) {
151         if(this.visible!=visible && 0!=windowHandle) {
152             this.visible=visible;
153             setVisible0(windowHandle, visible);
154         }
155     }
156
157     // @Override
158     public void setSize(int width, int height) {
159         if (0!=windowHandle && (width != this.width || this.height != height)) {
160             if(!fullscreen) {
161                 nfs_width=width;
162                 nfs_height=height;
163             }
164             this.width = width;
165             this.height = height;
166             setSize0(parentWindowHandle, windowHandle, x, y, width, height);
167         }
168     }
169
170     //@Override
171     public void setPosition(int x, int y) {
172         if (0!=windowHandle && (this.x != x || this.y != y)) {
173             if(!fullscreen) {
174                 nfs_x=x;
175                 nfs_y=y;
176             }
177             this.x = x;
178             this.y = y;
179             setPosition(parentWindowHandle, windowHandle, x , y);
180         }
181     }
182
183     public boolean setFullscreen(boolean fullscreen) {
184         if(0!=windowHandle && (this.fullscreen!=fullscreen)) {
185             int x,y,w,h;
186             this.fullscreen=fullscreen;
187             if(fullscreen) {
188                 x = 0; y = 0;
189                 w = screen.getWidth();
190                 h = screen.getHeight();
191             } else {
192                 x = nfs_x;
193                 y = nfs_y;
194                 w = nfs_width;
195                 h = nfs_height;
196             }
197             if(DEBUG_IMPLEMENTATION || DEBUG_WINDOW_EVENT) {
198                 System.err.println("WindowsWindow fs: "+fullscreen+" "+x+"/"+y+" "+w+"x"+h);
199             }
200             setFullscreen0(parentWindowHandle, windowHandle, x, y, w, h, undecorated, fullscreen);
201         }
202         return fullscreen;
203     }
204
205     // @Override
206     public void requestFocus() {
207         super.requestFocus();
208         if (windowHandle != 0L) {
209             requestFocus(windowHandle);
210         }
211     }
212
213     // @Override
214     public void setTitle(String title) {
215         if (title == null) {
216             title = "";
217         }
218         if (0!=windowHandle && !title.equals(getTitle())) {
219             super.setTitle(title);
220             setTitle(windowHandle, title);
221         }
222     }
223
224     public Insets getInsets() {
225         return (Insets)insets.clone();
226     }
227
228     //----------------------------------------------------------------------
229     // Internals only
230     //
231     protected static native boolean initIDs();
232     private        native long CreateWindow(long parentWindowHandle, 
233                                             int wndClassAtom, String wndName, 
234                                             long hInstance, long visualID,
235                                             boolean isUndecorated,
236                                             int x, int y, int width, int height);
237     private        native void DestroyWindow(long windowHandle);
238     private        native long GetDC(long windowHandle);
239     private        native void ReleaseDC(long windowHandle, long hdc);
240     private        native long MonitorFromWindow(long windowHandle);
241     private static native void setVisible0(long windowHandle, boolean visible);
242     private        native void setSize0(long parentWindowHandle, long windowHandle, int x, int y, int width, int height);
243     private        native void setPosition(long parentWindowHandle, long windowHandle, int x, int y);
244     private        native void setFullscreen0(long parentWindowHandle, long windowHandle, int x, int y, int width, int height, boolean isUndecorated, boolean on);
245     private static native void setTitle(long windowHandle, String title);
246     private static native void requestFocus(long windowHandle);
247
248     private void insetsChanged(int left, int top, int right, int bottom) {
249         if (left != -1 && top != -1 && right != -1 && bottom != -1) {
250             insets.left = left;
251             insets.top = top;
252             insets.right = right;
253             insets.bottom = bottom;
254         }
255     }
256     private void sizeChanged(int newWidth, int newHeight) {
257         width = newWidth;
258         height = newHeight;
259         if(!fullscreen) {
260             nfs_width=width;
261             nfs_height=height;
262         }
263         sendWindowEvent(WindowEvent.EVENT_WINDOW_RESIZED);
264     }
265
266     private void positionChanged(int newX, int newY) {
267         x = newX;
268         y = newY;
269         if(!fullscreen) {
270             nfs_x=x;
271             nfs_y=y;
272         }
273         sendWindowEvent(WindowEvent.EVENT_WINDOW_MOVED);
274     }
275
276     /**
277      *
278      * @param focusOwner if focusGained is true, focusOwner is the previous
279      * focus owner, if focusGained is false, focusOwner is the new focus owner
280      * @param focusGained
281      */
282     private void focusChanged(long focusOwner, boolean focusGained) {
283         if (focusGained) {
284             sendWindowEvent(WindowEvent.EVENT_WINDOW_GAINED_FOCUS);
285         } else {
286             sendWindowEvent(WindowEvent.EVENT_WINDOW_LOST_FOCUS);
287         }
288     }
289 }
http://JogAmp.org git info: FAQ, tutorial and man pages.