123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- /*
- ==============================================================================
- This file is part of the JUCE library.
- Copyright (c) 2013 - Raw Material Software 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.
- ==============================================================================
- */
- Desktop::Desktop()
- : mouseSources (new MouseInputSource::SourceList()),
- mouseClickCounter (0), mouseWheelCounter (0),
- kioskModeComponent (nullptr),
- kioskModeReentrant (false),
- allowedOrientations (allOrientations),
- masterScaleFactor ((float) getDefaultMasterScale())
- {
- displays = new Displays (*this);
- }
- Desktop::~Desktop()
- {
- setScreenSaverEnabled (true);
- jassert (instance == this);
- instance = nullptr;
- // doh! If you don't delete all your windows before exiting, you're going to
- // be leaking memory!
- jassert (desktopComponents.size() == 0);
- }
- Desktop& JUCE_CALLTYPE Desktop::getInstance()
- {
- if (instance == nullptr)
- instance = new Desktop();
- return *instance;
- }
- Desktop* Desktop::instance = nullptr;
- //==============================================================================
- int Desktop::getNumComponents() const noexcept
- {
- return desktopComponents.size();
- }
- Component* Desktop::getComponent (const int index) const noexcept
- {
- return desktopComponents [index];
- }
- Component* Desktop::findComponentAt (Point<int> screenPosition) const
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- for (int i = desktopComponents.size(); --i >= 0;)
- {
- Component* const c = desktopComponents.getUnchecked(i);
- if (c->isVisible())
- {
- const Point<int> relative (c->getLocalPoint (nullptr, screenPosition));
- if (c->contains (relative))
- return c->getComponentAt (relative);
- }
- }
- return nullptr;
- }
- //==============================================================================
- LookAndFeel& Desktop::getDefaultLookAndFeel() noexcept
- {
- if (currentLookAndFeel == nullptr)
- {
- if (defaultLookAndFeel == nullptr)
- defaultLookAndFeel = new LookAndFeel_V2();
- currentLookAndFeel = defaultLookAndFeel;
- }
- return *currentLookAndFeel;
- }
- void Desktop::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel)
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- currentLookAndFeel = newDefaultLookAndFeel;
- for (int i = getNumComponents(); --i >= 0;)
- if (Component* const c = getComponent (i))
- c->sendLookAndFeelChange();
- }
- //==============================================================================
- void Desktop::addDesktopComponent (Component* const c)
- {
- jassert (c != nullptr);
- jassert (! desktopComponents.contains (c));
- desktopComponents.addIfNotAlreadyThere (c);
- }
- void Desktop::removeDesktopComponent (Component* const c)
- {
- desktopComponents.removeFirstMatchingValue (c);
- }
- void Desktop::componentBroughtToFront (Component* const c)
- {
- const int index = desktopComponents.indexOf (c);
- jassert (index >= 0);
- if (index >= 0)
- {
- int newIndex = -1;
- if (! c->isAlwaysOnTop())
- {
- newIndex = desktopComponents.size();
- while (newIndex > 0 && desktopComponents.getUnchecked (newIndex - 1)->isAlwaysOnTop())
- --newIndex;
- --newIndex;
- }
- desktopComponents.move (index, newIndex);
- }
- }
- //==============================================================================
- Point<int> Desktop::getMousePosition()
- {
- return getMousePositionFloat().roundToInt();
- }
- Point<float> Desktop::getMousePositionFloat()
- {
- return getInstance().getMainMouseSource().getScreenPosition();
- }
- void Desktop::setMousePosition (Point<int> newPosition)
- {
- getInstance().getMainMouseSource().setScreenPosition (newPosition.toFloat());
- }
- Point<int> Desktop::getLastMouseDownPosition()
- {
- return getInstance().getMainMouseSource().getLastMouseDownPosition().roundToInt();
- }
- int Desktop::getMouseButtonClickCounter() const noexcept { return mouseClickCounter; }
- int Desktop::getMouseWheelMoveCounter() const noexcept { return mouseWheelCounter; }
- void Desktop::incrementMouseClickCounter() noexcept { ++mouseClickCounter; }
- void Desktop::incrementMouseWheelCounter() noexcept { ++mouseWheelCounter; }
- const Array<MouseInputSource>& Desktop::getMouseSources() const noexcept { return mouseSources->sourceArray; }
- int Desktop::getNumMouseSources() const noexcept { return mouseSources->sources.size(); }
- int Desktop::getNumDraggingMouseSources() const noexcept { return mouseSources->getNumDraggingMouseSources(); }
- MouseInputSource* Desktop::getMouseSource (int index) const noexcept { return mouseSources->getMouseSource (index); }
- MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept { return mouseSources->getDraggingMouseSource (index); }
- MouseInputSource Desktop::getMainMouseSource() const noexcept { return MouseInputSource (mouseSources->sources.getUnchecked(0)); }
- void Desktop::beginDragAutoRepeat (int interval) { mouseSources->beginDragAutoRepeat (interval); }
- //==============================================================================
- void Desktop::addFocusChangeListener (FocusChangeListener* const listener) { focusListeners.add (listener); }
- void Desktop::removeFocusChangeListener (FocusChangeListener* const listener) { focusListeners.remove (listener); }
- void Desktop::triggerFocusCallback() { triggerAsyncUpdate(); }
- void Desktop::handleAsyncUpdate()
- {
- // The component may be deleted during this operation, but we'll use a SafePointer rather than a
- // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
- WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
- focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
- }
- //==============================================================================
- void Desktop::resetTimer()
- {
- if (mouseListeners.size() == 0)
- stopTimer();
- else
- startTimer (100);
- lastFakeMouseMove = getMousePositionFloat();
- }
- ListenerList<MouseListener>& Desktop::getMouseListeners()
- {
- resetTimer();
- return mouseListeners;
- }
- void Desktop::addGlobalMouseListener (MouseListener* const listener)
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- mouseListeners.add (listener);
- resetTimer();
- }
- void Desktop::removeGlobalMouseListener (MouseListener* const listener)
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- mouseListeners.remove (listener);
- resetTimer();
- }
- void Desktop::timerCallback()
- {
- if (lastFakeMouseMove != getMousePositionFloat())
- sendMouseMove();
- }
- void Desktop::sendMouseMove()
- {
- if (! mouseListeners.isEmpty())
- {
- startTimer (20);
- lastFakeMouseMove = getMousePositionFloat();
- if (Component* const target = findComponentAt (lastFakeMouseMove.roundToInt()))
- {
- Component::BailOutChecker checker (target);
- const Point<float> pos (target->getLocalPoint (nullptr, lastFakeMouseMove));
- const Time now (Time::getCurrentTime());
- const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(),
- target, target, now, pos, now, 0, false);
- if (me.mods.isAnyMouseButtonDown())
- mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
- else
- mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
- }
- }
- }
- //==============================================================================
- Desktop::Displays::Displays (Desktop& desktop) { init (desktop); }
- Desktop::Displays::~Displays() {}
- const Desktop::Displays::Display& Desktop::Displays::getMainDisplay() const noexcept
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- jassert (displays.getReference(0).isMain);
- return displays.getReference(0);
- }
- const Desktop::Displays::Display& Desktop::Displays::getDisplayContaining (Point<int> position) const noexcept
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- const Display* best = &displays.getReference(0);
- double bestDistance = 1.0e10;
- for (int i = displays.size(); --i >= 0;)
- {
- const Display& d = displays.getReference(i);
- if (d.totalArea.contains (position))
- {
- best = &d;
- break;
- }
- const double distance = d.totalArea.getCentre().getDistanceFrom (position);
- if (distance < bestDistance)
- {
- bestDistance = distance;
- best = &d;
- }
- }
- return *best;
- }
- RectangleList<int> Desktop::Displays::getRectangleList (bool userAreasOnly) const
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- RectangleList<int> rl;
- for (int i = 0; i < displays.size(); ++i)
- {
- const Display& d = displays.getReference(i);
- rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
- }
- return rl;
- }
- Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
- {
- return getRectangleList (userAreasOnly).getBounds();
- }
- bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
- bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
- {
- return d1.userArea == d2.userArea
- && d1.totalArea == d2.totalArea
- && d1.scale == d2.scale
- && d1.isMain == d2.isMain;
- }
- bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
- bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
- {
- return ! (d1 == d2);
- }
- void Desktop::Displays::init (Desktop& desktop)
- {
- findDisplays (desktop.getGlobalScaleFactor());
- jassert (displays.size() > 0);
- }
- void Desktop::Displays::refresh()
- {
- Array<Display> oldDisplays;
- oldDisplays.swapWith (displays);
- init (Desktop::getInstance());
- jassert (displays.size() > 0);
- if (oldDisplays != displays)
- {
- for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
- if (ComponentPeer* const peer = ComponentPeer::getPeer (i))
- peer->handleScreenSizeChange();
- }
- }
- //==============================================================================
- void Desktop::setKioskModeComponent (Component* componentToUse, const bool allowMenusAndBars)
- {
- if (kioskModeReentrant)
- return;
- const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
- if (kioskModeComponent != componentToUse)
- {
- // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
- jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
- if (Component* const oldKioskComp = kioskModeComponent)
- {
- kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
- setKioskComponent (oldKioskComp, false, allowMenusAndBars);
- oldKioskComp->setBounds (kioskComponentOriginalBounds);
- }
- kioskModeComponent = componentToUse;
- if (kioskModeComponent != nullptr)
- {
- // Only components that are already on the desktop can be put into kiosk mode!
- jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
- kioskComponentOriginalBounds = kioskModeComponent->getBounds();
- setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
- }
- }
- }
- //==============================================================================
- void Desktop::setOrientationsEnabled (const int newOrientations)
- {
- // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
- jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
- allowedOrientations = newOrientations;
- }
- bool Desktop::isOrientationEnabled (const DisplayOrientation orientation) const noexcept
- {
- // Make sure you only pass one valid flag in here...
- jassert (orientation == upright || orientation == upsideDown
- || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
- return (allowedOrientations & orientation) != 0;
- }
- void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
- {
- ASSERT_MESSAGE_MANAGER_IS_LOCKED
- if (masterScaleFactor != newScaleFactor)
- {
- masterScaleFactor = newScaleFactor;
- displays->refresh();
- }
- }
|