f99b673763577b185fe0e489eefdd659a0b7d598
[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 "../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         unsigned int current_DisplayMode = fgState.DisplayMode ;
277         fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ;
278         window->Window.VisualInfo = fgChooseVisual( );
279         fgState.DisplayMode = current_DisplayMode ;
280     }
281
282     if( ! window->Window.VisualInfo )
283     {
284         /*
285          * The "fgChooseVisual" returned a null meaning that the visual
286          * context is not available.
287          * Try a couple of variations to see if they will work.
288          */
289         if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
290         {
291             fgState.DisplayMode |= GLUT_DOUBLE ;
292             window->Window.VisualInfo = fgChooseVisual( );
293             fgState.DisplayMode &= ~GLUT_DOUBLE;
294         }
295         
296         /*
297          * GLUT also checks for multi-sampling, but I don't see that
298          * anywhere else in FREEGLUT so I won't bother with it for the moment.
299          */
300     }
301
302     /*
303      * XXX This seems to be abusing an assert() for error-checking.
304      * XXX It is possible that the visual simply can't be found,
305      * XXX in which case we should print an error and return a 0
306      * XXX for the window id, I think.
307      */
308     assert( window->Window.VisualInfo != NULL );
309
310     window->State.IsOffscreen = GL_FALSE;
311     if( fgState.DisplayMode & GLUT_OFFSCREEN )
312     {
313         window->State.IsOffscreen = GL_TRUE;
314         window->Window.Pixmap = XCreatePixmap(
315             fgDisplay.Display, fgDisplay.RootWindow,
316             w, h,
317             window->Window.VisualInfo->depth
318         );
319         if( False != window->Window.Pixmap )
320         {
321             window->Window.Handle = glXCreateGLXPixmap(
322                 fgDisplay.Display,
323                 window->Window.VisualInfo,
324                 window->Window.Pixmap
325             );
326             if( False == window->Window.Handle )
327                 XFreePixmap( fgDisplay.Display, window->Window.Pixmap );
328         }
329     }
330     else
331     {
332         /*
333          * XXX HINT: the masks should be updated when adding/removing callbacks.
334          * XXX       This might speed up message processing. Is that true?
335          * XXX
336          * XXX A: Not appreciably, but it WILL make it easier to debug.
337          * XXX    Try tracing old GLUT and try tracing freeglut.  Old GLUT
338          * XXX    turns off events that it doesn't need and is a whole lot
339          * XXX    more pleasant to trace.  (Hint: Think mouse-motion!)
340          * XXX
341          * XXX    It may make a difference in networked environments or on
342          * XXX    some very slow systems, but I think that that is secondary
343          * XXX    to making debugging easier.
344          */
345         winAttr.event_mask        =
346             StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
347             ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyRelease |
348             VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
349             PointerMotionMask | ButtonMotionMask;
350         winAttr.background_pixmap = None;
351         winAttr.background_pixel  = 0;
352         winAttr.border_pixel      = 0;
353
354         winAttr.colormap = XCreateColormap(
355             fgDisplay.Display, fgDisplay.RootWindow,
356             window->Window.VisualInfo->visual, AllocNone
357         );
358
359         mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
360
361         if ( window->IsMenu )
362         {
363             winAttr.override_redirect = True;
364             mask |= CWOverrideRedirect;
365         }
366
367         window->Window.Handle = XCreateWindow(
368             fgDisplay.Display,
369             window->Parent == NULL ? fgDisplay.RootWindow :
370                                      window->Parent->Window.Handle,
371             x, y, w, h, 0,
372             window->Window.VisualInfo->depth, InputOutput,
373             window->Window.VisualInfo->visual, mask,
374             &winAttr
375         );
376     }
377     /*
378      * The GLX context creation, possibly trying the direct context rendering
379      *  or else use the current context if the user has so specified
380      */
381     if( window->IsMenu )
382     {
383         /*
384          * If there isn't already an OpenGL rendering context for menu
385          * windows, make one
386          */
387         if( !fgStructure.MenuContext )
388         {
389             fgStructure.MenuContext =
390                 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
391             fgStructure.MenuContext->VisualInfo = window->Window.VisualInfo;
392             fgStructure.MenuContext->Context = glXCreateContext(
393                 fgDisplay.Display, fgStructure.MenuContext->VisualInfo,
394                 NULL, fgState.ForceDirectContext | fgState.TryDirectContext
395             );
396         }
397
398 /*      window->Window.Context = fgStructure.MenuContext->Context ; */
399         window->Window.Context = glXCreateContext(
400             fgDisplay.Display, window->Window.VisualInfo,
401             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
402         );
403     }
404     else if( fgState.UseCurrentContext )
405     {
406         window->Window.Context = glXGetCurrentContext( );
407
408         if( ! window->Window.Context )
409             window->Window.Context = glXCreateContext(
410                 fgDisplay.Display, window->Window.VisualInfo,
411                 NULL, fgState.ForceDirectContext | fgState.TryDirectContext
412             );
413     }
414     else
415         window->Window.Context = glXCreateContext(
416             fgDisplay.Display, window->Window.VisualInfo,
417             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
418         );
419
420     if( fgState.ForceDirectContext &&
421         !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
422         fgError( "unable to force direct context rendering for window '%s'",
423                  title );
424
425     glXMakeCurrent(
426         fgDisplay.Display,
427         window->Window.Handle,
428         window->Window.Context
429     );
430
431     /*
432      * XXX Assume the new window is visible by default
433      * XXX Is this a  safe assumption?
434      */
435     window->State.Visible = GL_TRUE;
436
437     sizeHints.flags = 0;
438     if ( fgState.Position.Use )
439         sizeHints.flags |= USPosition;
440     if ( fgState.Size.Use )
441         sizeHints.flags |= USSize;
442
443     /*
444      * Fill in the size hints values now (the x, y, width and height
445      * settings are obsolote, are there any more WMs that support them?)
446      * Unless the X servers actually stop supporting these, we should
447      * continue to fill them in.  It is *not* our place to tell the user
448      * that they should replace a window manager that they like, and which
449      * works, just because *we* think that it's not "modern" enough.
450      */
451     sizeHints.x      = x;
452     sizeHints.y      = y;
453     sizeHints.width  = w;
454     sizeHints.height = h;
455
456     wmHints.flags = StateHint;
457     wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
458     if( GL_FALSE == window->State.IsOffscreen )
459     {
460         /*
461          * Prepare the window and iconified window names...
462          */
463         XStringListToTextProperty( (char **) &title, 1, &textProperty );
464
465         XSetWMProperties(
466             fgDisplay.Display,
467             window->Window.Handle,
468             &textProperty,
469             &textProperty,
470             0,
471             0,
472             &sizeHints,
473             &wmHints,
474             NULL
475         );
476     
477         XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
478                          &fgDisplay.DeleteWindow, 1 );
479     
480         XMapWindow( fgDisplay.Display, window->Window.Handle );
481     }
482     
483 #elif TARGET_HOST_WIN32
484
485     WNDCLASS wc;
486     int flags;
487     ATOM atom;
488
489     freeglut_assert_ready;
490     
491     /*
492      * Grab the window class we have registered on glutInit():
493      */
494     atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
495     assert( atom != 0 );
496     
497     if( gameMode )
498     {
499         assert( window->Parent == NULL );
500
501         /*
502          * Set the window creation flags appropriately to make the window
503          * entirely visible:
504          */
505         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
506     }
507     else
508     {
509         if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
510         {
511             /*
512              * Update the window dimensions, taking account of window
513              * decorations.  "freeglut" is to create the window with the
514              * outside of its border at (x,y) and with dimensions (w,h).
515              */
516             w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
517             h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
518                 GetSystemMetrics( SM_CYCAPTION );
519         }
520
521         if( ! fgState.Position.Use )
522         {
523             x = CW_USEDEFAULT;
524             y = CW_USEDEFAULT;
525         }
526         if( ! fgState.Size.Use )
527         {
528             w = CW_USEDEFAULT;
529             h = CW_USEDEFAULT;
530         }
531
532         /*
533          * There's a small difference between creating the top, child and
534          * game mode windows
535          */
536         flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
537
538         if ( window->IsMenu )
539             flags |= WS_POPUP ;
540         else if( window->Parent == NULL )
541             flags |= WS_OVERLAPPEDWINDOW;
542         else
543             flags |= WS_CHILD;
544     }
545
546     window->Window.Handle = CreateWindow( 
547         "FREEGLUT",
548         title,
549         flags,
550         x, y, w, h,
551         (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
552         (HMENU) NULL,
553         fgDisplay.Instance,
554         (LPVOID) window
555     );
556     if( !( window->Window.Handle ) )
557         fgError( "Failed to create a window (%s)!", title );
558
559     ShowWindow( window->Window.Handle,
560                 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
561     UpdateWindow( window->Window.Handle );
562     ShowCursor( TRUE );  /* XXX Old comments say "hide cusror"! */
563
564 #endif
565
566     fgSetWindow( window );
567
568     window->Window.DoubleBuffered =
569         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
570
571     if ( ! window->Window.DoubleBuffered )
572     {
573         glDrawBuffer ( GL_FRONT );
574         glReadBuffer ( GL_FRONT );
575     }
576 }
577
578 /*
579  * Closes a window, destroying the frame and OpenGL context
580  */
581 void fgCloseWindow( SFG_Window* window )
582 {
583     freeglut_assert_ready;
584
585 #if TARGET_HOST_UNIX_X11
586
587     glXDestroyContext( fgDisplay.Display, window->Window.Context );
588     if( GL_FALSE == window->State.IsOffscreen )
589         XDestroyWindow( fgDisplay.Display, window->Window.Handle );
590     else
591     {
592         glXDestroyGLXPixmap( fgDisplay.Display, window->Window.Handle );
593         XFreePixmap( fgDisplay.Display, window->Window.Pixmap );
594     }
595     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
596
597 #elif TARGET_HOST_WIN32
598
599     /*
600      * Make sure we don't close a window with current context active
601      */
602     if( fgStructure.Window == window )
603         wglMakeCurrent( NULL, NULL );
604
605     /*
606      * Step through the list of windows.  If the rendering context
607      * is not being used by another window, then we delete it.
608      */
609     {
610         int used = FALSE ;
611         SFG_Window *iter ;
612
613         for( iter = (SFG_Window *)fgStructure.Windows.First;
614              iter;
615              iter = (SFG_Window *)iter->Node.Next )
616         {
617             if( ( iter->Window.Context == window->Window.Context ) &&
618                 ( iter != window ) )
619                 used = TRUE;
620         }
621
622         if( ! used )
623             wglDeleteContext( window->Window.Context );
624     }
625
626     DestroyWindow( window->Window.Handle );
627 #endif
628 }
629
630
631 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
632
633 /*
634  * Creates a new top-level freeglut window
635  */
636 int FGAPIENTRY glutCreateWindow( const char* title )
637 {
638     return fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
639                            fgState.Size.X, fgState.Size.Y, GL_FALSE,
640                            GL_FALSE )->ID;
641 }
642
643 /*
644  * This function creates a sub window.
645  */
646 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
647 {
648     SFG_Window* window = NULL;
649     SFG_Window* parent = NULL;
650
651     freeglut_assert_ready;
652     parent = fgWindowByID( parentID );
653     freeglut_return_val_if_fail( parent != NULL, 0 );
654     window = fgCreateWindow( parent, "", x, y, w, h, GL_FALSE, GL_FALSE );
655     return window->ID;
656 }
657
658 /*
659  * Destroys a window and all of its subwindows
660  */
661 void FGAPIENTRY glutDestroyWindow( int windowID )
662 {
663     SFG_Window* window = fgWindowByID( windowID );
664     freeglut_return_if_fail( window != NULL );
665     {
666         fgExecutionState ExecState = fgState.ExecState;
667         fgAddToWindowDestroyList( window );
668         fgState.ExecState = ExecState;
669     }
670 }
671
672 /*
673  * This function selects the current window
674  */
675 void FGAPIENTRY glutSetWindow( int ID )
676 {
677     SFG_Window* window = NULL;
678
679     freeglut_assert_ready;
680     if( fgStructure.Window != NULL )
681         if( fgStructure.Window->ID == ID )
682             return;
683
684     window = fgWindowByID( ID );
685     if( window == NULL )
686     {
687         fgWarning( "glutSetWindow(): window ID %i not found!", ID );
688         return;
689     }
690
691     fgSetWindow( window );
692 }
693
694 /*
695  * This function returns the ID number of the current window, 0 if none exists
696  */
697 int FGAPIENTRY glutGetWindow( void )
698 {
699     freeglut_assert_ready;
700     if( fgStructure.Window == NULL )
701         return 0;
702     return fgStructure.Window->ID;
703 }
704
705 /*
706  * This function makes the current window visible
707  */
708 void FGAPIENTRY glutShowWindow( void )
709 {
710     freeglut_assert_ready;
711     freeglut_assert_window;
712
713     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
714     {
715 #if TARGET_HOST_UNIX_X11
716
717         XMapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
718         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
719
720 #elif TARGET_HOST_WIN32
721
722         ShowWindow( fgStructure.Window->Window.Handle, SW_SHOW );
723
724 #endif
725     }
726     fgStructure.Window->State.Redisplay = GL_TRUE;
727 }
728
729 /*
730  * This function hides the current window
731  */
732 void FGAPIENTRY glutHideWindow( void )
733 {
734     freeglut_assert_ready;
735     freeglut_assert_window;
736
737     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
738     {
739 #if TARGET_HOST_UNIX_X11
740
741         if( fgStructure.Window->Parent == NULL )
742             XWithdrawWindow( fgDisplay.Display,
743                              fgStructure.Window->Window.Handle,
744                              fgDisplay.Screen );
745         else
746             XUnmapWindow( fgDisplay.Display,
747                           fgStructure.Window->Window.Handle );
748         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
749
750 #elif TARGET_HOST_WIN32
751
752         ShowWindow( fgStructure.Window->Window.Handle, SW_HIDE );
753
754 #endif
755     }
756     fgStructure.Window->State.Redisplay = GL_FALSE;
757 }
758
759 /*
760  * Iconify the current window (top-level windows only)
761  */
762 void FGAPIENTRY glutIconifyWindow( void )
763 {
764     freeglut_assert_ready;
765     freeglut_assert_window;
766
767     fgStructure.Window->State.Visible   = GL_FALSE;
768     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
769     {
770 #if TARGET_HOST_UNIX_X11
771
772         XIconifyWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
773                         fgDisplay.Screen );
774         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
775
776 #elif TARGET_HOST_WIN32
777
778         ShowWindow( fgStructure.Window->Window.Handle, SW_MINIMIZE );
779
780 #endif
781     }
782     fgStructure.Window->State.Redisplay = GL_FALSE;
783 }
784
785 /*
786  * Set the current window's title
787  */
788 void FGAPIENTRY glutSetWindowTitle( const char* title )
789 {
790     freeglut_assert_ready;
791     freeglut_assert_window;
792     if( ! fgStructure.Window->Parent &&
793         ( GL_FALSE == fgStructure.Window->State.IsOffscreen ) )
794     {
795 #if TARGET_HOST_UNIX_X11
796
797         XTextProperty text;
798         
799         text.value = (unsigned char *) title;
800         text.encoding = XA_STRING;
801         text.format = 8;
802         text.nitems = strlen( title );
803         
804         XSetWMName(
805             fgDisplay.Display,
806             fgStructure.Window->Window.Handle,
807             &text
808         );
809         
810         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
811
812 #elif TARGET_HOST_WIN32
813
814         SetWindowText( fgStructure.Window->Window.Handle, title );
815
816 #endif
817     }
818 }
819
820 /*
821  * Set the current window's iconified title
822  */
823 void FGAPIENTRY glutSetIconTitle( const char* title )
824 {
825     freeglut_assert_ready;
826     freeglut_assert_window;
827
828     if( ! fgStructure.Window->Parent &&
829         GL_FALSE == fgStructure.Window->State.IsOffscreen )
830     {
831 #if TARGET_HOST_UNIX_X11
832
833         XTextProperty text;
834         
835         text.value = (unsigned char *) title;
836         text.encoding = XA_STRING;
837         text.format = 8;
838         text.nitems = strlen( title );
839
840         XSetWMIconName(
841             fgDisplay.Display,
842             fgStructure.Window->Window.Handle,
843             &text
844         );
845
846         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
847
848 #elif TARGET_HOST_WIN32
849
850         SetWindowText( fgStructure.Window->Window.Handle, title );
851
852 #endif
853     }
854 }
855
856 /*
857  * Change the current window's size
858  */
859 void FGAPIENTRY glutReshapeWindow( int width, int height )
860 {
861     freeglut_assert_ready;
862     freeglut_assert_window;
863
864     /* XXX Could delete/create/set-window-id for offscreen. */
865     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
866     {
867         fgStructure.Window->State.NeedToResize = GL_TRUE;
868         fgStructure.Window->State.Width  = width ;
869         fgStructure.Window->State.Height = height;
870     }
871 }
872
873 /*
874  * Change the current window's position
875  */
876 void FGAPIENTRY glutPositionWindow( int x, int y )
877 {
878     freeglut_assert_ready;
879     freeglut_assert_window;
880
881     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
882     {
883 #if TARGET_HOST_UNIX_X11
884
885         XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
886                      x, y );
887         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
888
889 #elif TARGET_HOST_WIN32
890
891         RECT winRect;
892         
893         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
894         MoveWindow(
895             fgStructure.Window->Window.Handle,
896             x,
897             y,
898             winRect.right - winRect.left,
899             winRect.bottom - winRect.top,
900             TRUE
901         );
902
903 #endif
904     }
905 }
906
907 /*
908  * Lowers the current window (by Z order change)
909  */
910 void FGAPIENTRY glutPushWindow( void )
911 {
912     freeglut_assert_ready;
913     freeglut_assert_window;
914
915     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
916     {
917 #if TARGET_HOST_UNIX_X11
918
919         XLowerWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
920
921 #elif TARGET_HOST_WIN32
922
923         SetWindowPos(
924             fgStructure.Window->Window.Handle,
925             HWND_BOTTOM,
926             0, 0, 0, 0,
927             SWP_NOSIZE | SWP_NOMOVE
928         );
929
930 #endif
931     }
932 }
933
934 /*
935  * Raises the current window (by Z order change)
936  */
937 void FGAPIENTRY glutPopWindow( void )
938 {
939     freeglut_assert_ready;
940     freeglut_assert_window;
941
942     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
943     {
944 #if TARGET_HOST_UNIX_X11
945
946         XRaiseWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
947
948 #elif TARGET_HOST_WIN32
949
950         SetWindowPos(
951             fgStructure.Window->Window.Handle,
952             HWND_TOP,
953             0, 0, 0, 0,
954             SWP_NOSIZE | SWP_NOMOVE
955         );
956
957 #endif
958     }
959 }
960
961 /*
962  * Resize the current window so that it fits the whole screen
963  */
964 void FGAPIENTRY glutFullScreen( void )
965 {
966     freeglut_assert_ready;
967     freeglut_assert_window;
968
969     if( GL_FALSE == fgStructure.Window->State.IsOffscreen )
970     {
971 #if TARGET_HOST_UNIX_X11
972         int x, y;
973         Window w;
974
975         XMoveResizeWindow(
976             fgDisplay.Display,
977             fgStructure.Window->Window.Handle,
978             0, 0,
979             fgDisplay.ScreenWidth,
980             fgDisplay.ScreenHeight
981         );
982
983         XFlush( fgDisplay.Display ); /* This is needed */
984
985         XTranslateCoordinates(
986             fgDisplay.Display,
987             fgStructure.Window->Window.Handle,
988             fgDisplay.RootWindow,
989             0, 0, &x, &y, &w
990         );
991
992         if (x || y)
993         {
994             XMoveWindow(
995                 fgDisplay.Display,
996                 fgStructure.Window->Window.Handle,
997                 -x, -y
998             );
999             XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1000         }
1001 #elif TARGET_HOST_WIN32
1002         RECT rect;
1003
1004         /* For fullscreen mode, force the top-left corner to 0,0
1005          * and adjust the window rectangle so that the client area
1006          * covers the whole screen.
1007          */
1008
1009         rect.left   = 0;
1010         rect.top    = 0;
1011         rect.right  = fgDisplay.ScreenWidth;
1012         rect.bottom = fgDisplay.ScreenHeight;
1013
1014         AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1015                                   WS_CLIPCHILDREN, FALSE );
1016
1017         /*
1018          * SWP_NOACTIVATE     Do not activate the window
1019          * SWP_NOOWNERZORDER  Do not change position in z-order
1020          * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1021          * SWP_NOZORDER       Retains the current Z order (ignore 2nd param)
1022          */
1023
1024         SetWindowPos( fgStructure.Window->Window.Handle,
1025                       HWND_TOP,
1026                       rect.left,
1027                       rect.top,
1028                       rect.right  - rect.left,
1029                       rect.bottom - rect.top,
1030                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1031                       SWP_NOZORDER
1032                     );
1033 #endif
1034     }
1035 }
1036
1037 /*
1038  * A.Donev: Set and retrieve the window's user data
1039  */
1040 void* FGAPIENTRY glutGetWindowData( void )
1041 {
1042     return fgStructure.Window->UserData;
1043 }
1044
1045 void FGAPIENTRY glutSetWindowData(void* data)
1046 {
1047     fgStructure.Window->UserData = data;
1048 }
1049
1050 /*** END OF FILE ***/