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.

299 lines
11KB

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