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.

270 lines
10KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  18. // and your header search path must make it accessible to the module's files.
  19. #include "AppConfig.h"
  20. #include "../utility/juce_CheckSettingMacros.h"
  21. #if JucePlugin_Build_VST3
  22. #define JUCE_MAC_WINDOW_VISIBITY_BODGE 1
  23. #include "../utility/juce_IncludeSystemHeaders.h"
  24. #include "../utility/juce_IncludeModuleHeaders.h"
  25. #include "../utility/juce_FakeMouseMoveGenerator.h"
  26. #include "../utility/juce_CarbonVisibility.h"
  27. #undef Component
  28. #undef Point
  29. //==============================================================================
  30. namespace juce
  31. {
  32. static void initialiseMac()
  33. {
  34. #if ! JUCE_64BIT
  35. NSApplicationLoad();
  36. #endif
  37. }
  38. #if ! JUCE_64BIT
  39. static void updateComponentPos (Component* const comp)
  40. {
  41. DBG ("updateComponentPos()");
  42. HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  43. comp->getProperties() ["dummyViewRef"].toString().getHexValue64();
  44. HIRect r;
  45. HIViewGetFrame (dummyView, &r);
  46. HIViewRef root;
  47. HIViewFindByID (HIViewGetRoot (HIViewGetWindow (dummyView)), kHIViewWindowContentID, &root);
  48. HIViewConvertRect (&r, HIViewGetSuperview (dummyView), root);
  49. Rect windowPos;
  50. GetWindowBounds (HIViewGetWindow (dummyView), kWindowContentRgn, &windowPos);
  51. comp->setTopLeftPosition ((int) (windowPos.left + r.origin.x),
  52. (int) (windowPos.top + r.origin.y));
  53. }
  54. static pascal OSStatus viewBoundsChangedEvent (EventHandlerCallRef, EventRef, void* user)
  55. {
  56. updateComponentPos ((Component*) user);
  57. return noErr;
  58. }
  59. #endif
  60. static void* attachComponentToWindowRef (Component* comp, void* windowRef, bool isHIView)
  61. {
  62. DBG ("attachComponentToWindowRef()");
  63. JUCE_AUTORELEASEPOOL
  64. {
  65. #if JUCE_64BIT
  66. NSView* parentView = (NSView*) windowRef;
  67. #if JucePlugin_EditorRequiresKeyboardFocus
  68. comp->addToDesktop (0, parentView);
  69. #else
  70. comp->addToDesktop (ComponentPeer::windowIgnoresKeyPresses, parentView);
  71. #endif
  72. // (this workaround is because Wavelab provides a zero-size parent view..)
  73. if ([parentView frame].size.height == 0)
  74. [((NSView*) comp->getWindowHandle()) setFrameOrigin: NSZeroPoint];
  75. comp->setVisible (true);
  76. comp->toFront (false);
  77. [[parentView window] setAcceptsMouseMovedEvents: YES];
  78. return parentView;
  79. #else
  80. //treat NSView like 64bit
  81. if (! isHIView)
  82. {
  83. NSView* parentView = (NSView*) windowRef;
  84. #if JucePlugin_EditorRequiresKeyboardFocus
  85. comp->addToDesktop (0, parentView);
  86. #else
  87. comp->addToDesktop (ComponentPeer::windowIgnoresKeyPresses, parentView);
  88. #endif
  89. // (this workaround is because Wavelab provides a zero-size parent view..)
  90. if ([parentView frame].size.height == 0)
  91. [((NSView*) comp->getWindowHandle()) setFrameOrigin: NSZeroPoint];
  92. comp->setVisible (true);
  93. comp->toFront (false);
  94. [[parentView window] setAcceptsMouseMovedEvents: YES];
  95. return parentView;
  96. }
  97. NSWindow* hostWindow = [[NSWindow alloc] initWithWindowRef: windowRef];
  98. [hostWindow retain];
  99. [hostWindow setCanHide: YES];
  100. [hostWindow setReleasedWhenClosed: YES];
  101. HIViewRef parentView = nullptr;
  102. WindowAttributes attributes;
  103. GetWindowAttributes ((WindowRef) windowRef, &attributes);
  104. if ((attributes & kWindowCompositingAttribute) != 0)
  105. {
  106. HIViewRef root = HIViewGetRoot ((WindowRef) windowRef);
  107. HIViewFindByID (root, kHIViewWindowContentID, &parentView);
  108. if (parentView == nullptr)
  109. parentView = root;
  110. }
  111. else
  112. {
  113. GetRootControl ((WindowRef) windowRef, (ControlRef*) &parentView);
  114. if (parentView == nullptr)
  115. CreateRootControl ((WindowRef) windowRef, (ControlRef*) &parentView);
  116. }
  117. // It seems that the only way to successfully position our overlaid window is by putting a dummy
  118. // HIView into the host's carbon window, and then catching events to see when it gets repositioned
  119. HIViewRef dummyView = 0;
  120. HIImageViewCreate (0, &dummyView);
  121. HIRect r = { {0, 0}, { (float) comp->getWidth(), (float) comp->getHeight()} };
  122. HIViewSetFrame (dummyView, &r);
  123. HIViewAddSubview (parentView, dummyView);
  124. comp->getProperties().set ("dummyViewRef", String::toHexString ((pointer_sized_int) (void*) dummyView));
  125. EventHandlerRef ref;
  126. const EventTypeSpec kControlBoundsChangedEvent = { kEventClassControl, kEventControlBoundsChanged };
  127. InstallEventHandler (GetControlEventTarget (dummyView), NewEventHandlerUPP (viewBoundsChangedEvent), 1, &kControlBoundsChangedEvent, (void*) comp, &ref);
  128. comp->getProperties().set ("boundsEventRef", String::toHexString ((pointer_sized_int) (void*) ref));
  129. updateComponentPos (comp);
  130. #if ! JucePlugin_EditorRequiresKeyboardFocus
  131. comp->addToDesktop (ComponentPeer::windowIsTemporary | ComponentPeer::windowIgnoresKeyPresses);
  132. #else
  133. comp->addToDesktop (ComponentPeer::windowIsTemporary);
  134. #endif
  135. comp->setVisible (true);
  136. comp->toFront (false);
  137. NSView* pluginView = (NSView*) comp->getWindowHandle();
  138. NSWindow* pluginWindow = [pluginView window];
  139. [pluginWindow setExcludedFromWindowsMenu: YES];
  140. [pluginWindow setCanHide: YES];
  141. [hostWindow addChildWindow: pluginWindow
  142. ordered: NSWindowAbove];
  143. [hostWindow orderFront: nil];
  144. [pluginWindow orderFront: nil];
  145. attachWindowHidingHooks (comp, (WindowRef) windowRef, hostWindow);
  146. return hostWindow;
  147. #endif
  148. }
  149. }
  150. static void detachComponentFromWindowRef (Component* comp, void* nsWindow, bool isHIView)
  151. {
  152. #if JUCE_64BIT
  153. comp->removeFromDesktop();
  154. #else
  155. //treat NSView like 64bit
  156. if (! isHIView)
  157. {
  158. comp->removeFromDesktop();
  159. }
  160. else
  161. {
  162. JUCE_AUTORELEASEPOOL
  163. {
  164. EventHandlerRef ref = (EventHandlerRef) (void*) (pointer_sized_int)
  165. comp->getProperties() ["boundsEventRef"].toString().getHexValue64();
  166. RemoveEventHandler (ref);
  167. removeWindowHidingHooks (comp);
  168. HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  169. comp->getProperties() ["dummyViewRef"].toString().getHexValue64();
  170. if (HIViewIsValid (dummyView))
  171. CFRelease (dummyView);
  172. NSWindow* hostWindow = (NSWindow*) nsWindow;
  173. NSView* pluginView = (NSView*) comp->getWindowHandle();
  174. NSWindow* pluginWindow = [pluginView window];
  175. [hostWindow removeChildWindow: pluginWindow];
  176. comp->removeFromDesktop();
  177. [hostWindow release];
  178. }
  179. // The event loop needs to be run between closing the window and deleting the plugin,
  180. // presumably to let the cocoa objects get tidied up. Leaving out this line causes crashes
  181. // in Live and Reaper when you delete the plugin with its window open.
  182. // (Doing it this way rather than using a single longer timout means that we can guarantee
  183. // how many messages will be dispatched, which seems to be vital in Reaper)
  184. for (int i = 20; --i >= 0;)
  185. MessageManager::getInstance()->runDispatchLoopUntil (1);
  186. }
  187. #endif
  188. }
  189. static void setNativeHostWindowSize (void* nsWindow, Component* component, int newWidth, int newHeight, bool isHIView)
  190. {
  191. JUCE_AUTORELEASEPOOL
  192. {
  193. #if JUCE_64BIT
  194. component->setSize (newWidth, newHeight);
  195. #else
  196. if (! isHIView)
  197. { //Treat NSView like 64bit:
  198. component->setSize (newWidth, newHeight);
  199. }
  200. else if (HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  201. component->getProperties() ["dummyViewRef"].toString().getHexValue64())
  202. {
  203. HIRect frameRect;
  204. HIViewGetFrame (dummyView, &frameRect);
  205. frameRect.size.width = newWidth;
  206. frameRect.size.height = newHeight;
  207. HIViewSetFrame (dummyView, &frameRect);
  208. }
  209. #endif
  210. }
  211. }
  212. } // (juce namespace)
  213. #endif