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.

475 lines
20KB

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