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

3051 lines
96KB

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