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.

264 lines
9.6KB

  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_VST
  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. //==============================================================================
  28. namespace juce
  29. {
  30. #if ! JUCE_64BIT
  31. void updateEditorCompBounds (Component*);
  32. void updateEditorCompBounds (Component* comp)
  33. {
  34. HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  35. comp->getProperties() ["dummyViewRef"].toString().getHexValue64();
  36. HIRect r;
  37. HIViewGetFrame (dummyView, &r);
  38. HIViewRef root;
  39. HIViewFindByID (HIViewGetRoot (HIViewGetWindow (dummyView)), kHIViewWindowContentID, &root);
  40. HIViewConvertRect (&r, HIViewGetSuperview (dummyView), root);
  41. Rect windowPos;
  42. GetWindowBounds (HIViewGetWindow (dummyView), kWindowContentRgn, &windowPos);
  43. comp->setTopLeftPosition ((int) (windowPos.left + r.origin.x),
  44. (int) (windowPos.top + r.origin.y));
  45. }
  46. static pascal OSStatus viewBoundsChangedEvent (EventHandlerCallRef, EventRef, void* user)
  47. {
  48. updateEditorCompBounds ((Component*) user);
  49. return noErr;
  50. }
  51. #endif
  52. //==============================================================================
  53. void initialiseMac();
  54. void initialiseMac()
  55. {
  56. #if ! JUCE_64BIT
  57. NSApplicationLoad();
  58. #endif
  59. }
  60. void* attachComponentToWindowRef (Component* comp, void* windowRef);
  61. void* attachComponentToWindowRef (Component* comp, void* windowRef)
  62. {
  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. NSWindow* hostWindow = [[NSWindow alloc] initWithWindowRef: windowRef];
  81. [hostWindow retain];
  82. [hostWindow setCanHide: YES];
  83. [hostWindow setReleasedWhenClosed: YES];
  84. HIViewRef parentView = 0;
  85. WindowAttributes attributes;
  86. GetWindowAttributes ((WindowRef) windowRef, &attributes);
  87. if ((attributes & kWindowCompositingAttribute) != 0)
  88. {
  89. HIViewRef root = HIViewGetRoot ((WindowRef) windowRef);
  90. HIViewFindByID (root, kHIViewWindowContentID, &parentView);
  91. if (parentView == 0)
  92. parentView = root;
  93. }
  94. else
  95. {
  96. GetRootControl ((WindowRef) windowRef, (ControlRef*) &parentView);
  97. if (parentView == 0)
  98. CreateRootControl ((WindowRef) windowRef, (ControlRef*) &parentView);
  99. }
  100. // It seems that the only way to successfully position our overlaid window is by putting a dummy
  101. // HIView into the host's carbon window, and then catching events to see when it gets repositioned
  102. HIViewRef dummyView = 0;
  103. HIImageViewCreate (0, &dummyView);
  104. HIRect r = { {0, 0}, { (float) comp->getWidth(), (float) comp->getHeight()} };
  105. HIViewSetFrame (dummyView, &r);
  106. HIViewAddSubview (parentView, dummyView);
  107. comp->getProperties().set ("dummyViewRef", String::toHexString ((pointer_sized_int) (void*) dummyView));
  108. EventHandlerRef ref;
  109. const EventTypeSpec kControlBoundsChangedEvent = { kEventClassControl, kEventControlBoundsChanged };
  110. InstallEventHandler (GetControlEventTarget (dummyView), NewEventHandlerUPP (viewBoundsChangedEvent), 1, &kControlBoundsChangedEvent, (void*) comp, &ref);
  111. comp->getProperties().set ("boundsEventRef", String::toHexString ((pointer_sized_int) (void*) ref));
  112. updateEditorCompBounds (comp);
  113. #if ! JucePlugin_EditorRequiresKeyboardFocus
  114. comp->addToDesktop (ComponentPeer::windowIsTemporary | ComponentPeer::windowIgnoresKeyPresses);
  115. #else
  116. comp->addToDesktop (ComponentPeer::windowIsTemporary);
  117. #endif
  118. comp->setVisible (true);
  119. comp->toFront (false);
  120. NSView* pluginView = (NSView*) comp->getWindowHandle();
  121. NSWindow* pluginWindow = [pluginView window];
  122. [pluginWindow setExcludedFromWindowsMenu: YES];
  123. [pluginWindow setCanHide: YES];
  124. [hostWindow addChildWindow: pluginWindow
  125. ordered: NSWindowAbove];
  126. [hostWindow orderFront: nil];
  127. [pluginWindow orderFront: nil];
  128. attachWindowHidingHooks (comp, (WindowRef) windowRef, hostWindow);
  129. return hostWindow;
  130. #endif
  131. }
  132. }
  133. void detachComponentFromWindowRef (Component* comp, void* nsWindow);
  134. void detachComponentFromWindowRef (Component* comp, void* nsWindow)
  135. {
  136. JUCE_AUTORELEASEPOOL
  137. {
  138. #if JUCE_64BIT
  139. comp->removeFromDesktop();
  140. #else
  141. EventHandlerRef ref = (EventHandlerRef) (void*) (pointer_sized_int)
  142. comp->getProperties() ["boundsEventRef"].toString().getHexValue64();
  143. RemoveEventHandler (ref);
  144. removeWindowHidingHooks (comp);
  145. HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  146. comp->getProperties() ["dummyViewRef"].toString().getHexValue64();
  147. if (HIViewIsValid (dummyView))
  148. CFRelease (dummyView);
  149. NSWindow* hostWindow = (NSWindow*) nsWindow;
  150. NSView* pluginView = (NSView*) comp->getWindowHandle();
  151. NSWindow* pluginWindow = [pluginView window];
  152. [hostWindow removeChildWindow: pluginWindow];
  153. comp->removeFromDesktop();
  154. [hostWindow release];
  155. // The event loop needs to be run between closing the window and deleting the plugin,
  156. // presumably to let the cocoa objects get tidied up. Leaving out this line causes crashes
  157. // in Live and Reaper when you delete the plugin with its window open.
  158. // (Doing it this way rather than using a single longer timout means that we can guarantee
  159. // how many messages will be dispatched, which seems to be vital in Reaper)
  160. for (int i = 20; --i >= 0;)
  161. MessageManager::getInstance()->runDispatchLoopUntil (1);
  162. #endif
  163. }
  164. }
  165. void setNativeHostWindowSize (void* nsWindow, Component* component, int newWidth, int newHeight);
  166. void setNativeHostWindowSize (void* nsWindow, Component* component, int newWidth, int newHeight)
  167. {
  168. JUCE_AUTORELEASEPOOL
  169. {
  170. #if JUCE_64BIT
  171. if (NSView* hostView = (NSView*) nsWindow)
  172. {
  173. // xxx is this necessary, or do the hosts detect a change in the child view and do this automatically?
  174. [hostView setFrameSize: NSMakeSize ([hostView frame].size.width + (newWidth - component->getWidth()),
  175. [hostView frame].size.height + (newHeight - component->getHeight()))];
  176. }
  177. #else
  178. (void) nsWindow;
  179. if (HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  180. component->getProperties() ["dummyViewRef"].toString().getHexValue64())
  181. {
  182. HIRect frameRect;
  183. HIViewGetFrame (dummyView, &frameRect);
  184. frameRect.size.width = newWidth;
  185. frameRect.size.height = newHeight;
  186. HIViewSetFrame (dummyView, &frameRect);
  187. }
  188. #endif
  189. }
  190. }
  191. void checkWindowVisibility (void* nsWindow, Component* comp);
  192. void checkWindowVisibility (void* nsWindow, Component* comp)
  193. {
  194. #if ! JUCE_64BIT
  195. comp->setVisible ([((NSWindow*) nsWindow) isVisible]);
  196. #endif
  197. }
  198. bool forwardCurrentKeyEventToHost (Component* comp);
  199. bool forwardCurrentKeyEventToHost (Component* comp)
  200. {
  201. #if JUCE_64BIT
  202. (void) comp;
  203. return false;
  204. #else
  205. NSWindow* win = [(NSView*) comp->getWindowHandle() window];
  206. [[win parentWindow] makeKeyWindow];
  207. repostCurrentNSEvent();
  208. return true;
  209. #endif
  210. }
  211. } // (juce namespace)
  212. #endif