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.

387 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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_COMPONENTPEER_JUCEHEADER__
  19. #define __JUCE_COMPONENTPEER_JUCEHEADER__
  20. #include "../juce_Component.h"
  21. #include "../mouse/juce_MouseCursor.h"
  22. #include "../keyboard/juce_TextInputTarget.h"
  23. #include "../../../events/juce_MessageListener.h"
  24. #include "../../../text/juce_StringArray.h"
  25. #include "../../graphics/geometry/juce_RectangleList.h"
  26. class ComponentBoundsConstrainer;
  27. //==============================================================================
  28. /**
  29. The base class for window objects that wrap a component as a real operating
  30. system object.
  31. This is an abstract base class - the platform specific code contains default
  32. implementations of it that create and manage windows.
  33. @see Component::createNewPeer
  34. */
  35. class JUCE_API ComponentPeer
  36. {
  37. public:
  38. //==============================================================================
  39. /** A combination of these flags is passed to the ComponentPeer constructor. */
  40. enum StyleFlags
  41. {
  42. windowAppearsOnTaskbar = (1 << 0), /**< Indicates that the window should have a corresponding
  43. entry on the taskbar (ignored on MacOSX) */
  44. windowIsTemporary = (1 << 1), /**< Indicates that the window is a temporary popup, like a menu,
  45. tooltip, etc. */
  46. windowIgnoresMouseClicks = (1 << 2), /**< Indicates that the window should let mouse clicks pass
  47. through it (may not be possible on some platforms). */
  48. windowHasTitleBar = (1 << 3), /**< Indicates that the window should have a normal OS-specific
  49. title bar and frame\. if not specified, the window will be
  50. borderless. */
  51. windowIsResizable = (1 << 4), /**< Indicates that the window should have a resizable border. */
  52. windowHasMinimiseButton = (1 << 5), /**< Indicates that if the window has a title bar, it should have a
  53. minimise button on it. */
  54. windowHasMaximiseButton = (1 << 6), /**< Indicates that if the window has a title bar, it should have a
  55. maximise button on it. */
  56. windowHasCloseButton = (1 << 7), /**< Indicates that if the window has a title bar, it should have a
  57. close button on it. */
  58. windowHasDropShadow = (1 << 8), /**< Indicates that the window should have a drop-shadow (this may
  59. not be possible on all platforms). */
  60. windowRepaintedExplictly = (1 << 9), /**< Not intended for public use - this tells a window not to
  61. do its own repainting, but only to repaint when the
  62. performAnyPendingRepaintsNow() method is called. */
  63. windowIgnoresKeyPresses = (1 << 10), /**< Tells the window not to catch any keypresses. This can
  64. be used for things like plugin windows, to stop them interfering
  65. with the host's shortcut keys */
  66. windowIsSemiTransparent = (1 << 31) /**< Not intended for public use - makes a window transparent. */
  67. };
  68. //==============================================================================
  69. /** Creates a peer.
  70. The component is the one that we intend to represent, and the style flags are
  71. a combination of the values in the StyleFlags enum
  72. */
  73. ComponentPeer (Component* component, int styleFlags);
  74. /** Destructor. */
  75. virtual ~ComponentPeer();
  76. //==============================================================================
  77. /** Returns the component being represented by this peer. */
  78. Component* getComponent() const throw() { return component; }
  79. /** Returns the set of style flags that were set when the window was created.
  80. @see Component::addToDesktop
  81. */
  82. int getStyleFlags() const throw() { return styleFlags; }
  83. //==============================================================================
  84. /** Returns the raw handle to whatever kind of window is being used.
  85. On windows, this is probably a HWND, on the mac, it's likely to be a WindowRef,
  86. but rememeber there's no guarantees what you'll get back.
  87. */
  88. virtual void* getNativeHandle() const = 0;
  89. /** Shows or hides the window. */
  90. virtual void setVisible (bool shouldBeVisible) = 0;
  91. /** Changes the title of the window. */
  92. virtual void setTitle (const String& title) = 0;
  93. /** Moves the window without changing its size.
  94. If the native window is contained in another window, then the co-ordinates are
  95. relative to the parent window's origin, not the screen origin.
  96. This should result in a callback to handleMovedOrResized().
  97. */
  98. virtual void setPosition (int x, int y) = 0;
  99. /** Resizes the window without changing its position.
  100. This should result in a callback to handleMovedOrResized().
  101. */
  102. virtual void setSize (int w, int h) = 0;
  103. /** Moves and resizes the window.
  104. If the native window is contained in another window, then the co-ordinates are
  105. relative to the parent window's origin, not the screen origin.
  106. This should result in a callback to handleMovedOrResized().
  107. */
  108. virtual void setBounds (int x, int y, int w, int h, bool isNowFullScreen) = 0;
  109. /** Returns the current position and size of the window.
  110. If the native window is contained in another window, then the co-ordinates are
  111. relative to the parent window's origin, not the screen origin.
  112. */
  113. virtual const Rectangle<int> getBounds() const = 0;
  114. /** Returns the x-position of this window, relative to the screen's origin. */
  115. virtual const Point<int> getScreenPosition() const = 0;
  116. /** Converts a position relative to the top-left of this component to screen co-ordinates. */
  117. virtual const Point<int> relativePositionToGlobal (const Point<int>& relativePosition) = 0;
  118. /** Converts a screen co-ordinate to a position relative to the top-left of this component. */
  119. virtual const Point<int> globalPositionToRelative (const Point<int>& screenPosition) = 0;
  120. /** Minimises the window. */
  121. virtual void setMinimised (bool shouldBeMinimised) = 0;
  122. /** True if the window is currently minimised. */
  123. virtual bool isMinimised() const = 0;
  124. /** Enable/disable fullscreen mode for the window. */
  125. virtual void setFullScreen (bool shouldBeFullScreen) = 0;
  126. /** True if the window is currently full-screen. */
  127. virtual bool isFullScreen() const = 0;
  128. /** Sets the size to restore to if fullscreen mode is turned off. */
  129. void setNonFullScreenBounds (const Rectangle<int>& newBounds) throw();
  130. /** Returns the size to restore to if fullscreen mode is turned off. */
  131. const Rectangle<int>& getNonFullScreenBounds() const throw();
  132. /** Attempts to change the icon associated with this window.
  133. */
  134. virtual void setIcon (const Image& newIcon) = 0;
  135. /** Sets a constrainer to use if the peer can resize itself.
  136. The constrainer won't be deleted by this object, so the caller must manage its lifetime.
  137. */
  138. void setConstrainer (ComponentBoundsConstrainer* newConstrainer) throw();
  139. /** Returns the current constrainer, if one has been set. */
  140. ComponentBoundsConstrainer* getConstrainer() const throw() { return constrainer; }
  141. /** Checks if a point is in the window.
  142. Coordinates are relative to the top-left of this window. If trueIfInAChildWindow
  143. is false, then this returns false if the point is actually inside a child of this
  144. window.
  145. */
  146. virtual bool contains (const Point<int>& position, bool trueIfInAChildWindow) const = 0;
  147. /** Returns the size of the window frame that's around this window.
  148. Whether or not the window has a normal window frame depends on the flags
  149. that were set when the window was created by Component::addToDesktop()
  150. */
  151. virtual const BorderSize getFrameSize() const = 0;
  152. /** This is called when the window's bounds change.
  153. A peer implementation must call this when the window is moved and resized, so that
  154. this method can pass the message on to the component.
  155. */
  156. void handleMovedOrResized();
  157. /** This is called if the screen resolution changes.
  158. A peer implementation must call this if the monitor arrangement changes or the available
  159. screen size changes.
  160. */
  161. void handleScreenSizeChange();
  162. //==============================================================================
  163. /** This is called to repaint the component into the given context. */
  164. void handlePaint (LowLevelGraphicsContext& contextToPaintTo);
  165. //==============================================================================
  166. /** Sets this window to either be always-on-top or normal.
  167. Some kinds of window might not be able to do this, so should return false.
  168. */
  169. virtual bool setAlwaysOnTop (bool alwaysOnTop) = 0;
  170. /** Brings the window to the top, optionally also giving it focus. */
  171. virtual void toFront (bool makeActive) = 0;
  172. /** Moves the window to be just behind another one. */
  173. virtual void toBehind (ComponentPeer* other) = 0;
  174. /** Called when the window is brought to the front, either by the OS or by a call
  175. to toFront().
  176. */
  177. void handleBroughtToFront();
  178. //==============================================================================
  179. /** True if the window has the keyboard focus. */
  180. virtual bool isFocused() const = 0;
  181. /** Tries to give the window keyboard focus. */
  182. virtual void grabFocus() = 0;
  183. /** Tells the window that text input may be required at the given position.
  184. This may cause things like a virtual on-screen keyboard to appear, depending
  185. on the OS.
  186. */
  187. virtual void textInputRequired (const Point<int>& position) = 0;
  188. /** Called when the window gains keyboard focus. */
  189. void handleFocusGain();
  190. /** Called when the window loses keyboard focus. */
  191. void handleFocusLoss();
  192. Component* getLastFocusedSubcomponent() const throw();
  193. /** Called when a key is pressed.
  194. For keycode info, see the KeyPress class.
  195. Returns true if the keystroke was used.
  196. */
  197. bool handleKeyPress (int keyCode,
  198. juce_wchar textCharacter);
  199. /** Called whenever a key is pressed or released.
  200. Returns true if the keystroke was used.
  201. */
  202. bool handleKeyUpOrDown (bool isKeyDown);
  203. /** Called whenever a modifier key is pressed or released. */
  204. void handleModifierKeysChange();
  205. /** Returns the currently focused TextInputTarget, or null if none is found. */
  206. TextInputTarget* findCurrentTextInputTarget();
  207. //==============================================================================
  208. /** Invalidates a region of the window to be repainted asynchronously. */
  209. virtual void repaint (const Rectangle<int>& area) = 0;
  210. /** This can be called (from the message thread) to cause the immediate redrawing
  211. of any areas of this window that need repainting.
  212. You shouldn't ever really need to use this, it's mainly for special purposes
  213. like supporting audio plugins where the host's event loop is out of our control.
  214. */
  215. virtual void performAnyPendingRepaintsNow() = 0;
  216. /** Changes the window's transparency. */
  217. virtual void setAlpha (float newAlpha) = 0;
  218. //==============================================================================
  219. void handleMouseEvent (int touchIndex, const Point<int>& positionWithinPeer, const ModifierKeys& newMods, int64 time);
  220. void handleMouseWheel (int touchIndex, const Point<int>& positionWithinPeer, int64 time, float x, float y);
  221. void handleUserClosingWindow();
  222. void handleFileDragMove (const StringArray& files, const Point<int>& position);
  223. void handleFileDragExit (const StringArray& files);
  224. void handleFileDragDrop (const StringArray& files, const Point<int>& position);
  225. //==============================================================================
  226. /** Resets the masking region.
  227. The subclass should call this every time it's about to call the handlePaint
  228. method.
  229. @see addMaskedRegion
  230. */
  231. void clearMaskedRegion();
  232. /** Adds a rectangle to the set of areas not to paint over.
  233. A component can call this on its peer during its paint() method, to signal
  234. that the painting code should ignore a given region. The reason
  235. for this is to stop embedded windows (such as OpenGL) getting painted over.
  236. The masked region is cleared each time before a paint happens, so a component
  237. will have to make sure it calls this every time it's painted.
  238. */
  239. void addMaskedRegion (int x, int y, int w, int h);
  240. //==============================================================================
  241. /** Returns the number of currently-active peers.
  242. @see getPeer
  243. */
  244. static int getNumPeers() throw();
  245. /** Returns one of the currently-active peers.
  246. @see getNumPeers
  247. */
  248. static ComponentPeer* getPeer (int index) throw();
  249. /** Checks if this peer object is valid.
  250. @see getNumPeers
  251. */
  252. static bool isValidPeer (const ComponentPeer* peer) throw();
  253. //==============================================================================
  254. static void bringModalComponentToFront();
  255. //==============================================================================
  256. virtual const StringArray getAvailableRenderingEngines();
  257. virtual int getCurrentRenderingEngine() throw();
  258. virtual void setCurrentRenderingEngine (int index);
  259. //==============================================================================
  260. juce_UseDebuggingNewOperator
  261. protected:
  262. Component* const component;
  263. const int styleFlags;
  264. RectangleList maskedRegion;
  265. Rectangle<int> lastNonFullscreenBounds;
  266. uint32 lastPaintTime;
  267. ComponentBoundsConstrainer* constrainer;
  268. static void updateCurrentModifiers() throw();
  269. private:
  270. //==============================================================================
  271. Component::SafePointer<Component> lastFocusedComponent, dragAndDropTargetComponent;
  272. Component* lastDragAndDropCompUnderMouse;
  273. bool fakeMouseMessageSent : 1, isWindowMinimised : 1;
  274. friend class Component;
  275. friend class Desktop;
  276. static ComponentPeer* getPeerFor (const Component* component) throw();
  277. void setLastDragDropTarget (Component* comp);
  278. ComponentPeer (const ComponentPeer&);
  279. ComponentPeer& operator= (const ComponentPeer&);
  280. };
  281. #endif // __JUCE_COMPONENTPEER_JUCEHEADER__