Audio plugin host https://kx.studio/carla
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.

juce_Desktop.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. : mouseSources (new MouseInputSource::SourceList()),
  19. mouseClickCounter (0), mouseWheelCounter (0),
  20. kioskModeComponent (nullptr),
  21. kioskModeReentrant (false),
  22. allowedOrientations (allOrientations),
  23. masterScaleFactor ((float) getDefaultMasterScale())
  24. {
  25. displays = new Displays (*this);
  26. }
  27. Desktop::~Desktop()
  28. {
  29. setScreenSaverEnabled (true);
  30. jassert (instance == this);
  31. instance = nullptr;
  32. // doh! If you don't delete all your windows before exiting, you're going to
  33. // be leaking memory!
  34. jassert (desktopComponents.size() == 0);
  35. }
  36. Desktop& JUCE_CALLTYPE Desktop::getInstance()
  37. {
  38. if (instance == nullptr)
  39. instance = new Desktop();
  40. return *instance;
  41. }
  42. Desktop* Desktop::instance = nullptr;
  43. //==============================================================================
  44. int Desktop::getNumComponents() const noexcept
  45. {
  46. return desktopComponents.size();
  47. }
  48. Component* Desktop::getComponent (const int index) const noexcept
  49. {
  50. return desktopComponents [index];
  51. }
  52. Component* Desktop::findComponentAt (Point<int> screenPosition) const
  53. {
  54. for (int i = desktopComponents.size(); --i >= 0;)
  55. {
  56. Component* const c = desktopComponents.getUnchecked(i);
  57. if (c->isVisible())
  58. {
  59. const Point<int> relative (c->getLocalPoint (nullptr, screenPosition));
  60. if (c->contains (relative))
  61. return c->getComponentAt (relative);
  62. }
  63. }
  64. return nullptr;
  65. }
  66. //==============================================================================
  67. LookAndFeel& Desktop::getDefaultLookAndFeel() noexcept
  68. {
  69. if (currentLookAndFeel == nullptr)
  70. {
  71. if (defaultLookAndFeel == nullptr)
  72. defaultLookAndFeel = new LookAndFeel_V2();
  73. currentLookAndFeel = defaultLookAndFeel;
  74. }
  75. return *currentLookAndFeel;
  76. }
  77. void Desktop::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel)
  78. {
  79. currentLookAndFeel = newDefaultLookAndFeel;
  80. for (int i = getNumComponents(); --i >= 0;)
  81. if (Component* const c = getComponent (i))
  82. c->sendLookAndFeelChange();
  83. }
  84. //==============================================================================
  85. void Desktop::addDesktopComponent (Component* const c)
  86. {
  87. jassert (c != nullptr);
  88. jassert (! desktopComponents.contains (c));
  89. desktopComponents.addIfNotAlreadyThere (c);
  90. }
  91. void Desktop::removeDesktopComponent (Component* const c)
  92. {
  93. desktopComponents.removeFirstMatchingValue (c);
  94. }
  95. void Desktop::componentBroughtToFront (Component* const c)
  96. {
  97. const int index = desktopComponents.indexOf (c);
  98. jassert (index >= 0);
  99. if (index >= 0)
  100. {
  101. int newIndex = -1;
  102. if (! c->isAlwaysOnTop())
  103. {
  104. newIndex = desktopComponents.size();
  105. while (newIndex > 0 && desktopComponents.getUnchecked (newIndex - 1)->isAlwaysOnTop())
  106. --newIndex;
  107. --newIndex;
  108. }
  109. desktopComponents.move (index, newIndex);
  110. }
  111. }
  112. //==============================================================================
  113. Point<int> Desktop::getMousePosition()
  114. {
  115. return getInstance().getMainMouseSource().getScreenPosition();
  116. }
  117. void Desktop::setMousePosition (Point<int> newPosition)
  118. {
  119. getInstance().getMainMouseSource().setScreenPosition (newPosition);
  120. }
  121. Point<int> Desktop::getLastMouseDownPosition()
  122. {
  123. return getInstance().getMainMouseSource().getLastMouseDownPosition();
  124. }
  125. int Desktop::getMouseButtonClickCounter() const noexcept { return mouseClickCounter; }
  126. int Desktop::getMouseWheelMoveCounter() const noexcept { return mouseWheelCounter; }
  127. void Desktop::incrementMouseClickCounter() noexcept { ++mouseClickCounter; }
  128. void Desktop::incrementMouseWheelCounter() noexcept { ++mouseWheelCounter; }
  129. const Array<MouseInputSource>& Desktop::getMouseSources() const noexcept { return mouseSources->sourceArray; }
  130. int Desktop::getNumMouseSources() const noexcept { return mouseSources->sources.size(); }
  131. int Desktop::getNumDraggingMouseSources() const noexcept { return mouseSources->getNumDraggingMouseSources(); }
  132. MouseInputSource* Desktop::getMouseSource (int index) const noexcept { return mouseSources->getMouseSource (index); }
  133. MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept { return mouseSources->getDraggingMouseSource (index); }
  134. MouseInputSource Desktop::getMainMouseSource() const noexcept { return MouseInputSource (mouseSources->sources.getUnchecked(0)); }
  135. void Desktop::beginDragAutoRepeat (int interval) { mouseSources->beginDragAutoRepeat (interval); }
  136. //==============================================================================
  137. void Desktop::addFocusChangeListener (FocusChangeListener* const listener) { focusListeners.add (listener); }
  138. void Desktop::removeFocusChangeListener (FocusChangeListener* const listener) { focusListeners.remove (listener); }
  139. void Desktop::triggerFocusCallback() { triggerAsyncUpdate(); }
  140. void Desktop::handleAsyncUpdate()
  141. {
  142. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  143. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  144. WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
  145. focusListeners.call (&FocusChangeListener::globalFocusChanged, currentFocus);
  146. }
  147. //==============================================================================
  148. void Desktop::resetTimer()
  149. {
  150. if (mouseListeners.size() == 0)
  151. stopTimer();
  152. else
  153. startTimer (100);
  154. lastFakeMouseMove = getMousePosition();
  155. }
  156. ListenerList <MouseListener>& Desktop::getMouseListeners()
  157. {
  158. resetTimer();
  159. return mouseListeners;
  160. }
  161. void Desktop::addGlobalMouseListener (MouseListener* const listener)
  162. {
  163. mouseListeners.add (listener);
  164. resetTimer();
  165. }
  166. void Desktop::removeGlobalMouseListener (MouseListener* const listener)
  167. {
  168. mouseListeners.remove (listener);
  169. resetTimer();
  170. }
  171. void Desktop::timerCallback()
  172. {
  173. if (lastFakeMouseMove != getMousePosition())
  174. sendMouseMove();
  175. }
  176. void Desktop::sendMouseMove()
  177. {
  178. if (! mouseListeners.isEmpty())
  179. {
  180. startTimer (20);
  181. lastFakeMouseMove = getMousePosition();
  182. if (Component* const target = findComponentAt (lastFakeMouseMove))
  183. {
  184. Component::BailOutChecker checker (target);
  185. const Point<int> pos (target->getLocalPoint (nullptr, lastFakeMouseMove));
  186. const Time now (Time::getCurrentTime());
  187. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::getCurrentModifiers(),
  188. target, target, now, pos, now, 0, false);
  189. if (me.mods.isAnyMouseButtonDown())
  190. mouseListeners.callChecked (checker, &MouseListener::mouseDrag, me);
  191. else
  192. mouseListeners.callChecked (checker, &MouseListener::mouseMove, me);
  193. }
  194. }
  195. }
  196. //==============================================================================
  197. Desktop::Displays::Displays (Desktop& desktop) { init (desktop); }
  198. Desktop::Displays::~Displays() {}
  199. const Desktop::Displays::Display& Desktop::Displays::getMainDisplay() const noexcept
  200. {
  201. jassert (displays.getReference(0).isMain);
  202. return displays.getReference(0);
  203. }
  204. const Desktop::Displays::Display& Desktop::Displays::getDisplayContaining (Point<int> position) const noexcept
  205. {
  206. const Display* best = &displays.getReference(0);
  207. double bestDistance = 1.0e10;
  208. for (int i = displays.size(); --i >= 0;)
  209. {
  210. const Display& d = displays.getReference(i);
  211. if (d.totalArea.contains (position))
  212. {
  213. best = &d;
  214. break;
  215. }
  216. const double distance = d.totalArea.getCentre().getDistanceFrom (position);
  217. if (distance < bestDistance)
  218. {
  219. bestDistance = distance;
  220. best = &d;
  221. }
  222. }
  223. return *best;
  224. }
  225. RectangleList<int> Desktop::Displays::getRectangleList (bool userAreasOnly) const
  226. {
  227. RectangleList<int> rl;
  228. for (int i = 0; i < displays.size(); ++i)
  229. {
  230. const Display& d = displays.getReference(i);
  231. rl.addWithoutMerging (userAreasOnly ? d.userArea : d.totalArea);
  232. }
  233. return rl;
  234. }
  235. Rectangle<int> Desktop::Displays::getTotalBounds (bool userAreasOnly) const
  236. {
  237. return getRectangleList (userAreasOnly).getBounds();
  238. }
  239. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  240. bool operator== (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  241. {
  242. return d1.userArea == d2.userArea
  243. && d1.totalArea == d2.totalArea
  244. && d1.scale == d2.scale
  245. && d1.isMain == d2.isMain;
  246. }
  247. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept;
  248. bool operator!= (const Desktop::Displays::Display& d1, const Desktop::Displays::Display& d2) noexcept
  249. {
  250. return ! (d1 == d2);
  251. }
  252. void Desktop::Displays::init (Desktop& desktop)
  253. {
  254. findDisplays (desktop.getGlobalScaleFactor());
  255. jassert (displays.size() > 0);
  256. }
  257. void Desktop::Displays::refresh()
  258. {
  259. Array<Display> oldDisplays;
  260. oldDisplays.swapWith (displays);
  261. init (Desktop::getInstance());
  262. if (oldDisplays != displays)
  263. {
  264. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  265. if (ComponentPeer* const peer = ComponentPeer::getPeer (i))
  266. peer->handleScreenSizeChange();
  267. }
  268. }
  269. //==============================================================================
  270. void Desktop::setKioskModeComponent (Component* componentToUse, const bool allowMenusAndBars)
  271. {
  272. if (kioskModeReentrant)
  273. return;
  274. const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
  275. if (kioskModeComponent != componentToUse)
  276. {
  277. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  278. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  279. if (Component* const oldKioskComp = kioskModeComponent)
  280. {
  281. kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
  282. setKioskComponent (oldKioskComp, false, allowMenusAndBars);
  283. oldKioskComp->setBounds (kioskComponentOriginalBounds);
  284. }
  285. kioskModeComponent = componentToUse;
  286. if (kioskModeComponent != nullptr)
  287. {
  288. // Only components that are already on the desktop can be put into kiosk mode!
  289. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  290. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  291. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  292. }
  293. }
  294. }
  295. //==============================================================================
  296. void Desktop::setOrientationsEnabled (const int newOrientations)
  297. {
  298. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  299. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  300. allowedOrientations = newOrientations;
  301. }
  302. bool Desktop::isOrientationEnabled (const DisplayOrientation orientation) const noexcept
  303. {
  304. // Make sure you only pass one valid flag in here...
  305. jassert (orientation == upright || orientation == upsideDown
  306. || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
  307. return (allowedOrientations & orientation) != 0;
  308. }
  309. void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
  310. {
  311. if (masterScaleFactor != newScaleFactor)
  312. {
  313. masterScaleFactor = newScaleFactor;
  314. displays->refresh();
  315. }
  316. }