src/freeglut_window.c:551 Removed extra carriage return for Windows users
[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 #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      * Here we are upon the stage. Have the visual selected.
365      */
366     window->Window.VisualInfo = fgChooseVisual();
367     if ( ! window->Window.VisualInfo )
368     {
369       /*
370        * The "fgChooseVisual" returned a null meaning that the visual context is not available.
371        * Try a couple of variations to see if they will work.
372        */
373       if ( ! ( fgState.DisplayMode & GLUT_DOUBLE ) )
374       {
375         /*
376          * Single buffering--try it doubled
377          */
378         fgState.DisplayMode |= GLUT_DOUBLE ;
379         window->Window.VisualInfo = fgChooseVisual();
380       }
381
382       /*
383        * GLUT also checks for multi-sampling, but I don't see that anywhere else in FREEGLUT
384        * so I won't bother with it for the moment.
385        */
386     }
387
388     assert( window->Window.VisualInfo != NULL );
389
390     /*
391      * Have the windows attributes set
392      *
393      * HINT: the masks should be updated when adding/removing callbacks.
394      *       This might speed up message processing. Is that true?
395      */
396     winAttr.event_mask        = StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
397                                 ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyRelease |
398                                 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
399                                 PointerMotionMask | ButtonMotionMask;
400     winAttr.background_pixmap = None;
401     winAttr.background_pixel  = 0;
402     winAttr.border_pixel      = 0;
403
404     /*
405      * The color map is required, too
406      */
407     winAttr.colormap = XCreateColormap(
408         fgDisplay.Display, fgDisplay.RootWindow,
409         window->Window.VisualInfo->visual, AllocNone
410     );
411
412     /*
413      * This tells the XCreateWindow() what attributes are we supplying it with
414      */
415     mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
416
417     /*
418      * Have the window created now
419      */
420     window->Window.Handle = XCreateWindow(
421         fgDisplay.Display,
422         window->Parent == NULL ? fgDisplay.RootWindow : window->Parent->Window.Handle,
423         x, y, w, h, 0,
424         window->Window.VisualInfo->depth, InputOutput,
425         window->Window.VisualInfo->visual, mask,
426         &winAttr
427     );
428
429     /*
430      * The GLX context creation, possibly trying the direct context rendering
431      *  or else use the current context if the user has so specified
432      */
433     if ( fgState.UseCurrentContext == TRUE )
434     {
435       window->Window.Context = glXGetCurrentContext();
436
437       if ( ! window->Window.Context )
438         window->Window.Context = glXCreateContext(
439             fgDisplay.Display, window->Window.VisualInfo,
440             NULL, fgState.ForceDirectContext | fgState.TryDirectContext
441         );
442     }
443     else
444       window->Window.Context = glXCreateContext(
445           fgDisplay.Display, window->Window.VisualInfo,
446           NULL, fgState.ForceDirectContext | fgState.TryDirectContext
447       );
448
449     /*
450      * Make sure the context is direct when the user wants it forced
451      */
452     if( fgState.ForceDirectContext && !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
453         fgError( "unable to force direct context rendering for window '%s'", title );
454
455     /*
456      * Set the new context as the current one. That's all about the window creation.
457      */
458     glXMakeCurrent(
459         fgDisplay.Display,
460         window->Window.Handle,
461         window->Window.Context
462     );
463
464     /*
465      * Assume the new window is visible by default
466      */
467     window->State.Visible = TRUE;
468
469     /*
470      * For the position and size hints -- make sure we are passing valid values
471      */
472     sizeHints.flags = 0;
473
474     if (fgState.Position.Use == TRUE) sizeHints.flags |= USPosition;
475     if (fgState.Size.Use     == TRUE) sizeHints.flags |= USSize;
476
477     /*
478      * Fill in the size hints values now (the x, y, width and height
479      * settings are obsolote, are there any more WMs that support them?)
480      */
481     sizeHints.x      = x; sizeHints.y      = y;
482     sizeHints.width  = w; sizeHints.height = h;
483
484     /*
485      * We can have forced all new windows start in iconified state:
486      */
487     wmHints.flags = StateHint;
488     wmHints.initial_state = (fgState.ForceIconic == FALSE) ? NormalState : IconicState;
489
490     /*
491      * Prepare the window and iconified window names...
492      */
493     XStringListToTextProperty( (char **) &title, 1, &textProperty );
494
495     /*
496      * Set the window's properties now
497      */
498     XSetWMProperties(
499         fgDisplay.Display,
500         window->Window.Handle,
501         &textProperty,
502         &textProperty,
503         0,
504         0,
505         &sizeHints,
506         &wmHints,
507         NULL
508     );
509
510     /*
511      * Make sure we are informed about the window deletion commands
512      */
513     XSetWMProtocols( fgDisplay.Display, window->Window.Handle, &fgDisplay.DeleteWindow, 1 );
514
515     /*
516      * Finally, have the window mapped to our display
517      */
518     XMapWindow( fgDisplay.Display, window->Window.Handle );
519
520     /*
521      * In game mode, move the viewport a bit to hide the decorations.
522      * This code depends on the XFree86 video mode extensions.
523      */
524     if( gameMode == TRUE )
525     {
526         /*
527          * This somehow fixes the glutGet() GLUT_WINDOW_X and GLUT_WINDOW_Y problem...
528          */
529         XMoveWindow( fgDisplay.Display, window->Window.Handle, x, y );
530
531 #       ifdef X_XF86VidModeSetViewPort
532
533         /*
534          * Set the newly created window as the current one...
535          */
536         fgSetWindow( window );
537
538         /*
539          * Move the viewport a bit down and right from top-left corner to hide the decorations
540          */
541         XF86VidModeSetViewPort(
542             fgDisplay.Display,
543             fgDisplay.Screen,
544             glutGet( GLUT_WINDOW_X ),
545             glutGet( GLUT_WINDOW_Y )
546         );
547
548 #       endif
549     }
550
551 #elif TARGET_HOST_WIN32
552
553         WNDCLASS wc;
554         int flags;
555         ATOM atom;
556
557     freeglut_assert_ready;
558
559         /*
560          * Grab the window class we have registered on glutInit():
561          */
562         atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
563         assert( atom != 0 );
564
565     if( gameMode == FALSE )
566     {
567       if ( !isSubWindow )
568       {
569         /*
570          * Update the window dimensions, taking account of window decorations.
571          * "freeglut" is to create the window with the outside of its border at (x,y)
572          * and with dimensions (w,h).
573          */
574                 w += (GetSystemMetrics( SM_CXSIZEFRAME ) )*2;
575                 h += (GetSystemMetrics( SM_CYSIZEFRAME ) )*2 + GetSystemMetrics( SM_CYCAPTION );
576       }
577
578
579       /*
580              * Check if the user wants us to use the default position/size
581              */
582             if( fgState.Position.Use == FALSE ) { x = CW_USEDEFAULT; y = CW_USEDEFAULT; }
583           
584   if( fgState.Size    .Use == FALSE ) { w = CW_USEDEFAULT; h = CW_USEDEFAULT; }
585
586         
587     /*
588              * There's a small difference between creating the top, child and game mode windows
589              */
590             flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
591
592             if( window->Parent == NULL )
593                     flags |= WS_OVERLAPPEDWINDOW;
594             else
595                     flags |= WS_CHILD;
596     }
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     /*
613      * Create the window now, passing the freeglut window structure as the parameter
614      */
615         window->Window.Handle = CreateWindow( 
616                 "FREEGLUT",
617         title,
618                 flags,
619         x, y, w, h,
620                 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
621                 (HMENU) NULL,
622                 fgDisplay.Instance,
623                 (LPVOID) window
624         );
625
626         /*
627      * Make sure window was created
628      */
629         assert( window->Window.Handle != NULL );
630
631     /*
632      * Show and update the main window. Hide the mouse cursor.
633      */
634     ShowWindow( window->Window.Handle, fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
635     UpdateWindow( window->Window.Handle );
636     ShowCursor( TRUE );
637
638 #endif
639
640     /*
641      * Save the window's single- or double-buffering state
642      */
643     window->Window.DoubleBuffered = ( fgState.DisplayMode & GLUT_DOUBLE ) ? 1 : 0 ;
644
645     /*
646      * If it's not double-buffered, make sure the rendering is done to the front buffer.
647      */
648     if ( ! window->Window.DoubleBuffered )
649     {
650       glDrawBuffer ( GL_FRONT ) ;
651       glReadBuffer ( GL_FRONT ) ;
652     }
653
654
655     /*
656      * Set the newly created window as the current one
657      */
658     fgSetWindow( window );
659 }
660
661
662 /*
663  * Closes a window, destroying the frame and OpenGL context
664  */
665 void fgCloseWindow( SFG_Window* window )
666 {
667     freeglut_assert_ready;
668
669 #if TARGET_HOST_UNIX_X11
670     /*
671      * As easy as kill bunnies with axes. Destroy the context first:
672      */
673     glXDestroyContext( fgDisplay.Display, window->Window.Context );
674
675     /*
676      * Then have the window killed:
677      */
678     XDestroyWindow( fgDisplay.Display, window->Window.Handle );
679
680     /*
681      * Finally, flush the rests down the stream
682      */
683     XFlush( fgDisplay.Display );
684
685 #elif TARGET_HOST_WIN32
686         /*
687          * Send the WM_CLOSE message to the window now
688          */
689         SendMessage( 
690                 window->Window.Handle,
691                 WM_CLOSE,
692                 0,
693                 0
694         );
695
696 #endif
697 }
698
699
700
701 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
702
703 /*
704  * Creates a new top-level freeglut window
705  */
706 int FGAPIENTRY glutCreateWindow( const char* title )
707 {
708     /*
709      * Create a new window and return its unique ID number
710      */
711     return( fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
712                             fgState.Size.X, fgState.Size.Y, FALSE )->ID );
713 }
714
715
716 /*
717  * This function creates a sub window.
718  */
719 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
720 {
721     SFG_Window* window = NULL;
722     SFG_Window* parent = NULL;
723
724     freeglut_assert_ready;
725
726     /*
727      * Find a parent to the newly created window...
728      */
729     parent = fgWindowByID( parentID );
730
731     /*
732      * Fail if the parent has not been found
733      */
734     freeglut_return_val_if_fail( parent != NULL, 0 );
735
736     /*
737      * Create the new window
738      */
739     window = fgCreateWindow( parent, "", x, y, w, h, FALSE );
740
741     /*
742      * Return the new window's ID
743      */
744     return( window->ID );
745 }
746
747
748 /*
749  * Destroys a window and all of its subwindows
750  */
751 void FGAPIENTRY glutDestroyWindow( int windowID )
752 {
753   fgExecutionState ExecState = fgState.ExecState ;
754
755   /*
756    * Grab the freeglut window pointer from the structure
757    */
758   SFG_Window* window = fgWindowByID( windowID );
759   freeglut_return_if_fail( window != NULL );
760
761   /*
762    * There is a function that performs all needed steps
763    * defined in freeglut_structure.c. Let's use it:
764    */
765   fgAddToWindowDestroyList( window, TRUE );
766
767   /*
768    * Since the "fgAddToWindowDestroyList" function could easily have set the "ExecState"
769    * to stop, let's set it back to what it was.
770    */
771   fgState.ExecState = ExecState ;
772 }
773
774
775 /*
776  * This function selects the current window
777  */
778 void FGAPIENTRY glutSetWindow( int ID )
779 {
780     SFG_Window* window = NULL;
781
782     /*
783      * Make sure we don't get called too early
784      */
785     freeglut_assert_ready;
786
787     /*
788      * Be wise. Be wise. Be wise. Be quick.
789      */
790     if( fgStructure.Window != NULL )
791         if( fgStructure.Window->ID == ID )
792             return;
793
794     /*
795      * Now we are sure there is sense in looking for the window
796      */
797     window = fgWindowByID( ID );
798
799     /*
800      * In the case of an utter failure...
801      */
802     if( window == NULL )
803     {
804         /*
805          * ...issue a warning message and keep rolling on
806          */
807         fgWarning( "glutSetWindow(): window ID %i not found!", ID );
808         return;
809     }
810
811
812     fgSetWindow ( window ) ;
813 }
814
815
816 /*
817  * This function returns the ID number of the current window, 0 if none exists
818  */
819 int FGAPIENTRY glutGetWindow( void )
820 {
821     freeglut_assert_ready;
822
823     /*
824      * Do we have a current window selected?
825      */
826     if( fgStructure.Window == NULL )
827     {
828         /*
829          * Nope. Return zero to mark the state.
830          */
831         return( 0 );
832     }
833
834
835     /*
836      * Otherwise, return the ID of the current window
837      */
838     return( fgStructure.Window->ID );
839 }
840
841
842 /*
843  * This function makes the current window visible
844  */
845 void FGAPIENTRY glutShowWindow( void )
846 {
847     freeglut_assert_ready; freeglut_assert_window;
848
849 #if TARGET_HOST_UNIX_X11
850     /*
851      * Showing the window is done via mapping under X
852      */
853     XMapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
854     XFlush( fgDisplay.Display );
855
856 #elif TARGET_HOST_WIN32
857         /*
858          * Restore the window's originial position and size
859          */
860         ShowWindow( fgStructure.Window->Window.Handle, SW_SHOW );
861
862 #endif
863 }
864
865
866 /*
867  * This function hides the current window
868  */
869 void FGAPIENTRY glutHideWindow( void )
870 {
871     freeglut_assert_ready; freeglut_assert_window;
872
873 #if TARGET_HOST_UNIX_X11
874     /*
875      * The way we hide a window depends on if we're dealing
876      * with a top-level or children one...
877      */
878     if( fgStructure.Window->Parent == NULL )
879     {
880         /*
881          * This is a top-level window
882          */
883         XWithdrawWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, fgDisplay.Screen );
884     }
885
886     else
887     {
888         /*
889          * Nope, it's a child window
890          */
891         XUnmapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
892     }
893
894
895     /*
896      * Flush the X state now
897      */
898     XFlush( fgDisplay.Display );
899
900 #elif TARGET_HOST_WIN32
901         /*
902          * Hide the window
903          */
904         ShowWindow( fgStructure.Window->Window.Handle, SW_HIDE );
905
906 #endif
907 }
908
909
910 /*
911  * Iconify the current window (top-level windows only)
912  */
913 void FGAPIENTRY glutIconifyWindow( void )
914 {
915     freeglut_assert_ready; freeglut_assert_window;
916
917 #if TARGET_HOST_UNIX_X11
918     /*
919      * Iconify the window and flush the X state
920      */
921     XIconifyWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, fgDisplay.Screen );
922     XFlush( fgDisplay.Display );
923
924 #elif TARGET_HOST_WIN32
925         /*
926          * Minimize the current window (this should be the same as X window iconifying)
927          */
928         ShowWindow( fgStructure.Window->Window.Handle, SW_MINIMIZE );
929
930 #endif
931 }
932
933
934 /*
935  * Set the current window's title
936  */
937 void FGAPIENTRY glutSetWindowTitle( const char* title )
938 {
939         freeglut_assert_ready; freeglut_assert_window;
940
941     /*
942      * Works only for top-level windows
943      */
944     if( fgStructure.Window->Parent != NULL )
945         return;
946
947 #if TARGET_HOST_UNIX_X11
948         {
949                 XTextProperty text;
950
951                 /*
952                  * Prepare the text properties
953                  */
954                 text.value = (unsigned char *) title;
955                 text.encoding = XA_STRING;
956                 text.format = 8;
957                 text.nitems = strlen( title );
958
959                 /*
960                  * Set the title now
961                  */
962                 XSetWMName(
963                         fgDisplay.Display,
964                         fgStructure.Window->Window.Handle,
965                         &text
966                 );
967
968                 /*
969                  * Have the X display state flushed
970                  */
971                 XFlush( fgDisplay.Display );
972         }
973
974
975 #elif TARGET_HOST_WIN32
976         /*
977          * This seems to be a bit easier under Win32
978          */
979         SetWindowText( fgStructure.Window->Window.Handle, title );
980
981 #endif
982 }
983
984
985 /*
986  * Set the current window's iconified title
987  */
988 void FGAPIENTRY glutSetIconTitle( const char* title )
989 {
990     freeglut_assert_ready; freeglut_assert_window;
991
992     /*
993      * Works only for top-level windows
994      */
995     if( fgStructure.Window->Parent != NULL )
996         return;
997
998 #if TARGET_HOST_UNIX_X11
999         {
1000                 XTextProperty text;
1001
1002                 /*
1003                  * Prepare the text properties
1004                  */
1005                 text.value = (unsigned char *) title;
1006                 text.encoding = XA_STRING;
1007                 text.format = 8;
1008                 text.nitems = strlen( title );
1009
1010                 /*
1011                  * Set the title now
1012                  */
1013                 XSetWMIconName(
1014                         fgDisplay.Display,
1015                         fgStructure.Window->Window.Handle,
1016                         &text
1017                 );
1018
1019                 /*
1020                  * Have the X display state flushed
1021                  */
1022                 XFlush( fgDisplay.Display );
1023         }
1024
1025
1026 #elif TARGET_HOST_WIN32
1027         /*
1028          * This seems to be a bit easier under Win32
1029          */
1030         SetWindowText( fgStructure.Window->Window.Handle, title );
1031
1032 #endif
1033 }
1034
1035
1036 /*
1037  * Change the current window's size
1038  */
1039 void FGAPIENTRY glutReshapeWindow( int width, int height )
1040 {
1041     freeglut_assert_ready; freeglut_assert_window;
1042
1043 #if TARGET_HOST_UNIX_X11
1044     /*
1045      * Resize the window and flush the X state
1046      */
1047     XResizeWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, width, height );
1048     XFlush( fgDisplay.Display );
1049
1050 #elif TARGET_HOST_WIN32
1051         {
1052                 RECT winRect;
1053     int x, y ;
1054
1055                 /*
1056                  * First off, grab the current window's position
1057                  */
1058                 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
1059     x = winRect.left ;
1060     y = winRect.top ;
1061
1062     if ( fgStructure.Window->Parent == NULL )  /* If this is not a subwindow ... */
1063     {
1064       /*
1065        * Adjust the size of the window to allow for the size of the frame
1066        */
1067                 width += GetSystemMetrics( SM_CXSIZEFRAME ) * 2;
1068                 height += GetSystemMetrics( SM_CYSIZEFRAME ) * 2 + GetSystemMetrics( SM_CYCAPTION );
1069     }
1070
1071     else  /* This is a subwindow, get the parent window's position and subtract it off */
1072     {
1073       GetWindowRect ( fgStructure.Window->Parent->Window.Handle, &winRect ) ;
1074       x -= winRect.left + GetSystemMetrics( SM_CXSIZEFRAME ) ;
1075       y -= winRect.top + GetSystemMetrics( SM_CYSIZEFRAME ) + GetSystemMetrics( SM_CYCAPTION ) ;
1076     }
1077
1078
1079                 /*
1080                  * Resize the window, forcing a redraw to happen
1081                  */
1082                 MoveWindow(
1083                         fgStructure.Window->Window.Handle,
1084                         x,
1085                         y,
1086                         width,
1087                         height,
1088                         TRUE
1089                 );
1090         }
1091
1092 #endif
1093 }
1094
1095
1096 /*
1097  * Change the current window's position
1098  */
1099 void FGAPIENTRY glutPositionWindow( int x, int y )
1100 {
1101     freeglut_assert_ready; freeglut_assert_window;
1102
1103 #if TARGET_HOST_UNIX_X11
1104     /*
1105      * Reposition the window and flush the X state
1106      */
1107     XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, x, y );
1108     XFlush( fgDisplay.Display );
1109
1110 #elif TARGET_HOST_WIN32
1111         {
1112                 RECT winRect;
1113
1114                 /*
1115                  * First off, grab the current window's position
1116                  */
1117                 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
1118
1119     /*
1120                  * Reposition the window, forcing a redraw to happen
1121                  */
1122                 MoveWindow(
1123                         fgStructure.Window->Window.Handle,
1124                         x,
1125                         y,
1126                         winRect.right - winRect.left,
1127                         winRect.bottom - winRect.top,
1128                         TRUE
1129                 );
1130         }
1131
1132
1133 #endif
1134 }
1135
1136
1137 /*
1138  * Lowers the current window (by Z order change)
1139  */
1140 void FGAPIENTRY glutPushWindow( void )
1141 {
1142     freeglut_assert_ready; freeglut_assert_window;
1143
1144 #if TARGET_HOST_UNIX_X11
1145     /*
1146      * Lower the current window
1147      */
1148     XLowerWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
1149
1150 #elif TARGET_HOST_WIN32
1151         /*
1152          * Set the new window's Z position, not affecting the rest of the settings:
1153          */
1154         SetWindowPos(
1155                 fgStructure.Window->Window.Handle,
1156                 HWND_BOTTOM,
1157                 0, 0, 0, 0,
1158                 SWP_NOSIZE | SWP_NOMOVE
1159         );
1160
1161 #endif
1162 }
1163
1164
1165 /*
1166  * Raises the current window (by Z order change)
1167  */
1168 void FGAPIENTRY glutPopWindow( void )
1169 {
1170     freeglut_assert_ready; freeglut_assert_window;
1171
1172 #if TARGET_HOST_UNIX_X11
1173     /*
1174      * Raise the current window
1175      */
1176     XRaiseWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
1177
1178 #elif TARGET_HOST_WIN32
1179         /*
1180          * Set the new window's Z position, not affecting the rest of the settings:
1181          */
1182         SetWindowPos(
1183                 fgStructure.Window->Window.Handle,
1184                 HWND_TOP,
1185                 0, 0, 0, 0,
1186                 SWP_NOSIZE | SWP_NOMOVE
1187         );
1188
1189 #endif
1190 }
1191
1192
1193 /*
1194  * Resize the current window so that it fits the whole screen
1195  */
1196 void FGAPIENTRY glutFullScreen( void )
1197 {
1198     freeglut_assert_ready; freeglut_assert_window;
1199
1200     /*
1201      * Just have the window repositioned and resized
1202      */
1203     glutPositionWindow( 0, 0 );
1204
1205     glutReshapeWindow(
1206         fgDisplay.ScreenWidth,
1207         fgDisplay.ScreenHeight
1208     );
1209 }
1210
1211
1212 /*
1213  * A.Donev: Set and retrieve the window's user data
1214  */
1215 void* FGAPIENTRY glutGetWindowData( void )
1216 {
1217    return(fgStructure.Window->UserData);
1218 }
1219
1220
1221 void FGAPIENTRY glutSetWindowData(void* data)
1222 {
1223   fgStructure.Window->UserData=data;
1224 }
1225
1226
1227 /*** END OF FILE ***/