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.

2996 lines
88KB

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