Audio plugin host https://kx.studio/carla
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.

616 lines
20KB

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