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.

421 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 (auto* 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(), MouseInputSource::invalidPressure,
  197. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  198. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  199. target, target, now, pos, now, 0, false);
  200. if (me.mods.isAnyMouseButtonDown())
  201. mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
  202. else
  203. mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
  204. }
  205. }
  206. }
  207. //==============================================================================
  208. Desktop::Displays::Displays (Desktop& desktop) { init (desktop); }
  209. Desktop::Displays::~Displays() {}
  210. const Desktop::Displays::Display& Desktop::Displays::getMainDisplay() const noexcept
  211. {
  212. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  213. jassert (displays.getReference(0).isMain);
  214. return displays.getReference(0);
  215. }
  216. const Desktop::Displays::Display& Desktop::Displays::getDisplayContaining (Point<int> position) const noexcept
  217. {
  218. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  219. const Display* best = &displays.getReference(0);
  220. double bestDistance = 1.0e10;
  221. for (int i = displays.size(); --i >= 0;)
  222. {
  223. const Display& d = displays.getReference(i);
  224. if (d.totalArea.contains (position))
  225. {
  226. best = &d;
  227. break;
  228. }
  229. const double distance = d.totalArea.getCentre().getDistanceFrom (position);
  230. if (distance < bestDistance)
  231. {
  232. bestDistance = distance;
  233. best = &d;
  234. }
  235. }
  236. return *best;
  237. }
  238. RectangleList<int> Desktop::Displays::getRectangleList (bool userAreasOnly) const
  239. {
  240. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  241. RectangleList<int> rl;
  242. for (int i = 0; i < displays.size(); ++i)
  243. {
  244. const Display& d = displays.getReference(i);
  245. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  246. }
  247. return rl;
  248. }
  249. Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
  250. {
  251. return getRectangleList (userAreasOnly).getBounds();
  252. }
  253. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  254. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  255. {
  256. return d1.userArea == d2.userArea
  257. && d1.totalArea == d2.totalArea
  258. && d1.scale == d2.scale
  259. && d1.isMain == d2.isMain;
  260. }
  261. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  262. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  263. {
  264. return ! (d1 == d2);
  265. }
  266. void Desktop::Displays::init (Desktop& desktop)
  267. {
  268. findDisplays (desktop.getGlobalScaleFactor());
  269. }
  270. void Desktop::Displays::refresh()
  271. {
  272. Array<Display> oldDisplays;
  273. oldDisplays.swapWith (displays);
  274. init (Desktop::getInstance());
  275. if (oldDisplays != displays)
  276. {
  277. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  278. if (ComponentPeer* const peer = ComponentPeer::getPeer (i))
  279. peer->handleScreenSizeChange();
  280. }
  281. }
  282. //==============================================================================
  283. void Desktop::setKioskModeComponent (Component* componentToUse, const bool allowMenusAndBars)
  284. {
  285. if (kioskModeReentrant)
  286. return;
  287. const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
  288. if (kioskModeComponent != componentToUse)
  289. {
  290. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  291. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  292. if (Component* const oldKioskComp = kioskModeComponent)
  293. {
  294. kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
  295. setKioskComponent (oldKioskComp, false, allowMenusAndBars);
  296. oldKioskComp->setBounds (kioskComponentOriginalBounds);
  297. }
  298. kioskModeComponent = componentToUse;
  299. if (kioskModeComponent != nullptr)
  300. {
  301. // Only components that are already on the desktop can be put into kiosk mode!
  302. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  303. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  304. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  305. }
  306. }
  307. }
  308. //==============================================================================
  309. void Desktop::setOrientationsEnabled (const int newOrientations)
  310. {
  311. if (allowedOrientations != newOrientations)
  312. {
  313. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  314. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  315. allowedOrientations = newOrientations;
  316. allowedOrientationsChanged();
  317. }
  318. }
  319. bool Desktop::isOrientationEnabled (const DisplayOrientation orientation) const noexcept
  320. {
  321. // Make sure you only pass one valid flag in here...
  322. jassert (orientation == upright || orientation == upsideDown
  323. || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
  324. return (allowedOrientations & orientation) != 0;
  325. }
  326. void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
  327. {
  328. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  329. if (masterScaleFactor != newScaleFactor)
  330. {
  331. masterScaleFactor = newScaleFactor;
  332. displays->refresh();
  333. }
  334. }