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.

juce_ComponentPeer.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - 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 this technical preview, this file is not subject to commercial licensing.
  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 (peerBounds.getWidth() / (float) componentBounds.getWidth(),
  88. 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. if (auto* currentlyFocused = Component::getCurrentlyFocusedComponent())
  159. {
  160. const bool isTab = (keyInfo == KeyPress::tabKey);
  161. const bool isShiftTab = (keyInfo == KeyPress (KeyPress::tabKey, ModifierKeys::shiftModifier, 0));
  162. if (isTab || isShiftTab)
  163. {
  164. currentlyFocused->moveKeyboardFocusToSibling (isTab);
  165. keyWasUsed = (currentlyFocused != Component::getCurrentlyFocusedComponent());
  166. if (keyWasUsed || deletionChecker == nullptr)
  167. break;
  168. }
  169. }
  170. }
  171. return keyWasUsed;
  172. }
  173. bool ComponentPeer::handleKeyUpOrDown (const bool isKeyDown)
  174. {
  175. bool keyWasUsed = false;
  176. for (auto* target = getTargetForKeyPress(); target != nullptr; target = target->getParentComponent())
  177. {
  178. const WeakReference<Component> deletionChecker (target);
  179. keyWasUsed = target->keyStateChanged (isKeyDown);
  180. if (keyWasUsed || deletionChecker == nullptr)
  181. break;
  182. if (auto* keyListeners = target->keyListeners.get())
  183. {
  184. for (int i = keyListeners->size(); --i >= 0;)
  185. {
  186. keyWasUsed = keyListeners->getUnchecked(i)->keyStateChanged (isKeyDown, target);
  187. if (keyWasUsed || deletionChecker == nullptr)
  188. return keyWasUsed;
  189. i = jmin (i, keyListeners->size());
  190. }
  191. }
  192. }
  193. return keyWasUsed;
  194. }
  195. void ComponentPeer::handleModifierKeysChange()
  196. {
  197. auto* target = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  198. if (target == nullptr)
  199. target = Component::getCurrentlyFocusedComponent();
  200. if (target == nullptr)
  201. target = &component;
  202. target->internalModifierKeysChanged();
  203. }
  204. TextInputTarget* ComponentPeer::findCurrentTextInputTarget()
  205. {
  206. auto* c = Component::getCurrentlyFocusedComponent();
  207. if (c == &component || component.isParentOf (c))
  208. if (auto* ti = dynamic_cast<TextInputTarget*> (c))
  209. if (ti->isTextInputActive())
  210. return ti;
  211. return nullptr;
  212. }
  213. void ComponentPeer::dismissPendingTextInput() {}
  214. //==============================================================================
  215. void ComponentPeer::handleBroughtToFront()
  216. {
  217. component.internalBroughtToFront();
  218. }
  219. void ComponentPeer::setConstrainer (ComponentBoundsConstrainer* const newConstrainer) noexcept
  220. {
  221. constrainer = newConstrainer;
  222. }
  223. void ComponentPeer::handleMovedOrResized()
  224. {
  225. const bool nowMinimised = isMinimised();
  226. if (component.flags.hasHeavyweightPeerFlag && ! nowMinimised)
  227. {
  228. const WeakReference<Component> deletionChecker (&component);
  229. auto newBounds = Component::ComponentHelpers::rawPeerPositionToLocal (component, getBounds());
  230. auto oldBounds = component.getBounds();
  231. const bool wasMoved = (oldBounds.getPosition() != newBounds.getPosition());
  232. const bool wasResized = (oldBounds.getWidth() != newBounds.getWidth() || oldBounds.getHeight() != newBounds.getHeight());
  233. if (wasMoved || wasResized)
  234. {
  235. component.boundsRelativeToParent = newBounds;
  236. if (wasResized)
  237. component.repaint();
  238. component.sendMovedResizedMessages (wasMoved, wasResized);
  239. if (deletionChecker == nullptr)
  240. return;
  241. }
  242. }
  243. if (isWindowMinimised != nowMinimised)
  244. {
  245. isWindowMinimised = nowMinimised;
  246. component.minimisationStateChanged (nowMinimised);
  247. component.sendVisibilityChangeMessage();
  248. }
  249. if (! isFullScreen())
  250. lastNonFullscreenBounds = component.getBounds();
  251. }
  252. void ComponentPeer::handleFocusGain()
  253. {
  254. if (component.isParentOf (lastFocusedComponent)
  255. && lastFocusedComponent->isShowing()
  256. && lastFocusedComponent->getWantsKeyboardFocus())
  257. {
  258. Component::currentlyFocusedComponent = lastFocusedComponent;
  259. Desktop::getInstance().triggerFocusCallback();
  260. lastFocusedComponent->internalFocusGain (Component::focusChangedDirectly);
  261. }
  262. else
  263. {
  264. if (! component.isCurrentlyBlockedByAnotherModalComponent())
  265. component.grabKeyboardFocus();
  266. else
  267. ModalComponentManager::getInstance()->bringModalComponentsToFront();
  268. }
  269. }
  270. void ComponentPeer::handleFocusLoss()
  271. {
  272. if (component.hasKeyboardFocus (true))
  273. {
  274. lastFocusedComponent = Component::currentlyFocusedComponent;
  275. if (lastFocusedComponent != nullptr)
  276. {
  277. Component::currentlyFocusedComponent = nullptr;
  278. Desktop::getInstance().triggerFocusCallback();
  279. lastFocusedComponent->internalFocusLoss (Component::focusChangedByMouseClick);
  280. }
  281. }
  282. }
  283. Component* ComponentPeer::getLastFocusedSubcomponent() const noexcept
  284. {
  285. return (component.isParentOf (lastFocusedComponent) && lastFocusedComponent->isShowing())
  286. ? static_cast<Component*> (lastFocusedComponent)
  287. : &component;
  288. }
  289. void ComponentPeer::handleScreenSizeChange()
  290. {
  291. component.parentSizeChanged();
  292. handleMovedOrResized();
  293. }
  294. void ComponentPeer::setNonFullScreenBounds (const Rectangle<int>& newBounds) noexcept
  295. {
  296. lastNonFullscreenBounds = newBounds;
  297. }
  298. const Rectangle<int>& ComponentPeer::getNonFullScreenBounds() const noexcept
  299. {
  300. return lastNonFullscreenBounds;
  301. }
  302. Point<int> ComponentPeer::localToGlobal (Point<int> p) { return localToGlobal (p.toFloat()).roundToInt(); }
  303. Point<int> ComponentPeer::globalToLocal (Point<int> p) { return globalToLocal (p.toFloat()).roundToInt(); }
  304. Rectangle<int> ComponentPeer::localToGlobal (const Rectangle<int>& relativePosition)
  305. {
  306. return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
  307. }
  308. Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPosition)
  309. {
  310. return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
  311. }
  312. Rectangle<int> ComponentPeer::getAreaCoveredBy (Component& subComponent) const
  313. {
  314. return ScalingHelpers::scaledScreenPosToUnscaled
  315. (component, component.getLocalArea (&subComponent, subComponent.getLocalBounds()));
  316. }
  317. //==============================================================================
  318. namespace DragHelpers
  319. {
  320. static bool isFileDrag (const ComponentPeer::DragInfo& info)
  321. {
  322. return ! info.files.isEmpty();
  323. }
  324. static bool isSuitableTarget (const ComponentPeer::DragInfo& info, Component* target)
  325. {
  326. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target) != nullptr
  327. : dynamic_cast<TextDragAndDropTarget*> (target) != nullptr;
  328. }
  329. static bool isInterested (const ComponentPeer::DragInfo& info, Component* target)
  330. {
  331. return isFileDrag (info) ? dynamic_cast<FileDragAndDropTarget*> (target)->isInterestedInFileDrag (info.files)
  332. : dynamic_cast<TextDragAndDropTarget*> (target)->isInterestedInTextDrag (info.text);
  333. }
  334. static Component* findDragAndDropTarget (Component* c, const ComponentPeer::DragInfo& info, Component* lastOne)
  335. {
  336. for (; c != nullptr; c = c->getParentComponent())
  337. if (isSuitableTarget (info, c) && (c == lastOne || isInterested (info, c)))
  338. return c;
  339. return nullptr;
  340. }
  341. }
  342. bool ComponentPeer::handleDragMove (const ComponentPeer::DragInfo& info)
  343. {
  344. auto* compUnderMouse = component.getComponentAt (info.position);
  345. auto* lastTarget = dragAndDropTargetComponent.get();
  346. Component* newTarget = nullptr;
  347. if (compUnderMouse != lastDragAndDropCompUnderMouse)
  348. {
  349. lastDragAndDropCompUnderMouse = compUnderMouse;
  350. newTarget = DragHelpers::findDragAndDropTarget (compUnderMouse, info, lastTarget);
  351. if (newTarget != lastTarget)
  352. {
  353. if (lastTarget != nullptr)
  354. {
  355. if (DragHelpers::isFileDrag (info))
  356. dynamic_cast<FileDragAndDropTarget*> (lastTarget)->fileDragExit (info.files);
  357. else
  358. dynamic_cast<TextDragAndDropTarget*> (lastTarget)->textDragExit (info.text);
  359. }
  360. dragAndDropTargetComponent = nullptr;
  361. if (DragHelpers::isSuitableTarget (info, newTarget))
  362. {
  363. dragAndDropTargetComponent = newTarget;
  364. auto pos = newTarget->getLocalPoint (&component, info.position);
  365. if (DragHelpers::isFileDrag (info))
  366. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragEnter (info.files, pos.x, pos.y);
  367. else
  368. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragEnter (info.text, pos.x, pos.y);
  369. }
  370. }
  371. }
  372. else
  373. {
  374. newTarget = lastTarget;
  375. }
  376. if (! DragHelpers::isSuitableTarget (info, newTarget))
  377. return false;
  378. auto pos = newTarget->getLocalPoint (&component, info.position);
  379. if (DragHelpers::isFileDrag (info))
  380. dynamic_cast<FileDragAndDropTarget*> (newTarget)->fileDragMove (info.files, pos.x, pos.y);
  381. else
  382. dynamic_cast<TextDragAndDropTarget*> (newTarget)->textDragMove (info.text, pos.x, pos.y);
  383. return true;
  384. }
  385. bool ComponentPeer::handleDragExit (const ComponentPeer::DragInfo& info)
  386. {
  387. DragInfo info2 (info);
  388. info2.position.setXY (-1, -1);
  389. const bool used = handleDragMove (info2);
  390. jassert (dragAndDropTargetComponent == nullptr);
  391. lastDragAndDropCompUnderMouse = nullptr;
  392. return used;
  393. }
  394. bool ComponentPeer::handleDragDrop (const ComponentPeer::DragInfo& info)
  395. {
  396. handleDragMove (info);
  397. if (WeakReference<Component> targetComp = dragAndDropTargetComponent)
  398. {
  399. dragAndDropTargetComponent = nullptr;
  400. lastDragAndDropCompUnderMouse = nullptr;
  401. if (DragHelpers::isSuitableTarget (info, targetComp))
  402. {
  403. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  404. {
  405. targetComp->internalModalInputAttempt();
  406. if (targetComp->isCurrentlyBlockedByAnotherModalComponent())
  407. return true;
  408. }
  409. ComponentPeer::DragInfo infoCopy (info);
  410. infoCopy.position = targetComp->getLocalPoint (&component, info.position);
  411. // We'll use an async message to deliver the drop, because if the target decides
  412. // to run a modal loop, it can gum-up the operating system..
  413. MessageManager::callAsync ([=]
  414. {
  415. if (auto* c = targetComp.get())
  416. {
  417. if (DragHelpers::isFileDrag (info))
  418. dynamic_cast<FileDragAndDropTarget*> (c)->filesDropped (infoCopy.files, infoCopy.position.x, infoCopy.position.y);
  419. else
  420. dynamic_cast<TextDragAndDropTarget*> (c)->textDropped (infoCopy.text, infoCopy.position.x, infoCopy.position.y);
  421. }
  422. });
  423. return true;
  424. }
  425. }
  426. return false;
  427. }
  428. //==============================================================================
  429. void ComponentPeer::handleUserClosingWindow()
  430. {
  431. component.userTriedToCloseWindow();
  432. }
  433. bool ComponentPeer::setDocumentEditedStatus (bool)
  434. {
  435. return false;
  436. }
  437. void ComponentPeer::setRepresentedFile (const File&)
  438. {
  439. }
  440. //==============================================================================
  441. int ComponentPeer::getCurrentRenderingEngine() const { return 0; }
  442. void ComponentPeer::setCurrentRenderingEngine (int index) { jassert (index == 0); ignoreUnused (index); }
  443. //==============================================================================
  444. std::function<ModifierKeys()> ComponentPeer::getNativeRealtimeModifiers = nullptr;
  445. ModifierKeys ComponentPeer::getCurrentModifiersRealtime() noexcept
  446. {
  447. if (getNativeRealtimeModifiers != nullptr)
  448. return getNativeRealtimeModifiers();
  449. return ModifierKeys::currentModifiers;
  450. }
  451. } // namespace juce