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