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.

3666 lines
108KB

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