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.

417 lines
18KB

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