juce_mac_Windowing.mm 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. void LookAndFeel::playAlertSound()
  18. {
  19. NSBeep();
  20. }
  21. //==============================================================================
  22. class OSXMessageBox : private AsyncUpdater
  23. {
  24. public:
  25. OSXMessageBox (AlertWindow::AlertIconType type, const String& t, const String& m,
  26. const char* b1, const char* b2, const char* b3,
  27. ModalComponentManager::Callback* c, const bool runAsync)
  28. : iconType (type), title (t), message (m), callback (c),
  29. button1 (b1), button2 (b2), button3 (b3)
  30. {
  31. if (runAsync)
  32. triggerAsyncUpdate();
  33. }
  34. int getResult() const
  35. {
  36. switch (getRawResult())
  37. {
  38. case NSAlertDefaultReturn: return 1;
  39. case NSAlertOtherReturn: return 2;
  40. default: return 0;
  41. }
  42. }
  43. static int show (AlertWindow::AlertIconType iconType, const String& title, const String& message,
  44. ModalComponentManager::Callback* callback, const char* b1, const char* b2, const char* b3,
  45. bool runAsync)
  46. {
  47. ScopedPointer<OSXMessageBox> mb (new OSXMessageBox (iconType, title, message, b1, b2, b3,
  48. callback, runAsync));
  49. if (! runAsync)
  50. return mb->getResult();
  51. mb.release();
  52. return 0;
  53. }
  54. private:
  55. AlertWindow::AlertIconType iconType;
  56. String title, message;
  57. ScopedPointer<ModalComponentManager::Callback> callback;
  58. const char* button1;
  59. const char* button2;
  60. const char* button3;
  61. void handleAsyncUpdate() override
  62. {
  63. const int result = getResult();
  64. if (callback != nullptr)
  65. callback->modalStateFinished (result);
  66. delete this;
  67. }
  68. static NSString* translateIfNotNull (const char* s)
  69. {
  70. return s != nullptr ? juceStringToNS (TRANS (s)) : nil;
  71. }
  72. NSInteger getRawResult() const
  73. {
  74. NSString* msg = juceStringToNS (message);
  75. NSString* ttl = juceStringToNS (title);
  76. NSString* b1 = translateIfNotNull (button1);
  77. NSString* b2 = translateIfNotNull (button2);
  78. NSString* b3 = translateIfNotNull (button3);
  79. switch (iconType)
  80. {
  81. case AlertWindow::InfoIcon: return NSRunInformationalAlertPanel (ttl, msg, b1, b2, b3);
  82. case AlertWindow::WarningIcon: return NSRunCriticalAlertPanel (ttl, msg, b1, b2, b3);
  83. default: return NSRunAlertPanel (ttl, msg, b1, b2, b3);
  84. }
  85. }
  86. };
  87. #if JUCE_MODAL_LOOPS_PERMITTED
  88. void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType iconType,
  89. const String& title, const String& message,
  90. Component* /*associatedComponent*/)
  91. {
  92. OSXMessageBox::show (iconType, title, message, nullptr, "OK", nullptr, nullptr, false);
  93. }
  94. #endif
  95. void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,
  96. const String& title, const String& message,
  97. Component* /*associatedComponent*/,
  98. ModalComponentManager::Callback* callback)
  99. {
  100. OSXMessageBox::show (iconType, title, message, callback, "OK", nullptr, nullptr, true);
  101. }
  102. bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType,
  103. const String& title, const String& message,
  104. Component* /*associatedComponent*/,
  105. ModalComponentManager::Callback* callback)
  106. {
  107. return OSXMessageBox::show (iconType, title, message, callback,
  108. "OK", "Cancel", nullptr, callback != nullptr) == 1;
  109. }
  110. int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType iconType,
  111. const String& title, const String& message,
  112. Component* /*associatedComponent*/,
  113. ModalComponentManager::Callback* callback)
  114. {
  115. return OSXMessageBox::show (iconType, title, message, callback,
  116. "Yes", "Cancel", "No", callback != nullptr);
  117. }
  118. //==============================================================================
  119. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool /*canMoveFiles*/)
  120. {
  121. if (files.size() == 0)
  122. return false;
  123. MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource(0);
  124. if (draggingSource == nullptr)
  125. {
  126. jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
  127. return false;
  128. }
  129. Component* sourceComp = draggingSource->getComponentUnderMouse();
  130. if (sourceComp == nullptr)
  131. {
  132. jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
  133. return false;
  134. }
  135. JUCE_AUTORELEASEPOOL
  136. {
  137. if (NSView* view = (NSView*) sourceComp->getWindowHandle())
  138. {
  139. if (NSEvent* event = [[view window] currentEvent])
  140. {
  141. NSPoint eventPos = [event locationInWindow];
  142. NSRect dragRect = [view convertRect: NSMakeRect (eventPos.x - 16.0f, eventPos.y - 16.0f, 32.0f, 32.0f)
  143. fromView: nil];
  144. for (int i = 0; i < files.size(); ++i)
  145. {
  146. if (! [view dragFile: juceStringToNS (files[i])
  147. fromRect: dragRect
  148. slideBack: YES
  149. event: event])
  150. return false;
  151. }
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. bool DragAndDropContainer::performExternalDragDropOfText (const String& /*text*/)
  159. {
  160. jassertfalse; // not implemented!
  161. return false;
  162. }
  163. //==============================================================================
  164. bool Desktop::canUseSemiTransparentWindows() noexcept
  165. {
  166. return true;
  167. }
  168. Point<float> MouseInputSource::getCurrentRawMousePosition()
  169. {
  170. JUCE_AUTORELEASEPOOL
  171. {
  172. const NSPoint p ([NSEvent mouseLocation]);
  173. return Point<float> ((float) p.x, (float) (getMainScreenHeight() - p.y));
  174. }
  175. }
  176. void MouseInputSource::setRawMousePosition (Point<float> newPosition)
  177. {
  178. // this rubbish needs to be done around the warp call, to avoid causing a
  179. // bizarre glitch..
  180. CGAssociateMouseAndMouseCursorPosition (false);
  181. CGWarpMouseCursorPosition (convertToCGPoint (newPosition));
  182. CGAssociateMouseAndMouseCursorPosition (true);
  183. }
  184. double Desktop::getDefaultMasterScale()
  185. {
  186. return 1.0;
  187. }
  188. Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
  189. {
  190. return upright;
  191. }
  192. //==============================================================================
  193. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7)
  194. #define JUCE_USE_IOPM_SCREENSAVER_DEFEAT 1
  195. #endif
  196. #if ! (defined (JUCE_USE_IOPM_SCREENSAVER_DEFEAT) || defined (__POWER__))
  197. extern "C" { extern OSErr UpdateSystemActivity (UInt8); } // Some versions of the SDK omit this function..
  198. #endif
  199. class ScreenSaverDefeater : public Timer
  200. {
  201. public:
  202. #if JUCE_USE_IOPM_SCREENSAVER_DEFEAT
  203. ScreenSaverDefeater()
  204. {
  205. startTimer (5000);
  206. timerCallback();
  207. }
  208. void timerCallback() override
  209. {
  210. if (Process::isForegroundProcess())
  211. {
  212. if (assertion == nullptr)
  213. assertion = new PMAssertion();
  214. }
  215. else
  216. {
  217. assertion = nullptr;
  218. }
  219. }
  220. struct PMAssertion
  221. {
  222. PMAssertion() : assertionID (kIOPMNullAssertionID)
  223. {
  224. IOReturn res = IOPMAssertionCreateWithName (kIOPMAssertionTypePreventUserIdleDisplaySleep,
  225. kIOPMAssertionLevelOn,
  226. CFSTR ("JUCE Playback"),
  227. &assertionID);
  228. jassert (res == kIOReturnSuccess); (void) res;
  229. }
  230. ~PMAssertion()
  231. {
  232. if (assertionID != kIOPMNullAssertionID)
  233. IOPMAssertionRelease (assertionID);
  234. }
  235. IOPMAssertionID assertionID;
  236. };
  237. ScopedPointer<PMAssertion> assertion;
  238. #else
  239. ScreenSaverDefeater()
  240. {
  241. startTimer (10000);
  242. timerCallback();
  243. }
  244. void timerCallback() override
  245. {
  246. if (Process::isForegroundProcess())
  247. UpdateSystemActivity (1 /*UsrActivity*/);
  248. }
  249. #endif
  250. };
  251. static ScopedPointer<ScreenSaverDefeater> screenSaverDefeater;
  252. void Desktop::setScreenSaverEnabled (const bool isEnabled)
  253. {
  254. if (isEnabled)
  255. screenSaverDefeater = nullptr;
  256. else if (screenSaverDefeater == nullptr)
  257. screenSaverDefeater = new ScreenSaverDefeater();
  258. }
  259. bool Desktop::isScreenSaverEnabled()
  260. {
  261. return screenSaverDefeater == nullptr;
  262. }
  263. //==============================================================================
  264. class DisplaySettingsChangeCallback : private DeletedAtShutdown
  265. {
  266. public:
  267. DisplaySettingsChangeCallback()
  268. {
  269. CGDisplayRegisterReconfigurationCallback (displayReconfigurationCallBack, 0);
  270. }
  271. ~DisplaySettingsChangeCallback()
  272. {
  273. CGDisplayRemoveReconfigurationCallback (displayReconfigurationCallBack, 0);
  274. clearSingletonInstance();
  275. }
  276. static void displayReconfigurationCallBack (CGDirectDisplayID, CGDisplayChangeSummaryFlags, void*)
  277. {
  278. const_cast <Desktop::Displays&> (Desktop::getInstance().getDisplays()).refresh();
  279. }
  280. juce_DeclareSingleton_SingleThreaded_Minimal (DisplaySettingsChangeCallback)
  281. private:
  282. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DisplaySettingsChangeCallback)
  283. };
  284. juce_ImplementSingleton_SingleThreaded (DisplaySettingsChangeCallback)
  285. static Rectangle<int> convertDisplayRect (NSRect r, CGFloat mainScreenBottom)
  286. {
  287. r.origin.y = mainScreenBottom - (r.origin.y + r.size.height);
  288. return convertToRectInt (r);
  289. }
  290. static Desktop::Displays::Display getDisplayFromScreen (NSScreen* s, CGFloat& mainScreenBottom, const float masterScale)
  291. {
  292. Desktop::Displays::Display d;
  293. d.isMain = (mainScreenBottom == 0);
  294. if (d.isMain)
  295. mainScreenBottom = [s frame].size.height;
  296. d.userArea = convertDisplayRect ([s visibleFrame], mainScreenBottom) / masterScale;
  297. d.totalArea = convertDisplayRect ([s frame], mainScreenBottom) / masterScale;
  298. d.scale = masterScale;
  299. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  300. if ([s respondsToSelector: @selector (backingScaleFactor)])
  301. d.scale *= s.backingScaleFactor;
  302. #endif
  303. NSSize dpi = [[[s deviceDescription] objectForKey: NSDeviceResolution] sizeValue];
  304. d.dpi = (dpi.width + dpi.height) / 2.0;
  305. return d;
  306. }
  307. void Desktop::Displays::findDisplays (const float masterScale)
  308. {
  309. JUCE_AUTORELEASEPOOL
  310. {
  311. DisplaySettingsChangeCallback::getInstance();
  312. CGFloat mainScreenBottom = 0;
  313. for (NSScreen* s in [NSScreen screens])
  314. displays.add (getDisplayFromScreen (s, mainScreenBottom, masterScale));
  315. }
  316. }
  317. //==============================================================================
  318. bool juce_areThereAnyAlwaysOnTopWindows()
  319. {
  320. for (NSWindow* window in [NSApp windows])
  321. if ([window level] > NSNormalWindowLevel)
  322. return true;
  323. return false;
  324. }
  325. //==============================================================================
  326. Image juce_createIconForFile (const File& file)
  327. {
  328. JUCE_AUTORELEASEPOOL
  329. {
  330. NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile: juceStringToNS (file.getFullPathName())];
  331. Image result (Image::ARGB, (int) [image size].width, (int) [image size].height, true);
  332. [NSGraphicsContext saveGraphicsState];
  333. [NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort: juce_getImageContext (result) flipped: false]];
  334. [image drawAtPoint: NSMakePoint (0, 0)
  335. fromRect: NSMakeRect (0, 0, [image size].width, [image size].height)
  336. operation: NSCompositeSourceOver fraction: 1.0f];
  337. [[NSGraphicsContext currentContext] flushGraphics];
  338. [NSGraphicsContext restoreGraphicsState];
  339. return result;
  340. }
  341. }
  342. //==============================================================================
  343. void SystemClipboard::copyTextToClipboard (const String& text)
  344. {
  345. NSPasteboard* pb = [NSPasteboard generalPasteboard];
  346. [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType]
  347. owner: nil];
  348. [pb setString: juceStringToNS (text)
  349. forType: NSStringPboardType];
  350. }
  351. String SystemClipboard::getTextFromClipboard()
  352. {
  353. NSString* text = [[NSPasteboard generalPasteboard] stringForType: NSStringPboardType];
  354. return text == nil ? String()
  355. : nsStringToJuce (text);
  356. }
  357. void Process::setDockIconVisible (bool isVisible)
  358. {
  359. #if defined (MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6)
  360. [NSApp setActivationPolicy: isVisible ? NSApplicationActivationPolicyRegular
  361. : NSApplicationActivationPolicyProhibited];
  362. #else
  363. (void) isVisible;
  364. jassertfalse; // sorry, not available in 10.5!
  365. #endif
  366. }