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 #define FREEGLUT_BUILDING_LIB
29 #include <GL/freeglut.h>
30 #include "freeglut_internal.h"
32 #if defined(_WIN32_WCE)
33 # include <Aygshell.h>
34 # ifdef FREEGLUT_LIB_PRAGMAS
35 # pragma comment( lib, "Aygshell.lib" )
37 #endif /* defined(_WIN32_WCE) */
41 * TODO BEFORE THE STABLE RELEASE:
43 * fgSetupPixelFormat -- ignores the display mode settings
44 * fgOpenWindow() -- check the Win32 version, -iconic handling!
45 * fgCloseWindow() -- check the Win32 version
46 * glutCreateWindow() -- Check when default position and size is {-1,-1}
47 * glutCreateSubWindow() -- Check when default position and size is {-1,-1}
48 * glutDestroyWindow() -- check the Win32 version
49 * glutSetWindow() -- check the Win32 version
50 * glutGetWindow() -- OK
51 * glutSetWindowTitle() -- check the Win32 version
52 * glutSetIconTitle() -- check the Win32 version
53 * glutShowWindow() -- check the Win32 version
54 * glutHideWindow() -- check the Win32 version
55 * glutIconifyWindow() -- check the Win32 version
56 * glutReshapeWindow() -- check the Win32 version
57 * glutPositionWindow() -- check the Win32 version
58 * glutPushWindow() -- check the Win32 version
59 * glutPopWindow() -- check the Win32 version
63 extern void fgPlatformSetWindow ( SFG_Window *window );
64 extern void fgPlatformOpenWindow( SFG_Window* window, const char* title,
65 GLboolean positionUse, int x, int y,
66 GLboolean sizeUse, int w, int h,
67 GLboolean gameMode, GLboolean isSubWindow );
68 extern void fgPlatformCloseWindow( SFG_Window* window );
69 extern void fgPlatformGlutShowWindow( void );
70 extern void fgPlatformGlutHideWindow( void );
71 extern void fgPlatformGlutIconifyWindow( void );
72 extern void fgPlatformGlutSetWindowTitle( const char* title );
73 extern void fgPlatformGlutSetIconTitle( const char* title );
74 extern void fgPlatformGlutPositionWindow( int x, int y );
75 extern void fgPlatformGlutPushWindow( void );
76 extern void fgPlatformGlutPopWindow( void );
77 extern void fgPlatformGlutFullScreen( SFG_Window *win );
78 extern void fgPlatformGlutLeaveFullScreen( SFG_Window *win );
79 extern void fgPlatformGlutFullScreenToggle( SFG_Window *win );
82 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
84 int fghIsLegacyContextVersionRequested( void )
86 return fgState.MajorVersion < 2 || (fgState.MajorVersion == 2 && fgState.MinorVersion <= 1);
89 int fghIsLegacyContextRequested( void )
91 return fghIsLegacyContextVersionRequested() &&
92 fgState.ContextFlags == 0 &&
93 fgState.ContextProfile == 0;
96 int fghNumberOfAuxBuffersRequested( void )
98 if ( fgState.DisplayMode & GLUT_AUX4 ) {
101 if ( fgState.DisplayMode & GLUT_AUX3 ) {
104 if ( fgState.DisplayMode & GLUT_AUX2 ) {
107 if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
108 return fgState.AuxiliaryBufferNumber;
113 int fghMapBit( int mask, int from, int to )
115 return ( mask & from ) ? to : 0;
119 void fghContextCreationError( void )
121 fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
122 fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
123 fgState.ContextProfile );
127 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
130 * Sets the OpenGL context and the fgStructure "Current Window" pointer to
131 * the window structure passed in.
133 void fgSetWindow ( SFG_Window *window )
135 fgPlatformSetWindow ( window );
137 fgStructure.CurrentWindow = window;
141 * Opens a window. Requires a SFG_Window object created and attached
142 * to the freeglut structure. OpenGL context is created here.
144 void fgOpenWindow( SFG_Window* window, const char* title,
145 GLboolean positionUse, int x, int y,
146 GLboolean sizeUse, int w, int h,
147 GLboolean gameMode, GLboolean isSubWindow )
149 fgPlatformOpenWindow( window, title,
152 gameMode, isSubWindow );
154 fgSetWindow( window );
156 window->Window.DoubleBuffered =
157 ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
159 if ( ! window->Window.DoubleBuffered )
161 glDrawBuffer ( GL_FRONT );
162 glReadBuffer ( GL_FRONT );
167 * Closes a window, destroying the frame and OpenGL context
169 void fgCloseWindow( SFG_Window* window )
171 /* if we're in gamemode and we're closing the gamemode window,
172 * call glutLeaveGameMode first to make sure the gamemode is
173 * properly closed before closing the window
175 if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
178 fgPlatformCloseWindow ( window );
182 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
185 * Creates a new top-level freeglut window
187 int FGAPIENTRY glutCreateWindow( const char* title )
189 /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
190 * XXX application has not already done so. The "freeglut" community
191 * XXX decided not to go this route (freeglut-developer e-mail from
192 * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
193 * XXX Desired 'freeglut' behaviour when there is no current window"
195 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
197 return fgCreateWindow( NULL, title, fgState.Position.Use,
198 fgState.Position.X, fgState.Position.Y,
199 fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
200 GL_FALSE, GL_FALSE )->ID;
204 * This function creates a sub window.
206 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
209 SFG_Window* window = NULL;
210 SFG_Window* parent = NULL;
212 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
213 parent = fgWindowByID( parentID );
214 freeglut_return_val_if_fail( parent != NULL, 0 );
217 x = parent->State.Width + x ;
218 if ( w >= 0 ) x -= w ;
221 if ( w < 0 ) w = parent->State.Width - x + w ;
230 y = parent->State.Height + y ;
231 if ( h >= 0 ) y -= h ;
234 if ( h < 0 ) h = parent->State.Height - y + h ;
241 window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
248 * Destroys a window and all of its subwindows
250 void FGAPIENTRY glutDestroyWindow( int windowID )
253 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
254 window = fgWindowByID( windowID );
255 freeglut_return_if_fail( window != NULL );
257 fgExecutionState ExecState = fgState.ExecState;
258 fgAddToWindowDestroyList( window );
259 fgState.ExecState = ExecState;
264 * This function selects the current window
266 void FGAPIENTRY glutSetWindow( int ID )
268 SFG_Window* window = NULL;
270 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
271 if( fgStructure.CurrentWindow != NULL )
272 if( fgStructure.CurrentWindow->ID == ID )
275 window = fgWindowByID( ID );
278 fgWarning( "glutSetWindow(): window ID %d not found!", ID );
282 fgSetWindow( window );
286 * This function returns the ID number of the current window, 0 if none exists
288 int FGAPIENTRY glutGetWindow( void )
290 SFG_Window *win = fgStructure.CurrentWindow;
292 * Since GLUT did not throw an error if this function was called without a prior call to
293 * "glutInit", this function shouldn't do so here. Instead let us return a zero.
294 * See Feature Request "[ 1307049 ] glutInit check".
296 if ( ! fgState.Initialised )
299 while ( win && win->IsMenu )
301 return win ? win->ID : 0;
305 * This function makes the current window visible
307 void FGAPIENTRY glutShowWindow( void )
309 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
310 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
312 fgPlatformGlutShowWindow ();
314 fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
318 * This function hides the current window
320 void FGAPIENTRY glutHideWindow( void )
322 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
323 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
325 fgPlatformGlutHideWindow ();
327 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
331 * Iconify the current window (top-level windows only)
333 void FGAPIENTRY glutIconifyWindow( void )
335 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
336 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
338 fgStructure.CurrentWindow->State.Visible = GL_FALSE;
340 fgPlatformGlutIconifyWindow ();
342 fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
346 * Set the current window's title
348 void FGAPIENTRY glutSetWindowTitle( const char* title )
350 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
351 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
352 if( ! fgStructure.CurrentWindow->Parent )
354 fgPlatformGlutSetWindowTitle ( title );
359 * Set the current window's iconified title
361 void FGAPIENTRY glutSetIconTitle( const char* title )
363 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
364 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
366 if( ! fgStructure.CurrentWindow->Parent )
368 fgPlatformGlutSetIconTitle ( title );
373 * Change the current window's size
375 void FGAPIENTRY glutReshapeWindow( int width, int height )
377 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
378 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
380 if (glutGet(GLUT_FULL_SCREEN))
382 /* Leave full screen state before resizing. */
383 glutLeaveFullScreen();
386 fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
387 fgStructure.CurrentWindow->State.Width = width ;
388 fgStructure.CurrentWindow->State.Height = height;
392 * Change the current window's position
394 void FGAPIENTRY glutPositionWindow( int x, int y )
396 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
397 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
399 if (glutGet(GLUT_FULL_SCREEN))
401 /* Leave full screen state before moving. */
402 glutLeaveFullScreen();
405 fgPlatformGlutPositionWindow ( x, y );
409 * Lowers the current window (by Z order change)
411 void FGAPIENTRY glutPushWindow( void )
413 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
414 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
416 fgPlatformGlutPushWindow ();
420 * Raises the current window (by Z order change)
422 void FGAPIENTRY glutPopWindow( void )
424 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
425 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
427 fgPlatformGlutPopWindow ();
431 * Resize the current window so that it fits the whole screen
433 void FGAPIENTRY glutFullScreen( void )
437 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
438 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
440 win = fgStructure.CurrentWindow;
444 /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
445 * Also, what would it mean for a child window to be fullscreen, given that it
446 * is confined to its parent?
448 fgWarning("glutFullScreen called on a child window, ignoring...");
451 else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID)
453 /* Ignore fullscreen call on GameMode window, those are always fullscreen already */
457 fgPlatformGlutFullScreen ( win );
461 * If we are fullscreen, resize the current window back to its original size
463 void FGAPIENTRY glutLeaveFullScreen( void )
467 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
468 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
470 win = fgStructure.CurrentWindow;
472 fgPlatformGlutLeaveFullScreen ( win );
476 * Toggle the window's full screen state.
478 void FGAPIENTRY glutFullScreenToggle( void )
482 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
483 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
485 win = fgStructure.CurrentWindow;
487 fgPlatformGlutFullScreenToggle ( win );
491 * A.Donev: Set and retrieve the window's user data
493 void* FGAPIENTRY glutGetWindowData( void )
495 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
496 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
497 return fgStructure.CurrentWindow->UserData;
500 void FGAPIENTRY glutSetWindowData(void* data)
502 FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
503 FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
504 fgStructure.CurrentWindow->UserData = data;
507 /*** END OF FILE ***/