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.

165 lines
5.3KB

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