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.

452 lines
13KB

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