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.

589 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. Desktop::getInstance().peers.add (this);
  28. }
  29. ComponentPeer::~ComponentPeer()
  30. {
  31. auto& desktop = Desktop::getInstance();
  32. desktop.peers.removeFirstMatchingValue (this);
  33. desktop.triggerFocusCallback();
  34. }
  35. //==============================================================================
  36. int ComponentPeer::getNumPeers() noexcept
  37. {
  38. return Desktop::getInstance().peers.size();
  39. }
  40. ComponentPeer* ComponentPeer::getPeer (const int index) noexcept
  41. {
  42. return Desktop::getInstance().peers [index];
  43. }
  44. ComponentPeer* ComponentPeer::getPeerFor (const Component* const component) noexcept
  45. {
  46. for (auto* peer : Desktop::getInstance().peers)
  47. if (&(peer->getComponent()) == component)
  48. return peer;
  49. return nullptr;
  50. }
  51. bool ComponentPeer::isValidPeer (const ComponentPeer* const peer) noexcept
  52. {
  53. return Desktop::getInstance().peers.contains (const_cast<ComponentPeer*> (peer));
  54. }
  55. void ComponentPeer::updateBounds()
  56. {
  57. setBounds (ScalingHelpers::scaledScreenPosToUnscaled (component, component.getBoundsInParent()), false);
  58. }
  59. bool ComponentPeer::isKioskMode() const
  60. {
  61. return Desktop::getInstance().getKioskModeComponent() == &component;
  62. }
  63. //==============================================================================
  64. void ComponentPeer::handleMouseEvent (MouseInputSource::InputSourceType type, Point<float> pos, ModifierKeys newMods,
  65. float newPressure, float newOrientation, int64 time, PenDetails pen, int touchIndex)
  66. {
  67. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  68. MouseInputSource (*mouse).handleEvent (*this, pos, time, newMods, newPressure, newOrientation, pen);
  69. }
  70. void ComponentPeer::handleMouseWheel (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, const MouseWheelDetails& wheel, int touchIndex)
  71. {
  72. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  73. MouseInputSource (*mouse).handleWheel (*this, pos, time, wheel);
  74. }
  75. void ComponentPeer::handleMagnifyGesture (MouseInputSource::InputSourceType type, Point<float> pos, int64 time, float scaleFactor, int touchIndex)
  76. {
  77. if (auto* mouse = Desktop::getInstance().mouseSources->getOrCreateMouseInputSource (type, touchIndex))
  78. MouseInputSource (*mouse).handleMagnifyGesture (*this, pos, time, scaleFactor);
  79. }
  80. //==============================================================================
  81. void ComponentPeer::handlePaint (LowLevelGraphicsContext& contextToPaintTo)
  82. {
  83. Graphics g (contextToPaintTo);
  84. if (component.isTransformed())
  85. g.addTransform (component.getTransform());
  86. auto peerBounds = getBounds();
  87. auto componentBounds = component.getLocalBounds();
  88. if (component.isTransformed())
  89. componentBounds = componentBounds.transformedBy (component.getTransform());
  90. if (peerBounds.getWidth() != componentBounds.getWidth() || peerBounds.getHeight() != componentBounds.getHeight())
  91. // Tweak the scaling so that the component's integer size exactly aligns with the peer's scaled size
  92. g.addTransform (AffineTransform::scale ((float) peerBounds.getWidth() / (float) componentBounds.getWidth(),
  93. (float) peerBounds.getHeight() / (float) componentBounds.getHeight()));
  94. #if JUCE_ENABLE_REPAINT_DEBUGGING
  95. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  96. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  97. #endif
  98. {
  99. g.saveState();
  100. }
  101. #endif
  102. JUCE_TRY
  103. {
  104. component.paintEntireComponent (g, true);
  105. }
  106. JUCE_CATCH_EXCEPTION
  107. #if JUCE_ENABLE_REPAINT_DEBUGGING
  108. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  109. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  110. #endif
  111. {
  112. // enabling this code will fill all areas that get repainted with a colour overlay, to show
  113. // clearly when things are being repainted.
  114. g.restoreState();
  115. static Random rng;
  116. g.fillAll (Colour ((uint8) rng.nextInt (255),
  117. (uint8) rng.nextInt (255),
  118. (uint8) rng.nextInt (255),
  119. (uint8) 0x50));
  120. }
  121. #endif
  122. /** If this fails, it's probably be because your CPU floating-point precision mode has
  123. been set to low.. This setting is sometimes changed by things like Direct3D, and can
  124. mess up a lot of the calculations that the library needs to do.
  125. */
  126. jassert (roundToInt (10.1f) == 10);
  127. }
  128. Component* ComponentPeer::getTargetForKeyPress()
  129. {
  130. auto* c = Component::getCurrentlyFocusedComponent();
  131. if (c == nullptr)
  132. c = &component;
  133. if (c->isCurrentlyBlockedByAnotherModalComponent())
  134. if (auto* currentModalComp = Component::getCurrentlyModalComponent())
  135. c = currentModalComp;
  136. return c;
  137. }
  138. bool ComponentPeer::handleKeyPress (const int keyCode, const juce_wchar textCharacter)
  139. {
  140. return handleKeyPress (KeyPress (keyCode,
  141. ModifierKeys::currentModifiers.withoutMouseButtons(),
  142. textCharacter));
  143. }
  144. bool ComponentPeer::handleKeyPress (const KeyPress& keyInfo)
  145. {
  146. bool keyWasUsed = false;
  147. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  148. {
  149. const WeakReference<Component> deletionChecker (target);
  150. if (auto* keyListeners = target->keyListeners.get())
  151. {
  152. for (int i = keyListeners->size(); --i >= 0;)
  153. {
  154. keyWasUsed = keyListeners->getUnchecked(i)->keyPressed (keyInfo, target);
  155. if (keyWasUsed || deletionChecker == nullptr)
  156. return keyWasUsed;
  157. i = jmin (i, keyListeners->size());
  158. }
  159. }
  160. keyWasUsed = target->keyPressed (keyInfo);
  161. if (keyWasUsed || deletionChecker == nullptr)
  162. break;
  163. if (auto* currentlyFocused = Component::getCurrentlyFocusedComponent())
  164. {
  165. const bool isTab = (keyInfo == KeyPress::tabKey);
  166. const bool isShiftTab = (keyInfo == KeyPress (KeyPress::tabKey, ModifierKeys::shiftModifier, 0));
  167. if (isTab || isShiftTab)
  168. {
  169. currentlyFocused->moveKeyboardFocusToSibling (isTab);
  170. keyWasUsed = (currentlyFocused != Component::getCurrentlyFocusedComponent());
  171. if (keyWasUsed || deletionChecker == nullptr)
  172. break;
  173. }
  174. }
  175. }
  176. return keyWasUsed;
  177. }
  178. bool ComponentPeer::handleKeyUpOrDown (const bool isKeyDown)
  179. {
  180. bool keyWasUsed = false;
  181. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  182. {
  183. const WeakReference<Component> deletionChecker (target);
  184. keyWasUsed = target->keyStateChanged (isKeyDown);
  185. if (keyWasUsed || deletionChecker == nullptr)
  186. break;
  187. if (auto* keyListeners = target->keyListeners.get())
  188. {
  189. for (int i = keyListeners->size(); --i >= 0;)
  190. {
  191. keyWasUsed = keyListeners->getUnchecked(i)->keyStateChanged (isKeyDown, target);
  192. if (keyWasUsed || deletionChecker == nullptr)
  193. return keyWasUsed;
  194. i = jmin (i, keyListeners->size());
  195. }
  196. }
  197. }
  198. return keyWasUsed;
  199. }
  200. void ComponentPeer::handleModifierKeysChange()
  201. {
  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. component.internalBroughtToFront();
  223. }
  224. void ComponentPeer::setConstrainer (ComponentBoundsConstrainer* const newConstrainer) noexcept
  225. {
  226. constrainer = newConstrainer;
  227. }
  228. void ComponentPeer::handleMovedOrResized()
  229. {
  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. if (component.isParentOf (lastFocusedComponent)
  260. && lastFocusedComponent->isShowing()
  261. && lastFocusedComponent->getWantsKeyboardFocus())
  262. {
  263. Component::currentlyFocusedComponent = lastFocusedComponent;
  264. Desktop::getInstance().triggerFocusCallback();
  265. lastFocusedComponent->internalFocusGain (Component::focusChangedDirectly);
  266. }
  267. else
  268. {
  269. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  270. component.grabKeyboardFocus();
  271. else
  272. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  273. }
  274. }
  275. void ComponentPeer::handleFocusLoss()
  276. {
  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. component.parentSizeChanged();
  297. handleMovedOrResized();
  298. }
  299. void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept
  300. {
  301. lastNonFullscreenBounds = newBounds;
  302. }
  303. const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const noexcept
  304. {
  305. return lastNonFullscreenBounds;
  306. }
  307. Point<int> ComponentPeer::localToGlobal (Point<int> p) { return localToGlobal (p.toFloat()).roundToInt(); }
  308. Point<int> ComponentPeer::globalToLocal (Point<int> p) { return globalToLocal (p.toFloat()).roundToInt(); }
  309. Rectangle<int> ComponentPeer::localToGlobal (const Rectangle<int>& relativePosition)
  310. {
  311. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  312. }
  313. Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPosition)
  314. {
  315. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  316. }
  317. Rectangle<float> ComponentPeer::localToGlobal (const Rectangle<float>& relativePosition)
  318. {
  319. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  320. }
  321. Rectangle<float> ComponentPeer::globalToLocal (const Rectangle<float>& screenPosition)
  322. {
  323. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  324. }
  325. Rectangle<int> ComponentPeer::getAreaCoveredBy (Component& subComponent) const
  326. {
  327. return ScalingHelpers::scaledScreenPosToUnscaled
  328. (component, component.getLocalArea (&subComponent, subComponent.getLocalBounds()));
  329. }
  330. //==============================================================================
  331. namespace DragHelpers
  332. {
  333. static bool isFileDrag (const ComponentPeer::DragInfo& info)
  334. {
  335. return ! info.files.isEmpty();
  336. }
  337. static bool isSuitableTarget (const ComponentPeer::DragInfo& info, Component* target)
  338. {
  339. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target) != nullptr
  340. : dynamic_cast<TextDragAndDropTarget*> (target) != nullptr;
  341. }
  342. static bool isInterested (const ComponentPeer::DragInfo& info, Component* target)
  343. {
  344. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target)->isInterestedInFileDrag (info.files)
  345. : dynamic_cast<TextDragAndDropTarget*> (target)->isInterestedInTextDrag (info.text);
  346. }
  347. static Component* findDragAndDropTarget (Component* c, const ComponentPeer::DragInfo& info, Component* lastOne)
  348. {
  349. for (; c != nullptr; c = c->getParentComponent())
  350. if (isSuitableTarget (info, c) && (c == lastOne || isInterested (info, c)))
  351. return c;
  352. return nullptr;
  353. }
  354. }
  355. bool ComponentPeer::handleDragMove (const ComponentPeer::DragInfo& info)
  356. {
  357. auto* compUnderMouse = component.getComponentAt (info.position);
  358. auto* lastTarget = dragAndDropTargetComponent.get();
  359. Component* newTarget = nullptr;
  360. if (compUnderMouse != lastDragAndDropCompUnderMouse)
  361. {
  362. lastDragAndDropCompUnderMouse = compUnderMouse;
  363. newTarget = DragHelpers::findDragAndDropTarget (compUnderMouse, info, lastTarget);
  364. if (newTarget != lastTarget)
  365. {
  366. if (lastTarget != nullptr)
  367. {
  368. if (DragHelpers::isFileDrag (info))
  369. dynamic_cast<FileDragAndDropTarget*> (lastTarget)->fileDragExit (info.files);
  370. else
  371. dynamic_cast<TextDragAndDropTarget*> (lastTarget)->textDragExit (info.text);
  372. }
  373. dragAndDropTargetComponent = nullptr;
  374. if (DragHelpers::isSuitableTarget (info, newTarget))
  375. {
  376. dragAndDropTargetComponent = newTarget;
  377. auto pos = newTarget->getLocalPoint (&component, info.position);
  378. if (DragHelpers::isFileDrag (info))
  379. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragEnter (info.files, pos.x, pos.y);
  380. else
  381. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragEnter (info.text, pos.x, pos.y);
  382. }
  383. }
  384. }
  385. else
  386. {
  387. newTarget = lastTarget;
  388. }
  389. if (! DragHelpers::isSuitableTarget (info, newTarget))
  390. return false;
  391. auto pos = newTarget->getLocalPoint (&component, info.position);
  392. if (DragHelpers::isFileDrag (info))
  393. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragMove (info.files, pos.x, pos.y);
  394. else
  395. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragMove (info.text, pos.x, pos.y);
  396. return true;
  397. }
  398. bool ComponentPeer::handleDragExit (const ComponentPeer::DragInfo& info)
  399. {
  400. DragInfo info2 (info);
  401. info2.position.setXY (-1, -1);
  402. const bool used = handleDragMove (info2);
  403. jassert (dragAndDropTargetComponent == nullptr);
  404. lastDragAndDropCompUnderMouse = nullptr;
  405. return used;
  406. }
  407. bool ComponentPeer::handleDragDrop (const ComponentPeer::DragInfo& info)
  408. {
  409. handleDragMove (info);
  410. if (WeakReference<Component> targetComp = dragAndDropTargetComponent)
  411. {
  412. dragAndDropTargetComponent = nullptr;
  413. lastDragAndDropCompUnderMouse = nullptr;
  414. if (DragHelpers::isSuitableTarget (info, targetComp))
  415. {
  416. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  417. {
  418. targetComp->internalModalInputAttempt();
  419. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  420. return true;
  421. }
  422. ComponentPeer::DragInfo infoCopy (info);
  423. infoCopy.position = targetComp->getLocalPoint (&component, info.position);
  424. // We'll use an async message to deliver the drop, because if the target decides
  425. // to run a modal loop, it can gum-up the operating system..
  426. MessageManager::callAsync ([=]
  427. {
  428. if (auto* c = targetComp.get())
  429. {
  430. if (DragHelpers::isFileDrag (info))
  431. dynamic_cast<FileDragAndDropTarget*> (c)->filesDropped (infoCopy.files, infoCopy.position.x, infoCopy.position.y);
  432. else
  433. dynamic_cast<TextDragAndDropTarget*> (c)->textDropped (infoCopy.text, infoCopy.position.x, infoCopy.position.y);
  434. }
  435. });
  436. return true;
  437. }
  438. }
  439. return false;
  440. }
  441. //==============================================================================
  442. void ComponentPeer::handleUserClosingWindow()
  443. {
  444. component.userTriedToCloseWindow();
  445. }
  446. bool ComponentPeer::setDocumentEditedStatus (bool)
  447. {
  448. return false;
  449. }
  450. void ComponentPeer::setRepresentedFile (const File&)
  451. {
  452. }
  453. //==============================================================================
  454. int ComponentPeer::getCurrentRenderingEngine() const { return 0; }
  455. void ComponentPeer::setCurrentRenderingEngine (int index) { jassert (index == 0); ignoreUnused (index); }
  456. //==============================================================================
  457. std::function<ModifierKeys()> ComponentPeer::getNativeRealtimeModifiers = nullptr;
  458. ModifierKeys ComponentPeer::getCurrentModifiersRealtime() noexcept
  459. {
  460. if (getNativeRealtimeModifiers != nullptr)
  461. return getNativeRealtimeModifiers();
  462. return ModifierKeys::currentModifiers;
  463. }
  464. } // namespace juce