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