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.

328 lines
11KB

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