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.

588 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. }
  164. if (! keyWasUsed && keyInfo.isKeyCode (KeyPress::tabKey))
  165. {
  166. if (auto* currentlyFocused = Component::getCurrentlyFocusedComponent())
  167. {
  168. currentlyFocused->moveKeyboardFocusToSibling (! keyInfo.getModifiers().isShiftDown());
  169. return true;
  170. }
  171. }
  172. return keyWasUsed;
  173. }
  174. bool ComponentPeer::handleKeyUpOrDown (const bool isKeyDown)
  175. {
  176. bool keyWasUsed = false;
  177. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  178. {
  179. const WeakReference<Component> deletionChecker (target);
  180. keyWasUsed = target->keyStateChanged (isKeyDown);
  181. if (keyWasUsed || deletionChecker == nullptr)
  182. break;
  183. if (auto* keyListeners = target->keyListeners.get())
  184. {
  185. for (int i = keyListeners->size(); --i >= 0;)
  186. {
  187. keyWasUsed = keyListeners->getUnchecked (i)->keyStateChanged (isKeyDown, target);
  188. if (keyWasUsed || deletionChecker == nullptr)
  189. return keyWasUsed;
  190. i = jmin (i, keyListeners->size());
  191. }
  192. }
  193. }
  194. return keyWasUsed;
  195. }
  196. void ComponentPeer::handleModifierKeysChange()
  197. {
  198. auto* target = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  199. if (target == nullptr)
  200. target = Component::getCurrentlyFocusedComponent();
  201. if (target == nullptr)
  202. target = &component;
  203. target->internalModifierKeysChanged();
  204. }
  205. TextInputTarget* ComponentPeer::findCurrentTextInputTarget()
  206. {
  207. auto* c = Component::getCurrentlyFocusedComponent();
  208. if (c == &component || component.isParentOf (c))
  209. if (auto* ti = dynamic_cast<TextInputTarget*> (c))
  210. if (ti->isTextInputActive())
  211. return ti;
  212. return nullptr;
  213. }
  214. void ComponentPeer::dismissPendingTextInput() {}
  215. //==============================================================================
  216. void ComponentPeer::handleBroughtToFront()
  217. {
  218. component.internalBroughtToFront();
  219. }
  220. void ComponentPeer::setConstrainer (ComponentBoundsConstrainer* const newConstrainer) noexcept
  221. {
  222. constrainer = newConstrainer;
  223. }
  224. void ComponentPeer::handleMovedOrResized()
  225. {
  226. const bool nowMinimised = isMinimised();
  227. if (component.flags.hasHeavyweightPeerFlag && ! nowMinimised)
  228. {
  229. const WeakReference<Component> deletionChecker (&component);
  230. auto newBounds = Component::ComponentHelpers::rawPeerPositionToLocal (component, getBounds());
  231. auto oldBounds = component.getBounds();
  232. const bool wasMoved = (oldBounds.getPosition() != newBounds.getPosition());
  233. const bool wasResized = (oldBounds.getWidth() != newBounds.getWidth() || oldBounds.getHeight() != newBounds.getHeight());
  234. if (wasMoved || wasResized)
  235. {
  236. component.boundsRelativeToParent = newBounds;
  237. if (wasResized)
  238. component.repaint();
  239. component.sendMovedResizedMessages (wasMoved, wasResized);
  240. if (deletionChecker == nullptr)
  241. return;
  242. }
  243. }
  244. if (isWindowMinimised != nowMinimised)
  245. {
  246. isWindowMinimised = nowMinimised;
  247. component.minimisationStateChanged (nowMinimised);
  248. component.sendVisibilityChangeMessage();
  249. }
  250. if (! isFullScreen())
  251. lastNonFullscreenBounds = component.getBounds();
  252. }
  253. void ComponentPeer::handleFocusGain()
  254. {
  255. if (component.isParentOf (lastFocusedComponent)
  256. && lastFocusedComponent->isShowing()
  257. && lastFocusedComponent->getWantsKeyboardFocus())
  258. {
  259. Component::currentlyFocusedComponent = lastFocusedComponent;
  260. Desktop::getInstance().triggerFocusCallback();
  261. lastFocusedComponent->internalKeyboardFocusGain (Component::focusChangedDirectly);
  262. }
  263. else
  264. {
  265. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  266. component.grabKeyboardFocus();
  267. else
  268. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  269. }
  270. }
  271. void ComponentPeer::handleFocusLoss()
  272. {
  273. if (component.hasKeyboardFocus (true))
  274. {
  275. lastFocusedComponent = Component::currentlyFocusedComponent;
  276. if (lastFocusedComponent != nullptr)
  277. {
  278. Component::currentlyFocusedComponent = nullptr;
  279. Desktop::getInstance().triggerFocusCallback();
  280. lastFocusedComponent->internalKeyboardFocusLoss (Component::focusChangedByMouseClick);
  281. }
  282. }
  283. }
  284. Component* ComponentPeer::getLastFocusedSubcomponent() const noexcept
  285. {
  286. return (component.isParentOf (lastFocusedComponent) && lastFocusedComponent->isShowing())
  287. ? static_cast<Component*> (lastFocusedComponent)
  288. : &component;
  289. }
  290. void ComponentPeer::handleScreenSizeChange()
  291. {
  292. component.parentSizeChanged();
  293. handleMovedOrResized();
  294. }
  295. void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept
  296. {
  297. lastNonFullscreenBounds = newBounds;
  298. }
  299. const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const noexcept
  300. {
  301. return lastNonFullscreenBounds;
  302. }
  303. Point<int> ComponentPeer::localToGlobal (Point<int> p) { return localToGlobal (p.toFloat()).roundToInt(); }
  304. Point<int> ComponentPeer::globalToLocal (Point<int> p) { return globalToLocal (p.toFloat()).roundToInt(); }
  305. Rectangle<int> ComponentPeer::localToGlobal (const Rectangle<int>& relativePosition)
  306. {
  307. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  308. }
  309. Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPosition)
  310. {
  311. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  312. }
  313. Rectangle<float> ComponentPeer::localToGlobal (const Rectangle<float>& relativePosition)
  314. {
  315. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  316. }
  317. Rectangle<float> ComponentPeer::globalToLocal (const Rectangle<float>& screenPosition)
  318. {
  319. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  320. }
  321. Rectangle<int> ComponentPeer::getAreaCoveredBy (const Component& subComponent) const
  322. {
  323. return ScalingHelpers::scaledScreenPosToUnscaled
  324. (component, component.getLocalArea (&subComponent, subComponent.getLocalBounds()));
  325. }
  326. //==============================================================================
  327. namespace DragHelpers
  328. {
  329. static bool isFileDrag (const ComponentPeer::DragInfo& info)
  330. {
  331. return ! info.files.isEmpty();
  332. }
  333. static bool isSuitableTarget (const ComponentPeer::DragInfo& info, Component* target)
  334. {
  335. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target) != nullptr
  336. : dynamic_cast<TextDragAndDropTarget*> (target) != nullptr;
  337. }
  338. static bool isInterested (const ComponentPeer::DragInfo& info, Component* target)
  339. {
  340. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target)->isInterestedInFileDrag (info.files)
  341. : dynamic_cast<TextDragAndDropTarget*> (target)->isInterestedInTextDrag (info.text);
  342. }
  343. static Component* findDragAndDropTarget (Component* c, const ComponentPeer::DragInfo& info, Component* lastOne)
  344. {
  345. for (; c != nullptr; c = c->getParentComponent())
  346. if (isSuitableTarget (info, c) && (c == lastOne || isInterested (info, c)))
  347. return c;
  348. return nullptr;
  349. }
  350. }
  351. bool ComponentPeer::handleDragMove (const ComponentPeer::DragInfo& info)
  352. {
  353. auto* compUnderMouse = component.getComponentAt (info.position);
  354. auto* lastTarget = dragAndDropTargetComponent.get();
  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. component.userTriedToCloseWindow();
  441. }
  442. bool ComponentPeer::setDocumentEditedStatus (bool)
  443. {
  444. return false;
  445. }
  446. void ComponentPeer::setRepresentedFile (const File&)
  447. {
  448. }
  449. //==============================================================================
  450. int ComponentPeer::getCurrentRenderingEngine() const { return 0; }
  451. void ComponentPeer::setCurrentRenderingEngine (int index) { jassert (index == 0); ignoreUnused (index); }
  452. //==============================================================================
  453. std::function<ModifierKeys()> ComponentPeer::getNativeRealtimeModifiers = nullptr;
  454. ModifierKeys ComponentPeer::getCurrentModifiersRealtime() noexcept
  455. {
  456. if (getNativeRealtimeModifiers != nullptr)
  457. return getNativeRealtimeModifiers();
  458. return ModifierKeys::currentModifiers;
  459. }
  460. //==============================================================================
  461. void ComponentPeer::forceDisplayUpdate()
  462. {
  463. Desktop::getInstance().displays->refresh();
  464. }
  465. } // namespace juce