Merge branch 'master' into blender2.8
[blender.git] / intern / ghost / intern / GHOST_WindowWin32.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Maarten Gribnau.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file ghost/intern/GHOST_WindowWin32.cpp
29  *  \ingroup GHOST
30  */
31
32 #define _USE_MATH_DEFINES
33
34 #include "GHOST_WindowWin32.h"
35 #include "GHOST_SystemWin32.h"
36 #include "GHOST_DropTargetWin32.h"
37 #include "GHOST_ContextNone.h"
38 #include "utfconv.h"
39 #include "utf_winfunc.h"
40
41 #if defined(WITH_GL_EGL)
42 #  include "GHOST_ContextEGL.h"
43 #else
44 #  include "GHOST_ContextWGL.h"
45 #endif
46 #ifdef WIN32_COMPOSITING
47 #include <Dwmapi.h>
48 #endif
49
50 #include <math.h>
51 #include <string.h>
52 #include <assert.h>
53
54
55
56 const wchar_t *GHOST_WindowWin32::s_windowClassName = L"GHOST_WindowClass";
57 const int GHOST_WindowWin32::s_maxTitleLength = 128;
58
59
60
61
62 /* force NVidia Optimus to used dedicated graphics */
63 extern "C" {
64         __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
65 }
66
67 GHOST_WindowWin32::GHOST_WindowWin32(GHOST_SystemWin32 *system,
68         const STR_String &title,
69         GHOST_TInt32 left,
70         GHOST_TInt32 top,
71         GHOST_TUns32 width,
72         GHOST_TUns32 height,
73         GHOST_TWindowState state,
74         GHOST_TDrawingContextType type,
75         bool wantStereoVisual,
76         bool alphaBackground,
77         GHOST_TUns16 wantNumOfAASamples,
78         GHOST_TEmbedderWindowID parentwindowhwnd,
79         bool is_debug)
80     : GHOST_Window(width, height, state,
81                    wantStereoVisual, false, wantNumOfAASamples),
82       m_inLiveResize(false),
83       m_system(system),
84       m_hDC(0),
85       m_hasMouseCaptured(false),
86       m_hasGrabMouse(false),
87       m_nPressedButtons(0),
88       m_customCursor(0),
89       m_wantAlphaBackground(alphaBackground),
90       m_wintab(NULL),
91       m_tabletData(NULL),
92       m_tablet(0),
93       m_maxPressure(0),
94       m_normal_state(GHOST_kWindowStateNormal),
95           m_user32(NULL),
96       m_parentWindowHwnd(parentwindowhwnd),
97       m_debug_context(is_debug)
98 {
99         if (state != GHOST_kWindowStateFullScreen) {
100                 RECT rect;
101                 MONITORINFO monitor;
102                 GHOST_TUns32 tw, th;
103
104 #ifndef _MSC_VER
105                 int cxsizeframe = GetSystemMetrics(SM_CXSIZEFRAME);
106                 int cysizeframe = GetSystemMetrics(SM_CYSIZEFRAME);
107 #else
108                 // MSVC 2012+ returns bogus values from GetSystemMetrics, bug in Windows
109                 // http://connect.microsoft.com/VisualStudio/feedback/details/753224/regression-getsystemmetrics-delivers-different-values
110                 RECT cxrect = {0, 0, 0, 0};
111                 AdjustWindowRectEx(&cxrect, WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_THICKFRAME | WS_DLGFRAME, FALSE, 0);
112
113                 int cxsizeframe = abs(cxrect.bottom);
114                 int cysizeframe = abs(cxrect.left);
115 #endif
116
117                 width += cxsizeframe * 2;
118                 height += cysizeframe * 2 + GetSystemMetrics(SM_CYCAPTION);
119
120                 rect.left = left;
121                 rect.right = left + width;
122                 rect.top = top;
123                 rect.bottom = top + height;
124
125                 monitor.cbSize = sizeof(monitor);
126                 monitor.dwFlags = 0;
127
128                 // take taskbar into account
129                 GetMonitorInfo(MonitorFromRect(&rect, MONITOR_DEFAULTTONEAREST), &monitor);
130
131                 th = monitor.rcWork.bottom - monitor.rcWork.top;
132                 tw = monitor.rcWork.right - monitor.rcWork.left;
133
134                 if (tw < width) {
135                         width = tw;
136                         left = monitor.rcWork.left;
137                 }
138                 else if (monitor.rcWork.right < left + (int)width)
139                         left = monitor.rcWork.right - width;
140                 else if (left < monitor.rcWork.left)
141                         left = monitor.rcWork.left;
142
143                 if (th < height) {
144                         height = th;
145                         top = monitor.rcWork.top;
146                 }
147                 else if (monitor.rcWork.bottom < top + (int)height)
148                         top = monitor.rcWork.bottom - height;
149                 else if (top < monitor.rcWork.top)
150                         top = monitor.rcWork.top;
151
152                 int wintype = WS_OVERLAPPEDWINDOW;
153                 if (m_parentWindowHwnd != 0) {
154                         wintype = WS_CHILD;
155                         GetWindowRect((HWND)m_parentWindowHwnd, &rect);
156                         left = 0;
157                         top = 0;
158                         width = rect.right - rect.left;
159                         height = rect.bottom - rect.top;
160                 }
161
162                 wchar_t *title_16 = alloc_utf16_from_8((char *)(const char *)title, 0);
163                 m_hWnd = ::CreateWindowW(
164                         s_windowClassName,          // pointer to registered class name
165                         title_16,                   // pointer to window name
166                         wintype,                    // window style
167                         left,                       // horizontal position of window
168                         top,                        // vertical position of window
169                         width,                      // window width
170                         height,                     // window height
171                         (HWND)m_parentWindowHwnd,  // handle to parent or owner window
172                         0,                          // handle to menu or child-window identifier
173                         ::GetModuleHandle(0),       // handle to application instance
174                         0);                         // pointer to window-creation data
175                 free(title_16);
176         }
177         else {
178                 wchar_t *title_16 = alloc_utf16_from_8((char *)(const char *)title, 0);
179                 m_hWnd = ::CreateWindowW(
180                     s_windowClassName,          // pointer to registered class name
181                     title_16,                   // pointer to window name
182                     WS_POPUP | WS_MAXIMIZE,     // window style
183                     left,                       // horizontal position of window
184                     top,                        // vertical position of window
185                     width,                      // window width
186                     height,                     // window height
187                     HWND_DESKTOP,               // handle to parent or owner window
188                     0,                          // handle to menu or child-window identifier
189                     ::GetModuleHandle(0),       // handle to application instance
190                     0);                         // pointer to window-creation data
191                 free(title_16);
192         }
193         if (m_hWnd) {
194                 // Register this window as a droptarget. Requires m_hWnd to be valid.
195                 // Note that OleInitialize(0) has to be called prior to this. Done in GHOST_SystemWin32.
196                 m_dropTarget = new GHOST_DropTargetWin32(this, m_system);
197                 if (m_dropTarget) {
198                         ::RegisterDragDrop(m_hWnd, m_dropTarget);
199                 }
200
201                 // Store a pointer to this class in the window structure
202                 ::SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR) this);
203
204                 // Store the device context
205                 m_hDC = ::GetDC(m_hWnd);
206
207                 GHOST_TSuccess success = setDrawingContextType(type);
208
209                 if (success) {
210                         // Show the window
211                         int nCmdShow;
212                         switch (state) {
213                                 case GHOST_kWindowStateMaximized:
214                                         nCmdShow = SW_SHOWMAXIMIZED;
215                                         break;
216                                 case GHOST_kWindowStateMinimized:
217                                         nCmdShow = SW_SHOWMINIMIZED;
218                                         break;
219                                 case GHOST_kWindowStateNormal:
220                                 default:
221                                         nCmdShow = SW_SHOWNORMAL;
222                                         break;
223                         }
224
225                         ::ShowWindow(m_hWnd, nCmdShow);
226 #ifdef WIN32_COMPOSITING
227                         if (alphaBackground && parentwindowhwnd == 0) {
228
229                                 HRESULT hr = S_OK;
230
231                                 // Create and populate the Blur Behind structure
232                                 DWM_BLURBEHIND bb = { 0 };
233
234                                 // Enable Blur Behind and apply to the entire client area
235                                 bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
236                                 bb.fEnable = true;
237                                 bb.hRgnBlur = CreateRectRgn(0, 0, -1, -1);
238
239                                 // Apply Blur Behind
240                                 hr = DwmEnableBlurBehindWindow(m_hWnd, &bb);
241                                 DeleteObject(bb.hRgnBlur);
242                         }
243 #endif
244                         // Force an initial paint of the window
245                         ::UpdateWindow(m_hWnd);
246                 }
247                 else {
248                         //invalidate the window
249                         ::DestroyWindow(m_hWnd);
250                         m_hWnd = NULL;
251                 }
252         }
253
254         if (parentwindowhwnd != 0) {
255                 RAWINPUTDEVICE device = {0};
256                 device.usUsagePage  = 0x01; /* usUsagePage & usUsage for keyboard*/
257                 device.usUsage      = 0x06; /* http://msdn.microsoft.com/en-us/windows/hardware/gg487473.aspx */
258                 device.dwFlags |= RIDEV_INPUTSINK; // makes WM_INPUT is visible for ghost when has parent window
259                 device.hwndTarget = m_hWnd;
260                 RegisterRawInputDevices(&device, 1, sizeof(device));
261         }
262
263         m_wintab = ::LoadLibrary("Wintab32.dll");
264         if (m_wintab) {
265                 GHOST_WIN32_WTInfo fpWTInfo = (GHOST_WIN32_WTInfo) ::GetProcAddress(m_wintab, "WTInfoA");
266                 GHOST_WIN32_WTOpen fpWTOpen = (GHOST_WIN32_WTOpen) ::GetProcAddress(m_wintab, "WTOpenA");
267
268                 // let's see if we can initialize tablet here
269                 /* check if WinTab available. */
270                 if (fpWTInfo && fpWTInfo(0, 0, NULL)) {
271                         // Now init the tablet
272                         LOGCONTEXT lc;
273                         /* The maximum tablet size, pressure and orientation (tilt) */
274                         AXIS TabletX, TabletY, Pressure, Orientation[3];
275
276                         // Open a Wintab context
277
278                         // Get default context information
279                         fpWTInfo(WTI_DEFCONTEXT, 0, &lc);
280
281                         // Open the context
282                         lc.lcPktData = PACKETDATA;
283                         lc.lcPktMode = PACKETMODE;
284                         lc.lcOptions |= CXO_MESSAGES | CXO_SYSTEM;
285
286                         /* Set the entire tablet as active */
287                         fpWTInfo(WTI_DEVICES, DVC_X, &TabletX);
288                         fpWTInfo(WTI_DEVICES, DVC_Y, &TabletY);
289
290                         /* get the max pressure, to divide into a float */
291                         BOOL pressureSupport = fpWTInfo(WTI_DEVICES, DVC_NPRESSURE, &Pressure);
292                         if (pressureSupport)
293                                 m_maxPressure = Pressure.axMax;
294                         else
295                                 m_maxPressure = 0;
296
297                         /* get the max tilt axes, to divide into floats */
298                         BOOL tiltSupport = fpWTInfo(WTI_DEVICES, DVC_ORIENTATION, &Orientation);
299                         if (tiltSupport) {
300                                 /* does the tablet support azimuth ([0]) and altitude ([1]) */
301                                 if (Orientation[0].axResolution && Orientation[1].axResolution) {
302                                         /* all this assumes the minimum is 0 */
303                                         m_maxAzimuth = Orientation[0].axMax;
304                                         m_maxAltitude = Orientation[1].axMax;
305                                 }
306                                 else {  /* no so dont do tilt stuff */
307                                         m_maxAzimuth = m_maxAltitude = 0;
308                                 }
309                         }
310
311                         if (fpWTOpen) {
312                                 m_tablet = fpWTOpen(m_hWnd, &lc, TRUE);
313                                 if (m_tablet) {
314                                         m_tabletData = new GHOST_TabletData();
315                                         m_tabletData->Active = GHOST_kTabletModeNone;
316                                 }
317                         }
318                 }
319         }
320         CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER, IID_ITaskbarList3, (LPVOID *)&m_Bar);
321 }
322
323
324 GHOST_WindowWin32::~GHOST_WindowWin32()
325 {
326         if (m_Bar) {
327                 m_Bar->SetProgressState(m_hWnd, TBPF_NOPROGRESS);
328                 m_Bar->Release();
329         }
330
331         if (m_wintab) {
332                 GHOST_WIN32_WTClose fpWTClose = (GHOST_WIN32_WTClose) ::GetProcAddress(m_wintab, "WTClose");
333                 if (fpWTClose) {
334                         if (m_tablet)
335                                 fpWTClose(m_tablet);
336                         delete m_tabletData;
337                         m_tabletData = NULL;
338                 }
339         }
340
341         if (m_customCursor) {
342                 DestroyCursor(m_customCursor);
343                 m_customCursor = NULL;
344         }
345
346         if (m_hWnd != NULL && m_hDC != NULL && releaseNativeHandles()) {
347                 ::ReleaseDC(m_hWnd, m_hDC);
348         }
349
350         if (m_hWnd) {
351                 if (m_dropTarget) {
352                         // Disable DragDrop
353                         RevokeDragDrop(m_hWnd);
354                         // Release our reference of the DropTarget and it will delete itself eventually.
355                         m_dropTarget->Release();
356                 }
357                 ::SetWindowLongPtr(m_hWnd, GWLP_USERDATA, NULL);
358                 ::DestroyWindow(m_hWnd);
359                 m_hWnd = 0;
360         }
361 }
362
363 bool GHOST_WindowWin32::getValid() const
364 {
365         return GHOST_Window::getValid() && m_hWnd != 0 && m_hDC != 0;
366 }
367
368 HWND GHOST_WindowWin32::getHWND() const
369 {
370         return m_hWnd;
371 }
372
373 void GHOST_WindowWin32::setTitle(const STR_String &title)
374 {
375         wchar_t *title_16 = alloc_utf16_from_8((char *)(const char *)title, 0);
376         ::SetWindowTextW(m_hWnd, (wchar_t *)title_16);
377         free(title_16);
378 }
379
380
381 void GHOST_WindowWin32::getTitle(STR_String &title) const
382 {
383         char buf[s_maxTitleLength]; /*CHANGE + never used yet*/
384         ::GetWindowText(m_hWnd, buf, s_maxTitleLength);
385         STR_String temp(buf);
386         title = buf;
387 }
388
389
390 void GHOST_WindowWin32::getWindowBounds(GHOST_Rect &bounds) const
391 {
392         RECT rect;
393         ::GetWindowRect(m_hWnd, &rect);
394         bounds.m_b = rect.bottom;
395         bounds.m_l = rect.left;
396         bounds.m_r = rect.right;
397         bounds.m_t = rect.top;
398 }
399
400
401 void GHOST_WindowWin32::getClientBounds(GHOST_Rect &bounds) const
402 {
403         RECT rect;
404         POINT coord;
405         if (!IsIconic(m_hWnd)) {
406                 ::GetClientRect(m_hWnd, &rect);
407
408                 coord.x = rect.left;
409                 coord.y = rect.top;
410                 ::ClientToScreen(m_hWnd, &coord);
411
412                 bounds.m_l = coord.x;
413                 bounds.m_t = coord.y;
414
415                 coord.x = rect.right;
416                 coord.y = rect.bottom;
417                 ::ClientToScreen(m_hWnd, &coord);
418
419                 bounds.m_r = coord.x;
420                 bounds.m_b = coord.y;
421         }
422         else {
423                 bounds.m_b = 0;
424                 bounds.m_l = 0;
425                 bounds.m_r = 0;
426                 bounds.m_t = 0;
427         }
428 }
429
430
431 GHOST_TSuccess GHOST_WindowWin32::setClientWidth(GHOST_TUns32 width)
432 {
433         GHOST_TSuccess success;
434         GHOST_Rect cBnds, wBnds;
435         getClientBounds(cBnds);
436         if (cBnds.getWidth() != (GHOST_TInt32)width) {
437                 getWindowBounds(wBnds);
438                 int cx = wBnds.getWidth() + width - cBnds.getWidth();
439                 int cy = wBnds.getHeight();
440                 success =  ::SetWindowPos(m_hWnd, HWND_TOP, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER) ?
441                           GHOST_kSuccess : GHOST_kFailure;
442         }
443         else {
444                 success = GHOST_kSuccess;
445         }
446         return success;
447 }
448
449
450 GHOST_TSuccess GHOST_WindowWin32::setClientHeight(GHOST_TUns32 height)
451 {
452         GHOST_TSuccess success;
453         GHOST_Rect cBnds, wBnds;
454         getClientBounds(cBnds);
455         if (cBnds.getHeight() != (GHOST_TInt32)height) {
456                 getWindowBounds(wBnds);
457                 int cx = wBnds.getWidth();
458                 int cy = wBnds.getHeight() + height - cBnds.getHeight();
459                 success = ::SetWindowPos(m_hWnd, HWND_TOP, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER) ?
460                           GHOST_kSuccess : GHOST_kFailure;
461         }
462         else {
463                 success = GHOST_kSuccess;
464         }
465         return success;
466 }
467
468
469 GHOST_TSuccess GHOST_WindowWin32::setClientSize(GHOST_TUns32 width, GHOST_TUns32 height)
470 {
471         GHOST_TSuccess success;
472         GHOST_Rect cBnds, wBnds;
473         getClientBounds(cBnds);
474         if ((cBnds.getWidth() != (GHOST_TInt32)width) || (cBnds.getHeight() != (GHOST_TInt32)height)) {
475                 getWindowBounds(wBnds);
476                 int cx = wBnds.getWidth() + width - cBnds.getWidth();
477                 int cy = wBnds.getHeight() + height - cBnds.getHeight();
478                 success = ::SetWindowPos(m_hWnd, HWND_TOP, 0, 0, cx, cy, SWP_NOMOVE | SWP_NOZORDER) ?
479                           GHOST_kSuccess : GHOST_kFailure;
480         }
481         else {
482                 success = GHOST_kSuccess;
483         }
484         return success;
485 }
486
487
488 GHOST_TWindowState GHOST_WindowWin32::getState() const
489 {
490         GHOST_TWindowState state;
491
492         // XXX 27.04.2011
493         // we need to find a way to combine parented windows + resizing if we simply set the
494         // state as GHOST_kWindowStateEmbedded we will need to check for them somewhere else.
495         // It's also strange that in Windows is the only platform we need to make this separation.
496         if (m_parentWindowHwnd != 0) {
497                 state = GHOST_kWindowStateEmbedded;
498                 return state;
499         }
500
501         if (::IsIconic(m_hWnd)) {
502                 state = GHOST_kWindowStateMinimized;
503         }
504         else if (::IsZoomed(m_hWnd)) {
505                 LONG_PTR result = ::GetWindowLongPtr(m_hWnd, GWL_STYLE);
506                 if ((result & (WS_POPUP | WS_MAXIMIZE)) != (WS_POPUP | WS_MAXIMIZE))
507                         state = GHOST_kWindowStateMaximized;
508                 else
509                         state = GHOST_kWindowStateFullScreen;
510         }
511         else {
512                 state = GHOST_kWindowStateNormal;
513         }
514         return state;
515 }
516
517
518 void GHOST_WindowWin32::screenToClient(
519         GHOST_TInt32 inX, GHOST_TInt32 inY,
520         GHOST_TInt32 &outX, GHOST_TInt32 &outY) const
521 {
522         POINT point = {inX, inY};
523         ::ScreenToClient(m_hWnd, &point);
524         outX = point.x;
525         outY = point.y;
526 }
527
528
529 void GHOST_WindowWin32::clientToScreen(
530         GHOST_TInt32 inX, GHOST_TInt32 inY,
531         GHOST_TInt32 &outX, GHOST_TInt32 &outY) const
532 {
533         POINT point = {inX, inY};
534         ::ClientToScreen(m_hWnd, &point);
535         outX = point.x;
536         outY = point.y;
537 }
538
539
540 GHOST_TSuccess GHOST_WindowWin32::setState(GHOST_TWindowState state)
541 {
542         GHOST_TWindowState curstate = getState();
543         WINDOWPLACEMENT wp;
544         wp.length = sizeof(WINDOWPLACEMENT);
545         ::GetWindowPlacement(m_hWnd, &wp);
546
547         if (state == GHOST_kWindowStateNormal)
548                 state = m_normal_state;
549
550         switch (state) {
551                 case GHOST_kWindowStateMinimized:
552                         wp.showCmd = SW_SHOWMINIMIZED;
553                         break;
554                 case GHOST_kWindowStateMaximized:
555                         wp.showCmd = SW_SHOWMAXIMIZED;
556                         ::SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);
557                         break;
558                 case GHOST_kWindowStateFullScreen:
559                         if (curstate != state && curstate != GHOST_kWindowStateMinimized)
560                                 m_normal_state = curstate;
561                         wp.showCmd = SW_SHOWMAXIMIZED;
562                         wp.ptMaxPosition.x = 0;
563                         wp.ptMaxPosition.y = 0;
564                         ::SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_POPUP | WS_MAXIMIZE);
565                         break;
566                 case GHOST_kWindowStateEmbedded:
567                         ::SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_CHILD);
568                         break;
569                 case GHOST_kWindowStateNormal:
570                 default:
571                         wp.showCmd = SW_SHOWNORMAL;
572                         ::SetWindowLongPtr(m_hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW);
573                         break;
574         }
575         /* Clears window cache for SetWindowLongPtr */
576         ::SetWindowPos(m_hWnd, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
577
578         return ::SetWindowPlacement(m_hWnd, &wp) == TRUE ? GHOST_kSuccess : GHOST_kFailure;
579 }
580
581
582 GHOST_TSuccess GHOST_WindowWin32::setOrder(GHOST_TWindowOrder order)
583 {
584         HWND hWndInsertAfter, hWndToRaise;
585
586         if (order == GHOST_kWindowOrderBottom) {
587                 hWndInsertAfter = HWND_BOTTOM;
588                 hWndToRaise = ::GetWindow(m_hWnd, GW_HWNDNEXT); /* the window to raise */
589         }
590         else {
591                 hWndInsertAfter = HWND_TOP;
592                 hWndToRaise = NULL;
593         }
594
595         if (::SetWindowPos(m_hWnd, hWndInsertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) == FALSE) {
596                 return GHOST_kFailure;
597         }
598
599         if (hWndToRaise && ::SetWindowPos(hWndToRaise, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE) == FALSE) {
600                 return GHOST_kFailure;
601         }
602         return GHOST_kSuccess;
603 }
604
605
606 GHOST_TSuccess GHOST_WindowWin32::invalidate()
607 {
608         GHOST_TSuccess success;
609         if (m_hWnd) {
610                 success = ::InvalidateRect(m_hWnd, 0, FALSE) != 0 ? GHOST_kSuccess : GHOST_kFailure;
611         }
612         else {
613                 success = GHOST_kFailure;
614         }
615         return success;
616 }
617
618
619 GHOST_Context *GHOST_WindowWin32::newDrawingContext(GHOST_TDrawingContextType type)
620 {
621         if (type == GHOST_kDrawingContextTypeOpenGL) {
622                 GHOST_Context *context;
623
624 #if defined(WITH_GL_PROFILE_CORE)
625                 /* - AMD and Intel give us exactly this version
626                  * - NVIDIA gives at least this version <-- desired behavior
627                  * So we ask for 4.5, 4.4 ... 3.3 in descending order to get the best version on the user's system. */
628                 for (int minor = 5; minor >= 0; --minor) {
629                         context = new GHOST_ContextWGL(
630                             m_wantStereoVisual,
631                             m_wantAlphaBackground,
632                             m_wantNumOfAASamples,
633                             m_hWnd,
634                             m_hDC,
635                             WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
636                             4, minor,
637                             (m_debug_context ? WGL_CONTEXT_DEBUG_BIT_ARB : 0),
638                             GHOST_OPENGL_WGL_RESET_NOTIFICATION_STRATEGY);
639
640                         if (context->initializeDrawingContext()) {
641                                 return context;
642                         }
643                         else {
644                                 delete context;
645                         }
646                 }
647                 context = new GHOST_ContextWGL(
648                     m_wantStereoVisual,
649                     m_wantAlphaBackground,
650                     m_wantNumOfAASamples,
651                     m_hWnd,
652                     m_hDC,
653                     WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
654                     3, 3,
655                     (m_debug_context ? WGL_CONTEXT_DEBUG_BIT_ARB : 0),
656                     GHOST_OPENGL_WGL_RESET_NOTIFICATION_STRATEGY);
657
658                 if (context->initializeDrawingContext()) {
659                         return context;
660                 }
661                 else {
662                         MessageBox(
663                                 m_hWnd,
664                                 "Blender requires a graphics driver with at least OpenGL 3.3 support.\n\n"
665                                 "The program will now close.",
666                                 "Blender - Unsupported Graphics Driver!",
667                                 MB_OK | MB_ICONERROR);
668                         delete context;
669                         exit(0);
670                 }
671
672 #elif defined(WITH_GL_PROFILE_COMPAT)
673                 // ask for 2.1 context, driver gives any GL version >= 2.1 (hopefully the latest compatibility profile)
674                 // 2.1 ignores the profile bit & is incompatible with core profile
675                 context = new GHOST_ContextWGL(
676                         m_wantStereoVisual,
677                         m_wantAlphaBackground,
678                         m_wantNumOfAASamples,
679                         m_hWnd,
680                         m_hDC,
681                         0, // no profile bit
682                         2, 1,
683                         (m_debug_context ? WGL_CONTEXT_DEBUG_BIT_ARB : 0),
684                         GHOST_OPENGL_WGL_RESET_NOTIFICATION_STRATEGY);
685
686                 if (context->initializeDrawingContext()) {
687                         return context;
688                 }
689                 else {
690                         delete context;
691                 }
692 #else
693 #  error // must specify either core or compat at build time
694 #endif
695         }
696
697         return NULL;
698 }
699
700 void GHOST_WindowWin32::lostMouseCapture()
701 {
702         if (m_hasMouseCaptured) {
703                 m_hasGrabMouse = false;
704                 m_nPressedButtons = 0;
705                 m_hasMouseCaptured = false;
706         }
707 }
708
709 void GHOST_WindowWin32::registerMouseClickEvent(int press)
710 {
711
712         switch (press) {
713                 case 0: m_nPressedButtons++;    break;
714                 case 1: if (m_nPressedButtons) m_nPressedButtons--; break;
715                 case 2: m_hasGrabMouse = true;    break;
716                 case 3: m_hasGrabMouse = false;   break;
717         }
718
719         if (!m_nPressedButtons && !m_hasGrabMouse && m_hasMouseCaptured) {
720                 ::ReleaseCapture();
721                 m_hasMouseCaptured = false;
722         }
723         else if ((m_nPressedButtons || m_hasGrabMouse) && !m_hasMouseCaptured) {
724                 ::SetCapture(m_hWnd);
725                 m_hasMouseCaptured = true;
726
727         }
728 }
729
730
731 void GHOST_WindowWin32::loadCursor(bool visible, GHOST_TStandardCursor cursor) const
732 {
733         if (!visible) {
734                 while (::ShowCursor(FALSE) >= 0) ;
735         }
736         else {
737                 while (::ShowCursor(TRUE) < 0) ;
738         }
739
740         if (cursor == GHOST_kStandardCursorCustom && m_customCursor) {
741                 ::SetCursor(m_customCursor);
742         }
743         else {
744                 // Convert GHOST cursor to Windows OEM cursor
745                 bool success = true;
746                 LPCSTR id;
747                 switch (cursor) {
748                         case GHOST_kStandardCursorDefault:              id = IDC_ARROW;     break;
749                         case GHOST_kStandardCursorRightArrow:           id = IDC_ARROW;     break;
750                         case GHOST_kStandardCursorLeftArrow:            id = IDC_ARROW;     break;
751                         case GHOST_kStandardCursorInfo:                 id = IDC_SIZEALL;   break;  // Four-pointed arrow pointing north, south, east, and west
752                         case GHOST_kStandardCursorDestroy:              id = IDC_NO;        break;  // Slashed circle
753                         case GHOST_kStandardCursorHelp:                 id = IDC_HELP;      break;  // Arrow and question mark
754                         case GHOST_kStandardCursorCycle:                id = IDC_NO;        break;  // Slashed circle
755                         case GHOST_kStandardCursorSpray:                id = IDC_SIZEALL;   break;  // Four-pointed arrow pointing north, south, east, and west
756                         case GHOST_kStandardCursorWait:                 id = IDC_WAIT;      break;  // Hourglass
757                         case GHOST_kStandardCursorText:                 id = IDC_IBEAM;     break;  // I-beam
758                         case GHOST_kStandardCursorCrosshair:            id = IDC_CROSS;     break;  // Crosshair
759                         case GHOST_kStandardCursorUpDown:               id = IDC_SIZENS;    break;  // Double-pointed arrow pointing north and south
760                         case GHOST_kStandardCursorLeftRight:            id = IDC_SIZEWE;    break;  // Double-pointed arrow pointing west and east
761                         case GHOST_kStandardCursorTopSide:              id = IDC_UPARROW;   break;  // Vertical arrow
762                         case GHOST_kStandardCursorBottomSide:           id = IDC_SIZENS;    break;
763                         case GHOST_kStandardCursorLeftSide:             id = IDC_SIZEWE;    break;
764                         case GHOST_kStandardCursorTopLeftCorner:        id = IDC_SIZENWSE;  break;
765                         case GHOST_kStandardCursorTopRightCorner:       id = IDC_SIZENESW;  break;
766                         case GHOST_kStandardCursorBottomRightCorner:    id = IDC_SIZENWSE;  break;
767                         case GHOST_kStandardCursorBottomLeftCorner:     id = IDC_SIZENESW;  break;
768                         case GHOST_kStandardCursorPencil:               id = IDC_ARROW;     break;
769                         case GHOST_kStandardCursorCopy:                 id = IDC_ARROW;     break;
770                         default:
771                                 success = false;
772                 }
773
774                 if (success) {
775                         ::SetCursor(::LoadCursor(0, id));
776                 }
777         }
778 }
779
780 GHOST_TSuccess GHOST_WindowWin32::setWindowCursorVisibility(bool visible)
781 {
782         if (::GetForegroundWindow() == m_hWnd) {
783                 loadCursor(visible, getCursorShape());
784         }
785
786         return GHOST_kSuccess;
787 }
788
789 GHOST_TSuccess GHOST_WindowWin32::setWindowCursorGrab(GHOST_TGrabCursorMode mode)
790 {
791         if (mode != GHOST_kGrabDisable) {
792                 if (mode != GHOST_kGrabNormal) {
793                         m_system->getCursorPosition(m_cursorGrabInitPos[0], m_cursorGrabInitPos[1]);
794                         setCursorGrabAccum(0, 0);
795
796                         if (mode == GHOST_kGrabHide)
797                                 setWindowCursorVisibility(false);
798                 }
799                 registerMouseClickEvent(2);
800         }
801         else {
802                 if (m_cursorGrab == GHOST_kGrabHide) {
803                         m_system->setCursorPosition(m_cursorGrabInitPos[0], m_cursorGrabInitPos[1]);
804                         setWindowCursorVisibility(true);
805                 }
806                 if (m_cursorGrab != GHOST_kGrabNormal) {
807                         /* use to generate a mouse move event, otherwise the last event
808                          * blender gets can be outside the screen causing menus not to show
809                          * properly unless the user moves the mouse */
810                         GHOST_TInt32 pos[2];
811                         m_system->getCursorPosition(pos[0], pos[1]);
812                         m_system->setCursorPosition(pos[0], pos[1]);
813                 }
814
815                 /* Almost works without but important otherwise the mouse GHOST location can be incorrect on exit */
816                 setCursorGrabAccum(0, 0);
817                 m_cursorGrabBounds.m_l = m_cursorGrabBounds.m_r = -1; /* disable */
818                 registerMouseClickEvent(3);
819         }
820
821         return GHOST_kSuccess;
822 }
823
824 GHOST_TSuccess GHOST_WindowWin32::setWindowCursorShape(GHOST_TStandardCursor cursorShape)
825 {
826         if (m_customCursor) {
827                 DestroyCursor(m_customCursor);
828                 m_customCursor = NULL;
829         }
830
831         if (::GetForegroundWindow() == m_hWnd) {
832                 loadCursor(getCursorVisibility(), cursorShape);
833         }
834
835         return GHOST_kSuccess;
836 }
837
838 void GHOST_WindowWin32::processWin32TabletInitEvent()
839 {
840         if (m_wintab && m_tabletData) {
841                 GHOST_WIN32_WTInfo fpWTInfo = (GHOST_WIN32_WTInfo) ::GetProcAddress(m_wintab, "WTInfoA");
842
843                 // let's see if we can initialize tablet here
844                 /* check if WinTab available. */
845                 if (fpWTInfo) {
846                         AXIS Pressure, Orientation[3]; /* The maximum tablet size */
847
848                         BOOL pressureSupport = fpWTInfo(WTI_DEVICES, DVC_NPRESSURE, &Pressure);
849                         if (pressureSupport)
850                                 m_maxPressure = Pressure.axMax;
851                         else
852                                 m_maxPressure = 0;
853
854                         BOOL tiltSupport = fpWTInfo(WTI_DEVICES, DVC_ORIENTATION, &Orientation);
855                         if (tiltSupport) {
856                                 /* does the tablet support azimuth ([0]) and altitude ([1]) */
857                                 if (Orientation[0].axResolution && Orientation[1].axResolution) {
858                                         m_maxAzimuth = Orientation[0].axMax;
859                                         m_maxAltitude = Orientation[1].axMax;
860                                 }
861                                 else {  /* no so dont do tilt stuff */
862                                         m_maxAzimuth = m_maxAltitude = 0;
863                                 }
864                         }
865
866                         m_tabletData->Active = GHOST_kTabletModeNone;
867                 }
868         }
869 }
870
871 void GHOST_WindowWin32::processWin32TabletEvent(WPARAM wParam, LPARAM lParam)
872 {
873         PACKET pkt;
874         if (m_wintab) {
875                 GHOST_WIN32_WTPacket fpWTPacket = (GHOST_WIN32_WTPacket) ::GetProcAddress(m_wintab, "WTPacket");
876                 if (fpWTPacket) {
877                         if (fpWTPacket((HCTX)lParam, wParam, &pkt)) {
878                                 if (m_tabletData) {
879                                         switch (pkt.pkCursor % 3) { /* % 3 for multiple devices ("DualTrack") */
880                                                 case 0:
881                                                         m_tabletData->Active = GHOST_kTabletModeNone; /* puck - not yet supported */
882                                                         break;
883                                                 case 1:
884                                                         m_tabletData->Active = GHOST_kTabletModeStylus; /* stylus */
885                                                         break;
886                                                 case 2:
887                                                         m_tabletData->Active = GHOST_kTabletModeEraser; /* eraser */
888                                                         break;
889                                         }
890                                         if (m_maxPressure > 0) {
891                                                 m_tabletData->Pressure = (float)pkt.pkNormalPressure / (float)m_maxPressure;
892                                         }
893                                         else {
894                                                 m_tabletData->Pressure = 1.0f;
895                                         }
896
897                                         if ((m_maxAzimuth > 0) && (m_maxAltitude > 0)) {
898                                                 ORIENTATION ort = pkt.pkOrientation;
899                                                 float vecLen;
900                                                 float altRad, azmRad;   /* in radians */
901
902                                                 /*
903                                                  * from the wintab spec:
904                                                  * orAzimuth    Specifies the clockwise rotation of the
905                                                  * cursor about the z axis through a full circular range.
906                                                  *
907                                                  * orAltitude   Specifies the angle with the x-y plane
908                                                  * through a signed, semicircular range.  Positive values
909                                                  * specify an angle upward toward the positive z axis;
910                                                  * negative values specify an angle downward toward the negative z axis.
911                                                  *
912                                                  * wintab.h defines .orAltitude as a UINT but documents .orAltitude
913                                                  * as positive for upward angles and negative for downward angles.
914                                                  * WACOM uses negative altitude values to show that the pen is inverted;
915                                                  * therefore we cast .orAltitude as an (int) and then use the absolute value.
916                                                  */
917
918                                                 /* convert raw fixed point data to radians */
919                                                 altRad = (float)((fabs((float)ort.orAltitude) / (float)m_maxAltitude) * M_PI / 2.0);
920                                                 azmRad = (float)(((float)ort.orAzimuth / (float)m_maxAzimuth) * M_PI * 2.0);
921
922                                                 /* find length of the stylus' projected vector on the XY plane */
923                                                 vecLen = cos(altRad);
924
925                                                 /* from there calculate X and Y components based on azimuth */
926                                                 m_tabletData->Xtilt = sin(azmRad) * vecLen;
927                                                 m_tabletData->Ytilt = (float)(sin(M_PI / 2.0 - azmRad) * vecLen);
928
929                                         }
930                                         else {
931                                                 m_tabletData->Xtilt = 0.0f;
932                                                 m_tabletData->Ytilt = 0.0f;
933                                         }
934                                 }
935                         }
936                 }
937         }
938 }
939
940 void GHOST_WindowWin32::bringTabletContextToFront()
941 {
942         if (m_wintab) {
943                 GHOST_WIN32_WTOverlap fpWTOverlap = (GHOST_WIN32_WTOverlap) ::GetProcAddress(m_wintab, "WTOverlap");
944                 if (fpWTOverlap) {
945                         fpWTOverlap(m_tablet, TRUE);
946                 }
947         }
948 }
949
950 GHOST_TUns16 GHOST_WindowWin32::getDPIHint()
951 {
952         if (!m_user32) {
953                 m_user32 = ::LoadLibrary("user32.dll");
954         }
955
956         if (m_user32) {
957                 GHOST_WIN32_GetDpiForWindow fpGetDpiForWindow = (GHOST_WIN32_GetDpiForWindow) ::GetProcAddress(m_user32, "GetDpiForWindow");
958
959                 if (fpGetDpiForWindow) {
960                         return fpGetDpiForWindow(this->m_hWnd);
961                 }
962         }
963
964         return USER_DEFAULT_SCREEN_DPI;
965 }
966
967 /** Reverse the bits in a GHOST_TUns8 */
968 static GHOST_TUns8 uns8ReverseBits(GHOST_TUns8 ch)
969 {
970         ch = ((ch >> 1) & 0x55) | ((ch << 1) & 0xAA);
971         ch = ((ch >> 2) & 0x33) | ((ch << 2) & 0xCC);
972         ch = ((ch >> 4) & 0x0F) | ((ch << 4) & 0xF0);
973         return ch;
974 }
975
976 #if 0  /* UNUSED */
977 /** Reverse the bits in a GHOST_TUns16 */
978 static GHOST_TUns16 uns16ReverseBits(GHOST_TUns16 shrt)
979 {
980         shrt = ((shrt >> 1) & 0x5555) | ((shrt << 1) & 0xAAAA);
981         shrt = ((shrt >> 2) & 0x3333) | ((shrt << 2) & 0xCCCC);
982         shrt = ((shrt >> 4) & 0x0F0F) | ((shrt << 4) & 0xF0F0);
983         shrt = ((shrt >> 8) & 0x00FF) | ((shrt << 8) & 0xFF00);
984         return shrt;
985 }
986 #endif
987 GHOST_TSuccess GHOST_WindowWin32::setWindowCustomCursorShape(
988         GHOST_TUns8 bitmap[16][2],
989         GHOST_TUns8 mask[16][2],
990         int hotX, int hotY)
991 {
992         return setWindowCustomCursorShape((GHOST_TUns8 *)bitmap, (GHOST_TUns8 *)mask,
993                                           16, 16, hotX, hotY, 0, 1);
994 }
995
996 GHOST_TSuccess GHOST_WindowWin32::setWindowCustomCursorShape(
997         GHOST_TUns8 *bitmap,
998         GHOST_TUns8 *mask, int sizeX, int sizeY, int hotX, int hotY,
999         int fg_color, int bg_color)
1000 {
1001         GHOST_TUns32 andData[32];
1002         GHOST_TUns32 xorData[32];
1003         GHOST_TUns32 fullBitRow, fullMaskRow;
1004         int x, y, cols;
1005
1006         cols = sizeX / 8; /* Num of whole bytes per row (width of bm/mask) */
1007         if (sizeX % 8) cols++;
1008
1009         if (m_customCursor) {
1010                 DestroyCursor(m_customCursor);
1011                 m_customCursor = NULL;
1012         }
1013
1014         memset(&andData, 0xFF, sizeof(andData));
1015         memset(&xorData, 0, sizeof(xorData));
1016
1017         for (y = 0; y < sizeY; y++) {
1018                 fullBitRow = 0;
1019                 fullMaskRow = 0;
1020                 for (x = cols - 1; x >= 0; x--) {
1021                         fullBitRow <<= 8;
1022                         fullMaskRow <<= 8;
1023                         fullBitRow  |= uns8ReverseBits(bitmap[cols * y + x]);
1024                         fullMaskRow |= uns8ReverseBits(mask[cols * y + x]);
1025                 }
1026                 xorData[y] = fullBitRow & fullMaskRow;
1027                 andData[y] = ~fullMaskRow;
1028         }
1029
1030         m_customCursor = ::CreateCursor(::GetModuleHandle(0), hotX, hotY, 32, 32, andData, xorData);
1031         if (!m_customCursor) {
1032                 return GHOST_kFailure;
1033         }
1034
1035         if (::GetForegroundWindow() == m_hWnd) {
1036                 loadCursor(getCursorVisibility(), GHOST_kStandardCursorCustom);
1037         }
1038
1039         return GHOST_kSuccess;
1040 }
1041
1042
1043 GHOST_TSuccess GHOST_WindowWin32::setProgressBar(float progress)
1044 {
1045         /*SetProgressValue sets state to TBPF_NORMAL automaticly*/
1046         if (m_Bar && S_OK == m_Bar->SetProgressValue(m_hWnd, 10000 * progress, 10000))
1047                 return GHOST_kSuccess;
1048
1049         return GHOST_kFailure;
1050 }
1051
1052 GHOST_TSuccess GHOST_WindowWin32::endProgressBar()
1053 {
1054         if (m_Bar && S_OK == m_Bar->SetProgressState(m_hWnd, TBPF_NOPROGRESS))
1055                 return GHOST_kSuccess;
1056
1057         return GHOST_kFailure;
1058 }
1059
1060
1061 #ifdef WITH_INPUT_IME
1062 void GHOST_WindowWin32::beginIME(GHOST_TInt32 x, GHOST_TInt32 y, GHOST_TInt32 w, GHOST_TInt32 h, int completed)
1063 {
1064         m_imeImput.BeginIME(m_hWnd, GHOST_Rect(x, y - h, x, y), (bool)completed);
1065 }
1066
1067
1068 void GHOST_WindowWin32::endIME()
1069 {
1070         m_imeImput.EndIME(m_hWnd);
1071 }
1072 #endif /* WITH_INPUT_IME */