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