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