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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. Desktop::Desktop()
  21. : mouseSources (new MouseInputSource::SourceList()),
  22. masterScaleFactor ((float) getDefaultMasterScale())
  23. {
  24. displays.reset (new Displays (*this));
  25. }
  26. Desktop::~Desktop()
  27. {
  28. setScreenSaverEnabled (true);
  29. animator.cancelAllAnimations (false);
  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 (int index) const noexcept
  49. {
  50. return desktopComponents [index];
  51. }
  52. Component* Desktop::findComponentAt (Point<int> screenPosition) const
  53. {
  54. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  55. for (int i = desktopComponents.size(); --i >= 0;)
  56. {
  57. auto* c = desktopComponents.getUnchecked(i);
  58. if (c->isVisible())
  59. {
  60. auto relative = c->getLocalPoint (nullptr, screenPosition);
  61. if (c->contains (relative))
  62. return c->getComponentAt (relative);
  63. }
  64. }
  65. return nullptr;
  66. }
  67. //==============================================================================
  68. LookAndFeel& Desktop::getDefaultLookAndFeel() noexcept
  69. {
  70. if (auto lf = currentLookAndFeel.get())
  71. return *lf;
  72. if (defaultLookAndFeel == nullptr)
  73. defaultLookAndFeel.reset (new LookAndFeel_V4());
  74. auto lf = defaultLookAndFeel.get();
  75. jassert (lf != nullptr);
  76. currentLookAndFeel = lf;
  77. return *lf;
  78. }
  79. void Desktop::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel)
  80. {
  81. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  82. currentLookAndFeel = newDefaultLookAndFeel;
  83. for (int i = getNumComponents(); --i >= 0;)
  84. if (auto* c = getComponent (i))
  85. c->sendLookAndFeelChange();
  86. }
  87. //==============================================================================
  88. void Desktop::addDesktopComponent (Component* c)
  89. {
  90. jassert (c != nullptr);
  91. jassert (! desktopComponents.contains (c));
  92. desktopComponents.addIfNotAlreadyThere (c);
  93. }
  94. void Desktop::removeDesktopComponent (Component* c)
  95. {
  96. desktopComponents.removeFirstMatchingValue (c);
  97. }
  98. void Desktop::componentBroughtToFront (Component* c)
  99. {
  100. auto index = desktopComponents.indexOf (c);
  101. jassert (index >= 0);
  102. if (index >= 0)
  103. {
  104. int newIndex = -1;
  105. if (! c->isAlwaysOnTop())
  106. {
  107. newIndex = desktopComponents.size();
  108. while (newIndex > 0 && desktopComponents.getUnchecked (newIndex - 1)->isAlwaysOnTop())
  109. --newIndex;
  110. --newIndex;
  111. }
  112. desktopComponents.move (index, newIndex);
  113. }
  114. }
  115. //==============================================================================
  116. Point<int> Desktop::getMousePosition()
  117. {
  118. return getMousePositionFloat().roundToInt();
  119. }
  120. Point<float> Desktop::getMousePositionFloat()
  121. {
  122. return getInstance().getMainMouseSource().getScreenPosition();
  123. }
  124. void Desktop::setMousePosition (Point<int> newPosition)
  125. {
  126. getInstance().getMainMouseSource().setScreenPosition (newPosition.toFloat());
  127. }
  128. Point<int> Desktop::getLastMouseDownPosition()
  129. {
  130. return getInstance().getMainMouseSource().getLastMouseDownPosition().roundToInt();
  131. }
  132. int Desktop::getMouseButtonClickCounter() const noexcept { return mouseClickCounter; }
  133. int Desktop::getMouseWheelMoveCounter() const noexcept { return mouseWheelCounter; }
  134. void Desktop::incrementMouseClickCounter() noexcept { ++mouseClickCounter; }
  135. void Desktop::incrementMouseWheelCounter() noexcept { ++mouseWheelCounter; }
  136. const Array<MouseInputSource>& Desktop::getMouseSources() const noexcept { return mouseSources->sourceArray; }
  137. int Desktop::getNumMouseSources() const noexcept { return mouseSources->sources.size(); }
  138. int Desktop::getNumDraggingMouseSources() const noexcept { return mouseSources->getNumDraggingMouseSources(); }
  139. MouseInputSource* Desktop::getMouseSource (int index) const noexcept { return mouseSources->getMouseSource (index); }
  140. MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept { return mouseSources->getDraggingMouseSource (index); }
  141. MouseInputSource Desktop::getMainMouseSource() const noexcept { return MouseInputSource (mouseSources->sources.getUnchecked(0)); }
  142. void Desktop::beginDragAutoRepeat (int interval) { mouseSources->beginDragAutoRepeat (interval); }
  143. //==============================================================================
  144. void Desktop::addFocusChangeListener (FocusChangeListener* l) { focusListeners.add (l); }
  145. void Desktop::removeFocusChangeListener (FocusChangeListener* l) { focusListeners.remove (l); }
  146. void Desktop::triggerFocusCallback() { triggerAsyncUpdate(); }
  147. void Desktop::handleAsyncUpdate()
  148. {
  149. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  150. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  151. WeakReference<Component> currentFocus (Component::getCurrentlyFocusedComponent());
  152. focusListeners.call ([&] (FocusChangeListener& l) { l.globalFocusChanged (currentFocus.get()); });
  153. }
  154. //==============================================================================
  155. void Desktop::resetTimer()
  156. {
  157. if (mouseListeners.size() == 0)
  158. stopTimer();
  159. else
  160. startTimer (100);
  161. lastFakeMouseMove = getMousePositionFloat();
  162. }
  163. ListenerList<MouseListener>& Desktop::getMouseListeners()
  164. {
  165. resetTimer();
  166. return mouseListeners;
  167. }
  168. void Desktop::addGlobalMouseListener (MouseListener* listener)
  169. {
  170. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  171. mouseListeners.add (listener);
  172. resetTimer();
  173. }
  174. void Desktop::removeGlobalMouseListener (MouseListener* listener)
  175. {
  176. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  177. mouseListeners.remove (listener);
  178. resetTimer();
  179. }
  180. void Desktop::timerCallback()
  181. {
  182. if (lastFakeMouseMove != getMousePositionFloat())
  183. sendMouseMove();
  184. }
  185. void Desktop::sendMouseMove()
  186. {
  187. if (! mouseListeners.isEmpty())
  188. {
  189. startTimer (20);
  190. lastFakeMouseMove = getMousePositionFloat();
  191. if (auto* target = findComponentAt (lastFakeMouseMove.roundToInt()))
  192. {
  193. Component::BailOutChecker checker (target);
  194. auto pos = target->getLocalPoint (nullptr, lastFakeMouseMove);
  195. auto now = Time::getCurrentTime();
  196. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::currentModifiers, MouseInputSource::invalidPressure,
  197. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  198. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  199. target, target, now, pos, now, 0, false);
  200. if (me.mods.isAnyMouseButtonDown())
  201. mouseListeners.callChecked (checker, [&] (MouseListener& l) { l.mouseDrag (me); });
  202. else
  203. mouseListeners.callChecked (checker, [&] (MouseListener& l) { l.mouseMove (me); });
  204. }
  205. }
  206. }
  207. //==============================================================================
  208. void Desktop::setKioskModeComponent (Component* componentToUse, bool allowMenusAndBars)
  209. {
  210. if (kioskModeReentrant)
  211. return;
  212. const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
  213. if (kioskModeComponent != componentToUse)
  214. {
  215. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  216. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  217. if (auto* oldKioskComp = kioskModeComponent)
  218. {
  219. kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
  220. setKioskComponent (oldKioskComp, false, allowMenusAndBars);
  221. oldKioskComp->setBounds (kioskComponentOriginalBounds);
  222. }
  223. kioskModeComponent = componentToUse;
  224. if (kioskModeComponent != nullptr)
  225. {
  226. // Only components that are already on the desktop can be put into kiosk mode!
  227. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  228. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  229. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  230. }
  231. }
  232. }
  233. //==============================================================================
  234. void Desktop::setOrientationsEnabled (int newOrientations)
  235. {
  236. if (allowedOrientations != newOrientations)
  237. {
  238. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  239. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  240. allowedOrientations = newOrientations;
  241. allowedOrientationsChanged();
  242. }
  243. }
  244. int Desktop::getOrientationsEnabled() const noexcept
  245. {
  246. return allowedOrientations;
  247. }
  248. bool Desktop::isOrientationEnabled (DisplayOrientation orientation) const noexcept
  249. {
  250. // Make sure you only pass one valid flag in here...
  251. jassert (orientation == upright || orientation == upsideDown
  252. || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
  253. return (allowedOrientations & orientation) != 0;
  254. }
  255. void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
  256. {
  257. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  258. if (masterScaleFactor != newScaleFactor)
  259. {
  260. masterScaleFactor = newScaleFactor;
  261. displays->refresh();
  262. }
  263. }
  264. } // namespace juce