The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5387 lines
192KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wcast-function-type")
  21. #undef GetSystemMetrics // multimon overrides this for some reason and causes a mess..
  22. // these are in the windows SDK, but need to be repeated here for GCC..
  23. #ifndef GET_APPCOMMAND_LPARAM
  24. #define GET_APPCOMMAND_LPARAM(lParam) ((short) (HIWORD (lParam) & ~FAPPCOMMAND_MASK))
  25. #define FAPPCOMMAND_MASK 0xF000
  26. #define APPCOMMAND_MEDIA_NEXTTRACK 11
  27. #define APPCOMMAND_MEDIA_PREVIOUSTRACK 12
  28. #define APPCOMMAND_MEDIA_STOP 13
  29. #define APPCOMMAND_MEDIA_PLAY_PAUSE 14
  30. #endif
  31. #ifndef WM_APPCOMMAND
  32. #define WM_APPCOMMAND 0x0319
  33. #endif
  34. void juce_repeatLastProcessPriority();
  35. using CheckEventBlockedByModalComps = bool (*) (const MSG&);
  36. extern CheckEventBlockedByModalComps isEventBlockedByModalComps;
  37. static bool shouldDeactivateTitleBar = true;
  38. void* getUser32Function (const char*);
  39. #if JUCE_DEBUG
  40. int numActiveScopedDpiAwarenessDisablers = 0;
  41. static bool isInScopedDPIAwarenessDisabler() { return numActiveScopedDpiAwarenessDisablers > 0; }
  42. extern HWND juce_messageWindowHandle;
  43. #endif
  44. struct ScopedDeviceContext
  45. {
  46. explicit ScopedDeviceContext (HWND h)
  47. : hwnd (h), dc (GetDC (hwnd))
  48. {
  49. }
  50. ~ScopedDeviceContext()
  51. {
  52. ReleaseDC (hwnd, dc);
  53. }
  54. HWND hwnd;
  55. HDC dc;
  56. JUCE_DECLARE_NON_COPYABLE (ScopedDeviceContext)
  57. JUCE_DECLARE_NON_MOVEABLE (ScopedDeviceContext)
  58. };
  59. //==============================================================================
  60. #ifndef WM_TOUCH
  61. enum
  62. {
  63. WM_TOUCH = 0x0240,
  64. TOUCHEVENTF_MOVE = 0x0001,
  65. TOUCHEVENTF_DOWN = 0x0002,
  66. TOUCHEVENTF_UP = 0x0004
  67. };
  68. typedef HANDLE HTOUCHINPUT;
  69. typedef HANDLE HGESTUREINFO;
  70. struct TOUCHINPUT
  71. {
  72. LONG x;
  73. LONG y;
  74. HANDLE hSource;
  75. DWORD dwID;
  76. DWORD dwFlags;
  77. DWORD dwMask;
  78. DWORD dwTime;
  79. ULONG_PTR dwExtraInfo;
  80. DWORD cxContact;
  81. DWORD cyContact;
  82. };
  83. struct GESTUREINFO
  84. {
  85. UINT cbSize;
  86. DWORD dwFlags;
  87. DWORD dwID;
  88. HWND hwndTarget;
  89. POINTS ptsLocation;
  90. DWORD dwInstanceID;
  91. DWORD dwSequenceID;
  92. ULONGLONG ullArguments;
  93. UINT cbExtraArgs;
  94. };
  95. #endif
  96. #ifndef WM_NCPOINTERUPDATE
  97. enum
  98. {
  99. WM_NCPOINTERUPDATE = 0x241,
  100. WM_NCPOINTERDOWN = 0x242,
  101. WM_NCPOINTERUP = 0x243,
  102. WM_POINTERUPDATE = 0x245,
  103. WM_POINTERDOWN = 0x246,
  104. WM_POINTERUP = 0x247,
  105. WM_POINTERENTER = 0x249,
  106. WM_POINTERLEAVE = 0x24A,
  107. WM_POINTERACTIVATE = 0x24B,
  108. WM_POINTERCAPTURECHANGED = 0x24C,
  109. WM_TOUCHHITTESTING = 0x24D,
  110. WM_POINTERWHEEL = 0x24E,
  111. WM_POINTERHWHEEL = 0x24F,
  112. WM_POINTERHITTEST = 0x250
  113. };
  114. enum
  115. {
  116. PT_TOUCH = 0x00000002,
  117. PT_PEN = 0x00000003
  118. };
  119. enum POINTER_BUTTON_CHANGE_TYPE
  120. {
  121. POINTER_CHANGE_NONE,
  122. POINTER_CHANGE_FIRSTBUTTON_DOWN,
  123. POINTER_CHANGE_FIRSTBUTTON_UP,
  124. POINTER_CHANGE_SECONDBUTTON_DOWN,
  125. POINTER_CHANGE_SECONDBUTTON_UP,
  126. POINTER_CHANGE_THIRDBUTTON_DOWN,
  127. POINTER_CHANGE_THIRDBUTTON_UP,
  128. POINTER_CHANGE_FOURTHBUTTON_DOWN,
  129. POINTER_CHANGE_FOURTHBUTTON_UP,
  130. POINTER_CHANGE_FIFTHBUTTON_DOWN,
  131. POINTER_CHANGE_FIFTHBUTTON_UP
  132. };
  133. enum
  134. {
  135. PEN_MASK_NONE = 0x00000000,
  136. PEN_MASK_PRESSURE = 0x00000001,
  137. PEN_MASK_ROTATION = 0x00000002,
  138. PEN_MASK_TILT_X = 0x00000004,
  139. PEN_MASK_TILT_Y = 0x00000008
  140. };
  141. enum
  142. {
  143. TOUCH_MASK_NONE = 0x00000000,
  144. TOUCH_MASK_CONTACTAREA = 0x00000001,
  145. TOUCH_MASK_ORIENTATION = 0x00000002,
  146. TOUCH_MASK_PRESSURE = 0x00000004
  147. };
  148. enum
  149. {
  150. POINTER_FLAG_NONE = 0x00000000,
  151. POINTER_FLAG_NEW = 0x00000001,
  152. POINTER_FLAG_INRANGE = 0x00000002,
  153. POINTER_FLAG_INCONTACT = 0x00000004,
  154. POINTER_FLAG_FIRSTBUTTON = 0x00000010,
  155. POINTER_FLAG_SECONDBUTTON = 0x00000020,
  156. POINTER_FLAG_THIRDBUTTON = 0x00000040,
  157. POINTER_FLAG_FOURTHBUTTON = 0x00000080,
  158. POINTER_FLAG_FIFTHBUTTON = 0x00000100,
  159. POINTER_FLAG_PRIMARY = 0x00002000,
  160. POINTER_FLAG_CONFIDENCE = 0x00004000,
  161. POINTER_FLAG_CANCELED = 0x00008000,
  162. POINTER_FLAG_DOWN = 0x00010000,
  163. POINTER_FLAG_UPDATE = 0x00020000,
  164. POINTER_FLAG_UP = 0x00040000,
  165. POINTER_FLAG_WHEEL = 0x00080000,
  166. POINTER_FLAG_HWHEEL = 0x00100000,
  167. POINTER_FLAG_CAPTURECHANGED = 0x00200000,
  168. POINTER_FLAG_HASTRANSFORM = 0x00400000
  169. };
  170. typedef DWORD POINTER_INPUT_TYPE;
  171. typedef UINT32 POINTER_FLAGS;
  172. typedef UINT32 PEN_FLAGS;
  173. typedef UINT32 PEN_MASK;
  174. typedef UINT32 TOUCH_FLAGS;
  175. typedef UINT32 TOUCH_MASK;
  176. struct POINTER_INFO
  177. {
  178. POINTER_INPUT_TYPE pointerType;
  179. UINT32 pointerId;
  180. UINT32 frameId;
  181. POINTER_FLAGS pointerFlags;
  182. HANDLE sourceDevice;
  183. HWND hwndTarget;
  184. POINT ptPixelLocation;
  185. POINT ptHimetricLocation;
  186. POINT ptPixelLocationRaw;
  187. POINT ptHimetricLocationRaw;
  188. DWORD dwTime;
  189. UINT32 historyCount;
  190. INT32 InputData;
  191. DWORD dwKeyStates;
  192. UINT64 PerformanceCount;
  193. POINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
  194. };
  195. struct POINTER_TOUCH_INFO
  196. {
  197. POINTER_INFO pointerInfo;
  198. TOUCH_FLAGS touchFlags;
  199. TOUCH_MASK touchMask;
  200. RECT rcContact;
  201. RECT rcContactRaw;
  202. UINT32 orientation;
  203. UINT32 pressure;
  204. };
  205. struct POINTER_PEN_INFO
  206. {
  207. POINTER_INFO pointerInfo;
  208. PEN_FLAGS penFlags;
  209. PEN_MASK penMask;
  210. UINT32 pressure;
  211. UINT32 rotation;
  212. INT32 tiltX;
  213. INT32 tiltY;
  214. };
  215. #define GET_POINTERID_WPARAM(wParam) (LOWORD(wParam))
  216. #endif
  217. #ifndef MONITOR_DPI_TYPE
  218. enum Monitor_DPI_Type
  219. {
  220. MDT_Effective_DPI = 0,
  221. MDT_Angular_DPI = 1,
  222. MDT_Raw_DPI = 2,
  223. MDT_Default = MDT_Effective_DPI
  224. };
  225. #endif
  226. #ifndef DPI_AWARENESS
  227. enum DPI_Awareness
  228. {
  229. DPI_Awareness_Invalid = -1,
  230. DPI_Awareness_Unaware = 0,
  231. DPI_Awareness_System_Aware = 1,
  232. DPI_Awareness_Per_Monitor_Aware = 2
  233. };
  234. #endif
  235. #ifndef USER_DEFAULT_SCREEN_DPI
  236. #define USER_DEFAULT_SCREEN_DPI 96
  237. #endif
  238. #ifndef _DPI_AWARENESS_CONTEXTS_
  239. typedef HANDLE DPI_AWARENESS_CONTEXT;
  240. #define DPI_AWARENESS_CONTEXT_UNAWARE ((DPI_AWARENESS_CONTEXT) - 1)
  241. #define DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ((DPI_AWARENESS_CONTEXT) - 2)
  242. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ((DPI_AWARENESS_CONTEXT) - 3)
  243. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT) - 4)
  244. #endif
  245. // Some versions of the Windows 10 SDK define _DPI_AWARENESS_CONTEXTS_ but not
  246. // DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
  247. #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
  248. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((DPI_AWARENESS_CONTEXT) - 4)
  249. #endif
  250. //==============================================================================
  251. using RegisterTouchWindowFunc = BOOL (WINAPI*) (HWND, ULONG);
  252. using GetTouchInputInfoFunc = BOOL (WINAPI*) (HTOUCHINPUT, UINT, TOUCHINPUT*, int);
  253. using CloseTouchInputHandleFunc = BOOL (WINAPI*) (HTOUCHINPUT);
  254. using GetGestureInfoFunc = BOOL (WINAPI*) (HGESTUREINFO, GESTUREINFO*);
  255. static RegisterTouchWindowFunc registerTouchWindow = nullptr;
  256. static GetTouchInputInfoFunc getTouchInputInfo = nullptr;
  257. static CloseTouchInputHandleFunc closeTouchInputHandle = nullptr;
  258. static GetGestureInfoFunc getGestureInfo = nullptr;
  259. static bool hasCheckedForMultiTouch = false;
  260. static bool canUseMultiTouch()
  261. {
  262. if (registerTouchWindow == nullptr && ! hasCheckedForMultiTouch)
  263. {
  264. hasCheckedForMultiTouch = true;
  265. registerTouchWindow = (RegisterTouchWindowFunc) getUser32Function ("RegisterTouchWindow");
  266. getTouchInputInfo = (GetTouchInputInfoFunc) getUser32Function ("GetTouchInputInfo");
  267. closeTouchInputHandle = (CloseTouchInputHandleFunc) getUser32Function ("CloseTouchInputHandle");
  268. getGestureInfo = (GetGestureInfoFunc) getUser32Function ("GetGestureInfo");
  269. }
  270. return registerTouchWindow != nullptr;
  271. }
  272. //==============================================================================
  273. using GetPointerTypeFunc = BOOL (WINAPI*) (UINT32, POINTER_INPUT_TYPE*);
  274. using GetPointerTouchInfoFunc = BOOL (WINAPI*) (UINT32, POINTER_TOUCH_INFO*);
  275. using GetPointerPenInfoFunc = BOOL (WINAPI*) (UINT32, POINTER_PEN_INFO*);
  276. static GetPointerTypeFunc getPointerTypeFunction = nullptr;
  277. static GetPointerTouchInfoFunc getPointerTouchInfo = nullptr;
  278. static GetPointerPenInfoFunc getPointerPenInfo = nullptr;
  279. static bool canUsePointerAPI = false;
  280. static void checkForPointerAPI()
  281. {
  282. getPointerTypeFunction = (GetPointerTypeFunc) getUser32Function ("GetPointerType");
  283. getPointerTouchInfo = (GetPointerTouchInfoFunc) getUser32Function ("GetPointerTouchInfo");
  284. getPointerPenInfo = (GetPointerPenInfoFunc) getUser32Function ("GetPointerPenInfo");
  285. canUsePointerAPI = (getPointerTypeFunction != nullptr
  286. && getPointerTouchInfo != nullptr
  287. && getPointerPenInfo != nullptr);
  288. }
  289. //==============================================================================
  290. using SetProcessDPIAwareFunc = BOOL (WINAPI*) ();
  291. using SetProcessDPIAwarenessContextFunc = BOOL (WINAPI*) (DPI_AWARENESS_CONTEXT);
  292. using SetProcessDPIAwarenessFunc = HRESULT (WINAPI*) (DPI_Awareness);
  293. using SetThreadDPIAwarenessContextFunc = DPI_AWARENESS_CONTEXT (WINAPI*) (DPI_AWARENESS_CONTEXT);
  294. using GetDPIForWindowFunc = UINT (WINAPI*) (HWND);
  295. using GetDPIForMonitorFunc = HRESULT (WINAPI*) (HMONITOR, Monitor_DPI_Type, UINT*, UINT*);
  296. using GetSystemMetricsForDpiFunc = int (WINAPI*) (int, UINT);
  297. using GetProcessDPIAwarenessFunc = HRESULT (WINAPI*) (HANDLE, DPI_Awareness*);
  298. using GetWindowDPIAwarenessContextFunc = DPI_AWARENESS_CONTEXT (WINAPI*) (HWND);
  299. using GetThreadDPIAwarenessContextFunc = DPI_AWARENESS_CONTEXT (WINAPI*) ();
  300. using GetAwarenessFromDpiAwarenessContextFunc = DPI_Awareness (WINAPI*) (DPI_AWARENESS_CONTEXT);
  301. using EnableNonClientDPIScalingFunc = BOOL (WINAPI*) (HWND);
  302. static SetProcessDPIAwareFunc setProcessDPIAware = nullptr;
  303. static SetProcessDPIAwarenessContextFunc setProcessDPIAwarenessContext = nullptr;
  304. static SetProcessDPIAwarenessFunc setProcessDPIAwareness = nullptr;
  305. static SetThreadDPIAwarenessContextFunc setThreadDPIAwarenessContext = nullptr;
  306. static GetDPIForMonitorFunc getDPIForMonitor = nullptr;
  307. static GetDPIForWindowFunc getDPIForWindow = nullptr;
  308. static GetProcessDPIAwarenessFunc getProcessDPIAwareness = nullptr;
  309. static GetWindowDPIAwarenessContextFunc getWindowDPIAwarenessContext = nullptr;
  310. static GetThreadDPIAwarenessContextFunc getThreadDPIAwarenessContext = nullptr;
  311. static GetAwarenessFromDpiAwarenessContextFunc getAwarenessFromDPIAwarenessContext = nullptr;
  312. static EnableNonClientDPIScalingFunc enableNonClientDPIScaling = nullptr;
  313. static bool hasCheckedForDPIAwareness = false;
  314. static void loadDPIAwarenessFunctions()
  315. {
  316. setProcessDPIAware = (SetProcessDPIAwareFunc) getUser32Function ("SetProcessDPIAware");
  317. constexpr auto shcore = "SHCore.dll";
  318. LoadLibraryA (shcore);
  319. const auto shcoreModule = GetModuleHandleA (shcore);
  320. if (shcoreModule == nullptr)
  321. return;
  322. getDPIForMonitor = (GetDPIForMonitorFunc) GetProcAddress (shcoreModule, "GetDpiForMonitor");
  323. setProcessDPIAwareness = (SetProcessDPIAwarenessFunc) GetProcAddress (shcoreModule, "SetProcessDpiAwareness");
  324. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  325. getDPIForWindow = (GetDPIForWindowFunc) getUser32Function ("GetDpiForWindow");
  326. getProcessDPIAwareness = (GetProcessDPIAwarenessFunc) GetProcAddress (shcoreModule, "GetProcessDpiAwareness");
  327. getWindowDPIAwarenessContext = (GetWindowDPIAwarenessContextFunc) getUser32Function ("GetWindowDpiAwarenessContext");
  328. setThreadDPIAwarenessContext = (SetThreadDPIAwarenessContextFunc) getUser32Function ("SetThreadDpiAwarenessContext");
  329. getThreadDPIAwarenessContext = (GetThreadDPIAwarenessContextFunc) getUser32Function ("GetThreadDpiAwarenessContext");
  330. getAwarenessFromDPIAwarenessContext = (GetAwarenessFromDpiAwarenessContextFunc) getUser32Function ("GetAwarenessFromDpiAwarenessContext");
  331. setProcessDPIAwarenessContext = (SetProcessDPIAwarenessContextFunc) getUser32Function ("SetProcessDpiAwarenessContext");
  332. enableNonClientDPIScaling = (EnableNonClientDPIScalingFunc) getUser32Function ("EnableNonClientDpiScaling");
  333. #endif
  334. }
  335. static void setDPIAwareness()
  336. {
  337. if (hasCheckedForDPIAwareness)
  338. return;
  339. hasCheckedForDPIAwareness = true;
  340. if (! JUCEApplicationBase::isStandaloneApp())
  341. return;
  342. loadDPIAwarenessFunctions();
  343. if (setProcessDPIAwarenessContext != nullptr
  344. && setProcessDPIAwarenessContext (DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
  345. return;
  346. if (setProcessDPIAwareness != nullptr && enableNonClientDPIScaling != nullptr
  347. && SUCCEEDED (setProcessDPIAwareness (DPI_Awareness::DPI_Awareness_Per_Monitor_Aware)))
  348. return;
  349. if (setProcessDPIAwareness != nullptr && getDPIForMonitor != nullptr
  350. && SUCCEEDED (setProcessDPIAwareness (DPI_Awareness::DPI_Awareness_System_Aware)))
  351. return;
  352. NullCheckedInvocation::invoke (setProcessDPIAware);
  353. }
  354. static bool isPerMonitorDPIAwareProcess()
  355. {
  356. #if ! JUCE_WIN_PER_MONITOR_DPI_AWARE
  357. return false;
  358. #else
  359. static bool dpiAware = []() -> bool
  360. {
  361. setDPIAwareness();
  362. if (! JUCEApplication::isStandaloneApp())
  363. return false;
  364. if (getProcessDPIAwareness == nullptr)
  365. return false;
  366. DPI_Awareness context;
  367. getProcessDPIAwareness (nullptr, &context);
  368. return context == DPI_Awareness::DPI_Awareness_Per_Monitor_Aware;
  369. }();
  370. return dpiAware;
  371. #endif
  372. }
  373. static bool isPerMonitorDPIAwareWindow ([[maybe_unused]] HWND nativeWindow)
  374. {
  375. #if ! JUCE_WIN_PER_MONITOR_DPI_AWARE
  376. return false;
  377. #else
  378. setDPIAwareness();
  379. if (getWindowDPIAwarenessContext != nullptr
  380. && getAwarenessFromDPIAwarenessContext != nullptr)
  381. {
  382. return (getAwarenessFromDPIAwarenessContext (getWindowDPIAwarenessContext (nativeWindow))
  383. == DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
  384. }
  385. return isPerMonitorDPIAwareProcess();
  386. #endif
  387. }
  388. static bool isPerMonitorDPIAwareThread (GetThreadDPIAwarenessContextFunc getThreadDPIAwarenessContextIn = getThreadDPIAwarenessContext,
  389. GetAwarenessFromDpiAwarenessContextFunc getAwarenessFromDPIAwarenessContextIn = getAwarenessFromDPIAwarenessContext)
  390. {
  391. #if ! JUCE_WIN_PER_MONITOR_DPI_AWARE
  392. return false;
  393. #else
  394. setDPIAwareness();
  395. if (getThreadDPIAwarenessContextIn != nullptr
  396. && getAwarenessFromDPIAwarenessContextIn != nullptr)
  397. {
  398. return (getAwarenessFromDPIAwarenessContextIn (getThreadDPIAwarenessContextIn())
  399. == DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
  400. }
  401. return isPerMonitorDPIAwareProcess();
  402. #endif
  403. }
  404. static double getGlobalDPI()
  405. {
  406. setDPIAwareness();
  407. ScopedDeviceContext deviceContext { nullptr };
  408. return (GetDeviceCaps (deviceContext.dc, LOGPIXELSX) + GetDeviceCaps (deviceContext.dc, LOGPIXELSY)) / 2.0;
  409. }
  410. //==============================================================================
  411. class ScopedSuspendResumeNotificationRegistration
  412. {
  413. static auto& getFunctions()
  414. {
  415. struct Functions
  416. {
  417. using Register = HPOWERNOTIFY (WINAPI*) (HANDLE, DWORD);
  418. using Unregister = BOOL (WINAPI*) (HPOWERNOTIFY);
  419. Register registerNotification = (Register) getUser32Function ("RegisterSuspendResumeNotification");
  420. Unregister unregisterNotification = (Unregister) getUser32Function ("UnregisterSuspendResumeNotification");
  421. bool isValid() const { return registerNotification != nullptr && unregisterNotification != nullptr; }
  422. Functions() = default;
  423. JUCE_DECLARE_NON_COPYABLE (Functions)
  424. JUCE_DECLARE_NON_MOVEABLE (Functions)
  425. };
  426. static const Functions functions;
  427. return functions;
  428. }
  429. public:
  430. ScopedSuspendResumeNotificationRegistration() = default;
  431. explicit ScopedSuspendResumeNotificationRegistration (HWND window)
  432. : handle (getFunctions().isValid()
  433. ? getFunctions().registerNotification (window, DEVICE_NOTIFY_WINDOW_HANDLE)
  434. : nullptr)
  435. {}
  436. private:
  437. struct Destructor
  438. {
  439. void operator() (HPOWERNOTIFY ptr) const
  440. {
  441. if (ptr != nullptr)
  442. getFunctions().unregisterNotification (ptr);
  443. }
  444. };
  445. std::unique_ptr<std::remove_pointer_t<HPOWERNOTIFY>, Destructor> handle;
  446. };
  447. //==============================================================================
  448. class ScopedThreadDPIAwarenessSetter::NativeImpl
  449. {
  450. public:
  451. static auto& getFunctions()
  452. {
  453. struct Functions
  454. {
  455. SetThreadDPIAwarenessContextFunc setThreadAwareness = (SetThreadDPIAwarenessContextFunc) getUser32Function ("SetThreadDpiAwarenessContext");
  456. GetWindowDPIAwarenessContextFunc getWindowAwareness = (GetWindowDPIAwarenessContextFunc) getUser32Function ("GetWindowDpiAwarenessContext");
  457. GetThreadDPIAwarenessContextFunc getThreadAwareness = (GetThreadDPIAwarenessContextFunc) getUser32Function ("GetThreadDpiAwarenessContext");
  458. GetAwarenessFromDpiAwarenessContextFunc getAwarenessFromContext = (GetAwarenessFromDpiAwarenessContextFunc) getUser32Function ("GetAwarenessFromDpiAwarenessContext");
  459. bool isLoaded() const noexcept
  460. {
  461. return setThreadAwareness != nullptr
  462. && getWindowAwareness != nullptr
  463. && getThreadAwareness != nullptr
  464. && getAwarenessFromContext != nullptr;
  465. }
  466. Functions() = default;
  467. JUCE_DECLARE_NON_COPYABLE (Functions)
  468. JUCE_DECLARE_NON_MOVEABLE (Functions)
  469. };
  470. static const Functions functions;
  471. return functions;
  472. }
  473. explicit NativeImpl (HWND nativeWindow [[maybe_unused]])
  474. {
  475. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  476. if (const auto& functions = getFunctions(); functions.isLoaded())
  477. {
  478. auto dpiAwareWindow = (functions.getAwarenessFromContext (functions.getWindowAwareness (nativeWindow))
  479. == DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
  480. auto dpiAwareThread = (functions.getAwarenessFromContext (functions.getThreadAwareness())
  481. == DPI_Awareness::DPI_Awareness_Per_Monitor_Aware);
  482. if (dpiAwareWindow && ! dpiAwareThread)
  483. oldContext = functions.setThreadAwareness (DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
  484. else if (! dpiAwareWindow && dpiAwareThread)
  485. oldContext = functions.setThreadAwareness (DPI_AWARENESS_CONTEXT_UNAWARE);
  486. }
  487. #endif
  488. }
  489. ~NativeImpl()
  490. {
  491. if (oldContext != nullptr)
  492. getFunctions().setThreadAwareness (oldContext);
  493. }
  494. private:
  495. DPI_AWARENESS_CONTEXT oldContext = nullptr;
  496. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeImpl)
  497. JUCE_DECLARE_NON_MOVEABLE (NativeImpl)
  498. };
  499. ScopedThreadDPIAwarenessSetter::ScopedThreadDPIAwarenessSetter (void* nativeWindow)
  500. {
  501. pimpl = std::make_unique<NativeImpl> ((HWND) nativeWindow);
  502. }
  503. ScopedThreadDPIAwarenessSetter::~ScopedThreadDPIAwarenessSetter() = default;
  504. static auto& getScopedDPIAwarenessDisablerFunctions()
  505. {
  506. struct Functions
  507. {
  508. GetThreadDPIAwarenessContextFunc localGetThreadDpiAwarenessContext = (GetThreadDPIAwarenessContextFunc) getUser32Function ("GetThreadDpiAwarenessContext");
  509. GetAwarenessFromDpiAwarenessContextFunc localGetAwarenessFromDpiAwarenessContextFunc = (GetAwarenessFromDpiAwarenessContextFunc) getUser32Function ("GetAwarenessFromDpiAwarenessContext");
  510. SetThreadDPIAwarenessContextFunc localSetThreadDPIAwarenessContext = (SetThreadDPIAwarenessContextFunc) getUser32Function ("SetThreadDpiAwarenessContext");
  511. Functions() = default;
  512. JUCE_DECLARE_NON_COPYABLE (Functions)
  513. JUCE_DECLARE_NON_MOVEABLE (Functions)
  514. };
  515. static const Functions functions;
  516. return functions;
  517. }
  518. ScopedDPIAwarenessDisabler::ScopedDPIAwarenessDisabler()
  519. {
  520. const auto& functions = getScopedDPIAwarenessDisablerFunctions();
  521. if (! isPerMonitorDPIAwareThread (functions.localGetThreadDpiAwarenessContext, functions.localGetAwarenessFromDpiAwarenessContextFunc))
  522. return;
  523. if (auto* localSetThreadDPIAwarenessContext = functions.localSetThreadDPIAwarenessContext)
  524. {
  525. previousContext = localSetThreadDPIAwarenessContext (DPI_AWARENESS_CONTEXT_UNAWARE);
  526. #if JUCE_DEBUG
  527. ++numActiveScopedDpiAwarenessDisablers;
  528. #endif
  529. }
  530. }
  531. ScopedDPIAwarenessDisabler::~ScopedDPIAwarenessDisabler()
  532. {
  533. if (previousContext != nullptr)
  534. {
  535. if (auto* localSetThreadDPIAwarenessContext = getScopedDPIAwarenessDisablerFunctions().localSetThreadDPIAwarenessContext)
  536. localSetThreadDPIAwarenessContext ((DPI_AWARENESS_CONTEXT) previousContext);
  537. #if JUCE_DEBUG
  538. --numActiveScopedDpiAwarenessDisablers;
  539. #endif
  540. }
  541. }
  542. //==============================================================================
  543. using SettingChangeCallbackFunc = void (*)(void);
  544. extern SettingChangeCallbackFunc settingChangeCallback;
  545. //==============================================================================
  546. static Rectangle<int> rectangleFromRECT (RECT r) noexcept { return { r.left, r.top, r.right - r.left, r.bottom - r.top }; }
  547. static RECT RECTFromRectangle (Rectangle<int> r) noexcept { return { r.getX(), r.getY(), r.getRight(), r.getBottom() }; }
  548. static Point<int> pointFromPOINT (POINT p) noexcept { return { p.x, p.y }; }
  549. static POINT POINTFromPoint (Point<int> p) noexcept { return { p.x, p.y }; }
  550. //==============================================================================
  551. static const Displays::Display* getCurrentDisplayFromScaleFactor (HWND hwnd);
  552. template <typename ValueType>
  553. static Rectangle<ValueType> convertPhysicalScreenRectangleToLogical (Rectangle<ValueType> r, HWND h) noexcept
  554. {
  555. if (isPerMonitorDPIAwareWindow (h))
  556. return Desktop::getInstance().getDisplays().physicalToLogical (r, getCurrentDisplayFromScaleFactor (h));
  557. return r;
  558. }
  559. template <typename ValueType>
  560. static Rectangle<ValueType> convertLogicalScreenRectangleToPhysical (Rectangle<ValueType> r, HWND h) noexcept
  561. {
  562. if (isPerMonitorDPIAwareWindow (h))
  563. return Desktop::getInstance().getDisplays().logicalToPhysical (r, getCurrentDisplayFromScaleFactor (h));
  564. return r;
  565. }
  566. static Point<int> convertPhysicalScreenPointToLogical (Point<int> p, HWND h) noexcept
  567. {
  568. if (isPerMonitorDPIAwareWindow (h))
  569. return Desktop::getInstance().getDisplays().physicalToLogical (p, getCurrentDisplayFromScaleFactor (h));
  570. return p;
  571. }
  572. static Point<int> convertLogicalScreenPointToPhysical (Point<int> p, HWND h) noexcept
  573. {
  574. if (isPerMonitorDPIAwareWindow (h))
  575. return Desktop::getInstance().getDisplays().logicalToPhysical (p, getCurrentDisplayFromScaleFactor (h));
  576. return p;
  577. }
  578. JUCE_API double getScaleFactorForWindow (HWND h);
  579. JUCE_API double getScaleFactorForWindow (HWND h)
  580. {
  581. // NB. Using a local function here because we need to call this method from the plug-in wrappers
  582. // which don't load the DPI-awareness functions on startup
  583. static auto localGetDPIForWindow = (GetDPIForWindowFunc) getUser32Function ("GetDpiForWindow");
  584. if (localGetDPIForWindow != nullptr)
  585. return (double) localGetDPIForWindow (h) / USER_DEFAULT_SCREEN_DPI;
  586. return 1.0;
  587. }
  588. //==============================================================================
  589. static void setWindowPos (HWND hwnd, Rectangle<int> bounds, UINT flags, bool adjustTopLeft = false)
  590. {
  591. ScopedThreadDPIAwarenessSetter setter { hwnd };
  592. if (isPerMonitorDPIAwareWindow (hwnd))
  593. {
  594. if (adjustTopLeft)
  595. bounds = convertLogicalScreenRectangleToPhysical (bounds, hwnd)
  596. .withPosition (Desktop::getInstance().getDisplays().logicalToPhysical (bounds.getTopLeft()));
  597. else
  598. bounds = convertLogicalScreenRectangleToPhysical (bounds, hwnd);
  599. }
  600. SetWindowPos (hwnd, nullptr, bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), flags);
  601. }
  602. static RECT getWindowScreenRect (HWND hwnd)
  603. {
  604. ScopedThreadDPIAwarenessSetter setter { hwnd };
  605. RECT rect;
  606. GetWindowRect (hwnd, &rect);
  607. return rect;
  608. }
  609. static RECT getWindowClientRect (HWND hwnd)
  610. {
  611. auto rect = getWindowScreenRect (hwnd);
  612. if (auto parentH = GetParent (hwnd))
  613. {
  614. ScopedThreadDPIAwarenessSetter setter { hwnd };
  615. MapWindowPoints (HWND_DESKTOP, parentH, (LPPOINT) &rect, 2);
  616. }
  617. return rect;
  618. }
  619. static void setWindowZOrder (HWND hwnd, HWND insertAfter)
  620. {
  621. SetWindowPos (hwnd, insertAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING);
  622. }
  623. //==============================================================================
  624. #if ! JUCE_MINGW
  625. extern RTL_OSVERSIONINFOW getWindowsVersionInfo();
  626. #endif
  627. double Desktop::getDefaultMasterScale()
  628. {
  629. if (! JUCEApplicationBase::isStandaloneApp() || isPerMonitorDPIAwareProcess())
  630. return 1.0;
  631. return getGlobalDPI() / USER_DEFAULT_SCREEN_DPI;
  632. }
  633. bool Desktop::canUseSemiTransparentWindows() noexcept
  634. {
  635. return true;
  636. }
  637. class Desktop::NativeDarkModeChangeDetectorImpl
  638. {
  639. public:
  640. NativeDarkModeChangeDetectorImpl()
  641. {
  642. #if ! JUCE_MINGW
  643. const auto winVer = getWindowsVersionInfo();
  644. if (winVer.dwMajorVersion >= 10 && winVer.dwBuildNumber >= 17763)
  645. {
  646. const auto uxtheme = "uxtheme.dll";
  647. LoadLibraryA (uxtheme);
  648. const auto uxthemeModule = GetModuleHandleA (uxtheme);
  649. if (uxthemeModule != nullptr)
  650. {
  651. shouldAppsUseDarkMode = (ShouldAppsUseDarkModeFunc) GetProcAddress (uxthemeModule, MAKEINTRESOURCEA (132));
  652. if (shouldAppsUseDarkMode != nullptr)
  653. darkModeEnabled = shouldAppsUseDarkMode() && ! isHighContrast();
  654. }
  655. }
  656. #endif
  657. }
  658. ~NativeDarkModeChangeDetectorImpl()
  659. {
  660. UnhookWindowsHookEx (hook);
  661. }
  662. bool isDarkModeEnabled() const noexcept { return darkModeEnabled; }
  663. private:
  664. static bool isHighContrast()
  665. {
  666. HIGHCONTRASTW highContrast {};
  667. if (SystemParametersInfoW (SPI_GETHIGHCONTRAST, sizeof (highContrast), &highContrast, false))
  668. return highContrast.dwFlags & HCF_HIGHCONTRASTON;
  669. return false;
  670. }
  671. static LRESULT CALLBACK callWndProc (int nCode, WPARAM wParam, LPARAM lParam)
  672. {
  673. auto* params = reinterpret_cast<CWPSTRUCT*> (lParam);
  674. if (nCode >= 0
  675. && params != nullptr
  676. && params->message == WM_SETTINGCHANGE
  677. && params->lParam != 0
  678. && CompareStringOrdinal (reinterpret_cast<LPWCH> (params->lParam), -1, L"ImmersiveColorSet", -1, true) == CSTR_EQUAL)
  679. {
  680. Desktop::getInstance().nativeDarkModeChangeDetectorImpl->colourSetChanged();
  681. }
  682. return CallNextHookEx ({}, nCode, wParam, lParam);
  683. }
  684. void colourSetChanged()
  685. {
  686. if (shouldAppsUseDarkMode != nullptr)
  687. {
  688. const auto wasDarkModeEnabled = std::exchange (darkModeEnabled, shouldAppsUseDarkMode() && ! isHighContrast());
  689. if (darkModeEnabled != wasDarkModeEnabled)
  690. Desktop::getInstance().darkModeChanged();
  691. }
  692. }
  693. using ShouldAppsUseDarkModeFunc = bool (WINAPI*)();
  694. ShouldAppsUseDarkModeFunc shouldAppsUseDarkMode = nullptr;
  695. bool darkModeEnabled = false;
  696. HHOOK hook { SetWindowsHookEx (WH_CALLWNDPROC,
  697. callWndProc,
  698. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  699. GetCurrentThreadId()) };
  700. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeDarkModeChangeDetectorImpl)
  701. };
  702. std::unique_ptr<Desktop::NativeDarkModeChangeDetectorImpl> Desktop::createNativeDarkModeChangeDetectorImpl()
  703. {
  704. return std::make_unique<NativeDarkModeChangeDetectorImpl>();
  705. }
  706. bool Desktop::isDarkModeActive() const
  707. {
  708. return nativeDarkModeChangeDetectorImpl->isDarkModeEnabled();
  709. }
  710. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  711. {
  712. return upright;
  713. }
  714. int64 getMouseEventTime();
  715. int64 getMouseEventTime()
  716. {
  717. static int64 eventTimeOffset = 0;
  718. static LONG lastMessageTime = 0;
  719. const LONG thisMessageTime = GetMessageTime();
  720. if (thisMessageTime < lastMessageTime || lastMessageTime == 0)
  721. {
  722. lastMessageTime = thisMessageTime;
  723. eventTimeOffset = Time::currentTimeMillis() - thisMessageTime;
  724. }
  725. return eventTimeOffset + thisMessageTime;
  726. }
  727. //==============================================================================
  728. const int extendedKeyModifier = 0x10000;
  729. const int KeyPress::spaceKey = VK_SPACE;
  730. const int KeyPress::returnKey = VK_RETURN;
  731. const int KeyPress::escapeKey = VK_ESCAPE;
  732. const int KeyPress::backspaceKey = VK_BACK;
  733. const int KeyPress::deleteKey = VK_DELETE | extendedKeyModifier;
  734. const int KeyPress::insertKey = VK_INSERT | extendedKeyModifier;
  735. const int KeyPress::tabKey = VK_TAB;
  736. const int KeyPress::leftKey = VK_LEFT | extendedKeyModifier;
  737. const int KeyPress::rightKey = VK_RIGHT | extendedKeyModifier;
  738. const int KeyPress::upKey = VK_UP | extendedKeyModifier;
  739. const int KeyPress::downKey = VK_DOWN | extendedKeyModifier;
  740. const int KeyPress::homeKey = VK_HOME | extendedKeyModifier;
  741. const int KeyPress::endKey = VK_END | extendedKeyModifier;
  742. const int KeyPress::pageUpKey = VK_PRIOR | extendedKeyModifier;
  743. const int KeyPress::pageDownKey = VK_NEXT | extendedKeyModifier;
  744. const int KeyPress::F1Key = VK_F1 | extendedKeyModifier;
  745. const int KeyPress::F2Key = VK_F2 | extendedKeyModifier;
  746. const int KeyPress::F3Key = VK_F3 | extendedKeyModifier;
  747. const int KeyPress::F4Key = VK_F4 | extendedKeyModifier;
  748. const int KeyPress::F5Key = VK_F5 | extendedKeyModifier;
  749. const int KeyPress::F6Key = VK_F6 | extendedKeyModifier;
  750. const int KeyPress::F7Key = VK_F7 | extendedKeyModifier;
  751. const int KeyPress::F8Key = VK_F8 | extendedKeyModifier;
  752. const int KeyPress::F9Key = VK_F9 | extendedKeyModifier;
  753. const int KeyPress::F10Key = VK_F10 | extendedKeyModifier;
  754. const int KeyPress::F11Key = VK_F11 | extendedKeyModifier;
  755. const int KeyPress::F12Key = VK_F12 | extendedKeyModifier;
  756. const int KeyPress::F13Key = VK_F13 | extendedKeyModifier;
  757. const int KeyPress::F14Key = VK_F14 | extendedKeyModifier;
  758. const int KeyPress::F15Key = VK_F15 | extendedKeyModifier;
  759. const int KeyPress::F16Key = VK_F16 | extendedKeyModifier;
  760. const int KeyPress::F17Key = VK_F17 | extendedKeyModifier;
  761. const int KeyPress::F18Key = VK_F18 | extendedKeyModifier;
  762. const int KeyPress::F19Key = VK_F19 | extendedKeyModifier;
  763. const int KeyPress::F20Key = VK_F20 | extendedKeyModifier;
  764. const int KeyPress::F21Key = VK_F21 | extendedKeyModifier;
  765. const int KeyPress::F22Key = VK_F22 | extendedKeyModifier;
  766. const int KeyPress::F23Key = VK_F23 | extendedKeyModifier;
  767. const int KeyPress::F24Key = VK_F24 | extendedKeyModifier;
  768. const int KeyPress::F25Key = 0x31000; // Windows doesn't support F-keys 25 or higher
  769. const int KeyPress::F26Key = 0x31001;
  770. const int KeyPress::F27Key = 0x31002;
  771. const int KeyPress::F28Key = 0x31003;
  772. const int KeyPress::F29Key = 0x31004;
  773. const int KeyPress::F30Key = 0x31005;
  774. const int KeyPress::F31Key = 0x31006;
  775. const int KeyPress::F32Key = 0x31007;
  776. const int KeyPress::F33Key = 0x31008;
  777. const int KeyPress::F34Key = 0x31009;
  778. const int KeyPress::F35Key = 0x3100a;
  779. const int KeyPress::numberPad0 = VK_NUMPAD0 | extendedKeyModifier;
  780. const int KeyPress::numberPad1 = VK_NUMPAD1 | extendedKeyModifier;
  781. const int KeyPress::numberPad2 = VK_NUMPAD2 | extendedKeyModifier;
  782. const int KeyPress::numberPad3 = VK_NUMPAD3 | extendedKeyModifier;
  783. const int KeyPress::numberPad4 = VK_NUMPAD4 | extendedKeyModifier;
  784. const int KeyPress::numberPad5 = VK_NUMPAD5 | extendedKeyModifier;
  785. const int KeyPress::numberPad6 = VK_NUMPAD6 | extendedKeyModifier;
  786. const int KeyPress::numberPad7 = VK_NUMPAD7 | extendedKeyModifier;
  787. const int KeyPress::numberPad8 = VK_NUMPAD8 | extendedKeyModifier;
  788. const int KeyPress::numberPad9 = VK_NUMPAD9 | extendedKeyModifier;
  789. const int KeyPress::numberPadAdd = VK_ADD | extendedKeyModifier;
  790. const int KeyPress::numberPadSubtract = VK_SUBTRACT | extendedKeyModifier;
  791. const int KeyPress::numberPadMultiply = VK_MULTIPLY | extendedKeyModifier;
  792. const int KeyPress::numberPadDivide = VK_DIVIDE | extendedKeyModifier;
  793. const int KeyPress::numberPadSeparator = VK_SEPARATOR | extendedKeyModifier;
  794. const int KeyPress::numberPadDecimalPoint = VK_DECIMAL | extendedKeyModifier;
  795. const int KeyPress::numberPadEquals = 0x92 /*VK_OEM_NEC_EQUAL*/ | extendedKeyModifier;
  796. const int KeyPress::numberPadDelete = VK_DELETE | extendedKeyModifier;
  797. const int KeyPress::playKey = 0x30000;
  798. const int KeyPress::stopKey = 0x30001;
  799. const int KeyPress::fastForwardKey = 0x30002;
  800. const int KeyPress::rewindKey = 0x30003;
  801. //==============================================================================
  802. class WindowsBitmapImage final : public ImagePixelData
  803. {
  804. public:
  805. WindowsBitmapImage (const Image::PixelFormat format,
  806. const int w, const int h, const bool clearImage)
  807. : ImagePixelData (format, w, h)
  808. {
  809. jassert (format == Image::RGB || format == Image::ARGB);
  810. static bool alwaysUse32Bits = isGraphicsCard32Bit(); // NB: for 32-bit cards, it's faster to use a 32-bit image.
  811. pixelStride = (alwaysUse32Bits || format == Image::ARGB) ? 4 : 3;
  812. lineStride = -((w * pixelStride + 3) & ~3);
  813. zerostruct (bitmapInfo);
  814. bitmapInfo.bV4Size = sizeof (BITMAPV4HEADER);
  815. bitmapInfo.bV4Width = w;
  816. bitmapInfo.bV4Height = h;
  817. bitmapInfo.bV4Planes = 1;
  818. bitmapInfo.bV4CSType = 1;
  819. bitmapInfo.bV4BitCount = (unsigned short) (pixelStride * 8);
  820. if (format == Image::ARGB)
  821. {
  822. bitmapInfo.bV4AlphaMask = 0xff000000;
  823. bitmapInfo.bV4RedMask = 0xff0000;
  824. bitmapInfo.bV4GreenMask = 0xff00;
  825. bitmapInfo.bV4BlueMask = 0xff;
  826. bitmapInfo.bV4V4Compression = BI_BITFIELDS;
  827. }
  828. else
  829. {
  830. bitmapInfo.bV4V4Compression = BI_RGB;
  831. }
  832. {
  833. ScopedDeviceContext deviceContext { nullptr };
  834. hdc = CreateCompatibleDC (deviceContext.dc);
  835. }
  836. SetMapMode (hdc, MM_TEXT);
  837. hBitmap = CreateDIBSection (hdc, (BITMAPINFO*) &(bitmapInfo), DIB_RGB_COLORS,
  838. (void**) &bitmapData, nullptr, 0);
  839. if (hBitmap != nullptr)
  840. previousBitmap = SelectObject (hdc, hBitmap);
  841. if (format == Image::ARGB && clearImage)
  842. zeromem (bitmapData, (size_t) std::abs (h * lineStride));
  843. imageData = bitmapData - (lineStride * (h - 1));
  844. }
  845. ~WindowsBitmapImage() override
  846. {
  847. SelectObject (hdc, previousBitmap); // Selecting the previous bitmap before deleting the DC avoids a warning in BoundsChecker
  848. DeleteDC (hdc);
  849. DeleteObject (hBitmap);
  850. }
  851. std::unique_ptr<ImageType> createType() const override { return std::make_unique<NativeImageType>(); }
  852. std::unique_ptr<LowLevelGraphicsContext> createLowLevelContext() override
  853. {
  854. sendDataChangeMessage();
  855. return std::make_unique<LowLevelGraphicsSoftwareRenderer> (Image (this));
  856. }
  857. void initialiseBitmapData (Image::BitmapData& bitmap, int x, int y, Image::BitmapData::ReadWriteMode mode) override
  858. {
  859. const auto offset = (size_t) (x * pixelStride + y * lineStride);
  860. bitmap.data = imageData + offset;
  861. bitmap.size = (size_t) (lineStride * height) - offset;
  862. bitmap.pixelFormat = pixelFormat;
  863. bitmap.lineStride = lineStride;
  864. bitmap.pixelStride = pixelStride;
  865. if (mode != Image::BitmapData::readOnly)
  866. sendDataChangeMessage();
  867. }
  868. ImagePixelData::Ptr clone() override
  869. {
  870. auto im = new WindowsBitmapImage (pixelFormat, width, height, false);
  871. for (int i = 0; i < height; ++i)
  872. memcpy (im->imageData + i * lineStride, imageData + i * lineStride, (size_t) lineStride);
  873. return im;
  874. }
  875. void blitToWindow (HWND hwnd, HDC dc, bool transparent, int x, int y, uint8 updateLayeredWindowAlpha) noexcept
  876. {
  877. SetMapMode (dc, MM_TEXT);
  878. if (transparent)
  879. {
  880. auto windowBounds = getWindowScreenRect (hwnd);
  881. POINT p = { -x, -y };
  882. POINT pos = { windowBounds.left, windowBounds.top };
  883. SIZE size = { windowBounds.right - windowBounds.left,
  884. windowBounds.bottom - windowBounds.top };
  885. BLENDFUNCTION bf;
  886. bf.AlphaFormat = 1 /*AC_SRC_ALPHA*/;
  887. bf.BlendFlags = 0;
  888. bf.BlendOp = AC_SRC_OVER;
  889. bf.SourceConstantAlpha = updateLayeredWindowAlpha;
  890. UpdateLayeredWindow (hwnd, nullptr, &pos, &size, hdc, &p, 0, &bf, 2 /*ULW_ALPHA*/);
  891. }
  892. else
  893. {
  894. StretchDIBits (dc,
  895. x, y, width, height,
  896. 0, 0, width, height,
  897. bitmapData, (const BITMAPINFO*) &bitmapInfo,
  898. DIB_RGB_COLORS, SRCCOPY);
  899. }
  900. }
  901. HBITMAP hBitmap;
  902. HGDIOBJ previousBitmap;
  903. BITMAPV4HEADER bitmapInfo;
  904. HDC hdc;
  905. uint8* bitmapData;
  906. int pixelStride, lineStride;
  907. uint8* imageData;
  908. private:
  909. static bool isGraphicsCard32Bit()
  910. {
  911. ScopedDeviceContext deviceContext { nullptr };
  912. return GetDeviceCaps (deviceContext.dc, BITSPIXEL) > 24;
  913. }
  914. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowsBitmapImage)
  915. };
  916. //==============================================================================
  917. Image createSnapshotOfNativeWindow (void* nativeWindowHandle)
  918. {
  919. auto hwnd = (HWND) nativeWindowHandle;
  920. auto r = convertPhysicalScreenRectangleToLogical (rectangleFromRECT (getWindowScreenRect (hwnd)), hwnd);
  921. const auto w = r.getWidth();
  922. const auto h = r.getHeight();
  923. auto nativeBitmap = new WindowsBitmapImage (Image::RGB, w, h, true);
  924. Image bitmap (nativeBitmap);
  925. ScopedDeviceContext deviceContext { hwnd };
  926. if (isPerMonitorDPIAwareProcess())
  927. {
  928. auto scale = getScaleFactorForWindow (hwnd);
  929. auto prevStretchMode = SetStretchBltMode (nativeBitmap->hdc, HALFTONE);
  930. SetBrushOrgEx (nativeBitmap->hdc, 0, 0, nullptr);
  931. StretchBlt (nativeBitmap->hdc, 0, 0, w, h,
  932. deviceContext.dc, 0, 0, roundToInt (w * scale), roundToInt (h * scale),
  933. SRCCOPY);
  934. SetStretchBltMode (nativeBitmap->hdc, prevStretchMode);
  935. }
  936. else
  937. {
  938. BitBlt (nativeBitmap->hdc, 0, 0, w, h, deviceContext.dc, 0, 0, SRCCOPY);
  939. }
  940. return SoftwareImageType().convert (bitmap);
  941. }
  942. //==============================================================================
  943. namespace IconConverters
  944. {
  945. struct IconDestructor
  946. {
  947. void operator() (HICON ptr) const { if (ptr != nullptr) DestroyIcon (ptr); }
  948. };
  949. using IconPtr = std::unique_ptr<std::remove_pointer_t<HICON>, IconDestructor>;
  950. static Image createImageFromHICON (HICON icon)
  951. {
  952. if (icon == nullptr)
  953. return {};
  954. struct ScopedICONINFO final : public ICONINFO
  955. {
  956. ScopedICONINFO()
  957. {
  958. hbmColor = nullptr;
  959. hbmMask = nullptr;
  960. }
  961. ~ScopedICONINFO()
  962. {
  963. if (hbmColor != nullptr)
  964. ::DeleteObject (hbmColor);
  965. if (hbmMask != nullptr)
  966. ::DeleteObject (hbmMask);
  967. }
  968. };
  969. ScopedICONINFO info;
  970. if (! ::GetIconInfo (icon, &info))
  971. return {};
  972. BITMAP bm;
  973. if (! (::GetObject (info.hbmColor, sizeof (BITMAP), &bm)
  974. && bm.bmWidth > 0 && bm.bmHeight > 0))
  975. return {};
  976. ScopedDeviceContext deviceContext { nullptr };
  977. if (auto* dc = ::CreateCompatibleDC (deviceContext.dc))
  978. {
  979. BITMAPV5HEADER header = {};
  980. header.bV5Size = sizeof (BITMAPV5HEADER);
  981. header.bV5Width = bm.bmWidth;
  982. header.bV5Height = -bm.bmHeight;
  983. header.bV5Planes = 1;
  984. header.bV5Compression = BI_RGB;
  985. header.bV5BitCount = 32;
  986. header.bV5RedMask = 0x00FF0000;
  987. header.bV5GreenMask = 0x0000FF00;
  988. header.bV5BlueMask = 0x000000FF;
  989. header.bV5AlphaMask = 0xFF000000;
  990. header.bV5CSType = 0x57696E20; // 'Win '
  991. header.bV5Intent = LCS_GM_IMAGES;
  992. uint32* bitmapImageData = nullptr;
  993. if (auto* dib = ::CreateDIBSection (deviceContext.dc, (BITMAPINFO*) &header, DIB_RGB_COLORS,
  994. (void**) &bitmapImageData, nullptr, 0))
  995. {
  996. auto oldObject = ::SelectObject (dc, dib);
  997. auto numPixels = bm.bmWidth * bm.bmHeight;
  998. auto numColourComponents = (size_t) numPixels * 4;
  999. // Windows icon data comes as two layers, an XOR mask which contains the bulk
  1000. // of the image data and an AND mask which provides the transparency. Annoyingly
  1001. // the XOR mask can also contain an alpha channel, in which case the transparency
  1002. // mask should not be applied, but there's no way to find out a priori if the XOR
  1003. // mask contains an alpha channel.
  1004. HeapBlock<bool> opacityMask (numPixels);
  1005. memset (bitmapImageData, 0, numColourComponents);
  1006. ::DrawIconEx (dc, 0, 0, icon, bm.bmWidth, bm.bmHeight, 0, nullptr, DI_MASK);
  1007. for (int i = 0; i < numPixels; ++i)
  1008. opacityMask[i] = (bitmapImageData[i] == 0);
  1009. Image result = Image (Image::ARGB, bm.bmWidth, bm.bmHeight, true);
  1010. Image::BitmapData imageData (result, Image::BitmapData::readWrite);
  1011. memset (bitmapImageData, 0, numColourComponents);
  1012. ::DrawIconEx (dc, 0, 0, icon, bm.bmWidth, bm.bmHeight, 0, nullptr, DI_NORMAL);
  1013. memcpy (imageData.data, bitmapImageData, numColourComponents);
  1014. auto imageHasAlphaChannel = [&imageData, numPixels]()
  1015. {
  1016. for (int i = 0; i < numPixels; ++i)
  1017. if (imageData.data[i * 4] != 0)
  1018. return true;
  1019. return false;
  1020. };
  1021. if (! imageHasAlphaChannel())
  1022. for (int i = 0; i < numPixels; ++i)
  1023. imageData.data[i * 4] = opacityMask[i] ? 0xff : 0x00;
  1024. ::SelectObject (dc, oldObject);
  1025. ::DeleteObject (dib);
  1026. ::DeleteDC (dc);
  1027. return result;
  1028. }
  1029. ::DeleteDC (dc);
  1030. }
  1031. return {};
  1032. }
  1033. HICON createHICONFromImage (const Image& image, const BOOL isIcon, int hotspotX, int hotspotY);
  1034. HICON createHICONFromImage (const Image& image, const BOOL isIcon, int hotspotX, int hotspotY)
  1035. {
  1036. auto nativeBitmap = new WindowsBitmapImage (Image::ARGB, image.getWidth(), image.getHeight(), true);
  1037. Image bitmap (nativeBitmap);
  1038. {
  1039. Graphics g (bitmap);
  1040. g.drawImageAt (image, 0, 0);
  1041. }
  1042. auto mask = CreateBitmap (image.getWidth(), image.getHeight(), 1, 1, nullptr);
  1043. ICONINFO info;
  1044. info.fIcon = isIcon;
  1045. info.xHotspot = (DWORD) hotspotX;
  1046. info.yHotspot = (DWORD) hotspotY;
  1047. info.hbmMask = mask;
  1048. info.hbmColor = nativeBitmap->hBitmap;
  1049. auto hi = CreateIconIndirect (&info);
  1050. DeleteObject (mask);
  1051. return hi;
  1052. }
  1053. } // namespace IconConverters
  1054. //==============================================================================
  1055. JUCE_IUNKNOWNCLASS (ITipInvocation, "37c994e7-432b-4834-a2f7-dce1f13b834b")
  1056. {
  1057. static CLSID getCLSID() noexcept { return { 0x4ce576fa, 0x83dc, 0x4f88, { 0x95, 0x1c, 0x9d, 0x07, 0x82, 0xb4, 0xe3, 0x76 } }; }
  1058. JUCE_COMCALL Toggle (HWND) = 0;
  1059. };
  1060. } // namespace juce
  1061. #ifdef __CRT_UUID_DECL
  1062. __CRT_UUID_DECL (juce::ITipInvocation, 0x37c994e7, 0x432b, 0x4834, 0xa2, 0xf7, 0xdc, 0xe1, 0xf1, 0x3b, 0x83, 0x4b)
  1063. #endif
  1064. namespace juce
  1065. {
  1066. //==============================================================================
  1067. struct HSTRING_PRIVATE;
  1068. typedef HSTRING_PRIVATE* HSTRING;
  1069. struct IInspectable : public IUnknown
  1070. {
  1071. JUCE_COMCALL GetIids (ULONG* ,IID**) = 0;
  1072. JUCE_COMCALL GetRuntimeClassName (HSTRING*) = 0;
  1073. JUCE_COMCALL GetTrustLevel (void*) = 0;
  1074. };
  1075. JUCE_COMCLASS (IUIViewSettingsInterop, "3694dbf9-8f68-44be-8ff5-195c98ede8a6") : public IInspectable
  1076. {
  1077. JUCE_COMCALL GetForWindow (HWND, REFIID, void**) = 0;
  1078. };
  1079. JUCE_COMCLASS (IUIViewSettings, "c63657f6-8850-470d-88f8-455e16ea2c26") : public IInspectable
  1080. {
  1081. enum UserInteractionMode
  1082. {
  1083. Mouse = 0,
  1084. Touch = 1
  1085. };
  1086. JUCE_COMCALL GetUserInteractionMode (UserInteractionMode*) = 0;
  1087. };
  1088. } // namespace juce
  1089. #ifdef __CRT_UUID_DECL
  1090. __CRT_UUID_DECL (juce::IUIViewSettingsInterop, 0x3694dbf9, 0x8f68, 0x44be, 0x8f, 0xf5, 0x19, 0x5c, 0x98, 0xed, 0xe8, 0xa6)
  1091. __CRT_UUID_DECL (juce::IUIViewSettings, 0xc63657f6, 0x8850, 0x470d, 0x88, 0xf8, 0x45, 0x5e, 0x16, 0xea, 0x2c, 0x26)
  1092. #endif
  1093. namespace juce
  1094. {
  1095. struct UWPUIViewSettings
  1096. {
  1097. UWPUIViewSettings()
  1098. {
  1099. ComBaseModule dll (L"api-ms-win-core-winrt-l1-1-0");
  1100. if (dll.h != nullptr)
  1101. {
  1102. roInitialize = (RoInitializeFuncPtr) ::GetProcAddress (dll.h, "RoInitialize");
  1103. roGetActivationFactory = (RoGetActivationFactoryFuncPtr) ::GetProcAddress (dll.h, "RoGetActivationFactory");
  1104. createHString = (WindowsCreateStringFuncPtr) ::GetProcAddress (dll.h, "WindowsCreateString");
  1105. deleteHString = (WindowsDeleteStringFuncPtr) ::GetProcAddress (dll.h, "WindowsDeleteString");
  1106. if (roInitialize == nullptr || roGetActivationFactory == nullptr
  1107. || createHString == nullptr || deleteHString == nullptr)
  1108. return;
  1109. auto status = roInitialize (1);
  1110. if (status != S_OK && status != S_FALSE && (unsigned) status != 0x80010106L)
  1111. return;
  1112. LPCWSTR uwpClassName = L"Windows.UI.ViewManagement.UIViewSettings";
  1113. HSTRING uwpClassId = nullptr;
  1114. if (createHString (uwpClassName, (::UINT32) wcslen (uwpClassName), &uwpClassId) != S_OK
  1115. || uwpClassId == nullptr)
  1116. return;
  1117. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  1118. status = roGetActivationFactory (uwpClassId, __uuidof (IUIViewSettingsInterop),
  1119. (void**) viewSettingsInterop.resetAndGetPointerAddress());
  1120. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  1121. deleteHString (uwpClassId);
  1122. if (status != S_OK || viewSettingsInterop == nullptr)
  1123. return;
  1124. // move dll into member var
  1125. comBaseDLL = std::move (dll);
  1126. }
  1127. }
  1128. private:
  1129. //==============================================================================
  1130. struct ComBaseModule
  1131. {
  1132. ComBaseModule() = default;
  1133. ComBaseModule (LPCWSTR libraryName) : h (::LoadLibrary (libraryName)) {}
  1134. ComBaseModule (ComBaseModule&& o) : h (o.h) { o.h = nullptr; }
  1135. ~ComBaseModule() { release(); }
  1136. void release() { if (h != nullptr) ::FreeLibrary (h); h = nullptr; }
  1137. ComBaseModule& operator= (ComBaseModule&& o) { release(); h = o.h; o.h = nullptr; return *this; }
  1138. HMODULE h = {};
  1139. };
  1140. using RoInitializeFuncPtr = HRESULT (WINAPI*) (int);
  1141. using RoGetActivationFactoryFuncPtr = HRESULT (WINAPI*) (HSTRING, REFIID, void**);
  1142. using WindowsCreateStringFuncPtr = HRESULT (WINAPI*) (LPCWSTR,UINT32, HSTRING*);
  1143. using WindowsDeleteStringFuncPtr = HRESULT (WINAPI*) (HSTRING);
  1144. ComBaseModule comBaseDLL;
  1145. ComSmartPtr<IUIViewSettingsInterop> viewSettingsInterop;
  1146. RoInitializeFuncPtr roInitialize;
  1147. RoGetActivationFactoryFuncPtr roGetActivationFactory;
  1148. WindowsCreateStringFuncPtr createHString;
  1149. WindowsDeleteStringFuncPtr deleteHString;
  1150. };
  1151. //==============================================================================
  1152. static HMONITOR getMonitorFromOutput (ComSmartPtr<IDXGIOutput> output)
  1153. {
  1154. DXGI_OUTPUT_DESC desc = {};
  1155. return (FAILED (output->GetDesc (&desc)) || ! desc.AttachedToDesktop)
  1156. ? nullptr
  1157. : desc.Monitor;
  1158. }
  1159. using VBlankListener = ComponentPeer::VBlankListener;
  1160. //==============================================================================
  1161. class VSyncThread final : private Thread,
  1162. private AsyncUpdater
  1163. {
  1164. public:
  1165. VSyncThread (ComSmartPtr<IDXGIOutput> out,
  1166. HMONITOR mon,
  1167. VBlankListener& listener)
  1168. : Thread ("VSyncThread"),
  1169. output (out),
  1170. monitor (mon)
  1171. {
  1172. listeners.push_back (listener);
  1173. startThread (Priority::highest);
  1174. }
  1175. ~VSyncThread() override
  1176. {
  1177. stopThread (-1);
  1178. cancelPendingUpdate();
  1179. }
  1180. void updateMonitor()
  1181. {
  1182. monitor = getMonitorFromOutput (output);
  1183. }
  1184. HMONITOR getMonitor() const noexcept { return monitor; }
  1185. void addListener (VBlankListener& listener)
  1186. {
  1187. listeners.push_back (listener);
  1188. }
  1189. bool removeListener (const VBlankListener& listener)
  1190. {
  1191. auto it = std::find_if (listeners.cbegin(),
  1192. listeners.cend(),
  1193. [&listener] (const auto& l) { return &(l.get()) == &listener; });
  1194. if (it != listeners.cend())
  1195. {
  1196. listeners.erase (it);
  1197. return true;
  1198. }
  1199. return false;
  1200. }
  1201. bool hasNoListeners() const noexcept
  1202. {
  1203. return listeners.empty();
  1204. }
  1205. bool hasListener (const VBlankListener& listener) const noexcept
  1206. {
  1207. return std::any_of (listeners.cbegin(),
  1208. listeners.cend(),
  1209. [&listener] (const auto& l) { return &(l.get()) == &listener; });
  1210. }
  1211. private:
  1212. //==============================================================================
  1213. void run() override
  1214. {
  1215. while (! threadShouldExit())
  1216. {
  1217. if (output->WaitForVBlank() == S_OK)
  1218. triggerAsyncUpdate();
  1219. else
  1220. Thread::sleep (1);
  1221. }
  1222. }
  1223. void handleAsyncUpdate() override
  1224. {
  1225. for (auto& listener : listeners)
  1226. listener.get().onVBlank();
  1227. }
  1228. //==============================================================================
  1229. ComSmartPtr<IDXGIOutput> output;
  1230. HMONITOR monitor = nullptr;
  1231. std::vector<std::reference_wrapper<VBlankListener>> listeners;
  1232. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VSyncThread)
  1233. JUCE_DECLARE_NON_MOVEABLE (VSyncThread)
  1234. };
  1235. //==============================================================================
  1236. class VBlankDispatcher final : public DeletedAtShutdown
  1237. {
  1238. public:
  1239. void updateDisplay (VBlankListener& listener, HMONITOR monitor)
  1240. {
  1241. if (monitor == nullptr)
  1242. {
  1243. removeListener (listener);
  1244. return;
  1245. }
  1246. auto threadWithListener = threads.end();
  1247. auto threadWithMonitor = threads.end();
  1248. for (auto it = threads.begin(); it != threads.end(); ++it)
  1249. {
  1250. if ((*it)->hasListener (listener))
  1251. threadWithListener = it;
  1252. if ((*it)->getMonitor() == monitor)
  1253. threadWithMonitor = it;
  1254. if (threadWithListener != threads.end()
  1255. && threadWithMonitor != threads.end())
  1256. {
  1257. if (threadWithListener == threadWithMonitor)
  1258. return;
  1259. (*threadWithMonitor)->addListener (listener);
  1260. // This may invalidate iterators, so be careful!
  1261. removeListener (threadWithListener, listener);
  1262. return;
  1263. }
  1264. }
  1265. if (threadWithMonitor != threads.end())
  1266. {
  1267. (*threadWithMonitor)->addListener (listener);
  1268. return;
  1269. }
  1270. if (threadWithListener != threads.end())
  1271. removeListener (threadWithListener, listener);
  1272. for (auto adapter : adapters)
  1273. {
  1274. UINT i = 0;
  1275. ComSmartPtr<IDXGIOutput> output;
  1276. while (adapter->EnumOutputs (i, output.resetAndGetPointerAddress()) != DXGI_ERROR_NOT_FOUND)
  1277. {
  1278. if (getMonitorFromOutput (output) == monitor)
  1279. {
  1280. threads.emplace_back (std::make_unique<VSyncThread> (output, monitor, listener));
  1281. return;
  1282. }
  1283. ++i;
  1284. }
  1285. }
  1286. }
  1287. void removeListener (const VBlankListener& listener)
  1288. {
  1289. for (auto it = threads.begin(); it != threads.end(); ++it)
  1290. if (removeListener (it, listener))
  1291. return;
  1292. }
  1293. void reconfigureDisplays()
  1294. {
  1295. adapters.clear();
  1296. ComSmartPtr<IDXGIFactory> factory;
  1297. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  1298. CreateDXGIFactory (__uuidof (IDXGIFactory), (void**)factory.resetAndGetPointerAddress());
  1299. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  1300. UINT i = 0;
  1301. ComSmartPtr<IDXGIAdapter> adapter;
  1302. while (factory->EnumAdapters (i, adapter.resetAndGetPointerAddress()) != DXGI_ERROR_NOT_FOUND)
  1303. {
  1304. adapters.push_back (adapter);
  1305. ++i;
  1306. }
  1307. for (auto& thread : threads)
  1308. thread->updateMonitor();
  1309. threads.erase (std::remove_if (threads.begin(),
  1310. threads.end(),
  1311. [] (const auto& thread) { return thread->getMonitor() == nullptr; }),
  1312. threads.end());
  1313. }
  1314. JUCE_DECLARE_SINGLETON_SINGLETHREADED (VBlankDispatcher, false)
  1315. private:
  1316. //==============================================================================
  1317. using Threads = std::vector<std::unique_ptr<VSyncThread>>;
  1318. VBlankDispatcher()
  1319. {
  1320. reconfigureDisplays();
  1321. }
  1322. ~VBlankDispatcher() override
  1323. {
  1324. threads.clear();
  1325. clearSingletonInstance();
  1326. }
  1327. // This may delete the corresponding thread and invalidate iterators,
  1328. // so be careful!
  1329. bool removeListener (Threads::iterator it, const VBlankListener& listener)
  1330. {
  1331. if ((*it)->removeListener (listener))
  1332. {
  1333. if ((*it)->hasNoListeners())
  1334. threads.erase (it);
  1335. return true;
  1336. }
  1337. return false;
  1338. }
  1339. //==============================================================================
  1340. std::vector<ComSmartPtr<IDXGIAdapter>> adapters;
  1341. Threads threads;
  1342. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VBlankDispatcher)
  1343. JUCE_DECLARE_NON_MOVEABLE (VBlankDispatcher)
  1344. };
  1345. JUCE_IMPLEMENT_SINGLETON (VBlankDispatcher)
  1346. //==============================================================================
  1347. class SimpleTimer final : private Timer
  1348. {
  1349. public:
  1350. SimpleTimer (int intervalMs, std::function<void()> callbackIn)
  1351. : callback (std::move (callbackIn))
  1352. {
  1353. jassert (callback);
  1354. startTimer (intervalMs);
  1355. }
  1356. ~SimpleTimer() override
  1357. {
  1358. stopTimer();
  1359. }
  1360. private:
  1361. void timerCallback() override
  1362. {
  1363. callback();
  1364. }
  1365. std::function<void()> callback;
  1366. };
  1367. //==============================================================================
  1368. class HWNDComponentPeer final : public ComponentPeer,
  1369. private VBlankListener,
  1370. private Timer
  1371. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client
  1372. , public ModifierKeyReceiver
  1373. #endif
  1374. {
  1375. public:
  1376. enum RenderingEngineType
  1377. {
  1378. softwareRenderingEngine = 0,
  1379. direct2DRenderingEngine
  1380. };
  1381. //==============================================================================
  1382. HWNDComponentPeer (Component& comp, int windowStyleFlags, HWND parent, bool nonRepainting)
  1383. : ComponentPeer (comp, windowStyleFlags),
  1384. dontRepaint (nonRepainting),
  1385. parentToAddTo (parent),
  1386. currentRenderingEngine (softwareRenderingEngine)
  1387. {
  1388. callFunctionIfNotLocked (&createWindowCallback, this);
  1389. setTitle (component.getName());
  1390. updateShadower();
  1391. getNativeRealtimeModifiers = []
  1392. {
  1393. HWNDComponentPeer::updateKeyModifiers();
  1394. int mouseMods = 0;
  1395. if (HWNDComponentPeer::isKeyDown (VK_LBUTTON)) mouseMods |= ModifierKeys::leftButtonModifier;
  1396. if (HWNDComponentPeer::isKeyDown (VK_RBUTTON)) mouseMods |= ModifierKeys::rightButtonModifier;
  1397. if (HWNDComponentPeer::isKeyDown (VK_MBUTTON)) mouseMods |= ModifierKeys::middleButtonModifier;
  1398. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (mouseMods);
  1399. return ModifierKeys::currentModifiers;
  1400. };
  1401. updateCurrentMonitorAndRefreshVBlankDispatcher();
  1402. if (parentToAddTo != nullptr)
  1403. monitorUpdateTimer.emplace (1000, [this] { updateCurrentMonitorAndRefreshVBlankDispatcher(); });
  1404. suspendResumeRegistration = ScopedSuspendResumeNotificationRegistration { hwnd };
  1405. }
  1406. ~HWNDComponentPeer() override
  1407. {
  1408. suspendResumeRegistration = {};
  1409. VBlankDispatcher::getInstance()->removeListener (*this);
  1410. // do this first to avoid messages arriving for this window before it's destroyed
  1411. JuceWindowIdentifier::setAsJUCEWindow (hwnd, false);
  1412. if (isAccessibilityActive)
  1413. WindowsAccessibility::revokeUIAMapEntriesForWindow (hwnd);
  1414. shadower = nullptr;
  1415. currentTouches.deleteAllTouchesForPeer (this);
  1416. callFunctionIfNotLocked (&destroyWindowCallback, (void*) hwnd);
  1417. if (dropTarget != nullptr)
  1418. {
  1419. dropTarget->peerIsDeleted = true;
  1420. dropTarget->Release();
  1421. dropTarget = nullptr;
  1422. }
  1423. #if JUCE_DIRECT2D
  1424. direct2DContext = nullptr;
  1425. #endif
  1426. }
  1427. //==============================================================================
  1428. void* getNativeHandle() const override { return hwnd; }
  1429. void setVisible (bool shouldBeVisible) override
  1430. {
  1431. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1432. ShowWindow (hwnd, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  1433. if (shouldBeVisible)
  1434. InvalidateRect (hwnd, nullptr, 0);
  1435. else
  1436. lastPaintTime = 0;
  1437. }
  1438. void setTitle (const String& title) override
  1439. {
  1440. // Unfortunately some ancient bits of win32 mean you can only perform this operation from the message thread.
  1441. JUCE_ASSERT_MESSAGE_THREAD
  1442. SetWindowText (hwnd, title.toWideCharPointer());
  1443. }
  1444. void repaintNowIfTransparent()
  1445. {
  1446. if (isUsingUpdateLayeredWindow() && lastPaintTime > 0 && Time::getMillisecondCounter() > lastPaintTime + 30)
  1447. handlePaintMessage();
  1448. }
  1449. void updateBorderSize()
  1450. {
  1451. WINDOWINFO info;
  1452. info.cbSize = sizeof (info);
  1453. if (GetWindowInfo (hwnd, &info))
  1454. windowBorder = BorderSize<int> (roundToInt ((info.rcClient.top - info.rcWindow.top) / scaleFactor),
  1455. roundToInt ((info.rcClient.left - info.rcWindow.left) / scaleFactor),
  1456. roundToInt ((info.rcWindow.bottom - info.rcClient.bottom) / scaleFactor),
  1457. roundToInt ((info.rcWindow.right - info.rcClient.right) / scaleFactor));
  1458. #if JUCE_DIRECT2D
  1459. if (direct2DContext != nullptr)
  1460. direct2DContext->resized();
  1461. #endif
  1462. }
  1463. void setBounds (const Rectangle<int>& bounds, bool isNowFullScreen) override
  1464. {
  1465. // If we try to set new bounds while handling an existing position change,
  1466. // Windows may get confused about our current scale and size.
  1467. // This can happen when moving a window between displays, because the mouse-move
  1468. // generator in handlePositionChanged can cause the window to move again.
  1469. if (inHandlePositionChanged)
  1470. return;
  1471. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1472. fullScreen = isNowFullScreen;
  1473. auto newBounds = windowBorder.addedTo (bounds);
  1474. if (isUsingUpdateLayeredWindow())
  1475. {
  1476. if (auto parentHwnd = GetParent (hwnd))
  1477. {
  1478. auto parentRect = convertPhysicalScreenRectangleToLogical (rectangleFromRECT (getWindowScreenRect (parentHwnd)), hwnd);
  1479. newBounds.translate (parentRect.getX(), parentRect.getY());
  1480. }
  1481. }
  1482. auto oldBounds = getBounds();
  1483. const bool hasMoved = (oldBounds.getPosition() != bounds.getPosition());
  1484. const bool hasResized = (oldBounds.getWidth() != bounds.getWidth()
  1485. || oldBounds.getHeight() != bounds.getHeight());
  1486. DWORD flags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER;
  1487. if (! hasMoved) flags |= SWP_NOMOVE;
  1488. if (! hasResized) flags |= SWP_NOSIZE;
  1489. setWindowPos (hwnd, newBounds, flags, ! inDpiChange);
  1490. if (hasResized && isValidPeer (this))
  1491. {
  1492. updateBorderSize();
  1493. repaintNowIfTransparent();
  1494. }
  1495. }
  1496. Rectangle<int> getBounds() const override
  1497. {
  1498. auto bounds = [this]
  1499. {
  1500. if (parentToAddTo == nullptr)
  1501. return convertPhysicalScreenRectangleToLogical (rectangleFromRECT (getWindowScreenRect (hwnd)), hwnd);
  1502. auto localBounds = rectangleFromRECT (getWindowClientRect (hwnd));
  1503. if (isPerMonitorDPIAwareWindow (hwnd))
  1504. return (localBounds.toDouble() / getPlatformScaleFactor()).toNearestInt();
  1505. return localBounds;
  1506. }();
  1507. return windowBorder.subtractedFrom (bounds);
  1508. }
  1509. Point<int> getScreenPosition() const
  1510. {
  1511. auto r = convertPhysicalScreenRectangleToLogical (rectangleFromRECT (getWindowScreenRect (hwnd)), hwnd);
  1512. return { r.getX() + windowBorder.getLeft(),
  1513. r.getY() + windowBorder.getTop() };
  1514. }
  1515. Point<float> localToGlobal (Point<float> relativePosition) override { return relativePosition + getScreenPosition().toFloat(); }
  1516. Point<float> globalToLocal (Point<float> screenPosition) override { return screenPosition - getScreenPosition().toFloat(); }
  1517. using ComponentPeer::localToGlobal;
  1518. using ComponentPeer::globalToLocal;
  1519. void setAlpha (float newAlpha) override
  1520. {
  1521. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1522. auto intAlpha = (uint8) jlimit (0, 255, (int) (newAlpha * 255.0f));
  1523. if (component.isOpaque())
  1524. {
  1525. if (newAlpha < 1.0f)
  1526. {
  1527. SetWindowLong (hwnd, GWL_EXSTYLE, GetWindowLong (hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
  1528. SetLayeredWindowAttributes (hwnd, RGB (0, 0, 0), intAlpha, LWA_ALPHA);
  1529. }
  1530. else
  1531. {
  1532. SetWindowLong (hwnd, GWL_EXSTYLE, GetWindowLong (hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED);
  1533. RedrawWindow (hwnd, nullptr, nullptr, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
  1534. }
  1535. }
  1536. else
  1537. {
  1538. updateLayeredWindowAlpha = intAlpha;
  1539. component.repaint();
  1540. }
  1541. }
  1542. void setMinimised (bool shouldBeMinimised) override
  1543. {
  1544. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1545. if (shouldBeMinimised != isMinimised())
  1546. ShowWindow (hwnd, shouldBeMinimised ? SW_MINIMIZE : SW_RESTORE);
  1547. }
  1548. bool isMinimised() const override
  1549. {
  1550. WINDOWPLACEMENT wp;
  1551. wp.length = sizeof (WINDOWPLACEMENT);
  1552. GetWindowPlacement (hwnd, &wp);
  1553. return wp.showCmd == SW_SHOWMINIMIZED;
  1554. }
  1555. void setFullScreen (bool shouldBeFullScreen) override
  1556. {
  1557. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1558. setMinimised (false);
  1559. if (isFullScreen() != shouldBeFullScreen)
  1560. {
  1561. if (constrainer != nullptr)
  1562. constrainer->resizeStart();
  1563. fullScreen = shouldBeFullScreen;
  1564. const WeakReference<Component> deletionChecker (&component);
  1565. if (! fullScreen)
  1566. {
  1567. auto boundsCopy = lastNonFullscreenBounds;
  1568. if (hasTitleBar())
  1569. ShowWindow (hwnd, SW_SHOWNORMAL);
  1570. if (! boundsCopy.isEmpty())
  1571. setBounds (detail::ScalingHelpers::scaledScreenPosToUnscaled (component, boundsCopy), false);
  1572. }
  1573. else
  1574. {
  1575. if (hasTitleBar())
  1576. ShowWindow (hwnd, SW_SHOWMAXIMIZED);
  1577. else
  1578. SendMessageW (hwnd, WM_SETTINGCHANGE, 0, 0);
  1579. }
  1580. if (deletionChecker != nullptr)
  1581. handleMovedOrResized();
  1582. if (constrainer != nullptr)
  1583. constrainer->resizeEnd();
  1584. }
  1585. }
  1586. bool isFullScreen() const override
  1587. {
  1588. if (! hasTitleBar())
  1589. return fullScreen;
  1590. WINDOWPLACEMENT wp;
  1591. wp.length = sizeof (wp);
  1592. GetWindowPlacement (hwnd, &wp);
  1593. return wp.showCmd == SW_SHOWMAXIMIZED;
  1594. }
  1595. bool contains (Point<int> localPos, bool trueIfInAChildWindow) const override
  1596. {
  1597. auto r = convertPhysicalScreenRectangleToLogical (rectangleFromRECT (getWindowScreenRect (hwnd)), hwnd);
  1598. if (! r.withZeroOrigin().contains (localPos))
  1599. return false;
  1600. auto w = WindowFromPoint (POINTFromPoint (convertLogicalScreenPointToPhysical (localPos + getScreenPosition(),
  1601. hwnd)));
  1602. return w == hwnd || (trueIfInAChildWindow && (IsChild (hwnd, w) != 0));
  1603. }
  1604. OptionalBorderSize getFrameSizeIfPresent() const override
  1605. {
  1606. return ComponentPeer::OptionalBorderSize { windowBorder };
  1607. }
  1608. BorderSize<int> getFrameSize() const override
  1609. {
  1610. return windowBorder;
  1611. }
  1612. bool setAlwaysOnTop (bool alwaysOnTop) override
  1613. {
  1614. const bool oldDeactivate = shouldDeactivateTitleBar;
  1615. shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);
  1616. setWindowZOrder (hwnd, alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST);
  1617. shouldDeactivateTitleBar = oldDeactivate;
  1618. if (shadower != nullptr)
  1619. handleBroughtToFront();
  1620. return true;
  1621. }
  1622. void toFront (bool makeActive) override
  1623. {
  1624. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1625. setMinimised (false);
  1626. const bool oldDeactivate = shouldDeactivateTitleBar;
  1627. shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);
  1628. callFunctionIfNotLocked (makeActive ? &toFrontCallback1 : &toFrontCallback2, hwnd);
  1629. shouldDeactivateTitleBar = oldDeactivate;
  1630. if (! makeActive)
  1631. {
  1632. // in this case a broughttofront call won't have occurred, so do it now..
  1633. handleBroughtToFront();
  1634. }
  1635. }
  1636. void toBehind (ComponentPeer* other) override
  1637. {
  1638. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1639. if (auto* otherPeer = dynamic_cast<HWNDComponentPeer*> (other))
  1640. {
  1641. setMinimised (false);
  1642. // Must be careful not to try to put a topmost window behind a normal one, or Windows
  1643. // promotes the normal one to be topmost!
  1644. if (component.isAlwaysOnTop() == otherPeer->getComponent().isAlwaysOnTop())
  1645. setWindowZOrder (hwnd, otherPeer->hwnd);
  1646. else if (otherPeer->getComponent().isAlwaysOnTop())
  1647. setWindowZOrder (hwnd, HWND_TOP);
  1648. }
  1649. else
  1650. {
  1651. jassertfalse; // wrong type of window?
  1652. }
  1653. }
  1654. bool isFocused() const override
  1655. {
  1656. return callFunctionIfNotLocked (&getFocusCallback, nullptr) == (void*) hwnd;
  1657. }
  1658. void grabFocus() override
  1659. {
  1660. const ScopedValueSetter<bool> scope (shouldIgnoreModalDismiss, true);
  1661. const bool oldDeactivate = shouldDeactivateTitleBar;
  1662. shouldDeactivateTitleBar = ((styleFlags & windowIsTemporary) == 0);
  1663. callFunctionIfNotLocked (&setFocusCallback, hwnd);
  1664. shouldDeactivateTitleBar = oldDeactivate;
  1665. }
  1666. void textInputRequired (Point<int>, TextInputTarget&) override
  1667. {
  1668. if (! hasCreatedCaret)
  1669. hasCreatedCaret = CreateCaret (hwnd, (HBITMAP) 1, 0, 0);
  1670. if (hasCreatedCaret)
  1671. {
  1672. SetCaretPos (0, 0);
  1673. ShowCaret (hwnd);
  1674. }
  1675. ImmAssociateContext (hwnd, nullptr);
  1676. // MSVC complains about the nullptr argument, but the docs for this
  1677. // function say that the second argument is ignored when the third
  1678. // argument is IACE_DEFAULT.
  1679. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6387)
  1680. ImmAssociateContextEx (hwnd, nullptr, IACE_DEFAULT);
  1681. JUCE_END_IGNORE_WARNINGS_MSVC
  1682. }
  1683. void closeInputMethodContext() override
  1684. {
  1685. imeHandler.handleSetContext (hwnd, false);
  1686. }
  1687. void dismissPendingTextInput() override
  1688. {
  1689. closeInputMethodContext();
  1690. ImmAssociateContext (hwnd, nullptr);
  1691. if (std::exchange (hasCreatedCaret, false))
  1692. DestroyCaret();
  1693. }
  1694. void repaint (const Rectangle<int>& area) override
  1695. {
  1696. deferredRepaints.add ((area.toDouble() * getPlatformScaleFactor()).getSmallestIntegerContainer());
  1697. }
  1698. void dispatchDeferredRepaints()
  1699. {
  1700. for (auto deferredRect : deferredRepaints)
  1701. {
  1702. auto r = RECTFromRectangle (deferredRect);
  1703. InvalidateRect (hwnd, &r, FALSE);
  1704. }
  1705. deferredRepaints.clear();
  1706. }
  1707. void performAnyPendingRepaintsNow() override
  1708. {
  1709. if (component.isVisible())
  1710. {
  1711. dispatchDeferredRepaints();
  1712. WeakReference<Component> localRef (&component);
  1713. MSG m;
  1714. if (isUsingUpdateLayeredWindow() || PeekMessage (&m, hwnd, WM_PAINT, WM_PAINT, PM_REMOVE))
  1715. if (localRef != nullptr) // (the PeekMessage call can dispatch messages, which may delete this comp)
  1716. handlePaintMessage();
  1717. }
  1718. }
  1719. //==============================================================================
  1720. void onVBlank() override
  1721. {
  1722. vBlankListeners.call ([] (auto& l) { l.onVBlank(); });
  1723. dispatchDeferredRepaints();
  1724. }
  1725. //==============================================================================
  1726. static HWNDComponentPeer* getOwnerOfWindow (HWND h) noexcept
  1727. {
  1728. if (h != nullptr && JuceWindowIdentifier::isJUCEWindow (h))
  1729. return (HWNDComponentPeer*) GetWindowLongPtr (h, 8);
  1730. return nullptr;
  1731. }
  1732. //==============================================================================
  1733. bool isInside (HWND h) const noexcept
  1734. {
  1735. return GetAncestor (hwnd, GA_ROOT) == h;
  1736. }
  1737. //==============================================================================
  1738. static bool isKeyDown (const int key) noexcept { return (GetAsyncKeyState (key) & 0x8000) != 0; }
  1739. static void updateKeyModifiers() noexcept
  1740. {
  1741. int keyMods = 0;
  1742. if (isKeyDown (VK_SHIFT)) keyMods |= ModifierKeys::shiftModifier;
  1743. if (isKeyDown (VK_CONTROL)) keyMods |= ModifierKeys::ctrlModifier;
  1744. if (isKeyDown (VK_MENU)) keyMods |= ModifierKeys::altModifier;
  1745. // workaround: Windows maps AltGr to left-Ctrl + right-Alt.
  1746. if (isKeyDown (VK_RMENU) && !isKeyDown (VK_RCONTROL))
  1747. {
  1748. keyMods = (keyMods & ~ModifierKeys::ctrlModifier) | ModifierKeys::altModifier;
  1749. }
  1750. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withOnlyMouseButtons().withFlags (keyMods);
  1751. }
  1752. static void updateModifiersFromWParam (const WPARAM wParam)
  1753. {
  1754. int mouseMods = 0;
  1755. if (wParam & MK_LBUTTON) mouseMods |= ModifierKeys::leftButtonModifier;
  1756. if (wParam & MK_RBUTTON) mouseMods |= ModifierKeys::rightButtonModifier;
  1757. if (wParam & MK_MBUTTON) mouseMods |= ModifierKeys::middleButtonModifier;
  1758. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (mouseMods);
  1759. updateKeyModifiers();
  1760. }
  1761. //==============================================================================
  1762. bool dontRepaint;
  1763. static ModifierKeys modifiersAtLastCallback;
  1764. //==============================================================================
  1765. struct FileDropTarget final : public ComBaseClassHelper<IDropTarget>
  1766. {
  1767. FileDropTarget (HWNDComponentPeer& p) : peer (p) {}
  1768. JUCE_COMRESULT DragEnter (IDataObject* pDataObject, DWORD grfKeyState, POINTL mousePos, DWORD* pdwEffect) override
  1769. {
  1770. auto hr = updateFileList (pDataObject);
  1771. if (FAILED (hr))
  1772. return hr;
  1773. return DragOver (grfKeyState, mousePos, pdwEffect);
  1774. }
  1775. JUCE_COMRESULT DragLeave() override
  1776. {
  1777. if (peerIsDeleted)
  1778. return S_FALSE;
  1779. peer.handleDragExit (dragInfo);
  1780. return S_OK;
  1781. }
  1782. JUCE_COMRESULT DragOver (DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect) override
  1783. {
  1784. if (peerIsDeleted)
  1785. return S_FALSE;
  1786. dragInfo.position = getMousePos (mousePos).roundToInt();
  1787. *pdwEffect = peer.handleDragMove (dragInfo) ? (DWORD) DROPEFFECT_COPY
  1788. : (DWORD) DROPEFFECT_NONE;
  1789. return S_OK;
  1790. }
  1791. JUCE_COMRESULT Drop (IDataObject* pDataObject, DWORD /*grfKeyState*/, POINTL mousePos, DWORD* pdwEffect) override
  1792. {
  1793. auto hr = updateFileList (pDataObject);
  1794. if (FAILED (hr))
  1795. return hr;
  1796. dragInfo.position = getMousePos (mousePos).roundToInt();
  1797. *pdwEffect = peer.handleDragDrop (dragInfo) ? (DWORD) DROPEFFECT_COPY
  1798. : (DWORD) DROPEFFECT_NONE;
  1799. return S_OK;
  1800. }
  1801. HWNDComponentPeer& peer;
  1802. ComponentPeer::DragInfo dragInfo;
  1803. bool peerIsDeleted = false;
  1804. private:
  1805. Point<float> getMousePos (POINTL mousePos) const
  1806. {
  1807. const auto originalPos = pointFromPOINT ({ mousePos.x, mousePos.y });
  1808. const auto logicalPos = convertPhysicalScreenPointToLogical (originalPos, peer.hwnd);
  1809. return detail::ScalingHelpers::screenPosToLocalPos (peer.component, logicalPos.toFloat());
  1810. }
  1811. struct DroppedData
  1812. {
  1813. DroppedData (IDataObject* dataObject, CLIPFORMAT type)
  1814. {
  1815. FORMATETC format = { type, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  1816. if (SUCCEEDED (error = dataObject->GetData (&format, &medium)) && medium.hGlobal != nullptr)
  1817. {
  1818. dataSize = GlobalSize (medium.hGlobal);
  1819. data = GlobalLock (medium.hGlobal);
  1820. }
  1821. }
  1822. ~DroppedData()
  1823. {
  1824. if (data != nullptr && medium.hGlobal != nullptr)
  1825. GlobalUnlock (medium.hGlobal);
  1826. }
  1827. HRESULT error;
  1828. STGMEDIUM medium { TYMED_HGLOBAL, { nullptr }, nullptr };
  1829. void* data = {};
  1830. SIZE_T dataSize;
  1831. };
  1832. void parseFileList (HDROP dropFiles)
  1833. {
  1834. dragInfo.files.clearQuick();
  1835. std::vector<TCHAR> nameBuffer;
  1836. const auto numFiles = DragQueryFile (dropFiles, ~(UINT) 0, nullptr, 0);
  1837. for (UINT i = 0; i < numFiles; ++i)
  1838. {
  1839. const auto bufferSize = DragQueryFile (dropFiles, i, nullptr, 0);
  1840. nameBuffer.clear();
  1841. nameBuffer.resize (bufferSize + 1, 0); // + 1 for the null terminator
  1842. [[maybe_unused]] const auto readCharacters = DragQueryFile (dropFiles, i, nameBuffer.data(), (UINT) nameBuffer.size());
  1843. jassert (readCharacters == bufferSize);
  1844. dragInfo.files.add (String (nameBuffer.data()));
  1845. }
  1846. }
  1847. HRESULT updateFileList (IDataObject* const dataObject)
  1848. {
  1849. if (peerIsDeleted)
  1850. return S_FALSE;
  1851. dragInfo.clear();
  1852. {
  1853. DroppedData fileData (dataObject, CF_HDROP);
  1854. if (SUCCEEDED (fileData.error))
  1855. {
  1856. parseFileList (static_cast<HDROP> (fileData.data));
  1857. return S_OK;
  1858. }
  1859. }
  1860. DroppedData textData (dataObject, CF_UNICODETEXT);
  1861. if (SUCCEEDED (textData.error))
  1862. {
  1863. dragInfo.text = String (CharPointer_UTF16 ((const WCHAR*) textData.data),
  1864. CharPointer_UTF16 ((const WCHAR*) addBytesToPointer (textData.data, textData.dataSize)));
  1865. return S_OK;
  1866. }
  1867. return textData.error;
  1868. }
  1869. JUCE_DECLARE_NON_COPYABLE (FileDropTarget)
  1870. };
  1871. static bool offerKeyMessageToJUCEWindow (const MSG& msg)
  1872. {
  1873. // If this isn't a keyboard message, let the host deal with it.
  1874. constexpr UINT messages[] { WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP, WM_CHAR, WM_SYSCHAR };
  1875. if (std::find (std::begin (messages), std::end (messages), msg.message) == std::end (messages))
  1876. return false;
  1877. auto* peer = getOwnerOfWindow (msg.hwnd);
  1878. auto* focused = Component::getCurrentlyFocusedComponent();
  1879. if (focused == nullptr || peer == nullptr || focused->getPeer() != peer)
  1880. return false;
  1881. auto* hwnd = static_cast<HWND> (peer->getNativeHandle());
  1882. if (hwnd == nullptr)
  1883. return false;
  1884. ScopedThreadDPIAwarenessSetter threadDpiAwarenessSetter { hwnd };
  1885. // If we've been sent a text character, process it as text.
  1886. if (msg.message == WM_CHAR || msg.message == WM_SYSCHAR)
  1887. return peer->doKeyChar ((int) msg.wParam, msg.lParam);
  1888. // The event was a keypress, rather than a text character
  1889. if (peer->findCurrentTextInputTarget() != nullptr)
  1890. {
  1891. // If there's a focused text input target, we want to attempt "real" text input with an
  1892. // IME, and we want to prevent the host from eating keystrokes (spaces etc.).
  1893. TranslateMessage (&msg);
  1894. // TranslateMessage may post WM_CHAR back to the window, so we remove those messages
  1895. // from the queue before the host gets to see them.
  1896. // This will dispatch pending WM_CHAR messages, so we may end up reentering
  1897. // offerKeyMessageToJUCEWindow and hitting the WM_CHAR case above.
  1898. // We always return true if WM_CHAR is posted so that the keypress is not forwarded
  1899. // to the host. Otherwise, the host may call TranslateMessage again on this message,
  1900. // resulting in duplicate WM_CHAR messages being posted.
  1901. MSG peeked{};
  1902. if (PeekMessage (&peeked, hwnd, WM_CHAR, WM_DEADCHAR, PM_REMOVE)
  1903. || PeekMessage (&peeked, hwnd, WM_SYSCHAR, WM_SYSDEADCHAR, PM_REMOVE))
  1904. {
  1905. return true;
  1906. }
  1907. // If TranslateMessage didn't add a WM_CHAR to the queue, fall back to processing the
  1908. // event as a plain keypress
  1909. }
  1910. // There's no text input target, or the key event wasn't translated, so we'll just see if we
  1911. // can use the plain keystroke event
  1912. if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN)
  1913. return peer->doKeyDown (msg.wParam);
  1914. return peer->doKeyUp (msg.wParam);
  1915. }
  1916. double getPlatformScaleFactor() const noexcept override
  1917. {
  1918. #if ! JUCE_WIN_PER_MONITOR_DPI_AWARE
  1919. return 1.0;
  1920. #else
  1921. if (! isPerMonitorDPIAwareWindow (hwnd))
  1922. return 1.0;
  1923. if (auto* parentHWND = GetParent (hwnd))
  1924. {
  1925. if (auto* parentPeer = getOwnerOfWindow (parentHWND))
  1926. return parentPeer->getPlatformScaleFactor();
  1927. if (getDPIForWindow != nullptr)
  1928. return getScaleFactorForWindow (parentHWND);
  1929. }
  1930. return scaleFactor;
  1931. #endif
  1932. }
  1933. private:
  1934. HWND hwnd, parentToAddTo;
  1935. std::unique_ptr<DropShadower> shadower;
  1936. RenderingEngineType currentRenderingEngine;
  1937. #if JUCE_DIRECT2D
  1938. std::unique_ptr<Direct2DLowLevelGraphicsContext> direct2DContext;
  1939. #endif
  1940. uint32 lastPaintTime = 0;
  1941. ULONGLONG lastMagnifySize = 0;
  1942. bool fullScreen = false, isDragging = false, isMouseOver = false,
  1943. hasCreatedCaret = false, constrainerIsResizing = false;
  1944. BorderSize<int> windowBorder;
  1945. IconConverters::IconPtr currentWindowIcon;
  1946. FileDropTarget* dropTarget = nullptr;
  1947. uint8 updateLayeredWindowAlpha = 255;
  1948. UWPUIViewSettings uwpViewSettings;
  1949. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client
  1950. ModifierKeyProvider* modProvider = nullptr;
  1951. #endif
  1952. double scaleFactor = 1.0;
  1953. bool inDpiChange = 0, inHandlePositionChanged = 0;
  1954. HMONITOR currentMonitor = nullptr;
  1955. bool isAccessibilityActive = false;
  1956. //==============================================================================
  1957. static MultiTouchMapper<DWORD> currentTouches;
  1958. //==============================================================================
  1959. struct TemporaryImage final : private Timer
  1960. {
  1961. TemporaryImage() {}
  1962. Image& getImage (bool transparent, int w, int h)
  1963. {
  1964. auto format = transparent ? Image::ARGB : Image::RGB;
  1965. if ((! image.isValid()) || image.getWidth() < w || image.getHeight() < h || image.getFormat() != format)
  1966. image = Image (new WindowsBitmapImage (format, (w + 31) & ~31, (h + 31) & ~31, false));
  1967. startTimer (3000);
  1968. return image;
  1969. }
  1970. void timerCallback() override
  1971. {
  1972. stopTimer();
  1973. image = {};
  1974. }
  1975. private:
  1976. Image image;
  1977. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemporaryImage)
  1978. };
  1979. TemporaryImage offscreenImageGenerator;
  1980. //==============================================================================
  1981. class WindowClassHolder final : private DeletedAtShutdown
  1982. {
  1983. public:
  1984. WindowClassHolder()
  1985. {
  1986. // this name has to be different for each app/dll instance because otherwise poor old Windows can
  1987. // get a bit confused (even despite it not being a process-global window class).
  1988. String windowClassName ("JUCE_");
  1989. windowClassName << String::toHexString (Time::currentTimeMillis());
  1990. auto moduleHandle = (HINSTANCE) Process::getCurrentModuleInstanceHandle();
  1991. TCHAR moduleFile[1024] = {};
  1992. GetModuleFileName (moduleHandle, moduleFile, 1024);
  1993. WNDCLASSEX wcex = {};
  1994. wcex.cbSize = sizeof (wcex);
  1995. wcex.style = CS_OWNDC;
  1996. wcex.lpfnWndProc = (WNDPROC) windowProc;
  1997. wcex.lpszClassName = windowClassName.toWideCharPointer();
  1998. wcex.cbWndExtra = 32;
  1999. wcex.hInstance = moduleHandle;
  2000. for (const auto& [index, field, ptr] : { std::tuple { 0, &wcex.hIcon, &iconBig },
  2001. std::tuple { 1, &wcex.hIconSm, &iconSmall } })
  2002. {
  2003. auto iconNum = static_cast<WORD> (index);
  2004. ptr->reset (*field = ExtractAssociatedIcon (moduleHandle, moduleFile, &iconNum));
  2005. }
  2006. atom = RegisterClassEx (&wcex);
  2007. jassert (atom != 0);
  2008. isEventBlockedByModalComps = checkEventBlockedByModalComps;
  2009. }
  2010. ~WindowClassHolder()
  2011. {
  2012. if (ComponentPeer::getNumPeers() == 0)
  2013. UnregisterClass (getWindowClassName(), (HINSTANCE) Process::getCurrentModuleInstanceHandle());
  2014. clearSingletonInstance();
  2015. }
  2016. LPCTSTR getWindowClassName() const noexcept { return (LPCTSTR) (pointer_sized_uint) atom; }
  2017. JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (WindowClassHolder)
  2018. private:
  2019. ATOM atom;
  2020. static bool isHWNDBlockedByModalComponents (HWND h)
  2021. {
  2022. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  2023. if (auto* c = Desktop::getInstance().getComponent (i))
  2024. if ((! c->isCurrentlyBlockedByAnotherModalComponent())
  2025. && IsChild ((HWND) c->getWindowHandle(), h))
  2026. return false;
  2027. return true;
  2028. }
  2029. static bool checkEventBlockedByModalComps (const MSG& m)
  2030. {
  2031. if (Component::getNumCurrentlyModalComponents() == 0 || JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  2032. return false;
  2033. switch (m.message)
  2034. {
  2035. case WM_MOUSEMOVE:
  2036. case WM_NCMOUSEMOVE:
  2037. case 0x020A: /* WM_MOUSEWHEEL */
  2038. case 0x020E: /* WM_MOUSEHWHEEL */
  2039. case WM_KEYUP:
  2040. case WM_SYSKEYUP:
  2041. case WM_CHAR:
  2042. case WM_APPCOMMAND:
  2043. case WM_LBUTTONUP:
  2044. case WM_MBUTTONUP:
  2045. case WM_RBUTTONUP:
  2046. case WM_MOUSEACTIVATE:
  2047. case WM_NCMOUSEHOVER:
  2048. case WM_MOUSEHOVER:
  2049. case WM_TOUCH:
  2050. case WM_POINTERUPDATE:
  2051. case WM_NCPOINTERUPDATE:
  2052. case WM_POINTERWHEEL:
  2053. case WM_POINTERHWHEEL:
  2054. case WM_POINTERUP:
  2055. case WM_POINTERACTIVATE:
  2056. return isHWNDBlockedByModalComponents (m.hwnd);
  2057. case WM_NCLBUTTONDOWN:
  2058. case WM_NCLBUTTONDBLCLK:
  2059. case WM_NCRBUTTONDOWN:
  2060. case WM_NCRBUTTONDBLCLK:
  2061. case WM_NCMBUTTONDOWN:
  2062. case WM_NCMBUTTONDBLCLK:
  2063. case WM_LBUTTONDOWN:
  2064. case WM_LBUTTONDBLCLK:
  2065. case WM_MBUTTONDOWN:
  2066. case WM_MBUTTONDBLCLK:
  2067. case WM_RBUTTONDOWN:
  2068. case WM_RBUTTONDBLCLK:
  2069. case WM_KEYDOWN:
  2070. case WM_SYSKEYDOWN:
  2071. case WM_NCPOINTERDOWN:
  2072. case WM_POINTERDOWN:
  2073. if (isHWNDBlockedByModalComponents (m.hwnd))
  2074. {
  2075. if (auto* modal = Component::getCurrentlyModalComponent (0))
  2076. modal->inputAttemptWhenModal();
  2077. return true;
  2078. }
  2079. break;
  2080. default:
  2081. break;
  2082. }
  2083. return false;
  2084. }
  2085. IconConverters::IconPtr iconBig, iconSmall;
  2086. JUCE_DECLARE_NON_COPYABLE (WindowClassHolder)
  2087. };
  2088. //==============================================================================
  2089. static void* createWindowCallback (void* userData)
  2090. {
  2091. static_cast<HWNDComponentPeer*> (userData)->createWindow();
  2092. return nullptr;
  2093. }
  2094. void createWindow()
  2095. {
  2096. DWORD exstyle = 0;
  2097. DWORD type = WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
  2098. if (hasTitleBar())
  2099. {
  2100. type |= WS_OVERLAPPED;
  2101. if ((styleFlags & windowHasCloseButton) != 0)
  2102. {
  2103. type |= WS_SYSMENU;
  2104. }
  2105. else
  2106. {
  2107. // annoyingly, windows won't let you have a min/max button without a close button
  2108. jassert ((styleFlags & (windowHasMinimiseButton | windowHasMaximiseButton)) == 0);
  2109. }
  2110. if ((styleFlags & windowIsResizable) != 0)
  2111. type |= WS_THICKFRAME;
  2112. }
  2113. else if (parentToAddTo != nullptr)
  2114. {
  2115. type |= WS_CHILD;
  2116. }
  2117. else
  2118. {
  2119. type |= WS_POPUP | WS_SYSMENU;
  2120. }
  2121. if ((styleFlags & windowAppearsOnTaskbar) == 0)
  2122. exstyle |= WS_EX_TOOLWINDOW;
  2123. else
  2124. exstyle |= WS_EX_APPWINDOW;
  2125. if ((styleFlags & windowHasMinimiseButton) != 0) type |= WS_MINIMIZEBOX;
  2126. if ((styleFlags & windowHasMaximiseButton) != 0) type |= WS_MAXIMIZEBOX;
  2127. if ((styleFlags & windowIgnoresMouseClicks) != 0) exstyle |= WS_EX_TRANSPARENT;
  2128. if ((styleFlags & windowIsSemiTransparent) != 0) exstyle |= WS_EX_LAYERED;
  2129. hwnd = CreateWindowEx (exstyle, WindowClassHolder::getInstance()->getWindowClassName(),
  2130. L"", type, 0, 0, 0, 0, parentToAddTo, nullptr,
  2131. (HINSTANCE) Process::getCurrentModuleInstanceHandle(), nullptr);
  2132. #if JUCE_DEBUG
  2133. // The DPI-awareness context of this window and JUCE's hidden message window are different.
  2134. // You normally want these to match otherwise timer events and async messages will happen
  2135. // in a different context to normal HWND messages which can cause issues with UI scaling.
  2136. jassert (isPerMonitorDPIAwareWindow (hwnd) == isPerMonitorDPIAwareWindow (juce_messageWindowHandle)
  2137. || isInScopedDPIAwarenessDisabler());
  2138. #endif
  2139. if (hwnd != nullptr)
  2140. {
  2141. SetWindowLongPtr (hwnd, 0, 0);
  2142. SetWindowLongPtr (hwnd, 8, (LONG_PTR) this);
  2143. JuceWindowIdentifier::setAsJUCEWindow (hwnd, true);
  2144. if (dropTarget == nullptr)
  2145. {
  2146. HWNDComponentPeer* peer = nullptr;
  2147. if (dontRepaint)
  2148. peer = getOwnerOfWindow (parentToAddTo);
  2149. if (peer == nullptr)
  2150. peer = this;
  2151. dropTarget = new FileDropTarget (*peer);
  2152. }
  2153. RegisterDragDrop (hwnd, dropTarget);
  2154. if (canUseMultiTouch())
  2155. registerTouchWindow (hwnd, 0);
  2156. setDPIAwareness();
  2157. if (isPerMonitorDPIAwareThread())
  2158. scaleFactor = getScaleFactorForWindow (hwnd);
  2159. setMessageFilter();
  2160. updateBorderSize();
  2161. checkForPointerAPI();
  2162. // This is needed so that our plugin window gets notified of WM_SETTINGCHANGE messages
  2163. // and can respond to display scale changes
  2164. if (! JUCEApplication::isStandaloneApp())
  2165. settingChangeCallback = ComponentPeer::forceDisplayUpdate;
  2166. // Calling this function here is (for some reason) necessary to make Windows
  2167. // correctly enable the menu items that we specify in the wm_initmenu message.
  2168. GetSystemMenu (hwnd, false);
  2169. auto alpha = component.getAlpha();
  2170. if (alpha < 1.0f)
  2171. setAlpha (alpha);
  2172. }
  2173. else
  2174. {
  2175. TCHAR messageBuffer[256] = {};
  2176. FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  2177. nullptr, GetLastError(), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
  2178. messageBuffer, (DWORD) numElementsInArray (messageBuffer) - 1, nullptr);
  2179. DBG (messageBuffer);
  2180. jassertfalse;
  2181. }
  2182. }
  2183. static BOOL CALLBACK revokeChildDragDropCallback (HWND hwnd, LPARAM) { RevokeDragDrop (hwnd); return TRUE; }
  2184. static void* destroyWindowCallback (void* handle)
  2185. {
  2186. auto hwnd = reinterpret_cast<HWND> (handle);
  2187. if (IsWindow (hwnd))
  2188. {
  2189. RevokeDragDrop (hwnd);
  2190. // NB: we need to do this before DestroyWindow() as child HWNDs will be invalid after
  2191. EnumChildWindows (hwnd, revokeChildDragDropCallback, 0);
  2192. DestroyWindow (hwnd);
  2193. }
  2194. return nullptr;
  2195. }
  2196. static void* toFrontCallback1 (void* h)
  2197. {
  2198. BringWindowToTop ((HWND) h);
  2199. return nullptr;
  2200. }
  2201. static void* toFrontCallback2 (void* h)
  2202. {
  2203. setWindowZOrder ((HWND) h, HWND_TOP);
  2204. return nullptr;
  2205. }
  2206. static void* setFocusCallback (void* h)
  2207. {
  2208. SetFocus ((HWND) h);
  2209. return nullptr;
  2210. }
  2211. static void* getFocusCallback (void*)
  2212. {
  2213. return GetFocus();
  2214. }
  2215. bool isUsingUpdateLayeredWindow() const
  2216. {
  2217. return ! component.isOpaque();
  2218. }
  2219. bool hasTitleBar() const noexcept { return (styleFlags & windowHasTitleBar) != 0; }
  2220. void updateShadower()
  2221. {
  2222. if (! component.isCurrentlyModal() && (styleFlags & windowHasDropShadow) != 0
  2223. && ((! hasTitleBar()) || SystemStats::getOperatingSystemType() < SystemStats::WinVista))
  2224. {
  2225. shadower = component.getLookAndFeel().createDropShadowerForComponent (component);
  2226. if (shadower != nullptr)
  2227. shadower->setOwner (&component);
  2228. }
  2229. }
  2230. void setIcon (const Image& newIcon) override
  2231. {
  2232. if (IconConverters::IconPtr hicon { IconConverters::createHICONFromImage (newIcon, TRUE, 0, 0) })
  2233. {
  2234. SendMessage (hwnd, WM_SETICON, ICON_BIG, reinterpret_cast<LPARAM> (hicon.get()));
  2235. SendMessage (hwnd, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM> (hicon.get()));
  2236. currentWindowIcon = std::move (hicon);
  2237. }
  2238. }
  2239. void setMessageFilter()
  2240. {
  2241. using ChangeWindowMessageFilterExFunc = BOOL (WINAPI*) (HWND, UINT, DWORD, PVOID);
  2242. static auto changeMessageFilter = (ChangeWindowMessageFilterExFunc) getUser32Function ("ChangeWindowMessageFilterEx");
  2243. if (changeMessageFilter != nullptr)
  2244. {
  2245. changeMessageFilter (hwnd, WM_DROPFILES, 1 /*MSGFLT_ALLOW*/, nullptr);
  2246. changeMessageFilter (hwnd, WM_COPYDATA, 1 /*MSGFLT_ALLOW*/, nullptr);
  2247. changeMessageFilter (hwnd, 0x49, 1 /*MSGFLT_ALLOW*/, nullptr);
  2248. }
  2249. }
  2250. struct ChildWindowClippingInfo
  2251. {
  2252. HDC dc;
  2253. HWNDComponentPeer* peer;
  2254. RectangleList<int>* clip;
  2255. Point<int> origin;
  2256. int savedDC;
  2257. };
  2258. static BOOL CALLBACK clipChildWindowCallback (HWND hwnd, LPARAM context)
  2259. {
  2260. if (IsWindowVisible (hwnd))
  2261. {
  2262. auto& info = *(ChildWindowClippingInfo*) context;
  2263. if (GetParent (hwnd) == info.peer->hwnd)
  2264. {
  2265. auto clip = rectangleFromRECT (getWindowClientRect (hwnd));
  2266. info.clip->subtract (clip - info.origin);
  2267. if (info.savedDC == 0)
  2268. info.savedDC = SaveDC (info.dc);
  2269. ExcludeClipRect (info.dc, clip.getX(), clip.getY(), clip.getRight(), clip.getBottom());
  2270. }
  2271. }
  2272. return TRUE;
  2273. }
  2274. //==============================================================================
  2275. void handlePaintMessage()
  2276. {
  2277. #if JUCE_DIRECT2D
  2278. if (direct2DContext != nullptr)
  2279. {
  2280. RECT r;
  2281. if (GetUpdateRect (hwnd, &r, false))
  2282. {
  2283. direct2DContext->start();
  2284. direct2DContext->clipToRectangle (convertPhysicalScreenRectangleToLogical (rectangleFromRECT (r), hwnd));
  2285. handlePaint (*direct2DContext);
  2286. direct2DContext->end();
  2287. ValidateRect (hwnd, &r);
  2288. }
  2289. }
  2290. else
  2291. #endif
  2292. {
  2293. HRGN rgn = CreateRectRgn (0, 0, 0, 0);
  2294. const int regionType = GetUpdateRgn (hwnd, rgn, false);
  2295. PAINTSTRUCT paintStruct;
  2296. HDC dc = BeginPaint (hwnd, &paintStruct); // Note this can immediately generate a WM_NCPAINT
  2297. // message and become re-entrant, but that's OK
  2298. // if something in a paint handler calls, e.g. a message box, this can become reentrant and
  2299. // corrupt the image it's using to paint into, so do a check here.
  2300. static bool reentrant = false;
  2301. if (! reentrant)
  2302. {
  2303. const ScopedValueSetter<bool> setter (reentrant, true, false);
  2304. if (dontRepaint)
  2305. component.handleCommandMessage (0); // (this triggers a repaint in the openGL context)
  2306. else
  2307. performPaint (dc, rgn, regionType, paintStruct);
  2308. }
  2309. DeleteObject (rgn);
  2310. EndPaint (hwnd, &paintStruct);
  2311. #if JUCE_MSVC
  2312. _fpreset(); // because some graphics cards can unmask FP exceptions
  2313. #endif
  2314. }
  2315. lastPaintTime = Time::getMillisecondCounter();
  2316. }
  2317. void performPaint (HDC dc, HRGN rgn, int regionType, PAINTSTRUCT& paintStruct)
  2318. {
  2319. int x = paintStruct.rcPaint.left;
  2320. int y = paintStruct.rcPaint.top;
  2321. int w = paintStruct.rcPaint.right - x;
  2322. int h = paintStruct.rcPaint.bottom - y;
  2323. const bool transparent = isUsingUpdateLayeredWindow();
  2324. if (transparent)
  2325. {
  2326. // it's not possible to have a transparent window with a title bar at the moment!
  2327. jassert (! hasTitleBar());
  2328. auto r = getWindowScreenRect (hwnd);
  2329. x = y = 0;
  2330. w = r.right - r.left;
  2331. h = r.bottom - r.top;
  2332. }
  2333. if (w > 0 && h > 0)
  2334. {
  2335. Image& offscreenImage = offscreenImageGenerator.getImage (transparent, w, h);
  2336. RectangleList<int> contextClip;
  2337. const Rectangle<int> clipBounds (w, h);
  2338. bool needToPaintAll = true;
  2339. if (regionType == COMPLEXREGION && ! transparent)
  2340. {
  2341. HRGN clipRgn = CreateRectRgnIndirect (&paintStruct.rcPaint);
  2342. CombineRgn (rgn, rgn, clipRgn, RGN_AND);
  2343. DeleteObject (clipRgn);
  2344. std::aligned_storage_t<8192, alignof (RGNDATA)> rgnData;
  2345. const DWORD res = GetRegionData (rgn, sizeof (rgnData), (RGNDATA*) &rgnData);
  2346. if (res > 0 && res <= sizeof (rgnData))
  2347. {
  2348. const RGNDATAHEADER* const hdr = &(((const RGNDATA*) &rgnData)->rdh);
  2349. if (hdr->iType == RDH_RECTANGLES
  2350. && hdr->rcBound.right - hdr->rcBound.left >= w
  2351. && hdr->rcBound.bottom - hdr->rcBound.top >= h)
  2352. {
  2353. needToPaintAll = false;
  2354. auto rects = unalignedPointerCast<const RECT*> ((char*) &rgnData + sizeof (RGNDATAHEADER));
  2355. for (int i = (int) ((RGNDATA*) &rgnData)->rdh.nCount; --i >= 0;)
  2356. {
  2357. if (rects->right <= x + w && rects->bottom <= y + h)
  2358. {
  2359. const int cx = jmax (x, (int) rects->left);
  2360. contextClip.addWithoutMerging (Rectangle<int> (cx - x, rects->top - y,
  2361. rects->right - cx, rects->bottom - rects->top)
  2362. .getIntersection (clipBounds));
  2363. }
  2364. else
  2365. {
  2366. needToPaintAll = true;
  2367. break;
  2368. }
  2369. ++rects;
  2370. }
  2371. }
  2372. }
  2373. }
  2374. if (needToPaintAll)
  2375. {
  2376. contextClip.clear();
  2377. contextClip.addWithoutMerging (Rectangle<int> (w, h));
  2378. }
  2379. ChildWindowClippingInfo childClipInfo = { dc, this, &contextClip, Point<int> (x, y), 0 };
  2380. EnumChildWindows (hwnd, clipChildWindowCallback, (LPARAM) &childClipInfo);
  2381. if (! contextClip.isEmpty())
  2382. {
  2383. if (transparent)
  2384. for (auto& i : contextClip)
  2385. offscreenImage.clear (i);
  2386. {
  2387. auto context = component.getLookAndFeel()
  2388. .createGraphicsContext (offscreenImage, { -x, -y }, contextClip);
  2389. context->addTransform (AffineTransform::scale ((float) getPlatformScaleFactor()));
  2390. handlePaint (*context);
  2391. }
  2392. static_cast<WindowsBitmapImage*> (offscreenImage.getPixelData())
  2393. ->blitToWindow (hwnd, dc, transparent, x, y, updateLayeredWindowAlpha);
  2394. }
  2395. if (childClipInfo.savedDC != 0)
  2396. RestoreDC (dc, childClipInfo.savedDC);
  2397. }
  2398. }
  2399. //==============================================================================
  2400. void doMouseEvent (Point<float> position, float pressure, float orientation = 0.0f, ModifierKeys mods = ModifierKeys::currentModifiers)
  2401. {
  2402. handleMouseEvent (MouseInputSource::InputSourceType::mouse, position, mods, pressure, orientation, getMouseEventTime());
  2403. }
  2404. StringArray getAvailableRenderingEngines() override
  2405. {
  2406. StringArray s ("Software Renderer");
  2407. #if JUCE_DIRECT2D
  2408. if (SystemStats::getOperatingSystemType() >= SystemStats::Windows7)
  2409. s.add ("Direct2D");
  2410. #endif
  2411. return s;
  2412. }
  2413. int getCurrentRenderingEngine() const override { return currentRenderingEngine; }
  2414. #if JUCE_DIRECT2D
  2415. void updateDirect2DContext()
  2416. {
  2417. if (currentRenderingEngine != direct2DRenderingEngine)
  2418. direct2DContext = nullptr;
  2419. else if (direct2DContext == nullptr)
  2420. direct2DContext.reset (new Direct2DLowLevelGraphicsContext (hwnd));
  2421. }
  2422. #endif
  2423. void setCurrentRenderingEngine ([[maybe_unused]] int index) override
  2424. {
  2425. #if JUCE_DIRECT2D
  2426. if (getAvailableRenderingEngines().size() > 1)
  2427. {
  2428. currentRenderingEngine = index == 1 ? direct2DRenderingEngine : softwareRenderingEngine;
  2429. updateDirect2DContext();
  2430. repaint (component.getLocalBounds());
  2431. }
  2432. #endif
  2433. }
  2434. static uint32 getMinTimeBetweenMouseMoves()
  2435. {
  2436. if (SystemStats::getOperatingSystemType() >= SystemStats::WinVista)
  2437. return 0;
  2438. return 1000 / 60; // Throttling the incoming mouse-events seems to still be needed in XP..
  2439. }
  2440. bool isTouchEvent() noexcept
  2441. {
  2442. if (registerTouchWindow == nullptr)
  2443. return false;
  2444. // Relevant info about touch/pen detection flags:
  2445. // https://msdn.microsoft.com/en-us/library/windows/desktop/ms703320(v=vs.85).aspx
  2446. // http://www.petertissen.de/?p=4
  2447. return ((uint32_t) GetMessageExtraInfo() & 0xFFFFFF80 /*SIGNATURE_MASK*/) == 0xFF515780 /*MI_WP_SIGNATURE*/;
  2448. }
  2449. static bool areOtherTouchSourcesActive()
  2450. {
  2451. for (auto& ms : Desktop::getInstance().getMouseSources())
  2452. if (ms.isDragging() && (ms.getType() == MouseInputSource::InputSourceType::touch
  2453. || ms.getType() == MouseInputSource::InputSourceType::pen))
  2454. return true;
  2455. return false;
  2456. }
  2457. void doMouseMove (Point<float> position, bool isMouseDownEvent)
  2458. {
  2459. ModifierKeys modsToSend (ModifierKeys::currentModifiers);
  2460. // this will be handled by WM_TOUCH
  2461. if (isTouchEvent() || areOtherTouchSourcesActive())
  2462. return;
  2463. if (! isMouseOver)
  2464. {
  2465. isMouseOver = true;
  2466. // This avoids a rare stuck-button problem when focus is lost unexpectedly, but must
  2467. // not be called as part of a move, in case it's actually a mouse-drag from another
  2468. // app which ends up here when we get focus before the mouse is released..
  2469. if (isMouseDownEvent)
  2470. NullCheckedInvocation::invoke (getNativeRealtimeModifiers);
  2471. updateKeyModifiers();
  2472. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client
  2473. if (modProvider != nullptr)
  2474. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withFlags (modProvider->getWin32Modifiers());
  2475. #endif
  2476. TRACKMOUSEEVENT tme;
  2477. tme.cbSize = sizeof (tme);
  2478. tme.dwFlags = TME_LEAVE;
  2479. tme.hwndTrack = hwnd;
  2480. tme.dwHoverTime = 0;
  2481. if (! TrackMouseEvent (&tme))
  2482. jassertfalse;
  2483. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  2484. }
  2485. else if (! isDragging)
  2486. {
  2487. if (! contains (position.roundToInt(), false))
  2488. return;
  2489. }
  2490. static uint32 lastMouseTime = 0;
  2491. static auto minTimeBetweenMouses = getMinTimeBetweenMouseMoves();
  2492. auto now = Time::getMillisecondCounter();
  2493. if (! Desktop::getInstance().getMainMouseSource().isDragging())
  2494. modsToSend = modsToSend.withoutMouseButtons();
  2495. if (now >= lastMouseTime + minTimeBetweenMouses)
  2496. {
  2497. lastMouseTime = now;
  2498. doMouseEvent (position, MouseInputSource::defaultPressure,
  2499. MouseInputSource::defaultOrientation, modsToSend);
  2500. }
  2501. }
  2502. void doMouseDown (Point<float> position, const WPARAM wParam)
  2503. {
  2504. // this will be handled by WM_TOUCH
  2505. if (isTouchEvent() || areOtherTouchSourcesActive())
  2506. return;
  2507. if (GetCapture() != hwnd)
  2508. SetCapture (hwnd);
  2509. doMouseMove (position, true);
  2510. if (isValidPeer (this))
  2511. {
  2512. updateModifiersFromWParam (wParam);
  2513. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client
  2514. if (modProvider != nullptr)
  2515. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withFlags (modProvider->getWin32Modifiers());
  2516. #endif
  2517. isDragging = true;
  2518. doMouseEvent (position, MouseInputSource::defaultPressure);
  2519. }
  2520. }
  2521. void doMouseUp (Point<float> position, const WPARAM wParam)
  2522. {
  2523. // this will be handled by WM_TOUCH
  2524. if (isTouchEvent() || areOtherTouchSourcesActive())
  2525. return;
  2526. updateModifiersFromWParam (wParam);
  2527. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client
  2528. if (modProvider != nullptr)
  2529. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withFlags (modProvider->getWin32Modifiers());
  2530. #endif
  2531. const bool wasDragging = isDragging;
  2532. isDragging = false;
  2533. // release the mouse capture if the user has released all buttons
  2534. if ((wParam & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON)) == 0 && hwnd == GetCapture())
  2535. ReleaseCapture();
  2536. // NB: under some circumstances (e.g. double-clicking a native title bar), a mouse-up can
  2537. // arrive without a mouse-down, so in that case we need to avoid sending a message.
  2538. if (wasDragging)
  2539. doMouseEvent (position, MouseInputSource::defaultPressure);
  2540. }
  2541. void doCaptureChanged()
  2542. {
  2543. if (constrainerIsResizing)
  2544. {
  2545. if (constrainer != nullptr)
  2546. constrainer->resizeEnd();
  2547. constrainerIsResizing = false;
  2548. }
  2549. if (isDragging)
  2550. doMouseUp (getCurrentMousePos(), (WPARAM) 0);
  2551. }
  2552. void doMouseExit()
  2553. {
  2554. isMouseOver = false;
  2555. if (! areOtherTouchSourcesActive())
  2556. doMouseEvent (getCurrentMousePos(), MouseInputSource::defaultPressure);
  2557. }
  2558. ComponentPeer* findPeerUnderMouse (Point<float>& localPos)
  2559. {
  2560. auto currentMousePos = getPOINTFromLParam ((LPARAM) GetMessagePos());
  2561. // Because Windows stupidly sends all wheel events to the window with the keyboard
  2562. // focus, we have to redirect them here according to the mouse pos..
  2563. auto* peer = getOwnerOfWindow (WindowFromPoint (currentMousePos));
  2564. if (peer == nullptr)
  2565. peer = this;
  2566. localPos = peer->globalToLocal (convertPhysicalScreenPointToLogical (pointFromPOINT (currentMousePos), hwnd).toFloat());
  2567. return peer;
  2568. }
  2569. static MouseInputSource::InputSourceType getPointerType (WPARAM wParam)
  2570. {
  2571. if (getPointerTypeFunction != nullptr)
  2572. {
  2573. POINTER_INPUT_TYPE pointerType;
  2574. if (getPointerTypeFunction (GET_POINTERID_WPARAM (wParam), &pointerType))
  2575. {
  2576. if (pointerType == 2)
  2577. return MouseInputSource::InputSourceType::touch;
  2578. if (pointerType == 3)
  2579. return MouseInputSource::InputSourceType::pen;
  2580. }
  2581. }
  2582. return MouseInputSource::InputSourceType::mouse;
  2583. }
  2584. void doMouseWheel (const WPARAM wParam, const bool isVertical)
  2585. {
  2586. updateKeyModifiers();
  2587. const float amount = jlimit (-1000.0f, 1000.0f, 0.5f * (short) HIWORD (wParam));
  2588. MouseWheelDetails wheel;
  2589. wheel.deltaX = isVertical ? 0.0f : amount / -256.0f;
  2590. wheel.deltaY = isVertical ? amount / 256.0f : 0.0f;
  2591. wheel.isReversed = false;
  2592. wheel.isSmooth = false;
  2593. wheel.isInertial = false;
  2594. Point<float> localPos;
  2595. if (auto* peer = findPeerUnderMouse (localPos))
  2596. peer->handleMouseWheel (getPointerType (wParam), localPos, getMouseEventTime(), wheel);
  2597. }
  2598. bool doGestureEvent (LPARAM lParam)
  2599. {
  2600. GESTUREINFO gi;
  2601. zerostruct (gi);
  2602. gi.cbSize = sizeof (gi);
  2603. if (getGestureInfo != nullptr && getGestureInfo ((HGESTUREINFO) lParam, &gi))
  2604. {
  2605. updateKeyModifiers();
  2606. Point<float> localPos;
  2607. if (auto* peer = findPeerUnderMouse (localPos))
  2608. {
  2609. switch (gi.dwID)
  2610. {
  2611. case 3: /*GID_ZOOM*/
  2612. if (gi.dwFlags != 1 /*GF_BEGIN*/ && lastMagnifySize > 0)
  2613. peer->handleMagnifyGesture (MouseInputSource::InputSourceType::touch, localPos, getMouseEventTime(),
  2614. (float) ((double) gi.ullArguments / (double) lastMagnifySize));
  2615. lastMagnifySize = gi.ullArguments;
  2616. return true;
  2617. case 4: /*GID_PAN*/
  2618. case 5: /*GID_ROTATE*/
  2619. case 6: /*GID_TWOFINGERTAP*/
  2620. case 7: /*GID_PRESSANDTAP*/
  2621. default:
  2622. break;
  2623. }
  2624. }
  2625. }
  2626. return false;
  2627. }
  2628. LRESULT doTouchEvent (const int numInputs, HTOUCHINPUT eventHandle)
  2629. {
  2630. if ((styleFlags & windowIgnoresMouseClicks) != 0)
  2631. if (auto* parent = getOwnerOfWindow (GetParent (hwnd)))
  2632. if (parent != this)
  2633. return parent->doTouchEvent (numInputs, eventHandle);
  2634. HeapBlock<TOUCHINPUT> inputInfo (numInputs);
  2635. if (getTouchInputInfo (eventHandle, (UINT) numInputs, inputInfo, sizeof (TOUCHINPUT)))
  2636. {
  2637. for (int i = 0; i < numInputs; ++i)
  2638. {
  2639. auto flags = inputInfo[i].dwFlags;
  2640. if ((flags & (TOUCHEVENTF_DOWN | TOUCHEVENTF_MOVE | TOUCHEVENTF_UP)) != 0)
  2641. if (! handleTouchInput (inputInfo[i], (flags & TOUCHEVENTF_DOWN) != 0, (flags & TOUCHEVENTF_UP) != 0))
  2642. return 0; // abandon method if this window was deleted by the callback
  2643. }
  2644. }
  2645. closeTouchInputHandle (eventHandle);
  2646. return 0;
  2647. }
  2648. bool handleTouchInput (const TOUCHINPUT& touch, const bool isDown, const bool isUp,
  2649. const float touchPressure = MouseInputSource::defaultPressure,
  2650. const float orientation = 0.0f)
  2651. {
  2652. auto isCancel = false;
  2653. const auto touchIndex = currentTouches.getIndexOfTouch (this, touch.dwID);
  2654. const auto time = getMouseEventTime();
  2655. const auto pos = globalToLocal (convertPhysicalScreenPointToLogical (pointFromPOINT ({ roundToInt (touch.x / 100.0f),
  2656. roundToInt (touch.y / 100.0f) }), hwnd).toFloat());
  2657. const auto pressure = touchPressure;
  2658. auto modsToSend = ModifierKeys::currentModifiers;
  2659. if (isDown)
  2660. {
  2661. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  2662. modsToSend = ModifierKeys::currentModifiers;
  2663. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  2664. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, modsToSend.withoutMouseButtons(),
  2665. pressure, orientation, time, {}, touchIndex);
  2666. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  2667. return false;
  2668. }
  2669. else if (isUp)
  2670. {
  2671. modsToSend = modsToSend.withoutMouseButtons();
  2672. ModifierKeys::currentModifiers = modsToSend;
  2673. currentTouches.clearTouch (touchIndex);
  2674. if (! currentTouches.areAnyTouchesActive())
  2675. isCancel = true;
  2676. }
  2677. else
  2678. {
  2679. modsToSend = ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  2680. }
  2681. handleMouseEvent (MouseInputSource::InputSourceType::touch, pos, modsToSend,
  2682. pressure, orientation, time, {}, touchIndex);
  2683. if (! isValidPeer (this))
  2684. return false;
  2685. if (isUp)
  2686. {
  2687. handleMouseEvent (MouseInputSource::InputSourceType::touch, MouseInputSource::offscreenMousePos, ModifierKeys::currentModifiers.withoutMouseButtons(),
  2688. pressure, orientation, time, {}, touchIndex);
  2689. if (! isValidPeer (this))
  2690. return false;
  2691. if (isCancel)
  2692. {
  2693. currentTouches.clear();
  2694. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons();
  2695. }
  2696. }
  2697. return true;
  2698. }
  2699. bool handlePointerInput (WPARAM wParam, LPARAM lParam, const bool isDown, const bool isUp)
  2700. {
  2701. if (! canUsePointerAPI)
  2702. return false;
  2703. auto pointerType = getPointerType (wParam);
  2704. if (pointerType == MouseInputSource::InputSourceType::touch)
  2705. {
  2706. POINTER_TOUCH_INFO touchInfo;
  2707. if (! getPointerTouchInfo (GET_POINTERID_WPARAM (wParam), &touchInfo))
  2708. return false;
  2709. const auto pressure = touchInfo.touchMask & TOUCH_MASK_PRESSURE ? static_cast<float> (touchInfo.pressure)
  2710. : MouseInputSource::defaultPressure;
  2711. const auto orientation = touchInfo.touchMask & TOUCH_MASK_ORIENTATION ? degreesToRadians (static_cast<float> (touchInfo.orientation))
  2712. : MouseInputSource::defaultOrientation;
  2713. if (! handleTouchInput (emulateTouchEventFromPointer (touchInfo.pointerInfo.ptPixelLocationRaw, wParam),
  2714. isDown, isUp, pressure, orientation))
  2715. return false;
  2716. }
  2717. else if (pointerType == MouseInputSource::InputSourceType::pen)
  2718. {
  2719. POINTER_PEN_INFO penInfo;
  2720. if (! getPointerPenInfo (GET_POINTERID_WPARAM (wParam), &penInfo))
  2721. return false;
  2722. const auto pressure = (penInfo.penMask & PEN_MASK_PRESSURE) ? (float) penInfo.pressure / 1024.0f : MouseInputSource::defaultPressure;
  2723. if (! handlePenInput (penInfo, globalToLocal (convertPhysicalScreenPointToLogical (pointFromPOINT (getPOINTFromLParam (lParam)), hwnd).toFloat()),
  2724. pressure, isDown, isUp))
  2725. return false;
  2726. }
  2727. else
  2728. {
  2729. return false;
  2730. }
  2731. return true;
  2732. }
  2733. TOUCHINPUT emulateTouchEventFromPointer (POINT p, WPARAM wParam)
  2734. {
  2735. TOUCHINPUT touchInput;
  2736. touchInput.dwID = GET_POINTERID_WPARAM (wParam);
  2737. touchInput.x = p.x * 100;
  2738. touchInput.y = p.y * 100;
  2739. return touchInput;
  2740. }
  2741. bool handlePenInput (POINTER_PEN_INFO penInfo, Point<float> pos, const float pressure, bool isDown, bool isUp)
  2742. {
  2743. const auto time = getMouseEventTime();
  2744. ModifierKeys modsToSend (ModifierKeys::currentModifiers);
  2745. PenDetails penDetails;
  2746. penDetails.rotation = (penInfo.penMask & PEN_MASK_ROTATION) ? degreesToRadians (static_cast<float> (penInfo.rotation)) : MouseInputSource::defaultRotation;
  2747. penDetails.tiltX = (penInfo.penMask & PEN_MASK_TILT_X) ? (float) penInfo.tiltX / 90.0f : MouseInputSource::defaultTiltX;
  2748. penDetails.tiltY = (penInfo.penMask & PEN_MASK_TILT_Y) ? (float) penInfo.tiltY / 90.0f : MouseInputSource::defaultTiltY;
  2749. auto pInfoFlags = penInfo.pointerInfo.pointerFlags;
  2750. if ((pInfoFlags & POINTER_FLAG_FIRSTBUTTON) != 0)
  2751. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::leftButtonModifier);
  2752. else if ((pInfoFlags & POINTER_FLAG_SECONDBUTTON) != 0)
  2753. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons().withFlags (ModifierKeys::rightButtonModifier);
  2754. if (isDown)
  2755. {
  2756. modsToSend = ModifierKeys::currentModifiers;
  2757. // this forces a mouse-enter/up event, in case for some reason we didn't get a mouse-up before.
  2758. handleMouseEvent (MouseInputSource::InputSourceType::pen, pos, modsToSend.withoutMouseButtons(),
  2759. pressure, MouseInputSource::defaultOrientation, time, penDetails);
  2760. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  2761. return false;
  2762. }
  2763. else if (isUp || ! (pInfoFlags & POINTER_FLAG_INCONTACT))
  2764. {
  2765. modsToSend = modsToSend.withoutMouseButtons();
  2766. ModifierKeys::currentModifiers = ModifierKeys::currentModifiers.withoutMouseButtons();
  2767. }
  2768. handleMouseEvent (MouseInputSource::InputSourceType::pen, pos, modsToSend, pressure,
  2769. MouseInputSource::defaultOrientation, time, penDetails);
  2770. if (! isValidPeer (this)) // (in case this component was deleted by the event)
  2771. return false;
  2772. if (isUp)
  2773. {
  2774. handleMouseEvent (MouseInputSource::InputSourceType::pen, MouseInputSource::offscreenMousePos, ModifierKeys::currentModifiers,
  2775. pressure, MouseInputSource::defaultOrientation, time, penDetails);
  2776. if (! isValidPeer (this))
  2777. return false;
  2778. }
  2779. return true;
  2780. }
  2781. //==============================================================================
  2782. void sendModifierKeyChangeIfNeeded()
  2783. {
  2784. if (modifiersAtLastCallback != ModifierKeys::currentModifiers)
  2785. {
  2786. modifiersAtLastCallback = ModifierKeys::currentModifiers;
  2787. handleModifierKeysChange();
  2788. }
  2789. }
  2790. bool doKeyUp (const WPARAM key)
  2791. {
  2792. updateKeyModifiers();
  2793. switch (key)
  2794. {
  2795. case VK_SHIFT:
  2796. case VK_CONTROL:
  2797. case VK_MENU:
  2798. case VK_CAPITAL:
  2799. case VK_LWIN:
  2800. case VK_RWIN:
  2801. case VK_APPS:
  2802. case VK_NUMLOCK:
  2803. case VK_SCROLL:
  2804. case VK_LSHIFT:
  2805. case VK_RSHIFT:
  2806. case VK_LCONTROL:
  2807. case VK_LMENU:
  2808. case VK_RCONTROL:
  2809. case VK_RMENU:
  2810. sendModifierKeyChangeIfNeeded();
  2811. }
  2812. return handleKeyUpOrDown (false)
  2813. || Component::getCurrentlyModalComponent() != nullptr;
  2814. }
  2815. bool doKeyDown (const WPARAM key)
  2816. {
  2817. updateKeyModifiers();
  2818. bool used = false;
  2819. switch (key)
  2820. {
  2821. case VK_SHIFT:
  2822. case VK_LSHIFT:
  2823. case VK_RSHIFT:
  2824. case VK_CONTROL:
  2825. case VK_LCONTROL:
  2826. case VK_RCONTROL:
  2827. case VK_MENU:
  2828. case VK_LMENU:
  2829. case VK_RMENU:
  2830. case VK_LWIN:
  2831. case VK_RWIN:
  2832. case VK_CAPITAL:
  2833. case VK_NUMLOCK:
  2834. case VK_SCROLL:
  2835. case VK_APPS:
  2836. used = handleKeyUpOrDown (true);
  2837. sendModifierKeyChangeIfNeeded();
  2838. break;
  2839. case VK_LEFT:
  2840. case VK_RIGHT:
  2841. case VK_UP:
  2842. case VK_DOWN:
  2843. case VK_PRIOR:
  2844. case VK_NEXT:
  2845. case VK_HOME:
  2846. case VK_END:
  2847. case VK_DELETE:
  2848. case VK_INSERT:
  2849. case VK_F1:
  2850. case VK_F2:
  2851. case VK_F3:
  2852. case VK_F4:
  2853. case VK_F5:
  2854. case VK_F6:
  2855. case VK_F7:
  2856. case VK_F8:
  2857. case VK_F9:
  2858. case VK_F10:
  2859. case VK_F11:
  2860. case VK_F12:
  2861. case VK_F13:
  2862. case VK_F14:
  2863. case VK_F15:
  2864. case VK_F16:
  2865. case VK_F17:
  2866. case VK_F18:
  2867. case VK_F19:
  2868. case VK_F20:
  2869. case VK_F21:
  2870. case VK_F22:
  2871. case VK_F23:
  2872. case VK_F24:
  2873. used = handleKeyUpOrDown (true);
  2874. used = handleKeyPress (extendedKeyModifier | (int) key, 0) || used;
  2875. break;
  2876. default:
  2877. used = handleKeyUpOrDown (true);
  2878. {
  2879. MSG msg;
  2880. if (! PeekMessage (&msg, hwnd, WM_CHAR, WM_DEADCHAR, PM_NOREMOVE))
  2881. {
  2882. // if there isn't a WM_CHAR or WM_DEADCHAR message pending, we need to
  2883. // manually generate the key-press event that matches this key-down.
  2884. const UINT keyChar = MapVirtualKey ((UINT) key, 2);
  2885. const UINT scanCode = MapVirtualKey ((UINT) key, 0);
  2886. BYTE keyState[256];
  2887. [[maybe_unused]] const auto state = GetKeyboardState (keyState);
  2888. WCHAR text[16] = { 0 };
  2889. if (ToUnicode ((UINT) key, scanCode, keyState, text, 8, 0) != 1)
  2890. text[0] = 0;
  2891. used = handleKeyPress ((int) LOWORD (keyChar), (juce_wchar) text[0]) || used;
  2892. }
  2893. }
  2894. break;
  2895. }
  2896. return used || (Component::getCurrentlyModalComponent() != nullptr);
  2897. }
  2898. bool doKeyChar (int key, const LPARAM flags)
  2899. {
  2900. updateKeyModifiers();
  2901. auto textChar = (juce_wchar) key;
  2902. const int virtualScanCode = (flags >> 16) & 0xff;
  2903. if (key >= '0' && key <= '9')
  2904. {
  2905. switch (virtualScanCode) // check for a numeric keypad scan-code
  2906. {
  2907. case 0x52:
  2908. case 0x4f:
  2909. case 0x50:
  2910. case 0x51:
  2911. case 0x4b:
  2912. case 0x4c:
  2913. case 0x4d:
  2914. case 0x47:
  2915. case 0x48:
  2916. case 0x49:
  2917. key = (key - '0') + KeyPress::numberPad0;
  2918. break;
  2919. default:
  2920. break;
  2921. }
  2922. }
  2923. else
  2924. {
  2925. // convert the scan code to an unmodified character code..
  2926. const UINT virtualKey = MapVirtualKey ((UINT) virtualScanCode, 1);
  2927. UINT keyChar = MapVirtualKey (virtualKey, 2);
  2928. keyChar = LOWORD (keyChar);
  2929. if (keyChar != 0)
  2930. key = (int) keyChar;
  2931. // avoid sending junk text characters for some control-key combinations
  2932. if (textChar < ' ' && ModifierKeys::currentModifiers.testFlags (ModifierKeys::ctrlModifier | ModifierKeys::altModifier))
  2933. textChar = 0;
  2934. }
  2935. return handleKeyPress (key, textChar);
  2936. }
  2937. void forwardMessageToParent (UINT message, WPARAM wParam, LPARAM lParam) const
  2938. {
  2939. if (HWND parentH = GetParent (hwnd))
  2940. PostMessage (parentH, message, wParam, lParam);
  2941. }
  2942. bool doAppCommand (const LPARAM lParam)
  2943. {
  2944. int key = 0;
  2945. switch (GET_APPCOMMAND_LPARAM (lParam))
  2946. {
  2947. case APPCOMMAND_MEDIA_PLAY_PAUSE: key = KeyPress::playKey; break;
  2948. case APPCOMMAND_MEDIA_STOP: key = KeyPress::stopKey; break;
  2949. case APPCOMMAND_MEDIA_NEXTTRACK: key = KeyPress::fastForwardKey; break;
  2950. case APPCOMMAND_MEDIA_PREVIOUSTRACK: key = KeyPress::rewindKey; break;
  2951. default: break;
  2952. }
  2953. if (key != 0)
  2954. {
  2955. updateKeyModifiers();
  2956. if (hwnd == GetActiveWindow())
  2957. return handleKeyPress (key, 0);
  2958. }
  2959. return false;
  2960. }
  2961. bool isConstrainedNativeWindow() const
  2962. {
  2963. return constrainer != nullptr
  2964. && (styleFlags & (windowHasTitleBar | windowIsResizable)) == (windowHasTitleBar | windowIsResizable)
  2965. && ! isKioskMode();
  2966. }
  2967. Rectangle<int> getCurrentScaledBounds() const
  2968. {
  2969. return detail::ScalingHelpers::unscaledScreenPosToScaled (component, windowBorder.addedTo (detail::ScalingHelpers::scaledScreenPosToUnscaled (component, component.getBounds())));
  2970. }
  2971. LRESULT handleSizeConstraining (RECT& r, const WPARAM wParam)
  2972. {
  2973. if (isConstrainedNativeWindow())
  2974. {
  2975. const auto logicalBounds = convertPhysicalScreenRectangleToLogical (rectangleFromRECT (r).toFloat(), hwnd);
  2976. auto pos = detail::ScalingHelpers::unscaledScreenPosToScaled (component, logicalBounds).toNearestInt();
  2977. const auto original = getCurrentScaledBounds();
  2978. constrainer->checkBounds (pos, original,
  2979. Desktop::getInstance().getDisplays().getTotalBounds (true),
  2980. wParam == WMSZ_TOP || wParam == WMSZ_TOPLEFT || wParam == WMSZ_TOPRIGHT,
  2981. wParam == WMSZ_LEFT || wParam == WMSZ_TOPLEFT || wParam == WMSZ_BOTTOMLEFT,
  2982. wParam == WMSZ_BOTTOM || wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_BOTTOMRIGHT,
  2983. wParam == WMSZ_RIGHT || wParam == WMSZ_TOPRIGHT || wParam == WMSZ_BOTTOMRIGHT);
  2984. r = RECTFromRectangle (convertLogicalScreenRectangleToPhysical (detail::ScalingHelpers::scaledScreenPosToUnscaled (component, pos.toFloat()).toNearestInt(), hwnd));
  2985. }
  2986. return TRUE;
  2987. }
  2988. LRESULT handlePositionChanging (WINDOWPOS& wp)
  2989. {
  2990. if (isConstrainedNativeWindow() && ! isFullScreen())
  2991. {
  2992. if ((wp.flags & (SWP_NOMOVE | SWP_NOSIZE)) != (SWP_NOMOVE | SWP_NOSIZE)
  2993. && (wp.x > -32000 && wp.y > -32000)
  2994. && ! Component::isMouseButtonDownAnywhere())
  2995. {
  2996. const auto logicalBounds = convertPhysicalScreenRectangleToLogical (rectangleFromRECT ({ wp.x, wp.y, wp.x + wp.cx, wp.y + wp.cy }).toFloat(), hwnd);
  2997. auto pos = detail::ScalingHelpers::unscaledScreenPosToScaled (component, logicalBounds).toNearestInt();
  2998. const auto original = getCurrentScaledBounds();
  2999. constrainer->checkBounds (pos, original,
  3000. Desktop::getInstance().getDisplays().getTotalBounds (true),
  3001. pos.getY() != original.getY() && pos.getBottom() == original.getBottom(),
  3002. pos.getX() != original.getX() && pos.getRight() == original.getRight(),
  3003. pos.getY() == original.getY() && pos.getBottom() != original.getBottom(),
  3004. pos.getX() == original.getX() && pos.getRight() != original.getRight());
  3005. auto physicalBounds = convertLogicalScreenRectangleToPhysical (detail::ScalingHelpers::scaledScreenPosToUnscaled (component, pos.toFloat()), hwnd);
  3006. auto getNewPositionIfNotRoundingError = [] (int posIn, float newPos)
  3007. {
  3008. return (std::abs ((float) posIn - newPos) >= 1.0f) ? roundToInt (newPos) : posIn;
  3009. };
  3010. wp.x = getNewPositionIfNotRoundingError (wp.x, physicalBounds.getX());
  3011. wp.y = getNewPositionIfNotRoundingError (wp.y, physicalBounds.getY());
  3012. wp.cx = getNewPositionIfNotRoundingError (wp.cx, physicalBounds.getWidth());
  3013. wp.cy = getNewPositionIfNotRoundingError (wp.cy, physicalBounds.getHeight());
  3014. }
  3015. }
  3016. if (((wp.flags & SWP_SHOWWINDOW) != 0 && ! component.isVisible()))
  3017. component.setVisible (true);
  3018. else if (((wp.flags & SWP_HIDEWINDOW) != 0 && component.isVisible()))
  3019. component.setVisible (false);
  3020. return 0;
  3021. }
  3022. enum class ForceRefreshDispatcher
  3023. {
  3024. no,
  3025. yes
  3026. };
  3027. void updateCurrentMonitorAndRefreshVBlankDispatcher (ForceRefreshDispatcher force = ForceRefreshDispatcher::no)
  3028. {
  3029. auto monitor = MonitorFromWindow (hwnd, MONITOR_DEFAULTTONULL);
  3030. if (std::exchange (currentMonitor, monitor) != monitor || force == ForceRefreshDispatcher::yes)
  3031. VBlankDispatcher::getInstance()->updateDisplay (*this, currentMonitor);
  3032. }
  3033. bool handlePositionChanged()
  3034. {
  3035. auto pos = getCurrentMousePos();
  3036. if (contains (pos.roundToInt(), false))
  3037. {
  3038. const ScopedValueSetter<bool> scope (inHandlePositionChanged, true);
  3039. if (! areOtherTouchSourcesActive())
  3040. doMouseEvent (pos, MouseInputSource::defaultPressure);
  3041. if (! isValidPeer (this))
  3042. return true;
  3043. }
  3044. handleMovedOrResized();
  3045. updateCurrentMonitorAndRefreshVBlankDispatcher();
  3046. return ! dontRepaint; // to allow non-accelerated openGL windows to draw themselves correctly.
  3047. }
  3048. //==============================================================================
  3049. LRESULT handleDPIChanging (int newDPI, RECT newRect)
  3050. {
  3051. // Sometimes, windows that should not be automatically scaled (secondary windows in plugins)
  3052. // are sent WM_DPICHANGED. The size suggested by the OS is incorrect for our unscaled
  3053. // window, so we should ignore it.
  3054. if (! isPerMonitorDPIAwareWindow (hwnd))
  3055. return 0;
  3056. const auto newScale = (double) newDPI / USER_DEFAULT_SCREEN_DPI;
  3057. if (approximatelyEqual (scaleFactor, newScale))
  3058. return 0;
  3059. scaleFactor = newScale;
  3060. {
  3061. const ScopedValueSetter<bool> setter (inDpiChange, true);
  3062. SetWindowPos (hwnd,
  3063. nullptr,
  3064. newRect.left,
  3065. newRect.top,
  3066. newRect.right - newRect.left,
  3067. newRect.bottom - newRect.top,
  3068. SWP_NOZORDER | SWP_NOACTIVATE);
  3069. }
  3070. // This is to handle reentrancy. If responding to a DPI change triggers further DPI changes,
  3071. // we should only notify listeners and resize windows once all of the DPI changes have
  3072. // resolved.
  3073. if (inDpiChange)
  3074. {
  3075. // Danger! Re-entrant call to handleDPIChanging.
  3076. // Please report this issue on the JUCE forum, along with instructions
  3077. // so that a JUCE developer can reproduce the issue.
  3078. jassertfalse;
  3079. return 0;
  3080. }
  3081. updateShadower();
  3082. InvalidateRect (hwnd, nullptr, FALSE);
  3083. scaleFactorListeners.call ([this] (ScaleFactorListener& l) { l.nativeScaleFactorChanged (scaleFactor); });
  3084. return 0;
  3085. }
  3086. //==============================================================================
  3087. void handleAppActivation (const WPARAM wParam)
  3088. {
  3089. modifiersAtLastCallback = -1;
  3090. updateKeyModifiers();
  3091. if (isMinimised())
  3092. {
  3093. component.repaint();
  3094. handleMovedOrResized();
  3095. if (! isValidPeer (this))
  3096. return;
  3097. }
  3098. auto* underMouse = component.getComponentAt (component.getMouseXYRelative());
  3099. if (underMouse == nullptr)
  3100. underMouse = &component;
  3101. if (underMouse->isCurrentlyBlockedByAnotherModalComponent())
  3102. {
  3103. if (LOWORD (wParam) == WA_CLICKACTIVE)
  3104. Component::getCurrentlyModalComponent()->inputAttemptWhenModal();
  3105. else
  3106. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  3107. }
  3108. else
  3109. {
  3110. handleBroughtToFront();
  3111. }
  3112. }
  3113. void handlePowerBroadcast (WPARAM wParam)
  3114. {
  3115. if (auto* app = JUCEApplicationBase::getInstance())
  3116. {
  3117. switch (wParam)
  3118. {
  3119. case PBT_APMSUSPEND: app->suspended(); break;
  3120. case PBT_APMQUERYSUSPENDFAILED:
  3121. case PBT_APMRESUMECRITICAL:
  3122. case PBT_APMRESUMESUSPEND:
  3123. case PBT_APMRESUMEAUTOMATIC: app->resumed(); break;
  3124. default: break;
  3125. }
  3126. }
  3127. }
  3128. void handleLeftClickInNCArea (WPARAM wParam)
  3129. {
  3130. if (! sendInputAttemptWhenModalMessage())
  3131. {
  3132. switch (wParam)
  3133. {
  3134. case HTBOTTOM:
  3135. case HTBOTTOMLEFT:
  3136. case HTBOTTOMRIGHT:
  3137. case HTGROWBOX:
  3138. case HTLEFT:
  3139. case HTRIGHT:
  3140. case HTTOP:
  3141. case HTTOPLEFT:
  3142. case HTTOPRIGHT:
  3143. if (isConstrainedNativeWindow())
  3144. {
  3145. constrainerIsResizing = true;
  3146. constrainer->resizeStart();
  3147. }
  3148. break;
  3149. default:
  3150. break;
  3151. }
  3152. }
  3153. }
  3154. void initialiseSysMenu (HMENU menu) const
  3155. {
  3156. if (! hasTitleBar())
  3157. {
  3158. if (isFullScreen())
  3159. {
  3160. EnableMenuItem (menu, SC_RESTORE, MF_BYCOMMAND | MF_ENABLED);
  3161. EnableMenuItem (menu, SC_MOVE, MF_BYCOMMAND | MF_GRAYED);
  3162. }
  3163. else if (! isMinimised())
  3164. {
  3165. EnableMenuItem (menu, SC_MAXIMIZE, MF_BYCOMMAND | MF_GRAYED);
  3166. }
  3167. }
  3168. }
  3169. void doSettingChange()
  3170. {
  3171. forceDisplayUpdate();
  3172. if (fullScreen && ! isMinimised())
  3173. setWindowPos (hwnd, detail::ScalingHelpers::scaledScreenPosToUnscaled (component, Desktop::getInstance().getDisplays()
  3174. .getDisplayForRect (component.getScreenBounds())->userArea),
  3175. SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOSENDCHANGING);
  3176. auto* dispatcher = VBlankDispatcher::getInstance();
  3177. dispatcher->reconfigureDisplays();
  3178. updateCurrentMonitorAndRefreshVBlankDispatcher (ForceRefreshDispatcher::yes);
  3179. }
  3180. //==============================================================================
  3181. #if JUCE_MODULE_AVAILABLE_juce_audio_plugin_client
  3182. void setModifierKeyProvider (ModifierKeyProvider* provider) override
  3183. {
  3184. modProvider = provider;
  3185. }
  3186. void removeModifierKeyProvider() override
  3187. {
  3188. modProvider = nullptr;
  3189. }
  3190. #endif
  3191. public:
  3192. static LRESULT CALLBACK windowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
  3193. {
  3194. // Ensure that non-client areas are scaled for per-monitor DPI awareness v1 - can't
  3195. // do this in peerWindowProc as we have no window at this point
  3196. if (message == WM_NCCREATE)
  3197. NullCheckedInvocation::invoke (enableNonClientDPIScaling, h);
  3198. if (auto* peer = getOwnerOfWindow (h))
  3199. {
  3200. jassert (isValidPeer (peer));
  3201. return peer->peerWindowProc (h, message, wParam, lParam);
  3202. }
  3203. return DefWindowProcW (h, message, wParam, lParam);
  3204. }
  3205. private:
  3206. static void* callFunctionIfNotLocked (MessageCallbackFunction* callback, void* userData)
  3207. {
  3208. auto& mm = *MessageManager::getInstance();
  3209. if (mm.currentThreadHasLockedMessageManager())
  3210. return callback (userData);
  3211. return mm.callFunctionOnMessageThread (callback, userData);
  3212. }
  3213. static POINT getPOINTFromLParam (LPARAM lParam) noexcept
  3214. {
  3215. return { GET_X_LPARAM (lParam), GET_Y_LPARAM (lParam) };
  3216. }
  3217. Point<float> getPointFromLocalLParam (LPARAM lParam) noexcept
  3218. {
  3219. auto p = pointFromPOINT (getPOINTFromLParam (lParam));
  3220. if (isPerMonitorDPIAwareWindow (hwnd))
  3221. {
  3222. // LPARAM is relative to this window's top-left but may be on a different monitor so we need to calculate the
  3223. // physical screen position and then convert this to local logical coordinates
  3224. auto r = getWindowScreenRect (hwnd);
  3225. return globalToLocal (Desktop::getInstance().getDisplays().physicalToLogical (pointFromPOINT ({ r.left + p.x + roundToInt (windowBorder.getLeft() * scaleFactor),
  3226. r.top + p.y + roundToInt (windowBorder.getTop() * scaleFactor) })).toFloat());
  3227. }
  3228. return p.toFloat();
  3229. }
  3230. Point<float> getCurrentMousePos() noexcept
  3231. {
  3232. return globalToLocal (convertPhysicalScreenPointToLogical (pointFromPOINT (getPOINTFromLParam ((LPARAM) GetMessagePos())), hwnd).toFloat());
  3233. }
  3234. LRESULT peerWindowProc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
  3235. {
  3236. switch (message)
  3237. {
  3238. //==============================================================================
  3239. case WM_NCHITTEST:
  3240. if ((styleFlags & windowIgnoresMouseClicks) != 0)
  3241. return HTTRANSPARENT;
  3242. if (! hasTitleBar())
  3243. return HTCLIENT;
  3244. break;
  3245. //==============================================================================
  3246. case WM_PAINT:
  3247. handlePaintMessage();
  3248. return 0;
  3249. case WM_NCPAINT:
  3250. handlePaintMessage(); // this must be done, even with native titlebars, or there are rendering artifacts.
  3251. if (hasTitleBar())
  3252. break; // let the DefWindowProc handle drawing the frame.
  3253. return 0;
  3254. case WM_ERASEBKGND:
  3255. case WM_NCCALCSIZE:
  3256. if (hasTitleBar())
  3257. break;
  3258. return 1;
  3259. //==============================================================================
  3260. case WM_POINTERUPDATE:
  3261. if (handlePointerInput (wParam, lParam, false, false))
  3262. return 0;
  3263. break;
  3264. case WM_POINTERDOWN:
  3265. if (handlePointerInput (wParam, lParam, true, false))
  3266. return 0;
  3267. break;
  3268. case WM_POINTERUP:
  3269. if (handlePointerInput (wParam, lParam, false, true))
  3270. return 0;
  3271. break;
  3272. //==============================================================================
  3273. case WM_MOUSEMOVE: doMouseMove (getPointFromLocalLParam (lParam), false); return 0;
  3274. case WM_POINTERLEAVE:
  3275. case WM_MOUSELEAVE: doMouseExit(); return 0;
  3276. case WM_LBUTTONDOWN:
  3277. case WM_MBUTTONDOWN:
  3278. case WM_RBUTTONDOWN: doMouseDown (getPointFromLocalLParam (lParam), wParam); return 0;
  3279. case WM_LBUTTONUP:
  3280. case WM_MBUTTONUP:
  3281. case WM_RBUTTONUP: doMouseUp (getPointFromLocalLParam (lParam), wParam); return 0;
  3282. case WM_POINTERWHEEL:
  3283. case 0x020A: /* WM_MOUSEWHEEL */ doMouseWheel (wParam, true); return 0;
  3284. case WM_POINTERHWHEEL:
  3285. case 0x020E: /* WM_MOUSEHWHEEL */ doMouseWheel (wParam, false); return 0;
  3286. case WM_CAPTURECHANGED: doCaptureChanged(); return 0;
  3287. case WM_NCPOINTERUPDATE:
  3288. case WM_NCMOUSEMOVE:
  3289. if (hasTitleBar())
  3290. break;
  3291. return 0;
  3292. case WM_TOUCH:
  3293. if (getTouchInputInfo != nullptr)
  3294. return doTouchEvent ((int) wParam, (HTOUCHINPUT) lParam);
  3295. break;
  3296. case 0x119: /* WM_GESTURE */
  3297. if (doGestureEvent (lParam))
  3298. return 0;
  3299. break;
  3300. //==============================================================================
  3301. case WM_SIZING: return handleSizeConstraining (*(RECT*) lParam, wParam);
  3302. case WM_WINDOWPOSCHANGING: return handlePositionChanging (*(WINDOWPOS*) lParam);
  3303. case 0x2e0: /* WM_DPICHANGED */ return handleDPIChanging ((int) HIWORD (wParam), *(RECT*) lParam);
  3304. case WM_WINDOWPOSCHANGED:
  3305. {
  3306. const WINDOWPOS& wPos = *reinterpret_cast<WINDOWPOS*> (lParam);
  3307. if ((wPos.flags & SWP_NOMOVE) != 0 && (wPos.flags & SWP_NOSIZE) != 0)
  3308. startTimer (100);
  3309. else
  3310. if (handlePositionChanged())
  3311. return 0;
  3312. }
  3313. break;
  3314. //==============================================================================
  3315. case WM_KEYDOWN:
  3316. case WM_SYSKEYDOWN:
  3317. if (doKeyDown (wParam))
  3318. return 0;
  3319. forwardMessageToParent (message, wParam, lParam);
  3320. break;
  3321. case WM_KEYUP:
  3322. case WM_SYSKEYUP:
  3323. if (doKeyUp (wParam))
  3324. return 0;
  3325. forwardMessageToParent (message, wParam, lParam);
  3326. break;
  3327. case WM_CHAR:
  3328. if (doKeyChar ((int) wParam, lParam))
  3329. return 0;
  3330. forwardMessageToParent (message, wParam, lParam);
  3331. break;
  3332. case WM_APPCOMMAND:
  3333. if (doAppCommand (lParam))
  3334. return TRUE;
  3335. break;
  3336. case WM_MENUCHAR: // triggered when alt+something is pressed
  3337. return MNC_CLOSE << 16; // (avoids making the default system beep)
  3338. //==============================================================================
  3339. case WM_SETFOCUS:
  3340. /* When the HWND receives Focus from the system it sends a
  3341. UIA_AutomationFocusChangedEventId notification redirecting the focus to the HWND
  3342. itself. This is a built-in behaviour of the HWND.
  3343. This means that whichever JUCE managed provider was active before the entire
  3344. window lost and then regained the focus, loses its focused state, and the
  3345. window's root element will become focused under which all JUCE managed providers
  3346. can be found.
  3347. This needs to be reflected on currentlyFocusedHandler so that the JUCE
  3348. accessibility mechanisms can detect that the root window got the focus, and send
  3349. another FocusChanged event to the system to redirect focus to a JUCE managed
  3350. provider if necessary.
  3351. */
  3352. AccessibilityHandler::clearCurrentlyFocusedHandler();
  3353. updateKeyModifiers();
  3354. handleFocusGain();
  3355. break;
  3356. case WM_KILLFOCUS:
  3357. if (hasCreatedCaret)
  3358. {
  3359. hasCreatedCaret = false;
  3360. DestroyCaret();
  3361. }
  3362. handleFocusLoss();
  3363. if (auto* modal = Component::getCurrentlyModalComponent())
  3364. if (auto* peer = modal->getPeer())
  3365. if ((peer->getStyleFlags() & ComponentPeer::windowIsTemporary) != 0)
  3366. sendInputAttemptWhenModalMessage();
  3367. break;
  3368. case WM_ACTIVATEAPP:
  3369. // Windows does weird things to process priority when you swap apps,
  3370. // so this forces an update when the app is brought to the front
  3371. if (wParam != FALSE)
  3372. juce_repeatLastProcessPriority();
  3373. else
  3374. Desktop::getInstance().setKioskModeComponent (nullptr); // turn kiosk mode off if we lose focus
  3375. detail::TopLevelWindowManager::checkCurrentlyFocusedTopLevelWindow();
  3376. modifiersAtLastCallback = -1;
  3377. return 0;
  3378. case WM_ACTIVATE:
  3379. if (LOWORD (wParam) == WA_ACTIVE || LOWORD (wParam) == WA_CLICKACTIVE)
  3380. {
  3381. handleAppActivation (wParam);
  3382. return 0;
  3383. }
  3384. break;
  3385. case WM_NCACTIVATE:
  3386. // while a temporary window is being shown, prevent Windows from deactivating the
  3387. // title bars of our main windows.
  3388. if (wParam == 0 && ! shouldDeactivateTitleBar)
  3389. wParam = TRUE; // change this and let it get passed to the DefWindowProc.
  3390. break;
  3391. case WM_POINTERACTIVATE:
  3392. case WM_MOUSEACTIVATE:
  3393. if (! component.getMouseClickGrabsKeyboardFocus())
  3394. return MA_NOACTIVATE;
  3395. break;
  3396. case WM_SHOWWINDOW:
  3397. if (wParam != 0)
  3398. {
  3399. component.setVisible (true);
  3400. handleBroughtToFront();
  3401. }
  3402. break;
  3403. case WM_CLOSE:
  3404. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  3405. handleUserClosingWindow();
  3406. return 0;
  3407. #if JUCE_REMOVE_COMPONENT_FROM_DESKTOP_ON_WM_DESTROY
  3408. case WM_DESTROY:
  3409. getComponent().removeFromDesktop();
  3410. return 0;
  3411. #endif
  3412. case WM_QUERYENDSESSION:
  3413. if (auto* app = JUCEApplicationBase::getInstance())
  3414. {
  3415. app->systemRequestedQuit();
  3416. return MessageManager::getInstance()->hasStopMessageBeenSent();
  3417. }
  3418. return TRUE;
  3419. case WM_POWERBROADCAST:
  3420. handlePowerBroadcast (wParam);
  3421. break;
  3422. case WM_SYNCPAINT:
  3423. return 0;
  3424. case WM_DISPLAYCHANGE:
  3425. InvalidateRect (h, nullptr, 0);
  3426. // intentional fall-through...
  3427. JUCE_FALLTHROUGH
  3428. case WM_SETTINGCHANGE: // note the fall-through in the previous case!
  3429. doSettingChange();
  3430. break;
  3431. case WM_INITMENU:
  3432. initialiseSysMenu ((HMENU) wParam);
  3433. break;
  3434. case WM_SYSCOMMAND:
  3435. switch (wParam & 0xfff0)
  3436. {
  3437. case SC_CLOSE:
  3438. if (sendInputAttemptWhenModalMessage())
  3439. return 0;
  3440. if (hasTitleBar())
  3441. {
  3442. PostMessage (h, WM_CLOSE, 0, 0);
  3443. return 0;
  3444. }
  3445. break;
  3446. case SC_KEYMENU:
  3447. #if ! JUCE_WINDOWS_ALT_KEY_TRIGGERS_MENU
  3448. // This test prevents a press of the ALT key from triggering the ancient top-left window menu.
  3449. // By default we suppress this behaviour because it's unlikely that more than a tiny subset of
  3450. // our users will actually want it, and it causes problems if you're trying to use the ALT key
  3451. // as a modifier for mouse actions. If you really need the old behaviour, then just define
  3452. // JUCE_WINDOWS_ALT_KEY_TRIGGERS_MENU=1 in your app.
  3453. if ((lParam >> 16) <= 0) // Values above zero indicate that a mouse-click triggered the menu
  3454. return 0;
  3455. #endif
  3456. // (NB mustn't call sendInputAttemptWhenModalMessage() here because of very obscure
  3457. // situations that can arise if a modal loop is started from an alt-key keypress).
  3458. if (hasTitleBar() && h == GetCapture())
  3459. ReleaseCapture();
  3460. break;
  3461. case SC_MAXIMIZE:
  3462. if (! sendInputAttemptWhenModalMessage())
  3463. setFullScreen (true);
  3464. return 0;
  3465. case SC_MINIMIZE:
  3466. if (sendInputAttemptWhenModalMessage())
  3467. return 0;
  3468. if (! hasTitleBar())
  3469. {
  3470. setMinimised (true);
  3471. return 0;
  3472. }
  3473. break;
  3474. case SC_RESTORE:
  3475. if (sendInputAttemptWhenModalMessage())
  3476. return 0;
  3477. if (hasTitleBar())
  3478. {
  3479. if (isFullScreen())
  3480. {
  3481. setFullScreen (false);
  3482. return 0;
  3483. }
  3484. }
  3485. else
  3486. {
  3487. if (isMinimised())
  3488. setMinimised (false);
  3489. else if (isFullScreen())
  3490. setFullScreen (false);
  3491. return 0;
  3492. }
  3493. break;
  3494. }
  3495. break;
  3496. case WM_NCPOINTERDOWN:
  3497. case WM_NCLBUTTONDOWN:
  3498. handleLeftClickInNCArea (wParam);
  3499. break;
  3500. case WM_NCRBUTTONDOWN:
  3501. case WM_NCMBUTTONDOWN:
  3502. sendInputAttemptWhenModalMessage();
  3503. break;
  3504. case WM_IME_SETCONTEXT:
  3505. imeHandler.handleSetContext (h, wParam == TRUE);
  3506. lParam &= ~(LPARAM) ISC_SHOWUICOMPOSITIONWINDOW;
  3507. return ImmIsUIMessage (h, message, wParam, lParam);
  3508. case WM_IME_STARTCOMPOSITION: imeHandler.handleStartComposition (*this); return 0;
  3509. case WM_IME_ENDCOMPOSITION: imeHandler.handleEndComposition (*this, h); return 0;
  3510. case WM_IME_COMPOSITION: imeHandler.handleComposition (*this, h, lParam); return 0;
  3511. case WM_GETDLGCODE:
  3512. return DLGC_WANTALLKEYS;
  3513. case WM_GETOBJECT:
  3514. {
  3515. if (static_cast<long> (lParam) == WindowsAccessibility::getUiaRootObjectId())
  3516. {
  3517. if (auto* handler = component.getAccessibilityHandler())
  3518. {
  3519. LRESULT res = 0;
  3520. if (WindowsAccessibility::handleWmGetObject (handler, wParam, lParam, &res))
  3521. {
  3522. isAccessibilityActive = true;
  3523. return res;
  3524. }
  3525. }
  3526. }
  3527. break;
  3528. }
  3529. default:
  3530. break;
  3531. }
  3532. return DefWindowProcW (h, message, wParam, lParam);
  3533. }
  3534. bool sendInputAttemptWhenModalMessage()
  3535. {
  3536. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  3537. return false;
  3538. if (auto* current = Component::getCurrentlyModalComponent())
  3539. if (auto* owner = getOwnerOfWindow ((HWND) current->getWindowHandle()))
  3540. if (! owner->shouldIgnoreModalDismiss)
  3541. current->inputAttemptWhenModal();
  3542. return true;
  3543. }
  3544. //==============================================================================
  3545. struct IMEHandler
  3546. {
  3547. IMEHandler()
  3548. {
  3549. reset();
  3550. }
  3551. void handleSetContext (HWND hWnd, const bool windowIsActive)
  3552. {
  3553. if (compositionInProgress && ! windowIsActive)
  3554. {
  3555. if (HIMC hImc = ImmGetContext (hWnd))
  3556. {
  3557. ImmNotifyIME (hImc, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
  3558. ImmReleaseContext (hWnd, hImc);
  3559. }
  3560. // If the composition is still in progress, calling ImmNotifyIME may call back
  3561. // into handleComposition to let us know that the composition has finished.
  3562. // We need to set compositionInProgress *after* calling handleComposition, so that
  3563. // the text replaces the current selection, rather than being inserted after the
  3564. // caret.
  3565. compositionInProgress = false;
  3566. }
  3567. }
  3568. void handleStartComposition (ComponentPeer& owner)
  3569. {
  3570. reset();
  3571. if (auto* target = owner.findCurrentTextInputTarget())
  3572. target->insertTextAtCaret (String());
  3573. }
  3574. void handleEndComposition (ComponentPeer& owner, HWND hWnd)
  3575. {
  3576. if (compositionInProgress)
  3577. {
  3578. // If this occurs, the user has cancelled the composition, so clear their changes..
  3579. if (auto* target = owner.findCurrentTextInputTarget())
  3580. {
  3581. target->setHighlightedRegion (compositionRange);
  3582. target->insertTextAtCaret (String());
  3583. compositionRange.setLength (0);
  3584. target->setHighlightedRegion (Range<int>::emptyRange (compositionRange.getEnd()));
  3585. target->setTemporaryUnderlining ({});
  3586. }
  3587. if (auto hImc = ImmGetContext (hWnd))
  3588. {
  3589. ImmNotifyIME (hImc, NI_CLOSECANDIDATE, 0, 0);
  3590. ImmReleaseContext (hWnd, hImc);
  3591. }
  3592. }
  3593. reset();
  3594. }
  3595. void handleComposition (ComponentPeer& owner, HWND hWnd, const LPARAM lParam)
  3596. {
  3597. if (auto* target = owner.findCurrentTextInputTarget())
  3598. {
  3599. if (auto hImc = ImmGetContext (hWnd))
  3600. {
  3601. if (compositionRange.getStart() < 0)
  3602. compositionRange = Range<int>::emptyRange (target->getHighlightedRegion().getStart());
  3603. if ((lParam & GCS_RESULTSTR) != 0) // (composition has finished)
  3604. {
  3605. replaceCurrentSelection (target, getCompositionString (hImc, GCS_RESULTSTR),
  3606. Range<int>::emptyRange (-1));
  3607. reset();
  3608. target->setTemporaryUnderlining ({});
  3609. }
  3610. else if ((lParam & GCS_COMPSTR) != 0) // (composition is still in-progress)
  3611. {
  3612. replaceCurrentSelection (target, getCompositionString (hImc, GCS_COMPSTR),
  3613. getCompositionSelection (hImc, lParam));
  3614. target->setTemporaryUnderlining (getCompositionUnderlines (hImc, lParam));
  3615. compositionInProgress = true;
  3616. }
  3617. moveCandidateWindowToLeftAlignWithSelection (hImc, owner, target);
  3618. ImmReleaseContext (hWnd, hImc);
  3619. }
  3620. }
  3621. }
  3622. private:
  3623. //==============================================================================
  3624. Range<int> compositionRange; // The range being modified in the TextInputTarget
  3625. bool compositionInProgress;
  3626. //==============================================================================
  3627. void reset()
  3628. {
  3629. compositionRange = Range<int>::emptyRange (-1);
  3630. compositionInProgress = false;
  3631. }
  3632. String getCompositionString (HIMC hImc, const DWORD type) const
  3633. {
  3634. jassert (hImc != HIMC{});
  3635. const auto stringSizeBytes = ImmGetCompositionString (hImc, type, nullptr, 0);
  3636. if (stringSizeBytes > 0)
  3637. {
  3638. HeapBlock<TCHAR> buffer;
  3639. buffer.calloc ((size_t) stringSizeBytes / sizeof (TCHAR) + 1);
  3640. ImmGetCompositionString (hImc, type, buffer, (DWORD) stringSizeBytes);
  3641. return String (buffer.get());
  3642. }
  3643. return {};
  3644. }
  3645. int getCompositionCaretPos (HIMC hImc, LPARAM lParam, const String& currentIMEString) const
  3646. {
  3647. jassert (hImc != HIMC{});
  3648. if ((lParam & CS_NOMOVECARET) != 0)
  3649. return compositionRange.getStart();
  3650. if ((lParam & GCS_CURSORPOS) != 0)
  3651. {
  3652. const int localCaretPos = ImmGetCompositionString (hImc, GCS_CURSORPOS, nullptr, 0);
  3653. return compositionRange.getStart() + jmax (0, localCaretPos);
  3654. }
  3655. return compositionRange.getStart() + currentIMEString.length();
  3656. }
  3657. // Get selected/highlighted range while doing composition:
  3658. // returned range is relative to beginning of TextInputTarget, not composition string
  3659. Range<int> getCompositionSelection (HIMC hImc, LPARAM lParam) const
  3660. {
  3661. jassert (hImc != HIMC{});
  3662. int selectionStart = 0;
  3663. int selectionEnd = 0;
  3664. if ((lParam & GCS_COMPATTR) != 0)
  3665. {
  3666. // Get size of attributes array:
  3667. const int attributeSizeBytes = ImmGetCompositionString (hImc, GCS_COMPATTR, nullptr, 0);
  3668. if (attributeSizeBytes > 0)
  3669. {
  3670. // Get attributes (8 bit flag per character):
  3671. HeapBlock<char> attributes (attributeSizeBytes);
  3672. ImmGetCompositionString (hImc, GCS_COMPATTR, attributes, (DWORD) attributeSizeBytes);
  3673. selectionStart = 0;
  3674. for (selectionStart = 0; selectionStart < attributeSizeBytes; ++selectionStart)
  3675. if (attributes[selectionStart] == ATTR_TARGET_CONVERTED || attributes[selectionStart] == ATTR_TARGET_NOTCONVERTED)
  3676. break;
  3677. for (selectionEnd = selectionStart; selectionEnd < attributeSizeBytes; ++selectionEnd)
  3678. if (attributes[selectionEnd] != ATTR_TARGET_CONVERTED && attributes[selectionEnd] != ATTR_TARGET_NOTCONVERTED)
  3679. break;
  3680. }
  3681. }
  3682. return Range<int> (selectionStart, selectionEnd) + compositionRange.getStart();
  3683. }
  3684. void replaceCurrentSelection (TextInputTarget* const target, const String& newContent, Range<int> newSelection)
  3685. {
  3686. if (compositionInProgress)
  3687. target->setHighlightedRegion (compositionRange);
  3688. target->insertTextAtCaret (newContent);
  3689. compositionRange.setLength (newContent.length());
  3690. if (newSelection.getStart() < 0)
  3691. newSelection = Range<int>::emptyRange (compositionRange.getEnd());
  3692. target->setHighlightedRegion (newSelection);
  3693. }
  3694. Array<Range<int>> getCompositionUnderlines (HIMC hImc, LPARAM lParam) const
  3695. {
  3696. Array<Range<int>> result;
  3697. if (hImc != HIMC{} && (lParam & GCS_COMPCLAUSE) != 0)
  3698. {
  3699. auto clauseDataSizeBytes = ImmGetCompositionString (hImc, GCS_COMPCLAUSE, nullptr, 0);
  3700. if (clauseDataSizeBytes > 0)
  3701. {
  3702. const auto numItems = (size_t) clauseDataSizeBytes / sizeof (uint32);
  3703. HeapBlock<uint32> clauseData (numItems);
  3704. if (ImmGetCompositionString (hImc, GCS_COMPCLAUSE, clauseData, (DWORD) clauseDataSizeBytes) > 0)
  3705. for (size_t i = 0; i + 1 < numItems; ++i)
  3706. result.add (Range<int> ((int) clauseData[i], (int) clauseData[i + 1]) + compositionRange.getStart());
  3707. }
  3708. }
  3709. return result;
  3710. }
  3711. void moveCandidateWindowToLeftAlignWithSelection (HIMC hImc, ComponentPeer& peer, TextInputTarget* target) const
  3712. {
  3713. if (auto* targetComp = dynamic_cast<Component*> (target))
  3714. {
  3715. auto area = peer.getComponent().getLocalArea (targetComp, target->getCaretRectangle());
  3716. CANDIDATEFORM pos = { 0, CFS_CANDIDATEPOS, { area.getX(), area.getBottom() }, { 0, 0, 0, 0 } };
  3717. ImmSetCandidateWindow (hImc, &pos);
  3718. }
  3719. }
  3720. JUCE_DECLARE_NON_COPYABLE (IMEHandler)
  3721. };
  3722. void timerCallback() override
  3723. {
  3724. handlePositionChanged();
  3725. stopTimer();
  3726. }
  3727. static bool isAncestor (HWND outer, HWND inner)
  3728. {
  3729. if (outer == nullptr || inner == nullptr)
  3730. return false;
  3731. if (outer == inner)
  3732. return true;
  3733. return isAncestor (outer, GetAncestor (inner, GA_PARENT));
  3734. }
  3735. void windowShouldDismissModals (HWND originator)
  3736. {
  3737. if (shouldIgnoreModalDismiss)
  3738. return;
  3739. if (isAncestor (originator, hwnd))
  3740. sendInputAttemptWhenModalMessage();
  3741. }
  3742. // Unfortunately SetWindowsHookEx only allows us to register a static function as a hook.
  3743. // To get around this, we keep a static list of listeners which are interested in
  3744. // top-level window events, and notify all of these listeners from the callback.
  3745. class TopLevelModalDismissBroadcaster
  3746. {
  3747. public:
  3748. TopLevelModalDismissBroadcaster()
  3749. : hook (SetWindowsHookEx (WH_CALLWNDPROC,
  3750. callWndProc,
  3751. (HINSTANCE) juce::Process::getCurrentModuleInstanceHandle(),
  3752. GetCurrentThreadId()))
  3753. {}
  3754. ~TopLevelModalDismissBroadcaster() noexcept
  3755. {
  3756. UnhookWindowsHookEx (hook);
  3757. }
  3758. private:
  3759. static void processMessage (int nCode, const CWPSTRUCT* info)
  3760. {
  3761. if (nCode < 0 || info == nullptr)
  3762. return;
  3763. constexpr UINT events[] { WM_MOVE,
  3764. WM_SIZE,
  3765. WM_WINDOWPOSCHANGING,
  3766. WM_NCPOINTERDOWN,
  3767. WM_NCLBUTTONDOWN,
  3768. WM_NCRBUTTONDOWN,
  3769. WM_NCMBUTTONDOWN };
  3770. if (std::find (std::begin (events), std::end (events), info->message) == std::end (events))
  3771. return;
  3772. if (info->message == WM_WINDOWPOSCHANGING)
  3773. {
  3774. const auto* windowPos = reinterpret_cast<const WINDOWPOS*> (info->lParam);
  3775. const auto windowPosFlags = windowPos->flags;
  3776. constexpr auto maskToCheck = SWP_NOMOVE | SWP_NOSIZE;
  3777. if ((windowPosFlags & maskToCheck) == maskToCheck)
  3778. return;
  3779. }
  3780. // windowMayDismissModals could affect the number of active ComponentPeer instances
  3781. for (auto i = ComponentPeer::getNumPeers(); --i >= 0;)
  3782. if (i < ComponentPeer::getNumPeers())
  3783. if (auto* hwndPeer = dynamic_cast<HWNDComponentPeer*> (ComponentPeer::getPeer (i)))
  3784. hwndPeer->windowShouldDismissModals (info->hwnd);
  3785. }
  3786. static LRESULT CALLBACK callWndProc (int nCode, WPARAM wParam, LPARAM lParam)
  3787. {
  3788. processMessage (nCode, reinterpret_cast<CWPSTRUCT*> (lParam));
  3789. return CallNextHookEx ({}, nCode, wParam, lParam);
  3790. }
  3791. HHOOK hook;
  3792. };
  3793. SharedResourcePointer<TopLevelModalDismissBroadcaster> modalDismissBroadcaster;
  3794. IMEHandler imeHandler;
  3795. bool shouldIgnoreModalDismiss = false;
  3796. RectangleList<int> deferredRepaints;
  3797. ScopedSuspendResumeNotificationRegistration suspendResumeRegistration;
  3798. std::optional<SimpleTimer> monitorUpdateTimer;
  3799. //==============================================================================
  3800. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (HWNDComponentPeer)
  3801. };
  3802. MultiTouchMapper<DWORD> HWNDComponentPeer::currentTouches;
  3803. ModifierKeys HWNDComponentPeer::modifiersAtLastCallback;
  3804. ComponentPeer* Component::createNewPeer (int styleFlags, void* parentHWND)
  3805. {
  3806. return new HWNDComponentPeer (*this, styleFlags, (HWND) parentHWND, false);
  3807. }
  3808. JUCE_API ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component& component, void* parentHWND);
  3809. JUCE_API ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component& component, void* parentHWND)
  3810. {
  3811. return new HWNDComponentPeer (component, ComponentPeer::windowIgnoresMouseClicks,
  3812. (HWND) parentHWND, true);
  3813. }
  3814. JUCE_IMPLEMENT_SINGLETON (HWNDComponentPeer::WindowClassHolder)
  3815. //==============================================================================
  3816. bool KeyPress::isKeyCurrentlyDown (const int keyCode)
  3817. {
  3818. const auto k = [&]
  3819. {
  3820. if ((keyCode & extendedKeyModifier) != 0)
  3821. return keyCode;
  3822. const auto vk = BYTE (VkKeyScan ((WCHAR) keyCode) & 0xff);
  3823. return vk != (BYTE) -1 ? vk : keyCode;
  3824. }();
  3825. return HWNDComponentPeer::isKeyDown (k);
  3826. }
  3827. //==============================================================================
  3828. static DWORD getProcess (HWND hwnd)
  3829. {
  3830. DWORD result = 0;
  3831. GetWindowThreadProcessId (hwnd, &result);
  3832. return result;
  3833. }
  3834. /* Returns true if the viewComponent is embedded into a window
  3835. owned by the foreground process.
  3836. */
  3837. bool detail::WindowingHelpers::isEmbeddedInForegroundProcess (Component* c)
  3838. {
  3839. if (c == nullptr)
  3840. return false;
  3841. auto* peer = c->getPeer();
  3842. auto* hwnd = peer != nullptr ? static_cast<HWND> (peer->getNativeHandle()) : nullptr;
  3843. if (hwnd == nullptr)
  3844. return true;
  3845. const auto fgProcess = getProcess (GetForegroundWindow());
  3846. const auto ownerProcess = getProcess (GetAncestor (hwnd, GA_ROOTOWNER));
  3847. return fgProcess == ownerProcess;
  3848. }
  3849. bool JUCE_CALLTYPE Process::isForegroundProcess()
  3850. {
  3851. if (auto fg = GetForegroundWindow())
  3852. return getProcess (fg) == GetCurrentProcessId();
  3853. return true;
  3854. }
  3855. // N/A on Windows as far as I know.
  3856. void JUCE_CALLTYPE Process::makeForegroundProcess() {}
  3857. void JUCE_CALLTYPE Process::hide() {}
  3858. //==============================================================================
  3859. bool detail::MouseInputSourceList::addSource()
  3860. {
  3861. auto numSources = sources.size();
  3862. if (numSources == 0 || canUseMultiTouch())
  3863. {
  3864. addSource (numSources, numSources == 0 ? MouseInputSource::InputSourceType::mouse
  3865. : MouseInputSource::InputSourceType::touch);
  3866. return true;
  3867. }
  3868. return false;
  3869. }
  3870. bool detail::MouseInputSourceList::canUseTouch() const
  3871. {
  3872. return canUseMultiTouch();
  3873. }
  3874. Point<float> MouseInputSource::getCurrentRawMousePosition()
  3875. {
  3876. POINT mousePos;
  3877. GetCursorPos (&mousePos);
  3878. auto p = pointFromPOINT (mousePos);
  3879. if (isPerMonitorDPIAwareThread())
  3880. p = Desktop::getInstance().getDisplays().physicalToLogical (p);
  3881. return p.toFloat();
  3882. }
  3883. void MouseInputSource::setRawMousePosition (Point<float> newPosition)
  3884. {
  3885. auto newPositionInt = newPosition.roundToInt();
  3886. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  3887. if (isPerMonitorDPIAwareThread())
  3888. newPositionInt = Desktop::getInstance().getDisplays().logicalToPhysical (newPositionInt);
  3889. #endif
  3890. auto point = POINTFromPoint (newPositionInt);
  3891. SetCursorPos (point.x, point.y);
  3892. }
  3893. //==============================================================================
  3894. class ScreenSaverDefeater final : public Timer
  3895. {
  3896. public:
  3897. ScreenSaverDefeater()
  3898. {
  3899. startTimer (10000);
  3900. timerCallback();
  3901. }
  3902. void timerCallback() override
  3903. {
  3904. if (Process::isForegroundProcess())
  3905. {
  3906. INPUT input = {};
  3907. input.type = INPUT_MOUSE;
  3908. input.mi.mouseData = MOUSEEVENTF_MOVE;
  3909. SendInput (1, &input, sizeof (INPUT));
  3910. }
  3911. }
  3912. };
  3913. static std::unique_ptr<ScreenSaverDefeater> screenSaverDefeater;
  3914. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  3915. {
  3916. if (isEnabled)
  3917. screenSaverDefeater = nullptr;
  3918. else if (screenSaverDefeater == nullptr)
  3919. screenSaverDefeater.reset (new ScreenSaverDefeater());
  3920. }
  3921. bool Desktop::isScreenSaverEnabled()
  3922. {
  3923. return screenSaverDefeater == nullptr;
  3924. }
  3925. //==============================================================================
  3926. void LookAndFeel::playAlertSound()
  3927. {
  3928. MessageBeep (MB_OK);
  3929. }
  3930. //==============================================================================
  3931. void SystemClipboard::copyTextToClipboard (const String& text)
  3932. {
  3933. if (OpenClipboard (nullptr) != 0)
  3934. {
  3935. if (EmptyClipboard() != 0)
  3936. {
  3937. auto bytesNeeded = CharPointer_UTF16::getBytesRequiredFor (text.getCharPointer()) + 4;
  3938. if (bytesNeeded > 0)
  3939. {
  3940. if (auto bufH = GlobalAlloc (GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT, bytesNeeded + sizeof (WCHAR)))
  3941. {
  3942. if (auto* data = static_cast<WCHAR*> (GlobalLock (bufH)))
  3943. {
  3944. text.copyToUTF16 (data, bytesNeeded);
  3945. GlobalUnlock (bufH);
  3946. SetClipboardData (CF_UNICODETEXT, bufH);
  3947. }
  3948. }
  3949. }
  3950. }
  3951. CloseClipboard();
  3952. }
  3953. }
  3954. String SystemClipboard::getTextFromClipboard()
  3955. {
  3956. String result;
  3957. if (OpenClipboard (nullptr) != 0)
  3958. {
  3959. if (auto bufH = GetClipboardData (CF_UNICODETEXT))
  3960. {
  3961. if (auto* data = (const WCHAR*) GlobalLock (bufH))
  3962. {
  3963. result = String (data, (size_t) (GlobalSize (bufH) / sizeof (WCHAR)));
  3964. GlobalUnlock (bufH);
  3965. }
  3966. }
  3967. CloseClipboard();
  3968. }
  3969. return result;
  3970. }
  3971. //==============================================================================
  3972. void Desktop::setKioskComponent (Component* kioskModeComp, bool enableOrDisable, bool /*allowMenusAndBars*/)
  3973. {
  3974. if (auto* tlw = dynamic_cast<TopLevelWindow*> (kioskModeComp))
  3975. tlw->setUsingNativeTitleBar (! enableOrDisable);
  3976. if (kioskModeComp != nullptr && enableOrDisable)
  3977. kioskModeComp->setBounds (getDisplays().getDisplayForRect (kioskModeComp->getScreenBounds())->totalArea);
  3978. }
  3979. void Desktop::allowedOrientationsChanged() {}
  3980. //==============================================================================
  3981. static const Displays::Display* getCurrentDisplayFromScaleFactor (HWND hwnd)
  3982. {
  3983. Array<const Displays::Display*> candidateDisplays;
  3984. const auto scaleToLookFor = [&]
  3985. {
  3986. if (auto* peer = HWNDComponentPeer::getOwnerOfWindow (hwnd))
  3987. return peer->getPlatformScaleFactor();
  3988. return getScaleFactorForWindow (hwnd);
  3989. }();
  3990. auto globalScale = Desktop::getInstance().getGlobalScaleFactor();
  3991. for (auto& d : Desktop::getInstance().getDisplays().displays)
  3992. if (approximatelyEqual (d.scale / globalScale, scaleToLookFor))
  3993. candidateDisplays.add (&d);
  3994. if (candidateDisplays.size() > 0)
  3995. {
  3996. if (candidateDisplays.size() == 1)
  3997. return candidateDisplays[0];
  3998. const auto bounds = [&]
  3999. {
  4000. if (auto* peer = HWNDComponentPeer::getOwnerOfWindow (hwnd))
  4001. return peer->getComponent().getTopLevelComponent()->getBounds();
  4002. return Desktop::getInstance().getDisplays().physicalToLogical (rectangleFromRECT (getWindowScreenRect (hwnd)));
  4003. }();
  4004. const Displays::Display* retVal = nullptr;
  4005. int maxArea = -1;
  4006. for (auto* d : candidateDisplays)
  4007. {
  4008. auto intersection = d->totalArea.getIntersection (bounds);
  4009. auto area = intersection.getWidth() * intersection.getHeight();
  4010. if (area > maxArea)
  4011. {
  4012. maxArea = area;
  4013. retVal = d;
  4014. }
  4015. }
  4016. if (retVal != nullptr)
  4017. return retVal;
  4018. }
  4019. return Desktop::getInstance().getDisplays().getPrimaryDisplay();
  4020. }
  4021. //==============================================================================
  4022. struct MonitorInfo
  4023. {
  4024. MonitorInfo (bool main, RECT totalArea, RECT workArea, double d) noexcept
  4025. : isMain (main),
  4026. totalAreaRect (totalArea),
  4027. workAreaRect (workArea),
  4028. dpi (d)
  4029. {
  4030. }
  4031. bool isMain;
  4032. RECT totalAreaRect, workAreaRect;
  4033. double dpi;
  4034. };
  4035. static BOOL CALLBACK enumMonitorsProc (HMONITOR hm, HDC, LPRECT, LPARAM userInfo)
  4036. {
  4037. MONITORINFO info = {};
  4038. info.cbSize = sizeof (info);
  4039. GetMonitorInfo (hm, &info);
  4040. auto isMain = (info.dwFlags & 1 /* MONITORINFOF_PRIMARY */) != 0;
  4041. auto dpi = 0.0;
  4042. if (getDPIForMonitor != nullptr)
  4043. {
  4044. UINT dpiX = 0, dpiY = 0;
  4045. if (SUCCEEDED (getDPIForMonitor (hm, MDT_Default, &dpiX, &dpiY)))
  4046. dpi = (dpiX + dpiY) / 2.0;
  4047. }
  4048. ((Array<MonitorInfo>*) userInfo)->add ({ isMain, info.rcMonitor, info.rcWork, dpi });
  4049. return TRUE;
  4050. }
  4051. void Displays::findDisplays (float masterScale)
  4052. {
  4053. setDPIAwareness();
  4054. Array<MonitorInfo> monitors;
  4055. EnumDisplayMonitors (nullptr, nullptr, &enumMonitorsProc, (LPARAM) &monitors);
  4056. auto globalDPI = getGlobalDPI();
  4057. if (monitors.size() == 0)
  4058. {
  4059. auto windowRect = getWindowScreenRect (GetDesktopWindow());
  4060. monitors.add ({ true, windowRect, windowRect, globalDPI });
  4061. }
  4062. // make sure the first in the list is the main monitor
  4063. for (int i = 1; i < monitors.size(); ++i)
  4064. if (monitors.getReference (i).isMain)
  4065. monitors.swap (i, 0);
  4066. for (auto& monitor : monitors)
  4067. {
  4068. Display d;
  4069. d.isMain = monitor.isMain;
  4070. d.dpi = monitor.dpi;
  4071. if (approximatelyEqual (d.dpi, 0.0))
  4072. {
  4073. d.dpi = globalDPI;
  4074. d.scale = masterScale;
  4075. }
  4076. else
  4077. {
  4078. d.scale = (d.dpi / USER_DEFAULT_SCREEN_DPI) * (masterScale / Desktop::getDefaultMasterScale());
  4079. }
  4080. d.totalArea = rectangleFromRECT (monitor.totalAreaRect);
  4081. d.userArea = rectangleFromRECT (monitor.workAreaRect);
  4082. displays.add (d);
  4083. }
  4084. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  4085. if (isPerMonitorDPIAwareThread())
  4086. updateToLogical();
  4087. else
  4088. #endif
  4089. {
  4090. for (auto& d : displays)
  4091. {
  4092. d.totalArea /= masterScale;
  4093. d.userArea /= masterScale;
  4094. }
  4095. }
  4096. }
  4097. //==============================================================================
  4098. static auto extractFileHICON (const File& file)
  4099. {
  4100. WORD iconNum = 0;
  4101. WCHAR name[MAX_PATH * 2];
  4102. file.getFullPathName().copyToUTF16 (name, sizeof (name));
  4103. return IconConverters::IconPtr { ExtractAssociatedIcon ((HINSTANCE) Process::getCurrentModuleInstanceHandle(),
  4104. name,
  4105. &iconNum) };
  4106. }
  4107. Image detail::WindowingHelpers::createIconForFile (const File& file)
  4108. {
  4109. if (const auto icon = extractFileHICON (file))
  4110. return IconConverters::createImageFromHICON (icon.get());
  4111. return {};
  4112. }
  4113. //==============================================================================
  4114. class MouseCursor::PlatformSpecificHandle
  4115. {
  4116. public:
  4117. explicit PlatformSpecificHandle (const MouseCursor::StandardCursorType type)
  4118. : impl (makeHandle (type)) {}
  4119. explicit PlatformSpecificHandle (const detail::CustomMouseCursorInfo& info)
  4120. : impl (makeHandle (info)) {}
  4121. static void showInWindow (PlatformSpecificHandle* handle, ComponentPeer* peer)
  4122. {
  4123. SetCursor ([&]
  4124. {
  4125. if (handle != nullptr && handle->impl != nullptr && peer != nullptr)
  4126. return handle->impl->getCursor (*peer);
  4127. return LoadCursor (nullptr, IDC_ARROW);
  4128. }());
  4129. }
  4130. private:
  4131. struct Impl
  4132. {
  4133. virtual ~Impl() = default;
  4134. virtual HCURSOR getCursor (ComponentPeer&) = 0;
  4135. };
  4136. class BuiltinImpl : public Impl
  4137. {
  4138. public:
  4139. explicit BuiltinImpl (HCURSOR cursorIn)
  4140. : cursor (cursorIn) {}
  4141. HCURSOR getCursor (ComponentPeer&) override { return cursor; }
  4142. private:
  4143. HCURSOR cursor;
  4144. };
  4145. class ImageImpl : public Impl
  4146. {
  4147. public:
  4148. explicit ImageImpl (const detail::CustomMouseCursorInfo& infoIn) : info (infoIn) {}
  4149. HCURSOR getCursor (ComponentPeer& peer) override
  4150. {
  4151. JUCE_ASSERT_MESSAGE_THREAD;
  4152. static auto getCursorSize = getCursorSizeForPeerFunction();
  4153. const auto size = getCursorSize (peer);
  4154. const auto iter = cursorsBySize.find (size);
  4155. if (iter != cursorsBySize.end())
  4156. return iter->second.get();
  4157. const auto logicalSize = info.image.getScaledBounds();
  4158. const auto scale = (float) size / (float) unityCursorSize;
  4159. const auto physicalSize = logicalSize * scale;
  4160. const auto& image = info.image.getImage();
  4161. const auto rescaled = image.rescaled (roundToInt ((float) physicalSize.getWidth()),
  4162. roundToInt ((float) physicalSize.getHeight()));
  4163. const auto effectiveScale = rescaled.getWidth() / logicalSize.getWidth();
  4164. const auto hx = jlimit (0, rescaled.getWidth(), roundToInt ((float) info.hotspot.x * effectiveScale));
  4165. const auto hy = jlimit (0, rescaled.getHeight(), roundToInt ((float) info.hotspot.y * effectiveScale));
  4166. return cursorsBySize.emplace (size, CursorPtr { IconConverters::createHICONFromImage (rescaled, false, hx, hy) }).first->second.get();
  4167. }
  4168. private:
  4169. struct CursorDestructor
  4170. {
  4171. void operator() (HCURSOR ptr) const { if (ptr != nullptr) DestroyCursor (ptr); }
  4172. };
  4173. using CursorPtr = std::unique_ptr<std::remove_pointer_t<HCURSOR>, CursorDestructor>;
  4174. const detail::CustomMouseCursorInfo info;
  4175. std::map<int, CursorPtr> cursorsBySize;
  4176. };
  4177. static auto getCursorSizeForPeerFunction() -> int (*) (ComponentPeer&)
  4178. {
  4179. static const auto getDpiForMonitor = []() -> GetDPIForMonitorFunc
  4180. {
  4181. constexpr auto library = "SHCore.dll";
  4182. LoadLibraryA (library);
  4183. if (auto* handle = GetModuleHandleA (library))
  4184. return (GetDPIForMonitorFunc) GetProcAddress (handle, "GetDpiForMonitor");
  4185. return nullptr;
  4186. }();
  4187. static const auto getSystemMetricsForDpi = []() -> GetSystemMetricsForDpiFunc
  4188. {
  4189. constexpr auto library = "User32.dll";
  4190. LoadLibraryA (library);
  4191. if (auto* handle = GetModuleHandleA (library))
  4192. return (GetSystemMetricsForDpiFunc) GetProcAddress (handle, "GetSystemMetricsForDpi");
  4193. return nullptr;
  4194. }();
  4195. if (getDpiForMonitor == nullptr || getSystemMetricsForDpi == nullptr)
  4196. return [] (ComponentPeer&) { return unityCursorSize; };
  4197. return [] (ComponentPeer& p)
  4198. {
  4199. const ScopedThreadDPIAwarenessSetter threadDpiAwarenessSetter { p.getNativeHandle() };
  4200. UINT dpiX = 0, dpiY = 0;
  4201. if (auto* monitor = MonitorFromWindow ((HWND) p.getNativeHandle(), MONITOR_DEFAULTTONULL))
  4202. if (SUCCEEDED (getDpiForMonitor (monitor, MDT_Default, &dpiX, &dpiY)))
  4203. return getSystemMetricsForDpi (SM_CXCURSOR, dpiX);
  4204. return unityCursorSize;
  4205. };
  4206. }
  4207. static constexpr auto unityCursorSize = 32;
  4208. static std::unique_ptr<Impl> makeHandle (const detail::CustomMouseCursorInfo& info)
  4209. {
  4210. return std::make_unique<ImageImpl> (info);
  4211. }
  4212. static std::unique_ptr<Impl> makeHandle (const MouseCursor::StandardCursorType type)
  4213. {
  4214. LPCTSTR cursorName = IDC_ARROW;
  4215. switch (type)
  4216. {
  4217. case NormalCursor:
  4218. case ParentCursor: break;
  4219. case NoCursor: return std::make_unique<BuiltinImpl> (nullptr);
  4220. case WaitCursor: cursorName = IDC_WAIT; break;
  4221. case IBeamCursor: cursorName = IDC_IBEAM; break;
  4222. case PointingHandCursor: cursorName = MAKEINTRESOURCE (32649); break;
  4223. case CrosshairCursor: cursorName = IDC_CROSS; break;
  4224. case LeftRightResizeCursor:
  4225. case LeftEdgeResizeCursor:
  4226. case RightEdgeResizeCursor: cursorName = IDC_SIZEWE; break;
  4227. case UpDownResizeCursor:
  4228. case TopEdgeResizeCursor:
  4229. case BottomEdgeResizeCursor: cursorName = IDC_SIZENS; break;
  4230. case TopLeftCornerResizeCursor:
  4231. case BottomRightCornerResizeCursor: cursorName = IDC_SIZENWSE; break;
  4232. case TopRightCornerResizeCursor:
  4233. case BottomLeftCornerResizeCursor: cursorName = IDC_SIZENESW; break;
  4234. case UpDownLeftRightResizeCursor: cursorName = IDC_SIZEALL; break;
  4235. case DraggingHandCursor:
  4236. {
  4237. static const unsigned char dragHandData[]
  4238. { 71,73,70,56,57,97,16,0,16,0,145,2,0,0,0,0,255,255,255,0,0,0,0,0,0,33,249,4,1,0,0,2,0,44,0,0,0,0,16,0,
  4239. 16,0,0,2,52,148,47,0,200,185,16,130,90,12,74,139,107,84,123,39,132,117,151,116,132,146,248,60,209,138,
  4240. 98,22,203,114,34,236,37,52,77,217,247,154,191,119,110,240,193,128,193,95,163,56,60,234,98,135,2,0,59 };
  4241. return makeHandle ({ ScaledImage (ImageFileFormat::loadFrom (dragHandData, sizeof (dragHandData))), { 8, 7 } });
  4242. }
  4243. case CopyingCursor:
  4244. {
  4245. static const unsigned char copyCursorData[]
  4246. { 71,73,70,56,57,97,21,0,21,0,145,0,0,0,0,0,255,255,255,0,128,128,255,255,255,33,249,4,1,0,0,3,0,44,0,0,0,0,21,0,
  4247. 21,0,0,2,72,4,134,169,171,16,199,98,11,79,90,71,161,93,56,111,78,133,218,215,137,31,82,154,100,200,86,91,202,142,
  4248. 12,108,212,87,235,174, 15,54,214,126,237,226,37,96,59,141,16,37,18,201,142,157,230,204,51,112,252,114,147,74,83,
  4249. 5,50,68,147,208,217,16,71,149,252,124,5,0,59,0,0 };
  4250. return makeHandle ({ ScaledImage (ImageFileFormat::loadFrom (copyCursorData, sizeof (copyCursorData))), { 1, 3 } });
  4251. }
  4252. case NumStandardCursorTypes: JUCE_FALLTHROUGH
  4253. default:
  4254. jassertfalse; break;
  4255. }
  4256. return std::make_unique<BuiltinImpl> ([&]
  4257. {
  4258. if (auto* c = LoadCursor (nullptr, cursorName))
  4259. return c;
  4260. return LoadCursor (nullptr, IDC_ARROW);
  4261. }());
  4262. }
  4263. std::unique_ptr<Impl> impl;
  4264. };
  4265. //==============================================================================
  4266. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  4267. //==============================================================================
  4268. JUCE_COMCLASS (JuceIVirtualDesktopManager, "a5cd92ff-29be-454c-8d04-d82879fb3f1b") : public IUnknown
  4269. {
  4270. public:
  4271. virtual HRESULT STDMETHODCALLTYPE IsWindowOnCurrentVirtualDesktop(
  4272. __RPC__in HWND topLevelWindow,
  4273. __RPC__out BOOL * onCurrentDesktop) = 0;
  4274. virtual HRESULT STDMETHODCALLTYPE GetWindowDesktopId(
  4275. __RPC__in HWND topLevelWindow,
  4276. __RPC__out GUID * desktopId) = 0;
  4277. virtual HRESULT STDMETHODCALLTYPE MoveWindowToDesktop(
  4278. __RPC__in HWND topLevelWindow,
  4279. __RPC__in REFGUID desktopId) = 0;
  4280. };
  4281. JUCE_COMCLASS (JuceVirtualDesktopManager, "aa509086-5ca9-4c25-8f95-589d3c07b48a");
  4282. } // namespace juce
  4283. #ifdef __CRT_UUID_DECL
  4284. __CRT_UUID_DECL (juce::JuceIVirtualDesktopManager, 0xa5cd92ff, 0x29be, 0x454c, 0x8d, 0x04, 0xd8, 0x28, 0x79, 0xfb, 0x3f, 0x1b)
  4285. __CRT_UUID_DECL (juce::JuceVirtualDesktopManager, 0xaa509086, 0x5ca9, 0x4c25, 0x8f, 0x95, 0x58, 0x9d, 0x3c, 0x07, 0xb4, 0x8a)
  4286. #endif
  4287. bool juce::detail::WindowingHelpers::isWindowOnCurrentVirtualDesktop (void* x)
  4288. {
  4289. if (x == nullptr)
  4290. return false;
  4291. static auto* desktopManager = []
  4292. {
  4293. JuceIVirtualDesktopManager* result = nullptr;
  4294. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  4295. if (SUCCEEDED (CoCreateInstance (__uuidof (JuceVirtualDesktopManager), nullptr, CLSCTX_ALL, IID_PPV_ARGS (&result))))
  4296. return result;
  4297. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  4298. return static_cast<JuceIVirtualDesktopManager*> (nullptr);
  4299. }();
  4300. BOOL current = false;
  4301. if (auto* dm = desktopManager)
  4302. if (SUCCEEDED (dm->IsWindowOnCurrentVirtualDesktop (static_cast<HWND> (x), &current)))
  4303. return current != false;
  4304. return true;
  4305. }