Audio plugin host https://kx.studio/carla
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.

419 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. Desktop::Desktop()
  18. : mouseSources (new MouseInputSource::SourceList()),
  19. mouseClickCounter (0), mouseWheelCounter (0),
  20. kioskModeComponent (nullptr),
  21. kioskModeReentrant (false),
  22. allowedOrientations (allOrientations),
  23. masterScaleFactor ((float) getDefaultMasterScale())
  24. {
  25. displays = new Displays (*this);
  26. }
  27. Desktop::~Desktop()
  28. {
  29. setScreenSaverEnabled (true);
  30. animator.cancelAllAnimations (false);
  31. jassert (instance == this);
  32. instance = nullptr;
  33. // doh! If you don't delete all your windows before exiting, you're going to
  34. // be leaking memory!
  35. jassert (desktopComponents.size() == 0);
  36. }
  37. Desktop& JUCE_CALLTYPE Desktop::getInstance()
  38. {
  39. if (instance == nullptr)
  40. instance = new Desktop();
  41. return *instance;
  42. }
  43. Desktop* Desktop::instance = nullptr;
  44. //==============================================================================
  45. int Desktop::getNumComponents() const noexcept
  46. {
  47. return desktopComponents.size();
  48. }
  49. Component* Desktop::getComponent (const int index) const noexcept
  50. {
  51. return desktopComponents [index];
  52. }
  53. Component* Desktop::findComponentAt (Point<int> screenPosition) const
  54. {
  55. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  56. for (int i = desktopComponents.size(); --i >= 0;)
  57. {
  58. Component* const c = desktopComponents.getUnchecked(i);
  59. if (c->isVisible())
  60. {
  61. const Point<int> relative (c->getLocalPoint (nullptr, screenPosition));
  62. if (c->contains (relative))
  63. return c->getComponentAt (relative);
  64. }
  65. }
  66. return nullptr;
  67. }
  68. //==============================================================================
  69. LookAndFeel& Desktop::getDefaultLookAndFeel() noexcept
  70. {
  71. if (currentLookAndFeel == nullptr)
  72. {
  73. if (defaultLookAndFeel == nullptr)
  74. defaultLookAndFeel = new LookAndFeel_V3();
  75. currentLookAndFeel = defaultLookAndFeel;
  76. }
  77. return *currentLookAndFeel;
  78. }
  79. void Desktop::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel)
  80. {
  81. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  82. currentLookAndFeel = newDefaultLookAndFeel;
  83. for (int i = getNumComponents(); --i >= 0;)
  84. if (Component* const c = getComponent (i))
  85. c->sendLookAndFeelChange();
  86. }
  87. //==============================================================================
  88. void Desktop::addDesktopComponent (Component* const c)
  89. {
  90. jassert (c != nullptr);
  91. jassert (! desktopComponents.contains (c));
  92. desktopComponents.addIfNotAlreadyThere (c);
  93. }
  94. void Desktop::removeDesktopComponent (Component* const c)
  95. {
  96. desktopComponents.removeFirstMatchingValue (c);
  97. }
  98. void Desktop::componentBroughtToFront (Component* const c)
  99. {
  100. const int index = desktopComponents.indexOf (c);
  101. jassert (index >= 0);
  102. if (index >= 0)
  103. {
  104. int newIndex = -1;
  105. if (! c->isAlwaysOnTop())
  106. {
  107. newIndex = desktopComponents.size();
  108. while (newIndex > 0 && desktopComponents.getUnchecked (newIndex - 1)->isAlwaysOnTop())
  109. --newIndex;
  110. --newIndex;
  111. }
  112. desktopComponents.move (index, newIndex);
  113. }
  114. }
  115. //==============================================================================
  116. Point<int> Desktop::getMousePosition()
  117. {
  118. return getMousePositionFloat().roundToInt();
  119. }
  120. Point<float> Desktop::getMousePositionFloat()
  121. {
  122. return getInstance().getMainMouseSource().getScreenPosition();
  123. }
  124. void Desktop::setMousePosition (Point<int> newPosition)
  125. {
  126. getInstance().getMainMouseSource().setScreenPosition (newPosition.toFloat());
  127. }
  128. Point<int> Desktop::getLastMouseDownPosition()
  129. {
  130. return getInstance().getMainMouseSource().getLastMouseDownPosition().roundToInt();
  131. }
  132. int Desktop::getMouseButtonClickCounter() const noexcept { return mouseClickCounter; }
  133. int Desktop::getMouseWheelMoveCounter() const noexcept { return mouseWheelCounter; }
  134. void Desktop::incrementMouseClickCounter() noexcept { ++mouseClickCounter; }
  135. void Desktop::incrementMouseWheelCounter() noexcept { ++mouseWheelCounter; }
  136. const Array<MouseInputSource>& Desktop::getMouseSources() const noexcept { return mouseSources->sourceArray; }
  137. int Desktop::getNumMouseSources() const noexcept { return mouseSources->sources.size(); }
  138. int Desktop::getNumDraggingMouseSources() const noexcept { return mouseSources->getNumDraggingMouseSources(); }
  139. MouseInputSource* Desktop::getMouseSource (int index) const noexcept { return mouseSources->getMouseSource (index); }
  140. MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept { return mouseSources->getDraggingMouseSource (index); }
  141. MouseInputSource Desktop::getMainMouseSource() const noexcept { return MouseInputSource (mouseSources->sources.getUnchecked(0)); }
  142. void Desktop::beginDragAutoRepeat (int interval) { mouseSources->beginDragAutoRepeat (interval); }
  143. //==============================================================================
  144. void Desktop::addFocusChangeListener (FocusChangeListener* const listener) { focusListeners.add (listener); }
  145. void Desktop::removeFocusChangeListener (FocusChangeListener* const listener) { focusListeners.remove (listener); }
  146. void Desktop::triggerFocusCallback() { triggerAsyncUpdate(); }
  147. void Desktop::handleAsyncUpdate()
  148. {
  149. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  150. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  151. WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
  152. focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
  153. }
  154. //==============================================================================
  155. void Desktop::resetTimer()
  156. {
  157. if (mouseListeners.size() == 0)
  158. stopTimer();
  159. else
  160. startTimer (100);
  161. lastFakeMouseMove = getMousePositionFloat();
  162. }
  163. ListenerList<MouseListener>& Desktop::getMouseListeners()
  164. {
  165. resetTimer();
  166. return mouseListeners;
  167. }
  168. void Desktop::addGlobalMouseListener (MouseListener* const listener)
  169. {
  170. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  171. mouseListeners.add (listener);
  172. resetTimer();
  173. }
  174. void Desktop::removeGlobalMouseListener (MouseListener* const listener)
  175. {
  176. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  177. mouseListeners.remove (listener);
  178. resetTimer();
  179. }
  180. void Desktop::timerCallback()
  181. {
  182. if (lastFakeMouseMove != getMousePositionFloat())
  183. sendMouseMove();
  184. }
  185. void Desktop::sendMouseMove()
  186. {
  187. if (! mouseListeners.isEmpty())
  188. {
  189. startTimer (20);
  190. lastFakeMouseMove = getMousePositionFloat();
  191. if (Component* const target = findComponentAt (lastFakeMouseMove.roundToInt()))
  192. {
  193. Component::BailOutChecker checker (target);
  194. const Point<float> pos (target->getLocalPoint (nullptr, lastFakeMouseMove));
  195. const Time now (Time::getCurrentTime());
  196. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(),
  197. MouseInputSource::invalidPressure, target, target, now, pos, now, 0, false);
  198. if (me.mods.isAnyMouseButtonDown())
  199. mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
  200. else
  201. mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
  202. }
  203. }
  204. }
  205. //==============================================================================
  206. Desktop::Displays::Displays (Desktop& desktop) { init (desktop); }
  207. Desktop::Displays::~Displays() {}
  208. const Desktop::Displays::Display& Desktop::Displays::getMainDisplay() const noexcept
  209. {
  210. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  211. jassert (displays.getReference(0).isMain);
  212. return displays.getReference(0);
  213. }
  214. const Desktop::Displays::Display& Desktop::Displays::getDisplayContaining (Point<int> position) const noexcept
  215. {
  216. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  217. const Display* best = &displays.getReference(0);
  218. double bestDistance = 1.0e10;
  219. for (int i = displays.size(); --i >= 0;)
  220. {
  221. const Display& d = displays.getReference(i);
  222. if (d.totalArea.contains (position))
  223. {
  224. best = &d;
  225. break;
  226. }
  227. const double distance = d.totalArea.getCentre().getDistanceFrom (position);
  228. if (distance < bestDistance)
  229. {
  230. bestDistance = distance;
  231. best = &d;
  232. }
  233. }
  234. return *best;
  235. }
  236. RectangleList<int> Desktop::Displays::getRectangleList (bool userAreasOnly) const
  237. {
  238. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  239. RectangleList<int> rl;
  240. for (int i = 0; i < displays.size(); ++i)
  241. {
  242. const Display& d = displays.getReference(i);
  243. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  244. }
  245. return rl;
  246. }
  247. Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
  248. {
  249. return getRectangleList (userAreasOnly).getBounds();
  250. }
  251. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  252. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  253. {
  254. return d1.userArea == d2.userArea
  255. && d1.totalArea == d2.totalArea
  256. && d1.scale == d2.scale
  257. && d1.isMain == d2.isMain;
  258. }
  259. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  260. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  261. {
  262. return ! (d1 == d2);
  263. }
  264. void Desktop::Displays::init (Desktop& desktop)
  265. {
  266. findDisplays (desktop.getGlobalScaleFactor());
  267. }
  268. void Desktop::Displays::refresh()
  269. {
  270. Array<Display> oldDisplays;
  271. oldDisplays.swapWith (displays);
  272. init (Desktop::getInstance());
  273. if (oldDisplays != displays)
  274. {
  275. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  276. if (ComponentPeer* const peer = ComponentPeer::getPeer (i))
  277. peer->handleScreenSizeChange();
  278. }
  279. }
  280. //==============================================================================
  281. void Desktop::setKioskModeComponent (Component* componentToUse, const bool allowMenusAndBars)
  282. {
  283. if (kioskModeReentrant)
  284. return;
  285. const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
  286. if (kioskModeComponent != componentToUse)
  287. {
  288. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  289. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  290. if (Component* const oldKioskComp = kioskModeComponent)
  291. {
  292. kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
  293. setKioskComponent (oldKioskComp, false, allowMenusAndBars);
  294. oldKioskComp->setBounds (kioskComponentOriginalBounds);
  295. }
  296. kioskModeComponent = componentToUse;
  297. if (kioskModeComponent != nullptr)
  298. {
  299. // Only components that are already on the desktop can be put into kiosk mode!
  300. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  301. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  302. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  303. }
  304. }
  305. }
  306. //==============================================================================
  307. void Desktop::setOrientationsEnabled (const int newOrientations)
  308. {
  309. if (allowedOrientations != newOrientations)
  310. {
  311. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  312. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  313. allowedOrientations = newOrientations;
  314. allowedOrientationsChanged();
  315. }
  316. }
  317. bool Desktop::isOrientationEnabled (const DisplayOrientation orientation) const noexcept
  318. {
  319. // Make sure you only pass one valid flag in here...
  320. jassert (orientation == upright || orientation == upsideDown
  321. || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
  322. return (allowedOrientations & orientation) != 0;
  323. }
  324. void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
  325. {
  326. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  327. if (masterScaleFactor != newScaleFactor)
  328. {
  329. masterScaleFactor = newScaleFactor;
  330. displays->refresh();
  331. }
  332. }