Fixing a bug I introduced into the "...ATEXIT" hack
[freeglut] / src / mswin / freeglut_window_mswin.c
1 /*\r
2  * freeglut_window_mswin.c\r
3  *\r
4  * The Windows-specific mouse cursor related stuff.\r
5  *\r
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.\r
7  * Written by John F. Fay, <fayjf@sourceforge.net>\r
8  * Creation date: Sun Jan 22, 2012\r
9  *\r
10  * Permission is hereby granted, free of charge, to any person obtaining a\r
11  * copy of this software and associated documentation files (the "Software"),\r
12  * to deal in the Software without restriction, including without limitation\r
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
14  * and/or sell copies of the Software, and to permit persons to whom the\r
15  * Software is furnished to do so, subject to the following conditions:\r
16  *\r
17  * The above copyright notice and this permission notice shall be included\r
18  * in all copies or substantial portions of the Software.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
26  */\r
27 \r
28 #define FREEGLUT_BUILDING_LIB\r
29 #include <GL/freeglut.h>\r
30 #include "../Common/freeglut_internal.h"\r
31 \r
32 \r
33 /* The following include file is available from SGI but is not standard:\r
34  *   #include <GL/wglext.h>\r
35  * So we copy the necessary parts out of it.\r
36  * XXX: should local definitions for extensions be put in a separate include file?\r
37  */\r
38 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);\r
39 \r
40 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);\r
41 \r
42 #define WGL_DRAW_TO_WINDOW_ARB         0x2001\r
43 #define WGL_ACCELERATION_ARB           0x2003\r
44 #define WGL_SUPPORT_OPENGL_ARB         0x2010\r
45 #define WGL_DOUBLE_BUFFER_ARB          0x2011\r
46 #define WGL_COLOR_BITS_ARB             0x2014\r
47 #define WGL_ALPHA_BITS_ARB             0x201B\r
48 #define WGL_DEPTH_BITS_ARB             0x2022\r
49 #define WGL_STENCIL_BITS_ARB           0x2023\r
50 #define WGL_FULL_ACCELERATION_ARB      0x2027\r
51 \r
52 #define WGL_SAMPLE_BUFFERS_ARB         0x2041\r
53 #define WGL_SAMPLES_ARB                0x2042\r
54 \r
55 #define WGL_TYPE_RGBA_FLOAT_ARB        0x21A0\r
56 \r
57 #define WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20A9\r
58 \r
59 #ifndef WGL_ARB_create_context\r
60 #define WGL_ARB_create_context 1\r
61 #ifdef WGL_WGLEXT_PROTOTYPES\r
62 extern HGLRC WINAPI wglCreateContextAttribsARB (HDC, HGLRC, const int *);\r
63 #endif /* WGL_WGLEXT_PROTOTYPES */\r
64 typedef HGLRC (WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int *attribList);\r
65 \r
66 #define WGL_CONTEXT_MAJOR_VERSION_ARB  0x2091\r
67 #define WGL_CONTEXT_MINOR_VERSION_ARB  0x2092\r
68 #define WGL_CONTEXT_LAYER_PLANE_ARB    0x2093\r
69 #define WGL_CONTEXT_FLAGS_ARB          0x2094\r
70 #define WGL_CONTEXT_PROFILE_MASK_ARB   0x9126\r
71 \r
72 #define WGL_CONTEXT_DEBUG_BIT_ARB      0x0001\r
73 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002\r
74 \r
75 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001\r
76 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002\r
77 \r
78 #define ERROR_INVALID_VERSION_ARB      0x2095\r
79 #define ERROR_INVALID_PROFILE_ARB      0x2096\r
80 #endif\r
81 /* End of copying the necessary parts out of it. */\r
82 \r
83 /* pushing attribute/value pairs into an array */\r
84 #define ATTRIB(a) attributes[where++]=(a)\r
85 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}\r
86 \r
87 \r
88 extern int fghIsLegacyContextVersionRequested( void );\r
89 extern int fghMapBit( int mask, int from, int to );\r
90 extern int fghIsLegacyContextRequested( void );\r
91 extern void fghContextCreationError( void );\r
92 extern int fghNumberOfAuxBuffersRequested( void );\r
93 \r
94 #ifdef WM_TOUCH\r
95 typedef BOOL (WINAPI *pRegisterTouchWindow)(HWND,ULONG);\r
96 static pRegisterTouchWindow fghRegisterTouchWindow = (pRegisterTouchWindow)0xDEADBEEF;\r
97 #endif\r
98 \r
99 /* \r
100  * Helper functions for getting client area from the window rect\r
101  * and the window rect from the client area given the style of the window\r
102  * (or a valid window pointer from which the style can be queried).\r
103  */\r
104 extern void fghGetBorderWidth(const DWORD windowStyle, int* xBorderWidth, int* yBorderWidth);\r
105 \r
106 \r
107 /*\r
108  * Setup the pixel format for a Win32 window\r
109  */\r
110 \r
111 #if defined(_WIN32_WCE)\r
112 static wchar_t* fghWstrFromStr(const char* str)\r
113 {\r
114     int i,len=strlen(str);\r
115     wchar_t* wstr = (wchar_t*)malloc(2*len+2);\r
116     for(i=0; i<len; i++)\r
117         wstr[i] = str[i];\r
118     wstr[len] = 0;\r
119     return wstr;\r
120 }\r
121 #endif /* defined(_WIN32_WCE) */\r
122 \r
123 \r
124 static void fghFillContextAttributes( int *attributes ) {\r
125   int where = 0, contextFlags, contextProfile;\r
126 \r
127   if ( !fghIsLegacyContextVersionRequested() ) {\r
128     ATTRIB_VAL( WGL_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );\r
129     ATTRIB_VAL( WGL_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );\r
130   }\r
131 \r
132   contextFlags =\r
133     fghMapBit( fgState.ContextFlags, GLUT_DEBUG, WGL_CONTEXT_DEBUG_BIT_ARB ) |\r
134     fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );\r
135   if ( contextFlags != 0 ) {\r
136     ATTRIB_VAL( WGL_CONTEXT_FLAGS_ARB, contextFlags );\r
137   }\r
138 \r
139   contextProfile =\r
140     fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, WGL_CONTEXT_CORE_PROFILE_BIT_ARB ) |\r
141     fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );\r
142   if ( contextProfile != 0 ) {\r
143     ATTRIB_VAL( WGL_CONTEXT_PROFILE_MASK_ARB, contextProfile );\r
144   }\r
145 \r
146   ATTRIB( 0 );\r
147 }\r
148 \r
149 static int fghIsExtensionSupported( HDC hdc, const char *extension ) {\r
150     const char *pWglExtString;\r
151     PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB =\r
152       (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");\r
153     if ( wglGetEntensionsStringARB == NULL )\r
154     {\r
155       return FALSE;\r
156     }\r
157     pWglExtString = wglGetEntensionsStringARB( hdc );\r
158     return ( pWglExtString != NULL ) && ( strstr(pWglExtString, extension) != NULL );\r
159 }\r
160 \r
161 void fgNewWGLCreateContext( SFG_Window* window )\r
162 {\r
163     HGLRC context;\r
164     int attributes[9];\r
165     PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;\r
166 \r
167     /* If nothing fancy has been required, leave the context as it is */\r
168     if ( fghIsLegacyContextRequested() )\r
169     {\r
170         return;\r
171     }\r
172 \r
173     wglMakeCurrent( window->Window.pContext.Device, window->Window.Context );\r
174 \r
175     if ( !fghIsExtensionSupported( window->Window.pContext.Device, "WGL_ARB_create_context" ) )\r
176     {\r
177         return;\r
178     }\r
179 \r
180     /* new context creation */\r
181     fghFillContextAttributes( attributes );\r
182 \r
183     wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress( "wglCreateContextAttribsARB" );\r
184     if ( wglCreateContextAttribsARB == NULL )\r
185     {\r
186         fgError( "wglCreateContextAttribsARB not found" );\r
187     }\r
188 \r
189     context = wglCreateContextAttribsARB( window->Window.pContext.Device, 0, attributes );\r
190     if ( context == NULL )\r
191     {\r
192         fghContextCreationError();\r
193     }\r
194 \r
195     wglMakeCurrent( NULL, NULL );\r
196     wglDeleteContext( window->Window.Context );\r
197     window->Window.Context = context;\r
198 }\r
199 \r
200 #if !defined(_WIN32_WCE)\r
201 \r
202 static void fghFillPFD( PIXELFORMATDESCRIPTOR *ppfd, HDC hdc, unsigned char layer_type )\r
203 {\r
204   int flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;\r
205   if ( fgState.DisplayMode & GLUT_DOUBLE ) {\r
206         flags |= PFD_DOUBLEBUFFER;\r
207   }\r
208   if ( fgState.DisplayMode & GLUT_STEREO ) {\r
209     flags |= PFD_STEREO;\r
210   }\r
211 \r
212 #if defined(_MSC_VER)\r
213 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )\r
214 #endif\r
215 \r
216   /* Specify which pixel format do we opt for... */\r
217   ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);\r
218   ppfd->nVersion = 1;\r
219   ppfd->dwFlags = flags;\r
220 \r
221   if( fgState.DisplayMode & GLUT_INDEX ) {\r
222     ppfd->iPixelType = PFD_TYPE_COLORINDEX;\r
223     ppfd->cRedBits = 0;\r
224     ppfd->cGreenBits = 0;\r
225     ppfd->cBlueBits = 0;\r
226     ppfd->cAlphaBits = 0;\r
227   } else {\r
228     ppfd->iPixelType = PFD_TYPE_RGBA;\r
229     ppfd->cRedBits = 8;\r
230     ppfd->cGreenBits = 8;\r
231     ppfd->cBlueBits = 8;\r
232     ppfd->cAlphaBits = ( fgState.DisplayMode & GLUT_ALPHA ) ? 8 : 0;\r
233   }\r
234 \r
235   ppfd->cColorBits = 24;\r
236   ppfd->cRedShift = 0;\r
237   ppfd->cGreenShift = 0;\r
238   ppfd->cBlueShift = 0;\r
239   ppfd->cAlphaShift = 0;\r
240   ppfd->cAccumBits = ( fgState.DisplayMode & GLUT_ACCUM ) ? 1 : 0;\r
241   ppfd->cAccumRedBits = 0;\r
242   ppfd->cAccumGreenBits = 0;\r
243   ppfd->cAccumBlueBits = 0;\r
244   ppfd->cAccumAlphaBits = 0;\r
245 \r
246   /* Hmmm, or 32/0 instead of 24/8? */\r
247   ppfd->cDepthBits = 24;\r
248   ppfd->cStencilBits = 8;\r
249 \r
250   ppfd->cAuxBuffers = fghNumberOfAuxBuffersRequested();\r
251   ppfd->iLayerType = layer_type;\r
252   ppfd->bReserved = 0;\r
253   ppfd->dwLayerMask = 0;\r
254   ppfd->dwVisibleMask = 0;\r
255   ppfd->dwDamageMask = 0;\r
256   \r
257   ppfd->cColorBits = (BYTE) GetDeviceCaps( hdc, BITSPIXEL );\r
258 }\r
259 \r
260 static void fghFillPixelFormatAttributes( int *attributes, const PIXELFORMATDESCRIPTOR *ppfd )\r
261 {\r
262   int where = 0;\r
263 \r
264   ATTRIB_VAL( WGL_DRAW_TO_WINDOW_ARB, GL_TRUE );\r
265   ATTRIB_VAL( WGL_SUPPORT_OPENGL_ARB, GL_TRUE );\r
266   ATTRIB_VAL( WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB );\r
267 \r
268   ATTRIB_VAL( WGL_COLOR_BITS_ARB, ppfd->cColorBits );\r
269   ATTRIB_VAL( WGL_ALPHA_BITS_ARB, ppfd->cAlphaBits );\r
270   ATTRIB_VAL( WGL_DEPTH_BITS_ARB, ppfd->cDepthBits );\r
271   ATTRIB_VAL( WGL_STENCIL_BITS_ARB, ppfd->cStencilBits );\r
272 \r
273   ATTRIB_VAL( WGL_DOUBLE_BUFFER_ARB, ( fgState.DisplayMode & GLUT_DOUBLE ) != 0 );\r
274 \r
275   if ( fgState.DisplayMode & GLUT_SRGB ) {\r
276     ATTRIB_VAL( WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, TRUE );\r
277   }\r
278 \r
279   ATTRIB_VAL( WGL_SAMPLE_BUFFERS_ARB, GL_TRUE );\r
280   ATTRIB_VAL( WGL_SAMPLES_ARB, fgState.SampleNumber );\r
281   ATTRIB( 0 );\r
282 }\r
283 #endif\r
284 \r
285 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,\r
286                               unsigned char layer_type )\r
287 {\r
288 #if defined(_WIN32_WCE)\r
289     return GL_TRUE;\r
290 #else\r
291     PIXELFORMATDESCRIPTOR pfd;\r
292     PIXELFORMATDESCRIPTOR* ppfd = &pfd;\r
293     int pixelformat;\r
294     HDC current_hDC;\r
295     GLboolean success;\r
296 \r
297     if (checkOnly)\r
298       current_hDC = CreateDC(TEXT("DISPLAY"), NULL ,NULL ,NULL);\r
299     else\r
300       current_hDC = window->Window.pContext.Device;\r
301 \r
302     fghFillPFD( ppfd, current_hDC, layer_type );\r
303     pixelformat = ChoosePixelFormat( current_hDC, ppfd );\r
304 \r
305     /* windows hack for multismapling/sRGB */\r
306     if ( ( fgState.DisplayMode & GLUT_MULTISAMPLE ) ||\r
307          ( fgState.DisplayMode & GLUT_SRGB ) )\r
308     {        \r
309         HGLRC rc, rc_before=wglGetCurrentContext();\r
310         HWND hWnd;\r
311         HDC hDC, hDC_before=wglGetCurrentDC();\r
312         WNDCLASS wndCls;\r
313 \r
314         /* create a dummy window */\r
315         ZeroMemory(&wndCls, sizeof(wndCls));\r
316         wndCls.lpfnWndProc = DefWindowProc;\r
317         wndCls.hInstance = fgDisplay.pDisplay.Instance;\r
318         wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;\r
319         wndCls.lpszClassName = _T("FREEGLUT_dummy");\r
320         RegisterClass( &wndCls );\r
321 \r
322         hWnd=CreateWindow(_T("FREEGLUT_dummy"), _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.pDisplay.Instance, 0 );\r
323         hDC=GetDC(hWnd);\r
324         SetPixelFormat( hDC, pixelformat, ppfd );\r
325 \r
326         rc = wglCreateContext( hDC );\r
327         wglMakeCurrent(hDC, rc);\r
328 \r
329         if ( fghIsExtensionSupported( hDC, "WGL_ARB_multisample" ) )\r
330         {\r
331             PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc =\r
332               (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");\r
333             if ( wglChoosePixelFormatARBProc )\r
334             {\r
335                 int attributes[100];\r
336                 int iPixelFormat;\r
337                 BOOL bValid;\r
338                 float fAttributes[] = { 0, 0 };\r
339                 UINT numFormats;\r
340                 fghFillPixelFormatAttributes( attributes, ppfd );\r
341                 bValid = wglChoosePixelFormatARBProc(hDC, attributes, fAttributes, 1, &iPixelFormat, &numFormats);\r
342 \r
343                 if ( bValid && numFormats > 0 )\r
344                 {\r
345                     pixelformat = iPixelFormat;\r
346                 }\r
347             }\r
348         }\r
349 \r
350         wglMakeCurrent( hDC_before, rc_before);\r
351         wglDeleteContext(rc);\r
352         ReleaseDC(hWnd, hDC);\r
353         DestroyWindow(hWnd);\r
354         UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.pDisplay.Instance);\r
355     }\r
356 \r
357     success = ( pixelformat != 0 ) && ( checkOnly || SetPixelFormat( current_hDC, pixelformat, ppfd ) );\r
358 \r
359     if (checkOnly)\r
360         DeleteDC(current_hDC);\r
361 \r
362     return success;\r
363 #endif /* defined(_WIN32_WCE) */\r
364 }\r
365 \r
366 \r
367 \r
368 void fgPlatformSetWindow ( SFG_Window *window )\r
369 {\r
370     if ( window != fgStructure.CurrentWindow )\r
371     {\r
372         if( fgStructure.CurrentWindow )\r
373             ReleaseDC( fgStructure.CurrentWindow->Window.Handle,\r
374                        fgStructure.CurrentWindow->Window.pContext.Device );\r
375 \r
376         if ( window )\r
377         {\r
378             window->Window.pContext.Device = GetDC( window->Window.Handle );\r
379             wglMakeCurrent(\r
380                 window->Window.pContext.Device,\r
381                 window->Window.Context\r
382             );\r
383         }\r
384     }\r
385 }\r
386 \r
387 \r
388 \r
389 /* Computes position of corners of window Rect (outer position including\r
390  * decorations) based on the provided client rect and based on the style\r
391  * of the window in question.\r
392  * If posIsOutside is set to true, the input client Rect is taken to follow\r
393  * freeGLUT's window specification convention in which the top-left corner\r
394  * is at the outside of the window, while the size\r
395  * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable\r
396  * area.\r
397  */\r
398 void fghComputeWindowRectFromClientArea_UseStyle( const DWORD windowStyle, RECT *clientRect, BOOL posIsOutside )\r
399 {\r
400     int xBorderWidth = 0, yBorderWidth = 0;\r
401 \r
402     /* If window has title bar, correct rect for it */\r
403     if (windowStyle & WS_MAXIMIZEBOX) /* Need to query for WS_MAXIMIZEBOX to see if we have a title bar, the WS_CAPTION query is also true for a WS_DLGFRAME only... */\r
404         if (posIsOutside)\r
405             clientRect->bottom += GetSystemMetrics( SM_CYCAPTION );\r
406         else\r
407             clientRect->top -= GetSystemMetrics( SM_CYCAPTION );\r
408 \r
409     /* get width of window's borders (frame), correct rect for it.\r
410      * Note, borders can be of zero width if style does not specify borders\r
411      */\r
412     fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);\r
413     if (posIsOutside)\r
414     {\r
415         clientRect->right  += xBorderWidth * 2;\r
416         clientRect->bottom += yBorderWidth * 2;\r
417     }\r
418     else\r
419     {\r
420         clientRect->left   -= xBorderWidth;\r
421         clientRect->right  += xBorderWidth;\r
422         clientRect->top    -= yBorderWidth;\r
423         clientRect->bottom += yBorderWidth;\r
424     }\r
425 }\r
426 \r
427 /* Computes position of corners of window Rect (outer position including\r
428  * decorations) based on the provided client rect and based on the style\r
429  * of the window in question. If the window pointer or the window handle\r
430  * is NULL, a fully decorated window (caption and border) is assumed.\r
431  * Furthermore, if posIsOutside is set to true, the input client Rect is\r
432  * taken to follow freeGLUT's window specification convention in which the\r
433  * top-left corner is at the outside of the window, while the size\r
434  * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable\r
435  * area.\r
436 */\r
437 void fghComputeWindowRectFromClientArea_QueryWindow( const SFG_Window *window, RECT *clientRect, BOOL posIsOutside )\r
438 {\r
439     DWORD windowStyle = 0;\r
440 \r
441     if (window && window->Window.Handle)\r
442         windowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);\r
443     else\r
444         windowStyle = WS_OVERLAPPEDWINDOW;\r
445 \r
446     fghComputeWindowRectFromClientArea_UseStyle(windowStyle, clientRect, posIsOutside);\r
447 }\r
448 \r
449 /* Computes position of corners of client area (drawable area) of a window\r
450  * based on the provided window Rect (outer position including decorations)\r
451  * and based on the style of the window in question. If the window pointer\r
452  * or the window handle is NULL, a fully decorated window (caption and\r
453  * border) is assumed.\r
454  * Furthermore, if wantPosOutside is set to true, the output client Rect\r
455  * will follow freeGLUT's window specification convention in which the\r
456  * top-left corner is at the outside of the window, the size\r
457  * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable\r
458  * area.\r
459  */\r
460 void fghComputeClientAreaFromWindowRect( const SFG_Window *window, RECT *windowRect, BOOL wantPosOutside )\r
461 {\r
462     DWORD windowStyle = 0;\r
463     int xBorderWidth = 0, yBorderWidth = 0;\r
464 \r
465     if (window && window->Window.Handle)\r
466         windowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);\r
467     else\r
468         windowStyle = WS_OVERLAPPEDWINDOW;\r
469 \r
470     /* If window has title bar, correct rect for it */\r
471     if (windowStyle & WS_MAXIMIZEBOX) /* Need to query for WS_MAXIMIZEBOX to see if we have a title bar, the WS_CAPTION query is also true for a WS_DLGFRAME only... */\r
472         if (wantPosOutside)\r
473             windowRect->bottom -= GetSystemMetrics( SM_CYCAPTION );\r
474         else\r
475             windowRect->top    += GetSystemMetrics( SM_CYCAPTION );\r
476 \r
477     /* get width of window's borders (frame), correct rect for it.\r
478      * Note, borders can be of zero width if style does not specify borders\r
479      */\r
480     fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);\r
481     if (wantPosOutside)\r
482     {\r
483         windowRect->right  -= xBorderWidth * 2;\r
484         windowRect->bottom -= yBorderWidth * 2;\r
485     }\r
486     else\r
487     {\r
488         windowRect->left   += xBorderWidth;\r
489         windowRect->right  -= xBorderWidth;\r
490         windowRect->top    += yBorderWidth;\r
491         windowRect->bottom -= yBorderWidth;\r
492     }\r
493 }\r
494 \r
495 /* Gets the rect describing the client area (drawable area) of the\r
496  * specified window.\r
497  * Returns an empty rect if window pointer or window handle is NULL.\r
498  * If wantPosOutside is set to true, the output client Rect\r
499  * will follow freeGLUT's window specification convention in which the\r
500  * top-left corner is at the outside of the window, while the size\r
501  * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable\r
502  * area.\r
503  */\r
504 RECT fghGetClientArea( const SFG_Window *window, BOOL wantPosOutside )\r
505 {\r
506     RECT windowRect = {0,0,0,0};\r
507 \r
508     freeglut_return_val_if_fail((window && window->Window.Handle),windowRect);\r
509     \r
510     /*\r
511      * call GetWindowRect()\r
512      * (this returns the pixel coordinates of the outside of the window)\r
513      */\r
514     GetWindowRect( window->Window.Handle, &windowRect );\r
515 \r
516     /* Then correct the results */\r
517     fghComputeClientAreaFromWindowRect(window, &windowRect, wantPosOutside);\r
518 \r
519     return windowRect;\r
520 }\r
521 \r
522 /* Returns the width of the window borders based on the window's style.\r
523  */\r
524 void fghGetBorderWidth(const DWORD windowStyle, int* xBorderWidth, int* yBorderWidth)\r
525 {\r
526     if (windowStyle & WS_THICKFRAME)\r
527     {\r
528         *xBorderWidth = GetSystemMetrics(SM_CXSIZEFRAME);\r
529         *yBorderWidth = GetSystemMetrics(SM_CYSIZEFRAME);\r
530     }\r
531     else if (windowStyle & WS_DLGFRAME)\r
532     {\r
533         *xBorderWidth = GetSystemMetrics(SM_CXFIXEDFRAME);\r
534         *yBorderWidth = GetSystemMetrics(SM_CYFIXEDFRAME);\r
535     }\r
536     else\r
537     {\r
538         *xBorderWidth = 0;\r
539         *yBorderWidth = 0;\r
540     }\r
541 }\r
542 \r
543 #if(WINVER >= 0x500)\r
544 typedef struct\r
545 {\r
546       int *x;\r
547       int *y;\r
548       const char *name;\r
549 } m_proc_t;\r
550 \r
551 static BOOL CALLBACK m_proc(HMONITOR mon,\r
552                             HDC hdc,\r
553                             LPRECT rect,\r
554                             LPARAM data)\r
555 {\r
556       m_proc_t *dp=(m_proc_t *)data;\r
557       MONITORINFOEX info;\r
558       BOOL res;\r
559       info.cbSize=sizeof(info);\r
560       res=GetMonitorInfo(mon,(LPMONITORINFO)&info);\r
561       if( res )\r
562       {\r
563           if( strcmp(dp->name,info.szDevice)==0 )\r
564           {\r
565               *(dp->x)=info.rcMonitor.left;\r
566               *(dp->y)=info.rcMonitor.top;\r
567               return FALSE;\r
568           }\r
569       }\r
570       return TRUE;\r
571 }\r
572 \r
573 /* \r
574  * this function returns the origin of the screen identified by\r
575  * fgDisplay.pDisplay.DisplayName, and 0 otherwise.\r
576  * This is used in fgOpenWindow to open the gamemode window on the screen\r
577  * identified by the -display command line argument. The function should\r
578  * not be called otherwise.\r
579  */\r
580 \r
581 static void get_display_origin(int *xp,int *yp)\r
582 {\r
583     *xp = 0;\r
584     *yp = 0;\r
585 \r
586     if( fgDisplay.pDisplay.DisplayName )\r
587     {\r
588         m_proc_t st;\r
589         st.x=xp;\r
590         st.y=yp;\r
591         st.name=fgDisplay.pDisplay.DisplayName;\r
592         EnumDisplayMonitors(0,0,m_proc,(LPARAM)&st);\r
593     }\r
594 }\r
595 #else\r
596 #pragma message( "-display parameter only works if compiled with WINVER >= 0x0500")\r
597 \r
598 static void get_display_origin(int *xp,int *yp)\r
599 {\r
600     *xp = 0;\r
601     *yp = 0;\r
602 \r
603     if( fgDisplay.pDisplay.DisplayName )\r
604     {\r
605         fgWarning( "for working -display support FreeGLUT must be compiled with WINVER >= 0x0500");\r
606     }\r
607 }\r
608 #endif\r
609 \r
610 \r
611 \r
612 /*\r
613  * Opens a window. Requires a SFG_Window object created and attached\r
614  * to the freeglut structure. OpenGL context is created here.\r
615  */\r
616 void fgPlatformOpenWindow( SFG_Window* window, const char* title,\r
617                            GLboolean positionUse, int x, int y,\r
618                            GLboolean sizeUse, int w, int h,\r
619                            GLboolean gameMode, GLboolean isSubWindow )\r
620 {\r
621 \r
622     WNDCLASS wc;\r
623     DWORD flags   = 0;\r
624     DWORD exFlags = 0;\r
625     ATOM atom;\r
626 \r
627     /* Grab the window class we have registered on glutInit(): */\r
628     atom = GetClassInfo( fgDisplay.pDisplay.Instance, _T("FREEGLUT"), &wc );\r
629     FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",\r
630                                    "fgOpenWindow" );\r
631 \r
632     /* Determine window style flags*/\r
633     if( gameMode )\r
634     {\r
635         FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,\r
636                                        "Game mode being invoked on a subwindow",\r
637                                        "fgOpenWindow" );\r
638 \r
639         /*\r
640          * Set the window creation flags appropriately to make the window\r
641          * entirely visible:\r
642          */\r
643         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;\r
644     }\r
645     else\r
646     {\r
647         flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;\r
648 \r
649         /*\r
650          * There's a small difference between creating the top, child and\r
651          * menu windows\r
652          */\r
653         if ( window->IsMenu )\r
654         {\r
655             flags |= WS_POPUP;\r
656             exFlags |= WS_EX_TOOLWINDOW;\r
657         }\r
658 #if defined(_WIN32_WCE)\r
659         /* no decorations for windows CE */\r
660 #else\r
661         /* if this is not a subwindow (child), set its style based on the requested display mode */\r
662         else if( window->Parent == NULL )\r
663             if ( fgState.DisplayMode & GLUT_BORDERLESS )\r
664             {\r
665                 /* no window decorations needed */\r
666             }\r
667             else if ( fgState.DisplayMode & GLUT_CAPTIONLESS )\r
668                 /* only window decoration is a border, no title bar or buttons */\r
669                 flags |= WS_DLGFRAME;\r
670             else\r
671                 /* window decoration are a border, title bar and buttons.\r
672                  * NB: we later query whether the window has a title bar or\r
673                  * not by testing for the maximize button, as the test for\r
674                  * WS_CAPTION can be true without the window having a title\r
675                  * bar. This style WS_OVERLAPPEDWINDOW gives you a maximize\r
676                  * button. */\r
677                 flags |= WS_OVERLAPPEDWINDOW;\r
678 #endif\r
679         else\r
680             /* subwindows always have no decoration, but are marked as a child window to the OS */\r
681             flags |= WS_CHILD;\r
682     }\r
683 \r
684     /* determine window size and position */\r
685     if( gameMode )\r
686     {\r
687         /* if in gamemode, query the origin of specified by the -display\r
688          * command line parameter (if any) and offset the upper-left corner\r
689          * of the window so we create the window on that screen.\r
690          * The -display argument doesn't do anything if not trying to enter\r
691          * gamemode.\r
692          */\r
693         int xoff=0, yoff=0;\r
694         get_display_origin(&xoff,&yoff);\r
695         x += xoff;\r
696         y += yoff;\r
697     }\r
698     if( !positionUse )\r
699     {\r
700         x = CW_USEDEFAULT;\r
701         y = CW_USEDEFAULT;\r
702     }\r
703     if( !sizeUse )\r
704     {\r
705         if( ! window->IsMenu )\r
706         {\r
707             w = CW_USEDEFAULT;\r
708             h = CW_USEDEFAULT;\r
709         }\r
710         else /* fail safe - Windows can make a window of size (0, 0) */\r
711             w = h = 300; /* default window size */\r
712     }\r
713     /* store requested client area width and height */\r
714     window->State.Width = w;\r
715     window->State.Height = h;\r
716 \r
717 #if !defined(_WIN32_WCE)    /* no decorations for windows CE */\r
718     if( sizeUse )\r
719     {\r
720         RECT windowRect;\r
721         /*\r
722          * Update the window dimensions, taking the window decorations\r
723          * into account.  FreeGLUT is to create the window with the\r
724          * topleft outside corner at (x,y) and with client area\r
725          * dimensions (w,h).\r
726          * note: don't need to do this when w=h=CW_USEDEFAULT, so in the\r
727          * if( sizeUse ) here is convenient.\r
728          */\r
729         windowRect.left     = x;\r
730         windowRect.top      = y;\r
731         windowRect.right    = x+w;\r
732         windowRect.bottom   = y+h;\r
733 \r
734         fghComputeWindowRectFromClientArea_UseStyle(flags,&windowRect,TRUE);\r
735 \r
736         w = windowRect.right - windowRect.left;\r
737         h = windowRect.bottom- windowRect.top;\r
738     }\r
739 #endif /* !defined(_WIN32_WCE) */\r
740 \r
741 #if defined(_WIN32_WCE)\r
742     {\r
743         wchar_t* wstr = fghWstrFromStr(title);\r
744 \r
745         window->Window.Handle = CreateWindow(\r
746             _T("FREEGLUT"),\r
747             wstr,\r
748             WS_VISIBLE | WS_POPUP,\r
749             0,0, 240,320,\r
750             NULL,\r
751             NULL,\r
752             fgDisplay.pDisplay.Instance,\r
753             (LPVOID) window\r
754         );\r
755 \r
756         free(wstr);\r
757 \r
758         SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);\r
759         SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);\r
760         SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);\r
761         MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);\r
762         ShowWindow(window->Window.Handle, SW_SHOW);\r
763         UpdateWindow(window->Window.Handle);\r
764     }\r
765 #else\r
766     window->Window.Handle = CreateWindowEx(\r
767         exFlags,\r
768         _T("FREEGLUT"),\r
769         title,\r
770         flags,\r
771         x, y, w, h,\r
772         (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,\r
773         (HMENU) NULL,\r
774         fgDisplay.pDisplay.Instance,\r
775         (LPVOID) window\r
776     );\r
777 #endif /* defined(_WIN32_WCE) */\r
778 \r
779     if( !( window->Window.Handle ) )\r
780         fgError( "Failed to create a window (%s)!", title );\r
781 \r
782 #if !defined(_WIN32_WCE)\r
783     /* Need to set requested style again, apparently Windows doesn't listen when requesting windows without title bar or borders */\r
784     SetWindowLong(window->Window.Handle, GWL_STYLE, flags);\r
785     SetWindowPos(window->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);\r
786 #endif /* defined(_WIN32_WCE) */\r
787 \r
788     /* Make a menu window always on top - fix Feature Request 947118 */\r
789     if( window->IsMenu || gameMode )\r
790         SetWindowPos(\r
791                         window->Window.Handle,\r
792                         HWND_TOPMOST,\r
793                         0, 0, 0, 0,\r
794                         SWP_NOMOVE | SWP_NOSIZE\r
795                     );\r
796 \r
797     /* Enable multitouch: additional flag TWF_FINETOUCH, TWF_WANTPALM */\r
798     #ifdef WM_TOUCH\r
799         if (fghRegisterTouchWindow == (pRegisterTouchWindow)0xDEADBEEF) \r
800                         fghRegisterTouchWindow = (pRegisterTouchWindow)GetProcAddress(GetModuleHandle("user32"),"RegisterTouchWindow");\r
801                 if (fghRegisterTouchWindow)\r
802              fghRegisterTouchWindow( window->Window.Handle, TWF_FINETOUCH | TWF_WANTPALM );\r
803     #endif\r
804 \r
805 #if defined(_WIN32_WCE)\r
806     ShowWindow( window->Window.Handle, SW_SHOW );\r
807 #else\r
808     ShowWindow( window->Window.Handle,\r
809                 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );\r
810 #endif /* defined(_WIN32_WCE) */\r
811 \r
812     UpdateWindow( window->Window.Handle );\r
813     ShowCursor( TRUE );  /* XXX Old comments say "hide cursor"! */\r
814 \r
815 }\r
816 \r
817 \r
818 /*\r
819  * Closes a window, destroying the frame and OpenGL context\r
820  */\r
821 void fgPlatformCloseWindow( SFG_Window* window )\r
822 {\r
823     /* Make sure we don't close a window with current context active */\r
824     if( fgStructure.CurrentWindow == window )\r
825         wglMakeCurrent( NULL, NULL );\r
826 \r
827     /*\r
828      * Step through the list of windows.  If the rendering context\r
829      * is not being used by another window, then we delete it.\r
830      */\r
831     {\r
832         int used = FALSE ;\r
833         SFG_Window *iter ;\r
834 \r
835         for( iter = (SFG_Window *)fgStructure.Windows.First;\r
836              iter;\r
837              iter = (SFG_Window *)iter->Node.Next )\r
838         {\r
839             if( ( iter->Window.Context == window->Window.Context ) &&\r
840                 ( iter != window ) )\r
841                 used = TRUE;\r
842         }\r
843 \r
844         if( ! used )\r
845             wglDeleteContext( window->Window.Context );\r
846     }\r
847 \r
848     DestroyWindow( window->Window.Handle );\r
849 }\r
850 \r
851 \r
852 \r
853 /*\r
854  * This function makes the current window visible\r
855  */\r
856 void fgPlatformGlutShowWindow( void )\r
857 {\r
858     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );\r
859 }\r
860 \r
861 /*\r
862  * This function hides the current window\r
863  */\r
864 void fgPlatformGlutHideWindow( void )\r
865 {\r
866     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );\r
867 }\r
868 \r
869 /*\r
870  * Iconify the current window (top-level windows only)\r
871  */\r
872 void fgPlatformGlutIconifyWindow( void )\r
873 {\r
874     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );\r
875 }\r
876 \r
877 /*\r
878  * Set the current window's title\r
879  */\r
880 void fgPlatformGlutSetWindowTitle( const char* title )\r
881 {\r
882 #ifdef _WIN32_WCE\r
883     {\r
884         wchar_t* wstr = fghWstrFromStr(title);\r
885         SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );\r
886         free(wstr);\r
887     }\r
888 #else\r
889     SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );\r
890 #endif\r
891 }\r
892 \r
893 /*\r
894  * Set the current window's iconified title\r
895  */\r
896 void fgPlatformGlutSetIconTitle( const char* title )\r
897 {\r
898 #ifdef _WIN32_WCE\r
899     {\r
900         wchar_t* wstr = fghWstrFromStr(title);\r
901         SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );\r
902         free(wstr);\r
903     }\r
904 #else\r
905     SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );\r
906 #endif\r
907 }\r
908 \r
909 /*\r
910  * Change the current window's position\r
911  */\r
912 void fgPlatformGlutPositionWindow( int x, int y )\r
913 {\r
914     RECT winRect;\r
915 \r
916     /* "GetWindowRect" returns the pixel coordinates of the outside of the window */\r
917     GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );\r
918     MoveWindow(\r
919         fgStructure.CurrentWindow->Window.Handle,\r
920         x,\r
921         y,\r
922         winRect.right - winRect.left,\r
923         winRect.bottom - winRect.top,\r
924         TRUE\r
925     );\r
926 }\r
927 \r
928 /*\r
929  * Lowers the current window (by Z order change)\r
930  */\r
931 void fgPlatformGlutPushWindow( void )\r
932 {\r
933     SetWindowPos(\r
934         fgStructure.CurrentWindow->Window.Handle,\r
935         HWND_BOTTOM,\r
936         0, 0, 0, 0,\r
937         SWP_NOSIZE | SWP_NOMOVE\r
938     );\r
939 }\r
940 \r
941 /*\r
942  * Raises the current window (by Z order change)\r
943  */\r
944 void fgPlatformGlutPopWindow( void )\r
945 {\r
946     SetWindowPos(\r
947         fgStructure.CurrentWindow->Window.Handle,\r
948         HWND_TOP,\r
949         0, 0, 0, 0,\r
950         SWP_NOSIZE | SWP_NOMOVE\r
951     );\r
952 }\r
953 \r
954 /*\r
955  * Resize the current window so that it fits the whole screen\r
956  */\r
957 void fgPlatformGlutFullScreen( SFG_Window *win )\r
958 {\r
959 #if !defined(_WIN32_WCE) /* FIXME: what about WinCE */\r
960 \r
961     if (glutGet(GLUT_FULL_SCREEN))\r
962     {\r
963         /*  Leave full screen state before entering fullscreen again (resizing?) */\r
964         glutLeaveFullScreen();\r
965     }\r
966 \r
967     {\r
968 #if(WINVER >= 0x0500) /* Windows 2000 or later */\r
969         DWORD s;\r
970         RECT rect;\r
971         HMONITOR hMonitor;\r
972         MONITORINFO mi;\r
973 \r
974         /* For fullscreen mode, first remove all window decoration\r
975          * and set style to popup so it will overlap the taskbar\r
976          * then force to maximize on the screen on which it has the most\r
977          * overlap.\r
978          */\r
979 \r
980         \r
981         /* store current window rect */\r
982         GetWindowRect( win->Window.Handle, &win->State.pWState.OldRect );\r
983 \r
984         /* store current window style */\r
985         win->State.pWState.OldStyle = s = GetWindowLong(win->Window.Handle, GWL_STYLE);\r
986 \r
987         /* remove decorations from style and add popup style*/\r
988         s &= ~WS_OVERLAPPEDWINDOW;\r
989         s |= WS_POPUP;\r
990         SetWindowLong(win->Window.Handle, GWL_STYLE, s);\r
991         SetWindowPos(win->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);\r
992 \r
993         /* For fullscreen mode, find the monitor that is covered the most\r
994          * by the window and get its rect as the resize target.\r
995              */\r
996         hMonitor= MonitorFromRect(&win->State.pWState.OldRect, MONITOR_DEFAULTTONEAREST);\r
997         mi.cbSize = sizeof(mi);\r
998         GetMonitorInfo(hMonitor, &mi);\r
999         rect = mi.rcMonitor;\r
1000 #else   /* if (WINVER >= 0x0500) */\r
1001         RECT rect;\r
1002 \r
1003         /* For fullscreen mode, force the top-left corner to 0,0\r
1004          * and adjust the window rectangle so that the client area\r
1005          * covers the whole screen.\r
1006          */\r
1007 \r
1008         rect.left   = 0;\r
1009         rect.top    = 0;\r
1010         rect.right  = fgDisplay.ScreenWidth;\r
1011         rect.bottom = fgDisplay.ScreenHeight;\r
1012 \r
1013         AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |\r
1014                                   WS_CLIPCHILDREN, FALSE );\r
1015 #endif  /* (WINVER >= 0x0500) */\r
1016 \r
1017         /*\r
1018          * then resize window\r
1019          * SWP_NOACTIVATE     Do not activate the window\r
1020          * SWP_NOOWNERZORDER  Do not change position in z-order\r
1021          * SWP_NOSENDCHANGING Suppress WM_WINDOWPOSCHANGING message\r
1022          * SWP_NOZORDER       Retains the current Z order (ignore 2nd param)\r
1023          */\r
1024         SetWindowPos( fgStructure.CurrentWindow->Window.Handle,\r
1025                       HWND_TOP,\r
1026                       rect.left,\r
1027                       rect.top,\r
1028                       rect.right  - rect.left,\r
1029                       rect.bottom - rect.top,\r
1030                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |\r
1031                       SWP_NOZORDER\r
1032                     );\r
1033 \r
1034         win->State.IsFullscreen = GL_TRUE;\r
1035     }\r
1036 #endif\r
1037 }\r
1038 \r
1039 /*\r
1040  * If we are fullscreen, resize the current window back to its original size\r
1041  */\r
1042 void fgPlatformGlutLeaveFullScreen( SFG_Window *win )\r
1043 {\r
1044 #if !defined(_WIN32_WCE) /* FIXME: what about WinCE */\r
1045     if (!glutGet(GLUT_FULL_SCREEN))\r
1046     {\r
1047         /* nothing to do */\r
1048         return;\r
1049     }\r
1050 \r
1051     /* restore style of window before making it fullscreen */\r
1052     SetWindowLong(win->Window.Handle, GWL_STYLE, win->State.pWState.OldStyle);\r
1053     SetWindowPos(win->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);\r
1054 \r
1055     /* Then resize */\r
1056     SetWindowPos(win->Window.Handle,\r
1057         HWND_TOP,\r
1058         win->State.pWState.OldRect.left,\r
1059         win->State.pWState.OldRect.top,\r
1060         win->State.pWState.OldRect.right  - win->State.pWState.OldRect.left,\r
1061         win->State.pWState.OldRect.bottom - win->State.pWState.OldRect.top,\r
1062         SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |\r
1063         SWP_NOZORDER\r
1064         );\r
1065 \r
1066     win->State.IsFullscreen = GL_FALSE;\r
1067 #endif\r
1068 }\r
1069 \r
1070 /*\r
1071  * Toggle the window's full screen state.\r
1072  */\r
1073 void fgPlatformGlutFullScreenToggle( SFG_Window *win )\r
1074 {\r
1075     if (!win->State.IsFullscreen)\r
1076         glutFullScreen();\r
1077     else\r
1078         glutLeaveFullScreen();\r
1079 }\r
1080 \r
1081 \r
1082 /* -- PLATFORM-SPECIFIC INTERFACE FUNCTION -------------------------------------------------- */\r
1083 \r
1084 int FGAPIENTRY __glutCreateWindowWithExit( const char *title, void (__cdecl *exit_function)(int) )\r
1085 {\r
1086   __glutExitFunc = exit_function;\r
1087   return glutCreateWindow( title );\r
1088 }\r
1089 \r