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.

2385 lines
110KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. The base class for all JUCE user-interface objects.
  18. @tags{GUI}
  19. */
  20. class JUCE_API Component : public MouseListener
  21. {
  22. public:
  23. //==============================================================================
  24. /** Creates a component.
  25. To get it to actually appear, you'll also need to:
  26. - Either add it to a parent component or use the addToDesktop() method to
  27. make it a desktop window
  28. - Set its size and position to something sensible
  29. - Use setVisible() to make it visible
  30. And for it to serve any useful purpose, you'll need to write a
  31. subclass of Component or use one of the other types of component from
  32. the library.
  33. */
  34. Component() noexcept;
  35. /** Destructor.
  36. Note that when a component is deleted, any child components it contains are NOT
  37. automatically deleted. It's your responsibility to manage their lifespan - you
  38. may want to use helper methods like deleteAllChildren(), or less haphazard
  39. approaches like using std::unique_ptrs or normal object aggregation to manage them.
  40. If the component being deleted is currently the child of another one, then during
  41. deletion, it will be removed from its parent, and the parent will receive a childrenChanged()
  42. callback. Any ComponentListener objects that have registered with it will also have their
  43. ComponentListener::componentBeingDeleted() methods called.
  44. */
  45. ~Component() override;
  46. //==============================================================================
  47. /** Creates a component, setting its name at the same time.
  48. @see getName, setName
  49. */
  50. explicit Component (const String& componentName) noexcept;
  51. /** Returns the name of this component.
  52. @see setName
  53. */
  54. const String& getName() const noexcept { return componentName; }
  55. /** Sets the name of this component.
  56. When the name changes, all registered ComponentListeners will receive a
  57. ComponentListener::componentNameChanged() callback.
  58. @see getName
  59. */
  60. virtual void setName (const String& newName);
  61. /** Returns the ID string that was set by setComponentID().
  62. @see setComponentID, findChildWithID
  63. */
  64. const String& getComponentID() const noexcept { return componentID; }
  65. /** Sets the component's ID string.
  66. You can retrieve the ID using getComponentID().
  67. @see getComponentID, findChildWithID
  68. */
  69. void setComponentID (const String& newID);
  70. //==============================================================================
  71. /** Makes the component visible or invisible.
  72. This method will show or hide the component.
  73. Note that components default to being non-visible when first created.
  74. Also note that visible components won't be seen unless all their parent components
  75. are also visible.
  76. This method will call visibilityChanged() and also componentVisibilityChanged()
  77. for any component listeners that are interested in this component.
  78. @param shouldBeVisible whether to show or hide the component
  79. @see isVisible, isShowing, visibilityChanged, ComponentListener::componentVisibilityChanged
  80. */
  81. virtual void setVisible (bool shouldBeVisible);
  82. /** Tests whether the component is visible or not.
  83. this doesn't necessarily tell you whether this comp is actually on the screen
  84. because this depends on whether all the parent components are also visible - use
  85. isShowing() to find this out.
  86. @see isShowing, setVisible
  87. */
  88. bool isVisible() const noexcept { return flags.visibleFlag; }
  89. /** Called when this component's visibility changes.
  90. @see setVisible, isVisible
  91. */
  92. virtual void visibilityChanged();
  93. /** Tests whether this component and all its parents are visible.
  94. @returns true only if this component and all its parents are visible.
  95. @see isVisible
  96. */
  97. bool isShowing() const;
  98. //==============================================================================
  99. /** Makes this component appear as a window on the desktop.
  100. Note that before calling this, you should make sure that the component's opacity is
  101. set correctly using setOpaque(). If the component is non-opaque, the windowing
  102. system will try to create a special transparent window for it, which will generally take
  103. a lot more CPU to operate (and might not even be possible on some platforms).
  104. If the component is inside a parent component at the time this method is called, it
  105. will first be removed from that parent. Likewise if a component is on the desktop
  106. and is subsequently added to another component, it'll be removed from the desktop.
  107. @param windowStyleFlags a combination of the flags specified in the
  108. ComponentPeer::StyleFlags enum, which define the
  109. window's characteristics.
  110. @param nativeWindowToAttachTo this allows an OS object to be passed-in as the window
  111. in which the juce component should place itself. On Windows,
  112. this would be a HWND, a HIViewRef on the Mac. Not necessarily
  113. supported on all platforms, and best left as 0 unless you know
  114. what you're doing.
  115. @see removeFromDesktop, isOnDesktop, userTriedToCloseWindow,
  116. getPeer, ComponentPeer::setMinimised, ComponentPeer::StyleFlags,
  117. ComponentPeer::getStyleFlags, ComponentPeer::setFullScreen
  118. */
  119. virtual void addToDesktop (int windowStyleFlags,
  120. void* nativeWindowToAttachTo = nullptr);
  121. /** If the component is currently showing on the desktop, this will hide it.
  122. You can also use setVisible() to hide a desktop window temporarily, but
  123. removeFromDesktop() will free any system resources that are being used up.
  124. @see addToDesktop, isOnDesktop
  125. */
  126. void removeFromDesktop();
  127. /** Returns true if this component is currently showing on the desktop.
  128. @see addToDesktop, removeFromDesktop
  129. */
  130. bool isOnDesktop() const noexcept;
  131. /** Returns the heavyweight window that contains this component.
  132. If this component is itself on the desktop, this will return the window
  133. object that it is using. Otherwise, it will return the window of
  134. its top-level parent component.
  135. This may return nullptr if there isn't a desktop component.
  136. @see addToDesktop, isOnDesktop
  137. */
  138. ComponentPeer* getPeer() const;
  139. /** For components on the desktop, this is called if the system wants to close the window.
  140. This is a signal that either the user or the system wants the window to close. The
  141. default implementation of this method will trigger an assertion to warn you that your
  142. component should do something about it, but you can override this to ignore the event
  143. if you want.
  144. */
  145. virtual void userTriedToCloseWindow();
  146. /** Called for a desktop component which has just been minimised or un-minimised.
  147. This will only be called for components on the desktop.
  148. @see getPeer, ComponentPeer::setMinimised, ComponentPeer::isMinimised
  149. */
  150. virtual void minimisationStateChanged (bool isNowMinimised);
  151. /** Returns the default scale factor to use for this component when it is placed
  152. on the desktop.
  153. The default implementation of this method just returns the value from
  154. Desktop::getGlobalScaleFactor(), but it can be overridden if a particular component
  155. has different requirements. The method only used if this component is added
  156. to the desktop - it has no effect for child components.
  157. */
  158. virtual float getDesktopScaleFactor() const;
  159. //==============================================================================
  160. /** Brings the component to the front of its siblings.
  161. If some of the component's siblings have had their 'always-on-top' flag set,
  162. then they will still be kept in front of this one (unless of course this
  163. one is also 'always-on-top').
  164. @param shouldAlsoGainFocus if true, this will also try to assign keyboard focus
  165. to the component (see grabKeyboardFocus() for more details)
  166. @see toBack, toBehind, setAlwaysOnTop
  167. */
  168. void toFront (bool shouldAlsoGainFocus);
  169. /** Changes this component's z-order to be at the back of all its siblings.
  170. If the component is set to be 'always-on-top', it will only be moved to the
  171. back of the other other 'always-on-top' components.
  172. @see toFront, toBehind, setAlwaysOnTop
  173. */
  174. void toBack();
  175. /** Changes this component's z-order so that it's just behind another component.
  176. @see toFront, toBack
  177. */
  178. void toBehind (Component* other);
  179. /** Sets whether the component should always be kept at the front of its siblings.
  180. @see isAlwaysOnTop
  181. */
  182. void setAlwaysOnTop (bool shouldStayOnTop);
  183. /** Returns true if this component is set to always stay in front of its siblings.
  184. @see setAlwaysOnTop
  185. */
  186. bool isAlwaysOnTop() const noexcept;
  187. //==============================================================================
  188. /** Returns the x coordinate of the component's left edge.
  189. This is a distance in pixels from the left edge of the component's parent.
  190. Note that if you've used setTransform() to apply a transform, then the component's
  191. bounds will no longer be a direct reflection of the position at which it appears within
  192. its parent, as the transform will be applied to its bounding box.
  193. */
  194. int getX() const noexcept { return boundsRelativeToParent.getX(); }
  195. /** Returns the y coordinate of the top of this component.
  196. This is a distance in pixels from the top edge of the component's parent.
  197. Note that if you've used setTransform() to apply a transform, then the component's
  198. bounds will no longer be a direct reflection of the position at which it appears within
  199. its parent, as the transform will be applied to its bounding box.
  200. */
  201. int getY() const noexcept { return boundsRelativeToParent.getY(); }
  202. /** Returns the component's width in pixels. */
  203. int getWidth() const noexcept { return boundsRelativeToParent.getWidth(); }
  204. /** Returns the component's height in pixels. */
  205. int getHeight() const noexcept { return boundsRelativeToParent.getHeight(); }
  206. /** Returns the x coordinate of the component's right-hand edge.
  207. This is a distance in pixels from the left edge of the component's parent.
  208. Note that if you've used setTransform() to apply a transform, then the component's
  209. bounds will no longer be a direct reflection of the position at which it appears within
  210. its parent, as the transform will be applied to its bounding box.
  211. */
  212. int getRight() const noexcept { return boundsRelativeToParent.getRight(); }
  213. /** Returns the component's top-left position as a Point. */
  214. Point<int> getPosition() const noexcept { return boundsRelativeToParent.getPosition(); }
  215. /** Returns the y coordinate of the bottom edge of this component.
  216. This is a distance in pixels from the top edge of the component's parent.
  217. Note that if you've used setTransform() to apply a transform, then the component's
  218. bounds will no longer be a direct reflection of the position at which it appears within
  219. its parent, as the transform will be applied to its bounding box.
  220. */
  221. int getBottom() const noexcept { return boundsRelativeToParent.getBottom(); }
  222. /** Returns this component's bounding box.
  223. The rectangle returned is relative to the top-left of the component's parent.
  224. Note that if you've used setTransform() to apply a transform, then the component's
  225. bounds will no longer be a direct reflection of the position at which it appears within
  226. its parent, as the transform will be applied to its bounding box.
  227. */
  228. Rectangle<int> getBounds() const noexcept { return boundsRelativeToParent; }
  229. /** Returns the component's bounds, relative to its own origin.
  230. This is like getBounds(), but returns the rectangle in local coordinates, In practice, it'll
  231. return a rectangle with position (0, 0), and the same size as this component.
  232. */
  233. Rectangle<int> getLocalBounds() const noexcept;
  234. /** Returns the area of this component's parent which this component covers.
  235. The returned area is relative to the parent's coordinate space.
  236. If the component has an affine transform specified, then the resulting area will be
  237. the smallest rectangle that fully covers the component's transformed bounding box.
  238. If this component has no parent, the return value will simply be the same as getBounds().
  239. */
  240. Rectangle<int> getBoundsInParent() const noexcept;
  241. //==============================================================================
  242. /** Returns this component's x coordinate relative the screen's top-left origin.
  243. @see getX, localPointToGlobal
  244. */
  245. int getScreenX() const;
  246. /** Returns this component's y coordinate relative the screen's top-left origin.
  247. @see getY, localPointToGlobal
  248. */
  249. int getScreenY() const;
  250. /** Returns the position of this component's top-left corner relative to the screen's top-left.
  251. @see getScreenBounds
  252. */
  253. Point<int> getScreenPosition() const;
  254. /** Returns the bounds of this component, relative to the screen's top-left.
  255. @see getScreenPosition
  256. */
  257. Rectangle<int> getScreenBounds() const;
  258. /** Converts a point to be relative to this component's coordinate space.
  259. This takes a point relative to a different component, and returns its position relative to this
  260. component. If the sourceComponent parameter is null, the source point is assumed to be a global
  261. screen coordinate.
  262. */
  263. Point<int> getLocalPoint (const Component* sourceComponent,
  264. Point<int> pointRelativeToSourceComponent) const;
  265. /** Converts a point to be relative to this component's coordinate space.
  266. This takes a point relative to a different component, and returns its position relative to this
  267. component. If the sourceComponent parameter is null, the source point is assumed to be a global
  268. screen coordinate.
  269. */
  270. Point<float> getLocalPoint (const Component* sourceComponent,
  271. Point<float> pointRelativeToSourceComponent) const;
  272. /** Converts a rectangle to be relative to this component's coordinate space.
  273. This takes a rectangle that is relative to a different component, and returns its position relative
  274. to this component. If the sourceComponent parameter is null, the source rectangle is assumed to be
  275. a screen coordinate.
  276. If you've used setTransform() to apply one or more transforms to components, then the source rectangle
  277. may not actually be rectangular when converted to the target space, so in that situation this will return
  278. the smallest rectangle that fully contains the transformed area.
  279. */
  280. Rectangle<int> getLocalArea (const Component* sourceComponent,
  281. Rectangle<int> areaRelativeToSourceComponent) const;
  282. /** Converts a point relative to this component's top-left into a screen coordinate.
  283. @see getLocalPoint, localAreaToGlobal
  284. */
  285. Point<int> localPointToGlobal (Point<int> localPoint) const;
  286. /** Converts a point relative to this component's top-left into a screen coordinate.
  287. @see getLocalPoint, localAreaToGlobal
  288. */
  289. Point<float> localPointToGlobal (Point<float> localPoint) const;
  290. /** Converts a rectangle from this component's coordinate space to a screen coordinate.
  291. If you've used setTransform() to apply one or more transforms to components, then the source rectangle
  292. may not actually be rectangular when converted to the target space, so in that situation this will return
  293. the smallest rectangle that fully contains the transformed area.
  294. @see getLocalPoint, localPointToGlobal
  295. */
  296. Rectangle<int> localAreaToGlobal (Rectangle<int> localArea) const;
  297. //==============================================================================
  298. /** Moves the component to a new position.
  299. Changes the component's top-left position (without changing its size).
  300. The position is relative to the top-left of the component's parent.
  301. If the component actually moves, this method will make a synchronous call to moved().
  302. Note that if you've used setTransform() to apply a transform, then the component's
  303. bounds will no longer be a direct reflection of the position at which it appears within
  304. its parent, as the transform will be applied to whatever bounds you set for it.
  305. @see setBounds, ComponentListener::componentMovedOrResized
  306. */
  307. void setTopLeftPosition (int x, int y);
  308. /** Moves the component to a new position.
  309. Changes the component's top-left position (without changing its size).
  310. The position is relative to the top-left of the component's parent.
  311. If the component actually moves, this method will make a synchronous call to moved().
  312. Note that if you've used setTransform() to apply a transform, then the component's
  313. bounds will no longer be a direct reflection of the position at which it appears within
  314. its parent, as the transform will be applied to whatever bounds you set for it.
  315. @see setBounds, ComponentListener::componentMovedOrResized
  316. */
  317. void setTopLeftPosition (Point<int> newTopLeftPosition);
  318. /** Moves the component to a new position.
  319. Changes the position of the component's top-right corner (keeping it the same size).
  320. The position is relative to the top-left of the component's parent.
  321. If the component actually moves, this method will make a synchronous call to moved().
  322. Note that if you've used setTransform() to apply a transform, then the component's
  323. bounds will no longer be a direct reflection of the position at which it appears within
  324. its parent, as the transform will be applied to whatever bounds you set for it.
  325. */
  326. void setTopRightPosition (int x, int y);
  327. /** Changes the size of the component.
  328. A synchronous call to resized() will occur if the size actually changes.
  329. Note that if you've used setTransform() to apply a transform, then the component's
  330. bounds will no longer be a direct reflection of the position at which it appears within
  331. its parent, as the transform will be applied to whatever bounds you set for it.
  332. */
  333. void setSize (int newWidth, int newHeight);
  334. /** Changes the component's position and size.
  335. The coordinates are relative to the top-left of the component's parent, or relative
  336. to the origin of the screen if the component is on the desktop.
  337. If this method changes the component's top-left position, it will make a synchronous
  338. call to moved(). If it changes the size, it will also make a call to resized().
  339. Note that if you've used setTransform() to apply a transform, then the component's
  340. bounds will no longer be a direct reflection of the position at which it appears within
  341. its parent, as the transform will be applied to whatever bounds you set for it.
  342. @see setTopLeftPosition, setSize, ComponentListener::componentMovedOrResized
  343. */
  344. void setBounds (int x, int y, int width, int height);
  345. /** Changes the component's position and size.
  346. The coordinates are relative to the top-left of the component's parent, or relative
  347. to the origin of the screen if the component is on the desktop.
  348. If this method changes the component's top-left position, it will make a synchronous
  349. call to moved(). If it changes the size, it will also make a call to resized().
  350. Note that if you've used setTransform() to apply a transform, then the component's
  351. bounds will no longer be a direct reflection of the position at which it appears within
  352. its parent, as the transform will be applied to whatever bounds you set for it.
  353. @see setBounds
  354. */
  355. void setBounds (Rectangle<int> newBounds);
  356. /** Changes the component's position and size in terms of fractions of its parent's size.
  357. The values are factors of the parent's size, so for example
  358. setBoundsRelative (0.2f, 0.2f, 0.5f, 0.5f) would give it half the
  359. width and height of the parent, with its top-left position 20% of
  360. the way across and down the parent.
  361. @see setBounds
  362. */
  363. void setBoundsRelative (float proportionalX, float proportionalY,
  364. float proportionalWidth, float proportionalHeight);
  365. /** Changes the component's position and size in terms of fractions of its parent's size.
  366. The values are factors of the parent's size, so for example
  367. setBoundsRelative ({ 0.2f, 0.2f, 0.5f, 0.5f }) would give it half the
  368. width and height of the parent, with its top-left position 20% of
  369. the way across and down the parent.
  370. @see setBounds
  371. */
  372. void setBoundsRelative (Rectangle<float> proportionalArea);
  373. /** Changes the component's position and size based on the amount of space to leave around it.
  374. This will position the component within its parent, leaving the specified number of
  375. pixels around each edge.
  376. @see setBounds
  377. */
  378. void setBoundsInset (BorderSize<int> borders);
  379. /** Positions the component within a given rectangle, keeping its proportions
  380. unchanged.
  381. If onlyReduceInSize is false, the component will be resized to fill as much of the
  382. rectangle as possible without changing its aspect ratio (the component's
  383. current size is used to determine its aspect ratio, so a zero-size component
  384. won't work here). If onlyReduceInSize is true, it will only be resized if it's
  385. too big to fit inside the rectangle.
  386. It will then be positioned within the rectangle according to the justification flags
  387. specified.
  388. @see setBounds
  389. */
  390. void setBoundsToFit (Rectangle<int> targetArea,
  391. Justification justification,
  392. bool onlyReduceInSize);
  393. /** Changes the position of the component's centre.
  394. Leaves the component's size unchanged, but sets the position of its centre
  395. relative to its parent's top-left.
  396. @see setBounds
  397. */
  398. void setCentrePosition (int x, int y);
  399. /** Changes the position of the component's centre.
  400. Leaves the component's size unchanged, but sets the position of its centre
  401. relative to its parent's top-left.
  402. @see setBounds
  403. */
  404. void setCentrePosition (Point<int> newCentrePosition);
  405. /** Changes the position of the component's centre.
  406. Leaves the size unchanged, but positions its centre relative to its parent's size.
  407. E.g. setCentreRelative (0.5f, 0.5f) would place it centrally in its parent.
  408. */
  409. void setCentreRelative (float x, float y);
  410. /** Changes the component's size and centres it within its parent.
  411. After changing the size, the component will be moved so that it's
  412. centred within its parent. If the component is on the desktop (or has no
  413. parent component), then it'll be centred within the main monitor area.
  414. */
  415. void centreWithSize (int width, int height);
  416. //==============================================================================
  417. /** Sets a transform matrix to be applied to this component.
  418. If you set a transform for a component, the component's position will be warped by it, relative to
  419. the component's parent's top-left origin. This means that the values you pass into setBounds() will no
  420. longer reflect the actual area within the parent that the component covers, as the bounds will be
  421. transformed and the component will probably end up actually appearing somewhere else within its parent.
  422. When using transforms you need to be extremely careful when converting coordinates between the
  423. coordinate spaces of different components or the screen - you should always use getLocalPoint(),
  424. getLocalArea(), etc to do this, and never just manually add a component's position to a point in order to
  425. convert it between different components (but I'm sure you would never have done that anyway...).
  426. Currently, transforms are not supported for desktop windows, so the transform will be ignored if you
  427. put a component on the desktop.
  428. To remove a component's transform, simply pass AffineTransform() as the parameter to this method.
  429. */
  430. void setTransform (const AffineTransform& transform);
  431. /** Returns the transform that is currently being applied to this component.
  432. For more details about transforms, see setTransform().
  433. @see setTransform
  434. */
  435. AffineTransform getTransform() const;
  436. /** Returns true if a non-identity transform is being applied to this component.
  437. For more details about transforms, see setTransform().
  438. @see setTransform
  439. */
  440. bool isTransformed() const noexcept;
  441. /** Returns the approximate scale factor for a given component by traversing its parent hierarchy
  442. and applying each transform and finally scaling this by the global scale factor.
  443. */
  444. static float JUCE_CALLTYPE getApproximateScaleFactorForComponent (Component* targetComponent);
  445. //==============================================================================
  446. /** Returns a proportion of the component's width.
  447. This is a handy equivalent of (getWidth() * proportion).
  448. */
  449. int proportionOfWidth (float proportion) const noexcept;
  450. /** Returns a proportion of the component's height.
  451. This is a handy equivalent of (getHeight() * proportion).
  452. */
  453. int proportionOfHeight (float proportion) const noexcept;
  454. /** Returns the width of the component's parent.
  455. If the component has no parent (i.e. if it's on the desktop), this will return
  456. the width of the screen.
  457. */
  458. int getParentWidth() const noexcept;
  459. /** Returns the height of the component's parent.
  460. If the component has no parent (i.e. if it's on the desktop), this will return
  461. the height of the screen.
  462. */
  463. int getParentHeight() const noexcept;
  464. /** Returns the screen coordinates of the monitor that contains this component.
  465. If there's only one monitor, this will return its size - if there are multiple
  466. monitors, it will return the area of the monitor that contains the component's
  467. centre.
  468. */
  469. Rectangle<int> getParentMonitorArea() const;
  470. //==============================================================================
  471. /** Returns the number of child components that this component contains.
  472. @see getChildren, getChildComponent, getIndexOfChildComponent
  473. */
  474. int getNumChildComponents() const noexcept;
  475. /** Returns one of this component's child components, by it index.
  476. The component with index 0 is at the back of the z-order, the one at the
  477. front will have index (getNumChildComponents() - 1).
  478. If the index is out-of-range, this will return a null pointer.
  479. @see getChildren, getNumChildComponents, getIndexOfChildComponent
  480. */
  481. Component* getChildComponent (int index) const noexcept;
  482. /** Returns the index of this component in the list of child components.
  483. A value of 0 means it is first in the list (i.e. behind all other components). Higher
  484. values are further towards the front.
  485. Returns -1 if the component passed-in is not a child of this component.
  486. @see getChildren, getNumChildComponents, getChildComponent, addChildComponent, toFront, toBack, toBehind
  487. */
  488. int getIndexOfChildComponent (const Component* child) const noexcept;
  489. /** Provides access to the underlying array of child components.
  490. The most likely reason you may want to use this is for iteration in a range-based for loop.
  491. */
  492. const Array<Component*>& getChildren() const noexcept { return childComponentList; }
  493. /** Looks for a child component with the specified ID.
  494. @see setComponentID, getComponentID
  495. */
  496. Component* findChildWithID (StringRef componentID) const noexcept;
  497. /** Adds a child component to this one.
  498. Adding a child component does not mean that the component will own or delete the child - it's
  499. your responsibility to delete the component. Note that it's safe to delete a component
  500. without first removing it from its parent - doing so will automatically remove it and
  501. send out the appropriate notifications before the deletion completes.
  502. If the child is already a child of this component, then no action will be taken, and its
  503. z-order will be left unchanged.
  504. @param child the new component to add. If the component passed-in is already
  505. the child of another component, it'll first be removed from it current parent.
  506. @param zOrder The index in the child-list at which this component should be inserted.
  507. A value of -1 will insert it in front of the others, 0 is the back.
  508. @see removeChildComponent, addAndMakeVisible, addChildAndSetID, getChild, ComponentListener::componentChildrenChanged
  509. */
  510. void addChildComponent (Component* child, int zOrder = -1);
  511. /** Adds a child component to this one.
  512. Adding a child component does not mean that the component will own or delete the child - it's
  513. your responsibility to delete the component. Note that it's safe to delete a component
  514. without first removing it from its parent - doing so will automatically remove it and
  515. send out the appropriate notifications before the deletion completes.
  516. If the child is already a child of this component, then no action will be taken, and its
  517. z-order will be left unchanged.
  518. @param child the new component to add. If the component passed-in is already
  519. the child of another component, it'll first be removed from it current parent.
  520. @param zOrder The index in the child-list at which this component should be inserted.
  521. A value of -1 will insert it in front of the others, 0 is the back.
  522. @see removeChildComponent, addAndMakeVisible, addChildAndSetID, getChild, ComponentListener::componentChildrenChanged
  523. */
  524. void addChildComponent (Component& child, int zOrder = -1);
  525. /** Adds a child component to this one, and also makes the child visible if it isn't already.
  526. This is the same as calling setVisible (true) on the child and then addChildComponent().
  527. See addChildComponent() for more details.
  528. @param child the new component to add. If the component passed-in is already
  529. the child of another component, it'll first be removed from it current parent.
  530. @param zOrder The index in the child-list at which this component should be inserted.
  531. A value of -1 will insert it in front of the others, 0 is the back.
  532. */
  533. void addAndMakeVisible (Component* child, int zOrder = -1);
  534. /** Adds a child component to this one, and also makes the child visible if it isn't already.
  535. This is the same as calling setVisible (true) on the child and then addChildComponent().
  536. See addChildComponent() for more details.
  537. @param child the new component to add. If the component passed-in is already
  538. the child of another component, it'll first be removed from it current parent.
  539. @param zOrder The index in the child-list at which this component should be inserted.
  540. A value of -1 will insert it in front of the others, 0 is the back.
  541. */
  542. void addAndMakeVisible (Component& child, int zOrder = -1);
  543. /** Adds a child component to this one, makes it visible, and sets its component ID.
  544. @see addAndMakeVisible, addChildComponent
  545. */
  546. void addChildAndSetID (Component* child, const String& componentID);
  547. /** Removes one of this component's child-components.
  548. If the child passed-in isn't actually a child of this component (either because
  549. it's invalid or is the child of a different parent), then no action is taken.
  550. Note that removing a child will not delete it! But it's ok to delete a component
  551. without first removing it - doing so will automatically remove it and send out the
  552. appropriate notifications before the deletion completes.
  553. @see addChildComponent, ComponentListener::componentChildrenChanged
  554. */
  555. void removeChildComponent (Component* childToRemove);
  556. /** Removes one of this component's child-components by index.
  557. This will return a pointer to the component that was removed, or null if
  558. the index was out-of-range.
  559. Note that removing a child will not delete it! But it's ok to delete a component
  560. without first removing it - doing so will automatically remove it and send out the
  561. appropriate notifications before the deletion completes.
  562. @see addChildComponent, ComponentListener::componentChildrenChanged
  563. */
  564. Component* removeChildComponent (int childIndexToRemove);
  565. /** Removes all this component's children.
  566. Note that this won't delete them! To do that, use deleteAllChildren() instead.
  567. */
  568. void removeAllChildren();
  569. /** Removes and deletes all of this component's children.
  570. My advice is to avoid this method! It's an old function that is only kept here for
  571. backwards-compatibility with legacy code, and should be viewed with extreme
  572. suspicion by anyone attempting to write modern C++. In almost all cases, it's much
  573. smarter to manage the lifetimes of your child components via modern RAII techniques
  574. such as simply making them member variables, or using std::unique_ptr, OwnedArray,
  575. etc to manage their lifetimes appropriately.
  576. @see removeAllChildren
  577. */
  578. void deleteAllChildren();
  579. /** Returns the component which this component is inside.
  580. If this is the highest-level component or hasn't yet been added to
  581. a parent, this will return null.
  582. */
  583. Component* getParentComponent() const noexcept { return parentComponent; }
  584. /** Searches the parent components for a component of a specified class.
  585. For example findParentComponentOfClass \<MyComp\>() would return the first parent
  586. component that can be dynamically cast to a MyComp, or will return nullptr if none
  587. of the parents are suitable.
  588. */
  589. template <class TargetClass>
  590. TargetClass* findParentComponentOfClass() const
  591. {
  592. for (auto* p = parentComponent; p != nullptr; p = p->parentComponent)
  593. if (auto* target = dynamic_cast<TargetClass*> (p))
  594. return target;
  595. return nullptr;
  596. }
  597. /** Returns the highest-level component which contains this one or its parents.
  598. This will search upwards in the parent-hierarchy from this component, until it
  599. finds the highest one that doesn't have a parent (i.e. is on the desktop or
  600. not yet added to a parent), and will return that.
  601. */
  602. Component* getTopLevelComponent() const noexcept;
  603. /** Checks whether a component is anywhere inside this component or its children.
  604. This will recursively check through this component's children to see if the
  605. given component is anywhere inside.
  606. */
  607. bool isParentOf (const Component* possibleChild) const noexcept;
  608. //==============================================================================
  609. /** Called to indicate that the component's parents have changed.
  610. When a component is added or removed from its parent, this method will
  611. be called on all of its children (recursively - so all children of its
  612. children will also be called as well).
  613. Subclasses can override this if they need to react to this in some way.
  614. @see getParentComponent, isShowing, ComponentListener::componentParentHierarchyChanged
  615. */
  616. virtual void parentHierarchyChanged();
  617. /** Subclasses can use this callback to be told when children are added or removed, or
  618. when their z-order changes.
  619. @see parentHierarchyChanged, ComponentListener::componentChildrenChanged
  620. */
  621. virtual void childrenChanged();
  622. //==============================================================================
  623. /** Tests whether a given point is inside the component.
  624. Overriding this method allows you to create components which only intercept
  625. mouse-clicks within a user-defined area.
  626. This is called to find out whether a particular x, y coordinate is
  627. considered to be inside the component or not, and is used by methods such
  628. as contains() and getComponentAt() to work out which component
  629. the mouse is clicked on.
  630. Components with custom shapes will probably want to override it to perform
  631. some more complex hit-testing.
  632. The default implementation of this method returns either true or false,
  633. depending on the value that was set by calling setInterceptsMouseClicks() (true
  634. is the default return value).
  635. Note that the hit-test region is not related to the opacity with which
  636. areas of a component are painted.
  637. Applications should never call hitTest() directly - instead use the
  638. contains() method, because this will also test for occlusion by the
  639. component's parent.
  640. Note that for components on the desktop, this method will be ignored, because it's
  641. not always possible to implement this behaviour on all platforms.
  642. @param x the x coordinate to test, relative to the left hand edge of this
  643. component. This value is guaranteed to be greater than or equal to
  644. zero, and less than the component's width
  645. @param y the y coordinate to test, relative to the top edge of this
  646. component. This value is guaranteed to be greater than or equal to
  647. zero, and less than the component's height
  648. @returns true if the click is considered to be inside the component
  649. @see setInterceptsMouseClicks, contains
  650. */
  651. virtual bool hitTest (int x, int y);
  652. /** Changes the default return value for the hitTest() method.
  653. Setting this to false is an easy way to make a component pass all its mouse events
  654. (not just clicks) through to the components behind it.
  655. When a component is created, the default setting for this is true.
  656. @param allowClicksOnThisComponent if true, hitTest() will always return true; if false, it will
  657. return false (or true for child components if allowClicksOnChildComponents
  658. is true)
  659. @param allowClicksOnChildComponents if this is true and allowClicksOnThisComponent is false, then child
  660. components can be clicked on as normal but clicks on this component pass
  661. straight through; if this is false and allowClicksOnThisComponent
  662. is false, then neither this component nor any child components can
  663. be clicked on
  664. @see hitTest, getInterceptsMouseClicks
  665. */
  666. void setInterceptsMouseClicks (bool allowClicksOnThisComponent,
  667. bool allowClicksOnChildComponents) noexcept;
  668. /** Retrieves the current state of the mouse-click interception flags.
  669. On return, the two parameters are set to the state used in the last call to
  670. setInterceptsMouseClicks().
  671. @see setInterceptsMouseClicks
  672. */
  673. void getInterceptsMouseClicks (bool& allowsClicksOnThisComponent,
  674. bool& allowsClicksOnChildComponents) const noexcept;
  675. /** Returns true if a given point lies within this component or one of its children.
  676. Never override this method! Use hitTest to create custom hit regions.
  677. @param localPoint the coordinate to test, relative to this component's top-left.
  678. @returns true if the point is within the component's hit-test area, but only if
  679. that part of the component isn't clipped by its parent component. Note
  680. that this won't take into account any overlapping sibling components
  681. which might be in the way - for that, see reallyContains()
  682. @see hitTest, reallyContains, getComponentAt
  683. */
  684. bool contains (Point<int> localPoint);
  685. /** Returns true if a given point lies in this component, taking any overlapping
  686. siblings into account.
  687. @param localPoint the coordinate to test, relative to this component's top-left.
  688. @param returnTrueIfWithinAChild if the point actually lies within a child of this component,
  689. this determines whether that is counted as a hit.
  690. @see contains, getComponentAt
  691. */
  692. bool reallyContains (Point<int> localPoint, bool returnTrueIfWithinAChild);
  693. /** Returns the component at a certain point within this one.
  694. @param x the x coordinate to test, relative to this component's left edge.
  695. @param y the y coordinate to test, relative to this component's top edge.
  696. @returns the component that is at this position - which may be 0, this component,
  697. or one of its children. Note that overlapping siblings that might actually
  698. be in the way are not taken into account by this method - to account for these,
  699. instead call getComponentAt on the top-level parent of this component.
  700. @see hitTest, contains, reallyContains
  701. */
  702. Component* getComponentAt (int x, int y);
  703. /** Returns the component at a certain point within this one.
  704. @param position the coordinate to test, relative to this component's top-left.
  705. @returns the component that is at this position - which may be 0, this component,
  706. or one of its children. Note that overlapping siblings that might actually
  707. be in the way are not taken into account by this method - to account for these,
  708. instead call getComponentAt on the top-level parent of this component.
  709. @see hitTest, contains, reallyContains
  710. */
  711. Component* getComponentAt (Point<int> position);
  712. //==============================================================================
  713. /** Marks the whole component as needing to be redrawn.
  714. Calling this will not do any repainting immediately, but will mark the component
  715. as 'dirty'. At some point in the near future the operating system will send a paint
  716. message, which will redraw all the dirty regions of all components.
  717. There's no guarantee about how soon after calling repaint() the redraw will actually
  718. happen, and other queued events may be delivered before a redraw is done.
  719. If the setBufferedToImage() method has been used to cause this component to use a
  720. buffer, the repaint() call will invalidate the cached buffer. If setCachedComponentImage()
  721. has been used to provide a custom image cache, that cache will be invalidated appropriately.
  722. To redraw just a subsection of the component rather than the whole thing,
  723. use the repaint (int, int, int, int) method.
  724. @see paint
  725. */
  726. void repaint();
  727. /** Marks a subsection of this component as needing to be redrawn.
  728. Calling this will not do any repainting immediately, but will mark the given region
  729. of the component as 'dirty'. At some point in the near future the operating system
  730. will send a paint message, which will redraw all the dirty regions of all components.
  731. There's no guarantee about how soon after calling repaint() the redraw will actually
  732. happen, and other queued events may be delivered before a redraw is done.
  733. The region that is passed in will be clipped to keep it within the bounds of this
  734. component.
  735. @see repaint()
  736. */
  737. void repaint (int x, int y, int width, int height);
  738. /** Marks a subsection of this component as needing to be redrawn.
  739. Calling this will not do any repainting immediately, but will mark the given region
  740. of the component as 'dirty'. At some point in the near future the operating system
  741. will send a paint message, which will redraw all the dirty regions of all components.
  742. There's no guarantee about how soon after calling repaint() the redraw will actually
  743. happen, and other queued events may be delivered before a redraw is done.
  744. The region that is passed in will be clipped to keep it within the bounds of this
  745. component.
  746. @see repaint()
  747. */
  748. void repaint (Rectangle<int> area);
  749. //==============================================================================
  750. /** Makes the component use an internal buffer to optimise its redrawing.
  751. Setting this flag to true will cause the component to allocate an
  752. internal buffer into which it paints itself and all its child components, so that
  753. when asked to redraw itself, it can use this buffer rather than actually calling
  754. the paint() method.
  755. Parts of the buffer are invalidated when repaint() is called on this component
  756. or its children. The buffer is then repainted at the next paint() callback.
  757. @see repaint, paint, createComponentSnapshot
  758. */
  759. void setBufferedToImage (bool shouldBeBuffered);
  760. /** Generates a snapshot of part of this component.
  761. This will return a new Image, the size of the rectangle specified,
  762. containing a snapshot of the specified area of the component and all
  763. its children.
  764. The image may or may not have an alpha-channel, depending on whether the
  765. image is opaque or not.
  766. If the clipImageToComponentBounds parameter is true and the area is greater than
  767. the size of the component, it'll be clipped. If clipImageToComponentBounds is false
  768. then parts of the component beyond its bounds can be drawn.
  769. @see paintEntireComponent
  770. */
  771. Image createComponentSnapshot (Rectangle<int> areaToGrab,
  772. bool clipImageToComponentBounds = true,
  773. float scaleFactor = 1.0f);
  774. /** Draws this component and all its subcomponents onto the specified graphics
  775. context.
  776. You should very rarely have to use this method, it's simply there in case you need
  777. to draw a component with a custom graphics context for some reason, e.g. for
  778. creating a snapshot of the component.
  779. It calls paint(), paintOverChildren() and recursively calls paintEntireComponent()
  780. on its children in order to render the entire tree.
  781. The graphics context may be left in an undefined state after this method returns,
  782. so you may need to reset it if you're going to use it again.
  783. If ignoreAlphaLevel is false, then the component will be drawn with the opacity level
  784. specified by getAlpha(); if ignoreAlphaLevel is true, then this will be ignored and
  785. an alpha of 1.0 will be used.
  786. */
  787. void paintEntireComponent (Graphics& context, bool ignoreAlphaLevel);
  788. /** This allows you to indicate that this component doesn't require its graphics
  789. context to be clipped when it is being painted.
  790. Most people will never need to use this setting, but in situations where you have a very large
  791. number of simple components being rendered, and where they are guaranteed never to do any drawing
  792. beyond their own boundaries, setting this to true will reduce the overhead involved in clipping
  793. the graphics context that gets passed to the component's paint() callback.
  794. If you enable this mode, you'll need to make sure your paint method doesn't call anything like
  795. Graphics::fillAll(), and doesn't draw beyond the component's bounds, because that'll produce
  796. artifacts. Your component also can't have any child components that may be placed beyond its
  797. bounds.
  798. */
  799. void setPaintingIsUnclipped (bool shouldPaintWithoutClipping) noexcept;
  800. /** Returns true if this component doesn't require its graphics context to be clipped
  801. when it is being painted.
  802. */
  803. bool isPaintingUnclipped() const noexcept;
  804. //==============================================================================
  805. /** Adds an effect filter to alter the component's appearance.
  806. When a component has an effect filter set, then this is applied to the
  807. results of its paint() method. There are a few preset effects, such as
  808. a drop-shadow or glow, but they can be user-defined as well.
  809. The effect that is passed in will not be deleted by the component - the
  810. caller must take care of deleting it.
  811. To remove an effect from a component, pass a null pointer in as the parameter.
  812. @see ImageEffectFilter, DropShadowEffect, GlowEffect
  813. */
  814. void setComponentEffect (ImageEffectFilter* newEffect);
  815. /** Returns the current component effect.
  816. @see setComponentEffect
  817. */
  818. ImageEffectFilter* getComponentEffect() const noexcept { return effect; }
  819. //==============================================================================
  820. /** Finds the appropriate look-and-feel to use for this component.
  821. If the component hasn't had a look-and-feel explicitly set, this will
  822. return the parent's look-and-feel, or just the default one if there's no
  823. parent.
  824. @see setLookAndFeel, lookAndFeelChanged
  825. */
  826. LookAndFeel& getLookAndFeel() const noexcept;
  827. /** Sets the look and feel to use for this component.
  828. This will also change the look and feel for any child components that haven't
  829. had their look set explicitly.
  830. The object passed in will not be deleted by the component, so it's the caller's
  831. responsibility to manage it. It may be used at any time until this component
  832. has been deleted.
  833. Calling this method will also invoke the sendLookAndFeelChange() method.
  834. @see getLookAndFeel, lookAndFeelChanged
  835. */
  836. void setLookAndFeel (LookAndFeel* newLookAndFeel);
  837. /** Called to let the component react to a change in the look-and-feel setting.
  838. When the look-and-feel is changed for a component, this will be called in
  839. all its child components, recursively.
  840. It can also be triggered manually by the sendLookAndFeelChange() method, in case
  841. an application uses a LookAndFeel class that might have changed internally.
  842. @see sendLookAndFeelChange, getLookAndFeel
  843. */
  844. virtual void lookAndFeelChanged();
  845. /** Calls the lookAndFeelChanged() method in this component and all its children.
  846. This will recurse through the children and their children, calling lookAndFeelChanged()
  847. on them all.
  848. @see lookAndFeelChanged
  849. */
  850. void sendLookAndFeelChange();
  851. //==============================================================================
  852. /** Indicates whether any parts of the component might be transparent.
  853. Components that always paint all of their contents with solid colour and
  854. thus completely cover any components behind them should use this method
  855. to tell the repaint system that they are opaque.
  856. This information is used to optimise drawing, because it means that
  857. objects underneath opaque windows don't need to be painted.
  858. By default, components are considered transparent, unless this is used to
  859. make it otherwise.
  860. @see isOpaque
  861. */
  862. void setOpaque (bool shouldBeOpaque);
  863. /** Returns true if no parts of this component are transparent.
  864. @returns the value that was set by setOpaque, (the default being false)
  865. @see setOpaque
  866. */
  867. bool isOpaque() const noexcept;
  868. //==============================================================================
  869. /** Indicates whether the component should be brought to the front when clicked.
  870. Setting this flag to true will cause the component to be brought to the front
  871. when the mouse is clicked somewhere inside it or its child components.
  872. Note that a top-level desktop window might still be brought to the front by the
  873. operating system when it's clicked, depending on how the OS works.
  874. By default this is set to false.
  875. @see setMouseClickGrabsKeyboardFocus
  876. */
  877. void setBroughtToFrontOnMouseClick (bool shouldBeBroughtToFront) noexcept;
  878. /** Indicates whether the component should be brought to the front when clicked-on.
  879. @see setBroughtToFrontOnMouseClick
  880. */
  881. bool isBroughtToFrontOnMouseClick() const noexcept;
  882. //==============================================================================
  883. // Keyboard focus methods
  884. /** Sets a flag to indicate whether this component needs keyboard focus or not.
  885. By default components aren't actually interested in gaining the
  886. focus, but this method can be used to turn this on.
  887. See the grabKeyboardFocus() method for details about the way a component
  888. is chosen to receive the focus.
  889. @see grabKeyboardFocus, getWantsKeyboardFocus
  890. */
  891. void setWantsKeyboardFocus (bool wantsFocus) noexcept;
  892. /** Returns true if the component is interested in getting keyboard focus.
  893. This returns the flag set by setWantsKeyboardFocus(). The default
  894. setting is false.
  895. @see setWantsKeyboardFocus
  896. */
  897. bool getWantsKeyboardFocus() const noexcept;
  898. //==============================================================================
  899. /** Chooses whether a click on this component automatically grabs the focus.
  900. By default this is set to true, but you might want a component which can
  901. be focused, but where you don't want the user to be able to affect it directly
  902. by clicking.
  903. */
  904. void setMouseClickGrabsKeyboardFocus (bool shouldGrabFocus);
  905. /** Returns the last value set with setMouseClickGrabsKeyboardFocus().
  906. See setMouseClickGrabsKeyboardFocus() for more info.
  907. */
  908. bool getMouseClickGrabsKeyboardFocus() const noexcept;
  909. //==============================================================================
  910. /** Tries to give keyboard focus to this component.
  911. When the user clicks on a component or its grabKeyboardFocus()
  912. method is called, the following procedure is used to work out which
  913. component should get it:
  914. - if the component that was clicked on actually wants focus (as indicated
  915. by calling getWantsKeyboardFocus), it gets it.
  916. - if the component itself doesn't want focus, it will try to pass it
  917. on to whichever of its children is the default component, as determined by
  918. KeyboardFocusTraverser::getDefaultComponent()
  919. - if none of its children want focus at all, it will pass it up to its
  920. parent instead, unless it's a top-level component without a parent,
  921. in which case it just takes the focus itself.
  922. Important note! It's obviously not possible for a component to be focused
  923. unless it's actually visible, on-screen, and inside a window that is also
  924. visible. So there's no point trying to call this in the component's own
  925. constructor or before all of its parent hierarchy has been fully instantiated.
  926. @see setWantsKeyboardFocus, getWantsKeyboardFocus, hasKeyboardFocus,
  927. getCurrentlyFocusedComponent, focusGained, focusLost,
  928. keyPressed, keyStateChanged
  929. */
  930. void grabKeyboardFocus();
  931. /** Returns true if this component currently has the keyboard focus.
  932. @param trueIfChildIsFocused if this is true, then the method returns true if
  933. either this component or any of its children (recursively)
  934. have the focus. If false, the method only returns true if
  935. this component has the focus.
  936. @see grabKeyboardFocus, setWantsKeyboardFocus, getCurrentlyFocusedComponent,
  937. focusGained, focusLost
  938. */
  939. bool hasKeyboardFocus (bool trueIfChildIsFocused) const;
  940. /** Returns the component that currently has the keyboard focus.
  941. @returns the focused component, or null if nothing is focused.
  942. */
  943. static Component* JUCE_CALLTYPE getCurrentlyFocusedComponent() noexcept;
  944. /** If any component has keyboard focus, this will defocus it. */
  945. static void JUCE_CALLTYPE unfocusAllComponents();
  946. //==============================================================================
  947. /** Tries to move the keyboard focus to one of this component's siblings.
  948. This will try to move focus to either the next or previous component. (This
  949. is the method that is used when shifting focus by pressing the tab key).
  950. Components for which getWantsKeyboardFocus() returns false are not looked at.
  951. @param moveToNext if true, the focus will move forwards; if false, it will
  952. move backwards
  953. @see grabKeyboardFocus, setFocusContainer, setWantsKeyboardFocus
  954. */
  955. void moveKeyboardFocusToSibling (bool moveToNext);
  956. /** Creates a KeyboardFocusTraverser object to use to determine the logic by
  957. which focus should be passed from this component.
  958. The default implementation of this method will return a default
  959. KeyboardFocusTraverser if this component is a focus container (as determined
  960. by the setFocusContainer() method). If the component isn't a focus
  961. container, then it will recursively ask its parents for a KeyboardFocusTraverser.
  962. If you override this to return a custom KeyboardFocusTraverser, then
  963. this component and all its sub-components will use the new object to
  964. make their focusing decisions.
  965. The method should return a new object, which the caller is required to
  966. delete when no longer needed.
  967. */
  968. virtual KeyboardFocusTraverser* createFocusTraverser();
  969. /** Returns the focus order of this component, if one has been specified.
  970. By default components don't have a focus order - in that case, this
  971. will return 0. Lower numbers indicate that the component will be
  972. earlier in the focus traversal order.
  973. To change the order, call setExplicitFocusOrder().
  974. The focus order may be used by the KeyboardFocusTraverser class as part of
  975. its algorithm for deciding the order in which components should be traversed.
  976. See the KeyboardFocusTraverser class for more details on this.
  977. @see moveKeyboardFocusToSibling, createFocusTraverser, KeyboardFocusTraverser
  978. */
  979. int getExplicitFocusOrder() const;
  980. /** Sets the index used in determining the order in which focusable components
  981. should be traversed.
  982. A value of 0 or less is taken to mean that no explicit order is wanted, and
  983. that traversal should use other factors, like the component's position.
  984. @see getExplicitFocusOrder, moveKeyboardFocusToSibling
  985. */
  986. void setExplicitFocusOrder (int newFocusOrderIndex);
  987. /** Indicates whether this component is a parent for components that can have
  988. their focus traversed.
  989. This flag is used by the default implementation of the createFocusTraverser()
  990. method, which uses the flag to find the first parent component (of the currently
  991. focused one) which wants to be a focus container.
  992. So using this method to set the flag to 'true' causes this component to
  993. act as the top level within which focus is passed around.
  994. @see isFocusContainer, createFocusTraverser, moveKeyboardFocusToSibling
  995. */
  996. void setFocusContainer (bool shouldBeFocusContainer) noexcept;
  997. /** Returns true if this component has been marked as a focus container.
  998. See setFocusContainer() for more details.
  999. @see setFocusContainer, moveKeyboardFocusToSibling, createFocusTraverser
  1000. */
  1001. bool isFocusContainer() const noexcept;
  1002. //==============================================================================
  1003. /** Returns true if the component (and all its parents) are enabled.
  1004. Components are enabled by default, and can be disabled with setEnabled(). Exactly
  1005. what difference this makes to the component depends on the type. E.g. buttons
  1006. and sliders will choose to draw themselves differently, etc.
  1007. Note that if one of this component's parents is disabled, this will always
  1008. return false, even if this component itself is enabled.
  1009. @see setEnabled, enablementChanged
  1010. */
  1011. bool isEnabled() const noexcept;
  1012. /** Enables or disables this component.
  1013. Disabling a component will also cause all of its child components to become
  1014. disabled.
  1015. Similarly, enabling a component which is inside a disabled parent
  1016. component won't make any difference until the parent is re-enabled.
  1017. @see isEnabled, enablementChanged
  1018. */
  1019. void setEnabled (bool shouldBeEnabled);
  1020. /** Callback to indicate that this component has been enabled or disabled.
  1021. This can be triggered by one of the component's parent components
  1022. being enabled or disabled, as well as changes to the component itself.
  1023. The default implementation of this method does nothing; your class may
  1024. wish to repaint itself or something when this happens.
  1025. @see setEnabled, isEnabled
  1026. */
  1027. virtual void enablementChanged();
  1028. //==============================================================================
  1029. /** Returns the component's current transparency level.
  1030. See setAlpha() for more details.
  1031. */
  1032. float getAlpha() const noexcept;
  1033. /** Changes the transparency of this component.
  1034. When painted, the entire component and all its children will be rendered
  1035. with this as the overall opacity level, where 0 is completely invisible, and
  1036. 1.0 is fully opaque (i.e. normal).
  1037. @see getAlpha, alphaChanged
  1038. */
  1039. void setAlpha (float newAlpha);
  1040. /** Called when setAlpha() is used to change the alpha value of this component.
  1041. If you override this, you should also invoke the base class's implementation
  1042. during your overridden function, as it performs some repainting behaviour.
  1043. */
  1044. virtual void alphaChanged();
  1045. //==============================================================================
  1046. /** Changes the mouse cursor shape to use when the mouse is over this component.
  1047. Note that the cursor set by this method can be overridden by the getMouseCursor
  1048. method.
  1049. @see MouseCursor
  1050. */
  1051. void setMouseCursor (const MouseCursor& cursorType);
  1052. /** Returns the mouse cursor shape to use when the mouse is over this component.
  1053. The default implementation will return the cursor that was set by setCursor()
  1054. but can be overridden for more specialised purposes, e.g. returning different
  1055. cursors depending on the mouse position.
  1056. @see MouseCursor
  1057. */
  1058. virtual MouseCursor getMouseCursor();
  1059. /** Forces the current mouse cursor to be updated.
  1060. If you're overriding the getMouseCursor() method to control which cursor is
  1061. displayed, then this will only be checked each time the user moves the mouse. So
  1062. if you want to force the system to check that the cursor being displayed is
  1063. up-to-date (even if the mouse is just sitting there), call this method.
  1064. (If you're changing the cursor using setMouseCursor(), you don't need to bother
  1065. calling this).
  1066. */
  1067. void updateMouseCursor() const;
  1068. //==============================================================================
  1069. /** Components can override this method to draw their content.
  1070. The paint() method gets called when a region of a component needs redrawing,
  1071. either because the component's repaint() method has been called, or because
  1072. something has happened on the screen that means a section of a window needs
  1073. to be redrawn.
  1074. Any child components will draw themselves over whatever this method draws. If
  1075. you need to paint over the top of your child components, you can also implement
  1076. the paintOverChildren() method to do this.
  1077. If you want to cause a component to redraw itself, this is done asynchronously -
  1078. calling the repaint() method marks a region of the component as "dirty", and the
  1079. paint() method will automatically be called sometime later, by the message thread,
  1080. to paint any bits that need refreshing. In JUCE (and almost all modern UI frameworks),
  1081. you never redraw something synchronously.
  1082. You should never need to call this method directly - to take a snapshot of the
  1083. component you could use createComponentSnapshot() or paintEntireComponent().
  1084. @param g the graphics context that must be used to do the drawing operations.
  1085. @see repaint, paintOverChildren, Graphics
  1086. */
  1087. virtual void paint (Graphics& g);
  1088. /** Components can override this method to draw over the top of their children.
  1089. For most drawing operations, it's better to use the normal paint() method,
  1090. but if you need to overlay something on top of the children, this can be
  1091. used.
  1092. @see paint, Graphics
  1093. */
  1094. virtual void paintOverChildren (Graphics& g);
  1095. //==============================================================================
  1096. /** Called when the mouse moves inside a component.
  1097. If the mouse button isn't pressed and the mouse moves over a component,
  1098. this will be called to let the component react to this.
  1099. A component will always get a mouseEnter callback before a mouseMove.
  1100. @param event details about the position and status of the mouse event, including
  1101. the source component in which it occurred
  1102. @see mouseEnter, mouseExit, mouseDrag, contains
  1103. */
  1104. void mouseMove (const MouseEvent& event) override;
  1105. /** Called when the mouse first enters a component.
  1106. If the mouse button isn't pressed and the mouse moves into a component,
  1107. this will be called to let the component react to this.
  1108. When the mouse button is pressed and held down while being moved in
  1109. or out of a component, no mouseEnter or mouseExit callbacks are made - only
  1110. mouseDrag messages are sent to the component that the mouse was originally
  1111. clicked on, until the button is released.
  1112. @param event details about the position and status of the mouse event, including
  1113. the source component in which it occurred
  1114. @see mouseExit, mouseDrag, mouseMove, contains
  1115. */
  1116. void mouseEnter (const MouseEvent& event) override;
  1117. /** Called when the mouse moves out of a component.
  1118. This will be called when the mouse moves off the edge of this
  1119. component.
  1120. If the mouse button was pressed, and it was then dragged off the
  1121. edge of the component and released, then this callback will happen
  1122. when the button is released, after the mouseUp callback.
  1123. @param event details about the position and status of the mouse event, including
  1124. the source component in which it occurred
  1125. @see mouseEnter, mouseDrag, mouseMove, contains
  1126. */
  1127. void mouseExit (const MouseEvent& event) override;
  1128. /** Called when a mouse button is pressed.
  1129. The MouseEvent object passed in contains lots of methods for finding out
  1130. which button was pressed, as well as which modifier keys (e.g. shift, ctrl)
  1131. were held down at the time.
  1132. Once a button is held down, the mouseDrag method will be called when the
  1133. mouse moves, until the button is released.
  1134. @param event details about the position and status of the mouse event, including
  1135. the source component in which it occurred
  1136. @see mouseUp, mouseDrag, mouseDoubleClick, contains
  1137. */
  1138. void mouseDown (const MouseEvent& event) override;
  1139. /** Called when the mouse is moved while a button is held down.
  1140. When a mouse button is pressed inside a component, that component
  1141. receives mouseDrag callbacks each time the mouse moves, even if the
  1142. mouse strays outside the component's bounds.
  1143. @param event details about the position and status of the mouse event, including
  1144. the source component in which it occurred
  1145. @see mouseDown, mouseUp, mouseMove, contains, setDragRepeatInterval
  1146. */
  1147. void mouseDrag (const MouseEvent& event) override;
  1148. /** Called when a mouse button is released.
  1149. A mouseUp callback is sent to the component in which a button was pressed
  1150. even if the mouse is actually over a different component when the
  1151. button is released.
  1152. The MouseEvent object passed in contains lots of methods for finding out
  1153. which buttons were down just before they were released.
  1154. @param event details about the position and status of the mouse event, including
  1155. the source component in which it occurred
  1156. @see mouseDown, mouseDrag, mouseDoubleClick, contains
  1157. */
  1158. void mouseUp (const MouseEvent& event) override;
  1159. /** Called when a mouse button has been double-clicked on a component.
  1160. The MouseEvent object passed in contains lots of methods for finding out
  1161. which button was pressed, as well as which modifier keys (e.g. shift, ctrl)
  1162. were held down at the time.
  1163. @param event details about the position and status of the mouse event, including
  1164. the source component in which it occurred
  1165. @see mouseDown, mouseUp
  1166. */
  1167. void mouseDoubleClick (const MouseEvent& event) override;
  1168. /** Called when the mouse-wheel is moved.
  1169. This callback is sent to the component that the mouse is over when the
  1170. wheel is moved.
  1171. If not overridden, a component will forward this message to its parent, so
  1172. that parent components can collect mouse-wheel messages that happen to
  1173. child components which aren't interested in them. (Bear in mind that if
  1174. you attach a component as a mouse-listener to other components, then
  1175. those wheel moves will also end up calling this method and being passed up
  1176. to the parents, which may not be what you intended to happen).
  1177. @param event details about the mouse event
  1178. @param wheel details about the mouse wheel movement
  1179. */
  1180. void mouseWheelMove (const MouseEvent& event,
  1181. const MouseWheelDetails& wheel) override;
  1182. /** Called when a pinch-to-zoom mouse-gesture is used.
  1183. If not overridden, a component will forward this message to its parent, so
  1184. that parent components can collect gesture messages that are unused by child
  1185. components.
  1186. @param event details about the mouse event
  1187. @param scaleFactor a multiplier to indicate by how much the size of the target
  1188. should be changed. A value of 1.0 would indicate no change,
  1189. values greater than 1.0 mean it should be enlarged.
  1190. */
  1191. void mouseMagnify (const MouseEvent& event, float scaleFactor) override;
  1192. //==============================================================================
  1193. /** Ensures that a non-stop stream of mouse-drag events will be sent during the
  1194. current mouse-drag operation.
  1195. This allows you to make sure that mouseDrag() events are sent continuously, even
  1196. when the mouse isn't moving. This can be useful for things like auto-scrolling
  1197. components when the mouse is near an edge.
  1198. Call this method during a mouseDown() or mouseDrag() callback, specifying the
  1199. minimum interval between consecutive mouse drag callbacks. The callbacks
  1200. will continue until the mouse is released, and then the interval will be reset,
  1201. so you need to make sure it's called every time you begin a drag event.
  1202. Passing an interval of 0 or less will cancel the auto-repeat.
  1203. @see mouseDrag, Desktop::beginDragAutoRepeat
  1204. */
  1205. static void JUCE_CALLTYPE beginDragAutoRepeat (int millisecondsBetweenCallbacks);
  1206. /** Causes automatic repaints when the mouse enters or exits this component.
  1207. If turned on, then when the mouse enters/exits, or when the button is pressed/released
  1208. on the component, it will trigger a repaint.
  1209. This is handy for things like buttons that need to draw themselves differently when
  1210. the mouse moves over them, and it avoids having to override all the different mouse
  1211. callbacks and call repaint().
  1212. @see mouseEnter, mouseExit, mouseDown, mouseUp
  1213. */
  1214. void setRepaintsOnMouseActivity (bool shouldRepaint) noexcept;
  1215. /** Registers a listener to be told when mouse events occur in this component.
  1216. If you need to get informed about mouse events in a component but can't or
  1217. don't want to override its methods, you can attach any number of listeners
  1218. to the component, and these will get told about the events in addition to
  1219. the component's own callbacks being called.
  1220. Note that a MouseListener can also be attached to more than one component.
  1221. @param newListener the listener to register
  1222. @param wantsEventsForAllNestedChildComponents if true, the listener will receive callbacks
  1223. for events that happen to any child component
  1224. within this component, including deeply-nested
  1225. child components. If false, it will only be
  1226. told about events that this component handles.
  1227. @see MouseListener, removeMouseListener
  1228. */
  1229. void addMouseListener (MouseListener* newListener,
  1230. bool wantsEventsForAllNestedChildComponents);
  1231. /** Deregisters a mouse listener.
  1232. @see addMouseListener, MouseListener
  1233. */
  1234. void removeMouseListener (MouseListener* listenerToRemove);
  1235. //==============================================================================
  1236. /** Adds a listener that wants to hear about keypresses that this component receives.
  1237. The listeners that are registered with a component are called by its keyPressed() or
  1238. keyStateChanged() methods (assuming these haven't been overridden to do something else).
  1239. If you add an object as a key listener, be careful to remove it when the object
  1240. is deleted, or the component will be left with a dangling pointer.
  1241. @see keyPressed, keyStateChanged, removeKeyListener
  1242. */
  1243. void addKeyListener (KeyListener* newListener);
  1244. /** Removes a previously-registered key listener.
  1245. @see addKeyListener
  1246. */
  1247. void removeKeyListener (KeyListener* listenerToRemove);
  1248. /** Called when a key is pressed.
  1249. When a key is pressed, the component that has the keyboard focus will have this
  1250. method called. Remember that a component will only be given the focus if its
  1251. setWantsKeyboardFocus() method has been used to enable this.
  1252. If your implementation returns true, the event will be consumed and not passed
  1253. on to any other listeners. If it returns false, the key will be passed to any
  1254. KeyListeners that have been registered with this component. As soon as one of these
  1255. returns true, the process will stop, but if they all return false, the event will
  1256. be passed upwards to this component's parent, and so on.
  1257. The default implementation of this method does nothing and returns false.
  1258. @see keyStateChanged, getCurrentlyFocusedComponent, addKeyListener
  1259. */
  1260. virtual bool keyPressed (const KeyPress& key);
  1261. /** Called when a key is pressed or released.
  1262. Whenever a key on the keyboard is pressed or released (including modifier keys
  1263. like shift and ctrl), this method will be called on the component that currently
  1264. has the keyboard focus. Remember that a component will only be given the focus if
  1265. its setWantsKeyboardFocus() method has been used to enable this.
  1266. If your implementation returns true, the event will be consumed and not passed
  1267. on to any other listeners. If it returns false, then any KeyListeners that have
  1268. been registered with this component will have their keyStateChanged methods called.
  1269. As soon as one of these returns true, the process will stop, but if they all return
  1270. false, the event will be passed upwards to this component's parent, and so on.
  1271. The default implementation of this method does nothing and returns false.
  1272. To find out which keys are up or down at any time, see the KeyPress::isKeyCurrentlyDown()
  1273. method.
  1274. @param isKeyDown true if a key has been pressed; false if it has been released
  1275. @see keyPressed, KeyPress, getCurrentlyFocusedComponent, addKeyListener
  1276. */
  1277. virtual bool keyStateChanged (bool isKeyDown);
  1278. /** Called when a modifier key is pressed or released.
  1279. Whenever the shift, control, alt or command keys are pressed or released,
  1280. this method will be called on the component that currently has the keyboard focus.
  1281. Remember that a component will only be given the focus if its setWantsKeyboardFocus()
  1282. method has been used to enable this.
  1283. The default implementation of this method actually calls its parent's modifierKeysChanged
  1284. method, so that focused components which aren't interested in this will give their
  1285. parents a chance to act on the event instead.
  1286. @see keyStateChanged, ModifierKeys
  1287. */
  1288. virtual void modifierKeysChanged (const ModifierKeys& modifiers);
  1289. //==============================================================================
  1290. /** Enumeration used by the focusGained() and focusLost() methods. */
  1291. enum FocusChangeType
  1292. {
  1293. focusChangedByMouseClick, /**< Means that the user clicked the mouse to change focus. */
  1294. focusChangedByTabKey, /**< Means that the user pressed the tab key to move the focus. */
  1295. focusChangedDirectly /**< Means that the focus was changed by a call to grabKeyboardFocus(). */
  1296. };
  1297. /** Called to indicate that this component has just acquired the keyboard focus.
  1298. @see focusLost, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus
  1299. */
  1300. virtual void focusGained (FocusChangeType cause);
  1301. /** Called to indicate that this component has just lost the keyboard focus.
  1302. @see focusGained, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus
  1303. */
  1304. virtual void focusLost (FocusChangeType cause);
  1305. /** Called to indicate a change in whether or not this component is the parent of the
  1306. currently-focused component.
  1307. Essentially this is called when the return value of a call to hasKeyboardFocus (true) has
  1308. changed. It happens when focus moves from one of this component's children (at any depth)
  1309. to a component that isn't contained in this one, (or vice-versa).
  1310. Note that this method does NOT get called to when focus simply moves from one of its
  1311. child components to another.
  1312. @see focusGained, setWantsKeyboardFocus, getCurrentlyFocusedComponent, hasKeyboardFocus
  1313. */
  1314. virtual void focusOfChildComponentChanged (FocusChangeType cause);
  1315. //==============================================================================
  1316. /** Returns true if the mouse is currently over this component.
  1317. If the mouse isn't over the component, this will return false, even if the
  1318. mouse is currently being dragged - so you can use this in your mouseDrag
  1319. method to find out whether it's really over the component or not.
  1320. Note that when the mouse button is being held down, then the only component
  1321. for which this method will return true is the one that was originally
  1322. clicked on.
  1323. Also note that on a touch-screen device, this will only return true when a finger
  1324. is actually down - as soon as all touch is released, isMouseOver will always
  1325. return false.
  1326. If includeChildren is true, then this will also return true if the mouse is over
  1327. any of the component's children (recursively) as well as the component itself.
  1328. @see isMouseButtonDown. isMouseOverOrDragging, mouseDrag
  1329. */
  1330. bool isMouseOver (bool includeChildren = false) const;
  1331. /** Returns true if the mouse button is currently held down in this component.
  1332. Note that this is a test to see whether the mouse is being pressed in this
  1333. component, so it'll return false if called on component A when the mouse
  1334. is actually being dragged in component B.
  1335. @see isMouseButtonDownAnywhere, isMouseOver, isMouseOverOrDragging
  1336. */
  1337. bool isMouseButtonDown (bool includeChildren = false) const;
  1338. /** True if the mouse is over this component, or if it's being dragged in this component.
  1339. This is a handy equivalent to (isMouseOver() || isMouseButtonDown()).
  1340. @see isMouseOver, isMouseButtonDown, isMouseButtonDownAnywhere
  1341. */
  1342. bool isMouseOverOrDragging (bool includeChildren = false) const;
  1343. /** Returns true if a mouse button is currently down.
  1344. Unlike isMouseButtonDown, this will test the current state of the
  1345. buttons without regard to which component (if any) it has been
  1346. pressed in.
  1347. @see isMouseButtonDown, ModifierKeys
  1348. */
  1349. static bool JUCE_CALLTYPE isMouseButtonDownAnywhere() noexcept;
  1350. /** Returns the mouse's current position, relative to this component.
  1351. The return value is relative to the component's top-left corner.
  1352. */
  1353. Point<int> getMouseXYRelative() const;
  1354. //==============================================================================
  1355. /** Called when this component's size has been changed.
  1356. A component can implement this method to do things such as laying out its
  1357. child components when its width or height changes.
  1358. The method is called synchronously as a result of the setBounds or setSize
  1359. methods, so repeatedly changing a components size will repeatedly call its
  1360. resized method (unlike things like repainting, where multiple calls to repaint
  1361. are coalesced together).
  1362. If the component is a top-level window on the desktop, its size could also
  1363. be changed by operating-system factors beyond the application's control.
  1364. @see moved, setSize
  1365. */
  1366. virtual void resized();
  1367. /** Called when this component's position has been changed.
  1368. This is called when the position relative to its parent changes, not when
  1369. its absolute position on the screen changes (so it won't be called for
  1370. all child components when a parent component is moved).
  1371. The method is called synchronously as a result of the setBounds, setTopLeftPosition
  1372. or any of the other repositioning methods, and like resized(), it will be
  1373. called each time those methods are called.
  1374. If the component is a top-level window on the desktop, its position could also
  1375. be changed by operating-system factors beyond the application's control.
  1376. @see resized, setBounds
  1377. */
  1378. virtual void moved();
  1379. /** Called when one of this component's children is moved or resized.
  1380. If the parent wants to know about changes to its immediate children (not
  1381. to children of its children), this is the method to override.
  1382. @see moved, resized, parentSizeChanged
  1383. */
  1384. virtual void childBoundsChanged (Component* child);
  1385. /** Called when this component's immediate parent has been resized.
  1386. If the component is a top-level window, this indicates that the screen size
  1387. has changed.
  1388. @see childBoundsChanged, moved, resized
  1389. */
  1390. virtual void parentSizeChanged();
  1391. /** Called when this component has been moved to the front of its siblings.
  1392. The component may have been brought to the front by the toFront() method, or
  1393. by the operating system if it's a top-level window.
  1394. @see toFront
  1395. */
  1396. virtual void broughtToFront();
  1397. /** Adds a listener to be told about changes to the component hierarchy or position.
  1398. Component listeners get called when this component's size, position or children
  1399. change - see the ComponentListener class for more details.
  1400. @param newListener the listener to register - if this is already registered, it
  1401. will be ignored.
  1402. @see ComponentListener, removeComponentListener
  1403. */
  1404. void addComponentListener (ComponentListener* newListener);
  1405. /** Removes a component listener.
  1406. @see addComponentListener
  1407. */
  1408. void removeComponentListener (ComponentListener* listenerToRemove);
  1409. //==============================================================================
  1410. /** Dispatches a numbered message to this component.
  1411. This is a quick and cheap way of allowing simple asynchronous messages to
  1412. be sent to components. It's also safe, because if the component that you
  1413. send the message to is a null or dangling pointer, this won't cause an error.
  1414. The command ID is later delivered to the component's handleCommandMessage() method by
  1415. the application's message queue.
  1416. @see handleCommandMessage
  1417. */
  1418. void postCommandMessage (int commandId);
  1419. /** Called to handle a command that was sent by postCommandMessage().
  1420. This is called by the message thread when a command message arrives, and
  1421. the component can override this method to process it in any way it needs to.
  1422. @see postCommandMessage
  1423. */
  1424. virtual void handleCommandMessage (int commandId);
  1425. //==============================================================================
  1426. #if JUCE_MODAL_LOOPS_PERMITTED
  1427. /** Runs a component modally, waiting until the loop terminates.
  1428. This method first makes the component visible, brings it to the front and
  1429. gives it the keyboard focus.
  1430. It then runs a loop, dispatching messages from the system message queue, but
  1431. blocking all mouse or keyboard messages from reaching any components other
  1432. than this one and its children.
  1433. This loop continues until the component's exitModalState() method is called (or
  1434. the component is deleted), and then this method returns, returning the value
  1435. passed into exitModalState().
  1436. Note that you SHOULD NEVER USE THIS METHOD! Modal loops are a dangerous construct
  1437. because things that happen during the events that they dispatch could affect the
  1438. state of objects which are currently in use somewhere on the stack, so when the
  1439. loop finishes and the stack unwinds, horrible problems can occur. This is especially
  1440. bad in plugins, where the host may choose to delete the plugin during runModalLoop(),
  1441. so that when it returns, the entire DLL could have been unloaded from memory!
  1442. Also, some OSes deliberately make it impossible to run modal loops (e.g. Android),
  1443. so this method won't even exist on some platforms.
  1444. @see enterModalState, exitModalState, isCurrentlyModal, getCurrentlyModalComponent,
  1445. isCurrentlyBlockedByAnotherModalComponent, ModalComponentManager
  1446. */
  1447. int runModalLoop();
  1448. #endif
  1449. /** Puts the component into a modal state.
  1450. This makes the component modal, so that messages are blocked from reaching
  1451. any components other than this one and its children, but unlike runModalLoop(),
  1452. this method returns immediately.
  1453. If takeKeyboardFocus is true, the component will use grabKeyboardFocus() to
  1454. get the focus, which is usually what you'll want it to do. If not, it will leave
  1455. the focus unchanged.
  1456. The callback is an optional object which will receive a callback when the modal
  1457. component loses its modal status, either by being hidden or when exitModalState()
  1458. is called. If you pass an object in here, the system will take care of deleting it
  1459. later, after making the callback
  1460. If deleteWhenDismissed is true, then when it is dismissed, the component will be
  1461. deleted and then the callback will be called. (This will safely handle the situation
  1462. where the component is deleted before its exitModalState() method is called).
  1463. @see exitModalState, runModalLoop, ModalComponentManager::attachCallback
  1464. */
  1465. void enterModalState (bool takeKeyboardFocus = true,
  1466. ModalComponentManager::Callback* callback = nullptr,
  1467. bool deleteWhenDismissed = false);
  1468. /** Ends a component's modal state.
  1469. If this component is currently modal, this will turn off its modalness, and return
  1470. a value to the runModalLoop() method that might have be running its modal loop.
  1471. @see runModalLoop, enterModalState, isCurrentlyModal
  1472. */
  1473. void exitModalState (int returnValue);
  1474. /** Returns true if this component is the modal one.
  1475. It's possible to have nested modal components, e.g. a pop-up dialog box
  1476. that launches another pop-up. If onlyConsiderForemostModalComponent is
  1477. true then isCurrentlyModal will only return true for the one at the top
  1478. of the stack. If onlyConsiderForemostModalComponent is false then
  1479. isCurrentlyModal will return true for any modal component in the stack.
  1480. @see getCurrentlyModalComponent
  1481. */
  1482. bool isCurrentlyModal (bool onlyConsiderForemostModalComponent = true) const noexcept;
  1483. /** Returns the number of components that are currently in a modal state.
  1484. @see getCurrentlyModalComponent
  1485. */
  1486. static int JUCE_CALLTYPE getNumCurrentlyModalComponents() noexcept;
  1487. /** Returns one of the components that are currently modal.
  1488. The index specifies which of the possible modal components to return. The order
  1489. of the components in this list is the reverse of the order in which they became
  1490. modal - so the component at index 0 is always the active component, and the others
  1491. are progressively earlier ones that are themselves now blocked by later ones.
  1492. @returns the modal component, or null if no components are modal (or if the
  1493. index is out of range)
  1494. @see getNumCurrentlyModalComponents, runModalLoop, isCurrentlyModal
  1495. */
  1496. static Component* JUCE_CALLTYPE getCurrentlyModalComponent (int index = 0) noexcept;
  1497. /** Checks whether there's a modal component somewhere that's stopping this one
  1498. from receiving messages.
  1499. If there is a modal component, its canModalEventBeSentToComponent() method
  1500. will be called to see if it will still allow this component to receive events.
  1501. @see runModalLoop, getCurrentlyModalComponent
  1502. */
  1503. bool isCurrentlyBlockedByAnotherModalComponent() const;
  1504. /** When a component is modal, this callback allows it to choose which other
  1505. components can still receive events.
  1506. When a modal component is active and the user clicks on a non-modal component,
  1507. this method is called on the modal component, and if it returns true, the
  1508. event is allowed to reach its target. If it returns false, the event is blocked
  1509. and the inputAttemptWhenModal() callback is made.
  1510. It called by the isCurrentlyBlockedByAnotherModalComponent() method. The default
  1511. implementation just returns false in all cases.
  1512. */
  1513. virtual bool canModalEventBeSentToComponent (const Component* targetComponent);
  1514. /** Called when the user tries to click on a component that is blocked by another
  1515. modal component.
  1516. When a component is modal and the user clicks on one of the other components,
  1517. the modal component will receive this callback.
  1518. The default implementation of this method will play a beep, and bring the currently
  1519. modal component to the front, but it can be overridden to do other tasks.
  1520. @see isCurrentlyBlockedByAnotherModalComponent, canModalEventBeSentToComponent
  1521. */
  1522. virtual void inputAttemptWhenModal();
  1523. //==============================================================================
  1524. /** Returns the set of properties that belong to this component.
  1525. Each component has a NamedValueSet object which you can use to attach arbitrary
  1526. items of data to it.
  1527. */
  1528. NamedValueSet& getProperties() noexcept { return properties; }
  1529. /** Returns the set of properties that belong to this component.
  1530. Each component has a NamedValueSet object which you can use to attach arbitrary
  1531. items of data to it.
  1532. */
  1533. const NamedValueSet& getProperties() const noexcept { return properties; }
  1534. //==============================================================================
  1535. /** Looks for a colour that has been registered with the given colour ID number.
  1536. If a colour has been set for this ID number using setColour(), then it is
  1537. returned. If none has been set, the method will try calling the component's
  1538. LookAndFeel class's findColour() method. If none has been registered with the
  1539. look-and-feel either, it will just return black.
  1540. The colour IDs for various purposes are stored as enums in the components that
  1541. they are relevant to - for an example, see Slider::ColourIds,
  1542. Label::ColourIds, TextEditor::ColourIds, TreeView::ColourIds, etc.
  1543. @see setColour, isColourSpecified, colourChanged, LookAndFeel::findColour, LookAndFeel::setColour
  1544. */
  1545. Colour findColour (int colourID, bool inheritFromParent = false) const;
  1546. /** Registers a colour to be used for a particular purpose.
  1547. Changing a colour will cause a synchronous callback to the colourChanged()
  1548. method, which your component can override if it needs to do something when
  1549. colours are altered.
  1550. For more details about colour IDs, see the comments for findColour().
  1551. @see findColour, isColourSpecified, colourChanged, LookAndFeel::findColour, LookAndFeel::setColour
  1552. */
  1553. void setColour (int colourID, Colour newColour);
  1554. /** If a colour has been set with setColour(), this will remove it.
  1555. This allows you to make a colour revert to its default state.
  1556. */
  1557. void removeColour (int colourID);
  1558. /** Returns true if the specified colour ID has been explicitly set for this
  1559. component using the setColour() method.
  1560. */
  1561. bool isColourSpecified (int colourID) const;
  1562. /** This looks for any colours that have been specified for this component,
  1563. and copies them to the specified target component.
  1564. */
  1565. void copyAllExplicitColoursTo (Component& target) const;
  1566. /** This method is called when a colour is changed by the setColour() method.
  1567. @see setColour, findColour
  1568. */
  1569. virtual void colourChanged();
  1570. //==============================================================================
  1571. /** Returns the underlying native window handle for this component.
  1572. This is platform-dependent and strictly for power-users only!
  1573. */
  1574. void* getWindowHandle() const;
  1575. //==============================================================================
  1576. /** Holds a pointer to some type of Component, which automatically becomes null if
  1577. the component is deleted.
  1578. If you're using a component which may be deleted by another event that's outside
  1579. of your control, use a SafePointer instead of a normal pointer to refer to it,
  1580. and you can test whether it's null before using it to see if something has deleted
  1581. it.
  1582. The ComponentType template parameter must be Component, or some subclass of Component.
  1583. You may also want to use a WeakReference<Component> object for the same purpose.
  1584. */
  1585. template <class ComponentType>
  1586. class SafePointer
  1587. {
  1588. public:
  1589. /** Creates a null SafePointer. */
  1590. SafePointer() = default;
  1591. /** Creates a SafePointer that points at the given component. */
  1592. SafePointer (ComponentType* component) : weakRef (component) {}
  1593. /** Creates a copy of another SafePointer. */
  1594. SafePointer (const SafePointer& other) noexcept : weakRef (other.weakRef) {}
  1595. /** Copies another pointer to this one. */
  1596. SafePointer& operator= (const SafePointer& other) { weakRef = other.weakRef; return *this; }
  1597. /** Copies another pointer to this one. */
  1598. SafePointer& operator= (ComponentType* newComponent) { weakRef = newComponent; return *this; }
  1599. /** Returns the component that this pointer refers to, or null if the component no longer exists. */
  1600. ComponentType* getComponent() const noexcept { return dynamic_cast<ComponentType*> (weakRef.get()); }
  1601. /** Returns the component that this pointer refers to, or null if the component no longer exists. */
  1602. operator ComponentType*() const noexcept { return getComponent(); }
  1603. /** Returns the component that this pointer refers to, or null if the component no longer exists. */
  1604. ComponentType* operator->() noexcept { return getComponent(); }
  1605. /** Returns the component that this pointer refers to, or null if the component no longer exists. */
  1606. const ComponentType* operator->() const noexcept { return getComponent(); }
  1607. /** If the component is valid, this deletes it and sets this pointer to null. */
  1608. void deleteAndZero() { delete getComponent(); }
  1609. bool operator== (ComponentType* component) const noexcept { return weakRef == component; }
  1610. bool operator!= (ComponentType* component) const noexcept { return weakRef != component; }
  1611. private:
  1612. WeakReference<Component> weakRef;
  1613. };
  1614. //==============================================================================
  1615. /** A class to keep an eye on a component and check for it being deleted.
  1616. This is designed for use with the ListenerList::callChecked() methods, to allow
  1617. the list iterator to stop cleanly if the component is deleted by a listener callback
  1618. while the list is still being iterated.
  1619. */
  1620. class JUCE_API BailOutChecker
  1621. {
  1622. public:
  1623. /** Creates a checker that watches one component. */
  1624. BailOutChecker (Component* component);
  1625. /** Returns true if either of the two components have been deleted since this object was created. */
  1626. bool shouldBailOut() const noexcept;
  1627. private:
  1628. const WeakReference<Component> safePointer;
  1629. JUCE_DECLARE_NON_COPYABLE (BailOutChecker)
  1630. };
  1631. //==============================================================================
  1632. /**
  1633. Base class for objects that can be used to automatically position a component according to
  1634. some kind of algorithm.
  1635. The component class simply holds onto a reference to a Positioner, but doesn't actually do
  1636. anything with it - all the functionality must be implemented by the positioner itself (e.g.
  1637. it might choose to watch some kind of value and move the component when the value changes).
  1638. */
  1639. class JUCE_API Positioner
  1640. {
  1641. public:
  1642. /** Creates a Positioner which can control the specified component. */
  1643. explicit Positioner (Component& component) noexcept;
  1644. /** Destructor. */
  1645. virtual ~Positioner() = default;
  1646. /** Returns the component that this positioner controls. */
  1647. Component& getComponent() const noexcept { return component; }
  1648. /** Attempts to set the component's position to the given rectangle.
  1649. Unlike simply calling Component::setBounds(), this may involve the positioner
  1650. being smart enough to adjust itself to fit the new bounds.
  1651. */
  1652. virtual void applyNewBounds (const Rectangle<int>& newBounds) = 0;
  1653. private:
  1654. Component& component;
  1655. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Positioner)
  1656. };
  1657. /** Returns the Positioner object that has been set for this component.
  1658. @see setPositioner()
  1659. */
  1660. Positioner* getPositioner() const noexcept;
  1661. /** Sets a new Positioner object for this component.
  1662. If there's currently another positioner set, it will be deleted. The object that is passed in
  1663. will be deleted automatically by this component when it's no longer required. Pass a null pointer
  1664. to clear the current positioner.
  1665. @see getPositioner()
  1666. */
  1667. void setPositioner (Positioner* newPositioner);
  1668. /** Gives the component a CachedComponentImage that should be used to buffer its painting.
  1669. The object that is passed-in will be owned by this component, and will be deleted automatically
  1670. later on.
  1671. @see setBufferedToImage
  1672. */
  1673. void setCachedComponentImage (CachedComponentImage* newCachedImage);
  1674. /** Returns the object that was set by setCachedComponentImage().
  1675. @see setCachedComponentImage
  1676. */
  1677. CachedComponentImage* getCachedComponentImage() const noexcept { return cachedImage.get(); }
  1678. /** Sets a flag to indicate whether mouse drag events on this Component should be ignored when it is inside a
  1679. Viewport with drag-to-scroll functionality enabled. This is useful for Components such as sliders that
  1680. should not move when their parent Viewport when dragged.
  1681. */
  1682. void setViewportIgnoreDragFlag (bool ignoreDrag) noexcept { flags.viewportIgnoreDragFlag = ignoreDrag; }
  1683. /** Retrieves the current state of the Viewport drag-to-scroll functionality flag.
  1684. @see setViewportIgnoreDragFlag
  1685. */
  1686. bool getViewportIgnoreDragFlag() const noexcept { return flags.viewportIgnoreDragFlag; }
  1687. private:
  1688. //==============================================================================
  1689. friend class ComponentPeer;
  1690. friend class MouseInputSource;
  1691. friend class MouseInputSourceInternal;
  1692. #ifndef DOXYGEN
  1693. static Component* currentlyFocusedComponent;
  1694. //==============================================================================
  1695. String componentName, componentID;
  1696. Component* parentComponent = nullptr;
  1697. Rectangle<int> boundsRelativeToParent;
  1698. std::unique_ptr<Positioner> positioner;
  1699. std::unique_ptr<AffineTransform> affineTransform;
  1700. Array<Component*> childComponentList;
  1701. WeakReference<LookAndFeel> lookAndFeel;
  1702. MouseCursor cursor;
  1703. ImageEffectFilter* effect = nullptr;
  1704. std::unique_ptr<CachedComponentImage> cachedImage;
  1705. class MouseListenerList;
  1706. std::unique_ptr<MouseListenerList> mouseListeners;
  1707. std::unique_ptr<Array<KeyListener*>> keyListeners;
  1708. ListenerList<ComponentListener> componentListeners;
  1709. NamedValueSet properties;
  1710. friend class WeakReference<Component>;
  1711. WeakReference<Component>::Master masterReference;
  1712. struct ComponentFlags
  1713. {
  1714. bool hasHeavyweightPeerFlag : 1;
  1715. bool visibleFlag : 1;
  1716. bool opaqueFlag : 1;
  1717. bool ignoresMouseClicksFlag : 1;
  1718. bool allowChildMouseClicksFlag : 1;
  1719. bool wantsFocusFlag : 1;
  1720. bool isFocusContainerFlag : 1;
  1721. bool dontFocusOnMouseClickFlag : 1;
  1722. bool alwaysOnTopFlag : 1;
  1723. bool bufferToImageFlag : 1;
  1724. bool bringToFrontOnClickFlag : 1;
  1725. bool repaintOnMouseActivityFlag : 1;
  1726. bool isDisabledFlag : 1;
  1727. bool childCompFocusedFlag : 1;
  1728. bool dontClipGraphicsFlag : 1;
  1729. bool mouseDownWasBlocked : 1;
  1730. bool isMoveCallbackPending : 1;
  1731. bool isResizeCallbackPending : 1;
  1732. bool viewportIgnoreDragFlag : 1;
  1733. #if JUCE_DEBUG
  1734. bool isInsidePaintCall : 1;
  1735. #endif
  1736. };
  1737. union
  1738. {
  1739. uint32 componentFlags;
  1740. ComponentFlags flags;
  1741. };
  1742. uint8 componentTransparency = 0;
  1743. //==============================================================================
  1744. void internalMouseEnter (MouseInputSource, Point<float>, Time);
  1745. void internalMouseExit (MouseInputSource, Point<float>, Time);
  1746. void internalMouseDown (MouseInputSource, Point<float>, Time, float, float, float, float, float);
  1747. void internalMouseUp (MouseInputSource, Point<float>, Time, const ModifierKeys oldModifiers, float, float, float, float, float);
  1748. void internalMouseDrag (MouseInputSource, Point<float>, Time, float, float, float, float, float);
  1749. void internalMouseMove (MouseInputSource, Point<float>, Time);
  1750. void internalMouseWheel (MouseInputSource, Point<float>, Time, const MouseWheelDetails&);
  1751. void internalMagnifyGesture (MouseInputSource, Point<float>, Time, float);
  1752. void internalBroughtToFront();
  1753. void internalFocusGain (FocusChangeType, const WeakReference<Component>&);
  1754. void internalFocusGain (FocusChangeType);
  1755. void internalFocusLoss (FocusChangeType);
  1756. void internalChildFocusChange (FocusChangeType, const WeakReference<Component>&);
  1757. void internalModalInputAttempt();
  1758. void internalModifierKeysChanged();
  1759. void internalChildrenChanged();
  1760. void internalHierarchyChanged();
  1761. void internalRepaint (Rectangle<int>);
  1762. void internalRepaintUnchecked (Rectangle<int>, bool);
  1763. Component* removeChildComponent (int index, bool sendParentEvents, bool sendChildEvents);
  1764. void reorderChildInternal (int sourceIndex, int destIndex);
  1765. void paintComponentAndChildren (Graphics&);
  1766. void paintWithinParentContext (Graphics&);
  1767. void sendMovedResizedMessages (bool wasMoved, bool wasResized);
  1768. void sendMovedResizedMessagesIfPending();
  1769. void repaintParent();
  1770. void sendFakeMouseMove() const;
  1771. void takeKeyboardFocus (FocusChangeType);
  1772. void grabFocusInternal (FocusChangeType, bool canTryParent);
  1773. static void giveAwayFocus (bool sendFocusLossEvent);
  1774. void sendEnablementChangeMessage();
  1775. void sendVisibilityChangeMessage();
  1776. struct ComponentHelpers;
  1777. friend struct ComponentHelpers;
  1778. /* Components aren't allowed to have copy constructors, as this would mess up parent hierarchies.
  1779. You might need to give your subclasses a private dummy constructor to avoid compiler warnings.
  1780. */
  1781. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Component)
  1782. //==============================================================================
  1783. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  1784. // This is included here just to cause a compile error if your code is still handling
  1785. // drag-and-drop with this method. If so, just update it to use the new FileDragAndDropTarget
  1786. // class, which is easy (just make your class inherit from FileDragAndDropTarget, and
  1787. // implement its methods instead of this Component method).
  1788. virtual void filesDropped (const StringArray&, int, int) {}
  1789. // This is included here to cause an error if you use or overload it - it has been deprecated in
  1790. // favour of contains (Point<int>)
  1791. void contains (int, int) = delete;
  1792. #endif
  1793. protected:
  1794. //==============================================================================
  1795. /** @internal */
  1796. virtual ComponentPeer* createNewPeer (int styleFlags, void* nativeWindowToAttachTo);
  1797. #endif
  1798. };
  1799. } // namespace juce