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.

348 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // (This file gets included by juce_win32_NativeCode.cpp, rather than being
  19. // compiled on its own).
  20. #if JUCE_INCLUDED_FILE
  21. //==============================================================================
  22. class HiddenMessageWindow
  23. {
  24. public:
  25. HiddenMessageWindow (const TCHAR* const messageWindowName, WNDPROC wndProc)
  26. {
  27. String className ("JUCE_");
  28. className << String::toHexString (Time::getHighResolutionTicks());
  29. HMODULE moduleHandle = (HMODULE) PlatformUtilities::getCurrentModuleInstanceHandle();
  30. WNDCLASSEX wc = { 0 };
  31. wc.cbSize = sizeof (wc);
  32. wc.lpfnWndProc = wndProc;
  33. wc.cbWndExtra = 4;
  34. wc.hInstance = moduleHandle;
  35. wc.lpszClassName = className.toWideCharPointer();
  36. atom = RegisterClassEx (&wc);
  37. jassert (atom != 0);
  38. hwnd = CreateWindow (getClassNameFromAtom(), messageWindowName,
  39. 0, 0, 0, 0, 0, 0, 0, moduleHandle, 0);
  40. jassert (hwnd != 0);
  41. }
  42. ~HiddenMessageWindow()
  43. {
  44. DestroyWindow (hwnd);
  45. UnregisterClass (getClassNameFromAtom(), 0);
  46. }
  47. inline HWND getHWND() const noexcept { return hwnd; }
  48. private:
  49. ATOM atom;
  50. HWND hwnd;
  51. LPCTSTR getClassNameFromAtom() noexcept { return (LPCTSTR) MAKELONG (atom, 0); }
  52. };
  53. //==============================================================================
  54. HWND juce_messageWindowHandle = 0; // (this is referred to by other parts of the codebase)
  55. //==============================================================================
  56. class JuceWindowIdentifier
  57. {
  58. public:
  59. static bool isJUCEWindow (HWND hwnd) noexcept
  60. {
  61. return GetWindowLong (hwnd, GWLP_USERDATA) == improbableWindowNumber;
  62. }
  63. static void setAsJUCEWindow (HWND hwnd, bool isJuceWindow) noexcept
  64. {
  65. SetWindowLongPtr (hwnd, GWLP_USERDATA, isJuceWindow ? improbableWindowNumber : 0);
  66. }
  67. private:
  68. enum { improbableWindowNumber = 0xf965aa01 };
  69. };
  70. //==============================================================================
  71. namespace WindowsMessageHelpers
  72. {
  73. const unsigned int specialId = WM_APP + 0x4400;
  74. const unsigned int broadcastId = WM_APP + 0x4403;
  75. const unsigned int specialCallbackId = WM_APP + 0x4402;
  76. const TCHAR messageWindowName[] = _T("JUCEWindow");
  77. ScopedPointer<HiddenMessageWindow> messageWindow;
  78. //==============================================================================
  79. LRESULT CALLBACK messageWndProc (HWND h, const UINT message, const WPARAM wParam, const LPARAM lParam) noexcept
  80. {
  81. JUCE_TRY
  82. {
  83. if (h == juce_messageWindowHandle)
  84. {
  85. if (message == specialCallbackId)
  86. {
  87. MessageCallbackFunction* const func = (MessageCallbackFunction*) wParam;
  88. return (LRESULT) (*func) ((void*) lParam);
  89. }
  90. else if (message == specialId)
  91. {
  92. // these are trapped early in the dispatch call, but must also be checked
  93. // here in case there are windows modal dialog boxes doing their own
  94. // dispatch loop and not calling our version
  95. Message* const message = reinterpret_cast <Message*> (lParam);
  96. MessageManager::getInstance()->deliverMessage (message);
  97. message->decReferenceCount();
  98. return 0;
  99. }
  100. else if (message == broadcastId)
  101. {
  102. const ScopedPointer <String> messageString ((String*) lParam);
  103. MessageManager::getInstance()->deliverBroadcastMessage (*messageString);
  104. return 0;
  105. }
  106. else if (message == WM_COPYDATA && ((const COPYDATASTRUCT*) lParam)->dwData == broadcastId)
  107. {
  108. const COPYDATASTRUCT* data = (COPYDATASTRUCT*) lParam;
  109. const String messageString (CharPointer_UTF32 ((const CharPointer_UTF32::CharType*) data->lpData),
  110. data->cbData / sizeof (CharPointer_UTF32::CharType));
  111. PostMessage (juce_messageWindowHandle, broadcastId, 0, (LPARAM) new String (messageString));
  112. return 0;
  113. }
  114. }
  115. }
  116. JUCE_CATCH_EXCEPTION
  117. return DefWindowProc (h, message, wParam, lParam);
  118. }
  119. bool isHWNDBlockedByModalComponents (HWND h) noexcept
  120. {
  121. for (int i = Desktop::getInstance().getNumComponents(); --i >= 0;)
  122. {
  123. Component* const c = Desktop::getInstance().getComponent (i);
  124. if (c != nullptr
  125. && (! c->isCurrentlyBlockedByAnotherModalComponent())
  126. && IsChild ((HWND) c->getWindowHandle(), h))
  127. return false;
  128. }
  129. return true;
  130. }
  131. bool isEventBlockedByModalComps (MSG& m)
  132. {
  133. if (Component::getNumCurrentlyModalComponents() == 0 || JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  134. return false;
  135. switch (m.message)
  136. {
  137. case WM_MOUSEMOVE:
  138. case WM_NCMOUSEMOVE:
  139. case 0x020A: /* WM_MOUSEWHEEL */
  140. case 0x020E: /* WM_MOUSEHWHEEL */
  141. case WM_KEYUP:
  142. case WM_SYSKEYUP:
  143. case WM_CHAR:
  144. case WM_APPCOMMAND:
  145. case WM_LBUTTONUP:
  146. case WM_MBUTTONUP:
  147. case WM_RBUTTONUP:
  148. case WM_MOUSEACTIVATE:
  149. case WM_NCMOUSEHOVER:
  150. case WM_MOUSEHOVER:
  151. return isHWNDBlockedByModalComponents (m.hwnd);
  152. case WM_NCLBUTTONDOWN:
  153. case WM_NCLBUTTONDBLCLK:
  154. case WM_NCRBUTTONDOWN:
  155. case WM_NCRBUTTONDBLCLK:
  156. case WM_NCMBUTTONDOWN:
  157. case WM_NCMBUTTONDBLCLK:
  158. case WM_LBUTTONDOWN:
  159. case WM_LBUTTONDBLCLK:
  160. case WM_MBUTTONDOWN:
  161. case WM_MBUTTONDBLCLK:
  162. case WM_RBUTTONDOWN:
  163. case WM_RBUTTONDBLCLK:
  164. case WM_KEYDOWN:
  165. case WM_SYSKEYDOWN:
  166. if (isHWNDBlockedByModalComponents (m.hwnd))
  167. {
  168. Component* const modal = Component::getCurrentlyModalComponent (0);
  169. if (modal != nullptr)
  170. modal->inputAttemptWhenModal();
  171. return true;
  172. }
  173. break;
  174. default:
  175. break;
  176. }
  177. return false;
  178. }
  179. BOOL CALLBACK broadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  180. {
  181. if (hwnd != juce_messageWindowHandle)
  182. reinterpret_cast <Array<HWND>*> (lParam)->add (hwnd);
  183. return TRUE;
  184. }
  185. }
  186. //==============================================================================
  187. bool MessageManager::dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
  188. {
  189. using namespace WindowsMessageHelpers;
  190. MSG m;
  191. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, 0))
  192. return false;
  193. if (GetMessage (&m, (HWND) 0, 0, 0) >= 0)
  194. {
  195. if (m.message == specialId && m.hwnd == juce_messageWindowHandle)
  196. {
  197. Message* const message = reinterpret_cast <Message*> (m.lParam);
  198. MessageManager::getInstance()->deliverMessage (message);
  199. message->decReferenceCount();
  200. }
  201. else if (m.message == WM_QUIT)
  202. {
  203. if (JUCEApplication::getInstance() != nullptr)
  204. JUCEApplication::getInstance()->systemRequestedQuit();
  205. }
  206. else if (! WindowsMessageHelpers::isEventBlockedByModalComps (m))
  207. {
  208. if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
  209. && ! JuceWindowIdentifier::isJUCEWindow (m.hwnd))
  210. {
  211. // if it's someone else's window being clicked on, and the focus is
  212. // currently on a juce window, pass the kb focus over..
  213. HWND currentFocus = GetFocus();
  214. if (currentFocus == 0 || JuceWindowIdentifier::isJUCEWindow (currentFocus))
  215. SetFocus (m.hwnd);
  216. }
  217. TranslateMessage (&m);
  218. DispatchMessage (&m);
  219. }
  220. }
  221. return true;
  222. }
  223. bool MessageManager::postMessageToSystemQueue (Message* message)
  224. {
  225. message->incReferenceCount();
  226. return PostMessage (juce_messageWindowHandle, WindowsMessageHelpers::specialId, 0, (LPARAM) message) != 0;
  227. }
  228. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
  229. void* userData)
  230. {
  231. if (MessageManager::getInstance()->isThisTheMessageThread())
  232. {
  233. return (*callback) (userData);
  234. }
  235. else
  236. {
  237. // If a thread has a MessageManagerLock and then tries to call this method, it'll
  238. // deadlock because the message manager is blocked from running, and can't
  239. // call your function..
  240. jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  241. return (void*) SendMessage (juce_messageWindowHandle,
  242. WindowsMessageHelpers::specialCallbackId,
  243. (WPARAM) callback,
  244. (LPARAM) userData);
  245. }
  246. }
  247. void MessageManager::broadcastMessage (const String& value)
  248. {
  249. Array<HWND> windows;
  250. EnumWindows (&WindowsMessageHelpers::broadcastEnumWindowProc, (LPARAM) &windows);
  251. const String localCopy (value);
  252. COPYDATASTRUCT data;
  253. data.dwData = WindowsMessageHelpers::broadcastId;
  254. data.cbData = (localCopy.length() + 1) * sizeof (CharPointer_UTF32::CharType);
  255. data.lpData = (void*) localCopy.toUTF32().getAddress();
  256. for (int i = windows.size(); --i >= 0;)
  257. {
  258. HWND hwnd = windows.getUnchecked(i);
  259. TCHAR windowName [64]; // no need to read longer strings than this
  260. GetWindowText (hwnd, windowName, 64);
  261. windowName [63] = 0;
  262. if (String (windowName) == WindowsMessageHelpers::messageWindowName)
  263. {
  264. DWORD_PTR result;
  265. SendMessageTimeout (hwnd, WM_COPYDATA,
  266. (WPARAM) juce_messageWindowHandle,
  267. (LPARAM) &data,
  268. SMTO_BLOCK | SMTO_ABORTIFHUNG, 8000, &result);
  269. }
  270. }
  271. }
  272. //==============================================================================
  273. void MessageManager::doPlatformSpecificInitialisation()
  274. {
  275. OleInitialize (0);
  276. using namespace WindowsMessageHelpers;
  277. messageWindow = new HiddenMessageWindow (messageWindowName, (WNDPROC) messageWndProc);
  278. juce_messageWindowHandle = messageWindow->getHWND();
  279. }
  280. void MessageManager::doPlatformSpecificShutdown()
  281. {
  282. WindowsMessageHelpers::messageWindow = nullptr;
  283. OleUninitialize();
  284. }
  285. #endif