Replace TRUE with GL_TRUE and FALSE with GL_FALSE where the type is
[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 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
166
167     /*
168      * Specify which pixel format do we opt for...
169      */
170     pfd.nSize           = sizeof(PIXELFORMATDESCRIPTOR);
171     pfd.nVersion        = 1;
172     pfd.dwFlags         = flags;
173     pfd.iPixelType      = PFD_TYPE_RGBA;
174     pfd.cColorBits      = 24;
175     pfd.cRedBits        = 0;
176     pfd.cRedShift       = 0;
177     pfd.cGreenBits      = 0;
178     pfd.cGreenShift     = 0;
179     pfd.cBlueBits       = 0;
180     pfd.cBlueShift      = 0;
181     pfd.cAlphaBits      = 0;
182     pfd.cAlphaShift     = 0;
183     pfd.cAccumBits      = 0;
184     pfd.cAccumRedBits   = 0;
185     pfd.cAccumGreenBits = 0;
186     pfd.cAccumBlueBits  = 0;
187     pfd.cAccumAlphaBits = 0;
188 #if 0
189     pfd.cDepthBits      = 32;
190     pfd.cStencilBits    = 0;
191 #else
192     pfd.cDepthBits      = 24;
193     pfd.cStencilBits    = 8;
194 #endif
195     pfd.cAuxBuffers     = 0;
196     pfd.iLayerType      = layer_type;
197     pfd.bReserved       = 0;
198     pfd.dwLayerMask     = 0;
199     pfd.dwVisibleMask   = 0;
200     pfd.dwDamageMask    = 0;
201
202     pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
203     ppfd = &pfd;
204     
205     pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
206     if( pixelformat == 0 )
207         return GL_FALSE;
208
209     if( checkOnly )
210         return GL_TRUE;
211     return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
212 }
213 #endif
214
215 /*
216  * Sets the OpenGL context and the fgStructure "Current Window" pointer to
217  * the window structure passed in.
218  */
219 void fgSetWindow ( SFG_Window *window )
220 {
221 #if TARGET_HOST_UNIX_X11
222     if ( window )
223         glXMakeCurrent(
224             fgDisplay.Display,
225             window->Window.Handle,
226             window->Window.Context
227         );
228 #elif TARGET_HOST_WIN32
229     if( fgStructure.Window )
230         ReleaseDC( fgStructure.Window->Window.Handle,
231                    fgStructure.Window->Window.Device );
232
233     if ( window )
234     {
235         window->Window.Device = GetDC( window->Window.Handle );
236         wglMakeCurrent( 
237             window->Window.Device, 
238             window->Window.Context
239         );
240     }
241 #endif
242     fgStructure.Window = window;
243 }
244
245
246 /*
247  * Opens a window. Requires a SFG_Window object created and attached
248  * to the freeglut structure. OpenGL context is created here.
249  */
250 void fgOpenWindow( SFG_Window* window, const char* title,
251                    int x, int y, int w, int h,
252                    GLboolean gameMode, GLboolean isSubWindow )
253 {
254 #if TARGET_HOST_UNIX_X11
255     XSetWindowAttributes winAttr;
256     XTextProperty textProperty;
257     XSizeHints sizeHints;
258     XWMHints wmHints;
259     unsigned long mask;
260
261     freeglut_assert_ready;
262
263     /*
264      * XXX fgChooseVisual() is a common part of all three.
265      * XXX With a little thought, we should be able to greatly
266      * XXX simplify this.
267      */
268     if ( !fgState.BuildingAMenu )
269       window->Window.VisualInfo = fgChooseVisual();
270     else if ( fgStructure.MenuContext )
271         window->Window.VisualInfo = fgChooseVisual();
272     else
273     {
274         unsigned int current_DisplayMode = fgState.DisplayMode ;
275         fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ;
276         window->Window.VisualInfo = fgChooseVisual();
277         fgState.DisplayMode = current_DisplayMode ;
278     }
279
280     if ( ! window->Window.VisualInfo )
281     {
282         /*
283          * The "fgChooseVisual" returned a null meaning that the visual
284          * context is not available.
285          * Try a couple of variations to see if they will work.
286          */
287         if ( ! ( fgState.DisplayMode & GLUT_DOUBLE ) )
288         {
289             fgState.DisplayMode |= GLUT_DOUBLE ;
290             window->Window.VisualInfo = fgChooseVisual();
291             fgState.DisplayMode &= ~GLUT_DOUBLE ;
292         }
293         
294         /*
295          * GLUT also checks for multi-sampling, but I don't see that
296          * anywhere else in FREEGLUT so I won't bother with it for the moment.
297          */
298     }
299
300     assert( window->Window.VisualInfo != NULL );
301
302     /*
303      * XXX HINT: the masks should be updated when adding/removing callbacks.
304      * XXX       This might speed up message processing. Is that true?
305      * XXX
306      * XXX A: Not appreciably, but it WILL make it easier to debug.
307      * XXX    Try tracing old GLUT and try tracing freeglut.  Old GLUT
308      * XXX    turns off events that it doesn't need and is a whole lot
309      * XXX    more pleasant to trace.  (Hint: Think mouse-motion!)
310      * XXX
311      * XXX    It may make a difference in networked environments or on
312      * XXX    some very slow systems, but I think that that is secondary
313      * XXX    to making debugging easier.
314      */
315     winAttr.event_mask        = StructureNotifyMask | SubstructureNotifyMask |
316         ExposureMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask |
317         KeyRelease | VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
318         PointerMotionMask | ButtonMotionMask;
319     winAttr.background_pixmap = None;
320     winAttr.background_pixel  = 0;
321     winAttr.border_pixel      = 0;
322
323     winAttr.colormap = XCreateColormap(
324         fgDisplay.Display, fgDisplay.RootWindow,
325         window->Window.VisualInfo->visual, AllocNone
326     );
327
328     mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
329
330     if ( fgState.BuildingAMenu )
331     {
332         winAttr.override_redirect = True;
333         mask |= CWOverrideRedirect;
334     }
335
336     window->Window.Handle = XCreateWindow(
337         fgDisplay.Display,
338         window->Parent == NULL ? fgDisplay.RootWindow :
339                                  window->Parent->Window.Handle,
340         x, y, w, h, 0,
341         window->Window.VisualInfo->depth, InputOutput,
342         window->Window.VisualInfo->visual, mask,
343         &winAttr
344     );
345
346     /*
347      * The GLX context creation, possibly trying the direct context rendering
348      *  or else use the current context if the user has so specified
349      */
350     if ( fgState.BuildingAMenu )
351     {
352         /*
353          * If there isn't already an OpenGL rendering context for menu
354          * windows, make one
355          */
356         if ( !fgStructure.MenuContext )
357         {
358             fgStructure.MenuContext =
359                 (SFG_MenuContext *)malloc ( sizeof(SFG_MenuContext) );
360             fgStructure.MenuContext->VisualInfo = window->Window.VisualInfo;
361             fgStructure.MenuContext->Context = glXCreateContext(
362                 fgDisplay.Display, fgStructure.MenuContext->VisualInfo,
363                 NULL, fgState.ForceDirectContext | fgState.TryDirectContext
364             );
365         }
366
367 /*      window->Window.Context = fgStructure.MenuContext->Context ; */
368         window->Window.Context = glXCreateContext(
369             fgDisplay.Display, window->Window.VisualInfo,
370             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
371         );
372     }
373     else if ( fgState.UseCurrentContext )
374     {
375       window->Window.Context = glXGetCurrentContext();
376
377       if ( ! window->Window.Context )
378         window->Window.Context = glXCreateContext(
379             fgDisplay.Display, window->Window.VisualInfo,
380             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
381         );
382     }
383     else
384         window->Window.Context = glXCreateContext(
385             fgDisplay.Display, window->Window.VisualInfo,
386             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
387         );
388
389     if( fgState.ForceDirectContext &&
390         !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
391         fgError( "unable to force direct context rendering for window '%s'",
392                  title );
393
394     glXMakeCurrent(
395         fgDisplay.Display,
396         window->Window.Handle,
397         window->Window.Context
398     );
399
400     /*
401      * XXX Assume the new window is visible by default
402      * XXX Is this a  safe assumption?
403      */
404     window->State.Visible = GL_TRUE;
405
406     sizeHints.flags = 0;
407     if ( fgState.Position.Use )
408         sizeHints.flags |= USPosition;
409     if ( fgState.Size.Use )
410         sizeHints.flags |= USSize;
411
412     /*
413      * Fill in the size hints values now (the x, y, width and height
414      * settings are obsolote, are there any more WMs that support them?)
415      * Unless the X servers actually stop supporting these, we should
416      * continue to fill them in.  It is *not* our place to tell the user
417      * that they should replace a window manager that they like, and which
418      * works, just because *we* think that it's not "modern" enough.
419      */
420     sizeHints.x      = x;
421     sizeHints.y      = y;
422     sizeHints.width  = w;
423     sizeHints.height = h;
424
425     wmHints.flags = StateHint;
426     wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
427
428     /*
429      * Prepare the window and iconified window names...
430      */
431     XStringListToTextProperty( (char **) &title, 1, &textProperty );
432
433     XSetWMProperties(
434         fgDisplay.Display,
435         window->Window.Handle,
436         &textProperty,
437         &textProperty,
438         0,
439         0,
440         &sizeHints,
441         &wmHints,
442         NULL
443     );
444     XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
445                      &fgDisplay.DeleteWindow, 1 );
446     XMapWindow( fgDisplay.Display, window->Window.Handle );
447
448 #elif TARGET_HOST_WIN32
449
450     WNDCLASS wc;
451     int flags;
452     ATOM atom;
453
454     freeglut_assert_ready;
455     
456     /*
457      * Grab the window class we have registered on glutInit():
458      */
459     atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
460     assert( atom != 0 );
461     
462     if( gameMode )
463     {
464         assert( window->Parent == NULL );
465
466         /*
467          * Set the window creation flags appropriately to make the window
468          * entirely visible:
469          */
470         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
471     }
472     else
473     {
474         if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
475         {
476             /*
477              * Update the window dimensions, taking account of window
478              * decorations.  "freeglut" is to create the window with the
479              * outside of its border at (x,y) and with dimensions (w,h).
480              */
481             w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
482             h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
483                 GetSystemMetrics( SM_CYCAPTION );
484         }
485
486         if( ! fgState.Position.Use )
487         {
488             x = CW_USEDEFAULT;
489             y = CW_USEDEFAULT;
490         }
491         if( ! fgState.Size.Use )
492         {
493             w = CW_USEDEFAULT;
494             h = CW_USEDEFAULT;
495         }
496
497         /*
498          * There's a small difference between creating the top, child and
499          * game mode windows
500          */
501         flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
502
503         if ( window->IsMenu )
504             flags |= WS_POPUP ;
505         else if( window->Parent == NULL )
506             flags |= WS_OVERLAPPEDWINDOW;
507         else
508             flags |= WS_CHILD;
509     }
510
511     window->Window.Handle = CreateWindow( 
512         "FREEGLUT",
513         title,
514         flags,
515         x, y, w, h,
516         (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
517         (HMENU) NULL,
518         fgDisplay.Instance,
519         (LPVOID) window
520     );
521     if( !( window->Window.Handle ) )
522         fgError( "Failed to create a window (%s)!", title );
523
524     ShowWindow( window->Window.Handle,
525                 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
526     UpdateWindow( window->Window.Handle );
527     ShowCursor( TRUE );  /* XXX Old comments say "hide cusror"! */
528
529 #endif
530
531     window->Window.DoubleBuffered =
532         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0 ;
533
534     if ( ! window->Window.DoubleBuffered )
535     {
536         glDrawBuffer ( GL_FRONT ) ;
537         glReadBuffer ( GL_FRONT ) ;
538     }
539     fgSetWindow( window );
540 }
541
542 /*
543  * Closes a window, destroying the frame and OpenGL context
544  */
545 void fgCloseWindow( SFG_Window* window )
546 {
547     freeglut_assert_ready;
548
549 #if TARGET_HOST_UNIX_X11
550
551     glXDestroyContext( fgDisplay.Display, window->Window.Context );
552     XDestroyWindow( fgDisplay.Display, window->Window.Handle );
553     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
554
555 #elif TARGET_HOST_WIN32
556
557     SendMessage( 
558         window->Window.Handle,
559         WM_CLOSE,
560         0,
561         0
562     );
563
564 #endif
565 }
566
567
568 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
569
570 /*
571  * Creates a new top-level freeglut window
572  */
573 int FGAPIENTRY glutCreateWindow( const char* title )
574 {
575     return fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
576                            fgState.Size.X, fgState.Size.Y, GL_FALSE )->ID;
577 }
578
579 /*
580  * This function creates a sub window.
581  */
582 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
583 {
584     SFG_Window* window = NULL;
585     SFG_Window* parent = NULL;
586
587     freeglut_assert_ready;
588     parent = fgWindowByID( parentID );
589     freeglut_return_val_if_fail( parent != NULL, 0 );
590     window = fgCreateWindow( parent, "", x, y, w, h, GL_FALSE );
591     return window->ID;
592 }
593
594 /*
595  * Destroys a window and all of its subwindows
596  */
597 void FGAPIENTRY glutDestroyWindow( int windowID )
598 {
599     SFG_Window* window = fgWindowByID( windowID );
600     freeglut_return_if_fail( window != NULL );
601     {
602         fgExecutionState ExecState = fgState.ExecState;
603         fgAddToWindowDestroyList( window, GL_TRUE );
604         fgState.ExecState = ExecState;
605     }
606 }
607
608 /*
609  * This function selects the current window
610  */
611 void FGAPIENTRY glutSetWindow( int ID )
612 {
613     SFG_Window* window = NULL;
614
615     freeglut_assert_ready;
616     if( fgStructure.Window != NULL )
617         if( fgStructure.Window->ID == ID )
618             return;
619
620     window = fgWindowByID( ID );
621     if( window == NULL )
622     {
623         fgWarning( "glutSetWindow(): window ID %i not found!", ID );
624         return;
625     }
626
627     fgSetWindow( window ) ;
628 }
629
630 /*
631  * This function returns the ID number of the current window, 0 if none exists
632  */
633 int FGAPIENTRY glutGetWindow( void )
634 {
635     freeglut_assert_ready;
636     if( fgStructure.Window == NULL )
637         return 0;
638     return fgStructure.Window->ID;
639 }
640
641 /*
642  * This function makes the current window visible
643  */
644 void FGAPIENTRY glutShowWindow( void )
645 {
646     freeglut_assert_ready;
647     freeglut_assert_window;
648
649 #if TARGET_HOST_UNIX_X11
650
651     XMapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
652     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
653
654 #elif TARGET_HOST_WIN32
655
656     ShowWindow( fgStructure.Window->Window.Handle, SW_SHOW );
657
658 #endif
659
660     fgStructure.Window->State.Redisplay = GL_TRUE;
661 }
662
663 /*
664  * This function hides the current window
665  */
666 void FGAPIENTRY glutHideWindow( void )
667 {
668     freeglut_assert_ready;
669     freeglut_assert_window;
670
671 #if TARGET_HOST_UNIX_X11
672
673     if( fgStructure.Window->Parent == NULL )
674         XWithdrawWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
675                          fgDisplay.Screen );
676     else
677         XUnmapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
678     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
679
680 #elif TARGET_HOST_WIN32
681
682     ShowWindow( fgStructure.Window->Window.Handle, SW_HIDE );
683
684 #endif
685
686     fgStructure.Window->State.Redisplay = GL_FALSE;
687 }
688
689 /*
690  * Iconify the current window (top-level windows only)
691  */
692 void FGAPIENTRY glutIconifyWindow( void )
693 {
694     freeglut_assert_ready;
695     freeglut_assert_window;
696
697 #if TARGET_HOST_UNIX_X11
698
699     XIconifyWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
700                     fgDisplay.Screen );
701     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
702
703 #elif TARGET_HOST_WIN32
704
705     ShowWindow( fgStructure.Window->Window.Handle, SW_MINIMIZE );
706
707 #endif
708
709     fgStructure.Window->State.Redisplay = GL_FALSE;
710 }
711
712 /*
713  * Set the current window's title
714  */
715 void FGAPIENTRY glutSetWindowTitle( const char* title )
716 {
717     freeglut_assert_ready;
718     freeglut_assert_window;
719     if( fgStructure.Window->Parent != NULL )
720         return;
721
722 #if TARGET_HOST_UNIX_X11
723
724     {
725         XTextProperty text;
726         
727         text.value = (unsigned char *) title;
728         text.encoding = XA_STRING;
729         text.format = 8;
730         text.nitems = strlen( title );
731         
732         XSetWMName(
733             fgDisplay.Display,
734             fgStructure.Window->Window.Handle,
735             &text
736         );
737         
738         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
739     }
740
741 #elif TARGET_HOST_WIN32
742
743     SetWindowText( fgStructure.Window->Window.Handle, title );
744
745 #endif
746
747 }
748
749 /*
750  * Set the current window's iconified title
751  */
752 void FGAPIENTRY glutSetIconTitle( const char* title )
753 {
754     freeglut_assert_ready;
755     freeglut_assert_window;
756
757     if( fgStructure.Window->Parent != NULL )
758         return;
759
760 #if TARGET_HOST_UNIX_X11
761
762     {
763         XTextProperty text;
764         
765         text.value = (unsigned char *) title;
766         text.encoding = XA_STRING;
767         text.format = 8;
768         text.nitems = strlen( title );
769
770         XSetWMIconName(
771             fgDisplay.Display,
772             fgStructure.Window->Window.Handle,
773             &text
774         );
775
776         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
777     }
778
779 #elif TARGET_HOST_WIN32
780
781     SetWindowText( fgStructure.Window->Window.Handle, title );
782
783 #endif
784
785 }
786
787 /*
788  * Change the current window's size
789  */
790 void FGAPIENTRY glutReshapeWindow( int width, int height )
791 {
792     freeglut_assert_ready;
793     freeglut_assert_window;
794
795 #if TARGET_HOST_UNIX_X11
796
797     XResizeWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
798                    width, height );
799     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
800     /*
801      * XXX REALLY shouldn't be done.  GLUT docs state that this
802      * XXX isn't even processed immediately, but rather waits
803      * XXX for return to the mainloop.  "This allows multiple
804      * XXX glutReshapeWindow, glutPositionWindow, and glutFullScreen
805      * XXX requests to the same window to be coalesced."  (This is
806      * XXX having some deleterious effect on a sample program of mine.)
807      * XXX Not only does GLUT not flush at this point, GLUT doesn't even
808      * XXX *do* the reshape at this point!  We should probably rip this
809      * XXX out and do what GLUT promises.  It would be more efficient, and
810      * XXX might be more compatible.
811      */
812
813 #elif TARGET_HOST_WIN32
814
815     {
816         RECT winRect;
817         int x, y;
818
819         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
820         x = winRect.left;
821         y = winRect.top;
822
823         if ( fgStructure.Window->Parent == NULL )
824         {
825             /*
826              * Adjust the size of the window to allow for the size of the
827              * frame, if we are not a menu
828              */
829             if ( ! fgStructure.Window->IsMenu )
830             {
831                 width += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
832                 height += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 +
833                     GetSystemMetrics( SM_CYCAPTION );
834             }
835         }
836         else
837         {
838             GetWindowRect( fgStructure.Window->Parent->Window.Handle,
839                            &winRect );
840             x -= winRect.left + GetSystemMetrics( SM_CXSIZEFRAME );
841             y -= winRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) +
842                 GetSystemMetrics( SM_CYCAPTION );
843         }
844
845         MoveWindow(
846             fgStructure.Window->Window.Handle,
847             x,
848             y,
849             width,
850             height,
851             TRUE
852         );
853     }
854
855 #endif
856
857 }
858
859 /*
860  * Change the current window's position
861  */
862 void FGAPIENTRY glutPositionWindow( int x, int y )
863 {
864     freeglut_assert_ready;
865     freeglut_assert_window;
866
867 #if TARGET_HOST_UNIX_X11
868
869     XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, x, y );
870     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
871
872 #elif TARGET_HOST_WIN32
873
874     {
875         RECT winRect;
876         
877         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
878         MoveWindow(
879             fgStructure.Window->Window.Handle,
880             x,
881             y,
882             winRect.right - winRect.left,
883             winRect.bottom - winRect.top,
884             TRUE
885         );
886     }
887
888 #endif
889
890 }
891
892 /*
893  * Lowers the current window (by Z order change)
894  */
895 void FGAPIENTRY glutPushWindow( void )
896 {
897     freeglut_assert_ready;
898     freeglut_assert_window;
899
900 #if TARGET_HOST_UNIX_X11
901
902     XLowerWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
903
904 #elif TARGET_HOST_WIN32
905
906     SetWindowPos(
907         fgStructure.Window->Window.Handle,
908         HWND_BOTTOM,
909         0, 0, 0, 0,
910         SWP_NOSIZE | SWP_NOMOVE
911     );
912
913 #endif
914
915 }
916
917 /*
918  * Raises the current window (by Z order change)
919  */
920 void FGAPIENTRY glutPopWindow( void )
921 {
922     freeglut_assert_ready;
923     freeglut_assert_window;
924
925 #if TARGET_HOST_UNIX_X11
926
927     XRaiseWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
928
929 #elif TARGET_HOST_WIN32
930
931     SetWindowPos(
932         fgStructure.Window->Window.Handle,
933         HWND_TOP,
934         0, 0, 0, 0,
935         SWP_NOSIZE | SWP_NOMOVE
936     );
937
938 #endif
939
940 }
941
942 /*
943  * Resize the current window so that it fits the whole screen
944  */
945 void FGAPIENTRY glutFullScreen( void )
946 {
947     freeglut_assert_ready;
948     freeglut_assert_window;
949
950 #if TARGET_HOST_UNIX_X11
951     {
952         int x, y;
953         Window w;
954
955         XMoveResizeWindow(
956             fgDisplay.Display,
957             fgStructure.Window->Window.Handle,
958             0, 0,
959             fgDisplay.ScreenWidth,
960             fgDisplay.ScreenHeight
961         );
962
963         XFlush( fgDisplay.Display ); /* This is needed */
964
965         XTranslateCoordinates(
966             fgDisplay.Display,
967             fgStructure.Window->Window.Handle,
968             fgDisplay.RootWindow,
969             0, 0, &x, &y, &w
970         );
971
972         if (x || y)
973         {
974             XMoveWindow(
975                 fgDisplay.Display,
976                 fgStructure.Window->Window.Handle,
977                 -x, -y
978             );
979             XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
980         }
981     }
982 #elif TARGET_HOST_WIN32
983     MoveWindow(
984         fgStructure.Window->Window.Handle,
985         0, 0,
986         fgDisplay.ScreenWidth,
987         fgDisplay.ScreenHeight,
988         TRUE
989     );
990 #endif
991 }
992
993 /*
994  * A.Donev: Set and retrieve the window's user data
995  */
996 void* FGAPIENTRY glutGetWindowData( void )
997 {
998     return fgStructure.Window->UserData;
999 }
1000
1001 void FGAPIENTRY glutSetWindowData(void* data)
1002 {
1003     fgStructure.Window->UserData=data;
1004 }
1005
1006 /*** END OF FILE ***/