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.

416 lines
17KB

  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. //==============================================================================
  21. /**
  22. Classes can implement this interface and register themselves with the Desktop class
  23. to receive callbacks when the currently focused component changes.
  24. @see Desktop::addFocusChangeListener, Desktop::removeFocusChangeListener
  25. @tags{GUI}
  26. */
  27. class JUCE_API FocusChangeListener
  28. {
  29. public:
  30. /** Destructor. */
  31. virtual ~FocusChangeListener() = default;
  32. /** Callback to indicate that the currently focused component has changed. */
  33. virtual void globalFocusChanged (Component* focusedComponent) = 0;
  34. };
  35. //==============================================================================
  36. /**
  37. Describes and controls aspects of the computer's desktop.
  38. @tags{GUI}
  39. */
  40. class JUCE_API Desktop : private DeletedAtShutdown,
  41. private Timer,
  42. private AsyncUpdater
  43. {
  44. public:
  45. //==============================================================================
  46. /** There's only one desktop object, and this method will return it. */
  47. static Desktop& JUCE_CALLTYPE getInstance();
  48. //==============================================================================
  49. /** Returns the mouse position.
  50. The coordinates are relative to the top-left of the main monitor.
  51. Note that this is just a shortcut for calling getMainMouseSource().getScreenPosition(), and
  52. you should only resort to grabbing the global mouse position if there's really no
  53. way to get the coordinates via a mouse event callback instead.
  54. */
  55. static Point<int> getMousePosition();
  56. /** Makes the mouse pointer jump to a given location.
  57. The coordinates are relative to the top-left of the main monitor.
  58. Note that this is a pretty old method, kept around mainly for backwards-compatibility,
  59. and you should use the MouseInputSource class directly in new code.
  60. */
  61. static void setMousePosition (Point<int> newPosition);
  62. /** Returns the last position at which a mouse button was pressed.
  63. Note that this is just a shortcut for calling getMainMouseSource().getLastMouseDownPosition(),
  64. and in a multi-touch environment, it doesn't make much sense. ALWAYS prefer to
  65. get this information via other means, such as MouseEvent::getMouseDownScreenPosition()
  66. if possible, and only ever call this as a last resort.
  67. */
  68. static Point<int> getLastMouseDownPosition();
  69. /** Returns the number of times the mouse button has been clicked since the app started.
  70. Each mouse-down event increments this number by 1.
  71. @see getMouseWheelMoveCounter
  72. */
  73. int getMouseButtonClickCounter() const noexcept;
  74. /** Returns the number of times the mouse wheel has been moved since the app started.
  75. Each mouse-wheel event increments this number by 1.
  76. @see getMouseButtonClickCounter
  77. */
  78. int getMouseWheelMoveCounter() const noexcept;
  79. //==============================================================================
  80. /** This lets you prevent the screensaver from becoming active.
  81. Handy if you're running some sort of presentation app where having a screensaver
  82. appear would be annoying.
  83. Pass false to disable the screensaver, and true to re-enable it. (Note that this
  84. won't enable a screensaver unless the user has actually set one up).
  85. The disablement will only happen while the JUCE application is the foreground
  86. process - if another task is running in front of it, then the screensaver will
  87. be unaffected.
  88. @see isScreenSaverEnabled
  89. */
  90. static void setScreenSaverEnabled (bool isEnabled);
  91. /** Returns true if the screensaver has not been turned off.
  92. This will return the last value passed into setScreenSaverEnabled(). Note that
  93. it won't tell you whether the user is actually using a screen saver, just
  94. whether this app is deliberately preventing one from running.
  95. @see setScreenSaverEnabled
  96. */
  97. static bool isScreenSaverEnabled();
  98. //==============================================================================
  99. /** Registers a MouseListener that will receive all mouse events that occur on
  100. any component.
  101. @see removeGlobalMouseListener
  102. */
  103. void addGlobalMouseListener (MouseListener* listener);
  104. /** Unregisters a MouseListener that was added with the addGlobalMouseListener()
  105. method.
  106. @see addGlobalMouseListener
  107. */
  108. void removeGlobalMouseListener (MouseListener* listener);
  109. //==============================================================================
  110. /** Registers a MouseListener that will receive a callback whenever the focused
  111. component changes.
  112. */
  113. void addFocusChangeListener (FocusChangeListener* listener);
  114. /** Unregisters a listener that was added with addFocusChangeListener(). */
  115. void removeFocusChangeListener (FocusChangeListener* listener);
  116. //==============================================================================
  117. /** Takes a component and makes it full-screen, removing the taskbar, dock, etc.
  118. The component must already be on the desktop for this method to work. It will
  119. be resized to completely fill the screen and any extraneous taskbars, menu bars,
  120. etc will be hidden.
  121. To exit kiosk mode, just call setKioskModeComponent (nullptr). When this is called,
  122. the component that's currently being used will be resized back to the size
  123. and position it was in before being put into this mode.
  124. If allowMenusAndBars is true, things like the menu and dock (on mac) are still
  125. allowed to pop up when the mouse moves onto them. If this is false, it'll try
  126. to hide as much on-screen paraphernalia as possible.
  127. */
  128. void setKioskModeComponent (Component* componentToUse,
  129. bool allowMenusAndBars = true);
  130. /** Returns the component that is currently being used in kiosk-mode.
  131. This is the component that was last set by setKioskModeComponent(). If none
  132. has been set, this returns nullptr.
  133. */
  134. Component* getKioskModeComponent() const noexcept { return kioskModeComponent; }
  135. //==============================================================================
  136. /** Returns the number of components that are currently active as top-level
  137. desktop windows.
  138. @see getComponent, Component::addToDesktop
  139. */
  140. int getNumComponents() const noexcept;
  141. /** Returns one of the top-level desktop window components.
  142. The index is from 0 to getNumComponents() - 1. This could return 0 if the
  143. index is out-of-range.
  144. @see getNumComponents, Component::addToDesktop
  145. */
  146. Component* getComponent (int index) const noexcept;
  147. /** Finds the component at a given screen location.
  148. This will drill down into top-level windows to find the child component at
  149. the given position.
  150. Returns nullptr if the coordinates are inside a non-JUCE window.
  151. */
  152. Component* findComponentAt (Point<int> screenPosition) const;
  153. /** The Desktop object has a ComponentAnimator instance which can be used for performing
  154. your animations.
  155. Having a single shared ComponentAnimator object makes it more efficient when multiple
  156. components are being moved around simultaneously. It's also more convenient than having
  157. to manage your own instance of one.
  158. @see ComponentAnimator
  159. */
  160. ComponentAnimator& getAnimator() noexcept { return animator; }
  161. //==============================================================================
  162. /** Returns the current default look-and-feel for components which don't have one
  163. explicitly set.
  164. @see setDefaultLookAndFeel
  165. */
  166. LookAndFeel& getDefaultLookAndFeel() noexcept;
  167. /** Changes the default look-and-feel.
  168. @param newDefaultLookAndFeel the new look-and-feel object to use - if this is
  169. set to nullptr, it will revert to using the system's
  170. default one. The object passed-in must be deleted by the
  171. caller when it's no longer needed.
  172. @see getDefaultLookAndFeel
  173. */
  174. void setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel);
  175. //==============================================================================
  176. /** Provides access to the array of mouse sources, for iteration.
  177. In a traditional single-mouse system, there might be only one MouseInputSource. On a
  178. multi-touch system, there could be one input source per potential finger. The number
  179. of mouse sources returned here may increase dynamically as the program runs.
  180. To find out how many mouse events are currently happening, use getNumDraggingMouseSources().
  181. */
  182. const Array<MouseInputSource>& getMouseSources() const noexcept;
  183. /** Returns the number of MouseInputSource objects the system has at its disposal.
  184. In a traditional single-mouse system, there might be only one MouseInputSource. On a
  185. multi-touch system, there could be one input source per potential finger. The number
  186. of mouse sources returned here may increase dynamically as the program runs.
  187. To find out how many mouse events are currently happening, use getNumDraggingMouseSources().
  188. @see getMouseSource
  189. */
  190. int getNumMouseSources() const noexcept;
  191. /** Returns one of the system's MouseInputSource objects.
  192. The index should be from 0 to getNumMouseSources() - 1. Out-of-range indexes will return
  193. a null pointer.
  194. In a traditional single-mouse system, there might be only one object. On a multi-touch
  195. system, there could be one input source per potential finger.
  196. */
  197. MouseInputSource* getMouseSource (int index) const noexcept;
  198. /** Returns the main mouse input device that the system is using.
  199. @see getNumMouseSources()
  200. */
  201. MouseInputSource getMainMouseSource() const noexcept;
  202. /** Returns the number of mouse-sources that are currently being dragged.
  203. In a traditional single-mouse system, this will be 0 or 1, depending on whether a
  204. JUCE component has the button down on it. In a multi-touch system, this could
  205. be any number from 0 to the number of simultaneous touches that can be detected.
  206. */
  207. int getNumDraggingMouseSources() const noexcept;
  208. /** Returns one of the mouse sources that's currently being dragged.
  209. The index should be between 0 and getNumDraggingMouseSources() - 1. If the index is
  210. out of range, or if no mice or fingers are down, this will return a null pointer.
  211. */
  212. MouseInputSource* getDraggingMouseSource (int index) const noexcept;
  213. /** Ensures that a non-stop stream of mouse-drag events will be sent during the
  214. current mouse-drag operation.
  215. This allows you to make sure that mouseDrag() events are sent continuously, even
  216. when the mouse isn't moving. This can be useful for things like auto-scrolling
  217. components when the mouse is near an edge.
  218. Call this method during a mouseDown() or mouseDrag() callback, specifying the
  219. minimum interval between consecutive mouse drag callbacks. The callbacks
  220. will continue until the mouse is released, and then the interval will be reset,
  221. so you need to make sure it's called every time you begin a drag event.
  222. Passing an interval of 0 or less will cancel the auto-repeat.
  223. @see mouseDrag
  224. */
  225. void beginDragAutoRepeat (int millisecondsBetweenCallbacks);
  226. //==============================================================================
  227. /** In a tablet/mobile device which can be turned around, this is used to indicate the orientation. */
  228. enum DisplayOrientation
  229. {
  230. upright = 1, /**< Indicates that the device is the normal way up. */
  231. upsideDown = 2, /**< Indicates that the device is upside-down. */
  232. rotatedClockwise = 4, /**< Indicates that the device is turned 90 degrees clockwise from its upright position. */
  233. rotatedAntiClockwise = 8, /**< Indicates that the device is turned 90 degrees anti-clockwise from its upright position. */
  234. allOrientations = 1 + 2 + 4 + 8 /**< A combination of all the orientation values */
  235. };
  236. /** In a tablet device which can be turned around, this returns the current orientation. */
  237. DisplayOrientation getCurrentOrientation() const;
  238. /** Sets which orientations the display is allowed to auto-rotate to.
  239. For devices that support rotating desktops, this lets you specify which of the orientations your app can use.
  240. The parameter is a bitwise or-ed combination of the values in DisplayOrientation, and must contain at least one
  241. set bit.
  242. */
  243. void setOrientationsEnabled (int allowedOrientations);
  244. /** Returns the set of orientations the display is allowed to rotate to.
  245. @see setOrientationsEnabled
  246. */
  247. int getOrientationsEnabled() const noexcept;
  248. /** Returns whether the display is allowed to auto-rotate to the given orientation.
  249. Each orientation can be enabled using setOrientationEnabled(). By default, all orientations are allowed.
  250. */
  251. bool isOrientationEnabled (DisplayOrientation orientation) const noexcept;
  252. //==============================================================================
  253. const Displays& getDisplays() const noexcept { return *displays; }
  254. //==============================================================================
  255. /** Sets a global scale factor to be used for all desktop windows.
  256. Setting this will also scale the monitor sizes that are returned by getDisplays().
  257. */
  258. void setGlobalScaleFactor (float newScaleFactor) noexcept;
  259. /** Returns the current global scale factor, as set by setGlobalScaleFactor().
  260. @see setGlobalScaleFactor
  261. */
  262. float getGlobalScaleFactor() const noexcept { return masterScaleFactor; }
  263. //==============================================================================
  264. /** True if the OS supports semitransparent windows */
  265. static bool canUseSemiTransparentWindows() noexcept;
  266. #if JUCE_MAC
  267. /** OSX-specific function to check for the "dark" title-bar and menu mode. */
  268. static bool isOSXDarkModeActive();
  269. #endif
  270. private:
  271. //==============================================================================
  272. static Desktop* instance;
  273. friend class Component;
  274. friend class ComponentPeer;
  275. friend class MouseInputSourceInternal;
  276. friend class DeletedAtShutdown;
  277. friend class TopLevelWindowManager;
  278. friend class Displays;
  279. std::unique_ptr<MouseInputSource::SourceList> mouseSources;
  280. ListenerList<MouseListener> mouseListeners;
  281. ListenerList<FocusChangeListener> focusListeners;
  282. Array<Component*> desktopComponents;
  283. Array<ComponentPeer*> peers;
  284. std::unique_ptr<Displays> displays;
  285. Point<float> lastFakeMouseMove;
  286. void sendMouseMove();
  287. int mouseClickCounter = 0, mouseWheelCounter = 0;
  288. void incrementMouseClickCounter() noexcept;
  289. void incrementMouseWheelCounter() noexcept;
  290. std::unique_ptr<LookAndFeel> defaultLookAndFeel;
  291. WeakReference<LookAndFeel> currentLookAndFeel;
  292. Component* kioskModeComponent = nullptr;
  293. Rectangle<int> kioskComponentOriginalBounds;
  294. bool kioskModeReentrant = false;
  295. int allowedOrientations = allOrientations;
  296. void allowedOrientationsChanged();
  297. float masterScaleFactor;
  298. ComponentAnimator animator;
  299. void timerCallback() override;
  300. void resetTimer();
  301. ListenerList<MouseListener>& getMouseListeners();
  302. void addDesktopComponent (Component*);
  303. void removeDesktopComponent (Component*);
  304. void componentBroughtToFront (Component*);
  305. void setKioskComponent (Component*, bool shouldBeEnabled, bool allowMenusAndBars);
  306. void triggerFocusCallback();
  307. void handleAsyncUpdate() override;
  308. static Point<float> getMousePositionFloat();
  309. static double getDefaultMasterScale();
  310. Desktop();
  311. ~Desktop() override;
  312. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Desktop)
  313. };
  314. } // namespace juce