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 TARGET_HOST_POSIX_X11
32 #include <limits.h> /* LONG_MAX */
35 #if defined(_WIN32_WCE)
36 # include <Aygshell.h>
37 # ifdef FREEGLUT_LIB_PRAGMAS
38 # pragma comment( lib, "Aygshell.lib" )
41 static wchar_t* fghWstrFromStr(const char* str)
43 int i,len=strlen(str);
44 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
51 #endif /* defined(_WIN32_WCE) */
54 * TODO BEFORE THE STABLE RELEASE:
56 * fgChooseFBConfig() -- OK, but what about glutInitDisplayString()?
57 * fgSetupPixelFormat -- ignores the display mode settings
58 * fgOpenWindow() -- check the Win32 version, -iconic handling!
59 * fgCloseWindow() -- check the Win32 version
60 * glutCreateWindow() -- Check when default position and size is {-1,-1}
61 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
62 * glutDestroyWindow() -- check the Win32 version
63 * glutSetWindow() -- check the Win32 version
64 * glutGetWindow() -- OK
65 * glutSetWindowTitle() -- check the Win32 version
66 * glutSetIconTitle() -- check the Win32 version
67 * glutShowWindow() -- check the Win32 version
68 * glutHideWindow() -- check the Win32 version
69 * glutIconifyWindow() -- check the Win32 version
70 * glutReshapeWindow() -- check the Win32 version
71 * glutPositionWindow() -- check the Win32 version
72 * glutPushWindow() -- check the Win32 version
73 * glutPopWindow() -- check the Win32 version
76 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
79 * Chooses a visual basing on the current display mode settings
81 #if TARGET_HOST_POSIX_X11
83 GLXFBConfig* fgChooseFBConfig( void )
85 GLboolean wantIndexedMode = GL_FALSE;
89 /* First we have to process the display mode settings... */
91 * XXX Why is there a semi-colon in this #define? The code
92 * XXX that uses the macro seems to always add more semicolons...
94 #define ATTRIB(a) attributes[where++]=a;
95 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
97 if( fgState.DisplayMode & GLUT_INDEX )
99 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
100 /* Buffer size is selected later. */
102 ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
103 wantIndexedMode = GL_TRUE;
107 ATTRIB_VAL( GLX_RED_SIZE, 1 );
108 ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
109 ATTRIB_VAL( GLX_BLUE_SIZE, 1 );
110 if( fgState.DisplayMode & GLUT_ALPHA )
111 ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
114 if( fgState.DisplayMode & GLUT_DOUBLE )
115 ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
117 if( fgState.DisplayMode & GLUT_STEREO )
118 ATTRIB_VAL( GLX_STEREO, True );
120 if( fgState.DisplayMode & GLUT_DEPTH )
121 ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
123 if( fgState.DisplayMode & GLUT_STENCIL )
124 ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
126 if( fgState.DisplayMode & GLUT_ACCUM )
128 ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
129 ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
130 ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
131 if( fgState.DisplayMode & GLUT_ALPHA )
132 ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
135 if ((fgState.DisplayMode & GLUT_AUX)
136 || (fgState.DisplayMode & GLUT_AUX1)
137 || (fgState.DisplayMode & GLUT_AUX2)
138 || (fgState.DisplayMode & GLUT_AUX3)
139 || (fgState.DisplayMode & GLUT_AUX4))
141 ATTRIB_VAL(GLX_AUX_BUFFERS, fgState.AuxiliaryBufferNumber)
144 if (fgState.DisplayMode & GLUT_MULTISAMPLE)
146 ATTRIB_VAL(GLX_SAMPLE_BUFFERS, 1)
147 ATTRIB_VAL(GLX_SAMPLES, fgState.SampleNumber)
150 /* Push a null at the end of the list */
154 GLXFBConfig * fbconfigArray; /* Array of FBConfigs */
155 GLXFBConfig * fbconfig; /* The FBConfig we want */
156 int fbconfigArraySize; /* Number of FBConfigs in the array */
159 /* Get all FBConfigs that match "attributes". */
160 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
163 &fbconfigArraySize );
165 if (fbconfigArray != NULL)
167 int result; /* Returned by glXGetFBConfigAttrib, not checked. */
170 if( wantIndexedMode )
173 * In index mode, we want the largest buffer size, i.e. visual
174 * depth. Here, FBConfigs are sorted by increasing buffer size
175 * first, so FBConfigs with the largest size come last.
178 int bufferSizeMin, bufferSizeMax;
180 /* Get bufferSizeMin. */
182 glXGetFBConfigAttrib( fgDisplay.Display,
186 /* Get bufferSizeMax. */
188 glXGetFBConfigAttrib( fgDisplay.Display,
189 fbconfigArray[fbconfigArraySize - 1],
193 if (bufferSizeMax > bufferSizeMin)
196 * Free and reallocate fbconfigArray, keeping only FBConfigs
197 * with the largest buffer size.
199 XFree(fbconfigArray);
201 /* Add buffer size token at the end of the list. */
203 ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
206 fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
209 &fbconfigArraySize );
214 * We now have an array of FBConfigs, the first one being the "best"
215 * one. So we should return only this FBConfig:
219 * - pick the XID of the FBConfig we want
220 * result = glXGetFBConfigAttrib( fgDisplay.Display,
226 * XFree(fbconfigArray);
228 * - reset "attributes" with the XID
230 * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
233 * - get our FBConfig only
234 * fbconfig = glXChooseFBConfig( fgDisplay.Display,
237 * &fbconfigArraySize );
239 * However, for some configurations (for instance multisampling with
240 * Mesa 6.5.2 and ATI drivers), this does not work:
241 * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
242 * XID. Further investigation is needed.
244 * So, for now, we return the whole array of FBConfigs. This should
245 * not produce any side effects elsewhere.
247 fbconfig = fbconfigArray;
257 #endif /* TARGET_HOST_POSIX_X11 */
260 * Setup the pixel format for a Win32 window
262 #if TARGET_HOST_MS_WINDOWS
263 /* The following include file is available from SGI but is not standard:
264 * #include <GL/wglext.h>
265 * So we copy the necessary parts out of it.
266 * XXX: should local definitions for extensions be put in a separate include file?
268 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
270 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
272 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
273 #define WGL_ACCELERATION_ARB 0x2003
274 #define WGL_SUPPORT_OPENGL_ARB 0x2010
275 #define WGL_DOUBLE_BUFFER_ARB 0x2011
276 #define WGL_COLOR_BITS_ARB 0x2014
277 #define WGL_ALPHA_BITS_ARB 0x201B
278 #define WGL_DEPTH_BITS_ARB 0x2022
279 #define WGL_STENCIL_BITS_ARB 0x2023
280 #define WGL_FULL_ACCELERATION_ARB 0x2027
282 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
283 #define WGL_SAMPLES_ARB 0x2042
286 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
287 unsigned char layer_type )
289 #if defined(_WIN32_WCE)
292 PIXELFORMATDESCRIPTOR* ppfd, pfd;
293 int flags, pixelformat;
295 freeglut_return_val_if_fail( window != NULL, 0 );
296 flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
297 if( fgState.DisplayMode & GLUT_DOUBLE )
298 flags |= PFD_DOUBLEBUFFER;
300 if( fgState.DisplayMode & GLUT_STEREO )
303 #if defined(_MSC_VER)
304 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
307 /* Specify which pixel format do we opt for... */
308 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
312 if( fgState.DisplayMode & GLUT_INDEX )
314 pfd.iPixelType = PFD_TYPE_COLORINDEX;
322 pfd.iPixelType = PFD_TYPE_RGBA;
326 if ( fgState.DisplayMode & GLUT_ALPHA )
338 pfd.cAccumRedBits = 0;
339 pfd.cAccumGreenBits = 0;
340 pfd.cAccumBlueBits = 0;
341 pfd.cAccumAlphaBits = 0;
344 pfd.cStencilBits = 0;
347 pfd.cStencilBits = 8;
349 if( fgState.DisplayMode & GLUT_AUX4 )
351 else if( fgState.DisplayMode & GLUT_AUX3 )
353 else if( fgState.DisplayMode & GLUT_AUX2 )
355 else if( fgState.DisplayMode & GLUT_AUX1 )
360 pfd.iLayerType = layer_type;
363 pfd.dwVisibleMask = 0;
364 pfd.dwDamageMask = 0;
366 pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
369 pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
371 /* windows hack for multismapling */
372 if (fgState.DisplayMode&GLUT_MULTISAMPLE)
374 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB=NULL;
375 HGLRC rc, rc_before=wglGetCurrentContext();
377 HDC hDC, hDC_before=wglGetCurrentDC();
381 /* create a dummy window */
382 ZeroMemory(&wndCls, sizeof(wndCls));
383 wndCls.lpfnWndProc = DefWindowProc;
384 wndCls.hInstance = fgDisplay.Instance;
385 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
386 wndCls.lpszClassName = _T("FREEGLUT_dummy");
387 atom = RegisterClass( &wndCls );
389 hWnd=CreateWindow((LPCSTR)atom, _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
391 SetPixelFormat( hDC, pixelformat, ppfd );
393 rc = wglCreateContext( hDC );
394 wglMakeCurrent(hDC, rc);
396 wglGetEntensionsStringARB=(PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
397 if (wglGetEntensionsStringARB)
399 const char * pWglExtString=wglGetEntensionsStringARB(hDC);
402 if (strstr(pWglExtString, "WGL_ARB_multisample"))
404 int pAttributes[100];
408 float fAttributes[] = {0,0};
410 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc=NULL;
412 wglChoosePixelFormatARBProc=(PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
413 if ( wglChoosePixelFormatARBProc )
415 pAttributes[iCounter++]=WGL_DRAW_TO_WINDOW_ARB; pAttributes[iCounter++]=GL_TRUE;
416 pAttributes[iCounter++]=WGL_SUPPORT_OPENGL_ARB; pAttributes[iCounter++]=GL_TRUE;
417 pAttributes[iCounter++]=WGL_ACCELERATION_ARB; pAttributes[iCounter++]=WGL_FULL_ACCELERATION_ARB;
419 pAttributes[iCounter++]=WGL_COLOR_BITS_ARB; pAttributes[iCounter++]=pfd.cColorBits ;
420 pAttributes[iCounter++]=WGL_ALPHA_BITS_ARB; pAttributes[iCounter++]=pfd.cAlphaBits;
421 pAttributes[iCounter++]=WGL_DEPTH_BITS_ARB; pAttributes[iCounter++]=pfd.cDepthBits;
422 pAttributes[iCounter++]=WGL_STENCIL_BITS_ARB; pAttributes[iCounter++]=pfd.cStencilBits;
424 pAttributes[iCounter++]=WGL_DOUBLE_BUFFER_ARB; pAttributes[iCounter++]=(fgState.DisplayMode & GLUT_DOUBLE)!=0;
425 pAttributes[iCounter++]=WGL_SAMPLE_BUFFERS_ARB; pAttributes[iCounter++]=GL_TRUE;
426 pAttributes[iCounter++]=WGL_SAMPLES_ARB; pAttributes[iCounter++]=4;
427 pAttributes[iCounter++]=0; pAttributes[iCounter++]=0; /* terminator */
429 bValid = wglChoosePixelFormatARBProc(window->Window.Device,pAttributes,fAttributes,1,&iPixelFormat,&numFormats);
431 if (bValid && numFormats>0)
432 pixelformat=iPixelFormat;
435 wglMakeCurrent( hDC_before, rc_before);
436 wglDeleteContext(rc);
437 ReleaseDC(hWnd, hDC);
439 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
444 if( pixelformat == 0 )
449 return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
450 #endif /* defined(_WIN32_WCE) */
452 #endif /* TARGET_HOST_MS_WINDOWS */
455 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
456 * the window structure passed in.
458 void fgSetWindow ( SFG_Window *window )
460 #if TARGET_HOST_POSIX_X11
462 glXMakeContextCurrent(
464 window->Window.Handle,
465 window->Window.Handle,
466 window->Window.Context
468 #elif TARGET_HOST_MS_WINDOWS
469 if( fgStructure.CurrentWindow )
470 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
471 fgStructure.CurrentWindow->Window.Device );
475 window->Window.Device = GetDC( window->Window.Handle );
477 window->Window.Device,
478 window->Window.Context
482 fgStructure.CurrentWindow = window;
487 #if TARGET_HOST_POSIX_X11
488 static GLXContext fghCreateNewContext( SFG_Window* window )
490 int menu = ( window->IsMenu && !fgStructure.MenuContext );
491 int index_mode = ( fgState.DisplayMode & GLUT_INDEX );
492 Display *dpy = fgDisplay.Display;
493 GLXFBConfig config = *(window->Window.FBConfig);
494 int render_type = ( !menu && index_mode ) ? GLX_COLOR_INDEX_TYPE : GLX_RGBA_TYPE;
495 GLXContext share_list = NULL;
496 Bool direct = ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT );
498 return glXCreateNewContext( dpy, config, render_type, share_list, direct );
504 * Opens a window. Requires a SFG_Window object created and attached
505 * to the freeglut structure. OpenGL context is created here.
507 void fgOpenWindow( SFG_Window* window, const char* title,
508 GLboolean positionUse, int x, int y,
509 GLboolean sizeUse, int w, int h,
510 GLboolean gameMode, GLboolean isSubWindow )
512 #if TARGET_HOST_POSIX_X11
513 XVisualInfo * visualInfo;
514 XSetWindowAttributes winAttr;
515 XTextProperty textProperty;
516 XSizeHints sizeHints;
519 unsigned int current_DisplayMode = fgState.DisplayMode ;
521 /* Save the display mode if we are creating a menu window */
522 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
523 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
525 window->Window.FBConfig = fgChooseFBConfig( );
527 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
528 fgState.DisplayMode = current_DisplayMode ;
530 if( ! window->Window.FBConfig )
533 * The "fgChooseFBConfig" returned a null meaning that the visual
534 * context is not available.
535 * Try a couple of variations to see if they will work.
537 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
539 fgState.DisplayMode |= GLUT_DOUBLE ;
540 window->Window.FBConfig = fgChooseFBConfig( );
541 fgState.DisplayMode &= ~GLUT_DOUBLE;
544 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
546 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
547 window->Window.FBConfig = fgChooseFBConfig( );
548 fgState.DisplayMode |= GLUT_MULTISAMPLE;
552 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
553 "FBConfig with necessary capabilities not found", "fgOpenWindow" );
555 /* Get the X visual. */
556 visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
557 *(window->Window.FBConfig) );
560 * XXX HINT: the masks should be updated when adding/removing callbacks.
561 * XXX This might speed up message processing. Is that true?
563 * XXX A: Not appreciably, but it WILL make it easier to debug.
564 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
565 * XXX turns off events that it doesn't need and is a whole lot
566 * XXX more pleasant to trace. (Think mouse-motion! Tons of
567 * XXX ``bonus'' GUI events stream in.)
570 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
571 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
572 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
573 PointerMotionMask | ButtonMotionMask;
574 winAttr.background_pixmap = None;
575 winAttr.background_pixel = 0;
576 winAttr.border_pixel = 0;
578 winAttr.colormap = XCreateColormap(
579 fgDisplay.Display, fgDisplay.RootWindow,
580 visualInfo->visual, AllocNone
583 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
585 if( window->IsMenu || ( gameMode == GL_TRUE ) )
587 winAttr.override_redirect = True;
588 mask |= CWOverrideRedirect;
592 x = y = -1; /* default window position */
594 w = h = 300; /* default window size */
596 window->Window.Handle = XCreateWindow(
598 window->Parent == NULL ? fgDisplay.RootWindow :
599 window->Parent->Window.Handle,
601 visualInfo->depth, InputOutput,
602 visualInfo->visual, mask,
607 * The GLX context creation, possibly trying the direct context rendering
608 * or else use the current context if the user has so specified
614 * If there isn't already an OpenGL rendering context for menu
617 if( !fgStructure.MenuContext )
619 fgStructure.MenuContext =
620 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
621 fgStructure.MenuContext->MContext = fghCreateNewContext( window );
624 /* window->Window.Context = fgStructure.MenuContext->MContext; */
625 window->Window.Context = fghCreateNewContext( window );
627 else if( fgState.UseCurrentContext )
629 window->Window.Context = glXGetCurrentContext( );
631 if( ! window->Window.Context )
632 window->Window.Context = fghCreateNewContext( window );
635 window->Window.Context = fghCreateNewContext( window );
637 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
638 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
640 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
641 fgError( "Unable to force direct context rendering for window '%s'",
643 else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
644 fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
650 * XXX Assume the new window is visible by default
651 * XXX Is this a safe assumption?
653 window->State.Visible = GL_TRUE;
657 sizeHints.flags |= USPosition;
659 sizeHints.flags |= USSize;
662 * Fill in the size hints values now (the x, y, width and height
663 * settings are obsolete, are there any more WMs that support them?)
664 * Unless the X servers actually stop supporting these, we should
665 * continue to fill them in. It is *not* our place to tell the user
666 * that they should replace a window manager that they like, and which
667 * works, just because *we* think that it's not "modern" enough.
672 sizeHints.height = h;
674 wmHints.flags = StateHint;
675 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
676 /* Prepare the window and iconified window names... */
677 XStringListToTextProperty( (char **) &title, 1, &textProperty );
681 window->Window.Handle,
690 XFree( textProperty.value );
692 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
693 &fgDisplay.DeleteWindow, 1 );
695 glXMakeContextCurrent(
697 window->Window.Handle,
698 window->Window.Handle,
699 window->Window.Context
702 XMapWindow( fgDisplay.Display, window->Window.Handle );
706 #elif TARGET_HOST_MS_WINDOWS
714 /* Grab the window class we have registered on glutInit(): */
715 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
716 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
721 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
722 "Game mode being invoked on a subwindow",
726 * Set the window creation flags appropriately to make the window
729 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
733 int worig = w, horig = h;
735 #if !defined(_WIN32_WCE)
736 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
739 * Update the window dimensions, taking account of window
740 * decorations. "freeglut" is to create the window with the
741 * outside of its border at (x,y) and with dimensions (w,h).
743 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
744 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
745 GetSystemMetrics( SM_CYCAPTION );
747 #endif /* defined(_WIN32_WCE) */
754 /* setting State.Width/Height to call resize callback later */
757 if( ! window->IsMenu )
762 else /* fail safe - Windows can make a window of size (0, 0) */
763 w = h = 300; /* default window size */
764 window->State.Width = window->State.Height = -1;
768 window->State.Width = worig;
769 window->State.Height = horig;
773 * There's a small difference between creating the top, child and
776 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
778 if ( window->IsMenu )
781 exFlags |= WS_EX_TOOLWINDOW;
783 #if !defined(_WIN32_WCE)
784 else if( window->Parent == NULL )
785 flags |= WS_OVERLAPPEDWINDOW;
791 #if defined(_WIN32_WCE)
793 wchar_t* wstr = fghWstrFromStr(title);
795 window->Window.Handle = CreateWindow(
798 WS_VISIBLE | WS_POPUP,
808 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
809 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
810 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
811 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
812 ShowWindow(window->Window.Handle, SW_SHOW);
813 UpdateWindow(window->Window.Handle);
816 window->Window.Handle = CreateWindowEx(
822 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
827 #endif /* defined(_WIN32_WCE) */
829 if( !( window->Window.Handle ) )
830 fgError( "Failed to create a window (%s)!", title );
832 /* Make a menu window always on top - fix Feature Request 947118 */
833 if( window->IsMenu || gameMode )
835 window->Window.Handle,
838 SWP_NOMOVE | SWP_NOSIZE
841 /* Hack to remove the caption (title bar) and/or border
842 * and all the system menu controls.
844 WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
845 if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
847 SetWindowLong ( window->Window.Handle, GWL_STYLE,
848 WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
850 else if ( fgState.DisplayMode & GLUT_BORDERLESS )
852 SetWindowLong ( window->Window.Handle, GWL_STYLE,
853 WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
855 /* SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
856 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); */
859 #if defined(_WIN32_WCE)
860 ShowWindow( window->Window.Handle, SW_SHOW );
862 ShowWindow( window->Window.Handle,
863 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
864 #endif /* defined(_WIN32_WCE) */
866 UpdateWindow( window->Window.Handle );
867 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
871 fgSetWindow( window );
873 window->Window.DoubleBuffered =
874 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
876 if ( ! window->Window.DoubleBuffered )
878 glDrawBuffer ( GL_FRONT );
879 glReadBuffer ( GL_FRONT );
884 * Closes a window, destroying the frame and OpenGL context
886 void fgCloseWindow( SFG_Window* window )
888 #if TARGET_HOST_POSIX_X11
890 glXDestroyContext( fgDisplay.Display, window->Window.Context );
891 XFree( window->Window.FBConfig );
892 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
893 /* XFlush( fgDisplay.Display ); */ /* XXX Shouldn't need this */
895 #elif TARGET_HOST_MS_WINDOWS
897 /* Make sure we don't close a window with current context active */
898 if( fgStructure.CurrentWindow == window )
899 wglMakeCurrent( NULL, NULL );
902 * Step through the list of windows. If the rendering context
903 * is not being used by another window, then we delete it.
909 for( iter = (SFG_Window *)fgStructure.Windows.First;
911 iter = (SFG_Window *)iter->Node.Next )
913 if( ( iter->Window.Context == window->Window.Context ) &&
919 wglDeleteContext( window->Window.Context );
922 DestroyWindow( window->Window.Handle );
927 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
930 * Creates a new top-level freeglut window
932 int FGAPIENTRY glutCreateWindow( const char* title )
934 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
935 * XXX application has not already done so. The "freeglut" community
936 * XXX decided not to go this route (freeglut-developer e-mail from
937 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
938 * XXX Desired 'freeglut' behaviour when there is no current window"
940 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
942 return fgCreateWindow( NULL, title, fgState.Position.Use,
943 fgState.Position.X, fgState.Position.Y,
944 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
945 GL_FALSE, GL_FALSE )->ID;
949 * This function creates a sub window.
951 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
954 SFG_Window* window = NULL;
955 SFG_Window* parent = NULL;
957 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
958 parent = fgWindowByID( parentID );
959 freeglut_return_val_if_fail( parent != NULL, 0 );
962 x = parent->State.Width + x ;
963 if ( w >= 0 ) x -= w ;
966 if ( w < 0 ) w = parent->State.Width - x + w ;
975 y = parent->State.Height + y ;
976 if ( h >= 0 ) y -= h ;
979 if ( h < 0 ) h = parent->State.Height - y + h ;
986 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
993 * Destroys a window and all of its subwindows
995 void FGAPIENTRY glutDestroyWindow( int windowID )
998 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
999 window = fgWindowByID( windowID );
1000 freeglut_return_if_fail( window != NULL );
1002 fgExecutionState ExecState = fgState.ExecState;
1003 fgAddToWindowDestroyList( window );
1004 fgState.ExecState = ExecState;
1009 * This function selects the current window
1011 void FGAPIENTRY glutSetWindow( int ID )
1013 SFG_Window* window = NULL;
1015 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1016 if( fgStructure.CurrentWindow != NULL )
1017 if( fgStructure.CurrentWindow->ID == ID )
1020 window = fgWindowByID( ID );
1021 if( window == NULL )
1023 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1027 fgSetWindow( window );
1031 * This function returns the ID number of the current window, 0 if none exists
1033 int FGAPIENTRY glutGetWindow( void )
1035 SFG_Window *win = fgStructure.CurrentWindow;
1037 * Since GLUT did not throw an error if this function was called without a prior call to
1038 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
1039 * See Feature Request "[ 1307049 ] glutInit check".
1041 if ( ! fgState.Initialised )
1044 while ( win && win->IsMenu )
1046 return win ? win->ID : 0;
1050 * This function makes the current window visible
1052 void FGAPIENTRY glutShowWindow( void )
1054 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1055 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1057 #if TARGET_HOST_POSIX_X11
1059 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1060 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1062 #elif TARGET_HOST_MS_WINDOWS
1064 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1068 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1072 * This function hides the current window
1074 void FGAPIENTRY glutHideWindow( void )
1076 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1077 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1079 #if TARGET_HOST_POSIX_X11
1081 if( fgStructure.CurrentWindow->Parent == NULL )
1082 XWithdrawWindow( fgDisplay.Display,
1083 fgStructure.CurrentWindow->Window.Handle,
1086 XUnmapWindow( fgDisplay.Display,
1087 fgStructure.CurrentWindow->Window.Handle );
1088 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1090 #elif TARGET_HOST_MS_WINDOWS
1092 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1096 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1100 * Iconify the current window (top-level windows only)
1102 void FGAPIENTRY glutIconifyWindow( void )
1104 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1105 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1107 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
1108 #if TARGET_HOST_POSIX_X11
1110 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1112 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1114 #elif TARGET_HOST_MS_WINDOWS
1116 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1120 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1124 * Set the current window's title
1126 void FGAPIENTRY glutSetWindowTitle( const char* title )
1128 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1129 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1130 if( ! fgStructure.CurrentWindow->Parent )
1132 #if TARGET_HOST_POSIX_X11
1136 text.value = (unsigned char *) title;
1137 text.encoding = XA_STRING;
1139 text.nitems = strlen( title );
1143 fgStructure.CurrentWindow->Window.Handle,
1147 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1149 #elif TARGET_HOST_MS_WINDOWS
1152 wchar_t* wstr = fghWstrFromStr(title);
1153 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1157 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1165 * Set the current window's iconified title
1167 void FGAPIENTRY glutSetIconTitle( const char* title )
1169 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1170 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1172 if( ! fgStructure.CurrentWindow->Parent )
1174 #if TARGET_HOST_POSIX_X11
1178 text.value = (unsigned char *) title;
1179 text.encoding = XA_STRING;
1181 text.nitems = strlen( title );
1185 fgStructure.CurrentWindow->Window.Handle,
1189 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1191 #elif TARGET_HOST_MS_WINDOWS
1194 wchar_t* wstr = fghWstrFromStr(title);
1195 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1199 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1207 * Change the current window's size
1209 void FGAPIENTRY glutReshapeWindow( int width, int height )
1211 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1212 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1214 if (glutGet(GLUT_FULL_SCREEN))
1216 /* Leave full screen state before resizing. */
1217 glutFullScreenToggle();
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 (glutGet(GLUT_FULL_SCREEN))
1235 /* Leave full screen state before moving. */
1236 glutFullScreenToggle();
1239 #if TARGET_HOST_POSIX_X11
1241 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1243 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1245 #elif TARGET_HOST_MS_WINDOWS
1250 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1251 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1253 fgStructure.CurrentWindow->Window.Handle,
1256 winRect.right - winRect.left,
1257 winRect.bottom - winRect.top,
1266 * Lowers the current window (by Z order change)
1268 void FGAPIENTRY glutPushWindow( void )
1270 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1271 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1273 #if TARGET_HOST_POSIX_X11
1275 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1277 #elif TARGET_HOST_MS_WINDOWS
1280 fgStructure.CurrentWindow->Window.Handle,
1283 SWP_NOSIZE | SWP_NOMOVE
1290 * Raises the current window (by Z order change)
1292 void FGAPIENTRY glutPopWindow( void )
1294 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1295 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1297 #if TARGET_HOST_POSIX_X11
1299 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1301 #elif TARGET_HOST_MS_WINDOWS
1304 fgStructure.CurrentWindow->Window.Handle,
1307 SWP_NOSIZE | SWP_NOMOVE
1314 * Resize the current window so that it fits the whole screen
1316 void FGAPIENTRY glutFullScreen( void )
1318 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1319 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1321 if (glutGet(GLUT_FULL_SCREEN))
1323 /* Leave full screen state before resizing. */
1324 glutFullScreenToggle();
1328 #if TARGET_HOST_POSIX_X11
1330 Status status; /* Returned by XGetWindowAttributes(), not checked. */
1331 XWindowAttributes attributes;
1333 status = XGetWindowAttributes(fgDisplay.Display,
1334 fgStructure.CurrentWindow->Window.Handle,
1337 * The "x" and "y" members of "attributes" are the window's coordinates
1338 * relative to its parent, i.e. to the decoration window.
1340 XMoveResizeWindow(fgDisplay.Display,
1341 fgStructure.CurrentWindow->Window.Handle,
1344 fgDisplay.ScreenWidth,
1345 fgDisplay.ScreenHeight);
1347 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1350 /* For fullscreen mode, force the top-left corner to 0,0
1351 * and adjust the window rectangle so that the client area
1352 * covers the whole screen.
1357 rect.right = fgDisplay.ScreenWidth;
1358 rect.bottom = fgDisplay.ScreenHeight;
1360 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1361 WS_CLIPCHILDREN, FALSE );
1364 * SWP_NOACTIVATE Do not activate the window
1365 * SWP_NOOWNERZORDER Do not change position in z-order
1366 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1367 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1370 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1374 rect.right - rect.left,
1375 rect.bottom - rect.top,
1376 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1384 * Toggle the window's full screen state.
1386 void FGAPIENTRY glutFullScreenToggle( void )
1388 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
1389 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
1392 #if TARGET_HOST_POSIX_X11
1394 if (fgDisplay.StateFullScreen != None)
1400 xevent.type = ClientMessage;
1401 xevent.xclient.type = ClientMessage;
1402 xevent.xclient.serial = 0;
1403 xevent.xclient.send_event = True;
1404 xevent.xclient.display = fgDisplay.Display;
1405 xevent.xclient.window = fgStructure.CurrentWindow->Window.Handle;
1406 xevent.xclient.message_type = fgDisplay.State;
1407 xevent.xclient.format = 32;
1408 xevent.xclient.data.l[0] = 2; /* _NET_WM_STATE_TOGGLE */
1409 xevent.xclient.data.l[1] = fgDisplay.StateFullScreen;
1410 xevent.xclient.data.l[2] = 0;
1411 xevent.xclient.data.l[3] = 0;
1412 xevent.xclient.data.l[4] = 0;
1414 /*** Don't really understand how event masks work... ***/
1415 event_mask = SubstructureRedirectMask | SubstructureNotifyMask;
1417 status = XSendEvent(fgDisplay.Display,
1418 fgDisplay.RootWindow,
1422 FREEGLUT_INTERNAL_ERROR_EXIT(status != 0,
1423 "XSendEvent failed",
1424 "glutFullScreenToggle");
1430 * If the window manager is not Net WM compliant, fall back to legacy
1439 * A.Donev: Set and retrieve the window's user data
1441 void* FGAPIENTRY glutGetWindowData( void )
1443 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1444 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1445 return fgStructure.CurrentWindow->UserData;
1448 void FGAPIENTRY glutSetWindowData(void* data)
1450 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1451 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1452 fgStructure.CurrentWindow->UserData = data;
1455 /*** END OF FILE ***/