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.

454 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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. Desktop::Desktop()
  21. : mouseClickCounter (0),
  22. kioskModeComponent (nullptr),
  23. allowedOrientations (allOrientations)
  24. {
  25. createMouseInputSources();
  26. refreshMonitorSizes();
  27. }
  28. Desktop::~Desktop()
  29. {
  30. setScreenSaverEnabled (true);
  31. jassert (instance == this);
  32. instance = nullptr;
  33. // doh! If you don't delete all your windows before exiting, you're going to
  34. // be leaking memory!
  35. jassert (desktopComponents.size() == 0);
  36. }
  37. Desktop& JUCE_CALLTYPE Desktop::getInstance()
  38. {
  39. if (instance == nullptr)
  40. instance = new Desktop();
  41. return *instance;
  42. }
  43. Desktop* Desktop::instance = nullptr;
  44. //==============================================================================
  45. void Desktop::refreshMonitorSizes()
  46. {
  47. Array <Rectangle<int> > oldClipped, oldUnclipped;
  48. oldClipped.swapWithArray (monitorCoordsClipped);
  49. oldUnclipped.swapWithArray (monitorCoordsUnclipped);
  50. getCurrentMonitorPositions (monitorCoordsClipped, true);
  51. getCurrentMonitorPositions (monitorCoordsUnclipped, false);
  52. jassert (monitorCoordsClipped.size() == monitorCoordsUnclipped.size());
  53. if (oldClipped != monitorCoordsClipped
  54. || oldUnclipped != monitorCoordsUnclipped)
  55. {
  56. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  57. {
  58. ComponentPeer* const p = ComponentPeer::getPeer (i);
  59. if (p != nullptr)
  60. p->handleScreenSizeChange();
  61. }
  62. }
  63. }
  64. int Desktop::getNumDisplayMonitors() const noexcept
  65. {
  66. return monitorCoordsClipped.size();
  67. }
  68. const Rectangle<int> Desktop::getDisplayMonitorCoordinates (const int index, const bool clippedToWorkArea) const noexcept
  69. {
  70. return clippedToWorkArea ? monitorCoordsClipped [index]
  71. : monitorCoordsUnclipped [index];
  72. }
  73. const RectangleList Desktop::getAllMonitorDisplayAreas (const bool clippedToWorkArea) const
  74. {
  75. RectangleList rl;
  76. for (int i = 0; i < getNumDisplayMonitors(); ++i)
  77. rl.addWithoutMerging (getDisplayMonitorCoordinates (i, clippedToWorkArea));
  78. return rl;
  79. }
  80. const Rectangle<int> Desktop::getMainMonitorArea (const bool clippedToWorkArea) const noexcept
  81. {
  82. return getDisplayMonitorCoordinates (0, clippedToWorkArea);
  83. }
  84. const Rectangle<int> Desktop::getMonitorAreaContaining (const Point<int>& position, const bool clippedToWorkArea) const
  85. {
  86. Rectangle<int> best (getMainMonitorArea (clippedToWorkArea));
  87. double bestDistance = 1.0e10;
  88. for (int i = getNumDisplayMonitors(); --i >= 0;)
  89. {
  90. const Rectangle<int> rect (getDisplayMonitorCoordinates (i, clippedToWorkArea));
  91. if (rect.contains (position))
  92. return rect;
  93. const double distance = rect.getCentre().getDistanceFrom (position);
  94. if (distance < bestDistance)
  95. {
  96. bestDistance = distance;
  97. best = rect;
  98. }
  99. }
  100. return best;
  101. }
  102. //==============================================================================
  103. int Desktop::getNumComponents() const noexcept
  104. {
  105. return desktopComponents.size();
  106. }
  107. Component* Desktop::getComponent (const int index) const noexcept
  108. {
  109. return desktopComponents [index];
  110. }
  111. Component* Desktop::findComponentAt (const Point<int>& screenPosition) const
  112. {
  113. for (int i = desktopComponents.size(); --i >= 0;)
  114. {
  115. Component* const c = desktopComponents.getUnchecked(i);
  116. if (c->isVisible())
  117. {
  118. const Point<int> relative (c->getLocalPoint (nullptr, screenPosition));
  119. if (c->contains (relative))
  120. return c->getComponentAt (relative);
  121. }
  122. }
  123. return nullptr;
  124. }
  125. //==============================================================================
  126. LookAndFeel& Desktop::getDefaultLookAndFeel() noexcept
  127. {
  128. if (currentLookAndFeel == nullptr)
  129. {
  130. if (defaultLookAndFeel == nullptr)
  131. defaultLookAndFeel = new LookAndFeel();
  132. currentLookAndFeel = defaultLookAndFeel;
  133. }
  134. return *currentLookAndFeel;
  135. }
  136. void Desktop::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel)
  137. {
  138. currentLookAndFeel = newDefaultLookAndFeel;
  139. for (int i = getNumComponents(); --i >= 0;)
  140. {
  141. Component* const c = getComponent (i);
  142. if (c != nullptr)
  143. c->sendLookAndFeelChange();
  144. }
  145. }
  146. //==============================================================================
  147. void Desktop::addDesktopComponent (Component* const c)
  148. {
  149. jassert (c != nullptr);
  150. jassert (! desktopComponents.contains (c));
  151. desktopComponents.addIfNotAlreadyThere (c);
  152. }
  153. void Desktop::removeDesktopComponent (Component* const c)
  154. {
  155. desktopComponents.removeValue (c);
  156. }
  157. void Desktop::componentBroughtToFront (Component* const c)
  158. {
  159. const int index = desktopComponents.indexOf (c);
  160. jassert (index >= 0);
  161. if (index >= 0)
  162. {
  163. int newIndex = -1;
  164. if (! c->isAlwaysOnTop())
  165. {
  166. newIndex = desktopComponents.size();
  167. while (newIndex > 0 && desktopComponents.getUnchecked (newIndex - 1)->isAlwaysOnTop())
  168. --newIndex;
  169. --newIndex;
  170. }
  171. desktopComponents.move (index, newIndex);
  172. }
  173. }
  174. //==============================================================================
  175. const Point<int> Desktop::getMousePosition()
  176. {
  177. return getInstance().getMainMouseSource().getScreenPosition();
  178. }
  179. const Point<int> Desktop::getLastMouseDownPosition()
  180. {
  181. return getInstance().getMainMouseSource().getLastMouseDownPosition();
  182. }
  183. int Desktop::getMouseButtonClickCounter()
  184. {
  185. return getInstance().mouseClickCounter;
  186. }
  187. void Desktop::incrementMouseClickCounter() noexcept
  188. {
  189. ++mouseClickCounter;
  190. }
  191. int Desktop::getNumDraggingMouseSources() const noexcept
  192. {
  193. int num = 0;
  194. for (int i = mouseSources.size(); --i >= 0;)
  195. if (mouseSources.getUnchecked(i)->isDragging())
  196. ++num;
  197. return num;
  198. }
  199. MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept
  200. {
  201. int num = 0;
  202. for (int i = mouseSources.size(); --i >= 0;)
  203. {
  204. MouseInputSource* const mi = mouseSources.getUnchecked(i);
  205. if (mi->isDragging())
  206. {
  207. if (index == num)
  208. return mi;
  209. ++num;
  210. }
  211. }
  212. return nullptr;
  213. }
  214. //==============================================================================
  215. class MouseDragAutoRepeater : public Timer
  216. {
  217. public:
  218. MouseDragAutoRepeater() {}
  219. void timerCallback()
  220. {
  221. Desktop& desktop = Desktop::getInstance();
  222. int numMiceDown = 0;
  223. for (int i = desktop.getNumMouseSources(); --i >= 0;)
  224. {
  225. MouseInputSource* const source = desktop.getMouseSource(i);
  226. if (source->isDragging())
  227. {
  228. source->triggerFakeMove();
  229. ++numMiceDown;
  230. }
  231. }
  232. if (numMiceDown == 0)
  233. desktop.beginDragAutoRepeat (0);
  234. }
  235. private:
  236. JUCE_DECLARE_NON_COPYABLE (MouseDragAutoRepeater);
  237. };
  238. void Desktop::beginDragAutoRepeat (const int interval)
  239. {
  240. if (interval > 0)
  241. {
  242. if (dragRepeater == nullptr)
  243. dragRepeater = new MouseDragAutoRepeater();
  244. if (dragRepeater->getTimerInterval() != interval)
  245. dragRepeater->startTimer (interval);
  246. }
  247. else
  248. {
  249. dragRepeater = nullptr;
  250. }
  251. }
  252. //==============================================================================
  253. void Desktop::addFocusChangeListener (FocusChangeListener* const listener)
  254. {
  255. focusListeners.add (listener);
  256. }
  257. void Desktop::removeFocusChangeListener (FocusChangeListener* const listener)
  258. {
  259. focusListeners.remove (listener);
  260. }
  261. void Desktop::triggerFocusCallback()
  262. {
  263. triggerAsyncUpdate();
  264. }
  265. void Desktop::handleAsyncUpdate()
  266. {
  267. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  268. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  269. WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
  270. focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
  271. }
  272. //==============================================================================
  273. void Desktop::resetTimer()
  274. {
  275. if (mouseListeners.size() == 0)
  276. stopTimer();
  277. else
  278. startTimer (100);
  279. lastFakeMouseMove = getMousePosition();
  280. }
  281. ListenerList <MouseListener>& Desktop::getMouseListeners()
  282. {
  283. resetTimer();
  284. return mouseListeners;
  285. }
  286. void Desktop::addGlobalMouseListener (MouseListener* const listener)
  287. {
  288. mouseListeners.add (listener);
  289. resetTimer();
  290. }
  291. void Desktop::removeGlobalMouseListener (MouseListener* const listener)
  292. {
  293. mouseListeners.remove (listener);
  294. resetTimer();
  295. }
  296. void Desktop::timerCallback()
  297. {
  298. if (lastFakeMouseMove != getMousePosition())
  299. sendMouseMove();
  300. }
  301. void Desktop::sendMouseMove()
  302. {
  303. if (! mouseListeners.isEmpty())
  304. {
  305. startTimer (20);
  306. lastFakeMouseMove = getMousePosition();
  307. Component* const target = findComponentAt (lastFakeMouseMove);
  308. if (target != nullptr)
  309. {
  310. Component::BailOutChecker checker (target);
  311. const Point<int> pos (target->getLocalPoint (nullptr, lastFakeMouseMove));
  312. const Time now (Time::getCurrentTime());
  313. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(),
  314. target, target, now, pos, now, 0, false);
  315. if (me.mods.isAnyMouseButtonDown())
  316. mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
  317. else
  318. mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
  319. }
  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. }
  357. END_JUCE_NAMESPACE