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