The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

464 lines
19KB

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