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.

167 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  19. // and your header search path must make it accessible to the module's files.
  20. #include "AppConfig.h"
  21. #include "../utility/juce_CheckSettingMacros.h"
  22. #if JucePlugin_Build_RTAS
  23. // Horrible carbon-based fix for a cocoa bug, where an NSWindow that wraps a carbon
  24. // window fails to keep its position updated when the user drags the window around..
  25. #define WINDOWPOSITION_BODGE 1
  26. #define JUCE_MAC_WINDOW_VISIBITY_BODGE 1
  27. #include "../utility/juce_IncludeSystemHeaders.h"
  28. #include "../utility/juce_IncludeModuleHeaders.h"
  29. #include "../utility/juce_CarbonVisibility.h"
  30. //==============================================================================
  31. void initialiseMacRTAS()
  32. {
  33. #if ! JUCE_64BIT
  34. NSApplicationLoad();
  35. #endif
  36. }
  37. void* attachSubWindow (void* hostWindowRef, Component* comp)
  38. {
  39. JUCE_AUTORELEASEPOOL
  40. {
  41. #if 0
  42. // This was suggested as a way to improve passing keypresses to the host, but
  43. // a side-effect seems to be occasional rendering artifacts.
  44. HIWindowChangeClass ((WindowRef) hostWindowRef, kFloatingWindowClass);
  45. #endif
  46. NSWindow* hostWindow = [[NSWindow alloc] initWithWindowRef: hostWindowRef];
  47. [hostWindow retain];
  48. [hostWindow setCanHide: YES];
  49. [hostWindow setReleasedWhenClosed: YES];
  50. NSRect oldWindowFrame = [hostWindow frame];
  51. NSView* content = [hostWindow contentView];
  52. NSRect f = [content frame];
  53. f.size.width = comp->getWidth();
  54. f.size.height = comp->getHeight();
  55. [content setFrame: f];
  56. const int mainScreenHeight = [[[NSScreen screens] objectAtIndex: 0] frame].size.height;
  57. #if WINDOWPOSITION_BODGE
  58. {
  59. Rect winBounds;
  60. GetWindowBounds ((WindowRef) hostWindowRef, kWindowContentRgn, &winBounds);
  61. NSRect w = [hostWindow frame];
  62. w.origin.x = winBounds.left;
  63. w.origin.y = mainScreenHeight - winBounds.bottom;
  64. [hostWindow setFrame: w display: NO animate: NO];
  65. }
  66. #endif
  67. NSPoint windowPos = [hostWindow convertBaseToScreen: f.origin];
  68. windowPos.x = windowPos.x + jmax (0.0f, (oldWindowFrame.size.width - f.size.width) / 2.0f);
  69. windowPos.y = mainScreenHeight - (windowPos.y + f.size.height);
  70. comp->setTopLeftPosition ((int) windowPos.x, (int) windowPos.y);
  71. #if ! JucePlugin_EditorRequiresKeyboardFocus
  72. comp->addToDesktop (ComponentPeer::windowIsTemporary | ComponentPeer::windowIgnoresKeyPresses);
  73. #else
  74. comp->addToDesktop (ComponentPeer::windowIsTemporary);
  75. #endif
  76. comp->setVisible (true);
  77. NSView* pluginView = (NSView*) comp->getWindowHandle();
  78. NSWindow* pluginWindow = [pluginView window];
  79. [hostWindow addChildWindow: pluginWindow
  80. ordered: NSWindowAbove];
  81. [hostWindow orderFront: nil];
  82. [pluginWindow orderFront: nil];
  83. attachWindowHidingHooks (comp, (WindowRef) hostWindowRef, hostWindow);
  84. return hostWindow;
  85. }
  86. }
  87. void removeSubWindow (void* nsWindow, Component* comp)
  88. {
  89. JUCE_AUTORELEASEPOOL
  90. {
  91. NSView* pluginView = (NSView*) comp->getWindowHandle();
  92. NSWindow* hostWindow = (NSWindow*) nsWindow;
  93. NSWindow* pluginWindow = [pluginView window];
  94. removeWindowHidingHooks (comp);
  95. [hostWindow removeChildWindow: pluginWindow];
  96. comp->removeFromDesktop();
  97. [hostWindow release];
  98. }
  99. }
  100. namespace
  101. {
  102. bool isJuceWindow (WindowRef w)
  103. {
  104. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  105. {
  106. ComponentPeer* peer = ComponentPeer::getPeer(i);
  107. NSView* view = (NSView*) peer->getNativeHandle();
  108. if ([[view window] windowRef] == w)
  109. return true;
  110. }
  111. return false;
  112. }
  113. }
  114. void forwardCurrentKeyEventToHostWindow()
  115. {
  116. WindowRef w = FrontNonFloatingWindow();
  117. WindowRef original = w;
  118. while (IsValidWindowPtr (w) && isJuceWindow (w))
  119. {
  120. w = GetNextWindowOfClass (w, kDocumentWindowClass, true);
  121. if (w == original)
  122. break;
  123. }
  124. if (! isJuceWindow (w))
  125. {
  126. ActivateWindow (w, true);
  127. [NSApp postEvent: [NSApp currentEvent] atStart: YES];
  128. }
  129. }
  130. #endif