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.

163 lines
5.1KB

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