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.

451 lines
13KB

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