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.

juce_ComponentPeer.h 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 : private FocusChangeListener
  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. windowRequiresSynchronousCoreGraphicsRendering = (1 << 11), /**< Indicates that the window should not be rendered with
  62. asynchronous Core Graphics drawing operations. Use this if there
  63. are issues with regions not being redrawn at the expected time
  64. (macOS and iOS only). */
  65. windowIsSemiTransparent = (1 << 30) /**< Not intended for public use - makes a window transparent. */
  66. };
  67. /** Represents the window borders around a window component.
  68. You must use operator bool() to evaluate the validity of the object before accessing
  69. its value.
  70. Returned by getFrameSizeIfPresent(). A missing value may be returned on Linux for a
  71. short time after window creation.
  72. */
  73. class JUCE_API OptionalBorderSize final
  74. {
  75. public:
  76. /** Default constructor. Creates an invalid object. */
  77. OptionalBorderSize() : valid (false) {}
  78. /** Constructor. Creates a valid object containing the provided BorderSize<int>. */
  79. explicit OptionalBorderSize (BorderSize<int> size) : valid (true), borderSize (std::move (size)) {}
  80. /** Returns true if a valid value has been provided. */
  81. explicit operator bool() const noexcept { return valid; }
  82. /** Returns a reference to the value.
  83. You must not call this function on an invalid object. Use operator bool() to
  84. determine validity.
  85. */
  86. const auto& operator*() const noexcept
  87. {
  88. jassert (valid);
  89. return borderSize;
  90. }
  91. /** Returns a pointer to the value.
  92. You must not call this function on an invalid object. Use operator bool() to
  93. determine validity.
  94. */
  95. const auto* operator->() const noexcept
  96. {
  97. jassert (valid);
  98. return &borderSize;
  99. }
  100. private:
  101. bool valid;
  102. BorderSize<int> borderSize;
  103. };
  104. //==============================================================================
  105. /** Creates a peer.
  106. The component is the one that we intend to represent, and the style flags are
  107. a combination of the values in the StyleFlags enum
  108. */
  109. ComponentPeer (Component& component, int styleFlags);
  110. /** Destructor. */
  111. ~ComponentPeer() override;
  112. //==============================================================================
  113. /** Returns the component being represented by this peer. */
  114. Component& getComponent() noexcept { return component; }
  115. /** Returns the set of style flags that were set when the window was created.
  116. @see Component::addToDesktop
  117. */
  118. int getStyleFlags() const noexcept { return styleFlags; }
  119. /** Returns a unique ID for this peer.
  120. Each peer that is created is given a different ID.
  121. */
  122. uint32 getUniqueID() const noexcept { return uniqueID; }
  123. //==============================================================================
  124. /** Returns the raw handle to whatever kind of window is being used.
  125. On windows, this is probably a HWND, on the mac, it's likely to be a WindowRef,
  126. but remember there's no guarantees what you'll get back.
  127. */
  128. virtual void* getNativeHandle() const = 0;
  129. /** Shows or hides the window. */
  130. virtual void setVisible (bool shouldBeVisible) = 0;
  131. /** Changes the title of the window. */
  132. virtual void setTitle (const String& title) = 0;
  133. /** If this type of window is capable of indicating that the document in it has been
  134. edited, then this changes its status.
  135. For example in OSX, this changes the appearance of the close button.
  136. @returns true if the window has a mechanism for showing this, or false if not.
  137. */
  138. virtual bool setDocumentEditedStatus (bool edited);
  139. /** If this type of window is capable of indicating that it represents a file, then
  140. this lets you set the file.
  141. E.g. in OSX it'll show an icon for the file in the title bar.
  142. */
  143. virtual void setRepresentedFile (const File&);
  144. //==============================================================================
  145. /** Moves and resizes the window.
  146. If the native window is contained in another window, then the coordinates are
  147. relative to the parent window's origin, not the screen origin.
  148. This should result in a callback to handleMovedOrResized().
  149. */
  150. virtual void setBounds (const Rectangle<int>& newBounds, bool isNowFullScreen) = 0;
  151. /** Updates the peer's bounds to match its component. */
  152. void updateBounds();
  153. /** Returns the current position and size of the window.
  154. If the native window is contained in another window, then the coordinates are
  155. relative to the parent window's origin, not the screen origin.
  156. */
  157. virtual Rectangle<int> getBounds() const = 0;
  158. /** Converts a position relative to the top-left of this component to screen coordinates. */
  159. virtual Point<float> localToGlobal (Point<float> relativePosition) = 0;
  160. /** Converts a screen coordinate to a position relative to the top-left of this component. */
  161. virtual Point<float> globalToLocal (Point<float> screenPosition) = 0;
  162. /** Converts a position relative to the top-left of this component to screen coordinates. */
  163. Point<int> localToGlobal (Point<int> relativePosition);
  164. /** Converts a screen coordinate to a position relative to the top-left of this component. */
  165. Point<int> globalToLocal (Point<int> screenPosition);
  166. /** Converts a rectangle relative to the top-left of this component to screen coordinates. */
  167. virtual Rectangle<int> localToGlobal (const Rectangle<int>& relativePosition);
  168. /** Converts a screen area to a position relative to the top-left of this component. */
  169. virtual Rectangle<int> globalToLocal (const Rectangle<int>& screenPosition);
  170. /** Converts a rectangle relative to the top-left of this component to screen coordinates. */
  171. Rectangle<float> localToGlobal (const Rectangle<float>& relativePosition);
  172. /** Converts a screen area to a position relative to the top-left of this component. */
  173. Rectangle<float> globalToLocal (const Rectangle<float>& screenPosition);
  174. /** Returns the area in peer coordinates that is covered by the given sub-comp (which
  175. may be at any depth)
  176. */
  177. Rectangle<int> getAreaCoveredBy (const Component& subComponent) const;
  178. /** Minimises the window. */
  179. virtual void setMinimised (bool shouldBeMinimised) = 0;
  180. /** True if the window is currently minimised. */
  181. virtual bool isMinimised() const = 0;
  182. /** Enable/disable fullscreen mode for the window. */
  183. virtual void setFullScreen (bool shouldBeFullScreen) = 0;
  184. /** True if the window is currently full-screen. */
  185. virtual bool isFullScreen() const = 0;
  186. /** True if the window is in kiosk-mode. */
  187. virtual bool isKioskMode() const;
  188. /** Sets the size to restore to if fullscreen mode is turned off. */
  189. void setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept;
  190. /** Returns the size to restore to if fullscreen mode is turned off. */
  191. const Rectangle<int>& getNonFullScreenBounds() const noexcept;
  192. /** Attempts to change the icon associated with this window. */
  193. virtual void setIcon (const Image& newIcon) = 0;
  194. /** Sets a constrainer to use if the peer can resize itself.
  195. The constrainer won't be deleted by this object, so the caller must manage its lifetime.
  196. */
  197. void setConstrainer (ComponentBoundsConstrainer* newConstrainer) noexcept;
  198. /** Returns the current constrainer, if one has been set. */
  199. ComponentBoundsConstrainer* getConstrainer() const noexcept { return constrainer; }
  200. /** Checks if a point is in the window.
  201. The position is relative to the top-left of this window, in unscaled peer coordinates.
  202. If trueIfInAChildWindow is false, then this returns false if the point is actually
  203. inside a child of this window.
  204. */
  205. virtual bool contains (Point<int> localPos, bool trueIfInAChildWindow) const = 0;
  206. /** Returns the size of the window frame that's around this window.
  207. Depending on the platform the border size may be invalid for a short transient
  208. after creating a new window. Hence the returned value must be checked using
  209. operator bool() and the contained value can be accessed using operator*() only
  210. if it is present.
  211. Whether or not the window has a normal window frame depends on the flags
  212. that were set when the window was created by Component::addToDesktop()
  213. */
  214. virtual OptionalBorderSize getFrameSizeIfPresent() const = 0;
  215. /** Returns the size of the window frame that's around this window.
  216. Whether or not the window has a normal window frame depends on the flags
  217. that were set when the window was created by Component::addToDesktop()
  218. */
  219. #if JUCE_LINUX || JUCE_BSD
  220. [[deprecated ("Use getFrameSizeIfPresent instead.")]]
  221. #endif
  222. virtual BorderSize<int> getFrameSize() const = 0;
  223. /** This is called when the window's bounds change.
  224. A peer implementation must call this when the window is moved and resized, so that
  225. this method can pass the message on to the component.
  226. */
  227. void handleMovedOrResized();
  228. /** This is called if the screen resolution changes.
  229. A peer implementation must call this if the monitor arrangement changes or the available
  230. screen size changes.
  231. */
  232. virtual void handleScreenSizeChange();
  233. //==============================================================================
  234. /** This is called to repaint the component into the given context. */
  235. void handlePaint (LowLevelGraphicsContext& contextToPaintTo);
  236. //==============================================================================
  237. /** Sets this window to either be always-on-top or normal.
  238. Some kinds of window might not be able to do this, so should return false.
  239. */
  240. virtual bool setAlwaysOnTop (bool alwaysOnTop) = 0;
  241. /** Brings the window to the top, optionally also giving it keyboard focus. */
  242. virtual void toFront (bool takeKeyboardFocus) = 0;
  243. /** Moves the window to be just behind another one. */
  244. virtual void toBehind (ComponentPeer* other) = 0;
  245. /** Called when the window is brought to the front, either by the OS or by a call
  246. to toFront().
  247. */
  248. void handleBroughtToFront();
  249. //==============================================================================
  250. /** True if the window has the keyboard focus. */
  251. virtual bool isFocused() const = 0;
  252. /** Tries to give the window keyboard focus. */
  253. virtual void grabFocus() = 0;
  254. /** Called when the window gains keyboard focus. */
  255. void handleFocusGain();
  256. /** Called when the window loses keyboard focus. */
  257. void handleFocusLoss();
  258. Component* getLastFocusedSubcomponent() const noexcept;
  259. /** Called when a key is pressed.
  260. For keycode info, see the KeyPress class.
  261. Returns true if the keystroke was used.
  262. */
  263. bool handleKeyPress (int keyCode, juce_wchar textCharacter);
  264. /** Called when a key is pressed.
  265. Returns true if the keystroke was used.
  266. */
  267. bool handleKeyPress (const KeyPress& key);
  268. /** Called whenever a key is pressed or released.
  269. Returns true if the keystroke was used.
  270. */
  271. bool handleKeyUpOrDown (bool isKeyDown);
  272. /** Called whenever a modifier key is pressed or released. */
  273. void handleModifierKeysChange();
  274. /** If there's a currently active input-method context - i.e. characters are being
  275. composed using multiple keystrokes - this should commit the current state of the
  276. context to the text and clear the context. This should not hide the virtual keyboard.
  277. */
  278. virtual void closeInputMethodContext();
  279. /** Alerts the peer that the current text input target has changed somehow.
  280. The peer may hide or show the virtual keyboard as a result of this call.
  281. */
  282. void refreshTextInputTarget();
  283. //==============================================================================
  284. /** Returns the currently focused TextInputTarget, or null if none is found. */
  285. TextInputTarget* findCurrentTextInputTarget();
  286. //==============================================================================
  287. /** Invalidates a region of the window to be repainted asynchronously. */
  288. virtual void repaint (const Rectangle<int>& area) = 0;
  289. /** This can be called (from the message thread) to cause the immediate redrawing
  290. of any areas of this window that need repainting.
  291. You shouldn't ever really need to use this, it's mainly for special purposes
  292. like supporting audio plugins where the host's event loop is out of our control.
  293. */
  294. virtual void performAnyPendingRepaintsNow() = 0;
  295. /** Changes the window's transparency. */
  296. virtual void setAlpha (float newAlpha) = 0;
  297. //==============================================================================
  298. void handleMouseEvent (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer, ModifierKeys newMods, float pressure,
  299. float orientation, int64 time, PenDetails pen = {}, int touchIndex = 0);
  300. void handleMouseWheel (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer,
  301. int64 time, const MouseWheelDetails&, int touchIndex = 0);
  302. void handleMagnifyGesture (MouseInputSource::InputSourceType type, Point<float> positionWithinPeer,
  303. int64 time, float scaleFactor, int touchIndex = 0);
  304. void handleUserClosingWindow();
  305. /** Structure to describe drag and drop information */
  306. struct DragInfo
  307. {
  308. StringArray files;
  309. String text;
  310. Point<int> position;
  311. bool isEmpty() const noexcept { return files.size() == 0 && text.isEmpty(); }
  312. void clear() noexcept { files.clear(); text.clear(); }
  313. };
  314. bool handleDragMove (const DragInfo&);
  315. bool handleDragExit (const DragInfo&);
  316. bool handleDragDrop (const DragInfo&);
  317. //==============================================================================
  318. /** Returns the number of currently-active peers.
  319. @see getPeer
  320. */
  321. static int getNumPeers() noexcept;
  322. /** Returns one of the currently-active peers.
  323. @see getNumPeers
  324. */
  325. static ComponentPeer* getPeer (int index) noexcept;
  326. /** Returns the peer that's attached to the given component, or nullptr if there isn't one. */
  327. static ComponentPeer* getPeerFor (const Component*) noexcept;
  328. /** Checks if this peer object is valid.
  329. @see getNumPeers
  330. */
  331. static bool isValidPeer (const ComponentPeer* peer) noexcept;
  332. //==============================================================================
  333. virtual StringArray getAvailableRenderingEngines() = 0;
  334. virtual int getCurrentRenderingEngine() const;
  335. virtual void setCurrentRenderingEngine (int index);
  336. //==============================================================================
  337. /** On desktop platforms this method will check all the mouse and key states and return
  338. a ModifierKeys object representing them.
  339. This isn't recommended and is only needed in special circumstances for up-to-date
  340. modifier information at times when the app's event loop isn't running normally.
  341. Another reason to avoid this method is that it's not stateless and calling it may
  342. update the ModifierKeys::currentModifiers object, which could cause subtle changes
  343. in the behaviour of some components.
  344. */
  345. static ModifierKeys getCurrentModifiersRealtime() noexcept;
  346. //==============================================================================
  347. /** Used to receive callbacks when the OS scale factor of this ComponentPeer changes.
  348. This is used internally by some native JUCE windows on Windows and Linux and you
  349. shouldn't need to worry about it in your own code unless you are dealing directly
  350. with native windows.
  351. */
  352. struct JUCE_API ScaleFactorListener
  353. {
  354. /** Destructor. */
  355. virtual ~ScaleFactorListener() = default;
  356. /** Called when the scale factor changes. */
  357. virtual void nativeScaleFactorChanged (double newScaleFactor) = 0;
  358. };
  359. /** Adds a scale factor listener. */
  360. void addScaleFactorListener (ScaleFactorListener* listenerToAdd) { scaleFactorListeners.add (listenerToAdd); }
  361. /** Removes a scale factor listener. */
  362. void removeScaleFactorListener (ScaleFactorListener* listenerToRemove) { scaleFactorListeners.remove (listenerToRemove); }
  363. //==============================================================================
  364. /** On Windows and Linux this will return the OS scaling factor currently being applied
  365. to the native window. This is used to convert between physical and logical pixels
  366. at the OS API level and you shouldn't need to use it in your own code unless you
  367. are dealing directly with the native window.
  368. */
  369. virtual double getPlatformScaleFactor() const noexcept { return 1.0; }
  370. /** On platforms that support it, this will update the window's titlebar in some
  371. way to indicate that the window's document needs saving.
  372. */
  373. virtual void setHasChangedSinceSaved (bool) {}
  374. enum class Style
  375. {
  376. /** A style that matches the system-wide style. */
  377. automatic,
  378. /** A light style, which will probably use dark text on a light background. */
  379. light,
  380. /** A dark style, which will probably use light text on a dark background. */
  381. dark
  382. };
  383. /** On operating systems that support it, this will update the style of this
  384. peer as requested.
  385. Note that this will not update the theme system-wide. This will only
  386. update UI elements so that they display appropriately for this peer!
  387. */
  388. void setAppStyle (Style s)
  389. {
  390. if (std::exchange (style, s) != style)
  391. appStyleChanged();
  392. }
  393. /** Returns the style requested for this app. */
  394. Style getAppStyle() const { return style; }
  395. protected:
  396. //==============================================================================
  397. static void forceDisplayUpdate();
  398. Component& component;
  399. const int styleFlags;
  400. Rectangle<int> lastNonFullscreenBounds;
  401. ComponentBoundsConstrainer* constrainer = nullptr;
  402. static std::function<ModifierKeys()> getNativeRealtimeModifiers;
  403. ListenerList<ScaleFactorListener> scaleFactorListeners;
  404. Style style = Style::automatic;
  405. private:
  406. //==============================================================================
  407. virtual void appStyleChanged() {}
  408. /** Tells the window that text input may be required at the given position.
  409. This may cause things like a virtual on-screen keyboard to appear, depending
  410. on the OS.
  411. This function should not be called directly by Components - use refreshTextInputTarget
  412. instead.
  413. */
  414. virtual void textInputRequired (Point<int>, TextInputTarget&) = 0;
  415. /** If there's some kind of OS input-method in progress, this should dismiss it.
  416. Overrides of this function should call closeInputMethodContext().
  417. This function should not be called directly by Components - use refreshTextInputTarget
  418. instead.
  419. */
  420. virtual void dismissPendingTextInput();
  421. void globalFocusChanged (Component*) override;
  422. Component* getTargetForKeyPress();
  423. WeakReference<Component> lastFocusedComponent, dragAndDropTargetComponent;
  424. Component* lastDragAndDropCompUnderMouse = nullptr;
  425. TextInputTarget* textInputTarget = nullptr;
  426. const uint32 uniqueID;
  427. bool isWindowMinimised = false;
  428. //==============================================================================
  429. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentPeer)
  430. };
  431. } // namespace juce