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