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.

171 lines
5.3KB

  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. #include "../../juce_core/system/juce_TargetPlatform.h"
  20. #include "../utility/juce_CheckSettingMacros.h"
  21. #if JucePlugin_Build_RTAS
  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. using namespace juce;
  30. //==============================================================================
  31. void initialiseMacRTAS();
  32. void initialiseMacRTAS()
  33. {
  34. #if ! JUCE_64BIT
  35. NSApplicationLoad();
  36. #endif
  37. }
  38. void* attachSubWindow (void*, Component*);
  39. void* attachSubWindow (void* hostWindowRef, Component* comp)
  40. {
  41. JUCE_AUTORELEASEPOOL
  42. {
  43. #if 0
  44. // This was suggested as a way to improve passing keypresses to the host, but
  45. // a side-effect seems to be occasional rendering artifacts.
  46. HIWindowChangeClass ((WindowRef) hostWindowRef, kFloatingWindowClass);
  47. #endif
  48. NSWindow* hostWindow = [[NSWindow alloc] initWithWindowRef: hostWindowRef];
  49. [hostWindow retain];
  50. [hostWindow setCanHide: YES];
  51. [hostWindow setReleasedWhenClosed: YES];
  52. NSRect oldWindowFrame = [hostWindow frame];
  53. NSView* content = [hostWindow contentView];
  54. NSRect f = [content frame];
  55. f.size.width = comp->getWidth();
  56. f.size.height = comp->getHeight();
  57. [content setFrame: f];
  58. const CGFloat mainScreenHeight = [[[NSScreen screens] objectAtIndex: 0] frame].size.height;
  59. #if WINDOWPOSITION_BODGE
  60. {
  61. Rect winBounds;
  62. GetWindowBounds ((WindowRef) hostWindowRef, kWindowContentRgn, &winBounds);
  63. NSRect w = [hostWindow frame];
  64. w.origin.x = winBounds.left;
  65. w.origin.y = mainScreenHeight - winBounds.bottom;
  66. [hostWindow setFrame: w display: NO animate: NO];
  67. }
  68. #endif
  69. NSPoint windowPos = [hostWindow convertBaseToScreen: f.origin];
  70. windowPos.x = windowPos.x + jmax (0.0f, (oldWindowFrame.size.width - f.size.width) / 2.0f);
  71. windowPos.y = mainScreenHeight - (windowPos.y + f.size.height);
  72. comp->setTopLeftPosition ((int) windowPos.x, (int) windowPos.y);
  73. #if ! JucePlugin_EditorRequiresKeyboardFocus
  74. comp->addToDesktop (ComponentPeer::windowIsTemporary | ComponentPeer::windowIgnoresKeyPresses);
  75. #else
  76. comp->addToDesktop (ComponentPeer::windowIsTemporary);
  77. #endif
  78. comp->setVisible (true);
  79. NSView* pluginView = (NSView*) comp->getWindowHandle();
  80. NSWindow* pluginWindow = [pluginView window];
  81. [hostWindow addChildWindow: pluginWindow
  82. ordered: NSWindowAbove];
  83. [hostWindow orderFront: nil];
  84. [pluginWindow orderFront: nil];
  85. attachWindowHidingHooks (comp, (WindowRef) hostWindowRef, hostWindow);
  86. return hostWindow;
  87. }
  88. }
  89. void removeSubWindow (void*, Component*);
  90. void removeSubWindow (void* nsWindow, Component* comp)
  91. {
  92. JUCE_AUTORELEASEPOOL
  93. {
  94. NSView* pluginView = (NSView*) comp->getWindowHandle();
  95. NSWindow* hostWindow = (NSWindow*) nsWindow;
  96. NSWindow* pluginWindow = [pluginView window];
  97. removeWindowHidingHooks (comp);
  98. [hostWindow removeChildWindow: pluginWindow];
  99. comp->removeFromDesktop();
  100. [hostWindow release];
  101. }
  102. }
  103. namespace
  104. {
  105. bool isJuceWindow (WindowRef w)
  106. {
  107. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  108. {
  109. ComponentPeer* peer = ComponentPeer::getPeer(i);
  110. NSView* view = (NSView*) peer->getNativeHandle();
  111. if ([[view window] windowRef] == w)
  112. return true;
  113. }
  114. return false;
  115. }
  116. }
  117. void forwardCurrentKeyEventToHostWindow();
  118. void forwardCurrentKeyEventToHostWindow()
  119. {
  120. WindowRef w = FrontNonFloatingWindow();
  121. WindowRef original = w;
  122. while (IsValidWindowPtr (w) && isJuceWindow (w))
  123. {
  124. w = GetNextWindowOfClass (w, kDocumentWindowClass, true);
  125. if (w == original)
  126. break;
  127. }
  128. if (! isJuceWindow (w))
  129. {
  130. ActivateWindow (w, true);
  131. repostCurrentNSEvent();
  132. }
  133. }
  134. #endif