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.

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