4 * The windows message processing 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-main"
34 #include "../include/GL/freeglut.h"
35 #include "../include/GL/freeglut_internal.h"
38 * TODO BEFORE THE STABLE RELEASE:
40 * There are some issues concerning window redrawing under X11, and maybe
41 * some events are not handled. The Win32 version lacks some more features,
42 * but seems acceptable for not demanding purposes.
44 * Need to investigate why the X11 version breaks out with an error when
45 * closing a window (using the window manager, not glutDestroyWindow)...
48 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */
51 * Calls a window's redraw method. This is used when
52 * a redraw is forced by the incoming window messages.
54 static void fghRedrawWindowByHandle
55 #if TARGET_HOST_UNIX_X11
57 #elif TARGET_HOST_WIN32
62 * Find the window we have to redraw...
64 SFG_Window* window = fgWindowByHandle( handle );
65 freeglut_return_if_fail( window != NULL );
68 * Check if there is a display callback hooked to it
70 freeglut_return_if_fail( window->Callbacks.Display != NULL );
73 * Return if the window is not visible
75 freeglut_return_if_fail( window->State.Visible == TRUE );
78 * Set the window as the current one. Calling glutSetWindow()
79 * might seem slow and generally redundant, but it is portable.
81 glutSetWindow( window->ID );
84 * Do not exagerate with the redisplaying
86 window->State.Redisplay = FALSE;
89 * Have the callback executed now. The buffers should
90 * be swapped by the glutSwapBuffers() execution inside
91 * the callback itself.
94 window->Callbacks.Display();
98 * Handle a window configuration change. When no reshape
99 * callback is hooked, the viewport size is updated to
100 * match the new window size.
102 static void fghReshapeWindowByHandle
103 #if TARGET_HOST_UNIX_X11
104 ( Window handle, int width, int height )
105 #elif TARGET_HOST_WIN32
106 ( HWND handle, int width, int height )
110 * Find the window that received the reshape event
112 SFG_Window* window = fgWindowByHandle( handle );
113 freeglut_return_if_fail( window != NULL );
116 * Remember about setting the current window...
118 glutSetWindow( window->ID );
121 * Check if there is a reshape callback hooked
123 if( window->Callbacks.Reshape != NULL )
126 * OKi, have it called immediately
128 window->Callbacks.Reshape( width, height );
133 * Otherwise just resize the viewport
135 glViewport( 0, 0, width, height );
139 * Force a window redraw. In Windows at least this is only a partial solution: if the
140 * window is increasing in size in either dimension, the already-drawn part does not get
141 * drawn again and things look funny. But without this we get this bad behaviour whenever
142 * we resize the window.
144 window->State.Redisplay = TRUE ;
148 * A static helper function to execute display callback for a window
150 static void fghcbDisplayWindow( SFG_Window *window, SFG_Enumerator *enumerator )
152 #if TARGET_HOST_UNIX_X11
154 * Check if there is an idle callback hooked
156 if( (window->Callbacks.Display != NULL) &&
157 (window->State.Redisplay == TRUE) &&
158 (window->State.Visible == TRUE) )
161 * OKi, this is the case: have the window set as the current one
163 glutSetWindow( window->ID );
166 * Do not exagerate with the redisplaying
168 window->State.Redisplay = FALSE;
171 * And execute the display callback immediately after
173 window->Callbacks.Display();
176 #elif TARGET_HOST_WIN32
179 * Do we need to explicitly resize the window?
181 if( window->State.NeedToResize )
183 glutSetWindow( window->ID );
185 fghReshapeWindowByHandle(
186 window->Window.Handle,
187 glutGet( GLUT_WINDOW_WIDTH ),
188 glutGet( GLUT_WINDOW_HEIGHT )
192 * Never ever do that again:
194 window->State.NeedToResize = FALSE;
198 * This is done in a bit different way under Windows
200 if( (window->Callbacks.Display != NULL) &&
201 (window->State.Redisplay == TRUE) &&
202 (window->State.Visible == TRUE) )
205 * Do not exagerate with the redisplaying
207 window->State.Redisplay = FALSE;
210 window->Window.Handle, NULL, NULL,
211 RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE
218 * Process this window's children (if any)
220 fgEnumSubWindows( window, fghcbDisplayWindow, enumerator );
224 * Make all windows perform a display call
226 static void fghDisplayAll( void )
228 SFG_Enumerator enumerator;
231 * Uses a method very similiar for fgWindowByHandle...
233 enumerator.found = FALSE;
234 enumerator.data = NULL;
237 * Start the enumeration now:
239 fgEnumWindows( fghcbDisplayWindow, &enumerator );
243 * Window enumerator callback to check for the joystick polling code
245 static void fghcbCheckJoystickPolls( SFG_Window *window, SFG_Enumerator *enumerator )
247 long int checkTime = fgElapsedTime();
250 * Check if actually need to do the poll for the currently enumerated window:
252 if( window->State.JoystickLastPoll + window->State.JoystickPollRate >= checkTime )
255 * Yeah, that's it. Poll the joystick...
257 fgJoystickPollWindow( window );
260 * ...and reset the polling counters:
262 window->State.JoystickLastPoll = checkTime;
266 * Process this window's children (if any)
268 fgEnumSubWindows( window, fghcbCheckJoystickPolls, enumerator );
272 * Check all windows for joystick polling
274 static void fghCheckJoystickPolls( void )
276 SFG_Enumerator enumerator;
279 * Uses a method very similiar for fgWindowByHandle...
281 enumerator.found = FALSE;
282 enumerator.data = NULL;
285 * Start the enumeration now:
287 fgEnumWindows( fghcbCheckJoystickPolls, &enumerator );
291 * Check the global timers
293 static void fghCheckTimers( void )
295 long checkTime = fgElapsedTime();
296 SFG_Timer *timer, *next;
299 fgListInit(&timedOut);
302 * For every timer that is waiting for triggering
304 for( timer = fgState.Timers.First; timer; timer = next )
306 next = timer->Node.Next;
309 * Check for the timeout:
311 if( timer->TriggerTime <= checkTime )
314 * Add the timer to the timed out timers list
316 fgListRemove( &fgState.Timers, &timer->Node );
317 fgListAppend( &timedOut, &timer->Node );
322 * Now feel free to execute all the hooked and timed out timer callbacks
323 * And delete the timed out timers...
325 while ( (timer = timedOut.First) )
327 if( timer->Callback != NULL )
328 timer->Callback( timer->ID );
329 fgListRemove( &timedOut, &timer->Node );
338 long fgElapsedTime( void )
340 #if TARGET_HOST_UNIX_X11
344 gettimeofday( &now, NULL );
346 elapsed = (now.tv_usec - fgState.Time.Value.tv_usec) / 1000;
347 elapsed += (now.tv_sec - fgState.Time.Value.tv_sec) * 1000;
350 #elif TARGET_HOST_WIN32
351 return (timeGetTime() - fgState.Time.Value);
358 void fgError( const char *fmt, ... )
364 fprintf( stderr, "freeglut: ");
365 vfprintf( stderr, fmt, ap );
366 fprintf( stderr, "\n" );
373 void fgWarning( const char *fmt, ... )
379 fprintf( stderr, "freeglut: ");
380 vfprintf( stderr, fmt, ap );
381 fprintf( stderr, "\n" );
389 static void fgCleanUpGlutsMess( void )
395 if ( fgStructure.Windows.First != NULL )
397 SFG_Window *win = fgStructure.Windows.First ;
401 while ( win != NULL )
403 SFG_Window *temp_win = win->Node.Next ;
404 fgDestroyWindow ( win, FALSE ) ;
410 /* these are pointers to external handles */
412 __glutWindowListSize = 0;
413 __glutStaleWindowList = NULL;
414 __glutWindowList = NULL;
415 __glutCurrentWindow = NULL;
417 /* make sure we no longer have a GL context */
419 if ( wglGetCurrentContext() != NULL )
421 wglDeleteContext( wglGetCurrentContext() );
424 hInstance = GetModuleHandle(NULL);
425 UnregisterClass( classname, hInstance );
427 /* clean up allocated timer memory */
429 tList = __glutTimerList;
432 while ( __glutTimerList )
435 tList = __glutTimerList;
437 if ( __glutTimerList )
438 __glutTimerList = __glutTimerList->next;
447 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
450 * Executes a single iteration in the FreeGLUT processing loop.
452 void FGAPIENTRY glutMainLoopEvent( void )
454 #if TARGET_HOST_UNIX_X11
460 * This code was repeated constantly, so here it goes into a definition:
462 # define GETWINDOW(a) window = fgWindowByHandle( event.a.window );if( window == NULL ) break;
463 # define GETMOUSE(a) window->State.MouseX = event.a.x; window->State.MouseY = event.a.y;
466 * Make sure the display has been created etc.
468 freeglut_assert_ready;
471 * Do we have any event messages pending?
473 if( XPending( fgDisplay.Display ) )
476 * Grab the next event to be processed...
478 XNextEvent( fgDisplay.Display, &event );
481 * Check the event's type
487 * The window creation confirmation
493 * This is sent to confirm the XDestroyWindow call.
496 * Call the window closure callback, remove from the structure, etc.
498 fgAddToWindowDestroyList ( window, FALSE );
504 * Destroy the window when the WM_DELETE_WINDOW message arrives
506 if( (Atom) event.xclient.data.l[ 0 ] == fgDisplay.DeleteWindow )
513 * A window of ours has been unmapped...
519 * We are too dumb to process partial exposes...
521 if( event.xexpose.count == 0 )
522 fghRedrawWindowByHandle( event.xexpose.window );
525 case ConfigureNotify:
527 * The window gets resized
529 fghReshapeWindowByHandle(
530 event.xconfigure.window,
531 event.xconfigure.width,
532 event.xconfigure.height
538 * Have the client's keyboard knowledge updated (xlib.ps,
539 * page 206, says that's a good thing to do)
541 XRefreshKeyboardMapping( (XMappingEvent *) &event );
544 case VisibilityNotify:
547 * The window's visiblity might have changed
549 GETWINDOW( xvisibility );
552 * Break now if no window status callback has been hooked to that window
554 if( window->Callbacks.WindowStatus == NULL )
558 * We're going to send a callback to a window. Make it current.
560 glutSetWindow( window->ID );
563 * Sending this event, the X server can notify us that the window has just
564 * acquired one of the three possible visibility states: VisibilityUnobscured,
565 * VisibilityPartiallyObscured or VisibilityFullyObscured
567 switch( event.xvisibility.state )
569 case VisibilityUnobscured:
571 * We are fully visible...
573 window->Callbacks.WindowStatus( GLUT_FULLY_RETAINED );
574 window->State.Visible = TRUE;
577 case VisibilityPartiallyObscured:
579 * The window is partially visible
581 window->Callbacks.WindowStatus( GLUT_PARTIALLY_RETAINED );
582 window->State.Visible = TRUE;
585 case VisibilityFullyObscured:
587 * The window is totally obscured
589 window->Callbacks.WindowStatus( GLUT_FULLY_COVERED );
590 window->State.Visible = FALSE;
599 * Mouse is over one of our windows
601 GETWINDOW( xcrossing ); GETMOUSE( xcrossing );
604 * Is there an entry callback hooked to the window?
606 if( window->Callbacks.Entry != NULL )
609 * Yeah. Notify the window about having the mouse cursor over
611 window->Callbacks.Entry( GLUT_ENTERED );
619 * Mouse is no longer over one of our windows
621 GETWINDOW( xcrossing ); GETMOUSE( xcrossing );
624 * Is there an entry callback hooked to the window?
626 if( window->Callbacks.Entry != NULL )
629 * Yeah. Notify the window about having the mouse cursor over
631 window->Callbacks.Entry( GLUT_LEFT );
639 * The mouse cursor was moved...
641 GETWINDOW( xmotion ); GETMOUSE( xmotion );
644 * What kind of a movement was it?
646 if( (event.xmotion.state & Button1Mask) || (event.xmotion.state & Button2Mask) ||
647 (event.xmotion.state & Button3Mask) || (event.xmotion.state & Button4Mask) ||
648 (event.xmotion.state & Button5Mask) )
651 * A mouse button was pressed during the movement...
652 * Is there a motion callback hooked to the window?
654 if( window->Callbacks.Motion != NULL )
657 * Yup. Have it executed immediately
659 window->Callbacks.Motion( event.xmotion.x, event.xmotion.y );
665 * Otherwise it was a passive movement...
667 if( window->Callbacks.Passive != NULL )
670 * That's right, and there is a passive callback, too.
672 window->Callbacks.Passive( event.xmotion.x, event.xmotion.y );
681 GLboolean pressed = TRUE ;
684 if ( event.type == ButtonRelease ) pressed = FALSE ;
687 * A mouse button has been pressed or released. Traditionally,
688 * break if the window was found within the freeglut structures.
690 GETWINDOW( xbutton ); GETMOUSE( xbutton );
693 * GLUT API assumes that you can't have more than three mouse buttons, so:
695 switch( event.xbutton.button )
698 * WARNING: this might be wrong, if we only have two mouse buttons,
699 * Button2 might mean the right button, isn't that right?
701 case Button1: button = GLUT_LEFT_BUTTON; break;
702 case Button2: button = GLUT_MIDDLE_BUTTON; break;
703 case Button3: button = GLUT_RIGHT_BUTTON; break;
704 default: button = -1; break;
708 * Skip the unwanted mouse buttons...
714 * Do not execute the application's mouse callback if a menu is hooked to this button.
715 * In that case an appropriate private call should be generated.
716 * Near as I can tell, this is the menu behaviour:
717 * - Down-click the menu button, menu not active: activate the menu with its upper left-hand corner at the mouse location.
718 * - Down-click any button outside the menu, menu active: deactivate the menu
719 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
720 * - Up-click the menu button, menu not active: nothing happens
721 * - Up-click the menu button outside the menu, menu active: nothing happens
722 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
724 if ( window->ActiveMenu != NULL ) /* Window has an active menu, it absorbs any mouse click */
726 if ( fgCheckActiveMenu ( window, window->ActiveMenu ) == TRUE ) /* Inside the menu, invoke the callback and deactivate the menu*/
728 fgExecuteMenuCallback ( window->ActiveMenu ) ;
729 fgDeactivateMenu ( window ) ;
731 else /* Outside the menu, deactivate the menu if it's a downclick */
733 if ( pressed == TRUE ) fgDeactivateMenu ( window ) ;
737 * Let's make the window redraw as a result of the mouse click and menu activity.
739 window->State.Redisplay = TRUE ;
745 * No active menu, let's check whether we need to activate one.
747 if ( ( window->Menu[ button ] != NULL ) && ( pressed == TRUE ) )
750 * Let's make the window redraw as a result of the mouse click.
752 window->State.Redisplay = TRUE ;
755 * Activate the appropriate menu structure...
757 fgActivateMenu( window, button );
763 * Check if there is a mouse callback hooked to the window
765 if( window->Callbacks.Mouse == NULL )
769 * Set the current window
771 glutSetWindow( window->ID );
774 * Remember the current modifiers state
777 if (event.xbutton.state & (ShiftMask|LockMask))
778 modifiers |= GLUT_ACTIVE_SHIFT;
779 if (event.xbutton.state & ControlMask)
780 modifiers |= GLUT_ACTIVE_CTRL;
781 if (event.xbutton.state & Mod1Mask)
782 modifiers |= GLUT_ACTIVE_ALT;
783 window->State.Modifiers = modifiers;
786 * Finally execute the mouse callback
788 window->Callbacks.Mouse(
790 event.type == ButtonPress ? GLUT_DOWN : GLUT_UP,
796 * Trash the modifiers state
798 window->State.Modifiers = 0xffffffff;
805 FGCBkeyboard keyboard_cb;
806 FGCBspecial special_cb;
809 * A key has been pressed, find the window that had the focus:
811 GETWINDOW( xkey ); GETMOUSE( xkey );
813 if( event.type == KeyPress )
815 keyboard_cb = window->Callbacks.Keyboard;
816 special_cb = window->Callbacks.Special;
820 keyboard_cb = window->Callbacks.KeyboardUp;
821 special_cb = window->Callbacks.SpecialUp;
825 * Is there a keyboard/special callback hooked for this window?
827 if( (keyboard_cb != NULL) || (special_cb != NULL) )
829 XComposeStatus composeStatus;
830 char asciiCode[ 32 ];
835 * Check for the ASCII/KeySym codes associated with the event:
837 len = XLookupString( &event.xkey, asciiCode, sizeof(asciiCode), &keySym, &composeStatus );
840 * Get ready to calling the keyboard/special callbacks
842 glutSetWindow( window->ID );
845 * GLUT API tells us to have two separate callbacks...
850 * ...one for the ASCII translateable keypresses...
852 if( keyboard_cb != NULL )
855 * Remember the current modifiers state
858 if (event.xkey.state & (ShiftMask|LockMask))
859 modifiers |= GLUT_ACTIVE_SHIFT;
860 if (event.xkey.state & ControlMask)
861 modifiers |= GLUT_ACTIVE_CTRL;
862 if (event.xkey.state & Mod1Mask)
863 modifiers |= GLUT_ACTIVE_ALT;
864 window->State.Modifiers = modifiers;
867 * Execute the callback
869 keyboard_cb( asciiCode[ 0 ], event.xkey.x, event.xkey.y );
872 * Trash the modifiers state
874 window->State.Modifiers = 0xffffffff;
882 * ...and one for all the others, which need to be translated to GLUT_KEY_Xs...
887 * First the function keys come:
889 case XK_F1: special = GLUT_KEY_F1; break;
890 case XK_F2: special = GLUT_KEY_F2; break;
891 case XK_F3: special = GLUT_KEY_F3; break;
892 case XK_F4: special = GLUT_KEY_F4; break;
893 case XK_F5: special = GLUT_KEY_F5; break;
894 case XK_F6: special = GLUT_KEY_F6; break;
895 case XK_F7: special = GLUT_KEY_F7; break;
896 case XK_F8: special = GLUT_KEY_F8; break;
897 case XK_F9: special = GLUT_KEY_F9; break;
898 case XK_F10: special = GLUT_KEY_F10; break;
899 case XK_F11: special = GLUT_KEY_F11; break;
900 case XK_F12: special = GLUT_KEY_F12; break;
903 * Then the arrows and stuff:
905 case XK_Left: special = GLUT_KEY_LEFT; break;
906 case XK_Right: special = GLUT_KEY_RIGHT; break;
907 case XK_Up: special = GLUT_KEY_UP; break;
908 case XK_Down: special = GLUT_KEY_DOWN; break;
911 case XK_Prior: special = GLUT_KEY_PAGE_UP; break;
913 case XK_Next: special = GLUT_KEY_PAGE_DOWN; break;
915 case XK_Home: special = GLUT_KEY_HOME; break;
917 case XK_End: special = GLUT_KEY_END; break;
919 case XK_Insert: special = GLUT_KEY_INSERT; break;
923 * Execute the callback (if one has been specified),
924 * given that the special code seems to be valid...
926 if( (special_cb != NULL) && (special != -1) )
929 * Remember the current modifiers state
932 if (event.xkey.state & (ShiftMask|LockMask))
933 modifiers |= GLUT_ACTIVE_SHIFT;
934 if (event.xkey.state & ControlMask)
935 modifiers |= GLUT_ACTIVE_CTRL;
936 if (event.xkey.state & Mod1Mask)
937 modifiers |= GLUT_ACTIVE_ALT;
938 window->State.Modifiers = modifiers;
940 special_cb( special, event.xkey.x, event.xkey.y );
943 * Trash the modifiers state
945 window->State.Modifiers = 0xffffffff;
956 * Have all the timers checked.
961 * Poll the joystick and notify all windows that want to be notified...
963 fghCheckJoystickPolls();
966 * No messages in the queue, which means we are idling...
968 if( fgState.IdleCallback != NULL )
969 fgState.IdleCallback();
972 * Remember about displaying all the windows that have
973 * been marked for a redisplay (possibly in the idle call):
978 #elif TARGET_HOST_WIN32
983 * The windows processing is considerably smaller
985 if( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
988 * Grab the message now, checking for WM_QUIT
990 if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
991 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
994 * Translate virtual-key messages and send them to the window...
996 TranslateMessage( &stMsg );
997 DispatchMessage( &stMsg );
1002 * Have all the timers checked.
1007 * Poll the joystick and notify all windows that want to be notified...
1009 fghCheckJoystickPolls();
1012 * No messages in the queue, which means we are idling...
1014 if( fgState.IdleCallback != NULL )
1015 fgState.IdleCallback();
1018 * Remember about displaying all the windows that have
1019 * been marked for a redisplay (possibly in the idle call):
1027 * Enters the FreeGLUT processing loop. Stays until the "ExecState" changes to "GLUT_EXEC_STATE_STOP".
1029 void FGAPIENTRY glutMainLoop( void )
1031 #if TARGET_HOST_WIN32
1032 SFG_Window *window = fgStructure.Windows.First ;
1036 * Make sure the display has been created etc.
1038 freeglut_assert_ready;
1040 #if TARGET_HOST_WIN32
1042 * Processing before the main loop: If there is a window which is open and which
1043 * has a visibility callback, call it. I know this is an ugly hack, but I'm not sure
1044 * what else to do about it. Ideally we should leave something uninitialized in the
1045 * create window code and initialize it in the main loop, and have that initialization
1046 * create a "WM_ACTIVATE" message. Then we would put the visibility callback code in
1047 * the "case WM_ACTIVATE" block below. - John Fay -- 10/24/02
1049 while ( window != NULL )
1051 if ( window->Callbacks.Visibility != NULL )
1052 window->Callbacks.Visibility ( window->State.Visible ) ;
1054 window = window->Node.Next ;
1059 * Set freeglut to be running
1061 fgState.ExecState = GLUT_EXEC_STATE_RUNNING ;
1064 * Enter the main loop itself. Inside the loop, process events and check for loop exit.
1066 while ( fgState.ExecState == GLUT_EXEC_STATE_RUNNING )
1068 glutMainLoopEvent () ;
1071 * If an event caused a window to be closed, do the actual closing here
1076 * If there are no more windows open, stop execution
1078 if ( fgStructure.Windows.First == NULL )
1079 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1084 * If we got here by the user closing a window or by the application closing down, there may still be windows open.
1086 fgCleanUpGlutsMess () ;
1089 * Check whether we return to the calling program or simply exit
1091 if ( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
1095 * When this loop terminates, destroy the display, state and structure
1096 * of a freeglut session, so that another glutInit() call can happen
1102 * Leaves the FreeGLUT processing loop.
1104 void FGAPIENTRY glutLeaveMainLoop( void )
1106 fgState.ExecState = GLUT_EXEC_STATE_STOP ;
1110 * The window procedure for handling Win32 events
1112 #if TARGET_HOST_WIN32
1113 LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
1115 SFG_Window* window = fgWindowByHandle( hWnd );
1119 if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
1120 return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
1122 /* if ( uMsg != 0x000f ) printf ( "message <%x>\n", uMsg ) ; */
1124 * Check what type of message are we receiving
1130 * The window structure is passed as the creation structure paramter...
1132 window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
1133 assert( window != NULL );
1136 * We can safely store the window's handle now:
1138 window->Window.Handle = hWnd;
1141 * Get the window's device context
1143 window->Window.Device = GetDC( hWnd );
1146 * Setup the pixel format of our window
1148 fgSetupPixelFormat( window, FALSE );
1151 * Create the OpenGL rendering context now
1153 window->Window.Context = wglCreateContext( window->Window.Device );
1156 * Still, we'll be needing to explicitly resize the window
1158 window->State.NeedToResize = TRUE;
1161 * Finally, have the window's device context released
1163 ReleaseDC( window->Window.Handle, window->Window.Device );
1168 * We got resized... But check if the window has been already added...
1170 fghReshapeWindowByHandle( hWnd, LOWORD(lParam), HIWORD(lParam) );
1174 printf("WM_SETFOCUS: %p\n", window );
1175 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1179 if (LOWORD(wParam) != WA_INACTIVE)
1181 //glutSetCursor( fgStructure.Window->State.Cursor );
1182 printf("WM_ACTIVATE: glutSetCursor( %p, %d)\n", window, window->State.Cursor );
1184 glutSetCursor( window->State.Cursor );
1187 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1193 * Windows seems to need reminding to erase the cursor for NONE.
1196 if ((LOWORD(lParam) == HTCLIENT) &&
1197 (fgStructure.Window->State.Cursor == GLUT_CURSOR_NONE))
1200 // Set the cursor AND change it for this window class.
1201 # define MAP_CURSOR(a,b) case a: SetCursor( LoadCursor( NULL, b ) ); \
1203 // Nuke the cursor AND change it for this window class.
1204 # define ZAP_CURSOR(a,b) case a: SetCursor( NULL ); \
1207 if (LOWORD(lParam) == HTCLIENT)
1208 switch( window->State.Cursor )
1210 MAP_CURSOR( GLUT_CURSOR_RIGHT_ARROW, IDC_ARROW );
1211 MAP_CURSOR( GLUT_CURSOR_LEFT_ARROW, IDC_ARROW );
1212 MAP_CURSOR( GLUT_CURSOR_INFO, IDC_HELP );
1213 MAP_CURSOR( GLUT_CURSOR_DESTROY, IDC_CROSS );
1214 MAP_CURSOR( GLUT_CURSOR_HELP, IDC_HELP );
1215 MAP_CURSOR( GLUT_CURSOR_CYCLE, IDC_SIZEALL );
1216 MAP_CURSOR( GLUT_CURSOR_SPRAY, IDC_CROSS );
1217 MAP_CURSOR( GLUT_CURSOR_WAIT, IDC_WAIT );
1218 MAP_CURSOR( GLUT_CURSOR_TEXT, IDC_UPARROW );
1219 MAP_CURSOR( GLUT_CURSOR_CROSSHAIR, IDC_CROSS );
1220 //MAP_CURSOR( GLUT_CURSOR_NONE, IDC_NO );
1221 ZAP_CURSOR( GLUT_CURSOR_NONE, NULL );
1224 MAP_CURSOR( GLUT_CURSOR_UP_DOWN, IDC_ARROW );
1228 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1233 * We are now Visible!
1235 window->State.Visible = TRUE;
1236 window->State.Redisplay = TRUE;
1241 * Start the painting job
1244 BeginPaint( hWnd, &ps );
1247 * Call the engine's main frame drawing method
1249 fghRedrawWindowByHandle( hWnd );
1252 * End the painting job, release the device context
1254 EndPaint( hWnd, &ps );
1259 * Make sure we don't close a window with current context active
1261 if( fgStructure.Window == window )
1263 wglMakeCurrent( NULL, NULL );
1264 wglDeleteContext( window->Window.Context );
1268 * Put on a linked list of windows to be removed after all the callbacks have returned
1270 fgAddToWindowDestroyList ( window, FALSE ) ;
1273 * Proceed with the window destruction
1275 DestroyWindow( hWnd );
1280 * The window already got destroyed, so don't bother with it.
1287 * The mouse cursor has moved. Remember the new mouse cursor's position
1289 window->State.MouseX = LOWORD( lParam );
1290 window->State.MouseY = HIWORD( lParam );
1293 * Fallback if there's an active menu hooked to this window
1295 if( window->ActiveMenu != NULL )
1298 * Let's make the window redraw as a result of the mouse motion.
1300 window->State.Redisplay = TRUE ;
1306 * Remember the current modifiers state.
1308 window->State.Modifiers =
1309 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1310 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1311 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1314 * Check if any of the mouse buttons is pressed...
1316 if( (wParam & MK_LBUTTON) || (wParam & MK_MBUTTON) || (wParam & MK_RBUTTON) )
1319 * Yeah, indeed. We need to use the motion callback then:
1321 if( window->Callbacks.Motion != NULL )
1324 * Make sure the current window is set...
1326 glutSetWindow( window->ID );
1329 * Execute the active mouse motion callback now
1331 window->Callbacks.Motion( window->State.MouseX, window->State.MouseY );
1337 * All mouse buttons are up, execute the passive mouse motion callback
1339 if( window->Callbacks.Passive != NULL )
1342 * Make sure the current window is set
1344 glutSetWindow( window->ID );
1347 * Execute the passive mouse motion callback
1349 window->Callbacks.Passive( window->State.MouseX, window->State.MouseY );
1354 * Thrash the current modifiers state now
1356 window->State.Modifiers = 0xffffffff;
1360 case WM_LBUTTONDOWN:
1361 case WM_MBUTTONDOWN:
1362 case WM_RBUTTONDOWN:
1367 GLboolean pressed = TRUE;
1371 * The mouse cursor has moved. Remember the new mouse cursor's position
1373 window->State.MouseX = LOWORD( lParam );
1374 window->State.MouseY = HIWORD( lParam );
1377 * We're curious about the GLUT API button name...
1381 case WM_LBUTTONDOWN: pressed = TRUE; button = GLUT_LEFT_BUTTON; break;
1382 case WM_MBUTTONDOWN: pressed = TRUE; button = GLUT_MIDDLE_BUTTON; break;
1383 case WM_RBUTTONDOWN: pressed = TRUE; button = GLUT_RIGHT_BUTTON; break;
1384 case WM_LBUTTONUP: pressed = FALSE; button = GLUT_LEFT_BUTTON; break;
1385 case WM_MBUTTONUP: pressed = FALSE; button = GLUT_MIDDLE_BUTTON; break;
1386 case WM_RBUTTONUP: pressed = FALSE; button = GLUT_RIGHT_BUTTON; break;
1387 default: pressed = FALSE; button = -1; break;
1391 * The left and right mouse buttons might have been swapped...
1393 if( GetSystemMetrics( SM_SWAPBUTTON ) )
1394 if( button == GLUT_LEFT_BUTTON ) button = GLUT_RIGHT_BUTTON;
1395 else if( button == GLUT_RIGHT_BUTTON ) button = GLUT_LEFT_BUTTON;
1398 * Hey, what's up with you?
1401 return( DefWindowProc( hWnd, uMsg, lParam, wParam ) );
1404 * Do not execute the application's mouse callback if a menu is hooked to this button.
1405 * In that case an appropriate private call should be generated.
1406 * Near as I can tell, this is the menu behaviour:
1407 * - Down-click the menu button, menu not active: activate the menu with its upper left-hand corner at the mouse location.
1408 * - Down-click any button outside the menu, menu active: deactivate the menu
1409 * - Down-click any button inside the menu, menu active: select the menu entry and deactivate the menu
1410 * - Up-click the menu button, menu not active: nothing happens
1411 * - Up-click the menu button outside the menu, menu active: nothing happens
1412 * - Up-click the menu button inside the menu, menu active: select the menu entry and deactivate the menu
1414 if ( window->ActiveMenu != NULL ) /* Window has an active menu, it absorbs any mouse click */
1416 if ( fgCheckActiveMenu ( window, window->ActiveMenu ) == TRUE ) /* Inside the menu, invoke the callback and deactivate the menu*/
1418 fgExecuteMenuCallback ( window->ActiveMenu ) ;
1419 fgDeactivateMenu ( window ) ;
1421 else /* Outside the menu, deactivate the menu if it's a downclick */
1423 if ( pressed == TRUE ) fgDeactivateMenu ( window ) ;
1427 * Let's make the window redraw as a result of the mouse click and menu activity.
1429 window->State.Redisplay = TRUE ;
1435 * No active menu, let's check whether we need to activate one.
1437 if ( ( window->Menu[ button ] != NULL ) && ( pressed == TRUE ) )
1440 * Let's make the window redraw as a result of the mouse click.
1442 window->State.Redisplay = TRUE ;
1445 * Activate the appropriate menu structure...
1447 fgActivateMenu( window, button );
1453 * Check if there is a mouse callback hooked to the window
1455 if( window->Callbacks.Mouse == NULL )
1459 * Set the current window
1461 glutSetWindow( window->ID );
1464 * Remember the current modifiers state.
1466 window->State.Modifiers =
1467 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1468 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1469 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1472 * Finally execute the mouse callback
1474 window->Callbacks.Mouse(
1476 pressed == TRUE ? GLUT_DOWN : GLUT_UP,
1477 window->State.MouseX,
1478 window->State.MouseY
1482 * Trash the modifiers state
1484 window->State.Modifiers = 0xffffffff;
1494 * Ignore the automatic key repetition if needed:
1496 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1500 * Set the current window
1502 glutSetWindow( window->ID );
1505 * Remember the current modifiers state. This is done here in order
1506 * to make sure the VK_DELETE keyboard callback is executed properly.
1508 window->State.Modifiers =
1509 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1510 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1511 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1514 * Convert the Win32 keystroke codes to GLUTtish way
1516 # define KEY(a,b) case a: keypress = b; break;
1521 * Most of the special characters can be handled automagically...
1523 KEY( VK_F1, GLUT_KEY_F1 ); KEY( VK_F2, GLUT_KEY_F2 );
1524 KEY( VK_F3, GLUT_KEY_F3 ); KEY( VK_F4, GLUT_KEY_F4 );
1525 KEY( VK_F5, GLUT_KEY_F5 ); KEY( VK_F6, GLUT_KEY_F6 );
1526 KEY( VK_F7, GLUT_KEY_F7 ); KEY( VK_F8, GLUT_KEY_F8 );
1527 KEY( VK_F9, GLUT_KEY_F9 ); KEY( VK_F10, GLUT_KEY_F10 );
1528 KEY( VK_F11, GLUT_KEY_F11 ); KEY( VK_F12, GLUT_KEY_F12 );
1529 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP ); KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1530 KEY( VK_HOME, GLUT_KEY_HOME ); KEY( VK_END, GLUT_KEY_END );
1531 KEY( VK_LEFT, GLUT_KEY_LEFT ); KEY( VK_UP, GLUT_KEY_UP );
1532 KEY( VK_RIGHT, GLUT_KEY_RIGHT ); KEY( VK_DOWN, GLUT_KEY_DOWN );
1533 KEY( VK_INSERT, GLUT_KEY_INSERT );
1536 * ...yet there is a small exception we need to have handled...
1540 * The delete key should be treated as an ASCII keypress:
1542 if( window->Callbacks.Keyboard != NULL )
1543 window->Callbacks.Keyboard( 127, window->State.MouseX, window->State.MouseY );
1547 * Execute the special callback, if present, given the conversion was a success:
1549 if( (keypress != -1) && (window->Callbacks.Special != NULL) )
1552 * Have the special callback executed:
1554 window->Callbacks.Special( keypress, window->State.MouseX, window->State.MouseY );
1558 * Thrash the modifiers register now
1560 window->State.Modifiers = 0xffffffff;
1570 * Set the current window
1572 glutSetWindow( window->ID );
1575 * Remember the current modifiers state. This is done here in order
1576 * to make sure the VK_DELETE keyboard callback is executed properly.
1578 window->State.Modifiers =
1579 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1580 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1581 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1584 * Convert the Win32 keystroke codes to GLUTtish way. "KEY(a,b)" was defined under "WM_KEYDOWN"
1590 * Most of the special characters can be handled automagically...
1592 KEY( VK_F1, GLUT_KEY_F1 ); KEY( VK_F2, GLUT_KEY_F2 );
1593 KEY( VK_F3, GLUT_KEY_F3 ); KEY( VK_F4, GLUT_KEY_F4 );
1594 KEY( VK_F5, GLUT_KEY_F5 ); KEY( VK_F6, GLUT_KEY_F6 );
1595 KEY( VK_F7, GLUT_KEY_F7 ); KEY( VK_F8, GLUT_KEY_F8 );
1596 KEY( VK_F9, GLUT_KEY_F9 ); KEY( VK_F10, GLUT_KEY_F10 );
1597 KEY( VK_F11, GLUT_KEY_F11 ); KEY( VK_F12, GLUT_KEY_F12 );
1598 KEY( VK_PRIOR, GLUT_KEY_PAGE_UP ); KEY( VK_NEXT, GLUT_KEY_PAGE_DOWN );
1599 KEY( VK_HOME, GLUT_KEY_HOME ); KEY( VK_END, GLUT_KEY_END );
1600 KEY( VK_LEFT, GLUT_KEY_LEFT ); KEY( VK_UP, GLUT_KEY_UP );
1601 KEY( VK_RIGHT, GLUT_KEY_RIGHT ); KEY( VK_DOWN, GLUT_KEY_DOWN );
1602 KEY( VK_INSERT, GLUT_KEY_INSERT );
1605 * ...yet there is a small exception we need to have handled...
1609 * The delete key should be treated as an ASCII keypress:
1611 if( window->Callbacks.KeyboardUp != NULL )
1612 window->Callbacks.KeyboardUp( 127, window->State.MouseX, window->State.MouseY );
1615 * Call the KeyboardUp callback for a regular character if there is one.
1617 if( window->Callbacks.KeyboardUp != NULL )
1618 window->Callbacks.KeyboardUp( wParam, window->State.MouseX, window->State.MouseY );
1622 * Execute the special callback, if present, given the conversion was a success:
1624 if( (keypress != -1) && (window->Callbacks.SpecialUp != NULL) )
1627 * Have the special callback executed:
1629 window->Callbacks.SpecialUp( keypress, window->State.MouseX, window->State.MouseY );
1633 * Thrash the modifiers register now
1635 window->State.Modifiers = 0xffffffff;
1643 * Ignore the automatic key repetition if needed:
1645 if( fgState.IgnoreKeyRepeat && (lParam & KF_REPEAT) )
1649 * Clear to go with the keyboard callback, if registered:
1651 if( window->Callbacks.Keyboard != NULL )
1654 * Remember the current modifiers state
1656 window->State.Modifiers =
1657 ( ( (GetKeyState( VK_LSHIFT ) < 0 ) || ( GetKeyState( VK_RSHIFT ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
1658 ( ( (GetKeyState( VK_LCONTROL ) < 0 ) || ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL : 0 ) |
1659 ( ( (GetKeyState( VK_LMENU ) < 0 ) || ( GetKeyState( VK_RMENU ) < 0 )) ? GLUT_ACTIVE_ALT : 0 );
1662 * Have the special callback executed:
1664 window->Callbacks.Keyboard( wParam, window->State.MouseX, window->State.MouseY );
1667 * Thrash the modifiers register now
1669 window->State.Modifiers = 0xffffffff;
1674 case WM_CAPTURECHANGED : /* User has finished resizing the window, force a redraw */
1675 if ( window->Callbacks.Display )
1676 window->Callbacks.Display () ;
1678 // lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ) ;
1683 * Handle unhandled messages
1685 lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1693 /*** END OF FILE ***/