Alas, the ChangeLog has not the usual ChangeLog format, so tell
[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     freeglut_assert_ready;
279
280     /*
281      * XXX fgChooseVisual() is a common part of all three.
282      * XXX With a little thought, we should be able to greatly
283      * XXX simplify this.
284      */
285     if( !window->IsMenu )
286         window->Window.VisualInfo = fgChooseVisual( );
287     else if( fgStructure.MenuContext )
288         window->Window.VisualInfo = fgChooseVisual( );
289     else
290     {
291         /* XXX Why are menus double- and depth-buffered? */
292         unsigned int current_DisplayMode = fgState.DisplayMode ;
293         fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ;
294         window->Window.VisualInfo = fgChooseVisual( );
295         fgState.DisplayMode = current_DisplayMode ;
296     }
297
298     if( ! window->Window.VisualInfo )
299     {
300         /*
301          * The "fgChooseVisual" returned a null meaning that the visual
302          * context is not available.
303          * Try a couple of variations to see if they will work.
304          */
305         if( !( fgState.DisplayMode & GLUT_DOUBLE ) )
306         {
307             fgState.DisplayMode |= GLUT_DOUBLE ;
308             window->Window.VisualInfo = fgChooseVisual( );
309             fgState.DisplayMode &= ~GLUT_DOUBLE;
310         }
311
312         /*
313          * GLUT also checks for multi-sampling, but I don't see that
314          * anywhere else in FREEGLUT so I won't bother with it for the moment.
315          */
316     }
317
318     /*
319      * XXX This seems to be abusing an assert() for error-checking.
320      * XXX It is possible that the visual simply can't be found,
321      * XXX in which case we should print an error and return a 0
322      * XXX for the window id, I think.
323      */
324     assert( window->Window.VisualInfo != NULL );
325
326
327     /*
328      * XXX HINT: the masks should be updated when adding/removing callbacks.
329      * XXX       This might speed up message processing. Is that true?
330      * XXX
331      * XXX A: Not appreciably, but it WILL make it easier to debug.
332      * XXX    Try tracing old GLUT and try tracing freeglut.  Old GLUT
333      * XXX    turns off events that it doesn't need and is a whole lot
334      * XXX    more pleasant to trace.  (Think mouse-motion!  Tons of
335      * XXX    ``bonus'' GUI events stream in.)
336      */
337     winAttr.event_mask        =
338         StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
339         ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
340         VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
341         PointerMotionMask | ButtonMotionMask;
342     winAttr.background_pixmap = None;
343     winAttr.background_pixel  = 0;
344     winAttr.border_pixel      = 0;
345
346     winAttr.colormap = XCreateColormap(
347         fgDisplay.Display, fgDisplay.RootWindow,
348         window->Window.VisualInfo->visual, AllocNone
349     );
350
351     mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
352
353     if( window->IsMenu )
354     {
355         winAttr.override_redirect = True;
356         mask |= CWOverrideRedirect;
357     }
358
359     window->Window.Handle = XCreateWindow(
360         fgDisplay.Display,
361         window->Parent == NULL ? fgDisplay.RootWindow :
362         window->Parent->Window.Handle,
363         x, y, w, h, 0,
364         window->Window.VisualInfo->depth, InputOutput,
365         window->Window.VisualInfo->visual, mask,
366         &winAttr
367     );
368
369     /*
370      * The GLX context creation, possibly trying the direct context rendering
371      *  or else use the current context if the user has so specified
372      */
373     if( window->IsMenu )
374     {
375         /*
376          * If there isn't already an OpenGL rendering context for menu
377          * windows, make one
378          */
379         if( !fgStructure.MenuContext )
380         {
381             fgStructure.MenuContext =
382                 (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
383             fgStructure.MenuContext->VisualInfo = window->Window.VisualInfo;
384             fgStructure.MenuContext->Context = glXCreateContext(
385                 fgDisplay.Display, fgStructure.MenuContext->VisualInfo,
386                 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
387             );
388         }
389
390         /* window->Window.Context = fgStructure.MenuContext->Context; */
391         window->Window.Context = glXCreateContext(
392             fgDisplay.Display, window->Window.VisualInfo,
393             NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
394         );
395     }
396     else if( fgState.UseCurrentContext )
397     {
398         window->Window.Context = glXGetCurrentContext( );
399
400         if( ! window->Window.Context )
401             window->Window.Context = glXCreateContext(
402                 fgDisplay.Display, window->Window.VisualInfo,
403                 NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
404             );
405     }
406     else
407         window->Window.Context = glXCreateContext(
408             fgDisplay.Display, window->Window.VisualInfo,
409             NULL, ( fgState.DirectContext != GLUT_FORCE_INDIRECT_CONTEXT )
410         );
411
412 #if !defined( __FreeBSD__ ) && !defined( __NetBSD__ )
413     if(  !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
414     {
415       if( fgState.DirectContext == GLUT_FORCE_DIRECT_CONTEXT )
416         fgError( "Unable to force direct context rendering for window '%s'",
417                  title );
418       else if( fgState.DirectContext == GLUT_TRY_DIRECT_CONTEXT )
419         fgWarning( "Unable to create direct context rendering for window '%s'\nThis may hurt performance.",
420                  title );
421     }
422 #endif
423
424     glXMakeCurrent(
425         fgDisplay.Display,
426         window->Window.Handle,
427         window->Window.Context
428     );
429
430     /*
431      * XXX Assume the new window is visible by default
432      * XXX Is this a  safe assumption?
433      */
434     window->State.Visible = GL_TRUE;
435
436     sizeHints.flags = 0;
437     if ( fgState.Position.Use )
438         sizeHints.flags |= USPosition;
439     if ( fgState.Size.Use )
440         sizeHints.flags |= USSize;
441
442     /*
443      * Fill in the size hints values now (the x, y, width and height
444      * settings are obsolete, are there any more WMs that support them?)
445      * Unless the X servers actually stop supporting these, we should
446      * continue to fill them in.  It is *not* our place to tell the user
447      * that they should replace a window manager that they like, and which
448      * works, just because *we* think that it's not "modern" enough.
449      */
450 #if TARGET_HOST_WINCE /* Since this is in the X11 branch, it's pretty dumb */
451     sizeHints.x      = 0;
452     sizeHints.y      = 0;
453     sizeHints.width  = 320;
454     sizeHints.height = 240;
455 #else
456     sizeHints.x      = x;
457     sizeHints.y      = y;
458     sizeHints.width  = w;
459     sizeHints.height = h;
460 #endif /* TARGET_HOST_WINCE */
461
462     wmHints.flags = StateHint;
463     wmHints.initial_state = fgState.ForceIconic ? IconicState : NormalState;
464     /* Prepare the window and iconified window names... */
465     XStringListToTextProperty( (char **) &title, 1, &textProperty );
466
467     XSetWMProperties(
468         fgDisplay.Display,
469         window->Window.Handle,
470         &textProperty,
471         &textProperty,
472         0,
473         0,
474         &sizeHints,
475         &wmHints,
476         NULL
477     );
478
479     XSetWMProtocols( fgDisplay.Display, window->Window.Handle,
480                      &fgDisplay.DeleteWindow, 1 );
481
482     XMapWindow( fgDisplay.Display, window->Window.Handle );
483
484 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
485
486     WNDCLASS wc;
487     DWORD flags;
488     DWORD exFlags = 0;
489     ATOM atom;
490
491     freeglut_assert_ready;
492
493     /* Grab the window class we have registered on glutInit(): */
494     atom = GetClassInfo( fgDisplay.Instance, _T("FREEGLUT"), &wc );
495     assert( atom != 0 );
496
497     if( gameMode )
498     {
499         assert( window->Parent == NULL );
500
501         /*
502          * Set the window creation flags appropriately to make the window
503          * entirely visible:
504          */
505         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
506     }
507     else
508     {
509 #if !TARGET_HOST_WINCE
510         if ( ( ! isSubWindow ) && ( ! window->IsMenu ) )
511         {
512             /*
513              * Update the window dimensions, taking account of window
514              * decorations.  "freeglut" is to create the window with the
515              * outside of its border at (x,y) and with dimensions (w,h).
516              */
517             w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
518             h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 +
519                 GetSystemMetrics( SM_CYCAPTION );
520         }
521 #endif /* TARGET_HOST_WINCE */
522
523         if( ! fgState.Position.Use )
524         {
525             x = CW_USEDEFAULT;
526             y = CW_USEDEFAULT;
527         }
528         if( ! fgState.Size.Use )
529         {
530             w = CW_USEDEFAULT;
531             h = CW_USEDEFAULT;
532         }
533
534         /*
535          * There's a small difference between creating the top, child and
536          * game mode windows
537          */
538         flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
539
540         if ( window->IsMenu )
541         {
542             flags |= WS_POPUP;
543             exFlags |= WS_EX_TOOLWINDOW;
544         }
545 #if !TARGET_HOST_WINCE
546         else if( window->Parent == NULL )
547             flags |= WS_OVERLAPPEDWINDOW;
548 #endif
549         else
550             flags |= WS_CHILD;
551     }
552
553 #if TARGET_HOST_WINCE
554     {
555         wchar_t* wstr = fghWstrFromStr(title);
556
557         window->Window.Handle = CreateWindow(
558             _T("FREEGLUT"),
559             wstr,
560             WS_VISIBLE | WS_POPUP,
561             0,0, 240,320,
562             NULL,
563             NULL,
564             fgDisplay.Instance,
565             (LPVOID) window
566         );
567
568         free(wstr);
569
570         SHFullScreen(window->Window.Handle, SHFS_HIDESTARTICON);
571         SHFullScreen(window->Window.Handle, SHFS_HIDESIPBUTTON);
572         SHFullScreen(window->Window.Handle, SHFS_HIDETASKBAR);
573         MoveWindow(window->Window.Handle, 0, 0, 240, 320, TRUE);
574         ShowWindow(window->Window.Handle, SW_SHOW);
575         UpdateWindow(window->Window.Handle);
576     }
577 #else
578     window->Window.Handle = CreateWindowEx(
579         exFlags,
580         "FREEGLUT",
581         title,
582         flags,
583         x, y, w, h,
584         (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
585         (HMENU) NULL,
586         fgDisplay.Instance,
587         (LPVOID) window
588     );
589 #endif /* TARGET_HOST_WINCE */
590
591     if( !( window->Window.Handle ) )
592         fgError( "Failed to create a window (%s)!", title );
593
594 #if TARGET_HOST_WINCE
595     ShowWindow( window->Window.Handle, SW_SHOW );
596 #else
597     ShowWindow( window->Window.Handle,
598                 fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
599 #endif /* TARGET_HOST_WINCE */
600
601     UpdateWindow( window->Window.Handle );
602     ShowCursor( TRUE );  /* XXX Old comments say "hide cusror"! */
603
604 #endif
605
606     fgSetWindow( window );
607
608     window->Window.DoubleBuffered =
609         ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0;
610
611     if ( ! window->Window.DoubleBuffered )
612     {
613         glDrawBuffer ( GL_FRONT );
614         glReadBuffer ( GL_FRONT );
615     }
616 }
617
618 /*
619  * Closes a window, destroying the frame and OpenGL context
620  */
621 void fgCloseWindow( SFG_Window* window )
622 {
623     freeglut_assert_ready;
624
625 #if TARGET_HOST_UNIX_X11
626
627     glXDestroyContext( fgDisplay.Display, window->Window.Context );
628     XDestroyWindow( fgDisplay.Display, window->Window.Handle );
629     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
630
631 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
632
633     /* Make sure we don't close a window with current context active */
634     if( fgStructure.Window == window )
635         wglMakeCurrent( NULL, NULL );
636
637     /*
638      * Step through the list of windows.  If the rendering context
639      * is not being used by another window, then we delete it.
640      */
641     {
642         int used = FALSE ;
643         SFG_Window *iter ;
644
645         for( iter = (SFG_Window *)fgStructure.Windows.First;
646              iter;
647              iter = (SFG_Window *)iter->Node.Next )
648         {
649             if( ( iter->Window.Context == window->Window.Context ) &&
650                 ( iter != window ) )
651                 used = TRUE;
652         }
653
654         if( ! used )
655             wglDeleteContext( window->Window.Context );
656     }
657
658     DestroyWindow( window->Window.Handle );
659 #endif
660 }
661
662
663 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
664
665 /*
666  * Creates a new top-level freeglut window
667  */
668 int FGAPIENTRY glutCreateWindow( const char* title )
669 {
670     return fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
671                            fgState.Size.X, fgState.Size.Y, GL_FALSE,
672                            GL_FALSE )->ID;
673 }
674
675 /*
676  * This function creates a sub window.
677  */
678 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
679 {
680     int ret = 0;
681     SFG_Window* window = NULL;
682     SFG_Window* parent = NULL;
683
684     freeglut_assert_ready;
685     parent = fgWindowByID( parentID );
686     freeglut_return_val_if_fail( parent != NULL, 0 );
687     window = fgCreateWindow( parent, "", x, y, w, h, GL_FALSE, GL_FALSE );
688     ret = window->ID;
689
690     return ret;
691 }
692
693 /*
694  * Destroys a window and all of its subwindows
695  */
696 void FGAPIENTRY glutDestroyWindow( int windowID )
697 {
698     SFG_Window* window = fgWindowByID( windowID );
699     freeglut_return_if_fail( window != NULL );
700     {
701         fgExecutionState ExecState = fgState.ExecState;
702         fgAddToWindowDestroyList( window );
703         fgState.ExecState = ExecState;
704     }
705 }
706
707 /*
708  * This function selects the current window
709  */
710 void FGAPIENTRY glutSetWindow( int ID )
711 {
712     SFG_Window* window = NULL;
713
714     freeglut_assert_ready;
715     if( fgStructure.Window != NULL )
716         if( fgStructure.Window->ID == ID )
717             return;
718
719     window = fgWindowByID( ID );
720     if( window == NULL )
721     {
722         fgWarning( "glutSetWindow(): window ID %d not found!", ID );
723         return;
724     }
725
726     fgSetWindow( window );
727 }
728
729 /*
730  * This function returns the ID number of the current window, 0 if none exists
731  */
732 int FGAPIENTRY glutGetWindow( void )
733 {
734     freeglut_assert_ready;
735     if( fgStructure.Window == NULL )
736         return 0;
737     return fgStructure.Window->ID;
738 }
739
740 /*
741  * This function makes the current window visible
742  */
743 void FGAPIENTRY glutShowWindow( void )
744 {
745     freeglut_assert_ready;
746     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutShowWindow" );
747
748 #if TARGET_HOST_UNIX_X11
749
750     XMapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
751     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
752
753 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
754
755     ShowWindow( fgStructure.Window->Window.Handle, SW_SHOW );
756
757 #endif
758
759     fgStructure.Window->State.Redisplay = GL_TRUE;
760 }
761
762 /*
763  * This function hides the current window
764  */
765 void FGAPIENTRY glutHideWindow( void )
766 {
767     freeglut_assert_ready;
768     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutHideWindow" );
769
770 #if TARGET_HOST_UNIX_X11
771
772     if( fgStructure.Window->Parent == NULL )
773         XWithdrawWindow( fgDisplay.Display,
774                          fgStructure.Window->Window.Handle,
775                          fgDisplay.Screen );
776     else
777         XUnmapWindow( fgDisplay.Display,
778                       fgStructure.Window->Window.Handle );
779     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
780
781 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
782
783     ShowWindow( fgStructure.Window->Window.Handle, SW_HIDE );
784
785 #endif
786
787     fgStructure.Window->State.Redisplay = GL_FALSE;
788 }
789
790 /*
791  * Iconify the current window (top-level windows only)
792  */
793 void FGAPIENTRY glutIconifyWindow( void )
794 {
795     freeglut_assert_ready;
796     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIconifyWindow" );
797
798     fgStructure.Window->State.Visible   = GL_FALSE;
799 #if TARGET_HOST_UNIX_X11
800
801     XIconifyWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
802                     fgDisplay.Screen );
803     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
804
805 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
806
807     ShowWindow( fgStructure.Window->Window.Handle, SW_MINIMIZE );
808
809 #endif
810
811     fgStructure.Window->State.Redisplay = GL_FALSE;
812 }
813
814 /*
815  * Set the current window's title
816  */
817 void FGAPIENTRY glutSetWindowTitle( const char* title )
818 {
819     freeglut_assert_ready;
820     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetWindowTitle" );
821     if( ! fgStructure.Window->Parent )
822     {
823 #if TARGET_HOST_UNIX_X11
824
825         XTextProperty text;
826
827         text.value = (unsigned char *) title;
828         text.encoding = XA_STRING;
829         text.format = 8;
830         text.nitems = strlen( title );
831
832         XSetWMName(
833             fgDisplay.Display,
834             fgStructure.Window->Window.Handle,
835             &text
836         );
837
838         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
839
840 #elif TARGET_HOST_WIN32
841
842         SetWindowText( fgStructure.Window->Window.Handle, title );
843
844 #elif TARGET_HOST_WINCE
845         {
846             wchar_t* wstr = fghWstrFromStr(title);
847
848             SetWindowText( fgStructure.Window->Window.Handle, wstr );
849
850             free(wstr);
851         }
852 #endif
853     }
854 }
855
856 /*
857  * Set the current window's iconified title
858  */
859 void FGAPIENTRY glutSetIconTitle( const char* title )
860 {
861     freeglut_assert_ready;
862     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutSetIconTitle" );
863
864     if( ! fgStructure.Window->Parent )
865     {
866 #if TARGET_HOST_UNIX_X11
867
868         XTextProperty text;
869
870         text.value = (unsigned char *) title;
871         text.encoding = XA_STRING;
872         text.format = 8;
873         text.nitems = strlen( title );
874
875         XSetWMIconName(
876             fgDisplay.Display,
877             fgStructure.Window->Window.Handle,
878             &text
879         );
880
881         XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
882
883 #elif TARGET_HOST_WIN32
884
885         SetWindowText( fgStructure.Window->Window.Handle, title );
886
887 #elif TARGET_HOST_WINCE
888         {
889             wchar_t* wstr = fghWstrFromStr(title);
890
891             SetWindowText( fgStructure.Window->Window.Handle, wstr );
892
893             free(wstr);
894         }
895 #endif
896     }
897 }
898
899 /*
900  * Change the current window's size
901  */
902 void FGAPIENTRY glutReshapeWindow( int width, int height )
903 {
904     freeglut_assert_ready;
905     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutReshapeWindow" );
906
907     fgStructure.Window->State.NeedToResize = GL_TRUE;
908     fgStructure.Window->State.Width  = width ;
909     fgStructure.Window->State.Height = height;
910 }
911
912 /*
913  * Change the current window's position
914  */
915 void FGAPIENTRY glutPositionWindow( int x, int y )
916 {
917     freeglut_assert_ready;
918     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPositionWindow" );
919
920 #if TARGET_HOST_UNIX_X11
921
922     XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle,
923                  x, y );
924     XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
925
926 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
927
928     {
929         RECT winRect;
930
931         /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
932         GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
933         MoveWindow(
934             fgStructure.Window->Window.Handle,
935             x,
936             y,
937             winRect.right - winRect.left,
938             winRect.bottom - winRect.top,
939             TRUE
940         );
941     }
942
943 #endif
944 }
945
946 /*
947  * Lowers the current window (by Z order change)
948  */
949 void FGAPIENTRY glutPushWindow( void )
950 {
951     freeglut_assert_ready;
952     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPushWindow" );
953
954 #if TARGET_HOST_UNIX_X11
955
956     XLowerWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
957
958 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
959
960     SetWindowPos(
961         fgStructure.Window->Window.Handle,
962         HWND_BOTTOM,
963         0, 0, 0, 0,
964         SWP_NOSIZE | SWP_NOMOVE
965     );
966
967 #endif
968 }
969
970 /*
971  * Raises the current window (by Z order change)
972  */
973 void FGAPIENTRY glutPopWindow( void )
974 {
975     freeglut_assert_ready;
976     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutPopWindow" );
977
978 #if TARGET_HOST_UNIX_X11
979
980     XRaiseWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
981
982 #elif TARGET_HOST_WIN32 || TARGET_HOST_WINCE
983
984     SetWindowPos(
985         fgStructure.Window->Window.Handle,
986         HWND_TOP,
987         0, 0, 0, 0,
988         SWP_NOSIZE | SWP_NOMOVE
989     );
990
991 #endif
992 }
993
994 /*
995  * Resize the current window so that it fits the whole screen
996  */
997 void FGAPIENTRY glutFullScreen( void )
998 {
999     freeglut_assert_ready;
1000     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutFullScreen" );
1001
1002     {
1003 #if TARGET_HOST_UNIX_X11
1004         int x, y;
1005         Window w;
1006
1007         XMoveResizeWindow(
1008             fgDisplay.Display,
1009             fgStructure.Window->Window.Handle,
1010             0, 0,
1011             fgDisplay.ScreenWidth,
1012             fgDisplay.ScreenHeight
1013         );
1014
1015         XFlush( fgDisplay.Display ); /* This is needed */
1016
1017         XTranslateCoordinates(
1018             fgDisplay.Display,
1019             fgStructure.Window->Window.Handle,
1020             fgDisplay.RootWindow,
1021             0, 0, &x, &y, &w
1022         );
1023
1024         if (x || y)
1025         {
1026             XMoveWindow(
1027                 fgDisplay.Display,
1028                 fgStructure.Window->Window.Handle,
1029                 -x, -y
1030             );
1031             XFlush( fgDisplay.Display ); /* XXX Shouldn't need this */
1032         }
1033 #elif TARGET_HOST_WIN32
1034         RECT rect;
1035
1036         /* For fullscreen mode, force the top-left corner to 0,0
1037          * and adjust the window rectangle so that the client area
1038          * covers the whole screen.
1039          */
1040
1041         rect.left   = 0;
1042         rect.top    = 0;
1043         rect.right  = fgDisplay.ScreenWidth;
1044         rect.bottom = fgDisplay.ScreenHeight;
1045
1046         AdjustWindowRect ( &rect, WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS |
1047                                   WS_CLIPCHILDREN, FALSE );
1048
1049         /*
1050          * SWP_NOACTIVATE     Do not activate the window
1051          * SWP_NOOWNERZORDER  Do not change position in z-order
1052          * SWP_NOSENDCHANGING Supress WM_WINDOWPOSCHANGING message
1053          * SWP_NOZORDER       Retains the current Z order (ignore 2nd param)
1054          */
1055
1056         SetWindowPos( fgStructure.Window->Window.Handle,
1057                       HWND_TOP,
1058                       rect.left,
1059                       rect.top,
1060                       rect.right  - rect.left,
1061                       rect.bottom - rect.top,
1062                       SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
1063                       SWP_NOZORDER
1064                     );
1065 #endif
1066     }
1067 }
1068
1069 /*
1070  * A.Donev: Set and retrieve the window's user data
1071  */
1072 void* FGAPIENTRY glutGetWindowData( void )
1073 {
1074     return fgStructure.Window->UserData;
1075 }
1076
1077 void FGAPIENTRY glutSetWindowData(void* data)
1078 {
1079     fgStructure.Window->UserData = data;
1080 }
1081
1082 /*** END OF FILE ***/