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.

259 lines
8.2KB

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