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.

583 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. static uint32 lastUniquePeerID = 1;
  16. //==============================================================================
  17. ComponentPeer::ComponentPeer (Component& comp, int flags)
  18. : component (comp),
  19. styleFlags (flags),
  20. uniqueID (lastUniquePeerID += 2) // increment by 2 so that this can never hit 0
  21. {
  22. Desktop::getInstance().peers.add (this);
  23. }
  24. ComponentPeer::~ComponentPeer()
  25. {
  26. auto& desktop = Desktop::getInstance();
  27. desktop.peers.removeFirstMatchingValue (this);
  28. desktop.triggerFocusCallback();
  29. }
  30. //==============================================================================
  31. int ComponentPeer::getNumPeers() noexcept
  32. {
  33. return Desktop::getInstance().peers.size();
  34. }
  35. ComponentPeer* ComponentPeer::getPeer (const int index) noexcept
  36. {
  37. return Desktop::getInstance().peers [index];
  38. }
  39. ComponentPeer* ComponentPeer::getPeerFor (const Component* const component) noexcept
  40. {
  41. for (auto* peer : Desktop::getInstance().peers)
  42. if (&(peer->getComponent()) == component)
  43. return peer;
  44. return nullptr;
  45. }
  46. bool ComponentPeer::isValidPeer (const ComponentPeer* const peer) noexcept
  47. {
  48. return Desktop::getInstance().peers.contains (const_cast<ComponentPeer*> (peer));
  49. }
  50. void ComponentPeer::updateBounds()
  51. {
  52. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, component.getBoundsInParent()), false);
  53. }
  54. bool ComponentPeer::isKioskMode() const
  55. {
  56. return Desktop::getInstance().getKioskModeComponent() == &component;
  57. }
  58. //==============================================================================
  59. void ComponentPeer::handleMouseEvent (MouseInputSource::InputSourceType type, Point<float> pos, ModifierKeys newMods,
  60. float newPressure, float newOrientation, int64 time, PenDetails pen, int touchIndex)
  61. {
  62. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  63. MouseInputSource (*mouse).handleEvent (*this, pos, time, newMods, newPressure, newOrientation, pen);
  64. }
  65. void ComponentPeer::handleMouseWheel (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, const MouseWheelDetails& wheel, int touchIndex)
  66. {
  67. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  68. MouseInputSource (*mouse).handleWheel (*this, pos, time, wheel);
  69. }
  70. void ComponentPeer::handleMagnifyGesture (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, float scaleFactor, int touchIndex)
  71. {
  72. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  73. MouseInputSource (*mouse).handleMagnifyGesture (*this, pos, time, scaleFactor);
  74. }
  75. //==============================================================================
  76. void ComponentPeer::handlePaint (LowLevelGraphicsContext& contextToPaintTo)
  77. {
  78. Graphics g (contextToPaintTo);
  79. if (component.isTransformed())
  80. g.addTransform (component.getTransform());
  81. auto peerBounds = getBounds();
  82. auto componentBounds = component.getLocalBounds();
  83. if (component.isTransformed())
  84. componentBounds = componentBounds.transformedBy (component.getTransform());
  85. if (peerBounds.getWidth() != componentBounds.getWidth() || peerBounds.getHeight() != componentBounds.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 ((float) peerBounds.getWidth() / (float) componentBounds.getWidth(),
  88. (float) peerBounds.getHeight() / (float) componentBounds.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. return handleKeyPress (KeyPress (keyCode,
  136. ModifierKeys::currentModifiers.withoutMouseButtons(),
  137. textCharacter));
  138. }
  139. bool ComponentPeer::handleKeyPress (const KeyPress& keyInfo)
  140. {
  141. bool keyWasUsed = false;
  142. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  143. {
  144. const WeakReference<Component> deletionChecker (target);
  145. if (auto* keyListeners = target->keyListeners.get())
  146. {
  147. for (int i = keyListeners->size(); --i >= 0;)
  148. {
  149. keyWasUsed = keyListeners->getUnchecked (i)->keyPressed (keyInfo, target);
  150. if (keyWasUsed || deletionChecker == nullptr)
  151. return keyWasUsed;
  152. i = jmin (i, keyListeners->size());
  153. }
  154. }
  155. keyWasUsed = target->keyPressed (keyInfo);
  156. if (keyWasUsed || deletionChecker == nullptr)
  157. break;
  158. }
  159. if (! keyWasUsed && keyInfo.isKeyCode (KeyPress::tabKey))
  160. {
  161. if (auto* currentlyFocused = Component::getCurrentlyFocusedComponent())
  162. {
  163. currentlyFocused->moveKeyboardFocusToSibling (! keyInfo.getModifiers().isShiftDown());
  164. return true;
  165. }
  166. }
  167. return keyWasUsed;
  168. }
  169. bool ComponentPeer::handleKeyUpOrDown (const bool isKeyDown)
  170. {
  171. bool keyWasUsed = false;
  172. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  173. {
  174. const WeakReference<Component> deletionChecker (target);
  175. keyWasUsed = target->keyStateChanged (isKeyDown);
  176. if (keyWasUsed || deletionChecker == nullptr)
  177. break;
  178. if (auto* keyListeners = target->keyListeners.get())
  179. {
  180. for (int i = keyListeners->size(); --i >= 0;)
  181. {
  182. keyWasUsed = keyListeners->getUnchecked (i)->keyStateChanged (isKeyDown, target);
  183. if (keyWasUsed || deletionChecker == nullptr)
  184. return keyWasUsed;
  185. i = jmin (i, keyListeners->size());
  186. }
  187. }
  188. }
  189. return keyWasUsed;
  190. }
  191. void ComponentPeer::handleModifierKeysChange()
  192. {
  193. auto* target = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  194. if (target == nullptr)
  195. target = Component::getCurrentlyFocusedComponent();
  196. if (target == nullptr)
  197. target = &component;
  198. target->internalModifierKeysChanged();
  199. }
  200. TextInputTarget* ComponentPeer::findCurrentTextInputTarget()
  201. {
  202. auto* c = Component::getCurrentlyFocusedComponent();
  203. if (c == &component || component.isParentOf (c))
  204. if (auto* ti = dynamic_cast<TextInputTarget*> (c))
  205. if (ti->isTextInputActive())
  206. return ti;
  207. return nullptr;
  208. }
  209. void ComponentPeer::dismissPendingTextInput() {}
  210. //==============================================================================
  211. void ComponentPeer::handleBroughtToFront()
  212. {
  213. component.internalBroughtToFront();
  214. }
  215. void ComponentPeer::setConstrainer (ComponentBoundsConstrainer* const newConstrainer) noexcept
  216. {
  217. constrainer = newConstrainer;
  218. }
  219. void ComponentPeer::handleMovedOrResized()
  220. {
  221. const bool nowMinimised = isMinimised();
  222. if (component.flags.hasHeavyweightPeerFlag && ! nowMinimised)
  223. {
  224. const WeakReference<Component> deletionChecker (&component);
  225. auto newBounds = Component::ComponentHelpers::rawPeerPositionToLocal (component, getBounds());
  226. auto oldBounds = component.getBounds();
  227. const bool wasMoved = (oldBounds.getPosition() != newBounds.getPosition());
  228. const bool wasResized = (oldBounds.getWidth() != newBounds.getWidth() || oldBounds.getHeight() != newBounds.getHeight());
  229. if (wasMoved || wasResized)
  230. {
  231. component.boundsRelativeToParent = newBounds;
  232. if (wasResized)
  233. component.repaint();
  234. component.sendMovedResizedMessages (wasMoved, wasResized);
  235. if (deletionChecker == nullptr)
  236. return;
  237. }
  238. }
  239. if (isWindowMinimised != nowMinimised)
  240. {
  241. isWindowMinimised = nowMinimised;
  242. component.minimisationStateChanged (nowMinimised);
  243. component.sendVisibilityChangeMessage();
  244. }
  245. const auto windowInSpecialState = isFullScreen() || isKioskMode() || nowMinimised;
  246. if (! windowInSpecialState)
  247. lastNonFullscreenBounds = component.getBounds();
  248. }
  249. void ComponentPeer::handleFocusGain()
  250. {
  251. if (component.isParentOf (lastFocusedComponent)
  252. && lastFocusedComponent->isShowing()
  253. && lastFocusedComponent->getWantsKeyboardFocus())
  254. {
  255. Component::currentlyFocusedComponent = lastFocusedComponent;
  256. Desktop::getInstance().triggerFocusCallback();
  257. lastFocusedComponent->internalKeyboardFocusGain (Component::focusChangedDirectly);
  258. }
  259. else
  260. {
  261. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  262. component.grabKeyboardFocus();
  263. else
  264. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  265. }
  266. }
  267. void ComponentPeer::handleFocusLoss()
  268. {
  269. if (component.hasKeyboardFocus (true))
  270. {
  271. lastFocusedComponent = Component::currentlyFocusedComponent;
  272. if (lastFocusedComponent != nullptr)
  273. {
  274. Component::currentlyFocusedComponent = nullptr;
  275. Desktop::getInstance().triggerFocusCallback();
  276. lastFocusedComponent->internalKeyboardFocusLoss (Component::focusChangedByMouseClick);
  277. }
  278. }
  279. }
  280. Component* ComponentPeer::getLastFocusedSubcomponent() const noexcept
  281. {
  282. return (component.isParentOf (lastFocusedComponent) && lastFocusedComponent->isShowing())
  283. ? static_cast<Component*> (lastFocusedComponent)
  284. : &component;
  285. }
  286. void ComponentPeer::handleScreenSizeChange()
  287. {
  288. component.parentSizeChanged();
  289. handleMovedOrResized();
  290. }
  291. void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept
  292. {
  293. lastNonFullscreenBounds = newBounds;
  294. }
  295. const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const noexcept
  296. {
  297. return lastNonFullscreenBounds;
  298. }
  299. Point<int> ComponentPeer::localToGlobal (Point<int> p) { return localToGlobal (p.toFloat()).roundToInt(); }
  300. Point<int> ComponentPeer::globalToLocal (Point<int> p) { return globalToLocal (p.toFloat()).roundToInt(); }
  301. Rectangle<int> ComponentPeer::localToGlobal (const Rectangle<int>& relativePosition)
  302. {
  303. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  304. }
  305. Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPosition)
  306. {
  307. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  308. }
  309. Rectangle<float> ComponentPeer::localToGlobal (const Rectangle<float>& relativePosition)
  310. {
  311. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  312. }
  313. Rectangle<float> ComponentPeer::globalToLocal (const Rectangle<float>& screenPosition)
  314. {
  315. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  316. }
  317. Rectangle<int> ComponentPeer::getAreaCoveredBy (const Component& subComponent) const
  318. {
  319. return ScalingHelpers::scaledScreenPosToUnscaled
  320. (component, component.getLocalArea (&subComponent, subComponent.getLocalBounds()));
  321. }
  322. //==============================================================================
  323. namespace DragHelpers
  324. {
  325. static bool isFileDrag (const ComponentPeer::DragInfo& info)
  326. {
  327. return ! info.files.isEmpty();
  328. }
  329. static bool isSuitableTarget (const ComponentPeer::DragInfo& info, Component* target)
  330. {
  331. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target) != nullptr
  332. : dynamic_cast<TextDragAndDropTarget*> (target) != nullptr;
  333. }
  334. static bool isInterested (const ComponentPeer::DragInfo& info, Component* target)
  335. {
  336. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target)->isInterestedInFileDrag (info.files)
  337. : dynamic_cast<TextDragAndDropTarget*> (target)->isInterestedInTextDrag (info.text);
  338. }
  339. static Component* findDragAndDropTarget (Component* c, const ComponentPeer::DragInfo& info, Component* lastOne)
  340. {
  341. for (; c != nullptr; c = c->getParentComponent())
  342. if (isSuitableTarget (info, c) && (c == lastOne || isInterested (info, c)))
  343. return c;
  344. return nullptr;
  345. }
  346. }
  347. bool ComponentPeer::handleDragMove (const ComponentPeer::DragInfo& info)
  348. {
  349. auto* compUnderMouse = component.getComponentAt (info.position);
  350. auto* lastTarget = dragAndDropTargetComponent.get();
  351. Component* newTarget = nullptr;
  352. if (compUnderMouse != lastDragAndDropCompUnderMouse)
  353. {
  354. lastDragAndDropCompUnderMouse = compUnderMouse;
  355. newTarget = DragHelpers::findDragAndDropTarget (compUnderMouse, info, lastTarget);
  356. if (newTarget != lastTarget)
  357. {
  358. if (lastTarget != nullptr)
  359. {
  360. if (DragHelpers::isFileDrag (info))
  361. dynamic_cast<FileDragAndDropTarget*> (lastTarget)->fileDragExit (info.files);
  362. else
  363. dynamic_cast<TextDragAndDropTarget*> (lastTarget)->textDragExit (info.text);
  364. }
  365. dragAndDropTargetComponent = nullptr;
  366. if (DragHelpers::isSuitableTarget (info, newTarget))
  367. {
  368. dragAndDropTargetComponent = newTarget;
  369. auto pos = newTarget->getLocalPoint (&component, info.position);
  370. if (DragHelpers::isFileDrag (info))
  371. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragEnter (info.files, pos.x, pos.y);
  372. else
  373. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragEnter (info.text, pos.x, pos.y);
  374. }
  375. }
  376. }
  377. else
  378. {
  379. newTarget = lastTarget;
  380. }
  381. if (! DragHelpers::isSuitableTarget (info, newTarget))
  382. return false;
  383. auto pos = newTarget->getLocalPoint (&component, info.position);
  384. if (DragHelpers::isFileDrag (info))
  385. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragMove (info.files, pos.x, pos.y);
  386. else
  387. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragMove (info.text, pos.x, pos.y);
  388. return true;
  389. }
  390. bool ComponentPeer::handleDragExit (const ComponentPeer::DragInfo& info)
  391. {
  392. DragInfo info2 (info);
  393. info2.position.setXY (-1, -1);
  394. const bool used = handleDragMove (info2);
  395. jassert (dragAndDropTargetComponent == nullptr);
  396. lastDragAndDropCompUnderMouse = nullptr;
  397. return used;
  398. }
  399. bool ComponentPeer::handleDragDrop (const ComponentPeer::DragInfo& info)
  400. {
  401. handleDragMove (info);
  402. if (WeakReference<Component> targetComp = dragAndDropTargetComponent)
  403. {
  404. dragAndDropTargetComponent = nullptr;
  405. lastDragAndDropCompUnderMouse = nullptr;
  406. if (DragHelpers::isSuitableTarget (info, targetComp))
  407. {
  408. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  409. {
  410. targetComp->internalModalInputAttempt();
  411. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  412. return true;
  413. }
  414. ComponentPeer::DragInfo infoCopy (info);
  415. infoCopy.position = targetComp->getLocalPoint (&component, info.position);
  416. // We'll use an async message to deliver the drop, because if the target decides
  417. // to run a modal loop, it can gum-up the operating system..
  418. MessageManager::callAsync ([=]
  419. {
  420. if (auto* c = targetComp.get())
  421. {
  422. if (DragHelpers::isFileDrag (info))
  423. dynamic_cast<FileDragAndDropTarget*> (c)->filesDropped (infoCopy.files, infoCopy.position.x, infoCopy.position.y);
  424. else
  425. dynamic_cast<TextDragAndDropTarget*> (c)->textDropped (infoCopy.text, infoCopy.position.x, infoCopy.position.y);
  426. }
  427. });
  428. return true;
  429. }
  430. }
  431. return false;
  432. }
  433. //==============================================================================
  434. void ComponentPeer::handleUserClosingWindow()
  435. {
  436. component.userTriedToCloseWindow();
  437. }
  438. bool ComponentPeer::setDocumentEditedStatus (bool)
  439. {
  440. return false;
  441. }
  442. void ComponentPeer::setRepresentedFile (const File&)
  443. {
  444. }
  445. //==============================================================================
  446. int ComponentPeer::getCurrentRenderingEngine() const { return 0; }
  447. void ComponentPeer::setCurrentRenderingEngine (int index) { jassert (index == 0); ignoreUnused (index); }
  448. //==============================================================================
  449. std::function<ModifierKeys()> ComponentPeer::getNativeRealtimeModifiers = nullptr;
  450. ModifierKeys ComponentPeer::getCurrentModifiersRealtime() noexcept
  451. {
  452. if (getNativeRealtimeModifiers != nullptr)
  453. return getNativeRealtimeModifiers();
  454. return ModifierKeys::currentModifiers;
  455. }
  456. //==============================================================================
  457. void ComponentPeer::forceDisplayUpdate()
  458. {
  459. Desktop::getInstance().displays->refresh();
  460. }
  461. } // namespace juce