06efa035db28a2fa82ac83aa1b90d4408bb51bee
[freeglut] / src / fg_window.c
1 /*
2  * freeglut_window.c
3  *
4  * Window management methods.
5  *
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
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
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.
26  */
27
28 #define FREEGLUT_BUILDING_LIB
29 #include <GL/freeglut.h>
30 #include "fg_internal.h"
31
32 /*
33  * TODO BEFORE THE STABLE RELEASE:
34  *
35  *  fgSetupPixelFormat      -- ignores the display mode settings
36  *  fgOpenWindow()          -- check the Win32 version, -iconic handling!
37  *  fgCloseWindow()         -- check the Win32 version
38  *  glutCreateWindow()      -- Check when default position and size is {-1,-1}
39  *  glutCreateSubWindow()   -- Check when default position and size is {-1,-1}
40  *  glutDestroyWindow()     -- check the Win32 version
41  *  glutSetWindow()         -- check the Win32 version
42  *  glutSetWindowTitle()    -- check the Win32 version
43  *  glutSetIconTitle()      -- check the Win32 version
44  *  glutShowWindow()        -- check the Win32 version
45  *  glutHideWindow()        -- check the Win32 version
46  *  glutIconifyWindow()     -- check the Win32 version
47  *  glutPushWindow()        -- check the Win32 version
48  *  glutPopWindow()         -- check the Win32 version
49  */
50
51
52 extern void fgPlatformSetWindow ( SFG_Window *window );
53 extern void fgPlatformOpenWindow( SFG_Window* window, const char* title,
54                                   GLboolean positionUse, int x, int y,
55                                   GLboolean sizeUse, int w, int h,
56                                   GLboolean gameMode, GLboolean isSubWindow );
57 extern void fgPlatformCloseWindow( SFG_Window* window );
58 extern void fgPlatformGlutShowWindow( void );
59 extern void fgPlatformGlutHideWindow( void );
60 extern void fgPlatformGlutIconifyWindow( void );
61 extern void fgPlatformGlutSetWindowTitle( const char* title );
62 extern void fgPlatformGlutSetIconTitle( const char* title );
63 extern void fgPlatformGlutPositionWindow( int x, int y );
64 extern void fgPlatformGlutPushWindow( void );
65 extern void fgPlatformGlutPopWindow( void );
66 extern void fgPlatformGlutFullScreen( SFG_Window *win );
67 extern void fgPlatformGlutLeaveFullScreen( SFG_Window *win );
68 extern void fgPlatformGlutFullScreenToggle( SFG_Window *win );
69
70
71 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
72
73 int fghIsLegacyContextRequested( void )
74 {
75     return fgState.MajorVersion < 2 || (fgState.MajorVersion == 2 && fgState.MinorVersion <= 1);
76 }
77
78 int fghNumberOfAuxBuffersRequested( void )
79 {
80   if ( fgState.DisplayMode & GLUT_AUX4 ) {
81     return 4;
82   }
83   if ( fgState.DisplayMode & GLUT_AUX3 ) {
84     return 3;
85   }
86   if ( fgState.DisplayMode & GLUT_AUX2 ) {
87     return 2;
88   }
89   if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
90     return fgState.AuxiliaryBufferNumber;
91   }
92   return 0;
93 }
94
95 int fghMapBit( int mask, int from, int to )
96 {
97   return ( mask & from ) ? to : 0;
98
99 }
100
101 void fghContextCreationError( void )
102 {
103     fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
104              fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
105              fgState.ContextProfile );
106 }
107
108
109 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
110
111 /*
112  * Sets the OpenGL context and the fgStructure "Current Window" pointer to
113  * the window structure passed in.
114  */
115 void fgSetWindow ( SFG_Window *window )
116 {
117         fgPlatformSetWindow ( window );
118
119     fgStructure.CurrentWindow = window;
120 }
121
122 /*
123  * Opens a window. Requires a SFG_Window object created and attached
124  * to the freeglut structure. OpenGL context is created here.
125  */
126 void fgOpenWindow( SFG_Window* window, const char* title,
127                    GLboolean positionUse, int x, int y,
128                    GLboolean sizeUse, int w, int h,
129                    GLboolean gameMode, GLboolean isSubWindow )
130 {
131         fgPlatformOpenWindow( window, title,
132                    positionUse, x, y,
133                    sizeUse, w, h,
134                    gameMode, isSubWindow );
135
136     fgSetWindow( window );
137
138     window->Window.DoubleBuffered =
139         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
140
141 #ifndef EGL_VERSION_1_0  /* No glDrawBuffer/glReadBuffer in GLES */
142     if ( ! window->Window.DoubleBuffered )
143     {
144         glDrawBuffer ( GL_FRONT );
145         glReadBuffer ( GL_FRONT );
146     }
147 #endif
148 }
149
150 /*
151  * Closes a window, destroying the frame and OpenGL context
152  */
153 void fgCloseWindow( SFG_Window* window )
154 {
155     /* if we're in gamemode and we're closing the gamemode window,
156      * call glutLeaveGameMode first to make sure the gamemode is
157      * properly closed before closing the window
158      */
159     if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
160         glutLeaveGameMode();
161
162         fgPlatformCloseWindow ( window );
163 }
164
165
166 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
167
168 /*
169  * Creates a new top-level freeglut window
170  */
171 int FGAPIENTRY glutCreateWindow( const char* title )
172 {
173     /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
174      * XXX application has not already done so.  The "freeglut" community
175      * XXX decided not to go this route (freeglut-developer e-mail from
176      * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
177      * XXX Desired 'freeglut' behaviour when there is no current window"
178      */
179     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
180
181     return fgCreateWindow( NULL, title, fgState.Position.Use,
182                            fgState.Position.X, fgState.Position.Y,
183                            fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
184                            GL_FALSE, GL_FALSE )->ID;
185 }
186
187 /*
188  * This function creates a sub window.
189  */
190 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
191 {
192     int ret = 0;
193     SFG_Window* window = NULL;
194     SFG_Window* parent = NULL;
195
196     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
197     parent = fgWindowByID( parentID );
198     freeglut_return_val_if_fail( parent != NULL, 0 );
199     if ( x < 0 )
200     {
201         x = parent->State.Width + x ;
202         if ( w >= 0 ) x -= w ;
203     }
204
205     if ( w < 0 ) w = parent->State.Width - x + w ;
206     if ( w < 0 )
207     {
208         x += w ;
209         w = -w ;
210     }
211
212     if ( y < 0 )
213     {
214         y = parent->State.Height + y ;
215         if ( h >= 0 ) y -= h ;
216     }
217
218     if ( h < 0 ) h = parent->State.Height - y + h ;
219     if ( h < 0 )
220     {
221         y += h ;
222         h = -h ;
223     }
224
225     window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
226     ret = window->ID;
227
228     return ret;
229 }
230
231 /*
232  * Destroys a window and all of its subwindows
233  */
234 void FGAPIENTRY glutDestroyWindow( int windowID )
235 {
236     SFG_Window* window;
237     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
238     window = fgWindowByID( windowID );
239     freeglut_return_if_fail( window != NULL );
240     {
241         fgExecutionState ExecState = fgState.ExecState;
242         fgAddToWindowDestroyList( window );
243         fgState.ExecState = ExecState;
244     }
245 }
246
247 /*
248  * This function selects the specified window as the current window
249  */
250 void FGAPIENTRY glutSetWindow( int ID )
251 {
252     SFG_Window* window = NULL;
253
254     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
255     if( fgStructure.CurrentWindow != NULL )
256         if( fgStructure.CurrentWindow->ID == ID )
257             return;
258
259     window = fgWindowByID( ID );
260     if( window == NULL )
261     {
262         fgWarning( "glutSetWindow(): window ID %d not found!", ID );
263         return;
264     }
265
266     fgSetWindow( window );
267 }
268
269 /*
270  * This function returns the ID number of the current window, 0 if none exists
271  */
272 int FGAPIENTRY glutGetWindow( void )
273 {
274     SFG_Window *win = fgStructure.CurrentWindow;
275     /*
276      * Since GLUT did not throw an error if this function was called without a prior call to
277      * "glutInit", this function shouldn't do so here.  Instead let us return a zero.
278      * See Feature Request "[ 1307049 ] glutInit check".
279      */
280     if ( ! fgState.Initialised )
281         return 0;
282
283     while ( win && win->IsMenu )
284         win = win->Parent;
285     return win ? win->ID : 0;
286 }
287
288 /*
289  * This function makes the current window visible
290  */
291 void FGAPIENTRY glutShowWindow( void )
292 {
293     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
294     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
295
296         fgPlatformGlutShowWindow ();
297
298     fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
299 }
300
301 /*
302  * This function hides the current window
303  */
304 void FGAPIENTRY glutHideWindow( void )
305 {
306     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
307     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
308
309         fgPlatformGlutHideWindow ();
310
311     fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
312 }
313
314 /*
315  * Iconify the current window (top-level windows only)
316  */
317 void FGAPIENTRY glutIconifyWindow( void )
318 {
319     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
320     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
321
322     fgStructure.CurrentWindow->State.Visible   = GL_FALSE;
323
324         fgPlatformGlutIconifyWindow ();
325
326     fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
327 }
328
329 /*
330  * Set the current window's title
331  */
332 void FGAPIENTRY glutSetWindowTitle( const char* title )
333 {
334     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
335     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
336     if( ! fgStructure.CurrentWindow->Parent )
337     {
338                 fgPlatformGlutSetWindowTitle ( title );
339     }
340 }
341
342 /*
343  * Set the current window's iconified title
344  */
345 void FGAPIENTRY glutSetIconTitle( const char* title )
346 {
347     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
348     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
349
350     if( ! fgStructure.CurrentWindow->Parent )
351     {
352                 fgPlatformGlutSetIconTitle ( title );
353     }
354 }
355
356 /*
357  * Change the current window's size
358  */
359 void FGAPIENTRY glutReshapeWindow( int width, int height )
360 {
361     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
362     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
363
364     if (glutGet(GLUT_FULL_SCREEN))
365     {
366       /*  Leave full screen state before resizing. */
367       glutLeaveFullScreen();
368     }
369
370     fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
371     fgStructure.CurrentWindow->State.Width  = width ;
372     fgStructure.CurrentWindow->State.Height = height;
373 }
374
375 /*
376  * Change the current window's position
377  */
378 void FGAPIENTRY glutPositionWindow( int x, int y )
379 {
380     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
381     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
382
383     if (glutGet(GLUT_FULL_SCREEN))
384     {
385       /*  Leave full screen state before moving. */
386       glutLeaveFullScreen();
387     }
388
389         fgPlatformGlutPositionWindow ( x, y );
390 }
391
392 /*
393  * Lowers the current window (by Z order change)
394  */
395 void FGAPIENTRY glutPushWindow( void )
396 {
397     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
398     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
399
400         fgPlatformGlutPushWindow ();
401 }
402
403 /*
404  * Raises the current window (by Z order change)
405  */
406 void FGAPIENTRY glutPopWindow( void )
407 {
408     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
409     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
410
411         fgPlatformGlutPopWindow ();
412 }
413
414 /*
415  * Resize the current window so that it fits the whole screen
416  */
417 void FGAPIENTRY glutFullScreen( void )
418 {
419     SFG_Window *win;
420
421     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
422     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
423
424     win = fgStructure.CurrentWindow;
425
426     if (win->Parent)
427     {
428         /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
429          * Also, what would it mean for a child window to be fullscreen, given that it
430          * is confined to its parent?
431          */
432         fgWarning("glutFullScreen called on a child window, ignoring...");
433         return;
434     }
435     else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID)
436     {
437         /* Ignore fullscreen call on GameMode window, those are always fullscreen already */
438         return;
439     }
440
441         fgPlatformGlutFullScreen ( win );
442 }
443
444 /*
445  * If we are fullscreen, resize the current window back to its original size
446  */
447 void FGAPIENTRY glutLeaveFullScreen( void )
448 {
449     SFG_Window *win;
450
451     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
452     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
453
454     win = fgStructure.CurrentWindow;
455
456         fgPlatformGlutLeaveFullScreen ( win );
457 }
458
459 /*
460  * Toggle the window's full screen state.
461  */
462 void FGAPIENTRY glutFullScreenToggle( void )
463 {
464     SFG_Window *win;
465
466     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
467     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
468
469     win = fgStructure.CurrentWindow;
470
471         fgPlatformGlutFullScreenToggle ( win );
472 }
473
474 /*
475  * A.Donev: Set and retrieve the window's user data
476  */
477 void* FGAPIENTRY glutGetWindowData( void )
478 {
479     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
480     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
481     return fgStructure.CurrentWindow->UserData;
482 }
483
484 void FGAPIENTRY glutSetWindowData(void* data)
485 {
486     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
487     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
488     fgStructure.CurrentWindow->UserData = data;
489 }
490
491 /*** END OF FILE ***/