Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3016 lines
95KB

  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. #define CHECK_MESSAGE_MANAGER_IS_LOCKED \
  18. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager());
  19. #define CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN \
  20. jassert (MessageManager::getInstance()->currentThreadHasLockedMessageManager() || getPeer() == nullptr);
  21. Component* Component::currentlyFocusedComponent = nullptr;
  22. //==============================================================================
  23. class Component::MouseListenerList
  24. {
  25. public:
  26. MouseListenerList() noexcept
  27. : numDeepMouseListeners (0)
  28. {
  29. }
  30. void addListener (MouseListener* const newListener, const bool wantsEventsForAllNestedChildComponents)
  31. {
  32. if (! listeners.contains (newListener))
  33. {
  34. if (wantsEventsForAllNestedChildComponents)
  35. {
  36. listeners.insert (0, newListener);
  37. ++numDeepMouseListeners;
  38. }
  39. else
  40. {
  41. listeners.add (newListener);
  42. }
  43. }
  44. }
  45. void removeListener (MouseListener* const listenerToRemove)
  46. {
  47. const int index = listeners.indexOf (listenerToRemove);
  48. if (index >= 0)
  49. {
  50. if (index < numDeepMouseListeners)
  51. --numDeepMouseListeners;
  52. listeners.remove (index);
  53. }
  54. }
  55. static void sendMouseEvent (Component& comp, Component::BailOutChecker& checker,
  56. void (MouseListener::*eventMethod) (const MouseEvent&), const MouseEvent& e)
  57. {
  58. if (checker.shouldBailOut())
  59. return;
  60. if (MouseListenerList* const list = comp.mouseListeners)
  61. {
  62. for (int i = list->listeners.size(); --i >= 0;)
  63. {
  64. (list->listeners.getUnchecked(i)->*eventMethod) (e);
  65. if (checker.shouldBailOut())
  66. return;
  67. i = jmin (i, list->listeners.size());
  68. }
  69. }
  70. for (Component* p = comp.parentComponent; p != nullptr; p = p->parentComponent)
  71. {
  72. MouseListenerList* const list = p->mouseListeners;
  73. if (list != nullptr && list->numDeepMouseListeners > 0)
  74. {
  75. BailOutChecker2 checker2 (checker, p);
  76. for (int i = list->numDeepMouseListeners; --i >= 0;)
  77. {
  78. (list->listeners.getUnchecked(i)->*eventMethod) (e);
  79. if (checker2.shouldBailOut())
  80. return;
  81. i = jmin (i, list->numDeepMouseListeners);
  82. }
  83. }
  84. }
  85. }
  86. static void sendWheelEvent (Component& comp, Component::BailOutChecker& checker,
  87. const MouseEvent& e, const MouseWheelDetails& wheel)
  88. {
  89. if (MouseListenerList* const list = comp.mouseListeners)
  90. {
  91. for (int i = list->listeners.size(); --i >= 0;)
  92. {
  93. list->listeners.getUnchecked(i)->mouseWheelMove (e, wheel);
  94. if (checker.shouldBailOut())
  95. return;
  96. i = jmin (i, list->listeners.size());
  97. }
  98. }
  99. for (Component* p = comp.parentComponent; p != nullptr; p = p->parentComponent)
  100. {
  101. MouseListenerList* const list = p->mouseListeners;
  102. if (list != nullptr && list->numDeepMouseListeners > 0)
  103. {
  104. BailOutChecker2 checker2 (checker, p);
  105. for (int i = list->numDeepMouseListeners; --i >= 0;)
  106. {
  107. list->listeners.getUnchecked(i)->mouseWheelMove (e, wheel);
  108. if (checker2.shouldBailOut())
  109. return;
  110. i = jmin (i, list->numDeepMouseListeners);
  111. }
  112. }
  113. }
  114. }
  115. private:
  116. Array <MouseListener*> listeners;
  117. int numDeepMouseListeners;
  118. class BailOutChecker2
  119. {
  120. public:
  121. BailOutChecker2 (Component::BailOutChecker& boc, Component* const comp)
  122. : checker (boc), safePointer (comp)
  123. {
  124. }
  125. bool shouldBailOut() const noexcept
  126. {
  127. return checker.shouldBailOut() || safePointer == 0;
  128. }
  129. private:
  130. Component::BailOutChecker& checker;
  131. const WeakReference<Component> safePointer;
  132. JUCE_DECLARE_NON_COPYABLE (BailOutChecker2)
  133. };
  134. JUCE_DECLARE_NON_COPYABLE (MouseListenerList)
  135. };
  136. //==============================================================================
  137. struct Component::ComponentHelpers
  138. {
  139. #if JUCE_MODAL_LOOPS_PERMITTED
  140. static void* runModalLoopCallback (void* userData)
  141. {
  142. return (void*) (pointer_sized_int) static_cast <Component*> (userData)->runModalLoop();
  143. }
  144. #endif
  145. static Identifier getColourPropertyId (int colourId)
  146. {
  147. char reversedHex[32];
  148. char* t = reversedHex;
  149. for (unsigned int v = (unsigned int) colourId;;)
  150. {
  151. *t++ = "0123456789abcdef" [(int) (v & 15)];
  152. v >>= 4;
  153. if (v == 0)
  154. break;
  155. }
  156. char destBuffer[32];
  157. char* dest = destBuffer;
  158. memcpy (dest, "jcclr_", 6);
  159. dest += 6;
  160. while (t > reversedHex)
  161. *dest++ = *--t;
  162. *dest++ = 0;
  163. return destBuffer;
  164. }
  165. //==============================================================================
  166. static inline bool hitTest (Component& comp, Point<int> localPoint)
  167. {
  168. return isPositiveAndBelow (localPoint.x, comp.getWidth())
  169. && isPositiveAndBelow (localPoint.y, comp.getHeight())
  170. && comp.hitTest (localPoint.x, localPoint.y);
  171. }
  172. template <typename PointOrRect>
  173. static PointOrRect unscaledScreenPosToScaled (float scale, PointOrRect pos) noexcept
  174. {
  175. return scale != 1.0f ? pos / scale : pos;
  176. }
  177. template <typename PointOrRect>
  178. static PointOrRect scaledScreenPosToUnscaled (float scale, PointOrRect pos) noexcept
  179. {
  180. return scale != 1.0f ? pos * scale : pos;
  181. }
  182. // For these, we need to avoid getSmallestIntegerContainer being used, which causes
  183. // judder when moving windows
  184. static Rectangle<int> unscaledScreenPosToScaled (float scale, const Rectangle<int>& pos) noexcept
  185. {
  186. return scale != 1.0f ? Rectangle<int> (roundToInt (pos.getX() / scale),
  187. roundToInt (pos.getY() / scale),
  188. roundToInt (pos.getWidth() / scale),
  189. roundToInt (pos.getHeight() / scale)) : pos;
  190. }
  191. static Rectangle<int> scaledScreenPosToUnscaled (float scale, Rectangle<int>& pos) noexcept
  192. {
  193. return scale != 1.0f ? Rectangle<int> (roundToInt (pos.getX() * scale),
  194. roundToInt (pos.getY() * scale),
  195. roundToInt (pos.getWidth() * scale),
  196. roundToInt (pos.getHeight() * scale)) : pos;
  197. }
  198. template <typename PointOrRect>
  199. static PointOrRect unscaledScreenPosToScaled (PointOrRect pos) noexcept
  200. {
  201. return unscaledScreenPosToScaled (Desktop::getInstance().getGlobalScaleFactor(), pos);
  202. }
  203. template <typename PointOrRect>
  204. static PointOrRect scaledScreenPosToUnscaled (PointOrRect pos) noexcept
  205. {
  206. return scaledScreenPosToUnscaled (Desktop::getInstance().getGlobalScaleFactor(), pos);
  207. }
  208. template <typename PointOrRect>
  209. static PointOrRect unscaledScreenPosToScaled (const Component& comp, PointOrRect pos) noexcept
  210. {
  211. return unscaledScreenPosToScaled (comp.getDesktopScaleFactor(), pos);
  212. }
  213. template <typename PointOrRect>
  214. static PointOrRect scaledScreenPosToUnscaled (const Component& comp, PointOrRect pos) noexcept
  215. {
  216. return scaledScreenPosToUnscaled (comp.getDesktopScaleFactor(), pos);
  217. }
  218. // converts an unscaled position within a peer to the local position within that peer's component
  219. template <typename PointOrRect>
  220. static PointOrRect rawPeerPositionToLocal (const Component& comp, PointOrRect pos) noexcept
  221. {
  222. if (comp.isTransformed())
  223. pos = pos.transformedBy (comp.getTransform().inverted());
  224. return unscaledScreenPosToScaled (comp, pos);
  225. }
  226. // converts a position within a peer's component to the unscaled position within the peer
  227. template <typename PointOrRect>
  228. static PointOrRect localPositionToRawPeerPos (const Component& comp, PointOrRect pos) noexcept
  229. {
  230. if (comp.isTransformed())
  231. pos = pos.transformedBy (comp.getTransform());
  232. return scaledScreenPosToUnscaled (comp, pos);
  233. }
  234. template <typename PointOrRect>
  235. static PointOrRect convertFromParentSpace (const Component& comp, PointOrRect pointInParentSpace)
  236. {
  237. if (comp.affineTransform != nullptr)
  238. pointInParentSpace = pointInParentSpace.transformedBy (comp.affineTransform->inverted());
  239. if (comp.isOnDesktop())
  240. {
  241. if (ComponentPeer* peer = comp.getPeer())
  242. pointInParentSpace = unscaledScreenPosToScaled (comp, peer->globalToLocal (scaledScreenPosToUnscaled (pointInParentSpace)));
  243. else
  244. jassertfalse;
  245. }
  246. else
  247. {
  248. pointInParentSpace -= comp.getPosition();
  249. }
  250. return pointInParentSpace;
  251. }
  252. template <typename PointOrRect>
  253. static PointOrRect convertToParentSpace (const Component& comp, PointOrRect pointInLocalSpace)
  254. {
  255. if (comp.isOnDesktop())
  256. {
  257. if (ComponentPeer* peer = comp.getPeer())
  258. pointInLocalSpace = unscaledScreenPosToScaled (peer->localToGlobal (scaledScreenPosToUnscaled (comp, pointInLocalSpace)));
  259. else
  260. jassertfalse;
  261. }
  262. else
  263. {
  264. pointInLocalSpace += comp.getPosition();
  265. }
  266. if (comp.affineTransform != nullptr)
  267. pointInLocalSpace = pointInLocalSpace.transformedBy (*comp.affineTransform);
  268. return pointInLocalSpace;
  269. }
  270. template <typename PointOrRect>
  271. static PointOrRect convertFromDistantParentSpace (const Component* parent, const Component& target, const PointOrRect& coordInParent)
  272. {
  273. const Component* const directParent = target.getParentComponent();
  274. jassert (directParent != nullptr);
  275. if (directParent == parent)
  276. return convertFromParentSpace (target, coordInParent);
  277. return convertFromParentSpace (target, convertFromDistantParentSpace (parent, *directParent, coordInParent));
  278. }
  279. template <typename PointOrRect>
  280. static PointOrRect convertCoordinate (const Component* target, const Component* source, PointOrRect p)
  281. {
  282. while (source != nullptr)
  283. {
  284. if (source == target)
  285. return p;
  286. if (source->isParentOf (target))
  287. return convertFromDistantParentSpace (source, *target, p);
  288. p = convertToParentSpace (*source, p);
  289. source = source->getParentComponent();
  290. }
  291. jassert (source == nullptr);
  292. if (target == nullptr)
  293. return p;
  294. const Component* const topLevelComp = target->getTopLevelComponent();
  295. p = convertFromParentSpace (*topLevelComp, p);
  296. if (topLevelComp == target)
  297. return p;
  298. return convertFromDistantParentSpace (topLevelComp, *target, p);
  299. }
  300. static Rectangle<int> getUnclippedArea (const Component& comp)
  301. {
  302. Rectangle<int> r (comp.getLocalBounds());
  303. if (Component* const p = comp.getParentComponent())
  304. r = r.getIntersection (convertFromParentSpace (comp, getUnclippedArea (*p)));
  305. return r;
  306. }
  307. static bool clipObscuredRegions (const Component& comp, Graphics& g, const Rectangle<int>& clipRect, Point<int> delta)
  308. {
  309. bool nothingChanged = true;
  310. for (int i = comp.childComponentList.size(); --i >= 0;)
  311. {
  312. const Component& child = *comp.childComponentList.getUnchecked(i);
  313. if (child.isVisible() && ! child.isTransformed())
  314. {
  315. const Rectangle<int> newClip (clipRect.getIntersection (child.bounds));
  316. if (! newClip.isEmpty())
  317. {
  318. if (child.isOpaque() && child.componentTransparency == 0)
  319. {
  320. g.excludeClipRegion (newClip + delta);
  321. nothingChanged = false;
  322. }
  323. else
  324. {
  325. const Point<int> childPos (child.getPosition());
  326. if (clipObscuredRegions (child, g, newClip - childPos, childPos + delta))
  327. nothingChanged = false;
  328. }
  329. }
  330. }
  331. }
  332. return nothingChanged;
  333. }
  334. static void subtractObscuredRegions (const Component& comp, RectangleList<int>& result,
  335. Point<int> delta, const Rectangle<int>& clipRect,
  336. const Component* const compToAvoid)
  337. {
  338. for (int i = comp.childComponentList.size(); --i >= 0;)
  339. {
  340. const Component* const c = comp.childComponentList.getUnchecked(i);
  341. if (c != compToAvoid && c->isVisible())
  342. {
  343. if (c->isOpaque() && c->componentTransparency == 0)
  344. {
  345. Rectangle<int> childBounds (c->bounds.getIntersection (clipRect));
  346. childBounds.translate (delta.x, delta.y);
  347. result.subtract (childBounds);
  348. }
  349. else
  350. {
  351. Rectangle<int> newClip (clipRect.getIntersection (c->bounds));
  352. newClip.translate (-c->getX(), -c->getY());
  353. subtractObscuredRegions (*c, result, c->getPosition() + delta,
  354. newClip, compToAvoid);
  355. }
  356. }
  357. }
  358. }
  359. static Rectangle<int> getParentOrMainMonitorBounds (const Component& comp)
  360. {
  361. if (Component* p = comp.getParentComponent())
  362. return p->getLocalBounds();
  363. return Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  364. }
  365. };
  366. //==============================================================================
  367. Component::Component()
  368. : parentComponent (nullptr),
  369. lookAndFeel (nullptr),
  370. effect (nullptr),
  371. componentFlags (0),
  372. componentTransparency (0)
  373. {
  374. }
  375. Component::Component (const String& name)
  376. : componentName (name),
  377. parentComponent (nullptr),
  378. lookAndFeel (nullptr),
  379. effect (nullptr),
  380. componentFlags (0),
  381. componentTransparency (0)
  382. {
  383. }
  384. Component::~Component()
  385. {
  386. static_jassert (sizeof (flags) <= sizeof (componentFlags));
  387. componentListeners.call (&ComponentListener::componentBeingDeleted, *this);
  388. masterReference.clear();
  389. while (childComponentList.size() > 0)
  390. removeChildComponent (childComponentList.size() - 1, false, true);
  391. if (parentComponent != nullptr)
  392. parentComponent->removeChildComponent (parentComponent->childComponentList.indexOf (this), true, false);
  393. else if (currentlyFocusedComponent == this || isParentOf (currentlyFocusedComponent))
  394. giveAwayFocus (currentlyFocusedComponent != this);
  395. if (flags.hasHeavyweightPeerFlag)
  396. removeFromDesktop();
  397. // Something has added some children to this component during its destructor! Not a smart idea!
  398. jassert (childComponentList.size() == 0);
  399. }
  400. //==============================================================================
  401. void Component::setName (const String& name)
  402. {
  403. // if component methods are being called from threads other than the message
  404. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  405. CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN
  406. if (componentName != name)
  407. {
  408. componentName = name;
  409. if (flags.hasHeavyweightPeerFlag)
  410. if (ComponentPeer* const peer = getPeer())
  411. peer->setTitle (name);
  412. BailOutChecker checker (this);
  413. componentListeners.callChecked (checker, &ComponentListener::componentNameChanged, *this);
  414. }
  415. }
  416. void Component::setComponentID (const String& newID)
  417. {
  418. componentID = newID;
  419. }
  420. void Component::setVisible (bool shouldBeVisible)
  421. {
  422. if (flags.visibleFlag != shouldBeVisible)
  423. {
  424. // if component methods are being called from threads other than the message
  425. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  426. CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN
  427. const WeakReference<Component> safePointer (this);
  428. flags.visibleFlag = shouldBeVisible;
  429. if (shouldBeVisible)
  430. repaint();
  431. else
  432. repaintParent();
  433. sendFakeMouseMove();
  434. if (! shouldBeVisible)
  435. {
  436. if (cachedImage != nullptr)
  437. cachedImage->releaseResources();
  438. if (currentlyFocusedComponent == this || isParentOf (currentlyFocusedComponent))
  439. {
  440. if (parentComponent != nullptr)
  441. parentComponent->grabKeyboardFocus();
  442. else
  443. giveAwayFocus (true);
  444. }
  445. }
  446. if (safePointer != nullptr)
  447. {
  448. sendVisibilityChangeMessage();
  449. if (safePointer != nullptr && flags.hasHeavyweightPeerFlag)
  450. {
  451. if (ComponentPeer* const peer = getPeer())
  452. {
  453. peer->setVisible (shouldBeVisible);
  454. internalHierarchyChanged();
  455. }
  456. }
  457. }
  458. }
  459. }
  460. void Component::visibilityChanged() {}
  461. void Component::sendVisibilityChangeMessage()
  462. {
  463. BailOutChecker checker (this);
  464. visibilityChanged();
  465. if (! checker.shouldBailOut())
  466. componentListeners.callChecked (checker, &ComponentListener::componentVisibilityChanged, *this);
  467. }
  468. bool Component::isShowing() const
  469. {
  470. if (! flags.visibleFlag)
  471. return false;
  472. if (parentComponent != nullptr)
  473. return parentComponent->isShowing();
  474. if (const ComponentPeer* const peer = getPeer())
  475. return ! peer->isMinimised();
  476. return false;
  477. }
  478. //==============================================================================
  479. void* Component::getWindowHandle() const
  480. {
  481. if (const ComponentPeer* const peer = getPeer())
  482. return peer->getNativeHandle();
  483. return nullptr;
  484. }
  485. //==============================================================================
  486. void Component::addToDesktop (int styleWanted, void* nativeWindowToAttachTo)
  487. {
  488. // if component methods are being called from threads other than the message
  489. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  490. CHECK_MESSAGE_MANAGER_IS_LOCKED
  491. if (isOpaque())
  492. styleWanted &= ~ComponentPeer::windowIsSemiTransparent;
  493. else
  494. styleWanted |= ComponentPeer::windowIsSemiTransparent;
  495. // don't use getPeer(), so that we only get the peer that's specifically
  496. // for this comp, and not for one of its parents.
  497. ComponentPeer* peer = ComponentPeer::getPeerFor (this);
  498. if (peer == nullptr || styleWanted != peer->getStyleFlags())
  499. {
  500. const WeakReference<Component> safePointer (this);
  501. #if JUCE_LINUX
  502. // it's wise to give the component a non-zero size before
  503. // putting it on the desktop, as X windows get confused by this, and
  504. // a (1, 1) minimum size is enforced here.
  505. setSize (jmax (1, getWidth()),
  506. jmax (1, getHeight()));
  507. #endif
  508. const Point<int> topLeft (getScreenPosition());
  509. bool wasFullscreen = false;
  510. bool wasMinimised = false;
  511. ComponentBoundsConstrainer* currentConstainer = nullptr;
  512. Rectangle<int> oldNonFullScreenBounds;
  513. int oldRenderingEngine = -1;
  514. if (peer != nullptr)
  515. {
  516. ScopedPointer<ComponentPeer> oldPeerToDelete (peer);
  517. wasFullscreen = peer->isFullScreen();
  518. wasMinimised = peer->isMinimised();
  519. currentConstainer = peer->getConstrainer();
  520. oldNonFullScreenBounds = peer->getNonFullScreenBounds();
  521. oldRenderingEngine = peer->getCurrentRenderingEngine();
  522. flags.hasHeavyweightPeerFlag = false;
  523. Desktop::getInstance().removeDesktopComponent (this);
  524. internalHierarchyChanged(); // give comps a chance to react to the peer change before the old peer is deleted.
  525. if (safePointer == nullptr)
  526. return;
  527. setTopLeftPosition (topLeft);
  528. }
  529. if (parentComponent != nullptr)
  530. parentComponent->removeChildComponent (this);
  531. if (safePointer != nullptr)
  532. {
  533. flags.hasHeavyweightPeerFlag = true;
  534. peer = createNewPeer (styleWanted, nativeWindowToAttachTo);
  535. Desktop::getInstance().addDesktopComponent (this);
  536. bounds.setPosition (topLeft);
  537. peer->updateBounds();
  538. if (oldRenderingEngine >= 0)
  539. peer->setCurrentRenderingEngine (oldRenderingEngine);
  540. peer->setVisible (isVisible());
  541. peer = ComponentPeer::getPeerFor (this);
  542. if (peer == nullptr)
  543. return;
  544. if (wasFullscreen)
  545. {
  546. peer->setFullScreen (true);
  547. peer->setNonFullScreenBounds (oldNonFullScreenBounds);
  548. }
  549. if (wasMinimised)
  550. peer->setMinimised (true);
  551. #if JUCE_WINDOWS
  552. if (isAlwaysOnTop())
  553. peer->setAlwaysOnTop (true);
  554. #endif
  555. peer->setConstrainer (currentConstainer);
  556. repaint();
  557. internalHierarchyChanged();
  558. }
  559. }
  560. }
  561. void Component::removeFromDesktop()
  562. {
  563. // if component methods are being called from threads other than the message
  564. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  565. CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN
  566. if (flags.hasHeavyweightPeerFlag)
  567. {
  568. ComponentPeer* const peer = ComponentPeer::getPeerFor (this);
  569. jassert (peer != nullptr);
  570. flags.hasHeavyweightPeerFlag = false;
  571. delete peer;
  572. Desktop::getInstance().removeDesktopComponent (this);
  573. }
  574. }
  575. bool Component::isOnDesktop() const noexcept
  576. {
  577. return flags.hasHeavyweightPeerFlag;
  578. }
  579. ComponentPeer* Component::getPeer() const
  580. {
  581. if (flags.hasHeavyweightPeerFlag)
  582. return ComponentPeer::getPeerFor (this);
  583. if (parentComponent == nullptr)
  584. return nullptr;
  585. return parentComponent->getPeer();
  586. }
  587. void Component::userTriedToCloseWindow()
  588. {
  589. /* This means that the user's trying to get rid of your window with the 'close window' system
  590. menu option (on windows) or possibly the task manager - you should really handle this
  591. and delete or hide your component in an appropriate way.
  592. If you want to ignore the event and don't want to trigger this assertion, just override
  593. this method and do nothing.
  594. */
  595. jassertfalse;
  596. }
  597. void Component::minimisationStateChanged (bool) {}
  598. float Component::getDesktopScaleFactor() const { return Desktop::getInstance().getGlobalScaleFactor(); }
  599. //==============================================================================
  600. void Component::setOpaque (const bool shouldBeOpaque)
  601. {
  602. if (shouldBeOpaque != flags.opaqueFlag)
  603. {
  604. flags.opaqueFlag = shouldBeOpaque;
  605. if (flags.hasHeavyweightPeerFlag)
  606. if (const ComponentPeer* const peer = ComponentPeer::getPeerFor (this))
  607. addToDesktop (peer->getStyleFlags()); // recreates the heavyweight window
  608. repaint();
  609. }
  610. }
  611. bool Component::isOpaque() const noexcept
  612. {
  613. return flags.opaqueFlag;
  614. }
  615. //==============================================================================
  616. class StandardCachedComponentImage : public CachedComponentImage
  617. {
  618. public:
  619. StandardCachedComponentImage (Component& c) noexcept : owner (c), scale (1.0f) {}
  620. void paint (Graphics& g) override
  621. {
  622. scale = g.getInternalContext().getPhysicalPixelScaleFactor();
  623. const Rectangle<int> compBounds (owner.getLocalBounds());
  624. const Rectangle<int> imageBounds (compBounds * scale);
  625. if (image.isNull() || image.getBounds() != imageBounds)
  626. {
  627. image = Image (owner.isOpaque() ? Image::RGB
  628. : Image::ARGB,
  629. jmax (1, imageBounds.getWidth()),
  630. jmax (1, imageBounds.getHeight()),
  631. ! owner.isOpaque());
  632. validArea.clear();
  633. }
  634. {
  635. Graphics imG (image);
  636. LowLevelGraphicsContext& lg = imG.getInternalContext();
  637. for (const Rectangle<int>* i = validArea.begin(), * const e = validArea.end(); i != e; ++i)
  638. lg.excludeClipRectangle (*i);
  639. if (! lg.isClipEmpty())
  640. {
  641. if (! owner.isOpaque())
  642. {
  643. lg.setFill (Colours::transparentBlack);
  644. lg.fillRect (imageBounds, true);
  645. lg.setFill (Colours::black);
  646. }
  647. lg.addTransform (AffineTransform::scale (scale));
  648. owner.paintEntireComponent (imG, true);
  649. }
  650. }
  651. validArea = imageBounds;
  652. g.setColour (Colours::black.withAlpha (owner.getAlpha()));
  653. g.drawImageTransformed (image, AffineTransform::scale (compBounds.getWidth() / (float) imageBounds.getWidth(),
  654. compBounds.getHeight() / (float) imageBounds.getHeight()), false);
  655. }
  656. bool invalidateAll() override { validArea.clear(); return true; }
  657. bool invalidate (const Rectangle<int>& area) override { validArea.subtract (area * scale); return true; }
  658. void releaseResources() override { image = Image::null; }
  659. private:
  660. Image image;
  661. RectangleList<int> validArea;
  662. Component& owner;
  663. float scale;
  664. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (StandardCachedComponentImage)
  665. };
  666. void Component::setCachedComponentImage (CachedComponentImage* newCachedImage)
  667. {
  668. if (cachedImage != newCachedImage)
  669. {
  670. cachedImage = newCachedImage;
  671. repaint();
  672. }
  673. }
  674. void Component::setBufferedToImage (const bool shouldBeBuffered)
  675. {
  676. // This assertion means that this component is already using a custom CachedComponentImage,
  677. // so by calling setBufferedToImage, you'll be deleting the custom one - this is almost certainly
  678. // not what you wanted to happen... If you really do know what you're doing here, and want to
  679. // avoid this assertion, just call setCachedComponentImage (nullptr) before setBufferedToImage().
  680. jassert (cachedImage == nullptr || dynamic_cast <StandardCachedComponentImage*> (cachedImage.get()) != nullptr);
  681. if (shouldBeBuffered)
  682. {
  683. if (cachedImage == nullptr)
  684. cachedImage = new StandardCachedComponentImage (*this);
  685. }
  686. else
  687. {
  688. cachedImage = nullptr;
  689. }
  690. }
  691. //==============================================================================
  692. void Component::reorderChildInternal (const int sourceIndex, const int destIndex)
  693. {
  694. if (sourceIndex != destIndex)
  695. {
  696. Component* const c = childComponentList.getUnchecked (sourceIndex);
  697. jassert (c != nullptr);
  698. c->repaintParent();
  699. childComponentList.move (sourceIndex, destIndex);
  700. sendFakeMouseMove();
  701. internalChildrenChanged();
  702. }
  703. }
  704. void Component::toFront (const bool setAsForeground)
  705. {
  706. // if component methods are being called from threads other than the message
  707. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  708. CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN
  709. if (flags.hasHeavyweightPeerFlag)
  710. {
  711. if (ComponentPeer* const peer = getPeer())
  712. {
  713. peer->toFront (setAsForeground);
  714. if (setAsForeground && ! hasKeyboardFocus (true))
  715. grabKeyboardFocus();
  716. }
  717. }
  718. else if (parentComponent != nullptr)
  719. {
  720. const Array<Component*>& childList = parentComponent->childComponentList;
  721. if (childList.getLast() != this)
  722. {
  723. const int index = childList.indexOf (this);
  724. if (index >= 0)
  725. {
  726. int insertIndex = -1;
  727. if (! flags.alwaysOnTopFlag)
  728. {
  729. insertIndex = childList.size() - 1;
  730. while (insertIndex > 0 && childList.getUnchecked (insertIndex)->isAlwaysOnTop())
  731. --insertIndex;
  732. }
  733. parentComponent->reorderChildInternal (index, insertIndex);
  734. }
  735. }
  736. if (setAsForeground)
  737. {
  738. internalBroughtToFront();
  739. grabKeyboardFocus();
  740. }
  741. }
  742. }
  743. void Component::toBehind (Component* const other)
  744. {
  745. if (other != nullptr && other != this)
  746. {
  747. // the two components must belong to the same parent..
  748. jassert (parentComponent == other->parentComponent);
  749. if (parentComponent != nullptr)
  750. {
  751. const Array<Component*>& childList = parentComponent->childComponentList;
  752. const int index = childList.indexOf (this);
  753. if (index >= 0 && childList [index + 1] != other)
  754. {
  755. int otherIndex = childList.indexOf (other);
  756. if (otherIndex >= 0)
  757. {
  758. if (index < otherIndex)
  759. --otherIndex;
  760. parentComponent->reorderChildInternal (index, otherIndex);
  761. }
  762. }
  763. }
  764. else if (isOnDesktop())
  765. {
  766. jassert (other->isOnDesktop());
  767. if (other->isOnDesktop())
  768. {
  769. ComponentPeer* const us = getPeer();
  770. ComponentPeer* const them = other->getPeer();
  771. jassert (us != nullptr && them != nullptr);
  772. if (us != nullptr && them != nullptr)
  773. us->toBehind (them);
  774. }
  775. }
  776. }
  777. }
  778. void Component::toBack()
  779. {
  780. if (isOnDesktop())
  781. {
  782. jassertfalse; //xxx need to add this to native window
  783. }
  784. else if (parentComponent != nullptr)
  785. {
  786. const Array<Component*>& childList = parentComponent->childComponentList;
  787. if (childList.getFirst() != this)
  788. {
  789. const int index = childList.indexOf (this);
  790. if (index > 0)
  791. {
  792. int insertIndex = 0;
  793. if (flags.alwaysOnTopFlag)
  794. while (insertIndex < childList.size() && ! childList.getUnchecked (insertIndex)->isAlwaysOnTop())
  795. ++insertIndex;
  796. parentComponent->reorderChildInternal (index, insertIndex);
  797. }
  798. }
  799. }
  800. }
  801. void Component::setAlwaysOnTop (const bool shouldStayOnTop)
  802. {
  803. if (shouldStayOnTop != flags.alwaysOnTopFlag)
  804. {
  805. BailOutChecker checker (this);
  806. flags.alwaysOnTopFlag = shouldStayOnTop;
  807. if (isOnDesktop())
  808. {
  809. if (ComponentPeer* const peer = getPeer())
  810. {
  811. if (! peer->setAlwaysOnTop (shouldStayOnTop))
  812. {
  813. // some kinds of peer can't change their always-on-top status, so
  814. // for these, we'll need to create a new window
  815. const int oldFlags = peer->getStyleFlags();
  816. removeFromDesktop();
  817. addToDesktop (oldFlags);
  818. }
  819. }
  820. }
  821. if (shouldStayOnTop && ! checker.shouldBailOut())
  822. toFront (false);
  823. if (! checker.shouldBailOut())
  824. internalHierarchyChanged();
  825. }
  826. }
  827. bool Component::isAlwaysOnTop() const noexcept
  828. {
  829. return flags.alwaysOnTopFlag;
  830. }
  831. //==============================================================================
  832. int Component::proportionOfWidth (const float proportion) const noexcept { return roundToInt (proportion * bounds.getWidth()); }
  833. int Component::proportionOfHeight (const float proportion) const noexcept { return roundToInt (proportion * bounds.getHeight()); }
  834. int Component::getParentWidth() const noexcept
  835. {
  836. return parentComponent != nullptr ? parentComponent->getWidth()
  837. : getParentMonitorArea().getWidth();
  838. }
  839. int Component::getParentHeight() const noexcept
  840. {
  841. return parentComponent != nullptr ? parentComponent->getHeight()
  842. : getParentMonitorArea().getHeight();
  843. }
  844. int Component::getScreenX() const { return getScreenPosition().x; }
  845. int Component::getScreenY() const { return getScreenPosition().y; }
  846. Point<int> Component::getScreenPosition() const { return localPointToGlobal (Point<int>()); }
  847. Rectangle<int> Component::getScreenBounds() const { return localAreaToGlobal (getLocalBounds()); }
  848. Rectangle<int> Component::getParentMonitorArea() const
  849. {
  850. return Desktop::getInstance().getDisplays().getDisplayContaining (getScreenBounds().getCentre()).userArea;
  851. }
  852. Point<int> Component::getLocalPoint (const Component* source, Point<int> point) const
  853. {
  854. return ComponentHelpers::convertCoordinate (this, source, point);
  855. }
  856. Rectangle<int> Component::getLocalArea (const Component* source, const Rectangle<int>& area) const
  857. {
  858. return ComponentHelpers::convertCoordinate (this, source, area);
  859. }
  860. Point<int> Component::localPointToGlobal (Point<int> point) const
  861. {
  862. return ComponentHelpers::convertCoordinate (nullptr, this, point);
  863. }
  864. Rectangle<int> Component::localAreaToGlobal (const Rectangle<int>& area) const
  865. {
  866. return ComponentHelpers::convertCoordinate (nullptr, this, area);
  867. }
  868. // Deprecated methods...
  869. Point<int> Component::relativePositionToGlobal (Point<int> relativePosition) const
  870. {
  871. return localPointToGlobal (relativePosition);
  872. }
  873. Point<int> Component::globalPositionToRelative (Point<int> screenPosition) const
  874. {
  875. return getLocalPoint (nullptr, screenPosition);
  876. }
  877. Point<int> Component::relativePositionToOtherComponent (const Component* const targetComponent, Point<int> positionRelativeToThis) const
  878. {
  879. return targetComponent == nullptr ? localPointToGlobal (positionRelativeToThis)
  880. : targetComponent->getLocalPoint (this, positionRelativeToThis);
  881. }
  882. //==============================================================================
  883. void Component::setBounds (const int x, const int y, int w, int h)
  884. {
  885. // if component methods are being called from threads other than the message
  886. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  887. CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN
  888. if (w < 0) w = 0;
  889. if (h < 0) h = 0;
  890. const bool wasResized = (getWidth() != w || getHeight() != h);
  891. const bool wasMoved = (getX() != x || getY() != y);
  892. #if JUCE_DEBUG
  893. // It's a very bad idea to try to resize a window during its paint() method!
  894. jassert (! (flags.isInsidePaintCall && wasResized && isOnDesktop()));
  895. #endif
  896. if (wasMoved || wasResized)
  897. {
  898. const bool showing = isShowing();
  899. if (showing)
  900. {
  901. // send a fake mouse move to trigger enter/exit messages if needed..
  902. sendFakeMouseMove();
  903. if (! flags.hasHeavyweightPeerFlag)
  904. repaintParent();
  905. }
  906. bounds.setBounds (x, y, w, h);
  907. if (showing)
  908. {
  909. if (wasResized)
  910. repaint();
  911. else if (! flags.hasHeavyweightPeerFlag)
  912. repaintParent();
  913. }
  914. else if (cachedImage != nullptr)
  915. {
  916. cachedImage->invalidateAll();
  917. }
  918. if (flags.hasHeavyweightPeerFlag)
  919. if (ComponentPeer* const peer = getPeer())
  920. peer->updateBounds();
  921. sendMovedResizedMessages (wasMoved, wasResized);
  922. }
  923. }
  924. void Component::sendMovedResizedMessages (const bool wasMoved, const bool wasResized)
  925. {
  926. BailOutChecker checker (this);
  927. if (wasMoved)
  928. {
  929. moved();
  930. if (checker.shouldBailOut())
  931. return;
  932. }
  933. if (wasResized)
  934. {
  935. resized();
  936. if (checker.shouldBailOut())
  937. return;
  938. for (int i = childComponentList.size(); --i >= 0;)
  939. {
  940. childComponentList.getUnchecked(i)->parentSizeChanged();
  941. if (checker.shouldBailOut())
  942. return;
  943. i = jmin (i, childComponentList.size());
  944. }
  945. }
  946. if (parentComponent != nullptr)
  947. parentComponent->childBoundsChanged (this);
  948. if (! checker.shouldBailOut())
  949. componentListeners.callChecked (checker, &ComponentListener::componentMovedOrResized,
  950. *this, wasMoved, wasResized);
  951. }
  952. void Component::setSize (const int w, const int h)
  953. {
  954. setBounds (getX(), getY(), w, h);
  955. }
  956. void Component::setTopLeftPosition (const int x, const int y)
  957. {
  958. setBounds (x, y, getWidth(), getHeight());
  959. }
  960. void Component::setTopLeftPosition (Point<int> pos)
  961. {
  962. setBounds (pos.x, pos.y, getWidth(), getHeight());
  963. }
  964. void Component::setTopRightPosition (const int x, const int y)
  965. {
  966. setTopLeftPosition (x - getWidth(), y);
  967. }
  968. void Component::setBounds (const Rectangle<int>& r)
  969. {
  970. setBounds (r.getX(), r.getY(), r.getWidth(), r.getHeight());
  971. }
  972. void Component::setBounds (const RelativeRectangle& newBounds)
  973. {
  974. newBounds.applyToComponent (*this);
  975. }
  976. void Component::setBounds (const String& newBoundsExpression)
  977. {
  978. setBounds (RelativeRectangle (newBoundsExpression));
  979. }
  980. void Component::setBoundsRelative (const float x, const float y,
  981. const float w, const float h)
  982. {
  983. const int pw = getParentWidth();
  984. const int ph = getParentHeight();
  985. setBounds (roundToInt (x * pw),
  986. roundToInt (y * ph),
  987. roundToInt (w * pw),
  988. roundToInt (h * ph));
  989. }
  990. void Component::setCentrePosition (const int x, const int y)
  991. {
  992. setTopLeftPosition (x - getWidth() / 2,
  993. y - getHeight() / 2);
  994. }
  995. void Component::setCentreRelative (const float x, const float y)
  996. {
  997. setCentrePosition (roundToInt (getParentWidth() * x),
  998. roundToInt (getParentHeight() * y));
  999. }
  1000. void Component::centreWithSize (const int width, const int height)
  1001. {
  1002. const Rectangle<int> parentArea (ComponentHelpers::getParentOrMainMonitorBounds (*this));
  1003. setBounds (parentArea.getCentreX() - width / 2,
  1004. parentArea.getCentreY() - height / 2,
  1005. width, height);
  1006. }
  1007. void Component::setBoundsInset (const BorderSize<int>& borders)
  1008. {
  1009. setBounds (borders.subtractedFrom (ComponentHelpers::getParentOrMainMonitorBounds (*this)));
  1010. }
  1011. void Component::setBoundsToFit (int x, int y, int width, int height,
  1012. Justification justification,
  1013. const bool onlyReduceInSize)
  1014. {
  1015. // it's no good calling this method unless both the component and
  1016. // target rectangle have a finite size.
  1017. jassert (getWidth() > 0 && getHeight() > 0 && width > 0 && height > 0);
  1018. if (getWidth() > 0 && getHeight() > 0
  1019. && width > 0 && height > 0)
  1020. {
  1021. int newW, newH;
  1022. if (onlyReduceInSize && getWidth() <= width && getHeight() <= height)
  1023. {
  1024. newW = getWidth();
  1025. newH = getHeight();
  1026. }
  1027. else
  1028. {
  1029. const double imageRatio = getHeight() / (double) getWidth();
  1030. const double targetRatio = height / (double) width;
  1031. if (imageRatio <= targetRatio)
  1032. {
  1033. newW = width;
  1034. newH = jmin (height, roundToInt (newW * imageRatio));
  1035. }
  1036. else
  1037. {
  1038. newH = height;
  1039. newW = jmin (width, roundToInt (newH / imageRatio));
  1040. }
  1041. }
  1042. if (newW > 0 && newH > 0)
  1043. setBounds (justification.appliedToRectangle (Rectangle<int> (newW, newH),
  1044. Rectangle<int> (x, y, width, height)));
  1045. }
  1046. }
  1047. //==============================================================================
  1048. void Component::setTransform (const AffineTransform& newTransform)
  1049. {
  1050. // If you pass in a transform with no inverse, the component will have no dimensions,
  1051. // and there will be all sorts of maths errors when converting coordinates.
  1052. jassert (! newTransform.isSingularity());
  1053. if (newTransform.isIdentity())
  1054. {
  1055. if (affineTransform != nullptr)
  1056. {
  1057. repaint();
  1058. affineTransform = nullptr;
  1059. repaint();
  1060. sendMovedResizedMessages (false, false);
  1061. }
  1062. }
  1063. else if (affineTransform == nullptr)
  1064. {
  1065. repaint();
  1066. affineTransform = new AffineTransform (newTransform);
  1067. repaint();
  1068. sendMovedResizedMessages (false, false);
  1069. }
  1070. else if (*affineTransform != newTransform)
  1071. {
  1072. repaint();
  1073. *affineTransform = newTransform;
  1074. repaint();
  1075. sendMovedResizedMessages (false, false);
  1076. }
  1077. }
  1078. bool Component::isTransformed() const noexcept
  1079. {
  1080. return affineTransform != nullptr;
  1081. }
  1082. AffineTransform Component::getTransform() const
  1083. {
  1084. return affineTransform != nullptr ? *affineTransform : AffineTransform::identity;
  1085. }
  1086. //==============================================================================
  1087. bool Component::hitTest (int x, int y)
  1088. {
  1089. if (! flags.ignoresMouseClicksFlag)
  1090. return true;
  1091. if (flags.allowChildMouseClicksFlag)
  1092. {
  1093. for (int i = childComponentList.size(); --i >= 0;)
  1094. {
  1095. Component& child = *childComponentList.getUnchecked (i);
  1096. if (child.isVisible()
  1097. && ComponentHelpers::hitTest (child, ComponentHelpers::convertFromParentSpace (child, Point<int> (x, y))))
  1098. return true;
  1099. }
  1100. }
  1101. return false;
  1102. }
  1103. void Component::setInterceptsMouseClicks (const bool allowClicks,
  1104. const bool allowClicksOnChildComponents) noexcept
  1105. {
  1106. flags.ignoresMouseClicksFlag = ! allowClicks;
  1107. flags.allowChildMouseClicksFlag = allowClicksOnChildComponents;
  1108. }
  1109. void Component::getInterceptsMouseClicks (bool& allowsClicksOnThisComponent,
  1110. bool& allowsClicksOnChildComponents) const noexcept
  1111. {
  1112. allowsClicksOnThisComponent = ! flags.ignoresMouseClicksFlag;
  1113. allowsClicksOnChildComponents = flags.allowChildMouseClicksFlag;
  1114. }
  1115. bool Component::contains (Point<int> point)
  1116. {
  1117. if (ComponentHelpers::hitTest (*this, point))
  1118. {
  1119. if (parentComponent != nullptr)
  1120. return parentComponent->contains (ComponentHelpers::convertToParentSpace (*this, point));
  1121. if (flags.hasHeavyweightPeerFlag)
  1122. if (const ComponentPeer* const peer = getPeer())
  1123. return peer->contains (ComponentHelpers::localPositionToRawPeerPos (*this, point), true);
  1124. }
  1125. return false;
  1126. }
  1127. bool Component::reallyContains (Point<int> point, const bool returnTrueIfWithinAChild)
  1128. {
  1129. if (! contains (point))
  1130. return false;
  1131. Component* const top = getTopLevelComponent();
  1132. const Component* const compAtPosition = top->getComponentAt (top->getLocalPoint (this, point));
  1133. return (compAtPosition == this) || (returnTrueIfWithinAChild && isParentOf (compAtPosition));
  1134. }
  1135. Component* Component::getComponentAt (Point<int> position)
  1136. {
  1137. if (flags.visibleFlag && ComponentHelpers::hitTest (*this, position))
  1138. {
  1139. for (int i = childComponentList.size(); --i >= 0;)
  1140. {
  1141. Component* child = childComponentList.getUnchecked(i);
  1142. child = child->getComponentAt (ComponentHelpers::convertFromParentSpace (*child, position));
  1143. if (child != nullptr)
  1144. return child;
  1145. }
  1146. return this;
  1147. }
  1148. return nullptr;
  1149. }
  1150. Component* Component::getComponentAt (const int x, const int y)
  1151. {
  1152. return getComponentAt (Point<int> (x, y));
  1153. }
  1154. //==============================================================================
  1155. void Component::addChildComponent (Component* const child, int zOrder)
  1156. {
  1157. // if component methods are being called from threads other than the message
  1158. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  1159. CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN
  1160. if (child != nullptr && child->parentComponent != this)
  1161. {
  1162. if (child->parentComponent != nullptr)
  1163. child->parentComponent->removeChildComponent (child);
  1164. else
  1165. child->removeFromDesktop();
  1166. child->parentComponent = this;
  1167. if (child->isVisible())
  1168. child->repaintParent();
  1169. if (! child->isAlwaysOnTop())
  1170. {
  1171. if (zOrder < 0 || zOrder > childComponentList.size())
  1172. zOrder = childComponentList.size();
  1173. while (zOrder > 0)
  1174. {
  1175. if (! childComponentList.getUnchecked (zOrder - 1)->isAlwaysOnTop())
  1176. break;
  1177. --zOrder;
  1178. }
  1179. }
  1180. childComponentList.insert (zOrder, child);
  1181. child->internalHierarchyChanged();
  1182. internalChildrenChanged();
  1183. }
  1184. }
  1185. void Component::addAndMakeVisible (Component* const child, int zOrder)
  1186. {
  1187. if (child != nullptr)
  1188. {
  1189. child->setVisible (true);
  1190. addChildComponent (child, zOrder);
  1191. }
  1192. }
  1193. void Component::addChildAndSetID (Component* const child, const String& childID)
  1194. {
  1195. if (child != nullptr)
  1196. {
  1197. child->setComponentID (childID);
  1198. addAndMakeVisible (child);
  1199. }
  1200. }
  1201. void Component::removeChildComponent (Component* const child)
  1202. {
  1203. removeChildComponent (childComponentList.indexOf (child), true, true);
  1204. }
  1205. Component* Component::removeChildComponent (const int index)
  1206. {
  1207. return removeChildComponent (index, true, true);
  1208. }
  1209. Component* Component::removeChildComponent (const int index, bool sendParentEvents, const bool sendChildEvents)
  1210. {
  1211. // if component methods are being called from threads other than the message
  1212. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  1213. CHECK_MESSAGE_MANAGER_IS_LOCKED_OR_OFFSCREEN
  1214. Component* const child = childComponentList [index];
  1215. if (child != nullptr)
  1216. {
  1217. sendParentEvents = sendParentEvents && child->isShowing();
  1218. if (sendParentEvents)
  1219. {
  1220. sendFakeMouseMove();
  1221. if (child->isVisible())
  1222. child->repaintParent();
  1223. }
  1224. childComponentList.remove (index);
  1225. child->parentComponent = nullptr;
  1226. if (child->cachedImage != nullptr)
  1227. child->cachedImage->releaseResources();
  1228. // (NB: there are obscure situations where child->isShowing() = false, but it still has the focus)
  1229. if (currentlyFocusedComponent == child || child->isParentOf (currentlyFocusedComponent))
  1230. {
  1231. if (sendParentEvents)
  1232. {
  1233. const WeakReference<Component> thisPointer (this);
  1234. giveAwayFocus (sendChildEvents || currentlyFocusedComponent != child);
  1235. if (thisPointer == nullptr)
  1236. return child;
  1237. grabKeyboardFocus();
  1238. }
  1239. else
  1240. {
  1241. giveAwayFocus (sendChildEvents || currentlyFocusedComponent != child);
  1242. }
  1243. }
  1244. if (sendChildEvents)
  1245. child->internalHierarchyChanged();
  1246. if (sendParentEvents)
  1247. internalChildrenChanged();
  1248. }
  1249. return child;
  1250. }
  1251. //==============================================================================
  1252. void Component::removeAllChildren()
  1253. {
  1254. while (childComponentList.size() > 0)
  1255. removeChildComponent (childComponentList.size() - 1);
  1256. }
  1257. void Component::deleteAllChildren()
  1258. {
  1259. while (childComponentList.size() > 0)
  1260. delete (removeChildComponent (childComponentList.size() - 1));
  1261. }
  1262. int Component::getNumChildComponents() const noexcept
  1263. {
  1264. return childComponentList.size();
  1265. }
  1266. Component* Component::getChildComponent (const int index) const noexcept
  1267. {
  1268. return childComponentList [index];
  1269. }
  1270. int Component::getIndexOfChildComponent (const Component* const child) const noexcept
  1271. {
  1272. return childComponentList.indexOf (const_cast <Component*> (child));
  1273. }
  1274. Component* Component::findChildWithID (StringRef targetID) const noexcept
  1275. {
  1276. for (int i = childComponentList.size(); --i >= 0;)
  1277. {
  1278. Component* const c = childComponentList.getUnchecked(i);
  1279. if (c->componentID == targetID)
  1280. return c;
  1281. }
  1282. return nullptr;
  1283. }
  1284. Component* Component::getTopLevelComponent() const noexcept
  1285. {
  1286. const Component* comp = this;
  1287. while (comp->parentComponent != nullptr)
  1288. comp = comp->parentComponent;
  1289. return const_cast <Component*> (comp);
  1290. }
  1291. bool Component::isParentOf (const Component* possibleChild) const noexcept
  1292. {
  1293. while (possibleChild != nullptr)
  1294. {
  1295. possibleChild = possibleChild->parentComponent;
  1296. if (possibleChild == this)
  1297. return true;
  1298. }
  1299. return false;
  1300. }
  1301. //==============================================================================
  1302. void Component::parentHierarchyChanged() {}
  1303. void Component::childrenChanged() {}
  1304. void Component::internalChildrenChanged()
  1305. {
  1306. if (componentListeners.isEmpty())
  1307. {
  1308. childrenChanged();
  1309. }
  1310. else
  1311. {
  1312. BailOutChecker checker (this);
  1313. childrenChanged();
  1314. if (! checker.shouldBailOut())
  1315. componentListeners.callChecked (checker, &ComponentListener::componentChildrenChanged, *this);
  1316. }
  1317. }
  1318. void Component::internalHierarchyChanged()
  1319. {
  1320. BailOutChecker checker (this);
  1321. parentHierarchyChanged();
  1322. if (checker.shouldBailOut())
  1323. return;
  1324. componentListeners.callChecked (checker, &ComponentListener::componentParentHierarchyChanged, *this);
  1325. if (checker.shouldBailOut())
  1326. return;
  1327. for (int i = childComponentList.size(); --i >= 0;)
  1328. {
  1329. childComponentList.getUnchecked (i)->internalHierarchyChanged();
  1330. if (checker.shouldBailOut())
  1331. {
  1332. // you really shouldn't delete the parent component during a callback telling you
  1333. // that it's changed..
  1334. jassertfalse;
  1335. return;
  1336. }
  1337. i = jmin (i, childComponentList.size());
  1338. }
  1339. }
  1340. //==============================================================================
  1341. #if JUCE_MODAL_LOOPS_PERMITTED
  1342. int Component::runModalLoop()
  1343. {
  1344. if (! MessageManager::getInstance()->isThisTheMessageThread())
  1345. {
  1346. // use a callback so this can be called from non-gui threads
  1347. return (int) (pointer_sized_int) MessageManager::getInstance()
  1348. ->callFunctionOnMessageThread (&ComponentHelpers::runModalLoopCallback, this);
  1349. }
  1350. if (! isCurrentlyModal())
  1351. enterModalState (true);
  1352. return ModalComponentManager::getInstance()->runEventLoopForCurrentComponent();
  1353. }
  1354. #endif
  1355. //==============================================================================
  1356. void Component::enterModalState (const bool shouldTakeKeyboardFocus,
  1357. ModalComponentManager::Callback* callback,
  1358. const bool deleteWhenDismissed)
  1359. {
  1360. // if component methods are being called from threads other than the message
  1361. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  1362. CHECK_MESSAGE_MANAGER_IS_LOCKED
  1363. // Check for an attempt to make a component modal when it already is!
  1364. // This can cause nasty problems..
  1365. jassert (! flags.currentlyModalFlag);
  1366. if (! isCurrentlyModal())
  1367. {
  1368. ModalComponentManager* const mcm = ModalComponentManager::getInstance();
  1369. mcm->startModal (this, deleteWhenDismissed);
  1370. mcm->attachCallback (this, callback);
  1371. flags.currentlyModalFlag = true;
  1372. setVisible (true);
  1373. if (shouldTakeKeyboardFocus)
  1374. grabKeyboardFocus();
  1375. }
  1376. }
  1377. void Component::exitModalState (const int returnValue)
  1378. {
  1379. if (flags.currentlyModalFlag)
  1380. {
  1381. if (MessageManager::getInstance()->isThisTheMessageThread())
  1382. {
  1383. ModalComponentManager::getInstance()->endModal (this, returnValue);
  1384. flags.currentlyModalFlag = false;
  1385. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  1386. }
  1387. else
  1388. {
  1389. class ExitModalStateMessage : public CallbackMessage
  1390. {
  1391. public:
  1392. ExitModalStateMessage (Component* const c, const int res)
  1393. : target (c), result (res) {}
  1394. void messageCallback() override
  1395. {
  1396. if (target.get() != nullptr) // (get() required for VS2003 bug)
  1397. target->exitModalState (result);
  1398. }
  1399. private:
  1400. WeakReference<Component> target;
  1401. int result;
  1402. };
  1403. (new ExitModalStateMessage (this, returnValue))->post();
  1404. }
  1405. }
  1406. }
  1407. bool Component::isCurrentlyModal() const noexcept
  1408. {
  1409. return flags.currentlyModalFlag
  1410. && getCurrentlyModalComponent() == this;
  1411. }
  1412. bool Component::isCurrentlyBlockedByAnotherModalComponent() const
  1413. {
  1414. Component* const mc = getCurrentlyModalComponent();
  1415. return ! (mc == nullptr || mc == this || mc->isParentOf (this)
  1416. || mc->canModalEventBeSentToComponent (this));
  1417. }
  1418. int JUCE_CALLTYPE Component::getNumCurrentlyModalComponents() noexcept
  1419. {
  1420. return ModalComponentManager::getInstance()->getNumModalComponents();
  1421. }
  1422. Component* JUCE_CALLTYPE Component::getCurrentlyModalComponent (int index) noexcept
  1423. {
  1424. return ModalComponentManager::getInstance()->getModalComponent (index);
  1425. }
  1426. //==============================================================================
  1427. void Component::setBroughtToFrontOnMouseClick (const bool shouldBeBroughtToFront) noexcept
  1428. {
  1429. flags.bringToFrontOnClickFlag = shouldBeBroughtToFront;
  1430. }
  1431. bool Component::isBroughtToFrontOnMouseClick() const noexcept
  1432. {
  1433. return flags.bringToFrontOnClickFlag;
  1434. }
  1435. //==============================================================================
  1436. void Component::setMouseCursor (const MouseCursor& newCursor)
  1437. {
  1438. if (cursor != newCursor)
  1439. {
  1440. cursor = newCursor;
  1441. if (flags.visibleFlag)
  1442. updateMouseCursor();
  1443. }
  1444. }
  1445. MouseCursor Component::getMouseCursor()
  1446. {
  1447. return cursor;
  1448. }
  1449. void Component::updateMouseCursor() const
  1450. {
  1451. Desktop::getInstance().getMainMouseSource().forceMouseCursorUpdate();
  1452. }
  1453. //==============================================================================
  1454. void Component::setRepaintsOnMouseActivity (const bool shouldRepaint) noexcept
  1455. {
  1456. flags.repaintOnMouseActivityFlag = shouldRepaint;
  1457. }
  1458. //==============================================================================
  1459. void Component::setAlpha (const float newAlpha)
  1460. {
  1461. const uint8 newIntAlpha = (uint8) (255 - jlimit (0, 255, roundToInt (newAlpha * 255.0)));
  1462. if (componentTransparency != newIntAlpha)
  1463. {
  1464. componentTransparency = newIntAlpha;
  1465. if (flags.hasHeavyweightPeerFlag)
  1466. {
  1467. if (ComponentPeer* const peer = getPeer())
  1468. peer->setAlpha (newAlpha);
  1469. }
  1470. else
  1471. {
  1472. repaint();
  1473. }
  1474. }
  1475. }
  1476. float Component::getAlpha() const
  1477. {
  1478. return (255 - componentTransparency) / 255.0f;
  1479. }
  1480. //==============================================================================
  1481. void Component::repaint()
  1482. {
  1483. internalRepaintUnchecked (getLocalBounds(), true);
  1484. }
  1485. void Component::repaint (const int x, const int y, const int w, const int h)
  1486. {
  1487. internalRepaint (Rectangle<int> (x, y, w, h));
  1488. }
  1489. void Component::repaint (const Rectangle<int>& area)
  1490. {
  1491. internalRepaint (area);
  1492. }
  1493. void Component::repaintParent()
  1494. {
  1495. if (parentComponent != nullptr)
  1496. parentComponent->internalRepaint (ComponentHelpers::convertToParentSpace (*this, getLocalBounds()));
  1497. }
  1498. void Component::internalRepaint (const Rectangle<int>& area)
  1499. {
  1500. const Rectangle<int> r (area.getIntersection (getLocalBounds()));
  1501. if (! r.isEmpty())
  1502. internalRepaintUnchecked (r, false);
  1503. }
  1504. void Component::internalRepaintUnchecked (const Rectangle<int>& area, const bool isEntireComponent)
  1505. {
  1506. if (flags.visibleFlag)
  1507. {
  1508. if (cachedImage != nullptr)
  1509. if (! (isEntireComponent ? cachedImage->invalidateAll()
  1510. : cachedImage->invalidate (area)))
  1511. return;
  1512. if (flags.hasHeavyweightPeerFlag)
  1513. {
  1514. // if component methods are being called from threads other than the message
  1515. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  1516. CHECK_MESSAGE_MANAGER_IS_LOCKED
  1517. if (ComponentPeer* const peer = getPeer())
  1518. {
  1519. // Tweak the scaling so that the component's integer size exactly aligns with the peer's scaled size
  1520. const Rectangle<int> peerBounds (peer->getBounds());
  1521. const Rectangle<int> scaled (area * Point<float> (peerBounds.getWidth() / (float) getWidth(),
  1522. peerBounds.getHeight() / (float) getHeight()));
  1523. peer->repaint (affineTransform != nullptr ? scaled.transformedBy (*affineTransform) : scaled);
  1524. }
  1525. }
  1526. else
  1527. {
  1528. if (parentComponent != nullptr)
  1529. parentComponent->internalRepaint (ComponentHelpers::convertToParentSpace (*this, area));
  1530. }
  1531. }
  1532. }
  1533. //==============================================================================
  1534. void Component::paint (Graphics&)
  1535. {
  1536. // if your component is marked as opaque, you must implement a paint
  1537. // method and ensure that its entire area is completely painted.
  1538. jassert (getBounds().isEmpty() || ! isOpaque());
  1539. }
  1540. void Component::paintOverChildren (Graphics&)
  1541. {
  1542. // all painting is done in the subclasses
  1543. }
  1544. //==============================================================================
  1545. void Component::paintWithinParentContext (Graphics& g)
  1546. {
  1547. g.setOrigin (getPosition());
  1548. if (cachedImage != nullptr)
  1549. cachedImage->paint (g);
  1550. else
  1551. paintEntireComponent (g, false);
  1552. }
  1553. void Component::paintComponentAndChildren (Graphics& g)
  1554. {
  1555. const Rectangle<int> clipBounds (g.getClipBounds());
  1556. if (flags.dontClipGraphicsFlag)
  1557. {
  1558. paint (g);
  1559. }
  1560. else
  1561. {
  1562. g.saveState();
  1563. if (ComponentHelpers::clipObscuredRegions (*this, g, clipBounds, Point<int>()) || ! g.isClipEmpty())
  1564. paint (g);
  1565. g.restoreState();
  1566. }
  1567. for (int i = 0; i < childComponentList.size(); ++i)
  1568. {
  1569. Component& child = *childComponentList.getUnchecked (i);
  1570. if (child.isVisible())
  1571. {
  1572. if (child.affineTransform != nullptr)
  1573. {
  1574. g.saveState();
  1575. g.addTransform (*child.affineTransform);
  1576. if ((child.flags.dontClipGraphicsFlag && ! g.isClipEmpty()) || g.reduceClipRegion (child.getBounds()))
  1577. child.paintWithinParentContext (g);
  1578. g.restoreState();
  1579. }
  1580. else if (clipBounds.intersects (child.getBounds()))
  1581. {
  1582. g.saveState();
  1583. if (child.flags.dontClipGraphicsFlag)
  1584. {
  1585. child.paintWithinParentContext (g);
  1586. }
  1587. else if (g.reduceClipRegion (child.getBounds()))
  1588. {
  1589. bool nothingClipped = true;
  1590. for (int j = i + 1; j < childComponentList.size(); ++j)
  1591. {
  1592. const Component& sibling = *childComponentList.getUnchecked (j);
  1593. if (sibling.flags.opaqueFlag && sibling.isVisible() && sibling.affineTransform == nullptr)
  1594. {
  1595. nothingClipped = false;
  1596. g.excludeClipRegion (sibling.getBounds());
  1597. }
  1598. }
  1599. if (nothingClipped || ! g.isClipEmpty())
  1600. child.paintWithinParentContext (g);
  1601. }
  1602. g.restoreState();
  1603. }
  1604. }
  1605. }
  1606. g.saveState();
  1607. paintOverChildren (g);
  1608. g.restoreState();
  1609. }
  1610. void Component::paintEntireComponent (Graphics& g, const bool ignoreAlphaLevel)
  1611. {
  1612. #if JUCE_DEBUG
  1613. flags.isInsidePaintCall = true;
  1614. #endif
  1615. if (effect != nullptr)
  1616. {
  1617. const float scale = g.getInternalContext().getPhysicalPixelScaleFactor();
  1618. const Rectangle<int> scaledBounds (getLocalBounds() * scale);
  1619. Image effectImage (flags.opaqueFlag ? Image::RGB : Image::ARGB,
  1620. scaledBounds.getWidth(), scaledBounds.getHeight(), ! flags.opaqueFlag);
  1621. {
  1622. Graphics g2 (effectImage);
  1623. g2.addTransform (AffineTransform::scale (scale));
  1624. paintComponentAndChildren (g2);
  1625. }
  1626. g.saveState();
  1627. g.addTransform (AffineTransform::scale (1.0f / scale));
  1628. effect->applyEffect (effectImage, g, scale, ignoreAlphaLevel ? 1.0f : getAlpha());
  1629. g.restoreState();
  1630. }
  1631. else if (componentTransparency > 0 && ! ignoreAlphaLevel)
  1632. {
  1633. if (componentTransparency < 255)
  1634. {
  1635. g.beginTransparencyLayer (getAlpha());
  1636. paintComponentAndChildren (g);
  1637. g.endTransparencyLayer();
  1638. }
  1639. }
  1640. else
  1641. {
  1642. paintComponentAndChildren (g);
  1643. }
  1644. #if JUCE_DEBUG
  1645. flags.isInsidePaintCall = false;
  1646. #endif
  1647. }
  1648. void Component::setPaintingIsUnclipped (const bool shouldPaintWithoutClipping) noexcept
  1649. {
  1650. flags.dontClipGraphicsFlag = shouldPaintWithoutClipping;
  1651. }
  1652. //==============================================================================
  1653. Image Component::createComponentSnapshot (const Rectangle<int>& areaToGrab,
  1654. const bool clipImageToComponentBounds)
  1655. {
  1656. Rectangle<int> r (areaToGrab);
  1657. if (clipImageToComponentBounds)
  1658. r = r.getIntersection (getLocalBounds());
  1659. if (r.isEmpty())
  1660. return Image();
  1661. Image componentImage (flags.opaqueFlag ? Image::RGB : Image::ARGB,
  1662. r.getWidth(), r.getHeight(), true);
  1663. Graphics imageContext (componentImage);
  1664. imageContext.setOrigin (-r.getPosition());
  1665. paintEntireComponent (imageContext, true);
  1666. return componentImage;
  1667. }
  1668. void Component::setComponentEffect (ImageEffectFilter* const newEffect)
  1669. {
  1670. if (effect != newEffect)
  1671. {
  1672. effect = newEffect;
  1673. repaint();
  1674. }
  1675. }
  1676. //==============================================================================
  1677. LookAndFeel& Component::getLookAndFeel() const noexcept
  1678. {
  1679. for (const Component* c = this; c != nullptr; c = c->parentComponent)
  1680. if (c->lookAndFeel != nullptr)
  1681. return *(c->lookAndFeel);
  1682. return LookAndFeel::getDefaultLookAndFeel();
  1683. }
  1684. void Component::setLookAndFeel (LookAndFeel* const newLookAndFeel)
  1685. {
  1686. if (lookAndFeel != newLookAndFeel)
  1687. {
  1688. lookAndFeel = newLookAndFeel;
  1689. sendLookAndFeelChange();
  1690. }
  1691. }
  1692. void Component::lookAndFeelChanged() {}
  1693. void Component::colourChanged() {}
  1694. void Component::sendLookAndFeelChange()
  1695. {
  1696. const WeakReference<Component> safePointer (this);
  1697. repaint();
  1698. lookAndFeelChanged();
  1699. if (safePointer != nullptr)
  1700. {
  1701. colourChanged();
  1702. if (safePointer != nullptr)
  1703. {
  1704. for (int i = childComponentList.size(); --i >= 0;)
  1705. {
  1706. childComponentList.getUnchecked (i)->sendLookAndFeelChange();
  1707. if (safePointer == nullptr)
  1708. return;
  1709. i = jmin (i, childComponentList.size());
  1710. }
  1711. }
  1712. }
  1713. }
  1714. Colour Component::findColour (const int colourId, const bool inheritFromParent) const
  1715. {
  1716. if (const var* const v = properties.getVarPointer (ComponentHelpers::getColourPropertyId (colourId)))
  1717. return Colour ((uint32) static_cast <int> (*v));
  1718. if (inheritFromParent && parentComponent != nullptr
  1719. && (lookAndFeel == nullptr || ! lookAndFeel->isColourSpecified (colourId)))
  1720. return parentComponent->findColour (colourId, true);
  1721. return getLookAndFeel().findColour (colourId);
  1722. }
  1723. bool Component::isColourSpecified (const int colourId) const
  1724. {
  1725. return properties.contains (ComponentHelpers::getColourPropertyId (colourId));
  1726. }
  1727. void Component::removeColour (const int colourId)
  1728. {
  1729. if (properties.remove (ComponentHelpers::getColourPropertyId (colourId)))
  1730. colourChanged();
  1731. }
  1732. void Component::setColour (const int colourId, Colour colour)
  1733. {
  1734. if (properties.set (ComponentHelpers::getColourPropertyId (colourId), (int) colour.getARGB()))
  1735. colourChanged();
  1736. }
  1737. void Component::copyAllExplicitColoursTo (Component& target) const
  1738. {
  1739. bool changed = false;
  1740. for (int i = properties.size(); --i >= 0;)
  1741. {
  1742. const Identifier name (properties.getName(i));
  1743. if (name.toString().startsWith ("jcclr_"))
  1744. if (target.properties.set (name, properties [name]))
  1745. changed = true;
  1746. }
  1747. if (changed)
  1748. target.colourChanged();
  1749. }
  1750. //==============================================================================
  1751. MarkerList* Component::getMarkers (bool /*xAxis*/)
  1752. {
  1753. return nullptr;
  1754. }
  1755. //==============================================================================
  1756. Component::Positioner::Positioner (Component& c) noexcept
  1757. : component (c)
  1758. {
  1759. }
  1760. Component::Positioner* Component::getPositioner() const noexcept
  1761. {
  1762. return positioner;
  1763. }
  1764. void Component::setPositioner (Positioner* newPositioner)
  1765. {
  1766. // You can only assign a positioner to the component that it was created for!
  1767. jassert (newPositioner == nullptr || this == &(newPositioner->getComponent()));
  1768. positioner = newPositioner;
  1769. }
  1770. //==============================================================================
  1771. Rectangle<int> Component::getLocalBounds() const noexcept
  1772. {
  1773. return bounds.withZeroOrigin();
  1774. }
  1775. Rectangle<int> Component::getBoundsInParent() const noexcept
  1776. {
  1777. return affineTransform == nullptr ? bounds
  1778. : bounds.transformedBy (*affineTransform);
  1779. }
  1780. void Component::getVisibleArea (RectangleList<int>& result, const bool includeSiblings) const
  1781. {
  1782. result.clear();
  1783. const Rectangle<int> unclipped (ComponentHelpers::getUnclippedArea (*this));
  1784. if (! unclipped.isEmpty())
  1785. {
  1786. result.add (unclipped);
  1787. if (includeSiblings)
  1788. {
  1789. const Component* const c = getTopLevelComponent();
  1790. ComponentHelpers::subtractObscuredRegions (*c, result, getLocalPoint (c, Point<int>()),
  1791. c->getLocalBounds(), this);
  1792. }
  1793. ComponentHelpers::subtractObscuredRegions (*this, result, Point<int>(), unclipped, nullptr);
  1794. result.consolidate();
  1795. }
  1796. }
  1797. //==============================================================================
  1798. void Component::mouseEnter (const MouseEvent&) {}
  1799. void Component::mouseExit (const MouseEvent&) {}
  1800. void Component::mouseDown (const MouseEvent&) {}
  1801. void Component::mouseUp (const MouseEvent&) {}
  1802. void Component::mouseDrag (const MouseEvent&) {}
  1803. void Component::mouseMove (const MouseEvent&) {}
  1804. void Component::mouseDoubleClick (const MouseEvent&) {}
  1805. void Component::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
  1806. {
  1807. // the base class just passes this event up to its parent..
  1808. if (parentComponent != nullptr)
  1809. parentComponent->mouseWheelMove (e.getEventRelativeTo (parentComponent), wheel);
  1810. }
  1811. void Component::mouseMagnify (const MouseEvent& e, float magnifyAmount)
  1812. {
  1813. // the base class just passes this event up to its parent..
  1814. if (parentComponent != nullptr)
  1815. parentComponent->mouseMagnify (e.getEventRelativeTo (parentComponent), magnifyAmount);
  1816. }
  1817. //==============================================================================
  1818. void Component::resized() {}
  1819. void Component::moved() {}
  1820. void Component::childBoundsChanged (Component*) {}
  1821. void Component::parentSizeChanged() {}
  1822. void Component::addComponentListener (ComponentListener* const newListener)
  1823. {
  1824. // if component methods are being called from threads other than the message
  1825. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  1826. CHECK_MESSAGE_MANAGER_IS_LOCKED
  1827. componentListeners.add (newListener);
  1828. }
  1829. void Component::removeComponentListener (ComponentListener* const listenerToRemove)
  1830. {
  1831. componentListeners.remove (listenerToRemove);
  1832. }
  1833. //==============================================================================
  1834. void Component::inputAttemptWhenModal()
  1835. {
  1836. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  1837. getLookAndFeel().playAlertSound();
  1838. }
  1839. bool Component::canModalEventBeSentToComponent (const Component*)
  1840. {
  1841. return false;
  1842. }
  1843. void Component::internalModalInputAttempt()
  1844. {
  1845. if (Component* const current = getCurrentlyModalComponent())
  1846. current->inputAttemptWhenModal();
  1847. }
  1848. //==============================================================================
  1849. void Component::postCommandMessage (const int commandId)
  1850. {
  1851. class CustomCommandMessage : public CallbackMessage
  1852. {
  1853. public:
  1854. CustomCommandMessage (Component* const c, const int command)
  1855. : target (c), commandId (command) {}
  1856. void messageCallback() override
  1857. {
  1858. if (target.get() != nullptr) // (get() required for VS2003 bug)
  1859. target->handleCommandMessage (commandId);
  1860. }
  1861. private:
  1862. WeakReference<Component> target;
  1863. int commandId;
  1864. };
  1865. (new CustomCommandMessage (this, commandId))->post();
  1866. }
  1867. void Component::handleCommandMessage (int)
  1868. {
  1869. // used by subclasses
  1870. }
  1871. //==============================================================================
  1872. void Component::addMouseListener (MouseListener* const newListener,
  1873. const bool wantsEventsForAllNestedChildComponents)
  1874. {
  1875. // if component methods are being called from threads other than the message
  1876. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  1877. CHECK_MESSAGE_MANAGER_IS_LOCKED
  1878. // If you register a component as a mouselistener for itself, it'll receive all the events
  1879. // twice - once via the direct callback that all components get anyway, and then again as a listener!
  1880. jassert ((newListener != this) || wantsEventsForAllNestedChildComponents);
  1881. if (mouseListeners == nullptr)
  1882. mouseListeners = new MouseListenerList();
  1883. mouseListeners->addListener (newListener, wantsEventsForAllNestedChildComponents);
  1884. }
  1885. void Component::removeMouseListener (MouseListener* const listenerToRemove)
  1886. {
  1887. // if component methods are being called from threads other than the message
  1888. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  1889. CHECK_MESSAGE_MANAGER_IS_LOCKED
  1890. if (mouseListeners != nullptr)
  1891. mouseListeners->removeListener (listenerToRemove);
  1892. }
  1893. //==============================================================================
  1894. void Component::internalMouseEnter (MouseInputSource source, Point<int> relativePos, Time time)
  1895. {
  1896. if (isCurrentlyBlockedByAnotherModalComponent())
  1897. {
  1898. // if something else is modal, always just show a normal mouse cursor
  1899. source.showMouseCursor (MouseCursor::NormalCursor);
  1900. return;
  1901. }
  1902. if (flags.repaintOnMouseActivityFlag)
  1903. repaint();
  1904. BailOutChecker checker (this);
  1905. const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
  1906. this, this, time, relativePos, time, 0, false);
  1907. mouseEnter (me);
  1908. if (checker.shouldBailOut())
  1909. return;
  1910. Desktop::getInstance().getMouseListeners().callChecked (checker, &MouseListener::mouseEnter, me);
  1911. MouseListenerList::sendMouseEvent (*this, checker, &MouseListener::mouseEnter, me);
  1912. }
  1913. void Component::internalMouseExit (MouseInputSource source, Point<int> relativePos, Time time)
  1914. {
  1915. if (flags.repaintOnMouseActivityFlag)
  1916. repaint();
  1917. BailOutChecker checker (this);
  1918. const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
  1919. this, this, time, relativePos, time, 0, false);
  1920. mouseExit (me);
  1921. if (checker.shouldBailOut())
  1922. return;
  1923. Desktop::getInstance().getMouseListeners().callChecked (checker, &MouseListener::mouseExit, me);
  1924. MouseListenerList::sendMouseEvent (*this, checker, &MouseListener::mouseExit, me);
  1925. }
  1926. void Component::internalMouseDown (MouseInputSource source, Point<int> relativePos, Time time)
  1927. {
  1928. Desktop& desktop = Desktop::getInstance();
  1929. BailOutChecker checker (this);
  1930. if (isCurrentlyBlockedByAnotherModalComponent())
  1931. {
  1932. flags.mouseDownWasBlocked = true;
  1933. internalModalInputAttempt();
  1934. if (checker.shouldBailOut())
  1935. return;
  1936. // If processing the input attempt has exited the modal loop, we'll allow the event
  1937. // to be delivered..
  1938. if (isCurrentlyBlockedByAnotherModalComponent())
  1939. {
  1940. // allow blocked mouse-events to go to global listeners..
  1941. const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
  1942. this, this, time, relativePos, time,
  1943. source.getNumberOfMultipleClicks(), false);
  1944. desktop.getMouseListeners().callChecked (checker, &MouseListener::mouseDown, me);
  1945. return;
  1946. }
  1947. }
  1948. flags.mouseDownWasBlocked = false;
  1949. for (Component* c = this; c != nullptr; c = c->parentComponent)
  1950. {
  1951. if (c->isBroughtToFrontOnMouseClick())
  1952. {
  1953. c->toFront (true);
  1954. if (checker.shouldBailOut())
  1955. return;
  1956. }
  1957. }
  1958. if (! flags.dontFocusOnMouseClickFlag)
  1959. {
  1960. grabFocusInternal (focusChangedByMouseClick, true);
  1961. if (checker.shouldBailOut())
  1962. return;
  1963. }
  1964. if (flags.repaintOnMouseActivityFlag)
  1965. repaint();
  1966. const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
  1967. this, this, time, relativePos, time,
  1968. source.getNumberOfMultipleClicks(), false);
  1969. mouseDown (me);
  1970. if (checker.shouldBailOut())
  1971. return;
  1972. desktop.getMouseListeners().callChecked (checker, &MouseListener::mouseDown, me);
  1973. MouseListenerList::sendMouseEvent (*this, checker, &MouseListener::mouseDown, me);
  1974. }
  1975. void Component::internalMouseUp (MouseInputSource source, Point<int> relativePos,
  1976. Time time, const ModifierKeys oldModifiers)
  1977. {
  1978. if (flags.mouseDownWasBlocked && isCurrentlyBlockedByAnotherModalComponent())
  1979. return;
  1980. BailOutChecker checker (this);
  1981. if (flags.repaintOnMouseActivityFlag)
  1982. repaint();
  1983. const MouseEvent me (source, relativePos,
  1984. oldModifiers, this, this, time,
  1985. getLocalPoint (nullptr, source.getLastMouseDownPosition()),
  1986. source.getLastMouseDownTime(),
  1987. source.getNumberOfMultipleClicks(),
  1988. source.hasMouseMovedSignificantlySincePressed());
  1989. mouseUp (me);
  1990. if (checker.shouldBailOut())
  1991. return;
  1992. Desktop& desktop = Desktop::getInstance();
  1993. desktop.getMouseListeners().callChecked (checker, &MouseListener::mouseUp, me);
  1994. MouseListenerList::sendMouseEvent (*this, checker, &MouseListener::mouseUp, me);
  1995. if (checker.shouldBailOut())
  1996. return;
  1997. // check for double-click
  1998. if (me.getNumberOfClicks() >= 2)
  1999. {
  2000. mouseDoubleClick (me);
  2001. if (checker.shouldBailOut())
  2002. return;
  2003. desktop.mouseListeners.callChecked (checker, &MouseListener::mouseDoubleClick, me);
  2004. MouseListenerList::sendMouseEvent (*this, checker, &MouseListener::mouseDoubleClick, me);
  2005. }
  2006. }
  2007. void Component::internalMouseDrag (MouseInputSource source, Point<int> relativePos, Time time)
  2008. {
  2009. if (! isCurrentlyBlockedByAnotherModalComponent())
  2010. {
  2011. BailOutChecker checker (this);
  2012. const MouseEvent me (source, relativePos,
  2013. source.getCurrentModifiers(), this, this, time,
  2014. getLocalPoint (nullptr, source.getLastMouseDownPosition()),
  2015. source.getLastMouseDownTime(),
  2016. source.getNumberOfMultipleClicks(),
  2017. source.hasMouseMovedSignificantlySincePressed());
  2018. mouseDrag (me);
  2019. if (checker.shouldBailOut())
  2020. return;
  2021. Desktop::getInstance().getMouseListeners().callChecked (checker, &MouseListener::mouseDrag, me);
  2022. MouseListenerList::sendMouseEvent (*this, checker, &MouseListener::mouseDrag, me);
  2023. }
  2024. }
  2025. void Component::internalMouseMove (MouseInputSource source, Point<int> relativePos, Time time)
  2026. {
  2027. Desktop& desktop = Desktop::getInstance();
  2028. if (isCurrentlyBlockedByAnotherModalComponent())
  2029. {
  2030. // allow blocked mouse-events to go to global listeners..
  2031. desktop.sendMouseMove();
  2032. }
  2033. else
  2034. {
  2035. BailOutChecker checker (this);
  2036. const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
  2037. this, this, time, relativePos, time, 0, false);
  2038. mouseMove (me);
  2039. if (checker.shouldBailOut())
  2040. return;
  2041. desktop.getMouseListeners().callChecked (checker, &MouseListener::mouseMove, me);
  2042. MouseListenerList::sendMouseEvent (*this, checker, &MouseListener::mouseMove, me);
  2043. }
  2044. }
  2045. void Component::internalMouseWheel (MouseInputSource source, Point<int> relativePos,
  2046. Time time, const MouseWheelDetails& wheel)
  2047. {
  2048. Desktop& desktop = Desktop::getInstance();
  2049. BailOutChecker checker (this);
  2050. const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
  2051. this, this, time, relativePos, time, 0, false);
  2052. if (isCurrentlyBlockedByAnotherModalComponent())
  2053. {
  2054. // allow blocked mouse-events to go to global listeners..
  2055. desktop.mouseListeners.callChecked (checker, &MouseListener::mouseWheelMove, me, wheel);
  2056. }
  2057. else
  2058. {
  2059. mouseWheelMove (me, wheel);
  2060. if (checker.shouldBailOut())
  2061. return;
  2062. desktop.mouseListeners.callChecked (checker, &MouseListener::mouseWheelMove, me, wheel);
  2063. if (! checker.shouldBailOut())
  2064. MouseListenerList::sendWheelEvent (*this, checker, me, wheel);
  2065. }
  2066. }
  2067. void Component::internalMagnifyGesture (MouseInputSource source, Point<int> relativePos,
  2068. Time time, float amount)
  2069. {
  2070. if (! isCurrentlyBlockedByAnotherModalComponent())
  2071. {
  2072. const MouseEvent me (source, relativePos, source.getCurrentModifiers(),
  2073. this, this, time, relativePos, time, 0, false);
  2074. mouseMagnify (me, amount);
  2075. }
  2076. }
  2077. void Component::sendFakeMouseMove() const
  2078. {
  2079. MouseInputSource mainMouse = Desktop::getInstance().getMainMouseSource();
  2080. if (! mainMouse.isDragging())
  2081. mainMouse.triggerFakeMove();
  2082. }
  2083. void JUCE_CALLTYPE Component::beginDragAutoRepeat (const int interval)
  2084. {
  2085. Desktop::getInstance().beginDragAutoRepeat (interval);
  2086. }
  2087. //==============================================================================
  2088. void Component::broughtToFront()
  2089. {
  2090. }
  2091. void Component::internalBroughtToFront()
  2092. {
  2093. if (flags.hasHeavyweightPeerFlag)
  2094. Desktop::getInstance().componentBroughtToFront (this);
  2095. BailOutChecker checker (this);
  2096. broughtToFront();
  2097. if (checker.shouldBailOut())
  2098. return;
  2099. componentListeners.callChecked (checker, &ComponentListener::componentBroughtToFront, *this);
  2100. if (checker.shouldBailOut())
  2101. return;
  2102. // When brought to the front and there's a modal component blocking this one,
  2103. // we need to bring the modal one to the front instead..
  2104. if (Component* const cm = getCurrentlyModalComponent())
  2105. if (cm->getTopLevelComponent() != getTopLevelComponent())
  2106. ModalComponentManager::getInstance()->bringModalComponentsToFront (false); // very important that this is false, otherwise in Windows,
  2107. // non-front components can't get focus when another modal comp is
  2108. // active, and therefore can't receive mouse-clicks
  2109. }
  2110. //==============================================================================
  2111. void Component::focusGained (FocusChangeType) {}
  2112. void Component::focusLost (FocusChangeType) {}
  2113. void Component::focusOfChildComponentChanged (FocusChangeType) {}
  2114. void Component::internalFocusGain (const FocusChangeType cause)
  2115. {
  2116. internalFocusGain (cause, WeakReference<Component> (this));
  2117. }
  2118. void Component::internalFocusGain (const FocusChangeType cause, const WeakReference<Component>& safePointer)
  2119. {
  2120. focusGained (cause);
  2121. if (safePointer != nullptr)
  2122. internalChildFocusChange (cause, safePointer);
  2123. }
  2124. void Component::internalFocusLoss (const FocusChangeType cause)
  2125. {
  2126. const WeakReference<Component> safePointer (this);
  2127. focusLost (focusChangedDirectly);
  2128. if (safePointer != nullptr)
  2129. internalChildFocusChange (cause, safePointer);
  2130. }
  2131. void Component::internalChildFocusChange (FocusChangeType cause, const WeakReference<Component>& safePointer)
  2132. {
  2133. const bool childIsNowFocused = hasKeyboardFocus (true);
  2134. if (flags.childCompFocusedFlag != childIsNowFocused)
  2135. {
  2136. flags.childCompFocusedFlag = childIsNowFocused;
  2137. focusOfChildComponentChanged (cause);
  2138. if (safePointer == nullptr)
  2139. return;
  2140. }
  2141. if (parentComponent != nullptr)
  2142. parentComponent->internalChildFocusChange (cause, WeakReference<Component> (parentComponent));
  2143. }
  2144. void Component::setWantsKeyboardFocus (const bool wantsFocus) noexcept
  2145. {
  2146. flags.wantsFocusFlag = wantsFocus;
  2147. }
  2148. void Component::setMouseClickGrabsKeyboardFocus (const bool shouldGrabFocus)
  2149. {
  2150. flags.dontFocusOnMouseClickFlag = ! shouldGrabFocus;
  2151. }
  2152. bool Component::getMouseClickGrabsKeyboardFocus() const noexcept
  2153. {
  2154. return ! flags.dontFocusOnMouseClickFlag;
  2155. }
  2156. bool Component::getWantsKeyboardFocus() const noexcept
  2157. {
  2158. return flags.wantsFocusFlag && ! flags.isDisabledFlag;
  2159. }
  2160. void Component::setFocusContainer (const bool shouldBeFocusContainer) noexcept
  2161. {
  2162. flags.isFocusContainerFlag = shouldBeFocusContainer;
  2163. }
  2164. bool Component::isFocusContainer() const noexcept
  2165. {
  2166. return flags.isFocusContainerFlag;
  2167. }
  2168. static const Identifier juce_explicitFocusOrderId ("_jexfo");
  2169. int Component::getExplicitFocusOrder() const
  2170. {
  2171. return properties [juce_explicitFocusOrderId];
  2172. }
  2173. void Component::setExplicitFocusOrder (const int newFocusOrderIndex)
  2174. {
  2175. properties.set (juce_explicitFocusOrderId, newFocusOrderIndex);
  2176. }
  2177. KeyboardFocusTraverser* Component::createFocusTraverser()
  2178. {
  2179. if (flags.isFocusContainerFlag || parentComponent == nullptr)
  2180. return new KeyboardFocusTraverser();
  2181. return parentComponent->createFocusTraverser();
  2182. }
  2183. void Component::takeKeyboardFocus (const FocusChangeType cause)
  2184. {
  2185. // give the focus to this component
  2186. if (currentlyFocusedComponent != this)
  2187. {
  2188. // get the focus onto our desktop window
  2189. if (ComponentPeer* const peer = getPeer())
  2190. {
  2191. const WeakReference<Component> safePointer (this);
  2192. peer->grabFocus();
  2193. if (peer->isFocused() && currentlyFocusedComponent != this)
  2194. {
  2195. WeakReference<Component> componentLosingFocus (currentlyFocusedComponent);
  2196. currentlyFocusedComponent = this;
  2197. Desktop::getInstance().triggerFocusCallback();
  2198. // call this after setting currentlyFocusedComponent so that the one that's
  2199. // losing it has a chance to see where focus is going
  2200. if (componentLosingFocus != nullptr)
  2201. componentLosingFocus->internalFocusLoss (cause);
  2202. if (currentlyFocusedComponent == this)
  2203. internalFocusGain (cause, safePointer);
  2204. }
  2205. }
  2206. }
  2207. }
  2208. void Component::grabFocusInternal (const FocusChangeType cause, const bool canTryParent)
  2209. {
  2210. if (isShowing())
  2211. {
  2212. if (flags.wantsFocusFlag && (isEnabled() || parentComponent == nullptr))
  2213. {
  2214. takeKeyboardFocus (cause);
  2215. }
  2216. else
  2217. {
  2218. if (isParentOf (currentlyFocusedComponent)
  2219. && currentlyFocusedComponent->isShowing())
  2220. {
  2221. // do nothing if the focused component is actually a child of ours..
  2222. }
  2223. else
  2224. {
  2225. // find the default child component..
  2226. ScopedPointer <KeyboardFocusTraverser> traverser (createFocusTraverser());
  2227. if (traverser != nullptr)
  2228. {
  2229. Component* const defaultComp = traverser->getDefaultComponent (this);
  2230. traverser = nullptr;
  2231. if (defaultComp != nullptr)
  2232. {
  2233. defaultComp->grabFocusInternal (cause, false);
  2234. return;
  2235. }
  2236. }
  2237. if (canTryParent && parentComponent != nullptr)
  2238. {
  2239. // if no children want it and we're allowed to try our parent comp,
  2240. // then pass up to parent, which will try our siblings.
  2241. parentComponent->grabFocusInternal (cause, true);
  2242. }
  2243. }
  2244. }
  2245. }
  2246. }
  2247. void Component::grabKeyboardFocus()
  2248. {
  2249. // if component methods are being called from threads other than the message
  2250. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  2251. CHECK_MESSAGE_MANAGER_IS_LOCKED
  2252. grabFocusInternal (focusChangedDirectly, true);
  2253. }
  2254. void Component::moveKeyboardFocusToSibling (const bool moveToNext)
  2255. {
  2256. // if component methods are being called from threads other than the message
  2257. // thread, you'll need to use a MessageManagerLock object to make sure it's thread-safe.
  2258. CHECK_MESSAGE_MANAGER_IS_LOCKED
  2259. if (parentComponent != nullptr)
  2260. {
  2261. ScopedPointer <KeyboardFocusTraverser> traverser (createFocusTraverser());
  2262. if (traverser != nullptr)
  2263. {
  2264. Component* const nextComp = moveToNext ? traverser->getNextComponent (this)
  2265. : traverser->getPreviousComponent (this);
  2266. traverser = nullptr;
  2267. if (nextComp != nullptr)
  2268. {
  2269. if (nextComp->isCurrentlyBlockedByAnotherModalComponent())
  2270. {
  2271. const WeakReference<Component> nextCompPointer (nextComp);
  2272. internalModalInputAttempt();
  2273. if (nextCompPointer == nullptr || nextComp->isCurrentlyBlockedByAnotherModalComponent())
  2274. return;
  2275. }
  2276. nextComp->grabFocusInternal (focusChangedByTabKey, true);
  2277. return;
  2278. }
  2279. }
  2280. parentComponent->moveKeyboardFocusToSibling (moveToNext);
  2281. }
  2282. }
  2283. bool Component::hasKeyboardFocus (const bool trueIfChildIsFocused) const
  2284. {
  2285. return (currentlyFocusedComponent == this)
  2286. || (trueIfChildIsFocused && isParentOf (currentlyFocusedComponent));
  2287. }
  2288. Component* JUCE_CALLTYPE Component::getCurrentlyFocusedComponent() noexcept
  2289. {
  2290. return currentlyFocusedComponent;
  2291. }
  2292. void JUCE_CALLTYPE Component::unfocusAllComponents()
  2293. {
  2294. if (Component* c = getCurrentlyFocusedComponent())
  2295. c->giveAwayFocus (true);
  2296. }
  2297. void Component::giveAwayFocus (const bool sendFocusLossEvent)
  2298. {
  2299. Component* const componentLosingFocus = currentlyFocusedComponent;
  2300. currentlyFocusedComponent = nullptr;
  2301. if (sendFocusLossEvent && componentLosingFocus != nullptr)
  2302. componentLosingFocus->internalFocusLoss (focusChangedDirectly);
  2303. Desktop::getInstance().triggerFocusCallback();
  2304. }
  2305. //==============================================================================
  2306. bool Component::isEnabled() const noexcept
  2307. {
  2308. return (! flags.isDisabledFlag)
  2309. && (parentComponent == nullptr || parentComponent->isEnabled());
  2310. }
  2311. void Component::setEnabled (const bool shouldBeEnabled)
  2312. {
  2313. if (flags.isDisabledFlag == shouldBeEnabled)
  2314. {
  2315. flags.isDisabledFlag = ! shouldBeEnabled;
  2316. // if any parent components are disabled, setting our flag won't make a difference,
  2317. // so no need to send a change message
  2318. if (parentComponent == nullptr || parentComponent->isEnabled())
  2319. sendEnablementChangeMessage();
  2320. }
  2321. }
  2322. void Component::enablementChanged() {}
  2323. void Component::sendEnablementChangeMessage()
  2324. {
  2325. const WeakReference<Component> safePointer (this);
  2326. enablementChanged();
  2327. if (safePointer == nullptr)
  2328. return;
  2329. for (int i = getNumChildComponents(); --i >= 0;)
  2330. {
  2331. if (Component* const c = getChildComponent (i))
  2332. {
  2333. c->sendEnablementChangeMessage();
  2334. if (safePointer == nullptr)
  2335. return;
  2336. }
  2337. }
  2338. }
  2339. //==============================================================================
  2340. bool Component::isMouseOver (const bool includeChildren) const
  2341. {
  2342. const Array<MouseInputSource>& mouseSources = Desktop::getInstance().getMouseSources();
  2343. for (MouseInputSource* mi = mouseSources.begin(), * const e = mouseSources.end(); mi != e; ++mi)
  2344. {
  2345. Component* const c = mi->getComponentUnderMouse();
  2346. if ((c == this || (includeChildren && isParentOf (c)))
  2347. && c->reallyContains (c->getLocalPoint (nullptr, mi->getScreenPosition()), false)
  2348. && (mi->isMouse() || mi->isDragging()))
  2349. return true;
  2350. }
  2351. return false;
  2352. }
  2353. bool Component::isMouseButtonDown() const
  2354. {
  2355. const Array<MouseInputSource>& mouseSources = Desktop::getInstance().getMouseSources();
  2356. for (MouseInputSource* mi = mouseSources.begin(), * const e = mouseSources.end(); mi != e; ++mi)
  2357. if (mi->isDragging() && mi->getComponentUnderMouse() == this)
  2358. return true;
  2359. return false;
  2360. }
  2361. bool Component::isMouseOverOrDragging() const
  2362. {
  2363. const Array<MouseInputSource>& mouseSources = Desktop::getInstance().getMouseSources();
  2364. for (MouseInputSource* mi = mouseSources.begin(), * const e = mouseSources.end(); mi != e; ++mi)
  2365. if (mi->getComponentUnderMouse() == this
  2366. && (mi->isMouse() || mi->isDragging()))
  2367. return true;
  2368. return false;
  2369. }
  2370. bool JUCE_CALLTYPE Component::isMouseButtonDownAnywhere() noexcept
  2371. {
  2372. return ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown();
  2373. }
  2374. Point<int> Component::getMouseXYRelative() const
  2375. {
  2376. return getLocalPoint (nullptr, Desktop::getMousePosition());
  2377. }
  2378. //==============================================================================
  2379. void Component::addKeyListener (KeyListener* const newListener)
  2380. {
  2381. if (keyListeners == nullptr)
  2382. keyListeners = new Array <KeyListener*>();
  2383. keyListeners->addIfNotAlreadyThere (newListener);
  2384. }
  2385. void Component::removeKeyListener (KeyListener* const listenerToRemove)
  2386. {
  2387. if (keyListeners != nullptr)
  2388. keyListeners->removeFirstMatchingValue (listenerToRemove);
  2389. }
  2390. bool Component::keyPressed (const KeyPress&) { return false; }
  2391. bool Component::keyStateChanged (const bool /*isKeyDown*/) { return false; }
  2392. void Component::modifierKeysChanged (const ModifierKeys& modifiers)
  2393. {
  2394. if (parentComponent != nullptr)
  2395. parentComponent->modifierKeysChanged (modifiers);
  2396. }
  2397. void Component::internalModifierKeysChanged()
  2398. {
  2399. sendFakeMouseMove();
  2400. modifierKeysChanged (ModifierKeys::getCurrentModifiers());
  2401. }
  2402. //==============================================================================
  2403. Component::BailOutChecker::BailOutChecker (Component* const component)
  2404. : safePointer (component)
  2405. {
  2406. jassert (component != nullptr);
  2407. }
  2408. bool Component::BailOutChecker::shouldBailOut() const noexcept
  2409. {
  2410. return safePointer == nullptr;
  2411. }