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;
437 * GLUT also checks for multi-sampling, but I don't see that
438 * anywhere else in FREEGLUT so I won't bother with it for the moment.
442 FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.VisualInfo != NULL,
443 "Visual with necessary capabilities not found", "fgOpenWindow" );
446 * XXX HINT: the masks should be updated when adding/removing callbacks.
447 * XXX This might speed up message processing. Is that true?
449 * XXX A: Not appreciably, but it WILL make it easier to debug.
450 * XXX Try tracing old GLUT and try tracing freeglut. Old GLUT
451 * XXX turns off events that it doesn't need and is a whole lot
452 * XXX more pleasant to trace. (Think mouse-motion! Tons of
453 * XXX ``bonus'' GUI events stream in.)
456 StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
457 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
458 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
459 PointerMotionMask | ButtonMotionMask;
460 winAttr.background_pixmap = None;
461 winAttr.background_pixel = 0;
462 winAttr.border_pixel = 0;
464 winAttr.colormap = XCreateColormap(
465 fgDisplay.Display, fgDisplay.RootWindow,
466 window->Window.VisualInfo->visual, AllocNone
469 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
471 if( window->IsMenu || ( gameMode == GL_TRUE ) )
473 winAttr.override_redirect = True;
474 mask |= CWOverrideRedirect;
477 window->Window.Handle = XCreateWindow(
479 window->Parent == NULL ? fgDisplay.RootWindow :
480 window->Parent->Window.Handle,
482 window->Window.VisualInfo->depth, InputOutput,
483 window->Window.VisualInfo->visual, mask,
488 * The GLX context creation, possibly trying the direct context rendering
489 * or else use the current context if the user has so specified
494 * If there isn't already an OpenGL rendering context for menu
497 if( !fgStructure.MenuContext )
499 fgStructure.MenuContext =
500 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
501 fgStructure.MenuContext->MVisualInfo = window->Window.VisualInfo;
502 fgStructure.MenuContext->MContext = glXCreateContext(
503 fgDisplay.Display, fgStructure.MenuContext->MVisualInfo,
504 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
508 /* window->Window.Context = fgStructure.MenuContext->MContext; */
509 window->Window.Context = glXCreateContext(
510 fgDisplay.Display, window->Window.VisualInfo,
511 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
514 else if( fgState.UseCurrentContext )
516 window->Window.Context = glXGetCurrentContext( );
518 if( ! window->Window.Context )
519 window->Window.Context = glXCreateContext(
520 fgDisplay.Display, window->Window.VisualInfo,
521 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
525 window->Window.Context = glXCreateContext(
526 fgDisplay.Display, window->Window.VisualInfo,
527 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
530 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
531 if( !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
533 if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
534 fgError( "Unable to force direct context rendering for window '%s'",
536 else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
537 fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
543 * XXX Assume the new window is visible by default
544 * XXX Is this a safe assumption?
546 window->State.Visible = GL_TRUE;
549 if ( fgState.Position.Use )
550 sizeHints.flags |= USPosition;
551 if ( fgState.Size.Use )
552 sizeHints.flags |= USSize;
555 * Fill in the size hints values now (the x, y, width and height
556 * settings are obsolete, are there any more WMs that support them?)
557 * Unless the X servers actually stop supporting these, we should
558 * continue to fill them in. It is *not* our place to tell the user
559 * that they should replace a window manager that they like, and which
560 * works, just because *we* think that it's not "modern" enough.
565 sizeHints.height = h;
567 wmHints.flags = StateHint;
568 wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
569 /* Prepare the window and iconified window names... */
570 XStringListToTextProperty( (char **) &title, 1, &textProperty );
574 window->Window.Handle,
583 XFree( textProperty.value );
585 XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
586 &fgDisplay.DeleteWindow, 1 );
590 window->Window.Handle,
591 window->Window.Context
594 XMapWindow( fgDisplay.Display, window->Window.Handle );
596 #elif TARGET_HOST_MS_WINDOWS
603 /* Grab the window class we have registered on glutInit(): */
604 atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
605 FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
610 FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
611 "Game mode being invoked on a subwindow",
615 * Set the window creation flags appropriately to make the window
618 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
622 #if !defined(_WIN32_WCE)
623 if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
626 * Update the window dimensions, taking account of window
627 * decorations. "freeglut" is to create the window with the
628 * outside of its border at (x,y) and with dimensions (w,h).
630 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
631 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
632 GetSystemMetrics( SM_CYCAPTION );
634 #endif /* defined(_WIN32_WCE) */
636 if( ! fgState.Position.Use )
641 if( ! fgState.Size.Use )
648 * There's a small difference between creating the top, child and
651 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
653 if ( window->IsMenu )
656 exFlags |= WS_EX_TOOLWINDOW;
658 #if !defined(_WIN32_WCE)
659 else if( window->Parent == NULL )
660 flags |= WS_OVERLAPPEDWINDOW;
666 #if defined(_WIN32_WCE)
668 wchar_t* wstr = fghWstrFromStr(title);
670 window->Window.Handle = CreateWindow(
673 WS_VISIBLE | WS_POPUP,
683 SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
684 SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
685 SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
686 MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
687 ShowWindow(window->Window.Handle, SW_SHOW);
688 UpdateWindow(window->Window.Handle);
691 window->Window.Handle = CreateWindowEx(
697 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
702 #endif /* defined(_WIN32_WCE) */
704 if( !( window->Window.Handle ) )
705 fgError( "Failed to create a window (%s)!", title );
707 #if defined(_WIN32_WCE)
708 ShowWindow( window->Window.Handle, SW_SHOW );
710 ShowWindow( window->Window.Handle,
711 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
712 #endif /* defined(_WIN32_WCE) */
714 UpdateWindow( window->Window.Handle );
715 ShowCursor( TRUE ); /* XXX Old comments say "hide cursor"! */
719 fgSetWindow( window );
721 window->Window.DoubleBuffered =
722 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
724 if ( ! window->Window.DoubleBuffered )
726 glDrawBuffer ( GL_FRONT );
727 glReadBuffer ( GL_FRONT );
732 * Closes a window, destroying the frame and OpenGL context
734 void fgCloseWindow( SFG_Window* window )
736 #if TARGET_HOST_POSIX_X11
738 glXDestroyContext( fgDisplay.Display, window->Window.Context );
739 XFree( window->Window.VisualInfo );
740 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
741 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
743 #elif TARGET_HOST_MS_WINDOWS
745 /* Make sure we don't close a window with current context active */
746 if( fgStructure.CurrentWindow == window )
747 wglMakeCurrent( NULL, NULL );
750 * Step through the list of windows. If the rendering context
751 * is not being used by another window, then we delete it.
757 for( iter = (SFG_Window *)fgStructure.Windows.First;
759 iter = (SFG_Window *)iter->Node.Next )
761 if( ( iter->Window.Context == window->Window.Context ) &&
767 wglDeleteContext( window->Window.Context );
770 DestroyWindow( window->Window.Handle );
775 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
778 * Creates a new top-level freeglut window
780 int FGAPIENTRY glutCreateWindow( const char* title )
782 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
783 * XXX application has not already done so. The "freeglut" community
784 * XXX decided not to go this route (freeglut-developer e-mail from
785 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
786 * XXX Desired 'freeglut' behaviour when there is no current window"
788 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
790 return fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
791 fgState.Size.X, fgState.Size.Y, GL_FALSE,
796 * This function creates a sub window.
798 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
801 SFG_Window* window = NULL;
802 SFG_Window* parent = NULL;
804 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
805 parent = fgWindowByID( parentID );
806 freeglut_return_val_if_fail( parent != NULL, 0 );
809 x = parent->State.Width + x ;
810 if ( w >= 0 ) x -= w ;
813 if ( w < 0 ) w = parent->State.Width - x + w ;
822 y = parent->State.Height + y ;
823 if ( h >= 0 ) y -= h ;
826 if ( h < 0 ) h = parent->State.Height - y + h ;
833 window = fgCreateWindow( parent, "", x, y, w, h, GL_FALSE, GL_FALSE );
840 * Destroys a window and all of its subwindows
842 void FGAPIENTRY glutDestroyWindow( int windowID )
845 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
846 window = fgWindowByID( windowID );
847 freeglut_return_if_fail( window != NULL );
849 fgExecutionState ExecState = fgState.ExecState;
850 fgAddToWindowDestroyList( window );
851 fgState.ExecState = ExecState;
856 * This function selects the current window
858 void FGAPIENTRY glutSetWindow( int ID )
860 SFG_Window* window = NULL;
862 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
863 if( fgStructure.CurrentWindow != NULL )
864 if( fgStructure.CurrentWindow->ID == ID )
867 window = fgWindowByID( ID );
870 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
874 fgSetWindow( window );
878 * This function returns the ID number of the current window, 0 if none exists
880 int FGAPIENTRY glutGetWindow( void )
882 SFG_Window *win = fgStructure.CurrentWindow;
883 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindow" );
884 while ( win && win->IsMenu )
886 return win ? win->ID : 0;
890 * This function makes the current window visible
892 void FGAPIENTRY glutShowWindow( void )
894 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
895 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
897 #if TARGET_HOST_POSIX_X11
899 XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
900 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
902 #elif TARGET_HOST_MS_WINDOWS
904 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
908 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
912 * This function hides the current window
914 void FGAPIENTRY glutHideWindow( void )
916 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
917 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
919 #if TARGET_HOST_POSIX_X11
921 if( fgStructure.CurrentWindow->Parent == NULL )
922 XWithdrawWindow( fgDisplay.Display,
923 fgStructure.CurrentWindow->Window.Handle,
926 XUnmapWindow( fgDisplay.Display,
927 fgStructure.CurrentWindow->Window.Handle );
928 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
930 #elif TARGET_HOST_MS_WINDOWS
932 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
936 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
940 * Iconify the current window (top-level windows only)
942 void FGAPIENTRY glutIconifyWindow( void )
944 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
945 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
947 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
948 #if TARGET_HOST_POSIX_X11
950 XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
952 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
954 #elif TARGET_HOST_MS_WINDOWS
956 ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
960 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
964 * Set the current window's title
966 void FGAPIENTRY glutSetWindowTitle( const char* title )
968 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
969 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
970 if( ! fgStructure.CurrentWindow->Parent )
972 #if TARGET_HOST_POSIX_X11
976 text.value = (unsigned char *) title;
977 text.encoding = XA_STRING;
979 text.nitems = strlen( title );
983 fgStructure.CurrentWindow->Window.Handle,
987 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
989 #elif TARGET_HOST_MS_WINDOWS
992 wchar_t* wstr = fghWstrFromStr(title);
993 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
997 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1005 * Set the current window's iconified title
1007 void FGAPIENTRY glutSetIconTitle( const char* title )
1009 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1010 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1012 if( ! fgStructure.CurrentWindow->Parent )
1014 #if TARGET_HOST_POSIX_X11
1018 text.value = (unsigned char *) title;
1019 text.encoding = XA_STRING;
1021 text.nitems = strlen( title );
1025 fgStructure.CurrentWindow->Window.Handle,
1029 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1031 #elif TARGET_HOST_MS_WINDOWS
1034 wchar_t* wstr = fghWstrFromStr(title);
1035 SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1039 SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1047 * Change the current window's size
1049 void FGAPIENTRY glutReshapeWindow( int width, int height )
1051 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1052 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1054 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1055 fgStructure.CurrentWindow->State.Width = width ;
1056 fgStructure.CurrentWindow->State.Height = height;
1060 * Change the current window's position
1062 void FGAPIENTRY glutPositionWindow( int x, int y )
1064 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1065 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1067 #if TARGET_HOST_POSIX_X11
1069 XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1071 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1073 #elif TARGET_HOST_MS_WINDOWS
1078 /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1079 GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1081 fgStructure.CurrentWindow->Window.Handle,
1084 winRect.right - winRect.left,
1085 winRect.bottom - winRect.top,
1094 * Lowers the current window (by Z order change)
1096 void FGAPIENTRY glutPushWindow( void )
1098 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1099 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1101 #if TARGET_HOST_POSIX_X11
1103 XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1105 #elif TARGET_HOST_MS_WINDOWS
1108 fgStructure.CurrentWindow->Window.Handle,
1111 SWP_NOSIZE | SWP_NOMOVE
1118 * Raises the current window (by Z order change)
1120 void FGAPIENTRY glutPopWindow( void )
1122 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1123 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1125 #if TARGET_HOST_POSIX_X11
1127 XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1129 #elif TARGET_HOST_MS_WINDOWS
1132 fgStructure.CurrentWindow->Window.Handle,
1135 SWP_NOSIZE | SWP_NOMOVE
1142 * Resize the current window so that it fits the whole screen
1144 void FGAPIENTRY glutFullScreen( void )
1146 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1147 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1150 #if TARGET_HOST_POSIX_X11
1156 fgStructure.CurrentWindow->Window.Handle,
1158 fgDisplay.ScreenWidth,
1159 fgDisplay.ScreenHeight
1162 XFlush( fgDisplay.Display ); /* This is needed */
1164 XTranslateCoordinates(
1166 fgStructure.CurrentWindow->Window.Handle,
1167 fgDisplay.RootWindow,
1175 fgStructure.CurrentWindow->Window.Handle,
1178 XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1180 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1183 /* For fullscreen mode, force the top-left corner to 0,0
1184 * and adjust the window rectangle so that the client area
1185 * covers the whole screen.
1190 rect.right = fgDisplay.ScreenWidth;
1191 rect.bottom = fgDisplay.ScreenHeight;
1193 AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1194 WS_CLIPCHILDREN, FALSE );
1197 * SWP_NOACTIVATE Do not activate the window
1198 * SWP_NOOWNERZORDER Do not change position in z-order
1199 * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1200 * SWP_NOZORDER Retains the current Z order (ignore 2nd param)
1203 SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1207 rect.right - rect.left,
1208 rect.bottom - rect.top,
1209 SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1217 * A.Donev: Set and retrieve the window's user data
1219 void* FGAPIENTRY glutGetWindowData( void )
1221 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1222 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1223 return fgStructure.CurrentWindow->UserData;
1226 void FGAPIENTRY glutSetWindowData(void* data)
1228 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1229 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1230 fgStructure.CurrentWindow->UserData = data;
1233 /*** END OF FILE ***/