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.

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