Audio plugin host https://kx.studio/carla
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.

383 lines
17KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. The Component class uses a ComponentPeer internally to create and manage a real
  24. operating-system window.
  25. This is an abstract base class - the platform specific code contains implementations of
  26. it for the various platforms.
  27. User-code should very rarely need to have any involvement with this class.
  28. @see Component::createNewPeer
  29. */
  30. class JUCE_API ComponentPeer
  31. {
  32. public:
  33. //==============================================================================
  34. /** A combination of these flags is passed to the ComponentPeer constructor. */
  35. enum StyleFlags
  36. {
  37. windowAppearsOnTaskbar = (1 << 0), /**< Indicates that the window should have a corresponding
  38. entry on the taskbar (ignored on MacOSX) */
  39. windowIsTemporary = (1 << 1), /**< Indicates that the window is a temporary popup, like a menu,
  40. tooltip, etc. */
  41. windowIgnoresMouseClicks = (1 << 2), /**< Indicates that the window should let mouse clicks pass
  42. through it (may not be possible on some platforms). */
  43. windowHasTitleBar = (1 << 3), /**< Indicates that the window should have a normal OS-specific
  44. title bar and frame. if not specified, the window will be
  45. borderless. */
  46. windowIsResizable = (1 << 4), /**< Indicates that the window should have a resizable border. */
  47. windowHasMinimiseButton = (1 << 5), /**< Indicates that if the window has a title bar, it should have a
  48. minimise button on it. */
  49. windowHasMaximiseButton = (1 << 6), /**< Indicates that if the window has a title bar, it should have a
  50. maximise button on it. */
  51. windowHasCloseButton = (1 << 7), /**< Indicates that if the window has a title bar, it should have a
  52. close button on it. */
  53. windowHasDropShadow = (1 << 8), /**< Indicates that the window should have a drop-shadow (this may
  54. not be possible on all platforms). */
  55. windowRepaintedExplictly = (1 << 9), /**< Not intended for public use - this tells a window not to
  56. do its own repainting, but only to repaint when the
  57. performAnyPendingRepaintsNow() method is called. */
  58. windowIgnoresKeyPresses = (1 << 10), /**< Tells the window not to catch any keypresses. This can
  59. be used for things like plugin windows, to stop them interfering
  60. with the host's shortcut keys */
  61. windowIsSemiTransparent = (1 << 31) /**< Not intended for public use - makes a window transparent. */
  62. };
  63. //==============================================================================
  64. /** Creates a peer.
  65. The component is the one that we intend to represent, and the style flags are
  66. a combination of the values in the StyleFlags enum
  67. */
  68. ComponentPeer (Component& component, int styleFlags);
  69. /** Destructor. */
  70. virtual ~ComponentPeer();
  71. //==============================================================================
  72. /** Returns the component being represented by this peer. */
  73. Component& getComponent() noexcept { return component; }
  74. /** Returns the set of style flags that were set when the window was created.
  75. @see Component::addToDesktop
  76. */
  77. int getStyleFlags() const noexcept { return styleFlags; }
  78. /** Returns a unique ID for this peer.
  79. Each peer that is created is given a different ID.
  80. */
  81. uint32 getUniqueID() const noexcept { return uniqueID; }
  82. //==============================================================================
  83. /** Returns the raw handle to whatever kind of window is being used.
  84. On windows, this is probably a HWND, on the mac, it's likely to be a WindowRef,
  85. but remember there's no guarantees what you'll get back.
  86. */
  87. virtual void* getNativeHandle() const = 0;
  88. /** Shows or hides the window. */
  89. virtual void setVisible (bool shouldBeVisible) = 0;
  90. /** Changes the title of the window. */
  91. virtual void setTitle (const String& title) = 0;
  92. /** If this type of window is capable of indicating that the document in it has been
  93. edited, then this changes its status.
  94. For example in OSX, this changes the appearance of the close button.
  95. @returns true if the window has a mechanism for showing this, or false if not.
  96. */
  97. virtual bool setDocumentEditedStatus (bool edited);
  98. /** If this type of window is capable of indicating that it represents a file, then
  99. this lets you set the file.
  100. E.g. in OSX it'll show an icon for the file in the title bar.
  101. */
  102. virtual void setRepresentedFile (const File&);
  103. //==============================================================================
  104. /** Moves and resizes the window.
  105. If the native window is contained in another window, then the coordinates are
  106. relative to the parent window's origin, not the screen origin.
  107. This should result in a callback to handleMovedOrResized().
  108. */
  109. virtual void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) = 0;
  110. /** Updates the peer's bounds to match its component. */
  111. void updateBounds();
  112. /** Returns the current position and size of the window.
  113. If the native window is contained in another window, then the coordinates are
  114. relative to the parent window's origin, not the screen origin.
  115. */
  116. virtual Rectangle<int> getBounds() const = 0;
  117. /** Converts a position relative to the top-left of this component to screen coordinates. */
  118. virtual Point<float> localToGlobal (Point<float> relativePosition) = 0;
  119. /** Converts a screen coordinate to a position relative to the top-left of this component. */
  120. virtual Point<float> globalToLocal (Point<float> screenPosition) = 0;
  121. /** Converts a position relative to the top-left of this component to screen coordinates. */
  122. Point<int> localToGlobal (Point<int> relativePosition);
  123. /** Converts a screen coordinate to a position relative to the top-left of this component. */
  124. Point<int> globalToLocal (Point<int> screenPosition);
  125. /** Converts a rectangle relative to the top-left of this component to screen coordinates. */
  126. virtual Rectangle<int> localToGlobal (const Rectangle<int>& relativePosition);
  127. /** Converts a screen area to a position relative to the top-left of this component. */
  128. virtual Rectangle<int> globalToLocal (const Rectangle<int>& screenPosition);
  129. /** Returns the area in peer coordinates that is covered by the given sub-comp (which
  130. may be at any depth)
  131. */
  132. Rectangle<int> getAreaCoveredBy (Component& subComponent) const;
  133. /** Minimises the window. */
  134. virtual void setMinimised (bool shouldBeMinimised) = 0;
  135. /** True if the window is currently minimised. */
  136. virtual bool isMinimised() const = 0;
  137. /** Enable/disable fullscreen mode for the window. */
  138. virtual void setFullScreen (bool shouldBeFullScreen) = 0;
  139. /** True if the window is currently full-screen. */
  140. virtual bool isFullScreen() const = 0;
  141. /** True if the window is in kiosk-mode. */
  142. virtual bool isKioskMode() const;
  143. /** Sets the size to restore to if fullscreen mode is turned off. */
  144. void setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept;
  145. /** Returns the size to restore to if fullscreen mode is turned off. */
  146. const Rectangle<int>& getNonFullScreenBounds() const noexcept;
  147. /** Attempts to change the icon associated with this window. */
  148. virtual void setIcon (const Image& newIcon) = 0;
  149. /** Sets a constrainer to use if the peer can resize itself.
  150. The constrainer won't be deleted by this object, so the caller must manage its lifetime.
  151. */
  152. void setConstrainer (ComponentBoundsConstrainer* newConstrainer) noexcept;
  153. /** Returns the current constrainer, if one has been set. */
  154. ComponentBoundsConstrainer* getConstrainer() const noexcept { return constrainer; }
  155. /** Checks if a point is in the window.
  156. The position is relative to the top-left of this window, in unscaled peer coordinates.
  157. If trueIfInAChildWindow is false, then this returns false if the point is actually
  158. inside a child of this window.
  159. */
  160. virtual bool contains (Point<int> localPos, bool trueIfInAChildWindow) const = 0;
  161. /** Returns the size of the window frame that's around this window.
  162. Whether or not the window has a normal window frame depends on the flags
  163. that were set when the window was created by Component::addToDesktop()
  164. */
  165. virtual BorderSize<int> getFrameSize() const = 0;
  166. /** This is called when the window's bounds change.
  167. A peer implementation must call this when the window is moved and resized, so that
  168. this method can pass the message on to the component.
  169. */
  170. void handleMovedOrResized();
  171. /** This is called if the screen resolution changes.
  172. A peer implementation must call this if the monitor arrangement changes or the available
  173. screen size changes.
  174. */
  175. virtual void handleScreenSizeChange();
  176. //==============================================================================
  177. /** This is called to repaint the component into the given context. */
  178. void handlePaint (LowLevelGraphicsContext& contextToPaintTo);
  179. //==============================================================================
  180. /** Sets this window to either be always-on-top or normal.
  181. Some kinds of window might not be able to do this, so should return false.
  182. */
  183. virtual bool setAlwaysOnTop (bool alwaysOnTop) = 0;
  184. /** Brings the window to the top, optionally also giving it focus. */
  185. virtual void toFront (bool makeActive) = 0;
  186. /** Moves the window to be just behind another one. */
  187. virtual void toBehind (ComponentPeer* other) = 0;
  188. /** Called when the window is brought to the front, either by the OS or by a call
  189. to toFront().
  190. */
  191. void handleBroughtToFront();
  192. //==============================================================================
  193. /** True if the window has the keyboard focus. */
  194. virtual bool isFocused() const = 0;
  195. /** Tries to give the window keyboard focus. */
  196. virtual void grabFocus() = 0;
  197. /** Called when the window gains keyboard focus. */
  198. void handleFocusGain();
  199. /** Called when the window loses keyboard focus. */
  200. void handleFocusLoss();
  201. Component* getLastFocusedSubcomponent() const noexcept;
  202. /** Called when a key is pressed.
  203. For keycode info, see the KeyPress class.
  204. Returns true if the keystroke was used.
  205. */
  206. bool handleKeyPress (int keyCode, juce_wchar textCharacter);
  207. /** Called when a key is pressed.
  208. Returns true if the keystroke was used.
  209. */
  210. bool handleKeyPress (const KeyPress& key);
  211. /** Called whenever a key is pressed or released.
  212. Returns true if the keystroke was used.
  213. */
  214. bool handleKeyUpOrDown (bool isKeyDown);
  215. /** Called whenever a modifier key is pressed or released. */
  216. void handleModifierKeysChange();
  217. //==============================================================================
  218. /** Tells the window that text input may be required at the given position.
  219. This may cause things like a virtual on-screen keyboard to appear, depending
  220. on the OS.
  221. */
  222. virtual void textInputRequired (Point<int> position, TextInputTarget&) = 0;
  223. /** If there's some kind of OS input-method in progress, this should dismiss it. */
  224. virtual void dismissPendingTextInput();
  225. /** Returns the currently focused TextInputTarget, or null if none is found. */
  226. TextInputTarget* findCurrentTextInputTarget();
  227. //==============================================================================
  228. /** Invalidates a region of the window to be repainted asynchronously. */
  229. virtual void repaint (const Rectangle<int>& area) = 0;
  230. /** This can be called (from the message thread) to cause the immediate redrawing
  231. of any areas of this window that need repainting.
  232. You shouldn't ever really need to use this, it's mainly for special purposes
  233. like supporting audio plugins where the host's event loop is out of our control.
  234. */
  235. virtual void performAnyPendingRepaintsNow() = 0;
  236. /** Changes the window's transparency. */
  237. virtual void setAlpha (float newAlpha) = 0;
  238. //==============================================================================
  239. void handleMouseEvent (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer, ModifierKeys newMods, float pressure,
  240. float orientation, int64 time, PenDetails pen = { 0.0f, 0.0f, 0.0f }, int touchIndex = 0);
  241. void handleMouseWheel (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer,
  242. int64 time, const MouseWheelDetails&, int touchIndex = 0);
  243. void handleMagnifyGesture (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer,
  244. int64 time, float scaleFactor, int touchIndex = 0);
  245. void handleUserClosingWindow();
  246. struct DragInfo
  247. {
  248. StringArray files;
  249. String text;
  250. Point<int> position;
  251. bool isEmpty() const noexcept { return files.size() == 0 && text.isEmpty(); }
  252. void clear() noexcept { files.clear(); text.clear(); }
  253. };
  254. bool handleDragMove (const DragInfo&);
  255. bool handleDragExit (const DragInfo&);
  256. bool handleDragDrop (const DragInfo&);
  257. //==============================================================================
  258. /** Returns the number of currently-active peers.
  259. @see getPeer
  260. */
  261. static int getNumPeers() noexcept;
  262. /** Returns one of the currently-active peers.
  263. @see getNumPeers
  264. */
  265. static ComponentPeer* getPeer (int index) noexcept;
  266. /** Returns the peer that's attached to the given component, or nullptr if there isn't one. */
  267. static ComponentPeer* getPeerFor (const Component*) noexcept;
  268. /** Checks if this peer object is valid.
  269. @see getNumPeers
  270. */
  271. static bool isValidPeer (const ComponentPeer* peer) noexcept;
  272. //==============================================================================
  273. virtual StringArray getAvailableRenderingEngines() = 0;
  274. virtual int getCurrentRenderingEngine() const;
  275. virtual void setCurrentRenderingEngine (int index);
  276. protected:
  277. //==============================================================================
  278. Component& component;
  279. const int styleFlags;
  280. Rectangle<int> lastNonFullscreenBounds;
  281. ComponentBoundsConstrainer* constrainer = nullptr;
  282. private:
  283. //==============================================================================
  284. WeakReference<Component> lastFocusedComponent, dragAndDropTargetComponent;
  285. Component* lastDragAndDropCompUnderMouse = nullptr;
  286. const uint32 uniqueID;
  287. bool isWindowMinimised = false;
  288. Component* getTargetForKeyPress();
  289. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentPeer)
  290. };
  291. } // namespace juce