2 * freeglut_window_mswin.c
4 * The Windows-specific mouse cursor related stuff.
6 * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7 * Written by John F. Fay, <fayjf@sourceforge.net>
8 * Creation date: Sun Jan 22, 2012
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #define FREEGLUT_BUILDING_LIB
29 #include <GL/freeglut.h>
30 #include "../fg_internal.h"
33 /* The following include file is available from SGI but is not standard:
34 * #include <GL/wglext.h>
35 * So we copy the necessary parts out of it.
36 * XXX: should local definitions for extensions be put in a separate include file?
38 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
40 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
42 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
43 #define WGL_ACCELERATION_ARB 0x2003
44 #define WGL_SUPPORT_OPENGL_ARB 0x2010
45 #define WGL_DOUBLE_BUFFER_ARB 0x2011
46 #define WGL_COLOR_BITS_ARB 0x2014
47 #define WGL_ALPHA_BITS_ARB 0x201B
48 #define WGL_DEPTH_BITS_ARB 0x2022
49 #define WGL_STENCIL_BITS_ARB 0x2023
50 #define WGL_FULL_ACCELERATION_ARB 0x2027
52 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
53 #define WGL_SAMPLES_ARB 0x2042
55 #define WGL_TYPE_RGBA_FLOAT_ARB 0x21A0
57 #define WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB 0x20A9
59 #ifndef WGL_ARB_create_context
60 #define WGL_ARB_create_context 1
61 #ifdef WGL_WGLEXT_PROTOTYPES
62 extern HGLRC WINAPI wglCreateContextAttribsARB (HDC, HGLRC, const int *);
63 #endif /* WGL_WGLEXT_PROTOTYPES */
64 typedef HGLRC (WINAPI * PFNWGLCREATECONTEXTATTRIBSARBPROC) (HDC hDC, HGLRC hShareContext, const int *attribList);
66 #define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
67 #define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
68 #define WGL_CONTEXT_LAYER_PLANE_ARB 0x2093
69 #define WGL_CONTEXT_FLAGS_ARB 0x2094
70 #define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
72 #define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
73 #define WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x0002
75 #define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
76 #define WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB 0x00000002
78 #define ERROR_INVALID_VERSION_ARB 0x2095
79 #define ERROR_INVALID_PROFILE_ARB 0x2096
81 /* End of copying the necessary parts out of it. */
84 typedef BOOL (WINAPI *pRegisterTouchWindow)(HWND,ULONG);
85 static pRegisterTouchWindow fghRegisterTouchWindow = (pRegisterTouchWindow)0xDEADBEEF;
89 * Helper functions for getting client area from the window rect
90 * and the window rect from the client area given the style of the window
91 * (or a valid window pointer from which the style can be queried).
93 extern void fghGetBorderWidth(const DWORD windowStyle, int* xBorderWidth, int* yBorderWidth);
97 * Setup the pixel format for a Win32 window
100 #if defined(_WIN32_WCE)
101 static wchar_t* fghWstrFromStr(const char* str)
103 int i,len=strlen(str);
104 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
110 #endif /* defined(_WIN32_WCE) */
113 static void fghFillContextAttributes( int *attributes ) {
114 int where = 0, contextFlags, contextProfile;
116 ATTRIB_VAL( WGL_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
117 ATTRIB_VAL( WGL_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
120 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, WGL_CONTEXT_DEBUG_BIT_ARB ) |
121 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
122 if ( contextFlags != 0 ) {
123 ATTRIB_VAL( WGL_CONTEXT_FLAGS_ARB, contextFlags );
127 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, WGL_CONTEXT_CORE_PROFILE_BIT_ARB ) |
128 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
129 if ( contextProfile != 0 ) {
130 ATTRIB_VAL( WGL_CONTEXT_PROFILE_MASK_ARB, contextProfile );
136 static int fghIsExtensionSupported( HDC hdc, const char *extension ) {
137 const char *pWglExtString;
138 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB =
139 (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");
140 if ( wglGetEntensionsStringARB == NULL )
144 pWglExtString = wglGetEntensionsStringARB( hdc );
145 return ( pWglExtString != NULL ) && ( strstr(pWglExtString, extension) != NULL );
148 void fgNewWGLCreateContext( SFG_Window* window )
152 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
154 /* If nothing fancy has been required, leave the context as it is */
155 if ( fghIsLegacyContextRequested() )
160 wglMakeCurrent( window->Window.pContext.Device, window->Window.Context );
162 if ( !fghIsExtensionSupported( window->Window.pContext.Device, "WGL_ARB_create_context" ) )
164 /* wglCreateContextAttribsARB not found, yet the user has requested the new context creation */
165 fgWarning( "OpenGL >2.1 context requested but wglCreateContextAttribsARB is not available! Falling back to legacy context creation" );
166 /* Legacy context already created at this point in WM_CREATE path of fgPlatformWindowProc, just return */
170 /* new context creation */
171 fghFillContextAttributes( attributes );
173 wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress( "wglCreateContextAttribsARB" );
174 if ( wglCreateContextAttribsARB == NULL )
176 /* wglCreateContextAttribsARB not found, yet the user has requested the new context creation */
177 fgWarning( "OpenGL >2.1 context requested but wglCreateContextAttribsARB is not available! Falling back to legacy context creation" );
178 /* Legacy context already created at this point in WM_CREATE path of fgPlatformWindowProc, just return */
182 context = wglCreateContextAttribsARB( window->Window.pContext.Device, 0, attributes );
183 if ( context == NULL )
185 fghContextCreationError();
188 wglMakeCurrent( NULL, NULL );
189 wglDeleteContext( window->Window.Context );
190 window->Window.Context = context;
193 #if !defined(_WIN32_WCE)
195 static void fghFillPFD( PIXELFORMATDESCRIPTOR *ppfd, HDC hdc, unsigned char layer_type )
197 int flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
198 if ( fgState.DisplayMode & GLUT_DOUBLE ) {
199 flags |= PFD_DOUBLEBUFFER;
201 if ( fgState.DisplayMode & GLUT_STEREO ) {
205 #if defined(_MSC_VER)
206 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
209 /* Specify which pixel format do we opt for... */
210 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
212 ppfd->dwFlags = flags;
214 if( fgState.DisplayMode & GLUT_INDEX ) {
215 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
217 ppfd->cGreenBits = 0;
219 ppfd->cAlphaBits = 0;
221 ppfd->iPixelType = PFD_TYPE_RGBA;
223 ppfd->cGreenBits = 8;
225 ppfd->cAlphaBits = ( fgState.DisplayMode & GLUT_ALPHA ) ? 8 : 0;
228 ppfd->cColorBits = 24;
230 ppfd->cGreenShift = 0;
231 ppfd->cBlueShift = 0;
232 ppfd->cAlphaShift = 0;
233 ppfd->cAccumBits = ( fgState.DisplayMode & GLUT_ACCUM ) ? 1 : 0;
234 ppfd->cAccumRedBits = 0;
235 ppfd->cAccumGreenBits = 0;
236 ppfd->cAccumBlueBits = 0;
237 ppfd->cAccumAlphaBits = 0;
239 /* Hmmm, or 32/0 instead of 24/8? */
240 ppfd->cDepthBits = 24;
241 ppfd->cStencilBits = 8;
243 ppfd->cAuxBuffers = fghNumberOfAuxBuffersRequested();
244 ppfd->iLayerType = layer_type;
246 ppfd->dwLayerMask = 0;
247 ppfd->dwVisibleMask = 0;
248 ppfd->dwDamageMask = 0;
250 ppfd->cColorBits = (BYTE) GetDeviceCaps( hdc, BITSPIXEL );
253 static void fghFillPixelFormatAttributes( int *attributes, const PIXELFORMATDESCRIPTOR *ppfd )
257 ATTRIB_VAL( WGL_DRAW_TO_WINDOW_ARB, GL_TRUE );
258 ATTRIB_VAL( WGL_SUPPORT_OPENGL_ARB, GL_TRUE );
259 ATTRIB_VAL( WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB );
261 ATTRIB_VAL( WGL_COLOR_BITS_ARB, ppfd->cColorBits );
262 ATTRIB_VAL( WGL_ALPHA_BITS_ARB, ppfd->cAlphaBits );
263 ATTRIB_VAL( WGL_DEPTH_BITS_ARB, ppfd->cDepthBits );
264 ATTRIB_VAL( WGL_STENCIL_BITS_ARB, ppfd->cStencilBits );
266 ATTRIB_VAL( WGL_DOUBLE_BUFFER_ARB, ( fgState.DisplayMode & GLUT_DOUBLE ) != 0 );
268 if ( fgState.DisplayMode & GLUT_SRGB ) {
269 ATTRIB_VAL( WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, TRUE );
272 ATTRIB_VAL( WGL_SAMPLE_BUFFERS_ARB, GL_TRUE );
273 ATTRIB_VAL( WGL_SAMPLES_ARB, fgState.SampleNumber );
278 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
279 unsigned char layer_type )
281 #if defined(_WIN32_WCE)
284 PIXELFORMATDESCRIPTOR pfd;
285 PIXELFORMATDESCRIPTOR* ppfd = &pfd;
291 current_hDC = CreateDC(TEXT("DISPLAY"), NULL ,NULL ,NULL);
293 current_hDC = window->Window.pContext.Device;
295 fghFillPFD( ppfd, current_hDC, layer_type );
296 pixelformat = ChoosePixelFormat( current_hDC, ppfd );
298 /* windows hack for multismapling/sRGB */
299 if ( ( fgState.DisplayMode & GLUT_MULTISAMPLE ) ||
300 ( fgState.DisplayMode & GLUT_SRGB ) )
302 HGLRC rc, rc_before=wglGetCurrentContext();
304 HDC hDC, hDC_before=wglGetCurrentDC();
307 /* create a dummy window */
308 ZeroMemory(&wndCls, sizeof(wndCls));
309 wndCls.lpfnWndProc = DefWindowProc;
310 wndCls.hInstance = fgDisplay.pDisplay.Instance;
311 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
312 wndCls.lpszClassName = _T("FREEGLUT_dummy");
313 RegisterClass( &wndCls );
315 hWnd=CreateWindow(_T("FREEGLUT_dummy"), _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.pDisplay.Instance, 0 );
317 SetPixelFormat( hDC, pixelformat, ppfd );
319 rc = wglCreateContext( hDC );
320 wglMakeCurrent(hDC, rc);
322 if ( fghIsExtensionSupported( hDC, "WGL_ARB_multisample" ) )
324 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc =
325 (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");
326 if ( wglChoosePixelFormatARBProc )
331 float fAttributes[] = { 0, 0 };
333 fghFillPixelFormatAttributes( attributes, ppfd );
334 bValid = wglChoosePixelFormatARBProc(hDC, attributes, fAttributes, 1, &iPixelFormat, &numFormats);
336 if ( bValid && numFormats > 0 )
338 pixelformat = iPixelFormat;
343 wglMakeCurrent( hDC_before, rc_before);
344 wglDeleteContext(rc);
345 ReleaseDC(hWnd, hDC);
347 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.pDisplay.Instance);
350 success = ( pixelformat != 0 ) && ( checkOnly || SetPixelFormat( current_hDC, pixelformat, ppfd ) );
353 DeleteDC(current_hDC);
356 #endif /* defined(_WIN32_WCE) */
361 void fgPlatformSetWindow ( SFG_Window *window )
363 if ( window != fgStructure.CurrentWindow )
365 if( fgStructure.CurrentWindow )
366 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
367 fgStructure.CurrentWindow->Window.pContext.Device );
371 window->Window.pContext.Device = GetDC( window->Window.Handle );
373 window->Window.pContext.Device,
374 window->Window.Context
382 /* Computes position of corners of window Rect (outer position including
383 * decorations) based on the provided client rect and based on the style
384 * of the window in question.
385 * If posIsOutside is set to true, the input client Rect is taken to follow
386 * freeGLUT's window specification convention in which the top-left corner
387 * is at the outside of the window, while the size
388 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
391 void fghComputeWindowRectFromClientArea_UseStyle( const DWORD windowStyle, RECT *clientRect, BOOL posIsOutside )
393 int xBorderWidth = 0, yBorderWidth = 0;
395 /* If window has title bar, correct rect for it */
396 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... */
399 clientRect->bottom += GetSystemMetrics( SM_CYCAPTION );
401 clientRect->top -= GetSystemMetrics( SM_CYCAPTION );
404 /* get width of window's borders (frame), correct rect for it.
405 * Note, borders can be of zero width if style does not specify borders
407 fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);
410 clientRect->right += xBorderWidth * 2;
411 clientRect->bottom += yBorderWidth * 2;
415 clientRect->left -= xBorderWidth;
416 clientRect->right += xBorderWidth;
417 clientRect->top -= yBorderWidth;
418 clientRect->bottom += yBorderWidth;
422 /* Computes position of corners of window Rect (outer position including
423 * decorations) based on the provided client rect and based on the style
424 * of the window in question. If the window pointer or the window handle
425 * is NULL, a fully decorated window (caption and border) is assumed.
426 * Furthermore, if posIsOutside is set to true, the input client Rect is
427 * taken to follow freeGLUT's window specification convention in which the
428 * top-left corner is at the outside of the window, while the size
429 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
432 void fghComputeWindowRectFromClientArea_QueryWindow( const SFG_Window *window, RECT *clientRect, BOOL posIsOutside )
434 DWORD windowStyle = 0;
436 if (window && window->Window.Handle)
437 windowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
439 windowStyle = WS_OVERLAPPEDWINDOW;
441 fghComputeWindowRectFromClientArea_UseStyle(windowStyle, clientRect, posIsOutside);
444 /* Computes position of corners of client area (drawable area) of a window
445 * based on the provided window Rect (outer position including decorations)
446 * and based on the style of the window in question. If the window pointer
447 * or the window handle is NULL, a fully decorated window (caption and
448 * border) is assumed.
449 * Furthermore, if wantPosOutside is set to true, the output client Rect
450 * will follow freeGLUT's window specification convention in which the
451 * top-left corner is at the outside of the window, the size
452 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
455 void fghComputeClientAreaFromWindowRect( const SFG_Window *window, RECT *windowRect, BOOL wantPosOutside )
457 DWORD windowStyle = 0;
458 int xBorderWidth = 0, yBorderWidth = 0;
460 if (window && window->Window.Handle)
461 windowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
463 windowStyle = WS_OVERLAPPEDWINDOW;
465 /* If window has title bar, correct rect for it */
466 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... */
469 windowRect->bottom -= GetSystemMetrics( SM_CYCAPTION );
471 windowRect->top += GetSystemMetrics( SM_CYCAPTION );
474 /* get width of window's borders (frame), correct rect for it.
475 * Note, borders can be of zero width if style does not specify borders
477 fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);
480 windowRect->right -= xBorderWidth * 2;
481 windowRect->bottom -= yBorderWidth * 2;
485 windowRect->left += xBorderWidth;
486 windowRect->right -= xBorderWidth;
487 windowRect->top += yBorderWidth;
488 windowRect->bottom -= yBorderWidth;
492 /* Gets the rect describing the client area (drawable area) of the
494 * Returns an empty rect if window pointer or window handle is NULL.
495 * If wantPosOutside is set to true, the output client Rect
496 * will follow freeGLUT's window specification convention in which the
497 * top-left corner is at the outside of the window, while the size
498 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
501 RECT fghGetClientArea( const SFG_Window *window, BOOL wantPosOutside )
503 RECT windowRect = {0,0,0,0};
505 freeglut_return_val_if_fail((window && window->Window.Handle),windowRect);
508 * call GetWindowRect()
509 * (this returns the pixel coordinates of the outside of the window)
511 GetWindowRect( window->Window.Handle, &windowRect );
513 /* Then correct the results */
514 fghComputeClientAreaFromWindowRect(window, &windowRect, wantPosOutside);
519 /* Returns the width of the window borders based on the window's style.
521 void fghGetBorderWidth(const DWORD windowStyle, int* xBorderWidth, int* yBorderWidth)
523 if (windowStyle & WS_THICKFRAME)
525 *xBorderWidth = GetSystemMetrics(SM_CXSIZEFRAME);
526 *yBorderWidth = GetSystemMetrics(SM_CYSIZEFRAME);
528 else if (windowStyle & WS_DLGFRAME)
530 *xBorderWidth = GetSystemMetrics(SM_CXFIXEDFRAME);
531 *yBorderWidth = GetSystemMetrics(SM_CYFIXEDFRAME);
548 static BOOL CALLBACK m_proc(HMONITOR mon,
553 m_proc_t *dp=(m_proc_t *)data;
556 info.cbSize=sizeof(info);
557 res=GetMonitorInfo(mon,(LPMONITORINFO)&info);
560 if( strcmp(dp->name,info.szDevice)==0 )
562 *(dp->x)=info.rcMonitor.left;
563 *(dp->y)=info.rcMonitor.top;
571 * this function returns the origin of the screen identified by
572 * fgDisplay.pDisplay.DisplayName, and 0 otherwise.
573 * This is used in fgOpenWindow to open the gamemode window on the screen
574 * identified by the -display command line argument. The function should
575 * not be called otherwise.
578 static void get_display_origin(int *xp,int *yp)
583 if( fgDisplay.pDisplay.DisplayName )
588 st.name=fgDisplay.pDisplay.DisplayName;
589 EnumDisplayMonitors(0,0,m_proc,(LPARAM)&st);
593 #pragma message( "-display parameter only works if compiled with WINVER >= 0x0500")
595 static void get_display_origin(int *xp,int *yp)
600 if( fgDisplay.pDisplay.DisplayName )
602 fgWarning( "for working -display support FreeGLUT must be compiled with WINVER >= 0x0500");
610 * Opens a window. Requires a SFG_Window object created and attached
611 * to the freeglut structure. OpenGL context is created here.
613 void fgPlatformOpenWindow( SFG_Window* window, const char* title,
614 GLboolean positionUse, int x, int y,
615 GLboolean sizeUse, int w, int h,
616 GLboolean gameMode, GLboolean isSubWindow )
624 /* Grab the window class we have registered on glutInit(): */
625 atom = GetClassInfo( fgDisplay.pDisplay.Instance, _T("FREEGLUT"), &wc );
626 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
629 /* Determine window style flags*/
632 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
633 "Game mode being invoked on a subwindow",
637 * Set the window creation flags appropriately to make the window
640 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
644 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
647 * There's a small difference between creating the top, child and
650 if ( window->IsMenu )
653 exFlags |= WS_EX_TOOLWINDOW;
655 #if defined(_WIN32_WCE)
656 /* no decorations for windows CE */
658 /* if this is not a subwindow (child), set its style based on the requested display mode */
659 else if( window->Parent == NULL )
660 if ( fgState.DisplayMode & GLUT_BORDERLESS )
662 /* no window decorations needed */
664 else if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
665 /* only window decoration is a border, no title bar or buttons */
666 flags |= WS_DLGFRAME;
668 /* window decoration are a border, title bar and buttons.
669 * NB: we later query whether the window has a title bar or
670 * not by testing for the maximize button, as the test for
671 * WS_CAPTION can be true without the window having a title
672 * bar. This style WS_OVERLAPPEDWINDOW gives you a maximize
674 flags |= WS_OVERLAPPEDWINDOW;
677 /* subwindows always have no decoration, but are marked as a child window to the OS */
681 /* determine window size and position */
684 /* if in gamemode, query the origin of specified by the -display
685 * command line parameter (if any) and offset the upper-left corner
686 * of the window so we create the window on that screen.
687 * The -display argument doesn't do anything if not trying to enter
691 get_display_origin(&xoff,&yoff);
702 if( ! window->IsMenu )
707 else /* fail safe - Windows can make a window of size (0, 0) */
708 w = h = 300; /* default window size */
710 /* store requested client area width and height */
711 window->State.Width = w;
712 window->State.Height = h;
714 #if !defined(_WIN32_WCE) /* no decorations for windows CE */
719 * Update the window dimensions, taking the window decorations
720 * into account. FreeGLUT is to create the window with the
721 * topleft outside corner at (x,y) and with client area
723 * note: don't need to do this when w=h=CW_USEDEFAULT, so in the
724 * if( sizeUse ) here is convenient.
728 windowRect.right = x+w;
729 windowRect.bottom = y+h;
731 fghComputeWindowRectFromClientArea_UseStyle(flags,&windowRect,TRUE);
733 w = windowRect.right - windowRect.left;
734 h = windowRect.bottom- windowRect.top;
736 #endif /* !defined(_WIN32_WCE) */
738 #if defined(_WIN32_WCE)
740 wchar_t* wstr = fghWstrFromStr(title);
742 window->Window.Handle = CreateWindow(
745 WS_VISIBLE | WS_POPUP,
749 fgDisplay.pDisplay.Instance,
755 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
756 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
757 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
758 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
759 ShowWindow(window->Window.Handle, SW_SHOW);
760 UpdateWindow(window->Window.Handle);
763 window->Window.Handle = CreateWindowEx(
769 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
771 fgDisplay.pDisplay.Instance,
774 #endif /* defined(_WIN32_WCE) */
776 if( !( window->Window.Handle ) )
777 fgError( "Failed to create a window (%s)!", title );
779 #if !defined(_WIN32_WCE)
780 /* Need to set requested style again, apparently Windows doesn't listen when requesting windows without title bar or borders */
781 SetWindowLong(window->Window.Handle, GWL_STYLE, flags);
782 SetWindowPos(window->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
783 #endif /* defined(_WIN32_WCE) */
785 /* Make a menu window always on top - fix Feature Request 947118 */
786 if( window->IsMenu || gameMode )
788 window->Window.Handle,
791 SWP_NOMOVE | SWP_NOSIZE
794 /* Enable multitouch: additional flag TWF_FINETOUCH, TWF_WANTPALM */
796 if (fghRegisterTouchWindow == (pRegisterTouchWindow)0xDEADBEEF)
797 fghRegisterTouchWindow = (pRegisterTouchWindow)GetProcAddress(GetModuleHandle("user32"),"RegisterTouchWindow");
798 if (fghRegisterTouchWindow)
799 fghRegisterTouchWindow( window->Window.Handle, TWF_FINETOUCH | TWF_WANTPALM );
802 #if defined(_WIN32_WCE)
803 ShowWindow( window->Window.Handle, SW_SHOW );
805 ShowWindow( window->Window.Handle,
806 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
807 #endif /* defined(_WIN32_WCE) */
809 UpdateWindow( window->Window.Handle );
810 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
815 * Closes a window, destroying the frame and OpenGL context
817 void fgPlatformCloseWindow( SFG_Window* window )
819 /* Make sure we don't close a window with current context active */
820 if( fgStructure.CurrentWindow == window )
821 wglMakeCurrent( NULL, NULL );
824 * Step through the list of windows. If the rendering context
825 * is not being used by another window, then we delete it.
831 for( iter = (SFG_Window *)fgStructure.Windows.First;
833 iter = (SFG_Window *)iter->Node.Next )
835 if( ( iter->Window.Context == window->Window.Context ) &&
841 wglDeleteContext( window->Window.Context );
844 DestroyWindow( window->Window.Handle );
850 * This function makes the current window visible
852 void fgPlatformGlutShowWindow( void )
854 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
858 * This function hides the current window
860 void fgPlatformGlutHideWindow( void )
862 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
866 * Iconify the current window (top-level windows only)
868 void fgPlatformGlutIconifyWindow( void )
870 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
874 * Set the current window's title
876 void fgPlatformGlutSetWindowTitle( const char* title )
880 wchar_t* wstr = fghWstrFromStr(title);
881 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
885 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
890 * Set the current window's iconified title
892 void fgPlatformGlutSetIconTitle( const char* title )
896 wchar_t* wstr = fghWstrFromStr(title);
897 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
901 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
906 * Change the current window's position
908 void fgPlatformGlutPositionWindow( int x, int y )
912 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
913 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
915 fgStructure.CurrentWindow->Window.Handle,
918 winRect.right - winRect.left,
919 winRect.bottom - winRect.top,
925 * Lowers the current window (by Z order change)
927 void fgPlatformGlutPushWindow( void )
930 fgStructure.CurrentWindow->Window.Handle,
933 SWP_NOSIZE | SWP_NOMOVE
938 * Raises the current window (by Z order change)
940 void fgPlatformGlutPopWindow( void )
943 fgStructure.CurrentWindow->Window.Handle,
946 SWP_NOSIZE | SWP_NOMOVE
951 * Resize the current window so that it fits the whole screen
953 void fgPlatformGlutFullScreen( SFG_Window *win )
955 #if !defined(_WIN32_WCE) /* FIXME: what about WinCE */
957 if (glutGet(GLUT_FULL_SCREEN))
959 /* Leave full screen state before entering fullscreen again (resizing?) */
960 glutLeaveFullScreen();
964 #if(WINVER >= 0x0500) /* Windows 2000 or later */
970 /* For fullscreen mode, first remove all window decoration
971 * and set style to popup so it will overlap the taskbar
972 * then force to maximize on the screen on which it has the most
977 /* store current window rect */
978 GetWindowRect( win->Window.Handle, &win->State.pWState.OldRect );
980 /* store current window style */
981 win->State.pWState.OldStyle = s = GetWindowLong(win->Window.Handle, GWL_STYLE);
983 /* remove decorations from style and add popup style*/
984 s &= ~WS_OVERLAPPEDWINDOW;
986 SetWindowLong(win->Window.Handle, GWL_STYLE, s);
987 SetWindowPos(win->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
989 /* For fullscreen mode, find the monitor that is covered the most
990 * by the window and get its rect as the resize target.
992 hMonitor= MonitorFromRect(&win->State.pWState.OldRect, MONITOR_DEFAULTTONEAREST);
993 mi.cbSize = sizeof(mi);
994 GetMonitorInfo(hMonitor, &mi);
996 #else /* if (WINVER >= 0x0500) */
999 /* For fullscreen mode, force the top-left corner to 0,0
1000 * and adjust the window rectangle so that the client area
1001 * covers the whole screen.
1006 rect.right = fgDisplay.ScreenWidth;
1007 rect.bottom = fgDisplay.ScreenHeight;
1009 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1010 WS_CLIPCHILDREN, FALSE );
1011 #endif /* (WINVER >= 0x0500) */
1014 * then resize window
1015 * SWP_NOACTIVATE Do not activate the window
1016 * SWP_NOOWNERZORDER Do not change position in z-order
1017 * SWP_NOSENDCHANGING Suppress WM_WINDOWPOSCHANGING message
1018 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1020 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1024 rect.right - rect.left,
1025 rect.bottom - rect.top,
1026 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1030 win->State.IsFullscreen = GL_TRUE;
1036 * If we are fullscreen, resize the current window back to its original size
1038 void fgPlatformGlutLeaveFullScreen( SFG_Window *win )
1040 #if !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1041 if (!glutGet(GLUT_FULL_SCREEN))
1047 /* restore style of window before making it fullscreen */
1048 SetWindowLong(win->Window.Handle, GWL_STYLE, win->State.pWState.OldStyle);
1049 SetWindowPos(win->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
1052 SetWindowPos(win->Window.Handle,
1054 win->State.pWState.OldRect.left,
1055 win->State.pWState.OldRect.top,
1056 win->State.pWState.OldRect.right - win->State.pWState.OldRect.left,
1057 win->State.pWState.OldRect.bottom - win->State.pWState.OldRect.top,
1058 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1062 win->State.IsFullscreen = GL_FALSE;
1067 * Toggle the window's full screen state.
1069 void fgPlatformGlutFullScreenToggle( SFG_Window *win )
1071 if (!win->State.IsFullscreen)
1074 glutLeaveFullScreen();
1078 /* -- PLATFORM-SPECIFIC INTERFACE FUNCTION -------------------------------------------------- */
1080 int FGAPIENTRY __glutCreateWindowWithExit( const char *title, void (__cdecl *exit_function)(int) )
1082 __glutExitFunc = exit_function;
1083 return glutCreateWindow( title );