7d67b61939ca103c4b1c82e56922b0b778cadff0
[freeglut] / freeglut-1.3 / 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 #define  G_LOG_DOMAIN  "freeglut-window"
33
34 #include "../include/GL/freeglut.h"
35 #include "freeglut_internal.h"
36
37 /*
38  * TODO BEFORE THE STABLE RELEASE:
39  *
40  *  fgChooseVisual()        -- OK, but what about glutInitDisplayString()?
41  *  fgSetupPixelFormat      -- ignores the display mode settings
42  *  fgOpenWindow()          -- check the Win32 version, -iconic handling!
43  *  fgCloseWindow()         -- check the Win32 version
44  *  glutCreateWindow()      -- see what happens when default position and size is {-1,-1}
45  *  glutCreateSubWindow()   -- see what happens when default position and size is {-1,-1}
46  *  glutDestroyWindow()     -- check the Win32 version
47  *  glutSetWindow()         -- check the Win32 version
48  *  glutGetWindow()         -- OK
49  *  glutSetWindowTitle()    -- check the Win32 version
50  *  glutSetIconTitle()      -- check the Win32 version
51  *  glutShowWindow()        -- check the Win32 version
52  *  glutHideWindow()        -- check the Win32 version
53  *  glutIconifyWindow()     -- check the Win32 version
54  *  glutReshapeWindow()     -- check the Win32 version
55  *  glutPositionWindow()    -- check the Win32 version
56  *  glutPushWindow()        -- check the Win32 version
57  *  glutPopWindow()         -- check the Win32 version
58  */
59
60 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
61
62 /*
63  * Chooses a visual basing on the current display mode settings
64  */
65 #if TARGET_HOST_UNIX_X11
66 XVisualInfo* fgChooseVisual( void )
67 {
68     int bufferSize[] = { 16, 12, 8, 4, 2, 1 };
69     GLboolean wantIndexedMode = FALSE;
70     int attributes[ 32 ];
71     int where = 0;
72
73     /*
74      * First we have to process the display mode settings...
75      */
76 #   define ATTRIB(a) attributes[where++]=a;
77
78     /*
79      * Decide if we want a true or indexed color visual:
80      */
81     if( !(fgState.DisplayMode & GLUT_INDEX) )
82     {
83         /*
84          * We are sure that there will be R, B and B components requested:
85          */
86         ATTRIB( GLX_RGBA       );
87         ATTRIB( GLX_RED_SIZE   ); ATTRIB( 1 );
88         ATTRIB( GLX_GREEN_SIZE ); ATTRIB( 1 );
89         ATTRIB( GLX_BLUE_SIZE  ); ATTRIB( 1 );
90
91         /*
92          * Check if the A component is required, too:
93          */
94         if( fgState.DisplayMode & GLUT_ALPHA )
95         {
96             ATTRIB( GLX_ALPHA_SIZE ); ATTRIB( 1 );
97         }
98     }
99     else
100     {
101         /*
102          * We've got an indexed color request
103          */
104         ATTRIB( GLX_BUFFER_SIZE ); ATTRIB( 8 );
105
106         /*
107          * Set the 'I want indexed mode' switch
108          */
109         wantIndexedMode = TRUE;
110     }
111
112     /*
113      * We can have double or single buffered contexts created
114      */
115     if( fgState.DisplayMode & GLUT_DOUBLE )
116     {
117         ATTRIB( GLX_DOUBLEBUFFER );
118     }
119
120     /*
121      * Stereoscopy seems a nice thing to have
122      */
123     if( fgState.DisplayMode & GLUT_STEREO )
124     {
125         ATTRIB( GLX_STEREO );
126     }
127
128     /*
129      * Depth buffer is almost always required
130      */
131     if( fgState.DisplayMode & GLUT_DEPTH )
132     {
133         ATTRIB( GLX_DEPTH_SIZE ); ATTRIB( 1 );
134     }
135
136     /*
137      * Stenciling support
138      */
139     if( fgState.DisplayMode & GLUT_STENCIL )
140     {
141         ATTRIB( GLX_STENCIL_SIZE ); ATTRIB( 1 );
142     }
143
144     /*
145      * And finally the accumulation buffers
146      */
147     if( fgState.DisplayMode & GLUT_ACCUM )
148     {
149         ATTRIB( GLX_ACCUM_RED_SIZE );   ATTRIB( 1 );
150         ATTRIB( GLX_ACCUM_GREEN_SIZE ); ATTRIB( 1 );
151         ATTRIB( GLX_ACCUM_BLUE_SIZE );  ATTRIB( 1 );
152
153         /*
154          * Check if the A component is required, too:
155          */
156         if( fgState.DisplayMode & GLUT_ALPHA )
157         {
158             ATTRIB( GLX_ACCUM_ALPHA_SIZE ); ATTRIB( 1 );
159         }
160     }
161
162     /*
163      * Push a null at the end of the list
164      */
165     ATTRIB( None );
166
167     /*
168      * OKi now, we've got two cases -- RGB(A) and index mode visuals
169      */
170     if( wantIndexedMode == FALSE )
171     {
172         /*
173          * The easier one. And more common, too.
174          */
175         return( glXChooseVisual( fgDisplay.Display, fgDisplay.Screen, attributes ) );
176     }
177     else
178     {
179         XVisualInfo* visualInfo;
180         int i;
181
182         /*
183          * In indexed mode, we need to check how many bits of depth can we achieve
184          */
185         for( i=0; i<6; i++ )
186         {
187
188             /*
189              * The GLX_BUFFER_SIZE value comes always first, so:
190              */
191             attributes[ 1 ] = bufferSize[ i ];
192
193             /*
194              * Check if such visual is possible
195              */
196             visualInfo = glXChooseVisual( fgDisplay.Display, fgDisplay.Screen, attributes );
197
198             /*
199              * The buffer size are sorted in descendant order, so choose the first:
200              */
201             if( visualInfo != NULL )
202                 return( visualInfo );
203         }
204
205         /*
206          * If we are still here, it means that the visual info was not found
207          */
208         return( NULL );
209     }
210 }
211 #endif
212
213 /*
214  * Setup the pixel format for a Win32 window
215  */
216 #if TARGET_HOST_WIN32
217 GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly, unsigned char layer_type )
218 {
219         PIXELFORMATDESCRIPTOR* ppfd, pfd;
220         int flags, pixelformat;
221
222         /*
223          * Check if the window seems valid
224          */
225         freeglut_return_val_if_fail( window != NULL, 0 );
226
227         /*
228          * The pixel format should allow us to draw to the window using OpenGL
229          */
230         flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
231         
232         /*
233          * It might be the case for us to use double buffering
234          */
235   if( fgState.DisplayMode & GLUT_DOUBLE )
236         flags |= PFD_DOUBLEBUFFER;
237
238   /*
239    * Specify which pixel format do we opt for...
240    */
241 #       pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
242
243   pfd.nSize                             = sizeof(PIXELFORMATDESCRIPTOR);
244   pfd.nVersion                  = 1;
245   pfd.dwFlags                           = flags;
246   pfd.iPixelType                        = PFD_TYPE_RGBA;
247   pfd.cColorBits                        = 24;
248   pfd.cRedBits                  = 0;
249   pfd.cRedShift                 = 0;
250   pfd.cGreenBits                        = 0;
251   pfd.cGreenShift                       = 0;
252   pfd.cBlueBits                 = 0;
253   pfd.cBlueShift                        = 0;
254   pfd.cAlphaBits                        = 0;
255   pfd.cAlphaShift                       = 0;
256   pfd.cAccumBits                        = 0;
257   pfd.cAccumRedBits             = 0;
258   pfd.cAccumGreenBits           = 0;
259   pfd.cAccumBlueBits            = 0;
260   pfd.cAccumAlphaBits           = 0;
261 #if 0
262   pfd.cDepthBits                        = 32;
263   pfd.cStencilBits              = 0;
264 #else
265   pfd.cDepthBits                        = 24;
266   pfd.cStencilBits              = 8;
267 #endif
268   pfd.cAuxBuffers                       = 0;
269   pfd.iLayerType                        = layer_type;
270   pfd.bReserved                 = 0;
271   pfd.dwLayerMask                       = 0;
272   pfd.dwVisibleMask             = 0;
273   pfd.dwDamageMask              = 0;
274
275   /*
276    * Fill in the color bits...
277    */
278   pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
279   ppfd = &pfd;
280
281         /*
282          * Choose the pixel format that matches our demand
283          */
284   pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
285         if( pixelformat == 0 )
286                 return( FALSE );
287
288         /*
289          * We might have been called to check if the pixel format exists only
290          */
291         if( checkOnly )
292                 return( TRUE );
293
294         /*
295          * Finally, set the window's pixel format
296          */
297         return ( SetPixelFormat( window->Window.Device, pixelformat, ppfd ) ) ;
298 }
299 #endif
300
301 /*
302  * Sets the OpenGL context and the fgStructure "Current Window" pointer to the window
303  * structure passed in.
304  */
305 void fgSetWindow ( SFG_Window *window )
306 {
307 #if TARGET_HOST_UNIX_X11
308     /*
309          * Make the selected window's GLX context the current one
310      */
311     glXMakeCurrent(
312         fgDisplay.Display,
313         window->Window.Handle,
314         window->Window.Context
315     );
316
317 #elif TARGET_HOST_WIN32
318         /*
319          * Release the previous' context's device context
320          */
321         if( fgStructure.Window != NULL )
322                 ReleaseDC( fgStructure.Window->Window.Handle, fgStructure.Window->Window.Device );
323
324   if ( window )
325   {
326         /*
327            * We will care about releasing the device context later
328            */
329         window->Window.Device = GetDC( window->Window.Handle );
330
331         /*
332            * Set the new current context:
333            */
334         wglMakeCurrent( 
335                 window->Window.Device, 
336                   window->Window.Context 
337         );
338   }
339 #endif
340
341     /*
342      * Remember that we have changed the current window state
343      */
344     fgStructure.Window = window;
345 }
346
347
348 /*
349  * Opens a window. Requires a SFG_Window object created and attached
350  * to the freeglut structure. OpenGL context is created here.
351  */
352 void fgOpenWindow( SFG_Window* window, const char* title, int x, int y, int w, int h, GLboolean gameMode, int isSubWindow )
353 {
354 #if TARGET_HOST_UNIX_X11
355     XSetWindowAttributes winAttr;
356     XTextProperty textProperty;
357     XSizeHints sizeHints;
358     XWMHints wmHints;
359     unsigned long mask;
360
361     freeglut_assert_ready;
362
363     /*
364      * Save the window's single- or double-buffering state
365      */
366     window->Window.DoubleBuffered = ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0 ;
367
368     /*
369      * Here we are upon the stage. Have the visual selected.
370      */
371     window->Window.VisualInfo = fgChooseVisual();
372     if ( ! window->Window.VisualInfo )
373     {
374       /*
375        * The "fgChooseVisual" returned a null meaning that the visual context is not available.
376        * Try a couple of variations to see if they will work.
377        */
378       if ( ! ( fgState.DisplayMode & GLUT_DOUBLE ) )
379       {
380         /*
381          * Single buffering--try it doubled
382          */
383         fgState.DisplayMode |= GLUT_DOUBLE ;
384         window->Window.VisualInfo = fgChooseVisual();
385       }
386
387       /*
388        * GLUT also checks for multi-sampling, but I don't see that anywhere else in FREEGLUT
389        * so I won't bother with it for the moment.
390        */
391     }
392
393     assert( window->Window.VisualInfo != NULL );
394
395     /*
396      * Have the windows attributes set
397      *
398      * HINT: the masks should be updated when adding/removing callbacks.
399      *       This might speed up message processing. Is that true?
400      */
401     winAttr.event_mask        = StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
402                                 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyRelease |
403                                 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
404                                 PointerMotionMask | ButtonMotionMask;
405     winAttr.background_pixmap = None;
406     winAttr.background_pixel  = 0;
407     winAttr.border_pixel      = 0;
408
409     /*
410      * The color map is required, too
411      */
412     winAttr.colormap = XCreateColormap(
413         fgDisplay.Display, fgDisplay.RootWindow,
414         window->Window.VisualInfo->visual, AllocNone
415     );
416
417     /*
418      * This tells the XCreateWindow() what attributes are we supplying it with
419      */
420     mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
421
422     /*
423      * Have the window created now
424      */
425     window->Window.Handle = XCreateWindow(
426         fgDisplay.Display,
427         window->Parent == NULL ? fgDisplay.RootWindow : window->Parent->Window.Handle,
428         x, y, w, h, 0,
429         window->Window.VisualInfo->depth, InputOutput,
430         window->Window.VisualInfo->visual, mask,
431         &winAttr
432     );
433
434     /*
435      * The GLX context creation, possibly trying the direct context rendering
436      */
437     window->Window.Context = glXCreateContext(
438         fgDisplay.Display, window->Window.VisualInfo,
439         NULL, fgState.ForceDirectContext | fgState.TryDirectContext
440     );
441
442     /*
443      * Make sure the context is direct when the user wants it forced
444      */
445     if( fgState.ForceDirectContext && !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
446         fgError( "unable to force direct context rendering for window '%s'", title );
447
448     /*
449      * Set the new context as the current one. That's all about the window creation.
450      */
451     glXMakeCurrent(
452         fgDisplay.Display,
453         window->Window.Handle,
454         window->Window.Context
455     );
456
457     /*
458      * Assume the new window is visible by default
459      */
460     window->State.Visible = TRUE;
461
462     /*
463      * For the position and size hints -- make sure we are passing valid values
464      */
465     sizeHints.flags = 0;
466
467     if (fgState.Position.Use == TRUE) sizeHints.flags |= USPosition;
468     if (fgState.Size.Use     == TRUE) sizeHints.flags |= USSize;
469
470     /*
471      * Fill in the size hints values now (the x, y, width and height
472      * settings are obsolote, are there any more WMs that support them?)
473      */
474     sizeHints.x      = x; sizeHints.y      = y;
475     sizeHints.width  = w; sizeHints.height = h;
476
477     /*
478      * We can have forced all new windows start in iconified state:
479      */
480     wmHints.flags = StateHint;
481     wmHints.initial_state = (fgState.ForceIconic == FALSE) ? NormalState : IconicState;
482
483     /*
484      * Prepare the window and iconified window names...
485      */
486     XStringListToTextProperty( (char **) &title, 1, &textProperty );
487
488     /*
489      * Set the window's properties now
490      */
491     XSetWMProperties(
492         fgDisplay.Display,
493         window->Window.Handle,
494         &textProperty,
495         &textProperty,
496         0,
497         0,
498         &sizeHints,
499         &wmHints,
500         NULL
501     );
502
503     /*
504      * Make sure we are informed about the window deletion commands
505      */
506     XSetWMProtocols( fgDisplay.Display, window->Window.Handle, &fgDisplay.DeleteWindow, 1 );
507
508     /*
509      * Finally, have the window mapped to our display
510      */
511     XMapWindow( fgDisplay.Display, window->Window.Handle );
512
513     /*
514      * In game mode, move the viewport a bit to hide the decorations.
515      * This code depends on the XFree86 video mode extensions.
516      */
517     if( gameMode == TRUE )
518     {
519         /*
520          * This somehow fixes the glutGet() GLUT_WINDOW_X and GLUT_WINDOW_Y problem...
521          */
522         XMoveWindow( fgDisplay.Display, window->Window.Handle, x, y );
523
524 #       ifdef X_XF86VidModeSetViewPort
525
526         /*
527          * Set the newly created window as the current one...
528          */
529         fgSetWindow( window );
530
531         /*
532          * Move the viewport a bit down and right from top-left corner to hide the decorations
533          */
534         XF86VidModeSetViewPort(
535             fgDisplay.Display,
536             fgDisplay.Screen,
537             glutGet( GLUT_WINDOW_X ),
538             glutGet( GLUT_WINDOW_Y )
539         );
540
541 #       endif
542     }
543
544     /*
545      * If it's not double-buffered, make sure the rendering is done to the front buffer.
546      */
547     if ( ! window->Window.DoubleBuffered )
548     {
549       glDrawBuffer ( GL_FRONT ) ;
550       glReadBuffer ( GL_FRONT ) ;
551     }
552
553 #elif TARGET_HOST_WIN32
554
555         WNDCLASS wc;
556         int flags;
557         ATOM atom;
558
559     freeglut_assert_ready;
560
561         /*
562          * Grab the window class we have registered on glutInit():
563          */
564         atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
565         assert( atom != 0 );
566
567     if( gameMode == FALSE )
568     {
569       if ( !isSubWindow )
570       {
571         /*
572          * Update the window position and dimensions, taking account of window decorations
573          */
574
575                     x -= (GetSystemMetrics( SM_CXSIZEFRAME ) ); 
576                 y -= (GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION ) );
577         if ( y < 0 ) y = 0 ;
578                 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
579                 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 + GetSystemMetrics( SM_CYCAPTION );
580       }
581
582       /*
583              * Check if the user wants us to use the default position/size
584              */
585             if( fgState.Position.Use == FALSE ) { x = CW_USEDEFAULT; y = CW_USEDEFAULT; }
586             if( fgState.Size    .Use == FALSE ) { w = CW_USEDEFAULT; h = CW_USEDEFAULT; }
587
588             /*
589              * There's a small difference between creating the top, child and game mode windows
590              */
591             flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
592
593             if( window->Parent == NULL )
594                     flags |= WS_OVERLAPPEDWINDOW;
595             else
596                     flags |= WS_CHILD;
597     }
598     else
599     {
600         /*
601          * In game mode, the story is a little bit different...
602          */
603         assert( window->Parent == NULL );
604
605         /*
606          * Set the window creation flags appropriately to make the window entirely visible:
607          */
608         flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
609     }
610
611     /*
612      * Create the window now, passing the freeglut window structure as the parameter
613      */
614         window->Window.Handle = CreateWindow( 
615                 "FREEGLUT",
616         title,
617                 flags,
618         x, y, w, h,
619                 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
620                 (HMENU) NULL,
621                 fgDisplay.Instance,
622                 (LPVOID) window
623         );
624
625         /*
626      * Make sure window was created
627      */
628         assert( window->Window.Handle != NULL );
629
630     /*
631      * Show and update the main window. Hide the mouse cursor.
632      */
633     ShowWindow( window->Window.Handle, fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
634     UpdateWindow( window->Window.Handle );
635     ShowCursor( TRUE );
636
637 #endif
638
639     /*
640      * Set the newly created window as the current one
641      */
642     fgSetWindow( window );
643 }
644
645 /*
646  * Closes a window, destroying the frame and OpenGL context
647  */
648 void fgCloseWindow( SFG_Window* window )
649 {
650     freeglut_assert_ready;
651
652 #if TARGET_HOST_UNIX_X11
653     /*
654      * As easy as kill bunnies with axes. Destroy the context first:
655      */
656     glXDestroyContext( fgDisplay.Display, window->Window.Context );
657
658     /*
659      * Then have the window killed:
660      */
661     XDestroyWindow( fgDisplay.Display, window->Window.Handle );
662
663     /*
664      * Finally, flush the rests down the stream
665      */
666     XFlush( fgDisplay.Display );
667
668 #elif TARGET_HOST_WIN32
669         /*
670          * Send the WM_CLOSE message to the window now
671          */
672         SendMessage( 
673                 window->Window.Handle,
674                 WM_CLOSE,
675                 0,
676                 0
677         );
678
679 #endif
680 }
681
682
683 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
684
685 /*
686  * Creates a new top-level freeglut window
687  */
688 int FGAPIENTRY glutCreateWindow( const char* title )
689 {
690     /*
691      * Create a new window and return its unique ID number
692      */
693     return( fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
694                             fgState.Size.X, fgState.Size.Y, FALSE )->ID );
695 }
696
697 /*
698  * This function creates a sub window.
699  */
700 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
701 {
702     SFG_Window* window = NULL;
703     SFG_Window* parent = NULL;
704
705     freeglut_assert_ready;
706
707     /*
708      * Find a parent to the newly created window...
709      */
710     parent = fgWindowByID( parentID );
711
712     /*
713      * Fail if the parent has not been found
714      */
715     freeglut_return_val_if_fail( parent != NULL, 0 );
716
717     /*
718      * Create the new window
719      */
720     window = fgCreateWindow( parent, "", x, y, w, h, FALSE );
721
722     /*
723      * Return the new window's ID
724      */
725     return( window->ID );
726 }
727
728 /*
729  * Destroys a window and all of its subwindows
730  */
731 void FGAPIENTRY glutDestroyWindow( int windowID )
732 {
733   fgExecutionState ExecState = fgState.ExecState ;
734
735   /*
736    * Grab the freeglut window pointer from the structure
737    */
738   SFG_Window* window = fgWindowByID( windowID );
739   freeglut_return_if_fail( window != NULL );
740
741   /*
742    * There is a function that performs all needed steps
743    * defined in freeglut_structure.c. Let's use it:
744    */
745   fgAddToWindowDestroyList( window, TRUE );
746
747   /*
748    * Since the "fgAddToWindowDestroyList" function could easily have set the "ExecState"
749    * to stop, let's set it back to what it was.
750    */
751   fgState.ExecState = ExecState ;
752 }
753
754 /*
755  * This function selects the current window
756  */
757 void FGAPIENTRY glutSetWindow( int ID )
758 {
759     SFG_Window* window = NULL;
760
761     /*
762      * Make sure we don't get called too early
763      */
764     freeglut_assert_ready;
765
766     /*
767      * Be wise. Be wise. Be wise. Be quick.
768      */
769     if( fgStructure.Window != NULL )
770         if( fgStructure.Window->ID == ID )
771             return;
772
773     /*
774      * Now we are sure there is sense in looking for the window
775      */
776     window = fgWindowByID( ID );
777
778     /*
779      * In the case of an utter failure...
780      */
781     if( window == NULL )
782     {
783         /*
784          * ...issue a warning message and keep rolling on
785          */
786         fgWarning( "glutSetWindow(): window ID %i not found!", ID );
787         return;
788     }
789
790     fgSetWindow ( window ) ;
791 }
792
793 /*
794  * This function returns the ID number of the current window, 0 if none exists
795  */
796 int FGAPIENTRY glutGetWindow( void )
797 {
798     freeglut_assert_ready;
799
800     /*
801      * Do we have a current window selected?
802      */
803     if( fgStructure.Window == NULL )
804     {
805         /*
806          * Nope. Return zero to mark the state.
807          */
808         return( 0 );
809     }
810
811     /*
812      * Otherwise, return the ID of the current window
813      */
814     return( fgStructure.Window->ID );
815 }
816
817 /*
818  * This function makes the current window visible
819  */
820 void FGAPIENTRY glutShowWindow( void )
821 {
822     freeglut_assert_ready; freeglut_assert_window;
823
824 #if TARGET_HOST_UNIX_X11
825     /*
826      * Showing the window is done via mapping under X
827      */
828     XMapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
829     XFlush( fgDisplay.Display );
830
831 #elif TARGET_HOST_WIN32
832         /*
833          * Restore the window's originial position and size
834          */
835         ShowWindow( fgStructure.Window->Window.Handle, SW_SHOW );
836
837 #endif
838 }
839
840 /*
841  * This function hides the current window
842  */
843 void FGAPIENTRY glutHideWindow( void )
844 {
845     freeglut_assert_ready; freeglut_assert_window;
846
847 #if TARGET_HOST_UNIX_X11
848     /*
849      * The way we hide a window depends on if we're dealing
850      * with a top-level or children one...
851      */
852     if( fgStructure.Window->Parent == NULL )
853     {
854         /*
855          * This is a top-level window
856          */
857         XWithdrawWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, fgDisplay.Screen );
858     }
859     else
860     {
861         /*
862          * Nope, it's a child window
863          */
864         XUnmapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
865     }
866
867     /*
868      * Flush the X state now
869      */
870     XFlush( fgDisplay.Display );
871
872 #elif TARGET_HOST_WIN32
873         /*
874          * Hide the window
875          */
876         ShowWindow( fgStructure.Window->Window.Handle, SW_HIDE );
877
878 #endif
879 }
880
881 /*
882  * Iconify the current window (top-level windows only)
883  */
884 void FGAPIENTRY glutIconifyWindow( void )
885 {
886     freeglut_assert_ready; freeglut_assert_window;
887
888 #if TARGET_HOST_UNIX_X11
889     /*
890      * Iconify the window and flush the X state
891      */
892     XIconifyWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, fgDisplay.Screen );
893     XFlush( fgDisplay.Display );
894
895 #elif TARGET_HOST_WIN32
896         /*
897          * Minimize the current window (this should be the same as X window iconifying)
898          */
899         ShowWindow( fgStructure.Window->Window.Handle, SW_MINIMIZE );
900
901 #endif
902 }
903
904 /*
905  * Set the current window's title
906  */
907 void FGAPIENTRY glutSetWindowTitle( const char* title )
908 {
909         freeglut_assert_ready; freeglut_assert_window;
910
911     /*
912      * Works only for top-level windows
913      */
914     if( fgStructure.Window->Parent != NULL )
915         return;
916
917 #if TARGET_HOST_UNIX_X11
918         {
919                 XTextProperty text;
920
921                 /*
922                  * Prepare the text properties
923                  */
924                 text.value = (unsigned char *) title;
925                 text.encoding = XA_STRING;
926                 text.format = 8;
927                 text.nitems = strlen( title );
928
929                 /*
930                  * Set the title now
931                  */
932                 XSetWMName(
933                         fgDisplay.Display,
934                         fgStructure.Window->Window.Handle,
935                         &text
936                 );
937
938                 /*
939                  * Have the X display state flushed
940                  */
941                 XFlush( fgDisplay.Display );
942         }
943
944 #elif TARGET_HOST_WIN32
945         /*
946          * This seems to be a bit easier under Win32
947          */
948         SetWindowText( fgStructure.Window->Window.Handle, title );
949
950 #endif
951 }
952
953 /*
954  * Set the current window's iconified title
955  */
956 void FGAPIENTRY glutSetIconTitle( const char* title )
957 {
958     freeglut_assert_ready; freeglut_assert_window;
959
960     /*
961      * Works only for top-level windows
962      */
963     if( fgStructure.Window->Parent != NULL )
964         return;
965
966 #if TARGET_HOST_UNIX_X11
967         {
968                 XTextProperty text;
969
970                 /*
971                  * Prepare the text properties
972                  */
973                 text.value = (unsigned char *) title;
974                 text.encoding = XA_STRING;
975                 text.format = 8;
976                 text.nitems = strlen( title );
977
978                 /*
979                  * Set the title now
980                  */
981                 XSetWMIconName(
982                         fgDisplay.Display,
983                         fgStructure.Window->Window.Handle,
984                         &text
985                 );
986
987                 /*
988                  * Have the X display state flushed
989                  */
990                 XFlush( fgDisplay.Display );
991         }
992
993 #elif TARGET_HOST_WIN32
994         /*
995          * This seems to be a bit easier under Win32
996          */
997         SetWindowText( fgStructure.Window->Window.Handle, title );
998
999 #endif
1000 }
1001
1002 /*
1003  * Change the current window's size
1004  */
1005 void FGAPIENTRY glutReshapeWindow( int width, int height )
1006 {
1007     freeglut_assert_ready; freeglut_assert_window;
1008
1009 #if TARGET_HOST_UNIX_X11
1010     /*
1011      * Resize the window and flush the X state
1012      */
1013     XResizeWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, width, height );
1014     XFlush( fgDisplay.Display );
1015
1016 #elif TARGET_HOST_WIN32
1017         {
1018                 RECT winRect;
1019     int x, y ;
1020
1021                 /*
1022                  * First off, grab the current window's position
1023                  */
1024                 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
1025     x = winRect.left ;
1026     y = winRect.top ;
1027
1028     if ( fgStructure.Window->Parent == NULL )  /* If this is not a subwindow ... */
1029     {
1030       /*
1031        * Adjust the size of the window to allow for the size of the frame
1032        */
1033                 width += (GetSystemMetrics( SM_CXSIZEFRAME ) - 1)*2;
1034                 height += (GetSystemMetrics( SM_CYSIZEFRAME ) - 1)*2 + GetSystemMetrics( SM_CYCAPTION );
1035     }
1036     else  /* This is a subwindow, get the parent window's position and subtract it off */
1037     {
1038       GetWindowRect ( fgStructure.Window->Parent->Window.Handle, &winRect ) ;
1039       x -= winRect.left + GetSystemMetrics( SM_CXSIZEFRAME ) ;
1040       y -= winRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION ) ;
1041     }
1042
1043                 /*
1044                  * Resize the window, forcing a redraw to happen
1045                  */
1046                 MoveWindow(
1047                         fgStructure.Window->Window.Handle,
1048                         x,
1049                         y,
1050                         width,
1051                         height,
1052                         TRUE
1053                 );
1054         }
1055 #endif
1056 }
1057
1058 /*
1059  * Change the current window's position
1060  */
1061 void FGAPIENTRY glutPositionWindow( int x, int y )
1062 {
1063     freeglut_assert_ready; freeglut_assert_window;
1064
1065 #if TARGET_HOST_UNIX_X11
1066     /*
1067      * Reposition the window and flush the X state
1068      */
1069     XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, x, y );
1070     XFlush( fgDisplay.Display );
1071
1072 #elif TARGET_HOST_WIN32
1073         {
1074                 RECT winRect;
1075
1076                 /*
1077                  * First off, grab the current window's position
1078                  */
1079                 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
1080
1081     if ( fgStructure.Window->Parent == NULL )  /* If this is not a subwindow ... */
1082     {
1083       /*
1084        * Adjust the position of the window to allow for the size of the frame
1085        */
1086                 x -= (GetSystemMetrics( SM_CXSIZEFRAME ) - 1); 
1087                 y -= (GetSystemMetrics( SM_CYSIZEFRAME ) - 1 + GetSystemMetrics( SM_CYCAPTION ));
1088       if ( y < 0 ) y = 0 ;
1089     }
1090
1091     /*
1092                  * Reposition the window, forcing a redraw to happen
1093                  */
1094                 MoveWindow(
1095                         fgStructure.Window->Window.Handle,
1096                         x,
1097                         y,
1098                         winRect.right - winRect.left,
1099                         winRect.bottom - winRect.top,
1100                         TRUE
1101                 );
1102         }
1103
1104 #endif
1105 }
1106
1107 /*
1108  * Lowers the current window (by Z order change)
1109  */
1110 void FGAPIENTRY glutPushWindow( void )
1111 {
1112     freeglut_assert_ready; freeglut_assert_window;
1113
1114 #if TARGET_HOST_UNIX_X11
1115     /*
1116      * Lower the current window
1117      */
1118     XLowerWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
1119
1120 #elif TARGET_HOST_WIN32
1121         /*
1122          * Set the new window's Z position, not affecting the rest of the settings:
1123          */
1124         SetWindowPos(
1125                 fgStructure.Window->Window.Handle,
1126                 HWND_BOTTOM,
1127                 0, 0, 0, 0,
1128                 SWP_NOSIZE | SWP_NOMOVE
1129         );
1130
1131 #endif
1132 }
1133
1134 /*
1135  * Raises the current window (by Z order change)
1136  */
1137 void FGAPIENTRY glutPopWindow( void )
1138 {
1139     freeglut_assert_ready; freeglut_assert_window;
1140
1141 #if TARGET_HOST_UNIX_X11
1142     /*
1143      * Raise the current window
1144      */
1145     XRaiseWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
1146
1147 #elif TARGET_HOST_WIN32
1148         /*
1149          * Set the new window's Z position, not affecting the rest of the settings:
1150          */
1151         SetWindowPos(
1152                 fgStructure.Window->Window.Handle,
1153                 HWND_TOP,
1154                 0, 0, 0, 0,
1155                 SWP_NOSIZE | SWP_NOMOVE
1156         );
1157
1158 #endif
1159 }
1160
1161 /*
1162  * Resize the current window so that it fits the whole screen
1163  */
1164 void FGAPIENTRY glutFullScreen( void )
1165 {
1166     freeglut_assert_ready; freeglut_assert_window;
1167
1168     /*
1169      * Just have the window repositioned and resized
1170      */
1171     glutPositionWindow( 0, 0 );
1172
1173     glutReshapeWindow(
1174         fgDisplay.ScreenWidth,
1175         fgDisplay.ScreenHeight
1176     );
1177 }
1178
1179 /*** END OF FILE ***/
1180
1181
1182
1183
1184
1185
1186
1187
1188