Putting in Jocelyn Frechot's X11 visual context changes. THIS WILL BREAK THE BUILD...
[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 defined(_WIN32_WCE)
32 #   include <Aygshell.h>
33 #   ifdef FREEGLUT_LIB_PRAGMAS
34 #       pragma comment( lib, "Aygshell.lib" )
35 #   endif
36
37 static wchar_t* fghWstrFromStr(const char* str)
38 {
39     int i,len=strlen(str);
40     wchar_t* wstr = (wchar_t*)malloc(2*len+2);
41     for(i=0; i<len; i++)
42         wstr[i] = str[i];
43     wstr[len] = 0;
44     return wstr;
45 }
46
47 #endif /* defined(_WIN32_WCE) */
48
49 /*
50  * TODO BEFORE THE STABLE RELEASE:
51  *
52  *  fgChooseFBConfig()      -- OK, but what about glutInitDisplayString()?
53  *  fgSetupPixelFormat      -- ignores the display mode settings
54  *  fgOpenWindow()          -- check the Win32 version, -iconic handling!
55  *  fgCloseWindow()         -- check the Win32 version
56  *  glutCreateWindow()      -- Check when default position and size is {-1,-1}
57  *  glutCreateSubWindow()   -- Check when default position and size is {-1,-1}
58  *  glutDestroyWindow()     -- check the Win32 version
59  *  glutSetWindow()         -- check the Win32 version
60  *  glutGetWindow()         -- OK
61  *  glutSetWindowTitle()    -- check the Win32 version
62  *  glutSetIconTitle()      -- check the Win32 version
63  *  glutShowWindow()        -- check the Win32 version
64  *  glutHideWindow()        -- check the Win32 version
65  *  glutIconifyWindow()     -- check the Win32 version
66  *  glutReshapeWindow()     -- check the Win32 version
67  *  glutPositionWindow()    -- check the Win32 version
68  *  glutPushWindow()        -- check the Win32 version
69  *  glutPopWindow()         -- check the Win32 version
70  */
71
72 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
73
74 /*
75  * Chooses a visual basing on the current display mode settings
76  */
77 #if TARGET_HOST_POSIX_X11
78
79 GLXFBConfig* fgChooseFBConfig( void )
80 {
81     GLboolean wantIndexedMode = GL_FALSE;
82     int attributes[ 32 ];
83     int where = 0;
84
85     /* First we have to process the display mode settings... */
86 /*
87  * XXX Why is there a semi-colon in this #define?  The code
88  * XXX that uses the macro seems to always add more semicolons...
89  */
90 #define ATTRIB(a) attributes[where++]=a;
91 #define ATTRIB_VAL(a,v) {ATTRIB(a); ATTRIB(v);}
92
93     if( fgState.DisplayMode & GLUT_INDEX )
94     {
95         ATTRIB_VAL( GLX_BUFFER_SIZE, 8 );
96         /*  Buffer size is selected later.  */
97
98         ATTRIB_VAL( GLX_RENDER_TYPE, GLX_COLOR_INDEX_BIT );
99         wantIndexedMode = GL_TRUE;
100     }
101     else
102     {
103         ATTRIB_VAL( GLX_RED_SIZE,   1 );
104         ATTRIB_VAL( GLX_GREEN_SIZE, 1 );
105         ATTRIB_VAL( GLX_BLUE_SIZE,  1 );
106         if( fgState.DisplayMode & GLUT_ALPHA )
107             ATTRIB_VAL( GLX_ALPHA_SIZE, 1 );
108     }
109
110     if( fgState.DisplayMode & GLUT_DOUBLE )
111         ATTRIB_VAL( GLX_DOUBLEBUFFER, True );
112
113     if( fgState.DisplayMode & GLUT_STEREO )
114         ATTRIB_VAL( GLX_STEREO, True );
115
116     if( fgState.DisplayMode & GLUT_DEPTH )
117         ATTRIB_VAL( GLX_DEPTH_SIZE, 1 );
118
119     if( fgState.DisplayMode & GLUT_STENCIL )
120         ATTRIB_VAL( GLX_STENCIL_SIZE, 1 );
121
122     if( fgState.DisplayMode & GLUT_ACCUM )
123     {
124         ATTRIB_VAL( GLX_ACCUM_RED_SIZE,   1 );
125         ATTRIB_VAL( GLX_ACCUM_GREEN_SIZE, 1 );
126         ATTRIB_VAL( GLX_ACCUM_BLUE_SIZE,  1 );
127         if( fgState.DisplayMode & GLUT_ALPHA )
128             ATTRIB_VAL( GLX_ACCUM_ALPHA_SIZE, 1 );
129     }
130
131     if( fgState.DisplayMode & GLUT_AUX1 )
132         ATTRIB_VAL( GLX_AUX_BUFFERS, 1 );
133     if( fgState.DisplayMode & GLUT_AUX2 )
134         ATTRIB_VAL( GLX_AUX_BUFFERS, 2 );
135     if( fgState.DisplayMode & GLUT_AUX3 )
136         ATTRIB_VAL( GLX_AUX_BUFFERS, 3 );
137     if( fgState.DisplayMode & GLUT_AUX4 )
138         ATTRIB_VAL( GLX_AUX_BUFFERS, 4 );
139     if ( fgState.DisplayMode & GLUT_MULTISAMPLE )
140     {
141         ATTRIB_VAL( GLX_SAMPLE_BUFFERS, 1 );
142     }
143
144
145     /* Push a null at the end of the list */
146     ATTRIB( None );
147
148     {
149         GLXFBConfig * fbconfigArray;  /*  Array of FBConfigs  */
150         GLXFBConfig * fbconfig;       /*  The FBConfig we want  */
151         int fbconfigArraySize;        /*  Number of FBConfigs in the array  */
152
153
154         /*  Get all FBConfigs that match "attributes".  */
155         fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
156                                            fgDisplay.Screen,
157                                            attributes,
158                                            &fbconfigArraySize );
159
160         if (fbconfigArray != NULL)
161         {
162             int result;  /* Returned by glXGetFBConfigAttrib, not checked. */
163
164
165             if( wantIndexedMode )
166             {
167                 /*
168                  * In index mode, we want the largest buffer size, i.e. visual
169                  * depth.  Here, FBConfigs are sorted by increasing buffer size
170                  * first, so FBConfigs with the largest size come last.
171                  */
172
173                 int bufferSizeMin, bufferSizeMax;
174
175                 /*  Get bufferSizeMin.  */
176                 result =
177                   glXGetFBConfigAttrib( fgDisplay.Display,
178                                         fbconfigArray[0],
179                                         GLX_BUFFER_SIZE,
180                                         &bufferSizeMin );
181                 /*  Get bufferSizeMax.  */
182                 result =
183                   glXGetFBConfigAttrib( fgDisplay.Display,
184                                         fbconfigArray[fbconfigArraySize - 1],
185                                         GLX_BUFFER_SIZE,
186                                         &bufferSizeMax );
187
188                 if (bufferSizeMax > bufferSizeMin)
189                 {
190                     /* 
191                      * Free and reallocate fbconfigArray, keeping only FBConfigs
192                      * with the largest buffer size.
193                      */
194                     XFree(fbconfigArray);
195
196                     /*  Add buffer size token at the end of the list.  */
197                     where--;
198                     ATTRIB_VAL( GLX_BUFFER_SIZE, bufferSizeMax );
199                     ATTRIB( None );
200
201                     fbconfigArray = glXChooseFBConfig( fgDisplay.Display,
202                                                        fgDisplay.Screen,
203                                                        attributes,
204                                                        &fbconfigArraySize );
205                 }
206             }
207
208             /*
209              * We now have an array of FBConfigs, the first one being the "best"
210              * one.  So we should return only this FBConfig:
211              *
212              * int fbconfigXID;
213              *
214              *  - pick the XID of the FBConfig we want
215              * result = glXGetFBConfigAttrib( fgDisplay.Display,
216              *                                fbconfigArray[0],
217              *                                GLX_FBCONFIG_ID,
218              *                                &fbconfigXID );
219              *
220              * - free the array
221              * XFree(fbconfigArray);
222              *
223              * - reset "attributes" with the XID
224              * where = 0;
225              * ATTRIB_VAL( GLX_FBCONFIG_ID, fbconfigXID );
226              * ATTRIB( None );
227              *
228              * - get our FBConfig only
229              * fbconfig = glXChooseFBConfig( fgDisplay.Display,
230              *                               fgDisplay.Screen,
231              *                               attributes,
232              *                               &fbconfigArraySize );
233              *
234              * However, for some configurations (for instance multisampling with
235              * Mesa 6.5.2 and ATI drivers), this does not work:
236              * glXChooseFBConfig returns NULL, whereas fbconfigXID is a valid
237              * XID.  Further investigation is needed.
238              *
239              * So, for now, we return the whole array of FBConfigs.  This should
240              * not produce any side effects elsewhere.
241              */
242             fbconfig = fbconfigArray;
243         }
244         else
245         {
246            fbconfig = NULL;
247         }
248
249         return fbconfig;
250     }
251 }
252 #endif
253
254 /*
255  * Setup the pixel format for a Win32 window
256  */
257 #if TARGET_HOST_MS_WINDOWS
258 /* The following include file is available from SGI but is not standard:
259  *   #include <GL/wglext.h>
260  * So we copy the necessary parts out of it.
261  * XXX: should local definitions for extensions be put in a separate include file?
262  */
263 typedef const char * (WINAPI * PFNWGLGETEXTENSIONSSTRINGARBPROC) (HDC hdc);
264
265 typedef BOOL (WINAPI * PFNWGLCHOOSEPIXELFORMATARBPROC) (HDC hdc, const int *piAttribIList, const FLOAT *pfAttribFList, UINT nMaxFormats, int *piFormats, UINT *nNumFormats);
266
267 #define WGL_DRAW_TO_WINDOW_ARB         0x2001
268 #define WGL_ACCELERATION_ARB           0x2003
269 #define WGL_SUPPORT_OPENGL_ARB         0x2010
270 #define WGL_DOUBLE_BUFFER_ARB          0x2011
271 #define WGL_COLOR_BITS_ARB             0x2014
272 #define WGL_ALPHA_BITS_ARB             0x201B
273 #define WGL_DEPTH_BITS_ARB             0x2022
274 #define WGL_STENCIL_BITS_ARB           0x2023
275 #define WGL_FULL_ACCELERATION_ARB      0x2027
276
277 #define WGL_SAMPLE_BUFFERS_ARB         0x2041
278 #define WGL_SAMPLES_ARB                0x2042
279
280
281 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
282                               unsigned char layer_type )
283 {
284 #if defined(_WIN32_WCE)
285     return GL_TRUE;
286 #else
287     PIXELFORMATDESCRIPTOR* ppfd, pfd;
288     int flags, pixelformat;
289
290     freeglut_return_val_if_fail( window != NULL, 0 );
291     flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
292     if( fgState.DisplayMode & GLUT_DOUBLE )
293         flags |= PFD_DOUBLEBUFFER;
294
295     if( fgState.DisplayMode & GLUT_STEREO )
296         flags |= PFD_STEREO;
297
298 #if defined(_MSC_VER)
299 #pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
300 #endif
301
302     /* Specify which pixel format do we opt for... */
303     pfd.nSize           = sizeof(PIXELFORMATDESCRIPTOR);
304     pfd.nVersion        = 1;
305     pfd.dwFlags         = flags;
306
307     if( fgState.DisplayMode & GLUT_INDEX )
308     {
309         pfd.iPixelType = PFD_TYPE_COLORINDEX;
310         pfd.cRedBits                = 0;
311         pfd.cGreenBits            = 0;
312         pfd.cBlueBits             = 0;
313         pfd.cAlphaBits            = 0;
314     }
315     else
316     {
317         pfd.iPixelType      = PFD_TYPE_RGBA;
318         pfd.cRedBits                = 8;
319         pfd.cGreenBits            = 8;
320         pfd.cBlueBits             = 8;
321         if ( fgState.DisplayMode & GLUT_ALPHA )
322             pfd.cAlphaBits            = 8;
323         else
324             pfd.cAlphaBits            = 0;
325     }
326
327     pfd.cColorBits      = 24;
328     pfd.cRedShift       = 0;
329     pfd.cGreenShift     = 0;
330     pfd.cBlueShift      = 0;
331     pfd.cAlphaShift     = 0;
332     pfd.cAccumBits      = 0;
333     pfd.cAccumRedBits   = 0;
334     pfd.cAccumGreenBits = 0;
335     pfd.cAccumBlueBits  = 0;
336     pfd.cAccumAlphaBits = 0;
337 #if 0
338     pfd.cDepthBits      = 32;
339     pfd.cStencilBits    = 0;
340 #else
341     pfd.cDepthBits      = 24;
342     pfd.cStencilBits    = 8;
343 #endif
344     if( fgState.DisplayMode & GLUT_AUX4 )
345         pfd.cAuxBuffers = 4;
346     else if( fgState.DisplayMode & GLUT_AUX3 )
347         pfd.cAuxBuffers = 3;
348     else if( fgState.DisplayMode & GLUT_AUX2 )
349         pfd.cAuxBuffers = 2;
350     else if( fgState.DisplayMode & GLUT_AUX1 )
351         pfd.cAuxBuffers = 1;
352     else
353         pfd.cAuxBuffers = 0;
354
355     pfd.iLayerType      = layer_type;
356     pfd.bReserved       = 0;
357     pfd.dwLayerMask     = 0;
358     pfd.dwVisibleMask   = 0;
359     pfd.dwDamageMask    = 0;
360
361     pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
362     ppfd = &pfd;
363
364     pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
365
366     /* windows hack for multismapling */
367     if (fgState.DisplayMode&GLUT_MULTISAMPLE)
368     {        
369         PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetEntensionsStringARB=NULL;
370         HGLRC rc, rc_before=wglGetCurrentContext();
371         HWND hWnd;
372         HDC hDC, hDC_before=wglGetCurrentDC();
373         WNDCLASS wndCls;
374         ATOM atom;
375
376         /* create a dummy window */
377         ZeroMemory(&wndCls, sizeof(wndCls));
378                 wndCls.lpfnWndProc        = DefWindowProc;
379                 wndCls.hInstance            = fgDisplay.Instance;
380                 wndCls.style                    = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
381                 wndCls.lpszClassName    = _T("FREEGLUT_dummy");
382         atom = RegisterClass( &wndCls );
383
384         hWnd=CreateWindow((LPCSTR)atom, _T(""), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW , 0,0,0,0, 0, 0, fgDisplay.Instance, 0 );
385         hDC=GetDC(hWnd);
386         SetPixelFormat( hDC, pixelformat, ppfd );
387         
388         rc = wglCreateContext( hDC );
389         wglMakeCurrent(hDC, rc);
390         
391         wglGetEntensionsStringARB=(PFNWGLGETEXTENSIONSSTRINGARBPROC)wglGetProcAddress("wglGetExtensionsStringARB");
392         if (wglGetEntensionsStringARB)
393         {
394             const char * pWglExtString=wglGetEntensionsStringARB(hDC);
395             if (pWglExtString)
396             {
397                 if (strstr(pWglExtString, "WGL_ARB_multisample"))
398                 {
399                     int pAttributes[100];
400                     int iCounter=0;
401                     int iPixelFormat;
402                     BOOL bValid;
403                     float fAttributes[] = {0,0};
404                     UINT numFormats;
405                     PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARBProc=NULL;
406
407                     wglChoosePixelFormatARBProc=(PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
408                     if ( wglChoosePixelFormatARBProc )
409                     {
410                         pAttributes[iCounter++]=WGL_DRAW_TO_WINDOW_ARB;        pAttributes[iCounter++]=GL_TRUE;
411                         pAttributes[iCounter++]=WGL_SUPPORT_OPENGL_ARB;        pAttributes[iCounter++]=GL_TRUE;
412                         pAttributes[iCounter++]=WGL_ACCELERATION_ARB;        pAttributes[iCounter++]=WGL_FULL_ACCELERATION_ARB;
413
414                         pAttributes[iCounter++]=WGL_COLOR_BITS_ARB;            pAttributes[iCounter++]=pfd.cColorBits ;
415                         pAttributes[iCounter++]=WGL_ALPHA_BITS_ARB;            pAttributes[iCounter++]=pfd.cAlphaBits;
416                         pAttributes[iCounter++]=WGL_DEPTH_BITS_ARB;            pAttributes[iCounter++]=pfd.cDepthBits;
417                         pAttributes[iCounter++]=WGL_STENCIL_BITS_ARB;        pAttributes[iCounter++]=pfd.cStencilBits;
418
419                         pAttributes[iCounter++]=WGL_DOUBLE_BUFFER_ARB;        pAttributes[iCounter++]=(fgState.DisplayMode & GLUT_DOUBLE)!=0;
420                         pAttributes[iCounter++]=WGL_SAMPLE_BUFFERS_ARB;        pAttributes[iCounter++]=GL_TRUE;
421                         pAttributes[iCounter++]=WGL_SAMPLES_ARB;            pAttributes[iCounter++]=4;
422                         pAttributes[iCounter++]=0;                            pAttributes[iCounter++]=0;    /* terminator */
423
424                         bValid = wglChoosePixelFormatARBProc(window->Window.Device,pAttributes,fAttributes,1,&iPixelFormat,&numFormats);
425
426                         if (bValid && numFormats>0)
427                             pixelformat=iPixelFormat;
428                     }
429                 }
430                 wglMakeCurrent( hDC_before, rc_before);
431                 wglDeleteContext(rc);
432                 ReleaseDC(hWnd, hDC);
433                 DestroyWindow(hWnd);
434                 UnregisterClass(_T("FREEGLUT_dummy"), fgDisplay.Instance);
435             }
436         }
437     }
438
439     if( pixelformat == 0 )
440         return GL_FALSE;
441
442     if( checkOnly )
443         return GL_TRUE;
444     return SetPixelFormat( window->Window.Device, pixelformat, ppfd );
445 #endif /* defined(_WIN32_WCE) */
446 }
447 #endif /* TARGET_HOST_MS_WINDOWS */
448
449 /*
450  * Sets the OpenGL context and the fgStructure "Current Window" pointer to
451  * the window structure passed in.
452  */
453 void fgSetWindow ( SFG_Window *window )
454 {
455 #if TARGET_HOST_POSIX_X11
456     if ( window )
457         glXMakeContextCurrent(
458             fgDisplay.Display,
459             window->Window.Handle,
460             window->Window.Handle,
461             window->Window.Context
462         );
463 #elif TARGET_HOST_MS_WINDOWS
464     if( fgStructure.CurrentWindow )
465         ReleaseDC( fgStructure.CurrentWindow->Window.Handle,
466                    fgStructure.CurrentWindow->Window.Device );
467
468     if ( window )
469     {
470         window->Window.Device = GetDC( window->Window.Handle );
471         wglMakeCurrent(
472             window->Window.Device,
473             window->Window.Context
474         );
475     }
476 #endif
477     fgStructure.CurrentWindow = window;
478 }
479
480
481 /*
482  * Opens a window. Requires a SFG_Window object created and attached
483  * to the freeglut structure. OpenGL context is created here.
484  */
485 void fgOpenWindow( SFG_Window* window, const char* title,
486                    GLboolean positionUse, int x, int y,
487                    GLboolean sizeUse, int w, int h,
488                    GLboolean gameMode, GLboolean isSubWindow )
489 {
490 #if TARGET_HOST_POSIX_X11
491     XVisualInfo * visualInfo;
492     XSetWindowAttributes winAttr;
493     XTextProperty textProperty;
494     XSizeHints sizeHints;
495     XWMHints wmHints;
496     unsigned long mask;
497     int renderType;  /*  GLX_RGBA_TYPE or GLX_COLOR_INDEX_TYPE  */
498     unsigned int current_DisplayMode = fgState.DisplayMode ;
499
500     /* Save the display mode if we are creating a menu window */
501     if( window->IsMenu && ( ! fgStructure.MenuContext ) )
502         fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB ;
503
504     window->Window.FBConfig = fgChooseFBConfig( );
505
506     if( window->IsMenu && ( ! fgStructure.MenuContext ) )
507         fgState.DisplayMode = current_DisplayMode ;
508
509     if( ! window->Window.FBConfig )
510     {
511         /*
512          * The "fgChooseFBConfig" returned a null meaning that the visual
513          * context is not available.
514          * Try a couple of variations to see if they will work.
515          */
516         if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
517         {
518             fgState.DisplayMode |= GLUT_DOUBLE ;
519             window->Window.FBConfig = fgChooseFBConfig( );
520             fgState.DisplayMode &= ~GLUT_DOUBLE;
521         }
522
523         if( fgState.DisplayMode & GLUT_MULTISAMPLE )
524         {
525             fgState.DisplayMode &= ~GLUT_MULTISAMPLE ;
526             window->Window.FBConfig = fgChooseFBConfig( );
527             fgState.DisplayMode |= GLUT_MULTISAMPLE;
528         }
529     }
530
531     FREEGLUT_INTERNAL_ERROR_EXIT( window->Window.FBConfig != NULL,
532                                   "FBConfig with necessary capabilities not found", "fgOpenWindow" );
533
534     /*  Get the X visual.  */
535     visualInfo = glXGetVisualFromFBConfig( fgDisplay.Display,
536                                            *(window->Window.FBConfig) );
537
538     /*
539      * XXX HINT: the masks should be updated when adding/removing callbacks.
540      * XXX       This might speed up message processing. Is that true?
541      * XXX
542      * XXX A: Not appreciably, but it WILL make it easier to debug.
543      * XXX    Try tracing old GLUT and try tracing freeglut.  Old GLUT
544      * XXX    turns off events that it doesn't need and is a whole lot
545      * XXX    more pleasant to trace.  (Think mouse-motion!  Tons of
546      * XXX    ``bonus'' GUI events stream in.)
547      */
548     winAttr.event_mask        =
549         StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
550         ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
551         VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
552         PointerMotionMask | ButtonMotionMask;
553     winAttr.background_pixmap = None;
554     winAttr.background_pixel  = 0;
555     winAttr.border_pixel      = 0;
556
557     winAttr.colormap = XCreateColormap(
558         fgDisplay.Display, fgDisplay.RootWindow,
559         visualInfo->visual, AllocNone
560     );
561
562     mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
563
564     if( window->IsMenu || ( gameMode == GL_TRUE ) )
565     {
566         winAttr.override_redirect = True;
567         mask |= CWOverrideRedirect;
568     }
569
570     if( ! positionUse )
571         x = y = -1; /* default window position */
572     if( ! sizeUse )
573         w = h = 300; /* default window size */
574
575     window->Window.Handle = XCreateWindow(
576         fgDisplay.Display,
577         window->Parent == NULL ? fgDisplay.RootWindow :
578         window->Parent->Window.Handle,
579         x, y, w, h, 0,
580         visualInfo->depth, InputOutput,
581         visualInfo->visual, mask,
582         &winAttr
583     );
584
585     /*
586      * The GLX context creation, possibly trying the direct context rendering
587      *  or else use the current context if the user has so specified
588      */
589
590     /*  Set renderType.  */
591     if( window->IsMenu && ( ! fgStructure.MenuContext ) )
592     {
593         /*  Display mode has been set to GLUT_RGB.  */
594         renderType = GLX_RGBA_TYPE;
595     }
596     else if (fgState.DisplayMode & GLUT_INDEX)
597     {
598         renderType = GLX_COLOR_INDEX_TYPE;
599     }
600     else
601     {
602         renderType = GLX_RGBA_TYPE;
603     }
604
605     if( window->IsMenu )
606     {
607         /*
608          * If there isn't already an OpenGL rendering context for menu
609          * windows, make one
610          */
611         if( !fgStructure.MenuContext )
612         {
613             fgStructure.MenuContext =
614                 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
615             fgStructure.MenuContext->Context = glXCreateNewContext(
616                 fgDisplay.Display, *(window->Window.FBConfig), renderType,
617                 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
618             );
619         }
620
621         /* window->Window.Context = fgStructure.MenuContext->MContext; */
622         window->Window.Context = glXCreateContext(
623             fgDisplay.Display, window->Window.FBConfig, renderType,
624             NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
625         );
626     }
627     else if( fgState.UseCurrentContext )
628     {
629         window->Window.Context = glXGetCurrentContext( );
630
631         if( ! window->Window.Context )
632             window->Window.Context = glXCreateNewContext(
633                 fgDisplay.Display, window->Window.FBConfig, renderType,
634                 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
635             );
636     }
637     else
638         window->Window.Context = glXCreateNewContext(
639             fgDisplay.Display, window->Window.FBConfig, renderType,
640             NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
641         );
642
643 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
644     if(  !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
645     {
646       if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
647         fgError( "Unable to force direct context rendering for window '%s'",
648                  title );
649       else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
650         fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
651                  title );
652     }
653 #endif
654
655     /*
656      * XXX Assume the new window is visible by default
657      * XXX Is this a  safe assumption?
658      */
659     window->State.Visible = GL_TRUE;
660
661     sizeHints.flags = 0;
662     if ( positionUse )
663         sizeHints.flags |= USPosition;
664     if ( sizeUse )
665         sizeHints.flags |= USSize;
666
667     /*
668      * Fill in the size hints values now (the x, y, width and height
669      * settings are obsolete, are there any more WMs that support them?)
670      * Unless the X servers actually stop supporting these, we should
671      * continue to fill them in.  It is *not* our place to tell the user
672      * that they should replace a window manager that they like, and which
673      * works, just because *we* think that it's not "modern" enough.
674      */
675     sizeHints.x      = x;
676     sizeHints.y      = y;
677     sizeHints.width  = w;
678     sizeHints.height = h;
679
680     wmHints.flags = StateHint;
681     wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
682     /* Prepare the window and iconified window names... */
683     XStringListToTextProperty( (char **) &title, 1, &textProperty );
684
685     XSetWMProperties(
686         fgDisplay.Display,
687         window->Window.Handle,
688         &textProperty,
689         &textProperty,
690         0,
691         0,
692         &sizeHints,
693         &wmHints,
694         NULL
695     );
696     XFree( textProperty.value );
697
698     XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
699                      &fgDisplay.DeleteWindow, 1 );
700
701     glXMakeContextCurrent(
702         fgDisplay.Display,
703         window->Window.Handle,
704         window->Window.Handle,
705         window->Window.Context
706     );
707
708     XMapWindow( fgDisplay.Display, window->Window.Handle );
709
710     XFree(visualInfo);
711
712 #elif TARGET_HOST_MS_WINDOWS
713
714     WNDCLASS wc;
715     DWORD flags;
716     DWORD exFlags = 0;
717     ATOM atom;
718     int WindowStyle = 0;
719
720     /* Grab the window class we have registered on glutInit(): */
721     atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
722     FREEGLUT_INTERNAL_ERROR_EXIT ( atom, "Window Class Info Not Found",
723                                    "fgOpenWindow" );
724
725     if( gameMode )
726     {
727         FREEGLUT_INTERNAL_ERROR_EXIT ( window->Parent == NULL,
728                                        "Game mode being invoked on a subwindow",
729                                        "fgOpenWindow" );
730
731         /*
732          * Set the window creation flags appropriately to make the window
733          * entirely visible:
734          */
735         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
736     }
737     else
738     {
739         int worig = w, horig = h;
740
741 #if !defined(_WIN32_WCE)
742         if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
743         {
744             /*
745              * Update the window dimensions, taking account of window
746              * decorations.  "freeglut" is to create the window with the
747              * outside of its border at (x,y) and with dimensions (w,h).
748              */
749             w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
750             h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
751                 GetSystemMetrics( SM_CYCAPTION );
752         }
753 #endif /* defined(_WIN32_WCE) */
754
755         if( ! positionUse )
756         {
757             x = CW_USEDEFAULT;
758             y = CW_USEDEFAULT;
759         }
760         /* setting State.Width/Height to call resize callback later */
761         if( ! sizeUse )
762         {
763             if( ! window->IsMenu )
764             {
765                 w = CW_USEDEFAULT;
766                 h = CW_USEDEFAULT;
767             }
768             else /* fail safe - Windows can make a window of size (0, 0) */
769                 w = h = 300; /* default window size */
770             window->State.Width = window->State.Height = -1;
771         }
772         else
773         {
774             window->State.Width = worig;
775             window->State.Height = horig;
776         }
777
778         /*
779          * There's a small difference between creating the top, child and
780          * game mode windows
781          */
782         flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
783
784         if ( window->IsMenu )
785         {
786             flags |= WS_POPUP;
787             exFlags |= WS_EX_TOOLWINDOW;
788         }
789 #if !defined(_WIN32_WCE)
790         else if( window->Parent == NULL )
791             flags |= WS_OVERLAPPEDWINDOW;
792 #endif
793         else
794             flags |= WS_CHILD;
795     }
796
797 #if defined(_WIN32_WCE)
798     {
799         wchar_t* wstr = fghWstrFromStr(title);
800
801         window->Window.Handle = CreateWindow(
802             _T("FREEGLUT"),
803             wstr,
804             WS_VISIBLE | WS_POPUP,
805             0,0, 240,320,
806             NULL,
807             NULL,
808             fgDisplay.Instance,
809             (LPVOID) window
810         );
811
812         free(wstr);
813
814         SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
815         SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
816         SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
817         MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
818         ShowWindow(window->Window.Handle, SW_SHOW);
819         UpdateWindow(window->Window.Handle);
820     }
821 #else
822     window->Window.Handle = CreateWindowEx(
823         exFlags,
824         _T("FREEGLUT"),
825         title,
826         flags,
827         x, y, w, h,
828         (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
829         (HMENU) NULL,
830         fgDisplay.Instance,
831         (LPVOID) window
832     );
833 #endif /* defined(_WIN32_WCE) */
834
835     if( !( window->Window.Handle ) )
836         fgError( "Failed to create a window (%s)!", title );
837
838     /* Make a menu window always on top - fix Feature Request 947118 */
839     if( window->IsMenu || gameMode )
840         SetWindowPos(
841                         window->Window.Handle,
842                         HWND_TOPMOST,
843                         0, 0, 0, 0,
844                         SWP_NOMOVE | SWP_NOSIZE
845                     );
846
847     /* Hack to remove the caption (title bar) and/or border
848      * and all the system menu controls.
849      */
850     WindowStyle = GetWindowLong(window->Window.Handle, GWL_STYLE);
851     if ( fgState.DisplayMode & GLUT_CAPTIONLESS )
852     {
853         SetWindowLong ( window->Window.Handle, GWL_STYLE,
854                         WindowStyle & ~(WS_DLGFRAME | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
855     }
856     else if ( fgState.DisplayMode & GLUT_BORDERLESS )
857     {
858         SetWindowLong ( window->Window.Handle, GWL_STYLE,
859                         WindowStyle & ~(WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX));
860     }
861 //SetWindowPos(window->Window.Handle, NULL, 0, 0, 0, 0,
862 //   SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
863
864
865 #if defined(_WIN32_WCE)
866     ShowWindow( window->Window.Handle, SW_SHOW );
867 #else
868     ShowWindow( window->Window.Handle,
869                 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
870 #endif /* defined(_WIN32_WCE) */
871
872     UpdateWindow( window->Window.Handle );
873     ShowCursor( TRUE );  /* XXX Old comments say "hide cursor"! */
874
875 #endif
876
877     fgSetWindow( window );
878
879     window->Window.DoubleBuffered =
880         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
881
882     if ( ! window->Window.DoubleBuffered )
883     {
884         glDrawBuffer ( GL_FRONT );
885         glReadBuffer ( GL_FRONT );
886     }
887 }
888
889 /*
890  * Closes a window, destroying the frame and OpenGL context
891  */
892 void fgCloseWindow( SFG_Window* window )
893 {
894 #if TARGET_HOST_POSIX_X11
895
896     glXDestroyContext( fgDisplay.Display, window->Window.Context );
897     XFree( window->Window.VisualInfo );
898     XDestroyWindow( fgDisplay.Display, window->Window.Handle );
899     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
900
901 #elif TARGET_HOST_MS_WINDOWS
902
903     /* Make sure we don't close a window with current context active */
904     if( fgStructure.CurrentWindow == window )
905         wglMakeCurrent( NULL, NULL );
906
907     /*
908      * Step through the list of windows.  If the rendering context
909      * is not being used by another window, then we delete it.
910      */
911     {
912         int used = FALSE ;
913         SFG_Window *iter ;
914
915         for( iter = (SFG_Window *)fgStructure.Windows.First;
916              iter;
917              iter = (SFG_Window *)iter->Node.Next )
918         {
919             if( ( iter->Window.Context == window->Window.Context ) &&
920                 ( iter != window ) )
921                 used = TRUE;
922         }
923
924         if( ! used )
925             wglDeleteContext( window->Window.Context );
926     }
927
928     DestroyWindow( window->Window.Handle );
929 #endif
930 }
931
932
933 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
934
935 /*
936  * Creates a new top-level freeglut window
937  */
938 int FGAPIENTRY glutCreateWindow( const char* title )
939 {
940     /* XXX GLUT does not exit; it simply calls "glutInit" quietly if the
941      * XXX application has not already done so.  The "freeglut" community
942      * XXX decided not to go this route (freeglut-developer e-mail from
943      * XXX Steve Baker, 12/16/04, 4:22 PM CST, "Re: [Freeglut-developer]
944      * XXX Desired 'freeglut' behaviour when there is no current window"
945      */
946     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateWindow" );
947
948     return fgCreateWindow( NULL, title, fgState.Position.Use,
949                            fgState.Position.X, fgState.Position.Y,
950                            fgState.Size.Use, fgState.Size.X, fgState.Size.Y,
951                            GL_FALSE, GL_FALSE )->ID;
952 }
953
954 /*
955  * This function creates a sub window.
956  */
957 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
958 {
959     int ret = 0;
960     SFG_Window* window = NULL;
961     SFG_Window* parent = NULL;
962
963     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateSubWindow" );
964     parent = fgWindowByID( parentID );
965     freeglut_return_val_if_fail( parent != NULL, 0 );
966     if ( x < 0 )
967     {
968         x = parent->State.Width + x ;
969         if ( w >= 0 ) x -= w ;
970     }
971
972     if ( w < 0 ) w = parent->State.Width - x + w ;
973     if ( w < 0 )
974     {
975         x += w ;
976         w = -w ;
977     }
978
979     if ( y < 0 )
980     {
981         y = parent->State.Height + y ;
982         if ( h >= 0 ) y -= h ;
983     }
984
985     if ( h < 0 ) h = parent->State.Height - y + h ;
986     if ( h < 0 )
987     {
988         y += h ;
989         h = -h ;
990     }
991
992     window = fgCreateWindow( parent, "", GL_TRUE, x, y, GL_TRUE, w, h, GL_FALSE, GL_FALSE );
993     ret = window->ID;
994
995     return ret;
996 }
997
998 /*
999  * Destroys a window and all of its subwindows
1000  */
1001 void FGAPIENTRY glutDestroyWindow( int windowID )
1002 {
1003     SFG_Window* window;
1004     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyWindow" );
1005     window = fgWindowByID( windowID );
1006     freeglut_return_if_fail( window != NULL );
1007     {
1008         fgExecutionState ExecState = fgState.ExecState;
1009         fgAddToWindowDestroyList( window );
1010         fgState.ExecState = ExecState;
1011     }
1012 }
1013
1014 /*
1015  * This function selects the current window
1016  */
1017 void FGAPIENTRY glutSetWindow( int ID )
1018 {
1019     SFG_Window* window = NULL;
1020
1021     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindow" );
1022     if( fgStructure.CurrentWindow != NULL )
1023         if( fgStructure.CurrentWindow->ID == ID )
1024             return;
1025
1026     window = fgWindowByID( ID );
1027     if( window == NULL )
1028     {
1029         fgWarning( "glutSetWindow(): window ID %d not found!", ID );
1030         return;
1031     }
1032
1033     fgSetWindow( window );
1034 }
1035
1036 /*
1037  * This function returns the ID number of the current window, 0 if none exists
1038  */
1039 int FGAPIENTRY glutGetWindow( void )
1040 {
1041     SFG_Window *win = fgStructure.CurrentWindow;
1042     /*
1043      * Since GLUT did not throw an error if this function was called without a prior call to
1044      * "glutInit", this function shouldn't do so here.  Instead let us return a zero.
1045      * See Feature Request "[ 1307049 ] glutInit check".
1046      */
1047     if ( ! fgState.Initialised )
1048         return 0;
1049
1050     while ( win && win->IsMenu )
1051         win = win->Parent;
1052     return win ? win->ID : 0;
1053 }
1054
1055 /*
1056  * This function makes the current window visible
1057  */
1058 void FGAPIENTRY glutShowWindow( void )
1059 {
1060     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutShowWindow" );
1061     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
1062
1063 #if TARGET_HOST_POSIX_X11
1064
1065     XMapWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1066     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1067
1068 #elif TARGET_HOST_MS_WINDOWS
1069
1070     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_SHOW );
1071
1072 #endif
1073
1074     fgStructure.CurrentWindow->State.Redisplay = GL_TRUE;
1075 }
1076
1077 /*
1078  * This function hides the current window
1079  */
1080 void FGAPIENTRY glutHideWindow( void )
1081 {
1082     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutHideWindow" );
1083     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
1084
1085 #if TARGET_HOST_POSIX_X11
1086
1087     if( fgStructure.CurrentWindow->Parent == NULL )
1088         XWithdrawWindow( fgDisplay.Display,
1089                          fgStructure.CurrentWindow->Window.Handle,
1090                          fgDisplay.Screen );
1091     else
1092         XUnmapWindow( fgDisplay.Display,
1093                       fgStructure.CurrentWindow->Window.Handle );
1094     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1095
1096 #elif TARGET_HOST_MS_WINDOWS
1097
1098     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_HIDE );
1099
1100 #endif
1101
1102     fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1103 }
1104
1105 /*
1106  * Iconify the current window (top-level windows only)
1107  */
1108 void FGAPIENTRY glutIconifyWindow( void )
1109 {
1110     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIconifyWindow" );
1111     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
1112
1113     fgStructure.CurrentWindow->State.Visible   = GL_FALSE;
1114 #if TARGET_HOST_POSIX_X11
1115
1116     XIconifyWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1117                     fgDisplay.Screen );
1118     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1119
1120 #elif TARGET_HOST_MS_WINDOWS
1121
1122     ShowWindow( fgStructure.CurrentWindow->Window.Handle, SW_MINIMIZE );
1123
1124 #endif
1125
1126     fgStructure.CurrentWindow->State.Redisplay = GL_FALSE;
1127 }
1128
1129 /*
1130  * Set the current window's title
1131  */
1132 void FGAPIENTRY glutSetWindowTitle( const char* title )
1133 {
1134     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowTitle" );
1135     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
1136     if( ! fgStructure.CurrentWindow->Parent )
1137     {
1138 #if TARGET_HOST_POSIX_X11
1139
1140         XTextProperty text;
1141
1142         text.value = (unsigned char *) title;
1143         text.encoding = XA_STRING;
1144         text.format = 8;
1145         text.nitems = strlen( title );
1146
1147         XSetWMName(
1148             fgDisplay.Display,
1149             fgStructure.CurrentWindow->Window.Handle,
1150             &text
1151         );
1152
1153         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1154
1155 #elif TARGET_HOST_MS_WINDOWS
1156 #    ifdef _WIN32_WCE
1157         {
1158             wchar_t* wstr = fghWstrFromStr(title);
1159             SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1160             free(wstr);
1161         }
1162 #    else
1163         SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1164 #    endif
1165
1166 #endif
1167     }
1168 }
1169
1170 /*
1171  * Set the current window's iconified title
1172  */
1173 void FGAPIENTRY glutSetIconTitle( const char* title )
1174 {
1175     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetIconTitle" );
1176     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
1177
1178     if( ! fgStructure.CurrentWindow->Parent )
1179     {
1180 #if TARGET_HOST_POSIX_X11
1181
1182         XTextProperty text;
1183
1184         text.value = (unsigned char *) title;
1185         text.encoding = XA_STRING;
1186         text.format = 8;
1187         text.nitems = strlen( title );
1188
1189         XSetWMIconName(
1190             fgDisplay.Display,
1191             fgStructure.CurrentWindow->Window.Handle,
1192             &text
1193         );
1194
1195         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1196
1197 #elif TARGET_HOST_MS_WINDOWS
1198 #    ifdef _WIN32_WCE
1199         {
1200             wchar_t* wstr = fghWstrFromStr(title);
1201             SetWindowText( fgStructure.CurrentWindow->Window.Handle, wstr );
1202             free(wstr);
1203         }
1204 #    else
1205         SetWindowText( fgStructure.CurrentWindow->Window.Handle, title );
1206 #    endif
1207
1208 #endif
1209     }
1210 }
1211
1212 /*
1213  * Change the current window's size
1214  */
1215 void FGAPIENTRY glutReshapeWindow( int width, int height )
1216 {
1217     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReshapeWindow" );
1218     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
1219
1220     fgStructure.CurrentWindow->State.NeedToResize = GL_TRUE;
1221     fgStructure.CurrentWindow->State.Width  = width ;
1222     fgStructure.CurrentWindow->State.Height = height;
1223 }
1224
1225 /*
1226  * Change the current window's position
1227  */
1228 void FGAPIENTRY glutPositionWindow( int x, int y )
1229 {
1230     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPositionWindow" );
1231     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
1232
1233 #if TARGET_HOST_POSIX_X11
1234
1235     XMoveWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle,
1236                  x, y );
1237     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1238
1239 #elif TARGET_HOST_MS_WINDOWS
1240
1241     {
1242         RECT winRect;
1243
1244         /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
1245         GetWindowRect( fgStructure.CurrentWindow->Window.Handle, &winRect );
1246         MoveWindow(
1247             fgStructure.CurrentWindow->Window.Handle,
1248             x,
1249             y,
1250             winRect.right - winRect.left,
1251             winRect.bottom - winRect.top,
1252             TRUE
1253         );
1254     }
1255
1256 #endif
1257 }
1258
1259 /*
1260  * Lowers the current window (by Z order change)
1261  */
1262 void FGAPIENTRY glutPushWindow( void )
1263 {
1264     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPushWindow" );
1265     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
1266
1267 #if TARGET_HOST_POSIX_X11
1268
1269     XLowerWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1270
1271 #elif TARGET_HOST_MS_WINDOWS
1272
1273     SetWindowPos(
1274         fgStructure.CurrentWindow->Window.Handle,
1275         HWND_BOTTOM,
1276         0, 0, 0, 0,
1277         SWP_NOSIZE | SWP_NOMOVE
1278     );
1279
1280 #endif
1281 }
1282
1283 /*
1284  * Raises the current window (by Z order change)
1285  */
1286 void FGAPIENTRY glutPopWindow( void )
1287 {
1288     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutPopWindow" );
1289     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
1290
1291 #if TARGET_HOST_POSIX_X11
1292
1293     XRaiseWindow( fgDisplay.Display, fgStructure.CurrentWindow->Window.Handle );
1294
1295 #elif TARGET_HOST_MS_WINDOWS
1296
1297     SetWindowPos(
1298         fgStructure.CurrentWindow->Window.Handle,
1299         HWND_TOP,
1300         0, 0, 0, 0,
1301         SWP_NOSIZE | SWP_NOMOVE
1302     );
1303
1304 #endif
1305 }
1306
1307 /*
1308  * Resize the current window so that it fits the whole screen
1309  */
1310 void FGAPIENTRY glutFullScreen( void )
1311 {
1312     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutFullScreen" );
1313     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1314
1315     {
1316 #if TARGET_HOST_POSIX_X11
1317         int x, y;
1318         Window w;
1319
1320         XMoveResizeWindow(
1321             fgDisplay.Display,
1322             fgStructure.CurrentWindow->Window.Handle,
1323             0, 0,
1324             fgDisplay.ScreenWidth,
1325             fgDisplay.ScreenHeight
1326         );
1327
1328         XFlush( fgDisplay.Display ); /* This is needed */
1329
1330         XTranslateCoordinates(
1331             fgDisplay.Display,
1332             fgStructure.CurrentWindow->Window.Handle,
1333             fgDisplay.RootWindow,
1334             0, 0, &x, &y, &w
1335         );
1336
1337         if (x || y)
1338         {
1339             XMoveWindow(
1340                 fgDisplay.Display,
1341                 fgStructure.CurrentWindow->Window.Handle,
1342                 -x, -y
1343             );
1344             XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1345         }
1346 #elif TARGET_HOST_MS_WINDOWS && !defined(_WIN32_WCE) /* FIXME: what about WinCE */
1347         RECT rect;
1348
1349         /* For fullscreen mode, force the top-left corner to 0,0
1350          * and adjust the window rectangle so that the client area
1351          * covers the whole screen.
1352          */
1353
1354         rect.left   = 0;
1355         rect.top    = 0;
1356         rect.right  = fgDisplay.ScreenWidth;
1357         rect.bottom = fgDisplay.ScreenHeight;
1358
1359         AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1360                                   WS_CLIPCHILDREN, FALSE );
1361
1362         /*
1363          * SWP_NOACTIVATE     Do not activate the window
1364          * SWP_NOOWNERZORDER  Do not change position in z-order
1365          * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1366          * SWP_NOZORDER       Retains the current Z order (ignore 2nd param)
1367          */
1368
1369         SetWindowPos( fgStructure.CurrentWindow->Window.Handle,
1370                       HWND_TOP,
1371                       rect.left,
1372                       rect.top,
1373                       rect.right  - rect.left,
1374                       rect.bottom - rect.top,
1375                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1376                       SWP_NOZORDER
1377                     );
1378 #endif
1379     }
1380 }
1381
1382 /*
1383  * A.Donev: Set and retrieve the window's user data
1384  */
1385 void* FGAPIENTRY glutGetWindowData( void )
1386 {
1387     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetWindowData" );
1388     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutGetWindowData" );
1389     return fgStructure.CurrentWindow->UserData;
1390 }
1391
1392 void FGAPIENTRY glutSetWindowData(void* data)
1393 {
1394     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetWindowData" );
1395     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowData" );
1396     fgStructure.CurrentWindow->UserData = data;
1397 }
1398
1399 /*** END OF FILE ***/