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.

2998 lines
94KB

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