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