removed SetFocus that was added to WM_MOUSEMOVE to handle keyboard focus
[freeglut] / src / mswin / fg_main_mswin.c
1 /*
2  * freeglut_main_mswin.c
3  *
4  * The Windows-specific mouse cursor related stuff.
5  *
6  * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
7  * Written by John F. Fay, <fayjf@sourceforge.net>
8  * Creation date: Sat Jan 21, 2012
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 #include <GL/freeglut.h>
29 #include "../fg_internal.h"
30
31
32 extern void fghRedrawWindow ( SFG_Window *window );
33
34 extern void fgNewWGLCreateContext( SFG_Window* window );
35 extern GLboolean fgSetupPixelFormat( SFG_Window* window, GLboolean checkOnly,
36                                      unsigned char layer_type );
37
38 #ifdef WM_TOUCH
39 typedef BOOL (WINAPI *pGetTouchInputInfo)(HTOUCHINPUT,UINT,PTOUCHINPUT,int);
40 typedef BOOL (WINAPI *pCloseTouchInputHandle)(HTOUCHINPUT);
41 static pGetTouchInputInfo fghGetTouchInputInfo = (pGetTouchInputInfo)0xDEADBEEF;
42 static pCloseTouchInputHandle fghCloseTouchInputHandle = (pCloseTouchInputHandle)0xDEADBEEF;
43 #endif
44
45 #ifdef _WIN32_WCE
46 typedef struct GXDisplayProperties GXDisplayProperties;
47 typedef struct GXKeyList GXKeyList;
48 #include <gx.h>
49
50 typedef struct GXKeyList (*GXGETDEFAULTKEYS)(int);
51 typedef int (*GXOPENINPUT)();
52
53 GXGETDEFAULTKEYS GXGetDefaultKeys_ = NULL;
54 GXOPENINPUT GXOpenInput_ = NULL;
55
56 struct GXKeyList gxKeyList;
57 #endif /* _WIN32_WCE */
58
59 /* 
60  * Helper functions for getting client area from the window rect
61  * and the window rect from the client area given the style of the window
62  * (or a valid window pointer from which the style can be queried).
63  */
64 extern void fghComputeWindowRectFromClientArea_QueryWindow( const SFG_Window *window, RECT *clientRect, BOOL posIsOutside );
65 extern RECT fghGetClientArea                              ( const SFG_Window *window,                   BOOL wantPosOutside );
66
67
68 void fgPlatformReshapeWindow ( SFG_Window *window, int width, int height )
69 {
70     RECT windowRect;
71
72     /*
73      * For windowed mode, get the current position of the
74      * window and resize taking the size of the frame
75      * decorations into account.
76      */
77
78     /* "GetWindowRect" returns the pixel coordinates of the outside of the window */
79     GetWindowRect( window->Window.Handle, &windowRect );
80
81     /* Create rect in FreeGLUT format, (X,Y) topleft outside window, WxH of client area */
82     windowRect.right    = windowRect.left+width;
83     windowRect.bottom   = windowRect.top+height;
84
85     if (window->Parent == NULL)
86         /* get the window rect from this to feed to SetWindowPos, correct for window decorations */
87         fghComputeWindowRectFromClientArea_QueryWindow(window,&windowRect,TRUE);
88     else
89     {
90         /* correct rect for position client area of parent window
91          * (SetWindowPos input for child windows is in coordinates
92          * relative to the parent's client area).
93          * Child windows don't have decoration, so no need to correct
94          * for them.
95          */
96         RECT parentRect;
97         parentRect = fghGetClientArea( window->Parent, FALSE );
98         windowRect.left   -= parentRect.left;
99         windowRect.right  -= parentRect.left;
100         windowRect.top    -= parentRect.top;
101         windowRect.bottom -= parentRect.top;
102     }
103     
104     /* Do the actual resizing */
105     SetWindowPos( window->Window.Handle,
106                   HWND_TOP,
107                   windowRect.left, windowRect.top,
108                   windowRect.right - windowRect.left,
109                   windowRect.bottom- windowRect.top,
110                   SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING |
111                   SWP_NOZORDER
112     );
113 }
114
115
116 void fgPlatformDisplayWindow ( SFG_Window *window )
117 {
118     RedrawWindow(
119         window->Window.Handle, NULL, NULL,
120         RDW_NOERASE | RDW_INTERNALPAINT | RDW_INVALIDATE | RDW_UPDATENOW
121     );
122 }
123
124
125 fg_time_t fgPlatformSystemTime ( void )
126 {
127 #if defined(_WIN32_WCE)
128     return GetTickCount();
129 #else
130     /* TODO: do this with QueryPerformanceCounter as timeGetTime has
131      * insufficient resolution (only about 5 ms on system under low load).
132      * See:
133      * http://msdn.microsoft.com/en-us/library/windows/desktop/dd757629(v=vs.85).aspx
134      * Or maybe QueryPerformanceCounter is not a good idea either, see
135      * http://old.nabble.com/Re%3A-glutTimerFunc-does-not-detect-if-system-time-moved-backward-p33479674.html
136      * for some other ideas (at bottom)...
137      */
138     return timeGetTime();
139 #endif
140 }
141
142
143 void fgPlatformSleepForEvents( fg_time_t msec )
144 {
145     MsgWaitForMultipleObjects( 0, NULL, FALSE, (DWORD) msec, QS_ALLINPUT );
146 }
147
148
149 void fgPlatformProcessSingleEvent ( void )
150 {
151     MSG stMsg;
152
153     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutMainLoopEvent" );
154
155     while( PeekMessage( &stMsg, NULL, 0, 0, PM_NOREMOVE ) )
156     {
157         if( GetMessage( &stMsg, NULL, 0, 0 ) == 0 )
158         {
159             if( fgState.ActionOnWindowClose == GLUT_ACTION_EXIT )
160             {
161                 fgDeinitialize( );
162                 exit( 0 );
163             }
164             else if( fgState.ActionOnWindowClose == GLUT_ACTION_GLUTMAINLOOP_RETURNS )
165                 fgState.ExecState = GLUT_EXEC_STATE_STOP;
166
167             return;
168         }
169
170         TranslateMessage( &stMsg );
171         DispatchMessage( &stMsg );
172     }
173 }
174
175
176
177 void fgPlatformMainLoopPreliminaryWork ( void )
178 {
179     SFG_Window *window = (SFG_Window *)fgStructure.Windows.First ;
180
181     /*
182      * Processing before the main loop:  If there is a window which is open and
183      * which has a visibility callback, call it.  I know this is an ugly hack,
184      * but I'm not sure what else to do about it.  Ideally we should leave
185      * something uninitialized in the create window code and initialize it in
186      * the main loop, and have that initialization create a "WM_ACTIVATE"
187      * message.  Then we would put the visibility callback code in the
188      * "case WM_ACTIVATE" block below.         - John Fay -- 10/24/02
189      */
190     while( window )
191     {
192         if ( FETCH_WCB( *window, Visibility ) )
193         {
194             SFG_Window *current_window = fgStructure.CurrentWindow ;
195
196             INVOKE_WCB( *window, Visibility, ( window->State.Visible ) );
197             fgSetWindow( current_window );
198         }
199
200         window = (SFG_Window *)window->Node.Next ;
201     }
202 }
203
204
205 /*
206  * Determine a GLUT modifer mask based on MS-WINDOWS system info.
207  */
208 static int fgPlatformGetModifiers (void)
209 {
210     return
211         ( ( ( GetKeyState( VK_LSHIFT   ) < 0 ) ||
212             ( GetKeyState( VK_RSHIFT   ) < 0 )) ? GLUT_ACTIVE_SHIFT : 0 ) |
213         ( ( ( GetKeyState( VK_LCONTROL ) < 0 ) ||
214             ( GetKeyState( VK_RCONTROL ) < 0 )) ? GLUT_ACTIVE_CTRL  : 0 ) |
215         ( ( ( GetKeyState( VK_LMENU    ) < 0 ) ||
216             ( GetKeyState( VK_RMENU    ) < 0 )) ? GLUT_ACTIVE_ALT   : 0 );
217 }
218
219 /*
220  * The window procedure for handling Win32 events
221  */
222 LRESULT CALLBACK fgPlatformWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
223                                        LPARAM lParam )
224 {
225     static unsigned char lControl = 0, rControl = 0, lShift = 0,
226                          rShift = 0, lAlt = 0, rAlt = 0;
227
228     SFG_Window* window;
229     PAINTSTRUCT ps;
230     LRESULT lRet = 1;
231
232     FREEGLUT_INTERNAL_ERROR_EXIT_IF_NOT_INITIALISED ( "Event Handler" ) ;
233
234     window = fgWindowByHandle( hWnd );
235
236     if ( ( window == NULL ) && ( uMsg != WM_CREATE ) )
237       return DefWindowProc( hWnd, uMsg, wParam, lParam );
238
239     /* printf ( "Window %3d message <%04x> %12d %12d\n", window?window->ID:0,
240              uMsg, wParam, lParam ); */
241
242     /* events only sent to main window. Check if the current window that the mouse
243        is over is a child window and if so, make sure we call the callback on that
244        child instead.
245     */
246     if (window && window->Children.First)
247     {
248         POINT mouse_pos;
249         SFG_WindowHandleType hwnd;
250
251         GetCursorPos( &mouse_pos );
252         ScreenToClient( window->Window.Handle, &mouse_pos );
253         hwnd = ChildWindowFromPoint(window->Window.Handle, mouse_pos);
254         window = fgWindowByHandle(hwnd);
255     }
256
257     if ( window )
258     {
259       fgState.Modifiers = fgPlatformGetModifiers( );
260
261       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Down! */
262       if ( !lControl && GetAsyncKeyState ( VK_LCONTROL ) )
263       {
264           INVOKE_WCB    ( *window, Special,
265                         ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
266                       );
267
268           lControl = 1;
269       }
270
271       if ( !rControl && GetAsyncKeyState ( VK_RCONTROL ) )
272       {
273           INVOKE_WCB ( *window, Special,
274                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
275                      );
276
277           rControl = 1;
278       }
279
280       if ( !lShift && GetAsyncKeyState ( VK_LSHIFT ) )
281       {
282           INVOKE_WCB ( *window, Special,
283                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
284                      );
285
286           lShift = 1;
287       }
288
289       if ( !rShift && GetAsyncKeyState ( VK_RSHIFT ) )
290       {
291           INVOKE_WCB ( *window, Special,
292                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
293                      );
294
295           rShift = 1;
296       }
297
298       if ( !lAlt && GetAsyncKeyState ( VK_LMENU ) )
299       {
300           INVOKE_WCB ( *window, Special,
301                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
302                      );
303
304           lAlt = 1;
305       }
306
307       if ( !rAlt && GetAsyncKeyState ( VK_RMENU ) )
308       {
309           INVOKE_WCB ( *window, Special,
310                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
311                      );
312
313           rAlt = 1;
314       }
315
316       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Up! */
317       if ( lControl && !GetAsyncKeyState ( VK_LCONTROL ) )
318       {
319           INVOKE_WCB ( *window, SpecialUp,
320                        ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
321                      );
322
323           lControl = 0;
324       }
325
326       if ( rControl && !GetAsyncKeyState ( VK_RCONTROL ) )
327       {
328           INVOKE_WCB ( *window, SpecialUp,
329                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
330                      );
331
332           rControl = 0;
333       }
334
335       if ( lShift && !GetAsyncKeyState ( VK_LSHIFT ) )
336       {
337           INVOKE_WCB ( *window, SpecialUp,
338                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
339                      );
340
341           lShift = 0;
342       }
343
344       if ( rShift && !GetAsyncKeyState ( VK_RSHIFT ) )
345       {
346           INVOKE_WCB ( *window, SpecialUp,
347                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
348                      );
349
350           rShift = 0;
351       }
352
353       if ( lAlt && !GetAsyncKeyState ( VK_LMENU ) )
354       {
355           INVOKE_WCB ( *window, SpecialUp,
356                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
357                      );
358
359           lAlt = 0;
360       }
361
362       if ( rAlt && !GetAsyncKeyState ( VK_RMENU ) )
363       {
364           INVOKE_WCB ( *window, SpecialUp,
365                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
366                      );
367
368           rAlt = 0;
369       }
370
371       fgState.Modifiers = INVALID_MODIFIERS;
372     }
373
374     switch( uMsg )
375     {
376     case WM_CREATE:
377         /* The window structure is passed as the creation structure parameter... */
378         window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
379         FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
380                                        "fgPlatformWindowProc" );
381
382         window->Window.Handle = hWnd;
383         window->Window.pContext.Device = GetDC( hWnd );
384         if( window->IsMenu )
385         {
386             unsigned int current_DisplayMode = fgState.DisplayMode;
387             fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
388 #if !defined(_WIN32_WCE)
389             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
390 #endif
391             fgState.DisplayMode = current_DisplayMode;
392
393             if( fgStructure.MenuContext )
394                 wglMakeCurrent( window->Window.pContext.Device,
395                                 fgStructure.MenuContext->MContext
396                 );
397             else
398             {
399                 fgStructure.MenuContext =
400                     (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
401                 fgStructure.MenuContext->MContext =
402                     wglCreateContext( window->Window.pContext.Device );
403             }
404
405             /* window->Window.Context = wglGetCurrentContext ();   */
406             window->Window.Context = wglCreateContext( window->Window.pContext.Device );
407         }
408         else
409         {
410 #if !defined(_WIN32_WCE)
411             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
412 #endif
413
414             if( ! fgState.UseCurrentContext )
415                 window->Window.Context =
416                     wglCreateContext( window->Window.pContext.Device );
417             else
418             {
419                 window->Window.Context = wglGetCurrentContext( );
420                 if( ! window->Window.Context )
421                     window->Window.Context =
422                         wglCreateContext( window->Window.pContext.Device );
423             }
424
425 #if !defined(_WIN32_WCE)
426             fgNewWGLCreateContext( window );
427 #endif
428         }
429
430         window->State.NeedToResize = GL_TRUE;
431         /* if we used CW_USEDEFAULT (thats a negative value) for the size
432          * of the window, query the window now for the size at which it
433          * was created.
434          */
435         if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )
436         {
437             SFG_Window *current_window = fgStructure.CurrentWindow;
438
439             fgSetWindow( window );
440             window->State.Width = glutGet( GLUT_WINDOW_WIDTH );
441             window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );
442             fgSetWindow( current_window );
443         }
444
445         ReleaseDC( window->Window.Handle, window->Window.pContext.Device );
446
447 #if defined(_WIN32_WCE)
448         /* Take over button handling */
449         {
450             HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
451             if (dxDllLib)
452             {
453                 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
454                 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
455             }
456
457             if(GXOpenInput_)
458                 (*GXOpenInput_)();
459             if(GXGetDefaultKeys_)
460                 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
461         }
462
463 #endif /* defined(_WIN32_WCE) */
464         break;
465
466     case WM_SIZE:
467         /*
468          * If the window is visible, then it is the user manually resizing it.
469          * If it is not, then it is the system sending us a dummy resize with
470          * zero dimensions on a "glutIconifyWindow" call.
471          */
472         if( window->State.Visible )
473         {
474             window->State.NeedToResize = GL_TRUE;
475 #if defined(_WIN32_WCE)
476             window->State.Width  = HIWORD(lParam);
477             window->State.Height = LOWORD(lParam);
478 #else
479             window->State.Width  = LOWORD(lParam);
480             window->State.Height = HIWORD(lParam);
481 #endif /* defined(_WIN32_WCE) */
482         }
483
484         break;
485
486     case WM_SETFOCUS:
487 /*        printf("WM_SETFOCUS: %p\n", window ); */
488         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
489         INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );
490
491                 UpdateWindow ( hWnd );
492         break;
493
494     case WM_KILLFOCUS:
495 /*        printf("WM_KILLFOCUS: %p\n", window ); */
496         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
497         INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
498
499         if( window->IsMenu &&
500             window->ActiveMenu && window->ActiveMenu->IsActive )
501             fgUpdateMenuHighlight( window->ActiveMenu );
502
503         break;
504
505 #if 0
506     case WM_ACTIVATE:
507         if (LOWORD(wParam) != WA_INACTIVE)
508         {
509 /*            printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,
510                    window->State.Cursor ); */
511             fgSetCursor( window, window->State.Cursor );
512         }
513
514         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
515         break;
516 #endif
517
518     case WM_SETCURSOR:
519 /*      printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */
520         if( LOWORD( lParam ) == HTCLIENT )
521             fgSetCursor ( window, window->State.Cursor ) ;
522         else
523             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
524         break;
525
526     case WM_SHOWWINDOW:
527         window->State.Visible = GL_TRUE;
528         window->State.Redisplay = GL_TRUE;
529         break;
530
531     case WM_PAINT:
532         /* Turn on the visibility in case it was turned off somehow */
533         window->State.Visible = GL_TRUE;
534         BeginPaint( hWnd, &ps );
535         fghRedrawWindow( window );
536         EndPaint( hWnd, &ps );
537         break;
538
539     case WM_CLOSE:
540         fgDestroyWindow ( window );
541         if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
542             PostQuitMessage(0);
543         break;
544
545     case WM_DESTROY:
546         /*
547          * The window already got destroyed, so don't bother with it.
548          */
549         return 0;
550
551     case WM_MOUSEMOVE:
552     {
553 #if defined(_WIN32_WCE)
554         window->State.MouseX = 320-HIWORD( lParam );
555         window->State.MouseY = LOWORD( lParam );
556 #else
557         window->State.MouseX = LOWORD( lParam );
558         window->State.MouseY = HIWORD( lParam );
559 #endif /* defined(_WIN32_WCE) */
560         /* Restrict to [-32768, 32767] to match X11 behaviour       */
561         /* See comment in "freeglut_developer" mailing list 10/4/04 */
562         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
563         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
564
565         if ( window->ActiveMenu )
566         {
567             fgUpdateMenuHighlight( window->ActiveMenu );
568             break;
569         }
570
571         fgState.Modifiers = fgPlatformGetModifiers( );
572
573         if( ( wParam & MK_LBUTTON ) ||
574             ( wParam & MK_MBUTTON ) ||
575             ( wParam & MK_RBUTTON ) )
576             INVOKE_WCB( *window, Motion, ( window->State.MouseX,
577                                            window->State.MouseY ) );
578         else
579             INVOKE_WCB( *window, Passive, ( window->State.MouseX,
580                                             window->State.MouseY ) );
581
582         fgState.Modifiers = INVALID_MODIFIERS;
583     }
584     break;
585
586     case WM_LBUTTONDOWN:
587     case WM_MBUTTONDOWN:
588     case WM_RBUTTONDOWN:
589     case WM_LBUTTONUP:
590     case WM_MBUTTONUP:
591     case WM_RBUTTONUP:
592     {
593         GLboolean pressed = GL_TRUE;
594         int button;
595
596 #if defined(_WIN32_WCE)
597         window->State.MouseX = 320-HIWORD( lParam );
598         window->State.MouseY = LOWORD( lParam );
599 #else
600         window->State.MouseX = LOWORD( lParam );
601         window->State.MouseY = HIWORD( lParam );
602 #endif /* defined(_WIN32_WCE) */
603
604         /* Restrict to [-32768, 32767] to match X11 behaviour       */
605         /* See comment in "freeglut_developer" mailing list 10/4/04 */
606         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
607         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
608
609         switch( uMsg )
610         {
611         case WM_LBUTTONDOWN:
612             pressed = GL_TRUE;
613             button = GLUT_LEFT_BUTTON;
614             break;
615         case WM_MBUTTONDOWN:
616             pressed = GL_TRUE;
617             button = GLUT_MIDDLE_BUTTON;
618             break;
619         case WM_RBUTTONDOWN:
620             pressed = GL_TRUE;
621             button = GLUT_RIGHT_BUTTON;
622             break;
623         case WM_LBUTTONUP:
624             pressed = GL_FALSE;
625             button = GLUT_LEFT_BUTTON;
626             break;
627         case WM_MBUTTONUP:
628             pressed = GL_FALSE;
629             button = GLUT_MIDDLE_BUTTON;
630             break;
631         case WM_RBUTTONUP:
632             pressed = GL_FALSE;
633             button = GLUT_RIGHT_BUTTON;
634             break;
635         default:
636             pressed = GL_FALSE;
637             button = -1;
638             break;
639         }
640
641 #if !defined(_WIN32_WCE)
642         if( GetSystemMetrics( SM_SWAPBUTTON ) )
643         {
644             if( button == GLUT_LEFT_BUTTON )
645                 button = GLUT_RIGHT_BUTTON;
646             else
647                 if( button == GLUT_RIGHT_BUTTON )
648                     button = GLUT_LEFT_BUTTON;
649         }
650 #endif /* !defined(_WIN32_WCE) */
651
652         if( button == -1 )
653             return DefWindowProc( hWnd, uMsg, lParam, wParam );
654
655         /*
656          * Do not execute the application's mouse callback if a menu
657          * is hooked to this button.  In that case an appropriate
658          * private call should be generated.
659          */
660         if( fgCheckActiveMenu( window, button, pressed,
661                                window->State.MouseX, window->State.MouseY ) )
662             break;
663
664         /* Set capture so that the window captures all the mouse messages */
665         /*
666          * XXX - Multiple button support:  Under X11, the mouse is not released
667          * XXX - from the window until all buttons have been released, even if the
668          * XXX - user presses a button in another window.  This will take more
669          * XXX - code changes than I am up to at the moment (10/5/04).  The present
670          * XXX - is a 90 percent solution.
671          */
672         if ( pressed == GL_TRUE )
673           SetCapture ( window->Window.Handle ) ;
674         else
675           ReleaseCapture () ;
676
677         if( ! FETCH_WCB( *window, Mouse ) )
678             break;
679
680         fgSetWindow( window );
681         fgState.Modifiers = fgPlatformGetModifiers( );
682
683         INVOKE_WCB(
684             *window, Mouse,
685             ( button,
686               pressed ? GLUT_DOWN : GLUT_UP,
687               window->State.MouseX,
688               window->State.MouseY
689             )
690         );
691
692         fgState.Modifiers = INVALID_MODIFIERS;
693     }
694     break;
695
696     case WM_MOUSEWHEEL:
697     {
698         int wheel_number = LOWORD( wParam );
699         short ticks = ( short )HIWORD( wParam );
700                 fgState.MouseWheelTicks += ticks;
701
702         /*
703          * XXX Should use WHEEL_DELTA instead of 120
704          */
705                 if ( abs ( fgState.MouseWheelTicks ) >= 120 )
706                 {
707                         int direction = ( fgState.MouseWheelTicks > 0 ) ? 1 : -1;
708
709             if( ! FETCH_WCB( *window, MouseWheel ) &&
710                 ! FETCH_WCB( *window, Mouse ) )
711                 break;
712
713             fgSetWindow( window );
714             fgState.Modifiers = fgPlatformGetModifiers( );
715
716             /*
717              * XXX Should use WHEEL_DELTA instead of 120
718              */
719             while( abs ( fgState.MouseWheelTicks ) >= 120 )
720                         {
721                 if( FETCH_WCB( *window, MouseWheel ) )
722                     INVOKE_WCB( *window, MouseWheel,
723                                 ( wheel_number,
724                                   direction,
725                                   window->State.MouseX,
726                                   window->State.MouseY
727                                 )
728                     );
729                 else  /* No mouse wheel, call the mouse button callback twice */
730                                 {
731                     /*
732                      * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
733                      *  "    "   one                     +1 to 5, -1 to 6, ...
734                      *
735                      * XXX The below assumes that you have no more than 3 mouse
736                      * XXX buttons.  Sorry.
737                      */
738                     int button = wheel_number * 2 + 3;
739                     if( direction < 0 )
740                         ++button;
741                     INVOKE_WCB( *window, Mouse,
742                                 ( button, GLUT_DOWN,
743                                   window->State.MouseX, window->State.MouseY )
744                     );
745                     INVOKE_WCB( *window, Mouse,
746                                 ( button, GLUT_UP,
747                                   window->State.MouseX, window->State.MouseY )
748                     );
749                                 }
750
751                 /*
752                  * XXX Should use WHEEL_DELTA instead of 120
753                  */
754                                 fgState.MouseWheelTicks -= 120 * direction;
755                         }
756
757             fgState.Modifiers = INVALID_MODIFIERS;
758                 }
759     }
760     break ;
761
762     case WM_SYSKEYDOWN:
763     case WM_KEYDOWN:
764     {
765         int keypress = -1;
766         POINT mouse_pos ;
767
768
769         if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
770             break;
771
772         /*
773          * Remember the current modifiers state. This is done here in order
774          * to make sure the VK_DELETE keyboard callback is executed properly.
775          */
776         fgState.Modifiers = fgPlatformGetModifiers( );
777
778         GetCursorPos( &mouse_pos );
779         ScreenToClient( window->Window.Handle, &mouse_pos );
780
781         window->State.MouseX = mouse_pos.x;
782         window->State.MouseY = mouse_pos.y;
783
784         /* Convert the Win32 keystroke codes to GLUTtish way */
785 #       define KEY(a,b) case a: keypress = b; break;
786
787         switch( wParam )
788         {
789             KEY( VK_F1,     GLUT_KEY_F1        );
790             KEY( VK_F2,     GLUT_KEY_F2        );
791             KEY( VK_F3,     GLUT_KEY_F3        );
792             KEY( VK_F4,     GLUT_KEY_F4        );
793             KEY( VK_F5,     GLUT_KEY_F5        );
794             KEY( VK_F6,     GLUT_KEY_F6        );
795             KEY( VK_F7,     GLUT_KEY_F7        );
796             KEY( VK_F8,     GLUT_KEY_F8        );
797             KEY( VK_F9,     GLUT_KEY_F9        );
798             KEY( VK_F10,    GLUT_KEY_F10       );
799             KEY( VK_F11,    GLUT_KEY_F11       );
800             KEY( VK_F12,    GLUT_KEY_F12       );
801             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
802             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
803             KEY( VK_HOME,   GLUT_KEY_HOME      );
804             KEY( VK_END,    GLUT_KEY_END       );
805             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
806             KEY( VK_UP,     GLUT_KEY_UP        );
807             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
808             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
809             KEY( VK_INSERT, GLUT_KEY_INSERT    );
810
811         case VK_LCONTROL:  case VK_RCONTROL:  case VK_CONTROL:
812         case VK_LSHIFT:    case VK_RSHIFT:    case VK_SHIFT:
813         case VK_LMENU:     case VK_RMENU:     case VK_MENU:
814             /* These keypresses and releases are handled earlier in the function */
815             break;
816
817         case VK_DELETE:
818             /* The delete key should be treated as an ASCII keypress: */
819             INVOKE_WCB( *window, Keyboard,
820                         ( 127, window->State.MouseX, window->State.MouseY )
821             );
822         }
823
824 #if defined(_WIN32_WCE)
825         if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
826         {
827             if(wParam==(unsigned)gxKeyList.vkRight)
828                 keypress = GLUT_KEY_RIGHT;
829             else if(wParam==(unsigned)gxKeyList.vkLeft)
830                 keypress = GLUT_KEY_LEFT;
831             else if(wParam==(unsigned)gxKeyList.vkUp)
832                 keypress = GLUT_KEY_UP;
833             else if(wParam==(unsigned)gxKeyList.vkDown)
834                 keypress = GLUT_KEY_DOWN;
835             else if(wParam==(unsigned)gxKeyList.vkA)
836                 keypress = GLUT_KEY_F1;
837             else if(wParam==(unsigned)gxKeyList.vkB)
838                 keypress = GLUT_KEY_F2;
839             else if(wParam==(unsigned)gxKeyList.vkC)
840                 keypress = GLUT_KEY_F3;
841             else if(wParam==(unsigned)gxKeyList.vkStart)
842                 keypress = GLUT_KEY_F4;
843         }
844 #endif
845
846         if( keypress != -1 )
847             INVOKE_WCB( *window, Special,
848                         ( keypress,
849                           window->State.MouseX, window->State.MouseY )
850             );
851
852         fgState.Modifiers = INVALID_MODIFIERS;
853     }
854     break;
855
856     case WM_SYSKEYUP:
857     case WM_KEYUP:
858     {
859         int keypress = -1;
860         POINT mouse_pos;
861
862         /*
863          * Remember the current modifiers state. This is done here in order
864          * to make sure the VK_DELETE keyboard callback is executed properly.
865          */
866         fgState.Modifiers = fgPlatformGetModifiers( );
867
868         GetCursorPos( &mouse_pos );
869         ScreenToClient( window->Window.Handle, &mouse_pos );
870
871         window->State.MouseX = mouse_pos.x;
872         window->State.MouseY = mouse_pos.y;
873
874         /*
875          * Convert the Win32 keystroke codes to GLUTtish way.
876          * "KEY(a,b)" was defined under "WM_KEYDOWN"
877          */
878
879         switch( wParam )
880         {
881             KEY( VK_F1,     GLUT_KEY_F1        );
882             KEY( VK_F2,     GLUT_KEY_F2        );
883             KEY( VK_F3,     GLUT_KEY_F3        );
884             KEY( VK_F4,     GLUT_KEY_F4        );
885             KEY( VK_F5,     GLUT_KEY_F5        );
886             KEY( VK_F6,     GLUT_KEY_F6        );
887             KEY( VK_F7,     GLUT_KEY_F7        );
888             KEY( VK_F8,     GLUT_KEY_F8        );
889             KEY( VK_F9,     GLUT_KEY_F9        );
890             KEY( VK_F10,    GLUT_KEY_F10       );
891             KEY( VK_F11,    GLUT_KEY_F11       );
892             KEY( VK_F12,    GLUT_KEY_F12       );
893             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
894             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
895             KEY( VK_HOME,   GLUT_KEY_HOME      );
896             KEY( VK_END,    GLUT_KEY_END       );
897             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
898             KEY( VK_UP,     GLUT_KEY_UP        );
899             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
900             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
901             KEY( VK_INSERT, GLUT_KEY_INSERT    );
902
903           case VK_LCONTROL:  case VK_RCONTROL:  case VK_CONTROL:
904           case VK_LSHIFT:    case VK_RSHIFT:    case VK_SHIFT:
905           case VK_LMENU:     case VK_RMENU:     case VK_MENU:
906               /* These keypresses and releases are handled earlier in the function */
907               break;
908
909           case VK_DELETE:
910               /* The delete key should be treated as an ASCII keypress: */
911               INVOKE_WCB( *window, KeyboardUp,
912                           ( 127, window->State.MouseX, window->State.MouseY )
913               );
914               break;
915
916         default:
917         {
918 #if !defined(_WIN32_WCE)
919             BYTE state[ 256 ];
920             WORD code[ 2 ];
921
922             GetKeyboardState( state );
923
924             if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )
925                 wParam=code[ 0 ];
926
927             INVOKE_WCB( *window, KeyboardUp,
928                         ( (char)wParam,
929                           window->State.MouseX, window->State.MouseY )
930             );
931 #endif /* !defined(_WIN32_WCE) */
932         }
933         }
934
935         if( keypress != -1 )
936             INVOKE_WCB( *window, SpecialUp,
937                         ( keypress,
938                           window->State.MouseX, window->State.MouseY )
939             );
940
941         fgState.Modifiers = INVALID_MODIFIERS;
942     }
943     break;
944
945     case WM_SYSCHAR:
946     case WM_CHAR:
947     {
948       if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
949             break;
950
951         fgState.Modifiers = fgPlatformGetModifiers( );
952         INVOKE_WCB( *window, Keyboard,
953                     ( (char)wParam,
954                       window->State.MouseX, window->State.MouseY )
955         );
956         fgState.Modifiers = INVALID_MODIFIERS;
957     }
958     break;
959
960     case WM_CAPTURECHANGED:
961         /* User has finished resizing the window, force a redraw */
962         INVOKE_WCB( *window, Display, ( ) );
963
964         /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
965         break;
966
967         /* Other messages that I have seen and which are not handled already */
968     case WM_SETTEXT:  /* 0x000c */
969         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
970         /* Pass it on to "DefWindowProc" to set the window text */
971         break;
972
973     case WM_GETTEXT:  /* 0x000d */
974         /* Ideally we would copy the title of the window into "lParam" */
975         /* strncpy ( (char *)lParam, "Window Title", wParam );
976            lRet = ( wParam > 12 ) ? 12 : wParam;  */
977         /* the number of characters copied */
978         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
979         break;
980
981     case WM_GETTEXTLENGTH:  /* 0x000e */
982         /* Ideally we would get the length of the title of the window */
983         lRet = 12;
984         /* the number of characters in "Window Title\0" (see above) */
985         break;
986
987     case WM_ERASEBKGND:  /* 0x0014 */
988         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
989         break;
990
991 #if !defined(_WIN32_WCE)
992     case WM_SYNCPAINT:  /* 0x0088 */
993         /* Another window has moved, need to update this one */
994         window->State.Redisplay = GL_TRUE;
995         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
996         /* Help screen says this message must be passed to "DefWindowProc" */
997         break;
998
999     case WM_NCPAINT:  /* 0x0085 */
1000       /* Need to update the border of this window */
1001         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1002         /* Pass it on to "DefWindowProc" to repaint a standard border */
1003         break;
1004
1005     case WM_SYSCOMMAND :  /* 0x0112 */
1006         {
1007           /*
1008            * We have received a system command message.  Try to act on it.
1009            * The commands are passed in through the "wParam" parameter:
1010            * The least significant digit seems to be which edge of the window
1011            * is being used for a resize event:
1012            *     4  3  5
1013            *     1     2
1014            *     7  6  8
1015            * Congratulations and thanks to Richard Rauch for figuring this out..
1016            */
1017             switch ( wParam & 0xfff0 )
1018             {
1019             case SC_SIZE       :
1020                 break ;
1021
1022             case SC_MOVE       :
1023                 break ;
1024
1025             case SC_MINIMIZE   :
1026                 /* User has clicked on the "-" to minimize the window */
1027                 /* Turn off the visibility */
1028                 window->State.Visible = GL_FALSE ;
1029
1030                 break ;
1031
1032             case SC_MAXIMIZE   :
1033                 break ;
1034
1035             case SC_NEXTWINDOW :
1036                 break ;
1037
1038             case SC_PREVWINDOW :
1039                 break ;
1040
1041             case SC_CLOSE      :
1042                 /* Followed very closely by a WM_CLOSE message */
1043                 break ;
1044
1045             case SC_VSCROLL    :
1046                 break ;
1047
1048             case SC_HSCROLL    :
1049                 break ;
1050
1051             case SC_MOUSEMENU  :
1052                 break ;
1053
1054             case SC_KEYMENU    :
1055                 break ;
1056
1057             case SC_ARRANGE    :
1058                 break ;
1059
1060             case SC_RESTORE    :
1061                 break ;
1062
1063             case SC_TASKLIST   :
1064                 break ;
1065
1066             case SC_SCREENSAVE :
1067                 break ;
1068
1069             case SC_HOTKEY     :
1070                 break ;
1071
1072 #if(WINVER >= 0x0400)
1073             case SC_DEFAULT    :
1074                 break ;
1075
1076             case SC_MONITORPOWER    :
1077                 break ;
1078
1079             case SC_CONTEXTHELP    :
1080                 break ;
1081 #endif /* WINVER >= 0x0400 */
1082
1083             default:
1084 #if _DEBUG
1085                 fgWarning( "Unknown wParam type 0x%x", wParam );
1086 #endif
1087                 break;
1088             }
1089         }
1090 #endif /* !defined(_WIN32_WCE) */
1091
1092         /* We need to pass the message on to the operating system as well */
1093         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1094         break;
1095
1096 #ifdef WM_TOUCH
1097         /* handle multi-touch messages */
1098         case WM_TOUCH:
1099         {
1100                 unsigned int numInputs = (unsigned int)wParam;
1101                 unsigned int i = 0;
1102                 TOUCHINPUT* ti = (TOUCHINPUT*)malloc( sizeof(TOUCHINPUT)*numInputs);
1103
1104                 if (fghGetTouchInputInfo == (pGetTouchInputInfo)0xDEADBEEF) {
1105                     fghGetTouchInputInfo = (pGetTouchInputInfo)GetProcAddress(GetModuleHandle("user32"),"GetTouchInputInfo");
1106                     fghCloseTouchInputHandle = (pCloseTouchInputHandle)GetProcAddress(GetModuleHandle("user32"),"CloseTouchInputHandle");
1107                 }
1108
1109                 if (!fghGetTouchInputInfo) { 
1110                         free( (void*)ti );
1111                         break;
1112                 }
1113
1114                 if (fghGetTouchInputInfo( (HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT) )) {
1115                         /* Handle each contact point */
1116                         for (i = 0; i < numInputs; ++i ) {
1117
1118                                 POINT tp;
1119                                 tp.x = TOUCH_COORD_TO_PIXEL(ti[i].x);
1120                                 tp.y = TOUCH_COORD_TO_PIXEL(ti[i].y);
1121                                 ScreenToClient( hWnd, &tp );
1122
1123                                 ti[i].dwID = ti[i].dwID * 2;
1124
1125                                 if (ti[i].dwFlags & TOUCHEVENTF_DOWN) {
1126                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_ENTERED ) );
1127                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_DOWN ) );
1128                                 } else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {
1129                                         INVOKE_WCB( *window, MultiMotion, ( ti[i].dwID, tp.x, tp.y ) );
1130                                 } else if (ti[i].dwFlags & TOUCHEVENTF_UP)   { 
1131                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_UP ) );
1132                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_LEFT ) );
1133                                 }
1134                         }
1135                 }
1136                 fghCloseTouchInputHandle((HTOUCHINPUT)lParam);
1137                 free( (void*)ti );
1138                 lRet = 0; /*DefWindowProc( hWnd, uMsg, wParam, lParam );*/
1139                 break;
1140         }
1141 #endif
1142     default:
1143         /* Handle unhandled messages */
1144         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1145         break;
1146     }
1147
1148     return lRet;
1149 }