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.

245 lines
8.8KB

  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. return (*callback) (userData);
  120. else
  121. return (void*) SendMessage (juce_messageWindowHandle,
  122. specialCallbackId,
  123. (WPARAM) callback,
  124. (LPARAM) userData);
  125. }
  126. //==============================================================================
  127. static BOOL CALLBACK BroadcastEnumWindowProc (HWND hwnd, LPARAM lParam)
  128. {
  129. if (hwnd != juce_messageWindowHandle)
  130. (reinterpret_cast <VoidArray*> (lParam))->add ((void*) hwnd);
  131. return TRUE;
  132. }
  133. void MessageManager::broadcastMessage (const String& value) throw()
  134. {
  135. VoidArray windows;
  136. EnumWindows (&BroadcastEnumWindowProc, (LPARAM) &windows);
  137. const String localCopy (value);
  138. COPYDATASTRUCT data;
  139. data.dwData = broadcastId;
  140. data.cbData = (localCopy.length() + 1) * sizeof (juce_wchar);
  141. data.lpData = (void*) (const juce_wchar*) localCopy;
  142. for (int i = windows.size(); --i >= 0;)
  143. {
  144. HWND hwnd = (HWND) windows.getUnchecked(i);
  145. TCHAR windowName [64]; // no need to read longer strings than this
  146. GetWindowText (hwnd, windowName, 64);
  147. windowName [63] = 0;
  148. if (String (windowName) == String (messageWindowName))
  149. {
  150. DWORD_PTR result;
  151. SendMessageTimeout (hwnd, WM_COPYDATA,
  152. (WPARAM) juce_messageWindowHandle,
  153. (LPARAM) &data,
  154. SMTO_BLOCK | SMTO_ABORTIFHUNG,
  155. 8000,
  156. &result);
  157. }
  158. }
  159. }
  160. //==============================================================================
  161. static const String getMessageWindowClassName()
  162. {
  163. // this name has to be different for each app/dll instance because otherwise
  164. // poor old Win32 can get a bit confused (even despite it not being a process-global
  165. // window class).
  166. static int number = 0;
  167. if (number == 0)
  168. number = 0x7fffffff & (int) Time::getHighResolutionTicks();
  169. return T("JUCEcs_") + String (number);
  170. }
  171. void MessageManager::doPlatformSpecificInitialisation()
  172. {
  173. OleInitialize (0);
  174. const String className (getMessageWindowClassName());
  175. HMODULE hmod = (HMODULE) PlatformUtilities::getCurrentModuleInstanceHandle();
  176. WNDCLASSEX wc;
  177. zerostruct (wc);
  178. wc.cbSize = sizeof (wc);
  179. wc.lpfnWndProc = (WNDPROC) juce_MessageWndProc;
  180. wc.cbWndExtra = 4;
  181. wc.hInstance = hmod;
  182. wc.lpszClassName = className;
  183. RegisterClassEx (&wc);
  184. juce_messageWindowHandle = CreateWindow (wc.lpszClassName,
  185. messageWindowName,
  186. 0, 0, 0, 0, 0, 0, 0,
  187. hmod, 0);
  188. }
  189. void MessageManager::doPlatformSpecificShutdown()
  190. {
  191. DestroyWindow (juce_messageWindowHandle);
  192. UnregisterClass (getMessageWindowClassName(), 0);
  193. OleUninitialize();
  194. }
  195. END_JUCE_NAMESPACE