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.

413 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_DESKTOP_JUCEHEADER__
  19. #define __JUCE_DESKTOP_JUCEHEADER__
  20. #include "juce_Component.h"
  21. #include "../layout/juce_ComponentAnimator.h"
  22. class MouseInputSource;
  23. class MouseInputSourceInternal;
  24. class MouseListener;
  25. //==============================================================================
  26. /**
  27. Classes can implement this interface and register themselves with the Desktop class
  28. to receive callbacks when the currently focused component changes.
  29. @see Desktop::addFocusChangeListener, Desktop::removeFocusChangeListener
  30. */
  31. class JUCE_API FocusChangeListener
  32. {
  33. public:
  34. /** Destructor. */
  35. virtual ~FocusChangeListener() {}
  36. /** Callback to indicate that the currently focused component has changed. */
  37. virtual void globalFocusChanged (Component* focusedComponent) = 0;
  38. };
  39. //==============================================================================
  40. /**
  41. Describes and controls aspects of the computer's desktop.
  42. */
  43. class JUCE_API Desktop : private DeletedAtShutdown,
  44. private Timer,
  45. private AsyncUpdater
  46. {
  47. public:
  48. //==============================================================================
  49. /** There's only one dektop object, and this method will return it.
  50. */
  51. static Desktop& JUCE_CALLTYPE getInstance();
  52. //==============================================================================
  53. /** Returns a list of the positions of all the monitors available.
  54. The first rectangle in the list will be the main monitor area.
  55. If clippedToWorkArea is true, it will exclude any areas like the taskbar on Windows,
  56. or the menu bar on Mac. If clippedToWorkArea is false, the entire monitor area is returned.
  57. */
  58. const RectangleList getAllMonitorDisplayAreas (bool clippedToWorkArea = true) const;
  59. /** Returns the position and size of the main monitor.
  60. If clippedToWorkArea is true, it will exclude any areas like the taskbar on Windows,
  61. or the menu bar on Mac. If clippedToWorkArea is false, the entire monitor area is returned.
  62. */
  63. const Rectangle<int> getMainMonitorArea (bool clippedToWorkArea = true) const noexcept;
  64. /** Returns the position and size of the monitor which contains this co-ordinate.
  65. If none of the monitors contains the point, this will just return the
  66. main monitor.
  67. If clippedToWorkArea is true, it will exclude any areas like the taskbar on Windows,
  68. or the menu bar on Mac. If clippedToWorkArea is false, the entire monitor area is returned.
  69. */
  70. const Rectangle<int> getMonitorAreaContaining (const Point<int>& position, bool clippedToWorkArea = true) const;
  71. //==============================================================================
  72. /** Returns the mouse position.
  73. The co-ordinates are relative to the top-left of the main monitor.
  74. Note that this is just a shortcut for calling getMainMouseSource().getScreenPosition(), and
  75. you should only resort to grabbing the global mouse position if there's really no
  76. way to get the coordinates via a mouse event callback instead.
  77. */
  78. static const Point<int> getMousePosition();
  79. /** Makes the mouse pointer jump to a given location.
  80. The co-ordinates are relative to the top-left of the main monitor.
  81. */
  82. static void setMousePosition (const Point<int>& newPosition);
  83. /** Returns the last position at which a mouse button was pressed.
  84. Note that this is just a shortcut for calling getMainMouseSource().getLastMouseDownPosition(),
  85. and in a multi-touch environment, it doesn't make much sense. ALWAYS prefer to
  86. get this information via other means, such as MouseEvent::getMouseDownScreenPosition()
  87. if possible, and only ever call this as a last resort.
  88. */
  89. static const Point<int> getLastMouseDownPosition();
  90. /** Returns the number of times the mouse button has been clicked since the
  91. app started.
  92. Each mouse-down event increments this number by 1.
  93. */
  94. static int getMouseButtonClickCounter();
  95. //==============================================================================
  96. /** This lets you prevent the screensaver from becoming active.
  97. Handy if you're running some sort of presentation app where having a screensaver
  98. appear would be annoying.
  99. Pass false to disable the screensaver, and true to re-enable it. (Note that this
  100. won't enable a screensaver unless the user has actually set one up).
  101. The disablement will only happen while the Juce application is the foreground
  102. process - if another task is running in front of it, then the screensaver will
  103. be unaffected.
  104. @see isScreenSaverEnabled
  105. */
  106. static void setScreenSaverEnabled (bool isEnabled);
  107. /** Returns true if the screensaver has not been turned off.
  108. This will return the last value passed into setScreenSaverEnabled(). Note that
  109. it won't tell you whether the user is actually using a screen saver, just
  110. whether this app is deliberately preventing one from running.
  111. @see setScreenSaverEnabled
  112. */
  113. static bool isScreenSaverEnabled();
  114. //==============================================================================
  115. /** Registers a MouseListener that will receive all mouse events that occur on
  116. any component.
  117. @see removeGlobalMouseListener
  118. */
  119. void addGlobalMouseListener (MouseListener* listener);
  120. /** Unregisters a MouseListener that was added with the addGlobalMouseListener()
  121. method.
  122. @see addGlobalMouseListener
  123. */
  124. void removeGlobalMouseListener (MouseListener* listener);
  125. //==============================================================================
  126. /** Registers a MouseListener that will receive a callback whenever the focused
  127. component changes.
  128. */
  129. void addFocusChangeListener (FocusChangeListener* listener);
  130. /** Unregisters a listener that was added with addFocusChangeListener(). */
  131. void removeFocusChangeListener (FocusChangeListener* listener);
  132. //==============================================================================
  133. /** Takes a component and makes it full-screen, removing the taskbar, dock, etc.
  134. The component must already be on the desktop for this method to work. It will
  135. be resized to completely fill the screen and any extraneous taskbars, menu bars,
  136. etc will be hidden.
  137. To exit kiosk mode, just call setKioskModeComponent (nullptr). When this is called,
  138. the component that's currently being used will be resized back to the size
  139. and position it was in before being put into this mode.
  140. If allowMenusAndBars is true, things like the menu and dock (on mac) are still
  141. allowed to pop up when the mouse moves onto them. If this is false, it'll try
  142. to hide as much on-screen paraphenalia as possible.
  143. */
  144. void setKioskModeComponent (Component* componentToUse,
  145. bool allowMenusAndBars = true);
  146. /** Returns the component that is currently being used in kiosk-mode.
  147. This is the component that was last set by setKioskModeComponent(). If none
  148. has been set, this returns 0.
  149. */
  150. Component* getKioskModeComponent() const noexcept { return kioskModeComponent; }
  151. //==============================================================================
  152. /** Returns the number of components that are currently active as top-level
  153. desktop windows.
  154. @see getComponent, Component::addToDesktop
  155. */
  156. int getNumComponents() const noexcept;
  157. /** Returns one of the top-level desktop window components.
  158. The index is from 0 to getNumComponents() - 1. This could return 0 if the
  159. index is out-of-range.
  160. @see getNumComponents, Component::addToDesktop
  161. */
  162. Component* getComponent (int index) const noexcept;
  163. /** Finds the component at a given screen location.
  164. This will drill down into top-level windows to find the child component at
  165. the given position.
  166. Returns 0 if the co-ordinates are inside a non-Juce window.
  167. */
  168. Component* findComponentAt (const Point<int>& screenPosition) const;
  169. /** The Desktop object has a ComponentAnimator instance which can be used for performing
  170. your animations.
  171. Having a single shared ComponentAnimator object makes it more efficient when multiple
  172. components are being moved around simultaneously. It's also more convenient than having
  173. to manage your own instance of one.
  174. @see ComponentAnimator
  175. */
  176. ComponentAnimator& getAnimator() noexcept { return animator; }
  177. //==============================================================================
  178. /** Returns the current default look-and-feel for components which don't have one
  179. explicitly set.
  180. @see setDefaultLookAndFeel
  181. */
  182. LookAndFeel& getDefaultLookAndFeel() noexcept;
  183. /** Changes the default look-and-feel.
  184. @param newDefaultLookAndFeel the new look-and-feel object to use - if this is
  185. set to nullptr, it will revert to using the system's
  186. default one. The object passed-in must be deleted by the
  187. caller when it's no longer needed.
  188. @see getDefaultLookAndFeel
  189. */
  190. void setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel);
  191. //==============================================================================
  192. /** Returns the number of MouseInputSource objects the system has at its disposal.
  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. To find out how many mouse events are currently happening, use getNumDraggingMouseSources().
  196. @see getMouseSource
  197. */
  198. int getNumMouseSources() const noexcept { return mouseSources.size(); }
  199. /** Returns one of the system's MouseInputSource objects.
  200. The index should be from 0 to getNumMouseSources() - 1. Out-of-range indexes will return
  201. a null pointer.
  202. In a traditional single-mouse system, there might be only one object. On a multi-touch
  203. system, there could be one input source per potential finger.
  204. */
  205. MouseInputSource* getMouseSource (int index) const noexcept { return mouseSources [index]; }
  206. /** Returns the main mouse input device that the system is using.
  207. @see getNumMouseSources()
  208. */
  209. MouseInputSource& getMainMouseSource() const noexcept { return *mouseSources.getUnchecked(0); }
  210. /** Returns the number of mouse-sources that are currently being dragged.
  211. In a traditional single-mouse system, this will be 0 or 1, depending on whether a
  212. juce component has the button down on it. In a multi-touch system, this could
  213. be any number from 0 to the number of simultaneous touches that can be detected.
  214. */
  215. int getNumDraggingMouseSources() const noexcept;
  216. /** Returns one of the mouse sources that's currently being dragged.
  217. The index should be between 0 and getNumDraggingMouseSources() - 1. If the index is
  218. out of range, or if no mice or fingers are down, this will return a null pointer.
  219. */
  220. MouseInputSource* getDraggingMouseSource (int index) const noexcept;
  221. /** Ensures that a non-stop stream of mouse-drag events will be sent during the
  222. current mouse-drag operation.
  223. This allows you to make sure that mouseDrag() events are sent continuously, even
  224. when the mouse isn't moving. This can be useful for things like auto-scrolling
  225. components when the mouse is near an edge.
  226. Call this method during a mouseDown() or mouseDrag() callback, specifying the
  227. minimum interval between consecutive mouse drag callbacks. The callbacks
  228. will continue until the mouse is released, and then the interval will be reset,
  229. so you need to make sure it's called every time you begin a drag event.
  230. Passing an interval of 0 or less will cancel the auto-repeat.
  231. @see mouseDrag
  232. */
  233. void beginDragAutoRepeat (int millisecondsBetweenCallbacks);
  234. //==============================================================================
  235. /** In a tablet device which can be turned around, this is used to inidicate the orientation. */
  236. enum DisplayOrientation
  237. {
  238. upright = 1, /**< Indicates that the display is the normal way up. */
  239. upsideDown = 2, /**< Indicates that the display is upside-down. */
  240. rotatedClockwise = 4, /**< Indicates that the display is turned 90 degrees clockwise from its upright position. */
  241. rotatedAntiClockwise = 8, /**< Indicates that the display is turned 90 degrees anti-clockwise from its upright position. */
  242. allOrientations = 1 + 2 + 4 + 8 /**< A combination of all the orientation values */
  243. };
  244. /** In a tablet device which can be turned around, this returns the current orientation. */
  245. DisplayOrientation getCurrentOrientation() const;
  246. /** Sets which orientations the display is allowed to auto-rotate to.
  247. For devices that support rotating desktops, this lets you specify which of the orientations your app can use.
  248. The parameter is a bitwise or-ed combination of the values in DisplayOrientation, and must contain at least one
  249. set bit.
  250. */
  251. void setOrientationsEnabled (int allowedOrientations);
  252. /** Returns whether the display is allowed to auto-rotate to the given orientation.
  253. Each orientation can be enabled using setOrientationEnabled(). By default, all orientations are allowed.
  254. */
  255. bool isOrientationEnabled (DisplayOrientation orientation) const noexcept;
  256. //==============================================================================
  257. /** Tells this object to refresh its idea of what the screen resolution is.
  258. (Called internally by the native code).
  259. */
  260. void refreshMonitorSizes();
  261. /** True if the OS supports semitransparent windows */
  262. static bool canUseSemiTransparentWindows() noexcept;
  263. private:
  264. //==============================================================================
  265. static Desktop* instance;
  266. friend class Component;
  267. friend class ComponentPeer;
  268. friend class MouseInputSource;
  269. friend class MouseInputSourceInternal;
  270. friend class DeletedAtShutdown;
  271. friend class TopLevelWindowManager;
  272. OwnedArray <MouseInputSource> mouseSources;
  273. void createMouseInputSources();
  274. ListenerList <MouseListener> mouseListeners;
  275. ListenerList <FocusChangeListener> focusListeners;
  276. Array <Component*> desktopComponents;
  277. Array <Rectangle<int> > monitorCoordsClipped, monitorCoordsUnclipped;
  278. Point<int> lastFakeMouseMove;
  279. void sendMouseMove();
  280. int mouseClickCounter;
  281. void incrementMouseClickCounter() noexcept;
  282. ScopedPointer<Timer> dragRepeater;
  283. ScopedPointer<LookAndFeel> defaultLookAndFeel;
  284. WeakReference<LookAndFeel> currentLookAndFeel;
  285. Component* kioskModeComponent;
  286. Rectangle<int> kioskComponentOriginalBounds;
  287. int allowedOrientations;
  288. ComponentAnimator animator;
  289. void timerCallback();
  290. void resetTimer();
  291. ListenerList <MouseListener>& getMouseListeners();
  292. int getNumDisplayMonitors() const noexcept;
  293. const Rectangle<int> getDisplayMonitorCoordinates (int index, bool clippedToWorkArea) const noexcept;
  294. static void getCurrentMonitorPositions (Array <Rectangle<int> >&, const bool clipToWorkArea);
  295. void addDesktopComponent (Component*);
  296. void removeDesktopComponent (Component*);
  297. void componentBroughtToFront (Component*);
  298. static void setKioskComponent (Component*, bool enableOrDisable, bool allowMenusAndBars);
  299. void triggerFocusCallback();
  300. void handleAsyncUpdate();
  301. Desktop();
  302. ~Desktop();
  303. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Desktop);
  304. };
  305. #endif // __JUCE_DESKTOP_JUCEHEADER__