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.

3151 lines
88KB

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