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.

428 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. //==============================================================================
  146. void Desktop::addFocusChangeListener (FocusChangeListener* const listener) { focusListeners.add (listener); }
  147. void Desktop::removeFocusChangeListener (FocusChangeListener* const listener) { focusListeners.remove (listener); }
  148. void Desktop::triggerFocusCallback() { triggerAsyncUpdate(); }
  149. void Desktop::handleAsyncUpdate()
  150. {
  151. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  152. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  153. WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
  154. focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
  155. }
  156. //==============================================================================
  157. void Desktop::resetTimer()
  158. {
  159. if (mouseListeners.size() == 0)
  160. stopTimer();
  161. else
  162. startTimer (100);
  163. lastFakeMouseMove = getMousePositionFloat();
  164. }
  165. ListenerList<MouseListener>& Desktop::getMouseListeners()
  166. {
  167. resetTimer();
  168. return mouseListeners;
  169. }
  170. void Desktop::addGlobalMouseListener (MouseListener* const listener)
  171. {
  172. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  173. mouseListeners.add (listener);
  174. resetTimer();
  175. }
  176. void Desktop::removeGlobalMouseListener (MouseListener* const listener)
  177. {
  178. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  179. mouseListeners.remove (listener);
  180. resetTimer();
  181. }
  182. void Desktop::timerCallback()
  183. {
  184. if (lastFakeMouseMove != getMousePositionFloat())
  185. sendMouseMove();
  186. }
  187. void Desktop::sendMouseMove()
  188. {
  189. if (! mouseListeners.isEmpty())
  190. {
  191. startTimer (20);
  192. lastFakeMouseMove = getMousePositionFloat();
  193. if (auto* target = findComponentAt (lastFakeMouseMove.roundToInt()))
  194. {
  195. Component::BailOutChecker checker (target);
  196. const Point<float> pos (target->getLocalPoint (nullptr, lastFakeMouseMove));
  197. const Time now (Time::getCurrentTime());
  198. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(), MouseInputSource::invalidPressure,
  199. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  200. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  201. target, target, now, pos, now, 0, false);
  202. if (me.mods.isAnyMouseButtonDown())
  203. mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
  204. else
  205. mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
  206. }
  207. }
  208. }
  209. //==============================================================================
  210. Desktop::Displays::Displays (Desktop& desktop) { init (desktop); }
  211. Desktop::Displays::~Displays() {}
  212. const Desktop::Displays::Display& Desktop::Displays::getMainDisplay() const noexcept
  213. {
  214. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  215. jassert (displays.getReference(0).isMain);
  216. return displays.getReference(0);
  217. }
  218. const Desktop::Displays::Display& Desktop::Displays::getDisplayContaining (Point<int> position) const noexcept
  219. {
  220. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  221. const Display* best = &displays.getReference(0);
  222. double bestDistance = 1.0e10;
  223. for (int i = displays.size(); --i >= 0;)
  224. {
  225. const Display& d = displays.getReference(i);
  226. if (d.totalArea.contains (position))
  227. {
  228. best = &d;
  229. break;
  230. }
  231. const double distance = d.totalArea.getCentre().getDistanceFrom (position);
  232. if (distance < bestDistance)
  233. {
  234. bestDistance = distance;
  235. best = &d;
  236. }
  237. }
  238. return *best;
  239. }
  240. RectangleList<int> Desktop::Displays::getRectangleList (bool userAreasOnly) const
  241. {
  242. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  243. RectangleList<int> rl;
  244. for (int i = 0; i < displays.size(); ++i)
  245. {
  246. const Display& d = displays.getReference(i);
  247. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  248. }
  249. return rl;
  250. }
  251. Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
  252. {
  253. return getRectangleList (userAreasOnly).getBounds();
  254. }
  255. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  256. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  257. {
  258. return d1.userArea == d2.userArea
  259. && d1.totalArea == d2.totalArea
  260. && d1.scale == d2.scale
  261. && d1.isMain == d2.isMain;
  262. }
  263. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  264. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  265. {
  266. return ! (d1 == d2);
  267. }
  268. void Desktop::Displays::init (Desktop& desktop)
  269. {
  270. findDisplays (desktop.getGlobalScaleFactor());
  271. }
  272. void Desktop::Displays::refresh()
  273. {
  274. Array<Display> oldDisplays;
  275. oldDisplays.swapWith (displays);
  276. init (Desktop::getInstance());
  277. if (oldDisplays != displays)
  278. {
  279. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  280. if (ComponentPeer* const peer = ComponentPeer::getPeer (i))
  281. peer->handleScreenSizeChange();
  282. }
  283. }
  284. //==============================================================================
  285. void Desktop::setKioskModeComponent (Component* componentToUse, const bool allowMenusAndBars)
  286. {
  287. if (kioskModeReentrant)
  288. return;
  289. const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
  290. if (kioskModeComponent != componentToUse)
  291. {
  292. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  293. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  294. if (Component* const oldKioskComp = kioskModeComponent)
  295. {
  296. kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
  297. setKioskComponent (oldKioskComp, false, allowMenusAndBars);
  298. oldKioskComp->setBounds (kioskComponentOriginalBounds);
  299. }
  300. kioskModeComponent = componentToUse;
  301. if (kioskModeComponent != nullptr)
  302. {
  303. // Only components that are already on the desktop can be put into kiosk mode!
  304. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  305. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  306. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  307. }
  308. }
  309. }
  310. //==============================================================================
  311. void Desktop::setOrientationsEnabled (const int newOrientations)
  312. {
  313. if (allowedOrientations != newOrientations)
  314. {
  315. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  316. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  317. allowedOrientations = newOrientations;
  318. allowedOrientationsChanged();
  319. }
  320. }
  321. int Desktop::getOrientationsEnabled() const noexcept
  322. {
  323. return allowedOrientations;
  324. }
  325. bool Desktop::isOrientationEnabled (const DisplayOrientation orientation) const noexcept
  326. {
  327. // Make sure you only pass one valid flag in here...
  328. jassert (orientation == upright || orientation == upsideDown
  329. || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
  330. return (allowedOrientations & orientation) != 0;
  331. }
  332. void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
  333. {
  334. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  335. if (masterScaleFactor != newScaleFactor)
  336. {
  337. masterScaleFactor = newScaleFactor;
  338. displays->refresh();
  339. }
  340. }