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. 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. Point<int> Desktop::getMousePosition()
  113. {
  114. return getInstance().getMainMouseSource().getScreenPosition();
  115. }
  116. void Desktop::setMousePosition (Point<int> newPosition)
  117. {
  118. getInstance().getMainMouseSource().setScreenPosition (newPosition);
  119. }
  120. Point<int> Desktop::getLastMouseDownPosition()
  121. {
  122. return getInstance().getMainMouseSource().getLastMouseDownPosition();
  123. }
  124. int Desktop::getMouseButtonClickCounter() const noexcept { return mouseClickCounter; }
  125. int Desktop::getMouseWheelMoveCounter() const noexcept { return mouseWheelCounter; }
  126. void Desktop::incrementMouseClickCounter() noexcept { ++mouseClickCounter; }
  127. void Desktop::incrementMouseWheelCounter() noexcept { ++mouseWheelCounter; }
  128. int Desktop::getNumDraggingMouseSources() const noexcept
  129. {
  130. int num = 0;
  131. for (int i = mouseSources.size(); --i >= 0;)
  132. if (mouseSources.getUnchecked(i)->isDragging())
  133. ++num;
  134. return num;
  135. }
  136. MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept
  137. {
  138. int num = 0;
  139. for (int i = mouseSources.size(); --i >= 0;)
  140. {
  141. MouseInputSource* const mi = mouseSources.getUnchecked(i);
  142. if (mi->isDragging())
  143. {
  144. if (index == num)
  145. return mi;
  146. ++num;
  147. }
  148. }
  149. return nullptr;
  150. }
  151. MouseInputSource* Desktop::getOrCreateMouseInputSource (int touchIndex)
  152. {
  153. jassert (touchIndex >= 0 && touchIndex < 100); // sanity-check on number of fingers
  154. for (;;)
  155. {
  156. if (MouseInputSource* mouse = getMouseSource (touchIndex))
  157. return mouse;
  158. if (! addMouseInputSource())
  159. {
  160. jassertfalse; // not enough mouse sources!
  161. return nullptr;
  162. }
  163. }
  164. }
  165. //==============================================================================
  166. class MouseDragAutoRepeater : public Timer
  167. {
  168. public:
  169. MouseDragAutoRepeater() {}
  170. void timerCallback() override
  171. {
  172. Desktop& desktop = Desktop::getInstance();
  173. int numMiceDown = 0;
  174. for (int i = desktop.getNumMouseSources(); --i >= 0;)
  175. {
  176. MouseInputSource& source = *desktop.getMouseSource(i);
  177. if (source.isDragging())
  178. {
  179. source.triggerFakeMove();
  180. ++numMiceDown;
  181. }
  182. }
  183. if (numMiceDown == 0)
  184. desktop.beginDragAutoRepeat (0);
  185. }
  186. private:
  187. JUCE_DECLARE_NON_COPYABLE (MouseDragAutoRepeater)
  188. };
  189. void Desktop::beginDragAutoRepeat (const int interval)
  190. {
  191. if (interval > 0)
  192. {
  193. if (dragRepeater == nullptr)
  194. dragRepeater = new MouseDragAutoRepeater();
  195. if (dragRepeater->getTimerInterval() != interval)
  196. dragRepeater->startTimer (interval);
  197. }
  198. else
  199. {
  200. dragRepeater = nullptr;
  201. }
  202. }
  203. //==============================================================================
  204. void Desktop::addFocusChangeListener (FocusChangeListener* const listener)
  205. {
  206. focusListeners.add (listener);
  207. }
  208. void Desktop::removeFocusChangeListener (FocusChangeListener* const listener)
  209. {
  210. focusListeners.remove (listener);
  211. }
  212. void Desktop::triggerFocusCallback()
  213. {
  214. triggerAsyncUpdate();
  215. }
  216. void Desktop::handleAsyncUpdate()
  217. {
  218. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  219. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  220. WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
  221. focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
  222. }
  223. //==============================================================================
  224. void Desktop::resetTimer()
  225. {
  226. if (mouseListeners.size() == 0)
  227. stopTimer();
  228. else
  229. startTimer (100);
  230. lastFakeMouseMove = getMousePosition();
  231. }
  232. ListenerList <MouseListener>& Desktop::getMouseListeners()
  233. {
  234. resetTimer();
  235. return mouseListeners;
  236. }
  237. void Desktop::addGlobalMouseListener (MouseListener* const listener)
  238. {
  239. mouseListeners.add (listener);
  240. resetTimer();
  241. }
  242. void Desktop::removeGlobalMouseListener (MouseListener* const listener)
  243. {
  244. mouseListeners.remove (listener);
  245. resetTimer();
  246. }
  247. void Desktop::timerCallback()
  248. {
  249. if (lastFakeMouseMove != getMousePosition())
  250. sendMouseMove();
  251. }
  252. void Desktop::sendMouseMove()
  253. {
  254. if (! mouseListeners.isEmpty())
  255. {
  256. startTimer (20);
  257. lastFakeMouseMove = getMousePosition();
  258. if (Component* const target = findComponentAt (lastFakeMouseMove))
  259. {
  260. Component::BailOutChecker checker (target);
  261. const Point<int> pos (target->getLocalPoint (nullptr, lastFakeMouseMove));
  262. const Time now (Time::getCurrentTime());
  263. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(),
  264. target, target, now, pos, now, 0, false);
  265. if (me.mods.isAnyMouseButtonDown())
  266. mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
  267. else
  268. mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
  269. }
  270. }
  271. }
  272. //==============================================================================
  273. Desktop::Displays::Displays (Desktop& desktop) { init (desktop); }
  274. Desktop::Displays::~Displays() {}
  275. const Desktop::Displays::Display& Desktop::Displays::getMainDisplay() const noexcept
  276. {
  277. jassert (displays.getReference(0).isMain);
  278. return displays.getReference(0);
  279. }
  280. const Desktop::Displays::Display& Desktop::Displays::getDisplayContaining (Point<int> position) const noexcept
  281. {
  282. const Display* best = &displays.getReference(0);
  283. double bestDistance = 1.0e10;
  284. for (int i = displays.size(); --i >= 0;)
  285. {
  286. const Display& d = displays.getReference(i);
  287. if (d.totalArea.contains (position))
  288. {
  289. best = &d;
  290. break;
  291. }
  292. const double distance = d.totalArea.getCentre().getDistanceFrom (position);
  293. if (distance < bestDistance)
  294. {
  295. bestDistance = distance;
  296. best = &d;
  297. }
  298. }
  299. return *best;
  300. }
  301. RectangleList<int> Desktop::Displays::getRectangleList (bool userAreasOnly) const
  302. {
  303. RectangleList<int> rl;
  304. for (int i = 0; i < displays.size(); ++i)
  305. {
  306. const Display& d = displays.getReference(i);
  307. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  308. }
  309. return rl;
  310. }
  311. Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
  312. {
  313. return getRectangleList (userAreasOnly).getBounds();
  314. }
  315. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  316. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  317. {
  318. return d1.userArea == d2.userArea
  319. && d1.totalArea == d2.totalArea
  320. && d1.scale == d2.scale
  321. && d1.isMain == d2.isMain;
  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 == d2);
  327. }
  328. void Desktop::Displays::init (Desktop& desktop)
  329. {
  330. findDisplays (desktop.masterScaleFactor);
  331. jassert (displays.size() > 0);
  332. }
  333. void Desktop::Displays::refresh()
  334. {
  335. Array<Display> oldDisplays;
  336. oldDisplays.swapWith (displays);
  337. init (Desktop::getInstance());
  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. }
  380. void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
  381. {
  382. if (masterScaleFactor != newScaleFactor)
  383. {
  384. masterScaleFactor = newScaleFactor;
  385. displays->refresh();
  386. }
  387. }