juce_Desktop.h 19 KB

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