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.

295 lines
10KB

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