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.

254 lines
9.2KB

  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. #include "win32_headers.h"
  24. #include "../../../src/juce_core/basics/juce_StandardHeader.h"
  25. BEGIN_JUCE_NAMESPACE
  26. #include "../../../src/juce_appframework/events/juce_MessageManager.h"
  27. #include "../../../src/juce_appframework/application/juce_Application.h"
  28. #include "../../../src/juce_core/basics/juce_SystemStats.h"
  29. #include "../../../src/juce_core/misc/juce_PlatformUtilities.h"
  30. #include "../../../src/juce_core/basics/juce_Time.h"
  31. //==============================================================================
  32. static const unsigned int specialId = WM_APP + 0x4400;
  33. static const unsigned int broadcastId = WM_APP + 0x4403;
  34. static const unsigned int specialCallbackId = WM_APP + 0x4402;
  35. static const TCHAR* const messageWindowName = _T("JUCEWindow");
  36. HWND juce_messageWindowHandle = 0;
  37. extern long improbableWindowNumber; // defined in windowing.cpp
  38. //==============================================================================
  39. static LRESULT CALLBACK juce_MessageWndProc (HWND h,
  40. const UINT message,
  41. const WPARAM wParam,
  42. const LPARAM lParam) throw()
  43. {
  44. JUCE_TRY
  45. {
  46. if (h == juce_messageWindowHandle)
  47. {
  48. if (message == specialCallbackId)
  49. {
  50. MessageCallbackFunction* const func = (MessageCallbackFunction*) wParam;
  51. return (LRESULT) (*func) ((void*) lParam);
  52. }
  53. else if (message == specialId)
  54. {
  55. // these are trapped early in the dispatch call, but must also be checked
  56. // here in case there are windows modal dialog boxes doing their own
  57. // dispatch loop and not calling our version
  58. MessageManager::getInstance()->deliverMessage ((void*) lParam);
  59. return 0;
  60. }
  61. else if (message == broadcastId)
  62. {
  63. String* const messageString = (String*) lParam;
  64. MessageManager::getInstance()->deliverBroadcastMessage (*messageString);
  65. delete messageString;
  66. return 0;
  67. }
  68. else if (message == WM_COPYDATA && ((const COPYDATASTRUCT*) lParam)->dwData == broadcastId)
  69. {
  70. const String messageString ((const juce_wchar*) ((const COPYDATASTRUCT*) lParam)->lpData,
  71. ((const COPYDATASTRUCT*) lParam)->cbData / sizeof (juce_wchar));
  72. PostMessage (juce_messageWindowHandle, broadcastId, 0, (LPARAM) new String (messageString));
  73. return 0;
  74. }
  75. }
  76. }
  77. JUCE_CATCH_EXCEPTION
  78. return DefWindowProc (h, message, wParam, lParam);
  79. }
  80. bool juce_dispatchNextMessageOnSystemQueue (const bool returnIfNoPendingMessages)
  81. {
  82. MSG m;
  83. if (returnIfNoPendingMessages && ! PeekMessage (&m, (HWND) 0, 0, 0, 0))
  84. return false;
  85. if (GetMessage (&m, (HWND) 0, 0, 0) > 0)
  86. {
  87. if (m.message == specialId
  88. && m.hwnd == juce_messageWindowHandle)
  89. {
  90. MessageManager::getInstance()->deliverMessage ((void*) m.lParam);
  91. }
  92. else
  93. {
  94. if (GetWindowLong (m.hwnd, GWLP_USERDATA) != improbableWindowNumber
  95. && (m.message == WM_LBUTTONDOWN || m.message == WM_RBUTTONDOWN))
  96. {
  97. // if it's someone else's window being clicked on, and the focus is
  98. // currently on a juce window, pass the kb focus over..
  99. HWND currentFocus = GetFocus();
  100. if (currentFocus == 0 || GetWindowLong (currentFocus, GWLP_USERDATA) == improbableWindowNumber)
  101. SetFocus (m.hwnd);
  102. }
  103. TranslateMessage (&m);
  104. DispatchMessage (&m);
  105. }
  106. }
  107. return true;
  108. }
  109. //==============================================================================
  110. bool juce_postMessageToSystemQueue (void* message)
  111. {
  112. return PostMessage (juce_messageWindowHandle, specialId, 0, (LPARAM) message) != 0;
  113. }
  114. //==============================================================================
  115. void* MessageManager::callFunctionOnMessageThread (MessageCallbackFunction* callback,
  116. void* userData)
  117. {
  118. if (MessageManager::getInstance()->isThisTheMessageThread())
  119. {
  120. return (*callback) (userData);
  121. }
  122. else
  123. {
  124. // If a thread has a MessageManagerLock and then tries to call this method, it'll
  125. // deadlock because the message manager is blocked from running, and can't
  126. // call your function..
  127. jassert (! MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  128. return (void*) SendMessage (juce_messageWindowHandle,
  129. specialCallbackId,
  130. (WPARAM) callback,
  131. (LPARAM) userData);
  132. }
  133. }
  134. //==============================================================================
  135. static BOOL CALLBACK BroadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  136. {
  137. if (hwnd != juce_messageWindowHandle)
  138. (reinterpret_cast <VoidArray*> (lParam))->add ((void*) hwnd);
  139. return TRUE;
  140. }
  141. void MessageManager::broadcastMessage (const String& value) throw()
  142. {
  143. VoidArray windows;
  144. EnumWindows (&BroadcastEnumWindowProc, (LPARAM) &windows);
  145. const String localCopy (value);
  146. COPYDATASTRUCT data;
  147. data.dwData = broadcastId;
  148. data.cbData = (localCopy.length() + 1) * sizeof (juce_wchar);
  149. data.lpData = (void*) (const juce_wchar*) localCopy;
  150. for (int i = windows.size(); --i >= 0;)
  151. {
  152. HWND hwnd = (HWND) windows.getUnchecked(i);
  153. TCHAR windowName [64]; // no need to read longer strings than this
  154. GetWindowText (hwnd, windowName, 64);
  155. windowName [63] = 0;
  156. if (String (windowName) == String (messageWindowName))
  157. {
  158. DWORD_PTR result;
  159. SendMessageTimeout (hwnd, WM_COPYDATA,
  160. (WPARAM) juce_messageWindowHandle,
  161. (LPARAM) &data,
  162. SMTO_BLOCK | SMTO_ABORTIFHUNG,
  163. 8000,
  164. &result);
  165. }
  166. }
  167. }
  168. //==============================================================================
  169. static const String getMessageWindowClassName()
  170. {
  171. // this name has to be different for each app/dll instance because otherwise
  172. // poor old Win32 can get a bit confused (even despite it not being a process-global
  173. // window class).
  174. static int number = 0;
  175. if (number == 0)
  176. number = 0x7fffffff & (int) Time::getHighResolutionTicks();
  177. return T("JUCEcs_") + String (number);
  178. }
  179. void MessageManager::doPlatformSpecificInitialisation()
  180. {
  181. OleInitialize (0);
  182. const String className (getMessageWindowClassName());
  183. HMODULE hmod = (HMODULE) PlatformUtilities::getCurrentModuleInstanceHandle();
  184. WNDCLASSEX wc;
  185. zerostruct (wc);
  186. wc.cbSize = sizeof (wc);
  187. wc.lpfnWndProc = (WNDPROC) juce_MessageWndProc;
  188. wc.cbWndExtra = 4;
  189. wc.hInstance = hmod;
  190. wc.lpszClassName = className;
  191. RegisterClassEx (&wc);
  192. juce_messageWindowHandle = CreateWindow (wc.lpszClassName,
  193. messageWindowName,
  194. 0, 0, 0, 0, 0, 0, 0,
  195. hmod, 0);
  196. }
  197. void MessageManager::doPlatformSpecificShutdown()
  198. {
  199. DestroyWindow (juce_messageWindowHandle);
  200. UnregisterClass (getMessageWindowClassName(), 0);
  201. OleUninitialize();
  202. }
  203. END_JUCE_NAMESPACE