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