|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2015 - ROLI Ltd.
-
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
-
- Details of these licenses can be found at: www.gnu.org/licenses
-
- JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- ------------------------------------------------------------------------------
-
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
-
- ==============================================================================
- */
-
- void LookAndFeel::playAlertSound()
- {
- NSBeep();
- }
-
- //==============================================================================
- class OSXMessageBox : private AsyncUpdater
- {
- public:
- OSXMessageBox (AlertWindow::AlertIconType type, const String& t, const String& m,
- const char* b1, const char* b2, const char* b3,
- ModalComponentManager::Callback* c, const bool runAsync)
- : iconType (type), title (t), message (m), callback (c),
- button1 (b1), button2 (b2), button3 (b3)
- {
- if (runAsync)
- triggerAsyncUpdate();
- }
-
- int getResult() const
- {
- switch (getRawResult())
- {
- case NSAlertFirstButtonReturn: return 1;
- case NSAlertThirdButtonReturn: return 2;
- default: return 0;
- }
- }
-
- static int show (AlertWindow::AlertIconType iconType, const String& title, const String& message,
- ModalComponentManager::Callback* callback, const char* b1, const char* b2, const char* b3,
- bool runAsync)
- {
- ScopedPointer<OSXMessageBox> mb (new OSXMessageBox (iconType, title, message, b1, b2, b3,
- callback, runAsync));
- if (! runAsync)
- return mb->getResult();
-
- mb.release();
- return 0;
- }
-
- private:
- AlertWindow::AlertIconType iconType;
- String title, message;
- ScopedPointer<ModalComponentManager::Callback> callback;
- const char* button1;
- const char* button2;
- const char* button3;
-
- void handleAsyncUpdate() override
- {
- const int result = getResult();
-
- if (callback != nullptr)
- callback->modalStateFinished (result);
-
- delete this;
- }
-
- NSInteger getRawResult() const
- {
- NSAlert* alert = [[[NSAlert alloc] init] autorelease];
-
- [alert setMessageText: juceStringToNS (title)];
- [alert setInformativeText: juceStringToNS (message)];
-
- [alert setAlertStyle: iconType == AlertWindow::WarningIcon ? NSCriticalAlertStyle
- : NSInformationalAlertStyle];
- addButton (alert, button1);
- addButton (alert, button2);
- addButton (alert, button3);
-
- return [alert runModal];
- }
-
- static void addButton (NSAlert* alert, const char* button)
- {
- if (button != nullptr)
- [alert addButtonWithTitle: juceStringToNS (TRANS (button))];
- }
- };
-
- #if JUCE_MODAL_LOOPS_PERMITTED
- void JUCE_CALLTYPE NativeMessageBox::showMessageBox (AlertWindow::AlertIconType iconType,
- const String& title, const String& message,
- Component* /*associatedComponent*/)
- {
- OSXMessageBox::show (iconType, title, message, nullptr, "OK", nullptr, nullptr, false);
- }
- #endif
-
- void JUCE_CALLTYPE NativeMessageBox::showMessageBoxAsync (AlertWindow::AlertIconType iconType,
- const String& title, const String& message,
- Component* /*associatedComponent*/,
- ModalComponentManager::Callback* callback)
- {
- OSXMessageBox::show (iconType, title, message, callback, "OK", nullptr, nullptr, true);
- }
-
- bool JUCE_CALLTYPE NativeMessageBox::showOkCancelBox (AlertWindow::AlertIconType iconType,
- const String& title, const String& message,
- Component* /*associatedComponent*/,
- ModalComponentManager::Callback* callback)
- {
- return OSXMessageBox::show (iconType, title, message, callback,
- "OK", "Cancel", nullptr, callback != nullptr) == 1;
- }
-
- int JUCE_CALLTYPE NativeMessageBox::showYesNoCancelBox (AlertWindow::AlertIconType iconType,
- const String& title, const String& message,
- Component* /*associatedComponent*/,
- ModalComponentManager::Callback* callback)
- {
- return OSXMessageBox::show (iconType, title, message, callback,
- "Yes", "Cancel", "No", callback != nullptr);
- }
-
-
- //==============================================================================
- bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool /*canMoveFiles*/)
- {
- if (files.size() == 0)
- return false;
-
- MouseInputSource* draggingSource = Desktop::getInstance().getDraggingMouseSource(0);
-
- if (draggingSource == nullptr)
- {
- jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
- return false;
- }
-
- Component* sourceComp = draggingSource->getComponentUnderMouse();
-
- if (sourceComp == nullptr)
- {
- jassertfalse; // This method must be called in response to a component's mouseDown or mouseDrag event!
- return false;
- }
-
- JUCE_AUTORELEASEPOOL
- {
- if (NSView* view = (NSView*) sourceComp->getWindowHandle())
- {
- if (NSEvent* event = [[view window] currentEvent])
- {
- NSPoint eventPos = [event locationInWindow];
- NSRect dragRect = [view convertRect: NSMakeRect (eventPos.x - 16.0f, eventPos.y - 16.0f, 32.0f, 32.0f)
- fromView: nil];
-
- for (int i = 0; i < files.size(); ++i)
- {
- if (! [view dragFile: juceStringToNS (files[i])
- fromRect: dragRect
- slideBack: YES
- event: event])
- return false;
- }
-
- return true;
- }
- }
- }
-
- return false;
- }
-
- bool DragAndDropContainer::performExternalDragDropOfText (const String& /*text*/)
- {
- jassertfalse; // not implemented!
- return false;
- }
-
- //==============================================================================
- bool Desktop::canUseSemiTransparentWindows() noexcept
- {
- return true;
- }
-
- Point<float> MouseInputSource::getCurrentRawMousePosition()
- {
- JUCE_AUTORELEASEPOOL
- {
- const NSPoint p ([NSEvent mouseLocation]);
- return Point<float> ((float) p.x, (float) (getMainScreenHeight() - p.y));
- }
- }
-
- void MouseInputSource::setRawMousePosition (Point<float> newPosition)
- {
- // this rubbish needs to be done around the warp call, to avoid causing a
- // bizarre glitch..
- CGAssociateMouseAndMouseCursorPosition (false);
- CGWarpMouseCursorPosition (convertToCGPoint (newPosition));
- CGAssociateMouseAndMouseCursorPosition (true);
- }
-
- double Desktop::getDefaultMasterScale()
- {
- return 1.0;
- }
-
- Desktop::DisplayOrientation Desktop::getCurrentOrientation() const
- {
- return upright;
- }
-
- //==============================================================================
- #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_7)
- #define JUCE_USE_IOPM_SCREENSAVER_DEFEAT 1
- #endif
-
- #if ! (defined (JUCE_USE_IOPM_SCREENSAVER_DEFEAT) || defined (__POWER__))
- extern "C" { extern OSErr UpdateSystemActivity (UInt8); } // Some versions of the SDK omit this function..
- #endif
-
- class ScreenSaverDefeater : public Timer
- {
- public:
- #if JUCE_USE_IOPM_SCREENSAVER_DEFEAT
- ScreenSaverDefeater()
- {
- startTimer (5000);
- timerCallback();
- }
-
- void timerCallback() override
- {
- if (Process::isForegroundProcess())
- {
- if (assertion == nullptr)
- assertion = new PMAssertion();
- }
- else
- {
- assertion = nullptr;
- }
- }
-
- struct PMAssertion
- {
- PMAssertion() : assertionID (kIOPMNullAssertionID)
- {
- IOReturn res = IOPMAssertionCreateWithName (kIOPMAssertionTypePreventUserIdleDisplaySleep,
- kIOPMAssertionLevelOn,
- CFSTR ("JUCE Playback"),
- &assertionID);
- jassert (res == kIOReturnSuccess); ignoreUnused (res);
- }
-
- ~PMAssertion()
- {
- if (assertionID != kIOPMNullAssertionID)
- IOPMAssertionRelease (assertionID);
- }
-
- IOPMAssertionID assertionID;
- };
-
- ScopedPointer<PMAssertion> assertion;
- #else
- ScreenSaverDefeater()
- {
- startTimer (10000);
- timerCallback();
- }
-
- void timerCallback() override
- {
- if (Process::isForegroundProcess())
- UpdateSystemActivity (1 /*UsrActivity*/);
- }
- #endif
- };
-
- static ScopedPointer<ScreenSaverDefeater> screenSaverDefeater;
-
- void Desktop::setScreenSaverEnabled (const bool isEnabled)
- {
- if (isEnabled)
- screenSaverDefeater = nullptr;
- else if (screenSaverDefeater == nullptr)
- screenSaverDefeater = new ScreenSaverDefeater();
- }
-
- bool Desktop::isScreenSaverEnabled()
- {
- return screenSaverDefeater == nullptr;
- }
-
- //==============================================================================
- class DisplaySettingsChangeCallback : private DeletedAtShutdown
- {
- public:
- DisplaySettingsChangeCallback()
- {
- CGDisplayRegisterReconfigurationCallback (displayReconfigurationCallBack, 0);
- }
-
- ~DisplaySettingsChangeCallback()
- {
- CGDisplayRemoveReconfigurationCallback (displayReconfigurationCallBack, 0);
- clearSingletonInstance();
- }
-
- static void displayReconfigurationCallBack (CGDirectDisplayID, CGDisplayChangeSummaryFlags, void*)
- {
- const_cast<Desktop::Displays&> (Desktop::getInstance().getDisplays()).refresh();
- }
-
- juce_DeclareSingleton_SingleThreaded_Minimal (DisplaySettingsChangeCallback)
-
- private:
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DisplaySettingsChangeCallback)
- };
-
- juce_ImplementSingleton_SingleThreaded (DisplaySettingsChangeCallback)
-
- static Rectangle<int> convertDisplayRect (NSRect r, CGFloat mainScreenBottom)
- {
- r.origin.y = mainScreenBottom - (r.origin.y + r.size.height);
- return convertToRectInt (r);
- }
-
- static Desktop::Displays::Display getDisplayFromScreen (NSScreen* s, CGFloat& mainScreenBottom, const float masterScale)
- {
- Desktop::Displays::Display d;
-
- d.isMain = (mainScreenBottom == 0);
-
- if (d.isMain)
- mainScreenBottom = [s frame].size.height;
-
- d.userArea = convertDisplayRect ([s visibleFrame], mainScreenBottom) / masterScale;
- d.totalArea = convertDisplayRect ([s frame], mainScreenBottom) / masterScale;
- d.scale = masterScale;
-
- #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
- if ([s respondsToSelector: @selector (backingScaleFactor)])
- d.scale *= s.backingScaleFactor;
- #endif
-
- NSSize dpi = [[[s deviceDescription] objectForKey: NSDeviceResolution] sizeValue];
- d.dpi = (dpi.width + dpi.height) / 2.0;
-
- return d;
- }
-
- void Desktop::Displays::findDisplays (const float masterScale)
- {
- JUCE_AUTORELEASEPOOL
- {
- DisplaySettingsChangeCallback::getInstance();
-
- CGFloat mainScreenBottom = 0;
-
- for (NSScreen* s in [NSScreen screens])
- displays.add (getDisplayFromScreen (s, mainScreenBottom, masterScale));
- }
- }
-
- //==============================================================================
- bool juce_areThereAnyAlwaysOnTopWindows()
- {
- for (NSWindow* window in [NSApp windows])
- if ([window level] > NSNormalWindowLevel)
- return true;
-
- return false;
- }
-
- //==============================================================================
- static void selectImageForDrawing (const Image& image)
- {
- [NSGraphicsContext saveGraphicsState];
- [NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithGraphicsPort: juce_getImageContext (image)
- flipped: false]];
- }
-
- static void releaseImageAfterDrawing()
- {
- [[NSGraphicsContext currentContext] flushGraphics];
- [NSGraphicsContext restoreGraphicsState];
- }
-
- Image juce_createIconForFile (const File& file)
- {
- JUCE_AUTORELEASEPOOL
- {
- NSImage* image = [[NSWorkspace sharedWorkspace] iconForFile: juceStringToNS (file.getFullPathName())];
-
- Image result (Image::ARGB, (int) [image size].width, (int) [image size].height, true);
-
- selectImageForDrawing (result);
- [image drawAtPoint: NSMakePoint (0, 0)
- fromRect: NSMakeRect (0, 0, [image size].width, [image size].height)
- operation: NSCompositeSourceOver fraction: 1.0f];
- releaseImageAfterDrawing();
-
- return result;
- }
- }
-
- static Image createNSWindowSnapshot (NSWindow* nsWindow)
- {
- JUCE_AUTORELEASEPOOL
- {
- CGImageRef screenShot = CGWindowListCreateImage (CGRectNull,
- kCGWindowListOptionIncludingWindow,
- (CGWindowID) [nsWindow windowNumber],
- kCGWindowImageBoundsIgnoreFraming);
-
- NSBitmapImageRep* bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage: screenShot];
-
- Image result (Image::ARGB, (int) [bitmapRep size].width, (int) [bitmapRep size].height, true);
-
- selectImageForDrawing (result);
- [bitmapRep drawAtPoint: NSMakePoint (0, 0)];
- releaseImageAfterDrawing();
-
- [bitmapRep release];
- CGImageRelease (screenShot);
-
- return result;
- }
- }
-
- Image createSnapshotOfNativeWindow (void* nativeWindowHandle)
- {
- if (id windowOrView = (id) nativeWindowHandle)
- {
- if ([windowOrView isKindOfClass: [NSWindow class]])
- return createNSWindowSnapshot ((NSWindow*) windowOrView);
-
- if ([windowOrView isKindOfClass: [NSView class]])
- return createNSWindowSnapshot ([(NSView*) windowOrView window]);
- }
-
- return Image();
- }
-
- //==============================================================================
- void SystemClipboard::copyTextToClipboard (const String& text)
- {
- NSPasteboard* pb = [NSPasteboard generalPasteboard];
-
- [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType]
- owner: nil];
-
- [pb setString: juceStringToNS (text)
- forType: NSStringPboardType];
- }
-
- String SystemClipboard::getTextFromClipboard()
- {
- return nsStringToJuce ([[NSPasteboard generalPasteboard] stringForType: NSStringPboardType]);
- }
-
- void Process::setDockIconVisible (bool isVisible)
- {
- #if defined (MAC_OS_X_VERSION_10_6) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6)
- [NSApp setActivationPolicy: isVisible ? NSApplicationActivationPolicyRegular
- : NSApplicationActivationPolicyProhibited];
- #else
- ignoreUnused (isVisible);
- jassertfalse; // sorry, not available in 10.5!
- #endif
- }
-
- bool Desktop::isOSXDarkModeActive()
- {
- return [[[NSUserDefaults standardUserDefaults] stringForKey: nsStringLiteral ("AppleInterfaceStyle")]
- isEqualToString: nsStringLiteral ("Dark")];
- }
|