ToDo: long outstanding patch to allow access to system paths in background mode
[blender-staging.git] / intern / ghost / intern / GHOST_SystemWin32.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL 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.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /**
30
31  * $Id$
32  * Copyright (C) 2001 NaN Technologies B.V.
33  * @author      Maarten Gribnau
34  * @date        May 7, 2001
35  */
36
37 #include <iostream>
38
39 #include "GHOST_SystemWin32.h"
40 #include "GHOST_EventDragnDrop.h"
41
42 #define WIN32_LEAN_AND_MEAN
43 #ifdef _WIN32_IE
44 #undef _WIN32_IE
45 #endif
46 #define _WIN32_IE 0x0501
47 #include <windows.h>
48 #include <shlobj.h>
49
50 // win64 doesn't define GWL_USERDATA
51 #ifdef WIN32
52 #ifndef GWL_USERDATA
53 #define GWL_USERDATA GWLP_USERDATA
54 #define GWL_WNDPROC GWLP_WNDPROC
55 #endif
56 #endif
57
58 /*
59  * According to the docs the mouse wheel message is supported from windows 98 
60  * upwards. Leaving WINVER at default value, the WM_MOUSEWHEEL message and the 
61  * wheel detent value are undefined.
62  */
63 #ifndef WM_MOUSEWHEEL
64 #define WM_MOUSEWHEEL 0x020A
65 #endif // WM_MOUSEWHEEL
66 #ifndef WHEEL_DELTA
67 #define WHEEL_DELTA 120 /* Value for rolling one detent, (old convention! MS changed it) */
68 #endif // WHEEL_DELTA
69
70 /* 
71  * Defines for mouse buttons 4 and 5 aka xbutton1 and xbutton2.
72  * MSDN: Declared in Winuser.h, include Windows.h 
73  * This does not seem to work with MinGW so we define our own here.
74  */
75 #ifndef XBUTTON1
76 #define XBUTTON1 0x0001
77 #endif // XBUTTON1
78 #ifndef XBUTTON2
79 #define XBUTTON2 0x0002
80 #endif // XBUTTON2
81 #ifndef WM_XBUTTONUP
82 #define WM_XBUTTONUP 524
83 #endif // WM_XBUTTONUP
84 #ifndef WM_XBUTTONDOWN
85 #define WM_XBUTTONDOWN 523
86 #endif // WM_XBUTTONDOWN
87
88 #include "GHOST_Debug.h"
89 #include "GHOST_DisplayManagerWin32.h"
90 #include "GHOST_EventButton.h"
91 #include "GHOST_EventCursor.h"
92 #include "GHOST_EventKey.h"
93 #include "GHOST_EventWheel.h"
94 #include "GHOST_EventNDOF.h"
95 #include "GHOST_TimerTask.h"
96 #include "GHOST_TimerManager.h"
97 #include "GHOST_WindowManager.h"
98 #include "GHOST_WindowWin32.h"
99 #include "GHOST_NDOFManager.h"
100
101 // Key code values not found in winuser.h
102 #ifndef VK_MINUS
103 #define VK_MINUS 0xBD
104 #endif // VK_MINUS
105 #ifndef VK_SEMICOLON
106 #define VK_SEMICOLON 0xBA
107 #endif // VK_SEMICOLON
108 #ifndef VK_PERIOD
109 #define VK_PERIOD 0xBE
110 #endif // VK_PERIOD
111 #ifndef VK_COMMA
112 #define VK_COMMA 0xBC
113 #endif // VK_COMMA
114 #ifndef VK_QUOTE
115 #define VK_QUOTE 0xDE
116 #endif // VK_QUOTE
117 #ifndef VK_BACK_QUOTE
118 #define VK_BACK_QUOTE 0xC0
119 #endif // VK_BACK_QUOTE
120 #ifndef VK_SLASH
121 #define VK_SLASH 0xBF
122 #endif // VK_SLASH
123 #ifndef VK_BACK_SLASH
124 #define VK_BACK_SLASH 0xDC
125 #endif // VK_BACK_SLASH
126 #ifndef VK_EQUALS
127 #define VK_EQUALS 0xBB
128 #endif // VK_EQUALS
129 #ifndef VK_OPEN_BRACKET
130 #define VK_OPEN_BRACKET 0xDB
131 #endif // VK_OPEN_BRACKET
132 #ifndef VK_CLOSE_BRACKET
133 #define VK_CLOSE_BRACKET 0xDD
134 #endif // VK_CLOSE_BRACKET
135 #ifndef VK_GR_LESS
136 #define VK_GR_LESS 0xE2
137 #endif // VK_GR_LESS
138
139
140 GHOST_SystemWin32::GHOST_SystemWin32()
141 : m_hasPerformanceCounter(false), m_freq(0), m_start(0)
142 {
143         m_displayManager = new GHOST_DisplayManagerWin32 ();
144         GHOST_ASSERT(m_displayManager, "GHOST_SystemWin32::GHOST_SystemWin32(): m_displayManager==0\n");
145         m_displayManager->initialize();
146         
147         // Check if current keyboard layout uses AltGr
148         this->keyboardAltGr();
149         // Require COM for GHOST_DropTargetWin32 created in GHOST_WindowWin32.
150         OleInitialize(0);
151 }
152
153 GHOST_SystemWin32::~GHOST_SystemWin32()
154 {
155         // Shutdown COM
156         OleUninitialize();
157 }
158
159
160 GHOST_TUns64 GHOST_SystemWin32::getMilliSeconds() const
161 {
162         // Hardware does not support high resolution timers. We will use GetTickCount instead then.
163         if (!m_hasPerformanceCounter) {
164                 return ::GetTickCount();
165         }
166
167         // Retrieve current count
168         __int64 count = 0;
169         ::QueryPerformanceCounter((LARGE_INTEGER*)&count);
170
171         // Calculate the time passed since system initialization.
172         __int64 delta = 1000*(count-m_start);
173
174         GHOST_TUns64 t = (GHOST_TUns64)(delta/m_freq);
175         return t; 
176 }
177
178
179 GHOST_TUns8 GHOST_SystemWin32::getNumDisplays() const
180 {
181         GHOST_ASSERT(m_displayManager, "GHOST_SystemWin32::getNumDisplays(): m_displayManager==0\n");
182         GHOST_TUns8 numDisplays;
183         m_displayManager->getNumDisplays(numDisplays);
184         return numDisplays;
185 }
186
187
188 void GHOST_SystemWin32::getMainDisplayDimensions(GHOST_TUns32& width, GHOST_TUns32& height) const
189 {
190         width = ::GetSystemMetrics(SM_CXSCREEN);
191         height= ::GetSystemMetrics(SM_CYSCREEN);
192 }
193
194
195 GHOST_IWindow* GHOST_SystemWin32::createWindow(
196         const STR_String& title, 
197         GHOST_TInt32 left, GHOST_TInt32 top, GHOST_TUns32 width, GHOST_TUns32 height,
198         GHOST_TWindowState state, GHOST_TDrawingContextType type,
199         bool stereoVisual, const GHOST_TUns16 numOfAASamples, const GHOST_TEmbedderWindowID parentWindow )
200 {
201         GHOST_Window* window = 0;
202         window = new GHOST_WindowWin32 (this, title, left, top, width, height, state, type, stereoVisual, numOfAASamples);
203         if (window) {
204                 if (window->getValid()) {
205                         // Store the pointer to the window
206 //                      if (state != GHOST_kWindowStateFullScreen) {
207                                 m_windowManager->addWindow(window);
208 //                      }
209                 }
210                 else {
211                         // An invalid window could be one that was used to test for AA
212                         window = ((GHOST_WindowWin32*)window)->getNextWindow();
213                         
214                         // If another window is found, let the wm know about that one, but not the old one
215                         if (window->getValid()) {
216                                 m_windowManager->addWindow(window);
217                         }
218                         else {
219                                 delete window;
220                                 window = 0;
221                         }
222
223                 }
224         }
225         return window;
226 }
227
228
229 bool GHOST_SystemWin32::processEvents(bool waitForEvent)
230 {
231         MSG msg;
232         bool anyProcessed = false;
233
234         do {
235                 GHOST_TimerManager* timerMgr = getTimerManager();
236
237                 if (waitForEvent && !::PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)) {
238 #if 1
239                         ::Sleep(1);
240 #else
241                         GHOST_TUns64 next = timerMgr->nextFireTime();
242                         GHOST_TInt64 maxSleep = next - getMilliSeconds();
243                         
244                         if (next == GHOST_kFireTimeNever) {
245                                 ::WaitMessage();
246                         } else if(maxSleep >= 0.0) {
247                                 ::SetTimer(NULL, 0, maxSleep, NULL);
248                                 ::WaitMessage();
249                                 ::KillTimer(NULL, 0);
250                         }
251 #endif
252                 }
253
254                 if (timerMgr->fireTimers(getMilliSeconds())) {
255                         anyProcessed = true;
256                 }
257
258                 // Process all the events waiting for us
259                 while (::PeekMessage(&msg, 0, 0, 0, PM_REMOVE) != 0) {
260                         ::TranslateMessage(&msg);
261                         ::DispatchMessage(&msg);
262                         anyProcessed = true;
263                 }
264         } while (waitForEvent && !anyProcessed);
265
266         return anyProcessed;
267 }
268
269
270 GHOST_TSuccess GHOST_SystemWin32::getCursorPosition(GHOST_TInt32& x, GHOST_TInt32& y) const
271 {
272         POINT point;
273         if(::GetCursorPos(&point)){
274                 x = point.x;
275                 y = point.y;
276                 return GHOST_kSuccess;
277         }
278         return GHOST_kFailure;
279 }
280
281
282 GHOST_TSuccess GHOST_SystemWin32::setCursorPosition(GHOST_TInt32 x, GHOST_TInt32 y)
283 {
284         if (!GetActiveWindow())
285                 return GHOST_kFailure;
286         return ::SetCursorPos(x, y) == TRUE ? GHOST_kSuccess : GHOST_kFailure;
287 }
288
289
290 GHOST_TSuccess GHOST_SystemWin32::getModifierKeys(GHOST_ModifierKeys& keys) const
291 {
292         bool down = HIBYTE(::GetKeyState(VK_LSHIFT)) != 0;
293         keys.set(GHOST_kModifierKeyLeftShift, down);
294         down = HIBYTE(::GetKeyState(VK_RSHIFT)) != 0;
295         keys.set(GHOST_kModifierKeyRightShift, down);
296         
297         down = HIBYTE(::GetKeyState(VK_LMENU)) != 0;
298         keys.set(GHOST_kModifierKeyLeftAlt, down);
299         down = HIBYTE(::GetKeyState(VK_RMENU)) != 0;
300         keys.set(GHOST_kModifierKeyRightAlt, down);
301         
302         down = HIBYTE(::GetKeyState(VK_LCONTROL)) != 0;
303         keys.set(GHOST_kModifierKeyLeftControl, down);
304         down = HIBYTE(::GetKeyState(VK_RCONTROL)) != 0;
305         keys.set(GHOST_kModifierKeyRightControl, down);
306         
307         bool lwindown = HIBYTE(::GetKeyState(VK_LWIN)) != 0;
308         bool rwindown = HIBYTE(::GetKeyState(VK_RWIN)) != 0;
309         if(lwindown || rwindown)
310                 keys.set(GHOST_kModifierKeyOS, true);
311         else
312                 keys.set(GHOST_kModifierKeyOS, false);
313         return GHOST_kSuccess;
314 }
315
316
317 GHOST_TSuccess GHOST_SystemWin32::getButtons(GHOST_Buttons& buttons) const
318 {
319         /* Check for swapped buttons (left-handed mouse buttons)
320          * GetAsyncKeyState() will give back the state of the physical mouse buttons.
321          */
322         bool swapped = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE;
323
324         bool down = HIBYTE(::GetKeyState(VK_LBUTTON)) != 0;
325         buttons.set(swapped ? GHOST_kButtonMaskRight : GHOST_kButtonMaskLeft, down);
326
327         down = HIBYTE(::GetKeyState(VK_MBUTTON)) != 0;
328         buttons.set(GHOST_kButtonMaskMiddle, down);
329
330         down = HIBYTE(::GetKeyState(VK_RBUTTON)) != 0;
331         buttons.set(swapped ? GHOST_kButtonMaskLeft : GHOST_kButtonMaskRight, down);
332         return GHOST_kSuccess;
333 }
334
335
336 GHOST_TSuccess GHOST_SystemWin32::init()
337 {
338         GHOST_TSuccess success = GHOST_System::init();
339         
340         for(int i = 0; i < 255; i++) {
341                 m_prevKeyStatus[i] = false;
342                 m_curKeyStatus[i] = false;
343         }
344
345         /* Disable scaling on high DPI displays on Vista */
346         HMODULE user32 = ::LoadLibraryA("user32.dll");
347         typedef BOOL (WINAPI * LPFNSETPROCESSDPIAWARE)();
348         LPFNSETPROCESSDPIAWARE SetProcessDPIAware =
349                 (LPFNSETPROCESSDPIAWARE)GetProcAddress(user32, "SetProcessDPIAware");
350         if (SetProcessDPIAware)
351                 SetProcessDPIAware();
352         FreeLibrary(user32);
353
354         // Determine whether this system has a high frequency performance counter. */
355         m_hasPerformanceCounter = ::QueryPerformanceFrequency((LARGE_INTEGER*)&m_freq) == TRUE;
356         if (m_hasPerformanceCounter) {
357                 GHOST_PRINT("GHOST_SystemWin32::init: High Frequency Performance Timer available\n")
358                 ::QueryPerformanceCounter((LARGE_INTEGER*)&m_start);
359         }
360         else {
361                 GHOST_PRINT("GHOST_SystemWin32::init: High Frequency Performance Timer not available\n")
362         }
363
364         if (success) {
365                 WNDCLASS wc;
366                 wc.style= CS_HREDRAW | CS_VREDRAW;
367                 wc.lpfnWndProc= s_wndProc;
368                 wc.cbClsExtra= 0;
369                 wc.cbWndExtra= 0;
370                 wc.hInstance= ::GetModuleHandle(0);
371                 wc.hIcon = ::LoadIcon(wc.hInstance, "APPICON");
372                 
373                 if (!wc.hIcon) {
374                         ::LoadIcon(NULL, IDI_APPLICATION);
375                 }
376                 wc.hCursor = ::LoadCursor(0, IDC_ARROW);
377                 wc.hbrBackground= (HBRUSH)::GetStockObject(BLACK_BRUSH);
378                 wc.lpszMenuName = 0;
379                 wc.lpszClassName= GHOST_WindowWin32::getWindowClassName();
380
381                 // Use RegisterClassEx for setting small icon
382                 if (::RegisterClass(&wc) == 0) {
383                         success = GHOST_kFailure;
384                 }
385                 
386                 // Add low-level keyboard hook for our process.
387                 m_llKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, s_llKeyboardProc, wc.hInstance, 0);
388         }
389         
390         return success;
391 }
392
393
394 GHOST_TSuccess GHOST_SystemWin32::exit()
395 {
396         // remove our low-level keyboard hook.
397         UnhookWindowsHookEx(m_llKeyboardHook);
398         
399         return GHOST_System::exit();
400 }
401
402 void GHOST_SystemWin32::triggerKey(GHOST_IWindow *window, bool down, GHOST_TKey key)
403 {
404         GHOST_Event *extra = new GHOST_EventKey(getSystem()->getMilliSeconds(), down ? GHOST_kEventKeyDown : GHOST_kEventKeyUp, window, key, '\0');
405         ((GHOST_SystemWin32*)getSystem())->pushEvent(extra);
406 }
407 void GHOST_SystemWin32::handleModifierKeys(GHOST_IWindow *window, WPARAM wParam, LPARAM lParam, GHOST_ModifierKeys &oldModifiers, GHOST_ModifierKeys &newModifiers) const
408 {
409         switch(wParam) {
410                 case VK_SHIFT:
411                         {
412                                 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftAlt) != newModifiers.get(GHOST_kModifierKeyLeftAlt);
413                                 if(lchanged) {
414                                         ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftAlt), GHOST_kKeyLeftAlt);
415                                 } else {
416                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightAlt) != newModifiers.get(GHOST_kModifierKeyRightAlt);
417                                         if (rchanged) {
418                                                 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightAlt), GHOST_kKeyRightAlt);
419                                         }
420                                 }
421                                 lchanged = oldModifiers.get(GHOST_kModifierKeyLeftControl) != newModifiers.get(GHOST_kModifierKeyLeftControl);
422                                 if(lchanged) {
423                                         ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftControl), GHOST_kKeyLeftControl);
424                                 } else {
425                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightControl) != newModifiers.get(GHOST_kModifierKeyRightControl);
426                                         if (rchanged) {
427                                                 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightControl), GHOST_kKeyRightControl);
428                                         }
429                                 }
430                         }
431                         break;
432                 case VK_CONTROL:
433                         {
434                                 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftAlt) != newModifiers.get(GHOST_kModifierKeyLeftAlt);
435                                 if(lchanged) {
436                                         ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftAlt), GHOST_kKeyLeftAlt);
437                                 } else {
438                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightAlt) != newModifiers.get(GHOST_kModifierKeyRightAlt);
439                                         if (rchanged) {
440                                                 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightAlt), GHOST_kKeyRightAlt);
441                                         }
442                                 }
443                                 lchanged = oldModifiers.get(GHOST_kModifierKeyLeftShift) != newModifiers.get(GHOST_kModifierKeyLeftShift);
444                                 if(lchanged) {
445                                         ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftShift), GHOST_kKeyLeftShift);
446                                 } else {
447                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightShift) != newModifiers.get(GHOST_kModifierKeyRightShift);
448                                         if (rchanged) {
449                                                 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightShift), GHOST_kKeyRightShift);
450                                         }
451                                 }
452                         }
453                         break;
454                 case VK_MENU:
455                         {
456                                 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftShift) != newModifiers.get(GHOST_kModifierKeyLeftShift);
457                                 if(lchanged) {
458                                         ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftShift), GHOST_kKeyLeftShift);
459                                 } else {
460                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightShift) != newModifiers.get(GHOST_kModifierKeyRightShift);
461                                         if (rchanged) {
462                                                 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightShift), GHOST_kKeyRightShift);
463                                         }
464                                 }
465                                 lchanged = oldModifiers.get(GHOST_kModifierKeyLeftControl) != newModifiers.get(GHOST_kModifierKeyLeftControl);
466                                 if(lchanged) {
467                                         ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyLeftControl), GHOST_kKeyLeftControl);
468                                 } else {
469                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightControl) != newModifiers.get(GHOST_kModifierKeyRightControl);
470                                         if (rchanged) {
471                                                 ((GHOST_SystemWin32*)getSystem())->triggerKey(window, newModifiers.get(GHOST_kModifierKeyRightControl), GHOST_kKeyRightControl);
472                                         }
473                                 }
474                         }
475                         break;
476                 default:
477                         break;
478         }
479 }
480
481 GHOST_TKey GHOST_SystemWin32::convertKey(GHOST_IWindow *window, WPARAM wParam, LPARAM lParam) const
482 {
483         GHOST_SystemWin32 *system = (GHOST_SystemWin32 *)getSystem();
484         bool isExtended = (lParam&(1<<24))?true:false;
485         
486         GHOST_TKey key;
487         GHOST_ModifierKeys oldModifiers, newModifiers;
488         system->retrieveModifierKeys(oldModifiers);
489         system->getModifierKeys(newModifiers);
490
491         //std::cout << wParam << " " << system->m_curKeyStatus[wParam] << " shift pressed: " << system->shiftPressed() << std::endl;
492
493         if ((wParam >= '0') && (wParam <= '9')) {
494                 // VK_0 thru VK_9 are the same as ASCII '0' thru '9' (0x30 - 0x39)
495                 key = (GHOST_TKey)(wParam - '0' + GHOST_kKey0);
496         }
497         else if ((wParam >= 'A') && (wParam <= 'Z')) {
498                 // VK_A thru VK_Z are the same as ASCII 'A' thru 'Z' (0x41 - 0x5A)
499                 key = (GHOST_TKey)(wParam - 'A' + GHOST_kKeyA);
500         }
501         else if ((wParam >= VK_F1) && (wParam <= VK_F24)) {
502                 key = (GHOST_TKey)(wParam - VK_F1 + GHOST_kKeyF1);
503         }
504         else {
505                 switch (wParam) {
506                 case VK_RETURN:
507                         key = isExtended?GHOST_kKeyNumpadEnter:GHOST_kKeyEnter;
508                         break;
509
510                 case VK_BACK:     key = GHOST_kKeyBackSpace;            break;
511                 case VK_TAB:      key = GHOST_kKeyTab;                          break;
512                 case VK_ESCAPE:   key = GHOST_kKeyEsc;                          break;
513                 case VK_SPACE:    key = GHOST_kKeySpace;                        break;
514                 case VK_PRIOR:    key = GHOST_kKeyUpPage;                       break;
515                 case VK_NEXT:     key = GHOST_kKeyDownPage;                     break;
516                 case VK_END:      key = GHOST_kKeyEnd;                          break;
517                 case VK_HOME:
518                         {
519                                 if(system->m_curKeyStatus[VK_NUMPAD7] && system->shiftPressed())
520                                         key = GHOST_kKeyNumpad7;
521                                 else
522                                         key = GHOST_kKeyHome;
523                         }
524                         break;
525                 case VK_INSERT:   key = GHOST_kKeyInsert;                       break;
526                 case VK_DELETE:   key = GHOST_kKeyDelete;                       break;
527                 case VK_LEFT:     key = GHOST_kKeyLeftArrow;            break;
528                 case VK_RIGHT:    key = GHOST_kKeyRightArrow;           break;
529                 case VK_UP:       key = GHOST_kKeyUpArrow;                      break;
530                 case VK_DOWN:     key = GHOST_kKeyDownArrow;            break;
531                 case VK_NUMPAD0:  key = GHOST_kKeyNumpad0;                      break;
532                 case VK_NUMPAD1:  key = GHOST_kKeyNumpad1;                      break;
533                 case VK_NUMPAD2:  key = GHOST_kKeyNumpad2;                      break;
534                 case VK_NUMPAD3:  key = GHOST_kKeyNumpad3;                      break;
535                 case VK_NUMPAD4:  key = GHOST_kKeyNumpad4;                      break;
536                 case VK_NUMPAD5:  key = GHOST_kKeyNumpad5;                      break;
537                 case VK_NUMPAD6:  key = GHOST_kKeyNumpad6;                      break;
538                 case VK_NUMPAD7:  key = GHOST_kKeyNumpad7;                      break;
539                 case VK_NUMPAD8:  key = GHOST_kKeyNumpad8;                      break;
540                 case VK_NUMPAD9:  key = GHOST_kKeyNumpad9;                      break;
541                 case VK_SNAPSHOT: key = GHOST_kKeyPrintScreen;          break;
542                 case VK_PAUSE:    key = GHOST_kKeyPause;                        break;
543                 case VK_MULTIPLY: key = GHOST_kKeyNumpadAsterisk;        break;
544                 case VK_SUBTRACT: key = GHOST_kKeyNumpadMinus;          break;
545                 case VK_DECIMAL:  key = GHOST_kKeyNumpadPeriod;         break;
546                 case VK_DIVIDE:   key = GHOST_kKeyNumpadSlash;          break;
547                 case VK_ADD:      key = GHOST_kKeyNumpadPlus;           break;
548
549                 case VK_SEMICOLON:              key = GHOST_kKeySemicolon;              break;
550                 case VK_EQUALS:                 key = GHOST_kKeyEqual;                  break;
551                 case VK_COMMA:                  key = GHOST_kKeyComma;                  break;
552                 case VK_MINUS:                  key = GHOST_kKeyMinus;                  break;
553                 case VK_PERIOD:                 key = GHOST_kKeyPeriod;                 break;
554                 case VK_SLASH:                  key = GHOST_kKeySlash;                  break;
555                 case VK_BACK_QUOTE:             key = GHOST_kKeyAccentGrave;    break;
556                 case VK_OPEN_BRACKET:   key = GHOST_kKeyLeftBracket;    break;
557                 case VK_BACK_SLASH:             key = GHOST_kKeyBackslash;              break;
558                 case VK_CLOSE_BRACKET:  key = GHOST_kKeyRightBracket;   break;
559                 case VK_QUOTE:                  key = GHOST_kKeyQuote;                  break;
560                 case VK_GR_LESS:                key = GHOST_kKeyGrLess;                 break;
561
562                 case VK_SHIFT:
563                         {
564                                 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftShift) != newModifiers.get(GHOST_kModifierKeyLeftShift);
565                                 if(lchanged) {
566                                         key = GHOST_kKeyLeftShift;
567                                 } else {
568                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightShift) != newModifiers.get(GHOST_kModifierKeyRightShift);
569                                         if(rchanged) {
570                                                 key = GHOST_kKeyRightShift;
571                                         } else {
572                                                 key = GHOST_kKeyUnknown;
573                                         }
574                                 }
575                         }
576                         break;
577                 case VK_CONTROL:
578                         {
579                                 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftControl) != newModifiers.get(GHOST_kModifierKeyLeftControl);
580                                 if(lchanged) {
581                                         key = GHOST_kKeyLeftControl;
582                                 } else {
583                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightControl) != newModifiers.get(GHOST_kModifierKeyRightControl);
584                                         if(rchanged) {
585                                                 key = GHOST_kKeyRightControl;
586                                         } else {
587                                                 key = GHOST_kKeyUnknown;
588                                         }
589                                 }
590                         }
591                         break;
592                 case VK_MENU:
593                         {
594                                 if(m_hasAltGr && isExtended) {
595                                         // We have here an extended RAlt, which is AltGr. The keyboard driver on Windows sends before this a LControl, so
596                                         // to be able to input characters created with AltGr (normal on German, French, Finnish and other keyboards) we
597                                         // push an extra LControl up event. This ensures we don't have a 'hanging' ctrl event in Blender windowmanager
598                                         // when typing in Text editor or Console.
599                                         GHOST_Event *extra = new GHOST_EventKey(getSystem()->getMilliSeconds(), GHOST_kEventKeyUp, window, GHOST_kKeyLeftControl, '\0');
600                                         ((GHOST_SystemWin32*)getSystem())->pushEvent(extra);
601                                         newModifiers.set(GHOST_kModifierKeyRightControl, false);
602                                         newModifiers.set(GHOST_kModifierKeyLeftControl, false);
603                                 }
604                                 bool lchanged = oldModifiers.get(GHOST_kModifierKeyLeftAlt) != newModifiers.get(GHOST_kModifierKeyLeftAlt);
605                                 if(lchanged) {
606                                         key = GHOST_kKeyLeftAlt;
607                                 } else {
608                                         bool rchanged = oldModifiers.get(GHOST_kModifierKeyRightAlt) != newModifiers.get(GHOST_kModifierKeyRightAlt);
609                                         if(rchanged) {
610                                                 key = GHOST_kKeyRightAlt;
611                                         } else {
612                                                 key = GHOST_kKeyUnknown;
613                                         }
614                                 }
615                         }
616                         break;
617                 case VK_LWIN:
618                 case VK_RWIN:
619                         key = GHOST_kKeyOS;
620                         break;
621                 case VK_NUMLOCK: key = GHOST_kKeyNumLock; break;
622                 case VK_SCROLL: key = GHOST_kKeyScrollLock; break;
623                 case VK_CAPITAL: key = GHOST_kKeyCapsLock; break;
624                 default:
625                         key = GHOST_kKeyUnknown;
626                         break;
627                 }
628         }
629         ((GHOST_SystemWin32*)getSystem())->storeModifierKeys(newModifiers);
630         return key;
631 }
632
633 GHOST_EventButton* GHOST_SystemWin32::processButtonEvent(GHOST_TEventType type, GHOST_IWindow *window, GHOST_TButtonMask mask)
634 {
635         return new GHOST_EventButton (getSystem()->getMilliSeconds(), type, window, mask);
636 }
637
638
639 GHOST_EventCursor* GHOST_SystemWin32::processCursorEvent(GHOST_TEventType type, GHOST_IWindow *Iwindow)
640 {
641         GHOST_TInt32 x_screen, y_screen;
642         GHOST_SystemWin32 * system = ((GHOST_SystemWin32 * ) getSystem());
643         GHOST_WindowWin32 * window = ( GHOST_WindowWin32 * ) Iwindow;
644         
645         system->getCursorPosition(x_screen, y_screen);
646
647         if(window->getCursorGrabMode() != GHOST_kGrabDisable && window->getCursorGrabMode() != GHOST_kGrabNormal)
648         {
649                 GHOST_TInt32 x_new= x_screen;
650                 GHOST_TInt32 y_new= y_screen;
651                 GHOST_TInt32 x_accum, y_accum;
652                 GHOST_Rect bounds;
653
654                 /* fallback to window bounds */
655                 if(window->getCursorGrabBounds(bounds)==GHOST_kFailure){
656                         window->getClientBounds(bounds);
657                 }
658
659                 /* could also clamp to screen bounds
660                  * wrap with a window outside the view will fail atm  */
661
662                 bounds.wrapPoint(x_new, y_new, 2); /* offset of one incase blender is at screen bounds */
663
664                 window->getCursorGrabAccum(x_accum, y_accum);
665                 if(x_new != x_screen|| y_new != y_screen) {
666                         /* when wrapping we don't need to add an event because the
667                          * setCursorPosition call will cause a new event after */
668                         system->setCursorPosition(x_new, y_new); /* wrap */
669                         window->setCursorGrabAccum(x_accum + (x_screen - x_new), y_accum + (y_screen - y_new));
670                 }else{
671                         return new GHOST_EventCursor(system->getMilliSeconds(),
672                                                                                  GHOST_kEventCursorMove,
673                                                                                  window,
674                                                                                  x_screen + x_accum,
675                                                                                  y_screen + y_accum
676                         );
677                 }
678
679         }
680         else {
681                 return new GHOST_EventCursor(system->getMilliSeconds(),
682                                                                          GHOST_kEventCursorMove,
683                                                                          window,
684                                                                          x_screen,
685                                                                          y_screen
686                 );
687         }
688         return NULL;
689 }
690
691
692 GHOST_EventWheel* GHOST_SystemWin32::processWheelEvent(GHOST_IWindow *window, WPARAM wParam, LPARAM lParam)
693 {
694         // short fwKeys = LOWORD(wParam);                       // key flags
695         int zDelta = (short) HIWORD(wParam);    // wheel rotation
696         
697         // zDelta /= WHEEL_DELTA;
698         // temporary fix below: microsoft now has added more precision, making the above division not work
699         if (zDelta <= 0 ) zDelta= -1; else zDelta= 1;   
700         
701         // short xPos = (short) LOWORD(lParam); // horizontal position of pointer
702         // short yPos = (short) HIWORD(lParam); // vertical position of pointer
703         return new GHOST_EventWheel (getSystem()->getMilliSeconds(), window, zDelta);
704 }
705
706
707 GHOST_EventKey* GHOST_SystemWin32::processKeyEvent(GHOST_IWindow *window, bool keyDown, WPARAM wParam, LPARAM lParam)
708 {
709         GHOST_TKey key = ((GHOST_SystemWin32*)getSystem())->convertKey(window, wParam, lParam);
710         GHOST_EventKey* event;
711         if (key != GHOST_kKeyUnknown) {
712                 MSG keyMsg;
713                 char ascii = '\0';
714
715                         /* Eat any character related messages */
716                 if (::PeekMessage(&keyMsg, NULL, WM_CHAR, WM_SYSDEADCHAR, PM_REMOVE)) {
717                         ascii = (char) keyMsg.wParam;
718                         
719                 }
720
721                 event = new GHOST_EventKey(getSystem()->getMilliSeconds(), keyDown ? GHOST_kEventKeyDown: GHOST_kEventKeyUp, window, key, ascii);
722         }
723         else {
724                 event = 0;
725         }
726         return event;
727 }
728
729
730 GHOST_Event* GHOST_SystemWin32::processWindowEvent(GHOST_TEventType type, GHOST_IWindow* window)
731 {
732         return new GHOST_Event(getSystem()->getMilliSeconds(), type, window);
733 }
734
735 GHOST_TSuccess GHOST_SystemWin32::pushDragDropEvent(GHOST_TEventType eventType, 
736                                                                                                         GHOST_TDragnDropTypes draggedObjectType,
737                                                                                                         GHOST_IWindow* window,
738                                                                                                         int mouseX, int mouseY,
739                                                                                                         void* data)
740 {
741         GHOST_SystemWin32* system = ((GHOST_SystemWin32*)getSystem());
742         return system->pushEvent(new GHOST_EventDragnDrop(system->getMilliSeconds(),
743                                                                                                           eventType,
744                                                                                                           draggedObjectType,
745                                                                                                           window,mouseX,mouseY,data)
746                         );
747 }
748
749 void GHOST_SystemWin32::processMinMaxInfo(MINMAXINFO * minmax)
750 {
751         minmax->ptMinTrackSize.x=320;
752         minmax->ptMinTrackSize.y=240;
753 }
754
755 /* Note that this function gets *all* key events from the entire system (all
756  * threads running in this desktop session. So when getting event here, don't assume
757  * it's for Blender. Thus we only do status bookkeeping, so we can check
758  * in s_wndProc and processKeyEvent what the real keyboard status is.
759  * This is needed for proper handling of shift+numpad keys for instance.
760  */
761 LRESULT CALLBACK GHOST_SystemWin32::s_llKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
762 {
763         GHOST_SystemWin32* system = ((GHOST_SystemWin32*)getSystem());
764         
765         bool down = false;
766         if(wParam==WM_KEYDOWN || wParam==WM_SYSKEYDOWN ){
767                 down = true;
768         }
769         
770         if(nCode!=HC_ACTION)
771                 return CallNextHookEx(system->m_llKeyboardHook, nCode, wParam, lParam);
772                 
773         KBDLLHOOKSTRUCT &keyb = *(PKBDLLHOOKSTRUCT)(lParam);
774         system->m_prevKeyStatus[keyb.vkCode] = system->m_curKeyStatus[keyb.vkCode];
775         //std::cout << "ll: " << keyb.vkCode << " " << down << " ||| ";
776         if(keyb.flags) {
777                 if((keyb.flags & LLKHF_EXTENDED) == LLKHF_EXTENDED) {
778                         //std::cout << "extended ";
779                 }
780                 if((keyb.flags & LLKHF_ALTDOWN) == LLKHF_ALTDOWN) {
781                         //std::cout << "alt ";
782                 }
783                 if((keyb.flags & LLKHF_INJECTED)== LLKHF_INJECTED) {
784                         //std::cout << "injected ";
785                 }
786                 if((keyb.flags & LLKHF_UP) == LLKHF_UP) {
787                         system->m_curKeyStatus[keyb.vkCode] = false;
788                         //std::cout << "up" << std::endl;
789                 } else {
790                         system->m_curKeyStatus[keyb.vkCode] = true;
791                         //std::cout << "down" << std::endl;
792                 }
793         }
794         else {
795                 system->m_curKeyStatus[keyb.vkCode] = true;
796                 //std::cout << "down" << std::endl;
797         }
798         
799         return CallNextHookEx(system->m_llKeyboardHook, nCode, wParam, lParam);
800 }
801
802 LRESULT WINAPI GHOST_SystemWin32::s_wndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
803 {
804         GHOST_Event* event = 0;
805         LRESULT lResult = 0;
806         GHOST_SystemWin32* system = ((GHOST_SystemWin32*)getSystem());
807         GHOST_ASSERT(system, "GHOST_SystemWin32::s_wndProc(): system not initialized")
808
809         if (hwnd) {
810                 GHOST_WindowWin32* window = (GHOST_WindowWin32*)::GetWindowLong(hwnd, GWL_USERDATA);
811                 if (window) {
812                         switch (msg) {
813                                 // we need to check if new key layout has AltGr
814                                 case WM_INPUTLANGCHANGE:
815                                         system->keyboardAltGr();
816                                         break;
817                                 ////////////////////////////////////////////////////////////////////////
818                                 // Keyboard events, processed
819                                 ////////////////////////////////////////////////////////////////////////
820                                 case WM_KEYDOWN:
821                                 case WM_SYSKEYDOWN:
822                                         event = processKeyEvent(window, true, wParam, lParam);
823                                         if (!event) {
824                                                 GHOST_PRINT("GHOST_SystemWin32::wndProc: key event ")
825                                                 GHOST_PRINT(msg)
826                                                 GHOST_PRINT(" key ignored\n")
827                                         }
828                                         break;
829
830                                 case WM_KEYUP:
831                                 case WM_SYSKEYUP:
832                                         event = processKeyEvent(window, false, wParam, lParam);
833                                         if (!event) {
834                                                 GHOST_PRINT("GHOST_SystemWin32::wndProc: key event ")
835                                                 GHOST_PRINT(msg)
836                                                 GHOST_PRINT(" key ignored\n")
837                                         }
838                                         break;
839
840                                 ////////////////////////////////////////////////////////////////////////
841                                 // Keyboard events, ignored
842                                 ////////////////////////////////////////////////////////////////////////
843                                 case WM_CHAR:
844                                         /* The WM_CHAR message is posted to the window with the keyboard focus when 
845                                          * a WM_KEYDOWN message is translated by the TranslateMessage function. WM_CHAR 
846                                          * contains the character code of the key that was pressed. 
847                                          */
848                                 case WM_DEADCHAR:
849                                         /* The WM_DEADCHAR message is posted to the window with the keyboard focus when a
850                                          * WM_KEYUP message is translated by the TranslateMessage function. WM_DEADCHAR 
851                                          * specifies a character code generated by a dead key. A dead key is a key that 
852                                          * generates a character, such as the umlaut (double-dot), that is combined with 
853                                          * another character to form a composite character. For example, the umlaut-O 
854                                          * character (Ö) is generated by typing the dead key for the umlaut character, and
855                                          * then typing the O key.
856                                          */
857                                 case WM_SYSDEADCHAR:
858                                         /* The WM_SYSDEADCHAR message is sent to the window with the keyboard focus when 
859                                          * a WM_SYSKEYDOWN message is translated by the TranslateMessage function. 
860                                          * WM_SYSDEADCHAR specifies the character code of a system dead key - that is, 
861                                          * a dead key that is pressed while holding down the alt key. 
862                                          */
863                                         break;
864                                 ////////////////////////////////////////////////////////////////////////
865                                 // Tablet events, processed
866                                 ////////////////////////////////////////////////////////////////////////
867                                 case WT_PACKET:
868                                         ((GHOST_WindowWin32*)window)->processWin32TabletEvent(wParam, lParam);
869                                         break;
870                                 case WT_CSRCHANGE:
871                                 case WT_PROXIMITY:
872                                         ((GHOST_WindowWin32*)window)->processWin32TabletInitEvent();
873                                         break;
874                                 ////////////////////////////////////////////////////////////////////////
875                                 // Mouse events, processed
876                                 ////////////////////////////////////////////////////////////////////////
877                                 case WM_LBUTTONDOWN:
878                                         window->registerMouseClickEvent(true);
879                                         event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskLeft);
880                                         break;
881                                 case WM_MBUTTONDOWN:
882                                         window->registerMouseClickEvent(true);
883                                         event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskMiddle);
884                                         break;
885                                 case WM_RBUTTONDOWN:
886                                         window->registerMouseClickEvent(true);
887                                         event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskRight);
888                                         break;
889                                 case WM_XBUTTONDOWN:
890                                         window->registerMouseClickEvent(true);
891                                         if ((short) HIWORD(wParam) == XBUTTON1){
892                                                 event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskButton4);
893                                         }else if((short) HIWORD(wParam) == XBUTTON2){
894                                                 event = processButtonEvent(GHOST_kEventButtonDown, window, GHOST_kButtonMaskButton5);
895                                         }
896                                         break;
897                                 case WM_LBUTTONUP:
898                                         window->registerMouseClickEvent(false);
899                                         event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskLeft);
900                                         break;
901                                 case WM_MBUTTONUP:
902                                         window->registerMouseClickEvent(false);
903                                         event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskMiddle);
904                                         break;
905                                 case WM_RBUTTONUP:
906                                         window->registerMouseClickEvent(false);
907                                         event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskRight);
908                                         break;
909                                 case WM_XBUTTONUP:
910                                         window->registerMouseClickEvent(false);
911                                         if ((short) HIWORD(wParam) == XBUTTON1){
912                                                 event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskButton4);
913                                         }else if((short) HIWORD(wParam) == XBUTTON2){
914                                                 event = processButtonEvent(GHOST_kEventButtonUp, window, GHOST_kButtonMaskButton5);
915                                         }
916                                         break;
917                                 case WM_MOUSEMOVE:
918                                         event = processCursorEvent(GHOST_kEventCursorMove, window);
919                                         break;
920                                 case WM_MOUSEWHEEL:
921                                         /* The WM_MOUSEWHEEL message is sent to the focus window 
922                                          * when the mouse wheel is rotated. The DefWindowProc 
923                                          * function propagates the message to the window's parent.
924                                          * There should be no internal forwarding of the message, 
925                                          * since DefWindowProc propagates it up the parent chain 
926                                          * until it finds a window that processes it.
927                                          */
928                                         event = processWheelEvent(window, wParam, lParam);
929                                         break;
930                                 case WM_SETCURSOR:
931                                         /* The WM_SETCURSOR message is sent to a window if the mouse causes the cursor
932                                          * to move within a window and mouse input is not captured.
933                                          * This means we have to set the cursor shape every time the mouse moves!
934                                          * The DefWindowProc function uses this message to set the cursor to an 
935                                          * arrow if it is not in the client area.
936                                          */
937                                         if (LOWORD(lParam) == HTCLIENT) {
938                                                 // Load the current cursor
939                                                 window->loadCursor(window->getCursorVisibility(), window->getCursorShape());
940                                                 // Bypass call to DefWindowProc
941                                                 return 0;
942                                         } 
943                                         else {
944                                                 // Outside of client area show standard cursor
945                                                 window->loadCursor(true, GHOST_kStandardCursorDefault);
946                                         }
947                                         break;
948
949                                 ////////////////////////////////////////////////////////////////////////
950                                 // Mouse events, ignored
951                                 ////////////////////////////////////////////////////////////////////////
952                                 case WM_NCMOUSEMOVE:
953                                         /* The WM_NCMOUSEMOVE message is posted to a window when the cursor is moved 
954                                          * within the nonclient area of the window. This message is posted to the window 
955                                          * that contains the cursor. If a window has captured the mouse, this message is not posted.
956                                          */
957                                 case WM_NCHITTEST:
958                                         /* The WM_NCHITTEST message is sent to a window when the cursor moves, or 
959                                          * when a mouse button is pressed or released. If the mouse is not captured, 
960                                          * the message is sent to the window beneath the cursor. Otherwise, the message 
961                                          * is sent to the window that has captured the mouse. 
962                                          */
963                                         break;
964
965                                 ////////////////////////////////////////////////////////////////////////
966                                 // Window events, processed
967                                 ////////////////////////////////////////////////////////////////////////
968                                 case WM_CLOSE:
969                                         /* The WM_CLOSE message is sent as a signal that a window or an application should terminate. */
970                                         event = processWindowEvent(GHOST_kEventWindowClose, window);
971                                         break;
972                                 case WM_ACTIVATE:
973                                         /* The WM_ACTIVATE message is sent to both the window being activated and the window being 
974                                          * deactivated. If the windows use the same input queue, the message is sent synchronously, 
975                                          * first to the window procedure of the top-level window being deactivated, then to the window
976                                          * procedure of the top-level window being activated. If the windows use different input queues,
977                                          * the message is sent asynchronously, so the window is activated immediately. 
978                                          */
979                                         event = processWindowEvent(LOWORD(wParam) ? GHOST_kEventWindowActivate : GHOST_kEventWindowDeactivate, window);
980                                         /* WARNING: Let DefWindowProc handle WM_ACTIVATE, otherwise WM_MOUSEWHEEL
981                                         will not be dispatched to OUR active window if we minimize one of OUR windows. */
982                                         lResult = ::DefWindowProc(hwnd, msg, wParam, lParam);
983                                         break;
984                                 case WM_PAINT:
985                                         /* An application sends the WM_PAINT message when the system or another application 
986                                          * makes a request to paint a portion of an application's window. The message is sent
987                                          * when the UpdateWindow or RedrawWindow function is called, or by the DispatchMessage 
988                                          * function when the application obtains a WM_PAINT message by using the GetMessage or 
989                                          * PeekMessage function. 
990                                          */
991                                         event = processWindowEvent(GHOST_kEventWindowUpdate, window);
992                                         ::ValidateRect(hwnd, NULL);
993                                         break;
994                                 case WM_GETMINMAXINFO:
995                                         /* The WM_GETMINMAXINFO message is sent to a window when the size or 
996                                          * position of the window is about to change. An application can use 
997                                          * this message to override the window's default maximized size and 
998                                          * position, or its default minimum or maximum tracking size. 
999                                          */
1000                                         processMinMaxInfo((MINMAXINFO *) lParam);
1001                                         /* Let DefWindowProc handle it. */
1002                                         break;
1003                                 case WM_SIZE:
1004                                         /* The WM_SIZE message is sent to a window after its size has changed.
1005                                          * The WM_SIZE and WM_MOVE messages are not sent if an application handles the 
1006                                          * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
1007                                          * to perform any move or size change processing during the WM_WINDOWPOSCHANGED 
1008                                          * message without calling DefWindowProc.
1009                                          */
1010                                         event = processWindowEvent(GHOST_kEventWindowSize, window);
1011                                         break;
1012                                 case WM_CAPTURECHANGED:
1013                                         window->lostMouseCapture();
1014                                         break;
1015                                 case WM_MOVING:
1016                                         /* The WM_MOVING message is sent to a window that the user is moving. By processing 
1017                                          * this message, an application can monitor the size and position of the drag rectangle
1018                                          * and, if needed, change its size or position.
1019                                          */
1020                                 case WM_MOVE:
1021                                         /* The WM_SIZE and WM_MOVE messages are not sent if an application handles the 
1022                                          * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
1023                                          * to perform any move or size change processing during the WM_WINDOWPOSCHANGED 
1024                                          * message without calling DefWindowProc. 
1025                                          */
1026                                         event = processWindowEvent(GHOST_kEventWindowMove, window);
1027                                         break;
1028                                 ////////////////////////////////////////////////////////////////////////
1029                                 // Window events, ignored
1030                                 ////////////////////////////////////////////////////////////////////////
1031                                 case WM_WINDOWPOSCHANGED:
1032                                         /* The WM_WINDOWPOSCHANGED message is sent to a window whose size, position, or place
1033                                          * in the Z order has changed as a result of a call to the SetWindowPos function or 
1034                                          * another window-management function.
1035                                          * The WM_SIZE and WM_MOVE messages are not sent if an application handles the 
1036                                          * WM_WINDOWPOSCHANGED message without calling DefWindowProc. It is more efficient
1037                                          * to perform any move or size change processing during the WM_WINDOWPOSCHANGED 
1038                                          * message without calling DefWindowProc.
1039                                          */
1040                                 case WM_ERASEBKGND:
1041                                         /* An application sends the WM_ERASEBKGND message when the window background must be 
1042                                          * erased (for example, when a window is resized). The message is sent to prepare an 
1043                                          * invalidated portion of a window for painting. 
1044                                          */
1045                                 case WM_NCPAINT:
1046                                         /* An application sends the WM_NCPAINT message to a window when its frame must be painted. */
1047                                 case WM_NCACTIVATE:
1048                                         /* The WM_NCACTIVATE message is sent to a window when its nonclient area needs to be changed 
1049                                          * to indicate an active or inactive state. 
1050                                          */
1051                                 case WM_DESTROY:
1052                                         /* The WM_DESTROY message is sent when a window is being destroyed. It is sent to the window 
1053                                          * procedure of the window being destroyed after the window is removed from the screen. 
1054                                          * This message is sent first to the window being destroyed and then to the child windows 
1055                                          * (if any) as they are destroyed. During the processing of the message, it can be assumed 
1056                                          * that all child windows still exist. 
1057                                          */
1058                                 case WM_NCDESTROY:
1059                                         /* The WM_NCDESTROY message informs a window that its nonclient area is being destroyed. The 
1060                                          * DestroyWindow function sends the WM_NCDESTROY message to the window following the WM_DESTROY
1061                                          * message. WM_DESTROY is used to free the allocated memory object associated with the window. 
1062                                          */
1063                                 case WM_KILLFOCUS:
1064                                         /* The WM_KILLFOCUS message is sent to a window immediately before it loses the keyboard focus. */
1065                                 case WM_SHOWWINDOW:
1066                                         /* The WM_SHOWWINDOW message is sent to a window when the window is about to be hidden or shown. */
1067                                 case WM_WINDOWPOSCHANGING:
1068                                         /* The WM_WINDOWPOSCHANGING message is sent to a window whose size, position, or place in 
1069                                          * the Z order is about to change as a result of a call to the SetWindowPos function or 
1070                                          * another window-management function. 
1071                                          */
1072                                 case WM_SETFOCUS:
1073                                         /* The WM_SETFOCUS message is sent to a window after it has gained the keyboard focus. */
1074                                 case WM_ENTERSIZEMOVE:
1075                                         /* The WM_ENTERSIZEMOVE message is sent one time to a window after it enters the moving 
1076                                          * or sizing modal loop. The window enters the moving or sizing modal loop when the user 
1077                                          * clicks the window's title bar or sizing border, or when the window passes the 
1078                                          * WM_SYSCOMMAND message to the DefWindowProc function and the wParam parameter of the 
1079                                          * message specifies the SC_MOVE or SC_SIZE value. The operation is complete when 
1080                                          * DefWindowProc returns. 
1081                                          */
1082                                         break;
1083                                         
1084                                 ////////////////////////////////////////////////////////////////////////
1085                                 // Other events
1086                                 ////////////////////////////////////////////////////////////////////////
1087                                 case WM_GETTEXT:
1088                                         /* An application sends a WM_GETTEXT message to copy the text that 
1089                                          * corresponds to a window into a buffer provided by the caller. 
1090                                          */
1091                                 case WM_ACTIVATEAPP:
1092                                         /* The WM_ACTIVATEAPP message is sent when a window belonging to a 
1093                                          * different application than the active window is about to be activated.
1094                                          * The message is sent to the application whose window is being activated
1095                                          * and to the application whose window is being deactivated. 
1096                                          */
1097                                 case WM_TIMER:
1098                                         /* The WIN32 docs say:
1099                                          * The WM_TIMER message is posted to the installing thread's message queue
1100                                          * when a timer expires. You can process the message by providing a WM_TIMER
1101                                          * case in the window procedure. Otherwise, the default window procedure will
1102                                          * call the TimerProc callback function specified in the call to the SetTimer
1103                                          * function used to install the timer. 
1104                                          *
1105                                          * In GHOST, we let DefWindowProc call the timer callback.
1106                                          */
1107                                         break;
1108                                 case WM_BLND_NDOF_AXIS:
1109                                         {
1110                                                 GHOST_TEventNDOFData ndofdata;
1111                                                 system->m_ndofManager->GHOST_NDOFGetDatas(ndofdata);
1112                                                 system->m_eventManager->
1113                                                         pushEvent(new GHOST_EventNDOF(
1114                                                                 system->getMilliSeconds(), 
1115                                                                 GHOST_kEventNDOFMotion, 
1116                                                                 window, ndofdata));
1117                                         }
1118                                         break;
1119                                 case WM_BLND_NDOF_BTN:
1120                                         {
1121                                                 GHOST_TEventNDOFData ndofdata;
1122                                                 system->m_ndofManager->GHOST_NDOFGetDatas(ndofdata);
1123                                                 system->m_eventManager->
1124                                                         pushEvent(new GHOST_EventNDOF(
1125                                                                 system->getMilliSeconds(), 
1126                                                                 GHOST_kEventNDOFButton, 
1127                                                                 window, ndofdata));
1128                                         }
1129                                         break;
1130                         }
1131                 }
1132                 else {
1133                         // Event found for a window before the pointer to the class has been set.
1134                         GHOST_PRINT("GHOST_SystemWin32::wndProc: GHOST window event before creation\n")
1135                         /* These are events we typically miss at this point:
1136                            WM_GETMINMAXINFO     0x24
1137                            WM_NCCREATE                  0x81
1138                            WM_NCCALCSIZE                0x83
1139                            WM_CREATE                    0x01
1140                            We let DefWindowProc do the work.
1141                         */
1142                 }
1143         }
1144         else {
1145                 // Events without valid hwnd
1146                 GHOST_PRINT("GHOST_SystemWin32::wndProc: event without window\n")
1147         }
1148
1149         if (event) {
1150                 system->pushEvent(event);
1151         }
1152         else {
1153                 lResult = ::DefWindowProc(hwnd, msg, wParam, lParam);
1154         }
1155         return lResult;
1156 }
1157
1158 GHOST_TUns8* GHOST_SystemWin32::getClipboard(bool selection) const 
1159 {
1160         char *buffer;
1161         char *temp_buff;
1162         
1163         if ( IsClipboardFormatAvailable(CF_TEXT) && OpenClipboard(NULL) ) {
1164                 HANDLE hData = GetClipboardData( CF_TEXT );
1165                 if (hData == NULL) {
1166                         CloseClipboard();
1167                         return NULL;
1168                 }
1169                 buffer = (char*)GlobalLock( hData );
1170                 
1171                 temp_buff = (char*) malloc(strlen(buffer)+1);
1172                 strcpy(temp_buff, buffer);
1173                 
1174                 GlobalUnlock( hData );
1175                 CloseClipboard();
1176                 
1177                 temp_buff[strlen(buffer)] = '\0';
1178                 if (buffer) {
1179                         return (GHOST_TUns8*)temp_buff;
1180                 } else {
1181                         return NULL;
1182                 }
1183         } else {
1184                 return NULL;
1185         }
1186 }
1187
1188 void GHOST_SystemWin32::putClipboard(GHOST_TInt8 *buffer, bool selection) const
1189 {
1190         if(selection) {return;} // for copying the selection, used on X11
1191
1192         if (OpenClipboard(NULL)) {
1193                 HLOCAL clipbuffer;
1194                 char *data;
1195                 
1196                 if (buffer) {
1197                         EmptyClipboard();
1198                         
1199                         clipbuffer = LocalAlloc(LMEM_FIXED,((strlen(buffer)+1)));
1200                         data = (char*)GlobalLock(clipbuffer);
1201
1202                         strcpy(data, (char*)buffer);
1203                         data[strlen(buffer)] = '\0';
1204                         LocalUnlock(clipbuffer);
1205                         SetClipboardData(CF_TEXT,clipbuffer);
1206                 }
1207                 CloseClipboard();
1208         } else {
1209                 return;
1210         }
1211 }