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;
90 * Setup the pixel format for a Win32 window
93 #if defined(_WIN32_WCE)
94 static wchar_t* fghWstrFromStr(const char* str)
96 int i,len=strlen(str);
97 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
103 #endif /* defined(_WIN32_WCE) */
106 static void fghFillContextAttributes( int *attributes ) {
107 int where = 0, contextFlags, contextProfile;
109 ATTRIB_VAL( WGL_CONTEXT_MAJOR_VERSION_ARB, fgState.MajorVersion );
110 ATTRIB_VAL( WGL_CONTEXT_MINOR_VERSION_ARB, fgState.MinorVersion );
113 fghMapBit( fgState.ContextFlags, GLUT_DEBUG, WGL_CONTEXT_DEBUG_BIT_ARB ) |
114 fghMapBit( fgState.ContextFlags, GLUT_FORWARD_COMPATIBLE, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB );
115 if ( contextFlags != 0 ) {
116 ATTRIB_VAL( WGL_CONTEXT_FLAGS_ARB, contextFlags );
120 fghMapBit( fgState.ContextProfile, GLUT_CORE_PROFILE, WGL_CONTEXT_CORE_PROFILE_BIT_ARB ) |
121 fghMapBit( fgState.ContextProfile, GLUT_COMPATIBILITY_PROFILE, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB );
122 if ( contextProfile != 0 ) {
123 ATTRIB_VAL( WGL_CONTEXT_PROFILE_MASK_ARB, contextProfile );
129 static int fghIsExtensionSupported( HDC hdc, const char *extension ) {
130 const char *pWglExtString;
131 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB =
132 (PFNWGLGETEXTENSIONSSTRINGARBPROC) wglGetProcAddress("wglGetExtensionsStringARB");
133 if ( wglGetEntensionsStringARB == NULL )
137 pWglExtString = wglGetEntensionsStringARB( hdc );
138 return ( pWglExtString != NULL ) && ( strstr(pWglExtString, extension) != NULL );
141 void fgNewWGLCreateContext( SFG_Window* window )
145 PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB;
147 /* If nothing fancy has been required, leave the context as it is */
148 if ( fghIsLegacyContextRequested() )
153 wglMakeCurrent( window->Window.pContext.Device, window->Window.Context );
155 if ( !fghIsExtensionSupported( window->Window.pContext.Device, "WGL_ARB_create_context" ) )
157 /* wglCreateContextAttribsARB not found, yet the user has requested the new context creation */
158 fgWarning( "OpenGL >2.1 context requested but wglCreateContextAttribsARB is not available! Falling back to legacy context creation" );
159 /* Legacy context already created at this point in WM_CREATE path of fgPlatformWindowProc, just return */
163 /* new context creation */
164 fghFillContextAttributes( attributes );
166 wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC) wglGetProcAddress( "wglCreateContextAttribsARB" );
167 if ( wglCreateContextAttribsARB == NULL )
169 /* wglCreateContextAttribsARB not found, yet the user has requested the new context creation */
170 fgWarning( "OpenGL >2.1 context requested but wglCreateContextAttribsARB is not available! Falling back to legacy context creation" );
171 /* Legacy context already created at this point in WM_CREATE path of fgPlatformWindowProc, just return */
175 context = wglCreateContextAttribsARB( window->Window.pContext.Device, 0, attributes );
176 if ( context == NULL )
178 fghContextCreationError();
181 wglMakeCurrent( NULL, NULL );
182 wglDeleteContext( window->Window.Context );
183 window->Window.Context = context;
186 #if !defined(_WIN32_WCE)
188 static void fghFillPFD( PIXELFORMATDESCRIPTOR *ppfd, HDC hdc, unsigned char layer_type )
190 int flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
191 if ( fgState.DisplayMode & GLUT_DOUBLE ) {
192 flags |= PFD_DOUBLEBUFFER;
194 if ( fgState.DisplayMode & GLUT_STEREO ) {
198 #if defined(_MSC_VER)
199 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
202 /* Specify which pixel format do we opt for... */
203 ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
205 ppfd->dwFlags = flags;
207 if( fgState.DisplayMode & GLUT_INDEX ) {
208 ppfd->iPixelType = PFD_TYPE_COLORINDEX;
210 ppfd->cGreenBits = 0;
212 ppfd->cAlphaBits = 0;
214 ppfd->iPixelType = PFD_TYPE_RGBA;
216 ppfd->cGreenBits = 8;
218 ppfd->cAlphaBits = ( fgState.DisplayMode & GLUT_ALPHA ) ? 8 : 0;
221 ppfd->cColorBits = 24;
223 ppfd->cGreenShift = 0;
224 ppfd->cBlueShift = 0;
225 ppfd->cAlphaShift = 0;
226 ppfd->cAccumBits = ( fgState.DisplayMode & GLUT_ACCUM ) ? 1 : 0;
227 ppfd->cAccumRedBits = 0;
228 ppfd->cAccumGreenBits = 0;
229 ppfd->cAccumBlueBits = 0;
230 ppfd->cAccumAlphaBits = 0;
232 /* Hmmm, or 32/0 instead of 24/8? */
233 ppfd->cDepthBits = 24;
234 ppfd->cStencilBits = 8;
236 ppfd->cAuxBuffers = fghNumberOfAuxBuffersRequested();
237 ppfd->iLayerType = layer_type;
239 ppfd->dwLayerMask = 0;
240 ppfd->dwVisibleMask = 0;
241 ppfd->dwDamageMask = 0;
243 ppfd->cColorBits = (BYTE) GetDeviceCaps( hdc, BITSPIXEL );
246 static void fghFillPixelFormatAttributes( int *attributes, const PIXELFORMATDESCRIPTOR *ppfd )
250 ATTRIB_VAL( WGL_DRAW_TO_WINDOW_ARB, GL_TRUE );
251 ATTRIB_VAL( WGL_SUPPORT_OPENGL_ARB, GL_TRUE );
252 ATTRIB_VAL( WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB );
254 ATTRIB_VAL( WGL_COLOR_BITS_ARB, ppfd->cColorBits );
255 ATTRIB_VAL( WGL_ALPHA_BITS_ARB, ppfd->cAlphaBits );
256 ATTRIB_VAL( WGL_DEPTH_BITS_ARB, ppfd->cDepthBits );
257 ATTRIB_VAL( WGL_STENCIL_BITS_ARB, ppfd->cStencilBits );
259 ATTRIB_VAL( WGL_DOUBLE_BUFFER_ARB, ( fgState.DisplayMode & GLUT_DOUBLE ) != 0 );
261 if ( fgState.DisplayMode & GLUT_SRGB ) {
262 ATTRIB_VAL( WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, TRUE );
265 ATTRIB_VAL( WGL_SAMPLE_BUFFERS_ARB, GL_TRUE );
266 ATTRIB_VAL( WGL_SAMPLES_ARB, fgState.SampleNumber );
271 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
272 unsigned char layer_type )
274 #if defined(_WIN32_WCE)
277 PIXELFORMATDESCRIPTOR pfd;
278 PIXELFORMATDESCRIPTOR* ppfd = &pfd;
284 current_hDC = CreateDC(TEXT("DISPLAY"), NULL ,NULL ,NULL);
286 current_hDC = window->Window.pContext.Device;
288 fghFillPFD( ppfd, current_hDC, layer_type );
289 pixelformat = ChoosePixelFormat( current_hDC, ppfd );
291 /* windows hack for multismapling/sRGB */
292 if ( ( fgState.DisplayMode & GLUT_MULTISAMPLE ) ||
293 ( fgState.DisplayMode & GLUT_SRGB ) )
295 HGLRC rc, rc_before=wglGetCurrentContext();
297 HDC hDC, hDC_before=wglGetCurrentDC();
300 /* create a dummy window */
301 ZeroMemory(&wndCls, sizeof(wndCls));
302 wndCls.lpfnWndProc = DefWindowProc;
303 wndCls.hInstance = fgDisplay.pDisplay.Instance;
304 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
305 wndCls.lpszClassName = _T("FREEGLUT_dummy");
306 RegisterClass( &wndCls );
308 hWnd=CreateWindow(_T("FREEGLUT_dummy"), _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.pDisplay.Instance, 0 );
310 SetPixelFormat( hDC, pixelformat, ppfd );
312 rc = wglCreateContext( hDC );
313 wglMakeCurrent(hDC, rc);
315 if ( fghIsExtensionSupported( hDC, "WGL_ARB_multisample" ) )
317 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc =
318 (PFNWGLCHOOSEPIXELFORMATARBPROC) wglGetProcAddress("wglChoosePixelFormatARB");
319 if ( wglChoosePixelFormatARBProc )
324 float fAttributes[] = { 0, 0 };
326 fghFillPixelFormatAttributes( attributes, ppfd );
327 bValid = wglChoosePixelFormatARBProc(hDC, attributes, fAttributes, 1, &iPixelFormat, &numFormats);
329 if ( bValid && numFormats > 0 )
331 pixelformat = iPixelFormat;
336 wglMakeCurrent( hDC_before, rc_before);
337 wglDeleteContext(rc);
338 ReleaseDC(hWnd, hDC);
340 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.pDisplay.Instance);
343 success = ( pixelformat != 0 ) && ( checkOnly || SetPixelFormat( current_hDC, pixelformat, ppfd ) );
346 DeleteDC(current_hDC);
349 #endif /* defined(_WIN32_WCE) */
354 void fgPlatformSetWindow ( SFG_Window *window )
356 if ( window != fgStructure.CurrentWindow )
358 if( fgStructure.CurrentWindow )
359 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
360 fgStructure.CurrentWindow->Window.pContext.Device );
364 window->Window.pContext.Device = GetDC( window->Window.Handle );
366 window->Window.pContext.Device,
367 window->Window.Context
374 void fghPlatformGetMousePos(SFG_XYUse *mouse_pos)
379 mouse_pos->X = pos.x;
380 mouse_pos->Y = pos.y;
381 mouse_pos->Use = GL_TRUE;
384 /* Returns the width of the window borders based on the window's style.
386 void fghGetBorderWidth(const DWORD windowStyle, int* xBorderWidth, int* yBorderWidth)
388 if (windowStyle & WS_THICKFRAME)
390 *xBorderWidth = GetSystemMetrics(SM_CXSIZEFRAME);
391 *yBorderWidth = GetSystemMetrics(SM_CYSIZEFRAME);
393 else if (windowStyle & WS_DLGFRAME)
395 *xBorderWidth = GetSystemMetrics(SM_CXFIXEDFRAME);
396 *yBorderWidth = GetSystemMetrics(SM_CYFIXEDFRAME);
407 /* Computes position of corners of window Rect (outer position including
408 * decorations) based on the provided client rect and based on the style
409 * of the window in question.
410 * If posIsOutside is set to true, the input client Rect is taken to follow
411 * freeGLUT's window specification convention in which the top-left corner
412 * is at the outside of the window, while the size
413 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
416 void fghComputeWindowRectFromClientArea_UseStyle( const DWORD windowStyle, RECT *clientRect, BOOL posIsOutside )
418 int xBorderWidth = 0, yBorderWidth = 0;
420 /* If window has title bar, correct rect for it */
421 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... */
424 clientRect->bottom += GetSystemMetrics( SM_CYCAPTION );
426 clientRect->top -= GetSystemMetrics( SM_CYCAPTION );
429 /* get width of window's borders (frame), correct rect for it.
430 * Note, borders can be of zero width if style does not specify borders
432 fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);
435 clientRect->right += xBorderWidth * 2;
436 clientRect->bottom += yBorderWidth * 2;
440 clientRect->left -= xBorderWidth;
441 clientRect->right += xBorderWidth;
442 clientRect->top -= yBorderWidth;
443 clientRect->bottom += yBorderWidth;
447 /* Computes position of corners of window Rect (outer position including
448 * decorations) based on the provided client rect and based on the style
449 * of the window in question. If the window pointer or the window handle
450 * is NULL, a fully decorated window (caption and border) is assumed.
451 * Furthermore, if posIsOutside is set to true, the input client Rect is
452 * taken to follow freeGLUT's window specification convention in which the
453 * top-left corner is at the outside of the window, while the size
454 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
457 void fghComputeWindowRectFromClientArea_QueryWindow( const SFG_Window *window, RECT *clientRect, BOOL posIsOutside )
459 DWORD windowStyle = 0;
461 if (window && window->Window.Handle)
462 windowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
464 windowStyle = WS_OVERLAPPEDWINDOW;
466 fghComputeWindowRectFromClientArea_UseStyle(windowStyle, clientRect, posIsOutside);
469 /* Computes position of corners of client area (drawable area) of a window
470 * based on the provided window Rect (outer position including decorations)
471 * and based on the style of the window in question. If the window pointer
472 * or the window handle is NULL, a fully decorated window (caption and
473 * border) is assumed.
474 * Furthermore, if wantPosOutside is set to true, the output client Rect
475 * will follow freeGLUT's window specification convention in which the
476 * top-left corner is at the outside of the window, the size
477 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
480 void fghComputeClientAreaFromWindowRect( const SFG_Window *window, RECT *windowRect, BOOL wantPosOutside )
482 DWORD windowStyle = 0;
483 int xBorderWidth = 0, yBorderWidth = 0;
485 if (window && window->Window.Handle)
486 windowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
488 windowStyle = WS_OVERLAPPEDWINDOW;
490 /* If window has title bar, correct rect for it */
491 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... */
494 windowRect->bottom -= GetSystemMetrics( SM_CYCAPTION );
496 windowRect->top += GetSystemMetrics( SM_CYCAPTION );
499 /* get width of window's borders (frame), correct rect for it.
500 * Note, borders can be of zero width if style does not specify borders
502 fghGetBorderWidth(windowStyle, &xBorderWidth, &yBorderWidth);
505 windowRect->right -= xBorderWidth * 2;
506 windowRect->bottom -= yBorderWidth * 2;
510 windowRect->left += xBorderWidth;
511 windowRect->right -= xBorderWidth;
512 windowRect->top += yBorderWidth;
513 windowRect->bottom -= yBorderWidth;
517 /* Gets the rect describing the client area (drawable area) of the
519 * Returns an empty rect if window pointer or window handle is NULL.
520 * If wantPosOutside is set to true, the output client Rect
521 * will follow freeGLUT's window specification convention in which the
522 * top-left corner is at the outside of the window, while the size
523 * (rect.right-rect.left,rect.bottom-rect.top) is the size of the drawable
526 RECT fghGetClientArea( const SFG_Window *window, BOOL wantPosOutside )
528 RECT windowRect = {0,0,0,0};
530 freeglut_return_val_if_fail((window && window->Window.Handle),windowRect);
533 * call GetWindowRect()
534 * (this returns the pixel coordinates of the outside of the window)
536 GetWindowRect( window->Window.Handle, &windowRect );
538 /* Then correct the results */
539 fghComputeClientAreaFromWindowRect(window, &windowRect, wantPosOutside);
552 static BOOL CALLBACK m_proc(HMONITOR mon,
557 m_proc_t *dp=(m_proc_t *)data;
560 info.cbSize=sizeof(info);
561 res=GetMonitorInfo(mon,(LPMONITORINFO)&info);
564 if( strcmp(dp->name,info.szDevice)==0 )
566 *(dp->x)=info.rcMonitor.left;
567 *(dp->y)=info.rcMonitor.top;
575 * this function returns the origin of the screen identified by
576 * fgDisplay.pDisplay.DisplayName, and 0 otherwise.
577 * This is used in fgOpenWindow to open the gamemode window on the screen
578 * identified by the -display command line argument. The function should
579 * not be called otherwise.
582 static void get_display_origin(int *xp,int *yp)
587 if( fgDisplay.pDisplay.DisplayName )
592 st.name=fgDisplay.pDisplay.DisplayName;
593 EnumDisplayMonitors(0,0,m_proc,(LPARAM)&st);
597 #pragma message( "-display parameter only works if compiled with WINVER >= 0x0500")
599 static void get_display_origin(int *xp,int *yp)
604 if( fgDisplay.pDisplay.DisplayName )
606 fgWarning( "for working -display support FreeGLUT must be compiled with WINVER >= 0x0500");
614 * Opens a window. Requires a SFG_Window object created and attached
615 * to the freeglut structure. OpenGL context is created here.
617 void fgPlatformOpenWindow( SFG_Window* window, const char* title,
618 GLboolean positionUse, int x, int y,
619 GLboolean sizeUse, int w, int h,
620 GLboolean gameMode, GLboolean isSubWindow )
628 /* Grab the window class we have registered on glutInit(): */
629 atom = GetClassInfo( fgDisplay.pDisplay.Instance, _T("FREEGLUT"), &wc );
630 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
633 /* Determine window style flags*/
636 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
637 "Game mode being invoked on a subwindow",
641 * Set the window creation flags appropriately to make the window
644 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
648 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
651 * There's a small difference between creating the top, child and
654 if ( window->IsMenu )
657 exFlags |= WS_EX_TOOLWINDOW;
659 #if defined(_WIN32_WCE)
660 /* no decorations for windows CE */
662 /* if this is not a subwindow (child), set its style based on the requested display mode */
663 else if( window->Parent == NULL )
664 if ( fgState.DisplayMode & GLUT_BORDERLESS )
666 /* no window decorations needed */
668 else if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
669 /* only window decoration is a border, no title bar or buttons */
670 flags |= WS_DLGFRAME;
672 /* window decoration are a border, title bar and buttons.
673 * NB: we later query whether the window has a title bar or
674 * not by testing for the maximize button, as the test for
675 * WS_CAPTION can be true without the window having a title
676 * bar. This style WS_OVERLAPPEDWINDOW gives you a maximize
678 flags |= WS_OVERLAPPEDWINDOW;
681 /* subwindows always have no decoration, but are marked as a child window to the OS */
685 /* determine window size and position */
688 /* if in gamemode, query the origin of specified by the -display
689 * command line parameter (if any) and offset the upper-left corner
690 * of the window so we create the window on that screen.
691 * The -display argument doesn't do anything if not trying to enter
695 get_display_origin(&xoff,&yoff);
706 if( ! window->IsMenu )
711 else /* fail safe - Windows can make a window of size (0, 0) */
712 w = h = 300; /* default window size */
714 /* store requested client area width and height */
715 window->State.Width = w;
716 window->State.Height = h;
718 #if !defined(_WIN32_WCE) /* no decorations for windows CE */
723 * Update the window dimensions, taking the window decorations
724 * into account. FreeGLUT is to create the window with the
725 * topleft outside corner at (x,y) and with client area
727 * note: don't need to do this when w=h=CW_USEDEFAULT, so in the
728 * if( sizeUse ) here is convenient.
732 windowRect.right = x+w;
733 windowRect.bottom = y+h;
735 fghComputeWindowRectFromClientArea_UseStyle(flags,&windowRect,TRUE);
737 w = windowRect.right - windowRect.left;
738 h = windowRect.bottom- windowRect.top;
740 #endif /* !defined(_WIN32_WCE) */
742 #if defined(_WIN32_WCE)
744 wchar_t* wstr = fghWstrFromStr(title);
746 window->Window.Handle = CreateWindow(
749 WS_VISIBLE | WS_POPUP,
753 fgDisplay.pDisplay.Instance,
759 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
760 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
761 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
762 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
763 ShowWindow(window->Window.Handle, SW_SHOW);
764 UpdateWindow(window->Window.Handle);
767 window->Window.Handle = CreateWindowEx(
773 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
775 fgDisplay.pDisplay.Instance,
778 #endif /* defined(_WIN32_WCE) */
780 if( !( window->Window.Handle ) )
781 fgError( "Failed to create a window (%s)!", title );
783 #if !defined(_WIN32_WCE)
784 /* Need to set requested style again, apparently Windows doesn't listen when requesting windows without title bar or borders */
785 SetWindowLong(window->Window.Handle, GWL_STYLE, flags);
786 SetWindowPos(window->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
787 #endif /* defined(_WIN32_WCE) */
789 /* Make a menu window always on top - fix Feature Request 947118 */
790 if( window->IsMenu || gameMode )
792 window->Window.Handle,
795 SWP_NOMOVE | SWP_NOSIZE
798 /* Enable multitouch: additional flag TWF_FINETOUCH, TWF_WANTPALM */
800 if (fghRegisterTouchWindow == (pRegisterTouchWindow)0xDEADBEEF)
801 fghRegisterTouchWindow = (pRegisterTouchWindow)GetProcAddress(GetModuleHandle("user32"),"RegisterTouchWindow");
802 if (fghRegisterTouchWindow)
803 fghRegisterTouchWindow( window->Window.Handle, TWF_FINETOUCH | TWF_WANTPALM );
806 #if defined(_WIN32_WCE)
807 ShowWindow( window->Window.Handle, SW_SHOW );
809 ShowWindow( window->Window.Handle,
810 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
811 #endif /* defined(_WIN32_WCE) */
813 UpdateWindow( window->Window.Handle );
814 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
819 * Closes a window, destroying the frame and OpenGL context
821 void fgPlatformCloseWindow( SFG_Window* window )
823 /* Make sure we don't close a window with current context active */
824 if( fgStructure.CurrentWindow == window )
825 wglMakeCurrent( NULL, NULL );
828 * Step through the list of windows. If the rendering context
829 * is not being used by another window, then we delete it.
835 for( iter = (SFG_Window *)fgStructure.Windows.First;
837 iter = (SFG_Window *)iter->Node.Next )
839 if( ( iter->Window.Context == window->Window.Context ) &&
845 wglDeleteContext( window->Window.Context );
848 DestroyWindow( window->Window.Handle );
854 * This function makes the current window visible
856 void fgPlatformGlutShowWindow( void )
858 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
862 * This function hides the current window
864 void fgPlatformGlutHideWindow( void )
866 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
870 * Iconify the current window (top-level windows only)
872 void fgPlatformGlutIconifyWindow( void )
874 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
878 * Set the current window's title
880 void fgPlatformGlutSetWindowTitle( const char* title )
884 wchar_t* wstr = fghWstrFromStr(title);
885 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
889 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
894 * Set the current window's iconified title
895 * There really isn't a way to set the icon name separate from the
896 * windows name in Win32, so, just set the windows name.
898 void fgPlatformGlutSetIconTitle( const char* title )
902 wchar_t* wstr = fghWstrFromStr(title);
903 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
907 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
912 * Change the current window's position
914 void fgPlatformGlutPositionWindow( int x, int y )
918 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
919 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
921 fgStructure.CurrentWindow->Window.Handle,
924 winRect.right - winRect.left,
925 winRect.bottom - winRect.top,
931 * Lowers the current window (by Z order change)
933 void fgPlatformGlutPushWindow( void )
936 fgStructure.CurrentWindow->Window.Handle,
939 SWP_NOSIZE | SWP_NOMOVE
944 * Raises the current window (by Z order change)
946 void fgPlatformGlutPopWindow( void )
949 fgStructure.CurrentWindow->Window.Handle,
952 SWP_NOSIZE | SWP_NOMOVE
957 * Resize the current window so that it fits the whole screen
959 void fgPlatformGlutFullScreen( SFG_Window *win )
961 #if !defined(_WIN32_WCE) /* FIXME: what about WinCE */
963 if (glutGet(GLUT_FULL_SCREEN))
965 /* Leave full screen state before entering fullscreen again (resizing?) */
966 glutLeaveFullScreen();
970 #if(WINVER >= 0x0500) /* Windows 2000 or later */
976 /* For fullscreen mode, first remove all window decoration
977 * and set style to popup so it will overlap the taskbar
978 * then force to maximize on the screen on which it has the most
983 /* store current window rect */
984 GetWindowRect( win->Window.Handle, &win->State.pWState.OldRect );
986 /* store current window style */
987 win->State.pWState.OldStyle = s = GetWindowLong(win->Window.Handle, GWL_STYLE);
989 /* remove decorations from style and add popup style*/
990 s &= ~WS_OVERLAPPEDWINDOW;
992 SetWindowLong(win->Window.Handle, GWL_STYLE, s);
993 SetWindowPos(win->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
995 /* For fullscreen mode, find the monitor that is covered the most
996 * by the window and get its rect as the resize target.
998 hMonitor= MonitorFromRect(&win->State.pWState.OldRect, MONITOR_DEFAULTTONEAREST);
999 mi.cbSize = sizeof(mi);
1000 GetMonitorInfo(hMonitor, &mi);
1001 rect = mi.rcMonitor;
1002 #else /* if (WINVER >= 0x0500) */
1005 /* For fullscreen mode, force the top-left corner to 0,0
1006 * and adjust the window rectangle so that the client area
1007 * covers the whole screen.
1012 rect.right = fgDisplay.ScreenWidth;
1013 rect.bottom = fgDisplay.ScreenHeight;
1015 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1016 WS_CLIPCHILDREN, FALSE );
1017 #endif /* (WINVER >= 0x0500) */
1020 * then resize window
1021 * SWP_NOACTIVATE Do not activate the window
1022 * SWP_NOOWNERZORDER Do not change position in z-order
1023 * SWP_NOSENDCHANGING Suppress WM_WINDOWPOSCHANGING message
1024 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1026 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1030 rect.right - rect.left,
1031 rect.bottom - rect.top,
1032 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1036 win->State.IsFullscreen = GL_TRUE;
1042 * If we are fullscreen, resize the current window back to its original size
1044 void fgPlatformGlutLeaveFullScreen( SFG_Window *win )
1046 #if !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1047 if (!glutGet(GLUT_FULL_SCREEN))
1053 /* restore style of window before making it fullscreen */
1054 SetWindowLong(win->Window.Handle, GWL_STYLE, win->State.pWState.OldStyle);
1055 SetWindowPos(win->Window.Handle, HWND_TOP, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
1058 SetWindowPos(win->Window.Handle,
1060 win->State.pWState.OldRect.left,
1061 win->State.pWState.OldRect.top,
1062 win->State.pWState.OldRect.right - win->State.pWState.OldRect.left,
1063 win->State.pWState.OldRect.bottom - win->State.pWState.OldRect.top,
1064 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1068 win->State.IsFullscreen = GL_FALSE;
1073 * Toggle the window's full screen state.
1075 void fgPlatformGlutFullScreenToggle( SFG_Window *win )
1077 if (!win->State.IsFullscreen)
1080 glutLeaveFullScreen();
1084 /* -- PLATFORM-SPECIFIC INTERFACE FUNCTION -------------------------------------------------- */
1086 int FGAPIENTRY __glutCreateWindowWithExit( const char *title, void (__cdecl *exit_function)(int) )
1088 __glutExitFunc = exit_function;
1089 return glutCreateWindow( title );