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.

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