The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2380 lines
111KB

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