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.

170 lines
5.3KB

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