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.

575 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. static uint32 lastUniquePeerID = 1;
  18. //==============================================================================
  19. ComponentPeer::ComponentPeer (Component& comp, int flags)
  20. : component (comp),
  21. styleFlags (flags),
  22. uniqueID (lastUniquePeerID += 2) // increment by 2 so that this can never hit 0
  23. {
  24. Desktop::getInstance().peers.add (this);
  25. }
  26. ComponentPeer::~ComponentPeer()
  27. {
  28. auto& desktop = Desktop::getInstance();
  29. desktop.peers.removeFirstMatchingValue (this);
  30. desktop.triggerFocusCallback();
  31. }
  32. //==============================================================================
  33. int ComponentPeer::getNumPeers() noexcept
  34. {
  35. return Desktop::getInstance().peers.size();
  36. }
  37. ComponentPeer* ComponentPeer::getPeer (const int index) noexcept
  38. {
  39. return Desktop::getInstance().peers [index];
  40. }
  41. ComponentPeer* ComponentPeer::getPeerFor (const Component* const component) noexcept
  42. {
  43. for (auto* peer : Desktop::getInstance().peers)
  44. if (&(peer->getComponent()) == component)
  45. return peer;
  46. return nullptr;
  47. }
  48. bool ComponentPeer::isValidPeer (const ComponentPeer* const peer) noexcept
  49. {
  50. return Desktop::getInstance().peers.contains (const_cast<ComponentPeer*> (peer));
  51. }
  52. void ComponentPeer::updateBounds()
  53. {
  54. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, component.getBoundsInParent()), false);
  55. }
  56. bool ComponentPeer::isKioskMode() const
  57. {
  58. return Desktop::getInstance().getKioskModeComponent() == &component;
  59. }
  60. //==============================================================================
  61. void ComponentPeer::handleMouseEvent (MouseInputSource::InputSourceType type, Point<float> pos, ModifierKeys newMods,
  62. float newPressure, float newOrientation, int64 time, PenDetails pen, int touchIndex)
  63. {
  64. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  65. MouseInputSource (*mouse).handleEvent (*this, pos, time, newMods, newPressure, newOrientation, pen);
  66. }
  67. void ComponentPeer::handleMouseWheel (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, const MouseWheelDetails& wheel, int touchIndex)
  68. {
  69. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  70. MouseInputSource (*mouse).handleWheel (*this, pos, time, wheel);
  71. }
  72. void ComponentPeer::handleMagnifyGesture (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, float scaleFactor, int touchIndex)
  73. {
  74. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  75. MouseInputSource (*mouse).handleMagnifyGesture (*this, pos, time, scaleFactor);
  76. }
  77. //==============================================================================
  78. void ComponentPeer::handlePaint (LowLevelGraphicsContext& contextToPaintTo)
  79. {
  80. ModifierKeys::updateCurrentModifiers();
  81. Graphics g (contextToPaintTo);
  82. if (component.isTransformed())
  83. g.addTransform (component.getTransform());
  84. auto peerBounds = getBounds();
  85. if (peerBounds.getWidth() != component.getWidth() || peerBounds.getHeight() != component.getHeight())
  86. // Tweak the scaling so that the component's integer size exactly aligns with the peer's scaled size
  87. g.addTransform (AffineTransform::scale (peerBounds.getWidth() / (float) component.getWidth(),
  88. peerBounds.getHeight() / (float) component.getHeight()));
  89. #if JUCE_ENABLE_REPAINT_DEBUGGING
  90. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  91. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  92. #endif
  93. {
  94. g.saveState();
  95. }
  96. #endif
  97. JUCE_TRY
  98. {
  99. component.paintEntireComponent (g, true);
  100. }
  101. JUCE_CATCH_EXCEPTION
  102. #if JUCE_ENABLE_REPAINT_DEBUGGING
  103. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  104. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  105. #endif
  106. {
  107. // enabling this code will fill all areas that get repainted with a colour overlay, to show
  108. // clearly when things are being repainted.
  109. g.restoreState();
  110. static Random rng;
  111. g.fillAll (Colour ((uint8) rng.nextInt (255),
  112. (uint8) rng.nextInt (255),
  113. (uint8) rng.nextInt (255),
  114. (uint8) 0x50));
  115. }
  116. #endif
  117. /** If this fails, it's probably be because your CPU floating-point precision mode has
  118. been set to low.. This setting is sometimes changed by things like Direct3D, and can
  119. mess up a lot of the calculations that the library needs to do.
  120. */
  121. jassert (roundToInt (10.1f) == 10);
  122. }
  123. Component* ComponentPeer::getTargetForKeyPress()
  124. {
  125. auto* c = Component::getCurrentlyFocusedComponent();
  126. if (c == nullptr)
  127. c = &component;
  128. if (c->isCurrentlyBlockedByAnotherModalComponent())
  129. if (auto* currentModalComp = Component::getCurrentlyModalComponent())
  130. c = currentModalComp;
  131. return c;
  132. }
  133. bool ComponentPeer::handleKeyPress (const int keyCode, const juce_wchar textCharacter)
  134. {
  135. ModifierKeys::updateCurrentModifiers();
  136. return handleKeyPress (KeyPress (keyCode,
  137. ModifierKeys::getCurrentModifiers().withoutMouseButtons(),
  138. textCharacter));
  139. }
  140. bool ComponentPeer::handleKeyPress (const KeyPress& keyInfo)
  141. {
  142. bool keyWasUsed = false;
  143. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  144. {
  145. const WeakReference<Component> deletionChecker (target);
  146. if (auto* keyListeners = target->keyListeners.get())
  147. {
  148. for (int i = keyListeners->size(); --i >= 0;)
  149. {
  150. keyWasUsed = keyListeners->getUnchecked(i)->keyPressed (keyInfo, target);
  151. if (keyWasUsed || deletionChecker == nullptr)
  152. return keyWasUsed;
  153. i = jmin (i, keyListeners->size());
  154. }
  155. }
  156. keyWasUsed = target->keyPressed (keyInfo);
  157. if (keyWasUsed || deletionChecker == nullptr)
  158. break;
  159. if (auto* currentlyFocused = Component::getCurrentlyFocusedComponent())
  160. {
  161. const bool isTab = (keyInfo == KeyPress::tabKey);
  162. const bool isShiftTab = (keyInfo == KeyPress (KeyPress::tabKey, ModifierKeys::shiftModifier, 0));
  163. if (isTab || isShiftTab)
  164. {
  165. currentlyFocused->moveKeyboardFocusToSibling (isTab);
  166. keyWasUsed = (currentlyFocused != Component::getCurrentlyFocusedComponent());
  167. if (keyWasUsed || deletionChecker == nullptr)
  168. break;
  169. }
  170. }
  171. }
  172. return keyWasUsed;
  173. }
  174. bool ComponentPeer::handleKeyUpOrDown (const bool isKeyDown)
  175. {
  176. ModifierKeys::updateCurrentModifiers();
  177. bool keyWasUsed = false;
  178. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  179. {
  180. const WeakReference<Component> deletionChecker (target);
  181. keyWasUsed = target->keyStateChanged (isKeyDown);
  182. if (keyWasUsed || deletionChecker == nullptr)
  183. break;
  184. if (auto* keyListeners = target->keyListeners.get())
  185. {
  186. for (int i = keyListeners->size(); --i >= 0;)
  187. {
  188. keyWasUsed = keyListeners->getUnchecked(i)->keyStateChanged (isKeyDown, target);
  189. if (keyWasUsed || deletionChecker == nullptr)
  190. return keyWasUsed;
  191. i = jmin (i, keyListeners->size());
  192. }
  193. }
  194. }
  195. return keyWasUsed;
  196. }
  197. void ComponentPeer::handleModifierKeysChange()
  198. {
  199. ModifierKeys::updateCurrentModifiers();
  200. auto* target = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  201. if (target == nullptr)
  202. target = Component::getCurrentlyFocusedComponent();
  203. if (target == nullptr)
  204. target = &component;
  205. target->internalModifierKeysChanged();
  206. }
  207. TextInputTarget* ComponentPeer::findCurrentTextInputTarget()
  208. {
  209. auto* c = Component::getCurrentlyFocusedComponent();
  210. if (c == &component || component.isParentOf (c))
  211. if (auto* ti = dynamic_cast<TextInputTarget*> (c))
  212. if (ti->isTextInputActive())
  213. return ti;
  214. return nullptr;
  215. }
  216. void ComponentPeer::dismissPendingTextInput() {}
  217. //==============================================================================
  218. void ComponentPeer::handleBroughtToFront()
  219. {
  220. ModifierKeys::updateCurrentModifiers();
  221. component.internalBroughtToFront();
  222. }
  223. void ComponentPeer::setConstrainer (ComponentBoundsConstrainer* const newConstrainer) noexcept
  224. {
  225. constrainer = newConstrainer;
  226. }
  227. void ComponentPeer::handleMovedOrResized()
  228. {
  229. ModifierKeys::updateCurrentModifiers();
  230. const bool nowMinimised = isMinimised();
  231. if (component.flags.hasHeavyweightPeerFlag && ! nowMinimised)
  232. {
  233. const WeakReference<Component> deletionChecker (&component);
  234. auto newBounds = Component::ComponentHelpers::rawPeerPositionToLocal (component, getBounds());
  235. auto oldBounds = component.getBounds();
  236. const bool wasMoved = (oldBounds.getPosition() != newBounds.getPosition());
  237. const bool wasResized = (oldBounds.getWidth() != newBounds.getWidth() || oldBounds.getHeight() != newBounds.getHeight());
  238. if (wasMoved || wasResized)
  239. {
  240. component.boundsRelativeToParent = newBounds;
  241. if (wasResized)
  242. component.repaint();
  243. component.sendMovedResizedMessages (wasMoved, wasResized);
  244. if (deletionChecker == nullptr)
  245. return;
  246. }
  247. }
  248. if (isWindowMinimised != nowMinimised)
  249. {
  250. isWindowMinimised = nowMinimised;
  251. component.minimisationStateChanged (nowMinimised);
  252. component.sendVisibilityChangeMessage();
  253. }
  254. if (! isFullScreen())
  255. lastNonFullscreenBounds = component.getBounds();
  256. }
  257. void ComponentPeer::handleFocusGain()
  258. {
  259. ModifierKeys::updateCurrentModifiers();
  260. if (component.isParentOf (lastFocusedComponent))
  261. {
  262. Component::currentlyFocusedComponent = lastFocusedComponent;
  263. Desktop::getInstance().triggerFocusCallback();
  264. lastFocusedComponent->internalFocusGain (Component::focusChangedDirectly);
  265. }
  266. else
  267. {
  268. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  269. component.grabKeyboardFocus();
  270. else
  271. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  272. }
  273. }
  274. void ComponentPeer::handleFocusLoss()
  275. {
  276. ModifierKeys::updateCurrentModifiers();
  277. if (component.hasKeyboardFocus (true))
  278. {
  279. lastFocusedComponent = Component::currentlyFocusedComponent;
  280. if (lastFocusedComponent != nullptr)
  281. {
  282. Component::currentlyFocusedComponent = nullptr;
  283. Desktop::getInstance().triggerFocusCallback();
  284. lastFocusedComponent->internalFocusLoss (Component::focusChangedByMouseClick);
  285. }
  286. }
  287. }
  288. Component* ComponentPeer::getLastFocusedSubcomponent() const noexcept
  289. {
  290. return (component.isParentOf (lastFocusedComponent) && lastFocusedComponent->isShowing())
  291. ? static_cast<Component*> (lastFocusedComponent)
  292. : &component;
  293. }
  294. void ComponentPeer::handleScreenSizeChange()
  295. {
  296. ModifierKeys::updateCurrentModifiers();
  297. component.parentSizeChanged();
  298. handleMovedOrResized();
  299. }
  300. void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept
  301. {
  302. lastNonFullscreenBounds = newBounds;
  303. }
  304. const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const noexcept
  305. {
  306. return lastNonFullscreenBounds;
  307. }
  308. Point<int> ComponentPeer::localToGlobal (Point<int> p) { return localToGlobal (p.toFloat()).roundToInt(); }
  309. Point<int> ComponentPeer::globalToLocal (Point<int> p) { return globalToLocal (p.toFloat()).roundToInt(); }
  310. Rectangle<int> ComponentPeer::localToGlobal (const Rectangle<int>& relativePosition)
  311. {
  312. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  313. }
  314. Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPosition)
  315. {
  316. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  317. }
  318. Rectangle<int> ComponentPeer::getAreaCoveredBy (Component& subComponent) const
  319. {
  320. return ScalingHelpers::scaledScreenPosToUnscaled
  321. (component, component.getLocalArea (&subComponent, subComponent.getLocalBounds()));
  322. }
  323. //==============================================================================
  324. namespace DragHelpers
  325. {
  326. static bool isFileDrag (const ComponentPeer::DragInfo& info)
  327. {
  328. return info.files.size() > 0;
  329. }
  330. static bool isSuitableTarget (const ComponentPeer::DragInfo& info, Component* target)
  331. {
  332. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target) != nullptr
  333. : dynamic_cast<TextDragAndDropTarget*> (target) != nullptr;
  334. }
  335. static bool isInterested (const ComponentPeer::DragInfo& info, Component* target)
  336. {
  337. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target)->isInterestedInFileDrag (info.files)
  338. : dynamic_cast<TextDragAndDropTarget*> (target)->isInterestedInTextDrag (info.text);
  339. }
  340. static Component* findDragAndDropTarget (Component* c, const ComponentPeer::DragInfo& info, Component* const lastOne)
  341. {
  342. for (; c != nullptr; c = c->getParentComponent())
  343. if (isSuitableTarget (info, c) && (c == lastOne || isInterested (info, c)))
  344. return c;
  345. return nullptr;
  346. }
  347. }
  348. bool ComponentPeer::handleDragMove (const ComponentPeer::DragInfo& info)
  349. {
  350. ModifierKeys::updateCurrentModifiers();
  351. Component* const compUnderMouse = component.getComponentAt (info.position);
  352. Component* const lastTarget = dragAndDropTargetComponent;
  353. Component* newTarget = nullptr;
  354. if (compUnderMouse != lastDragAndDropCompUnderMouse)
  355. {
  356. lastDragAndDropCompUnderMouse = compUnderMouse;
  357. newTarget = DragHelpers::findDragAndDropTarget (compUnderMouse, info, lastTarget);
  358. if (newTarget != lastTarget)
  359. {
  360. if (lastTarget != nullptr)
  361. {
  362. if (DragHelpers::isFileDrag (info))
  363. dynamic_cast<FileDragAndDropTarget*> (lastTarget)->fileDragExit (info.files);
  364. else
  365. dynamic_cast<TextDragAndDropTarget*> (lastTarget)->textDragExit (info.text);
  366. }
  367. dragAndDropTargetComponent = nullptr;
  368. if (DragHelpers::isSuitableTarget (info, newTarget))
  369. {
  370. dragAndDropTargetComponent = newTarget;
  371. auto pos = newTarget->getLocalPoint (&component, info.position);
  372. if (DragHelpers::isFileDrag (info))
  373. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragEnter (info.files, pos.x, pos.y);
  374. else
  375. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragEnter (info.text, pos.x, pos.y);
  376. }
  377. }
  378. }
  379. else
  380. {
  381. newTarget = lastTarget;
  382. }
  383. if (! DragHelpers::isSuitableTarget (info, newTarget))
  384. return false;
  385. auto pos = newTarget->getLocalPoint (&component, info.position);
  386. if (DragHelpers::isFileDrag (info))
  387. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragMove (info.files, pos.x, pos.y);
  388. else
  389. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragMove (info.text, pos.x, pos.y);
  390. return true;
  391. }
  392. bool ComponentPeer::handleDragExit (const ComponentPeer::DragInfo& info)
  393. {
  394. DragInfo info2 (info);
  395. info2.position.setXY (-1, -1);
  396. const bool used = handleDragMove (info2);
  397. jassert (dragAndDropTargetComponent == nullptr);
  398. lastDragAndDropCompUnderMouse = nullptr;
  399. return used;
  400. }
  401. bool ComponentPeer::handleDragDrop (const ComponentPeer::DragInfo& info)
  402. {
  403. handleDragMove (info);
  404. if (WeakReference<Component> targetComp = dragAndDropTargetComponent)
  405. {
  406. dragAndDropTargetComponent = nullptr;
  407. lastDragAndDropCompUnderMouse = nullptr;
  408. if (DragHelpers::isSuitableTarget (info, targetComp))
  409. {
  410. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  411. {
  412. targetComp->internalModalInputAttempt();
  413. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  414. return true;
  415. }
  416. ComponentPeer::DragInfo infoCopy (info);
  417. infoCopy.position = targetComp->getLocalPoint (&component, info.position);
  418. // We'll use an async message to deliver the drop, because if the target decides
  419. // to run a modal loop, it can gum-up the operating system..
  420. MessageManager::callAsync ([=]()
  421. {
  422. if (auto* c = targetComp.get())
  423. {
  424. if (DragHelpers::isFileDrag (info))
  425. dynamic_cast<FileDragAndDropTarget*> (c)->filesDropped (infoCopy.files, infoCopy.position.x, infoCopy.position.y);
  426. else
  427. dynamic_cast<TextDragAndDropTarget*> (c)->textDropped (infoCopy.text, infoCopy.position.x, infoCopy.position.y);
  428. }
  429. });
  430. return true;
  431. }
  432. }
  433. return false;
  434. }
  435. //==============================================================================
  436. void ComponentPeer::handleUserClosingWindow()
  437. {
  438. ModifierKeys::updateCurrentModifiers();
  439. component.userTriedToCloseWindow();
  440. }
  441. bool ComponentPeer::setDocumentEditedStatus (bool)
  442. {
  443. return false;
  444. }
  445. void ComponentPeer::setRepresentedFile (const File&)
  446. {
  447. }
  448. //==============================================================================
  449. int ComponentPeer::getCurrentRenderingEngine() const { return 0; }
  450. void ComponentPeer::setCurrentRenderingEngine (int index) { jassert (index == 0); ignoreUnused (index); }