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