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