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