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.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  19. // and your header search path must make it accessible to the module's files.
  20. #include "AppConfig.h"
  21. #include "../utility/juce_CheckSettingMacros.h"
  22. #if JucePlugin_Build_VST
  23. #define JUCE_MAC_WINDOW_VISIBITY_BODGE 1
  24. #include "../utility/juce_IncludeSystemHeaders.h"
  25. #include "../utility/juce_IncludeModuleHeaders.h"
  26. #include "../utility/juce_FakeMouseMoveGenerator.h"
  27. #include "../utility/juce_CarbonVisibility.h"
  28. #include "../utility/juce_PluginHostType.h"
  29. //==============================================================================
  30. namespace juce
  31. {
  32. #if ! JUCE_64BIT
  33. static void updateComponentPos (Component* const comp)
  34. {
  35. HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  36. comp->getProperties() ["dummyViewRef"].toString().getHexValue64();
  37. HIRect r;
  38. HIViewGetFrame (dummyView, &r);
  39. HIViewRef root;
  40. HIViewFindByID (HIViewGetRoot (HIViewGetWindow (dummyView)), kHIViewWindowContentID, &root);
  41. HIViewConvertRect (&r, HIViewGetSuperview (dummyView), root);
  42. Rect windowPos;
  43. GetWindowBounds (HIViewGetWindow (dummyView), kWindowContentRgn, &windowPos);
  44. comp->setTopLeftPosition ((int) (windowPos.left + r.origin.x),
  45. (int) (windowPos.top + r.origin.y));
  46. }
  47. static pascal OSStatus viewBoundsChangedEvent (EventHandlerCallRef, EventRef, void* user)
  48. {
  49. updateComponentPos ((Component*) user);
  50. return noErr;
  51. }
  52. #endif
  53. //==============================================================================
  54. void initialiseMac();
  55. void initialiseMac()
  56. {
  57. #if ! JUCE_64BIT
  58. NSApplicationLoad();
  59. #endif
  60. }
  61. void* attachComponentToWindowRef (Component* comp, void* windowRef);
  62. void* attachComponentToWindowRef (Component* comp, void* windowRef)
  63. {
  64. JUCE_AUTORELEASEPOOL
  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. updateComponentPos (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. void detachComponentFromWindowRef (Component* comp, void* nsWindow);
  133. void detachComponentFromWindowRef (Component* comp, void* nsWindow)
  134. {
  135. #if JUCE_64BIT
  136. comp->removeFromDesktop();
  137. #else
  138. {
  139. JUCE_AUTORELEASEPOOL
  140. EventHandlerRef ref = (EventHandlerRef) (void*) (pointer_sized_int)
  141. comp->getProperties() ["boundsEventRef"].toString().getHexValue64();
  142. RemoveEventHandler (ref);
  143. removeWindowHidingHooks (comp);
  144. HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  145. comp->getProperties() ["dummyViewRef"].toString().getHexValue64();
  146. if (HIViewIsValid (dummyView))
  147. CFRelease (dummyView);
  148. NSWindow* hostWindow = (NSWindow*) nsWindow;
  149. NSView* pluginView = (NSView*) comp->getWindowHandle();
  150. NSWindow* pluginWindow = [pluginView window];
  151. [hostWindow removeChildWindow: pluginWindow];
  152. comp->removeFromDesktop();
  153. [hostWindow release];
  154. }
  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. void setNativeHostWindowSize (void* nsWindow, Component* component, int newWidth, int newHeight, const PluginHostType& host);
  165. void setNativeHostWindowSize (void* nsWindow, Component* component, int newWidth, int newHeight, const PluginHostType& host)
  166. {
  167. JUCE_AUTORELEASEPOOL
  168. #if JUCE_64BIT
  169. NSView* hostView = (NSView*) nsWindow;
  170. if (hostView != nil)
  171. {
  172. // xxx is this necessary, or do the hosts detect a change in the child view and do this automatically?
  173. [hostView setFrameSize: NSMakeSize ([hostView frame].size.width + (newWidth - component->getWidth()),
  174. [hostView frame].size.height + (newHeight - component->getHeight()))];
  175. }
  176. #else
  177. HIViewRef dummyView = (HIViewRef) (void*) (pointer_sized_int)
  178. component->getProperties() ["dummyViewRef"].toString().getHexValue64();
  179. if (dummyView != 0)
  180. {
  181. HIRect frameRect;
  182. HIViewGetFrame (dummyView, &frameRect);
  183. frameRect.size.width = newWidth;
  184. frameRect.size.height = newHeight;
  185. HIViewSetFrame (dummyView, &frameRect);
  186. }
  187. #endif
  188. }
  189. void checkWindowVisibility (void* nsWindow, Component* comp);
  190. void checkWindowVisibility (void* nsWindow, Component* comp)
  191. {
  192. #if ! JUCE_64BIT
  193. comp->setVisible ([((NSWindow*) nsWindow) isVisible]);
  194. #endif
  195. }
  196. bool forwardCurrentKeyEventToHost (Component* comp);
  197. bool forwardCurrentKeyEventToHost (Component* comp)
  198. {
  199. #if JUCE_64BIT
  200. return false;
  201. #else
  202. NSWindow* win = [(NSView*) comp->getWindowHandle() window];
  203. [[win parentWindow] makeKeyWindow];
  204. [NSApp postEvent: [NSApp currentEvent] atStart: YES];
  205. return true;
  206. #endif
  207. }
  208. } // (juce namespace)
  209. #endif