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