c777b46a8278186e9f94501939a572f6fc223cbd
[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         if (hwnd)   /* can be NULL if mouse outside parent by the time we get here */
255             window = fgWindowByHandle(hwnd);
256     }
257
258     if ( window )
259     {
260       fgState.Modifiers = fgPlatformGetModifiers( );
261
262       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Down! */
263       if ( !lControl && GetAsyncKeyState ( VK_LCONTROL ) )
264       {
265           INVOKE_WCB    ( *window, Special,
266                         ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
267                       );
268
269           lControl = 1;
270       }
271
272       if ( !rControl && GetAsyncKeyState ( VK_RCONTROL ) )
273       {
274           INVOKE_WCB ( *window, Special,
275                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
276                      );
277
278           rControl = 1;
279       }
280
281       if ( !lShift && GetAsyncKeyState ( VK_LSHIFT ) )
282       {
283           INVOKE_WCB ( *window, Special,
284                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
285                      );
286
287           lShift = 1;
288       }
289
290       if ( !rShift && GetAsyncKeyState ( VK_RSHIFT ) )
291       {
292           INVOKE_WCB ( *window, Special,
293                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
294                      );
295
296           rShift = 1;
297       }
298
299       if ( !lAlt && GetAsyncKeyState ( VK_LMENU ) )
300       {
301           INVOKE_WCB ( *window, Special,
302                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
303                      );
304
305           lAlt = 1;
306       }
307
308       if ( !rAlt && GetAsyncKeyState ( VK_RMENU ) )
309       {
310           INVOKE_WCB ( *window, Special,
311                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
312                      );
313
314           rAlt = 1;
315       }
316
317       /* Checking for CTRL, ALT, and SHIFT key positions:  Key Up! */
318       if ( lControl && !GetAsyncKeyState ( VK_LCONTROL ) )
319       {
320           INVOKE_WCB ( *window, SpecialUp,
321                        ( GLUT_KEY_CTRL_L, window->State.MouseX, window->State.MouseY )
322                      );
323
324           lControl = 0;
325       }
326
327       if ( rControl && !GetAsyncKeyState ( VK_RCONTROL ) )
328       {
329           INVOKE_WCB ( *window, SpecialUp,
330                        ( GLUT_KEY_CTRL_R, window->State.MouseX, window->State.MouseY )
331                      );
332
333           rControl = 0;
334       }
335
336       if ( lShift && !GetAsyncKeyState ( VK_LSHIFT ) )
337       {
338           INVOKE_WCB ( *window, SpecialUp,
339                        ( GLUT_KEY_SHIFT_L, window->State.MouseX, window->State.MouseY )
340                      );
341
342           lShift = 0;
343       }
344
345       if ( rShift && !GetAsyncKeyState ( VK_RSHIFT ) )
346       {
347           INVOKE_WCB ( *window, SpecialUp,
348                        ( GLUT_KEY_SHIFT_R, window->State.MouseX, window->State.MouseY )
349                      );
350
351           rShift = 0;
352       }
353
354       if ( lAlt && !GetAsyncKeyState ( VK_LMENU ) )
355       {
356           INVOKE_WCB ( *window, SpecialUp,
357                        ( GLUT_KEY_ALT_L, window->State.MouseX, window->State.MouseY )
358                      );
359
360           lAlt = 0;
361       }
362
363       if ( rAlt && !GetAsyncKeyState ( VK_RMENU ) )
364       {
365           INVOKE_WCB ( *window, SpecialUp,
366                        ( GLUT_KEY_ALT_R, window->State.MouseX, window->State.MouseY )
367                      );
368
369           rAlt = 0;
370       }
371
372       fgState.Modifiers = INVALID_MODIFIERS;
373     }
374
375     switch( uMsg )
376     {
377     case WM_CREATE:
378         /* The window structure is passed as the creation structure parameter... */
379         window = (SFG_Window *) (((LPCREATESTRUCT) lParam)->lpCreateParams);
380         FREEGLUT_INTERNAL_ERROR_EXIT ( ( window != NULL ), "Cannot create window",
381                                        "fgPlatformWindowProc" );
382
383         window->Window.Handle = hWnd;
384         window->Window.pContext.Device = GetDC( hWnd );
385         if( window->IsMenu )
386         {
387             unsigned int current_DisplayMode = fgState.DisplayMode;
388             fgState.DisplayMode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
389 #if !defined(_WIN32_WCE)
390             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
391 #endif
392             fgState.DisplayMode = current_DisplayMode;
393
394             if( fgStructure.MenuContext )
395                 wglMakeCurrent( window->Window.pContext.Device,
396                                 fgStructure.MenuContext->MContext
397                 );
398             else
399             {
400                 fgStructure.MenuContext =
401                     (SFG_MenuContext *)malloc( sizeof(SFG_MenuContext) );
402                 fgStructure.MenuContext->MContext =
403                     wglCreateContext( window->Window.pContext.Device );
404             }
405
406             /* window->Window.Context = wglGetCurrentContext ();   */
407             window->Window.Context = wglCreateContext( window->Window.pContext.Device );
408         }
409         else
410         {
411 #if !defined(_WIN32_WCE)
412             fgSetupPixelFormat( window, GL_FALSE, PFD_MAIN_PLANE );
413 #endif
414
415             if( ! fgState.UseCurrentContext )
416                 window->Window.Context =
417                     wglCreateContext( window->Window.pContext.Device );
418             else
419             {
420                 window->Window.Context = wglGetCurrentContext( );
421                 if( ! window->Window.Context )
422                     window->Window.Context =
423                         wglCreateContext( window->Window.pContext.Device );
424             }
425
426 #if !defined(_WIN32_WCE)
427             fgNewWGLCreateContext( window );
428 #endif
429         }
430
431         window->State.NeedToResize = GL_TRUE;
432         /* if we used CW_USEDEFAULT (thats a negative value) for the size
433          * of the window, query the window now for the size at which it
434          * was created.
435          */
436         if( ( window->State.Width < 0 ) || ( window->State.Height < 0 ) )
437         {
438             SFG_Window *current_window = fgStructure.CurrentWindow;
439
440             fgSetWindow( window );
441             window->State.Width = glutGet( GLUT_WINDOW_WIDTH );
442             window->State.Height = glutGet( GLUT_WINDOW_HEIGHT );
443             fgSetWindow( current_window );
444         }
445
446         ReleaseDC( window->Window.Handle, window->Window.pContext.Device );
447
448 #if defined(_WIN32_WCE)
449         /* Take over button handling */
450         {
451             HINSTANCE dxDllLib=LoadLibrary(_T("gx.dll"));
452             if (dxDllLib)
453             {
454                 GXGetDefaultKeys_=(GXGETDEFAULTKEYS)GetProcAddress(dxDllLib, _T("?GXGetDefaultKeys@@YA?AUGXKeyList@@H@Z"));
455                 GXOpenInput_=(GXOPENINPUT)GetProcAddress(dxDllLib, _T("?GXOpenInput@@YAHXZ"));
456             }
457
458             if(GXOpenInput_)
459                 (*GXOpenInput_)();
460             if(GXGetDefaultKeys_)
461                 gxKeyList = (*GXGetDefaultKeys_)(GX_LANDSCAPEKEYS);
462         }
463
464 #endif /* defined(_WIN32_WCE) */
465         break;
466
467     case WM_SIZE:
468         /*
469          * If the window is visible, then it is the user manually resizing it.
470          * If it is not, then it is the system sending us a dummy resize with
471          * zero dimensions on a "glutIconifyWindow" call.
472          */
473         if( window->State.Visible )
474         {
475             window->State.NeedToResize = GL_TRUE;
476 #if defined(_WIN32_WCE)
477             window->State.Width  = HIWORD(lParam);
478             window->State.Height = LOWORD(lParam);
479 #else
480             window->State.Width  = LOWORD(lParam);
481             window->State.Height = HIWORD(lParam);
482 #endif /* defined(_WIN32_WCE) */
483         }
484
485         break;
486
487     case WM_SETFOCUS:
488 /*        printf("WM_SETFOCUS: %p\n", window ); */
489         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
490         INVOKE_WCB( *window, Entry, ( GLUT_ENTERED ) );
491
492                 UpdateWindow ( hWnd );
493         break;
494
495     case WM_KILLFOCUS:
496 /*        printf("WM_KILLFOCUS: %p\n", window ); */
497         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
498         INVOKE_WCB( *window, Entry, ( GLUT_LEFT ) );
499
500         if( window->IsMenu &&
501             window->ActiveMenu && window->ActiveMenu->IsActive )
502             fgUpdateMenuHighlight( window->ActiveMenu );
503
504         break;
505
506 #if 0
507     case WM_ACTIVATE:
508         if (LOWORD(wParam) != WA_INACTIVE)
509         {
510 /*            printf("WM_ACTIVATE: fgSetCursor( %p, %d)\n", window,
511                    window->State.Cursor ); */
512             fgSetCursor( window, window->State.Cursor );
513         }
514
515         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
516         break;
517 #endif
518
519     case WM_SETCURSOR:
520 /*      printf ( "Cursor event %x %x %x %x\n", window, window->State.Cursor, lParam, wParam ) ; */
521         if( LOWORD( lParam ) == HTCLIENT )
522             fgSetCursor ( window, window->State.Cursor ) ;
523         else
524             lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
525         break;
526
527     case WM_SHOWWINDOW:
528         window->State.Visible = GL_TRUE;
529         window->State.Redisplay = GL_TRUE;
530         break;
531
532     case WM_PAINT:
533         /* Turn on the visibility in case it was turned off somehow */
534         window->State.Visible = GL_TRUE;
535         BeginPaint( hWnd, &ps );
536         fghRedrawWindow( window );
537         EndPaint( hWnd, &ps );
538         break;
539
540     case WM_CLOSE:
541         fgDestroyWindow ( window );
542         if ( fgState.ActionOnWindowClose != GLUT_ACTION_CONTINUE_EXECUTION )
543             PostQuitMessage(0);
544         break;
545
546     case WM_DESTROY:
547         /*
548          * The window already got destroyed, so don't bother with it.
549          */
550         return 0;
551
552     case WM_MOUSEMOVE:
553     {
554 #if defined(_WIN32_WCE)
555         window->State.MouseX = 320-HIWORD( lParam );
556         window->State.MouseY = LOWORD( lParam );
557 #else
558         window->State.MouseX = LOWORD( lParam );
559         window->State.MouseY = HIWORD( lParam );
560 #endif /* defined(_WIN32_WCE) */
561         /* Restrict to [-32768, 32767] to match X11 behaviour       */
562         /* See comment in "freeglut_developer" mailing list 10/4/04 */
563         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
564         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
565
566         if ( window->ActiveMenu )
567         {
568             fgUpdateMenuHighlight( window->ActiveMenu );
569             break;
570         }
571
572         fgState.Modifiers = fgPlatformGetModifiers( );
573
574         if( ( wParam & MK_LBUTTON ) ||
575             ( wParam & MK_MBUTTON ) ||
576             ( wParam & MK_RBUTTON ) )
577             INVOKE_WCB( *window, Motion, ( window->State.MouseX,
578                                            window->State.MouseY ) );
579         else
580             INVOKE_WCB( *window, Passive, ( window->State.MouseX,
581                                             window->State.MouseY ) );
582
583         fgState.Modifiers = INVALID_MODIFIERS;
584     }
585     break;
586
587     case WM_LBUTTONDOWN:
588     case WM_MBUTTONDOWN:
589     case WM_RBUTTONDOWN:
590     case WM_LBUTTONUP:
591     case WM_MBUTTONUP:
592     case WM_RBUTTONUP:
593     {
594         GLboolean pressed = GL_TRUE;
595         int button;
596
597 #if defined(_WIN32_WCE)
598         window->State.MouseX = 320-HIWORD( lParam );
599         window->State.MouseY = LOWORD( lParam );
600 #else
601         window->State.MouseX = LOWORD( lParam );
602         window->State.MouseY = HIWORD( lParam );
603 #endif /* defined(_WIN32_WCE) */
604
605         /* Restrict to [-32768, 32767] to match X11 behaviour       */
606         /* See comment in "freeglut_developer" mailing list 10/4/04 */
607         if ( window->State.MouseX > 32767 ) window->State.MouseX -= 65536;
608         if ( window->State.MouseY > 32767 ) window->State.MouseY -= 65536;
609
610         switch( uMsg )
611         {
612         case WM_LBUTTONDOWN:
613             pressed = GL_TRUE;
614             button = GLUT_LEFT_BUTTON;
615             break;
616         case WM_MBUTTONDOWN:
617             pressed = GL_TRUE;
618             button = GLUT_MIDDLE_BUTTON;
619             break;
620         case WM_RBUTTONDOWN:
621             pressed = GL_TRUE;
622             button = GLUT_RIGHT_BUTTON;
623             break;
624         case WM_LBUTTONUP:
625             pressed = GL_FALSE;
626             button = GLUT_LEFT_BUTTON;
627             break;
628         case WM_MBUTTONUP:
629             pressed = GL_FALSE;
630             button = GLUT_MIDDLE_BUTTON;
631             break;
632         case WM_RBUTTONUP:
633             pressed = GL_FALSE;
634             button = GLUT_RIGHT_BUTTON;
635             break;
636         default:
637             pressed = GL_FALSE;
638             button = -1;
639             break;
640         }
641
642 #if !defined(_WIN32_WCE)
643         if( GetSystemMetrics( SM_SWAPBUTTON ) )
644         {
645             if( button == GLUT_LEFT_BUTTON )
646                 button = GLUT_RIGHT_BUTTON;
647             else
648                 if( button == GLUT_RIGHT_BUTTON )
649                     button = GLUT_LEFT_BUTTON;
650         }
651 #endif /* !defined(_WIN32_WCE) */
652
653         if( button == -1 )
654             return DefWindowProc( hWnd, uMsg, lParam, wParam );
655
656         /*
657          * Do not execute the application's mouse callback if a menu
658          * is hooked to this button.  In that case an appropriate
659          * private call should be generated.
660          */
661         if( fgCheckActiveMenu( window, button, pressed,
662                                window->State.MouseX, window->State.MouseY ) )
663             break;
664
665         /* Set capture so that the window captures all the mouse messages */
666         /*
667          * XXX - Multiple button support:  Under X11, the mouse is not released
668          * XXX - from the window until all buttons have been released, even if the
669          * XXX - user presses a button in another window.  This will take more
670          * XXX - code changes than I am up to at the moment (10/5/04).  The present
671          * XXX - is a 90 percent solution.
672          */
673         if ( pressed == GL_TRUE )
674           SetCapture ( window->Window.Handle ) ;
675         else
676           ReleaseCapture () ;
677
678         if( ! FETCH_WCB( *window, Mouse ) )
679             break;
680
681         fgSetWindow( window );
682         fgState.Modifiers = fgPlatformGetModifiers( );
683
684         INVOKE_WCB(
685             *window, Mouse,
686             ( button,
687               pressed ? GLUT_DOWN : GLUT_UP,
688               window->State.MouseX,
689               window->State.MouseY
690             )
691         );
692
693         fgState.Modifiers = INVALID_MODIFIERS;
694     }
695     break;
696
697     case WM_MOUSEWHEEL:
698     {
699         int wheel_number = LOWORD( wParam );
700         short ticks = ( short )HIWORD( wParam );
701                 fgState.MouseWheelTicks += ticks;
702
703         /*
704          * XXX Should use WHEEL_DELTA instead of 120
705          */
706                 if ( abs ( fgState.MouseWheelTicks ) >= 120 )
707                 {
708                         int direction = ( fgState.MouseWheelTicks > 0 ) ? 1 : -1;
709
710             if( ! FETCH_WCB( *window, MouseWheel ) &&
711                 ! FETCH_WCB( *window, Mouse ) )
712                 break;
713
714             fgSetWindow( window );
715             fgState.Modifiers = fgPlatformGetModifiers( );
716
717             /*
718              * XXX Should use WHEEL_DELTA instead of 120
719              */
720             while( abs ( fgState.MouseWheelTicks ) >= 120 )
721                         {
722                 if( FETCH_WCB( *window, MouseWheel ) )
723                     INVOKE_WCB( *window, MouseWheel,
724                                 ( wheel_number,
725                                   direction,
726                                   window->State.MouseX,
727                                   window->State.MouseY
728                                 )
729                     );
730                 else  /* No mouse wheel, call the mouse button callback twice */
731                                 {
732                     /*
733                      * Map wheel zero to button 3 and 4; +1 to 3, -1 to 4
734                      *  "    "   one                     +1 to 5, -1 to 6, ...
735                      *
736                      * XXX The below assumes that you have no more than 3 mouse
737                      * XXX buttons.  Sorry.
738                      */
739                     int button = wheel_number * 2 + 3;
740                     if( direction < 0 )
741                         ++button;
742                     INVOKE_WCB( *window, Mouse,
743                                 ( button, GLUT_DOWN,
744                                   window->State.MouseX, window->State.MouseY )
745                     );
746                     INVOKE_WCB( *window, Mouse,
747                                 ( button, GLUT_UP,
748                                   window->State.MouseX, window->State.MouseY )
749                     );
750                                 }
751
752                 /*
753                  * XXX Should use WHEEL_DELTA instead of 120
754                  */
755                                 fgState.MouseWheelTicks -= 120 * direction;
756                         }
757
758             fgState.Modifiers = INVALID_MODIFIERS;
759                 }
760     }
761     break ;
762
763     case WM_SYSKEYDOWN:
764     case WM_KEYDOWN:
765     {
766         int keypress = -1;
767         POINT mouse_pos ;
768
769
770         if( ( fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE ) && (HIWORD(lParam) & KF_REPEAT) )
771             break;
772
773         /*
774          * Remember the current modifiers state. This is done here in order
775          * to make sure the VK_DELETE keyboard callback is executed properly.
776          */
777         fgState.Modifiers = fgPlatformGetModifiers( );
778
779         GetCursorPos( &mouse_pos );
780         ScreenToClient( window->Window.Handle, &mouse_pos );
781
782         window->State.MouseX = mouse_pos.x;
783         window->State.MouseY = mouse_pos.y;
784
785         /* Convert the Win32 keystroke codes to GLUTtish way */
786 #       define KEY(a,b) case a: keypress = b; break;
787
788         switch( wParam )
789         {
790             KEY( VK_F1,     GLUT_KEY_F1        );
791             KEY( VK_F2,     GLUT_KEY_F2        );
792             KEY( VK_F3,     GLUT_KEY_F3        );
793             KEY( VK_F4,     GLUT_KEY_F4        );
794             KEY( VK_F5,     GLUT_KEY_F5        );
795             KEY( VK_F6,     GLUT_KEY_F6        );
796             KEY( VK_F7,     GLUT_KEY_F7        );
797             KEY( VK_F8,     GLUT_KEY_F8        );
798             KEY( VK_F9,     GLUT_KEY_F9        );
799             KEY( VK_F10,    GLUT_KEY_F10       );
800             KEY( VK_F11,    GLUT_KEY_F11       );
801             KEY( VK_F12,    GLUT_KEY_F12       );
802             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
803             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
804             KEY( VK_HOME,   GLUT_KEY_HOME      );
805             KEY( VK_END,    GLUT_KEY_END       );
806             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
807             KEY( VK_UP,     GLUT_KEY_UP        );
808             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
809             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
810             KEY( VK_INSERT, GLUT_KEY_INSERT    );
811
812         case VK_LCONTROL:  case VK_RCONTROL:  case VK_CONTROL:
813         case VK_LSHIFT:    case VK_RSHIFT:    case VK_SHIFT:
814         case VK_LMENU:     case VK_RMENU:     case VK_MENU:
815             /* These keypresses and releases are handled earlier in the function */
816             break;
817
818         case VK_DELETE:
819             /* The delete key should be treated as an ASCII keypress: */
820             INVOKE_WCB( *window, Keyboard,
821                         ( 127, window->State.MouseX, window->State.MouseY )
822             );
823         }
824
825 #if defined(_WIN32_WCE)
826         if(!(lParam & 0x40000000)) /* Prevent auto-repeat */
827         {
828             if(wParam==(unsigned)gxKeyList.vkRight)
829                 keypress = GLUT_KEY_RIGHT;
830             else if(wParam==(unsigned)gxKeyList.vkLeft)
831                 keypress = GLUT_KEY_LEFT;
832             else if(wParam==(unsigned)gxKeyList.vkUp)
833                 keypress = GLUT_KEY_UP;
834             else if(wParam==(unsigned)gxKeyList.vkDown)
835                 keypress = GLUT_KEY_DOWN;
836             else if(wParam==(unsigned)gxKeyList.vkA)
837                 keypress = GLUT_KEY_F1;
838             else if(wParam==(unsigned)gxKeyList.vkB)
839                 keypress = GLUT_KEY_F2;
840             else if(wParam==(unsigned)gxKeyList.vkC)
841                 keypress = GLUT_KEY_F3;
842             else if(wParam==(unsigned)gxKeyList.vkStart)
843                 keypress = GLUT_KEY_F4;
844         }
845 #endif
846
847         if( keypress != -1 )
848             INVOKE_WCB( *window, Special,
849                         ( keypress,
850                           window->State.MouseX, window->State.MouseY )
851             );
852
853         fgState.Modifiers = INVALID_MODIFIERS;
854     }
855     break;
856
857     case WM_SYSKEYUP:
858     case WM_KEYUP:
859     {
860         int keypress = -1;
861         POINT mouse_pos;
862
863         /*
864          * Remember the current modifiers state. This is done here in order
865          * to make sure the VK_DELETE keyboard callback is executed properly.
866          */
867         fgState.Modifiers = fgPlatformGetModifiers( );
868
869         GetCursorPos( &mouse_pos );
870         ScreenToClient( window->Window.Handle, &mouse_pos );
871
872         window->State.MouseX = mouse_pos.x;
873         window->State.MouseY = mouse_pos.y;
874
875         /*
876          * Convert the Win32 keystroke codes to GLUTtish way.
877          * "KEY(a,b)" was defined under "WM_KEYDOWN"
878          */
879
880         switch( wParam )
881         {
882             KEY( VK_F1,     GLUT_KEY_F1        );
883             KEY( VK_F2,     GLUT_KEY_F2        );
884             KEY( VK_F3,     GLUT_KEY_F3        );
885             KEY( VK_F4,     GLUT_KEY_F4        );
886             KEY( VK_F5,     GLUT_KEY_F5        );
887             KEY( VK_F6,     GLUT_KEY_F6        );
888             KEY( VK_F7,     GLUT_KEY_F7        );
889             KEY( VK_F8,     GLUT_KEY_F8        );
890             KEY( VK_F9,     GLUT_KEY_F9        );
891             KEY( VK_F10,    GLUT_KEY_F10       );
892             KEY( VK_F11,    GLUT_KEY_F11       );
893             KEY( VK_F12,    GLUT_KEY_F12       );
894             KEY( VK_PRIOR,  GLUT_KEY_PAGE_UP   );
895             KEY( VK_NEXT,   GLUT_KEY_PAGE_DOWN );
896             KEY( VK_HOME,   GLUT_KEY_HOME      );
897             KEY( VK_END,    GLUT_KEY_END       );
898             KEY( VK_LEFT,   GLUT_KEY_LEFT      );
899             KEY( VK_UP,     GLUT_KEY_UP        );
900             KEY( VK_RIGHT,  GLUT_KEY_RIGHT     );
901             KEY( VK_DOWN,   GLUT_KEY_DOWN      );
902             KEY( VK_INSERT, GLUT_KEY_INSERT    );
903
904           case VK_LCONTROL:  case VK_RCONTROL:  case VK_CONTROL:
905           case VK_LSHIFT:    case VK_RSHIFT:    case VK_SHIFT:
906           case VK_LMENU:     case VK_RMENU:     case VK_MENU:
907               /* These keypresses and releases are handled earlier in the function */
908               break;
909
910           case VK_DELETE:
911               /* The delete key should be treated as an ASCII keypress: */
912               INVOKE_WCB( *window, KeyboardUp,
913                           ( 127, window->State.MouseX, window->State.MouseY )
914               );
915               break;
916
917         default:
918         {
919 #if !defined(_WIN32_WCE)
920             BYTE state[ 256 ];
921             WORD code[ 2 ];
922
923             GetKeyboardState( state );
924
925             if( ToAscii( (UINT)wParam, 0, state, code, 0 ) == 1 )
926                 wParam=code[ 0 ];
927
928             INVOKE_WCB( *window, KeyboardUp,
929                         ( (char)wParam,
930                           window->State.MouseX, window->State.MouseY )
931             );
932 #endif /* !defined(_WIN32_WCE) */
933         }
934         }
935
936         if( keypress != -1 )
937             INVOKE_WCB( *window, SpecialUp,
938                         ( keypress,
939                           window->State.MouseX, window->State.MouseY )
940             );
941
942         fgState.Modifiers = INVALID_MODIFIERS;
943     }
944     break;
945
946     case WM_SYSCHAR:
947     case WM_CHAR:
948     {
949       if( (fgState.KeyRepeat==GLUT_KEY_REPEAT_OFF || window->State.IgnoreKeyRepeat==GL_TRUE) && (HIWORD(lParam) & KF_REPEAT) )
950             break;
951
952         fgState.Modifiers = fgPlatformGetModifiers( );
953         INVOKE_WCB( *window, Keyboard,
954                     ( (char)wParam,
955                       window->State.MouseX, window->State.MouseY )
956         );
957         fgState.Modifiers = INVALID_MODIFIERS;
958     }
959     break;
960
961     case WM_CAPTURECHANGED:
962         /* User has finished resizing the window, force a redraw */
963         INVOKE_WCB( *window, Display, ( ) );
964
965         /*lRet = DefWindowProc( hWnd, uMsg, wParam, lParam ); */
966         break;
967
968         /* Other messages that I have seen and which are not handled already */
969     case WM_SETTEXT:  /* 0x000c */
970         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
971         /* Pass it on to "DefWindowProc" to set the window text */
972         break;
973
974     case WM_GETTEXT:  /* 0x000d */
975         /* Ideally we would copy the title of the window into "lParam" */
976         /* strncpy ( (char *)lParam, "Window Title", wParam );
977            lRet = ( wParam > 12 ) ? 12 : wParam;  */
978         /* the number of characters copied */
979         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
980         break;
981
982     case WM_GETTEXTLENGTH:  /* 0x000e */
983         /* Ideally we would get the length of the title of the window */
984         lRet = 12;
985         /* the number of characters in "Window Title\0" (see above) */
986         break;
987
988     case WM_ERASEBKGND:  /* 0x0014 */
989         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
990         break;
991
992 #if !defined(_WIN32_WCE)
993     case WM_SYNCPAINT:  /* 0x0088 */
994         /* Another window has moved, need to update this one */
995         window->State.Redisplay = GL_TRUE;
996         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
997         /* Help screen says this message must be passed to "DefWindowProc" */
998         break;
999
1000     case WM_NCPAINT:  /* 0x0085 */
1001       /* Need to update the border of this window */
1002         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1003         /* Pass it on to "DefWindowProc" to repaint a standard border */
1004         break;
1005
1006     case WM_SYSCOMMAND :  /* 0x0112 */
1007         {
1008           /*
1009            * We have received a system command message.  Try to act on it.
1010            * The commands are passed in through the "wParam" parameter:
1011            * The least significant digit seems to be which edge of the window
1012            * is being used for a resize event:
1013            *     4  3  5
1014            *     1     2
1015            *     7  6  8
1016            * Congratulations and thanks to Richard Rauch for figuring this out..
1017            */
1018             switch ( wParam & 0xfff0 )
1019             {
1020             case SC_SIZE       :
1021                 break ;
1022
1023             case SC_MOVE       :
1024                 break ;
1025
1026             case SC_MINIMIZE   :
1027                 /* User has clicked on the "-" to minimize the window */
1028                 /* Turn off the visibility */
1029                 window->State.Visible = GL_FALSE ;
1030
1031                 break ;
1032
1033             case SC_MAXIMIZE   :
1034                 break ;
1035
1036             case SC_NEXTWINDOW :
1037                 break ;
1038
1039             case SC_PREVWINDOW :
1040                 break ;
1041
1042             case SC_CLOSE      :
1043                 /* Followed very closely by a WM_CLOSE message */
1044                 break ;
1045
1046             case SC_VSCROLL    :
1047                 break ;
1048
1049             case SC_HSCROLL    :
1050                 break ;
1051
1052             case SC_MOUSEMENU  :
1053                 break ;
1054
1055             case SC_KEYMENU    :
1056                 break ;
1057
1058             case SC_ARRANGE    :
1059                 break ;
1060
1061             case SC_RESTORE    :
1062                 break ;
1063
1064             case SC_TASKLIST   :
1065                 break ;
1066
1067             case SC_SCREENSAVE :
1068                 break ;
1069
1070             case SC_HOTKEY     :
1071                 break ;
1072
1073 #if(WINVER >= 0x0400)
1074             case SC_DEFAULT    :
1075                 break ;
1076
1077             case SC_MONITORPOWER    :
1078                 break ;
1079
1080             case SC_CONTEXTHELP    :
1081                 break ;
1082 #endif /* WINVER >= 0x0400 */
1083
1084             default:
1085 #if _DEBUG
1086                 fgWarning( "Unknown wParam type 0x%x", wParam );
1087 #endif
1088                 break;
1089             }
1090         }
1091 #endif /* !defined(_WIN32_WCE) */
1092
1093         /* We need to pass the message on to the operating system as well */
1094         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1095         break;
1096
1097 #ifdef WM_TOUCH
1098         /* handle multi-touch messages */
1099         case WM_TOUCH:
1100         {
1101                 unsigned int numInputs = (unsigned int)wParam;
1102                 unsigned int i = 0;
1103                 TOUCHINPUT* ti = (TOUCHINPUT*)malloc( sizeof(TOUCHINPUT)*numInputs);
1104
1105                 if (fghGetTouchInputInfo == (pGetTouchInputInfo)0xDEADBEEF) {
1106                     fghGetTouchInputInfo = (pGetTouchInputInfo)GetProcAddress(GetModuleHandle("user32"),"GetTouchInputInfo");
1107                     fghCloseTouchInputHandle = (pCloseTouchInputHandle)GetProcAddress(GetModuleHandle("user32"),"CloseTouchInputHandle");
1108                 }
1109
1110                 if (!fghGetTouchInputInfo) { 
1111                         free( (void*)ti );
1112                         break;
1113                 }
1114
1115                 if (fghGetTouchInputInfo( (HTOUCHINPUT)lParam, numInputs, ti, sizeof(TOUCHINPUT) )) {
1116                         /* Handle each contact point */
1117                         for (i = 0; i < numInputs; ++i ) {
1118
1119                                 POINT tp;
1120                                 tp.x = TOUCH_COORD_TO_PIXEL(ti[i].x);
1121                                 tp.y = TOUCH_COORD_TO_PIXEL(ti[i].y);
1122                                 ScreenToClient( hWnd, &tp );
1123
1124                                 ti[i].dwID = ti[i].dwID * 2;
1125
1126                                 if (ti[i].dwFlags & TOUCHEVENTF_DOWN) {
1127                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_ENTERED ) );
1128                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_DOWN ) );
1129                                 } else if (ti[i].dwFlags & TOUCHEVENTF_MOVE) {
1130                                         INVOKE_WCB( *window, MultiMotion, ( ti[i].dwID, tp.x, tp.y ) );
1131                                 } else if (ti[i].dwFlags & TOUCHEVENTF_UP)   { 
1132                                         INVOKE_WCB( *window, MultiButton, ( ti[i].dwID, tp.x, tp.y, 0, GLUT_UP ) );
1133                                         INVOKE_WCB( *window, MultiEntry,  ( ti[i].dwID, GLUT_LEFT ) );
1134                                 }
1135                         }
1136                 }
1137                 fghCloseTouchInputHandle((HTOUCHINPUT)lParam);
1138                 free( (void*)ti );
1139                 lRet = 0; /*DefWindowProc( hWnd, uMsg, wParam, lParam );*/
1140                 break;
1141         }
1142 #endif
1143     default:
1144         /* Handle unhandled messages */
1145         lRet = DefWindowProc( hWnd, uMsg, wParam, lParam );
1146         break;
1147     }
1148
1149     return lRet;
1150 }