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.

176 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #if JucePlugin_Build_RTAS
  20. #include "../../juce_core/system/juce_TargetPlatform.h"
  21. #include "../utility/juce_CheckSettingMacros.h"
  22. // Horrible carbon-based fix for a cocoa bug, where an NSWindow that wraps a carbon
  23. // window fails to keep its position updated when the user drags the window around..
  24. #define WINDOWPOSITION_BODGE 1
  25. #define JUCE_MAC_WINDOW_VISIBITY_BODGE 1
  26. #include "../utility/juce_IncludeSystemHeaders.h"
  27. #include "../utility/juce_IncludeModuleHeaders.h"
  28. #include "../utility/juce_CarbonVisibility.h"
  29. namespace juce
  30. {
  31. using namespace juce;
  32. //==============================================================================
  33. void initialiseMacRTAS();
  34. void initialiseMacRTAS()
  35. {
  36. #if ! JUCE_64BIT
  37. NSApplicationLoad();
  38. #endif
  39. }
  40. void* attachSubWindow (void*, Component*);
  41. void* attachSubWindow (void* hostWindowRef, Component* comp)
  42. {
  43. JUCE_AUTORELEASEPOOL
  44. {
  45. #if 0
  46. // This was suggested as a way to improve passing keypresses to the host, but
  47. // a side-effect seems to be occasional rendering artifacts.
  48. HIWindowChangeClass ((WindowRef) hostWindowRef, kFloatingWindowClass);
  49. #endif
  50. NSWindow* hostWindow = [[NSWindow alloc] initWithWindowRef: hostWindowRef];
  51. [hostWindow retain];
  52. [hostWindow setCanHide: YES];
  53. [hostWindow setReleasedWhenClosed: YES];
  54. NSRect oldWindowFrame = [hostWindow frame];
  55. NSView* content = [hostWindow contentView];
  56. NSRect f = [content frame];
  57. f.size.width = comp->getWidth();
  58. f.size.height = comp->getHeight();
  59. [content setFrame: f];
  60. const CGFloat mainScreenHeight = [[[NSScreen screens] objectAtIndex: 0] frame].size.height;
  61. #if WINDOWPOSITION_BODGE
  62. {
  63. Rect winBounds;
  64. GetWindowBounds ((WindowRef) hostWindowRef, kWindowContentRgn, &winBounds);
  65. NSRect w = [hostWindow frame];
  66. w.origin.x = winBounds.left;
  67. w.origin.y = mainScreenHeight - winBounds.bottom;
  68. [hostWindow setFrame: w display: NO animate: NO];
  69. }
  70. #endif
  71. NSPoint windowPos = [hostWindow convertBaseToScreen: f.origin];
  72. windowPos.x = windowPos.x + jmax (0.0f, (oldWindowFrame.size.width - f.size.width) / 2.0f);
  73. windowPos.y = mainScreenHeight - (windowPos.y + f.size.height);
  74. comp->setTopLeftPosition ((int) windowPos.x, (int) windowPos.y);
  75. #if ! JucePlugin_EditorRequiresKeyboardFocus
  76. comp->addToDesktop (ComponentPeer::windowIsTemporary | ComponentPeer::windowIgnoresKeyPresses);
  77. #else
  78. comp->addToDesktop (ComponentPeer::windowIsTemporary);
  79. #endif
  80. comp->setVisible (true);
  81. NSView* pluginView = (NSView*) comp->getWindowHandle();
  82. NSWindow* pluginWindow = [pluginView window];
  83. [hostWindow addChildWindow: pluginWindow
  84. ordered: NSWindowAbove];
  85. [hostWindow orderFront: nil];
  86. [pluginWindow orderFront: nil];
  87. attachWindowHidingHooks (comp, (WindowRef) hostWindowRef, hostWindow);
  88. return hostWindow;
  89. }
  90. }
  91. void removeSubWindow (void*, Component*);
  92. void removeSubWindow (void* nsWindow, Component* comp)
  93. {
  94. JUCE_AUTORELEASEPOOL
  95. {
  96. NSView* pluginView = (NSView*) comp->getWindowHandle();
  97. NSWindow* hostWindow = (NSWindow*) nsWindow;
  98. NSWindow* pluginWindow = [pluginView window];
  99. removeWindowHidingHooks (comp);
  100. [hostWindow removeChildWindow: pluginWindow];
  101. comp->removeFromDesktop();
  102. [hostWindow release];
  103. }
  104. }
  105. namespace
  106. {
  107. bool isJuceWindow (WindowRef w)
  108. {
  109. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  110. {
  111. ComponentPeer* peer = ComponentPeer::getPeer(i);
  112. NSView* view = (NSView*) peer->getNativeHandle();
  113. if ([[view window] windowRef] == w)
  114. return true;
  115. }
  116. return false;
  117. }
  118. }
  119. void forwardCurrentKeyEventToHostWindow();
  120. void forwardCurrentKeyEventToHostWindow()
  121. {
  122. WindowRef w = FrontNonFloatingWindow();
  123. WindowRef original = w;
  124. while (IsValidWindowPtr (w) && isJuceWindow (w))
  125. {
  126. w = GetNextWindowOfClass (w, kDocumentWindowClass, true);
  127. if (w == original)
  128. break;
  129. }
  130. if (! isJuceWindow (w))
  131. {
  132. ActivateWindow (w, true);
  133. repostCurrentNSEvent();
  134. }
  135. }
  136. } // namespace juce
  137. #endif