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.

492 lines
14KB

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