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)
33 #pragma comment( lib, "Aygshell.lib" ) /* library pragmas are bad */
35 static wchar_t* fghWstrFromStr(const char* str)
37 int i,len=strlen(str);
38 wchar_t* wstr = (wchar_t*)malloc(2*len+2);
46 #endif /* defined(_WIN32_WCE) */
49 * TODO BEFORE THE STABLE RELEASE:
51 * fgChooseVisual() -- OK, but what about glutInitDisplayString()?
52 * fgSetupPixelFormat -- ignores the display mode settings
53 * fgOpenWindow() -- check the Win32 version, -iconic handling!
54 * fgCloseWindow() -- check the Win32 version
55 * glutCreateWindow() -- Check when default position and size is {-1,-1}
56 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
57 * glutDestroyWindow() -- check the Win32 version
58 * glutSetWindow() -- check the Win32 version
59 * glutGetWindow() -- OK
60 * glutSetWindowTitle() -- check the Win32 version
61 * glutSetIconTitle() -- check the Win32 version
62 * glutShowWindow() -- check the Win32 version
63 * glutHideWindow() -- check the Win32 version
64 * glutIconifyWindow() -- check the Win32 version
65 * glutReshapeWindow() -- check the Win32 version
66 * glutPositionWindow() -- check the Win32 version
67 * glutPushWindow() -- check the Win32 version
68 * glutPopWindow() -- check the Win32 version
71 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
74 * Chooses a visual basing on the current display mode settings
76 #if TARGET_HOST_POSIX_X11
78 XVisualInfo* fgChooseVisual( void )
80 #define BUFFER_SIZES 6
81 int bufferSize[BUFFER_SIZES] = { 16, 12, 8, 4, 2, 1 };
82 GLboolean wantIndexedMode = GL_FALSE;
86 /* First we have to process the display mode settings... */
88 * XXX Why is there a semi-colon in this #define? The code
89 * XXX that uses the macro seems to always add more semicolons...
91 #define ATTRIB(a) attributes[where++]=a;
92 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
94 if( fgState.DisplayMode & GLUT_INDEX )
96 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
97 wantIndexedMode = GL_TRUE;
102 ATTRIB_VAL( GLX_RED_SIZE, 1 );
103 ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
104 ATTRIB_VAL( GLX_BLUE_SIZE, 1 );
105 if( fgState.DisplayMode & GLUT_ALPHA )
106 ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
109 if( fgState.DisplayMode & GLUT_DOUBLE )
110 ATTRIB( GLX_DOUBLEBUFFER );
112 if( fgState.DisplayMode & GLUT_STEREO )
113 ATTRIB( GLX_STEREO );
115 if( fgState.DisplayMode & GLUT_DEPTH )
116 ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
118 if( fgState.DisplayMode & GLUT_STENCIL )
119 ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
121 if( fgState.DisplayMode & GLUT_ACCUM )
123 ATTRIB_VAL( GLX_ACCUM_RED_SIZE, 1 );
124 ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
125 ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE, 1 );
126 if( fgState.DisplayMode & GLUT_ALPHA )
127 ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
130 if( fgState.DisplayMode & GLUT_AUX1 )
131 ATTRIB_VAL( GLX_AUX_BUFFERS, 1 );
132 if( fgState.DisplayMode & GLUT_AUX2 )
133 ATTRIB_VAL( GLX_AUX_BUFFERS, 2 );
134 if( fgState.DisplayMode & GLUT_AUX3 )
135 ATTRIB_VAL( GLX_AUX_BUFFERS, 3 );
136 if( fgState.DisplayMode & GLUT_AUX4 )
137 ATTRIB_VAL( GLX_AUX_BUFFERS, 4 );
138 if ( fgState.DisplayMode & GLUT_MULTISAMPLE )
139 ATTRIB_VAL( GLX_SAMPLES_SGIS, 4 );
142 /* Push a null at the end of the list */
145 if( ! wantIndexedMode )
146 return glXChooseVisual( fgDisplay.Display, fgDisplay.Screen,
150 XVisualInfo* visualInfo;
154 * In indexed mode, we need to check how many bits of depth can we
155 * achieve. We do this by trying each possibility from the list
156 * given in the {bufferSize} array. If we match, we return to caller.
158 for( i=0; i<BUFFER_SIZES; i++ )
160 attributes[ 1 ] = bufferSize[ i ];
161 visualInfo = glXChooseVisual( fgDisplay.Display, fgDisplay.Screen,
163 if( visualInfo != NULL )
172 * Setup the pixel format for a Win32 window
174 #if TARGET_HOST_MS_WINDOWS
176 /* The following include file is available from SGI but is not standard:
177 * #include <GL/wglext.h>
178 * So we copy the necessary parts out of it.
180 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
182 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
184 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
185 #define WGL_ACCELERATION_ARB 0x2003
186 #define WGL_SUPPORT_OPENGL_ARB 0x2010
187 #define WGL_DOUBLE_BUFFER_ARB 0x2011
188 #define WGL_COLOR_BITS_ARB 0x2014
189 #define WGL_ALPHA_BITS_ARB 0x201B
190 #define WGL_DEPTH_BITS_ARB 0x2022
191 #define WGL_STENCIL_BITS_ARB 0x2023
192 #define WGL_FULL_ACCELERATION_ARB 0x2027
194 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
195 #define WGL_SAMPLES_ARB 0x2042
198 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
199 unsigned char layer_type )
201 #if defined(_WIN32_WCE)
204 PIXELFORMATDESCRIPTOR* ppfd, pfd;
205 int flags, pixelformat;
207 freeglut_return_val_if_fail( window != NULL, 0 );
208 flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
209 if( fgState.DisplayMode & GLUT_DOUBLE )
210 flags |= PFD_DOUBLEBUFFER;
212 if( fgState.DisplayMode & GLUT_STEREO )
215 #if defined(_MSC_VER)
216 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
219 /* Specify which pixel format do we opt for... */
220 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
224 if( fgState.DisplayMode & GLUT_INDEX )
226 pfd.iPixelType = PFD_TYPE_COLORINDEX;
234 pfd.iPixelType = PFD_TYPE_RGBA;
238 if ( fgState.DisplayMode & GLUT_ALPHA )
250 pfd.cAccumRedBits = 0;
251 pfd.cAccumGreenBits = 0;
252 pfd.cAccumBlueBits = 0;
253 pfd.cAccumAlphaBits = 0;
256 pfd.cStencilBits = 0;
259 pfd.cStencilBits = 8;
261 if( fgState.DisplayMode & GLUT_AUX4 )
263 else if( fgState.DisplayMode & GLUT_AUX3 )
265 else if( fgState.DisplayMode & GLUT_AUX2 )
267 else if( fgState.DisplayMode & GLUT_AUX1 )
272 pfd.iLayerType = layer_type;
275 pfd.dwVisibleMask = 0;
276 pfd.dwDamageMask = 0;
278 pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
281 pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
283 /* windows hack for multismapling */
284 if (fgState.DisplayMode&GLUT_MULTISAMPLE)
286 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB=NULL;
287 HGLRC rc, rc_before=wglGetCurrentContext();
289 HDC hDC, hDC_before=wglGetCurrentDC();
293 /* create a dummy window */
294 ZeroMemory(&wndCls, sizeof(wndCls));
295 wndCls.lpfnWndProc = DefWindowProc;
296 wndCls.hInstance = fgDisplay.Instance;
297 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
298 wndCls.lpszClassName = _T("FREEGLUT_dummy");
299 atom = RegisterClass( &wndCls );
301 hWnd=CreateWindow((LPCSTR)atom, _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
303 SetPixelFormat( hDC, pixelformat, ppfd );
305 rc = wglCreateContext( hDC );
306 wglMakeCurrent(hDC, rc);
308 wglGetEntensionsStringARB=(PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
309 if (wglGetEntensionsStringARB)
311 const char * pWglExtString=wglGetEntensionsStringARB(hDC);
314 if (strstr(pWglExtString, "WGL_ARB_multisample"))
316 int pAttributes[100];
320 float fAttributes[] = {0,0};
322 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc=NULL;
324 wglChoosePixelFormatARBProc=(PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
325 if ( wglChoosePixelFormatARBProc )
327 pAttributes[iCounter++]=WGL_DRAW_TO_WINDOW_ARB; pAttributes[iCounter++]=GL_TRUE;
328 pAttributes[iCounter++]=WGL_SUPPORT_OPENGL_ARB; pAttributes[iCounter++]=GL_TRUE;
329 pAttributes[iCounter++]=WGL_ACCELERATION_ARB; pAttributes[iCounter++]=WGL_FULL_ACCELERATION_ARB;
331 pAttributes[iCounter++]=WGL_COLOR_BITS_ARB; pAttributes[iCounter++]=pfd.cColorBits ;
332 pAttributes[iCounter++]=WGL_ALPHA_BITS_ARB; pAttributes[iCounter++]=pfd.cAlphaBits;
333 pAttributes[iCounter++]=WGL_DEPTH_BITS_ARB; pAttributes[iCounter++]=pfd.cDepthBits;
334 pAttributes[iCounter++]=WGL_STENCIL_BITS_ARB; pAttributes[iCounter++]=pfd.cStencilBits;
336 pAttributes[iCounter++]=WGL_DOUBLE_BUFFER_ARB; pAttributes[iCounter++]=(fgState.DisplayMode & GLUT_DOUBLE)!=0;
337 pAttributes[iCounter++]=WGL_SAMPLE_BUFFERS_ARB; pAttributes[iCounter++]=GL_TRUE;
338 pAttributes[iCounter++]=WGL_SAMPLES_ARB; pAttributes[iCounter++]=4;
339 pAttributes[iCounter++]=0; pAttributes[iCounter++]=0; /* terminator */
341 bValid = wglChoosePixelFormatARBProc(window->Window.Device,pAttributes,fAttributes,1,&iPixelFormat,&numFormats);
343 if (bValid && numFormats>0)
344 pixelformat=iPixelFormat;
347 wglMakeCurrent( hDC_before, rc_before);
348 wglDeleteContext(rc);
349 ReleaseDC(hWnd, hDC);
351 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
356 if( pixelformat == 0 )
361 return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
362 #endif /* defined(_WIN32_WCE) */
364 #endif /* TARGET_HOST_MS_WINDOWS */
367 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
368 * the window structure passed in.
370 void fgSetWindow ( SFG_Window *window )
372 #if TARGET_HOST_POSIX_X11
376 window->Window.Handle,
377 window->Window.Context
379 #elif TARGET_HOST_MS_WINDOWS
380 if( fgStructure.CurrentWindow )
381 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
382 fgStructure.CurrentWindow->Window.Device );
386 window->Window.Device = GetDC( window->Window.Handle );
388 window->Window.Device,
389 window->Window.Context
393 fgStructure.CurrentWindow = window;
398 * Opens a window. Requires a SFG_Window object created and attached
399 * to the freeglut structure. OpenGL context is created here.
401 void fgOpenWindow( SFG_Window* window, const char* title,
402 int x, int y, int w, int h,
403 GLboolean gameMode, GLboolean isSubWindow )
405 #if TARGET_HOST_POSIX_X11
406 XSetWindowAttributes winAttr;
407 XTextProperty textProperty;
408 XSizeHints sizeHints;
411 unsigned int current_DisplayMode = fgState.DisplayMode ;
413 /* Save the display mode if we are creating a menu window */
414 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
415 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
417 window->Window.VisualInfo = fgChooseVisual( );
419 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
420 fgState.DisplayMode = current_DisplayMode ;
422 if( ! window->Window.VisualInfo )
425 * The "fgChooseVisual" returned a null meaning that the visual
426 * context is not available.
427 * Try a couple of variations to see if they will work.
429 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
431 fgState.DisplayMode |= GLUT_DOUBLE ;
432 window->Window.VisualInfo = fgChooseVisual( );
433 fgState.DisplayMode &= ~GLUT_DOUBLE;
436 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
438 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
439 window->Window.VisualInfo = fgChooseVisual( );
440 fgState.DisplayMode &= GLUT_MULTISAMPLE;
444 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.VisualInfo != NULL,
445 "Visual with necessary capabilities not found", "fgOpenWindow" );
448 * XXX HINT: the masks should be updated when adding/removing callbacks.
449 * XXX This might speed up message processing. Is that true?
451 * XXX A: Not appreciably, but it WILL make it easier to debug.
452 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
453 * XXX turns off events that it doesn't need and is a whole lot
454 * XXX more pleasant to trace. (Think mouse-motion! Tons of
455 * XXX ``bonus'' GUI events stream in.)
458 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
459 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
460 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
461 PointerMotionMask | ButtonMotionMask;
462 winAttr.background_pixmap = None;
463 winAttr.background_pixel = 0;
464 winAttr.border_pixel = 0;
466 winAttr.colormap = XCreateColormap(
467 fgDisplay.Display, fgDisplay.RootWindow,
468 window->Window.VisualInfo->visual, AllocNone
471 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
473 if( window->IsMenu || ( gameMode == GL_TRUE ) )
475 winAttr.override_redirect = True;
476 mask |= CWOverrideRedirect;
479 window->Window.Handle = XCreateWindow(
481 window->Parent == NULL ? fgDisplay.RootWindow :
482 window->Parent->Window.Handle,
484 window->Window.VisualInfo->depth, InputOutput,
485 window->Window.VisualInfo->visual, mask,
490 * The GLX context creation, possibly trying the direct context rendering
491 * or else use the current context if the user has so specified
496 * If there isn't already an OpenGL rendering context for menu
499 if( !fgStructure.MenuContext )
501 fgStructure.MenuContext =
502 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
503 fgStructure.MenuContext->MVisualInfo = window->Window.VisualInfo;
504 fgStructure.MenuContext->MContext = glXCreateContext(
505 fgDisplay.Display, fgStructure.MenuContext->MVisualInfo,
506 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
510 /* window->Window.Context = fgStructure.MenuContext->MContext; */
511 window->Window.Context = glXCreateContext(
512 fgDisplay.Display, window->Window.VisualInfo,
513 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
516 else if( fgState.UseCurrentContext )
518 window->Window.Context = glXGetCurrentContext( );
520 if( ! window->Window.Context )
521 window->Window.Context = glXCreateContext(
522 fgDisplay.Display, window->Window.VisualInfo,
523 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
527 window->Window.Context = glXCreateContext(
528 fgDisplay.Display, window->Window.VisualInfo,
529 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
532 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
533 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
535 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
536 fgError( "Unable to force direct context rendering for window '%s'",
538 else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
539 fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
545 * XXX Assume the new window is visible by default
546 * XXX Is this a safe assumption?
548 window->State.Visible = GL_TRUE;
551 if ( fgState.Position.Use )
552 sizeHints.flags |= USPosition;
553 if ( fgState.Size.Use )
554 sizeHints.flags |= USSize;
557 * Fill in the size hints values now (the x, y, width and height
558 * settings are obsolete, are there any more WMs that support them?)
559 * Unless the X servers actually stop supporting these, we should
560 * continue to fill them in. It is *not* our place to tell the user
561 * that they should replace a window manager that they like, and which
562 * works, just because *we* think that it's not "modern" enough.
567 sizeHints.height = h;
569 wmHints.flags = StateHint;
570 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
571 /* Prepare the window and iconified window names... */
572 XStringListToTextProperty( (char **) &title, 1, &textProperty );
576 window->Window.Handle,
585 XFree( textProperty.value );
587 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
588 &fgDisplay.DeleteWindow, 1 );
592 window->Window.Handle,
593 window->Window.Context
596 XMapWindow( fgDisplay.Display, window->Window.Handle );
598 #elif TARGET_HOST_MS_WINDOWS
605 /* Grab the window class we have registered on glutInit(): */
606 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
607 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
612 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
613 "Game mode being invoked on a subwindow",
617 * Set the window creation flags appropriately to make the window
620 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
624 #if !defined(_WIN32_WCE)
625 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
628 * Update the window dimensions, taking account of window
629 * decorations. "freeglut" is to create the window with the
630 * outside of its border at (x,y) and with dimensions (w,h).
632 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
633 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
634 GetSystemMetrics( SM_CYCAPTION );
636 #endif /* defined(_WIN32_WCE) */
638 if( ! fgState.Position.Use )
643 if( ! fgState.Size.Use )
650 * There's a small difference between creating the top, child and
653 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
655 if ( window->IsMenu )
658 exFlags |= WS_EX_TOOLWINDOW;
660 #if !defined(_WIN32_WCE)
661 else if( window->Parent == NULL )
662 flags |= WS_OVERLAPPEDWINDOW;
668 #if defined(_WIN32_WCE)
670 wchar_t* wstr = fghWstrFromStr(title);
672 window->Window.Handle = CreateWindow(
675 WS_VISIBLE | WS_POPUP,
685 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
686 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
687 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
688 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
689 ShowWindow(window->Window.Handle, SW_SHOW);
690 UpdateWindow(window->Window.Handle);
693 window->Window.Handle = CreateWindowEx(
699 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
704 #endif /* defined(_WIN32_WCE) */
706 if( !( window->Window.Handle ) )
707 fgError( "Failed to create a window (%s)!", title );
709 #if defined(_WIN32_WCE)
710 ShowWindow( window->Window.Handle, SW_SHOW );
712 ShowWindow( window->Window.Handle,
713 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
714 #endif /* defined(_WIN32_WCE) */
716 UpdateWindow( window->Window.Handle );
717 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
721 fgSetWindow( window );
723 window->Window.DoubleBuffered =
724 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
726 if ( ! window->Window.DoubleBuffered )
728 glDrawBuffer ( GL_FRONT );
729 glReadBuffer ( GL_FRONT );
734 * Closes a window, destroying the frame and OpenGL context
736 void fgCloseWindow( SFG_Window* window )
738 #if TARGET_HOST_POSIX_X11
740 glXDestroyContext( fgDisplay.Display, window->Window.Context );
741 XFree( window->Window.VisualInfo );
742 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
743 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
745 #elif TARGET_HOST_MS_WINDOWS
747 /* Make sure we don't close a window with current context active */
748 if( fgStructure.CurrentWindow == window )
749 wglMakeCurrent( NULL, NULL );
752 * Step through the list of windows. If the rendering context
753 * is not being used by another window, then we delete it.
759 for( iter = (SFG_Window *)fgStructure.Windows.First;
761 iter = (SFG_Window *)iter->Node.Next )
763 if( ( iter->Window.Context == window->Window.Context ) &&
769 wglDeleteContext( window->Window.Context );
772 DestroyWindow( window->Window.Handle );
777 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
780 * Creates a new top-level freeglut window
782 int FGAPIENTRY glutCreateWindow( const char* title )
784 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
785 * XXX application has not already done so. The "freeglut" community
786 * XXX decided not to go this route (freeglut-developer e-mail from
787 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
788 * XXX Desired 'freeglut' behaviour when there is no current window"
790 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
792 return fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
793 fgState.Size.X, fgState.Size.Y, GL_FALSE,
798 * This function creates a sub window.
800 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
803 SFG_Window* window = NULL;
804 SFG_Window* parent = NULL;
806 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
807 parent = fgWindowByID( parentID );
808 freeglut_return_val_if_fail( parent != NULL, 0 );
811 x = parent->State.Width + x ;
812 if ( w >= 0 ) x -= w ;
815 if ( w < 0 ) w = parent->State.Width - x + w ;
824 y = parent->State.Height + y ;
825 if ( h >= 0 ) y -= h ;
828 if ( h < 0 ) h = parent->State.Height - y + h ;
835 window = fgCreateWindow( parent, "", x, y, w, h, GL_FALSE, GL_FALSE );
842 * Destroys a window and all of its subwindows
844 void FGAPIENTRY glutDestroyWindow( int windowID )
847 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
848 window = fgWindowByID( windowID );
849 freeglut_return_if_fail( window != NULL );
851 fgExecutionState ExecState = fgState.ExecState;
852 fgAddToWindowDestroyList( window );
853 fgState.ExecState = ExecState;
858 * This function selects the current window
860 void FGAPIENTRY glutSetWindow( int ID )
862 SFG_Window* window = NULL;
864 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
865 if( fgStructure.CurrentWindow != NULL )
866 if( fgStructure.CurrentWindow->ID == ID )
869 window = fgWindowByID( ID );
872 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
876 fgSetWindow( window );
880 * This function returns the ID number of the current window, 0 if none exists
882 int FGAPIENTRY glutGetWindow( void )
884 SFG_Window *win = fgStructure.CurrentWindow;
885 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindow" );
886 while ( win && win->IsMenu )
888 return win ? win->ID : 0;
892 * This function makes the current window visible
894 void FGAPIENTRY glutShowWindow( void )
896 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
897 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
899 #if TARGET_HOST_POSIX_X11
901 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
902 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
904 #elif TARGET_HOST_MS_WINDOWS
906 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
910 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
914 * This function hides the current window
916 void FGAPIENTRY glutHideWindow( void )
918 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
919 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
921 #if TARGET_HOST_POSIX_X11
923 if( fgStructure.CurrentWindow->Parent == NULL )
924 XWithdrawWindow( fgDisplay.Display,
925 fgStructure.CurrentWindow->Window.Handle,
928 XUnmapWindow( fgDisplay.Display,
929 fgStructure.CurrentWindow->Window.Handle );
930 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
932 #elif TARGET_HOST_MS_WINDOWS
934 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
938 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
942 * Iconify the current window (top-level windows only)
944 void FGAPIENTRY glutIconifyWindow( void )
946 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
947 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
949 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
950 #if TARGET_HOST_POSIX_X11
952 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
954 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
956 #elif TARGET_HOST_MS_WINDOWS
958 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
962 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
966 * Set the current window's title
968 void FGAPIENTRY glutSetWindowTitle( const char* title )
970 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
971 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
972 if( ! fgStructure.CurrentWindow->Parent )
974 #if TARGET_HOST_POSIX_X11
978 text.value = (unsigned char *) title;
979 text.encoding = XA_STRING;
981 text.nitems = strlen( title );
985 fgStructure.CurrentWindow->Window.Handle,
989 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
991 #elif TARGET_HOST_MS_WINDOWS
994 wchar_t* wstr = fghWstrFromStr(title);
995 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
999 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1007 * Set the current window's iconified title
1009 void FGAPIENTRY glutSetIconTitle( const char* title )
1011 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1012 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1014 if( ! fgStructure.CurrentWindow->Parent )
1016 #if TARGET_HOST_POSIX_X11
1020 text.value = (unsigned char *) title;
1021 text.encoding = XA_STRING;
1023 text.nitems = strlen( title );
1027 fgStructure.CurrentWindow->Window.Handle,
1031 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1033 #elif TARGET_HOST_MS_WINDOWS
1036 wchar_t* wstr = fghWstrFromStr(title);
1037 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1041 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1049 * Change the current window's size
1051 void FGAPIENTRY glutReshapeWindow( int width, int height )
1053 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1054 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1056 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1057 fgStructure.CurrentWindow->State.Width = width ;
1058 fgStructure.CurrentWindow->State.Height = height;
1062 * Change the current window's position
1064 void FGAPIENTRY glutPositionWindow( int x, int y )
1066 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1067 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1069 #if TARGET_HOST_POSIX_X11
1071 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1073 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1075 #elif TARGET_HOST_MS_WINDOWS
1080 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1081 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1083 fgStructure.CurrentWindow->Window.Handle,
1086 winRect.right - winRect.left,
1087 winRect.bottom - winRect.top,
1096 * Lowers the current window (by Z order change)
1098 void FGAPIENTRY glutPushWindow( void )
1100 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1101 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1103 #if TARGET_HOST_POSIX_X11
1105 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1107 #elif TARGET_HOST_MS_WINDOWS
1110 fgStructure.CurrentWindow->Window.Handle,
1113 SWP_NOSIZE | SWP_NOMOVE
1120 * Raises the current window (by Z order change)
1122 void FGAPIENTRY glutPopWindow( void )
1124 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1125 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1127 #if TARGET_HOST_POSIX_X11
1129 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1131 #elif TARGET_HOST_MS_WINDOWS
1134 fgStructure.CurrentWindow->Window.Handle,
1137 SWP_NOSIZE | SWP_NOMOVE
1144 * Resize the current window so that it fits the whole screen
1146 void FGAPIENTRY glutFullScreen( void )
1148 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1149 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1152 #if TARGET_HOST_POSIX_X11
1158 fgStructure.CurrentWindow->Window.Handle,
1160 fgDisplay.ScreenWidth,
1161 fgDisplay.ScreenHeight
1164 XFlush( fgDisplay.Display ); /* This is needed */
1166 XTranslateCoordinates(
1168 fgStructure.CurrentWindow->Window.Handle,
1169 fgDisplay.RootWindow,
1177 fgStructure.CurrentWindow->Window.Handle,
1180 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1182 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1185 /* For fullscreen mode, force the top-left corner to 0,0
1186 * and adjust the window rectangle so that the client area
1187 * covers the whole screen.
1192 rect.right = fgDisplay.ScreenWidth;
1193 rect.bottom = fgDisplay.ScreenHeight;
1195 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1196 WS_CLIPCHILDREN, FALSE );
1199 * SWP_NOACTIVATE Do not activate the window
1200 * SWP_NOOWNERZORDER Do not change position in z-order
1201 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1202 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1205 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1209 rect.right - rect.left,
1210 rect.bottom - rect.top,
1211 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1219 * A.Donev: Set and retrieve the window's user data
1221 void* FGAPIENTRY glutGetWindowData( void )
1223 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1224 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1225 return fgStructure.CurrentWindow->UserData;
1228 void FGAPIENTRY glutSetWindowData(void* data)
1230 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1231 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1232 fgStructure.CurrentWindow->UserData = data;
1235 /*** END OF FILE ***/