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.

250 lines
8.9KB

  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. bool juce_dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
  76. {
  77. MSG m;
  78. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, 0))
  79. return false;
  80. if (GetMessage (&m, (HWND) 0, 0, 0) > 0)
  81. {
  82. const MessageManagerLock mml;
  83. if (m.message == specialId
  84. && m.hwnd == juce_messageWindowHandle)
  85. {
  86. MessageManager::getInstance()->deliverMessage ((void*) m.lParam);
  87. }
  88. else
  89. {
  90. if (GetWindowLong (m.hwnd, GWLP_USERDATA) != improbableWindowNumber
  91. && (m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN))
  92. {
  93. // if it's someone else's window being clicked on, and the focus is
  94. // currently on a juce window, pass the kb focus over..
  95. HWND currentFocus = GetFocus();
  96. if (currentFocus == 0 || GetWindowLong (currentFocus, GWLP_USERDATA) == improbableWindowNumber)
  97. SetFocus (m.hwnd);
  98. }
  99. TranslateMessage (&m);
  100. DispatchMessage (&m);
  101. }
  102. }
  103. return true;
  104. }
  105. //==============================================================================
  106. bool juce_postMessageToSystemQueue (void* message)
  107. {
  108. return PostMessage (juce_messageWindowHandle, specialId, 0, (LPARAM) message) != 0;
  109. }
  110. //==============================================================================
  111. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
  112. void* userData)
  113. {
  114. if (MessageManager::getInstance()->isThisTheMessageThread())
  115. {
  116. return (*callback) (userData);
  117. }
  118. else
  119. {
  120. // If a thread has a MessageManagerLock and then tries to call this method, it'll
  121. // deadlock because the message manager is blocked from running, and can't
  122. // call your function..
  123. jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  124. return (void*) SendMessage (juce_messageWindowHandle,
  125. specialCallbackId,
  126. (WPARAM) callback,
  127. (LPARAM) userData);
  128. }
  129. }
  130. //==============================================================================
  131. static BOOL CALLBACK BroadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  132. {
  133. if (hwnd != juce_messageWindowHandle)
  134. (reinterpret_cast <VoidArray*> (lParam))->add ((void*) hwnd);
  135. return TRUE;
  136. }
  137. void MessageManager::broadcastMessage (const String& value) throw()
  138. {
  139. VoidArray windows;
  140. EnumWindows (&BroadcastEnumWindowProc, (LPARAM) &windows);
  141. const String localCopy (value);
  142. COPYDATASTRUCT data;
  143. data.dwData = broadcastId;
  144. data.cbData = (localCopy.length() + 1) * sizeof (juce_wchar);
  145. data.lpData = (void*) (const juce_wchar*) localCopy;
  146. for (int i = windows.size(); --i >= 0;)
  147. {
  148. HWND hwnd = (HWND) windows.getUnchecked(i);
  149. TCHAR windowName [64]; // no need to read longer strings than this
  150. GetWindowText (hwnd, windowName, 64);
  151. windowName [63] = 0;
  152. if (String (windowName) == String (messageWindowName))
  153. {
  154. DWORD_PTR result;
  155. SendMessageTimeout (hwnd, WM_COPYDATA,
  156. (WPARAM) juce_messageWindowHandle,
  157. (LPARAM) &data,
  158. SMTO_BLOCK | SMTO_ABORTIFHUNG,
  159. 8000,
  160. &result);
  161. }
  162. }
  163. }
  164. //==============================================================================
  165. static const String getMessageWindowClassName()
  166. {
  167. // this name has to be different for each app/dll instance because otherwise
  168. // poor old Win32 can get a bit confused (even despite it not being a process-global
  169. // window class).
  170. static int number = 0;
  171. if (number == 0)
  172. number = 0x7fffffff & (int) Time::getHighResolutionTicks();
  173. return T("JUCEcs_") + String (number);
  174. }
  175. void MessageManager::doPlatformSpecificInitialisation()
  176. {
  177. OleInitialize (0);
  178. const String className (getMessageWindowClassName());
  179. HMODULE hmod = (HMODULE) PlatformUtilities::getCurrentModuleInstanceHandle();
  180. WNDCLASSEX wc;
  181. zerostruct (wc);
  182. wc.cbSize = sizeof (wc);
  183. wc.lpfnWndProc = (WNDPROC) juce_MessageWndProc;
  184. wc.cbWndExtra = 4;
  185. wc.hInstance = hmod;
  186. wc.lpszClassName = className;
  187. RegisterClassEx (&wc);
  188. juce_messageWindowHandle = CreateWindow (wc.lpszClassName,
  189. messageWindowName,
  190. 0, 0, 0, 0, 0, 0, 0,
  191. hmod, 0);
  192. }
  193. void MessageManager::doPlatformSpecificShutdown()
  194. {
  195. DestroyWindow (juce_messageWindowHandle);
  196. UnregisterClass (getMessageWindowClassName(), 0);
  197. OleUninitialize();
  198. }
  199. #endif