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.

161 lines
5.1KB

  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. NSWindow* hostWindow = [[NSWindow alloc] initWithWindowRef: hostWindowRef];
  41. [hostWindow retain];
  42. [hostWindow setCanHide: YES];
  43. [hostWindow setReleasedWhenClosed: YES];
  44. NSRect oldWindowFrame = [hostWindow frame];
  45. NSView* content = [hostWindow contentView];
  46. NSRect f = [content frame];
  47. f.size.width = comp->getWidth();
  48. f.size.height = comp->getHeight();
  49. [content setFrame: f];
  50. NSRect hostWindowScreenFrame = [[hostWindow screen] frame];
  51. const int mainScreenHeight = [[[NSScreen screens] objectAtIndex: 0] frame].size.height;
  52. #if WINDOWPOSITION_BODGE
  53. {
  54. Rect winBounds;
  55. GetWindowBounds ((WindowRef) hostWindowRef, kWindowContentRgn, &winBounds);
  56. NSRect w = [hostWindow frame];
  57. w.origin.x = winBounds.left;
  58. w.origin.y = mainScreenHeight - winBounds.bottom;
  59. [hostWindow setFrame: w display: NO animate: NO];
  60. }
  61. #endif
  62. NSPoint windowPos = [hostWindow convertBaseToScreen: f.origin];
  63. windowPos.x = windowPos.x + jmax (0.0f, (oldWindowFrame.size.width - f.size.width) / 2.0f);
  64. windowPos.y = mainScreenHeight - (windowPos.y + f.size.height);
  65. comp->setTopLeftPosition ((int) windowPos.x, (int) windowPos.y);
  66. #if ! JucePlugin_EditorRequiresKeyboardFocus
  67. comp->addToDesktop (ComponentPeer::windowIsTemporary | ComponentPeer::windowIgnoresKeyPresses);
  68. #else
  69. comp->addToDesktop (ComponentPeer::windowIsTemporary);
  70. #endif
  71. comp->setVisible (true);
  72. NSView* pluginView = (NSView*) comp->getWindowHandle();
  73. NSWindow* pluginWindow = [pluginView window];
  74. [hostWindow addChildWindow: pluginWindow
  75. ordered: NSWindowAbove];
  76. [hostWindow orderFront: nil];
  77. [pluginWindow orderFront: nil];
  78. attachWindowHidingHooks (comp, (WindowRef) hostWindowRef, hostWindow);
  79. return hostWindow;
  80. }
  81. void removeSubWindow (void* nsWindow, Component* comp)
  82. {
  83. JUCE_AUTORELEASEPOOL
  84. NSView* pluginView = (NSView*) comp->getWindowHandle();
  85. NSWindow* hostWindow = (NSWindow*) nsWindow;
  86. NSWindow* pluginWindow = [pluginView window];
  87. removeWindowHidingHooks (comp);
  88. [hostWindow removeChildWindow: pluginWindow];
  89. comp->removeFromDesktop();
  90. [hostWindow release];
  91. }
  92. namespace
  93. {
  94. bool isJuceWindow (WindowRef w)
  95. {
  96. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  97. {
  98. ComponentPeer* peer = ComponentPeer::getPeer(i);
  99. NSView* view = (NSView*) peer->getNativeHandle();
  100. if ([[view window] windowRef] == w)
  101. return true;
  102. }
  103. return false;
  104. }
  105. }
  106. void forwardCurrentKeyEventToHostWindow()
  107. {
  108. WindowRef w = FrontNonFloatingWindow();
  109. WindowRef original = w;
  110. while (IsValidWindowPtr (w) && isJuceWindow (w))
  111. {
  112. w = GetNextWindowOfClass (w, kDocumentWindowClass, true);
  113. if (w == original)
  114. break;
  115. }
  116. if (! isJuceWindow (w))
  117. {
  118. ActivateWindow (w, true);
  119. [NSApp postEvent: [NSApp currentEvent] atStart: YES];
  120. }
  121. }
  122. #endif