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

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  18. // and your header search path must make it accessible to the module's files.
  19. #include "AppConfig.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. //==============================================================================
  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