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

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