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.

480 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. Point<int> Desktop::getLastMouseDownPosition()
  125. {
  126. return getInstance().getMainMouseSource().getLastMouseDownPosition();
  127. }
  128. int Desktop::getMouseButtonClickCounter() const noexcept { return mouseClickCounter; }
  129. int Desktop::getMouseWheelMoveCounter() const noexcept { return mouseWheelCounter; }
  130. void Desktop::incrementMouseClickCounter() noexcept { ++mouseClickCounter; }
  131. void Desktop::incrementMouseWheelCounter() noexcept { ++mouseWheelCounter; }
  132. int Desktop::getNumDraggingMouseSources() const noexcept
  133. {
  134. int num = 0;
  135. for (int i = mouseSources.size(); --i >= 0;)
  136. if (mouseSources.getUnchecked(i)->isDragging())
  137. ++num;
  138. return num;
  139. }
  140. MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept
  141. {
  142. int num = 0;
  143. for (int i = mouseSources.size(); --i >= 0;)
  144. {
  145. MouseInputSource* const mi = mouseSources.getUnchecked(i);
  146. if (mi->isDragging())
  147. {
  148. if (index == num)
  149. return mi;
  150. ++num;
  151. }
  152. }
  153. return nullptr;
  154. }
  155. MouseInputSource* Desktop::getOrCreateMouseInputSource (int touchIndex)
  156. {
  157. jassert (touchIndex >= 0 && touchIndex < 100); // sanity-check on number of fingers
  158. for (;;)
  159. {
  160. if (MouseInputSource* mouse = getMouseSource (touchIndex))
  161. return mouse;
  162. if (! addMouseInputSource())
  163. {
  164. jassertfalse; // not enough mouse sources!
  165. return nullptr;
  166. }
  167. }
  168. }
  169. //==============================================================================
  170. class MouseDragAutoRepeater : public Timer
  171. {
  172. public:
  173. MouseDragAutoRepeater() {}
  174. void timerCallback() override
  175. {
  176. Desktop& desktop = Desktop::getInstance();
  177. int numMiceDown = 0;
  178. for (int i = desktop.getNumMouseSources(); --i >= 0;)
  179. {
  180. MouseInputSource& source = *desktop.getMouseSource(i);
  181. if (source.isDragging())
  182. {
  183. source.triggerFakeMove();
  184. ++numMiceDown;
  185. }
  186. }
  187. if (numMiceDown == 0)
  188. desktop.beginDragAutoRepeat (0);
  189. }
  190. private:
  191. JUCE_DECLARE_NON_COPYABLE (MouseDragAutoRepeater)
  192. };
  193. void Desktop::beginDragAutoRepeat (const int interval)
  194. {
  195. if (interval > 0)
  196. {
  197. if (dragRepeater == nullptr)
  198. dragRepeater = new MouseDragAutoRepeater();
  199. if (dragRepeater->getTimerInterval() != interval)
  200. dragRepeater->startTimer (interval);
  201. }
  202. else
  203. {
  204. dragRepeater = nullptr;
  205. }
  206. }
  207. //==============================================================================
  208. void Desktop::addFocusChangeListener (FocusChangeListener* const listener)
  209. {
  210. focusListeners.add (listener);
  211. }
  212. void Desktop::removeFocusChangeListener (FocusChangeListener* const listener)
  213. {
  214. focusListeners.remove (listener);
  215. }
  216. void Desktop::triggerFocusCallback()
  217. {
  218. triggerAsyncUpdate();
  219. }
  220. void Desktop::handleAsyncUpdate()
  221. {
  222. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  223. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  224. WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
  225. focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
  226. }
  227. //==============================================================================
  228. void Desktop::resetTimer()
  229. {
  230. if (mouseListeners.size() == 0)
  231. stopTimer();
  232. else
  233. startTimer (100);
  234. lastFakeMouseMove = getMousePosition();
  235. }
  236. ListenerList <MouseListener>& Desktop::getMouseListeners()
  237. {
  238. resetTimer();
  239. return mouseListeners;
  240. }
  241. void Desktop::addGlobalMouseListener (MouseListener* const listener)
  242. {
  243. mouseListeners.add (listener);
  244. resetTimer();
  245. }
  246. void Desktop::removeGlobalMouseListener (MouseListener* const listener)
  247. {
  248. mouseListeners.remove (listener);
  249. resetTimer();
  250. }
  251. void Desktop::timerCallback()
  252. {
  253. if (lastFakeMouseMove != getMousePosition())
  254. sendMouseMove();
  255. }
  256. void Desktop::sendMouseMove()
  257. {
  258. if (! mouseListeners.isEmpty())
  259. {
  260. startTimer (20);
  261. lastFakeMouseMove = getMousePosition();
  262. if (Component* const target = findComponentAt (lastFakeMouseMove))
  263. {
  264. Component::BailOutChecker checker (target);
  265. const Point<int> pos (target->getLocalPoint (nullptr, lastFakeMouseMove));
  266. const Time now (Time::getCurrentTime());
  267. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(),
  268. target, target, now, pos, now, 0, false);
  269. if (me.mods.isAnyMouseButtonDown())
  270. mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
  271. else
  272. mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
  273. }
  274. }
  275. }
  276. //==============================================================================
  277. Desktop::Displays::Displays() { refresh(); }
  278. Desktop::Displays::~Displays() {}
  279. const Desktop::Displays::Display& Desktop::Displays::getMainDisplay() const noexcept
  280. {
  281. jassert (displays.getReference(0).isMain);
  282. return displays.getReference(0);
  283. }
  284. const Desktop::Displays::Display& Desktop::Displays::getDisplayContaining (Point<int> position) const noexcept
  285. {
  286. const Display* best = &displays.getReference(0);
  287. double bestDistance = 1.0e10;
  288. for (int i = displays.size(); --i >= 0;)
  289. {
  290. const Display& d = displays.getReference(i);
  291. if (d.totalArea.contains (position))
  292. {
  293. best = &d;
  294. break;
  295. }
  296. const double distance = d.totalArea.getCentre().getDistanceFrom (position);
  297. if (distance < bestDistance)
  298. {
  299. bestDistance = distance;
  300. best = &d;
  301. }
  302. }
  303. return *best;
  304. }
  305. RectangleList Desktop::Displays::getRectangleList (bool userAreasOnly) const
  306. {
  307. RectangleList rl;
  308. for (int i = 0; i < displays.size(); ++i)
  309. {
  310. const Display& d = displays.getReference(i);
  311. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  312. }
  313. return rl;
  314. }
  315. Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
  316. {
  317. return getRectangleList (userAreasOnly).getBounds();
  318. }
  319. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  320. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  321. {
  322. return d1.userArea == d2.userArea
  323. && d1.totalArea == d2.totalArea
  324. && d1.scale == d2.scale
  325. && d1.isMain == d2.isMain;
  326. }
  327. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  328. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  329. {
  330. return ! (d1 == d2);
  331. }
  332. void Desktop::Displays::refresh()
  333. {
  334. Array<Display> oldDisplays;
  335. oldDisplays.swapWithArray (displays);
  336. findDisplays();
  337. jassert (displays.size() > 0);
  338. if (oldDisplays != displays)
  339. {
  340. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  341. if (ComponentPeer* const peer = ComponentPeer::getPeer (i))
  342. peer->handleScreenSizeChange();
  343. }
  344. }
  345. //==============================================================================
  346. void Desktop::setKioskModeComponent (Component* componentToUse, const bool allowMenusAndBars)
  347. {
  348. if (kioskModeComponent != componentToUse)
  349. {
  350. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  351. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  352. if (kioskModeComponent != nullptr)
  353. {
  354. setKioskComponent (kioskModeComponent, false, allowMenusAndBars);
  355. kioskModeComponent->setBounds (kioskComponentOriginalBounds);
  356. }
  357. kioskModeComponent = componentToUse;
  358. if (kioskModeComponent != nullptr)
  359. {
  360. // Only components that are already on the desktop can be put into kiosk mode!
  361. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  362. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  363. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  364. }
  365. }
  366. }
  367. //==============================================================================
  368. void Desktop::setOrientationsEnabled (const int newOrientations)
  369. {
  370. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  371. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  372. allowedOrientations = newOrientations;
  373. }
  374. bool Desktop::isOrientationEnabled (const DisplayOrientation orientation) const noexcept
  375. {
  376. // Make sure you only pass one valid flag in here...
  377. jassert (orientation == upright || orientation == upsideDown || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
  378. return (allowedOrientations & orientation) != 0;
  379. }