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.

301 lines
10KB

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