- moving to a new way of handling window changes (position, size, visibility)
[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 #include "fg_gl2.h"
32
33 /*
34  * TODO BEFORE THE STABLE RELEASE:
35  *
36  *  fgSetupPixelFormat      -- ignores the display mode settings
37  *  fgOpenWindow()          -- check the Win32 version, -iconic handling!
38  *  fgCloseWindow()         -- check the Win32 version
39  *  glutCreateWindow()      -- Check when default position and size is {-1,-1}
40  *  glutCreateSubWindow()   -- Check when default position and size is {-1,-1}
41  *  glutDestroyWindow()     -- check the Win32 version
42  *  glutSetWindow()         -- check the Win32 version
43  *  glutSetWindowTitle()    -- check the Win32 version
44  *  glutSetIconTitle()      -- check the Win32 version
45  *  glutShowWindow()        -- check the Win32 version
46  *  glutHideWindow()        -- check the Win32 version
47  *  glutIconifyWindow()     -- check the Win32 version
48  *  glutPushWindow()        -- check the Win32 version
49  *  glutPopWindow()         -- check the Win32 version
50  */
51
52
53 extern void fgPlatformSetWindow ( SFG_Window *window );
54 extern void fgPlatformOpenWindow( SFG_Window* window, const char* title,
55                                   GLboolean positionUse, int x, int y,
56                                   GLboolean sizeUse, int w, int h,
57                                   GLboolean gameMode, GLboolean isSubWindow );
58 extern void fgPlatformCloseWindow( SFG_Window* window );
59 extern void fgPlatformGlutSetWindowTitle( const char* title );
60 extern void fgPlatformGlutSetIconTitle( const char* title );
61
62
63 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
64
65 int fghIsLegacyContextRequested( void )
66 {
67     return fgState.MajorVersion < 2 || (fgState.MajorVersion == 2 && fgState.MinorVersion <= 1);
68 }
69
70 int fghNumberOfAuxBuffersRequested( void )
71 {
72   if ( fgState.DisplayMode & GLUT_AUX4 ) {
73     return 4;
74   }
75   if ( fgState.DisplayMode & GLUT_AUX3 ) {
76     return 3;
77   }
78   if ( fgState.DisplayMode & GLUT_AUX2 ) {
79     return 2;
80   }
81   if ( fgState.DisplayMode & GLUT_AUX1 ) { /* NOTE: Same as GLUT_AUX! */
82     return fgState.AuxiliaryBufferNumber;
83   }
84   return 0;
85 }
86
87 int fghMapBit( int mask, int from, int to )
88 {
89   return ( mask & from ) ? to : 0;
90
91 }
92
93 void fghContextCreationError( void )
94 {
95     fgError( "Unable to create OpenGL %d.%d context (flags %x, profile %x)",
96              fgState.MajorVersion, fgState.MinorVersion, fgState.ContextFlags,
97              fgState.ContextProfile );
98 }
99
100
101 /* -- SYSTEM-DEPENDENT PRIVATE FUNCTIONS ------------------------------------ */
102
103 /*
104  * Sets the OpenGL context and the fgStructure "Current Window" pointer to
105  * the window structure passed in.
106  */
107 void fgSetWindow ( SFG_Window *window )
108 {
109         fgPlatformSetWindow ( window );
110
111     fgStructure.CurrentWindow = window;
112 }
113
114 /*
115  * Opens a window. Requires a SFG_Window object created and attached
116  * to the freeglut structure. OpenGL context is created here.
117  */
118 void fgOpenWindow( SFG_Window* window, const char* title,
119                    GLboolean positionUse, int x, int y,
120                    GLboolean sizeUse, int w, int h,
121                    GLboolean gameMode, GLboolean isSubWindow )
122 {
123         fgPlatformOpenWindow( window, title,
124                    positionUse, x, y,
125                    sizeUse, w, h,
126                    gameMode, isSubWindow );
127
128     fgSetWindow( window );
129
130     window->Window.DoubleBuffered =
131         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
132
133 #ifndef EGL_VERSION_1_0  /* No glDrawBuffer/glReadBuffer in GLES */
134     if ( ! window->Window.DoubleBuffered )
135     {
136         glDrawBuffer ( GL_FRONT );
137         glReadBuffer ( GL_FRONT );
138     }
139 #endif
140     window->Window.attribute_v_coord = -1;
141     window->Window.attribute_v_normal = -1;
142     window->Window.attribute_v_texture = -1;
143
144     fgInitGL2();
145
146     window->State.WorkMask |= GLUT_INIT_WORK;
147 }
148
149 /*
150  * Closes a window, destroying the frame and OpenGL context
151  */
152 void fgCloseWindow( SFG_Window* window )
153 {
154     /* if we're in gamemode and we're closing the gamemode window,
155      * call glutLeaveGameMode first to make sure the gamemode is
156      * properly closed before closing the window
157      */
158     if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==window->ID)
159         glutLeaveGameMode();
160
161         fgPlatformCloseWindow ( window );
162 }
163
164
165 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
166
167 /*
168  * Creates a new top-level freeglut window
169  */
170 int FGAPIENTRY glutCreateWindow( const char* title )
171 {
172     /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
173      * XXX application has not already done so.  The "freeglut" community
174      * XXX decided not to go this route (freeglut-developer e-mail from
175      * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
176      * XXX Desired 'freeglut' behaviour when there is no current window"
177      */
178     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
179
180     return fgCreateWindow( NULL, title, fgState.Position.Use,
181                            fgState.Position.X, fgState.Position.Y,
182                            fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
183                            GL_FALSE, GL_FALSE )->ID;
184 }
185
186 /*
187  * This function creates a sub window.
188  */
189 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
190 {
191     int ret = 0;
192     SFG_Window* window = NULL;
193     SFG_Window* parent = NULL;
194
195     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
196     parent = fgWindowByID( parentID );
197     freeglut_return_val_if_fail( parent != NULL, 0 );
198     if ( x < 0 )
199     {
200         x = parent->State.Width + x ;
201         if ( w >= 0 ) x -= w ;
202     }
203
204     if ( w < 0 ) w = parent->State.Width - x + w ;
205     if ( w < 0 )
206     {
207         x += w ;
208         w = -w ;
209     }
210
211     if ( y < 0 )
212     {
213         y = parent->State.Height + y ;
214         if ( h >= 0 ) y -= h ;
215     }
216
217     if ( h < 0 ) h = parent->State.Height - y + h ;
218     if ( h < 0 )
219     {
220         y += h ;
221         h = -h ;
222     }
223
224     window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
225     ret = window->ID;
226
227     return ret;
228 }
229
230 /*
231  * Destroys a window and all of its subwindows
232  */
233 void FGAPIENTRY glutDestroyWindow( int windowID )
234 {
235     SFG_Window* window;
236     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
237     window = fgWindowByID( windowID );
238     freeglut_return_if_fail( window != NULL );
239     {
240         fgExecutionState ExecState = fgState.ExecState;
241         fgAddToWindowDestroyList( window );
242         fgState.ExecState = ExecState;
243     }
244 }
245
246 /*
247  * This function selects the specified window as the current window
248  */
249 void FGAPIENTRY glutSetWindow( int ID )
250 {
251     SFG_Window* window = NULL;
252
253     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
254     if( fgStructure.CurrentWindow != NULL )
255         if( fgStructure.CurrentWindow->ID == ID )
256             return;
257
258     window = fgWindowByID( ID );
259     if( window == NULL )
260     {
261         fgWarning( "glutSetWindow(): window ID %d not found!", ID );
262         return;
263     }
264
265     fgSetWindow( window );
266 }
267
268 /*
269  * This function returns the ID number of the current window, 0 if none exists
270  */
271 int FGAPIENTRY glutGetWindow( void )
272 {
273     SFG_Window *win = fgStructure.CurrentWindow;
274     /*
275      * Since GLUT did not throw an error if this function was called without a prior call to
276      * "glutInit", this function shouldn't do so here.  Instead let us return a zero.
277      * See Feature Request "[ 1307049 ] glutInit check".
278      */
279     if ( ! fgState.Initialised )
280         return 0;
281
282     while ( win && win->IsMenu )
283         win = win->Parent;
284     return win ? win->ID : 0;
285 }
286
287 /*
288  * This function makes the current window visible
289  */
290 void FGAPIENTRY glutShowWindow( void )
291 {
292     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
293     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
294
295     fgStructure.CurrentWindow->State.WorkMask |= GLUT_VISIBILITY_WORK;
296     fgStructure.CurrentWindow->State.DesiredVisibility = DesireNormalState;
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     fgStructure.CurrentWindow->State.WorkMask |= GLUT_VISIBILITY_WORK;
310     fgStructure.CurrentWindow->State.DesiredVisibility = DesireHiddenState;
311
312     fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
313 }
314
315 /*
316  * Iconify the current window (top-level windows only)
317  */
318 void FGAPIENTRY glutIconifyWindow( void )
319 {
320     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
321     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
322
323     fgStructure.CurrentWindow->State.WorkMask |= GLUT_VISIBILITY_WORK;
324     fgStructure.CurrentWindow->State.DesiredVisibility = DesireIconicState;
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.WorkMask |= GLUT_SIZE_WORK;
371     fgStructure.CurrentWindow->State.DesiredWidth  = width ;
372     fgStructure.CurrentWindow->State.DesiredHeight = 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     fgStructure.CurrentWindow->State.WorkMask |= GLUT_POSITION_WORK;
390     fgStructure.CurrentWindow->State.DesiredXpos = x;
391     fgStructure.CurrentWindow->State.DesiredYpos = y;
392 }
393
394 /*
395  * Lowers the current window (by Z order change)
396  */
397 void FGAPIENTRY glutPushWindow( void )
398 {
399     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
400     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
401
402     fgStructure.CurrentWindow->State.WorkMask |= GLUT_ZORDER_WORK;
403     fgStructure.CurrentWindow->State.DesiredZOrder = -1;
404 }
405
406 /*
407  * Raises the current window (by Z order change)
408  */
409 void FGAPIENTRY glutPopWindow( void )
410 {
411     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
412     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
413
414     fgStructure.CurrentWindow->State.WorkMask |= GLUT_ZORDER_WORK;
415     fgStructure.CurrentWindow->State.DesiredZOrder = 1;
416 }
417
418 /*
419  * Resize the current window so that it fits the whole screen
420  */
421 void FGAPIENTRY glutFullScreen( void )
422 {
423     SFG_Window *win;
424
425     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
426     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
427
428     win = fgStructure.CurrentWindow;
429
430     if (win->Parent)
431     {
432         /* Child windows cannot be made fullscreen, consistent with GLUT's behavior
433          * Also, what would it mean for a child window to be fullscreen, given that it
434          * is confined to its parent?
435          */
436         fgWarning("glutFullScreen called on a child window, ignoring...");
437         return;
438     }
439     else if (fgStructure.GameModeWindow != NULL && fgStructure.GameModeWindow->ID==win->ID && win->State.IsFullscreen)
440     {
441         /* Ignore fullscreen call on GameMode window, those are always fullscreen already
442          * only exception is when first entering GameMode
443          */
444         return;
445     }
446
447     if (!win->State.IsFullscreen)
448             win->State.WorkMask |= GLUT_FULL_SCREEN_WORK;
449 }
450
451 /*
452  * If we are fullscreen, resize the current window back to its original size
453  */
454 void FGAPIENTRY glutLeaveFullScreen( void )
455 {
456     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
457     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
458
459     if (fgStructure.CurrentWindow->State.IsFullscreen)
460         fgStructure.CurrentWindow->State.WorkMask |= GLUT_FULL_SCREEN_WORK;
461 }
462
463 /*
464  * Toggle the window's full screen state.
465  */
466 void FGAPIENTRY glutFullScreenToggle( void )
467 {
468     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreenToggle" );
469     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreenToggle" );
470
471     fgStructure.CurrentWindow->State.WorkMask |= GLUT_FULL_SCREEN_WORK;
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 ***/