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.

265 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  24. // compiled on its own).
  25. #ifdef JUCE_INCLUDED_FILE
  26. //==============================================================================
  27. ScopedAutoReleasePool::ScopedAutoReleasePool()
  28. {
  29. pool = [[NSAutoreleasePool alloc] init];
  30. }
  31. ScopedAutoReleasePool::~ScopedAutoReleasePool()
  32. {
  33. [((NSAutoreleasePool*) pool) release];
  34. }
  35. //==============================================================================
  36. void PlatformUtilities::beep()
  37. {
  38. NSBeep();
  39. }
  40. //==============================================================================
  41. void PlatformUtilities::addItemToDock (const File& file)
  42. {
  43. // check that it's not already there...
  44. if (! juce_getOutputFromCommand ("defaults read com.apple.dock persistent-apps")
  45. .containsIgnoreCase (file.getFullPathName()))
  46. {
  47. juce_runSystemCommand ("defaults write com.apple.dock persistent-apps -array-add \"<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>"
  48. + file.getFullPathName() + "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>\"");
  49. juce_runSystemCommand ("osascript -e \"tell application \\\"Dock\\\" to quit\"");
  50. }
  51. }
  52. //==============================================================================
  53. #if ! JUCE_ONLY_BUILD_CORE_LIBRARY
  54. bool AlertWindow::showNativeDialogBox (const String& title,
  55. const String& bodyText,
  56. bool isOkCancel)
  57. {
  58. const ScopedAutoReleasePool pool;
  59. return NSRunAlertPanel (juceStringToNS (title),
  60. juceStringToNS (bodyText),
  61. @"Ok",
  62. isOkCancel ? @"Cancel" : nil,
  63. nil) == 0;
  64. }
  65. //==============================================================================
  66. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMoveFiles)
  67. {
  68. if (files.size() == 0)
  69. return false;
  70. Component* sourceComp = Component::getComponentUnderMouse();
  71. if (sourceComp == 0)
  72. {
  73. jassertfalse // this method must be called in response to a
  74. // component's mouseDrag event!
  75. return false;
  76. }
  77. const ScopedAutoReleasePool pool;
  78. NSView* view = (NSView*) sourceComp->getWindowHandle();
  79. if (view == 0)
  80. return false;
  81. NSPasteboard* pboard = [NSPasteboard pasteboardWithName: NSDragPboard];
  82. [pboard declareTypes: [NSArray arrayWithObject: NSFilenamesPboardType]
  83. owner: nil];
  84. NSMutableArray* filesArray = [NSMutableArray arrayWithCapacity: 4];
  85. for (int i = 0; i < files.size(); ++i)
  86. [filesArray addObject: juceStringToNS (files[i])];
  87. [pboard setPropertyList: filesArray
  88. forType: NSFilenamesPboardType];
  89. NSPoint dragPosition = [view convertPoint: [[[view window] currentEvent] locationInWindow]
  90. fromView: nil];
  91. dragPosition.x -= 16;
  92. dragPosition.y -= 16;
  93. [view dragImage: [[NSWorkspace sharedWorkspace] iconForFile: juceStringToNS (files[0])]
  94. at: dragPosition
  95. offset: NSMakeSize (0, 0)
  96. event: [[view window] currentEvent]
  97. pasteboard: pboard
  98. source: view
  99. slideBack: YES];
  100. return true;
  101. }
  102. bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
  103. {
  104. jassertfalse // not implemented!
  105. return false;
  106. }
  107. //==============================================================================
  108. bool Desktop::canUseSemiTransparentWindows() throw()
  109. {
  110. return true;
  111. }
  112. void Desktop::getMousePosition (int& x, int& y) throw()
  113. {
  114. const ScopedAutoReleasePool pool;
  115. const NSPoint p ([NSEvent mouseLocation]);
  116. x = roundFloatToInt (p.x);
  117. y = roundFloatToInt ([[[NSScreen screens] objectAtIndex: 0] frame].size.height - p.y);
  118. }
  119. void Desktop::setMousePosition (int x, int y) throw()
  120. {
  121. // this rubbish needs to be done around the warp call, to avoid causing a
  122. // bizarre glitch..
  123. CGAssociateMouseAndMouseCursorPosition (false);
  124. CGSetLocalEventsSuppressionInterval (0);
  125. CGPoint pos = { x, y };
  126. CGWarpMouseCursorPosition (pos);
  127. CGAssociateMouseAndMouseCursorPosition (true);
  128. }
  129. //==============================================================================
  130. #if MACOS_10_4_OR_EARLIER
  131. class ScreenSaverDefeater : public Timer,
  132. public DeletedAtShutdown
  133. {
  134. public:
  135. ScreenSaverDefeater() throw()
  136. {
  137. startTimer (10000);
  138. timerCallback();
  139. }
  140. ~ScreenSaverDefeater() {}
  141. void timerCallback()
  142. {
  143. if (Process::isForegroundProcess())
  144. UpdateSystemActivity (UsrActivity);
  145. }
  146. };
  147. static ScreenSaverDefeater* screenSaverDefeater = 0;
  148. void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
  149. {
  150. if (isEnabled)
  151. {
  152. deleteAndZero (screenSaverDefeater);
  153. }
  154. else if (screenSaverDefeater == 0)
  155. {
  156. screenSaverDefeater = new ScreenSaverDefeater();
  157. }
  158. }
  159. bool Desktop::isScreenSaverEnabled() throw()
  160. {
  161. return screenSaverDefeater == 0;
  162. }
  163. #else
  164. //==============================================================================
  165. static IOPMAssertionID screenSaverDisablerID = 0;
  166. void Desktop::setScreenSaverEnabled (const bool isEnabled) throw()
  167. {
  168. if (isEnabled)
  169. {
  170. if (screenSaverDisablerID != 0)
  171. {
  172. IOPMAssertionRelease (screenSaverDisablerID);
  173. screenSaverDisablerID = 0;
  174. }
  175. }
  176. else
  177. {
  178. if (screenSaverDisablerID == 0)
  179. {
  180. IOPMAssertionCreate (kIOPMAssertionTypeNoIdleSleep,
  181. kIOPMAssertionLevelOn, &screenSaverDisablerID);
  182. }
  183. }
  184. }
  185. bool Desktop::isScreenSaverEnabled() throw()
  186. {
  187. return screenSaverDisablerID == 0;
  188. }
  189. #endif
  190. //==============================================================================
  191. void juce_updateMultiMonitorInfo (Array <Rectangle>& monitorCoords, const bool clipToWorkArea) throw()
  192. {
  193. const ScopedAutoReleasePool pool;
  194. monitorCoords.clear();
  195. NSArray* screens = [NSScreen screens];
  196. const float mainScreenBottom = [[[NSScreen screens] objectAtIndex: 0] frame].size.height;
  197. for (unsigned int i = 0; i < [screens count]; ++i)
  198. {
  199. NSScreen* s = (NSScreen*) [screens objectAtIndex: i];
  200. NSRect r = clipToWorkArea ? [s visibleFrame]
  201. : [s frame];
  202. monitorCoords.add (Rectangle ((int) r.origin.x,
  203. (int) (mainScreenBottom - (r.origin.y + r.size.height)),
  204. (int) r.size.width,
  205. (int) r.size.height));
  206. }
  207. jassert (monitorCoords.size() > 0);
  208. }
  209. #endif
  210. #endif