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