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