4 * Window management methods.
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
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:
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
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.
32 #define G_LOG_DOMAIN "freeglut-window"
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
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
60 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
63 * Chooses a visual basing on the current display mode settings
65 #if TARGET_HOST_UNIX_X11
66 XVisualInfo* fgChooseVisual( void )
68 int bufferSize[] = { 16, 12, 8, 4, 2, 1 };
69 GLboolean wantIndexedMode = FALSE;
74 * First we have to process the display mode settings...
76 # define ATTRIB(a) attributes[where++]=a;
79 * Decide if we want a true or indexed color visual:
81 if( !(fgState.DisplayMode & GLUT_INDEX) )
84 * We are sure that there will be R, B and B components requested:
87 ATTRIB( GLX_RED_SIZE ); ATTRIB( 1 );
88 ATTRIB( GLX_GREEN_SIZE ); ATTRIB( 1 );
89 ATTRIB( GLX_BLUE_SIZE ); ATTRIB( 1 );
92 * Check if the A component is required, too:
94 if( fgState.DisplayMode & GLUT_ALPHA )
96 ATTRIB( GLX_ALPHA_SIZE ); ATTRIB( 1 );
102 * We've got an indexed color request
104 ATTRIB( GLX_BUFFER_SIZE ); ATTRIB( 8 );
107 * Set the 'I want indexed mode' switch
109 wantIndexedMode = TRUE;
113 * We can have double or single buffered contexts created
115 if( fgState.DisplayMode & GLUT_DOUBLE )
117 ATTRIB( GLX_DOUBLEBUFFER );
121 * Stereoscopy seems a nice thing to have
123 if( fgState.DisplayMode & GLUT_STEREO )
125 ATTRIB( GLX_STEREO );
129 * Depth buffer is almost always required
131 if( fgState.DisplayMode & GLUT_DEPTH )
133 ATTRIB( GLX_DEPTH_SIZE ); ATTRIB( 1 );
139 if( fgState.DisplayMode & GLUT_STENCIL )
141 ATTRIB( GLX_STENCIL_SIZE ); ATTRIB( 1 );
145 * And finally the accumulation buffers
147 if( fgState.DisplayMode & GLUT_ACCUM )
149 ATTRIB( GLX_ACCUM_RED_SIZE ); ATTRIB( 1 );
150 ATTRIB( GLX_ACCUM_GREEN_SIZE ); ATTRIB( 1 );
151 ATTRIB( GLX_ACCUM_BLUE_SIZE ); ATTRIB( 1 );
154 * Check if the A component is required, too:
156 if( fgState.DisplayMode & GLUT_ALPHA )
158 ATTRIB( GLX_ACCUM_ALPHA_SIZE ); ATTRIB( 1 );
163 * Push a null at the end of the list
168 * OKi now, we've got two cases -- RGB(A) and index mode visuals
170 if( wantIndexedMode == FALSE )
173 * The easier one. And more common, too.
175 return( glXChooseVisual( fgDisplay.Display, fgDisplay.Screen, attributes ) );
179 XVisualInfo* visualInfo;
183 * In indexed mode, we need to check how many bits of depth can we achieve
189 * The GLX_BUFFER_SIZE value comes always first, so:
191 attributes[ 1 ] = bufferSize[ i ];
194 * Check if such visual is possible
196 visualInfo = glXChooseVisual( fgDisplay.Display, fgDisplay.Screen, attributes );
199 * The buffer size are sorted in descendant order, so choose the first:
201 if( visualInfo != NULL )
202 return( visualInfo );
206 * If we are still here, it means that the visual info was not found
214 * Setup the pixel format for a Win32 window
216 #if TARGET_HOST_WIN32
217 gboolean fgSetupPixelFormat( SFG_Window* window, gboolean checkOnly )
219 PIXELFORMATDESCRIPTOR* ppfd, pfd;
220 gint flags, pixelformat;
223 * Check if the window seems valid
225 freeglut_return_val_if_fail( window != NULL, 0 );
228 * The pixel format should allow us to draw to the window using OpenGL
230 flags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL;
233 * It might be the case for us to use double buffering
235 if( fgState.DisplayMode & GLUT_DOUBLE )
236 flags |= PFD_DOUBLEBUFFER;
239 * Specify which pixel format do we opt for...
241 # pragma message( "fgSetupPixelFormat(): there is still some work to do here!" )
243 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
246 pfd.iPixelType = PFD_TYPE_RGBA;
257 pfd.cAccumRedBits = 0;
258 pfd.cAccumGreenBits = 0;
259 pfd.cAccumBlueBits = 0;
260 pfd.cAccumAlphaBits = 0;
262 pfd.cStencilBits = 0;
264 pfd.iLayerType = PFD_MAIN_PLANE;
267 pfd.dwVisibleMask = 0;
268 pfd.dwDamageMask = 0;
271 * Fill in the color bits...
273 pfd.cColorBits = (BYTE) GetDeviceCaps( window->Window.Device, BITSPIXEL );
277 * Choose the pixel format that matches our demand
279 pixelformat = ChoosePixelFormat( window->Window.Device, ppfd );
280 if( pixelformat == 0 )
284 * We might have been called to check if the pixel format exists only
290 * Finally, set the window's pixel format
292 if( SetPixelFormat( window->Window.Device, pixelformat, ppfd ) == FALSE )
300 * Opens a window. Requires a SFG_Window object created and attached
301 * to the freeglut structure. OpenGL context is created here.
303 void fgOpenWindow( SFG_Window* window, const char* title, int x, int y, int w, int h, GLboolean gameMode )
305 #if TARGET_HOST_UNIX_X11
306 XSetWindowAttributes winAttr;
307 XTextProperty textProperty;
308 XSizeHints sizeHints;
312 freeglut_assert_ready;
315 * Here we are upon the stage. Have the visual selected.
317 window->Window.VisualInfo = fgChooseVisual();
318 assert( window->Window.VisualInfo != NULL );
321 * Have the windows attributes set
323 * HINT: the masks should be updated when adding/removing callbacks.
324 * This might speed up message processing. Is that true?
326 winAttr.event_mask = StructureNotifyMask | SubstructureNotifyMask | ExposureMask |
327 ButtonPressMask | ButtonReleaseMask | KeyPressMask |
328 VisibilityChangeMask | EnterWindowMask | LeaveWindowMask |
329 PointerMotionMask | ButtonMotionMask;
330 winAttr.background_pixmap = None;
331 winAttr.background_pixel = 0;
332 winAttr.border_pixel = 0;
335 * The color map is required, too
337 winAttr.colormap = XCreateColormap(
338 fgDisplay.Display, fgDisplay.RootWindow,
339 window->Window.VisualInfo->visual, AllocNone
343 * This tells the XCreateWindow() what attributes are we supplying it with
345 mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;
348 * Have the window created now
350 window->Window.Handle = XCreateWindow(
352 window->Parent == NULL ? fgDisplay.RootWindow : window->Parent->Window.Handle,
354 window->Window.VisualInfo->depth, InputOutput,
355 window->Window.VisualInfo->visual, mask,
360 * The GLX context creation, possibly trying the direct context rendering
362 window->Window.Context = glXCreateContext(
363 fgDisplay.Display, window->Window.VisualInfo,
364 NULL, fgState.ForceDirectContext | fgState.TryDirectContext
368 * Make sure the context is direct when the user wants it forced
370 if( fgState.ForceDirectContext && !glXIsDirect( fgDisplay.Display, window->Window.Context ) )
371 fgError( "unable to force direct context rendering for window '%s'", title );
374 * Set the new context as the current one. That's all about the window creation.
378 window->Window.Handle,
379 window->Window.Context
383 * Assume the new window is visible by default
385 window->State.Visible = TRUE;
388 * For the position and size hints -- make sure we are passing valid values
392 sizeHints.flags |= (fgState.Position.Use == TRUE) ? USPosition : PPosition;
393 sizeHints.flags |= (fgState.Size.Use == TRUE) ? USSize : PSize;
396 * Fill in the size hints values now (the x, y, width and height
397 * settings are obsolote, are there any more WMs that support them?)
399 sizeHints.x = x; sizeHints.y = y;
400 sizeHints.width = w; sizeHints.height = h;
403 * We can have forced all new windows start in iconified state:
405 wmHints.flags = StateHint;
406 wmHints.initial_state = (fgState.ForceIconic == FALSE) ? NormalState : IconicState;
409 * Prepare the window and iconified window names...
411 XStringListToTextProperty( (char **) &title, 1, &textProperty );
414 * Set the window's properties now
418 window->Window.Handle,
429 * Make sure we are informed about the window deletion commands
431 XSetWMProtocols( fgDisplay.Display, window->Window.Handle, &fgDisplay.DeleteWindow, 1 );
434 * Finally, have the window mapped to our display
436 XMapWindow( fgDisplay.Display, window->Window.Handle );
439 * This somehow fixes the glutGet() GLUT_WINDOW_X and GLUT_WINDOW_Y problem...
441 XMoveWindow( fgDisplay.Display, window->Window.Handle, x, y );
444 * In game mode, move the viewport a bit to hide the decorations.
445 * This code depends on the XFree86 video mode extensions.
447 if( gameMode == TRUE )
449 # ifdef X_XF86VidModeSetViewPort
452 * Set the newly created window as the current one...
454 glutSetWindow( window->ID );
457 * Move the viewport a bit down and right from top-left corner to hide the decorations
459 XF86VidModeSetViewPort(
462 glutGet( GLUT_WINDOW_X ),
463 glutGet( GLUT_WINDOW_Y )
469 #elif TARGET_HOST_WIN32
476 freeglut_assert_ready;
479 * Grab the window class we have registered on glutInit():
481 atom = GetClassInfo( fgDisplay.Instance, "FREEGLUT", &wc );
482 g_assert( atom != 0 );
484 if( gameMode == FALSE )
487 * Update the window position and dimensions, taking account of window decorations
489 x -= (GetSystemMetrics( SM_CXSIZEFRAME ) - 1);
490 y -= (GetSystemMetrics( SM_CYSIZEFRAME ) - 1);
491 w += (GetSystemMetrics( SM_CXSIZEFRAME ) - 1)*2;
492 h += (GetSystemMetrics( SM_CYSIZEFRAME ) - 1)*2 + GetSystemMetrics( SM_CYCAPTION );
495 * Check if the user wants us to use the default position/size
497 if( fgState.Position.Use == FALSE ) { x = CW_USEDEFAULT; y = CW_USEDEFAULT; }
498 if( fgState.Size .Use == FALSE ) { w = CW_USEDEFAULT; h = CW_USEDEFAULT; }
501 * There's a small difference between creating the top, child and game mode windows
503 flags = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
505 if( window->Parent == NULL )
506 flags |= WS_OVERLAPPEDWINDOW;
513 * In game mode, the story is a little bit different...
515 g_assert( window->Parent == NULL );
518 * Set the window creation flags appropriately to make the window entirely visible:
520 flags = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE;
524 * Create the window now, passing the freeglut window structure as the parameter
526 window->Window.Handle = CreateWindow(
531 (HWND) window->Parent == NULL ? NULL : window->Parent->Window.Handle,
538 * Make sure window was created
540 g_assert( window->Window.Handle != NULL );
543 * Show and update the main window. Hide the mouse cursor.
545 ShowWindow( window->Window.Handle, fgState.ForceIconic ? SW_SHOWMINIMIZED : SW_SHOW );
546 UpdateWindow( window->Window.Handle );
552 * Set the newly created window as the current one
554 glutSetWindow( window->ID );
558 * Closes a window, destroying the frame and OpenGL context
560 void fgCloseWindow( SFG_Window* window )
562 freeglut_assert_ready;
564 #if TARGET_HOST_UNIX_X11
566 * As easy as kill bunnies with axes. Destroy the context first:
568 glXDestroyContext( fgDisplay.Display, window->Window.Context );
571 * Then have the window killed:
573 XDestroyWindow( fgDisplay.Display, window->Window.Handle );
576 * Finally, flush the rests down the stream
578 XFlush( fgDisplay.Display );
580 #elif TARGET_HOST_WIN32
582 * Send the WM_CLOSE message to the window now
585 window->Window.Handle,
595 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
598 * Creates a new top-level freeglut window
600 int FGAPIENTRY glutCreateWindow( const char* title )
603 * Create a new window and return it's unique ID number
605 return( fgCreateWindow( NULL, title, fgState.Position.X, fgState.Position.Y,
606 fgState.Size.X, fgState.Size.Y, FALSE )->ID );
610 * This function creates a sub window.
612 int FGAPIENTRY glutCreateSubWindow( int parentID, int x, int y, int w, int h )
614 SFG_Window* window = NULL;
615 SFG_Window* parent = NULL;
617 freeglut_assert_ready;
620 * Find a parent to the newly created window...
622 parent = fgWindowByID( parentID );
625 * Fail if the parent has not been found
627 freeglut_return_val_if_fail( parent != NULL, 0 );
630 * Create the new window
632 window = fgCreateWindow( parent, "", x, y, w, h, FALSE );
635 * Return the new window's ID
637 return( window->ID );
641 * Destroys a window and all of it's subwindows
643 void FGAPIENTRY glutDestroyWindow( int windowID )
646 * Grab the freeglut window pointer from the structure
648 SFG_Window* window = fgWindowByID( windowID );
649 freeglut_return_if_fail( window != NULL );
652 * There is a function that performs all needed steps
653 * defined in freeglut_structure.c. Let's use it:
655 fgDestroyWindow( window, TRUE );
659 * This function selects the current window
661 void FGAPIENTRY glutSetWindow( int ID )
663 SFG_Window* window = NULL;
666 * Make sure we don't get called too early
668 freeglut_assert_ready;
671 * Be wise. Be wise. Be wise. Be quick.
673 if( fgStructure.Window != NULL )
674 if( fgStructure.Window->ID == ID )
678 * Now we are sure there is sense in looking for the window
680 window = fgWindowByID( ID );
683 * In the case of an utter failure...
688 * ...issue a warning message and keep rolling on
690 fgWarning( "glutSetWindow(): window ID %i not found!", ID );
694 #if TARGET_HOST_UNIX_X11
696 * Make the selected window's GLX context the current one
700 window->Window.Handle,
701 window->Window.Context
704 #elif TARGET_HOST_WIN32
706 * Release the previous' context's device context
708 if( fgStructure.Window != NULL )
709 ReleaseDC( fgStructure.Window->Window.Handle, fgStructure.Window->Window.Device );
712 * We will care about releasing the device context later
714 window->Window.Device = GetDC( window->Window.Handle );
717 * Set the new current context:
720 window->Window.Device,
721 window->Window.Context
727 * Remember that we have changed the current window state
729 fgStructure.Window = window;
733 * This function returns the ID number of the current window, 0 if none exists
735 int FGAPIENTRY glutGetWindow( void )
737 freeglut_assert_ready;
740 * Do we have a current window selected?
742 if( fgStructure.Window == NULL )
745 * Nope. Return zero to mark the state.
751 * Otherwise, return the ID of the current window
753 return( fgStructure.Window->ID );
757 * This function makes the current window visible
759 void FGAPIENTRY glutShowWindow( void )
761 freeglut_assert_ready; freeglut_assert_window;
763 #if TARGET_HOST_UNIX_X11
765 * Showing the window is done via mapping under X
767 XMapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
768 XFlush( fgDisplay.Display );
770 #elif TARGET_HOST_WIN32
772 * Restore the window's originial position and size
774 ShowWindow( fgStructure.Window->Window.Handle, SW_SHOW );
780 * This function hides the current window
782 void FGAPIENTRY glutHideWindow( void )
784 freeglut_assert_ready; freeglut_assert_window;
786 #if TARGET_HOST_UNIX_X11
788 * The way we hide a window depends on if we're dealing
789 * with a top-level or children one...
791 if( fgStructure.Window->Parent == NULL )
794 * This is a top-level window
796 XWithdrawWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, fgDisplay.Screen );
801 * Nope, it's a child window
803 XUnmapWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
807 * Flush the X state now
809 XFlush( fgDisplay.Display );
811 #elif TARGET_HOST_WIN32
815 ShowWindow( fgStructure.Window->Window.Handle, SW_HIDE );
821 * Iconify the current window (top-level windows only)
823 void FGAPIENTRY glutIconifyWindow( void )
825 freeglut_assert_ready; freeglut_assert_window;
827 #if TARGET_HOST_UNIX_X11
829 * Iconify the window and flush the X state
831 XIconifyWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, fgDisplay.Screen );
832 XFlush( fgDisplay.Display );
834 #elif TARGET_HOST_WIN32
836 * Minimize the current window (this should be the same as X window iconifying)
838 ShowWindow( fgStructure.Window->Window.Handle, SW_MINIMIZE );
844 * Set the current window's title
846 void FGAPIENTRY glutSetWindowTitle( char* title )
848 freeglut_assert_ready; freeglut_assert_window;
851 * Works only for top-level windows
853 if( fgStructure.Window->Parent != NULL )
856 #if TARGET_HOST_UNIX_X11
861 * Prepare the text properties
863 text.value = (unsigned char *) title;
864 text.encoding = XA_STRING;
866 text.nitems = strlen( title );
873 fgStructure.Window->Window.Handle,
878 * Have the X display state flushed
880 XFlush( fgDisplay.Display );
883 #elif TARGET_HOST_WIN32
885 * This seems to be a bit easier under Win32
887 SetWindowText( fgStructure.Window->Window.Handle, title );
893 * Set the current window's iconified title
895 void FGAPIENTRY glutSetIconTitle( char* title )
897 freeglut_assert_ready; freeglut_assert_window;
900 * Works only for top-level windows
902 if( fgStructure.Window->Parent != NULL )
905 #if TARGET_HOST_UNIX_X11
910 * Prepare the text properties
912 text.value = (unsigned char *) title;
913 text.encoding = XA_STRING;
915 text.nitems = strlen( title );
922 fgStructure.Window->Window.Handle,
927 * Have the X display state flushed
929 XFlush( fgDisplay.Display );
932 #elif TARGET_HOST_WIN32
934 * This seems to be a bit easier under Win32
936 SetWindowText( fgStructure.Window->Window.Handle, title );
942 * Change the current window's size
944 void FGAPIENTRY glutReshapeWindow( int width, int height )
946 freeglut_assert_ready; freeglut_assert_window;
948 #if TARGET_HOST_UNIX_X11
950 * Resize the window and flush the X state
952 XResizeWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, width, height );
953 XFlush( fgDisplay.Display );
955 #elif TARGET_HOST_WIN32
960 * First off, grab the current window's position
962 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
965 * Resize the window, forcing a redraw to happen
968 fgStructure.Window->Window.Handle,
980 * Change the current window's position
982 void FGAPIENTRY glutPositionWindow( int x, int y )
984 freeglut_assert_ready; freeglut_assert_window;
986 #if TARGET_HOST_UNIX_X11
988 * Reposition the window and flush the X state
990 XMoveWindow( fgDisplay.Display, fgStructure.Window->Window.Handle, x, y );
991 XFlush( fgDisplay.Display );
993 #elif TARGET_HOST_WIN32
998 * First off, grab the current window's position
1000 GetWindowRect( fgStructure.Window->Window.Handle, &winRect );
1003 * Reposition the window, forcing a redraw to happen
1006 fgStructure.Window->Window.Handle,
1009 winRect.right - winRect.left,
1010 winRect.bottom - winRect.top,
1019 * Lowers the current window (by Z order change)
1021 void FGAPIENTRY glutPushWindow( void )
1023 freeglut_assert_ready; freeglut_assert_window;
1025 #if TARGET_HOST_UNIX_X11
1027 * Lower the current window
1029 XLowerWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
1031 #elif TARGET_HOST_WIN32
1033 * Set the new window's Z position, not affecting the rest of the settings:
1036 fgStructure.Window->Window.Handle,
1039 SWP_NOSIZE | SWP_NOMOVE
1046 * Raises the current window (by Z order change)
1048 void FGAPIENTRY glutPopWindow( void )
1050 freeglut_assert_ready; freeglut_assert_window;
1052 #if TARGET_HOST_UNIX_X11
1054 * Raise the current window
1056 XRaiseWindow( fgDisplay.Display, fgStructure.Window->Window.Handle );
1058 #elif TARGET_HOST_WIN32
1060 * Set the new window's Z position, not affecting the rest of the settings:
1063 fgStructure.Window->Window.Handle,
1066 SWP_NOSIZE | SWP_NOMOVE
1073 * Resize the current window so that it fits the whole screen
1075 void FGAPIENTRY glutFullScreen( void )
1077 freeglut_assert_ready; freeglut_assert_window;
1080 * Just have the window repositioned and resized
1082 glutPositionWindow( 0, 0 );
1085 fgDisplay.ScreenWidth,
1086 fgDisplay.ScreenHeight
1090 /*** END OF FILE ***/