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.

307 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. static const unsigned int specialId = WM_APP + 0x4400;
  23. static const unsigned int broadcastId = WM_APP + 0x4403;
  24. static const unsigned int specialCallbackId = WM_APP + 0x4402;
  25. static const TCHAR* const messageWindowName = _T("JUCEWindow");
  26. HWND juce_messageWindowHandle = 0;
  27. extern long improbableWindowNumber; // defined in windowing.cpp
  28. #ifndef WM_APPCOMMAND
  29. #define WM_APPCOMMAND 0x0319
  30. #endif
  31. //==============================================================================
  32. static LRESULT CALLBACK juce_MessageWndProc (HWND h,
  33. const UINT message,
  34. const WPARAM wParam,
  35. const LPARAM lParam) throw()
  36. {
  37. JUCE_TRY
  38. {
  39. if (h == juce_messageWindowHandle)
  40. {
  41. if (message == specialCallbackId)
  42. {
  43. MessageCallbackFunction* const func = (MessageCallbackFunction*) wParam;
  44. return (LRESULT) (*func) ((void*) lParam);
  45. }
  46. else if (message == specialId)
  47. {
  48. // these are trapped early in the dispatch call, but must also be checked
  49. // here in case there are windows modal dialog boxes doing their own
  50. // dispatch loop and not calling our version
  51. Message* const message = reinterpret_cast <Message*> (lParam);
  52. MessageManager::getInstance()->deliverMessage (message);
  53. message->decReferenceCount();
  54. return 0;
  55. }
  56. else if (message == broadcastId)
  57. {
  58. const ScopedPointer <String> messageString ((String*) lParam);
  59. MessageManager::getInstance()->deliverBroadcastMessage (*messageString);
  60. return 0;
  61. }
  62. else if (message == WM_COPYDATA && ((const COPYDATASTRUCT*) lParam)->dwData == broadcastId)
  63. {
  64. const String messageString ((const juce_wchar*) ((const COPYDATASTRUCT*) lParam)->lpData,
  65. ((const COPYDATASTRUCT*) lParam)->cbData / sizeof (juce_wchar));
  66. PostMessage (juce_messageWindowHandle, broadcastId, 0, (LPARAM) new String (messageString));
  67. return 0;
  68. }
  69. }
  70. }
  71. JUCE_CATCH_EXCEPTION
  72. return DefWindowProc (h, message, wParam, lParam);
  73. }
  74. static bool isEventBlockedByModalComps (MSG& m)
  75. {
  76. if (Component::getNumCurrentlyModalComponents() == 0
  77. || GetWindowLong (m.hwnd, GWLP_USERDATA) == improbableWindowNumber)
  78. return false;
  79. switch (m.message)
  80. {
  81. case WM_MOUSEMOVE:
  82. case WM_NCMOUSEMOVE:
  83. case 0x020A: /* WM_MOUSEWHEEL */
  84. case 0x020E: /* WM_MOUSEHWHEEL */
  85. case WM_KEYUP:
  86. case WM_SYSKEYUP:
  87. case WM_CHAR:
  88. case WM_APPCOMMAND:
  89. case WM_LBUTTONUP:
  90. case WM_MBUTTONUP:
  91. case WM_RBUTTONUP:
  92. case WM_MOUSEACTIVATE:
  93. case WM_NCMOUSEHOVER:
  94. case WM_MOUSEHOVER:
  95. return true;
  96. case WM_NCLBUTTONDOWN:
  97. case WM_NCLBUTTONDBLCLK:
  98. case WM_NCRBUTTONDOWN:
  99. case WM_NCRBUTTONDBLCLK:
  100. case WM_NCMBUTTONDOWN:
  101. case WM_NCMBUTTONDBLCLK:
  102. case WM_LBUTTONDOWN:
  103. case WM_LBUTTONDBLCLK:
  104. case WM_MBUTTONDOWN:
  105. case WM_MBUTTONDBLCLK:
  106. case WM_RBUTTONDOWN:
  107. case WM_RBUTTONDBLCLK:
  108. case WM_KEYDOWN:
  109. case WM_SYSKEYDOWN:
  110. {
  111. Component* const modal = Component::getCurrentlyModalComponent (0);
  112. if (modal != 0)
  113. modal->inputAttemptWhenModal();
  114. return true;
  115. }
  116. default:
  117. break;
  118. }
  119. return false;
  120. }
  121. bool juce_dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
  122. {
  123. MSG m;
  124. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, 0))
  125. return false;
  126. if (GetMessage (&m, (HWND) 0, 0, 0) >= 0)
  127. {
  128. if (m.message == specialId && m.hwnd == juce_messageWindowHandle)
  129. {
  130. Message* const message = reinterpret_cast <Message*> (m.lParam);
  131. MessageManager::getInstance()->deliverMessage (message);
  132. message->decReferenceCount();
  133. }
  134. else if (m.message == WM_QUIT)
  135. {
  136. if (JUCEApplication::getInstance() != 0)
  137. JUCEApplication::getInstance()->systemRequestedQuit();
  138. }
  139. else if (! isEventBlockedByModalComps (m))
  140. {
  141. if ((m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN)
  142. && GetWindowLong (m.hwnd, GWLP_USERDATA) != improbableWindowNumber)
  143. {
  144. // if it's someone else's window being clicked on, and the focus is
  145. // currently on a juce window, pass the kb focus over..
  146. HWND currentFocus = GetFocus();
  147. if (currentFocus == 0 || GetWindowLong (currentFocus, GWLP_USERDATA) == improbableWindowNumber)
  148. SetFocus (m.hwnd);
  149. }
  150. TranslateMessage (&m);
  151. DispatchMessage (&m);
  152. }
  153. }
  154. return true;
  155. }
  156. //==============================================================================
  157. bool juce_postMessageToSystemQueue (Message* message)
  158. {
  159. message->incReferenceCount();
  160. return PostMessage (juce_messageWindowHandle, specialId, 0, (LPARAM) message) != 0;
  161. }
  162. //==============================================================================
  163. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
  164. void* userData)
  165. {
  166. if (MessageManager::getInstance()->isThisTheMessageThread())
  167. {
  168. return (*callback) (userData);
  169. }
  170. else
  171. {
  172. // If a thread has a MessageManagerLock and then tries to call this method, it'll
  173. // deadlock because the message manager is blocked from running, and can't
  174. // call your function..
  175. jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  176. return (void*) SendMessage (juce_messageWindowHandle,
  177. specialCallbackId,
  178. (WPARAM) callback,
  179. (LPARAM) userData);
  180. }
  181. }
  182. //==============================================================================
  183. static BOOL CALLBACK BroadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  184. {
  185. if (hwnd != juce_messageWindowHandle)
  186. reinterpret_cast <Array<void*>*> (lParam)->add ((void*) hwnd);
  187. return TRUE;
  188. }
  189. void MessageManager::broadcastMessage (const String& value)
  190. {
  191. Array<void*> windows;
  192. EnumWindows (&BroadcastEnumWindowProc, (LPARAM) &windows);
  193. const String localCopy (value);
  194. COPYDATASTRUCT data;
  195. data.dwData = broadcastId;
  196. data.cbData = (localCopy.length() + 1) * sizeof (juce_wchar);
  197. data.lpData = (void*) localCopy.toUTF16().getAddress();
  198. for (int i = windows.size(); --i >= 0;)
  199. {
  200. HWND hwnd = (HWND) windows.getUnchecked(i);
  201. TCHAR windowName [64]; // no need to read longer strings than this
  202. GetWindowText (hwnd, windowName, 64);
  203. windowName [63] = 0;
  204. if (String (windowName) == messageWindowName)
  205. {
  206. DWORD_PTR result;
  207. SendMessageTimeout (hwnd, WM_COPYDATA,
  208. (WPARAM) juce_messageWindowHandle,
  209. (LPARAM) &data,
  210. SMTO_BLOCK | SMTO_ABORTIFHUNG,
  211. 8000,
  212. &result);
  213. }
  214. }
  215. }
  216. //==============================================================================
  217. static const String getMessageWindowClassName()
  218. {
  219. // this name has to be different for each app/dll instance because otherwise
  220. // poor old Win32 can get a bit confused (even despite it not being a process-global
  221. // window class).
  222. static int number = 0;
  223. if (number == 0)
  224. number = 0x7fffffff & (int) Time::getHighResolutionTicks();
  225. return "JUCEcs_" + String (number);
  226. }
  227. void MessageManager::doPlatformSpecificInitialisation()
  228. {
  229. OleInitialize (0);
  230. const String className (getMessageWindowClassName());
  231. HMODULE hmod = (HMODULE) PlatformUtilities::getCurrentModuleInstanceHandle();
  232. WNDCLASSEX wc;
  233. zerostruct (wc);
  234. wc.cbSize = sizeof (wc);
  235. wc.lpfnWndProc = (WNDPROC) juce_MessageWndProc;
  236. wc.cbWndExtra = 4;
  237. wc.hInstance = hmod;
  238. wc.lpszClassName = className.toUTF16();
  239. RegisterClassEx (&wc);
  240. juce_messageWindowHandle = CreateWindow (wc.lpszClassName,
  241. messageWindowName,
  242. 0, 0, 0, 0, 0, 0, 0,
  243. hmod, 0);
  244. }
  245. void MessageManager::doPlatformSpecificShutdown()
  246. {
  247. DestroyWindow (juce_messageWindowHandle);
  248. UnregisterClass (getMessageWindowClassName().toUTF16(), 0);
  249. OleUninitialize();
  250. }
  251. #endif