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