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.

595 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::closeInputMethodContext() {}
  215. void ComponentPeer::dismissPendingTextInput()
  216. {
  217. closeInputMethodContext();
  218. }
  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. const auto windowInSpecialState = isFullScreen() || isKioskMode() || nowMinimised;
  255. if (! windowInSpecialState)
  256. lastNonFullscreenBounds = component.getBounds();
  257. }
  258. void ComponentPeer::handleFocusGain()
  259. {
  260. if (component.isParentOf (lastFocusedComponent)
  261. && lastFocusedComponent->isShowing()
  262. && lastFocusedComponent->getWantsKeyboardFocus())
  263. {
  264. Component::currentlyFocusedComponent = lastFocusedComponent;
  265. Desktop::getInstance().triggerFocusCallback();
  266. lastFocusedComponent->internalKeyboardFocusGain (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. if (component.hasKeyboardFocus (true))
  279. {
  280. lastFocusedComponent = Component::currentlyFocusedComponent;
  281. if (lastFocusedComponent != nullptr)
  282. {
  283. Component::currentlyFocusedComponent = nullptr;
  284. Desktop::getInstance().triggerFocusCallback();
  285. lastFocusedComponent->internalKeyboardFocusLoss (Component::focusChangedByMouseClick);
  286. }
  287. }
  288. }
  289. Component* ComponentPeer::getLastFocusedSubcomponent() const noexcept
  290. {
  291. return (component.isParentOf (lastFocusedComponent) && lastFocusedComponent->isShowing())
  292. ? static_cast<Component*> (lastFocusedComponent)
  293. : &component;
  294. }
  295. void ComponentPeer::handleScreenSizeChange()
  296. {
  297. component.parentSizeChanged();
  298. handleMovedOrResized();
  299. }
  300. void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept
  301. {
  302. lastNonFullscreenBounds = newBounds;
  303. }
  304. const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const noexcept
  305. {
  306. return lastNonFullscreenBounds;
  307. }
  308. Point<int> ComponentPeer::localToGlobal (Point<int> p) { return localToGlobal (p.toFloat()).roundToInt(); }
  309. Point<int> ComponentPeer::globalToLocal (Point<int> p) { return globalToLocal (p.toFloat()).roundToInt(); }
  310. Rectangle<int> ComponentPeer::localToGlobal (const Rectangle<int>& relativePosition)
  311. {
  312. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  313. }
  314. Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPosition)
  315. {
  316. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  317. }
  318. Rectangle<float> ComponentPeer::localToGlobal (const Rectangle<float>& relativePosition)
  319. {
  320. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  321. }
  322. Rectangle<float> ComponentPeer::globalToLocal (const Rectangle<float>& screenPosition)
  323. {
  324. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  325. }
  326. Rectangle<int> ComponentPeer::getAreaCoveredBy (const Component& subComponent) const
  327. {
  328. return ScalingHelpers::scaledScreenPosToUnscaled
  329. (component, component.getLocalArea (&subComponent, subComponent.getLocalBounds()));
  330. }
  331. //==============================================================================
  332. namespace DragHelpers
  333. {
  334. static bool isFileDrag (const ComponentPeer::DragInfo& info)
  335. {
  336. return ! info.files.isEmpty();
  337. }
  338. static bool isSuitableTarget (const ComponentPeer::DragInfo& info, Component* target)
  339. {
  340. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target) != nullptr
  341. : dynamic_cast<TextDragAndDropTarget*> (target) != nullptr;
  342. }
  343. static bool isInterested (const ComponentPeer::DragInfo& info, Component* target)
  344. {
  345. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target)->isInterestedInFileDrag (info.files)
  346. : dynamic_cast<TextDragAndDropTarget*> (target)->isInterestedInTextDrag (info.text);
  347. }
  348. static Component* findDragAndDropTarget (Component* c, const ComponentPeer::DragInfo& info, Component* lastOne)
  349. {
  350. for (; c != nullptr; c = c->getParentComponent())
  351. if (isSuitableTarget (info, c) && (c == lastOne || isInterested (info, c)))
  352. return c;
  353. return nullptr;
  354. }
  355. }
  356. bool ComponentPeer::handleDragMove (const ComponentPeer::DragInfo& info)
  357. {
  358. auto* compUnderMouse = component.getComponentAt (info.position);
  359. auto* lastTarget = dragAndDropTargetComponent.get();
  360. Component* newTarget = nullptr;
  361. if (compUnderMouse != lastDragAndDropCompUnderMouse)
  362. {
  363. lastDragAndDropCompUnderMouse = compUnderMouse;
  364. newTarget = DragHelpers::findDragAndDropTarget (compUnderMouse, info, lastTarget);
  365. if (newTarget != lastTarget)
  366. {
  367. if (lastTarget != nullptr)
  368. {
  369. if (DragHelpers::isFileDrag (info))
  370. dynamic_cast<FileDragAndDropTarget*> (lastTarget)->fileDragExit (info.files);
  371. else
  372. dynamic_cast<TextDragAndDropTarget*> (lastTarget)->textDragExit (info.text);
  373. }
  374. dragAndDropTargetComponent = nullptr;
  375. if (DragHelpers::isSuitableTarget (info, newTarget))
  376. {
  377. dragAndDropTargetComponent = newTarget;
  378. auto pos = newTarget->getLocalPoint (&component, info.position);
  379. if (DragHelpers::isFileDrag (info))
  380. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragEnter (info.files, pos.x, pos.y);
  381. else
  382. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragEnter (info.text, pos.x, pos.y);
  383. }
  384. }
  385. }
  386. else
  387. {
  388. newTarget = lastTarget;
  389. }
  390. if (! DragHelpers::isSuitableTarget (info, newTarget))
  391. return false;
  392. auto pos = newTarget->getLocalPoint (&component, info.position);
  393. if (DragHelpers::isFileDrag (info))
  394. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragMove (info.files, pos.x, pos.y);
  395. else
  396. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragMove (info.text, pos.x, pos.y);
  397. return true;
  398. }
  399. bool ComponentPeer::handleDragExit (const ComponentPeer::DragInfo& info)
  400. {
  401. DragInfo info2 (info);
  402. info2.position.setXY (-1, -1);
  403. const bool used = handleDragMove (info2);
  404. jassert (dragAndDropTargetComponent == nullptr);
  405. lastDragAndDropCompUnderMouse = nullptr;
  406. return used;
  407. }
  408. bool ComponentPeer::handleDragDrop (const ComponentPeer::DragInfo& info)
  409. {
  410. handleDragMove (info);
  411. if (WeakReference<Component> targetComp = dragAndDropTargetComponent)
  412. {
  413. dragAndDropTargetComponent = nullptr;
  414. lastDragAndDropCompUnderMouse = nullptr;
  415. if (DragHelpers::isSuitableTarget (info, targetComp))
  416. {
  417. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  418. {
  419. targetComp->internalModalInputAttempt();
  420. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  421. return true;
  422. }
  423. ComponentPeer::DragInfo infoCopy (info);
  424. infoCopy.position = targetComp->getLocalPoint (&component, info.position);
  425. // We'll use an async message to deliver the drop, because if the target decides
  426. // to run a modal loop, it can gum-up the operating system..
  427. MessageManager::callAsync ([=]
  428. {
  429. if (auto* c = targetComp.get())
  430. {
  431. if (DragHelpers::isFileDrag (info))
  432. dynamic_cast<FileDragAndDropTarget*> (c)->filesDropped (infoCopy.files, infoCopy.position.x, infoCopy.position.y);
  433. else
  434. dynamic_cast<TextDragAndDropTarget*> (c)->textDropped (infoCopy.text, infoCopy.position.x, infoCopy.position.y);
  435. }
  436. });
  437. return true;
  438. }
  439. }
  440. return false;
  441. }
  442. //==============================================================================
  443. void ComponentPeer::handleUserClosingWindow()
  444. {
  445. component.userTriedToCloseWindow();
  446. }
  447. bool ComponentPeer::setDocumentEditedStatus (bool)
  448. {
  449. return false;
  450. }
  451. void ComponentPeer::setRepresentedFile (const File&)
  452. {
  453. }
  454. //==============================================================================
  455. int ComponentPeer::getCurrentRenderingEngine() const { return 0; }
  456. void ComponentPeer::setCurrentRenderingEngine (int index) { jassert (index == 0); ignoreUnused (index); }
  457. //==============================================================================
  458. std::function<ModifierKeys()> ComponentPeer::getNativeRealtimeModifiers = nullptr;
  459. ModifierKeys ComponentPeer::getCurrentModifiersRealtime() noexcept
  460. {
  461. if (getNativeRealtimeModifiers != nullptr)
  462. return getNativeRealtimeModifiers();
  463. return ModifierKeys::currentModifiers;
  464. }
  465. //==============================================================================
  466. void ComponentPeer::forceDisplayUpdate()
  467. {
  468. Desktop::getInstance().displays->refresh();
  469. }
  470. } // namespace juce