3723520ec24916690ad56dcb1dea9035c6b277fa
[freeglut] / src / freeglut_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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <GL/freeglut.h>
33 #include "freeglut_internal.h"
34
35 /*
36  * TODO BEFORE THE STABLE RELEASE:
37  *
38  *  fgChooseVisual()        -- OK, but what about glutInitDisplayString()?
39  *  fgSetupPixelFormat      -- ignores the display mode settings
40  *  fgOpenWindow()          -- check the Win32 version, -iconic handling!
41  *  fgCloseWindow()         -- check the Win32 version
42  *  glutCreateWindow()      -- Check when default position and size is {-1,-1}
43  *  glutCreateSubWindow()   -- Check when default position and size is {-1,-1}
44  *  glutDestroyWindow()     -- check the Win32 version
45  *  glutSetWindow()         -- check the Win32 version
46  *  glutGetWindow()         -- OK
47  *  glutSetWindowTitle()    -- check the Win32 version
48  *  glutSetIconTitle()      -- check the Win32 version
49  *  glutShowWindow()        -- check the Win32 version
50  *  glutHideWindow()        -- check the Win32 version
51  *  glutIconifyWindow()     -- check the Win32 version
52  *  glutReshapeWindow()     -- check the Win32 version
53  *  glutPositionWindow()    -- check the Win32 version
54  *  glutPushWindow()        -- check the Win32 version
55  *  glutPopWindow()         -- check the Win32 version
56  */
57
58 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
59
60 /*
61  * Chooses a visual basing on the current display mode settings
62  */
63 #if TARGET_HOST_UNIX_X11
64
65 XVisualInfo* fgChooseVisual( void )
66 {
67 #define BUFFER_SIZES 6
68     int bufferSize[BUFFER_SIZES] = { 16, 12, 8, 4, 2, 1 };
69     GLboolean wantIndexedMode = GL_FALSE;
70     int attributes[ 32 ];
71     int where = 0;
72
73     /*
74      * First we have to process the display mode settings...
75      */
76 /*
77  * Why is there a semi-colon in this #define?  The code
78  * that uses the macro seems to always add more semicolons...
79  */
80 #define ATTRIB(a) attributes[where++]=a;
81 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
82
83     if( fgState.DisplayMode & GLUT_INDEX )
84     {
85         ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
86         wantIndexedMode = GL_TRUE;
87     }
88     else
89     {
90         ATTRIB( GLX_RGBA );
91         ATTRIB_VAL( GLX_RED_SIZE,   1 );
92         ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
93         ATTRIB_VAL( GLX_BLUE_SIZE,  1 );
94         if( fgState.DisplayMode & GLUT_ALPHA )
95             ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
96     }
97
98     if( fgState.DisplayMode & GLUT_DOUBLE )
99         ATTRIB( GLX_DOUBLEBUFFER );
100
101     if( fgState.DisplayMode & GLUT_STEREO )
102         ATTRIB( GLX_STEREO );
103
104     if( fgState.DisplayMode & GLUT_DEPTH )
105         ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
106
107     if( fgState.DisplayMode & GLUT_STENCIL )
108         ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
109
110     if( fgState.DisplayMode & GLUT_ACCUM )
111     {
112         ATTRIB_VAL( GLX_ACCUM_RED_SIZE,   1 );
113         ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
114         ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE,  1 );
115         if( fgState.DisplayMode & GLUT_ALPHA )
116             ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
117     }
118
119     /*
120      * Push a null at the end of the list
121      */
122     ATTRIB( None );
123
124     if( ! wantIndexedMode )
125         return glXChooseVisual( fgDisplay.Display, fgDisplay.Screen,
126                                 attributes );
127     else
128     {
129         XVisualInfo* visualInfo;
130         int i;
131
132         /*
133          * In indexed mode, we need to check how many bits of depth can we
134          * achieve.  We do this by trying each possibility from the list
135          * given in the {bufferSize} array.  If we match, we return to caller.
136          */
137         for( i=0; i<BUFFER_SIZES; i++ )
138         {
139             attributes[ 1 ] = bufferSize[ i ];
140             visualInfo = glXChooseVisual( fgDisplay.Display, fgDisplay.Screen,
141                                           attributes );
142             if( visualInfo != NULL )
143                 return visualInfo;
144         }
145         return NULL;
146     }
147 }
148 #endif
149
150 /*
151  * Setup the pixel format for a Win32 window
152  */
153 #if TARGET_HOST_WIN32
154 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
155                               unsigned char layer_type )
156 {
157     PIXELFORMATDESCRIPTOR* ppfd, pfd;
158     int flags, pixelformat;
159
160     freeglut_return_val_if_fail( window != NULL, 0 );
161     flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
162     if( fgState.DisplayMode & GLUT_DOUBLE )
163         flags |= PFD_DOUBLEBUFFER;
164
165 #if defined(_MSC_VER)
166 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
167 #endif
168
169     /*
170      * Specify which pixel format do we opt for...
171      */
172     pfd.nSize           = sizeof(PIXELFORMATDESCRIPTOR);
173     pfd.nVersion        = 1;
174     pfd.dwFlags         = flags;
175     pfd.iPixelType      = PFD_TYPE_RGBA;
176     pfd.cColorBits      = 24;
177     pfd.cRedBits        = 0;
178     pfd.cRedShift       = 0;
179     pfd.cGreenBits      = 0;
180     pfd.cGreenShift     = 0;
181     pfd.cBlueBits       = 0;
182     pfd.cBlueShift      = 0;
183     pfd.cAlphaBits      = 0;
184     pfd.cAlphaShift     = 0;
185     pfd.cAccumBits      = 0;
186     pfd.cAccumRedBits   = 0;
187     pfd.cAccumGreenBits = 0;
188     pfd.cAccumBlueBits  = 0;
189     pfd.cAccumAlphaBits = 0;
190 #if 0
191     pfd.cDepthBits      = 32;
192     pfd.cStencilBits    = 0;
193 #else
194     pfd.cDepthBits      = 24;
195     pfd.cStencilBits    = 8;
196 #endif
197     pfd.cAuxBuffers     = 0;
198     pfd.iLayerType      = layer_type;
199     pfd.bReserved       = 0;
200     pfd.dwLayerMask     = 0;
201     pfd.dwVisibleMask   = 0;
202     pfd.dwDamageMask    = 0;
203
204     pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
205     ppfd = &pfd;
206
207     pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
208     if( pixelformat == 0 )
209         return GL_FALSE;
210
211     if( checkOnly )
212         return GL_TRUE;
213     return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
214 }
215 #endif
216
217 /*
218  * Sets the OpenGL context and the fgStructure "Current Window" pointer to
219  * the window structure passed in.
220  */
221 void fgSetWindow ( SFG_Window *window )
222 {
223 #if TARGET_HOST_UNIX_X11
224     if ( window )
225         glXMakeCurrent(
226             fgDisplay.Display,
227             window->Window.Handle,
228             window->Window.Context
229         );
230 #elif TARGET_HOST_WIN32
231     if( fgStructure.Window )
232         ReleaseDC( fgStructure.Window->Window.Handle,
233                    fgStructure.Window->Window.Device );
234
235     if ( window )
236     {
237         window->Window.Device = GetDC( window->Window.Handle );
238         wglMakeCurrent(
239             window->Window.Device,
240             window->Window.Context
241         );
242     }
243 #endif
244     fgStructure.Window = window;
245 }
246
247
248 /*
249  * Opens a window. Requires a SFG_Window object created and attached
250  * to the freeglut structure. OpenGL context is created here.
251  */
252 void fgOpenWindow( SFG_Window* window, const char* title,
253                    int x, int y, int w, int h,
254                    GLboolean gameMode, GLboolean isSubWindow )
255 {
256 #if TARGET_HOST_UNIX_X11
257     XSetWindowAttributes winAttr;
258     XTextProperty textProperty;
259     XSizeHints sizeHints;
260     XWMHints wmHints;
261     unsigned long mask;
262
263     freeglut_assert_ready;
264
265     /*
266      * XXX fgChooseVisual() is a common part of all three.
267      * XXX With a little thought, we should be able to greatly
268      * XXX simplify this.
269      */
270     if( !window->IsMenu )
271         window->Window.VisualInfo = fgChooseVisual( );
272     else if( fgStructure.MenuContext )
273         window->Window.VisualInfo = fgChooseVisual( );
274     else
275     {
276         /* XXX Why are menus double- and depth-buffered? */
277         unsigned int current_DisplayMode = fgState.DisplayMode ;
278         fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ;
279         window->Window.VisualInfo = fgChooseVisual( );
280         fgState.DisplayMode = current_DisplayMode ;
281     }
282
283     if( ! window->Window.VisualInfo )
284     {
285         /*
286          * The "fgChooseVisual" returned a null meaning that the visual
287          * context is not available.
288          * Try a couple of variations to see if they will work.
289          */
290         if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
291         {
292             fgState.DisplayMode |= GLUT_DOUBLE ;
293             window->Window.VisualInfo = fgChooseVisual( );
294             fgState.DisplayMode &= ~GLUT_DOUBLE;
295         }
296
297         /*
298          * GLUT also checks for multi-sampling, but I don't see that
299          * anywhere else in FREEGLUT so I won't bother with it for the moment.
300          */
301     }
302
303     /*
304      * XXX This seems to be abusing an assert() for error-checking.
305      * XXX It is possible that the visual simply can't be found,
306      * XXX in which case we should print an error and return a 0
307      * XXX for the window id, I think.
308      */
309     assert( window->Window.VisualInfo != NULL );
310
311     window->State.IsOffscreen = GL_FALSE;
312     if( fgState.DisplayMode & GLUT_OFFSCREEN )
313     {
314         window->State.IsOffscreen = GL_TRUE;
315         window->Window.Pixmap = XCreatePixmap(
316             fgDisplay.Display, fgDisplay.RootWindow,
317             w, h,
318             window->Window.VisualInfo->depth
319         );
320         if( False != window->Window.Pixmap )
321         {
322             window->Window.Handle = glXCreateGLXPixmap(
323                 fgDisplay.Display,
324                 window->Window.VisualInfo,
325                 window->Window.Pixmap
326             );
327             if( False == window->Window.Handle )
328                 XFreePixmap( fgDisplay.Display, window->Window.Pixmap );
329         }
330     }
331     else
332     {
333         /*
334          * XXX HINT: the masks should be updated when adding/removing callbacks.
335          * XXX       This might speed up message processing. Is that true?
336          * XXX
337          * XXX A: Not appreciably, but it WILL make it easier to debug.
338          * XXX    Try tracing old GLUT and try tracing freeglut.  Old GLUT
339          * XXX    turns off events that it doesn't need and is a whole lot
340          * XXX    more pleasant to trace.  (Think mouse-motion!  Tons of
341          * XXX    ``bonus'' GUI events stream in.)
342          */
343         winAttr.event_mask        =
344             StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
345             ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyRelease |
346             VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
347             PointerMotionMask | ButtonMotionMask;
348         winAttr.background_pixmap = None;
349         winAttr.background_pixel  = 0;
350         winAttr.border_pixel      = 0;
351
352         winAttr.colormap = XCreateColormap(
353             fgDisplay.Display, fgDisplay.RootWindow,
354             window->Window.VisualInfo->visual, AllocNone
355         );
356
357         mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
358
359         if ( window->IsMenu )
360         {
361             winAttr.override_redirect = True;
362             mask |= CWOverrideRedirect;
363         }
364
365         window->Window.Handle = XCreateWindow(
366             fgDisplay.Display,
367             window->Parent == NULL ? fgDisplay.RootWindow :
368                                      window->Parent->Window.Handle,
369             x, y, w, h, 0,
370             window->Window.VisualInfo->depth, InputOutput,
371             window->Window.VisualInfo->visual, mask,
372             &winAttr
373         );
374     }
375     /*
376      * The GLX context creation, possibly trying the direct context rendering
377      *  or else use the current context if the user has so specified
378      */
379     if( window->IsMenu )
380     {
381         /*
382          * If there isn't already an OpenGL rendering context for menu
383          * windows, make one
384          */
385         if( !fgStructure.MenuContext )
386         {
387             fgStructure.MenuContext =
388                 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
389             fgStructure.MenuContext->VisualInfo = window->Window.VisualInfo;
390             fgStructure.MenuContext->Context = glXCreateContext(
391                 fgDisplay.Display, fgStructure.MenuContext->VisualInfo,
392                 NULL, fgState.ForceDirectContext | fgState.TryDirectContext
393             );
394         }
395
396         /* window->Window.Context = fgStructure.MenuContext->Context; */
397         window->Window.Context = glXCreateContext(
398             fgDisplay.Display, window->Window.VisualInfo,
399             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
400         );
401     }
402     else if( fgState.UseCurrentContext )
403     {
404         window->Window.Context = glXGetCurrentContext( );
405
406         if( ! window->Window.Context )
407             window->Window.Context = glXCreateContext(
408                 fgDisplay.Display, window->Window.VisualInfo,
409                 NULL, fgState.ForceDirectContext | fgState.TryDirectContext
410             );
411     }
412     else
413         window->Window.Context = glXCreateContext(
414             fgDisplay.Display, window->Window.VisualInfo,
415             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
416         );
417
418     if( fgState.ForceDirectContext &&
419         !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
420         fgError( "unable to force direct context rendering for window '%s'",
421                  title );
422
423     glXMakeCurrent(
424         fgDisplay.Display,
425         window->Window.Handle,
426         window->Window.Context
427     );
428
429     /*
430      * XXX Assume the new window is visible by default
431      * XXX Is this a  safe assumption?
432      */
433     window->State.Visible = GL_TRUE;
434
435     sizeHints.flags = 0;
436     if ( fgState.Position.Use )
437         sizeHints.flags |= USPosition;
438     if ( fgState.Size.Use )
439         sizeHints.flags |= USSize;
440
441     /*
442      * Fill in the size hints values now (the x, y, width and height
443      * settings are obsolote, are there any more WMs that support them?)
444      * Unless the X servers actually stop supporting these, we should
445      * continue to fill them in.  It is *not* our place to tell the user
446      * that they should replace a window manager that they like, and which
447      * works, just because *we* think that it's not "modern" enough.
448      */
449     sizeHints.x      = x;
450     sizeHints.y      = y;
451     sizeHints.width  = w;
452     sizeHints.height = h;
453
454     wmHints.flags = StateHint;
455     wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
456     if( GL_FALSE == window->State.IsOffscreen )
457     {
458         /*
459          * Prepare the window and iconified window names...
460          */
461         XStringListToTextProperty( (char **) &title, 1, &textProperty );
462
463         XSetWMProperties(
464             fgDisplay.Display,
465             window->Window.Handle,
466             &textProperty,
467             &textProperty,
468             0,
469             0,
470             &sizeHints,
471             &wmHints,
472             NULL
473         );
474
475         XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
476                          &fgDisplay.DeleteWindow, 1 );
477
478         XMapWindow( fgDisplay.Display, window->Window.Handle );
479     }
480
481 #elif TARGET_HOST_WIN32
482
483     WNDCLASS wc;
484     DWORD flags;
485     DWORD exFlags = 0;
486     ATOM atom;
487
488     freeglut_assert_ready;
489
490     /*
491      * Grab the window class we have registered on glutInit():
492      */
493     atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
494     assert( atom != 0 );
495
496     if( gameMode )
497     {
498         assert( window->Parent == NULL );
499
500         /*
501          * Set the window creation flags appropriately to make the window
502          * entirely visible:
503          */
504         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
505     }
506     else
507     {
508         if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
509         {
510             /*
511              * Update the window dimensions, taking account of window
512              * decorations.  "freeglut" is to create the window with the
513              * outside of its border at (x,y) and with dimensions (w,h).
514              */
515             w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
516             h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
517                 GetSystemMetrics( SM_CYCAPTION );
518         }
519
520         if( ! fgState.Position.Use )
521         {
522             x = CW_USEDEFAULT;
523             y = CW_USEDEFAULT;
524         }
525         if( ! fgState.Size.Use )
526         {
527             w = CW_USEDEFAULT;
528             h = CW_USEDEFAULT;
529         }
530
531         /*
532          * There's a small difference between creating the top, child and
533          * game mode windows
534          */
535         flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
536
537         if ( window->IsMenu )
538         {
539             flags |= WS_POPUP;
540             exFlags |= WS_EX_TOOLWINDOW;
541         }
542         else if( window->Parent == NULL )
543             flags |= WS_OVERLAPPEDWINDOW;
544         else
545             flags |= WS_CHILD;
546     }
547
548     window->Window.Handle = CreateWindowEx(
549         exFlags,
550         "FREEGLUT",
551         title,
552         flags,
553         x, y, w, h,
554         (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
555         (HMENU) NULL,
556         fgDisplay.Instance,
557         (LPVOID) window
558     );
559     if( !( window->Window.Handle ) )
560         fgError( "Failed to create a window (%s)!", title );
561
562     ShowWindow( window->Window.Handle,
563                 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
564     UpdateWindow( window->Window.Handle );
565     ShowCursor( TRUE );  /* XXX Old comments say "hide cusror"! */
566
567 #endif
568
569     fgSetWindow( window );
570
571     window->Window.DoubleBuffered =
572         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
573
574     if ( ! window->Window.DoubleBuffered )
575     {
576         glDrawBuffer ( GL_FRONT );
577         glReadBuffer ( GL_FRONT );
578     }
579 }
580
581 /*
582  * Closes a window, destroying the frame and OpenGL context
583  */
584 void fgCloseWindow( SFG_Window* window )
585 {
586     freeglut_assert_ready;
587
588 #if TARGET_HOST_UNIX_X11
589
590     glXDestroyContext( fgDisplay.Display, window->Window.Context );
591     if( GL_FALSE == window->State.IsOffscreen )
592         XDestroyWindow( fgDisplay.Display, window->Window.Handle );
593     else
594     {
595         glXDestroyGLXPixmap( fgDisplay.Display, window->Window.Handle );
596         XFreePixmap( fgDisplay.Display, window->Window.Pixmap );
597     }
598     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
599
600 #elif TARGET_HOST_WIN32
601
602     /*
603      * Make sure we don't close a window with current context active
604      */
605     if( fgStructure.Window == window )
606         wglMakeCurrent( NULL, NULL );
607
608     /*
609      * Step through the list of windows.  If the rendering context
610      * is not being used by another window, then we delete it.
611      */
612     {
613         int used = FALSE ;
614         SFG_Window *iter ;
615
616         for( iter = (SFG_Window *)fgStructure.Windows.First;
617              iter;
618              iter = (SFG_Window *)iter->Node.Next )
619         {
620             if( ( iter->Window.Context == window->Window.Context ) &&
621                 ( iter != window ) )
622                 used = TRUE;
623         }
624
625         if( ! used )
626             wglDeleteContext( window->Window.Context );
627     }
628
629     DestroyWindow( window->Window.Handle );
630 #endif
631 }
632
633
634 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
635
636 /*
637  * Creates a new top-level freeglut window
638  */
639 int FGAPIENTRY glutCreateWindow( const char* title )
640 {
641     return fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
642                            fgState.Size.X, fgState.Size.Y, GL_FALSE,
643                            GL_FALSE )->ID;
644 }
645
646 /*
647  * This function creates a sub window.
648  */
649 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
650 {
651     int ret = 0;
652
653     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
654     {
655         SFG_Window* window = NULL;
656         SFG_Window* parent = NULL;
657
658         freeglut_assert_ready;
659         parent = fgWindowByID( parentID );
660         freeglut_return_val_if_fail( parent != NULL, 0 );
661         window = fgCreateWindow( parent, "", x, y, w, h, GL_FALSE, GL_FALSE );
662         ret = window->ID;
663     }
664     return ret;
665 }
666
667 /*
668  * Destroys a window and all of its subwindows
669  */
670 void FGAPIENTRY glutDestroyWindow( int windowID )
671 {
672     SFG_Window* window = fgWindowByID( windowID );
673     freeglut_return_if_fail( window != NULL );
674     {
675         fgExecutionState ExecState = fgState.ExecState;
676         fgAddToWindowDestroyList( window );
677         fgState.ExecState = ExecState;
678     }
679 }
680
681 /*
682  * This function selects the current window
683  */
684 void FGAPIENTRY glutSetWindow( int ID )
685 {
686     SFG_Window* window = NULL;
687
688     freeglut_assert_ready;
689     if( fgStructure.Window != NULL )
690         if( fgStructure.Window->ID == ID )
691             return;
692
693     window = fgWindowByID( ID );
694     if( window == NULL )
695     {
696         fgWarning( "glutSetWindow(): window ID %i not found!", ID );
697         return;
698     }
699
700     fgSetWindow( window );
701 }
702
703 /*
704  * This function returns the ID number of the current window, 0 if none exists
705  */
706 int FGAPIENTRY glutGetWindow( void )
707 {
708     freeglut_assert_ready;
709     if( fgStructure.Window == NULL )
710         return 0;
711     return fgStructure.Window->ID;
712 }
713
714 /*
715  * This function makes the current window visible
716  */
717 void FGAPIENTRY glutShowWindow( void )
718 {
719     freeglut_assert_ready;
720     freeglut_assert_window;
721
722     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
723     {
724 #if TARGET_HOST_UNIX_X11
725
726         XMapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
727         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
728
729 #elif TARGET_HOST_WIN32
730
731         ShowWindow( fgStructure.Window->Window.Handle, SW_SHOW );
732
733 #endif
734     }
735     fgStructure.Window->State.Redisplay = GL_TRUE;
736 }
737
738 /*
739  * This function hides the current window
740  */
741 void FGAPIENTRY glutHideWindow( void )
742 {
743     freeglut_assert_ready;
744     freeglut_assert_window;
745
746     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
747     {
748 #if TARGET_HOST_UNIX_X11
749
750         if( fgStructure.Window->Parent == NULL )
751             XWithdrawWindow( fgDisplay.Display,
752                              fgStructure.Window->Window.Handle,
753                              fgDisplay.Screen );
754         else
755             XUnmapWindow( fgDisplay.Display,
756                           fgStructure.Window->Window.Handle );
757         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
758
759 #elif TARGET_HOST_WIN32
760
761         ShowWindow( fgStructure.Window->Window.Handle, SW_HIDE );
762
763 #endif
764     }
765     fgStructure.Window->State.Redisplay = GL_FALSE;
766 }
767
768 /*
769  * Iconify the current window (top-level windows only)
770  */
771 void FGAPIENTRY glutIconifyWindow( void )
772 {
773     freeglut_assert_ready;
774     freeglut_assert_window;
775
776     fgStructure.Window->State.Visible   = GL_FALSE;
777     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
778     {
779 #if TARGET_HOST_UNIX_X11
780
781         XIconifyWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
782                         fgDisplay.Screen );
783         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
784
785 #elif TARGET_HOST_WIN32
786
787         ShowWindow( fgStructure.Window->Window.Handle, SW_MINIMIZE );
788
789 #endif
790     }
791     fgStructure.Window->State.Redisplay = GL_FALSE;
792 }
793
794 /*
795  * Set the current window's title
796  */
797 void FGAPIENTRY glutSetWindowTitle( const char* title )
798 {
799     freeglut_assert_ready;
800     freeglut_assert_window;
801     if( ! fgStructure.Window->Parent &&
802         ( GL_FALSE == fgStructure.Window->State.IsOffscreen ) )
803     {
804 #if TARGET_HOST_UNIX_X11
805
806         XTextProperty text;
807
808         text.value = (unsigned char *) title;
809         text.encoding = XA_STRING;
810         text.format = 8;
811         text.nitems = strlen( title );
812
813         XSetWMName(
814             fgDisplay.Display,
815             fgStructure.Window->Window.Handle,
816             &text
817         );
818
819         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
820
821 #elif TARGET_HOST_WIN32
822
823         SetWindowText( fgStructure.Window->Window.Handle, title );
824
825 #endif
826     }
827 }
828
829 /*
830  * Set the current window's iconified title
831  */
832 void FGAPIENTRY glutSetIconTitle( const char* title )
833 {
834     freeglut_assert_ready;
835     freeglut_assert_window;
836
837     if( ! fgStructure.Window->Parent &&
838         GL_FALSE == fgStructure.Window->State.IsOffscreen )
839     {
840 #if TARGET_HOST_UNIX_X11
841
842         XTextProperty text;
843
844         text.value = (unsigned char *) title;
845         text.encoding = XA_STRING;
846         text.format = 8;
847         text.nitems = strlen( title );
848
849         XSetWMIconName(
850             fgDisplay.Display,
851             fgStructure.Window->Window.Handle,
852             &text
853         );
854
855         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
856
857 #elif TARGET_HOST_WIN32
858
859         SetWindowText( fgStructure.Window->Window.Handle, title );
860
861 #endif
862     }
863 }
864
865 /*
866  * Change the current window's size
867  */
868 void FGAPIENTRY glutReshapeWindow( int width, int height )
869 {
870     freeglut_assert_ready;
871     freeglut_assert_window;
872
873     /* XXX Could delete/create/set-window-id for offscreen. */
874     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
875     {
876         fgStructure.Window->State.NeedToResize = GL_TRUE;
877         fgStructure.Window->State.Width  = width ;
878         fgStructure.Window->State.Height = height;
879     }
880 }
881
882 /*
883  * Change the current window's position
884  */
885 void FGAPIENTRY glutPositionWindow( int x, int y )
886 {
887     freeglut_assert_ready;
888     freeglut_assert_window;
889
890     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
891     {
892 #if TARGET_HOST_UNIX_X11
893
894         XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
895                      x, y );
896         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
897
898 #elif TARGET_HOST_WIN32
899
900         RECT winRect;
901
902         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
903         MoveWindow(
904             fgStructure.Window->Window.Handle,
905             x,
906             y,
907             winRect.right - winRect.left,
908             winRect.bottom - winRect.top,
909             TRUE
910         );
911
912 #endif
913     }
914 }
915
916 /*
917  * Lowers the current window (by Z order change)
918  */
919 void FGAPIENTRY glutPushWindow( void )
920 {
921     freeglut_assert_ready;
922     freeglut_assert_window;
923
924     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
925     {
926 #if TARGET_HOST_UNIX_X11
927
928         XLowerWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
929
930 #elif TARGET_HOST_WIN32
931
932         SetWindowPos(
933             fgStructure.Window->Window.Handle,
934             HWND_BOTTOM,
935             0, 0, 0, 0,
936             SWP_NOSIZE | SWP_NOMOVE
937         );
938
939 #endif
940     }
941 }
942
943 /*
944  * Raises the current window (by Z order change)
945  */
946 void FGAPIENTRY glutPopWindow( void )
947 {
948     freeglut_assert_ready;
949     freeglut_assert_window;
950
951     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
952     {
953 #if TARGET_HOST_UNIX_X11
954
955         XRaiseWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
956
957 #elif TARGET_HOST_WIN32
958
959         SetWindowPos(
960             fgStructure.Window->Window.Handle,
961             HWND_TOP,
962             0, 0, 0, 0,
963             SWP_NOSIZE | SWP_NOMOVE
964         );
965
966 #endif
967     }
968 }
969
970 /*
971  * Resize the current window so that it fits the whole screen
972  */
973 void FGAPIENTRY glutFullScreen( void )
974 {
975     freeglut_assert_ready;
976     freeglut_assert_window;
977
978     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
979     {
980 #if TARGET_HOST_UNIX_X11
981         int x, y;
982         Window w;
983
984         XMoveResizeWindow(
985             fgDisplay.Display,
986             fgStructure.Window->Window.Handle,
987             0, 0,
988             fgDisplay.ScreenWidth,
989             fgDisplay.ScreenHeight
990         );
991
992         XFlush( fgDisplay.Display ); /* This is needed */
993
994         XTranslateCoordinates(
995             fgDisplay.Display,
996             fgStructure.Window->Window.Handle,
997             fgDisplay.RootWindow,
998             0, 0, &x, &y, &w
999         );
1000
1001         if (x || y)
1002         {
1003             XMoveWindow(
1004                 fgDisplay.Display,
1005                 fgStructure.Window->Window.Handle,
1006                 -x, -y
1007             );
1008             XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1009         }
1010 #elif TARGET_HOST_WIN32
1011         RECT rect;
1012
1013         /* For fullscreen mode, force the top-left corner to 0,0
1014          * and adjust the window rectangle so that the client area
1015          * covers the whole screen.
1016          */
1017
1018         rect.left   = 0;
1019         rect.top    = 0;
1020         rect.right  = fgDisplay.ScreenWidth;
1021         rect.bottom = fgDisplay.ScreenHeight;
1022
1023         AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1024                                   WS_CLIPCHILDREN, FALSE );
1025
1026         /*
1027          * SWP_NOACTIVATE     Do not activate the window
1028          * SWP_NOOWNERZORDER  Do not change position in z-order
1029          * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1030          * SWP_NOZORDER       Retains the current Z order (ignore 2nd param)
1031          */
1032
1033         SetWindowPos( fgStructure.Window->Window.Handle,
1034                       HWND_TOP,
1035                       rect.left,
1036                       rect.top,
1037                       rect.right  - rect.left,
1038                       rect.bottom - rect.top,
1039                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1040                       SWP_NOZORDER
1041         );
1042 #endif
1043     }
1044 }
1045
1046 /*
1047  * A.Donev: Set and retrieve the window's user data
1048  */
1049 void* FGAPIENTRY glutGetWindowData( void )
1050 {
1051     return fgStructure.Window->UserData;
1052 }
1053
1054 void FGAPIENTRY glutSetWindowData(void* data)
1055 {
1056     fgStructure.Window->UserData = data;
1057 }
1058
1059 /*** END OF FILE ***/