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.

424 lines
13KB

  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. namespace juce
  20. {
  21. Desktop::Desktop()
  22. : mouseSources (new MouseInputSource::SourceList()),
  23. masterScaleFactor ((float) getDefaultMasterScale())
  24. {
  25. displays.reset (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 (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. auto* c = desktopComponents.getUnchecked(i);
  59. if (c->isVisible())
  60. {
  61. auto 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.reset (new LookAndFeel_V4());
  75. currentLookAndFeel = defaultLookAndFeel.get();
  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 (auto* c = getComponent (i))
  85. c->sendLookAndFeelChange();
  86. }
  87. //==============================================================================
  88. void Desktop::addDesktopComponent (Component* c)
  89. {
  90. jassert (c != nullptr);
  91. jassert (! desktopComponents.contains (c));
  92. desktopComponents.addIfNotAlreadyThere (c);
  93. }
  94. void Desktop::removeDesktopComponent (Component* c)
  95. {
  96. desktopComponents.removeFirstMatchingValue (c);
  97. }
  98. void Desktop::componentBroughtToFront (Component* c)
  99. {
  100. auto 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* l) { focusListeners.add (l); }
  145. void Desktop::removeFocusChangeListener (FocusChangeListener* l) { focusListeners.remove (l); }
  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& l) { l.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* listener)
  169. {
  170. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  171. mouseListeners.add (listener);
  172. resetTimer();
  173. }
  174. void Desktop::removeGlobalMouseListener (MouseListener* 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. auto pos = target->getLocalPoint (nullptr, lastFakeMouseMove);
  195. auto now = Time::getCurrentTime();
  196. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::currentModifiers, 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& l) { l.mouseDrag (me); });
  202. else
  203. mouseListeners.callChecked (checker, [&] (MouseListener& l) { l.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. auto* best = &displays.getReference(0);
  220. double bestDistance = 1.0e10;
  221. for (auto& d : displays)
  222. {
  223. if (d.totalArea.contains (position))
  224. {
  225. best = &d;
  226. break;
  227. }
  228. auto distance = d.totalArea.getCentre().getDistanceFrom (position);
  229. if (distance < bestDistance)
  230. {
  231. bestDistance = distance;
  232. best = &d;
  233. }
  234. }
  235. return *best;
  236. }
  237. RectangleList<int> Desktop::Displays::getRectangleList (bool userAreasOnly) const
  238. {
  239. ASSERT_MESSAGE_MANAGER_IS_LOCKED
  240. RectangleList<int> rl;
  241. for (auto& d : displays)
  242. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  243. return rl;
  244. }
  245. Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
  246. {
  247. return getRectangleList (userAreasOnly).getBounds();
  248. }
  249. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  250. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  251. {
  252. return d1.userArea == d2.userArea
  253. && d1.totalArea == d2.totalArea
  254. && d1.scale == d2.scale
  255. && d1.isMain == d2.isMain;
  256. }
  257. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  258. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  259. {
  260. return ! (d1 == d2);
  261. }
  262. void Desktop::Displays::init (Desktop& desktop)
  263. {
  264. findDisplays (desktop.getGlobalScaleFactor());
  265. }
  266. void Desktop::Displays::refresh()
  267. {
  268. Array<Display> oldDisplays;
  269. oldDisplays.swapWith (displays);
  270. init (Desktop::getInstance());
  271. if (oldDisplays != displays)
  272. {
  273. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  274. if (auto* peer = ComponentPeer::getPeer (i))
  275. peer->handleScreenSizeChange();
  276. }
  277. }
  278. //==============================================================================
  279. void Desktop::setKioskModeComponent (Component* componentToUse, bool allowMenusAndBars)
  280. {
  281. if (kioskModeReentrant)
  282. return;
  283. const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
  284. if (kioskModeComponent != componentToUse)
  285. {
  286. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  287. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  288. if (auto* oldKioskComp = kioskModeComponent)
  289. {
  290. kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
  291. setKioskComponent (oldKioskComp, false, allowMenusAndBars);
  292. oldKioskComp->setBounds (kioskComponentOriginalBounds);
  293. }
  294. kioskModeComponent = componentToUse;
  295. if (kioskModeComponent != nullptr)
  296. {
  297. // Only components that are already on the desktop can be put into kiosk mode!
  298. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  299. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  300. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  301. }
  302. }
  303. }
  304. //==============================================================================
  305. void Desktop::setOrientationsEnabled (int newOrientations)
  306. {
  307. if (allowedOrientations != newOrientations)
  308. {
  309. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  310. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  311. allowedOrientations = newOrientations;
  312. allowedOrientationsChanged();
  313. }
  314. }
  315. int Desktop::getOrientationsEnabled() const noexcept
  316. {
  317. return allowedOrientations;
  318. }
  319. bool Desktop::isOrientationEnabled (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. }
  335. } // namespace juce