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.

429 lines
14KB

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