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.

436 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. The Component class uses a ComponentPeer internally to create and manage a real
  23. operating-system window.
  24. This is an abstract base class - the platform specific code contains implementations of
  25. it for the various platforms.
  26. User-code should very rarely need to have any involvement with this class.
  27. @see Component::createNewPeer
  28. @tags{GUI}
  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 << 30) /**< 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. /** Converts a rectangle relative to the top-left of this component to screen coordinates. */
  130. Rectangle<float> localToGlobal (const Rectangle<float>& relativePosition);
  131. /** Converts a screen area to a position relative to the top-left of this component. */
  132. Rectangle<float> globalToLocal (const Rectangle<float>& screenPosition);
  133. /** Returns the area in peer coordinates that is covered by the given sub-comp (which
  134. may be at any depth)
  135. */
  136. Rectangle<int> getAreaCoveredBy (Component& subComponent) const;
  137. /** Minimises the window. */
  138. virtual void setMinimised (bool shouldBeMinimised) = 0;
  139. /** True if the window is currently minimised. */
  140. virtual bool isMinimised() const = 0;
  141. /** Enable/disable fullscreen mode for the window. */
  142. virtual void setFullScreen (bool shouldBeFullScreen) = 0;
  143. /** True if the window is currently full-screen. */
  144. virtual bool isFullScreen() const = 0;
  145. /** True if the window is in kiosk-mode. */
  146. virtual bool isKioskMode() const;
  147. /** Sets the size to restore to if fullscreen mode is turned off. */
  148. void setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept;
  149. /** Returns the size to restore to if fullscreen mode is turned off. */
  150. const Rectangle<int>& getNonFullScreenBounds() const noexcept;
  151. /** Attempts to change the icon associated with this window. */
  152. virtual void setIcon (const Image& newIcon) = 0;
  153. /** Sets a constrainer to use if the peer can resize itself.
  154. The constrainer won't be deleted by this object, so the caller must manage its lifetime.
  155. */
  156. void setConstrainer (ComponentBoundsConstrainer* newConstrainer) noexcept;
  157. /** Returns the current constrainer, if one has been set. */
  158. ComponentBoundsConstrainer* getConstrainer() const noexcept { return constrainer; }
  159. /** Checks if a point is in the window.
  160. The position is relative to the top-left of this window, in unscaled peer coordinates.
  161. If trueIfInAChildWindow is false, then this returns false if the point is actually
  162. inside a child of this window.
  163. */
  164. virtual bool contains (Point<int> localPos, bool trueIfInAChildWindow) const = 0;
  165. /** Returns the size of the window frame that's around this window.
  166. Whether or not the window has a normal window frame depends on the flags
  167. that were set when the window was created by Component::addToDesktop()
  168. */
  169. virtual BorderSize<int> getFrameSize() const = 0;
  170. /** This is called when the window's bounds change.
  171. A peer implementation must call this when the window is moved and resized, so that
  172. this method can pass the message on to the component.
  173. */
  174. void handleMovedOrResized();
  175. /** This is called if the screen resolution changes.
  176. A peer implementation must call this if the monitor arrangement changes or the available
  177. screen size changes.
  178. */
  179. virtual void handleScreenSizeChange();
  180. //==============================================================================
  181. /** This is called to repaint the component into the given context. */
  182. void handlePaint (LowLevelGraphicsContext& contextToPaintTo);
  183. //==============================================================================
  184. /** Sets this window to either be always-on-top or normal.
  185. Some kinds of window might not be able to do this, so should return false.
  186. */
  187. virtual bool setAlwaysOnTop (bool alwaysOnTop) = 0;
  188. /** Brings the window to the top, optionally also giving it focus. */
  189. virtual void toFront (bool makeActive) = 0;
  190. /** Moves the window to be just behind another one. */
  191. virtual void toBehind (ComponentPeer* other) = 0;
  192. /** Called when the window is brought to the front, either by the OS or by a call
  193. to toFront().
  194. */
  195. void handleBroughtToFront();
  196. //==============================================================================
  197. /** True if the window has the keyboard focus. */
  198. virtual bool isFocused() const = 0;
  199. /** Tries to give the window keyboard focus. */
  200. virtual void grabFocus() = 0;
  201. /** Called when the window gains keyboard focus. */
  202. void handleFocusGain();
  203. /** Called when the window loses keyboard focus. */
  204. void handleFocusLoss();
  205. Component* getLastFocusedSubcomponent() const noexcept;
  206. /** Called when a key is pressed.
  207. For keycode info, see the KeyPress class.
  208. Returns true if the keystroke was used.
  209. */
  210. bool handleKeyPress (int keyCode, juce_wchar textCharacter);
  211. /** Called when a key is pressed.
  212. Returns true if the keystroke was used.
  213. */
  214. bool handleKeyPress (const KeyPress& key);
  215. /** Called whenever a key is pressed or released.
  216. Returns true if the keystroke was used.
  217. */
  218. bool handleKeyUpOrDown (bool isKeyDown);
  219. /** Called whenever a modifier key is pressed or released. */
  220. void handleModifierKeysChange();
  221. //==============================================================================
  222. /** Tells the window that text input may be required at the given position.
  223. This may cause things like a virtual on-screen keyboard to appear, depending
  224. on the OS.
  225. */
  226. virtual void textInputRequired (Point<int> position, TextInputTarget&) = 0;
  227. /** If there's some kind of OS input-method in progress, this should dismiss it. */
  228. virtual void dismissPendingTextInput();
  229. /** Returns the currently focused TextInputTarget, or null if none is found. */
  230. TextInputTarget* findCurrentTextInputTarget();
  231. //==============================================================================
  232. /** Invalidates a region of the window to be repainted asynchronously. */
  233. virtual void repaint (const Rectangle<int>& area) = 0;
  234. /** This can be called (from the message thread) to cause the immediate redrawing
  235. of any areas of this window that need repainting.
  236. You shouldn't ever really need to use this, it's mainly for special purposes
  237. like supporting audio plugins where the host's event loop is out of our control.
  238. */
  239. virtual void performAnyPendingRepaintsNow() = 0;
  240. /** Changes the window's transparency. */
  241. virtual void setAlpha (float newAlpha) = 0;
  242. //==============================================================================
  243. void handleMouseEvent (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer, ModifierKeys newMods, float pressure,
  244. float orientation, int64 time, PenDetails pen = PenDetails(), int touchIndex = 0);
  245. void handleMouseWheel (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer,
  246. int64 time, const MouseWheelDetails&, int touchIndex = 0);
  247. void handleMagnifyGesture (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer,
  248. int64 time, float scaleFactor, int touchIndex = 0);
  249. void handleUserClosingWindow();
  250. /** Structure to describe drag and drop information */
  251. struct DragInfo
  252. {
  253. StringArray files;
  254. String text;
  255. Point<int> position;
  256. bool isEmpty() const noexcept { return files.size() == 0 && text.isEmpty(); }
  257. void clear() noexcept { files.clear(); text.clear(); }
  258. };
  259. bool handleDragMove (const DragInfo&);
  260. bool handleDragExit (const DragInfo&);
  261. bool handleDragDrop (const DragInfo&);
  262. //==============================================================================
  263. /** Returns the number of currently-active peers.
  264. @see getPeer
  265. */
  266. static int getNumPeers() noexcept;
  267. /** Returns one of the currently-active peers.
  268. @see getNumPeers
  269. */
  270. static ComponentPeer* getPeer (int index) noexcept;
  271. /** Returns the peer that's attached to the given component, or nullptr if there isn't one. */
  272. static ComponentPeer* getPeerFor (const Component*) noexcept;
  273. /** Checks if this peer object is valid.
  274. @see getNumPeers
  275. */
  276. static bool isValidPeer (const ComponentPeer* peer) noexcept;
  277. //==============================================================================
  278. virtual StringArray getAvailableRenderingEngines() = 0;
  279. virtual int getCurrentRenderingEngine() const;
  280. virtual void setCurrentRenderingEngine (int index);
  281. //==============================================================================
  282. /** On desktop platforms this method will check all the mouse and key states and return
  283. a ModifierKeys object representing them.
  284. This isn't recommended and is only needed in special circumstances for up-to-date
  285. modifier information at times when the app's event loop isn't running normally.
  286. Another reason to avoid this method is that it's not stateless and calling it may
  287. update the ModifierKeys::currentModifiers object, which could cause subtle changes
  288. in the behaviour of some components.
  289. */
  290. static ModifierKeys getCurrentModifiersRealtime() noexcept;
  291. //==============================================================================
  292. /** Used to receive callbacks when the OS scale factor of this ComponentPeer changes.
  293. This is used internally by some native JUCE windows on Windows and Linux and you
  294. shouldn't need to worry about it in your own code unless you are dealing directly
  295. with native windows.
  296. */
  297. struct JUCE_API ScaleFactorListener
  298. {
  299. /** Destructor. */
  300. virtual ~ScaleFactorListener() = default;
  301. /** Called when the scale factor changes. */
  302. virtual void nativeScaleFactorChanged (double newScaleFactor) = 0;
  303. };
  304. /** Adds a scale factor listener. */
  305. void addScaleFactorListener (ScaleFactorListener* listenerToAdd) { scaleFactorListeners.add (listenerToAdd); }
  306. /** Removes a scale factor listener. */
  307. void removeScaleFactorListener (ScaleFactorListener* listenerToRemove) { scaleFactorListeners.remove (listenerToRemove); }
  308. //==============================================================================
  309. /** On Windows and Linux this will return the OS scaling factor currently being applied
  310. to the native window. This is used to convert between physical and logical pixels
  311. at the OS API level and you shouldn't need to use it in your own code unless you
  312. are dealing directly with the native window.
  313. */
  314. virtual double getPlatformScaleFactor() const noexcept { return 1.0; }
  315. protected:
  316. //==============================================================================
  317. Component& component;
  318. const int styleFlags;
  319. Rectangle<int> lastNonFullscreenBounds;
  320. ComponentBoundsConstrainer* constrainer = nullptr;
  321. static std::function<ModifierKeys()> getNativeRealtimeModifiers;
  322. ListenerList<ScaleFactorListener> scaleFactorListeners;
  323. private:
  324. //==============================================================================
  325. WeakReference<Component> lastFocusedComponent, dragAndDropTargetComponent;
  326. Component* lastDragAndDropCompUnderMouse = nullptr;
  327. const uint32 uniqueID;
  328. bool isWindowMinimised = false;
  329. Component* getTargetForKeyPress();
  330. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentPeer)
  331. };
  332. } // namespace juce