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 * fgChooseVisual() -- 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 XVisualInfo* fgChooseVisual( void )
81 #define BUFFER_SIZES 6
82 int bufferSize[BUFFER_SIZES] = { 16, 12, 8, 4, 2, 1 };
83 GLboolean wantIndexedMode = GL_FALSE;
87 /* First we have to process the display mode settings... */
89 * XXX Why is there a semi-colon in this #define? The code
90 * XXX that uses the macro seems to always add more semicolons...
92 #define ATTRIB(a) attributes[where++]=a;
93 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
95 if( fgState.DisplayMode & GLUT_INDEX )
97 ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
98 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( GLX_DOUBLEBUFFER );
113 if( fgState.DisplayMode & GLUT_STEREO )
114 ATTRIB( GLX_STEREO );
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 )
140 ATTRIB_VAL( GLX_SAMPLES_SGIS, 4 );
143 /* Push a null at the end of the list */
146 if( ! wantIndexedMode )
147 return glXChooseVisual( fgDisplay.Display, fgDisplay.Screen,
151 XVisualInfo* visualInfo;
155 * In indexed mode, we need to check how many bits of depth can we
156 * achieve. We do this by trying each possibility from the list
157 * given in the {bufferSize} array. If we match, we return to caller.
159 for( i=0; i<BUFFER_SIZES; i++ )
161 attributes[ 1 ] = bufferSize[ i ];
162 visualInfo = glXChooseVisual( fgDisplay.Display, fgDisplay.Screen,
164 if( visualInfo != NULL )
173 * Setup the pixel format for a Win32 window
175 #if TARGET_HOST_MS_WINDOWS
177 /* The following include file is available from SGI but is not standard:
178 * #include <GL/wglext.h>
179 * So we copy the necessary parts out of it.
181 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
183 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
185 #define WGL_DRAW_TO_WINDOW_ARB 0x2001
186 #define WGL_ACCELERATION_ARB 0x2003
187 #define WGL_SUPPORT_OPENGL_ARB 0x2010
188 #define WGL_DOUBLE_BUFFER_ARB 0x2011
189 #define WGL_COLOR_BITS_ARB 0x2014
190 #define WGL_ALPHA_BITS_ARB 0x201B
191 #define WGL_DEPTH_BITS_ARB 0x2022
192 #define WGL_STENCIL_BITS_ARB 0x2023
193 #define WGL_FULL_ACCELERATION_ARB 0x2027
195 #define WGL_SAMPLE_BUFFERS_ARB 0x2041
196 #define WGL_SAMPLES_ARB 0x2042
199 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
200 unsigned char layer_type )
202 #if defined(_WIN32_WCE)
205 PIXELFORMATDESCRIPTOR* ppfd, pfd;
206 int flags, pixelformat;
208 freeglut_return_val_if_fail( window != NULL, 0 );
209 flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
210 if( fgState.DisplayMode & GLUT_DOUBLE )
211 flags |= PFD_DOUBLEBUFFER;
213 if( fgState.DisplayMode & GLUT_STEREO )
216 #if defined(_MSC_VER)
217 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
220 /* Specify which pixel format do we opt for... */
221 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
225 if( fgState.DisplayMode & GLUT_INDEX )
227 pfd.iPixelType = PFD_TYPE_COLORINDEX;
235 pfd.iPixelType = PFD_TYPE_RGBA;
239 if ( fgState.DisplayMode & GLUT_ALPHA )
251 pfd.cAccumRedBits = 0;
252 pfd.cAccumGreenBits = 0;
253 pfd.cAccumBlueBits = 0;
254 pfd.cAccumAlphaBits = 0;
257 pfd.cStencilBits = 0;
260 pfd.cStencilBits = 8;
262 if( fgState.DisplayMode & GLUT_AUX4 )
264 else if( fgState.DisplayMode & GLUT_AUX3 )
266 else if( fgState.DisplayMode & GLUT_AUX2 )
268 else if( fgState.DisplayMode & GLUT_AUX1 )
273 pfd.iLayerType = layer_type;
276 pfd.dwVisibleMask = 0;
277 pfd.dwDamageMask = 0;
279 pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
282 pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
284 /* windows hack for multismapling */
285 if (fgState.DisplayMode&GLUT_MULTISAMPLE)
287 PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB=NULL;
288 HGLRC rc, rc_before=wglGetCurrentContext();
290 HDC hDC, hDC_before=wglGetCurrentDC();
294 /* create a dummy window */
295 ZeroMemory(&wndCls, sizeof(wndCls));
296 wndCls.lpfnWndProc = DefWindowProc;
297 wndCls.hInstance = fgDisplay.Instance;
298 wndCls.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
299 wndCls.lpszClassName = _T("FREEGLUT_dummy");
300 atom = RegisterClass( &wndCls );
302 hWnd=CreateWindow((LPCSTR)atom, _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
304 SetPixelFormat( hDC, pixelformat, ppfd );
306 rc = wglCreateContext( hDC );
307 wglMakeCurrent(hDC, rc);
309 wglGetEntensionsStringARB=(PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
310 if (wglGetEntensionsStringARB)
312 const char * pWglExtString=wglGetEntensionsStringARB(hDC);
315 if (strstr(pWglExtString, "WGL_ARB_multisample"))
317 int pAttributes[100];
321 float fAttributes[] = {0,0};
323 PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc=NULL;
325 wglChoosePixelFormatARBProc=(PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
326 if ( wglChoosePixelFormatARBProc )
328 pAttributes[iCounter++]=WGL_DRAW_TO_WINDOW_ARB; pAttributes[iCounter++]=GL_TRUE;
329 pAttributes[iCounter++]=WGL_SUPPORT_OPENGL_ARB; pAttributes[iCounter++]=GL_TRUE;
330 pAttributes[iCounter++]=WGL_ACCELERATION_ARB; pAttributes[iCounter++]=WGL_FULL_ACCELERATION_ARB;
332 pAttributes[iCounter++]=WGL_COLOR_BITS_ARB; pAttributes[iCounter++]=pfd.cColorBits ;
333 pAttributes[iCounter++]=WGL_ALPHA_BITS_ARB; pAttributes[iCounter++]=pfd.cAlphaBits;
334 pAttributes[iCounter++]=WGL_DEPTH_BITS_ARB; pAttributes[iCounter++]=pfd.cDepthBits;
335 pAttributes[iCounter++]=WGL_STENCIL_BITS_ARB; pAttributes[iCounter++]=pfd.cStencilBits;
337 pAttributes[iCounter++]=WGL_DOUBLE_BUFFER_ARB; pAttributes[iCounter++]=(fgState.DisplayMode & GLUT_DOUBLE)!=0;
338 pAttributes[iCounter++]=WGL_SAMPLE_BUFFERS_ARB; pAttributes[iCounter++]=GL_TRUE;
339 pAttributes[iCounter++]=WGL_SAMPLES_ARB; pAttributes[iCounter++]=4;
340 pAttributes[iCounter++]=0; pAttributes[iCounter++]=0; /* terminator */
342 bValid = wglChoosePixelFormatARBProc(window->Window.Device,pAttributes,fAttributes,1,&iPixelFormat,&numFormats);
344 if (bValid && numFormats>0)
345 pixelformat=iPixelFormat;
348 wglMakeCurrent( hDC_before, rc_before);
349 wglDeleteContext(rc);
350 ReleaseDC(hWnd, hDC);
352 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
357 if( pixelformat == 0 )
362 return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
363 #endif /* defined(_WIN32_WCE) */
365 #endif /* TARGET_HOST_MS_WINDOWS */
368 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
369 * the window structure passed in.
371 void fgSetWindow ( SFG_Window *window )
373 #if TARGET_HOST_POSIX_X11
377 window->Window.Handle,
378 window->Window.Context
380 #elif TARGET_HOST_MS_WINDOWS
381 if( fgStructure.CurrentWindow )
382 ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
383 fgStructure.CurrentWindow->Window.Device );
387 window->Window.Device = GetDC( window->Window.Handle );
389 window->Window.Device,
390 window->Window.Context
394 fgStructure.CurrentWindow = window;
399 * Opens a window. Requires a SFG_Window object created and attached
400 * to the freeglut structure. OpenGL context is created here.
402 void fgOpenWindow( SFG_Window* window, const char* title,
403 int x, int y, int w, int h,
404 GLboolean gameMode, GLboolean isSubWindow )
406 #if TARGET_HOST_POSIX_X11
407 XSetWindowAttributes winAttr;
408 XTextProperty textProperty;
409 XSizeHints sizeHints;
412 unsigned int current_DisplayMode = fgState.DisplayMode ;
414 /* Save the display mode if we are creating a menu window */
415 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
416 fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
418 window->Window.VisualInfo = fgChooseVisual( );
420 if( window->IsMenu && ( ! fgStructure.MenuContext ) )
421 fgState.DisplayMode = current_DisplayMode ;
423 if( ! window->Window.VisualInfo )
426 * The "fgChooseVisual" returned a null meaning that the visual
427 * context is not available.
428 * Try a couple of variations to see if they will work.
430 if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
432 fgState.DisplayMode |= GLUT_DOUBLE ;
433 window->Window.VisualInfo = fgChooseVisual( );
434 fgState.DisplayMode &= ~GLUT_DOUBLE;
437 if( fgState.DisplayMode & GLUT_MULTISAMPLE )
439 fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
440 window->Window.VisualInfo = fgChooseVisual( );
441 fgState.DisplayMode |= GLUT_MULTISAMPLE;
445 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.VisualInfo != NULL,
446 "Visual with necessary capabilities not found", "fgOpenWindow" );
449 * XXX HINT: the masks should be updated when adding/removing callbacks.
450 * XXX This might speed up message processing. Is that true?
452 * XXX A: Not appreciably, but it WILL make it easier to debug.
453 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
454 * XXX turns off events that it doesn't need and is a whole lot
455 * XXX more pleasant to trace. (Think mouse-motion! Tons of
456 * XXX ``bonus'' GUI events stream in.)
459 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
460 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
461 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
462 PointerMotionMask | ButtonMotionMask;
463 winAttr.background_pixmap = None;
464 winAttr.background_pixel = 0;
465 winAttr.border_pixel = 0;
467 winAttr.colormap = XCreateColormap(
468 fgDisplay.Display, fgDisplay.RootWindow,
469 window->Window.VisualInfo->visual, AllocNone
472 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
474 if( window->IsMenu || ( gameMode == GL_TRUE ) )
476 winAttr.override_redirect = True;
477 mask |= CWOverrideRedirect;
480 window->Window.Handle = XCreateWindow(
482 window->Parent == NULL ? fgDisplay.RootWindow :
483 window->Parent->Window.Handle,
485 window->Window.VisualInfo->depth, InputOutput,
486 window->Window.VisualInfo->visual, mask,
491 * The GLX context creation, possibly trying the direct context rendering
492 * or else use the current context if the user has so specified
497 * If there isn't already an OpenGL rendering context for menu
500 if( !fgStructure.MenuContext )
502 fgStructure.MenuContext =
503 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
504 fgStructure.MenuContext->MVisualInfo = window->Window.VisualInfo;
505 fgStructure.MenuContext->MContext = glXCreateContext(
506 fgDisplay.Display, fgStructure.MenuContext->MVisualInfo,
507 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
511 /* window->Window.Context = fgStructure.MenuContext->MContext; */
512 window->Window.Context = glXCreateContext(
513 fgDisplay.Display, window->Window.VisualInfo,
514 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
517 else if( fgState.UseCurrentContext )
519 window->Window.Context = glXGetCurrentContext( );
521 if( ! window->Window.Context )
522 window->Window.Context = glXCreateContext(
523 fgDisplay.Display, window->Window.VisualInfo,
524 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
528 window->Window.Context = glXCreateContext(
529 fgDisplay.Display, window->Window.VisualInfo,
530 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
533 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
534 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
536 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
537 fgError( "Unable to force direct context rendering for window '%s'",
539 else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
540 fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
546 * XXX Assume the new window is visible by default
547 * XXX Is this a safe assumption?
549 window->State.Visible = GL_TRUE;
552 if ( fgState.Position.Use )
553 sizeHints.flags |= USPosition;
554 if ( fgState.Size.Use )
555 sizeHints.flags |= USSize;
558 * Fill in the size hints values now (the x, y, width and height
559 * settings are obsolete, are there any more WMs that support them?)
560 * Unless the X servers actually stop supporting these, we should
561 * continue to fill them in. It is *not* our place to tell the user
562 * that they should replace a window manager that they like, and which
563 * works, just because *we* think that it's not "modern" enough.
568 sizeHints.height = h;
570 wmHints.flags = StateHint;
571 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
572 /* Prepare the window and iconified window names... */
573 XStringListToTextProperty( (char **) &title, 1, &textProperty );
577 window->Window.Handle,
586 XFree( textProperty.value );
588 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
589 &fgDisplay.DeleteWindow, 1 );
593 window->Window.Handle,
594 window->Window.Context
597 XMapWindow( fgDisplay.Display, window->Window.Handle );
599 #elif TARGET_HOST_MS_WINDOWS
606 /* Grab the window class we have registered on glutInit(): */
607 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
608 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
613 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
614 "Game mode being invoked on a subwindow",
618 * Set the window creation flags appropriately to make the window
621 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
625 #if !defined(_WIN32_WCE)
626 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
629 * Update the window dimensions, taking account of window
630 * decorations. "freeglut" is to create the window with the
631 * outside of its border at (x,y) and with dimensions (w,h).
633 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
634 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
635 GetSystemMetrics( SM_CYCAPTION );
637 #endif /* defined(_WIN32_WCE) */
639 if( ! fgState.Position.Use )
644 if( ! fgState.Size.Use )
651 * There's a small difference between creating the top, child and
654 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
656 if ( window->IsMenu )
659 exFlags |= WS_EX_TOOLWINDOW;
661 #if !defined(_WIN32_WCE)
662 else if( window->Parent == NULL )
663 flags |= WS_OVERLAPPEDWINDOW;
669 #if defined(_WIN32_WCE)
671 wchar_t* wstr = fghWstrFromStr(title);
673 window->Window.Handle = CreateWindow(
676 WS_VISIBLE | WS_POPUP,
686 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
687 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
688 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
689 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
690 ShowWindow(window->Window.Handle, SW_SHOW);
691 UpdateWindow(window->Window.Handle);
694 window->Window.Handle = CreateWindowEx(
700 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
705 #endif /* defined(_WIN32_WCE) */
707 if( !( window->Window.Handle ) )
708 fgError( "Failed to create a window (%s)!", title );
710 #if defined(_WIN32_WCE)
711 ShowWindow( window->Window.Handle, SW_SHOW );
713 ShowWindow( window->Window.Handle,
714 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
715 #endif /* defined(_WIN32_WCE) */
717 UpdateWindow( window->Window.Handle );
718 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
722 fgSetWindow( window );
724 window->Window.DoubleBuffered =
725 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
727 if ( ! window->Window.DoubleBuffered )
729 glDrawBuffer ( GL_FRONT );
730 glReadBuffer ( GL_FRONT );
735 * Closes a window, destroying the frame and OpenGL context
737 void fgCloseWindow( SFG_Window* window )
739 #if TARGET_HOST_POSIX_X11
741 glXDestroyContext( fgDisplay.Display, window->Window.Context );
742 XFree( window->Window.VisualInfo );
743 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
744 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
746 #elif TARGET_HOST_MS_WINDOWS
748 /* Make sure we don't close a window with current context active */
749 if( fgStructure.CurrentWindow == window )
750 wglMakeCurrent( NULL, NULL );
753 * Step through the list of windows. If the rendering context
754 * is not being used by another window, then we delete it.
760 for( iter = (SFG_Window *)fgStructure.Windows.First;
762 iter = (SFG_Window *)iter->Node.Next )
764 if( ( iter->Window.Context == window->Window.Context ) &&
770 wglDeleteContext( window->Window.Context );
773 DestroyWindow( window->Window.Handle );
778 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
781 * Creates a new top-level freeglut window
783 int FGAPIENTRY glutCreateWindow( const char* title )
785 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
786 * XXX application has not already done so. The "freeglut" community
787 * XXX decided not to go this route (freeglut-developer e-mail from
788 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
789 * XXX Desired 'freeglut' behaviour when there is no current window"
791 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
793 return fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
794 fgState.Size.X, fgState.Size.Y, GL_FALSE,
799 * This function creates a sub window.
801 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
804 SFG_Window* window = NULL;
805 SFG_Window* parent = NULL;
807 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
808 parent = fgWindowByID( parentID );
809 freeglut_return_val_if_fail( parent != NULL, 0 );
812 x = parent->State.Width + x ;
813 if ( w >= 0 ) x -= w ;
816 if ( w < 0 ) w = parent->State.Width - x + w ;
825 y = parent->State.Height + y ;
826 if ( h >= 0 ) y -= h ;
829 if ( h < 0 ) h = parent->State.Height - y + h ;
836 window = fgCreateWindow( parent, "", x, y, w, h, GL_FALSE, GL_FALSE );
843 * Destroys a window and all of its subwindows
845 void FGAPIENTRY glutDestroyWindow( int windowID )
848 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
849 window = fgWindowByID( windowID );
850 freeglut_return_if_fail( window != NULL );
852 fgExecutionState ExecState = fgState.ExecState;
853 fgAddToWindowDestroyList( window );
854 fgState.ExecState = ExecState;
859 * This function selects the current window
861 void FGAPIENTRY glutSetWindow( int ID )
863 SFG_Window* window = NULL;
865 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
866 if( fgStructure.CurrentWindow != NULL )
867 if( fgStructure.CurrentWindow->ID == ID )
870 window = fgWindowByID( ID );
873 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
877 fgSetWindow( window );
881 * This function returns the ID number of the current window, 0 if none exists
883 int FGAPIENTRY glutGetWindow( void )
885 SFG_Window *win = fgStructure.CurrentWindow;
886 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindow" );
887 while ( win && win->IsMenu )
889 return win ? win->ID : 0;
893 * This function makes the current window visible
895 void FGAPIENTRY glutShowWindow( void )
897 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
898 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
900 #if TARGET_HOST_POSIX_X11
902 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
903 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
905 #elif TARGET_HOST_MS_WINDOWS
907 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
911 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
915 * This function hides the current window
917 void FGAPIENTRY glutHideWindow( void )
919 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
920 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
922 #if TARGET_HOST_POSIX_X11
924 if( fgStructure.CurrentWindow->Parent == NULL )
925 XWithdrawWindow( fgDisplay.Display,
926 fgStructure.CurrentWindow->Window.Handle,
929 XUnmapWindow( fgDisplay.Display,
930 fgStructure.CurrentWindow->Window.Handle );
931 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
933 #elif TARGET_HOST_MS_WINDOWS
935 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
939 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
943 * Iconify the current window (top-level windows only)
945 void FGAPIENTRY glutIconifyWindow( void )
947 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
948 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
950 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
951 #if TARGET_HOST_POSIX_X11
953 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
955 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
957 #elif TARGET_HOST_MS_WINDOWS
959 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
963 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
967 * Set the current window's title
969 void FGAPIENTRY glutSetWindowTitle( const char* title )
971 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
972 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
973 if( ! fgStructure.CurrentWindow->Parent )
975 #if TARGET_HOST_POSIX_X11
979 text.value = (unsigned char *) title;
980 text.encoding = XA_STRING;
982 text.nitems = strlen( title );
986 fgStructure.CurrentWindow->Window.Handle,
990 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
992 #elif TARGET_HOST_MS_WINDOWS
995 wchar_t* wstr = fghWstrFromStr(title);
996 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1000 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1008 * Set the current window's iconified title
1010 void FGAPIENTRY glutSetIconTitle( const char* title )
1012 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1013 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1015 if( ! fgStructure.CurrentWindow->Parent )
1017 #if TARGET_HOST_POSIX_X11
1021 text.value = (unsigned char *) title;
1022 text.encoding = XA_STRING;
1024 text.nitems = strlen( title );
1028 fgStructure.CurrentWindow->Window.Handle,
1032 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1034 #elif TARGET_HOST_MS_WINDOWS
1037 wchar_t* wstr = fghWstrFromStr(title);
1038 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1042 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1050 * Change the current window's size
1052 void FGAPIENTRY glutReshapeWindow( int width, int height )
1054 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1055 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1057 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1058 fgStructure.CurrentWindow->State.Width = width ;
1059 fgStructure.CurrentWindow->State.Height = height;
1063 * Change the current window's position
1065 void FGAPIENTRY glutPositionWindow( int x, int y )
1067 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1068 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1070 #if TARGET_HOST_POSIX_X11
1072 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1074 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1076 #elif TARGET_HOST_MS_WINDOWS
1081 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1082 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1084 fgStructure.CurrentWindow->Window.Handle,
1087 winRect.right - winRect.left,
1088 winRect.bottom - winRect.top,
1097 * Lowers the current window (by Z order change)
1099 void FGAPIENTRY glutPushWindow( void )
1101 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1102 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1104 #if TARGET_HOST_POSIX_X11
1106 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1108 #elif TARGET_HOST_MS_WINDOWS
1111 fgStructure.CurrentWindow->Window.Handle,
1114 SWP_NOSIZE | SWP_NOMOVE
1121 * Raises the current window (by Z order change)
1123 void FGAPIENTRY glutPopWindow( void )
1125 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1126 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1128 #if TARGET_HOST_POSIX_X11
1130 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1132 #elif TARGET_HOST_MS_WINDOWS
1135 fgStructure.CurrentWindow->Window.Handle,
1138 SWP_NOSIZE | SWP_NOMOVE
1145 * Resize the current window so that it fits the whole screen
1147 void FGAPIENTRY glutFullScreen( void )
1149 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1150 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1153 #if TARGET_HOST_POSIX_X11
1159 fgStructure.CurrentWindow->Window.Handle,
1161 fgDisplay.ScreenWidth,
1162 fgDisplay.ScreenHeight
1165 XFlush( fgDisplay.Display ); /* This is needed */
1167 XTranslateCoordinates(
1169 fgStructure.CurrentWindow->Window.Handle,
1170 fgDisplay.RootWindow,
1178 fgStructure.CurrentWindow->Window.Handle,
1181 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1183 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1186 /* For fullscreen mode, force the top-left corner to 0,0
1187 * and adjust the window rectangle so that the client area
1188 * covers the whole screen.
1193 rect.right = fgDisplay.ScreenWidth;
1194 rect.bottom = fgDisplay.ScreenHeight;
1196 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1197 WS_CLIPCHILDREN, FALSE );
1200 * SWP_NOACTIVATE Do not activate the window
1201 * SWP_NOOWNERZORDER Do not change position in z-order
1202 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1203 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1206 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1210 rect.right - rect.left,
1211 rect.bottom - rect.top,
1212 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1220 * A.Donev: Set and retrieve the window's user data
1222 void* FGAPIENTRY glutGetWindowData( void )
1224 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1225 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1226 return fgStructure.CurrentWindow->UserData;
1229 void FGAPIENTRY glutSetWindowData(void* data)
1231 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1232 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1233 fgStructure.CurrentWindow->UserData = data;
1236 /*** END OF FILE ***/