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.

577 lines
19KB

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