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