Yes I did it again ;)
[blender.git] / intern / ghost / intern / GHOST_SystemWin32.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  */
31
32 /**
33
34  * $Id$
35  * Copyright (C) 2001 NaN Technologies B.V.
36  * @author      Maarten Gribnau
37  * @date        May 7, 2001
38  */
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #include "GHOST_SystemWin32.h"
45
46
47 #include "GHOST_Debug.h"
48 #include "GHOST_DisplayManagerWin32.h"
49 #include "GHOST_EventButton.h"
50 #include "GHOST_EventCursor.h"
51 #include "GHOST_EventKey.h"
52 #include "GHOST_TimerTask.h"
53 #include "GHOST_TimerManager.h"
54 #include "GHOST_WindowManager.h"
55 #include "GHOST_WindowWin32.h"
56
57 // Key code values not found in winuser.h
58 #ifndef VK_MINUS
59 #define VK_MINUS 0xBD
60 #endif // VK_MINUS
61 #ifndef VK_SEMICOLON
62 #define VK_SEMICOLON 0xBA
63 #endif // VK_SEMICOLON
64 #ifndef VK_PERIOD
65 #define VK_PERIOD 0xBE
66 #endif // VK_PERIOD
67 #ifndef VK_COMMA
68 #define VK_COMMA 0xBC
69 #endif // VK_COMMA
70 #ifndef VK_QUOTE
71 #define VK_QUOTE 0xDE
72 #endif // VK_QUOTE
73 #ifndef VK_BACK_QUOTE
74 #define VK_BACK_QUOTE 0xC0
75 #endif // VK_BACK_QUOTE
76 #ifndef VK_SLASH
77 #define VK_SLASH 0xBF
78 #endif // VK_SLASH
79 #ifndef VK_BACK_SLASH
80 #define VK_BACK_SLASH 0xDC
81 #endif // VK_BACK_SLASH
82 #ifndef VK_EQUALS
83 #define VK_EQUALS 0xBB
84 #endif // VK_EQUALS
85 #ifndef VK_OPEN_BRACKET
86 #define VK_OPEN_BRACKET 0xDB
87 #endif // VK_OPEN_BRACKET
88 #ifndef VK_CLOSE_BRACKET
89 #define VK_CLOSE_BRACKET 0xDD
90 #endif // VK_CLOSE_BRACKET
91
92
93 GHOST_SystemWin32::GHOST_SystemWin32()
94 : m_hasPerformanceCounter(false), m_freq(0), m_start(0),
95   m_seperateLeftRight(false),
96   m_seperateLeftRightInitialized(false)
97 {
98         m_displayManager = new GHOST_DisplayManagerWin32 ();
99         GHOST_ASSERT(m_displayManager, "GHOST_SystemWin32::GHOST_SystemWin32(): m_displayManager==0\n");
100         m_displayManager->initialize();
101 }
102
103 GHOST_SystemWin32::~GHOST_SystemWin32()
104 {
105 }
106
107
108 GHOST_TUns64 GHOST_SystemWin32::getMilliSeconds() const
109 {
110         // Hardware does not support high resolution timers. We will use GetTickCount instead then.
111         if (!m_hasPerformanceCounter) {
112                 return ::GetTickCount();
113         }
114
115         // Retrieve current count
116         __int64 count = 0;
117         ::QueryPerformanceCounter((LARGE_INTEGER*)&count);
118
119         // Calculate the time passed since system initialization.
120         __int64 delta = 1000*(count-m_start);
121
122         GHOST_TUns64 t = (GHOST_TUns64)(delta/m_freq);
123         return t; 
124 }
125
126
127 GHOST_TUns8 GHOST_SystemWin32::getNumDisplays() const
128 {
129         GHOST_ASSERT(m_displayManager, "GHOST_SystemWin32::getNumDisplays(): m_displayManager==0\n");
130         GHOST_TUns8 numDisplays;
131         m_displayManager->getNumDisplays(numDisplays);
132         return numDisplays;
133 }
134
135
136 void GHOST_SystemWin32::getMainDisplayDimensions(GHOST_TUns32& width, GHOST_TUns32& height) const
137 {
138         width = ::GetSystemMetrics(SM_CXSCREEN);
139         height= ::GetSystemMetrics(SM_CYSCREEN);
140 }
141
142
143 GHOST_IWindow* GHOST_SystemWin32::createWindow(
144         const STR_String& title, 
145         GHOST_TInt32 left, GHOST_TInt32 top, GHOST_TUns32 width, GHOST_TUns32 height,
146         GHOST_TWindowState state, GHOST_TDrawingContextType type,
147         bool stereoVisual)
148 {
149         GHOST_Window* window = 0;
150         window = new GHOST_WindowWin32 (title, left, top, width, height, state, type, stereoVisual);
151         if (window) {
152                 if (window->getValid()) {
153                         // Store the pointer to the window
154                         if (state != GHOST_kWindowStateFullScreen) {
155                                 m_windowManager->addWindow(window);
156                         }
157                 }
158                 else {
159                         delete window;
160                         window = 0;
161                 }
162         }
163         return window;
164 }
165
166
167 bool GHOST_SystemWin32::processEvents(bool waitForEvent)
168 {
169         MSG msg;
170         bool anyProcessed = false;
171
172         do {
173                 GHOST_TimerManager* timerMgr = getTimerManager();
174
175                 if (waitForEvent && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
176 #if 1
177                         ::Sleep(1);
178 #else
179                         GHOST_TUns64 next = timerMgr->nextFireTime();
180                         
181                         if (next == GHOST_kFireTimeNever) {
182                                 ::WaitMessage();
183                         } else {
184                                 ::SetTimer(NULL, 0, next - getMilliSeconds(), NULL);
185                                 ::WaitMessage();
186                                 ::KillTimer(NULL, 0);
187                         }
188 #endif
189                 }
190
191                 if (timerMgr->fireTimers(getMilliSeconds())) {
192                         anyProcessed = true;
193                 }
194
195                 // Process all the events waiting for us
196                 while (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE) != 0) {
197                         ::TranslateMessage(&msg);
198                         ::DispatchMessage(&msg);
199                         anyProcessed = true;
200                 }
201         } while (waitForEvent && !anyProcessed);
202
203         return anyProcessed;
204 }
205
206
207 GHOST_TSuccess GHOST_SystemWin32::getCursorPosition(GHOST_TInt32& x, GHOST_TInt32& y) const
208 {
209         POINT point;
210         bool success = ::GetCursorPos(&point) == TRUE;
211         x = point.x;
212         y = point.y;
213         return GHOST_kSuccess;
214 }
215
216
217 GHOST_TSuccess GHOST_SystemWin32::setCursorPosition(GHOST_TInt32 x, GHOST_TInt32 y) const
218 {
219         return ::SetCursorPos(x, y) == TRUE ? GHOST_kSuccess : GHOST_kFailure;
220 }
221
222
223 GHOST_TSuccess GHOST_SystemWin32::getModifierKeys(GHOST_ModifierKeys& keys) const
224 {
225         /*
226         GetKeyState and GetAsyncKeyState only work with Win95, Win98, NT4,
227         Terminal Server and Windows 2000.
228         But on WinME it always returns zero. These two functions are simply
229         skipped by Millenium Edition!
230
231         Official explanation from Microsoft:
232         Intentionally disabled.
233         It didn't work all that well on some newer hardware, and worked less 
234         well with the passage of time, so it was fully disabled in ME.
235         */
236         if (m_seperateLeftRight && m_seperateLeftRightInitialized) {
237                 bool down = HIBYTE(::GetKeyState(VK_LSHIFT)) != 0;
238                 keys.set(GHOST_kModifierKeyLeftShift, down);
239                 down = HIBYTE(::GetKeyState(VK_RSHIFT)) != 0;
240                 keys.set(GHOST_kModifierKeyRightShift, down);
241                 down = HIBYTE(::GetKeyState(VK_LMENU)) != 0;
242                 keys.set(GHOST_kModifierKeyLeftAlt, down);
243                 down = HIBYTE(::GetKeyState(VK_RMENU)) != 0;
244                 keys.set(GHOST_kModifierKeyRightAlt, down);
245                 down = HIBYTE(::GetKeyState(VK_LCONTROL)) != 0;
246                 keys.set(GHOST_kModifierKeyLeftControl, down);
247                 down = HIBYTE(::GetKeyState(VK_RCONTROL)) != 0;
248                 keys.set(GHOST_kModifierKeyRightControl, down);
249         }
250         else {
251                 bool down = HIBYTE(::GetKeyState(VK_SHIFT)) != 0;
252                 keys.set(GHOST_kModifierKeyLeftShift, down);
253                 keys.set(GHOST_kModifierKeyRightShift, down);
254                 down = HIBYTE(::GetKeyState(VK_MENU)) != 0;
255                 keys.set(GHOST_kModifierKeyLeftAlt, down);
256                 keys.set(GHOST_kModifierKeyRightAlt, down);
257                 down = HIBYTE(::GetKeyState(VK_CONTROL)) != 0;
258                 keys.set(GHOST_kModifierKeyLeftControl, down);
259                 keys.set(GHOST_kModifierKeyRightControl, down);
260         }
261         return GHOST_kSuccess;
262 }
263
264
265 GHOST_TSuccess GHOST_SystemWin32::getButtons(GHOST_Buttons& buttons) const
266 {
267         /* Check for swapped buttons (left-handed mouse buttons)
268          * GetAsyncKeyState() will give back the state of the physical mouse buttons.
269          */
270         bool swapped = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE;
271
272         bool down = HIBYTE(::GetKeyState(VK_LBUTTON)) != 0;
273         buttons.set(swapped ? GHOST_kButtonMaskRight : GHOST_kButtonMaskLeft, down);
274
275         down = HIBYTE(::GetKeyState(VK_MBUTTON)) != 0;
276         buttons.set(GHOST_kButtonMaskMiddle, down);
277
278         down = HIBYTE(::GetKeyState(VK_RBUTTON)) != 0;
279         buttons.set(swapped ? GHOST_kButtonMaskLeft : GHOST_kButtonMaskRight, down);
280         return GHOST_kSuccess;
281 }
282
283
284 GHOST_TSuccess GHOST_SystemWin32::init()
285 {
286         GHOST_TSuccess success = GHOST_System::init();
287
288         // Determine whether this system has a high frequency performance counter. */
289         m_hasPerformanceCounter = ::QueryPerformanceFrequency((LARGE_INTEGER*)&m_freq) == TRUE;
290         if (m_hasPerformanceCounter) {
291                 GHOST_PRINT("GHOST_SystemWin32::init: High Frequency Performance Timer available\n")
292                 ::QueryPerformanceCounter((LARGE_INTEGER*)&m_start);
293         }
294         else {
295                 GHOST_PRINT("GHOST_SystemWin32::init: High Frequency Performance Timer not available\n")
296         }
297
298         if (success) {
299                 WNDCLASS wc;
300                 wc.style= CS_HREDRAW | CS_VREDRAW;
301                 wc.lpfnWndProc= s_wndProc;
302                 wc.cbClsExtra= 0;
303                 wc.cbWndExtra= 0;
304                 wc.hInstance= ::GetModuleHandle(0);
305                 wc.hIcon = ::LoadIcon(wc.hInstance, "APPICON");
306                 if (!wc.hIcon) {
307                         ::LoadIcon(NULL, IDI_APPLICATION);
308                 }
309                 wc.hCursor = ::LoadCursor(0, IDC_ARROW);
310                 wc.hbrBackground= (HBRUSH)::GetStockObject(BLACK_BRUSH);
311                 wc.lpszMenuName = 0;
312                 wc.lpszClassName= GHOST_WindowWin32::getWindowClassName();
313     
314                 // Use RegisterClassEx for setting small icon
315                 if (::RegisterClass(&wc) == 0) {
316                         success = GHOST_kFailure;
317                 }
318         }
319         return success;
320 }
321
322
323 GHOST_TSuccess GHOST_SystemWin32::exit()
324 {
325         return GHOST_System::exit();
326 }
327
328
329 GHOST_TKey GHOST_SystemWin32::convertKey(WPARAM wParam, LPARAM lParam) const
330 {
331         GHOST_TKey key;
332         bool isExtended = (lParam&(1<<24))?true:false;
333
334         if ((wParam >= '0') && (wParam <= '9')) {
335                 // VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)
336                 key = (GHOST_TKey)(wParam - '0' + GHOST_kKey0);
337         }
338         else if ((wParam >= 'A') && (wParam <= 'Z')) {
339                 // VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)
340                 key = (GHOST_TKey)(wParam - 'A' + GHOST_kKeyA);
341         }
342         else if ((wParam >= VK_F1) && (wParam <= VK_F24)) {
343                 key = (GHOST_TKey)(wParam - VK_F1 + GHOST_kKeyF1);
344         }
345         else {
346                 switch (wParam) {
347                 case VK_RETURN:
348                         key = isExtended?GHOST_kKeyNumpadEnter:GHOST_kKeyEnter;
349                         break;
350
351                 case VK_BACK:     key = GHOST_kKeyBackSpace;            break;
352                 case VK_TAB:      key = GHOST_kKeyTab;                          break;
353                 case VK_ESCAPE:   key = GHOST_kKeyEsc;                          break;
354                 case VK_SPACE:    key = GHOST_kKeySpace;                        break;
355                 case VK_PRIOR:    key = GHOST_kKeyUpPage;                       break;
356                 case VK_NEXT:     key = GHOST_kKeyDownPage;                     break;
357                 case VK_END:      key = GHOST_kKeyEnd;                          break;
358                 case VK_HOME:     key = GHOST_kKeyHome;                         break;
359                 case VK_INSERT:   key = GHOST_kKeyInsert;                       break;
360                 case VK_DELETE:   key = GHOST_kKeyDelete;                       break;
361                 case VK_LEFT:     key = GHOST_kKeyLeftArrow;            break;
362                 case VK_RIGHT:    key = GHOST_kKeyRightArrow;           break;
363                 case VK_UP:       key = GHOST_kKeyUpArrow;                      break;
364                 case VK_DOWN:     key = GHOST_kKeyDownArrow;            break;
365                 case VK_NUMPAD0:  key = GHOST_kKeyNumpad0;                      break;
366                 case VK_NUMPAD1:  key = GHOST_kKeyNumpad1;                      break;
367                 case VK_NUMPAD2:  key = GHOST_kKeyNumpad2;                      break;
368                 case VK_NUMPAD3:  key = GHOST_kKeyNumpad3;                      break;
369                 case VK_NUMPAD4:  key = GHOST_kKeyNumpad4;                      break;
370                 case VK_NUMPAD5:  key = GHOST_kKeyNumpad5;                      break;
371                 case VK_NUMPAD6:  key = GHOST_kKeyNumpad6;                      break;
372                 case VK_NUMPAD7:  key = GHOST_kKeyNumpad7;                      break;
373                 case VK_NUMPAD8:  key = GHOST_kKeyNumpad8;                      break;
374                 case VK_NUMPAD9:  key = GHOST_kKeyNumpad9;                      break;
375                 case VK_SNAPSHOT: key = GHOST_kKeyPrintScreen;          break;
376                 case VK_PAUSE:    key = GHOST_kKeyPause;                        break;
377                 case VK_MULTIPLY: key = GHOST_kKeyNumpadAsterisk;        break;
378                 case VK_SUBTRACT: key = GHOST_kKeyNumpadMinus;          break;
379                 case VK_DECIMAL:  key = GHOST_kKeyNumpadPeriod;         break;
380                 case VK_DIVIDE:   key = GHOST_kKeyNumpadSlash;          break;
381                 case VK_ADD:      key = GHOST_kKeyNumpadPlus;           break;
382
383                 case VK_SEMICOLON:              key = GHOST_kKeySemicolon;              break;
384                 case VK_EQUALS:                 key = GHOST_kKeyEqual;                  break;
385                 case VK_COMMA:                  key = GHOST_kKeyComma;                  break;
386                 case VK_MINUS:                  key = GHOST_kKeyMinus;                  break;
387                 case VK_PERIOD:                 key = GHOST_kKeyPeriod;                 break;
388                 case VK_SLASH:                  key = GHOST_kKeySlash;                  break;
389                 case VK_BACK_QUOTE:             key = GHOST_kKeyAccentGrave;    break;
390                 case VK_OPEN_BRACKET:   key = GHOST_kKeyLeftBracket;    break;
391                 case VK_BACK_SLASH:             key = GHOST_kKeyBackslash;              break;
392                 case VK_CLOSE_BRACKET:  key = GHOST_kKeyRightBracket;   break;
393                 case VK_QUOTE:                  key = GHOST_kKeyQuote;                  break;
394
395                 // Process these keys separately because we need to distinguish right from left modifier keys
396                 case VK_SHIFT:
397                 case VK_CONTROL:
398                 case VK_MENU:
399
400                 // Ignore these keys
401                 case VK_NUMLOCK:
402                 case VK_SCROLL:
403                 case VK_CAPITAL:
404                 default:
405                         key = GHOST_kKeyUnknown;
406                         break;
407                 }
408         }
409         return key;
410 }
411
412
413 void GHOST_SystemWin32::processModifierKeys(GHOST_IWindow *window)
414 {
415         GHOST_ModifierKeys oldModifiers, newModifiers;
416         // Retrieve old state of the modifier keys
417         ((GHOST_SystemWin32*)getSystem())->retrieveModifierKeys(oldModifiers);
418         // Retrieve current state of the modifier keys
419         ((GHOST_SystemWin32*)getSystem())->getModifierKeys(newModifiers);
420
421         // Compare the old and the new
422         if (!newModifiers.equals(oldModifiers)) {
423                 // Create events for the masks that changed
424                 for (int i = 0; i < GHOST_kModifierKeyNumMasks; i++) {
425                         if (newModifiers.get((GHOST_TModifierKeyMask)i) != oldModifiers.get((GHOST_TModifierKeyMask)i)) {
426                                 // Convert the mask to a key code
427                                 GHOST_TKey key = GHOST_ModifierKeys::getModifierKeyCode((GHOST_TModifierKeyMask)i);
428                                 bool keyDown = newModifiers.get((GHOST_TModifierKeyMask)i);
429                                 GHOST_EventKey* event;
430                                 if (key != GHOST_kKeyUnknown) {
431                                         // Create an event
432                                         event = new GHOST_EventKey(getSystem()->getMilliSeconds(), keyDown ? GHOST_kEventKeyDown: GHOST_kEventKeyUp, window, key);
433                                         pushEvent(event);
434                                 }
435                         }
436                 }
437         }
438
439         // Store new modifier keys state
440         ((GHOST_SystemWin32*)getSystem())->storeModifierKeys(newModifiers);
441 }
442
443
444 GHOST_EventButton* GHOST_SystemWin32::processButtonEvent(GHOST_TEventType type, GHOST_IWindow *window, GHOST_TButtonMask mask)
445 {
446         return new GHOST_EventButton (getSystem()->getMilliSeconds(), type, window, mask);
447 }
448
449
450 GHOST_EventCursor* GHOST_SystemWin32::processCursorEvent(GHOST_TEventType type, GHOST_IWindow *window)
451 {
452         GHOST_TInt32 x, y;
453         getSystem()->getCursorPosition(x, y);
454         return new GHOST_EventCursor (getSystem()->getMilliSeconds(), type, window, x, y);
455 }
456
457
458 GHOST_EventKey* GHOST_SystemWin32::processKeyEvent(GHOST_IWindow *window, bool keyDown, WPARAM wParam, LPARAM lParam)
459 {
460         GHOST_TKey key = ((GHOST_SystemWin32*)getSystem())->convertKey(wParam, lParam);
461         GHOST_EventKey* event;
462         if (key != GHOST_kKeyUnknown) {
463                 MSG keyMsg;
464                 char ascii = '\0';
465
466                         /* Eat any character related messages */
467                 if (::PeekMessage(&keyMsg, NULL, WM_CHAR, WM_SYSDEADCHAR, PM_REMOVE)) {
468                         ascii = (char) keyMsg.wParam;
469                 }
470
471                 event = new GHOST_EventKey(getSystem()->getMilliSeconds(), keyDown ? GHOST_kEventKeyDown: GHOST_kEventKeyUp, window, key, ascii);
472         }
473         else {
474                 event = 0;
475         }
476         return event;
477 }
478
479
480 GHOST_Event* GHOST_SystemWin32::processWindowEvent(GHOST_TEventType type, GHOST_IWindow* window)
481 {
482         return new GHOST_Event(getSystem()->getMilliSeconds(), type, window);
483 }
484
485
486 LRESULT WINAPI GHOST_SystemWin32::s_wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
487 {
488         GHOST_Event* event = 0;
489         LRESULT lResult;
490         GHOST_SystemWin32* system = ((GHOST_SystemWin32*)getSystem());
491         GHOST_ASSERT(system, "GHOST_SystemWin32::s_wndProc(): system not initialized")
492
493         if (hwnd) {
494                 GHOST_WindowWin32* window = (GHOST_WindowWin32*)::GetWindowLong(hwnd, GWL_USERDATA);
495                 if (window) {
496                         switch (msg) {
497                                 ////////////////////////////////////////////////////////////////////////
498                                 // Keyboard events, processed
499                                 ////////////////////////////////////////////////////////////////////////
500                                 case WM_KEYDOWN:
501                                         /* The WM_KEYDOWN message is posted to the window with the keyboard focus when a 
502                                          * nonsystem key is pressed. A nonsystem key is a key that is pressed when the alt
503                                          * key is not pressed. 
504                                          */
505                                 case WM_SYSKEYDOWN:
506                                         /* The WM_SYSKEYDOWN message is posted to the window with the keyboard focus when 
507                                          * the user presses the F10 key (which activates the menu bar) or holds down the 
508                                          * alt key and then presses another key. It also occurs when no window currently 
509                                          * has the keyboard focus; in this case, the WM_SYSKEYDOWN message is sent to the 
510                                          * active window. The window that receives the message can distinguish between these 
511                                          * two contexts by checking the context code in the lKeyData parameter. 
512                                          */
513                                         switch (wParam) {
514                                                 case VK_SHIFT:
515                                                 case VK_CONTROL:
516                                                 case VK_MENU:
517                                                         if (!system->m_seperateLeftRightInitialized) {
518                                                                 // Check whether this system supports seperate left and right keys
519                                                                 switch (wParam) {
520                                                                         case VK_SHIFT:
521                                                                                 system->m_seperateLeftRight = 
522                                                                                         (HIBYTE(::GetKeyState(VK_LSHIFT)) != 0) ||
523                                                                                         (HIBYTE(::GetKeyState(VK_RSHIFT)) != 0) ?
524                                                                                         true : false;
525                                                                                 break;
526                                                                         case VK_CONTROL:
527                                                                                 system->m_seperateLeftRight = 
528                                                                                         (HIBYTE(::GetKeyState(VK_LCONTROL)) != 0) ||
529                                                                                         (HIBYTE(::GetKeyState(VK_RCONTROL)) != 0) ?
530                                                                                         true : false;
531                                                                                 break;
532                                                                         case VK_MENU:
533                                                                                 system->m_seperateLeftRight = 
534                                                                                         (HIBYTE(::GetKeyState(VK_LMENU)) != 0) ||
535                                                                                         (HIBYTE(::GetKeyState(VK_RMENU)) != 0) ?
536                                                                                         true : false;
537                                                                                 break;
538                                                                 }
539                                                                 system->m_seperateLeftRightInitialized = true;
540                                                         }
541                                                         system->processModifierKeys(window);
542                                                         // Bypass call to DefWindowProc
543                                                         return 0;
544                                                 default:
545                                                         event = processKeyEvent(window, true, wParam, lParam);
546                                                         if (!event) {
547                                                                 GHOST_PRINT("GHOST_SystemWin32::wndProc: key event ")
548                                                                 GHOST_PRINT(msg)
549                                                                 GHOST_PRINT(" key ignored\n")
550                                                         }
551                                                         break;
552                                                 }
553                                         break;
554
555                                 case WM_KEYUP:
556                                 case WM_SYSKEYUP:
557                                         switch (wParam) {
558                                                 case VK_SHIFT:
559                                                 case VK_CONTROL:
560                                                 case VK_MENU:
561                                                         system->processModifierKeys(window);
562                                                         // Bypass call to DefWindowProc
563                                                         return 0;
564                                                 default:
565                                                         event = processKeyEvent(window, false, wParam, lParam);
566                                                         if (!event) {
567                                                                 GHOST_PRINT("GHOST_SystemWin32::wndProc: key event ")
568                                                                 GHOST_PRINT(msg)
569                                                                 GHOST_PRINT(" key ignored\n")
570                                                         }
571                                                         break;
572                                         }
573                                         break;
574
575                                 ////////////////////////////////////////////////////////////////////////
576                                 // Keyboard events, ignored
577                                 ////////////////////////////////////////////////////////////////////////
578                                 case WM_CHAR:
579                                         /* The WM_CHAR message is posted to the window with the keyboard focus when 
580                                          * a WM_KEYDOWN message is translated by the TranslateMessage function. WM_CHAR 
581                                          * contains the character code of the key that was pressed. 
582                                          */
583                                 case WM_DEADCHAR:
584                                         /* The WM_DEADCHAR message is posted to the window with the keyboard focus when a
585                                          * WM_KEYUP message is translated by the TranslateMessage function. WM_DEADCHAR 
586                                          * specifies a character code generated by a dead key. A dead key is a key that 
587                                          * generates a character, such as the umlaut (double-dot), that is combined with 
588                                          * another character to form a composite character. For example, the umlaut-O 
589                                          * character (Ö) is generated by typing the dead key for the umlaut character, and 
590                                          * then typing the O key.
591                                          */
592                                 case WM_SYSDEADCHAR:
593                                         /* The WM_SYSDEADCHAR message is sent to the window with the keyboard focus when 
594                                          * a WM_SYSKEYDOWN message is translated by the TranslateMessage function. 
595                                          * WM_SYSDEADCHAR specifies the character code of a system dead key - that is, 
596                                          * a dead key that is pressed while holding down the alt key. 
597                                          */
598                                         break;
599
600                                 ////////////////////////////////////////////////////////////////////////
601                                 // Mouse events, processed
602                                 ////////////////////////////////////////////////////////////////////////
603                                 case WM_LBUTTONDOWN:
604                                         window->registerMouseClickEvent(true);
605                                         event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskLeft);
606                                         break;
607                                 case WM_MBUTTONDOWN:
608                                         window->registerMouseClickEvent(true);
609                                         event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskMiddle);
610                                         break;
611                                 case WM_RBUTTONDOWN:
612                                         window->registerMouseClickEvent(true);
613                                         event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskRight);
614                                         break;
615                                 case WM_LBUTTONUP:
616                                         window->registerMouseClickEvent(false);
617                                         event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskLeft);
618                                         break;
619                                 case WM_MBUTTONUP:
620                                         window->registerMouseClickEvent(false);
621                                         event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskMiddle);
622                                         break;
623                                 case WM_RBUTTONUP:
624                                         window->registerMouseClickEvent(false);
625                                         event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskRight);
626                                         break;
627                                 case WM_MOUSEMOVE:
628                                         event = processCursorEvent(GHOST_kEventCursorMove, window);
629                                         break;
630                                 case WM_SETCURSOR:
631                                         /* The WM_SETCURSOR message is sent to a window if the mouse causes the cursor
632                                          * to move within a window and mouse input is not captured.
633                                          * This means we have to set the cursor shape every time the mouse moves!
634                                          * The DefWindowProc function uses this message to set the cursor to an 
635                                          * arrow if it is not in the client area.
636                                          */
637                                         if (LOWORD(lParam) == HTCLIENT) {
638                                                 // Load the current cursor
639                                                 window->loadCursor(window->getCursorVisibility(), window->getCursorShape());
640                                                 // Bypass call to DefWindowProc
641                                                 return 0;
642                                         } 
643                                         else {
644                                                 // Outside of client area show standard cursor
645                                                 window->loadCursor(true, GHOST_kStandardCursorDefault);
646                                         }
647                                         break;
648
649                                 ////////////////////////////////////////////////////////////////////////
650                                 // Mouse events, ignored
651                                 ////////////////////////////////////////////////////////////////////////
652                                 case WM_NCMOUSEMOVE:
653                                         /* The WM_NCMOUSEMOVE message is posted to a window when the cursor is moved 
654                                          * within the nonclient area of the window. This message is posted to the window 
655                                          * that contains the cursor. If a window has captured the mouse, this message is not posted.
656                                          */
657                                 case WM_NCHITTEST:
658                                         /* The WM_NCHITTEST message is sent to a window when the cursor moves, or 
659                                          * when a mouse button is pressed or released. If the mouse is not captured, 
660                                          * the message is sent to the window beneath the cursor. Otherwise, the message 
661                                          * is sent to the window that has captured the mouse. 
662                                          */
663                                         break;
664
665                                 ////////////////////////////////////////////////////////////////////////
666                                 // Window events, processed
667                                 ////////////////////////////////////////////////////////////////////////
668                                 case WM_CLOSE:
669                                         /* The WM_CLOSE message is sent as a signal that a window or an application should terminate. */
670                                         event = processWindowEvent(GHOST_kEventWindowClose, window);
671                                         break;
672                                 case WM_ACTIVATE:
673                                         /* The WM_ACTIVATE message is sent to both the window being activated and the window being 
674                                          * deactivated. If the windows use the same input queue, the message is sent synchronously, 
675                                          * first to the window procedure of the top-level window being deactivated, then to the window
676                                          * procedure of the top-level window being activated. If the windows use different input queues,
677                                          * the message is sent asynchronously, so the window is activated immediately. 
678                                          */
679                                         event = processWindowEvent(LOWORD(wParam) ? GHOST_kEventWindowActivate : GHOST_kEventWindowDeactivate, window);
680                                         break;
681                                 case WM_PAINT:
682                                         /* An application sends the WM_PAINT message when the system or another application 
683                                          * makes a request to paint a portion of an application's window. The message is sent
684                                          * when the UpdateWindow or RedrawWindow function is called, or by the DispatchMessage 
685                                          * function when the application obtains a WM_PAINT message by using the GetMessage or 
686                                          * PeekMessage function. 
687                                          */
688                                         event = processWindowEvent(GHOST_kEventWindowUpdate, window);
689                                         ::ValidateRect(hwnd, NULL);
690                                         break;
691                                 case WM_SIZE:
692                                         /* The WM_SIZE message is sent to a window after its size has changed.
693                                          * The WM_SIZE and WM_MOVE messages are not sent if an application handles the 
694                                          * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
695                                          * to perform any move or size change processing during the WM_WINDOWPOSCHANGED 
696                                          * message without calling DefWindowProc.
697                                          */
698                                         event = processWindowEvent(GHOST_kEventWindowSize, window);
699                                 case WM_CAPTURECHANGED:
700                                         window->lostMouseCapture();
701                                         break;
702
703                                 ////////////////////////////////////////////////////////////////////////
704                                 // Window events, ignored
705                                 ////////////////////////////////////////////////////////////////////////
706                                 case WM_WINDOWPOSCHANGED:
707                                         /* The WM_WINDOWPOSCHANGED message is sent to a window whose size, position, or place
708                                          * in the Z order has changed as a result of a call to the SetWindowPos function or 
709                                          * another window-management function.
710                                          * The WM_SIZE and WM_MOVE messages are not sent if an application handles the 
711                                          * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
712                                          * to perform any move or size change processing during the WM_WINDOWPOSCHANGED 
713                                          * message without calling DefWindowProc.
714                                          */
715                                 case WM_MOVE:
716                                         /* The WM_SIZE and WM_MOVE messages are not sent if an application handles the 
717                                          * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
718                                          * to perform any move or size change processing during the WM_WINDOWPOSCHANGED 
719                                          * message without calling DefWindowProc. 
720                                          */
721                                 case WM_ERASEBKGND:
722                                         /* An application sends the WM_ERASEBKGND message when the window background must be 
723                                          * erased (for example, when a window is resized). The message is sent to prepare an 
724                                          * invalidated portion of a window for painting. 
725                                          */
726                                 case WM_NCPAINT:
727                                         /* An application sends the WM_NCPAINT message to a window when its frame must be painted. */
728                                 case WM_NCACTIVATE:
729                                         /* The WM_NCACTIVATE message is sent to a window when its nonclient area needs to be changed 
730                                          * to indicate an active or inactive state. 
731                                          */
732                                 case WM_DESTROY:
733                                         /* The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window 
734                                          * procedure of the window being destroyed after the window is removed from the screen. 
735                                          * This message is sent first to the window being destroyed and then to the child windows 
736                                          * (if any) as they are destroyed. During the processing of the message, it can be assumed 
737                                          * that all child windows still exist. 
738                                          */
739                                 case WM_NCDESTROY:
740                                         /* The WM_NCDESTROY message informs a window that its nonclient area is being destroyed. The 
741                                          * DestroyWindow function sends the WM_NCDESTROY message to the window following the WM_DESTROY
742                                          * message. WM_DESTROY is used to free the allocated memory object associated with the window. 
743                                          */
744                                 case WM_KILLFOCUS:
745                                         /* The WM_KILLFOCUS message is sent to a window immediately before it loses the keyboard focus. */
746                                 case WM_SHOWWINDOW:
747                                         /* The WM_SHOWWINDOW message is sent to a window when the window is about to be hidden or shown. */
748                                 case WM_WINDOWPOSCHANGING:
749                                         /* The WM_WINDOWPOSCHANGING message is sent to a window whose size, position, or place in 
750                                          * the Z order is about to change as a result of a call to the SetWindowPos function or 
751                                          * another window-management function. 
752                                          */
753                                 case WM_SETFOCUS:
754                                         /* The WM_SETFOCUS message is sent to a window after it has gained the keyboard focus. */
755                                 case WM_MOVING:
756                                         /* The WM_MOVING message is sent to a window that the user is moving. By processing 
757                                          * this message, an application can monitor the size and position of the drag rectangle
758                                          * and, if needed, change its size or position.
759                                          */
760                                 case WM_ENTERSIZEMOVE:
761                                         /* The WM_ENTERSIZEMOVE message is sent one time to a window after it enters the moving 
762                                          * or sizing modal loop. The window enters the moving or sizing modal loop when the user 
763                                          * clicks the window's title bar or sizing border, or when the window passes the 
764                                          * WM_SYSCOMMAND message to the DefWindowProc function and the wParam parameter of the 
765                                          * message specifies the SC_MOVE or SC_SIZE value. The operation is complete when 
766                                          * DefWindowProc returns. 
767                                          */
768                                         break;
769                                         
770                                 ////////////////////////////////////////////////////////////////////////
771                                 // Other events
772                                 ////////////////////////////////////////////////////////////////////////
773                                 case WM_GETTEXT:
774                                         /* An application sends a WM_GETTEXT message to copy the text that 
775                                          * corresponds to a window into a buffer provided by the caller. 
776                                          */
777                                 case WM_ACTIVATEAPP:
778                                         /* The WM_ACTIVATEAPP message is sent when a window belonging to a 
779                                          * different application than the active window is about to be activated.
780                                          * The message is sent to the application whose window is being activated
781                                          * and to the application whose window is being deactivated. 
782                                          */
783                                 case WM_TIMER:
784                                         /* The WIN32 docs say:
785                                          * The WM_TIMER message is posted to the installing thread's message queue
786                                          * when a timer expires. You can process the message by providing a WM_TIMER
787                                          * case in the window procedure. Otherwise, the default window procedure will
788                                          * call the TimerProc callback function specified in the call to the SetTimer
789                                          * function used to install the timer. 
790                                          *
791                                          * In GHOST, we let DefWindowProc call the timer callback.
792                                          */
793                                         break;
794                         }
795                 }
796                 else {
797                         // Event found for a window before the pointer to the class has been set.
798                         GHOST_PRINT("GHOST_SystemWin32::wndProc: GHOST window event before creation\n")
799                         /* These are events we typically miss at this point:
800                            WM_GETMINMAXINFO     0x24
801                            WM_NCCREATE                  0x81
802                            WM_NCCALCSIZE                0x83
803                            WM_CREATE                    0x01
804                            We let DefWindowProc do the work.
805                         */
806                 }
807         }
808         else {
809                 // Events without valid hwnd
810                 GHOST_PRINT("GHOST_SystemWin32::wndProc: event without window\n")
811         }
812
813         if (event) {
814                 system->pushEvent(event);
815                 lResult = 0;
816         }
817         else {
818                 lResult = ::DefWindowProc(hwnd, msg, wParam, lParam);
819         }
820         return lResult;
821 }