4 * Window management methods.
6 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8 * Creation date: Fri Dec 3 1999
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 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
31 #if defined(_WIN32_WCE)
32 # include <Aygshell.h>
33 # ifdef FREEGLUT_LIB_PRAGMAS
34 # pragma comment( lib, "Aygshell.lib" )
37 static wchar_t* fghWstrFromStr(const char* str)
39 int i,len=strlen(str);
40 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
47 #endif /* defined(_WIN32_WCE) */
50 * TODO BEFORE THE STABLE RELEASE:
52 * fgChooseFBConfig() -- OK, but what about glutInitDisplayString()?
53 * fgSetupPixelFormat -- ignores the display mode settings
54 * fgOpenWindow() -- check the Win32 version, -iconic handling!
55 * fgCloseWindow() -- check the Win32 version
56 * glutCreateWindow() -- Check when default position and size is {-1,-1}
57 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
58 * glutDestroyWindow() -- check the Win32 version
59 * glutSetWindow() -- check the Win32 version
60 * glutGetWindow() -- OK
61 * glutSetWindowTitle() -- check the Win32 version
62 * glutSetIconTitle() -- check the Win32 version
63 * glutShowWindow() -- check the Win32 version
64 * glutHideWindow() -- check the Win32 version
65 * glutIconifyWindow() -- check the Win32 version
66 * glutReshapeWindow() -- check the Win32 version
67 * glutPositionWindow() -- check the Win32 version
68 * glutPushWindow() -- check the Win32 version
69 * glutPopWindow() -- check the Win32 version
72 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
75 * Chooses a visual basing on the current display mode settings
77 #if TARGET_HOST_POSIX_X11
79 GLXFBConfig* fgChooseFBConfig( void )
81 GLboolean wantIndexedMode = GL_FALSE;
85 /* First we have to process the display mode settings... */
87 * XXX Why is there a semi-colon in this #define? The code
88 * XXX that uses the macro seems to always add more semicolons...
90 #define ATTRIB(a) attributes[where++]=a;
91 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
93 if( fgState.DisplayMode & GLUT_INDEX )
95 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
96 /* Buffer size is selected later. */
98 ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
99 wantIndexedMode = GL_TRUE;
103 ATTRIB_VAL( GLX_RED_SIZE, 1 );
104 ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
105 ATTRIB_VAL( GLX_BLUE_SIZE, 1 );
106 if( fgState.DisplayMode & GLUT_ALPHA )
107 ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
110 if( fgState.DisplayMode & GLUT_DOUBLE )
111 ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
113 if( fgState.DisplayMode & GLUT_STEREO )
114 ATTRIB_VAL( GLX_STEREO, True );
116 if( fgState.DisplayMode & GLUT_DEPTH )
117 ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
119 if( fgState.DisplayMode & GLUT_STENCIL )
120 ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
122 if( fgState.DisplayMode & GLUT_ACCUM )
124 ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
125 ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
126 ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
127 if( fgState.DisplayMode & GLUT_ALPHA )
128 ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
131 if( fgState.DisplayMode & GLUT_AUX1 )
132 ATTRIB_VAL( GLX_AUX_BUFFERS, 1 );
133 if( fgState.DisplayMode & GLUT_AUX2 )
134 ATTRIB_VAL( GLX_AUX_BUFFERS, 2 );
135 if( fgState.DisplayMode & GLUT_AUX3 )
136 ATTRIB_VAL( GLX_AUX_BUFFERS, 3 );
137 if( fgState.DisplayMode & GLUT_AUX4 )
138 ATTRIB_VAL( GLX_AUX_BUFFERS, 4 );
139 if ( fgState.DisplayMode & GLUT_MULTISAMPLE )
141 ATTRIB_VAL( GLX_SAMPLE_BUFFERS, 1 );
145 /* Push a null at the end of the list */
149 GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
150 GLXFBConfig * fbconfig; /* The FBConfig we want */
151 int fbconfigArraySize; /* Number of FBConfigs in the array */
154 /* Get all FBConfigs that match "attributes". */
155 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
158 &fbconfigArraySize );
160 if (fbconfigArray != NULL)
162 int result; /* Returned by glXGetFBConfigAttrib, not checked. */
165 if( wantIndexedMode )
168 * In index mode, we want the largest buffer size, i.e. visual
169 * depth. Here, FBConfigs are sorted by increasing buffer size
170 * first, so FBConfigs with the largest size come last.
173 int bufferSizeMin, bufferSizeMax;
175 /* Get bufferSizeMin. */
177 glXGetFBConfigAttrib( fgDisplay.Display,
181 /* Get bufferSizeMax. */
183 glXGetFBConfigAttrib( fgDisplay.Display,
184 fbconfigArray[fbconfigArraySize - 1],
188 if (bufferSizeMax > bufferSizeMin)
191 * Free and reallocate fbconfigArray, keeping only FBConfigs
192 * with the largest buffer size.
194 XFree(fbconfigArray);
196 /* Add buffer size token at the end of the list. */
198 ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
201 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
204 &fbconfigArraySize );
209 * We now have an array of FBConfigs, the first one being the "best"
210 * one. So we should return only this FBConfig:
214 * - pick the XID of the FBConfig we want
215 * result = glXGetFBConfigAttrib( fgDisplay.Display,
221 * XFree(fbconfigArray);
223 * - reset "attributes" with the XID
225 * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
228 * - get our FBConfig only
229 * fbconfig = glXChooseFBConfig( fgDisplay.Display,
232 * &fbconfigArraySize );
234 * However, for some configurations (for instance multisampling with
235 * Mesa 6.5.2 and ATI drivers), this does not work:
236 * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
237 * XID. Further investigation is needed.
239 * So, for now, we return the whole array of FBConfigs. This should
240 * not produce any side effects elsewhere.
242 fbconfig = fbconfigArray;
255 * Setup the pixel format for a Win32 window
257 #if TARGET_HOST_MS_WINDOWS
258 /* The following include file is available from SGI but is not standard:
259 * #include <GL/wglext.h>
260 * So we copy the necessary parts out of it.
261 * XXX: should local definitions for extensions be put in a separate include file?
263 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
265 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
267 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
268 #define WGL_ACCELERATION_ARB 0x2003
269 #define WGL_SUPPORT_OPENGL_ARB 0x2010
270 #define WGL_DOUBLE_BUFFER_ARB 0x2011
271 #define WGL_COLOR_BITS_ARB 0x2014
272 #define WGL_ALPHA_BITS_ARB 0x201B
273 #define WGL_DEPTH_BITS_ARB 0x2022
274 #define WGL_STENCIL_BITS_ARB 0x2023
275 #define WGL_FULL_ACCELERATION_ARB 0x2027
277 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
278 #define WGL_SAMPLES_ARB 0x2042
281 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
282 unsigned char layer_type )
284 #if defined(_WIN32_WCE)
287 PIXELFORMATDESCRIPTOR* ppfd, pfd;
288 int flags, pixelformat;
290 freeglut_return_val_if_fail( window != NULL, 0 );
291 flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
292 if( fgState.DisplayMode & GLUT_DOUBLE )
293 flags |= PFD_DOUBLEBUFFER;
295 if( fgState.DisplayMode & GLUT_STEREO )
298 #if defined(_MSC_VER)
299 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
302 /* Specify which pixel format do we opt for... */
303 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
307 if( fgState.DisplayMode & GLUT_INDEX )
309 pfd.iPixelType = PFD_TYPE_COLORINDEX;
317 pfd.iPixelType = PFD_TYPE_RGBA;
321 if ( fgState.DisplayMode & GLUT_ALPHA )
333 pfd.cAccumRedBits = 0;
334 pfd.cAccumGreenBits = 0;
335 pfd.cAccumBlueBits = 0;
336 pfd.cAccumAlphaBits = 0;
339 pfd.cStencilBits = 0;
342 pfd.cStencilBits = 8;
344 if( fgState.DisplayMode & GLUT_AUX4 )
346 else if( fgState.DisplayMode & GLUT_AUX3 )
348 else if( fgState.DisplayMode & GLUT_AUX2 )
350 else if( fgState.DisplayMode & GLUT_AUX1 )
355 pfd.iLayerType = layer_type;
358 pfd.dwVisibleMask = 0;
359 pfd.dwDamageMask = 0;
361 pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
364 pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
366 /* windows hack for multismapling */
367 if (fgState.DisplayMode&GLUT_MULTISAMPLE)
369 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB=NULL;
370 HGLRC rc, rc_before=wglGetCurrentContext();
372 HDC hDC, hDC_before=wglGetCurrentDC();
376 /* create a dummy window */
377 ZeroMemory(&wndCls, sizeof(wndCls));
378 wndCls.lpfnWndProc = DefWindowProc;
379 wndCls.hInstance = fgDisplay.Instance;
380 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
381 wndCls.lpszClassName = _T("FREEGLUT_dummy");
382 atom = RegisterClass( &wndCls );
384 hWnd=CreateWindow((LPCSTR)atom, _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
386 SetPixelFormat( hDC, pixelformat, ppfd );
388 rc = wglCreateContext( hDC );
389 wglMakeCurrent(hDC, rc);
391 wglGetEntensionsStringARB=(PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
392 if (wglGetEntensionsStringARB)
394 const char * pWglExtString=wglGetEntensionsStringARB(hDC);
397 if (strstr(pWglExtString, "WGL_ARB_multisample"))
399 int pAttributes[100];
403 float fAttributes[] = {0,0};
405 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc=NULL;
407 wglChoosePixelFormatARBProc=(PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
408 if ( wglChoosePixelFormatARBProc )
410 pAttributes[iCounter++]=WGL_DRAW_TO_WINDOW_ARB; pAttributes[iCounter++]=GL_TRUE;
411 pAttributes[iCounter++]=WGL_SUPPORT_OPENGL_ARB; pAttributes[iCounter++]=GL_TRUE;
412 pAttributes[iCounter++]=WGL_ACCELERATION_ARB; pAttributes[iCounter++]=WGL_FULL_ACCELERATION_ARB;
414 pAttributes[iCounter++]=WGL_COLOR_BITS_ARB; pAttributes[iCounter++]=pfd.cColorBits ;
415 pAttributes[iCounter++]=WGL_ALPHA_BITS_ARB; pAttributes[iCounter++]=pfd.cAlphaBits;
416 pAttributes[iCounter++]=WGL_DEPTH_BITS_ARB; pAttributes[iCounter++]=pfd.cDepthBits;
417 pAttributes[iCounter++]=WGL_STENCIL_BITS_ARB; pAttributes[iCounter++]=pfd.cStencilBits;
419 pAttributes[iCounter++]=WGL_DOUBLE_BUFFER_ARB; pAttributes[iCounter++]=(fgState.DisplayMode & GLUT_DOUBLE)!=0;
420 pAttributes[iCounter++]=WGL_SAMPLE_BUFFERS_ARB; pAttributes[iCounter++]=GL_TRUE;
421 pAttributes[iCounter++]=WGL_SAMPLES_ARB; pAttributes[iCounter++]=4;
422 pAttributes[iCounter++]=0; pAttributes[iCounter++]=0; /* terminator */
424 bValid = wglChoosePixelFormatARBProc(window->Window.Device,pAttributes,fAttributes,1,&iPixelFormat,&numFormats);
426 if (bValid && numFormats>0)
427 pixelformat=iPixelFormat;
430 wglMakeCurrent( hDC_before, rc_before);
431 wglDeleteContext(rc);
432 ReleaseDC(hWnd, hDC);
434 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
439 if( pixelformat == 0 )
444 return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
445 #endif /* defined(_WIN32_WCE) */
447 #endif /* TARGET_HOST_MS_WINDOWS */
450 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
451 * the window structure passed in.
453 void fgSetWindow ( SFG_Window *window )
455 #if TARGET_HOST_POSIX_X11
457 glXMakeContextCurrent(
459 window->Window.Handle,
460 window->Window.Handle,
461 window->Window.Context
463 #elif TARGET_HOST_MS_WINDOWS
464 if( fgStructure.CurrentWindow )
465 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
466 fgStructure.CurrentWindow->Window.Device );
470 window->Window.Device = GetDC( window->Window.Handle );
472 window->Window.Device,
473 window->Window.Context
477 fgStructure.CurrentWindow = window;
482 * Opens a window. Requires a SFG_Window object created and attached
483 * to the freeglut structure. OpenGL context is created here.
485 void fgOpenWindow( SFG_Window* window, const char* title,
486 GLboolean positionUse, int x, int y,
487 GLboolean sizeUse, int w, int h,
488 GLboolean gameMode, GLboolean isSubWindow )
490 #if TARGET_HOST_POSIX_X11
491 XVisualInfo * visualInfo;
492 XSetWindowAttributes winAttr;
493 XTextProperty textProperty;
494 XSizeHints sizeHints;
497 int renderType; /* GLX_RGBA_TYPE or GLX_COLOR_INDEX_TYPE */
498 unsigned int current_DisplayMode = fgState.DisplayMode ;
500 /* Save the display mode if we are creating a menu window */
501 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
502 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
504 window->Window.FBConfig = fgChooseFBConfig( );
506 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
507 fgState.DisplayMode = current_DisplayMode ;
509 if( ! window->Window.FBConfig )
512 * The "fgChooseFBConfig" returned a null meaning that the visual
513 * context is not available.
514 * Try a couple of variations to see if they will work.
516 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
518 fgState.DisplayMode |= GLUT_DOUBLE ;
519 window->Window.FBConfig = fgChooseFBConfig( );
520 fgState.DisplayMode &= ~GLUT_DOUBLE;
523 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
525 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
526 window->Window.FBConfig = fgChooseFBConfig( );
527 fgState.DisplayMode |= GLUT_MULTISAMPLE;
531 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
532 "FBConfig with necessary capabilities not found", "fgOpenWindow" );
534 /* Get the X visual. */
535 visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
536 *(window->Window.FBConfig) );
539 * XXX HINT: the masks should be updated when adding/removing callbacks.
540 * XXX This might speed up message processing. Is that true?
542 * XXX A: Not appreciably, but it WILL make it easier to debug.
543 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
544 * XXX turns off events that it doesn't need and is a whole lot
545 * XXX more pleasant to trace. (Think mouse-motion! Tons of
546 * XXX ``bonus'' GUI events stream in.)
549 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
550 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
551 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
552 PointerMotionMask | ButtonMotionMask;
553 winAttr.background_pixmap = None;
554 winAttr.background_pixel = 0;
555 winAttr.border_pixel = 0;
557 winAttr.colormap = XCreateColormap(
558 fgDisplay.Display, fgDisplay.RootWindow,
559 visualInfo->visual, AllocNone
562 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
564 if( window->IsMenu || ( gameMode == GL_TRUE ) )
566 winAttr.override_redirect = True;
567 mask |= CWOverrideRedirect;
571 x = y = -1; /* default window position */
573 w = h = 300; /* default window size */
575 window->Window.Handle = XCreateWindow(
577 window->Parent == NULL ? fgDisplay.RootWindow :
578 window->Parent->Window.Handle,
580 visualInfo->depth, InputOutput,
581 visualInfo->visual, mask,
586 * The GLX context creation, possibly trying the direct context rendering
587 * or else use the current context if the user has so specified
590 /* Set renderType. */
591 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
593 /* Display mode has been set to GLUT_RGB. */
594 renderType = GLX_RGBA_TYPE;
596 else if (fgState.DisplayMode & GLUT_INDEX)
598 renderType = GLX_COLOR_INDEX_TYPE;
602 renderType = GLX_RGBA_TYPE;
608 * If there isn't already an OpenGL rendering context for menu
611 if( !fgStructure.MenuContext )
613 fgStructure.MenuContext =
614 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
615 fgStructure.MenuContext->MContext = glXCreateNewContext(
616 fgDisplay.Display, *(window->Window.FBConfig), renderType,
617 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
621 /* window->Window.Context = fgStructure.MenuContext->MContext; */
622 window->Window.Context = glXCreateNewContext(
623 fgDisplay.Display, *(window->Window.FBConfig), renderType,
624 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
627 else if( fgState.UseCurrentContext )
629 window->Window.Context = glXGetCurrentContext( );
631 if( ! window->Window.Context )
632 window->Window.Context = glXCreateNewContext(
633 fgDisplay.Display, *(window->Window.FBConfig), renderType,
634 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
638 window->Window.Context = glXCreateNewContext(
639 fgDisplay.Display, *(window->Window.FBConfig), renderType,
640 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
643 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
644 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
646 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
647 fgError( "Unable to force direct context rendering for window '%s'",
649 else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
650 fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
656 * XXX Assume the new window is visible by default
657 * XXX Is this a safe assumption?
659 window->State.Visible = GL_TRUE;
663 sizeHints.flags |= USPosition;
665 sizeHints.flags |= USSize;
668 * Fill in the size hints values now (the x, y, width and height
669 * settings are obsolete, are there any more WMs that support them?)
670 * Unless the X servers actually stop supporting these, we should
671 * continue to fill them in. It is *not* our place to tell the user
672 * that they should replace a window manager that they like, and which
673 * works, just because *we* think that it's not "modern" enough.
678 sizeHints.height = h;
680 wmHints.flags = StateHint;
681 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
682 /* Prepare the window and iconified window names... */
683 XStringListToTextProperty( (char **) &title, 1, &textProperty );
687 window->Window.Handle,
696 XFree( textProperty.value );
698 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
699 &fgDisplay.DeleteWindow, 1 );
701 glXMakeContextCurrent(
703 window->Window.Handle,
704 window->Window.Handle,
705 window->Window.Context
708 XMapWindow( fgDisplay.Display, window->Window.Handle );
712 #elif TARGET_HOST_MS_WINDOWS
720 /* Grab the window class we have registered on glutInit(): */
721 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
722 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
727 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
728 "Game mode being invoked on a subwindow",
732 * Set the window creation flags appropriately to make the window
735 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
739 int worig = w, horig = h;
741 #if !defined(_WIN32_WCE)
742 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
745 * Update the window dimensions, taking account of window
746 * decorations. "freeglut" is to create the window with the
747 * outside of its border at (x,y) and with dimensions (w,h).
749 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
750 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
751 GetSystemMetrics( SM_CYCAPTION );
753 #endif /* defined(_WIN32_WCE) */
760 /* setting State.Width/Height to call resize callback later */
763 if( ! window->IsMenu )
768 else /* fail safe - Windows can make a window of size (0, 0) */
769 w = h = 300; /* default window size */
770 window->State.Width = window->State.Height = -1;
774 window->State.Width = worig;
775 window->State.Height = horig;
779 * There's a small difference between creating the top, child and
782 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
784 if ( window->IsMenu )
787 exFlags |= WS_EX_TOOLWINDOW;
789 #if !defined(_WIN32_WCE)
790 else if( window->Parent == NULL )
791 flags |= WS_OVERLAPPEDWINDOW;
797 #if defined(_WIN32_WCE)
799 wchar_t* wstr = fghWstrFromStr(title);
801 window->Window.Handle = CreateWindow(
804 WS_VISIBLE | WS_POPUP,
814 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
815 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
816 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
817 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
818 ShowWindow(window->Window.Handle, SW_SHOW);
819 UpdateWindow(window->Window.Handle);
822 window->Window.Handle = CreateWindowEx(
828 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
833 #endif /* defined(_WIN32_WCE) */
835 if( !( window->Window.Handle ) )
836 fgError( "Failed to create a window (%s)!", title );
838 /* Make a menu window always on top - fix Feature Request 947118 */
839 if( window->IsMenu || gameMode )
841 window->Window.Handle,
844 SWP_NOMOVE | SWP_NOSIZE
847 /* Hack to remove the caption (title bar) and/or border
848 * and all the system menu controls.
850 WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
851 if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
853 SetWindowLong ( window->Window.Handle, GWL_STYLE,
854 WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
856 else if ( fgState.DisplayMode & GLUT_BORDERLESS )
858 SetWindowLong ( window->Window.Handle, GWL_STYLE,
859 WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
861 /* SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
862 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); */
865 #if defined(_WIN32_WCE)
866 ShowWindow( window->Window.Handle, SW_SHOW );
868 ShowWindow( window->Window.Handle,
869 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
870 #endif /* defined(_WIN32_WCE) */
872 UpdateWindow( window->Window.Handle );
873 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
877 fgSetWindow( window );
879 window->Window.DoubleBuffered =
880 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
882 if ( ! window->Window.DoubleBuffered )
884 glDrawBuffer ( GL_FRONT );
885 glReadBuffer ( GL_FRONT );
890 * Closes a window, destroying the frame and OpenGL context
892 void fgCloseWindow( SFG_Window* window )
894 #if TARGET_HOST_POSIX_X11
896 glXDestroyContext( fgDisplay.Display, window->Window.Context );
897 XFree( window->Window.FBConfig );
898 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
899 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
901 #elif TARGET_HOST_MS_WINDOWS
903 /* Make sure we don't close a window with current context active */
904 if( fgStructure.CurrentWindow == window )
905 wglMakeCurrent( NULL, NULL );
908 * Step through the list of windows. If the rendering context
909 * is not being used by another window, then we delete it.
915 for( iter = (SFG_Window *)fgStructure.Windows.First;
917 iter = (SFG_Window *)iter->Node.Next )
919 if( ( iter->Window.Context == window->Window.Context ) &&
925 wglDeleteContext( window->Window.Context );
928 DestroyWindow( window->Window.Handle );
933 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
936 * Creates a new top-level freeglut window
938 int FGAPIENTRY glutCreateWindow( const char* title )
940 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
941 * XXX application has not already done so. The "freeglut" community
942 * XXX decided not to go this route (freeglut-developer e-mail from
943 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
944 * XXX Desired 'freeglut' behaviour when there is no current window"
946 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
948 return fgCreateWindow( NULL, title, fgState.Position.Use,
949 fgState.Position.X, fgState.Position.Y,
950 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
951 GL_FALSE, GL_FALSE )->ID;
955 * This function creates a sub window.
957 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
960 SFG_Window* window = NULL;
961 SFG_Window* parent = NULL;
963 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
964 parent = fgWindowByID( parentID );
965 freeglut_return_val_if_fail( parent != NULL, 0 );
968 x = parent->State.Width + x ;
969 if ( w >= 0 ) x -= w ;
972 if ( w < 0 ) w = parent->State.Width - x + w ;
981 y = parent->State.Height + y ;
982 if ( h >= 0 ) y -= h ;
985 if ( h < 0 ) h = parent->State.Height - y + h ;
992 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
999 * Destroys a window and all of its subwindows
1001 void FGAPIENTRY glutDestroyWindow( int windowID )
1004 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
1005 window = fgWindowByID( windowID );
1006 freeglut_return_if_fail( window != NULL );
1008 fgExecutionState ExecState = fgState.ExecState;
1009 fgAddToWindowDestroyList( window );
1010 fgState.ExecState = ExecState;
1015 * This function selects the current window
1017 void FGAPIENTRY glutSetWindow( int ID )
1019 SFG_Window* window = NULL;
1021 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1022 if( fgStructure.CurrentWindow != NULL )
1023 if( fgStructure.CurrentWindow->ID == ID )
1026 window = fgWindowByID( ID );
1027 if( window == NULL )
1029 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1033 fgSetWindow( window );
1037 * This function returns the ID number of the current window, 0 if none exists
1039 int FGAPIENTRY glutGetWindow( void )
1041 SFG_Window *win = fgStructure.CurrentWindow;
1043 * Since GLUT did not throw an error if this function was called without a prior call to
1044 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
1045 * See Feature Request "[ 1307049 ] glutInit check".
1047 if ( ! fgState.Initialised )
1050 while ( win && win->IsMenu )
1052 return win ? win->ID : 0;
1056 * This function makes the current window visible
1058 void FGAPIENTRY glutShowWindow( void )
1060 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1061 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1063 #if TARGET_HOST_POSIX_X11
1065 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1066 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1068 #elif TARGET_HOST_MS_WINDOWS
1070 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1074 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1078 * This function hides the current window
1080 void FGAPIENTRY glutHideWindow( void )
1082 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1083 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1085 #if TARGET_HOST_POSIX_X11
1087 if( fgStructure.CurrentWindow->Parent == NULL )
1088 XWithdrawWindow( fgDisplay.Display,
1089 fgStructure.CurrentWindow->Window.Handle,
1092 XUnmapWindow( fgDisplay.Display,
1093 fgStructure.CurrentWindow->Window.Handle );
1094 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1096 #elif TARGET_HOST_MS_WINDOWS
1098 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1102 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1106 * Iconify the current window (top-level windows only)
1108 void FGAPIENTRY glutIconifyWindow( void )
1110 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1111 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1113 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
1114 #if TARGET_HOST_POSIX_X11
1116 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1118 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1120 #elif TARGET_HOST_MS_WINDOWS
1122 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1126 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1130 * Set the current window's title
1132 void FGAPIENTRY glutSetWindowTitle( const char* title )
1134 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1135 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1136 if( ! fgStructure.CurrentWindow->Parent )
1138 #if TARGET_HOST_POSIX_X11
1142 text.value = (unsigned char *) title;
1143 text.encoding = XA_STRING;
1145 text.nitems = strlen( title );
1149 fgStructure.CurrentWindow->Window.Handle,
1153 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1155 #elif TARGET_HOST_MS_WINDOWS
1158 wchar_t* wstr = fghWstrFromStr(title);
1159 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1163 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1171 * Set the current window's iconified title
1173 void FGAPIENTRY glutSetIconTitle( const char* title )
1175 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1176 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1178 if( ! fgStructure.CurrentWindow->Parent )
1180 #if TARGET_HOST_POSIX_X11
1184 text.value = (unsigned char *) title;
1185 text.encoding = XA_STRING;
1187 text.nitems = strlen( title );
1191 fgStructure.CurrentWindow->Window.Handle,
1195 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1197 #elif TARGET_HOST_MS_WINDOWS
1200 wchar_t* wstr = fghWstrFromStr(title);
1201 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1205 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1213 * Change the current window's size
1215 void FGAPIENTRY glutReshapeWindow( int width, int height )
1217 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1218 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1220 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1221 fgStructure.CurrentWindow->State.Width = width ;
1222 fgStructure.CurrentWindow->State.Height = height;
1226 * Change the current window's position
1228 void FGAPIENTRY glutPositionWindow( int x, int y )
1230 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1231 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1233 #if TARGET_HOST_POSIX_X11
1235 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1237 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1239 #elif TARGET_HOST_MS_WINDOWS
1244 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1245 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1247 fgStructure.CurrentWindow->Window.Handle,
1250 winRect.right - winRect.left,
1251 winRect.bottom - winRect.top,
1260 * Lowers the current window (by Z order change)
1262 void FGAPIENTRY glutPushWindow( void )
1264 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1265 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1267 #if TARGET_HOST_POSIX_X11
1269 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1271 #elif TARGET_HOST_MS_WINDOWS
1274 fgStructure.CurrentWindow->Window.Handle,
1277 SWP_NOSIZE | SWP_NOMOVE
1284 * Raises the current window (by Z order change)
1286 void FGAPIENTRY glutPopWindow( void )
1288 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1289 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1291 #if TARGET_HOST_POSIX_X11
1293 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1295 #elif TARGET_HOST_MS_WINDOWS
1298 fgStructure.CurrentWindow->Window.Handle,
1301 SWP_NOSIZE | SWP_NOMOVE
1308 * Resize the current window so that it fits the whole screen
1310 void FGAPIENTRY glutFullScreen( void )
1312 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1313 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1316 #if TARGET_HOST_POSIX_X11
1322 fgStructure.CurrentWindow->Window.Handle,
1324 fgDisplay.ScreenWidth,
1325 fgDisplay.ScreenHeight
1328 XFlush( fgDisplay.Display ); /* This is needed */
1330 XTranslateCoordinates(
1332 fgStructure.CurrentWindow->Window.Handle,
1333 fgDisplay.RootWindow,
1341 fgStructure.CurrentWindow->Window.Handle,
1344 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1346 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1349 /* For fullscreen mode, force the top-left corner to 0,0
1350 * and adjust the window rectangle so that the client area
1351 * covers the whole screen.
1356 rect.right = fgDisplay.ScreenWidth;
1357 rect.bottom = fgDisplay.ScreenHeight;
1359 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1360 WS_CLIPCHILDREN, FALSE );
1363 * SWP_NOACTIVATE Do not activate the window
1364 * SWP_NOOWNERZORDER Do not change position in z-order
1365 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1366 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1369 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1373 rect.right - rect.left,
1374 rect.bottom - rect.top,
1375 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1383 * A.Donev: Set and retrieve the window's user data
1385 void* FGAPIENTRY glutGetWindowData( void )
1387 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1388 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1389 return fgStructure.CurrentWindow->UserData;
1392 void FGAPIENTRY glutSetWindowData(void* data)
1394 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1395 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1396 fgStructure.CurrentWindow->UserData = data;
1399 /*** END OF FILE ***/